blob: d4d8277e890e12252d13400581d3951831bbbf7a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Thomas Gleixnerfe599f92008-01-30 13:30:26 +01002 * RTC related functions
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 */
Thomas Gleixner1122b132008-01-30 13:30:27 +01004#include <linux/acpi.h>
Thomas Gleixnerfe599f92008-01-30 13:30:26 +01005#include <linux/bcd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/mc146818rtc.h>
7
Thomas Gleixnerfe599f92008-01-30 13:30:26 +01008#include <asm/time.h>
Ingo Molnarcdc79572008-01-30 13:32:39 +01009#include <asm/vsyscall.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
Thomas Gleixner1122b132008-01-30 13:30:27 +010011#ifdef CONFIG_X86_32
Thomas Gleixner1122b132008-01-30 13:30:27 +010012/*
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 */
17volatile unsigned long cmos_lock = 0;
18EXPORT_SYMBOL(cmos_lock);
Thomas Gleixner1122b132008-01-30 13:30:27 +010019#endif
20
Andi Kleenb62576a2008-02-09 16:16:58 +010021/* For two digit years assume time is always after that */
22#define CMOS_YEARS_OFFS 2000
23
Thomas Gleixner1122b132008-01-30 13:30:27 +010024DEFINE_SPINLOCK(rtc_lock);
25EXPORT_SYMBOL(rtc_lock);
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027/*
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 Gleixnerfe599f92008-01-30 13:30:26 +010037int mach_set_rtc_mmss(unsigned long nowtime)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
39 int retval = 0;
40 int real_seconds, real_minutes, cmos_minutes;
41 unsigned char save_control, save_freq_select;
42
Thomas Gleixner1122b132008-01-30 13:30:27 +010043 /* tell the clock it's being set */
44 save_control = CMOS_READ(RTC_CONTROL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
46
Thomas Gleixner1122b132008-01-30 13:30:27 +010047 /* stop and reset prescaler */
48 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 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 Gleixner1122b132008-01-30 13:30:27 +010063 /* correct for half hour time zone */
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1)
Thomas Gleixner1122b132008-01-30 13:30:27 +010065 real_minutes += 30;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 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 Gleixnerfe599f92008-01-30 13:30:26 +010095unsigned long mach_get_cmos_time(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Andi Kleen068c9222008-02-09 16:16:59 +010097 unsigned int status, year, mon, day, hour, min, sec, century = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Thomas Gleixner1122b132008-01-30 13:30:27 +010099 /*
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 Mackall63732c22006-03-28 01:55:58 -0800107
Thomas Gleixner1122b132008-01-30 13:30:27 +0100108 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
115#if defined(CONFIG_ACPI) && defined(CONFIG_X86_64)
116 /* CHECKME: Is this really 64bit only ??? */
117 if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
118 acpi_gbl_FADT.century)
119 century = CMOS_READ(acpi_gbl_FADT.century);
120#endif
121
Andi Kleen068c9222008-02-09 16:16:59 +0100122 status = CMOS_READ(RTC_CONTROL);
123 WARN_ON_ONCE((RTC_ALWAYS_BCD && (status & RTC_DM_BINARY));
124
125 if (RTC_ALWAYS_BCD || !(status & RTC_DM_BINARY)) {
Matt Mackall41623b02006-03-28 01:56:09 -0800126 BCD_TO_BIN(sec);
127 BCD_TO_BIN(min);
128 BCD_TO_BIN(hour);
129 BCD_TO_BIN(day);
130 BCD_TO_BIN(mon);
131 BCD_TO_BIN(year);
132 }
133
Thomas Gleixner1122b132008-01-30 13:30:27 +0100134 if (century) {
135 BCD_TO_BIN(century);
136 year += century * 100;
137 printk(KERN_INFO "Extended CMOS year: %d\n", century * 100);
Andi Kleenb62576a2008-02-09 16:16:58 +0100138 } else
Thomas Gleixner1122b132008-01-30 13:30:27 +0100139 year += CMOS_YEARS_OFFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 return mktime(year, mon, day, hour, min, sec);
142}
143
Thomas Gleixnerfe599f92008-01-30 13:30:26 +0100144/* Routines for accessing the CMOS RAM/RTC. */
145unsigned char rtc_cmos_read(unsigned char addr)
146{
147 unsigned char val;
148
149 lock_cmos_prefix(addr);
150 outb_p(addr, RTC_PORT(0));
151 val = inb_p(RTC_PORT(1));
152 lock_cmos_suffix(addr);
153 return val;
154}
155EXPORT_SYMBOL(rtc_cmos_read);
156
157void rtc_cmos_write(unsigned char val, unsigned char addr)
158{
159 lock_cmos_prefix(addr);
160 outb_p(addr, RTC_PORT(0));
161 outb_p(val, RTC_PORT(1));
162 lock_cmos_suffix(addr);
163}
164EXPORT_SYMBOL(rtc_cmos_write);
165
166static int set_rtc_mmss(unsigned long nowtime)
167{
168 int retval;
169 unsigned long flags;
170
Thomas Gleixnerfe599f92008-01-30 13:30:26 +0100171 spin_lock_irqsave(&rtc_lock, flags);
172 retval = set_wallclock(nowtime);
173 spin_unlock_irqrestore(&rtc_lock, flags);
174
175 return retval;
176}
177
178/* not static: needed by APM */
179unsigned long read_persistent_clock(void)
180{
Thomas Gleixner1122b132008-01-30 13:30:27 +0100181 unsigned long retval, flags;
Thomas Gleixnerfe599f92008-01-30 13:30:26 +0100182
183 spin_lock_irqsave(&rtc_lock, flags);
184 retval = get_wallclock();
185 spin_unlock_irqrestore(&rtc_lock, flags);
186
187 return retval;
188}
189
190int update_persistent_clock(struct timespec now)
191{
192 return set_rtc_mmss(now.tv_sec);
193}
Ingo Molnarcdc79572008-01-30 13:32:39 +0100194
Ingo Molnar92767af2008-01-30 13:32:40 +0100195unsigned long long native_read_tsc(void)
Ingo Molnarcdc79572008-01-30 13:32:39 +0100196{
Ingo Molnar92767af2008-01-30 13:32:40 +0100197 return __native_read_tsc();
Ingo Molnarcdc79572008-01-30 13:32:39 +0100198}
Ingo Molnar92767af2008-01-30 13:32:40 +0100199EXPORT_SYMBOL(native_read_tsc);
200