pcrtc.c revision 143456
1/*-
2 * Copyright (c) 1990 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * William Jolitz and Don Ahn.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 *	from: @(#)clock.c	7.2 (Berkeley) 5/12/91
33 * $FreeBSD: head/sys/pc98/cbus/pcrtc.c 143456 2005-03-12 10:30:06Z nyan $
34 */
35
36/*
37 * Routines to handle clock hardware.
38 */
39
40/*
41 * inittodr, settodr and support routines written
42 * by Christoph Robitschko <chmr@edvz.tu-graz.ac.at>
43 *
44 * reintroduced and updated by Chris Stenton <chris@gnome.co.uk> 8/10/94
45 */
46
47/*
48 * modified for PC98 by Kakefuda
49 */
50
51#include "opt_apic.h"
52#include "opt_clock.h"
53#include "opt_isa.h"
54#include "opt_mca.h"
55
56#include <sys/param.h>
57#include <sys/systm.h>
58#include <sys/bus.h>
59#include <sys/lock.h>
60#include <sys/kdb.h>
61#include <sys/mutex.h>
62#include <sys/proc.h>
63#include <sys/time.h>
64#include <sys/timetc.h>
65#include <sys/kernel.h>
66#include <sys/limits.h>
67#include <sys/module.h>
68#include <sys/sysctl.h>
69#include <sys/cons.h>
70#include <sys/power.h>
71
72#include <machine/clock.h>
73#include <machine/cputypes.h>
74#include <machine/frame.h>
75#include <machine/intr_machdep.h>
76#include <machine/md_var.h>
77#include <machine/psl.h>
78#ifdef DEV_APIC
79#include <machine/apicvar.h>
80#endif
81#include <machine/specialreg.h>
82
83#include <i386/isa/icu.h>
84#include <pc98/pc98/pc98.h>
85#include <pc98/pc98/pc98_machdep.h>
86#ifdef DEV_ISA
87#include <isa/isavar.h>
88#endif
89#include <i386/isa/timerreg.h>
90
91/*
92 * 32-bit time_t's can't reach leap years before 1904 or after 2036, so we
93 * can use a simple formula for leap years.
94 */
95#define	LEAPYEAR(y) (((u_int)(y) % 4 == 0) ? 1 : 0)
96#define DAYSPERYEAR   (31+28+31+30+31+30+31+31+30+31+30+31)
97
98#define	TIMER_DIV(x) ((timer_freq + (x) / 2) / (x))
99
100int	adjkerntz;		/* local offset from GMT in seconds */
101int	clkintr_pending;
102int	disable_rtc_set;	/* disable resettodr() if != 0 */
103int	pscnt = 1;
104int	psdiv = 1;
105int	statclock_disable;
106#ifndef TIMER_FREQ
107#define TIMER_FREQ   2457600
108#endif
109u_int	timer_freq = TIMER_FREQ;
110int	timer0_max_count;
111int	wall_cmos_clock;	/* wall CMOS clock assumed if != 0 */
112struct mtx clock_lock;
113
114static	int	beeping = 0;
115static	const u_char daysinmonth[] = {31,28,31,30,31,30,31,31,30,31,30,31};
116static	u_int	hardclock_max_count;
117static	u_int32_t i8254_lastcount;
118static	u_int32_t i8254_offset;
119static	int	i8254_ticked;
120static	int	using_lapic_timer;
121static	struct intsrc *i8254_intsrc;
122
123/* Values for timerX_state: */
124#define	RELEASED	0
125#define	RELEASE_PENDING	1
126#define	ACQUIRED	2
127#define	ACQUIRE_PENDING	3
128
129static 	u_char	timer1_state;
130static	u_char	timer2_state;
131static void rtc_serialcombit(int);
132static void rtc_serialcom(int);
133static int rtc_inb(void);
134static void rtc_outb(int);
135
136static	unsigned i8254_get_timecount(struct timecounter *tc);
137static	void	set_timer_freq(u_int freq, int intr_freq);
138
139static struct timecounter i8254_timecounter = {
140	i8254_get_timecount,	/* get_timecount */
141	0,			/* no poll_pps */
142	~0u,			/* counter_mask */
143	0,			/* frequency */
144	"i8254",		/* name */
145	0			/* quality */
146};
147
148static void
149clkintr(struct clockframe *frame)
150{
151
152	if (timecounter->tc_get_timecount == i8254_get_timecount) {
153		mtx_lock_spin(&clock_lock);
154		if (i8254_ticked)
155			i8254_ticked = 0;
156		else {
157			i8254_offset += timer0_max_count;
158			i8254_lastcount = 0;
159		}
160		clkintr_pending = 0;
161		mtx_unlock_spin(&clock_lock);
162	}
163	if (!using_lapic_timer)
164		hardclock(frame);
165}
166
167int
168acquire_timer1(int mode)
169{
170
171	if (timer1_state != RELEASED)
172		return (-1);
173	timer1_state = ACQUIRED;
174
175	/*
176	 * This access to the timer registers is as atomic as possible
177	 * because it is a single instruction.  We could do better if we
178	 * knew the rate.  Use of splclock() limits glitches to 10-100us,
179	 * and this is probably good enough for timer2, so we aren't as
180	 * careful with it as with timer0.
181	 */
182	outb(TIMER_MODE, TIMER_SEL1 | (mode & 0x3f));
183
184	return (0);
185}
186
187int
188acquire_timer2(int mode)
189{
190
191	if (timer2_state != RELEASED)
192		return (-1);
193	timer2_state = ACQUIRED;
194
195	/*
196	 * This access to the timer registers is as atomic as possible
197	 * because it is a single instruction.  We could do better if we
198	 * knew the rate.  Use of splclock() limits glitches to 10-100us,
199	 * and this is probably good enough for timer2, so we aren't as
200	 * careful with it as with timer0.
201	 */
202	outb(TIMER_MODE, TIMER_SEL2 | (mode & 0x3f));
203
204	return (0);
205}
206
207int
208release_timer1()
209{
210
211	if (timer1_state != ACQUIRED)
212		return (-1);
213	timer1_state = RELEASED;
214	outb(TIMER_MODE, TIMER_SEL1 | TIMER_SQWAVE | TIMER_16BIT);
215	return (0);
216}
217
218int
219release_timer2()
220{
221
222	if (timer2_state != ACQUIRED)
223		return (-1);
224	timer2_state = RELEASED;
225	outb(TIMER_MODE, TIMER_SEL2 | TIMER_SQWAVE | TIMER_16BIT);
226	return (0);
227}
228
229
230static int
231getit(void)
232{
233	int high, low;
234
235	mtx_lock_spin(&clock_lock);
236
237	/* Select timer0 and latch counter value. */
238	outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH);
239
240	low = inb(TIMER_CNTR0);
241	high = inb(TIMER_CNTR0);
242
243	mtx_unlock_spin(&clock_lock);
244	return ((high << 8) | low);
245}
246
247/*
248 * Wait "n" microseconds.
249 * Relies on timer 1 counting down from (timer_freq / hz)
250 * Note: timer had better have been programmed before this is first used!
251 */
252void
253DELAY(int n)
254{
255	int delta, prev_tick, tick, ticks_left;
256
257#ifdef DELAYDEBUG
258	int getit_calls = 1;
259	int n1;
260	static int state = 0;
261
262	if (state == 0) {
263		state = 1;
264		for (n1 = 1; n1 <= 10000000; n1 *= 10)
265			DELAY(n1);
266		state = 2;
267	}
268	if (state == 1)
269		printf("DELAY(%d)...", n);
270#endif
271	/*
272	 * Guard against the timer being uninitialized if we are called
273	 * early for console i/o.
274	 */
275	if (timer0_max_count == 0)
276		set_timer_freq(timer_freq, hz);
277
278	/*
279	 * Read the counter first, so that the rest of the setup overhead is
280	 * counted.  Guess the initial overhead is 20 usec (on most systems it
281	 * takes about 1.5 usec for each of the i/o's in getit().  The loop
282	 * takes about 6 usec on a 486/33 and 13 usec on a 386/20.  The
283	 * multiplications and divisions to scale the count take a while).
284	 *
285	 * However, if ddb is active then use a fake counter since reading
286	 * the i8254 counter involves acquiring a lock.  ddb must not do
287	 * locking for many reasons, but it calls here for at least atkbd
288	 * input.
289	 */
290#ifdef KDB
291	if (kdb_active)
292		prev_tick = 1;
293	else
294#endif
295		prev_tick = getit();
296	n -= 0;			/* XXX actually guess no initial overhead */
297	/*
298	 * Calculate (n * (timer_freq / 1e6)) without using floating point
299	 * and without any avoidable overflows.
300	 */
301	if (n <= 0)
302		ticks_left = 0;
303	else if (n < 256)
304		/*
305		 * Use fixed point to avoid a slow division by 1000000.
306		 * 39099 = 1193182 * 2^15 / 10^6 rounded to nearest.
307		 * 2^15 is the first power of 2 that gives exact results
308		 * for n between 0 and 256.
309		 */
310		ticks_left = ((u_int)n * 39099 + (1 << 15) - 1) >> 15;
311	else
312		/*
313		 * Don't bother using fixed point, although gcc-2.7.2
314		 * generates particularly poor code for the long long
315		 * division, since even the slow way will complete long
316		 * before the delay is up (unless we're interrupted).
317		 */
318		ticks_left = ((u_int)n * (long long)timer_freq + 999999)
319			     / 1000000;
320
321	while (ticks_left > 0) {
322#ifdef KDB
323		if (kdb_active) {
324			outb(0x5f, 0);
325			tick = prev_tick - 1;
326			if (tick <= 0)
327				tick = timer0_max_count;
328		} else
329#endif
330			tick = getit();
331#ifdef DELAYDEBUG
332		++getit_calls;
333#endif
334		delta = prev_tick - tick;
335		prev_tick = tick;
336		if (delta < 0) {
337			delta += timer0_max_count;
338			/*
339			 * Guard against timer0_max_count being wrong.
340			 * This shouldn't happen in normal operation,
341			 * but it may happen if set_timer_freq() is
342			 * traced.
343			 */
344			if (delta < 0)
345				delta = 0;
346		}
347		ticks_left -= delta;
348	}
349#ifdef DELAYDEBUG
350	if (state == 1)
351		printf(" %d calls to getit() at %d usec each\n",
352		       getit_calls, (n + 5) / getit_calls);
353#endif
354}
355
356static void
357sysbeepstop(void *chan)
358{
359	outb(IO_PPI, inb(IO_PPI)|0x08);	/* disable counter1 output to speaker */
360	release_timer1();
361	beeping = 0;
362}
363
364int
365sysbeep(int pitch, int period)
366{
367	int x = splclock();
368
369	if (acquire_timer1(TIMER_SQWAVE|TIMER_16BIT))
370		if (!beeping) {
371			/* Something else owns it. */
372			splx(x);
373			return (-1); /* XXX Should be EBUSY, but nobody cares anyway. */
374		}
375	disable_intr();
376	outb(0x3fdb, pitch);
377	outb(0x3fdb, (pitch>>8));
378	enable_intr();
379	if (!beeping) {
380		/* enable counter1 output to speaker */
381		outb(IO_PPI, (inb(IO_PPI) & 0xf7));
382		beeping = period;
383		timeout(sysbeepstop, (void *)NULL, period);
384	}
385	splx(x);
386	return (0);
387}
388
389
390unsigned int delaycount;
391#define FIRST_GUESS	0x2000
392static void findcpuspeed(void)
393{
394	int i;
395	int remainder;
396
397	/* Put counter in count down mode */
398	outb(TIMER_MODE, TIMER_SEL0 | TIMER_16BIT | TIMER_RATEGEN);
399	outb(TIMER_CNTR0, 0xff);
400	outb(TIMER_CNTR0, 0xff);
401	for (i = FIRST_GUESS; i; i--)
402		;
403	remainder = getit();
404	delaycount = (FIRST_GUESS * TIMER_DIV(1000)) / (0xffff - remainder);
405}
406
407static u_int
408calibrate_clocks(void)
409{
410	int	timeout;
411	u_int	count, prev_count, tot_count;
412	u_short	sec, start_sec;
413
414	if (bootverbose)
415	        printf("Calibrating clock(s) ... ");
416	/* Check ARTIC. */
417	if (!(PC98_SYSTEM_PARAMETER(0x458) & 0x80) &&
418	    !(PC98_SYSTEM_PARAMETER(0x45b) & 0x04))
419		goto fail;
420	timeout = 100000000;
421
422	/* Read the ARTIC. */
423	sec = inw(0x5e);
424
425	/* Wait for the ARTIC to changes. */
426	start_sec = sec;
427	for (;;) {
428		sec = inw(0x5e);
429		if (sec != start_sec)
430			break;
431		if (--timeout == 0)
432			goto fail;
433	}
434	prev_count = getit();
435	if (prev_count == 0 || prev_count > timer0_max_count)
436		goto fail;
437	tot_count = 0;
438
439	start_sec = sec;
440	for (;;) {
441		sec = inw(0x5e);
442		count = getit();
443		if (count == 0 || count > timer0_max_count)
444			goto fail;
445		if (count > prev_count)
446			tot_count += prev_count - (count - timer0_max_count);
447		else
448			tot_count += prev_count - count;
449		prev_count = count;
450		if ((sec == start_sec + 1200) || /* 1200 = 307.2KHz >> 8 */
451		    (sec < start_sec &&
452		        (u_int)sec + 0x10000 == (u_int)start_sec + 1200))
453			break;
454		if (--timeout == 0)
455			goto fail;
456	}
457
458	if (bootverbose) {
459	        printf("i8254 clock: %u Hz\n", tot_count);
460	}
461	return (tot_count);
462
463fail:
464	if (bootverbose)
465	        printf("failed, using default i8254 clock of %u Hz\n",
466		       timer_freq);
467	return (timer_freq);
468}
469
470static void
471set_timer_freq(u_int freq, int intr_freq)
472{
473	int new_timer0_max_count;
474
475	mtx_lock_spin(&clock_lock);
476	timer_freq = freq;
477	new_timer0_max_count = hardclock_max_count = TIMER_DIV(intr_freq);
478	if (new_timer0_max_count != timer0_max_count) {
479		timer0_max_count = new_timer0_max_count;
480		outb(TIMER_MODE, TIMER_SEL0 | TIMER_RATEGEN | TIMER_16BIT);
481		outb(TIMER_CNTR0, timer0_max_count & 0xff);
482		outb(TIMER_CNTR0, timer0_max_count >> 8);
483	}
484	mtx_unlock_spin(&clock_lock);
485}
486
487static void
488i8254_restore(void)
489{
490
491	mtx_lock_spin(&clock_lock);
492	outb(TIMER_MODE, TIMER_SEL0 | TIMER_RATEGEN | TIMER_16BIT);
493	outb(TIMER_CNTR0, timer0_max_count & 0xff);
494	outb(TIMER_CNTR0, timer0_max_count >> 8);
495	mtx_unlock_spin(&clock_lock);
496}
497
498
499/*
500 * Restore all the timers non-atomically (XXX: should be atomically).
501 *
502 * This function is called from pmtimer_resume() to restore all the timers.
503 * This should not be necessary, but there are broken laptops that do not
504 * restore all the timers on resume.
505 */
506void
507timer_restore(void)
508{
509
510	i8254_restore();		/* restore timer_freq and hz */
511}
512
513/*
514 * Initialize 8254 timer 0 early so that it can be used in DELAY().
515 * XXX initialization of other timers is unintentionally left blank.
516 */
517void
518startrtclock()
519{
520	u_int delta, freq;
521
522	findcpuspeed();
523	if (pc98_machine_type & M_8M)
524		timer_freq = 1996800L; /* 1.9968 MHz */
525	else
526		timer_freq = 2457600L; /* 2.4576 MHz */
527
528	set_timer_freq(timer_freq, hz);
529	freq = calibrate_clocks();
530#ifdef CLK_CALIBRATION_LOOP
531	if (bootverbose) {
532		printf(
533		"Press a key on the console to abort clock calibration\n");
534		while (cncheckc() == -1)
535			calibrate_clocks();
536	}
537#endif
538
539	/*
540	 * Use the calibrated i8254 frequency if it seems reasonable.
541	 * Otherwise use the default, and don't use the calibrated i586
542	 * frequency.
543	 */
544	delta = freq > timer_freq ? freq - timer_freq : timer_freq - freq;
545	if (delta < timer_freq / 100) {
546#ifndef CLK_USE_I8254_CALIBRATION
547		if (bootverbose)
548			printf(
549"CLK_USE_I8254_CALIBRATION not specified - using default frequency\n");
550		freq = timer_freq;
551#endif
552		timer_freq = freq;
553	} else {
554		if (bootverbose)
555			printf(
556		    "%d Hz differs from default of %d Hz by more than 1%%\n",
557			       freq, timer_freq);
558	}
559
560	set_timer_freq(timer_freq, hz);
561	i8254_timecounter.tc_frequency = timer_freq;
562	tc_init(&i8254_timecounter);
563
564	init_TSC();
565}
566
567static void
568rtc_serialcombit(int i)
569{
570	outb(IO_RTC, ((i&0x01)<<5)|0x07);
571	DELAY(1);
572	outb(IO_RTC, ((i&0x01)<<5)|0x17);
573	DELAY(1);
574	outb(IO_RTC, ((i&0x01)<<5)|0x07);
575	DELAY(1);
576}
577
578static void
579rtc_serialcom(int i)
580{
581	rtc_serialcombit(i&0x01);
582	rtc_serialcombit((i&0x02)>>1);
583	rtc_serialcombit((i&0x04)>>2);
584	rtc_serialcombit((i&0x08)>>3);
585	outb(IO_RTC, 0x07);
586	DELAY(1);
587	outb(IO_RTC, 0x0f);
588	DELAY(1);
589	outb(IO_RTC, 0x07);
590 	DELAY(1);
591}
592
593static void
594rtc_outb(int val)
595{
596	int s;
597	int sa = 0;
598
599	for (s=0;s<8;s++) {
600	    sa = ((val >> s) & 0x01) ? 0x27 : 0x07;
601	    outb(IO_RTC, sa);		/* set DI & CLK 0 */
602	    DELAY(1);
603	    outb(IO_RTC, sa | 0x10);	/* CLK 1 */
604	    DELAY(1);
605	}
606	outb(IO_RTC, sa & 0xef);	/* CLK 0 */
607}
608
609static int
610rtc_inb(void)
611{
612	int s;
613	int sa = 0;
614
615	for (s=0;s<8;s++) {
616	    sa |= ((inb(0x33) & 0x01) << s);
617	    outb(IO_RTC, 0x17);	/* CLK 1 */
618	    DELAY(1);
619	    outb(IO_RTC, 0x07);	/* CLK 0 */
620	    DELAY(2);
621	}
622	return sa;
623}
624
625/*
626 * Initialize the time of day register, based on the time base which is, e.g.
627 * from a filesystem.
628 */
629void
630inittodr(time_t base)
631{
632	unsigned long	sec, days;
633	int		year, month;
634	int		y, m, s;
635	struct timespec ts;
636	int		second, min, hour;
637
638	if (base) {
639		s = splclock();
640		ts.tv_sec = base;
641		ts.tv_nsec = 0;
642		tc_setclock(&ts);
643		splx(s);
644	}
645
646	rtc_serialcom(0x03);	/* Time Read */
647	rtc_serialcom(0x01);	/* Register shift command. */
648	DELAY(20);
649
650	second = bcd2bin(rtc_inb() & 0xff);	/* sec */
651	min = bcd2bin(rtc_inb() & 0xff);	/* min */
652	hour = bcd2bin(rtc_inb() & 0xff);	/* hour */
653	days = bcd2bin(rtc_inb() & 0xff) - 1;	/* date */
654
655	month = (rtc_inb() >> 4) & 0x0f;	/* month */
656	for (m = 1; m <	month; m++)
657		days +=	daysinmonth[m-1];
658	year = bcd2bin(rtc_inb() & 0xff) + 1900;	/* year */
659	/* 2000 year problem */
660	if (year < 1995)
661		year += 100;
662	if (year < 1970)
663		goto wrong_time;
664	for (y = 1970; y < year; y++)
665		days +=	DAYSPERYEAR + LEAPYEAR(y);
666	if ((month > 2)	&& LEAPYEAR(year))
667		days ++;
668	sec = ((( days * 24 +
669		  hour) * 60 +
670		  min) * 60 +
671		  second);
672	/* sec now contains the	number of seconds, since Jan 1 1970,
673	   in the local	time zone */
674
675	s = splhigh();
676
677	sec += tz_minuteswest * 60 + (wall_cmos_clock ? adjkerntz : 0);
678
679	y = time_second - sec;
680	if (y <= -2 || y >= 2) {
681		/* badly off, adjust it */
682		ts.tv_sec = sec;
683		ts.tv_nsec = 0;
684		tc_setclock(&ts);
685	}
686	splx(s);
687	return;
688
689wrong_time:
690	printf("Invalid time in real time clock.\n");
691	printf("Check and reset the date immediately!\n");
692}
693
694/*
695 * Write system time back to RTC
696 */
697void
698resettodr()
699{
700	unsigned long	tm;
701	int		y, m, s;
702	int		wd;
703
704	if (disable_rtc_set)
705		return;
706
707	s = splclock();
708	tm = time_second;
709	splx(s);
710
711	rtc_serialcom(0x01);	/* Register shift command. */
712
713	/* Calculate local time	to put in RTC */
714
715	tm -= tz_minuteswest * 60 + (wall_cmos_clock ? adjkerntz : 0);
716
717	rtc_outb(bin2bcd(tm%60)); tm /= 60;	/* Write back Seconds */
718	rtc_outb(bin2bcd(tm%60)); tm /= 60;	/* Write back Minutes */
719	rtc_outb(bin2bcd(tm%24)); tm /= 24;	/* Write back Hours   */
720
721	/* We have now the days	since 01-01-1970 in tm */
722	wd = (tm + 4) % 7 + 1;			/* Write back Weekday */
723	for (y = 1970, m = DAYSPERYEAR + LEAPYEAR(y);
724	     tm >= m;
725	     y++,      m = DAYSPERYEAR + LEAPYEAR(y))
726	     tm -= m;
727
728	/* Now we have the years in y and the day-of-the-year in tm */
729	for (m = 0; ; m++) {
730		int ml;
731
732		ml = daysinmonth[m];
733		if (m == 1 && LEAPYEAR(y))
734			ml++;
735		if (tm < ml)
736			break;
737		tm -= ml;
738	}
739
740	m++;
741	rtc_outb(bin2bcd(tm+1));		/* Write back Day     */
742	rtc_outb((m << 4) | wd);		/* Write back Month & Weekday  */
743	rtc_outb(bin2bcd(y%100));		/* Write back Year    */
744
745	rtc_serialcom(0x02);	/* Time set & Counter hold command. */
746	rtc_serialcom(0x00);	/* Register hold command. */
747}
748
749
750/*
751 * Start both clocks running.
752 */
753void
754cpu_initclocks()
755{
756
757#ifdef DEV_APIC
758	using_lapic_timer = lapic_setup_clock();
759#endif
760	/* Finish initializing 8254 timer 0. */
761	intr_add_handler("clk", 0, (driver_intr_t *)clkintr, NULL,
762	    INTR_TYPE_CLK | INTR_FAST, NULL);
763	i8254_intsrc = intr_lookup_source(0);
764
765	init_TSC_tc();
766}
767
768void
769cpu_startprofclock(void)
770{
771}
772
773void
774cpu_stopprofclock(void)
775{
776}
777
778static int
779sysctl_machdep_i8254_freq(SYSCTL_HANDLER_ARGS)
780{
781	int error;
782	u_int freq;
783
784	/*
785	 * Use `i8254' instead of `timer' in external names because `timer'
786	 * is is too generic.  Should use it everywhere.
787	 */
788	freq = timer_freq;
789	error = sysctl_handle_int(oidp, &freq, sizeof(freq), req);
790	if (error == 0 && req->newptr != NULL) {
791		set_timer_freq(freq, hz);
792		i8254_timecounter.tc_frequency = freq;
793	}
794	return (error);
795}
796
797SYSCTL_PROC(_machdep, OID_AUTO, i8254_freq, CTLTYPE_INT | CTLFLAG_RW,
798    0, sizeof(u_int), sysctl_machdep_i8254_freq, "IU", "");
799
800static unsigned
801i8254_get_timecount(struct timecounter *tc)
802{
803	u_int count;
804	u_int high, low;
805	u_int eflags;
806
807	eflags = read_eflags();
808	mtx_lock_spin(&clock_lock);
809
810	/* Select timer0 and latch counter value. */
811	outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH);
812
813	low = inb(TIMER_CNTR0);
814	high = inb(TIMER_CNTR0);
815	count = timer0_max_count - ((high << 8) | low);
816	if (count < i8254_lastcount ||
817	    (!i8254_ticked && (clkintr_pending ||
818	    ((count < 20 || (!(eflags & PSL_I) && count < timer0_max_count / 2u)) &&
819	    i8254_intsrc != NULL &&
820	    i8254_intsrc->is_pic->pic_source_pending(i8254_intsrc))))) {
821		i8254_ticked = 1;
822		i8254_offset += timer0_max_count;
823	}
824	i8254_lastcount = count;
825	count += i8254_offset;
826	mtx_unlock_spin(&clock_lock);
827	return (count);
828}
829
830#ifdef DEV_ISA
831/*
832 * Attach to the ISA PnP descriptors for the timer and realtime clock.
833 */
834static struct isa_pnp_id attimer_ids[] = {
835	{ 0x0001d041 /* PNP0100 */, "AT timer" },
836	{ 0x000bd041 /* PNP0B00 */, "AT realtime clock" },
837	{ 0 }
838};
839
840static int
841attimer_probe(device_t dev)
842{
843	int result;
844
845	if ((result = ISA_PNP_PROBE(device_get_parent(dev), dev, attimer_ids)) <= 0)
846		device_quiet(dev);
847	return(result);
848}
849
850static int
851attimer_attach(device_t dev)
852{
853	return(0);
854}
855
856static device_method_t attimer_methods[] = {
857	/* Device interface */
858	DEVMETHOD(device_probe,		attimer_probe),
859	DEVMETHOD(device_attach,	attimer_attach),
860	DEVMETHOD(device_detach,	bus_generic_detach),
861	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
862	DEVMETHOD(device_suspend,	bus_generic_suspend),	/* XXX stop statclock? */
863	DEVMETHOD(device_resume,	bus_generic_resume),	/* XXX restart statclock? */
864	{ 0, 0 }
865};
866
867static driver_t attimer_driver = {
868	"attimer",
869	attimer_methods,
870	1,		/* no softc */
871};
872
873static devclass_t attimer_devclass;
874
875DRIVER_MODULE(attimer, isa, attimer_driver, attimer_devclass, 0, 0);
876#endif /* DEV_ISA */
877