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