[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.41.8.1

1.41.8.1! matt        1: /*     $NetBSD: acpi_ec.c,v 1.42 2007/10/19 11:59:34 ad Exp $  */
1.1       thorpej     2:
                      3: /*
                      4:  * Copyright 2001 Wasabi Systems, Inc.
                      5:  * All rights reserved.
                      6:  *
                      7:  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
                      8:  *
                      9:  * Redistribution and use in source and binary forms, with or without
                     10:  * modification, are permitted provided that the following conditions
                     11:  * are met:
                     12:  * 1. Redistributions of source code must retain the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer.
                     14:  * 2. Redistributions in binary form must reproduce the above copyright
                     15:  *    notice, this list of conditions and the following disclaimer in the
                     16:  *    documentation and/or other materials provided with the distribution.
                     17:  * 3. All advertising materials mentioning features or use of this software
                     18:  *    must display the following acknowledgement:
                     19:  *     This product includes software developed for the NetBSD Project by
                     20:  *     Wasabi Systems, Inc.
                     21:  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
                     22:  *    or promote products derived from this software without specific prior
                     23:  *    written permission.
                     24:  *
                     25:  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
                     26:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
                     27:  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                     28:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
                     29:  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
                     30:  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
                     31:  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
                     32:  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
                     33:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     34:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
                     35:  * POSSIBILITY OF SUCH DAMAGE.
                     36:  */
                     37:
                     38: /*-
                     39:  * Copyright (c) 2000 Michael Smith
                     40:  * Copyright (c) 2000 BSDi
                     41:  * All rights reserved.
                     42:  *
                     43:  * Redistribution and use in source and binary forms, with or without
                     44:  * modification, are permitted provided that the following conditions
                     45:  * are met:
                     46:  * 1. Redistributions of source code must retain the above copyright
                     47:  *    notice, this list of conditions and the following disclaimer.
                     48:  * 2. Redistributions in binary form must reproduce the above copyright
                     49:  *    notice, this list of conditions and the following disclaimer in the
                     50:  *    documentation and/or other materials provided with the distribution.
                     51:  *
                     52:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
                     53:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     54:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     55:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
                     56:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     57:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     58:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     59:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     60:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     61:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     62:  * SUCH DAMAGE.
                     63:  */
                     64:
                     65: /******************************************************************************
                     66:  *
                     67:  * 1. Copyright Notice
                     68:  *
                     69:  * Some or all of this work - Copyright (c) 1999, Intel Corp.  All rights
                     70:  * reserved.
                     71:  *
                     72:  * 2. License
                     73:  *
                     74:  * 2.1. This is your license from Intel Corp. under its intellectual property
                     75:  * rights.  You may have additional license terms from the party that provided
                     76:  * you this software, covering your right to use that party's intellectual
                     77:  * property rights.
                     78:  *
                     79:  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
                     80:  * copy of the source code appearing in this file ("Covered Code") an
                     81:  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
                     82:  * base code distributed originally by Intel ("Original Intel Code") to copy,
                     83:  * make derivatives, distribute, use and display any portion of the Covered
                     84:  * Code in any form, with the right to sublicense such rights; and
                     85:  *
                     86:  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
                     87:  * license (with the right to sublicense), under only those claims of Intel
                     88:  * patents that are infringed by the Original Intel Code, to make, use, sell,
                     89:  * offer to sell, and import the Covered Code and derivative works thereof
                     90:  * solely to the minimum extent necessary to exercise the above copyright
                     91:  * license, and in no event shall the patent license extend to any additions
                     92:  * to or modifications of the Original Intel Code.  No other license or right
                     93:  * is granted directly or by implication, estoppel or otherwise;
                     94:  *
                     95:  * The above copyright and patent license is granted only if the following
                     96:  * conditions are met:
                     97:  *
1.24      kochi      98:  * 3. Conditions
1.1       thorpej    99:  *
1.24      kochi     100:  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
1.1       thorpej   101:  * Redistribution of source code of any substantial portion of the Covered
                    102:  * Code or modification with rights to further distribute source must include
                    103:  * the above Copyright Notice, the above License, this list of Conditions,
                    104:  * and the following Disclaimer and Export Compliance provision.  In addition,
                    105:  * Licensee must cause all Covered Code to which Licensee contributes to
                    106:  * contain a file documenting the changes Licensee made to create that Covered
                    107:  * Code and the date of any change.  Licensee must include in that file the
1.24      kochi     108:  * documentation of any changes made by any predecessor Licensee.  Licensee
1.1       thorpej   109:  * must include a prominent statement that the modification is derived,
                    110:  * directly or indirectly, from Original Intel Code.
                    111:  *
1.24      kochi     112:  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
1.1       thorpej   113:  * Redistribution of source code of any substantial portion of the Covered
                    114:  * Code or modification without rights to further distribute source must
                    115:  * include the following Disclaimer and Export Compliance provision in the
                    116:  * documentation and/or other materials provided with distribution.  In
                    117:  * addition, Licensee may not authorize further sublicense of source of any
                    118:  * portion of the Covered Code, and must include terms to the effect that the
                    119:  * license from Licensee to its licensee is limited to the intellectual
                    120:  * property embodied in the software Licensee provides to its licensee, and
                    121:  * not to intellectual property embodied in modifications its licensee may
                    122:  * make.
                    123:  *
                    124:  * 3.3. Redistribution of Executable. Redistribution in executable form of any
                    125:  * substantial portion of the Covered Code or modification must reproduce the
                    126:  * above Copyright Notice, and the following Disclaimer and Export Compliance
                    127:  * provision in the documentation and/or other materials provided with the
                    128:  * distribution.
                    129:  *
                    130:  * 3.4. Intel retains all right, title, and interest in and to the Original
                    131:  * Intel Code.
                    132:  *
                    133:  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
                    134:  * Intel shall be used in advertising or otherwise to promote the sale, use or
                    135:  * other dealings in products derived from or relating to the Covered Code
                    136:  * without prior written authorization from Intel.
                    137:  *
                    138:  * 4. Disclaimer and Export Compliance
                    139:  *
                    140:  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
                    141:  * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
                    142:  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
                    143:  * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
                    144:  * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
                    145:  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
1.24      kochi     146:  * PARTICULAR PURPOSE.
1.1       thorpej   147:  *
                    148:  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
                    149:  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
                    150:  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
                    151:  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
                    152:  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
                    153:  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
                    154:  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
                    155:  * LIMITED REMEDY.
                    156:  *
                    157:  * 4.3. Licensee shall not export, either directly or indirectly, any of this
                    158:  * software or system incorporating such software without first obtaining any
                    159:  * required license or other approval from the U. S. Department of Commerce or
                    160:  * any other agency or department of the United States Government.  In the
                    161:  * event Licensee exports any such software from the United States or
                    162:  * re-exports any such software from a foreign destination, Licensee shall
                    163:  * ensure that the distribution and export/re-export of the software is in
                    164:  * compliance with all laws, regulations, orders, or other restrictions of the
                    165:  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
                    166:  * any of its subsidiaries will export/re-export any technical data, process,
                    167:  * software, or service, directly or indirectly, to any country for which the
                    168:  * United States government or any agency thereof requires an export license,
                    169:  * other governmental approval, or letter of assurance, without first obtaining
                    170:  * such license, approval or letter.
                    171:  *
                    172:  *****************************************************************************/
1.3       lukem     173:
                    174: #include <sys/cdefs.h>
1.41.8.1! matt      175: __KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.42 2007/10/19 11:59:34 ad Exp $");
1.1       thorpej   176:
                    177: #include <sys/param.h>
                    178: #include <sys/systm.h>
                    179: #include <sys/device.h>
1.23      kochi     180: #include <sys/malloc.h>
1.1       thorpej   181: #include <sys/proc.h>
                    182: #include <sys/kernel.h>
                    183:
1.41.8.1! matt      184: #include <sys/bus.h>
1.1       thorpej   185:
                    186: #include <dev/acpi/acpica.h>
1.4       thorpej   187: #include <dev/acpi/acpireg.h>
1.1       thorpej   188: #include <dev/acpi/acpivar.h>
                    189: #include <dev/acpi/acpi_ecreg.h>
                    190:
1.23      kochi     191: MALLOC_DECLARE(M_ACPI);
                    192:
1.4       thorpej   193: #define _COMPONENT     ACPI_EC_COMPONENT
                    194: ACPI_MODULE_NAME("EC")
1.1       thorpej   195:
                    196: struct acpi_ec_softc {
                    197:        struct device   sc_dev;         /* base device glue */
1.23      kochi     198:        ACPI_HANDLE sc_handle;          /* ACPI handle */
1.1       thorpej   199:
                    200:        UINT32          sc_gpebit;      /* our GPE interrupt bit */
                    201:
                    202:        bus_space_tag_t sc_data_st;     /* space tag for data register */
                    203:        bus_space_handle_t sc_data_sh;  /* space handle for data register */
                    204:
                    205:        bus_space_tag_t sc_csr_st;      /* space tag for control register */
                    206:        bus_space_handle_t sc_csr_sh;   /* space handle for control register */
                    207:
                    208:        int             sc_flags;       /* see below */
                    209:
                    210:        uint32_t        sc_csrvalue;    /* saved control register */
1.23      kochi     211:        uint32_t        sc_uid;         /* _UID in namespace (ECDT only) */
1.17      mycroft   212:
                    213:        struct lock     sc_lock;        /* serialize operations to this EC */
1.20      yamt      214:        struct simplelock sc_slock;     /* protect against interrupts */
1.17      mycroft   215:        UINT32          sc_glkhandle;   /* global lock handle */
                    216:        UINT32          sc_glk;         /* need global lock? */
1.23      kochi     217: };
1.1       thorpej   218:
1.16      kochi     219: static const char * const ec_hid[] = {
                    220:        "PNP0C09",
                    221:        NULL
                    222: };
                    223:
1.20      yamt      224: #define        EC_F_TRANSACTION        0x01    /* doing transaction */
                    225: #define        EC_F_PENDQUERY          0x02    /* query is pending */
1.1       thorpej   226:
1.17      mycroft   227: /*
                    228:  * how long to wait to acquire global lock.
                    229:  * the value is taken from FreeBSD driver.
                    230:  */
                    231: #define        EC_LOCK_TIMEOUT 1000
                    232:
1.1       thorpej   233: #define        EC_DATA_READ(sc)                                                \
                    234:        bus_space_read_1((sc)->sc_data_st, (sc)->sc_data_sh, 0)
                    235: #define        EC_DATA_WRITE(sc, v)                                            \
                    236:        bus_space_write_1((sc)->sc_data_st, (sc)->sc_data_sh, 0, (v))
                    237:
                    238: #define        EC_CSR_READ(sc)                                                 \
                    239:        bus_space_read_1((sc)->sc_csr_st, (sc)->sc_csr_sh, 0)
                    240: #define        EC_CSR_WRITE(sc, v)                                             \
                    241:        bus_space_write_1((sc)->sc_csr_st, (sc)->sc_csr_sh, 0, (v))
                    242:
1.20      yamt      243: typedef struct {
                    244:        EC_COMMAND      Command;
                    245:        UINT8           Address;
                    246:        UINT8           Data;
                    247: } EC_REQUEST;
                    248:
1.36      kochi     249: static UINT32          EcGpeHandler(void *);
                    250: static ACPI_STATUS     EcSpaceSetup(ACPI_HANDLE, UINT32, void *, void **);
                    251: static ACPI_STATUS     EcSpaceHandler(UINT32, ACPI_PHYSICAL_ADDRESS, UINT32,
                    252:                            ACPI_INTEGER *, void *, void *);
                    253:
                    254: static ACPI_STATUS     EcWaitEvent(struct acpi_ec_softc *, EC_EVENT);
                    255: static ACPI_STATUS     EcQuery(struct acpi_ec_softc *, UINT8 *);
                    256: static ACPI_STATUS     EcTransaction(struct acpi_ec_softc *, EC_REQUEST *);
                    257: static ACPI_STATUS     EcRead(struct acpi_ec_softc *, UINT8, UINT8 *);
                    258: static ACPI_STATUS     EcWrite(struct acpi_ec_softc *, UINT8, UINT8 *);
1.20      yamt      259: static void            EcGpeQueryHandler(void *);
1.35      perry     260: static inline int      EcIsLocked(struct acpi_ec_softc *);
                    261: static inline void     EcLock(struct acpi_ec_softc *);
                    262: static inline void     EcUnlock(struct acpi_ec_softc *);
1.20      yamt      263:
                    264:
1.31      kochi     265: static int     acpiec_match(struct device *, struct cfdata *, void *);
                    266: static void    acpiec_attach(struct device *, struct device *, void *);
1.20      yamt      267:
                    268: CFATTACH_DECL(acpiec, sizeof(struct acpi_ec_softc),
                    269:     acpiec_match, acpiec_attach, NULL, NULL);
                    270:
1.23      kochi     271: static struct acpi_ec_softc *ecdt_sc;
                    272:
1.35      perry     273: static inline int
1.9       tshiozak  274: EcIsLocked(struct acpi_ec_softc *sc)
                    275: {
1.24      kochi     276:
1.17      mycroft   277:        return (lockstatus(&sc->sc_lock) == LK_EXCLUSIVE);
1.9       tshiozak  278: }
1.4       thorpej   279:
1.35      perry     280: static inline void
1.1       thorpej   281: EcLock(struct acpi_ec_softc *sc)
                    282: {
1.25      kochi     283:        ACPI_STATUS rv;
1.20      yamt      284:        int s;
1.1       thorpej   285:
1.17      mycroft   286:        lockmgr(&sc->sc_lock, LK_EXCLUSIVE, NULL);
                    287:        if (sc->sc_glk) {
1.25      kochi     288:                rv = AcpiAcquireGlobalLock(EC_LOCK_TIMEOUT,
1.17      mycroft   289:                    &sc->sc_glkhandle);
1.25      kochi     290:                if (ACPI_FAILURE(rv)) {
1.17      mycroft   291:                        printf("%s: failed to acquire global lock\n",
                    292:                            sc->sc_dev.dv_xname);
                    293:                        lockmgr(&sc->sc_lock, LK_RELEASE, NULL);
                    294:                        return;
                    295:                }
                    296:        }
1.20      yamt      297:
                    298:        s = splvm(); /* XXX */
                    299:        simple_lock(&sc->sc_slock);
                    300:        sc->sc_flags |= EC_F_TRANSACTION;
                    301:        simple_unlock(&sc->sc_slock);
                    302:        splx(s);
1.1       thorpej   303: }
                    304:
1.35      perry     305: static inline void
1.1       thorpej   306: EcUnlock(struct acpi_ec_softc *sc)
                    307: {
1.25      kochi     308:        ACPI_STATUS rv;
1.20      yamt      309:        int s;
                    310:
                    311:        /*
                    312:         * Clear & Re-Enable the EC GPE:
                    313:         * -----------------------------
                    314:         * 'Consume' any EC GPE events that we generated while performing
                    315:         * the transaction (e.g. IBF/OBF). Clearing the GPE here shouldn't
                    316:         * have an adverse affect on outstanding EC-SCI's, as the source
                    317:         * (EC-SCI) will still be high and thus should trigger the GPE
                    318:         * immediately after we re-enabling it.
                    319:         */
                    320:        s = splvm(); /* XXX */
                    321:        simple_lock(&sc->sc_slock);
                    322:        if (sc->sc_flags & EC_F_PENDQUERY) {
1.25      kochi     323:                ACPI_STATUS rv2;
1.20      yamt      324:
1.25      kochi     325:                rv2 = AcpiOsQueueForExecution(OSD_PRIORITY_HIGH,
1.20      yamt      326:                    EcGpeQueryHandler, sc);
1.25      kochi     327:                if (ACPI_FAILURE(rv2))
1.20      yamt      328:                        printf("%s: unable to queue pending query: %s\n",
1.25      kochi     329:                            sc->sc_dev.dv_xname, AcpiFormatException(rv2));
1.20      yamt      330:                sc->sc_flags &= ~EC_F_PENDQUERY;
                    331:        }
                    332:        sc->sc_flags &= ~EC_F_TRANSACTION;
                    333:        simple_unlock(&sc->sc_slock);
                    334:        splx(s);
1.11      mycroft   335:
1.17      mycroft   336:        if (sc->sc_glk) {
1.25      kochi     337:                rv = AcpiReleaseGlobalLock(sc->sc_glkhandle);
                    338:                if (ACPI_FAILURE(rv))
1.17      mycroft   339:                        printf("%s: failed to release global lock\n",
                    340:                            sc->sc_dev.dv_xname);
                    341:        }
                    342:        lockmgr(&sc->sc_lock, LK_RELEASE, NULL);
1.1       thorpej   343: }
                    344:
                    345: /*
                    346:  * acpiec_match:
                    347:  *
                    348:  *     Autoconfiguration `match' routine.
                    349:  */
1.31      kochi     350: static int
1.40      christos  351: acpiec_match(struct device *parent, struct cfdata *match,
1.39      christos  352:     void *aux)
1.1       thorpej   353: {
                    354:        struct acpi_attach_args *aa = aux;
                    355:
                    356:        if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
1.25      kochi     357:                return 0;
1.1       thorpej   358:
1.25      kochi     359:        return acpi_match_hid(aa->aa_node->ad_devinfo, ec_hid);
1.1       thorpej   360: }
                    361:
1.17      mycroft   362: void
                    363: acpiec_early_attach(struct device *parent)
                    364: {
1.23      kochi     365:        struct acpi_softc *sc = (struct acpi_softc *)parent;
1.17      mycroft   366:        EC_BOOT_RESOURCES *ep;
                    367:        ACPI_HANDLE handle;
1.23      kochi     368:        ACPI_STATUS rv;
                    369:        ACPI_INTEGER tmp;
1.17      mycroft   370:
1.23      kochi     371:        rv = AcpiGetFirmwareTable("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
                    372:            (void *)&ep);
                    373:        if (ACPI_FAILURE(rv))
1.17      mycroft   374:                return;
                    375:
                    376:        if (ep->EcControl.RegisterBitWidth != 8 ||
                    377:            ep->EcData.RegisterBitWidth != 8) {
                    378:                printf("%s: ECDT data is invalid, RegisterBitWidth=%d/%d\n",
1.23      kochi     379:                    parent->dv_xname,
1.25      kochi     380:                    ep->EcControl.RegisterBitWidth,
                    381:                    ep->EcData.RegisterBitWidth);
1.17      mycroft   382:                return;
                    383:        }
                    384:
1.23      kochi     385:        rv = AcpiGetHandle(ACPI_ROOT_OBJECT, ep->EcId, &handle);
                    386:        if (ACPI_FAILURE(rv)) {
                    387:                printf("%s: failed to look up EC object %s: %s\n",
                    388:                    parent->dv_xname,
                    389:                    ep->EcId, AcpiFormatException(rv));
                    390:                return;
                    391:        }
                    392:
                    393:        ecdt_sc = malloc(sizeof(*ecdt_sc), M_ACPI, M_ZERO);
                    394:
                    395:        strcpy(ecdt_sc->sc_dev.dv_xname, "acpiecdt0"); /* XXX */
                    396:        ecdt_sc->sc_handle = handle;
                    397:
                    398:        ecdt_sc->sc_gpebit = ep->GpeBit;
                    399:        ecdt_sc->sc_uid = ep->Uid;
                    400:
                    401:        ecdt_sc->sc_data_st = sc->sc_iot;
                    402:        if (bus_space_map(ecdt_sc->sc_data_st,
                    403:                (bus_addr_t)(ep->EcData.Address), 1, 0,
                    404:                &ecdt_sc->sc_data_sh) != 0) {
                    405:                printf("%s: bus_space_map failed for EC data.\n",
                    406:                    parent->dv_xname);
                    407:                goto out1;
                    408:        }
                    409:
                    410:        ecdt_sc->sc_csr_st = sc->sc_iot;
                    411:        if (bus_space_map(ecdt_sc->sc_csr_st,
                    412:                (bus_addr_t)(ep->EcControl.Address), 1, 0,
                    413:                &ecdt_sc->sc_csr_sh) != 0) {
                    414:                printf("%s: bus_space_map failed for EC control.\n",
                    415:                    parent->dv_xname);
                    416:                goto out2;
                    417:        }
                    418:
                    419:        rv = acpi_eval_integer(handle, "_GLK", &tmp);
                    420:        if (ACPI_SUCCESS(rv) && tmp == 1)
                    421:                ecdt_sc->sc_glk = 1;
                    422:
                    423:        rv = AcpiInstallGpeHandler(NULL, ecdt_sc->sc_gpebit,
1.33      kochi     424:            ACPI_GPE_EDGE_TRIGGERED, EcGpeHandler, ecdt_sc);
1.23      kochi     425:        if (ACPI_FAILURE(rv)) {
                    426:                printf("%s: unable to install GPE handler: %s\n",
1.33      kochi     427:                    parent->dv_xname, AcpiFormatException(rv));
1.23      kochi     428:                goto out3;
                    429:        }
                    430:
1.33      kochi     431:        rv = AcpiSetGpeType(NULL, ecdt_sc->sc_gpebit, ACPI_GPE_TYPE_RUNTIME);
                    432:        if (ACPI_FAILURE(rv)) {
                    433:                printf("%s: unable to set GPE type: %s\n",
                    434:                    parent->dv_xname, AcpiFormatException(rv));
                    435:                goto out4;
                    436:        }
                    437:
                    438:        rv = AcpiEnableGpe(NULL, ecdt_sc->sc_gpebit, ACPI_NOT_ISR);
                    439:        if (ACPI_FAILURE(rv)) {
                    440:                printf("%s: unable to enable GPE: %s\n",
                    441:                    parent->dv_xname, AcpiFormatException(rv));
                    442:                goto out4;
                    443:        }
                    444:
1.23      kochi     445:        rv = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
                    446:            ACPI_ADR_SPACE_EC, EcSpaceHandler, EcSpaceSetup, ecdt_sc);
                    447:        if (ACPI_FAILURE(rv)) {
                    448:                printf("%s: unable to install address space handler: %s\n",
                    449:                    parent->dv_xname,
                    450:                    AcpiFormatException(rv));
                    451:                goto out4;
1.17      mycroft   452:        }
1.23      kochi     453:
                    454:        printf("%s: found ECDT, GPE %d\n", parent->dv_xname,
                    455:            ecdt_sc->sc_gpebit);
                    456:
                    457:        lockinit(&ecdt_sc->sc_lock, PWAIT, "eclock", 0, 0);
                    458:        simple_lock_init(&ecdt_sc->sc_slock);
                    459:
                    460:        AcpiOsUnmapMemory(ep, ep->Length);
                    461:        return;
                    462:
                    463:  out4:
                    464:        AcpiRemoveGpeHandler(NULL, ecdt_sc->sc_gpebit, EcGpeHandler);
                    465:  out3:
                    466:        bus_space_unmap(ecdt_sc->sc_csr_st, ecdt_sc->sc_csr_sh, 1);
                    467:  out2:
                    468:        bus_space_unmap(ecdt_sc->sc_data_st, ecdt_sc->sc_data_sh, 1);
                    469:  out1:
                    470:        free(ecdt_sc, M_ACPI);
                    471:        ecdt_sc = NULL;
                    472:
                    473:        AcpiOsUnmapMemory(ep, ep->Length);
                    474:
                    475:        return;
1.17      mycroft   476: }
                    477:
