xref: /freebsd/sys/dev/acpica/acpi_hpet.c (revision b28624fde638caadd4a89f50c9b7e7da0f98c4d2)
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$");
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 
42 ACPI_SERIAL_DECL(hpet, "ACPI HPET support");
43 
44 static devclass_t acpi_hpet_devclass;
45 
46 /* ACPI CA debugging */
47 #define _COMPONENT	ACPI_TIMER
48 ACPI_MODULE_NAME("HPET")
49 
50 struct acpi_hpet_softc {
51 	device_t		dev;
52 	struct resource		*mem_res;
53 	ACPI_HANDLE		handle;
54 };
55 
56 static u_int hpet_get_timecount(struct timecounter *tc);
57 static void acpi_hpet_test(struct acpi_hpet_softc *sc);
58 
59 static char *hpet_ids[] = { "PNP0103", NULL };
60 
61 #define HPET_MEM_WIDTH		0x400	/* Expected memory region size */
62 #define HPET_OFFSET_INFO	0	/* Location of info in region */
63 #define HPET_OFFSET_PERIOD	4	/* Location of period (1/hz) */
64 #define HPET_OFFSET_ENABLE	0x10	/* Location of enable word */
65 #define HPET_OFFSET_VALUE	0xf0	/* Location of actual timer value */
66 
67 #define DEV_HPET(x)	(acpi_get_magic(x) == (uintptr_t)&acpi_hpet_devclass)
68 
69 struct timecounter hpet_timecounter = {
70 	.tc_get_timecount =	hpet_get_timecount,
71 	.tc_counter_mask =	~0u,
72 	.tc_name =		"HPET",
73 	.tc_quality =		900,
74 };
75 
76 static u_int
77 hpet_get_timecount(struct timecounter *tc)
78 {
79 	struct acpi_hpet_softc *sc;
80 
81 	sc = tc->tc_priv;
82 	return (bus_read_4(sc->mem_res, HPET_OFFSET_VALUE));
83 }
84 
85 /* Discover the HPET via the ACPI table of the same name. */
86 void
87 acpi_hpet_table_probe(device_t parent)
88 {
89 	ACPI_TABLE_HPET *hpet;
90 	ACPI_TABLE_HEADER *hdr;
91 	ACPI_STATUS	status;
92 	device_t	child;
93 
94 	/* Currently, ID and minimum clock tick info is unused. */
95 
96 	status = AcpiGetTable(ACPI_SIG_HPET, 1, (ACPI_TABLE_HEADER **)&hdr);
97 	if (ACPI_FAILURE(status))
98 		return;
99 
100 	/*
101 	 * The unit number could be derived from hdr->Sequence but we only
102 	 * support one HPET device.
103 	 */
104 	hpet = (ACPI_TABLE_HPET *)hdr;
105 	if (hpet->Sequence != 0)
106 		printf("ACPI HPET table warning: Sequence is non-zero (%d)\n",
107 		    hpet->Sequence);
108 	child = BUS_ADD_CHILD(parent, 0, "acpi_hpet", 0);
109 	if (child == NULL) {
110 		printf("%s: can't add child\n", __func__);
111 		return;
112 	}
113 
114 	/* Record a magic value so we can detect this device later. */
115 	acpi_set_magic(child, (uintptr_t)&acpi_hpet_devclass);
116 	bus_set_resource(child, SYS_RES_MEMORY, 0, hpet->Address.Address,
117 	    HPET_MEM_WIDTH);
118 	if (device_probe_and_attach(child) != 0)
119 		device_delete_child(parent, child);
120 }
121 
122 static int
123 acpi_hpet_probe(device_t dev)
124 {
125 	ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
126 
127 	if (acpi_disabled("hpet"))
128 		return (ENXIO);
129 	if (!DEV_HPET(dev) &&
130 	    (ACPI_ID_PROBE(device_get_parent(dev), dev, hpet_ids) == NULL ||
131 	    device_get_unit(dev) != 0))
132 		return (ENXIO);
133 
134 	device_set_desc(dev, "High Precision Event Timer");
135 	return (0);
136 }
137 
138 static int
139 acpi_hpet_attach(device_t dev)
140 {
141 	struct acpi_hpet_softc *sc;
142 	int rid;
143 	uint32_t val, val2;
144 	uintmax_t freq;
145 
146 	ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
147 
148 	sc = device_get_softc(dev);
149 	sc->dev = dev;
150 	sc->handle = acpi_get_handle(dev);
151 
152 	rid = 0;
153 	sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
154 	    RF_ACTIVE);
155 	if (sc->mem_res == NULL)
156 		return (ENOMEM);
157 
158 	/* Validate that we can access the whole region. */
159 	if (rman_get_size(sc->mem_res) < HPET_MEM_WIDTH) {
160 		device_printf(dev, "memory region width %ld too small\n",
161 		    rman_get_size(sc->mem_res));
162 		bus_free_resource(dev, SYS_RES_MEMORY, sc->mem_res);
163 		return (ENXIO);
164 	}
165 
166 	/* Be sure timer is enabled. */
167 	bus_write_4(sc->mem_res, HPET_OFFSET_ENABLE, 1);
168 
169 	/* Read basic statistics about the timer. */
170 	val = bus_read_4(sc->mem_res, HPET_OFFSET_PERIOD);
171 	freq = (1000000000000000LL + val / 2) / val;
172 	if (bootverbose) {
173 		val = bus_read_4(sc->mem_res, HPET_OFFSET_INFO);
174 		device_printf(dev,
175 		    "vend: 0x%x rev: 0x%x num: %d hz: %jd opts:%s%s\n",
176 		    val >> 16, val & 0xff, (val >> 18) & 0xf, freq,
177 		    ((val >> 15) & 1) ? " leg_route" : "",
178 		    ((val >> 13) & 1) ? " count_size" : "");
179 	}
180 
181 	if (testenv("debug.acpi.hpet_test"))
182 		acpi_hpet_test(sc);
183 
184 	/*
185 	 * Don't attach if the timer never increments.  Since the spec
186 	 * requires it to be at least 10 MHz, it has to change in 1 us.
187 	 */
188 	val = bus_read_4(sc->mem_res, HPET_OFFSET_VALUE);
189 	DELAY(1);
190 	val2 = bus_read_4(sc->mem_res, HPET_OFFSET_VALUE);
191 	if (val == val2) {
192 		device_printf(dev, "HPET never increments, disabling\n");
193 		bus_write_4(sc->mem_res, HPET_OFFSET_ENABLE, 0);
194 		bus_free_resource(dev, SYS_RES_MEMORY, sc->mem_res);
195 		return (ENXIO);
196 	}
197 
198 	hpet_timecounter.tc_frequency = freq;
199 	hpet_timecounter.tc_priv = sc;
200 	tc_init(&hpet_timecounter);
201 
202 	return (0);
203 }
204 
205 static int
206 acpi_hpet_detach(device_t dev)
207 {
208 	ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);
209 
210 	/* XXX Without a tc_remove() function, we can't detach. */
211 	return (EBUSY);
212 }
213 
214 static int
215 acpi_hpet_resume(device_t dev)
216 {
217 	struct acpi_hpet_softc *sc;
218 
219 	/* Re-enable the timer after a resume to keep the clock advancing. */
220 	sc = device_get_softc(dev);
221 	bus_write_4(sc->mem_res, HPET_OFFSET_ENABLE, 1);
222 
223 	return (0);
224 }
225 
226 /* Print some basic latency/rate information to assist in debugging. */
227 static void
228 acpi_hpet_test(struct acpi_hpet_softc *sc)
229 {
230 	int i;
231 	uint32_t u1, u2;
232 	struct bintime b0, b1, b2;
233 	struct timespec ts;
234 
235 	binuptime(&b0);
236 	binuptime(&b0);
237 	binuptime(&b1);
238 	u1 = bus_read_4(sc->mem_res, HPET_OFFSET_VALUE);
239 	for (i = 1; i < 1000; i++)
240 		u2 = bus_read_4(sc->mem_res, HPET_OFFSET_VALUE);
241 	binuptime(&b2);
242 	u2 = bus_read_4(sc->mem_res, HPET_OFFSET_VALUE);
243 
244 	bintime_sub(&b2, &b1);
245 	bintime_sub(&b1, &b0);
246 	bintime_sub(&b2, &b1);
247 	bintime2timespec(&b2, &ts);
248 
249 	device_printf(sc->dev, "%ld.%09ld: %u ... %u = %u\n",
250 	    (long)ts.tv_sec, ts.tv_nsec, u1, u2, u2 - u1);
251 
252 	device_printf(sc->dev, "time per call: %ld ns\n", ts.tv_nsec / 1000);
253 }
254 
255 static device_method_t acpi_hpet_methods[] = {
256 	/* Device interface */
257 	DEVMETHOD(device_probe, acpi_hpet_probe),
258 	DEVMETHOD(device_attach, acpi_hpet_attach),
259 	DEVMETHOD(device_detach, acpi_hpet_detach),
260 	DEVMETHOD(device_resume, acpi_hpet_resume),
261 
262 	{0, 0}
263 };
264 
265 static driver_t	acpi_hpet_driver = {
266 	"acpi_hpet",
267 	acpi_hpet_methods,
268 	sizeof(struct acpi_hpet_softc),
269 };
270 
271 
272 DRIVER_MODULE(acpi_hpet, acpi, acpi_hpet_driver, acpi_hpet_devclass, 0, 0);
273 MODULE_DEPEND(acpi_hpet, acpi, 1, 1, 1);
274