xref: /openbmc/u-boot/drivers/rtc/s3c24x0_rtc.c (revision ae51b570)
1 /*
2  * (C) Copyright 2003
3  * David Müller ELSOFT AG Switzerland. d.mueller@elsoft.ch
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 /*
9  * Date & Time support for the built-in Samsung S3C24X0 RTC
10  */
11 
12 #include <common.h>
13 #include <command.h>
14 
15 #if (defined(CONFIG_CMD_DATE))
16 
17 #include <asm/arch/s3c24x0_cpu.h>
18 
19 #include <rtc.h>
20 #include <asm/io.h>
21 #include <linux/compiler.h>
22 
23 typedef enum {
24 	RTC_ENABLE,
25 	RTC_DISABLE
26 } RTC_ACCESS;
27 
28 
29 static inline void SetRTC_Access(RTC_ACCESS a)
30 {
31 	struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc();
32 
33 	switch (a) {
34 	case RTC_ENABLE:
35 		writeb(readb(&rtc->rtccon) | 0x01, &rtc->rtccon);
36 		break;
37 
38 	case RTC_DISABLE:
39 		writeb(readb(&rtc->rtccon) & ~0x01, &rtc->rtccon);
40 		break;
41 	}
42 }
43 
44 /* ------------------------------------------------------------------------- */
45 
46 int rtc_get(struct rtc_time *tmp)
47 {
48 	struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc();
49 	uchar sec, min, hour, mday, wday, mon, year;
50 	__maybe_unused uchar a_sec, a_min, a_hour, a_date,
51 			     a_mon, a_year, a_armed;
52 
53 	/* enable access to RTC registers */
54 	SetRTC_Access(RTC_ENABLE);
55 
56 	/* read RTC registers */
57 	do {
58 		sec  = readb(&rtc->bcdsec);
59 		min  = readb(&rtc->bcdmin);
60 		hour = readb(&rtc->bcdhour);
61 		mday = readb(&rtc->bcddate);
62 		wday = readb(&rtc->bcdday);
63 		mon  = readb(&rtc->bcdmon);
64 		year = readb(&rtc->bcdyear);
65 	} while (sec != readb(&rtc->bcdsec));
66 
67 	/* read ALARM registers */
68 	a_sec   = readb(&rtc->almsec);
69 	a_min   = readb(&rtc->almmin);
70 	a_hour  = readb(&rtc->almhour);
71 	a_date  = readb(&rtc->almdate);
72 	a_mon   = readb(&rtc->almmon);
73 	a_year  = readb(&rtc->almyear);
74 	a_armed = readb(&rtc->rtcalm);
75 
76 	/* disable access to RTC registers */
77 	SetRTC_Access(RTC_DISABLE);
78 
79 #ifdef RTC_DEBUG
80 	printf("Get RTC year: %02x mon/cent: %02x mday: %02x wday: %02x "
81 	       "hr: %02x min: %02x sec: %02x\n",
82 	       year, mon, mday, wday, hour, min, sec);
83 	printf("Alarms: %02x: year: %02x month: %02x date: %02x hour: "
84 	       "%02x min: %02x sec: %02x\n",
85 	       a_armed, a_year, a_mon, a_date, a_hour, a_min, a_sec);
86 #endif
87 
88 	tmp->tm_sec  = bcd2bin(sec & 0x7F);
89 	tmp->tm_min  = bcd2bin(min & 0x7F);
90 	tmp->tm_hour = bcd2bin(hour & 0x3F);
91 	tmp->tm_mday = bcd2bin(mday & 0x3F);
92 	tmp->tm_mon  = bcd2bin(mon & 0x1F);
93 	tmp->tm_year = bcd2bin(year);
94 	tmp->tm_wday = bcd2bin(wday & 0x07);
95 	if (tmp->tm_year < 70)
96 		tmp->tm_year += 2000;
97 	else
98 		tmp->tm_year += 1900;
99 	tmp->tm_yday  = 0;
100 	tmp->tm_isdst = 0;
101 #ifdef RTC_DEBUG
102 	printf("Get DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
103 	       tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
104 	       tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
105 #endif
106 
107 	return 0;
108 }
109 
110 int rtc_set(struct rtc_time *tmp)
111 {
112 	struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc();
113 	uchar sec, min, hour, mday, wday, mon, year;
114 
115 #ifdef RTC_DEBUG
116 	printf("Set DATE: %4d-%02d-%02d (wday=%d)  TIME: %2d:%02d:%02d\n",
117 	       tmp->tm_year, tmp->tm_mon, tmp->tm_mday, tmp->tm_wday,
118 	       tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
119 #endif
120 	year = bin2bcd(tmp->tm_year % 100);
121 	mon  = bin2bcd(tmp->tm_mon);
122 	wday = bin2bcd(tmp->tm_wday);
123 	mday = bin2bcd(tmp->tm_mday);
124 	hour = bin2bcd(tmp->tm_hour);
125 	min  = bin2bcd(tmp->tm_min);
126 	sec  = bin2bcd(tmp->tm_sec);
127 
128 	/* enable access to RTC registers */
129 	SetRTC_Access(RTC_ENABLE);
130 
131 	/* write RTC registers */
132 	writeb(sec, &rtc->bcdsec);
133 	writeb(min, &rtc->bcdmin);
134 	writeb(hour, &rtc->bcdhour);
135 	writeb(mday, &rtc->bcddate);
136 	writeb(wday, &rtc->bcdday);
137 	writeb(mon, &rtc->bcdmon);
138 	writeb(year, &rtc->bcdyear);
139 
140 	/* disable access to RTC registers */
141 	SetRTC_Access(RTC_DISABLE);
142 
143 	return 0;
144 }
145 
146 void rtc_reset(void)
147 {
148 	struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc();
149 
150 	writeb((readb(&rtc->rtccon) & ~0x06) | 0x08, &rtc->rtccon);
151 	writeb(readb(&rtc->rtccon) & ~(0x08 | 0x01), &rtc->rtccon);
152 }
153 
154 #endif
155