18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * i8253 PIT clocksource
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci#include <linux/clockchips.h>
68c2ecf20Sopenharmony_ci#include <linux/init.h>
78c2ecf20Sopenharmony_ci#include <linux/io.h>
88c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
98c2ecf20Sopenharmony_ci#include <linux/timex.h>
108c2ecf20Sopenharmony_ci#include <linux/module.h>
118c2ecf20Sopenharmony_ci#include <linux/i8253.h>
128c2ecf20Sopenharmony_ci#include <linux/smp.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci/*
158c2ecf20Sopenharmony_ci * Protects access to I/O ports
168c2ecf20Sopenharmony_ci *
178c2ecf20Sopenharmony_ci * 0040-0043 : timer0, i8253 / i8254
188c2ecf20Sopenharmony_ci * 0061-0061 : NMI Control Register which contains two speaker control bits.
198c2ecf20Sopenharmony_ci */
208c2ecf20Sopenharmony_ciDEFINE_RAW_SPINLOCK(i8253_lock);
218c2ecf20Sopenharmony_ciEXPORT_SYMBOL(i8253_lock);
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci/*
248c2ecf20Sopenharmony_ci * Handle PIT quirk in pit_shutdown() where zeroing the counter register
258c2ecf20Sopenharmony_ci * restarts the PIT, negating the shutdown. On platforms with the quirk,
268c2ecf20Sopenharmony_ci * platform specific code can set this to false.
278c2ecf20Sopenharmony_ci */
288c2ecf20Sopenharmony_cibool i8253_clear_counter_on_shutdown __ro_after_init = true;
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#ifdef CONFIG_CLKSRC_I8253
318c2ecf20Sopenharmony_ci/*
328c2ecf20Sopenharmony_ci * Since the PIT overflows every tick, its not very useful
338c2ecf20Sopenharmony_ci * to just read by itself. So use jiffies to emulate a free
348c2ecf20Sopenharmony_ci * running counter:
358c2ecf20Sopenharmony_ci */
368c2ecf20Sopenharmony_cistatic u64 i8253_read(struct clocksource *cs)
378c2ecf20Sopenharmony_ci{
388c2ecf20Sopenharmony_ci	static int old_count;
398c2ecf20Sopenharmony_ci	static u32 old_jifs;
408c2ecf20Sopenharmony_ci	unsigned long flags;
418c2ecf20Sopenharmony_ci	int count;
428c2ecf20Sopenharmony_ci	u32 jifs;
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	raw_spin_lock_irqsave(&i8253_lock, flags);
458c2ecf20Sopenharmony_ci	/*
468c2ecf20Sopenharmony_ci	 * Although our caller may have the read side of jiffies_lock,
478c2ecf20Sopenharmony_ci	 * this is now a seqlock, and we are cheating in this routine
488c2ecf20Sopenharmony_ci	 * by having side effects on state that we cannot undo if
498c2ecf20Sopenharmony_ci	 * there is a collision on the seqlock and our caller has to
508c2ecf20Sopenharmony_ci	 * retry.  (Namely, old_jifs and old_count.)  So we must treat
518c2ecf20Sopenharmony_ci	 * jiffies as volatile despite the lock.  We read jiffies
528c2ecf20Sopenharmony_ci	 * before latching the timer count to guarantee that although
538c2ecf20Sopenharmony_ci	 * the jiffies value might be older than the count (that is,
548c2ecf20Sopenharmony_ci	 * the counter may underflow between the last point where
558c2ecf20Sopenharmony_ci	 * jiffies was incremented and the point where we latch the
568c2ecf20Sopenharmony_ci	 * count), it cannot be newer.
578c2ecf20Sopenharmony_ci	 */
588c2ecf20Sopenharmony_ci	jifs = jiffies;
598c2ecf20Sopenharmony_ci	outb_p(0x00, PIT_MODE);	/* latch the count ASAP */
608c2ecf20Sopenharmony_ci	count = inb_p(PIT_CH0);	/* read the latched count */
618c2ecf20Sopenharmony_ci	count |= inb_p(PIT_CH0) << 8;
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	/* VIA686a test code... reset the latch if count > max + 1 */
648c2ecf20Sopenharmony_ci	if (count > PIT_LATCH) {
658c2ecf20Sopenharmony_ci		outb_p(0x34, PIT_MODE);
668c2ecf20Sopenharmony_ci		outb_p(PIT_LATCH & 0xff, PIT_CH0);
678c2ecf20Sopenharmony_ci		outb_p(PIT_LATCH >> 8, PIT_CH0);
688c2ecf20Sopenharmony_ci		count = PIT_LATCH - 1;
698c2ecf20Sopenharmony_ci	}
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	/*
728c2ecf20Sopenharmony_ci	 * It's possible for count to appear to go the wrong way for a
738c2ecf20Sopenharmony_ci	 * couple of reasons:
748c2ecf20Sopenharmony_ci	 *
758c2ecf20Sopenharmony_ci	 *  1. The timer counter underflows, but we haven't handled the
768c2ecf20Sopenharmony_ci	 *     resulting interrupt and incremented jiffies yet.
778c2ecf20Sopenharmony_ci	 *  2. Hardware problem with the timer, not giving us continuous time,
788c2ecf20Sopenharmony_ci	 *     the counter does small "jumps" upwards on some Pentium systems,
798c2ecf20Sopenharmony_ci	 *     (see c't 95/10 page 335 for Neptun bug.)
808c2ecf20Sopenharmony_ci	 *
818c2ecf20Sopenharmony_ci	 * Previous attempts to handle these cases intelligently were
828c2ecf20Sopenharmony_ci	 * buggy, so we just do the simple thing now.
838c2ecf20Sopenharmony_ci	 */
848c2ecf20Sopenharmony_ci	if (count > old_count && jifs == old_jifs)
858c2ecf20Sopenharmony_ci		count = old_count;
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	old_count = count;
888c2ecf20Sopenharmony_ci	old_jifs = jifs;
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	raw_spin_unlock_irqrestore(&i8253_lock, flags);
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	count = (PIT_LATCH - 1) - count;
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	return (u64)(jifs * PIT_LATCH) + count;
958c2ecf20Sopenharmony_ci}
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_cistatic struct clocksource i8253_cs = {
988c2ecf20Sopenharmony_ci	.name		= "pit",
998c2ecf20Sopenharmony_ci	.rating		= 110,
1008c2ecf20Sopenharmony_ci	.read		= i8253_read,
1018c2ecf20Sopenharmony_ci	.mask		= CLOCKSOURCE_MASK(32),
1028c2ecf20Sopenharmony_ci};
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ciint __init clocksource_i8253_init(void)
1058c2ecf20Sopenharmony_ci{
1068c2ecf20Sopenharmony_ci	return clocksource_register_hz(&i8253_cs, PIT_TICK_RATE);
1078c2ecf20Sopenharmony_ci}
1088c2ecf20Sopenharmony_ci#endif
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci#ifdef CONFIG_CLKEVT_I8253
1118c2ecf20Sopenharmony_cistatic int pit_shutdown(struct clock_event_device *evt)
1128c2ecf20Sopenharmony_ci{
1138c2ecf20Sopenharmony_ci	if (!clockevent_state_oneshot(evt) && !clockevent_state_periodic(evt))
1148c2ecf20Sopenharmony_ci		return 0;
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	raw_spin_lock(&i8253_lock);
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	outb_p(0x30, PIT_MODE);
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	if (i8253_clear_counter_on_shutdown) {
1218c2ecf20Sopenharmony_ci		outb_p(0, PIT_CH0);
1228c2ecf20Sopenharmony_ci		outb_p(0, PIT_CH0);
1238c2ecf20Sopenharmony_ci	}
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	raw_spin_unlock(&i8253_lock);
1268c2ecf20Sopenharmony_ci	return 0;
1278c2ecf20Sopenharmony_ci}
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_cistatic int pit_set_oneshot(struct clock_event_device *evt)
1308c2ecf20Sopenharmony_ci{
1318c2ecf20Sopenharmony_ci	raw_spin_lock(&i8253_lock);
1328c2ecf20Sopenharmony_ci	outb_p(0x38, PIT_MODE);
1338c2ecf20Sopenharmony_ci	raw_spin_unlock(&i8253_lock);
1348c2ecf20Sopenharmony_ci	return 0;
1358c2ecf20Sopenharmony_ci}
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_cistatic int pit_set_periodic(struct clock_event_device *evt)
1388c2ecf20Sopenharmony_ci{
1398c2ecf20Sopenharmony_ci	raw_spin_lock(&i8253_lock);
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	/* binary, mode 2, LSB/MSB, ch 0 */
1428c2ecf20Sopenharmony_ci	outb_p(0x34, PIT_MODE);
1438c2ecf20Sopenharmony_ci	outb_p(PIT_LATCH & 0xff, PIT_CH0);	/* LSB */
1448c2ecf20Sopenharmony_ci	outb_p(PIT_LATCH >> 8, PIT_CH0);	/* MSB */
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	raw_spin_unlock(&i8253_lock);
1478c2ecf20Sopenharmony_ci	return 0;
1488c2ecf20Sopenharmony_ci}
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci/*
1518c2ecf20Sopenharmony_ci * Program the next event in oneshot mode
1528c2ecf20Sopenharmony_ci *
1538c2ecf20Sopenharmony_ci * Delta is given in PIT ticks
1548c2ecf20Sopenharmony_ci */
1558c2ecf20Sopenharmony_cistatic int pit_next_event(unsigned long delta, struct clock_event_device *evt)
1568c2ecf20Sopenharmony_ci{
1578c2ecf20Sopenharmony_ci	raw_spin_lock(&i8253_lock);
1588c2ecf20Sopenharmony_ci	outb_p(delta & 0xff , PIT_CH0);	/* LSB */
1598c2ecf20Sopenharmony_ci	outb_p(delta >> 8 , PIT_CH0);		/* MSB */
1608c2ecf20Sopenharmony_ci	raw_spin_unlock(&i8253_lock);
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	return 0;
1638c2ecf20Sopenharmony_ci}
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci/*
1668c2ecf20Sopenharmony_ci * On UP the PIT can serve all of the possible timer functions. On SMP systems
1678c2ecf20Sopenharmony_ci * it can be solely used for the global tick.
1688c2ecf20Sopenharmony_ci */
1698c2ecf20Sopenharmony_cistruct clock_event_device i8253_clockevent = {
1708c2ecf20Sopenharmony_ci	.name			= "pit",
1718c2ecf20Sopenharmony_ci	.features		= CLOCK_EVT_FEAT_PERIODIC,
1728c2ecf20Sopenharmony_ci	.set_state_shutdown	= pit_shutdown,
1738c2ecf20Sopenharmony_ci	.set_state_periodic	= pit_set_periodic,
1748c2ecf20Sopenharmony_ci	.set_next_event		= pit_next_event,
1758c2ecf20Sopenharmony_ci};
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci/*
1788c2ecf20Sopenharmony_ci * Initialize the conversion factor and the min/max deltas of the clock event
1798c2ecf20Sopenharmony_ci * structure and register the clock event source with the framework.
1808c2ecf20Sopenharmony_ci */
1818c2ecf20Sopenharmony_civoid __init clockevent_i8253_init(bool oneshot)
1828c2ecf20Sopenharmony_ci{
1838c2ecf20Sopenharmony_ci	if (oneshot) {
1848c2ecf20Sopenharmony_ci		i8253_clockevent.features |= CLOCK_EVT_FEAT_ONESHOT;
1858c2ecf20Sopenharmony_ci		i8253_clockevent.set_state_oneshot = pit_set_oneshot;
1868c2ecf20Sopenharmony_ci	}
1878c2ecf20Sopenharmony_ci	/*
1888c2ecf20Sopenharmony_ci	 * Start pit with the boot cpu mask. x86 might make it global
1898c2ecf20Sopenharmony_ci	 * when it is used as broadcast device later.
1908c2ecf20Sopenharmony_ci	 */
1918c2ecf20Sopenharmony_ci	i8253_clockevent.cpumask = cpumask_of(smp_processor_id());
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	clockevents_config_and_register(&i8253_clockevent, PIT_TICK_RATE,
1948c2ecf20Sopenharmony_ci					0xF, 0x7FFF);
1958c2ecf20Sopenharmony_ci}
1968c2ecf20Sopenharmony_ci#endif
197