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>
71 #include <machine/apicvar.h>
73 #include <machine/ppireg.h>
74 #include <machine/timerreg.h>
75 #include <machine/smp.h>
77 #include <pc98/pc98/pc98_machdep.h>
79 #include <pc98/cbus/cbus.h>
80 #include <isa/isavar.h>
84 #include <sys/dtrace_bsd.h>
87 #define TIMER_DIV(x) ((i8254_freq + (x) / 2) / (x))
91 #define TIMER_FREQ 2457600
93 u_int i8254_freq = TIMER_FREQ;
94 TUNABLE_INT("hw.i8254.freq", &i8254_freq);
96 static int i8254_real_max_count;
98 static struct mtx clock_lock;
99 static struct intsrc *i8254_intsrc;
100 static u_int32_t i8254_lastcount;
101 static u_int32_t i8254_offset;
102 static int (*i8254_pending)(struct intsrc *);
103 static int i8254_ticked;
104 static int using_lapic_timer;
106 /* Values for timerX_state: */
108 #define RELEASE_PENDING 1
110 #define ACQUIRE_PENDING 3
112 static u_char timer1_state;
114 static unsigned i8254_get_timecount(struct timecounter *tc);
115 static unsigned i8254_simple_get_timecount(struct timecounter *tc);
116 static void set_i8254_freq(u_int freq, int intr_freq);
118 static struct timecounter i8254_timecounter = {
119 i8254_get_timecount, /* get_timecount */
121 ~0u, /* counter_mask */
128 hardclockintr(struct trapframe *frame)
131 if (PCPU_GET(cpuid) == 0)
132 hardclock(TRAPF_USERMODE(frame), TRAPF_PC(frame));
134 hardclock_cpu(TRAPF_USERMODE(frame));
135 return (FILTER_HANDLED);
139 statclockintr(struct trapframe *frame)
142 return (FILTER_HANDLED);
146 profclockintr(struct trapframe *frame)
149 return (FILTER_HANDLED);
153 clkintr(struct trapframe *frame)
156 if (timecounter->tc_get_timecount == i8254_get_timecount) {
157 mtx_lock_spin(&clock_lock);
161 i8254_offset += i8254_max_count;
165 mtx_unlock_spin(&clock_lock);
167 KASSERT(!using_lapic_timer, ("clk interrupt enabled with lapic timer"));
171 * If the DTrace hooks are configured and a callback function
172 * has been registered, then call it to process the high speed
175 int cpu = PCPU_GET(cpuid);
176 if (lapic_cyclic_clock_func[cpu] != NULL)
177 (*lapic_cyclic_clock_func[cpu])(frame);
182 ipi_all_but_self(IPI_HARDCLOCK);
184 hardclockintr(frame);
185 return (FILTER_HANDLED);
189 timer_spkr_acquire(void)
193 mode = TIMER_SEL1 | TIMER_SQWAVE | TIMER_16BIT;
195 if (timer1_state != RELEASED)
197 timer1_state = ACQUIRED;
200 * This access to the timer registers is as atomic as possible
201 * because it is a single instruction. We could do better if we
202 * knew the rate. Use of splclock() limits glitches to 10-100us,
203 * and this is probably good enough for timer2, so we aren't as
204 * careful with it as with timer0.
206 outb(TIMER_MODE, TIMER_SEL1 | (mode & 0x3f));
207 ppi_spkr_on(); /* enable counter1 output to speaker */
213 timer_spkr_release(void)
216 if (timer1_state != ACQUIRED)
218 timer1_state = RELEASED;
219 outb(TIMER_MODE, TIMER_SEL1 | TIMER_SQWAVE | TIMER_16BIT);
220 ppi_spkr_off(); /* disable counter1 output to speaker */
225 timer_spkr_setfreq(int freq)
228 freq = i8254_freq / freq;
229 mtx_lock_spin(&clock_lock);
230 outb(TIMER_CNTR1, (freq) & 0xff);
231 outb(TIMER_CNTR1, (freq) >> 8);
232 mtx_unlock_spin(&clock_lock);
241 mtx_lock_spin(&clock_lock);
243 /* Select timer0 and latch counter value. */
244 outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH);
246 low = inb(TIMER_CNTR0);
247 high = inb(TIMER_CNTR0);
249 mtx_unlock_spin(&clock_lock);
250 return ((high << 8) | low);
254 * Wait "n" microseconds.
255 * Relies on timer 1 counting down from (i8254_freq / hz)
256 * Note: timer had better have been programmed before this is first used!
261 int delta, prev_tick, tick, ticks_left;
266 static int state = 0;
270 for (n1 = 1; n1 <= 10000000; n1 *= 10)
275 printf("DELAY(%d)...", n);
278 * Read the counter first, so that the rest of the setup overhead is
279 * counted. Guess the initial overhead is 20 usec (on most systems it
280 * takes about 1.5 usec for each of the i/o's in getit(). The loop
281 * takes about 6 usec on a 486/33 and 13 usec on a 386/20. The
282 * multiplications and divisions to scale the count take a while).
284 * However, if ddb is active then use a fake counter since reading
285 * the i8254 counter involves acquiring a lock. ddb must not do
286 * locking for many reasons, but it calls here for at least atkbd
295 n -= 0; /* XXX actually guess no initial overhead */
297 * Calculate (n * (i8254_freq / 1e6)) without using floating point
298 * and without any avoidable overflows.
304 * Use fixed point to avoid a slow division by 1000000.
305 * 39099 = 1193182 * 2^15 / 10^6 rounded to nearest.
306 * 2^15 is the first power of 2 that gives exact results
307 * for n between 0 and 256.
309 ticks_left = ((u_int)n * 39099 + (1 << 15) - 1) >> 15;
312 * Don't bother using fixed point, although gcc-2.7.2
313 * generates particularly poor code for the long long
314 * division, since even the slow way will complete long
315 * before the delay is up (unless we're interrupted).
317 ticks_left = ((u_int)n * (long long)i8254_freq + 999999)
320 while (ticks_left > 0) {
324 tick = prev_tick - 1;
326 tick = i8254_max_count;
333 delta = prev_tick - tick;
336 delta += i8254_max_count;
338 * Guard against i8254_max_count being wrong.
339 * This shouldn't happen in normal operation,
340 * but it may happen if set_i8254_freq() is
350 printf(" %d calls to getit() at %d usec each\n",
351 getit_calls, (n + 5) / getit_calls);
356 set_i8254_freq(u_int freq, int intr_freq)
358 int new_i8254_real_max_count;
360 i8254_timecounter.tc_frequency = freq;
361 mtx_lock_spin(&clock_lock);
363 if (using_lapic_timer)
364 new_i8254_real_max_count = 0x10000;
366 new_i8254_real_max_count = TIMER_DIV(intr_freq);
367 if (new_i8254_real_max_count != i8254_real_max_count) {
368 i8254_real_max_count = new_i8254_real_max_count;
369 if (i8254_real_max_count == 0x10000)
370 i8254_max_count = 0xffff;
372 i8254_max_count = i8254_real_max_count;
373 outb(TIMER_MODE, TIMER_SEL0 | TIMER_RATEGEN | TIMER_16BIT);
374 outb(TIMER_CNTR0, i8254_real_max_count & 0xff);
375 outb(TIMER_CNTR0, i8254_real_max_count >> 8);
377 mtx_unlock_spin(&clock_lock);
384 mtx_lock_spin(&clock_lock);
385 outb(TIMER_MODE, TIMER_SEL0 | TIMER_RATEGEN | TIMER_16BIT);
386 outb(TIMER_CNTR0, i8254_real_max_count & 0xff);
387 outb(TIMER_CNTR0, i8254_real_max_count >> 8);
388 mtx_unlock_spin(&clock_lock);
392 * Restore all the timers non-atomically (XXX: should be atomically).
394 * This function is called from pmtimer_resume() to restore all the timers.
395 * This should not be necessary, but there are broken laptops that do not
396 * restore all the timers on resume.
402 i8254_restore(); /* restore i8254_freq and hz */
405 /* This is separate from startrtclock() so that it can be called early. */
410 mtx_init(&clock_lock, "clk", NULL, MTX_SPIN | MTX_NOPROFILE);
412 if (pc98_machine_type & M_8M)
413 i8254_freq = 1996800L; /* 1.9968 MHz */
415 i8254_freq = 2457600L; /* 2.4576 MHz */
417 set_i8254_freq(i8254_freq, hz);
424 set_i8254_freq(i8254_freq, hz);
425 tc_init(&i8254_timecounter);
431 * Start both clocks running.
438 using_lapic_timer = lapic_setup_clock();
441 * If we aren't using the local APIC timer to drive the kernel
442 * clocks, setup the interrupt handler for the 8254 timer 0 so
443 * that it can drive hardclock(). Otherwise, change the 8254
444 * timecounter to user a simpler algorithm.
446 if (!using_lapic_timer) {
447 intr_add_handler("clk", 0, (driver_filter_t *)clkintr, NULL,
448 NULL, INTR_TYPE_CLK, NULL);
449 i8254_intsrc = intr_lookup_source(0);
450 if (i8254_intsrc != NULL)
452 i8254_intsrc->is_pic->pic_source_pending;
454 i8254_timecounter.tc_get_timecount =
455 i8254_simple_get_timecount;
456 i8254_timecounter.tc_counter_mask = 0xffff;
457 set_i8254_freq(i8254_freq, hz);
464 cpu_startprofclock(void)
469 cpu_stopprofclock(void)
474 sysctl_machdep_i8254_freq(SYSCTL_HANDLER_ARGS)
480 * Use `i8254' instead of `timer' in external names because `timer'
481 * is is too generic. Should use it everywhere.
484 error = sysctl_handle_int(oidp, &freq, 0, req);
485 if (error == 0 && req->newptr != NULL)
486 set_i8254_freq(freq, hz);
490 SYSCTL_PROC(_machdep, OID_AUTO, i8254_freq, CTLTYPE_INT | CTLFLAG_RW,
491 0, sizeof(u_int), sysctl_machdep_i8254_freq, "IU", "");
494 i8254_simple_get_timecount(struct timecounter *tc)
497 return (i8254_max_count - getit());
501 i8254_get_timecount(struct timecounter *tc)
507 eflags = read_eflags();
508 mtx_lock_spin(&clock_lock);
510 /* Select timer0 and latch counter value. */
511 outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH);
513 low = inb(TIMER_CNTR0);
514 high = inb(TIMER_CNTR0);
515 count = i8254_max_count - ((high << 8) | low);
516 if (count < i8254_lastcount ||
517 (!i8254_ticked && (clkintr_pending ||
518 ((count < 20 || (!(eflags & PSL_I) &&
519 count < i8254_max_count / 2u)) &&
520 i8254_pending != NULL && i8254_pending(i8254_intsrc))))) {
522 i8254_offset += i8254_max_count;
524 i8254_lastcount = count;
525 count += i8254_offset;
526 mtx_unlock_spin(&clock_lock);
532 * Attach to the ISA PnP descriptors for the timer
534 static struct isa_pnp_id attimer_ids[] = {
535 { 0x0001d041 /* PNP0100 */, "AT timer" },
540 attimer_probe(device_t dev)
544 result = ISA_PNP_PROBE(device_get_parent(dev), dev, attimer_ids);
551 attimer_attach(device_t dev)
556 static device_method_t attimer_methods[] = {
557 /* Device interface */
558 DEVMETHOD(device_probe, attimer_probe),
559 DEVMETHOD(device_attach, attimer_attach),
560 DEVMETHOD(device_detach, bus_generic_detach),
561 DEVMETHOD(device_shutdown, bus_generic_shutdown),
562 DEVMETHOD(device_suspend, bus_generic_suspend),
563 DEVMETHOD(device_resume, bus_generic_resume),
567 static driver_t attimer_driver = {
573 static devclass_t attimer_devclass;
575 DRIVER_MODULE(attimer, isa, attimer_driver, attimer_devclass, 0, 0);