xref: /netbsd-src/sys/dev/acpi/acpi_ec.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: acpi_ec.c,v 1.52 2008/06/03 15:12:39 joerg 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.52 2008/06/03 15:12:39 joerg Exp $");
63 
64 #include <sys/param.h>
65 #include <sys/systm.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 
72 #include <sys/bus.h>
73 
74 #include <dev/acpi/acpivar.h>
75 #include <dev/acpi/acpi_ecvar.h>
76 
77 /* Maximum time to wait for global ACPI lock in ms */
78 #define	EC_LOCK_TIMEOUT		5
79 
80 /* Maximum time to poll for completion of a command  in ms */
81 #define	EC_POLL_TIMEOUT		5
82 
83 /* Maximum time to give a single EC command in s */
84 #define EC_CMD_TIMEOUT		10
85 
86 /* From ACPI 3.0b, chapter 12.3 */
87 #define EC_COMMAND_READ		0x80
88 #define	EC_COMMAND_WRITE	0x81
89 #define	EC_COMMAND_BURST_EN	0x82
90 #define	EC_COMMAND_BURST_DIS	0x83
91 #define	EC_COMMAND_QUERY	0x84
92 
93 /* From ACPI 3.0b, chapter 12.2.1 */
94 #define	EC_STATUS_OBF		0x01
95 #define	EC_STATUS_IBF		0x02
96 #define	EC_STATUS_CMD		0x08
97 #define	EC_STATUS_BURST		0x10
98 #define	EC_STATUS_SCI		0x20
99 #define	EC_STATUS_SMI		0x40
100 
101 static const char *ec_hid[] = {
102 	"PNP0C09",
103 	NULL,
104 };
105 
106 enum ec_state_t {
107 	EC_STATE_QUERY,
108 	EC_STATE_QUERY_VAL,
109 	EC_STATE_READ,
110 	EC_STATE_READ_ADDR,
111 	EC_STATE_READ_VAL,
112 	EC_STATE_WRITE,
113 	EC_STATE_WRITE_ADDR,
114 	EC_STATE_WRITE_VAL,
115 	EC_STATE_FREE
116 };
117 
118 struct acpiec_softc {
119 	ACPI_HANDLE sc_ech;
120 
121 	ACPI_HANDLE sc_gpeh;
122 	UINT8 sc_gpebit;
123 
124 	bus_space_tag_t sc_data_st;
125 	bus_space_handle_t sc_data_sh;
126 
127 	bus_space_tag_t sc_csr_st;
128 	bus_space_handle_t sc_csr_sh;
129 
130 	bool sc_need_global_lock;
131 	UINT32 sc_global_lock;
132 
133 	kmutex_t sc_mtx, sc_access_mtx;
134 	kcondvar_t sc_cv, sc_cv_sci;
135 	enum ec_state_t sc_state;
136 	bool sc_got_sci;
137 	callout_t sc_pseudo_intr;
138 
139 	uint8_t sc_cur_addr, sc_cur_val;
140 };
141 
142 static int acpiecdt_match(device_t, struct cfdata *, void *);
143 static void acpiecdt_attach(device_t, device_t, void *);
144 
145 static int acpiec_match(device_t, struct cfdata *, void *);
146 static void acpiec_attach(device_t, device_t, void *);
147 
148 static void acpiec_common_attach(device_t, device_t, ACPI_HANDLE,
149     bus_addr_t, bus_addr_t, ACPI_HANDLE, uint8_t);
150 
151 static bool acpiec_resume(device_t PMF_FN_PROTO);
152 static bool acpiec_suspend(device_t PMF_FN_PROTO);
153 
154 static bool acpiec_parse_gpe_package(device_t, ACPI_HANDLE,
155     ACPI_HANDLE *, uint8_t *);
156 
157 static void acpiec_callout(void *);
158 static void acpiec_gpe_query(void *);
159 static UINT32 acpiec_gpe_handler(void *);
160 static ACPI_STATUS acpiec_space_setup(ACPI_HANDLE, UINT32, void *, void **);
161 static ACPI_STATUS acpiec_space_handler(UINT32, ACPI_PHYSICAL_ADDRESS,
162     UINT32, ACPI_INTEGER *, void *, void *);
163 
164 static void acpiec_gpe_state_machine(device_t);
165 
166 CFATTACH_DECL_NEW(acpiec, sizeof(struct acpiec_softc),
167     acpiec_match, acpiec_attach, NULL, NULL);
168 
169 CFATTACH_DECL_NEW(acpiecdt, sizeof(struct acpiec_softc),
170     acpiecdt_match, acpiecdt_attach, NULL, NULL);
171 
172 static device_t ec_singleton = NULL;
173 static bool acpiec_cold = false;
174 
175 static bool
176 acpiecdt_find(device_t parent, ACPI_HANDLE *ec_handle,
177     bus_addr_t *cmd_reg, bus_addr_t *data_reg, uint8_t *gpebit)
178 {
179 	ACPI_TABLE_ECDT *ecdt;
180 	ACPI_STATUS rv;
181 
182 	rv = AcpiGetTable(ACPI_SIG_ECDT, 1, (ACPI_TABLE_HEADER **)&ecdt);
183 	if (ACPI_FAILURE(rv))
184 		return false;
185 
186 	if (ecdt->Control.BitWidth != 8 || ecdt->Data.BitWidth != 8) {
187 		aprint_error_dev(parent,
188 		    "ECDT register width invalid (%d/%d)\n",
189 		    ecdt->Control.BitWidth, ecdt->Data.BitWidth);
190 		return false;
191 	}
192 
193 	rv = AcpiGetHandle(ACPI_ROOT_OBJECT, ecdt->Id, ec_handle);
194 	if (ACPI_FAILURE(rv)) {
195 		aprint_error_dev(parent,
196 		    "failed to look up EC object %s: %s\n",
197 		    ecdt->Id, AcpiFormatException(rv));
198 		return false;
199 	}
200 
201 	*cmd_reg = ecdt->Control.Address;
202 	*data_reg = ecdt->Data.Address;
203 	*gpebit = ecdt->Gpe;
204 
205 	return true;
206 }
207 
208 static int
209 acpiecdt_match(device_t parent, struct cfdata *match, void *aux)
210 {
211 	ACPI_HANDLE ec_handle;
212 	bus_addr_t cmd_reg, data_reg;
213 	uint8_t gpebit;
214 
215 	if (acpiecdt_find(parent, &ec_handle, &cmd_reg, &data_reg, &gpebit))
216 		return 1;
217 	else
218 		return 0;
219 }
220 
221 static void
222 acpiecdt_attach(device_t parent, device_t self, void *aux)
223 {
224 	ACPI_HANDLE ec_handle;
225 	bus_addr_t cmd_reg, data_reg;
226 	uint8_t gpebit;
227 
228 	if (!acpiecdt_find(parent, &ec_handle, &cmd_reg, &data_reg, &gpebit))
229 		panic("ECDT disappeared");
230 
231 	aprint_naive(": ACPI Embedded Controller via ECDT\n");
232 	aprint_normal(": ACPI Embedded Controller via ECDT\n");
233 
234 	acpiec_common_attach(parent, self, ec_handle, cmd_reg, data_reg,
235 	    NULL, gpebit);
236 }
237 
238 static int
239 acpiec_match(device_t parent, struct cfdata *match, void *aux)
240 {
241 	struct acpi_attach_args *aa = aux;
242 
243 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
244 		return 0;
245 
246 	return acpi_match_hid(aa->aa_node->ad_devinfo, ec_hid);
247 }
248 
249 static void
250 acpiec_attach(device_t parent, device_t self, void *aux)
251 {
252 	struct acpi_attach_args *aa = aux;
253 	struct acpi_resources ec_res;
254 	struct acpi_io *io0, *io1;
255 	ACPI_HANDLE gpe_handle;
256 	uint8_t gpebit;
257 	ACPI_STATUS rv;
258 
259 	if (ec_singleton != NULL) {
260 		aprint_naive(": ACPI Embedded Controller (disabled)\n");
261 		aprint_normal(": ACPI Embedded Controller (disabled)\n");
262 		if (!pmf_device_register(self, NULL, NULL))
263 			aprint_error_dev(self, "couldn't establish power handler\n");
264 		return;
265 	}
266 
267 	aprint_naive(": ACPI Embedded Controller\n");
268 	aprint_normal(": ACPI Embedded Controller\n");
269 
270 	if (!acpiec_parse_gpe_package(self, aa->aa_node->ad_handle,
271 				      &gpe_handle, &gpebit))
272 		return;
273 
274 	rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
275 	    &ec_res, &acpi_resource_parse_ops_default);
276 	if (rv != AE_OK) {
277 		aprint_error_dev(self, "resource parsing failed: %s\n",
278 		    AcpiFormatException(rv));
279 		return;
280 	}
281 
282 	if ((io0 = acpi_res_io(&ec_res, 0)) == NULL) {
283 		aprint_error_dev(self, "no data register resource\n");
284 		goto free_res;
285 	}
286 	if ((io1 = acpi_res_io(&ec_res, 1)) == NULL) {
287 		aprint_error_dev(self, "no CSR register resource\n");
288 		goto free_res;
289 	}
290 
291 	acpiec_common_attach(parent, self, aa->aa_node->ad_handle,
292 	    io1->ar_base, io0->ar_base, gpe_handle, gpebit);
293 
294 free_res:
295 	acpi_resource_cleanup(&ec_res);
296 }
297 
298 static void
299 acpiec_common_attach(device_t parent, device_t self,
300     ACPI_HANDLE ec_handle, bus_addr_t cmd_reg, bus_addr_t data_reg,
301     ACPI_HANDLE gpe_handle, uint8_t gpebit)
302 {
303 	struct acpiec_softc *sc = device_private(self);
304 	ACPI_STATUS rv;
305 	ACPI_INTEGER val;
306 
307 	sc->sc_ech = ec_handle;
308 	sc->sc_gpeh = gpe_handle;
309 	sc->sc_gpebit = gpebit;
310 
311 	sc->sc_state = EC_STATE_FREE;
312 	mutex_init(&sc->sc_mtx, MUTEX_DRIVER, IPL_TTY);
313 	mutex_init(&sc->sc_access_mtx, MUTEX_DEFAULT, IPL_NONE);
314 	cv_init(&sc->sc_cv, "eccv");
315 	cv_init(&sc->sc_cv_sci, "ecsci");
316 
317 	if (bus_space_map(sc->sc_data_st, data_reg, 1, 0,
318 	    &sc->sc_data_sh) != 0) {
319 		aprint_error_dev(self, "unable to map data register\n");
320 		return;
321 	}
322 
323 	if (bus_space_map(sc->sc_csr_st, cmd_reg, 1, 0, &sc->sc_csr_sh) != 0) {
324 		aprint_error_dev(self, "unable to map CSR register\n");
325 		goto post_data_map;
326 	}
327 
328 	rv = acpi_eval_integer(sc->sc_ech, "_GLK", &val);
329 	if (rv == AE_OK) {
330 		sc->sc_need_global_lock = val != 0;
331 	} else if (rv != AE_NOT_FOUND) {
332 		aprint_error_dev(self, "unable to evaluate _GLK: %s\n",
333 		    AcpiFormatException(rv));
334 		goto post_csr_map;
335 	} else {
336 		sc->sc_need_global_lock = false;
337 	}
338 	if (sc->sc_need_global_lock)
339 		aprint_normal_dev(self, "using global ACPI lock\n");
340 
341 	callout_init(&sc->sc_pseudo_intr, CALLOUT_MPSAFE);
342 	callout_setfunc(&sc->sc_pseudo_intr, acpiec_callout, self);
343 
344 	rv = AcpiInstallAddressSpaceHandler(sc->sc_ech, ACPI_ADR_SPACE_EC,
345 	    acpiec_space_handler, acpiec_space_setup, self);
346 	if (rv != AE_OK) {
347 		aprint_error_dev(self,
348 		    "unable to install address space handler: %s\n",
349 		    AcpiFormatException(rv));
350 		goto post_csr_map;
351 	}
352 
353 	rv = AcpiInstallGpeHandler(sc->sc_gpeh, sc->sc_gpebit,
354 	    ACPI_GPE_EDGE_TRIGGERED, acpiec_gpe_handler, self);
355 	if (rv != AE_OK) {
356 		aprint_error_dev(self, "unable to install GPE handler: %s\n",
357 		    AcpiFormatException(rv));
358 		goto post_csr_map;
359 	}
360 
361 	rv = AcpiSetGpeType(sc->sc_gpeh, sc->sc_gpebit, ACPI_GPE_TYPE_RUNTIME);
362 	if (rv != AE_OK) {
363 		aprint_error_dev(self, "unable to set GPE type: %s\n",
364 		    AcpiFormatException(rv));
365 		goto post_csr_map;
366 	}
367 
368 	rv = AcpiEnableGpe(sc->sc_gpeh, sc->sc_gpebit, ACPI_ISR);
369 	if (rv != AE_OK) {
370 		aprint_error_dev(self, "unable to enable GPE: %s\n",
371 		    AcpiFormatException(rv));
372 		goto post_csr_map;
373 	}
374 
375 	if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL, acpiec_gpe_query,
376 		           self, NULL, "acpiec sci thread")) {
377 		aprint_error_dev(self, "unable to create query kthread\n");
378 		goto post_csr_map;
379 	}
380 
381 	ec_singleton = self;
382 
383 	if (!pmf_device_register(self, acpiec_suspend, acpiec_resume))
384 		aprint_error_dev(self, "couldn't establish power handler\n");
385 
386 	return;
387 
388 post_csr_map:
389 	(void)AcpiRemoveGpeHandler(sc->sc_gpeh, sc->sc_gpebit,
390 	    acpiec_gpe_handler);
391 	(void)AcpiRemoveAddressSpaceHandler(sc->sc_ech,
392 	    ACPI_ADR_SPACE_EC, acpiec_space_handler);
393 	bus_space_unmap(sc->sc_csr_st, sc->sc_csr_sh, 1);
394 post_data_map:
395 	bus_space_unmap(sc->sc_data_st, sc->sc_data_sh, 1);
396 }
397 
398 static bool
399 acpiec_suspend(device_t dv PMF_FN_ARGS)
400 {
401 	acpiec_cold = true;
402 
403 	return true;
404 }
405 
406 static bool
407 acpiec_resume(device_t dv PMF_FN_ARGS)
408 {
409 	acpiec_cold = false;
410 
411 	return true;
412 }
413 
414 static bool
415 acpiec_parse_gpe_package(device_t self, ACPI_HANDLE ec_handle,
416     ACPI_HANDLE *gpe_handle, uint8_t *gpebit)
417 {
418 	ACPI_BUFFER buf;
419 	ACPI_OBJECT *p, *c;
420 	ACPI_STATUS rv;
421 
422 	rv = acpi_eval_struct(ec_handle, "_GPE", &buf);
423 	if (rv != AE_OK) {
424 		aprint_error_dev(self, "unable to evaluate _GPE: %s\n",
425 		    AcpiFormatException(rv));
426 		return false;
427 	}
428 
429 	p = buf.Pointer;
430 
431 	if (p->Type == ACPI_TYPE_INTEGER) {
432 		*gpe_handle = NULL;
433 		*gpebit = p->Integer.Value;
434 		AcpiOsFree(p);
435 		return true;
436 	}
437 
438 	if (p->Type != ACPI_TYPE_PACKAGE) {
439 		aprint_error_dev(self, "_GPE is neither integer nor package\n");
440 		AcpiOsFree(p);
441 		return false;
442 	}
443 
444 	if (p->Package.Count != 2) {
445 		aprint_error_dev(self, "_GPE package does not contain 2 elements\n");
446 		AcpiOsFree(p);
447 		return false;
448 	}
449 
450 	c = &p->Package.Elements[0];
451 	switch (c->Type) {
452 	case ACPI_TYPE_LOCAL_REFERENCE:
453 	case ACPI_TYPE_ANY:
454 		*gpe_handle = c->Reference.Handle;
455 		break;
456 	case ACPI_TYPE_STRING:
457 		/* XXX should be using real scope here */
458 		rv = AcpiGetHandle(NULL, p->String.Pointer, gpe_handle);
459 		if (rv != AE_OK) {
460 			aprint_error_dev(self,
461 			    "_GPE device reference unresolvable\n");
462 			AcpiOsFree(p);
463 			return false;
464 		}
465 		break;
466 	default:
467 		aprint_error_dev(self, "_GPE device reference incorrect\n");
468 		AcpiOsFree(p);
469 		return false;
470 	}
471 	c = &p->Package.Elements[1];
472 	if (c->Type != ACPI_TYPE_INTEGER) {
473 		aprint_error_dev(self,
474 		    "_GPE package needs integer as 2nd field\n");
475 		AcpiOsFree(p);
476 		return false;
477 	}
478 	*gpebit = c->Integer.Value;
479 	AcpiOsFree(p);
480 	return true;
481 }
482 
483 static uint8_t
484 acpiec_read_data(struct acpiec_softc *sc)
485 {
486 	return bus_space_read_1(sc->sc_data_st, sc->sc_data_sh, 0);
487 }
488 
489 static void
490 acpiec_write_data(struct acpiec_softc *sc, uint8_t val)
491 {
492 	bus_space_write_1(sc->sc_data_st, sc->sc_data_sh, 0, val);
493 }
494 
495 static uint8_t
496 acpiec_read_status(struct acpiec_softc *sc)
497 {
498 	return bus_space_read_1(sc->sc_csr_st, sc->sc_csr_sh, 0);
499 }
500 
501 static void
502 acpiec_write_command(struct acpiec_softc *sc, uint8_t cmd)
503 {
504 	bus_space_write_1(sc->sc_csr_st, sc->sc_csr_sh, 0, cmd);
505 }
506 
507 static ACPI_STATUS
508 acpiec_space_setup(ACPI_HANDLE region, UINT32 func, void *arg,
509     void **region_arg)
510 {
511 	if (func == ACPI_REGION_DEACTIVATE)
512 		*region_arg = NULL;
513 	else
514 		*region_arg = arg;
515 
516 	return AE_OK;
517 }
518 
519 static void
520 acpiec_lock(device_t dv)
521 {
522 	struct acpiec_softc *sc = device_private(dv);
523 	ACPI_STATUS rv;
524 
525 	mutex_enter(&sc->sc_access_mtx);
526 
527 	if (sc->sc_need_global_lock) {
528 		rv = AcpiAcquireGlobalLock(EC_LOCK_TIMEOUT, &sc->sc_global_lock);
529 		if (rv != AE_OK) {
530 			aprint_error_dev(dv, "failed to acquire global lock: %s\n",
531 			    AcpiFormatException(rv));
532 			return;
533 		}
534 	}
535 }
536 
537 static void
538 acpiec_unlock(device_t dv)
539 {
540 	struct acpiec_softc *sc = device_private(dv);
541 	ACPI_STATUS rv;
542 
543 	if (sc->sc_need_global_lock) {
544 		rv = AcpiReleaseGlobalLock(sc->sc_global_lock);
545 		if (rv != AE_OK) {
546 			aprint_error_dev(dv, "failed to release global lock: %s\n",
547 			    AcpiFormatException(rv));
548 		}
549 	}
550 	mutex_exit(&sc->sc_access_mtx);
551 }
552 
553 static ACPI_STATUS
554 acpiec_read(device_t dv, uint8_t addr, uint8_t *val)
555 {
556 	struct acpiec_softc *sc = device_private(dv);
557 	int i, timeo = 1000 * EC_CMD_TIMEOUT;
558 
559 	acpiec_lock(dv);
560 	mutex_enter(&sc->sc_mtx);
561 
562 	sc->sc_cur_addr = addr;
563 	sc->sc_state = EC_STATE_READ;
564 
565 	for (i = 0; i < EC_POLL_TIMEOUT; ++i) {
566 		acpiec_gpe_state_machine(dv);
567 		if (sc->sc_state == EC_STATE_FREE)
568 			goto done;
569 		delay(1);
570 	}
571 
572 	if (cold || acpiec_cold) {
573 		while (sc->sc_state != EC_STATE_FREE && timeo-- > 0) {
574 			delay(1000);
575 			acpiec_gpe_state_machine(dv);
576 		}
577 		if (sc->sc_state != EC_STATE_FREE) {
578 			mutex_exit(&sc->sc_mtx);
579 			AcpiClearGpe(sc->sc_gpeh, sc->sc_gpebit, ACPI_NOT_ISR);
580 			acpiec_unlock(dv);
581 			aprint_error_dev(dv, "command timed out, state %d\n",
582 			    sc->sc_state);
583 			return AE_ERROR;
584 		}
585 	} else if (cv_timedwait(&sc->sc_cv, &sc->sc_mtx, EC_CMD_TIMEOUT * hz)) {
586 		mutex_exit(&sc->sc_mtx);
587 		AcpiClearGpe(sc->sc_gpeh, sc->sc_gpebit, ACPI_NOT_ISR);
588 		acpiec_unlock(dv);
589 		aprint_error_dev(dv, "command takes over %d sec...\n", EC_CMD_TIMEOUT);
590 		return AE_ERROR;
591 	}
592 
593 done:
594 	*val = sc->sc_cur_val;
595 
596 	mutex_exit(&sc->sc_mtx);
597 	acpiec_unlock(dv);
598 	return AE_OK;
599 }
600 
601 static ACPI_STATUS
602 acpiec_write(device_t dv, uint8_t addr, uint8_t val)
603 {
604 	struct acpiec_softc *sc = device_private(dv);
605 	int i, timeo = 1000 * EC_CMD_TIMEOUT;
606 
607 	acpiec_lock(dv);
608 	mutex_enter(&sc->sc_mtx);
609 
610 	sc->sc_cur_addr = addr;
611 	sc->sc_cur_val = val;
612 	sc->sc_state = EC_STATE_WRITE;
613 
614 	for (i = 0; i < EC_POLL_TIMEOUT; ++i) {
615 		acpiec_gpe_state_machine(dv);
616 		if (sc->sc_state == EC_STATE_FREE)
617 			goto done;
618 		delay(1);
619 	}
620 
621 	if (cold || acpiec_cold) {
622 		while (sc->sc_state != EC_STATE_FREE && timeo-- > 0) {
623 			delay(1000);
624 			acpiec_gpe_state_machine(dv);
625 		}
626 		if (sc->sc_state != EC_STATE_FREE) {
627 			mutex_exit(&sc->sc_mtx);
628 			AcpiClearGpe(sc->sc_gpeh, sc->sc_gpebit, ACPI_NOT_ISR);
629 			acpiec_unlock(dv);
630 			aprint_error_dev(dv, "command timed out, state %d\n",
631 			    sc->sc_state);
632 			return AE_ERROR;
633 		}
634 	} else if (cv_timedwait(&sc->sc_cv, &sc->sc_mtx, EC_CMD_TIMEOUT * hz)) {
635 		mutex_exit(&sc->sc_mtx);
636 		AcpiClearGpe(sc->sc_gpeh, sc->sc_gpebit, ACPI_NOT_ISR);
637 		acpiec_unlock(dv);
638 		aprint_error_dev(dv, "command takes over %d sec...\n", EC_CMD_TIMEOUT);
639 		return AE_ERROR;
640 	}
641 
642 done:
643 	mutex_exit(&sc->sc_mtx);
644 	acpiec_unlock(dv);
645 	return AE_OK;
646 }
647 
648 static ACPI_STATUS
649 acpiec_space_handler(UINT32 func, ACPI_PHYSICAL_ADDRESS paddr,
650     UINT32 width, ACPI_INTEGER *value, void *arg, void *region_arg)
651 {
652 	device_t dv;
653 	struct acpiec_softc *sc;
654 	ACPI_STATUS rv;
655 	uint8_t addr, reg;
656 	unsigned int i;
657 
658 	if (paddr > 0xff || width % 8 != 0 || value == NULL || arg == NULL ||
659 	    paddr + width / 8 > 0xff)
660 		return AE_BAD_PARAMETER;
661 
662 	addr = paddr;
663 	dv = arg;
664 	sc = device_private(dv);
665 
666 	rv = AE_OK;
667 
668 	switch (func) {
669 	case ACPI_READ:
670 		*value = 0;
671 		for (i = 0; i < width; i += 8, ++addr) {
672 			rv = acpiec_read(dv, addr, &reg);
673 			if (rv != AE_OK)
674 				break;
675 			*value |= (ACPI_INTEGER)reg << i;
676 		}
677 		break;
678 	case ACPI_WRITE:
679 		for (i = 0; i < width; i += 8, ++addr) {
680 			reg = (*value >>i) & 0xff;
681 			rv = acpiec_write(dv, addr, reg);
682 			if (rv != AE_OK)
683 				break;
684 		}
685 		break;
686 	default:
687 		aprint_error("%s: invalid Address Space function called: %x\n",
688 		    device_xname(dv), (unsigned int)func);
689 		return AE_BAD_PARAMETER;
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("%s: GPE query method %s failed: %s",
744 		    device_xname(dv), 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 	AcpiClearGpe(sc->sc_gpeh, sc->sc_gpebit, ACPI_NOT_ISR);
847 
848 	mutex_enter(&sc->sc_mtx);
849 	acpiec_gpe_state_machine(dv);
850 	mutex_exit(&sc->sc_mtx);
851 }
852 
853 static UINT32
854 acpiec_gpe_handler(void *arg)
855 {
856 	device_t dv = arg;
857 	struct acpiec_softc *sc = device_private(dv);
858 
859 	AcpiClearGpe(sc->sc_gpeh, sc->sc_gpebit, ACPI_ISR);
860 
861 	mutex_enter(&sc->sc_mtx);
862 	acpiec_gpe_state_machine(dv);
863 	mutex_exit(&sc->sc_mtx);
864 
865 	return 0;
866 }
867 
868 ACPI_STATUS
869 acpiec_bus_read(device_t dv, u_int addr, ACPI_INTEGER *val, int width)
870 {
871 	return acpiec_space_handler(ACPI_READ, addr, width * 8, val, dv, NULL);
872 }
873 
874 ACPI_STATUS
875 acpiec_bus_write(device_t dv, u_int addr, ACPI_INTEGER val, int width)
876 {
877 	return acpiec_space_handler(ACPI_WRITE, addr, width * 8, &val, dv, NULL);
878 }
879 
880 ACPI_HANDLE
881 acpiec_get_handle(device_t dv)
882 {
883 	struct acpiec_softc *sc = device_private(dv);
884 
885 	return sc->sc_ech;
886 }
887