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"
48 #include "opt_kdtrace.h"
52 #include <sys/param.h>
53 #include <sys/systm.h>
57 #include <sys/mutex.h>
59 #include <sys/timetc.h>
60 #include <sys/kernel.h>
61 #include <sys/module.h>
63 #include <sys/sysctl.h>
65 #include <machine/clock.h>
66 #include <machine/cpu.h>
67 #include <machine/frame.h>
68 #include <machine/intr_machdep.h>
69 #include <machine/md_var.h>
70 #include <machine/apicvar.h>
71 #include <machine/ppireg.h>
72 #include <machine/timerreg.h>
73 #include <machine/smp.h>
75 #include <pc98/pc98/pc98_machdep.h>
77 #include <pc98/cbus/cbus.h>
78 #include <isa/isavar.h>
82 #include <sys/dtrace_bsd.h>
85 #define TIMER_DIV(x) ((i8254_freq + (x) / 2) / (x))
89 #define TIMER_FREQ 2457600
91 u_int i8254_freq = TIMER_FREQ;
92 TUNABLE_INT("hw.i8254.freq", &i8254_freq);
94 static int i8254_real_max_count;
96 static struct mtx clock_lock;
97 static struct intsrc *i8254_intsrc;
98 static u_int32_t i8254_lastcount;
99 static u_int32_t i8254_offset;
100 static int (*i8254_pending)(struct intsrc *);
101 static int i8254_ticked;
102 static enum lapic_clock using_lapic_timer = LAPIC_CLOCK_NONE;
104 /* Values for timerX_state: */
106 #define RELEASE_PENDING 1
108 #define ACQUIRE_PENDING 3
110 static u_char timer1_state;
112 static unsigned i8254_get_timecount(struct timecounter *tc);
113 static unsigned i8254_simple_get_timecount(struct timecounter *tc);
114 static void set_i8254_freq(u_int freq, int intr_freq);
116 static struct timecounter i8254_timecounter = {
117 i8254_get_timecount, /* get_timecount */
119 ~0u, /* counter_mask */
126 hardclockintr(struct trapframe *frame)
129 if (PCPU_GET(cpuid) == 0)
130 hardclock(TRAPF_USERMODE(frame), TRAPF_PC(frame));
132 hardclock_cpu(TRAPF_USERMODE(frame));
133 return (FILTER_HANDLED);
137 statclockintr(struct trapframe *frame)
140 return (FILTER_HANDLED);
144 profclockintr(struct trapframe *frame)
147 return (FILTER_HANDLED);
151 clkintr(struct trapframe *frame)
154 if (timecounter->tc_get_timecount == i8254_get_timecount) {
155 mtx_lock_spin(&clock_lock);
159 i8254_offset += i8254_max_count;
163 mtx_unlock_spin(&clock_lock);
165 KASSERT(using_lapic_timer == LAPIC_CLOCK_NONE,
166 ("clk interrupt enabled with lapic timer"));
170 * If the DTrace hooks are configured and a callback function
171 * has been registered, then call it to process the high speed
174 int cpu = PCPU_GET(cpuid);
175 if (lapic_cyclic_clock_func[cpu] != NULL)
176 (*lapic_cyclic_clock_func[cpu])(frame);
181 ipi_all_but_self(IPI_HARDCLOCK);
183 hardclockintr(frame);
184 return (FILTER_HANDLED);
188 timer_spkr_acquire(void)
192 mode = TIMER_SEL1 | TIMER_SQWAVE | TIMER_16BIT;
194 if (timer1_state != RELEASED)
196 timer1_state = ACQUIRED;
199 * This access to the timer registers is as atomic as possible
200 * because it is a single instruction. We could do better if we
201 * knew the rate. Use of splclock() limits glitches to 10-100us,
202 * and this is probably good enough for timer2, so we aren't as
203 * careful with it as with timer0.
205 outb(TIMER_MODE, TIMER_SEL1 | (mode & 0x3f));
206 ppi_spkr_on(); /* enable counter1 output to speaker */
212 timer_spkr_release(void)
215 if (timer1_state != ACQUIRED)
217 timer1_state = RELEASED;
218 outb(TIMER_MODE, TIMER_SEL1 | TIMER_SQWAVE | TIMER_16BIT);
219 ppi_spkr_off(); /* disable counter1 output to speaker */
224 timer_spkr_setfreq(int freq)
227 freq = i8254_freq / freq;
228 mtx_lock_spin(&clock_lock);
229 outb(TIMER_CNTR1, (freq) & 0xff);
230 outb(TIMER_CNTR1, (freq) >> 8);
231 mtx_unlock_spin(&clock_lock);
240 mtx_lock_spin(&clock_lock);
242 /* Select timer0 and latch counter value. */
243 outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH);
245 low = inb(TIMER_CNTR0);
246 high = inb(TIMER_CNTR0);
248 mtx_unlock_spin(&clock_lock);
249 return ((high << 8) | low);
253 * Wait "n" microseconds.
254 * Relies on timer 1 counting down from (i8254_freq / hz)
255 * Note: timer had better have been programmed before this is first used!
260 int delta, prev_tick, tick, ticks_left;
265 static int state = 0;
269 for (n1 = 1; n1 <= 10000000; n1 *= 10)
274 printf("DELAY(%d)...", n);
277 * Read the counter first, so that the rest of the setup overhead is
278 * counted. Guess the initial overhead is 20 usec (on most systems it
279 * takes about 1.5 usec for each of the i/o's in getit(). The loop
280 * takes about 6 usec on a 486/33 and 13 usec on a 386/20. The
281 * multiplications and divisions to scale the count take a while).
283 * However, if ddb is active then use a fake counter since reading
284 * the i8254 counter involves acquiring a lock. ddb must not do
285 * locking for many reasons, but it calls here for at least atkbd
294 n -= 0; /* XXX actually guess no initial overhead */
296 * Calculate (n * (i8254_freq / 1e6)) without using floating point
297 * and without any avoidable overflows.
303 * Use fixed point to avoid a slow division by 1000000.
304 * 39099 = 1193182 * 2^15 / 10^6 rounded to nearest.
305 * 2^15 is the first power of 2 that gives exact results
306 * for n between 0 and 256.
308 ticks_left = ((u_int)n * 39099 + (1 << 15) - 1) >> 15;
311 * Don't bother using fixed point, although gcc-2.7.2
312 * generates particularly poor code for the long long
313 * division, since even the slow way will complete long
314 * before the delay is up (unless we're interrupted).
316 ticks_left = ((u_int)n * (long long)i8254_freq + 999999)
319 while (ticks_left > 0) {
323 tick = prev_tick - 1;
325 tick = i8254_max_count;
332 delta = prev_tick - tick;
335 delta += i8254_max_count;
337 * Guard against i8254_max_count being wrong.
338 * This shouldn't happen in normal operation,
339 * but it may happen if set_i8254_freq() is
349 printf(" %d calls to getit() at %d usec each\n",
350 getit_calls, (n + 5) / getit_calls);
355 set_i8254_freq(u_int freq, int intr_freq)
357 int new_i8254_real_max_count;
359 i8254_timecounter.tc_frequency = freq;
360 mtx_lock_spin(&clock_lock);
362 if (using_lapic_timer != LAPIC_CLOCK_NONE)
363 new_i8254_real_max_count = 0x10000;
365 new_i8254_real_max_count = TIMER_DIV(intr_freq);
366 if (new_i8254_real_max_count != i8254_real_max_count) {
367 i8254_real_max_count = new_i8254_real_max_count;
368 if (i8254_real_max_count == 0x10000)
369 i8254_max_count = 0xffff;
371 i8254_max_count = i8254_real_max_count;
372 outb(TIMER_MODE, TIMER_SEL0 | TIMER_RATEGEN | TIMER_16BIT);
373 outb(TIMER_CNTR0, i8254_real_max_count & 0xff);
374 outb(TIMER_CNTR0, i8254_real_max_count >> 8);
376 mtx_unlock_spin(&clock_lock);
383 mtx_lock_spin(&clock_lock);
384 outb(TIMER_MODE, TIMER_SEL0 | TIMER_RATEGEN | TIMER_16BIT);
385 outb(TIMER_CNTR0, i8254_real_max_count & 0xff);
386 outb(TIMER_CNTR0, i8254_real_max_count >> 8);
387 mtx_unlock_spin(&clock_lock);
391 * Restore all the timers non-atomically (XXX: should be atomically).
393 * This function is called from pmtimer_resume() to restore all the timers.
394 * This should not be necessary, but there are broken laptops that do not
395 * restore all the timers on resume.
401 i8254_restore(); /* restore i8254_freq and hz */
404 /* This is separate from startrtclock() so that it can be called early. */
409 mtx_init(&clock_lock, "clk", NULL, MTX_SPIN | MTX_NOPROFILE);
411 if (pc98_machine_type & M_8M)
412 i8254_freq = 1996800L; /* 1.9968 MHz */
414 i8254_freq = 2457600L; /* 2.4576 MHz */
416 set_i8254_freq(i8254_freq, hz);
423 set_i8254_freq(i8254_freq, hz);
424 tc_init(&i8254_timecounter);
430 * Start both clocks running.
437 using_lapic_timer = lapic_setup_clock();
440 * If we aren't using the local APIC timer to drive the kernel
441 * clocks, setup the interrupt handler for the 8254 timer 0 so
442 * that it can drive hardclock(). Otherwise, change the 8254
443 * timecounter to user a simpler algorithm.
445 if (using_lapic_timer == LAPIC_CLOCK_NONE) {
446 intr_add_handler("clk", 0, (driver_filter_t *)clkintr, NULL,
447 NULL, INTR_TYPE_CLK, NULL);
448 i8254_intsrc = intr_lookup_source(0);
449 if (i8254_intsrc != NULL)
451 i8254_intsrc->is_pic->pic_source_pending;
453 i8254_timecounter.tc_get_timecount =
454 i8254_simple_get_timecount;
455 i8254_timecounter.tc_counter_mask = 0xffff;
456 set_i8254_freq(i8254_freq, hz);
463 cpu_startprofclock(void)
468 cpu_stopprofclock(void)
473 sysctl_machdep_i8254_freq(SYSCTL_HANDLER_ARGS)
479 * Use `i8254' instead of `timer' in external names because `timer'
480 * is is too generic. Should use it everywhere.
483 error = sysctl_handle_int(oidp, &freq, 0, req);
484 if (error == 0 && req->newptr != NULL)
485 set_i8254_freq(freq, hz);
489 SYSCTL_PROC(_machdep, OID_AUTO, i8254_freq, CTLTYPE_INT | CTLFLAG_RW,
490 0, sizeof(u_int), sysctl_machdep_i8254_freq, "IU", "");
493 i8254_simple_get_timecount(struct timecounter *tc)
496 return (i8254_max_count - getit());
500 i8254_get_timecount(struct timecounter *tc)
506 eflags = read_eflags();
507 mtx_lock_spin(&clock_lock);
509 /* Select timer0 and latch counter value. */
510 outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH);
512 low = inb(TIMER_CNTR0);
513 high = inb(TIMER_CNTR0);
514 count = i8254_max_count - ((high << 8) | low);
515 if (count < i8254_lastcount ||
516 (!i8254_ticked && (clkintr_pending ||
517 ((count < 20 || (!(eflags & PSL_I) &&
518 count < i8254_max_count / 2u)) &&
519 i8254_pending != NULL && i8254_pending(i8254_intsrc))))) {
521 i8254_offset += i8254_max_count;
523 i8254_lastcount = count;
524 count += i8254_offset;
525 mtx_unlock_spin(&clock_lock);
531 * Attach to the ISA PnP descriptors for the timer
533 static struct isa_pnp_id attimer_ids[] = {
534 { 0x0001d041 /* PNP0100 */, "AT timer" },
539 attimer_probe(device_t dev)
543 result = ISA_PNP_PROBE(device_get_parent(dev), dev, attimer_ids);
550 attimer_attach(device_t dev)
555 static device_method_t attimer_methods[] = {
556 /* Device interface */
557 DEVMETHOD(device_probe, attimer_probe),
558 DEVMETHOD(device_attach, attimer_attach),
559 DEVMETHOD(device_detach, bus_generic_detach),
560 DEVMETHOD(device_shutdown, bus_generic_shutdown),
561 DEVMETHOD(device_suspend, bus_generic_suspend),
562 DEVMETHOD(device_resume, bus_generic_resume),
566 static driver_t attimer_driver = {
572 static devclass_t attimer_devclass;
574 DRIVER_MODULE(attimer, isa, attimer_driver, attimer_devclass, 0, 0);