blob: 2053df153f4cb91e069cb9c09d2b60b2f75ca742 [file] [log] [blame]
wdenkb983fa22004-01-16 00:30:56 +00001/*
2 * (C) Copyright 2004
3 * Reinhard Meyer, EMK Elektronik GmbH
4 * r.meyer@emk-elektronik.de
5 * www.emk-elektronik.de
6 *
7 * See file CREDITS for list of people who contributed to this
8 * project.
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 * Date & Time support for internal RTC of MPC52xx
28 *****************************************************************************/
29/*#define DEBUG*/
30
31#include <common.h>
32#include <command.h>
33#include <rtc.h>
34
35#if defined(CONFIG_RTC_MPC5200) && (CONFIG_COMMANDS & CFG_CMD_DATE)
36
37/*****************************************************************************
38 * this structure should be defined in mpc5200.h ...
39 *****************************************************************************/
40typedef 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 */
50} RTC5200;
51
52#define RTC_SET 0x02000000
53#define RTC_PAUSE 0x01000000
54
55/*****************************************************************************
56 * get time
57 *****************************************************************************/
58void rtc_get (struct rtc_time *tmp)
59{
60 RTC5200 *rtc = (RTC5200 *) (CFG_MBAR+0x800);
61 ulong time, date, time2;
62
63 /* read twice to avoid getting a funny time when the second is just changing */
64 do {
65 time = rtc->ctr;
66 date = rtc->cdr;
67 time2 = rtc->ctr;
68 } while (time != time2);
69
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)
76 tmp->tm_wday = 0;
77 tmp->tm_hour = (time >> 16) & 0x1f;
78 tmp->tm_min = (time >> 8) & 0x3f;
79 tmp->tm_sec = time & 0x3f;
80
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);
84}
85
86/*****************************************************************************
87 * set time
88 *****************************************************************************/
89void rtc_set (struct rtc_time *tmp)
90{
91 RTC5200 *rtc = (RTC5200 *) (CFG_MBAR+0x800);
92 ulong time, date, year;
93
94 debug ( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
95 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
96 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
97
98 time = (tmp->tm_hour << 16) | (tmp->tm_min << 8) | tmp->tm_sec;
99 date = (tmp->tm_mon << 16) | tmp->tm_mday;
100 if (tmp->tm_wday == 0)
101 date |= (7 << 8);
102 else
103 date |= (tmp->tm_wday << 8);
104 year = tmp->tm_year;
105
106 /* mask unwanted bits that might show up when rtc_time is corrupt */
107 time &= 0x001f3f3f;
108 date &= 0x001f071f;
109 year &= 0x00000fff;
110
111 /* pause and set the RTC */
112 rtc->nysr = year;
113 rtc->dsr = date | RTC_PAUSE;
114 udelay (1000);
115 rtc->dsr = date | RTC_PAUSE | RTC_SET;
116 udelay (1000);
117 rtc->dsr = date | RTC_PAUSE;
118 udelay (1000);
119 rtc->dsr = date;
120 udelay (1000);
121
122 rtc->tsr = time | RTC_PAUSE;
123 udelay (1000);
124 rtc->tsr = time | RTC_PAUSE | RTC_SET;
125 udelay (1000);
126 rtc->tsr = time | RTC_PAUSE;
127 udelay (1000);
128 rtc->tsr = time;
129 udelay (1000);
130}
131
132/*****************************************************************************
133 * reset rtc circuit
134 *****************************************************************************/
135void rtc_reset (void)
136{
137 return; /* nothing to do */
138}
139
140#endif /* CONFIG_RTC_MPC5200 && CFG_CMD_DATE */