Russell King | 89c0b8e | 2011-05-08 18:47:58 +0100 | [diff] [blame] | 1 | /* |
| 2 | * i8253 PIT clocksource |
| 3 | */ |
| 4 | #include <linux/clocksource.h> |
| 5 | #include <linux/init.h> |
| 6 | #include <linux/io.h> |
| 7 | #include <linux/spinlock.h> |
| 8 | #include <linux/timex.h> |
Ralf Baechle | 15f304b | 2011-06-01 19:04:59 +0100 | [diff] [blame^] | 9 | #include <linux/module.h> |
Ralf Baechle | 334955e | 2011-06-01 19:04:57 +0100 | [diff] [blame] | 10 | #include <linux/i8253.h> |
Russell King | 89c0b8e | 2011-05-08 18:47:58 +0100 | [diff] [blame] | 11 | |
| 12 | /* |
Ralf Baechle | 15f304b | 2011-06-01 19:04:59 +0100 | [diff] [blame^] | 13 | * Protects access to I/O ports |
| 14 | * |
| 15 | * 0040-0043 : timer0, i8253 / i8254 |
| 16 | * 0061-0061 : NMI Control Register which contains two speaker control bits. |
| 17 | */ |
| 18 | DEFINE_RAW_SPINLOCK(i8253_lock); |
| 19 | EXPORT_SYMBOL(i8253_lock); |
| 20 | |
| 21 | #ifdef CONFIG_CLKSRC_I8253 |
| 22 | /* |
Russell King | 89c0b8e | 2011-05-08 18:47:58 +0100 | [diff] [blame] | 23 | * Since the PIT overflows every tick, its not very useful |
| 24 | * to just read by itself. So use jiffies to emulate a free |
| 25 | * running counter: |
| 26 | */ |
| 27 | static cycle_t i8253_read(struct clocksource *cs) |
| 28 | { |
| 29 | static int old_count; |
| 30 | static u32 old_jifs; |
| 31 | unsigned long flags; |
| 32 | int count; |
| 33 | u32 jifs; |
| 34 | |
| 35 | raw_spin_lock_irqsave(&i8253_lock, flags); |
| 36 | /* |
| 37 | * Although our caller may have the read side of xtime_lock, |
| 38 | * this is now a seqlock, and we are cheating in this routine |
| 39 | * by having side effects on state that we cannot undo if |
| 40 | * there is a collision on the seqlock and our caller has to |
| 41 | * retry. (Namely, old_jifs and old_count.) So we must treat |
| 42 | * jiffies as volatile despite the lock. We read jiffies |
| 43 | * before latching the timer count to guarantee that although |
| 44 | * the jiffies value might be older than the count (that is, |
| 45 | * the counter may underflow between the last point where |
| 46 | * jiffies was incremented and the point where we latch the |
| 47 | * count), it cannot be newer. |
| 48 | */ |
| 49 | jifs = jiffies; |
| 50 | outb_pit(0x00, PIT_MODE); /* latch the count ASAP */ |
| 51 | count = inb_pit(PIT_CH0); /* read the latched count */ |
| 52 | count |= inb_pit(PIT_CH0) << 8; |
| 53 | |
| 54 | /* VIA686a test code... reset the latch if count > max + 1 */ |
| 55 | if (count > LATCH) { |
| 56 | outb_pit(0x34, PIT_MODE); |
| 57 | outb_pit(PIT_LATCH & 0xff, PIT_CH0); |
| 58 | outb_pit(PIT_LATCH >> 8, PIT_CH0); |
| 59 | count = PIT_LATCH - 1; |
| 60 | } |
| 61 | |
| 62 | /* |
| 63 | * It's possible for count to appear to go the wrong way for a |
| 64 | * couple of reasons: |
| 65 | * |
| 66 | * 1. The timer counter underflows, but we haven't handled the |
| 67 | * resulting interrupt and incremented jiffies yet. |
| 68 | * 2. Hardware problem with the timer, not giving us continuous time, |
| 69 | * the counter does small "jumps" upwards on some Pentium systems, |
| 70 | * (see c't 95/10 page 335 for Neptun bug.) |
| 71 | * |
| 72 | * Previous attempts to handle these cases intelligently were |
| 73 | * buggy, so we just do the simple thing now. |
| 74 | */ |
| 75 | if (count > old_count && jifs == old_jifs) |
| 76 | count = old_count; |
| 77 | |
| 78 | old_count = count; |
| 79 | old_jifs = jifs; |
| 80 | |
| 81 | raw_spin_unlock_irqrestore(&i8253_lock, flags); |
| 82 | |
| 83 | count = (PIT_LATCH - 1) - count; |
| 84 | |
| 85 | return (cycle_t)(jifs * PIT_LATCH) + count; |
| 86 | } |
| 87 | |
| 88 | static struct clocksource i8253_cs = { |
| 89 | .name = "pit", |
| 90 | .rating = 110, |
| 91 | .read = i8253_read, |
| 92 | .mask = CLOCKSOURCE_MASK(32), |
| 93 | }; |
| 94 | |
| 95 | int __init clocksource_i8253_init(void) |
| 96 | { |
| 97 | return clocksource_register_hz(&i8253_cs, PIT_TICK_RATE); |
| 98 | } |
Ralf Baechle | 15f304b | 2011-06-01 19:04:59 +0100 | [diff] [blame^] | 99 | #endif |