Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Arnd Bergmann | d6faca4 | 2016-06-01 16:46:23 +0200 | [diff] [blame] | 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 | |
Mateusz Jończyk | ea6fa49 | 2021-12-10 21:01:26 +0100 | [diff] [blame^] | 11 | /* |
| 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. |
| 14 | */ |
| 15 | bool mc146818_does_rtc_work(void) |
| 16 | { |
| 17 | int i; |
| 18 | unsigned char val; |
| 19 | unsigned long flags; |
| 20 | |
| 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); |
| 25 | |
| 26 | if ((val & RTC_UIP) == 0) |
| 27 | return true; |
| 28 | |
| 29 | mdelay(1); |
| 30 | } |
| 31 | |
| 32 | return false; |
| 33 | } |
| 34 | EXPORT_SYMBOL_GPL(mc146818_does_rtc_work); |
| 35 | |
Arnd Bergmann | d6faca4 | 2016-06-01 16:46:23 +0200 | [diff] [blame] | 36 | unsigned int mc146818_get_time(struct rtc_time *time) |
| 37 | { |
| 38 | unsigned char ctrl; |
| 39 | unsigned long flags; |
Mateusz Jończyk | ea6fa49 | 2021-12-10 21:01:26 +0100 | [diff] [blame^] | 40 | unsigned int iter_count = 0; |
Arnd Bergmann | d6faca4 | 2016-06-01 16:46:23 +0200 | [diff] [blame] | 41 | unsigned char century = 0; |
Thomas Gleixner | 05a0302 | 2020-12-06 22:46:14 +0100 | [diff] [blame] | 42 | bool retry; |
Arnd Bergmann | d6faca4 | 2016-06-01 16:46:23 +0200 | [diff] [blame] | 43 | |
| 44 | #ifdef CONFIG_MACH_DECSTATION |
| 45 | unsigned int real_year; |
| 46 | #endif |
| 47 | |
Thomas Gleixner | 05a0302 | 2020-12-06 22:46:14 +0100 | [diff] [blame] | 48 | again: |
Mateusz Jończyk | ea6fa49 | 2021-12-10 21:01:26 +0100 | [diff] [blame^] | 49 | if (iter_count > 10) { |
Mateusz Jończyk | 0dd8d6c | 2021-12-10 21:01:25 +0100 | [diff] [blame] | 50 | memset(time, 0, sizeof(*time)); |
Mateusz Jończyk | d35786b | 2021-12-10 21:01:24 +0100 | [diff] [blame] | 51 | return -EIO; |
Thomas Gleixner | 211e5db | 2021-01-26 18:02:11 +0100 | [diff] [blame] | 52 | } |
Mateusz Jończyk | ea6fa49 | 2021-12-10 21:01:26 +0100 | [diff] [blame^] | 53 | iter_count++; |
| 54 | |
| 55 | spin_lock_irqsave(&rtc_lock, flags); |
Thomas Gleixner | 211e5db | 2021-01-26 18:02:11 +0100 | [diff] [blame] | 56 | |
Arnd Bergmann | d6faca4 | 2016-06-01 16:46:23 +0200 | [diff] [blame] | 57 | /* |
Thomas Gleixner | 05a0302 | 2020-12-06 22:46:14 +0100 | [diff] [blame] | 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. |
| 61 | * |
| 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 |
| 64 | * cycle invisible. |
Arnd Bergmann | d6faca4 | 2016-06-01 16:46:23 +0200 | [diff] [blame] | 65 | */ |
Thomas Gleixner | 05a0302 | 2020-12-06 22:46:14 +0100 | [diff] [blame] | 66 | time->tm_sec = CMOS_READ(RTC_SECONDS); |
| 67 | |
| 68 | if (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP) { |
| 69 | spin_unlock_irqrestore(&rtc_lock, flags); |
| 70 | mdelay(1); |
| 71 | goto again; |
| 72 | } |
| 73 | |
| 74 | /* Revalidate the above readout */ |
| 75 | if (time->tm_sec != CMOS_READ(RTC_SECONDS)) { |
| 76 | spin_unlock_irqrestore(&rtc_lock, flags); |
| 77 | goto again; |
| 78 | } |
Arnd Bergmann | d6faca4 | 2016-06-01 16:46:23 +0200 | [diff] [blame] | 79 | |
| 80 | /* |
| 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. |
| 85 | */ |
Arnd Bergmann | d6faca4 | 2016-06-01 16:46:23 +0200 | [diff] [blame] | 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); |
| 93 | #endif |
| 94 | #ifdef CONFIG_ACPI |
| 95 | if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID && |
| 96 | acpi_gbl_FADT.century) |
| 97 | century = CMOS_READ(acpi_gbl_FADT.century); |
| 98 | #endif |
| 99 | ctrl = CMOS_READ(RTC_CONTROL); |
Thomas Gleixner | 05a0302 | 2020-12-06 22:46:14 +0100 | [diff] [blame] | 100 | /* |
| 101 | * Check for the UIP bit again. If it is set now then |
| 102 | * the above values may contain garbage. |
| 103 | */ |
| 104 | retry = CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP; |
| 105 | /* |
| 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... |
| 110 | */ |
| 111 | retry |= time->tm_sec != CMOS_READ(RTC_SECONDS); |
| 112 | |
Arnd Bergmann | d6faca4 | 2016-06-01 16:46:23 +0200 | [diff] [blame] | 113 | spin_unlock_irqrestore(&rtc_lock, flags); |
| 114 | |
Thomas Gleixner | 05a0302 | 2020-12-06 22:46:14 +0100 | [diff] [blame] | 115 | if (retry) |
| 116 | goto again; |
| 117 | |
Arnd Bergmann | d6faca4 | 2016-06-01 16:46:23 +0200 | [diff] [blame] | 118 | if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD) |
| 119 | { |
| 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); |
| 127 | } |
| 128 | |
| 129 | #ifdef CONFIG_MACH_DECSTATION |
| 130 | time->tm_year += real_year - 72; |
| 131 | #endif |
| 132 | |
Eric Wong | 2a4daad | 2019-01-06 08:21:03 +0000 | [diff] [blame] | 133 | if (century > 20) |
Arnd Bergmann | d6faca4 | 2016-06-01 16:46:23 +0200 | [diff] [blame] | 134 | time->tm_year += (century - 19) * 100; |
| 135 | |
| 136 | /* |
| 137 | * Account for differences between how the RTC uses the values |
| 138 | * and how they are defined in a struct rtc_time; |
| 139 | */ |
| 140 | if (time->tm_year <= 69) |
| 141 | time->tm_year += 100; |
| 142 | |
| 143 | time->tm_mon--; |
| 144 | |
Mateusz Jończyk | d35786b | 2021-12-10 21:01:24 +0100 | [diff] [blame] | 145 | return 0; |
Arnd Bergmann | d6faca4 | 2016-06-01 16:46:23 +0200 | [diff] [blame] | 146 | } |
| 147 | EXPORT_SYMBOL_GPL(mc146818_get_time); |
| 148 | |
| 149 | /* Set the current date and time in the real time clock. */ |
| 150 | int mc146818_set_time(struct rtc_time *time) |
| 151 | { |
| 152 | unsigned long flags; |
| 153 | unsigned char mon, day, hrs, min, sec; |
| 154 | unsigned char save_control, save_freq_select; |
| 155 | unsigned int yrs; |
| 156 | #ifdef CONFIG_MACH_DECSTATION |
| 157 | unsigned int real_yrs, leap_yr; |
| 158 | #endif |
| 159 | unsigned char century = 0; |
| 160 | |
| 161 | yrs = time->tm_year; |
| 162 | mon = time->tm_mon + 1; /* tm_mon starts at zero */ |
| 163 | day = time->tm_mday; |
| 164 | hrs = time->tm_hour; |
| 165 | min = time->tm_min; |
| 166 | sec = time->tm_sec; |
| 167 | |
| 168 | if (yrs > 255) /* They are unsigned */ |
| 169 | return -EINVAL; |
| 170 | |
Arnd Bergmann | d6faca4 | 2016-06-01 16:46:23 +0200 | [diff] [blame] | 171 | #ifdef CONFIG_MACH_DECSTATION |
| 172 | real_yrs = yrs; |
| 173 | leap_yr = ((!((yrs + 1900) % 4) && ((yrs + 1900) % 100)) || |
| 174 | !((yrs + 1900) % 400)); |
| 175 | yrs = 72; |
| 176 | |
| 177 | /* |
| 178 | * We want to keep the year set to 73 until March |
| 179 | * for non-leap years, so that Feb, 29th is handled |
| 180 | * correctly. |
| 181 | */ |
| 182 | if (!leap_yr && mon < 3) { |
| 183 | real_yrs--; |
| 184 | yrs = 73; |
| 185 | } |
| 186 | #endif |
| 187 | |
| 188 | #ifdef CONFIG_ACPI |
| 189 | if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID && |
| 190 | acpi_gbl_FADT.century) { |
| 191 | century = (yrs + 1900) / 100; |
| 192 | yrs %= 100; |
| 193 | } |
| 194 | #endif |
| 195 | |
| 196 | /* These limits and adjustments are independent of |
| 197 | * whether the chip is in binary mode or not. |
| 198 | */ |
Thomas Gleixner | dcf257e | 2020-12-06 22:46:15 +0100 | [diff] [blame] | 199 | if (yrs > 169) |
Arnd Bergmann | d6faca4 | 2016-06-01 16:46:23 +0200 | [diff] [blame] | 200 | return -EINVAL; |
Arnd Bergmann | d6faca4 | 2016-06-01 16:46:23 +0200 | [diff] [blame] | 201 | |
| 202 | if (yrs >= 100) |
| 203 | yrs -= 100; |
| 204 | |
| 205 | if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) |
| 206 | || RTC_ALWAYS_BCD) { |
| 207 | sec = bin2bcd(sec); |
| 208 | min = bin2bcd(min); |
| 209 | hrs = bin2bcd(hrs); |
| 210 | day = bin2bcd(day); |
| 211 | mon = bin2bcd(mon); |
| 212 | yrs = bin2bcd(yrs); |
| 213 | century = bin2bcd(century); |
| 214 | } |
| 215 | |
Thomas Gleixner | dcf257e | 2020-12-06 22:46:15 +0100 | [diff] [blame] | 216 | spin_lock_irqsave(&rtc_lock, flags); |
Arnd Bergmann | d6faca4 | 2016-06-01 16:46:23 +0200 | [diff] [blame] | 217 | save_control = CMOS_READ(RTC_CONTROL); |
| 218 | CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL); |
| 219 | save_freq_select = CMOS_READ(RTC_FREQ_SELECT); |
Alexandre Belloni | f01f4ff | 2020-01-04 05:31:10 +0100 | [diff] [blame] | 220 | CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT); |
Arnd Bergmann | d6faca4 | 2016-06-01 16:46:23 +0200 | [diff] [blame] | 221 | |
| 222 | #ifdef CONFIG_MACH_DECSTATION |
| 223 | CMOS_WRITE(real_yrs, RTC_DEC_YEAR); |
| 224 | #endif |
| 225 | CMOS_WRITE(yrs, RTC_YEAR); |
| 226 | CMOS_WRITE(mon, RTC_MONTH); |
| 227 | CMOS_WRITE(day, RTC_DAY_OF_MONTH); |
| 228 | CMOS_WRITE(hrs, RTC_HOURS); |
| 229 | CMOS_WRITE(min, RTC_MINUTES); |
| 230 | CMOS_WRITE(sec, RTC_SECONDS); |
| 231 | #ifdef CONFIG_ACPI |
| 232 | if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID && |
| 233 | acpi_gbl_FADT.century) |
| 234 | CMOS_WRITE(century, acpi_gbl_FADT.century); |
| 235 | #endif |
| 236 | |
| 237 | CMOS_WRITE(save_control, RTC_CONTROL); |
| 238 | CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT); |
| 239 | |
| 240 | spin_unlock_irqrestore(&rtc_lock, flags); |
| 241 | |
| 242 | return 0; |
| 243 | } |
| 244 | EXPORT_SYMBOL_GPL(mc146818_set_time); |