2 * Copyright (c) 2000, 2001 Michael Smith
3 * Copyright (c) 2000 BSDi
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * $FreeBSD: src/sys/dev/acpica/acpi_timer.c,v 1.33 2004/05/30 20:08:23 phk Exp $
28 * $DragonFly: src/sys/dev/acpica5/acpi_timer.c,v 1.4 2005/06/01 20:40:33 dillon Exp $
31 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/sysctl.h>
36 #include <sys/systimer.h>
38 #include <machine/bus.h>
39 #include <machine/resource.h>
40 #include <machine/lock.h>
42 #include <bus/pci/pcivar.h>
48 * A timecounter based on the free-running ACPI timer.
50 * Based on the i386-only mp_clock.c by <phk@FreeBSD.ORG>.
53 /* Hooks for the ACPI CA debugging infrastructure */
54 #define _COMPONENT ACPI_TIMER
55 ACPI_MODULE_NAME("TIMER")
57 static device_t acpi_timer_dev;
58 static struct resource *acpi_timer_reg;
59 static bus_space_handle_t acpi_timer_bsh;
60 static bus_space_tag_t acpi_timer_bst;
61 static sysclock_t acpi_counter_mask;
62 static sysclock_t acpi_last_counter;
64 #define ACPI_TIMER_FREQ (14318182 / 4)
66 static sysclock_t acpi_timer_get_timecount(void);
67 static sysclock_t acpi_timer_get_timecount24(void);
68 static sysclock_t acpi_timer_get_timecount_safe(void);
69 static void acpi_timer_construct(struct cputimer *timer, sysclock_t oldclock);
71 static struct cputimer acpi_cputimer = {
76 acpi_timer_get_timecount_safe,
77 cputimer_default_fromhz,
78 cputimer_default_fromus,
80 cputimer_default_destruct,
85 static void acpi_timer_identify(driver_t *driver, device_t parent);
86 static int acpi_timer_probe(device_t dev);
87 static int acpi_timer_attach(device_t dev);
88 static int acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS);
90 static u_int acpi_timer_read(void);
91 static int acpi_timer_test(void);
93 static device_method_t acpi_timer_methods[] = {
94 DEVMETHOD(device_identify, acpi_timer_identify),
95 DEVMETHOD(device_probe, acpi_timer_probe),
96 DEVMETHOD(device_attach, acpi_timer_attach),
101 static driver_t acpi_timer_driver = {
107 static devclass_t acpi_timer_devclass;
108 DRIVER_MODULE(acpi_timer, acpi, acpi_timer_driver, acpi_timer_devclass, 0, 0);
109 MODULE_DEPEND(acpi_timer, acpi, 1, 1, 1);
114 return (bus_space_read_4(acpi_timer_bst, acpi_timer_bsh, 0));
118 * Locate the ACPI timer using the FADT, set up and allocate the I/O resources
122 acpi_timer_identify(driver_t *driver, device_t parent)
127 int i, j, rid, rtype;
129 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
131 if (acpi_disabled("timer") || AcpiGbl_FADT == NULL)
134 if ((dev = BUS_ADD_CHILD(parent, 0, "acpi_timer", 0)) == NULL) {
135 device_printf(parent, "could not add acpi_timer0\n");
138 acpi_timer_dev = dev;
141 rlen = AcpiGbl_FADT->PmTmLen;
142 rtype = (AcpiGbl_FADT->XPmTmrBlk.AddressSpaceId)
143 ? SYS_RES_IOPORT : SYS_RES_MEMORY;
144 rstart = AcpiGbl_FADT->XPmTmrBlk.Address;
145 bus_set_resource(dev, rtype, rid, rstart, rlen);
146 acpi_timer_reg = bus_alloc_resource_any(dev, rtype, &rid, RF_ACTIVE);
147 if (acpi_timer_reg == NULL) {
148 device_printf(dev, "couldn't allocate I/O resource (%s 0x%lx)\n",
149 rtype == SYS_RES_IOPORT ? "port" : "mem", rstart);
152 acpi_timer_bsh = rman_get_bushandle(acpi_timer_reg);
153 acpi_timer_bst = rman_get_bustag(acpi_timer_reg);
154 if (AcpiGbl_FADT->TmrValExt != 0)
155 acpi_counter_mask = 0xffffffff;
157 acpi_counter_mask = 0x00ffffff;
160 * If all tests of the counter succeed, use the ACPI-fast method. If
161 * at least one failed, default to using the safe routine, which reads
162 * the timer multiple times to get a consistent value before returning.
165 for (i = 0; i < 10; i++)
166 j += acpi_timer_test();
168 if (acpi_counter_mask == 0xffffffff) {
169 acpi_cputimer.name = "ACPI-fast";
170 acpi_cputimer.count = acpi_timer_get_timecount;
172 acpi_cputimer.name = "ACPI-fast24";
173 acpi_cputimer.count = acpi_timer_get_timecount24;
176 acpi_cputimer.name = "ACPI-safe";
177 acpi_cputimer.count = acpi_timer_get_timecount_safe;
180 sprintf(desc, "%d-bit timer at 3.579545MHz",
181 AcpiGbl_FADT->TmrValExt ? 32 : 24);
182 device_set_desc_copy(dev, desc);
184 cputimer_register(&acpi_cputimer);
185 cputimer_select(&acpi_cputimer, 0);
190 acpi_timer_probe(device_t dev)
192 if (dev == acpi_timer_dev)
199 acpi_timer_attach(device_t dev)
205 * Construct the timer. Adjust the base so the system clock does not
209 acpi_timer_construct(struct cputimer *timer, sysclock_t oldclock)
212 timer->base = oldclock - acpi_timer_get_timecount_safe();
216 * Fetch current time value from reliable hardware.
218 * The cputimer interface requires a 32 bit return value. If the ACPI timer
219 * is only 24 bits then we have to keep track of the upper 8 bits on our
222 * XXX we could probably get away with using a per-cpu field for this and
223 * just use interrupt disablement instead of clock_lock.
226 acpi_timer_get_timecount24(void)
231 counter = acpi_timer_read();
232 if (counter < acpi_last_counter)
233 acpi_cputimer.base += 0x01000000;
234 acpi_last_counter = counter;
235 counter += acpi_cputimer.base;
241 acpi_timer_get_timecount(void)
243 return (acpi_timer_read() + acpi_cputimer.base);
247 * Fetch current time value from hardware that may not correctly
248 * latch the counter. We need to read until we have three monotonic
249 * samples and then use the middle one, otherwise we are not protected
250 * against the fact that the bits can be wrong in two directions. If
251 * we only cared about monosity, two reads would be enough.
254 acpi_timer_get_timecount_safe(void)
258 if (acpi_counter_mask != 0xffffffff)
261 u2 = acpi_timer_read();
262 u3 = acpi_timer_read();
266 u3 = acpi_timer_read();
267 } while (u1 > u2 || u2 > u3);
269 if (acpi_counter_mask != 0xffffffff) {
270 if (u2 < acpi_last_counter)
271 acpi_cputimer.base += 0x01000000;
272 acpi_last_counter = u2;
275 return (u2 + acpi_cputimer.base);
279 * Timecounter freqency adjustment interface.
282 acpi_timer_sysctl_freq(SYSCTL_HANDLER_ARGS)
287 if (acpi_cputimer.freq == 0)
289 freq = acpi_cputimer.freq;
290 error = sysctl_handle_int(oidp, &freq, sizeof(freq), req);
291 if (error == 0 && req->newptr != NULL)
292 cputimer_set_frequency(&acpi_cputimer, freq);
297 SYSCTL_PROC(_machdep, OID_AUTO, acpi_timer_freq, CTLTYPE_INT | CTLFLAG_RW,
298 0, sizeof(u_int), acpi_timer_sysctl_freq, "I", "");
301 * Some ACPI timers are known or believed to suffer from implementation
302 * problems which can lead to erroneous values being read. This function
303 * tests for consistent results from the timer and returns 1 if it believes
304 * the timer is consistent, otherwise it returns 0.
306 * It appears the cause is that the counter is not latched to the PCI bus
309 * ] 20. ACPI Timer Errata
311 * ] Problem: The power management timer may return improper result when
312 * ] read. Although the timer value settles properly after incrementing,
313 * ] while incrementing there is a 3nS window every 69.8nS where the
314 * ] timer value is indeterminate (a 4.2% chance that the data will be
315 * ] incorrect when read). As a result, the ACPI free running count up
316 * ] timer specification is violated due to erroneous reads. Implication:
317 * ] System hangs due to the "inaccuracy" of the timer when used by
318 * ] software for time critical events and delays.
320 * ] Workaround: Read the register twice and compare.
321 * ] Status: This will not be fixed in the PIIX4 or PIIX4E, it is fixed
329 int min, max, n, delta;
335 /* Test the timer with interrupts disabled to get accurate results. */
338 last = acpi_timer_read();
339 for (n = 0; n < 2000; n++) {
340 this = acpi_timer_read();
341 delta = acpi_TimerDelta(this, last);
344 else if (delta < min)
352 else if (min < 0 || max == 0)
357 printf("ACPI timer looks %s min = %d, max = %d, width = %d\n",
359 min, max, max - min);