1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2009 Nathan Whitehorn 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 ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 * 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 */ 29 30 #include <sys/cdefs.h> 31 #include <sys/param.h> 32 #include <sys/bus.h> 33 #include <sys/eventhandler.h> 34 #include <sys/systm.h> 35 #include <sys/module.h> 36 #include <sys/conf.h> 37 #include <sys/cpu.h> 38 #include <sys/clock.h> 39 #include <sys/ctype.h> 40 #include <sys/kernel.h> 41 #include <sys/kthread.h> 42 #include <sys/lock.h> 43 #include <sys/mutex.h> 44 #include <sys/reboot.h> 45 #include <sys/rman.h> 46 #include <sys/sysctl.h> 47 #include <sys/unistd.h> 48 49 #include <machine/bus.h> 50 #include <machine/intr_machdep.h> 51 #include <machine/md_var.h> 52 53 #include <dev/iicbus/iicbus.h> 54 #include <dev/iicbus/iiconf.h> 55 #include <dev/led/led.h> 56 #include <dev/ofw/openfirm.h> 57 #include <dev/ofw/ofw_bus.h> 58 #include <dev/ofw/ofw_bus_subr.h> 59 #include <powerpc/powermac/macgpiovar.h> 60 #include <powerpc/powermac/powermac_thermal.h> 61 62 #include "clock_if.h" 63 #include "iicbus_if.h" 64 65 struct smu_cmd { 66 volatile uint8_t cmd; 67 uint8_t len; 68 uint8_t data[254]; 69 70 STAILQ_ENTRY(smu_cmd) cmd_q; 71 }; 72 73 STAILQ_HEAD(smu_cmdq, smu_cmd); 74 75 struct smu_fan { 76 struct pmac_fan fan; 77 device_t dev; 78 cell_t reg; 79 80 enum { 81 SMU_FAN_RPM, 82 SMU_FAN_PWM 83 } type; 84 int setpoint; 85 int old_style; 86 int rpm; 87 }; 88 89 /* We can read the PWM and the RPM from a PWM controlled fan. 90 * Offer both values via sysctl. 91 */ 92 enum { 93 SMU_PWM_SYSCTL_PWM = 1 << 8, 94 SMU_PWM_SYSCTL_RPM = 2 << 8 95 }; 96 97 struct smu_sensor { 98 struct pmac_therm therm; 99 device_t dev; 100 101 cell_t reg; 102 enum { 103 SMU_CURRENT_SENSOR, 104 SMU_VOLTAGE_SENSOR, 105 SMU_POWER_SENSOR, 106 SMU_TEMP_SENSOR 107 } type; 108 }; 109 110 struct smu_softc { 111 device_t sc_dev; 112 struct mtx sc_mtx; 113 114 struct resource *sc_memr; 115 int sc_memrid; 116 int sc_u3; 117 118 bus_dma_tag_t sc_dmatag; 119 bus_space_tag_t sc_bt; 120 bus_space_handle_t sc_mailbox; 121 122 struct smu_cmd *sc_cmd, *sc_cur_cmd; 123 bus_addr_t sc_cmd_phys; 124 bus_dmamap_t sc_cmd_dmamap; 125 struct smu_cmdq sc_cmdq; 126 127 struct smu_fan *sc_fans; 128 int sc_nfans; 129 int old_style_fans; 130 struct smu_sensor *sc_sensors; 131 int sc_nsensors; 132 133 int sc_doorbellirqid; 134 struct resource *sc_doorbellirq; 135 void *sc_doorbellirqcookie; 136 137 struct proc *sc_fanmgt_proc; 138 time_t sc_lastuserchange; 139 140 /* Calibration data */ 141 uint16_t sc_cpu_diode_scale; 142 int16_t sc_cpu_diode_offset; 143 144 uint16_t sc_cpu_volt_scale; 145 int16_t sc_cpu_volt_offset; 146 uint16_t sc_cpu_curr_scale; 147 int16_t sc_cpu_curr_offset; 148 149 uint16_t sc_slots_pow_scale; 150 int16_t sc_slots_pow_offset; 151 152 struct cdev *sc_leddev; 153 }; 154 155 /* regular bus attachment functions */ 156 157 static int smu_probe(device_t); 158 static int smu_attach(device_t); 159 static const struct ofw_bus_devinfo * 160 smu_get_devinfo(device_t bus, device_t dev); 161 162 /* cpufreq notification hooks */ 163 164 static void smu_cpufreq_pre_change(device_t, const struct cf_level *level); 165 static void smu_cpufreq_post_change(device_t, const struct cf_level *level); 166 167 /* clock interface */ 168 static int smu_gettime(device_t dev, struct timespec *ts); 169 static int smu_settime(device_t dev, struct timespec *ts); 170 171 /* utility functions */ 172 static int smu_run_cmd(device_t dev, struct smu_cmd *cmd, int wait); 173 static int smu_get_datablock(device_t dev, int8_t id, uint8_t *buf, 174 size_t len); 175 static void smu_attach_i2c(device_t dev, phandle_t i2croot); 176 static void smu_attach_fans(device_t dev, phandle_t fanroot); 177 static void smu_attach_sensors(device_t dev, phandle_t sensroot); 178 static void smu_set_sleepled(void *xdev, int onoff); 179 static int smu_server_mode(SYSCTL_HANDLER_ARGS); 180 static void smu_doorbell_intr(void *xdev); 181 static void smu_shutdown(void *xdev, int howto); 182 183 /* where to find the doorbell GPIO */ 184 185 static device_t smu_doorbell = NULL; 186 187 static device_method_t smu_methods[] = { 188 /* Device interface */ 189 DEVMETHOD(device_probe, smu_probe), 190 DEVMETHOD(device_attach, smu_attach), 191 192 /* Clock interface */ 193 DEVMETHOD(clock_gettime, smu_gettime), 194 DEVMETHOD(clock_settime, smu_settime), 195 196 /* ofw_bus interface */ 197 DEVMETHOD(bus_child_pnpinfo, ofw_bus_gen_child_pnpinfo), 198 DEVMETHOD(ofw_bus_get_devinfo, smu_get_devinfo), 199 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat), 200 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model), 201 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name), 202 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node), 203 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type), 204 205 { 0, 0 }, 206 }; 207 208 static driver_t smu_driver = { 209 "smu", 210 smu_methods, 211 sizeof(struct smu_softc) 212 }; 213 214 DRIVER_MODULE(smu, ofwbus, smu_driver, 0, 0); 215 static MALLOC_DEFINE(M_SMU, "smu", "SMU Sensor Information"); 216 217 #define SMU_MAILBOX 0x8000860c 218 #define SMU_FANMGT_INTERVAL 1000 /* ms */ 219 220 /* Command types */ 221 #define SMU_ADC 0xd8 222 #define SMU_FAN 0x4a 223 #define SMU_RPM_STATUS 0x01 224 #define SMU_RPM_SETPOINT 0x02 225 #define SMU_PWM_STATUS 0x11 226 #define SMU_PWM_SETPOINT 0x12 227 #define SMU_I2C 0x9a 228 #define SMU_I2C_SIMPLE 0x00 229 #define SMU_I2C_NORMAL 0x01 230 #define SMU_I2C_COMBINED 0x02 231 #define SMU_MISC 0xee 232 #define SMU_MISC_GET_DATA 0x02 233 #define SMU_MISC_LED_CTRL 0x04 234 #define SMU_POWER 0xaa 235 #define SMU_POWER_EVENTS 0x8f 236 #define SMU_PWR_GET_POWERUP 0x00 237 #define SMU_PWR_SET_POWERUP 0x01 238 #define SMU_PWR_CLR_POWERUP 0x02 239 #define SMU_RTC 0x8e 240 #define SMU_RTC_GET 0x81 241 #define SMU_RTC_SET 0x80 242 243 /* Power event types */ 244 #define SMU_WAKEUP_KEYPRESS 0x01 245 #define SMU_WAKEUP_AC_INSERT 0x02 246 #define SMU_WAKEUP_AC_CHANGE 0x04 247 #define SMU_WAKEUP_RING 0x10 248 249 /* Data blocks */ 250 #define SMU_CPUTEMP_CAL 0x18 251 #define SMU_CPUVOLT_CAL 0x21 252 #define SMU_SLOTPW_CAL 0x78 253 254 /* Partitions */ 255 #define SMU_PARTITION 0x3e 256 #define SMU_PARTITION_LATEST 0x01 257 #define SMU_PARTITION_BASE 0x02 258 #define SMU_PARTITION_UPDATE 0x03 259 260 static int 261 smu_probe(device_t dev) 262 { 263 const char *name = ofw_bus_get_name(dev); 264 265 if (strcmp(name, "smu") != 0) 266 return (ENXIO); 267 268 device_set_desc(dev, "Apple System Management Unit"); 269 return (0); 270 } 271 272 static void 273 smu_phys_callback(void *xsc, bus_dma_segment_t *segs, int nsegs, int error) 274 { 275 struct smu_softc *sc = xsc; 276 277 sc->sc_cmd_phys = segs[0].ds_addr; 278 } 279 280 static int 281 smu_attach(device_t dev) 282 { 283 struct smu_softc *sc; 284 phandle_t node, child; 285 uint8_t data[12]; 286 287 sc = device_get_softc(dev); 288 289 mtx_init(&sc->sc_mtx, "smu", NULL, MTX_DEF); 290 sc->sc_cur_cmd = NULL; 291 sc->sc_doorbellirqid = -1; 292 293 sc->sc_u3 = 0; 294 if (OF_finddevice("/u3") != -1) 295 sc->sc_u3 = 1; 296 297 /* 298 * Map the mailbox area. This should be determined from firmware, 299 * but I have not found a simple way to do that. 300 */ 301 bus_dma_tag_create(NULL, 16, 0, BUS_SPACE_MAXADDR_32BIT, 302 BUS_SPACE_MAXADDR, NULL, NULL, PAGE_SIZE, 1, PAGE_SIZE, 0, NULL, 303 NULL, &(sc->sc_dmatag)); 304 sc->sc_bt = &bs_le_tag; 305 bus_space_map(sc->sc_bt, SMU_MAILBOX, 4, 0, &sc->sc_mailbox); 306 307 /* 308 * Allocate the command buffer. This can be anywhere in the low 4 GB 309 * of memory. 310 */ 311 bus_dmamem_alloc(sc->sc_dmatag, (void **)&sc->sc_cmd, BUS_DMA_WAITOK | 312 BUS_DMA_ZERO, &sc->sc_cmd_dmamap); 313 bus_dmamap_load(sc->sc_dmatag, sc->sc_cmd_dmamap, 314 sc->sc_cmd, PAGE_SIZE, smu_phys_callback, sc, 0); 315 STAILQ_INIT(&sc->sc_cmdq); 316 317 /* 318 * Set up handlers to change CPU voltage when CPU frequency is changed. 319 */ 320 EVENTHANDLER_REGISTER(cpufreq_pre_change, smu_cpufreq_pre_change, dev, 321 EVENTHANDLER_PRI_ANY); 322 EVENTHANDLER_REGISTER(cpufreq_post_change, smu_cpufreq_post_change, dev, 323 EVENTHANDLER_PRI_ANY); 324 325 node = ofw_bus_get_node(dev); 326 327 /* Some SMUs have RPM and PWM controlled fans which do not sit 328 * under the same node. So we have to attach them separately. 329 */ 330 smu_attach_fans(dev, node); 331 332 /* 333 * Now detect and attach the other child devices. 334 */ 335 for (child = OF_child(node); child != 0; child = OF_peer(child)) { 336 char name[32]; 337 memset(name, 0, sizeof(name)); 338 OF_getprop(child, "name", name, sizeof(name)); 339 340 if (strncmp(name, "sensors", 8) == 0) 341 smu_attach_sensors(dev, child); 342 343 if (strncmp(name, "smu-i2c-control", 15) == 0) 344 smu_attach_i2c(dev, child); 345 } 346 347 /* Some SMUs have the I2C children directly under the bus. */ 348 smu_attach_i2c(dev, node); 349 350 /* 351 * Collect calibration constants. 352 */ 353 smu_get_datablock(dev, SMU_CPUTEMP_CAL, data, sizeof(data)); 354 sc->sc_cpu_diode_scale = (data[4] << 8) + data[5]; 355 sc->sc_cpu_diode_offset = (data[6] << 8) + data[7]; 356 357 smu_get_datablock(dev, SMU_CPUVOLT_CAL, data, sizeof(data)); 358 sc->sc_cpu_volt_scale = (data[4] << 8) + data[5]; 359 sc->sc_cpu_volt_offset = (data[6] << 8) + data[7]; 360 sc->sc_cpu_curr_scale = (data[8] << 8) + data[9]; 361 sc->sc_cpu_curr_offset = (data[10] << 8) + data[11]; 362 363 smu_get_datablock(dev, SMU_SLOTPW_CAL, data, sizeof(data)); 364 sc->sc_slots_pow_scale = (data[4] << 8) + data[5]; 365 sc->sc_slots_pow_offset = (data[6] << 8) + data[7]; 366 367 /* 368 * Set up LED interface 369 */ 370 sc->sc_leddev = led_create(smu_set_sleepled, dev, "sleepled"); 371 372 /* 373 * Reset on power loss behavior 374 */ 375 376 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 377 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, 378 "server_mode", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, dev, 379 0, smu_server_mode, "I", "Enable reboot after power failure"); 380 381 /* 382 * Set up doorbell interrupt. 383 */ 384 sc->sc_doorbellirqid = 0; 385 sc->sc_doorbellirq = bus_alloc_resource_any(smu_doorbell, SYS_RES_IRQ, 386 &sc->sc_doorbellirqid, RF_ACTIVE); 387 bus_setup_intr(smu_doorbell, sc->sc_doorbellirq, 388 INTR_TYPE_MISC | INTR_MPSAFE, NULL, smu_doorbell_intr, dev, 389 &sc->sc_doorbellirqcookie); 390 powerpc_config_intr(rman_get_start(sc->sc_doorbellirq), 391 INTR_TRIGGER_EDGE, INTR_POLARITY_LOW); 392 393 /* 394 * Connect RTC interface. 395 */ 396 clock_register(dev, 1000); 397 398 /* 399 * Learn about shutdown events 400 */ 401 EVENTHANDLER_REGISTER(shutdown_final, smu_shutdown, dev, 402 SHUTDOWN_PRI_LAST); 403 404 return (bus_generic_attach(dev)); 405 } 406 407 static const struct ofw_bus_devinfo * 408 smu_get_devinfo(device_t bus, device_t dev) 409 { 410 411 return (device_get_ivars(dev)); 412 } 413 414 static void 415 smu_send_cmd(device_t dev, struct smu_cmd *cmd) 416 { 417 struct smu_softc *sc; 418 419 sc = device_get_softc(dev); 420 421 mtx_assert(&sc->sc_mtx, MA_OWNED); 422 423 if (sc->sc_u3) 424 powerpc_pow_enabled = 0; /* SMU cannot work if we go to NAP */ 425 426 sc->sc_cur_cmd = cmd; 427 428 /* Copy the command to the mailbox */ 429 sc->sc_cmd->cmd = cmd->cmd; 430 sc->sc_cmd->len = cmd->len; 431 memcpy(sc->sc_cmd->data, cmd->data, sizeof(cmd->data)); 432 bus_dmamap_sync(sc->sc_dmatag, sc->sc_cmd_dmamap, BUS_DMASYNC_PREWRITE); 433 bus_space_write_4(sc->sc_bt, sc->sc_mailbox, 0, sc->sc_cmd_phys); 434 435 /* Flush the cacheline it is in -- SMU bypasses the cache */ 436 __asm __volatile("sync; dcbf 0,%0; sync" :: "r"(sc->sc_cmd): "memory"); 437 438 /* Ring SMU doorbell */ 439 macgpio_write(smu_doorbell, GPIO_DDR_OUTPUT); 440 } 441 442 static void 443 smu_doorbell_intr(void *xdev) 444 { 445 device_t smu; 446 struct smu_softc *sc; 447 int doorbell_ack; 448 449 smu = xdev; 450 doorbell_ack = macgpio_read(smu_doorbell); 451 sc = device_get_softc(smu); 452 453 if (doorbell_ack != (GPIO_DDR_OUTPUT | GPIO_LEVEL_RO | GPIO_DATA)) 454 return; 455 456 mtx_lock(&sc->sc_mtx); 457 458 if (sc->sc_cur_cmd == NULL) /* spurious */ 459 goto done; 460 461 /* Check result. First invalidate the cache again... */ 462 __asm __volatile("dcbf 0,%0; sync" :: "r"(sc->sc_cmd) : "memory"); 463 464 bus_dmamap_sync(sc->sc_dmatag, sc->sc_cmd_dmamap, BUS_DMASYNC_POSTREAD); 465 466 sc->sc_cur_cmd->cmd = sc->sc_cmd->cmd; 467 sc->sc_cur_cmd->len = sc->sc_cmd->len; 468 memcpy(sc->sc_cur_cmd->data, sc->sc_cmd->data, 469 sizeof(sc->sc_cmd->data)); 470 wakeup(sc->sc_cur_cmd); 471 sc->sc_cur_cmd = NULL; 472 if (sc->sc_u3) 473 powerpc_pow_enabled = 1; 474 475 done: 476 /* Queue next command if one is pending */ 477 if (STAILQ_FIRST(&sc->sc_cmdq) != NULL) { 478 sc->sc_cur_cmd = STAILQ_FIRST(&sc->sc_cmdq); 479 STAILQ_REMOVE_HEAD(&sc->sc_cmdq, cmd_q); 480 smu_send_cmd(smu, sc->sc_cur_cmd); 481 } 482 483 mtx_unlock(&sc->sc_mtx); 484 } 485 486 static int 487 smu_run_cmd(device_t dev, struct smu_cmd *cmd, int wait) 488 { 489 struct smu_softc *sc; 490 uint8_t cmd_code; 491 int error; 492 493 sc = device_get_softc(dev); 494 cmd_code = cmd->cmd; 495 496 mtx_lock(&sc->sc_mtx); 497 if (sc->sc_cur_cmd != NULL) { 498 STAILQ_INSERT_TAIL(&sc->sc_cmdq, cmd, cmd_q); 499 } else 500 smu_send_cmd(dev, cmd); 501 mtx_unlock(&sc->sc_mtx); 502 503 if (!wait) 504 return (0); 505 506 if (sc->sc_doorbellirqid < 0) { 507 /* Poll if the IRQ has not been set up yet */ 508 do { 509 DELAY(50); 510 smu_doorbell_intr(dev); 511 } while (sc->sc_cur_cmd != NULL); 512 } else { 513 /* smu_doorbell_intr will wake us when the command is ACK'ed */ 514 error = tsleep(cmd, 0, "smu", 800 * hz / 1000); 515 if (error != 0) 516 smu_doorbell_intr(dev); /* One last chance */ 517 518 if (error != 0) { 519 mtx_lock(&sc->sc_mtx); 520 if (cmd->cmd == cmd_code) { /* Never processed */ 521 /* Abort this command if we timed out */ 522 if (sc->sc_cur_cmd == cmd) 523 sc->sc_cur_cmd = NULL; 524 else 525 STAILQ_REMOVE(&sc->sc_cmdq, cmd, smu_cmd, 526 cmd_q); 527 mtx_unlock(&sc->sc_mtx); 528 return (error); 529 } 530 error = 0; 531 mtx_unlock(&sc->sc_mtx); 532 } 533 } 534 535 /* SMU acks the command by inverting the command bits */ 536 if (cmd->cmd == ((~cmd_code) & 0xff)) 537 error = 0; 538 else 539 error = EIO; 540 541 return (error); 542 } 543 544 static int 545 smu_get_datablock(device_t dev, int8_t id, uint8_t *buf, size_t len) 546 { 547 struct smu_cmd cmd; 548 uint8_t addr[4]; 549 550 cmd.cmd = SMU_PARTITION; 551 cmd.len = 2; 552 cmd.data[0] = SMU_PARTITION_LATEST; 553 cmd.data[1] = id; 554 555 smu_run_cmd(dev, &cmd, 1); 556 557 addr[0] = addr[1] = 0; 558 addr[2] = cmd.data[0]; 559 addr[3] = cmd.data[1]; 560 561 cmd.cmd = SMU_MISC; 562 cmd.len = 7; 563 cmd.data[0] = SMU_MISC_GET_DATA; 564 cmd.data[1] = sizeof(addr); 565 memcpy(&cmd.data[2], addr, sizeof(addr)); 566 cmd.data[6] = len; 567 568 smu_run_cmd(dev, &cmd, 1); 569 memcpy(buf, cmd.data, len); 570 return (0); 571 } 572 573 static void 574 smu_slew_cpu_voltage(device_t dev, int to) 575 { 576 struct smu_cmd cmd; 577 578 cmd.cmd = SMU_POWER; 579 cmd.len = 8; 580 cmd.data[0] = 'V'; 581 cmd.data[1] = 'S'; 582 cmd.data[2] = 'L'; 583 cmd.data[3] = 'E'; 584 cmd.data[4] = 'W'; 585 cmd.data[5] = 0xff; 586 cmd.data[6] = 1; 587 cmd.data[7] = to; 588 589 smu_run_cmd(dev, &cmd, 1); 590 } 591 592 static void 593 smu_cpufreq_pre_change(device_t dev, const struct cf_level *level) 594 { 595 /* 596 * Make sure the CPU voltage is raised before we raise 597 * the clock. 598 */ 599 600 if (level->rel_set[0].freq == 10000 /* max */) 601 smu_slew_cpu_voltage(dev, 0); 602 } 603 604 static void 605 smu_cpufreq_post_change(device_t dev, const struct cf_level *level) 606 { 607 /* We are safe to reduce CPU voltage after a downward transition */ 608 609 if (level->rel_set[0].freq < 10000 /* max */) 610 smu_slew_cpu_voltage(dev, 1); /* XXX: 1/4 voltage for 970MP? */ 611 } 612 613 /* Routines for probing the SMU doorbell GPIO */ 614 static int doorbell_probe(device_t dev); 615 static int doorbell_attach(device_t dev); 616 617 static device_method_t doorbell_methods[] = { 618 /* Device interface */ 619 DEVMETHOD(device_probe, doorbell_probe), 620 DEVMETHOD(device_attach, doorbell_attach), 621 { 0, 0 }, 622 }; 623 624 static driver_t doorbell_driver = { 625 "smudoorbell", 626 doorbell_methods, 627 0 628 }; 629 630 EARLY_DRIVER_MODULE(smudoorbell, macgpio, doorbell_driver, 0, 0, 631 BUS_PASS_SUPPORTDEV); 632 633 static int 634 doorbell_probe(device_t dev) 635 { 636 const char *name = ofw_bus_get_name(dev); 637 638 if (strcmp(name, "smu-doorbell") != 0) 639 return (ENXIO); 640 641 device_set_desc(dev, "SMU Doorbell GPIO"); 642 device_quiet(dev); 643 return (0); 644 } 645 646 static int 647 doorbell_attach(device_t dev) 648 { 649 smu_doorbell = dev; 650 return (0); 651 } 652 653 /* 654 * Sensor and fan management 655 */ 656 657 static int 658 smu_fan_check_old_style(struct smu_fan *fan) 659 { 660 device_t smu = fan->dev; 661 struct smu_softc *sc = device_get_softc(smu); 662 struct smu_cmd cmd; 663 int error; 664 665 if (sc->old_style_fans != -1) 666 return (sc->old_style_fans); 667 668 /* 669 * Apple has two fan control mechanisms. We can't distinguish 670 * them except by seeing if the new one fails. If the new one 671 * fails, use the old one. 672 */ 673 674 cmd.cmd = SMU_FAN; 675 cmd.len = 2; 676 cmd.data[0] = 0x31; 677 cmd.data[1] = fan->reg; 678 679 do { 680 error = smu_run_cmd(smu, &cmd, 1); 681 } while (error == EWOULDBLOCK); 682 683 sc->old_style_fans = (error != 0); 684 685 return (sc->old_style_fans); 686 } 687 688 static int 689 smu_fan_set_rpm(struct smu_fan *fan, int rpm) 690 { 691 device_t smu = fan->dev; 692 struct smu_cmd cmd; 693 int error; 694 695 cmd.cmd = SMU_FAN; 696 error = EIO; 697 698 /* Clamp to allowed range */ 699 rpm = max(fan->fan.min_rpm, rpm); 700 rpm = min(fan->fan.max_rpm, rpm); 701 702 smu_fan_check_old_style(fan); 703 704 if (!fan->old_style) { 705 cmd.len = 4; 706 cmd.data[0] = 0x30; 707 cmd.data[1] = fan->reg; 708 cmd.data[2] = (rpm >> 8) & 0xff; 709 cmd.data[3] = rpm & 0xff; 710 711 error = smu_run_cmd(smu, &cmd, 1); 712 if (error && error != EWOULDBLOCK) 713 fan->old_style = 1; 714 } else { 715 cmd.len = 14; 716 cmd.data[0] = 0x00; /* RPM fan. */ 717 cmd.data[1] = 1 << fan->reg; 718 cmd.data[2 + 2*fan->reg] = (rpm >> 8) & 0xff; 719 cmd.data[3 + 2*fan->reg] = rpm & 0xff; 720 error = smu_run_cmd(smu, &cmd, 1); 721 } 722 723 if (error == 0) 724 fan->setpoint = rpm; 725 726 return (error); 727 } 728 729 static int 730 smu_fan_read_rpm(struct smu_fan *fan) 731 { 732 device_t smu = fan->dev; 733 struct smu_cmd cmd; 734 int rpm, error; 735 736 smu_fan_check_old_style(fan); 737 738 if (!fan->old_style) { 739 cmd.cmd = SMU_FAN; 740 cmd.len = 2; 741 cmd.data[0] = 0x31; 742 cmd.data[1] = fan->reg; 743 744 error = smu_run_cmd(smu, &cmd, 1); 745 if (error && error != EWOULDBLOCK) 746 fan->old_style = 1; 747 748 rpm = (cmd.data[0] << 8) | cmd.data[1]; 749 } 750 751 if (fan->old_style) { 752 cmd.cmd = SMU_FAN; 753 cmd.len = 1; 754 cmd.data[0] = SMU_RPM_STATUS; 755 756 error = smu_run_cmd(smu, &cmd, 1); 757 if (error) 758 return (error); 759 760 rpm = (cmd.data[fan->reg*2+1] << 8) | cmd.data[fan->reg*2+2]; 761 } 762 763 return (rpm); 764 } 765 static int 766 smu_fan_set_pwm(struct smu_fan *fan, int pwm) 767 { 768 device_t smu = fan->dev; 769 struct smu_cmd cmd; 770 int error; 771 772 cmd.cmd = SMU_FAN; 773 error = EIO; 774 775 /* Clamp to allowed range */ 776 pwm = max(fan->fan.min_rpm, pwm); 777 pwm = min(fan->fan.max_rpm, pwm); 778 779 /* 780 * Apple has two fan control mechanisms. We can't distinguish 781 * them except by seeing if the new one fails. If the new one 782 * fails, use the old one. 783 */ 784 785 if (!fan->old_style) { 786 cmd.len = 4; 787 cmd.data[0] = 0x30; 788 cmd.data[1] = fan->reg; 789 cmd.data[2] = (pwm >> 8) & 0xff; 790 cmd.data[3] = pwm & 0xff; 791 792 error = smu_run_cmd(smu, &cmd, 1); 793 if (error && error != EWOULDBLOCK) 794 fan->old_style = 1; 795 } 796 797 if (fan->old_style) { 798 cmd.len = 14; 799 cmd.data[0] = 0x10; /* PWM fan. */ 800 cmd.data[1] = 1 << fan->reg; 801 cmd.data[2 + 2*fan->reg] = (pwm >> 8) & 0xff; 802 cmd.data[3 + 2*fan->reg] = pwm & 0xff; 803 error = smu_run_cmd(smu, &cmd, 1); 804 } 805 806 if (error == 0) 807 fan->setpoint = pwm; 808 809 return (error); 810 } 811 812 static int 813 smu_fan_read_pwm(struct smu_fan *fan, int *pwm, int *rpm) 814 { 815 device_t smu = fan->dev; 816 struct smu_cmd cmd; 817 int error; 818 819 if (!fan->old_style) { 820 cmd.cmd = SMU_FAN; 821 cmd.len = 2; 822 cmd.data[0] = 0x31; 823 cmd.data[1] = fan->reg; 824 825 error = smu_run_cmd(smu, &cmd, 1); 826 if (error && error != EWOULDBLOCK) 827 fan->old_style = 1; 828 829 *rpm = (cmd.data[0] << 8) | cmd.data[1]; 830 } 831 832 if (fan->old_style) { 833 cmd.cmd = SMU_FAN; 834 cmd.len = 1; 835 cmd.data[0] = SMU_PWM_STATUS; 836 837 error = smu_run_cmd(smu, &cmd, 1); 838 if (error) 839 return (error); 840 841 *rpm = (cmd.data[fan->reg*2+1] << 8) | cmd.data[fan->reg*2+2]; 842 } 843 if (fan->old_style) { 844 cmd.cmd = SMU_FAN; 845 cmd.len = 14; 846 cmd.data[0] = SMU_PWM_SETPOINT; 847 cmd.data[1] = 1 << fan->reg; 848 849 error = smu_run_cmd(smu, &cmd, 1); 850 if (error) 851 return (error); 852 853 *pwm = cmd.data[fan->reg*2+2]; 854 } 855 return (0); 856 } 857 858 static int 859 smu_fanrpm_sysctl(SYSCTL_HANDLER_ARGS) 860 { 861 device_t smu; 862 struct smu_softc *sc; 863 struct smu_fan *fan; 864 int pwm = 0, rpm, error = 0; 865 866 smu = arg1; 867 sc = device_get_softc(smu); 868 fan = &sc->sc_fans[arg2 & 0xff]; 869 870 if (fan->type == SMU_FAN_RPM) { 871 rpm = smu_fan_read_rpm(fan); 872 if (rpm < 0) 873 return (rpm); 874 875 error = sysctl_handle_int(oidp, &rpm, 0, req); 876 } else { 877 error = smu_fan_read_pwm(fan, &pwm, &rpm); 878 if (error < 0) 879 return (EIO); 880 881 switch (arg2 & 0xff00) { 882 case SMU_PWM_SYSCTL_PWM: 883 error = sysctl_handle_int(oidp, &pwm, 0, req); 884 break; 885 case SMU_PWM_SYSCTL_RPM: 886 error = sysctl_handle_int(oidp, &rpm, 0, req); 887 break; 888 default: 889 /* This should never happen */ 890 return (EINVAL); 891 } 892 } 893 /* We can only read the RPM from a PWM controlled fan, so return. */ 894 if ((arg2 & 0xff00) == SMU_PWM_SYSCTL_RPM) 895 return (0); 896 897 if (error || !req->newptr) 898 return (error); 899 900 sc->sc_lastuserchange = time_uptime; 901 902 if (fan->type == SMU_FAN_RPM) 903 return (smu_fan_set_rpm(fan, rpm)); 904 else 905 return (smu_fan_set_pwm(fan, pwm)); 906 } 907 908 static void 909 smu_fill_fan_prop(device_t dev, phandle_t child, int id) 910 { 911 struct smu_fan *fan; 912 struct smu_softc *sc; 913 char type[32]; 914 915 sc = device_get_softc(dev); 916 fan = &sc->sc_fans[id]; 917 918 OF_getprop(child, "device_type", type, sizeof(type)); 919 /* We have either RPM or PWM controlled fans. */ 920 if (strcmp(type, "fan-rpm-control") == 0) 921 fan->type = SMU_FAN_RPM; 922 else 923 fan->type = SMU_FAN_PWM; 924 925 fan->dev = dev; 926 fan->old_style = 0; 927 OF_getprop(child, "reg", &fan->reg, 928 sizeof(cell_t)); 929 OF_getprop(child, "min-value", &fan->fan.min_rpm, 930 sizeof(int)); 931 OF_getprop(child, "max-value", &fan->fan.max_rpm, 932 sizeof(int)); 933 OF_getprop(child, "zone", &fan->fan.zone, 934 sizeof(int)); 935 936 if (OF_getprop(child, "unmanaged-value", 937 &fan->fan.default_rpm, 938 sizeof(int)) != sizeof(int)) 939 fan->fan.default_rpm = fan->fan.max_rpm; 940 941 OF_getprop(child, "location", fan->fan.name, 942 sizeof(fan->fan.name)); 943 944 if (fan->type == SMU_FAN_RPM) 945 fan->setpoint = smu_fan_read_rpm(fan); 946 else 947 smu_fan_read_pwm(fan, &fan->setpoint, &fan->rpm); 948 } 949 950 /* On the first call count the number of fans. In the second call, 951 * after allocating the fan struct, fill the properties of the fans. 952 */ 953 static int 954 smu_count_fans(device_t dev) 955 { 956 struct smu_softc *sc; 957 phandle_t child, node, root; 958 int nfans = 0; 959 960 node = ofw_bus_get_node(dev); 961 sc = device_get_softc(dev); 962 963 /* First find the fanroots and count the number of fans. */ 964 for (root = OF_child(node); root != 0; root = OF_peer(root)) { 965 char name[32]; 966 memset(name, 0, sizeof(name)); 967 OF_getprop(root, "name", name, sizeof(name)); 968 if (strncmp(name, "rpm-fans", 9) == 0 || 969 strncmp(name, "pwm-fans", 9) == 0 || 970 strncmp(name, "fans", 5) == 0) 971 for (child = OF_child(root); child != 0; 972 child = OF_peer(child)) { 973 nfans++; 974 /* When allocated, fill the fan properties. */ 975 if (sc->sc_fans != NULL) { 976 smu_fill_fan_prop(dev, child, 977 nfans - 1); 978 } 979 } 980 } 981 if (nfans == 0) { 982 device_printf(dev, "WARNING: No fans detected!\n"); 983 return (0); 984 } 985 return (nfans); 986 } 987 988 static void 989 smu_attach_fans(device_t dev, phandle_t fanroot) 990 { 991 struct smu_fan *fan; 992 struct smu_softc *sc; 993 struct sysctl_oid *oid, *fanroot_oid; 994 struct sysctl_ctx_list *ctx; 995 char sysctl_name[32]; 996 int i, j; 997 998 sc = device_get_softc(dev); 999 1000 /* Get the number of fans. */ 1001 sc->sc_nfans = smu_count_fans(dev); 1002 if (sc->sc_nfans == 0) 1003 return; 1004 1005 /* Now we're able to allocate memory for the fans struct. */ 1006 sc->sc_fans = malloc(sc->sc_nfans * sizeof(struct smu_fan), M_SMU, 1007 M_WAITOK | M_ZERO); 1008 1009 /* Now fill in the properties. */ 1010 smu_count_fans(dev); 1011 1012 /* Register fans with pmac_thermal */ 1013 for (i = 0; i < sc->sc_nfans; i++) 1014 pmac_thermal_fan_register(&sc->sc_fans[i].fan); 1015 1016 ctx = device_get_sysctl_ctx(dev); 1017 fanroot_oid = SYSCTL_ADD_NODE(ctx, 1018 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "fans", 1019 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "SMU Fan Information"); 1020 1021 /* Add sysctls */ 1022 for (i = 0; i < sc->sc_nfans; i++) { 1023 fan = &sc->sc_fans[i]; 1024 for (j = 0; j < strlen(fan->fan.name); j++) { 1025 sysctl_name[j] = tolower(fan->fan.name[j]); 1026 if (isspace(sysctl_name[j])) 1027 sysctl_name[j] = '_'; 1028 } 1029 sysctl_name[j] = 0; 1030 if (fan->type == SMU_FAN_RPM) { 1031 oid = SYSCTL_ADD_NODE(ctx, 1032 SYSCTL_CHILDREN(fanroot_oid), OID_AUTO, 1033 sysctl_name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 1034 "Fan Information"); 1035 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 1036 "minrpm", CTLFLAG_RD, 1037 &fan->fan.min_rpm, 0, 1038 "Minimum allowed RPM"); 1039 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 1040 "maxrpm", CTLFLAG_RD, 1041 &fan->fan.max_rpm, 0, 1042 "Maximum allowed RPM"); 1043 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 1044 "rpm",CTLTYPE_INT | CTLFLAG_RW | 1045 CTLFLAG_MPSAFE, dev, i, 1046 smu_fanrpm_sysctl, "I", "Fan RPM"); 1047 1048 fan->fan.read = (int (*)(struct pmac_fan *))smu_fan_read_rpm; 1049 fan->fan.set = (int (*)(struct pmac_fan *, int))smu_fan_set_rpm; 1050 1051 } else { 1052 oid = SYSCTL_ADD_NODE(ctx, 1053 SYSCTL_CHILDREN(fanroot_oid), OID_AUTO, 1054 sysctl_name, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 1055 "Fan Information"); 1056 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 1057 "minpwm", CTLFLAG_RD, 1058 &fan->fan.min_rpm, 0, 1059 "Minimum allowed PWM in %"); 1060 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 1061 "maxpwm", CTLFLAG_RD, 1062 &fan->fan.max_rpm, 0, 1063 "Maximum allowed PWM in %"); 1064 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 1065 "pwm",CTLTYPE_INT | CTLFLAG_RW | 1066 CTLFLAG_MPSAFE, dev, 1067 SMU_PWM_SYSCTL_PWM | i, 1068 smu_fanrpm_sysctl, "I", "Fan PWM in %"); 1069 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 1070 "rpm",CTLTYPE_INT | CTLFLAG_RD | 1071 CTLFLAG_MPSAFE, dev, 1072 SMU_PWM_SYSCTL_RPM | i, 1073 smu_fanrpm_sysctl, "I", "Fan RPM"); 1074 fan->fan.read = NULL; 1075 fan->fan.set = (int (*)(struct pmac_fan *, int))smu_fan_set_pwm; 1076 } 1077 if (bootverbose) 1078 device_printf(dev, "Fan: %s type: %d\n", 1079 fan->fan.name, fan->type); 1080 } 1081 } 1082 1083 static int 1084 smu_sensor_read(struct smu_sensor *sens) 1085 { 1086 device_t smu = sens->dev; 1087 struct smu_cmd cmd; 1088 struct smu_softc *sc; 1089 int64_t value; 1090 int error; 1091 1092 cmd.cmd = SMU_ADC; 1093 cmd.len = 1; 1094 cmd.data[0] = sens->reg; 1095 error = 0; 1096 1097 error = smu_run_cmd(smu, &cmd, 1); 1098 if (error != 0) 1099 return (-1); 1100 1101 sc = device_get_softc(smu); 1102 value = (cmd.data[0] << 8) | cmd.data[1]; 1103 1104 switch (sens->type) { 1105 case SMU_TEMP_SENSOR: 1106 value *= sc->sc_cpu_diode_scale; 1107 value >>= 3; 1108 value += ((int64_t)sc->sc_cpu_diode_offset) << 9; 1109 value <<= 1; 1110 1111 /* Convert from 16.16 fixed point degC into integer 0.1 K. */ 1112 value = 10*(value >> 16) + ((10*(value & 0xffff)) >> 16) + 2731; 1113 break; 1114 case SMU_VOLTAGE_SENSOR: 1115 value *= sc->sc_cpu_volt_scale; 1116 value += sc->sc_cpu_volt_offset; 1117 value <<= 4; 1118 1119 /* Convert from 16.16 fixed point V into mV. */ 1120 value *= 15625; 1121 value /= 1024; 1122 value /= 1000; 1123 break; 1124 case SMU_CURRENT_SENSOR: 1125 value *= sc->sc_cpu_curr_scale; 1126 value += sc->sc_cpu_curr_offset; 1127 value <<= 4; 1128 1129 /* Convert from 16.16 fixed point A into mA. */ 1130 value *= 15625; 1131 value /= 1024; 1132 value /= 1000; 1133 break; 1134 case SMU_POWER_SENSOR: 1135 value *= sc->sc_slots_pow_scale; 1136 value += sc->sc_slots_pow_offset; 1137 value <<= 4; 1138 1139 /* Convert from 16.16 fixed point W into mW. */ 1140 value *= 15625; 1141 value /= 1024; 1142 value /= 1000; 1143 break; 1144 } 1145 1146 return (value); 1147 } 1148 1149 static int 1150 smu_sensor_sysctl(SYSCTL_HANDLER_ARGS) 1151 { 1152 device_t smu; 1153 struct smu_softc *sc; 1154 struct smu_sensor *sens; 1155 int value, error; 1156 1157 smu = arg1; 1158 sc = device_get_softc(smu); 1159 sens = &sc->sc_sensors[arg2]; 1160 1161 value = smu_sensor_read(sens); 1162 if (value < 0) 1163 return (EBUSY); 1164 1165 error = sysctl_handle_int(oidp, &value, 0, req); 1166 1167 return (error); 1168 } 1169 1170 static void 1171 smu_attach_sensors(device_t dev, phandle_t sensroot) 1172 { 1173 struct smu_sensor *sens; 1174 struct smu_softc *sc; 1175 struct sysctl_oid *sensroot_oid; 1176 struct sysctl_ctx_list *ctx; 1177 phandle_t child; 1178 char type[32]; 1179 int i; 1180 1181 sc = device_get_softc(dev); 1182 sc->sc_nsensors = 0; 1183 1184 for (child = OF_child(sensroot); child != 0; child = OF_peer(child)) 1185 sc->sc_nsensors++; 1186 1187 if (sc->sc_nsensors == 0) { 1188 device_printf(dev, "WARNING: No sensors detected!\n"); 1189 return; 1190 } 1191 1192 sc->sc_sensors = malloc(sc->sc_nsensors * sizeof(struct smu_sensor), 1193 M_SMU, M_WAITOK | M_ZERO); 1194 1195 sens = sc->sc_sensors; 1196 sc->sc_nsensors = 0; 1197 1198 ctx = device_get_sysctl_ctx(dev); 1199 sensroot_oid = SYSCTL_ADD_NODE(ctx, 1200 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "sensors", 1201 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "SMU Sensor Information"); 1202 1203 for (child = OF_child(sensroot); child != 0; child = OF_peer(child)) { 1204 char sysctl_name[40], sysctl_desc[40]; 1205 const char *units; 1206 1207 sens->dev = dev; 1208 OF_getprop(child, "device_type", type, sizeof(type)); 1209 1210 if (strcmp(type, "current-sensor") == 0) { 1211 sens->type = SMU_CURRENT_SENSOR; 1212 units = "mA"; 1213 } else if (strcmp(type, "temp-sensor") == 0) { 1214 sens->type = SMU_TEMP_SENSOR; 1215 units = "C"; 1216 } else if (strcmp(type, "voltage-sensor") == 0) { 1217 sens->type = SMU_VOLTAGE_SENSOR; 1218 units = "mV"; 1219 } else if (strcmp(type, "power-sensor") == 0) { 1220 sens->type = SMU_POWER_SENSOR; 1221 units = "mW"; 1222 } else { 1223 continue; 1224 } 1225 1226 OF_getprop(child, "reg", &sens->reg, sizeof(cell_t)); 1227 OF_getprop(child, "zone", &sens->therm.zone, sizeof(int)); 1228 OF_getprop(child, "location", sens->therm.name, 1229 sizeof(sens->therm.name)); 1230 1231 for (i = 0; i < strlen(sens->therm.name); i++) { 1232 sysctl_name[i] = tolower(sens->therm.name[i]); 1233 if (isspace(sysctl_name[i])) 1234 sysctl_name[i] = '_'; 1235 } 1236 sysctl_name[i] = 0; 1237 1238 sprintf(sysctl_desc,"%s (%s)", sens->therm.name, units); 1239 1240 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(sensroot_oid), OID_AUTO, 1241 sysctl_name, CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, 1242 dev, sc->sc_nsensors, smu_sensor_sysctl, 1243 (sens->type == SMU_TEMP_SENSOR) ? "IK" : "I", sysctl_desc); 1244 1245 if (sens->type == SMU_TEMP_SENSOR) { 1246 /* Make up some numbers */ 1247 sens->therm.target_temp = 500 + 2731; /* 50 C */ 1248 sens->therm.max_temp = 900 + 2731; /* 90 C */ 1249 1250 sens->therm.read = 1251 (int (*)(struct pmac_therm *))smu_sensor_read; 1252 pmac_thermal_sensor_register(&sens->therm); 1253 } 1254 1255 sens++; 1256 sc->sc_nsensors++; 1257 } 1258 } 1259 1260 static void 1261 smu_set_sleepled(void *xdev, int onoff) 1262 { 1263 static struct smu_cmd cmd; 1264 device_t smu = xdev; 1265 1266 cmd.cmd = SMU_MISC; 1267 cmd.len = 3; 1268 cmd.data[0] = SMU_MISC_LED_CTRL; 1269 cmd.data[1] = 0; 1270 cmd.data[2] = onoff; 1271 1272 smu_run_cmd(smu, &cmd, 0); 1273 } 1274 1275 static int 1276 smu_server_mode(SYSCTL_HANDLER_ARGS) 1277 { 1278 struct smu_cmd cmd; 1279 u_int server_mode; 1280 device_t smu = arg1; 1281 int error; 1282 1283 cmd.cmd = SMU_POWER_EVENTS; 1284 cmd.len = 1; 1285 cmd.data[0] = SMU_PWR_GET_POWERUP; 1286 1287 error = smu_run_cmd(smu, &cmd, 1); 1288 1289 if (error) 1290 return (error); 1291 1292 server_mode = (cmd.data[1] & SMU_WAKEUP_AC_INSERT) ? 1 : 0; 1293 1294 error = sysctl_handle_int(oidp, &server_mode, 0, req); 1295 1296 if (error || !req->newptr) 1297 return (error); 1298 1299 if (server_mode == 1) 1300 cmd.data[0] = SMU_PWR_SET_POWERUP; 1301 else if (server_mode == 0) 1302 cmd.data[0] = SMU_PWR_CLR_POWERUP; 1303 else 1304 return (EINVAL); 1305 1306 cmd.len = 3; 1307 cmd.data[1] = 0; 1308 cmd.data[2] = SMU_WAKEUP_AC_INSERT; 1309 1310 return (smu_run_cmd(smu, &cmd, 1)); 1311 } 1312 1313 static void 1314 smu_shutdown(void *xdev, int howto) 1315 { 1316 device_t smu = xdev; 1317 struct smu_cmd cmd; 1318 1319 cmd.cmd = SMU_POWER; 1320 if (howto & RB_HALT) 1321 strcpy(cmd.data, "SHUTDOWN"); 1322 else 1323 strcpy(cmd.data, "RESTART"); 1324 1325 cmd.len = strlen(cmd.data); 1326 1327 smu_run_cmd(smu, &cmd, 1); 1328 1329 for (;;); 1330 } 1331 1332 static int 1333 smu_gettime(device_t dev, struct timespec *ts) 1334 { 1335 struct smu_cmd cmd; 1336 struct clocktime ct; 1337 1338 cmd.cmd = SMU_RTC; 1339 cmd.len = 1; 1340 cmd.data[0] = SMU_RTC_GET; 1341 1342 if (smu_run_cmd(dev, &cmd, 1) != 0) 1343 return (ENXIO); 1344 1345 ct.nsec = 0; 1346 ct.sec = bcd2bin(cmd.data[0]); 1347 ct.min = bcd2bin(cmd.data[1]); 1348 ct.hour = bcd2bin(cmd.data[2]); 1349 ct.dow = bcd2bin(cmd.data[3]); 1350 ct.day = bcd2bin(cmd.data[4]); 1351 ct.mon = bcd2bin(cmd.data[5]); 1352 ct.year = bcd2bin(cmd.data[6]) + 2000; 1353 1354 return (clock_ct_to_ts(&ct, ts)); 1355 } 1356 1357 static int 1358 smu_settime(device_t dev, struct timespec *ts) 1359 { 1360 static struct smu_cmd cmd; 1361 struct clocktime ct; 1362 1363 cmd.cmd = SMU_RTC; 1364 cmd.len = 8; 1365 cmd.data[0] = SMU_RTC_SET; 1366 1367 clock_ts_to_ct(ts, &ct); 1368 1369 cmd.data[1] = bin2bcd(ct.sec); 1370 cmd.data[2] = bin2bcd(ct.min); 1371 cmd.data[3] = bin2bcd(ct.hour); 1372 cmd.data[4] = bin2bcd(ct.dow); 1373 cmd.data[5] = bin2bcd(ct.day); 1374 cmd.data[6] = bin2bcd(ct.mon); 1375 cmd.data[7] = bin2bcd(ct.year - 2000); 1376 1377 return (smu_run_cmd(dev, &cmd, 0)); 1378 } 1379 1380 /* SMU I2C Interface */ 1381 1382 static int smuiic_probe(device_t dev); 1383 static int smuiic_attach(device_t dev); 1384 static int smuiic_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs); 1385 static phandle_t smuiic_get_node(device_t bus, device_t dev); 1386 1387 static device_method_t smuiic_methods[] = { 1388 /* device interface */ 1389 DEVMETHOD(device_probe, smuiic_probe), 1390 DEVMETHOD(device_attach, smuiic_attach), 1391 1392 /* iicbus interface */ 1393 DEVMETHOD(iicbus_callback, iicbus_null_callback), 1394 DEVMETHOD(iicbus_transfer, smuiic_transfer), 1395 1396 /* ofw_bus interface */ 1397 DEVMETHOD(ofw_bus_get_node, smuiic_get_node), 1398 { 0, 0 } 1399 }; 1400 1401 struct smuiic_softc { 1402 struct mtx sc_mtx; 1403 volatile int sc_iic_inuse; 1404 int sc_busno; 1405 }; 1406 1407 static driver_t smuiic_driver = { 1408 "iichb", 1409 smuiic_methods, 1410 sizeof(struct smuiic_softc) 1411 }; 1412 1413 DRIVER_MODULE(smuiic, smu, smuiic_driver, 0, 0); 1414 1415 static void 1416 smu_attach_i2c(device_t smu, phandle_t i2croot) 1417 { 1418 phandle_t child; 1419 device_t cdev; 1420 struct ofw_bus_devinfo *dinfo; 1421 char name[32]; 1422 1423 for (child = OF_child(i2croot); child != 0; child = OF_peer(child)) { 1424 if (OF_getprop(child, "name", name, sizeof(name)) <= 0) 1425 continue; 1426 1427 if (strcmp(name, "i2c-bus") != 0 && strcmp(name, "i2c") != 0) 1428 continue; 1429 1430 dinfo = malloc(sizeof(struct ofw_bus_devinfo), M_SMU, 1431 M_WAITOK | M_ZERO); 1432 if (ofw_bus_gen_setup_devinfo(dinfo, child) != 0) { 1433 free(dinfo, M_SMU); 1434 continue; 1435 } 1436 1437 cdev = device_add_child(smu, NULL, -1); 1438 if (cdev == NULL) { 1439 device_printf(smu, "<%s>: device_add_child failed\n", 1440 dinfo->obd_name); 1441 ofw_bus_gen_destroy_devinfo(dinfo); 1442 free(dinfo, M_SMU); 1443 continue; 1444 } 1445 device_set_ivars(cdev, dinfo); 1446 } 1447 } 1448 1449 static int 1450 smuiic_probe(device_t dev) 1451 { 1452 const char *name; 1453 1454 name = ofw_bus_get_name(dev); 1455 if (name == NULL) 1456 return (ENXIO); 1457 1458 if (strcmp(name, "i2c-bus") == 0 || strcmp(name, "i2c") == 0) { 1459 device_set_desc(dev, "SMU I2C controller"); 1460 return (0); 1461 } 1462 1463 return (ENXIO); 1464 } 1465 1466 static int 1467 smuiic_attach(device_t dev) 1468 { 1469 struct smuiic_softc *sc = device_get_softc(dev); 1470 mtx_init(&sc->sc_mtx, "smuiic", NULL, MTX_DEF); 1471 sc->sc_iic_inuse = 0; 1472 1473 /* Get our bus number */ 1474 OF_getprop(ofw_bus_get_node(dev), "reg", &sc->sc_busno, 1475 sizeof(sc->sc_busno)); 1476 1477 /* Add the IIC bus layer */ 1478 device_add_child(dev, "iicbus", -1); 1479 1480 return (bus_generic_attach(dev)); 1481 } 1482 1483 static int 1484 smuiic_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs) 1485 { 1486 struct smuiic_softc *sc = device_get_softc(dev); 1487 struct smu_cmd cmd; 1488 int i, j, error; 1489 1490 mtx_lock(&sc->sc_mtx); 1491 while (sc->sc_iic_inuse) 1492 mtx_sleep(sc, &sc->sc_mtx, 0, "smuiic", 100); 1493 1494 sc->sc_iic_inuse = 1; 1495 error = 0; 1496 1497 for (i = 0; i < nmsgs; i++) { 1498 cmd.cmd = SMU_I2C; 1499 cmd.data[0] = sc->sc_busno; 1500 if (msgs[i].flags & IIC_M_NOSTOP) 1501 cmd.data[1] = SMU_I2C_COMBINED; 1502 else 1503 cmd.data[1] = SMU_I2C_SIMPLE; 1504 1505 cmd.data[2] = msgs[i].slave; 1506 if (msgs[i].flags & IIC_M_RD) 1507 cmd.data[2] |= 1; 1508 1509 if (msgs[i].flags & IIC_M_NOSTOP) { 1510 KASSERT(msgs[i].len < 4, 1511 ("oversize I2C combined message")); 1512 1513 cmd.data[3] = min(msgs[i].len, 3); 1514 memcpy(&cmd.data[4], msgs[i].buf, min(msgs[i].len, 3)); 1515 i++; /* Advance to next part of message */ 1516 } else { 1517 cmd.data[3] = 0; 1518 memset(&cmd.data[4], 0, 3); 1519 } 1520 1521 cmd.data[7] = msgs[i].slave; 1522 if (msgs[i].flags & IIC_M_RD) 1523 cmd.data[7] |= 1; 1524 1525 cmd.data[8] = msgs[i].len; 1526 if (msgs[i].flags & IIC_M_RD) { 1527 memset(&cmd.data[9], 0xff, msgs[i].len); 1528 cmd.len = 9; 1529 } else { 1530 memcpy(&cmd.data[9], msgs[i].buf, msgs[i].len); 1531 cmd.len = 9 + msgs[i].len; 1532 } 1533 1534 mtx_unlock(&sc->sc_mtx); 1535 smu_run_cmd(device_get_parent(dev), &cmd, 1); 1536 mtx_lock(&sc->sc_mtx); 1537 1538 for (j = 0; j < 10; j++) { 1539 cmd.cmd = SMU_I2C; 1540 cmd.len = 1; 1541 cmd.data[0] = 0; 1542 memset(&cmd.data[1], 0xff, msgs[i].len); 1543 1544 mtx_unlock(&sc->sc_mtx); 1545 smu_run_cmd(device_get_parent(dev), &cmd, 1); 1546 mtx_lock(&sc->sc_mtx); 1547 1548 if (!(cmd.data[0] & 0x80)) 1549 break; 1550 1551 mtx_sleep(sc, &sc->sc_mtx, 0, "smuiic", 10); 1552 } 1553 1554 if (cmd.data[0] & 0x80) { 1555 error = EIO; 1556 msgs[i].len = 0; 1557 goto exit; 1558 } 1559 memcpy(msgs[i].buf, &cmd.data[1], msgs[i].len); 1560 msgs[i].len = cmd.len - 1; 1561 } 1562 1563 exit: 1564 sc->sc_iic_inuse = 0; 1565 mtx_unlock(&sc->sc_mtx); 1566 wakeup(sc); 1567 return (error); 1568 } 1569 1570 static phandle_t 1571 smuiic_get_node(device_t bus, device_t dev) 1572 { 1573 1574 return (ofw_bus_get_node(bus)); 1575 } 1576