2 * Copyright (c) 2003-2007 Nate Lawson
3 * Copyright (c) 2000 Michael Smith
4 * Copyright (c) 2000 BSDi
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * $FreeBSD: head/sys/dev/acpica/acpi_ec.c 246128 2013-01-30 18:01:20Z sbz $
32 #include <sys/param.h>
33 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
44 #include <dev/acpica/acpivar.h>
46 /* Hooks for the ACPICA debugging infrastructure */
47 #define _COMPONENT ACPI_EC
48 ACPI_MODULE_NAME("EC")
56 typedef UINT8 EC_COMMAND;
58 #define EC_COMMAND_UNKNOWN ((EC_COMMAND) 0x00)
59 #define EC_COMMAND_READ ((EC_COMMAND) 0x80)
60 #define EC_COMMAND_WRITE ((EC_COMMAND) 0x81)
61 #define EC_COMMAND_BURST_ENABLE ((EC_COMMAND) 0x82)
62 #define EC_COMMAND_BURST_DISABLE ((EC_COMMAND) 0x83)
63 #define EC_COMMAND_QUERY ((EC_COMMAND) 0x84)
68 * The encoding of the EC status register is illustrated below.
69 * Note that a set bit (1) indicates the property is TRUE
70 * (e.g. if bit 0 is set then the output buffer is full).
75 * | | | | | | | +- Output Buffer Full?
76 * | | | | | | +--- Input Buffer Full?
77 * | | | | | +----- <reserved>
78 * | | | | +------- Data Register is Command Byte?
79 * | | | +--------- Burst Mode Enabled?
80 * | | +----------- SCI Event?
81 * | +------------- SMI Event?
82 * +--------------- <reserved>
85 typedef UINT8 EC_STATUS;
87 #define EC_FLAG_OUTPUT_BUFFER ((EC_STATUS) 0x01)
88 #define EC_FLAG_INPUT_BUFFER ((EC_STATUS) 0x02)
89 #define EC_FLAG_DATA_IS_CMD ((EC_STATUS) 0x08)
90 #define EC_FLAG_BURST_MODE ((EC_STATUS) 0x10)
96 typedef UINT8 EC_EVENT;
98 #define EC_EVENT_UNKNOWN ((EC_EVENT) 0x00)
99 #define EC_EVENT_OUTPUT_BUFFER_FULL ((EC_EVENT) 0x01)
100 #define EC_EVENT_INPUT_BUFFER_EMPTY ((EC_EVENT) 0x02)
101 #define EC_EVENT_SCI ((EC_EVENT) 0x20)
102 #define EC_EVENT_SMI ((EC_EVENT) 0x40)
104 /* Data byte returned after burst enable indicating it was successful. */
105 #define EC_BURST_ACK 0x90
108 * Register access primitives
110 #define EC_GET_DATA(sc) \
111 bus_space_read_1((sc)->ec_data_tag, (sc)->ec_data_handle, 0)
113 #define EC_SET_DATA(sc, v) \
114 bus_space_write_1((sc)->ec_data_tag, (sc)->ec_data_handle, 0, (v))
116 #define EC_GET_CSR(sc) \
117 bus_space_read_1((sc)->ec_csr_tag, (sc)->ec_csr_handle, 0)
119 #define EC_SET_CSR(sc, v) \
120 bus_space_write_1((sc)->ec_csr_tag, (sc)->ec_csr_handle, 0, (v))
122 /* Additional params to pass from the probe routine */
123 struct acpi_ec_params {
126 ACPI_HANDLE gpe_handle;
133 struct acpi_ec_softc {
135 ACPI_HANDLE ec_handle;
137 ACPI_HANDLE ec_gpehandle;
141 struct resource *ec_data_res;
142 bus_space_tag_t ec_data_tag;
143 bus_space_handle_t ec_data_handle;
146 struct resource *ec_csr_res;
147 bus_space_tag_t ec_csr_tag;
148 bus_space_handle_t ec_csr_handle;
154 volatile u_int ec_gencount;
160 * I couldn't find it in the spec but other implementations also use a
161 * value of 1 ms for the time to acquire global lock.
163 #define EC_LOCK_TIMEOUT 1000
165 /* Default delay in microseconds between each run of the status polling loop. */
166 #define EC_POLL_DELAY 50
168 /* Total time in ms spent waiting for a response from EC. */
169 #define EC_TIMEOUT 750
171 #define EVENT_READY(event, status) \
172 (((event) == EC_EVENT_OUTPUT_BUFFER_FULL && \
173 ((status) & EC_FLAG_OUTPUT_BUFFER) != 0) || \
174 ((event) == EC_EVENT_INPUT_BUFFER_EMPTY && \
175 ((status) & EC_FLAG_INPUT_BUFFER) == 0))
177 ACPI_SERIAL_DECL(ec, "ACPI embedded controller");
179 static SYSCTL_NODE(_debug_acpi, OID_AUTO, ec, CTLFLAG_RD, NULL, "EC debugging");
181 static int ec_burst_mode;
182 TUNABLE_INT("debug.acpi.ec.burst", &ec_burst_mode);
183 SYSCTL_INT(_debug_acpi_ec, OID_AUTO, burst, CTLFLAG_RW, &ec_burst_mode, 0,
184 "Enable use of burst mode (faster for nearly all systems)");
185 static int ec_polled_mode;
186 TUNABLE_INT("debug.acpi.ec.polled", &ec_polled_mode);
187 SYSCTL_INT(_debug_acpi_ec, OID_AUTO, polled, CTLFLAG_RW, &ec_polled_mode, 0,
188 "Force use of polled mode (only if interrupt mode doesn't work)");
189 static int ec_timeout = EC_TIMEOUT;
190 TUNABLE_INT("debug.acpi.ec.timeout", &ec_timeout);
191 SYSCTL_INT(_debug_acpi_ec, OID_AUTO, timeout, CTLFLAG_RW, &ec_timeout,
192 EC_TIMEOUT, "Total time spent waiting for a response (poll+sleep)");
195 #define KTR_ACPI_EC KTR_ALL
198 KTR_INFO_MASTER(acpi_ec);
199 KTR_INFO(KTR_ACPI_EC, acpi_ec, burstdis, 0,
200 "ec burst disabled in waitevent (%s)", const char *msg);
201 KTR_INFO(KTR_ACPI_EC, acpi_ec, burstdisok, 1,
202 "ec disabled burst ok");
203 KTR_INFO(KTR_ACPI_EC, acpi_ec, burstenl, 2,
205 KTR_INFO(KTR_ACPI_EC, acpi_ec, cmdrun, 3,
206 "ec running command %#x", EC_COMMAND cmd);
207 KTR_INFO(KTR_ACPI_EC, acpi_ec, gpehdlstart, 4,
208 "ec gpe handler start");
209 KTR_INFO(KTR_ACPI_EC, acpi_ec, gpequeuehdl, 5,
210 "ec gpe queueing query handler");
211 KTR_INFO(KTR_ACPI_EC, acpi_ec, gperun, 6,
212 "ec running gpe handler directly");
213 KTR_INFO(KTR_ACPI_EC, acpi_ec, qryoknotrun, 7,
214 "ec query ok, not running _Q%02X", uint8_t Data);
215 KTR_INFO(KTR_ACPI_EC, acpi_ec, qryokrun, 8,
216 "ec query ok, running _Q%02X", uint8_t Data);
217 KTR_INFO(KTR_ACPI_EC, acpi_ec, readaddr, 9,
218 "ec read from %#x", UINT8 Address);
219 KTR_INFO(KTR_ACPI_EC, acpi_ec, timeout, 10,
220 "error: ec wait timed out");
221 KTR_INFO(KTR_ACPI_EC, acpi_ec, waitrdy, 11,
222 "ec %s wait ready, status %#x", const char *msg, EC_STATUS ec_status);
223 KTR_INFO(KTR_ACPI_EC, acpi_ec, writeaddr, 12,
224 "ec write to %#x, data %#x", UINT8 Address, UINT8 Data);
227 EcLock(struct acpi_ec_softc *sc)
231 /* If _GLK is non-zero, acquire the global lock. */
234 status = AcpiAcquireGlobalLock(EC_LOCK_TIMEOUT, &sc->ec_glkhandle);
235 if (ACPI_FAILURE(status))
238 ACPI_SERIAL_BEGIN(ec);
243 EcUnlock(struct acpi_ec_softc *sc)
247 AcpiReleaseGlobalLock(sc->ec_glkhandle);
250 static UINT32 EcGpeHandler(ACPI_HANDLE GpeDevice,
251 UINT32 GpeNumber, void *Context);
252 static ACPI_STATUS EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function,
253 void *Context, void **return_Context);
254 static ACPI_STATUS EcSpaceHandler(UINT32 Function,
255 ACPI_PHYSICAL_ADDRESS Address,
256 UINT32 Width, UINT64 *Value,
257 void *Context, void *RegionContext);
258 static ACPI_STATUS EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event,
260 static ACPI_STATUS EcCommand(struct acpi_ec_softc *sc, EC_COMMAND cmd);
261 static ACPI_STATUS EcRead(struct acpi_ec_softc *sc, UINT8 Address,
263 static ACPI_STATUS EcWrite(struct acpi_ec_softc *sc, UINT8 Address,
265 static int acpi_ec_probe(device_t dev);
266 static int acpi_ec_attach(device_t dev);
267 static int acpi_ec_suspend(device_t dev);
268 static int acpi_ec_resume(device_t dev);
269 static int acpi_ec_shutdown(device_t dev);
270 static int acpi_ec_read_method(device_t dev, u_int addr,
271 UINT64 *val, int width);
272 static int acpi_ec_write_method(device_t dev, u_int addr,
273 UINT64 val, int width);
275 static device_method_t acpi_ec_methods[] = {
276 /* Device interface */
277 DEVMETHOD(device_probe, acpi_ec_probe),
278 DEVMETHOD(device_attach, acpi_ec_attach),
279 DEVMETHOD(device_suspend, acpi_ec_suspend),
280 DEVMETHOD(device_resume, acpi_ec_resume),
281 DEVMETHOD(device_shutdown, acpi_ec_shutdown),
283 /* Embedded controller interface */
284 DEVMETHOD(acpi_ec_read, acpi_ec_read_method),
285 DEVMETHOD(acpi_ec_write, acpi_ec_write_method),
290 static driver_t acpi_ec_driver = {
293 sizeof(struct acpi_ec_softc),
296 static devclass_t acpi_ec_devclass;
297 DRIVER_MODULE(acpi_ec, acpi, acpi_ec_driver, acpi_ec_devclass, NULL, NULL);
298 MODULE_DEPEND(acpi_ec, acpi, 1, 1, 1);
301 * Look for an ECDT and if we find one, set up default GPE and
302 * space handlers to catch attempts to access EC space before
303 * we have a real driver instance in place.
305 * TODO: Some old Gateway laptops need us to fake up an ECDT or
306 * otherwise attach early so that _REG methods can run.
309 acpi_ec_ecdt_probe(device_t parent)
311 ACPI_TABLE_ECDT *ecdt;
315 struct acpi_ec_params *params;
317 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
319 /* Find and validate the ECDT. */
320 status = AcpiGetTable(ACPI_SIG_ECDT, 1, (ACPI_TABLE_HEADER **)&ecdt);
321 if (ACPI_FAILURE(status) ||
322 ecdt->Control.BitWidth != 8 ||
323 ecdt->Data.BitWidth != 8) {
327 /* Create the child device with the given unit number. */
328 child = BUS_ADD_CHILD(parent, parent, 0, "acpi_ec", ecdt->Uid);
330 kprintf("%s: can't add child\n", __func__);
334 /* Find and save the ACPI handle for this device. */
335 status = AcpiGetHandle(NULL, ecdt->Id, &h);
336 if (ACPI_FAILURE(status)) {
337 device_delete_child(parent, child);
338 kprintf("%s: can't get handle\n", __func__);
341 acpi_set_handle(child, h);
343 /* Set the data and CSR register addresses. */
344 bus_set_resource(child, SYS_RES_IOPORT, 0, ecdt->Data.Address,
346 bus_set_resource(child, SYS_RES_IOPORT, 1, ecdt->Control.Address,
350 * Store values for the probe/attach routines to use. Store the
351 * ECDT GPE bit and set the global lock flag according to _GLK.
352 * Note that it is not perfectly correct to be evaluating a method
353 * before initializing devices, but in practice this function
354 * should be safe to call at this point.
356 params = kmalloc(sizeof(struct acpi_ec_params), M_TEMP, M_WAITOK | M_ZERO);
357 params->gpe_handle = NULL;
358 params->gpe_bit = ecdt->Gpe;
359 params->uid = ecdt->Uid;
360 acpi_GetInteger(h, "_GLK", ¶ms->glk);
361 acpi_set_private(child, params);
363 /* Finish the attach process. */
364 if (device_probe_and_attach(child) != 0)
365 device_delete_child(parent, child);
369 acpi_ec_probe(device_t dev)
379 struct acpi_ec_params *params;
380 static char *ec_ids[] = { "PNP0C09", NULL };
382 /* Check that this is a device and that EC is not disabled. */
383 if (acpi_get_type(dev) != ACPI_TYPE_DEVICE || acpi_disabled("ec"))
387 * If probed via ECDT, set description and continue. Otherwise,
388 * we can access the namespace and make sure this is not a
394 buf.Length = ACPI_ALLOCATE_BUFFER;
395 params = acpi_get_private(dev);
396 if (params != NULL) {
399 } else if (ACPI_ID_PROBE(device_get_parent(dev), dev, ec_ids)) {
400 params = kmalloc(sizeof(struct acpi_ec_params), M_TEMP,
402 h = acpi_get_handle(dev);
405 * Read the unit ID to check for duplicate attach and the
406 * global lock value to see if we should acquire it when
409 status = acpi_GetInteger(h, "_UID", ¶ms->uid);
410 if (ACPI_FAILURE(status))
412 status = acpi_GetInteger(h, "_GLK", ¶ms->glk);
413 if (ACPI_FAILURE(status))
417 * Evaluate the _GPE method to find the GPE bit used by the EC to
418 * signal status (SCI). If it's a package, it contains a reference
419 * and GPE bit, similar to _PRW.
421 status = AcpiEvaluateObject(h, "_GPE", NULL, &buf);
422 if (ACPI_FAILURE(status)) {
423 device_printf(dev, "can't evaluate _GPE - %s\n",
424 AcpiFormatException(status));
427 obj = (ACPI_OBJECT *)buf.Pointer;
432 case ACPI_TYPE_INTEGER:
433 params->gpe_handle = NULL;
434 params->gpe_bit = obj->Integer.Value;
436 case ACPI_TYPE_PACKAGE:
437 if (!ACPI_PKG_VALID(obj, 2))
440 acpi_GetReference(NULL, &obj->Package.Elements[0]);
441 if (params->gpe_handle == NULL ||
442 acpi_PkgInt32(obj, 1, ¶ms->gpe_bit) != 0)
446 device_printf(dev, "_GPE has invalid type %d\n", obj->Type);
450 /* Store the values we got from the namespace for attach. */
451 acpi_set_private(dev, params);
454 * Check for a duplicate probe. This can happen when a probe
455 * via ECDT succeeded already. If this is a duplicate, disable
458 peer = devclass_get_device(acpi_ec_devclass, params->uid);
459 if (peer == NULL || !device_is_alive(peer))
467 ksnprintf(desc, sizeof(desc), "Embedded Controller: GPE %#x%s%s",
468 params->gpe_bit, (params->glk) ? ", GLK" : "",
469 ecdt ? ", ECDT" : "");
470 device_set_desc_copy(dev, desc);
473 if (ret > 0 && params)
474 kfree(params, M_TEMP);
476 AcpiOsFree(buf.Pointer);
481 acpi_ec_attach(device_t dev)
483 struct acpi_ec_softc *sc;
484 struct acpi_ec_params *params;
487 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
489 /* Fetch/initialize softc (assumes softc is pre-zeroed). */
490 sc = device_get_softc(dev);
491 params = acpi_get_private(dev);
493 sc->ec_handle = acpi_get_handle(dev);
494 ACPI_SERIAL_INIT(ec);
496 /* Retrieve previously probed values via device ivars. */
497 sc->ec_glk = params->glk;
498 sc->ec_gpebit = params->gpe_bit;
499 sc->ec_gpehandle = params->gpe_handle;
500 sc->ec_uid = params->uid;
501 sc->ec_suspending = FALSE;
502 acpi_set_private(dev, NULL);
503 kfree(params, M_TEMP);
505 /* Attach bus resources for data and command/status ports. */
507 sc->ec_data_res = bus_alloc_resource_any(sc->ec_dev, SYS_RES_IOPORT,
508 &sc->ec_data_rid, RF_ACTIVE);
509 if (sc->ec_data_res == NULL) {
510 device_printf(dev, "can't allocate data port\n");
513 sc->ec_data_tag = rman_get_bustag(sc->ec_data_res);
514 sc->ec_data_handle = rman_get_bushandle(sc->ec_data_res);
517 sc->ec_csr_res = bus_alloc_resource_any(sc->ec_dev, SYS_RES_IOPORT,
518 &sc->ec_csr_rid, RF_ACTIVE);
519 if (sc->ec_csr_res == NULL) {
520 device_printf(dev, "can't allocate command/status port\n");
523 sc->ec_csr_tag = rman_get_bustag(sc->ec_csr_res);
524 sc->ec_csr_handle = rman_get_bushandle(sc->ec_csr_res);
527 * Install a handler for this EC's GPE bit. We want edge-triggered
530 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "attaching GPE handler\n"));
531 Status = AcpiInstallGpeHandler(sc->ec_gpehandle, sc->ec_gpebit,
532 ACPI_GPE_EDGE_TRIGGERED, EcGpeHandler, sc);
533 if (ACPI_FAILURE(Status)) {
534 device_printf(dev, "can't install GPE handler for %s - %s\n",
535 acpi_name(sc->ec_handle), AcpiFormatException(Status));
540 * Install address space handler
542 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "attaching address space handler\n"));
543 Status = AcpiInstallAddressSpaceHandler(sc->ec_handle, ACPI_ADR_SPACE_EC,
544 &EcSpaceHandler, &EcSpaceSetup, sc);
545 if (ACPI_FAILURE(Status)) {
546 device_printf(dev, "can't install address space handler for %s - %s\n",
547 acpi_name(sc->ec_handle), AcpiFormatException(Status));
551 /* Enable runtime GPEs for the handler. */
552 Status = AcpiEnableGpe(sc->ec_gpehandle, sc->ec_gpebit);
553 if (ACPI_FAILURE(Status)) {
554 device_printf(dev, "AcpiEnableGpe failed: %s\n",
555 AcpiFormatException(Status));
559 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "acpi_ec_attach complete\n"));
563 AcpiRemoveGpeHandler(sc->ec_gpehandle, sc->ec_gpebit, EcGpeHandler);
564 AcpiRemoveAddressSpaceHandler(sc->ec_handle, ACPI_ADR_SPACE_EC,
567 bus_release_resource(sc->ec_dev, SYS_RES_IOPORT, sc->ec_csr_rid,
570 bus_release_resource(sc->ec_dev, SYS_RES_IOPORT, sc->ec_data_rid,
576 acpi_ec_suspend(device_t dev)
578 struct acpi_ec_softc *sc;
580 sc = device_get_softc(dev);
581 sc->ec_suspending = TRUE;
586 acpi_ec_resume(device_t dev)
588 struct acpi_ec_softc *sc;
590 sc = device_get_softc(dev);
591 sc->ec_suspending = FALSE;
596 acpi_ec_shutdown(device_t dev)
598 struct acpi_ec_softc *sc;
600 /* Disable the GPE so we don't get EC events during shutdown. */
601 sc = device_get_softc(dev);
602 AcpiDisableGpe(sc->ec_gpehandle, sc->ec_gpebit);
606 /* Methods to allow other devices (e.g., smbat) to read/write EC space. */
608 acpi_ec_read_method(device_t dev, u_int addr, UINT64 *val, int width)
610 struct acpi_ec_softc *sc;
613 sc = device_get_softc(dev);
614 status = EcSpaceHandler(ACPI_READ, addr, width * 8, val, sc, NULL);
615 if (ACPI_FAILURE(status))
621 acpi_ec_write_method(device_t dev, u_int addr, UINT64 val, int width)
623 struct acpi_ec_softc *sc;
626 sc = device_get_softc(dev);
627 status = EcSpaceHandler(ACPI_WRITE, addr, width * 8, &val, sc, NULL);
628 if (ACPI_FAILURE(status))
634 EcCheckStatus(struct acpi_ec_softc *sc, const char *msg, EC_EVENT event)
639 status = AE_NO_HARDWARE_RESPONSE;
640 ec_status = EC_GET_CSR(sc);
641 if (sc->ec_burstactive && !(ec_status & EC_FLAG_BURST_MODE)) {
642 KTR_LOG(acpi_ec_burstdis, msg);
643 sc->ec_burstactive = FALSE;
645 if (EVENT_READY(event, ec_status)) {
646 KTR_LOG(acpi_ec_waitrdy, msg, ec_status);
653 EcGpeQueryHandler(void *Context)
655 struct acpi_ec_softc *sc = (struct acpi_ec_softc *)Context;
658 int retry, sci_enqueued;
661 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
662 KASSERT(Context != NULL, ("EcGpeQueryHandler called with NULL"));
664 /* Serialize user access with EcSpaceHandler(). */
666 if (ACPI_FAILURE(Status)) {
667 device_printf(sc->ec_dev, "GpeQuery lock error: %s\n",
668 AcpiFormatException(Status));
673 * Send a query command to the EC to find out which _Qxx call it
674 * wants to make. This command clears the SCI bit and also the
675 * interrupt source since we are edge-triggered. To prevent the GPE
676 * that may arise from running the query from causing another query
677 * to be queued, we clear the pending flag only after running it.
679 sci_enqueued = sc->ec_sci_pend;
680 for (retry = 0; retry < 2; retry++) {
681 Status = EcCommand(sc, EC_COMMAND_QUERY);
682 if (ACPI_SUCCESS(Status))
684 if (ACPI_SUCCESS(EcCheckStatus(sc, "retr_check",
685 EC_EVENT_INPUT_BUFFER_EMPTY)))
690 sc->ec_sci_pend = FALSE;
691 if (ACPI_FAILURE(Status)) {
693 device_printf(sc->ec_dev, "GPE query failed: %s\n",
694 AcpiFormatException(Status));
697 Data = EC_GET_DATA(sc);
700 * We have to unlock before running the _Qxx method below since that
701 * method may attempt to read/write from EC address space, causing
702 * recursive acquisition of the lock.
706 /* Ignore the value for "no outstanding event". (13.3.5) */
708 KTR_LOG(acpi_ec_qryoknotrun, Data);
711 KTR_LOG(acpi_ec_qryokrun, Data);
714 /* Evaluate _Qxx to respond to the controller. */
715 ksnprintf(qxx, sizeof(qxx), "_Q%02X", Data);
717 Status = AcpiEvaluateObject(sc->ec_handle, qxx, NULL, NULL);
718 if (ACPI_FAILURE(Status) && Status != AE_NOT_FOUND) {
719 device_printf(sc->ec_dev, "evaluation of query method %s failed: %s\n",
720 qxx, AcpiFormatException(Status));
723 /* Reenable runtime GPE if its execution was deferred. */
725 Status = AcpiFinishGpe(sc->ec_gpehandle, sc->ec_gpebit);
726 if (ACPI_FAILURE(Status))
727 device_printf(sc->ec_dev, "reenabling runtime GPE failed: %s\n",
728 AcpiFormatException(Status));
733 * The GPE handler is called when IBE/OBF or SCI events occur. We are
734 * called from an unknown lock context.
737 EcGpeHandler(ACPI_HANDLE GpeDevice, UINT32 GpeNumber, void *Context)
739 struct acpi_ec_softc *sc = Context;
743 KASSERT(Context != NULL, ("EcGpeHandler called with NULL"));
744 KTR_LOG(acpi_ec_gpehdlstart);
746 * Notify EcWaitEvent() that the status register is now fresh. If we
747 * didn't do this, it wouldn't be possible to distinguish an old IBE
748 * from a new one, for example when doing a write transaction (writing
749 * address and then data values.)
751 atomic_add_int(&sc->ec_gencount, 1);
755 * If the EC_SCI bit of the status register is set, queue a query handler.
756 * It will run the query and _Qxx method later, under the lock.
758 EcStatus = EC_GET_CSR(sc);
759 if ((EcStatus & EC_EVENT_SCI) && !sc->ec_sci_pend) {
760 KTR_LOG(acpi_ec_gpequeuehdl);
761 Status = AcpiOsExecute(OSL_GPE_HANDLER, EcGpeQueryHandler, Context);
762 if (ACPI_SUCCESS(Status)) {
763 sc->ec_sci_pend = TRUE;
766 kprintf("EcGpeHandler: queuing GPE query handler failed\n");
769 return (ACPI_REENABLE_GPE);
773 EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function, void *Context,
774 void **RegionContext)
777 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
780 * If deactivating a region, always set the output to NULL. Otherwise,
781 * just pass the context through.
783 if (Function == ACPI_REGION_DEACTIVATE)
784 *RegionContext = NULL;
786 *RegionContext = Context;
788 return_ACPI_STATUS (AE_OK);
792 EcSpaceHandler(UINT32 Function, ACPI_PHYSICAL_ADDRESS Address, UINT32 Width,
793 UINT64 *Value, void *Context, void *RegionContext)
795 struct acpi_ec_softc *sc = (struct acpi_ec_softc *)Context;
796 ACPI_PHYSICAL_ADDRESS EcAddr;
800 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, (UINT32)Address);
802 if (Function != ACPI_READ && Function != ACPI_WRITE)
803 return_ACPI_STATUS (AE_BAD_PARAMETER);
804 if (Width % 8 != 0 || Value == NULL || Context == NULL)
805 return_ACPI_STATUS (AE_BAD_PARAMETER);
806 if (Address + Width / 8 > 256)
807 return_ACPI_STATUS (AE_BAD_ADDRESS);
810 * If booting, check if we need to run the query handler. If so, we
811 * we call it directly here since our thread taskq is not active yet.
813 if (cold || rebooting || sc->ec_suspending) {
814 if ((EC_GET_CSR(sc) & EC_EVENT_SCI)) {
815 KTR_LOG(acpi_ec_gperun);
816 EcGpeQueryHandler(sc);
820 /* Serialize with EcGpeQueryHandler() at transaction granularity. */
822 if (ACPI_FAILURE(Status))
823 return_ACPI_STATUS (Status);
825 /* If we can't start burst mode, continue anyway. */
826 Status = EcCommand(sc, EC_COMMAND_BURST_ENABLE);
827 if (ACPI_SUCCESS(Status)) {
828 if (EC_GET_DATA(sc) == EC_BURST_ACK) {
829 KTR_LOG(acpi_ec_burstenl);
830 sc->ec_burstactive = TRUE;
834 /* Perform the transaction(s), based on Width. */
836 EcData = (UINT8 *)Value;
837 if (Function == ACPI_READ)
842 Status = EcRead(sc, EcAddr, EcData);
845 Status = EcWrite(sc, EcAddr, *EcData);
848 if (ACPI_FAILURE(Status))
852 } while (EcAddr < Address + Width / 8);
854 if (sc->ec_burstactive) {
855 sc->ec_burstactive = FALSE;
856 if (ACPI_SUCCESS(EcCommand(sc, EC_COMMAND_BURST_DISABLE)))
857 KTR_LOG(acpi_ec_burstdisok);
861 return_ACPI_STATUS (Status);
865 EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event, u_int gen_count)
867 static int no_intr = 0;
869 int count, i, need_poll, slp_ival;
871 ACPI_SERIAL_ASSERT(ec);
872 Status = AE_NO_HARDWARE_RESPONSE;
873 need_poll = cold || rebooting || ec_polled_mode || sc->ec_suspending;
875 /* Wait for event by polling or GPE (interrupt). */
877 count = (ec_timeout * 1000) / EC_POLL_DELAY;
881 for (i = 0; i < count; i++) {
882 Status = EcCheckStatus(sc, "poll", Event);
883 if (ACPI_SUCCESS(Status))
885 DELAY(EC_POLL_DELAY);
888 slp_ival = hz / 1000;
892 /* hz has less than 1 ms resolution so scale timeout. */
894 count = ec_timeout / (1000 / hz);
898 * Wait for the GPE to signal the status changed, checking the
899 * status register each time we get one. It's possible to get a
900 * GPE for an event we're not interested in here (i.e., SCI for
903 for (i = 0; i < count; i++) {
904 if (gen_count == sc->ec_gencount)
905 tsleep(sc, 0, "ecgpe", slp_ival);
907 * Record new generation count. It's possible the GPE was
908 * just to notify us that a query is needed and we need to
909 * wait for a second GPE to signal the completion of the
910 * event we are actually waiting for.
912 Status = EcCheckStatus(sc, "sleep", Event);
913 if (ACPI_SUCCESS(Status)) {
914 if (gen_count == sc->ec_gencount)
920 gen_count = sc->ec_gencount;
924 * We finished waiting for the GPE and it never arrived. Try to
925 * read the register once and trust whatever value we got. This is
926 * the best we can do at this point.
928 if (ACPI_FAILURE(Status))
929 Status = EcCheckStatus(sc, "sleep_end", Event);
931 if (!need_poll && no_intr > 10) {
932 device_printf(sc->ec_dev,
933 "not getting interrupts, switched to polled mode\n");
936 if (ACPI_FAILURE(Status))
937 KTR_LOG(acpi_ec_timeout);
942 EcCommand(struct acpi_ec_softc *sc, EC_COMMAND cmd)
949 ACPI_SERIAL_ASSERT(ec);
951 /* Don't use burst mode if user disabled it. */
952 if (!ec_burst_mode && cmd == EC_COMMAND_BURST_ENABLE)
955 /* Decide what to wait for based on command type. */
957 case EC_COMMAND_READ:
958 case EC_COMMAND_WRITE:
959 case EC_COMMAND_BURST_DISABLE:
960 event = EC_EVENT_INPUT_BUFFER_EMPTY;
962 case EC_COMMAND_QUERY:
963 case EC_COMMAND_BURST_ENABLE:
964 event = EC_EVENT_OUTPUT_BUFFER_FULL;
967 device_printf(sc->ec_dev, "EcCommand: invalid command %#x\n", cmd);
968 return (AE_BAD_PARAMETER);
972 * Ensure empty input buffer before issuing command.
973 * Use generation count of zero to force a quick check.
975 status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY, 0);
976 if (ACPI_FAILURE(status))
979 /* Run the command and wait for the chosen event. */
980 KTR_LOG(acpi_ec_cmdrun, cmd);
981 gen_count = sc->ec_gencount;
983 status = EcWaitEvent(sc, event, gen_count);
984 if (ACPI_SUCCESS(status)) {
985 /* If we succeeded, burst flag should now be present. */
986 if (cmd == EC_COMMAND_BURST_ENABLE) {
987 ec_status = EC_GET_CSR(sc);
988 if ((ec_status & EC_FLAG_BURST_MODE) == 0)
992 device_printf(sc->ec_dev, "EcCommand: no response to %#x\n", cmd);
997 EcRead(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data)
1003 ACPI_SERIAL_ASSERT(ec);
1004 KTR_LOG(acpi_ec_readaddr, Address);
1006 for (retry = 0; retry < 2; retry++) {
1007 status = EcCommand(sc, EC_COMMAND_READ);
1008 if (ACPI_FAILURE(status))
1011 gen_count = sc->ec_gencount;
1012 EC_SET_DATA(sc, Address);
1013 status = EcWaitEvent(sc, EC_EVENT_OUTPUT_BUFFER_FULL, gen_count);
1014 if (ACPI_FAILURE(status)) {
1015 if (ACPI_SUCCESS(EcCheckStatus(sc, "retr_check",
1016 EC_EVENT_INPUT_BUFFER_EMPTY)))
1021 *Data = EC_GET_DATA(sc);
1024 device_printf(sc->ec_dev, "EcRead: failed waiting to get data\n");
1029 EcWrite(struct acpi_ec_softc *sc, UINT8 Address, UINT8 Data)
1034 ACPI_SERIAL_ASSERT(ec);
1035 KTR_LOG(acpi_ec_writeaddr, Address, Data);
1037 status = EcCommand(sc, EC_COMMAND_WRITE);
1038 if (ACPI_FAILURE(status))
1041 gen_count = sc->ec_gencount;
1042 EC_SET_DATA(sc, Address);
1043 status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY, gen_count);
1044 if (ACPI_FAILURE(status)) {
1045 device_printf(sc->ec_dev, "EcWrite: failed waiting for sent address\n");
1049 gen_count = sc->ec_gencount;
1050 EC_SET_DATA(sc, Data);
1051 status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY, gen_count);
1052 if (ACPI_FAILURE(status)) {
1053 device_printf(sc->ec_dev, "EcWrite: failed waiting for sent data\n");