x86: use year 2000 offset for cmos clock
[linux-2.6-block.git] / arch / x86 / kernel / rtc.c
CommitLineData
1da177e4 1/*
fe599f9f 2 * RTC related functions
1da177e4 3 */
1122b134 4#include <linux/acpi.h>
fe599f9f 5#include <linux/bcd.h>
1da177e4
LT
6#include <linux/mc146818rtc.h>
7
fe599f9f 8#include <asm/time.h>
cdc7957d 9#include <asm/vsyscall.h>
1da177e4 10
1122b134 11#ifdef CONFIG_X86_32
1122b134
TG
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 */
17volatile unsigned long cmos_lock = 0;
18EXPORT_SYMBOL(cmos_lock);
1122b134
TG
19#endif
20
b62576a2
AK
21/* For two digit years assume time is always after that */
22#define CMOS_YEARS_OFFS 2000
23
1122b134
TG
24DEFINE_SPINLOCK(rtc_lock);
25EXPORT_SYMBOL(rtc_lock);
26
1da177e4
LT
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 */
fe599f9f 37int mach_set_rtc_mmss(unsigned long nowtime)
1da177e4
LT
38{
39 int retval = 0;
40 int real_seconds, real_minutes, cmos_minutes;
41 unsigned char save_control, save_freq_select;
42
1122b134
TG
43 /* tell the clock it's being set */
44 save_control = CMOS_READ(RTC_CONTROL);
1da177e4
LT
45 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
46
1122b134
TG
47 /* stop and reset prescaler */
48 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
1da177e4
LT
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;
1122b134 63 /* correct for half hour time zone */
1da177e4 64 if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1)
1122b134 65 real_minutes += 30;
1da177e4
LT
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
fe599f9f 95unsigned long mach_get_cmos_time(void)
1da177e4 96{
1122b134
TG
97 unsigned int year, mon, day, hour, min, sec, century = 0;
98
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();
107
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
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
122 if (RTC_ALWAYS_BCD || !(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY)) {
41623b06
MM
123 BCD_TO_BIN(sec);
124 BCD_TO_BIN(min);
125 BCD_TO_BIN(hour);
126 BCD_TO_BIN(day);
127 BCD_TO_BIN(mon);
128 BCD_TO_BIN(year);
129 }
130
1122b134
TG
131 if (century) {
132 BCD_TO_BIN(century);
133 year += century * 100;
134 printk(KERN_INFO "Extended CMOS year: %d\n", century * 100);
b62576a2 135 } else
1122b134 136 year += CMOS_YEARS_OFFS;
1da177e4
LT
137
138 return mktime(year, mon, day, hour, min, sec);
139}
140
fe599f9f
TG
141/* Routines for accessing the CMOS RAM/RTC. */
142unsigned char rtc_cmos_read(unsigned char addr)
143{
144 unsigned char val;
145
146 lock_cmos_prefix(addr);
147 outb_p(addr, RTC_PORT(0));
148 val = inb_p(RTC_PORT(1));
149 lock_cmos_suffix(addr);
150 return val;
151}
152EXPORT_SYMBOL(rtc_cmos_read);
153
154void rtc_cmos_write(unsigned char val, unsigned char addr)
155{
156 lock_cmos_prefix(addr);
157 outb_p(addr, RTC_PORT(0));
158 outb_p(val, RTC_PORT(1));
159 lock_cmos_suffix(addr);
160}
161EXPORT_SYMBOL(rtc_cmos_write);
162
163static int set_rtc_mmss(unsigned long nowtime)
164{
165 int retval;
166 unsigned long flags;
167
fe599f9f
TG
168 spin_lock_irqsave(&rtc_lock, flags);
169 retval = set_wallclock(nowtime);
170 spin_unlock_irqrestore(&rtc_lock, flags);
171
172 return retval;
173}
174
175/* not static: needed by APM */
176unsigned long read_persistent_clock(void)
177{
1122b134 178 unsigned long retval, flags;
fe599f9f
TG
179
180 spin_lock_irqsave(&rtc_lock, flags);
181 retval = get_wallclock();
182 spin_unlock_irqrestore(&rtc_lock, flags);
183
184 return retval;
185}
186
187int update_persistent_clock(struct timespec now)
188{
189 return set_rtc_mmss(now.tv_sec);
190}
cdc7957d 191
92767af0 192unsigned long long native_read_tsc(void)
cdc7957d 193{
92767af0 194 return __native_read_tsc();
cdc7957d 195}
92767af0
IM
196EXPORT_SYMBOL(native_read_tsc);
197