blob: bf8008d8da557defb3200ba08894a80dbf51ee20 [file] [log] [blame]
wdenk48b42612003-06-19 23:01:32 +00001/*
2 * (C) Copyright 2003
3 * David Müller ELSOFT AG Switzerland. d.mueller@elsoft.ch
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24/*
25 * Date & Time support for the built-in Samsung S3C24X0 RTC
26 */
27
28#include <common.h>
29#include <command.h>
30
31#if defined(CONFIG_RTC_S3C24X0) && (CONFIG_COMMANDS & CFG_CMD_DATE)
32
33#if defined(CONFIG_S3C2400)
34#include <s3c2400.h>
35#elif defined(CONFIG_S3C2410)
36#include <s3c2410.h>
37#endif
38
39#include <rtc.h>
40
41/*#define DEBUG*/
42
43typedef enum {
44 RTC_ENABLE,
45 RTC_DISABLE
46} RTC_ACCESS;
47
48
49static inline void SetRTC_Access(RTC_ACCESS a)
50{
51 S3C24X0_RTC * const rtc = S3C24X0_GetBase_RTC();
52 switch (a) {
53 case RTC_ENABLE:
54 rtc->RTCCON |= 0x01; break;
55
56 case RTC_DISABLE:
57 rtc->RTCCON &= ~0x01; break;
58 }
59}
60
61static unsigned bcd2bin (uchar n)
62{
63 return ((((n >> 4) & 0x0F) * 10) + (n & 0x0F));
64}
65
66static unsigned char bin2bcd (unsigned int n)
67{
68 return (((n / 10) << 4) | (n % 10));
69}
70
71/* ------------------------------------------------------------------------- */
72
73void rtc_get (struct rtc_time *tmp)
74{
75 S3C24X0_RTC * const rtc = S3C24X0_GetBase_RTC();
76 uchar sec, min, hour, mday, wday, mon, year;
77 uchar a_sec,a_min, a_hour, a_date, a_mon, a_year, a_armed;
78
79 /* enable access to RTC registers */
80 SetRTC_Access(RTC_ENABLE);
81
82 /* read RTC registers */
83 sec = rtc->BCDSEC;
84 min = rtc->BCDMIN;
85 hour = rtc->BCDHOUR;
86 mday = rtc->BCDDATE;
87 wday = rtc->BCDDAY;
88 mon = rtc->BCDMON;
89 year = rtc->BCDYEAR;
90
91 /* read ALARM registers */
92 a_sec = rtc->ALMSEC;
93 a_min = rtc->ALMMIN;
94 a_hour = rtc->ALMHOUR;
95 a_date = rtc->ALMDATE;
96 a_mon = rtc->ALMMON;
97 a_year = rtc->ALMYEAR;
98 a_armed = rtc->RTCALM;
99
100 /* disable access to RTC registers */
101 SetRTC_Access(RTC_DISABLE);
102
103#ifdef RTC_DEBUG
104 printf ( "Get RTC year: %02x mon/cent: %02x mday: %02x wday: %02x "
105 "hr: %02x min: %02x sec: %02x\n",
106 year, mon, mday, wday,
107 hour, min, sec);
108 printf ( "Alarms: %02x: year: %02x month: %02x date: %02x hour: %02x min: %02x sec: %02x\n",
109 a_armed,
110 a_year, a_mon, a_date,
111 a_hour, a_min, a_sec);
112#endif
113
114 tmp->tm_sec = bcd2bin(sec & 0x7F);
115 tmp->tm_min = bcd2bin(min & 0x7F);
116 tmp->tm_hour = bcd2bin(hour & 0x3F);
117 tmp->tm_mday = bcd2bin(mday & 0x3F);
118 tmp->tm_mon = bcd2bin(mon & 0x1F);
119 tmp->tm_year = bcd2bin(year);
120 tmp->tm_wday = bcd2bin(wday & 0x07);
121 if(tmp->tm_year<70)
122 tmp->tm_year+=2000;
123 else
124 tmp->tm_year+=1900;
125 tmp->tm_yday = 0;
126 tmp->tm_isdst= 0;
127#ifdef RTC_DEBUG
128 printf ( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
129 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
130 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
131#endif
132}
133
134void rtc_set (struct rtc_time *tmp)
135{
136 S3C24X0_RTC * const rtc = S3C24X0_GetBase_RTC();
137 uchar sec, min, hour, mday, wday, mon, year;
138
139#ifdef RTC_DEBUG
140 printf ( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
141 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
142 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
143#endif
144 year = bin2bcd(tmp->tm_year % 100);
145 mon = bin2bcd(tmp->tm_mon);
146 wday = bin2bcd(tmp->tm_wday);
147 mday = bin2bcd(tmp->tm_mday);
148 hour = bin2bcd(tmp->tm_hour);
149 min = bin2bcd(tmp->tm_min);
150 sec = bin2bcd(tmp->tm_sec);
151
152 /* enable access to RTC registers */
153 SetRTC_Access(RTC_ENABLE);
154
155 /* write RTC registers */
156 rtc->BCDSEC = sec;
157 rtc->BCDMIN = min;
158 rtc->BCDHOUR = hour;
159 rtc->BCDDATE = mday;
160 rtc->BCDDAY = wday;
161 rtc->BCDMON = mon;
162 rtc->BCDYEAR = year;
163
164 /* disable access to RTC registers */
165 SetRTC_Access(RTC_DISABLE);
166}
167
168void rtc_reset (void)
169{
170 S3C24X0_RTC * const rtc = S3C24X0_GetBase_RTC();
171
172 rtc->RTCCON = (rtc->RTCCON & ~0x06) | 0x08;
173 rtc->RTCCON &= ~0x08;
174}
175
176/* ------------------------------------------------------------------------- */
177
178#endif /* CONFIG_RTC_S3C24X0 && CFG_CMD_DATE */