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