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 <asm/types.h>
19 #include <asm/timer.h>
22 #include <asm/arch_hooks.h>
25 /* The I/O port the PMTMR resides at.
26 * The location is detected during setup_arch(),
27 * in arch/i386/acpi/boot.c */
31 /* value of the Power timer at last timer interrupt */
32 static u32 offset_tick;
33 static u32 offset_delay;
35 static unsigned long long monotonic_base;
36 static seqlock_t monotonic_lock = SEQLOCK_UNLOCKED;
38 #define ACPI_PM_MASK 0xFFFFFF /* limit it to 24 bits */
40 /*helper function to safely read acpi pm timesource*/
41 static inline u32 read_pmtmr(void)
44 /* It has been reported that because of various broken
45 * chipsets (ICH4, PIIX4 and PIIX4E) where the ACPI PM time
46 * source is not latched, so you must read it multiple
47 * times to insure a safe value is read.
50 v1 = inl(pmtmr_ioport);
51 v2 = inl(pmtmr_ioport);
52 v3 = inl(pmtmr_ioport);
53 } while ((v1 > v2 && v1 < v3) || (v2 > v3 && v2 < v1)
54 || (v3 > v1 && v3 < v2));
56 /* mask the output to 24 bits */
57 return v2 & ACPI_PM_MASK;
60 static int init_pmtmr(char* override)
65 if (override[0] && strncmp(override,"pmtmr",5))
71 /* we use the TSC for delay_pmtmr, so make sure it exists */
75 /* "verify" this timing source */
76 value1 = read_pmtmr();
77 for (i = 0; i < 10000; i++) {
78 value2 = read_pmtmr();
83 if ((value2 < value1) && ((value2) < 0xFFF))
85 printk(KERN_INFO "PM-Timer had inconsistent results: 0x%#x, 0x%#x - aborting.\n", value1, value2);
88 printk(KERN_INFO "PM-Timer had no reasonable result: 0x%#x - aborting.\n", value1);
96 static inline u32 cyc2us(u32 cycles)
98 /* The Power Management Timer ticks at 3.579545 ticks per microsecond.
99 * 1 / PM_TIMER_FREQUENCY == 0.27936511 =~ 286/1024 [error: 0.024%]
101 * Even with HZ = 100, delta is at maximum 35796 ticks, so it can
102 * easily be multiplied with 286 (=0x11E) without having to fear
106 return (cycles >> 10);
110 * this gets called during each timer interrupt
111 * - Called while holding the writer xtime_lock
113 static void mark_offset_pmtmr(void)
115 u32 lost, delta, last_offset;
116 static int first_run = 1;
117 last_offset = offset_tick;
119 write_seqlock(&monotonic_lock);
121 offset_tick = read_pmtmr();
123 /* calculate tick interval */
124 delta = (offset_tick - last_offset) & ACPI_PM_MASK;
126 /* convert to usecs */
127 delta = cyc2us(delta);
129 /* update the monotonic base value */
130 monotonic_base += delta * NSEC_PER_USEC;
131 write_sequnlock(&monotonic_lock);
133 /* convert to ticks */
134 delta += offset_delay;
135 lost = delta / (USEC_PER_SEC / HZ);
136 offset_delay = delta % (USEC_PER_SEC / HZ);
139 /* compensate for lost ticks */
141 jiffies_64 += lost - 1;
143 /* don't calculate delay for first run,
144 or if we've got less then a tick */
145 if (first_run || (lost < 1)) {
152 static unsigned long long monotonic_clock_pmtmr(void)
154 u32 last_offset, this_offset;
155 unsigned long long base, ret;
159 /* atomically read monotonic base & last_offset */
161 seq = read_seqbegin(&monotonic_lock);
162 last_offset = offset_tick;
163 base = monotonic_base;
164 } while (read_seqretry(&monotonic_lock, seq));
167 this_offset = read_pmtmr();
169 /* convert to nanoseconds */
170 ret = (this_offset - last_offset) & ACPI_PM_MASK;
171 ret = base + (cyc2us(ret) * NSEC_PER_USEC);
175 static void delay_pmtmr(unsigned long loops)
177 unsigned long bclock, now;
184 } while ((now-bclock) < loops);
189 * get the offset (in microseconds) from the last call to mark_offset()
190 * - Called holding a reader xtime_lock
192 static unsigned long get_offset_pmtmr(void)
194 u32 now, offset, delta = 0;
196 offset = offset_tick;
198 delta = (now - offset)&ACPI_PM_MASK;
200 return (unsigned long) offset_delay + cyc2us(delta);
204 /* acpi timer_opts struct */
205 struct timer_opts timer_pmtmr = {
208 .mark_offset = mark_offset_pmtmr,
209 .get_offset = get_offset_pmtmr,
210 .monotonic_clock = monotonic_clock_pmtmr,
211 .delay = delay_pmtmr,
215 MODULE_LICENSE("GPL");
216 MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");
217 MODULE_DESCRIPTION("Power Management Timer (PMTMR) as primary timing source for x86");