2 * RTC related functions
4 #include <linux/acpi.h>
6 #include <linux/mc146818rtc.h>
11 # define CMOS_YEARS_OFFS 1900
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.
17 volatile unsigned long cmos_lock = 0;
18 EXPORT_SYMBOL(cmos_lock);
21 * x86-64 systems only exists since 2002.
22 * This will work up to Dec 31, 2100
24 # define CMOS_YEARS_OFFS 2000
27 DEFINE_SPINLOCK(rtc_lock);
28 EXPORT_SYMBOL(rtc_lock);
31 * In order to set the CMOS clock precisely, set_rtc_mmss has to be
32 * called 500 ms after the second nowtime has started, because when
33 * nowtime is written into the registers of the CMOS clock, it will
34 * jump to the next second precisely 500 ms later. Check the Motorola
35 * MC146818A or Dallas DS12887 data sheet for details.
37 * BUG: This routine does not handle hour overflow properly; it just
38 * sets the minutes. Usually you'll only notice that after reboot!
40 int mach_set_rtc_mmss(unsigned long nowtime)
43 int real_seconds, real_minutes, cmos_minutes;
44 unsigned char save_control, save_freq_select;
46 /* tell the clock it's being set */
47 save_control = CMOS_READ(RTC_CONTROL);
48 CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
50 /* stop and reset prescaler */
51 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
52 CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
54 cmos_minutes = CMOS_READ(RTC_MINUTES);
55 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
56 BCD_TO_BIN(cmos_minutes);
59 * since we're only adjusting minutes and seconds,
60 * don't interfere with hour overflow. This avoids
61 * messing with unknown time zones but requires your
62 * RTC not to be off by more than 15 minutes
64 real_seconds = nowtime % 60;
65 real_minutes = nowtime / 60;
66 /* correct for half hour time zone */
67 if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1)
71 if (abs(real_minutes - cmos_minutes) < 30) {
72 if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
73 BIN_TO_BCD(real_seconds);
74 BIN_TO_BCD(real_minutes);
76 CMOS_WRITE(real_seconds,RTC_SECONDS);
77 CMOS_WRITE(real_minutes,RTC_MINUTES);
80 "set_rtc_mmss: can't update from %d to %d\n",
81 cmos_minutes, real_minutes);
85 /* The following flags have to be released exactly in this order,
86 * otherwise the DS12887 (popular MC146818A clone with integrated
87 * battery and quartz) will not reset the oscillator and will not
88 * update precisely 500 ms later. You won't find this mentioned in
89 * the Dallas Semiconductor data sheets, but who believes data
90 * sheets anyway ... -- Markus Kuhn
92 CMOS_WRITE(save_control, RTC_CONTROL);
93 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
98 unsigned long mach_get_cmos_time(void)
100 unsigned int year, mon, day, hour, min, sec, century = 0;
103 * If UIP is clear, then we have >= 244 microseconds before
104 * RTC registers will be updated. Spec sheet says that this
105 * is the reliable way to read RTC - registers. If UIP is set
106 * then the register access might be invalid.
108 while ((CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
111 sec = CMOS_READ(RTC_SECONDS);
112 min = CMOS_READ(RTC_MINUTES);
113 hour = CMOS_READ(RTC_HOURS);
114 day = CMOS_READ(RTC_DAY_OF_MONTH);
115 mon = CMOS_READ(RTC_MONTH);
116 year = CMOS_READ(RTC_YEAR);
118 #if defined(CONFIG_ACPI) && defined(CONFIG_X86_64)
119 /* CHECKME: Is this really 64bit only ??? */
120 if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
121 acpi_gbl_FADT.century)
122 century = CMOS_READ(acpi_gbl_FADT.century);
125 if (RTC_ALWAYS_BCD || !(CMOS_READ(RTC_CONTROL) & RTC_DM_BINARY)) {
136 year += century * 100;
137 printk(KERN_INFO "Extended CMOS year: %d\n", century * 100);
139 year += CMOS_YEARS_OFFS;
144 return mktime(year, mon, day, hour, min, sec);
147 /* Routines for accessing the CMOS RAM/RTC. */
148 unsigned char rtc_cmos_read(unsigned char addr)
152 lock_cmos_prefix(addr);
153 outb_p(addr, RTC_PORT(0));
154 val = inb_p(RTC_PORT(1));
155 lock_cmos_suffix(addr);
158 EXPORT_SYMBOL(rtc_cmos_read);
160 void rtc_cmos_write(unsigned char val, unsigned char addr)
162 lock_cmos_prefix(addr);
163 outb_p(addr, RTC_PORT(0));
164 outb_p(val, RTC_PORT(1));
165 lock_cmos_suffix(addr);
167 EXPORT_SYMBOL(rtc_cmos_write);
169 static int set_rtc_mmss(unsigned long nowtime)
174 spin_lock_irqsave(&rtc_lock, flags);
175 retval = set_wallclock(nowtime);
176 spin_unlock_irqrestore(&rtc_lock, flags);
181 /* not static: needed by APM */
182 unsigned long read_persistent_clock(void)
184 unsigned long retval, flags;
186 spin_lock_irqsave(&rtc_lock, flags);
187 retval = get_wallclock();
188 spin_unlock_irqrestore(&rtc_lock, flags);
193 int update_persistent_clock(struct timespec now)
195 return set_rtc_mmss(now.tv_sec);