2005-04-16 15:20:36 -07:00
|
|
|
/*
|
2008-01-30 05:30:26 -07:00
|
|
|
* RTC related functions
|
2005-04-16 15:20:36 -07:00
|
|
|
*/
|
2009-03-21 04:26:37 -07:00
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/mc146818rtc.h>
|
2008-01-30 05:30:27 -07:00
|
|
|
#include <linux/acpi.h>
|
2008-01-30 05:30:26 -07:00
|
|
|
#include <linux/bcd.h>
|
2008-06-12 15:21:54 -07:00
|
|
|
#include <linux/pnp.h>
|
2011-02-22 13:07:46 -07:00
|
|
|
#include <linux/of.h>
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2008-01-30 05:32:39 -07:00
|
|
|
#include <asm/vsyscall.h>
|
2009-09-09 19:48:56 -07:00
|
|
|
#include <asm/x86_init.h>
|
2009-03-21 04:26:37 -07:00
|
|
|
#include <asm/time.h>
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2008-01-30 05:30:27 -07: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 04:26:37 -07:00
|
|
|
volatile unsigned long cmos_lock;
|
2008-01-30 05:30:27 -07:00
|
|
|
EXPORT_SYMBOL(cmos_lock);
|
2009-03-21 04:26:37 -07:00
|
|
|
#endif /* CONFIG_X86_32 */
|
2008-01-30 05:30:27 -07:00
|
|
|
|
2008-02-09 08:16:58 -07:00
|
|
|
/* For two digit years assume time is always after that */
|
|
|
|
#define CMOS_YEARS_OFFS 2000
|
|
|
|
|
2008-01-30 05:30:27 -07:00
|
|
|
DEFINE_SPINLOCK(rtc_lock);
|
|
|
|
EXPORT_SYMBOL(rtc_lock);
|
|
|
|
|
2005-04-16 15:20:36 -07: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 05:30:26 -07:00
|
|
|
int mach_set_rtc_mmss(unsigned long nowtime)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
|
|
|
int real_seconds, real_minutes, cmos_minutes;
|
|
|
|
unsigned char save_control, save_freq_select;
|
2009-03-21 04:26:37 -07:00
|
|
|
int retval = 0;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
2008-01-30 05:30:27 -07:00
|
|
|
/* tell the clock it's being set */
|
|
|
|
save_control = CMOS_READ(RTC_CONTROL);
|
2005-04-16 15:20:36 -07:00
|
|
|
CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL);
|
|
|
|
|
2008-01-30 05:30:27 -07:00
|
|
|
/* stop and reset prescaler */
|
|
|
|
save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
|
2005-04-16 15:20:36 -07: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-18 20:28:42 -07:00
|
|
|
cmos_minutes = bcd2bin(cmos_minutes);
|
2005-04-16 15:20:36 -07: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 05:30:27 -07:00
|
|
|
/* correct for half hour time zone */
|
2005-04-16 15:20:36 -07:00
|
|
|
if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1)
|
2008-01-30 05:30:27 -07:00
|
|
|
real_minutes += 30;
|
2005-04-16 15:20:36 -07:00
|
|
|
real_minutes %= 60;
|
|
|
|
|
|
|
|
if (abs(real_minutes - cmos_minutes) < 30) {
|
|
|
|
if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
|
2008-10-18 20:28:42 -07:00
|
|
|
real_seconds = bin2bcd(real_seconds);
|
|
|
|
real_minutes = bin2bcd(real_minutes);
|
2005-04-16 15:20:36 -07:00
|
|
|
}
|
2009-03-21 04:26:37 -07:00
|
|
|
CMOS_WRITE(real_seconds, RTC_SECONDS);
|
|
|
|
CMOS_WRITE(real_minutes, RTC_MINUTES);
|
2005-04-16 15:20:36 -07:00
|
|
|
} else {
|
2011-01-12 17:59:31 -07:00
|
|
|
printk_once(KERN_NOTICE
|
2005-04-16 15:20:36 -07: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);
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2008-01-30 05:30:26 -07:00
|
|
|
unsigned long mach_get_cmos_time(void)
|
2005-04-16 15:20:36 -07:00
|
|
|
{
|
2008-02-09 08:16:59 -07:00
|
|
|
unsigned int status, year, mon, day, hour, min, sec, century = 0;
|
2008-01-30 05:30:27 -07: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 08:17:01 -07:00
|
|
|
#ifdef CONFIG_ACPI
|
2008-01-30 05:30:27 -07: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 08:16:59 -07:00
|
|
|
status = CMOS_READ(RTC_CONTROL);
|
2008-02-09 08:17:01 -07:00
|
|
|
WARN_ON_ONCE(RTC_ALWAYS_BCD && (status & RTC_DM_BINARY));
|
2008-02-09 08:16:59 -07:00
|
|
|
|
|
|
|
if (RTC_ALWAYS_BCD || !(status & RTC_DM_BINARY)) {
|
2008-10-18 20:28:42 -07:00
|
|
|
sec = bcd2bin(sec);
|
|
|
|
min = bcd2bin(min);
|
|
|
|
hour = bcd2bin(hour);
|
|
|
|
day = bcd2bin(day);
|
|
|
|
mon = bcd2bin(mon);
|
|
|
|
year = bcd2bin(year);
|
2006-03-28 02:56:09 -07:00
|
|
|
}
|
|
|
|
|
2008-01-30 05:30:27 -07:00
|
|
|
if (century) {
|
2008-10-18 20:28:42 -07:00
|
|
|
century = bcd2bin(century);
|
2008-01-30 05:30:27 -07:00
|
|
|
year += century * 100;
|
|
|
|
printk(KERN_INFO "Extended CMOS year: %d\n", century * 100);
|
2008-02-09 08:16:58 -07:00
|
|
|
} else
|
2008-01-30 05:30:27 -07:00
|
|
|
year += CMOS_YEARS_OFFS;
|
2005-04-16 15:20:36 -07:00
|
|
|
|
|
|
|
return mktime(year, mon, day, hour, min, sec);
|
|
|
|
}
|
|
|
|
|
2008-01-30 05:30:26 -07: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 14:56:39 -07:00
|
|
|
outb(addr, RTC_PORT(0));
|
|
|
|
val = inb(RTC_PORT(1));
|
2008-01-30 05:30:26 -07:00
|
|
|
lock_cmos_suffix(addr);
|
2009-03-21 04:26:37 -07:00
|
|
|
|
2008-01-30 05:30:26 -07: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 14:56:39 -07:00
|
|
|
outb(addr, RTC_PORT(0));
|
|
|
|
outb(val, RTC_PORT(1));
|
2008-01-30 05:30:26 -07:00
|
|
|
lock_cmos_suffix(addr);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(rtc_cmos_write);
|
|
|
|
|
2009-09-09 19:48:56 -07:00
|
|
|
int update_persistent_clock(struct timespec now)
|
2008-01-30 05:30:26 -07:00
|
|
|
{
|
|
|
|
unsigned long flags;
|
2009-03-21 04:26:37 -07:00
|
|
|
int retval;
|
2008-01-30 05:30:26 -07:00
|
|
|
|
|
|
|
spin_lock_irqsave(&rtc_lock, flags);
|
2009-09-09 19:48:56 -07:00
|
|
|
retval = x86_platform.set_wallclock(now.tv_sec);
|
2008-01-30 05:30:26 -07:00
|
|
|
spin_unlock_irqrestore(&rtc_lock, flags);
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* not static: needed by APM */
|
2009-08-14 06:47:31 -07:00
|
|
|
void read_persistent_clock(struct timespec *ts)
|
2008-01-30 05:30:26 -07:00
|
|
|
{
|
2008-01-30 05:30:27 -07:00
|
|
|
unsigned long retval, flags;
|
2008-01-30 05:30:26 -07:00
|
|
|
|
|
|
|
spin_lock_irqsave(&rtc_lock, flags);
|
2009-09-09 19:48:56 -07:00
|
|
|
retval = x86_platform.get_wallclock();
|
2008-01-30 05:30:26 -07:00
|
|
|
spin_unlock_irqrestore(&rtc_lock, flags);
|
|
|
|
|
2009-08-14 06:47:31 -07:00
|
|
|
ts->tv_sec = retval;
|
|
|
|
ts->tv_nsec = 0;
|
2008-01-30 05:30:26 -07:00
|
|
|
}
|
|
|
|
|
2008-01-30 05:32:40 -07:00
|
|
|
unsigned long long native_read_tsc(void)
|
2008-01-30 05:32:39 -07:00
|
|
|
{
|
2008-01-30 05:32:40 -07:00
|
|
|
return __native_read_tsc();
|
2008-01-30 05:32:39 -07:00
|
|
|
}
|
2008-01-30 05:32:40 -07:00
|
|
|
EXPORT_SYMBOL(native_read_tsc);
|
|
|
|
|
2008-06-12 15:21:54 -07: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 16:01:03 -07: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 13:07:46 -07:00
|
|
|
if (of_have_populated_dt())
|
|
|
|
return 0;
|
2008-10-14 16:01:03 -07:00
|
|
|
|
2008-06-12 15:21:54 -07:00
|
|
|
platform_device_register(&rtc_device);
|
2008-10-14 16:01:03 -07:00
|
|
|
dev_info(&rtc_device.dev,
|
|
|
|
"registered platform RTC device (no PNP device found)\n");
|
2009-03-21 04:26:37 -07:00
|
|
|
|
2008-06-12 15:21:54 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
device_initcall(add_rtc_cmos);
|