blob: c16ff2eb719940f8f048fe000221b78886019e1a [file] [log] [blame]
wdenk7539dea2003-06-19 23:01:32 +00001/*
2 * (C) Copyright 2003
Albert ARIBAUD60fbc8d2011-08-04 18:45:45 +02003 * David Müller ELSOFT AG Switzerland. d.mueller@elsoft.ch
wdenk7539dea2003-06-19 23:01:32 +00004 *
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
Michal Simekc3e6c552008-07-14 19:45:37 +020031#if (defined(CONFIG_CMD_DATE))
wdenk7539dea2003-06-19 23:01:32 +000032
kevin.morfitt@fearnside-systems.co.uke0d81312009-11-17 18:30:34 +090033#include <asm/arch/s3c24x0_cpu.h>
wdenk7539dea2003-06-19 23:01:32 +000034
35#include <rtc.h>
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090036#include <asm/io.h>
Anatolij Gustschin49a358b2011-11-15 13:21:03 +000037#include <linux/compiler.h>
wdenk7539dea2003-06-19 23:01:32 +000038
39typedef enum {
40 RTC_ENABLE,
41 RTC_DISABLE
42} RTC_ACCESS;
43
44
45static inline void SetRTC_Access(RTC_ACCESS a)
46{
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090047 struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc();
48
wdenk7539dea2003-06-19 23:01:32 +000049 switch (a) {
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090050 case RTC_ENABLE:
C Nauman383c43e2010-10-26 23:04:31 +090051 writeb(readb(&rtc->rtccon) | 0x01, &rtc->rtccon);
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090052 break;
wdenk7539dea2003-06-19 23:01:32 +000053
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090054 case RTC_DISABLE:
C Nauman383c43e2010-10-26 23:04:31 +090055 writeb(readb(&rtc->rtccon) & ~0x01, &rtc->rtccon);
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090056 break;
wdenk7539dea2003-06-19 23:01:32 +000057 }
58}
59
wdenk7539dea2003-06-19 23:01:32 +000060/* ------------------------------------------------------------------------- */
61
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090062int rtc_get(struct rtc_time *tmp)
wdenk7539dea2003-06-19 23:01:32 +000063{
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090064 struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc();
wdenk7539dea2003-06-19 23:01:32 +000065 uchar sec, min, hour, mday, wday, mon, year;
Anatolij Gustschin49a358b2011-11-15 13:21:03 +000066 __maybe_unused uchar a_sec, a_min, a_hour, a_date,
67 a_mon, a_year, a_armed;
wdenk7539dea2003-06-19 23:01:32 +000068
69 /* enable access to RTC registers */
70 SetRTC_Access(RTC_ENABLE);
71
72 /* read RTC registers */
wdenk25521902003-09-13 19:01:12 +000073 do {
C Nauman383c43e2010-10-26 23:04:31 +090074 sec = readb(&rtc->bcdsec);
75 min = readb(&rtc->bcdmin);
76 hour = readb(&rtc->bcdhour);
77 mday = readb(&rtc->bcddate);
78 wday = readb(&rtc->bcdday);
79 mon = readb(&rtc->bcdmon);
80 year = readb(&rtc->bcdyear);
81 } while (sec != readb(&rtc->bcdsec));
wdenk7539dea2003-06-19 23:01:32 +000082
83 /* read ALARM registers */
C Nauman383c43e2010-10-26 23:04:31 +090084 a_sec = readb(&rtc->almsec);
85 a_min = readb(&rtc->almmin);
86 a_hour = readb(&rtc->almhour);
87 a_date = readb(&rtc->almdate);
88 a_mon = readb(&rtc->almmon);
89 a_year = readb(&rtc->almyear);
90 a_armed = readb(&rtc->rtcalm);
wdenk7539dea2003-06-19 23:01:32 +000091
92 /* disable access to RTC registers */
93 SetRTC_Access(RTC_DISABLE);
94
95#ifdef RTC_DEBUG
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090096 printf("Get RTC year: %02x mon/cent: %02x mday: %02x wday: %02x "
97 "hr: %02x min: %02x sec: %02x\n",
98 year, mon, mday, wday, hour, min, sec);
99 printf("Alarms: %02x: year: %02x month: %02x date: %02x hour: "
100 "%02x min: %02x sec: %02x\n",
101 a_armed, a_year, a_mon, a_date, a_hour, a_min, a_sec);
wdenk7539dea2003-06-19 23:01:32 +0000102#endif
103
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900104 tmp->tm_sec = bcd2bin(sec & 0x7F);
105 tmp->tm_min = bcd2bin(min & 0x7F);
wdenk7539dea2003-06-19 23:01:32 +0000106 tmp->tm_hour = bcd2bin(hour & 0x3F);
107 tmp->tm_mday = bcd2bin(mday & 0x3F);
108 tmp->tm_mon = bcd2bin(mon & 0x1F);
109 tmp->tm_year = bcd2bin(year);
110 tmp->tm_wday = bcd2bin(wday & 0x07);
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900111 if (tmp->tm_year < 70)
112 tmp->tm_year += 2000;
wdenk7539dea2003-06-19 23:01:32 +0000113 else
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900114 tmp->tm_year += 1900;
115 tmp->tm_yday = 0;
116 tmp->tm_isdst = 0;
wdenk7539dea2003-06-19 23:01:32 +0000117#ifdef RTC_DEBUG
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900118 printf("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
119 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
120 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
wdenk7539dea2003-06-19 23:01:32 +0000121#endif
Yuri Tikhonov9bacd942008-03-20 17:56:04 +0300122
123 return 0;
wdenk7539dea2003-06-19 23:01:32 +0000124}
125
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900126int rtc_set(struct rtc_time *tmp)
wdenk7539dea2003-06-19 23:01:32 +0000127{
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900128 struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc();
wdenk7539dea2003-06-19 23:01:32 +0000129 uchar sec, min, hour, mday, wday, mon, year;
130
131#ifdef RTC_DEBUG
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900132 printf("Set 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);
wdenk7539dea2003-06-19 23:01:32 +0000135#endif
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900136 year = bin2bcd(tmp->tm_year % 100);
137 mon = bin2bcd(tmp->tm_mon);
138 wday = bin2bcd(tmp->tm_wday);
139 mday = bin2bcd(tmp->tm_mday);
140 hour = bin2bcd(tmp->tm_hour);
141 min = bin2bcd(tmp->tm_min);
142 sec = bin2bcd(tmp->tm_sec);
wdenk7539dea2003-06-19 23:01:32 +0000143
144 /* enable access to RTC registers */
145 SetRTC_Access(RTC_ENABLE);
146
147 /* write RTC registers */
C Nauman383c43e2010-10-26 23:04:31 +0900148 writeb(sec, &rtc->bcdsec);
149 writeb(min, &rtc->bcdmin);
150 writeb(hour, &rtc->bcdhour);
151 writeb(mday, &rtc->bcddate);
152 writeb(wday, &rtc->bcdday);
153 writeb(mon, &rtc->bcdmon);
154 writeb(year, &rtc->bcdyear);
wdenk7539dea2003-06-19 23:01:32 +0000155
156 /* disable access to RTC registers */
157 SetRTC_Access(RTC_DISABLE);
Jean-Christophe PLAGNIOL-VILLARD97a2e102008-09-01 23:06:23 +0200158
159 return 0;
wdenk7539dea2003-06-19 23:01:32 +0000160}
161
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900162void rtc_reset(void)
wdenk7539dea2003-06-19 23:01:32 +0000163{
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900164 struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc();
wdenk7539dea2003-06-19 23:01:32 +0000165
C Nauman383c43e2010-10-26 23:04:31 +0900166 writeb((readb(&rtc->rtccon) & ~0x06) | 0x08, &rtc->rtccon);
167 writeb(readb(&rtc->rtccon) & ~(0x08 | 0x01), &rtc->rtccon);
wdenk7539dea2003-06-19 23:01:32 +0000168}
169
Jon Loeliger07efe2a2007-07-10 10:27:39 -0500170#endif