[BACK]Return to acpi_ec.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / dev / acpi

Annotation of src/sys/dev/acpi/acpi_ec.c, Revision 1.85

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

CVSweb <webmaster@jp.NetBSD.org>