2005-04-17 02:20:36 +04:00
|
|
|
/*
|
2008-01-30 15:30:26 +03:00
|
|
|
* RTC related functions
|
2005-04-17 02:20:36 +04:00
|
|
|
*/
|
2009-03-21 14:26:37 +03:00
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/mc146818rtc.h>
|
2008-01-30 15:30:27 +03:00
|
|
|
#include <linux/acpi.h>
|
2008-01-30 15:30:26 +03:00
|
|
|
#include <linux/bcd.h>
|
2011-05-26 20:22:53 +04:00
|
|
|
#include <linux/export.h>
|
2008-06-13 02:21:54 +04:00
|
|
|
#include <linux/pnp.h>
|
2011-02-22 23:07:46 +03:00
|
|
|
#include <linux/of.h>
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2008-01-30 15:32:39 +03:00
|
|
|
#include <asm/vsyscall.h>
|
2009-09-10 06:48:56 +04:00
|
|
|
#include <asm/x86_init.h>
|
2009-03-21 14:26:37 +03:00
|
|
|
#include <asm/time.h>
|
2013-10-18 02:35:27 +04:00
|
|
|
#include <asm/intel-mid.h>
|
2016-04-14 03:04:34 +03:00
|
|
|
#include <asm/setup.h>
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2008-01-30 15:30:27 +03: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 14:26:37 +03:00
|
|
|
volatile unsigned long cmos_lock;
|
2008-01-30 15:30:27 +03:00
|
|
|
EXPORT_SYMBOL(cmos_lock);
|
2009-03-21 14:26:37 +03:00
|
|
|
#endif /* CONFIG_X86_32 */
|
2008-01-30 15:30:27 +03:00
|
|
|
|
2008-02-09 18:16:58 +03:00
|
|
|
/* For two digit years assume time is always after that */
|
|
|
|
#define CMOS_YEARS_OFFS 2000
|
|
|
|
|
2008-01-30 15:30:27 +03:00
|
|
|
DEFINE_SPINLOCK(rtc_lock);
|
|
|
|
EXPORT_SYMBOL(rtc_lock);
|
|
|
|
|
2005-04-17 02:20:36 +04: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 21:56:06 +04:00
|
|
|
int mach_set_rtc_mmss(const struct timespec *now)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2013-05-13 21:56:06 +04: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 21:02:54 +04:00
|
|
|
struct rtc_time tm;
|
2009-03-21 14:26:37 +03:00
|
|
|
int retval = 0;
|
2005-04-17 02:20:36 +04: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 21:02:54 +04:00
|
|
|
rtc_time_to_tm(nowtime, &tm);
|
|
|
|
if (!rtc_valid_tm(&tm)) {
|
2016-05-30 21:57:51 +03:00
|
|
|
retval = mc146818_set_time(&tm);
|
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 21:02:54 +04:00
|
|
|
if (retval)
|
|
|
|
printk(KERN_ERR "%s: RTC write failed with error %d\n",
|
2015-02-13 02:01:31 +03:00
|
|
|
__func__, retval);
|
2005-04-17 02:20:36 +04: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 21:02:54 +04:00
|
|
|
printk(KERN_ERR
|
|
|
|
"%s: Invalid RTC value: write of %lx to RTC failed\n",
|
2015-02-13 02:01:31 +03:00
|
|
|
__func__, nowtime);
|
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 21:02:54 +04:00
|
|
|
retval = -EINVAL;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2013-05-13 21:56:06 +04:00
|
|
|
void mach_get_cmos_time(struct timespec *now)
|
2005-04-17 02:20:36 +04:00
|
|
|
{
|
2008-02-09 18:16:59 +03:00
|
|
|
unsigned int status, year, mon, day, hour, min, sec, century = 0;
|
2011-09-21 18:08:03 +04:00
|
|
|
unsigned long flags;
|
|
|
|
|
2016-11-29 01:35:19 +03:00
|
|
|
/*
|
|
|
|
* If pm_trace abused the RTC as storage, set the timespec to 0,
|
|
|
|
* which tells the caller that this RTC value is unusable.
|
|
|
|
*/
|
|
|
|
if (!pm_trace_rtc_valid()) {
|
|
|
|
now->tv_sec = now->tv_nsec = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-21 18:08:03 +04:00
|
|
|
spin_lock_irqsave(&rtc_lock, flags);
|
2008-01-30 15:30:27 +03: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 18:17:01 +03:00
|
|
|
#ifdef CONFIG_ACPI
|
2008-01-30 15:30:27 +03: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 18:16:59 +03:00
|
|
|
status = CMOS_READ(RTC_CONTROL);
|
2008-02-09 18:17:01 +03:00
|
|
|
WARN_ON_ONCE(RTC_ALWAYS_BCD && (status & RTC_DM_BINARY));
|
2008-02-09 18:16:59 +03:00
|
|
|
|
2011-09-21 18:08:03 +04:00
|
|
|
spin_unlock_irqrestore(&rtc_lock, flags);
|
|
|
|
|
2008-02-09 18:16:59 +03:00
|
|
|
if (RTC_ALWAYS_BCD || !(status & RTC_DM_BINARY)) {
|
2008-10-19 07:28:42 +04:00
|
|
|
sec = bcd2bin(sec);
|
|
|
|
min = bcd2bin(min);
|
|
|
|
hour = bcd2bin(hour);
|
|
|
|
day = bcd2bin(day);
|
|
|
|
mon = bcd2bin(mon);
|
|
|
|
year = bcd2bin(year);
|
2006-03-28 13:56:09 +04:00
|
|
|
}
|
|
|
|
|
2008-01-30 15:30:27 +03:00
|
|
|
if (century) {
|
2008-10-19 07:28:42 +04:00
|
|
|
century = bcd2bin(century);
|
2008-01-30 15:30:27 +03:00
|
|
|
year += century * 100;
|
2008-02-09 18:16:58 +03:00
|
|
|
} else
|
2008-01-30 15:30:27 +03:00
|
|
|
year += CMOS_YEARS_OFFS;
|
2005-04-17 02:20:36 +04:00
|
|
|
|
2013-05-13 21:56:06 +04:00
|
|
|
now->tv_sec = mktime(year, mon, day, hour, min, sec);
|
|
|
|
now->tv_nsec = 0;
|
2005-04-17 02:20:36 +04:00
|
|
|
}
|
|
|
|
|
2008-01-30 15:30:26 +03: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-18 00:56:39 +03:00
|
|
|
outb(addr, RTC_PORT(0));
|
|
|
|
val = inb(RTC_PORT(1));
|
2008-01-30 15:30:26 +03:00
|
|
|
lock_cmos_suffix(addr);
|
2009-03-21 14:26:37 +03:00
|
|
|
|
2008-01-30 15:30:26 +03:00
|
|
|
return val;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(rtc_cmos_read);
|
|
|
|
|
|
|
|
void rtc_cmos_write(unsigned char val, unsigned char addr)
|
|
|
|
{
|
|
|
|
lock_cmos_prefix(addr);
|
2008-02-18 00:56:39 +03:00
|
|
|
outb(addr, RTC_PORT(0));
|
|
|
|
outb(val, RTC_PORT(1));
|
2008-01-30 15:30:26 +03:00
|
|
|
lock_cmos_suffix(addr);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(rtc_cmos_write);
|
|
|
|
|
2009-09-10 06:48:56 +04:00
|
|
|
int update_persistent_clock(struct timespec now)
|
2008-01-30 15:30:26 +03:00
|
|
|
{
|
2013-05-13 21:56:06 +04:00
|
|
|
return x86_platform.set_wallclock(&now);
|
2008-01-30 15:30:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* not static: needed by APM */
|
2009-08-14 17:47:31 +04:00
|
|
|
void read_persistent_clock(struct timespec *ts)
|
2008-01-30 15:30:26 +03:00
|
|
|
{
|
2013-05-13 21:56:06 +04:00
|
|
|
x86_platform.get_wallclock(ts);
|
2008-01-30 15:30:26 +03:00
|
|
|
}
|
|
|
|
|
2008-06-13 02:21:54 +04: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
|
2015-01-14 17:07:55 +03:00
|
|
|
static const char * const ids[] __initconst =
|
2008-10-15 03:01:03 +04: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
|
2016-04-14 03:04:34 +03:00
|
|
|
if (!x86_platform.legacy.rtc)
|
2015-12-11 17:07:53 +03:00
|
|
|
return -ENODEV;
|
|
|
|
|
2008-06-13 02:21:54 +04:00
|
|
|
platform_device_register(&rtc_device);
|
2008-10-15 03:01:03 +04:00
|
|
|
dev_info(&rtc_device.dev,
|
|
|
|
"registered platform RTC device (no PNP device found)\n");
|
2009-03-21 14:26:37 +03:00
|
|
|
|
2008-06-13 02:21:54 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
device_initcall(add_rtc_cmos);
|