3 * Reinhard Meyer, EMK Elektronik GmbH
4 * r.meyer@emk-elektronik.de
5 * www.emk-elektronik.de
7 * See file CREDITS for list of people who contributed to this
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,
26 /*****************************************************************************
27 * Date & Time support for internal RTC of MPC52xx
28 *****************************************************************************/
35 #if defined(CONFIG_CMD_DATE)
37 /*****************************************************************************
38 * this structure should be defined in mpc5200.h ...
39 *****************************************************************************/
40 typedef struct rtc5200 {
41 volatile ulong tsr; /* MBAR+0x800: time set register */
42 volatile ulong dsr; /* MBAR+0x804: data set register */
43 volatile ulong nysr; /* MBAR+0x808: new year and stopwatch register */
44 volatile ulong aier; /* MBAR+0x80C: alarm and interrupt enable register */
45 volatile ulong ctr; /* MBAR+0x810: current time register */
46 volatile ulong cdr; /* MBAR+0x814: current data register */
47 volatile ulong asir; /* MBAR+0x818: alarm and stopwatch interupt register */
48 volatile ulong piber; /* MBAR+0x81C: periodic interrupt and bus error register */
49 volatile ulong trdr; /* MBAR+0x820: test register/divides register */
52 #define RTC_SET 0x02000000
53 #define RTC_PAUSE 0x01000000
55 /*****************************************************************************
57 *****************************************************************************/
58 int rtc_get (struct rtc_time *tmp)
60 RTC5200 *rtc = (RTC5200 *) (CONFIG_SYS_MBAR+0x800);
61 ulong time, date, time2;
63 /* read twice to avoid getting a funny time when the second is just changing */
68 } while (time != time2);
70 tmp->tm_year = date & 0xfff;
71 tmp->tm_mon = (date >> 24) & 0xf;
72 tmp->tm_mday = (date >> 16) & 0x1f;
73 tmp->tm_wday = (date >> 21) & 7;
74 /* sunday is 7 in 5200 but 0 in rtc_time */
75 if (tmp->tm_wday == 7)
77 tmp->tm_hour = (time >> 16) & 0x1f;
78 tmp->tm_min = (time >> 8) & 0x3f;
79 tmp->tm_sec = time & 0x3f;
81 debug ( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
82 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
83 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
88 /*****************************************************************************
90 *****************************************************************************/
91 int rtc_set (struct rtc_time *tmp)
93 RTC5200 *rtc = (RTC5200 *) (CONFIG_SYS_MBAR+0x800);
94 ulong time, date, year;
96 debug ( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
97 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
98 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
100 time = (tmp->tm_hour << 16) | (tmp->tm_min << 8) | tmp->tm_sec;
101 date = (tmp->tm_mon << 16) | tmp->tm_mday;
102 if (tmp->tm_wday == 0)
105 date |= (tmp->tm_wday << 8);
108 /* mask unwanted bits that might show up when rtc_time is corrupt */
113 /* pause and set the RTC */
115 rtc->dsr = date | RTC_PAUSE;
117 rtc->dsr = date | RTC_PAUSE | RTC_SET;
119 rtc->dsr = date | RTC_PAUSE;
124 rtc->tsr = time | RTC_PAUSE;
126 rtc->tsr = time | RTC_PAUSE | RTC_SET;
128 rtc->tsr = time | RTC_PAUSE;
136 /*****************************************************************************
138 *****************************************************************************/
139 void rtc_reset (void)
141 return; /* nothing to do */