rx8025.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * (C) Copyright 2007
  3. * Matthias Fuchs, esd gmbh, matthias.fuchs@esd-electronics.com.
  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. /*
  24. * Epson RX8025 RTC driver.
  25. */
  26. #include <common.h>
  27. #include <command.h>
  28. #include <rtc.h>
  29. #include <i2c.h>
  30. #if defined(CONFIG_RTC_RX8025) && defined(CONFIG_CMD_DATE)
  31. /*---------------------------------------------------------------------*/
  32. #undef DEBUG_RTC
  33. #ifdef DEBUG_RTC
  34. #define DEBUGR(fmt,args...) printf(fmt ,##args)
  35. #else
  36. #define DEBUGR(fmt,args...)
  37. #endif
  38. /*---------------------------------------------------------------------*/
  39. #ifndef CFG_I2C_RTC_ADDR
  40. # define CFG_I2C_RTC_ADDR 0x32
  41. #endif
  42. /*
  43. * RTC register addresses
  44. */
  45. #define RTC_SEC_REG_ADDR 0x00
  46. #define RTC_MIN_REG_ADDR 0x01
  47. #define RTC_HR_REG_ADDR 0x02
  48. #define RTC_DAY_REG_ADDR 0x03
  49. #define RTC_DATE_REG_ADDR 0x04
  50. #define RTC_MON_REG_ADDR 0x05
  51. #define RTC_YR_REG_ADDR 0x06
  52. #define RTC_CTL1_REG_ADDR 0x0e
  53. #define RTC_CTL2_REG_ADDR 0x0f
  54. /*
  55. * Control register 1 bits
  56. */
  57. #define RTC_CTL1_BIT_2412 0x20
  58. /*
  59. * Control register 2 bits
  60. */
  61. #define RTC_CTL2_BIT_PON 0x10
  62. #define RTC_CTL2_BIT_VDET 0x40
  63. #define RTC_CTL2_BIT_XST 0x20
  64. #define RTC_CTL2_BIT_VDSL 0x80
  65. /*
  66. * Note: the RX8025 I2C RTC requires register
  67. * reads and write to consist of a single bus
  68. * cycle. It is not allowed to write the register
  69. * address in a first cycle that is terminated by
  70. * a STOP condition. The chips needs a 'restart'
  71. * sequence (start sequence without a prior stop).
  72. * This driver has been written for a 4xx board.
  73. * U-Boot's 4xx i2c driver is currently not capable
  74. * to generate such cycles to some work arounds
  75. * are used.
  76. */
  77. /* static uchar rtc_read (uchar reg); */
  78. #define rtc_read(reg) buf[((reg) + 1) & 0xf]
  79. static void rtc_write (uchar reg, uchar val);
  80. static uchar bin2bcd (unsigned int n);
  81. static unsigned bcd2bin (uchar c);
  82. /*
  83. * Get the current time from the RTC
  84. */
  85. void rtc_get (struct rtc_time *tmp)
  86. {
  87. uchar sec, min, hour, mday, wday, mon, year, ctl2;
  88. uchar buf[16];
  89. if (i2c_read(CFG_I2C_RTC_ADDR, 0, 0, buf, 16))
  90. printf("Error reading from RTC\n");
  91. sec = rtc_read(RTC_SEC_REG_ADDR);
  92. min = rtc_read(RTC_MIN_REG_ADDR);
  93. hour = rtc_read(RTC_HR_REG_ADDR);
  94. wday = rtc_read(RTC_DAY_REG_ADDR);
  95. mday = rtc_read(RTC_DATE_REG_ADDR);
  96. mon = rtc_read(RTC_MON_REG_ADDR);
  97. year = rtc_read(RTC_YR_REG_ADDR);
  98. DEBUGR ("Get RTC year: %02x mon: %02x mday: %02x wday: %02x "
  99. "hr: %02x min: %02x sec: %02x\n",
  100. year, mon, mday, wday, hour, min, sec);
  101. /* dump status */
  102. ctl2 = rtc_read(RTC_CTL2_REG_ADDR);
  103. if (ctl2 & RTC_CTL2_BIT_PON)
  104. printf("RTC: power-on detected\n");
  105. if (ctl2 & RTC_CTL2_BIT_VDET)
  106. printf("RTC: voltage drop detected\n");
  107. if (!(ctl2 & RTC_CTL2_BIT_XST))
  108. printf("RTC: oscillator stop detected\n");
  109. tmp->tm_sec = bcd2bin (sec & 0x7F);
  110. tmp->tm_min = bcd2bin (min & 0x7F);
  111. tmp->tm_hour = bcd2bin (hour & 0x3F);
  112. tmp->tm_mday = bcd2bin (mday & 0x3F);
  113. tmp->tm_mon = bcd2bin (mon & 0x1F);
  114. tmp->tm_year = bcd2bin (year) + ( bcd2bin (year) >= 70 ? 1900 : 2000);
  115. tmp->tm_wday = bcd2bin (wday & 0x07);
  116. tmp->tm_yday = 0;
  117. tmp->tm_isdst= 0;
  118. DEBUGR ("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  119. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  120. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  121. }
  122. /*
  123. * Set the RTC
  124. */
  125. void rtc_set (struct rtc_time *tmp)
  126. {
  127. DEBUGR ("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  128. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  129. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  130. if (tmp->tm_year < 1970 || tmp->tm_year > 2069)
  131. printf("WARNING: year should be between 1970 and 2069!\n");
  132. rtc_write (RTC_YR_REG_ADDR, bin2bcd (tmp->tm_year % 100));
  133. rtc_write (RTC_MON_REG_ADDR, bin2bcd (tmp->tm_mon));
  134. rtc_write (RTC_DAY_REG_ADDR, bin2bcd (tmp->tm_wday));
  135. rtc_write (RTC_DATE_REG_ADDR, bin2bcd (tmp->tm_mday));
  136. rtc_write (RTC_HR_REG_ADDR, bin2bcd (tmp->tm_hour));
  137. rtc_write (RTC_MIN_REG_ADDR, bin2bcd (tmp->tm_min));
  138. rtc_write (RTC_SEC_REG_ADDR, bin2bcd (tmp->tm_sec));
  139. rtc_write (RTC_CTL1_REG_ADDR, RTC_CTL1_BIT_2412);
  140. }
  141. /*
  142. * Reset the RTC. We setting the date back to 1970-01-01.
  143. */
  144. void rtc_reset (void)
  145. {
  146. struct rtc_time tmp;
  147. uchar buf[16];
  148. uchar ctl2;
  149. if (i2c_read(CFG_I2C_RTC_ADDR, 0, 0, buf, 16))
  150. printf("Error reading from RTC\n");
  151. ctl2 = rtc_read(RTC_CTL2_REG_ADDR);
  152. ctl2 &= ~(RTC_CTL2_BIT_PON | RTC_CTL2_BIT_VDET);
  153. ctl2 |= RTC_CTL2_BIT_XST | RTC_CTL2_BIT_VDSL;
  154. rtc_write (RTC_CTL2_REG_ADDR, ctl2);
  155. tmp.tm_year = 1970;
  156. tmp.tm_mon = 1;
  157. tmp.tm_mday= 1;
  158. tmp.tm_hour = 0;
  159. tmp.tm_min = 0;
  160. tmp.tm_sec = 0;
  161. rtc_set(&tmp);
  162. printf ( "RTC: %4d-%02d-%02d %2d:%02d:%02d UTC\n",
  163. tmp.tm_year, tmp.tm_mon, tmp.tm_mday,
  164. tmp.tm_hour, tmp.tm_min, tmp.tm_sec);
  165. return;
  166. }
  167. /*
  168. * Helper functions
  169. */
  170. static void rtc_write (uchar reg, uchar val)
  171. {
  172. uchar buf[2];
  173. buf[0] = reg << 4;
  174. buf[1] = val;
  175. if (i2c_write(CFG_I2C_RTC_ADDR, 0, 0, buf, 2) != 0)
  176. printf("Error writing to RTC\n");
  177. }
  178. static unsigned bcd2bin (uchar n)
  179. {
  180. return ((((n >> 4) & 0x0F) * 10) + (n & 0x0F));
  181. }
  182. static unsigned char bin2bcd (unsigned int n)
  183. {
  184. return (((n / 10) << 4) | (n % 10));
  185. }
  186. #endif /* CONFIG_RTC_RX8025 && (CFG_COMMANDS & CFG_CMD_DATE) */