xref: /netbsd-src/sys/dev/acpi/acpi_ec.c (revision 7330f729ccf0bd976a06f95fad452fe774fc7fd1)
1 /*	$NetBSD: acpi_ec.c,v 1.77 2019/08/06 01:53:47 msaitoh Exp $	*/
2 
3 /*-
4  * Copyright (c) 2007 Joerg Sonnenberger <joerg@NetBSD.org>.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
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
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
22  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
24  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 /*
33  * The ACPI Embedded Controller (EC) driver serves two different purposes:
34  * - read and write access from ASL, e.g. to read battery state
35  * - notification of ASL of System Control Interrupts.
36  *
37  * Access to the EC is serialised by sc_access_mtx and optionally the
38  * ACPI global mutex.  Both locks are held until the request is fulfilled.
39  * All access to the softc has to hold sc_mtx to serialise against the GPE
40  * handler and the callout.  sc_mtx is also used for wakeup conditions.
41  *
42  * SCIs are processed in a kernel thread. Handling gets a bit complicated
43  * by the lock order (sc_mtx must be acquired after sc_access_mtx and the
44  * ACPI global mutex).
45  *
46  * Read and write requests spin around for a short time as many requests
47  * can be handled instantly by the EC.  During normal processing interrupt
48  * mode is used exclusively.  At boot and resume time interrupts are not
49  * working and the handlers just busy loop.
50  *
51  * A callout is scheduled to compensate for missing interrupts on some
52  * hardware.  If the EC doesn't process a request for 5s, it is most likely
53  * in a wedged state.  No method to reset the EC is currently known.
54  *
55  * Special care has to be taken to not poll the EC in a busy loop without
56  * delay.  This can prevent processing of Power Button events. At least some
57  * Lenovo Thinkpads seem to be implement the Power Button Override in the EC
58  * and the only option to recover on those models is to cut off all power.
59  */
60 
61 #include <sys/cdefs.h>
62 __KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.77 2019/08/06 01:53:47 msaitoh Exp $");
63 
64 #include <sys/param.h>
65 #include <sys/callout.h>
66 #include <sys/condvar.h>
67 #include <sys/device.h>
68 #include <sys/kernel.h>
69 #include <sys/kthread.h>
70 #include <sys/mutex.h>
71 #include <sys/systm.h>
72 
73 #include <dev/acpi/acpireg.h>
74 #include <dev/acpi/acpivar.h>
75 #include <dev/acpi/acpi_ecvar.h>
76 
77 #define _COMPONENT          ACPI_EC_COMPONENT
78 ACPI_MODULE_NAME            ("acpi_ec")
79 
80 /* Maximum time to wait for global ACPI lock in ms */
81 #define	EC_LOCK_TIMEOUT		5
82 
83 /* Maximum time to poll for completion of a command  in ms */
84 #define	EC_POLL_TIMEOUT		5
85 
86 /* Maximum time to give a single EC command in s */
87 #define EC_CMD_TIMEOUT		10
88 
89 /* From ACPI 3.0b, chapter 12.3 */
90 #define EC_COMMAND_READ		0x80
91 #define	EC_COMMAND_WRITE	0x81
92 #define	EC_COMMAND_BURST_EN	0x82
93 #define	EC_COMMAND_BURST_DIS	0x83
94 #define	EC_COMMAND_QUERY	0x84
95 
96 /* From ACPI 3.0b, chapter 12.2.1 */
97 #define	EC_STATUS_OBF		0x01
98 #define	EC_STATUS_IBF		0x02
99 #define	EC_STATUS_CMD		0x08
100 #define	EC_STATUS_BURST		0x10
101 #define	EC_STATUS_SCI		0x20
102 #define	EC_STATUS_SMI		0x40
103 
104 static const char *ec_hid[] = {
105 	"PNP0C09",
106 	NULL,
107 };
108 
109 enum ec_state_t {
110 	EC_STATE_QUERY,
111 	EC_STATE_QUERY_VAL,
112 	EC_STATE_READ,
113 	EC_STATE_READ_ADDR,
114 	EC_STATE_READ_VAL,
115 	EC_STATE_WRITE,
116 	EC_STATE_WRITE_ADDR,
117 	EC_STATE_WRITE_VAL,
118 	EC_STATE_FREE
119 };
120 
121 struct acpiec_softc {
122 	ACPI_HANDLE sc_ech;
123 
124 	ACPI_HANDLE sc_gpeh;
125 	uint8_t sc_gpebit;
126 
127 	bus_space_tag_t sc_data_st;
128 	bus_space_handle_t sc_data_sh;
129 
130 	bus_space_tag_t sc_csr_st;
131 	bus_space_handle_t sc_csr_sh;
132 
133 	bool sc_need_global_lock;
134 	uint32_t sc_global_lock;
135 
136 	kmutex_t sc_mtx, sc_access_mtx;
137 	kcondvar_t sc_cv, sc_cv_sci;
138 	enum ec_state_t sc_state;
139 	bool sc_got_sci;
140 	callout_t sc_pseudo_intr;
141 
142 	uint8_t sc_cur_addr, sc_cur_val;
143 };
144 
145 static int acpiecdt_match(device_t, cfdata_t, void *);
146 static void acpiecdt_attach(device_t, device_t, void *);
147 
148 static int acpiec_match(device_t, cfdata_t, void *);
149 static void acpiec_attach(device_t, device_t, void *);
150 
151 static void acpiec_common_attach(device_t, device_t, ACPI_HANDLE,
152     bus_space_tag_t, bus_addr_t, bus_space_tag_t, bus_addr_t,
153     ACPI_HANDLE, uint8_t);
154 
155 static bool acpiec_suspend(device_t, const pmf_qual_t *);
156 static bool acpiec_resume(device_t, const pmf_qual_t *);
157 static bool acpiec_shutdown(device_t, int);
158 
159 static bool acpiec_parse_gpe_package(device_t, ACPI_HANDLE,
160     ACPI_HANDLE *, uint8_t *);
161 
162 static void acpiec_callout(void *);
163 static void acpiec_gpe_query(void *);
164 static uint32_t acpiec_gpe_handler(ACPI_HANDLE, uint32_t, void *);
165 static ACPI_STATUS acpiec_space_setup(ACPI_HANDLE, uint32_t, void *, void **);
166 static ACPI_STATUS acpiec_space_handler(uint32_t, ACPI_PHYSICAL_ADDRESS,
167     uint32_t, ACPI_INTEGER *, void *, void *);
168 
169 static void acpiec_gpe_state_machine(device_t);
170 
171 CFATTACH_DECL_NEW(acpiec, sizeof(struct acpiec_softc),
172     acpiec_match, acpiec_attach, NULL, NULL);
173 
174 CFATTACH_DECL_NEW(acpiecdt, sizeof(struct acpiec_softc),
175     acpiecdt_match, acpiecdt_attach, NULL, NULL);
176 
177 static device_t ec_singleton = NULL;
178 static bool acpiec_cold = false;
179 
180 static bool
181 acpiecdt_find(device_t parent, ACPI_HANDLE *ec_handle,
182     bus_addr_t *cmd_reg, bus_addr_t *data_reg, uint8_t *gpebit)
183 {
184 	ACPI_TABLE_ECDT *ecdt;
185 	ACPI_STATUS rv;
186 
187 	rv = AcpiGetTable(ACPI_SIG_ECDT, 1, (ACPI_TABLE_HEADER **)&ecdt);
188 	if (ACPI_FAILURE(rv))
189 		return false;
190 
191 	if (ecdt->Control.BitWidth != 8 || ecdt->Data.BitWidth != 8) {
192 		aprint_error_dev(parent,
193 		    "ECDT register width invalid (%u/%u)\n",
194 		    ecdt->Control.BitWidth, ecdt->Data.BitWidth);
195 		return false;
196 	}
197 
198 	rv = AcpiGetHandle(ACPI_ROOT_OBJECT, ecdt->Id, ec_handle);
199 	if (ACPI_FAILURE(rv)) {
200 		aprint_error_dev(parent,
201 		    "failed to look up EC object %s: %s\n",
202 		    ecdt->Id, AcpiFormatException(rv));
203 		return false;
204 	}
205 
206 	*cmd_reg = ecdt->Control.Address;
207 	*data_reg = ecdt->Data.Address;
208 	*gpebit = ecdt->Gpe;
209 
210 	return true;
211 }
212 
213 static int
214 acpiecdt_match(device_t parent, cfdata_t match, void *aux)
215 {
216 	ACPI_HANDLE ec_handle;
217 	bus_addr_t cmd_reg, data_reg;
218 	uint8_t gpebit;
219 
220 	if (acpiecdt_find(parent, &ec_handle, &cmd_reg, &data_reg, &gpebit))
221 		return 1;
222 	else
223 		return 0;
224 }
225 
226 static void
227 acpiecdt_attach(device_t parent, device_t self, void *aux)
228 {
229 	struct acpibus_attach_args *aa = aux;
230 	ACPI_HANDLE ec_handle;
231 	bus_addr_t cmd_reg, data_reg;
232 	uint8_t gpebit;
233 
234 	if (!acpiecdt_find(parent, &ec_handle, &cmd_reg, &data_reg, &gpebit))
235 		panic("ECDT disappeared");
236 
237 	aprint_naive("\n");
238 	aprint_normal(": ACPI Embedded Controller via ECDT\n");
239 
240 	acpiec_common_attach(parent, self, ec_handle, aa->aa_iot, cmd_reg,
241 	    aa->aa_iot, data_reg, NULL, gpebit);
242 }
243 
244 static int
245 acpiec_match(device_t parent, cfdata_t match, void *aux)
246 {
247 	struct acpi_attach_args *aa = aux;
248 
249 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
250 		return 0;
251 
252 	return acpi_match_hid(aa->aa_node->ad_devinfo, ec_hid);
253 }
254 
255 static void
256 acpiec_attach(device_t parent, device_t self, void *aux)
257 {
258 	struct acpi_attach_args *aa = aux;
259 	struct acpi_resources ec_res;
260 	struct acpi_io *io0, *io1;
261 	ACPI_HANDLE gpe_handle;
262 	uint8_t gpebit;
263 	ACPI_STATUS rv;
264 
265 	if (ec_singleton != NULL) {
266 		aprint_naive(": using %s\n", device_xname(ec_singleton));
267 		aprint_normal(": using %s\n", device_xname(ec_singleton));
268 		goto fail0;
269 	}
270 
271 	if (!acpiec_parse_gpe_package(self, aa->aa_node->ad_handle,
272 				      &gpe_handle, &gpebit))
273 		goto fail0;
274 
275 	rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
276 	    &ec_res, &acpi_resource_parse_ops_default);
277 	if (rv != AE_OK) {
278 		aprint_error_dev(self, "resource parsing failed: %s\n",
279 		    AcpiFormatException(rv));
280 		goto fail0;
281 	}
282 
283 	if ((io0 = acpi_res_io(&ec_res, 0)) == NULL) {
284 		aprint_error_dev(self, "no data register resource\n");
285 		goto fail1;
286 	}
287 	if ((io1 = acpi_res_io(&ec_res, 1)) == NULL) {
288 		aprint_error_dev(self, "no CSR register resource\n");
289 		goto fail1;
290 	}
291 
292 	acpiec_common_attach(parent, self, aa->aa_node->ad_handle,
293 	    aa->aa_iot, io1->ar_base, aa->aa_iot, io0->ar_base,
294 	    gpe_handle, gpebit);
295 
296 	acpi_resource_cleanup(&ec_res);
297 	return;
298 
299 fail1:	acpi_resource_cleanup(&ec_res);
300 fail0:	if (!pmf_device_register(self, NULL, NULL))
301 		aprint_error_dev(self, "couldn't establish power handler\n");
302 }
303 
304 static void
305 acpiec_common_attach(device_t parent, device_t self,
306     ACPI_HANDLE ec_handle, bus_space_tag_t cmdt, bus_addr_t cmd_reg,
307     bus_space_tag_t datat, bus_addr_t data_reg,
308     ACPI_HANDLE gpe_handle, uint8_t gpebit)
309 {
310 	struct acpiec_softc *sc = device_private(self);
311 	ACPI_STATUS rv;
312 	ACPI_INTEGER val;
313 
314 	sc->sc_csr_st = cmdt;
315 	sc->sc_data_st = datat;
316 
317 	sc->sc_ech = ec_handle;
318 	sc->sc_gpeh = gpe_handle;
319 	sc->sc_gpebit = gpebit;
320 
321 	sc->sc_state = EC_STATE_FREE;
322 	mutex_init(&sc->sc_mtx, MUTEX_DRIVER, IPL_TTY);
323 	mutex_init(&sc->sc_access_mtx, MUTEX_DEFAULT, IPL_NONE);
324 	cv_init(&sc->sc_cv, "eccv");
325 	cv_init(&sc->sc_cv_sci, "ecsci");
326 
327 	if (bus_space_map(sc->sc_data_st, data_reg, 1, 0,
328 	    &sc->sc_data_sh) != 0) {
329 		aprint_error_dev(self, "unable to map data register\n");
330 		return;
331 	}
332 
333 	if (bus_space_map(sc->sc_csr_st, cmd_reg, 1, 0, &sc->sc_csr_sh) != 0) {
334 		aprint_error_dev(self, "unable to map CSR register\n");
335 		goto post_data_map;
336 	}
337 
338 	rv = acpi_eval_integer(sc->sc_ech, "_GLK", &val);
339 	if (rv == AE_OK) {
340 		sc->sc_need_global_lock = val != 0;
341 	} else if (rv != AE_NOT_FOUND) {
342 		aprint_error_dev(self, "unable to evaluate _GLK: %s\n",
343 		    AcpiFormatException(rv));
344 		goto post_csr_map;
345 	} else {
346 		sc->sc_need_global_lock = false;
347 	}
348 	if (sc->sc_need_global_lock)
349 		aprint_normal_dev(self, "using global ACPI lock\n");
350 
351 	callout_init(&sc->sc_pseudo_intr, CALLOUT_MPSAFE);
352 	callout_setfunc(&sc->sc_pseudo_intr, acpiec_callout, self);
353 
354 	rv = AcpiInstallAddressSpaceHandler(sc->sc_ech, ACPI_ADR_SPACE_EC,
355 	    acpiec_space_handler, acpiec_space_setup, self);
356 	if (rv != AE_OK) {
357 		aprint_error_dev(self,
358 		    "unable to install address space handler: %s\n",
359 		    AcpiFormatException(rv));
360 		goto post_csr_map;
361 	}
362 
363 	rv = AcpiInstallGpeHandler(sc->sc_gpeh, sc->sc_gpebit,
364 	    ACPI_GPE_EDGE_TRIGGERED, acpiec_gpe_handler, self);
365 	if (rv != AE_OK) {
366 		aprint_error_dev(self, "unable to install GPE handler: %s\n",
367 		    AcpiFormatException(rv));
368 		goto post_csr_map;
369 	}
370 
371 	rv = AcpiEnableGpe(sc->sc_gpeh, sc->sc_gpebit);
372 	if (rv != AE_OK) {
373 		aprint_error_dev(self, "unable to enable GPE: %s\n",
374 		    AcpiFormatException(rv));
375 		goto post_csr_map;
376 	}
377 
378 	if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL, acpiec_gpe_query,
379 		           self, NULL, "acpiec sci thread")) {
380 		aprint_error_dev(self, "unable to create query kthread\n");
381 		goto post_csr_map;
382 	}
383 
384 	ec_singleton = self;
385 
386 	if (!pmf_device_register1(self, acpiec_suspend, acpiec_resume,
387 	    acpiec_shutdown))
388 		aprint_error_dev(self, "couldn't establish power handler\n");
389 
390 	return;
391 
392 post_csr_map:
393 	(void)AcpiRemoveGpeHandler(sc->sc_gpeh, sc->sc_gpebit,
394 	    acpiec_gpe_handler);
395 	(void)AcpiRemoveAddressSpaceHandler(sc->sc_ech,
396 	    ACPI_ADR_SPACE_EC, acpiec_space_handler);
397 	bus_space_unmap(sc->sc_csr_st, sc->sc_csr_sh, 1);
398 post_data_map:
399 	bus_space_unmap(sc->sc_data_st, sc->sc_data_sh, 1);
400 	if (!pmf_device_register(self, NULL, NULL))
401 		aprint_error_dev(self, "couldn't establish power handler\n");
402 }
403 
404 static bool
405 acpiec_suspend(device_t dv, const pmf_qual_t *qual)
406 {
407 
408 	acpiec_cold = true;
409 
410 	return true;
411 }
412 
413 static bool
414 acpiec_resume(device_t dv, const pmf_qual_t *qual)
415 {
416 
417 	acpiec_cold = false;
418 
419 	return true;
420 }
421 
422 static bool
423 acpiec_shutdown(device_t dv, int how)
424 {
425 
426 	acpiec_cold = true;
427 	return true;
428 }
429 
430 static bool
431 acpiec_parse_gpe_package(device_t self, ACPI_HANDLE ec_handle,
432     ACPI_HANDLE *gpe_handle, uint8_t *gpebit)
433 {
434 	ACPI_BUFFER buf;
435 	ACPI_OBJECT *p, *c;
436 	ACPI_STATUS rv;
437 
438 	rv = acpi_eval_struct(ec_handle, "_GPE", &buf);
439 	if (rv != AE_OK) {
440 		aprint_error_dev(self, "unable to evaluate _GPE: %s\n",
441 		    AcpiFormatException(rv));
442 		return false;
443 	}
444 
445 	p = buf.Pointer;
446 
447 	if (p->Type == ACPI_TYPE_INTEGER) {
448 		*gpe_handle = NULL;
449 		*gpebit = p->Integer.Value;
450 		ACPI_FREE(p);
451 		return true;
452 	}
453 
454 	if (p->Type != ACPI_TYPE_PACKAGE) {
455 		aprint_error_dev(self, "_GPE is neither integer nor package\n");
456 		ACPI_FREE(p);
457 		return false;
458 	}
459 
460 	if (p->Package.Count != 2) {
461 		aprint_error_dev(self,
462 		    "_GPE package does not contain 2 elements\n");
463 		ACPI_FREE(p);
464 		return false;
465 	}
466 
467 	c = &p->Package.Elements[0];
468 	rv = acpi_eval_reference_handle(c, gpe_handle);
469 
470 	if (ACPI_FAILURE(rv)) {
471 		aprint_error_dev(self, "failed to evaluate _GPE handle\n");
472 		ACPI_FREE(p);
473 		return false;
474 	}
475 
476 	c = &p->Package.Elements[1];
477 
478 	if (c->Type != ACPI_TYPE_INTEGER) {
479 		aprint_error_dev(self,
480 		    "_GPE package needs integer as 2nd field\n");
481 		ACPI_FREE(p);
482 		return false;
483 	}
484 	*gpebit = c->Integer.Value;
485 	ACPI_FREE(p);
486 	return true;
487 }
488 
489 static uint8_t
490 acpiec_read_data(struct acpiec_softc *sc)
491 {
492 	return bus_space_read_1(sc->sc_data_st, sc->sc_data_sh, 0);
493 }
494 
495 static void
496 acpiec_write_data(struct acpiec_softc *sc, uint8_t val)
497 {
498 	bus_space_write_1(sc->sc_data_st, sc->sc_data_sh, 0, val);
499 }
500 
501 static uint8_t
502 acpiec_read_status(struct acpiec_softc *sc)
503 {
504 	return bus_space_read_1(sc->sc_csr_st, sc->sc_csr_sh, 0);
505 }
506 
507 static void
508 acpiec_write_command(struct acpiec_softc *sc, uint8_t cmd)
509 {
510 	bus_space_write_1(sc->sc_csr_st, sc->sc_csr_sh, 0, cmd);
511 }
512 
513 static ACPI_STATUS
514 acpiec_space_setup(ACPI_HANDLE region, uint32_t func, void *arg,
515     void **region_arg)
516 {
517 
518 	if (func == ACPI_REGION_DEACTIVATE)
519 		*region_arg = NULL;
520 	else
521 		*region_arg = arg;
522 
523 	return AE_OK;
524 }
525 
526 static void
527 acpiec_lock(device_t dv)
528 {
529 	struct acpiec_softc *sc = device_private(dv);
530 	ACPI_STATUS rv;
531 
532 	mutex_enter(&sc->sc_access_mtx);
533 
534 	if (sc->sc_need_global_lock) {
535 		rv = AcpiAcquireGlobalLock(EC_LOCK_TIMEOUT,
536 		    &sc->sc_global_lock);
537 		if (rv != AE_OK) {
538 			aprint_error_dev(dv,
539 			    "failed to acquire global lock: %s\n",
540 			    AcpiFormatException(rv));
541 			return;
542 		}
543 	}
544 }
545 
546 static void
547 acpiec_unlock(device_t dv)
548 {
549 	struct acpiec_softc *sc = device_private(dv);
550 	ACPI_STATUS rv;
551 
552 	if (sc->sc_need_global_lock) {
553 		rv = AcpiReleaseGlobalLock(sc->sc_global_lock);
554 		if (rv != AE_OK) {
555 			aprint_error_dev(dv,
556 			    "failed to release global lock: %s\n",
557 			    AcpiFormatException(rv));
558 		}
559 	}
560 	mutex_exit(&sc->sc_access_mtx);
561 }
562 
563 static ACPI_STATUS
564 acpiec_read(device_t dv, uint8_t addr, uint8_t *val)
565 {
566 	struct acpiec_softc *sc = device_private(dv);
567 	int i, timeo = 1000 * EC_CMD_TIMEOUT;
568 
569 	acpiec_lock(dv);
570 	mutex_enter(&sc->sc_mtx);
571 
572 	sc->sc_cur_addr = addr;
573 	sc->sc_state = EC_STATE_READ;
574 
575 	for (i = 0; i < EC_POLL_TIMEOUT; ++i) {
576 		acpiec_gpe_state_machine(dv);
577 		if (sc->sc_state == EC_STATE_FREE)
578 			goto done;
579 		delay(1);
580 	}
581 
582 	if (cold || acpiec_cold) {
583 		while (sc->sc_state != EC_STATE_FREE && timeo-- > 0) {
584 			delay(1000);
585 			acpiec_gpe_state_machine(dv);
586 		}
587 		if (sc->sc_state != EC_STATE_FREE) {
588 			mutex_exit(&sc->sc_mtx);
589 			acpiec_unlock(dv);
590 			aprint_error_dev(dv, "command timed out, state %d\n",
591 			    sc->sc_state);
592 			return AE_ERROR;
593 		}
594 	} else if (cv_timedwait(&sc->sc_cv, &sc->sc_mtx, EC_CMD_TIMEOUT * hz)) {
595 		mutex_exit(&sc->sc_mtx);
596 		acpiec_unlock(dv);
597 		aprint_error_dev(dv,
598 		    "command takes over %d sec...\n", EC_CMD_TIMEOUT);
599 		return AE_ERROR;
600 	}
601 
602 done:
603 	*val = sc->sc_cur_val;
604 
605 	mutex_exit(&sc->sc_mtx);
606 	acpiec_unlock(dv);
607 	return AE_OK;
608 }
609 
610 static ACPI_STATUS
611 acpiec_write(device_t dv, uint8_t addr, uint8_t val)
612 {
613 	struct acpiec_softc *sc = device_private(dv);
614 	int i, timeo = 1000 * EC_CMD_TIMEOUT;
615 
616 	acpiec_lock(dv);
617 	mutex_enter(&sc->sc_mtx);
618 
619 	sc->sc_cur_addr = addr;
620 	sc->sc_cur_val = val;
621 	sc->sc_state = EC_STATE_WRITE;
622 
623 	for (i = 0; i < EC_POLL_TIMEOUT; ++i) {
624 		acpiec_gpe_state_machine(dv);
625 		if (sc->sc_state == EC_STATE_FREE)
626 			goto done;
627 		delay(1);
628 	}
629 
630 	if (cold || acpiec_cold) {
631 		while (sc->sc_state != EC_STATE_FREE && timeo-- > 0) {
632 			delay(1000);
633 			acpiec_gpe_state_machine(dv);
634 		}
635 		if (sc->sc_state != EC_STATE_FREE) {
636 			mutex_exit(&sc->sc_mtx);
637 			acpiec_unlock(dv);
638 			aprint_error_dev(dv, "command timed out, state %d\n",
639 			    sc->sc_state);
640 			return AE_ERROR;
641 		}
642 	} else if (cv_timedwait(&sc->sc_cv, &sc->sc_mtx, EC_CMD_TIMEOUT * hz)) {
643 		mutex_exit(&sc->sc_mtx);
644 		acpiec_unlock(dv);
645 		aprint_error_dev(dv,
646 		    "command takes over %d sec...\n", EC_CMD_TIMEOUT);
647 		return AE_ERROR;
648 	}
649 
650 done:
651 	mutex_exit(&sc->sc_mtx);
652 	acpiec_unlock(dv);
653 	return AE_OK;
654 }
655 
656 static ACPI_STATUS
657 acpiec_space_handler(uint32_t func, ACPI_PHYSICAL_ADDRESS paddr,
658     uint32_t width, ACPI_INTEGER *value, void *arg, void *region_arg)
659 {
660 	device_t dv = arg;
661 	ACPI_STATUS rv;
662 	uint8_t addr;
663 	uint8_t *reg;
664 
665 	if ((func != ACPI_READ) && (func != ACPI_WRITE)) {
666 		aprint_error("%s: invalid Address Space function called: %x\n",
667 		    device_xname(dv), (unsigned int)func);
668 		return AE_BAD_PARAMETER;
669 	}
670 	if (paddr > 0xff || width % 8 != 0 || value == NULL || arg == NULL ||
671 	    paddr + width / 8 > 0x100)
672 		return AE_BAD_PARAMETER;
673 
674 	addr = paddr;
675 	reg = (uint8_t *)value;
676 
677 	rv = AE_OK;
678 
679 	if (func == ACPI_READ)
680 		*value = 0;
681 
682 	for (addr = paddr; addr < (paddr + width / 8); addr++, reg++) {
683 		if (func == ACPI_READ)
684 			rv = acpiec_read(dv, addr, reg);
685 		else
686 			rv = acpiec_write(dv, addr, *reg);
687 
688 		if (rv != AE_OK)
689 			break;
690 	}
691 
692 	return rv;
693 }
694 
695 static void
696 acpiec_gpe_query(void *arg)
697 {
698 	device_t dv = arg;
699 	struct acpiec_softc *sc = device_private(dv);
700 	uint8_t reg;
701 	char qxx[5];
702 	ACPI_STATUS rv;
703 	int i;
704 
705 loop:
706 	mutex_enter(&sc->sc_mtx);
707 
708 	if (sc->sc_got_sci == false)
709 		cv_wait(&sc->sc_cv_sci, &sc->sc_mtx);
710 	mutex_exit(&sc->sc_mtx);
711 
712 	acpiec_lock(dv);
713 	mutex_enter(&sc->sc_mtx);
714 
715 	/* The Query command can always be issued, so be defensive here. */
716 	sc->sc_got_sci = false;
717 	sc->sc_state = EC_STATE_QUERY;
718 
719 	for (i = 0; i < EC_POLL_TIMEOUT; ++i) {
720 		acpiec_gpe_state_machine(dv);
721 		if (sc->sc_state == EC_STATE_FREE)
722 			goto done;
723 		delay(1);
724 	}
725 
726 	cv_wait(&sc->sc_cv, &sc->sc_mtx);
727 
728 done:
729 	reg = sc->sc_cur_val;
730 
731 	mutex_exit(&sc->sc_mtx);
732 	acpiec_unlock(dv);
733 
734 	if (reg == 0)
735 		goto loop; /* Spurious query result */
736 
737 	/*
738 	 * Evaluate _Qxx to respond to the controller.
739 	 */
740 	snprintf(qxx, sizeof(qxx), "_Q%02X", (unsigned int)reg);
741 	rv = AcpiEvaluateObject(sc->sc_ech, qxx, NULL, NULL);
742 	if (rv != AE_OK && rv != AE_NOT_FOUND) {
743 		aprint_error_dev(dv, "GPE query method %s failed: %s",
744 		    qxx, AcpiFormatException(rv));
745 	}
746 
747 	goto loop;
748 }
749 
750 static void
751 acpiec_gpe_state_machine(device_t dv)
752 {
753 	struct acpiec_softc *sc = device_private(dv);
754 	uint8_t reg;
755 
756 	reg = acpiec_read_status(sc);
757 
758 	if (reg & EC_STATUS_SCI)
759 		sc->sc_got_sci = true;
760 
761 	switch (sc->sc_state) {
762 	case EC_STATE_QUERY:
763 		if ((reg & EC_STATUS_IBF) != 0)
764 			break; /* Nothing of interest here. */
765 		acpiec_write_command(sc, EC_COMMAND_QUERY);
766 		sc->sc_state = EC_STATE_QUERY_VAL;
767 		break;
768 
769 	case EC_STATE_QUERY_VAL:
770 		if ((reg & EC_STATUS_OBF) == 0)
771 			break; /* Nothing of interest here. */
772 
773 		sc->sc_cur_val = acpiec_read_data(sc);
774 		sc->sc_state = EC_STATE_FREE;
775 
776 		cv_signal(&sc->sc_cv);
777 		break;
778 
779 	case EC_STATE_READ:
780 		if ((reg & EC_STATUS_IBF) != 0)
781 			break; /* Nothing of interest here. */
782 
783 		acpiec_write_command(sc, EC_COMMAND_READ);
784 		sc->sc_state = EC_STATE_READ_ADDR;
785 		break;
786 
787 	case EC_STATE_READ_ADDR:
788 		if ((reg & EC_STATUS_IBF) != 0)
789 			break; /* Nothing of interest here. */
790 
791 		acpiec_write_data(sc, sc->sc_cur_addr);
792 		sc->sc_state = EC_STATE_READ_VAL;
793 		break;
794 
795 	case EC_STATE_READ_VAL:
796 		if ((reg & EC_STATUS_OBF) == 0)
797 			break; /* Nothing of interest here. */
798 		sc->sc_cur_val = acpiec_read_data(sc);
799 		sc->sc_state = EC_STATE_FREE;
800 
801 		cv_signal(&sc->sc_cv);
802 		break;
803 
804 	case EC_STATE_WRITE:
805 		if ((reg & EC_STATUS_IBF) != 0)
806 			break; /* Nothing of interest here. */
807 
808 		acpiec_write_command(sc, EC_COMMAND_WRITE);
809 		sc->sc_state = EC_STATE_WRITE_ADDR;
810 		break;
811 
812 	case EC_STATE_WRITE_ADDR:
813 		if ((reg & EC_STATUS_IBF) != 0)
814 			break; /* Nothing of interest here. */
815 		acpiec_write_data(sc, sc->sc_cur_addr);
816 		sc->sc_state = EC_STATE_WRITE_VAL;
817 		break;
818 
819 	case EC_STATE_WRITE_VAL:
820 		if ((reg & EC_STATUS_IBF) != 0)
821 			break; /* Nothing of interest here. */
822 		sc->sc_state = EC_STATE_FREE;
823 		cv_signal(&sc->sc_cv);
824 
825 		acpiec_write_data(sc, sc->sc_cur_val);
826 		break;
827 
828 	case EC_STATE_FREE:
829 		if (sc->sc_got_sci)
830 			cv_signal(&sc->sc_cv_sci);
831 		break;
832 	default:
833 		panic("invalid state");
834 	}
835 
836 	if (sc->sc_state != EC_STATE_FREE)
837 		callout_schedule(&sc->sc_pseudo_intr, 1);
838 }
839 
840 static void
841 acpiec_callout(void *arg)
842 {
843 	device_t dv = arg;
844 	struct acpiec_softc *sc = device_private(dv);
845 
846 	mutex_enter(&sc->sc_mtx);
847 	acpiec_gpe_state_machine(dv);
848 	mutex_exit(&sc->sc_mtx);
849 }
850 
851 static uint32_t
852 acpiec_gpe_handler(ACPI_HANDLE hdl, uint32_t gpebit, void *arg)
853 {
854 	device_t dv = arg;
855 	struct acpiec_softc *sc = device_private(dv);
856 
857 	mutex_enter(&sc->sc_mtx);
858 	acpiec_gpe_state_machine(dv);
859 	mutex_exit(&sc->sc_mtx);
860 
861 	return ACPI_INTERRUPT_HANDLED | ACPI_REENABLE_GPE;
862 }
863 
864 ACPI_STATUS
865 acpiec_bus_read(device_t dv, u_int addr, ACPI_INTEGER *val, int width)
866 {
867 	return acpiec_space_handler(ACPI_READ, addr, width * 8, val, dv, NULL);
868 }
869 
870 ACPI_STATUS
871 acpiec_bus_write(device_t dv, u_int addr, ACPI_INTEGER val, int width)
872 {
873 	return acpiec_space_handler(ACPI_WRITE, addr, width * 8, &val, dv,
874 	    NULL);
875 }
876 
877 ACPI_HANDLE
878 acpiec_get_handle(device_t dv)
879 {
880 	struct acpiec_softc *sc = device_private(dv);
881 
882 	return sc->sc_ech;
883 }
884