| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
| Thomas Gleixner | fe599f9 | 2008-01-30 13:30:26 +0100 | [diff] [blame] | 2 |  * RTC related functions | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 |  */ | 
| Jaswinder Singh Rajput | 8383d82 | 2009-03-21 16:56:37 +0530 | [diff] [blame] | 4 | #include <linux/platform_device.h> | 
 | 5 | #include <linux/mc146818rtc.h> | 
| Thomas Gleixner | 1122b13 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 6 | #include <linux/acpi.h> | 
| Thomas Gleixner | fe599f9 | 2008-01-30 13:30:26 +0100 | [diff] [blame] | 7 | #include <linux/bcd.h> | 
| Stas Sergeev | 1da2e3d | 2008-06-12 15:21:54 -0700 | [diff] [blame] | 8 | #include <linux/pnp.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 |  | 
| Ingo Molnar | cdc7957 | 2008-01-30 13:32:39 +0100 | [diff] [blame] | 10 | #include <asm/vsyscall.h> | 
| Feng Tang | 7bd867d | 2009-09-10 10:48:56 +0800 | [diff] [blame] | 11 | #include <asm/x86_init.h> | 
| Jaswinder Singh Rajput | 8383d82 | 2009-03-21 16:56:37 +0530 | [diff] [blame] | 12 | #include <asm/time.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 |  | 
| Thomas Gleixner | 1122b13 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 14 | #ifdef CONFIG_X86_32 | 
| Thomas Gleixner | 1122b13 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 15 | /* | 
 | 16 |  * This is a special lock that is owned by the CPU and holds the index | 
 | 17 |  * register we are working with.  It is required for NMI access to the | 
 | 18 |  * CMOS/RTC registers.  See include/asm-i386/mc146818rtc.h for details. | 
 | 19 |  */ | 
| Jaswinder Singh Rajput | 8383d82 | 2009-03-21 16:56:37 +0530 | [diff] [blame] | 20 | volatile unsigned long cmos_lock; | 
| Thomas Gleixner | 1122b13 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 21 | EXPORT_SYMBOL(cmos_lock); | 
| Jaswinder Singh Rajput | 8383d82 | 2009-03-21 16:56:37 +0530 | [diff] [blame] | 22 | #endif /* CONFIG_X86_32 */ | 
| Thomas Gleixner | 1122b13 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 23 |  | 
| Andi Kleen | b62576a | 2008-02-09 16:16:58 +0100 | [diff] [blame] | 24 | /* For two digit years assume time is always after that */ | 
 | 25 | #define CMOS_YEARS_OFFS 2000 | 
 | 26 |  | 
| Thomas Gleixner | 1122b13 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 27 | DEFINE_SPINLOCK(rtc_lock); | 
 | 28 | EXPORT_SYMBOL(rtc_lock); | 
 | 29 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | /* | 
 | 31 |  * In order to set the CMOS clock precisely, set_rtc_mmss has to be | 
 | 32 |  * called 500 ms after the second nowtime has started, because when | 
 | 33 |  * nowtime is written into the registers of the CMOS clock, it will | 
 | 34 |  * jump to the next second precisely 500 ms later. Check the Motorola | 
 | 35 |  * MC146818A or Dallas DS12887 data sheet for details. | 
 | 36 |  * | 
 | 37 |  * BUG: This routine does not handle hour overflow properly; it just | 
 | 38 |  *      sets the minutes. Usually you'll only notice that after reboot! | 
 | 39 |  */ | 
| Thomas Gleixner | fe599f9 | 2008-01-30 13:30:26 +0100 | [diff] [blame] | 40 | int mach_set_rtc_mmss(unsigned long nowtime) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | 	int real_seconds, real_minutes, cmos_minutes; | 
 | 43 | 	unsigned char save_control, save_freq_select; | 
