rtc.c 4.6 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 * RTC related functions
L
Linus Torvalds 已提交
3
 */
J
Jaswinder Singh Rajput 已提交
4 5
#include <linux/platform_device.h>
#include <linux/mc146818rtc.h>
T
Thomas Gleixner 已提交
6
#include <linux/acpi.h>
7
#include <linux/bcd.h>
8
#include <linux/export.h>
S
Stas Sergeev 已提交
9
#include <linux/pnp.h>
10
#include <linux/of.h>
L
Linus Torvalds 已提交
11

12
#include <asm/vsyscall.h>
13
#include <asm/x86_init.h>
J
Jaswinder Singh Rajput 已提交
14
#include <asm/time.h>
15
#include <asm/mrst.h>
16
#include <asm/rtc.h>
L
Linus Torvalds 已提交
17

T
Thomas Gleixner 已提交
18 19 20 21 22 23
#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.
 */
J
Jaswinder Singh Rajput 已提交
24
volatile unsigned long cmos_lock;
T
Thomas Gleixner 已提交
25
EXPORT_SYMBOL(cmos_lock);
J
Jaswinder Singh Rajput 已提交
26
#endif /* CONFIG_X86_32 */
T
Thomas Gleixner 已提交
27

28 29 30
/* For two digit years assume time is always after that */
#define CMOS_YEARS_OFFS 2000

T
Thomas Gleixner 已提交
31 32 33
DEFINE_SPINLOCK(rtc_lock);
EXPORT_SYMBOL(rtc_lock);

L
Linus Torvalds 已提交
34 35 36 37 38 39 40
/*
 * 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.
 */
41
int mach_set_rtc_mmss(const struct timespec *now)
L
Linus Torvalds 已提交
42
{
43
	unsigned long nowtime = now->tv_sec;
44
	struct rtc_time tm;
J
Jaswinder Singh Rajput 已提交
45
	int retval = 0;
L
Linus Torvalds 已提交
46

47 48 49 50 51 52
	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);
L
Linus Torvalds 已提交
53
	} else {
54 55 56 57
		printk(KERN_ERR
		       "%s: Invalid RTC value: write of %lx to RTC failed\n",
			__FUNCTION__, nowtime);
		retval = -EINVAL;
L
Linus Torvalds 已提交
58 59 60 61
	}
	return retval;
}

62
void mach_get_cmos_time(struct timespec *now)
L
Linus Torvalds 已提交
63
{
64
	unsigned int status, year, mon, day, hour, min, sec, century = 0;
65 66 67
	unsigned long flags;

	spin_lock_irqsave(&rtc_lock, flags);
T
Thomas Gleixner 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

	/*
	 * 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);

85
#ifdef CONFIG_ACPI
T
Thomas Gleixner 已提交
86 87 88 89 90
	if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID &&
	    acpi_gbl_FADT.century)
		century = CMOS_READ(acpi_gbl_FADT.century);
#endif

91
	status = CMOS_READ(RTC_CONTROL);
92
	WARN_ON_ONCE(RTC_ALWAYS_BCD && (status & RTC_DM_BINARY));
93

94 95
	spin_unlock_irqrestore(&rtc_lock, flags);

96
	if (RTC_ALWAYS_BCD || !(status & RTC_DM_BINARY)) {
A
Adrian Bunk 已提交
97 98 99 100 101 102
		sec = bcd2bin(sec);
		min = bcd2bin(min);
		hour = bcd2bin(hour);
		day = bcd2bin(day);
		mon = bcd2bin(mon);
		year = bcd2bin(year);
103 104
	}

T
Thomas Gleixner 已提交
105
	if (century) {
A
Adrian Bunk 已提交
106
		century = bcd2bin(century);
T
Thomas Gleixner 已提交
107
		year += century * 100;
108
	} else
T
Thomas Gleixner 已提交
109
		year += CMOS_YEARS_OFFS;
L
Linus Torvalds 已提交
110

111 112
	now->tv_sec = mktime(year, mon, day, hour, min, sec);
	now->tv_nsec = 0;
L
Linus Torvalds 已提交
113 114
}

115 116 117 118 119 120
/* Routines for accessing the CMOS RAM/RTC. */
unsigned char rtc_cmos_read(unsigned char addr)
{
	unsigned char val;

	lock_cmos_prefix(addr);
121 122
	outb(addr, RTC_PORT(0));
	val = inb(RTC_PORT(1));
123
	lock_cmos_suffix(addr);
J
Jaswinder Singh Rajput 已提交
124

125 126 127 128 129 130 131
	return val;
}
EXPORT_SYMBOL(rtc_cmos_read);

void rtc_cmos_write(unsigned char val, unsigned char addr)
{
	lock_cmos_prefix(addr);
132 133
	outb(addr, RTC_PORT(0));
	outb(val, RTC_PORT(1));
134 135 136 137
	lock_cmos_suffix(addr);
}
EXPORT_SYMBOL(rtc_cmos_write);

138
int update_persistent_clock(struct timespec now)
139
{
140
	return x86_platform.set_wallclock(&now);
141 142 143
}

/* not static: needed by APM */
144
void read_persistent_clock(struct timespec *ts)
145
{
146
	x86_platform.get_wallclock(ts);
147 148
}

S
Stas Sergeev 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172

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
173
	static const char * const  const ids[] __initconst =
174 175 176 177 178 179 180 181 182 183 184 185 186 187
	    { "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
188 189
	if (of_have_populated_dt())
		return 0;
190

191 192 193 194
	/* Intel MID platforms don't have ioport rtc */
	if (mrst_identify_cpu())
		return -ENODEV;

S
Stas Sergeev 已提交
195
	platform_device_register(&rtc_device);
196 197
	dev_info(&rtc_device.dev,
		 "registered platform RTC device (no PNP device found)\n");
J
Jaswinder Singh Rajput 已提交
198

S
Stas Sergeev 已提交
199 200 201
	return 0;
}
device_initcall(add_rtc_cmos);