rv3029.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. #ifdef RTC_DEBUG
  77. printf( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  78. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  79. tmp->tm_hour, tmp->tm_min, tmp->tm_sec );
  80. #endif
  81. return 0;
  82. }
  83. int rtc_set( struct rtc_time *tmp )
  84. {
  85. int ret;
  86. unsigned char buf[RTC_RV3029_PAGE_LEN];
  87. #ifdef RTC_DEBUG
  88. printf( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  89. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  90. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  91. #endif
  92. if (tmp->tm_year < 2000) {
  93. printf("RTC: year %d < 2000 not possible\n", tmp->tm_year);
  94. return -1;
  95. }
  96. buf[RV3029C2_W_SECONDS] = bin2bcd(tmp->tm_sec);
  97. buf[RV3029C2_W_MINUTES] = bin2bcd(tmp->tm_min);
  98. buf[RV3029C2_W_HOURS] = bin2bcd(tmp->tm_hour);
  99. /* set 24h format */
  100. buf[RV3029C2_W_HOURS] &= ~RV3029C2_REG_HR_12_24;
  101. buf[RV3029C2_W_DATE] = bin2bcd(tmp->tm_mday);
  102. buf[RV3029C2_W_DAYS] = bin2bcd(tmp->tm_wday);
  103. buf[RV3029C2_W_MONTHS] = bin2bcd(tmp->tm_mon);
  104. tmp->tm_year -= 2000;
  105. buf[RV3029C2_W_YEARS] = bin2bcd(tmp->tm_year);
  106. ret = i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CLOCK_PAGE, 1,
  107. buf, RTC_RV3029_PAGE_LEN);
  108. /* give the RTC some time to update */
  109. udelay(1000);
  110. return 0;
  111. }
  112. /* sets EERE-Bit (automatic EEPROM refresh) */
  113. static void set_eere_bit(int state)
  114. {
  115. int ret;
  116. unsigned char reg_ctrl1;
  117. ret = i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CTRL1, 1,
  118. &reg_ctrl1, 1);
  119. if (state)
  120. reg_ctrl1 |= RTC_RV3029_CTRL1_EERE;
  121. else
  122. reg_ctrl1 &= (~RTC_RV3029_CTRL1_EERE);
  123. ret = i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CTRL1, 1,
  124. &reg_ctrl1, 1);
  125. }
  126. /* waits until EEPROM page is no longer busy (times out after 10ms*loops) */
  127. static int wait_eebusy(int loops)
  128. {
  129. int i, ret;
  130. unsigned char ctrl_status;
  131. for (i = 0; i < loops; i++) {
  132. ret = i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CTRL_STATUS,
  133. 1, &ctrl_status, 1);
  134. if ((ctrl_status & RTC_RV3029_CTRLS_EEBUSY) == 0)
  135. break;
  136. udelay(10000);
  137. }
  138. return i;
  139. }
  140. void rtc_reset (void)
  141. {
  142. int ret;
  143. unsigned char buf[RTC_RV3029_PAGE_LEN];
  144. buf[0] = RTC_RV3029_CTRL_SYS_R;
  145. ret = i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CTRL_RESET, 1,
  146. buf, 1);
  147. #if defined(CONFIG_SYS_RV3029_TCR)
  148. /*
  149. * because EEPROM_CTRL register is in EEPROM page it is necessary to
  150. * disable automatic EEPROM refresh and check if EEPROM is busy
  151. * before EEPORM_CTRL register may be accessed
  152. */
  153. set_eere_bit(0);
  154. wait_eebusy(100);
  155. /* read current trickle charger setting */
  156. ret = i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_EEPROM_CTRL,
  157. 1, buf, 1);
  158. /* enable automatic EEPROM refresh again */
  159. set_eere_bit(1);
  160. /*
  161. * to minimize EEPROM access write trickle charger setting only if it
  162. * differs from current value
  163. */
  164. if ((buf[0] & 0xF0) != CONFIG_SYS_RV3029_TCR) {
  165. buf[0] = (buf[0] & 0x0F) | CONFIG_SYS_RV3029_TCR;
  166. /*
  167. * write trickle charger setting (disable autom. EEPROM
  168. * refresh and wait until EEPROM is idle)
  169. */
  170. set_eere_bit(0);
  171. wait_eebusy(100);
  172. ret = i2c_write(CONFIG_SYS_I2C_RTC_ADDR,
  173. RTC_RV3029_EEPROM_CTRL, 1, buf, 1);
  174. /*
  175. * it is necessary to wait 10ms before EEBUSY-Bit may be read
  176. * (this is not documented in the data sheet yet, but the
  177. * manufacturer recommends it)
  178. */
  179. udelay(10000);
  180. /* wait until EEPROM write access is finished */
  181. wait_eebusy(100);
  182. set_eere_bit(1);
  183. }
  184. #endif
  185. }