Commit 3195ef59 authored by Prarit Bhargava's avatar Prarit Bhargava Committed by John Stultz

x86: Do full rtc synchronization with ntp

Every 11 minutes ntp attempts to update the x86 rtc with the current
system time.  Currently, the x86 code only updates the rtc if the system
time is within +/-15 minutes of the current value of the rtc. This
was done originally to avoid setting the RTC if the RTC was in localtime
mode (common with Windows dualbooting).  Other architectures do a full
synchronization and now that we have better infrastructure to detect
when the RTC is in localtime, there is no reason that x86 should be
software limited to a 30 minute window.

This patch changes the behavior of the kernel to do a full synchronization
(year, month, day, hour, minute, and second) of the rtc when ntp requests
a synchronization between the system time and the rtc.

I've used the RTC library functions in this patchset as they do all the
required bounds checking.

Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: John Stultz <john.stultz@linaro.org>
Cc: x86@kernel.org
Cc: Matt Fleming <matt.fleming@intel.com>
Cc: David Vrabel <david.vrabel@citrix.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: linux-efi@vger.kernel.org
Signed-off-by: default avatarPrarit Bhargava <prarit@redhat.com>
[jstultz: Tweak commit message, fold in build fix found by fengguang
Also add select RTC_LIB to X86, per new dependency, as found by prarit]
Signed-off-by: default avatarJohn Stultz <john.stultz@linaro.org>
parent 7859e404
...@@ -120,6 +120,7 @@ config X86 ...@@ -120,6 +120,7 @@ config X86
select OLD_SIGSUSPEND3 if X86_32 || IA32_EMULATION select OLD_SIGSUSPEND3 if X86_32 || IA32_EMULATION
select OLD_SIGACTION if X86_32 select OLD_SIGACTION if X86_32
select COMPAT_OLD_SIGACTION if IA32_EMULATION select COMPAT_OLD_SIGACTION if IA32_EMULATION
select RTC_LIB
config INSTRUCTION_DECODER config INSTRUCTION_DECODER
def_bool y def_bool y
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
#include <asm/x86_init.h> #include <asm/x86_init.h>
#include <asm/time.h> #include <asm/time.h>
#include <asm/mrst.h> #include <asm/mrst.h>
#include <asm/rtc.h>
#ifdef CONFIG_X86_32 #ifdef CONFIG_X86_32
/* /*
...@@ -36,70 +37,24 @@ EXPORT_SYMBOL(rtc_lock); ...@@ -36,70 +37,24 @@ EXPORT_SYMBOL(rtc_lock);
* nowtime is written into the registers of the CMOS clock, it will * nowtime is written into the registers of the CMOS clock, it will
* jump to the next second precisely 500 ms later. Check the Motorola * jump to the next second precisely 500 ms later. Check the Motorola
* MC146818A or Dallas DS12887 data sheet for details. * MC146818A or Dallas DS12887 data sheet for details.
*
* BUG: This routine does not handle hour overflow properly; it just
* sets the minutes. Usually you'll only notice that after reboot!
*/ */
int mach_set_rtc_mmss(unsigned long nowtime) int mach_set_rtc_mmss(unsigned long nowtime)
{ {
int real_seconds, real_minutes, cmos_minutes; struct rtc_time tm;
unsigned char save_control, save_freq_select;
unsigned long flags;
int retval = 0; int retval = 0;
spin_lock_irqsave(&rtc_lock, flags); rtc_time_to_tm(nowtime, &tm);
if (!rtc_valid_tm(&tm)) {
/* tell the clock it's being set */ retval = set_rtc_time(&tm);
save_control = CMOS_READ(RTC_CONTROL); if (retval)
CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL); printk(KERN_ERR "%s: RTC write failed with error %d\n",
__FUNCTION__, retval);
/* stop and reset prescaler */
save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT);
cmos_minutes = CMOS_READ(RTC_MINUTES);
if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
cmos_minutes = bcd2bin(cmos_minutes);
/*
* since we're only adjusting minutes and seconds,
* don't interfere with hour overflow. This avoids
* messing with unknown time zones but requires your
* RTC not to be off by more than 15 minutes
*/
real_seconds = nowtime % 60;
real_minutes = nowtime / 60;
/* correct for half hour time zone */
if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1)
real_minutes += 30;
real_minutes %= 60;
if (abs(real_minutes - cmos_minutes) < 30) {
if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
real_seconds = bin2bcd(real_seconds);
real_minutes = bin2bcd(real_minutes);
}
CMOS_WRITE(real_seconds, RTC_SECONDS);
CMOS_WRITE(real_minutes, RTC_MINUTES);
} else { } else {
printk_once(KERN_NOTICE printk(KERN_ERR
"set_rtc_mmss: can't update from %d to %d\n", "%s: Invalid RTC value: write of %lx to RTC failed\n",
cmos_minutes, real_minutes); __FUNCTION__, nowtime);
retval = -1; retval = -EINVAL;
} }
/* The following flags have to be released exactly in this order,
* otherwise the DS12887 (popular MC146818A clone with integrated
* battery and quartz) will not reset the oscillator and will not
* update precisely 500 ms later. You won't find this mentioned in
* the Dallas Semiconductor data sheets, but who believes data
* sheets anyway ... -- Markus Kuhn
*/
CMOS_WRITE(save_control, RTC_CONTROL);
CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
spin_unlock_irqrestore(&rtc_lock, flags);
return retval; return retval;
} }
......
...@@ -48,6 +48,7 @@ ...@@ -48,6 +48,7 @@
#include <asm/cacheflush.h> #include <asm/cacheflush.h>
#include <asm/tlbflush.h> #include <asm/tlbflush.h>
#include <asm/x86_init.h> #include <asm/x86_init.h>
#include <asm/rtc.h>
#define EFI_DEBUG 1 #define EFI_DEBUG 1
...@@ -258,10 +259,10 @@ static efi_status_t __init phys_efi_get_time(efi_time_t *tm, ...@@ -258,10 +259,10 @@ static efi_status_t __init phys_efi_get_time(efi_time_t *tm,
int efi_set_rtc_mmss(unsigned long nowtime) int efi_set_rtc_mmss(unsigned long nowtime)
{ {
int real_seconds, real_minutes;
efi_status_t status; efi_status_t status;
efi_time_t eft; efi_time_t eft;
efi_time_cap_t cap; efi_time_cap_t cap;
struct rtc_time tm;
status = efi.get_time(&eft, &cap); status = efi.get_time(&eft, &cap);
if (status != EFI_SUCCESS) { if (status != EFI_SUCCESS) {
...@@ -269,13 +270,20 @@ int efi_set_rtc_mmss(unsigned long nowtime) ...@@ -269,13 +270,20 @@ int efi_set_rtc_mmss(unsigned long nowtime)
return -1; return -1;
} }
real_seconds = nowtime % 60; rtc_time_to_tm(nowtime, &tm);
real_minutes = nowtime / 60; if (!rtc_valid_tm(&tm)) {
if (((abs(real_minutes - eft.minute) + 15)/30) & 1) eft.year = tm.tm_year + 1900;
real_minutes += 30; eft.month = tm.tm_mon + 1;
real_minutes %= 60; eft.day = tm.tm_mday;
eft.minute = real_minutes; eft.minute = tm.tm_min;
eft.second = real_seconds; eft.second = tm.tm_sec;
eft.nanosecond = 0;
} else {
printk(KERN_ERR
"%s: Invalid EFI RTC value: write of %lx to EFI RTC failed\n",
__FUNCTION__, nowtime);
return -1;
}
status = efi.set_time(&eft); status = efi.set_time(&eft);
if (status != EFI_SUCCESS) { if (status != EFI_SUCCESS) {
......
...@@ -85,27 +85,35 @@ unsigned long vrtc_get_time(void) ...@@ -85,27 +85,35 @@ unsigned long vrtc_get_time(void)
return mktime(year, mon, mday, hour, min, sec); return mktime(year, mon, mday, hour, min, sec);
} }
/* Only care about the minutes and seconds */
int vrtc_set_mmss(unsigned long nowtime) int vrtc_set_mmss(unsigned long nowtime)
{ {
int real_sec, real_min;
unsigned long flags; unsigned long flags;
int vrtc_min; struct rtc_time tm;
int year;
spin_lock_irqsave(&rtc_lock, flags); int retval = 0;
vrtc_min = vrtc_cmos_read(RTC_MINUTES);
rtc_time_to_tm(nowtime, &tm);
real_sec = nowtime % 60; if (!rtc_valid_tm(&tm) && tm.tm_year >= 72) {
real_min = nowtime / 60; /*
if (((abs(real_min - vrtc_min) + 15)/30) & 1) * tm.year is the number of years since 1900, and the
real_min += 30; * vrtc need the years since 1972.
real_min %= 60; */
year = tm.tm_year - 72;
vrtc_cmos_write(real_sec, RTC_SECONDS); spin_lock_irqsave(&rtc_lock, flags);
vrtc_cmos_write(real_min, RTC_MINUTES); vrtc_cmos_write(year, RTC_YEAR);
spin_unlock_irqrestore(&rtc_lock, flags); vrtc_cmos_write(tm.tm_mon, RTC_MONTH);
vrtc_cmos_write(tm.tm_mday, RTC_DAY_OF_MONTH);
return 0; vrtc_cmos_write(tm.tm_hour, RTC_HOURS);
vrtc_cmos_write(tm.tm_min, RTC_MINUTES);
vrtc_cmos_write(tm.tm_sec, RTC_SECONDS);
spin_unlock_irqrestore(&rtc_lock, flags);
} else {
printk(KERN_ERR
"%s: Invalid vRTC value: write of %lx to vRTC failed\n",
__FUNCTION__, nowtime);
retval = -EINVAL;
}
return retval;
} }
void __init mrst_rtc_init(void) void __init mrst_rtc_init(void)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment