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 unsigned int mc146818_get_time(struct rtc_time
*time
)
15 unsigned char century
= 0;
18 #ifdef CONFIG_MACH_DECSTATION
19 unsigned int real_year
;
23 spin_lock_irqsave(&rtc_lock
, flags
);
25 * Check whether there is an update in progress during which the
26 * readout is unspecified. The maximum update time is ~2ms. Poll
27 * every msec for completion.
29 * Store the second value before checking UIP so a long lasting NMI
30 * which happens to hit after the UIP check cannot make an update
33 time
->tm_sec
= CMOS_READ(RTC_SECONDS
);
35 if (CMOS_READ(RTC_FREQ_SELECT
) & RTC_UIP
) {
36 spin_unlock_irqrestore(&rtc_lock
, flags
);
41 /* Revalidate the above readout */
42 if (time
->tm_sec
!= CMOS_READ(RTC_SECONDS
)) {
43 spin_unlock_irqrestore(&rtc_lock
, flags
);
48 * Only the values that we read from the RTC are set. We leave
49 * tm_wday, tm_yday and tm_isdst untouched. Even though the
50 * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated
51 * by the RTC when initially set to a non-zero value.
53 time
->tm_min
= CMOS_READ(RTC_MINUTES
);
54 time
->tm_hour
= CMOS_READ(RTC_HOURS
);
55 time
->tm_mday
= CMOS_READ(RTC_DAY_OF_MONTH
);
56 time
->tm_mon
= CMOS_READ(RTC_MONTH
);
57 time
->tm_year
= CMOS_READ(RTC_YEAR
);
58 #ifdef CONFIG_MACH_DECSTATION
59 real_year
= CMOS_READ(RTC_DEC_YEAR
);
62 if (acpi_gbl_FADT
.header
.revision
>= FADT2_REVISION_ID
&&
63 acpi_gbl_FADT
.century
)
64 century
= CMOS_READ(acpi_gbl_FADT
.century
);
66 ctrl
= CMOS_READ(RTC_CONTROL
);
68 * Check for the UIP bit again. If it is set now then
69 * the above values may contain garbage.
71 retry
= CMOS_READ(RTC_FREQ_SELECT
) & RTC_UIP
;
73 * A NMI might have interrupted the above sequence so check whether
74 * the seconds value has changed which indicates that the NMI took
75 * longer than the UIP bit was set. Unlikely, but possible and
76 * there is also virt...
78 retry
|= time
->tm_sec
!= CMOS_READ(RTC_SECONDS
);
80 spin_unlock_irqrestore(&rtc_lock
, flags
);
85 if (!(ctrl
& RTC_DM_BINARY
) || RTC_ALWAYS_BCD
)
87 time
->tm_sec
= bcd2bin(time
->tm_sec
);
88 time
->tm_min
= bcd2bin(time
->tm_min
);
89 time
->tm_hour
= bcd2bin(time
->tm_hour
);
90 time
->tm_mday
= bcd2bin(time
->tm_mday
);
91 time
->tm_mon
= bcd2bin(time
->tm_mon
);
92 time
->tm_year
= bcd2bin(time
->tm_year
);
93 century
= bcd2bin(century
);
96 #ifdef CONFIG_MACH_DECSTATION
97 time
->tm_year
+= real_year
- 72;
101 time
->tm_year
+= (century
- 19) * 100;
104 * Account for differences between how the RTC uses the values
105 * and how they are defined in a struct rtc_time;
107 if (time
->tm_year
<= 69)
108 time
->tm_year
+= 100;
114 EXPORT_SYMBOL_GPL(mc146818_get_time
);
116 /* Set the current date and time in the real time clock. */
117 int mc146818_set_time(struct rtc_time
*time
)
120 unsigned char mon
, day
, hrs
, min
, sec
;
121 unsigned char save_control
, save_freq_select
;
123 #ifdef CONFIG_MACH_DECSTATION
124 unsigned int real_yrs
, leap_yr
;
126 unsigned char century
= 0;
129 mon
= time
->tm_mon
+ 1; /* tm_mon starts at zero */
135 if (yrs
> 255) /* They are unsigned */
138 #ifdef CONFIG_MACH_DECSTATION
140 leap_yr
= ((!((yrs
+ 1900) % 4) && ((yrs
+ 1900) % 100)) ||
141 !((yrs
+ 1900) % 400));
145 * We want to keep the year set to 73 until March
146 * for non-leap years, so that Feb, 29th is handled
149 if (!leap_yr
&& mon
< 3) {
156 if (acpi_gbl_FADT
.header
.revision
>= FADT2_REVISION_ID
&&
157 acpi_gbl_FADT
.century
) {
158 century
= (yrs
+ 1900) / 100;
163 /* These limits and adjustments are independent of
164 * whether the chip is in binary mode or not.
172 if (!(CMOS_READ(RTC_CONTROL
) & RTC_DM_BINARY
)
180 century
= bin2bcd(century
);
183 spin_lock_irqsave(&rtc_lock
, flags
);
184 save_control
= CMOS_READ(RTC_CONTROL
);
185 CMOS_WRITE((save_control
|RTC_SET
), RTC_CONTROL
);
186 save_freq_select
= CMOS_READ(RTC_FREQ_SELECT
);
187 CMOS_WRITE((save_freq_select
|RTC_DIV_RESET2
), RTC_FREQ_SELECT
);
189 #ifdef CONFIG_MACH_DECSTATION
190 CMOS_WRITE(real_yrs
, RTC_DEC_YEAR
);
192 CMOS_WRITE(yrs
, RTC_YEAR
);
193 CMOS_WRITE(mon
, RTC_MONTH
);
194 CMOS_WRITE(day
, RTC_DAY_OF_MONTH
);
195 CMOS_WRITE(hrs
, RTC_HOURS
);
196 CMOS_WRITE(min
, RTC_MINUTES
);
197 CMOS_WRITE(sec
, RTC_SECONDS
);
199 if (acpi_gbl_FADT
.header
.revision
>= FADT2_REVISION_ID
&&
200 acpi_gbl_FADT
.century
)
201 CMOS_WRITE(century
, acpi_gbl_FADT
.century
);
204 CMOS_WRITE(save_control
, RTC_CONTROL
);
205 CMOS_WRITE(save_freq_select
, RTC_FREQ_SELECT
);
207 spin_unlock_irqrestore(&rtc_lock
, flags
);
211 EXPORT_SYMBOL_GPL(mc146818_set_time
);