m41t60.c 6.4 KB

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