rv3029.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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_CTRL_RESET 0x04
  28. #define RTC_RV3029_CTRL_SYS_R (1 << 4)
  29. #define RTC_RV3029_CLOCK_PAGE 0x08
  30. #define RTC_RV3029_PAGE_LEN 7
  31. #define RV3029C2_W_SECONDS 0x00
  32. #define RV3029C2_W_MINUTES 0x01
  33. #define RV3029C2_W_HOURS 0x02
  34. #define RV3029C2_W_DATE 0x03
  35. #define RV3029C2_W_DAYS 0x04
  36. #define RV3029C2_W_MONTHS 0x05
  37. #define RV3029C2_W_YEARS 0x06
  38. #define RV3029C2_REG_HR_12_24 (1 << 6) /* 24h/12h mode */
  39. #define RV3029C2_REG_HR_PM (1 << 5) /* PM/AM bit in 12h mode */
  40. int rtc_get( struct rtc_time *tmp )
  41. {
  42. int ret;
  43. unsigned char buf[RTC_RV3029_PAGE_LEN];
  44. ret = i2c_read(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CLOCK_PAGE, 1, buf, \
  45. RTC_RV3029_PAGE_LEN);
  46. if (ret) {
  47. printf("%s: error reading RTC: %x\n", __func__, ret);
  48. return -1;
  49. }
  50. tmp->tm_sec = bcd2bin( buf[RV3029C2_W_SECONDS] & 0x7f);
  51. tmp->tm_min = bcd2bin( buf[RV3029C2_W_MINUTES] & 0x7f);
  52. if (buf[RV3029C2_W_HOURS] & RV3029C2_REG_HR_12_24) {
  53. /* 12h format */
  54. tmp->tm_hour = bcd2bin(buf[RV3029C2_W_HOURS] & 0x1f);
  55. if (buf[RV3029C2_W_HOURS] & RV3029C2_REG_HR_PM)
  56. /* PM flag set */
  57. tmp->tm_hour += 12;
  58. } else
  59. tmp->tm_hour = bcd2bin(buf[RV3029C2_W_HOURS] & 0x3f);
  60. tmp->tm_mday = bcd2bin( buf[RV3029C2_W_DATE] & 0x3F );
  61. tmp->tm_mon = bcd2bin( buf[RV3029C2_W_MONTHS] & 0x1F );
  62. tmp->tm_wday = bcd2bin( buf[RV3029C2_W_DAYS] & 0x07 );
  63. /* RTC supports only years > 1999 */
  64. tmp->tm_year = bcd2bin( buf[RV3029C2_W_YEARS]) + 2000;
  65. tmp->tm_yday = 0;
  66. tmp->tm_isdst = 0;
  67. #ifdef RTC_DEBUG
  68. printf( "Get 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. #endif
  72. return 0;
  73. }
  74. int rtc_set( struct rtc_time *tmp )
  75. {
  76. int ret;
  77. unsigned char buf[RTC_RV3029_PAGE_LEN];
  78. #ifdef RTC_DEBUG
  79. printf( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  80. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  81. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  82. #endif
  83. if (tmp->tm_year < 2000) {
  84. printf("RTC: year %d < 2000 not possible\n", tmp->tm_year);
  85. return -1;
  86. }
  87. buf[RV3029C2_W_SECONDS] = bin2bcd(tmp->tm_sec);
  88. buf[RV3029C2_W_MINUTES] = bin2bcd(tmp->tm_min);
  89. buf[RV3029C2_W_HOURS] = bin2bcd(tmp->tm_hour);
  90. /* set 24h format */
  91. buf[RV3029C2_W_HOURS] &= ~RV3029C2_REG_HR_12_24;
  92. buf[RV3029C2_W_DATE] = bin2bcd(tmp->tm_mday);
  93. buf[RV3029C2_W_DAYS] = bin2bcd(tmp->tm_wday);
  94. buf[RV3029C2_W_MONTHS] = bin2bcd(tmp->tm_mon);
  95. tmp->tm_year -= 2000;
  96. buf[RV3029C2_W_YEARS] = bin2bcd(tmp->tm_year);
  97. ret = i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CLOCK_PAGE, 1,
  98. buf, RTC_RV3029_PAGE_LEN);
  99. /* give the RTC some time to update */
  100. udelay(1000);
  101. return 0;
  102. }
  103. void rtc_reset (void)
  104. {
  105. int ret;
  106. unsigned char buf[RTC_RV3029_PAGE_LEN];
  107. buf[0] = RTC_RV3029_CTRL_SYS_R;
  108. ret = i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_RV3029_CTRL_RESET, 1,
  109. buf, 1);
  110. }