1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * i8253 PIT clocksource
4*4882a593Smuzhiyun */
5*4882a593Smuzhiyun #include <linux/clockchips.h>
6*4882a593Smuzhiyun #include <linux/init.h>
7*4882a593Smuzhiyun #include <linux/io.h>
8*4882a593Smuzhiyun #include <linux/spinlock.h>
9*4882a593Smuzhiyun #include <linux/timex.h>
10*4882a593Smuzhiyun #include <linux/module.h>
11*4882a593Smuzhiyun #include <linux/i8253.h>
12*4882a593Smuzhiyun #include <linux/smp.h>
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun /*
15*4882a593Smuzhiyun * Protects access to I/O ports
16*4882a593Smuzhiyun *
17*4882a593Smuzhiyun * 0040-0043 : timer0, i8253 / i8254
18*4882a593Smuzhiyun * 0061-0061 : NMI Control Register which contains two speaker control bits.
19*4882a593Smuzhiyun */
20*4882a593Smuzhiyun DEFINE_RAW_SPINLOCK(i8253_lock);
21*4882a593Smuzhiyun EXPORT_SYMBOL(i8253_lock);
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun /*
24*4882a593Smuzhiyun * Handle PIT quirk in pit_shutdown() where zeroing the counter register
25*4882a593Smuzhiyun * restarts the PIT, negating the shutdown. On platforms with the quirk,
26*4882a593Smuzhiyun * platform specific code can set this to false.
27*4882a593Smuzhiyun */
28*4882a593Smuzhiyun bool i8253_clear_counter_on_shutdown __ro_after_init = true;
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun #ifdef CONFIG_CLKSRC_I8253
31*4882a593Smuzhiyun /*
32*4882a593Smuzhiyun * Since the PIT overflows every tick, its not very useful
33*4882a593Smuzhiyun * to just read by itself. So use jiffies to emulate a free
34*4882a593Smuzhiyun * running counter:
35*4882a593Smuzhiyun */
i8253_read(struct clocksource * cs)36*4882a593Smuzhiyun static u64 i8253_read(struct clocksource *cs)
37*4882a593Smuzhiyun {
38*4882a593Smuzhiyun static int old_count;
39*4882a593Smuzhiyun static u32 old_jifs;
40*4882a593Smuzhiyun unsigned long flags;
41*4882a593Smuzhiyun int count;
42*4882a593Smuzhiyun u32 jifs;
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun raw_spin_lock_irqsave(&i8253_lock, flags);
45*4882a593Smuzhiyun /*
46*4882a593Smuzhiyun * Although our caller may have the read side of jiffies_lock,
47*4882a593Smuzhiyun * this is now a seqlock, and we are cheating in this routine
48*4882a593Smuzhiyun * by having side effects on state that we cannot undo if
49*4882a593Smuzhiyun * there is a collision on the seqlock and our caller has to
50*4882a593Smuzhiyun * retry. (Namely, old_jifs and old_count.) So we must treat
51*4882a593Smuzhiyun * jiffies as volatile despite the lock. We read jiffies
52*4882a593Smuzhiyun * before latching the timer count to guarantee that although
53*4882a593Smuzhiyun * the jiffies value might be older than the count (that is,
54*4882a593Smuzhiyun * the counter may underflow between the last point where
55*4882a593Smuzhiyun * jiffies was incremented and the point where we latch the
56*4882a593Smuzhiyun * count), it cannot be newer.
57*4882a593Smuzhiyun */
58*4882a593Smuzhiyun jifs = jiffies;
59*4882a593Smuzhiyun outb_p(0x00, PIT_MODE); /* latch the count ASAP */
60*4882a593Smuzhiyun count = inb_p(PIT_CH0); /* read the latched count */
61*4882a593Smuzhiyun count |= inb_p(PIT_CH0) << 8;
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun /* VIA686a test code... reset the latch if count > max + 1 */
64*4882a593Smuzhiyun if (count > PIT_LATCH) {
65*4882a593Smuzhiyun outb_p(0x34, PIT_MODE);
66*4882a593Smuzhiyun outb_p(PIT_LATCH & 0xff, PIT_CH0);
67*4882a593Smuzhiyun outb_p(PIT_LATCH >> 8, PIT_CH0);
68*4882a593Smuzhiyun count = PIT_LATCH - 1;
69*4882a593Smuzhiyun }
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun /*
72*4882a593Smuzhiyun * It's possible for count to appear to go the wrong way for a
73*4882a593Smuzhiyun * couple of reasons:
74*4882a593Smuzhiyun *
75*4882a593Smuzhiyun * 1. The timer counter underflows, but we haven't handled the
76*4882a593Smuzhiyun * resulting interrupt and incremented jiffies yet.
77*4882a593Smuzhiyun * 2. Hardware problem with the timer, not giving us continuous time,
78*4882a593Smuzhiyun * the counter does small "jumps" upwards on some Pentium systems,
79*4882a593Smuzhiyun * (see c't 95/10 page 335 for Neptun bug.)
80*4882a593Smuzhiyun *
81*4882a593Smuzhiyun * Previous attempts to handle these cases intelligently were
82*4882a593Smuzhiyun * buggy, so we just do the simple thing now.
83*4882a593Smuzhiyun */
84*4882a593Smuzhiyun if (count > old_count && jifs == old_jifs)
85*4882a593Smuzhiyun count = old_count;
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun old_count = count;
88*4882a593Smuzhiyun old_jifs = jifs;
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun raw_spin_unlock_irqrestore(&i8253_lock, flags);
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun count = (PIT_LATCH - 1) - count;
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun return (u64)(jifs * PIT_LATCH) + count;
95*4882a593Smuzhiyun }
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun static struct clocksource i8253_cs = {
98*4882a593Smuzhiyun .name = "pit",
99*4882a593Smuzhiyun .rating = 110,
100*4882a593Smuzhiyun .read = i8253_read,
101*4882a593Smuzhiyun .mask = CLOCKSOURCE_MASK(32),
102*4882a593Smuzhiyun };
103*4882a593Smuzhiyun
clocksource_i8253_init(void)104*4882a593Smuzhiyun int __init clocksource_i8253_init(void)
105*4882a593Smuzhiyun {
106*4882a593Smuzhiyun return clocksource_register_hz(&i8253_cs, PIT_TICK_RATE);
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun #endif
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun #ifdef CONFIG_CLKEVT_I8253
pit_shutdown(struct clock_event_device * evt)111*4882a593Smuzhiyun static int pit_shutdown(struct clock_event_device *evt)
112*4882a593Smuzhiyun {
113*4882a593Smuzhiyun if (!clockevent_state_oneshot(evt) && !clockevent_state_periodic(evt))
114*4882a593Smuzhiyun return 0;
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun raw_spin_lock(&i8253_lock);
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun outb_p(0x30, PIT_MODE);
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun if (i8253_clear_counter_on_shutdown) {
121*4882a593Smuzhiyun outb_p(0, PIT_CH0);
122*4882a593Smuzhiyun outb_p(0, PIT_CH0);
123*4882a593Smuzhiyun }
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun raw_spin_unlock(&i8253_lock);
126*4882a593Smuzhiyun return 0;
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun
pit_set_oneshot(struct clock_event_device * evt)129*4882a593Smuzhiyun static int pit_set_oneshot(struct clock_event_device *evt)
130*4882a593Smuzhiyun {
131*4882a593Smuzhiyun raw_spin_lock(&i8253_lock);
132*4882a593Smuzhiyun outb_p(0x38, PIT_MODE);
133*4882a593Smuzhiyun raw_spin_unlock(&i8253_lock);
134*4882a593Smuzhiyun return 0;
135*4882a593Smuzhiyun }
136*4882a593Smuzhiyun
pit_set_periodic(struct clock_event_device * evt)137*4882a593Smuzhiyun static int pit_set_periodic(struct clock_event_device *evt)
138*4882a593Smuzhiyun {
139*4882a593Smuzhiyun raw_spin_lock(&i8253_lock);
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun /* binary, mode 2, LSB/MSB, ch 0 */
142*4882a593Smuzhiyun outb_p(0x34, PIT_MODE);
143*4882a593Smuzhiyun outb_p(PIT_LATCH & 0xff, PIT_CH0); /* LSB */
144*4882a593Smuzhiyun outb_p(PIT_LATCH >> 8, PIT_CH0); /* MSB */
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun raw_spin_unlock(&i8253_lock);
147*4882a593Smuzhiyun return 0;
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun /*
151*4882a593Smuzhiyun * Program the next event in oneshot mode
152*4882a593Smuzhiyun *
153*4882a593Smuzhiyun * Delta is given in PIT ticks
154*4882a593Smuzhiyun */
pit_next_event(unsigned long delta,struct clock_event_device * evt)155*4882a593Smuzhiyun static int pit_next_event(unsigned long delta, struct clock_event_device *evt)
156*4882a593Smuzhiyun {
157*4882a593Smuzhiyun raw_spin_lock(&i8253_lock);
158*4882a593Smuzhiyun outb_p(delta & 0xff , PIT_CH0); /* LSB */
159*4882a593Smuzhiyun outb_p(delta >> 8 , PIT_CH0); /* MSB */
160*4882a593Smuzhiyun raw_spin_unlock(&i8253_lock);
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun return 0;
163*4882a593Smuzhiyun }
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun /*
166*4882a593Smuzhiyun * On UP the PIT can serve all of the possible timer functions. On SMP systems
167*4882a593Smuzhiyun * it can be solely used for the global tick.
168*4882a593Smuzhiyun */
169*4882a593Smuzhiyun struct clock_event_device i8253_clockevent = {
170*4882a593Smuzhiyun .name = "pit",
171*4882a593Smuzhiyun .features = CLOCK_EVT_FEAT_PERIODIC,
172*4882a593Smuzhiyun .set_state_shutdown = pit_shutdown,
173*4882a593Smuzhiyun .set_state_periodic = pit_set_periodic,
174*4882a593Smuzhiyun .set_next_event = pit_next_event,
175*4882a593Smuzhiyun };
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun /*
178*4882a593Smuzhiyun * Initialize the conversion factor and the min/max deltas of the clock event
179*4882a593Smuzhiyun * structure and register the clock event source with the framework.
180*4882a593Smuzhiyun */
clockevent_i8253_init(bool oneshot)181*4882a593Smuzhiyun void __init clockevent_i8253_init(bool oneshot)
182*4882a593Smuzhiyun {
183*4882a593Smuzhiyun if (oneshot) {
184*4882a593Smuzhiyun i8253_clockevent.features |= CLOCK_EVT_FEAT_ONESHOT;
185*4882a593Smuzhiyun i8253_clockevent.set_state_oneshot = pit_set_oneshot;
186*4882a593Smuzhiyun }
187*4882a593Smuzhiyun /*
188*4882a593Smuzhiyun * Start pit with the boot cpu mask. x86 might make it global
189*4882a593Smuzhiyun * when it is used as broadcast device later.
190*4882a593Smuzhiyun */
191*4882a593Smuzhiyun i8253_clockevent.cpumask = cpumask_of(smp_processor_id());
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun clockevents_config_and_register(&i8253_clockevent, PIT_TICK_RATE,
194*4882a593Smuzhiyun 0xF, 0x7FFF);
195*4882a593Smuzhiyun }
196*4882a593Smuzhiyun #endif
197