Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Uwe Zeisberger | f30c226 | 2006-10-03 23:01:26 +0200 | [diff] [blame] | 2 | * include/asm-generic/rtc.h |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Author: Tom Rini <trini@mvista.com> |
| 5 | * |
| 6 | * Based on: |
| 7 | * drivers/char/rtc.c |
| 8 | * |
| 9 | * Please read the COPYING file for all license details. |
| 10 | */ |
| 11 | |
| 12 | #ifndef __ASM_RTC_H__ |
| 13 | #define __ASM_RTC_H__ |
| 14 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/mc146818rtc.h> |
| 16 | #include <linux/rtc.h> |
| 17 | #include <linux/bcd.h> |
Ingo Molnar | 38c052f | 2008-08-23 17:59:07 +0200 | [diff] [blame] | 18 | #include <linux/delay.h> |
Sylvain Chouleur | 3c217e51d | 2015-06-08 11:45:19 +0200 | [diff] [blame] | 19 | #ifdef CONFIG_ACPI |
| 20 | #include <linux/acpi.h> |
| 21 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
| 23 | #define RTC_PIE 0x40 /* periodic interrupt enable */ |
| 24 | #define RTC_AIE 0x20 /* alarm interrupt enable */ |
| 25 | #define RTC_UIE 0x10 /* update-finished interrupt enable */ |
| 26 | |
| 27 | /* some dummy definitions */ |
| 28 | #define RTC_BATT_BAD 0x100 /* battery bad */ |
| 29 | #define RTC_SQWE 0x08 /* enable square-wave output */ |
| 30 | #define RTC_DM_BINARY 0x04 /* all time/date values are BCD if clear */ |
| 31 | #define RTC_24H 0x02 /* 24 hour mode - else hours bit 7 means pm */ |
| 32 | #define RTC_DST_EN 0x01 /* auto switch DST - works f. USA only */ |
| 33 | |
| 34 | /* |
| 35 | * Returns true if a clock update is in progress |
| 36 | */ |
| 37 | static inline unsigned char rtc_is_updating(void) |
| 38 | { |
| 39 | unsigned char uip; |
Andrew Morton | 795d45b | 2008-02-04 16:48:10 +0100 | [diff] [blame] | 40 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
Andrew Morton | 795d45b | 2008-02-04 16:48:10 +0100 | [diff] [blame] | 42 | spin_lock_irqsave(&rtc_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | uip = (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP); |
Andrew Morton | 795d45b | 2008-02-04 16:48:10 +0100 | [diff] [blame] | 44 | spin_unlock_irqrestore(&rtc_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | return uip; |
| 46 | } |
| 47 | |
Ivan Kokshaysky | 5f7dc5d | 2009-01-15 13:51:19 -0800 | [diff] [blame] | 48 | static inline unsigned int __get_rtc_time(struct rtc_time *time) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | unsigned char ctrl; |
Andrew Morton | 795d45b | 2008-02-04 16:48:10 +0100 | [diff] [blame] | 51 | unsigned long flags; |
Sylvain Chouleur | 3c217e51d | 2015-06-08 11:45:19 +0200 | [diff] [blame] | 52 | unsigned char century = 0; |
Andrew Morton | 795d45b | 2008-02-04 16:48:10 +0100 | [diff] [blame] | 53 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | #ifdef CONFIG_MACH_DECSTATION |
| 55 | unsigned int real_year; |
| 56 | #endif |
| 57 | |
| 58 | /* |
| 59 | * read RTC once any update in progress is done. The update |
Ingo Molnar | 38c052f | 2008-08-23 17:59:07 +0200 | [diff] [blame] | 60 | * can take just over 2ms. We wait 20ms. There is no need to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | * to poll-wait (up to 1s - eeccch) for the falling edge of RTC_UIP. |
| 62 | * If you need to know *exactly* when a second has started, enable |
| 63 | * periodic update complete interrupts, (via ioctl) and then |
| 64 | * immediately read /dev/rtc which will block until you get the IRQ. |
| 65 | * Once the read clears, read the RTC time (again via ioctl). Easy. |
| 66 | */ |
Ingo Molnar | 38c052f | 2008-08-23 17:59:07 +0200 | [diff] [blame] | 67 | if (rtc_is_updating()) |
| 68 | mdelay(20); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
| 70 | /* |
| 71 | * Only the values that we read from the RTC are set. We leave |
| 72 | * tm_wday, tm_yday and tm_isdst untouched. Even though the |
| 73 | * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated |
| 74 | * by the RTC when initially set to a non-zero value. |
| 75 | */ |
Andrew Morton | 795d45b | 2008-02-04 16:48:10 +0100 | [diff] [blame] | 76 | spin_lock_irqsave(&rtc_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | time->tm_sec = CMOS_READ(RTC_SECONDS); |
| 78 | time->tm_min = CMOS_READ(RTC_MINUTES); |
| 79 | time->tm_hour = CMOS_READ(RTC_HOURS); |
| 80 | time->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH); |
| 81 | time->tm_mon = CMOS_READ(RTC_MONTH); |
| 82 | time->tm_year = CMOS_READ(RTC_YEAR); |
| 83 | #ifdef CONFIG_MACH_DECSTATION |
| 84 | real_year = CMOS_READ(RTC_DEC_YEAR); |
| 85 | #endif |
Sylvain Chouleur | 3c217e51d | 2015-06-08 11:45:19 +0200 | [diff] [blame] | 86 | #ifdef CONFIG_ACPI |
| 87 | if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID && |
| 88 | acpi_gbl_FADT.century) |
| 89 | century = CMOS_READ(acpi_gbl_FADT.century); |
| 90 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | ctrl = CMOS_READ(RTC_CONTROL); |
Andrew Morton | 795d45b | 2008-02-04 16:48:10 +0100 | [diff] [blame] | 92 | spin_unlock_irqrestore(&rtc_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
| 94 | if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD) |
| 95 | { |
Adrian Bunk | 357c6e6 | 2008-10-18 20:28:42 -0700 | [diff] [blame] | 96 | time->tm_sec = bcd2bin(time->tm_sec); |
| 97 | time->tm_min = bcd2bin(time->tm_min); |
| 98 | time->tm_hour = bcd2bin(time->tm_hour); |
| 99 | time->tm_mday = bcd2bin(time->tm_mday); |
| 100 | time->tm_mon = bcd2bin(time->tm_mon); |
| 101 | time->tm_year = bcd2bin(time->tm_year); |
Sylvain Chouleur | 3c217e51d | 2015-06-08 11:45:19 +0200 | [diff] [blame] | 102 | century = bcd2bin(century); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | #ifdef CONFIG_MACH_DECSTATION |
| 106 | time->tm_year += real_year - 72; |
| 107 | #endif |
| 108 | |
Sylvain Chouleur | 3c217e51d | 2015-06-08 11:45:19 +0200 | [diff] [blame] | 109 | if (century) |
| 110 | time->tm_year += (century - 19) * 100; |
| 111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | /* |
| 113 | * Account for differences between how the RTC uses the values |
| 114 | * and how they are defined in a struct rtc_time; |
| 115 | */ |
| 116 | if (time->tm_year <= 69) |
| 117 | time->tm_year += 100; |
| 118 | |
| 119 | time->tm_mon--; |
| 120 | |
| 121 | return RTC_24H; |
| 122 | } |
| 123 | |
Ivan Kokshaysky | 5f7dc5d | 2009-01-15 13:51:19 -0800 | [diff] [blame] | 124 | #ifndef get_rtc_time |
| 125 | #define get_rtc_time __get_rtc_time |
| 126 | #endif |
| 127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | /* Set the current date and time in the real time clock. */ |
Ivan Kokshaysky | 5f7dc5d | 2009-01-15 13:51:19 -0800 | [diff] [blame] | 129 | static inline int __set_rtc_time(struct rtc_time *time) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | { |
Linus Torvalds | eb71c87 | 2006-06-24 14:27:42 -0700 | [diff] [blame] | 131 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | unsigned char mon, day, hrs, min, sec; |
| 133 | unsigned char save_control, save_freq_select; |
| 134 | unsigned int yrs; |
| 135 | #ifdef CONFIG_MACH_DECSTATION |
| 136 | unsigned int real_yrs, leap_yr; |
| 137 | #endif |
Sylvain Chouleur | 3c217e51d | 2015-06-08 11:45:19 +0200 | [diff] [blame] | 138 | unsigned char century = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
| 140 | yrs = time->tm_year; |
| 141 | mon = time->tm_mon + 1; /* tm_mon starts at zero */ |
| 142 | day = time->tm_mday; |
| 143 | hrs = time->tm_hour; |
| 144 | min = time->tm_min; |
| 145 | sec = time->tm_sec; |
| 146 | |
| 147 | if (yrs > 255) /* They are unsigned */ |
| 148 | return -EINVAL; |
| 149 | |
Linus Torvalds | eb71c87 | 2006-06-24 14:27:42 -0700 | [diff] [blame] | 150 | spin_lock_irqsave(&rtc_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | #ifdef CONFIG_MACH_DECSTATION |
| 152 | real_yrs = yrs; |
| 153 | leap_yr = ((!((yrs + 1900) % 4) && ((yrs + 1900) % 100)) || |
| 154 | !((yrs + 1900) % 400)); |
| 155 | yrs = 72; |
| 156 | |
| 157 | /* |
| 158 | * We want to keep the year set to 73 until March |
| 159 | * for non-leap years, so that Feb, 29th is handled |
| 160 | * correctly. |
| 161 | */ |
| 162 | if (!leap_yr && mon < 3) { |
| 163 | real_yrs--; |
| 164 | yrs = 73; |
| 165 | } |
| 166 | #endif |
Sylvain Chouleur | 3c217e51d | 2015-06-08 11:45:19 +0200 | [diff] [blame] | 167 | |
| 168 | #ifdef CONFIG_ACPI |
| 169 | if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID && |
| 170 | acpi_gbl_FADT.century) { |
| 171 | century = (yrs + 1900) / 100; |
| 172 | yrs %= 100; |
| 173 | } |
| 174 | #endif |
| 175 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | /* These limits and adjustments are independent of |
| 177 | * whether the chip is in binary mode or not. |
| 178 | */ |
| 179 | if (yrs > 169) { |
Linus Torvalds | eb71c87 | 2006-06-24 14:27:42 -0700 | [diff] [blame] | 180 | spin_unlock_irqrestore(&rtc_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | return -EINVAL; |
| 182 | } |
| 183 | |
| 184 | if (yrs >= 100) |
| 185 | yrs -= 100; |
| 186 | |
| 187 | if (!(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY) |
| 188 | || RTC_ALWAYS_BCD) { |
Adrian Bunk | 357c6e6 | 2008-10-18 20:28:42 -0700 | [diff] [blame] | 189 | sec = bin2bcd(sec); |
| 190 | min = bin2bcd(min); |
| 191 | hrs = bin2bcd(hrs); |
| 192 | day = bin2bcd(day); |
| 193 | mon = bin2bcd(mon); |
| 194 | yrs = bin2bcd(yrs); |
Sylvain Chouleur | 3c217e51d | 2015-06-08 11:45:19 +0200 | [diff] [blame] | 195 | century = bin2bcd(century); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | save_control = CMOS_READ(RTC_CONTROL); |
| 199 | CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL); |
| 200 | save_freq_select = CMOS_READ(RTC_FREQ_SELECT); |
| 201 | CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT); |
| 202 | |
| 203 | #ifdef CONFIG_MACH_DECSTATION |
| 204 | CMOS_WRITE(real_yrs, RTC_DEC_YEAR); |
| 205 | #endif |
| 206 | CMOS_WRITE(yrs, RTC_YEAR); |
| 207 | CMOS_WRITE(mon, RTC_MONTH); |
| 208 | CMOS_WRITE(day, RTC_DAY_OF_MONTH); |
| 209 | CMOS_WRITE(hrs, RTC_HOURS); |
| 210 | CMOS_WRITE(min, RTC_MINUTES); |
| 211 | CMOS_WRITE(sec, RTC_SECONDS); |
Sylvain Chouleur | 3c217e51d | 2015-06-08 11:45:19 +0200 | [diff] [blame] | 212 | #ifdef CONFIG_ACPI |
| 213 | if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID && |
| 214 | acpi_gbl_FADT.century) |
| 215 | CMOS_WRITE(century, acpi_gbl_FADT.century); |
| 216 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | |
| 218 | CMOS_WRITE(save_control, RTC_CONTROL); |
| 219 | CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT); |
| 220 | |
Linus Torvalds | eb71c87 | 2006-06-24 14:27:42 -0700 | [diff] [blame] | 221 | spin_unlock_irqrestore(&rtc_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
Ivan Kokshaysky | 5f7dc5d | 2009-01-15 13:51:19 -0800 | [diff] [blame] | 226 | #ifndef set_rtc_time |
| 227 | #define set_rtc_time __set_rtc_time |
| 228 | #endif |
| 229 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | static inline unsigned int get_rtc_ss(void) |
| 231 | { |
| 232 | struct rtc_time h; |
| 233 | |
Arnd Bergmann | 3aef392 | 2009-05-17 21:00:05 +0000 | [diff] [blame] | 234 | get_rtc_time(&h); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | return h.tm_sec; |
| 236 | } |
| 237 | |
| 238 | static inline int get_rtc_pll(struct rtc_pll_info *pll) |
| 239 | { |
| 240 | return -EINVAL; |
| 241 | } |
| 242 | static inline int set_rtc_pll(struct rtc_pll_info *pll) |
| 243 | { |
| 244 | return -EINVAL; |
| 245 | } |
| 246 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | #endif /* __ASM_RTC_H__ */ |