Home | History | Annotate | Line # | Download | only in acpi
acpi_ec.c revision 1.47
      1 /*	$NetBSD: acpi_ec.c,v 1.47 2007/12/19 20:48:56 joerg Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2007 Joerg Sonnenberger <joerg (at) 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.47 2007/12/19 20:48:56 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 
     76 /* Maximum time to wait for global ACPI lock in ms */
     77 #define	EC_LOCK_TIMEOUT		5
     78 
     79 /* Maximum time to poll for completion of a command  in ms */
     80 #define	EC_POLL_TIMEOUT		5
     81 
     82 /* Maximum time to give a single EC command in s */
     83 #define EC_CMD_TIMEOUT		10
     84 
     85 /* From ACPI 3.0b, chapter 12.3 */
     86 #define EC_COMMAND_READ		0x80
     87 #define	EC_COMMAND_WRITE	0x81
     88 #define	EC_COMMAND_BURST_EN	0x82
     89 #define	EC_COMMAND_BURST_DIS	0x83
     90 #define	EC_COMMAND_QUERY	0x84
     91 
     92 /* From ACPI 3.0b, chapter 12.2.1 */
     93 #define	EC_STATUS_OBF		0x01
     94 #define	EC_STATUS_IBF		0x02
     95 #define	EC_STATUS_CMD		0x08
     96 #define	EC_STATUS_BURST		0x10
     97 #define	EC_STATUS_SCI		0x20
     98 #define	EC_STATUS_SMI		0x40
     99 
    100 static const char *ec_hid[] = {
    101 	"PNP0C09",
    102 	NULL,
    103 };
    104 
    105 enum ec_state_t {
    106 	EC_STATE_QUERY,
    107 	EC_STATE_QUERY_VAL,
    108 	EC_STATE_READ,
    109 	EC_STATE_READ_ADDR,
    110 	EC_STATE_READ_VAL,
    111 	EC_STATE_WRITE,
    112 	EC_STATE_WRITE_ADDR,
    113 	EC_STATE_WRITE_VAL,
    114 	EC_STATE_FREE
    115 };
    116 
    117 struct acpiec_softc {
    118 	ACPI_HANDLE sc_ech;
    119 
    120 	ACPI_HANDLE sc_gpeh;
    121 	UINT8 sc_gpebit;
    122 
    123 	bus_space_tag_t sc_data_st;
    124 	bus_space_handle_t sc_data_sh;
    125 
    126 	bus_space_tag_t sc_csr_st;
    127 	bus_space_handle_t sc_csr_sh;
    128 
    129 	bool sc_need_global_lock;
    130 	UINT32 sc_global_lock;
    131 
    132 	kmutex_t sc_mtx, sc_access_mtx;
    133 	kcondvar_t sc_cv, sc_cv_sci;
    134 	enum ec_state_t sc_state;
    135 	bool sc_got_sci;
    136 	callout_t sc_pseudo_intr;
    137 
    138 	uint8_t sc_cur_addr, sc_cur_val;
    139 };
    140 
    141 static int acpiecdt_match(device_t, struct cfdata *, void *);
    142 static void acpiecdt_attach(device_t, device_t, void *);
    143 
    144 static int acpiec_match(device_t, struct cfdata *, void *);
    145 static void acpiec_attach(device_t, device_t, void *);
    146 
    147 static void acpiec_common_attach(device_t, device_t, ACPI_HANDLE,
    148     bus_addr_t, bus_addr_t, ACPI_HANDLE, uint8_t);
    149 
    150 static bool acpiec_resume(device_t);
    151 static bool acpiec_suspend(device_t);
    152 
    153 static bool acpiec_parse_gpe_package(device_t, ACPI_HANDLE,
    154     ACPI_HANDLE *, uint8_t *);
    155 
    156 static void acpiec_callout(void *);
    157 static void acpiec_gpe_query(void *);
    158 static UINT32 acpiec_gpe_handler(void *);
    159 static ACPI_STATUS acpiec_space_setup(ACPI_HANDLE, UINT32, void *, void **);
    160 static ACPI_STATUS acpiec_space_handler(UINT32, ACPI_PHYSICAL_ADDRESS,
    161     UINT32, ACPI_INTEGER *, void *, void *);
    162 
    163 static void acpiec_gpe_state_machine(device_t);
    164 
    165 CFATTACH_DECL_NEW(acpiec, sizeof(struct acpiec_softc),
    166     acpiec_match, acpiec_attach, NULL, NULL);
    167 
    168 CFATTACH_DECL_NEW(acpiecdt, sizeof(struct acpiec_softc),
    169     acpiecdt_match, acpiecdt_attach, NULL, NULL);
    170 
    171 static device_t ec_singleton = NULL;
    172 static bool acpiec_cold = false;
    173 
    174 static bool
    175 acpiecdt_find(device_t parent, ACPI_HANDLE *ec_handle,
    176     bus_addr_t *cmd_reg, bus_addr_t *data_reg, uint8_t *gpebit)
    177 {
    178 	ACPI_TABLE_ECDT *ecdt;
    179 	ACPI_STATUS rv;
    180 
    181 	rv = AcpiGetTable(ACPI_SIG_ECDT, 1, (ACPI_TABLE_HEADER **)&ecdt);
    182 	if (ACPI_FAILURE(rv))
    183 		return false;
    184 
    185 	if (ecdt->Control.BitWidth != 8 || ecdt->Data.BitWidth != 8) {
    186 		aprint_error_dev(parent,
    187 		    "ECDT register width invalid (%d/%d)\n",
    188 		    ecdt->Control.BitWidth, ecdt->Data.BitWidth);
    189 		return false;
    190 	}
    191 
    192 	rv = AcpiGetHandle(ACPI_ROOT_OBJECT, ecdt->Id, ec_handle);
    193 	if (ACPI_FAILURE(rv)) {
    194 		aprint_error_dev(parent,
    195 		    "failed to look up EC object %s: %s\n",
    196 		    ecdt->Id, AcpiFormatException(rv));
    197 		return false;
    198 	}
    199 
    200 	*cmd_reg = ecdt->Control.Address;
    201 	*data_reg = ecdt->Data.Address;
    202 	*gpebit = ecdt->Gpe;
    203 
    204 	return true;
    205 }
    206 
    207 static int
    208 acpiecdt_match(device_t parent, struct cfdata *match, void *aux)
    209 {
    210 	ACPI_HANDLE ec_handle;
    211 	bus_addr_t cmd_reg, data_reg;
    212 	uint8_t gpebit;
    213 
    214 	if (acpiecdt_find(parent, &ec_handle, &cmd_reg, &data_reg, &gpebit))
    215 		return 1;
    216 	else
    217 		return 0;
    218 }
    219 
    220 static void
    221 acpiecdt_attach(device_t parent, device_t self, void *aux)
    222 {
    223 	ACPI_HANDLE ec_handle;
    224 	bus_addr_t cmd_reg, data_reg;
    225 	uint8_t gpebit;
    226 
    227 	if (!acpiecdt_find(parent, &ec_handle, &cmd_reg, &data_reg, &gpebit))
    228 		panic("ECDT disappeared");
    229 
    230 	aprint_naive(": ACPI Embedded Controller via ECDT\n");
    231 	aprint_normal(": ACPI Embedded Controller via ECDT\n");
    232 
    233 	acpiec_common_attach(parent, self, ec_handle, cmd_reg, data_reg,
    234 	    NULL, gpebit);
    235 }
    236 
    237 static int
    238 acpiec_match(device_t parent, struct cfdata *match, void *aux)
    239 {
    240 	struct acpi_attach_args *aa = aux;
    241 
    242 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
    243 		return 0;
    244 
    245 	return acpi_match_hid(aa->aa_node->ad_devinfo, ec_hid);
    246 }
    247 
    248 static void
    249 acpiec_attach(device_t parent, device_t self, void *aux)
    250 {
    251 	struct acpi_attach_args *aa = aux;
    252 	struct acpi_resources ec_res;
    253 	struct acpi_io *io0, *io1;
    254 	ACPI_HANDLE gpe_handle;
    255 	uint8_t gpebit;
    256 	ACPI_STATUS rv;
    257 
    258 	if (ec_singleton != NULL) {
    259 		aprint_naive(": ACPI Embedded Controller (disabled)\n");
    260 		aprint_normal(": ACPI Embedded Controller (disabled)\n");
    261 		if (!pmf_device_register(self, NULL, NULL))
    262 			aprint_error_dev(self, "couldn't establish power handler\n");
    263 		return;
    264 	}
    265 
    266 	aprint_naive(": ACPI Embedded Controller\n");
    267 	aprint_normal(": ACPI Embedded Controller\n");
    268 
    269 	if (!acpiec_parse_gpe_package(self, aa->aa_node->ad_handle,
    270 				      &gpe_handle, &gpebit))
    271 		return;
    272 
    273 	rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
    274 	    &ec_res, &acpi_resource_parse_ops_default);
    275 	if (rv != AE_OK) {
    276 		aprint_error_dev(self, "resource parsing failed: %s\n",
    277 		    AcpiFormatException(rv));
    278 		return;
    279 	}
    280 
    281 	if ((io0 = acpi_res_io(&ec_res, 0)) == NULL) {
    282 		aprint_error_dev(self, "no data register resource\n");
    283 		goto free_res;
    284 	}
    285 	if ((io1 = acpi_res_io(&ec_res, 1)) == NULL) {
    286 		aprint_error_dev(self, "no CSR register resource\n");
    287 		goto free_res;
    288 	}
    289 
    290 	acpiec_common_attach(parent, self, aa->aa_node->ad_handle,
    291 	    io1->ar_base, io0->ar_base, gpe_handle, gpebit);
    292 
    293 free_res:
    294 	acpi_resource_cleanup(&ec_res);
    295 }
    296 
    297 static void
    298 acpiec_common_attach(device_t parent, device_t self,
    299     ACPI_HANDLE ec_handle, bus_addr_t cmd_reg, bus_addr_t data_reg,
    300     ACPI_HANDLE gpe_handle, uint8_t gpebit)
    301 {
    302 	struct acpiec_softc *sc = device_private(self);
    303 	ACPI_STATUS rv;
    304 	ACPI_INTEGER val;
    305 
    306 	sc->sc_ech = ec_handle;
    307 	sc->sc_gpeh = gpe_handle;
    308 	sc->sc_gpebit = gpebit;
    309 
    310 	sc->sc_state = EC_STATE_FREE;
    311 	mutex_init(&sc->sc_mtx, MUTEX_DRIVER, IPL_TTY);
    312 	mutex_init(&sc->sc_access_mtx, MUTEX_DEFAULT, IPL_NONE);
    313 	cv_init(&sc->sc_cv, "eccv");
    314 	cv_init(&sc->sc_cv_sci, "ecsci");
    315 
    316 	if (bus_space_map(sc->sc_data_st, data_reg, 1, 0,
    317 	    &sc->sc_data_sh) != 0) {
    318 		aprint_error_dev(self, "unable to map data register\n");
    319 		return;
    320 	}
    321 
    322 	if (bus_space_map(sc->sc_csr_st, cmd_reg, 1, 0, &sc->sc_csr_sh) != 0) {
    323 		aprint_error_dev(self, "unable to map CSR register\n");
    324 		goto post_data_map;
    325 	}
    326 
    327 	rv = acpi_eval_integer(sc->sc_ech, "_GLK", &val);
    328 	if (rv == AE_OK) {
    329 		sc->sc_need_global_lock = val != 0;
    330 	} else if (rv != AE_NOT_FOUND) {
    331 		aprint_error_dev(self, "unable to evaluate _GLK: %s\n",
    332 		    AcpiFormatException(rv));
    333 		goto post_csr_map;
    334 	} else {
    335 		sc->sc_need_global_lock = false;
    336 	}
    337 	if (sc->sc_need_global_lock)
    338 		aprint_normal_dev(self, "using global ACPI lock\n");
    339 
    340 	callout_init(&sc->sc_pseudo_intr, CALLOUT_MPSAFE);
    341 	callout_setfunc(&sc->sc_pseudo_intr, acpiec_callout, self);
    342 
    343 	rv = AcpiInstallAddressSpaceHandler(sc->sc_ech, ACPI_ADR_SPACE_EC,
    344 	    acpiec_space_handler, acpiec_space_setup, self);
    345 	if (rv != AE_OK) {
    346 		aprint_error_dev(self,
    347 		    "unable to install address space handler: %s\n",
    348 		    AcpiFormatException(rv));
    349 		goto post_csr_map;
    350 	}
    351 
    352 	rv = AcpiInstallGpeHandler(sc->sc_gpeh, sc->sc_gpebit,
    353 	    ACPI_GPE_EDGE_TRIGGERED, acpiec_gpe_handler, self);
    354 	if (rv != AE_OK) {
    355 		aprint_error_dev(self, "unable to install GPE handler: %s\n",
    356 		    AcpiFormatException(rv));
    357 		goto post_csr_map;
    358 	}
    359 
    360 	rv = AcpiSetGpeType(sc->sc_gpeh, sc->sc_gpebit, ACPI_GPE_TYPE_RUNTIME);
    361 	if (rv != AE_OK) {
    362 		aprint_error_dev(self, "unable to set GPE type: %s\n",
    363 		    AcpiFormatException(rv));
    364 		goto post_csr_map;
    365 	}
    366 
    367 	rv = AcpiEnableGpe(sc->sc_gpeh, sc->sc_gpebit, ACPI_ISR);
    368 	if (rv != AE_OK) {
    369 		aprint_error_dev(self, "unable to enable GPE: %s\n",
    370 		    AcpiFormatException(rv));
    371 		goto post_csr_map;
    372 	}
    373 
    374 	if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL, acpiec_gpe_query,
    375 		           self, NULL, "acpiec sci thread")) {
    376 		aprint_error_dev(self, "unable to create query kthread\n");
    377 		goto post_csr_map;
    378 	}
    379 
    380 	ec_singleton = self;
    381 
    382 	if (!pmf_device_register(self, acpiec_suspend, acpiec_resume))
    383 		aprint_error_dev(self, "couldn't establish power handler\n");
    384 
    385 	return;
    386 
    387 post_csr_map:
    388 	(void)AcpiRemoveGpeHandler(sc->sc_gpeh, sc->sc_gpebit,
    389 	    acpiec_gpe_handler);
    390 	(void)AcpiRemoveAddressSpaceHandler(sc->sc_ech,
    391 	    ACPI_ADR_SPACE_EC, acpiec_space_handler);
    392 	bus_space_unmap(sc->sc_csr_st, sc->sc_csr_sh, 1);
    393 post_data_map:
    394 	bus_space_unmap(sc->sc_data_st, sc->sc_data_sh, 1);
    395 }
    396 
    397 static bool
    398 acpiec_suspend(device_t dv)
    399 {
    400 	acpiec_cold = true;
    401 
    402 	return true;
    403 }
    404 
    405 static bool
    406 acpiec_resume(device_t dv)
    407 {
    408 	acpiec_cold = false;
    409 
    410 	return true;
    411 }
    412 
    413 static bool
    414 acpiec_parse_gpe_package(device_t self, ACPI_HANDLE ec_handle,
    415     ACPI_HANDLE *gpe_handle, uint8_t *gpebit)
    416 {
    417 	ACPI_BUFFER buf;
    418 	ACPI_OBJECT *p, *c;
    419 	ACPI_STATUS rv;
    420 
    421 	rv = acpi_eval_struct(ec_handle, "_GPE", &buf);
    422 	if (rv != AE_OK) {
    423 		aprint_error_dev(self, "unable to evaluate _GPE: %s\n",
    424 		    AcpiFormatException(rv));
    425 		return false;
    426 	}
    427 
    428 	p = buf.Pointer;
    429 
    430 	if (p->Type == ACPI_TYPE_INTEGER) {
    431 		*gpe_handle = NULL;
    432 		*gpebit = p->Integer.Value;
    433 		AcpiOsFree(p);
    434 		return true;
    435 	}
    436 
    437 	if (p->Type != ACPI_TYPE_PACKAGE) {
    438 		aprint_error_dev(self, "_GPE is neither integer nor package\n");
    439 		AcpiOsFree(p);
    440 		return false;
    441 	}
    442 
    443 	if (p->Package.Count != 2) {
    444 		aprint_error_dev(self, "_GPE package does not contain 2 elements\n");
    445 		AcpiOsFree(p);
    446 		return false;
    447 	}
    448 
    449 	c = &p->Package.Elements[0];
    450 	switch (c->Type) {
    451 	case ACPI_TYPE_LOCAL_REFERENCE:
    452 	case ACPI_TYPE_ANY:
    453 		*gpe_handle = c->Reference.Handle;
    454 		break;
    455 	case ACPI_TYPE_STRING:
    456 		/* XXX should be using real scope here */
    457 		rv = AcpiGetHandle(NULL, p->String.Pointer, gpe_handle);
    458 		if (rv != AE_OK) {
    459 			aprint_error_dev(self,
    460 			    "_GPE device reference unresolvable\n");
    461 			AcpiOsFree(p);
    462 			return false;
    463 		}
    464 		break;
    465 	default:
    466 		aprint_error_dev(self, "_GPE device reference incorrect\n");
    467 		AcpiOsFree(p);
    468 		return false;
    469 	}
    470 	c = &p->Package.Elements[1];
    471 	if (c->Type != ACPI_TYPE_INTEGER) {
    472 		aprint_error_dev(self,
    473 		    "_GPE package needs integer as 2nd field\n");
    474 		AcpiOsFree(p);
    475 		return false;
    476 	}
    477 	*gpebit = c->Integer.Value;
    478 	AcpiOsFree(p);
    479 	return true;
    480 }
    481 
    482 static uint8_t
    483 acpiec_read_data(struct acpiec_softc *sc)
    484 {
    485 	return bus_space_read_1(sc->sc_data_st, sc->sc_data_sh, 0);
    486 }
    487 
    488 static void
    489 acpiec_write_data(struct acpiec_softc *sc, uint8_t val)
    490 {
    491 	bus_space_write_1(sc->sc_data_st, sc->sc_data_sh, 0, val);
    492 }
    493 
    494 static uint8_t
    495 acpiec_read_status(struct acpiec_softc *sc)
    496 {
    497 	return bus_space_read_1(sc->sc_csr_st, sc->sc_csr_sh, 0);
    498 }
    499 
    500 static void
    501 acpiec_write_command(struct acpiec_softc *sc, uint8_t cmd)
    502 {
    503 	bus_space_write_1(sc->sc_csr_st, sc->sc_csr_sh, 0, cmd);
    504 }
    505 
    506 static ACPI_STATUS
    507 acpiec_space_setup(ACPI_HANDLE region, UINT32 func, void *arg,
    508     void **region_arg)
    509 {
    510 	if (func == ACPI_REGION_DEACTIVATE)
    511 		*region_arg = NULL;
    512 	else
    513 		*region_arg = arg;
    514 
    515 	return AE_OK;
    516 }
    517 
    518 static void
    519 acpiec_lock(device_t dv)
    520 {
    521 	struct acpiec_softc *sc = device_private(dv);
    522 	ACPI_STATUS rv;
    523 
    524 	mutex_enter(&sc->sc_access_mtx);
    525 
    526 	if (sc->sc_need_global_lock) {
    527 		rv = AcpiAcquireGlobalLock(EC_LOCK_TIMEOUT, &sc->sc_global_lock);
    528 		if (rv != AE_OK) {
    529 			aprint_error_dev(dv, "failed to acquire global lock: %s\n",
    530 			    AcpiFormatException(rv));
    531 			return;
    532 		}
    533 	}
    534 }
    535 
    536 static void
    537 acpiec_unlock(device_t dv)
    538 {
    539 	struct acpiec_softc *sc = device_private(dv);
    540 	ACPI_STATUS rv;
    541 
    542 	if (sc->sc_need_global_lock) {
    543 		rv = AcpiReleaseGlobalLock(sc->sc_global_lock);
    544 		if (rv != AE_OK) {
    545 			aprint_error_dev(dv, "failed to release global lock: %s\n",
    546 			    AcpiFormatException(rv));
    547 		}
    548 	}
    549 	mutex_exit(&sc->sc_access_mtx);
    550 }
    551 
    552 static ACPI_STATUS
    553 acpiec_read(device_t dv, uint8_t addr, uint8_t *val)
    554 {
    555 	struct acpiec_softc *sc = device_private(dv);
    556 	int i;
    557 
    558 	acpiec_lock(dv);
    559 	mutex_enter(&sc->sc_mtx);
    560 
    561 	sc->sc_cur_addr = addr;
    562 	sc->sc_state = EC_STATE_READ;
    563 
    564 	for (i = 0; i < EC_POLL_TIMEOUT; ++i) {
    565 		acpiec_gpe_state_machine(dv);
    566 		if (sc->sc_state == EC_STATE_FREE)
    567 			goto done;
    568 		delay(1);
    569 	}
    570 
    571 	if (cold || acpiec_cold) {
    572 		while (sc->sc_state != EC_STATE_FREE) {
    573 			delay(1);
    574 			acpiec_gpe_state_machine(dv);
    575 		}
    576 	} else while (cv_timedwait(&sc->sc_cv, &sc->sc_mtx, EC_CMD_TIMEOUT * hz)) {
    577 		mutex_exit(&sc->sc_mtx);
    578 		AcpiClearGpe(sc->sc_gpeh, sc->sc_gpebit, ACPI_NOT_ISR);
    579 		acpiec_unlock(dv);
    580 		aprint_error_dev(dv, "command takes over %d sec...\n", EC_CMD_TIMEOUT);
    581 		return AE_ERROR;
    582 	}
    583 
    584 done:
    585 	*val = sc->sc_cur_val;
    586 
    587 	mutex_exit(&sc->sc_mtx);
    588 	acpiec_unlock(dv);
    589 	return AE_OK;
    590 }
    591 
    592 static ACPI_STATUS
    593 acpiec_write(device_t dv, uint8_t addr, uint8_t val)
    594 {
    595 	struct acpiec_softc *sc = device_private(dv);
    596 	int i;
    597 
    598 	acpiec_lock(dv);
    599 	mutex_enter(&sc->sc_mtx);
    600 
    601 	sc->sc_cur_addr = addr;
    602 	sc->sc_cur_val = val;
    603 	sc->sc_state = EC_STATE_WRITE;
    604 
    605 	for (i = 0; i < EC_POLL_TIMEOUT; ++i) {
    606 		acpiec_gpe_state_machine(dv);
    607 		if (sc->sc_state == EC_STATE_FREE)
    608 			goto done;
    609 		delay(1);
    610 	}
    611 
    612 	if (cold || acpiec_cold) {
    613 		while (sc->sc_state != EC_STATE_FREE) {
    614 			delay(1);
    615 			acpiec_gpe_state_machine(dv);
    616 		}
    617 	} else while (cv_timedwait(&sc->sc_cv, &sc->sc_mtx, EC_CMD_TIMEOUT * hz)) {
    618 		mutex_exit(&sc->sc_mtx);
    619 		AcpiClearGpe(sc->sc_gpeh, sc->sc_gpebit, ACPI_NOT_ISR);
    620 		acpiec_unlock(dv);
    621 		aprint_error_dev(dv, "command takes over %d sec...\n", EC_CMD_TIMEOUT);
    622 		return AE_ERROR;
    623 	}
    624 
    625 done:
    626 	mutex_exit(&sc->sc_mtx);
    627 	acpiec_unlock(dv);
    628 	return AE_OK;
    629 }
    630 
    631 static ACPI_STATUS
    632 acpiec_space_handler(UINT32 func, ACPI_PHYSICAL_ADDRESS paddr,
    633     UINT32 width, ACPI_INTEGER *value, void *arg, void *region_arg)
    634 {
    635 	device_t dv;
    636 	struct acpiec_softc *sc;
    637 	ACPI_STATUS rv;
    638 	uint8_t addr, reg;
    639 	unsigned int i;
    640 
    641 	if (paddr > 0xff || width % 8 != 0 || value == NULL || arg == NULL ||
    642 	    paddr + width / 8 > 0xff)
    643 		return AE_BAD_PARAMETER;
    644 
    645 	addr = paddr;
    646 	dv = arg;
    647 	sc = device_private(dv);
    648 
    649 	rv = AE_OK;
    650 
    651 	switch (func) {
    652 	case ACPI_READ:
    653 		*value = 0;
    654 		for (i = 0; i < width; i += 8, ++addr) {
    655 			rv = acpiec_read(dv, addr, &reg);
    656 			if (rv != AE_OK)
    657 				break;
    658 			*value |= (ACPI_INTEGER)reg << i;
    659 		}
    660 		break;
    661 	case ACPI_WRITE:
    662 		for (i = 0; i < width; i += 8, ++addr) {
    663 			reg = (*value >>i) & 0xff;
    664 			rv = acpiec_write(dv, addr, reg);
    665 			if (rv != AE_OK)
    666 				break;
    667 		}
    668 		break;
    669 	default:
    670 		aprint_error("%s: invalid Address Space function called: %x\n",
    671 		    device_xname(dv), (unsigned int)func);
    672 		return AE_BAD_PARAMETER;
    673 	}
    674 
    675 	return rv;
    676 }
    677 
    678 static void
    679 acpiec_gpe_query(void *arg)
    680 {
    681 	device_t dv = arg;
    682 	struct acpiec_softc *sc = device_private(dv);
    683 	uint8_t reg;
    684 	char qxx[5];
    685 	ACPI_STATUS rv;
    686 	int i;
    687 
    688 loop:
    689 	mutex_enter(&sc->sc_mtx);
    690 
    691 	if (sc->sc_got_sci == false)
    692 		cv_wait(&sc->sc_cv_sci, &sc->sc_mtx);
    693 	mutex_exit(&sc->sc_mtx);
    694 
    695 	acpiec_lock(dv);
    696 	mutex_enter(&sc->sc_mtx);
    697 
    698 	/* The Query command can always be issued, so be defensive here. */
    699 	sc->sc_got_sci = false;
    700 	sc->sc_state = EC_STATE_QUERY;
    701 
    702 	for (i = 0; i < EC_POLL_TIMEOUT; ++i) {
    703 		acpiec_gpe_state_machine(dv);
    704 		if (sc->sc_state == EC_STATE_FREE)
    705 			goto done;
    706 		delay(1);
    707 	}
    708 
    709 	cv_wait(&sc->sc_cv, &sc->sc_mtx);
    710 
    711 done:
    712 	reg = sc->sc_cur_val;
    713 
    714 	mutex_exit(&sc->sc_mtx);
    715 	acpiec_unlock(dv);
    716 
    717 	if (reg == 0)
    718 		goto loop; /* Spurious query result */
    719 
    720 	/*
    721 	 * Evaluate _Qxx to respond to the controller.
    722 	 */
    723 	snprintf(qxx, sizeof(qxx), "_Q%02X", (unsigned int)reg);
    724 	rv = AcpiEvaluateObject(sc->sc_ech, qxx, NULL, NULL);
    725 	if (rv != AE_OK && rv != AE_NOT_FOUND) {
    726 		aprint_error("%s: GPE query method %s failed: %s",
    727 		    device_xname(dv), qxx, AcpiFormatException(rv));
    728 	}
    729 
    730 	goto loop;
    731 }
    732 
    733 static void
    734 acpiec_gpe_state_machine(device_t dv)
    735 {
    736 	struct acpiec_softc *sc = device_private(dv);
    737 	uint8_t reg;
    738 
    739 	reg = acpiec_read_status(sc);
    740 
    741 	if (reg & EC_STATUS_SCI)
    742 		sc->sc_got_sci = true;
    743 
    744 	switch (sc->sc_state) {
    745 	case EC_STATE_QUERY:
    746 		if ((reg & EC_STATUS_IBF) != 0)
    747 			break; /* Nothing of interest here. */
    748 		acpiec_write_command(sc, EC_COMMAND_QUERY);
    749 		sc->sc_state = EC_STATE_QUERY_VAL;
    750 		break;
    751 
    752 	case EC_STATE_QUERY_VAL:
    753 		if ((reg & EC_STATUS_OBF) == 0)
    754 			break; /* Nothing of interest here. */
    755 
    756 		sc->sc_cur_val = acpiec_read_data(sc);
    757 		sc->sc_state = EC_STATE_FREE;
    758 
    759 		cv_signal(&sc->sc_cv);
    760 		break;
    761 
    762 	case EC_STATE_READ:
    763 		if ((reg & EC_STATUS_IBF) != 0)
    764 			break; /* Nothing of interest here. */
    765 
    766 		acpiec_write_command(sc, EC_COMMAND_READ);
    767 		sc->sc_state = EC_STATE_READ_ADDR;
    768 		break;
    769 
    770 	case EC_STATE_READ_ADDR:
    771 		if ((reg & EC_STATUS_IBF) != 0)
    772 			break; /* Nothing of interest here. */
    773 
    774 		acpiec_write_data(sc, sc->sc_cur_addr);
    775 		sc->sc_state = EC_STATE_READ_VAL;
    776 		break;
    777 
    778 	case EC_STATE_READ_VAL:
    779 		if ((reg & EC_STATUS_OBF) == 0)
    780 			break; /* Nothing of interest here. */
    781 		sc->sc_cur_val = acpiec_read_data(sc);
    782 		sc->sc_state = EC_STATE_FREE;
    783 
    784 		cv_signal(&sc->sc_cv);
    785 		break;
    786 
    787 	case EC_STATE_WRITE:
    788 		if ((reg & EC_STATUS_IBF) != 0)
    789 			break; /* Nothing of interest here. */
    790 
    791 		acpiec_write_command(sc, EC_COMMAND_WRITE);
    792 		sc->sc_state = EC_STATE_WRITE_ADDR;
    793 		break;
    794 
    795 	case EC_STATE_WRITE_ADDR:
    796 		if ((reg & EC_STATUS_IBF) != 0)
    797 			break; /* Nothing of interest here. */
    798 		acpiec_write_data(sc, sc->sc_cur_addr);
    799 		sc->sc_state = EC_STATE_WRITE_VAL;
    800 		break;
    801 
    802 	case EC_STATE_WRITE_VAL:
    803 		if ((reg & EC_STATUS_IBF) != 0)
    804 			break; /* Nothing of interest here. */
    805 		sc->sc_state = EC_STATE_FREE;
    806 		cv_signal(&sc->sc_cv);
    807 
    808 		acpiec_write_data(sc, sc->sc_cur_val);
    809 		break;
    810 
    811 	case EC_STATE_FREE:
    812 		if (sc->sc_got_sci)
    813 			cv_signal(&sc->sc_cv_sci);
    814 		break;
    815 	default:
    816 		panic("invalid state");
    817 	}
    818 
    819 	if (sc->sc_state != EC_STATE_FREE)
    820 		callout_schedule(&sc->sc_pseudo_intr, 1);
    821 }
    822 
    823 static void
    824 acpiec_callout(void *arg)
    825 {
    826 	device_t dv = arg;
    827 	struct acpiec_softc *sc = device_private(dv);
    828 
    829 	AcpiClearGpe(sc->sc_gpeh, sc->sc_gpebit, ACPI_NOT_ISR);
    830 
    831 	mutex_enter(&sc->sc_mtx);
    832 	acpiec_gpe_state_machine(dv);
    833 	mutex_exit(&sc->sc_mtx);
    834 }
    835 
    836 static UINT32
    837 acpiec_gpe_handler(void *arg)
    838 {
    839 	device_t dv = arg;
    840 	struct acpiec_softc *sc = device_private(dv);
    841 
    842 	AcpiClearGpe(sc->sc_gpeh, sc->sc_gpebit, ACPI_ISR);
    843 
    844 	mutex_enter(&sc->sc_mtx);
    845 	acpiec_gpe_state_machine(dv);
    846 	mutex_exit(&sc->sc_mtx);
    847 
    848 	return 0;
    849 }
    850