xref: /freebsd/sys/dev/acpica/acpi_ec.c (revision 1a305bda15c1153ab79dd8e9bff792be9a3c8150)
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>
3515e32d5dSMike Smith #include <sys/bus.h>
364e7f640dSJohn Baldwin #include <sys/lock.h>
370025fb0fSNate Lawson #include <sys/malloc.h>
38b0eefa38SNate Lawson #include <sys/module.h>
39b0eefa38SNate Lawson #include <sys/sx.h>
4015e32d5dSMike Smith 
4115e32d5dSMike Smith #include <machine/bus.h>
4215e32d5dSMike Smith #include <machine/resource.h>
4315e32d5dSMike Smith #include <sys/rman.h>
4415e32d5dSMike Smith 
45129d3046SJung-uk Kim #include <contrib/dev/acpica/include/acpi.h>
46129d3046SJung-uk Kim #include <contrib/dev/acpica/include/accommon.h>
47129d3046SJung-uk Kim 
4815e32d5dSMike Smith #include <dev/acpica/acpivar.h>
4915e32d5dSMike Smith 
50f4b7de15SNate Lawson /* Hooks for the ACPI CA debugging infrastructure */
51a9cf0dffSMike Smith #define _COMPONENT	ACPI_EC
52dbd0058aSMike Smith ACPI_MODULE_NAME("EC")
530ae55423SMike Smith 
54da3b867eSMike Smith /*
55da3b867eSMike Smith  * EC_COMMAND:
56da3b867eSMike Smith  * -----------
57da3b867eSMike Smith  */
58da3b867eSMike Smith typedef UINT8				EC_COMMAND;
59da3b867eSMike Smith 
60da3b867eSMike Smith #define EC_COMMAND_UNKNOWN		((EC_COMMAND) 0x00)
61da3b867eSMike Smith #define EC_COMMAND_READ			((EC_COMMAND) 0x80)
62da3b867eSMike Smith #define EC_COMMAND_WRITE		((EC_COMMAND) 0x81)
63da3b867eSMike Smith #define EC_COMMAND_BURST_ENABLE		((EC_COMMAND) 0x82)
64da3b867eSMike Smith #define EC_COMMAND_BURST_DISABLE	((EC_COMMAND) 0x83)
65da3b867eSMike Smith #define EC_COMMAND_QUERY		((EC_COMMAND) 0x84)
66da3b867eSMike Smith 
67da3b867eSMike Smith /*
68da3b867eSMike Smith  * EC_STATUS:
69da3b867eSMike Smith  * ----------
70da3b867eSMike Smith  * The encoding of the EC status register is illustrated below.
71da3b867eSMike Smith  * Note that a set bit (1) indicates the property is TRUE
72da3b867eSMike Smith  * (e.g. if bit 0 is set then the output buffer is full).
73da3b867eSMike Smith  * +-+-+-+-+-+-+-+-+
74da3b867eSMike Smith  * |7|6|5|4|3|2|1|0|
75da3b867eSMike Smith  * +-+-+-+-+-+-+-+-+
76da3b867eSMike Smith  *  | | | | | | | |
77da3b867eSMike Smith  *  | | | | | | | +- Output Buffer Full?
78da3b867eSMike Smith  *  | | | | | | +--- Input Buffer Full?
79da3b867eSMike Smith  *  | | | | | +----- <reserved>
80da3b867eSMike Smith  *  | | | | +------- Data Register is Command Byte?
81da3b867eSMike Smith  *  | | | +--------- Burst Mode Enabled?
82da3b867eSMike Smith  *  | | +----------- SCI Event?
83da3b867eSMike Smith  *  | +------------- SMI Event?
84ef2374f7SNate Lawson  *  +--------------- <reserved>
85da3b867eSMike Smith  *
86da3b867eSMike Smith  */
87da3b867eSMike Smith typedef UINT8				EC_STATUS;
88da3b867eSMike Smith 
89da3b867eSMike Smith #define EC_FLAG_OUTPUT_BUFFER		((EC_STATUS) 0x01)
90da3b867eSMike Smith #define EC_FLAG_INPUT_BUFFER		((EC_STATUS) 0x02)
91ef2374f7SNate Lawson #define EC_FLAG_DATA_IS_CMD		((EC_STATUS) 0x08)
92da3b867eSMike Smith #define EC_FLAG_BURST_MODE		((EC_STATUS) 0x10)
93da3b867eSMike Smith 
94da3b867eSMike Smith /*
95da3b867eSMike Smith  * EC_EVENT:
96da3b867eSMike Smith  * ---------
97da3b867eSMike Smith  */
98da3b867eSMike Smith typedef UINT8				EC_EVENT;
99da3b867eSMike Smith 
100da3b867eSMike Smith #define EC_EVENT_UNKNOWN		((EC_EVENT) 0x00)
101da3b867eSMike Smith #define EC_EVENT_OUTPUT_BUFFER_FULL	((EC_EVENT) 0x01)
102da3b867eSMike Smith #define EC_EVENT_INPUT_BUFFER_EMPTY	((EC_EVENT) 0x02)
103da3b867eSMike Smith #define EC_EVENT_SCI			((EC_EVENT) 0x20)
104ef2374f7SNate Lawson #define EC_EVENT_SMI			((EC_EVENT) 0x40)
105ef2374f7SNate Lawson 
106ef2374f7SNate Lawson /* Data byte returned after burst enable indicating it was successful. */
107ef2374f7SNate Lawson #define EC_BURST_ACK			0x90
108da3b867eSMike Smith 
109da3b867eSMike Smith /*
110da3b867eSMike Smith  * Register access primitives
111da3b867eSMike Smith  */
112da3b867eSMike Smith #define EC_GET_DATA(sc)							\
113da3b867eSMike Smith 	bus_space_read_1((sc)->ec_data_tag, (sc)->ec_data_handle, 0)
114da3b867eSMike Smith 
115da3b867eSMike Smith #define EC_SET_DATA(sc, v)						\
116da3b867eSMike Smith 	bus_space_write_1((sc)->ec_data_tag, (sc)->ec_data_handle, 0, (v))
117da3b867eSMike Smith 
118da3b867eSMike Smith #define EC_GET_CSR(sc)							\
119da3b867eSMike Smith 	bus_space_read_1((sc)->ec_csr_tag, (sc)->ec_csr_handle, 0)
120da3b867eSMike Smith 
121da3b867eSMike Smith #define EC_SET_CSR(sc, v)						\
122da3b867eSMike Smith 	bus_space_write_1((sc)->ec_csr_tag, (sc)->ec_csr_handle, 0, (v))
123da3b867eSMike Smith 
1240025fb0fSNate Lawson /* Additional params to pass from the probe routine */
1250025fb0fSNate Lawson struct acpi_ec_params {
1260025fb0fSNate Lawson     int		glk;
1270025fb0fSNate Lawson     int		gpe_bit;
1280025fb0fSNate Lawson     ACPI_HANDLE	gpe_handle;
1290025fb0fSNate Lawson     int		uid;
1300025fb0fSNate Lawson };
1310025fb0fSNate Lawson 
132da3b867eSMike Smith /*
133da3b867eSMike Smith  * Driver softc.
134da3b867eSMike Smith  */
13515e32d5dSMike Smith struct acpi_ec_softc {
13615e32d5dSMike Smith     device_t		ec_dev;
13715e32d5dSMike Smith     ACPI_HANDLE		ec_handle;
1380025fb0fSNate Lawson     int			ec_uid;
1390025fb0fSNate Lawson     ACPI_HANDLE		ec_gpehandle;
1403a371f32SNate Lawson     UINT8		ec_gpebit;
14115e32d5dSMike Smith 
14215e32d5dSMike Smith     int			ec_data_rid;
14315e32d5dSMike Smith     struct resource	*ec_data_res;
14415e32d5dSMike Smith     bus_space_tag_t	ec_data_tag;
14515e32d5dSMike Smith     bus_space_handle_t	ec_data_handle;
14615e32d5dSMike Smith 
14715e32d5dSMike Smith     int			ec_csr_rid;
14815e32d5dSMike Smith     struct resource	*ec_csr_res;
14915e32d5dSMike Smith     bus_space_tag_t	ec_csr_tag;
15015e32d5dSMike Smith     bus_space_handle_t	ec_csr_handle;
15115e32d5dSMike Smith 
1521f04e8f5SNate Lawson     int			ec_glk;
1531f04e8f5SNate Lawson     int			ec_glkhandle;
154ef2374f7SNate Lawson     int			ec_burstactive;
155ef2374f7SNate Lawson     int			ec_sci_pend;
1565ff14fa9SAndriy Gapon     volatile u_int	ec_gencount;
1570bfeadedSTakanori Watanabe     int			ec_suspending;
15815e32d5dSMike Smith };
15915e32d5dSMike Smith 
1601f04e8f5SNate Lawson /*
161f4b7de15SNate Lawson  * XXX njl
1621f04e8f5SNate Lawson  * I couldn't find it in the spec but other implementations also use a
1631f04e8f5SNate Lawson  * value of 1 ms for the time to acquire global lock.
1641f04e8f5SNate Lawson  */
1651f04e8f5SNate Lawson #define EC_LOCK_TIMEOUT	1000
1664690674eSMitsuru IWASAKI 
167ef2374f7SNate Lawson /* Default delay in microseconds between each run of the status polling loop. */
1685ff14fa9SAndriy Gapon #define EC_POLL_DELAY	50
169ef2374f7SNate Lawson 
170ef2374f7SNate Lawson /* Total time in ms spent waiting for a response from EC. */
17183dcc133SNate Lawson #define EC_TIMEOUT	750
17215e32d5dSMike Smith 
173ff40920eSNate Lawson #define EVENT_READY(event, status)			\
174ff40920eSNate Lawson 	(((event) == EC_EVENT_OUTPUT_BUFFER_FULL &&	\
175ff40920eSNate Lawson 	 ((status) & EC_FLAG_OUTPUT_BUFFER) != 0) ||	\
176ff40920eSNate Lawson 	 ((event) == EC_EVENT_INPUT_BUFFER_EMPTY && 	\
177ff40920eSNate Lawson 	 ((status) & EC_FLAG_INPUT_BUFFER) == 0))
178ff40920eSNate Lawson 
179f4b7de15SNate Lawson ACPI_SERIAL_DECL(ec, "ACPI embedded controller");
180f4b7de15SNate Lawson 
1816472ac3dSEd Schouten static SYSCTL_NODE(_debug_acpi, OID_AUTO, ec, CTLFLAG_RD, NULL, "EC debugging");
182ef2374f7SNate Lawson 
183675e5627SNate Lawson static int	ec_burst_mode;
184af3b2549SHans Petter Selasky SYSCTL_INT(_debug_acpi_ec, OID_AUTO, burst, CTLFLAG_RWTUN, &ec_burst_mode, 0,
185ef2374f7SNate Lawson     "Enable use of burst mode (faster for nearly all systems)");
18683dcc133SNate Lawson static int	ec_polled_mode;
187af3b2549SHans Petter Selasky SYSCTL_INT(_debug_acpi_ec, OID_AUTO, polled, CTLFLAG_RWTUN, &ec_polled_mode, 0,
18883dcc133SNate Lawson     "Force use of polled mode (only if interrupt mode doesn't work)");
189ef2374f7SNate Lawson static int	ec_timeout = EC_TIMEOUT;
190af3b2549SHans Petter Selasky SYSCTL_INT(_debug_acpi_ec, OID_AUTO, timeout, CTLFLAG_RWTUN, &ec_timeout,
191ef2374f7SNate Lawson     EC_TIMEOUT, "Total time spent waiting for a response (poll+sleep)");
192ef2374f7SNate Lawson 
19383dcc133SNate Lawson static ACPI_STATUS
19483dcc133SNate Lawson EcLock(struct acpi_ec_softc *sc)
19515e32d5dSMike Smith {
196f4b7de15SNate Lawson     ACPI_STATUS	status;
19715e32d5dSMike Smith 
19835440dd3SNate Lawson     /* If _GLK is non-zero, acquire the global lock. */
19935440dd3SNate Lawson     status = AE_OK;
20035440dd3SNate Lawson     if (sc->ec_glk) {
20135440dd3SNate Lawson 	status = AcpiAcquireGlobalLock(EC_LOCK_TIMEOUT, &sc->ec_glkhandle);
20235440dd3SNate Lawson 	if (ACPI_FAILURE(status))
20335440dd3SNate Lawson 	    return (status);
20435440dd3SNate Lawson     }
205f4b7de15SNate Lawson     ACPI_SERIAL_BEGIN(ec);
20615e32d5dSMike Smith     return (status);
20715e32d5dSMike Smith }
20815e32d5dSMike Smith 
20983dcc133SNate Lawson static void
21015e32d5dSMike Smith EcUnlock(struct acpi_ec_softc *sc)
21115e32d5dSMike Smith {
212f4b7de15SNate Lawson     ACPI_SERIAL_END(ec);
21335440dd3SNate Lawson     if (sc->ec_glk)
21435440dd3SNate Lawson 	AcpiReleaseGlobalLock(sc->ec_glkhandle);
21515e32d5dSMike Smith }
21615e32d5dSMike Smith 
2175a77b11bSJung-uk Kim static UINT32		EcGpeHandler(ACPI_HANDLE, UINT32, void *);
21815e32d5dSMike Smith static ACPI_STATUS	EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function,
21915e32d5dSMike Smith 				void *Context, void **return_Context);
220464c662eSNate Lawson static ACPI_STATUS	EcSpaceHandler(UINT32 Function,
221464c662eSNate Lawson 				ACPI_PHYSICAL_ADDRESS Address,
222e115a49fSJung-uk Kim 				UINT32 Width, UINT64 *Value,
22315e32d5dSMike Smith 				void *Context, void *RegionContext);
22483dcc133SNate Lawson static ACPI_STATUS	EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event,
22583dcc133SNate Lawson 				u_int gen_count);
2261f04e8f5SNate Lawson static ACPI_STATUS	EcCommand(struct acpi_ec_softc *sc, EC_COMMAND cmd);
227464c662eSNate Lawson static ACPI_STATUS	EcRead(struct acpi_ec_softc *sc, UINT8 Address,
228464c662eSNate Lawson 				UINT8 *Data);
229464c662eSNate Lawson static ACPI_STATUS	EcWrite(struct acpi_ec_softc *sc, UINT8 Address,
230e115a49fSJung-uk Kim 				UINT8 Data);
23115e32d5dSMike Smith static int		acpi_ec_probe(device_t dev);
23215e32d5dSMike Smith static int		acpi_ec_attach(device_t dev);
2330bfeadedSTakanori Watanabe static int		acpi_ec_suspend(device_t dev);
2340bfeadedSTakanori Watanabe static int		acpi_ec_resume(device_t dev);
235340a7f6aSNate Lawson static int		acpi_ec_shutdown(device_t dev);
236e33bea8dSNate Lawson static int		acpi_ec_read_method(device_t dev, u_int addr,
2379a179dd8SJung-uk Kim 				UINT64 *val, int width);
238e33bea8dSNate Lawson static int		acpi_ec_write_method(device_t dev, u_int addr,
2399a179dd8SJung-uk Kim 				UINT64 val, int width);
24015e32d5dSMike Smith 
24115e32d5dSMike Smith static device_method_t acpi_ec_methods[] = {
24215e32d5dSMike Smith     /* Device interface */
24315e32d5dSMike Smith     DEVMETHOD(device_probe,	acpi_ec_probe),
24415e32d5dSMike Smith     DEVMETHOD(device_attach,	acpi_ec_attach),
2450bfeadedSTakanori Watanabe     DEVMETHOD(device_suspend,	acpi_ec_suspend),
2460bfeadedSTakanori Watanabe     DEVMETHOD(device_resume,	acpi_ec_resume),
247340a7f6aSNate Lawson     DEVMETHOD(device_shutdown,	acpi_ec_shutdown),
24815e32d5dSMike Smith 
249e33bea8dSNate Lawson     /* Embedded controller interface */
250e33bea8dSNate Lawson     DEVMETHOD(acpi_ec_read,	acpi_ec_read_method),
251e33bea8dSNate Lawson     DEVMETHOD(acpi_ec_write,	acpi_ec_write_method),
252e33bea8dSNate Lawson 
25361bfd867SSofian Brabez     DEVMETHOD_END
25415e32d5dSMike Smith };
25515e32d5dSMike Smith 
25615e32d5dSMike Smith static driver_t acpi_ec_driver = {
25715e32d5dSMike Smith     "acpi_ec",
25815e32d5dSMike Smith     acpi_ec_methods,
25915e32d5dSMike Smith     sizeof(struct acpi_ec_softc),
26015e32d5dSMike Smith };
26115e32d5dSMike Smith 
2623273b005SMike Smith static devclass_t acpi_ec_devclass;
26315e32d5dSMike Smith DRIVER_MODULE(acpi_ec, acpi, acpi_ec_driver, acpi_ec_devclass, 0, 0);
26464278df5SNate Lawson MODULE_DEPEND(acpi_ec, acpi, 1, 1, 1);
26515e32d5dSMike Smith 
26615e32d5dSMike Smith /*
267f8335e3aSNate Lawson  * Look for an ECDT and if we find one, set up default GPE and
268f8335e3aSNate Lawson  * space handlers to catch attempts to access EC space before
26915e32d5dSMike Smith  * we have a real driver instance in place.
27083dcc133SNate Lawson  *
27183dcc133SNate Lawson  * TODO: Some old Gateway laptops need us to fake up an ECDT or
27283dcc133SNate Lawson  * otherwise attach early so that _REG methods can run.
27315e32d5dSMike Smith  */
274f8335e3aSNate Lawson void
275f8335e3aSNate Lawson acpi_ec_ecdt_probe(device_t parent)
27615e32d5dSMike Smith {
277f8335e3aSNate Lawson     ACPI_TABLE_ECDT *ecdt;
278f8335e3aSNate Lawson     ACPI_STATUS	     status;
279f8335e3aSNate Lawson     device_t	     child;
280f8335e3aSNate Lawson     ACPI_HANDLE	     h;
2810025fb0fSNate Lawson     struct acpi_ec_params *params;
282f8335e3aSNate Lawson 
283b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
28415e32d5dSMike Smith 
285f8335e3aSNate Lawson     /* Find and validate the ECDT. */
2862be4e471SJung-uk Kim     status = AcpiGetTable(ACPI_SIG_ECDT, 1, (ACPI_TABLE_HEADER **)&ecdt);
287f8335e3aSNate Lawson     if (ACPI_FAILURE(status) ||
2882be4e471SJung-uk Kim 	ecdt->Control.BitWidth != 8 ||
2892be4e471SJung-uk Kim 	ecdt->Data.BitWidth != 8) {
290f8335e3aSNate Lawson 	return;
29115e32d5dSMike Smith     }
29215e32d5dSMike Smith 
293f8335e3aSNate Lawson     /* Create the child device with the given unit number. */
294404b0d10SJung-uk Kim     child = BUS_ADD_CHILD(parent, 3, "acpi_ec", ecdt->Uid);
295f8335e3aSNate Lawson     if (child == NULL) {
2960025fb0fSNate Lawson 	printf("%s: can't add child\n", __func__);
297f8335e3aSNate Lawson 	return;
298f8335e3aSNate Lawson     }
299f8335e3aSNate Lawson 
300f8335e3aSNate Lawson     /* Find and save the ACPI handle for this device. */
3012be4e471SJung-uk Kim     status = AcpiGetHandle(NULL, ecdt->Id, &h);
302f8335e3aSNate Lawson     if (ACPI_FAILURE(status)) {
303f8335e3aSNate Lawson 	device_delete_child(parent, child);
3040025fb0fSNate Lawson 	printf("%s: can't get handle\n", __func__);
305f8335e3aSNate Lawson 	return;
306f8335e3aSNate Lawson     }
307f8335e3aSNate Lawson     acpi_set_handle(child, h);
308f8335e3aSNate Lawson 
309f8335e3aSNate Lawson     /* Set the data and CSR register addresses. */
3102be4e471SJung-uk Kim     bus_set_resource(child, SYS_RES_IOPORT, 0, ecdt->Data.Address,
311f8335e3aSNate Lawson 	/*count*/1);
3122be4e471SJung-uk Kim     bus_set_resource(child, SYS_RES_IOPORT, 1, ecdt->Control.Address,
313f8335e3aSNate Lawson 	/*count*/1);
314f8335e3aSNate Lawson 
31515e32d5dSMike Smith     /*
316f8335e3aSNate Lawson      * Store values for the probe/attach routines to use.  Store the
317c868ac7dSNate Lawson      * ECDT GPE bit and set the global lock flag according to _GLK.
3180025fb0fSNate Lawson      * Note that it is not perfectly correct to be evaluating a method
3190025fb0fSNate Lawson      * before initializing devices, but in practice this function
3200025fb0fSNate Lawson      * should be safe to call at this point.
32115e32d5dSMike Smith      */
3220025fb0fSNate Lawson     params = malloc(sizeof(struct acpi_ec_params), M_TEMP, M_WAITOK | M_ZERO);
3230025fb0fSNate Lawson     params->gpe_handle = NULL;
3242be4e471SJung-uk Kim     params->gpe_bit = ecdt->Gpe;
3252be4e471SJung-uk Kim     params->uid = ecdt->Uid;
3260025fb0fSNate Lawson     acpi_GetInteger(h, "_GLK", &params->glk);
3270025fb0fSNate Lawson     acpi_set_private(child, params);
328f8335e3aSNate Lawson 
329f8335e3aSNate Lawson     /* Finish the attach process. */
330f8335e3aSNate Lawson     if (device_probe_and_attach(child) != 0)
331f8335e3aSNate Lawson 	device_delete_child(parent, child);
332f8335e3aSNate Lawson }
333f8335e3aSNate Lawson 
33415e32d5dSMike Smith static int
33515e32d5dSMike Smith acpi_ec_probe(device_t dev)
33615e32d5dSMike Smith {
3370025fb0fSNate Lawson     ACPI_BUFFER buf;
338f8335e3aSNate Lawson     ACPI_HANDLE h;
3390025fb0fSNate Lawson     ACPI_OBJECT *obj;
340f8335e3aSNate Lawson     ACPI_STATUS status;
341f8335e3aSNate Lawson     device_t	peer;
342f8335e3aSNate Lawson     char	desc[64];
343f6eb382cSAndriy Gapon     int		ecdt;
3440025fb0fSNate Lawson     int		ret;
3450025fb0fSNate Lawson     struct acpi_ec_params *params;
3465fcc8a58SNate Lawson     static char *ec_ids[] = { "PNP0C09", NULL };
3470ae55423SMike Smith 
348*1a305bdaSBen Widawsky     ret = ENXIO;
349*1a305bdaSBen Widawsky 
350c72508e2SNate Lawson     /* Check that this is a device and that EC is not disabled. */
351c72508e2SNate Lawson     if (acpi_get_type(dev) != ACPI_TYPE_DEVICE || acpi_disabled("ec"))
352*1a305bdaSBen Widawsky 	return (ret);
35315e32d5dSMike Smith 
354*1a305bdaSBen Widawsky     if (device_is_devclass_fixed(dev)) {
35515e32d5dSMike Smith 	/*
356*1a305bdaSBen Widawsky 	 * If probed via ECDT, set description and continue. Otherwise, we can
357*1a305bdaSBen Widawsky 	 * access the namespace and make sure this is not a duplicate probe.
35815e32d5dSMike Smith 	 */
359f6eb382cSAndriy Gapon         ecdt = 1;
360*1a305bdaSBen Widawsky         params = acpi_get_private(dev);
361*1a305bdaSBen Widawsky 	if (params != NULL)
3621f04e8f5SNate Lawson 	    ret = 0;
363*1a305bdaSBen Widawsky 
364*1a305bdaSBen Widawsky 	goto out;
365*1a305bdaSBen Widawsky     }
366*1a305bdaSBen Widawsky 
3675efca36fSTakanori Watanabe     ret = ACPI_ID_PROBE(device_get_parent(dev), dev, ec_ids, NULL);
3685efca36fSTakanori Watanabe     if (ret > 0)
369*1a305bdaSBen Widawsky 	return (ret);
370*1a305bdaSBen Widawsky 
371*1a305bdaSBen Widawsky     params = malloc(sizeof(struct acpi_ec_params), M_TEMP, M_WAITOK | M_ZERO);
372*1a305bdaSBen Widawsky 
373*1a305bdaSBen Widawsky     buf.Pointer = NULL;
374*1a305bdaSBen Widawsky     buf.Length = ACPI_ALLOCATE_BUFFER;
375f8335e3aSNate Lawson     h = acpi_get_handle(dev);
376f8335e3aSNate Lawson 
377f8335e3aSNate Lawson     /*
378*1a305bdaSBen Widawsky      * Read the unit ID to check for duplicate attach and the global lock value
379*1a305bdaSBen Widawsky      * to see if we should acquire it when accessing the EC.
380f8335e3aSNate Lawson      */
3810025fb0fSNate Lawson     status = acpi_GetInteger(h, "_UID", &params->uid);
382f8335e3aSNate Lawson     if (ACPI_FAILURE(status))
3830025fb0fSNate Lawson 	params->uid = 0;
384*1a305bdaSBen Widawsky 
3850025fb0fSNate Lawson     status = acpi_GetInteger(h, "_GLK", &params->glk);
386f8335e3aSNate Lawson     if (ACPI_FAILURE(status))
3870025fb0fSNate Lawson 	params->glk = 0;
388f8335e3aSNate Lawson 
389f8335e3aSNate Lawson     /*
390*1a305bdaSBen Widawsky      * Evaluate the _GPE method to find the GPE bit used by the EC to signal
391*1a305bdaSBen Widawsky      * status (SCI).  If it's a package, it contains a reference and GPE bit,
392*1a305bdaSBen Widawsky      * similar to _PRW.
393f8335e3aSNate Lawson      */
3940025fb0fSNate Lawson     status = AcpiEvaluateObject(h, "_GPE", NULL, &buf);
395f8335e3aSNate Lawson     if (ACPI_FAILURE(status)) {
396*1a305bdaSBen Widawsky 	device_printf(dev, "can't evaluate _GPE - %s\n", AcpiFormatException(status));
397a6761eb3SNate Lawson 	goto out;
398f8335e3aSNate Lawson     }
399*1a305bdaSBen Widawsky 
4000025fb0fSNate Lawson     obj = (ACPI_OBJECT *)buf.Pointer;
4010025fb0fSNate Lawson     if (obj == NULL)
402a6761eb3SNate Lawson 	goto out;
4030025fb0fSNate Lawson 
4040025fb0fSNate Lawson     switch (obj->Type) {
4050025fb0fSNate Lawson     case ACPI_TYPE_INTEGER:
4060025fb0fSNate Lawson 	params->gpe_handle = NULL;
4070025fb0fSNate Lawson 	params->gpe_bit = obj->Integer.Value;
4080025fb0fSNate Lawson 	break;
4090025fb0fSNate Lawson     case ACPI_TYPE_PACKAGE:
4100025fb0fSNate Lawson 	if (!ACPI_PKG_VALID(obj, 2))
4110025fb0fSNate Lawson 	    goto out;
412*1a305bdaSBen Widawsky 	params->gpe_handle = acpi_GetReference(NULL, &obj->Package.Elements[0]);
4130025fb0fSNate Lawson 	if (params->gpe_handle == NULL ||
4140025fb0fSNate Lawson 	    acpi_PkgInt32(obj, 1, &params->gpe_bit) != 0)
4150025fb0fSNate Lawson 		goto out;
4160025fb0fSNate Lawson 	break;
4170025fb0fSNate Lawson     default:
4180025fb0fSNate Lawson 	device_printf(dev, "_GPE has invalid type %d\n", obj->Type);
4190025fb0fSNate Lawson 	goto out;
4200025fb0fSNate Lawson     }
421f8335e3aSNate Lawson 
422f8335e3aSNate Lawson     /* Store the values we got from the namespace for attach. */
4230025fb0fSNate Lawson     acpi_set_private(dev, params);
424f8335e3aSNate Lawson 
425f8335e3aSNate Lawson     /*
426*1a305bdaSBen Widawsky      * Check for a duplicate probe. This can happen when a probe via ECDT
427*1a305bdaSBen Widawsky      * succeeded already. If this is a duplicate, disable this device.
428f8335e3aSNate Lawson      */
4290025fb0fSNate Lawson     peer = devclass_get_device(acpi_ec_devclass, params->uid);
430*1a305bdaSBen Widawsky     if (peer == NULL || !device_is_alive(peer))
431*1a305bdaSBen Widawsky 	ret = 0;
432*1a305bdaSBen Widawsky     else
433c868ac7dSNate Lawson 	device_disable(dev);
434f8335e3aSNate Lawson 
435*1a305bdaSBen Widawsky     if (buf.Pointer)
436*1a305bdaSBen Widawsky 	AcpiOsFree(buf.Pointer);
4370025fb0fSNate Lawson out:
4385efca36fSTakanori Watanabe     if (ret <= 0) {
439c868ac7dSNate Lawson 	snprintf(desc, sizeof(desc), "Embedded Controller: GPE %#x%s%s",
4400025fb0fSNate Lawson 		 params->gpe_bit, (params->glk) ? ", GLK" : "",
441f6eb382cSAndriy Gapon 		 ecdt ? ", ECDT" : "");
442c868ac7dSNate Lawson 	device_set_desc_copy(dev, desc);
4432f977ab4SJung-uk Kim     } else
4440025fb0fSNate Lawson 	free(params, M_TEMP);
445*1a305bdaSBen Widawsky 
4461f04e8f5SNate Lawson     return (ret);
44715e32d5dSMike Smith }
44815e32d5dSMike Smith 
44915e32d5dSMike Smith static int
45015e32d5dSMike Smith acpi_ec_attach(device_t dev)
45115e32d5dSMike Smith {
45215e32d5dSMike Smith     struct acpi_ec_softc	*sc;
4530025fb0fSNate Lawson     struct acpi_ec_params	*params;
45415e32d5dSMike Smith     ACPI_STATUS			Status;
455dbd0058aSMike Smith 
456b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
4570ae55423SMike Smith 
4581f04e8f5SNate Lawson     /* Fetch/initialize softc (assumes softc is pre-zeroed). */
45915e32d5dSMike Smith     sc = device_get_softc(dev);
4600025fb0fSNate Lawson     params = acpi_get_private(dev);
46115e32d5dSMike Smith     sc->ec_dev = dev;
46215e32d5dSMike Smith     sc->ec_handle = acpi_get_handle(dev);
46315e32d5dSMike Smith 
464f8335e3aSNate Lawson     /* Retrieve previously probed values via device ivars. */
4650025fb0fSNate Lawson     sc->ec_glk = params->glk;
4660025fb0fSNate Lawson     sc->ec_gpebit = params->gpe_bit;
4670025fb0fSNate Lawson     sc->ec_gpehandle = params->gpe_handle;
4680025fb0fSNate Lawson     sc->ec_uid = params->uid;
46968fb6c48STakanori Watanabe     sc->ec_suspending = FALSE;
470943124d3SAndriy Gapon     acpi_set_private(dev, NULL);
4710025fb0fSNate Lawson     free(params, M_TEMP);
472f8335e3aSNate Lawson 
4731f04e8f5SNate Lawson     /* Attach bus resources for data and command/status ports. */
47415e32d5dSMike Smith     sc->ec_data_rid = 0;
4755f96beb9SNate Lawson     sc->ec_data_res = bus_alloc_resource_any(sc->ec_dev, SYS_RES_IOPORT,
4765f96beb9SNate Lawson 			&sc->ec_data_rid, RF_ACTIVE);
477464c662eSNate Lawson     if (sc->ec_data_res == NULL) {
47815e32d5dSMike Smith 	device_printf(dev, "can't allocate data port\n");
479f3fc4f8bSNate Lawson 	goto error;
48015e32d5dSMike Smith     }
48115e32d5dSMike Smith     sc->ec_data_tag = rman_get_bustag(sc->ec_data_res);
48215e32d5dSMike Smith     sc->ec_data_handle = rman_get_bushandle(sc->ec_data_res);
48315e32d5dSMike Smith 
48415e32d5dSMike Smith     sc->ec_csr_rid = 1;
4855f96beb9SNate Lawson     sc->ec_csr_res = bus_alloc_resource_any(sc->ec_dev, SYS_RES_IOPORT,
4865f96beb9SNate Lawson 			&sc->ec_csr_rid, RF_ACTIVE);
487464c662eSNate Lawson     if (sc->ec_csr_res == NULL) {
48815e32d5dSMike Smith 	device_printf(dev, "can't allocate command/status port\n");
489f3fc4f8bSNate Lawson 	goto error;
49015e32d5dSMike Smith     }
49115e32d5dSMike Smith     sc->ec_csr_tag = rman_get_bustag(sc->ec_csr_res);
49215e32d5dSMike Smith     sc->ec_csr_handle = rman_get_bushandle(sc->ec_csr_res);
49315e32d5dSMike Smith 
49415e32d5dSMike Smith     /*
4951f04e8f5SNate Lawson      * Install a handler for this EC's GPE bit.  We want edge-triggered
4961f04e8f5SNate Lawson      * behavior.
49715e32d5dSMike Smith      */
4981f04e8f5SNate Lawson     ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "attaching GPE handler\n"));
4990025fb0fSNate Lawson     Status = AcpiInstallGpeHandler(sc->ec_gpehandle, sc->ec_gpebit,
5005a77b11bSJung-uk Kim 		ACPI_GPE_EDGE_TRIGGERED, EcGpeHandler, sc);
501464c662eSNate Lawson     if (ACPI_FAILURE(Status)) {
50276d1dff4SMike Smith 	device_printf(dev, "can't install GPE handler for %s - %s\n",
503bfae45aaSMike Smith 		      acpi_name(sc->ec_handle), AcpiFormatException(Status));
504f3fc4f8bSNate Lawson 	goto error;
50515e32d5dSMike Smith     }
50615e32d5dSMike Smith 
50715e32d5dSMike Smith     /*
50815e32d5dSMike Smith      * Install address space handler
50915e32d5dSMike Smith      */
5104c1cdee6SMike Smith     ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "attaching address space handler\n"));
511464c662eSNate Lawson     Status = AcpiInstallAddressSpaceHandler(sc->ec_handle, ACPI_ADR_SPACE_EC,
5121f04e8f5SNate Lawson 		&EcSpaceHandler, &EcSpaceSetup, sc);
513464c662eSNate Lawson     if (ACPI_FAILURE(Status)) {
51476d1dff4SMike Smith 	device_printf(dev, "can't install address space handler for %s - %s\n",
515bfae45aaSMike Smith 		      acpi_name(sc->ec_handle), AcpiFormatException(Status));
516f3fc4f8bSNate Lawson 	goto error;
517f3fc4f8bSNate Lawson     }
518f3fc4f8bSNate Lawson 
519f3fc4f8bSNate Lawson     /* Enable runtime GPEs for the handler. */
520a88e22b7SJung-uk Kim     Status = AcpiEnableGpe(sc->ec_gpehandle, sc->ec_gpebit);
521f3fc4f8bSNate Lawson     if (ACPI_FAILURE(Status)) {
522f3fc4f8bSNate Lawson 	device_printf(dev, "AcpiEnableGpe failed: %s\n",
523f3fc4f8bSNate Lawson 		      AcpiFormatException(Status));
524f3fc4f8bSNate Lawson 	goto error;
52515e32d5dSMike Smith     }
5261f04e8f5SNate Lawson 
5271f04e8f5SNate Lawson     ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "acpi_ec_attach complete\n"));
5281f04e8f5SNate Lawson     return (0);
529464c662eSNate Lawson 
530f3fc4f8bSNate Lawson error:
5315a77b11bSJung-uk Kim     AcpiRemoveGpeHandler(sc->ec_gpehandle, sc->ec_gpebit, EcGpeHandler);
532f3fc4f8bSNate Lawson     AcpiRemoveAddressSpaceHandler(sc->ec_handle, ACPI_ADR_SPACE_EC,
533f3fc4f8bSNate Lawson 	EcSpaceHandler);
534f8372adeSTakanori Watanabe     if (sc->ec_csr_res)
535f8372adeSTakanori Watanabe 	bus_release_resource(sc->ec_dev, SYS_RES_IOPORT, sc->ec_csr_rid,
536f8372adeSTakanori Watanabe 			     sc->ec_csr_res);
537f8372adeSTakanori Watanabe     if (sc->ec_data_res)
538f8372adeSTakanori Watanabe 	bus_release_resource(sc->ec_dev, SYS_RES_IOPORT, sc->ec_data_rid,
539f8372adeSTakanori Watanabe 			     sc->ec_data_res);
540f3fc4f8bSNate Lawson     return (ENXIO);
5411f04e8f5SNate Lawson }
5421f04e8f5SNate Lawson 
543340a7f6aSNate Lawson static int
5440bfeadedSTakanori Watanabe acpi_ec_suspend(device_t dev)
5450bfeadedSTakanori Watanabe {
5460bfeadedSTakanori Watanabe     struct acpi_ec_softc	*sc;
5470bfeadedSTakanori Watanabe 
5480bfeadedSTakanori Watanabe     sc = device_get_softc(dev);
54968fb6c48STakanori Watanabe     sc->ec_suspending = TRUE;
5500bfeadedSTakanori Watanabe     return (0);
5510bfeadedSTakanori Watanabe }
5520bfeadedSTakanori Watanabe 
5530bfeadedSTakanori Watanabe static int
5540bfeadedSTakanori Watanabe acpi_ec_resume(device_t dev)
5550bfeadedSTakanori Watanabe {
5560bfeadedSTakanori Watanabe     struct acpi_ec_softc	*sc;
5570bfeadedSTakanori Watanabe 
5580bfeadedSTakanori Watanabe     sc = device_get_softc(dev);
55968fb6c48STakanori Watanabe     sc->ec_suspending = FALSE;
5600bfeadedSTakanori Watanabe     return (0);
5610bfeadedSTakanori Watanabe }
5620bfeadedSTakanori Watanabe 
5630bfeadedSTakanori Watanabe static int
564340a7f6aSNate Lawson acpi_ec_shutdown(device_t dev)
565340a7f6aSNate Lawson {
566340a7f6aSNate Lawson     struct acpi_ec_softc	*sc;
567340a7f6aSNate Lawson 
568340a7f6aSNate Lawson     /* Disable the GPE so we don't get EC events during shutdown. */
569340a7f6aSNate Lawson     sc = device_get_softc(dev);
570a88e22b7SJung-uk Kim     AcpiDisableGpe(sc->ec_gpehandle, sc->ec_gpebit);
571340a7f6aSNate Lawson     return (0);
572340a7f6aSNate Lawson }
573340a7f6aSNate Lawson 
574e33bea8dSNate Lawson /* Methods to allow other devices (e.g., smbat) to read/write EC space. */
575e33bea8dSNate Lawson static int
5769a179dd8SJung-uk Kim acpi_ec_read_method(device_t dev, u_int addr, UINT64 *val, int width)
577e33bea8dSNate Lawson {
578e33bea8dSNate Lawson     struct acpi_ec_softc *sc;
579e33bea8dSNate Lawson     ACPI_STATUS status;
580e33bea8dSNate Lawson 
581e33bea8dSNate Lawson     sc = device_get_softc(dev);
582e33bea8dSNate Lawson     status = EcSpaceHandler(ACPI_READ, addr, width * 8, val, sc, NULL);
583e33bea8dSNate Lawson     if (ACPI_FAILURE(status))
584e33bea8dSNate Lawson 	return (ENXIO);
585e33bea8dSNate Lawson     return (0);
586e33bea8dSNate Lawson }
587e33bea8dSNate Lawson 
588e33bea8dSNate Lawson static int
5899a179dd8SJung-uk Kim acpi_ec_write_method(device_t dev, u_int addr, UINT64 val, int width)
590e33bea8dSNate Lawson {
591e33bea8dSNate Lawson     struct acpi_ec_softc *sc;
592e33bea8dSNate Lawson     ACPI_STATUS status;
593e33bea8dSNate Lawson 
594e33bea8dSNate Lawson     sc = device_get_softc(dev);
595e33bea8dSNate Lawson     status = EcSpaceHandler(ACPI_WRITE, addr, width * 8, &val, sc, NULL);
596e33bea8dSNate Lawson     if (ACPI_FAILURE(status))
597e33bea8dSNate Lawson 	return (ENXIO);
598e33bea8dSNate Lawson     return (0);
599e33bea8dSNate Lawson }
600e33bea8dSNate Lawson 
6015ff14fa9SAndriy Gapon static ACPI_STATUS
6025ff14fa9SAndriy Gapon EcCheckStatus(struct acpi_ec_softc *sc, const char *msg, EC_EVENT event)
6035ff14fa9SAndriy Gapon {
6045ff14fa9SAndriy Gapon     ACPI_STATUS status;
6055ff14fa9SAndriy Gapon     EC_STATUS ec_status;
6065ff14fa9SAndriy Gapon 
6075ff14fa9SAndriy Gapon     status = AE_NO_HARDWARE_RESPONSE;
6085ff14fa9SAndriy Gapon     ec_status = EC_GET_CSR(sc);
6095ff14fa9SAndriy Gapon     if (sc->ec_burstactive && !(ec_status & EC_FLAG_BURST_MODE)) {
6105ff14fa9SAndriy Gapon 	CTR1(KTR_ACPI, "ec burst disabled in waitevent (%s)", msg);
6115ff14fa9SAndriy Gapon 	sc->ec_burstactive = FALSE;
6125ff14fa9SAndriy Gapon     }
6135ff14fa9SAndriy Gapon     if (EVENT_READY(event, ec_status)) {
6145ff14fa9SAndriy Gapon 	CTR2(KTR_ACPI, "ec %s wait ready, status %#x", msg, ec_status);
6155ff14fa9SAndriy Gapon 	status = AE_OK;
6165ff14fa9SAndriy Gapon     }
6175ff14fa9SAndriy Gapon     return (status);
6185ff14fa9SAndriy Gapon }
6195ff14fa9SAndriy Gapon 
62015e32d5dSMike Smith static void
621c6e6b4feSHans Petter Selasky EcGpeQueryHandlerSub(struct acpi_ec_softc *sc)
62215e32d5dSMike Smith {
62315e32d5dSMike Smith     UINT8			Data;
62415e32d5dSMike Smith     ACPI_STATUS			Status;
6254552fccbSJung-uk Kim     int				retry;
62615e32d5dSMike Smith     char			qxx[5];
62715e32d5dSMike Smith 
628b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
62915e32d5dSMike Smith 
630ef2374f7SNate Lawson     /* Serialize user access with EcSpaceHandler(). */
63183dcc133SNate Lawson     Status = EcLock(sc);
6323a371f32SNate Lawson     if (ACPI_FAILURE(Status)) {
63383dcc133SNate Lawson 	device_printf(sc->ec_dev, "GpeQuery lock error: %s\n",
63483dcc133SNate Lawson 	    AcpiFormatException(Status));
6353a371f32SNate Lawson 	return;
6363a371f32SNate Lawson     }
6373a371f32SNate Lawson 
63815e32d5dSMike Smith     /*
6393a371f32SNate Lawson      * Send a query command to the EC to find out which _Qxx call it
6403a371f32SNate Lawson      * wants to make.  This command clears the SCI bit and also the
64183dcc133SNate Lawson      * interrupt source since we are edge-triggered.  To prevent the GPE
64283dcc133SNate Lawson      * that may arise from running the query from causing another query
64383dcc133SNate Lawson      * to be queued, we clear the pending flag only after running it.
6443a371f32SNate Lawson      */
6455ff14fa9SAndriy Gapon     for (retry = 0; retry < 2; retry++) {
6463a371f32SNate Lawson 	Status = EcCommand(sc, EC_COMMAND_QUERY);
6475ff14fa9SAndriy Gapon 	if (ACPI_SUCCESS(Status))
6485ff14fa9SAndriy Gapon 	    break;
649ae1ef64fSJung-uk Kim 	if (ACPI_FAILURE(EcCheckStatus(sc, "retr_check",
6509ad56977SJung-uk Kim 	    EC_EVENT_INPUT_BUFFER_EMPTY)))
6515ff14fa9SAndriy Gapon 	    break;
6525ff14fa9SAndriy Gapon     }
653dbd0058aSMike Smith     if (ACPI_FAILURE(Status)) {
6543a371f32SNate Lawson 	EcUnlock(sc);
65583dcc133SNate Lawson 	device_printf(sc->ec_dev, "GPE query failed: %s\n",
65683dcc133SNate Lawson 	    AcpiFormatException(Status));
65783dcc133SNate Lawson 	return;
65815e32d5dSMike Smith     }
6593a371f32SNate Lawson     Data = EC_GET_DATA(sc);
660ef2374f7SNate Lawson 
66183dcc133SNate Lawson     /*
66283dcc133SNate Lawson      * We have to unlock before running the _Qxx method below since that
66383dcc133SNate Lawson      * method may attempt to read/write from EC address space, causing
66483dcc133SNate Lawson      * recursive acquisition of the lock.
66583dcc133SNate Lawson      */
6663a371f32SNate Lawson     EcUnlock(sc);
66715e32d5dSMike Smith 
6681f04e8f5SNate Lawson     /* Ignore the value for "no outstanding event". (13.3.5) */
66983dcc133SNate Lawson     CTR2(KTR_ACPI, "ec query ok,%s running _Q%02X", Data ? "" : " not", Data);
6701f04e8f5SNate Lawson     if (Data == 0)
67183dcc133SNate Lawson 	return;
6721f04e8f5SNate Lawson 
6731f04e8f5SNate Lawson     /* Evaluate _Qxx to respond to the controller. */
67483dcc133SNate Lawson     snprintf(qxx, sizeof(qxx), "_Q%02X", Data);
675b3919c8dSMark Santcroos     AcpiUtStrupr(qxx);
676ee785aa9SJohn Baldwin     Status = AcpiEvaluateObject(sc->ec_handle, qxx, NULL, NULL);
6771f04e8f5SNate Lawson     if (ACPI_FAILURE(Status) && Status != AE_NOT_FOUND) {
67883dcc133SNate Lawson 	device_printf(sc->ec_dev, "evaluation of query method %s failed: %s\n",
679bfae45aaSMike Smith 	    qxx, AcpiFormatException(Status));
68015e32d5dSMike Smith     }
681c07572e7STakanori Watanabe }
68242f6d122SMike Smith 
683c6e6b4feSHans Petter Selasky static void
684c6e6b4feSHans Petter Selasky EcGpeQueryHandler(void *Context)
685c6e6b4feSHans Petter Selasky {
686c6e6b4feSHans Petter Selasky     struct acpi_ec_softc *sc = (struct acpi_ec_softc *)Context;
687c6e6b4feSHans Petter Selasky     int pending;
688c6e6b4feSHans Petter Selasky 
689c6e6b4feSHans Petter Selasky     KASSERT(Context != NULL, ("EcGpeQueryHandler called with NULL"));
690c6e6b4feSHans Petter Selasky 
691c6e6b4feSHans Petter Selasky     do {
692c6e6b4feSHans Petter Selasky 	/* Read the current pending count */
693c6e6b4feSHans Petter Selasky 	pending = atomic_load_acq_int(&sc->ec_sci_pend);
694c6e6b4feSHans Petter Selasky 
695c6e6b4feSHans Petter Selasky 	/* Call GPE handler function */
696c6e6b4feSHans Petter Selasky 	EcGpeQueryHandlerSub(sc);
697c6e6b4feSHans Petter Selasky 
698c6e6b4feSHans Petter Selasky 	/*
699c6e6b4feSHans Petter Selasky 	 * Try to reset the pending count to zero. If this fails we
700c6e6b4feSHans Petter Selasky 	 * know another GPE event has occurred while handling the
701c6e6b4feSHans Petter Selasky 	 * current GPE event and need to loop.
702c6e6b4feSHans Petter Selasky 	 */
703c6e6b4feSHans Petter Selasky     } while (!atomic_cmpset_int(&sc->ec_sci_pend, pending, 0));
704c6e6b4feSHans Petter Selasky }
705c6e6b4feSHans Petter Selasky 
706a9cf0dffSMike Smith /*
70783dcc133SNate Lawson  * The GPE handler is called when IBE/OBF or SCI events occur.  We are
70883dcc133SNate Lawson  * called from an unknown lock context.
709a9cf0dffSMike Smith  */
7105a77b11bSJung-uk Kim static UINT32
7115a77b11bSJung-uk Kim EcGpeHandler(ACPI_HANDLE GpeDevice, UINT32 GpeNumber, void *Context)
712c07572e7STakanori Watanabe {
713c07572e7STakanori Watanabe     struct acpi_ec_softc *sc = Context;
7141f04e8f5SNate Lawson     ACPI_STATUS		       Status;
715ef2374f7SNate Lawson     EC_STATUS		       EcStatus;
716a9cf0dffSMike Smith 
7171f04e8f5SNate Lawson     KASSERT(Context != NULL, ("EcGpeHandler called with NULL"));
71883dcc133SNate Lawson     CTR0(KTR_ACPI, "ec gpe handler start");
7191f04e8f5SNate Lawson 
7201395b555SNate Lawson     /*
72183dcc133SNate Lawson      * Notify EcWaitEvent() that the status register is now fresh.  If we
72283dcc133SNate Lawson      * didn't do this, it wouldn't be possible to distinguish an old IBE
72383dcc133SNate Lawson      * from a new one, for example when doing a write transaction (writing
72483dcc133SNate Lawson      * address and then data values.)
7251395b555SNate Lawson      */
72683dcc133SNate Lawson     atomic_add_int(&sc->ec_gencount, 1);
72780b1151eSJung-uk Kim     wakeup(sc);
728ef2374f7SNate Lawson 
729ef2374f7SNate Lawson     /*
73083dcc133SNate Lawson      * If the EC_SCI bit of the status register is set, queue a query handler.
73183dcc133SNate Lawson      * It will run the query and _Qxx method later, under the lock.
732ef2374f7SNate Lawson      */
733ef2374f7SNate Lawson     EcStatus = EC_GET_CSR(sc);
734c6e6b4feSHans Petter Selasky     if ((EcStatus & EC_EVENT_SCI) &&
735c6e6b4feSHans Petter Selasky 	atomic_fetchadd_int(&sc->ec_sci_pend, 1) == 0) {
73683dcc133SNate Lawson 	CTR0(KTR_ACPI, "ec gpe queueing query handler");
7372be4e471SJung-uk Kim 	Status = AcpiOsExecute(OSL_GPE_HANDLER, EcGpeQueryHandler, Context);
738c6e6b4feSHans Petter Selasky 	if (ACPI_FAILURE(Status)) {
73983dcc133SNate Lawson 	    printf("EcGpeHandler: queuing GPE query handler failed\n");
740c6e6b4feSHans Petter Selasky 	    atomic_store_rel_int(&sc->ec_sci_pend, 0);
741c6e6b4feSHans Petter Selasky 	}
7426e141df2SNate Lawson     }
7435a77b11bSJung-uk Kim     return (ACPI_REENABLE_GPE);
74415e32d5dSMike Smith }
74515e32d5dSMike Smith 
74615e32d5dSMike Smith static ACPI_STATUS
747464c662eSNate Lawson EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function, void *Context,
748464c662eSNate Lawson 	     void **RegionContext)
74915e32d5dSMike Smith {
75042f6d122SMike Smith 
751b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
75242f6d122SMike Smith 
75315e32d5dSMike Smith     /*
75416844a97SNate Lawson      * If deactivating a region, always set the output to NULL.  Otherwise,
75516844a97SNate Lawson      * just pass the context through.
75615e32d5dSMike Smith      */
75716844a97SNate Lawson     if (Function == ACPI_REGION_DEACTIVATE)
75816844a97SNate Lawson 	*RegionContext = NULL;
75916844a97SNate Lawson     else
76015e32d5dSMike Smith 	*RegionContext = Context;
76115e32d5dSMike Smith 
76242f6d122SMike Smith     return_ACPI_STATUS (AE_OK);
76315e32d5dSMike Smith }
76415e32d5dSMike Smith 
76515e32d5dSMike Smith static ACPI_STATUS
766e115a49fSJung-uk Kim EcSpaceHandler(UINT32 Function, ACPI_PHYSICAL_ADDRESS Address, UINT32 Width,
7679a179dd8SJung-uk Kim 	       UINT64 *Value, void *Context, void *RegionContext)
76815e32d5dSMike Smith {
76915e32d5dSMike Smith     struct acpi_ec_softc	*sc = (struct acpi_ec_softc *)Context;
77034622ff1SJung-uk Kim     ACPI_PHYSICAL_ADDRESS	EcAddr;
771e115a49fSJung-uk Kim     UINT8			*EcData;
77234622ff1SJung-uk Kim     ACPI_STATUS			Status;
77315e32d5dSMike Smith 
774b4a05238SPeter Wemm     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, (UINT32)Address);
7750ae55423SMike Smith 
77634622ff1SJung-uk Kim     if (Function != ACPI_READ && Function != ACPI_WRITE)
77734622ff1SJung-uk Kim 	return_ACPI_STATUS (AE_BAD_PARAMETER);
778e115a49fSJung-uk Kim     if (Width % 8 != 0 || Value == NULL || Context == NULL)
7790ae55423SMike Smith 	return_ACPI_STATUS (AE_BAD_PARAMETER);
78034622ff1SJung-uk Kim     if (Address + Width / 8 > 256)
7814ed391b8SNate Lawson 	return_ACPI_STATUS (AE_BAD_ADDRESS);
78215e32d5dSMike Smith 
78383dcc133SNate Lawson     /*
78483dcc133SNate Lawson      * If booting, check if we need to run the query handler.  If so, we
78583dcc133SNate Lawson      * we call it directly here since our thread taskq is not active yet.
78683dcc133SNate Lawson      */
787c66d2b38SJung-uk Kim     if (cold || rebooting || sc->ec_suspending) {
788c6e6b4feSHans Petter Selasky 	if ((EC_GET_CSR(sc) & EC_EVENT_SCI) &&
789c6e6b4feSHans Petter Selasky 	    atomic_fetchadd_int(&sc->ec_sci_pend, 1) == 0) {
79083dcc133SNate Lawson 	    CTR0(KTR_ACPI, "ec running gpe handler directly");
79183dcc133SNate Lawson 	    EcGpeQueryHandler(sc);
79283dcc133SNate Lawson 	}
79383dcc133SNate Lawson     }
79483dcc133SNate Lawson 
79583dcc133SNate Lawson     /* Serialize with EcGpeQueryHandler() at transaction granularity. */
79683dcc133SNate Lawson     Status = EcLock(sc);
797464c662eSNate Lawson     if (ACPI_FAILURE(Status))
798f4b7de15SNate Lawson 	return_ACPI_STATUS (Status);
7991f04e8f5SNate Lawson 
80034622ff1SJung-uk Kim     /* If we can't start burst mode, continue anyway. */
80134622ff1SJung-uk Kim     Status = EcCommand(sc, EC_COMMAND_BURST_ENABLE);
80234622ff1SJung-uk Kim     if (ACPI_SUCCESS(Status)) {
80334622ff1SJung-uk Kim 	if (EC_GET_DATA(sc) == EC_BURST_ACK) {
80434622ff1SJung-uk Kim 	    CTR0(KTR_ACPI, "ec burst enabled");
80534622ff1SJung-uk Kim 	    sc->ec_burstactive = TRUE;
80634622ff1SJung-uk Kim 	}
80734622ff1SJung-uk Kim     }
80834622ff1SJung-uk Kim 
809e115a49fSJung-uk Kim     /* Perform the transaction(s), based on Width. */
81034622ff1SJung-uk Kim     EcAddr = Address;
81134622ff1SJung-uk Kim     EcData = (UINT8 *)Value;
81234622ff1SJung-uk Kim     if (Function == ACPI_READ)
81334622ff1SJung-uk Kim 	*Value = 0;
81434622ff1SJung-uk Kim     do {
8151f04e8f5SNate Lawson 	switch (Function) {
8161f04e8f5SNate Lawson 	case ACPI_READ:
817e115a49fSJung-uk Kim 	    Status = EcRead(sc, EcAddr, EcData);
818ee785aa9SJohn Baldwin 	    break;
8191f04e8f5SNate Lawson 	case ACPI_WRITE:
820e115a49fSJung-uk Kim 	    Status = EcWrite(sc, EcAddr, *EcData);
8211f04e8f5SNate Lawson 	    break;
8221f04e8f5SNate Lawson 	}
8231f04e8f5SNate Lawson 	if (ACPI_FAILURE(Status))
8244ed391b8SNate Lawson 	    break;
82534622ff1SJung-uk Kim 	EcAddr++;
82634622ff1SJung-uk Kim 	EcData++;
82734622ff1SJung-uk Kim     } while (EcAddr < Address + Width / 8);
82834622ff1SJung-uk Kim 
82934622ff1SJung-uk Kim     if (sc->ec_burstactive) {
83034622ff1SJung-uk Kim 	sc->ec_burstactive = FALSE;
83134622ff1SJung-uk Kim 	if (ACPI_SUCCESS(EcCommand(sc, EC_COMMAND_BURST_DISABLE)))
83234622ff1SJung-uk Kim 	    CTR0(KTR_ACPI, "ec disabled burst ok");
833ee785aa9SJohn Baldwin     }
8344ed391b8SNate Lawson 
835f4b7de15SNate Lawson     EcUnlock(sc);
8360ae55423SMike Smith     return_ACPI_STATUS (Status);
83715e32d5dSMike Smith }
8382a4ac806SMike Smith 
83915e32d5dSMike Smith static ACPI_STATUS
84083dcc133SNate Lawson EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event, u_int gen_count)
84183dcc133SNate Lawson {
8425ff14fa9SAndriy Gapon     static int	no_intr = 0;
8431f04e8f5SNate Lawson     ACPI_STATUS	Status;
8445ff14fa9SAndriy Gapon     int		count, i, need_poll, slp_ival;
84515e32d5dSMike Smith 
846f4b7de15SNate Lawson     ACPI_SERIAL_ASSERT(ec);
8471f04e8f5SNate Lawson     Status = AE_NO_HARDWARE_RESPONSE;
8485ff14fa9SAndriy Gapon     need_poll = cold || rebooting || ec_polled_mode || sc->ec_suspending;
84983dcc133SNate Lawson 
85083dcc133SNate Lawson     /* Wait for event by polling or GPE (interrupt). */
8511de5ce99STakanori Watanabe     if (need_poll) {
85283dcc133SNate Lawson 	count = (ec_timeout * 1000) / EC_POLL_DELAY;
85383dcc133SNate Lawson 	if (count == 0)
854ef2374f7SNate Lawson 	    count = 1;
8555ff14fa9SAndriy Gapon 	DELAY(10);
856ef2374f7SNate Lawson 	for (i = 0; i < count; i++) {
85783dcc133SNate Lawson 	    Status = EcCheckStatus(sc, "poll", Event);
8589ad56977SJung-uk Kim 	    if (ACPI_SUCCESS(Status))
85915e32d5dSMike Smith 		break;
8605ff14fa9SAndriy Gapon 	    DELAY(EC_POLL_DELAY);
8611f04e8f5SNate Lawson 	}
86283dcc133SNate Lawson     } else {
863ef2374f7SNate Lawson 	slp_ival = hz / 1000;
864ef2374f7SNate Lawson 	if (slp_ival != 0) {
86583dcc133SNate Lawson 	    count = ec_timeout;
866ef2374f7SNate Lawson 	} else {
86783dcc133SNate Lawson 	    /* hz has less than 1 ms resolution so scale timeout. */
868b0eefa38SNate Lawson 	    slp_ival = 1;
869ef2374f7SNate Lawson 	    count = ec_timeout / (1000 / hz);
870ef2374f7SNate Lawson 	}
87183dcc133SNate Lawson 
87283dcc133SNate Lawson 	/*
87383dcc133SNate Lawson 	 * Wait for the GPE to signal the status changed, checking the
87483dcc133SNate Lawson 	 * status register each time we get one.  It's possible to get a
87583dcc133SNate Lawson 	 * GPE for an event we're not interested in here (i.e., SCI for
87683dcc133SNate Lawson 	 * EC query).
87783dcc133SNate Lawson 	 */
878b0eefa38SNate Lawson 	for (i = 0; i < count; i++) {
8795ff14fa9SAndriy Gapon 	    if (gen_count == sc->ec_gencount)
88080b1151eSJung-uk Kim 		tsleep(sc, 0, "ecgpe", slp_ival);
88183dcc133SNate Lawson 	    /*
88283dcc133SNate Lawson 	     * Record new generation count.  It's possible the GPE was
88383dcc133SNate Lawson 	     * just to notify us that a query is needed and we need to
88483dcc133SNate Lawson 	     * wait for a second GPE to signal the completion of the
88583dcc133SNate Lawson 	     * event we are actually waiting for.
88683dcc133SNate Lawson 	     */
88783dcc133SNate Lawson 	    Status = EcCheckStatus(sc, "sleep", Event);
8889ad56977SJung-uk Kim 	    if (ACPI_SUCCESS(Status)) {
8895ff14fa9SAndriy Gapon 		if (gen_count == sc->ec_gencount)
8905ff14fa9SAndriy Gapon 		    no_intr++;
8915ff14fa9SAndriy Gapon 		else
8925ff14fa9SAndriy Gapon 		    no_intr = 0;
893ff40920eSNate Lawson 		break;
894ff40920eSNate Lawson 	    }
8955ff14fa9SAndriy Gapon 	    gen_count = sc->ec_gencount;
8961f04e8f5SNate Lawson 	}
8971f04e8f5SNate Lawson 
89883dcc133SNate Lawson 	/*
89983dcc133SNate Lawson 	 * We finished waiting for the GPE and it never arrived.  Try to
90083dcc133SNate Lawson 	 * read the register once and trust whatever value we got.  This is
9015ff14fa9SAndriy Gapon 	 * the best we can do at this point.
90283dcc133SNate Lawson 	 */
9039ad56977SJung-uk Kim 	if (ACPI_FAILURE(Status))
90483dcc133SNate Lawson 	    Status = EcCheckStatus(sc, "sleep_end", Event);
90583dcc133SNate Lawson     }
9065ff14fa9SAndriy Gapon     if (!need_poll && no_intr > 10) {
9075ff14fa9SAndriy Gapon 	device_printf(sc->ec_dev,
9085ff14fa9SAndriy Gapon 	    "not getting interrupts, switched to polled mode\n");
9095ff14fa9SAndriy Gapon 	ec_polled_mode = 1;
91083dcc133SNate Lawson     }
9119ad56977SJung-uk Kim     if (ACPI_FAILURE(Status))
91283dcc133SNate Lawson 	    CTR0(KTR_ACPI, "error: ec wait timed out");
9131f04e8f5SNate Lawson     return (Status);
9141f04e8f5SNate Lawson }
9151f04e8f5SNate Lawson 
9161f04e8f5SNate Lawson static ACPI_STATUS
9171f04e8f5SNate Lawson EcCommand(struct acpi_ec_softc *sc, EC_COMMAND cmd)
9181f04e8f5SNate Lawson {
919ef2374f7SNate Lawson     ACPI_STATUS	status;
920ef2374f7SNate Lawson     EC_EVENT	event;
921ef2374f7SNate Lawson     EC_STATUS	ec_status;
92283dcc133SNate Lawson     u_int	gen_count;
9231f04e8f5SNate Lawson 
924f4b7de15SNate Lawson     ACPI_SERIAL_ASSERT(ec);
9251f04e8f5SNate Lawson 
926ef2374f7SNate Lawson     /* Don't use burst mode if user disabled it. */
927ef2374f7SNate Lawson     if (!ec_burst_mode && cmd == EC_COMMAND_BURST_ENABLE)
928ef2374f7SNate Lawson 	return (AE_ERROR);
929ef2374f7SNate Lawson 
9301f04e8f5SNate Lawson     /* Decide what to wait for based on command type. */
9311f04e8f5SNate Lawson     switch (cmd) {
9321f04e8f5SNate Lawson     case EC_COMMAND_READ:
93315e32d5dSMike Smith     case EC_COMMAND_WRITE:
9341f04e8f5SNate Lawson     case EC_COMMAND_BURST_DISABLE:
935ef2374f7SNate Lawson 	event = EC_EVENT_INPUT_BUFFER_EMPTY;
9361f04e8f5SNate Lawson 	break;
9371f04e8f5SNate Lawson     case EC_COMMAND_QUERY:
9381f04e8f5SNate Lawson     case EC_COMMAND_BURST_ENABLE:
939ef2374f7SNate Lawson 	event = EC_EVENT_OUTPUT_BUFFER_FULL;
94015e32d5dSMike Smith 	break;
94115e32d5dSMike Smith     default:
94283dcc133SNate Lawson 	device_printf(sc->ec_dev, "EcCommand: invalid command %#x\n", cmd);
9431f04e8f5SNate Lawson 	return (AE_BAD_PARAMETER);
944464c662eSNate Lawson     }
9451f04e8f5SNate Lawson 
9465ff14fa9SAndriy Gapon     /*
9475ff14fa9SAndriy Gapon      * Ensure empty input buffer before issuing command.
9485ff14fa9SAndriy Gapon      * Use generation count of zero to force a quick check.
9495ff14fa9SAndriy Gapon      */
9505ff14fa9SAndriy Gapon     status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY, 0);
9515ff14fa9SAndriy Gapon     if (ACPI_FAILURE(status))
9525ff14fa9SAndriy Gapon 	return (status);
9535ff14fa9SAndriy Gapon 
9541f04e8f5SNate Lawson     /* Run the command and wait for the chosen event. */
955ef2374f7SNate Lawson     CTR1(KTR_ACPI, "ec running command %#x", cmd);
95683dcc133SNate Lawson     gen_count = sc->ec_gencount;
9571f04e8f5SNate Lawson     EC_SET_CSR(sc, cmd);
95883dcc133SNate Lawson     status = EcWaitEvent(sc, event, gen_count);
959ef2374f7SNate Lawson     if (ACPI_SUCCESS(status)) {
960ef2374f7SNate Lawson 	/* If we succeeded, burst flag should now be present. */
961ef2374f7SNate Lawson 	if (cmd == EC_COMMAND_BURST_ENABLE) {
962ef2374f7SNate Lawson 	    ec_status = EC_GET_CSR(sc);
963ef2374f7SNate Lawson 	    if ((ec_status & EC_FLAG_BURST_MODE) == 0)
964ef2374f7SNate Lawson 		status = AE_ERROR;
965ef2374f7SNate Lawson 	}
96683dcc133SNate Lawson     } else
96783dcc133SNate Lawson 	device_printf(sc->ec_dev, "EcCommand: no response to %#x\n", cmd);
968ef2374f7SNate Lawson     return (status);
96915e32d5dSMike Smith }
97015e32d5dSMike Smith 
97115e32d5dSMike Smith static ACPI_STATUS
97215e32d5dSMike Smith EcRead(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data)
97315e32d5dSMike Smith {
974ef2374f7SNate Lawson     ACPI_STATUS	status;
97583dcc133SNate Lawson     u_int gen_count;
9765ff14fa9SAndriy Gapon     int retry;
97715e32d5dSMike Smith 
978f4b7de15SNate Lawson     ACPI_SERIAL_ASSERT(ec);
979c4a9fa45SNate Lawson     CTR1(KTR_ACPI, "ec read from %#x", Address);
98015e32d5dSMike Smith 
9815ff14fa9SAndriy Gapon     for (retry = 0; retry < 2; retry++) {
982ef2374f7SNate Lawson 	status = EcCommand(sc, EC_COMMAND_READ);
983ef2374f7SNate Lawson 	if (ACPI_FAILURE(status))
984ef2374f7SNate Lawson 	    return (status);
98515e32d5dSMike Smith 
98683dcc133SNate Lawson 	gen_count = sc->ec_gencount;
98715e32d5dSMike Smith 	EC_SET_DATA(sc, Address);
98883dcc133SNate Lawson 	status = EcWaitEvent(sc, EC_EVENT_OUTPUT_BUFFER_FULL, gen_count);
989ae1ef64fSJung-uk Kim 	if (ACPI_SUCCESS(status)) {
990464c662eSNate Lawson 	    *Data = EC_GET_DATA(sc);
99115e32d5dSMike Smith 	    return (AE_OK);
99215e32d5dSMike Smith 	}
993ae1ef64fSJung-uk Kim 	if (ACPI_FAILURE(EcCheckStatus(sc, "retr_check",
994ae1ef64fSJung-uk Kim 	    EC_EVENT_INPUT_BUFFER_EMPTY)))
995ae1ef64fSJung-uk Kim 	    break;
996ae1ef64fSJung-uk Kim     }
9975ff14fa9SAndriy Gapon     device_printf(sc->ec_dev, "EcRead: failed waiting to get data\n");
9985ff14fa9SAndriy Gapon     return (status);
9995ff14fa9SAndriy Gapon }
100015e32d5dSMike Smith 
100115e32d5dSMike Smith static ACPI_STATUS
1002e115a49fSJung-uk Kim EcWrite(struct acpi_ec_softc *sc, UINT8 Address, UINT8 Data)
100315e32d5dSMike Smith {
1004ef2374f7SNate Lawson     ACPI_STATUS	status;
100583dcc133SNate Lawson     u_int gen_count;
100615e32d5dSMike Smith 
1007f4b7de15SNate Lawson     ACPI_SERIAL_ASSERT(ec);
1008e115a49fSJung-uk Kim     CTR2(KTR_ACPI, "ec write to %#x, data %#x", Address, Data);
100915e32d5dSMike Smith 
1010ef2374f7SNate Lawson     status = EcCommand(sc, EC_COMMAND_WRITE);
1011ef2374f7SNate Lawson     if (ACPI_FAILURE(status))
1012ef2374f7SNate Lawson 	return (status);
101315e32d5dSMike Smith 
101483dcc133SNate Lawson     gen_count = sc->ec_gencount;
101515e32d5dSMike Smith     EC_SET_DATA(sc, Address);
101683dcc133SNate Lawson     status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY, gen_count);
1017ef2374f7SNate Lawson     if (ACPI_FAILURE(status)) {
10185ff14fa9SAndriy Gapon 	device_printf(sc->ec_dev, "EcWrite: failed waiting for sent address\n");
1019ef2374f7SNate Lawson 	return (status);
102015e32d5dSMike Smith     }
102115e32d5dSMike Smith 
102283dcc133SNate Lawson     gen_count = sc->ec_gencount;
1023e115a49fSJung-uk Kim     EC_SET_DATA(sc, Data);
102483dcc133SNate Lawson     status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY, gen_count);
1025ef2374f7SNate Lawson     if (ACPI_FAILURE(status)) {
102683dcc133SNate Lawson 	device_printf(sc->ec_dev, "EcWrite: failed waiting for sent data\n");
1027ef2374f7SNate Lawson 	return (status);
102815e32d5dSMike Smith     }
102915e32d5dSMike Smith 
103015e32d5dSMike Smith     return (AE_OK);
103115e32d5dSMike Smith }
1032