rv3029.c 5.9 KB

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