blob: 632fe4b14550173c41b3cb9c59b326ba6fb9709f [file] [log] [blame]
Larry Johnson12618272007-12-22 15:14:00 -05001/*
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/*
27 * STMicroelectronics M41T60 serial access real-time clock
28 */
29
30/* #define DEBUG 1 */
31
32#include <common.h>
33#include <command.h>
34#include <rtc.h>
35#include <i2c.h>
36
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020037#if defined(CONFIG_SYS_I2C_RTC_ADDR) && defined(CONFIG_CMD_DATE)
Larry Johnson12618272007-12-22 15:14:00 -050038
Larry Johnson12618272007-12-22 15:14:00 -050039/*
40 * Convert between century and "century bits" (CB1 and CB0). These routines
41 * assume years are in the range 1900 - 2299.
42 */
43
44static unsigned char year2cb(unsigned const year)
45{
46 if (year < 1900 || year >= 2300)
47 printf("M41T60 RTC: year %d out of range\n", year);
48
49 return (year / 100) & 0x3;
50}
51
52static unsigned cb2year(unsigned const cb)
53{
54 return 1900 + 100 * ((cb + 1) & 0x3);
55}
56
57/*
58 * These are simple defines for the chip local to here so they aren't too
59 * verbose. DAY/DATE aren't nice but that is how they are on the data sheet.
60 */
61#define RTC_SEC 0x0
62#define RTC_MIN 0x1
63#define RTC_HOUR 0x2
64#define RTC_DAY 0x3
65#define RTC_DATE 0x4
66#define RTC_MONTH 0x5
67#define RTC_YEAR 0x6
68
69#define RTC_REG_CNT 7
70
71#define RTC_CTRL 0x7
72
73#if defined(DEBUG)
74static void rtc_dump(char const *const label)
75{
76 uchar data[8];
77
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020078 if (i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, data, sizeof(data))) {
Larry Johnson12618272007-12-22 15:14:00 -050079 printf("I2C read failed in rtc_dump()\n");
80 return;
81 }
82 printf("RTC dump %s: %02X-%02X-%02X-%02X-%02X-%02X-%02X-%02X\n",
83 label, data[0], data[1], data[2], data[3],
84 data[4], data[5], data[6], data[7]);
85}
86#else
87#define rtc_dump(label)
88#endif
89
90static uchar *rtc_validate(void)
91{
92 /*
93 * This routine uses the OUT bit and the validity of the time values to
94 * determine whether there has been an initial power-up since the last
95 * time the routine was run. It assumes that the OUT bit is not being
96 * used for any other purpose.
97 */
98 static const uchar daysInMonth[0x13] = {
99 0x00, 0x31, 0x29, 0x31, 0x30, 0x31, 0x30, 0x31,
100 0x31, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
101 0x31, 0x30, 0x31
102 };
103 static uchar data[8];
104 uchar min, date, month, years;
105
106 rtc_dump("begin validate");
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200107 if (i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, data, sizeof(data))) {
Larry Johnson12618272007-12-22 15:14:00 -0500108 printf("I2C read failed in rtc_validate()\n");
109 return 0;
110 }
111 /*
112 * If the OUT bit is "1", there has been a loss of power, so stop the
113 * oscillator so it can be "kick-started" as per data sheet.
114 */
115 if (0x00 != (data[RTC_CTRL] & 0x80)) {
116 printf("M41T60 RTC clock lost power.\n");
117 data[RTC_SEC] = 0x80;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200118 if (i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_SEC, 1, data, 1)) {
Larry Johnson12618272007-12-22 15:14:00 -0500119 printf("I2C write failed in rtc_validate()\n");
120 return 0;
121 }
122 }
123 /*
124 * If the oscillator is stopped or the date is invalid, then reset the
125 * OUT bit to "0", reset the date registers, and start the oscillator.
126 */
127 min = data[RTC_MIN] & 0x7F;
128 date = data[RTC_DATE];
129 month = data[RTC_MONTH] & 0x3F;
130 years = data[RTC_YEAR];
131 if (0x59 < data[RTC_SEC] || 0x09 < (data[RTC_SEC] & 0x0F) ||
132 0x59 < min || 0x09 < (min & 0x0F) ||
133 0x23 < data[RTC_HOUR] || 0x09 < (data[RTC_HOUR] & 0x0F) ||
134 0x07 < data[RTC_DAY] || 0x00 == data[RTC_DAY] ||
135 0x12 < month ||
136 0x99 < years || 0x09 < (years & 0x0F) ||
137 daysInMonth[month] < date || 0x09 < (date & 0x0F) || 0x00 == date ||
138 (0x29 == date && 0x02 == month &&
139 ((0x00 != (years & 0x03)) ||
140 (0x00 == years && 0x00 != (data[RTC_MONTH] & 0xC0))))) {
141 printf("Resetting M41T60 RTC clock.\n");
142 /*
143 * Set to 00:00:00 1900-01-01 (Monday)
144 */
145 data[RTC_SEC] = 0x00;
146 data[RTC_MIN] &= 0x80; /* preserve OFIE bit */
147 data[RTC_HOUR] = 0x00;
148 data[RTC_DAY] = 0x02;
149 data[RTC_DATE] = 0x01;
150 data[RTC_MONTH] = 0xC1;
151 data[RTC_YEAR] = 0x00;
152 data[RTC_CTRL] &= 0x7F; /* reset OUT bit */
153
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200154 if (i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, data, sizeof(data))) {
Larry Johnson12618272007-12-22 15:14:00 -0500155 printf("I2C write failed in rtc_validate()\n");
156 return 0;
157 }
158 }
159 return data;
160}
161
Yuri Tikhonovb73a19e2008-03-20 17:56:04 +0300162int rtc_get(struct rtc_time *tmp)
Larry Johnson12618272007-12-22 15:14:00 -0500163{
164 uchar const *const data = rtc_validate();
165
166 if (!data)
Yuri Tikhonovb73a19e2008-03-20 17:56:04 +0300167 return -1;
Larry Johnson12618272007-12-22 15:14:00 -0500168
169 tmp->tm_sec = bcd2bin(data[RTC_SEC] & 0x7F);
170 tmp->tm_min = bcd2bin(data[RTC_MIN] & 0x7F);
171 tmp->tm_hour = bcd2bin(data[RTC_HOUR] & 0x3F);
172 tmp->tm_mday = bcd2bin(data[RTC_DATE] & 0x3F);
173 tmp->tm_mon = bcd2bin(data[RTC_MONTH] & 0x1F);
174 tmp->tm_year = cb2year(data[RTC_MONTH] >> 6) + bcd2bin(data[RTC_YEAR]);
175 tmp->tm_wday = bcd2bin(data[RTC_DAY] & 0x07) - 1;
176 tmp->tm_yday = 0;
177 tmp->tm_isdst = 0;
178
179 debug("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
180 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
181 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
Yuri Tikhonovb73a19e2008-03-20 17:56:04 +0300182
183 return 0;
Larry Johnson12618272007-12-22 15:14:00 -0500184}
185
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +0200186int rtc_set(struct rtc_time *tmp)
Larry Johnson12618272007-12-22 15:14:00 -0500187{
188 uchar *const data = rtc_validate();
189
190 if (!data)
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +0200191 return -1;
Larry Johnson12618272007-12-22 15:14:00 -0500192
193 debug("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
194 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
195 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
196
197 data[RTC_SEC] = (data[RTC_SEC] & 0x80) | (bin2bcd(tmp->tm_sec) & 0x7F);
198 data[RTC_MIN] = (data[RTC_MIN] & 0X80) | (bin2bcd(tmp->tm_min) & 0X7F);
199 data[RTC_HOUR] = bin2bcd(tmp->tm_hour) & 0x3F;
200 data[RTC_DATE] = bin2bcd(tmp->tm_mday) & 0x3F;
201 data[RTC_MONTH] = bin2bcd(tmp->tm_mon) & 0x1F;
202 data[RTC_YEAR] = bin2bcd(tmp->tm_year % 100);
203 data[RTC_MONTH] |= year2cb(tmp->tm_year) << 6;
204 data[RTC_DAY] = bin2bcd(tmp->tm_wday + 1) & 0x07;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200205 if (i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, data, RTC_REG_CNT)) {
Larry Johnson12618272007-12-22 15:14:00 -0500206 printf("I2C write failed in rtc_set()\n");
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +0200207 return -1;
Larry Johnson12618272007-12-22 15:14:00 -0500208 }
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +0200209
210 return 0;
Larry Johnson12618272007-12-22 15:14:00 -0500211}
212
213void rtc_reset(void)
214{
215 uchar *const data = rtc_validate();
216 char const *const s = getenv("rtccal");
217
218 if (!data)
219 return;
220
221 rtc_dump("begin reset");
222 /*
223 * If environmental variable "rtccal" is present, it must be a hex value
224 * between 0x00 and 0x3F, inclusive. The five least-significan bits
225 * represent the calibration magnitude, and the sixth bit the sign bit.
226 * If these do not match the contents of the hardware register, that
227 * register is updated. The value 0x00 imples no correction. Consult
228 * the M41T60 documentation for further details.
229 */
230 if (s) {
231 unsigned long const l = simple_strtoul(s, 0, 16);
232
233 if (l <= 0x3F) {
234 if ((data[RTC_CTRL] & 0x3F) != l) {
Stefan Roese10943c92008-07-10 10:00:45 +0200235 printf("Setting RTC calibration to 0x%02lX\n",
Larry Johnson12618272007-12-22 15:14:00 -0500236 l);
237 data[RTC_CTRL] &= 0xC0;
238 data[RTC_CTRL] |= (uchar) l;
239 }
240 } else
241 printf("environment parameter \"rtccal\" not valid: "
242 "ignoring\n");
243 }
244 /*
245 * Turn off frequency test.
246 */
247 data[RTC_CTRL] &= 0xBF;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200248 if (i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_CTRL, 1, data + RTC_CTRL, 1)) {
Larry Johnson12618272007-12-22 15:14:00 -0500249 printf("I2C write failed in rtc_reset()\n");
250 return;
251 }
252 rtc_dump("end reset");
253}
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200254#endif /* CONFIG_RTC_M41T60 && CONFIG_SYS_I2C_RTC_ADDR && CONFIG_CMD_DATE */