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