2005-04-16 22:20:36 +00:00
|
|
|
/*
|
2008-01-30 12:30:26 +00:00
|
|
|
* RTC related functions
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
2009-03-21 11:26:37 +00:00
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/mc146818rtc.h>
|
2008-01-30 12:30:27 +00:00
|
|
|
#include <linux/acpi.h>
|
2008-01-30 12:30:26 +00:00
|
|
|
#include <linux/bcd.h>
|
2011-05-26 16:22:53 +00:00
|
|
|
#include <linux/export.h>
|
2008-06-12 22:21:54 +00:00
|
|
|
#include <linux/pnp.h>
|
2011-02-22 20:07:46 +00:00
|
|
|
#include <linux/of.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-01-30 12:32:39 +00:00
|
|
|
#include <asm/vsyscall.h>
|
2009-09-10 02:48:56 +00:00
|
|
|
#include <asm/x86_init.h>
|
2009-03-21 11:26:37 +00:00
|
|
|
#include <asm/time.h>
|
2013-10-17 22:35:27 +00:00
|
|
|
#include <asm/intel-mid.h>
|
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: Prarit 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: John Stultz <john.stultz@linaro.org>
2013-02-14 17:02:54 +00:00
|
|
|
#include <asm/rtc.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2008-01-30 12:30:27 +00:00
|
|
|
#ifdef CONFIG_X86_32
|
|
|
|
/*
|
|
|
|
* This is a special lock that is owned by the CPU and holds the index
|
|
|
|
* register we are working with. It is required for NMI access to the
|
|
|
|
* CMOS/RTC registers. See include/asm-i386/mc146818rtc.h for details.
|
|
|
|
*/
|
2009-03-21 11:26:37 +00:00
|
|
|
volatile unsigned long cmos_lock;
|
2008-01-30 12:30:27 +00:00
|
|
|
EXPORT_SYMBOL(cmos_lock);
|
2009-03-21 11:26:37 +00:00
|
|
|
#endif /* CONFIG_X86_32 */
|
2008-01-30 12:30:27 +00:00
|
|
|
|
2008-02-09 15:16:58 +00:00
|
|
|
/* For two digit years assume time is always after that */
|
|
|
|
#define CMOS_YEARS_OFFS 2000
|
|
|
|
|
2008-01-30 12:30:27 +00:00
|
|
|
DEFINE_SPINLOCK(rtc_lock);
|
|
|
|
EXPORT_SYMBOL(rtc_lock);
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* In order to set the CMOS clock precisely, set_rtc_mmss has to be
|
|
|
|
* called 500 ms after the second nowtime has started, because when
|
|
|
|
* nowtime is written into the registers of the CMOS clock, it will
|
|
|
|
* jump to the next second precisely 500 ms later. Check the Motorola
|
|
|
|
* MC146818A or Dallas DS12887 data sheet for details.
|
|
|
|
*/
|
2013-05-13 17:56:06 +00:00
|
|
|
int mach_set_rtc_mmss(const struct timespec *now)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2013-05-13 17:56:06 +00:00
|
|
|
unsigned long nowtime = now->tv_sec;
|
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: Prarit 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: John Stultz <john.stultz@linaro.org>
2013-02-14 17:02:54 +00:00
|
|
|
struct rtc_time tm;
|
2009-03-21 11:26:37 +00:00
|
|
|
int retval = 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
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: Prarit 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: John Stultz <john.stultz@linaro.org>
2013-02-14 17:02:54 +00:00
|
|
|
rtc_time_to_tm(nowtime, &tm);
|
|
|
|
if (!rtc_valid_tm(&tm)) {
|
|
|
|
retval = set_rtc_time(&tm);
|
|
|
|
if (retval)
|
|
|
|
printk(KERN_ERR "%s: RTC write failed with error %d\n",
|
|
|
|
__FUNCTION__, retval);
|
2005-04-16 22:20:36 +00:00
|
|
|
} else {
|
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: Prarit 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: John Stultz <john.stultz@linaro.org>
2013-02-14 17:02:54 +00:00
|
|
|
printk(KERN_ERR
|
|
|
|
"%s: Invalid RTC value: write of %lx to RTC failed\n",
|
|
|
|
__FUNCTION__, nowtime);
|
|
|
|
retval = -EINVAL;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2013-05-13 17:56:06 +00:00
|
|
|
void mach_get_cmos_time(struct timespec *now)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2008-02-09 15:16:59 +00:00
|
|
|
unsigned int status, year, mon, day, hour, min, sec, century = 0;
|
2011-09-21 14:08:03 +00:00
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&rtc_lock, flags);
|
2008-01-30 12:30:27 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If UIP is clear, then we have >= 244 microseconds before
|
|
|
|
* RTC registers will be updated. Spec sheet says that this
|
|
|
|
* is the reliable way to read RTC - registers. If UIP is set
|
|
|
|
* then the register access might be invalid.
|
|
|
|
*/
|
|
|
|
while ((CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
|
|
|
|
cpu_relax();
|
|
|
|
|
|
|
|
sec = CMOS_READ(RTC_SECONDS);
|
|
|
|
min = CMOS_READ(RTC_MINUTES);
|
|
|
|
hour = CMOS_READ(RTC_HOURS);
|
|
|
|
day = CMOS_READ(RTC_DAY_OF_MONTH);
|
|
|
|
mon = CMOS_READ(RTC_MONTH);
|
|
|
|
year = CMOS_READ(RTC_YEAR);
|
|
|
|
|
2008-02-09 15:17:01 +00:00
|
|
|
#ifdef CONFIG_ACPI
|
2008-01-30 12:30:27 +00:00
|
|
|
if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
|
|
|
|
acpi_gbl_FADT.century)
|
|
|
|
century = CMOS_READ(acpi_gbl_FADT.century);
|
|
|
|
#endif
|
|
|
|
|
2008-02-09 15:16:59 +00:00
|
|
|
status = CMOS_READ(RTC_CONTROL);
|
2008-02-09 15:17:01 +00:00
|
|
|
WARN_ON_ONCE(RTC_ALWAYS_BCD && (status & RTC_DM_BINARY));
|
2008-02-09 15:16:59 +00:00
|
|
|
|
2011-09-21 14:08:03 +00:00
|
|
|
spin_unlock_irqrestore(&rtc_lock, flags);
|
|
|
|
|
2008-02-09 15:16:59 +00:00
|
|
|
if (RTC_ALWAYS_BCD || !(status & RTC_DM_BINARY)) {
|
2008-10-19 03:28:42 +00:00
|
|
|
sec = bcd2bin(sec);
|
|
|
|
min = bcd2bin(min);
|
|
|
|
hour = bcd2bin(hour);
|
|
|
|
day = bcd2bin(day);
|
|
|
|
mon = bcd2bin(mon);
|
|
|
|
year = bcd2bin(year);
|
2006-03-28 09:56:09 +00:00
|
|
|
}
|
|
|
|
|
2008-01-30 12:30:27 +00:00
|
|
|
if (century) {
|
2008-10-19 03:28:42 +00:00
|
|
|
century = bcd2bin(century);
|
2008-01-30 12:30:27 +00:00
|
|
|
year += century * 100;
|
2008-02-09 15:16:58 +00:00
|
|
|
} else
|
2008-01-30 12:30:27 +00:00
|
|
|
year += CMOS_YEARS_OFFS;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2013-05-13 17:56:06 +00:00
|
|
|
now->tv_sec = mktime(year, mon, day, hour, min, sec);
|
|
|
|
now->tv_nsec = 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2008-01-30 12:30:26 +00:00
|
|
|
/* Routines for accessing the CMOS RAM/RTC. */
|
|
|
|
unsigned char rtc_cmos_read(unsigned char addr)
|
|
|
|
{
|
|
|
|
unsigned char val;
|
|
|
|
|
|
|
|
lock_cmos_prefix(addr);
|
2008-02-17 21:56:39 +00:00
|
|
|
outb(addr, RTC_PORT(0));
|
|
|
|
val = inb(RTC_PORT(1));
|
2008-01-30 12:30:26 +00:00
|
|
|
lock_cmos_suffix(addr);
|
2009-03-21 11:26:37 +00:00
|
|
|
|
2008-01-30 12:30:26 +00:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(rtc_cmos_read);
|
|
|
|
|
|
|
|
void rtc_cmos_write(unsigned char val, unsigned char addr)
|
|
|
|
{
|
|
|
|
lock_cmos_prefix(addr);
|
2008-02-17 21:56:39 +00:00
|
|
|
outb(addr, RTC_PORT(0));
|
|
|
|
outb(val, RTC_PORT(1));
|
2008-01-30 12:30:26 +00:00
|
|
|
lock_cmos_suffix(addr);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(rtc_cmos_write);
|
|
|
|
|
2009-09-10 02:48:56 +00:00
|
|
|
int update_persistent_clock(struct timespec now)
|
2008-01-30 12:30:26 +00:00
|
|
|
{
|
2013-05-13 17:56:06 +00:00
|
|
|
return x86_platform.set_wallclock(&now);
|
2008-01-30 12:30:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* not static: needed by APM */
|
2009-08-14 13:47:31 +00:00
|
|
|
void read_persistent_clock(struct timespec *ts)
|
2008-01-30 12:30:26 +00:00
|
|
|
{
|
2013-05-13 17:56:06 +00:00
|
|
|
x86_platform.get_wallclock(ts);
|
2008-01-30 12:30:26 +00:00
|
|
|
}
|
|
|
|
|
2008-06-12 22:21:54 +00:00
|
|
|
|
|
|
|
static struct resource rtc_resources[] = {
|
|
|
|
[0] = {
|
|
|
|
.start = RTC_PORT(0),
|
|
|
|
.end = RTC_PORT(1),
|
|
|
|
.flags = IORESOURCE_IO,
|
|
|
|
},
|
|
|
|
[1] = {
|
|
|
|
.start = RTC_IRQ,
|
|
|
|
.end = RTC_IRQ,
|
|
|
|
.flags = IORESOURCE_IRQ,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct platform_device rtc_device = {
|
|
|
|
.name = "rtc_cmos",
|
|
|
|
.id = -1,
|
|
|
|
.resource = rtc_resources,
|
|
|
|
.num_resources = ARRAY_SIZE(rtc_resources),
|
|
|
|
};
|
|
|
|
|
|
|
|
static __init int add_rtc_cmos(void)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_PNP
|
2012-10-05 00:11:42 +00:00
|
|
|
static const char * const const ids[] __initconst =
|
2008-10-14 23:01:03 +00:00
|
|
|
{ "PNP0b00", "PNP0b01", "PNP0b02", };
|
|
|
|
struct pnp_dev *dev;
|
|
|
|
struct pnp_id *id;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
pnp_for_each_dev(dev) {
|
|
|
|
for (id = dev->id; id; id = id->next) {
|
|
|
|
for (i = 0; i < ARRAY_SIZE(ids); i++) {
|
|
|
|
if (compare_pnp_id(id, ids[i]) != 0)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2011-02-22 20:07:46 +00:00
|
|
|
if (of_have_populated_dt())
|
|
|
|
return 0;
|
2008-10-14 23:01:03 +00:00
|
|
|
|
2011-11-15 22:46:52 +00:00
|
|
|
/* Intel MID platforms don't have ioport rtc */
|
2013-10-17 22:35:29 +00:00
|
|
|
if (intel_mid_identify_cpu())
|
2011-11-15 22:46:52 +00:00
|
|
|
return -ENODEV;
|
|
|
|
|
2013-10-21 08:31:57 +00:00
|
|
|
#ifdef CONFIG_ACPI
|
|
|
|
if (acpi_gbl_FADT.boot_flags & ACPI_FADT_NO_CMOS_RTC) {
|
|
|
|
/* This warning can likely go away again in a year or two. */
|
|
|
|
pr_info("ACPI: not registering RTC platform device\n");
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-06-12 22:21:54 +00:00
|
|
|
platform_device_register(&rtc_device);
|
2008-10-14 23:01:03 +00:00
|
|
|
dev_info(&rtc_device.dev,
|
|
|
|
"registered platform RTC device (no PNP device found)\n");
|
2009-03-21 11:26:37 +00:00
|
|
|
|
2008-06-12 22:21:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
device_initcall(add_rtc_cmos);
|