xref: /freebsd/sys/dev/acpica/acpi_ec.c (revision f8335e3a97801ebb49d091a7e09a33711d56a76c)
115e32d5dSMike Smith /*-
21f04e8f5SNate Lawson  * Copyright (c) 2003 Nate Lawson
315e32d5dSMike Smith  * Copyright (c) 2000 Michael Smith
415e32d5dSMike Smith  * Copyright (c) 2000 BSDi
515e32d5dSMike Smith  * All rights reserved.
615e32d5dSMike Smith  *
715e32d5dSMike Smith  * Redistribution and use in source and binary forms, with or without
815e32d5dSMike Smith  * modification, are permitted provided that the following conditions
915e32d5dSMike Smith  * are met:
1015e32d5dSMike Smith  * 1. Redistributions of source code must retain the above copyright
1115e32d5dSMike Smith  *    notice, this list of conditions and the following disclaimer.
1215e32d5dSMike Smith  * 2. Redistributions in binary form must reproduce the above copyright
1315e32d5dSMike Smith  *    notice, this list of conditions and the following disclaimer in the
1415e32d5dSMike Smith  *    documentation and/or other materials provided with the distribution.
1515e32d5dSMike Smith  *
1615e32d5dSMike Smith  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1715e32d5dSMike Smith  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1815e32d5dSMike Smith  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1915e32d5dSMike Smith  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2015e32d5dSMike Smith  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2115e32d5dSMike Smith  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2215e32d5dSMike Smith  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2315e32d5dSMike Smith  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2415e32d5dSMike Smith  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2515e32d5dSMike Smith  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2615e32d5dSMike Smith  * SUCH DAMAGE.
2715e32d5dSMike Smith  *
2815e32d5dSMike Smith  *	$FreeBSD$
2915e32d5dSMike Smith  */
3015e32d5dSMike Smith /******************************************************************************
3115e32d5dSMike Smith  *
3215e32d5dSMike Smith  * 1. Copyright Notice
3315e32d5dSMike Smith  *
3415e32d5dSMike Smith  * Some or all of this work - Copyright (c) 1999, Intel Corp.  All rights
3515e32d5dSMike Smith  * reserved.
3615e32d5dSMike Smith  *
3715e32d5dSMike Smith  * 2. License
3815e32d5dSMike Smith  *
3915e32d5dSMike Smith  * 2.1. This is your license from Intel Corp. under its intellectual property
4015e32d5dSMike Smith  * rights.  You may have additional license terms from the party that provided
4115e32d5dSMike Smith  * you this software, covering your right to use that party's intellectual
4215e32d5dSMike Smith  * property rights.
4315e32d5dSMike Smith  *
4415e32d5dSMike Smith  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
4515e32d5dSMike Smith  * copy of the source code appearing in this file ("Covered Code") an
4615e32d5dSMike Smith  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
4715e32d5dSMike Smith  * base code distributed originally by Intel ("Original Intel Code") to copy,
4815e32d5dSMike Smith  * make derivatives, distribute, use and display any portion of the Covered
4915e32d5dSMike Smith  * Code in any form, with the right to sublicense such rights; and
5015e32d5dSMike Smith  *
5115e32d5dSMike Smith  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
5215e32d5dSMike Smith  * license (with the right to sublicense), under only those claims of Intel
5315e32d5dSMike Smith  * patents that are infringed by the Original Intel Code, to make, use, sell,
5415e32d5dSMike Smith  * offer to sell, and import the Covered Code and derivative works thereof
5515e32d5dSMike Smith  * solely to the minimum extent necessary to exercise the above copyright
5615e32d5dSMike Smith  * license, and in no event shall the patent license extend to any additions
5715e32d5dSMike Smith  * to or modifications of the Original Intel Code.  No other license or right
5815e32d5dSMike Smith  * is granted directly or by implication, estoppel or otherwise;
5915e32d5dSMike Smith  *
6015e32d5dSMike Smith  * The above copyright and patent license is granted only if the following
6115e32d5dSMike Smith  * conditions are met:
6215e32d5dSMike Smith  *
6315e32d5dSMike Smith  * 3. Conditions
6415e32d5dSMike Smith  *
6515e32d5dSMike Smith  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
6615e32d5dSMike Smith  * Redistribution of source code of any substantial portion of the Covered
6715e32d5dSMike Smith  * Code or modification with rights to further distribute source must include
6815e32d5dSMike Smith  * the above Copyright Notice, the above License, this list of Conditions,
6915e32d5dSMike Smith  * and the following Disclaimer and Export Compliance provision.  In addition,
7015e32d5dSMike Smith  * Licensee must cause all Covered Code to which Licensee contributes to
7115e32d5dSMike Smith  * contain a file documenting the changes Licensee made to create that Covered
7215e32d5dSMike Smith  * Code and the date of any change.  Licensee must include in that file the
7315e32d5dSMike Smith  * documentation of any changes made by any predecessor Licensee.  Licensee
7415e32d5dSMike Smith  * must include a prominent statement that the modification is derived,
7515e32d5dSMike Smith  * directly or indirectly, from Original Intel Code.
7615e32d5dSMike Smith  *
7715e32d5dSMike Smith  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
7815e32d5dSMike Smith  * Redistribution of source code of any substantial portion of the Covered
7915e32d5dSMike Smith  * Code or modification without rights to further distribute source must
8015e32d5dSMike Smith  * include the following Disclaimer and Export Compliance provision in the
8115e32d5dSMike Smith  * documentation and/or other materials provided with distribution.  In
8215e32d5dSMike Smith  * addition, Licensee may not authorize further sublicense of source of any
8315e32d5dSMike Smith  * portion of the Covered Code, and must include terms to the effect that the
8415e32d5dSMike Smith  * license from Licensee to its licensee is limited to the intellectual
8515e32d5dSMike Smith  * property embodied in the software Licensee provides to its licensee, and
8615e32d5dSMike Smith  * not to intellectual property embodied in modifications its licensee may
8715e32d5dSMike Smith  * make.
8815e32d5dSMike Smith  *
8915e32d5dSMike Smith  * 3.3. Redistribution of Executable. Redistribution in executable form of any
9015e32d5dSMike Smith  * substantial portion of the Covered Code or modification must reproduce the
9115e32d5dSMike Smith  * above Copyright Notice, and the following Disclaimer and Export Compliance
9215e32d5dSMike Smith  * provision in the documentation and/or other materials provided with the
9315e32d5dSMike Smith  * distribution.
9415e32d5dSMike Smith  *
9515e32d5dSMike Smith  * 3.4. Intel retains all right, title, and interest in and to the Original
9615e32d5dSMike Smith  * Intel Code.
9715e32d5dSMike Smith  *
9815e32d5dSMike Smith  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
9915e32d5dSMike Smith  * Intel shall be used in advertising or otherwise to promote the sale, use or
10015e32d5dSMike Smith  * other dealings in products derived from or relating to the Covered Code
10115e32d5dSMike Smith  * without prior written authorization from Intel.
10215e32d5dSMike Smith  *
10315e32d5dSMike Smith  * 4. Disclaimer and Export Compliance
10415e32d5dSMike Smith  *
10515e32d5dSMike Smith  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
10615e32d5dSMike Smith  * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
10715e32d5dSMike Smith  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
10815e32d5dSMike Smith  * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
10915e32d5dSMike Smith  * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
11015e32d5dSMike Smith  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
11115e32d5dSMike Smith  * PARTICULAR PURPOSE.
11215e32d5dSMike Smith  *
11315e32d5dSMike Smith  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
11415e32d5dSMike Smith  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
11515e32d5dSMike Smith  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
11615e32d5dSMike Smith  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
11715e32d5dSMike Smith  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
11815e32d5dSMike Smith  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
11915e32d5dSMike Smith  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
12015e32d5dSMike Smith  * LIMITED REMEDY.
12115e32d5dSMike Smith  *
12215e32d5dSMike Smith  * 4.3. Licensee shall not export, either directly or indirectly, any of this
12315e32d5dSMike Smith  * software or system incorporating such software without first obtaining any
12415e32d5dSMike Smith  * required license or other approval from the U. S. Department of Commerce or
12515e32d5dSMike Smith  * any other agency or department of the United States Government.  In the
12615e32d5dSMike Smith  * event Licensee exports any such software from the United States or
12715e32d5dSMike Smith  * re-exports any such software from a foreign destination, Licensee shall
12815e32d5dSMike Smith  * ensure that the distribution and export/re-export of the software is in
12915e32d5dSMike Smith  * compliance with all laws, regulations, orders, or other restrictions of the
13015e32d5dSMike Smith  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
13115e32d5dSMike Smith  * any of its subsidiaries will export/re-export any technical data, process,
13215e32d5dSMike Smith  * software, or service, directly or indirectly, to any country for which the
13315e32d5dSMike Smith  * United States government or any agency thereof requires an export license,
13415e32d5dSMike Smith  * other governmental approval, or letter of assurance, without first obtaining
13515e32d5dSMike Smith  * such license, approval or letter.
13615e32d5dSMike Smith  *
13715e32d5dSMike Smith  *****************************************************************************/
13815e32d5dSMike Smith 
13915e32d5dSMike Smith #include "opt_acpi.h"
14015e32d5dSMike Smith #include <sys/param.h>
14115e32d5dSMike Smith #include <sys/kernel.h>
14215e32d5dSMike Smith #include <sys/bus.h>
14315e32d5dSMike Smith 
14415e32d5dSMike Smith #include <machine/bus.h>
14515e32d5dSMike Smith #include <machine/resource.h>
14615e32d5dSMike Smith #include <sys/rman.h>
14715e32d5dSMike Smith 
14815e32d5dSMike Smith #include "acpi.h"
14915e32d5dSMike Smith 
15015e32d5dSMike Smith #include <dev/acpica/acpivar.h>
15115e32d5dSMike Smith 
1520ae55423SMike Smith /*
1530ae55423SMike Smith  * Hooks for the ACPI CA debugging infrastructure
1540ae55423SMike Smith  */
155a9cf0dffSMike Smith #define _COMPONENT	ACPI_EC
156dbd0058aSMike Smith ACPI_MODULE_NAME("EC")
1570ae55423SMike Smith 
158da3b867eSMike Smith /*
159da3b867eSMike Smith  * EC_COMMAND:
160da3b867eSMike Smith  * -----------
161da3b867eSMike Smith  */
162da3b867eSMike Smith typedef UINT8				EC_COMMAND;
163da3b867eSMike Smith 
164da3b867eSMike Smith #define EC_COMMAND_UNKNOWN		((EC_COMMAND) 0x00)
165da3b867eSMike Smith #define EC_COMMAND_READ			((EC_COMMAND) 0x80)
166da3b867eSMike Smith #define EC_COMMAND_WRITE		((EC_COMMAND) 0x81)
167da3b867eSMike Smith #define EC_COMMAND_BURST_ENABLE		((EC_COMMAND) 0x82)
168da3b867eSMike Smith #define EC_COMMAND_BURST_DISABLE	((EC_COMMAND) 0x83)
169da3b867eSMike Smith #define EC_COMMAND_QUERY		((EC_COMMAND) 0x84)
170da3b867eSMike Smith 
171da3b867eSMike Smith /*
172da3b867eSMike Smith  * EC_STATUS:
173da3b867eSMike Smith  * ----------
174da3b867eSMike Smith  * The encoding of the EC status register is illustrated below.
175da3b867eSMike Smith  * Note that a set bit (1) indicates the property is TRUE
176da3b867eSMike Smith  * (e.g. if bit 0 is set then the output buffer is full).
177da3b867eSMike Smith  * +-+-+-+-+-+-+-+-+
178da3b867eSMike Smith  * |7|6|5|4|3|2|1|0|
179da3b867eSMike Smith  * +-+-+-+-+-+-+-+-+
180da3b867eSMike Smith  *  | | | | | | | |
181da3b867eSMike Smith  *  | | | | | | | +- Output Buffer Full?
182da3b867eSMike Smith  *  | | | | | | +--- Input Buffer Full?
183da3b867eSMike Smith  *  | | | | | +----- <reserved>
184da3b867eSMike Smith  *  | | | | +------- Data Register is Command Byte?
185da3b867eSMike Smith  *  | | | +--------- Burst Mode Enabled?
186da3b867eSMike Smith  *  | | +----------- SCI Event?
187da3b867eSMike Smith  *  | +------------- SMI Event?
188da3b867eSMike Smith  *  +--------------- <Reserved>
189da3b867eSMike Smith  *
190da3b867eSMike Smith  */
191da3b867eSMike Smith typedef UINT8				EC_STATUS;
192da3b867eSMike Smith 
193da3b867eSMike Smith #define EC_FLAG_OUTPUT_BUFFER		((EC_STATUS) 0x01)
194da3b867eSMike Smith #define EC_FLAG_INPUT_BUFFER		((EC_STATUS) 0x02)
195da3b867eSMike Smith #define EC_FLAG_BURST_MODE		((EC_STATUS) 0x10)
196da3b867eSMike Smith #define EC_FLAG_SCI			((EC_STATUS) 0x20)
197da3b867eSMike Smith 
198da3b867eSMike Smith /*
199da3b867eSMike Smith  * EC_EVENT:
200da3b867eSMike Smith  * ---------
201da3b867eSMike Smith  */
202da3b867eSMike Smith typedef UINT8				EC_EVENT;
203da3b867eSMike Smith 
204da3b867eSMike Smith #define EC_EVENT_UNKNOWN		((EC_EVENT) 0x00)
205da3b867eSMike Smith #define EC_EVENT_OUTPUT_BUFFER_FULL	((EC_EVENT) 0x01)
206da3b867eSMike Smith #define EC_EVENT_INPUT_BUFFER_EMPTY	((EC_EVENT) 0x02)
207da3b867eSMike Smith #define EC_EVENT_SCI			((EC_EVENT) 0x20)
208da3b867eSMike Smith 
209da3b867eSMike Smith /*
210da3b867eSMike Smith  * Register access primitives
211da3b867eSMike Smith  */
212da3b867eSMike Smith #define EC_GET_DATA(sc)							\
213da3b867eSMike Smith 	bus_space_read_1((sc)->ec_data_tag, (sc)->ec_data_handle, 0)
214da3b867eSMike Smith 
215da3b867eSMike Smith #define EC_SET_DATA(sc, v)						\
216da3b867eSMike Smith 	bus_space_write_1((sc)->ec_data_tag, (sc)->ec_data_handle, 0, (v))
217da3b867eSMike Smith 
218da3b867eSMike Smith #define EC_GET_CSR(sc)							\
219da3b867eSMike Smith 	bus_space_read_1((sc)->ec_csr_tag, (sc)->ec_csr_handle, 0)
220da3b867eSMike Smith 
221da3b867eSMike Smith #define EC_SET_CSR(sc, v)						\
222da3b867eSMike Smith 	bus_space_write_1((sc)->ec_csr_tag, (sc)->ec_csr_handle, 0, (v))
223da3b867eSMike Smith 
224f8335e3aSNate Lawson /* Embedded Controller Boot Resources Table (ECDT) */
225f8335e3aSNate Lawson typedef struct {
226f8335e3aSNate Lawson     ACPI_TABLE_HEADER		header;
227f8335e3aSNate Lawson     ACPI_GENERIC_ADDRESS	control;
228f8335e3aSNate Lawson     ACPI_GENERIC_ADDRESS	data;
229f8335e3aSNate Lawson     UINT32			uid;
230f8335e3aSNate Lawson     UINT8			gpe_bit;
231f8335e3aSNate Lawson     char			ec_id[0];
232f8335e3aSNate Lawson } ACPI_TABLE_ECDT;
233f8335e3aSNate Lawson 
234f8335e3aSNate Lawson /* Indicate that this device has already been probed via ECDT. */
235f8335e3aSNate Lawson #define DEV_ECDT_FLAG		0x80000000
236f8335e3aSNate Lawson 
237f8335e3aSNate Lawson /* Indicate that this device should use the global lock. */
238f8335e3aSNate Lawson #define DEV_GLK_FLAG		0x40000000
239f8335e3aSNate Lawson 
240f8335e3aSNate Lawson /* Get/set GPE bit value in the magic ivar. */
241f8335e3aSNate Lawson #define DEV_GET_GPEBIT(x)	((x) & 0xff)
242f8335e3aSNate Lawson #define DEV_SET_GPEBIT(x, y)	((x) = ((x) & ~0xff) | ((y) & 0xff))
243f8335e3aSNate Lawson 
244da3b867eSMike Smith /*
245da3b867eSMike Smith  * Driver softc.
246da3b867eSMike Smith  */
24715e32d5dSMike Smith struct acpi_ec_softc {
24815e32d5dSMike Smith     device_t		ec_dev;
24915e32d5dSMike Smith     ACPI_HANDLE		ec_handle;
25015e32d5dSMike Smith     UINT32		ec_gpebit;
25115e32d5dSMike Smith 
25215e32d5dSMike Smith     int			ec_data_rid;
25315e32d5dSMike Smith     struct resource	*ec_data_res;
25415e32d5dSMike Smith     bus_space_tag_t	ec_data_tag;
25515e32d5dSMike Smith     bus_space_handle_t	ec_data_handle;
25615e32d5dSMike Smith 
25715e32d5dSMike Smith     int			ec_csr_rid;
25815e32d5dSMike Smith     struct resource	*ec_csr_res;
25915e32d5dSMike Smith     bus_space_tag_t	ec_csr_tag;
26015e32d5dSMike Smith     bus_space_handle_t	ec_csr_handle;
26115e32d5dSMike Smith 
2621f04e8f5SNate Lawson     int			ec_glk;
2631f04e8f5SNate Lawson     int			ec_glkhandle;
2641f04e8f5SNate Lawson     struct mtx		ec_mtx;
2651f04e8f5SNate Lawson     UINT32		ec_polldelay;
26615e32d5dSMike Smith };
26715e32d5dSMike Smith 
2681f04e8f5SNate Lawson /*
2691f04e8f5SNate Lawson  * XXX
2701f04e8f5SNate Lawson  * I couldn't find it in the spec but other implementations also use a
2711f04e8f5SNate Lawson  * value of 1 ms for the time to acquire global lock.
2721f04e8f5SNate Lawson  */
2731f04e8f5SNate Lawson #define EC_LOCK_TIMEOUT	1000
2744690674eSMitsuru IWASAKI 
2751f04e8f5SNate Lawson /*
2761f04e8f5SNate Lawson  * Start with an interval of 1 us for status poll loop.  This delay
2771f04e8f5SNate Lawson  * will be dynamically adjusted based on the actual time waited.
2781f04e8f5SNate Lawson  */
2791f04e8f5SNate Lawson #define EC_POLL_DELAY	1
2801f04e8f5SNate Lawson 
2811f04e8f5SNate Lawson /* Total time in ms spent in the poll loop waiting for a response. */
2821f04e8f5SNate Lawson #define EC_POLL_TIMEOUT	50
28315e32d5dSMike Smith 
284ff40920eSNate Lawson #define EVENT_READY(event, status)			\
285ff40920eSNate Lawson 	(((event) == EC_EVENT_OUTPUT_BUFFER_FULL &&	\
286ff40920eSNate Lawson 	 ((status) & EC_FLAG_OUTPUT_BUFFER) != 0) ||	\
287ff40920eSNate Lawson 	 ((event) == EC_EVENT_INPUT_BUFFER_EMPTY && 	\
288ff40920eSNate Lawson 	 ((status) & EC_FLAG_INPUT_BUFFER) == 0))
289ff40920eSNate Lawson 
29015e32d5dSMike Smith static __inline ACPI_STATUS
29115e32d5dSMike Smith EcLock(struct acpi_ec_softc *sc)
29215e32d5dSMike Smith {
2931f04e8f5SNate Lawson     ACPI_STATUS	status = AE_OK;
29415e32d5dSMike Smith 
2951f04e8f5SNate Lawson     /* Always acquire this EC's mutex. */
2961f04e8f5SNate Lawson     mtx_lock(&sc->ec_mtx);
2971f04e8f5SNate Lawson 
2981f04e8f5SNate Lawson     /* If _GLK is non-zero, also acquire the global lock. */
2991f04e8f5SNate Lawson     if (sc->ec_glk) {
3001f04e8f5SNate Lawson 	status = AcpiAcquireGlobalLock(EC_LOCK_TIMEOUT, &sc->ec_glkhandle);
3011f04e8f5SNate Lawson 	if (ACPI_FAILURE(status))
3021f04e8f5SNate Lawson 	    mtx_unlock(&sc->ec_mtx);
3031f04e8f5SNate Lawson     }
304c573e654SMitsuru IWASAKI 
30515e32d5dSMike Smith     return (status);
30615e32d5dSMike Smith }
30715e32d5dSMike Smith 
30815e32d5dSMike Smith static __inline void
30915e32d5dSMike Smith EcUnlock(struct acpi_ec_softc *sc)
31015e32d5dSMike Smith {
3111f04e8f5SNate Lawson     if (sc->ec_glk)
3121f04e8f5SNate Lawson 	AcpiReleaseGlobalLock(sc->ec_glkhandle);
3131f04e8f5SNate Lawson     mtx_unlock(&sc->ec_mtx);
31415e32d5dSMike Smith }
31515e32d5dSMike Smith 
31615e32d5dSMike Smith static void		EcGpeHandler(void *Context);
31715e32d5dSMike Smith static ACPI_STATUS	EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function,
31815e32d5dSMike Smith 				void *Context, void **return_Context);
319464c662eSNate Lawson static ACPI_STATUS	EcSpaceHandler(UINT32 Function,
320464c662eSNate Lawson 				ACPI_PHYSICAL_ADDRESS Address,
321464c662eSNate Lawson 				UINT32 width, ACPI_INTEGER *Value,
32215e32d5dSMike Smith 				void *Context, void *RegionContext);
32315e32d5dSMike Smith static ACPI_STATUS	EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event);
32415e32d5dSMike Smith static ACPI_STATUS	EcQuery(struct acpi_ec_softc *sc, UINT8 *Data);
3251f04e8f5SNate Lawson static ACPI_STATUS	EcCommand(struct acpi_ec_softc *sc, EC_COMMAND cmd);
326464c662eSNate Lawson static ACPI_STATUS	EcRead(struct acpi_ec_softc *sc, UINT8 Address,
327464c662eSNate Lawson 				UINT8 *Data);
328464c662eSNate Lawson static ACPI_STATUS	EcWrite(struct acpi_ec_softc *sc, UINT8 Address,
329464c662eSNate Lawson 				UINT8 *Data);
33015e32d5dSMike Smith static int		acpi_ec_probe(device_t dev);
33115e32d5dSMike Smith static int		acpi_ec_attach(device_t dev);
33215e32d5dSMike Smith 
33315e32d5dSMike Smith static device_method_t acpi_ec_methods[] = {
33415e32d5dSMike Smith     /* Device interface */
33515e32d5dSMike Smith     DEVMETHOD(device_probe,	acpi_ec_probe),
33615e32d5dSMike Smith     DEVMETHOD(device_attach,	acpi_ec_attach),
33715e32d5dSMike Smith 
33815e32d5dSMike Smith     {0, 0}
33915e32d5dSMike Smith };
34015e32d5dSMike Smith 
34115e32d5dSMike Smith static driver_t acpi_ec_driver = {
34215e32d5dSMike Smith     "acpi_ec",
34315e32d5dSMike Smith     acpi_ec_methods,
34415e32d5dSMike Smith     sizeof(struct acpi_ec_softc),
34515e32d5dSMike Smith };
34615e32d5dSMike Smith 
3473273b005SMike Smith static devclass_t acpi_ec_devclass;
34815e32d5dSMike Smith DRIVER_MODULE(acpi_ec, acpi, acpi_ec_driver, acpi_ec_devclass, 0, 0);
34915e32d5dSMike Smith 
35015e32d5dSMike Smith /*
351f8335e3aSNate Lawson  * Look for an ECDT and if we find one, set up default GPE and
352f8335e3aSNate Lawson  * space handlers to catch attempts to access EC space before
35315e32d5dSMike Smith  * we have a real driver instance in place.
354f8335e3aSNate Lawson  * TODO: if people report invalid ECDTs, add a tunable to disable them.
35515e32d5dSMike Smith  */
356f8335e3aSNate Lawson void
357f8335e3aSNate Lawson acpi_ec_ecdt_probe(device_t parent)
35815e32d5dSMike Smith {
359f8335e3aSNate Lawson     ACPI_TABLE_ECDT *ecdt;
360f8335e3aSNate Lawson     ACPI_STATUS	     status;
361f8335e3aSNate Lawson     device_t	     child;
362f8335e3aSNate Lawson     ACPI_HANDLE	     h;
363f8335e3aSNate Lawson     int		     magic;
364f8335e3aSNate Lawson 
365b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
36615e32d5dSMike Smith 
367f8335e3aSNate Lawson     /* Find and validate the ECDT. */
368f8335e3aSNate Lawson     status = AcpiGetFirmwareTable("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
369f8335e3aSNate Lawson 		(ACPI_TABLE_HEADER **)&ecdt);
370f8335e3aSNate Lawson     if (ACPI_FAILURE(status) ||
371f8335e3aSNate Lawson 	ecdt->control.RegisterBitWidth != 8 ||
372f8335e3aSNate Lawson 	ecdt->data.RegisterBitWidth != 8) {
373f8335e3aSNate Lawson 	return;
37415e32d5dSMike Smith     }
37515e32d5dSMike Smith 
376f8335e3aSNate Lawson     /* Create the child device with the given unit number. */
377f8335e3aSNate Lawson     child = BUS_ADD_CHILD(parent, 0, "acpi_ec", ecdt->uid);
378f8335e3aSNate Lawson     if (child == NULL) {
379f8335e3aSNate Lawson 	printf("acpi_ec_ecdt_probe: can't add child\n");
380f8335e3aSNate Lawson 	return;
381f8335e3aSNate Lawson     }
382f8335e3aSNate Lawson 
383f8335e3aSNate Lawson     /* Find and save the ACPI handle for this device. */
384f8335e3aSNate Lawson     status = AcpiGetHandle(NULL, ecdt->ec_id, &h);
385f8335e3aSNate Lawson     if (ACPI_FAILURE(status)) {
386f8335e3aSNate Lawson 	device_delete_child(parent, child);
387f8335e3aSNate Lawson 	printf("acpi_ec_ecdt_probe: can't get handle\n");
388f8335e3aSNate Lawson 	return;
389f8335e3aSNate Lawson     }
390f8335e3aSNate Lawson     acpi_set_handle(child, h);
391f8335e3aSNate Lawson 
392f8335e3aSNate Lawson     /* Set the data and CSR register addresses. */
393f8335e3aSNate Lawson     bus_set_resource(child, SYS_RES_IOPORT, 0, ecdt->data.Address,
394f8335e3aSNate Lawson 	/*count*/1);
395f8335e3aSNate Lawson     bus_set_resource(child, SYS_RES_IOPORT, 1, ecdt->control.Address,
396f8335e3aSNate Lawson 	/*count*/1);
397f8335e3aSNate Lawson 
39815e32d5dSMike Smith     /*
399f8335e3aSNate Lawson      * Store values for the probe/attach routines to use.  Store the
400f8335e3aSNate Lawson      * ECDT GPE bit and set the global lock flag (just to be safe).
401f8335e3aSNate Lawson      * We'll determine whether we really want to use the global lock
402f8335e3aSNate Lawson      * in a later call to attach.
40315e32d5dSMike Smith      */
404f8335e3aSNate Lawson     magic = DEV_ECDT_FLAG | DEV_GLK_FLAG;
405f8335e3aSNate Lawson     DEV_SET_GPEBIT(magic, ecdt->gpe_bit);
406f8335e3aSNate Lawson     acpi_set_magic(child, magic);
407f8335e3aSNate Lawson 
408f8335e3aSNate Lawson     /* Finish the attach process. */
409f8335e3aSNate Lawson     if (device_probe_and_attach(child) != 0)
410f8335e3aSNate Lawson 	device_delete_child(parent, child);
411f8335e3aSNate Lawson }
412f8335e3aSNate Lawson 
41315e32d5dSMike Smith static int
41415e32d5dSMike Smith acpi_ec_probe(device_t dev)
41515e32d5dSMike Smith {
416f8335e3aSNate Lawson     ACPI_HANDLE h;
417f8335e3aSNate Lawson     ACPI_STATUS status;
418f8335e3aSNate Lawson     device_t	peer;
419f8335e3aSNate Lawson     char	desc[64];
420f8335e3aSNate Lawson     int		magic, uid, glk, gpebit, ret = ENXIO;
4210ae55423SMike Smith 
422f8335e3aSNate Lawson     /* Check that this is an EC device and it's not disabled. */
423f8335e3aSNate Lawson     if (acpi_get_type(dev) != ACPI_TYPE_DEVICE || acpi_disabled("ec") ||
424f8335e3aSNate Lawson 	!acpi_MatchHid(dev, "PNP0C09")) {
425f8335e3aSNate Lawson 	return (ENXIO);
426f8335e3aSNate Lawson     }
42715e32d5dSMike Smith 
42815e32d5dSMike Smith     /*
429f8335e3aSNate Lawson      * If probed via ECDT, set description and continue.  Otherwise,
430f8335e3aSNate Lawson      * we can access the namespace and make sure this is not a
431f8335e3aSNate Lawson      * duplicate probe.
43215e32d5dSMike Smith      */
433f8335e3aSNate Lawson     magic = acpi_get_magic(dev);
434f8335e3aSNate Lawson     if ((magic & DEV_ECDT_FLAG) != 0) {
435f8335e3aSNate Lawson 	snprintf(desc, sizeof(desc), "embedded controller: ECDT, GPE %#x, GLK",
436f8335e3aSNate Lawson 		 DEV_GET_GPEBIT(magic));
437f8335e3aSNate Lawson 	device_set_desc_copy(dev, desc);
4381f04e8f5SNate Lawson 	ret = 0;
439f8335e3aSNate Lawson     } else {
440f8335e3aSNate Lawson 	h = acpi_get_handle(dev);
441f8335e3aSNate Lawson 
442f8335e3aSNate Lawson 	/*
443f8335e3aSNate Lawson 	 * Read the unit ID to check for duplicate attach and the
444f8335e3aSNate Lawson 	 * global lock value to see if we should acquire it when
445f8335e3aSNate Lawson 	 * accessing the EC.
446f8335e3aSNate Lawson 	 */
447f8335e3aSNate Lawson 	status = acpi_EvaluateInteger(h, "_UID", &uid);
448f8335e3aSNate Lawson 	if (ACPI_FAILURE(status))
449f8335e3aSNate Lawson 	    uid = 0;
450f8335e3aSNate Lawson 	status = acpi_EvaluateInteger(h, "_GLK", &glk);
451f8335e3aSNate Lawson 	if (ACPI_FAILURE(status))
452f8335e3aSNate Lawson 	    glk = 0;
453f8335e3aSNate Lawson 
454f8335e3aSNate Lawson 	/*
455f8335e3aSNate Lawson 	 * Evaluate the _GPE method to find the GPE bit used by the EC to
456f8335e3aSNate Lawson 	 * signal status (SCI).  Note that we don't handle the case where
457f8335e3aSNate Lawson 	 * it can return a package instead of an int.
458f8335e3aSNate Lawson 	 */
459f8335e3aSNate Lawson 	status = acpi_EvaluateInteger(h, "_GPE", &gpebit);
460f8335e3aSNate Lawson 	if (ACPI_FAILURE(status)) {
461f8335e3aSNate Lawson 	    device_printf(dev, "can't evaluate _GPE - %s\n",
462f8335e3aSNate Lawson 			  AcpiFormatException(status));
463f8335e3aSNate Lawson 	    return (ENXIO);
464f8335e3aSNate Lawson 	}
465f8335e3aSNate Lawson 
466f8335e3aSNate Lawson 	/* Store the values we got from the namespace for attach. */
467f8335e3aSNate Lawson 	magic = glk != 0 ? DEV_GLK_FLAG : 0;
468f8335e3aSNate Lawson 	DEV_SET_GPEBIT(magic, gpebit);
469f8335e3aSNate Lawson 	acpi_set_magic(dev, magic);
470f8335e3aSNate Lawson 
471f8335e3aSNate Lawson 	/*
472f8335e3aSNate Lawson 	 * Check for a duplicate probe.  This can happen when a probe
473f8335e3aSNate Lawson 	 * via ECDT succeeded already.  If there is a duplicate, override
474f8335e3aSNate Lawson 	 * its value for GLK in the peer's softc since the ECDT case
475f8335e3aSNate Lawson 	 * always enables the global lock to be safe.  Otherwise, just
476f8335e3aSNate Lawson 	 * continue on to attach.
477f8335e3aSNate Lawson 	 */
478f8335e3aSNate Lawson 	peer = devclass_get_device(acpi_ec_devclass, uid);
479f8335e3aSNate Lawson 	if (peer == NULL || !device_is_alive(peer)) {
480f8335e3aSNate Lawson 	    snprintf(desc, sizeof(desc), "embedded controller: GPE %#x%s",
481f8335e3aSNate Lawson 		     gpebit, glk != 0 ? ", GLK" : "");
482f8335e3aSNate Lawson 	    device_set_desc_copy(dev, desc);
483f8335e3aSNate Lawson 	    ret = 0;
484f8335e3aSNate Lawson 	} else {
485f8335e3aSNate Lawson 	    struct acpi_ec_softc *sc;
486f8335e3aSNate Lawson 
487f8335e3aSNate Lawson 	    /*
488f8335e3aSNate Lawson 	     * Set the peer's sc->ec_glk with locks held so we won't
489f8335e3aSNate Lawson 	     * override it between another thread's lock/unlock calls.
490f8335e3aSNate Lawson 	     */
491f8335e3aSNate Lawson 	    sc = device_get_softc(peer);
492f8335e3aSNate Lawson 	    if (sc->ec_glk != glk) {
493f8335e3aSNate Lawson 		ACPI_VPRINT(peer, acpi_device_get_parent_softc(peer),
494f8335e3aSNate Lawson 		    "Changing GLK from %d to %d\n", sc->ec_glk, glk);
495f8335e3aSNate Lawson 		mtx_lock(&sc->ec_mtx);
496f8335e3aSNate Lawson 		sc->ec_glk = glk != 0 ? 1 : 0;
497f8335e3aSNate Lawson 		mtx_unlock(&sc->ec_mtx);
498f8335e3aSNate Lawson 	    }
499f8335e3aSNate Lawson 	}
50015e32d5dSMike Smith     }
5011f04e8f5SNate Lawson 
5021f04e8f5SNate Lawson     return (ret);
50315e32d5dSMike Smith }
50415e32d5dSMike Smith 
50515e32d5dSMike Smith static int
50615e32d5dSMike Smith acpi_ec_attach(device_t dev)
50715e32d5dSMike Smith {
50815e32d5dSMike Smith     struct acpi_ec_softc	*sc;
50915e32d5dSMike Smith     ACPI_STATUS			Status;
510f8335e3aSNate Lawson     int				magic, errval = 0;
511dbd0058aSMike Smith 
512b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
5130ae55423SMike Smith 
5141f04e8f5SNate Lawson     /* Fetch/initialize softc (assumes softc is pre-zeroed). */
51515e32d5dSMike Smith     sc = device_get_softc(dev);
51615e32d5dSMike Smith     sc->ec_dev = dev;
51715e32d5dSMike Smith     sc->ec_handle = acpi_get_handle(dev);
5181f04e8f5SNate Lawson     sc->ec_polldelay = EC_POLL_DELAY;
5191f04e8f5SNate Lawson     mtx_init(&sc->ec_mtx, "ACPI embedded controller", NULL, MTX_DEF);
52015e32d5dSMike Smith 
521f8335e3aSNate Lawson     /* Retrieve previously probed values via device ivars. */
522f8335e3aSNate Lawson     magic = acpi_get_magic(dev);
523f8335e3aSNate Lawson     sc->ec_glk = (magic & DEV_GLK_FLAG) != 0 ? 1 : 0;
524f8335e3aSNate Lawson     sc->ec_gpebit = DEV_GET_GPEBIT(magic);
525f8335e3aSNate Lawson 
5261f04e8f5SNate Lawson     /* Attach bus resources for data and command/status ports. */
52715e32d5dSMike Smith     sc->ec_data_rid = 0;
528464c662eSNate Lawson     sc->ec_data_res = bus_alloc_resource(sc->ec_dev, SYS_RES_IOPORT,
529464c662eSNate Lawson 			&sc->ec_data_rid, 0, ~0, 1, RF_ACTIVE);
530464c662eSNate Lawson     if (sc->ec_data_res == NULL) {
53115e32d5dSMike Smith 	device_printf(dev, "can't allocate data port\n");
532f8372adeSTakanori Watanabe 	errval = ENXIO;
533f8372adeSTakanori Watanabe 	goto out;
53415e32d5dSMike Smith     }
53515e32d5dSMike Smith     sc->ec_data_tag = rman_get_bustag(sc->ec_data_res);
53615e32d5dSMike Smith     sc->ec_data_handle = rman_get_bushandle(sc->ec_data_res);
53715e32d5dSMike Smith 
53815e32d5dSMike Smith     sc->ec_csr_rid = 1;
539464c662eSNate Lawson     sc->ec_csr_res = bus_alloc_resource(sc->ec_dev, SYS_RES_IOPORT,
540464c662eSNate Lawson 			&sc->ec_csr_rid, 0, ~0, 1, RF_ACTIVE);
541464c662eSNate Lawson     if (sc->ec_csr_res == NULL) {
54215e32d5dSMike Smith 	device_printf(dev, "can't allocate command/status port\n");
543f8372adeSTakanori Watanabe 	errval = ENXIO;
544f8372adeSTakanori Watanabe 	goto out;
54515e32d5dSMike Smith     }
54615e32d5dSMike Smith     sc->ec_csr_tag = rman_get_bustag(sc->ec_csr_res);
54715e32d5dSMike Smith     sc->ec_csr_handle = rman_get_bushandle(sc->ec_csr_res);
54815e32d5dSMike Smith 
54915e32d5dSMike Smith     /*
5501f04e8f5SNate Lawson      * Install a handler for this EC's GPE bit.  We want edge-triggered
5511f04e8f5SNate Lawson      * behavior.
55215e32d5dSMike Smith      */
5531f04e8f5SNate Lawson     ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "attaching GPE handler\n"));
5546fca9360SNate Lawson     Status = AcpiInstallGpeHandler(NULL, sc->ec_gpebit,
5556fca9360SNate Lawson 		ACPI_EVENT_EDGE_TRIGGERED, &EcGpeHandler, sc);
556464c662eSNate Lawson     if (ACPI_FAILURE(Status)) {
55776d1dff4SMike Smith 	device_printf(dev, "can't install GPE handler for %s - %s\n",
558bfae45aaSMike Smith 		      acpi_name(sc->ec_handle), AcpiFormatException(Status));
559f8372adeSTakanori Watanabe 	errval = ENXIO;
560f8372adeSTakanori Watanabe 	goto out;
56115e32d5dSMike Smith     }
56215e32d5dSMike Smith 
56315e32d5dSMike Smith     /*
56415e32d5dSMike Smith      * Install address space handler
56515e32d5dSMike Smith      */
5664c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "attaching address space handler\n"));
567464c662eSNate Lawson     Status = AcpiInstallAddressSpaceHandler(sc->ec_handle, ACPI_ADR_SPACE_EC,
5681f04e8f5SNate Lawson 		&EcSpaceHandler, &EcSpaceSetup, sc);
569464c662eSNate Lawson     if (ACPI_FAILURE(Status)) {
57076d1dff4SMike Smith 	device_printf(dev, "can't install address space handler for %s - %s\n",
571bfae45aaSMike Smith 		      acpi_name(sc->ec_handle), AcpiFormatException(Status));
572ff40920eSNate Lawson 	Status = AcpiRemoveGpeHandler(NULL, sc->ec_gpebit, &EcGpeHandler);
5731f04e8f5SNate Lawson 	if (ACPI_FAILURE(Status))
5741f04e8f5SNate Lawson 	    panic("Added GPE handler but can't remove it");
575f8372adeSTakanori Watanabe 	errval = ENXIO;
576f8372adeSTakanori Watanabe 	goto out;
57715e32d5dSMike Smith     }
5781f04e8f5SNate Lawson 
5791f04e8f5SNate Lawson     ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "acpi_ec_attach complete\n"));
5801f04e8f5SNate Lawson     return (0);
581464c662eSNate Lawson 
582f8372adeSTakanori Watanabe  out:
583f8372adeSTakanori Watanabe     if (sc->ec_csr_res)
584f8372adeSTakanori Watanabe 	bus_release_resource(sc->ec_dev, SYS_RES_IOPORT, sc->ec_csr_rid,
585f8372adeSTakanori Watanabe 			     sc->ec_csr_res);
586f8372adeSTakanori Watanabe     if (sc->ec_data_res)
587f8372adeSTakanori Watanabe 	bus_release_resource(sc->ec_dev, SYS_RES_IOPORT, sc->ec_data_rid,
588f8372adeSTakanori Watanabe 			     sc->ec_data_res);
589ff40920eSNate Lawson     mtx_destroy(&sc->ec_mtx);
5901f04e8f5SNate Lawson     return (errval);
5911f04e8f5SNate Lawson }
5921f04e8f5SNate Lawson 
5931f04e8f5SNate Lawson static ACPI_STATUS
5941f04e8f5SNate Lawson EcQuery(struct acpi_ec_softc *sc, UINT8 *Data)
5951f04e8f5SNate Lawson {
5961f04e8f5SNate Lawson     ACPI_STATUS	Status;
5971f04e8f5SNate Lawson 
5981f04e8f5SNate Lawson     Status = EcLock(sc);
5991f04e8f5SNate Lawson     if (ACPI_FAILURE(Status))
6001f04e8f5SNate Lawson 	return (Status);
6011f04e8f5SNate Lawson 
6021f04e8f5SNate Lawson     /*
6031f04e8f5SNate Lawson      * Send a query command to the EC to find out which _Qxx call it
6041f04e8f5SNate Lawson      * wants to make.  This command clears the SCI bit and also the
6051f04e8f5SNate Lawson      * interrupt source since we are edge-triggered.
6061f04e8f5SNate Lawson      */
6071f04e8f5SNate Lawson     Status = EcCommand(sc, EC_COMMAND_QUERY);
6081f04e8f5SNate Lawson     if (ACPI_SUCCESS(Status))
6091f04e8f5SNate Lawson 	*Data = EC_GET_DATA(sc);
6101f04e8f5SNate Lawson 
6111f04e8f5SNate Lawson     EcUnlock(sc);
6121f04e8f5SNate Lawson 
6131f04e8f5SNate Lawson     return (Status);
61415e32d5dSMike Smith }
61515e32d5dSMike Smith 
61615e32d5dSMike Smith static void
617c07572e7STakanori Watanabe EcGpeQueryHandler(void *Context)
61815e32d5dSMike Smith {
61915e32d5dSMike Smith     struct acpi_ec_softc	*sc = (struct acpi_ec_softc *)Context;
62015e32d5dSMike Smith     UINT8			Data;
62115e32d5dSMike Smith     ACPI_STATUS			Status;
6221f04e8f5SNate Lawson     EC_STATUS			EcStatus;
62315e32d5dSMike Smith     char			qxx[5];
62415e32d5dSMike Smith 
625b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
6261f04e8f5SNate Lawson     KASSERT(Context != NULL, ("EcGpeQueryHandler called with NULL"));
62715e32d5dSMike Smith 
62815e32d5dSMike Smith     /*
6291f04e8f5SNate Lawson      * Check status for EC_SCI.
63015e32d5dSMike Smith      *
63115e32d5dSMike Smith      * Bail out if the EC_SCI bit of the status register is not set.
63215e32d5dSMike Smith      * Note that this function should only be called when
63315e32d5dSMike Smith      * this bit is set (polling is used to detect IBE/OBF events).
63415e32d5dSMike Smith      *
6351f04e8f5SNate Lawson      * We don't acquire the global lock here but do protect against other
6361f04e8f5SNate Lawson      * running commands (read/write/query) by grabbing ec_mtx.
63715e32d5dSMike Smith      */
6381f04e8f5SNate Lawson     mtx_lock(&sc->ec_mtx);
6391f04e8f5SNate Lawson     EcStatus = EC_GET_CSR(sc);
6401f04e8f5SNate Lawson     mtx_unlock(&sc->ec_mtx);
641ff40920eSNate Lawson     if ((EcStatus & EC_EVENT_SCI) == 0) {
642ff40920eSNate Lawson 	/* If it's not an SCI, wakeup the EcWaitEvent sleep. */
643ff40920eSNate Lawson 	wakeup(&sc->ec_polldelay);
6441f04e8f5SNate Lawson 	goto re_enable;
645ff40920eSNate Lawson     }
64615e32d5dSMike Smith 
6471f04e8f5SNate Lawson     /* Find out why the EC is signaling us. */
64815e32d5dSMike Smith     Status = EcQuery(sc, &Data);
649dbd0058aSMike Smith     if (ACPI_FAILURE(Status)) {
6506971b3c7SMitsuru IWASAKI 	ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
6516971b3c7SMitsuru IWASAKI 		    "GPE query failed - %s\n", AcpiFormatException(Status));
6521f04e8f5SNate Lawson 	goto re_enable;
65315e32d5dSMike Smith     }
65415e32d5dSMike Smith 
6551f04e8f5SNate Lawson     /* Ignore the value for "no outstanding event". (13.3.5) */
6561f04e8f5SNate Lawson     if (Data == 0)
6571f04e8f5SNate Lawson 	goto re_enable;
6581f04e8f5SNate Lawson 
6591f04e8f5SNate Lawson     /* Evaluate _Qxx to respond to the controller. */
66015e32d5dSMike Smith     sprintf(qxx, "_Q%02x", Data);
66115e32d5dSMike Smith     strupr(qxx);
662ee785aa9SJohn Baldwin     Status = AcpiEvaluateObject(sc->ec_handle, qxx, NULL, NULL);
6631f04e8f5SNate Lawson     if (ACPI_FAILURE(Status) && Status != AE_NOT_FOUND) {
6646971b3c7SMitsuru IWASAKI 	ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
6656971b3c7SMitsuru IWASAKI 		    "evaluation of GPE query method %s failed - %s\n",
666bfae45aaSMike Smith 		    qxx, AcpiFormatException(Status));
66715e32d5dSMike Smith     }
6681f04e8f5SNate Lawson 
6691f04e8f5SNate Lawson re_enable:
6701f04e8f5SNate Lawson     /* Re-enable the GPE event so we'll get future requests. */
6716fca9360SNate Lawson     Status = AcpiEnableGpe(NULL, sc->ec_gpebit, ACPI_NOT_ISR);
6721f04e8f5SNate Lawson     if (ACPI_FAILURE(Status))
6731f04e8f5SNate Lawson 	printf("EcGpeQueryHandler: AcpiEnableEvent failed\n");
674c07572e7STakanori Watanabe }
67542f6d122SMike Smith 
676a9cf0dffSMike Smith /*
6771f04e8f5SNate Lawson  * Handle a GPE.  Currently we only handle SCI events as others must
6781f04e8f5SNate Lawson  * be handled by polling in EcWaitEvent().  This is because some ECs
6791f04e8f5SNate Lawson  * treat events as level when they should be edge-triggered.
680a9cf0dffSMike Smith  */
681a9cf0dffSMike Smith static void
682a9cf0dffSMike Smith EcGpeHandler(void *Context)
683c07572e7STakanori Watanabe {
684c07572e7STakanori Watanabe     struct acpi_ec_softc *sc = Context;
6851f04e8f5SNate Lawson     ACPI_STATUS		       Status;
686a9cf0dffSMike Smith 
6871f04e8f5SNate Lawson     KASSERT(Context != NULL, ("EcGpeHandler called with NULL"));
6881f04e8f5SNate Lawson 
6891f04e8f5SNate Lawson     /* Disable further GPEs while we handle this one. */
6906fca9360SNate Lawson     AcpiDisableGpe(NULL, sc->ec_gpebit, ACPI_ISR);
6911f04e8f5SNate Lawson 
6921f04e8f5SNate Lawson     /* Schedule the GPE query handler. */
6931f04e8f5SNate Lawson     Status = AcpiOsQueueForExecution(OSD_PRIORITY_GPE, EcGpeQueryHandler,
6941f04e8f5SNate Lawson 		Context);
6951f04e8f5SNate Lawson     if (ACPI_FAILURE(Status)) {
6961f04e8f5SNate Lawson 	printf("Queuing GPE query handler failed.\n");
6976fca9360SNate Lawson 	Status = AcpiEnableGpe(NULL, sc->ec_gpebit, ACPI_ISR);
6981f04e8f5SNate Lawson 	if (ACPI_FAILURE(Status))
6991f04e8f5SNate Lawson 	    printf("EcGpeHandler: AcpiEnableEvent failed\n");
700c07572e7STakanori Watanabe     }
70115e32d5dSMike Smith }
70215e32d5dSMike Smith 
70315e32d5dSMike Smith static ACPI_STATUS
704464c662eSNate Lawson EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function, void *Context,
705464c662eSNate Lawson 	     void **RegionContext)
70615e32d5dSMike Smith {
70742f6d122SMike Smith 
708b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
70942f6d122SMike Smith 
71015e32d5dSMike Smith     /*
71115e32d5dSMike Smith      * Just pass the context through, there's nothing to do here.
71215e32d5dSMike Smith      */
71315e32d5dSMike Smith     *RegionContext = Context;
71415e32d5dSMike Smith 
71542f6d122SMike Smith     return_ACPI_STATUS (AE_OK);
71615e32d5dSMike Smith }
71715e32d5dSMike Smith 
71815e32d5dSMike Smith static ACPI_STATUS
719464c662eSNate Lawson EcSpaceHandler(UINT32 Function, ACPI_PHYSICAL_ADDRESS Address, UINT32 width,
720464c662eSNate Lawson 	       ACPI_INTEGER *Value, void *Context, void *RegionContext)
72115e32d5dSMike Smith {
72215e32d5dSMike Smith     struct acpi_ec_softc	*sc = (struct acpi_ec_softc *)Context;
72315e32d5dSMike Smith     ACPI_STATUS			Status = AE_OK;
7241f04e8f5SNate Lawson     UINT8			EcAddr, EcData;
725ee785aa9SJohn Baldwin     int				i;
72615e32d5dSMike Smith 
727b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, (UINT32)Address);
7280ae55423SMike Smith 
729464c662eSNate Lawson     if (Address > 0xFF || width % 8 != 0 || Value == NULL || Context == NULL)
7300ae55423SMike Smith 	return_ACPI_STATUS (AE_BAD_PARAMETER);
73115e32d5dSMike Smith 
73215e32d5dSMike Smith     /*
73315e32d5dSMike Smith      * Perform the transaction.
73415e32d5dSMike Smith      */
7351f04e8f5SNate Lawson     EcAddr = Address;
736ee785aa9SJohn Baldwin     for (i = 0; i < width; i += 8) {
7371f04e8f5SNate Lawson 	Status = EcLock(sc);
738464c662eSNate Lawson 	if (ACPI_FAILURE(Status))
7391f04e8f5SNate Lawson 	    return (Status);
7401f04e8f5SNate Lawson 
7411f04e8f5SNate Lawson 	switch (Function) {
7421f04e8f5SNate Lawson 	case ACPI_READ:
7431f04e8f5SNate Lawson 	    EcData = 0;
7441f04e8f5SNate Lawson 	    Status = EcRead(sc, EcAddr, &EcData);
745ee785aa9SJohn Baldwin 	    break;
7461f04e8f5SNate Lawson 	case ACPI_WRITE:
7471f04e8f5SNate Lawson 	    EcData = (UINT8)((*Value) >> i);
7481f04e8f5SNate Lawson 	    Status = EcWrite(sc, EcAddr, &EcData);
7491f04e8f5SNate Lawson 	    break;
7501f04e8f5SNate Lawson 	default:
7511f04e8f5SNate Lawson 	    device_printf(sc->ec_dev, "invalid EcSpaceHandler function %d\n",
7521f04e8f5SNate Lawson 			  Function);
7531f04e8f5SNate Lawson 	    Status = AE_BAD_PARAMETER;
7541f04e8f5SNate Lawson 	    break;
7551f04e8f5SNate Lawson 	}
7561f04e8f5SNate Lawson 
7571f04e8f5SNate Lawson 	EcUnlock(sc);
7581f04e8f5SNate Lawson 	if (ACPI_FAILURE(Status))
7591f04e8f5SNate Lawson 	    return (Status);
7601f04e8f5SNate Lawson 
7611f04e8f5SNate Lawson 	*Value |= (ACPI_INTEGER)EcData << i;
7621f04e8f5SNate Lawson 	if (++EcAddr == 0)
763ee785aa9SJohn Baldwin 	    return_ACPI_STATUS (AE_BAD_PARAMETER);
764ee785aa9SJohn Baldwin     }
7650ae55423SMike Smith     return_ACPI_STATUS (Status);
76615e32d5dSMike Smith }
7672a4ac806SMike Smith 
76815e32d5dSMike Smith static ACPI_STATUS
76915e32d5dSMike Smith EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event)
77015e32d5dSMike Smith {
77115e32d5dSMike Smith     EC_STATUS	EcStatus;
7721f04e8f5SNate Lawson     ACPI_STATUS	Status;
7731f04e8f5SNate Lawson     UINT32	i, period;
77415e32d5dSMike Smith 
7751f04e8f5SNate Lawson     mtx_assert(&sc->ec_mtx, MA_OWNED);
7761f04e8f5SNate Lawson     Status = AE_NO_HARDWARE_RESPONSE;
77715e32d5dSMike Smith 
77815e32d5dSMike Smith     /*
7791f04e8f5SNate Lawson      * Wait for 1 us before checking the CSR.  Testing shows about
7801f04e8f5SNate Lawson      * 50% of requests complete in 1 us and 90% of them complete
7811f04e8f5SNate Lawson      * in 5 us or less.
78215e32d5dSMike Smith      */
783da3b867eSMike Smith     AcpiOsStall(1);
78415e32d5dSMike Smith 
78515e32d5dSMike Smith     /*
78615e32d5dSMike Smith      * Poll the EC status register to detect completion of the last
787ff40920eSNate Lawson      * command.  First, wait up to 1 ms in chunks of sc->ec_polldelay
788ff40920eSNate Lawson      * microseconds.
78915e32d5dSMike Smith      */
790ff40920eSNate Lawson     for (i = 0; i < 1000 / sc->ec_polldelay; i++) {
79115e32d5dSMike Smith 	EcStatus = EC_GET_CSR(sc);
792ff40920eSNate Lawson 	if (EVENT_READY(Event, EcStatus)) {
7931f04e8f5SNate Lawson 	    Status = AE_OK;
79415e32d5dSMike Smith 	    break;
7951f04e8f5SNate Lawson 	}
7961f04e8f5SNate Lawson 	AcpiOsStall(sc->ec_polldelay);
7971f04e8f5SNate Lawson     }
7981f04e8f5SNate Lawson 
7991f04e8f5SNate Lawson     /* Scale poll delay by the amount of time actually waited. */
8001f04e8f5SNate Lawson     period = i * sc->ec_polldelay;
8011f04e8f5SNate Lawson     if (period <= 5)
8021f04e8f5SNate Lawson 	sc->ec_polldelay = 1;
8031f04e8f5SNate Lawson     else if (period <= 20)
8041f04e8f5SNate Lawson 	sc->ec_polldelay = 5;
8051f04e8f5SNate Lawson     else if (period <= 100)
8061f04e8f5SNate Lawson 	sc->ec_polldelay = 10;
8071f04e8f5SNate Lawson     else
8081f04e8f5SNate Lawson 	sc->ec_polldelay = 100;
809ff40920eSNate Lawson 
810ff40920eSNate Lawson     /*
811ff40920eSNate Lawson      * If we still don't have a response, wait up to EC_POLL_TIMEOUT ms
812ff40920eSNate Lawson      * for completion, sleeping for chunks of 10 ms.
813ff40920eSNate Lawson      */
814ff40920eSNate Lawson     if (Status != AE_OK) {
815ff40920eSNate Lawson 	for (i = 0; i < EC_POLL_TIMEOUT / 10; i++) {
816ff40920eSNate Lawson 	    EcStatus = EC_GET_CSR(sc);
817ff40920eSNate Lawson 	    if (EVENT_READY(Event, EcStatus)) {
818ff40920eSNate Lawson 		Status = AE_OK;
819ff40920eSNate Lawson 		break;
820ff40920eSNate Lawson 	    }
821ff40920eSNate Lawson 	    msleep(&sc->ec_polldelay, &sc->ec_mtx, PZERO, "ecpoll", 10/*ms*/);
822ff40920eSNate Lawson 	}
8231f04e8f5SNate Lawson     }
8241f04e8f5SNate Lawson 
8251f04e8f5SNate Lawson     return (Status);
8261f04e8f5SNate Lawson }
8271f04e8f5SNate Lawson 
8281f04e8f5SNate Lawson static ACPI_STATUS
8291f04e8f5SNate Lawson EcCommand(struct acpi_ec_softc *sc, EC_COMMAND cmd)
8301f04e8f5SNate Lawson {
8311f04e8f5SNate Lawson     ACPI_STATUS	Status;
8321f04e8f5SNate Lawson     EC_EVENT	Event;
8331f04e8f5SNate Lawson 
8341f04e8f5SNate Lawson     mtx_assert(&sc->ec_mtx, MA_OWNED);
8351f04e8f5SNate Lawson 
8361f04e8f5SNate Lawson     /* Decide what to wait for based on command type. */
8371f04e8f5SNate Lawson     switch (cmd) {
8381f04e8f5SNate Lawson     case EC_COMMAND_READ:
83915e32d5dSMike Smith     case EC_COMMAND_WRITE:
8401f04e8f5SNate Lawson     case EC_COMMAND_BURST_DISABLE:
8411f04e8f5SNate Lawson 	Event = EC_EVENT_INPUT_BUFFER_EMPTY;
8421f04e8f5SNate Lawson 	break;
8431f04e8f5SNate Lawson     case EC_COMMAND_QUERY:
8441f04e8f5SNate Lawson     case EC_COMMAND_BURST_ENABLE:
8451f04e8f5SNate Lawson 	Event = EC_EVENT_OUTPUT_BUFFER_FULL;
84615e32d5dSMike Smith 	break;
84715e32d5dSMike Smith     default:
8486971b3c7SMitsuru IWASAKI 	ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
8491f04e8f5SNate Lawson 		    "EcCommand: Invalid command %#x\n", cmd);
8501f04e8f5SNate Lawson 	return (AE_BAD_PARAMETER);
851464c662eSNate Lawson     }
8521f04e8f5SNate Lawson 
8531f04e8f5SNate Lawson     /* Run the command and wait for the chosen event. */
8541f04e8f5SNate Lawson     EC_SET_CSR(sc, cmd);
8551f04e8f5SNate Lawson     Status = EcWaitEvent(sc, Event);
8561f04e8f5SNate Lawson     if (ACPI_FAILURE(Status)) {
8576971b3c7SMitsuru IWASAKI 	ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
8581f04e8f5SNate Lawson 		    "EcCommand: no response to %#x\n", cmd);
859464c662eSNate Lawson     }
86015e32d5dSMike Smith 
86115e32d5dSMike Smith     return (Status);
86215e32d5dSMike Smith }
86315e32d5dSMike Smith 
86415e32d5dSMike Smith static ACPI_STATUS
86515e32d5dSMike Smith EcRead(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data)
86615e32d5dSMike Smith {
86715e32d5dSMike Smith     ACPI_STATUS	Status;
86815e32d5dSMike Smith 
8691f04e8f5SNate Lawson     mtx_assert(&sc->ec_mtx, MA_OWNED);
87015e32d5dSMike Smith 
8711f04e8f5SNate Lawson #ifdef notyet
8721f04e8f5SNate Lawson     /* If we can't start burst mode, continue anyway. */
8731f04e8f5SNate Lawson     EcCommand(sc, EC_COMMAND_BURST_ENABLE);
8741f04e8f5SNate Lawson #endif
87515e32d5dSMike Smith 
8761f04e8f5SNate Lawson     Status = EcCommand(sc, EC_COMMAND_READ);
8771f04e8f5SNate Lawson     if (ACPI_FAILURE(Status))
87815e32d5dSMike Smith 	return (Status);
87915e32d5dSMike Smith 
88015e32d5dSMike Smith     EC_SET_DATA(sc, Address);
8811f04e8f5SNate Lawson     Status = EcWaitEvent(sc, EC_EVENT_OUTPUT_BUFFER_FULL);
882464c662eSNate Lawson     if (ACPI_FAILURE(Status)) {
8836971b3c7SMitsuru IWASAKI 	ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
8846971b3c7SMitsuru IWASAKI 		    "EcRead: Failed waiting for EC to send data.\n");
88515e32d5dSMike Smith 	return (Status);
88615e32d5dSMike Smith     }
88715e32d5dSMike Smith 
888464c662eSNate Lawson     *Data = EC_GET_DATA(sc);
88915e32d5dSMike Smith 
8901f04e8f5SNate Lawson #ifdef notyet
8911f04e8f5SNate Lawson     if (sc->ec_burstactive) {
8921f04e8f5SNate Lawson 	Status = EcCommand(sc, EC_COMMAND_BURST_DISABLE);
8931f04e8f5SNate Lawson 	if (ACPI_FAILURE(Status))
8941f04e8f5SNate Lawson 	    return (Status);
8951f04e8f5SNate Lawson     }
8961f04e8f5SNate Lawson #endif
89715e32d5dSMike Smith 
89815e32d5dSMike Smith     return (AE_OK);
89915e32d5dSMike Smith }
90015e32d5dSMike Smith 
90115e32d5dSMike Smith static ACPI_STATUS
90215e32d5dSMike Smith EcWrite(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data)
90315e32d5dSMike Smith {
90415e32d5dSMike Smith     ACPI_STATUS	Status;
90515e32d5dSMike Smith 
9061f04e8f5SNate Lawson     mtx_assert(&sc->ec_mtx, MA_OWNED);
90715e32d5dSMike Smith 
9081f04e8f5SNate Lawson #ifdef notyet
9091f04e8f5SNate Lawson     /* If we can't start burst mode, continue anyway. */
9101f04e8f5SNate Lawson     EcCommand(sc, EC_COMMAND_BURST_ENABLE);
9111f04e8f5SNate Lawson #endif
91215e32d5dSMike Smith 
9131f04e8f5SNate Lawson     Status = EcCommand(sc, EC_COMMAND_WRITE);
9141f04e8f5SNate Lawson     if (ACPI_FAILURE(Status))
91515e32d5dSMike Smith 	return (Status);
91615e32d5dSMike Smith 
91715e32d5dSMike Smith     EC_SET_DATA(sc, Address);
9181f04e8f5SNate Lawson     Status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY);
919464c662eSNate Lawson     if (ACPI_FAILURE(Status)) {
9206971b3c7SMitsuru IWASAKI 	ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
9211f04e8f5SNate Lawson 		    "EcRead: Failed waiting for EC to process address\n");
92215e32d5dSMike Smith 	return (Status);
92315e32d5dSMike Smith     }
92415e32d5dSMike Smith 
92515e32d5dSMike Smith     EC_SET_DATA(sc, *Data);
9261f04e8f5SNate Lawson     Status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY);
927464c662eSNate Lawson     if (ACPI_FAILURE(Status)) {
9286971b3c7SMitsuru IWASAKI 	ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
9291f04e8f5SNate Lawson 		    "EcWrite: Failed waiting for EC to process data\n");
93015e32d5dSMike Smith 	return (Status);
93115e32d5dSMike Smith     }
93215e32d5dSMike Smith 
9331f04e8f5SNate Lawson #ifdef notyet
9341f04e8f5SNate Lawson     if (sc->ec_burstactive) {
9351f04e8f5SNate Lawson 	Status = EcCommand(sc, EC_COMMAND_BURST_DISABLE);
9361f04e8f5SNate Lawson 	if (ACPI_FAILURE(Status))
9371f04e8f5SNate Lawson 	    return (Status);
9381f04e8f5SNate Lawson     }
9391f04e8f5SNate Lawson #endif
94015e32d5dSMike Smith 
94115e32d5dSMike Smith     return (AE_OK);
94215e32d5dSMike Smith }
943