1.1       thorpej   478: /*
                    479:  * acpiec_attach:
                    480:  *
                    481:  *     Autoconfiguration `attach' routine.
                    482:  */
1.31      kochi     483: static void
1.40      christos  484: acpiec_attach(struct device *parent, struct device *self, void *aux)
1.1       thorpej   485: {
                    486:        struct acpi_ec_softc *sc = (void *) self;
                    487:        struct acpi_attach_args *aa = aux;
                    488:        struct acpi_io *io0, *io1;
1.28      kochi     489:        struct acpi_resources res;
1.1       thorpej   490:        ACPI_STATUS rv;
1.21      kanaoka   491:        ACPI_INTEGER v;
1.1       thorpej   492:
1.4       thorpej   493:        ACPI_FUNCTION_TRACE(__FUNCTION__);
1.1       thorpej   494:
1.37      kochi     495:        aprint_naive(": ACPI Embedded Controller\n");
                    496:        aprint_normal(": ACPI Embedded Controller\n");
1.1       thorpej   497:
1.17      mycroft   498:        lockinit(&sc->sc_lock, PWAIT, "eclock", 0, 0);
1.20      yamt      499:        simple_lock_init(&sc->sc_slock);
1.17      mycroft   500:
1.23      kochi     501:        sc->sc_handle = aa->aa_node->ad_handle;
1.1       thorpej   502:
                    503:        /* Parse our resources. */
                    504:        ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "parsing EC resources\n"));
