1 // SPDX-License-Identifier: GPL-2.0+
4 * David Müller ELSOFT AG Switzerland. d.mueller@elsoft.ch
8 * Date & Time support for the built-in Samsung S3C24X0 RTC
14 #include <asm/arch/s3c24x0_cpu.h>
18 #include <linux/compiler.h>
26 static inline void SetRTC_Access(RTC_ACCESS a)
28 struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc();
32 writeb(readb(&rtc->rtccon) | 0x01, &rtc->rtccon);
36 writeb(readb(&rtc->rtccon) & ~0x01, &rtc->rtccon);
41 /* ------------------------------------------------------------------------- */
43 int rtc_get(struct rtc_time *tmp)
45 struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc();
46 uchar sec, min, hour, mday, wday, mon, year;
47 __maybe_unused uchar a_sec, a_min, a_hour, a_date,
48 a_mon, a_year, a_armed;
50 /* enable access to RTC registers */
51 SetRTC_Access(RTC_ENABLE);
53 /* read RTC registers */
55 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));
64 /* read ALARM registers */
65 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);
73 /* disable access to RTC registers */
74 SetRTC_Access(RTC_DISABLE);
77 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);
85 tmp->tm_sec = bcd2bin(sec & 0x7F);
86 tmp->tm_min = bcd2bin(min & 0x7F);
87 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);
92 if (tmp->tm_year < 70)
99 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);
107 int rtc_set(struct rtc_time *tmp)
109 struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc();
110 uchar sec, min, hour, mday, wday, mon, year;
113 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);
117 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);
125 /* enable access to RTC registers */
126 SetRTC_Access(RTC_ENABLE);
128 /* write RTC registers */
129 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);
137 /* disable access to RTC registers */
138 SetRTC_Access(RTC_DISABLE);
145 struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc();
147 writeb((readb(&rtc->rtccon) & ~0x06) | 0x08, &rtc->rtccon);
148 writeb(readb(&rtc->rtccon) & ~(0x08 | 0x01), &rtc->rtccon);