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