]> git.sur5r.net Git - u-boot/blob - drivers/rtc/s3c24x0_rtc.c
dm: core: Update of_read_fmap_entry() for livetree
[u-boot] / drivers / rtc / s3c24x0_rtc.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2003
4  * David Müller ELSOFT AG Switzerland. d.mueller@elsoft.ch
5  */
6
7 /*
8  * Date & Time support for the built-in Samsung S3C24X0 RTC
9  */
10
11 #include <common.h>
12 #include <command.h>
13
14 #if (defined(CONFIG_CMD_DATE))
15
16 #include <asm/arch/s3c24x0_cpu.h>
17
18 #include <rtc.h>
19 #include <asm/io.h>
20 #include <linux/compiler.h>
21
22 typedef enum {
23         RTC_ENABLE,
24         RTC_DISABLE
25 } RTC_ACCESS;
26
27
28 static inline void SetRTC_Access(RTC_ACCESS a)
29 {
30         struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc();
31
32         switch (a) {
33         case RTC_ENABLE:
34                 writeb(readb(&rtc->rtccon) | 0x01, &rtc->rtccon);
35                 break;
36
37         case RTC_DISABLE:
38                 writeb(readb(&rtc->rtccon) & ~0x01, &rtc->rtccon);
39                 break;
40         }
41 }
42
43 /* ------------------------------------------------------------------------- */
44
45 int rtc_get(struct rtc_time *tmp)
46 {
47         struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc();
48         uchar sec, min, hour, mday, wday, mon, year;
49         __maybe_unused uchar a_sec, a_min, a_hour, a_date,
50                              a_mon, a_year, a_armed;
51
52         /* enable access to RTC registers */
53         SetRTC_Access(RTC_ENABLE);
54
55         /* read RTC registers */
56         do {
57                 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));
65
66         /* read ALARM registers */
67         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);
74
75         /* disable access to RTC registers */
76         SetRTC_Access(RTC_DISABLE);
77
78 #ifdef RTC_DEBUG
79         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);
85 #endif
86
87         tmp->tm_sec  = bcd2bin(sec & 0x7F);
88         tmp->tm_min  = bcd2bin(min & 0x7F);
89         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);
94         if (tmp->tm_year < 70)
95                 tmp->tm_year += 2000;
96         else
97                 tmp->tm_year += 1900;
98         tmp->tm_yday  = 0;
99         tmp->tm_isdst = 0;
100 #ifdef RTC_DEBUG
101         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);
104 #endif
105
106         return 0;
107 }
108
109 int rtc_set(struct rtc_time *tmp)
110 {
111         struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc();
112         uchar sec, min, hour, mday, wday, mon, year;
113
114 #ifdef RTC_DEBUG
115         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);
118 #endif
119         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);
126
127         /* enable access to RTC registers */
128         SetRTC_Access(RTC_ENABLE);
129
130         /* write RTC registers */
131         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);
138
139         /* disable access to RTC registers */
140         SetRTC_Access(RTC_DISABLE);
141
142         return 0;
143 }
144
145 void rtc_reset(void)
146 {
147         struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc();
148
149         writeb((readb(&rtc->rtccon) & ~0x06) | 0x08, &rtc->rtccon);
150         writeb(readb(&rtc->rtccon) & ~(0x08 | 0x01), &rtc->rtccon);
151 }
152
153 #endif