hpet: Bark loud if 1024B hpet register space couldn't be mapped
[dragonfly.git] / sys / dev / acpica5 / acpi_hpet.c
bloba6b9fc2077ffe4fa16689e38a24a09c7310ae9be
1 /*-
2 * Copyright (c) 2005 Poul-Henning Kamp
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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
24 * SUCH DAMAGE.
26 * $FreeBSD: src/sys/dev/acpica/acpi_hpet.c,v 1.12.2.1.2.1 2008/11/25 02:59:29 kensmith Exp $
29 #include "opt_acpi.h"
31 #include <sys/param.h>
32 #include <sys/bus.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/systimer.h>
36 #include <sys/rman.h>
38 #include "acpi.h"
39 #include "acpivar.h"
40 #include "acpi_hpet.h"
42 /* Hooks for the ACPI CA debugging infrastructure */
43 #define _COMPONENT ACPI_TIMER
44 ACPI_MODULE_NAME("HPET")
46 static bus_space_handle_t acpi_hpet_bsh;
47 static bus_space_tag_t acpi_hpet_bst;
48 static u_long acpi_hpet_res_start;
50 struct acpi_hpet_softc {
51 device_t dev;
52 struct resource *mem_res;
53 ACPI_HANDLE handle;
56 #define DEV_HPET(x) (acpi_get_magic(x) == (uintptr_t)&acpi_hpet_devclass)
58 static sysclock_t acpi_hpet_get_timecount(void);
59 static void acpi_hpet_construct(struct cputimer *, sysclock_t);
61 static int acpi_hpet_identify(driver_t *, device_t);
62 static int acpi_hpet_probe(device_t);
63 static int acpi_hpet_attach(device_t);
64 static int acpi_hpet_resume(device_t);
65 static int acpi_hpet_suspend(device_t);
67 static void acpi_hpet_test(struct acpi_hpet_softc *sc);
68 static u_int acpi_hpet_read(void);
69 static void acpi_hpet_enable(struct acpi_hpet_softc *);
70 static void acpi_hpet_disable(struct acpi_hpet_softc *);
72 static char *hpet_ids[] = { "PNP0103", NULL };
74 static struct cputimer acpi_hpet_timer = {
75 SLIST_ENTRY_INITIALIZER,
76 "HPET",
77 CPUTIMER_PRI_HPET,
78 CPUTIMER_HPET,
79 acpi_hpet_get_timecount,
80 cputimer_default_fromhz,
81 cputimer_default_fromus,
82 acpi_hpet_construct,
83 cputimer_default_destruct,
85 0, 0, 0
88 static device_method_t acpi_hpet_methods[] = {
89 DEVMETHOD(device_identify, acpi_hpet_identify),
90 DEVMETHOD(device_probe, acpi_hpet_probe),
91 DEVMETHOD(device_attach, acpi_hpet_attach),
92 DEVMETHOD(device_suspend, acpi_hpet_suspend),
93 DEVMETHOD(device_resume, acpi_hpet_resume),
94 { 0, 0 }
97 static driver_t acpi_hpet_driver = {
98 "acpi_hpet",
99 acpi_hpet_methods,
100 sizeof(struct acpi_hpet_softc),
103 static devclass_t acpi_hpet_devclass;
104 DRIVER_MODULE(acpi_hpet, acpi, acpi_hpet_driver, acpi_hpet_devclass, 0, 0);
105 MODULE_DEPEND(acpi_hpet, acpi, 1, 1, 1);
107 static u_int
108 acpi_hpet_read(void)
110 return bus_space_read_4(acpi_hpet_bst, acpi_hpet_bsh,
111 HPET_MAIN_COUNTER);
115 * Locate the ACPI timer using the FADT, set up and allocate the I/O resources
116 * we will be using.
118 static int
119 acpi_hpet_identify(driver_t *driver, device_t parent)
121 ACPI_TABLE_HPET *hpet;
122 ACPI_TABLE_HEADER *hdr;
123 ACPI_STATUS status;
124 device_t child;
127 * Just try once, do nothing if the 'acpi' bus is rescanned.
129 if (device_get_state(parent) == DS_ATTACHED)
130 return 0;
132 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
134 /* Only one HPET device can be added. */
135 if (devclass_get_device(acpi_hpet_devclass, 0))
136 return ENXIO;
138 /* Currently, ID and minimum clock tick info is unused. */
140 status = AcpiGetTable(ACPI_SIG_HPET, 1, (ACPI_TABLE_HEADER **)&hdr);
141 if (ACPI_FAILURE(status))
142 return ENXIO;
145 * The unit number could be derived from hdr->Sequence but we only
146 * support one HPET device.
148 hpet = (ACPI_TABLE_HPET *)hdr;
149 if (hpet->Sequence != 0) {
150 kprintf("ACPI HPET table warning: Sequence is non-zero (%d)\n",
151 hpet->Sequence);
154 child = BUS_ADD_CHILD(parent, parent, 0, "acpi_hpet", 0);
155 if (child == NULL) {
156 device_printf(parent, "%s: can't add acpi_hpet0\n", __func__);
157 return ENXIO;
160 /* Record a magic value so we can detect this device later. */
161 acpi_set_magic(child, (uintptr_t)&acpi_hpet_devclass);
163 acpi_hpet_res_start = hpet->Address.Address;
164 if (bus_set_resource(child, SYS_RES_MEMORY, 0,
165 hpet->Address.Address, HPET_MEM_WIDTH)) {
166 device_printf(child, "could not set iomem resources: "
167 "0x%llx, %d\n", hpet->Address.Address,
168 HPET_MEM_WIDTH);
169 return ENOMEM;
171 return 0;
174 static int
175 acpi_hpet_probe(device_t dev)
177 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
179 if (!acpi_enabled("hpet"))
180 return ENXIO;
182 if (!DEV_HPET(dev) &&
183 (ACPI_ID_PROBE(device_get_parent(dev), dev, hpet_ids) == NULL ||
184 device_get_unit(dev) != 0))
185 return ENXIO;
187 device_set_desc(dev, "High Precision Event Timer");
188 return 0;
191 static int
192 acpi_hpet_attach(device_t dev)
194 struct acpi_hpet_softc *sc;
195 int rid;
196 uint32_t val, val2;
197 uintmax_t freq;
199 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
201 sc = device_get_softc(dev);
202 sc->dev = dev;
203 sc->handle = acpi_get_handle(dev);
205 rid = 0;
206 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
207 RF_ACTIVE);
208 if (sc->mem_res == NULL) {
210 * We only need to make sure that main counter
211 * is accessable.
213 device_printf(dev, "can't map %dB register space, try %dB\n",
214 HPET_MEM_WIDTH, HPET_MEM_WIDTH_MIN);
215 rid = 0;
216 sc->mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
217 acpi_hpet_res_start,
218 acpi_hpet_res_start + HPET_MEM_WIDTH_MIN - 1,
219 HPET_MEM_WIDTH_MIN, RF_ACTIVE);
220 if (sc->mem_res == NULL)
221 return ENOMEM;
224 /* Validate that we can access the whole region. */
225 if (rman_get_size(sc->mem_res) < HPET_MEM_WIDTH_MIN) {
226 device_printf(dev, "memory region width %ld too small\n",
227 rman_get_size(sc->mem_res));
228 bus_release_resource(dev, SYS_RES_MEMORY, rid, sc->mem_res);
229 return ENXIO;
232 acpi_hpet_bsh = rman_get_bushandle(sc->mem_res);
233 acpi_hpet_bst = rman_get_bustag(sc->mem_res);
235 /* Be sure timer is enabled. */
236 acpi_hpet_enable(sc);
238 /* Read basic statistics about the timer. */
239 val = bus_space_read_4(acpi_hpet_bst, acpi_hpet_bsh, HPET_PERIOD);
240 if (val == 0) {
241 device_printf(dev, "invalid period\n");
242 acpi_hpet_disable(sc);
243 bus_release_resource(dev, SYS_RES_MEMORY, rid, sc->mem_res);
244 return ENXIO;
247 freq = (1000000000000000LL + val / 2) / val;
248 if (bootverbose) {
249 val = bus_space_read_4(acpi_hpet_bst, acpi_hpet_bsh,
250 HPET_CAPABILITIES);
251 device_printf(dev,
252 "vend: 0x%x, rev: 0x%x, num: %d, opts:%s%s\n",
253 val >> 16, val & HPET_CAP_REV_ID,
254 (val & HPET_CAP_NUM_TIM) >> 8,
255 (val & HPET_CAP_LEG_RT) ? " legacy_route" : "",
256 (val & HPET_CAP_COUNT_SIZE) ? " 64-bit" : "");
259 if (ktestenv("debug.acpi.hpet_test"))
260 acpi_hpet_test(sc);
263 * Don't attach if the timer never increments. Since the spec
264 * requires it to be at least 10 MHz, it has to change in 1 us.
266 val = bus_space_read_4(acpi_hpet_bst, acpi_hpet_bsh,
267 HPET_MAIN_COUNTER);
268 DELAY(1);
269 val2 = bus_space_read_4(acpi_hpet_bst, acpi_hpet_bsh,
270 HPET_MAIN_COUNTER);
271 if (val == val2) {
272 device_printf(dev, "HPET never increments, disabling\n");
273 acpi_hpet_disable(sc);
274 bus_release_resource(dev, SYS_RES_MEMORY, rid, sc->mem_res);
275 return ENXIO;
278 acpi_hpet_timer.freq = freq;
279 device_printf(dev, "frequency %u\n", acpi_hpet_timer.freq);
281 cputimer_register(&acpi_hpet_timer);
282 cputimer_select(&acpi_hpet_timer, 0);
284 return 0;
288 * Construct the timer. Adjust the base so the system clock does not
289 * jump weirdly.
291 static void
292 acpi_hpet_construct(struct cputimer *timer, sysclock_t oldclock)
294 timer->base = 0;
295 timer->base = oldclock - acpi_hpet_get_timecount();
298 static sysclock_t
299 acpi_hpet_get_timecount(void)
301 return acpi_hpet_read() + acpi_hpet_timer.base;
304 static void
305 acpi_hpet_enable(struct acpi_hpet_softc *sc)
307 uint32_t val;
309 val = bus_space_read_4(acpi_hpet_bst, acpi_hpet_bsh, HPET_CONFIG);
310 bus_space_write_4(acpi_hpet_bst, acpi_hpet_bsh, HPET_CONFIG,
311 val | HPET_CNF_ENABLE);
314 static void
315 acpi_hpet_disable(struct acpi_hpet_softc *sc)
317 uint32_t val;
319 val = bus_space_read_4(acpi_hpet_bst, acpi_hpet_bsh, HPET_CONFIG);
320 bus_space_write_4(acpi_hpet_bst, acpi_hpet_bsh, HPET_CONFIG,
321 val & ~HPET_CNF_ENABLE);
324 static int
325 acpi_hpet_suspend(device_t dev)
327 struct acpi_hpet_softc *sc;
330 * Disable the timer during suspend. The timer will not lose
331 * its state in S1 or S2, but we are required to disable
332 * it.
334 sc = device_get_softc(dev);
335 acpi_hpet_disable(sc);
337 return (0);
340 static int
341 acpi_hpet_resume(device_t dev)
343 struct acpi_hpet_softc *sc;
345 /* Re-enable the timer after a resume to keep the clock advancing. */
346 sc = device_get_softc(dev);
347 acpi_hpet_enable(sc);
349 return (0);
352 /* Print some basic latency/rate information to assist in debugging. */
353 static void
354 acpi_hpet_test(struct acpi_hpet_softc *sc)
356 int i;
357 uint32_t u1, u2;
358 struct timeval b0, b1, b2;
359 struct timespec ts;
361 microuptime(&b0);
362 microuptime(&b0);
363 microuptime(&b1);
364 u1 = bus_space_read_4(acpi_hpet_bst, acpi_hpet_bsh, HPET_MAIN_COUNTER);
365 for (i = 1; i < 1000; i++) {
366 u2 = bus_space_read_4(acpi_hpet_bst, acpi_hpet_bsh,
367 HPET_MAIN_COUNTER);
369 microuptime(&b2);
370 u2 = bus_space_read_4(acpi_hpet_bst, acpi_hpet_bsh, HPET_MAIN_COUNTER);
372 timevalsub(&b2, &b1);
373 timevalsub(&b1, &b0);
374 timevalsub(&b2, &b1);
376 TIMEVAL_TO_TIMESPEC(&b2, &ts);
378 device_printf(sc->dev, "%ld.%09ld: %u ... %u = %u\n",
379 (long)b2.tv_sec, b2.tv_usec, u1, u2, u2 - u1);
381 device_printf(sc->dev, "time per call: %ld ns\n", ts.tv_nsec / 1000);