Merge tag 'regmap-fix-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / rtc / rtc-mc146818-lib.c
blob972a5b9a629d3bfd992bac180e623927b4144adb
1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/bcd.h>
3 #include <linux/delay.h>
4 #include <linux/export.h>
5 #include <linux/mc146818rtc.h>
7 #ifdef CONFIG_ACPI
8 #include <linux/acpi.h>
9 #endif
11 unsigned int mc146818_get_time(struct rtc_time *time)
13 unsigned char ctrl;
14 unsigned long flags;
15 unsigned char century = 0;
16 bool retry;
18 #ifdef CONFIG_MACH_DECSTATION
19 unsigned int real_year;
20 #endif
22 again:
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
31 * cycle invisible.
33 time->tm_sec = CMOS_READ(RTC_SECONDS);
35 if (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP) {
36 spin_unlock_irqrestore(&rtc_lock, flags);
37 mdelay(1);
38 goto again;
41 /* Revalidate the above readout */
42 if (time->tm_sec != CMOS_READ(RTC_SECONDS)) {
43 spin_unlock_irqrestore(&rtc_lock, flags);
44 goto again;
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);
60 #endif
61 #ifdef CONFIG_ACPI
62 if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
63 acpi_gbl_FADT.century)
64 century = CMOS_READ(acpi_gbl_FADT.century);
65 #endif
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);
82 if (retry)
83 goto again;
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;
98 #endif
100 if (century > 20)
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;
110 time->tm_mon--;
112 return RTC_24H;
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)
119 unsigned long flags;
120 unsigned char mon, day, hrs, min, sec;
121 unsigned char save_control, save_freq_select;
122 unsigned int yrs;
123 #ifdef CONFIG_MACH_DECSTATION
124 unsigned int real_yrs, leap_yr;
125 #endif
126 unsigned char century = 0;
128 yrs = time->tm_year;
129 mon = time->tm_mon + 1; /* tm_mon starts at zero */
130 day = time->tm_mday;
131 hrs = time->tm_hour;
132 min = time->tm_min;
133 sec = time->tm_sec;
135 if (yrs > 255) /* They are unsigned */
136 return -EINVAL;
138 #ifdef CONFIG_MACH_DECSTATION
139 real_yrs = yrs;
140 leap_yr = ((!((yrs + 1900) % 4) && ((yrs + 1900) % 100)) ||
141 !((yrs + 1900) % 400));
142 yrs = 72;
145 * We want to keep the year set to 73 until March
146 * for non-leap years, so that Feb, 29th is handled
147 * correctly.
149 if (!leap_yr && mon < 3) {
150 real_yrs--;
151 yrs = 73;
153 #endif
155 #ifdef CONFIG_ACPI
156 if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
157 acpi_gbl_FADT.century) {
158 century = (yrs + 1900) / 100;
159 yrs %= 100;
161 #endif
163 /* These limits and adjustments are independent of
164 * whether the chip is in binary mode or not.
166 if (yrs > 169)
167 return -EINVAL;
169 if (yrs >= 100)
170 yrs -= 100;
172 if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY)
173 || RTC_ALWAYS_BCD) {
174 sec = bin2bcd(sec);
175 min = bin2bcd(min);
176 hrs = bin2bcd(hrs);
177 day = bin2bcd(day);
178 mon = bin2bcd(mon);
179 yrs = bin2bcd(yrs);
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);
191 #endif
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);
198 #ifdef CONFIG_ACPI
199 if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
200 acpi_gbl_FADT.century)
201 CMOS_WRITE(century, acpi_gbl_FADT.century);
202 #endif
204 CMOS_WRITE(save_control, RTC_CONTROL);
205 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
207 spin_unlock_irqrestore(&rtc_lock, flags);
209 return 0;
211 EXPORT_SYMBOL_GPL(mc146818_set_time);