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 int lapic_allclocks = 1;
97 TUNABLE_INT("machdep.lapic_allclocks", &lapic_allclocks);
99 static struct mtx clock_lock;
100 static struct intsrc *i8254_intsrc;
101 static u_int32_t i8254_lastcount;
102 static u_int32_t i8254_offset;
103 static int (*i8254_pending)(struct intsrc *);
104 static int i8254_ticked;
105 static enum lapic_clock using_lapic_timer = LAPIC_CLOCK_NONE;
107 /* Values for timerX_state: */
109 #define RELEASE_PENDING 1
111 #define ACQUIRE_PENDING 3
113 static u_char timer1_state;
115 static unsigned i8254_get_timecount(struct timecounter *tc);
116 static unsigned i8254_simple_get_timecount(struct timecounter *tc);
117 static void set_i8254_freq(u_int freq, int intr_freq);
119 static struct timecounter i8254_timecounter = {
120 i8254_get_timecount, /* get_timecount */
122 ~0u, /* counter_mask */
129 hardclockintr(struct trapframe *frame)
132 if (PCPU_GET(cpuid) == 0)
133 hardclock(TRAPF_USERMODE(frame), TRAPF_PC(frame));
135 hardclock_cpu(TRAPF_USERMODE(frame));
136 return (FILTER_HANDLED);
140 statclockintr(struct trapframe *frame)
143 return (FILTER_HANDLED);
147 profclockintr(struct trapframe *frame)
150 return (FILTER_HANDLED);
154 clkintr(struct trapframe *frame)
157 if (timecounter->tc_get_timecount == i8254_get_timecount) {
158 mtx_lock_spin(&clock_lock);
162 i8254_offset += i8254_max_count;
166 mtx_unlock_spin(&clock_lock);
168 KASSERT(using_lapic_timer == LAPIC_CLOCK_NONE,
169 ("clk interrupt enabled with lapic timer"));
173 * If the DTrace hooks are configured and a callback function
174 * has been registered, then call it to process the high speed
177 int cpu = PCPU_GET(cpuid);
178 if (lapic_cyclic_clock_func[cpu] != NULL)
179 (*lapic_cyclic_clock_func[cpu])(frame);
184 ipi_all_but_self(IPI_HARDCLOCK);
186 hardclockintr(frame);
187 return (FILTER_HANDLED);
191 timer_spkr_acquire(void)
195 mode = TIMER_SEL1 | TIMER_SQWAVE | TIMER_16BIT;
197 if (timer1_state != RELEASED)
199 timer1_state = ACQUIRED;
202 * This access to the timer registers is as atomic as possible
203 * because it is a single instruction. We could do better if we
204 * knew the rate. Use of splclock() limits glitches to 10-100us,
205 * and this is probably good enough for timer2, so we aren't as
206 * careful with it as with timer0.
208 outb(TIMER_MODE, TIMER_SEL1 | (mode & 0x3f));
209 ppi_spkr_on(); /* enable counter1 output to speaker */
215 timer_spkr_release(void)
218 if (timer1_state != ACQUIRED)
220 timer1_state = RELEASED;
221 outb(TIMER_MODE, TIMER_SEL1 | TIMER_SQWAVE | TIMER_16BIT);
222 ppi_spkr_off(); /* disable counter1 output to speaker */
227 timer_spkr_setfreq(int freq)
230 freq = i8254_freq / freq;
231 mtx_lock_spin(&clock_lock);
232 outb(TIMER_CNTR1, (freq) & 0xff);
233 outb(TIMER_CNTR1, (freq) >> 8);
234 mtx_unlock_spin(&clock_lock);
243 mtx_lock_spin(&clock_lock);
245 /* Select timer0 and latch counter value. */
246 outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH);
248 low = inb(TIMER_CNTR0);
249 high = inb(TIMER_CNTR0);
251 mtx_unlock_spin(&clock_lock);
252 return ((high << 8) | low);
256 * Wait "n" microseconds.
257 * Relies on timer 1 counting down from (i8254_freq / hz)
258 * Note: timer had better have been programmed before this is first used!
263 int delta, prev_tick, tick, ticks_left;
268 static int state = 0;
272 for (n1 = 1; n1 <= 10000000; n1 *= 10)
277 printf("DELAY(%d)...", n);
280 * Read the counter first, so that the rest of the setup overhead is
281 * counted. Guess the initial overhead is 20 usec (on most systems it
282 * takes about 1.5 usec for each of the i/o's in getit(). The loop
283 * takes about 6 usec on a 486/33 and 13 usec on a 386/20. The
284 * multiplications and divisions to scale the count take a while).
286 * However, if ddb is active then use a fake counter since reading
287 * the i8254 counter involves acquiring a lock. ddb must not do
288 * locking for many reasons, but it calls here for at least atkbd
297 n -= 0; /* XXX actually guess no initial overhead */
299 * Calculate (n * (i8254_freq / 1e6)) without using floating point
300 * and without any avoidable overflows.
306 * Use fixed point to avoid a slow division by 1000000.
307 * 39099 = 1193182 * 2^15 / 10^6 rounded to nearest.
308 * 2^15 is the first power of 2 that gives exact results
309 * for n between 0 and 256.
311 ticks_left = ((u_int)n * 39099 + (1 << 15) - 1) >> 15;
314 * Don't bother using fixed point, although gcc-2.7.2
315 * generates particularly poor code for the long long
316 * division, since even the slow way will complete long
317 * before the delay is up (unless we're interrupted).
319 ticks_left = ((u_int)n * (long long)i8254_freq + 999999)
322 while (ticks_left > 0) {
326 tick = prev_tick - 1;
328 tick = i8254_max_count;
335 delta = prev_tick - tick;
338 delta += i8254_max_count;
340 * Guard against i8254_max_count being wrong.
341 * This shouldn't happen in normal operation,
342 * but it may happen if set_i8254_freq() is
352 printf(" %d calls to getit() at %d usec each\n",
353 getit_calls, (n + 5) / getit_calls);
358 set_i8254_freq(u_int freq, int intr_freq)
360 int new_i8254_real_max_count;
362 i8254_timecounter.tc_frequency = freq;
363 mtx_lock_spin(&clock_lock);
365 if (using_lapic_timer != LAPIC_CLOCK_NONE)
366 new_i8254_real_max_count = 0x10000;
368 new_i8254_real_max_count = TIMER_DIV(intr_freq);
369 if (new_i8254_real_max_count != i8254_real_max_count) {
370 i8254_real_max_count = new_i8254_real_max_count;
371 if (i8254_real_max_count == 0x10000)
372 i8254_max_count = 0xffff;
374 i8254_max_count = i8254_real_max_count;
375 outb(TIMER_MODE, TIMER_SEL0 | TIMER_RATEGEN | TIMER_16BIT);
376 outb(TIMER_CNTR0, i8254_real_max_count & 0xff);
377 outb(TIMER_CNTR0, i8254_real_max_count >> 8);
379 mtx_unlock_spin(&clock_lock);
386 mtx_lock_spin(&clock_lock);
387 outb(TIMER_MODE, TIMER_SEL0 | TIMER_RATEGEN | TIMER_16BIT);
388 outb(TIMER_CNTR0, i8254_real_max_count & 0xff);
389 outb(TIMER_CNTR0, i8254_real_max_count >> 8);
390 mtx_unlock_spin(&clock_lock);
394 * Restore all the timers non-atomically (XXX: should be atomically).
396 * This function is called from pmtimer_resume() to restore all the timers.
397 * This should not be necessary, but there are broken laptops that do not
398 * restore all the timers on resume.
404 i8254_restore(); /* restore i8254_freq and hz */
407 /* This is separate from startrtclock() so that it can be called early. */
412 mtx_init(&clock_lock, "clk", NULL, MTX_SPIN | MTX_NOPROFILE);
414 if (pc98_machine_type & M_8M)
415 i8254_freq = 1996800L; /* 1.9968 MHz */
417 i8254_freq = 2457600L; /* 2.4576 MHz */
419 set_i8254_freq(i8254_freq, hz);
426 set_i8254_freq(i8254_freq, hz);
427 tc_init(&i8254_timecounter);
433 * Start both clocks running.
438 #if defined(DEV_APIC)
439 enum lapic_clock tlsca;
441 tlsca = lapic_allclocks == 0 ? LAPIC_CLOCK_HARDCLOCK : LAPIC_CLOCK_ALL;
442 using_lapic_timer = lapic_setup_clock(tlsca);
445 * If we aren't using the local APIC timer to drive the kernel
446 * clocks, setup the interrupt handler for the 8254 timer 0 so
447 * that it can drive hardclock(). Otherwise, change the 8254
448 * timecounter to user a simpler algorithm.
450 if (using_lapic_timer == LAPIC_CLOCK_NONE) {
451 intr_add_handler("clk", 0, (driver_filter_t *)clkintr, NULL,
452 NULL, INTR_TYPE_CLK, NULL);
453 i8254_intsrc = intr_lookup_source(0);
454 if (i8254_intsrc != NULL)
456 i8254_intsrc->is_pic->pic_source_pending;
458 i8254_timecounter.tc_get_timecount =
459 i8254_simple_get_timecount;
460 i8254_timecounter.tc_counter_mask = 0xffff;
461 set_i8254_freq(i8254_freq, hz);
468 cpu_startprofclock(void)
473 cpu_stopprofclock(void)
478 sysctl_machdep_i8254_freq(SYSCTL_HANDLER_ARGS)
484 * Use `i8254' instead of `timer' in external names because `timer'
485 * is is too generic. Should use it everywhere.
488 error = sysctl_handle_int(oidp, &freq, 0, req);
489 if (error == 0 && req->newptr != NULL)
490 set_i8254_freq(freq, hz);
494 SYSCTL_PROC(_machdep, OID_AUTO, i8254_freq, CTLTYPE_INT | CTLFLAG_RW,
495 0, sizeof(u_int), sysctl_machdep_i8254_freq, "IU", "");
498 i8254_simple_get_timecount(struct timecounter *tc)
501 return (i8254_max_count - getit());
505 i8254_get_timecount(struct timecounter *tc)
511 eflags = read_eflags();
512 mtx_lock_spin(&clock_lock);
514 /* Select timer0 and latch counter value. */
515 outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH);
517 low = inb(TIMER_CNTR0);
518 high = inb(TIMER_CNTR0);
519 count = i8254_max_count - ((high << 8) | low);
520 if (count < i8254_lastcount ||
521 (!i8254_ticked && (clkintr_pending ||
522 ((count < 20 || (!(eflags & PSL_I) &&
523 count < i8254_max_count / 2u)) &&
524 i8254_pending != NULL && i8254_pending(i8254_intsrc))))) {
526 i8254_offset += i8254_max_count;
528 i8254_lastcount = count;
529 count += i8254_offset;
530 mtx_unlock_spin(&clock_lock);
536 * Attach to the ISA PnP descriptors for the timer
538 static struct isa_pnp_id attimer_ids[] = {
539 { 0x0001d041 /* PNP0100 */, "AT timer" },
544 attimer_probe(device_t dev)
548 result = ISA_PNP_PROBE(device_get_parent(dev), dev, attimer_ids);
555 attimer_attach(device_t dev)
560 static device_method_t attimer_methods[] = {
561 /* Device interface */
562 DEVMETHOD(device_probe, attimer_probe),
563 DEVMETHOD(device_attach, attimer_attach),
564 DEVMETHOD(device_detach, bus_generic_detach),
565 DEVMETHOD(device_shutdown, bus_generic_shutdown),
566 DEVMETHOD(device_suspend, bus_generic_suspend),
567 DEVMETHOD(device_resume, bus_generic_resume),
571 static driver_t attimer_driver = {
577 static devclass_t attimer_devclass;
579 DRIVER_MODULE(attimer, isa, attimer_driver, attimer_devclass, 0, 0);