1.28      kochi     505:        rv = acpi_resource_parse(&sc->sc_dev, sc->sc_handle, "_CRS",
                    506:            &res, &acpi_resource_parse_ops_default);
1.18      mycroft   507:        if (ACPI_FAILURE(rv))
1.1       thorpej   508:                return;
                    509:
1.28      kochi     510: #define adi aa->aa_node->ad_devinfo
1.23      kochi     511:
                    512:        /* check if we already attached EC via ECDT */
1.28      kochi     513:        if (ecdt_sc &&
                    514:            ((adi->Valid & ACPI_VALID_UID) == ACPI_VALID_UID) &&
                    515:            ecdt_sc->sc_uid == strtoul(adi->UniqueId.Value, NULL, 10)) {
1.23      kochi     516:                /* detach all ECDT handles */
                    517:                rv = AcpiRemoveAddressSpaceHandler(ACPI_ROOT_OBJECT,
                    518:                    ACPI_ADR_SPACE_EC, EcSpaceHandler);
                    519:                if (ACPI_FAILURE(rv))
1.37      kochi     520:                        aprint_error("ERROR: RemoveAddressSpaceHandler: %s\n",
1.23      kochi     521:                            AcpiFormatException(rv));
                    522:                rv = AcpiRemoveGpeHandler(NULL, ecdt_sc->sc_gpebit,
                    523:                    EcGpeHandler);
                    524:                if (ACPI_FAILURE(rv))
1.37      kochi     525:                        aprint_error("ERROR: RemoveAddressSpaceHandler: %s\n",
1.23      kochi     526:                            AcpiFormatException(rv));
                    527:
                    528:                bus_space_unmap(ecdt_sc->sc_csr_st,
                    529:                    ecdt_sc->sc_csr_sh, 1);
                    530:                bus_space_unmap(ecdt_sc->sc_data_st,
                    531:                    ecdt_sc->sc_data_sh, 1);
                    532:
                    533:                free(ecdt_sc, M_ACPI);
                    534:                ecdt_sc = NULL;
                    535:        }
                    536:
