blob: 96ea3cf878ee10df59d3f66cc207708cc39956ea [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk7539dea2003-06-19 23:01:32 +00002/*
3 * (C) Copyright 2003
Albert ARIBAUD60fbc8d2011-08-04 18:45:45 +02004 * David Müller ELSOFT AG Switzerland. d.mueller@elsoft.ch
wdenk7539dea2003-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
kevin.morfitt@fearnside-systems.co.uke0d81312009-11-17 18:30:34 +090014#include <asm/arch/s3c24x0_cpu.h>
wdenk7539dea2003-06-19 23:01:32 +000015
16#include <rtc.h>
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090017#include <asm/io.h>
Anatolij Gustschin49a358b2011-11-15 13:21:03 +000018#include <linux/compiler.h>
wdenk7539dea2003-06-19 23:01:32 +000019
20typedef enum {
21 RTC_ENABLE,
22 RTC_DISABLE
23} RTC_ACCESS;
24
25
26static inline void SetRTC_Access(RTC_ACCESS a)
27{
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090028 struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc();
29
wdenk7539dea2003-06-19 23:01:32 +000030 switch (a) {
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090031 case RTC_ENABLE:
C Nauman383c43e2010-10-26 23:04:31 +090032 writeb(readb(&rtc->rtccon) | 0x01, &rtc->rtccon);
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090033 break;
wdenk7539dea2003-06-19 23:01:32 +000034
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090035 case RTC_DISABLE:
C Nauman383c43e2010-10-26 23:04:31 +090036 writeb(readb(&rtc->rtccon) & ~0x01, &rtc->rtccon);
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090037 break;
wdenk7539dea2003-06-19 23:01:32 +000038 }
39}
40
wdenk7539dea2003-06-19 23:01:32 +000041/* ------------------------------------------------------------------------- */
42
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090043int rtc_get(struct rtc_time *tmp)
wdenk7539dea2003-06-19 23:01:32 +000044{
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090045 struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc();
wdenk7539dea2003-06-19 23:01:32 +000046 uchar sec, min, hour, mday, wday, mon, year;
Anatolij Gustschin49a358b2011-11-15 13:21:03 +000047 __maybe_unused uchar a_sec, a_min, a_hour, a_date,
48 a_mon, a_year, a_armed;
wdenk7539dea2003-06-19 23:01:32 +000049
50 /* enable access to RTC registers */
51 SetRTC_Access(RTC_ENABLE);
52
53 /* read RTC registers */
wdenk25521902003-09-13 19:01:12 +000054 do {
C Nauman383c43e2010-10-26 23:04:31 +090055 sec = readb(&rtc->bcdsec);
56 min = readb(&rtc->bcdmin);
57 hour = readb(&rtc->bcdhour);
58 mday = readb(&rtc->bcddate);
59 wday = readb(&rtc->bcdday);
60 mon = readb(&rtc->bcdmon);
61 year = readb(&rtc->bcdyear);
62 } while (sec != readb(&rtc->bcdsec));
wdenk7539dea2003-06-19 23:01:32 +000063
64 /* read ALARM registers */
C Nauman383c43e2010-10-26 23:04:31 +090065 a_sec = readb(&rtc->almsec);
66 a_min = readb(&rtc->almmin);
67 a_hour = readb(&rtc->almhour);
68 a_date = readb(&rtc->almdate);
69 a_mon = readb(&rtc->almmon);
70 a_year = readb(&rtc->almyear);
71 a_armed = readb(&rtc->rtcalm);
wdenk7539dea2003-06-19 23:01:32 +000072
73 /* disable access to RTC registers */
74 SetRTC_Access(RTC_DISABLE);
75
76#ifdef RTC_DEBUG
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090077 printf("Get RTC year: %02x mon/cent: %02x mday: %02x wday: %02x "
78 "hr: %02x min: %02x sec: %02x\n",
79 year, mon, mday, wday, hour, min, sec);
80 printf("Alarms: %02x: year: %02x month: %02x date: %02x hour: "
81 "%02x min: %02x sec: %02x\n",
82 a_armed, a_year, a_mon, a_date, a_hour, a_min, a_sec);
wdenk7539dea2003-06-19 23:01:32 +000083#endif
84
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090085 tmp->tm_sec = bcd2bin(sec & 0x7F);
86 tmp->tm_min = bcd2bin(min & 0x7F);
wdenk7539dea2003-06-19 23:01:32 +000087 tmp->tm_hour = bcd2bin(hour & 0x3F);
88 tmp->tm_mday = bcd2bin(mday & 0x3F);
89 tmp->tm_mon = bcd2bin(mon & 0x1F);
90 tmp->tm_year = bcd2bin(year);
91 tmp->tm_wday = bcd2bin(wday & 0x07);
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090092 if (tmp->tm_year < 70)
93 tmp->tm_year += 2000;
wdenk7539dea2003-06-19 23:01:32 +000094 else
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090095 tmp->tm_year += 1900;
96 tmp->tm_yday = 0;
97 tmp->tm_isdst = 0;
wdenk7539dea2003-06-19 23:01:32 +000098#ifdef RTC_DEBUG
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +090099 printf("Get DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
100 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
101 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
wdenk7539dea2003-06-19 23:01:32 +0000102#endif
Yuri Tikhonov9bacd942008-03-20 17:56:04 +0300103
104 return 0;
wdenk7539dea2003-06-19 23:01:32 +0000105}
106
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900107int rtc_set(struct rtc_time *tmp)
wdenk7539dea2003-06-19 23:01:32 +0000108{
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900109 struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc();
wdenk7539dea2003-06-19 23:01:32 +0000110 uchar sec, min, hour, mday, wday, mon, year;
111
112#ifdef RTC_DEBUG
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900113 printf("Set DATE: %4d-%02d-%02d (wday=%d) TIME: %2d:%02d:%02d\n",
114 tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
115 tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
wdenk7539dea2003-06-19 23:01:32 +0000116#endif
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900117 year = bin2bcd(tmp->tm_year % 100);
118 mon = bin2bcd(tmp->tm_mon);
119 wday = bin2bcd(tmp->tm_wday);
120 mday = bin2bcd(tmp->tm_mday);
121 hour = bin2bcd(tmp->tm_hour);
122 min = bin2bcd(tmp->tm_min);
123 sec = bin2bcd(tmp->tm_sec);
wdenk7539dea2003-06-19 23:01:32 +0000124
125 /* enable access to RTC registers */
126 SetRTC_Access(RTC_ENABLE);
127
128 /* write RTC registers */
C Nauman383c43e2010-10-26 23:04:31 +0900129 writeb(sec, &rtc->bcdsec);
130 writeb(min, &rtc->bcdmin);
131 writeb(hour, &rtc->bcdhour);
132 writeb(mday, &rtc->bcddate);
133 writeb(wday, &rtc->bcdday);
134 writeb(mon, &rtc->bcdmon);
135 writeb(year, &rtc->bcdyear);
wdenk7539dea2003-06-19 23:01:32 +0000136
137 /* disable access to RTC registers */
138 SetRTC_Access(RTC_DISABLE);
Jean-Christophe PLAGNIOL-VILLARD97a2e102008-09-01 23:06:23 +0200139
140 return 0;
wdenk7539dea2003-06-19 23:01:32 +0000141}
142
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900143void rtc_reset(void)
wdenk7539dea2003-06-19 23:01:32 +0000144{
kevin.morfitt@fearnside-systems.co.uk1464f4d2009-10-10 13:33:11 +0900145 struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc();
wdenk7539dea2003-06-19 23:01:32 +0000146
C Nauman383c43e2010-10-26 23:04:31 +0900147 writeb((readb(&rtc->rtccon) & ~0x06) | 0x08, &rtc->rtccon);
148 writeb(readb(&rtc->rtccon) & ~(0x08 | 0x01), &rtc->rtccon);
wdenk7539dea2003-06-19 23:01:32 +0000149}