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>
|
2011-11-15 22:46:52 +00:00
|
|
|
#include <asm/mrst.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.
|
|
|
|
*
|
|
|
|
* BUG: This routine does not handle hour overflow properly; it just
|
|
|
|
* sets the minutes. Usually you'll only notice that after reboot!
|
|
|
|
*/
|
2008-01-30 12:30:26 +00:00
|
|
|
int mach_set_rtc_mmss(unsigned long nowtime)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
int real_seconds, real_minutes, cmos_minutes;
|
|
|
|
unsigned char save_control, save_freq_select;
|
2011-09-21 14:08:03 +00:00
|
|
|
unsigned long flags;
|
2009-03-21 11:26:37 +00:00
|
|
|
int retval = 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2011-09-21 14:08:03 +00:00
|
|
|
spin_lock_irqsave(&rtc_lock, flags);
|
|
|
|
|
2008-01-30 12:30:27 +00:00
|
|
|
/* tell the clock it's being set */
|
|
|
|
save_control = CMOS_READ(RTC_CONTROL);
|
2005-04-16 22:20:36 +00:00
|
|
|
CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
|
|
|
|
|
2008-01-30 12:30:27 +00:00
|
|
|
/* stop and reset prescaler */
|
|
|
|
save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
|
2005-04-16 22:20:36 +00:00
|
|
|
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)
|
2008-10-19 03:28:42 +00:00
|
|
|
cmos_minutes = bcd2bin(cmos_minutes);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
2008-01-30 12:30:27 +00:00
|
|
|
/* correct for half hour time zone */
|
2005-04-16 22:20:36 +00:00
|
|
|
if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1)
|
2008-01-30 12:30:27 +00:00
|
|
|
real_minutes += 30;
|
2005-04-16 22:20:36 +00:00
|
|
|
real_minutes %= 60;
|
|
|
|
|
|
|
|
if (abs(real_minutes - cmos_minutes) < 30) {
|
|
|
|
if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
|
2008-10-19 03:28:42 +00:00
|
|
|
real_seconds = bin2bcd(real_seconds);
|
|
|
|
real_minutes = bin2bcd(real_minutes);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
2009-03-21 11:26:37 +00:00
|
|
|
CMOS_WRITE(real_seconds, RTC_SECONDS);
|
|
|
|
CMOS_WRITE(real_minutes, RTC_MINUTES);
|
2005-04-16 22:20:36 +00:00
|
|
|
} else {
|
2011-01-13 00:59:31 +00:00
|
|
|
printk_once(KERN_NOTICE
|
2005-04-16 22:20:36 +00:00
|
|
|
"set_rtc_mmss: can't update from %d to %d\n",
|
|
|
|
cmos_minutes, real_minutes);
|
|
|
|
retval = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
2011-09-21 14:08:03 +00:00
|
|
|
spin_unlock_irqrestore(&rtc_lock, flags);
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2008-01-30 12:30:26 +00:00
|
|
|
unsigned long mach_get_cmos_time(void)
|
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;
|
|
|
|
printk(KERN_INFO "Extended CMOS year: %d\n", 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
|
|
|
|
|
|
|
return mktime(year, mon, day, hour, min, sec);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2011-09-21 14:08:03 +00:00
|
|
|
return x86_platform.set_wallclock(now.tv_sec);
|
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
|
|
|
{
|
2011-09-21 14:08:03 +00:00
|
|
|
unsigned long retval;
|
2008-01-30 12:30:26 +00:00
|
|
|
|
2009-09-10 02:48:56 +00:00
|
|
|
retval = x86_platform.get_wallclock();
|
2008-01-30 12:30:26 +00:00
|
|
|
|
2009-08-14 13:47:31 +00:00
|
|
|
ts->tv_sec = retval;
|
|
|
|
ts->tv_nsec = 0;
|
2008-01-30 12:30:26 +00:00
|
|
|
}
|
|
|
|
|
2008-01-30 12:32:40 +00:00
|
|
|
unsigned long long native_read_tsc(void)
|
2008-01-30 12:32:39 +00:00
|
|
|
{
|
2008-01-30 12:32:40 +00:00
|
|
|
return __native_read_tsc();
|
2008-01-30 12:32:39 +00:00
|
|
|
}
|
2008-01-30 12:32:40 +00:00
|
|
|
EXPORT_SYMBOL(native_read_tsc);
|
|
|
|
|
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
|
2008-10-14 23:01:03 +00:00
|
|
|
static const char *ids[] __initconst =
|
|
|
|
{ "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 */
|
|
|
|
if (mrst_identify_cpu())
|
|
|
|
return -ENODEV;
|
|
|
|
|
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);
|