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>
62 #include <sys/sysctl.h>
64 #include <machine/clock.h>
65 #include <machine/cpu.h>
66 #include <machine/frame.h>
67 #include <machine/intr_machdep.h>
68 #include <machine/md_var.h>
70 #include <machine/apicvar.h>
72 #include <machine/ppireg.h>
73 #include <machine/timerreg.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))
88 int statclock_disable;
90 #define TIMER_FREQ 2457600
92 u_int i8254_freq = TIMER_FREQ;
93 TUNABLE_INT("hw.i8254.freq", &i8254_freq);
95 static int i8254_real_max_count;
97 static struct mtx clock_lock;
98 static struct intsrc *i8254_intsrc;
99 static u_int32_t i8254_lastcount;
100 static u_int32_t i8254_offset;
101 static int (*i8254_pending)(struct intsrc *);
102 static int i8254_ticked;
103 static int using_lapic_timer;
105 /* Values for timerX_state: */
107 #define RELEASE_PENDING 1
109 #define ACQUIRE_PENDING 3
111 static u_char timer1_state;
113 static unsigned i8254_get_timecount(struct timecounter *tc);
114 static unsigned i8254_simple_get_timecount(struct timecounter *tc);
115 static void set_i8254_freq(u_int freq, int intr_freq);
117 static struct timecounter i8254_timecounter = {
118 i8254_get_timecount, /* get_timecount */
120 ~0u, /* counter_mask */
127 clkintr(struct trapframe *frame)
130 if (timecounter->tc_get_timecount == i8254_get_timecount) {
131 mtx_lock_spin(&clock_lock);
135 i8254_offset += i8254_max_count;
139 mtx_unlock_spin(&clock_lock);
141 KASSERT(!using_lapic_timer, ("clk interrupt enabled with lapic timer"));
145 * If the DTrace hooks are configured and a callback function
146 * has been registered, then call it to process the high speed
149 int cpu = PCPU_GET(cpuid);
150 if (lapic_cyclic_clock_func[cpu] != NULL)
151 (*lapic_cyclic_clock_func[cpu])(frame);
154 hardclock(TRAPF_USERMODE(frame), TRAPF_PC(frame));
155 return (FILTER_HANDLED);
159 timer_spkr_acquire(void)
163 mode = TIMER_SEL1 | TIMER_SQWAVE | TIMER_16BIT;
165 if (timer1_state != RELEASED)
167 timer1_state = ACQUIRED;
170 * This access to the timer registers is as atomic as possible
171 * because it is a single instruction. We could do better if we
172 * knew the rate. Use of splclock() limits glitches to 10-100us,
173 * and this is probably good enough for timer2, so we aren't as
174 * careful with it as with timer0.
176 outb(TIMER_MODE, TIMER_SEL1 | (mode & 0x3f));
177 ppi_spkr_on(); /* enable counter1 output to speaker */
183 timer_spkr_release(void)
186 if (timer1_state != ACQUIRED)
188 timer1_state = RELEASED;
189 outb(TIMER_MODE, TIMER_SEL1 | TIMER_SQWAVE | TIMER_16BIT);
190 ppi_spkr_off(); /* disable counter1 output to speaker */
195 timer_spkr_setfreq(int freq)
198 freq = i8254_freq / freq;
199 mtx_lock_spin(&clock_lock);
200 outb(TIMER_CNTR1, (freq) & 0xff);
201 outb(TIMER_CNTR1, (freq) >> 8);
202 mtx_unlock_spin(&clock_lock);
211 mtx_lock_spin(&clock_lock);
213 /* Select timer0 and latch counter value. */
214 outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH);
216 low = inb(TIMER_CNTR0);
217 high = inb(TIMER_CNTR0);
219 mtx_unlock_spin(&clock_lock);
220 return ((high << 8) | low);
224 * Wait "n" microseconds.
225 * Relies on timer 1 counting down from (i8254_freq / hz)
226 * Note: timer had better have been programmed before this is first used!
231 int delta, prev_tick, tick, ticks_left;
236 static int state = 0;
240 for (n1 = 1; n1 <= 10000000; n1 *= 10)
245 printf("DELAY(%d)...", n);
248 * Read the counter first, so that the rest of the setup overhead is
249 * counted. Guess the initial overhead is 20 usec (on most systems it
250 * takes about 1.5 usec for each of the i/o's in getit(). The loop
251 * takes about 6 usec on a 486/33 and 13 usec on a 386/20. The
252 * multiplications and divisions to scale the count take a while).
254 * However, if ddb is active then use a fake counter since reading
255 * the i8254 counter involves acquiring a lock. ddb must not do
256 * locking for many reasons, but it calls here for at least atkbd
265 n -= 0; /* XXX actually guess no initial overhead */
267 * Calculate (n * (i8254_freq / 1e6)) without using floating point
268 * and without any avoidable overflows.
274 * Use fixed point to avoid a slow division by 1000000.
275 * 39099 = 1193182 * 2^15 / 10^6 rounded to nearest.
276 * 2^15 is the first power of 2 that gives exact results
277 * for n between 0 and 256.
279 ticks_left = ((u_int)n * 39099 + (1 << 15) - 1) >> 15;
282 * Don't bother using fixed point, although gcc-2.7.2
283 * generates particularly poor code for the long long
284 * division, since even the slow way will complete long
285 * before the delay is up (unless we're interrupted).
287 ticks_left = ((u_int)n * (long long)i8254_freq + 999999)
290 while (ticks_left > 0) {
294 tick = prev_tick - 1;
296 tick = i8254_max_count;
303 delta = prev_tick - tick;
306 delta += i8254_max_count;
308 * Guard against i8254_max_count being wrong.
309 * This shouldn't happen in normal operation,
310 * but it may happen if set_i8254_freq() is
320 printf(" %d calls to getit() at %d usec each\n",
321 getit_calls, (n + 5) / getit_calls);
326 set_i8254_freq(u_int freq, int intr_freq)
328 int new_i8254_real_max_count;
330 i8254_timecounter.tc_frequency = freq;
331 mtx_lock_spin(&clock_lock);
333 if (using_lapic_timer)
334 new_i8254_real_max_count = 0x10000;
336 new_i8254_real_max_count = TIMER_DIV(intr_freq);
337 if (new_i8254_real_max_count != i8254_real_max_count) {
338 i8254_real_max_count = new_i8254_real_max_count;
339 if (i8254_real_max_count == 0x10000)
340 i8254_max_count = 0xffff;
342 i8254_max_count = i8254_real_max_count;
343 outb(TIMER_MODE, TIMER_SEL0 | TIMER_RATEGEN | TIMER_16BIT);
344 outb(TIMER_CNTR0, i8254_real_max_count & 0xff);
345 outb(TIMER_CNTR0, i8254_real_max_count >> 8);
347 mtx_unlock_spin(&clock_lock);
354 mtx_lock_spin(&clock_lock);
355 outb(TIMER_MODE, TIMER_SEL0 | TIMER_RATEGEN | TIMER_16BIT);
356 outb(TIMER_CNTR0, i8254_real_max_count & 0xff);
357 outb(TIMER_CNTR0, i8254_real_max_count >> 8);
358 mtx_unlock_spin(&clock_lock);
362 * Restore all the timers non-atomically (XXX: should be atomically).
364 * This function is called from pmtimer_resume() to restore all the timers.
365 * This should not be necessary, but there are broken laptops that do not
366 * restore all the timers on resume.
372 i8254_restore(); /* restore i8254_freq and hz */
375 /* This is separate from startrtclock() so that it can be called early. */
380 mtx_init(&clock_lock, "clk", NULL, MTX_SPIN | MTX_NOPROFILE);
382 if (pc98_machine_type & M_8M)
383 i8254_freq = 1996800L; /* 1.9968 MHz */
385 i8254_freq = 2457600L; /* 2.4576 MHz */
387 set_i8254_freq(i8254_freq, hz);
394 set_i8254_freq(i8254_freq, hz);
395 tc_init(&i8254_timecounter);
401 * Start both clocks running.
408 using_lapic_timer = lapic_setup_clock();
411 * If we aren't using the local APIC timer to drive the kernel
412 * clocks, setup the interrupt handler for the 8254 timer 0 so
413 * that it can drive hardclock(). Otherwise, change the 8254
414 * timecounter to user a simpler algorithm.
416 if (!using_lapic_timer) {
417 intr_add_handler("clk", 0, (driver_filter_t *)clkintr, NULL,
418 NULL, INTR_TYPE_CLK, NULL);
419 i8254_intsrc = intr_lookup_source(0);
420 if (i8254_intsrc != NULL)
422 i8254_intsrc->is_pic->pic_source_pending;
424 i8254_timecounter.tc_get_timecount =
425 i8254_simple_get_timecount;
426 i8254_timecounter.tc_counter_mask = 0xffff;
427 set_i8254_freq(i8254_freq, hz);
434 cpu_startprofclock(void)
439 cpu_stopprofclock(void)
444 sysctl_machdep_i8254_freq(SYSCTL_HANDLER_ARGS)
450 * Use `i8254' instead of `timer' in external names because `timer'
451 * is is too generic. Should use it everywhere.
454 error = sysctl_handle_int(oidp, &freq, 0, req);
455 if (error == 0 && req->newptr != NULL)
456 set_i8254_freq(freq, hz);
460 SYSCTL_PROC(_machdep, OID_AUTO, i8254_freq, CTLTYPE_INT | CTLFLAG_RW,
461 0, sizeof(u_int), sysctl_machdep_i8254_freq, "IU", "");
464 i8254_simple_get_timecount(struct timecounter *tc)
467 return (i8254_max_count - getit());
471 i8254_get_timecount(struct timecounter *tc)
477 eflags = read_eflags();
478 mtx_lock_spin(&clock_lock);
480 /* Select timer0 and latch counter value. */
481 outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH);
483 low = inb(TIMER_CNTR0);
484 high = inb(TIMER_CNTR0);
485 count = i8254_max_count - ((high << 8) | low);
486 if (count < i8254_lastcount ||
487 (!i8254_ticked && (clkintr_pending ||
488 ((count < 20 || (!(eflags & PSL_I) &&
489 count < i8254_max_count / 2u)) &&
490 i8254_pending != NULL && i8254_pending(i8254_intsrc))))) {
492 i8254_offset += i8254_max_count;
494 i8254_lastcount = count;
495 count += i8254_offset;
496 mtx_unlock_spin(&clock_lock);
502 * Attach to the ISA PnP descriptors for the timer
504 static struct isa_pnp_id attimer_ids[] = {
505 { 0x0001d041 /* PNP0100 */, "AT timer" },
510 attimer_probe(device_t dev)
514 result = ISA_PNP_PROBE(device_get_parent(dev), dev, attimer_ids);
521 attimer_attach(device_t dev)
526 static device_method_t attimer_methods[] = {
527 /* Device interface */
528 DEVMETHOD(device_probe, attimer_probe),
529 DEVMETHOD(device_attach, attimer_attach),
530 DEVMETHOD(device_detach, bus_generic_detach),
531 DEVMETHOD(device_shutdown, bus_generic_shutdown),
532 DEVMETHOD(device_suspend, bus_generic_suspend),
533 DEVMETHOD(device_resume, bus_generic_resume),
537 static driver_t attimer_driver = {
543 static devclass_t attimer_devclass;
545 DRIVER_MODULE(attimer, isa, attimer_driver, attimer_devclass, 0, 0);