2010-11-11 01:29:00 +08:00
|
|
|
/*
|
2013-10-18 06:35:27 +08:00
|
|
|
* intel_mid_vrtc.c: Driver for virtual RTC device on Intel MID platform
|
2010-11-11 01:29:00 +08:00
|
|
|
*
|
|
|
|
* (C) Copyright 2009 Intel Corporation
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; version 2
|
|
|
|
* of the License.
|
|
|
|
*
|
|
|
|
* Note:
|
|
|
|
* VRTC is emulated by system controller firmware, the real HW
|
|
|
|
* RTC is located in the PMIC device. SCU FW shadows PMIC RTC
|
|
|
|
* in a memory mapped IO space that is visible to the host IA
|
|
|
|
* processor.
|
|
|
|
*
|
|
|
|
* This driver is based on RTC CMOS driver.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
2011-05-27 00:22:53 +08:00
|
|
|
#include <linux/export.h>
|
2010-11-11 01:29:00 +08:00
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/sfi.h>
|
2010-11-11 01:29:17 +08:00
|
|
|
#include <linux/platform_device.h>
|
2010-11-11 01:29:00 +08:00
|
|
|
|
2013-10-18 06:35:27 +08:00
|
|
|
#include <asm/intel-mid.h>
|
|
|
|
#include <asm/intel_mid_vrtc.h>
|
2010-11-11 01:29:00 +08:00
|
|
|
#include <asm/time.h>
|
|
|
|
#include <asm/fixmap.h>
|
|
|
|
|
|
|
|
static unsigned char __iomem *vrtc_virt_base;
|
|
|
|
|
|
|
|
unsigned char vrtc_cmos_read(unsigned char reg)
|
|
|
|
{
|
|
|
|
unsigned char retval;
|
|
|
|
|
|
|
|
/* vRTC's registers range from 0x0 to 0xD */
|
|
|
|
if (reg > 0xd || !vrtc_virt_base)
|
|
|
|
return 0xff;
|
|
|
|
|
|
|
|
lock_cmos_prefix(reg);
|
|
|
|
retval = __raw_readb(vrtc_virt_base + (reg << 2));
|
|
|
|
lock_cmos_suffix(reg);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(vrtc_cmos_read);
|
|
|
|
|
|
|
|
void vrtc_cmos_write(unsigned char val, unsigned char reg)
|
|
|
|
{
|
|
|
|
if (reg > 0xd || !vrtc_virt_base)
|
|
|
|
return;
|
|
|
|
|
|
|
|
lock_cmos_prefix(reg);
|
|
|
|
__raw_writeb(val, vrtc_virt_base + (reg << 2));
|
|
|
|
lock_cmos_suffix(reg);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(vrtc_cmos_write);
|
|
|
|
|
2013-05-29 08:03:09 +08:00
|
|
|
void vrtc_get_time(struct timespec *now)
|
2010-11-11 01:29:00 +08:00
|
|
|
{
|
|
|
|
u8 sec, min, hour, mday, mon;
|
2011-09-21 22:08:03 +08:00
|
|
|
unsigned long flags;
|
2010-11-11 01:29:00 +08:00
|
|
|
u32 year;
|
|
|
|
|
2011-09-21 22:08:03 +08:00
|
|
|
spin_lock_irqsave(&rtc_lock, flags);
|
|
|
|
|
2010-11-11 01:29:00 +08:00
|
|
|
while ((vrtc_cmos_read(RTC_FREQ_SELECT) & RTC_UIP))
|
|
|
|
cpu_relax();
|
|
|
|
|
|
|
|
sec = vrtc_cmos_read(RTC_SECONDS);
|
|
|
|
min = vrtc_cmos_read(RTC_MINUTES);
|
|
|
|
hour = vrtc_cmos_read(RTC_HOURS);
|
|
|
|
mday = vrtc_cmos_read(RTC_DAY_OF_MONTH);
|
|
|
|
mon = vrtc_cmos_read(RTC_MONTH);
|
|
|
|
year = vrtc_cmos_read(RTC_YEAR);
|
|
|
|
|
2011-09-21 22:08:03 +08:00
|
|
|
spin_unlock_irqrestore(&rtc_lock, flags);
|
|
|
|
|
2011-11-10 21:23:39 +08:00
|
|
|
/* vRTC YEAR reg contains the offset to 1972 */
|
|
|
|
year += 1972;
|
2010-11-11 01:29:00 +08:00
|
|
|
|
2013-10-18 06:35:25 +08:00
|
|
|
pr_info("vRTC: sec: %d min: %d hour: %d day: %d "
|
2010-11-11 01:29:00 +08:00
|
|
|
"mon: %d year: %d\n", sec, min, hour, mday, mon, year);
|
|
|
|
|
2013-05-29 08:03:09 +08:00
|
|
|
now->tv_sec = mktime(year, mon, mday, hour, min, sec);
|
|
|
|
now->tv_nsec = 0;
|
2010-11-11 01:29:00 +08:00
|
|
|
}
|
|
|
|
|
2013-05-29 08:03:09 +08:00
|
|
|
int vrtc_set_mmss(const struct timespec *now)
|
2010-11-11 01:29:00 +08:00
|
|
|
{
|
2011-09-21 22:08:03 +08:00
|
|
|
unsigned long flags;
|
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-15 01:02:54 +08:00
|
|
|
struct rtc_time tm;
|
|
|
|
int year;
|
|
|
|
int retval = 0;
|
|
|
|
|
2013-05-29 08:03:09 +08:00
|
|
|
rtc_time_to_tm(now->tv_sec, &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-15 01:02:54 +08:00
|
|
|
if (!rtc_valid_tm(&tm) && tm.tm_year >= 72) {
|
|
|
|
/*
|
|
|
|
* tm.year is the number of years since 1900, and the
|
|
|
|
* vrtc need the years since 1972.
|
|
|
|
*/
|
|
|
|
year = tm.tm_year - 72;
|
|
|
|
spin_lock_irqsave(&rtc_lock, flags);
|
|
|
|
vrtc_cmos_write(year, RTC_YEAR);
|
|
|
|
vrtc_cmos_write(tm.tm_mon, RTC_MONTH);
|
|
|
|
vrtc_cmos_write(tm.tm_mday, RTC_DAY_OF_MONTH);
|
|
|
|
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 {
|
2013-10-18 06:35:25 +08:00
|
|
|
pr_err("%s: Invalid vRTC value: write of %lx to vRTC failed\n",
|
2015-02-13 07:01:31 +08:00
|
|
|
__func__, 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-15 01:02:54 +08:00
|
|
|
retval = -EINVAL;
|
|
|
|
}
|
|
|
|
return retval;
|
2010-11-11 01:29:00 +08:00
|
|
|
}
|
|
|
|
|
2013-10-18 06:35:29 +08:00
|
|
|
void __init intel_mid_rtc_init(void)
|
2010-11-11 01:29:00 +08:00
|
|
|
{
|
2011-04-07 09:39:49 +08:00
|
|
|
unsigned long vrtc_paddr;
|
2010-11-11 01:29:00 +08:00
|
|
|
|
|
|
|
sfi_table_parse(SFI_SIG_MRTC, NULL, NULL, sfi_parse_mrtc);
|
2011-04-07 09:39:49 +08:00
|
|
|
|
|
|
|
vrtc_paddr = sfi_mrtc_array[0].phys_addr;
|
2011-02-15 00:13:32 +08:00
|
|
|
if (!sfi_mrtc_num || !vrtc_paddr)
|
2010-11-11 01:29:00 +08:00
|
|
|
return;
|
|
|
|
|
2011-02-15 00:13:32 +08:00
|
|
|
vrtc_virt_base = (void __iomem *)set_fixmap_offset_nocache(FIX_LNW_VRTC,
|
|
|
|
vrtc_paddr);
|
2010-11-11 01:29:00 +08:00
|
|
|
x86_platform.get_wallclock = vrtc_get_time;
|
|
|
|
x86_platform.set_wallclock = vrtc_set_mmss;
|
|
|
|
}
|
2010-11-11 01:29:17 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The Moorestown platform has a memory mapped virtual RTC device that emulates
|
|
|
|
* the programming interface of the RTC.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static struct resource vrtc_resources[] = {
|
|
|
|
[0] = {
|
|
|
|
.flags = IORESOURCE_MEM,
|
|
|
|
},
|
|
|
|
[1] = {
|
|
|
|
.flags = IORESOURCE_IRQ,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct platform_device vrtc_device = {
|
|
|
|
.name = "rtc_mrst",
|
|
|
|
.id = -1,
|
|
|
|
.resource = vrtc_resources,
|
|
|
|
.num_resources = ARRAY_SIZE(vrtc_resources),
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Register the RTC device if appropriate */
|
2013-10-18 06:35:29 +08:00
|
|
|
static int __init intel_mid_device_create(void)
|
2010-11-11 01:29:17 +08:00
|
|
|
{
|
|
|
|
/* No Moorestown, no device */
|
2013-10-18 06:35:29 +08:00
|
|
|
if (!intel_mid_identify_cpu())
|
2010-11-11 01:29:17 +08:00
|
|
|
return -ENODEV;
|
|
|
|
/* No timer, no device */
|
|
|
|
if (!sfi_mrtc_num)
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
/* iomem resource */
|
|
|
|
vrtc_resources[0].start = sfi_mrtc_array[0].phys_addr;
|
|
|
|
vrtc_resources[0].end = sfi_mrtc_array[0].phys_addr +
|
|
|
|
MRST_VRTC_MAP_SZ;
|
|
|
|
/* irq resource */
|
|
|
|
vrtc_resources[1].start = sfi_mrtc_array[0].irq;
|
|
|
|
vrtc_resources[1].end = sfi_mrtc_array[0].irq;
|
|
|
|
|
2010-11-19 02:16:45 +08:00
|
|
|
return platform_device_register(&vrtc_device);
|
2010-11-11 01:29:17 +08:00
|
|
|
}
|
2015-05-02 08:05:49 +08:00
|
|
|
device_initcall(intel_mid_device_create);
|