1.28      kochi     537: #undef adi
                    538:
1.1       thorpej   539:        sc->sc_data_st = aa->aa_iot;
1.28      kochi     540:        io0 = acpi_res_io(&res, 0);
1.1       thorpej   541:        if (io0 == NULL) {
1.37      kochi     542:                aprint_error("%s: unable to find data register resource\n",
1.1       thorpej   543:                    sc->sc_dev.dv_xname);
1.27      kochi     544:                goto out;
1.1       thorpej   545:        }
                    546:        if (bus_space_map(sc->sc_data_st, io0->ar_base, io0->ar_length,
                    547:            0, &sc->sc_data_sh) != 0) {
1.37      kochi     548:                aprint_error("%s: unable to map data register\n",
1.1       thorpej   549:                    sc->sc_dev.dv_xname);
1.27      kochi     550:                goto out;
1.1       thorpej   551:        }
                    552:
                    553:        sc->sc_csr_st = aa->aa_iot;
1.28      kochi     554:        io1 = acpi_res_io(&res, 1);
1.1       thorpej   555:        if (io1 == NULL) {
1.37      kochi     556:                aprint_error("%s: unable to find csr register resource\n",
1.1       thorpej   557:                    sc->sc_dev.dv_xname);
1.27      kochi     558:                goto out;
1.1       thorpej   559:        }
                    560:        if (bus_space_map(sc->sc_csr_st, io1->ar_base, io1->ar_length,
                    561:            0, &sc->sc_csr_sh) != 0) {
1.37      kochi     562:                aprint_error("%s: unable to map csr register\n",
1.1       thorpej   563:                    sc->sc_dev.dv_xname);
1.27      kochi     564:                goto out;
1.1       thorpej   565:        }
                    566:
                    567:        /*
1.23      kochi     568:         * evaluate _GLK to see if we should acquire global lock
                    569:         * when accessing the EC.
                    570:         */
                    571:        rv = acpi_eval_integer(sc->sc_handle, "_GLK", &v);
                    572:        if (ACPI_FAILURE(rv)) {
                    573:                if (rv != AE_NOT_FOUND)
1.37      kochi     574:                        aprint_error("%s: unable to evaluate _GLK: %s\n",
1.23      kochi     575:                               sc->sc_dev.dv_xname,
                    576:                               AcpiFormatException(rv));
                    577:                sc->sc_glk = 0;
                    578:        } else
                    579:                sc->sc_glk = v;
                    580:
                    581:        /*
1.1       thorpej   582:         * Install GPE handler.
                    583:         *
                    584:         * We evaluate the _GPE method to find the GPE bit used by the
                    585:         * Embedded Controller to signal status (SCI).
                    586:         */
