Home | History | Annotate | Line # | Download | only in acpi
acpi_ec.c revision 1.8
      1 /*	$NetBSD: acpi_ec.c,v 1.8 2002/10/15 20:53:37 tshiozak 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.8 2002/10/15 20:53:37 tshiozak Exp $");
    176 
    177 #include <sys/param.h>
    178 #include <sys/systm.h>
    179 #include <sys/device.h>
    180 #include <sys/proc.h>
    181 #include <sys/kernel.h>
    182 
    183 #include <machine/bus.h>
    184 
    185 #include <dev/acpi/acpica.h>
    186 #include <dev/acpi/acpireg.h>
    187 #include <dev/acpi/acpivar.h>
    188 #include <dev/acpi/acpi_ecreg.h>
    189 
    190 #define _COMPONENT	ACPI_EC_COMPONENT
    191 ACPI_MODULE_NAME("EC")
    192 
    193 struct acpi_ec_softc {
    194 	struct device	sc_dev;		/* base device glue */
    195 	struct acpi_devnode *sc_node;	/* our ACPI devnode */
    196 
    197 	struct acpi_resources sc_res;	/* our bus resources */
    198 
    199 	UINT32		sc_gpebit;	/* our GPE interrupt bit */
    200 
    201 	bus_space_tag_t	sc_data_st;	/* space tag for data register */
    202 	bus_space_handle_t sc_data_sh;	/* space handle for data register */
    203 
    204 	bus_space_tag_t	sc_csr_st;	/* space tag for control register */
    205 	bus_space_handle_t sc_csr_sh;	/* space handle for control register */
    206 
    207 	int		sc_flags;	/* see below */
    208 
    209 	uint32_t	sc_csrvalue;	/* saved control register */
    210 };
    211 
    212 #define	EC_F_LOCKED	0x01		/* EC is locked */
    213 #define	EC_F_PENDQUERY	0x02		/* query is pending */
    214 
    215 #define	EC_LOCK_TIMEOUT	1000		/* 1ms */
    216 
    217 #define	EC_DATA_READ(sc)						\
    218 	bus_space_read_1((sc)->sc_data_st, (sc)->sc_data_sh, 0)
    219 #define	EC_DATA_WRITE(sc, v)						\
    220 	bus_space_write_1((sc)->sc_data_st, (sc)->sc_data_sh, 0, (v))
    221 
    222 #define	EC_CSR_READ(sc)							\
    223 	bus_space_read_1((sc)->sc_csr_st, (sc)->sc_csr_sh, 0)
    224 #define	EC_CSR_WRITE(sc, v)						\
    225 	bus_space_write_1((sc)->sc_csr_st, (sc)->sc_csr_sh, 0, (v))
    226 
    227 static UINT32 glk;			/* XXX XXX XXX */
    228 
    229 static __inline ACPI_STATUS
    230 EcLock(struct acpi_ec_softc *sc)
    231 {
    232 	ACPI_STATUS status;
    233 
    234 	status = AcpiAcquireGlobalLock(EC_LOCK_TIMEOUT, &glk);
    235 	if (status == AE_OK)
    236 		(sc)->sc_flags |= EC_F_LOCKED;
    237 	return (status);
    238 }
    239 
    240 static __inline void
    241 EcUnlock(struct acpi_ec_softc *sc)
    242 {
    243 
    244 	(sc)->sc_flags &= ~EC_F_LOCKED;
    245 	AcpiReleaseGlobalLock(glk);
    246 }
    247 
    248 static __inline int
    249 EcIsLocked(struct acpi_ec_softc *sc)
    250 {
    251 
    252 	return (((sc)->sc_flags & EC_F_LOCKED) != 0);
    253 }
    254 
    255 typedef struct {
    256 	EC_COMMAND	Command;
    257 	UINT8		Address;
    258 	UINT8		Data;
    259 } EC_REQUEST;
    260 
    261 static void		EcGpeHandler(void *Context);
    262 static ACPI_STATUS	EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function,
    263 			    void *Context, void **return_Context);
    264 static ACPI_STATUS	EcSpaceHandler(UINT32 Function,
    265 			    ACPI_PHYSICAL_ADDRESS Address, UINT32 width,
    266 			    ACPI_INTEGER *Value, void *Context,
    267 			    void *RegionContext);
    268 
    269 static ACPI_STATUS	EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event);
    270 static ACPI_STATUS	EcQuery(struct acpi_ec_softc *sc, UINT8 *Data);
    271 static ACPI_STATUS	EcTransaction(struct acpi_ec_softc *sc,
    272 			    EC_REQUEST *EcRequest);
    273 static ACPI_STATUS	EcRead(struct acpi_ec_softc *sc, UINT8 Address,
    274 			    UINT8 *Data);
    275 static ACPI_STATUS	EcWrite(struct acpi_ec_softc *sc, UINT8 Address,
    276 			    UINT8 *Data);
    277 
    278 int	acpiec_match(struct device *, struct cfdata *, void *);
    279 void	acpiec_attach(struct device *, struct device *, void *);
    280 
    281 CFATTACH_DECL(acpiec, sizeof(struct acpi_ec_softc),
    282     acpiec_match, acpiec_attach, NULL, NULL);
    283 
    284 /*
    285  * acpiec_match:
    286  *
    287  *	Autoconfiguration `match' routine.
    288  */
    289 int
    290 acpiec_match(struct device *parent, struct cfdata *match, void *aux)
    291 {
    292 	struct acpi_attach_args *aa = aux;
    293 
    294 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
    295 		return (0);
    296 
    297 	if (strcmp(aa->aa_node->ad_devinfo.HardwareId, "PNP0C09") == 0)
    298 		return (1);
    299 
    300 	return (0);
    301 }
    302 
    303 /*
    304  * acpiec_attach:
    305  *
    306  *	Autoconfiguration `attach' routine.
    307  */
    308 void
    309 acpiec_attach(struct device *parent, struct device *self, void *aux)
    310 {
    311 	struct acpi_ec_softc *sc = (void *) self;
    312 	struct acpi_attach_args *aa = aux;
    313 	struct acpi_io *io0, *io1;
    314 	ACPI_STATUS rv;
    315 
    316 	ACPI_FUNCTION_TRACE(__FUNCTION__);
    317 
    318 	printf(": ACPI Embedded Controller\n");
    319 
    320 	sc->sc_node = aa->aa_node;
    321 
    322 	/* Parse our resources. */
    323 	ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "parsing EC resources\n"));
    324 	rv = acpi_resource_parse(&sc->sc_dev, sc->sc_node, &sc->sc_res,
    325 	    &acpi_resource_parse_ops_default);
    326 	if (rv != AE_OK) {
    327 		printf("%s: unable to parse resources: %d\n",
    328 		    sc->sc_dev.dv_xname, rv);
    329 		return;
    330 	}
    331 
    332 	sc->sc_data_st = aa->aa_iot;
    333 	io0 = acpi_res_io(&sc->sc_res, 0);
    334 	if (io0 == NULL) {
    335 		printf("%s: unable to find data register resource\n",
    336 		    sc->sc_dev.dv_xname);
    337 		return;
    338 	}
    339 	if (bus_space_map(sc->sc_data_st, io0->ar_base, io0->ar_length,
    340 	    0, &sc->sc_data_sh) != 0) {
    341 		printf("%s: unable to map data register\n",
    342 		    sc->sc_dev.dv_xname);
    343 		return;
    344 	}
    345 
    346 	sc->sc_csr_st = aa->aa_iot;
    347 	io1 = acpi_res_io(&sc->sc_res, 1);
    348 	if (io1 == NULL) {
    349 		printf("%s: unable to find csr register resource\n",
    350 		    sc->sc_dev.dv_xname);
    351 		return;
    352 	}
    353 	if (bus_space_map(sc->sc_csr_st, io1->ar_base, io1->ar_length,
    354 	    0, &sc->sc_csr_sh) != 0) {
    355 		printf("%s: unable to map csr register\n",
    356 		    sc->sc_dev.dv_xname);
    357 		return;
    358 	}
    359 
    360 	/*
    361 	 * Install GPE handler.
    362 	 *
    363 	 * We evaluate the _GPE method to find the GPE bit used by the
    364 	 * Embedded Controller to signal status (SCI).
    365 	 */
    366 	if ((rv = acpi_eval_integer(sc->sc_node->ad_handle, "_GPE",
    367 	     &sc->sc_gpebit)) != AE_OK) {
    368 		printf("%s: unable to evaluate _GPE: %d\n",
    369 		    sc->sc_dev.dv_xname, rv);
    370 		return;
    371 	}
    372 
    373 	/*
    374 	 * Install a handler for this EC's GPE bit.  Note that EC SCIs are
    375 	 * treated as both edge- and level-triggered interrupts; in other words
    376 	 * we clear the status bit immediately after getting an EC-SCI, then
    377 	 * again after we're done processing the event.  This guarantees that
    378 	 * events we cause while performing a transaction (e.g. IBE/OBF) get
    379 	 * cleared before re-enabling the GPE.
    380 	 */
    381 	if ((rv = AcpiInstallGpeHandler(sc->sc_gpebit,
    382 	     ACPI_EVENT_LEVEL_TRIGGERED | ACPI_EVENT_EDGE_TRIGGERED,
    383 	     EcGpeHandler, sc)) != AE_OK) {
    384 		printf("%s: unable to install GPE handler: %d\n",
    385 		    sc->sc_dev.dv_xname, rv);
    386 		return;
    387 	}
    388 
    389 	/* Install address space handler. */
    390 	if ((rv = AcpiInstallAddressSpaceHandler(sc->sc_node->ad_handle,
    391 	     ACPI_ADR_SPACE_EC, EcSpaceHandler, EcSpaceSetup, sc)) != AE_OK) {
    392 		printf("%s: unable to install address space handler: %d\n",
    393 		    sc->sc_dev.dv_xname, rv);
    394 		return;
    395 	}
    396 
    397 	return_VOID;
    398 }
    399 
    400 static void
    401 EcGpeQueryHandler(void *Context)
    402 {
    403 	struct acpi_ec_softc *sc = Context;
    404 	UINT8 Data;
    405 	ACPI_STATUS Status;
    406 	char qxx[5];
    407 
    408 	ACPI_FUNCTION_TRACE(__FUNCTION__);
    409 
    410 	for (;;) {
    411 		/*
    412 		 * Check EC_SCI.
    413 		 *
    414 		 * Bail out if the EC_SCI bit of the status register is not
    415 		 * set. Note that this function should only be called when
    416 		 * this bit is set (polling is used to detect IBE/OBF events).
    417 		 *
    418 		 * It is safe to do this without locking the controller, as
    419 		 * it's OK to call EcQuery when there's no data ready; in the
    420 		 * worst case we should just find nothing waiting for us and
    421 		 * bail.
    422 		 */
    423 		if ((EC_CSR_READ(sc) & EC_EVENT_SCI) == 0)
    424 			break;
    425 
    426 		/*
    427 		 * Find out why the EC is signalling us
    428 		 */
    429 		Status = EcQuery(sc, &Data);
    430 
    431 		/*
    432 		 * If we failed to get anything from the EC, give up.
    433 		 */
    434 		if (Status != AE_OK) {
    435 			printf("%s: GPE query failed: %d\n",
    436 			    sc->sc_dev.dv_xname, Status);
    437 			break;
    438 		}
    439 
    440 		/*
    441 		 * Evaluate _Qxx to respond to the controller.
    442 		 */
    443 		sprintf(qxx, "_Q%02x", Data);
    444 		strupr(qxx);
    445 		Status = AcpiEvaluateObject(sc->sc_node->ad_handle, qxx,
    446 		    NULL, NULL);
    447 
    448 		/*
    449 		 * Ignore spurious query requests.
    450 		 */
    451 		if (Status != AE_OK &&
    452 		    (Data != 0 || Status != AE_NOT_FOUND)) {
    453 			printf("%s: evaluation of GPE query method %s "
    454 			    "failed: %d\n", sc->sc_dev.dv_xname, qxx, Status);
    455 		}
    456 	}
    457 
    458 	/* I know I request Level trigger cleanup */
    459 	if (AcpiClearEvent(sc->sc_gpebit, ACPI_EVENT_GPE) != AE_OK)
    460 		printf("%s: AcpiClearEvent failed\n", sc->sc_dev.dv_xname);
    461 	if (AcpiEnableEvent(sc->sc_gpebit, ACPI_EVENT_GPE, 0) != AE_OK)
    462 		printf("%s: AcpiEnableEvent failed\n", sc->sc_dev.dv_xname);
    463 
    464 	return_VOID;
    465 }
    466 
    467 static void
    468 EcGpeHandler(void *Context)
    469 {
    470 	struct acpi_ec_softc *sc = Context;
    471 	uint32_t csrvalue;
    472 
    473 	/*
    474 	 * If EC is locked, the intr must process EcRead/Write wait only.
    475 	 * Query request must be pending.
    476 	 */
    477 	if (EcIsLocked(sc)) {
    478 		csrvalue = EC_CSR_READ(sc);
    479 		if (csrvalue & EC_EVENT_SCI)
    480 			sc->sc_flags |= EC_F_PENDQUERY;
    481 
    482 		if ((csrvalue & EC_FLAG_OUTPUT_BUFFER) != 0 ||
    483 		    (csrvalue & EC_FLAG_INPUT_BUFFER) == 0) {
    484 			sc->sc_csrvalue = csrvalue;
    485 			wakeup(&sc->sc_csrvalue);
    486 		}
    487 	} else {
    488 		/* Enqueue GpeQuery handler. */
    489 		if (AcpiOsQueueForExecution(OSD_PRIORITY_HIGH,
    490 		    EcGpeQueryHandler, Context) != AE_OK) {
    491 			printf("%s: failed to enqueue query handler\n",
    492 			    sc->sc_dev.dv_xname);
    493 		}
    494 	}
    495 }
    496 
    497 static ACPI_STATUS
    498 EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function, void *Context,
    499     void **RegionContext)
    500 {
    501 
    502 	ACPI_FUNCTION_TRACE(__FUNCTION__);
    503 
    504 	/*
    505 	 * Just pass the context through, there's nothing to do here.
    506 	 */
    507 	*RegionContext = Context;
    508 
    509 	return_ACPI_STATUS(AE_OK);
    510 }
    511 
    512 static ACPI_STATUS
    513 EcSpaceHandler(UINT32 Function, ACPI_PHYSICAL_ADDRESS Address, UINT32 width,
    514     ACPI_INTEGER *Value, void *Context, void *RegionContext)
    515 {
    516 	struct acpi_ec_softc *sc = Context;
    517 	ACPI_STATUS Status = AE_OK;
    518 	EC_REQUEST EcRequest;
    519 	int i;
    520 
    521 	ACPI_FUNCTION_TRACE_U32(__FUNCTION__, (UINT32)Address);
    522 
    523 	if ((Address > 0xFF) || (width % 8 != 0) || (Value == NULL) ||
    524 	    (Context == NULL))
    525 		return_ACPI_STATUS(AE_BAD_PARAMETER);
    526 
    527 	switch (Function) {
    528 	case ACPI_READ:
    529 		EcRequest.Command = EC_COMMAND_READ;
    530 		EcRequest.Address = Address;
    531 		(*Value) = 0;
    532 		break;
    533 
    534 	case ACPI_WRITE:
    535 		EcRequest.Command = EC_COMMAND_WRITE;
    536 		EcRequest.Address = Address;
    537 		break;
    538 
    539 	default:
    540 		printf("%s: invalid Address Space function: %d\n",
    541 		    sc->sc_dev.dv_xname, Function);
    542 		return_ACPI_STATUS(AE_BAD_PARAMETER);
    543 	}
    544 
    545 	/*
    546 	 * Perform the transaction.
    547 	 */
    548 	for (i = 0; i < width; i += 8) {
    549 		if (Function == ACPI_READ)
    550 			EcRequest.Data = 0;
    551 		else
    552 			EcRequest.Data = (UINT8)((*Value) >> i);
    553 
    554 		if ((Status = EcTransaction(sc, &EcRequest)) != AE_OK)
    555 			break;
    556 
    557 		(*Value) |= (UINT32)EcRequest.Data << i;
    558 		if (++EcRequest.Address == 0)
    559 			return_ACPI_STATUS(AE_BAD_PARAMETER);
    560 	}
    561 
    562 	return_ACPI_STATUS(Status);
    563 }
    564 
    565 static ACPI_STATUS
    566 EcWaitEventIntr(struct acpi_ec_softc *sc, EC_EVENT Event)
    567 {
    568 	EC_STATUS EcStatus;
    569 	int i;
    570 
    571 	ACPI_FUNCTION_TRACE_U32(__FUNCTION__, (UINT32)Event);
    572 
    573 	/* XXX Need better test for "yes, you have interrupts". */
    574 	if (cold)
    575 		return_ACPI_STATUS(EcWaitEvent(sc, Event));
    576 
    577 	if (EcIsLocked(sc) == 0)
    578 		printf("%s: EcWaitEventIntr called without EC lock!\n",
    579 		    sc->sc_dev.dv_xname);
    580 
    581 	EcStatus = EC_CSR_READ(sc);
    582 
    583 	/* Too long? */
    584 	for (i = 0; i < 10; i++) {
    585 		/* Check EC status against the desired event. */
    586 		if ((Event == EC_EVENT_OUTPUT_BUFFER_FULL) &&
    587 		    (EcStatus & EC_FLAG_OUTPUT_BUFFER) != 0)
    588 			return_ACPI_STATUS(AE_OK);
    589 
    590 		if ((Event == EC_EVENT_INPUT_BUFFER_EMPTY) &&
    591 		    (EcStatus & EC_FLAG_INPUT_BUFFER) == 0)
    592 			return_ACPI_STATUS(AE_OK);
    593 
    594 		sc->sc_csrvalue = 0;
    595 		/* XXXJRT Sleeping with a lock held? */
    596 		if (tsleep(&sc->sc_csrvalue, 0, "EcWait", 1) != EWOULDBLOCK)
    597 			EcStatus = sc->sc_csrvalue;
    598 		else
    599 			EcStatus = EC_CSR_READ(sc);
    600 	}
    601 	return_ACPI_STATUS(AE_ERROR);
    602 }
    603 
    604 static ACPI_STATUS
    605 EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event)
    606 {
    607 	EC_STATUS EcStatus;
    608 	UINT32 i = 0;
    609 
    610 	if (EcIsLocked(sc) == 0)
    611 		printf("%s: EcWaitEvent called without EC lock!\n",
    612 		    sc->sc_dev.dv_xname);
    613 
    614 	/*
    615 	 * Stall 1us:
    616 	 * ----------
    617 	 * Stall for 1 microsecond before reading the status register
    618 	 * for the first time.  This allows the EC to set the IBF/OBF
    619 	 * bit to its proper state.
    620 	 *
    621 	 * XXX it is not clear why we read the CSR twice.
    622 	 */
    623 	AcpiOsStall(1);
    624 	EcStatus = EC_CSR_READ(sc);
    625 
    626 	/*
    627 	 * Wait For Event:
    628 	 * ---------------
    629 	 * Poll the EC status register to detect completion of the last
    630 	 * command.  Wait up to 10ms (in 100us chunks) for this to occur.
    631 	 */
    632 	for (i = 0; i < 100; i++) {
    633 		EcStatus = EC_CSR_READ(sc);
    634 
    635 		if ((Event == EC_EVENT_OUTPUT_BUFFER_FULL) &&
    636 		    (EcStatus & EC_FLAG_OUTPUT_BUFFER) != 0)
    637 			return (AE_OK);
    638 
    639 		if ((Event == EC_EVENT_INPUT_BUFFER_EMPTY) &&
    640 		    (EcStatus & EC_FLAG_INPUT_BUFFER) == 0)
    641 			return (AE_OK);
    642 
    643 		AcpiOsStall(10);
    644 	}
    645 
    646 	return (AE_ERROR);
    647 }
    648 
    649 static ACPI_STATUS
    650 EcQuery(struct acpi_ec_softc *sc, UINT8 *Data)
    651 {
    652 	ACPI_STATUS Status;
    653 
    654 	if ((Status = EcLock(sc)) != AE_OK)
    655 		return (Status);
    656 
    657 	EC_CSR_WRITE(sc, EC_COMMAND_QUERY);
    658 	Status = EcWaitEvent(sc, EC_EVENT_OUTPUT_BUFFER_FULL);
    659 	if (Status == AE_OK)
    660 		*Data = EC_DATA_READ(sc);
    661 
    662 	EcUnlock(sc);
    663 
    664 	if (Status != AE_OK)
    665 		printf("%s: timed out waiting for EC to respond to "
    666 		    "EC_COMMAND_QUERY\n", sc->sc_dev.dv_xname);
    667 
    668 	return (Status);
    669 }
    670 
    671 static ACPI_STATUS
    672 EcTransaction(struct acpi_ec_softc *sc, EC_REQUEST *EcRequest)
    673 {
    674 	ACPI_STATUS Status;
    675 
    676 	if ((Status = EcLock(sc)) != AE_OK)
    677 		return (Status);
    678 
    679 	/*
    680 	 * Perform the transaction.
    681 	 */
    682 	switch (EcRequest->Command) {
    683 	case EC_COMMAND_READ:
    684 		Status = EcRead(sc, EcRequest->Address, &(EcRequest->Data));
    685 		break;
    686 
    687 	case EC_COMMAND_WRITE:
    688 		Status = EcWrite(sc, EcRequest->Address, &(EcRequest->Data));
    689 		break;
    690 
    691 	default:
    692 		Status = AE_SUPPORT;
    693 		break;
    694 	}
    695 
    696 	/*
    697 	 * Clear & Re-Enable the EC GPE:
    698 	 * -----------------------------
    699 	 * 'Consume' any EC GPE events that we generated while performing
    700 	 * the transaction (e.g. IBF/OBF). Clearing the GPE here shouldn't
    701 	 * have an adverse affect on outstanding EC-SCI's, as the source
    702 	 * (EC-SCI) will still be high and thus should trigger the GPE
    703 	 * immediately after we re-enabling it.
    704 	 */
    705 	if (sc->sc_flags & EC_F_PENDQUERY) {
    706 		if (AcpiOsQueueForExecution(OSD_PRIORITY_HIGH,
    707 		    EcGpeQueryHandler, sc) != AE_OK)
    708 			printf("%s: unable to queue pending query\n",
    709 			    sc->sc_dev.dv_xname);
    710 		sc->sc_flags &= ~EC_F_PENDQUERY;
    711 	}
    712 
    713 	if (AcpiClearEvent(sc->sc_gpebit, ACPI_EVENT_GPE) != AE_OK)
    714 		printf("%s: EcRequest: unable to clear EC GPE\n",
    715 		    sc->sc_dev.dv_xname);
    716 	if (AcpiEnableEvent(sc->sc_gpebit, ACPI_EVENT_GPE, 0) != AE_OK)
    717 		printf("%s: EcRequest: unable to reenable EC GPE\n",
    718 		    sc->sc_dev.dv_xname);
    719 
    720 	EcUnlock(sc);
    721 
    722 	return(Status);
    723 }
    724 
    725 static ACPI_STATUS
    726 EcRead(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data)
    727 {
    728 	ACPI_STATUS Status;
    729 
    730 	if (EcIsLocked(sc) == 0)
    731 		printf("%s: EcRead called without EC lock!\n",
    732 		    sc->sc_dev.dv_xname);
    733 
    734 	/* EcBurstEnable(EmbeddedController); */
    735 
    736 	EC_CSR_WRITE(sc, EC_COMMAND_READ);
    737 	if ((Status = EcWaitEventIntr(sc, EC_EVENT_INPUT_BUFFER_EMPTY)) !=
    738 	    AE_OK) {
    739 		printf("%s: EcRead: timeout waiting for EC to process "
    740 		    "read command\n", sc->sc_dev.dv_xname);
    741 		return (Status);
    742 	}
    743 
    744 	EC_DATA_WRITE(sc, Address);
    745 	if ((Status = EcWaitEventIntr(sc, EC_EVENT_OUTPUT_BUFFER_FULL)) !=
    746 	    AE_OK) {
    747 		printf("%s: EcRead: timeout waiting for EC to send data\n",
    748 		    sc->sc_dev.dv_xname);
    749 		return (Status);
    750 	}
    751 
    752 	(*Data) = EC_DATA_READ(sc);
    753 
    754 	/* EcBurstDisable(EmbeddedController); */
    755 
    756 	return (AE_OK);
    757 }
    758 
    759 static ACPI_STATUS
    760 EcWrite(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data)
    761 {
    762 	ACPI_STATUS Status;
    763 
    764 	if (EcIsLocked(sc) == 0)
    765 		printf("%s: EcWrite called without EC lock!\n",
    766 		    sc->sc_dev.dv_xname);
    767 
    768 	/* EcBurstEnable(EmbeddedController); */
    769 
    770 	EC_CSR_WRITE(sc, EC_COMMAND_WRITE);
    771 	if ((Status = EcWaitEventIntr(sc, EC_EVENT_INPUT_BUFFER_EMPTY)) !=
    772 	    AE_OK) {
    773 		printf("%s: EcWrite: timeout waiting for EC to process "
    774 		    "write command\n", sc->sc_dev.dv_xname);
    775 		return (Status);
    776 	}
    777 
    778 	EC_DATA_WRITE(sc, Address);
    779 	if ((Status = EcWaitEventIntr(sc, EC_EVENT_INPUT_BUFFER_EMPTY)) !=
    780 	    AE_OK) {
    781 		printf("%s: EcWrite: timeout waiting for EC to process "
    782 		    "address\n", sc->sc_dev.dv_xname);
    783 		return (Status);
    784 	}
    785 
    786 	EC_DATA_WRITE(sc, *Data);
    787 	if ((Status = EcWaitEventIntr(sc, EC_EVENT_INPUT_BUFFER_EMPTY)) !=
    788 	    AE_OK) {
    789 		printf("%s: EcWrite: timeout waiting for EC to process "
    790 		    "data\n", sc->sc_dev.dv_xname);
    791 		return (Status);
    792 	}
    793 
    794 	/* EcBurstDisable(EmbeddedController); */
    795 
    796 	return (AE_OK);
    797 }
    798