blob: 1253085f5359f21d4c2b15b5ba32b5423aed3330 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk48b42612003-06-19 23:01:32 +00002/*
3 * (C) Copyright 2003
Albert ARIBAUDfa82f872011-08-04 18:45:45 +02004 * David Müller ELSOFT AG Switzerland. d.mueller@elsoft.ch
wdenk48b42612003-06-19 23:01:32 +00005 */
6
7/*
8 * Date & Time support for the built-in Samsung S3C24X0 RTC
9 */
10
11#include <common.h>
12#include <command.h>
13
Michal Simek871c18d2008-07-14 19:45:37 +020014#if (defined(CONFIG_CMD_DATE))
wdenk48b42612003-06-19 23:01:32 +000015
kevin.morfitt@fearnside-systems.co.ukac678042009-11-17 18:30:34 +090016#include <asm/arch/s3c24x0_cpu.h>
wdenk48b42612003-06-19 23:01:32 +000017
18#include <rtc.h>
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090019#include <asm/io.h>
Anatolij Gustschinf45e91c2011-11-15 13:21:03 +000020#include <linux/compiler.h>
wdenk48b42612003-06-19 23:01:32 +000021
22typedef enum {
23 RTC_ENABLE,
24 RTC_DISABLE
25} RTC_ACCESS;
26
27
28static inline void SetRTC_Access(RTC_ACCESS a)
29{
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090030 struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc();
wdenk48b42612003-06-19 23:01:32 +000031
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090032 switch (a) {
33 case RTC_ENABLE:
C Naumand9abba82010-10-26 23:04:31 +090034 writeb(readb(&rtc->rtccon) | 0x01, &rtc->rtccon);
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090035 break;
36
37 case RTC_DISABLE:
C Naumand9abba82010-10-26 23:04:31 +090038 writeb(readb(&rtc->rtccon) & ~0x01, &rtc->rtccon);
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090039 break;
wdenk48b42612003-06-19 23:01:32 +000040 }
41}
42
wdenk48b42612003-06-19 23:01:32 +000043/* ------------------------------------------------------------------------- */
44
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090045int rtc_get(struct rtc_time *tmp)
wdenk48b42612003-06-19 23:01:32 +000046{
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090047 struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc();
wdenk48b42612003-06-19 23:01:32 +000048 uchar sec, min, hour, mday, wday, mon, year;
Anatolij Gustschinf45e91c2011-11-15 13:21:03 +000049 __maybe_unused uchar a_sec, a_min, a_hour, a_date,
50 a_mon, a_year, a_armed;
wdenk48b42612003-06-19 23:01:32 +000051
52 /* enable access to RTC registers */
53 SetRTC_Access(RTC_ENABLE);
54
55 /* read RTC registers */
wdenk531716e2003-09-13 19:01:12 +000056 do {
C Naumand9abba82010-10-26 23:04:31 +090057 sec = readb(&rtc->bcdsec);
58 min = readb(&rtc->bcdmin);
59 hour = readb(&rtc->bcdhour);
60 mday = readb(&rtc->bcddate);
61 wday = readb(&rtc->bcdday);
62 mon = readb(&rtc->bcdmon);
63 year = readb(&rtc->bcdyear);
64 } while (sec != readb(&rtc->bcdsec));
wdenk48b42612003-06-19 23:01:32 +000065
66 /* read ALARM registers */
C Naumand9abba82010-10-26 23:04:31 +090067 a_sec = readb(&rtc->almsec);
68 a_min = readb(&rtc->almmin);
69 a_hour = readb(&rtc->almhour);
70 a_date = readb(&rtc->almdate);
71 a_mon = readb(&rtc->almmon);
72 a_year = readb(&rtc->almyear);
73 a_armed = readb(&rtc->rtcalm);
wdenk48b42612003-06-19 23:01:32 +000074
75 /* disable access to RTC registers */
76 SetRTC_Access(RTC_DISABLE);
77
78#ifdef RTC_DEBUG
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090079 printf("Get RTC year: %02x mon/cent: %02x mday: %02x wday: %02x "
80 "hr: %02x min: %02x sec: %02x\n",
81 year, mon, mday, wday, hour, min, sec);
82 printf("Alarms: %02x: year: %02x month: %02x date: %02x hour: "
83 "%02x min: %02x sec: %02x\n",
84 a_armed, a_year, a_mon, a_date, a_hour, a_min, a_sec);
wdenk48b42612003-06-19 23:01:32 +000085#endif
86
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090087 tmp->tm_sec = bcd2bin(sec & 0x7F);
88 tmp->tm_min = bcd2bin(min & 0x7F);
wdenk48b42612003-06-19 23:01:32 +000089 tmp->tm_hour = bcd2bin(hour & 0x3F);
90 tmp->tm_mday = bcd2bin(mday & 0x3F);
91 tmp->tm_mon = bcd2bin(mon & 0x1F);
92 tmp->tm_year = bcd2bin(year);
93 tmp->tm_wday = bcd2bin(wday & 0x07);
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090094 if (tmp->tm_year < 70)
95 tmp->tm_year += 2000;
wdenk48b42612003-06-19 23:01:32 +000096 else
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +090097 tmp->tm_year += 1900;
98 tmp->tm_yday = 0;
99 tmp->tm_isdst = 0;
wdenk48b42612003-06-19 23:01:32 +0000100#ifdef RTC_DEBUG
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900101 printf("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
102 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
103 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
wdenk48b42612003-06-19 23:01:32 +0000104#endif
Yuri Tikhonovb73a19e2008-03-20 17:56:04 +0300105
106 return 0;
wdenk48b42612003-06-19 23:01:32 +0000107}
108
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900109int rtc_set(struct rtc_time *tmp)
wdenk48b42612003-06-19 23:01:32 +0000110{
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900111 struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc();
wdenk48b42612003-06-19 23:01:32 +0000112 uchar sec, min, hour, mday, wday, mon, year;
113
114#ifdef RTC_DEBUG
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900115 printf("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
116 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
117 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
wdenk48b42612003-06-19 23:01:32 +0000118#endif
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900119 year = bin2bcd(tmp->tm_year % 100);
120 mon = bin2bcd(tmp->tm_mon);
121 wday = bin2bcd(tmp->tm_wday);
122 mday = bin2bcd(tmp->tm_mday);
123 hour = bin2bcd(tmp->tm_hour);
124 min = bin2bcd(tmp->tm_min);
125 sec = bin2bcd(tmp->tm_sec);
wdenk48b42612003-06-19 23:01:32 +0000126
127 /* enable access to RTC registers */
128 SetRTC_Access(RTC_ENABLE);
129
130 /* write RTC registers */
C Naumand9abba82010-10-26 23:04:31 +0900131 writeb(sec, &rtc->bcdsec);
132 writeb(min, &rtc->bcdmin);
133 writeb(hour, &rtc->bcdhour);
134 writeb(mday, &rtc->bcddate);
135 writeb(wday, &rtc->bcdday);
136 writeb(mon, &rtc->bcdmon);
137 writeb(year, &rtc->bcdyear);
wdenk48b42612003-06-19 23:01:32 +0000138
139 /* disable access to RTC registers */
140 SetRTC_Access(RTC_DISABLE);
Jean-Christophe PLAGNIOL-VILLARDd1e23192008-09-01 23:06:23 +0200141
142 return 0;
wdenk48b42612003-06-19 23:01:32 +0000143}
144
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900145void rtc_reset(void)
wdenk48b42612003-06-19 23:01:32 +0000146{
kevin.morfitt@fearnside-systems.co.ukeb0ae7f2009-10-10 13:33:11 +0900147 struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc();
wdenk48b42612003-06-19 23:01:32 +0000148
C Naumand9abba82010-10-26 23:04:31 +0900149 writeb((readb(&rtc->rtccon) & ~0x06) | 0x08, &rtc->rtccon);
150 writeb(readb(&rtc->rtccon) & ~(0x08 | 0x01), &rtc->rtccon);
wdenk48b42612003-06-19 23:01:32 +0000151}
152
Jon Loeliger068b60a2007-07-10 10:27:39 -0500153#endif