18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  Machine specific calibrate_tsc() for generic.
48c2ecf20Sopenharmony_ci *  Split out from timer_tsc.c by Osamu Tomita <tomita@cinet.co.jp>
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci/* ------ Calibrate the TSC -------
78c2ecf20Sopenharmony_ci * Return 2^32 * (1 / (TSC clocks per usec)) for do_fast_gettimeoffset().
88c2ecf20Sopenharmony_ci * Too much 64-bit arithmetic here to do this cleanly in C, and for
98c2ecf20Sopenharmony_ci * accuracy's sake we want to keep the overhead on the CTC speaker (channel 2)
108c2ecf20Sopenharmony_ci * output busy loop as low as possible. We avoid reading the CTC registers
118c2ecf20Sopenharmony_ci * directly because of the awkward 8-bit access mechanism of the 82C54
128c2ecf20Sopenharmony_ci * device.
138c2ecf20Sopenharmony_ci */
148c2ecf20Sopenharmony_ci#ifndef _ASM_X86_MACH_DEFAULT_MACH_TIMER_H
158c2ecf20Sopenharmony_ci#define _ASM_X86_MACH_DEFAULT_MACH_TIMER_H
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#define CALIBRATE_TIME_MSEC 30 /* 30 msecs */
188c2ecf20Sopenharmony_ci#define CALIBRATE_LATCH	\
198c2ecf20Sopenharmony_ci	((PIT_TICK_RATE * CALIBRATE_TIME_MSEC + 1000/2)/1000)
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_cistatic inline void mach_prepare_counter(void)
228c2ecf20Sopenharmony_ci{
238c2ecf20Sopenharmony_ci       /* Set the Gate high, disable speaker */
248c2ecf20Sopenharmony_ci	outb((inb(0x61) & ~0x02) | 0x01, 0x61);
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci	/*
278c2ecf20Sopenharmony_ci	 * Now let's take care of CTC channel 2
288c2ecf20Sopenharmony_ci	 *
298c2ecf20Sopenharmony_ci	 * Set the Gate high, program CTC channel 2 for mode 0,
308c2ecf20Sopenharmony_ci	 * (interrupt on terminal count mode), binary count,
318c2ecf20Sopenharmony_ci	 * load 5 * LATCH count, (LSB and MSB) to begin countdown.
328c2ecf20Sopenharmony_ci	 *
338c2ecf20Sopenharmony_ci	 * Some devices need a delay here.
348c2ecf20Sopenharmony_ci	 */
358c2ecf20Sopenharmony_ci	outb(0xb0, 0x43);			/* binary, mode 0, LSB/MSB, Ch 2 */
368c2ecf20Sopenharmony_ci	outb_p(CALIBRATE_LATCH & 0xff, 0x42);	/* LSB of count */
378c2ecf20Sopenharmony_ci	outb_p(CALIBRATE_LATCH >> 8, 0x42);       /* MSB of count */
388c2ecf20Sopenharmony_ci}
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic inline void mach_countup(unsigned long *count_p)
418c2ecf20Sopenharmony_ci{
428c2ecf20Sopenharmony_ci	unsigned long count = 0;
438c2ecf20Sopenharmony_ci	do {
448c2ecf20Sopenharmony_ci		count++;
458c2ecf20Sopenharmony_ci	} while ((inb_p(0x61) & 0x20) == 0);
468c2ecf20Sopenharmony_ci	*count_p = count;
478c2ecf20Sopenharmony_ci}
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci#endif /* _ASM_X86_MACH_DEFAULT_MACH_TIMER_H */
50