162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci *  Machine specific calibrate_tsc() for generic.
462306a36Sopenharmony_ci *  Split out from timer_tsc.c by Osamu Tomita <tomita@cinet.co.jp>
562306a36Sopenharmony_ci */
662306a36Sopenharmony_ci/* ------ Calibrate the TSC -------
762306a36Sopenharmony_ci * Return 2^32 * (1 / (TSC clocks per usec)) for do_fast_gettimeoffset().
862306a36Sopenharmony_ci * Too much 64-bit arithmetic here to do this cleanly in C, and for
962306a36Sopenharmony_ci * accuracy's sake we want to keep the overhead on the CTC speaker (channel 2)
1062306a36Sopenharmony_ci * output busy loop as low as possible. We avoid reading the CTC registers
1162306a36Sopenharmony_ci * directly because of the awkward 8-bit access mechanism of the 82C54
1262306a36Sopenharmony_ci * device.
1362306a36Sopenharmony_ci */
1462306a36Sopenharmony_ci#ifndef _ASM_X86_MACH_DEFAULT_MACH_TIMER_H
1562306a36Sopenharmony_ci#define _ASM_X86_MACH_DEFAULT_MACH_TIMER_H
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci#define CALIBRATE_TIME_MSEC 30 /* 30 msecs */
1862306a36Sopenharmony_ci#define CALIBRATE_LATCH	\
1962306a36Sopenharmony_ci	((PIT_TICK_RATE * CALIBRATE_TIME_MSEC + 1000/2)/1000)
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_cistatic inline void mach_prepare_counter(void)
2262306a36Sopenharmony_ci{
2362306a36Sopenharmony_ci       /* Set the Gate high, disable speaker */
2462306a36Sopenharmony_ci	outb((inb(0x61) & ~0x02) | 0x01, 0x61);
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci	/*
2762306a36Sopenharmony_ci	 * Now let's take care of CTC channel 2
2862306a36Sopenharmony_ci	 *
2962306a36Sopenharmony_ci	 * Set the Gate high, program CTC channel 2 for mode 0,
3062306a36Sopenharmony_ci	 * (interrupt on terminal count mode), binary count,
3162306a36Sopenharmony_ci	 * load 5 * LATCH count, (LSB and MSB) to begin countdown.
3262306a36Sopenharmony_ci	 *
3362306a36Sopenharmony_ci	 * Some devices need a delay here.
3462306a36Sopenharmony_ci	 */
3562306a36Sopenharmony_ci	outb(0xb0, 0x43);			/* binary, mode 0, LSB/MSB, Ch 2 */
3662306a36Sopenharmony_ci	outb_p(CALIBRATE_LATCH & 0xff, 0x42);	/* LSB of count */
3762306a36Sopenharmony_ci	outb_p(CALIBRATE_LATCH >> 8, 0x42);       /* MSB of count */
3862306a36Sopenharmony_ci}
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_cistatic inline void mach_countup(unsigned long *count_p)
4162306a36Sopenharmony_ci{
4262306a36Sopenharmony_ci	unsigned long count = 0;
4362306a36Sopenharmony_ci	do {
4462306a36Sopenharmony_ci		count++;
4562306a36Sopenharmony_ci	} while ((inb_p(0x61) & 0x20) == 0);
4662306a36Sopenharmony_ci	*count_p = count;
4762306a36Sopenharmony_ci}
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci#endif /* _ASM_X86_MACH_DEFAULT_MACH_TIMER_H */
50