2 * Copyright (c) 2005 Poul-Henning Kamp
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
37 #include <sys/timetc.h>
39 #include <contrib/dev/acpica/include/acpi.h>
40 #include <contrib/dev/acpica/include/accommon.h>
42 #include <dev/acpica/acpivar.h>
43 #include <dev/acpica/acpi_hpet.h>
45 ACPI_SERIAL_DECL(hpet, "ACPI HPET support");
47 static devclass_t acpi_hpet_devclass;
49 /* ACPI CA debugging */
50 #define _COMPONENT ACPI_TIMER
51 ACPI_MODULE_NAME("HPET")
53 struct acpi_hpet_softc {
55 struct resource *mem_res;
59 static u_int hpet_get_timecount(struct timecounter *tc);
60 static void acpi_hpet_test(struct acpi_hpet_softc *sc);
62 static char *hpet_ids[] = { "PNP0103", NULL };
64 #define DEV_HPET(x) (acpi_get_magic(x) == (uintptr_t)&acpi_hpet_devclass)
66 struct timecounter hpet_timecounter = {
67 .tc_get_timecount = hpet_get_timecount,
68 .tc_counter_mask = ~0u,
74 hpet_get_timecount(struct timecounter *tc)
76 struct acpi_hpet_softc *sc;
79 return (bus_read_4(sc->mem_res, HPET_MAIN_COUNTER));
83 hpet_enable(struct acpi_hpet_softc *sc)
87 val = bus_read_4(sc->mem_res, HPET_CONFIG);
88 val &= ~HPET_CNF_LEG_RT;
89 val |= HPET_CNF_ENABLE;
90 bus_write_4(sc->mem_res, HPET_CONFIG, val);
94 hpet_disable(struct acpi_hpet_softc *sc)
98 val = bus_read_4(sc->mem_res, HPET_CONFIG);
99 val &= ~HPET_CNF_ENABLE;
100 bus_write_4(sc->mem_res, HPET_CONFIG, val);
103 /* Discover the HPET via the ACPI table of the same name. */
105 acpi_hpet_identify(driver_t *driver, device_t parent)
107 ACPI_TABLE_HPET *hpet;
108 ACPI_TABLE_HEADER *hdr;
112 /* Only one HPET device can be added. */
113 if (devclass_get_device(acpi_hpet_devclass, 0))
116 /* Currently, ID and minimum clock tick info is unused. */
118 status = AcpiGetTable(ACPI_SIG_HPET, 1, (ACPI_TABLE_HEADER **)&hdr);
119 if (ACPI_FAILURE(status))
123 * The unit number could be derived from hdr->Sequence but we only
124 * support one HPET device.
126 hpet = (ACPI_TABLE_HPET *)hdr;
127 if (hpet->Sequence != 0)
128 printf("ACPI HPET table warning: Sequence is non-zero (%d)\n",
130 child = BUS_ADD_CHILD(parent, ACPI_DEV_BASE_ORDER, "acpi_hpet", 0);
132 printf("%s: can't add child\n", __func__);
136 /* Record a magic value so we can detect this device later. */
137 acpi_set_magic(child, (uintptr_t)&acpi_hpet_devclass);
138 bus_set_resource(child, SYS_RES_MEMORY, 0, hpet->Address.Address,
143 acpi_hpet_probe(device_t dev)
145 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
147 if (acpi_disabled("hpet"))
149 if (!DEV_HPET(dev) &&
150 (ACPI_ID_PROBE(device_get_parent(dev), dev, hpet_ids) == NULL ||
151 device_get_unit(dev) != 0))
154 device_set_desc(dev, "High Precision Event Timer");
159 acpi_hpet_attach(device_t dev)
161 struct acpi_hpet_softc *sc;
166 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
168 sc = device_get_softc(dev);
170 sc->handle = acpi_get_handle(dev);
173 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
175 if (sc->mem_res == NULL)
178 /* Validate that we can access the whole region. */
179 if (rman_get_size(sc->mem_res) < HPET_MEM_WIDTH) {
180 device_printf(dev, "memory region width %ld too small\n",
181 rman_get_size(sc->mem_res));
182 bus_free_resource(dev, SYS_RES_MEMORY, sc->mem_res);
186 /* Be sure timer is enabled. */
189 /* Read basic statistics about the timer. */
190 val = bus_read_4(sc->mem_res, HPET_PERIOD);
192 device_printf(dev, "invalid period\n");
194 bus_free_resource(dev, SYS_RES_MEMORY, sc->mem_res);
198 freq = (1000000000000000LL + val / 2) / val;
200 val = bus_read_4(sc->mem_res, HPET_CAPABILITIES);
202 "vend: 0x%x rev: 0x%x num: %d hz: %jd opts:%s%s\n",
203 val >> 16, val & HPET_CAP_REV_ID,
204 (val & HPET_CAP_NUM_TIM) >> 8, freq,
205 (val & HPET_CAP_LEG_RT) ? " legacy_route" : "",
206 (val & HPET_CAP_COUNT_SIZE) ? " 64-bit" : "");
209 if (testenv("debug.acpi.hpet_test"))
213 * Don't attach if the timer never increments. Since the spec
214 * requires it to be at least 10 MHz, it has to change in 1 us.
216 val = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
218 val2 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
220 device_printf(dev, "HPET never increments, disabling\n");
222 bus_free_resource(dev, SYS_RES_MEMORY, sc->mem_res);
226 hpet_timecounter.tc_frequency = freq;
227 hpet_timecounter.tc_priv = sc;
228 tc_init(&hpet_timecounter);
234 acpi_hpet_detach(device_t dev)
236 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
238 /* XXX Without a tc_remove() function, we can't detach. */
243 acpi_hpet_suspend(device_t dev)
245 struct acpi_hpet_softc *sc;
248 * Disable the timer during suspend. The timer will not lose
249 * its state in S1 or S2, but we are required to disable
252 sc = device_get_softc(dev);
259 acpi_hpet_resume(device_t dev)
261 struct acpi_hpet_softc *sc;
263 /* Re-enable the timer after a resume to keep the clock advancing. */
264 sc = device_get_softc(dev);
270 /* Print some basic latency/rate information to assist in debugging. */
272 acpi_hpet_test(struct acpi_hpet_softc *sc)
276 struct bintime b0, b1, b2;
282 u1 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
283 for (i = 1; i < 1000; i++)
284 u2 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
286 u2 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
288 bintime_sub(&b2, &b1);
289 bintime_sub(&b1, &b0);
290 bintime_sub(&b2, &b1);
291 bintime2timespec(&b2, &ts);
293 device_printf(sc->dev, "%ld.%09ld: %u ... %u = %u\n",
294 (long)ts.tv_sec, ts.tv_nsec, u1, u2, u2 - u1);
296 device_printf(sc->dev, "time per call: %ld ns\n", ts.tv_nsec / 1000);
299 static device_method_t acpi_hpet_methods[] = {
300 /* Device interface */
301 DEVMETHOD(device_identify, acpi_hpet_identify),
302 DEVMETHOD(device_probe, acpi_hpet_probe),
303 DEVMETHOD(device_attach, acpi_hpet_attach),
304 DEVMETHOD(device_detach, acpi_hpet_detach),
305 DEVMETHOD(device_suspend, acpi_hpet_suspend),
306 DEVMETHOD(device_resume, acpi_hpet_resume),
311 static driver_t acpi_hpet_driver = {
314 sizeof(struct acpi_hpet_softc),
318 DRIVER_MODULE(acpi_hpet, acpi, acpi_hpet_driver, acpi_hpet_devclass, 0, 0);
319 MODULE_DEPEND(acpi_hpet, acpi, 1, 1, 1);