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