xref: /openbmc/linux/drivers/clocksource/i8253.c (revision 36db6e8484ed455bbb320d89a119378897ae991c)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
289c0b8e2SRussell King /*
389c0b8e2SRussell King  * i8253 PIT clocksource
489c0b8e2SRussell King  */
5e6220bdcSThomas Gleixner #include <linux/clockchips.h>
689c0b8e2SRussell King #include <linux/init.h>
789c0b8e2SRussell King #include <linux/io.h>
889c0b8e2SRussell King #include <linux/spinlock.h>
989c0b8e2SRussell King #include <linux/timex.h>
1015f304b6SRalf Baechle #include <linux/module.h>
11334955efSRalf Baechle #include <linux/i8253.h>
12e6220bdcSThomas Gleixner #include <linux/smp.h>
1389c0b8e2SRussell King 
1489c0b8e2SRussell King /*
1515f304b6SRalf Baechle  * Protects access to I/O ports
1615f304b6SRalf Baechle  *
1715f304b6SRalf Baechle  * 0040-0043 : timer0, i8253 / i8254
1815f304b6SRalf Baechle  * 0061-0061 : NMI Control Register which contains two speaker control bits.
1915f304b6SRalf Baechle  */
2015f304b6SRalf Baechle DEFINE_RAW_SPINLOCK(i8253_lock);
2115f304b6SRalf Baechle EXPORT_SYMBOL(i8253_lock);
2215f304b6SRalf Baechle 
2315f304b6SRalf Baechle #ifdef CONFIG_CLKSRC_I8253
2415f304b6SRalf Baechle /*
2589c0b8e2SRussell King  * Since the PIT overflows every tick, its not very useful
2689c0b8e2SRussell King  * to just read by itself. So use jiffies to emulate a free
2789c0b8e2SRussell King  * running counter:
2889c0b8e2SRussell King  */
29a5a1d1c2SThomas Gleixner static u64 i8253_read(struct clocksource *cs)
3089c0b8e2SRussell King {
3189c0b8e2SRussell King 	static int old_count;
3289c0b8e2SRussell King 	static u32 old_jifs;
3389c0b8e2SRussell King 	unsigned long flags;
3489c0b8e2SRussell King 	int count;
3589c0b8e2SRussell King 	u32 jifs;
i8253_read(struct clocksource * cs)3689c0b8e2SRussell King 
3789c0b8e2SRussell King 	raw_spin_lock_irqsave(&i8253_lock, flags);
3889c0b8e2SRussell King 	/*
39d6ad4187SJohn Stultz 	 * Although our caller may have the read side of jiffies_lock,
4089c0b8e2SRussell King 	 * this is now a seqlock, and we are cheating in this routine
4189c0b8e2SRussell King 	 * by having side effects on state that we cannot undo if
4289c0b8e2SRussell King 	 * there is a collision on the seqlock and our caller has to
4389c0b8e2SRussell King 	 * retry.  (Namely, old_jifs and old_count.)  So we must treat
4489c0b8e2SRussell King 	 * jiffies as volatile despite the lock.  We read jiffies
4589c0b8e2SRussell King 	 * before latching the timer count to guarantee that although
4689c0b8e2SRussell King 	 * the jiffies value might be older than the count (that is,
4789c0b8e2SRussell King 	 * the counter may underflow between the last point where
4889c0b8e2SRussell King 	 * jiffies was incremented and the point where we latch the
4989c0b8e2SRussell King 	 * count), it cannot be newer.
5089c0b8e2SRussell King 	 */
5189c0b8e2SRussell King 	jifs = jiffies;
52e6220bdcSThomas Gleixner 	outb_p(0x00, PIT_MODE);	/* latch the count ASAP */
53e6220bdcSThomas Gleixner 	count = inb_p(PIT_CH0);	/* read the latched count */
54e6220bdcSThomas Gleixner 	count |= inb_p(PIT_CH0) << 8;
5589c0b8e2SRussell King 
5689c0b8e2SRussell King 	/* VIA686a test code... reset the latch if count > max + 1 */
575f724e84SDeepak Saxena 	if (count > PIT_LATCH) {
58e6220bdcSThomas Gleixner 		outb_p(0x34, PIT_MODE);
59e6220bdcSThomas Gleixner 		outb_p(PIT_LATCH & 0xff, PIT_CH0);
60e6220bdcSThomas Gleixner 		outb_p(PIT_LATCH >> 8, PIT_CH0);
6189c0b8e2SRussell King 		count = PIT_LATCH - 1;
6289c0b8e2SRussell King 	}
6389c0b8e2SRussell King 
6489c0b8e2SRussell King 	/*
6589c0b8e2SRussell King 	 * It's possible for count to appear to go the wrong way for a
6689c0b8e2SRussell King 	 * couple of reasons:
6789c0b8e2SRussell King 	 *
6889c0b8e2SRussell King 	 *  1. The timer counter underflows, but we haven't handled the
6989c0b8e2SRussell King 	 *     resulting interrupt and incremented jiffies yet.
7089c0b8e2SRussell King 	 *  2. Hardware problem with the timer, not giving us continuous time,
7189c0b8e2SRussell King 	 *     the counter does small "jumps" upwards on some Pentium systems,
7289c0b8e2SRussell King 	 *     (see c't 95/10 page 335 for Neptun bug.)
7389c0b8e2SRussell King 	 *
7489c0b8e2SRussell King 	 * Previous attempts to handle these cases intelligently were
7589c0b8e2SRussell King 	 * buggy, so we just do the simple thing now.
7689c0b8e2SRussell King 	 */
7789c0b8e2SRussell King 	if (count > old_count && jifs == old_jifs)
7889c0b8e2SRussell King 		count = old_count;
7989c0b8e2SRussell King 
8089c0b8e2SRussell King 	old_count = count;
8189c0b8e2SRussell King 	old_jifs = jifs;
8289c0b8e2SRussell King 
8389c0b8e2SRussell King 	raw_spin_unlock_irqrestore(&i8253_lock, flags);
8489c0b8e2SRussell King 
8589c0b8e2SRussell King 	count = (PIT_LATCH - 1) - count;
8689c0b8e2SRussell King 
87a5a1d1c2SThomas Gleixner 	return (u64)(jifs * PIT_LATCH) + count;
8889c0b8e2SRussell King }
8989c0b8e2SRussell King 
9089c0b8e2SRussell King static struct clocksource i8253_cs = {
9189c0b8e2SRussell King 	.name		= "pit",
9289c0b8e2SRussell King 	.rating		= 110,
9389c0b8e2SRussell King 	.read		= i8253_read,
9489c0b8e2SRussell King 	.mask		= CLOCKSOURCE_MASK(32),
9589c0b8e2SRussell King };
9689c0b8e2SRussell King 
9789c0b8e2SRussell King int __init clocksource_i8253_init(void)
9889c0b8e2SRussell King {
9989c0b8e2SRussell King 	return clocksource_register_hz(&i8253_cs, PIT_TICK_RATE);
10089c0b8e2SRussell King }
10115f304b6SRalf Baechle #endif
102e6220bdcSThomas Gleixner 
103e6220bdcSThomas Gleixner #ifdef CONFIG_CLKEVT_I8253
clocksource_i8253_init(void)104c02c5203SDavid Woodhouse void clockevent_i8253_disable(void)
1058eda41b0SViresh Kumar {
1068eda41b0SViresh Kumar 	raw_spin_lock(&i8253_lock);
1078eda41b0SViresh Kumar 
108*de47f33dSDavid Woodhouse 	/*
109*de47f33dSDavid Woodhouse 	 * Writing the MODE register should stop the counter, according to
110*de47f33dSDavid Woodhouse 	 * the datasheet. This appears to work on real hardware (well, on
111*de47f33dSDavid Woodhouse 	 * modern Intel and AMD boxes; I didn't dig the Pegasos out of the
112*de47f33dSDavid Woodhouse 	 * shed).
113*de47f33dSDavid Woodhouse 	 *
114*de47f33dSDavid Woodhouse 	 * However, some virtual implementations differ, and the MODE change
115*de47f33dSDavid Woodhouse 	 * doesn't have any effect until either the counter is written (KVM
116*de47f33dSDavid Woodhouse 	 * in-kernel PIT) or the next interrupt (QEMU). And in those cases,
117*de47f33dSDavid Woodhouse 	 * it may not stop the *count*, only the interrupts. Although in
118*de47f33dSDavid Woodhouse 	 * the virt case, that probably doesn't matter, as the value of the
119*de47f33dSDavid Woodhouse 	 * counter will only be calculated on demand if the guest reads it;
120*de47f33dSDavid Woodhouse 	 * it's the interrupts which cause steal time.
121*de47f33dSDavid Woodhouse 	 *
122*de47f33dSDavid Woodhouse 	 * Hyper-V apparently has a bug where even in mode 0, the IRQ keeps
123*de47f33dSDavid Woodhouse 	 * firing repeatedly if the counter is running. But it *does* do the
124*de47f33dSDavid Woodhouse 	 * right thing when the MODE register is written.
125*de47f33dSDavid Woodhouse 	 *
126*de47f33dSDavid Woodhouse 	 * So: write the MODE and then load the counter, which ensures that
127*de47f33dSDavid Woodhouse 	 * the IRQ is stopped on those buggy virt implementations. And then
128*de47f33dSDavid Woodhouse 	 * write the MODE again, which is the right way to stop it.
129*de47f33dSDavid Woodhouse 	 */
1308eda41b0SViresh Kumar 	outb_p(0x30, PIT_MODE);
131*de47f33dSDavid Woodhouse 	outb_p(0, PIT_CH0);
132*de47f33dSDavid Woodhouse 	outb_p(0, PIT_CH0);
13335b69a42SMichael Kelley 
134*de47f33dSDavid Woodhouse 	outb_p(0x30, PIT_MODE);
1358eda41b0SViresh Kumar 
1368eda41b0SViresh Kumar 	raw_spin_unlock(&i8253_lock);
137c02c5203SDavid Woodhouse }
138c02c5203SDavid Woodhouse 
139c02c5203SDavid Woodhouse static int pit_shutdown(struct clock_event_device *evt)
140c02c5203SDavid Woodhouse {
141c02c5203SDavid Woodhouse 	if (!clockevent_state_oneshot(evt) && !clockevent_state_periodic(evt))
pit_set_periodic(struct clock_event_device * evt)142c02c5203SDavid Woodhouse 		return 0;
143c02c5203SDavid Woodhouse 
144c02c5203SDavid Woodhouse 	clockevent_i8253_disable();
1458eda41b0SViresh Kumar 	return 0;
1468eda41b0SViresh Kumar }
1478eda41b0SViresh Kumar 
1488eda41b0SViresh Kumar static int pit_set_oneshot(struct clock_event_device *evt)
1498eda41b0SViresh Kumar {
1508eda41b0SViresh Kumar 	raw_spin_lock(&i8253_lock);
1518eda41b0SViresh Kumar 	outb_p(0x38, PIT_MODE);
1528eda41b0SViresh Kumar 	raw_spin_unlock(&i8253_lock);
1538eda41b0SViresh Kumar 	return 0;
1548eda41b0SViresh Kumar }
1558eda41b0SViresh Kumar 
1568eda41b0SViresh Kumar static int pit_set_periodic(struct clock_event_device *evt)
157e6220bdcSThomas Gleixner {
158e6220bdcSThomas Gleixner 	raw_spin_lock(&i8253_lock);
159e6220bdcSThomas Gleixner 
pit_next_event(unsigned long delta,struct clock_event_device * evt)160e6220bdcSThomas Gleixner 	/* binary, mode 2, LSB/MSB, ch 0 */
161e6220bdcSThomas Gleixner 	outb_p(0x34, PIT_MODE);
1625f724e84SDeepak Saxena 	outb_p(PIT_LATCH & 0xff, PIT_CH0);	/* LSB */
1635f724e84SDeepak Saxena 	outb_p(PIT_LATCH >> 8, PIT_CH0);	/* MSB */
164e6220bdcSThomas Gleixner 
165e6220bdcSThomas Gleixner 	raw_spin_unlock(&i8253_lock);
1668eda41b0SViresh Kumar 	return 0;
167e6220bdcSThomas Gleixner }
168e6220bdcSThomas Gleixner 
169e6220bdcSThomas Gleixner /*
170e6220bdcSThomas Gleixner  * Program the next event in oneshot mode
171e6220bdcSThomas Gleixner  *
172e6220bdcSThomas Gleixner  * Delta is given in PIT ticks
173e6220bdcSThomas Gleixner  */
174e6220bdcSThomas Gleixner static int pit_next_event(unsigned long delta, struct clock_event_device *evt)
175e6220bdcSThomas Gleixner {
176e6220bdcSThomas Gleixner 	raw_spin_lock(&i8253_lock);
177e6220bdcSThomas Gleixner 	outb_p(delta & 0xff , PIT_CH0);	/* LSB */
178e6220bdcSThomas Gleixner 	outb_p(delta >> 8 , PIT_CH0);		/* MSB */
179e6220bdcSThomas Gleixner 	raw_spin_unlock(&i8253_lock);
180e6220bdcSThomas Gleixner 
181e6220bdcSThomas Gleixner 	return 0;
182e6220bdcSThomas Gleixner }
183e6220bdcSThomas Gleixner 
184e6220bdcSThomas Gleixner /*
185e6220bdcSThomas Gleixner  * On UP the PIT can serve all of the possible timer functions. On SMP systems
clockevent_i8253_init(bool oneshot)186e6220bdcSThomas Gleixner  * it can be solely used for the global tick.
187e6220bdcSThomas Gleixner  */
188e6220bdcSThomas Gleixner struct clock_event_device i8253_clockevent = {
189e6220bdcSThomas Gleixner 	.name			= "pit",
190e6220bdcSThomas Gleixner 	.features		= CLOCK_EVT_FEAT_PERIODIC,
1918eda41b0SViresh Kumar 	.set_state_shutdown	= pit_shutdown,
1928eda41b0SViresh Kumar 	.set_state_periodic	= pit_set_periodic,
193e6220bdcSThomas Gleixner 	.set_next_event		= pit_next_event,
194e6220bdcSThomas Gleixner };
195e6220bdcSThomas Gleixner 
196e6220bdcSThomas Gleixner /*
197e6220bdcSThomas Gleixner  * Initialize the conversion factor and the min/max deltas of the clock event
198e6220bdcSThomas Gleixner  * structure and register the clock event source with the framework.
199e6220bdcSThomas Gleixner  */
200e6220bdcSThomas Gleixner void __init clockevent_i8253_init(bool oneshot)
201e6220bdcSThomas Gleixner {
2028eda41b0SViresh Kumar 	if (oneshot) {
203e6220bdcSThomas Gleixner 		i8253_clockevent.features |= CLOCK_EVT_FEAT_ONESHOT;
2048eda41b0SViresh Kumar 		i8253_clockevent.set_state_oneshot = pit_set_oneshot;
2058eda41b0SViresh Kumar 	}
206e6220bdcSThomas Gleixner 	/*
207e6220bdcSThomas Gleixner 	 * Start pit with the boot cpu mask. x86 might make it global
208e6220bdcSThomas Gleixner 	 * when it is used as broadcast device later.
209e6220bdcSThomas Gleixner 	 */
210e6220bdcSThomas Gleixner 	i8253_clockevent.cpumask = cpumask_of(smp_processor_id());
211e6220bdcSThomas Gleixner 
212e6220bdcSThomas Gleixner 	clockevents_config_and_register(&i8253_clockevent, PIT_TICK_RATE,
213e6220bdcSThomas Gleixner 					0xF, 0x7FFF);
214e6220bdcSThomas Gleixner }
215e6220bdcSThomas Gleixner #endif
216