Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Thomas Gleixner | fe599f9 | 2008-01-30 13:30:26 +0100 | [diff] [blame] | 2 | * RTC related functions |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | */ |
Thomas Gleixner | 1122b13 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 4 | #include <linux/acpi.h> |
Thomas Gleixner | fe599f9 | 2008-01-30 13:30:26 +0100 | [diff] [blame] | 5 | #include <linux/bcd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include <linux/mc146818rtc.h> |
| 7 | |
Thomas Gleixner | fe599f9 | 2008-01-30 13:30:26 +0100 | [diff] [blame] | 8 | #include <asm/time.h> |
Ingo Molnar | cdc7957 | 2008-01-30 13:32:39 +0100 | [diff] [blame] | 9 | #include <asm/vsyscall.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | |
Thomas Gleixner | 1122b13 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 11 | #ifdef CONFIG_X86_32 |
Thomas Gleixner | 1122b13 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 12 | /* |
| 13 | * This is a special lock that is owned by the CPU and holds the index |
| 14 | * register we are working with. It is required for NMI access to the |
| 15 | * CMOS/RTC registers. See include/asm-i386/mc146818rtc.h for details. |
| 16 | */ |
| 17 | volatile unsigned long cmos_lock = 0; |
| 18 | EXPORT_SYMBOL(cmos_lock); |
Thomas Gleixner | 1122b13 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 19 | #endif |
| 20 | |
Andi Kleen | b62576a | 2008-02-09 16:16:58 +0100 | [diff] [blame] | 21 | /* For two digit years assume time is always after that */ |
| 22 | #define CMOS_YEARS_OFFS 2000 |
| 23 | |
Thomas Gleixner | 1122b13 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 24 | DEFINE_SPINLOCK(rtc_lock); |
| 25 | EXPORT_SYMBOL(rtc_lock); |
| 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | /* |
| 28 | * In order to set the CMOS clock precisely, set_rtc_mmss has to be |
| 29 | * called 500 ms after the second nowtime has started, because when |
| 30 | * nowtime is written into the registers of the CMOS clock, it will |
| 31 | * jump to the next second precisely 500 ms later. Check the Motorola |
| 32 | * MC146818A or Dallas DS12887 data sheet for details. |
| 33 | * |
| 34 | * BUG: This routine does not handle hour overflow properly; it just |
| 35 | * sets the minutes. Usually you'll only notice that after reboot! |
| 36 | */ |
Thomas Gleixner | fe599f9 | 2008-01-30 13:30:26 +0100 | [diff] [blame] | 37 | int mach_set_rtc_mmss(unsigned long nowtime) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | { |
| 39 | int retval = 0; |
| 40 | int real_seconds, real_minutes, cmos_minutes; |
| 41 | unsigned char save_control, save_freq_select; |
| 42 | |
Thomas Gleixner | 1122b13 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 43 | /* tell the clock it's being set */ |
| 44 | save_control = CMOS_READ(RTC_CONTROL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL); |
| 46 | |
Thomas Gleixner | 1122b13 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 47 | /* stop and reset prescaler */ |
| 48 | save_freq_select = CMOS_READ(RTC_FREQ_SELECT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT); |
| 50 | |
| 51 | cmos_minutes = CMOS_READ(RTC_MINUTES); |
| 52 | if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) |
| 53 | BCD_TO_BIN(cmos_minutes); |
| 54 | |
| 55 | /* |
| 56 | * since we're only adjusting minutes and seconds, |
| 57 | * don't interfere with hour overflow. This avoids |
| 58 | * messing with unknown time zones but requires your |
| 59 | * RTC not to be off by more than 15 minutes |
| 60 | */ |
| 61 | real_seconds = nowtime % 60; |
| 62 | real_minutes = nowtime / 60; |
Thomas Gleixner | 1122b13 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 63 | /* correct for half hour time zone */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1) |
Thomas Gleixner | 1122b13 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 65 | real_minutes += 30; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | real_minutes %= 60; |
| 67 | |
| 68 | if (abs(real_minutes - cmos_minutes) < 30) { |
| 69 | if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) { |
| 70 | BIN_TO_BCD(real_seconds); |
| 71 | BIN_TO_BCD(real_minutes); |
| 72 | } |
| 73 | CMOS_WRITE(real_seconds,RTC_SECONDS); |
| 74 | CMOS_WRITE(real_minutes,RTC_MINUTES); |
| 75 | } else { |
| 76 | printk(KERN_WARNING |
| 77 | "set_rtc_mmss: can't update from %d to %d\n", |
| 78 | cmos_minutes, real_minutes); |
| 79 | retval = -1; |
| 80 | } |
| 81 | |
| 82 | /* The following flags have to be released exactly in this order, |
| 83 | * otherwise the DS12887 (popular MC146818A clone with integrated |
| 84 | * battery and quartz) will not reset the oscillator and will not |
| 85 | * update precisely 500 ms later. You won't find this mentioned in |
| 86 | * the Dallas Semiconductor data sheets, but who believes data |
| 87 | * sheets anyway ... -- Markus Kuhn |
| 88 | */ |
| 89 | CMOS_WRITE(save_control, RTC_CONTROL); |
| 90 | CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT); |
| 91 | |
| 92 | return retval; |
| 93 | } |
| 94 | |
Thomas Gleixner | fe599f9 | 2008-01-30 13:30:26 +0100 | [diff] [blame] | 95 | unsigned long mach_get_cmos_time(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | { |
Andi Kleen | 068c922 | 2008-02-09 16:16:59 +0100 | [diff] [blame] | 97 | unsigned int status, year, mon, day, hour, min, sec, century = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
Thomas Gleixner | 1122b13 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 99 | /* |
| 100 | * If UIP is clear, then we have >= 244 microseconds before |
| 101 | * RTC registers will be updated. Spec sheet says that this |
| 102 | * is the reliable way to read RTC - registers. If UIP is set |
| 103 | * then the register access might be invalid. |
| 104 | */ |
| 105 | while ((CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP)) |
| 106 | cpu_relax(); |
Matt Mackall | 63732c2 | 2006-03-28 01:55:58 -0800 | [diff] [blame] | 107 | |
Thomas Gleixner | 1122b13 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 108 | sec = CMOS_READ(RTC_SECONDS); |
| 109 | min = CMOS_READ(RTC_MINUTES); |
| 110 | hour = CMOS_READ(RTC_HOURS); |
| 111 | day = CMOS_READ(RTC_DAY_OF_MONTH); |
| 112 | mon = CMOS_READ(RTC_MONTH); |
| 113 | year = CMOS_READ(RTC_YEAR); |
| 114 | |
Andi Kleen | 45de707 | 2008-02-09 16:17:01 +0100 | [diff] [blame] | 115 | #ifdef CONFIG_ACPI |
Thomas Gleixner | 1122b13 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 116 | if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID && |
| 117 | acpi_gbl_FADT.century) |
| 118 | century = CMOS_READ(acpi_gbl_FADT.century); |
| 119 | #endif |
| 120 | |
Andi Kleen | 068c922 | 2008-02-09 16:16:59 +0100 | [diff] [blame] | 121 | status = CMOS_READ(RTC_CONTROL); |
Andi Kleen | 45de707 | 2008-02-09 16:17:01 +0100 | [diff] [blame] | 122 | WARN_ON_ONCE(RTC_ALWAYS_BCD && (status & RTC_DM_BINARY)); |
Andi Kleen | 068c922 | 2008-02-09 16:16:59 +0100 | [diff] [blame] | 123 | |
| 124 | if (RTC_ALWAYS_BCD || !(status & RTC_DM_BINARY)) { |
Matt Mackall | 41623b0 | 2006-03-28 01:56:09 -0800 | [diff] [blame] | 125 | BCD_TO_BIN(sec); |
| 126 | BCD_TO_BIN(min); |
| 127 | BCD_TO_BIN(hour); |
| 128 | BCD_TO_BIN(day); |
| 129 | BCD_TO_BIN(mon); |
| 130 | BCD_TO_BIN(year); |
| 131 | } |
| 132 | |
Thomas Gleixner | 1122b13 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 133 | if (century) { |
| 134 | BCD_TO_BIN(century); |
| 135 | year += century * 100; |
| 136 | printk(KERN_INFO "Extended CMOS year: %d\n", century * 100); |
Andi Kleen | b62576a | 2008-02-09 16:16:58 +0100 | [diff] [blame] | 137 | } else |
Thomas Gleixner | 1122b13 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 138 | year += CMOS_YEARS_OFFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
| 140 | return mktime(year, mon, day, hour, min, sec); |
| 141 | } |
| 142 | |
Thomas Gleixner | fe599f9 | 2008-01-30 13:30:26 +0100 | [diff] [blame] | 143 | /* Routines for accessing the CMOS RAM/RTC. */ |
| 144 | unsigned char rtc_cmos_read(unsigned char addr) |
| 145 | { |
| 146 | unsigned char val; |
| 147 | |
| 148 | lock_cmos_prefix(addr); |
David P. Reed | 04aaa7b | 2008-02-17 16:56:39 -0500 | [diff] [blame] | 149 | outb(addr, RTC_PORT(0)); |
| 150 | val = inb(RTC_PORT(1)); |
Thomas Gleixner | fe599f9 | 2008-01-30 13:30:26 +0100 | [diff] [blame] | 151 | lock_cmos_suffix(addr); |
| 152 | return val; |
| 153 | } |
| 154 | EXPORT_SYMBOL(rtc_cmos_read); |
| 155 | |
| 156 | void rtc_cmos_write(unsigned char val, unsigned char addr) |
| 157 | { |
| 158 | lock_cmos_prefix(addr); |
David P. Reed | 04aaa7b | 2008-02-17 16:56:39 -0500 | [diff] [blame] | 159 | outb(addr, RTC_PORT(0)); |
| 160 | outb(val, RTC_PORT(1)); |
Thomas Gleixner | fe599f9 | 2008-01-30 13:30:26 +0100 | [diff] [blame] | 161 | lock_cmos_suffix(addr); |
| 162 | } |
| 163 | EXPORT_SYMBOL(rtc_cmos_write); |
| 164 | |
| 165 | static int set_rtc_mmss(unsigned long nowtime) |
| 166 | { |
| 167 | int retval; |
| 168 | unsigned long flags; |
| 169 | |
Thomas Gleixner | fe599f9 | 2008-01-30 13:30:26 +0100 | [diff] [blame] | 170 | spin_lock_irqsave(&rtc_lock, flags); |
| 171 | retval = set_wallclock(nowtime); |
| 172 | spin_unlock_irqrestore(&rtc_lock, flags); |
| 173 | |
| 174 | return retval; |
| 175 | } |
| 176 | |
| 177 | /* not static: needed by APM */ |
| 178 | unsigned long read_persistent_clock(void) |
| 179 | { |
Thomas Gleixner | 1122b13 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 180 | unsigned long retval, flags; |
Thomas Gleixner | fe599f9 | 2008-01-30 13:30:26 +0100 | [diff] [blame] | 181 | |
| 182 | spin_lock_irqsave(&rtc_lock, flags); |
| 183 | retval = get_wallclock(); |
| 184 | spin_unlock_irqrestore(&rtc_lock, flags); |
| 185 | |
| 186 | return retval; |
| 187 | } |
| 188 | |
| 189 | int update_persistent_clock(struct timespec now) |
| 190 | { |
| 191 | return set_rtc_mmss(now.tv_sec); |
| 192 | } |
Ingo Molnar | cdc7957 | 2008-01-30 13:32:39 +0100 | [diff] [blame] | 193 | |
Ingo Molnar | 92767af | 2008-01-30 13:32:40 +0100 | [diff] [blame] | 194 | unsigned long long native_read_tsc(void) |
Ingo Molnar | cdc7957 | 2008-01-30 13:32:39 +0100 | [diff] [blame] | 195 | { |
Ingo Molnar | 92767af | 2008-01-30 13:32:40 +0100 | [diff] [blame] | 196 | return __native_read_tsc(); |
Ingo Molnar | cdc7957 | 2008-01-30 13:32:39 +0100 | [diff] [blame] | 197 | } |
Ingo Molnar | 92767af | 2008-01-30 13:32:40 +0100 | [diff] [blame] | 198 | EXPORT_SYMBOL(native_read_tsc); |
| 199 | |