acpi_hpet.c revision 168010
1/*-
2 * Copyright (c) 2005 Poul-Henning Kamp
3 * All rights reserved.
4 *
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.
13 *
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.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/dev/acpica/acpi_hpet.c 168010 2007-03-28 22:28:48Z njl $");
29
30#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/rman.h>
36#include <sys/time.h>
37#include <sys/timetc.h>
38
39#include <contrib/dev/acpica/acpi.h>
40#include <dev/acpica/acpivar.h>
41
42ACPI_SERIAL_DECL(hpet, "ACPI HPET support");
43
44/* ACPI CA debugging */
45#define _COMPONENT	ACPI_TIMER
46ACPI_MODULE_NAME("HPET")
47
48struct acpi_hpet_softc {
49	device_t		dev;
50	struct resource		*mem_res;
51	ACPI_HANDLE		handle;
52};
53
54static u_int hpet_get_timecount(struct timecounter *tc);
55static void acpi_hpet_test(struct acpi_hpet_softc *sc);
56
57static char *hpet_ids[] = { "PNP0103", NULL };
58
59#define HPET_MEM_WIDTH		0x400	/* Expected memory region size */
60#define HPET_OFFSET_INFO	0	/* Location of info in region */
61#define HPET_OFFSET_PERIOD	4	/* Location of period (1/hz) */
62#define HPET_OFFSET_ENABLE	0x10	/* Location of enable word */
63#define HPET_OFFSET_VALUE	0xf0	/* Location of actual timer value */
64
65struct timecounter hpet_timecounter = {
66	.tc_get_timecount =	hpet_get_timecount,
67	.tc_counter_mask =	~0u,
68	.tc_name =		"HPET",
69	.tc_quality =		2000,
70};
71
72static u_int
73hpet_get_timecount(struct timecounter *tc)
74{
75	struct acpi_hpet_softc *sc;
76
77	sc = tc->tc_priv;
78	return (bus_read_4(sc->mem_res, HPET_OFFSET_VALUE));
79}
80
81static int
82acpi_hpet_probe(device_t dev)
83{
84	ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
85
86	if (acpi_disabled("hpet") ||
87	    ACPI_ID_PROBE(device_get_parent(dev), dev, hpet_ids) == NULL ||
88	    device_get_unit(dev) != 0)
89		return (ENXIO);
90
91	device_set_desc(dev, "High Precision Event Timer");
92	return (0);
93}
94
95static int
96acpi_hpet_attach(device_t dev)
97{
98	struct acpi_hpet_softc *sc;
99	int rid;
100	uint32_t val;
101	uintmax_t freq;
102
103	ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
104
105	sc = device_get_softc(dev);
106	sc->dev = dev;
107	sc->handle = acpi_get_handle(dev);
108
109	rid = 0;
110	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
111	    RF_ACTIVE);
112	if (sc->mem_res == NULL)
113		return (ENOMEM);
114
115	/* Validate that we can access the whole region. */
116	if (rman_get_size(sc->mem_res) < HPET_MEM_WIDTH) {
117		device_printf(dev, "memory region width %ld too small\n",
118		    rman_get_size(sc->mem_res));
119		bus_free_resource(dev, SYS_RES_MEMORY, sc->mem_res);
120		return (ENXIO);
121	}
122
123	/* Read basic statistics about the timer. */
124	val = bus_read_4(sc->mem_res, HPET_OFFSET_PERIOD);
125	freq = (1000000000000000LL + val / 2) / val;
126	if (bootverbose) {
127		val = bus_read_4(sc->mem_res, HPET_OFFSET_INFO);
128		device_printf(dev,
129		    "vend: 0x%x rev: 0x%x num: %d hz: %jd opts:%s%s\n",
130		    val >> 16, val & 0xff, (val >> 18) & 0xf, freq,
131		    ((val >> 15) & 1) ? " leg_route" : "",
132		    ((val >> 13) & 1) ? " count_size" : "");
133	}
134
135	/* Be sure it is enabled. */
136	bus_write_4(sc->mem_res, HPET_OFFSET_ENABLE, 1);
137
138	if (testenv("debug.acpi.hpet_test"))
139		acpi_hpet_test(sc);
140
141	hpet_timecounter.tc_frequency = freq;
142	hpet_timecounter.tc_priv = sc;
143	tc_init(&hpet_timecounter);
144
145	return (0);
146}
147
148static int
149acpi_hpet_detach(device_t dev)
150{
151	ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
152
153	/* XXX Without a tc_remove() function, we can't detach. */
154	return (EBUSY);
155}
156
157static int
158acpi_hpet_resume(device_t dev)
159{
160	struct acpi_hpet_softc *sc;
161
162	/* Re-enable the timer after a resume to keep the clock advancing. */
163	sc = device_get_softc(dev);
164	bus_write_4(sc->mem_res, HPET_OFFSET_ENABLE, 1);
165
166	return (0);
167}
168
169/* Print some basic latency/rate information to assist in debugging. */
170static void
171acpi_hpet_test(struct acpi_hpet_softc *sc)
172{
173	int i;
174	uint32_t u1, u2;
175	struct bintime b0, b1, b2;
176	struct timespec ts;
177
178	binuptime(&b0);
179	binuptime(&b0);
180	binuptime(&b1);
181	u1 = bus_read_4(sc->mem_res, HPET_OFFSET_VALUE);
182	for (i = 1; i < 1000; i++)
183		u2 = bus_read_4(sc->mem_res, HPET_OFFSET_VALUE);
184	binuptime(&b2);
185	u2 = bus_read_4(sc->mem_res, HPET_OFFSET_VALUE);
186
187	bintime_sub(&b2, &b1);
188	bintime_sub(&b1, &b0);
189	bintime_sub(&b2, &b1);
190	bintime2timespec(&b2, &ts);
191
192	device_printf(sc->dev, "%ld.%09ld: %u ... %u = %u\n",
193	    (long)ts.tv_sec, ts.tv_nsec, u1, u2, u2 - u1);
194
195	device_printf(sc->dev, "time per call: %ld ns\n", ts.tv_nsec / 1000);
196}
197
198static device_method_t acpi_hpet_methods[] = {
199	/* Device interface */
200	DEVMETHOD(device_probe, acpi_hpet_probe),
201	DEVMETHOD(device_attach, acpi_hpet_attach),
202	DEVMETHOD(device_detach, acpi_hpet_detach),
203	DEVMETHOD(device_resume, acpi_hpet_resume),
204
205	{0, 0}
206};
207
208static driver_t	acpi_hpet_driver = {
209	"acpi_hpet",
210	acpi_hpet_methods,
211	sizeof(struct acpi_hpet_softc),
212};
213
214static devclass_t acpi_hpet_devclass;
215
216DRIVER_MODULE(acpi_hpet, acpi, acpi_hpet_driver, acpi_hpet_devclass, 0, 0);
217MODULE_DEPEND(acpi_hpet, acpi, 1, 1, 1);
218