m41t60.c 6.4 KB

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