2 * Copyright (c) 1990 The Regents of the University of California.
5 * This code is derived from software contributed to Berkeley by
6 * William Jolitz and Don Ahn.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
32 * from: @(#)clock.c 7.2 (Berkeley) 5/12/91
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 * Routines to handle clock hardware.
43 * modified for PC98 by Kakefuda
47 #include "opt_clock.h"
51 #include <sys/param.h>
52 #include <sys/systm.h>
56 #include <sys/mutex.h>
58 #include <sys/timetc.h>
59 #include <sys/kernel.h>
60 #include <sys/module.h>
61 #include <sys/sysctl.h>
63 #include <machine/clock.h>
64 #include <machine/cpu.h>
65 #include <machine/frame.h>
66 #include <machine/intr_machdep.h>
67 #include <machine/md_var.h>
69 #include <machine/apicvar.h>
71 #include <machine/ppireg.h>
72 #include <machine/timerreg.h>
74 #include <pc98/pc98/pc98_machdep.h>
76 #include <pc98/cbus/cbus.h>
77 #include <isa/isavar.h>
80 #define TIMER_DIV(x) ((i8254_freq + (x) / 2) / (x))
83 int statclock_disable;
85 #define TIMER_FREQ 2457600
87 u_int i8254_freq = TIMER_FREQ;
88 TUNABLE_INT("hw.i8254.freq", &i8254_freq);
90 static int i8254_real_max_count;
92 static struct mtx clock_lock;
93 static struct intsrc *i8254_intsrc;
94 static u_int32_t i8254_lastcount;
95 static u_int32_t i8254_offset;
96 static int (*i8254_pending)(struct intsrc *);
97 static int i8254_ticked;
98 static int using_lapic_timer;
100 /* Values for timerX_state: */
102 #define RELEASE_PENDING 1
104 #define ACQUIRE_PENDING 3
106 static u_char timer1_state;
108 static unsigned i8254_get_timecount(struct timecounter *tc);
109 static unsigned i8254_simple_get_timecount(struct timecounter *tc);
110 static void set_i8254_freq(u_int freq, int intr_freq);
112 static struct timecounter i8254_timecounter = {
113 i8254_get_timecount, /* get_timecount */
115 ~0u, /* counter_mask */
122 clkintr(struct trapframe *frame)
125 if (timecounter->tc_get_timecount == i8254_get_timecount) {
126 mtx_lock_spin(&clock_lock);
130 i8254_offset += i8254_max_count;
134 mtx_unlock_spin(&clock_lock);
136 KASSERT(!using_lapic_timer, ("clk interrupt enabled with lapic timer"));
137 hardclock(TRAPF_USERMODE(frame), TRAPF_PC(frame));
138 return (FILTER_HANDLED);
142 timer_spkr_acquire(void)
146 mode = TIMER_SEL1 | TIMER_SQWAVE | TIMER_16BIT;
148 if (timer1_state != RELEASED)
150 timer1_state = ACQUIRED;
153 * This access to the timer registers is as atomic as possible
154 * because it is a single instruction. We could do better if we
155 * knew the rate. Use of splclock() limits glitches to 10-100us,
156 * and this is probably good enough for timer2, so we aren't as
157 * careful with it as with timer0.
159 outb(TIMER_MODE, TIMER_SEL1 | (mode & 0x3f));
160 ppi_spkr_on(); /* enable counter1 output to speaker */
166 timer_spkr_release(void)
169 if (timer1_state != ACQUIRED)
171 timer1_state = RELEASED;
172 outb(TIMER_MODE, TIMER_SEL1 | TIMER_SQWAVE | TIMER_16BIT);
173 ppi_spkr_off(); /* disable counter1 output to speaker */
178 timer_spkr_setfreq(int freq)
181 freq = i8254_freq / freq;
182 mtx_lock_spin(&clock_lock);
183 outb(TIMER_CNTR1, (freq) & 0xff);
184 outb(TIMER_CNTR1, (freq) >> 8);
185 mtx_unlock_spin(&clock_lock);
194 mtx_lock_spin(&clock_lock);
196 /* Select timer0 and latch counter value. */
197 outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH);
199 low = inb(TIMER_CNTR0);
200 high = inb(TIMER_CNTR0);
202 mtx_unlock_spin(&clock_lock);
203 return ((high << 8) | low);
207 * Wait "n" microseconds.
208 * Relies on timer 1 counting down from (i8254_freq / hz)
209 * Note: timer had better have been programmed before this is first used!
214 int delta, prev_tick, tick, ticks_left;
219 static int state = 0;
223 for (n1 = 1; n1 <= 10000000; n1 *= 10)
228 printf("DELAY(%d)...", n);
231 * Read the counter first, so that the rest of the setup overhead is
232 * counted. Guess the initial overhead is 20 usec (on most systems it
233 * takes about 1.5 usec for each of the i/o's in getit(). The loop
234 * takes about 6 usec on a 486/33 and 13 usec on a 386/20. The
235 * multiplications and divisions to scale the count take a while).
237 * However, if ddb is active then use a fake counter since reading
238 * the i8254 counter involves acquiring a lock. ddb must not do
239 * locking for many reasons, but it calls here for at least atkbd
248 n -= 0; /* XXX actually guess no initial overhead */
250 * Calculate (n * (i8254_freq / 1e6)) without using floating point
251 * and without any avoidable overflows.
257 * Use fixed point to avoid a slow division by 1000000.
258 * 39099 = 1193182 * 2^15 / 10^6 rounded to nearest.
259 * 2^15 is the first power of 2 that gives exact results
260 * for n between 0 and 256.
262 ticks_left = ((u_int)n * 39099 + (1 << 15) - 1) >> 15;
265 * Don't bother using fixed point, although gcc-2.7.2
266 * generates particularly poor code for the long long
267 * division, since even the slow way will complete long
268 * before the delay is up (unless we're interrupted).
270 ticks_left = ((u_int)n * (long long)i8254_freq + 999999)
273 while (ticks_left > 0) {
277 tick = prev_tick - 1;
279 tick = i8254_max_count;
286 delta = prev_tick - tick;
289 delta += i8254_max_count;
291 * Guard against i8254_max_count being wrong.
292 * This shouldn't happen in normal operation,
293 * but it may happen if set_i8254_freq() is
303 printf(" %d calls to getit() at %d usec each\n",
304 getit_calls, (n + 5) / getit_calls);
309 set_i8254_freq(u_int freq, int intr_freq)
311 int new_i8254_real_max_count;
313 i8254_timecounter.tc_frequency = freq;
314 mtx_lock_spin(&clock_lock);
316 if (using_lapic_timer)
317 new_i8254_real_max_count = 0x10000;
319 new_i8254_real_max_count = TIMER_DIV(intr_freq);
320 if (new_i8254_real_max_count != i8254_real_max_count) {
321 i8254_real_max_count = new_i8254_real_max_count;
322 if (i8254_real_max_count == 0x10000)
323 i8254_max_count = 0xffff;
325 i8254_max_count = i8254_real_max_count;
326 outb(TIMER_MODE, TIMER_SEL0 | TIMER_RATEGEN | TIMER_16BIT);
327 outb(TIMER_CNTR0, i8254_real_max_count & 0xff);
328 outb(TIMER_CNTR0, i8254_real_max_count >> 8);
330 mtx_unlock_spin(&clock_lock);
337 mtx_lock_spin(&clock_lock);
338 outb(TIMER_MODE, TIMER_SEL0 | TIMER_RATEGEN | TIMER_16BIT);
339 outb(TIMER_CNTR0, i8254_real_max_count & 0xff);
340 outb(TIMER_CNTR0, i8254_real_max_count >> 8);
341 mtx_unlock_spin(&clock_lock);
345 * Restore all the timers non-atomically (XXX: should be atomically).
347 * This function is called from pmtimer_resume() to restore all the timers.
348 * This should not be necessary, but there are broken laptops that do not
349 * restore all the timers on resume.
355 i8254_restore(); /* restore i8254_freq and hz */
358 /* This is separate from startrtclock() so that it can be called early. */
363 mtx_init(&clock_lock, "clk", NULL, MTX_SPIN | MTX_NOPROFILE);
365 if (pc98_machine_type & M_8M)
366 i8254_freq = 1996800L; /* 1.9968 MHz */
368 i8254_freq = 2457600L; /* 2.4576 MHz */
370 set_i8254_freq(i8254_freq, hz);
377 set_i8254_freq(i8254_freq, hz);
378 tc_init(&i8254_timecounter);
384 * Start both clocks running.
391 using_lapic_timer = lapic_setup_clock();
394 * If we aren't using the local APIC timer to drive the kernel
395 * clocks, setup the interrupt handler for the 8254 timer 0 so
396 * that it can drive hardclock(). Otherwise, change the 8254
397 * timecounter to user a simpler algorithm.
399 if (!using_lapic_timer) {
400 intr_add_handler("clk", 0, (driver_filter_t *)clkintr, NULL,
401 NULL, INTR_TYPE_CLK, NULL);
402 i8254_intsrc = intr_lookup_source(0);
403 if (i8254_intsrc != NULL)
405 i8254_intsrc->is_pic->pic_source_pending;
407 i8254_timecounter.tc_get_timecount =
408 i8254_simple_get_timecount;
409 i8254_timecounter.tc_counter_mask = 0xffff;
410 set_i8254_freq(i8254_freq, hz);
417 cpu_startprofclock(void)
422 cpu_stopprofclock(void)
427 sysctl_machdep_i8254_freq(SYSCTL_HANDLER_ARGS)
433 * Use `i8254' instead of `timer' in external names because `timer'
434 * is is too generic. Should use it everywhere.
437 error = sysctl_handle_int(oidp, &freq, 0, req);
438 if (error == 0 && req->newptr != NULL)
439 set_i8254_freq(freq, hz);
443 SYSCTL_PROC(_machdep, OID_AUTO, i8254_freq, CTLTYPE_INT | CTLFLAG_RW,
444 0, sizeof(u_int), sysctl_machdep_i8254_freq, "IU", "");
447 i8254_simple_get_timecount(struct timecounter *tc)
450 return (i8254_max_count - getit());
454 i8254_get_timecount(struct timecounter *tc)
460 eflags = read_eflags();
461 mtx_lock_spin(&clock_lock);
463 /* Select timer0 and latch counter value. */
464 outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH);
466 low = inb(TIMER_CNTR0);
467 high = inb(TIMER_CNTR0);
468 count = i8254_max_count - ((high << 8) | low);
469 if (count < i8254_lastcount ||
470 (!i8254_ticked && (clkintr_pending ||
471 ((count < 20 || (!(eflags & PSL_I) &&
472 count < i8254_max_count / 2u)) &&
473 i8254_pending != NULL && i8254_pending(i8254_intsrc))))) {
475 i8254_offset += i8254_max_count;
477 i8254_lastcount = count;
478 count += i8254_offset;
479 mtx_unlock_spin(&clock_lock);
485 * Attach to the ISA PnP descriptors for the timer
487 static struct isa_pnp_id attimer_ids[] = {
488 { 0x0001d041 /* PNP0100 */, "AT timer" },
493 attimer_probe(device_t dev)
497 result = ISA_PNP_PROBE(device_get_parent(dev), dev, attimer_ids);
504 attimer_attach(device_t dev)
509 static device_method_t attimer_methods[] = {
510 /* Device interface */
511 DEVMETHOD(device_probe, attimer_probe),
512 DEVMETHOD(device_attach, attimer_attach),
513 DEVMETHOD(device_detach, bus_generic_detach),
514 DEVMETHOD(device_shutdown, bus_generic_shutdown),
515 DEVMETHOD(device_suspend, bus_generic_suspend),
516 DEVMETHOD(device_resume, bus_generic_resume),
520 static driver_t attimer_driver = {
526 static devclass_t attimer_devclass;
528 DRIVER_MODULE(attimer, isa, attimer_driver, attimer_devclass, 0, 0);