blob: 187620ac896cdbebd1f429989a817588c5a3b712 [file] [log] [blame]
wdenk48b42612003-06-19 23:01:32 +00001/*
2 * (C) Copyright 2003
Albert ARIBAUDfa82f872011-08-04 18:45:45 +02003 * David Müller ELSOFT AG Switzerland. d.mueller@elsoft.ch
wdenk48b42612003-06-19 23:01:32 +00004 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk48b42612003-06-19 23:01:32 +00006 */
7
8/*
9 * Date & Time support for the built-in Samsung S3C24X0 RTC
10 */
11
12#include <common.h>
13#include <command.h>
14
Michal Simek871c18d2008-07-14 19:45:37 +020015#if (defined(CONFIG_CMD_DATE))
wdenk48b42612003-06-19 23:01:32 +000016
kevin.morfitt@fearnside-systems.co.ukac678042009-11-17 18:30:34 +090017#include <asm/arch/s3c24x0_cpu.h>
wdenk48b42612003-06-19 23:01:32 +000018
19#include <rtc.h>
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090020#include <asm/io.h>
Anatolij Gustschinf45e91c2011-11-15 13:21:03 +000021#include <linux/compiler.h>
wdenk48b42612003-06-19 23:01:32 +000022
23typedef enum {
24 RTC_ENABLE,
25 RTC_DISABLE
26} RTC_ACCESS;
27
28
29static inline void SetRTC_Access(RTC_ACCESS a)
30{
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090031 struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc();
wdenk48b42612003-06-19 23:01:32 +000032
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090033 switch (a) {
34 case RTC_ENABLE:
C Naumand9abba82010-10-26 23:04:31 +090035 writeb(readb(&rtc->rtccon) | 0x01, &rtc->rtccon);
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090036 break;
37
38 case RTC_DISABLE:
C Naumand9abba82010-10-26 23:04:31 +090039 writeb(readb(&rtc->rtccon) & ~0x01, &rtc->rtccon);
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090040 break;
wdenk48b42612003-06-19 23:01:32 +000041 }
42}
43
wdenk48b42612003-06-19 23:01:32 +000044/* ------------------------------------------------------------------------- */
45
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090046int rtc_get(struct rtc_time *tmp)
wdenk48b42612003-06-19 23:01:32 +000047{
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090048 struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc();
wdenk48b42612003-06-19 23:01:32 +000049 uchar sec, min, hour, mday, wday, mon, year;
Anatolij Gustschinf45e91c2011-11-15 13:21:03 +000050 __maybe_unused uchar a_sec, a_min, a_hour, a_date,
51 a_mon, a_year, a_armed;
wdenk48b42612003-06-19 23:01:32 +000052
53 /* enable access to RTC registers */
54 SetRTC_Access(RTC_ENABLE);
55
56 /* read RTC registers */
wdenk531716e2003-09-13 19:01:12 +000057 do {
C Naumand9abba82010-10-26 23:04:31 +090058 sec = readb(&rtc->bcdsec);
59 min = readb(&rtc->bcdmin);
60 hour = readb(&rtc->bcdhour);
61 mday = readb(&rtc->bcddate);
62 wday = readb(&rtc->bcdday);
63 mon = readb(&rtc->bcdmon);
64 year = readb(&rtc->bcdyear);
65 } while (sec != readb(&rtc->bcdsec));
wdenk48b42612003-06-19 23:01:32 +000066
67 /* read ALARM registers */
C Naumand9abba82010-10-26 23:04:31 +090068 a_sec = readb(&rtc->almsec);
69 a_min = readb(&rtc->almmin);
70 a_hour = readb(&rtc->almhour);
71 a_date = readb(&rtc->almdate);
72 a_mon = readb(&rtc->almmon);
73 a_year = readb(&rtc->almyear);
74 a_armed = readb(&rtc->rtcalm);
wdenk48b42612003-06-19 23:01:32 +000075
76 /* disable access to RTC registers */
77 SetRTC_Access(RTC_DISABLE);
78
79#ifdef RTC_DEBUG
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090080 printf("Get RTC year: %02x mon/cent: %02x mday: %02x wday: %02x "
81 "hr: %02x min: %02x sec: %02x\n",
82 year, mon, mday, wday, hour, min, sec);
83 printf("Alarms: %02x: year: %02x month: %02x date: %02x hour: "
84 "%02x min: %02x sec: %02x\n",
85 a_armed, a_year, a_mon, a_date, a_hour, a_min, a_sec);
wdenk48b42612003-06-19 23:01:32 +000086#endif
87
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090088 tmp->tm_sec = bcd2bin(sec & 0x7F);
89 tmp->tm_min = bcd2bin(min & 0x7F);
wdenk48b42612003-06-19 23:01:32 +000090 tmp->tm_hour = bcd2bin(hour & 0x3F);
91 tmp->tm_mday = bcd2bin(mday & 0x3F);
92 tmp->tm_mon = bcd2bin(mon & 0x1F);
93 tmp->tm_year = bcd2bin(year);
94 tmp->tm_wday = bcd2bin(wday & 0x07);
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090095 if (tmp->tm_year < 70)
96 tmp->tm_year += 2000;
wdenk48b42612003-06-19 23:01:32 +000097 else
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090098 tmp->tm_year += 1900;
99 tmp->tm_yday = 0;
100 tmp->tm_isdst = 0;
wdenk48b42612003-06-19 23:01:32 +0000101#ifdef RTC_DEBUG
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900102 printf("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
103 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
104 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
wdenk48b42612003-06-19 23:01:32 +0000105#endif
Yuri Tikhonovb73a19e2008-03-20 17:56:04 +0300106
107 return 0;
wdenk48b42612003-06-19 23:01:32 +0000108}
109
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900110int rtc_set(struct rtc_time *tmp)
wdenk48b42612003-06-19 23:01:32 +0000111{
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900112 struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc();
wdenk48b42612003-06-19 23:01:32 +0000113 uchar sec, min, hour, mday, wday, mon, year;
114
115#ifdef RTC_DEBUG
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900116 printf("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
117 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
118 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
wdenk48b42612003-06-19 23:01:32 +0000119#endif
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900120 year = bin2bcd(tmp->tm_year % 100);
121 mon = bin2bcd(tmp->tm_mon);
122 wday = bin2bcd(tmp->tm_wday);
123 mday = bin2bcd(tmp->tm_mday);
124 hour = bin2bcd(tmp->tm_hour);
125 min = bin2bcd(tmp->tm_min);
126 sec = bin2bcd(tmp->tm_sec);
wdenk48b42612003-06-19 23:01:32 +0000127
128 /* enable access to RTC registers */
129 SetRTC_Access(RTC_ENABLE);
130
131 /* write RTC registers */
C Naumand9abba82010-10-26 23:04:31 +0900132 writeb(sec, &rtc->bcdsec);
133 writeb(min, &rtc->bcdmin);
134 writeb(hour, &rtc->bcdhour);
135 writeb(mday, &rtc->bcddate);
136 writeb(wday, &rtc->bcdday);
137 writeb(mon, &rtc->bcdmon);
138 writeb(year, &rtc->bcdyear);
wdenk48b42612003-06-19 23:01:32 +0000139
140 /* disable access to RTC registers */
141 SetRTC_Access(RTC_DISABLE);
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +0200142
143 return 0;
wdenk48b42612003-06-19 23:01:32 +0000144}
145
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900146void rtc_reset(void)
wdenk48b42612003-06-19 23:01:32 +0000147{
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900148 struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc();
wdenk48b42612003-06-19 23:01:32 +0000149
C Naumand9abba82010-10-26 23:04:31 +0900150 writeb((readb(&rtc->rtccon) & ~0x06) | 0x08, &rtc->rtccon);
151 writeb(readb(&rtc->rtccon) & ~(0x08 | 0x01), &rtc->rtccon);
wdenk48b42612003-06-19 23:01:32 +0000152}
153
Jon Loeliger068b60a2007-07-10 10:27:39 -0500154#endif