1 /* $NetBSD: hpet_acpi.c,v 1.12 2021/01/29 15:49:55 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 2011 Jukka Ruohonen
5 * Copyright (c) 2006 Nicolas Joly
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: hpet_acpi.c,v 1.12 2021/01/29 15:49:55 thorpej Exp $");
33
34 #include <sys/param.h>
35 #include <sys/device.h>
36 #include <sys/time.h>
37 #include <sys/timetc.h>
38
39 #include <dev/acpi/acpivar.h>
40
41 #include <dev/ic/hpetreg.h>
42 #include <dev/ic/hpetvar.h>
43
44 #define _COMPONENT ACPI_RESOURCE_COMPONENT
45 ACPI_MODULE_NAME ("acpi_hpet")
46
47 static int hpet_acpi_tab_match(device_t, cfdata_t, void *);
48 static void hpet_acpi_tab_attach(device_t, device_t, void *);
49 static bus_addr_t hpet_acpi_tab_addr(void);
50 static int hpet_acpi_dev_match(device_t, cfdata_t, void *);
51 static void hpet_acpi_dev_attach(device_t, device_t, void *);
52 static bus_addr_t hpet_acpi_dev_addr(device_t, void *, bus_size_t *);
53 static int hpet_acpi_detach(device_t, int);
54
55 static const struct device_compatible_entry compat_data[] = {
56 { .compat = "PNP0103" },
57 DEVICE_COMPAT_EOL
58 };
59
60 CFATTACH_DECL_NEW(hpet_acpi_tab, sizeof(struct hpet_softc),
61 hpet_acpi_tab_match, hpet_acpi_tab_attach, hpet_acpi_detach, NULL);
62
63 CFATTACH_DECL_NEW(hpet_acpi_dev, sizeof(struct hpet_softc),
64 hpet_acpi_dev_match, hpet_acpi_dev_attach, hpet_acpi_detach, NULL);
65
66 static int
hpet_acpi_tab_match(device_t parent,cfdata_t match,void * aux)67 hpet_acpi_tab_match(device_t parent, cfdata_t match, void *aux)
68 {
69 struct acpi_attach_args *aa = aux;
70 bus_space_handle_t bh;
71 bus_space_tag_t bt;
72 bus_addr_t addr;
73
74 addr = hpet_acpi_tab_addr();
75
76 if (addr == 0)
77 return 0;
78
79 bt = aa->aa_memt;
80
81 if (bus_space_map(bt, addr, HPET_WINDOW_SIZE, 0, &bh) != 0)
82 return 0;
83
84 bus_space_unmap(bt, bh, HPET_WINDOW_SIZE);
85
86 return 1;
87 }
88
89 static void
hpet_acpi_tab_attach(device_t parent,device_t self,void * aux)90 hpet_acpi_tab_attach(device_t parent, device_t self, void *aux)
91 {
92 struct hpet_softc *sc = device_private(self);
93 struct acpi_attach_args *aa = aux;
94 bus_addr_t addr;
95
96 sc->sc_mapped = false;
97 addr = hpet_acpi_tab_addr();
98
99 if (addr == 0) {
100 aprint_error(": failed to get address\n");
101 return;
102 }
103
104 sc->sc_memt = aa->aa_memt;
105 sc->sc_mems = HPET_WINDOW_SIZE;
106
107 if (bus_space_map(sc->sc_memt, addr, sc->sc_mems, 0, &sc->sc_memh)) {
108 aprint_error(": failed to map mem space\n");
109 return;
110 }
111
112 aprint_naive("\n");
113 aprint_normal(": high precision event timer (mem 0x%08x-0x%08x)\n",
114 (uint32_t)addr, (uint32_t)addr + HPET_WINDOW_SIZE);
115
116 sc->sc_mapped = true;
117 hpet_attach_subr(self);
118 }
119
120 static bus_addr_t
hpet_acpi_tab_addr(void)121 hpet_acpi_tab_addr(void)
122 {
123 ACPI_TABLE_HPET *hpet;
124 ACPI_STATUS rv;
125
126 rv = AcpiGetTable(ACPI_SIG_HPET, 1, (ACPI_TABLE_HEADER **)&hpet);
127
128 if (ACPI_FAILURE(rv))
129 return 0;
130
131 if (hpet->Address.Address == 0)
132 return 0;
133
134 if (hpet->Address.SpaceId != ACPI_ADR_SPACE_SYSTEM_MEMORY)
135 return 0;
136
137 if (hpet->Address.Address == 0xfed0000000000000UL) /* A quirk. */
138 hpet->Address.Address >>= 32;
139
140 return hpet->Address.Address;
141 }
142
143 static int
hpet_acpi_dev_match(device_t parent,cfdata_t match,void * aux)144 hpet_acpi_dev_match(device_t parent, cfdata_t match, void *aux)
145 {
146 struct acpi_attach_args *aa = aux;
147 bus_space_handle_t bh;
148 bus_space_tag_t bt;
149 bus_size_t len = 0;
150 bus_addr_t addr;
151 int ret;
152
153 ret = acpi_compatible_match(aa, compat_data);
154 if (ret == 0)
155 return 0;
156
157 addr = hpet_acpi_dev_addr(parent, aa, &len);
158
159 if (addr == 0 || len == 0)
160 return 0;
161
162 bt = aa->aa_memt;
163
164 if (bus_space_map(bt, addr, len, 0, &bh) == 0) {
165 bus_space_unmap(bt, bh, len);
166 return ret;
167 }
168
169 return 0;
170 }
171
172 static void
hpet_acpi_dev_attach(device_t parent,device_t self,void * aux)173 hpet_acpi_dev_attach(device_t parent, device_t self, void *aux)
174 {
175 struct hpet_softc *sc = device_private(self);
176 struct acpi_attach_args *aa = aux;
177 bus_addr_t addr;
178
179 sc->sc_mapped = false;
180 addr = hpet_acpi_dev_addr(self, aa, &sc->sc_mems);
181
182 if (addr == 0) {
183 aprint_error(": failed to get address\n");
184 return;
185 }
186
187 sc->sc_memt = aa->aa_memt;
188
189 if (bus_space_map(sc->sc_memt, addr, sc->sc_mems, 0, &sc->sc_memh)) {
190 aprint_error(": failed to map mem space\n");
191 return;
192 }
193
194 aprint_naive("\n");
195 aprint_normal(": high precision event timer (mem 0x%08x-0x%08x)\n",
196 (uint32_t)addr, (uint32_t)(addr + sc->sc_mems));
197
198 sc->sc_mapped = true;
199 hpet_attach_subr(self);
200 }
201
202 static bus_addr_t
hpet_acpi_dev_addr(device_t self,void * aux,bus_size_t * len)203 hpet_acpi_dev_addr(device_t self, void *aux, bus_size_t *len)
204 {
205 struct acpi_attach_args *aa = aux;
206 struct acpi_resources res;
207 struct acpi_mem *mem;
208 bus_addr_t addr = 0;
209 ACPI_STATUS rv;
210
211 rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
212 &res, &acpi_resource_parse_ops_quiet);
213
214 if (ACPI_FAILURE(rv))
215 return 0;
216
217 mem = acpi_res_mem(&res, 0);
218
219 if (mem == NULL)
220 goto out;
221
222 if (mem->ar_length < HPET_WINDOW_SIZE)
223 goto out;
224
225 addr = mem->ar_base;
226 *len = mem->ar_length;
227
228 out:
229 acpi_resource_cleanup(&res);
230
231 return addr;
232 }
233
234 static int
hpet_acpi_detach(device_t self,int flags)235 hpet_acpi_detach(device_t self, int flags)
236 {
237 struct hpet_softc *sc = device_private(self);
238 int rv;
239
240 if (sc->sc_mapped != true)
241 return 0;
242
243 rv = hpet_detach(self, flags);
244
245 if (rv != 0)
246 return rv;
247
248 bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_mems);
249
250 return 0;
251 }
252