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>
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.
15 bool mc146818_does_rtc_work(void)
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);
26 if ((val & RTC_UIP) == 0)
34 EXPORT_SYMBOL_GPL(mc146818_does_rtc_work);
36 int mc146818_get_time(struct rtc_time *time)
40 unsigned int iter_count = 0;
41 unsigned char century = 0;
44 #ifdef CONFIG_MACH_DECSTATION
45 unsigned int real_year;
49 if (iter_count > 10) {
50 memset(time, 0, sizeof(*time));
55 spin_lock_irqsave(&rtc_lock, flags);
58 * 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.
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
66 time->tm_sec = CMOS_READ(RTC_SECONDS);
68 if (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP) {
69 spin_unlock_irqrestore(&rtc_lock, flags);
74 /* Revalidate the above readout */
75 if (time->tm_sec != CMOS_READ(RTC_SECONDS)) {
76 spin_unlock_irqrestore(&rtc_lock, flags);
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.
86 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);
95 if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
96 acpi_gbl_FADT.century)
97 century = CMOS_READ(acpi_gbl_FADT.century);
99 ctrl = CMOS_READ(RTC_CONTROL);
101 * Check for the UIP bit again. If it is set now then
102 * the above values may contain garbage.
104 retry = CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP;
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...
111 retry |= time->tm_sec != CMOS_READ(RTC_SECONDS);
113 spin_unlock_irqrestore(&rtc_lock, flags);
118 if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
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);
129 #ifdef CONFIG_MACH_DECSTATION
130 time->tm_year += real_year - 72;
134 time->tm_year += (century - 19) * 100;
137 * Account for differences between how the RTC uses the values
138 * and how they are defined in a struct rtc_time;
140 if (time->tm_year <= 69)
141 time->tm_year += 100;
147 EXPORT_SYMBOL_GPL(mc146818_get_time);
149 /* AMD systems don't allow access to AltCentury with DV1 */
150 static bool apply_amd_register_a_behavior(void)
153 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD ||
154 boot_cpu_data.x86_vendor == X86_VENDOR_HYGON)
160 /* Set the current date and time in the real time clock. */
161 int mc146818_set_time(struct rtc_time *time)
164 unsigned char mon, day, hrs, min, sec;
165 unsigned char save_control, save_freq_select;
167 #ifdef CONFIG_MACH_DECSTATION
168 unsigned int real_yrs, leap_yr;
170 unsigned char century = 0;
173 mon = time->tm_mon + 1; /* tm_mon starts at zero */
179 if (yrs > 255) /* They are unsigned */
182 #ifdef CONFIG_MACH_DECSTATION
184 leap_yr = ((!((yrs + 1900) % 4) && ((yrs + 1900) % 100)) ||
185 !((yrs + 1900) % 400));
189 * We want to keep the year set to 73 until March
190 * for non-leap years, so that Feb, 29th is handled
193 if (!leap_yr && mon < 3) {
200 if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
201 acpi_gbl_FADT.century) {
202 century = (yrs + 1900) / 100;
207 /* These limits and adjustments are independent of
208 * whether the chip is in binary mode or not.
216 spin_lock_irqsave(&rtc_lock, flags);
217 save_control = CMOS_READ(RTC_CONTROL);
218 spin_unlock_irqrestore(&rtc_lock, flags);
219 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
226 century = bin2bcd(century);
229 spin_lock_irqsave(&rtc_lock, flags);
230 save_control = CMOS_READ(RTC_CONTROL);
231 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
232 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
233 if (apply_amd_register_a_behavior())
234 CMOS_WRITE((save_freq_select & ~RTC_AMD_BANK_SELECT), RTC_FREQ_SELECT);
236 CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
238 #ifdef CONFIG_MACH_DECSTATION
239 CMOS_WRITE(real_yrs, RTC_DEC_YEAR);
241 CMOS_WRITE(yrs, RTC_YEAR);
242 CMOS_WRITE(mon, RTC_MONTH);
243 CMOS_WRITE(day, RTC_DAY_OF_MONTH);
244 CMOS_WRITE(hrs, RTC_HOURS);
245 CMOS_WRITE(min, RTC_MINUTES);
246 CMOS_WRITE(sec, RTC_SECONDS);
248 if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
249 acpi_gbl_FADT.century)
250 CMOS_WRITE(century, acpi_gbl_FADT.century);
253 CMOS_WRITE(save_control, RTC_CONTROL);
254 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
256 spin_unlock_irqrestore(&rtc_lock, flags);
260 EXPORT_SYMBOL_GPL(mc146818_set_time);