2 * Copyright (c) 2003 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: src/sys/dev/acpica/acpi_ec.c,v 1.51 2004/05/30 20:08:23 phk Exp $
29 * $DragonFly: src/sys/dev/acpica5/acpi_ec.c,v 1.4 2004/07/05 00:07:35 dillon Exp $
31 /******************************************************************************
35 * Some or all of this work - Copyright (c) 1999, Intel Corp. All rights
40 * 2.1. This is your license from Intel Corp. under its intellectual property
41 * rights. You may have additional license terms from the party that provided
42 * you this software, covering your right to use that party's intellectual
45 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
46 * copy of the source code appearing in this file ("Covered Code") an
47 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
48 * base code distributed originally by Intel ("Original Intel Code") to copy,
49 * make derivatives, distribute, use and display any portion of the Covered
50 * Code in any form, with the right to sublicense such rights; and
52 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
53 * license (with the right to sublicense), under only those claims of Intel
54 * patents that are infringed by the Original Intel Code, to make, use, sell,
55 * offer to sell, and import the Covered Code and derivative works thereof
56 * solely to the minimum extent necessary to exercise the above copyright
57 * license, and in no event shall the patent license extend to any additions
58 * to or modifications of the Original Intel Code. No other license or right
59 * is granted directly or by implication, estoppel or otherwise;
61 * The above copyright and patent license is granted only if the following
66 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
67 * Redistribution of source code of any substantial portion of the Covered
68 * Code or modification with rights to further distribute source must include
69 * the above Copyright Notice, the above License, this list of Conditions,
70 * and the following Disclaimer and Export Compliance provision. In addition,
71 * Licensee must cause all Covered Code to which Licensee contributes to
72 * contain a file documenting the changes Licensee made to create that Covered
73 * Code and the date of any change. Licensee must include in that file the
74 * documentation of any changes made by any predecessor Licensee. Licensee
75 * must include a prominent statement that the modification is derived,
76 * directly or indirectly, from Original Intel Code.
78 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
79 * Redistribution of source code of any substantial portion of the Covered
80 * Code or modification without rights to further distribute source must
81 * include the following Disclaimer and Export Compliance provision in the
82 * documentation and/or other materials provided with distribution. In
83 * addition, Licensee may not authorize further sublicense of source of any
84 * portion of the Covered Code, and must include terms to the effect that the
85 * license from Licensee to its licensee is limited to the intellectual
86 * property embodied in the software Licensee provides to its licensee, and
87 * not to intellectual property embodied in modifications its licensee may
90 * 3.3. Redistribution of Executable. Redistribution in executable form of any
91 * substantial portion of the Covered Code or modification must reproduce the
92 * above Copyright Notice, and the following Disclaimer and Export Compliance
93 * provision in the documentation and/or other materials provided with the
96 * 3.4. Intel retains all right, title, and interest in and to the Original
99 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
100 * Intel shall be used in advertising or otherwise to promote the sale, use or
101 * other dealings in products derived from or relating to the Covered Code
102 * without prior written authorization from Intel.
104 * 4. Disclaimer and Export Compliance
106 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
107 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
108 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
109 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
110 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
111 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
112 * PARTICULAR PURPOSE.
114 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
115 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
116 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
117 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
118 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
119 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
120 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
123 * 4.3. Licensee shall not export, either directly or indirectly, any of this
124 * software or system incorporating such software without first obtaining any
125 * required license or other approval from the U. S. Department of Commerce or
126 * any other agency or department of the United States Government. In the
127 * event Licensee exports any such software from the United States or
128 * re-exports any such software from a foreign destination, Licensee shall
129 * ensure that the distribution and export/re-export of the software is in
130 * compliance with all laws, regulations, orders, or other restrictions of the
131 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
132 * any of its subsidiaries will export/re-export any technical data, process,
133 * software, or service, directly or indirectly, to any country for which the
134 * United States government or any agency thereof requires an export license,
135 * other governmental approval, or letter of assurance, without first obtaining
136 * such license, approval or letter.
138 *****************************************************************************/
140 * $FreeBSD: src/sys/dev/acpica/acpi_ec.c,v 1.51 2004/05/30 20:08:23 phk Exp $
141 * $DragonFly: src/sys/dev/acpica5/acpi_ec.c,v 1.4 2004/07/05 00:07:35 dillon Exp $
145 #include "opt_acpi.h"
146 #include <sys/param.h>
147 #include <sys/kernel.h>
148 #include <sys/module.h>
150 #include <sys/thread.h>
151 #include <sys/malloc.h>
153 #include <machine/bus.h>
154 #include <machine/resource.h>
155 #include <sys/rman.h>
158 #include <dev/acpica5/acpivar.h>
161 * Hooks for the ACPI CA debugging infrastructure
163 #define _COMPONENT ACPI_EC
164 ACPI_MODULE_NAME("EC")
170 typedef UINT8 EC_COMMAND;
172 #define EC_COMMAND_UNKNOWN ((EC_COMMAND) 0x00)
173 #define EC_COMMAND_READ ((EC_COMMAND) 0x80)
174 #define EC_COMMAND_WRITE ((EC_COMMAND) 0x81)
175 #define EC_COMMAND_BURST_ENABLE ((EC_COMMAND) 0x82)
176 #define EC_COMMAND_BURST_DISABLE ((EC_COMMAND) 0x83)
177 #define EC_COMMAND_QUERY ((EC_COMMAND) 0x84)
182 * The encoding of the EC status register is illustrated below.
183 * Note that a set bit (1) indicates the property is TRUE
184 * (e.g. if bit 0 is set then the output buffer is full).
189 * | | | | | | | +- Output Buffer Full?
190 * | | | | | | +--- Input Buffer Full?
191 * | | | | | +----- <reserved>
192 * | | | | +------- Data Register is Command Byte?
193 * | | | +--------- Burst Mode Enabled?
194 * | | +----------- SCI Event?
195 * | +------------- SMI Event?
196 * +--------------- <Reserved>
199 typedef UINT8 EC_STATUS;
201 #define EC_FLAG_OUTPUT_BUFFER ((EC_STATUS) 0x01)
202 #define EC_FLAG_INPUT_BUFFER ((EC_STATUS) 0x02)
203 #define EC_FLAG_BURST_MODE ((EC_STATUS) 0x10)
204 #define EC_FLAG_SCI ((EC_STATUS) 0x20)
210 typedef UINT8 EC_EVENT;
212 #define EC_EVENT_UNKNOWN ((EC_EVENT) 0x00)
213 #define EC_EVENT_OUTPUT_BUFFER_FULL ((EC_EVENT) 0x01)
214 #define EC_EVENT_INPUT_BUFFER_EMPTY ((EC_EVENT) 0x02)
215 #define EC_EVENT_SCI ((EC_EVENT) 0x20)
218 * Register access primitives
220 #define EC_GET_DATA(sc) \
221 bus_space_read_1((sc)->ec_data_tag, (sc)->ec_data_handle, 0)
223 #define EC_SET_DATA(sc, v) \
224 bus_space_write_1((sc)->ec_data_tag, (sc)->ec_data_handle, 0, (v))
226 #define EC_GET_CSR(sc) \
227 bus_space_read_1((sc)->ec_csr_tag, (sc)->ec_csr_handle, 0)
229 #define EC_SET_CSR(sc, v) \
230 bus_space_write_1((sc)->ec_csr_tag, (sc)->ec_csr_handle, 0, (v))
232 /* Embedded Controller Boot Resources Table (ECDT) */
234 ACPI_TABLE_HEADER header;
235 ACPI_GENERIC_ADDRESS control;
236 ACPI_GENERIC_ADDRESS data;
242 /* Additional params to pass from the probe routine */
243 struct acpi_ec_params {
246 ACPI_HANDLE gpe_handle;
250 /* Indicate that this device has already been probed via ECDT. */
251 #define DEV_ECDT(x) (acpi_get_magic(x) == (int)&acpi_ec_devclass)
256 struct acpi_ec_softc {
258 ACPI_HANDLE ec_handle;
260 ACPI_HANDLE ec_gpehandle;
265 struct resource *ec_data_res;
266 bus_space_tag_t ec_data_tag;
267 bus_space_handle_t ec_data_handle;
270 struct resource *ec_csr_res;
271 bus_space_tag_t ec_csr_tag;
272 bus_space_handle_t ec_csr_handle;
276 struct lwkt_rwlock ec_rwlock;
282 * I couldn't find it in the spec but other implementations also use a
283 * value of 1 ms for the time to acquire global lock.
285 #define EC_LOCK_TIMEOUT 1000
288 * Start with an interval of 1 us for status poll loop. This delay
289 * will be dynamically adjusted based on the actual time waited.
291 #define EC_POLL_DELAY 1
293 /* Total time in ms spent in the poll loop waiting for a response. */
294 #define EC_POLL_TIMEOUT 100
296 #define EVENT_READY(event, status) \
297 (((event) == EC_EVENT_OUTPUT_BUFFER_FULL && \
298 ((status) & EC_FLAG_OUTPUT_BUFFER) != 0) || \
299 ((event) == EC_EVENT_INPUT_BUFFER_EMPTY && \
300 ((status) & EC_FLAG_INPUT_BUFFER) == 0))
302 static int ec_poll_timeout = EC_POLL_TIMEOUT;
303 TUNABLE_INT("hw.acpi.ec.poll_timeout", &ec_poll_timeout);
305 static __inline ACPI_STATUS
306 EcLock(struct acpi_ec_softc *sc)
308 ACPI_STATUS status = AE_OK;
310 /* Always acquire this EC's mutex. */
311 lwkt_exlock(&sc->ec_rwlock, "acpi2");
313 /* If _GLK is non-zero, also acquire the global lock. */
315 status = AcpiAcquireGlobalLock(EC_LOCK_TIMEOUT, &sc->ec_glkhandle);
316 if (ACPI_FAILURE(status))
317 lwkt_exunlock(&sc->ec_rwlock);
324 EcUnlock(struct acpi_ec_softc *sc)
327 AcpiReleaseGlobalLock(sc->ec_glkhandle);
328 lwkt_exunlock(&sc->ec_rwlock);
331 static uint32_t EcGpeHandler(void *Context);
332 static ACPI_STATUS EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function,
333 void *Context, void **return_Context);
334 static ACPI_STATUS EcSpaceHandler(UINT32 Function,
335 ACPI_PHYSICAL_ADDRESS Address,
336 UINT32 width, ACPI_INTEGER *Value,
337 void *Context, void *RegionContext);
338 static ACPI_STATUS EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event);
339 static ACPI_STATUS EcCommand(struct acpi_ec_softc *sc, EC_COMMAND cmd);
340 static ACPI_STATUS EcRead(struct acpi_ec_softc *sc, UINT8 Address,
342 static ACPI_STATUS EcWrite(struct acpi_ec_softc *sc, UINT8 Address,
344 static int acpi_ec_probe(device_t dev);
345 static int acpi_ec_attach(device_t dev);
347 static device_method_t acpi_ec_methods[] = {
348 /* Device interface */
349 DEVMETHOD(device_probe, acpi_ec_probe),
350 DEVMETHOD(device_attach, acpi_ec_attach),
355 static driver_t acpi_ec_driver = {
358 sizeof(struct acpi_ec_softc),
361 static devclass_t acpi_ec_devclass;
362 DRIVER_MODULE(acpi_ec, acpi, acpi_ec_driver, acpi_ec_devclass, 0, 0);
363 MODULE_DEPEND(acpi_ec, acpi, 1, 1, 1);
366 * Look for an ECDT and if we find one, set up default GPE and
367 * space handlers to catch attempts to access EC space before
368 * we have a real driver instance in place.
369 * TODO: if people report invalid ECDTs, add a tunable to disable them.
372 acpi_ec_ecdt_probe(device_t parent)
374 ACPI_TABLE_ECDT *ecdt;
378 struct acpi_ec_params *params;
380 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
382 /* Find and validate the ECDT. */
383 status = AcpiGetFirmwareTable("ECDT", 1, ACPI_LOGICAL_ADDRESSING,
384 (ACPI_TABLE_HEADER **)&ecdt);
385 if (ACPI_FAILURE(status) ||
386 ecdt->control.RegisterBitWidth != 8 ||
387 ecdt->data.RegisterBitWidth != 8) {
391 /* Create the child device with the given unit number. */
392 child = BUS_ADD_CHILD(parent, 0, "acpi_ec", ecdt->uid);
394 printf("%s: can't add child\n", __func__);
398 /* Find and save the ACPI handle for this device. */
399 status = AcpiGetHandle(NULL, ecdt->ec_id, &h);
400 if (ACPI_FAILURE(status)) {
401 device_delete_child(parent, child);
402 printf("%s: can't get handle\n", __func__);
405 acpi_set_handle(child, h);
407 /* Set the data and CSR register addresses. */
408 bus_set_resource(child, SYS_RES_IOPORT, 0, ecdt->data.Address,
410 bus_set_resource(child, SYS_RES_IOPORT, 1, ecdt->control.Address,
414 * Store values for the probe/attach routines to use. Store the
415 * ECDT GPE bit and set the global lock flag according to _GLK.
416 * Note that it is not perfectly correct to be evaluating a method
417 * before initializing devices, but in practice this function
418 * should be safe to call at this point.
420 params = malloc(sizeof(struct acpi_ec_params), M_TEMP, M_WAITOK | M_ZERO);
421 params->gpe_handle = NULL;
422 params->gpe_bit = ecdt->gpe_bit;
423 params->uid = ecdt->uid;
424 acpi_GetInteger(h, "_GLK", ¶ms->glk);
425 acpi_set_private(child, params);
426 acpi_set_magic(child, (int)&acpi_ec_devclass);
428 /* Finish the attach process. */
429 if (device_probe_and_attach(child) != 0)
430 device_delete_child(parent, child);
434 acpi_ec_probe(device_t dev)
443 struct acpi_ec_params *params;
445 /* Check that this is a device and that EC is not disabled. */
446 if (acpi_get_type(dev) != ACPI_TYPE_DEVICE || acpi_disabled("ec"))
450 * If probed via ECDT, set description and continue. Otherwise,
451 * we can access the namespace and make sure this is not a
457 buf.Length = ACPI_ALLOCATE_BUFFER;
459 params = acpi_get_private(dev);
461 } else if (acpi_MatchHid(dev, "PNP0C09")) {
462 params = malloc(sizeof(struct acpi_ec_params), M_TEMP,
464 h = acpi_get_handle(dev);
467 * Read the unit ID to check for duplicate attach and the
468 * global lock value to see if we should acquire it when
471 status = acpi_GetInteger(h, "_UID", ¶ms->uid);
472 if (ACPI_FAILURE(status))
474 status = acpi_GetInteger(h, "_GLK", ¶ms->glk);
475 if (ACPI_FAILURE(status))
479 * Evaluate the _GPE method to find the GPE bit used by the EC to
480 * signal status (SCI). If it's a package, it contains a reference
481 * and GPE bit, similar to _PRW.
483 status = AcpiEvaluateObject(h, "_GPE", NULL, &buf);
484 if (ACPI_FAILURE(status)) {
485 device_printf(dev, "can't evaluate _GPE - %s\n",
486 AcpiFormatException(status));
489 obj = (ACPI_OBJECT *)buf.Pointer;
494 case ACPI_TYPE_INTEGER:
495 params->gpe_handle = NULL;
496 params->gpe_bit = obj->Integer.Value;
498 case ACPI_TYPE_PACKAGE:
499 if (!ACPI_PKG_VALID(obj, 2))
502 acpi_GetReference(NULL, &obj->Package.Elements[0]);
503 if (params->gpe_handle == NULL ||
504 acpi_PkgInt32(obj, 1, ¶ms->gpe_bit) != 0)
508 device_printf(dev, "_GPE has invalid type %d\n", obj->Type);
512 /* Store the values we got from the namespace for attach. */
513 acpi_set_private(dev, params);
516 * Check for a duplicate probe. This can happen when a probe
517 * via ECDT succeeded already. If this is a duplicate, disable
520 peer = devclass_get_device(acpi_ec_devclass, params->uid);
521 if (peer == NULL || !device_is_alive(peer))
529 snprintf(desc, sizeof(desc), "Embedded Controller: GPE %#x%s%s",
530 params->gpe_bit, (params->glk) ? ", GLK" : "",
531 DEV_ECDT(dev) ? ", ECDT" : "");
532 device_set_desc_copy(dev, desc);
535 if (ret > 0 && params)
536 free(params, M_TEMP);
538 AcpiOsFree(buf.Pointer);
543 acpi_ec_attach(device_t dev)
545 struct acpi_ec_softc *sc;
546 struct acpi_ec_params *params;
549 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
551 /* Fetch/initialize softc (assumes softc is pre-zeroed). */
552 sc = device_get_softc(dev);
553 params = acpi_get_private(dev);
555 sc->ec_handle = acpi_get_handle(dev);
556 sc->ec_polldelay = EC_POLL_DELAY;
557 lwkt_rwlock_init(&sc->ec_rwlock);
559 /* Retrieve previously probed values via device ivars. */
560 sc->ec_glk = params->glk;
561 sc->ec_gpebit = params->gpe_bit;
562 sc->ec_gpehandle = params->gpe_handle;
563 sc->ec_uid = params->uid;
564 free(params, M_TEMP);
566 /* Attach bus resources for data and command/status ports. */
568 sc->ec_data_res = bus_alloc_resource_any(sc->ec_dev, SYS_RES_IOPORT,
569 &sc->ec_data_rid, RF_ACTIVE);
570 if (sc->ec_data_res == NULL) {
571 device_printf(dev, "can't allocate data port\n");
574 sc->ec_data_tag = rman_get_bustag(sc->ec_data_res);
575 sc->ec_data_handle = rman_get_bushandle(sc->ec_data_res);
578 sc->ec_csr_res = bus_alloc_resource_any(sc->ec_dev, SYS_RES_IOPORT,
579 &sc->ec_csr_rid, RF_ACTIVE);
580 if (sc->ec_csr_res == NULL) {
581 device_printf(dev, "can't allocate command/status port\n");
584 sc->ec_csr_tag = rman_get_bustag(sc->ec_csr_res);
585 sc->ec_csr_handle = rman_get_bushandle(sc->ec_csr_res);
588 * Install a handler for this EC's GPE bit. We want edge-triggered
591 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "attaching GPE handler\n"));
592 Status = AcpiInstallGpeHandler(sc->ec_gpehandle, sc->ec_gpebit,
593 ACPI_GPE_EDGE_TRIGGERED, &EcGpeHandler, sc);
594 if (ACPI_FAILURE(Status)) {
595 device_printf(dev, "can't install GPE handler for %s - %s\n",
596 acpi_name(sc->ec_handle), AcpiFormatException(Status));
601 * Install address space handler
603 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "attaching address space handler\n"));
604 Status = AcpiInstallAddressSpaceHandler(sc->ec_handle, ACPI_ADR_SPACE_EC,
605 &EcSpaceHandler, &EcSpaceSetup, sc);
606 if (ACPI_FAILURE(Status)) {
607 device_printf(dev, "can't install address space handler for %s - %s\n",
608 acpi_name(sc->ec_handle), AcpiFormatException(Status));
612 /* Enable runtime GPEs for the handler. */
613 Status = AcpiSetGpeType(sc->ec_gpehandle, sc->ec_gpebit,
614 ACPI_GPE_TYPE_RUNTIME);
615 if (ACPI_FAILURE(Status)) {
616 device_printf(dev, "AcpiSetGpeType failed: %s\n",
617 AcpiFormatException(Status));
620 Status = AcpiEnableGpe(sc->ec_gpehandle, sc->ec_gpebit, ACPI_NOT_ISR);
621 if (ACPI_FAILURE(Status)) {
622 device_printf(dev, "AcpiEnableGpe failed: %s\n",
623 AcpiFormatException(Status));
627 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "acpi_ec_attach complete\n"));
631 AcpiRemoveGpeHandler(sc->ec_gpehandle, sc->ec_gpebit, &EcGpeHandler);
632 AcpiRemoveAddressSpaceHandler(sc->ec_handle, ACPI_ADR_SPACE_EC,
635 bus_release_resource(sc->ec_dev, SYS_RES_IOPORT, sc->ec_csr_rid,
638 bus_release_resource(sc->ec_dev, SYS_RES_IOPORT, sc->ec_data_rid,
640 /* mtx_destroy(&sc->ec_mtx); */
645 EcGpeQueryHandler(void *Context)
647 struct acpi_ec_softc *sc = (struct acpi_ec_softc *)Context;
653 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
654 KASSERT(Context != NULL, ("EcGpeQueryHandler called with NULL"));
657 if (ACPI_FAILURE(Status)) {
658 ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
659 "GpeQuery lock error: %s\n", AcpiFormatException(Status));
664 * If the EC_SCI bit of the status register is not set, then pass
665 * it along to any potential waiters as it may be an IBE/OBF event.
667 EcStatus = EC_GET_CSR(sc);
668 if ((EcStatus & EC_EVENT_SCI) == 0) {
669 sc->ec_csrvalue = EcStatus;
670 wakeup(&sc->ec_csrvalue);
676 * Send a query command to the EC to find out which _Qxx call it
677 * wants to make. This command clears the SCI bit and also the
678 * interrupt source since we are edge-triggered.
680 Status = EcCommand(sc, EC_COMMAND_QUERY);
681 if (ACPI_FAILURE(Status)) {
683 ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
684 "GPE query failed - %s\n", AcpiFormatException(Status));
687 Data = EC_GET_DATA(sc);
690 /* Ignore the value for "no outstanding event". (13.3.5) */
694 /* Evaluate _Qxx to respond to the controller. */
695 sprintf(qxx, "_Q%02x", Data);
697 Status = AcpiEvaluateObject(sc->ec_handle, qxx, NULL, NULL);
698 if (ACPI_FAILURE(Status) && Status != AE_NOT_FOUND) {
699 ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
700 "evaluation of GPE query method %s failed - %s\n",
701 qxx, AcpiFormatException(Status));
705 /* Re-enable the GPE event so we'll get future requests. */
706 Status = AcpiEnableGpe(sc->ec_gpehandle, sc->ec_gpebit, ACPI_NOT_ISR);
707 if (ACPI_FAILURE(Status))
708 printf("EcGpeQueryHandler: AcpiEnableEvent failed\n");
712 * Handle a GPE. Currently we only handle SCI events as others must
713 * be handled by polling in EcWaitEvent(). This is because some ECs
714 * treat events as level when they should be edge-triggered.
717 EcGpeHandler(void *Context)
719 struct acpi_ec_softc *sc = Context;
722 KASSERT(Context != NULL, ("EcGpeHandler called with NULL"));
724 /* Disable further GPEs while we handle this one. */
725 AcpiDisableGpe(sc->ec_gpehandle, sc->ec_gpebit, ACPI_NOT_ISR);
727 /* Schedule the GPE query handler. */
728 Status = AcpiOsQueueForExecution(OSD_PRIORITY_GPE, EcGpeQueryHandler,
730 if (ACPI_FAILURE(Status)) {
731 printf("Queuing GPE query handler failed.\n");
732 Status = AcpiEnableGpe(sc->ec_gpehandle, sc->ec_gpebit, ACPI_NOT_ISR);
733 if (ACPI_FAILURE(Status))
734 printf("EcGpeHandler: AcpiEnableEvent failed\n");
741 EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function, void *Context,
742 void **RegionContext)
745 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
748 * If deactivating a region, always set the output to NULL. Otherwise,
749 * just pass the context through.
751 if (Function == ACPI_REGION_DEACTIVATE)
752 *RegionContext = NULL;
754 *RegionContext = Context;
756 return_ACPI_STATUS (AE_OK);
760 EcSpaceHandler(UINT32 Function, ACPI_PHYSICAL_ADDRESS Address, UINT32 width,
761 ACPI_INTEGER *Value, void *Context, void *RegionContext)
763 struct acpi_ec_softc *sc = (struct acpi_ec_softc *)Context;
765 UINT8 EcAddr, EcData;
768 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, (UINT32)Address);
770 if (width % 8 != 0 || Value == NULL || Context == NULL)
771 return_ACPI_STATUS (AE_BAD_PARAMETER);
772 if (Address + (width / 8) - 1 > 0xFF)
773 return_ACPI_STATUS (AE_BAD_ADDRESS);
775 if (Function == ACPI_READ)
780 /* Perform the transaction(s), based on width. */
781 for (i = 0; i < width; i += 8, EcAddr++) {
783 if (ACPI_FAILURE(Status))
788 Status = EcRead(sc, EcAddr, &EcData);
789 if (ACPI_SUCCESS(Status))
790 *Value |= ((ACPI_INTEGER)EcData) << i;
793 EcData = (UINT8)((*Value) >> i);
794 Status = EcWrite(sc, EcAddr, &EcData);
797 device_printf(sc->ec_dev, "invalid EcSpaceHandler function %d\n",
799 Status = AE_BAD_PARAMETER;
803 if (ACPI_FAILURE(Status))
807 return_ACPI_STATUS (Status);
811 EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event)
815 int i, period, retval;
816 static int EcDbgMaxDelay;
818 /* mtx_assert(&sc->ec_mtx, MA_OWNED); */
819 Status = AE_NO_HARDWARE_RESPONSE;
822 * Wait for 1 us before checking the CSR. Testing shows about
823 * 50% of requests complete in 1 us and 90% of them complete
829 * Poll the EC status register to detect completion of the last
830 * command. First, wait up to 1 ms in chunks of sc->ec_polldelay
833 for (i = 0; i < 1000 / sc->ec_polldelay; i++) {
834 EcStatus = EC_GET_CSR(sc);
835 if (EVENT_READY(Event, EcStatus)) {
839 AcpiOsStall(sc->ec_polldelay);
842 /* Scale poll delay by the amount of time actually waited. */
843 period = i * sc->ec_polldelay;
845 sc->ec_polldelay = 1;
846 else if (period <= 20)
847 sc->ec_polldelay = 5;
848 else if (period <= 100)
849 sc->ec_polldelay = 10;
851 sc->ec_polldelay = 100;
854 * If we still don't have a response, wait up to ec_poll_timeout ms
855 * for completion, sleeping for chunks of 10 ms.
857 if (Status != AE_OK) {
859 for (i = 0; i < ec_poll_timeout / 10; i++) {
861 EcStatus = EC_GET_CSR(sc);
863 EcStatus = sc->ec_csrvalue;
864 if (EVENT_READY(Event, EcStatus)) {
868 retval = tsleep(&sc->ec_csrvalue, 0, "ecpoll", 1 + hz / 100);
872 /* Calculate new delay and print it if it exceeds the max. */
875 if (period > EcDbgMaxDelay) {
876 EcDbgMaxDelay = period;
877 ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
878 "info: new max delay is %d us\n", period);
885 EcCommand(struct acpi_ec_softc *sc, EC_COMMAND cmd)
890 /*mtx_assert(&sc->ec_mtx, MA_OWNED);*/
892 /* Decide what to wait for based on command type. */
894 case EC_COMMAND_READ:
895 case EC_COMMAND_WRITE:
896 case EC_COMMAND_BURST_DISABLE:
897 Event = EC_EVENT_INPUT_BUFFER_EMPTY;
899 case EC_COMMAND_QUERY:
900 case EC_COMMAND_BURST_ENABLE:
901 Event = EC_EVENT_OUTPUT_BUFFER_FULL;
904 ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
905 "EcCommand: Invalid command %#x\n", cmd);
906 return (AE_BAD_PARAMETER);
909 /* Run the command and wait for the chosen event. */
911 Status = EcWaitEvent(sc, Event);
912 if (ACPI_FAILURE(Status)) {
913 ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
914 "EcCommand: no response to %#x\n", cmd);
921 EcRead(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data)
925 /*mtx_assert(&sc->ec_mtx, MA_OWNED);*/
928 /* If we can't start burst mode, continue anyway. */
929 EcCommand(sc, EC_COMMAND_BURST_ENABLE);
932 Status = EcCommand(sc, EC_COMMAND_READ);
933 if (ACPI_FAILURE(Status))
936 EC_SET_DATA(sc, Address);
937 Status = EcWaitEvent(sc, EC_EVENT_OUTPUT_BUFFER_FULL);
938 if (ACPI_FAILURE(Status)) {
939 ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
940 "EcRead: Failed waiting for EC to send data.\n");
944 *Data = EC_GET_DATA(sc);
947 if (sc->ec_burstactive) {
948 Status = EcCommand(sc, EC_COMMAND_BURST_DISABLE);
949 if (ACPI_FAILURE(Status))
958 EcWrite(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data)
962 /*mtx_assert(&sc->ec_mtx, MA_OWNED);*/
965 /* If we can't start burst mode, continue anyway. */
966 EcCommand(sc, EC_COMMAND_BURST_ENABLE);
969 Status = EcCommand(sc, EC_COMMAND_WRITE);
970 if (ACPI_FAILURE(Status))
973 EC_SET_DATA(sc, Address);
974 Status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY);
975 if (ACPI_FAILURE(Status)) {
976 ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
977 "EcRead: Failed waiting for EC to process address\n");
981 EC_SET_DATA(sc, *Data);
982 Status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY);
983 if (ACPI_FAILURE(Status)) {
984 ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
985 "EcWrite: Failed waiting for EC to process data\n");
990 if (sc->ec_burstactive) {
991 Status = EcCommand(sc, EC_COMMAND_BURST_DISABLE);
992 if (ACPI_FAILURE(Status))