rv3029.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * (C) Copyright 2010
  3. * Heiko Schocher, DENX Software Engineering, hs@denx.de
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <command.h>
  9. #include <i2c.h>
  10. #include <rtc.h>
  11. #define RTC_RV3029_CTRL1 0x00
  12. #define RTC_RV3029_CTRL1_EERE (1 << 3)
  13. #define RTC_RV3029_CTRL_STATUS 0x03
  14. #define RTC_RV3029_CTRLS_EEBUSY (1 << 7)
  15. #define RTC_RV3029_CTRL_RESET 0x04
  16. #define RTC_RV3029_CTRL_SYS_R (1 << 4)
  17. #define RTC_RV3029_CLOCK_PAGE 0x08
  18. #define RTC_RV3029_PAGE_LEN 7
  19. #define RV3029C2_W_SECONDS 0x00
  20. #define RV3029C2_W_MINUTES 0x01
  21. #define RV3029C2_W_HOURS 0x02
  22. #define RV3029C2_W_DATE 0x03
  23. #define RV3029C2_W_DAYS 0x04
  24. #define RV3029C2_W_MONTHS 0x05
  25. #define RV3029C2_W_YEARS 0x06
  26. #define RV3029C2_REG_HR_12_24 (1 << 6) /* 24h/12h mode */
  27. #define RV3029C2_REG_HR_PM (1 << 5) /* PM/AM bit in 12h mode */
  28. #define RTC_RV3029_EEPROM_CTRL 0x30
  29. #define RTC_RV3029_TRICKLE_1K (1 << 4)
  30. #define RTC_RV3029_TRICKLE_5K (1 << 5)
  31. #define RTC_RV3029_TRICKLE_20K (1 << 6)
  32. #define RTC_RV3029_TRICKLE_80K (1 << 7)
  33. int rtc_get( struct rtc_time *tmp )
  34. {
  35. int ret;
  36. unsigned char buf[RTC_RV3029_PAGE_LEN];
  37. ret = i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CLOCK_PAGE, 1, buf, \
  38. RTC_RV3029_PAGE_LEN);
  39. if (ret) {
  40. printf("%s: error reading RTC: %x\n", __func__, ret);
  41. return -1;
  42. }
  43. tmp->tm_sec = bcd2bin( buf[RV3029C2_W_SECONDS] & 0x7f);
  44. tmp->tm_min = bcd2bin( buf[RV3029C2_W_MINUTES] & 0x7f);
  45. if (buf[RV3029C2_W_HOURS] & RV3029C2_REG_HR_12_24) {
  46. /* 12h format */
  47. tmp->tm_hour = bcd2bin(buf[RV3029C2_W_HOURS] & 0x1f);
  48. if (buf[RV3029C2_W_HOURS] & RV3029C2_REG_HR_PM)
  49. /* PM flag set */
  50. tmp->tm_hour += 12;
  51. } else
  52. tmp->tm_hour = bcd2bin(buf[RV3029C2_W_HOURS] & 0x3f);
  53. tmp->tm_mday = bcd2bin( buf[RV3029C2_W_DATE] & 0x3F );
  54. tmp->tm_mon = bcd2bin( buf[RV3029C2_W_MONTHS] & 0x1F );
  55. tmp->tm_wday = bcd2bin( buf[RV3029C2_W_DAYS] & 0x07 );
  56. /* RTC supports only years > 1999 */
  57. tmp->tm_year = bcd2bin( buf[RV3029C2_W_YEARS]) + 2000;
  58. tmp->tm_yday = 0;
  59. tmp->tm_isdst = 0;
  60. debug( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  61. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  62. tmp->tm_hour, tmp->tm_min, tmp->tm_sec );
  63. return 0;
  64. }
  65. int rtc_set( struct rtc_time *tmp )
  66. {
  67. int ret;
  68. unsigned char buf[RTC_RV3029_PAGE_LEN];
  69. debug( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  70. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  71. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  72. if (tmp->tm_year < 2000) {
  73. printf("RTC: year %d < 2000 not possible\n", tmp->tm_year);
  74. return -1;
  75. }
  76. buf[RV3029C2_W_SECONDS] = bin2bcd(tmp->tm_sec);
  77. buf[RV3029C2_W_MINUTES] = bin2bcd(tmp->tm_min);
  78. buf[RV3029C2_W_HOURS] = bin2bcd(tmp->tm_hour);
  79. /* set 24h format */
  80. buf[RV3029C2_W_HOURS] &= ~RV3029C2_REG_HR_12_24;
  81. buf[RV3029C2_W_DATE] = bin2bcd(tmp->tm_mday);
  82. buf[RV3029C2_W_DAYS] = bin2bcd(tmp->tm_wday);
  83. buf[RV3029C2_W_MONTHS] = bin2bcd(tmp->tm_mon);
  84. tmp->tm_year -= 2000;
  85. buf[RV3029C2_W_YEARS] = bin2bcd(tmp->tm_year);
  86. ret = i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CLOCK_PAGE, 1,
  87. buf, RTC_RV3029_PAGE_LEN);
  88. /* give the RTC some time to update */
  89. udelay(1000);
  90. return ret;
  91. }
  92. /* sets EERE-Bit (automatic EEPROM refresh) */
  93. static void set_eere_bit(int state)
  94. {
  95. unsigned char reg_ctrl1;
  96. (void)i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CTRL1, 1,
  97. &reg_ctrl1, 1);
  98. if (state)
  99. reg_ctrl1 |= RTC_RV3029_CTRL1_EERE;
  100. else
  101. reg_ctrl1 &= (~RTC_RV3029_CTRL1_EERE);
  102. (void)i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CTRL1, 1,
  103. &reg_ctrl1, 1);
  104. }
  105. /* waits until EEPROM page is no longer busy (times out after 10ms*loops) */
  106. static int wait_eebusy(int loops)
  107. {
  108. int i;
  109. unsigned char ctrl_status;
  110. for (i = 0; i < loops; i++) {
  111. (void)i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CTRL_STATUS,
  112. 1, &ctrl_status, 1);
  113. if ((ctrl_status & RTC_RV3029_CTRLS_EEBUSY) == 0)
  114. break;
  115. udelay(10000);
  116. }
  117. return i;
  118. }
  119. void rtc_reset (void)
  120. {
  121. unsigned char buf[RTC_RV3029_PAGE_LEN];
  122. buf[0] = RTC_RV3029_CTRL_SYS_R;
  123. (void)i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CTRL_RESET, 1,
  124. buf, 1);
  125. #if defined(CONFIG_SYS_RV3029_TCR)
  126. /*
  127. * because EEPROM_CTRL register is in EEPROM page it is necessary to
  128. * disable automatic EEPROM refresh and check if EEPROM is busy
  129. * before EEPORM_CTRL register may be accessed
  130. */
  131. set_eere_bit(0);
  132. wait_eebusy(100);
  133. /* read current trickle charger setting */
  134. (void)i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_EEPROM_CTRL,
  135. 1, buf, 1);
  136. /* enable automatic EEPROM refresh again */
  137. set_eere_bit(1);
  138. /*
  139. * to minimize EEPROM access write trickle charger setting only if it
  140. * differs from current value
  141. */
  142. if ((buf[0] & 0xF0) != CONFIG_SYS_RV3029_TCR) {
  143. buf[0] = (buf[0] & 0x0F) | CONFIG_SYS_RV3029_TCR;
  144. /*
  145. * write trickle charger setting (disable autom. EEPROM
  146. * refresh and wait until EEPROM is idle)
  147. */
  148. set_eere_bit(0);
  149. wait_eebusy(100);
  150. (void)i2c_write(CONFIG_SYS_I2C_RTC_ADDR,
  151. RTC_RV3029_EEPROM_CTRL, 1, buf, 1);
  152. /*
  153. * it is necessary to wait 10ms before EEBUSY-Bit may be read
  154. * (this is not documented in the data sheet yet, but the
  155. * manufacturer recommends it)
  156. */
  157. udelay(10000);
  158. /* wait until EEPROM write access is finished */
  159. wait_eebusy(100);
  160. set_eere_bit(1);
  161. }
  162. #endif
  163. }