| 
									
										
										
										
											2006-06-26 00:25:12 -07:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * linux/drivers/clocksource/acpi_pm.c | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This file contains the ACPI PM based clocksource. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This code was largely moved from the i386 timer_pm.c file | 
					
						
							|  |  |  |  * which was (C) Dominik Brodowski <linux@brodo.de> 2003 | 
					
						
							|  |  |  |  * and contained the following comments: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Driver to use the Power Management Timer (PMTMR) available in some | 
					
						
							|  |  |  |  * southbridges as primary timing source for the Linux kernel. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Based on parts of linux/drivers/acpi/hardware/hwtimer.c, timer_pit.c, | 
					
						
							|  |  |  |  * timer_hpet.c, and on Arjan van de Ven's implementation for 2.4. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This file is licensed under the GPL v2. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-02-16 01:27:57 -08:00
										 |  |  | #include <linux/acpi_pmtmr.h>
 | 
					
						
							| 
									
										
										
										
											2006-06-26 00:25:12 -07:00
										 |  |  | #include <linux/clocksource.h>
 | 
					
						
							| 
									
										
										
										
											2009-06-16 15:31:12 -07:00
										 |  |  | #include <linux/timex.h>
 | 
					
						
							| 
									
										
										
										
											2006-06-26 00:25:12 -07:00
										 |  |  | #include <linux/errno.h>
 | 
					
						
							|  |  |  | #include <linux/init.h>
 | 
					
						
							|  |  |  | #include <linux/pci.h>
 | 
					
						
							| 
									
										
										
										
											2008-09-05 14:05:35 -07:00
										 |  |  | #include <linux/delay.h>
 | 
					
						
							| 
									
										
										
										
											2006-06-26 00:25:12 -07:00
										 |  |  | #include <asm/io.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * The I/O port the PMTMR resides at. | 
					
						
							|  |  |  |  * The location is detected during setup_arch(), | 
					
						
							| 
									
										
										
										
											2007-04-25 14:27:06 -04:00
										 |  |  |  * in arch/i386/kernel/acpi/boot.c | 
					
						
							| 
									
										
										
										
											2006-06-26 00:25:12 -07:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2006-06-26 00:25:14 -07:00
										 |  |  | u32 pmtmr_ioport __read_mostly; | 
					
						
							| 
									
										
										
										
											2006-06-26 00:25:12 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | static inline u32 read_pmtmr(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	/* mask the output to 24 bits */ | 
					
						
							|  |  |  | 	return inl(pmtmr_ioport) & ACPI_PM_MASK; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-02-16 01:27:57 -08:00
										 |  |  | u32 acpi_pm_read_verified(void) | 
					
						
							| 
									
										
										
										
											2006-06-26 00:25:12 -07:00
										 |  |  | { | 
					
						
							|  |  |  | 	u32 v1 = 0, v2 = 0, v3 = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/*
 | 
					
						
							|  |  |  | 	 * It has been reported that because of various broken | 
					
						
							|  |  |  | 	 * chipsets (ICH4, PIIX4 and PIIX4E) where the ACPI PM clock | 
					
						
							| 
									
										
										
										
											2006-06-26 00:25:14 -07:00
										 |  |  | 	 * source is not latched, you must read it multiple | 
					
						
							| 
									
										
										
										
											2006-06-26 00:25:12 -07:00
										 |  |  | 	 * times to ensure a safe value is read: | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	do { | 
					
						
							|  |  |  | 		v1 = read_pmtmr(); | 
					
						
							|  |  |  | 		v2 = read_pmtmr(); | 
					
						
							|  |  |  | 		v3 = read_pmtmr(); | 
					
						
							| 
									
										
										
										
											2006-10-21 10:24:10 -07:00
										 |  |  | 	} while (unlikely((v1 > v2 && v1 < v3) || (v2 > v3 && v2 < v1) | 
					
						
							|  |  |  | 			  || (v3 > v1 && v3 < v2))); | 
					
						
							| 
									
										
										
										
											2006-06-26 00:25:12 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-02-16 01:27:57 -08:00
										 |  |  | 	return v2; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-21 12:24:00 -07:00
										 |  |  | static cycle_t acpi_pm_read(struct clocksource *cs) | 
					
						
							| 
									
										
										
										
											2006-06-26 00:25:12 -07:00
										 |  |  | { | 
					
						
							|  |  |  | 	return (cycle_t)read_pmtmr(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct clocksource clocksource_acpi_pm = { | 
					
						
							|  |  |  | 	.name		= "acpi_pm", | 
					
						
							|  |  |  | 	.rating		= 200, | 
					
						
							|  |  |  | 	.read		= acpi_pm_read, | 
					
						
							|  |  |  | 	.mask		= (cycle_t)ACPI_PM_MASK, | 
					
						
							| 
									
										
										
										
											2007-02-16 01:27:36 -08:00
										 |  |  | 	.flags		= CLOCK_SOURCE_IS_CONTINUOUS, | 
					
						
							| 
									
										
										
										
											2006-06-26 00:25:12 -07:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef CONFIG_PCI
 | 
					
						
							| 
									
										
										
										
											2012-12-21 15:11:38 -08:00
										 |  |  | static int acpi_pm_good; | 
					
						
							| 
									
										
										
										
											2006-06-26 00:25:12 -07:00
										 |  |  | static int __init acpi_pm_good_setup(char *__str) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2006-12-10 02:21:33 -08:00
										 |  |  | 	acpi_pm_good = 1; | 
					
						
							|  |  |  | 	return 1; | 
					
						
							| 
									
										
										
										
											2006-06-26 00:25:12 -07:00
										 |  |  | } | 
					
						
							|  |  |  | __setup("acpi_pm_good", acpi_pm_good_setup); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-21 12:24:00 -07:00
										 |  |  | static cycle_t acpi_pm_read_slow(struct clocksource *cs) | 
					
						
							| 
									
										
										
										
											2008-12-01 14:18:12 -08:00
										 |  |  | { | 
					
						
							|  |  |  | 	return (cycle_t)acpi_pm_read_verified(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-06-26 00:25:12 -07:00
										 |  |  | static inline void acpi_pm_need_workaround(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2007-02-16 01:27:57 -08:00
										 |  |  | 	clocksource_acpi_pm.read = acpi_pm_read_slow; | 
					
						
							| 
									
										
										
										
											2007-03-26 21:32:19 -08:00
										 |  |  | 	clocksource_acpi_pm.rating = 120; | 
					
						
							| 
									
										
										
										
											2006-06-26 00:25:12 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * PIIX4 Errata: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * The power management timer may return improper results when read. | 
					
						
							|  |  |  |  * Although the timer value settles properly after incrementing, | 
					
						
							|  |  |  |  * while incrementing there is a 3 ns window every 69.8 ns where the | 
					
						
							|  |  |  |  * timer value is indeterminate (a 4.2% chance that the data will be | 
					
						
							|  |  |  |  * incorrect when read). As a result, the ACPI free running count up | 
					
						
							|  |  |  |  * timer specification is violated due to erroneous reads. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2012-12-21 15:11:38 -08:00
										 |  |  | static void acpi_pm_check_blacklist(struct pci_dev *dev) | 
					
						
							| 
									
										
										
										
											2006-06-26 00:25:12 -07:00
										 |  |  | { | 
					
						
							|  |  |  | 	if (acpi_pm_good) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* the bug has been fixed in PIIX4M */ | 
					
						
							| 
									
										
										
										
											2007-06-08 15:46:36 -07:00
										 |  |  | 	if (dev->revision < 3) { | 
					
						
							| 
									
										
										
										
											2006-06-26 00:25:12 -07:00
										 |  |  | 		printk(KERN_WARNING "* Found PM-Timer Bug on the chipset." | 
					
						
							|  |  |  | 		       " Due to workarounds for a bug,\n" | 
					
						
							|  |  |  | 		       "* this clock source is slow. Consider trying" | 
					
						
							|  |  |  | 		       " other clock sources\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		acpi_pm_need_workaround(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82371AB_3, | 
					
						
							|  |  |  | 			acpi_pm_check_blacklist); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-12-21 15:11:38 -08:00
										 |  |  | static void acpi_pm_check_graylist(struct pci_dev *dev) | 
					
						
							| 
									
										
										
										
											2006-06-26 00:25:12 -07:00
										 |  |  | { | 
					
						
							|  |  |  | 	if (acpi_pm_good) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	printk(KERN_WARNING "* The chipset may have PM-Timer Bug. Due to" | 
					
						
							|  |  |  | 	       " workarounds for a bug,\n" | 
					
						
							|  |  |  | 	       "* this clock source is slow. If you are sure your timer" | 
					
						
							|  |  |  | 	       " does not have\n" | 
					
						
							|  |  |  | 	       "* this bug, please use \"acpi_pm_good\" to disable the" | 
					
						
							|  |  |  | 	       " workaround\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	acpi_pm_need_workaround(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82801DB_0, | 
					
						
							|  |  |  | 			acpi_pm_check_graylist); | 
					
						
							| 
									
										
										
										
											2006-10-21 10:24:10 -07:00
										 |  |  | DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_SERVERWORKS, PCI_DEVICE_ID_SERVERWORKS_LE, | 
					
						
							|  |  |  | 			acpi_pm_check_graylist); | 
					
						
							| 
									
										
										
										
											2006-06-26 00:25:12 -07:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-12-08 02:36:02 -08:00
										 |  |  | #ifndef CONFIG_X86_64
 | 
					
						
							| 
									
										
										
										
											2009-01-28 19:34:09 +01:00
										 |  |  | #include <asm/mach_timer.h>
 | 
					
						
							| 
									
										
										
										
											2006-12-08 02:36:02 -08:00
										 |  |  | #define PMTMR_EXPECTED_RATE \
 | 
					
						
							| 
									
										
										
										
											2011-11-01 14:25:01 -07:00
										 |  |  |   ((CALIBRATE_LATCH * (PMTMR_TICKS_PER_SEC >> 10)) / (PIT_TICK_RATE>>10)) | 
					
						
							| 
									
										
										
										
											2006-12-08 02:36:02 -08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Some boards have the PMTMR running way too fast. We check | 
					
						
							|  |  |  |  * the PMTMR rate against PIT channel 2 to catch these cases. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static int verify_pmtmr_rate(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2008-09-05 14:05:33 -07:00
										 |  |  | 	cycle_t value1, value2; | 
					
						
							| 
									
										
										
										
											2006-12-08 02:36:02 -08:00
										 |  |  | 	unsigned long count, delta; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	mach_prepare_counter(); | 
					
						
							| 
									
										
										
										
											2009-04-21 12:24:00 -07:00
										 |  |  | 	value1 = clocksource_acpi_pm.read(&clocksource_acpi_pm); | 
					
						
							| 
									
										
										
										
											2006-12-08 02:36:02 -08:00
										 |  |  | 	mach_countup(&count); | 
					
						
							| 
									
										
										
										
											2009-04-21 12:24:00 -07:00
										 |  |  | 	value2 = clocksource_acpi_pm.read(&clocksource_acpi_pm); | 
					
						
							| 
									
										
										
										
											2006-12-08 02:36:02 -08:00
										 |  |  | 	delta = (value2 - value1) & ACPI_PM_MASK; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Check that the PMTMR delta is within 5% of what we expect */ | 
					
						
							|  |  |  | 	if (delta < (PMTMR_EXPECTED_RATE * 19) / 20 || | 
					
						
							|  |  |  | 	    delta > (PMTMR_EXPECTED_RATE * 21) / 20) { | 
					
						
							|  |  |  | 		printk(KERN_INFO "PM-Timer running at invalid rate: %lu%% " | 
					
						
							|  |  |  | 			"of normal - aborting.\n", | 
					
						
							|  |  |  | 			100UL * delta / PMTMR_EXPECTED_RATE); | 
					
						
							|  |  |  | 		return -1; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  | #define verify_pmtmr_rate() (0)
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2006-06-26 00:25:12 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-05 14:05:35 -07:00
										 |  |  | /* Number of monotonicity checks to perform during initialization */ | 
					
						
							|  |  |  | #define ACPI_PM_MONOTONICITY_CHECKS 10
 | 
					
						
							| 
									
										
										
										
											2008-09-11 11:09:49 +02:00
										 |  |  | /* Number of reads we try to get two different values */ | 
					
						
							|  |  |  | #define ACPI_PM_READ_CHECKS 10000
 | 
					
						
							| 
									
										
										
										
											2008-09-05 14:05:35 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-11 23:49:16 +02:00
										 |  |  | static int __init init_acpi_pm_clocksource(void) | 
					
						
							| 
									
										
										
										
											2006-06-26 00:25:12 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2008-09-05 14:05:33 -07:00
										 |  |  | 	cycle_t value1, value2; | 
					
						
							| 
									
										
										
										
											2008-09-11 11:09:49 +02:00
										 |  |  | 	unsigned int i, j = 0; | 
					
						
							| 
									
										
										
										
											2006-06-26 00:25:12 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-11 23:49:16 +02:00
										 |  |  | 	if (!pmtmr_ioport) | 
					
						
							|  |  |  | 		return -ENODEV; | 
					
						
							| 
									
										
										
										
											2006-06-26 00:25:12 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* "verify" this timing source: */ | 
					
						
							| 
									
										
										
										
											2008-09-05 14:05:35 -07:00
										 |  |  | 	for (j = 0; j < ACPI_PM_MONOTONICITY_CHECKS; j++) { | 
					
						
							| 
									
										
										
										
											2012-04-11 23:49:16 +02:00
										 |  |  | 		udelay(100 * j); | 
					
						
							| 
									
										
										
										
											2009-04-21 12:24:00 -07:00
										 |  |  | 		value1 = clocksource_acpi_pm.read(&clocksource_acpi_pm); | 
					
						
							| 
									
										
										
										
											2008-09-11 11:09:49 +02:00
										 |  |  | 		for (i = 0; i < ACPI_PM_READ_CHECKS; i++) { | 
					
						
							| 
									
										
										
										
											2009-04-21 12:24:00 -07:00
										 |  |  | 			value2 = clocksource_acpi_pm.read(&clocksource_acpi_pm); | 
					
						
							| 
									
										
										
										
											2008-09-05 14:05:35 -07:00
										 |  |  | 			if (value2 == value1) | 
					
						
							|  |  |  | 				continue; | 
					
						
							|  |  |  | 			if (value2 > value1) | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			if ((value2 < value1) && ((value2) < 0xFFF)) | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			printk(KERN_INFO "PM-Timer had inconsistent results:" | 
					
						
							| 
									
										
										
										
											2013-07-25 11:53:25 -07:00
										 |  |  | 			       " %#llx, %#llx - aborting.\n", | 
					
						
							| 
									
										
										
										
											2008-09-05 14:05:35 -07:00
										 |  |  | 			       value1, value2); | 
					
						
							| 
									
										
										
										
											2011-01-14 09:47:26 -08:00
										 |  |  | 			pmtmr_ioport = 0; | 
					
						
							| 
									
										
										
										
											2012-04-11 23:49:16 +02:00
										 |  |  | 			return -EINVAL; | 
					
						
							| 
									
										
										
										
											2008-09-05 14:05:35 -07:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2008-09-11 11:09:49 +02:00
										 |  |  | 		if (i == ACPI_PM_READ_CHECKS) { | 
					
						
							|  |  |  | 			printk(KERN_INFO "PM-Timer failed consistency check " | 
					
						
							| 
									
										
										
										
											2013-07-25 11:53:25 -07:00
										 |  |  | 			       " (%#llx) - aborting.\n", value1); | 
					
						
							| 
									
										
										
										
											2011-01-14 09:47:26 -08:00
										 |  |  | 			pmtmr_ioport = 0; | 
					
						
							| 
									
										
										
										
											2012-04-11 23:49:16 +02:00
										 |  |  | 			return -ENODEV; | 
					
						
							| 
									
										
										
										
											2008-09-11 11:09:49 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2006-06-26 00:25:12 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-14 09:47:26 -08:00
										 |  |  | 	if (verify_pmtmr_rate() != 0){ | 
					
						
							|  |  |  | 		pmtmr_ioport = 0; | 
					
						
							| 
									
										
										
										
											2012-04-11 23:49:16 +02:00
										 |  |  | 		return -ENODEV; | 
					
						
							| 
									
										
										
										
											2011-01-14 09:47:26 -08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2006-12-08 02:36:02 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-11 23:49:16 +02:00
										 |  |  | 	return clocksource_register_hz(&clocksource_acpi_pm, | 
					
						
							| 
									
										
										
										
											2010-07-13 17:56:27 -07:00
										 |  |  | 						PMTMR_TICKS_PER_SEC); | 
					
						
							| 
									
										
										
										
											2006-06-26 00:25:12 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-03-05 00:30:50 -08:00
										 |  |  | /* We use fs_initcall because we want the PCI fixups to have run
 | 
					
						
							|  |  |  |  * but we still need to load before device_initcall | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | fs_initcall(init_acpi_pm_clocksource); | 
					
						
							| 
									
										
										
										
											2008-05-21 21:14:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Allow an override of the IOPort. Stupid BIOSes do not tell us about | 
					
						
							|  |  |  |  * the PMTimer, but we might know where it is. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static int __init parse_pmtmr(char *arg) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2012-10-20 07:46:02 +03:00
										 |  |  | 	unsigned int base; | 
					
						
							|  |  |  | 	int ret; | 
					
						
							| 
									
										
										
										
											2008-05-21 21:14:58 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-10-20 07:46:02 +03:00
										 |  |  | 	ret = kstrtouint(arg, 16, &base); | 
					
						
							|  |  |  | 	if (ret) | 
					
						
							|  |  |  | 		return ret; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	pr_info("PMTMR IOPort override: 0x%04x -> 0x%04x\n", pmtmr_ioport, | 
					
						
							|  |  |  | 		base); | 
					
						
							| 
									
										
										
										
											2008-05-21 21:14:58 +02:00
										 |  |  | 	pmtmr_ioport = base; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | __setup("pmtmr=", parse_pmtmr); |