xref: /freebsd/sys/dev/acpica/acpi_ec.c (revision daec92844e8fef4d0a629c70443043691e59530c)
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>
35*daec9284SConrad 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 
1826472ac3dSEd Schouten static SYSCTL_NODE(_debug_acpi, OID_AUTO, ec, CTLFLAG_RD, NULL, "EC debugging");
183ef2374f7SNate Lawson 
184675e5627SNate Lawson static int	ec_burst_mode;
185af3b2549SHans Petter Selasky SYSCTL_INT(_debug_acpi_ec, OID_AUTO, burst, CTLFLAG_RWTUN, &ec_burst_mode, 0,
186ef2374f7SNate Lawson     "Enable use of burst mode (faster for nearly all systems)");
18783dcc133SNate Lawson static int	ec_polled_mode;
188af3b2549SHans Petter Selasky SYSCTL_INT(_debug_acpi_ec, OID_AUTO, polled, CTLFLAG_RWTUN, &ec_polled_mode, 0,
18983dcc133SNate Lawson     "Force use of polled mode (only if interrupt mode doesn't work)");
190ef2374f7SNate Lawson static int	ec_timeout = EC_TIMEOUT;
191af3b2549SHans Petter Selasky SYSCTL_INT(_debug_acpi_ec, OID_AUTO, timeout, CTLFLAG_RWTUN, &ec_timeout,
192ef2374f7SNate Lawson     EC_TIMEOUT, "Total time spent waiting for a response (poll+sleep)");
193ef2374f7SNate Lawson 
19483dcc133SNate Lawson static ACPI_STATUS
19583dcc133SNate Lawson EcLock(struct acpi_ec_softc *sc)
19615e32d5dSMike Smith {
197f4b7de15SNate Lawson     ACPI_STATUS	status;
19815e32d5dSMike Smith 
19935440dd3SNate Lawson     /* If _GLK is non-zero, acquire the global lock. */
20035440dd3SNate Lawson     status = AE_OK;
20135440dd3SNate Lawson     if (sc->ec_glk) {
20235440dd3SNate Lawson 	status = AcpiAcquireGlobalLock(EC_LOCK_TIMEOUT, &sc->ec_glkhandle);
20335440dd3SNate Lawson 	if (ACPI_FAILURE(status))
20435440dd3SNate Lawson 	    return (status);
20535440dd3SNate Lawson     }
206f4b7de15SNate Lawson     ACPI_SERIAL_BEGIN(ec);
20715e32d5dSMike Smith     return (status);
20815e32d5dSMike Smith }
20915e32d5dSMike Smith 
21083dcc133SNate Lawson static void
21115e32d5dSMike Smith EcUnlock(struct acpi_ec_softc *sc)
21215e32d5dSMike Smith {
213f4b7de15SNate Lawson     ACPI_SERIAL_END(ec);
21435440dd3SNate Lawson     if (sc->ec_glk)
21535440dd3SNate Lawson 	AcpiReleaseGlobalLock(sc->ec_glkhandle);
21615e32d5dSMike Smith }
21715e32d5dSMike Smith 
2185a77b11bSJung-uk Kim static UINT32		EcGpeHandler(ACPI_HANDLE, UINT32, void *);
21915e32d5dSMike Smith static ACPI_STATUS	EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function,
22015e32d5dSMike Smith 				void *Context, void **return_Context);
221464c662eSNate Lawson static ACPI_STATUS	EcSpaceHandler(UINT32 Function,
222464c662eSNate Lawson 				ACPI_PHYSICAL_ADDRESS Address,
223e115a49fSJung-uk Kim 				UINT32 Width, UINT64 *Value,
22415e32d5dSMike Smith 				void *Context, void *RegionContext);
22583dcc133SNate Lawson static ACPI_STATUS	EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event,
22683dcc133SNate Lawson 				u_int gen_count);
2271f04e8f5SNate Lawson static ACPI_STATUS	EcCommand(struct acpi_ec_softc *sc, EC_COMMAND cmd);
228464c662eSNate Lawson static ACPI_STATUS	EcRead(struct acpi_ec_softc *sc, UINT8 Address,
229464c662eSNate Lawson 				UINT8 *Data);
230464c662eSNate Lawson static ACPI_STATUS	EcWrite(struct acpi_ec_softc *sc, UINT8 Address,
231e115a49fSJung-uk Kim 				UINT8 Data);
23215e32d5dSMike Smith static int		acpi_ec_probe(device_t dev);
23315e32d5dSMike Smith static int		acpi_ec_attach(device_t dev);
2340bfeadedSTakanori Watanabe static int		acpi_ec_suspend(device_t dev);
2350bfeadedSTakanori Watanabe static int		acpi_ec_resume(device_t dev);
236340a7f6aSNate Lawson static int		acpi_ec_shutdown(device_t dev);
237e33bea8dSNate Lawson static int		acpi_ec_read_method(device_t dev, u_int addr,
2389a179dd8SJung-uk Kim 				UINT64 *val, int width);
239e33bea8dSNate Lawson static int		acpi_ec_write_method(device_t dev, u_int addr,
2409a179dd8SJung-uk Kim 				UINT64 val, int width);
24115e32d5dSMike Smith 
24215e32d5dSMike Smith static device_method_t acpi_ec_methods[] = {
24315e32d5dSMike Smith     /* Device interface */
24415e32d5dSMike Smith     DEVMETHOD(device_probe,	acpi_ec_probe),
24515e32d5dSMike Smith     DEVMETHOD(device_attach,	acpi_ec_attach),
2460bfeadedSTakanori Watanabe     DEVMETHOD(device_suspend,	acpi_ec_suspend),
2470bfeadedSTakanori Watanabe     DEVMETHOD(device_resume,	acpi_ec_resume),
248340a7f6aSNate Lawson     DEVMETHOD(device_shutdown,	acpi_ec_shutdown),
24915e32d5dSMike Smith 
250e33bea8dSNate Lawson     /* Embedded controller interface */
251e33bea8dSNate Lawson     DEVMETHOD(acpi_ec_read,	acpi_ec_read_method),
252e33bea8dSNate Lawson     DEVMETHOD(acpi_ec_write,	acpi_ec_write_method),
253e33bea8dSNate Lawson 
25461bfd867SSofian Brabez     DEVMETHOD_END
25515e32d5dSMike Smith };
25615e32d5dSMike Smith 
25715e32d5dSMike Smith static driver_t acpi_ec_driver = {
25815e32d5dSMike Smith     "acpi_ec",
25915e32d5dSMike Smith     acpi_ec_methods,
26015e32d5dSMike Smith     sizeof(struct acpi_ec_softc),
26115e32d5dSMike Smith };
26215e32d5dSMike Smith 
2633273b005SMike Smith static devclass_t acpi_ec_devclass;
26415e32d5dSMike Smith DRIVER_MODULE(acpi_ec, acpi, acpi_ec_driver, acpi_ec_devclass, 0, 0);
26564278df5SNate Lawson MODULE_DEPEND(acpi_ec, acpi, 1, 1, 1);
26615e32d5dSMike Smith 
26715e32d5dSMike Smith /*
268f8335e3aSNate Lawson  * Look for an ECDT and if we find one, set up default GPE and
269f8335e3aSNate Lawson  * space handlers to catch attempts to access EC space before
27015e32d5dSMike Smith  * we have a real driver instance in place.
27183dcc133SNate Lawson  *
27283dcc133SNate Lawson  * TODO: Some old Gateway laptops need us to fake up an ECDT or
27383dcc133SNate Lawson  * otherwise attach early so that _REG methods can run.
27415e32d5dSMike Smith  */
275f8335e3aSNate Lawson void
276f8335e3aSNate Lawson acpi_ec_ecdt_probe(device_t parent)
27715e32d5dSMike Smith {
278f8335e3aSNate Lawson     ACPI_TABLE_ECDT *ecdt;
279f8335e3aSNate Lawson     ACPI_STATUS	     status;
280f8335e3aSNate Lawson     device_t	     child;
281f8335e3aSNate Lawson     ACPI_HANDLE	     h;
2820025fb0fSNate Lawson     struct acpi_ec_params *params;
283f8335e3aSNate Lawson 
284b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
28515e32d5dSMike Smith 
286f8335e3aSNate Lawson     /* Find and validate the ECDT. */
2872be4e471SJung-uk Kim     status = AcpiGetTable(ACPI_SIG_ECDT, 1, (ACPI_TABLE_HEADER **)&ecdt);
288f8335e3aSNate Lawson     if (ACPI_FAILURE(status) ||
2892be4e471SJung-uk Kim 	ecdt->Control.BitWidth != 8 ||
2902be4e471SJung-uk Kim 	ecdt->Data.BitWidth != 8) {
291f8335e3aSNate Lawson 	return;
29215e32d5dSMike Smith     }
29315e32d5dSMike Smith 
294f8335e3aSNate Lawson     /* Create the child device with the given unit number. */
295404b0d10SJung-uk Kim     child = BUS_ADD_CHILD(parent, 3, "acpi_ec", ecdt->Uid);
296f8335e3aSNate Lawson     if (child == NULL) {
2970025fb0fSNate Lawson 	printf("%s: can't add child\n", __func__);
298f8335e3aSNate Lawson 	return;
299f8335e3aSNate Lawson     }
300f8335e3aSNate Lawson 
301f8335e3aSNate Lawson     /* Find and save the ACPI handle for this device. */
3022be4e471SJung-uk Kim     status = AcpiGetHandle(NULL, ecdt->Id, &h);
303f8335e3aSNate Lawson     if (ACPI_FAILURE(status)) {
304f8335e3aSNate Lawson 	device_delete_child(parent, child);
3050025fb0fSNate Lawson 	printf("%s: can't get handle\n", __func__);
306f8335e3aSNate Lawson 	return;
307f8335e3aSNate Lawson     }
308f8335e3aSNate Lawson     acpi_set_handle(child, h);
309f8335e3aSNate Lawson 
310f8335e3aSNate Lawson     /* Set the data and CSR register addresses. */
3112be4e471SJung-uk Kim     bus_set_resource(child, SYS_RES_IOPORT, 0, ecdt->Data.Address,
312f8335e3aSNate Lawson 	/*count*/1);
3132be4e471SJung-uk Kim     bus_set_resource(child, SYS_RES_IOPORT, 1, ecdt->Control.Address,
314f8335e3aSNate Lawson 	/*count*/1);
315f8335e3aSNate Lawson 
31615e32d5dSMike Smith     /*
317f8335e3aSNate Lawson      * Store values for the probe/attach routines to use.  Store the
318c868ac7dSNate Lawson      * ECDT GPE bit and set the global lock flag according to _GLK.
3190025fb0fSNate Lawson      * Note that it is not perfectly correct to be evaluating a method
3200025fb0fSNate Lawson      * before initializing devices, but in practice this function
3210025fb0fSNate Lawson      * should be safe to call at this point.
32215e32d5dSMike Smith      */
3230025fb0fSNate Lawson     params = malloc(sizeof(struct acpi_ec_params), M_TEMP, M_WAITOK | M_ZERO);
3240025fb0fSNate Lawson     params->gpe_handle = NULL;
3252be4e471SJung-uk Kim     params->gpe_bit = ecdt->Gpe;
3262be4e471SJung-uk Kim     params->uid = ecdt->Uid;
3270025fb0fSNate Lawson     acpi_GetInteger(h, "_GLK", &params->glk);
3280025fb0fSNate Lawson     acpi_set_private(child, params);
329f8335e3aSNate Lawson 
330f8335e3aSNate Lawson     /* Finish the attach process. */
331f8335e3aSNate Lawson     if (device_probe_and_attach(child) != 0)
332f8335e3aSNate Lawson 	device_delete_child(parent, child);
333f8335e3aSNate Lawson }
334f8335e3aSNate Lawson 
33515e32d5dSMike Smith static int
33615e32d5dSMike Smith acpi_ec_probe(device_t dev)
33715e32d5dSMike Smith {
3380025fb0fSNate Lawson     ACPI_BUFFER buf;
339f8335e3aSNate Lawson     ACPI_HANDLE h;
3400025fb0fSNate Lawson     ACPI_OBJECT *obj;
341f8335e3aSNate Lawson     ACPI_STATUS status;
342f8335e3aSNate Lawson     device_t	peer;
343f8335e3aSNate Lawson     char	desc[64];
344f6eb382cSAndriy Gapon     int		ecdt;
3450025fb0fSNate Lawson     int		ret;
3460025fb0fSNate Lawson     struct acpi_ec_params *params;
3475fcc8a58SNate Lawson     static char *ec_ids[] = { "PNP0C09", NULL };
3480ae55423SMike Smith 
3491a305bdaSBen Widawsky     ret = ENXIO;
3501a305bdaSBen Widawsky 
351c72508e2SNate Lawson     /* Check that this is a device and that EC is not disabled. */
352c72508e2SNate Lawson     if (acpi_get_type(dev) != ACPI_TYPE_DEVICE || acpi_disabled("ec"))
3531a305bdaSBen Widawsky 	return (ret);
35415e32d5dSMike Smith 
3551a305bdaSBen Widawsky     if (device_is_devclass_fixed(dev)) {
35615e32d5dSMike Smith 	/*
3571a305bdaSBen Widawsky 	 * If probed via ECDT, set description and continue. Otherwise, we can
3581a305bdaSBen Widawsky 	 * access the namespace and make sure this is not a duplicate probe.
35915e32d5dSMike Smith 	 */
360f6eb382cSAndriy Gapon         ecdt = 1;
3611a305bdaSBen Widawsky         params = acpi_get_private(dev);
3621a305bdaSBen Widawsky 	if (params != NULL)
3631f04e8f5SNate Lawson 	    ret = 0;
3641a305bdaSBen Widawsky 
3651a305bdaSBen Widawsky 	goto out;
366ec60b7f9SBen Widawsky     } else
367ec60b7f9SBen Widawsky 	ecdt = 0;
3681a305bdaSBen Widawsky 
3695efca36fSTakanori Watanabe     ret = ACPI_ID_PROBE(device_get_parent(dev), dev, ec_ids, NULL);
3705efca36fSTakanori Watanabe     if (ret > 0)
3711a305bdaSBen Widawsky 	return (ret);
3721a305bdaSBen Widawsky 
3731a305bdaSBen Widawsky     params = malloc(sizeof(struct acpi_ec_params), M_TEMP, M_WAITOK | M_ZERO);
3741a305bdaSBen Widawsky 
3751a305bdaSBen Widawsky     buf.Pointer = NULL;
3761a305bdaSBen Widawsky     buf.Length = ACPI_ALLOCATE_BUFFER;
377f8335e3aSNate Lawson     h = acpi_get_handle(dev);
378f8335e3aSNate Lawson 
379f8335e3aSNate Lawson     /*
3801a305bdaSBen Widawsky      * Read the unit ID to check for duplicate attach and the global lock value
3811a305bdaSBen Widawsky      * to see if we should acquire it when accessing the EC.
382f8335e3aSNate Lawson      */
3830025fb0fSNate Lawson     status = acpi_GetInteger(h, "_UID", &params->uid);
384f8335e3aSNate Lawson     if (ACPI_FAILURE(status))
3850025fb0fSNate Lawson 	params->uid = 0;
3861a305bdaSBen Widawsky 
387ec60b7f9SBen Widawsky     /*
388ec60b7f9SBen Widawsky      * Check for a duplicate probe. This can happen when a probe via ECDT
389ec60b7f9SBen Widawsky      * succeeded already. If this is a duplicate, disable this device.
390ec60b7f9SBen Widawsky      *
391ec60b7f9SBen Widawsky      * NB: It would seem device_disable would be sufficient to not get
392ec60b7f9SBen Widawsky      * duplicated devices, and ENXIO isn't needed, however, device_probe() only
393ec60b7f9SBen Widawsky      * checks DF_ENABLED at the start and so disabling it here is too late to
394ec60b7f9SBen Widawsky      * prevent device_attach() from being called.
395ec60b7f9SBen Widawsky      */
396ec60b7f9SBen Widawsky     peer = devclass_get_device(acpi_ec_devclass, params->uid);
397ec60b7f9SBen Widawsky     if (peer != NULL && device_is_alive(peer)) {
398ec60b7f9SBen Widawsky 	device_disable(dev);
399ec60b7f9SBen Widawsky 	ret = ENXIO;
400ec60b7f9SBen Widawsky 	goto out;
401ec60b7f9SBen Widawsky     }
402ec60b7f9SBen Widawsky 
4030025fb0fSNate Lawson     status = acpi_GetInteger(h, "_GLK", &params->glk);
404f8335e3aSNate Lawson     if (ACPI_FAILURE(status))
4050025fb0fSNate Lawson 	params->glk = 0;
406f8335e3aSNate Lawson 
407f8335e3aSNate Lawson     /*
4081a305bdaSBen Widawsky      * Evaluate the _GPE method to find the GPE bit used by the EC to signal
4091a305bdaSBen Widawsky      * status (SCI).  If it's a package, it contains a reference and GPE bit,
4101a305bdaSBen Widawsky      * similar to _PRW.
411f8335e3aSNate Lawson      */
4120025fb0fSNate Lawson     status = AcpiEvaluateObject(h, "_GPE", NULL, &buf);
413f8335e3aSNate Lawson     if (ACPI_FAILURE(status)) {
4141a305bdaSBen Widawsky 	device_printf(dev, "can't evaluate _GPE - %s\n", AcpiFormatException(status));
415a6761eb3SNate Lawson 	goto out;
416f8335e3aSNate Lawson     }
4171a305bdaSBen Widawsky 
4180025fb0fSNate Lawson     obj = (ACPI_OBJECT *)buf.Pointer;
4190025fb0fSNate Lawson     if (obj == NULL)
420a6761eb3SNate Lawson 	goto out;
4210025fb0fSNate Lawson 
4220025fb0fSNate Lawson     switch (obj->Type) {
4230025fb0fSNate Lawson     case ACPI_TYPE_INTEGER:
4240025fb0fSNate Lawson 	params->gpe_handle = NULL;
4250025fb0fSNate Lawson 	params->gpe_bit = obj->Integer.Value;
4260025fb0fSNate Lawson 	break;
4270025fb0fSNate Lawson     case ACPI_TYPE_PACKAGE:
4280025fb0fSNate Lawson 	if (!ACPI_PKG_VALID(obj, 2))
4290025fb0fSNate Lawson 	    goto out;
4301a305bdaSBen Widawsky 	params->gpe_handle = acpi_GetReference(NULL, &obj->Package.Elements[0]);
4310025fb0fSNate Lawson 	if (params->gpe_handle == NULL ||
4320025fb0fSNate Lawson 	    acpi_PkgInt32(obj, 1, &params->gpe_bit) != 0)
4330025fb0fSNate Lawson 		goto out;
4340025fb0fSNate Lawson 	break;
4350025fb0fSNate Lawson     default:
4360025fb0fSNate Lawson 	device_printf(dev, "_GPE has invalid type %d\n", obj->Type);
4370025fb0fSNate Lawson 	goto out;
4380025fb0fSNate Lawson     }
439f8335e3aSNate Lawson 
440f8335e3aSNate Lawson     /* Store the values we got from the namespace for attach. */
4410025fb0fSNate Lawson     acpi_set_private(dev, params);
442f8335e3aSNate Lawson 
4431a305bdaSBen Widawsky     if (buf.Pointer)
4441a305bdaSBen Widawsky 	AcpiOsFree(buf.Pointer);
4450025fb0fSNate Lawson out:
4465efca36fSTakanori Watanabe     if (ret <= 0) {
447c868ac7dSNate Lawson 	snprintf(desc, sizeof(desc), "Embedded Controller: GPE %#x%s%s",
4480025fb0fSNate Lawson 		 params->gpe_bit, (params->glk) ? ", GLK" : "",
449f6eb382cSAndriy Gapon 		 ecdt ? ", ECDT" : "");
450c868ac7dSNate Lawson 	device_set_desc_copy(dev, desc);
4512f977ab4SJung-uk Kim     } else
4520025fb0fSNate Lawson 	free(params, M_TEMP);
4531a305bdaSBen Widawsky 
4541f04e8f5SNate Lawson     return (ret);
45515e32d5dSMike Smith }
45615e32d5dSMike Smith 
45715e32d5dSMike Smith static int
45815e32d5dSMike Smith acpi_ec_attach(device_t dev)
45915e32d5dSMike Smith {
46015e32d5dSMike Smith     struct acpi_ec_softc	*sc;
4610025fb0fSNate Lawson     struct acpi_ec_params	*params;
46215e32d5dSMike Smith     ACPI_STATUS			Status;
463dbd0058aSMike Smith 
464b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
4650ae55423SMike Smith 
4661f04e8f5SNate Lawson     /* Fetch/initialize softc (assumes softc is pre-zeroed). */
46715e32d5dSMike Smith     sc = device_get_softc(dev);
4680025fb0fSNate Lawson     params = acpi_get_private(dev);
46915e32d5dSMike Smith     sc->ec_dev = dev;
47015e32d5dSMike Smith     sc->ec_handle = acpi_get_handle(dev);
47115e32d5dSMike Smith 
472f8335e3aSNate Lawson     /* Retrieve previously probed values via device ivars. */
4730025fb0fSNate Lawson     sc->ec_glk = params->glk;
4740025fb0fSNate Lawson     sc->ec_gpebit = params->gpe_bit;
4750025fb0fSNate Lawson     sc->ec_gpehandle = params->gpe_handle;
4760025fb0fSNate Lawson     sc->ec_uid = params->uid;
47768fb6c48STakanori Watanabe     sc->ec_suspending = FALSE;
478943124d3SAndriy Gapon     acpi_set_private(dev, NULL);
4790025fb0fSNate Lawson     free(params, M_TEMP);
480f8335e3aSNate Lawson 
4811f04e8f5SNate Lawson     /* Attach bus resources for data and command/status ports. */
48215e32d5dSMike Smith     sc->ec_data_rid = 0;
4835f96beb9SNate Lawson     sc->ec_data_res = bus_alloc_resource_any(sc->ec_dev, SYS_RES_IOPORT,
4845f96beb9SNate Lawson 			&sc->ec_data_rid, RF_ACTIVE);
485464c662eSNate Lawson     if (sc->ec_data_res == NULL) {
48615e32d5dSMike Smith 	device_printf(dev, "can't allocate data port\n");
487f3fc4f8bSNate Lawson 	goto error;
48815e32d5dSMike Smith     }
48915e32d5dSMike Smith     sc->ec_data_tag = rman_get_bustag(sc->ec_data_res);
49015e32d5dSMike Smith     sc->ec_data_handle = rman_get_bushandle(sc->ec_data_res);
49115e32d5dSMike Smith 
49215e32d5dSMike Smith     sc->ec_csr_rid = 1;
4935f96beb9SNate Lawson     sc->ec_csr_res = bus_alloc_resource_any(sc->ec_dev, SYS_RES_IOPORT,
4945f96beb9SNate Lawson 			&sc->ec_csr_rid, RF_ACTIVE);
495464c662eSNate Lawson     if (sc->ec_csr_res == NULL) {
49615e32d5dSMike Smith 	device_printf(dev, "can't allocate command/status port\n");
497f3fc4f8bSNate Lawson 	goto error;
49815e32d5dSMike Smith     }
49915e32d5dSMike Smith     sc->ec_csr_tag = rman_get_bustag(sc->ec_csr_res);
50015e32d5dSMike Smith     sc->ec_csr_handle = rman_get_bushandle(sc->ec_csr_res);
50115e32d5dSMike Smith 
50215e32d5dSMike Smith     /*
5031f04e8f5SNate Lawson      * Install a handler for this EC's GPE bit.  We want edge-triggered
5041f04e8f5SNate Lawson      * behavior.
50515e32d5dSMike Smith      */
5061f04e8f5SNate Lawson     ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "attaching GPE handler\n"));
5070025fb0fSNate Lawson     Status = AcpiInstallGpeHandler(sc->ec_gpehandle, sc->ec_gpebit,
5085a77b11bSJung-uk Kim 		ACPI_GPE_EDGE_TRIGGERED, EcGpeHandler, sc);
509464c662eSNate Lawson     if (ACPI_FAILURE(Status)) {
51076d1dff4SMike Smith 	device_printf(dev, "can't install GPE handler for %s - %s\n",
511bfae45aaSMike Smith 		      acpi_name(sc->ec_handle), AcpiFormatException(Status));
512f3fc4f8bSNate Lawson 	goto error;
51315e32d5dSMike Smith     }
51415e32d5dSMike Smith 
51515e32d5dSMike Smith     /*
51615e32d5dSMike Smith      * Install address space handler
51715e32d5dSMike Smith      */
5184c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "attaching address space handler\n"));
519464c662eSNate Lawson     Status = AcpiInstallAddressSpaceHandler(sc->ec_handle, ACPI_ADR_SPACE_EC,
5201f04e8f5SNate Lawson 		&EcSpaceHandler, &EcSpaceSetup, sc);
521464c662eSNate Lawson     if (ACPI_FAILURE(Status)) {
52276d1dff4SMike Smith 	device_printf(dev, "can't install address space handler for %s - %s\n",
523bfae45aaSMike Smith 		      acpi_name(sc->ec_handle), AcpiFormatException(Status));
524f3fc4f8bSNate Lawson 	goto error;
525f3fc4f8bSNate Lawson     }
526f3fc4f8bSNate Lawson 
527f3fc4f8bSNate Lawson     /* Enable runtime GPEs for the handler. */
528a88e22b7SJung-uk Kim     Status = AcpiEnableGpe(sc->ec_gpehandle, sc->ec_gpebit);
529f3fc4f8bSNate Lawson     if (ACPI_FAILURE(Status)) {
530f3fc4f8bSNate Lawson 	device_printf(dev, "AcpiEnableGpe failed: %s\n",
531f3fc4f8bSNate Lawson 		      AcpiFormatException(Status));
532f3fc4f8bSNate Lawson 	goto error;
53315e32d5dSMike Smith     }
5341f04e8f5SNate Lawson 
5351f04e8f5SNate Lawson     ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "acpi_ec_attach complete\n"));
5361f04e8f5SNate Lawson     return (0);
537464c662eSNate Lawson 
538f3fc4f8bSNate Lawson error:
5395a77b11bSJung-uk Kim     AcpiRemoveGpeHandler(sc->ec_gpehandle, sc->ec_gpebit, EcGpeHandler);
540f3fc4f8bSNate Lawson     AcpiRemoveAddressSpaceHandler(sc->ec_handle, ACPI_ADR_SPACE_EC,
541f3fc4f8bSNate Lawson 	EcSpaceHandler);
542f8372adeSTakanori Watanabe     if (sc->ec_csr_res)
543f8372adeSTakanori Watanabe 	bus_release_resource(sc->ec_dev, SYS_RES_IOPORT, sc->ec_csr_rid,
544f8372adeSTakanori Watanabe 			     sc->ec_csr_res);
545f8372adeSTakanori Watanabe     if (sc->ec_data_res)
546f8372adeSTakanori Watanabe 	bus_release_resource(sc->ec_dev, SYS_RES_IOPORT, sc->ec_data_rid,
547f8372adeSTakanori Watanabe 			     sc->ec_data_res);
548f3fc4f8bSNate Lawson     return (ENXIO);
5491f04e8f5SNate Lawson }
5501f04e8f5SNate Lawson 
551340a7f6aSNate Lawson static int
5520bfeadedSTakanori Watanabe acpi_ec_suspend(device_t dev)
5530bfeadedSTakanori Watanabe {
5540bfeadedSTakanori Watanabe     struct acpi_ec_softc	*sc;
5550bfeadedSTakanori Watanabe 
5560bfeadedSTakanori Watanabe     sc = device_get_softc(dev);
55768fb6c48STakanori Watanabe     sc->ec_suspending = TRUE;
5580bfeadedSTakanori Watanabe     return (0);
5590bfeadedSTakanori Watanabe }
5600bfeadedSTakanori Watanabe 
5610bfeadedSTakanori Watanabe static int
5620bfeadedSTakanori Watanabe acpi_ec_resume(device_t dev)
5630bfeadedSTakanori Watanabe {
5640bfeadedSTakanori Watanabe     struct acpi_ec_softc	*sc;
5650bfeadedSTakanori Watanabe 
5660bfeadedSTakanori Watanabe     sc = device_get_softc(dev);
56768fb6c48STakanori Watanabe     sc->ec_suspending = FALSE;
5680bfeadedSTakanori Watanabe     return (0);
5690bfeadedSTakanori Watanabe }
5700bfeadedSTakanori Watanabe 
5710bfeadedSTakanori Watanabe static int
572340a7f6aSNate Lawson acpi_ec_shutdown(device_t dev)
573340a7f6aSNate Lawson {
574340a7f6aSNate Lawson     struct acpi_ec_softc	*sc;
575340a7f6aSNate Lawson 
576340a7f6aSNate Lawson     /* Disable the GPE so we don't get EC events during shutdown. */
577340a7f6aSNate Lawson     sc = device_get_softc(dev);
578a88e22b7SJung-uk Kim     AcpiDisableGpe(sc->ec_gpehandle, sc->ec_gpebit);
579340a7f6aSNate Lawson     return (0);
580340a7f6aSNate Lawson }
581340a7f6aSNate Lawson 
582e33bea8dSNate Lawson /* Methods to allow other devices (e.g., smbat) to read/write EC space. */
583e33bea8dSNate Lawson static int
5849a179dd8SJung-uk Kim acpi_ec_read_method(device_t dev, u_int addr, UINT64 *val, int width)
585e33bea8dSNate Lawson {
586e33bea8dSNate Lawson     struct acpi_ec_softc *sc;
587e33bea8dSNate Lawson     ACPI_STATUS status;
588e33bea8dSNate Lawson 
589e33bea8dSNate Lawson     sc = device_get_softc(dev);
590e33bea8dSNate Lawson     status = EcSpaceHandler(ACPI_READ, addr, width * 8, val, sc, NULL);
591e33bea8dSNate Lawson     if (ACPI_FAILURE(status))
592e33bea8dSNate Lawson 	return (ENXIO);
593e33bea8dSNate Lawson     return (0);
594e33bea8dSNate Lawson }
595e33bea8dSNate Lawson 
596e33bea8dSNate Lawson static int
5979a179dd8SJung-uk Kim acpi_ec_write_method(device_t dev, u_int addr, UINT64 val, int width)
598e33bea8dSNate Lawson {
599e33bea8dSNate Lawson     struct acpi_ec_softc *sc;
600e33bea8dSNate Lawson     ACPI_STATUS status;
601e33bea8dSNate Lawson 
602e33bea8dSNate Lawson     sc = device_get_softc(dev);
603e33bea8dSNate Lawson     status = EcSpaceHandler(ACPI_WRITE, addr, width * 8, &val, sc, NULL);
604e33bea8dSNate Lawson     if (ACPI_FAILURE(status))
605e33bea8dSNate Lawson 	return (ENXIO);
606e33bea8dSNate Lawson     return (0);
607e33bea8dSNate Lawson }
608e33bea8dSNate Lawson 
6095ff14fa9SAndriy Gapon static ACPI_STATUS
6105ff14fa9SAndriy Gapon EcCheckStatus(struct acpi_ec_softc *sc, const char *msg, EC_EVENT event)
6115ff14fa9SAndriy Gapon {
6125ff14fa9SAndriy Gapon     ACPI_STATUS status;
6135ff14fa9SAndriy Gapon     EC_STATUS ec_status;
6145ff14fa9SAndriy Gapon 
6155ff14fa9SAndriy Gapon     status = AE_NO_HARDWARE_RESPONSE;
6165ff14fa9SAndriy Gapon     ec_status = EC_GET_CSR(sc);
6175ff14fa9SAndriy Gapon     if (sc->ec_burstactive && !(ec_status & EC_FLAG_BURST_MODE)) {
6185ff14fa9SAndriy Gapon 	CTR1(KTR_ACPI, "ec burst disabled in waitevent (%s)", msg);
6195ff14fa9SAndriy Gapon 	sc->ec_burstactive = FALSE;
6205ff14fa9SAndriy Gapon     }
6215ff14fa9SAndriy Gapon     if (EVENT_READY(event, ec_status)) {
6225ff14fa9SAndriy Gapon 	CTR2(KTR_ACPI, "ec %s wait ready, status %#x", msg, ec_status);
6235ff14fa9SAndriy Gapon 	status = AE_OK;
6245ff14fa9SAndriy Gapon     }
6255ff14fa9SAndriy Gapon     return (status);
6265ff14fa9SAndriy Gapon }
6275ff14fa9SAndriy Gapon 
62815e32d5dSMike Smith static void
629c6e6b4feSHans Petter Selasky EcGpeQueryHandlerSub(struct acpi_ec_softc *sc)
63015e32d5dSMike Smith {
63115e32d5dSMike Smith     UINT8			Data;
63215e32d5dSMike Smith     ACPI_STATUS			Status;
6334552fccbSJung-uk Kim     int				retry;
63415e32d5dSMike Smith     char			qxx[5];
63515e32d5dSMike Smith 
636b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
63715e32d5dSMike Smith 
638ef2374f7SNate Lawson     /* Serialize user access with EcSpaceHandler(). */
63983dcc133SNate Lawson     Status = EcLock(sc);
6403a371f32SNate Lawson     if (ACPI_FAILURE(Status)) {
64183dcc133SNate Lawson 	device_printf(sc->ec_dev, "GpeQuery lock error: %s\n",
64283dcc133SNate Lawson 	    AcpiFormatException(Status));
6433a371f32SNate Lawson 	return;
6443a371f32SNate Lawson     }
6453a371f32SNate Lawson 
64615e32d5dSMike Smith     /*
6473a371f32SNate Lawson      * Send a query command to the EC to find out which _Qxx call it
6483a371f32SNate Lawson      * wants to make.  This command clears the SCI bit and also the
64983dcc133SNate Lawson      * interrupt source since we are edge-triggered.  To prevent the GPE
65083dcc133SNate Lawson      * that may arise from running the query from causing another query
65183dcc133SNate Lawson      * to be queued, we clear the pending flag only after running it.
6523a371f32SNate Lawson      */
6535ff14fa9SAndriy Gapon     for (retry = 0; retry < 2; retry++) {
6543a371f32SNate Lawson 	Status = EcCommand(sc, EC_COMMAND_QUERY);
6555ff14fa9SAndriy Gapon 	if (ACPI_SUCCESS(Status))
6565ff14fa9SAndriy Gapon 	    break;
657ae1ef64fSJung-uk Kim 	if (ACPI_FAILURE(EcCheckStatus(sc, "retr_check",
6589ad56977SJung-uk Kim 	    EC_EVENT_INPUT_BUFFER_EMPTY)))
6595ff14fa9SAndriy Gapon 	    break;
6605ff14fa9SAndriy Gapon     }
661dbd0058aSMike Smith     if (ACPI_FAILURE(Status)) {
6623a371f32SNate Lawson 	EcUnlock(sc);
66383dcc133SNate Lawson 	device_printf(sc->ec_dev, "GPE query failed: %s\n",
66483dcc133SNate Lawson 	    AcpiFormatException(Status));
66583dcc133SNate Lawson 	return;
66615e32d5dSMike Smith     }
6673a371f32SNate Lawson     Data = EC_GET_DATA(sc);
668ef2374f7SNate Lawson 
66983dcc133SNate Lawson     /*
67083dcc133SNate Lawson      * We have to unlock before running the _Qxx method below since that
67183dcc133SNate Lawson      * method may attempt to read/write from EC address space, causing
67283dcc133SNate Lawson      * recursive acquisition of the lock.
67383dcc133SNate Lawson      */
6743a371f32SNate Lawson     EcUnlock(sc);
67515e32d5dSMike Smith 
6761f04e8f5SNate Lawson     /* Ignore the value for "no outstanding event". (13.3.5) */
67783dcc133SNate Lawson     CTR2(KTR_ACPI, "ec query ok,%s running _Q%02X", Data ? "" : " not", Data);
6781f04e8f5SNate Lawson     if (Data == 0)
67983dcc133SNate Lawson 	return;
6801f04e8f5SNate Lawson 
6811f04e8f5SNate Lawson     /* Evaluate _Qxx to respond to the controller. */
68283dcc133SNate Lawson     snprintf(qxx, sizeof(qxx), "_Q%02X", Data);
683b3919c8dSMark Santcroos     AcpiUtStrupr(qxx);
684ee785aa9SJohn Baldwin     Status = AcpiEvaluateObject(sc->ec_handle, qxx, NULL, NULL);
6851f04e8f5SNate Lawson     if (ACPI_FAILURE(Status) && Status != AE_NOT_FOUND) {
68683dcc133SNate Lawson 	device_printf(sc->ec_dev, "evaluation of query method %s failed: %s\n",
687bfae45aaSMike Smith 	    qxx, AcpiFormatException(Status));
68815e32d5dSMike Smith     }
689c07572e7STakanori Watanabe }
69042f6d122SMike Smith 
691c6e6b4feSHans Petter Selasky static void
692c6e6b4feSHans Petter Selasky EcGpeQueryHandler(void *Context)
693c6e6b4feSHans Petter Selasky {
694c6e6b4feSHans Petter Selasky     struct acpi_ec_softc *sc = (struct acpi_ec_softc *)Context;
695c6e6b4feSHans Petter Selasky     int pending;
696c6e6b4feSHans Petter Selasky 
697c6e6b4feSHans Petter Selasky     KASSERT(Context != NULL, ("EcGpeQueryHandler called with NULL"));
698c6e6b4feSHans Petter Selasky 
699c6e6b4feSHans Petter Selasky     do {
700c6e6b4feSHans Petter Selasky 	/* Read the current pending count */
701c6e6b4feSHans Petter Selasky 	pending = atomic_load_acq_int(&sc->ec_sci_pend);
702c6e6b4feSHans Petter Selasky 
703c6e6b4feSHans Petter Selasky 	/* Call GPE handler function */
704c6e6b4feSHans Petter Selasky 	EcGpeQueryHandlerSub(sc);
705c6e6b4feSHans Petter Selasky 
706c6e6b4feSHans Petter Selasky 	/*
707c6e6b4feSHans Petter Selasky 	 * Try to reset the pending count to zero. If this fails we
708c6e6b4feSHans Petter Selasky 	 * know another GPE event has occurred while handling the
709c6e6b4feSHans Petter Selasky 	 * current GPE event and need to loop.
710c6e6b4feSHans Petter Selasky 	 */
711c6e6b4feSHans Petter Selasky     } while (!atomic_cmpset_int(&sc->ec_sci_pend, pending, 0));
712c6e6b4feSHans Petter Selasky }
713c6e6b4feSHans Petter Selasky 
714a9cf0dffSMike Smith /*
71583dcc133SNate Lawson  * The GPE handler is called when IBE/OBF or SCI events occur.  We are
71683dcc133SNate Lawson  * called from an unknown lock context.
717a9cf0dffSMike Smith  */
7185a77b11bSJung-uk Kim static UINT32
7195a77b11bSJung-uk Kim EcGpeHandler(ACPI_HANDLE GpeDevice, UINT32 GpeNumber, void *Context)
720c07572e7STakanori Watanabe {
721c07572e7STakanori Watanabe     struct acpi_ec_softc *sc = Context;
7221f04e8f5SNate Lawson     ACPI_STATUS		       Status;
723ef2374f7SNate Lawson     EC_STATUS		       EcStatus;
724a9cf0dffSMike Smith 
7251f04e8f5SNate Lawson     KASSERT(Context != NULL, ("EcGpeHandler called with NULL"));
72683dcc133SNate Lawson     CTR0(KTR_ACPI, "ec gpe handler start");
7271f04e8f5SNate Lawson 
7281395b555SNate Lawson     /*
72983dcc133SNate Lawson      * Notify EcWaitEvent() that the status register is now fresh.  If we
73083dcc133SNate Lawson      * didn't do this, it wouldn't be possible to distinguish an old IBE
73183dcc133SNate Lawson      * from a new one, for example when doing a write transaction (writing
73283dcc133SNate Lawson      * address and then data values.)
7331395b555SNate Lawson      */
73483dcc133SNate Lawson     atomic_add_int(&sc->ec_gencount, 1);
73580b1151eSJung-uk Kim     wakeup(sc);
736ef2374f7SNate Lawson 
737ef2374f7SNate Lawson     /*
73883dcc133SNate Lawson      * If the EC_SCI bit of the status register is set, queue a query handler.
73983dcc133SNate Lawson      * It will run the query and _Qxx method later, under the lock.
740ef2374f7SNate Lawson      */
741ef2374f7SNate Lawson     EcStatus = EC_GET_CSR(sc);
742c6e6b4feSHans Petter Selasky     if ((EcStatus & EC_EVENT_SCI) &&
743c6e6b4feSHans Petter Selasky 	atomic_fetchadd_int(&sc->ec_sci_pend, 1) == 0) {
74483dcc133SNate Lawson 	CTR0(KTR_ACPI, "ec gpe queueing query handler");
7452be4e471SJung-uk Kim 	Status = AcpiOsExecute(OSL_GPE_HANDLER, EcGpeQueryHandler, Context);
746c6e6b4feSHans Petter Selasky 	if (ACPI_FAILURE(Status)) {
74783dcc133SNate Lawson 	    printf("EcGpeHandler: queuing GPE query handler failed\n");
748c6e6b4feSHans Petter Selasky 	    atomic_store_rel_int(&sc->ec_sci_pend, 0);
749c6e6b4feSHans Petter Selasky 	}
7506e141df2SNate Lawson     }
7515a77b11bSJung-uk Kim     return (ACPI_REENABLE_GPE);
75215e32d5dSMike Smith }
75315e32d5dSMike Smith 
75415e32d5dSMike Smith static ACPI_STATUS
755464c662eSNate Lawson EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function, void *Context,
756464c662eSNate Lawson 	     void **RegionContext)
75715e32d5dSMike Smith {
75842f6d122SMike Smith 
759b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
76042f6d122SMike Smith 
76115e32d5dSMike Smith     /*
76216844a97SNate Lawson      * If deactivating a region, always set the output to NULL.  Otherwise,
76316844a97SNate Lawson      * just pass the context through.
76415e32d5dSMike Smith      */
76516844a97SNate Lawson     if (Function == ACPI_REGION_DEACTIVATE)
76616844a97SNate Lawson 	*RegionContext = NULL;
76716844a97SNate Lawson     else
76815e32d5dSMike Smith 	*RegionContext = Context;
76915e32d5dSMike Smith 
77042f6d122SMike Smith     return_ACPI_STATUS (AE_OK);
77115e32d5dSMike Smith }
77215e32d5dSMike Smith 
77315e32d5dSMike Smith static ACPI_STATUS
774e115a49fSJung-uk Kim EcSpaceHandler(UINT32 Function, ACPI_PHYSICAL_ADDRESS Address, UINT32 Width,
7759a179dd8SJung-uk Kim 	       UINT64 *Value, void *Context, void *RegionContext)
77615e32d5dSMike Smith {
77715e32d5dSMike Smith     struct acpi_ec_softc	*sc = (struct acpi_ec_softc *)Context;
77834622ff1SJung-uk Kim     ACPI_PHYSICAL_ADDRESS	EcAddr;
779e115a49fSJung-uk Kim     UINT8			*EcData;
78034622ff1SJung-uk Kim     ACPI_STATUS			Status;
78115e32d5dSMike Smith 
782b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, (UINT32)Address);
7830ae55423SMike Smith 
78434622ff1SJung-uk Kim     if (Function != ACPI_READ && Function != ACPI_WRITE)
78534622ff1SJung-uk Kim 	return_ACPI_STATUS (AE_BAD_PARAMETER);
786e115a49fSJung-uk Kim     if (Width % 8 != 0 || Value == NULL || Context == NULL)
7870ae55423SMike Smith 	return_ACPI_STATUS (AE_BAD_PARAMETER);
78834622ff1SJung-uk Kim     if (Address + Width / 8 > 256)
7894ed391b8SNate Lawson 	return_ACPI_STATUS (AE_BAD_ADDRESS);
79015e32d5dSMike Smith 
79183dcc133SNate Lawson     /*
79283dcc133SNate Lawson      * If booting, check if we need to run the query handler.  If so, we
79383dcc133SNate Lawson      * we call it directly here since our thread taskq is not active yet.
79483dcc133SNate Lawson      */
795c66d2b38SJung-uk Kim     if (cold || rebooting || sc->ec_suspending) {
796c6e6b4feSHans Petter Selasky 	if ((EC_GET_CSR(sc) & EC_EVENT_SCI) &&
797c6e6b4feSHans Petter Selasky 	    atomic_fetchadd_int(&sc->ec_sci_pend, 1) == 0) {
79883dcc133SNate Lawson 	    CTR0(KTR_ACPI, "ec running gpe handler directly");
79983dcc133SNate Lawson 	    EcGpeQueryHandler(sc);
80083dcc133SNate Lawson 	}
80183dcc133SNate Lawson     }
80283dcc133SNate Lawson 
80383dcc133SNate Lawson     /* Serialize with EcGpeQueryHandler() at transaction granularity. */
80483dcc133SNate Lawson     Status = EcLock(sc);
805464c662eSNate Lawson     if (ACPI_FAILURE(Status))
806f4b7de15SNate Lawson 	return_ACPI_STATUS (Status);
8071f04e8f5SNate Lawson 
80834622ff1SJung-uk Kim     /* If we can't start burst mode, continue anyway. */
80934622ff1SJung-uk Kim     Status = EcCommand(sc, EC_COMMAND_BURST_ENABLE);
81034622ff1SJung-uk Kim     if (ACPI_SUCCESS(Status)) {
81134622ff1SJung-uk Kim 	if (EC_GET_DATA(sc) == EC_BURST_ACK) {
81234622ff1SJung-uk Kim 	    CTR0(KTR_ACPI, "ec burst enabled");
81334622ff1SJung-uk Kim 	    sc->ec_burstactive = TRUE;
81434622ff1SJung-uk Kim 	}
81534622ff1SJung-uk Kim     }
81634622ff1SJung-uk Kim 
817e115a49fSJung-uk Kim     /* Perform the transaction(s), based on Width. */
81834622ff1SJung-uk Kim     EcAddr = Address;
81934622ff1SJung-uk Kim     EcData = (UINT8 *)Value;
82034622ff1SJung-uk Kim     if (Function == ACPI_READ)
82134622ff1SJung-uk Kim 	*Value = 0;
82234622ff1SJung-uk Kim     do {
8231f04e8f5SNate Lawson 	switch (Function) {
8241f04e8f5SNate Lawson 	case ACPI_READ:
825e115a49fSJung-uk Kim 	    Status = EcRead(sc, EcAddr, EcData);
826ee785aa9SJohn Baldwin 	    break;
8271f04e8f5SNate Lawson 	case ACPI_WRITE:
828e115a49fSJung-uk Kim 	    Status = EcWrite(sc, EcAddr, *EcData);
8291f04e8f5SNate Lawson 	    break;
8301f04e8f5SNate Lawson 	}
8311f04e8f5SNate Lawson 	if (ACPI_FAILURE(Status))
8324ed391b8SNate Lawson 	    break;
83334622ff1SJung-uk Kim 	EcAddr++;
83434622ff1SJung-uk Kim 	EcData++;
83534622ff1SJung-uk Kim     } while (EcAddr < Address + Width / 8);
83634622ff1SJung-uk Kim 
83734622ff1SJung-uk Kim     if (sc->ec_burstactive) {
83834622ff1SJung-uk Kim 	sc->ec_burstactive = FALSE;
83934622ff1SJung-uk Kim 	if (ACPI_SUCCESS(EcCommand(sc, EC_COMMAND_BURST_DISABLE)))
84034622ff1SJung-uk Kim 	    CTR0(KTR_ACPI, "ec disabled burst ok");
841ee785aa9SJohn Baldwin     }
8424ed391b8SNate Lawson 
843f4b7de15SNate Lawson     EcUnlock(sc);
8440ae55423SMike Smith     return_ACPI_STATUS (Status);
84515e32d5dSMike Smith }
8462a4ac806SMike Smith 
84715e32d5dSMike Smith static ACPI_STATUS
84883dcc133SNate Lawson EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event, u_int gen_count)
84983dcc133SNate Lawson {
8505ff14fa9SAndriy Gapon     static int	no_intr = 0;
8511f04e8f5SNate Lawson     ACPI_STATUS	Status;
8525ff14fa9SAndriy Gapon     int		count, i, need_poll, slp_ival;
85315e32d5dSMike Smith 
854f4b7de15SNate Lawson     ACPI_SERIAL_ASSERT(ec);
8551f04e8f5SNate Lawson     Status = AE_NO_HARDWARE_RESPONSE;
8565ff14fa9SAndriy Gapon     need_poll = cold || rebooting || ec_polled_mode || sc->ec_suspending;
85783dcc133SNate Lawson 
85883dcc133SNate Lawson     /* Wait for event by polling or GPE (interrupt). */
8591de5ce99STakanori Watanabe     if (need_poll) {
86083dcc133SNate Lawson 	count = (ec_timeout * 1000) / EC_POLL_DELAY;
86183dcc133SNate Lawson 	if (count == 0)
862ef2374f7SNate Lawson 	    count = 1;
8635ff14fa9SAndriy Gapon 	DELAY(10);
864ef2374f7SNate Lawson 	for (i = 0; i < count; i++) {
86583dcc133SNate Lawson 	    Status = EcCheckStatus(sc, "poll", Event);
8669ad56977SJung-uk Kim 	    if (ACPI_SUCCESS(Status))
86715e32d5dSMike Smith 		break;
8685ff14fa9SAndriy Gapon 	    DELAY(EC_POLL_DELAY);
8691f04e8f5SNate Lawson 	}
87083dcc133SNate Lawson     } else {
871ef2374f7SNate Lawson 	slp_ival = hz / 1000;
872ef2374f7SNate Lawson 	if (slp_ival != 0) {
87383dcc133SNate Lawson 	    count = ec_timeout;
874ef2374f7SNate Lawson 	} else {
87583dcc133SNate Lawson 	    /* hz has less than 1 ms resolution so scale timeout. */
876b0eefa38SNate Lawson 	    slp_ival = 1;
877ef2374f7SNate Lawson 	    count = ec_timeout / (1000 / hz);
878ef2374f7SNate Lawson 	}
87983dcc133SNate Lawson 
88083dcc133SNate Lawson 	/*
88183dcc133SNate Lawson 	 * Wait for the GPE to signal the status changed, checking the
88283dcc133SNate Lawson 	 * status register each time we get one.  It's possible to get a
88383dcc133SNate Lawson 	 * GPE for an event we're not interested in here (i.e., SCI for
88483dcc133SNate Lawson 	 * EC query).
88583dcc133SNate Lawson 	 */
886b0eefa38SNate Lawson 	for (i = 0; i < count; i++) {
8875ff14fa9SAndriy Gapon 	    if (gen_count == sc->ec_gencount)
88880b1151eSJung-uk Kim 		tsleep(sc, 0, "ecgpe", slp_ival);
88983dcc133SNate Lawson 	    /*
89083dcc133SNate Lawson 	     * Record new generation count.  It's possible the GPE was
89183dcc133SNate Lawson 	     * just to notify us that a query is needed and we need to
89283dcc133SNate Lawson 	     * wait for a second GPE to signal the completion of the
89383dcc133SNate Lawson 	     * event we are actually waiting for.
89483dcc133SNate Lawson 	     */
89583dcc133SNate Lawson 	    Status = EcCheckStatus(sc, "sleep", Event);
8969ad56977SJung-uk Kim 	    if (ACPI_SUCCESS(Status)) {
8975ff14fa9SAndriy Gapon 		if (gen_count == sc->ec_gencount)
8985ff14fa9SAndriy Gapon 		    no_intr++;
8995ff14fa9SAndriy Gapon 		else
9005ff14fa9SAndriy Gapon 		    no_intr = 0;
901ff40920eSNate Lawson 		break;
902ff40920eSNate Lawson 	    }
9035ff14fa9SAndriy Gapon 	    gen_count = sc->ec_gencount;
9041f04e8f5SNate Lawson 	}
9051f04e8f5SNate Lawson 
90683dcc133SNate Lawson 	/*
90783dcc133SNate Lawson 	 * We finished waiting for the GPE and it never arrived.  Try to
90883dcc133SNate Lawson 	 * read the register once and trust whatever value we got.  This is
9095ff14fa9SAndriy Gapon 	 * the best we can do at this point.
91083dcc133SNate Lawson 	 */
9119ad56977SJung-uk Kim 	if (ACPI_FAILURE(Status))
91283dcc133SNate Lawson 	    Status = EcCheckStatus(sc, "sleep_end", Event);
91383dcc133SNate Lawson     }
9145ff14fa9SAndriy Gapon     if (!need_poll && no_intr > 10) {
9155ff14fa9SAndriy Gapon 	device_printf(sc->ec_dev,
9165ff14fa9SAndriy Gapon 	    "not getting interrupts, switched to polled mode\n");
9175ff14fa9SAndriy Gapon 	ec_polled_mode = 1;
91883dcc133SNate Lawson     }
9199ad56977SJung-uk Kim     if (ACPI_FAILURE(Status))
92083dcc133SNate Lawson 	    CTR0(KTR_ACPI, "error: ec wait timed out");
9211f04e8f5SNate Lawson     return (Status);
9221f04e8f5SNate Lawson }
9231f04e8f5SNate Lawson 
9241f04e8f5SNate Lawson static ACPI_STATUS
9251f04e8f5SNate Lawson EcCommand(struct acpi_ec_softc *sc, EC_COMMAND cmd)
9261f04e8f5SNate Lawson {
927ef2374f7SNate Lawson     ACPI_STATUS	status;
928ef2374f7SNate Lawson     EC_EVENT	event;
929ef2374f7SNate Lawson     EC_STATUS	ec_status;
93083dcc133SNate Lawson     u_int	gen_count;
9311f04e8f5SNate Lawson 
932f4b7de15SNate Lawson     ACPI_SERIAL_ASSERT(ec);
9331f04e8f5SNate Lawson 
934ef2374f7SNate Lawson     /* Don't use burst mode if user disabled it. */
935ef2374f7SNate Lawson     if (!ec_burst_mode && cmd == EC_COMMAND_BURST_ENABLE)
936ef2374f7SNate Lawson 	return (AE_ERROR);
937ef2374f7SNate Lawson 
9381f04e8f5SNate Lawson     /* Decide what to wait for based on command type. */
9391f04e8f5SNate Lawson     switch (cmd) {
9401f04e8f5SNate Lawson     case EC_COMMAND_READ:
94115e32d5dSMike Smith     case EC_COMMAND_WRITE:
9421f04e8f5SNate Lawson     case EC_COMMAND_BURST_DISABLE:
943ef2374f7SNate Lawson 	event = EC_EVENT_INPUT_BUFFER_EMPTY;
9441f04e8f5SNate Lawson 	break;
9451f04e8f5SNate Lawson     case EC_COMMAND_QUERY:
9461f04e8f5SNate Lawson     case EC_COMMAND_BURST_ENABLE:
947ef2374f7SNate Lawson 	event = EC_EVENT_OUTPUT_BUFFER_FULL;
94815e32d5dSMike Smith 	break;
94915e32d5dSMike Smith     default:
95083dcc133SNate Lawson 	device_printf(sc->ec_dev, "EcCommand: invalid command %#x\n", cmd);
9511f04e8f5SNate Lawson 	return (AE_BAD_PARAMETER);
952464c662eSNate Lawson     }
9531f04e8f5SNate Lawson 
9545ff14fa9SAndriy Gapon     /*
9555ff14fa9SAndriy Gapon      * Ensure empty input buffer before issuing command.
9565ff14fa9SAndriy Gapon      * Use generation count of zero to force a quick check.
9575ff14fa9SAndriy Gapon      */
9585ff14fa9SAndriy Gapon     status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY, 0);
9595ff14fa9SAndriy Gapon     if (ACPI_FAILURE(status))
9605ff14fa9SAndriy Gapon 	return (status);
9615ff14fa9SAndriy Gapon 
9621f04e8f5SNate Lawson     /* Run the command and wait for the chosen event. */
963ef2374f7SNate Lawson     CTR1(KTR_ACPI, "ec running command %#x", cmd);
96483dcc133SNate Lawson     gen_count = sc->ec_gencount;
9651f04e8f5SNate Lawson     EC_SET_CSR(sc, cmd);
96683dcc133SNate Lawson     status = EcWaitEvent(sc, event, gen_count);
967ef2374f7SNate Lawson     if (ACPI_SUCCESS(status)) {
968ef2374f7SNate Lawson 	/* If we succeeded, burst flag should now be present. */
969ef2374f7SNate Lawson 	if (cmd == EC_COMMAND_BURST_ENABLE) {
970ef2374f7SNate Lawson 	    ec_status = EC_GET_CSR(sc);
971ef2374f7SNate Lawson 	    if ((ec_status & EC_FLAG_BURST_MODE) == 0)
972ef2374f7SNate Lawson 		status = AE_ERROR;
973ef2374f7SNate Lawson 	}
97483dcc133SNate Lawson     } else
97583dcc133SNate Lawson 	device_printf(sc->ec_dev, "EcCommand: no response to %#x\n", cmd);
976ef2374f7SNate Lawson     return (status);
97715e32d5dSMike Smith }
97815e32d5dSMike Smith 
97915e32d5dSMike Smith static ACPI_STATUS
98015e32d5dSMike Smith EcRead(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data)
98115e32d5dSMike Smith {
982ef2374f7SNate Lawson     ACPI_STATUS	status;
98383dcc133SNate Lawson     u_int gen_count;
9845ff14fa9SAndriy Gapon     int retry;
98515e32d5dSMike Smith 
986f4b7de15SNate Lawson     ACPI_SERIAL_ASSERT(ec);
987c4a9fa45SNate Lawson     CTR1(KTR_ACPI, "ec read from %#x", Address);
98815e32d5dSMike Smith 
9895ff14fa9SAndriy Gapon     for (retry = 0; retry < 2; retry++) {
990ef2374f7SNate Lawson 	status = EcCommand(sc, EC_COMMAND_READ);
991ef2374f7SNate Lawson 	if (ACPI_FAILURE(status))
992ef2374f7SNate Lawson 	    return (status);
99315e32d5dSMike Smith 
99483dcc133SNate Lawson 	gen_count = sc->ec_gencount;
99515e32d5dSMike Smith 	EC_SET_DATA(sc, Address);
99683dcc133SNate Lawson 	status = EcWaitEvent(sc, EC_EVENT_OUTPUT_BUFFER_FULL, gen_count);
997ae1ef64fSJung-uk Kim 	if (ACPI_SUCCESS(status)) {
998464c662eSNate Lawson 	    *Data = EC_GET_DATA(sc);
99915e32d5dSMike Smith 	    return (AE_OK);
100015e32d5dSMike Smith 	}
1001ae1ef64fSJung-uk Kim 	if (ACPI_FAILURE(EcCheckStatus(sc, "retr_check",
1002ae1ef64fSJung-uk Kim 	    EC_EVENT_INPUT_BUFFER_EMPTY)))
1003ae1ef64fSJung-uk Kim 	    break;
1004ae1ef64fSJung-uk Kim     }
10055ff14fa9SAndriy Gapon     device_printf(sc->ec_dev, "EcRead: failed waiting to get data\n");
10065ff14fa9SAndriy Gapon     return (status);
10075ff14fa9SAndriy Gapon }
100815e32d5dSMike Smith 
100915e32d5dSMike Smith static ACPI_STATUS
1010e115a49fSJung-uk Kim EcWrite(struct acpi_ec_softc *sc, UINT8 Address, UINT8 Data)
101115e32d5dSMike Smith {
1012ef2374f7SNate Lawson     ACPI_STATUS	status;
101383dcc133SNate Lawson     u_int gen_count;
101415e32d5dSMike Smith 
1015f4b7de15SNate Lawson     ACPI_SERIAL_ASSERT(ec);
1016e115a49fSJung-uk Kim     CTR2(KTR_ACPI, "ec write to %#x, data %#x", Address, Data);
101715e32d5dSMike Smith 
1018ef2374f7SNate Lawson     status = EcCommand(sc, EC_COMMAND_WRITE);
1019ef2374f7SNate Lawson     if (ACPI_FAILURE(status))
1020ef2374f7SNate Lawson 	return (status);
102115e32d5dSMike Smith 
102283dcc133SNate Lawson     gen_count = sc->ec_gencount;
102315e32d5dSMike Smith     EC_SET_DATA(sc, Address);
102483dcc133SNate Lawson     status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY, gen_count);
1025ef2374f7SNate Lawson     if (ACPI_FAILURE(status)) {
10265ff14fa9SAndriy Gapon 	device_printf(sc->ec_dev, "EcWrite: failed waiting for sent address\n");
1027ef2374f7SNate Lawson 	return (status);
102815e32d5dSMike Smith     }
102915e32d5dSMike Smith 
103083dcc133SNate Lawson     gen_count = sc->ec_gencount;
1031e115a49fSJung-uk Kim     EC_SET_DATA(sc, Data);
103283dcc133SNate Lawson     status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY, gen_count);
1033ef2374f7SNate Lawson     if (ACPI_FAILURE(status)) {
103483dcc133SNate Lawson 	device_printf(sc->ec_dev, "EcWrite: failed waiting for sent data\n");
1035ef2374f7SNate Lawson 	return (status);
103615e32d5dSMike Smith     }
103715e32d5dSMike Smith 
103815e32d5dSMike Smith     return (AE_OK);
103915e32d5dSMike Smith }
1040