1.23      kochi     587:        rv = acpi_eval_integer(sc->sc_handle, "_GPE", &v);
1.18      mycroft   588:        if (ACPI_FAILURE(rv)) {
1.37      kochi     589:                aprint_error("%s: unable to evaluate _GPE: %s\n",
1.18      mycroft   590:                    sc->sc_dev.dv_xname, AcpiFormatException(rv));
1.27      kochi     591:                goto out;
1.17      mycroft   592:        }
1.21      kanaoka   593:        sc->sc_gpebit = v;
1.17      mycroft   594:
                    595:        /*
1.24      kochi     596:         * Install a handler for this EC's GPE bit.  Note that EC SCIs are
1.1       thorpej   597:         * treated as both edge- and level-triggered interrupts; in other words
                    598:         * we clear the status bit immediately after getting an EC-SCI, then
                    599:         * again after we're done processing the event.  This guarantees that
1.24      kochi     600:         * events we cause while performing a transaction (e.g. IBE/OBF) get
1.1       thorpej   601:         * cleared before re-enabling the GPE.
                    602:         */
1.18      mycroft   603:        rv = AcpiInstallGpeHandler(NULL, sc->sc_gpebit,
1.33      kochi     604:            ACPI_GPE_EDGE_TRIGGERED, EcGpeHandler, sc);
1.18      mycroft   605:        if (ACPI_FAILURE(rv)) {
1.37      kochi     606:                aprint_error("%s: unable to install GPE handler: %s\n",
1.18      mycroft   607:                    sc->sc_dev.dv_xname, AcpiFormatException(rv));
1.27      kochi     608:                goto out;
1.1       thorpej   609:        }
                    610:
1.33      kochi     611:        rv = AcpiSetGpeType(NULL, sc->sc_gpebit, ACPI_GPE_TYPE_RUNTIME);
                    612:        if (ACPI_FAILURE(rv)) {
1.37      kochi     613:                aprint_error("%s: unable to set GPE type: %s\n",
1.33      kochi     614:                    sc->sc_dev.dv_xname, AcpiFormatException(rv));
                    615:                goto out2;
                    616:        }
                    617:
                    618:        rv = AcpiEnableGpe(NULL, sc->sc_gpebit, ACPI_NOT_ISR);
                    619:        if (ACPI_FAILURE(rv)) {
1.37      kochi     620:                aprint_error("%s: unable to enable GPE: %s\n",
1.33      kochi     621:                    sc->sc_dev.dv_xname, AcpiFormatException(rv));
                    622:                goto out2;
                    623:        }
                    624:
1.1       thorpej   625:        /* Install address space handler. */
1.23      kochi     626:        rv = AcpiInstallAddressSpaceHandler(sc->sc_handle,
1.18      mycroft   627:             ACPI_ADR_SPACE_EC, EcSpaceHandler, EcSpaceSetup, sc);
                    628:        if (ACPI_FAILURE(rv)) {
1.37      kochi     629:                aprint_error("%s: unable to install address space handler: %s\n",
1.18      mycroft   630:                    sc->sc_dev.dv_xname, AcpiFormatException(rv));
1.33      kochi     631:                goto out2;
1.1       thorpej   632:        }
                    633:
