1 /*- 2 * Copyright (c) 2003-2007 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: head/sys/dev/acpica/acpi_ec.c 246128 2013-01-30 18:01:20Z sbz $ 29 */ 30 31 #include "opt_acpi.h" 32 #include <sys/param.h> 33 #include <sys/kernel.h> 34 #include <sys/bus.h> 35 #include <sys/lock.h> 36 #include <sys/malloc.h> 37 #include <sys/module.h> 38 #include <sys/rman.h> 39 #include <sys/ktr.h> 40 41 #include "acpi.h" 42 #include "accommon.h" 43 44 #include <dev/acpica/acpivar.h> 45 46 /* Hooks for the ACPICA debugging infrastructure */ 47 #define _COMPONENT ACPI_EC 48 ACPI_MODULE_NAME("EC") 49 50 #define rebooting 0 51 52 /* 53 * EC_COMMAND: 54 * ----------- 55 */ 56 typedef UINT8 EC_COMMAND; 57 58 #define EC_COMMAND_UNKNOWN ((EC_COMMAND) 0x00) 59 #define EC_COMMAND_READ ((EC_COMMAND) 0x80) 60 #define EC_COMMAND_WRITE ((EC_COMMAND) 0x81) 61 #define EC_COMMAND_BURST_ENABLE ((EC_COMMAND) 0x82) 62 #define EC_COMMAND_BURST_DISABLE ((EC_COMMAND) 0x83) 63 #define EC_COMMAND_QUERY ((EC_COMMAND) 0x84) 64 65 /* 66 * EC_STATUS: 67 * ---------- 68 * The encoding of the EC status register is illustrated below. 69 * Note that a set bit (1) indicates the property is TRUE 70 * (e.g. if bit 0 is set then the output buffer is full). 71 * +-+-+-+-+-+-+-+-+ 72 * |7|6|5|4|3|2|1|0| 73 * +-+-+-+-+-+-+-+-+ 74 * | | | | | | | | 75 * | | | | | | | +- Output Buffer Full? 76 * | | | | | | +--- Input Buffer Full? 77 * | | | | | +----- <reserved> 78 * | | | | +------- Data Register is Command Byte? 79 * | | | +--------- Burst Mode Enabled? 80 * | | +----------- SCI Event? 81 * | +------------- SMI Event? 82 * +--------------- <reserved> 83 * 84 */ 85 typedef UINT8 EC_STATUS; 86 87 #define EC_FLAG_OUTPUT_BUFFER ((EC_STATUS) 0x01) 88 #define EC_FLAG_INPUT_BUFFER ((EC_STATUS) 0x02) 89 #define EC_FLAG_DATA_IS_CMD ((EC_STATUS) 0x08) 90 #define EC_FLAG_BURST_MODE ((EC_STATUS) 0x10) 91 92 /* 93 * EC_EVENT: 94 * --------- 95 */ 96 typedef UINT8 EC_EVENT; 97 98 #define EC_EVENT_UNKNOWN ((EC_EVENT) 0x00) 99 #define EC_EVENT_OUTPUT_BUFFER_FULL ((EC_EVENT) 0x01) 100 #define EC_EVENT_INPUT_BUFFER_EMPTY ((EC_EVENT) 0x02) 101 #define EC_EVENT_SCI ((EC_EVENT) 0x20) 102 #define EC_EVENT_SMI ((EC_EVENT) 0x40) 103 104 /* Data byte returned after burst enable indicating it was successful. */ 105 #define EC_BURST_ACK 0x90 106 107 /* 108 * Register access primitives 109 */ 110 #define EC_GET_DATA(sc) \ 111 bus_space_read_1((sc)->ec_data_tag, (sc)->ec_data_handle, 0) 112 113 #define EC_SET_DATA(sc, v) \ 114 bus_space_write_1((sc)->ec_data_tag, (sc)->ec_data_handle, 0, (v)) 115 116 #define EC_GET_CSR(sc) \ 117 bus_space_read_1((sc)->ec_csr_tag, (sc)->ec_csr_handle, 0) 118 119 #define EC_SET_CSR(sc, v) \ 120 bus_space_write_1((sc)->ec_csr_tag, (sc)->ec_csr_handle, 0, (v)) 121 122 /* Additional params to pass from the probe routine */ 123 struct acpi_ec_params { 124 int glk; 125 int gpe_bit; 126 ACPI_HANDLE gpe_handle; 127 int uid; 128 }; 129 130 /* 131 * Driver softc. 132 */ 133 struct acpi_ec_softc { 134 device_t ec_dev; 135 ACPI_HANDLE ec_handle; 136 int ec_uid; 137 ACPI_HANDLE ec_gpehandle; 138 UINT8 ec_gpebit; 139 140 int ec_data_rid; 141 struct resource *ec_data_res; 142 bus_space_tag_t ec_data_tag; 143 bus_space_handle_t ec_data_handle; 144 145 int ec_csr_rid; 146 struct resource *ec_csr_res; 147 bus_space_tag_t ec_csr_tag; 148 bus_space_handle_t ec_csr_handle; 149 150 int ec_glk; 151 int ec_glkhandle; 152 int ec_burstactive; 153 int ec_sci_pend; 154 volatile u_int ec_gencount; 155 int ec_suspending; 156 }; 157 158 /* 159 * XXX njl 160 * I couldn't find it in the spec but other implementations also use a 161 * value of 1 ms for the time to acquire global lock. 162 */ 163 #define EC_LOCK_TIMEOUT 1000 164 165 /* Default delay in microseconds between each run of the status polling loop. */ 166 #define EC_POLL_DELAY 50 167 168 /* Total time in ms spent waiting for a response from EC. */ 169 #define EC_TIMEOUT 750 170 171 #define EVENT_READY(event, status) \ 172 (((event) == EC_EVENT_OUTPUT_BUFFER_FULL && \ 173 ((status) & EC_FLAG_OUTPUT_BUFFER) != 0) || \ 174 ((event) == EC_EVENT_INPUT_BUFFER_EMPTY && \ 175 ((status) & EC_FLAG_INPUT_BUFFER) == 0)) 176 177 ACPI_SERIAL_DECL(ec, "ACPI embedded controller"); 178 179 static SYSCTL_NODE(_debug_acpi, OID_AUTO, ec, CTLFLAG_RD, NULL, "EC debugging"); 180 181 static int ec_burst_mode; 182 TUNABLE_INT("debug.acpi.ec.burst", &ec_burst_mode); 183 SYSCTL_INT(_debug_acpi_ec, OID_AUTO, burst, CTLFLAG_RW, &ec_burst_mode, 0, 184 "Enable use of burst mode (faster for nearly all systems)"); 185 static int ec_polled_mode; 186 TUNABLE_INT("debug.acpi.ec.polled", &ec_polled_mode); 187 SYSCTL_INT(_debug_acpi_ec, OID_AUTO, polled, CTLFLAG_RW, &ec_polled_mode, 0, 188 "Force use of polled mode (only if interrupt mode doesn't work)"); 189 static int ec_timeout = EC_TIMEOUT; 190 TUNABLE_INT("debug.acpi.ec.timeout", &ec_timeout); 191 SYSCTL_INT(_debug_acpi_ec, OID_AUTO, timeout, CTLFLAG_RW, &ec_timeout, 192 EC_TIMEOUT, "Total time spent waiting for a response (poll+sleep)"); 193 194 #ifndef KTR_ACPI_EC 195 #define KTR_ACPI_EC KTR_ALL 196 #endif 197 198 KTR_INFO_MASTER(acpi_ec); 199 KTR_INFO(KTR_ACPI_EC, acpi_ec, burstdis, 0, 200 "ec burst disabled in waitevent (%s)", const char *msg); 201 KTR_INFO(KTR_ACPI_EC, acpi_ec, burstdisok, 1, 202 "ec disabled burst ok"); 203 KTR_INFO(KTR_ACPI_EC, acpi_ec, burstenl, 2, 204 "ec burst enabled"); 205 KTR_INFO(KTR_ACPI_EC, acpi_ec, cmdrun, 3, 206 "ec running command %#hhx", EC_COMMAND cmd); 207 KTR_INFO(KTR_ACPI_EC, acpi_ec, gpehdlstart, 4, 208 "ec gpe handler start"); 209 KTR_INFO(KTR_ACPI_EC, acpi_ec, gpequeuehdl, 5, 210 "ec gpe queueing query handler"); 211 KTR_INFO(KTR_ACPI_EC, acpi_ec, gperun, 6, 212 "ec running gpe handler directly"); 213 KTR_INFO(KTR_ACPI_EC, acpi_ec, qryoknotrun, 7, 214 "ec query ok, not running _Q%02hhX", uint8_t Data); 215 KTR_INFO(KTR_ACPI_EC, acpi_ec, qryokrun, 8, 216 "ec query ok, running _Q%02hhX", uint8_t Data); 217 KTR_INFO(KTR_ACPI_EC, acpi_ec, readaddr, 9, 218 "ec read from %#hhx", UINT8 Address); 219 KTR_INFO(KTR_ACPI_EC, acpi_ec, timeout, 10, 220 "error: ec wait timed out"); 221 KTR_INFO(KTR_ACPI_EC, acpi_ec, waitrdy, 11, 222 "ec %s wait ready, status %#hhx", const char *msg, EC_STATUS ec_status); 223 KTR_INFO(KTR_ACPI_EC, acpi_ec, writeaddr, 12, 224 "ec write to %#hhx, data %#hhx", UINT8 Address, UINT8 Data); 225 226 static ACPI_STATUS 227 EcLock(struct acpi_ec_softc *sc) 228 { 229 ACPI_STATUS status; 230 231 /* If _GLK is non-zero, acquire the global lock. */ 232 status = AE_OK; 233 if (sc->ec_glk) { 234 status = AcpiAcquireGlobalLock(EC_LOCK_TIMEOUT, &sc->ec_glkhandle); 235 if (ACPI_FAILURE(status)) 236 return (status); 237 } 238 ACPI_SERIAL_BEGIN(ec); 239 return (status); 240 } 241 242 static void 243 EcUnlock(struct acpi_ec_softc *sc) 244 { 245 ACPI_SERIAL_END(ec); 246 if (sc->ec_glk) 247 AcpiReleaseGlobalLock(sc->ec_glkhandle); 248 } 249 250 static UINT32 EcGpeHandler(ACPI_HANDLE GpeDevice, 251 UINT32 GpeNumber, void *Context); 252 static ACPI_STATUS EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function, 253 void *Context, void **return_Context); 254 static ACPI_STATUS EcSpaceHandler(UINT32 Function, 255 ACPI_PHYSICAL_ADDRESS Address, 256 UINT32 Width, UINT64 *Value, 257 void *Context, void *RegionContext); 258 static ACPI_STATUS EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event, 259 u_int gen_count); 260 static ACPI_STATUS EcCommand(struct acpi_ec_softc *sc, EC_COMMAND cmd); 261 static ACPI_STATUS EcRead(struct acpi_ec_softc *sc, UINT8 Address, 262 UINT8 *Data); 263 static ACPI_STATUS EcWrite(struct acpi_ec_softc *sc, UINT8 Address, 264 UINT8 Data); 265 static int acpi_ec_probe(device_t dev); 266 static int acpi_ec_attach(device_t dev); 267 static int acpi_ec_suspend(device_t dev); 268 static int acpi_ec_resume(device_t dev); 269 static int acpi_ec_shutdown(device_t dev); 270 static int acpi_ec_read_method(device_t dev, u_int addr, 271 UINT64 *val, int width); 272 static int acpi_ec_write_method(device_t dev, u_int addr, 273 UINT64 val, int width); 274 275 static device_method_t acpi_ec_methods[] = { 276 /* Device interface */ 277 DEVMETHOD(device_probe, acpi_ec_probe), 278 DEVMETHOD(device_attach, acpi_ec_attach), 279 DEVMETHOD(device_suspend, acpi_ec_suspend), 280 DEVMETHOD(device_resume, acpi_ec_resume), 281 DEVMETHOD(device_shutdown, acpi_ec_shutdown), 282 283 /* Embedded controller interface */ 284 DEVMETHOD(acpi_ec_read, acpi_ec_read_method), 285 DEVMETHOD(acpi_ec_write, acpi_ec_write_method), 286 287 DEVMETHOD_END 288 }; 289 290 static driver_t acpi_ec_driver = { 291 "acpi_ec", 292 acpi_ec_methods, 293 sizeof(struct acpi_ec_softc), 294 }; 295 296 static devclass_t acpi_ec_devclass; 297 DRIVER_MODULE(acpi_ec, acpi, acpi_ec_driver, acpi_ec_devclass, NULL, NULL); 298 MODULE_DEPEND(acpi_ec, acpi, 1, 1, 1); 299 300 /* 301 * Look for an ECDT and if we find one, set up default GPE and 302 * space handlers to catch attempts to access EC space before 303 * we have a real driver instance in place. 304 * 305 * TODO: Some old Gateway laptops need us to fake up an ECDT or 306 * otherwise attach early so that _REG methods can run. 307 */ 308 void 309 acpi_ec_ecdt_probe(device_t parent) 310 { 311 ACPI_TABLE_ECDT *ecdt; 312 ACPI_STATUS status; 313 device_t child; 314 ACPI_HANDLE h; 315 struct acpi_ec_params *params; 316 317 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 318 319 /* Find and validate the ECDT. */ 320 status = AcpiGetTable(ACPI_SIG_ECDT, 1, (ACPI_TABLE_HEADER **)&ecdt); 321 if (ACPI_FAILURE(status) || 322 ecdt->Control.BitWidth != 8 || 323 ecdt->Data.BitWidth != 8) { 324 return; 325 } 326 327 /* Create the child device with the given unit number. */ 328 child = BUS_ADD_CHILD(parent, parent, 0, "acpi_ec", ecdt->Uid); 329 if (child == NULL) { 330 kprintf("%s: can't add child\n", __func__); 331 return; 332 } 333 334 /* Find and save the ACPI handle for this device. */ 335 status = AcpiGetHandle(NULL, ecdt->Id, &h); 336 if (ACPI_FAILURE(status)) { 337 device_delete_child(parent, child); 338 kprintf("%s: can't get handle\n", __func__); 339 return; 340 } 341 acpi_set_handle(child, h); 342 343 /* Set the data and CSR register addresses. */ 344 bus_set_resource(child, SYS_RES_IOPORT, 0, ecdt->Data.Address, 345 /*count*/1, -1); 346 bus_set_resource(child, SYS_RES_IOPORT, 1, ecdt->Control.Address, 347 /*count*/1, -1); 348 349 /* 350 * Store values for the probe/attach routines to use. Store the 351 * ECDT GPE bit and set the global lock flag according to _GLK. 352 * Note that it is not perfectly correct to be evaluating a method 353 * before initializing devices, but in practice this function 354 * should be safe to call at this point. 355 */ 356 params = kmalloc(sizeof(struct acpi_ec_params), M_TEMP, M_WAITOK | M_ZERO); 357 params->gpe_handle = NULL; 358 params->gpe_bit = ecdt->Gpe; 359 params->uid = ecdt->Uid; 360 acpi_GetInteger(h, "_GLK", ¶ms->glk); 361 acpi_set_private(child, params); 362 363 /* Finish the attach process. */ 364 if (device_probe_and_attach(child) != 0) 365 device_delete_child(parent, child); 366 } 367 368 static int 369 acpi_ec_probe(device_t dev) 370 { 371 ACPI_BUFFER buf; 372 ACPI_HANDLE h; 373 ACPI_OBJECT *obj; 374 ACPI_STATUS status; 375 device_t peer; 376 char desc[64]; 377 int ecdt; 378 int ret; 379 struct acpi_ec_params *params; 380 static char *ec_ids[] = { "PNP0C09", NULL }; 381 382 /* Check that this is a device and that EC is not disabled. */ 383 if (acpi_get_type(dev) != ACPI_TYPE_DEVICE || acpi_disabled("ec")) 384 return (ENXIO); 385 386 /* 387 * If probed via ECDT, set description and continue. Otherwise, 388 * we can access the namespace and make sure this is not a 389 * duplicate probe. 390 */ 391 ret = ENXIO; 392 ecdt = 0; 393 buf.Pointer = NULL; 394 buf.Length = ACPI_ALLOCATE_BUFFER; 395 params = acpi_get_private(dev); 396 if (params != NULL) { 397 ecdt = 1; 398 ret = 0; 399 } else if (ACPI_ID_PROBE(device_get_parent(dev), dev, ec_ids)) { 400 params = kmalloc(sizeof(struct acpi_ec_params), M_TEMP, 401 M_WAITOK | M_ZERO); 402 h = acpi_get_handle(dev); 403 404 /* 405 * Read the unit ID to check for duplicate attach and the 406 * global lock value to see if we should acquire it when 407 * accessing the EC. 408 */ 409 status = acpi_GetInteger(h, "_UID", ¶ms->uid); 410 if (ACPI_FAILURE(status)) 411 params->uid = 0; 412 status = acpi_GetInteger(h, "_GLK", ¶ms->glk); 413 if (ACPI_FAILURE(status)) 414 params->glk = 0; 415 416 /* 417 * Evaluate the _GPE method to find the GPE bit used by the EC to 418 * signal status (SCI). If it's a package, it contains a reference 419 * and GPE bit, similar to _PRW. 420 */ 421 status = AcpiEvaluateObject(h, "_GPE", NULL, &buf); 422 if (ACPI_FAILURE(status)) { 423 device_printf(dev, "can't evaluate _GPE - %s\n", 424 AcpiFormatException(status)); 425 goto out; 426 } 427 obj = (ACPI_OBJECT *)buf.Pointer; 428 if (obj == NULL) 429 goto out; 430 431 switch (obj->Type) { 432 case ACPI_TYPE_INTEGER: 433 params->gpe_handle = NULL; 434 params->gpe_bit = obj->Integer.Value; 435 break; 436 case ACPI_TYPE_PACKAGE: 437 if (!ACPI_PKG_VALID(obj, 2)) 438 goto out; 439 params->gpe_handle = 440 acpi_GetReference(NULL, &obj->Package.Elements[0]); 441 if (params->gpe_handle == NULL || 442 acpi_PkgInt32(obj, 1, ¶ms->gpe_bit) != 0) 443 goto out; 444 break; 445 default: 446 device_printf(dev, "_GPE has invalid type %d\n", obj->Type); 447 goto out; 448 } 449 450 /* Store the values we got from the namespace for attach. */ 451 acpi_set_private(dev, params); 452 453 /* 454 * Check for a duplicate probe. This can happen when a probe 455 * via ECDT succeeded already. If this is a duplicate, disable 456 * this device. 457 */ 458 peer = devclass_get_device(acpi_ec_devclass, params->uid); 459 if (peer == NULL || !device_is_alive(peer)) 460 ret = 0; 461 else 462 device_disable(dev); 463 } 464 465 out: 466 if (ret == 0) { 467 ksnprintf(desc, sizeof(desc), "Embedded Controller: GPE %#x%s%s", 468 params->gpe_bit, (params->glk) ? ", GLK" : "", 469 ecdt ? ", ECDT" : ""); 470 device_set_desc_copy(dev, desc); 471 } 472 473 if (ret > 0 && params) 474 kfree(params, M_TEMP); 475 if (buf.Pointer) 476 AcpiOsFree(buf.Pointer); 477 return (ret); 478 } 479 480 static int 481 acpi_ec_attach(device_t dev) 482 { 483 struct acpi_ec_softc *sc; 484 struct acpi_ec_params *params; 485 ACPI_STATUS Status; 486 487 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 488 489 /* Fetch/initialize softc (assumes softc is pre-zeroed). */ 490 sc = device_get_softc(dev); 491 params = acpi_get_private(dev); 492 sc->ec_dev = dev; 493 sc->ec_handle = acpi_get_handle(dev); 494 ACPI_SERIAL_INIT(ec); 495 496 /* Retrieve previously probed values via device ivars. */ 497 sc->ec_glk = params->glk; 498 sc->ec_gpebit = params->gpe_bit; 499 sc->ec_gpehandle = params->gpe_handle; 500 sc->ec_uid = params->uid; 501 sc->ec_suspending = FALSE; 502 acpi_set_private(dev, NULL); 503 kfree(params, M_TEMP); 504 505 /* Attach bus resources for data and command/status ports. */ 506 sc->ec_data_rid = 0; 507 sc->ec_data_res = bus_alloc_resource_any(sc->ec_dev, SYS_RES_IOPORT, 508 &sc->ec_data_rid, RF_ACTIVE); 509 if (sc->ec_data_res == NULL) { 510 device_printf(dev, "can't allocate data port\n"); 511 goto error; 512 } 513 sc->ec_data_tag = rman_get_bustag(sc->ec_data_res); 514 sc->ec_data_handle = rman_get_bushandle(sc->ec_data_res); 515 516 sc->ec_csr_rid = 1; 517 sc->ec_csr_res = bus_alloc_resource_any(sc->ec_dev, SYS_RES_IOPORT, 518 &sc->ec_csr_rid, RF_ACTIVE); 519 if (sc->ec_csr_res == NULL) { 520 device_printf(dev, "can't allocate command/status port\n"); 521 goto error; 522 } 523 sc->ec_csr_tag = rman_get_bustag(sc->ec_csr_res); 524 sc->ec_csr_handle = rman_get_bushandle(sc->ec_csr_res); 525 526 /* 527 * Install a handler for this EC's GPE bit. We want edge-triggered 528 * behavior. 529 */ 530 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "attaching GPE handler\n")); 531 Status = AcpiInstallGpeHandler(sc->ec_gpehandle, sc->ec_gpebit, 532 ACPI_GPE_EDGE_TRIGGERED, EcGpeHandler, sc); 533 if (ACPI_FAILURE(Status)) { 534 device_printf(dev, "can't install GPE handler for %s - %s\n", 535 acpi_name(sc->ec_handle), AcpiFormatException(Status)); 536 goto error; 537 } 538 539 /* 540 * Install address space handler 541 */ 542 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "attaching address space handler\n")); 543 Status = AcpiInstallAddressSpaceHandler(sc->ec_handle, ACPI_ADR_SPACE_EC, 544 &EcSpaceHandler, &EcSpaceSetup, sc); 545 if (ACPI_FAILURE(Status)) { 546 device_printf(dev, "can't install address space handler for %s - %s\n", 547 acpi_name(sc->ec_handle), AcpiFormatException(Status)); 548 goto error; 549 } 550 551 /* Enable runtime GPEs for the handler. */ 552 Status = AcpiEnableGpe(sc->ec_gpehandle, sc->ec_gpebit); 553 if (ACPI_FAILURE(Status)) { 554 device_printf(dev, "AcpiEnableGpe failed: %s\n", 555 AcpiFormatException(Status)); 556 goto error; 557 } 558 559 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "acpi_ec_attach complete\n")); 560 return (0); 561 562 error: 563 AcpiRemoveGpeHandler(sc->ec_gpehandle, sc->ec_gpebit, EcGpeHandler); 564 AcpiRemoveAddressSpaceHandler(sc->ec_handle, ACPI_ADR_SPACE_EC, 565 EcSpaceHandler); 566 if (sc->ec_csr_res) 567 bus_release_resource(sc->ec_dev, SYS_RES_IOPORT, sc->ec_csr_rid, 568 sc->ec_csr_res); 569 if (sc->ec_data_res) 570 bus_release_resource(sc->ec_dev, SYS_RES_IOPORT, sc->ec_data_rid, 571 sc->ec_data_res); 572 return (ENXIO); 573 } 574 575 static int 576 acpi_ec_suspend(device_t dev) 577 { 578 struct acpi_ec_softc *sc; 579 580 sc = device_get_softc(dev); 581 sc->ec_suspending = TRUE; 582 return (0); 583 } 584 585 static int 586 acpi_ec_resume(device_t dev) 587 { 588 struct acpi_ec_softc *sc; 589 590 sc = device_get_softc(dev); 591 sc->ec_suspending = FALSE; 592 return (0); 593 } 594 595 static int 596 acpi_ec_shutdown(device_t dev) 597 { 598 struct acpi_ec_softc *sc; 599 600 /* Disable the GPE so we don't get EC events during shutdown. */ 601 sc = device_get_softc(dev); 602 AcpiDisableGpe(sc->ec_gpehandle, sc->ec_gpebit); 603 return (0); 604 } 605 606 /* Methods to allow other devices (e.g., smbat) to read/write EC space. */ 607 static int 608 acpi_ec_read_method(device_t dev, u_int addr, UINT64 *val, int width) 609 { 610 struct acpi_ec_softc *sc; 611 ACPI_STATUS status; 612 613 sc = device_get_softc(dev); 614 status = EcSpaceHandler(ACPI_READ, addr, width * 8, val, sc, NULL); 615 if (ACPI_FAILURE(status)) 616 return (ENXIO); 617 return (0); 618 } 619 620 static int 621 acpi_ec_write_method(device_t dev, u_int addr, UINT64 val, int width) 622 { 623 struct acpi_ec_softc *sc; 624 ACPI_STATUS status; 625 626 sc = device_get_softc(dev); 627 status = EcSpaceHandler(ACPI_WRITE, addr, width * 8, &val, sc, NULL); 628 if (ACPI_FAILURE(status)) 629 return (ENXIO); 630 return (0); 631 } 632 633 static ACPI_STATUS 634 EcCheckStatus(struct acpi_ec_softc *sc, const char *msg, EC_EVENT event) 635 { 636 ACPI_STATUS status; 637 EC_STATUS ec_status; 638 639 status = AE_NO_HARDWARE_RESPONSE; 640 ec_status = EC_GET_CSR(sc); 641 if (sc->ec_burstactive && !(ec_status & EC_FLAG_BURST_MODE)) { 642 KTR_LOG(acpi_ec_burstdis, msg); 643 sc->ec_burstactive = FALSE; 644 } 645 if (EVENT_READY(event, ec_status)) { 646 KTR_LOG(acpi_ec_waitrdy, msg, ec_status); 647 status = AE_OK; 648 } 649 return (status); 650 } 651 652 static void 653 EcGpeQueryHandler(void *Context) 654 { 655 struct acpi_ec_softc *sc = (struct acpi_ec_softc *)Context; 656 UINT8 Data; 657 ACPI_STATUS Status; 658 int retry, sci_enqueued; 659 char qxx[5]; 660 661 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 662 KASSERT(Context != NULL, ("EcGpeQueryHandler called with NULL")); 663 664 /* Serialize user access with EcSpaceHandler(). */ 665 Status = EcLock(sc); 666 if (ACPI_FAILURE(Status)) { 667 device_printf(sc->ec_dev, "GpeQuery lock error: %s\n", 668 AcpiFormatException(Status)); 669 return; 670 } 671 672 /* 673 * Send a query command to the EC to find out which _Qxx call it 674 * wants to make. This command clears the SCI bit and also the 675 * interrupt source since we are edge-triggered. To prevent the GPE 676 * that may arise from running the query from causing another query 677 * to be queued, we clear the pending flag only after running it. 678 */ 679 sci_enqueued = sc->ec_sci_pend; 680 for (retry = 0; retry < 2; retry++) { 681 Status = EcCommand(sc, EC_COMMAND_QUERY); 682 if (ACPI_SUCCESS(Status)) 683 break; 684 if (ACPI_SUCCESS(EcCheckStatus(sc, "retr_check", 685 EC_EVENT_INPUT_BUFFER_EMPTY))) 686 continue; 687 else 688 break; 689 } 690 sc->ec_sci_pend = FALSE; 691 if (ACPI_FAILURE(Status)) { 692 EcUnlock(sc); 693 device_printf(sc->ec_dev, "GPE query failed: %s\n", 694 AcpiFormatException(Status)); 695 return; 696 } 697 Data = EC_GET_DATA(sc); 698 699 /* 700 * We have to unlock before running the _Qxx method below since that 701 * method may attempt to read/write from EC address space, causing 702 * recursive acquisition of the lock. 703 */ 704 EcUnlock(sc); 705 706 /* Ignore the value for "no outstanding event". (13.3.5) */ 707 if (Data == 0) { 708 KTR_LOG(acpi_ec_qryoknotrun, Data); 709 return; 710 } else { 711 KTR_LOG(acpi_ec_qryokrun, Data); 712 } 713 714 /* Evaluate _Qxx to respond to the controller. */ 715 ksnprintf(qxx, sizeof(qxx), "_Q%02X", Data); 716 AcpiUtStrupr(qxx); 717 Status = AcpiEvaluateObject(sc->ec_handle, qxx, NULL, NULL); 718 if (ACPI_FAILURE(Status) && Status != AE_NOT_FOUND) { 719 device_printf(sc->ec_dev, "evaluation of query method %s failed: %s\n", 720 qxx, AcpiFormatException(Status)); 721 } 722 723 /* Reenable runtime GPE if its execution was deferred. */ 724 if (sci_enqueued) { 725 Status = AcpiFinishGpe(sc->ec_gpehandle, sc->ec_gpebit); 726 if (ACPI_FAILURE(Status)) 727 device_printf(sc->ec_dev, "reenabling runtime GPE failed: %s\n", 728 AcpiFormatException(Status)); 729 } 730 } 731 732 /* 733 * The GPE handler is called when IBE/OBF or SCI events occur. We are 734 * called from an unknown lock context. 735 */ 736 static UINT32 737 EcGpeHandler(ACPI_HANDLE GpeDevice, UINT32 GpeNumber, void *Context) 738 { 739 struct acpi_ec_softc *sc = Context; 740 ACPI_STATUS Status; 741 EC_STATUS EcStatus; 742 743 KASSERT(Context != NULL, ("EcGpeHandler called with NULL")); 744 KTR_LOG(acpi_ec_gpehdlstart); 745 /* 746 * Notify EcWaitEvent() that the status register is now fresh. If we 747 * didn't do this, it wouldn't be possible to distinguish an old IBE 748 * from a new one, for example when doing a write transaction (writing 749 * address and then data values.) 750 */ 751 atomic_add_int(&sc->ec_gencount, 1); 752 wakeup(sc); 753 754 /* 755 * If the EC_SCI bit of the status register is set, queue a query handler. 756 * It will run the query and _Qxx method later, under the lock. 757 */ 758 EcStatus = EC_GET_CSR(sc); 759 if ((EcStatus & EC_EVENT_SCI) && !sc->ec_sci_pend) { 760 KTR_LOG(acpi_ec_gpequeuehdl); 761 Status = AcpiOsExecute(OSL_GPE_HANDLER, EcGpeQueryHandler, Context); 762 if (ACPI_SUCCESS(Status)) { 763 sc->ec_sci_pend = TRUE; 764 return (0); 765 } else { 766 kprintf("EcGpeHandler: queuing GPE query handler failed\n"); 767 } 768 } 769 return (ACPI_REENABLE_GPE); 770 } 771 772 static ACPI_STATUS 773 EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function, void *Context, 774 void **RegionContext) 775 { 776 777 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 778 779 /* 780 * If deactivating a region, always set the output to NULL. Otherwise, 781 * just pass the context through. 782 */ 783 if (Function == ACPI_REGION_DEACTIVATE) 784 *RegionContext = NULL; 785 else 786 *RegionContext = Context; 787 788 return_ACPI_STATUS (AE_OK); 789 } 790 791 static ACPI_STATUS 792 EcSpaceHandler(UINT32 Function, ACPI_PHYSICAL_ADDRESS Address, UINT32 Width, 793 UINT64 *Value, void *Context, void *RegionContext) 794 { 795 struct acpi_ec_softc *sc = (struct acpi_ec_softc *)Context; 796 ACPI_PHYSICAL_ADDRESS EcAddr; 797 UINT8 *EcData; 798 ACPI_STATUS Status; 799 800 ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, (UINT32)Address); 801 802 if (Function != ACPI_READ && Function != ACPI_WRITE) 803 return_ACPI_STATUS (AE_BAD_PARAMETER); 804 if (Width % 8 != 0 || Value == NULL || Context == NULL) 805 return_ACPI_STATUS (AE_BAD_PARAMETER); 806 if (Address + Width / 8 > 256) 807 return_ACPI_STATUS (AE_BAD_ADDRESS); 808 809 /* 810 * If booting, check if we need to run the query handler. If so, we 811 * we call it directly here since our thread taskq is not active yet. 812 */ 813 if (cold || rebooting || sc->ec_suspending) { 814 if ((EC_GET_CSR(sc) & EC_EVENT_SCI)) { 815 KTR_LOG(acpi_ec_gperun); 816 EcGpeQueryHandler(sc); 817 } 818 } 819 820 /* Serialize with EcGpeQueryHandler() at transaction granularity. */ 821 Status = EcLock(sc); 822 if (ACPI_FAILURE(Status)) 823 return_ACPI_STATUS (Status); 824 825 /* If we can't start burst mode, continue anyway. */ 826 Status = EcCommand(sc, EC_COMMAND_BURST_ENABLE); 827 if (ACPI_SUCCESS(Status)) { 828 if (EC_GET_DATA(sc) == EC_BURST_ACK) { 829 KTR_LOG(acpi_ec_burstenl); 830 sc->ec_burstactive = TRUE; 831 } 832 } 833 834 /* Perform the transaction(s), based on Width. */ 835 EcAddr = Address; 836 EcData = (UINT8 *)Value; 837 if (Function == ACPI_READ) 838 *Value = 0; 839 do { 840 switch (Function) { 841 case ACPI_READ: 842 Status = EcRead(sc, EcAddr, EcData); 843 break; 844 case ACPI_WRITE: 845 Status = EcWrite(sc, EcAddr, *EcData); 846 break; 847 } 848 if (ACPI_FAILURE(Status)) 849 break; 850 EcAddr++; 851 EcData++; 852 } while (EcAddr < Address + Width / 8); 853 854 if (sc->ec_burstactive) { 855 sc->ec_burstactive = FALSE; 856 if (ACPI_SUCCESS(EcCommand(sc, EC_COMMAND_BURST_DISABLE))) 857 KTR_LOG(acpi_ec_burstdisok); 858 } 859 860 EcUnlock(sc); 861 return_ACPI_STATUS (Status); 862 } 863 864 static ACPI_STATUS 865 EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event, u_int gen_count) 866 { 867 static int no_intr = 0; 868 ACPI_STATUS Status; 869 int count, i, need_poll, slp_ival; 870 871 ACPI_SERIAL_ASSERT(ec); 872 Status = AE_NO_HARDWARE_RESPONSE; 873 need_poll = cold || rebooting || ec_polled_mode || sc->ec_suspending; 874 875 /* Wait for event by polling or GPE (interrupt). */ 876 if (need_poll) { 877 count = (ec_timeout * 1000) / EC_POLL_DELAY; 878 if (count == 0) 879 count = 1; 880 DELAY(10); 881 for (i = 0; i < count; i++) { 882 Status = EcCheckStatus(sc, "poll", Event); 883 if (ACPI_SUCCESS(Status)) 884 break; 885 DELAY(EC_POLL_DELAY); 886 } 887 } else { 888 slp_ival = hz / 1000; 889 if (slp_ival != 0) { 890 count = ec_timeout; 891 } else { 892 /* hz has less than 1 ms resolution so scale timeout. */ 893 slp_ival = 1; 894 count = ec_timeout / (1000 / hz); 895 } 896 897 /* 898 * Wait for the GPE to signal the status changed, checking the 899 * status register each time we get one. It's possible to get a 900 * GPE for an event we're not interested in here (i.e., SCI for 901 * EC query). 902 */ 903 for (i = 0; i < count; i++) { 904 if (gen_count == sc->ec_gencount) 905 tsleep(sc, 0, "ecgpe", slp_ival); 906 /* 907 * Record new generation count. It's possible the GPE was 908 * just to notify us that a query is needed and we need to 909 * wait for a second GPE to signal the completion of the 910 * event we are actually waiting for. 911 */ 912 Status = EcCheckStatus(sc, "sleep", Event); 913 if (ACPI_SUCCESS(Status)) { 914 if (gen_count == sc->ec_gencount) 915 no_intr++; 916 else 917 no_intr = 0; 918 break; 919 } 920 gen_count = sc->ec_gencount; 921 } 922 923 /* 924 * We finished waiting for the GPE and it never arrived. Try to 925 * read the register once and trust whatever value we got. This is 926 * the best we can do at this point. 927 */ 928 if (ACPI_FAILURE(Status)) 929 Status = EcCheckStatus(sc, "sleep_end", Event); 930 } 931 if (!need_poll && no_intr > 10) { 932 device_printf(sc->ec_dev, 933 "not getting interrupts, switched to polled mode\n"); 934 ec_polled_mode = 1; 935 } 936 if (ACPI_FAILURE(Status)) 937 KTR_LOG(acpi_ec_timeout); 938 return (Status); 939 } 940 941 static ACPI_STATUS 942 EcCommand(struct acpi_ec_softc *sc, EC_COMMAND cmd) 943 { 944 ACPI_STATUS status; 945 EC_EVENT event; 946 EC_STATUS ec_status; 947 u_int gen_count; 948 949 ACPI_SERIAL_ASSERT(ec); 950 951 /* Don't use burst mode if user disabled it. */ 952 if (!ec_burst_mode && cmd == EC_COMMAND_BURST_ENABLE) 953 return (AE_ERROR); 954 955 /* Decide what to wait for based on command type. */ 956 switch (cmd) { 957 case EC_COMMAND_READ: 958 case EC_COMMAND_WRITE: 959 case EC_COMMAND_BURST_DISABLE: 960 event = EC_EVENT_INPUT_BUFFER_EMPTY; 961 break; 962 case EC_COMMAND_QUERY: 963 case EC_COMMAND_BURST_ENABLE: 964 event = EC_EVENT_OUTPUT_BUFFER_FULL; 965 break; 966 default: 967 device_printf(sc->ec_dev, "EcCommand: invalid command %#x\n", cmd); 968 return (AE_BAD_PARAMETER); 969 } 970 971 /* 972 * Ensure empty input buffer before issuing command. 973 * Use generation count of zero to force a quick check. 974 */ 975 status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY, 0); 976 if (ACPI_FAILURE(status)) 977 return (status); 978 979 /* Run the command and wait for the chosen event. */ 980 KTR_LOG(acpi_ec_cmdrun, cmd); 981 gen_count = sc->ec_gencount; 982 EC_SET_CSR(sc, cmd); 983 status = EcWaitEvent(sc, event, gen_count); 984 if (ACPI_SUCCESS(status)) { 985 /* If we succeeded, burst flag should now be present. */ 986 if (cmd == EC_COMMAND_BURST_ENABLE) { 987 ec_status = EC_GET_CSR(sc); 988 if ((ec_status & EC_FLAG_BURST_MODE) == 0) 989 status = AE_ERROR; 990 } 991 } else 992 device_printf(sc->ec_dev, "EcCommand: no response to %#x\n", cmd); 993 return (status); 994 } 995 996 static ACPI_STATUS 997 EcRead(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data) 998 { 999 ACPI_STATUS status; 1000 u_int gen_count; 1001 int retry; 1002 1003 ACPI_SERIAL_ASSERT(ec); 1004 KTR_LOG(acpi_ec_readaddr, Address); 1005 1006 for (retry = 0; retry < 2; retry++) { 1007 status = EcCommand(sc, EC_COMMAND_READ); 1008 if (ACPI_FAILURE(status)) 1009 return (status); 1010 1011 gen_count = sc->ec_gencount; 1012 EC_SET_DATA(sc, Address); 1013 status = EcWaitEvent(sc, EC_EVENT_OUTPUT_BUFFER_FULL, gen_count); 1014 if (ACPI_FAILURE(status)) { 1015 if (ACPI_SUCCESS(EcCheckStatus(sc, "retr_check", 1016 EC_EVENT_INPUT_BUFFER_EMPTY))) 1017 continue; 1018 else 1019 break; 1020 } 1021 *Data = EC_GET_DATA(sc); 1022 return (AE_OK); 1023 } 1024 device_printf(sc->ec_dev, "EcRead: failed waiting to get data\n"); 1025 return (status); 1026 } 1027 1028 static ACPI_STATUS 1029 EcWrite(struct acpi_ec_softc *sc, UINT8 Address, UINT8 Data) 1030 { 1031 ACPI_STATUS status; 1032 u_int gen_count; 1033 1034 ACPI_SERIAL_ASSERT(ec); 1035 KTR_LOG(acpi_ec_writeaddr, Address, Data); 1036 1037 status = EcCommand(sc, EC_COMMAND_WRITE); 1038 if (ACPI_FAILURE(status)) 1039 return (status); 1040 1041 gen_count = sc->ec_gencount; 1042 EC_SET_DATA(sc, Address); 1043 status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY, gen_count); 1044 if (ACPI_FAILURE(status)) { 1045 device_printf(sc->ec_dev, "EcWrite: failed waiting for sent address\n"); 1046 return (status); 1047 } 1048 1049 gen_count = sc->ec_gencount; 1050 EC_SET_DATA(sc, Data); 1051 status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY, gen_count); 1052 if (ACPI_FAILURE(status)) { 1053 device_printf(sc->ec_dev, "EcWrite: failed waiting for sent data\n"); 1054 return (status); 1055 } 1056 1057 return (AE_OK); 1058 } 1059