| Jaswinder Singh Rajput | 8383d82 | 2009-03-21 16:56:37 +0530 | [diff] [blame] | 44 | 	int retval = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 |  | 
| Thomas Gleixner | 1122b13 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 46 | 	 /* tell the clock it's being set */ | 
 | 47 | 	save_control = CMOS_READ(RTC_CONTROL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | 	CMOS_WRITE((save_control|RTC_SET), RTC_CONTROL); | 
 | 49 |  | 
| Thomas Gleixner | 1122b13 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 50 | 	/* stop and reset prescaler */ | 
 | 51 | 	save_freq_select = CMOS_READ(RTC_FREQ_SELECT); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | 	CMOS_WRITE((save_freq_select|RTC_DIV_RESET2), RTC_FREQ_SELECT); | 
 | 53 |  | 
 | 54 | 	cmos_minutes = CMOS_READ(RTC_MINUTES); | 
 | 55 | 	if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) | 
| Adrian Bunk | 357c6e6 | 2008-10-18 20:28:42 -0700 | [diff] [blame] | 56 | 		cmos_minutes = bcd2bin(cmos_minutes); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 |  | 
 | 58 | 	/* | 
 | 59 | 	 * since we're only adjusting minutes and seconds, | 
 | 60 | 	 * don't interfere with hour overflow. This avoids | 
 | 61 | 	 * messing with unknown time zones but requires your | 
 | 62 | 	 * RTC not to be off by more than 15 minutes | 
 | 63 | 	 */ | 
 | 64 | 	real_seconds = nowtime % 60; | 
 | 65 | 	real_minutes = nowtime / 60; | 
| Thomas Gleixner | 1122b13 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 66 | 	/* correct for half hour time zone */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | 	if (((abs(real_minutes - cmos_minutes) + 15)/30) & 1) | 
| Thomas Gleixner | 1122b13 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 68 | 		real_minutes += 30; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | 	real_minutes %= 60; | 
 | 70 |  | 
 | 71 | 	if (abs(real_minutes - cmos_minutes) < 30) { | 
 | 72 | 		if (!(save_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) { | 
| Adrian Bunk | 357c6e6 | 2008-10-18 20:28:42 -0700 | [diff] [blame] | 73 | 			real_seconds = bin2bcd(real_seconds); | 
 | 74 | 			real_minutes = bin2bcd(real_minutes); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | 		} | 
| Jaswinder Singh Rajput | 8383d82 | 2009-03-21 16:56:37 +0530 | [diff] [blame] | 76 | 		CMOS_WRITE(real_seconds, RTC_SECONDS); | 
 | 77 | 		CMOS_WRITE(real_minutes, RTC_MINUTES); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | 	} else { | 
 | 79 | 		printk(KERN_WARNING | 
 | 80 | 		       "set_rtc_mmss: can't update from %d to %d\n", | 
 | 81 | 		       cmos_minutes, real_minutes); | 
 | 82 | 		retval = -1; | 
 | 83 | 	} | 
 | 84 |  | 
 | 85 | 	/* The following flags have to be released exactly in this order, | 
 | 86 | 	 * otherwise the DS12887 (popular MC146818A clone with integrated | 
 | 87 | 	 * battery and quartz) will not reset the oscillator and will not | 
 | 88 | 	 * update precisely 500 ms later. You won't find this mentioned in | 
 | 89 | 	 * the Dallas Semiconductor data sheets, but who believes data | 
 | 90 | 	 * sheets anyway ...                           -- Markus Kuhn | 
 | 91 | 	 */ | 
 | 92 | 	CMOS_WRITE(save_control, RTC_CONTROL); | 
 | 93 | 	CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT); | 
 | 94 |  | 
 | 95 | 	return retval; | 
 | 96 | } | 
 | 97 |  | 