1.33      kochi     634:        return_VOID;
                    635:  out2:
                    636:        (void)AcpiRemoveGpeHandler(NULL, sc->sc_gpebit, EcGpeHandler);
                    637:
1.27      kochi     638:  out:
1.28      kochi     639:        acpi_resource_cleanup(&res);
1.1       thorpej   640:        return_VOID;
                    641: }
                    642:
                    643: static void
                    644: EcGpeQueryHandler(void *Context)
                    645: {
                    646:        struct acpi_ec_softc *sc = Context;
1.38      dogcow    647:        UINT8 Data = 0;
1.25      kochi     648:        ACPI_STATUS rv;
1.1       thorpej   649:        char qxx[5];
                    650:
1.4       thorpej   651:        ACPI_FUNCTION_TRACE(__FUNCTION__);
1.1       thorpej   652:
                    653:        for (;;) {
                    654:                /*
                    655:                 * Check EC_SCI.
1.24      kochi     656:                 *
1.1       thorpej   657:                 * Bail out if the EC_SCI bit of the status register is not
                    658:                 * set. Note that this function should only be called when
                    659:                 * this bit is set (polling is used to detect IBE/OBF events).
                    660:                 *
                    661:                 * It is safe to do this without locking the controller, as
                    662:                 * it's OK to call EcQuery when there's no data ready; in the
                    663:                 * worst case we should just find nothing waiting for us and
                    664:                 * bail.
                    665:                 */
                    666:                if ((EC_CSR_READ(sc) & EC_EVENT_SCI) == 0)
                    667:                        break;
                    668:
1.20      yamt      669:                EcLock(sc);
                    670:
1.1       thorpej   671:                /*
                    672:                 * Find out why the EC is signalling us
                    673:                 */
1.25      kochi     674:                rv = EcQuery(sc, &Data);
1.20      yamt      675:
                    676:                EcUnlock(sc);
1.24      kochi     677:
1.1       thorpej   678:                /*
                    679:                 * If we failed to get anything from the EC, give up.
                    680:                 */
1.25      kochi     681:                if (ACPI_FAILURE(rv)) {
1.18      mycroft   682:                        printf("%s: GPE query failed: %s\n",
1.25      kochi     683:                            sc->sc_dev.dv_xname, AcpiFormatException(rv));
1.1       thorpej   684:                        break;
                    685:                }
                    686:
                    687:                /*
                    688:                 * Evaluate _Qxx to respond to the controller.
                    689:                 */
1.29      itojun    690:                snprintf(qxx, sizeof(qxx), "_Q%02X", Data);
1.25      kochi     691:                rv = AcpiEvaluateObject(sc->sc_handle, qxx,
1.1       thorpej   692:                    NULL, NULL);
                    693:
                    694:                /*
                    695:                 * Ignore spurious query requests.
                    696:                 */
1.25      kochi     697:                if (ACPI_FAILURE(rv) &&
                    698:                    (Data != 0 || rv != AE_NOT_FOUND)) {
1.1       thorpej   699:                        printf("%s: evaluation of GPE query method %s "
1.18      mycroft   700:                            "failed: %s\n", sc->sc_dev.dv_xname, qxx,
1.25      kochi     701:                            AcpiFormatException(rv));
1.1       thorpej   702:                }
                    703:        }
                    704:
                    705:        /* I know I request Level trigger cleanup */
1.25      kochi     706:        rv = AcpiClearGpe(NULL, sc->sc_gpebit, ACPI_NOT_ISR);
                    707:        if (ACPI_FAILURE(rv))
1.18      mycroft   708:                printf("%s: AcpiClearGpe failed: %s\n", sc->sc_dev.dv_xname,
1.25      kochi     709:                    AcpiFormatException(rv));
1.1       thorpej   710:
                    711:        return_VOID;
                    712: }
                    713:
1.33      kochi     714: static UINT32
1.1       thorpej   715: EcGpeHandler(void *Context)
                    716: {
                    717:        struct acpi_ec_softc *sc = Context;
                    718:        uint32_t csrvalue;
1.25      kochi     719:        ACPI_STATUS rv;
1.1       thorpej   720:
1.24      kochi     721:        /*
1.1       thorpej   722:         * If EC is locked, the intr must process EcRead/Write wait only.
                    723:         * Query request must be pending.
                    724:         */
1.20      yamt      725:        simple_lock(&sc->sc_slock);
                    726:        if (sc->sc_flags & EC_F_TRANSACTION) {
1.1       thorpej   727:                csrvalue = EC_CSR_READ(sc);
1.30      yamt      728:                sc->sc_flags |= EC_F_PENDQUERY;
1.1       thorpej   729:
                    730:                if ((csrvalue & EC_FLAG_OUTPUT_BUFFER) != 0 ||
                    731:                    (csrvalue & EC_FLAG_INPUT_BUFFER) == 0) {
                    732:                        sc->sc_csrvalue = csrvalue;
                    733:                        wakeup(&sc->sc_csrvalue);
                    734:                }
1.20      yamt      735:                simple_unlock(&sc->sc_slock);
1.1       thorpej   736:        } else {
1.20      yamt      737:                simple_unlock(&sc->sc_slock);
1.1       thorpej   738:                /* Enqueue GpeQuery handler. */
1.25      kochi     739:                rv = AcpiOsQueueForExecution(OSD_PRIORITY_HIGH,
1.18      mycroft   740:                    EcGpeQueryHandler, Context);
1.25      kochi     741:                if (ACPI_FAILURE(rv))
1.18      mycroft   742:                        printf("%s: failed to enqueue query handler: %s\n",
1.25      kochi     743:                            sc->sc_dev.dv_xname, AcpiFormatException(rv));
1.1       thorpej   744:        }
1.33      kochi     745:
                    746:        return 0;                       /* XXX not yet used in ACPI-CA */
1.1       thorpej   747: }
                    748:
                    749: static ACPI_STATUS
