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 #if (defined(CONFIG_CMD_DATE))
16 #include <asm/arch/s3c24x0_cpu.h>
20 #include <linux/compiler.h>
28 static inline void SetRTC_Access(RTC_ACCESS a)
30 struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc();
34 writeb(readb(&rtc->rtccon) | 0x01, &rtc->rtccon);
38 writeb(readb(&rtc->rtccon) & ~0x01, &rtc->rtccon);
43 /* ------------------------------------------------------------------------- */
45 int rtc_get(struct rtc_time *tmp)
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;
52 /* enable access to RTC registers */
53 SetRTC_Access(RTC_ENABLE);
55 /* read RTC registers */
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));
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);
75 /* disable access to RTC registers */
76 SetRTC_Access(RTC_DISABLE);
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);
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)
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);
109 int rtc_set(struct rtc_time *tmp)
111 struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc();
112 uchar sec, min, hour, mday, wday, mon, year;
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);
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);
127 /* enable access to RTC registers */
128 SetRTC_Access(RTC_ENABLE);
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);
139 /* disable access to RTC registers */
140 SetRTC_Access(RTC_DISABLE);
147 struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc();
149 writeb((readb(&rtc->rtccon) & ~0x06) | 0x08, &rtc->rtccon);
150 writeb(readb(&rtc->rtccon) & ~(0x08 | 0x01), &rtc->rtccon);