rv3029.c 5.2 KB

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