1 // SPDX-License-Identifier: GPL-2.0+
4 * Larry Johnson, lrj@acm.org
6 * based on rtc/m41t11.c which is ...
9 * Andrew May, Viasat Inc, amay@viasat.com
13 * STMicroelectronics M41T60 serial access real-time clock
25 * Convert between century and "century bits" (CB1 and CB0). These routines
26 * assume years are in the range 1900 - 2299.
29 static unsigned char year2cb(unsigned const year)
31 if (year < 1900 || year >= 2300)
32 printf("M41T60 RTC: year %d out of range\n", year);
34 return (year / 100) & 0x3;
37 static unsigned cb2year(unsigned const cb)
39 return 1900 + 100 * ((cb + 1) & 0x3);
43 * These are simple defines for the chip local to here so they aren't too
44 * verbose. DAY/DATE aren't nice but that is how they are on the data sheet.
59 static void rtc_dump(char const *const label)
63 if (i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, data, sizeof(data))) {
64 printf("I2C read failed in rtc_dump()\n");
67 printf("RTC dump %s: %02X-%02X-%02X-%02X-%02X-%02X-%02X-%02X\n",
68 label, data[0], data[1], data[2], data[3],
69 data[4], data[5], data[6], data[7]);
72 #define rtc_dump(label)
75 static uchar *rtc_validate(void)
78 * This routine uses the OUT bit and the validity of the time values to
79 * determine whether there has been an initial power-up since the last
80 * time the routine was run. It assumes that the OUT bit is not being
81 * used for any other purpose.
83 static const uchar daysInMonth[0x13] = {
84 0x00, 0x31, 0x29, 0x31, 0x30, 0x31, 0x30, 0x31,
85 0x31, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
89 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");
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.
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");
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.
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] ||
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");
128 * Set to 00:00:00 1900-01-01 (Monday)
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 */
139 if (i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, data, sizeof(data))) {
140 printf("I2C write failed in rtc_validate()\n");
147 int rtc_get(struct rtc_time *tmp)
149 uchar const *const data = rtc_validate();
154 tmp->tm_sec = bcd2bin(data[RTC_SEC] & 0x7F);
155 tmp->tm_min = bcd2bin(data[RTC_MIN] & 0x7F);
156 tmp->tm_hour = bcd2bin(data[RTC_HOUR] & 0x3F);
157 tmp->tm_mday = bcd2bin(data[RTC_DATE] & 0x3F);
158 tmp->tm_mon = bcd2bin(data[RTC_MONTH] & 0x1F);
159 tmp->tm_year = cb2year(data[RTC_MONTH] >> 6) + bcd2bin(data[RTC_YEAR]);
160 tmp->tm_wday = bcd2bin(data[RTC_DAY] & 0x07) - 1;
164 debug("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
165 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
166 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
171 int rtc_set(struct rtc_time *tmp)
173 uchar *const data = rtc_validate();
178 debug("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
179 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
180 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
182 data[RTC_SEC] = (data[RTC_SEC] & 0x80) | (bin2bcd(tmp->tm_sec) & 0x7F);
183 data[RTC_MIN] = (data[RTC_MIN] & 0X80) | (bin2bcd(tmp->tm_min) & 0X7F);
184 data[RTC_HOUR] = bin2bcd(tmp->tm_hour) & 0x3F;
185 data[RTC_DATE] = bin2bcd(tmp->tm_mday) & 0x3F;
186 data[RTC_MONTH] = bin2bcd(tmp->tm_mon) & 0x1F;
187 data[RTC_YEAR] = bin2bcd(tmp->tm_year % 100);
188 data[RTC_MONTH] |= year2cb(tmp->tm_year) << 6;
189 data[RTC_DAY] = bin2bcd(tmp->tm_wday + 1) & 0x07;
190 if (i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, data, RTC_REG_CNT)) {
191 printf("I2C write failed in rtc_set()\n");
200 uchar *const data = rtc_validate();
201 char const *const s = env_get("rtccal");
206 rtc_dump("begin reset");
208 * If environmental variable "rtccal" is present, it must be a hex value
209 * between 0x00 and 0x3F, inclusive. The five least-significan bits
210 * represent the calibration magnitude, and the sixth bit the sign bit.
211 * If these do not match the contents of the hardware register, that
212 * register is updated. The value 0x00 imples no correction. Consult
213 * the M41T60 documentation for further details.
216 unsigned long const l = simple_strtoul(s, 0, 16);
219 if ((data[RTC_CTRL] & 0x3F) != l) {
220 printf("Setting RTC calibration to 0x%02lX\n",
222 data[RTC_CTRL] &= 0xC0;
223 data[RTC_CTRL] |= (uchar) l;
226 printf("environment parameter \"rtccal\" not valid: "
230 * Turn off frequency test.
232 data[RTC_CTRL] &= 0xBF;
233 if (i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_CTRL, 1, data + RTC_CTRL, 1)) {
234 printf("I2C write failed in rtc_reset()\n");
237 rtc_dump("end reset");