| Thomas Gleixner | fe599f9 | 2008-01-30 13:30:26 +0100 | [diff] [blame] | 98 | unsigned long mach_get_cmos_time(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | { | 
| Andi Kleen | 068c922 | 2008-02-09 16:16:59 +0100 | [diff] [blame] | 100 | 	unsigned int status, year, mon, day, hour, min, sec, century = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 |  | 
| Thomas Gleixner | 1122b13 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 102 | 	/* | 
 | 103 | 	 * If UIP is clear, then we have >= 244 microseconds before | 
 | 104 | 	 * RTC registers will be updated.  Spec sheet says that this | 
 | 105 | 	 * is the reliable way to read RTC - registers. If UIP is set | 
 | 106 | 	 * then the register access might be invalid. | 
 | 107 | 	 */ | 
 | 108 | 	while ((CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP)) | 
 | 109 | 		cpu_relax(); | 
| Matt Mackall | 63732c2 | 2006-03-28 01:55:58 -0800 | [diff] [blame] | 110 |  | 
| Thomas Gleixner | 1122b13 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 111 | 	sec = CMOS_READ(RTC_SECONDS); | 
 | 112 | 	min = CMOS_READ(RTC_MINUTES); | 
 | 113 | 	hour = CMOS_READ(RTC_HOURS); | 
 | 114 | 	day = CMOS_READ(RTC_DAY_OF_MONTH); | 
 | 115 | 	mon = CMOS_READ(RTC_MONTH); | 
 | 116 | 	year = CMOS_READ(RTC_YEAR); | 
 | 117 |  | 
| Andi Kleen | 45de707 | 2008-02-09 16:17:01 +0100 | [diff] [blame] | 118 | #ifdef CONFIG_ACPI | 
| Thomas Gleixner | 1122b13 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 119 | 	if (acpi_gbl_FADT.header.revision >= FADT2_REVISION_ID && | 
 | 120 | 	    acpi_gbl_FADT.century) | 
 | 121 | 		century = CMOS_READ(acpi_gbl_FADT.century); | 
 | 122 | #endif | 
 | 123 |  | 
| Andi Kleen | 068c922 | 2008-02-09 16:16:59 +0100 | [diff] [blame] | 124 | 	status = CMOS_READ(RTC_CONTROL); | 
| Andi Kleen | 45de707 | 2008-02-09 16:17:01 +0100 | [diff] [blame] | 125 | 	WARN_ON_ONCE(RTC_ALWAYS_BCD && (status & RTC_DM_BINARY)); | 
| Andi Kleen | 068c922 | 2008-02-09 16:16:59 +0100 | [diff] [blame] | 126 |  | 
 | 127 | 	if (RTC_ALWAYS_BCD || !(status & RTC_DM_BINARY)) { | 
| Adrian Bunk | 357c6e6 | 2008-10-18 20:28:42 -0700 | [diff] [blame] | 128 | 		sec = bcd2bin(sec); | 
 | 129 | 		min = bcd2bin(min); | 
 | 130 | 		hour = bcd2bin(hour); | 
 | 131 | 		day = bcd2bin(day); | 
 | 132 | 		mon = bcd2bin(mon); | 
 | 133 | 		year = bcd2bin(year); | 
| Matt Mackall | 41623b0 | 2006-03-28 01:56:09 -0800 | [diff] [blame] | 134 | 	} | 
 | 135 |  | 
| Thomas Gleixner | 1122b13 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 136 | 	if (century) { | 
| Adrian Bunk | 357c6e6 | 2008-10-18 20:28:42 -0700 | [diff] [blame] | 137 | 		century = bcd2bin(century); | 
| Thomas Gleixner | 1122b13 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 138 | 		year += century * 100; | 
 | 139 | 		printk(KERN_INFO "Extended CMOS year: %d\n", century * 100); | 
| Andi Kleen | b62576a | 2008-02-09 16:16:58 +0100 | [diff] [blame] | 140 | 	} else | 
| Thomas Gleixner | 1122b13 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 141 | 		year += CMOS_YEARS_OFFS; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 |  | 
 | 143 | 	return mktime(year, mon, day, hour, min, sec); | 
 | 144 | } | 
 | 145 |  | 
| Thomas Gleixner | fe599f9 | 2008-01-30 13:30:26 +0100 | [diff] [blame] | 146 | /* Routines for accessing the CMOS RAM/RTC. */ | 
 | 147 | unsigned char rtc_cmos_read(unsigned char addr) | 
 | 148 | { | 
 | 149 | 	unsigned char val; | 
 | 150 |  | 
 | 151 | 	lock_cmos_prefix(addr); | 
| David P. Reed | 04aaa7b | 2008-02-17 16:56:39 -0500 | [diff] [blame] | 152 | 	outb(addr, RTC_PORT(0)); | 
 | 153 | 	val = inb(RTC_PORT(1)); | 
| Thomas Gleixner | fe599f9 | 2008-01-30 13:30:26 +0100 | [diff] [blame] | 154 | 	lock_cmos_suffix(addr); | 
| Jaswinder Singh Rajput | 8383d82 | 2009-03-21 16:56:37 +0530 | [diff] [blame] | 155 |  | 
| Thomas Gleixner | fe599f9 | 2008-01-30 13:30:26 +0100 | [diff] [blame] | 156 | 	return val; | 
 | 157 | } | 
 | 158 | EXPORT_SYMBOL(rtc_cmos_read); | 
 | 159 |  | 
 | 160 | void rtc_cmos_write(unsigned char val, unsigned char addr) | 
 | 161 | { | 
 | 162 | 	lock_cmos_prefix(addr); | 
| David P. Reed | 04aaa7b | 2008-02-17 16:56:39 -0500 | [diff] [blame] | 163 | 	outb(addr, RTC_PORT(0)); | 
 | 164 | 	outb(val, RTC_PORT(1)); | 
| Thomas Gleixner | fe599f9 | 2008-01-30 13:30:26 +0100 | [diff] [blame] | 165 | 	lock_cmos_suffix(addr); | 
 | 166 | } | 
 | 167 | EXPORT_SYMBOL(rtc_cmos_write); | 
 | 168 |  | 
| Feng Tang | 7bd867d | 2009-09-10 10:48:56 +0800 | [diff] [blame] | 169 | int update_persistent_clock(struct timespec now) | 
| Thomas Gleixner | fe599f9 | 2008-01-30 13:30:26 +0100 | [diff] [blame] | 170 | { | 
| Thomas Gleixner | fe599f9 | 2008-01-30 13:30:26 +0100 | [diff] [blame] | 171 | 	unsigned long flags; | 
| Jaswinder Singh Rajput | 8383d82 | 2009-03-21 16:56:37 +0530 | [diff] [blame] | 172 | 	int retval; | 
| Thomas Gleixner | fe599f9 | 2008-01-30 13:30:26 +0100 | [diff] [blame] | 173 |  | 
| Thomas Gleixner | fe599f9 | 2008-01-30 13:30:26 +0100 | [diff] [blame] | 174 | 	spin_lock_irqsave(&rtc_lock, flags); | 
| Feng Tang | 7bd867d | 2009-09-10 10:48:56 +0800 | [diff] [blame] | 175 | 	retval = x86_platform.set_wallclock(now.tv_sec); | 
| Thomas Gleixner | fe599f9 | 2008-01-30 13:30:26 +0100 | [diff] [blame] | 176 | 	spin_unlock_irqrestore(&rtc_lock, flags); | 
 | 177 |  | 
 | 178 | 	return retval; | 
 | 179 | } | 
 | 180 |  | 
 | 181 | /* not static: needed by APM */ | 
| Martin Schwidefsky | d4f587c | 2009-08-14 15:47:31 +0200 | [diff] [blame] | 182 | void read_persistent_clock(struct timespec *ts) | 
| Thomas Gleixner | fe599f9 | 2008-01-30 13:30:26 +0100 | [diff] [blame] | 183 | { | 
| Thomas Gleixner | 1122b13 | 2008-01-30 13:30:27 +0100 | [diff] [blame] | 184 | 	unsigned long retval, flags; | 
| Thomas Gleixner | fe599f9 | 2008-01-30 13:30:26 +0100 | [diff] [blame] | 185 |  | 
 | 186 | 	spin_lock_irqsave(&rtc_lock, flags); | 
| Feng Tang | 7bd867d | 2009-09-10 10:48:56 +0800 | [diff] [blame] | 187 | 	retval = x86_platform.get_wallclock(); | 
| Thomas Gleixner | fe599f9 | 2008-01-30 13:30:26 +0100 | [diff] [blame] | 188 | 	spin_unlock_irqrestore(&rtc_lock, flags); | 
 | 189 |  | 
| Martin Schwidefsky | d4f587c | 2009-08-14 15:47:31 +0200 | [diff] [blame] | 190 | 	ts->tv_sec = retval; | 
 | 191 | 	ts->tv_nsec = 0; | 
| Thomas Gleixner | fe599f9 | 2008-01-30 13:30:26 +0100 | [diff] [blame] | 192 | } | 
 | 193 |  | 
| Ingo Molnar | 92767af | 2008-01-30 13:32:40 +0100 | [diff] [blame] | 194 | unsigned long long native_read_tsc(void) | 
| Ingo Molnar | cdc7957 | 2008-01-30 13:32:39 +0100 | [diff] [blame] | 195 | { | 
| Ingo Molnar | 92767af | 2008-01-30 13:32:40 +0100 | [diff] [blame] | 196 | 	return __native_read_tsc(); | 
| Ingo Molnar | cdc7957 | 2008-01-30 13:32:39 +0100 | [diff] [blame] | 197 | } | 
| Ingo Molnar | 92767af | 2008-01-30 13:32:40 +0100 | [diff] [blame] | 198 | EXPORT_SYMBOL(native_read_tsc); | 
 | 199 |  | 
| Stas Sergeev | 1da2e3d | 2008-06-12 15:21:54 -0700 | [diff] [blame] | 200 |  | 
 | 201 | static struct resource rtc_resources[] = { | 
 | 202 | 	[0] = { | 
 | 203 | 		.start	= RTC_PORT(0), | 
 | 204 | 		.end	= RTC_PORT(1), | 
 | 205 | 		.flags	= IORESOURCE_IO, | 
 | 206 | 	}, | 
 | 207 | 	[1] = { | 
 | 208 | 		.start	= RTC_IRQ, | 
 | 209 | 		.end	= RTC_IRQ, | 
 | 210 | 		.flags	= IORESOURCE_IRQ, | 
 | 211 | 	} | 
 | 212 | }; | 
 | 213 |  | 
 | 214 | static struct platform_device rtc_device = { | 
 | 215 | 	.name		= "rtc_cmos", | 
 | 216 | 	.id		= -1, | 
 | 217 | 	.resource	= rtc_resources, | 
 | 218 | 	.num_resources	= ARRAY_SIZE(rtc_resources), | 
 | 219 | }; | 
 | 220 |  | 
 | 221 | static __init int add_rtc_cmos(void) | 
 | 222 | { | 
 | 223 | #ifdef CONFIG_PNP | 
| Bjorn Helgaas | 758a7f7 | 2008-10-14 17:01:03 -0600 | [diff] [blame] | 224 | 	static const char *ids[] __initconst = | 
 | 225 | 	    { "PNP0b00", "PNP0b01", "PNP0b02", }; | 
 | 226 | 	struct pnp_dev *dev; | 
 | 227 | 	struct pnp_id *id; | 
 | 228 | 	int i; | 
 | 229 |  | 
 | 230 | 	pnp_for_each_dev(dev) { | 
 | 231 | 		for (id = dev->id; id; id = id->next) { | 
 | 232 | 			for (i = 0; i < ARRAY_SIZE(ids); i++) { | 
 | 233 | 				if (compare_pnp_id(id, ids[i]) != 0) | 
 | 234 | 					return 0; | 
 | 235 | 			} | 
 | 236 | 		} | 
 | 237 | 	} | 
 | 238 | #endif | 
 | 239 |  | 
| Stas Sergeev | 1da2e3d | 2008-06-12 15:21:54 -0700 | [diff] [blame] | 240 | 	platform_device_register(&rtc_device); | 
| Bjorn Helgaas | 758a7f7 | 2008-10-14 17:01:03 -0600 | [diff] [blame] | 241 | 	dev_info(&rtc_device.dev, | 
 | 242 | 		 "registered platform RTC device (no PNP device found)\n"); | 
| Jaswinder Singh Rajput | 8383d82 | 2009-03-21 16:56:37 +0530 | [diff] [blame] | 243 |  | 
| Stas Sergeev | 1da2e3d | 2008-06-12 15:21:54 -0700 | [diff] [blame] | 244 | 	return 0; | 
 | 245 | } | 
 | 246 | device_initcall(add_rtc_cmos); |