2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1990 The Regents of the University of California.
5 * Copyright (c) 2010 Alexander Motin <mav@FreeBSD.org>
8 * This code is derived from software contributed to Berkeley by
9 * William Jolitz and Don Ahn.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * from: @(#)clock.c 7.2 (Berkeley) 5/12/91
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
42 * Routines to handle clock hardware.
45 #include "opt_clock.h"
48 #include <sys/param.h>
49 #include <sys/systm.h>
53 #include <sys/mutex.h>
55 #include <sys/kernel.h>
56 #include <sys/module.h>
58 #include <sys/sched.h>
60 #include <sys/sysctl.h>
61 #include <sys/timeet.h>
62 #include <sys/timetc.h>
64 #include <machine/clock.h>
65 #include <machine/cpu.h>
66 #include <machine/intr_machdep.h>
67 #include <machine/ppireg.h>
68 #include <machine/timerreg.h>
73 #include <isa/isareg.h>
74 #include <isa/isavar.h>
79 #define TIMER_FREQ 1193182
81 u_int i8254_freq = TIMER_FREQ;
82 TUNABLE_INT("hw.i8254.freq", &i8254_freq);
84 static int i8254_timecounter = 1;
86 struct mtx clock_lock;
87 static struct intsrc *i8254_intsrc;
88 static uint16_t i8254_lastcount;
89 static uint16_t i8254_offset;
90 static int (*i8254_pending)(struct intsrc *);
91 static int i8254_ticked;
93 struct attimer_softc {
95 int port_rid, intr_rid;
96 struct resource *port_res;
97 struct resource *intr_res;
99 struct timecounter tc;
100 struct eventtimer et;
103 #define MODE_PERIODIC 1
104 #define MODE_ONESHOT 2
107 static struct attimer_softc *attimer_sc = NULL;
109 static int timer0_period = -2;
110 static int timer0_mode = 0xffff;
111 static int timer0_last = 0xffff;
113 /* Values for timerX_state: */
115 #define RELEASE_PENDING 1
117 #define ACQUIRE_PENDING 3
119 static u_char timer2_state;
121 static unsigned i8254_get_timecount(struct timecounter *tc);
122 static void set_i8254_freq(int mode, uint32_t period);
127 /* Init the clock lock */
128 mtx_init(&clock_lock, "clk", NULL, MTX_SPIN | MTX_NOPROFILE);
129 /* Init the clock in order to use DELAY */
130 init_ops.early_clock_source_init();
136 struct attimer_softc *sc = (struct attimer_softc *)arg;
138 if (i8254_timecounter && sc->period != 0) {
139 mtx_lock_spin(&clock_lock);
143 i8254_offset += i8254_max_count;
147 mtx_unlock_spin(&clock_lock);
150 if (sc->et.et_active && sc->mode != MODE_STOP)
151 sc->et.et_event_cb(&sc->et, sc->et.et_arg);
153 return (FILTER_HANDLED);
157 timer_spkr_acquire(void)
161 mode = TIMER_SEL2 | TIMER_SQWAVE | TIMER_16BIT;
163 if (timer2_state != RELEASED)
165 timer2_state = ACQUIRED;
168 * This access to the timer registers is as atomic as possible
169 * because it is a single instruction. We could do better if we
170 * knew the rate. Use of splclock() limits glitches to 10-100us,
171 * and this is probably good enough for timer2, so we aren't as
172 * careful with it as with timer0.
174 outb(TIMER_MODE, TIMER_SEL2 | (mode & 0x3f));
176 ppi_spkr_on(); /* enable counter2 output to speaker */
181 timer_spkr_release(void)
184 if (timer2_state != ACQUIRED)
186 timer2_state = RELEASED;
187 outb(TIMER_MODE, TIMER_SEL2 | TIMER_SQWAVE | TIMER_16BIT);
189 ppi_spkr_off(); /* disable counter2 output to speaker */
194 timer_spkr_setfreq(int freq)
197 freq = i8254_freq / freq;
198 mtx_lock_spin(&clock_lock);
199 outb(TIMER_CNTR2, freq & 0xff);
200 outb(TIMER_CNTR2, freq >> 8);
201 mtx_unlock_spin(&clock_lock);
209 mtx_lock_spin(&clock_lock);
211 /* Select timer0 and latch counter value. */
212 outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH);
214 low = inb(TIMER_CNTR0);
215 high = inb(TIMER_CNTR0);
217 mtx_unlock_spin(&clock_lock);
218 return ((high << 8) | low);
222 * Wait "n" microseconds.
223 * Relies on timer 1 counting down from (i8254_freq / hz)
224 * Note: timer had better have been programmed before this is first used!
229 int delta, prev_tick, tick, ticks_left;
233 static int state = 0;
237 for (n1 = 1; n1 <= 10000000; n1 *= 10)
242 printf("DELAY(%d)...", n);
245 * Read the counter first, so that the rest of the setup overhead is
246 * counted. Guess the initial overhead is 20 usec (on most systems it
247 * takes about 1.5 usec for each of the i/o's in getit(). The loop
248 * takes about 6 usec on a 486/33 and 13 usec on a 386/20. The
249 * multiplications and divisions to scale the count take a while).
251 * However, if ddb is active then use a fake counter since reading
252 * the i8254 counter involves acquiring a lock. ddb must not do
253 * locking for many reasons, but it calls here for at least atkbd
262 n -= 0; /* XXX actually guess no initial overhead */
264 * Calculate (n * (i8254_freq / 1e6)) without using floating point
265 * and without any avoidable overflows.
271 * Use fixed point to avoid a slow division by 1000000.
272 * 39099 = 1193182 * 2^15 / 10^6 rounded to nearest.
273 * 2^15 is the first power of 2 that gives exact results
274 * for n between 0 and 256.
276 ticks_left = ((u_int)n * 39099 + (1 << 15) - 1) >> 15;
279 * Don't bother using fixed point, although gcc-2.7.2
280 * generates particularly poor code for the long long
281 * division, since even the slow way will complete long
282 * before the delay is up (unless we're interrupted).
284 ticks_left = ((u_int)n * (long long)i8254_freq + 999999)
287 while (ticks_left > 0) {
291 tick = prev_tick - 1;
293 tick = i8254_max_count;
300 delta = prev_tick - tick;
303 delta += i8254_max_count;
305 * Guard against i8254_max_count being wrong.
306 * This shouldn't happen in normal operation,
307 * but it may happen if set_i8254_freq() is
317 printf(" %d calls to getit() at %d usec each\n",
318 getit_calls, (n + 5) / getit_calls);
323 set_i8254_freq(int mode, uint32_t period)
325 int new_count, new_mode;
327 mtx_lock_spin(&clock_lock);
328 if (mode == MODE_STOP) {
329 if (i8254_timecounter) {
330 mode = MODE_PERIODIC;
335 new_count = min(((uint64_t)i8254_freq * period +
336 0x80000000LLU) >> 32, 0x10000);
338 if (new_count == timer0_period)
340 i8254_max_count = ((new_count & ~0xffff) != 0) ? 0xffff : new_count;
341 timer0_period = (mode == MODE_PERIODIC) ? new_count : -1;
344 new_mode = TIMER_SEL0 | TIMER_INTTC | TIMER_16BIT;
345 outb(TIMER_MODE, new_mode);
346 outb(TIMER_CNTR0, 0);
347 outb(TIMER_CNTR0, 0);
350 new_mode = TIMER_SEL0 | TIMER_RATEGEN | TIMER_16BIT;
351 outb(TIMER_MODE, new_mode);
352 outb(TIMER_CNTR0, new_count & 0xff);
353 outb(TIMER_CNTR0, new_count >> 8);
356 if (new_count < 256 && timer0_last < 256) {
357 new_mode = TIMER_SEL0 | TIMER_INTTC | TIMER_LSB;
358 if (new_mode != timer0_mode)
359 outb(TIMER_MODE, new_mode);
360 outb(TIMER_CNTR0, new_count & 0xff);
363 new_mode = TIMER_SEL0 | TIMER_INTTC | TIMER_16BIT;
364 if (new_mode != timer0_mode)
365 outb(TIMER_MODE, new_mode);
366 outb(TIMER_CNTR0, new_count & 0xff);
367 outb(TIMER_CNTR0, new_count >> 8);
370 panic("set_i8254_freq: unknown operational mode");
372 timer0_mode = new_mode;
373 timer0_last = new_count;
375 mtx_unlock_spin(&clock_lock);
383 timer0_mode = 0xffff;
384 timer0_last = 0xffff;
385 if (attimer_sc != NULL)
386 set_i8254_freq(attimer_sc->mode, attimer_sc->period);
388 set_i8254_freq(MODE_STOP, 0);
393 * Restore all the timers non-atomically (XXX: should be atomically).
395 * This function is called from pmtimer_resume() to restore all the timers.
396 * This should not be necessary, but there are broken laptops that do not
397 * restore all the timers on resume. The APM spec was at best vague on the
399 * pmtimer is used only with the old APM power management, and not with
400 * acpi, which is required for amd64, so skip it in that case.
406 i8254_restore(); /* restore i8254_freq and hz */
407 atrtc_restore(); /* reenable RTC interrupts */
411 /* This is separate from startrtclock() so that it can be called early. */
416 set_i8254_freq(MODE_STOP, 0);
429 #ifdef EARLY_AP_STARTUP
434 cpu_initclocks_bsp();
444 if (sched_is_bound(td))
448 cpu_initclocks_bsp();
453 sysctl_machdep_i8254_freq(SYSCTL_HANDLER_ARGS)
459 * Use `i8254' instead of `timer' in external names because `timer'
460 * is too generic. Should use it everywhere.
463 error = sysctl_handle_int(oidp, &freq, 0, req);
464 if (error == 0 && req->newptr != NULL) {
466 if (attimer_sc != NULL) {
467 set_i8254_freq(attimer_sc->mode, attimer_sc->period);
468 attimer_sc->tc.tc_frequency = freq;
470 set_i8254_freq(MODE_STOP, 0);
476 SYSCTL_PROC(_machdep, OID_AUTO, i8254_freq, CTLTYPE_INT | CTLFLAG_RW,
477 0, sizeof(u_int), sysctl_machdep_i8254_freq, "IU",
478 "i8254 timer frequency");
481 i8254_get_timecount(struct timecounter *tc)
483 device_t dev = (device_t)tc->tc_priv;
484 struct attimer_softc *sc = device_get_softc(dev);
490 return (i8254_max_count - getit());
493 flags = read_rflags();
495 flags = read_eflags();
497 mtx_lock_spin(&clock_lock);
499 /* Select timer0 and latch counter value. */
500 outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH);
502 low = inb(TIMER_CNTR0);
503 high = inb(TIMER_CNTR0);
504 count = i8254_max_count - ((high << 8) | low);
505 if (count < i8254_lastcount ||
506 (!i8254_ticked && (clkintr_pending ||
507 ((count < 20 || (!(flags & PSL_I) &&
508 count < i8254_max_count / 2u)) &&
509 i8254_pending != NULL && i8254_pending(i8254_intsrc))))) {
511 i8254_offset += i8254_max_count;
513 i8254_lastcount = count;
514 count += i8254_offset;
515 mtx_unlock_spin(&clock_lock);
520 attimer_start(struct eventtimer *et, sbintime_t first, sbintime_t period)
522 device_t dev = (device_t)et->et_priv;
523 struct attimer_softc *sc = device_get_softc(dev);
526 sc->mode = MODE_PERIODIC;
529 sc->mode = MODE_ONESHOT;
533 i8254_intsrc->is_pic->pic_enable_source(i8254_intsrc);
536 set_i8254_freq(sc->mode, sc->period);
541 attimer_stop(struct eventtimer *et)
543 device_t dev = (device_t)et->et_priv;
544 struct attimer_softc *sc = device_get_softc(dev);
546 sc->mode = MODE_STOP;
548 set_i8254_freq(sc->mode, sc->period);
554 * Attach to the ISA PnP descriptors for the timer
556 static struct isa_pnp_id attimer_ids[] = {
557 { 0x0001d041 /* PNP0100 */, "AT timer" },
562 attimer_probe(device_t dev)
566 result = ISA_PNP_PROBE(device_get_parent(dev), dev, attimer_ids);
567 /* ENOENT means no PnP-ID, device is hinted. */
568 if (result == ENOENT) {
569 device_set_desc(dev, "AT timer");
570 return (BUS_PROBE_LOW_PRIORITY);
576 attimer_attach(device_t dev)
578 struct attimer_softc *sc;
582 attimer_sc = sc = device_get_softc(dev);
583 bzero(sc, sizeof(struct attimer_softc));
584 if (!(sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
585 &sc->port_rid, IO_TIMER1, IO_TIMER1 + 3, 4, RF_ACTIVE)))
586 device_printf(dev,"Warning: Couldn't map I/O.\n");
587 i8254_intsrc = intr_lookup_source(0);
588 if (i8254_intsrc != NULL)
589 i8254_pending = i8254_intsrc->is_pic->pic_source_pending;
590 resource_int_value(device_get_name(dev), device_get_unit(dev),
591 "timecounter", &i8254_timecounter);
592 set_i8254_freq(MODE_STOP, 0);
593 if (i8254_timecounter) {
594 sc->tc.tc_get_timecount = i8254_get_timecount;
595 sc->tc.tc_counter_mask = 0xffff;
596 sc->tc.tc_frequency = i8254_freq;
597 sc->tc.tc_name = "i8254";
598 sc->tc.tc_quality = 0;
599 sc->tc.tc_priv = dev;
602 if (resource_int_value(device_get_name(dev), device_get_unit(dev),
603 "clock", &i) != 0 || i != 0) {
605 while (bus_get_resource(dev, SYS_RES_IRQ, sc->intr_rid,
606 &s, NULL) == 0 && s != 0)
608 if (!(sc->intr_res = bus_alloc_resource(dev, SYS_RES_IRQ,
609 &sc->intr_rid, 0, 0, 1, RF_ACTIVE))) {
610 device_printf(dev,"Can't map interrupt.\n");
613 /* Dirty hack, to make bus_setup_intr to not enable source. */
614 i8254_intsrc->is_handlers++;
615 if ((bus_setup_intr(dev, sc->intr_res,
616 INTR_MPSAFE | INTR_TYPE_CLK,
617 (driver_filter_t *)clkintr, NULL,
618 sc, &sc->intr_handler))) {
619 device_printf(dev, "Can't setup interrupt.\n");
620 i8254_intsrc->is_handlers--;
623 i8254_intsrc->is_handlers--;
624 i8254_intsrc->is_pic->pic_enable_intr(i8254_intsrc);
625 sc->et.et_name = "i8254";
626 sc->et.et_flags = ET_FLAGS_PERIODIC;
627 if (!i8254_timecounter)
628 sc->et.et_flags |= ET_FLAGS_ONESHOT;
629 sc->et.et_quality = 100;
630 sc->et.et_frequency = i8254_freq;
631 sc->et.et_min_period = (0x0002LLU << 32) / i8254_freq;
632 sc->et.et_max_period = (0xfffeLLU << 32) / i8254_freq;
633 sc->et.et_start = attimer_start;
634 sc->et.et_stop = attimer_stop;
635 sc->et.et_priv = dev;
636 et_register(&sc->et);
642 attimer_resume(device_t dev)
649 static device_method_t attimer_methods[] = {
650 /* Device interface */
651 DEVMETHOD(device_probe, attimer_probe),
652 DEVMETHOD(device_attach, attimer_attach),
653 DEVMETHOD(device_detach, bus_generic_detach),
654 DEVMETHOD(device_shutdown, bus_generic_shutdown),
655 DEVMETHOD(device_suspend, bus_generic_suspend),
656 DEVMETHOD(device_resume, attimer_resume),
660 static driver_t attimer_driver = {
663 sizeof(struct attimer_softc),
666 static devclass_t attimer_devclass;
668 DRIVER_MODULE(attimer, isa, attimer_driver, attimer_devclass, 0, 0);
669 DRIVER_MODULE(attimer, acpi, attimer_driver, attimer_devclass, 0, 0);
670 ISA_PNP_INFO(attimer_ids);