xref: /freebsd/sys/dev/acpica/acpi_ec.c (revision 4aed56306389b4b0ca8bff4ca8cf91a95d60996c)
115e32d5dSMike Smith /*-
283dcc133SNate Lawson  * Copyright (c) 2003-2007 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 
29aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
30aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
31aad970f1SDavid E. O'Brien 
3215e32d5dSMike Smith #include "opt_acpi.h"
3315e32d5dSMike Smith #include <sys/param.h>
3415e32d5dSMike Smith #include <sys/kernel.h>
35daec9284SConrad Meyer #include <sys/ktr.h>
3615e32d5dSMike Smith #include <sys/bus.h>
374e7f640dSJohn Baldwin #include <sys/lock.h>
380025fb0fSNate Lawson #include <sys/malloc.h>
39b0eefa38SNate Lawson #include <sys/module.h>
40b0eefa38SNate Lawson #include <sys/sx.h>
4115e32d5dSMike Smith 
4215e32d5dSMike Smith #include <machine/bus.h>
4315e32d5dSMike Smith #include <machine/resource.h>
4415e32d5dSMike Smith #include <sys/rman.h>
4515e32d5dSMike Smith 
46129d3046SJung-uk Kim #include <contrib/dev/acpica/include/acpi.h>
47129d3046SJung-uk Kim #include <contrib/dev/acpica/include/accommon.h>
48129d3046SJung-uk Kim 
4915e32d5dSMike Smith #include <dev/acpica/acpivar.h>
5015e32d5dSMike Smith 
51f4b7de15SNate Lawson /* Hooks for the ACPI CA debugging infrastructure */
52a9cf0dffSMike Smith #define _COMPONENT	ACPI_EC
53dbd0058aSMike Smith ACPI_MODULE_NAME("EC")
540ae55423SMike Smith 
55da3b867eSMike Smith /*
56da3b867eSMike Smith  * EC_COMMAND:
57da3b867eSMike Smith  * -----------
58da3b867eSMike Smith  */
59da3b867eSMike Smith typedef UINT8				EC_COMMAND;
60da3b867eSMike Smith 
61da3b867eSMike Smith #define EC_COMMAND_UNKNOWN		((EC_COMMAND) 0x00)
62da3b867eSMike Smith #define EC_COMMAND_READ			((EC_COMMAND) 0x80)
63da3b867eSMike Smith #define EC_COMMAND_WRITE		((EC_COMMAND) 0x81)
64da3b867eSMike Smith #define EC_COMMAND_BURST_ENABLE		((EC_COMMAND) 0x82)
65da3b867eSMike Smith #define EC_COMMAND_BURST_DISABLE	((EC_COMMAND) 0x83)
66da3b867eSMike Smith #define EC_COMMAND_QUERY		((EC_COMMAND) 0x84)
67da3b867eSMike Smith 
68da3b867eSMike Smith /*
69da3b867eSMike Smith  * EC_STATUS:
70da3b867eSMike Smith  * ----------
71da3b867eSMike Smith  * The encoding of the EC status register is illustrated below.
72da3b867eSMike Smith  * Note that a set bit (1) indicates the property is TRUE
73da3b867eSMike Smith  * (e.g. if bit 0 is set then the output buffer is full).
74da3b867eSMike Smith  * +-+-+-+-+-+-+-+-+
75da3b867eSMike Smith  * |7|6|5|4|3|2|1|0|
76da3b867eSMike Smith  * +-+-+-+-+-+-+-+-+
77da3b867eSMike Smith  *  | | | | | | | |
78da3b867eSMike Smith  *  | | | | | | | +- Output Buffer Full?
79da3b867eSMike Smith  *  | | | | | | +--- Input Buffer Full?
80da3b867eSMike Smith  *  | | | | | +----- <reserved>
81da3b867eSMike Smith  *  | | | | +------- Data Register is Command Byte?
82da3b867eSMike Smith  *  | | | +--------- Burst Mode Enabled?
83da3b867eSMike Smith  *  | | +----------- SCI Event?
84da3b867eSMike Smith  *  | +------------- SMI Event?
85ef2374f7SNate Lawson  *  +--------------- <reserved>
86da3b867eSMike Smith  *
87da3b867eSMike Smith  */
88da3b867eSMike Smith typedef UINT8				EC_STATUS;
89da3b867eSMike Smith 
90da3b867eSMike Smith #define EC_FLAG_OUTPUT_BUFFER		((EC_STATUS) 0x01)
91da3b867eSMike Smith #define EC_FLAG_INPUT_BUFFER		((EC_STATUS) 0x02)
92ef2374f7SNate Lawson #define EC_FLAG_DATA_IS_CMD		((EC_STATUS) 0x08)
93da3b867eSMike Smith #define EC_FLAG_BURST_MODE		((EC_STATUS) 0x10)
94da3b867eSMike Smith 
95da3b867eSMike Smith /*
96da3b867eSMike Smith  * EC_EVENT:
97da3b867eSMike Smith  * ---------
98da3b867eSMike Smith  */
99da3b867eSMike Smith typedef UINT8				EC_EVENT;
100da3b867eSMike Smith 
101da3b867eSMike Smith #define EC_EVENT_UNKNOWN		((EC_EVENT) 0x00)
102da3b867eSMike Smith #define EC_EVENT_OUTPUT_BUFFER_FULL	((EC_EVENT) 0x01)
103da3b867eSMike Smith #define EC_EVENT_INPUT_BUFFER_EMPTY	((EC_EVENT) 0x02)
104da3b867eSMike Smith #define EC_EVENT_SCI			((EC_EVENT) 0x20)
105ef2374f7SNate Lawson #define EC_EVENT_SMI			((EC_EVENT) 0x40)
106ef2374f7SNate Lawson 
107ef2374f7SNate Lawson /* Data byte returned after burst enable indicating it was successful. */
108ef2374f7SNate Lawson #define EC_BURST_ACK			0x90
109da3b867eSMike Smith 
110da3b867eSMike Smith /*
111da3b867eSMike Smith  * Register access primitives
112da3b867eSMike Smith  */
113da3b867eSMike Smith #define EC_GET_DATA(sc)							\
114da3b867eSMike Smith 	bus_space_read_1((sc)->ec_data_tag, (sc)->ec_data_handle, 0)
115da3b867eSMike Smith 
116da3b867eSMike Smith #define EC_SET_DATA(sc, v)						\
117da3b867eSMike Smith 	bus_space_write_1((sc)->ec_data_tag, (sc)->ec_data_handle, 0, (v))
118da3b867eSMike Smith 
119da3b867eSMike Smith #define EC_GET_CSR(sc)							\
120da3b867eSMike Smith 	bus_space_read_1((sc)->ec_csr_tag, (sc)->ec_csr_handle, 0)
121da3b867eSMike Smith 
122da3b867eSMike Smith #define EC_SET_CSR(sc, v)						\
123da3b867eSMike Smith 	bus_space_write_1((sc)->ec_csr_tag, (sc)->ec_csr_handle, 0, (v))
124da3b867eSMike Smith 
1250025fb0fSNate Lawson /* Additional params to pass from the probe routine */
1260025fb0fSNate Lawson struct acpi_ec_params {
1270025fb0fSNate Lawson     int		glk;
1280025fb0fSNate Lawson     int		gpe_bit;
1290025fb0fSNate Lawson     ACPI_HANDLE	gpe_handle;
1300025fb0fSNate Lawson     int		uid;
1310025fb0fSNate Lawson };
1320025fb0fSNate Lawson 
133da3b867eSMike Smith /*
134da3b867eSMike Smith  * Driver softc.
135da3b867eSMike Smith  */
13615e32d5dSMike Smith struct acpi_ec_softc {
13715e32d5dSMike Smith     device_t		ec_dev;
13815e32d5dSMike Smith     ACPI_HANDLE		ec_handle;
1390025fb0fSNate Lawson     int			ec_uid;
1400025fb0fSNate Lawson     ACPI_HANDLE		ec_gpehandle;
1413a371f32SNate Lawson     UINT8		ec_gpebit;
14215e32d5dSMike Smith 
14315e32d5dSMike Smith     int			ec_data_rid;
14415e32d5dSMike Smith     struct resource	*ec_data_res;
14515e32d5dSMike Smith     bus_space_tag_t	ec_data_tag;
14615e32d5dSMike Smith     bus_space_handle_t	ec_data_handle;
14715e32d5dSMike Smith 
14815e32d5dSMike Smith     int			ec_csr_rid;
14915e32d5dSMike Smith     struct resource	*ec_csr_res;
15015e32d5dSMike Smith     bus_space_tag_t	ec_csr_tag;
15115e32d5dSMike Smith     bus_space_handle_t	ec_csr_handle;
15215e32d5dSMike Smith 
1531f04e8f5SNate Lawson     int			ec_glk;
1541f04e8f5SNate Lawson     int			ec_glkhandle;
155ef2374f7SNate Lawson     int			ec_burstactive;
156ef2374f7SNate Lawson     int			ec_sci_pend;
1575ff14fa9SAndriy Gapon     volatile u_int	ec_gencount;
1580bfeadedSTakanori Watanabe     int			ec_suspending;
15915e32d5dSMike Smith };
16015e32d5dSMike Smith 
1611f04e8f5SNate Lawson /*
162f4b7de15SNate Lawson  * XXX njl
1631f04e8f5SNate Lawson  * I couldn't find it in the spec but other implementations also use a
1641f04e8f5SNate Lawson  * value of 1 ms for the time to acquire global lock.
1651f04e8f5SNate Lawson  */
1661f04e8f5SNate Lawson #define EC_LOCK_TIMEOUT	1000
1674690674eSMitsuru IWASAKI 
168ef2374f7SNate Lawson /* Default delay in microseconds between each run of the status polling loop. */
1695ff14fa9SAndriy Gapon #define EC_POLL_DELAY	50
170ef2374f7SNate Lawson 
171ef2374f7SNate Lawson /* Total time in ms spent waiting for a response from EC. */
17283dcc133SNate Lawson #define EC_TIMEOUT	750
17315e32d5dSMike Smith 
174ff40920eSNate Lawson #define EVENT_READY(event, status)			\
175ff40920eSNate Lawson 	(((event) == EC_EVENT_OUTPUT_BUFFER_FULL &&	\
176ff40920eSNate Lawson 	 ((status) & EC_FLAG_OUTPUT_BUFFER) != 0) ||	\
177ff40920eSNate Lawson 	 ((event) == EC_EVENT_INPUT_BUFFER_EMPTY && 	\
178ff40920eSNate Lawson 	 ((status) & EC_FLAG_INPUT_BUFFER) == 0))
179ff40920eSNate Lawson 
180f4b7de15SNate Lawson ACPI_SERIAL_DECL(ec, "ACPI embedded controller");
181f4b7de15SNate Lawson 
1827029da5cSPawel Biernacki static SYSCTL_NODE(_debug_acpi, OID_AUTO, ec,
1837029da5cSPawel Biernacki     CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
1847029da5cSPawel Biernacki     "EC debugging");
185ef2374f7SNate Lawson 
186675e5627SNate Lawson static int	ec_burst_mode;
187af3b2549SHans Petter Selasky SYSCTL_INT(_debug_acpi_ec, OID_AUTO, burst, CTLFLAG_RWTUN, &ec_burst_mode, 0,
188ef2374f7SNate Lawson     "Enable use of burst mode (faster for nearly all systems)");
18983dcc133SNate Lawson static int	ec_polled_mode;
190af3b2549SHans Petter Selasky SYSCTL_INT(_debug_acpi_ec, OID_AUTO, polled, CTLFLAG_RWTUN, &ec_polled_mode, 0,
19183dcc133SNate Lawson     "Force use of polled mode (only if interrupt mode doesn't work)");
192ef2374f7SNate Lawson static int	ec_timeout = EC_TIMEOUT;
193af3b2549SHans Petter Selasky SYSCTL_INT(_debug_acpi_ec, OID_AUTO, timeout, CTLFLAG_RWTUN, &ec_timeout,
194ef2374f7SNate Lawson     EC_TIMEOUT, "Total time spent waiting for a response (poll+sleep)");
195ef2374f7SNate Lawson 
19683dcc133SNate Lawson static ACPI_STATUS
19783dcc133SNate Lawson EcLock(struct acpi_ec_softc *sc)
19815e32d5dSMike Smith {
199f4b7de15SNate Lawson     ACPI_STATUS	status;
20015e32d5dSMike Smith 
20135440dd3SNate Lawson     /* If _GLK is non-zero, acquire the global lock. */
20235440dd3SNate Lawson     status = AE_OK;
20335440dd3SNate Lawson     if (sc->ec_glk) {
20435440dd3SNate Lawson 	status = AcpiAcquireGlobalLock(EC_LOCK_TIMEOUT, &sc->ec_glkhandle);
20535440dd3SNate Lawson 	if (ACPI_FAILURE(status))
20635440dd3SNate Lawson 	    return (status);
20735440dd3SNate Lawson     }
208f4b7de15SNate Lawson     ACPI_SERIAL_BEGIN(ec);
20915e32d5dSMike Smith     return (status);
21015e32d5dSMike Smith }
21115e32d5dSMike Smith 
21283dcc133SNate Lawson static void
21315e32d5dSMike Smith EcUnlock(struct acpi_ec_softc *sc)
21415e32d5dSMike Smith {
215f4b7de15SNate Lawson     ACPI_SERIAL_END(ec);
21635440dd3SNate Lawson     if (sc->ec_glk)
21735440dd3SNate Lawson 	AcpiReleaseGlobalLock(sc->ec_glkhandle);
21815e32d5dSMike Smith }
21915e32d5dSMike Smith 
2205a77b11bSJung-uk Kim static UINT32		EcGpeHandler(ACPI_HANDLE, UINT32, void *);
22115e32d5dSMike Smith static ACPI_STATUS	EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function,
22215e32d5dSMike Smith 				void *Context, void **return_Context);
223464c662eSNate Lawson static ACPI_STATUS	EcSpaceHandler(UINT32 Function,
224464c662eSNate Lawson 				ACPI_PHYSICAL_ADDRESS Address,
225e115a49fSJung-uk Kim 				UINT32 Width, UINT64 *Value,
22615e32d5dSMike Smith 				void *Context, void *RegionContext);
22783dcc133SNate Lawson static ACPI_STATUS	EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event,
22883dcc133SNate Lawson 				u_int gen_count);
2291f04e8f5SNate Lawson static ACPI_STATUS	EcCommand(struct acpi_ec_softc *sc, EC_COMMAND cmd);
230464c662eSNate Lawson static ACPI_STATUS	EcRead(struct acpi_ec_softc *sc, UINT8 Address,
231464c662eSNate Lawson 				UINT8 *Data);
232464c662eSNate Lawson static ACPI_STATUS	EcWrite(struct acpi_ec_softc *sc, UINT8 Address,
233e115a49fSJung-uk Kim 				UINT8 Data);
23415e32d5dSMike Smith static int		acpi_ec_probe(device_t dev);
23515e32d5dSMike Smith static int		acpi_ec_attach(device_t dev);
2360bfeadedSTakanori Watanabe static int		acpi_ec_suspend(device_t dev);
2370bfeadedSTakanori Watanabe static int		acpi_ec_resume(device_t dev);
238340a7f6aSNate Lawson static int		acpi_ec_shutdown(device_t dev);
239e33bea8dSNate Lawson static int		acpi_ec_read_method(device_t dev, u_int addr,
2409a179dd8SJung-uk Kim 				UINT64 *val, int width);
241e33bea8dSNate Lawson static int		acpi_ec_write_method(device_t dev, u_int addr,
2429a179dd8SJung-uk Kim 				UINT64 val, int width);
24315e32d5dSMike Smith 
24415e32d5dSMike Smith static device_method_t acpi_ec_methods[] = {
24515e32d5dSMike Smith     /* Device interface */
24615e32d5dSMike Smith     DEVMETHOD(device_probe,	acpi_ec_probe),
24715e32d5dSMike Smith     DEVMETHOD(device_attach,	acpi_ec_attach),
2480bfeadedSTakanori Watanabe     DEVMETHOD(device_suspend,	acpi_ec_suspend),
2490bfeadedSTakanori Watanabe     DEVMETHOD(device_resume,	acpi_ec_resume),
250340a7f6aSNate Lawson     DEVMETHOD(device_shutdown,	acpi_ec_shutdown),
25115e32d5dSMike Smith 
252e33bea8dSNate Lawson     /* Embedded controller interface */
253e33bea8dSNate Lawson     DEVMETHOD(acpi_ec_read,	acpi_ec_read_method),
254e33bea8dSNate Lawson     DEVMETHOD(acpi_ec_write,	acpi_ec_write_method),
255e33bea8dSNate Lawson 
25661bfd867SSofian Brabez     DEVMETHOD_END
25715e32d5dSMike Smith };
25815e32d5dSMike Smith 
25915e32d5dSMike Smith static driver_t acpi_ec_driver = {
26015e32d5dSMike Smith     "acpi_ec",
26115e32d5dSMike Smith     acpi_ec_methods,
26215e32d5dSMike Smith     sizeof(struct acpi_ec_softc),
26315e32d5dSMike Smith };
26415e32d5dSMike Smith 
2653273b005SMike Smith static devclass_t acpi_ec_devclass;
26615e32d5dSMike Smith DRIVER_MODULE(acpi_ec, acpi, acpi_ec_driver, acpi_ec_devclass, 0, 0);
26764278df5SNate Lawson MODULE_DEPEND(acpi_ec, acpi, 1, 1, 1);
26815e32d5dSMike Smith 
26915e32d5dSMike Smith /*
270f8335e3aSNate Lawson  * Look for an ECDT and if we find one, set up default GPE and
271f8335e3aSNate Lawson  * space handlers to catch attempts to access EC space before
27215e32d5dSMike Smith  * we have a real driver instance in place.
27383dcc133SNate Lawson  *
27483dcc133SNate Lawson  * TODO: Some old Gateway laptops need us to fake up an ECDT or
27583dcc133SNate Lawson  * otherwise attach early so that _REG methods can run.
27615e32d5dSMike Smith  */
277f8335e3aSNate Lawson void
278f8335e3aSNate Lawson acpi_ec_ecdt_probe(device_t parent)
27915e32d5dSMike Smith {
280f8335e3aSNate Lawson     ACPI_TABLE_ECDT *ecdt;
281f8335e3aSNate Lawson     ACPI_STATUS	     status;
282f8335e3aSNate Lawson     device_t	     child;
283f8335e3aSNate Lawson     ACPI_HANDLE	     h;
2840025fb0fSNate Lawson     struct acpi_ec_params *params;
285f8335e3aSNate Lawson 
286b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
28715e32d5dSMike Smith 
288f8335e3aSNate Lawson     /* Find and validate the ECDT. */
2892be4e471SJung-uk Kim     status = AcpiGetTable(ACPI_SIG_ECDT, 1, (ACPI_TABLE_HEADER **)&ecdt);
290f8335e3aSNate Lawson     if (ACPI_FAILURE(status) ||
2912be4e471SJung-uk Kim 	ecdt->Control.BitWidth != 8 ||
2922be4e471SJung-uk Kim 	ecdt->Data.BitWidth != 8) {
293f8335e3aSNate Lawson 	return;
29415e32d5dSMike Smith     }
29515e32d5dSMike Smith 
296f8335e3aSNate Lawson     /* Create the child device with the given unit number. */
297404b0d10SJung-uk Kim     child = BUS_ADD_CHILD(parent, 3, "acpi_ec", ecdt->Uid);
298f8335e3aSNate Lawson     if (child == NULL) {
2990025fb0fSNate Lawson 	printf("%s: can't add child\n", __func__);
300f8335e3aSNate Lawson 	return;
301f8335e3aSNate Lawson     }
302f8335e3aSNate Lawson 
303f8335e3aSNate Lawson     /* Find and save the ACPI handle for this device. */
3042be4e471SJung-uk Kim     status = AcpiGetHandle(NULL, ecdt->Id, &h);
305f8335e3aSNate Lawson     if (ACPI_FAILURE(status)) {
306f8335e3aSNate Lawson 	device_delete_child(parent, child);
3070025fb0fSNate Lawson 	printf("%s: can't get handle\n", __func__);
308f8335e3aSNate Lawson 	return;
309f8335e3aSNate Lawson     }
310f8335e3aSNate Lawson     acpi_set_handle(child, h);
311f8335e3aSNate Lawson 
312f8335e3aSNate Lawson     /* Set the data and CSR register addresses. */
3132be4e471SJung-uk Kim     bus_set_resource(child, SYS_RES_IOPORT, 0, ecdt->Data.Address,
314f8335e3aSNate Lawson 	/*count*/1);
3152be4e471SJung-uk Kim     bus_set_resource(child, SYS_RES_IOPORT, 1, ecdt->Control.Address,
316f8335e3aSNate Lawson 	/*count*/1);
317f8335e3aSNate Lawson 
31815e32d5dSMike Smith     /*
319f8335e3aSNate Lawson      * Store values for the probe/attach routines to use.  Store the
320c868ac7dSNate Lawson      * ECDT GPE bit and set the global lock flag according to _GLK.
3210025fb0fSNate Lawson      * Note that it is not perfectly correct to be evaluating a method
3220025fb0fSNate Lawson      * before initializing devices, but in practice this function
3230025fb0fSNate Lawson      * should be safe to call at this point.
32415e32d5dSMike Smith      */
3250025fb0fSNate Lawson     params = malloc(sizeof(struct acpi_ec_params), M_TEMP, M_WAITOK | M_ZERO);
3260025fb0fSNate Lawson     params->gpe_handle = NULL;
3272be4e471SJung-uk Kim     params->gpe_bit = ecdt->Gpe;
3282be4e471SJung-uk Kim     params->uid = ecdt->Uid;
3290025fb0fSNate Lawson     acpi_GetInteger(h, "_GLK", &params->glk);
3300025fb0fSNate Lawson     acpi_set_private(child, params);
331f8335e3aSNate Lawson 
332f8335e3aSNate Lawson     /* Finish the attach process. */
333f8335e3aSNate Lawson     if (device_probe_and_attach(child) != 0)
334f8335e3aSNate Lawson 	device_delete_child(parent, child);
335f8335e3aSNate Lawson }
336f8335e3aSNate Lawson 
33715e32d5dSMike Smith static int
33815e32d5dSMike Smith acpi_ec_probe(device_t dev)
33915e32d5dSMike Smith {
3400025fb0fSNate Lawson     ACPI_BUFFER buf;
341f8335e3aSNate Lawson     ACPI_HANDLE h;
3420025fb0fSNate Lawson     ACPI_OBJECT *obj;
343f8335e3aSNate Lawson     ACPI_STATUS status;
344f8335e3aSNate Lawson     device_t	peer;
345f8335e3aSNate Lawson     char	desc[64];
346f6eb382cSAndriy Gapon     int		ecdt;
347*4aed5630SConrad Meyer     int		ret, rc;
3480025fb0fSNate Lawson     struct acpi_ec_params *params;
3495fcc8a58SNate Lawson     static char *ec_ids[] = { "PNP0C09", NULL };
3500ae55423SMike Smith 
3511a305bdaSBen Widawsky     ret = ENXIO;
3521a305bdaSBen Widawsky 
353c72508e2SNate Lawson     /* Check that this is a device and that EC is not disabled. */
354c72508e2SNate Lawson     if (acpi_get_type(dev) != ACPI_TYPE_DEVICE || acpi_disabled("ec"))
3551a305bdaSBen Widawsky 	return (ret);
35615e32d5dSMike Smith 
3571a305bdaSBen Widawsky     if (device_is_devclass_fixed(dev)) {
35815e32d5dSMike Smith 	/*
3591a305bdaSBen Widawsky 	 * If probed via ECDT, set description and continue. Otherwise, we can
3601a305bdaSBen Widawsky 	 * access the namespace and make sure this is not a duplicate probe.
36115e32d5dSMike Smith 	 */
362f6eb382cSAndriy Gapon         ecdt = 1;
3631a305bdaSBen Widawsky         params = acpi_get_private(dev);
3641a305bdaSBen Widawsky 	if (params != NULL)
3651f04e8f5SNate Lawson 	    ret = 0;
3661a305bdaSBen Widawsky 
3671a305bdaSBen Widawsky 	goto out;
368ec60b7f9SBen Widawsky     } else
369ec60b7f9SBen Widawsky 	ecdt = 0;
3701a305bdaSBen Widawsky 
371*4aed5630SConrad Meyer     rc = ACPI_ID_PROBE(device_get_parent(dev), dev, ec_ids, NULL);
372*4aed5630SConrad Meyer     if (rc > 0)
373*4aed5630SConrad Meyer 	return (rc);
3741a305bdaSBen Widawsky 
3751a305bdaSBen Widawsky     params = malloc(sizeof(struct acpi_ec_params), M_TEMP, M_WAITOK | M_ZERO);
3761a305bdaSBen Widawsky 
3771a305bdaSBen Widawsky     buf.Pointer = NULL;
3781a305bdaSBen Widawsky     buf.Length = ACPI_ALLOCATE_BUFFER;
379f8335e3aSNate Lawson     h = acpi_get_handle(dev);
380f8335e3aSNate Lawson 
381f8335e3aSNate Lawson     /*
3821a305bdaSBen Widawsky      * Read the unit ID to check for duplicate attach and the global lock value
3831a305bdaSBen Widawsky      * to see if we should acquire it when accessing the EC.
384f8335e3aSNate Lawson      */
3850025fb0fSNate Lawson     status = acpi_GetInteger(h, "_UID", &params->uid);
386f8335e3aSNate Lawson     if (ACPI_FAILURE(status))
3870025fb0fSNate Lawson 	params->uid = 0;
3881a305bdaSBen Widawsky 
389ec60b7f9SBen Widawsky     /*
390ec60b7f9SBen Widawsky      * Check for a duplicate probe. This can happen when a probe via ECDT
391ec60b7f9SBen Widawsky      * succeeded already. If this is a duplicate, disable this device.
392ec60b7f9SBen Widawsky      *
393ec60b7f9SBen Widawsky      * NB: It would seem device_disable would be sufficient to not get
394ec60b7f9SBen Widawsky      * duplicated devices, and ENXIO isn't needed, however, device_probe() only
395ec60b7f9SBen Widawsky      * checks DF_ENABLED at the start and so disabling it here is too late to
396ec60b7f9SBen Widawsky      * prevent device_attach() from being called.
397ec60b7f9SBen Widawsky      */
398ec60b7f9SBen Widawsky     peer = devclass_get_device(acpi_ec_devclass, params->uid);
399ec60b7f9SBen Widawsky     if (peer != NULL && device_is_alive(peer)) {
400ec60b7f9SBen Widawsky 	device_disable(dev);
401ec60b7f9SBen Widawsky 	goto out;
402ec60b7f9SBen Widawsky     }
403ec60b7f9SBen Widawsky 
4040025fb0fSNate Lawson     status = acpi_GetInteger(h, "_GLK", &params->glk);
405f8335e3aSNate Lawson     if (ACPI_FAILURE(status))
4060025fb0fSNate Lawson 	params->glk = 0;
407f8335e3aSNate Lawson 
408f8335e3aSNate Lawson     /*
4091a305bdaSBen Widawsky      * Evaluate the _GPE method to find the GPE bit used by the EC to signal
4101a305bdaSBen Widawsky      * status (SCI).  If it's a package, it contains a reference and GPE bit,
4111a305bdaSBen Widawsky      * similar to _PRW.
412f8335e3aSNate Lawson      */
4130025fb0fSNate Lawson     status = AcpiEvaluateObject(h, "_GPE", NULL, &buf);
414f8335e3aSNate Lawson     if (ACPI_FAILURE(status)) {
4151a305bdaSBen Widawsky 	device_printf(dev, "can't evaluate _GPE - %s\n", AcpiFormatException(status));
416a6761eb3SNate Lawson 	goto out;
417f8335e3aSNate Lawson     }
4181a305bdaSBen Widawsky 
4190025fb0fSNate Lawson     obj = (ACPI_OBJECT *)buf.Pointer;
4200025fb0fSNate Lawson     if (obj == NULL)
421a6761eb3SNate Lawson 	goto out;
4220025fb0fSNate Lawson 
4230025fb0fSNate Lawson     switch (obj->Type) {
4240025fb0fSNate Lawson     case ACPI_TYPE_INTEGER:
4250025fb0fSNate Lawson 	params->gpe_handle = NULL;
4260025fb0fSNate Lawson 	params->gpe_bit = obj->Integer.Value;
4270025fb0fSNate Lawson 	break;
4280025fb0fSNate Lawson     case ACPI_TYPE_PACKAGE:
4290025fb0fSNate Lawson 	if (!ACPI_PKG_VALID(obj, 2))
4300025fb0fSNate Lawson 	    goto out;
4311a305bdaSBen Widawsky 	params->gpe_handle = acpi_GetReference(NULL, &obj->Package.Elements[0]);
4320025fb0fSNate Lawson 	if (params->gpe_handle == NULL ||
4330025fb0fSNate Lawson 	    acpi_PkgInt32(obj, 1, &params->gpe_bit) != 0)
4340025fb0fSNate Lawson 		goto out;
4350025fb0fSNate Lawson 	break;
4360025fb0fSNate Lawson     default:
4370025fb0fSNate Lawson 	device_printf(dev, "_GPE has invalid type %d\n", obj->Type);
4380025fb0fSNate Lawson 	goto out;
4390025fb0fSNate Lawson     }
440f8335e3aSNate Lawson 
441f8335e3aSNate Lawson     /* Store the values we got from the namespace for attach. */
4420025fb0fSNate Lawson     acpi_set_private(dev, params);
443f8335e3aSNate Lawson 
4441a305bdaSBen Widawsky     if (buf.Pointer)
4451a305bdaSBen Widawsky 	AcpiOsFree(buf.Pointer);
4460025fb0fSNate Lawson out:
4475efca36fSTakanori Watanabe     if (ret <= 0) {
448c868ac7dSNate Lawson 	snprintf(desc, sizeof(desc), "Embedded Controller: GPE %#x%s%s",
4490025fb0fSNate Lawson 		 params->gpe_bit, (params->glk) ? ", GLK" : "",
450f6eb382cSAndriy Gapon 		 ecdt ? ", ECDT" : "");
451c868ac7dSNate Lawson 	device_set_desc_copy(dev, desc);
4522f977ab4SJung-uk Kim     } else
4530025fb0fSNate Lawson 	free(params, M_TEMP);
4541a305bdaSBen Widawsky 
4551f04e8f5SNate Lawson     return (ret);
45615e32d5dSMike Smith }
45715e32d5dSMike Smith 
45815e32d5dSMike Smith static int
45915e32d5dSMike Smith acpi_ec_attach(device_t dev)
46015e32d5dSMike Smith {
46115e32d5dSMike Smith     struct acpi_ec_softc	*sc;
4620025fb0fSNate Lawson     struct acpi_ec_params	*params;
46315e32d5dSMike Smith     ACPI_STATUS			Status;
464dbd0058aSMike Smith 
465b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
4660ae55423SMike Smith 
4671f04e8f5SNate Lawson     /* Fetch/initialize softc (assumes softc is pre-zeroed). */
46815e32d5dSMike Smith     sc = device_get_softc(dev);
4690025fb0fSNate Lawson     params = acpi_get_private(dev);
47015e32d5dSMike Smith     sc->ec_dev = dev;
47115e32d5dSMike Smith     sc->ec_handle = acpi_get_handle(dev);
47215e32d5dSMike Smith 
473f8335e3aSNate Lawson     /* Retrieve previously probed values via device ivars. */
4740025fb0fSNate Lawson     sc->ec_glk = params->glk;
4750025fb0fSNate Lawson     sc->ec_gpebit = params->gpe_bit;
4760025fb0fSNate Lawson     sc->ec_gpehandle = params->gpe_handle;
4770025fb0fSNate Lawson     sc->ec_uid = params->uid;
47868fb6c48STakanori Watanabe     sc->ec_suspending = FALSE;
479943124d3SAndriy Gapon     acpi_set_private(dev, NULL);
4800025fb0fSNate Lawson     free(params, M_TEMP);
481f8335e3aSNate Lawson 
4821f04e8f5SNate Lawson     /* Attach bus resources for data and command/status ports. */
48315e32d5dSMike Smith     sc->ec_data_rid = 0;
4845f96beb9SNate Lawson     sc->ec_data_res = bus_alloc_resource_any(sc->ec_dev, SYS_RES_IOPORT,
4855f96beb9SNate Lawson 			&sc->ec_data_rid, RF_ACTIVE);
486464c662eSNate Lawson     if (sc->ec_data_res == NULL) {
48715e32d5dSMike Smith 	device_printf(dev, "can't allocate data port\n");
488f3fc4f8bSNate Lawson 	goto error;
48915e32d5dSMike Smith     }
49015e32d5dSMike Smith     sc->ec_data_tag = rman_get_bustag(sc->ec_data_res);
49115e32d5dSMike Smith     sc->ec_data_handle = rman_get_bushandle(sc->ec_data_res);
49215e32d5dSMike Smith 
49315e32d5dSMike Smith     sc->ec_csr_rid = 1;
4945f96beb9SNate Lawson     sc->ec_csr_res = bus_alloc_resource_any(sc->ec_dev, SYS_RES_IOPORT,
4955f96beb9SNate Lawson 			&sc->ec_csr_rid, RF_ACTIVE);
496464c662eSNate Lawson     if (sc->ec_csr_res == NULL) {
49715e32d5dSMike Smith 	device_printf(dev, "can't allocate command/status port\n");
498f3fc4f8bSNate Lawson 	goto error;
49915e32d5dSMike Smith     }
50015e32d5dSMike Smith     sc->ec_csr_tag = rman_get_bustag(sc->ec_csr_res);
50115e32d5dSMike Smith     sc->ec_csr_handle = rman_get_bushandle(sc->ec_csr_res);
50215e32d5dSMike Smith 
50315e32d5dSMike Smith     /*
5041f04e8f5SNate Lawson      * Install a handler for this EC's GPE bit.  We want edge-triggered
5051f04e8f5SNate Lawson      * behavior.
50615e32d5dSMike Smith      */
5071f04e8f5SNate Lawson     ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "attaching GPE handler\n"));
5080025fb0fSNate Lawson     Status = AcpiInstallGpeHandler(sc->ec_gpehandle, sc->ec_gpebit,
5095a77b11bSJung-uk Kim 		ACPI_GPE_EDGE_TRIGGERED, EcGpeHandler, sc);
510464c662eSNate Lawson     if (ACPI_FAILURE(Status)) {
51176d1dff4SMike Smith 	device_printf(dev, "can't install GPE handler for %s - %s\n",
512bfae45aaSMike Smith 		      acpi_name(sc->ec_handle), AcpiFormatException(Status));
513f3fc4f8bSNate Lawson 	goto error;
51415e32d5dSMike Smith     }
51515e32d5dSMike Smith 
51615e32d5dSMike Smith     /*
51715e32d5dSMike Smith      * Install address space handler
51815e32d5dSMike Smith      */
5194c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "attaching address space handler\n"));
520464c662eSNate Lawson     Status = AcpiInstallAddressSpaceHandler(sc->ec_handle, ACPI_ADR_SPACE_EC,
5211f04e8f5SNate Lawson 		&EcSpaceHandler, &EcSpaceSetup, sc);
522464c662eSNate Lawson     if (ACPI_FAILURE(Status)) {
52376d1dff4SMike Smith 	device_printf(dev, "can't install address space handler for %s - %s\n",
524bfae45aaSMike Smith 		      acpi_name(sc->ec_handle), AcpiFormatException(Status));
525f3fc4f8bSNate Lawson 	goto error;
526f3fc4f8bSNate Lawson     }
527f3fc4f8bSNate Lawson 
528f3fc4f8bSNate Lawson     /* Enable runtime GPEs for the handler. */
529a88e22b7SJung-uk Kim     Status = AcpiEnableGpe(sc->ec_gpehandle, sc->ec_gpebit);
530f3fc4f8bSNate Lawson     if (ACPI_FAILURE(Status)) {
531f3fc4f8bSNate Lawson 	device_printf(dev, "AcpiEnableGpe failed: %s\n",
532f3fc4f8bSNate Lawson 		      AcpiFormatException(Status));
533f3fc4f8bSNate Lawson 	goto error;
53415e32d5dSMike Smith     }
5351f04e8f5SNate Lawson 
5361f04e8f5SNate Lawson     ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "acpi_ec_attach complete\n"));
5371f04e8f5SNate Lawson     return (0);
538464c662eSNate Lawson 
539f3fc4f8bSNate Lawson error:
5405a77b11bSJung-uk Kim     AcpiRemoveGpeHandler(sc->ec_gpehandle, sc->ec_gpebit, EcGpeHandler);
541f3fc4f8bSNate Lawson     AcpiRemoveAddressSpaceHandler(sc->ec_handle, ACPI_ADR_SPACE_EC,
542f3fc4f8bSNate Lawson 	EcSpaceHandler);
543f8372adeSTakanori Watanabe     if (sc->ec_csr_res)
544f8372adeSTakanori Watanabe 	bus_release_resource(sc->ec_dev, SYS_RES_IOPORT, sc->ec_csr_rid,
545f8372adeSTakanori Watanabe 			     sc->ec_csr_res);
546f8372adeSTakanori Watanabe     if (sc->ec_data_res)
547f8372adeSTakanori Watanabe 	bus_release_resource(sc->ec_dev, SYS_RES_IOPORT, sc->ec_data_rid,
548f8372adeSTakanori Watanabe 			     sc->ec_data_res);
549f3fc4f8bSNate Lawson     return (ENXIO);
5501f04e8f5SNate Lawson }
5511f04e8f5SNate Lawson 
552340a7f6aSNate Lawson static int
5530bfeadedSTakanori Watanabe acpi_ec_suspend(device_t dev)
5540bfeadedSTakanori Watanabe {
5550bfeadedSTakanori Watanabe     struct acpi_ec_softc	*sc;
5560bfeadedSTakanori Watanabe 
5570bfeadedSTakanori Watanabe     sc = device_get_softc(dev);
55868fb6c48STakanori Watanabe     sc->ec_suspending = TRUE;
5590bfeadedSTakanori Watanabe     return (0);
5600bfeadedSTakanori Watanabe }
5610bfeadedSTakanori Watanabe 
5620bfeadedSTakanori Watanabe static int
5630bfeadedSTakanori Watanabe acpi_ec_resume(device_t dev)
5640bfeadedSTakanori Watanabe {
5650bfeadedSTakanori Watanabe     struct acpi_ec_softc	*sc;
5660bfeadedSTakanori Watanabe 
5670bfeadedSTakanori Watanabe     sc = device_get_softc(dev);
56868fb6c48STakanori Watanabe     sc->ec_suspending = FALSE;
5690bfeadedSTakanori Watanabe     return (0);
5700bfeadedSTakanori Watanabe }
5710bfeadedSTakanori Watanabe 
5720bfeadedSTakanori Watanabe static int
573340a7f6aSNate Lawson acpi_ec_shutdown(device_t dev)
574340a7f6aSNate Lawson {
575340a7f6aSNate Lawson     struct acpi_ec_softc	*sc;
576340a7f6aSNate Lawson 
577340a7f6aSNate Lawson     /* Disable the GPE so we don't get EC events during shutdown. */
578340a7f6aSNate Lawson     sc = device_get_softc(dev);
579a88e22b7SJung-uk Kim     AcpiDisableGpe(sc->ec_gpehandle, sc->ec_gpebit);
580340a7f6aSNate Lawson     return (0);
581340a7f6aSNate Lawson }
582340a7f6aSNate Lawson 
583e33bea8dSNate Lawson /* Methods to allow other devices (e.g., smbat) to read/write EC space. */
584e33bea8dSNate Lawson static int
5859a179dd8SJung-uk Kim acpi_ec_read_method(device_t dev, u_int addr, UINT64 *val, int width)
586e33bea8dSNate Lawson {
587e33bea8dSNate Lawson     struct acpi_ec_softc *sc;
588e33bea8dSNate Lawson     ACPI_STATUS status;
589e33bea8dSNate Lawson 
590e33bea8dSNate Lawson     sc = device_get_softc(dev);
591e33bea8dSNate Lawson     status = EcSpaceHandler(ACPI_READ, addr, width * 8, val, sc, NULL);
592e33bea8dSNate Lawson     if (ACPI_FAILURE(status))
593e33bea8dSNate Lawson 	return (ENXIO);
594e33bea8dSNate Lawson     return (0);
595e33bea8dSNate Lawson }
596e33bea8dSNate Lawson 
597e33bea8dSNate Lawson static int
5989a179dd8SJung-uk Kim acpi_ec_write_method(device_t dev, u_int addr, UINT64 val, int width)
599e33bea8dSNate Lawson {
600e33bea8dSNate Lawson     struct acpi_ec_softc *sc;
601e33bea8dSNate Lawson     ACPI_STATUS status;
602e33bea8dSNate Lawson 
603e33bea8dSNate Lawson     sc = device_get_softc(dev);
604e33bea8dSNate Lawson     status = EcSpaceHandler(ACPI_WRITE, addr, width * 8, &val, sc, NULL);
605e33bea8dSNate Lawson     if (ACPI_FAILURE(status))
606e33bea8dSNate Lawson 	return (ENXIO);
607e33bea8dSNate Lawson     return (0);
608e33bea8dSNate Lawson }
609e33bea8dSNate Lawson 
6105ff14fa9SAndriy Gapon static ACPI_STATUS
6115ff14fa9SAndriy Gapon EcCheckStatus(struct acpi_ec_softc *sc, const char *msg, EC_EVENT event)
6125ff14fa9SAndriy Gapon {
6135ff14fa9SAndriy Gapon     ACPI_STATUS status;
6145ff14fa9SAndriy Gapon     EC_STATUS ec_status;
6155ff14fa9SAndriy Gapon 
6165ff14fa9SAndriy Gapon     status = AE_NO_HARDWARE_RESPONSE;
6175ff14fa9SAndriy Gapon     ec_status = EC_GET_CSR(sc);
6185ff14fa9SAndriy Gapon     if (sc->ec_burstactive && !(ec_status & EC_FLAG_BURST_MODE)) {
6195ff14fa9SAndriy Gapon 	CTR1(KTR_ACPI, "ec burst disabled in waitevent (%s)", msg);
6205ff14fa9SAndriy Gapon 	sc->ec_burstactive = FALSE;
6215ff14fa9SAndriy Gapon     }
6225ff14fa9SAndriy Gapon     if (EVENT_READY(event, ec_status)) {
6235ff14fa9SAndriy Gapon 	CTR2(KTR_ACPI, "ec %s wait ready, status %#x", msg, ec_status);
6245ff14fa9SAndriy Gapon 	status = AE_OK;
6255ff14fa9SAndriy Gapon     }
6265ff14fa9SAndriy Gapon     return (status);
6275ff14fa9SAndriy Gapon }
6285ff14fa9SAndriy Gapon 
62915e32d5dSMike Smith static void
630c6e6b4feSHans Petter Selasky EcGpeQueryHandlerSub(struct acpi_ec_softc *sc)
63115e32d5dSMike Smith {
63215e32d5dSMike Smith     UINT8			Data;
63315e32d5dSMike Smith     ACPI_STATUS			Status;
6344552fccbSJung-uk Kim     int				retry;
63515e32d5dSMike Smith     char			qxx[5];
63615e32d5dSMike Smith 
637b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
63815e32d5dSMike Smith 
639ef2374f7SNate Lawson     /* Serialize user access with EcSpaceHandler(). */
64083dcc133SNate Lawson     Status = EcLock(sc);
6413a371f32SNate Lawson     if (ACPI_FAILURE(Status)) {
64283dcc133SNate Lawson 	device_printf(sc->ec_dev, "GpeQuery lock error: %s\n",
64383dcc133SNate Lawson 	    AcpiFormatException(Status));
6443a371f32SNate Lawson 	return;
6453a371f32SNate Lawson     }
6463a371f32SNate Lawson 
64715e32d5dSMike Smith     /*
6483a371f32SNate Lawson      * Send a query command to the EC to find out which _Qxx call it
6493a371f32SNate Lawson      * wants to make.  This command clears the SCI bit and also the
65083dcc133SNate Lawson      * interrupt source since we are edge-triggered.  To prevent the GPE
65183dcc133SNate Lawson      * that may arise from running the query from causing another query
65283dcc133SNate Lawson      * to be queued, we clear the pending flag only after running it.
6533a371f32SNate Lawson      */
6545ff14fa9SAndriy Gapon     for (retry = 0; retry < 2; retry++) {
6553a371f32SNate Lawson 	Status = EcCommand(sc, EC_COMMAND_QUERY);
6565ff14fa9SAndriy Gapon 	if (ACPI_SUCCESS(Status))
6575ff14fa9SAndriy Gapon 	    break;
658ae1ef64fSJung-uk Kim 	if (ACPI_FAILURE(EcCheckStatus(sc, "retr_check",
6599ad56977SJung-uk Kim 	    EC_EVENT_INPUT_BUFFER_EMPTY)))
6605ff14fa9SAndriy Gapon 	    break;
6615ff14fa9SAndriy Gapon     }
662dbd0058aSMike Smith     if (ACPI_FAILURE(Status)) {
6633a371f32SNate Lawson 	EcUnlock(sc);
66483dcc133SNate Lawson 	device_printf(sc->ec_dev, "GPE query failed: %s\n",
66583dcc133SNate Lawson 	    AcpiFormatException(Status));
66683dcc133SNate Lawson 	return;
66715e32d5dSMike Smith     }
6683a371f32SNate Lawson     Data = EC_GET_DATA(sc);
669ef2374f7SNate Lawson 
67083dcc133SNate Lawson     /*
67183dcc133SNate Lawson      * We have to unlock before running the _Qxx method below since that
67283dcc133SNate Lawson      * method may attempt to read/write from EC address space, causing
67383dcc133SNate Lawson      * recursive acquisition of the lock.
67483dcc133SNate Lawson      */
6753a371f32SNate Lawson     EcUnlock(sc);
67615e32d5dSMike Smith 
6771f04e8f5SNate Lawson     /* Ignore the value for "no outstanding event". (13.3.5) */
67883dcc133SNate Lawson     CTR2(KTR_ACPI, "ec query ok,%s running _Q%02X", Data ? "" : " not", Data);
6791f04e8f5SNate Lawson     if (Data == 0)
68083dcc133SNate Lawson 	return;
6811f04e8f5SNate Lawson 
6821f04e8f5SNate Lawson     /* Evaluate _Qxx to respond to the controller. */
68383dcc133SNate Lawson     snprintf(qxx, sizeof(qxx), "_Q%02X", Data);
684b3919c8dSMark Santcroos     AcpiUtStrupr(qxx);
685ee785aa9SJohn Baldwin     Status = AcpiEvaluateObject(sc->ec_handle, qxx, NULL, NULL);
6861f04e8f5SNate Lawson     if (ACPI_FAILURE(Status) && Status != AE_NOT_FOUND) {
68783dcc133SNate Lawson 	device_printf(sc->ec_dev, "evaluation of query method %s failed: %s\n",
688bfae45aaSMike Smith 	    qxx, AcpiFormatException(Status));
68915e32d5dSMike Smith     }
690c07572e7STakanori Watanabe }
69142f6d122SMike Smith 
692c6e6b4feSHans Petter Selasky static void
693c6e6b4feSHans Petter Selasky EcGpeQueryHandler(void *Context)
694c6e6b4feSHans Petter Selasky {
695c6e6b4feSHans Petter Selasky     struct acpi_ec_softc *sc = (struct acpi_ec_softc *)Context;
696c6e6b4feSHans Petter Selasky     int pending;
697c6e6b4feSHans Petter Selasky 
698c6e6b4feSHans Petter Selasky     KASSERT(Context != NULL, ("EcGpeQueryHandler called with NULL"));
699c6e6b4feSHans Petter Selasky 
700c6e6b4feSHans Petter Selasky     do {
701c6e6b4feSHans Petter Selasky 	/* Read the current pending count */
702c6e6b4feSHans Petter Selasky 	pending = atomic_load_acq_int(&sc->ec_sci_pend);
703c6e6b4feSHans Petter Selasky 
704c6e6b4feSHans Petter Selasky 	/* Call GPE handler function */
705c6e6b4feSHans Petter Selasky 	EcGpeQueryHandlerSub(sc);
706c6e6b4feSHans Petter Selasky 
707c6e6b4feSHans Petter Selasky 	/*
708c6e6b4feSHans Petter Selasky 	 * Try to reset the pending count to zero. If this fails we
709c6e6b4feSHans Petter Selasky 	 * know another GPE event has occurred while handling the
710c6e6b4feSHans Petter Selasky 	 * current GPE event and need to loop.
711c6e6b4feSHans Petter Selasky 	 */
712c6e6b4feSHans Petter Selasky     } while (!atomic_cmpset_int(&sc->ec_sci_pend, pending, 0));
713c6e6b4feSHans Petter Selasky }
714c6e6b4feSHans Petter Selasky 
715a9cf0dffSMike Smith /*
71683dcc133SNate Lawson  * The GPE handler is called when IBE/OBF or SCI events occur.  We are
71783dcc133SNate Lawson  * called from an unknown lock context.
718a9cf0dffSMike Smith  */
7195a77b11bSJung-uk Kim static UINT32
7205a77b11bSJung-uk Kim EcGpeHandler(ACPI_HANDLE GpeDevice, UINT32 GpeNumber, void *Context)
721c07572e7STakanori Watanabe {
722c07572e7STakanori Watanabe     struct acpi_ec_softc *sc = Context;
7231f04e8f5SNate Lawson     ACPI_STATUS		       Status;
724ef2374f7SNate Lawson     EC_STATUS		       EcStatus;
725a9cf0dffSMike Smith 
7261f04e8f5SNate Lawson     KASSERT(Context != NULL, ("EcGpeHandler called with NULL"));
72783dcc133SNate Lawson     CTR0(KTR_ACPI, "ec gpe handler start");
7281f04e8f5SNate Lawson 
7291395b555SNate Lawson     /*
73083dcc133SNate Lawson      * Notify EcWaitEvent() that the status register is now fresh.  If we
73183dcc133SNate Lawson      * didn't do this, it wouldn't be possible to distinguish an old IBE
73283dcc133SNate Lawson      * from a new one, for example when doing a write transaction (writing
73383dcc133SNate Lawson      * address and then data values.)
7341395b555SNate Lawson      */
73583dcc133SNate Lawson     atomic_add_int(&sc->ec_gencount, 1);
73680b1151eSJung-uk Kim     wakeup(sc);
737ef2374f7SNate Lawson 
738ef2374f7SNate Lawson     /*
73983dcc133SNate Lawson      * If the EC_SCI bit of the status register is set, queue a query handler.
74083dcc133SNate Lawson      * It will run the query and _Qxx method later, under the lock.
741ef2374f7SNate Lawson      */
742ef2374f7SNate Lawson     EcStatus = EC_GET_CSR(sc);
743c6e6b4feSHans Petter Selasky     if ((EcStatus & EC_EVENT_SCI) &&
744c6e6b4feSHans Petter Selasky 	atomic_fetchadd_int(&sc->ec_sci_pend, 1) == 0) {
74583dcc133SNate Lawson 	CTR0(KTR_ACPI, "ec gpe queueing query handler");
7462be4e471SJung-uk Kim 	Status = AcpiOsExecute(OSL_GPE_HANDLER, EcGpeQueryHandler, Context);
747c6e6b4feSHans Petter Selasky 	if (ACPI_FAILURE(Status)) {
74883dcc133SNate Lawson 	    printf("EcGpeHandler: queuing GPE query handler failed\n");
749c6e6b4feSHans Petter Selasky 	    atomic_store_rel_int(&sc->ec_sci_pend, 0);
750c6e6b4feSHans Petter Selasky 	}
7516e141df2SNate Lawson     }
7525a77b11bSJung-uk Kim     return (ACPI_REENABLE_GPE);
75315e32d5dSMike Smith }
75415e32d5dSMike Smith 
75515e32d5dSMike Smith static ACPI_STATUS
756464c662eSNate Lawson EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function, void *Context,
757464c662eSNate Lawson 	     void **RegionContext)
75815e32d5dSMike Smith {
75942f6d122SMike Smith 
760b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
76142f6d122SMike Smith 
76215e32d5dSMike Smith     /*
76316844a97SNate Lawson      * If deactivating a region, always set the output to NULL.  Otherwise,
76416844a97SNate Lawson      * just pass the context through.
76515e32d5dSMike Smith      */
76616844a97SNate Lawson     if (Function == ACPI_REGION_DEACTIVATE)
76716844a97SNate Lawson 	*RegionContext = NULL;
76816844a97SNate Lawson     else
76915e32d5dSMike Smith 	*RegionContext = Context;
77015e32d5dSMike Smith 
77142f6d122SMike Smith     return_ACPI_STATUS (AE_OK);
77215e32d5dSMike Smith }
77315e32d5dSMike Smith 
77415e32d5dSMike Smith static ACPI_STATUS
775e115a49fSJung-uk Kim EcSpaceHandler(UINT32 Function, ACPI_PHYSICAL_ADDRESS Address, UINT32 Width,
7769a179dd8SJung-uk Kim 	       UINT64 *Value, void *Context, void *RegionContext)
77715e32d5dSMike Smith {
77815e32d5dSMike Smith     struct acpi_ec_softc	*sc = (struct acpi_ec_softc *)Context;
77934622ff1SJung-uk Kim     ACPI_PHYSICAL_ADDRESS	EcAddr;
780e115a49fSJung-uk Kim     UINT8			*EcData;
78134622ff1SJung-uk Kim     ACPI_STATUS			Status;
78215e32d5dSMike Smith 
783b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, (UINT32)Address);
7840ae55423SMike Smith 
78534622ff1SJung-uk Kim     if (Function != ACPI_READ && Function != ACPI_WRITE)
78634622ff1SJung-uk Kim 	return_ACPI_STATUS (AE_BAD_PARAMETER);
787e115a49fSJung-uk Kim     if (Width % 8 != 0 || Value == NULL || Context == NULL)
7880ae55423SMike Smith 	return_ACPI_STATUS (AE_BAD_PARAMETER);
78934622ff1SJung-uk Kim     if (Address + Width / 8 > 256)
7904ed391b8SNate Lawson 	return_ACPI_STATUS (AE_BAD_ADDRESS);
79115e32d5dSMike Smith 
79283dcc133SNate Lawson     /*
79383dcc133SNate Lawson      * If booting, check if we need to run the query handler.  If so, we
79483dcc133SNate Lawson      * we call it directly here since our thread taskq is not active yet.
79583dcc133SNate Lawson      */
796c66d2b38SJung-uk Kim     if (cold || rebooting || sc->ec_suspending) {
797c6e6b4feSHans Petter Selasky 	if ((EC_GET_CSR(sc) & EC_EVENT_SCI) &&
798c6e6b4feSHans Petter Selasky 	    atomic_fetchadd_int(&sc->ec_sci_pend, 1) == 0) {
79983dcc133SNate Lawson 	    CTR0(KTR_ACPI, "ec running gpe handler directly");
80083dcc133SNate Lawson 	    EcGpeQueryHandler(sc);
80183dcc133SNate Lawson 	}
80283dcc133SNate Lawson     }
80383dcc133SNate Lawson 
80483dcc133SNate Lawson     /* Serialize with EcGpeQueryHandler() at transaction granularity. */
80583dcc133SNate Lawson     Status = EcLock(sc);
806464c662eSNate Lawson     if (ACPI_FAILURE(Status))
807f4b7de15SNate Lawson 	return_ACPI_STATUS (Status);
8081f04e8f5SNate Lawson 
80934622ff1SJung-uk Kim     /* If we can't start burst mode, continue anyway. */
81034622ff1SJung-uk Kim     Status = EcCommand(sc, EC_COMMAND_BURST_ENABLE);
81134622ff1SJung-uk Kim     if (ACPI_SUCCESS(Status)) {
81234622ff1SJung-uk Kim 	if (EC_GET_DATA(sc) == EC_BURST_ACK) {
81334622ff1SJung-uk Kim 	    CTR0(KTR_ACPI, "ec burst enabled");
81434622ff1SJung-uk Kim 	    sc->ec_burstactive = TRUE;
81534622ff1SJung-uk Kim 	}
81634622ff1SJung-uk Kim     }
81734622ff1SJung-uk Kim 
818e115a49fSJung-uk Kim     /* Perform the transaction(s), based on Width. */
81934622ff1SJung-uk Kim     EcAddr = Address;
82034622ff1SJung-uk Kim     EcData = (UINT8 *)Value;
82134622ff1SJung-uk Kim     if (Function == ACPI_READ)
82234622ff1SJung-uk Kim 	*Value = 0;
82334622ff1SJung-uk Kim     do {
8241f04e8f5SNate Lawson 	switch (Function) {
8251f04e8f5SNate Lawson 	case ACPI_READ:
826e115a49fSJung-uk Kim 	    Status = EcRead(sc, EcAddr, EcData);
827ee785aa9SJohn Baldwin 	    break;
8281f04e8f5SNate Lawson 	case ACPI_WRITE:
829e115a49fSJung-uk Kim 	    Status = EcWrite(sc, EcAddr, *EcData);
8301f04e8f5SNate Lawson 	    break;
8311f04e8f5SNate Lawson 	}
8321f04e8f5SNate Lawson 	if (ACPI_FAILURE(Status))
8334ed391b8SNate Lawson 	    break;
83434622ff1SJung-uk Kim 	EcAddr++;
83534622ff1SJung-uk Kim 	EcData++;
83634622ff1SJung-uk Kim     } while (EcAddr < Address + Width / 8);
83734622ff1SJung-uk Kim 
83834622ff1SJung-uk Kim     if (sc->ec_burstactive) {
83934622ff1SJung-uk Kim 	sc->ec_burstactive = FALSE;
84034622ff1SJung-uk Kim 	if (ACPI_SUCCESS(EcCommand(sc, EC_COMMAND_BURST_DISABLE)))
84134622ff1SJung-uk Kim 	    CTR0(KTR_ACPI, "ec disabled burst ok");
842ee785aa9SJohn Baldwin     }
8434ed391b8SNate Lawson 
844f4b7de15SNate Lawson     EcUnlock(sc);
8450ae55423SMike Smith     return_ACPI_STATUS (Status);
84615e32d5dSMike Smith }
8472a4ac806SMike Smith 
84815e32d5dSMike Smith static ACPI_STATUS
84983dcc133SNate Lawson EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event, u_int gen_count)
85083dcc133SNate Lawson {
8515ff14fa9SAndriy Gapon     static int	no_intr = 0;
8521f04e8f5SNate Lawson     ACPI_STATUS	Status;
8535ff14fa9SAndriy Gapon     int		count, i, need_poll, slp_ival;
85415e32d5dSMike Smith 
855f4b7de15SNate Lawson     ACPI_SERIAL_ASSERT(ec);
8561f04e8f5SNate Lawson     Status = AE_NO_HARDWARE_RESPONSE;
8575ff14fa9SAndriy Gapon     need_poll = cold || rebooting || ec_polled_mode || sc->ec_suspending;
85883dcc133SNate Lawson 
85983dcc133SNate Lawson     /* Wait for event by polling or GPE (interrupt). */
8601de5ce99STakanori Watanabe     if (need_poll) {
86183dcc133SNate Lawson 	count = (ec_timeout * 1000) / EC_POLL_DELAY;
86283dcc133SNate Lawson 	if (count == 0)
863ef2374f7SNate Lawson 	    count = 1;
8645ff14fa9SAndriy Gapon 	DELAY(10);
865ef2374f7SNate Lawson 	for (i = 0; i < count; i++) {
86683dcc133SNate Lawson 	    Status = EcCheckStatus(sc, "poll", Event);
8679ad56977SJung-uk Kim 	    if (ACPI_SUCCESS(Status))
86815e32d5dSMike Smith 		break;
8695ff14fa9SAndriy Gapon 	    DELAY(EC_POLL_DELAY);
8701f04e8f5SNate Lawson 	}
87183dcc133SNate Lawson     } else {
872ef2374f7SNate Lawson 	slp_ival = hz / 1000;
873ef2374f7SNate Lawson 	if (slp_ival != 0) {
87483dcc133SNate Lawson 	    count = ec_timeout;
875ef2374f7SNate Lawson 	} else {
87683dcc133SNate Lawson 	    /* hz has less than 1 ms resolution so scale timeout. */
877b0eefa38SNate Lawson 	    slp_ival = 1;
878ef2374f7SNate Lawson 	    count = ec_timeout / (1000 / hz);
879ef2374f7SNate Lawson 	}
88083dcc133SNate Lawson 
88183dcc133SNate Lawson 	/*
88283dcc133SNate Lawson 	 * Wait for the GPE to signal the status changed, checking the
88383dcc133SNate Lawson 	 * status register each time we get one.  It's possible to get a
88483dcc133SNate Lawson 	 * GPE for an event we're not interested in here (i.e., SCI for
88583dcc133SNate Lawson 	 * EC query).
88683dcc133SNate Lawson 	 */
887b0eefa38SNate Lawson 	for (i = 0; i < count; i++) {
8885ff14fa9SAndriy Gapon 	    if (gen_count == sc->ec_gencount)
88980b1151eSJung-uk Kim 		tsleep(sc, 0, "ecgpe", slp_ival);
89083dcc133SNate Lawson 	    /*
89183dcc133SNate Lawson 	     * Record new generation count.  It's possible the GPE was
89283dcc133SNate Lawson 	     * just to notify us that a query is needed and we need to
89383dcc133SNate Lawson 	     * wait for a second GPE to signal the completion of the
89483dcc133SNate Lawson 	     * event we are actually waiting for.
89583dcc133SNate Lawson 	     */
89683dcc133SNate Lawson 	    Status = EcCheckStatus(sc, "sleep", Event);
8979ad56977SJung-uk Kim 	    if (ACPI_SUCCESS(Status)) {
8985ff14fa9SAndriy Gapon 		if (gen_count == sc->ec_gencount)
8995ff14fa9SAndriy Gapon 		    no_intr++;
9005ff14fa9SAndriy Gapon 		else
9015ff14fa9SAndriy Gapon 		    no_intr = 0;
902ff40920eSNate Lawson 		break;
903ff40920eSNate Lawson 	    }
9045ff14fa9SAndriy Gapon 	    gen_count = sc->ec_gencount;
9051f04e8f5SNate Lawson 	}
9061f04e8f5SNate Lawson 
90783dcc133SNate Lawson 	/*
90883dcc133SNate Lawson 	 * We finished waiting for the GPE and it never arrived.  Try to
90983dcc133SNate Lawson 	 * read the register once and trust whatever value we got.  This is
9105ff14fa9SAndriy Gapon 	 * the best we can do at this point.
91183dcc133SNate Lawson 	 */
9129ad56977SJung-uk Kim 	if (ACPI_FAILURE(Status))
91383dcc133SNate Lawson 	    Status = EcCheckStatus(sc, "sleep_end", Event);
91483dcc133SNate Lawson     }
9155ff14fa9SAndriy Gapon     if (!need_poll && no_intr > 10) {
9165ff14fa9SAndriy Gapon 	device_printf(sc->ec_dev,
9175ff14fa9SAndriy Gapon 	    "not getting interrupts, switched to polled mode\n");
9185ff14fa9SAndriy Gapon 	ec_polled_mode = 1;
91983dcc133SNate Lawson     }
9209ad56977SJung-uk Kim     if (ACPI_FAILURE(Status))
92183dcc133SNate Lawson 	    CTR0(KTR_ACPI, "error: ec wait timed out");
9221f04e8f5SNate Lawson     return (Status);
9231f04e8f5SNate Lawson }
9241f04e8f5SNate Lawson 
9251f04e8f5SNate Lawson static ACPI_STATUS
9261f04e8f5SNate Lawson EcCommand(struct acpi_ec_softc *sc, EC_COMMAND cmd)
9271f04e8f5SNate Lawson {
928ef2374f7SNate Lawson     ACPI_STATUS	status;
929ef2374f7SNate Lawson     EC_EVENT	event;
930ef2374f7SNate Lawson     EC_STATUS	ec_status;
93183dcc133SNate Lawson     u_int	gen_count;
9321f04e8f5SNate Lawson 
933f4b7de15SNate Lawson     ACPI_SERIAL_ASSERT(ec);
9341f04e8f5SNate Lawson 
935ef2374f7SNate Lawson     /* Don't use burst mode if user disabled it. */
936ef2374f7SNate Lawson     if (!ec_burst_mode && cmd == EC_COMMAND_BURST_ENABLE)
937ef2374f7SNate Lawson 	return (AE_ERROR);
938ef2374f7SNate Lawson 
9391f04e8f5SNate Lawson     /* Decide what to wait for based on command type. */
9401f04e8f5SNate Lawson     switch (cmd) {
9411f04e8f5SNate Lawson     case EC_COMMAND_READ:
94215e32d5dSMike Smith     case EC_COMMAND_WRITE:
9431f04e8f5SNate Lawson     case EC_COMMAND_BURST_DISABLE:
944ef2374f7SNate Lawson 	event = EC_EVENT_INPUT_BUFFER_EMPTY;
9451f04e8f5SNate Lawson 	break;
9461f04e8f5SNate Lawson     case EC_COMMAND_QUERY:
9471f04e8f5SNate Lawson     case EC_COMMAND_BURST_ENABLE:
948ef2374f7SNate Lawson 	event = EC_EVENT_OUTPUT_BUFFER_FULL;
94915e32d5dSMike Smith 	break;
95015e32d5dSMike Smith     default:
95183dcc133SNate Lawson 	device_printf(sc->ec_dev, "EcCommand: invalid command %#x\n", cmd);
9521f04e8f5SNate Lawson 	return (AE_BAD_PARAMETER);
953464c662eSNate Lawson     }
9541f04e8f5SNate Lawson 
9555ff14fa9SAndriy Gapon     /*
9565ff14fa9SAndriy Gapon      * Ensure empty input buffer before issuing command.
9575ff14fa9SAndriy Gapon      * Use generation count of zero to force a quick check.
9585ff14fa9SAndriy Gapon      */
9595ff14fa9SAndriy Gapon     status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY, 0);
9605ff14fa9SAndriy Gapon     if (ACPI_FAILURE(status))
9615ff14fa9SAndriy Gapon 	return (status);
9625ff14fa9SAndriy Gapon 
9631f04e8f5SNate Lawson     /* Run the command and wait for the chosen event. */
964ef2374f7SNate Lawson     CTR1(KTR_ACPI, "ec running command %#x", cmd);
96583dcc133SNate Lawson     gen_count = sc->ec_gencount;
9661f04e8f5SNate Lawson     EC_SET_CSR(sc, cmd);
96783dcc133SNate Lawson     status = EcWaitEvent(sc, event, gen_count);
968ef2374f7SNate Lawson     if (ACPI_SUCCESS(status)) {
969ef2374f7SNate Lawson 	/* If we succeeded, burst flag should now be present. */
970ef2374f7SNate Lawson 	if (cmd == EC_COMMAND_BURST_ENABLE) {
971ef2374f7SNate Lawson 	    ec_status = EC_GET_CSR(sc);
972ef2374f7SNate Lawson 	    if ((ec_status & EC_FLAG_BURST_MODE) == 0)
973ef2374f7SNate Lawson 		status = AE_ERROR;
974ef2374f7SNate Lawson 	}
97583dcc133SNate Lawson     } else
97683dcc133SNate Lawson 	device_printf(sc->ec_dev, "EcCommand: no response to %#x\n", cmd);
977ef2374f7SNate Lawson     return (status);
97815e32d5dSMike Smith }
97915e32d5dSMike Smith 
98015e32d5dSMike Smith static ACPI_STATUS
98115e32d5dSMike Smith EcRead(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data)
98215e32d5dSMike Smith {
983ef2374f7SNate Lawson     ACPI_STATUS	status;
98483dcc133SNate Lawson     u_int gen_count;
9855ff14fa9SAndriy Gapon     int retry;
98615e32d5dSMike Smith 
987f4b7de15SNate Lawson     ACPI_SERIAL_ASSERT(ec);
988c4a9fa45SNate Lawson     CTR1(KTR_ACPI, "ec read from %#x", Address);
98915e32d5dSMike Smith 
9905ff14fa9SAndriy Gapon     for (retry = 0; retry < 2; retry++) {
991ef2374f7SNate Lawson 	status = EcCommand(sc, EC_COMMAND_READ);
992ef2374f7SNate Lawson 	if (ACPI_FAILURE(status))
993ef2374f7SNate Lawson 	    return (status);
99415e32d5dSMike Smith 
99583dcc133SNate Lawson 	gen_count = sc->ec_gencount;
99615e32d5dSMike Smith 	EC_SET_DATA(sc, Address);
99783dcc133SNate Lawson 	status = EcWaitEvent(sc, EC_EVENT_OUTPUT_BUFFER_FULL, gen_count);
998ae1ef64fSJung-uk Kim 	if (ACPI_SUCCESS(status)) {
999464c662eSNate Lawson 	    *Data = EC_GET_DATA(sc);
100015e32d5dSMike Smith 	    return (AE_OK);
100115e32d5dSMike Smith 	}
1002ae1ef64fSJung-uk Kim 	if (ACPI_FAILURE(EcCheckStatus(sc, "retr_check",
1003ae1ef64fSJung-uk Kim 	    EC_EVENT_INPUT_BUFFER_EMPTY)))
1004ae1ef64fSJung-uk Kim 	    break;
1005ae1ef64fSJung-uk Kim     }
10065ff14fa9SAndriy Gapon     device_printf(sc->ec_dev, "EcRead: failed waiting to get data\n");
10075ff14fa9SAndriy Gapon     return (status);
10085ff14fa9SAndriy Gapon }
100915e32d5dSMike Smith 
101015e32d5dSMike Smith static ACPI_STATUS
1011e115a49fSJung-uk Kim EcWrite(struct acpi_ec_softc *sc, UINT8 Address, UINT8 Data)
101215e32d5dSMike Smith {
1013ef2374f7SNate Lawson     ACPI_STATUS	status;
101483dcc133SNate Lawson     u_int gen_count;
101515e32d5dSMike Smith 
1016f4b7de15SNate Lawson     ACPI_SERIAL_ASSERT(ec);
1017e115a49fSJung-uk Kim     CTR2(KTR_ACPI, "ec write to %#x, data %#x", Address, Data);
101815e32d5dSMike Smith 
1019ef2374f7SNate Lawson     status = EcCommand(sc, EC_COMMAND_WRITE);
1020ef2374f7SNate Lawson     if (ACPI_FAILURE(status))
1021ef2374f7SNate Lawson 	return (status);
102215e32d5dSMike Smith 
102383dcc133SNate Lawson     gen_count = sc->ec_gencount;
102415e32d5dSMike Smith     EC_SET_DATA(sc, Address);
102583dcc133SNate Lawson     status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY, gen_count);
1026ef2374f7SNate Lawson     if (ACPI_FAILURE(status)) {
10275ff14fa9SAndriy Gapon 	device_printf(sc->ec_dev, "EcWrite: failed waiting for sent address\n");
1028ef2374f7SNate Lawson 	return (status);
102915e32d5dSMike Smith     }
103015e32d5dSMike Smith 
103183dcc133SNate Lawson     gen_count = sc->ec_gencount;
1032e115a49fSJung-uk Kim     EC_SET_DATA(sc, Data);
103383dcc133SNate Lawson     status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY, gen_count);
1034ef2374f7SNate Lawson     if (ACPI_FAILURE(status)) {
103583dcc133SNate Lawson 	device_printf(sc->ec_dev, "EcWrite: failed waiting for sent data\n");
1036ef2374f7SNate Lawson 	return (status);
103715e32d5dSMike Smith     }
103815e32d5dSMike Smith 
103915e32d5dSMike Smith     return (AE_OK);
104015e32d5dSMike Smith }
1041