m41t60.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * (C) Copyright 2007
  3. * Larry Johnson, lrj@acm.org
  4. *
  5. * based on rtc/m41t11.c which is ...
  6. *
  7. * (C) Copyright 2002
  8. * Andrew May, Viasat Inc, amay@viasat.com
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of
  13. * the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  23. * MA 02111-1307 USA
  24. */
  25. /*
  26. * STMicroelectronics M41T60 serial access real-time clock
  27. */
  28. /* #define DEBUG 1 */
  29. #include <common.h>
  30. #include <command.h>
  31. #include <rtc.h>
  32. #include <i2c.h>
  33. #if defined(CONFIG_SYS_I2C_RTC_ADDR) && defined(CONFIG_CMD_DATE)
  34. /*
  35. * Convert between century and "century bits" (CB1 and CB0). These routines
  36. * assume years are in the range 1900 - 2299.
  37. */
  38. static unsigned char year2cb(unsigned const year)
  39. {
  40. if (year < 1900 || year >= 2300)
  41. printf("M41T60 RTC: year %d out of range\n", year);
  42. return (year / 100) & 0x3;
  43. }
  44. static unsigned cb2year(unsigned const cb)
  45. {
  46. return 1900 + 100 * ((cb + 1) & 0x3);
  47. }
  48. /*
  49. * These are simple defines for the chip local to here so they aren't too
  50. * verbose. DAY/DATE aren't nice but that is how they are on the data sheet.
  51. */
  52. #define RTC_SEC 0x0
  53. #define RTC_MIN 0x1
  54. #define RTC_HOUR 0x2
  55. #define RTC_DAY 0x3
  56. #define RTC_DATE 0x4
  57. #define RTC_MONTH 0x5
  58. #define RTC_YEAR 0x6
  59. #define RTC_REG_CNT 7
  60. #define RTC_CTRL 0x7
  61. #if defined(DEBUG)
  62. static void rtc_dump(char const *const label)
  63. {
  64. uchar data[8];
  65. if (i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, data, sizeof(data))) {
  66. printf("I2C read failed in rtc_dump()\n");
  67. return;
  68. }
  69. printf("RTC dump %s: %02X-%02X-%02X-%02X-%02X-%02X-%02X-%02X\n",
  70. label, data[0], data[1], data[2], data[3],
  71. data[4], data[5], data[6], data[7]);
  72. }
  73. #else
  74. #define rtc_dump(label)
  75. #endif
  76. static uchar *rtc_validate(void)
  77. {
  78. /*
  79. * This routine uses the OUT bit and the validity of the time values to
  80. * determine whether there has been an initial power-up since the last
  81. * time the routine was run. It assumes that the OUT bit is not being
  82. * used for any other purpose.
  83. */
  84. static const uchar daysInMonth[0x13] = {
  85. 0x00, 0x31, 0x29, 0x31, 0x30, 0x31, 0x30, 0x31,
  86. 0x31, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  87. 0x31, 0x30, 0x31
  88. };
  89. static uchar data[8];
  90. uchar min, date, month, years;
  91. rtc_dump("begin validate");
  92. if (i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, data, sizeof(data))) {
  93. printf("I2C read failed in rtc_validate()\n");
  94. return 0;
  95. }
  96. /*
  97. * If the OUT bit is "1", there has been a loss of power, so stop the
  98. * oscillator so it can be "kick-started" as per data sheet.
  99. */
  100. if (0x00 != (data[RTC_CTRL] & 0x80)) {
  101. printf("M41T60 RTC clock lost power.\n");
  102. data[RTC_SEC] = 0x80;
  103. if (i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_SEC, 1, data, 1)) {
  104. printf("I2C write failed in rtc_validate()\n");
  105. return 0;
  106. }
  107. }
  108. /*
  109. * If the oscillator is stopped or the date is invalid, then reset the
  110. * OUT bit to "0", reset the date registers, and start the oscillator.
  111. */
  112. min = data[RTC_MIN] & 0x7F;
  113. date = data[RTC_DATE];
  114. month = data[RTC_MONTH] & 0x3F;
  115. years = data[RTC_YEAR];
  116. if (0x59 < data[RTC_SEC] || 0x09 < (data[RTC_SEC] & 0x0F) ||
  117. 0x59 < min || 0x09 < (min & 0x0F) ||
  118. 0x23 < data[RTC_HOUR] || 0x09 < (data[RTC_HOUR] & 0x0F) ||
  119. 0x07 < data[RTC_DAY] || 0x00 == data[RTC_DAY] ||
  120. 0x12 < month ||
  121. 0x99 < years || 0x09 < (years & 0x0F) ||
  122. daysInMonth[month] < date || 0x09 < (date & 0x0F) || 0x00 == date ||
  123. (0x29 == date && 0x02 == month &&
  124. ((0x00 != (years & 0x03)) ||
  125. (0x00 == years && 0x00 != (data[RTC_MONTH] & 0xC0))))) {
  126. printf("Resetting M41T60 RTC clock.\n");
  127. /*
  128. * Set to 00:00:00 1900-01-01 (Monday)
  129. */
  130. data[RTC_SEC] = 0x00;
  131. data[RTC_MIN] &= 0x80; /* preserve OFIE bit */
  132. data[RTC_HOUR] = 0x00;
  133. data[RTC_DAY] = 0x02;
  134. data[RTC_DATE] = 0x01;
  135. data[RTC_MONTH] = 0xC1;
  136. data[RTC_YEAR] = 0x00;
  137. data[RTC_CTRL] &= 0x7F; /* reset OUT bit */
  138. if (i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, data, sizeof(data))) {
  139. printf("I2C write failed in rtc_validate()\n");
  140. return 0;
  141. }
  142. }
  143. return data;
  144. }
  145. int rtc_get(struct rtc_time *tmp)
  146. {
  147. uchar const *const data = rtc_validate();
  148. if (!data)
  149. return -1;
  150. tmp->tm_sec = bcd2bin(data[RTC_SEC] & 0x7F);
  151. tmp->tm_min = bcd2bin(data[RTC_MIN] & 0x7F);
  152. tmp->tm_hour = bcd2bin(data[RTC_HOUR] & 0x3F);
  153. tmp->tm_mday = bcd2bin(data[RTC_DATE] & 0x3F);
  154. tmp->tm_mon = bcd2bin(data[RTC_MONTH] & 0x1F);
  155. tmp->tm_year = cb2year(data[RTC_MONTH] >> 6) + bcd2bin(data[RTC_YEAR]);
  156. tmp->tm_wday = bcd2bin(data[RTC_DAY] & 0x07) - 1;
  157. tmp->tm_yday = 0;
  158. tmp->tm_isdst = 0;
  159. debug("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  160. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  161. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  162. return 0;
  163. }
  164. int rtc_set(struct rtc_time *tmp)
  165. {
  166. uchar *const data = rtc_validate();
  167. if (!data)
  168. return -1;
  169. debug("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  170. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  171. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  172. data[RTC_SEC] = (data[RTC_SEC] & 0x80) | (bin2bcd(tmp->tm_sec) & 0x7F);
  173. data[RTC_MIN] = (data[RTC_MIN] & 0X80) | (bin2bcd(tmp->tm_min) & 0X7F);
  174. data[RTC_HOUR] = bin2bcd(tmp->tm_hour) & 0x3F;
  175. data[RTC_DATE] = bin2bcd(tmp->tm_mday) & 0x3F;
  176. data[RTC_MONTH] = bin2bcd(tmp->tm_mon) & 0x1F;
  177. data[RTC_YEAR] = bin2bcd(tmp->tm_year % 100);
  178. data[RTC_MONTH] |= year2cb(tmp->tm_year) << 6;
  179. data[RTC_DAY] = bin2bcd(tmp->tm_wday + 1) & 0x07;
  180. if (i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, data, RTC_REG_CNT)) {
  181. printf("I2C write failed in rtc_set()\n");
  182. return -1;
  183. }
  184. return 0;
  185. }
  186. void rtc_reset(void)
  187. {
  188. uchar *const data = rtc_validate();
  189. char const *const s = getenv("rtccal");
  190. if (!data)
  191. return;
  192. rtc_dump("begin reset");
  193. /*
  194. * If environmental variable "rtccal" is present, it must be a hex value
  195. * between 0x00 and 0x3F, inclusive. The five least-significan bits
  196. * represent the calibration magnitude, and the sixth bit the sign bit.
  197. * If these do not match the contents of the hardware register, that
  198. * register is updated. The value 0x00 imples no correction. Consult
  199. * the M41T60 documentation for further details.
  200. */
  201. if (s) {
  202. unsigned long const l = simple_strtoul(s, 0, 16);
  203. if (l <= 0x3F) {
  204. if ((data[RTC_CTRL] & 0x3F) != l) {
  205. printf("Setting RTC calibration to 0x%02lX\n",
  206. l);
  207. data[RTC_CTRL] &= 0xC0;
  208. data[RTC_CTRL] |= (uchar) l;
  209. }
  210. } else
  211. printf("environment parameter \"rtccal\" not valid: "
  212. "ignoring\n");
  213. }
  214. /*
  215. * Turn off frequency test.
  216. */
  217. data[RTC_CTRL] &= 0xBF;
  218. if (i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_CTRL, 1, data + RTC_CTRL, 1)) {
  219. printf("I2C write failed in rtc_reset()\n");
  220. return;
  221. }
  222. rtc_dump("end reset");
  223. }
  224. #endif /* CONFIG_RTC_M41T60 && CONFIG_SYS_I2C_RTC_ADDR && CONFIG_CMD_DATE */