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>
6 
7 #ifdef CONFIG_ACPI
8 #include <linux/acpi.h>
9 #endif
10 
11 unsigned int mc146818_get_time(struct rtc_time *time)
12 {
13 	unsigned char ctrl;
14 	unsigned long flags;
15 	unsigned char century = 0;
16 	bool retry;
17 
18 #ifdef CONFIG_MACH_DECSTATION
19 	unsigned int real_year;
20 #endif
21 
22 again:
23 	spin_lock_irqsave(&rtc_lock, flags);
24 	/*
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.
28 	 *
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.
32 	 */
33 	time->tm_sec = CMOS_READ(RTC_SECONDS);
34 
35 	if (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP) {
36 		spin_unlock_irqrestore(&rtc_lock, flags);
37 		mdelay(1);
38 		goto again;
39 	}
40 
41 	/* Revalidate the above readout */
42 	if (time->tm_sec != CMOS_READ(RTC_SECONDS)) {
43 		spin_unlock_irqrestore(&rtc_lock, flags);
44 		goto again;
45 	}
46 
47 	/*
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.
52 	 */
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);
67 	/*
68 	 * Check for the UIP bit again. If it is set now then
69 	 * the above values may contain garbage.
70 	 */
71 	retry = CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP;
72 	/*
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...
77 	 */
78 	retry |= time->tm_sec != CMOS_READ(RTC_SECONDS);
79 
80 	spin_unlock_irqrestore(&rtc_lock, flags);
81 
82 	if (retry)
83 		goto again;
84 
85 	if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
86 	{
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);
94 	}
95 
96 #ifdef CONFIG_MACH_DECSTATION
97 	time->tm_year += real_year - 72;
98 #endif
99 
100 	if (century > 20)
101 		time->tm_year += (century - 19) * 100;
102 
103 	/*
104 	 * Account for differences between how the RTC uses the values
105 	 * and how they are defined in a struct rtc_time;
106 	 */
107 	if (time->tm_year <= 69)
108 		time->tm_year += 100;
109 
110 	time->tm_mon--;
111 
112 	return RTC_24H;
113 }
114 EXPORT_SYMBOL_GPL(mc146818_get_time);
115 
116 /* Set the current date and time in the real time clock. */
117 int mc146818_set_time(struct rtc_time *time)
118 {
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;
127 
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;
134 
135 	if (yrs > 255)	/* They are unsigned */
136 		return -EINVAL;
137 
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;
143 
144 	/*
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.
148 	 */
149 	if (!leap_yr && mon < 3) {
150 		real_yrs--;
151 		yrs = 73;
152 	}
153 #endif
154 
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;
160 	}
161 #endif
162 
163 	/* These limits and adjustments are independent of
164 	 * whether the chip is in binary mode or not.
165 	 */
166 	if (yrs > 169)
167 		return -EINVAL;
168 
169 	if (yrs >= 100)
170 		yrs -= 100;
171 
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);
181 	}
182 
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);
188 
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
203 
204 	CMOS_WRITE(save_control, RTC_CONTROL);
205 	CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
206 
207 	spin_unlock_irqrestore(&rtc_lock, flags);
208 
209 	return 0;
210 }
211 EXPORT_SYMBOL_GPL(mc146818_set_time);
212