1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32#include <common.h>
33#include <command.h>
34#include <rtc.h>
35#include <i2c.h>
36
37#if defined(CONFIG_SYS_I2C_RTC_ADDR) && defined(CONFIG_CMD_DATE)
38
39static unsigned bcd2bin(uchar n)
40{
41 return ((((n >> 4) & 0x0F) * 10) + (n & 0x0F));
42}
43
44static unsigned char bin2bcd(unsigned int n)
45{
46 return (((n / 10) << 4) | (n % 10));
47}
48
49
50
51
52
53
54static unsigned char year2cb(unsigned const year)
55{
56 if (year < 1900 || year >= 2300)
57 printf("M41T60 RTC: year %d out of range\n", year);
58
59 return (year / 100) & 0x3;
60}
61
62static unsigned cb2year(unsigned const cb)
63{
64 return 1900 + 100 * ((cb + 1) & 0x3);
65}
66
67
68
69
70
71#define RTC_SEC 0x0
72#define RTC_MIN 0x1
73#define RTC_HOUR 0x2
74#define RTC_DAY 0x3
75#define RTC_DATE 0x4
76#define RTC_MONTH 0x5
77#define RTC_YEAR 0x6
78
79#define RTC_REG_CNT 7
80
81#define RTC_CTRL 0x7
82
83#if defined(DEBUG)
84static void rtc_dump(char const *const label)
85{
86 uchar data[8];
87
88 if (i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, data, sizeof(data))) {
89 printf("I2C read failed in rtc_dump()\n");
90 return;
91 }
92 printf("RTC dump %s: %02X-%02X-%02X-%02X-%02X-%02X-%02X-%02X\n",
93 label, data[0], data[1], data[2], data[3],
94 data[4], data[5], data[6], data[7]);
95}
96#else
97#define rtc_dump(label)
98#endif
99
100static uchar *rtc_validate(void)
101{
102
103
104
105
106
107
108 static const uchar daysInMonth[0x13] = {
109 0x00, 0x31, 0x29, 0x31, 0x30, 0x31, 0x30, 0x31,
110 0x31, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
111 0x31, 0x30, 0x31
112 };
113 static uchar data[8];
114 uchar min, date, month, years;
115
116 rtc_dump("begin validate");
117 if (i2c_read(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, data, sizeof(data))) {
118 printf("I2C read failed in rtc_validate()\n");
119 return 0;
120 }
121
122
123
124
125 if (0x00 != (data[RTC_CTRL] & 0x80)) {
126 printf("M41T60 RTC clock lost power.\n");
127 data[RTC_SEC] = 0x80;
128 if (i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_SEC, 1, data, 1)) {
129 printf("I2C write failed in rtc_validate()\n");
130 return 0;
131 }
132 }
133
134
135
136
137 min = data[RTC_MIN] & 0x7F;
138 date = data[RTC_DATE];
139 month = data[RTC_MONTH] & 0x3F;
140 years = data[RTC_YEAR];
141 if (0x59 < data[RTC_SEC] || 0x09 < (data[RTC_SEC] & 0x0F) ||
142 0x59 < min || 0x09 < (min & 0x0F) ||
143 0x23 < data[RTC_HOUR] || 0x09 < (data[RTC_HOUR] & 0x0F) ||
144 0x07 < data[RTC_DAY] || 0x00 == data[RTC_DAY] ||
145 0x12 < month ||
146 0x99 < years || 0x09 < (years & 0x0F) ||
147 daysInMonth[month] < date || 0x09 < (date & 0x0F) || 0x00 == date ||
148 (0x29 == date && 0x02 == month &&
149 ((0x00 != (years & 0x03)) ||
150 (0x00 == years && 0x00 != (data[RTC_MONTH] & 0xC0))))) {
151 printf("Resetting M41T60 RTC clock.\n");
152
153
154
155 data[RTC_SEC] = 0x00;
156 data[RTC_MIN] &= 0x80;
157 data[RTC_HOUR] = 0x00;
158 data[RTC_DAY] = 0x02;
159 data[RTC_DATE] = 0x01;
160 data[RTC_MONTH] = 0xC1;
161 data[RTC_YEAR] = 0x00;
162 data[RTC_CTRL] &= 0x7F;
163
164 if (i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, data, sizeof(data))) {
165 printf("I2C write failed in rtc_validate()\n");
166 return 0;
167 }
168 }
169 return data;
170}
171
172int rtc_get(struct rtc_time *tmp)
173{
174 uchar const *const data = rtc_validate();
175
176 if (!data)
177 return -1;
178
179 tmp->tm_sec = bcd2bin(data[RTC_SEC] & 0x7F);
180 tmp->tm_min = bcd2bin(data[RTC_MIN] & 0x7F);
181 tmp->tm_hour = bcd2bin(data[RTC_HOUR] & 0x3F);
182 tmp->tm_mday = bcd2bin(data[RTC_DATE] & 0x3F);
183 tmp->tm_mon = bcd2bin(data[RTC_MONTH] & 0x1F);
184 tmp->tm_year = cb2year(data[RTC_MONTH] >> 6) + bcd2bin(data[RTC_YEAR]);
185 tmp->tm_wday = bcd2bin(data[RTC_DAY] & 0x07) - 1;
186 tmp->tm_yday = 0;
187 tmp->tm_isdst = 0;
188
189 debug("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
190 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
191 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
192
193 return 0;
194}
195
196int rtc_set(struct rtc_time *tmp)
197{
198 uchar *const data = rtc_validate();
199
200 if (!data)
201 return -1;
202
203 debug("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
204 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
205 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
206
207 data[RTC_SEC] = (data[RTC_SEC] & 0x80) | (bin2bcd(tmp->tm_sec) & 0x7F);
208 data[RTC_MIN] = (data[RTC_MIN] & 0X80) | (bin2bcd(tmp->tm_min) & 0X7F);
209 data[RTC_HOUR] = bin2bcd(tmp->tm_hour) & 0x3F;
210 data[RTC_DATE] = bin2bcd(tmp->tm_mday) & 0x3F;
211 data[RTC_MONTH] = bin2bcd(tmp->tm_mon) & 0x1F;
212 data[RTC_YEAR] = bin2bcd(tmp->tm_year % 100);
213 data[RTC_MONTH] |= year2cb(tmp->tm_year) << 6;
214 data[RTC_DAY] = bin2bcd(tmp->tm_wday + 1) & 0x07;
215 if (i2c_write(CONFIG_SYS_I2C_RTC_ADDR, 0, 1, data, RTC_REG_CNT)) {
216 printf("I2C write failed in rtc_set()\n");
217 return -1;
218 }
219
220 return 0;
221}
222
223void rtc_reset(void)
224{
225 uchar *const data = rtc_validate();
226 char const *const s = getenv("rtccal");
227
228 if (!data)
229 return;
230
231 rtc_dump("begin reset");
232
233
234
235
236
237
238
239
240 if (s) {
241 unsigned long const l = simple_strtoul(s, 0, 16);
242
243 if (l <= 0x3F) {
244 if ((data[RTC_CTRL] & 0x3F) != l) {
245 printf("Setting RTC calibration to 0x%02lX\n",
246 l);
247 data[RTC_CTRL] &= 0xC0;
248 data[RTC_CTRL] |= (uchar) l;
249 }
250 } else
251 printf("environment parameter \"rtccal\" not valid: "
252 "ignoring\n");
253 }
254
255
256
257 data[RTC_CTRL] &= 0xBF;
258 if (i2c_write(CONFIG_SYS_I2C_RTC_ADDR, RTC_CTRL, 1, data + RTC_CTRL, 1)) {
259 printf("I2C write failed in rtc_reset()\n");
260 return;
261 }
262 rtc_dump("end reset");
263}
264#endif
265