3 * Larry Johnson, lrj@acm.org
5 * based on rtc/m41t11.c which is ...
8 * Andrew May, Viasat Inc, amay@viasat.com
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.
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.
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,
27 * STMicroelectronics M41T60 serial access real-time clock
37 #if defined(CONFIG_RTC_M41T60) && defined(CFG_I2C_RTC_ADDR) && \
38 defined(CONFIG_CMD_DATE)
40 static unsigned bcd2bin(uchar n)
42 return ((((n >> 4) & 0x0F) * 10) + (n & 0x0F));
45 static unsigned char bin2bcd(unsigned int n)
47 return (((n / 10) << 4) | (n % 10));
51 * Convert between century and "century bits" (CB1 and CB0). These routines
52 * assume years are in the range 1900 - 2299.
55 static unsigned char year2cb(unsigned const year)
57 if (year < 1900 || year >= 2300)
58 printf("M41T60 RTC: year %d out of range\n", year);
60 return (year / 100) & 0x3;
63 static unsigned cb2year(unsigned const cb)
65 return 1900 + 100 * ((cb + 1) & 0x3);
69 * These are simple defines for the chip local to here so they aren't too
70 * verbose. DAY/DATE aren't nice but that is how they are on the data sheet.
85 static void rtc_dump(char const *const label)
89 if (i2c_read(CFG_I2C_RTC_ADDR, 0, 1, data, sizeof(data))) {
90 printf("I2C read failed in rtc_dump()\n");
93 printf("RTC dump %s: %02X-%02X-%02X-%02X-%02X-%02X-%02X-%02X\n",
94 label, data[0], data[1], data[2], data[3],
95 data[4], data[5], data[6], data[7]);
98 #define rtc_dump(label)
101 static uchar *rtc_validate(void)
104 * This routine uses the OUT bit and the validity of the time values to
105 * determine whether there has been an initial power-up since the last
106 * time the routine was run. It assumes that the OUT bit is not being
107 * used for any other purpose.
109 static const uchar daysInMonth[0x13] = {
110 0x00, 0x31, 0x29, 0x31, 0x30, 0x31, 0x30, 0x31,
111 0x31, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
114 static uchar data[8];
115 uchar min, date, month, years;
117 rtc_dump("begin validate");
118 if (i2c_read(CFG_I2C_RTC_ADDR, 0, 1, data, sizeof(data))) {
119 printf("I2C read failed in rtc_validate()\n");
123 * If the OUT bit is "1", there has been a loss of power, so stop the
124 * oscillator so it can be "kick-started" as per data sheet.
126 if (0x00 != (data[RTC_CTRL] & 0x80)) {
127 printf("M41T60 RTC clock lost power.\n");
128 data[RTC_SEC] = 0x80;
129 if (i2c_write(CFG_I2C_RTC_ADDR, RTC_SEC, 1, data, 1)) {
130 printf("I2C write failed in rtc_validate()\n");
135 * If the oscillator is stopped or the date is invalid, then reset the
136 * OUT bit to "0", reset the date registers, and start the oscillator.
138 min = data[RTC_MIN] & 0x7F;
139 date = data[RTC_DATE];
140 month = data[RTC_MONTH] & 0x3F;
141 years = data[RTC_YEAR];
142 if (0x59 < data[RTC_SEC] || 0x09 < (data[RTC_SEC] & 0x0F) ||
143 0x59 < min || 0x09 < (min & 0x0F) ||
144 0x23 < data[RTC_HOUR] || 0x09 < (data[RTC_HOUR] & 0x0F) ||
145 0x07 < data[RTC_DAY] || 0x00 == data[RTC_DAY] ||
147 0x99 < years || 0x09 < (years & 0x0F) ||
148 daysInMonth[month] < date || 0x09 < (date & 0x0F) || 0x00 == date ||
149 (0x29 == date && 0x02 == month &&
150 ((0x00 != (years & 0x03)) ||
151 (0x00 == years && 0x00 != (data[RTC_MONTH] & 0xC0))))) {
152 printf("Resetting M41T60 RTC clock.\n");
154 * Set to 00:00:00 1900-01-01 (Monday)
156 data[RTC_SEC] = 0x00;
157 data[RTC_MIN] &= 0x80; /* preserve OFIE bit */
158 data[RTC_HOUR] = 0x00;
159 data[RTC_DAY] = 0x02;
160 data[RTC_DATE] = 0x01;
161 data[RTC_MONTH] = 0xC1;
162 data[RTC_YEAR] = 0x00;
163 data[RTC_CTRL] &= 0x7F; /* reset OUT bit */
165 if (i2c_write(CFG_I2C_RTC_ADDR, 0, 1, data, sizeof(data))) {
166 printf("I2C write failed in rtc_validate()\n");
173 int rtc_get(struct rtc_time *tmp)
175 uchar const *const data = rtc_validate();
180 tmp->tm_sec = bcd2bin(data[RTC_SEC] & 0x7F);
181 tmp->tm_min = bcd2bin(data[RTC_MIN] & 0x7F);
182 tmp->tm_hour = bcd2bin(data[RTC_HOUR] & 0x3F);
183 tmp->tm_mday = bcd2bin(data[RTC_DATE] & 0x3F);
184 tmp->tm_mon = bcd2bin(data[RTC_MONTH] & 0x1F);
185 tmp->tm_year = cb2year(data[RTC_MONTH] >> 6) + bcd2bin(data[RTC_YEAR]);
186 tmp->tm_wday = bcd2bin(data[RTC_DAY] & 0x07) - 1;
190 debug("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
191 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
192 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
197 void rtc_set(struct rtc_time *tmp)
199 uchar *const data = rtc_validate();
204 debug("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
205 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
206 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
208 data[RTC_SEC] = (data[RTC_SEC] & 0x80) | (bin2bcd(tmp->tm_sec) & 0x7F);
209 data[RTC_MIN] = (data[RTC_MIN] & 0X80) | (bin2bcd(tmp->tm_min) & 0X7F);
210 data[RTC_HOUR] = bin2bcd(tmp->tm_hour) & 0x3F;
211 data[RTC_DATE] = bin2bcd(tmp->tm_mday) & 0x3F;
212 data[RTC_MONTH] = bin2bcd(tmp->tm_mon) & 0x1F;
213 data[RTC_YEAR] = bin2bcd(tmp->tm_year % 100);
214 data[RTC_MONTH] |= year2cb(tmp->tm_year) << 6;
215 data[RTC_DAY] = bin2bcd(tmp->tm_wday + 1) & 0x07;
216 if (i2c_write(CFG_I2C_RTC_ADDR, 0, 1, data, RTC_REG_CNT)) {
217 printf("I2C write failed in rtc_set()\n");
224 uchar *const data = rtc_validate();
225 char const *const s = getenv("rtccal");
230 rtc_dump("begin reset");
232 * If environmental variable "rtccal" is present, it must be a hex value
233 * between 0x00 and 0x3F, inclusive. The five least-significan bits
234 * represent the calibration magnitude, and the sixth bit the sign bit.
235 * If these do not match the contents of the hardware register, that
236 * register is updated. The value 0x00 imples no correction. Consult
237 * the M41T60 documentation for further details.
240 unsigned long const l = simple_strtoul(s, 0, 16);
243 if ((data[RTC_CTRL] & 0x3F) != l) {
244 printf("Setting RTC calibration to 0x%02X\n",
246 data[RTC_CTRL] &= 0xC0;
247 data[RTC_CTRL] |= (uchar) l;
250 printf("environment parameter \"rtccal\" not valid: "
254 * Turn off frequency test.
256 data[RTC_CTRL] &= 0xBF;
257 if (i2c_write(CFG_I2C_RTC_ADDR, RTC_CTRL, 1, data + RTC_CTRL, 1)) {
258 printf("I2C write failed in rtc_reset()\n");
261 rtc_dump("end reset");
263 #endif /* CONFIG_RTC_M41T60 && CFG_I2C_RTC_ADDR && CONFIG_CMD_DATE */