2 * SGS M48-T59Y TOD/NVRAM Driver
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 * (C) Copyright 1999, by Curt McDowell, 08-06-99, Broadcom Corp.
9 * (C) Copyright 2001, James Dougherty, 07/18/01, Broadcom Corp.
11 * SPDX-License-Identifier: GPL-2.0+
15 * SGS M48-T59Y TOD/NVRAM Driver
17 * The SGS M48 an 8K NVRAM starting at offset M48_BASE_ADDR and
18 * continuing for 8176 bytes. After that starts the Time-Of-Day (TOD)
19 * registers which are used to set/get the internal date/time functions.
21 * This module implements Y2K compliance by taking full year numbers
22 * and translating back and forth from the TOD 2-digit year.
24 * NOTE: for proper interaction with an operating system, the TOD should
25 * be used to store Universal Coordinated Time (GMT) and timezone
26 * conversions should be used.
28 * Here is a diagram of the memory layout:
30 * +---------------------------------------------+ 0xffe0a000
31 * | Non-volatile memory | .
33 * | (8176 bytes of Non-volatile memory) | .
35 * +---------------------------------------------+ 0xffe0bff0
37 * +---------------------------------------------+ 0xffe0bff1
39 * +---------------------------------------------+ 0xffe0bff2
41 * +---------------------------------------------+ 0xffe0bff3
43 * +---------------------------------------------+ 0xffe0bff4
45 * +---------------------------------------------+ 0xffe0bff5
47 * +---------------------------------------------+ 0xffe0bff6
49 * +---------------------------------------------+ 0xffe0bff7
51 * +---------------------------------------------+ 0xffe0bff8
53 * +---------------------------------------------+ 0xffe0bff9
55 * +---------------------------------------------+ 0xffe0bffa
57 * +---------------------------------------------+ 0xffe0bffb
59 * +---------------------------------------------+ 0xffe0bffc
61 * +---------------------------------------------+ 0xffe0bffd
63 * +---------------------------------------------+ 0xffe0bffe
64 * | Year (2 digits only) |
65 * +---------------------------------------------+ 0xffe0bfff
72 * Imported from mousse.h:
74 * TOD_REG_BASE Base of m48t59y TOD registers
75 * SYS_TOD_UNPROTECT() Disable NVRAM write protect
76 * SYS_TOD_PROTECT() Re-enable NVRAM write protect
82 #define DAY_OF_WEEK 0xc
96 #define M48_ADDR ((volatile unsigned char *) TOD_REG_BASE)
98 int m48_tod_init(void)
102 M48_ADDR[CONTROL] = 0;
104 M48_ADDR[INTCTL] = 0;
107 * If the oscillator is currently stopped (as on a new part shipped
108 * from the factory), start it running.
110 * Here is an example of the TOD bytes on a brand new M48T59Y part:
111 * 00 00 00 00 00 00 00 00 00 88 8c c3 bf c8 f5 01
114 if (M48_ADDR[SECOND] & 0x80)
115 M48_ADDR[SECOND] = 0;
118 if ( M48_ADDR[FLAGS] & 0x10) {
119 printf("NOTICE: Battery low on Real-Time Clock (replace SNAPHAT).\n");
131 static int to_bcd(int value)
133 return value / 10 * 16 + value % 10;
136 static int from_bcd(int value)
138 return value / 16 * 10 + value % 16;
141 static int day_of_week(int y, int m, int d) /* 0-6 ==> Sun-Sat */
143 static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
145 return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
149 * Note: the TOD should store the current GMT
152 int m48_tod_set(int year, /* 1980-2079 */
153 int month, /* 01-12 */
155 int hour, /* 00-23 */
156 int minute, /* 00-59 */
157 int second) /* 00-59 */
162 M48_ADDR[CONTROL] |= 0x80; /* Set WRITE bit */
164 M48_ADDR[YEAR] = to_bcd(year % 100);
165 M48_ADDR[MONTH] = to_bcd(month);
166 M48_ADDR[DAY] = to_bcd(day);
167 M48_ADDR[DAY_OF_WEEK] = day_of_week(year, month, day) + 1;
168 M48_ADDR[HOUR] = to_bcd(hour);
169 M48_ADDR[MINUTE] = to_bcd(minute);
170 M48_ADDR[SECOND] = to_bcd(second);
172 M48_ADDR[CONTROL] &= ~0x80; /* Clear WRITE bit */
180 * Note: the TOD should store the current GMT
183 int m48_tod_get(int *year, /* 1980-2079 */
184 int *month, /* 01-12 */
185 int *day, /* 01-31 */
186 int *hour, /* 00-23 */
187 int *minute, /* 00-59 */
188 int *second) /* 00-59 */
194 M48_ADDR[CONTROL] |= 0x40; /* Set READ bit */
196 y = from_bcd(M48_ADDR[YEAR]);
197 *year = y < 80 ? 2000 + y : 1900 + y;
198 *month = from_bcd(M48_ADDR[MONTH]);
199 *day = from_bcd(M48_ADDR[DAY]);
200 /* day_of_week = M48_ADDR[DAY_OF_WEEK] & 0xf; */
201 *hour = from_bcd(M48_ADDR[HOUR]);
202 *minute = from_bcd(M48_ADDR[MINUTE]);
203 *second = from_bcd(M48_ADDR[SECOND] & 0x7f);
205 M48_ADDR[CONTROL] &= ~0x40; /* Clear READ bit */
212 int m48_tod_get_second(void)
214 return from_bcd(M48_ADDR[SECOND] & 0x7f);
220 * If usec is 0, the watchdog timer is disarmed.
222 * If usec is non-zero, the watchdog timer is armed (or re-armed) for
223 * approximately usec microseconds (if the exact requested usec is
224 * not supported by the chip, the next higher available value is used).
226 * Minimum watchdog timeout = 62500 usec
227 * Maximum watchdog timeout = 124 sec (124000000 usec)
230 void m48_watchdog_arm(int usec)
239 } else if (usec < 2000000) { /* Resolution: 1/16s if below 2s */
241 mpy = (usec + 62499) / 62500;
242 } else if (usec < 8000000) { /* Resolution: 1/4s if below 8s */
244 mpy = (usec + 249999) / 250000;
245 } else if (usec < 32000000) { /* Resolution: 1s if below 32s */
247 mpy = (usec + 999999) / 1000000;
248 } else { /* Resolution: 4s up to 124s */
250 mpy = (usec + 3999999) / 4000000;
255 M48_ADDR[WATCH] = (0x80 | /* Steer to RST signal (IRQ = N/C) */
263 * U-Boot RTC support.
266 rtc_get( struct rtc_time *tmp )
268 m48_tod_get(&tmp->tm_year,
278 printf( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
279 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
280 tmp->tm_hour, tmp->tm_min, tmp->tm_sec );
286 int rtc_set( struct rtc_time *tmp )
288 m48_tod_set(tmp->tm_year, /* 1980-2079 */
289 tmp->tm_mon, /* 01-12 */
290 tmp->tm_mday, /* 01-31 */
291 tmp->tm_hour, /* 00-23 */
292 tmp->tm_min, /* 00-59 */
293 tmp->tm_sec); /* 00-59 */
296 printf( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
297 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
298 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);