1 /* $NetBSD: acpi_ec.c,v 1.65 2010/04/14 19:27:28 jruoho Exp $ */ 2 3 /*- 4 * Copyright (c) 2007 Joerg Sonnenberger <joerg@NetBSD.org>. 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 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 22 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 28 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 /* 33 * The ACPI Embedded Controller (EC) driver serves two different purposes: 34 * - read and write access from ASL, e.g. to read battery state 35 * - notification of ASL of System Control Interrupts. 36 * 37 * Access to the EC is serialised by sc_access_mtx and optionally the 38 * ACPI global mutex. Both locks are held until the request is fulfilled. 39 * All access to the softc has to hold sc_mtx to serialise against the GPE 40 * handler and the callout. sc_mtx is also used for wakeup conditions. 41 * 42 * SCIs are processed in a kernel thread. Handling gets a bit complicated 43 * by the lock order (sc_mtx must be acquired after sc_access_mtx and the 44 * ACPI global mutex). 45 * 46 * Read and write requests spin around for a short time as many requests 47 * can be handled instantly by the EC. During normal processing interrupt 48 * mode is used exclusively. At boot and resume time interrupts are not 49 * working and the handlers just busy loop. 50 * 51 * A callout is scheduled to compensate for missing interrupts on some 52 * hardware. If the EC doesn't process a request for 5s, it is most likely 53 * in a wedged state. No method to reset the EC is currently known. 54 * 55 * Special care has to be taken to not poll the EC in a busy loop without 56 * delay. This can prevent processing of Power Button events. At least some 57 * Lenovo Thinkpads seem to be implement the Power Button Override in the EC 58 * and the only option to recover on those models is to cut off all power. 59 */ 60 61 #include <sys/cdefs.h> 62 __KERNEL_RCSID(0, "$NetBSD: acpi_ec.c,v 1.65 2010/04/14 19:27:28 jruoho Exp $"); 63 64 #include <sys/param.h> 65 #include <sys/callout.h> 66 #include <sys/condvar.h> 67 #include <sys/device.h> 68 #include <sys/kernel.h> 69 #include <sys/kthread.h> 70 #include <sys/mutex.h> 71 #include <sys/systm.h> 72 73 #include <dev/acpi/acpireg.h> 74 #include <dev/acpi/acpivar.h> 75 #include <dev/acpi/acpi_ecvar.h> 76 77 #define _COMPONENT ACPI_EC_COMPONENT 78 ACPI_MODULE_NAME ("acpi_ec") 79 80 /* Maximum time to wait for global ACPI lock in ms */ 81 #define EC_LOCK_TIMEOUT 5 82 83 /* Maximum time to poll for completion of a command in ms */ 84 #define EC_POLL_TIMEOUT 5 85 86 /* Maximum time to give a single EC command in s */ 87 #define EC_CMD_TIMEOUT 10 88 89 /* From ACPI 3.0b, chapter 12.3 */ 90 #define EC_COMMAND_READ 0x80 91 #define EC_COMMAND_WRITE 0x81 92 #define EC_COMMAND_BURST_EN 0x82 93 #define EC_COMMAND_BURST_DIS 0x83 94 #define EC_COMMAND_QUERY 0x84 95 96 /* From ACPI 3.0b, chapter 12.2.1 */ 97 #define EC_STATUS_OBF 0x01 98 #define EC_STATUS_IBF 0x02 99 #define EC_STATUS_CMD 0x08 100 #define EC_STATUS_BURST 0x10 101 #define EC_STATUS_SCI 0x20 102 #define EC_STATUS_SMI 0x40 103 104 static const char *ec_hid[] = { 105 "PNP0C09", 106 NULL, 107 }; 108 109 enum ec_state_t { 110 EC_STATE_QUERY, 111 EC_STATE_QUERY_VAL, 112 EC_STATE_READ, 113 EC_STATE_READ_ADDR, 114 EC_STATE_READ_VAL, 115 EC_STATE_WRITE, 116 EC_STATE_WRITE_ADDR, 117 EC_STATE_WRITE_VAL, 118 EC_STATE_FREE 119 }; 120 121 struct acpiec_softc { 122 ACPI_HANDLE sc_ech; 123 124 ACPI_HANDLE sc_gpeh; 125 uint8_t sc_gpebit; 126 127 bus_space_tag_t sc_data_st; 128 bus_space_handle_t sc_data_sh; 129 130 bus_space_tag_t sc_csr_st; 131 bus_space_handle_t sc_csr_sh; 132 133 bool sc_need_global_lock; 134 uint32_t sc_global_lock; 135 136 kmutex_t sc_mtx, sc_access_mtx; 137 kcondvar_t sc_cv, sc_cv_sci; 138 enum ec_state_t sc_state; 139 bool sc_got_sci; 140 callout_t sc_pseudo_intr; 141 142 uint8_t sc_cur_addr, sc_cur_val; 143 }; 144 145 static int acpiecdt_match(device_t, cfdata_t, void *); 146 static void acpiecdt_attach(device_t, device_t, void *); 147 148 static int acpiec_match(device_t, cfdata_t, void *); 149 static void acpiec_attach(device_t, device_t, void *); 150 151 static void acpiec_common_attach(device_t, device_t, ACPI_HANDLE, 152 bus_space_tag_t, bus_addr_t, bus_space_tag_t, bus_addr_t, 153 ACPI_HANDLE, uint8_t); 154 155 static bool acpiec_suspend(device_t, const pmf_qual_t *); 156 static bool acpiec_resume(device_t, const pmf_qual_t *); 157 static bool acpiec_shutdown(device_t, int); 158 159 static bool acpiec_parse_gpe_package(device_t, ACPI_HANDLE, 160 ACPI_HANDLE *, uint8_t *); 161 162 static void acpiec_callout(void *); 163 static void acpiec_gpe_query(void *); 164 static uint32_t acpiec_gpe_handler(void *); 165 static ACPI_STATUS acpiec_space_setup(ACPI_HANDLE, uint32_t, void *, void **); 166 static ACPI_STATUS acpiec_space_handler(uint32_t, ACPI_PHYSICAL_ADDRESS, 167 uint32_t, ACPI_INTEGER *, void *, void *); 168 169 static void acpiec_gpe_state_machine(device_t); 170 171 CFATTACH_DECL_NEW(acpiec, sizeof(struct acpiec_softc), 172 acpiec_match, acpiec_attach, NULL, NULL); 173 174 CFATTACH_DECL_NEW(acpiecdt, sizeof(struct acpiec_softc), 175 acpiecdt_match, acpiecdt_attach, NULL, NULL); 176 177 static device_t ec_singleton = NULL; 178 static bool acpiec_cold = false; 179 180 static bool 181 acpiecdt_find(device_t parent, ACPI_HANDLE *ec_handle, 182 bus_addr_t *cmd_reg, bus_addr_t *data_reg, uint8_t *gpebit) 183 { 184 ACPI_TABLE_ECDT *ecdt; 185 ACPI_STATUS rv; 186 187 rv = AcpiGetTable(ACPI_SIG_ECDT, 1, (ACPI_TABLE_HEADER **)&ecdt); 188 if (ACPI_FAILURE(rv)) 189 return false; 190 191 if (ecdt->Control.BitWidth != 8 || ecdt->Data.BitWidth != 8) { 192 aprint_error_dev(parent, 193 "ECDT register width invalid (%u/%u)\n", 194 ecdt->Control.BitWidth, ecdt->Data.BitWidth); 195 return false; 196 } 197 198 rv = AcpiGetHandle(ACPI_ROOT_OBJECT, ecdt->Id, ec_handle); 199 if (ACPI_FAILURE(rv)) { 200 aprint_error_dev(parent, 201 "failed to look up EC object %s: %s\n", 202 ecdt->Id, AcpiFormatException(rv)); 203 return false; 204 } 205 206 *cmd_reg = ecdt->Control.Address; 207 *data_reg = ecdt->Data.Address; 208 *gpebit = ecdt->Gpe; 209 210 return true; 211 } 212 213 static int 214 acpiecdt_match(device_t parent, cfdata_t match, void *aux) 215 { 216 ACPI_HANDLE ec_handle; 217 bus_addr_t cmd_reg, data_reg; 218 uint8_t gpebit; 219 220 if (acpiecdt_find(parent, &ec_handle, &cmd_reg, &data_reg, &gpebit)) 221 return 1; 222 else 223 return 0; 224 } 225 226 static void 227 acpiecdt_attach(device_t parent, device_t self, void *aux) 228 { 229 struct acpibus_attach_args *aa = aux; 230 ACPI_HANDLE ec_handle; 231 bus_addr_t cmd_reg, data_reg; 232 uint8_t gpebit; 233 234 if (!acpiecdt_find(parent, &ec_handle, &cmd_reg, &data_reg, &gpebit)) 235 panic("ECDT disappeared"); 236 237 aprint_naive("\n"); 238 aprint_normal(": ACPI Embedded Controller via ECDT\n"); 239 240 acpiec_common_attach(parent, self, ec_handle, aa->aa_iot, cmd_reg, 241 aa->aa_iot, data_reg, NULL, gpebit); 242 } 243 244 static int 245 acpiec_match(device_t parent, cfdata_t match, void *aux) 246 { 247 struct acpi_attach_args *aa = aux; 248 249 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE) 250 return 0; 251 252 return acpi_match_hid(aa->aa_node->ad_devinfo, ec_hid); 253 } 254 255 static void 256 acpiec_attach(device_t parent, device_t self, void *aux) 257 { 258 struct acpi_attach_args *aa = aux; 259 struct acpi_resources ec_res; 260 struct acpi_io *io0, *io1; 261 ACPI_HANDLE gpe_handle; 262 uint8_t gpebit; 263 ACPI_STATUS rv; 264 265 if (ec_singleton != NULL) { 266 aprint_naive(": using %s\n", device_xname(ec_singleton)); 267 aprint_normal(": using %s\n", device_xname(ec_singleton)); 268 if (!pmf_device_register(self, NULL, NULL)) 269 aprint_error_dev(self, "couldn't establish power handler\n"); 270 return; 271 } 272 273 if (!acpiec_parse_gpe_package(self, aa->aa_node->ad_handle, 274 &gpe_handle, &gpebit)) 275 return; 276 277 rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS", 278 &ec_res, &acpi_resource_parse_ops_default); 279 if (rv != AE_OK) { 280 aprint_error_dev(self, "resource parsing failed: %s\n", 281 AcpiFormatException(rv)); 282 return; 283 } 284 285 if ((io0 = acpi_res_io(&ec_res, 0)) == NULL) { 286 aprint_error_dev(self, "no data register resource\n"); 287 goto free_res; 288 } 289 if ((io1 = acpi_res_io(&ec_res, 1)) == NULL) { 290 aprint_error_dev(self, "no CSR register resource\n"); 291 goto free_res; 292 } 293 294 acpiec_common_attach(parent, self, aa->aa_node->ad_handle, 295 aa->aa_iot, io1->ar_base, aa->aa_iot, io0->ar_base, 296 gpe_handle, gpebit); 297 298 free_res: 299 acpi_resource_cleanup(&ec_res); 300 } 301 302 static void 303 acpiec_common_attach(device_t parent, device_t self, 304 ACPI_HANDLE ec_handle, bus_space_tag_t cmdt, bus_addr_t cmd_reg, 305 bus_space_tag_t datat, bus_addr_t data_reg, 306 ACPI_HANDLE gpe_handle, uint8_t gpebit) 307 { 308 struct acpiec_softc *sc = device_private(self); 309 ACPI_STATUS rv; 310 ACPI_INTEGER val; 311 312 sc->sc_csr_st = cmdt; 313 sc->sc_data_st = datat; 314 315 sc->sc_ech = ec_handle; 316 sc->sc_gpeh = gpe_handle; 317 sc->sc_gpebit = gpebit; 318 319 sc->sc_state = EC_STATE_FREE; 320 mutex_init(&sc->sc_mtx, MUTEX_DRIVER, IPL_TTY); 321 mutex_init(&sc->sc_access_mtx, MUTEX_DEFAULT, IPL_NONE); 322 cv_init(&sc->sc_cv, "eccv"); 323 cv_init(&sc->sc_cv_sci, "ecsci"); 324 325 if (bus_space_map(sc->sc_data_st, data_reg, 1, 0, 326 &sc->sc_data_sh) != 0) { 327 aprint_error_dev(self, "unable to map data register\n"); 328 return; 329 } 330 331 if (bus_space_map(sc->sc_csr_st, cmd_reg, 1, 0, &sc->sc_csr_sh) != 0) { 332 aprint_error_dev(self, "unable to map CSR register\n"); 333 goto post_data_map; 334 } 335 336 rv = acpi_eval_integer(sc->sc_ech, "_GLK", &val); 337 if (rv == AE_OK) { 338 sc->sc_need_global_lock = val != 0; 339 } else if (rv != AE_NOT_FOUND) { 340 aprint_error_dev(self, "unable to evaluate _GLK: %s\n", 341 AcpiFormatException(rv)); 342 goto post_csr_map; 343 } else { 344 sc->sc_need_global_lock = false; 345 } 346 if (sc->sc_need_global_lock) 347 aprint_normal_dev(self, "using global ACPI lock\n"); 348 349 callout_init(&sc->sc_pseudo_intr, CALLOUT_MPSAFE); 350 callout_setfunc(&sc->sc_pseudo_intr, acpiec_callout, self); 351 352 rv = AcpiInstallAddressSpaceHandler(sc->sc_ech, ACPI_ADR_SPACE_EC, 353 acpiec_space_handler, acpiec_space_setup, self); 354 if (rv != AE_OK) { 355 aprint_error_dev(self, 356 "unable to install address space handler: %s\n", 357 AcpiFormatException(rv)); 358 goto post_csr_map; 359 } 360 361 rv = AcpiInstallGpeHandler(sc->sc_gpeh, sc->sc_gpebit, 362 ACPI_GPE_EDGE_TRIGGERED, acpiec_gpe_handler, self); 363 if (rv != AE_OK) { 364 aprint_error_dev(self, "unable to install GPE handler: %s\n", 365 AcpiFormatException(rv)); 366 goto post_csr_map; 367 } 368 369 rv = AcpiSetGpeType(sc->sc_gpeh, sc->sc_gpebit, ACPI_GPE_TYPE_RUNTIME); 370 if (rv != AE_OK) { 371 aprint_error_dev(self, "unable to set GPE type: %s\n", 372 AcpiFormatException(rv)); 373 goto post_csr_map; 374 } 375 376 rv = AcpiEnableGpe(sc->sc_gpeh, sc->sc_gpebit, ACPI_ISR); 377 if (rv != AE_OK) { 378 aprint_error_dev(self, "unable to enable GPE: %s\n", 379 AcpiFormatException(rv)); 380 goto post_csr_map; 381 } 382 383 if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL, acpiec_gpe_query, 384 self, NULL, "acpiec sci thread")) { 385 aprint_error_dev(self, "unable to create query kthread\n"); 386 goto post_csr_map; 387 } 388 389 ec_singleton = self; 390 391 if (!pmf_device_register1(self, acpiec_suspend, acpiec_resume, 392 acpiec_shutdown)) 393 aprint_error_dev(self, "couldn't establish power handler\n"); 394 395 return; 396 397 post_csr_map: 398 (void)AcpiRemoveGpeHandler(sc->sc_gpeh, sc->sc_gpebit, 399 acpiec_gpe_handler); 400 (void)AcpiRemoveAddressSpaceHandler(sc->sc_ech, 401 ACPI_ADR_SPACE_EC, acpiec_space_handler); 402 bus_space_unmap(sc->sc_csr_st, sc->sc_csr_sh, 1); 403 post_data_map: 404 bus_space_unmap(sc->sc_data_st, sc->sc_data_sh, 1); 405 } 406 407 static bool 408 acpiec_suspend(device_t dv, const pmf_qual_t *qual) 409 { 410 acpiec_cold = true; 411 412 return true; 413 } 414 415 static bool 416 acpiec_resume(device_t dv, const pmf_qual_t *qual) 417 { 418 acpiec_cold = false; 419 420 return true; 421 } 422 423 static bool 424 acpiec_shutdown(device_t dv, int how) 425 { 426 427 acpiec_cold = true; 428 return true; 429 } 430 431 static bool 432 acpiec_parse_gpe_package(device_t self, ACPI_HANDLE ec_handle, 433 ACPI_HANDLE *gpe_handle, uint8_t *gpebit) 434 { 435 ACPI_BUFFER buf; 436 ACPI_OBJECT *p, *c; 437 ACPI_STATUS rv; 438 439 rv = acpi_eval_struct(ec_handle, "_GPE", &buf); 440 if (rv != AE_OK) { 441 aprint_error_dev(self, "unable to evaluate _GPE: %s\n", 442 AcpiFormatException(rv)); 443 return false; 444 } 445 446 p = buf.Pointer; 447 448 if (p->Type == ACPI_TYPE_INTEGER) { 449 *gpe_handle = NULL; 450 *gpebit = p->Integer.Value; 451 ACPI_FREE(p); 452 return true; 453 } 454 455 if (p->Type != ACPI_TYPE_PACKAGE) { 456 aprint_error_dev(self, "_GPE is neither integer nor package\n"); 457 ACPI_FREE(p); 458 return false; 459 } 460 461 if (p->Package.Count != 2) { 462 aprint_error_dev(self, "_GPE package does not contain 2 elements\n"); 463 ACPI_FREE(p); 464 return false; 465 } 466 467 c = &p->Package.Elements[0]; 468 rv = acpi_eval_reference_handle(c, gpe_handle); 469 470 if (ACPI_FAILURE(rv)) { 471 aprint_error_dev(self, "failed to evaluate _GPE handle\n"); 472 ACPI_FREE(p); 473 return false; 474 } 475 476 c = &p->Package.Elements[1]; 477 478 if (c->Type != ACPI_TYPE_INTEGER) { 479 aprint_error_dev(self, 480 "_GPE package needs integer as 2nd field\n"); 481 ACPI_FREE(p); 482 return false; 483 } 484 *gpebit = c->Integer.Value; 485 ACPI_FREE(p); 486 return true; 487 } 488 489 static uint8_t 490 acpiec_read_data(struct acpiec_softc *sc) 491 { 492 return bus_space_read_1(sc->sc_data_st, sc->sc_data_sh, 0); 493 } 494 495 static void 496 acpiec_write_data(struct acpiec_softc *sc, uint8_t val) 497 { 498 bus_space_write_1(sc->sc_data_st, sc->sc_data_sh, 0, val); 499 } 500 501 static uint8_t 502 acpiec_read_status(struct acpiec_softc *sc) 503 { 504 return bus_space_read_1(sc->sc_csr_st, sc->sc_csr_sh, 0); 505 } 506 507 static void 508 acpiec_write_command(struct acpiec_softc *sc, uint8_t cmd) 509 { 510 bus_space_write_1(sc->sc_csr_st, sc->sc_csr_sh, 0, cmd); 511 } 512 513 static ACPI_STATUS 514 acpiec_space_setup(ACPI_HANDLE region, uint32_t func, void *arg, 515 void **region_arg) 516 { 517 if (func == ACPI_REGION_DEACTIVATE) 518 *region_arg = NULL; 519 else 520 *region_arg = arg; 521 522 return AE_OK; 523 } 524 525 static void 526 acpiec_lock(device_t dv) 527 { 528 struct acpiec_softc *sc = device_private(dv); 529 ACPI_STATUS rv; 530 531 mutex_enter(&sc->sc_access_mtx); 532 533 if (sc->sc_need_global_lock) { 534 rv = AcpiAcquireGlobalLock(EC_LOCK_TIMEOUT, &sc->sc_global_lock); 535 if (rv != AE_OK) { 536 aprint_error_dev(dv, "failed to acquire global lock: %s\n", 537 AcpiFormatException(rv)); 538 return; 539 } 540 } 541 } 542 543 static void 544 acpiec_unlock(device_t dv) 545 { 546 struct acpiec_softc *sc = device_private(dv); 547 ACPI_STATUS rv; 548 549 if (sc->sc_need_global_lock) { 550 rv = AcpiReleaseGlobalLock(sc->sc_global_lock); 551 if (rv != AE_OK) { 552 aprint_error_dev(dv, "failed to release global lock: %s\n", 553 AcpiFormatException(rv)); 554 } 555 } 556 mutex_exit(&sc->sc_access_mtx); 557 } 558 559 static ACPI_STATUS 560 acpiec_read(device_t dv, uint8_t addr, uint8_t *val) 561 { 562 struct acpiec_softc *sc = device_private(dv); 563 int i, timeo = 1000 * EC_CMD_TIMEOUT; 564 565 acpiec_lock(dv); 566 mutex_enter(&sc->sc_mtx); 567 568 sc->sc_cur_addr = addr; 569 sc->sc_state = EC_STATE_READ; 570 571 for (i = 0; i < EC_POLL_TIMEOUT; ++i) { 572 acpiec_gpe_state_machine(dv); 573 if (sc->sc_state == EC_STATE_FREE) 574 goto done; 575 delay(1); 576 } 577 578 if (cold || acpiec_cold) { 579 while (sc->sc_state != EC_STATE_FREE && timeo-- > 0) { 580 delay(1000); 581 acpiec_gpe_state_machine(dv); 582 } 583 if (sc->sc_state != EC_STATE_FREE) { 584 mutex_exit(&sc->sc_mtx); 585 AcpiClearGpe(sc->sc_gpeh, sc->sc_gpebit, ACPI_NOT_ISR); 586 acpiec_unlock(dv); 587 aprint_error_dev(dv, "command timed out, state %d\n", 588 sc->sc_state); 589 return AE_ERROR; 590 } 591 } else if (cv_timedwait(&sc->sc_cv, &sc->sc_mtx, EC_CMD_TIMEOUT * hz)) { 592 mutex_exit(&sc->sc_mtx); 593 AcpiClearGpe(sc->sc_gpeh, sc->sc_gpebit, ACPI_NOT_ISR); 594 acpiec_unlock(dv); 595 aprint_error_dev(dv, "command takes over %d sec...\n", EC_CMD_TIMEOUT); 596 return AE_ERROR; 597 } 598 599 done: 600 *val = sc->sc_cur_val; 601 602 mutex_exit(&sc->sc_mtx); 603 acpiec_unlock(dv); 604 return AE_OK; 605 } 606 607 static ACPI_STATUS 608 acpiec_write(device_t dv, uint8_t addr, uint8_t val) 609 { 610 struct acpiec_softc *sc = device_private(dv); 611 int i, timeo = 1000 * EC_CMD_TIMEOUT; 612 613 acpiec_lock(dv); 614 mutex_enter(&sc->sc_mtx); 615 616 sc->sc_cur_addr = addr; 617 sc->sc_cur_val = val; 618 sc->sc_state = EC_STATE_WRITE; 619 620 for (i = 0; i < EC_POLL_TIMEOUT; ++i) { 621 acpiec_gpe_state_machine(dv); 622 if (sc->sc_state == EC_STATE_FREE) 623 goto done; 624 delay(1); 625 } 626 627 if (cold || acpiec_cold) { 628 while (sc->sc_state != EC_STATE_FREE && timeo-- > 0) { 629 delay(1000); 630 acpiec_gpe_state_machine(dv); 631 } 632 if (sc->sc_state != EC_STATE_FREE) { 633 mutex_exit(&sc->sc_mtx); 634 AcpiClearGpe(sc->sc_gpeh, sc->sc_gpebit, ACPI_NOT_ISR); 635 acpiec_unlock(dv); 636 aprint_error_dev(dv, "command timed out, state %d\n", 637 sc->sc_state); 638 return AE_ERROR; 639 } 640 } else if (cv_timedwait(&sc->sc_cv, &sc->sc_mtx, EC_CMD_TIMEOUT * hz)) { 641 mutex_exit(&sc->sc_mtx); 642 AcpiClearGpe(sc->sc_gpeh, sc->sc_gpebit, ACPI_NOT_ISR); 643 acpiec_unlock(dv); 644 aprint_error_dev(dv, "command takes over %d sec...\n", EC_CMD_TIMEOUT); 645 return AE_ERROR; 646 } 647 648 done: 649 mutex_exit(&sc->sc_mtx); 650 acpiec_unlock(dv); 651 return AE_OK; 652 } 653 654 static ACPI_STATUS 655 acpiec_space_handler(uint32_t func, ACPI_PHYSICAL_ADDRESS paddr, 656 uint32_t width, ACPI_INTEGER *value, void *arg, void *region_arg) 657 { 658 device_t dv; 659 struct acpiec_softc *sc; 660 ACPI_STATUS rv; 661 uint8_t addr, reg; 662 unsigned int i; 663 664 if (paddr > 0xff || width % 8 != 0 || value == NULL || arg == NULL || 665 paddr + width / 8 > 0xff) 666 return AE_BAD_PARAMETER; 667 668 addr = paddr; 669 dv = arg; 670 sc = device_private(dv); 671 672 rv = AE_OK; 673 674 switch (func) { 675 case ACPI_READ: 676 *value = 0; 677 for (i = 0; i < width; i += 8, ++addr) { 678 rv = acpiec_read(dv, addr, ®); 679 if (rv != AE_OK) 680 break; 681 *value |= (ACPI_INTEGER)reg << i; 682 } 683 break; 684 case ACPI_WRITE: 685 for (i = 0; i < width; i += 8, ++addr) { 686 reg = (*value >>i) & 0xff; 687 rv = acpiec_write(dv, addr, reg); 688 if (rv != AE_OK) 689 break; 690 } 691 break; 692 default: 693 aprint_error("%s: invalid Address Space function called: %x\n", 694 device_xname(dv), (unsigned int)func); 695 return AE_BAD_PARAMETER; 696 } 697 698 return rv; 699 } 700 701 static void 702 acpiec_gpe_query(void *arg) 703 { 704 device_t dv = arg; 705 struct acpiec_softc *sc = device_private(dv); 706 uint8_t reg; 707 char qxx[5]; 708 ACPI_STATUS rv; 709 int i; 710 711 loop: 712 mutex_enter(&sc->sc_mtx); 713 714 if (sc->sc_got_sci == false) 715 cv_wait(&sc->sc_cv_sci, &sc->sc_mtx); 716 mutex_exit(&sc->sc_mtx); 717 718 acpiec_lock(dv); 719 mutex_enter(&sc->sc_mtx); 720 721 /* The Query command can always be issued, so be defensive here. */ 722 sc->sc_got_sci = false; 723 sc->sc_state = EC_STATE_QUERY; 724 725 for (i = 0; i < EC_POLL_TIMEOUT; ++i) { 726 acpiec_gpe_state_machine(dv); 727 if (sc->sc_state == EC_STATE_FREE) 728 goto done; 729 delay(1); 730 } 731 732 cv_wait(&sc->sc_cv, &sc->sc_mtx); 733 734 done: 735 reg = sc->sc_cur_val; 736 737 mutex_exit(&sc->sc_mtx); 738 acpiec_unlock(dv); 739 740 if (reg == 0) 741 goto loop; /* Spurious query result */ 742 743 /* 744 * Evaluate _Qxx to respond to the controller. 745 */ 746 snprintf(qxx, sizeof(qxx), "_Q%02X", (unsigned int)reg); 747 rv = AcpiEvaluateObject(sc->sc_ech, qxx, NULL, NULL); 748 if (rv != AE_OK && rv != AE_NOT_FOUND) { 749 aprint_error("%s: GPE query method %s failed: %s", 750 device_xname(dv), qxx, AcpiFormatException(rv)); 751 } 752 753 goto loop; 754 } 755 756 static void 757 acpiec_gpe_state_machine(device_t dv) 758 { 759 struct acpiec_softc *sc = device_private(dv); 760 uint8_t reg; 761 762 reg = acpiec_read_status(sc); 763 764 if (reg & EC_STATUS_SCI) 765 sc->sc_got_sci = true; 766 767 switch (sc->sc_state) { 768 case EC_STATE_QUERY: 769 if ((reg & EC_STATUS_IBF) != 0) 770 break; /* Nothing of interest here. */ 771 acpiec_write_command(sc, EC_COMMAND_QUERY); 772 sc->sc_state = EC_STATE_QUERY_VAL; 773 break; 774 775 case EC_STATE_QUERY_VAL: 776 if ((reg & EC_STATUS_OBF) == 0) 777 break; /* Nothing of interest here. */ 778 779 sc->sc_cur_val = acpiec_read_data(sc); 780 sc->sc_state = EC_STATE_FREE; 781 782 cv_signal(&sc->sc_cv); 783 break; 784 785 case EC_STATE_READ: 786 if ((reg & EC_STATUS_IBF) != 0) 787 break; /* Nothing of interest here. */ 788 789 acpiec_write_command(sc, EC_COMMAND_READ); 790 sc->sc_state = EC_STATE_READ_ADDR; 791 break; 792 793 case EC_STATE_READ_ADDR: 794 if ((reg & EC_STATUS_IBF) != 0) 795 break; /* Nothing of interest here. */ 796 797 acpiec_write_data(sc, sc->sc_cur_addr); 798 sc->sc_state = EC_STATE_READ_VAL; 799 break; 800 801 case EC_STATE_READ_VAL: 802 if ((reg & EC_STATUS_OBF) == 0) 803 break; /* Nothing of interest here. */ 804 sc->sc_cur_val = acpiec_read_data(sc); 805 sc->sc_state = EC_STATE_FREE; 806 807 cv_signal(&sc->sc_cv); 808 break; 809 810 case EC_STATE_WRITE: 811 if ((reg & EC_STATUS_IBF) != 0) 812 break; /* Nothing of interest here. */ 813 814 acpiec_write_command(sc, EC_COMMAND_WRITE); 815 sc->sc_state = EC_STATE_WRITE_ADDR; 816 break; 817 818 case EC_STATE_WRITE_ADDR: 819 if ((reg & EC_STATUS_IBF) != 0) 820 break; /* Nothing of interest here. */ 821 acpiec_write_data(sc, sc->sc_cur_addr); 822 sc->sc_state = EC_STATE_WRITE_VAL; 823 break; 824 825 case EC_STATE_WRITE_VAL: 826 if ((reg & EC_STATUS_IBF) != 0) 827 break; /* Nothing of interest here. */ 828 sc->sc_state = EC_STATE_FREE; 829 cv_signal(&sc->sc_cv); 830 831 acpiec_write_data(sc, sc->sc_cur_val); 832 break; 833 834 case EC_STATE_FREE: 835 if (sc->sc_got_sci) 836 cv_signal(&sc->sc_cv_sci); 837 break; 838 default: 839 panic("invalid state"); 840 } 841 842 if (sc->sc_state != EC_STATE_FREE) 843 callout_schedule(&sc->sc_pseudo_intr, 1); 844 } 845 846 static void 847 acpiec_callout(void *arg) 848 { 849 device_t dv = arg; 850 struct acpiec_softc *sc = device_private(dv); 851 852 AcpiClearGpe(sc->sc_gpeh, sc->sc_gpebit, ACPI_NOT_ISR); 853 854 mutex_enter(&sc->sc_mtx); 855 acpiec_gpe_state_machine(dv); 856 mutex_exit(&sc->sc_mtx); 857 } 858 859 static uint32_t 860 acpiec_gpe_handler(void *arg) 861 { 862 device_t dv = arg; 863 struct acpiec_softc *sc = device_private(dv); 864 865 AcpiClearGpe(sc->sc_gpeh, sc->sc_gpebit, ACPI_ISR); 866 867 mutex_enter(&sc->sc_mtx); 868 acpiec_gpe_state_machine(dv); 869 mutex_exit(&sc->sc_mtx); 870 871 return 0; 872 } 873 874 ACPI_STATUS 875 acpiec_bus_read(device_t dv, u_int addr, ACPI_INTEGER *val, int width) 876 { 877 return acpiec_space_handler(ACPI_READ, addr, width * 8, val, dv, NULL); 878 } 879 880 ACPI_STATUS 881 acpiec_bus_write(device_t dv, u_int addr, ACPI_INTEGER val, int width) 882 { 883 return acpiec_space_handler(ACPI_WRITE, addr, width * 8, &val, dv, NULL); 884 } 885 886 ACPI_HANDLE 887 acpiec_get_handle(device_t dv) 888 { 889 struct acpiec_softc *sc = device_private(dv); 890 891 return sc->sc_ech; 892 } 893