config-gta02v5-beyond.patch
[u-boot-openmoko/mini2440.git] / drivers / rtc / s3c24x0_rtc.c
blob47bd19ace2068fcc1c47f7b05a1bbf2b6b8f04f7
1 /*
2 * (C) Copyright 2003
3 * David Müller ELSOFT AG Switzerland. d.mueller@elsoft.ch
5 * See file CREDITS for list of people who contributed to this
6 * project.
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.
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.
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
25 * Date & Time support for the built-in Samsung S3C24X0 RTC
28 #include <common.h>
29 #include <command.h>
31 #if defined(CONFIG_RTC_S3C24X0) && (defined(CONFIG_CMD_DATE))
33 #if defined(CONFIG_S3C2400)
34 #include <s3c2400.h>
35 #elif defined(CONFIG_S3C2410)
36 #include <s3c2410.h>
37 #elif defined(CONFIG_S3C2440) || defined(CONFIG_S3C2442)
38 #include <s3c2440.h>
39 #endif
41 #include <rtc.h>
43 /*#define DEBUG*/
45 typedef enum {
46 RTC_ENABLE,
47 RTC_DISABLE
48 } RTC_ACCESS;
51 static inline void SetRTC_Access(RTC_ACCESS a)
53 S3C24X0_RTC * const rtc = S3C24X0_GetBase_RTC();
54 switch (a) {
55 case RTC_ENABLE:
56 rtc->RTCCON |= 0x01; break;
58 case RTC_DISABLE:
59 rtc->RTCCON &= ~0x01; break;
63 static unsigned bcd2bin (uchar n)
65 return ((((n >> 4) & 0x0F) * 10) + (n & 0x0F));
68 static unsigned char bin2bcd (unsigned int n)
70 return (((n / 10) << 4) | (n % 10));
73 /* ------------------------------------------------------------------------- */
75 int rtc_get (struct rtc_time *tmp)
77 S3C24X0_RTC * const rtc = S3C24X0_GetBase_RTC();
78 uchar sec, min, hour, mday, wday, mon, year;
79 uchar a_sec,a_min, a_hour, a_date, a_mon, a_year, a_armed;
81 /* enable access to RTC registers */
82 SetRTC_Access(RTC_ENABLE);
84 /* read RTC registers */
85 do {
86 sec = rtc->BCDSEC;
87 min = rtc->BCDMIN;
88 hour = rtc->BCDHOUR;
89 mday = rtc->BCDDATE;
90 wday = rtc->BCDDAY;
91 mon = rtc->BCDMON;
92 year = rtc->BCDYEAR;
93 } while (sec != rtc->BCDSEC);
95 /* read ALARM registers */
96 a_sec = rtc->ALMSEC;
97 a_min = rtc->ALMMIN;
98 a_hour = rtc->ALMHOUR;
99 a_date = rtc->ALMDATE;
100 a_mon = rtc->ALMMON;
101 a_year = rtc->ALMYEAR;
102 a_armed = rtc->RTCALM;
104 /* disable access to RTC registers */
105 SetRTC_Access(RTC_DISABLE);
107 #ifdef RTC_DEBUG
108 printf ( "Get RTC year: %02x mon/cent: %02x mday: %02x wday: %02x "
109 "hr: %02x min: %02x sec: %02x\n",
110 year, mon, mday, wday,
111 hour, min, sec);
112 printf ( "Alarms: %02x: year: %02x month: %02x date: %02x hour: %02x min: %02x sec: %02x\n",
113 a_armed,
114 a_year, a_mon, a_date,
115 a_hour, a_min, a_sec);
116 #endif
118 tmp->tm_sec = bcd2bin(sec & 0x7F);
119 tmp->tm_min = bcd2bin(min & 0x7F);
120 tmp->tm_hour = bcd2bin(hour & 0x3F);
121 tmp->tm_mday = bcd2bin(mday & 0x3F);
122 tmp->tm_mon = bcd2bin(mon & 0x1F);
123 tmp->tm_year = bcd2bin(year);
124 tmp->tm_wday = bcd2bin(wday & 0x07);
125 if(tmp->tm_year<70)
126 tmp->tm_year+=2000;
127 else
128 tmp->tm_year+=1900;
129 tmp->tm_yday = 0;
130 tmp->tm_isdst= 0;
131 #ifdef RTC_DEBUG
132 printf ( "Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
133 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
134 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
135 #endif
137 return 0;
140 void rtc_set (struct rtc_time *tmp)
142 S3C24X0_RTC * const rtc = S3C24X0_GetBase_RTC();
143 uchar sec, min, hour, mday, wday, mon, year;
145 #ifdef RTC_DEBUG
146 printf ( "Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
147 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
148 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
149 #endif
150 year = bin2bcd(tmp->tm_year % 100);
151 mon = bin2bcd(tmp->tm_mon);
152 wday = bin2bcd(tmp->tm_wday);
153 mday = bin2bcd(tmp->tm_mday);
154 hour = bin2bcd(tmp->tm_hour);
155 min = bin2bcd(tmp->tm_min);
156 sec = bin2bcd(tmp->tm_sec);
158 /* enable access to RTC registers */
159 SetRTC_Access(RTC_ENABLE);
161 /* write RTC registers */
162 rtc->BCDSEC = sec;
163 rtc->BCDMIN = min;
164 rtc->BCDHOUR = hour;
165 rtc->BCDDATE = mday;
166 rtc->BCDDAY = wday;
167 rtc->BCDMON = mon;
168 rtc->BCDYEAR = year;
170 /* disable access to RTC registers */
171 SetRTC_Access(RTC_DISABLE);
174 void rtc_reset (void)
176 S3C24X0_RTC * const rtc = S3C24X0_GetBase_RTC();
178 rtc->RTCCON = (rtc->RTCCON & ~0x06) | 0x08;
179 rtc->RTCCON &= ~(0x08|0x01);
182 #endif