2 * Copyright (c) 1990 The Regents of the University of California.
3 * Copyright (c) 2010 Alexander Motin <mav@FreeBSD.org>
6 * This code is derived from software contributed to Berkeley by
7 * William Jolitz and Don Ahn.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * from: @(#)clock.c 7.2 (Berkeley) 5/12/91
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
40 * Routines to handle clock hardware.
43 #include "opt_clock.h"
47 #include <sys/param.h>
48 #include <sys/systm.h>
52 #include <sys/mutex.h>
54 #include <sys/kernel.h>
55 #include <sys/module.h>
57 #include <sys/sched.h>
59 #include <sys/sysctl.h>
60 #include <sys/timeet.h>
61 #include <sys/timetc.h>
63 #include <machine/clock.h>
64 #include <machine/cpu.h>
65 #include <machine/intr_machdep.h>
66 #include <machine/ppireg.h>
67 #include <machine/timerreg.h>
72 #include <isa/isareg.h>
73 #include <isa/isavar.h>
77 #include <i386/bios/mca_machdep.h>
82 #define TIMER_FREQ 1193182
84 u_int i8254_freq = TIMER_FREQ;
85 TUNABLE_INT("hw.i8254.freq", &i8254_freq);
87 static int i8254_timecounter = 1;
89 struct mtx clock_lock;
90 static struct intsrc *i8254_intsrc;
91 static uint16_t i8254_lastcount;
92 static uint16_t i8254_offset;
93 static int (*i8254_pending)(struct intsrc *);
94 static int i8254_ticked;
96 struct attimer_softc {
98 int port_rid, intr_rid;
99 struct resource *port_res;
100 struct resource *intr_res;
102 struct timecounter tc;
103 struct eventtimer et;
106 #define MODE_PERIODIC 1
107 #define MODE_ONESHOT 2
110 static struct attimer_softc *attimer_sc = NULL;
112 static int timer0_period = -2;
113 static int timer0_mode = 0xffff;
114 static int timer0_last = 0xffff;
116 /* Values for timerX_state: */
118 #define RELEASE_PENDING 1
120 #define ACQUIRE_PENDING 3
122 static u_char timer2_state;
124 static unsigned i8254_get_timecount(struct timecounter *tc);
125 static void set_i8254_freq(int mode, uint32_t period);
130 /* Init the clock lock */
131 mtx_init(&clock_lock, "clk", NULL, MTX_SPIN | MTX_NOPROFILE);
132 /* Init the clock in order to use DELAY */
133 init_ops.early_clock_source_init();
139 struct attimer_softc *sc = (struct attimer_softc *)arg;
141 if (i8254_timecounter && sc->period != 0) {
142 mtx_lock_spin(&clock_lock);
146 i8254_offset += i8254_max_count;
150 mtx_unlock_spin(&clock_lock);
153 if (sc->et.et_active && sc->mode != MODE_STOP)
154 sc->et.et_event_cb(&sc->et, sc->et.et_arg);
157 /* Reset clock interrupt by asserting bit 7 of port 0x61 */
159 outb(0x61, inb(0x61) | 0x80);
161 return (FILTER_HANDLED);
165 timer_spkr_acquire(void)
169 mode = TIMER_SEL2 | TIMER_SQWAVE | TIMER_16BIT;
171 if (timer2_state != RELEASED)
173 timer2_state = ACQUIRED;
176 * This access to the timer registers is as atomic as possible
177 * because it is a single instruction. We could do better if we
178 * knew the rate. Use of splclock() limits glitches to 10-100us,
179 * and this is probably good enough for timer2, so we aren't as
180 * careful with it as with timer0.
182 outb(TIMER_MODE, TIMER_SEL2 | (mode & 0x3f));
184 ppi_spkr_on(); /* enable counter2 output to speaker */
189 timer_spkr_release(void)
192 if (timer2_state != ACQUIRED)
194 timer2_state = RELEASED;
195 outb(TIMER_MODE, TIMER_SEL2 | TIMER_SQWAVE | TIMER_16BIT);
197 ppi_spkr_off(); /* disable counter2 output to speaker */
202 timer_spkr_setfreq(int freq)
205 freq = i8254_freq / freq;
206 mtx_lock_spin(&clock_lock);
207 outb(TIMER_CNTR2, freq & 0xff);
208 outb(TIMER_CNTR2, freq >> 8);
209 mtx_unlock_spin(&clock_lock);
217 mtx_lock_spin(&clock_lock);
219 /* Select timer0 and latch counter value. */
220 outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH);
222 low = inb(TIMER_CNTR0);
223 high = inb(TIMER_CNTR0);
225 mtx_unlock_spin(&clock_lock);
226 return ((high << 8) | low);
230 * Wait "n" microseconds.
231 * Relies on timer 1 counting down from (i8254_freq / hz)
232 * Note: timer had better have been programmed before this is first used!
237 int delta, prev_tick, tick, ticks_left;
241 static int state = 0;
245 for (n1 = 1; n1 <= 10000000; n1 *= 10)
250 printf("DELAY(%d)...", n);
253 * Read the counter first, so that the rest of the setup overhead is
254 * counted. Guess the initial overhead is 20 usec (on most systems it
255 * takes about 1.5 usec for each of the i/o's in getit(). The loop
256 * takes about 6 usec on a 486/33 and 13 usec on a 386/20. The
257 * multiplications and divisions to scale the count take a while).
259 * However, if ddb is active then use a fake counter since reading
260 * the i8254 counter involves acquiring a lock. ddb must not do
261 * locking for many reasons, but it calls here for at least atkbd
270 n -= 0; /* XXX actually guess no initial overhead */
272 * Calculate (n * (i8254_freq / 1e6)) without using floating point
273 * and without any avoidable overflows.
279 * Use fixed point to avoid a slow division by 1000000.
280 * 39099 = 1193182 * 2^15 / 10^6 rounded to nearest.
281 * 2^15 is the first power of 2 that gives exact results
282 * for n between 0 and 256.
284 ticks_left = ((u_int)n * 39099 + (1 << 15) - 1) >> 15;
287 * Don't bother using fixed point, although gcc-2.7.2
288 * generates particularly poor code for the long long
289 * division, since even the slow way will complete long
290 * before the delay is up (unless we're interrupted).
292 ticks_left = ((u_int)n * (long long)i8254_freq + 999999)
295 while (ticks_left > 0) {
299 tick = prev_tick - 1;
301 tick = i8254_max_count;
308 delta = prev_tick - tick;
311 delta += i8254_max_count;
313 * Guard against i8254_max_count being wrong.
314 * This shouldn't happen in normal operation,
315 * but it may happen if set_i8254_freq() is
325 printf(" %d calls to getit() at %d usec each\n",
326 getit_calls, (n + 5) / getit_calls);
331 set_i8254_freq(int mode, uint32_t period)
333 int new_count, new_mode;
335 mtx_lock_spin(&clock_lock);
336 if (mode == MODE_STOP) {
337 if (i8254_timecounter) {
338 mode = MODE_PERIODIC;
343 new_count = min(((uint64_t)i8254_freq * period +
344 0x80000000LLU) >> 32, 0x10000);
346 if (new_count == timer0_period)
348 i8254_max_count = ((new_count & ~0xffff) != 0) ? 0xffff : new_count;
349 timer0_period = (mode == MODE_PERIODIC) ? new_count : -1;
352 new_mode = TIMER_SEL0 | TIMER_INTTC | TIMER_16BIT;
353 outb(TIMER_MODE, new_mode);
354 outb(TIMER_CNTR0, 0);
355 outb(TIMER_CNTR0, 0);
358 new_mode = TIMER_SEL0 | TIMER_RATEGEN | TIMER_16BIT;
359 outb(TIMER_MODE, new_mode);
360 outb(TIMER_CNTR0, new_count & 0xff);
361 outb(TIMER_CNTR0, new_count >> 8);
364 if (new_count < 256 && timer0_last < 256) {
365 new_mode = TIMER_SEL0 | TIMER_INTTC | TIMER_LSB;
366 if (new_mode != timer0_mode)
367 outb(TIMER_MODE, new_mode);
368 outb(TIMER_CNTR0, new_count & 0xff);
371 new_mode = TIMER_SEL0 | TIMER_INTTC | TIMER_16BIT;
372 if (new_mode != timer0_mode)
373 outb(TIMER_MODE, new_mode);
374 outb(TIMER_CNTR0, new_count & 0xff);
375 outb(TIMER_CNTR0, new_count >> 8);
378 panic("set_i8254_freq: unknown operational mode");
380 timer0_mode = new_mode;
381 timer0_last = new_count;
383 mtx_unlock_spin(&clock_lock);
391 timer0_mode = 0xffff;
392 timer0_last = 0xffff;
393 if (attimer_sc != NULL)
394 set_i8254_freq(attimer_sc->mode, attimer_sc->period);
396 set_i8254_freq(MODE_STOP, 0);
401 * Restore all the timers non-atomically (XXX: should be atomically).
403 * This function is called from pmtimer_resume() to restore all the timers.
404 * This should not be necessary, but there are broken laptops that do not
405 * restore all the timers on resume. The APM spec was at best vague on the
407 * pmtimer is used only with the old APM power management, and not with
408 * acpi, which is required for amd64, so skip it in that case.
414 i8254_restore(); /* restore i8254_freq and hz */
415 atrtc_restore(); /* reenable RTC interrupts */
419 /* This is separate from startrtclock() so that it can be called early. */
424 set_i8254_freq(MODE_STOP, 0);
437 #ifdef EARLY_AP_STARTUP
442 cpu_initclocks_bsp();
452 if (sched_is_bound(td))
456 cpu_initclocks_bsp();
461 sysctl_machdep_i8254_freq(SYSCTL_HANDLER_ARGS)
467 * Use `i8254' instead of `timer' in external names because `timer'
468 * is too generic. Should use it everywhere.
471 error = sysctl_handle_int(oidp, &freq, 0, req);
472 if (error == 0 && req->newptr != NULL) {
474 if (attimer_sc != NULL) {
475 set_i8254_freq(attimer_sc->mode, attimer_sc->period);
476 attimer_sc->tc.tc_frequency = freq;
478 set_i8254_freq(MODE_STOP, 0);
484 SYSCTL_PROC(_machdep, OID_AUTO, i8254_freq, CTLTYPE_INT | CTLFLAG_RW,
485 0, sizeof(u_int), sysctl_machdep_i8254_freq, "IU",
486 "i8254 timer frequency");
489 i8254_get_timecount(struct timecounter *tc)
491 device_t dev = (device_t)tc->tc_priv;
492 struct attimer_softc *sc = device_get_softc(dev);
498 return (i8254_max_count - getit());
501 flags = read_rflags();
503 flags = read_eflags();
505 mtx_lock_spin(&clock_lock);
507 /* Select timer0 and latch counter value. */
508 outb(TIMER_MODE, TIMER_SEL0 | TIMER_LATCH);
510 low = inb(TIMER_CNTR0);
511 high = inb(TIMER_CNTR0);
512 count = i8254_max_count - ((high << 8) | low);
513 if (count < i8254_lastcount ||
514 (!i8254_ticked && (clkintr_pending ||
515 ((count < 20 || (!(flags & PSL_I) &&
516 count < i8254_max_count / 2u)) &&
517 i8254_pending != NULL && i8254_pending(i8254_intsrc))))) {
519 i8254_offset += i8254_max_count;
521 i8254_lastcount = count;
522 count += i8254_offset;
523 mtx_unlock_spin(&clock_lock);
528 attimer_start(struct eventtimer *et, sbintime_t first, sbintime_t period)
530 device_t dev = (device_t)et->et_priv;
531 struct attimer_softc *sc = device_get_softc(dev);
534 sc->mode = MODE_PERIODIC;
537 sc->mode = MODE_ONESHOT;
541 i8254_intsrc->is_pic->pic_enable_source(i8254_intsrc);
544 set_i8254_freq(sc->mode, sc->period);
549 attimer_stop(struct eventtimer *et)
551 device_t dev = (device_t)et->et_priv;
552 struct attimer_softc *sc = device_get_softc(dev);
554 sc->mode = MODE_STOP;
556 set_i8254_freq(sc->mode, sc->period);
562 * Attach to the ISA PnP descriptors for the timer
564 static struct isa_pnp_id attimer_ids[] = {
565 { 0x0001d041 /* PNP0100 */, "AT timer" },
570 attimer_probe(device_t dev)
574 result = ISA_PNP_PROBE(device_get_parent(dev), dev, attimer_ids);
575 /* ENOENT means no PnP-ID, device is hinted. */
576 if (result == ENOENT) {
577 device_set_desc(dev, "AT timer");
578 return (BUS_PROBE_LOW_PRIORITY);
584 attimer_attach(device_t dev)
586 struct attimer_softc *sc;
590 attimer_sc = sc = device_get_softc(dev);
591 bzero(sc, sizeof(struct attimer_softc));
592 if (!(sc->port_res = bus_alloc_resource(dev, SYS_RES_IOPORT,
593 &sc->port_rid, IO_TIMER1, IO_TIMER1 + 3, 4, RF_ACTIVE)))
594 device_printf(dev,"Warning: Couldn't map I/O.\n");
595 i8254_intsrc = intr_lookup_source(0);
596 if (i8254_intsrc != NULL)
597 i8254_pending = i8254_intsrc->is_pic->pic_source_pending;
598 resource_int_value(device_get_name(dev), device_get_unit(dev),
599 "timecounter", &i8254_timecounter);
600 set_i8254_freq(MODE_STOP, 0);
601 if (i8254_timecounter) {
602 sc->tc.tc_get_timecount = i8254_get_timecount;
603 sc->tc.tc_counter_mask = 0xffff;
604 sc->tc.tc_frequency = i8254_freq;
605 sc->tc.tc_name = "i8254";
606 sc->tc.tc_quality = 0;
607 sc->tc.tc_priv = dev;
610 if (resource_int_value(device_get_name(dev), device_get_unit(dev),
611 "clock", &i) != 0 || i != 0) {
613 while (bus_get_resource(dev, SYS_RES_IRQ, sc->intr_rid,
614 &s, NULL) == 0 && s != 0)
616 if (!(sc->intr_res = bus_alloc_resource(dev, SYS_RES_IRQ,
617 &sc->intr_rid, 0, 0, 1, RF_ACTIVE))) {
618 device_printf(dev,"Can't map interrupt.\n");
621 /* Dirty hack, to make bus_setup_intr to not enable source. */
622 i8254_intsrc->is_handlers++;
623 if ((bus_setup_intr(dev, sc->intr_res,
624 INTR_MPSAFE | INTR_TYPE_CLK,
625 (driver_filter_t *)clkintr, NULL,
626 sc, &sc->intr_handler))) {
627 device_printf(dev, "Can't setup interrupt.\n");
628 i8254_intsrc->is_handlers--;
631 i8254_intsrc->is_handlers--;
632 i8254_intsrc->is_pic->pic_enable_intr(i8254_intsrc);
633 sc->et.et_name = "i8254";
634 sc->et.et_flags = ET_FLAGS_PERIODIC;
635 if (!i8254_timecounter)
636 sc->et.et_flags |= ET_FLAGS_ONESHOT;
637 sc->et.et_quality = 100;
638 sc->et.et_frequency = i8254_freq;
639 sc->et.et_min_period = (0x0002LLU << 32) / i8254_freq;
640 sc->et.et_max_period = (0xfffeLLU << 32) / i8254_freq;
641 sc->et.et_start = attimer_start;
642 sc->et.et_stop = attimer_stop;
643 sc->et.et_priv = dev;
644 et_register(&sc->et);
650 attimer_resume(device_t dev)
657 static device_method_t attimer_methods[] = {
658 /* Device interface */
659 DEVMETHOD(device_probe, attimer_probe),
660 DEVMETHOD(device_attach, attimer_attach),
661 DEVMETHOD(device_detach, bus_generic_detach),
662 DEVMETHOD(device_shutdown, bus_generic_shutdown),
663 DEVMETHOD(device_suspend, bus_generic_suspend),
664 DEVMETHOD(device_resume, attimer_resume),
668 static driver_t attimer_driver = {
671 sizeof(struct attimer_softc),
674 static devclass_t attimer_devclass;
676 DRIVER_MODULE(attimer, isa, attimer_driver, attimer_devclass, 0, 0);
677 DRIVER_MODULE(attimer, acpi, attimer_driver, attimer_devclass, 0, 0);