1 /*- 2 * Copyright (c) 2003 Nate Lawson 3 * Copyright (c) 2000 Michael Smith 4 * Copyright (c) 2000 BSDi 5 * All rights reserved. 6 * 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. 15 * 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. 27 * 28 * $FreeBSD$ 29 */ 30 /****************************************************************************** 31 * 32 * 1. Copyright Notice 33 * 34 * Some or all of this work - Copyright (c) 1999, Intel Corp. All rights 35 * reserved. 36 * 37 * 2. License 38 * 39 * 2.1. This is your license from Intel Corp. under its intellectual property 40 * rights. You may have additional license terms from the party that provided 41 * you this software, covering your right to use that party's intellectual 42 * property rights. 43 * 44 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a 45 * copy of the source code appearing in this file ("Covered Code") an 46 * irrevocable, perpetual, worldwide license under Intel's copyrights in the 47 * base code distributed originally by Intel ("Original Intel Code") to copy, 48 * make derivatives, distribute, use and display any portion of the Covered 49 * Code in any form, with the right to sublicense such rights; and 50 * 51 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent 52 * license (with the right to sublicense), under only those claims of Intel 53 * patents that are infringed by the Original Intel Code, to make, use, sell, 54 * offer to sell, and import the Covered Code and derivative works thereof 55 * solely to the minimum extent necessary to exercise the above copyright 56 * license, and in no event shall the patent license extend to any additions 57 * to or modifications of the Original Intel Code. No other license or right 58 * is granted directly or by implication, estoppel or otherwise; 59 * 60 * The above copyright and patent license is granted only if the following 61 * conditions are met: 62 * 63 * 3. Conditions 64 * 65 * 3.1. Redistribution of Source with Rights to Further Distribute Source. 66 * Redistribution of source code of any substantial portion of the Covered 67 * Code or modification with rights to further distribute source must include 68 * the above Copyright Notice, the above License, this list of Conditions, 69 * and the following Disclaimer and Export Compliance provision. In addition, 70 * Licensee must cause all Covered Code to which Licensee contributes to 71 * contain a file documenting the changes Licensee made to create that Covered 72 * Code and the date of any change. Licensee must include in that file the 73 * documentation of any changes made by any predecessor Licensee. Licensee 74 * must include a prominent statement that the modification is derived, 75 * directly or indirectly, from Original Intel Code. 76 * 77 * 3.2. Redistribution of Source with no Rights to Further Distribute Source. 78 * Redistribution of source code of any substantial portion of the Covered 79 * Code or modification without rights to further distribute source must 80 * include the following Disclaimer and Export Compliance provision in the 81 * documentation and/or other materials provided with distribution. In 82 * addition, Licensee may not authorize further sublicense of source of any 83 * portion of the Covered Code, and must include terms to the effect that the 84 * license from Licensee to its licensee is limited to the intellectual 85 * property embodied in the software Licensee provides to its licensee, and 86 * not to intellectual property embodied in modifications its licensee may 87 * make. 88 * 89 * 3.3. Redistribution of Executable. Redistribution in executable form of any 90 * substantial portion of the Covered Code or modification must reproduce the 91 * above Copyright Notice, and the following Disclaimer and Export Compliance 92 * provision in the documentation and/or other materials provided with the 93 * distribution. 94 * 95 * 3.4. Intel retains all right, title, and interest in and to the Original 96 * Intel Code. 97 * 98 * 3.5. Neither the name Intel nor any other trademark owned or controlled by 99 * Intel shall be used in advertising or otherwise to promote the sale, use or 100 * other dealings in products derived from or relating to the Covered Code 101 * without prior written authorization from Intel. 102 * 103 * 4. Disclaimer and Export Compliance 104 * 105 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED 106 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE 107 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE, 108 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY 109 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY 110 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A 111 * PARTICULAR PURPOSE. 112 * 113 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES 114 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR 115 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT, 116 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY 117 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL 118 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS 119 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY 120 * LIMITED REMEDY. 121 * 122 * 4.3. Licensee shall not export, either directly or indirectly, any of this 123 * software or system incorporating such software without first obtaining any 124 * required license or other approval from the U. S. Department of Commerce or 125 * any other agency or department of the United States Government. In the 126 * event Licensee exports any such software from the United States or 127 * re-exports any such software from a foreign destination, Licensee shall 128 * ensure that the distribution and export/re-export of the software is in 129 * compliance with all laws, regulations, orders, or other restrictions of the 130 * U.S. Export Administration Regulations. Licensee agrees that neither it nor 131 * any of its subsidiaries will export/re-export any technical data, process, 132 * software, or service, directly or indirectly, to any country for which the 133 * United States government or any agency thereof requires an export license, 134 * other governmental approval, or letter of assurance, without first obtaining 135 * such license, approval or letter. 136 * 137 *****************************************************************************/ 138 139 #include <sys/cdefs.h> 140 __FBSDID("$FreeBSD$"); 141 142 #include "opt_acpi.h" 143 #include <sys/param.h> 144 #include <sys/kernel.h> 145 #include <sys/bus.h> 146 #include <sys/malloc.h> 147 #include <sys/module.h> 148 #include <sys/sx.h> 149 150 #include <machine/bus.h> 151 #include <machine/resource.h> 152 #include <sys/rman.h> 153 154 #include "acpi.h" 155 #include <dev/acpica/acpivar.h> 156 157 /* 158 * Hooks for the ACPI CA debugging infrastructure 159 */ 160 #define _COMPONENT ACPI_EC 161 ACPI_MODULE_NAME("EC") 162 163 /* 164 * EC_COMMAND: 165 * ----------- 166 */ 167 typedef UINT8 EC_COMMAND; 168 169 #define EC_COMMAND_UNKNOWN ((EC_COMMAND) 0x00) 170 #define EC_COMMAND_READ ((EC_COMMAND) 0x80) 171 #define EC_COMMAND_WRITE ((EC_COMMAND) 0x81) 172 #define EC_COMMAND_BURST_ENABLE ((EC_COMMAND) 0x82) 173 #define EC_COMMAND_BURST_DISABLE ((EC_COMMAND) 0x83) 174 #define EC_COMMAND_QUERY ((EC_COMMAND) 0x84) 175 176 /* 177 * EC_STATUS: 178 * ---------- 179 * The encoding of the EC status register is illustrated below. 180 * Note that a set bit (1) indicates the property is TRUE 181 * (e.g. if bit 0 is set then the output buffer is full). 182 * +-+-+-+-+-+-+-+-+ 183 * |7|6|5|4|3|2|1|0| 184 * +-+-+-+-+-+-+-+-+ 185 * | | | | | | | | 186 * | | | | | | | +- Output Buffer Full? 187 * | | | | | | +--- Input Buffer Full? 188 * | | | | | +----- <reserved> 189 * | | | | +------- Data Register is Command Byte? 190 * | | | +--------- Burst Mode Enabled? 191 * | | +----------- SCI Event? 192 * | +------------- SMI Event? 193 * +--------------- <Reserved> 194 * 195 */ 196 typedef UINT8 EC_STATUS; 197 198 #define EC_FLAG_OUTPUT_BUFFER ((EC_STATUS) 0x01) 199 #define EC_FLAG_INPUT_BUFFER ((EC_STATUS) 0x02) 200 #define EC_FLAG_BURST_MODE ((EC_STATUS) 0x10) 201 #define EC_FLAG_SCI ((EC_STATUS) 0x20) 202 203 /* 204 * EC_EVENT: 205 * --------- 206 */ 207 typedef UINT8 EC_EVENT; 208 209 #define EC_EVENT_UNKNOWN ((EC_EVENT) 0x00) 210 #define EC_EVENT_OUTPUT_BUFFER_FULL ((EC_EVENT) 0x01) 211 #define EC_EVENT_INPUT_BUFFER_EMPTY ((EC_EVENT) 0x02) 212 #define EC_EVENT_SCI ((EC_EVENT) 0x20) 213 214 /* 215 * Register access primitives 216 */ 217 #define EC_GET_DATA(sc) \ 218 bus_space_read_1((sc)->ec_data_tag, (sc)->ec_data_handle, 0) 219 220 #define EC_SET_DATA(sc, v) \ 221 bus_space_write_1((sc)->ec_data_tag, (sc)->ec_data_handle, 0, (v)) 222 223 #define EC_GET_CSR(sc) \ 224 bus_space_read_1((sc)->ec_csr_tag, (sc)->ec_csr_handle, 0) 225 226 #define EC_SET_CSR(sc, v) \ 227 bus_space_write_1((sc)->ec_csr_tag, (sc)->ec_csr_handle, 0, (v)) 228 229 /* Embedded Controller Boot Resources Table (ECDT) */ 230 typedef struct { 231 ACPI_TABLE_HEADER header; 232 ACPI_GENERIC_ADDRESS control; 233 ACPI_GENERIC_ADDRESS data; 234 UINT32 uid; 235 UINT8 gpe_bit; 236 char ec_id[0]; 237 } ACPI_TABLE_ECDT; 238 239 /* Additional params to pass from the probe routine */ 240 struct acpi_ec_params { 241 int glk; 242 int gpe_bit; 243 ACPI_HANDLE gpe_handle; 244 int uid; 245 }; 246 247 /* Indicate that this device has already been probed via ECDT. */ 248 #define DEV_ECDT(x) (acpi_get_magic(x) == (int)&acpi_ec_devclass) 249 250 /* 251 * Driver softc. 252 */ 253 struct acpi_ec_softc { 254 device_t ec_dev; 255 ACPI_HANDLE ec_handle; 256 int ec_uid; 257 ACPI_HANDLE ec_gpehandle; 258 UINT8 ec_gpebit; 259 UINT8 ec_csrvalue; 260 261 int ec_data_rid; 262 struct resource *ec_data_res; 263 bus_space_tag_t ec_data_tag; 264 bus_space_handle_t ec_data_handle; 265 266 int ec_csr_rid; 267 struct resource *ec_csr_res; 268 bus_space_tag_t ec_csr_tag; 269 bus_space_handle_t ec_csr_handle; 270 271 int ec_glk; 272 int ec_glkhandle; 273 struct sx ec_sxlock; 274 }; 275 276 /* 277 * XXX 278 * I couldn't find it in the spec but other implementations also use a 279 * value of 1 ms for the time to acquire global lock. 280 */ 281 #define EC_LOCK_TIMEOUT 1000 282 283 /* Default interval in microseconds for the status polling loop. */ 284 #define EC_POLL_DELAY 10 285 286 /* Total time in ms spent in the poll loop waiting for a response. */ 287 #define EC_POLL_TIMEOUT 100 288 289 #define EVENT_READY(event, status) \ 290 (((event) == EC_EVENT_OUTPUT_BUFFER_FULL && \ 291 ((status) & EC_FLAG_OUTPUT_BUFFER) != 0) || \ 292 ((event) == EC_EVENT_INPUT_BUFFER_EMPTY && \ 293 ((status) & EC_FLAG_INPUT_BUFFER) == 0)) 294 295 static int ec_poll_timeout = EC_POLL_TIMEOUT; 296 TUNABLE_INT("hw.acpi.ec.poll_timeout", &ec_poll_timeout); 297 298 static __inline ACPI_STATUS 299 EcLock(struct acpi_ec_softc *sc) 300 { 301 ACPI_STATUS status = AE_OK; 302 303 /* Always acquire this EC's mutex. */ 304 sx_xlock(&sc->ec_sxlock); 305 306 /* If _GLK is non-zero, also acquire the global lock. */ 307 if (sc->ec_glk) { 308 status = AcpiAcquireGlobalLock(EC_LOCK_TIMEOUT, &sc->ec_glkhandle); 309 if (ACPI_FAILURE(status)) 310 sx_xunlock(&sc->ec_sxlock); 311 } 312 313 return (status); 314 } 315 316 static __inline void 317 EcUnlock(struct acpi_ec_softc *sc) 318 { 319 if (sc->ec_glk) 320 AcpiReleaseGlobalLock(sc->ec_glkhandle); 321 sx_xunlock(&sc->ec_sxlock); 322 } 323 324 static uint32_t EcGpeHandler(void *Context); 325 static ACPI_STATUS EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function, 326 void *Context, void **return_Context); 327 static ACPI_STATUS EcSpaceHandler(UINT32 Function, 328 ACPI_PHYSICAL_ADDRESS Address, 329 UINT32 width, ACPI_INTEGER *Value, 330 void *Context, void *RegionContext); 331 static ACPI_STATUS EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event); 332 static ACPI_STATUS EcCommand(struct acpi_ec_softc *sc, EC_COMMAND cmd); 333 static ACPI_STATUS EcRead(struct acpi_ec_softc *sc, UINT8 Address, 334 UINT8 *Data); 335 static ACPI_STATUS EcWrite(struct acpi_ec_softc *sc, UINT8 Address, 336 UINT8 *Data); 337 static int acpi_ec_probe(device_t dev); 338 static int acpi_ec_attach(device_t dev); 339 static int acpi_ec_shutdown(device_t dev); 340 341 static device_method_t acpi_ec_methods[] = { 342 /* Device interface */ 343 DEVMETHOD(device_probe, acpi_ec_probe), 344 DEVMETHOD(device_attach, acpi_ec_attach), 345 DEVMETHOD(device_shutdown, acpi_ec_shutdown), 346 347 {0, 0} 348 }; 349 350 static driver_t acpi_ec_driver = { 351 "acpi_ec", 352 acpi_ec_methods, 353 sizeof(struct acpi_ec_softc), 354 }; 355 356 static devclass_t acpi_ec_devclass; 357 DRIVER_MODULE(acpi_ec, acpi, acpi_ec_driver, acpi_ec_devclass, 0, 0); 358 MODULE_DEPEND(acpi_ec, acpi, 1, 1, 1); 359 360 /* 361 * Look for an ECDT and if we find one, set up default GPE and 362 * space handlers to catch attempts to access EC space before 363 * we have a real driver instance in place. 364 * TODO: if people report invalid ECDTs, add a tunable to disable them. 365 */ 366 void 367 acpi_ec_ecdt_probe(device_t parent) 368 { 369 ACPI_TABLE_ECDT *ecdt; 370 ACPI_STATUS status; 371 device_t child; 372 ACPI_HANDLE h; 373 struct acpi_ec_params *params; 374 375 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 376 377 /* Find and validate the ECDT. */ 378 status = AcpiGetFirmwareTable("ECDT", 1, ACPI_LOGICAL_ADDRESSING, 379 (ACPI_TABLE_HEADER **)&ecdt); 380 if (ACPI_FAILURE(status) || 381 ecdt->control.RegisterBitWidth != 8 || 382 ecdt->data.RegisterBitWidth != 8) { 383 return; 384 } 385 386 /* Create the child device with the given unit number. */ 387 child = BUS_ADD_CHILD(parent, 0, "acpi_ec", ecdt->uid); 388 if (child == NULL) { 389 printf("%s: can't add child\n", __func__); 390 return; 391 } 392 393 /* Find and save the ACPI handle for this device. */ 394 status = AcpiGetHandle(NULL, ecdt->ec_id, &h); 395 if (ACPI_FAILURE(status)) { 396 device_delete_child(parent, child); 397 printf("%s: can't get handle\n", __func__); 398 return; 399 } 400 acpi_set_handle(child, h); 401 402 /* Set the data and CSR register addresses. */ 403 bus_set_resource(child, SYS_RES_IOPORT, 0, ecdt->data.Address, 404 /*count*/1); 405 bus_set_resource(child, SYS_RES_IOPORT, 1, ecdt->control.Address, 406 /*count*/1); 407 408 /* 409 * Store values for the probe/attach routines to use. Store the 410 * ECDT GPE bit and set the global lock flag according to _GLK. 411 * Note that it is not perfectly correct to be evaluating a method 412 * before initializing devices, but in practice this function 413 * should be safe to call at this point. 414 */ 415 params = malloc(sizeof(struct acpi_ec_params), M_TEMP, M_WAITOK | M_ZERO); 416 params->gpe_handle = NULL; 417 params->gpe_bit = ecdt->gpe_bit; 418 params->uid = ecdt->uid; 419 acpi_GetInteger(h, "_GLK", ¶ms->glk); 420 acpi_set_private(child, params); 421 acpi_set_magic(child, (int)&acpi_ec_devclass); 422 423 /* Finish the attach process. */ 424 if (device_probe_and_attach(child) != 0) 425 device_delete_child(parent, child); 426 } 427 428 static int 429 acpi_ec_probe(device_t dev) 430 { 431 ACPI_BUFFER buf; 432 ACPI_HANDLE h; 433 ACPI_OBJECT *obj; 434 ACPI_STATUS status; 435 device_t peer; 436 char desc[64]; 437 int ret; 438 struct acpi_ec_params *params; 439 static char *ec_ids[] = { "PNP0C09", NULL }; 440 441 /* Check that this is a device and that EC is not disabled. */ 442 if (acpi_get_type(dev) != ACPI_TYPE_DEVICE || acpi_disabled("ec")) 443 return (ENXIO); 444 445 /* 446 * If probed via ECDT, set description and continue. Otherwise, 447 * we can access the namespace and make sure this is not a 448 * duplicate probe. 449 */ 450 ret = ENXIO; 451 params = NULL; 452 buf.Pointer = NULL; 453 buf.Length = ACPI_ALLOCATE_BUFFER; 454 if (DEV_ECDT(dev)) { 455 params = acpi_get_private(dev); 456 ret = 0; 457 } else if (!acpi_disabled("ec") && 458 ACPI_ID_PROBE(device_get_parent(dev), dev, ec_ids)) { 459 params = malloc(sizeof(struct acpi_ec_params), M_TEMP, 460 M_WAITOK | M_ZERO); 461 h = acpi_get_handle(dev); 462 463 /* 464 * Read the unit ID to check for duplicate attach and the 465 * global lock value to see if we should acquire it when 466 * accessing the EC. 467 */ 468 status = acpi_GetInteger(h, "_UID", ¶ms->uid); 469 if (ACPI_FAILURE(status)) 470 params->uid = 0; 471 status = acpi_GetInteger(h, "_GLK", ¶ms->glk); 472 if (ACPI_FAILURE(status)) 473 params->glk = 0; 474 475 /* 476 * Evaluate the _GPE method to find the GPE bit used by the EC to 477 * signal status (SCI). If it's a package, it contains a reference 478 * and GPE bit, similar to _PRW. 479 */ 480 status = AcpiEvaluateObject(h, "_GPE", NULL, &buf); 481 if (ACPI_FAILURE(status)) { 482 device_printf(dev, "can't evaluate _GPE - %s\n", 483 AcpiFormatException(status)); 484 return (ENXIO); 485 } 486 obj = (ACPI_OBJECT *)buf.Pointer; 487 if (obj == NULL) 488 return (ENXIO); 489 490 switch (obj->Type) { 491 case ACPI_TYPE_INTEGER: 492 params->gpe_handle = NULL; 493 params->gpe_bit = obj->Integer.Value; 494 break; 495 case ACPI_TYPE_PACKAGE: 496 if (!ACPI_PKG_VALID(obj, 2)) 497 goto out; 498 params->gpe_handle = 499 acpi_GetReference(NULL, &obj->Package.Elements[0]); 500 if (params->gpe_handle == NULL || 501 acpi_PkgInt32(obj, 1, ¶ms->gpe_bit) != 0) 502 goto out; 503 break; 504 default: 505 device_printf(dev, "_GPE has invalid type %d\n", obj->Type); 506 goto out; 507 } 508 509 /* Store the values we got from the namespace for attach. */ 510 acpi_set_private(dev, params); 511 512 /* 513 * Check for a duplicate probe. This can happen when a probe 514 * via ECDT succeeded already. If this is a duplicate, disable 515 * this device. 516 */ 517 peer = devclass_get_device(acpi_ec_devclass, params->uid); 518 if (peer == NULL || !device_is_alive(peer)) 519 ret = 0; 520 else 521 device_disable(dev); 522 } 523 524 out: 525 if (ret == 0) { 526 snprintf(desc, sizeof(desc), "Embedded Controller: GPE %#x%s%s", 527 params->gpe_bit, (params->glk) ? ", GLK" : "", 528 DEV_ECDT(dev) ? ", ECDT" : ""); 529 device_set_desc_copy(dev, desc); 530 } 531 532 if (ret > 0 && params) 533 free(params, M_TEMP); 534 if (buf.Pointer) 535 AcpiOsFree(buf.Pointer); 536 return (ret); 537 } 538 539 static int 540 acpi_ec_attach(device_t dev) 541 { 542 struct acpi_ec_softc *sc; 543 struct acpi_ec_params *params; 544 ACPI_STATUS Status; 545 546 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 547 548 /* Fetch/initialize softc (assumes softc is pre-zeroed). */ 549 sc = device_get_softc(dev); 550 params = acpi_get_private(dev); 551 sc->ec_dev = dev; 552 sc->ec_handle = acpi_get_handle(dev); 553 sx_init(&sc->ec_sxlock, "ACPI embedded controller"); 554 555 /* Retrieve previously probed values via device ivars. */ 556 sc->ec_glk = params->glk; 557 sc->ec_gpebit = params->gpe_bit; 558 sc->ec_gpehandle = params->gpe_handle; 559 sc->ec_uid = params->uid; 560 free(params, M_TEMP); 561 562 /* Attach bus resources for data and command/status ports. */ 563 sc->ec_data_rid = 0; 564 sc->ec_data_res = bus_alloc_resource_any(sc->ec_dev, SYS_RES_IOPORT, 565 &sc->ec_data_rid, RF_ACTIVE); 566 if (sc->ec_data_res == NULL) { 567 device_printf(dev, "can't allocate data port\n"); 568 goto error; 569 } 570 sc->ec_data_tag = rman_get_bustag(sc->ec_data_res); 571 sc->ec_data_handle = rman_get_bushandle(sc->ec_data_res); 572 573 sc->ec_csr_rid = 1; 574 sc->ec_csr_res = bus_alloc_resource_any(sc->ec_dev, SYS_RES_IOPORT, 575 &sc->ec_csr_rid, RF_ACTIVE); 576 if (sc->ec_csr_res == NULL) { 577 device_printf(dev, "can't allocate command/status port\n"); 578 goto error; 579 } 580 sc->ec_csr_tag = rman_get_bustag(sc->ec_csr_res); 581 sc->ec_csr_handle = rman_get_bushandle(sc->ec_csr_res); 582 583 /* 584 * Install a handler for this EC's GPE bit. We want edge-triggered 585 * behavior. 586 */ 587 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "attaching GPE handler\n")); 588 Status = AcpiInstallGpeHandler(sc->ec_gpehandle, sc->ec_gpebit, 589 ACPI_GPE_EDGE_TRIGGERED, &EcGpeHandler, sc); 590 if (ACPI_FAILURE(Status)) { 591 device_printf(dev, "can't install GPE handler for %s - %s\n", 592 acpi_name(sc->ec_handle), AcpiFormatException(Status)); 593 goto error; 594 } 595 596 /* 597 * Install address space handler 598 */ 599 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "attaching address space handler\n")); 600 Status = AcpiInstallAddressSpaceHandler(sc->ec_handle, ACPI_ADR_SPACE_EC, 601 &EcSpaceHandler, &EcSpaceSetup, sc); 602 if (ACPI_FAILURE(Status)) { 603 device_printf(dev, "can't install address space handler for %s - %s\n", 604 acpi_name(sc->ec_handle), AcpiFormatException(Status)); 605 goto error; 606 } 607 608 /* Enable runtime GPEs for the handler. */ 609 Status = AcpiSetGpeType(sc->ec_gpehandle, sc->ec_gpebit, 610 ACPI_GPE_TYPE_RUNTIME); 611 if (ACPI_FAILURE(Status)) { 612 device_printf(dev, "AcpiSetGpeType failed: %s\n", 613 AcpiFormatException(Status)); 614 goto error; 615 } 616 Status = AcpiEnableGpe(sc->ec_gpehandle, sc->ec_gpebit, ACPI_NOT_ISR); 617 if (ACPI_FAILURE(Status)) { 618 device_printf(dev, "AcpiEnableGpe failed: %s\n", 619 AcpiFormatException(Status)); 620 goto error; 621 } 622 623 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "acpi_ec_attach complete\n")); 624 return (0); 625 626 error: 627 AcpiRemoveGpeHandler(sc->ec_gpehandle, sc->ec_gpebit, &EcGpeHandler); 628 AcpiRemoveAddressSpaceHandler(sc->ec_handle, ACPI_ADR_SPACE_EC, 629 EcSpaceHandler); 630 if (sc->ec_csr_res) 631 bus_release_resource(sc->ec_dev, SYS_RES_IOPORT, sc->ec_csr_rid, 632 sc->ec_csr_res); 633 if (sc->ec_data_res) 634 bus_release_resource(sc->ec_dev, SYS_RES_IOPORT, sc->ec_data_rid, 635 sc->ec_data_res); 636 sx_destroy(&sc->ec_sxlock); 637 return (ENXIO); 638 } 639 640 static int 641 acpi_ec_shutdown(device_t dev) 642 { 643 struct acpi_ec_softc *sc; 644 645 /* Disable the GPE so we don't get EC events during shutdown. */ 646 sc = device_get_softc(dev); 647 AcpiDisableGpe(sc->ec_gpehandle, sc->ec_gpebit, ACPI_NOT_ISR); 648 return (0); 649 } 650 651 static void 652 EcGpeQueryHandler(void *Context) 653 { 654 struct acpi_ec_softc *sc = (struct acpi_ec_softc *)Context; 655 UINT8 Data; 656 ACPI_STATUS Status; 657 EC_STATUS EcStatus; 658 char qxx[5]; 659 660 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 661 KASSERT(Context != NULL, ("EcGpeQueryHandler called with NULL")); 662 663 Status = EcLock(sc); 664 if (ACPI_FAILURE(Status)) { 665 ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev), 666 "GpeQuery lock error: %s\n", AcpiFormatException(Status)); 667 return; 668 } 669 670 /* 671 * If the EC_SCI bit of the status register is not set, then pass 672 * it along to any potential waiters as it may be an IBE/OBF event. 673 */ 674 EcStatus = EC_GET_CSR(sc); 675 if ((EcStatus & EC_EVENT_SCI) == 0) { 676 sc->ec_csrvalue = EcStatus; 677 wakeup(&sc->ec_csrvalue); 678 EcUnlock(sc); 679 goto re_enable; 680 } 681 682 /* 683 * Send a query command to the EC to find out which _Qxx call it 684 * wants to make. This command clears the SCI bit and also the 685 * interrupt source since we are edge-triggered. 686 */ 687 Status = EcCommand(sc, EC_COMMAND_QUERY); 688 if (ACPI_FAILURE(Status)) { 689 EcUnlock(sc); 690 ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev), 691 "GPE query failed - %s\n", AcpiFormatException(Status)); 692 goto re_enable; 693 } 694 Data = EC_GET_DATA(sc); 695 EcUnlock(sc); 696 697 /* Ignore the value for "no outstanding event". (13.3.5) */ 698 if (Data == 0) 699 goto re_enable; 700 701 /* Evaluate _Qxx to respond to the controller. */ 702 sprintf(qxx, "_Q%02x", Data); 703 strupr(qxx); 704 Status = AcpiEvaluateObject(sc->ec_handle, qxx, NULL, NULL); 705 if (ACPI_FAILURE(Status) && Status != AE_NOT_FOUND) { 706 ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev), 707 "evaluation of GPE query method %s failed - %s\n", 708 qxx, AcpiFormatException(Status)); 709 } 710 711 re_enable: 712 /* Re-enable the GPE event so we'll get future requests. */ 713 Status = AcpiEnableGpe(sc->ec_gpehandle, sc->ec_gpebit, ACPI_NOT_ISR); 714 if (ACPI_FAILURE(Status)) 715 printf("EcGpeQueryHandler: AcpiEnableEvent failed\n"); 716 } 717 718 /* 719 * Handle a GPE. Currently we only handle SCI events as others must 720 * be handled by polling in EcWaitEvent(). This is because some ECs 721 * treat events as level when they should be edge-triggered. 722 */ 723 static uint32_t 724 EcGpeHandler(void *Context) 725 { 726 struct acpi_ec_softc *sc = Context; 727 ACPI_STATUS Status; 728 729 KASSERT(Context != NULL, ("EcGpeHandler called with NULL")); 730 731 /* Disable further GPEs while we handle this one. */ 732 AcpiDisableGpe(sc->ec_gpehandle, sc->ec_gpebit, ACPI_NOT_ISR); 733 734 /* Schedule the GPE query handler. */ 735 Status = AcpiOsQueueForExecution(OSD_PRIORITY_GPE, EcGpeQueryHandler, 736 Context); 737 if (ACPI_FAILURE(Status)) { 738 printf("Queuing GPE query handler failed.\n"); 739 Status = AcpiEnableGpe(sc->ec_gpehandle, sc->ec_gpebit, ACPI_NOT_ISR); 740 if (ACPI_FAILURE(Status)) 741 printf("EcGpeHandler: AcpiEnableEvent failed\n"); 742 } 743 744 return (0); 745 } 746 747 static ACPI_STATUS 748 EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function, void *Context, 749 void **RegionContext) 750 { 751 752 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 753 754 /* 755 * If deactivating a region, always set the output to NULL. Otherwise, 756 * just pass the context through. 757 */ 758 if (Function == ACPI_REGION_DEACTIVATE) 759 *RegionContext = NULL; 760 else 761 *RegionContext = Context; 762 763 return_ACPI_STATUS (AE_OK); 764 } 765 766 static ACPI_STATUS 767 EcSpaceHandler(UINT32 Function, ACPI_PHYSICAL_ADDRESS Address, UINT32 width, 768 ACPI_INTEGER *Value, void *Context, void *RegionContext) 769 { 770 struct acpi_ec_softc *sc = (struct acpi_ec_softc *)Context; 771 ACPI_STATUS Status; 772 UINT8 EcAddr, EcData; 773 int i; 774 775 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, (UINT32)Address); 776 777 if (width % 8 != 0 || Value == NULL || Context == NULL) 778 return_ACPI_STATUS (AE_BAD_PARAMETER); 779 if (Address + (width / 8) - 1 > 0xFF) 780 return_ACPI_STATUS (AE_BAD_ADDRESS); 781 782 if (Function == ACPI_READ) 783 *Value = 0; 784 EcAddr = Address; 785 Status = AE_ERROR; 786 787 /* Perform the transaction(s), based on width. */ 788 for (i = 0; i < width; i += 8, EcAddr++) { 789 Status = EcLock(sc); 790 if (ACPI_FAILURE(Status)) 791 break; 792 793 switch (Function) { 794 case ACPI_READ: 795 Status = EcRead(sc, EcAddr, &EcData); 796 if (ACPI_SUCCESS(Status)) 797 *Value |= ((ACPI_INTEGER)EcData) << i; 798 break; 799 case ACPI_WRITE: 800 EcData = (UINT8)((*Value) >> i); 801 Status = EcWrite(sc, EcAddr, &EcData); 802 break; 803 default: 804 device_printf(sc->ec_dev, "invalid EcSpaceHandler function %d\n", 805 Function); 806 Status = AE_BAD_PARAMETER; 807 break; 808 } 809 EcUnlock(sc); 810 if (ACPI_FAILURE(Status)) 811 break; 812 } 813 814 return_ACPI_STATUS (Status); 815 } 816 817 static ACPI_STATUS 818 EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event) 819 { 820 EC_STATUS EcStatus; 821 ACPI_STATUS Status; 822 int count, i, period, retval, slp_ival; 823 static int EcDbgMaxDelay; 824 825 sx_assert(&sc->ec_sxlock, SX_XLOCKED); 826 Status = AE_NO_HARDWARE_RESPONSE; 827 828 /* 829 * Wait for 1 us before checking the CSR. Testing shows about 830 * 50% of requests complete in 1 us and 90% of them complete 831 * in 5 us or less. 832 */ 833 AcpiOsStall(1); 834 835 /* 836 * Poll the EC status register for up to 1 ms in chunks of 10 us 837 * to detect completion of the last command. 838 */ 839 for (i = 0; i < 1000 / EC_POLL_DELAY; i++) { 840 EcStatus = EC_GET_CSR(sc); 841 if (EVENT_READY(Event, EcStatus)) { 842 Status = AE_OK; 843 break; 844 } 845 AcpiOsStall(EC_POLL_DELAY); 846 } 847 period = i * EC_POLL_DELAY; 848 849 /* 850 * If we still don't have a response and we're up and running, wait up 851 * to ec_poll_timeout ms for completion, sleeping for chunks of 10 ms. 852 */ 853 slp_ival = 0; 854 if (Status != AE_OK) { 855 retval = ENXIO; 856 count = ec_poll_timeout / 10; 857 if (count == 0) 858 count = 1; 859 slp_ival = hz / 100; 860 if (slp_ival == 0) 861 slp_ival = 1; 862 for (i = 0; i < count; i++) { 863 if (retval != 0) 864 EcStatus = EC_GET_CSR(sc); 865 else 866 EcStatus = sc->ec_csrvalue; 867 if (EVENT_READY(Event, EcStatus)) { 868 Status = AE_OK; 869 break; 870 } 871 if (!cold) 872 retval = tsleep(&sc->ec_csrvalue, PZERO, "ecpoll", slp_ival); 873 else 874 AcpiOsStall(10000); 875 } 876 } 877 878 /* Calculate new delay and print it if it exceeds the max. */ 879 if (slp_ival > 0) 880 period += i * 10000; 881 if (period > EcDbgMaxDelay) { 882 EcDbgMaxDelay = period; 883 ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev), 884 "info: new max delay is %d us\n", period); 885 } 886 887 return (Status); 888 } 889 890 static ACPI_STATUS 891 EcCommand(struct acpi_ec_softc *sc, EC_COMMAND cmd) 892 { 893 ACPI_STATUS Status; 894 EC_EVENT Event; 895 896 sx_assert(&sc->ec_sxlock, SX_XLOCKED); 897 898 /* Decide what to wait for based on command type. */ 899 switch (cmd) { 900 case EC_COMMAND_READ: 901 case EC_COMMAND_WRITE: 902 case EC_COMMAND_BURST_DISABLE: 903 Event = EC_EVENT_INPUT_BUFFER_EMPTY; 904 break; 905 case EC_COMMAND_QUERY: 906 case EC_COMMAND_BURST_ENABLE: 907 Event = EC_EVENT_OUTPUT_BUFFER_FULL; 908 break; 909 default: 910 ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev), 911 "EcCommand: Invalid command %#x\n", cmd); 912 return (AE_BAD_PARAMETER); 913 } 914 915 /* Run the command and wait for the chosen event. */ 916 EC_SET_CSR(sc, cmd); 917 Status = EcWaitEvent(sc, Event); 918 if (ACPI_FAILURE(Status)) { 919 ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev), 920 "EcCommand: no response to %#x\n", cmd); 921 } 922 923 return (Status); 924 } 925 926 static ACPI_STATUS 927 EcRead(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data) 928 { 929 ACPI_STATUS Status; 930 931 sx_assert(&sc->ec_sxlock, SX_XLOCKED); 932 933 #ifdef notyet 934 /* If we can't start burst mode, continue anyway. */ 935 EcCommand(sc, EC_COMMAND_BURST_ENABLE); 936 #endif 937 938 Status = EcCommand(sc, EC_COMMAND_READ); 939 if (ACPI_FAILURE(Status)) 940 return (Status); 941 942 EC_SET_DATA(sc, Address); 943 Status = EcWaitEvent(sc, EC_EVENT_OUTPUT_BUFFER_FULL); 944 if (ACPI_FAILURE(Status)) { 945 ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev), 946 "EcRead: Failed waiting for EC to send data.\n"); 947 return (Status); 948 } 949 950 *Data = EC_GET_DATA(sc); 951 952 #ifdef notyet 953 if (sc->ec_burstactive) { 954 Status = EcCommand(sc, EC_COMMAND_BURST_DISABLE); 955 if (ACPI_FAILURE(Status)) 956 return (Status); 957 } 958 #endif 959 960 return (AE_OK); 961 } 962 963 static ACPI_STATUS 964 EcWrite(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data) 965 { 966 ACPI_STATUS Status; 967 968 sx_assert(&sc->ec_sxlock, SX_XLOCKED); 969 970 #ifdef notyet 971 /* If we can't start burst mode, continue anyway. */ 972 EcCommand(sc, EC_COMMAND_BURST_ENABLE); 973 #endif 974 975 Status = EcCommand(sc, EC_COMMAND_WRITE); 976 if (ACPI_FAILURE(Status)) 977 return (Status); 978 979 EC_SET_DATA(sc, Address); 980 Status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY); 981 if (ACPI_FAILURE(Status)) { 982 ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev), 983 "EcRead: Failed waiting for EC to process address\n"); 984 return (Status); 985 } 986 987 EC_SET_DATA(sc, *Data); 988 Status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY); 989 if (ACPI_FAILURE(Status)) { 990 ACPI_VPRINT(sc->ec_dev, acpi_device_get_parent_softc(sc->ec_dev), 991 "EcWrite: Failed waiting for EC to process data\n"); 992 return (Status); 993 } 994 995 #ifdef notyet 996 if (sc->ec_burstactive) { 997 Status = EcCommand(sc, EC_COMMAND_BURST_DISABLE); 998 if (ACPI_FAILURE(Status)) 999 return (Status); 1000 } 1001 #endif 1002 1003 return (AE_OK); 1004 } 1005