Commit | Line | Data |
---|---|---|
48b42616 WD |
1 | /* |
2 | * (C) Copyright 2003 | |
fa82f871 | 3 | * David Müller ELSOFT AG Switzerland. d.mueller@elsoft.ch |
48b42616 | 4 | * |
1a459660 | 5 | * SPDX-License-Identifier: GPL-2.0+ |
48b42616 WD |
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 | ||
871c18dd | 15 | #if (defined(CONFIG_CMD_DATE)) |
48b42616 | 16 | |
ac67804f | 17 | #include <asm/arch/s3c24x0_cpu.h> |
48b42616 WD |
18 | |
19 | #include <rtc.h> | |
eb0ae7f5 | 20 | #include <asm/io.h> |
f45e91c4 | 21 | #include <linux/compiler.h> |
48b42616 WD |
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 | { | |
eb0ae7f5 | 31 | struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc(); |
32 | ||
48b42616 | 33 | switch (a) { |
eb0ae7f5 | 34 | case RTC_ENABLE: |
d9abba82 | 35 | writeb(readb(&rtc->rtccon) | 0x01, &rtc->rtccon); |
eb0ae7f5 | 36 | break; |
48b42616 | 37 | |
eb0ae7f5 | 38 | case RTC_DISABLE: |
d9abba82 | 39 | writeb(readb(&rtc->rtccon) & ~0x01, &rtc->rtccon); |
eb0ae7f5 | 40 | break; |
48b42616 WD |
41 | } |
42 | } | |
43 | ||
48b42616 WD |
44 | /* ------------------------------------------------------------------------- */ |
45 | ||
eb0ae7f5 | 46 | int rtc_get(struct rtc_time *tmp) |
48b42616 | 47 | { |
eb0ae7f5 | 48 | struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc(); |
48b42616 | 49 | uchar sec, min, hour, mday, wday, mon, year; |
f45e91c4 AG |
50 | __maybe_unused uchar a_sec, a_min, a_hour, a_date, |
51 | a_mon, a_year, a_armed; | |
48b42616 WD |
52 | |
53 | /* enable access to RTC registers */ | |
54 | SetRTC_Access(RTC_ENABLE); | |
55 | ||
56 | /* read RTC registers */ | |
531716e1 | 57 | do { |
d9abba82 N |
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)); | |
48b42616 WD |
66 | |
67 | /* read ALARM registers */ | |
d9abba82 N |
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); | |
48b42616 WD |
75 | |
76 | /* disable access to RTC registers */ | |
77 | SetRTC_Access(RTC_DISABLE); | |
78 | ||
79 | #ifdef RTC_DEBUG | |
eb0ae7f5 | 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); | |
48b42616 WD |
86 | #endif |
87 | ||
eb0ae7f5 | 88 | tmp->tm_sec = bcd2bin(sec & 0x7F); |
89 | tmp->tm_min = bcd2bin(min & 0x7F); | |
48b42616 WD |
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); | |
eb0ae7f5 | 95 | if (tmp->tm_year < 70) |
96 | tmp->tm_year += 2000; | |
48b42616 | 97 | else |
eb0ae7f5 | 98 | tmp->tm_year += 1900; |
99 | tmp->tm_yday = 0; | |
100 | tmp->tm_isdst = 0; | |
48b42616 | 101 | #ifdef RTC_DEBUG |
eb0ae7f5 | 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); | |
48b42616 | 105 | #endif |
b73a19e1 YT |
106 | |
107 | return 0; | |
48b42616 WD |
108 | } |
109 | ||
eb0ae7f5 | 110 | int rtc_set(struct rtc_time *tmp) |
48b42616 | 111 | { |
eb0ae7f5 | 112 | struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc(); |
48b42616 WD |
113 | uchar sec, min, hour, mday, wday, mon, year; |
114 | ||
115 | #ifdef RTC_DEBUG | |
eb0ae7f5 | 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); | |
48b42616 | 119 | #endif |
eb0ae7f5 | 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); | |
48b42616 WD |
127 | |
128 | /* enable access to RTC registers */ | |
129 | SetRTC_Access(RTC_ENABLE); | |
130 | ||
131 | /* write RTC registers */ | |
d9abba82 N |
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); | |
48b42616 WD |
139 | |
140 | /* disable access to RTC registers */ | |
141 | SetRTC_Access(RTC_DISABLE); | |
d1e23194 JCPV |
142 | |
143 | return 0; | |
48b42616 WD |
144 | } |
145 | ||
eb0ae7f5 | 146 | void rtc_reset(void) |
48b42616 | 147 | { |
eb0ae7f5 | 148 | struct s3c24x0_rtc *rtc = s3c24x0_get_base_rtc(); |
48b42616 | 149 | |
d9abba82 N |
150 | writeb((readb(&rtc->rtccon) & ~0x06) | 0x08, &rtc->rtccon); |
151 | writeb(readb(&rtc->rtccon) & ~(0x08 | 0x01), &rtc->rtccon); | |
48b42616 WD |
152 | } |
153 | ||
068b60a0 | 154 | #endif |