2 * (C) Dominik Brodowski <linux@brodo.de> 2003
4 * Driver to use the Power Management Timer (PMTMR) available in some
5 * southbridges as primary timing source for the Linux kernel.
7 * Based on parts of linux/drivers/acpi/hardware/hwtimer.c, timer_pit.c,
8 * timer_hpet.c, and on Arjan van de Ven's implementation for 2.4.
10 * This file is licensed under the GPL v2.
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/device.h>
17 #include <linux/init.h>
18 #include <linux/pci.h>
19 #include <asm/types.h>
20 #include <asm/timer.h>
23 #include <asm/arch_hooks.h>
25 #include <linux/timex.h>
26 #include "mach_timer.h"
28 /* Number of PMTMR ticks expected during calibration run */
29 #define PMTMR_TICKS_PER_SEC 3579545
30 #define PMTMR_EXPECTED_RATE \
31 ((CALIBRATE_LATCH * (PMTMR_TICKS_PER_SEC >> 10)) / (CLOCK_TICK_RATE>>10))
34 /* The I/O port the PMTMR resides at.
35 * The location is detected during setup_arch(),
36 * in arch/i386/acpi/boot.c */
40 /* value of the Power timer at last timer interrupt */
41 static u32 offset_tick;
42 static u32 offset_delay;
44 static unsigned long long monotonic_base;
45 static seqlock_t monotonic_lock = SEQLOCK_UNLOCKED;
47 #define ACPI_PM_MASK 0xFFFFFF /* limit it to 24 bits */
49 static int pmtmr_need_workaround __read_mostly = 1;
51 /*helper function to safely read acpi pm timesource*/
52 static inline u32 read_pmtmr(void)
54 if (pmtmr_need_workaround) {
57 /* It has been reported that because of various broken
58 * chipsets (ICH4, PIIX4 and PIIX4E) where the ACPI PM time
59 * source is not latched, so you must read it multiple
60 * times to insure a safe value is read.
63 v1 = inl(pmtmr_ioport);
64 v2 = inl(pmtmr_ioport);
65 v3 = inl(pmtmr_ioport);
66 } while ((v1 > v2 && v1 < v3) || (v2 > v3 && v2 < v1)
67 || (v3 > v1 && v3 < v2));
69 /* mask the output to 24 bits */
70 return v2 & ACPI_PM_MASK;
73 return inl(pmtmr_ioport) & ACPI_PM_MASK;
78 * Some boards have the PMTMR running way too fast. We check
79 * the PMTMR rate against PIT channel 2 to catch these cases.
81 static int verify_pmtmr_rate(void)
84 unsigned long count, delta;
86 mach_prepare_counter();
87 value1 = read_pmtmr();
89 value2 = read_pmtmr();
90 delta = (value2 - value1) & ACPI_PM_MASK;
92 /* Check that the PMTMR delta is within 5% of what we expect */
93 if (delta < (PMTMR_EXPECTED_RATE * 19) / 20 ||
94 delta > (PMTMR_EXPECTED_RATE * 21) / 20) {
95 printk(KERN_INFO "PM-Timer running at invalid rate: %lu%% of normal - aborting.\n", 100UL * delta / PMTMR_EXPECTED_RATE);
103 static int init_pmtmr(char* override)
108 if (override[0] && strncmp(override,"pmtmr",5))
114 /* we use the TSC for delay_pmtmr, so make sure it exists */
118 /* "verify" this timing source */
119 value1 = read_pmtmr();
120 for (i = 0; i < 10000; i++) {
121 value2 = read_pmtmr();
122 if (value2 == value1)
126 if ((value2 < value1) && ((value2) < 0xFFF))
128 printk(KERN_INFO "PM-Timer had inconsistent results: 0x%#x, 0x%#x - aborting.\n", value1, value2);
131 printk(KERN_INFO "PM-Timer had no reasonable result: 0x%#x - aborting.\n", value1);
135 if (verify_pmtmr_rate() != 0)
142 static inline u32 cyc2us(u32 cycles)
144 /* The Power Management Timer ticks at 3.579545 ticks per microsecond.
145 * 1 / PM_TIMER_FREQUENCY == 0.27936511 =~ 286/1024 [error: 0.024%]
147 * Even with HZ = 100, delta is at maximum 35796 ticks, so it can
148 * easily be multiplied with 286 (=0x11E) without having to fear
152 return (cycles >> 10);
156 * this gets called during each timer interrupt
157 * - Called while holding the writer xtime_lock
159 static void mark_offset_pmtmr(void)
161 u32 lost, delta, last_offset;
162 static int first_run = 1;
163 last_offset = offset_tick;
165 write_seqlock(&monotonic_lock);
167 offset_tick = read_pmtmr();
169 /* calculate tick interval */
170 delta = (offset_tick - last_offset) & ACPI_PM_MASK;
172 /* convert to usecs */
173 delta = cyc2us(delta);
175 /* update the monotonic base value */
176 monotonic_base += delta * NSEC_PER_USEC;
177 write_sequnlock(&monotonic_lock);
179 /* convert to ticks */
180 delta += offset_delay;
181 lost = delta / (USEC_PER_SEC / HZ);
182 offset_delay = delta % (USEC_PER_SEC / HZ);
185 /* compensate for lost ticks */
187 jiffies_64 += lost - 1;
189 /* don't calculate delay for first run,
190 or if we've got less then a tick */
191 if (first_run || (lost < 1)) {
197 static int pmtmr_resume(void)
199 write_seqlock(&monotonic_lock);
200 /* Assume this is the last mark offset time */
201 offset_tick = read_pmtmr();
202 write_sequnlock(&monotonic_lock);
206 static unsigned long long monotonic_clock_pmtmr(void)
208 u32 last_offset, this_offset;
209 unsigned long long base, ret;
213 /* atomically read monotonic base & last_offset */
215 seq = read_seqbegin(&monotonic_lock);
216 last_offset = offset_tick;
217 base = monotonic_base;
218 } while (read_seqretry(&monotonic_lock, seq));
221 this_offset = read_pmtmr();
223 /* convert to nanoseconds */
224 ret = (this_offset - last_offset) & ACPI_PM_MASK;
225 ret = base + (cyc2us(ret) * NSEC_PER_USEC);
229 static void delay_pmtmr(unsigned long loops)
231 unsigned long bclock, now;
238 } while ((now-bclock) < loops);
243 * get the offset (in microseconds) from the last call to mark_offset()
244 * - Called holding a reader xtime_lock
246 static unsigned long get_offset_pmtmr(void)
248 u32 now, offset, delta = 0;
250 offset = offset_tick;
252 delta = (now - offset)&ACPI_PM_MASK;
254 return (unsigned long) offset_delay + cyc2us(delta);
258 /* acpi timer_opts struct */
259 static struct timer_opts timer_pmtmr = {
261 .mark_offset = mark_offset_pmtmr,
262 .get_offset = get_offset_pmtmr,
263 .monotonic_clock = monotonic_clock_pmtmr,
264 .delay = delay_pmtmr,
265 .read_timer = read_timer_tsc,
266 .resume = pmtmr_resume,
269 struct init_timer_opts __initdata timer_pmtmr_init = {
271 .opts = &timer_pmtmr,
278 * The power management timer may return improper results when read.
279 * Although the timer value settles properly after incrementing,
280 * while incrementing there is a 3 ns window every 69.8 ns where the
281 * timer value is indeterminate (a 4.2% chance that the data will be
282 * incorrect when read). As a result, the ACPI free running count up
283 * timer specification is violated due to erroneous reads.
285 static int __init pmtmr_bug_check(void)
287 static struct pci_device_id gray_list[] __initdata = {
288 /* these chipsets may have bug. */
289 { PCI_DEVICE(PCI_VENDOR_ID_INTEL,
290 PCI_DEVICE_ID_INTEL_82801DB_0) },
294 int pmtmr_has_bug = 0;
297 if (cur_timer != &timer_pmtmr || !pmtmr_need_workaround)
300 dev = pci_get_device(PCI_VENDOR_ID_INTEL,
301 PCI_DEVICE_ID_INTEL_82371AB_3, NULL);
303 pci_read_config_byte(dev, PCI_REVISION_ID, &rev);
304 /* the bug has been fixed in PIIX4M */
306 printk(KERN_WARNING "* Found PM-Timer Bug on this "
307 "chipset. Due to workarounds for a bug,\n"
308 "* this time source is slow. Consider trying "
309 "other time sources (clock=)\n");
315 if (pci_dev_present(gray_list)) {
316 printk(KERN_WARNING "* This chipset may have PM-Timer Bug. Due"
317 " to workarounds for a bug,\n"
318 "* this time source is slow. If you are sure your timer"
320 "* this bug, please use \"pmtmr_good\" to disable the "
326 pmtmr_need_workaround = 0;
330 device_initcall(pmtmr_bug_check);
333 static int __init pmtr_good_setup(char *__str)
335 pmtmr_need_workaround = 0;
338 __setup("pmtmr_good", pmtr_good_setup);
340 MODULE_LICENSE("GPL");
341 MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");
342 MODULE_DESCRIPTION("Power Management Timer (PMTMR) as primary timing source for x86");