More minor IPI work.
[dragonfly/vkernel-mp.git] / sys / dev / acpica5 / acpi_ec.c
blobd46338265e70252ddeffbb24a7c45f82b8cf4175
1 /*-
2 * Copyright (c) 2003 Nate Lawson
3 * Copyright (c) 2000 Michael Smith
4 * Copyright (c) 2000 BSDi
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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
26 * SUCH DAMAGE.
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.12 2007/01/17 18:31:19 y0netan1 Exp $
31 /******************************************************************************
33 * 1. Copyright Notice
35 * Some or all of this work - Copyright (c) 1999, Intel Corp. All rights
36 * reserved.
38 * 2. License
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
43 * property rights.
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
62 * conditions are met:
64 * 3. Conditions
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
88 * make.
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
94 * distribution.
96 * 3.4. Intel retains all right, title, and interest in and to the Original
97 * Intel Code.
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
121 * LIMITED REMEDY.
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.12 2007/01/17 18:31:19 y0netan1 Exp $
145 #include "opt_acpi.h"
146 #include <sys/param.h>
147 #include <sys/kernel.h>
148 #include <sys/bus.h>
149 #include <sys/thread.h>
150 #include <sys/malloc.h>
151 #include <sys/module.h>
152 #include <sys/lock.h>
153 #include <sys/rman.h>
155 #include "acpi.h"
156 #include <dev/acpica5/acpivar.h>
159 * Hooks for the ACPI CA debugging infrastructure
161 #define _COMPONENT ACPI_EC
162 ACPI_MODULE_NAME("EC")
165 * EC_COMMAND:
166 * -----------
168 typedef UINT8 EC_COMMAND;
170 #define EC_COMMAND_UNKNOWN ((EC_COMMAND) 0x00)
171 #define EC_COMMAND_READ ((EC_COMMAND) 0x80)
172 #define EC_COMMAND_WRITE ((EC_COMMAND) 0x81)
173 #define EC_COMMAND_BURST_ENABLE ((EC_COMMAND) 0x82)
174 #define EC_COMMAND_BURST_DISABLE ((EC_COMMAND) 0x83)
175 #define EC_COMMAND_QUERY ((EC_COMMAND) 0x84)
178 * EC_STATUS:
179 * ----------
180 * The encoding of the EC status register is illustrated below.
181 * Note that a set bit (1) indicates the property is TRUE
182 * (e.g. if bit 0 is set then the output buffer is full).
183 * +-+-+-+-+-+-+-+-+
184 * |7|6|5|4|3|2|1|0|
185 * +-+-+-+-+-+-+-+-+
186 * | | | | | | | |
187 * | | | | | | | +- Output Buffer Full?
188 * | | | | | | +--- Input Buffer Full?
189 * | | | | | +----- <reserved>
190 * | | | | +------- Data Register is Command Byte?
191 * | | | +--------- Burst Mode Enabled?
192 * | | +----------- SCI Event?
193 * | +------------- SMI Event?
194 * +--------------- <Reserved>
197 typedef UINT8 EC_STATUS;
199 #define EC_FLAG_OUTPUT_BUFFER ((EC_STATUS) 0x01)
200 #define EC_FLAG_INPUT_BUFFER ((EC_STATUS) 0x02)
201 #define EC_FLAG_BURST_MODE ((EC_STATUS) 0x10)
202 #define EC_FLAG_SCI ((EC_STATUS) 0x20)
205 * EC_EVENT:
206 * ---------
208 typedef UINT8 EC_EVENT;
210 #define EC_EVENT_UNKNOWN ((EC_EVENT) 0x00)
211 #define EC_EVENT_OUTPUT_BUFFER_FULL ((EC_EVENT) 0x01)
212 #define EC_EVENT_INPUT_BUFFER_EMPTY ((EC_EVENT) 0x02)
213 #define EC_EVENT_SCI ((EC_EVENT) 0x20)
216 * Register access primitives
218 #define EC_GET_DATA(sc) \
219 bus_space_read_1((sc)->ec_data_tag, (sc)->ec_data_handle, 0)
221 #define EC_SET_DATA(sc, v) \
222 bus_space_write_1((sc)->ec_data_tag, (sc)->ec_data_handle, 0, (v))
224 #define EC_GET_CSR(sc) \
225 bus_space_read_1((sc)->ec_csr_tag, (sc)->ec_csr_handle, 0)
227 #define EC_SET_CSR(sc, v) \
228 bus_space_write_1((sc)->ec_csr_tag, (sc)->ec_csr_handle, 0, (v))
230 /* Additional params to pass from the probe routine */
231 struct acpi_ec_params {
232 int glk;
233 int gpe_bit;
234 ACPI_HANDLE gpe_handle;
235 int uid;
238 /* Indicate that this device has already been probed via ECDT. */
239 #define DEV_ECDT(x) (acpi_get_magic(x) == (int)&acpi_ec_devclass)
242 * Driver softc.
244 struct acpi_ec_softc {
245 device_t ec_dev;
246 ACPI_HANDLE ec_handle;
247 int ec_uid;
248 ACPI_HANDLE ec_gpehandle;
249 UINT8 ec_gpebit;
250 UINT8 ec_csrvalue;
252 int ec_data_rid;
253 struct resource *ec_data_res;
254 bus_space_tag_t ec_data_tag;
255 bus_space_handle_t ec_data_handle;
257 int ec_csr_rid;
258 struct resource *ec_csr_res;
259 bus_space_tag_t ec_csr_tag;
260 bus_space_handle_t ec_csr_handle;
262 int ec_glk;
263 int ec_glkhandle;
264 struct lock ec_lock;
268 * XXX
269 * I couldn't find it in the spec but other implementations also use a
270 * value of 1 ms for the time to acquire global lock.
272 #define EC_LOCK_TIMEOUT 1000
274 /* Default interval in microseconds for the status polling loop. */
275 #define EC_POLL_DELAY 10
277 /* Total time in ms spent in the poll loop waiting for a response. */
278 #define EC_POLL_TIMEOUT 100
280 #define EVENT_READY(event, status) \
281 (((event) == EC_EVENT_OUTPUT_BUFFER_FULL && \
282 ((status) & EC_FLAG_OUTPUT_BUFFER) != 0) || \
283 ((event) == EC_EVENT_INPUT_BUFFER_EMPTY && \
284 ((status) & EC_FLAG_INPUT_BUFFER) == 0))
286 static int ec_poll_timeout = EC_POLL_TIMEOUT;
287 TUNABLE_INT("hw.acpi.ec.poll_timeout", &ec_poll_timeout);
289 static __inline ACPI_STATUS
290 EcLock(struct acpi_ec_softc *sc)
292 ACPI_STATUS status = AE_OK;
294 /* Always acquire this EC's mutex. */
295 lockmgr(&sc->ec_lock, LK_EXCLUSIVE|LK_RETRY);
297 /* If _GLK is non-zero, also acquire the global lock. */
298 if (sc->ec_glk) {
299 status = AcpiAcquireGlobalLock(EC_LOCK_TIMEOUT, &sc->ec_glkhandle);
300 if (ACPI_FAILURE(status))
301 lockmgr(&sc->ec_lock, LK_RELEASE);
304 return (status);
307 static __inline void
308 EcUnlock(struct acpi_ec_softc *sc)
310 if (sc->ec_glk)
311 AcpiReleaseGlobalLock(sc->ec_glkhandle);
312 lockmgr(&sc->ec_lock, LK_RELEASE);
315 static uint32_t EcGpeHandler(void *Context);
316 static ACPI_STATUS EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function,
317 void *Context, void **return_Context);
318 static ACPI_STATUS EcSpaceHandler(UINT32 Function,
319 ACPI_PHYSICAL_ADDRESS Address,
320 UINT32 width, ACPI_INTEGER *Value,
321 void *Context, void *RegionContext);
322 static ACPI_STATUS EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event);
323 static ACPI_STATUS EcCommand(struct acpi_ec_softc *sc, EC_COMMAND cmd);
324 static ACPI_STATUS EcRead(struct acpi_ec_softc *sc, UINT8 Address,
325 UINT8 *Data);
326 static ACPI_STATUS EcWrite(struct acpi_ec_softc *sc, UINT8 Address,
327 UINT8 *Data);
328 static int acpi_ec_probe(device_t dev);
329 static int acpi_ec_attach(device_t dev);
331 static device_method_t acpi_ec_methods[] = {
332 /* Device interface */
333 DEVMETHOD(device_probe, acpi_ec_probe),
334 DEVMETHOD(device_attach, acpi_ec_attach),
336 {0, 0}
339 static driver_t acpi_ec_driver = {
340 "acpi_ec",
341 acpi_ec_methods,
342 sizeof(struct acpi_ec_softc),
345 static devclass_t acpi_ec_devclass;
346 DRIVER_MODULE(acpi_ec, acpi, acpi_ec_driver, acpi_ec_devclass, 0, 0);
347 MODULE_DEPEND(acpi_ec, acpi, 1, 1, 1);
350 * Look for an ECDT and if we find one, set up default GPE and
351 * space handlers to catch attempts to access EC space before
352 * we have a real driver instance in place.
353 * TODO: if people report invalid ECDTs, add a tunable to disable them.
355 void
356 acpi_ec_ecdt_probe(device_t parent)
358 ACPI_TABLE_ECDT *ecdt;
359 ACPI_STATUS status;
360 device_t child;
361 ACPI_HANDLE h;
362 struct acpi_ec_params *params;
364 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
366 /* Find and validate the ECDT. */
367 status = AcpiGetTable(ACPI_SIG_ECDT, 1, (ACPI_TABLE_HEADER **)&ecdt);
368 if (ACPI_FAILURE(status) ||
369 ecdt->Control.BitWidth != 8 || ecdt->Data.BitWidth != 8) {
370 return;
373 /* Create the child device with the given unit number. */
374 child = BUS_ADD_CHILD(parent, parent, 0, "acpi_ec", ecdt->Uid);
375 if (child == NULL) {
376 kprintf("%s: can't add child\n", __func__);
377 return;
380 /* Find and save the ACPI handle for this device. */
381 status = AcpiGetHandle(NULL, ecdt->Id, &h);
382 if (ACPI_FAILURE(status)) {
383 device_delete_child(parent, child);
384 kprintf("%s: can't get handle\n", __func__);
385 return;
387 acpi_set_handle(child, h);
389 /* Set the data and CSR register addresses. */
390 bus_set_resource(child, SYS_RES_IOPORT, 0, ecdt->Data.Address,
391 /*count*/1);
392 bus_set_resource(child, SYS_RES_IOPORT, 1, ecdt->Control.Address,
393 /*count*/1);
396 * Store values for the probe/attach routines to use. Store the
397 * ECDT GPE bit and set the global lock flag according to _GLK.
398 * Note that it is not perfectly correct to be evaluating a method
399 * before initializing devices, but in practice this function
400 * should be safe to call at this point.
402 params = kmalloc(sizeof(struct acpi_ec_params), M_TEMP, M_WAITOK | M_ZERO);
403 params->gpe_handle = NULL;
404 params->gpe_bit = ecdt->Gpe;
405 params->uid = ecdt->Uid;
406 acpi_GetInteger(h, "_GLK", &params->glk);
407 acpi_set_private(child, params);
408 acpi_set_magic(child, (int)&acpi_ec_devclass);
410 /* Finish the attach process. */
411 if (device_probe_and_attach(child) != 0)
412 device_delete_child(parent, child);
415 static int
416 acpi_ec_probe(device_t dev)
418 ACPI_BUFFER buf;
419 ACPI_HANDLE h;
420 ACPI_OBJECT *obj;
421 ACPI_STATUS status;
422 device_t peer;
423 char desc[64];
424 int ret;
425 struct acpi_ec_params *params;
427 /* Check that this is a device and that EC is not disabled. */
428 if (acpi_get_type(dev) != ACPI_TYPE_DEVICE || acpi_disabled("ec"))
429 return (ENXIO);
432 * If probed via ECDT, set description and continue. Otherwise,
433 * we can access the namespace and make sure this is not a
434 * duplicate probe.
436 ret = ENXIO;
437 params = NULL;
438 buf.Pointer = NULL;
439 buf.Length = ACPI_ALLOCATE_BUFFER;
440 if (DEV_ECDT(dev)) {
441 params = acpi_get_private(dev);
442 ret = 0;
443 } else if (acpi_MatchHid(dev, "PNP0C09")) {
444 params = kmalloc(sizeof(struct acpi_ec_params), M_TEMP,
445 M_WAITOK | M_ZERO);
446 h = acpi_get_handle(dev);
449 * Read the unit ID to check for duplicate attach and the
450 * global lock value to see if we should acquire it when
451 * accessing the EC.
453 status = acpi_GetInteger(h, "_UID", &params->uid);
454 if (ACPI_FAILURE(status))
455 params->uid = 0;
456 status = acpi_GetInteger(h, "_GLK", &params->glk);
457 if (ACPI_FAILURE(status))
458 params->glk = 0;
461 * Evaluate the _GPE method to find the GPE bit used by the EC to
462 * signal status (SCI). If it's a package, it contains a reference
463 * and GPE bit, similar to _PRW.
465 status = AcpiEvaluateObject(h, "_GPE", NULL, &buf);
466 if (ACPI_FAILURE(status)) {
467 device_printf(dev, "can't evaluate _GPE - %s\n",
468 AcpiFormatException(status));
469 return (ENXIO);
471 obj = (ACPI_OBJECT *)buf.Pointer;
472 if (obj == NULL)
473 return (ENXIO);
475 switch (obj->Type) {
476 case ACPI_TYPE_INTEGER:
477 params->gpe_handle = NULL;
478 params->gpe_bit = obj->Integer.Value;
479 break;
480 case ACPI_TYPE_PACKAGE:
481 if (!ACPI_PKG_VALID(obj, 2))
482 goto out;
483 params->gpe_handle =
484 acpi_GetReference(NULL, &obj->Package.Elements[0]);
485 if (params->gpe_handle == NULL ||
486 acpi_PkgInt32(obj, 1, &params->gpe_bit) != 0)
487 goto out;
488 break;
489 default:
490 device_printf(dev, "_GPE has invalid type %d\n", obj->Type);
491 goto out;
494 /* Store the values we got from the namespace for attach. */
495 acpi_set_private(dev, params);
498 * Check for a duplicate probe. This can happen when a probe
499 * via ECDT succeeded already. If this is a duplicate, disable
500 * this device.
502 peer = devclass_get_device(acpi_ec_devclass, params->uid);
503 if (peer == NULL || !device_is_alive(peer))
504 ret = 0;
505 else
506 device_disable(dev);
509 out:
510 if (ret == 0) {
511 ksnprintf(desc, sizeof(desc), "Embedded Controller: GPE %#x%s%s",
512 params->gpe_bit, (params->glk) ? ", GLK" : "",
513 DEV_ECDT(dev) ? ", ECDT" : "");
514 device_set_desc_copy(dev, desc);
517 if (ret > 0 && params)
518 kfree(params, M_TEMP);
519 if (buf.Pointer)
520 AcpiOsFree(buf.Pointer);
521 return (ret);
524 static int
525 acpi_ec_attach(device_t dev)
527 struct acpi_ec_softc *sc;
528 struct acpi_ec_params *params;
529 ACPI_STATUS Status;
531 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
533 /* Fetch/initialize softc (assumes softc is pre-zeroed). */
534 sc = device_get_softc(dev);
535 params = acpi_get_private(dev);
536 sc->ec_dev = dev;
537 sc->ec_handle = acpi_get_handle(dev);
538 lockinit(&sc->ec_lock, "eclock", 0, 0);
540 /* Retrieve previously probed values via device ivars. */
541 sc->ec_glk = params->glk;
542 sc->ec_gpebit = params->gpe_bit;
543 sc->ec_gpehandle = params->gpe_handle;
544 sc->ec_uid = params->uid;
545 kfree(params, M_TEMP);
547 /* Attach bus resources for data and command/status ports. */
548 sc->ec_data_rid = 0;
549 sc->ec_data_res = bus_alloc_resource_any(sc->ec_dev, SYS_RES_IOPORT,
550 &sc->ec_data_rid, RF_ACTIVE);
551 if (sc->ec_data_res == NULL) {
552 device_printf(dev, "can't allocate data port\n");
553 goto error;
555 sc->ec_data_tag = rman_get_bustag(sc->ec_data_res);
556 sc->ec_data_handle = rman_get_bushandle(sc->ec_data_res);
558 sc->ec_csr_rid = 1;
559 sc->ec_csr_res = bus_alloc_resource_any(sc->ec_dev, SYS_RES_IOPORT,
560 &sc->ec_csr_rid, RF_ACTIVE);
561 if (sc->ec_csr_res == NULL) {
562 device_printf(dev, "can't allocate command/status port\n");
563 goto error;
565 sc->ec_csr_tag = rman_get_bustag(sc->ec_csr_res);
566 sc->ec_csr_handle = rman_get_bushandle(sc->ec_csr_res);
569 * Install a handler for this EC's GPE bit. We want edge-triggered
570 * behavior.
572 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "attaching GPE handler\n"));
573 Status = AcpiInstallGpeHandler(sc->ec_gpehandle, sc->ec_gpebit,
574 ACPI_GPE_EDGE_TRIGGERED, &EcGpeHandler, sc);
575 if (ACPI_FAILURE(Status)) {
576 device_printf(dev, "can't install GPE handler for %s - %s\n",
577 acpi_name(sc->ec_handle), AcpiFormatException(Status));
578 goto error;
582 * Install address space handler
584 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "attaching address space handler\n"));
585 Status = AcpiInstallAddressSpaceHandler(sc->ec_handle, ACPI_ADR_SPACE_EC,
586 &EcSpaceHandler, &EcSpaceSetup, sc);
587 if (ACPI_FAILURE(Status)) {
588 device_printf(dev, "can't install address space handler for %s - %s\n",
589 acpi_name(sc->ec_handle), AcpiFormatException(Status));
590 goto error;
593 /* Enable runtime GPEs for the handler. */
594 Status = AcpiSetGpeType(sc->ec_gpehandle, sc->ec_gpebit,
595 ACPI_GPE_TYPE_RUNTIME);
596 if (ACPI_FAILURE(Status)) {
597 device_printf(dev, "AcpiSetGpeType failed: %s\n",
598 AcpiFormatException(Status));
599 goto error;
601 Status = AcpiEnableGpe(sc->ec_gpehandle, sc->ec_gpebit, ACPI_NOT_ISR);
602 if (ACPI_FAILURE(Status)) {
603 device_printf(dev, "AcpiEnableGpe failed: %s\n",
604 AcpiFormatException(Status));
605 goto error;
608 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "acpi_ec_attach complete\n"));
609 return (0);
611 error:
612 AcpiRemoveGpeHandler(sc->ec_gpehandle, sc->ec_gpebit, &EcGpeHandler);
613 AcpiRemoveAddressSpaceHandler(sc->ec_handle, ACPI_ADR_SPACE_EC,
614 EcSpaceHandler);
615 if (sc->ec_csr_res)
616 bus_release_resource(sc->ec_dev, SYS_RES_IOPORT, sc->ec_csr_rid,
617 sc->ec_csr_res);
618 if (sc->ec_data_res)
619 bus_release_resource(sc->ec_dev, SYS_RES_IOPORT, sc->ec_data_rid,
620 sc->ec_data_res);
621 /* mtx_destroy(&sc->ec_mtx); */
622 return (ENXIO);
625 static void
626 EcGpeQueryHandler(void *Context)
628 struct acpi_ec_softc *sc = (struct acpi_ec_softc *)Context;
629 UINT8 Data;
630 ACPI_STATUS Status;
631 EC_STATUS EcStatus;
632 char qxx[5];
634 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
635 KASSERT(Context != NULL, ("EcGpeQueryHandler called with NULL"));
637 Status = EcLock(sc);
638 if (ACPI_FAILURE(Status)) {
639 ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
640 "GpeQuery lock error: %s\n", AcpiFormatException(Status));
641 return;
645 * If the EC_SCI bit of the status register is not set, then pass
646 * it along to any potential waiters as it may be an IBE/OBF event.
648 EcStatus = EC_GET_CSR(sc);
649 if ((EcStatus & EC_EVENT_SCI) == 0) {
650 sc->ec_csrvalue = EcStatus;
651 wakeup(&sc->ec_csrvalue);
652 EcUnlock(sc);
653 goto re_enable;
657 * Send a query command to the EC to find out which _Qxx call it
658 * wants to make. This command clears the SCI bit and also the
659 * interrupt source since we are edge-triggered.
661 Status = EcCommand(sc, EC_COMMAND_QUERY);
662 if (ACPI_FAILURE(Status)) {
663 EcUnlock(sc);
664 ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
665 "GPE query failed - %s\n", AcpiFormatException(Status));
666 goto re_enable;
668 Data = EC_GET_DATA(sc);
669 EcUnlock(sc);
671 /* Ignore the value for "no outstanding event". (13.3.5) */
672 if (Data == 0)
673 goto re_enable;
675 /* Evaluate _Qxx to respond to the controller. */
676 ksprintf(qxx, "_Q%02x", Data);
677 strupr(qxx);
678 Status = AcpiEvaluateObject(sc->ec_handle, qxx, NULL, NULL);
679 if (ACPI_FAILURE(Status) && Status != AE_NOT_FOUND) {
680 ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
681 "evaluation of GPE query method %s failed - %s\n",
682 qxx, AcpiFormatException(Status));
685 re_enable:
686 /* Re-enable the GPE event so we'll get future requests. */
687 Status = AcpiEnableGpe(sc->ec_gpehandle, sc->ec_gpebit, ACPI_NOT_ISR);
688 if (ACPI_FAILURE(Status))
689 kprintf("EcGpeQueryHandler: AcpiEnableEvent failed\n");
693 * Handle a GPE. Currently we only handle SCI events as others must
694 * be handled by polling in EcWaitEvent(). This is because some ECs
695 * treat events as level when they should be edge-triggered.
697 static uint32_t
698 EcGpeHandler(void *Context)
700 struct acpi_ec_softc *sc = Context;
701 ACPI_STATUS Status;
703 KASSERT(Context != NULL, ("EcGpeHandler called with NULL"));
705 /* Disable further GPEs while we handle this one. */
706 AcpiDisableGpe(sc->ec_gpehandle, sc->ec_gpebit, ACPI_ISR);
708 /* Schedule the GPE query handler. */
709 Status = AcpiOsExecute(OSL_GPE_HANDLER, EcGpeQueryHandler,
710 Context);
711 if (ACPI_FAILURE(Status)) {
712 kprintf("Queuing GPE query handler failed.\n");
713 Status = AcpiEnableGpe(sc->ec_gpehandle, sc->ec_gpebit, ACPI_ISR);
714 if (ACPI_FAILURE(Status))
715 kprintf("EcGpeHandler: AcpiEnableEvent failed\n");
718 return (0);
721 static ACPI_STATUS
722 EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function, void *Context,
723 void **RegionContext)
726 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
729 * If deactivating a region, always set the output to NULL. Otherwise,
730 * just pass the context through.
732 if (Function == ACPI_REGION_DEACTIVATE)
733 *RegionContext = NULL;
734 else
735 *RegionContext = Context;
737 return_ACPI_STATUS (AE_OK);
740 static ACPI_STATUS
741 EcSpaceHandler(UINT32 Function, ACPI_PHYSICAL_ADDRESS Address, UINT32 width,
742 ACPI_INTEGER *Value, void *Context, void *RegionContext)
744 struct acpi_ec_softc *sc = (struct acpi_ec_softc *)Context;
745 ACPI_STATUS Status;
746 UINT8 EcAddr, EcData;
747 int i;
749 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, (UINT32)Address);
751 if (width % 8 != 0 || Value == NULL || Context == NULL)
752 return_ACPI_STATUS (AE_BAD_PARAMETER);
753 if (Address + (width / 8) - 1 > 0xFF)
754 return_ACPI_STATUS (AE_BAD_ADDRESS);
756 if (Function == ACPI_READ)
757 *Value = 0;
758 EcAddr = Address;
759 Status = AE_ERROR;
761 /* Perform the transaction(s), based on width. */
762 for (i = 0; i < width; i += 8, EcAddr++) {
763 Status = EcLock(sc);
764 if (ACPI_FAILURE(Status))
765 break;
767 switch (Function) {
768 case ACPI_READ:
769 Status = EcRead(sc, EcAddr, &EcData);
770 if (ACPI_SUCCESS(Status))
771 *Value |= ((ACPI_INTEGER)EcData) << i;
772 break;
773 case ACPI_WRITE:
774 EcData = (UINT8)((*Value) >> i);
775 Status = EcWrite(sc, EcAddr, &EcData);
776 break;
777 default:
778 device_printf(sc->ec_dev, "invalid EcSpaceHandler function %d\n",
779 Function);
780 Status = AE_BAD_PARAMETER;
781 break;
783 EcUnlock(sc);
784 if (ACPI_FAILURE(Status))
785 break;
788 return_ACPI_STATUS (Status);
791 static ACPI_STATUS
792 EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event)
794 EC_STATUS EcStatus;
795 ACPI_STATUS Status;
796 int count, i, period, retval, slp_ival;
797 static int EcDbgMaxDelay;
799 /* mtx_assert(&sc->ec_mtx, MA_OWNED); */
800 Status = AE_NO_HARDWARE_RESPONSE;
803 * Wait for 1 us before checking the CSR. Testing shows about
804 * 50% of requests complete in 1 us and 90% of them complete
805 * in 5 us or less.
807 AcpiOsStall(1);
810 * Poll the EC status register for up to 1 ms in chunks of 10 us
811 * to detect completion of the last command.
813 for (i = 0; i < 1000 / EC_POLL_DELAY; i++) {
814 EcStatus = EC_GET_CSR(sc);
815 if (EVENT_READY(Event, EcStatus)) {
816 Status = AE_OK;
817 break;
819 AcpiOsStall(EC_POLL_DELAY);
821 period = i * EC_POLL_DELAY;
824 * If we still don't have a response and we're up and running, wait up
825 * to ec_poll_timeout ms for completion, sleeping for chunks of 10 ms.
827 slp_ival = 0;
828 if (Status != AE_OK) {
829 retval = ENXIO;
830 count = ec_poll_timeout / 10;
831 if (count == 0)
832 count = 1;
833 slp_ival = hz / 100;
834 if (slp_ival == 0)
835 slp_ival = 1;
836 for (i = 0; i < count; i++) {
837 if (retval != 0)
838 EcStatus = EC_GET_CSR(sc);
839 else
840 EcStatus = sc->ec_csrvalue;
841 if (EVENT_READY(Event, EcStatus)) {
842 Status = AE_OK;
843 break;
845 if (!cold)
846 retval = tsleep(&sc->ec_csrvalue, 0, "ecpoll", slp_ival);
847 else
848 AcpiOsStall(10000);
852 /* Calculate new delay and print it if it exceeds the max. */
853 if (slp_ival > 0)
854 period += i * 10000;
855 if (period > EcDbgMaxDelay) {
856 EcDbgMaxDelay = period;
857 ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
858 "info: new max delay is %d us\n", period);
861 return (Status);
864 static ACPI_STATUS
865 EcCommand(struct acpi_ec_softc *sc, EC_COMMAND cmd)
867 ACPI_STATUS Status;
868 EC_EVENT Event;
870 /*mtx_assert(&sc->ec_mtx, MA_OWNED);*/
872 /* Decide what to wait for based on command type. */
873 switch (cmd) {
874 case EC_COMMAND_READ:
875 case EC_COMMAND_WRITE:
876 case EC_COMMAND_BURST_DISABLE:
877 Event = EC_EVENT_INPUT_BUFFER_EMPTY;
878 break;
879 case EC_COMMAND_QUERY:
880 case EC_COMMAND_BURST_ENABLE:
881 Event = EC_EVENT_OUTPUT_BUFFER_FULL;
882 break;
883 default:
884 ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
885 "EcCommand: Invalid command %#x\n", cmd);
886 return (AE_BAD_PARAMETER);
889 /* Run the command and wait for the chosen event. */
890 EC_SET_CSR(sc, cmd);
891 Status = EcWaitEvent(sc, Event);
892 if (ACPI_FAILURE(Status)) {
893 ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
894 "EcCommand: no response to %#x\n", cmd);
897 return (Status);
900 static ACPI_STATUS
901 EcRead(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data)
903 ACPI_STATUS Status;
905 /*mtx_assert(&sc->ec_mtx, MA_OWNED);*/
907 #ifdef notyet
908 /* If we can't start burst mode, continue anyway. */
909 EcCommand(sc, EC_COMMAND_BURST_ENABLE);
910 #endif
912 Status = EcCommand(sc, EC_COMMAND_READ);
913 if (ACPI_FAILURE(Status))
914 return (Status);
916 EC_SET_DATA(sc, Address);
917 Status = EcWaitEvent(sc, EC_EVENT_OUTPUT_BUFFER_FULL);
918 if (ACPI_FAILURE(Status)) {
919 ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
920 "EcRead: Failed waiting for EC to send data.\n");
921 return (Status);
924 *Data = EC_GET_DATA(sc);
926 #ifdef notyet
927 if (sc->ec_burstactive) {
928 Status = EcCommand(sc, EC_COMMAND_BURST_DISABLE);
929 if (ACPI_FAILURE(Status))
930 return (Status);
932 #endif
934 return (AE_OK);
937 static ACPI_STATUS
938 EcWrite(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data)
940 ACPI_STATUS Status;
942 /*mtx_assert(&sc->ec_mtx, MA_OWNED);*/
944 #ifdef notyet
945 /* If we can't start burst mode, continue anyway. */
946 EcCommand(sc, EC_COMMAND_BURST_ENABLE);
947 #endif
949 Status = EcCommand(sc, EC_COMMAND_WRITE);
950 if (ACPI_FAILURE(Status))
951 return (Status);
953 EC_SET_DATA(sc, Address);
954 Status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY);
955 if (ACPI_FAILURE(Status)) {
956 ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
957 "EcRead: Failed waiting for EC to process address\n");
958 return (Status);
961 EC_SET_DATA(sc, *Data);
962 Status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY);
963 if (ACPI_FAILURE(Status)) {
964 ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev),
965 "EcWrite: Failed waiting for EC to process data\n");
966 return (Status);
969 #ifdef notyet
970 if (sc->ec_burstactive) {
971 Status = EcCommand(sc, EC_COMMAND_BURST_DISABLE);
972 if (ACPI_FAILURE(Status))
973 return (Status);
975 #endif
977 return (AE_OK);