1 // SPDX-License-Identifier: GPL-2.0-only
3 #include <linux/delay.h>
4 #include <linux/export.h>
5 #include <linux/mc146818rtc.h>
8 #include <linux/acpi.h>
11 #define UIP_RECHECK_DELAY 100 /* usec */
12 #define UIP_RECHECK_DELAY_MS (USEC_PER_MSEC / UIP_RECHECK_DELAY)
13 #define UIP_RECHECK_LOOPS_MS(x) (x / UIP_RECHECK_DELAY_MS)
16 * Execute a function while the UIP (Update-in-progress) bit of the RTC is
17 * unset. The timeout is configurable by the caller in ms.
19 * Warning: callback may be executed more then once.
21 bool mc146818_avoid_UIP(void (*callback
)(unsigned char seconds
, void *param
),
27 unsigned char seconds
;
29 for (i
= 0; UIP_RECHECK_LOOPS_MS(i
) < timeout
; i
++) {
30 spin_lock_irqsave(&rtc_lock
, flags
);
33 * Check whether there is an update in progress during which the
34 * readout is unspecified. The maximum update time is ~2ms. Poll
37 * Store the second value before checking UIP so a long lasting
38 * NMI which happens to hit after the UIP check cannot make
39 * an update cycle invisible.
41 seconds
= CMOS_READ(RTC_SECONDS
);
43 if (CMOS_READ(RTC_FREQ_SELECT
) & RTC_UIP
) {
44 spin_unlock_irqrestore(&rtc_lock
, flags
);
45 udelay(UIP_RECHECK_DELAY
);
49 /* Revalidate the above readout */
50 if (seconds
!= CMOS_READ(RTC_SECONDS
)) {
51 spin_unlock_irqrestore(&rtc_lock
, flags
);
56 callback(seconds
, param
);
59 * Check for the UIP bit again. If it is set now then
60 * the above values may contain garbage.
62 if (CMOS_READ(RTC_FREQ_SELECT
) & RTC_UIP
) {
63 spin_unlock_irqrestore(&rtc_lock
, flags
);
64 udelay(UIP_RECHECK_DELAY
);
69 * A NMI might have interrupted the above sequence so check
70 * whether the seconds value has changed which indicates that
71 * the NMI took longer than the UIP bit was set. Unlikely, but
72 * possible and there is also virt...
74 if (seconds
!= CMOS_READ(RTC_SECONDS
)) {
75 spin_unlock_irqrestore(&rtc_lock
, flags
);
78 spin_unlock_irqrestore(&rtc_lock
, flags
);
80 if (UIP_RECHECK_LOOPS_MS(i
) >= 100)
81 pr_warn("Reading current time from RTC took around %li ms\n",
82 UIP_RECHECK_LOOPS_MS(i
));
88 EXPORT_SYMBOL_GPL(mc146818_avoid_UIP
);
91 * If the UIP (Update-in-progress) bit of the RTC is set for more then
92 * 10ms, the RTC is apparently broken or not present.
94 bool mc146818_does_rtc_work(void)
96 return mc146818_avoid_UIP(NULL
, 1000, NULL
);
98 EXPORT_SYMBOL_GPL(mc146818_does_rtc_work
);
100 struct mc146818_get_time_callback_param
{
101 struct rtc_time
*time
;
104 unsigned char century
;
106 #ifdef CONFIG_MACH_DECSTATION
107 unsigned int real_year
;
111 static void mc146818_get_time_callback(unsigned char seconds
, void *param_in
)
113 struct mc146818_get_time_callback_param
*p
= param_in
;
116 * Only the values that we read from the RTC are set. We leave
117 * tm_wday, tm_yday and tm_isdst untouched. Even though the
118 * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
119 * by the RTC when initially set to a non-zero value.
121 p
->time
->tm_sec
= seconds
;
122 p
->time
->tm_min
= CMOS_READ(RTC_MINUTES
);
123 p
->time
->tm_hour
= CMOS_READ(RTC_HOURS
);
124 p
->time
->tm_mday
= CMOS_READ(RTC_DAY_OF_MONTH
);
125 p
->time
->tm_mon
= CMOS_READ(RTC_MONTH
);
126 p
->time
->tm_year
= CMOS_READ(RTC_YEAR
);
127 #ifdef CONFIG_MACH_DECSTATION
128 p
->real_year
= CMOS_READ(RTC_DEC_YEAR
);
131 if (acpi_gbl_FADT
.header
.revision
>= FADT2_REVISION_ID
&&
132 acpi_gbl_FADT
.century
) {
133 p
->century
= CMOS_READ(acpi_gbl_FADT
.century
);
139 p
->ctrl
= CMOS_READ(RTC_CONTROL
);
143 * mc146818_get_time - Get the current time from the RTC
144 * @time: pointer to struct rtc_time to store the current time
145 * @timeout: timeout value in ms
147 * This function reads the current time from the RTC and stores it in the
148 * provided struct rtc_time. The timeout parameter specifies the maximum
149 * time to wait for the RTC to become ready.
151 * Return: 0 on success, -ETIMEDOUT if the RTC did not become ready within
152 * the specified timeout, or another error code if an error occurred.
154 int mc146818_get_time(struct rtc_time
*time
, int timeout
)
156 struct mc146818_get_time_callback_param p
= {
160 if (!mc146818_avoid_UIP(mc146818_get_time_callback
, timeout
, &p
)) {
161 memset(time
, 0, sizeof(*time
));
165 if (!(p
.ctrl
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
)
167 time
->tm_sec
= bcd2bin(time
->tm_sec
);
168 time
->tm_min
= bcd2bin(time
->tm_min
);
169 time
->tm_hour
= bcd2bin(time
->tm_hour
);
170 time
->tm_mday
= bcd2bin(time
->tm_mday
);
171 time
->tm_mon
= bcd2bin(time
->tm_mon
);
172 time
->tm_year
= bcd2bin(time
->tm_year
);
174 p
.century
= bcd2bin(p
.century
);
178 #ifdef CONFIG_MACH_DECSTATION
179 time
->tm_year
+= p
.real_year
- 72;
184 time
->tm_year
+= (p
.century
- 19) * 100;
188 * Account for differences between how the RTC uses the values
189 * and how they are defined in a struct rtc_time;
191 if (time
->tm_year
<= 69)
192 time
->tm_year
+= 100;
198 EXPORT_SYMBOL_GPL(mc146818_get_time
);
200 /* AMD systems don't allow access to AltCentury with DV1 */
201 static bool apply_amd_register_a_behavior(void)
204 if (boot_cpu_data
.x86_vendor
== X86_VENDOR_AMD
||
205 boot_cpu_data
.x86_vendor
== X86_VENDOR_HYGON
)
211 /* Set the current date and time in the real time clock. */
212 int mc146818_set_time(struct rtc_time
*time
)
215 unsigned char mon
, day
, hrs
, min
, sec
;
216 unsigned char save_control
, save_freq_select
;
218 #ifdef CONFIG_MACH_DECSTATION
219 unsigned int real_yrs
;
221 unsigned char century
= 0;
224 mon
= time
->tm_mon
+ 1; /* tm_mon starts at zero */
230 if (yrs
> 255) /* They are unsigned */
233 #ifdef CONFIG_MACH_DECSTATION
238 * We want to keep the year set to 73 until March
239 * for non-leap years, so that Feb, 29th is handled
242 if (!is_leap_year(real_yrs
+ 1900) && mon
< 3) {
249 if (acpi_gbl_FADT
.header
.revision
>= FADT2_REVISION_ID
&&
250 acpi_gbl_FADT
.century
) {
251 century
= (yrs
+ 1900) / 100;
256 /* These limits and adjustments are independent of
257 * whether the chip is in binary mode or not.
265 spin_lock_irqsave(&rtc_lock
, flags
);
266 save_control
= CMOS_READ(RTC_CONTROL
);
267 spin_unlock_irqrestore(&rtc_lock
, flags
);
268 if (!(save_control
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
) {
275 century
= bin2bcd(century
);
278 spin_lock_irqsave(&rtc_lock
, flags
);
279 save_control
= CMOS_READ(RTC_CONTROL
);
280 CMOS_WRITE((save_control
|RTC_SET
), RTC_CONTROL
);
281 save_freq_select
= CMOS_READ(RTC_FREQ_SELECT
);
282 if (apply_amd_register_a_behavior())
283 CMOS_WRITE((save_freq_select
& ~RTC_AMD_BANK_SELECT
), RTC_FREQ_SELECT
);
285 CMOS_WRITE((save_freq_select
|RTC_DIV_RESET2
), RTC_FREQ_SELECT
);
287 #ifdef CONFIG_MACH_DECSTATION
288 CMOS_WRITE(real_yrs
, RTC_DEC_YEAR
);
290 CMOS_WRITE(yrs
, RTC_YEAR
);
291 CMOS_WRITE(mon
, RTC_MONTH
);
292 CMOS_WRITE(day
, RTC_DAY_OF_MONTH
);
293 CMOS_WRITE(hrs
, RTC_HOURS
);
294 CMOS_WRITE(min
, RTC_MINUTES
);
295 CMOS_WRITE(sec
, RTC_SECONDS
);
297 if (acpi_gbl_FADT
.header
.revision
>= FADT2_REVISION_ID
&&
298 acpi_gbl_FADT
.century
)
299 CMOS_WRITE(century
, acpi_gbl_FADT
.century
);
302 CMOS_WRITE(save_control
, RTC_CONTROL
);
303 CMOS_WRITE(save_freq_select
, RTC_FREQ_SELECT
);
305 spin_unlock_irqrestore(&rtc_lock
, flags
);
309 EXPORT_SYMBOL_GPL(mc146818_set_time
);