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