xref: /openbmc/u-boot/drivers/rtc/s3c24x0_rtc.c (revision da4cfa6b)
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 #include <asm/arch/s3c24x0_cpu.h>
15 
16 #include <rtc.h>
17 #include <asm/io.h>
18 #include <linux/compiler.h>
19 
20 typedef enum {
21 	RTC_ENABLE,
22 	RTC_DISABLE
23 } RTC_ACCESS;
24 
25 
26 static inline void SetRTC_Access(RTC_ACCESS a)
27 {
28 	struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc();
29 
30 	switch (a) {
31 	case RTC_ENABLE:
32 		writeb(readb(&rtc->rtccon) | 0x01, &rtc->rtccon);
33 		break;
34 
35 	case RTC_DISABLE:
36 		writeb(readb(&rtc->rtccon) & ~0x01, &rtc->rtccon);
37 		break;
38 	}
39 }
40 
41 /* ------------------------------------------------------------------------- */
42 
43 int rtc_get(struct rtc_time *tmp)
44 {
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;
49 
50 	/* enable access to RTC registers */
51 	SetRTC_Access(RTC_ENABLE);
52 
53 	/* read RTC registers */
54 	do {
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));
63 
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);
72 
73 	/* disable access to RTC registers */
74 	SetRTC_Access(RTC_DISABLE);
75 
76 #ifdef RTC_DEBUG
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);
83 #endif
84 
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)
93 		tmp->tm_year += 2000;
94 	else
95 		tmp->tm_year += 1900;
96 	tmp->tm_yday  = 0;
97 	tmp->tm_isdst = 0;
98 #ifdef RTC_DEBUG
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);
102 #endif
103 
104 	return 0;
105 }
106 
107 int rtc_set(struct rtc_time *tmp)
108 {
109 	struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc();
110 	uchar sec, min, hour, mday, wday, mon, year;
111 
112 #ifdef RTC_DEBUG
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);
116 #endif
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);
124 
125 	/* enable access to RTC registers */
126 	SetRTC_Access(RTC_ENABLE);
127 
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);
136 
137 	/* disable access to RTC registers */
138 	SetRTC_Access(RTC_DISABLE);
139 
140 	return 0;
141 }
142 
143 void rtc_reset(void)
144 {
145 	struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc();
146 
147 	writeb((readb(&rtc->rtccon) & ~0x06) | 0x08, &rtc->rtccon);
148 	writeb(readb(&rtc->rtccon) & ~(0x08 | 0x01), &rtc->rtccon);
149 }
150