acpi_ec.c revision 1.41.6.6 1 1.41.6.6 jmcneill /* $NetBSD: acpi_ec.c,v 1.41.6.6 2007/10/02 23:37:18 jmcneill Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.41.6.5 joerg * Copyright (c) 2007 Joerg Sonnenberger <joerg (at) NetBSD.org>.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * Redistribution and use in source and binary forms, with or without
8 1.1 thorpej * modification, are permitted provided that the following conditions
9 1.1 thorpej * are met:
10 1.41.6.5 joerg *
11 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
12 1.1 thorpej * notice, this list of conditions and the following disclaimer.
13 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
14 1.41.6.5 joerg * notice, this list of conditions and the following disclaimer in
15 1.41.6.5 joerg * the documentation and/or other materials provided with the
16 1.41.6.5 joerg * distribution.
17 1.41.6.5 joerg *
18 1.41.6.5 joerg * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 1.41.6.5 joerg * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 1.41.6.5 joerg * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 1.41.6.5 joerg * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 1.41.6.5 joerg * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 1.41.6.5 joerg * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 1.41.6.5 joerg * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 1.41.6.5 joerg * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 1.41.6.5 joerg * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 1.41.6.5 joerg * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28 1.41.6.5 joerg * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 thorpej * SUCH DAMAGE.
30 1.1 thorpej */
31 1.1 thorpej
32 1.3 lukem #include <sys/cdefs.h>
33 1.41.6.6 jmcneill __KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.41.6.6 2007/10/02 23:37:18 jmcneill Exp $");
34 1.1 thorpej
35 1.1 thorpej #include <sys/param.h>
36 1.1 thorpej #include <sys/systm.h>
37 1.41.6.5 joerg #include <sys/condvar.h>
38 1.1 thorpej #include <sys/device.h>
39 1.1 thorpej #include <sys/kernel.h>
40 1.41.6.5 joerg #include <sys/kthread.h>
41 1.41.6.5 joerg #include <sys/mutex.h>
42 1.1 thorpej
43 1.1 thorpej #include <machine/bus.h>
44 1.1 thorpej
45 1.1 thorpej #include <dev/acpi/acpivar.h>
46 1.1 thorpej
47 1.41.6.5 joerg #define EC_LOCK_TIMEOUT 5
48 1.23 kochi
49 1.41.6.5 joerg /* From ACPI 3.0b, chapter 12.3 */
50 1.41.6.5 joerg #define EC_COMMAND_READ 0x80
51 1.41.6.5 joerg #define EC_COMMAND_WRITE 0x81
52 1.41.6.5 joerg #define EC_COMMAND_BURST_EN 0x82
53 1.41.6.5 joerg #define EC_COMMAND_BURST_DIS 0x83
54 1.41.6.5 joerg #define EC_COMMAND_QUERY 0x84
55 1.41.6.5 joerg
56 1.41.6.5 joerg /* From ACPI 3.0b, chapter 12.2.1 */
57 1.41.6.5 joerg #define EC_STATUS_OBF 0x01
58 1.41.6.5 joerg #define EC_STATUS_IBF 0x02
59 1.41.6.5 joerg #define EC_STATUS_CMD 0x08
60 1.41.6.5 joerg #define EC_STATUS_BURST 0x10
61 1.41.6.5 joerg #define EC_STATUS_SCI 0x20
62 1.41.6.5 joerg #define EC_STATUS_SMI 0x40
63 1.1 thorpej
64 1.41.6.5 joerg static const char *ec_hid[] = {
65 1.41.6.5 joerg "PNP0C09",
66 1.41.6.5 joerg NULL,
67 1.41.6.5 joerg };
68 1.1 thorpej
69 1.41.6.5 joerg enum ec_state_t {
70 1.41.6.5 joerg EC_STATE_QUERY,
71 1.41.6.5 joerg EC_STATE_READ,
72 1.41.6.5 joerg EC_STATE_READ_VAL,
73 1.41.6.5 joerg EC_STATE_READ_WAIT,
74 1.41.6.5 joerg EC_STATE_WRITE,
75 1.41.6.5 joerg EC_STATE_WRITE_VAL,
76 1.41.6.5 joerg EC_STATE_WRITE_WAIT,
77 1.41.6.5 joerg EC_STATE_FREE
78 1.41.6.5 joerg };
79 1.1 thorpej
80 1.41.6.5 joerg struct acpiec_softc {
81 1.41.6.5 joerg ACPI_HANDLE sc_ech;
82 1.1 thorpej
83 1.41.6.5 joerg ACPI_HANDLE sc_gpeh;
84 1.41.6.5 joerg UINT8 sc_gpebit;
85 1.1 thorpej
86 1.41.6.5 joerg bus_space_tag_t sc_data_st;
87 1.41.6.5 joerg bus_space_handle_t sc_data_sh;
88 1.1 thorpej
89 1.41.6.5 joerg bus_space_tag_t sc_csr_st;
90 1.41.6.5 joerg bus_space_handle_t sc_csr_sh;
91 1.17 mycroft
92 1.41.6.5 joerg bool sc_need_global_lock;
93 1.41.6.5 joerg UINT32 sc_global_lock;
94 1.1 thorpej
95 1.41.6.5 joerg kmutex_t sc_mtx, sc_access_mtx;
96 1.41.6.5 joerg kcondvar_t sc_cv, sc_cv_sci;
97 1.41.6.5 joerg enum ec_state_t sc_state;
98 1.41.6.5 joerg bool sc_got_sci;
99 1.41.6.5 joerg
100 1.41.6.5 joerg uint8_t sc_cur_addr, sc_cur_val;
101 1.16 kochi };
102 1.16 kochi
103 1.41.6.5 joerg static int acpiecdt_match(device_t, struct cfdata *, void *);
104 1.41.6.5 joerg static void acpiecdt_attach(device_t, device_t, void *);
105 1.1 thorpej
106 1.41.6.5 joerg static int acpiec_match(device_t, struct cfdata *, void *);
107 1.41.6.5 joerg static void acpiec_attach(device_t, device_t, void *);
108 1.41.6.5 joerg
109 1.41.6.5 joerg static void acpiec_common_attach(device_t, device_t, ACPI_HANDLE,
110 1.41.6.5 joerg bus_addr_t, bus_addr_t, ACPI_HANDLE, uint8_t);
111 1.41.6.5 joerg
112 1.41.6.5 joerg static pnp_status_t acpiec_power(device_t, pnp_request_t, void *);
113 1.17 mycroft
114 1.41.6.5 joerg static bool acpiec_parse_gpe_package(device_t, ACPI_HANDLE,
115 1.41.6.5 joerg ACPI_HANDLE *, uint8_t *);
116 1.20 yamt
117 1.41.6.5 joerg static void acpiec_gpe_query(void *);
118 1.41.6.5 joerg static UINT32 acpiec_gpe_handler(void *);
119 1.41.6.5 joerg static ACPI_STATUS acpiec_space_setup(ACPI_HANDLE, UINT32, void *, void **);
120 1.41.6.5 joerg static ACPI_STATUS acpiec_space_handler(UINT32, ACPI_PHYSICAL_ADDRESS,
121 1.41.6.5 joerg UINT32, ACPI_INTEGER *, void *, void *);
122 1.20 yamt
123 1.41.6.5 joerg static void acpiec_gpe_state_maschine(device_t);
124 1.20 yamt
125 1.41.6.5 joerg CFATTACH_DECL_NEW(acpiec, sizeof(struct acpiec_softc),
126 1.20 yamt acpiec_match, acpiec_attach, NULL, NULL);
127 1.20 yamt
128 1.41.6.5 joerg CFATTACH_DECL_NEW(acpiecdt, sizeof(struct acpiec_softc),
129 1.41.6.5 joerg acpiecdt_match, acpiecdt_attach, NULL, NULL);
130 1.23 kochi
131 1.41.6.5 joerg static device_t ec_singleton = NULL;
132 1.41.6.5 joerg static bool acpiec_cold = false;
133 1.41.6.5 joerg
134 1.41.6.5 joerg static bool
135 1.41.6.5 joerg acpiecdt_find(device_t parent, ACPI_HANDLE *ec_handle,
136 1.41.6.5 joerg bus_addr_t *cmd_reg, bus_addr_t *data_reg, uint8_t *gpebit)
137 1.9 tshiozak {
138 1.41.6.6 jmcneill ACPI_TABLE_ECDT *ecdt;
139 1.41.6.5 joerg ACPI_STATUS rv;
140 1.41.6.5 joerg
141 1.41.6.6 jmcneill rv = AcpiGetTable(ACPI_SIG_ECDT, 1, (ACPI_TABLE_HEADER **)&ecdt);
142 1.41.6.6 jmcneill if (ACPI_FAILURE(rv))
143 1.41.6.5 joerg return false;
144 1.41.6.5 joerg
145 1.41.6.6 jmcneill if (ecdt->Control.BitWidth != 8 || ecdt->Data.BitWidth != 8) {
146 1.41.6.5 joerg aprint_error_dev(parent,
147 1.41.6.5 joerg "ECDT register width invalid (%d/%d)\n",
148 1.41.6.6 jmcneill ecdt->Control.BitWidth, ecdt->Data.BitWidth);
149 1.41.6.5 joerg return false;
150 1.41.6.5 joerg }
151 1.41.6.5 joerg
152 1.41.6.6 jmcneill rv = AcpiGetHandle(ACPI_ROOT_OBJECT, ecdt->Id, ec_handle);
153 1.41.6.6 jmcneill if (ACPI_FAILURE(rv)) {
154 1.41.6.5 joerg aprint_error_dev(parent,
155 1.41.6.5 joerg "failed to look up EC object %s: %s\n",
156 1.41.6.6 jmcneill ecdt->Id, AcpiFormatException(rv));
157 1.41.6.5 joerg return false;
158 1.41.6.5 joerg }
159 1.41.6.5 joerg
160 1.41.6.6 jmcneill *cmd_reg = ecdt->Control.Address;
161 1.41.6.6 jmcneill *data_reg = ecdt->Data.Address;
162 1.41.6.6 jmcneill *gpebit = ecdt->Gpe;
163 1.24 kochi
164 1.41.6.5 joerg return true;
165 1.9 tshiozak }
166 1.4 thorpej
167 1.41.6.5 joerg static int
168 1.41.6.5 joerg acpiecdt_match(device_t parent, struct cfdata *match, void *aux)
169 1.1 thorpej {
170 1.41.6.5 joerg ACPI_HANDLE ec_handle;
171 1.41.6.5 joerg bus_addr_t cmd_reg, data_reg;
172 1.41.6.5 joerg uint8_t gpebit;
173 1.20 yamt
174 1.41.6.5 joerg if (acpiecdt_find(parent, &ec_handle, &cmd_reg, &data_reg, &gpebit))
175 1.41.6.5 joerg return 1;
176 1.41.6.5 joerg else
177 1.41.6.5 joerg return 0;
178 1.1 thorpej }
179 1.1 thorpej
180 1.41.6.5 joerg static void
181 1.41.6.5 joerg acpiecdt_attach(device_t parent, device_t self, void *aux)
182 1.1 thorpej {
183 1.41.6.5 joerg ACPI_HANDLE ec_handle;
184 1.41.6.5 joerg bus_addr_t cmd_reg, data_reg;
185 1.41.6.5 joerg uint8_t gpebit;
186 1.20 yamt
187 1.41.6.5 joerg if (!acpiecdt_find(parent, &ec_handle, &cmd_reg, &data_reg, &gpebit))
188 1.41.6.5 joerg panic("ECDT disappeared");
189 1.41.6.5 joerg
190 1.41.6.5 joerg aprint_naive(": ACPI Embedded Controller via ECDT\n");
191 1.41.6.5 joerg aprint_normal(": ACPI Embedded Controller via ECDT\n");
192 1.41.6.5 joerg
193 1.41.6.5 joerg acpiec_common_attach(parent, self, ec_handle, cmd_reg, data_reg,
194 1.41.6.5 joerg NULL, gpebit);
195 1.1 thorpej }
196 1.1 thorpej
197 1.31 kochi static int
198 1.41.6.5 joerg acpiec_match(device_t parent, struct cfdata *match, void *aux)
199 1.1 thorpej {
200 1.1 thorpej struct acpi_attach_args *aa = aux;
201 1.1 thorpej
202 1.1 thorpej if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
203 1.25 kochi return 0;
204 1.1 thorpej
205 1.25 kochi return acpi_match_hid(aa->aa_node->ad_devinfo, ec_hid);
206 1.1 thorpej }
207 1.1 thorpej
208 1.41.6.5 joerg static void
209 1.41.6.5 joerg acpiec_attach(device_t parent, device_t self, void *aux)
210 1.17 mycroft {
211 1.41.6.5 joerg struct acpi_attach_args *aa = aux;
212 1.41.6.5 joerg struct acpi_resources ec_res;
213 1.41.6.5 joerg struct acpi_io *io0, *io1;
214 1.41.6.5 joerg ACPI_HANDLE gpe_handle;
215 1.41.6.5 joerg uint8_t gpebit;
216 1.23 kochi ACPI_STATUS rv;
217 1.17 mycroft
218 1.41.6.5 joerg if (ec_singleton != NULL) {
219 1.41.6.5 joerg aprint_naive(": ACPI Embedded Controller (disabled)\n");
220 1.41.6.5 joerg aprint_normal(": ACPI Embedded Controller (disabled)\n");
221 1.41.6.5 joerg pnp_register(self, pnp_generic_power);
222 1.23 kochi return;
223 1.23 kochi }
224 1.23 kochi
225 1.41.6.5 joerg aprint_naive(": ACPI Embedded Controller\n");
226 1.41.6.5 joerg aprint_normal(": ACPI Embedded Controller\n");
227 1.23 kochi
228 1.41.6.5 joerg if (!acpiec_parse_gpe_package(self, aa->aa_node->ad_handle,
229 1.41.6.5 joerg &gpe_handle, &gpebit))
230 1.41.6.5 joerg return;
231 1.23 kochi
232 1.41.6.5 joerg rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
233 1.41.6.5 joerg &ec_res, &acpi_resource_parse_ops_default);
234 1.41.6.5 joerg if (rv != AE_OK) {
235 1.41.6.5 joerg aprint_error_dev(self, "resource parsing failed: %s\n",
236 1.23 kochi AcpiFormatException(rv));
237 1.41.6.5 joerg return;
238 1.17 mycroft }
239 1.23 kochi
240 1.41.6.5 joerg if ((io0 = acpi_res_io(&ec_res, 0)) == NULL) {
241 1.41.6.5 joerg aprint_error_dev(self, "no data register resource\n");
242 1.41.6.5 joerg goto free_res;
243 1.41.6.5 joerg }
244 1.41.6.5 joerg if ((io1 = acpi_res_io(&ec_res, 1)) == NULL) {
245 1.41.6.5 joerg aprint_error_dev(self, "no CSR register resource\n");
246 1.41.6.5 joerg goto free_res;
247 1.41.6.5 joerg }
248 1.23 kochi
249 1.41.6.5 joerg acpiec_common_attach(parent, self, aa->aa_node->ad_handle,
250 1.41.6.5 joerg io1->ar_base, io0->ar_base, gpe_handle, gpebit);
251 1.23 kochi
252 1.41.6.5 joerg free_res:
253 1.41.6.5 joerg acpi_resource_cleanup(&ec_res);
254 1.17 mycroft }
255 1.17 mycroft
256 1.31 kochi static void
257 1.41.6.5 joerg acpiec_common_attach(device_t parent, device_t self,
258 1.41.6.5 joerg ACPI_HANDLE ec_handle, bus_addr_t cmd_reg, bus_addr_t data_reg,
259 1.41.6.5 joerg ACPI_HANDLE gpe_handle, uint8_t gpebit)
260 1.1 thorpej {
261 1.41.6.5 joerg struct acpiec_softc *sc = device_private(self);
262 1.1 thorpej ACPI_STATUS rv;
263 1.41.6.5 joerg ACPI_INTEGER val;
264 1.1 thorpej
265 1.41.6.5 joerg sc->sc_ech = ec_handle;
266 1.41.6.5 joerg sc->sc_gpeh = gpe_handle;
267 1.41.6.5 joerg sc->sc_gpebit = gpebit;
268 1.41.6.5 joerg
269 1.41.6.5 joerg sc->sc_state = EC_STATE_FREE;
270 1.41.6.5 joerg mutex_init(&sc->sc_mtx, MUTEX_DRIVER, IPL_TTY);
271 1.41.6.5 joerg mutex_init(&sc->sc_access_mtx, MUTEX_DEFAULT, IPL_NONE);
272 1.41.6.5 joerg cv_init(&sc->sc_cv, "eccv");
273 1.41.6.5 joerg cv_init(&sc->sc_cv_sci, "ecsci");
274 1.41.6.5 joerg
275 1.41.6.5 joerg if (bus_space_map(sc->sc_data_st, data_reg, 1, 0,
276 1.41.6.5 joerg &sc->sc_data_sh) != 0) {
277 1.41.6.5 joerg aprint_error_dev(self, "unable to map data register\n");
278 1.41.6.5 joerg return;
279 1.41.6.5 joerg }
280 1.1 thorpej
281 1.41.6.5 joerg if (bus_space_map(sc->sc_csr_st, cmd_reg, 1, 0, &sc->sc_csr_sh) != 0) {
282 1.41.6.5 joerg aprint_error_dev(self, "unable to map CSR register\n");
283 1.41.6.5 joerg goto post_data_map;
284 1.41.6.5 joerg }
285 1.17 mycroft
286 1.41.6.5 joerg rv = acpi_eval_integer(sc->sc_ech, "_GLK", &val);
287 1.41.6.5 joerg if (rv == AE_OK) {
288 1.41.6.5 joerg sc->sc_need_global_lock = val != 0;
289 1.41.6.5 joerg } else if (rv != AE_NOT_FOUND) {
290 1.41.6.5 joerg aprint_error_dev(self, "unable to evaluate _GLK: %s\n",
291 1.41.6.5 joerg AcpiFormatException(rv));
292 1.41.6.5 joerg goto post_csr_map;
293 1.41.6.5 joerg } else {
294 1.41.6.5 joerg sc->sc_need_global_lock = false;
295 1.41.6.5 joerg }
296 1.41.6.5 joerg if (sc->sc_need_global_lock)
297 1.41.6.5 joerg aprint_normal_dev(self, "using global ACPI lock\n");
298 1.1 thorpej
299 1.41.6.5 joerg rv = AcpiInstallAddressSpaceHandler(sc->sc_ech, ACPI_ADR_SPACE_EC,
300 1.41.6.5 joerg acpiec_space_handler, acpiec_space_setup, self);
301 1.41.6.5 joerg if (rv != AE_OK) {
302 1.41.6.5 joerg aprint_error_dev(self,
303 1.41.6.5 joerg "unable to install address space handler: %s\n",
304 1.41.6.5 joerg AcpiFormatException(rv));
305 1.41.6.5 joerg goto post_csr_map;
306 1.41.6.5 joerg }
307 1.1 thorpej
308 1.41.6.5 joerg rv = AcpiInstallGpeHandler(sc->sc_gpeh, sc->sc_gpebit,
309 1.41.6.5 joerg ACPI_GPE_EDGE_TRIGGERED, acpiec_gpe_handler, self);
310 1.41.6.5 joerg if (rv != AE_OK) {
311 1.41.6.5 joerg aprint_error_dev(self, "unable to install GPE handler: %s\n",
312 1.41.6.5 joerg AcpiFormatException(rv));
313 1.41.6.5 joerg goto post_csr_map;
314 1.41.6.5 joerg }
315 1.23 kochi
316 1.41.6.5 joerg rv = AcpiSetGpeType(sc->sc_gpeh, sc->sc_gpebit, ACPI_GPE_TYPE_RUNTIME);
317 1.41.6.5 joerg if (rv != AE_OK) {
318 1.41.6.5 joerg aprint_error_dev(self, "unable to set GPE type: %s\n",
319 1.41.6.5 joerg AcpiFormatException(rv));
320 1.41.6.5 joerg goto post_csr_map;
321 1.1 thorpej }
322 1.1 thorpej
323 1.41.6.5 joerg rv = AcpiEnableGpe(sc->sc_gpeh, sc->sc_gpebit, ACPI_ISR);
324 1.41.6.5 joerg if (rv != AE_OK) {
325 1.41.6.5 joerg aprint_error_dev(self, "unable to enable GPE: %s\n",
326 1.41.6.5 joerg AcpiFormatException(rv));
327 1.41.6.5 joerg goto post_csr_map;
328 1.41.6.5 joerg }
329 1.23 kochi
330 1.41.6.5 joerg if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL, acpiec_gpe_query,
331 1.41.6.5 joerg self, NULL, "acpiec sci thread")) {
332 1.41.6.5 joerg aprint_error_dev(self, "unable to create query kthread\n");
333 1.41.6.5 joerg goto post_csr_map;
334 1.17 mycroft }
335 1.17 mycroft
336 1.41.6.5 joerg ec_singleton = self;
337 1.1 thorpej
338 1.41.6.5 joerg pnp_register(self, acpiec_power);
339 1.1 thorpej
340 1.41.6.5 joerg return;
341 1.1 thorpej
342 1.41.6.5 joerg post_csr_map:
343 1.41.6.5 joerg (void)AcpiRemoveGpeHandler(sc->sc_gpeh, sc->sc_gpebit,
344 1.41.6.5 joerg acpiec_gpe_handler);
345 1.41.6.5 joerg (void)AcpiRemoveAddressSpaceHandler(sc->sc_ech,
346 1.41.6.5 joerg ACPI_ADR_SPACE_EC, acpiec_space_handler);
347 1.41.6.5 joerg bus_space_unmap(sc->sc_csr_st, sc->sc_csr_sh, 1);
348 1.41.6.5 joerg post_data_map:
349 1.41.6.5 joerg bus_space_unmap(sc->sc_data_st, sc->sc_data_sh, 1);
350 1.41.6.5 joerg }
351 1.41.6.5 joerg
352 1.41.6.5 joerg static pnp_status_t
353 1.41.6.5 joerg acpiec_power(device_t dv, pnp_request_t req, void *opaque)
354 1.41.6.5 joerg {
355 1.41.6.5 joerg pnp_capabilities_t *pcaps;
356 1.41.6.5 joerg pnp_state_t *pstate;
357 1.41.6.5 joerg
358 1.41.6.5 joerg switch (req) {
359 1.41.6.5 joerg case PNP_REQUEST_GET_CAPABILITIES:
360 1.41.6.5 joerg pcaps = opaque;
361 1.41.6.5 joerg pcaps->state = PNP_STATE_D0 | PNP_STATE_D3;
362 1.41.6.5 joerg break;
363 1.41.6.5 joerg case PNP_REQUEST_GET_STATE:
364 1.41.6.5 joerg pstate = opaque;
365 1.41.6.5 joerg if (acpiec_cold)
366 1.41.6.5 joerg *pstate = PNP_STATE_D0;
367 1.41.6.5 joerg else
368 1.41.6.5 joerg *pstate = PNP_STATE_D3;
369 1.41.6.5 joerg break;
370 1.41.6.5 joerg case PNP_REQUEST_SET_STATE:
371 1.41.6.5 joerg pstate = opaque;
372 1.41.6.5 joerg switch (*pstate) {
373 1.41.6.5 joerg case PNP_STATE_D0:
374 1.41.6.5 joerg acpiec_cold = false;
375 1.1 thorpej break;
376 1.41.6.5 joerg case PNP_STATE_D3:
377 1.41.6.5 joerg acpiec_cold = true;
378 1.1 thorpej break;
379 1.41.6.5 joerg default:
380 1.41.6.5 joerg return PNP_STATUS_UNSUPPORTED;
381 1.1 thorpej }
382 1.41.6.5 joerg break;
383 1.41.6.5 joerg default:
384 1.41.6.5 joerg return PNP_STATUS_UNSUPPORTED;
385 1.1 thorpej }
386 1.1 thorpej
387 1.41.6.5 joerg return PNP_STATUS_SUCCESS;
388 1.1 thorpej }
389 1.1 thorpej
390 1.41.6.5 joerg static bool
391 1.41.6.5 joerg acpiec_parse_gpe_package(device_t self, ACPI_HANDLE ec_handle,
392 1.41.6.5 joerg ACPI_HANDLE *gpe_handle, uint8_t *gpebit)
393 1.1 thorpej {
394 1.41.6.5 joerg ACPI_BUFFER buf;
395 1.41.6.5 joerg ACPI_OBJECT *p, *c;
396 1.25 kochi ACPI_STATUS rv;
397 1.1 thorpej
398 1.41.6.5 joerg rv = acpi_eval_struct(ec_handle, "_GPE", &buf);
399 1.41.6.5 joerg if (rv != AE_OK) {
400 1.41.6.5 joerg aprint_error_dev(self, "unable to evaluate _GPE: %s\n",
401 1.41.6.5 joerg AcpiFormatException(rv));
402 1.41.6.5 joerg return false;
403 1.41.6.5 joerg }
404 1.41.6.5 joerg
405 1.41.6.5 joerg p = buf.Pointer;
406 1.41.6.5 joerg
407 1.41.6.5 joerg if (p->Type == ACPI_TYPE_INTEGER) {
408 1.41.6.5 joerg *gpe_handle = NULL;
409 1.41.6.5 joerg *gpebit = p->Integer.Value;
410 1.41.6.5 joerg AcpiOsFree(p);
411 1.41.6.5 joerg return true;
412 1.41.6.5 joerg }
413 1.41.6.5 joerg
414 1.41.6.5 joerg if (p->Type != ACPI_TYPE_PACKAGE) {
415 1.41.6.5 joerg aprint_error_dev(self, "_GPE is neither integer nor package\n");
416 1.41.6.5 joerg AcpiOsFree(p);
417 1.41.6.5 joerg return false;
418 1.41.6.5 joerg }
419 1.41.6.5 joerg
420 1.41.6.5 joerg if (p->Package.Count != 2) {
421 1.41.6.5 joerg aprint_error_dev(self, "_GPE package does not contain 2 elements\n");
422 1.41.6.5 joerg AcpiOsFree(p);
423 1.41.6.5 joerg return false;
424 1.1 thorpej }
425 1.33 kochi
426 1.41.6.5 joerg c = &p->Package.Elements[0];
427 1.41.6.5 joerg switch (c->Type) {
428 1.41.6.5 joerg case ACPI_TYPE_LOCAL_REFERENCE:
429 1.41.6.5 joerg case ACPI_TYPE_ANY:
430 1.41.6.5 joerg *gpe_handle = c->Reference.Handle;
431 1.41.6.5 joerg break;
432 1.41.6.5 joerg case ACPI_TYPE_STRING:
433 1.41.6.5 joerg /* XXX should be using real scope here */
434 1.41.6.5 joerg rv = AcpiGetHandle(NULL, p->String.Pointer, gpe_handle);
435 1.41.6.5 joerg if (rv != AE_OK) {
436 1.41.6.5 joerg aprint_error_dev(self,
437 1.41.6.5 joerg "_GPE device reference unresolvable\n");
438 1.41.6.5 joerg AcpiOsFree(p);
439 1.41.6.5 joerg return false;
440 1.41.6.5 joerg }
441 1.41.6.5 joerg break;
442 1.41.6.5 joerg default:
443 1.41.6.5 joerg aprint_error_dev(self, "_GPE device reference incorrect\n");
444 1.41.6.5 joerg AcpiOsFree(p);
445 1.41.6.5 joerg return false;
446 1.41.6.5 joerg }
447 1.41.6.5 joerg c = &p->Package.Elements[1];
448 1.41.6.5 joerg if (c->Type != ACPI_TYPE_INTEGER) {
449 1.41.6.5 joerg aprint_error_dev(self,
450 1.41.6.5 joerg "_GPE package needs integer as 2nd field\n");
451 1.41.6.5 joerg AcpiOsFree(p);
452 1.41.6.5 joerg return false;
453 1.41.6.5 joerg }
454 1.41.6.5 joerg *gpebit = c->Integer.Value;
455 1.41.6.5 joerg AcpiOsFree(p);
456 1.41.6.5 joerg return true;
457 1.1 thorpej }
458 1.1 thorpej
459 1.41.6.5 joerg static uint8_t
460 1.41.6.5 joerg acpiec_read_data(struct acpiec_softc *sc)
461 1.1 thorpej {
462 1.41.6.5 joerg return bus_space_read_1(sc->sc_data_st, sc->sc_data_sh, 0);
463 1.41.6.5 joerg }
464 1.1 thorpej
465 1.41.6.5 joerg static void
466 1.41.6.5 joerg acpiec_write_data(struct acpiec_softc *sc, uint8_t val)
467 1.41.6.5 joerg {
468 1.41.6.5 joerg bus_space_write_1(sc->sc_data_st, sc->sc_data_sh, 0, val);
469 1.41.6.5 joerg }
470 1.1 thorpej
471 1.41.6.5 joerg static uint8_t
472 1.41.6.5 joerg acpiec_read_status(struct acpiec_softc *sc)
473 1.41.6.5 joerg {
474 1.41.6.5 joerg return bus_space_read_1(sc->sc_csr_st, sc->sc_csr_sh, 0);
475 1.41.6.5 joerg }
476 1.1 thorpej
477 1.41.6.5 joerg static void
478 1.41.6.5 joerg acpiec_write_command(struct acpiec_softc *sc, uint8_t cmd)
479 1.41.6.5 joerg {
480 1.41.6.5 joerg bus_space_write_1(sc->sc_csr_st, sc->sc_csr_sh, 0, cmd);
481 1.1 thorpej }
482 1.1 thorpej
483 1.1 thorpej static ACPI_STATUS
484 1.41.6.5 joerg acpiec_space_setup(ACPI_HANDLE region, UINT32 func, void *arg,
485 1.41.6.5 joerg void **region_arg)
486 1.1 thorpej {
487 1.41.6.5 joerg if (func == ACPI_REGION_DEACTIVATE)
488 1.41.6.5 joerg *region_arg = NULL;
489 1.41.6.5 joerg else
490 1.41.6.5 joerg *region_arg = arg;
491 1.1 thorpej
492 1.41.6.5 joerg return AE_OK;
493 1.41.6.5 joerg }
494 1.1 thorpej
495 1.41.6.5 joerg static void
496 1.41.6.5 joerg acpiec_lock(device_t dv)
497 1.41.6.5 joerg {
498 1.41.6.5 joerg struct acpiec_softc *sc = device_private(dv);
499 1.41.6.5 joerg ACPI_STATUS rv;
500 1.1 thorpej
501 1.41.6.5 joerg mutex_enter(&sc->sc_access_mtx);
502 1.1 thorpej
503 1.41.6.5 joerg if (sc->sc_need_global_lock) {
504 1.41.6.5 joerg rv = AcpiAcquireGlobalLock(EC_LOCK_TIMEOUT, &sc->sc_global_lock);
505 1.41.6.5 joerg if (rv != AE_OK) {
506 1.41.6.5 joerg aprint_error("%s: failed to acquire global lock (continuing)\n",
507 1.41.6.5 joerg device_xname(dv));
508 1.41.6.5 joerg return;
509 1.41.6.5 joerg }
510 1.41.6.5 joerg }
511 1.41.6.5 joerg }
512 1.1 thorpej
513 1.41.6.5 joerg static void
514 1.41.6.5 joerg acpiec_unlock(device_t dv)
515 1.41.6.5 joerg {
516 1.41.6.5 joerg struct acpiec_softc *sc = device_private(dv);
517 1.41.6.5 joerg ACPI_STATUS rv;
518 1.41.6.5 joerg
519 1.41.6.5 joerg if (sc->sc_need_global_lock) {
520 1.41.6.5 joerg rv = AcpiReleaseGlobalLock(sc->sc_global_lock);
521 1.41.6.5 joerg if (rv != AE_OK) {
522 1.41.6.5 joerg aprint_error("%s: failed to release global lock (continuing)\n",
523 1.41.6.5 joerg device_xname(dv));
524 1.41.6.5 joerg }
525 1.1 thorpej }
526 1.41.6.5 joerg mutex_exit(&sc->sc_access_mtx);
527 1.41.6.5 joerg }
528 1.1 thorpej
529 1.41.6.5 joerg static ACPI_STATUS
530 1.41.6.5 joerg acpiec_read(device_t dv, uint8_t addr, uint8_t *val)
531 1.41.6.5 joerg {
532 1.41.6.5 joerg struct acpiec_softc *sc = device_private(dv);
533 1.41.6.5 joerg int timeouts = 0;
534 1.1 thorpej
535 1.41.6.5 joerg acpiec_lock(dv);
536 1.41.6.5 joerg mutex_enter(&sc->sc_mtx);
537 1.1 thorpej
538 1.41.6.5 joerg retry:
539 1.41.6.5 joerg sc->sc_cur_addr = addr;
540 1.41.6.5 joerg sc->sc_state = EC_STATE_READ;
541 1.41.6.5 joerg
542 1.41.6.5 joerg acpiec_write_command(sc, EC_COMMAND_READ);
543 1.41.6.5 joerg if (cold || acpiec_cold) {
544 1.41.6.5 joerg for (delay(1); sc->sc_state != EC_STATE_READ_WAIT; delay(1))
545 1.41.6.5 joerg acpiec_gpe_state_maschine(dv);
546 1.41.6.5 joerg } else while (cv_timedwait(&sc->sc_cv, &sc->sc_mtx, hz)) {
547 1.41.6.5 joerg mutex_exit(&sc->sc_mtx);
548 1.41.6.5 joerg AcpiClearGpe(sc->sc_gpeh, sc->sc_gpebit, ACPI_NOT_ISR);
549 1.41.6.5 joerg mutex_enter(&sc->sc_mtx);
550 1.41.6.5 joerg if (++timeouts < 5)
551 1.41.6.5 joerg goto retry;
552 1.41.6.5 joerg mutex_exit(&sc->sc_mtx);
553 1.41.6.5 joerg acpiec_unlock(dv);
554 1.41.6.5 joerg aprint_error_dev(dv, "command takes over 5sec...\n");
555 1.41.6.5 joerg return AE_ERROR;
556 1.41.6.5 joerg }
557 1.41.6.5 joerg *val = acpiec_read_data(sc);
558 1.1 thorpej
559 1.41.6.5 joerg sc->sc_state = EC_STATE_FREE;
560 1.41.6.5 joerg
561 1.41.6.5 joerg if (sc->sc_got_sci)
562 1.41.6.5 joerg cv_signal(&sc->sc_cv_sci);
563 1.41.6.5 joerg
564 1.41.6.5 joerg
565 1.41.6.5 joerg mutex_exit(&sc->sc_mtx);
566 1.41.6.5 joerg acpiec_unlock(dv);
567 1.41.6.5 joerg return AE_OK;
568 1.1 thorpej }
569 1.1 thorpej
570 1.1 thorpej static ACPI_STATUS
571 1.41.6.5 joerg acpiec_write(device_t dv, uint8_t addr, uint8_t val)
572 1.1 thorpej {
573 1.41.6.5 joerg struct acpiec_softc *sc = device_private(dv);
574 1.41.6.5 joerg int timeouts = 0;
575 1.1 thorpej
576 1.41.6.5 joerg acpiec_lock(dv);
577 1.41.6.5 joerg mutex_enter(&sc->sc_mtx);
578 1.1 thorpej
579 1.41.6.5 joerg retry:
580 1.41.6.5 joerg sc->sc_cur_addr = addr;
581 1.41.6.5 joerg sc->sc_cur_val = val;
582 1.41.6.5 joerg sc->sc_state = EC_STATE_WRITE;
583 1.41.6.5 joerg
584 1.41.6.5 joerg acpiec_write_command(sc, EC_COMMAND_WRITE);
585 1.41.6.5 joerg if (cold || acpiec_cold) {
586 1.41.6.5 joerg for (delay(1); sc->sc_state != EC_STATE_WRITE_WAIT; delay(1))
587 1.41.6.5 joerg acpiec_gpe_state_maschine(dv);
588 1.41.6.5 joerg } else while (cv_timedwait(&sc->sc_cv, &sc->sc_mtx, hz)) {
589 1.41.6.5 joerg mutex_exit(&sc->sc_mtx);
590 1.41.6.5 joerg AcpiClearGpe(sc->sc_gpeh, sc->sc_gpebit, ACPI_NOT_ISR);
591 1.41.6.5 joerg mutex_enter(&sc->sc_mtx);
592 1.41.6.5 joerg if (++timeouts < 5)
593 1.41.6.5 joerg goto retry;
594 1.41.6.5 joerg mutex_exit(&sc->sc_mtx);
595 1.41.6.5 joerg acpiec_unlock(dv);
596 1.41.6.5 joerg aprint_error_dev(dv, "command takes over 5sec...\n");
597 1.41.6.5 joerg return AE_ERROR;
598 1.41.6.4 joerg }
599 1.41.6.4 joerg
600 1.41.6.5 joerg sc->sc_state = EC_STATE_FREE;
601 1.1 thorpej
602 1.41.6.5 joerg if (sc->sc_got_sci)
603 1.41.6.5 joerg cv_signal(&sc->sc_cv_sci);
604 1.41.6.5 joerg
605 1.41.6.5 joerg mutex_exit(&sc->sc_mtx);
606 1.41.6.5 joerg acpiec_unlock(dv);
607 1.41.6.5 joerg return AE_OK;
608 1.1 thorpej }
609 1.1 thorpej
610 1.1 thorpej static ACPI_STATUS
611 1.41.6.5 joerg acpiec_space_handler(UINT32 func, ACPI_PHYSICAL_ADDRESS paddr,
612 1.41.6.5 joerg UINT32 width, ACPI_INTEGER *value, void *arg, void *region_arg)
613 1.1 thorpej {
614 1.41.6.5 joerg device_t dv;
615 1.41.6.5 joerg struct acpiec_softc *sc;
616 1.25 kochi ACPI_STATUS rv;
617 1.41.6.5 joerg uint8_t addr, reg;
618 1.41.6.5 joerg unsigned int i;
619 1.41.6.5 joerg
620 1.41.6.5 joerg if (paddr > 0xff || width % 8 != 0 || value == NULL || arg == NULL ||
621 1.41.6.5 joerg paddr + width / 8 > 0xff)
622 1.41.6.5 joerg return AE_BAD_PARAMETER;
623 1.1 thorpej
624 1.41.6.5 joerg addr = paddr;
625 1.41.6.5 joerg dv = arg;
626 1.41.6.5 joerg sc = device_private(dv);
627 1.41.6.5 joerg
628 1.41.6.5 joerg rv = AE_OK;
629 1.41.6.5 joerg
630 1.41.6.5 joerg switch (func) {
631 1.41.6.5 joerg case ACPI_READ:
632 1.41.6.5 joerg for (i = 0; i < width; i += 8, ++addr) {
633 1.41.6.5 joerg rv = acpiec_read(dv, addr, ®);
634 1.41.6.5 joerg if (rv != AE_OK)
635 1.41.6.5 joerg break;
636 1.41.6.5 joerg *value |= (ACPI_INTEGER)reg << i;
637 1.41.6.5 joerg }
638 1.41.6.5 joerg break;
639 1.41.6.5 joerg case ACPI_WRITE:
640 1.41.6.5 joerg for (i = 0; i < width; i += 8, ++addr) {
641 1.41.6.5 joerg reg = (*value >>i) & 0xff;
642 1.41.6.5 joerg rv = acpiec_write(dv, addr, reg);
643 1.41.6.5 joerg if (rv != AE_OK)
644 1.41.6.5 joerg break;
645 1.41.6.5 joerg }
646 1.41.6.5 joerg break;
647 1.41.6.5 joerg default:
648 1.41.6.5 joerg aprint_error("%s: invalid Address Space function called: %x\n",
649 1.41.6.5 joerg device_xname(dv), (unsigned int)func);
650 1.41.6.5 joerg return AE_BAD_PARAMETER;
651 1.41.6.5 joerg }
652 1.1 thorpej
653 1.25 kochi return rv;
654 1.24 kochi }
655 1.1 thorpej
656 1.41.6.5 joerg static void
657 1.41.6.5 joerg acpiec_gpe_query(void *arg)
658 1.1 thorpej {
659 1.41.6.5 joerg device_t dv = arg;
660 1.41.6.5 joerg struct acpiec_softc *sc = device_private(dv);
661 1.41.6.5 joerg uint8_t reg;
662 1.41.6.5 joerg char qxx[5];
663 1.25 kochi ACPI_STATUS rv;
664 1.1 thorpej
665 1.41.6.5 joerg loop:
666 1.41.6.5 joerg mutex_enter(&sc->sc_mtx);
667 1.1 thorpej
668 1.41.6.5 joerg if (sc->sc_got_sci == false)
669 1.41.6.5 joerg cv_wait(&sc->sc_cv_sci, &sc->sc_mtx);
670 1.41.6.5 joerg mutex_exit(&sc->sc_mtx);
671 1.1 thorpej
672 1.41.6.5 joerg acpiec_lock(dv);
673 1.41.6.5 joerg mutex_enter(&sc->sc_mtx);
674 1.1 thorpej
675 1.41.6.5 joerg acpiec_write_command(sc, EC_COMMAND_QUERY);
676 1.41.6.5 joerg sc->sc_state = EC_STATE_QUERY;
677 1.1 thorpej
678 1.41.6.5 joerg cv_wait(&sc->sc_cv, &sc->sc_mtx);
679 1.1 thorpej
680 1.41.6.5 joerg reg = acpiec_read_data(sc);
681 1.41.6.5 joerg sc->sc_state = EC_STATE_FREE;
682 1.41.6.5 joerg sc->sc_got_sci = false;
683 1.1 thorpej
684 1.41.6.5 joerg mutex_exit(&sc->sc_mtx);
685 1.41.6.5 joerg acpiec_unlock(dv);
686 1.41.6.5 joerg
687 1.41.6.5 joerg if (reg == 0)
688 1.41.6.5 joerg goto loop; /* Spurious query result */
689 1.41.6.5 joerg /*
690 1.41.6.5 joerg * Evaluate _Qxx to respond to the controller.
691 1.41.6.5 joerg */
692 1.41.6.5 joerg snprintf(qxx, sizeof(qxx), "_Q%02X", (unsigned int)reg);
693 1.41.6.5 joerg rv = AcpiEvaluateObject(sc->sc_ech, qxx, NULL, NULL);
694 1.41.6.5 joerg if (rv != AE_OK && rv != AE_NOT_FOUND) {
695 1.41.6.5 joerg aprint_error("%s: GPE query method %s failed: %s",
696 1.41.6.5 joerg device_xname(dv), qxx, AcpiFormatException(rv));
697 1.41.6.5 joerg }
698 1.41.6.5 joerg
699 1.41.6.5 joerg goto loop;
700 1.24 kochi }
701 1.1 thorpej
702 1.41.6.5 joerg static void
703 1.41.6.5 joerg acpiec_gpe_state_maschine(device_t dv)
704 1.1 thorpej {
705 1.41.6.5 joerg struct acpiec_softc *sc = device_private(dv);
706 1.41.6.5 joerg uint8_t reg;
707 1.1 thorpej
708 1.41.6.5 joerg reg = acpiec_read_status(sc);
709 1.1 thorpej
710 1.41.6.5 joerg if (reg & EC_STATUS_SCI)
711 1.41.6.5 joerg sc->sc_got_sci = true;
712 1.1 thorpej
713 1.41.6.5 joerg switch (sc->sc_state) {
714 1.41.6.5 joerg case EC_STATE_QUERY:
715 1.41.6.5 joerg if ((reg & EC_STATUS_OBF) == 0)
716 1.41.6.5 joerg break; /* Nothing of interest here. */
717 1.1 thorpej
718 1.41.6.5 joerg cv_signal(&sc->sc_cv);
719 1.41.6.5 joerg break;
720 1.1 thorpej
721 1.41.6.5 joerg case EC_STATE_READ:
722 1.41.6.5 joerg if ((reg & EC_STATUS_IBF) != 0)
723 1.41.6.5 joerg break; /* Nothing of interest here. */
724 1.1 thorpej
725 1.41.6.5 joerg acpiec_write_data(sc, sc->sc_cur_addr);
726 1.41.6.5 joerg sc->sc_state = EC_STATE_READ_VAL;
727 1.41.6.5 joerg break;
728 1.1 thorpej
729 1.41.6.5 joerg case EC_STATE_READ_VAL:
730 1.41.6.5 joerg if ((reg & EC_STATUS_OBF) == 0)
731 1.41.6.5 joerg break; /* Nothing of interest here. */
732 1.41.6.5 joerg cv_signal(&sc->sc_cv);
733 1.41.6.5 joerg sc->sc_state = EC_STATE_READ_WAIT;
734 1.41.6.5 joerg break;
735 1.41.6.3 joerg
736 1.41.6.5 joerg case EC_STATE_READ_WAIT:
737 1.41.6.5 joerg break; /* Nothing of interest here. */
738 1.41.6.3 joerg
739 1.41.6.5 joerg case EC_STATE_WRITE:
740 1.41.6.5 joerg if ((reg & EC_STATUS_IBF) != 0)
741 1.41.6.5 joerg break; /* Nothing of interest here. */
742 1.41.6.5 joerg acpiec_write_data(sc, sc->sc_cur_addr);
743 1.41.6.5 joerg sc->sc_state = EC_STATE_WRITE_VAL;
744 1.41.6.5 joerg break;
745 1.41.6.3 joerg
746 1.41.6.5 joerg case EC_STATE_WRITE_VAL:
747 1.41.6.5 joerg if ((reg & EC_STATUS_IBF) != 0)
748 1.41.6.5 joerg break; /* Nothing of interest here. */
749 1.41.6.5 joerg cv_signal(&sc->sc_cv);
750 1.41.6.5 joerg acpiec_write_data(sc, sc->sc_cur_val);
751 1.41.6.5 joerg sc->sc_state = EC_STATE_WRITE_WAIT;
752 1.41.6.3 joerg break;
753 1.41.6.3 joerg
754 1.41.6.5 joerg case EC_STATE_WRITE_WAIT:
755 1.41.6.3 joerg break;
756 1.41.6.3 joerg
757 1.41.6.5 joerg case EC_STATE_FREE:
758 1.41.6.5 joerg if (sc->sc_got_sci)
759 1.41.6.5 joerg cv_signal(&sc->sc_cv_sci);
760 1.41.6.3 joerg break;
761 1.41.6.5 joerg default:
762 1.41.6.5 joerg panic("invalid state");
763 1.41.6.3 joerg }
764 1.41.6.5 joerg }
765 1.41.6.3 joerg
766 1.41.6.5 joerg static UINT32
767 1.41.6.5 joerg acpiec_gpe_handler(void *arg)
768 1.41.6.5 joerg {
769 1.41.6.5 joerg device_t dv = arg;
770 1.41.6.5 joerg struct acpiec_softc *sc = device_private(dv);
771 1.41.6.3 joerg
772 1.41.6.5 joerg AcpiClearGpe(sc->sc_gpeh, sc->sc_gpebit, ACPI_ISR);
773 1.41.6.5 joerg
774 1.41.6.5 joerg mutex_enter(&sc->sc_mtx);
775 1.41.6.5 joerg acpiec_gpe_state_maschine(dv);
776 1.41.6.5 joerg mutex_exit(&sc->sc_mtx);
777 1.41.6.5 joerg
778 1.41.6.5 joerg return 0;
779 1.41.6.3 joerg }
780