DRM from FreeBSD current, tested for r600
[dragonfly.git] / sys / dev / acpica5 / acpi_hpet.c
blobd712351c52815e51b4a42367ab2cae7f93561b55
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 "accommon.h"
40 #include "acpivar.h"
41 #include "acpi_hpet.h"
43 /* Hooks for the ACPI CA debugging infrastructure */
44 #define _COMPONENT ACPI_TIMER
45 ACPI_MODULE_NAME("HPET")
47 static bus_space_handle_t acpi_hpet_bsh;
48 static bus_space_tag_t acpi_hpet_bst;
49 static u_long acpi_hpet_res_start;
51 struct acpi_hpet_softc {
52 device_t dev;
53 struct resource *mem_res;
54 ACPI_HANDLE handle;
57 #define DEV_HPET(x) (acpi_get_magic(x) == (uintptr_t)&acpi_hpet_devclass)
59 static sysclock_t acpi_hpet_get_timecount(void);
60 static void acpi_hpet_construct(struct cputimer *, sysclock_t);
62 static int acpi_hpet_identify(driver_t *, device_t);
63 static int acpi_hpet_probe(device_t);
64 static int acpi_hpet_attach(device_t);
65 static int acpi_hpet_resume(device_t);
66 static int acpi_hpet_suspend(device_t);
68 static void acpi_hpet_test(struct acpi_hpet_softc *sc);
69 static u_int acpi_hpet_read(void);
70 static void acpi_hpet_enable(struct acpi_hpet_softc *);
71 static void acpi_hpet_disable(struct acpi_hpet_softc *);
73 static char *hpet_ids[] = { "PNP0103", NULL };
75 static struct cputimer acpi_hpet_timer = {
76 SLIST_ENTRY_INITIALIZER,
77 "HPET",
78 CPUTIMER_PRI_HPET,
79 CPUTIMER_HPET,
80 acpi_hpet_get_timecount,
81 cputimer_default_fromhz,
82 cputimer_default_fromus,
83 acpi_hpet_construct,
84 cputimer_default_destruct,
86 0, 0, 0
89 static device_method_t acpi_hpet_methods[] = {
90 DEVMETHOD(device_identify, acpi_hpet_identify),
91 DEVMETHOD(device_probe, acpi_hpet_probe),
92 DEVMETHOD(device_attach, acpi_hpet_attach),
93 DEVMETHOD(device_suspend, acpi_hpet_suspend),
94 DEVMETHOD(device_resume, acpi_hpet_resume),
95 { 0, 0 }
98 static driver_t acpi_hpet_driver = {
99 "acpi_hpet",
100 acpi_hpet_methods,
101 sizeof(struct acpi_hpet_softc),
104 static devclass_t acpi_hpet_devclass;
105 DRIVER_MODULE(acpi_hpet, acpi, acpi_hpet_driver, acpi_hpet_devclass, NULL, NULL);
106 MODULE_DEPEND(acpi_hpet, acpi, 1, 1, 1);
108 static u_int
109 acpi_hpet_read(void)
111 return bus_space_read_4(acpi_hpet_bst, acpi_hpet_bsh,
112 HPET_MAIN_COUNTER);
116 * Locate the ACPI timer using the FADT, set up and allocate the I/O resources
117 * we will be using.
119 static int
120 acpi_hpet_identify(driver_t *driver, device_t parent)
122 ACPI_TABLE_HPET *hpet;
123 ACPI_TABLE_HEADER *hdr;
124 ACPI_STATUS status;
125 device_t child;
128 * Just try once, do nothing if the 'acpi' bus is rescanned.
130 if (device_get_state(parent) == DS_ATTACHED)
131 return 0;
133 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
135 /* Only one HPET device can be added. */
136 if (devclass_get_device(acpi_hpet_devclass, 0))
137 return ENXIO;
139 /* Currently, ID and minimum clock tick info is unused. */
141 status = AcpiGetTable(ACPI_SIG_HPET, 1, (ACPI_TABLE_HEADER **)&hdr);
142 if (ACPI_FAILURE(status))
143 return ENXIO;
146 * The unit number could be derived from hdr->Sequence but we only
147 * support one HPET device.
149 hpet = (ACPI_TABLE_HPET *)hdr;
150 if (hpet->Sequence != 0) {
151 kprintf("ACPI HPET table warning: Sequence is non-zero (%d)\n",
152 hpet->Sequence);
155 child = BUS_ADD_CHILD(parent, parent, 0, "acpi_hpet", 0);
156 if (child == NULL) {
157 device_printf(parent, "%s: can't add acpi_hpet0\n", __func__);
158 return ENXIO;
161 /* Record a magic value so we can detect this device later. */
162 acpi_set_magic(child, (uintptr_t)&acpi_hpet_devclass);
164 acpi_hpet_res_start = hpet->Address.Address;
165 if (bus_set_resource(child, SYS_RES_MEMORY, 0,
166 hpet->Address.Address, HPET_MEM_WIDTH, -1)) {
167 device_printf(child, "could not set iomem resources: "
168 "0x%jx, %d\n", (uintmax_t)hpet->Address.Address,
169 HPET_MEM_WIDTH);
170 return ENOMEM;
172 return 0;
175 static int
176 acpi_hpet_probe(device_t dev)
178 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
180 if (acpi_disabled("hpet"))
181 return ENXIO;
183 if (!DEV_HPET(dev) &&
184 (ACPI_ID_PROBE(device_get_parent(dev), dev, hpet_ids) == NULL ||
185 device_get_unit(dev) != 0))
186 return ENXIO;
188 device_set_desc(dev, "High Precision Event Timer");
189 return 0;
192 static int
193 acpi_hpet_attach(device_t dev)
195 struct acpi_hpet_softc *sc;
196 int rid;
197 uint32_t val, val2;
198 uintmax_t freq;
200 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
202 sc = device_get_softc(dev);
203 sc->dev = dev;
204 sc->handle = acpi_get_handle(dev);
206 rid = 0;
207 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
208 RF_ACTIVE);
209 if (sc->mem_res == NULL) {
211 * We only need to make sure that main counter
212 * is accessable.
214 device_printf(dev, "can't map %dB register space, try %dB\n",
215 HPET_MEM_WIDTH, HPET_MEM_WIDTH_MIN);
216 rid = 0;
217 sc->mem_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
218 acpi_hpet_res_start,
219 acpi_hpet_res_start + HPET_MEM_WIDTH_MIN - 1,
220 HPET_MEM_WIDTH_MIN, RF_ACTIVE);
221 if (sc->mem_res == NULL)
222 return ENOMEM;
225 /* Validate that we can access the whole region. */
226 if (rman_get_size(sc->mem_res) < HPET_MEM_WIDTH_MIN) {
227 device_printf(dev, "memory region width %ld too small\n",
228 rman_get_size(sc->mem_res));
229 bus_release_resource(dev, SYS_RES_MEMORY, rid, sc->mem_res);
230 return ENXIO;
233 acpi_hpet_bsh = rman_get_bushandle(sc->mem_res);
234 acpi_hpet_bst = rman_get_bustag(sc->mem_res);
236 /* Be sure timer is enabled. */
237 acpi_hpet_enable(sc);
239 /* Read basic statistics about the timer. */
240 val = bus_space_read_4(acpi_hpet_bst, acpi_hpet_bsh, HPET_PERIOD);
241 if (val == 0) {
242 device_printf(dev, "invalid period\n");
243 acpi_hpet_disable(sc);
244 bus_release_resource(dev, SYS_RES_MEMORY, rid, sc->mem_res);
245 return ENXIO;
248 freq = (1000000000000000LL + val / 2) / val;
249 if (bootverbose) {
250 val = bus_space_read_4(acpi_hpet_bst, acpi_hpet_bsh,
251 HPET_CAPABILITIES);
252 device_printf(dev,
253 "vend: 0x%x, rev: 0x%x, num: %d, opts:%s%s\n",
254 val >> 16, val & HPET_CAP_REV_ID,
255 (val & HPET_CAP_NUM_TIM) >> 8,
256 (val & HPET_CAP_LEG_RT) ? " legacy_route" : "",
257 (val & HPET_CAP_COUNT_SIZE) ? " 64-bit" : "");
260 if (ktestenv("debug.acpi.hpet_test"))
261 acpi_hpet_test(sc);
264 * Don't attach if the timer never increments. Since the spec
265 * requires it to be at least 10 MHz, it has to change in 1 us.
267 val = bus_space_read_4(acpi_hpet_bst, acpi_hpet_bsh,
268 HPET_MAIN_COUNTER);
269 DELAY(1);
270 val2 = bus_space_read_4(acpi_hpet_bst, acpi_hpet_bsh,
271 HPET_MAIN_COUNTER);
272 if (val == val2) {
273 device_printf(dev, "HPET never increments, disabling\n");
274 acpi_hpet_disable(sc);
275 bus_release_resource(dev, SYS_RES_MEMORY, rid, sc->mem_res);
276 return ENXIO;
279 acpi_hpet_timer.freq = freq;
280 device_printf(dev, "frequency %u\n", acpi_hpet_timer.freq);
282 cputimer_register(&acpi_hpet_timer);
283 cputimer_select(&acpi_hpet_timer, 0);
285 return 0;
289 * Construct the timer. Adjust the base so the system clock does not
290 * jump weirdly.
292 static void
293 acpi_hpet_construct(struct cputimer *timer, sysclock_t oldclock)
295 timer->base = 0;
296 timer->base = oldclock - acpi_hpet_get_timecount();
299 static sysclock_t
300 acpi_hpet_get_timecount(void)
302 return acpi_hpet_read() + acpi_hpet_timer.base;
305 static void
306 acpi_hpet_enable(struct acpi_hpet_softc *sc)
308 uint32_t val;
310 val = bus_space_read_4(acpi_hpet_bst, acpi_hpet_bsh, HPET_CONFIG);
311 bus_space_write_4(acpi_hpet_bst, acpi_hpet_bsh, HPET_CONFIG,
312 val | HPET_CNF_ENABLE);
315 static void
316 acpi_hpet_disable(struct acpi_hpet_softc *sc)
318 uint32_t val;
320 val = bus_space_read_4(acpi_hpet_bst, acpi_hpet_bsh, HPET_CONFIG);
321 bus_space_write_4(acpi_hpet_bst, acpi_hpet_bsh, HPET_CONFIG,
322 val & ~HPET_CNF_ENABLE);
325 static int
326 acpi_hpet_suspend(device_t dev)
329 * According to IA-PC HPET specification rev 1.0a
331 * Page 10, 2.3.3:
332 * "1. The Event Timer registers (including the main counter)
333 * are not expected to be preserved through an S3, S4, or S5
334 * state."
336 * Page 11, 2.3.3:
337 * "3. The main counter is permitted, but not required, to run
338 * during S1 or S2 states. ..."
340 * These mean we are not allowed to enter any of Sx states,
341 * if HPET is used as the sys_cputimer.
343 if (sys_cputimer != &acpi_hpet_timer) {
344 struct acpi_hpet_softc *sc;
346 sc = device_get_softc(dev);
347 acpi_hpet_disable(sc);
349 return 0;
350 } else {
351 return EOPNOTSUPP;
355 static int
356 acpi_hpet_resume(device_t dev)
358 if (sys_cputimer != &acpi_hpet_timer) {
359 struct acpi_hpet_softc *sc;
361 sc = device_get_softc(dev);
362 acpi_hpet_enable(sc);
364 return 0;
367 /* Print some basic latency/rate information to assist in debugging. */
368 static void
369 acpi_hpet_test(struct acpi_hpet_softc *sc)
371 int i;
372 uint32_t u1, u2;
373 struct timeval b0, b1, b2;
374 struct timespec ts;
376 microuptime(&b0);
377 microuptime(&b0);
378 microuptime(&b1);
379 u1 = bus_space_read_4(acpi_hpet_bst, acpi_hpet_bsh, HPET_MAIN_COUNTER);
380 for (i = 1; i < 1000; i++) {
381 u2 = bus_space_read_4(acpi_hpet_bst, acpi_hpet_bsh,
382 HPET_MAIN_COUNTER);
384 microuptime(&b2);
385 u2 = bus_space_read_4(acpi_hpet_bst, acpi_hpet_bsh, HPET_MAIN_COUNTER);
387 timevalsub(&b2, &b1);
388 timevalsub(&b1, &b0);
389 timevalsub(&b2, &b1);
391 TIMEVAL_TO_TIMESPEC(&b2, &ts);
393 device_printf(sc->dev, "%ld.%09ld: %u ... %u = %u\n",
394 (long)b2.tv_sec, b2.tv_usec, u1, u2, u2 - u1);
396 device_printf(sc->dev, "time per call: %ld ns\n", ts.tv_nsec / 1000);