rx8025.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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_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 CONFIG_SYS_I2C_RTC_ADDR
  40. # define CONFIG_SYS_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. int rtc_get (struct rtc_time *tmp)
  86. {
  87. int rel = 0;
  88. uchar sec, min, hour, mday, wday, mon, year, ctl2;
  89. uchar buf[16];
  90. if (i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0, 0, buf, 16))
  91. printf("Error reading from RTC\n");
  92. sec = rtc_read(RTC_SEC_REG_ADDR);
  93. min = rtc_read(RTC_MIN_REG_ADDR);
  94. hour = rtc_read(RTC_HR_REG_ADDR);
  95. wday = rtc_read(RTC_DAY_REG_ADDR);
  96. mday = rtc_read(RTC_DATE_REG_ADDR);
  97. mon = rtc_read(RTC_MON_REG_ADDR);
  98. year = rtc_read(RTC_YR_REG_ADDR);
  99. DEBUGR ("Get RTC year: %02x mon: %02x mday: %02x wday: %02x "
  100. "hr: %02x min: %02x sec: %02x\n",
  101. year, mon, mday, wday, hour, min, sec);
  102. /* dump status */
  103. ctl2 = rtc_read(RTC_CTL2_REG_ADDR);
  104. if (ctl2 & RTC_CTL2_BIT_PON) {
  105. printf("RTC: power-on detected\n");
  106. rel = -1;
  107. }
  108. if (ctl2 & RTC_CTL2_BIT_VDET) {
  109. printf("RTC: voltage drop detected\n");
  110. rel = -1;
  111. }
  112. if (!(ctl2 & RTC_CTL2_BIT_XST)) {
  113. printf("RTC: oscillator stop detected\n");
  114. rel = -1;
  115. }
  116. tmp->tm_sec = bcd2bin (sec & 0x7F);
  117. tmp->tm_min = bcd2bin (min & 0x7F);
  118. if (rtc_read(RTC_CTL1_REG_ADDR) & RTC_CTL1_BIT_2412)
  119. tmp->tm_hour = bcd2bin (hour & 0x3F);
  120. else
  121. tmp->tm_hour = bcd2bin (hour & 0x1F) % 12 +
  122. ((hour & 0x20) ? 12 : 0);
  123. tmp->tm_mday = bcd2bin (mday & 0x3F);
  124. tmp->tm_mon = bcd2bin (mon & 0x1F);
  125. tmp->tm_year = bcd2bin (year) + ( bcd2bin (year) >= 70 ? 1900 : 2000);
  126. tmp->tm_wday = bcd2bin (wday & 0x07);
  127. tmp->tm_yday = 0;
  128. tmp->tm_isdst= 0;
  129. DEBUGR ("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  130. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  131. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  132. return rel;
  133. }
  134. /*
  135. * Set the RTC
  136. */
  137. int rtc_set (struct rtc_time *tmp)
  138. {
  139. DEBUGR ("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  140. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  141. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  142. if (tmp->tm_year < 1970 || tmp->tm_year > 2069)
  143. printf("WARNING: year should be between 1970 and 2069!\n");
  144. rtc_write (RTC_YR_REG_ADDR, bin2bcd (tmp->tm_year % 100));
  145. rtc_write (RTC_MON_REG_ADDR, bin2bcd (tmp->tm_mon));
  146. rtc_write (RTC_DAY_REG_ADDR, bin2bcd (tmp->tm_wday));
  147. rtc_write (RTC_DATE_REG_ADDR, bin2bcd (tmp->tm_mday));
  148. rtc_write (RTC_HR_REG_ADDR, bin2bcd (tmp->tm_hour));
  149. rtc_write (RTC_MIN_REG_ADDR, bin2bcd (tmp->tm_min));
  150. rtc_write (RTC_SEC_REG_ADDR, bin2bcd (tmp->tm_sec));
  151. rtc_write (RTC_CTL1_REG_ADDR, RTC_CTL1_BIT_2412);
  152. return 0;
  153. }
  154. /*
  155. * Reset the RTC. We setting the date back to 1970-01-01.
  156. */
  157. void rtc_reset (void)
  158. {
  159. struct rtc_time tmp;
  160. uchar buf[16];
  161. uchar ctl2;
  162. if (i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0, 0, buf, 16))
  163. printf("Error reading from RTC\n");
  164. ctl2 = rtc_read(RTC_CTL2_REG_ADDR);
  165. ctl2 &= ~(RTC_CTL2_BIT_PON | RTC_CTL2_BIT_VDET);
  166. ctl2 |= RTC_CTL2_BIT_XST | RTC_CTL2_BIT_VDSL;
  167. rtc_write (RTC_CTL2_REG_ADDR, ctl2);
  168. tmp.tm_year = 1970;
  169. tmp.tm_mon = 1;
  170. tmp.tm_mday= 1;
  171. tmp.tm_hour = 0;
  172. tmp.tm_min = 0;
  173. tmp.tm_sec = 0;
  174. rtc_set(&tmp);
  175. printf ( "RTC: %4d-%02d-%02d %2d:%02d:%02d UTC\n",
  176. tmp.tm_year, tmp.tm_mon, tmp.tm_mday,
  177. tmp.tm_hour, tmp.tm_min, tmp.tm_sec);
  178. return;
  179. }
  180. /*
  181. * Helper functions
  182. */
  183. static void rtc_write (uchar reg, uchar val)
  184. {
  185. uchar buf[2];
  186. buf[0] = reg << 4;
  187. buf[1] = val;
  188. if (i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0, 0, buf, 2) != 0)
  189. printf("Error writing to RTC\n");
  190. }
  191. static unsigned bcd2bin (uchar n)
  192. {
  193. return ((((n >> 4) & 0x0F) * 10) + (n & 0x0F));
  194. }
  195. static unsigned char bin2bcd (unsigned int n)
  196. {
  197. return (((n / 10) << 4) | (n % 10));
  198. }
  199. #endif /* CONFIG_RTC_RX8025 && CONFIG_CMD_DATE */