1.40      christos  750: EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function, void *Context,
1.1       thorpej   751:     void **RegionContext)
                    752: {
                    753:
1.4       thorpej   754:        ACPI_FUNCTION_TRACE(__FUNCTION__);
1.1       thorpej   755:
                    756:        /*
                    757:         * Just pass the context through, there's nothing to do here.
                    758:         */
1.23      kochi     759:        if (Function == ACPI_REGION_DEACTIVATE)
                    760:                *RegionContext = NULL;
                    761:        else
                    762:                *RegionContext = Context;
1.1       thorpej   763:
                    764:        return_ACPI_STATUS(AE_OK);
                    765: }
                    766:
                    767: static ACPI_STATUS
                    768: EcSpaceHandler(UINT32 Function, ACPI_PHYSICAL_ADDRESS Address, UINT32 width,
1.40      christos  769:     ACPI_INTEGER *Value, void *Context, void *RegionContext)
1.1       thorpej   770: {
                    771:        struct acpi_ec_softc *sc = Context;
1.25      kochi     772:        ACPI_STATUS rv = AE_OK;
1.1       thorpej   773:        EC_REQUEST EcRequest;
                    774:        int i;
                    775:
1.4       thorpej   776:        ACPI_FUNCTION_TRACE_U32(__FUNCTION__, (UINT32)Address);
1.1       thorpej   777:
                    778:        if ((Address > 0xFF) || (width % 8 != 0) || (Value == NULL) ||
                    779:            (Context == NULL))
                    780:                return_ACPI_STATUS(AE_BAD_PARAMETER);
                    781:
                    782:        switch (Function) {
1.4       thorpej   783:        case ACPI_READ:
1.1       thorpej   784:                EcRequest.Command = EC_COMMAND_READ;
                    785:                EcRequest.Address = Address;
                    786:                (*Value) = 0;
                    787:                break;
                    788:
1.4       thorpej   789:        case ACPI_WRITE:
1.1       thorpej   790:                EcRequest.Command = EC_COMMAND_WRITE;
                    791:                EcRequest.Address = Address;
                    792:                break;
                    793:
                    794:        default:
                    795:                printf("%s: invalid Address Space function: %d\n",
                    796:                    sc->sc_dev.dv_xname, Function);
                    797:                return_ACPI_STATUS(AE_BAD_PARAMETER);
                    798:        }
                    799:
                    800:        /*
                    801:         * Perform the transaction.
                    802:         */
                    803:        for (i = 0; i < width; i += 8) {
1.4       thorpej   804:                if (Function == ACPI_READ)
1.1       thorpej   805:                        EcRequest.Data = 0;
                    806:                else
                    807:                        EcRequest.Data = (UINT8)((*Value) >> i);
                    808:
1.25      kochi     809:                rv = EcTransaction(sc, &EcRequest);
                    810:                if (ACPI_FAILURE(rv))
1.1       thorpej   811:                        break;
                    812:
                    813:                (*Value) |= (UINT32)EcRequest.Data << i;
                    814:                if (++EcRequest.Address == 0)
                    815:                        return_ACPI_STATUS(AE_BAD_PARAMETER);
                    816:        }
                    817:
1.25      kochi     818:        return_ACPI_STATUS(rv);
1.1       thorpej   819: }
                    820:
                    821: static ACPI_STATUS
                    822: EcWaitEventIntr(struct acpi_ec_softc *sc, EC_EVENT Event)
                    823: {
                    824:        EC_STATUS EcStatus;
                    825:        int i;
                    826:
1.4       thorpej   827:        ACPI_FUNCTION_TRACE_U32(__FUNCTION__, (UINT32)Event);
1.1       thorpej   828:
                    829:        /* XXX Need better test for "yes, you have interrupts". */
                    830:        if (cold)
                    831:                return_ACPI_STATUS(EcWaitEvent(sc, Event));
                    832:
                    833:        if (EcIsLocked(sc) == 0)
                    834:                printf("%s: EcWaitEventIntr called without EC lock!\n",
                    835:                    sc->sc_dev.dv_xname);
                    836:
                    837:        EcStatus = EC_CSR_READ(sc);
                    838:
                    839:        /* Too long? */
                    840:        for (i = 0; i < 10; i++) {
                    841:                /* Check EC status against the desired event. */
                    842:                if ((Event == EC_EVENT_OUTPUT_BUFFER_FULL) &&
                    843:                    (EcStatus & EC_FLAG_OUTPUT_BUFFER) != 0)
                    844:                        return_ACPI_STATUS(AE_OK);
1.24      kochi     845:
1.1       thorpej   846:                if ((Event == EC_EVENT_INPUT_BUFFER_EMPTY) &&
                    847:                    (EcStatus & EC_FLAG_INPUT_BUFFER) == 0)
                    848:                        return_ACPI_STATUS(AE_OK);
                    849:
                    850:                sc->sc_csrvalue = 0;
                    851:                /* XXXJRT Sleeping with a lock held? */
1.32      yamt      852:                if (tsleep(&sc->sc_csrvalue, 0, "EcWait", (hz + 99) / 100)
                    853:                    != EWOULDBLOCK)
1.1       thorpej   854:                        EcStatus = sc->sc_csrvalue;
                    855:                else
                    856:                        EcStatus = EC_CSR_READ(sc);
                    857:        }
                    858:        return_ACPI_STATUS(AE_ERROR);
                    859: }
                    860:
                    861: static ACPI_STATUS
                    862: EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event)
                    863: {
                    864:        EC_STATUS EcStatus;
                    865:        UINT32 i = 0;
                    866:
                    867:        if (EcIsLocked(sc) == 0)
                    868:                printf("%s: EcWaitEvent called without EC lock!\n",
                    869:                    sc->sc_dev.dv_xname);
                    870:
                    871:        /*
1.41      jmcneill  872:         * Stall 10us:
1.1       thorpej   873:         * ----------
1.41      jmcneill  874:         * Stall for 10 microseconds before reading the status register
1.1       thorpej   875:         * for the first time.  This allows the EC to set the IBF/OBF
                    876:         * bit to its proper state.
                    877:         */
1.41      jmcneill  878:        AcpiOsStall(10);
1.1       thorpej   879:
                    880:        /*
                    881:         * Wait For Event:
                    882:         * ---------------
                    883:         * Poll the EC status register to detect completion of the last
1.41      jmcneill  884:         * command.  Wait up to 100ms (in 100us chunks) for this to occur.
1.1       thorpej   885:         */
1.41      jmcneill  886:        for (i = 0; i < 1000; i++) {
1.1       thorpej   887:                EcStatus = EC_CSR_READ(sc);
                    888:
                    889:                if ((Event == EC_EVENT_OUTPUT_BUFFER_FULL) &&
                    890:                    (EcStatus & EC_FLAG_OUTPUT_BUFFER) != 0)
1.25      kochi     891:                        return AE_OK;
1.1       thorpej   892:
                    893:                if ((Event == EC_EVENT_INPUT_BUFFER_EMPTY) &&
                    894:                    (EcStatus & EC_FLAG_INPUT_BUFFER) == 0)
1.25      kochi     895:                        return AE_OK;
1.1       thorpej   896:
                    897:                AcpiOsStall(10);
                    898:        }
                    899:
1.25      kochi     900:        return AE_ERROR;
1.24      kochi     901: }
1.1       thorpej   902:
                    903: static ACPI_STATUS
                    904: EcQuery(struct acpi_ec_softc *sc, UINT8 *Data)
                    905: {
1.25      kochi     906:        ACPI_STATUS rv;
1.1       thorpej   907:
1.20      yamt      908:        if (EcIsLocked(sc) == 0)
                    909:                printf("%s: EcQuery called without EC lock!\n",
                    910:                    sc->sc_dev.dv_xname);
1.1       thorpej   911:
                    912:        EC_CSR_WRITE(sc, EC_COMMAND_QUERY);
1.25      kochi     913:        rv = EcWaitEventIntr(sc, EC_EVENT_OUTPUT_BUFFER_FULL);
                    914:        if (ACPI_SUCCESS(rv))
1.1       thorpej   915:                *Data = EC_DATA_READ(sc);
                    916:
1.25      kochi     917:        if (ACPI_FAILURE(rv))
1.2       enami     918:                printf("%s: timed out waiting for EC to respond to "
1.1       thorpej   919:                    "EC_COMMAND_QUERY\n", sc->sc_dev.dv_xname);
                    920:
1.25      kochi     921:        return rv;
1.24      kochi     922: }
1.1       thorpej   923:
                    924: static ACPI_STATUS
                    925: EcTransaction(struct acpi_ec_softc *sc, EC_REQUEST *EcRequest)
                    926: {
1.25      kochi     927:        ACPI_STATUS rv;
1.1       thorpej   928:
1.11      mycroft   929:        EcLock(sc);
1.1       thorpej   930:
                    931:        /*
1.41      jmcneill  932:         * Perform the transaction, and make sure GPE is enabled before
                    933:         * doing so.
1.1       thorpej   934:         */
1.41      jmcneill  935:        rv = AcpiEnableGpe(NULL, sc->sc_gpebit, ACPI_NOT_ISR);
                    936:        if (ACPI_FAILURE(rv))
                    937:                return rv;
1.1       thorpej   938:        switch (EcRequest->Command) {
                    939:        case EC_COMMAND_READ:
1.25      kochi     940:                rv = EcRead(sc, EcRequest->Address, &(EcRequest->Data));
1.1       thorpej   941:                break;
                    942:
                    943:        case EC_COMMAND_WRITE:
1.25      kochi     944:                rv = EcWrite(sc, EcRequest->Address, &(EcRequest->Data));
1.1       thorpej   945:                break;
                    946:
                    947:        default:
1.25      kochi     948:                rv = AE_SUPPORT;
1.1       thorpej   949:                break;
                    950:        }
                    951:
                    952:        EcUnlock(sc);
                    953:
1.25      kochi     954:        return rv;
1.1       thorpej   955: }
                    956:
                    957: static ACPI_STATUS
                    958: EcRead(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data)
                    959: {
1.25      kochi     960:        ACPI_STATUS rv;
1.1       thorpej   961:
                    962:        if (EcIsLocked(sc) == 0)
                    963:                printf("%s: EcRead called without EC lock!\n",
                    964:                    sc->sc_dev.dv_xname);
                    965:
                    966:        /* EcBurstEnable(EmbeddedController); */
                    967:
                    968:        EC_CSR_WRITE(sc, EC_COMMAND_READ);
1.25      kochi     969:        if ((rv = EcWaitEventIntr(sc, EC_EVENT_INPUT_BUFFER_EMPTY)) !=
1.1       thorpej   970:            AE_OK) {
                    971:                printf("%s: EcRead: timeout waiting for EC to process "
                    972:                    "read command\n", sc->sc_dev.dv_xname);
1.25      kochi     973:                return rv;
1.1       thorpej   974:        }
                    975:
                    976:        EC_DATA_WRITE(sc, Address);
1.25      kochi     977:        if ((rv = EcWaitEventIntr(sc, EC_EVENT_OUTPUT_BUFFER_FULL)) !=
1.1       thorpej   978:            AE_OK) {
                    979:                printf("%s: EcRead: timeout waiting for EC to send data\n",
                    980:                    sc->sc_dev.dv_xname);
1.25      kochi     981:                return rv;
1.1       thorpej   982:        }
                    983:
                    984:        (*Data) = EC_DATA_READ(sc);
                    985:
                    986:        /* EcBurstDisable(EmbeddedController); */
                    987:
1.25      kochi     988:        return AE_OK;
1.24      kochi     989: }
1.1       thorpej   990:
                    991: static ACPI_STATUS
                    992: EcWrite(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data)
                    993: {
1.25      kochi     994:        ACPI_STATUS rv;
1.1       thorpej   995:
                    996:        if (EcIsLocked(sc) == 0)
                    997:                printf("%s: EcWrite called without EC lock!\n",
                    998:                    sc->sc_dev.dv_xname);
                    999:
                   1000:        /* EcBurstEnable(EmbeddedController); */
                   1001:
                   1002:        EC_CSR_WRITE(sc, EC_COMMAND_WRITE);
1.25      kochi    1003:        if ((rv = EcWaitEventIntr(sc, EC_EVENT_INPUT_BUFFER_EMPTY)) !=
1.1       thorpej  1004:            AE_OK) {
                   1005:                printf("%s: EcWrite: timeout waiting for EC to process "
                   1006:                    "write command\n", sc->sc_dev.dv_xname);
1.25      kochi    1007:                return rv;
1.1       thorpej  1008:        }
                   1009:
                   1010:        EC_DATA_WRITE(sc, Address);
1.25      kochi    1011:        if ((rv = EcWaitEventIntr(sc, EC_EVENT_INPUT_BUFFER_EMPTY)) !=
1.1       thorpej  1012:            AE_OK) {
                   1013:                printf("%s: EcWrite: timeout waiting for EC to process "
                   1014:                    "address\n", sc->sc_dev.dv_xname);
1.25      kochi    1015:                return rv;
1.1       thorpej  1016:        }
                   1017:
                   1018:        EC_DATA_WRITE(sc, *Data);
1.25      kochi    1019:        if ((rv = EcWaitEventIntr(sc, EC_EVENT_INPUT_BUFFER_EMPTY)) !=
1.1       thorpej  1020:            AE_OK) {
                   1021:                printf("%s: EcWrite: timeout waiting for EC to process "
                   1022:                    "data\n", sc->sc_dev.dv_xname);
1.25      kochi    1023:                return rv;
1.1       thorpej  1024:        }
                   1025:
                   1026:        /* EcBurstDisable(EmbeddedController); */
                   1027:
1.25      kochi    1028:        return AE_OK;
1.1       thorpej  1029: }

CVSweb <webmaster@jp.NetBSD.org>