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 * See file CREDITS for list of people who contributed to this
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of
17 * the License, or (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
31 * SGS M48-T59Y TOD/NVRAM Driver
33 * The SGS M48 an 8K NVRAM starting at offset M48_BASE_ADDR and
34 * continuing for 8176 bytes. After that starts the Time-Of-Day (TOD)
35 * registers which are used to set/get the internal date/time functions.
37 * This module implements Y2K compliance by taking full year numbers
38 * and translating back and forth from the TOD 2-digit year.
40 * NOTE: for proper interaction with an operating system, the TOD should
41 * be used to store Universal Coordinated Time (GMT) and timezone
42 * conversions should be used.
44 * Here is a diagram of the memory layout:
46 * +---------------------------------------------+ 0xffe0a000
47 * | Non-volatile memory | .
49 * | (8176 bytes of Non-volatile memory) | .
51 * +---------------------------------------------+ 0xffe0bff0
53 * +---------------------------------------------+ 0xffe0bff1
55 * +---------------------------------------------+ 0xffe0bff2
57 * +---------------------------------------------+ 0xffe0bff3
59 * +---------------------------------------------+ 0xffe0bff4
61 * +---------------------------------------------+ 0xffe0bff5
63 * +---------------------------------------------+ 0xffe0bff6
65 * +---------------------------------------------+ 0xffe0bff7
67 * +---------------------------------------------+ 0xffe0bff8
69 * +---------------------------------------------+ 0xffe0bff9
71 * +---------------------------------------------+ 0xffe0bffa
73 * +---------------------------------------------+ 0xffe0bffb
75 * +---------------------------------------------+ 0xffe0bffc
77 * +---------------------------------------------+ 0xffe0bffd
79 * +---------------------------------------------+ 0xffe0bffe
80 * | Year (2 digits only) |
81 * +---------------------------------------------+ 0xffe0bfff
88 * Imported from mousse.h:
90 * TOD_REG_BASE Base of m48t59y TOD registers
91 * SYS_TOD_UNPROTECT() Disable NVRAM write protect
92 * SYS_TOD_PROTECT() Re-enable NVRAM write protect
98 #define DAY_OF_WEEK 0xc
112 #define M48_ADDR ((volatile unsigned char *) TOD_REG_BASE)
114 int m48_tod_init(void)
118 M48_ADDR[CONTROL] = 0;
120 M48_ADDR[INTCTL] = 0;
123 * If the oscillator is currently stopped (as on a new part shipped
124 * from the factory), start it running.
126 * Here is an example of the TOD bytes on a brand new M48T59Y part:
127 * 00 00 00 00 00 00 00 00 00 88 8c c3 bf c8 f5 01
130 if (M48_ADDR[SECOND] & 0x80)
131 M48_ADDR[SECOND] = 0;
134 if ( M48_ADDR[FLAGS] & 0x10) {
135 printf("NOTICE: Battery low on Real-Time Clock (replace SNAPHAT).\n");
147 static int to_bcd(int value)
149 return value / 10 * 16 + value % 10;
152 static int from_bcd(int value)
154 return value / 16 * 10 + value % 16;
157 static int day_of_week(int y, int m, int d) /* 0-6 ==> Sun-Sat */
159 static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
161 return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
165 * Note: the TOD should store the current GMT
168 int m48_tod_set(int year, /* 1980-2079 */
169 int month, /* 01-12 */
171 int hour, /* 00-23 */
172 int minute, /* 00-59 */
173 int second) /* 00-59 */
178 M48_ADDR[CONTROL] |= 0x80; /* Set WRITE bit */
180 M48_ADDR[YEAR] = to_bcd(year % 100);
181 M48_ADDR[MONTH] = to_bcd(month);
182 M48_ADDR[DAY] = to_bcd(day);
183 M48_ADDR[DAY_OF_WEEK] = day_of_week(year, month, day) + 1;
184 M48_ADDR[HOUR] = to_bcd(hour);
185 M48_ADDR[MINUTE] = to_bcd(minute);
186 M48_ADDR[SECOND] = to_bcd(second);
188 M48_ADDR[CONTROL] &= ~0x80; /* Clear WRITE bit */
196 * Note: the TOD should store the current GMT
199 int m48_tod_get(int *year, /* 1980-2079 */
200 int *month, /* 01-12 */
201 int *day, /* 01-31 */
202 int *hour, /* 00-23 */
203 int *minute, /* 00-59 */
204 int *second) /* 00-59 */
210 M48_ADDR[CONTROL] |= 0x40; /* Set READ bit */
212 y = from_bcd(M48_ADDR[YEAR]);
213 *year = y < 80 ? 2000 + y : 1900 + y;
214 *month = from_bcd(M48_ADDR[MONTH]);
215 *day = from_bcd(M48_ADDR[DAY]);
216 /* day_of_week = M48_ADDR[DAY_OF_WEEK] & 0xf; */
217 *hour = from_bcd(M48_ADDR[HOUR]);
218 *minute = from_bcd(M48_ADDR[MINUTE]);
219 *second = from_bcd(M48_ADDR[SECOND] & 0x7f);
221 M48_ADDR[CONTROL] &= ~0x40; /* Clear READ bit */
228 int m48_tod_get_second(void)
230 return from_bcd(M48_ADDR[SECOND] & 0x7f);
236 * If usec is 0, the watchdog timer is disarmed.
238 * If usec is non-zero, the watchdog timer is armed (or re-armed) for
239 * approximately usec microseconds (if the exact requested usec is
240 * not supported by the chip, the next higher available value is used).
242 * Minimum watchdog timeout = 62500 usec
243 * Maximum watchdog timeout = 124 sec (124000000 usec)
246 void m48_watchdog_arm(int usec)
255 } else if (usec < 2000000) { /* Resolution: 1/16s if below 2s */
257 mpy = (usec + 62499) / 62500;
258 } else if (usec < 8000000) { /* Resolution: 1/4s if below 8s */
260 mpy = (usec + 249999) / 250000;
261 } else if (usec < 32000000) { /* Resolution: 1s if below 32s */
263 mpy = (usec + 999999) / 1000000;
264 } else { /* Resolution: 4s up to 124s */
266 mpy = (usec + 3999999) / 4000000;
271 M48_ADDR[WATCH] = (0x80 | /* Steer to RST signal (IRQ = N/C) */
279 * U-Boot RTC support.
282 rtc_get( struct rtc_time *tmp )
284 m48_tod_get(&tmp->tm_year,
294 printf( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
295 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
296 tmp->tm_hour, tmp->tm_min, tmp->tm_sec );
302 int rtc_set( struct rtc_time *tmp )
304 m48_tod_set(tmp->tm_year, /* 1980-2079 */
305 tmp->tm_mon, /* 01-12 */
306 tmp->tm_mday, /* 01-31 */
307 tmp->tm_hour, /* 00-23 */
308 tmp->tm_min, /* 00-59 */
309 tmp->tm_sec); /* 00-59 */
312 printf( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
313 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
314 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);