blob: d8e67a01220ee9aa18698f224b093efecca477a9 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Arnd Bergmannd6faca42016-06-01 16:46:23 +02002#include <linux/bcd.h>
3#include <linux/delay.h>
4#include <linux/export.h>
5#include <linux/mc146818rtc.h>
6
7#ifdef CONFIG_ACPI
8#include <linux/acpi.h>
9#endif
10
Mateusz Jończykea6fa492021-12-10 21:01:26 +010011/*
12 * If the UIP (Update-in-progress) bit of the RTC is set for more then
13 * 10ms, the RTC is apparently broken or not present.
14 */
15bool mc146818_does_rtc_work(void)
16{
17 int i;
18 unsigned char val;
19 unsigned long flags;
20
21 for (i = 0; i < 10; i++) {
22 spin_lock_irqsave(&rtc_lock, flags);
23 val = CMOS_READ(RTC_FREQ_SELECT);
24 spin_unlock_irqrestore(&rtc_lock, flags);
25
26 if ((val & RTC_UIP) == 0)
27 return true;
28
29 mdelay(1);
30 }
31
32 return false;
33}
34EXPORT_SYMBOL_GPL(mc146818_does_rtc_work);
35
Arnd Bergmannd6faca42016-06-01 16:46:23 +020036unsigned int mc146818_get_time(struct rtc_time *time)
37{
38 unsigned char ctrl;
39 unsigned long flags;
Mateusz Jończykea6fa492021-12-10 21:01:26 +010040 unsigned int iter_count = 0;
Arnd Bergmannd6faca42016-06-01 16:46:23 +020041 unsigned char century = 0;
Thomas Gleixner05a03022020-12-06 22:46:14 +010042 bool retry;
Arnd Bergmannd6faca42016-06-01 16:46:23 +020043
44#ifdef CONFIG_MACH_DECSTATION
45 unsigned int real_year;
46#endif
47
Thomas Gleixner05a03022020-12-06 22:46:14 +010048again:
Mateusz Jończykea6fa492021-12-10 21:01:26 +010049 if (iter_count > 10) {
Mateusz Jończyk0dd8d6c2021-12-10 21:01:25 +010050 memset(time, 0, sizeof(*time));
Mateusz Jończykd35786b2021-12-10 21:01:24 +010051 return -EIO;
Thomas Gleixner211e5db2021-01-26 18:02:11 +010052 }
Mateusz Jończykea6fa492021-12-10 21:01:26 +010053 iter_count++;
54
55 spin_lock_irqsave(&rtc_lock, flags);
Thomas Gleixner211e5db2021-01-26 18:02:11 +010056
Arnd Bergmannd6faca42016-06-01 16:46:23 +020057 /*
Thomas Gleixner05a03022020-12-06 22:46:14 +010058 * Check whether there is an update in progress during which the
59 * readout is unspecified. The maximum update time is ~2ms. Poll
60 * every msec for completion.
61 *
62 * Store the second value before checking UIP so a long lasting NMI
63 * which happens to hit after the UIP check cannot make an update
64 * cycle invisible.
Arnd Bergmannd6faca42016-06-01 16:46:23 +020065 */
Thomas Gleixner05a03022020-12-06 22:46:14 +010066 time->tm_sec = CMOS_READ(RTC_SECONDS);
67
68 if (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP) {
69 spin_unlock_irqrestore(&rtc_lock, flags);
70 mdelay(1);
71 goto again;
72 }
73
74 /* Revalidate the above readout */
75 if (time->tm_sec != CMOS_READ(RTC_SECONDS)) {
76 spin_unlock_irqrestore(&rtc_lock, flags);
77 goto again;
78 }
Arnd Bergmannd6faca42016-06-01 16:46:23 +020079
80 /*
81 * Only the values that we read from the RTC are set. We leave
82 * tm_wday, tm_yday and tm_isdst untouched. Even though the
83 * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
84 * by the RTC when initially set to a non-zero value.
85 */
Arnd Bergmannd6faca42016-06-01 16:46:23 +020086 time->tm_min = CMOS_READ(RTC_MINUTES);
87 time->tm_hour = CMOS_READ(RTC_HOURS);
88 time->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH);
89 time->tm_mon = CMOS_READ(RTC_MONTH);
90 time->tm_year = CMOS_READ(RTC_YEAR);
91#ifdef CONFIG_MACH_DECSTATION
92 real_year = CMOS_READ(RTC_DEC_YEAR);
93#endif
94#ifdef CONFIG_ACPI
95 if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
96 acpi_gbl_FADT.century)
97 century = CMOS_READ(acpi_gbl_FADT.century);
98#endif
99 ctrl = CMOS_READ(RTC_CONTROL);
Thomas Gleixner05a03022020-12-06 22:46:14 +0100100 /*
101 * Check for the UIP bit again. If it is set now then
102 * the above values may contain garbage.
103 */
104 retry = CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP;
105 /*
106 * A NMI might have interrupted the above sequence so check whether
107 * the seconds value has changed which indicates that the NMI took
108 * longer than the UIP bit was set. Unlikely, but possible and
109 * there is also virt...
110 */
111 retry |= time->tm_sec != CMOS_READ(RTC_SECONDS);
112
Arnd Bergmannd6faca42016-06-01 16:46:23 +0200113 spin_unlock_irqrestore(&rtc_lock, flags);
114
Thomas Gleixner05a03022020-12-06 22:46:14 +0100115 if (retry)
116 goto again;
117
Arnd Bergmannd6faca42016-06-01 16:46:23 +0200118 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
119 {
120 time->tm_sec = bcd2bin(time->tm_sec);
121 time->tm_min = bcd2bin(time->tm_min);
122 time->tm_hour = bcd2bin(time->tm_hour);
123 time->tm_mday = bcd2bin(time->tm_mday);
124 time->tm_mon = bcd2bin(time->tm_mon);
125 time->tm_year = bcd2bin(time->tm_year);
126 century = bcd2bin(century);
127 }
128
129#ifdef CONFIG_MACH_DECSTATION
130 time->tm_year += real_year - 72;
131#endif
132
Eric Wong2a4daad2019-01-06 08:21:03 +0000133 if (century > 20)
Arnd Bergmannd6faca42016-06-01 16:46:23 +0200134 time->tm_year += (century - 19) * 100;
135
136 /*
137 * Account for differences between how the RTC uses the values
138 * and how they are defined in a struct rtc_time;
139 */
140 if (time->tm_year <= 69)
141 time->tm_year += 100;
142
143 time->tm_mon--;
144
Mateusz Jończykd35786b2021-12-10 21:01:24 +0100145 return 0;
Arnd Bergmannd6faca42016-06-01 16:46:23 +0200146}
147EXPORT_SYMBOL_GPL(mc146818_get_time);
148
149/* Set the current date and time in the real time clock. */
150int mc146818_set_time(struct rtc_time *time)
151{
152 unsigned long flags;
153 unsigned char mon, day, hrs, min, sec;
154 unsigned char save_control, save_freq_select;
155 unsigned int yrs;
156#ifdef CONFIG_MACH_DECSTATION
157 unsigned int real_yrs, leap_yr;
158#endif
159 unsigned char century = 0;
160
161 yrs = time->tm_year;
162 mon = time->tm_mon + 1; /* tm_mon starts at zero */
163 day = time->tm_mday;
164 hrs = time->tm_hour;
165 min = time->tm_min;
166 sec = time->tm_sec;
167
168 if (yrs > 255) /* They are unsigned */
169 return -EINVAL;
170
Arnd Bergmannd6faca42016-06-01 16:46:23 +0200171#ifdef CONFIG_MACH_DECSTATION
172 real_yrs = yrs;
173 leap_yr = ((!((yrs + 1900) % 4) && ((yrs + 1900) % 100)) ||
174 !((yrs + 1900) % 400));
175 yrs = 72;
176
177 /*
178 * We want to keep the year set to 73 until March
179 * for non-leap years, so that Feb, 29th is handled
180 * correctly.
181 */
182 if (!leap_yr && mon < 3) {
183 real_yrs--;
184 yrs = 73;
185 }
186#endif
187
188#ifdef CONFIG_ACPI
189 if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
190 acpi_gbl_FADT.century) {
191 century = (yrs + 1900) / 100;
192 yrs %= 100;
193 }
194#endif
195
196 /* These limits and adjustments are independent of
197 * whether the chip is in binary mode or not.
198 */
Thomas Gleixnerdcf257e2020-12-06 22:46:15 +0100199 if (yrs > 169)
Arnd Bergmannd6faca42016-06-01 16:46:23 +0200200 return -EINVAL;
Arnd Bergmannd6faca42016-06-01 16:46:23 +0200201
202 if (yrs >= 100)
203 yrs -= 100;
204
205 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY)
206 || RTC_ALWAYS_BCD) {
207 sec = bin2bcd(sec);
208 min = bin2bcd(min);
209 hrs = bin2bcd(hrs);
210 day = bin2bcd(day);
211 mon = bin2bcd(mon);
212 yrs = bin2bcd(yrs);
213 century = bin2bcd(century);
214 }
215
Thomas Gleixnerdcf257e2020-12-06 22:46:15 +0100216 spin_lock_irqsave(&rtc_lock, flags);
Arnd Bergmannd6faca42016-06-01 16:46:23 +0200217 save_control = CMOS_READ(RTC_CONTROL);
218 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
219 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
Alexandre Bellonif01f4ff2020-01-04 05:31:10 +0100220 CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
Arnd Bergmannd6faca42016-06-01 16:46:23 +0200221
222#ifdef CONFIG_MACH_DECSTATION
223 CMOS_WRITE(real_yrs, RTC_DEC_YEAR);
224#endif
225 CMOS_WRITE(yrs, RTC_YEAR);
226 CMOS_WRITE(mon, RTC_MONTH);
227 CMOS_WRITE(day, RTC_DAY_OF_MONTH);
228 CMOS_WRITE(hrs, RTC_HOURS);
229 CMOS_WRITE(min, RTC_MINUTES);
230 CMOS_WRITE(sec, RTC_SECONDS);
231#ifdef CONFIG_ACPI
232 if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
233 acpi_gbl_FADT.century)
234 CMOS_WRITE(century, acpi_gbl_FADT.century);
235#endif
236
237 CMOS_WRITE(save_control, RTC_CONTROL);
238 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
239
240 spin_unlock_irqrestore(&rtc_lock, flags);
241
242 return 0;
243}
244EXPORT_SYMBOL_GPL(mc146818_set_time);