Home | History | Annotate | Line # | Download | only in acpi
acpi_ec.c revision 1.27
      1 /*	$NetBSD: acpi_ec.c,v 1.27 2004/04/11 10:36:35 kochi Exp $	*/
      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  *
     98  * 3. Conditions
     99  *
    100  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
    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
    108  * documentation of any changes made by any predecessor Licensee.  Licensee
    109  * must include a prominent statement that the modification is derived,
    110  * directly or indirectly, from Original Intel Code.
    111  *
    112  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
    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
    146  * PARTICULAR PURPOSE.
    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  *****************************************************************************/
    173 
    174 #include <sys/cdefs.h>
    175 __KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.27 2004/04/11 10:36:35 kochi Exp $");
    176 
    177 #include <sys/param.h>
    178 #include <sys/systm.h>
    179 #include <sys/device.h>
    180 #include <sys/malloc.h>
    181 #include <sys/proc.h>
    182 #include <sys/kernel.h>
    183 
    184 #include <machine/bus.h>
    185 
    186 #include <dev/acpi/acpica.h>
    187 #include <dev/acpi/acpireg.h>
    188 #include <dev/acpi/acpivar.h>
    189 #include <dev/acpi/acpi_ecreg.h>
    190 
    191 MALLOC_DECLARE(M_ACPI);
    192 
    193 #define _COMPONENT	ACPI_EC_COMPONENT
    194 ACPI_MODULE_NAME("EC")
    195 
    196 struct acpi_ec_softc {
    197 	struct device	sc_dev;		/* base device glue */
    198 	ACPI_HANDLE sc_handle;		/* ACPI handle */
    199 
    200 	struct acpi_resources sc_res;	/* our bus resources */
    201 
    202 	UINT32		sc_gpebit;	/* our GPE interrupt bit */
    203 
    204 	bus_space_tag_t	sc_data_st;	/* space tag for data register */
    205 	bus_space_handle_t sc_data_sh;	/* space handle for data register */
    206 
    207 	bus_space_tag_t	sc_csr_st;	/* space tag for control register */
    208 	bus_space_handle_t sc_csr_sh;	/* space handle for control register */
    209 
    210 	int		sc_flags;	/* see below */
    211 
    212 	uint32_t	sc_csrvalue;	/* saved control register */
    213 	uint32_t	sc_uid;		/* _UID in namespace (ECDT only) */
    214 
    215 	struct lock	sc_lock;	/* serialize operations to this EC */
    216 	struct simplelock sc_slock;	/* protect against interrupts */
    217 	UINT32		sc_glkhandle;	/* global lock handle */
    218 	UINT32		sc_glk;		/* need global lock? */
    219 };
    220 
    221 static const char * const ec_hid[] = {
    222 	"PNP0C09",
    223 	NULL
    224 };
    225 
    226 #define	EC_F_TRANSACTION	0x01	/* doing transaction */
    227 #define	EC_F_PENDQUERY		0x02	/* query is pending */
    228 
    229 /*
    230  * how long to wait to acquire global lock.
    231  * the value is taken from FreeBSD driver.
    232  */
    233 #define	EC_LOCK_TIMEOUT	1000
    234 
    235 #define	EC_DATA_READ(sc)						\
    236 	bus_space_read_1((sc)->sc_data_st, (sc)->sc_data_sh, 0)
    237 #define	EC_DATA_WRITE(sc, v)						\
    238 	bus_space_write_1((sc)->sc_data_st, (sc)->sc_data_sh, 0, (v))
    239 
    240 #define	EC_CSR_READ(sc)							\
    241 	bus_space_read_1((sc)->sc_csr_st, (sc)->sc_csr_sh, 0)
    242 #define	EC_CSR_WRITE(sc, v)						\
    243 	bus_space_write_1((sc)->sc_csr_st, (sc)->sc_csr_sh, 0, (v))
    244 
    245 typedef struct {
    246 	EC_COMMAND	Command;
    247 	UINT8		Address;
    248 	UINT8		Data;
    249 } EC_REQUEST;
    250 
    251 static void		EcGpeHandler(void *Context);
    252 static ACPI_STATUS	EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function,
    253 			    void *Context, void **return_Context);
    254 static ACPI_STATUS	EcSpaceHandler(UINT32 Function,
    255 			    ACPI_PHYSICAL_ADDRESS Address, UINT32 width,
    256 			    ACPI_INTEGER *Value, void *Context,
    257 			    void *RegionContext);
    258 
    259 static ACPI_STATUS	EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event);
    260 static ACPI_STATUS	EcQuery(struct acpi_ec_softc *sc, UINT8 *Data);
    261 static ACPI_STATUS	EcTransaction(struct acpi_ec_softc *sc,
    262 			    EC_REQUEST *EcRequest);
    263 static ACPI_STATUS	EcRead(struct acpi_ec_softc *sc, UINT8 Address,
    264 			    UINT8 *Data);
    265 static ACPI_STATUS	EcWrite(struct acpi_ec_softc *sc, UINT8 Address,
    266 			    UINT8 *Data);
    267 static void		EcGpeQueryHandler(void *);
    268 static __inline int	EcIsLocked(struct acpi_ec_softc *);
    269 static __inline void	EcLock(struct acpi_ec_softc *);
    270 static __inline void	EcUnlock(struct acpi_ec_softc *);
    271 
    272 
    273 int	acpiec_match(struct device *, struct cfdata *, void *);
    274 void	acpiec_attach(struct device *, struct device *, void *);
    275 
    276 CFATTACH_DECL(acpiec, sizeof(struct acpi_ec_softc),
    277     acpiec_match, acpiec_attach, NULL, NULL);
    278 
    279 static struct acpi_ec_softc *ecdt_sc;
    280 
    281 static __inline int
    282 EcIsLocked(struct acpi_ec_softc *sc)
    283 {
    284 
    285 	return (lockstatus(&sc->sc_lock) == LK_EXCLUSIVE);
    286 }
    287 
    288 static __inline void
    289 EcLock(struct acpi_ec_softc *sc)
    290 {
    291 	ACPI_STATUS rv;
    292 	int s;
    293 
    294 	lockmgr(&sc->sc_lock, LK_EXCLUSIVE, NULL);
    295 	if (sc->sc_glk) {
    296 		rv = AcpiAcquireGlobalLock(EC_LOCK_TIMEOUT,
    297 		    &sc->sc_glkhandle);
    298 		if (ACPI_FAILURE(rv)) {
    299 			printf("%s: failed to acquire global lock\n",
    300 			    sc->sc_dev.dv_xname);
    301 			lockmgr(&sc->sc_lock, LK_RELEASE, NULL);
    302 			return;
    303 		}
    304 	}
    305 
    306 	s = splvm(); /* XXX */
    307 	simple_lock(&sc->sc_slock);
    308 	sc->sc_flags |= EC_F_TRANSACTION;
    309 	simple_unlock(&sc->sc_slock);
    310 	splx(s);
    311 }
    312 
    313 static __inline void
    314 EcUnlock(struct acpi_ec_softc *sc)
    315 {
    316 	ACPI_STATUS rv;
    317 	int s;
    318 
    319 	/*
    320 	 * Clear & Re-Enable the EC GPE:
    321 	 * -----------------------------
    322 	 * 'Consume' any EC GPE events that we generated while performing
    323 	 * the transaction (e.g. IBF/OBF). Clearing the GPE here shouldn't
    324 	 * have an adverse affect on outstanding EC-SCI's, as the source
    325 	 * (EC-SCI) will still be high and thus should trigger the GPE
    326 	 * immediately after we re-enabling it.
    327 	 */
    328 	s = splvm(); /* XXX */
    329 	simple_lock(&sc->sc_slock);
    330 	if (sc->sc_flags & EC_F_PENDQUERY) {
    331 		ACPI_STATUS rv2;
    332 
    333 		rv2 = AcpiOsQueueForExecution(OSD_PRIORITY_HIGH,
    334 		    EcGpeQueryHandler, sc);
    335 		if (ACPI_FAILURE(rv2))
    336 			printf("%s: unable to queue pending query: %s\n",
    337 			    sc->sc_dev.dv_xname, AcpiFormatException(rv2));
    338 		sc->sc_flags &= ~EC_F_PENDQUERY;
    339 	}
    340 	sc->sc_flags &= ~EC_F_TRANSACTION;
    341 	simple_unlock(&sc->sc_slock);
    342 	splx(s);
    343 
    344 	if (sc->sc_glk) {
    345 		rv = AcpiReleaseGlobalLock(sc->sc_glkhandle);
    346 		if (ACPI_FAILURE(rv))
    347 			printf("%s: failed to release global lock\n",
    348 			    sc->sc_dev.dv_xname);
    349 	}
    350 	lockmgr(&sc->sc_lock, LK_RELEASE, NULL);
    351 }
    352 
    353 /*
    354  * acpiec_match:
    355  *
    356  *	Autoconfiguration `match' routine.
    357  */
    358 int
    359 acpiec_match(struct device *parent, struct cfdata *match, void *aux)
    360 {
    361 	struct acpi_attach_args *aa = aux;
    362 
    363 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
    364 		return 0;
    365 
    366 	return acpi_match_hid(aa->aa_node->ad_devinfo, ec_hid);
    367 }
    368 
    369 void
    370 acpiec_early_attach(struct device *parent)
    371 {
    372 	struct acpi_softc *sc = (struct acpi_softc *)parent;
    373 	EC_BOOT_RESOURCES *ep;
    374 	ACPI_HANDLE handle;
    375 	ACPI_STATUS rv;
    376 	ACPI_INTEGER tmp;
    377 
    378 	rv = AcpiGetFirmwareTable("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
    379 	    (void *)&ep);
    380 	if (ACPI_FAILURE(rv))
    381 		return;
    382 
    383 	if (ep->EcControl.RegisterBitWidth != 8 ||
    384 	    ep->EcData.RegisterBitWidth != 8) {
    385 		printf("%s: ECDT data is invalid, RegisterBitWidth=%d/%d\n",
    386 		    parent->dv_xname,
    387 		    ep->EcControl.RegisterBitWidth,
    388 		    ep->EcData.RegisterBitWidth);
    389 		return;
    390 	}
    391 
    392 	rv = AcpiGetHandle(ACPI_ROOT_OBJECT, ep->EcId, &handle);
    393 	if (ACPI_FAILURE(rv)) {
    394 		printf("%s: failed to look up EC object %s: %s\n",
    395 		    parent->dv_xname,
    396 		    ep->EcId, AcpiFormatException(rv));
    397 		return;
    398 	}
    399 
    400 	ecdt_sc = malloc(sizeof(*ecdt_sc), M_ACPI, M_ZERO);
    401 
    402 	strcpy(ecdt_sc->sc_dev.dv_xname, "acpiecdt0"); /* XXX */
    403 	ecdt_sc->sc_handle = handle;
    404 
    405 	ecdt_sc->sc_gpebit = ep->GpeBit;
    406 	ecdt_sc->sc_uid = ep->Uid;
    407 
    408 	ecdt_sc->sc_data_st = sc->sc_iot;
    409 	if (bus_space_map(ecdt_sc->sc_data_st,
    410 		(bus_addr_t)(ep->EcData.Address), 1, 0,
    411 		&ecdt_sc->sc_data_sh) != 0) {
    412 		printf("%s: bus_space_map failed for EC data.\n",
    413 		    parent->dv_xname);
    414 		goto out1;
    415 	}
    416 
    417 	ecdt_sc->sc_csr_st = sc->sc_iot;
    418 	if (bus_space_map(ecdt_sc->sc_csr_st,
    419 		(bus_addr_t)(ep->EcControl.Address), 1, 0,
    420 		&ecdt_sc->sc_csr_sh) != 0) {
    421 		printf("%s: bus_space_map failed for EC control.\n",
    422 		    parent->dv_xname);
    423 		goto out2;
    424 	}
    425 
    426 	rv = acpi_eval_integer(handle, "_GLK", &tmp);
    427 	if (ACPI_SUCCESS(rv) && tmp == 1)
    428 		ecdt_sc->sc_glk = 1;
    429 
    430 	rv = AcpiInstallGpeHandler(NULL, ecdt_sc->sc_gpebit,
    431 	    ACPI_EVENT_EDGE_TRIGGERED, EcGpeHandler, ecdt_sc);
    432 	if (ACPI_FAILURE(rv)) {
    433 		printf("%s: unable to install GPE handler: %s\n",
    434 		    parent->dv_xname,
    435 		    AcpiFormatException(rv));
    436 		goto out3;
    437 	}
    438 
    439 	rv = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT,
    440 	    ACPI_ADR_SPACE_EC, EcSpaceHandler, EcSpaceSetup, ecdt_sc);
    441 	if (ACPI_FAILURE(rv)) {
    442 		printf("%s: unable to install address space handler: %s\n",
    443 		    parent->dv_xname,
    444 		    AcpiFormatException(rv));
    445 		goto out4;
    446 	}
    447 
    448 	printf("%s: found ECDT, GPE %d\n", parent->dv_xname,
    449 	    ecdt_sc->sc_gpebit);
    450 
    451 	lockinit(&ecdt_sc->sc_lock, PWAIT, "eclock", 0, 0);
    452 	simple_lock_init(&ecdt_sc->sc_slock);
    453 
    454 	AcpiOsUnmapMemory(ep, ep->Length);
    455 	return;
    456 
    457  out4:
    458 	AcpiRemoveGpeHandler(NULL, ecdt_sc->sc_gpebit, EcGpeHandler);
    459  out3:
    460 	bus_space_unmap(ecdt_sc->sc_csr_st, ecdt_sc->sc_csr_sh, 1);
    461  out2:
    462 	bus_space_unmap(ecdt_sc->sc_data_st, ecdt_sc->sc_data_sh, 1);
    463  out1:
    464 	free(ecdt_sc, M_ACPI);
    465 	ecdt_sc = NULL;
    466 
    467 	AcpiOsUnmapMemory(ep, ep->Length);
    468 
    469 	return;
    470 }
    471 
    472 /*
    473  * acpiec_attach:
    474  *
    475  *	Autoconfiguration `attach' routine.
    476  */
    477 void
    478 acpiec_attach(struct device *parent, struct device *self, void *aux)
    479 {
    480 	struct acpi_ec_softc *sc = (void *) self;
    481 	struct acpi_attach_args *aa = aux;
    482 	struct acpi_io *io0, *io1;
    483 	ACPI_STATUS rv;
    484 	ACPI_INTEGER v;
    485 
    486 	ACPI_FUNCTION_TRACE(__FUNCTION__);
    487 
    488 	printf(": ACPI Embedded Controller\n");
    489 
    490 	lockinit(&sc->sc_lock, PWAIT, "eclock", 0, 0);
    491 	simple_lock_init(&sc->sc_slock);
    492 
    493 	sc->sc_handle = aa->aa_node->ad_handle;
    494 
    495 	/* Parse our resources. */
    496 	ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "parsing EC resources\n"));
    497 	rv = acpi_resource_parse(&sc->sc_dev, aa->aa_node->ad_handle, "_CRS",
    498 	    &sc->sc_res, &acpi_resource_parse_ops_default);
    499 	if (ACPI_FAILURE(rv))
    500 		return;
    501 
    502 	rv = acpi_eval_integer(aa->aa_node->ad_handle, "_UID", &v);
    503 
    504 	/* check if we already attached EC via ECDT */
    505 	if (ACPI_SUCCESS(rv) && ecdt_sc && ecdt_sc->sc_uid == v) {
    506 
    507 		/* detach all ECDT handles */
    508 		rv = AcpiRemoveAddressSpaceHandler(ACPI_ROOT_OBJECT,
    509 		    ACPI_ADR_SPACE_EC, EcSpaceHandler);
    510 		if (ACPI_FAILURE(rv))
    511 			printf("ERROR: RemoveAddressSpaceHandler: %s\n",
    512 			    AcpiFormatException(rv));
    513 		rv = AcpiRemoveGpeHandler(NULL, ecdt_sc->sc_gpebit,
    514 		    EcGpeHandler);
    515 		if (ACPI_FAILURE(rv))
    516 			printf("ERROR: RemoveAddressSpaceHandler: %s\n",
    517 			    AcpiFormatException(rv));
    518 
    519 		bus_space_unmap(ecdt_sc->sc_csr_st,
    520 		    ecdt_sc->sc_csr_sh, 1);
    521 		bus_space_unmap(ecdt_sc->sc_data_st,
    522 		    ecdt_sc->sc_data_sh, 1);
    523 
    524 		free(ecdt_sc, M_ACPI);
    525 		ecdt_sc = NULL;
    526 	}
    527 
    528 	sc->sc_data_st = aa->aa_iot;
    529 	io0 = acpi_res_io(&sc->sc_res, 0);
    530 	if (io0 == NULL) {
    531 		printf("%s: unable to find data register resource\n",
    532 		    sc->sc_dev.dv_xname);
    533 		goto out;
    534 	}
    535 	if (bus_space_map(sc->sc_data_st, io0->ar_base, io0->ar_length,
    536 	    0, &sc->sc_data_sh) != 0) {
    537 		printf("%s: unable to map data register\n",
    538 		    sc->sc_dev.dv_xname);
    539 		goto out;
    540 	}
    541 
    542 	sc->sc_csr_st = aa->aa_iot;
    543 	io1 = acpi_res_io(&sc->sc_res, 1);
    544 	if (io1 == NULL) {
    545 		printf("%s: unable to find csr register resource\n",
    546 		    sc->sc_dev.dv_xname);
    547 		goto out;
    548 	}
    549 	if (bus_space_map(sc->sc_csr_st, io1->ar_base, io1->ar_length,
    550 	    0, &sc->sc_csr_sh) != 0) {
    551 		printf("%s: unable to map csr register\n",
    552 		    sc->sc_dev.dv_xname);
    553 		goto out;
    554 	}
    555 
    556 	/*
    557 	 * evaluate _GLK to see if we should acquire global lock
    558 	 * when accessing the EC.
    559 	 */
    560 	rv = acpi_eval_integer(sc->sc_handle, "_GLK", &v);
    561 	if (ACPI_FAILURE(rv)) {
    562 		if (rv != AE_NOT_FOUND)
    563 			printf("%s: unable to evaluate _GLK: %s\n",
    564 			       sc->sc_dev.dv_xname,
    565 			       AcpiFormatException(rv));
    566 		sc->sc_glk = 0;
    567 	} else
    568 		sc->sc_glk = v;
    569 
    570 	/*
    571 	 * Install GPE handler.
    572 	 *
    573 	 * We evaluate the _GPE method to find the GPE bit used by the
    574 	 * Embedded Controller to signal status (SCI).
    575 	 */
    576 	rv = acpi_eval_integer(sc->sc_handle, "_GPE", &v);
    577 	if (ACPI_FAILURE(rv)) {
    578 		printf("%s: unable to evaluate _GPE: %s\n",
    579 		    sc->sc_dev.dv_xname, AcpiFormatException(rv));
    580 		goto out;
    581 	}
    582 	sc->sc_gpebit = v;
    583 
    584 	/*
    585 	 * Install a handler for this EC's GPE bit.  Note that EC SCIs are
    586 	 * treated as both edge- and level-triggered interrupts; in other words
    587 	 * we clear the status bit immediately after getting an EC-SCI, then
    588 	 * again after we're done processing the event.  This guarantees that
    589 	 * events we cause while performing a transaction (e.g. IBE/OBF) get
    590 	 * cleared before re-enabling the GPE.
    591 	 */
    592 	rv = AcpiInstallGpeHandler(NULL, sc->sc_gpebit,
    593 	     ACPI_EVENT_EDGE_TRIGGERED, EcGpeHandler, sc);
    594 	if (ACPI_FAILURE(rv)) {
    595 		printf("%s: unable to install GPE handler: %s\n",
    596 		    sc->sc_dev.dv_xname, AcpiFormatException(rv));
    597 		goto out;
    598 	}
    599 
    600 	/* Install address space handler. */
    601 	rv = AcpiInstallAddressSpaceHandler(sc->sc_handle,
    602 	     ACPI_ADR_SPACE_EC, EcSpaceHandler, EcSpaceSetup, sc);
    603 	if (ACPI_FAILURE(rv)) {
    604 		printf("%s: unable to install address space handler: %s\n",
    605 		    sc->sc_dev.dv_xname, AcpiFormatException(rv));
    606 		(void)AcpiRemoveGpeHandler(NULL, sc->sc_gpebit,
    607 		    EcGpeHandler);
    608 	}
    609 
    610  out:
    611 	acpi_resource_cleanup(&sc->sc_res);
    612 	return_VOID;
    613 }
    614 
    615 static void
    616 EcGpeQueryHandler(void *Context)
    617 {
    618 	struct acpi_ec_softc *sc = Context;
    619 	UINT8 Data;
    620 	ACPI_STATUS rv;
    621 	char qxx[5];
    622 
    623 	ACPI_FUNCTION_TRACE(__FUNCTION__);
    624 
    625 	for (;;) {
    626 		/*
    627 		 * Check EC_SCI.
    628 		 *
    629 		 * Bail out if the EC_SCI bit of the status register is not
    630 		 * set. Note that this function should only be called when
    631 		 * this bit is set (polling is used to detect IBE/OBF events).
    632 		 *
    633 		 * It is safe to do this without locking the controller, as
    634 		 * it's OK to call EcQuery when there's no data ready; in the
    635 		 * worst case we should just find nothing waiting for us and
    636 		 * bail.
    637 		 */
    638 		if ((EC_CSR_READ(sc) & EC_EVENT_SCI) == 0)
    639 			break;
    640 
    641 		EcLock(sc);
    642 
    643 		/*
    644 		 * Find out why the EC is signalling us
    645 		 */
    646 		rv = EcQuery(sc, &Data);
    647 
    648 		EcUnlock(sc);
    649 
    650 		/*
    651 		 * If we failed to get anything from the EC, give up.
    652 		 */
    653 		if (ACPI_FAILURE(rv)) {
    654 			printf("%s: GPE query failed: %s\n",
    655 			    sc->sc_dev.dv_xname, AcpiFormatException(rv));
    656 			break;
    657 		}
    658 
    659 		/*
    660 		 * Evaluate _Qxx to respond to the controller.
    661 		 */
    662 		sprintf(qxx, "_Q%02X", Data);
    663 		rv = AcpiEvaluateObject(sc->sc_handle, qxx,
    664 		    NULL, NULL);
    665 
    666 		/*
    667 		 * Ignore spurious query requests.
    668 		 */
    669 		if (ACPI_FAILURE(rv) &&
    670 		    (Data != 0 || rv != AE_NOT_FOUND)) {
    671 			printf("%s: evaluation of GPE query method %s "
    672 			    "failed: %s\n", sc->sc_dev.dv_xname, qxx,
    673 			    AcpiFormatException(rv));
    674 		}
    675 	}
    676 
    677 	/* I know I request Level trigger cleanup */
    678 	rv = AcpiClearGpe(NULL, sc->sc_gpebit, ACPI_NOT_ISR);
    679 	if (ACPI_FAILURE(rv))
    680 		printf("%s: AcpiClearGpe failed: %s\n", sc->sc_dev.dv_xname,
    681 		    AcpiFormatException(rv));
    682 
    683 	return_VOID;
    684 }
    685 
    686 static void
    687 EcGpeHandler(void *Context)
    688 {
    689 	struct acpi_ec_softc *sc = Context;
    690 	uint32_t csrvalue;
    691 	ACPI_STATUS rv;
    692 
    693 	/*
    694 	 * If EC is locked, the intr must process EcRead/Write wait only.
    695 	 * Query request must be pending.
    696 	 */
    697 	simple_lock(&sc->sc_slock);
    698 	if (sc->sc_flags & EC_F_TRANSACTION) {
    699 		csrvalue = EC_CSR_READ(sc);
    700 		if (csrvalue & EC_EVENT_SCI)
    701 			sc->sc_flags |= EC_F_PENDQUERY;
    702 
    703 		if ((csrvalue & EC_FLAG_OUTPUT_BUFFER) != 0 ||
    704 		    (csrvalue & EC_FLAG_INPUT_BUFFER) == 0) {
    705 			sc->sc_csrvalue = csrvalue;
    706 			wakeup(&sc->sc_csrvalue);
    707 		}
    708 		simple_unlock(&sc->sc_slock);
    709 	} else {
    710 		simple_unlock(&sc->sc_slock);
    711 		/* Enqueue GpeQuery handler. */
    712 		rv = AcpiOsQueueForExecution(OSD_PRIORITY_HIGH,
    713 		    EcGpeQueryHandler, Context);
    714 		if (ACPI_FAILURE(rv))
    715 			printf("%s: failed to enqueue query handler: %s\n",
    716 			    sc->sc_dev.dv_xname, AcpiFormatException(rv));
    717 	}
    718 }
    719 
    720 static ACPI_STATUS
    721 EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function, void *Context,
    722     void **RegionContext)
    723 {
    724 
    725 	ACPI_FUNCTION_TRACE(__FUNCTION__);
    726 
    727 	/*
    728 	 * Just pass the context through, there's nothing to do here.
    729 	 */
    730 	if (Function == ACPI_REGION_DEACTIVATE)
    731 		*RegionContext = NULL;
    732 	else
    733 		*RegionContext = Context;
    734 
    735 	return_ACPI_STATUS(AE_OK);
    736 }
    737 
    738 static ACPI_STATUS
    739 EcSpaceHandler(UINT32 Function, ACPI_PHYSICAL_ADDRESS Address, UINT32 width,
    740     ACPI_INTEGER *Value, void *Context, void *RegionContext)
    741 {
    742 	struct acpi_ec_softc *sc = Context;
    743 	ACPI_STATUS rv = AE_OK;
    744 	EC_REQUEST EcRequest;
    745 	int i;
    746 
    747 	ACPI_FUNCTION_TRACE_U32(__FUNCTION__, (UINT32)Address);
    748 
    749 	if ((Address > 0xFF) || (width % 8 != 0) || (Value == NULL) ||
    750 	    (Context == NULL))
    751 		return_ACPI_STATUS(AE_BAD_PARAMETER);
    752 
    753 	switch (Function) {
    754 	case ACPI_READ:
    755 		EcRequest.Command = EC_COMMAND_READ;
    756 		EcRequest.Address = Address;
    757 		(*Value) = 0;
    758 		break;
    759 
    760 	case ACPI_WRITE:
    761 		EcRequest.Command = EC_COMMAND_WRITE;
    762 		EcRequest.Address = Address;
    763 		break;
    764 
    765 	default:
    766 		printf("%s: invalid Address Space function: %d\n",
    767 		    sc->sc_dev.dv_xname, Function);
    768 		return_ACPI_STATUS(AE_BAD_PARAMETER);
    769 	}
    770 
    771 	/*
    772 	 * Perform the transaction.
    773 	 */
    774 	for (i = 0; i < width; i += 8) {
    775 		if (Function == ACPI_READ)
    776 			EcRequest.Data = 0;
    777 		else
    778 			EcRequest.Data = (UINT8)((*Value) >> i);
    779 
    780 		rv = EcTransaction(sc, &EcRequest);
    781 		if (ACPI_FAILURE(rv))
    782 			break;
    783 
    784 		(*Value) |= (UINT32)EcRequest.Data << i;
    785 		if (++EcRequest.Address == 0)
    786 			return_ACPI_STATUS(AE_BAD_PARAMETER);
    787 	}
    788 
    789 	return_ACPI_STATUS(rv);
    790 }
    791 
    792 static ACPI_STATUS
    793 EcWaitEventIntr(struct acpi_ec_softc *sc, EC_EVENT Event)
    794 {
    795 	EC_STATUS EcStatus;
    796 	int i;
    797 
    798 	ACPI_FUNCTION_TRACE_U32(__FUNCTION__, (UINT32)Event);
    799 
    800 	/* XXX Need better test for "yes, you have interrupts". */
    801 	if (cold)
    802 		return_ACPI_STATUS(EcWaitEvent(sc, Event));
    803 
    804 	if (EcIsLocked(sc) == 0)
    805 		printf("%s: EcWaitEventIntr called without EC lock!\n",
    806 		    sc->sc_dev.dv_xname);
    807 
    808 	EcStatus = EC_CSR_READ(sc);
    809 
    810 	/* Too long? */
    811 	for (i = 0; i < 10; i++) {
    812 		/* Check EC status against the desired event. */
    813 		if ((Event == EC_EVENT_OUTPUT_BUFFER_FULL) &&
    814 		    (EcStatus & EC_FLAG_OUTPUT_BUFFER) != 0)
    815 			return_ACPI_STATUS(AE_OK);
    816 
    817 		if ((Event == EC_EVENT_INPUT_BUFFER_EMPTY) &&
    818 		    (EcStatus & EC_FLAG_INPUT_BUFFER) == 0)
    819 			return_ACPI_STATUS(AE_OK);
    820 
    821 		sc->sc_csrvalue = 0;
    822 		/* XXXJRT Sleeping with a lock held? */
    823 		if (tsleep(&sc->sc_csrvalue, 0, "EcWait", 1) != EWOULDBLOCK)
    824 			EcStatus = sc->sc_csrvalue;
    825 		else
    826 			EcStatus = EC_CSR_READ(sc);
    827 	}
    828 	return_ACPI_STATUS(AE_ERROR);
    829 }
    830 
    831 static ACPI_STATUS
    832 EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event)
    833 {
    834 	EC_STATUS EcStatus;
    835 	UINT32 i = 0;
    836 
    837 	if (EcIsLocked(sc) == 0)
    838 		printf("%s: EcWaitEvent called without EC lock!\n",
    839 		    sc->sc_dev.dv_xname);
    840 
    841 	/*
    842 	 * Stall 1us:
    843 	 * ----------
    844 	 * Stall for 1 microsecond before reading the status register
    845 	 * for the first time.  This allows the EC to set the IBF/OBF
    846 	 * bit to its proper state.
    847 	 *
    848 	 * XXX it is not clear why we read the CSR twice.
    849 	 */
    850 	AcpiOsStall(1);
    851 	EcStatus = EC_CSR_READ(sc);
    852 
    853 	/*
    854 	 * Wait For Event:
    855 	 * ---------------
    856 	 * Poll the EC status register to detect completion of the last
    857 	 * command.  Wait up to 10ms (in 100us chunks) for this to occur.
    858 	 */
    859 	for (i = 0; i < 100; i++) {
    860 		EcStatus = EC_CSR_READ(sc);
    861 
    862 		if ((Event == EC_EVENT_OUTPUT_BUFFER_FULL) &&
    863 		    (EcStatus & EC_FLAG_OUTPUT_BUFFER) != 0)
    864 			return AE_OK;
    865 
    866 		if ((Event == EC_EVENT_INPUT_BUFFER_EMPTY) &&
    867 		    (EcStatus & EC_FLAG_INPUT_BUFFER) == 0)
    868 			return AE_OK;
    869 
    870 		AcpiOsStall(10);
    871 	}
    872 
    873 	return AE_ERROR;
    874 }
    875 
    876 static ACPI_STATUS
    877 EcQuery(struct acpi_ec_softc *sc, UINT8 *Data)
    878 {
    879 	ACPI_STATUS rv;
    880 
    881 	if (EcIsLocked(sc) == 0)
    882 		printf("%s: EcQuery called without EC lock!\n",
    883 		    sc->sc_dev.dv_xname);
    884 
    885 	EC_CSR_WRITE(sc, EC_COMMAND_QUERY);
    886 	rv = EcWaitEventIntr(sc, EC_EVENT_OUTPUT_BUFFER_FULL);
    887 	if (ACPI_SUCCESS(rv))
    888 		*Data = EC_DATA_READ(sc);
    889 
    890 	if (ACPI_FAILURE(rv))
    891 		printf("%s: timed out waiting for EC to respond to "
    892 		    "EC_COMMAND_QUERY\n", sc->sc_dev.dv_xname);
    893 
    894 	return rv;
    895 }
    896 
    897 static ACPI_STATUS
    898 EcTransaction(struct acpi_ec_softc *sc, EC_REQUEST *EcRequest)
    899 {
    900 	ACPI_STATUS rv;
    901 
    902 	EcLock(sc);
    903 
    904 	/*
    905 	 * Perform the transaction.
    906 	 */
    907 	switch (EcRequest->Command) {
    908 	case EC_COMMAND_READ:
    909 		rv = EcRead(sc, EcRequest->Address, &(EcRequest->Data));
    910 		break;
    911 
    912 	case EC_COMMAND_WRITE:
    913 		rv = EcWrite(sc, EcRequest->Address, &(EcRequest->Data));
    914 		break;
    915 
    916 	default:
    917 		rv = AE_SUPPORT;
    918 		break;
    919 	}
    920 
    921 	EcUnlock(sc);
    922 
    923 	return rv;
    924 }
    925 
    926 static ACPI_STATUS
    927 EcRead(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data)
    928 {
    929 	ACPI_STATUS rv;
    930 
    931 	if (EcIsLocked(sc) == 0)
    932 		printf("%s: EcRead called without EC lock!\n",
    933 		    sc->sc_dev.dv_xname);
    934 
    935 	/* EcBurstEnable(EmbeddedController); */
    936 
    937 	EC_CSR_WRITE(sc, EC_COMMAND_READ);
    938 	if ((rv = EcWaitEventIntr(sc, EC_EVENT_INPUT_BUFFER_EMPTY)) !=
    939 	    AE_OK) {
    940 		printf("%s: EcRead: timeout waiting for EC to process "
    941 		    "read command\n", sc->sc_dev.dv_xname);
    942 		return rv;
    943 	}
    944 
    945 	EC_DATA_WRITE(sc, Address);
    946 	if ((rv = EcWaitEventIntr(sc, EC_EVENT_OUTPUT_BUFFER_FULL)) !=
    947 	    AE_OK) {
    948 		printf("%s: EcRead: timeout waiting for EC to send data\n",
    949 		    sc->sc_dev.dv_xname);
    950 		return rv;
    951 	}
    952 
    953 	(*Data) = EC_DATA_READ(sc);
    954 
    955 	/* EcBurstDisable(EmbeddedController); */
    956 
    957 	return AE_OK;
    958 }
    959 
    960 static ACPI_STATUS
    961 EcWrite(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data)
    962 {
    963 	ACPI_STATUS rv;
    964 
    965 	if (EcIsLocked(sc) == 0)
    966 		printf("%s: EcWrite called without EC lock!\n",
    967 		    sc->sc_dev.dv_xname);
    968 
    969 	/* EcBurstEnable(EmbeddedController); */
    970 
    971 	EC_CSR_WRITE(sc, EC_COMMAND_WRITE);
    972 	if ((rv = EcWaitEventIntr(sc, EC_EVENT_INPUT_BUFFER_EMPTY)) !=
    973 	    AE_OK) {
    974 		printf("%s: EcWrite: timeout waiting for EC to process "
    975 		    "write command\n", sc->sc_dev.dv_xname);
    976 		return rv;
    977 	}
    978 
    979 	EC_DATA_WRITE(sc, Address);
    980 	if ((rv = EcWaitEventIntr(sc, EC_EVENT_INPUT_BUFFER_EMPTY)) !=
    981 	    AE_OK) {
    982 		printf("%s: EcWrite: timeout waiting for EC to process "
    983 		    "address\n", sc->sc_dev.dv_xname);
    984 		return rv;
    985 	}
    986 
    987 	EC_DATA_WRITE(sc, *Data);
    988 	if ((rv = EcWaitEventIntr(sc, EC_EVENT_INPUT_BUFFER_EMPTY)) !=
    989 	    AE_OK) {
    990 		printf("%s: EcWrite: timeout waiting for EC to process "
    991 		    "data\n", sc->sc_dev.dv_xname);
    992 		return rv;
    993 	}
    994 
    995 	/* EcBurstDisable(EmbeddedController); */
    996 
    997 	return AE_OK;
    998 }
    999