xref: /freebsd/sys/dev/acpica/acpi_ec.c (revision f3fc4f8b4a61da724ede7525225d1444202d01eb)
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 
139aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
140aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
141aad970f1SDavid E. O'Brien 
14215e32d5dSMike Smith #include "opt_acpi.h"
14315e32d5dSMike Smith #include <sys/param.h>
14415e32d5dSMike Smith #include <sys/kernel.h>
14515e32d5dSMike Smith #include <sys/bus.h>
1460025fb0fSNate Lawson #include <sys/malloc.h>
14715e32d5dSMike Smith 
14815e32d5dSMike Smith #include <machine/bus.h>
14915e32d5dSMike Smith #include <machine/resource.h>
15015e32d5dSMike Smith #include <sys/rman.h>
15115e32d5dSMike Smith 
15215e32d5dSMike Smith #include "acpi.h"
15315e32d5dSMike Smith #include <dev/acpica/acpivar.h>
15415e32d5dSMike Smith 
1550ae55423SMike Smith /*
1560ae55423SMike Smith  * Hooks for the ACPI CA debugging infrastructure
1570ae55423SMike Smith  */
158a9cf0dffSMike Smith #define _COMPONENT	ACPI_EC
159dbd0058aSMike Smith ACPI_MODULE_NAME("EC")
1600ae55423SMike Smith 
161da3b867eSMike Smith /*
162da3b867eSMike Smith  * EC_COMMAND:
163da3b867eSMike Smith  * -----------
164da3b867eSMike Smith  */
165da3b867eSMike Smith typedef UINT8				EC_COMMAND;
166da3b867eSMike Smith 
167da3b867eSMike Smith #define EC_COMMAND_UNKNOWN		((EC_COMMAND) 0x00)
168da3b867eSMike Smith #define EC_COMMAND_READ			((EC_COMMAND) 0x80)
169da3b867eSMike Smith #define EC_COMMAND_WRITE		((EC_COMMAND) 0x81)
170da3b867eSMike Smith #define EC_COMMAND_BURST_ENABLE		((EC_COMMAND) 0x82)
171da3b867eSMike Smith #define EC_COMMAND_BURST_DISABLE	((EC_COMMAND) 0x83)
172da3b867eSMike Smith #define EC_COMMAND_QUERY		((EC_COMMAND) 0x84)
173da3b867eSMike Smith 
174da3b867eSMike Smith /*
175da3b867eSMike Smith  * EC_STATUS:
176da3b867eSMike Smith  * ----------
177da3b867eSMike Smith  * The encoding of the EC status register is illustrated below.
178da3b867eSMike Smith  * Note that a set bit (1) indicates the property is TRUE
179da3b867eSMike Smith  * (e.g. if bit 0 is set then the output buffer is full).
180da3b867eSMike Smith  * +-+-+-+-+-+-+-+-+
181da3b867eSMike Smith  * |7|6|5|4|3|2|1|0|
182da3b867eSMike Smith  * +-+-+-+-+-+-+-+-+
183da3b867eSMike Smith  *  | | | | | | | |
184da3b867eSMike Smith  *  | | | | | | | +- Output Buffer Full?
185da3b867eSMike Smith  *  | | | | | | +--- Input Buffer Full?
186da3b867eSMike Smith  *  | | | | | +----- <reserved>
187da3b867eSMike Smith  *  | | | | +------- Data Register is Command Byte?
188da3b867eSMike Smith  *  | | | +--------- Burst Mode Enabled?
189da3b867eSMike Smith  *  | | +----------- SCI Event?
190da3b867eSMike Smith  *  | +------------- SMI Event?
191da3b867eSMike Smith  *  +--------------- <Reserved>
192da3b867eSMike Smith  *
193da3b867eSMike Smith  */
194da3b867eSMike Smith typedef UINT8				EC_STATUS;
195da3b867eSMike Smith 
196da3b867eSMike Smith #define EC_FLAG_OUTPUT_BUFFER		((EC_STATUS) 0x01)
197da3b867eSMike Smith #define EC_FLAG_INPUT_BUFFER		((EC_STATUS) 0x02)
198da3b867eSMike Smith #define EC_FLAG_BURST_MODE		((EC_STATUS) 0x10)
199da3b867eSMike Smith #define EC_FLAG_SCI			((EC_STATUS) 0x20)
200da3b867eSMike Smith 
201da3b867eSMike Smith /*
202da3b867eSMike Smith  * EC_EVENT:
203da3b867eSMike Smith  * ---------
204da3b867eSMike Smith  */
205da3b867eSMike Smith typedef UINT8				EC_EVENT;
206da3b867eSMike Smith 
207da3b867eSMike Smith #define EC_EVENT_UNKNOWN		((EC_EVENT) 0x00)
208da3b867eSMike Smith #define EC_EVENT_OUTPUT_BUFFER_FULL	((EC_EVENT) 0x01)
209da3b867eSMike Smith #define EC_EVENT_INPUT_BUFFER_EMPTY	((EC_EVENT) 0x02)
210da3b867eSMike Smith #define EC_EVENT_SCI			((EC_EVENT) 0x20)
211da3b867eSMike Smith 
212da3b867eSMike Smith /*
213da3b867eSMike Smith  * Register access primitives
214da3b867eSMike Smith  */
215da3b867eSMike Smith #define EC_GET_DATA(sc)							\
216da3b867eSMike Smith 	bus_space_read_1((sc)->ec_data_tag, (sc)->ec_data_handle, 0)
217da3b867eSMike Smith 
218da3b867eSMike Smith #define EC_SET_DATA(sc, v)						\
219da3b867eSMike Smith 	bus_space_write_1((sc)->ec_data_tag, (sc)->ec_data_handle, 0, (v))
220da3b867eSMike Smith 
221da3b867eSMike Smith #define EC_GET_CSR(sc)							\
222da3b867eSMike Smith 	bus_space_read_1((sc)->ec_csr_tag, (sc)->ec_csr_handle, 0)
223da3b867eSMike Smith 
224da3b867eSMike Smith #define EC_SET_CSR(sc, v)						\
225da3b867eSMike Smith 	bus_space_write_1((sc)->ec_csr_tag, (sc)->ec_csr_handle, 0, (v))
226da3b867eSMike Smith 
227f8335e3aSNate Lawson /* Embedded Controller Boot Resources Table (ECDT) */
228f8335e3aSNate Lawson typedef struct {
229f8335e3aSNate Lawson     ACPI_TABLE_HEADER		header;
230f8335e3aSNate Lawson     ACPI_GENERIC_ADDRESS	control;
231f8335e3aSNate Lawson     ACPI_GENERIC_ADDRESS	data;
232f8335e3aSNate Lawson     UINT32			uid;
233f8335e3aSNate Lawson     UINT8			gpe_bit;
234f8335e3aSNate Lawson     char			ec_id[0];
235f8335e3aSNate Lawson } ACPI_TABLE_ECDT;
236f8335e3aSNate Lawson 
2370025fb0fSNate Lawson /* Additional params to pass from the probe routine */
2380025fb0fSNate Lawson struct acpi_ec_params {
2390025fb0fSNate Lawson     int		glk;
2400025fb0fSNate Lawson     int		gpe_bit;
2410025fb0fSNate Lawson     ACPI_HANDLE	gpe_handle;
2420025fb0fSNate Lawson     int		uid;
2430025fb0fSNate Lawson };
2440025fb0fSNate Lawson 
245f8335e3aSNate Lawson /* Indicate that this device has already been probed via ECDT. */
2460025fb0fSNate Lawson #define DEV_ECDT(x)		(acpi_get_magic(x) == (int)&acpi_ec_devclass)
247f8335e3aSNate Lawson 
248da3b867eSMike Smith /*
249da3b867eSMike Smith  * Driver softc.
250da3b867eSMike Smith  */
25115e32d5dSMike Smith struct acpi_ec_softc {
25215e32d5dSMike Smith     device_t		ec_dev;
25315e32d5dSMike Smith     ACPI_HANDLE		ec_handle;
2540025fb0fSNate Lawson     int			ec_uid;
2550025fb0fSNate Lawson     ACPI_HANDLE		ec_gpehandle;
2563a371f32SNate Lawson     UINT8		ec_gpebit;
2573a371f32SNate Lawson     UINT8		ec_csrvalue;
25815e32d5dSMike Smith 
25915e32d5dSMike Smith     int			ec_data_rid;
26015e32d5dSMike Smith     struct resource	*ec_data_res;
26115e32d5dSMike Smith     bus_space_tag_t	ec_data_tag;
26215e32d5dSMike Smith     bus_space_handle_t	ec_data_handle;
26315e32d5dSMike Smith 
26415e32d5dSMike Smith     int			ec_csr_rid;
26515e32d5dSMike Smith     struct resource	*ec_csr_res;
26615e32d5dSMike Smith     bus_space_tag_t	ec_csr_tag;
26715e32d5dSMike Smith     bus_space_handle_t	ec_csr_handle;
26815e32d5dSMike Smith 
2691f04e8f5SNate Lawson     int			ec_glk;
2701f04e8f5SNate Lawson     int			ec_glkhandle;
2711f04e8f5SNate Lawson     struct mtx		ec_mtx;
2723a371f32SNate Lawson     int			ec_polldelay;
27315e32d5dSMike Smith };
27415e32d5dSMike Smith 
2751f04e8f5SNate Lawson /*
2761f04e8f5SNate Lawson  * XXX
2771f04e8f5SNate Lawson  * I couldn't find it in the spec but other implementations also use a
2781f04e8f5SNate Lawson  * value of 1 ms for the time to acquire global lock.
2791f04e8f5SNate Lawson  */
2801f04e8f5SNate Lawson #define EC_LOCK_TIMEOUT	1000
2814690674eSMitsuru IWASAKI 
2821f04e8f5SNate Lawson /*
2831f04e8f5SNate Lawson  * Start with an interval of 1 us for status poll loop.  This delay
2841f04e8f5SNate Lawson  * will be dynamically adjusted based on the actual time waited.
2851f04e8f5SNate Lawson  */
2861f04e8f5SNate Lawson #define EC_POLL_DELAY	1
2871f04e8f5SNate Lawson 
2881f04e8f5SNate Lawson /* Total time in ms spent in the poll loop waiting for a response. */
289c87cfaedSNate Lawson #define EC_POLL_TIMEOUT	100
29015e32d5dSMike Smith 
291ff40920eSNate Lawson #define EVENT_READY(event, status)			\
292ff40920eSNate Lawson 	(((event) == EC_EVENT_OUTPUT_BUFFER_FULL &&	\
293ff40920eSNate Lawson 	 ((status) & EC_FLAG_OUTPUT_BUFFER) != 0) ||	\
294ff40920eSNate Lawson 	 ((event) == EC_EVENT_INPUT_BUFFER_EMPTY && 	\
295ff40920eSNate Lawson 	 ((status) & EC_FLAG_INPUT_BUFFER) == 0))
296ff40920eSNate Lawson 
297a329ebcaSNate Lawson static int	ec_poll_timeout = EC_POLL_TIMEOUT;
298a329ebcaSNate Lawson TUNABLE_INT("hw.acpi.ec.poll_timeout", &ec_poll_timeout);
299a329ebcaSNate Lawson 
30015e32d5dSMike Smith static __inline ACPI_STATUS
30115e32d5dSMike Smith EcLock(struct acpi_ec_softc *sc)
30215e32d5dSMike Smith {
3031f04e8f5SNate Lawson     ACPI_STATUS	status = AE_OK;
30415e32d5dSMike Smith 
3051f04e8f5SNate Lawson     /* Always acquire this EC's mutex. */
3061f04e8f5SNate Lawson     mtx_lock(&sc->ec_mtx);
3071f04e8f5SNate Lawson 
3081f04e8f5SNate Lawson     /* If _GLK is non-zero, also acquire the global lock. */
3091f04e8f5SNate Lawson     if (sc->ec_glk) {
3101f04e8f5SNate Lawson 	status = AcpiAcquireGlobalLock(EC_LOCK_TIMEOUT, &sc->ec_glkhandle);
3111f04e8f5SNate Lawson 	if (ACPI_FAILURE(status))
3121f04e8f5SNate Lawson 	    mtx_unlock(&sc->ec_mtx);
3131f04e8f5SNate Lawson     }
314c573e654SMitsuru IWASAKI 
31515e32d5dSMike Smith     return (status);
31615e32d5dSMike Smith }
31715e32d5dSMike Smith 
31815e32d5dSMike Smith static __inline void
31915e32d5dSMike Smith EcUnlock(struct acpi_ec_softc *sc)
32015e32d5dSMike Smith {
3211f04e8f5SNate Lawson     if (sc->ec_glk)
3221f04e8f5SNate Lawson 	AcpiReleaseGlobalLock(sc->ec_glkhandle);
3231f04e8f5SNate Lawson     mtx_unlock(&sc->ec_mtx);
32415e32d5dSMike Smith }
32515e32d5dSMike Smith 
326f3fc4f8bSNate Lawson static uint32_t		EcGpeHandler(void *Context);
32715e32d5dSMike Smith static ACPI_STATUS	EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function,
32815e32d5dSMike Smith 				void *Context, void **return_Context);
329464c662eSNate Lawson static ACPI_STATUS	EcSpaceHandler(UINT32 Function,
330464c662eSNate Lawson 				ACPI_PHYSICAL_ADDRESS Address,
331464c662eSNate Lawson 				UINT32 width, ACPI_INTEGER *Value,
33215e32d5dSMike Smith 				void *Context, void *RegionContext);
33315e32d5dSMike Smith static ACPI_STATUS	EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event);
3341f04e8f5SNate Lawson static ACPI_STATUS	EcCommand(struct acpi_ec_softc *sc, EC_COMMAND cmd);
335464c662eSNate Lawson static ACPI_STATUS	EcRead(struct acpi_ec_softc *sc, UINT8 Address,
336464c662eSNate Lawson 				UINT8 *Data);
337464c662eSNate Lawson static ACPI_STATUS	EcWrite(struct acpi_ec_softc *sc, UINT8 Address,
338464c662eSNate Lawson 				UINT8 *Data);
33915e32d5dSMike Smith static int		acpi_ec_probe(device_t dev);
34015e32d5dSMike Smith static int		acpi_ec_attach(device_t dev);
34115e32d5dSMike Smith 
34215e32d5dSMike Smith static device_method_t acpi_ec_methods[] = {
34315e32d5dSMike Smith     /* Device interface */
34415e32d5dSMike Smith     DEVMETHOD(device_probe,	acpi_ec_probe),
34515e32d5dSMike Smith     DEVMETHOD(device_attach,	acpi_ec_attach),
34615e32d5dSMike Smith 
34715e32d5dSMike Smith     {0, 0}
34815e32d5dSMike Smith };
34915e32d5dSMike Smith 
35015e32d5dSMike Smith static driver_t acpi_ec_driver = {
35115e32d5dSMike Smith     "acpi_ec",
35215e32d5dSMike Smith     acpi_ec_methods,
35315e32d5dSMike Smith     sizeof(struct acpi_ec_softc),
35415e32d5dSMike Smith };
35515e32d5dSMike Smith 
3563273b005SMike Smith static devclass_t acpi_ec_devclass;
35715e32d5dSMike Smith DRIVER_MODULE(acpi_ec, acpi, acpi_ec_driver, acpi_ec_devclass, 0, 0);
35864278df5SNate Lawson MODULE_DEPEND(acpi_ec, acpi, 1, 1, 1);
35915e32d5dSMike Smith 
36015e32d5dSMike Smith /*
361f8335e3aSNate Lawson  * Look for an ECDT and if we find one, set up default GPE and
362f8335e3aSNate Lawson  * space handlers to catch attempts to access EC space before
36315e32d5dSMike Smith  * we have a real driver instance in place.
364f8335e3aSNate Lawson  * TODO: if people report invalid ECDTs, add a tunable to disable them.
36515e32d5dSMike Smith  */
366f8335e3aSNate Lawson void
367f8335e3aSNate Lawson acpi_ec_ecdt_probe(device_t parent)
36815e32d5dSMike Smith {
369f8335e3aSNate Lawson     ACPI_TABLE_ECDT *ecdt;
370f8335e3aSNate Lawson     ACPI_STATUS	     status;
371f8335e3aSNate Lawson     device_t	     child;
372f8335e3aSNate Lawson     ACPI_HANDLE	     h;
3730025fb0fSNate Lawson     struct acpi_ec_params *params;
374f8335e3aSNate Lawson 
375b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
37615e32d5dSMike Smith 
377f8335e3aSNate Lawson     /* Find and validate the ECDT. */
378f8335e3aSNate Lawson     status = AcpiGetFirmwareTable("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
379f8335e3aSNate Lawson 		(ACPI_TABLE_HEADER **)&ecdt);
380f8335e3aSNate Lawson     if (ACPI_FAILURE(status) ||
381f8335e3aSNate Lawson 	ecdt->control.RegisterBitWidth != 8 ||
382f8335e3aSNate Lawson 	ecdt->data.RegisterBitWidth != 8) {
383f8335e3aSNate Lawson 	return;
38415e32d5dSMike Smith     }
38515e32d5dSMike Smith 
386f8335e3aSNate Lawson     /* Create the child device with the given unit number. */
387f8335e3aSNate Lawson     child = BUS_ADD_CHILD(parent, 0, "acpi_ec", ecdt->uid);
388f8335e3aSNate Lawson     if (child == NULL) {
3890025fb0fSNate Lawson 	printf("%s: can't add child\n", __func__);
390f8335e3aSNate Lawson 	return;
391f8335e3aSNate Lawson     }
392f8335e3aSNate Lawson 
393f8335e3aSNate Lawson     /* Find and save the ACPI handle for this device. */
394f8335e3aSNate Lawson     status = AcpiGetHandle(NULL, ecdt->ec_id, &h);
395f8335e3aSNate Lawson     if (ACPI_FAILURE(status)) {
396f8335e3aSNate Lawson 	device_delete_child(parent, child);
3970025fb0fSNate Lawson 	printf("%s: can't get handle\n", __func__);
398f8335e3aSNate Lawson 	return;
399f8335e3aSNate Lawson     }
400f8335e3aSNate Lawson     acpi_set_handle(child, h);
401f8335e3aSNate Lawson 
402f8335e3aSNate Lawson     /* Set the data and CSR register addresses. */
403f8335e3aSNate Lawson     bus_set_resource(child, SYS_RES_IOPORT, 0, ecdt->data.Address,
404f8335e3aSNate Lawson 	/*count*/1);
405f8335e3aSNate Lawson     bus_set_resource(child, SYS_RES_IOPORT, 1, ecdt->control.Address,
406f8335e3aSNate Lawson 	/*count*/1);
407f8335e3aSNate Lawson 
40815e32d5dSMike Smith     /*
409f8335e3aSNate Lawson      * Store values for the probe/attach routines to use.  Store the
410c868ac7dSNate Lawson      * ECDT GPE bit and set the global lock flag according to _GLK.
4110025fb0fSNate Lawson      * Note that it is not perfectly correct to be evaluating a method
4120025fb0fSNate Lawson      * before initializing devices, but in practice this function
4130025fb0fSNate Lawson      * should be safe to call at this point.
41415e32d5dSMike Smith      */
4150025fb0fSNate Lawson     params = malloc(sizeof(struct acpi_ec_params), M_TEMP, M_WAITOK | M_ZERO);
4160025fb0fSNate Lawson     params->gpe_handle = NULL;
4170025fb0fSNate Lawson     params->gpe_bit = ecdt->gpe_bit;
4180025fb0fSNate Lawson     params->uid = ecdt->uid;
4190025fb0fSNate Lawson     acpi_GetInteger(h, "_GLK", &params->glk);
4200025fb0fSNate Lawson     acpi_set_private(child, params);
4210025fb0fSNate Lawson     acpi_set_magic(child, (int)&acpi_ec_devclass);
422f8335e3aSNate Lawson 
423f8335e3aSNate Lawson     /* Finish the attach process. */
424f8335e3aSNate Lawson     if (device_probe_and_attach(child) != 0)
425f8335e3aSNate Lawson 	device_delete_child(parent, child);
426f8335e3aSNate Lawson }
427f8335e3aSNate Lawson 
42815e32d5dSMike Smith static int
42915e32d5dSMike Smith acpi_ec_probe(device_t dev)
43015e32d5dSMike Smith {
4310025fb0fSNate Lawson     ACPI_BUFFER buf;
432f8335e3aSNate Lawson     ACPI_HANDLE h;
4330025fb0fSNate Lawson     ACPI_OBJECT *obj;
434f8335e3aSNate Lawson     ACPI_STATUS status;
435f8335e3aSNate Lawson     device_t	peer;
436f8335e3aSNate Lawson     char	desc[64];
4370025fb0fSNate Lawson     int		ret;
4380025fb0fSNate Lawson     struct acpi_ec_params *params;
4390ae55423SMike Smith 
440c72508e2SNate Lawson     /* Check that this is a device and that EC is not disabled. */
441c72508e2SNate Lawson     if (acpi_get_type(dev) != ACPI_TYPE_DEVICE || acpi_disabled("ec"))
442f8335e3aSNate Lawson 	return (ENXIO);
44315e32d5dSMike Smith 
44415e32d5dSMike Smith     /*
445f8335e3aSNate Lawson      * If probed via ECDT, set description and continue.  Otherwise,
446f8335e3aSNate Lawson      * we can access the namespace and make sure this is not a
447f8335e3aSNate Lawson      * duplicate probe.
44815e32d5dSMike Smith      */
4490025fb0fSNate Lawson     ret = ENXIO;
4500025fb0fSNate Lawson     params = NULL;
4510025fb0fSNate Lawson     buf.Pointer = NULL;
4520025fb0fSNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
4530025fb0fSNate Lawson     if (DEV_ECDT(dev)) {
4540025fb0fSNate Lawson 	params = acpi_get_private(dev);
4551f04e8f5SNate Lawson 	ret = 0;
4560025fb0fSNate Lawson     } else if (acpi_MatchHid(dev, "PNP0C09")) {
4570025fb0fSNate Lawson 	params = malloc(sizeof(struct acpi_ec_params), M_TEMP,
4580025fb0fSNate Lawson 			M_WAITOK | M_ZERO);
459f8335e3aSNate Lawson 	h = acpi_get_handle(dev);
460f8335e3aSNate Lawson 
461f8335e3aSNate Lawson 	/*
462f8335e3aSNate Lawson 	 * Read the unit ID to check for duplicate attach and the
463f8335e3aSNate Lawson 	 * global lock value to see if we should acquire it when
464f8335e3aSNate Lawson 	 * accessing the EC.
465f8335e3aSNate Lawson 	 */
4660025fb0fSNate Lawson 	status = acpi_GetInteger(h, "_UID", &params->uid);
467f8335e3aSNate Lawson 	if (ACPI_FAILURE(status))
4680025fb0fSNate Lawson 	    params->uid = 0;
4690025fb0fSNate Lawson 	status = acpi_GetInteger(h, "_GLK", &params->glk);
470f8335e3aSNate Lawson 	if (ACPI_FAILURE(status))
4710025fb0fSNate Lawson 	    params->glk = 0;
472f8335e3aSNate Lawson 
473f8335e3aSNate Lawson 	/*
474f8335e3aSNate Lawson 	 * Evaluate the _GPE method to find the GPE bit used by the EC to
4750025fb0fSNate Lawson 	 * signal status (SCI).  If it's a package, it contains a reference
4760025fb0fSNate Lawson 	 * and GPE bit, similar to _PRW.
477f8335e3aSNate Lawson 	 */
4780025fb0fSNate Lawson 	status = AcpiEvaluateObject(h, "_GPE", NULL, &buf);
479f8335e3aSNate Lawson 	if (ACPI_FAILURE(status)) {
480f8335e3aSNate Lawson 	    device_printf(dev, "can't evaluate _GPE - %s\n",
481f8335e3aSNate Lawson 			  AcpiFormatException(status));
482f8335e3aSNate Lawson 	    return (ENXIO);
483f8335e3aSNate Lawson 	}
4840025fb0fSNate Lawson 	obj = (ACPI_OBJECT *)buf.Pointer;
4850025fb0fSNate Lawson 	if (obj == NULL)
4860025fb0fSNate Lawson 	    return (ENXIO);
4870025fb0fSNate Lawson 
4880025fb0fSNate Lawson 	switch (obj->Type) {
4890025fb0fSNate Lawson 	case ACPI_TYPE_INTEGER:
4900025fb0fSNate Lawson 	    params->gpe_handle = NULL;
4910025fb0fSNate Lawson 	    params->gpe_bit = obj->Integer.Value;
4920025fb0fSNate Lawson 	    break;
4930025fb0fSNate Lawson 	case ACPI_TYPE_PACKAGE:
4940025fb0fSNate Lawson 	    if (!ACPI_PKG_VALID(obj, 2))
4950025fb0fSNate Lawson 		goto out;
4960025fb0fSNate Lawson 	    params->gpe_handle =
4970025fb0fSNate Lawson 		acpi_GetReference(NULL, &obj->Package.Elements[0]);
4980025fb0fSNate Lawson 	    if (params->gpe_handle == NULL ||
4990025fb0fSNate Lawson 		acpi_PkgInt32(obj, 1, &params->gpe_bit) != 0)
5000025fb0fSNate Lawson 		goto out;
5010025fb0fSNate Lawson 	    break;
5020025fb0fSNate Lawson 	default:
5030025fb0fSNate Lawson 	    device_printf(dev, "_GPE has invalid type %d\n", obj->Type);
5040025fb0fSNate Lawson 	    goto out;
5050025fb0fSNate Lawson 	}
506f8335e3aSNate Lawson 
507f8335e3aSNate Lawson 	/* Store the values we got from the namespace for attach. */
5080025fb0fSNate Lawson 	acpi_set_private(dev, params);
509f8335e3aSNate Lawson 
510f8335e3aSNate Lawson 	/*
511f8335e3aSNate Lawson 	 * Check for a duplicate probe.  This can happen when a probe
512c868ac7dSNate Lawson 	 * via ECDT succeeded already.  If this is a duplicate, disable
513c868ac7dSNate Lawson 	 * this device.
514f8335e3aSNate Lawson 	 */
5150025fb0fSNate Lawson 	peer = devclass_get_device(acpi_ec_devclass, params->uid);
516c868ac7dSNate Lawson 	if (peer == NULL || !device_is_alive(peer))
517f8335e3aSNate Lawson 	    ret = 0;
518c868ac7dSNate Lawson 	else
519c868ac7dSNate Lawson 	    device_disable(dev);
520c868ac7dSNate Lawson     }
521f8335e3aSNate Lawson 
5220025fb0fSNate Lawson out:
523c868ac7dSNate Lawson     if (ret == 0) {
524c868ac7dSNate Lawson 	snprintf(desc, sizeof(desc), "Embedded Controller: GPE %#x%s%s",
5250025fb0fSNate Lawson 		 params->gpe_bit, (params->glk) ? ", GLK" : "",
526c868ac7dSNate Lawson 		 DEV_ECDT(dev) ? ", ECDT" : "");
527c868ac7dSNate Lawson 	device_set_desc_copy(dev, desc);
52815e32d5dSMike Smith     }
5291f04e8f5SNate Lawson 
5300025fb0fSNate Lawson     if (ret > 0 && params)
5310025fb0fSNate Lawson 	free(params, M_TEMP);
5320025fb0fSNate Lawson     if (buf.Pointer)
5330025fb0fSNate Lawson 	AcpiOsFree(buf.Pointer);
5341f04e8f5SNate Lawson     return (ret);
53515e32d5dSMike Smith }
53615e32d5dSMike Smith 
53715e32d5dSMike Smith static int
53815e32d5dSMike Smith acpi_ec_attach(device_t dev)
53915e32d5dSMike Smith {
54015e32d5dSMike Smith     struct acpi_ec_softc	*sc;
5410025fb0fSNate Lawson     struct acpi_ec_params	*params;
54215e32d5dSMike Smith     ACPI_STATUS			Status;
543dbd0058aSMike Smith 
544b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
5450ae55423SMike Smith 
5461f04e8f5SNate Lawson     /* Fetch/initialize softc (assumes softc is pre-zeroed). */
54715e32d5dSMike Smith     sc = device_get_softc(dev);
5480025fb0fSNate Lawson     params = acpi_get_private(dev);
54915e32d5dSMike Smith     sc->ec_dev = dev;
55015e32d5dSMike Smith     sc->ec_handle = acpi_get_handle(dev);
5511f04e8f5SNate Lawson     sc->ec_polldelay = EC_POLL_DELAY;
5521f04e8f5SNate Lawson     mtx_init(&sc->ec_mtx, "ACPI embedded controller", NULL, MTX_DEF);
55315e32d5dSMike Smith 
554f8335e3aSNate Lawson     /* Retrieve previously probed values via device ivars. */
5550025fb0fSNate Lawson     sc->ec_glk = params->glk;
5560025fb0fSNate Lawson     sc->ec_gpebit = params->gpe_bit;
5570025fb0fSNate Lawson     sc->ec_gpehandle = params->gpe_handle;
5580025fb0fSNate Lawson     sc->ec_uid = params->uid;
5590025fb0fSNate Lawson     free(params, M_TEMP);
560f8335e3aSNate Lawson 
5611f04e8f5SNate Lawson     /* Attach bus resources for data and command/status ports. */
56215e32d5dSMike Smith     sc->ec_data_rid = 0;
5635f96beb9SNate Lawson     sc->ec_data_res = bus_alloc_resource_any(sc->ec_dev, SYS_RES_IOPORT,
5645f96beb9SNate Lawson 			&sc->ec_data_rid, RF_ACTIVE);
565464c662eSNate Lawson     if (sc->ec_data_res == NULL) {
56615e32d5dSMike Smith 	device_printf(dev, "can't allocate data port\n");
567f3fc4f8bSNate Lawson 	goto error;
56815e32d5dSMike Smith     }
56915e32d5dSMike Smith     sc->ec_data_tag = rman_get_bustag(sc->ec_data_res);
57015e32d5dSMike Smith     sc->ec_data_handle = rman_get_bushandle(sc->ec_data_res);
57115e32d5dSMike Smith 
57215e32d5dSMike Smith     sc->ec_csr_rid = 1;
5735f96beb9SNate Lawson     sc->ec_csr_res = bus_alloc_resource_any(sc->ec_dev, SYS_RES_IOPORT,
5745f96beb9SNate Lawson 			&sc->ec_csr_rid, RF_ACTIVE);
575464c662eSNate Lawson     if (sc->ec_csr_res == NULL) {
57615e32d5dSMike Smith 	device_printf(dev, "can't allocate command/status port\n");
577f3fc4f8bSNate Lawson 	goto error;
57815e32d5dSMike Smith     }
57915e32d5dSMike Smith     sc->ec_csr_tag = rman_get_bustag(sc->ec_csr_res);
58015e32d5dSMike Smith     sc->ec_csr_handle = rman_get_bushandle(sc->ec_csr_res);
58115e32d5dSMike Smith 
58215e32d5dSMike Smith     /*
5831f04e8f5SNate Lawson      * Install a handler for this EC's GPE bit.  We want edge-triggered
5841f04e8f5SNate Lawson      * behavior.
58515e32d5dSMike Smith      */
5861f04e8f5SNate Lawson     ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "attaching GPE handler\n"));
5870025fb0fSNate Lawson     Status = AcpiInstallGpeHandler(sc->ec_gpehandle, sc->ec_gpebit,
588b7d13479SNate Lawson 		ACPI_GPE_EDGE_TRIGGERED, &EcGpeHandler, sc);
589464c662eSNate Lawson     if (ACPI_FAILURE(Status)) {
59076d1dff4SMike Smith 	device_printf(dev, "can't install GPE handler for %s - %s\n",
591bfae45aaSMike Smith 		      acpi_name(sc->ec_handle), AcpiFormatException(Status));
592f3fc4f8bSNate Lawson 	goto error;
59315e32d5dSMike Smith     }
59415e32d5dSMike Smith 
59515e32d5dSMike Smith     /*
59615e32d5dSMike Smith      * Install address space handler
59715e32d5dSMike Smith      */
5984c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "attaching address space handler\n"));
599464c662eSNate Lawson     Status = AcpiInstallAddressSpaceHandler(sc->ec_handle, ACPI_ADR_SPACE_EC,
6001f04e8f5SNate Lawson 		&EcSpaceHandler, &EcSpaceSetup, sc);
601464c662eSNate Lawson     if (ACPI_FAILURE(Status)) {
60276d1dff4SMike Smith 	device_printf(dev, "can't install address space handler for %s - %s\n",
603bfae45aaSMike Smith 		      acpi_name(sc->ec_handle), AcpiFormatException(Status));
604f3fc4f8bSNate Lawson 	goto error;
605f3fc4f8bSNate Lawson     }
606f3fc4f8bSNate Lawson 
607f3fc4f8bSNate Lawson     /* Enable runtime GPEs for the handler. */
608f3fc4f8bSNate Lawson     Status = AcpiSetGpeType(sc->ec_gpehandle, sc->ec_gpebit,
609f3fc4f8bSNate Lawson 			    ACPI_GPE_TYPE_RUNTIME);
610f3fc4f8bSNate Lawson     if (ACPI_FAILURE(Status)) {
611f3fc4f8bSNate Lawson 	device_printf(dev, "AcpiSetGpeType failed: %s\n",
612f3fc4f8bSNate Lawson 		      AcpiFormatException(Status));
613f3fc4f8bSNate Lawson 	goto error;
614f3fc4f8bSNate Lawson     }
615f3fc4f8bSNate Lawson     Status = AcpiEnableGpe(sc->ec_gpehandle, sc->ec_gpebit, ACPI_NOT_ISR);
616f3fc4f8bSNate Lawson     if (ACPI_FAILURE(Status)) {
617f3fc4f8bSNate Lawson 	device_printf(dev, "AcpiEnableGpe failed: %s\n",
618f3fc4f8bSNate Lawson 		      AcpiFormatException(Status));
619f3fc4f8bSNate Lawson 	goto error;
62015e32d5dSMike Smith     }
6211f04e8f5SNate Lawson 
6221f04e8f5SNate Lawson     ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "acpi_ec_attach complete\n"));
6231f04e8f5SNate Lawson     return (0);
624464c662eSNate Lawson 
625f3fc4f8bSNate Lawson error:
626f3fc4f8bSNate Lawson     AcpiRemoveGpeHandler(sc->ec_gpehandle, sc->ec_gpebit, &EcGpeHandler);
627f3fc4f8bSNate Lawson     AcpiRemoveAddressSpaceHandler(sc->ec_handle, ACPI_ADR_SPACE_EC,
628f3fc4f8bSNate Lawson 	EcSpaceHandler);
629f8372adeSTakanori Watanabe     if (sc->ec_csr_res)
630f8372adeSTakanori Watanabe 	bus_release_resource(sc->ec_dev, SYS_RES_IOPORT, sc->ec_csr_rid,
631f8372adeSTakanori Watanabe 			     sc->ec_csr_res);
632f8372adeSTakanori Watanabe     if (sc->ec_data_res)
633f8372adeSTakanori Watanabe 	bus_release_resource(sc->ec_dev, SYS_RES_IOPORT, sc->ec_data_rid,
634f8372adeSTakanori Watanabe 			     sc->ec_data_res);
635ff40920eSNate Lawson     mtx_destroy(&sc->ec_mtx);
636f3fc4f8bSNate Lawson     return (ENXIO);
6371f04e8f5SNate Lawson }
6381f04e8f5SNate Lawson 
63915e32d5dSMike Smith static void
640c07572e7STakanori Watanabe EcGpeQueryHandler(void *Context)
64115e32d5dSMike Smith {
64215e32d5dSMike Smith     struct acpi_ec_softc	*sc = (struct acpi_ec_softc *)Context;
64315e32d5dSMike Smith     UINT8			Data;
64415e32d5dSMike Smith     ACPI_STATUS			Status;
6451f04e8f5SNate Lawson     EC_STATUS			EcStatus;
64615e32d5dSMike Smith     char			qxx[5];
64715e32d5dSMike Smith 
648b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
6491f04e8f5SNate Lawson     KASSERT(Context != NULL, ("EcGpeQueryHandler called with NULL"));
65015e32d5dSMike Smith 
6513a371f32SNate Lawson     Status = EcLock(sc);
6523a371f32SNate Lawson     if (ACPI_FAILURE(Status)) {
6533a371f32SNate Lawson 	ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
6543a371f32SNate Lawson 		    "GpeQuery lock error: %s\n", AcpiFormatException(Status));
6553a371f32SNate Lawson 	return;
6563a371f32SNate Lawson     }
6573a371f32SNate Lawson 
65815e32d5dSMike Smith     /*
6593a371f32SNate Lawson      * If the EC_SCI bit of the status register is not set, then pass
6603a371f32SNate Lawson      * it along to any potential waiters as it may be an IBE/OBF event.
66115e32d5dSMike Smith      */
6621f04e8f5SNate Lawson     EcStatus = EC_GET_CSR(sc);
663ff40920eSNate Lawson     if ((EcStatus & EC_EVENT_SCI) == 0) {
6643a371f32SNate Lawson 	sc->ec_csrvalue = EcStatus;
6653a371f32SNate Lawson 	wakeup(&sc->ec_csrvalue);
6663a371f32SNate Lawson 	EcUnlock(sc);
6671f04e8f5SNate Lawson 	goto re_enable;
668ff40920eSNate Lawson     }
66915e32d5dSMike Smith 
6703a371f32SNate Lawson     /*
6713a371f32SNate Lawson      * Send a query command to the EC to find out which _Qxx call it
6723a371f32SNate Lawson      * wants to make.  This command clears the SCI bit and also the
6733a371f32SNate Lawson      * interrupt source since we are edge-triggered.
6743a371f32SNate Lawson      */
6753a371f32SNate Lawson     Status = EcCommand(sc, EC_COMMAND_QUERY);
676dbd0058aSMike Smith     if (ACPI_FAILURE(Status)) {
6773a371f32SNate Lawson 	EcUnlock(sc);
6786971b3c7SMitsuru IWASAKI 	ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
6796971b3c7SMitsuru IWASAKI 		    "GPE query failed - %s\n", AcpiFormatException(Status));
6801f04e8f5SNate Lawson 	goto re_enable;
68115e32d5dSMike Smith     }
6823a371f32SNate Lawson     Data = EC_GET_DATA(sc);
6833a371f32SNate Lawson     EcUnlock(sc);
68415e32d5dSMike Smith 
6851f04e8f5SNate Lawson     /* Ignore the value for "no outstanding event". (13.3.5) */
6861f04e8f5SNate Lawson     if (Data == 0)
6871f04e8f5SNate Lawson 	goto re_enable;
6881f04e8f5SNate Lawson 
6891f04e8f5SNate Lawson     /* Evaluate _Qxx to respond to the controller. */
69015e32d5dSMike Smith     sprintf(qxx, "_Q%02x", Data);
69115e32d5dSMike Smith     strupr(qxx);
692ee785aa9SJohn Baldwin     Status = AcpiEvaluateObject(sc->ec_handle, qxx, NULL, NULL);
6931f04e8f5SNate Lawson     if (ACPI_FAILURE(Status) && Status != AE_NOT_FOUND) {
6946971b3c7SMitsuru IWASAKI 	ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
6956971b3c7SMitsuru IWASAKI 		    "evaluation of GPE query method %s failed - %s\n",
696bfae45aaSMike Smith 		    qxx, AcpiFormatException(Status));
69715e32d5dSMike Smith     }
6981f04e8f5SNate Lawson 
6991f04e8f5SNate Lawson re_enable:
7001f04e8f5SNate Lawson     /* Re-enable the GPE event so we'll get future requests. */
701f3fc4f8bSNate Lawson     Status = AcpiEnableGpe(sc->ec_gpehandle, sc->ec_gpebit, ACPI_NOT_ISR);
7021f04e8f5SNate Lawson     if (ACPI_FAILURE(Status))
7031f04e8f5SNate Lawson 	printf("EcGpeQueryHandler: AcpiEnableEvent failed\n");
704c07572e7STakanori Watanabe }
70542f6d122SMike Smith 
706a9cf0dffSMike Smith /*
7071f04e8f5SNate Lawson  * Handle a GPE.  Currently we only handle SCI events as others must
7081f04e8f5SNate Lawson  * be handled by polling in EcWaitEvent().  This is because some ECs
7091f04e8f5SNate Lawson  * treat events as level when they should be edge-triggered.
710a9cf0dffSMike Smith  */
711f3fc4f8bSNate Lawson static uint32_t
712a9cf0dffSMike Smith EcGpeHandler(void *Context)
713c07572e7STakanori Watanabe {
714c07572e7STakanori Watanabe     struct acpi_ec_softc *sc = Context;
7151f04e8f5SNate Lawson     ACPI_STATUS		       Status;
716a9cf0dffSMike Smith 
7171f04e8f5SNate Lawson     KASSERT(Context != NULL, ("EcGpeHandler called with NULL"));
7181f04e8f5SNate Lawson 
7191f04e8f5SNate Lawson     /* Disable further GPEs while we handle this one. */
720f3fc4f8bSNate Lawson     AcpiDisableGpe(sc->ec_gpehandle, sc->ec_gpebit, ACPI_NOT_ISR);
7211f04e8f5SNate Lawson 
7221f04e8f5SNate Lawson     /* Schedule the GPE query handler. */
7231f04e8f5SNate Lawson     Status = AcpiOsQueueForExecution(OSD_PRIORITY_GPE, EcGpeQueryHandler,
7241f04e8f5SNate Lawson 		Context);
7251f04e8f5SNate Lawson     if (ACPI_FAILURE(Status)) {
7261f04e8f5SNate Lawson 	printf("Queuing GPE query handler failed.\n");
727f3fc4f8bSNate Lawson 	Status = AcpiEnableGpe(sc->ec_gpehandle, sc->ec_gpebit, ACPI_NOT_ISR);
7281f04e8f5SNate Lawson 	if (ACPI_FAILURE(Status))
7291f04e8f5SNate Lawson 	    printf("EcGpeHandler: AcpiEnableEvent failed\n");
730c07572e7STakanori Watanabe     }
731f3fc4f8bSNate Lawson 
732f3fc4f8bSNate Lawson     return (0);
73315e32d5dSMike Smith }
73415e32d5dSMike Smith 
73515e32d5dSMike Smith static ACPI_STATUS
736464c662eSNate Lawson EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function, void *Context,
737464c662eSNate Lawson 	     void **RegionContext)
73815e32d5dSMike Smith {
73942f6d122SMike Smith 
740b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
74142f6d122SMike Smith 
74215e32d5dSMike Smith     /*
74316844a97SNate Lawson      * If deactivating a region, always set the output to NULL.  Otherwise,
74416844a97SNate Lawson      * just pass the context through.
74515e32d5dSMike Smith      */
74616844a97SNate Lawson     if (Function == ACPI_REGION_DEACTIVATE)
74716844a97SNate Lawson 	*RegionContext = NULL;
74816844a97SNate Lawson     else
74915e32d5dSMike Smith 	*RegionContext = Context;
75015e32d5dSMike Smith 
75142f6d122SMike Smith     return_ACPI_STATUS (AE_OK);
75215e32d5dSMike Smith }
75315e32d5dSMike Smith 
75415e32d5dSMike Smith static ACPI_STATUS
755464c662eSNate Lawson EcSpaceHandler(UINT32 Function, ACPI_PHYSICAL_ADDRESS Address, UINT32 width,
756464c662eSNate Lawson 	       ACPI_INTEGER *Value, void *Context, void *RegionContext)
75715e32d5dSMike Smith {
75815e32d5dSMike Smith     struct acpi_ec_softc	*sc = (struct acpi_ec_softc *)Context;
7594ed391b8SNate Lawson     ACPI_STATUS			Status;
7601f04e8f5SNate Lawson     UINT8			EcAddr, EcData;
761ee785aa9SJohn Baldwin     int				i;
76215e32d5dSMike Smith 
763b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, (UINT32)Address);
7640ae55423SMike Smith 
7654ed391b8SNate Lawson     if (width % 8 != 0 || Value == NULL || Context == NULL)
7660ae55423SMike Smith 	return_ACPI_STATUS (AE_BAD_PARAMETER);
7674ed391b8SNate Lawson     if (Address + (width / 8) - 1 > 0xFF)
7684ed391b8SNate Lawson 	return_ACPI_STATUS (AE_BAD_ADDRESS);
76915e32d5dSMike Smith 
7704ed391b8SNate Lawson     if (Function == ACPI_READ)
7714ed391b8SNate Lawson 	*Value = 0;
7721f04e8f5SNate Lawson     EcAddr = Address;
7734ed391b8SNate Lawson     Status = AE_ERROR;
7744ed391b8SNate Lawson 
7754ed391b8SNate Lawson     /* Perform the transaction(s), based on width. */
7764ed391b8SNate Lawson     for (i = 0; i < width; i += 8, EcAddr++) {
7771f04e8f5SNate Lawson 	Status = EcLock(sc);
778464c662eSNate Lawson 	if (ACPI_FAILURE(Status))
7794ed391b8SNate Lawson 	    break;
7801f04e8f5SNate Lawson 
7811f04e8f5SNate Lawson 	switch (Function) {
7821f04e8f5SNate Lawson 	case ACPI_READ:
7831f04e8f5SNate Lawson 	    Status = EcRead(sc, EcAddr, &EcData);
7844ed391b8SNate Lawson 	    if (ACPI_SUCCESS(Status))
7854ed391b8SNate Lawson 		*Value |= ((ACPI_INTEGER)EcData) << i;
786ee785aa9SJohn Baldwin 	    break;
7871f04e8f5SNate Lawson 	case ACPI_WRITE:
7881f04e8f5SNate Lawson 	    EcData = (UINT8)((*Value) >> i);
7891f04e8f5SNate Lawson 	    Status = EcWrite(sc, EcAddr, &EcData);
7901f04e8f5SNate Lawson 	    break;
7911f04e8f5SNate Lawson 	default:
7921f04e8f5SNate Lawson 	    device_printf(sc->ec_dev, "invalid EcSpaceHandler function %d\n",
7931f04e8f5SNate Lawson 			  Function);
7941f04e8f5SNate Lawson 	    Status = AE_BAD_PARAMETER;
7951f04e8f5SNate Lawson 	    break;
7961f04e8f5SNate Lawson 	}
7971f04e8f5SNate Lawson 	EcUnlock(sc);
7981f04e8f5SNate Lawson 	if (ACPI_FAILURE(Status))
7994ed391b8SNate Lawson 	    break;
800ee785aa9SJohn Baldwin     }
8014ed391b8SNate Lawson 
8020ae55423SMike Smith     return_ACPI_STATUS (Status);
80315e32d5dSMike Smith }
8042a4ac806SMike Smith 
80515e32d5dSMike Smith static ACPI_STATUS
80615e32d5dSMike Smith EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event)
80715e32d5dSMike Smith {
80815e32d5dSMike Smith     EC_STATUS	EcStatus;
8091f04e8f5SNate Lawson     ACPI_STATUS	Status;
8103a371f32SNate Lawson     int		i, period, retval;
81144904c40SNate Lawson     static int	EcDbgMaxDelay;
81215e32d5dSMike Smith 
8131f04e8f5SNate Lawson     mtx_assert(&sc->ec_mtx, MA_OWNED);
8141f04e8f5SNate Lawson     Status = AE_NO_HARDWARE_RESPONSE;
81515e32d5dSMike Smith 
81615e32d5dSMike Smith     /*
8171f04e8f5SNate Lawson      * Wait for 1 us before checking the CSR.  Testing shows about
8181f04e8f5SNate Lawson      * 50% of requests complete in 1 us and 90% of them complete
8191f04e8f5SNate Lawson      * in 5 us or less.
82015e32d5dSMike Smith      */
821da3b867eSMike Smith     AcpiOsStall(1);
82215e32d5dSMike Smith 
82315e32d5dSMike Smith     /*
82415e32d5dSMike Smith      * Poll the EC status register to detect completion of the last
825ff40920eSNate Lawson      * command.  First, wait up to 1 ms in chunks of sc->ec_polldelay
826ff40920eSNate Lawson      * microseconds.
82715e32d5dSMike Smith      */
828ff40920eSNate Lawson     for (i = 0; i < 1000 / sc->ec_polldelay; i++) {
82915e32d5dSMike Smith 	EcStatus = EC_GET_CSR(sc);
830ff40920eSNate Lawson 	if (EVENT_READY(Event, EcStatus)) {
8311f04e8f5SNate Lawson 	    Status = AE_OK;
83215e32d5dSMike Smith 	    break;
8331f04e8f5SNate Lawson 	}
8341f04e8f5SNate Lawson 	AcpiOsStall(sc->ec_polldelay);
8351f04e8f5SNate Lawson     }
8361f04e8f5SNate Lawson 
8371f04e8f5SNate Lawson     /* Scale poll delay by the amount of time actually waited. */
8381f04e8f5SNate Lawson     period = i * sc->ec_polldelay;
8391f04e8f5SNate Lawson     if (period <= 5)
8401f04e8f5SNate Lawson 	sc->ec_polldelay = 1;
8411f04e8f5SNate Lawson     else if (period <= 20)
8421f04e8f5SNate Lawson 	sc->ec_polldelay = 5;
8431f04e8f5SNate Lawson     else if (period <= 100)
8441f04e8f5SNate Lawson 	sc->ec_polldelay = 10;
8451f04e8f5SNate Lawson     else
8461f04e8f5SNate Lawson 	sc->ec_polldelay = 100;
847ff40920eSNate Lawson 
848ff40920eSNate Lawson     /*
849a329ebcaSNate Lawson      * If we still don't have a response, wait up to ec_poll_timeout ms
850ff40920eSNate Lawson      * for completion, sleeping for chunks of 10 ms.
851ff40920eSNate Lawson      */
852ff40920eSNate Lawson     if (Status != AE_OK) {
8533a371f32SNate Lawson 	retval = -1;
854a329ebcaSNate Lawson 	for (i = 0; i < ec_poll_timeout / 10; i++) {
8553a371f32SNate Lawson 	    if (retval != 0)
856ff40920eSNate Lawson 		EcStatus = EC_GET_CSR(sc);
8573a371f32SNate Lawson 	    else
8583a371f32SNate Lawson 		EcStatus = sc->ec_csrvalue;
859ff40920eSNate Lawson 	    if (EVENT_READY(Event, EcStatus)) {
860ff40920eSNate Lawson 		Status = AE_OK;
861ff40920eSNate Lawson 		break;
862ff40920eSNate Lawson 	    }
8633a371f32SNate Lawson 	    retval = msleep(&sc->ec_csrvalue, &sc->ec_mtx, PZERO, "ecpoll",
8643a371f32SNate Lawson 			    10/*ms*/);
865ff40920eSNate Lawson 	}
8661f04e8f5SNate Lawson     }
8671f04e8f5SNate Lawson 
86844904c40SNate Lawson     /* Calculate new delay and print it if it exceeds the max. */
86944904c40SNate Lawson     if (period == 1000)
87044904c40SNate Lawson 	period += i * 10000;
87144904c40SNate Lawson     if (period > EcDbgMaxDelay) {
87244904c40SNate Lawson 	EcDbgMaxDelay = period;
87344904c40SNate Lawson 	ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
87444904c40SNate Lawson 		    "info: new max delay is %d us\n", period);
87544904c40SNate Lawson     }
87644904c40SNate Lawson 
8771f04e8f5SNate Lawson     return (Status);
8781f04e8f5SNate Lawson }
8791f04e8f5SNate Lawson 
8801f04e8f5SNate Lawson static ACPI_STATUS
8811f04e8f5SNate Lawson EcCommand(struct acpi_ec_softc *sc, EC_COMMAND cmd)
8821f04e8f5SNate Lawson {
8831f04e8f5SNate Lawson     ACPI_STATUS	Status;
8841f04e8f5SNate Lawson     EC_EVENT	Event;
8851f04e8f5SNate Lawson 
8861f04e8f5SNate Lawson     mtx_assert(&sc->ec_mtx, MA_OWNED);
8871f04e8f5SNate Lawson 
8881f04e8f5SNate Lawson     /* Decide what to wait for based on command type. */
8891f04e8f5SNate Lawson     switch (cmd) {
8901f04e8f5SNate Lawson     case EC_COMMAND_READ:
89115e32d5dSMike Smith     case EC_COMMAND_WRITE:
8921f04e8f5SNate Lawson     case EC_COMMAND_BURST_DISABLE:
8931f04e8f5SNate Lawson 	Event = EC_EVENT_INPUT_BUFFER_EMPTY;
8941f04e8f5SNate Lawson 	break;
8951f04e8f5SNate Lawson     case EC_COMMAND_QUERY:
8961f04e8f5SNate Lawson     case EC_COMMAND_BURST_ENABLE:
8971f04e8f5SNate Lawson 	Event = EC_EVENT_OUTPUT_BUFFER_FULL;
89815e32d5dSMike Smith 	break;
89915e32d5dSMike Smith     default:
9006971b3c7SMitsuru IWASAKI 	ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
9011f04e8f5SNate Lawson 		    "EcCommand: Invalid command %#x\n", cmd);
9021f04e8f5SNate Lawson 	return (AE_BAD_PARAMETER);
903464c662eSNate Lawson     }
9041f04e8f5SNate Lawson 
9051f04e8f5SNate Lawson     /* Run the command and wait for the chosen event. */
9061f04e8f5SNate Lawson     EC_SET_CSR(sc, cmd);
9071f04e8f5SNate Lawson     Status = EcWaitEvent(sc, Event);
9081f04e8f5SNate Lawson     if (ACPI_FAILURE(Status)) {
9096971b3c7SMitsuru IWASAKI 	ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
9101f04e8f5SNate Lawson 		    "EcCommand: no response to %#x\n", cmd);
911464c662eSNate Lawson     }
91215e32d5dSMike Smith 
91315e32d5dSMike Smith     return (Status);
91415e32d5dSMike Smith }
91515e32d5dSMike Smith 
91615e32d5dSMike Smith static ACPI_STATUS
91715e32d5dSMike Smith EcRead(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data)
91815e32d5dSMike Smith {
91915e32d5dSMike Smith     ACPI_STATUS	Status;
92015e32d5dSMike Smith 
9211f04e8f5SNate Lawson     mtx_assert(&sc->ec_mtx, MA_OWNED);
92215e32d5dSMike Smith 
9231f04e8f5SNate Lawson #ifdef notyet
9241f04e8f5SNate Lawson     /* If we can't start burst mode, continue anyway. */
9251f04e8f5SNate Lawson     EcCommand(sc, EC_COMMAND_BURST_ENABLE);
9261f04e8f5SNate Lawson #endif
92715e32d5dSMike Smith 
9281f04e8f5SNate Lawson     Status = EcCommand(sc, EC_COMMAND_READ);
9291f04e8f5SNate Lawson     if (ACPI_FAILURE(Status))
93015e32d5dSMike Smith 	return (Status);
93115e32d5dSMike Smith 
93215e32d5dSMike Smith     EC_SET_DATA(sc, Address);
9331f04e8f5SNate Lawson     Status = EcWaitEvent(sc, EC_EVENT_OUTPUT_BUFFER_FULL);
934464c662eSNate Lawson     if (ACPI_FAILURE(Status)) {
9356971b3c7SMitsuru IWASAKI 	ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
9366971b3c7SMitsuru IWASAKI 		    "EcRead: Failed waiting for EC to send data.\n");
93715e32d5dSMike Smith 	return (Status);
93815e32d5dSMike Smith     }
93915e32d5dSMike Smith 
940464c662eSNate Lawson     *Data = EC_GET_DATA(sc);
94115e32d5dSMike Smith 
9421f04e8f5SNate Lawson #ifdef notyet
9431f04e8f5SNate Lawson     if (sc->ec_burstactive) {
9441f04e8f5SNate Lawson 	Status = EcCommand(sc, EC_COMMAND_BURST_DISABLE);
9451f04e8f5SNate Lawson 	if (ACPI_FAILURE(Status))
9461f04e8f5SNate Lawson 	    return (Status);
9471f04e8f5SNate Lawson     }
9481f04e8f5SNate Lawson #endif
94915e32d5dSMike Smith 
95015e32d5dSMike Smith     return (AE_OK);
95115e32d5dSMike Smith }
95215e32d5dSMike Smith 
95315e32d5dSMike Smith static ACPI_STATUS
95415e32d5dSMike Smith EcWrite(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data)
95515e32d5dSMike Smith {
95615e32d5dSMike Smith     ACPI_STATUS	Status;
95715e32d5dSMike Smith 
9581f04e8f5SNate Lawson     mtx_assert(&sc->ec_mtx, MA_OWNED);
95915e32d5dSMike Smith 
9601f04e8f5SNate Lawson #ifdef notyet
9611f04e8f5SNate Lawson     /* If we can't start burst mode, continue anyway. */
9621f04e8f5SNate Lawson     EcCommand(sc, EC_COMMAND_BURST_ENABLE);
9631f04e8f5SNate Lawson #endif
96415e32d5dSMike Smith 
9651f04e8f5SNate Lawson     Status = EcCommand(sc, EC_COMMAND_WRITE);
9661f04e8f5SNate Lawson     if (ACPI_FAILURE(Status))
96715e32d5dSMike Smith 	return (Status);
96815e32d5dSMike Smith 
96915e32d5dSMike Smith     EC_SET_DATA(sc, Address);
9701f04e8f5SNate Lawson     Status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY);
971464c662eSNate Lawson     if (ACPI_FAILURE(Status)) {
9726971b3c7SMitsuru IWASAKI 	ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
9731f04e8f5SNate Lawson 		    "EcRead: Failed waiting for EC to process address\n");
97415e32d5dSMike Smith 	return (Status);
97515e32d5dSMike Smith     }
97615e32d5dSMike Smith 
97715e32d5dSMike Smith     EC_SET_DATA(sc, *Data);
9781f04e8f5SNate Lawson     Status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY);
979464c662eSNate Lawson     if (ACPI_FAILURE(Status)) {
9806971b3c7SMitsuru IWASAKI 	ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
9811f04e8f5SNate Lawson 		    "EcWrite: Failed waiting for EC to process data\n");
98215e32d5dSMike Smith 	return (Status);
98315e32d5dSMike Smith     }
98415e32d5dSMike Smith 
9851f04e8f5SNate Lawson #ifdef notyet
9861f04e8f5SNate Lawson     if (sc->ec_burstactive) {
9871f04e8f5SNate Lawson 	Status = EcCommand(sc, EC_COMMAND_BURST_DISABLE);
9881f04e8f5SNate Lawson 	if (ACPI_FAILURE(Status))
9891f04e8f5SNate Lawson 	    return (Status);
9901f04e8f5SNate Lawson     }
9911f04e8f5SNate Lawson #endif
99215e32d5dSMike Smith 
99315e32d5dSMike Smith     return (AE_OK);
99415e32d5dSMike Smith }
995