xref: /freebsd/sys/dev/acpica/acpi_ec.c (revision 2be4e4713aac95323b389243f0f7fa00f6a6112a)
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  */
28098ca2bdSWarner Losh /*-
29098ca2bdSWarner Losh  ******************************************************************************
3015e32d5dSMike Smith  *
3115e32d5dSMike Smith  * 1. Copyright Notice
3215e32d5dSMike Smith  *
3315e32d5dSMike Smith  * Some or all of this work - Copyright (c) 1999, Intel Corp.  All rights
3415e32d5dSMike Smith  * reserved.
3515e32d5dSMike Smith  *
3615e32d5dSMike Smith  * 2. License
3715e32d5dSMike Smith  *
3815e32d5dSMike Smith  * 2.1. This is your license from Intel Corp. under its intellectual property
3915e32d5dSMike Smith  * rights.  You may have additional license terms from the party that provided
4015e32d5dSMike Smith  * you this software, covering your right to use that party's intellectual
4115e32d5dSMike Smith  * property rights.
4215e32d5dSMike Smith  *
4315e32d5dSMike Smith  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
4415e32d5dSMike Smith  * copy of the source code appearing in this file ("Covered Code") an
4515e32d5dSMike Smith  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
4615e32d5dSMike Smith  * base code distributed originally by Intel ("Original Intel Code") to copy,
4715e32d5dSMike Smith  * make derivatives, distribute, use and display any portion of the Covered
4815e32d5dSMike Smith  * Code in any form, with the right to sublicense such rights; and
4915e32d5dSMike Smith  *
5015e32d5dSMike Smith  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
5115e32d5dSMike Smith  * license (with the right to sublicense), under only those claims of Intel
5215e32d5dSMike Smith  * patents that are infringed by the Original Intel Code, to make, use, sell,
5315e32d5dSMike Smith  * offer to sell, and import the Covered Code and derivative works thereof
5415e32d5dSMike Smith  * solely to the minimum extent necessary to exercise the above copyright
5515e32d5dSMike Smith  * license, and in no event shall the patent license extend to any additions
5615e32d5dSMike Smith  * to or modifications of the Original Intel Code.  No other license or right
5715e32d5dSMike Smith  * is granted directly or by implication, estoppel or otherwise;
5815e32d5dSMike Smith  *
5915e32d5dSMike Smith  * The above copyright and patent license is granted only if the following
6015e32d5dSMike Smith  * conditions are met:
6115e32d5dSMike Smith  *
6215e32d5dSMike Smith  * 3. Conditions
6315e32d5dSMike Smith  *
6415e32d5dSMike Smith  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
6515e32d5dSMike Smith  * Redistribution of source code of any substantial portion of the Covered
6615e32d5dSMike Smith  * Code or modification with rights to further distribute source must include
6715e32d5dSMike Smith  * the above Copyright Notice, the above License, this list of Conditions,
6815e32d5dSMike Smith  * and the following Disclaimer and Export Compliance provision.  In addition,
6915e32d5dSMike Smith  * Licensee must cause all Covered Code to which Licensee contributes to
7015e32d5dSMike Smith  * contain a file documenting the changes Licensee made to create that Covered
7115e32d5dSMike Smith  * Code and the date of any change.  Licensee must include in that file the
7215e32d5dSMike Smith  * documentation of any changes made by any predecessor Licensee.  Licensee
7315e32d5dSMike Smith  * must include a prominent statement that the modification is derived,
7415e32d5dSMike Smith  * directly or indirectly, from Original Intel Code.
7515e32d5dSMike Smith  *
7615e32d5dSMike Smith  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
7715e32d5dSMike Smith  * Redistribution of source code of any substantial portion of the Covered
7815e32d5dSMike Smith  * Code or modification without rights to further distribute source must
7915e32d5dSMike Smith  * include the following Disclaimer and Export Compliance provision in the
8015e32d5dSMike Smith  * documentation and/or other materials provided with distribution.  In
8115e32d5dSMike Smith  * addition, Licensee may not authorize further sublicense of source of any
8215e32d5dSMike Smith  * portion of the Covered Code, and must include terms to the effect that the
8315e32d5dSMike Smith  * license from Licensee to its licensee is limited to the intellectual
8415e32d5dSMike Smith  * property embodied in the software Licensee provides to its licensee, and
8515e32d5dSMike Smith  * not to intellectual property embodied in modifications its licensee may
8615e32d5dSMike Smith  * make.
8715e32d5dSMike Smith  *
8815e32d5dSMike Smith  * 3.3. Redistribution of Executable. Redistribution in executable form of any
8915e32d5dSMike Smith  * substantial portion of the Covered Code or modification must reproduce the
9015e32d5dSMike Smith  * above Copyright Notice, and the following Disclaimer and Export Compliance
9115e32d5dSMike Smith  * provision in the documentation and/or other materials provided with the
9215e32d5dSMike Smith  * distribution.
9315e32d5dSMike Smith  *
9415e32d5dSMike Smith  * 3.4. Intel retains all right, title, and interest in and to the Original
9515e32d5dSMike Smith  * Intel Code.
9615e32d5dSMike Smith  *
9715e32d5dSMike Smith  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
9815e32d5dSMike Smith  * Intel shall be used in advertising or otherwise to promote the sale, use or
9915e32d5dSMike Smith  * other dealings in products derived from or relating to the Covered Code
10015e32d5dSMike Smith  * without prior written authorization from Intel.
10115e32d5dSMike Smith  *
10215e32d5dSMike Smith  * 4. Disclaimer and Export Compliance
10315e32d5dSMike Smith  *
10415e32d5dSMike Smith  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
10515e32d5dSMike Smith  * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
10615e32d5dSMike Smith  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
10715e32d5dSMike Smith  * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
10815e32d5dSMike Smith  * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
10915e32d5dSMike Smith  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
11015e32d5dSMike Smith  * PARTICULAR PURPOSE.
11115e32d5dSMike Smith  *
11215e32d5dSMike Smith  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
11315e32d5dSMike Smith  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
11415e32d5dSMike Smith  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
11515e32d5dSMike Smith  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
11615e32d5dSMike Smith  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
11715e32d5dSMike Smith  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
11815e32d5dSMike Smith  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
11915e32d5dSMike Smith  * LIMITED REMEDY.
12015e32d5dSMike Smith  *
12115e32d5dSMike Smith  * 4.3. Licensee shall not export, either directly or indirectly, any of this
12215e32d5dSMike Smith  * software or system incorporating such software without first obtaining any
12315e32d5dSMike Smith  * required license or other approval from the U. S. Department of Commerce or
12415e32d5dSMike Smith  * any other agency or department of the United States Government.  In the
12515e32d5dSMike Smith  * event Licensee exports any such software from the United States or
12615e32d5dSMike Smith  * re-exports any such software from a foreign destination, Licensee shall
12715e32d5dSMike Smith  * ensure that the distribution and export/re-export of the software is in
12815e32d5dSMike Smith  * compliance with all laws, regulations, orders, or other restrictions of the
12915e32d5dSMike Smith  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
13015e32d5dSMike Smith  * any of its subsidiaries will export/re-export any technical data, process,
13115e32d5dSMike Smith  * software, or service, directly or indirectly, to any country for which the
13215e32d5dSMike Smith  * United States government or any agency thereof requires an export license,
13315e32d5dSMike Smith  * other governmental approval, or letter of assurance, without first obtaining
13415e32d5dSMike Smith  * such license, approval or letter.
13515e32d5dSMike Smith  *
13615e32d5dSMike Smith  *****************************************************************************/
13715e32d5dSMike Smith 
138aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
139aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
140aad970f1SDavid E. O'Brien 
14115e32d5dSMike Smith #include "opt_acpi.h"
14215e32d5dSMike Smith #include <sys/param.h>
14315e32d5dSMike Smith #include <sys/kernel.h>
14415e32d5dSMike Smith #include <sys/bus.h>
1450025fb0fSNate Lawson #include <sys/malloc.h>
146b0eefa38SNate Lawson #include <sys/module.h>
147b0eefa38SNate Lawson #include <sys/sx.h>
14815e32d5dSMike Smith 
14915e32d5dSMike Smith #include <machine/bus.h>
15015e32d5dSMike Smith #include <machine/resource.h>
15115e32d5dSMike Smith #include <sys/rman.h>
15215e32d5dSMike Smith 
1532a191126SDavid E. O'Brien #include <contrib/dev/acpica/acpi.h>
15415e32d5dSMike Smith #include <dev/acpica/acpivar.h>
15515e32d5dSMike Smith 
156f4b7de15SNate Lawson /* Hooks for the ACPI CA debugging infrastructure */
157a9cf0dffSMike Smith #define _COMPONENT	ACPI_EC
158dbd0058aSMike Smith ACPI_MODULE_NAME("EC")
1590ae55423SMike Smith 
160da3b867eSMike Smith /*
161da3b867eSMike Smith  * EC_COMMAND:
162da3b867eSMike Smith  * -----------
163da3b867eSMike Smith  */
164da3b867eSMike Smith typedef UINT8				EC_COMMAND;
165da3b867eSMike Smith 
166da3b867eSMike Smith #define EC_COMMAND_UNKNOWN		((EC_COMMAND) 0x00)
167da3b867eSMike Smith #define EC_COMMAND_READ			((EC_COMMAND) 0x80)
168da3b867eSMike Smith #define EC_COMMAND_WRITE		((EC_COMMAND) 0x81)
169da3b867eSMike Smith #define EC_COMMAND_BURST_ENABLE		((EC_COMMAND) 0x82)
170da3b867eSMike Smith #define EC_COMMAND_BURST_DISABLE	((EC_COMMAND) 0x83)
171da3b867eSMike Smith #define EC_COMMAND_QUERY		((EC_COMMAND) 0x84)
172da3b867eSMike Smith 
173da3b867eSMike Smith /*
174da3b867eSMike Smith  * EC_STATUS:
175da3b867eSMike Smith  * ----------
176da3b867eSMike Smith  * The encoding of the EC status register is illustrated below.
177da3b867eSMike Smith  * Note that a set bit (1) indicates the property is TRUE
178da3b867eSMike Smith  * (e.g. if bit 0 is set then the output buffer is full).
179da3b867eSMike Smith  * +-+-+-+-+-+-+-+-+
180da3b867eSMike Smith  * |7|6|5|4|3|2|1|0|
181da3b867eSMike Smith  * +-+-+-+-+-+-+-+-+
182da3b867eSMike Smith  *  | | | | | | | |
183da3b867eSMike Smith  *  | | | | | | | +- Output Buffer Full?
184da3b867eSMike Smith  *  | | | | | | +--- Input Buffer Full?
185da3b867eSMike Smith  *  | | | | | +----- <reserved>
186da3b867eSMike Smith  *  | | | | +------- Data Register is Command Byte?
187da3b867eSMike Smith  *  | | | +--------- Burst Mode Enabled?
188da3b867eSMike Smith  *  | | +----------- SCI Event?
189da3b867eSMike Smith  *  | +------------- SMI Event?
190ef2374f7SNate Lawson  *  +--------------- <reserved>
191da3b867eSMike Smith  *
192da3b867eSMike Smith  */
193da3b867eSMike Smith typedef UINT8				EC_STATUS;
194da3b867eSMike Smith 
195da3b867eSMike Smith #define EC_FLAG_OUTPUT_BUFFER		((EC_STATUS) 0x01)
196da3b867eSMike Smith #define EC_FLAG_INPUT_BUFFER		((EC_STATUS) 0x02)
197ef2374f7SNate Lawson #define EC_FLAG_DATA_IS_CMD		((EC_STATUS) 0x08)
198da3b867eSMike Smith #define EC_FLAG_BURST_MODE		((EC_STATUS) 0x10)
199da3b867eSMike Smith 
200da3b867eSMike Smith /*
201da3b867eSMike Smith  * EC_EVENT:
202da3b867eSMike Smith  * ---------
203da3b867eSMike Smith  */
204da3b867eSMike Smith typedef UINT8				EC_EVENT;
205da3b867eSMike Smith 
206da3b867eSMike Smith #define EC_EVENT_UNKNOWN		((EC_EVENT) 0x00)
207da3b867eSMike Smith #define EC_EVENT_OUTPUT_BUFFER_FULL	((EC_EVENT) 0x01)
208da3b867eSMike Smith #define EC_EVENT_INPUT_BUFFER_EMPTY	((EC_EVENT) 0x02)
209da3b867eSMike Smith #define EC_EVENT_SCI			((EC_EVENT) 0x20)
210ef2374f7SNate Lawson #define EC_EVENT_SMI			((EC_EVENT) 0x40)
211ef2374f7SNate Lawson 
212ef2374f7SNate Lawson /* Data byte returned after burst enable indicating it was successful. */
213ef2374f7SNate Lawson #define EC_BURST_ACK			0x90
214da3b867eSMike Smith 
215da3b867eSMike Smith /*
216da3b867eSMike Smith  * Register access primitives
217da3b867eSMike Smith  */
218da3b867eSMike Smith #define EC_GET_DATA(sc)							\
219da3b867eSMike Smith 	bus_space_read_1((sc)->ec_data_tag, (sc)->ec_data_handle, 0)
220da3b867eSMike Smith 
221da3b867eSMike Smith #define EC_SET_DATA(sc, v)						\
222da3b867eSMike Smith 	bus_space_write_1((sc)->ec_data_tag, (sc)->ec_data_handle, 0, (v))
223da3b867eSMike Smith 
224da3b867eSMike Smith #define EC_GET_CSR(sc)							\
225da3b867eSMike Smith 	bus_space_read_1((sc)->ec_csr_tag, (sc)->ec_csr_handle, 0)
226da3b867eSMike Smith 
227da3b867eSMike Smith #define EC_SET_CSR(sc, v)						\
228da3b867eSMike Smith 	bus_space_write_1((sc)->ec_csr_tag, (sc)->ec_csr_handle, 0, (v))
229da3b867eSMike Smith 
2300025fb0fSNate Lawson /* Additional params to pass from the probe routine */
2310025fb0fSNate Lawson struct acpi_ec_params {
2320025fb0fSNate Lawson     int		glk;
2330025fb0fSNate Lawson     int		gpe_bit;
2340025fb0fSNate Lawson     ACPI_HANDLE	gpe_handle;
2350025fb0fSNate Lawson     int		uid;
2360025fb0fSNate Lawson };
2370025fb0fSNate Lawson 
238f8335e3aSNate Lawson /* Indicate that this device has already been probed via ECDT. */
2390025fb0fSNate Lawson #define DEV_ECDT(x)		(acpi_get_magic(x) == (int)&acpi_ec_devclass)
240f8335e3aSNate Lawson 
241da3b867eSMike Smith /*
242da3b867eSMike Smith  * Driver softc.
243da3b867eSMike Smith  */
24415e32d5dSMike Smith struct acpi_ec_softc {
24515e32d5dSMike Smith     device_t		ec_dev;
24615e32d5dSMike Smith     ACPI_HANDLE		ec_handle;
2470025fb0fSNate Lawson     int			ec_uid;
2480025fb0fSNate Lawson     ACPI_HANDLE		ec_gpehandle;
2493a371f32SNate Lawson     UINT8		ec_gpebit;
2503a371f32SNate Lawson     UINT8		ec_csrvalue;
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 
262ef2374f7SNate Lawson     struct mtx		ec_mtx;
2631f04e8f5SNate Lawson     int			ec_glk;
2641f04e8f5SNate Lawson     int			ec_glkhandle;
265ef2374f7SNate Lawson     int			ec_burstactive;
266ef2374f7SNate Lawson     int			ec_sci_pend;
26715e32d5dSMike Smith };
26815e32d5dSMike Smith 
2691f04e8f5SNate Lawson /*
270f4b7de15SNate Lawson  * XXX njl
2711f04e8f5SNate Lawson  * I couldn't find it in the spec but other implementations also use a
2721f04e8f5SNate Lawson  * value of 1 ms for the time to acquire global lock.
2731f04e8f5SNate Lawson  */
2741f04e8f5SNate Lawson #define EC_LOCK_TIMEOUT	1000
2754690674eSMitsuru IWASAKI 
276ef2374f7SNate Lawson /* Default delay in microseconds between each run of the status polling loop. */
277b0eefa38SNate Lawson #define EC_POLL_DELAY	10
2781f04e8f5SNate Lawson 
279ef2374f7SNate Lawson /* Default time in microseconds spent polling before sleep waiting. */
280ef2374f7SNate Lawson #define EC_POLL_TIME	500
281ef2374f7SNate Lawson 
282ef2374f7SNate Lawson /* Total time in ms spent waiting for a response from EC. */
283ef2374f7SNate Lawson #define EC_TIMEOUT	500
28415e32d5dSMike Smith 
285ff40920eSNate Lawson #define EVENT_READY(event, status)			\
286ff40920eSNate Lawson 	(((event) == EC_EVENT_OUTPUT_BUFFER_FULL &&	\
287ff40920eSNate Lawson 	 ((status) & EC_FLAG_OUTPUT_BUFFER) != 0) ||	\
288ff40920eSNate Lawson 	 ((event) == EC_EVENT_INPUT_BUFFER_EMPTY && 	\
289ff40920eSNate Lawson 	 ((status) & EC_FLAG_INPUT_BUFFER) == 0))
290ff40920eSNate Lawson 
291f4b7de15SNate Lawson ACPI_SERIAL_DECL(ec, "ACPI embedded controller");
292f4b7de15SNate Lawson 
293ef2374f7SNate Lawson SYSCTL_DECL(_debug_acpi);
294ef2374f7SNate Lawson SYSCTL_NODE(_debug_acpi, OID_AUTO, ec, CTLFLAG_RD, NULL, "EC debugging");
295ef2374f7SNate Lawson 
296675e5627SNate Lawson static int	ec_burst_mode;
297ef2374f7SNate Lawson TUNABLE_INT("debug.acpi.ec.burst", &ec_burst_mode);
298675e5627SNate Lawson SYSCTL_INT(_debug_acpi_ec, OID_AUTO, burst, CTLFLAG_RW, &ec_burst_mode, 0,
299ef2374f7SNate Lawson     "Enable use of burst mode (faster for nearly all systems)");
300ef2374f7SNate Lawson static int	ec_poll_time = EC_POLL_TIME;
301ef2374f7SNate Lawson TUNABLE_INT("debug.acpi.ec.poll_time", &ec_poll_time);
302ef2374f7SNate Lawson SYSCTL_INT(_debug_acpi_ec, OID_AUTO, poll_time, CTLFLAG_RW, &ec_poll_time,
303ef2374f7SNate Lawson     EC_POLL_TIME, "Time spent polling vs. sleeping (CPU intensive)");
304ef2374f7SNate Lawson static int	ec_timeout = EC_TIMEOUT;
305ef2374f7SNate Lawson TUNABLE_INT("debug.acpi.ec.timeout", &ec_timeout);
306ef2374f7SNate Lawson SYSCTL_INT(_debug_acpi_ec, OID_AUTO, timeout, CTLFLAG_RW, &ec_timeout,
307ef2374f7SNate Lawson     EC_TIMEOUT, "Total time spent waiting for a response (poll+sleep)");
308ef2374f7SNate Lawson 
30915e32d5dSMike Smith static __inline ACPI_STATUS
310ef2374f7SNate Lawson EcLock(struct acpi_ec_softc *sc, int serialize)
31115e32d5dSMike Smith {
312f4b7de15SNate Lawson     ACPI_STATUS	status;
31315e32d5dSMike Smith 
314ef2374f7SNate Lawson     /*
315ef2374f7SNate Lawson      * If caller is executing a series of commands, acquire the exclusive lock
316ef2374f7SNate Lawson      * to serialize with other users.
317ef2374f7SNate Lawson      * To sync with bottom-half interrupt handler, always acquire the mutex.
318ef2374f7SNate Lawson      */
319f4b7de15SNate Lawson     status = AE_OK;
320ef2374f7SNate Lawson     if (serialize)
321f4b7de15SNate Lawson 	ACPI_SERIAL_BEGIN(ec);
322ef2374f7SNate Lawson     mtx_lock(&sc->ec_mtx);
3231f04e8f5SNate Lawson 
3241f04e8f5SNate Lawson     /* If _GLK is non-zero, also acquire the global lock. */
3251f04e8f5SNate Lawson     if (sc->ec_glk) {
3261f04e8f5SNate Lawson 	status = AcpiAcquireGlobalLock(EC_LOCK_TIMEOUT, &sc->ec_glkhandle);
327ef2374f7SNate Lawson 	if (ACPI_FAILURE(status)) {
328ef2374f7SNate Lawson 	    mtx_unlock(&sc->ec_mtx);
329ef2374f7SNate Lawson 	    if (serialize)
330f4b7de15SNate Lawson 		ACPI_SERIAL_END(ec);
3311f04e8f5SNate Lawson 	}
332ef2374f7SNate Lawson     }
333c573e654SMitsuru IWASAKI 
33415e32d5dSMike Smith     return (status);
33515e32d5dSMike Smith }
33615e32d5dSMike Smith 
33715e32d5dSMike Smith static __inline void
33815e32d5dSMike Smith EcUnlock(struct acpi_ec_softc *sc)
33915e32d5dSMike Smith {
3401f04e8f5SNate Lawson     if (sc->ec_glk)
3411f04e8f5SNate Lawson 	AcpiReleaseGlobalLock(sc->ec_glkhandle);
342ef2374f7SNate Lawson     mtx_unlock(&sc->ec_mtx);
343ef2374f7SNate Lawson     if (sx_xlocked(&ec_sxlock))
344f4b7de15SNate Lawson 	ACPI_SERIAL_END(ec);
34515e32d5dSMike Smith }
34615e32d5dSMike Smith 
347f3fc4f8bSNate Lawson static uint32_t		EcGpeHandler(void *Context);
34815e32d5dSMike Smith static ACPI_STATUS	EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function,
34915e32d5dSMike Smith 				void *Context, void **return_Context);
350464c662eSNate Lawson static ACPI_STATUS	EcSpaceHandler(UINT32 Function,
351464c662eSNate Lawson 				ACPI_PHYSICAL_ADDRESS Address,
352464c662eSNate Lawson 				UINT32 width, ACPI_INTEGER *Value,
35315e32d5dSMike Smith 				void *Context, void *RegionContext);
35415e32d5dSMike Smith static ACPI_STATUS	EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event);
3551f04e8f5SNate Lawson static ACPI_STATUS	EcCommand(struct acpi_ec_softc *sc, EC_COMMAND cmd);
356464c662eSNate Lawson static ACPI_STATUS	EcRead(struct acpi_ec_softc *sc, UINT8 Address,
357464c662eSNate Lawson 				UINT8 *Data);
358464c662eSNate Lawson static ACPI_STATUS	EcWrite(struct acpi_ec_softc *sc, UINT8 Address,
359464c662eSNate Lawson 				UINT8 *Data);
36015e32d5dSMike Smith static int		acpi_ec_probe(device_t dev);
36115e32d5dSMike Smith static int		acpi_ec_attach(device_t dev);
362340a7f6aSNate Lawson static int		acpi_ec_shutdown(device_t dev);
363e33bea8dSNate Lawson static int		acpi_ec_read_method(device_t dev, u_int addr,
364e33bea8dSNate Lawson 				ACPI_INTEGER *val, int width);
365e33bea8dSNate Lawson static int		acpi_ec_write_method(device_t dev, u_int addr,
366e33bea8dSNate Lawson 				ACPI_INTEGER val, int width);
36715e32d5dSMike Smith 
36815e32d5dSMike Smith static device_method_t acpi_ec_methods[] = {
36915e32d5dSMike Smith     /* Device interface */
37015e32d5dSMike Smith     DEVMETHOD(device_probe,	acpi_ec_probe),
37115e32d5dSMike Smith     DEVMETHOD(device_attach,	acpi_ec_attach),
372340a7f6aSNate Lawson     DEVMETHOD(device_shutdown,	acpi_ec_shutdown),
37315e32d5dSMike Smith 
374e33bea8dSNate Lawson     /* Embedded controller interface */
375e33bea8dSNate Lawson     DEVMETHOD(acpi_ec_read,	acpi_ec_read_method),
376e33bea8dSNate Lawson     DEVMETHOD(acpi_ec_write,	acpi_ec_write_method),
377e33bea8dSNate Lawson 
37815e32d5dSMike Smith     {0, 0}
37915e32d5dSMike Smith };
38015e32d5dSMike Smith 
38115e32d5dSMike Smith static driver_t acpi_ec_driver = {
38215e32d5dSMike Smith     "acpi_ec",
38315e32d5dSMike Smith     acpi_ec_methods,
38415e32d5dSMike Smith     sizeof(struct acpi_ec_softc),
38515e32d5dSMike Smith };
38615e32d5dSMike Smith 
3873273b005SMike Smith static devclass_t acpi_ec_devclass;
38815e32d5dSMike Smith DRIVER_MODULE(acpi_ec, acpi, acpi_ec_driver, acpi_ec_devclass, 0, 0);
38964278df5SNate Lawson MODULE_DEPEND(acpi_ec, acpi, 1, 1, 1);
39015e32d5dSMike Smith 
39115e32d5dSMike Smith /*
392f8335e3aSNate Lawson  * Look for an ECDT and if we find one, set up default GPE and
393f8335e3aSNate Lawson  * space handlers to catch attempts to access EC space before
39415e32d5dSMike Smith  * we have a real driver instance in place.
395f8335e3aSNate Lawson  * TODO: if people report invalid ECDTs, add a tunable to disable them.
39615e32d5dSMike Smith  */
397f8335e3aSNate Lawson void
398f8335e3aSNate Lawson acpi_ec_ecdt_probe(device_t parent)
39915e32d5dSMike Smith {
400f8335e3aSNate Lawson     ACPI_TABLE_ECDT *ecdt;
401f8335e3aSNate Lawson     ACPI_STATUS	     status;
402f8335e3aSNate Lawson     device_t	     child;
403f8335e3aSNate Lawson     ACPI_HANDLE	     h;
4040025fb0fSNate Lawson     struct acpi_ec_params *params;
405f8335e3aSNate Lawson 
406b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
40715e32d5dSMike Smith 
408f8335e3aSNate Lawson     /* Find and validate the ECDT. */
4092be4e471SJung-uk Kim     status = AcpiGetTable(ACPI_SIG_ECDT, 1, (ACPI_TABLE_HEADER **)&ecdt);
410f8335e3aSNate Lawson     if (ACPI_FAILURE(status) ||
4112be4e471SJung-uk Kim 	ecdt->Control.BitWidth != 8 ||
4122be4e471SJung-uk Kim 	ecdt->Data.BitWidth != 8) {
413f8335e3aSNate Lawson 	return;
41415e32d5dSMike Smith     }
41515e32d5dSMike Smith 
416f8335e3aSNate Lawson     /* Create the child device with the given unit number. */
4172be4e471SJung-uk Kim     child = BUS_ADD_CHILD(parent, 0, "acpi_ec", ecdt->Uid);
418f8335e3aSNate Lawson     if (child == NULL) {
4190025fb0fSNate Lawson 	printf("%s: can't add child\n", __func__);
420f8335e3aSNate Lawson 	return;
421f8335e3aSNate Lawson     }
422f8335e3aSNate Lawson 
423f8335e3aSNate Lawson     /* Find and save the ACPI handle for this device. */
4242be4e471SJung-uk Kim     status = AcpiGetHandle(NULL, ecdt->Id, &h);
425f8335e3aSNate Lawson     if (ACPI_FAILURE(status)) {
426f8335e3aSNate Lawson 	device_delete_child(parent, child);
4270025fb0fSNate Lawson 	printf("%s: can't get handle\n", __func__);
428f8335e3aSNate Lawson 	return;
429f8335e3aSNate Lawson     }
430f8335e3aSNate Lawson     acpi_set_handle(child, h);
431f8335e3aSNate Lawson 
432f8335e3aSNate Lawson     /* Set the data and CSR register addresses. */
4332be4e471SJung-uk Kim     bus_set_resource(child, SYS_RES_IOPORT, 0, ecdt->Data.Address,
434f8335e3aSNate Lawson 	/*count*/1);
4352be4e471SJung-uk Kim     bus_set_resource(child, SYS_RES_IOPORT, 1, ecdt->Control.Address,
436f8335e3aSNate Lawson 	/*count*/1);
437f8335e3aSNate Lawson 
43815e32d5dSMike Smith     /*
439f8335e3aSNate Lawson      * Store values for the probe/attach routines to use.  Store the
440c868ac7dSNate Lawson      * ECDT GPE bit and set the global lock flag according to _GLK.
4410025fb0fSNate Lawson      * Note that it is not perfectly correct to be evaluating a method
4420025fb0fSNate Lawson      * before initializing devices, but in practice this function
4430025fb0fSNate Lawson      * should be safe to call at this point.
44415e32d5dSMike Smith      */
4450025fb0fSNate Lawson     params = malloc(sizeof(struct acpi_ec_params), M_TEMP, M_WAITOK | M_ZERO);
4460025fb0fSNate Lawson     params->gpe_handle = NULL;
4472be4e471SJung-uk Kim     params->gpe_bit = ecdt->Gpe;
4482be4e471SJung-uk Kim     params->uid = ecdt->Uid;
4490025fb0fSNate Lawson     acpi_GetInteger(h, "_GLK", &params->glk);
4500025fb0fSNate Lawson     acpi_set_private(child, params);
4510025fb0fSNate Lawson     acpi_set_magic(child, (int)&acpi_ec_devclass);
452f8335e3aSNate Lawson 
453f8335e3aSNate Lawson     /* Finish the attach process. */
454f8335e3aSNate Lawson     if (device_probe_and_attach(child) != 0)
455f8335e3aSNate Lawson 	device_delete_child(parent, child);
456f8335e3aSNate Lawson }
457f8335e3aSNate Lawson 
45815e32d5dSMike Smith static int
45915e32d5dSMike Smith acpi_ec_probe(device_t dev)
46015e32d5dSMike Smith {
4610025fb0fSNate Lawson     ACPI_BUFFER buf;
462f8335e3aSNate Lawson     ACPI_HANDLE h;
4630025fb0fSNate Lawson     ACPI_OBJECT *obj;
464f8335e3aSNate Lawson     ACPI_STATUS status;
465f8335e3aSNate Lawson     device_t	peer;
466f8335e3aSNate Lawson     char	desc[64];
4670025fb0fSNate Lawson     int		ret;
4680025fb0fSNate Lawson     struct acpi_ec_params *params;
4695fcc8a58SNate Lawson     static char *ec_ids[] = { "PNP0C09", NULL };
4700ae55423SMike Smith 
471c72508e2SNate Lawson     /* Check that this is a device and that EC is not disabled. */
472c72508e2SNate Lawson     if (acpi_get_type(dev) != ACPI_TYPE_DEVICE || acpi_disabled("ec"))
473f8335e3aSNate Lawson 	return (ENXIO);
47415e32d5dSMike Smith 
47515e32d5dSMike Smith     /*
476f8335e3aSNate Lawson      * If probed via ECDT, set description and continue.  Otherwise,
477f8335e3aSNate Lawson      * we can access the namespace and make sure this is not a
478f8335e3aSNate Lawson      * duplicate probe.
47915e32d5dSMike Smith      */
4800025fb0fSNate Lawson     ret = ENXIO;
4810025fb0fSNate Lawson     params = NULL;
4820025fb0fSNate Lawson     buf.Pointer = NULL;
4830025fb0fSNate Lawson     buf.Length = ACPI_ALLOCATE_BUFFER;
4840025fb0fSNate Lawson     if (DEV_ECDT(dev)) {
4850025fb0fSNate Lawson 	params = acpi_get_private(dev);
4861f04e8f5SNate Lawson 	ret = 0;
4875fcc8a58SNate Lawson     } else if (!acpi_disabled("ec") &&
4885fcc8a58SNate Lawson 	ACPI_ID_PROBE(device_get_parent(dev), dev, ec_ids)) {
4890025fb0fSNate Lawson 	params = malloc(sizeof(struct acpi_ec_params), M_TEMP,
4900025fb0fSNate Lawson 			M_WAITOK | M_ZERO);
491f8335e3aSNate Lawson 	h = acpi_get_handle(dev);
492f8335e3aSNate Lawson 
493f8335e3aSNate Lawson 	/*
494f8335e3aSNate Lawson 	 * Read the unit ID to check for duplicate attach and the
495f8335e3aSNate Lawson 	 * global lock value to see if we should acquire it when
496f8335e3aSNate Lawson 	 * accessing the EC.
497f8335e3aSNate Lawson 	 */
4980025fb0fSNate Lawson 	status = acpi_GetInteger(h, "_UID", &params->uid);
499f8335e3aSNate Lawson 	if (ACPI_FAILURE(status))
5000025fb0fSNate Lawson 	    params->uid = 0;
5010025fb0fSNate Lawson 	status = acpi_GetInteger(h, "_GLK", &params->glk);
502f8335e3aSNate Lawson 	if (ACPI_FAILURE(status))
5030025fb0fSNate Lawson 	    params->glk = 0;
504f8335e3aSNate Lawson 
505f8335e3aSNate Lawson 	/*
506f8335e3aSNate Lawson 	 * Evaluate the _GPE method to find the GPE bit used by the EC to
5070025fb0fSNate Lawson 	 * signal status (SCI).  If it's a package, it contains a reference
5080025fb0fSNate Lawson 	 * and GPE bit, similar to _PRW.
509f8335e3aSNate Lawson 	 */
5100025fb0fSNate Lawson 	status = AcpiEvaluateObject(h, "_GPE", NULL, &buf);
511f8335e3aSNate Lawson 	if (ACPI_FAILURE(status)) {
512f8335e3aSNate Lawson 	    device_printf(dev, "can't evaluate _GPE - %s\n",
513f8335e3aSNate Lawson 			  AcpiFormatException(status));
514a6761eb3SNate Lawson 	    goto out;
515f8335e3aSNate Lawson 	}
5160025fb0fSNate Lawson 	obj = (ACPI_OBJECT *)buf.Pointer;
5170025fb0fSNate Lawson 	if (obj == NULL)
518a6761eb3SNate Lawson 	    goto out;
5190025fb0fSNate Lawson 
5200025fb0fSNate Lawson 	switch (obj->Type) {
5210025fb0fSNate Lawson 	case ACPI_TYPE_INTEGER:
5220025fb0fSNate Lawson 	    params->gpe_handle = NULL;
5230025fb0fSNate Lawson 	    params->gpe_bit = obj->Integer.Value;
5240025fb0fSNate Lawson 	    break;
5250025fb0fSNate Lawson 	case ACPI_TYPE_PACKAGE:
5260025fb0fSNate Lawson 	    if (!ACPI_PKG_VALID(obj, 2))
5270025fb0fSNate Lawson 		goto out;
5280025fb0fSNate Lawson 	    params->gpe_handle =
5290025fb0fSNate Lawson 		acpi_GetReference(NULL, &obj->Package.Elements[0]);
5300025fb0fSNate Lawson 	    if (params->gpe_handle == NULL ||
5310025fb0fSNate Lawson 		acpi_PkgInt32(obj, 1, &params->gpe_bit) != 0)
5320025fb0fSNate Lawson 		goto out;
5330025fb0fSNate Lawson 	    break;
5340025fb0fSNate Lawson 	default:
5350025fb0fSNate Lawson 	    device_printf(dev, "_GPE has invalid type %d\n", obj->Type);
5360025fb0fSNate Lawson 	    goto out;
5370025fb0fSNate Lawson 	}
538f8335e3aSNate Lawson 
539f8335e3aSNate Lawson 	/* Store the values we got from the namespace for attach. */
5400025fb0fSNate Lawson 	acpi_set_private(dev, params);
541f8335e3aSNate Lawson 
542f8335e3aSNate Lawson 	/*
543f8335e3aSNate Lawson 	 * Check for a duplicate probe.  This can happen when a probe
544c868ac7dSNate Lawson 	 * via ECDT succeeded already.  If this is a duplicate, disable
545c868ac7dSNate Lawson 	 * this device.
546f8335e3aSNate Lawson 	 */
5470025fb0fSNate Lawson 	peer = devclass_get_device(acpi_ec_devclass, params->uid);
548c868ac7dSNate Lawson 	if (peer == NULL || !device_is_alive(peer))
549f8335e3aSNate Lawson 	    ret = 0;
550c868ac7dSNate Lawson 	else
551c868ac7dSNate Lawson 	    device_disable(dev);
552c868ac7dSNate Lawson     }
553f8335e3aSNate Lawson 
5540025fb0fSNate Lawson out:
555c868ac7dSNate Lawson     if (ret == 0) {
556c868ac7dSNate Lawson 	snprintf(desc, sizeof(desc), "Embedded Controller: GPE %#x%s%s",
5570025fb0fSNate Lawson 		 params->gpe_bit, (params->glk) ? ", GLK" : "",
558c868ac7dSNate Lawson 		 DEV_ECDT(dev) ? ", ECDT" : "");
559c868ac7dSNate Lawson 	device_set_desc_copy(dev, desc);
56015e32d5dSMike Smith     }
5611f04e8f5SNate Lawson 
5620025fb0fSNate Lawson     if (ret > 0 && params)
5630025fb0fSNate Lawson 	free(params, M_TEMP);
5640025fb0fSNate Lawson     if (buf.Pointer)
5650025fb0fSNate Lawson 	AcpiOsFree(buf.Pointer);
5661f04e8f5SNate Lawson     return (ret);
56715e32d5dSMike Smith }
56815e32d5dSMike Smith 
56915e32d5dSMike Smith static int
57015e32d5dSMike Smith acpi_ec_attach(device_t dev)
57115e32d5dSMike Smith {
57215e32d5dSMike Smith     struct acpi_ec_softc	*sc;
5730025fb0fSNate Lawson     struct acpi_ec_params	*params;
57415e32d5dSMike Smith     ACPI_STATUS			Status;
575dbd0058aSMike Smith 
576b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
5770ae55423SMike Smith 
5781f04e8f5SNate Lawson     /* Fetch/initialize softc (assumes softc is pre-zeroed). */
57915e32d5dSMike Smith     sc = device_get_softc(dev);
5800025fb0fSNate Lawson     params = acpi_get_private(dev);
58115e32d5dSMike Smith     sc->ec_dev = dev;
58215e32d5dSMike Smith     sc->ec_handle = acpi_get_handle(dev);
583ef2374f7SNate Lawson     mtx_init(&sc->ec_mtx, "ACPI EC lock", NULL, MTX_DEF);
58415e32d5dSMike Smith 
585f8335e3aSNate Lawson     /* Retrieve previously probed values via device ivars. */
5860025fb0fSNate Lawson     sc->ec_glk = params->glk;
5870025fb0fSNate Lawson     sc->ec_gpebit = params->gpe_bit;
5880025fb0fSNate Lawson     sc->ec_gpehandle = params->gpe_handle;
5890025fb0fSNate Lawson     sc->ec_uid = params->uid;
5900025fb0fSNate Lawson     free(params, M_TEMP);
591f8335e3aSNate Lawson 
5921f04e8f5SNate Lawson     /* Attach bus resources for data and command/status ports. */
59315e32d5dSMike Smith     sc->ec_data_rid = 0;
5945f96beb9SNate Lawson     sc->ec_data_res = bus_alloc_resource_any(sc->ec_dev, SYS_RES_IOPORT,
5955f96beb9SNate Lawson 			&sc->ec_data_rid, RF_ACTIVE);
596464c662eSNate Lawson     if (sc->ec_data_res == NULL) {
59715e32d5dSMike Smith 	device_printf(dev, "can't allocate data port\n");
598f3fc4f8bSNate Lawson 	goto error;
59915e32d5dSMike Smith     }
60015e32d5dSMike Smith     sc->ec_data_tag = rman_get_bustag(sc->ec_data_res);
60115e32d5dSMike Smith     sc->ec_data_handle = rman_get_bushandle(sc->ec_data_res);
60215e32d5dSMike Smith 
60315e32d5dSMike Smith     sc->ec_csr_rid = 1;
6045f96beb9SNate Lawson     sc->ec_csr_res = bus_alloc_resource_any(sc->ec_dev, SYS_RES_IOPORT,
6055f96beb9SNate Lawson 			&sc->ec_csr_rid, RF_ACTIVE);
606464c662eSNate Lawson     if (sc->ec_csr_res == NULL) {
60715e32d5dSMike Smith 	device_printf(dev, "can't allocate command/status port\n");
608f3fc4f8bSNate Lawson 	goto error;
60915e32d5dSMike Smith     }
61015e32d5dSMike Smith     sc->ec_csr_tag = rman_get_bustag(sc->ec_csr_res);
61115e32d5dSMike Smith     sc->ec_csr_handle = rman_get_bushandle(sc->ec_csr_res);
61215e32d5dSMike Smith 
61315e32d5dSMike Smith     /*
6141f04e8f5SNate Lawson      * Install a handler for this EC's GPE bit.  We want edge-triggered
6151f04e8f5SNate Lawson      * behavior.
61615e32d5dSMike Smith      */
6171f04e8f5SNate Lawson     ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "attaching GPE handler\n"));
6180025fb0fSNate Lawson     Status = AcpiInstallGpeHandler(sc->ec_gpehandle, sc->ec_gpebit,
619b7d13479SNate Lawson 		ACPI_GPE_EDGE_TRIGGERED, &EcGpeHandler, sc);
620464c662eSNate Lawson     if (ACPI_FAILURE(Status)) {
62176d1dff4SMike Smith 	device_printf(dev, "can't install GPE handler for %s - %s\n",
622bfae45aaSMike Smith 		      acpi_name(sc->ec_handle), AcpiFormatException(Status));
623f3fc4f8bSNate Lawson 	goto error;
62415e32d5dSMike Smith     }
62515e32d5dSMike Smith 
62615e32d5dSMike Smith     /*
62715e32d5dSMike Smith      * Install address space handler
62815e32d5dSMike Smith      */
6294c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "attaching address space handler\n"));
630464c662eSNate Lawson     Status = AcpiInstallAddressSpaceHandler(sc->ec_handle, ACPI_ADR_SPACE_EC,
6311f04e8f5SNate Lawson 		&EcSpaceHandler, &EcSpaceSetup, sc);
632464c662eSNate Lawson     if (ACPI_FAILURE(Status)) {
63376d1dff4SMike Smith 	device_printf(dev, "can't install address space handler for %s - %s\n",
634bfae45aaSMike Smith 		      acpi_name(sc->ec_handle), AcpiFormatException(Status));
635f3fc4f8bSNate Lawson 	goto error;
636f3fc4f8bSNate Lawson     }
637f3fc4f8bSNate Lawson 
638f3fc4f8bSNate Lawson     /* Enable runtime GPEs for the handler. */
639f3fc4f8bSNate Lawson     Status = AcpiSetGpeType(sc->ec_gpehandle, sc->ec_gpebit,
640f3fc4f8bSNate Lawson 			    ACPI_GPE_TYPE_RUNTIME);
641f3fc4f8bSNate Lawson     if (ACPI_FAILURE(Status)) {
642f3fc4f8bSNate Lawson 	device_printf(dev, "AcpiSetGpeType failed: %s\n",
643f3fc4f8bSNate Lawson 		      AcpiFormatException(Status));
644f3fc4f8bSNate Lawson 	goto error;
645f3fc4f8bSNate Lawson     }
646f3fc4f8bSNate Lawson     Status = AcpiEnableGpe(sc->ec_gpehandle, sc->ec_gpebit, ACPI_NOT_ISR);
647f3fc4f8bSNate Lawson     if (ACPI_FAILURE(Status)) {
648f3fc4f8bSNate Lawson 	device_printf(dev, "AcpiEnableGpe failed: %s\n",
649f3fc4f8bSNate Lawson 		      AcpiFormatException(Status));
650f3fc4f8bSNate Lawson 	goto error;
65115e32d5dSMike Smith     }
6521f04e8f5SNate Lawson 
6531f04e8f5SNate Lawson     ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "acpi_ec_attach complete\n"));
6541f04e8f5SNate Lawson     return (0);
655464c662eSNate Lawson 
656f3fc4f8bSNate Lawson error:
657f3fc4f8bSNate Lawson     AcpiRemoveGpeHandler(sc->ec_gpehandle, sc->ec_gpebit, &EcGpeHandler);
658f3fc4f8bSNate Lawson     AcpiRemoveAddressSpaceHandler(sc->ec_handle, ACPI_ADR_SPACE_EC,
659f3fc4f8bSNate Lawson 	EcSpaceHandler);
660f8372adeSTakanori Watanabe     if (sc->ec_csr_res)
661f8372adeSTakanori Watanabe 	bus_release_resource(sc->ec_dev, SYS_RES_IOPORT, sc->ec_csr_rid,
662f8372adeSTakanori Watanabe 			     sc->ec_csr_res);
663f8372adeSTakanori Watanabe     if (sc->ec_data_res)
664f8372adeSTakanori Watanabe 	bus_release_resource(sc->ec_dev, SYS_RES_IOPORT, sc->ec_data_rid,
665f8372adeSTakanori Watanabe 			     sc->ec_data_res);
666ef2374f7SNate Lawson     mtx_destroy(&sc->ec_mtx);
667f3fc4f8bSNate Lawson     return (ENXIO);
6681f04e8f5SNate Lawson }
6691f04e8f5SNate Lawson 
670340a7f6aSNate Lawson static int
671340a7f6aSNate Lawson acpi_ec_shutdown(device_t dev)
672340a7f6aSNate Lawson {
673340a7f6aSNate Lawson     struct acpi_ec_softc	*sc;
674340a7f6aSNate Lawson 
675340a7f6aSNate Lawson     /* Disable the GPE so we don't get EC events during shutdown. */
676340a7f6aSNate Lawson     sc = device_get_softc(dev);
677340a7f6aSNate Lawson     AcpiDisableGpe(sc->ec_gpehandle, sc->ec_gpebit, ACPI_NOT_ISR);
678340a7f6aSNate Lawson     return (0);
679340a7f6aSNate Lawson }
680340a7f6aSNate Lawson 
681e33bea8dSNate Lawson /* Methods to allow other devices (e.g., smbat) to read/write EC space. */
682e33bea8dSNate Lawson static int
683e33bea8dSNate Lawson acpi_ec_read_method(device_t dev, u_int addr, ACPI_INTEGER *val, int width)
684e33bea8dSNate Lawson {
685e33bea8dSNate Lawson     struct acpi_ec_softc *sc;
686e33bea8dSNate Lawson     ACPI_STATUS status;
687e33bea8dSNate Lawson 
688e33bea8dSNate Lawson     sc = device_get_softc(dev);
689e33bea8dSNate Lawson     status = EcSpaceHandler(ACPI_READ, addr, width * 8, val, sc, NULL);
690e33bea8dSNate Lawson     if (ACPI_FAILURE(status))
691e33bea8dSNate Lawson 	return (ENXIO);
692e33bea8dSNate Lawson     return (0);
693e33bea8dSNate Lawson }
694e33bea8dSNate Lawson 
695e33bea8dSNate Lawson static int
696e33bea8dSNate Lawson acpi_ec_write_method(device_t dev, u_int addr, ACPI_INTEGER val, int width)
697e33bea8dSNate Lawson {
698e33bea8dSNate Lawson     struct acpi_ec_softc *sc;
699e33bea8dSNate Lawson     ACPI_STATUS status;
700e33bea8dSNate Lawson 
701e33bea8dSNate Lawson     sc = device_get_softc(dev);
702e33bea8dSNate Lawson     status = EcSpaceHandler(ACPI_WRITE, addr, width * 8, &val, sc, NULL);
703e33bea8dSNate Lawson     if (ACPI_FAILURE(status))
704e33bea8dSNate Lawson 	return (ENXIO);
705e33bea8dSNate Lawson     return (0);
706e33bea8dSNate Lawson }
707e33bea8dSNate Lawson 
70815e32d5dSMike Smith static void
709c07572e7STakanori Watanabe EcGpeQueryHandler(void *Context)
71015e32d5dSMike Smith {
71115e32d5dSMike Smith     struct acpi_ec_softc	*sc = (struct acpi_ec_softc *)Context;
71215e32d5dSMike Smith     UINT8			Data;
71315e32d5dSMike Smith     ACPI_STATUS			Status;
71415e32d5dSMike Smith     char			qxx[5];
71515e32d5dSMike Smith 
716b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
7171f04e8f5SNate Lawson     KASSERT(Context != NULL, ("EcGpeQueryHandler called with NULL"));
71815e32d5dSMike Smith 
719ef2374f7SNate Lawson     /* Serialize user access with EcSpaceHandler(). */
720ef2374f7SNate Lawson     Status = EcLock(sc, TRUE);
7213a371f32SNate Lawson     if (ACPI_FAILURE(Status)) {
7223a371f32SNate Lawson 	ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
7233a371f32SNate Lawson 		    "GpeQuery lock error: %s\n", AcpiFormatException(Status));
7243a371f32SNate Lawson 	return;
7253a371f32SNate Lawson     }
7263a371f32SNate Lawson 
72715e32d5dSMike Smith     /*
7283a371f32SNate Lawson      * Send a query command to the EC to find out which _Qxx call it
7293a371f32SNate Lawson      * wants to make.  This command clears the SCI bit and also the
7303a371f32SNate Lawson      * interrupt source since we are edge-triggered.
7313a371f32SNate Lawson      */
7323a371f32SNate Lawson     Status = EcCommand(sc, EC_COMMAND_QUERY);
733dbd0058aSMike Smith     if (ACPI_FAILURE(Status)) {
7343a371f32SNate Lawson 	EcUnlock(sc);
7356971b3c7SMitsuru IWASAKI 	ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
7366971b3c7SMitsuru IWASAKI 		    "GPE query failed - %s\n", AcpiFormatException(Status));
7371f04e8f5SNate Lawson 	goto re_enable;
73815e32d5dSMike Smith     }
7393a371f32SNate Lawson     Data = EC_GET_DATA(sc);
740ef2374f7SNate Lawson     sc->ec_sci_pend = FALSE;
741ef2374f7SNate Lawson 
742ef2374f7SNate Lawson     /* Drop locks before evaluating _Qxx method since it may trigger GPEs. */
7433a371f32SNate Lawson     EcUnlock(sc);
74415e32d5dSMike Smith 
7451f04e8f5SNate Lawson     /* Ignore the value for "no outstanding event". (13.3.5) */
746c4a9fa45SNate Lawson     CTR2(KTR_ACPI, "ec query ok,%s running _Q%02x", Data ? "" : " not", Data);
7471f04e8f5SNate Lawson     if (Data == 0)
7481f04e8f5SNate Lawson 	goto re_enable;
7491f04e8f5SNate Lawson 
7501f04e8f5SNate Lawson     /* Evaluate _Qxx to respond to the controller. */
751ef2374f7SNate Lawson     snprintf(qxx, sizeof(qxx), "_Q%02x", Data);
752b3919c8dSMark Santcroos     AcpiUtStrupr(qxx);
753ee785aa9SJohn Baldwin     Status = AcpiEvaluateObject(sc->ec_handle, qxx, NULL, NULL);
7541f04e8f5SNate Lawson     if (ACPI_FAILURE(Status) && Status != AE_NOT_FOUND) {
7556971b3c7SMitsuru IWASAKI 	ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
7566971b3c7SMitsuru IWASAKI 		    "evaluation of GPE query method %s failed - %s\n",
757bfae45aaSMike Smith 		    qxx, AcpiFormatException(Status));
75815e32d5dSMike Smith     }
7591f04e8f5SNate Lawson 
7601f04e8f5SNate Lawson re_enable:
7611f04e8f5SNate Lawson     /* Re-enable the GPE event so we'll get future requests. */
762ef2374f7SNate Lawson     Status = AcpiEnableGpe(sc->ec_gpehandle, sc->ec_gpebit, ACPI_ISR);
7631f04e8f5SNate Lawson     if (ACPI_FAILURE(Status))
7641f04e8f5SNate Lawson 	printf("EcGpeQueryHandler: AcpiEnableEvent failed\n");
765c07572e7STakanori Watanabe }
76642f6d122SMike Smith 
767a9cf0dffSMike Smith /*
7681f04e8f5SNate Lawson  * Handle a GPE.  Currently we only handle SCI events as others must
7691f04e8f5SNate Lawson  * be handled by polling in EcWaitEvent().  This is because some ECs
7701f04e8f5SNate Lawson  * treat events as level when they should be edge-triggered.
771a9cf0dffSMike Smith  */
772f3fc4f8bSNate Lawson static uint32_t
773a9cf0dffSMike Smith EcGpeHandler(void *Context)
774c07572e7STakanori Watanabe {
775c07572e7STakanori Watanabe     struct acpi_ec_softc *sc = Context;
7761f04e8f5SNate Lawson     ACPI_STATUS		       Status;
777ef2374f7SNate Lawson     EC_STATUS		       EcStatus;
7786e141df2SNate Lawson     int			       query_pend;
779a9cf0dffSMike Smith 
7801f04e8f5SNate Lawson     KASSERT(Context != NULL, ("EcGpeHandler called with NULL"));
7811f04e8f5SNate Lawson 
7821395b555SNate Lawson     /*
7831395b555SNate Lawson      * Disable further GPEs while we handle this one.  Since we are directly
7841395b555SNate Lawson      * called by ACPI-CA and it may have unknown locks held, we specify the
785ef2374f7SNate Lawson      * ACPI_ISR flag to keep it from acquiring any more mutexes (although
786ef2374f7SNate Lawson      * sleeping would be ok since we're in an ithread.)
7871395b555SNate Lawson      */
7881395b555SNate Lawson     AcpiDisableGpe(sc->ec_gpehandle, sc->ec_gpebit, ACPI_ISR);
7891f04e8f5SNate Lawson 
790ef2374f7SNate Lawson     /* For interrupt (GPE) handler, don't acquire serialization lock. */
791ef2374f7SNate Lawson     Status = EcLock(sc, FALSE);
792ef2374f7SNate Lawson     if (ACPI_FAILURE(Status)) {
793ef2374f7SNate Lawson 	ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
794ef2374f7SNate Lawson 		    "GpeQuery lock error: %s\n", AcpiFormatException(Status));
795ef2374f7SNate Lawson 	return (-1);
796ef2374f7SNate Lawson     }
797ef2374f7SNate Lawson 
798ef2374f7SNate Lawson     /*
799ef2374f7SNate Lawson      * If burst was active, but the status bit was cleared, the EC had to
800ef2374f7SNate Lawson      * exit burst mode for some reason.  Record this for later.
801ef2374f7SNate Lawson      */
802ef2374f7SNate Lawson     EcStatus = EC_GET_CSR(sc);
803ef2374f7SNate Lawson     if (sc->ec_burstactive && (EcStatus & EC_FLAG_BURST_MODE) == 0) {
804ef2374f7SNate Lawson 	CTR0(KTR_ACPI, "ec burst disabled in query handler");
805ef2374f7SNate Lawson 	sc->ec_burstactive = FALSE;
806ef2374f7SNate Lawson     }
807ef2374f7SNate Lawson 
808ef2374f7SNate Lawson     /*
809ef2374f7SNate Lawson      * If the EC_SCI bit of the status register is not set, then pass
810ef2374f7SNate Lawson      * it along to any potential waiters as it may be an IBE/OBF event.
811ef2374f7SNate Lawson      * If it is set, queue a query handler.
812ef2374f7SNate Lawson      */
8136e141df2SNate Lawson     query_pend = FALSE;
814ef2374f7SNate Lawson     if ((EcStatus & EC_EVENT_SCI) == 0) {
815ef2374f7SNate Lawson 	CTR1(KTR_ACPI, "ec event was IBE/OBF, status %#x", EcStatus);
816ef2374f7SNate Lawson 	sc->ec_csrvalue = EcStatus;
817ef2374f7SNate Lawson 	wakeup(&sc->ec_csrvalue);
818ef2374f7SNate Lawson     } else if (!sc->ec_sci_pend) {
819ef2374f7SNate Lawson 	/* SCI bit set and no pending query handler, so schedule one. */
820ef2374f7SNate Lawson 	CTR0(KTR_ACPI, "ec queueing gpe handler");
8212be4e471SJung-uk Kim 	Status = AcpiOsExecute(OSL_GPE_HANDLER, EcGpeQueryHandler, Context);
822ef2374f7SNate Lawson 	if (ACPI_SUCCESS(Status)) {
823ef2374f7SNate Lawson 	    sc->ec_sci_pend = TRUE;
8246e141df2SNate Lawson 	    query_pend = TRUE;
8256e141df2SNate Lawson 	} else
8261f04e8f5SNate Lawson 	    printf("Queuing GPE query handler failed.\n");
8276e141df2SNate Lawson     }
8286e141df2SNate Lawson 
8296e141df2SNate Lawson     /*
8306e141df2SNate Lawson      * If we didn't queue a query handler, which will eventually re-enable
8316e141df2SNate Lawson      * the GPE, re-enable it right now so we can get more events.
8326e141df2SNate Lawson      */
8336e141df2SNate Lawson     if (!query_pend) {
8341395b555SNate Lawson 	Status = AcpiEnableGpe(sc->ec_gpehandle, sc->ec_gpebit, ACPI_ISR);
8351f04e8f5SNate Lawson 	if (ACPI_FAILURE(Status))
8366e141df2SNate Lawson 	    printf("EcGpeHandler: AcpiEnableGpe failed\n");
837ef2374f7SNate Lawson     }
838f3fc4f8bSNate Lawson 
839ef2374f7SNate Lawson     EcUnlock(sc);
840f3fc4f8bSNate Lawson     return (0);
84115e32d5dSMike Smith }
84215e32d5dSMike Smith 
84315e32d5dSMike Smith static ACPI_STATUS
844464c662eSNate Lawson EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function, void *Context,
845464c662eSNate Lawson 	     void **RegionContext)
84615e32d5dSMike Smith {
84742f6d122SMike Smith 
848b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
84942f6d122SMike Smith 
85015e32d5dSMike Smith     /*
85116844a97SNate Lawson      * If deactivating a region, always set the output to NULL.  Otherwise,
85216844a97SNate Lawson      * just pass the context through.
85315e32d5dSMike Smith      */
85416844a97SNate Lawson     if (Function == ACPI_REGION_DEACTIVATE)
85516844a97SNate Lawson 	*RegionContext = NULL;
85616844a97SNate Lawson     else
85715e32d5dSMike Smith 	*RegionContext = Context;
85815e32d5dSMike Smith 
85942f6d122SMike Smith     return_ACPI_STATUS (AE_OK);
86015e32d5dSMike Smith }
86115e32d5dSMike Smith 
86215e32d5dSMike Smith static ACPI_STATUS
863464c662eSNate Lawson EcSpaceHandler(UINT32 Function, ACPI_PHYSICAL_ADDRESS Address, UINT32 width,
864464c662eSNate Lawson 	       ACPI_INTEGER *Value, void *Context, void *RegionContext)
86515e32d5dSMike Smith {
86615e32d5dSMike Smith     struct acpi_ec_softc	*sc = (struct acpi_ec_softc *)Context;
8674ed391b8SNate Lawson     ACPI_STATUS			Status;
8681f04e8f5SNate Lawson     UINT8			EcAddr, EcData;
869ee785aa9SJohn Baldwin     int				i;
87015e32d5dSMike Smith 
871b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, (UINT32)Address);
8720ae55423SMike Smith 
8734ed391b8SNate Lawson     if (width % 8 != 0 || Value == NULL || Context == NULL)
8740ae55423SMike Smith 	return_ACPI_STATUS (AE_BAD_PARAMETER);
8754ed391b8SNate Lawson     if (Address + (width / 8) - 1 > 0xFF)
8764ed391b8SNate Lawson 	return_ACPI_STATUS (AE_BAD_ADDRESS);
87715e32d5dSMike Smith 
8784ed391b8SNate Lawson     if (Function == ACPI_READ)
8794ed391b8SNate Lawson 	*Value = 0;
8801f04e8f5SNate Lawson     EcAddr = Address;
8814ed391b8SNate Lawson     Status = AE_ERROR;
8824ed391b8SNate Lawson 
883ef2374f7SNate Lawson     /* Grab serialization lock to hold across command sequence. */
884ef2374f7SNate Lawson     Status = EcLock(sc, TRUE);
885464c662eSNate Lawson     if (ACPI_FAILURE(Status))
886f4b7de15SNate Lawson 	return_ACPI_STATUS (Status);
8871f04e8f5SNate Lawson 
888f4b7de15SNate Lawson     /* Perform the transaction(s), based on width. */
889f4b7de15SNate Lawson     for (i = 0; i < width; i += 8, EcAddr++) {
8901f04e8f5SNate Lawson 	switch (Function) {
8911f04e8f5SNate Lawson 	case ACPI_READ:
8921f04e8f5SNate Lawson 	    Status = EcRead(sc, EcAddr, &EcData);
8934ed391b8SNate Lawson 	    if (ACPI_SUCCESS(Status))
8944ed391b8SNate Lawson 		*Value |= ((ACPI_INTEGER)EcData) << i;
895ee785aa9SJohn Baldwin 	    break;
8961f04e8f5SNate Lawson 	case ACPI_WRITE:
8971f04e8f5SNate Lawson 	    EcData = (UINT8)((*Value) >> i);
8981f04e8f5SNate Lawson 	    Status = EcWrite(sc, EcAddr, &EcData);
8991f04e8f5SNate Lawson 	    break;
9001f04e8f5SNate Lawson 	default:
9011f04e8f5SNate Lawson 	    device_printf(sc->ec_dev, "invalid EcSpaceHandler function %d\n",
9021f04e8f5SNate Lawson 			  Function);
9031f04e8f5SNate Lawson 	    Status = AE_BAD_PARAMETER;
9041f04e8f5SNate Lawson 	    break;
9051f04e8f5SNate Lawson 	}
9061f04e8f5SNate Lawson 	if (ACPI_FAILURE(Status))
9074ed391b8SNate Lawson 	    break;
908ee785aa9SJohn Baldwin     }
9094ed391b8SNate Lawson 
910f4b7de15SNate Lawson     EcUnlock(sc);
9110ae55423SMike Smith     return_ACPI_STATUS (Status);
91215e32d5dSMike Smith }
9132a4ac806SMike Smith 
91415e32d5dSMike Smith static ACPI_STATUS
91515e32d5dSMike Smith EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event)
91615e32d5dSMike Smith {
91715e32d5dSMike Smith     EC_STATUS	EcStatus;
9181f04e8f5SNate Lawson     ACPI_STATUS	Status;
919ef2374f7SNate Lawson     int		count, i, retval, slp_ival;
92015e32d5dSMike Smith 
921f4b7de15SNate Lawson     ACPI_SERIAL_ASSERT(ec);
9221f04e8f5SNate Lawson     Status = AE_NO_HARDWARE_RESPONSE;
923ef2374f7SNate Lawson     EcStatus = 0;
92415e32d5dSMike Smith 
92515e32d5dSMike Smith     /*
926ef2374f7SNate Lawson      * Poll for up to ec_poll_time microseconds since many ECs complete
927ef2374f7SNate Lawson      * the command quickly, especially if in burst mode.
92815e32d5dSMike Smith      */
929ef2374f7SNate Lawson #if 0 /* Enable this as a possible workaround if EC times out. */
930ef2374f7SNate Lawson     AcpiOsStall(EC_POLL_DELAY);
931ef2374f7SNate Lawson #endif
932ef2374f7SNate Lawson     count = ec_poll_time / EC_POLL_DELAY;
933ef2374f7SNate Lawson     if (count <= 0)
934ef2374f7SNate Lawson 	count = 1;
935ef2374f7SNate Lawson     for (i = 0; i < count; i++) {
93615e32d5dSMike Smith 	EcStatus = EC_GET_CSR(sc);
937ef2374f7SNate Lawson 	if (sc->ec_burstactive && (EcStatus & EC_FLAG_BURST_MODE) == 0) {
938ef2374f7SNate Lawson 	    CTR0(KTR_ACPI, "ec burst disabled in waitevent (poll)");
939ef2374f7SNate Lawson 	    sc->ec_burstactive = FALSE;
940ef2374f7SNate Lawson 	}
941ff40920eSNate Lawson 	if (EVENT_READY(Event, EcStatus)) {
942ef2374f7SNate Lawson 	    CTR1(KTR_ACPI, "ec poll wait ready, status %#x", EcStatus);
9431f04e8f5SNate Lawson 	    Status = AE_OK;
94415e32d5dSMike Smith 	    break;
9451f04e8f5SNate Lawson 	}
946b0eefa38SNate Lawson 	AcpiOsStall(EC_POLL_DELAY);
9471f04e8f5SNate Lawson     }
948ff40920eSNate Lawson 
949ff40920eSNate Lawson     /*
950636af382SNate Lawson      * If we still don't have a response and we're up and running, wait up
951ef2374f7SNate Lawson      * to ec_timeout ms for completion, sleeping for chunks of 1 ms or the
952ef2374f7SNate Lawson      * smallest resolution hz supports.
953ff40920eSNate Lawson      */
954b0eefa38SNate Lawson     slp_ival = 0;
955b0eefa38SNate Lawson     if (Status != AE_OK) {
956b0eefa38SNate Lawson 	retval = ENXIO;
957ef2374f7SNate Lawson 	if (!cold) {
958ef2374f7SNate Lawson 	    slp_ival = hz / 1000;
959ef2374f7SNate Lawson 	    if (slp_ival != 0) {
960ef2374f7SNate Lawson 		count = ec_timeout / slp_ival;
961ef2374f7SNate Lawson 	    } else {
962ef2374f7SNate Lawson 		/* hz has less than 1000 Hz resolution so scale timeout. */
963b0eefa38SNate Lawson 		slp_ival = 1;
964ef2374f7SNate Lawson 		count = ec_timeout / (1000 / hz);
965ef2374f7SNate Lawson 	    }
966ef2374f7SNate Lawson 	} else
967ef2374f7SNate Lawson 	    count = ec_timeout;
968b0eefa38SNate Lawson 	for (i = 0; i < count; i++) {
9693a371f32SNate Lawson 	    if (retval != 0)
970ff40920eSNate Lawson 		EcStatus = EC_GET_CSR(sc);
9713a371f32SNate Lawson 	    else
9723a371f32SNate Lawson 		EcStatus = sc->ec_csrvalue;
973ef2374f7SNate Lawson 	    if (sc->ec_burstactive && (EcStatus & EC_FLAG_BURST_MODE) == 0) {
974ef2374f7SNate Lawson 		CTR0(KTR_ACPI, "ec burst disabled in waitevent (slp)");
975ef2374f7SNate Lawson 		sc->ec_burstactive = FALSE;
976ef2374f7SNate Lawson 	    }
977ff40920eSNate Lawson 	    if (EVENT_READY(Event, EcStatus)) {
978ef2374f7SNate Lawson 		CTR1(KTR_ACPI, "ec sleep wait ready, status %#x", EcStatus);
979ff40920eSNate Lawson 		Status = AE_OK;
980ff40920eSNate Lawson 		break;
981ff40920eSNate Lawson 	    }
982ef2374f7SNate Lawson 	    if (!cold) {
983ef2374f7SNate Lawson 		retval = msleep(&sc->ec_csrvalue, &sc->ec_mtx, PZERO, "ecpoll",
984ef2374f7SNate Lawson 		    slp_ival);
985ef2374f7SNate Lawson 	    } else
986ef2374f7SNate Lawson 		AcpiOsStall(1000);
987ff40920eSNate Lawson 	}
9881f04e8f5SNate Lawson     }
9891f04e8f5SNate Lawson 
9901f04e8f5SNate Lawson     return (Status);
9911f04e8f5SNate Lawson }
9921f04e8f5SNate Lawson 
9931f04e8f5SNate Lawson static ACPI_STATUS
9941f04e8f5SNate Lawson EcCommand(struct acpi_ec_softc *sc, EC_COMMAND cmd)
9951f04e8f5SNate Lawson {
996ef2374f7SNate Lawson     ACPI_STATUS	status;
997ef2374f7SNate Lawson     EC_EVENT	event;
998ef2374f7SNate Lawson     EC_STATUS	ec_status;
9991f04e8f5SNate Lawson 
1000f4b7de15SNate Lawson     ACPI_SERIAL_ASSERT(ec);
10011f04e8f5SNate Lawson 
1002ef2374f7SNate Lawson     /* Don't use burst mode if user disabled it. */
1003ef2374f7SNate Lawson     if (!ec_burst_mode && cmd == EC_COMMAND_BURST_ENABLE)
1004ef2374f7SNate Lawson 	return (AE_ERROR);
1005ef2374f7SNate Lawson 
10061f04e8f5SNate Lawson     /* Decide what to wait for based on command type. */
10071f04e8f5SNate Lawson     switch (cmd) {
10081f04e8f5SNate Lawson     case EC_COMMAND_READ:
100915e32d5dSMike Smith     case EC_COMMAND_WRITE:
10101f04e8f5SNate Lawson     case EC_COMMAND_BURST_DISABLE:
1011ef2374f7SNate Lawson 	event = EC_EVENT_INPUT_BUFFER_EMPTY;
10121f04e8f5SNate Lawson 	break;
10131f04e8f5SNate Lawson     case EC_COMMAND_QUERY:
10141f04e8f5SNate Lawson     case EC_COMMAND_BURST_ENABLE:
1015ef2374f7SNate Lawson 	event = EC_EVENT_OUTPUT_BUFFER_FULL;
101615e32d5dSMike Smith 	break;
101715e32d5dSMike Smith     default:
10186971b3c7SMitsuru IWASAKI 	ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
10191f04e8f5SNate Lawson 		    "EcCommand: Invalid command %#x\n", cmd);
10201f04e8f5SNate Lawson 	return (AE_BAD_PARAMETER);
1021464c662eSNate Lawson     }
10221f04e8f5SNate Lawson 
10231f04e8f5SNate Lawson     /* Run the command and wait for the chosen event. */
1024ef2374f7SNate Lawson     CTR1(KTR_ACPI, "ec running command %#x", cmd);
10251f04e8f5SNate Lawson     EC_SET_CSR(sc, cmd);
1026ef2374f7SNate Lawson     status = EcWaitEvent(sc, event);
1027ef2374f7SNate Lawson     if (ACPI_SUCCESS(status)) {
1028ef2374f7SNate Lawson 	/* If we succeeded, burst flag should now be present. */
1029ef2374f7SNate Lawson 	if (cmd == EC_COMMAND_BURST_ENABLE) {
1030ef2374f7SNate Lawson 	    ec_status = EC_GET_CSR(sc);
1031ef2374f7SNate Lawson 	    if ((ec_status & EC_FLAG_BURST_MODE) == 0)
1032ef2374f7SNate Lawson 		status = AE_ERROR;
1033ef2374f7SNate Lawson 	}
1034ef2374f7SNate Lawson     } else {
10356971b3c7SMitsuru IWASAKI 	ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
10361f04e8f5SNate Lawson 		    "EcCommand: no response to %#x\n", cmd);
1037464c662eSNate Lawson     }
103815e32d5dSMike Smith 
1039ef2374f7SNate Lawson     return (status);
104015e32d5dSMike Smith }
104115e32d5dSMike Smith 
104215e32d5dSMike Smith static ACPI_STATUS
104315e32d5dSMike Smith EcRead(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data)
104415e32d5dSMike Smith {
1045ef2374f7SNate Lawson     ACPI_STATUS	status;
1046ef2374f7SNate Lawson     UINT8 data;
104715e32d5dSMike Smith 
1048f4b7de15SNate Lawson     ACPI_SERIAL_ASSERT(ec);
1049c4a9fa45SNate Lawson     CTR1(KTR_ACPI, "ec read from %#x", Address);
105015e32d5dSMike Smith 
10511f04e8f5SNate Lawson     /* If we can't start burst mode, continue anyway. */
1052ef2374f7SNate Lawson     status = EcCommand(sc, EC_COMMAND_BURST_ENABLE);
1053ef2374f7SNate Lawson     if (status == AE_OK) {
1054ef2374f7SNate Lawson     	data = EC_GET_DATA(sc);
1055ef2374f7SNate Lawson 	if (data == EC_BURST_ACK) {
1056ef2374f7SNate Lawson 	    CTR0(KTR_ACPI, "ec burst enabled");
1057ef2374f7SNate Lawson 	    sc->ec_burstactive = TRUE;
1058ef2374f7SNate Lawson 	}
1059ef2374f7SNate Lawson     }
106015e32d5dSMike Smith 
1061ef2374f7SNate Lawson     status = EcCommand(sc, EC_COMMAND_READ);
1062ef2374f7SNate Lawson     if (ACPI_FAILURE(status))
1063ef2374f7SNate Lawson 	return (status);
106415e32d5dSMike Smith 
106515e32d5dSMike Smith     EC_SET_DATA(sc, Address);
1066ef2374f7SNate Lawson     status = EcWaitEvent(sc, EC_EVENT_OUTPUT_BUFFER_FULL);
1067ef2374f7SNate Lawson     if (ACPI_FAILURE(status)) {
10686971b3c7SMitsuru IWASAKI 	ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
10696971b3c7SMitsuru IWASAKI 		    "EcRead: Failed waiting for EC to send data.\n");
1070ef2374f7SNate Lawson 	return (status);
107115e32d5dSMike Smith     }
107215e32d5dSMike Smith 
1073464c662eSNate Lawson     *Data = EC_GET_DATA(sc);
107415e32d5dSMike Smith 
10751f04e8f5SNate Lawson     if (sc->ec_burstactive) {
1076ef2374f7SNate Lawson 	status = EcCommand(sc, EC_COMMAND_BURST_DISABLE);
1077ef2374f7SNate Lawson 	if (ACPI_FAILURE(status))
1078ef2374f7SNate Lawson 	    return (status);
1079ef2374f7SNate Lawson 	sc->ec_burstactive = FALSE;
1080ef2374f7SNate Lawson 	CTR0(KTR_ACPI, "ec disabled burst ok");
10811f04e8f5SNate Lawson     }
108215e32d5dSMike Smith 
108315e32d5dSMike Smith     return (AE_OK);
108415e32d5dSMike Smith }
108515e32d5dSMike Smith 
108615e32d5dSMike Smith static ACPI_STATUS
108715e32d5dSMike Smith EcWrite(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data)
108815e32d5dSMike Smith {
1089ef2374f7SNate Lawson     ACPI_STATUS	status;
1090ef2374f7SNate Lawson     UINT8 data;
109115e32d5dSMike Smith 
1092f4b7de15SNate Lawson     ACPI_SERIAL_ASSERT(ec);
1093c4a9fa45SNate Lawson     CTR2(KTR_ACPI, "ec write to %#x, data %#x", Address, *Data);
109415e32d5dSMike Smith 
10951f04e8f5SNate Lawson     /* If we can't start burst mode, continue anyway. */
1096ef2374f7SNate Lawson     status = EcCommand(sc, EC_COMMAND_BURST_ENABLE);
1097ef2374f7SNate Lawson     if (status == AE_OK) {
1098ef2374f7SNate Lawson     	data = EC_GET_DATA(sc);
1099ef2374f7SNate Lawson 	if (data == EC_BURST_ACK) {
1100ef2374f7SNate Lawson 	    CTR0(KTR_ACPI, "ec burst enabled");
1101ef2374f7SNate Lawson 	    sc->ec_burstactive = TRUE;
1102ef2374f7SNate Lawson 	}
1103ef2374f7SNate Lawson     }
110415e32d5dSMike Smith 
1105ef2374f7SNate Lawson     status = EcCommand(sc, EC_COMMAND_WRITE);
1106ef2374f7SNate Lawson     if (ACPI_FAILURE(status))
1107ef2374f7SNate Lawson 	return (status);
110815e32d5dSMike Smith 
110915e32d5dSMike Smith     EC_SET_DATA(sc, Address);
1110ef2374f7SNate Lawson     status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY);
1111ef2374f7SNate Lawson     if (ACPI_FAILURE(status)) {
11126971b3c7SMitsuru IWASAKI 	ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
11131f04e8f5SNate Lawson 		    "EcRead: Failed waiting for EC to process address\n");
1114ef2374f7SNate Lawson 	return (status);
111515e32d5dSMike Smith     }
111615e32d5dSMike Smith 
111715e32d5dSMike Smith     EC_SET_DATA(sc, *Data);
1118ef2374f7SNate Lawson     status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY);
1119ef2374f7SNate Lawson     if (ACPI_FAILURE(status)) {
11206971b3c7SMitsuru IWASAKI 	ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
11211f04e8f5SNate Lawson 		    "EcWrite: Failed waiting for EC to process data\n");
1122ef2374f7SNate Lawson 	return (status);
112315e32d5dSMike Smith     }
112415e32d5dSMike Smith 
11251f04e8f5SNate Lawson     if (sc->ec_burstactive) {
1126ef2374f7SNate Lawson 	status = EcCommand(sc, EC_COMMAND_BURST_DISABLE);
1127ef2374f7SNate Lawson 	if (ACPI_FAILURE(status))
1128ef2374f7SNate Lawson 	    return (status);
1129ef2374f7SNate Lawson 	sc->ec_burstactive = FALSE;
1130ef2374f7SNate Lawson 	CTR0(KTR_ACPI, "ec disabled burst ok");
11311f04e8f5SNate Lawson     }
113215e32d5dSMike Smith 
113315e32d5dSMike Smith     return (AE_OK);
113415e32d5dSMike Smith }
1135