1 | /* $NetBSD: hpet_acpi.c,v 1.11 2011/06/15 09:02:38 jruoho 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.11 2011/06/15 09:02:38 jruoho 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 char * const hpet_acpi_ids[] = { |
56 | "PNP0103" , |
57 | NULL |
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 |
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 |
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 |
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 |
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 | |
152 | if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE) |
153 | return 0; |
154 | |
155 | if (acpi_match_hid(aa->aa_node->ad_devinfo, hpet_acpi_ids) == 0) |
156 | return 0; |
157 | |
158 | addr = hpet_acpi_dev_addr(parent, aa, &len); |
159 | |
160 | if (addr == 0 || len == 0) |
161 | return 0; |
162 | |
163 | bt = aa->aa_memt; |
164 | |
165 | if (bus_space_map(bt, addr, len, 0, &bh) == 0) { |
166 | bus_space_unmap(bt, bh, len); |
167 | return 1; |
168 | } |
169 | |
170 | return 0; |
171 | } |
172 | |
173 | static void |
174 | hpet_acpi_dev_attach(device_t parent, device_t self, void *aux) |
175 | { |
176 | struct hpet_softc *sc = device_private(self); |
177 | struct acpi_attach_args *aa = aux; |
178 | bus_addr_t addr; |
179 | |
180 | sc->sc_mapped = false; |
181 | addr = hpet_acpi_dev_addr(self, aa, &sc->sc_mems); |
182 | |
183 | if (addr == 0) { |
184 | aprint_error(": failed to get address\n" ); |
185 | return; |
186 | } |
187 | |
188 | sc->sc_memt = aa->aa_memt; |
189 | |
190 | if (bus_space_map(sc->sc_memt, addr, sc->sc_mems, 0, &sc->sc_memh)) { |
191 | aprint_error(": failed to map mem space\n" ); |
192 | return; |
193 | } |
194 | |
195 | aprint_naive("\n" ); |
196 | aprint_normal(": high precision event timer (mem 0x%08x-0x%08x)\n" , |
197 | (uint32_t)addr, (uint32_t)(addr + sc->sc_mems)); |
198 | |
199 | sc->sc_mapped = true; |
200 | hpet_attach_subr(self); |
201 | } |
202 | |
203 | static bus_addr_t |
204 | hpet_acpi_dev_addr(device_t self, void *aux, bus_size_t *len) |
205 | { |
206 | struct acpi_attach_args *aa = aux; |
207 | struct acpi_resources res; |
208 | struct acpi_mem *mem; |
209 | bus_addr_t addr = 0; |
210 | ACPI_STATUS rv; |
211 | |
212 | rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS" , |
213 | &res, &acpi_resource_parse_ops_quiet); |
214 | |
215 | if (ACPI_FAILURE(rv)) |
216 | return 0; |
217 | |
218 | mem = acpi_res_mem(&res, 0); |
219 | |
220 | if (mem == NULL) |
221 | goto out; |
222 | |
223 | if (mem->ar_length < HPET_WINDOW_SIZE) |
224 | goto out; |
225 | |
226 | addr = mem->ar_base; |
227 | *len = mem->ar_length; |
228 | |
229 | out: |
230 | acpi_resource_cleanup(&res); |
231 | |
232 | return addr; |
233 | } |
234 | |
235 | static int |
236 | hpet_acpi_detach(device_t self, int flags) |
237 | { |
238 | struct hpet_softc *sc = device_private(self); |
239 | int rv; |
240 | |
241 | if (sc->sc_mapped != true) |
242 | return 0; |
243 | |
244 | rv = hpet_detach(self, flags); |
245 | |
246 | if (rv != 0) |
247 | return rv; |
248 | |
249 | bus_space_unmap(sc->sc_memt, sc->sc_memh, sc->sc_mems); |
250 | |
251 | return 0; |
252 | } |
253 | |