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