167761Smsmith/*-
2172313Snjl * Copyright (c) 2003-2007 Nate Lawson
367761Smsmith * Copyright (c) 2000 Michael Smith
467761Smsmith * Copyright (c) 2000 BSDi
567761Smsmith * All rights reserved.
667761Smsmith *
767761Smsmith * Redistribution and use in source and binary forms, with or without
867761Smsmith * modification, are permitted provided that the following conditions
967761Smsmith * are met:
1067761Smsmith * 1. Redistributions of source code must retain the above copyright
1167761Smsmith *    notice, this list of conditions and the following disclaimer.
1267761Smsmith * 2. Redistributions in binary form must reproduce the above copyright
1367761Smsmith *    notice, this list of conditions and the following disclaimer in the
1467761Smsmith *    documentation and/or other materials provided with the distribution.
1567761Smsmith *
1667761Smsmith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1767761Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1867761Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1967761Smsmith * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2067761Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2167761Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2267761Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2367761Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2467761Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2567761Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2667761Smsmith * SUCH DAMAGE.
2767761Smsmith */
2867761Smsmith
29119418Sobrien#include <sys/cdefs.h>
30119418Sobrien__FBSDID("$FreeBSD$");
31119418Sobrien
3267761Smsmith#include "opt_acpi.h"
3367761Smsmith#include <sys/param.h>
3467761Smsmith#include <sys/kernel.h>
3567761Smsmith#include <sys/bus.h>
36168191Sjhb#include <sys/lock.h>
37129197Snjl#include <sys/malloc.h>
38131384Snjl#include <sys/module.h>
39131384Snjl#include <sys/sx.h>
4067761Smsmith
4167761Smsmith#include <machine/bus.h>
4267761Smsmith#include <machine/resource.h>
4367761Smsmith#include <sys/rman.h>
4467761Smsmith
45193530Sjkim#include <contrib/dev/acpica/include/acpi.h>
46193530Sjkim#include <contrib/dev/acpica/include/accommon.h>
47193530Sjkim
4867761Smsmith#include <dev/acpica/acpivar.h>
4967761Smsmith
50133617Snjl/* Hooks for the ACPI CA debugging infrastructure */
5178992Smsmith#define _COMPONENT	ACPI_EC
5291122SmsmithACPI_MODULE_NAME("EC")
5369744Smsmith
5480069Smsmith/*
5580069Smsmith * EC_COMMAND:
5680069Smsmith * -----------
5780069Smsmith */
5880069Smsmithtypedef UINT8				EC_COMMAND;
5980069Smsmith
6080069Smsmith#define EC_COMMAND_UNKNOWN		((EC_COMMAND) 0x00)
6180069Smsmith#define EC_COMMAND_READ			((EC_COMMAND) 0x80)
6280069Smsmith#define EC_COMMAND_WRITE		((EC_COMMAND) 0x81)
6380069Smsmith#define EC_COMMAND_BURST_ENABLE		((EC_COMMAND) 0x82)
6480069Smsmith#define EC_COMMAND_BURST_DISABLE	((EC_COMMAND) 0x83)
6580069Smsmith#define EC_COMMAND_QUERY		((EC_COMMAND) 0x84)
6680069Smsmith
67172313Snjl/*
6880069Smsmith * EC_STATUS:
6980069Smsmith * ----------
7080069Smsmith * The encoding of the EC status register is illustrated below.
7180069Smsmith * Note that a set bit (1) indicates the property is TRUE
7280069Smsmith * (e.g. if bit 0 is set then the output buffer is full).
7380069Smsmith * +-+-+-+-+-+-+-+-+
74139339Snjl * |7|6|5|4|3|2|1|0|
7580069Smsmith * +-+-+-+-+-+-+-+-+
7680069Smsmith *  | | | | | | | |
7780069Smsmith *  | | | | | | | +- Output Buffer Full?
7880069Smsmith *  | | | | | | +--- Input Buffer Full?
7980069Smsmith *  | | | | | +----- <reserved>
8080069Smsmith *  | | | | +------- Data Register is Command Byte?
8180069Smsmith *  | | | +--------- Burst Mode Enabled?
8280069Smsmith *  | | +----------- SCI Event?
8380069Smsmith *  | +------------- SMI Event?
84167044Snjl *  +--------------- <reserved>
8580069Smsmith *
8680069Smsmith */
8780069Smsmithtypedef UINT8				EC_STATUS;
8880069Smsmith
8980069Smsmith#define EC_FLAG_OUTPUT_BUFFER		((EC_STATUS) 0x01)
9080069Smsmith#define EC_FLAG_INPUT_BUFFER		((EC_STATUS) 0x02)
91167044Snjl#define EC_FLAG_DATA_IS_CMD		((EC_STATUS) 0x08)
9280069Smsmith#define EC_FLAG_BURST_MODE		((EC_STATUS) 0x10)
9380069Smsmith
9480069Smsmith/*
9580069Smsmith * EC_EVENT:
9680069Smsmith * ---------
9780069Smsmith */
9880069Smsmithtypedef UINT8				EC_EVENT;
9980069Smsmith
10080069Smsmith#define EC_EVENT_UNKNOWN		((EC_EVENT) 0x00)
10180069Smsmith#define EC_EVENT_OUTPUT_BUFFER_FULL	((EC_EVENT) 0x01)
10280069Smsmith#define EC_EVENT_INPUT_BUFFER_EMPTY	((EC_EVENT) 0x02)
10380069Smsmith#define EC_EVENT_SCI			((EC_EVENT) 0x20)
104167044Snjl#define EC_EVENT_SMI			((EC_EVENT) 0x40)
10580069Smsmith
106167044Snjl/* Data byte returned after burst enable indicating it was successful. */
107167044Snjl#define EC_BURST_ACK			0x90
108167044Snjl
10980069Smsmith/*
11080069Smsmith * Register access primitives
11180069Smsmith */
11280069Smsmith#define EC_GET_DATA(sc)							\
11380069Smsmith	bus_space_read_1((sc)->ec_data_tag, (sc)->ec_data_handle, 0)
11480069Smsmith
11580069Smsmith#define EC_SET_DATA(sc, v)						\
11680069Smsmith	bus_space_write_1((sc)->ec_data_tag, (sc)->ec_data_handle, 0, (v))
11780069Smsmith
11880069Smsmith#define EC_GET_CSR(sc)							\
11980069Smsmith	bus_space_read_1((sc)->ec_csr_tag, (sc)->ec_csr_handle, 0)
12080069Smsmith
12180069Smsmith#define EC_SET_CSR(sc, v)						\
12280069Smsmith	bus_space_write_1((sc)->ec_csr_tag, (sc)->ec_csr_handle, 0, (v))
12380069Smsmith
124129197Snjl/* Additional params to pass from the probe routine */
125129197Snjlstruct acpi_ec_params {
126129197Snjl    int		glk;
127129197Snjl    int		gpe_bit;
128129197Snjl    ACPI_HANDLE	gpe_handle;
129129197Snjl    int		uid;
130129197Snjl};
131129197Snjl
13280069Smsmith/*
13380069Smsmith * Driver softc.
13480069Smsmith */
13567761Smsmithstruct acpi_ec_softc {
13667761Smsmith    device_t		ec_dev;
13767761Smsmith    ACPI_HANDLE		ec_handle;
138129197Snjl    int			ec_uid;
139129197Snjl    ACPI_HANDLE		ec_gpehandle;
140117809Snjl    UINT8		ec_gpebit;
141172313Snjl
14267761Smsmith    int			ec_data_rid;
14367761Smsmith    struct resource	*ec_data_res;
14467761Smsmith    bus_space_tag_t	ec_data_tag;
14567761Smsmith    bus_space_handle_t	ec_data_handle;
14667761Smsmith
14767761Smsmith    int			ec_csr_rid;
14867761Smsmith    struct resource	*ec_csr_res;
14967761Smsmith    bus_space_tag_t	ec_csr_tag;
15067761Smsmith    bus_space_handle_t	ec_csr_handle;
15167761Smsmith
152117384Snjl    int			ec_glk;
153117384Snjl    int			ec_glkhandle;
154167044Snjl    int			ec_burstactive;
155167044Snjl    int			ec_sci_pend;
156213737Savg    volatile u_int	ec_gencount;
157172973Stakawata    int			ec_suspending;
15867761Smsmith};
15967761Smsmith
160117384Snjl/*
161133617Snjl * XXX njl
162117384Snjl * I couldn't find it in the spec but other implementations also use a
163117384Snjl * value of 1 ms for the time to acquire global lock.
164117384Snjl */
165117384Snjl#define EC_LOCK_TIMEOUT	1000
166105857Siwasaki
167167044Snjl/* Default delay in microseconds between each run of the status polling loop. */
168213737Savg#define EC_POLL_DELAY	50
16967761Smsmith
170167044Snjl/* Total time in ms spent waiting for a response from EC. */
171172313Snjl#define EC_TIMEOUT	750
172167044Snjl
173117649Snjl#define EVENT_READY(event, status)			\
174117649Snjl	(((event) == EC_EVENT_OUTPUT_BUFFER_FULL &&	\
175117649Snjl	 ((status) & EC_FLAG_OUTPUT_BUFFER) != 0) ||	\
176117649Snjl	 ((event) == EC_EVENT_INPUT_BUFFER_EMPTY && 	\
177117649Snjl	 ((status) & EC_FLAG_INPUT_BUFFER) == 0))
178117649Snjl
179133617SnjlACPI_SERIAL_DECL(ec, "ACPI embedded controller");
180133617Snjl
181248085Smariusstatic SYSCTL_NODE(_debug_acpi, OID_AUTO, ec, CTLFLAG_RD, NULL, "EC debugging");
182167044Snjl
183167671Snjlstatic int	ec_burst_mode;
184167044SnjlTUNABLE_INT("debug.acpi.ec.burst", &ec_burst_mode);
185167671SnjlSYSCTL_INT(_debug_acpi_ec, OID_AUTO, burst, CTLFLAG_RW, &ec_burst_mode, 0,
186167044Snjl    "Enable use of burst mode (faster for nearly all systems)");
187172313Snjlstatic int	ec_polled_mode;
188172313SnjlTUNABLE_INT("debug.acpi.ec.polled", &ec_polled_mode);
189172313SnjlSYSCTL_INT(_debug_acpi_ec, OID_AUTO, polled, CTLFLAG_RW, &ec_polled_mode, 0,
190172313Snjl    "Force use of polled mode (only if interrupt mode doesn't work)");
191167044Snjlstatic int	ec_timeout = EC_TIMEOUT;
192167044SnjlTUNABLE_INT("debug.acpi.ec.timeout", &ec_timeout);
193167044SnjlSYSCTL_INT(_debug_acpi_ec, OID_AUTO, timeout, CTLFLAG_RW, &ec_timeout,
194167044Snjl    EC_TIMEOUT, "Total time spent waiting for a response (poll+sleep)");
195167044Snjl
196172313Snjlstatic ACPI_STATUS
197172313SnjlEcLock(struct acpi_ec_softc *sc)
19867761Smsmith{
199133617Snjl    ACPI_STATUS	status;
20067761Smsmith
201170211Snjl    /* If _GLK is non-zero, acquire the global lock. */
202170211Snjl    status = AE_OK;
203170211Snjl    if (sc->ec_glk) {
204170211Snjl	status = AcpiAcquireGlobalLock(EC_LOCK_TIMEOUT, &sc->ec_glkhandle);
205170211Snjl	if (ACPI_FAILURE(status))
206170211Snjl	    return (status);
207170211Snjl    }
208172313Snjl    ACPI_SERIAL_BEGIN(ec);
209116927Snjl    return (status);
21067761Smsmith}
21167761Smsmith
212172313Snjlstatic void
21367761SmsmithEcUnlock(struct acpi_ec_softc *sc)
21467761Smsmith{
215172313Snjl    ACPI_SERIAL_END(ec);
216170211Snjl    if (sc->ec_glk)
217170211Snjl	AcpiReleaseGlobalLock(sc->ec_glkhandle);
21867761Smsmith}
21967761Smsmith
220216471Sjkimstatic UINT32		EcGpeHandler(ACPI_HANDLE, UINT32, void *);
221172313Snjlstatic ACPI_STATUS	EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function,
222116927Snjl				void *Context, void **return_Context);
223116927Snjlstatic ACPI_STATUS	EcSpaceHandler(UINT32 Function,
224116927Snjl				ACPI_PHYSICAL_ADDRESS Address,
225208722Sjkim				UINT32 Width, UINT64 *Value,
226116927Snjl				void *Context, void *RegionContext);
227172313Snjlstatic ACPI_STATUS	EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event,
228172313Snjl				u_int gen_count);
229117384Snjlstatic ACPI_STATUS	EcCommand(struct acpi_ec_softc *sc, EC_COMMAND cmd);
230116927Snjlstatic ACPI_STATUS	EcRead(struct acpi_ec_softc *sc, UINT8 Address,
231116927Snjl				UINT8 *Data);
232116927Snjlstatic ACPI_STATUS	EcWrite(struct acpi_ec_softc *sc, UINT8 Address,
233208722Sjkim				UINT8 Data);
23467761Smsmithstatic int		acpi_ec_probe(device_t dev);
23567761Smsmithstatic int		acpi_ec_attach(device_t dev);
236172973Stakawatastatic int		acpi_ec_suspend(device_t dev);
237172973Stakawatastatic int		acpi_ec_resume(device_t dev);
238131339Snjlstatic int		acpi_ec_shutdown(device_t dev);
239143861Snjlstatic int		acpi_ec_read_method(device_t dev, u_int addr,
240202771Sjkim				UINT64 *val, int width);
241143861Snjlstatic int		acpi_ec_write_method(device_t dev, u_int addr,
242202771Sjkim				UINT64 val, int width);
24367761Smsmith
24467761Smsmithstatic device_method_t acpi_ec_methods[] = {
24567761Smsmith    /* Device interface */
24667761Smsmith    DEVMETHOD(device_probe,	acpi_ec_probe),
24767761Smsmith    DEVMETHOD(device_attach,	acpi_ec_attach),
248172973Stakawata    DEVMETHOD(device_suspend,	acpi_ec_suspend),
249172973Stakawata    DEVMETHOD(device_resume,	acpi_ec_resume),
250131339Snjl    DEVMETHOD(device_shutdown,	acpi_ec_shutdown),
25167761Smsmith
252143861Snjl    /* Embedded controller interface */
253143861Snjl    DEVMETHOD(acpi_ec_read,	acpi_ec_read_method),
254143861Snjl    DEVMETHOD(acpi_ec_write,	acpi_ec_write_method),
255143861Snjl
25667761Smsmith    {0, 0}
25767761Smsmith};
25867761Smsmith
25967761Smsmithstatic driver_t acpi_ec_driver = {
26067761Smsmith    "acpi_ec",
26167761Smsmith    acpi_ec_methods,
26267761Smsmith    sizeof(struct acpi_ec_softc),
26367761Smsmith};
26467761Smsmith
26589054Smsmithstatic devclass_t acpi_ec_devclass;
26667761SmsmithDRIVER_MODULE(acpi_ec, acpi, acpi_ec_driver, acpi_ec_devclass, 0, 0);
267128071SnjlMODULE_DEPEND(acpi_ec, acpi, 1, 1, 1);
26867761Smsmith
26967761Smsmith/*
270172313Snjl * Look for an ECDT and if we find one, set up default GPE and
271117795Snjl * space handlers to catch attempts to access EC space before
27267761Smsmith * we have a real driver instance in place.
273172313Snjl *
274172313Snjl * TODO: Some old Gateway laptops need us to fake up an ECDT or
275172313Snjl * otherwise attach early so that _REG methods can run.
27667761Smsmith */
277117795Snjlvoid
278117795Snjlacpi_ec_ecdt_probe(device_t parent)
27967761Smsmith{
280117795Snjl    ACPI_TABLE_ECDT *ecdt;
281117795Snjl    ACPI_STATUS	     status;
282117795Snjl    device_t	     child;
283117795Snjl    ACPI_HANDLE	     h;
284129197Snjl    struct acpi_ec_params *params;
285117795Snjl
28696926Speter    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
28767761Smsmith
288117795Snjl    /* Find and validate the ECDT. */
289167814Sjkim    status = AcpiGetTable(ACPI_SIG_ECDT, 1, (ACPI_TABLE_HEADER **)&ecdt);
290117795Snjl    if (ACPI_FAILURE(status) ||
291167814Sjkim	ecdt->Control.BitWidth != 8 ||
292167814Sjkim	ecdt->Data.BitWidth != 8) {
293117795Snjl	return;
294117795Snjl    }
295117795Snjl
296117795Snjl    /* Create the child device with the given unit number. */
297232086Sjkim    child = BUS_ADD_CHILD(parent, 3, "acpi_ec", ecdt->Uid);
298117795Snjl    if (child == NULL) {
299129197Snjl	printf("%s: can't add child\n", __func__);
300117795Snjl	return;
301117795Snjl    }
302117795Snjl
303117795Snjl    /* Find and save the ACPI handle for this device. */
304167814Sjkim    status = AcpiGetHandle(NULL, ecdt->Id, &h);
305117795Snjl    if (ACPI_FAILURE(status)) {
306117795Snjl	device_delete_child(parent, child);
307129197Snjl	printf("%s: can't get handle\n", __func__);
308117795Snjl	return;
309117795Snjl    }
310117795Snjl    acpi_set_handle(child, h);
311117795Snjl
312117795Snjl    /* Set the data and CSR register addresses. */
313167814Sjkim    bus_set_resource(child, SYS_RES_IOPORT, 0, ecdt->Data.Address,
314117795Snjl	/*count*/1);
315167814Sjkim    bus_set_resource(child, SYS_RES_IOPORT, 1, ecdt->Control.Address,
316117795Snjl	/*count*/1);
317117795Snjl
318117795Snjl    /*
319117795Snjl     * Store values for the probe/attach routines to use.  Store the
320129168Snjl     * ECDT GPE bit and set the global lock flag according to _GLK.
321129197Snjl     * Note that it is not perfectly correct to be evaluating a method
322129197Snjl     * before initializing devices, but in practice this function
323129197Snjl     * should be safe to call at this point.
324117795Snjl     */
325129197Snjl    params = malloc(sizeof(struct acpi_ec_params), M_TEMP, M_WAITOK | M_ZERO);
326129197Snjl    params->gpe_handle = NULL;
327167814Sjkim    params->gpe_bit = ecdt->Gpe;
328167814Sjkim    params->uid = ecdt->Uid;
329129197Snjl    acpi_GetInteger(h, "_GLK", &params->glk);
330129197Snjl    acpi_set_private(child, params);
331117795Snjl
332117795Snjl    /* Finish the attach process. */
333117795Snjl    if (device_probe_and_attach(child) != 0)
334117795Snjl	device_delete_child(parent, child);
33567761Smsmith}
33667761Smsmith
33767761Smsmithstatic int
33867761Smsmithacpi_ec_probe(device_t dev)
33967761Smsmith{
340129197Snjl    ACPI_BUFFER buf;
341117795Snjl    ACPI_HANDLE h;
342129197Snjl    ACPI_OBJECT *obj;
343117795Snjl    ACPI_STATUS status;
344117795Snjl    device_t	peer;
345117795Snjl    char	desc[64];
346199016Savg    int		ecdt;
347129197Snjl    int		ret;
348129197Snjl    struct acpi_ec_params *params;
349131282Snjl    static char *ec_ids[] = { "PNP0C09", NULL };
35069744Smsmith
351117810Snjl    /* Check that this is a device and that EC is not disabled. */
352117810Snjl    if (acpi_get_type(dev) != ACPI_TYPE_DEVICE || acpi_disabled("ec"))
353117795Snjl	return (ENXIO);
35467761Smsmith
355117795Snjl    /*
356117795Snjl     * If probed via ECDT, set description and continue.  Otherwise,
357117795Snjl     * we can access the namespace and make sure this is not a
358117795Snjl     * duplicate probe.
359117795Snjl     */
360129197Snjl    ret = ENXIO;
361199016Savg    ecdt = 0;
362129197Snjl    buf.Pointer = NULL;
363129197Snjl    buf.Length = ACPI_ALLOCATE_BUFFER;
364199016Savg    params = acpi_get_private(dev);
365199016Savg    if (params != NULL) {
366199016Savg	ecdt = 1;
367117795Snjl	ret = 0;
368202567Savg    } else if (ACPI_ID_PROBE(device_get_parent(dev), dev, ec_ids)) {
369129197Snjl	params = malloc(sizeof(struct acpi_ec_params), M_TEMP,
370129197Snjl			M_WAITOK | M_ZERO);
371117795Snjl	h = acpi_get_handle(dev);
372117795Snjl
37367761Smsmith	/*
374117795Snjl	 * Read the unit ID to check for duplicate attach and the
375117795Snjl	 * global lock value to see if we should acquire it when
376117795Snjl	 * accessing the EC.
37767761Smsmith	 */
378129197Snjl	status = acpi_GetInteger(h, "_UID", &params->uid);
379117795Snjl	if (ACPI_FAILURE(status))
380129197Snjl	    params->uid = 0;
381129197Snjl	status = acpi_GetInteger(h, "_GLK", &params->glk);
382117795Snjl	if (ACPI_FAILURE(status))
383129197Snjl	    params->glk = 0;
384117795Snjl
385117795Snjl	/*
386117795Snjl	 * Evaluate the _GPE method to find the GPE bit used by the EC to
387129197Snjl	 * signal status (SCI).  If it's a package, it contains a reference
388129197Snjl	 * and GPE bit, similar to _PRW.
389117795Snjl	 */
390129197Snjl	status = AcpiEvaluateObject(h, "_GPE", NULL, &buf);
391117795Snjl	if (ACPI_FAILURE(status)) {
392117795Snjl	    device_printf(dev, "can't evaluate _GPE - %s\n",
393117795Snjl			  AcpiFormatException(status));
394146166Snjl	    goto out;
395117795Snjl	}
396129197Snjl	obj = (ACPI_OBJECT *)buf.Pointer;
397129197Snjl	if (obj == NULL)
398146166Snjl	    goto out;
399117795Snjl
400129197Snjl	switch (obj->Type) {
401129197Snjl	case ACPI_TYPE_INTEGER:
402129197Snjl	    params->gpe_handle = NULL;
403129197Snjl	    params->gpe_bit = obj->Integer.Value;
404129197Snjl	    break;
405129197Snjl	case ACPI_TYPE_PACKAGE:
406129197Snjl	    if (!ACPI_PKG_VALID(obj, 2))
407129197Snjl		goto out;
408129197Snjl	    params->gpe_handle =
409129197Snjl		acpi_GetReference(NULL, &obj->Package.Elements[0]);
410129197Snjl	    if (params->gpe_handle == NULL ||
411129197Snjl		acpi_PkgInt32(obj, 1, &params->gpe_bit) != 0)
412129197Snjl		goto out;
413129197Snjl	    break;
414129197Snjl	default:
415129197Snjl	    device_printf(dev, "_GPE has invalid type %d\n", obj->Type);
416129197Snjl	    goto out;
417129197Snjl	}
418129197Snjl
419117795Snjl	/* Store the values we got from the namespace for attach. */
420129197Snjl	acpi_set_private(dev, params);
421117795Snjl
422117795Snjl	/*
423117795Snjl	 * Check for a duplicate probe.  This can happen when a probe
424129168Snjl	 * via ECDT succeeded already.  If this is a duplicate, disable
425129168Snjl	 * this device.
426117795Snjl	 */
427129197Snjl	peer = devclass_get_device(acpi_ec_devclass, params->uid);
428129168Snjl	if (peer == NULL || !device_is_alive(peer))
429117795Snjl	    ret = 0;
430129168Snjl	else
431129168Snjl	    device_disable(dev);
432129168Snjl    }
433117795Snjl
434129197Snjlout:
435129168Snjl    if (ret == 0) {
436129168Snjl	snprintf(desc, sizeof(desc), "Embedded Controller: GPE %#x%s%s",
437129197Snjl		 params->gpe_bit, (params->glk) ? ", GLK" : "",
438199016Savg		 ecdt ? ", ECDT" : "");
439129168Snjl	device_set_desc_copy(dev, desc);
44067761Smsmith    }
441117384Snjl
442129197Snjl    if (ret > 0 && params)
443129197Snjl	free(params, M_TEMP);
444129197Snjl    if (buf.Pointer)
445129197Snjl	AcpiOsFree(buf.Pointer);
446117384Snjl    return (ret);
44767761Smsmith}
44867761Smsmith
44967761Smsmithstatic int
45067761Smsmithacpi_ec_attach(device_t dev)
45167761Smsmith{
45267761Smsmith    struct acpi_ec_softc	*sc;
453129197Snjl    struct acpi_ec_params	*params;
45467761Smsmith    ACPI_STATUS			Status;
45569744Smsmith
45696926Speter    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
45791122Smsmith
458117384Snjl    /* Fetch/initialize softc (assumes softc is pre-zeroed). */
45967761Smsmith    sc = device_get_softc(dev);
460129197Snjl    params = acpi_get_private(dev);
46167761Smsmith    sc->ec_dev = dev;
46267761Smsmith    sc->ec_handle = acpi_get_handle(dev);
46367761Smsmith
464117795Snjl    /* Retrieve previously probed values via device ivars. */
465129197Snjl    sc->ec_glk = params->glk;
466129197Snjl    sc->ec_gpebit = params->gpe_bit;
467129197Snjl    sc->ec_gpehandle = params->gpe_handle;
468129197Snjl    sc->ec_uid = params->uid;
469172987Stakawata    sc->ec_suspending = FALSE;
470202558Savg    acpi_set_private(dev, NULL);
471129197Snjl    free(params, M_TEMP);
472117795Snjl
473117384Snjl    /* Attach bus resources for data and command/status ports. */
47467761Smsmith    sc->ec_data_rid = 0;
475127135Snjl    sc->ec_data_res = bus_alloc_resource_any(sc->ec_dev, SYS_RES_IOPORT,
476127135Snjl			&sc->ec_data_rid, RF_ACTIVE);
477116927Snjl    if (sc->ec_data_res == NULL) {
47867761Smsmith	device_printf(dev, "can't allocate data port\n");
479129692Snjl	goto error;
48067761Smsmith    }
48167761Smsmith    sc->ec_data_tag = rman_get_bustag(sc->ec_data_res);
48267761Smsmith    sc->ec_data_handle = rman_get_bushandle(sc->ec_data_res);
48367761Smsmith
48467761Smsmith    sc->ec_csr_rid = 1;
485127135Snjl    sc->ec_csr_res = bus_alloc_resource_any(sc->ec_dev, SYS_RES_IOPORT,
486127135Snjl			&sc->ec_csr_rid, RF_ACTIVE);
487116927Snjl    if (sc->ec_csr_res == NULL) {
48867761Smsmith	device_printf(dev, "can't allocate command/status port\n");
489129692Snjl	goto error;
49067761Smsmith    }
49167761Smsmith    sc->ec_csr_tag = rman_get_bustag(sc->ec_csr_res);
49267761Smsmith    sc->ec_csr_handle = rman_get_bushandle(sc->ec_csr_res);
49367761Smsmith
49467761Smsmith    /*
495117384Snjl     * Install a handler for this EC's GPE bit.  We want edge-triggered
496117384Snjl     * behavior.
49767761Smsmith     */
498117384Snjl    ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "attaching GPE handler\n"));
499129197Snjl    Status = AcpiInstallGpeHandler(sc->ec_gpehandle, sc->ec_gpebit,
500216471Sjkim		ACPI_GPE_EDGE_TRIGGERED, EcGpeHandler, sc);
501116927Snjl    if (ACPI_FAILURE(Status)) {
50279285Smsmith	device_printf(dev, "can't install GPE handler for %s - %s\n",
50380078Smsmith		      acpi_name(sc->ec_handle), AcpiFormatException(Status));
504129692Snjl	goto error;
50567761Smsmith    }
50667761Smsmith
507172313Snjl    /*
50867761Smsmith     * Install address space handler
50967761Smsmith     */
51082372Smsmith    ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "attaching address space handler\n"));
511116927Snjl    Status = AcpiInstallAddressSpaceHandler(sc->ec_handle, ACPI_ADR_SPACE_EC,
512117384Snjl		&EcSpaceHandler, &EcSpaceSetup, sc);
513116927Snjl    if (ACPI_FAILURE(Status)) {
51479285Smsmith	device_printf(dev, "can't install address space handler for %s - %s\n",
51580078Smsmith		      acpi_name(sc->ec_handle), AcpiFormatException(Status));
516129692Snjl	goto error;
51767761Smsmith    }
518116927Snjl
519129692Snjl    /* Enable runtime GPEs for the handler. */
520209746Sjkim    Status = AcpiEnableGpe(sc->ec_gpehandle, sc->ec_gpebit);
521129692Snjl    if (ACPI_FAILURE(Status)) {
522129692Snjl	device_printf(dev, "AcpiEnableGpe failed: %s\n",
523129692Snjl		      AcpiFormatException(Status));
524129692Snjl	goto error;
525129692Snjl    }
526129692Snjl
527117384Snjl    ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "acpi_ec_attach complete\n"));
528117384Snjl    return (0);
529117384Snjl
530129692Snjlerror:
531216471Sjkim    AcpiRemoveGpeHandler(sc->ec_gpehandle, sc->ec_gpebit, EcGpeHandler);
532129692Snjl    AcpiRemoveAddressSpaceHandler(sc->ec_handle, ACPI_ADR_SPACE_EC,
533129692Snjl	EcSpaceHandler);
534116927Snjl    if (sc->ec_csr_res)
535172313Snjl	bus_release_resource(sc->ec_dev, SYS_RES_IOPORT, sc->ec_csr_rid,
53690005Stakawata			     sc->ec_csr_res);
537116927Snjl    if (sc->ec_data_res)
538117384Snjl	bus_release_resource(sc->ec_dev, SYS_RES_IOPORT, sc->ec_data_rid,
53990005Stakawata			     sc->ec_data_res);
540129692Snjl    return (ENXIO);
54167761Smsmith}
54267761Smsmith
543131339Snjlstatic int
544172973Stakawataacpi_ec_suspend(device_t dev)
545172973Stakawata{
546172973Stakawata    struct acpi_ec_softc	*sc;
547172973Stakawata
548172973Stakawata    sc = device_get_softc(dev);
549172987Stakawata    sc->ec_suspending = TRUE;
550172973Stakawata    return (0);
551172973Stakawata}
552172973Stakawata
553172973Stakawatastatic int
554172973Stakawataacpi_ec_resume(device_t dev)
555172973Stakawata{
556172973Stakawata    struct acpi_ec_softc	*sc;
557172973Stakawata
558172973Stakawata    sc = device_get_softc(dev);
559172987Stakawata    sc->ec_suspending = FALSE;
560172973Stakawata    return (0);
561172973Stakawata}
562172973Stakawata
563172973Stakawatastatic int
564131339Snjlacpi_ec_shutdown(device_t dev)
565131339Snjl{
566131339Snjl    struct acpi_ec_softc	*sc;
567131339Snjl
568131339Snjl    /* Disable the GPE so we don't get EC events during shutdown. */
569131339Snjl    sc = device_get_softc(dev);
570209746Sjkim    AcpiDisableGpe(sc->ec_gpehandle, sc->ec_gpebit);
571131339Snjl    return (0);
572131339Snjl}
573131339Snjl
574143861Snjl/* Methods to allow other devices (e.g., smbat) to read/write EC space. */
575143861Snjlstatic int
576202771Sjkimacpi_ec_read_method(device_t dev, u_int addr, UINT64 *val, int width)
577143861Snjl{
578143861Snjl    struct acpi_ec_softc *sc;
579143861Snjl    ACPI_STATUS status;
580143861Snjl
581143861Snjl    sc = device_get_softc(dev);
582143861Snjl    status = EcSpaceHandler(ACPI_READ, addr, width * 8, val, sc, NULL);
583143861Snjl    if (ACPI_FAILURE(status))
584143861Snjl	return (ENXIO);
585143861Snjl    return (0);
586143861Snjl}
587143861Snjl
588143861Snjlstatic int
589202771Sjkimacpi_ec_write_method(device_t dev, u_int addr, UINT64 val, int width)
590143861Snjl{
591143861Snjl    struct acpi_ec_softc *sc;
592143861Snjl    ACPI_STATUS status;
593143861Snjl
594143861Snjl    sc = device_get_softc(dev);
595143861Snjl    status = EcSpaceHandler(ACPI_WRITE, addr, width * 8, &val, sc, NULL);
596143861Snjl    if (ACPI_FAILURE(status))
597143861Snjl	return (ENXIO);
598143861Snjl    return (0);
599143861Snjl}
600143861Snjl
601213737Savgstatic ACPI_STATUS
602213737SavgEcCheckStatus(struct acpi_ec_softc *sc, const char *msg, EC_EVENT event)
603213737Savg{
604213737Savg    ACPI_STATUS status;
605213737Savg    EC_STATUS ec_status;
606213737Savg
607213737Savg    status = AE_NO_HARDWARE_RESPONSE;
608213737Savg    ec_status = EC_GET_CSR(sc);
609213737Savg    if (sc->ec_burstactive && !(ec_status & EC_FLAG_BURST_MODE)) {
610213737Savg	CTR1(KTR_ACPI, "ec burst disabled in waitevent (%s)", msg);
611213737Savg	sc->ec_burstactive = FALSE;
612213737Savg    }
613213737Savg    if (EVENT_READY(event, ec_status)) {
614213737Savg	CTR2(KTR_ACPI, "ec %s wait ready, status %#x", msg, ec_status);
615213737Savg	status = AE_OK;
616213737Savg    }
617213737Savg    return (status);
618213737Savg}
619213737Savg
62067761Smsmithstatic void
62170018StakawataEcGpeQueryHandler(void *Context)
62267761Smsmith{
62367761Smsmith    struct acpi_ec_softc	*sc = (struct acpi_ec_softc *)Context;
62467761Smsmith    UINT8			Data;
62567761Smsmith    ACPI_STATUS			Status;
626216942Sjkim    int				retry, sci_enqueued;
62767761Smsmith    char			qxx[5];
62867761Smsmith
62996926Speter    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
630117384Snjl    KASSERT(Context != NULL, ("EcGpeQueryHandler called with NULL"));
63169744Smsmith
632167044Snjl    /* Serialize user access with EcSpaceHandler(). */
633172313Snjl    Status = EcLock(sc);
634117809Snjl    if (ACPI_FAILURE(Status)) {
635172313Snjl	device_printf(sc->ec_dev, "GpeQuery lock error: %s\n",
636172313Snjl	    AcpiFormatException(Status));
637117809Snjl	return;
638117809Snjl    }
639117809Snjl
640117384Snjl    /*
641117809Snjl     * Send a query command to the EC to find out which _Qxx call it
642117809Snjl     * wants to make.  This command clears the SCI bit and also the
643172313Snjl     * interrupt source since we are edge-triggered.  To prevent the GPE
644172313Snjl     * that may arise from running the query from causing another query
645172313Snjl     * to be queued, we clear the pending flag only after running it.
646117809Snjl     */
647216942Sjkim    sci_enqueued = sc->ec_sci_pend;
648213737Savg    for (retry = 0; retry < 2; retry++) {
649213737Savg	Status = EcCommand(sc, EC_COMMAND_QUERY);
650213737Savg	if (ACPI_SUCCESS(Status))
651213737Savg	    break;
652213737Savg	if (EcCheckStatus(sc, "retr_check",
653213737Savg	    EC_EVENT_INPUT_BUFFER_EMPTY) == AE_OK)
654213737Savg	    continue;
655213737Savg	else
656213737Savg	    break;
657213737Savg    }
658172313Snjl    sc->ec_sci_pend = FALSE;
659117384Snjl    if (ACPI_FAILURE(Status)) {
660117809Snjl	EcUnlock(sc);
661172313Snjl	device_printf(sc->ec_dev, "GPE query failed: %s\n",
662172313Snjl	    AcpiFormatException(Status));
663172313Snjl	return;
664117384Snjl    }
665117809Snjl    Data = EC_GET_DATA(sc);
666167044Snjl
667172313Snjl    /*
668172313Snjl     * We have to unlock before running the _Qxx method below since that
669172313Snjl     * method may attempt to read/write from EC address space, causing
670172313Snjl     * recursive acquisition of the lock.
671172313Snjl     */
672117809Snjl    EcUnlock(sc);
67367761Smsmith
674117384Snjl    /* Ignore the value for "no outstanding event". (13.3.5) */
675172313Snjl    CTR2(KTR_ACPI, "ec query ok,%s running _Q%02X", Data ? "" : " not", Data);
676117384Snjl    if (Data == 0)
677172313Snjl	return;
67867761Smsmith
679117384Snjl    /* Evaluate _Qxx to respond to the controller. */
680172313Snjl    snprintf(qxx, sizeof(qxx), "_Q%02X", Data);
681145059Smarks    AcpiUtStrupr(qxx);
682117384Snjl    Status = AcpiEvaluateObject(sc->ec_handle, qxx, NULL, NULL);
683117384Snjl    if (ACPI_FAILURE(Status) && Status != AE_NOT_FOUND) {
684172313Snjl	device_printf(sc->ec_dev, "evaluation of query method %s failed: %s\n",
685172313Snjl	    qxx, AcpiFormatException(Status));
68667761Smsmith    }
687216965Sjkim
688216965Sjkim    /* Reenable runtime GPE if its execution was deferred. */
689216942Sjkim    if (sci_enqueued) {
690216942Sjkim	Status = AcpiFinishGpe(sc->ec_gpehandle, sc->ec_gpebit);
691216942Sjkim	if (ACPI_FAILURE(Status))
692216965Sjkim	    device_printf(sc->ec_dev, "reenabling runtime GPE failed: %s\n",
693216942Sjkim		AcpiFormatException(Status));
694216942Sjkim    }
69570018Stakawata}
69671873Smsmith
69778992Smsmith/*
698172313Snjl * The GPE handler is called when IBE/OBF or SCI events occur.  We are
699172313Snjl * called from an unknown lock context.
70078992Smsmith */
701216471Sjkimstatic UINT32
702216471SjkimEcGpeHandler(ACPI_HANDLE GpeDevice, UINT32 GpeNumber, void *Context)
70370018Stakawata{
70478992Smsmith    struct acpi_ec_softc *sc = Context;
705117384Snjl    ACPI_STATUS		       Status;
706167044Snjl    EC_STATUS		       EcStatus;
70778992Smsmith
708117384Snjl    KASSERT(Context != NULL, ("EcGpeHandler called with NULL"));
709172313Snjl    CTR0(KTR_ACPI, "ec gpe handler start");
710117384Snjl
711142191Snjl    /*
712172313Snjl     * Notify EcWaitEvent() that the status register is now fresh.  If we
713172313Snjl     * didn't do this, it wouldn't be possible to distinguish an old IBE
714172313Snjl     * from a new one, for example when doing a write transaction (writing
715172313Snjl     * address and then data values.)
716142191Snjl     */
717172313Snjl    atomic_add_int(&sc->ec_gencount, 1);
718216940Sjkim    wakeup(sc);
719117384Snjl
720167044Snjl    /*
721172313Snjl     * If the EC_SCI bit of the status register is set, queue a query handler.
722172313Snjl     * It will run the query and _Qxx method later, under the lock.
723167044Snjl     */
724167044Snjl    EcStatus = EC_GET_CSR(sc);
725172313Snjl    if ((EcStatus & EC_EVENT_SCI) && !sc->ec_sci_pend) {
726172313Snjl	CTR0(KTR_ACPI, "ec gpe queueing query handler");
727167814Sjkim	Status = AcpiOsExecute(OSL_GPE_HANDLER, EcGpeQueryHandler, Context);
728216942Sjkim	if (ACPI_SUCCESS(Status)) {
729167044Snjl	    sc->ec_sci_pend = TRUE;
730216942Sjkim	    return (0);
731216942Sjkim	} else
732172313Snjl	    printf("EcGpeHandler: queuing GPE query handler failed\n");
733167044Snjl    }
734216471Sjkim    return (ACPI_REENABLE_GPE);
73567761Smsmith}
73667761Smsmith
73767761Smsmithstatic ACPI_STATUS
738116927SnjlEcSpaceSetup(ACPI_HANDLE Region, UINT32 Function, void *Context,
739116927Snjl	     void **RegionContext)
74067761Smsmith{
74171873Smsmith
74296926Speter    ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
74371873Smsmith
74467761Smsmith    /*
745124091Snjl     * If deactivating a region, always set the output to NULL.  Otherwise,
746124091Snjl     * just pass the context through.
74767761Smsmith     */
748124091Snjl    if (Function == ACPI_REGION_DEACTIVATE)
749124091Snjl	*RegionContext = NULL;
750124091Snjl    else
751124091Snjl	*RegionContext = Context;
75267761Smsmith
753116927Snjl    return_ACPI_STATUS (AE_OK);
75467761Smsmith}
75567761Smsmith
75667761Smsmithstatic ACPI_STATUS
757208722SjkimEcSpaceHandler(UINT32 Function, ACPI_PHYSICAL_ADDRESS Address, UINT32 Width,
758202771Sjkim	       UINT64 *Value, void *Context, void *RegionContext)
75967761Smsmith{
76067761Smsmith    struct acpi_ec_softc	*sc = (struct acpi_ec_softc *)Context;
761210977Sjkim    ACPI_PHYSICAL_ADDRESS	EcAddr;
762210977Sjkim    UINT8			*EcData;
763126578Snjl    ACPI_STATUS			Status;
76467761Smsmith
76596926Speter    ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, (UINT32)Address);
76669744Smsmith
767210977Sjkim    if (Function != ACPI_READ && Function != ACPI_WRITE)
768210977Sjkim	return_ACPI_STATUS (AE_BAD_PARAMETER);
769208722Sjkim    if (Width % 8 != 0 || Value == NULL || Context == NULL)
770117384Snjl	return_ACPI_STATUS (AE_BAD_PARAMETER);
771210977Sjkim    if (Address + Width / 8 > 256)
772126578Snjl	return_ACPI_STATUS (AE_BAD_ADDRESS);
77367761Smsmith
774172313Snjl    /*
775172313Snjl     * If booting, check if we need to run the query handler.  If so, we
776172313Snjl     * we call it directly here since our thread taskq is not active yet.
777172313Snjl     */
778189903Sjkim    if (cold || rebooting || sc->ec_suspending) {
779172313Snjl	if ((EC_GET_CSR(sc) & EC_EVENT_SCI)) {
780172313Snjl	    CTR0(KTR_ACPI, "ec running gpe handler directly");
781172313Snjl	    EcGpeQueryHandler(sc);
782172313Snjl	}
783172313Snjl    }
784172313Snjl
785172313Snjl    /* Serialize with EcGpeQueryHandler() at transaction granularity. */
786172313Snjl    Status = EcLock(sc);
787133617Snjl    if (ACPI_FAILURE(Status))
788133617Snjl	return_ACPI_STATUS (Status);
789133617Snjl
790210977Sjkim    /* If we can't start burst mode, continue anyway. */
791210977Sjkim    Status = EcCommand(sc, EC_COMMAND_BURST_ENABLE);
792210977Sjkim    if (ACPI_SUCCESS(Status)) {
793210977Sjkim	if (EC_GET_DATA(sc) == EC_BURST_ACK) {
794210977Sjkim	    CTR0(KTR_ACPI, "ec burst enabled");
795210977Sjkim	    sc->ec_burstactive = TRUE;
796210977Sjkim	}
797210977Sjkim    }
798210977Sjkim
799208722Sjkim    /* Perform the transaction(s), based on Width. */
800210977Sjkim    EcAddr = Address;
801210977Sjkim    EcData = (UINT8 *)Value;
802210977Sjkim    if (Function == ACPI_READ)
803210977Sjkim	*Value = 0;
804210977Sjkim    do {
805117384Snjl	switch (Function) {
806117384Snjl	case ACPI_READ:
807208722Sjkim	    Status = EcRead(sc, EcAddr, EcData);
80873107Sjhb	    break;
809117384Snjl	case ACPI_WRITE:
810208722Sjkim	    Status = EcWrite(sc, EcAddr, *EcData);
811117384Snjl	    break;
812117384Snjl	}
813117384Snjl	if (ACPI_FAILURE(Status))
814126578Snjl	    break;
815210977Sjkim	EcAddr++;
816210977Sjkim	EcData++;
817210977Sjkim    } while (EcAddr < Address + Width / 8);
818210977Sjkim
819210977Sjkim    if (sc->ec_burstactive) {
820210977Sjkim	sc->ec_burstactive = FALSE;
821210977Sjkim	if (ACPI_SUCCESS(EcCommand(sc, EC_COMMAND_BURST_DISABLE)))
822210977Sjkim	    CTR0(KTR_ACPI, "ec disabled burst ok");
823126578Snjl    }
82478992Smsmith
825133617Snjl    EcUnlock(sc);
826117384Snjl    return_ACPI_STATUS (Status);
82770018Stakawata}
82867761Smsmith
82967761Smsmithstatic ACPI_STATUS
830172313SnjlEcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event, u_int gen_count)
831172313Snjl{
832213737Savg    static int	no_intr = 0;
833117384Snjl    ACPI_STATUS	Status;
834213737Savg    int		count, i, need_poll, slp_ival;
83567761Smsmith
836133617Snjl    ACPI_SERIAL_ASSERT(ec);
837117384Snjl    Status = AE_NO_HARDWARE_RESPONSE;
838213737Savg    need_poll = cold || rebooting || ec_polled_mode || sc->ec_suspending;
839172313Snjl
840172313Snjl    /* Wait for event by polling or GPE (interrupt). */
841172978Stakawata    if (need_poll) {
842172313Snjl	count = (ec_timeout * 1000) / EC_POLL_DELAY;
843172313Snjl	if (count == 0)
844172313Snjl	    count = 1;
845213737Savg	DELAY(10);
846172313Snjl	for (i = 0; i < count; i++) {
847172313Snjl	    Status = EcCheckStatus(sc, "poll", Event);
848172313Snjl	    if (Status == AE_OK)
849172313Snjl		break;
850213737Savg	    DELAY(EC_POLL_DELAY);
851172313Snjl	}
852172313Snjl    } else {
853172313Snjl	slp_ival = hz / 1000;
854172313Snjl	if (slp_ival != 0) {
855167044Snjl	    count = ec_timeout;
856172313Snjl	} else {
857172313Snjl	    /* hz has less than 1 ms resolution so scale timeout. */
858172313Snjl	    slp_ival = 1;
859172313Snjl	    count = ec_timeout / (1000 / hz);
860172313Snjl	}
861172313Snjl
862172313Snjl	/*
863172313Snjl	 * Wait for the GPE to signal the status changed, checking the
864172313Snjl	 * status register each time we get one.  It's possible to get a
865172313Snjl	 * GPE for an event we're not interested in here (i.e., SCI for
866172313Snjl	 * EC query).
867172313Snjl	 */
868131384Snjl	for (i = 0; i < count; i++) {
869213737Savg	    if (gen_count == sc->ec_gencount)
870216940Sjkim		tsleep(sc, 0, "ecgpe", slp_ival);
871213737Savg	    /*
872213737Savg	     * Record new generation count.  It's possible the GPE was
873213737Savg	     * just to notify us that a query is needed and we need to
874213737Savg	     * wait for a second GPE to signal the completion of the
875213737Savg	     * event we are actually waiting for.
876213737Savg	     */
877213737Savg	    Status = EcCheckStatus(sc, "sleep", Event);
878213737Savg	    if (Status == AE_OK) {
879213737Savg		if (gen_count == sc->ec_gencount)
880213737Savg		    no_intr++;
881213737Savg		else
882213737Savg		    no_intr = 0;
883213737Savg		break;
884167044Snjl	    }
885213737Savg	    gen_count = sc->ec_gencount;
886117384Snjl	}
887172313Snjl
888172313Snjl	/*
889172313Snjl	 * We finished waiting for the GPE and it never arrived.  Try to
890172313Snjl	 * read the register once and trust whatever value we got.  This is
891213737Savg	 * the best we can do at this point.
892172313Snjl	 */
893213737Savg	if (Status != AE_OK)
894172313Snjl	    Status = EcCheckStatus(sc, "sleep_end", Event);
89567761Smsmith    }
896213737Savg    if (!need_poll && no_intr > 10) {
897213737Savg	device_printf(sc->ec_dev,
898213737Savg	    "not getting interrupts, switched to polled mode\n");
899213737Savg	ec_polled_mode = 1;
900213737Savg    }
901172313Snjl    if (Status != AE_OK)
902172313Snjl	    CTR0(KTR_ACPI, "error: ec wait timed out");
903116927Snjl    return (Status);
904172313Snjl}
90567761Smsmith
90667761Smsmithstatic ACPI_STATUS
907117384SnjlEcCommand(struct acpi_ec_softc *sc, EC_COMMAND cmd)
90867761Smsmith{
909167044Snjl    ACPI_STATUS	status;
910167044Snjl    EC_EVENT	event;
911167044Snjl    EC_STATUS	ec_status;
912172313Snjl    u_int	gen_count;
91367761Smsmith
914133617Snjl    ACPI_SERIAL_ASSERT(ec);
91567761Smsmith
916167044Snjl    /* Don't use burst mode if user disabled it. */
917167044Snjl    if (!ec_burst_mode && cmd == EC_COMMAND_BURST_ENABLE)
918167044Snjl	return (AE_ERROR);
919167044Snjl
920117384Snjl    /* Decide what to wait for based on command type. */
921117384Snjl    switch (cmd) {
92267761Smsmith    case EC_COMMAND_READ:
92367761Smsmith    case EC_COMMAND_WRITE:
924117384Snjl    case EC_COMMAND_BURST_DISABLE:
925167044Snjl	event = EC_EVENT_INPUT_BUFFER_EMPTY;
92667761Smsmith	break;
927117384Snjl    case EC_COMMAND_QUERY:
928117384Snjl    case EC_COMMAND_BURST_ENABLE:
929167044Snjl	event = EC_EVENT_OUTPUT_BUFFER_FULL;
930117384Snjl	break;
93167761Smsmith    default:
932172313Snjl	device_printf(sc->ec_dev, "EcCommand: invalid command %#x\n", cmd);
933117384Snjl	return (AE_BAD_PARAMETER);
93467761Smsmith    }
93567761Smsmith
936213737Savg    /*
937213737Savg     * Ensure empty input buffer before issuing command.
938213737Savg     * Use generation count of zero to force a quick check.
939213737Savg     */
940213737Savg    status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY, 0);
941213737Savg    if (ACPI_FAILURE(status))
942213737Savg	return (status);
943213737Savg
944117384Snjl    /* Run the command and wait for the chosen event. */
945167044Snjl    CTR1(KTR_ACPI, "ec running command %#x", cmd);
946172313Snjl    gen_count = sc->ec_gencount;
947117384Snjl    EC_SET_CSR(sc, cmd);
948172313Snjl    status = EcWaitEvent(sc, event, gen_count);
949167044Snjl    if (ACPI_SUCCESS(status)) {
950167044Snjl	/* If we succeeded, burst flag should now be present. */
951167044Snjl	if (cmd == EC_COMMAND_BURST_ENABLE) {
952167044Snjl	    ec_status = EC_GET_CSR(sc);
953167044Snjl	    if ((ec_status & EC_FLAG_BURST_MODE) == 0)
954167044Snjl		status = AE_ERROR;
955167044Snjl	}
956172313Snjl    } else
957172313Snjl	device_printf(sc->ec_dev, "EcCommand: no response to %#x\n", cmd);
958167044Snjl    return (status);
95967761Smsmith}
96067761Smsmith
96167761Smsmithstatic ACPI_STATUS
96267761SmsmithEcRead(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data)
96367761Smsmith{
964167044Snjl    ACPI_STATUS	status;
965172313Snjl    u_int gen_count;
966213737Savg    int retry;
96767761Smsmith
968133617Snjl    ACPI_SERIAL_ASSERT(ec);
969153171Snjl    CTR1(KTR_ACPI, "ec read from %#x", Address);
97067761Smsmith
971213737Savg    for (retry = 0; retry < 2; retry++) {
972213737Savg	status = EcCommand(sc, EC_COMMAND_READ);
973213737Savg	if (ACPI_FAILURE(status))
974213737Savg	    return (status);
97567761Smsmith
976213737Savg	gen_count = sc->ec_gencount;
977213737Savg	EC_SET_DATA(sc, Address);
978213737Savg	status = EcWaitEvent(sc, EC_EVENT_OUTPUT_BUFFER_FULL, gen_count);
979213737Savg	if (ACPI_FAILURE(status)) {
980213737Savg	    if (EcCheckStatus(sc, "retr_check",
981213737Savg		EC_EVENT_INPUT_BUFFER_EMPTY) == AE_OK)
982213737Savg		continue;
983213737Savg	    else
984213737Savg		break;
985213737Savg	}
986213737Savg	*Data = EC_GET_DATA(sc);
987213737Savg	return (AE_OK);
98867761Smsmith    }
989213737Savg    device_printf(sc->ec_dev, "EcRead: failed waiting to get data\n");
990213737Savg    return (status);
991172313Snjl}
99267761Smsmith
99367761Smsmithstatic ACPI_STATUS
994208722SjkimEcWrite(struct acpi_ec_softc *sc, UINT8 Address, UINT8 Data)
99567761Smsmith{
996167044Snjl    ACPI_STATUS	status;
997172313Snjl    u_int gen_count;
99867761Smsmith
999133617Snjl    ACPI_SERIAL_ASSERT(ec);
1000208722Sjkim    CTR2(KTR_ACPI, "ec write to %#x, data %#x", Address, Data);
100167761Smsmith
1002167044Snjl    status = EcCommand(sc, EC_COMMAND_WRITE);
1003167044Snjl    if (ACPI_FAILURE(status))
1004167044Snjl	return (status);
100567761Smsmith
1006172313Snjl    gen_count = sc->ec_gencount;
100767761Smsmith    EC_SET_DATA(sc, Address);
1008172313Snjl    status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY, gen_count);
1009167044Snjl    if (ACPI_FAILURE(status)) {
1010213737Savg	device_printf(sc->ec_dev, "EcWrite: failed waiting for sent address\n");
1011167044Snjl	return (status);
101267761Smsmith    }
101367761Smsmith
1014172313Snjl    gen_count = sc->ec_gencount;
1015208722Sjkim    EC_SET_DATA(sc, Data);
1016172313Snjl    status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY, gen_count);
1017167044Snjl    if (ACPI_FAILURE(status)) {
1018172313Snjl	device_printf(sc->ec_dev, "EcWrite: failed waiting for sent data\n");
1019167044Snjl	return (status);
102067761Smsmith    }
102167761Smsmith
1022116927Snjl    return (AE_OK);
102370018Stakawata}
1024