acpi_hpet.c revision 185103
1132242Stjr/*-
2132242Stjr * Copyright (c) 2005 Poul-Henning Kamp
3132242Stjr * All rights reserved.
4132242Stjr *
5227753Stheraven * Redistribution and use in source and binary forms, with or without
6227753Stheraven * modification, are permitted provided that the following conditions
7227753Stheraven * are met:
8227753Stheraven * 1. Redistributions of source code must retain the above copyright
9227753Stheraven *    notice, this list of conditions and the following disclaimer.
10132242Stjr * 2. Redistributions in binary form must reproduce the above copyright
11132242Stjr *    notice, this list of conditions and the following disclaimer in the
12132242Stjr *    documentation and/or other materials provided with the distribution.
13132242Stjr *
14132242Stjr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15132242Stjr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16132242Stjr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17132242Stjr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18132242Stjr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19132242Stjr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20132242Stjr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21132242Stjr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22132242Stjr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23132242Stjr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24132242Stjr * SUCH DAMAGE.
25132242Stjr */
26132242Stjr
27132242Stjr#include <sys/cdefs.h>
28132242Stjr__FBSDID("$FreeBSD: head/sys/dev/acpica/acpi_hpet.c 185103 2008-11-19 20:31:38Z jkim $");
29132242Stjr
30132242Stjr#include "opt_acpi.h"
31132242Stjr#include <sys/param.h>
32132242Stjr#include <sys/bus.h>
33132242Stjr#include <sys/kernel.h>
34132242Stjr#include <sys/module.h>
35132242Stjr#include <sys/rman.h>
36132242Stjr#include <sys/time.h>
37132242Stjr#include <sys/timetc.h>
38132242Stjr
39132242Stjr#include <contrib/dev/acpica/acpi.h>
40132242Stjr#include <dev/acpica/acpivar.h>
41227753Stheraven#include <dev/acpica/acpi_hpet.h>
42132242Stjr
43291336SngieACPI_SERIAL_DECL(hpet, "ACPI HPET support");
44291336Sngie
45132242Stjrstatic devclass_t acpi_hpet_devclass;
46227753Stheraven
47132242Stjr/* ACPI CA debugging */
48132242Stjr#define _COMPONENT	ACPI_TIMER
49132242StjrACPI_MODULE_NAME("HPET")
50227753Stheraven
51132242Stjrstruct acpi_hpet_softc {
52132242Stjr	device_t		dev;
53132242Stjr	struct resource		*mem_res;
54132242Stjr	ACPI_HANDLE		handle;
55132242Stjr};
56227753Stheraven
57132242Stjrstatic u_int hpet_get_timecount(struct timecounter *tc);
58132242Stjrstatic void acpi_hpet_test(struct acpi_hpet_softc *sc);
59132242Stjr
60132242Stjrstatic char *hpet_ids[] = { "PNP0103", NULL };
61133223Stjr
62132242Stjr#define DEV_HPET(x)	(acpi_get_magic(x) == (uintptr_t)&acpi_hpet_devclass)
63132242Stjr
64132242Stjrstruct timecounter hpet_timecounter = {
65132242Stjr	.tc_get_timecount =	hpet_get_timecount,
66132242Stjr	.tc_counter_mask =	~0u,
67132242Stjr	.tc_name =		"HPET",
68132242Stjr	.tc_quality =		900,
69132242Stjr};
70132242Stjr
71132242Stjrstatic u_int
72132242Stjrhpet_get_timecount(struct timecounter *tc)
73132242Stjr{
74132242Stjr	struct acpi_hpet_softc *sc;
75132242Stjr
76132242Stjr	sc = tc->tc_priv;
77227753Stheraven	return (bus_read_4(sc->mem_res, HPET_MAIN_COUNTER));
78227753Stheraven}
79227753Stheraven
80227753Stheravenstatic void
81227753Stheravenhpet_enable(struct acpi_hpet_softc *sc)
82{
83	uint32_t val;
84
85	val = bus_read_4(sc->mem_res, HPET_CONFIG);
86	val &= ~HPET_CNF_LEG_RT;
87	val |= HPET_CNF_ENABLE;
88	bus_write_4(sc->mem_res, HPET_CONFIG, val);
89}
90
91static void
92hpet_disable(struct acpi_hpet_softc *sc)
93{
94	uint32_t val;
95
96	val = bus_read_4(sc->mem_res, HPET_CONFIG);
97	val &= ~HPET_CNF_ENABLE;
98	bus_write_4(sc->mem_res, HPET_CONFIG, val);
99}
100
101/* Discover the HPET via the ACPI table of the same name. */
102static void
103acpi_hpet_identify(driver_t *driver, device_t parent)
104{
105	ACPI_TABLE_HPET *hpet;
106	ACPI_TABLE_HEADER *hdr;
107	ACPI_STATUS	status;
108	device_t	child;
109
110	/* Only one HPET device can be added. */
111	if (devclass_get_device(acpi_hpet_devclass, 0))
112		return;
113
114	/* Currently, ID and minimum clock tick info is unused. */
115
116	status = AcpiGetTable(ACPI_SIG_HPET, 1, (ACPI_TABLE_HEADER **)&hdr);
117	if (ACPI_FAILURE(status))
118		return;
119
120	/*
121	 * The unit number could be derived from hdr->Sequence but we only
122	 * support one HPET device.
123	 */
124	hpet = (ACPI_TABLE_HPET *)hdr;
125	if (hpet->Sequence != 0)
126		printf("ACPI HPET table warning: Sequence is non-zero (%d)\n",
127		    hpet->Sequence);
128	child = BUS_ADD_CHILD(parent, ACPI_DEV_BASE_ORDER, "acpi_hpet", 0);
129	if (child == NULL) {
130		printf("%s: can't add child\n", __func__);
131		return;
132	}
133
134	/* Record a magic value so we can detect this device later. */
135	acpi_set_magic(child, (uintptr_t)&acpi_hpet_devclass);
136	bus_set_resource(child, SYS_RES_MEMORY, 0, hpet->Address.Address,
137	    HPET_MEM_WIDTH);
138}
139
140static int
141acpi_hpet_probe(device_t dev)
142{
143	ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
144
145	if (acpi_disabled("hpet"))
146		return (ENXIO);
147	if (!DEV_HPET(dev) &&
148	    (ACPI_ID_PROBE(device_get_parent(dev), dev, hpet_ids) == NULL ||
149	    device_get_unit(dev) != 0))
150		return (ENXIO);
151
152	device_set_desc(dev, "High Precision Event Timer");
153	return (0);
154}
155
156static int
157acpi_hpet_attach(device_t dev)
158{
159	struct acpi_hpet_softc *sc;
160	int rid;
161	uint32_t val, val2;
162	uintmax_t freq;
163
164	ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
165
166	sc = device_get_softc(dev);
167	sc->dev = dev;
168	sc->handle = acpi_get_handle(dev);
169
170	rid = 0;
171	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
172	    RF_ACTIVE);
173	if (sc->mem_res == NULL)
174		return (ENOMEM);
175
176	/* Validate that we can access the whole region. */
177	if (rman_get_size(sc->mem_res) < HPET_MEM_WIDTH) {
178		device_printf(dev, "memory region width %ld too small\n",
179		    rman_get_size(sc->mem_res));
180		bus_free_resource(dev, SYS_RES_MEMORY, sc->mem_res);
181		return (ENXIO);
182	}
183
184	/* Be sure timer is enabled. */
185	hpet_enable(sc);
186
187	/* Read basic statistics about the timer. */
188	val = bus_read_4(sc->mem_res, HPET_PERIOD);
189	if (val == 0) {
190		device_printf(dev, "invalid period\n");
191		hpet_disable(sc);
192		bus_free_resource(dev, SYS_RES_MEMORY, sc->mem_res);
193		return (ENXIO);
194	}
195
196	freq = (1000000000000000LL + val / 2) / val;
197	if (bootverbose) {
198		val = bus_read_4(sc->mem_res, HPET_CAPABILITIES);
199		device_printf(dev,
200		    "vend: 0x%x rev: 0x%x num: %d hz: %jd opts:%s%s\n",
201		    val >> 16, val & HPET_CAP_REV_ID,
202		    (val & HPET_CAP_NUM_TIM) >> 8, freq,
203		    (val & HPET_CAP_LEG_RT) ? " legacy_route" : "",
204		    (val & HPET_CAP_COUNT_SIZE) ? " 64-bit" : "");
205	}
206
207	if (testenv("debug.acpi.hpet_test"))
208		acpi_hpet_test(sc);
209
210	/*
211	 * Don't attach if the timer never increments.  Since the spec
212	 * requires it to be at least 10 MHz, it has to change in 1 us.
213	 */
214	val = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
215	DELAY(1);
216	val2 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
217	if (val == val2) {
218		device_printf(dev, "HPET never increments, disabling\n");
219		hpet_disable(sc);
220		bus_free_resource(dev, SYS_RES_MEMORY, sc->mem_res);
221		return (ENXIO);
222	}
223
224	hpet_timecounter.tc_frequency = freq;
225	hpet_timecounter.tc_priv = sc;
226	tc_init(&hpet_timecounter);
227
228	return (0);
229}
230
231static int
232acpi_hpet_detach(device_t dev)
233{
234	ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
235
236	/* XXX Without a tc_remove() function, we can't detach. */
237	return (EBUSY);
238}
239
240static int
241acpi_hpet_suspend(device_t dev)
242{
243	struct acpi_hpet_softc *sc;
244
245	/*
246	 * Disable the timer during suspend.  The timer will not lose
247	 * its state in S1 or S2, but we are required to disable
248	 * it.
249	 */
250	sc = device_get_softc(dev);
251	hpet_disable(sc);
252
253	return (0);
254}
255
256static int
257acpi_hpet_resume(device_t dev)
258{
259	struct acpi_hpet_softc *sc;
260
261	/* Re-enable the timer after a resume to keep the clock advancing. */
262	sc = device_get_softc(dev);
263	hpet_enable(sc);
264
265	return (0);
266}
267
268/* Print some basic latency/rate information to assist in debugging. */
269static void
270acpi_hpet_test(struct acpi_hpet_softc *sc)
271{
272	int i;
273	uint32_t u1, u2;
274	struct bintime b0, b1, b2;
275	struct timespec ts;
276
277	binuptime(&b0);
278	binuptime(&b0);
279	binuptime(&b1);
280	u1 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
281	for (i = 1; i < 1000; i++)
282		u2 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
283	binuptime(&b2);
284	u2 = bus_read_4(sc->mem_res, HPET_MAIN_COUNTER);
285
286	bintime_sub(&b2, &b1);
287	bintime_sub(&b1, &b0);
288	bintime_sub(&b2, &b1);
289	bintime2timespec(&b2, &ts);
290
291	device_printf(sc->dev, "%ld.%09ld: %u ... %u = %u\n",
292	    (long)ts.tv_sec, ts.tv_nsec, u1, u2, u2 - u1);
293
294	device_printf(sc->dev, "time per call: %ld ns\n", ts.tv_nsec / 1000);
295}
296
297static device_method_t acpi_hpet_methods[] = {
298	/* Device interface */
299	DEVMETHOD(device_identify, acpi_hpet_identify),
300	DEVMETHOD(device_probe, acpi_hpet_probe),
301	DEVMETHOD(device_attach, acpi_hpet_attach),
302	DEVMETHOD(device_detach, acpi_hpet_detach),
303	DEVMETHOD(device_suspend, acpi_hpet_suspend),
304	DEVMETHOD(device_resume, acpi_hpet_resume),
305
306	{0, 0}
307};
308
309static driver_t	acpi_hpet_driver = {
310	"acpi_hpet",
311	acpi_hpet_methods,
312	sizeof(struct acpi_hpet_softc),
313};
314
315
316DRIVER_MODULE(acpi_hpet, acpi, acpi_hpet_driver, acpi_hpet_devclass, 0, 0);
317MODULE_DEPEND(acpi_hpet, acpi, 1, 1, 1);
318