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
170 #define EC_TIMEOUT_BACKOFF 100
172 #define EVENT_READY(event, status) \
173 (((event) == EC_EVENT_OUTPUT_BUFFER_FULL && \
174 ((status) & EC_FLAG_OUTPUT_BUFFER) != 0) || \
175 ((event) == EC_EVENT_INPUT_BUFFER_EMPTY && \
176 ((status) & EC_FLAG_INPUT_BUFFER) == 0))
178 ACPI_SERIAL_DECL(ec, "ACPI embedded controller");
180 extern int acpi_silence_all;
182 static SYSCTL_NODE(_debug_acpi, OID_AUTO, ec, CTLFLAG_RD, NULL, "EC debugging");
184 static int ec_burst_mode;
185 TUNABLE_INT("debug.acpi.ec.burst", &ec_burst_mode);
186 SYSCTL_INT(_debug_acpi_ec, OID_AUTO, burst, CTLFLAG_RW, &ec_burst_mode, 0,
187 "Enable use of burst mode (faster for nearly all systems)");
188 static int ec_polled_mode;
189 TUNABLE_INT("debug.acpi.ec.polled", &ec_polled_mode);
190 SYSCTL_INT(_debug_acpi_ec, OID_AUTO, polled, CTLFLAG_RW, &ec_polled_mode, 0,
191 "Force use of polled mode (only if interrupt mode doesn't work)");
192 static int ec_timeout = EC_TIMEOUT;
193 TUNABLE_INT("debug.acpi.ec.timeout", &ec_timeout);
194 SYSCTL_INT(_debug_acpi_ec, OID_AUTO, timeout, CTLFLAG_RW, &ec_timeout,
195 EC_TIMEOUT, "Total time spent waiting for a response (poll+sleep)");
196 static int ec_timeout_backoff = EC_TIMEOUT_BACKOFF;
197 TUNABLE_INT("debug.acpi.ec.timeout_backoff", &ec_timeout_backoff);
198 SYSCTL_INT(_debug_acpi_ec, OID_AUTO, timeout_backoff, CTLFLAG_RW,
199 &ec_timeout_backoff, EC_TIMEOUT_BACKOFF,
200 "Total time spent retrying for a response (poll+sleep) in case of failure");
201 static int ec_auto_silence = 10; /* silence after 10 errors */
202 TUNABLE_INT("debug.acpi.ec.auto_silence", &ec_auto_silence);
203 SYSCTL_INT(_debug_acpi_ec, OID_AUTO, auto_silence, CTLFLAG_RW,
205 "Auto-silence ACPI messages after x retries in case of failure");
208 #define KTR_ACPI_EC KTR_ALL
211 KTR_INFO_MASTER(acpi_ec);
212 KTR_INFO(KTR_ACPI_EC, acpi_ec, burstdis, 0,
213 "ec burst disabled in waitevent (%s)", const char *msg);
214 KTR_INFO(KTR_ACPI_EC, acpi_ec, burstdisok, 1,
215 "ec disabled burst ok");
216 KTR_INFO(KTR_ACPI_EC, acpi_ec, burstenl, 2,
218 KTR_INFO(KTR_ACPI_EC, acpi_ec, cmdrun, 3,
219 "ec running command %#hhx", EC_COMMAND cmd);
220 KTR_INFO(KTR_ACPI_EC, acpi_ec, gpehdlstart, 4,
221 "ec gpe handler start");
222 KTR_INFO(KTR_ACPI_EC, acpi_ec, gpequeuehdl, 5,
223 "ec gpe queueing query handler");
224 KTR_INFO(KTR_ACPI_EC, acpi_ec, gperun, 6,
225 "ec running gpe handler directly");
226 KTR_INFO(KTR_ACPI_EC, acpi_ec, qryoknotrun, 7,
227 "ec query ok, not running _Q%02hhX", uint8_t Data);
228 KTR_INFO(KTR_ACPI_EC, acpi_ec, qryokrun, 8,
229 "ec query ok, running _Q%02hhX", uint8_t Data);
230 KTR_INFO(KTR_ACPI_EC, acpi_ec, readaddr, 9,
231 "ec read from %#hhx", UINT8 Address);
232 KTR_INFO(KTR_ACPI_EC, acpi_ec, timeout, 10,
233 "error: ec wait timed out");
234 KTR_INFO(KTR_ACPI_EC, acpi_ec, waitrdy, 11,
235 "ec %s wait ready, status %#hhx", const char *msg, EC_STATUS ec_status);
236 KTR_INFO(KTR_ACPI_EC, acpi_ec, writeaddr, 12,
237 "ec write to %#hhx, data %#hhx", UINT8 Address, UINT8 Data);
240 EcLock(struct acpi_ec_softc *sc)
244 /* If _GLK is non-zero, acquire the global lock. */
247 status = AcpiAcquireGlobalLock(EC_LOCK_TIMEOUT, &sc->ec_glkhandle);
248 if (ACPI_FAILURE(status))
251 ACPI_SERIAL_BEGIN(ec);
256 EcUnlock(struct acpi_ec_softc *sc)
260 AcpiReleaseGlobalLock(sc->ec_glkhandle);
263 static UINT32 EcGpeHandler(ACPI_HANDLE GpeDevice,
264 UINT32 GpeNumber, void *Context);
265 static ACPI_STATUS EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function,
266 void *Context, void **return_Context);
267 static ACPI_STATUS EcSpaceHandler(UINT32 Function,
268 ACPI_PHYSICAL_ADDRESS Address,
269 UINT32 Width, UINT64 *Value,
270 void *Context, void *RegionContext);
271 static ACPI_STATUS EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event,
273 static ACPI_STATUS EcCommand(struct acpi_ec_softc *sc, EC_COMMAND cmd);
274 static ACPI_STATUS EcRead(struct acpi_ec_softc *sc, UINT8 Address,
276 static ACPI_STATUS EcWrite(struct acpi_ec_softc *sc, UINT8 Address,
278 static int acpi_ec_probe(device_t dev);
279 static int acpi_ec_attach(device_t dev);
280 static int acpi_ec_suspend(device_t dev);
281 static int acpi_ec_resume(device_t dev);
282 static int acpi_ec_shutdown(device_t dev);
283 static int acpi_ec_read_method(device_t dev, u_int addr,
284 UINT64 *val, int width);
285 static int acpi_ec_write_method(device_t dev, u_int addr,
286 UINT64 val, int width);
288 static device_method_t acpi_ec_methods[] = {
289 /* Device interface */
290 DEVMETHOD(device_probe, acpi_ec_probe),
291 DEVMETHOD(device_attach, acpi_ec_attach),
292 DEVMETHOD(device_suspend, acpi_ec_suspend),
293 DEVMETHOD(device_resume, acpi_ec_resume),
294 DEVMETHOD(device_shutdown, acpi_ec_shutdown),
296 /* Embedded controller interface */
297 DEVMETHOD(acpi_ec_read, acpi_ec_read_method),
298 DEVMETHOD(acpi_ec_write, acpi_ec_write_method),
303 static driver_t acpi_ec_driver = {
306 sizeof(struct acpi_ec_softc),
307 .gpri = KOBJ_GPRI_ACPI
310 static devclass_t acpi_ec_devclass;
311 DRIVER_MODULE(acpi_ec, acpi, acpi_ec_driver, acpi_ec_devclass, NULL, NULL);
312 MODULE_DEPEND(acpi_ec, acpi, 1, 1, 1);
315 * Look for an ECDT and if we find one, set up default GPE and
316 * space handlers to catch attempts to access EC space before
317 * we have a real driver instance in place.
319 * TODO: Some old Gateway laptops need us to fake up an ECDT or
320 * otherwise attach early so that _REG methods can run.
323 acpi_ec_ecdt_probe(device_t parent)
325 ACPI_TABLE_ECDT *ecdt;
329 struct acpi_ec_params *params;
331 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
333 /* Find and validate the ECDT. */
334 status = AcpiGetTable(ACPI_SIG_ECDT, 1, (ACPI_TABLE_HEADER **)&ecdt);
335 if (ACPI_FAILURE(status) ||
336 ecdt->Control.BitWidth != 8 ||
337 ecdt->Data.BitWidth != 8) {
341 /* Create the child device with the given unit number. */
342 child = BUS_ADD_CHILD(parent, parent, 0, "acpi_ec", ecdt->Uid);
344 kprintf("%s: can't add child\n", __func__);
348 /* Find and save the ACPI handle for this device. */
349 status = AcpiGetHandle(NULL, ecdt->Id, &h);
350 if (ACPI_FAILURE(status)) {
351 device_delete_child(parent, child);
352 kprintf("%s: can't get handle\n", __func__);
355 acpi_set_handle(child, h);
357 /* Set the data and CSR register addresses. */
358 bus_set_resource(child, SYS_RES_IOPORT, 0, ecdt->Data.Address,
360 bus_set_resource(child, SYS_RES_IOPORT, 1, ecdt->Control.Address,
364 * Store values for the probe/attach routines to use. Store the
365 * ECDT GPE bit and set the global lock flag according to _GLK.
366 * Note that it is not perfectly correct to be evaluating a method
367 * before initializing devices, but in practice this function
368 * should be safe to call at this point.
370 params = kmalloc(sizeof(struct acpi_ec_params), M_TEMP, M_WAITOK | M_ZERO);
371 params->gpe_handle = NULL;
372 params->gpe_bit = ecdt->Gpe;
373 params->uid = ecdt->Uid;
374 acpi_GetInteger(h, "_GLK", ¶ms->glk);
375 acpi_set_private(child, params);
377 /* Finish the attach process. */
378 if (device_probe_and_attach(child) != 0)
379 device_delete_child(parent, child);
383 acpi_ec_probe(device_t dev)
393 struct acpi_ec_params *params;
394 static char *ec_ids[] = { "PNP0C09", NULL };
396 /* Check that this is a device and that EC is not disabled. */
397 if (acpi_get_type(dev) != ACPI_TYPE_DEVICE || acpi_disabled("ec"))
401 * If probed via ECDT, set description and continue. Otherwise,
402 * we can access the namespace and make sure this is not a
408 buf.Length = ACPI_ALLOCATE_BUFFER;
409 params = acpi_get_private(dev);
410 if (params != NULL) {
413 } else if (ACPI_ID_PROBE(device_get_parent(dev), dev, ec_ids)) {
414 params = kmalloc(sizeof(struct acpi_ec_params), M_TEMP,
416 h = acpi_get_handle(dev);
419 * Read the unit ID to check for duplicate attach and the
420 * global lock value to see if we should acquire it when
423 status = acpi_GetInteger(h, "_UID", ¶ms->uid);
424 if (ACPI_FAILURE(status))
426 status = acpi_GetInteger(h, "_GLK", ¶ms->glk);
427 if (ACPI_FAILURE(status))
431 * Evaluate the _GPE method to find the GPE bit used by the EC to
432 * signal status (SCI). If it's a package, it contains a reference
433 * and GPE bit, similar to _PRW.
435 status = AcpiEvaluateObject(h, "_GPE", NULL, &buf);
436 if (ACPI_FAILURE(status)) {
437 device_printf(dev, "can't evaluate _GPE - %s\n",
438 AcpiFormatException(status));
441 obj = (ACPI_OBJECT *)buf.Pointer;
446 case ACPI_TYPE_INTEGER:
447 params->gpe_handle = NULL;
448 params->gpe_bit = obj->Integer.Value;
450 case ACPI_TYPE_PACKAGE:
451 if (!ACPI_PKG_VALID(obj, 2))
454 acpi_GetReference(NULL, &obj->Package.Elements[0]);
455 if (params->gpe_handle == NULL ||
456 acpi_PkgInt32(obj, 1, ¶ms->gpe_bit) != 0)
460 device_printf(dev, "_GPE has invalid type %d\n", obj->Type);
464 /* Store the values we got from the namespace for attach. */
465 acpi_set_private(dev, params);
468 * Check for a duplicate probe. This can happen when a probe
469 * via ECDT succeeded already. If this is a duplicate, disable
472 peer = devclass_get_device(acpi_ec_devclass, params->uid);
473 if (peer == NULL || !device_is_alive(peer))
481 ksnprintf(desc, sizeof(desc), "Embedded Controller: GPE %#x%s%s",
482 params->gpe_bit, (params->glk) ? ", GLK" : "",
483 ecdt ? ", ECDT" : "");
484 device_set_desc_copy(dev, desc);
487 if (ret > 0 && params)
488 kfree(params, M_TEMP);
490 AcpiOsFree(buf.Pointer);
495 acpi_ec_attach(device_t dev)
497 struct acpi_ec_softc *sc;
498 struct acpi_ec_params *params;
501 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
503 /* Fetch/initialize softc (assumes softc is pre-zeroed). */
504 sc = device_get_softc(dev);
505 params = acpi_get_private(dev);
507 sc->ec_handle = acpi_get_handle(dev);
508 ACPI_SERIAL_INIT(ec);
510 /* Retrieve previously probed values via device ivars. */
511 sc->ec_glk = params->glk;
512 sc->ec_gpebit = params->gpe_bit;
513 sc->ec_gpehandle = params->gpe_handle;
514 sc->ec_uid = params->uid;
515 sc->ec_suspending = FALSE;
516 acpi_set_private(dev, NULL);
517 kfree(params, M_TEMP);
519 /* Attach bus resources for data and command/status ports. */
521 sc->ec_data_res = bus_alloc_resource_any(sc->ec_dev, SYS_RES_IOPORT,
522 &sc->ec_data_rid, RF_ACTIVE);
523 if (sc->ec_data_res == NULL) {
524 device_printf(dev, "can't allocate data port\n");
527 sc->ec_data_tag = rman_get_bustag(sc->ec_data_res);
528 sc->ec_data_handle = rman_get_bushandle(sc->ec_data_res);
531 sc->ec_csr_res = bus_alloc_resource_any(sc->ec_dev, SYS_RES_IOPORT,
532 &sc->ec_csr_rid, RF_ACTIVE);
533 if (sc->ec_csr_res == NULL) {
534 device_printf(dev, "can't allocate command/status port\n");
537 sc->ec_csr_tag = rman_get_bustag(sc->ec_csr_res);
538 sc->ec_csr_handle = rman_get_bushandle(sc->ec_csr_res);
541 * Install a handler for this EC's GPE bit. We want edge-triggered
544 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "attaching GPE handler\n"));
545 Status = AcpiInstallGpeHandler(sc->ec_gpehandle, sc->ec_gpebit,
546 ACPI_GPE_EDGE_TRIGGERED, EcGpeHandler, sc);
547 if (ACPI_FAILURE(Status)) {
548 device_printf(dev, "can't install GPE handler for %s - %s\n",
549 acpi_name(sc->ec_handle), AcpiFormatException(Status));
554 * Install address space handler
556 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "attaching address space handler\n"));
557 Status = AcpiInstallAddressSpaceHandler(sc->ec_handle, ACPI_ADR_SPACE_EC,
558 &EcSpaceHandler, &EcSpaceSetup, sc);
559 if (ACPI_FAILURE(Status)) {
560 device_printf(dev, "can't install address space handler for %s - %s\n",
561 acpi_name(sc->ec_handle), AcpiFormatException(Status));
565 /* Enable runtime GPEs for the handler. */
566 Status = AcpiEnableGpe(sc->ec_gpehandle, sc->ec_gpebit);
567 if (ACPI_FAILURE(Status)) {
568 device_printf(dev, "AcpiEnableGpe failed: %s\n",
569 AcpiFormatException(Status));
573 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "acpi_ec_attach complete\n"));
577 AcpiRemoveGpeHandler(sc->ec_gpehandle, sc->ec_gpebit, EcGpeHandler);
578 AcpiRemoveAddressSpaceHandler(sc->ec_handle, ACPI_ADR_SPACE_EC,
581 bus_release_resource(sc->ec_dev, SYS_RES_IOPORT, sc->ec_csr_rid,
584 bus_release_resource(sc->ec_dev, SYS_RES_IOPORT, sc->ec_data_rid,
590 acpi_ec_suspend(device_t dev)
592 struct acpi_ec_softc *sc;
594 sc = device_get_softc(dev);
595 sc->ec_suspending = TRUE;
600 acpi_ec_resume(device_t dev)
602 struct acpi_ec_softc *sc;
604 sc = device_get_softc(dev);
605 sc->ec_suspending = FALSE;
610 acpi_ec_shutdown(device_t dev)
612 struct acpi_ec_softc *sc;
614 /* Disable the GPE so we don't get EC events during shutdown. */
615 sc = device_get_softc(dev);
616 AcpiDisableGpe(sc->ec_gpehandle, sc->ec_gpebit);
620 /* Methods to allow other devices (e.g., smbat) to read/write EC space. */
622 acpi_ec_read_method(device_t dev, u_int addr, UINT64 *val, int width)
624 struct acpi_ec_softc *sc;
627 sc = device_get_softc(dev);
628 status = EcSpaceHandler(ACPI_READ, addr, width * 8, val, sc, NULL);
629 if (ACPI_FAILURE(status))
635 acpi_ec_write_method(device_t dev, u_int addr, UINT64 val, int width)
637 struct acpi_ec_softc *sc;
640 sc = device_get_softc(dev);
641 status = EcSpaceHandler(ACPI_WRITE, addr, width * 8, &val, sc, NULL);
642 if (ACPI_FAILURE(status))
648 EcCheckStatus(struct acpi_ec_softc *sc, const char *msg, EC_EVENT event)
653 status = AE_NO_HARDWARE_RESPONSE;
654 ec_status = EC_GET_CSR(sc);
655 if (sc->ec_burstactive && !(ec_status & EC_FLAG_BURST_MODE)) {
656 KTR_LOG(acpi_ec_burstdis, msg);
657 sc->ec_burstactive = FALSE;
659 if (EVENT_READY(event, ec_status)) {
660 KTR_LOG(acpi_ec_waitrdy, msg, ec_status);
667 EcGpeQueryHandler(void *Context)
669 struct acpi_ec_softc *sc = (struct acpi_ec_softc *)Context;
672 int retry, sci_enqueued;
675 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
676 KASSERT(Context != NULL, ("EcGpeQueryHandler called with NULL"));
678 /* Serialize user access with EcSpaceHandler(). */
680 if (ACPI_FAILURE(Status)) {
681 device_printf(sc->ec_dev, "GpeQuery lock error: %s\n",
682 AcpiFormatException(Status));
687 * Send a query command to the EC to find out which _Qxx call it
688 * wants to make. This command clears the SCI bit and also the
689 * interrupt source since we are edge-triggered. To prevent the GPE
690 * that may arise from running the query from causing another query
691 * to be queued, we clear the pending flag only after running it.
693 sci_enqueued = sc->ec_sci_pend;
694 for (retry = 0; retry < 2; retry++) {
695 Status = EcCommand(sc, EC_COMMAND_QUERY);
696 if (ACPI_SUCCESS(Status))
698 if (ACPI_SUCCESS(EcCheckStatus(sc, "retr_check",
699 EC_EVENT_INPUT_BUFFER_EMPTY)))
704 sc->ec_sci_pend = FALSE;
705 if (ACPI_FAILURE(Status)) {
707 device_printf(sc->ec_dev, "GPE query failed: %s\n",
708 AcpiFormatException(Status));
711 Data = EC_GET_DATA(sc);
714 * We have to unlock before running the _Qxx method below since that
715 * method may attempt to read/write from EC address space, causing
716 * recursive acquisition of the lock.
720 /* Ignore the value for "no outstanding event". (13.3.5) */
722 KTR_LOG(acpi_ec_qryoknotrun, Data);
725 KTR_LOG(acpi_ec_qryokrun, Data);
728 /* Evaluate _Qxx to respond to the controller. */
729 ksnprintf(qxx, sizeof(qxx), "_Q%02X", Data);
731 Status = AcpiEvaluateObject(sc->ec_handle, qxx, NULL, NULL);
732 if (ACPI_FAILURE(Status) && Status != AE_NOT_FOUND) {
733 device_printf(sc->ec_dev, "evaluation of query method %s failed: %s\n",
734 qxx, AcpiFormatException(Status));
737 /* Reenable runtime GPE if its execution was deferred. */
739 Status = AcpiFinishGpe(sc->ec_gpehandle, sc->ec_gpebit);
740 if (ACPI_FAILURE(Status))
741 device_printf(sc->ec_dev, "reenabling runtime GPE failed: %s\n",
742 AcpiFormatException(Status));
747 * The GPE handler is called when IBE/OBF or SCI events occur. We are
748 * called from an unknown lock context.
751 EcGpeHandler(ACPI_HANDLE GpeDevice, UINT32 GpeNumber, void *Context)
753 struct acpi_ec_softc *sc = Context;
757 KASSERT(Context != NULL, ("EcGpeHandler called with NULL"));
758 KTR_LOG(acpi_ec_gpehdlstart);
760 * Notify EcWaitEvent() that the status register is now fresh. If we
761 * didn't do this, it wouldn't be possible to distinguish an old IBE
762 * from a new one, for example when doing a write transaction (writing
763 * address and then data values.)
765 atomic_add_int(&sc->ec_gencount, 1);
769 * If the EC_SCI bit of the status register is set, queue a query handler.
770 * It will run the query and _Qxx method later, under the lock.
772 EcStatus = EC_GET_CSR(sc);
773 if ((EcStatus & EC_EVENT_SCI) && !sc->ec_sci_pend) {
774 KTR_LOG(acpi_ec_gpequeuehdl);
775 Status = AcpiOsExecute(OSL_GPE_HANDLER, EcGpeQueryHandler, Context);
776 if (ACPI_SUCCESS(Status)) {
777 sc->ec_sci_pend = TRUE;
780 kprintf("EcGpeHandler: queuing GPE query handler failed\n");
783 return (ACPI_REENABLE_GPE);
787 EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function, void *Context,
788 void **RegionContext)
791 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
794 * If deactivating a region, always set the output to NULL. Otherwise,
795 * just pass the context through.
797 if (Function == ACPI_REGION_DEACTIVATE)
798 *RegionContext = NULL;
800 *RegionContext = Context;
802 return_ACPI_STATUS (AE_OK);
806 EcSpaceHandler(UINT32 Function, ACPI_PHYSICAL_ADDRESS Address, UINT32 Width,
807 UINT64 *Value, void *Context, void *RegionContext)
809 struct acpi_ec_softc *sc = (struct acpi_ec_softc *)Context;
810 ACPI_PHYSICAL_ADDRESS EcAddr;
814 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, (UINT32)Address);
816 if (Function != ACPI_READ && Function != ACPI_WRITE)
817 return_ACPI_STATUS (AE_BAD_PARAMETER);
818 if (Width % 8 != 0 || Value == NULL || Context == NULL)
819 return_ACPI_STATUS (AE_BAD_PARAMETER);
820 if (Address + Width / 8 > 256)
821 return_ACPI_STATUS (AE_BAD_ADDRESS);
824 * If booting, check if we need to run the query handler. If so, we
825 * we call it directly here since our thread taskq is not active yet.
827 if (cold || rebooting || sc->ec_suspending) {
828 if ((EC_GET_CSR(sc) & EC_EVENT_SCI)) {
829 KTR_LOG(acpi_ec_gperun);
830 EcGpeQueryHandler(sc);
834 /* Serialize with EcGpeQueryHandler() at transaction granularity. */
836 if (ACPI_FAILURE(Status))
837 return_ACPI_STATUS (Status);
839 /* If we can't start burst mode, continue anyway. */
840 Status = EcCommand(sc, EC_COMMAND_BURST_ENABLE);
841 if (ACPI_SUCCESS(Status)) {
842 if (EC_GET_DATA(sc) == EC_BURST_ACK) {
843 KTR_LOG(acpi_ec_burstenl);
844 sc->ec_burstactive = TRUE;
848 /* Perform the transaction(s), based on Width. */
850 EcData = (UINT8 *)Value;
851 if (Function == ACPI_READ)
856 Status = EcRead(sc, EcAddr, EcData);
859 Status = EcWrite(sc, EcAddr, *EcData);
862 if (ACPI_FAILURE(Status))
866 } while (EcAddr < Address + Width / 8);
868 if (sc->ec_burstactive) {
869 sc->ec_burstactive = FALSE;
870 if (ACPI_SUCCESS(EcCommand(sc, EC_COMMAND_BURST_DISABLE)))
871 KTR_LOG(acpi_ec_burstdisok);
875 return_ACPI_STATUS (Status);
879 EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event, u_int gen_count)
881 static int no_intr = 0;
883 int count, i, need_poll, slp_ival;
885 ACPI_SERIAL_ASSERT(ec);
886 Status = AE_NO_HARDWARE_RESPONSE;
887 need_poll = cold || rebooting || ec_polled_mode || sc->ec_suspending;
889 /* Wait for event by polling or GPE (interrupt). */
891 count = (ec_timeout * 1000) / EC_POLL_DELAY;
895 for (i = 0; i < count; i++) {
896 Status = EcCheckStatus(sc, "poll", Event);
897 if (ACPI_SUCCESS(Status))
899 DELAY(EC_POLL_DELAY);
902 slp_ival = hz / 1000;
906 /* hz has less than 1 ms resolution so scale timeout. */
908 count = ec_timeout / (1000 / hz);
912 * Wait for the GPE to signal the status changed, checking the
913 * status register each time we get one. It's possible to get a
914 * GPE for an event we're not interested in here (i.e., SCI for
917 for (i = 0; i < count; i++) {
918 if (gen_count == sc->ec_gencount)
919 tsleep(sc, 0, "ecgpe", slp_ival);
921 * Record new generation count. It's possible the GPE was
922 * just to notify us that a query is needed and we need to
923 * wait for a second GPE to signal the completion of the
924 * event we are actually waiting for.
926 Status = EcCheckStatus(sc, "sleep", Event);
927 if (ACPI_SUCCESS(Status)) {
928 if (gen_count == sc->ec_gencount)
934 gen_count = sc->ec_gencount;
938 * We finished waiting for the GPE and it never arrived. Try to
939 * read the register once and trust whatever value we got. This is
940 * the best we can do at this point.
942 if (ACPI_FAILURE(Status))
943 Status = EcCheckStatus(sc, "sleep_end", Event);
945 if (!need_poll && no_intr > 10) {
946 device_printf(sc->ec_dev,
947 "not getting interrupts, switched to polled mode\n");
950 if (ACPI_FAILURE(Status)) {
951 ec_timeout = ec_timeout_backoff;
952 if (ec_auto_silence > 0) {
953 if (--ec_auto_silence <= 0)
954 acpi_silence_all = 1;
956 KTR_LOG(acpi_ec_timeout);
962 EcCommand(struct acpi_ec_softc *sc, EC_COMMAND cmd)
969 ACPI_SERIAL_ASSERT(ec);
971 /* Don't use burst mode if user disabled it. */
972 if (!ec_burst_mode && cmd == EC_COMMAND_BURST_ENABLE)
975 /* Decide what to wait for based on command type. */
977 case EC_COMMAND_READ:
978 case EC_COMMAND_WRITE:
979 case EC_COMMAND_BURST_DISABLE:
980 event = EC_EVENT_INPUT_BUFFER_EMPTY;
982 case EC_COMMAND_QUERY:
983 case EC_COMMAND_BURST_ENABLE:
984 event = EC_EVENT_OUTPUT_BUFFER_FULL;
987 device_printf(sc->ec_dev, "EcCommand: invalid command %#x\n", cmd);
988 return (AE_BAD_PARAMETER);
992 * Ensure empty input buffer before issuing command.
993 * Use generation count of zero to force a quick check.
995 status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY, 0);
996 if (ACPI_FAILURE(status))
999 /* Run the command and wait for the chosen event. */
1000 KTR_LOG(acpi_ec_cmdrun, cmd);
1001 gen_count = sc->ec_gencount;
1002 EC_SET_CSR(sc, cmd);
1003 status = EcWaitEvent(sc, event, gen_count);
1004 if (ACPI_SUCCESS(status)) {
1005 /* If we succeeded, burst flag should now be present. */
1006 if (cmd == EC_COMMAND_BURST_ENABLE) {
1007 ec_status = EC_GET_CSR(sc);
1008 if ((ec_status & EC_FLAG_BURST_MODE) == 0)
1012 device_printf(sc->ec_dev, "EcCommand: no response to %#x\n", cmd);
1017 EcRead(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data)
1023 ACPI_SERIAL_ASSERT(ec);
1024 KTR_LOG(acpi_ec_readaddr, Address);
1026 for (retry = 0; retry < 2; retry++) {
1027 status = EcCommand(sc, EC_COMMAND_READ);
1028 if (ACPI_FAILURE(status))
1031 gen_count = sc->ec_gencount;
1032 EC_SET_DATA(sc, Address);
1033 status = EcWaitEvent(sc, EC_EVENT_OUTPUT_BUFFER_FULL, gen_count);
1034 if (ACPI_FAILURE(status)) {
1035 if (ACPI_SUCCESS(EcCheckStatus(sc, "retr_check",
1036 EC_EVENT_INPUT_BUFFER_EMPTY)))
1041 *Data = EC_GET_DATA(sc);
1044 device_printf(sc->ec_dev, "EcRead: failed waiting to get data\n");
1049 EcWrite(struct acpi_ec_softc *sc, UINT8 Address, UINT8 Data)
1054 ACPI_SERIAL_ASSERT(ec);
1055 KTR_LOG(acpi_ec_writeaddr, Address, Data);
1057 status = EcCommand(sc, EC_COMMAND_WRITE);
1058 if (ACPI_FAILURE(status))
1061 gen_count = sc->ec_gencount;
1062 EC_SET_DATA(sc, Address);
1063 status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY, gen_count);
1064 if (ACPI_FAILURE(status)) {
1065 device_printf(sc->ec_dev, "EcWrite: failed waiting for sent address\n");
1069 gen_count = sc->ec_gencount;
1070 EC_SET_DATA(sc, Data);
1071 status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY, gen_count);
1072 if (ACPI_FAILURE(status)) {
1073 device_printf(sc->ec_dev, "EcWrite: failed waiting for sent data\n");