m41t60.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. static unsigned bcd2bin(uchar n)
  35. {
  36. return ((((n >> 4) & 0x0F) * 10) + (n & 0x0F));
  37. }
  38. static unsigned char bin2bcd(unsigned int n)
  39. {
  40. return (((n / 10) << 4) | (n % 10));
  41. }
  42. /*
  43. * Convert between century and "century bits" (CB1 and CB0). These routines
  44. * assume years are in the range 1900 - 2299.
  45. */
  46. static unsigned char year2cb(unsigned const year)
  47. {
  48. if (year < 1900 || year >= 2300)
  49. printf("M41T60 RTC: year %d out of range\n", year);
  50. return (year / 100) & 0x3;
  51. }
  52. static unsigned cb2year(unsigned const cb)
  53. {
  54. return 1900 + 100 * ((cb + 1) & 0x3);
  55. }
  56. /*
  57. * These are simple defines for the chip local to here so they aren't too
  58. * verbose. DAY/DATE aren't nice but that is how they are on the data sheet.
  59. */
  60. #define RTC_SEC 0x0
  61. #define RTC_MIN 0x1
  62. #define RTC_HOUR 0x2
  63. #define RTC_DAY 0x3
  64. #define RTC_DATE 0x4
  65. #define RTC_MONTH 0x5
  66. #define RTC_YEAR 0x6
  67. #define RTC_REG_CNT 7
  68. #define RTC_CTRL 0x7
  69. #if defined(DEBUG)
  70. static void rtc_dump(char const *const label)
  71. {
  72. uchar data[8];
  73. if (i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, data, sizeof(data))) {
  74. printf("I2C read failed in rtc_dump()\n");
  75. return;
  76. }
  77. printf("RTC dump %s: %02X-%02X-%02X-%02X-%02X-%02X-%02X-%02X\n",
  78. label, data[0], data[1], data[2], data[3],
  79. data[4], data[5], data[6], data[7]);
  80. }
  81. #else
  82. #define rtc_dump(label)
  83. #endif
  84. static uchar *rtc_validate(void)
  85. {
  86. /*
  87. * This routine uses the OUT bit and the validity of the time values to
  88. * determine whether there has been an initial power-up since the last
  89. * time the routine was run. It assumes that the OUT bit is not being
  90. * used for any other purpose.
  91. */
  92. static const uchar daysInMonth[0x13] = {
  93. 0x00, 0x31, 0x29, 0x31, 0x30, 0x31, 0x30, 0x31,
  94. 0x31, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  95. 0x31, 0x30, 0x31
  96. };
  97. static uchar data[8];
  98. uchar min, date, month, years;
  99. rtc_dump("begin validate");
  100. if (i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, data, sizeof(data))) {
  101. printf("I2C read failed in rtc_validate()\n");
  102. return 0;
  103. }
  104. /*
  105. * If the OUT bit is "1", there has been a loss of power, so stop the
  106. * oscillator so it can be "kick-started" as per data sheet.
  107. */
  108. if (0x00 != (data[RTC_CTRL] & 0x80)) {
  109. printf("M41T60 RTC clock lost power.\n");
  110. data[RTC_SEC] = 0x80;
  111. if (i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_SEC, 1, data, 1)) {
  112. printf("I2C write failed in rtc_validate()\n");
  113. return 0;
  114. }
  115. }
  116. /*
  117. * If the oscillator is stopped or the date is invalid, then reset the
  118. * OUT bit to "0", reset the date registers, and start the oscillator.
  119. */
  120. min = data[RTC_MIN] & 0x7F;
  121. date = data[RTC_DATE];
  122. month = data[RTC_MONTH] & 0x3F;
  123. years = data[RTC_YEAR];
  124. if (0x59 < data[RTC_SEC] || 0x09 < (data[RTC_SEC] & 0x0F) ||
  125. 0x59 < min || 0x09 < (min & 0x0F) ||
  126. 0x23 < data[RTC_HOUR] || 0x09 < (data[RTC_HOUR] & 0x0F) ||
  127. 0x07 < data[RTC_DAY] || 0x00 == data[RTC_DAY] ||
  128. 0x12 < month ||
  129. 0x99 < years || 0x09 < (years & 0x0F) ||
  130. daysInMonth[month] < date || 0x09 < (date & 0x0F) || 0x00 == date ||
  131. (0x29 == date && 0x02 == month &&
  132. ((0x00 != (years & 0x03)) ||
  133. (0x00 == years && 0x00 != (data[RTC_MONTH] & 0xC0))))) {
  134. printf("Resetting M41T60 RTC clock.\n");
  135. /*
  136. * Set to 00:00:00 1900-01-01 (Monday)
  137. */
  138. data[RTC_SEC] = 0x00;
  139. data[RTC_MIN] &= 0x80; /* preserve OFIE bit */
  140. data[RTC_HOUR] = 0x00;
  141. data[RTC_DAY] = 0x02;
  142. data[RTC_DATE] = 0x01;
  143. data[RTC_MONTH] = 0xC1;
  144. data[RTC_YEAR] = 0x00;
  145. data[RTC_CTRL] &= 0x7F; /* reset OUT bit */
  146. if (i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, data, sizeof(data))) {
  147. printf("I2C write failed in rtc_validate()\n");
  148. return 0;
  149. }
  150. }
  151. return data;
  152. }
  153. int rtc_get(struct rtc_time *tmp)
  154. {
  155. uchar const *const data = rtc_validate();
  156. if (!data)
  157. return -1;
  158. tmp->tm_sec = bcd2bin(data[RTC_SEC] & 0x7F);
  159. tmp->tm_min = bcd2bin(data[RTC_MIN] & 0x7F);
  160. tmp->tm_hour = bcd2bin(data[RTC_HOUR] & 0x3F);
  161. tmp->tm_mday = bcd2bin(data[RTC_DATE] & 0x3F);
  162. tmp->tm_mon = bcd2bin(data[RTC_MONTH] & 0x1F);
  163. tmp->tm_year = cb2year(data[RTC_MONTH] >> 6) + bcd2bin(data[RTC_YEAR]);
  164. tmp->tm_wday = bcd2bin(data[RTC_DAY] & 0x07) - 1;
  165. tmp->tm_yday = 0;
  166. tmp->tm_isdst = 0;
  167. debug("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  168. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  169. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  170. return 0;
  171. }
  172. int rtc_set(struct rtc_time *tmp)
  173. {
  174. uchar *const data = rtc_validate();
  175. if (!data)
  176. return -1;
  177. debug("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
  178. tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
  179. tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
  180. data[RTC_SEC] = (data[RTC_SEC] & 0x80) | (bin2bcd(tmp->tm_sec) & 0x7F);
  181. data[RTC_MIN] = (data[RTC_MIN] & 0X80) | (bin2bcd(tmp->tm_min) & 0X7F);
  182. data[RTC_HOUR] = bin2bcd(tmp->tm_hour) & 0x3F;
  183. data[RTC_DATE] = bin2bcd(tmp->tm_mday) & 0x3F;
  184. data[RTC_MONTH] = bin2bcd(tmp->tm_mon) & 0x1F;
  185. data[RTC_YEAR] = bin2bcd(tmp->tm_year % 100);
  186. data[RTC_MONTH] |= year2cb(tmp->tm_year) << 6;
  187. data[RTC_DAY] = bin2bcd(tmp->tm_wday + 1) & 0x07;
  188. if (i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, data, RTC_REG_CNT)) {
  189. printf("I2C write failed in rtc_set()\n");
  190. return -1;
  191. }
  192. return 0;
  193. }
  194. void rtc_reset(void)
  195. {
  196. uchar *const data = rtc_validate();
  197. char const *const s = getenv("rtccal");
  198. if (!data)
  199. return;
  200. rtc_dump("begin reset");
  201. /*
  202. * If environmental variable "rtccal" is present, it must be a hex value
  203. * between 0x00 and 0x3F, inclusive. The five least-significan bits
  204. * represent the calibration magnitude, and the sixth bit the sign bit.
  205. * If these do not match the contents of the hardware register, that
  206. * register is updated. The value 0x00 imples no correction. Consult
  207. * the M41T60 documentation for further details.
  208. */
  209. if (s) {
  210. unsigned long const l = simple_strtoul(s, 0, 16);
  211. if (l <= 0x3F) {
  212. if ((data[RTC_CTRL] & 0x3F) != l) {
  213. printf("Setting RTC calibration to 0x%02lX\n",
  214. l);
  215. data[RTC_CTRL] &= 0xC0;
  216. data[RTC_CTRL] |= (uchar) l;
  217. }
  218. } else
  219. printf("environment parameter \"rtccal\" not valid: "
  220. "ignoring\n");
  221. }
  222. /*
  223. * Turn off frequency test.
  224. */
  225. data[RTC_CTRL] &= 0xBF;
  226. if (i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_CTRL, 1, data + RTC_CTRL, 1)) {
  227. printf("I2C write failed in rtc_reset()\n");
  228. return;
  229. }
  230. rtc_dump("end reset");
  231. }
  232. #endif /* CONFIG_RTC_M41T60 && CONFIG_SYS_I2C_RTC_ADDR && CONFIG_CMD_DATE */