1 /*- 2 * Copyright (c) 2013 Phileas Fogg 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 15 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 16 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 18 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 24 * POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <sys/param.h> 28 #include <sys/systm.h> 29 #include <sys/kernel.h> 30 #include <sys/malloc.h> 31 #include <sys/device.h> 32 #include <sys/proc.h> 33 #include <sys/mutex.h> 34 #include <sys/time.h> 35 #include <sys/reboot.h> 36 #include <sys/sysctl.h> 37 #include <sys/kthread.h> 38 39 #include <machine/autoconf.h> 40 41 #include <dev/ofw/openfirm.h> 42 #include <dev/i2c/i2cvar.h> 43 #include <dev/clock_subr.h> 44 #include <dev/sysmon/sysmonvar.h> 45 #include <dev/sysmon/sysmon_taskq.h> 46 47 #include <macppc/dev/obiovar.h> 48 #include <macppc/dev/smuvar.h> 49 50 #include "opt_smu.h" 51 52 struct smu_softc; 53 54 struct smu_cmd { 55 u_char cmd; 56 u_char len; 57 u_char data[254]; 58 }; 59 60 struct smu_fan { 61 struct smu_softc* sc; 62 63 char location[32]; 64 int reg; 65 int zone; 66 int rpm_ctl; 67 int min_rpm; 68 int max_rpm; 69 int default_rpm; 70 int current_rpm; 71 time_t last_update; 72 }; 73 74 struct smu_iicbus { 75 struct smu_softc* sc; 76 77 int reg; 78 struct i2c_controller i2c; 79 }; 80 81 #define SMU_MAX_FANS 8 82 #define SMU_MAX_IICBUS 3 83 #define SMU_MAX_SME_SENSORS SMU_MAX_FANS 84 85 struct smu_zone { 86 bool (*filter)(const envsys_data_t *); 87 int nfans; 88 int fans[SMU_MAX_FANS]; 89 int threshold, step; 90 int duty; 91 }; 92 93 94 #define SMU_ZONE_CPUS 0 95 #define SMU_ZONE_DRIVES 1 96 #define SMU_ZONE_SLOTS 2 97 #define SMU_ZONES 3 98 99 #define C_TO_uK(n) (n * 1000000 + 273150000) 100 101 struct smu_softc { 102 device_t sc_dev; 103 int sc_node; 104 struct sysctlnode *sc_sysctl_me; 105 106 kmutex_t sc_cmd_lock; 107 kmutex_t sc_msg_lock; 108 struct smu_cmd *sc_cmd; 109 paddr_t sc_cmd_paddr; 110 int sc_dbell_mbox; 111 int sc_dbell_gpio; 112 113 int sc_num_fans; 114 struct smu_fan sc_fans[SMU_MAX_FANS]; 115 116 kmutex_t sc_iicbus_lock; 117 int sc_num_iicbus; 118 struct smu_iicbus sc_iicbus[SMU_MAX_IICBUS]; 119 120 struct todr_chip_handle sc_todr; 121 122 struct sysmon_envsys *sc_sme; 123 envsys_data_t sc_sme_sensors[SMU_MAX_SME_SENSORS]; 124 125 struct smu_zone sc_zones[SMU_ZONES]; 126 lwp_t *sc_thread; 127 bool sc_dying; 128 }; 129 130 #define SMU_CMD_FAN 0x4a 131 #define SMU_CMD_RTC 0x8e 132 #define SMU_CMD_I2C 0x9a 133 #define SMU_CMD_POWER 0xaa 134 135 #ifdef SMU_DEBUG 136 #define DPRINTF printf 137 #else 138 #define DPRINTF while (0) printf 139 #endif 140 141 static int smu_match(device_t, struct cfdata *, void *); 142 static void smu_attach(device_t, device_t, void *); 143 static int smu_setup_doorbell(struct smu_softc *); 144 static void smu_setup_fans(struct smu_softc *); 145 static void smu_setup_iicbus(struct smu_softc *); 146 static void smu_setup_sme(struct smu_softc *); 147 static int smu_iicbus_print(void *, const char *); 148 static void smu_sme_refresh(struct sysmon_envsys *, envsys_data_t *); 149 static int smu_do_cmd(struct smu_softc *, struct smu_cmd *, int); 150 static int smu_dbell_gpio_intr(void *); 151 static int smu_todr_gettime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *); 152 static int smu_todr_settime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *); 153 static int smu_fan_update_rpm(struct smu_fan *); 154 static int smu_fan_get_rpm(struct smu_fan *, int *); 155 static int smu_fan_set_rpm(struct smu_fan *, int); 156 static int smu_iicbus_acquire_bus(void *, int); 157 static void smu_iicbus_release_bus(void *, int); 158 static int smu_iicbus_exec(void *, i2c_op_t, i2c_addr_t, const void *, 159 size_t, void *, size_t, int); 160 static int smu_sysctl_fan_rpm(SYSCTLFN_ARGS); 161 162 static void smu_setup_zones(struct smu_softc *); 163 static void smu_adjust_zone(struct smu_softc *, int); 164 static void smu_adjust(void *); 165 static bool is_cpu_sensor(const envsys_data_t *); 166 static bool is_drive_sensor(const envsys_data_t *); 167 static bool is_slots_sensor(const envsys_data_t *); 168 169 CFATTACH_DECL_NEW(smu, sizeof(struct smu_softc), 170 smu_match, smu_attach, NULL, NULL); 171 172 static struct smu_softc *smu0 = NULL; 173 174 static int 175 smu_match(device_t parent, struct cfdata *cf, void *aux) 176 { 177 struct confargs *ca = aux; 178 179 if (strcmp(ca->ca_name, "smu") == 0) 180 return 5; 181 182 return 0; 183 } 184 185 static void 186 smu_attach(device_t parent, device_t self, void *aux) 187 { 188 struct confargs *ca = aux; 189 struct smu_softc *sc = device_private(self); 190 191 sc->sc_dev = self; 192 sc->sc_node = ca->ca_node; 193 194 sysctl_createv(NULL, 0, NULL, (void *) &sc->sc_sysctl_me, 195 CTLFLAG_READWRITE, 196 CTLTYPE_NODE, device_xname(sc->sc_dev), NULL, 197 NULL, 0, NULL, 0, 198 CTL_MACHDEP, CTL_CREATE, CTL_EOL); 199 200 if (smu_setup_doorbell(sc) != 0) { 201 aprint_normal(": unable to set up doorbell\n"); 202 return; 203 } 204 205 smu_setup_fans(sc); 206 smu_setup_iicbus(sc); 207 208 sc->sc_todr.todr_gettime_ymdhms = smu_todr_gettime_ymdhms; 209 sc->sc_todr.todr_settime_ymdhms = smu_todr_settime_ymdhms; 210 sc->sc_todr.cookie = sc; 211 todr_attach(&sc->sc_todr); 212 213 smu_setup_sme(sc); 214 215 if (smu0 == NULL) 216 smu0 = sc; 217 218 printf("\n"); 219 smu_setup_zones(sc); 220 } 221 222 static int 223 smu_setup_doorbell(struct smu_softc *sc) 224 { 225 int node, parent, reg[4], gpio_base, irq; 226 227 mutex_init(&sc->sc_cmd_lock, MUTEX_DEFAULT, IPL_NONE); 228 sc->sc_cmd = malloc(4096, M_DEVBUF, M_NOWAIT); 229 sc->sc_cmd_paddr = vtophys((vaddr_t) sc->sc_cmd); 230 231 DPRINTF("%s: cmd vaddr 0x%x paddr 0x%x\n", 232 __func__, (unsigned int) sc->sc_cmd, 233 (unsigned int) sc->sc_cmd_paddr); 234 235 if (OF_getprop(sc->sc_node, "platform-doorbell-buff", 236 &node, sizeof(node)) <= 0) 237 return -1; 238 239 if (OF_getprop(node, "platform-do-doorbell-buff", 240 reg, sizeof(reg)) < sizeof(reg)) 241 return -1; 242 243 sc->sc_dbell_mbox = reg[3]; 244 245 if (OF_getprop(sc->sc_node, "platform-doorbell-ack", 246 &node, sizeof(node)) <= 0) 247 return -1; 248 249 parent = OF_parent(node); 250 if (parent == 0) 251 return -1; 252 253 if (OF_getprop(parent, "reg", &gpio_base, sizeof(gpio_base)) <= 0) 254 return -1; 255 256 if (OF_getprop(node, "reg", reg, sizeof(reg)) <= 0) 257 return -1; 258 259 if (OF_getprop(node, "interrupts", &irq, sizeof(irq)) <= 0) 260 return -1; 261 262 sc->sc_dbell_gpio = gpio_base + reg[0]; 263 264 aprint_normal(" mbox 0x%x gpio 0x%x irq %d", 265 sc->sc_dbell_mbox, sc->sc_dbell_gpio, irq); 266 267 intr_establish(irq, IST_EDGE_FALLING, IPL_TTY, smu_dbell_gpio_intr, sc); 268 269 return 0; 270 } 271 272 static void 273 smu_setup_fans(struct smu_softc *sc) 274 { 275 struct smu_fan *fan; 276 struct sysctlnode *sysctl_fans, *sysctl_fan, *sysctl_node; 277 char type[32], sysctl_fan_name[32]; 278 int node, i, j; 279 280 node = of_getnode_byname(sc->sc_node, "fans"); 281 for (node = OF_child(node); 282 (node != 0) && (sc->sc_num_fans < SMU_MAX_FANS); 283 node = OF_peer(node)) { 284 fan = &sc->sc_fans[sc->sc_num_fans]; 285 fan->sc = sc; 286 287 memset(fan->location, 0, sizeof(fan->location)); 288 OF_getprop(node, "location", fan->location, 289 sizeof(fan->location)); 290 291 if (OF_getprop(node, "reg", &fan->reg, 292 sizeof(fan->reg)) <= 0) 293 continue; 294 295 if (OF_getprop(node, "zone", &fan->zone, 296 sizeof(fan->zone)) <= 0) 297 continue; 298 299 memset(type, 0, sizeof(type)); 300 OF_getprop(node, "device_type", type, sizeof(type)); 301 if (strcmp(type, "fan-rpm-control") == 0) 302 fan->rpm_ctl = 1; 303 else 304 fan->rpm_ctl = 0; 305 306 if (OF_getprop(node, "min-value", &fan->min_rpm, 307 sizeof(fan->min_rpm)) <= 0) 308 fan->min_rpm = 0; 309 310 if (OF_getprop(node, "max-value", &fan->max_rpm, 311 sizeof(fan->max_rpm)) <= 0) 312 fan->max_rpm = 0xffff; 313 314 if (OF_getprop(node, "unmanage-value", &fan->default_rpm, 315 sizeof(fan->default_rpm)) <= 0) 316 fan->default_rpm = fan->max_rpm; 317 318 DPRINTF("fan: location %s reg %x zone %d rpm_ctl %d " 319 "min_rpm %d max_rpm %d default_rpm %d\n", 320 fan->location, fan->reg, fan->zone, fan->rpm_ctl, 321 fan->min_rpm, fan->max_rpm, fan->default_rpm); 322 323 sc->sc_num_fans++; 324 } 325 326 for (i = 0; i < sc->sc_num_fans; i++) { 327 fan = &sc->sc_fans[i]; 328 smu_fan_set_rpm(fan, fan->default_rpm); 329 smu_fan_get_rpm(fan, &fan->current_rpm); 330 } 331 332 /* Create sysctl nodes for each fan */ 333 334 sysctl_createv(NULL, 0, NULL, (void *) &sysctl_fans, 335 CTLFLAG_READWRITE | CTLFLAG_OWNDESC, 336 CTLTYPE_NODE, "fans", NULL, 337 NULL, 0, NULL, 0, 338 CTL_MACHDEP, 339 sc->sc_sysctl_me->sysctl_num, 340 CTL_CREATE, CTL_EOL); 341 342 for (i = 0; i < sc->sc_num_fans; i++) { 343 fan = &sc->sc_fans[i]; 344 345 for (j = 0; j < strlen(fan->location); j++) { 346 sysctl_fan_name[j] = tolower(fan->location[j]); 347 if (sysctl_fan_name[j] == ' ') 348 sysctl_fan_name[j] = '_'; 349 } 350 sysctl_fan_name[j] = '\0'; 351 352 sysctl_createv(NULL, 0, NULL, (void *) &sysctl_fan, 353 CTLFLAG_READWRITE | CTLFLAG_OWNDESC, 354 CTLTYPE_NODE, sysctl_fan_name, "fan information", 355 NULL, 0, NULL, 0, 356 CTL_MACHDEP, 357 sc->sc_sysctl_me->sysctl_num, 358 sysctl_fans->sysctl_num, 359 CTL_CREATE, CTL_EOL); 360 361 sysctl_createv(NULL, 0, NULL, (void *) &sysctl_node, 362 CTLFLAG_READONLY | CTLFLAG_OWNDESC, 363 CTLTYPE_INT, "zone", "fan zone", 364 NULL, 0, &fan->zone, 0, 365 CTL_MACHDEP, 366 sc->sc_sysctl_me->sysctl_num, 367 sysctl_fans->sysctl_num, 368 sysctl_fan->sysctl_num, 369 CTL_CREATE, CTL_EOL); 370 371 sysctl_createv(NULL, 0, NULL, (void *) &sysctl_node, 372 CTLFLAG_READONLY | CTLFLAG_OWNDESC, 373 CTLTYPE_INT, "min_rpm", "fan minimum rpm", 374 NULL, 0, &fan->min_rpm, 0, 375 CTL_MACHDEP, 376 sc->sc_sysctl_me->sysctl_num, 377 sysctl_fans->sysctl_num, 378 sysctl_fan->sysctl_num, 379 CTL_CREATE, CTL_EOL); 380 381 sysctl_createv(NULL, 0, NULL, (void *) &sysctl_node, 382 CTLFLAG_READONLY | CTLFLAG_OWNDESC, 383 CTLTYPE_INT, "max_rpm", "fan maximum rpm", 384 NULL, 0, &fan->max_rpm, 0, 385 CTL_MACHDEP, 386 sc->sc_sysctl_me->sysctl_num, 387 sysctl_fans->sysctl_num, 388 sysctl_fan->sysctl_num, 389 CTL_CREATE, CTL_EOL); 390 391 sysctl_createv(NULL, 0, NULL, (void *) &sysctl_node, 392 CTLFLAG_READONLY | CTLFLAG_OWNDESC, 393 CTLTYPE_INT, "default_rpm", "fan default rpm", 394 NULL, 0, &fan->default_rpm, 0, 395 CTL_MACHDEP, 396 sc->sc_sysctl_me->sysctl_num, 397 sysctl_fans->sysctl_num, 398 sysctl_fan->sysctl_num, 399 CTL_CREATE, CTL_EOL); 400 401 sysctl_createv(NULL, 0, NULL, (void *) &sysctl_node, 402 CTLFLAG_READWRITE | CTLFLAG_OWNDESC, 403 CTLTYPE_INT, "rpm", "fan current rpm", 404 smu_sysctl_fan_rpm, 0, (void *) fan, 0, 405 CTL_MACHDEP, 406 sc->sc_sysctl_me->sysctl_num, 407 sysctl_fans->sysctl_num, 408 sysctl_fan->sysctl_num, 409 CTL_CREATE, CTL_EOL); 410 } 411 } 412 413 static void 414 smu_setup_iicbus(struct smu_softc *sc) 415 { 416 struct smu_iicbus *iicbus; 417 struct i2c_controller *i2c; 418 struct smu_iicbus_confargs ca; 419 int node; 420 char name[32]; 421 422 mutex_init(&sc->sc_iicbus_lock, MUTEX_DEFAULT, IPL_NONE); 423 424 node = of_getnode_byname(sc->sc_node, "smu-i2c-control"); 425 for (node = OF_child(node); 426 (node != 0) && (sc->sc_num_iicbus < SMU_MAX_IICBUS); 427 node = OF_peer(node)) { 428 memset(name, 0, sizeof(name)); 429 OF_getprop(node, "name", name, sizeof(name)); 430 if (strcmp(name, "i2c-bus") != 0) 431 continue; 432 433 iicbus = &sc->sc_iicbus[sc->sc_num_iicbus]; 434 iicbus->sc = sc; 435 i2c = &iicbus->i2c; 436 437 if (OF_getprop(node, "reg", &iicbus->reg, sizeof(iicbus->reg)) <= 0) 438 continue; 439 440 DPRINTF("iicbus: reg %x\n", iicbus->reg); 441 442 i2c->ic_cookie = iicbus; 443 i2c->ic_acquire_bus = smu_iicbus_acquire_bus; 444 i2c->ic_release_bus = smu_iicbus_release_bus; 445 i2c->ic_send_start = NULL; 446 i2c->ic_send_stop = NULL; 447 i2c->ic_initiate_xfer = NULL; 448 i2c->ic_read_byte = NULL; 449 i2c->ic_write_byte = NULL; 450 i2c->ic_exec = smu_iicbus_exec; 451 452 ca.ca_name = name; 453 ca.ca_node = node; 454 ca.ca_tag = i2c; 455 config_found_ia(sc->sc_dev, "smu", &ca, smu_iicbus_print); 456 457 sc->sc_num_iicbus++; 458 } 459 } 460 461 static void 462 smu_setup_sme(struct smu_softc *sc) 463 { 464 struct smu_fan *fan; 465 envsys_data_t *sme_sensor; 466 int i; 467 468 sc->sc_sme = sysmon_envsys_create(); 469 470 for (i = 0; i < sc->sc_num_fans; i++) { 471 sme_sensor = &sc->sc_sme_sensors[i]; 472 fan = &sc->sc_fans[i]; 473 474 sme_sensor->units = ENVSYS_SFANRPM; 475 sme_sensor->state = ENVSYS_SINVALID; 476 snprintf(sme_sensor->desc, sizeof(sme_sensor->desc), 477 "%s", fan->location); 478 479 if (sysmon_envsys_sensor_attach(sc->sc_sme, sme_sensor)) { 480 sysmon_envsys_destroy(sc->sc_sme); 481 return; 482 } 483 } 484 485 sc->sc_sme->sme_name = device_xname(sc->sc_dev); 486 sc->sc_sme->sme_cookie = sc; 487 sc->sc_sme->sme_refresh = smu_sme_refresh; 488 489 if (sysmon_envsys_register(sc->sc_sme)) { 490 aprint_error_dev(sc->sc_dev, 491 "unable to register with sysmon\n"); 492 sysmon_envsys_destroy(sc->sc_sme); 493 } 494 } 495 496 static int 497 smu_iicbus_print(void *aux, const char *smu) 498 { 499 struct smu_iicbus_confargs *ca = aux; 500 501 if (smu) 502 aprint_normal("%s at %s", ca->ca_name, smu); 503 504 return UNCONF; 505 } 506 507 static void 508 smu_sme_refresh(struct sysmon_envsys *sme, envsys_data_t *edata) 509 { 510 struct smu_softc *sc = sme->sme_cookie; 511 struct smu_fan *fan; 512 int which = edata->sensor; 513 int ret; 514 515 edata->state = ENVSYS_SINVALID; 516 517 if (which < sc->sc_num_fans) { 518 fan = &sc->sc_fans[which]; 519 520 ret = smu_fan_get_rpm(fan, &fan->current_rpm); 521 if (ret == 0) { 522 edata->value_cur = fan->current_rpm; 523 edata->state = ENVSYS_SVALID; 524 } 525 } 526 } 527 528 static int 529 smu_do_cmd(struct smu_softc *sc, struct smu_cmd *cmd, int timo) 530 { 531 int gpio, ret, bail; 532 u_char ack; 533 534 mutex_enter(&sc->sc_cmd_lock); 535 536 DPRINTF("%s: cmd %02x len %02x\n", __func__, cmd->cmd, cmd->len); 537 DPRINTF("%s: data %02x %02x %02x %02x %02x %02x %02x %02x\n", __func__, 538 cmd->data[0], cmd->data[1], cmd->data[2], cmd->data[3], 539 cmd->data[4], cmd->data[5], cmd->data[6], cmd->data[7]); 540 541 sc->sc_cmd->cmd = cmd->cmd; 542 sc->sc_cmd->len = cmd->len; 543 memcpy(sc->sc_cmd->data, cmd->data, cmd->len); 544 545 __asm volatile ("dcbf 0,%0; sync" :: "r"(sc->sc_cmd) : "memory"); 546 547 obio_write_4(sc->sc_dbell_mbox, sc->sc_cmd_paddr); 548 obio_write_1(sc->sc_dbell_gpio, 0x04); 549 550 bail = 0; 551 552 gpio = obio_read_1(sc->sc_dbell_gpio); 553 554 while (((gpio & 0x07) != 0x07) && (bail < timo)) { 555 ret = tsleep(sc->sc_cmd, PWAIT, "smu_cmd", mstohz(10)); 556 if (ret != 0) { 557 bail++; 558 } 559 gpio = obio_read_1(sc->sc_dbell_gpio); 560 } 561 562 if ((gpio & 0x07) != 0x07) { 563 mutex_exit(&sc->sc_cmd_lock); 564 return EWOULDBLOCK; 565 } 566 567 __asm volatile ("dcbf 0,%0; sync" :: "r"(sc->sc_cmd) : "memory"); 568 569 ack = (~cmd->cmd) & 0xff; 570 if (sc->sc_cmd->cmd != ack) { 571 DPRINTF("%s: invalid ack, got %x expected %x\n", 572 __func__, sc->sc_cmd->cmd, ack); 573 mutex_exit(&sc->sc_cmd_lock); 574 return EIO; 575 } 576 577 cmd->cmd = sc->sc_cmd->cmd; 578 cmd->len = sc->sc_cmd->len; 579 memcpy(cmd->data, sc->sc_cmd->data, sc->sc_cmd->len); 580 581 mutex_exit(&sc->sc_cmd_lock); 582 583 return 0; 584 } 585 586 587 static int 588 smu_dbell_gpio_intr(void *arg) 589 { 590 struct smu_softc *sc = arg; 591 592 DPRINTF("%s\n", __func__); 593 594 wakeup(sc->sc_cmd); 595 596 return 1; 597 } 598 599 void 600 smu_poweroff(void) 601 { 602 struct smu_cmd cmd; 603 604 if (smu0 == NULL) 605 return; 606 607 cmd.cmd = SMU_CMD_POWER; 608 strcpy(cmd.data, "SHUTDOWN"); 609 cmd.len = strlen(cmd.data) + 1; 610 smu_do_cmd(smu0, &cmd, 800); 611 612 for (;;); 613 } 614 615 void 616 smu_restart(void) 617 { 618 struct smu_cmd cmd; 619 620 if (smu0 == NULL) 621 return; 622 623 cmd.cmd = SMU_CMD_POWER; 624 strcpy(cmd.data, "RESTART"); 625 cmd.len = strlen(cmd.data) + 1; 626 smu_do_cmd(smu0, &cmd, 800); 627 628 for (;;); 629 } 630 631 static int 632 smu_todr_gettime_ymdhms(todr_chip_handle_t tch, struct clock_ymdhms *dt) 633 { 634 struct smu_softc *sc = tch->cookie; 635 struct smu_cmd cmd; 636 int ret; 637 638 cmd.cmd = SMU_CMD_RTC; 639 cmd.len = 1; 640 cmd.data[0] = 0x81; 641 642 ret = smu_do_cmd(sc, &cmd, 800); 643 if (ret != 0) 644 return ret; 645 646 dt->dt_sec = bcdtobin(cmd.data[0]); 647 dt->dt_min = bcdtobin(cmd.data[1]); 648 dt->dt_hour = bcdtobin(cmd.data[2]); 649 dt->dt_wday = bcdtobin(cmd.data[3]); 650 dt->dt_day = bcdtobin(cmd.data[4]); 651 dt->dt_mon = bcdtobin(cmd.data[5]); 652 dt->dt_year = bcdtobin(cmd.data[6]) + 2000; 653 654 return 0; 655 } 656 657 static int 658 smu_todr_settime_ymdhms(todr_chip_handle_t tch, struct clock_ymdhms *dt) 659 { 660 struct smu_softc *sc = tch->cookie; 661 struct smu_cmd cmd; 662 663 cmd.cmd = SMU_CMD_RTC; 664 cmd.len = 8; 665 cmd.data[0] = 0x80; 666 cmd.data[1] = bintobcd(dt->dt_sec); 667 cmd.data[2] = bintobcd(dt->dt_min); 668 cmd.data[3] = bintobcd(dt->dt_hour); 669 cmd.data[4] = bintobcd(dt->dt_wday); 670 cmd.data[5] = bintobcd(dt->dt_day); 671 cmd.data[6] = bintobcd(dt->dt_mon); 672 cmd.data[7] = bintobcd(dt->dt_year - 2000); 673 674 return smu_do_cmd(sc, &cmd, 800); 675 } 676 677 static int 678 smu_fan_update_rpm(struct smu_fan *fan) 679 { 680 struct smu_softc *sc = fan->sc; 681 struct smu_cmd cmd; 682 int ret; 683 684 cmd.cmd = SMU_CMD_FAN; 685 cmd.len = 2; 686 cmd.data[0] = 0x31; 687 cmd.data[1] = fan->reg; 688 689 ret = smu_do_cmd(sc, &cmd, 800); 690 if (ret == 0) { 691 fan->last_update = time_uptime; 692 fan->current_rpm = (cmd.data[0] << 8) | cmd.data[1]; 693 } else { 694 cmd.cmd = SMU_CMD_FAN; 695 cmd.len = 1; 696 cmd.data[0] = 0x01; 697 698 ret = smu_do_cmd(sc, &cmd, 800); 699 if (ret == 0) { 700 fan->last_update = time_uptime; 701 fan->current_rpm = (cmd.data[1 + fan->reg * 2] << 8) | 702 cmd.data[2 + fan->reg * 2]; 703 } 704 } 705 706 return ret; 707 } 708 709 static int 710 smu_fan_get_rpm(struct smu_fan *fan, int *rpm) 711 { 712 int ret; 713 714 if (time_uptime - fan->last_update > 1) { 715 ret = smu_fan_update_rpm(fan); 716 if (ret != 0) 717 return ret; 718 } 719 720 *rpm = fan->current_rpm; 721 722 return ret; 723 } 724 725 static int 726 smu_fan_set_rpm(struct smu_fan *fan, int rpm) 727 { 728 struct smu_softc *sc = fan->sc; 729 struct smu_cmd cmd; 730 int ret; 731 732 DPRINTF("%s: fan %s rpm %d\n", __func__, fan->location, rpm); 733 734 rpm = max(fan->min_rpm, rpm); 735 rpm = min(fan->max_rpm, rpm); 736 737 cmd.cmd = SMU_CMD_FAN; 738 cmd.len = 4; 739 cmd.data[0] = 0x30; 740 cmd.data[1] = fan->reg; 741 cmd.data[2] = (rpm >> 8) & 0xff; 742 cmd.data[3] = rpm & 0xff; 743 744 ret = smu_do_cmd(sc, &cmd, 800); 745 if (ret != 0) { 746 cmd.cmd = SMU_CMD_FAN; 747 cmd.len = 14; 748 cmd.data[0] = fan->rpm_ctl ? 0x00 : 0x10; 749 cmd.data[1] = 1 << fan->reg; 750 cmd.data[2] = cmd.data[2 + fan->reg * 2] = (rpm >> 8) & 0xff; 751 cmd.data[3] = cmd.data[3 + fan->reg * 2] = rpm & 0xff; 752 753 ret = smu_do_cmd(sc, &cmd, 800); 754 } 755 756 return ret; 757 } 758 759 static int 760 smu_iicbus_acquire_bus(void *cookie, int flags) 761 { 762 struct smu_iicbus *iicbus = cookie; 763 struct smu_softc *sc = iicbus->sc; 764 765 mutex_enter(&sc->sc_iicbus_lock); 766 767 return 0; 768 } 769 770 static void 771 smu_iicbus_release_bus(void *cookie, int flags) 772 { 773 struct smu_iicbus *iicbus = cookie; 774 struct smu_softc *sc = iicbus->sc; 775 776 mutex_exit(&sc->sc_iicbus_lock); 777 } 778 779 static int 780 smu_iicbus_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *send, 781 size_t send_len, void *recv, size_t recv_len, int flags) 782 { 783 struct smu_iicbus *iicbus = cookie; 784 struct smu_softc *sc = iicbus->sc; 785 struct smu_cmd cmd; 786 int retries, ret; 787 788 DPRINTF("%s: op %x addr %x send_len %d recv_len %d\n", 789 __func__, op, addr, send_len, recv_len); 790 791 cmd.cmd = SMU_CMD_I2C; 792 cmd.len = 9 + recv_len; 793 cmd.data[0] = iicbus->reg; 794 cmd.data[1] = I2C_OP_READ_P(op) ? 0x02 : 0x00; 795 cmd.data[2] = addr; 796 cmd.data[3] = send_len; 797 memcpy(&cmd.data[4], send, send_len); 798 cmd.data[7] = addr; 799 if (I2C_OP_READ_P(op)) 800 cmd.data[7] |= 0x01; 801 cmd.data[8] = recv_len; 802 memcpy(&cmd.data[9], recv, recv_len); 803 804 ret = smu_do_cmd(sc, &cmd, 800); 805 if (ret != 0) 806 return (ret); 807 808 for (retries = 0; retries < 10; retries++) { 809 cmd.cmd = SMU_CMD_I2C; 810 cmd.len = 1; 811 cmd.data[0] = 0x00; 812 memset(&cmd.data[1], 0xff, recv_len); 813 814 ret = smu_do_cmd(sc, &cmd, 800); 815 816 DPRINTF("%s: cmd data[0] %x\n", __func__, cmd.data[0]); 817 818 if (ret == 0 && (cmd.data[0] & 0x80) == 0) 819 break; 820 821 DELAY(10000); 822 } 823 824 if (cmd.data[0] & 0x80) 825 return EIO; 826 827 if (I2C_OP_READ_P(op)) 828 memcpy(recv, &cmd.data[1], recv_len); 829 830 return 0; 831 } 832 833 static int 834 smu_sysctl_fan_rpm(SYSCTLFN_ARGS) 835 { 836 struct sysctlnode node = *rnode; 837 struct smu_fan *fan = node.sysctl_data; 838 int rpm = 0; 839 int ret; 840 841 node.sysctl_data = &rpm; 842 843 if (newp) { 844 if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) { 845 rpm = *(int *) node.sysctl_data; 846 return smu_fan_set_rpm(fan, rpm); 847 } 848 return EINVAL; 849 } else { 850 ret = smu_fan_get_rpm(fan, &rpm); 851 if (ret != 0) 852 return (ret); 853 854 return sysctl_lookup(SYSCTLFN_CALL(&node)); 855 } 856 857 return 0; 858 } 859 860 SYSCTL_SETUP(smu_sysctl_setup, "SMU sysctl subtree setup") 861 { 862 sysctl_createv(NULL, 0, NULL, NULL, 863 CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep", NULL, 864 NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL); 865 } 866 867 static void 868 smu_setup_zones(struct smu_softc *sc) 869 { 870 struct smu_zone *z; 871 struct smu_fan *f; 872 int i; 873 874 /* find CPU fans */ 875 z = &sc->sc_zones[SMU_ZONE_CPUS]; 876 z->nfans = 0; 877 for (i = 0; i < SMU_MAX_FANS; i++) { 878 f = &sc->sc_fans[i]; 879 if (strstr(f->location, "CPU") != NULL) { 880 z->fans[z->nfans] = i; 881 z->nfans++; 882 } 883 } 884 printf("using %d fans for CPU zone\n", z->nfans); 885 z->threshold = C_TO_uK(45); 886 z->duty = 150; 887 z->step = 3; 888 z->filter = is_cpu_sensor; 889 890 z = &sc->sc_zones[SMU_ZONE_DRIVES]; 891 z->nfans = 0; 892 for (i = 0; i < SMU_MAX_FANS; i++) { 893 f = &sc->sc_fans[i]; 894 if (strstr(f->location, "DRIVE") != NULL) { 895 z->fans[z->nfans] = i; 896 z->nfans++; 897 } 898 } 899 printf("using %d fans for drive bay zone\n", z->nfans); 900 z->threshold = C_TO_uK(40); 901 z->duty = 150; 902 z->step = 2; 903 z->filter = is_drive_sensor; 904 905 z = &sc->sc_zones[SMU_ZONE_SLOTS]; 906 z->nfans = 0; 907 for (i = 0; i < SMU_MAX_FANS; i++) { 908 f = &sc->sc_fans[i]; 909 if ((strstr(f->location, "BACKSIDE") != NULL) || 910 (strstr(f->location, "SLOTS") != NULL)) { 911 z->fans[z->nfans] = i; 912 z->nfans++; 913 } 914 } 915 printf("using %d fans for expansion slots zone\n", z->nfans); 916 z->threshold = C_TO_uK(40); 917 z->duty = 150; 918 z->step = 2; 919 z->filter = is_slots_sensor; 920 921 sc->sc_dying = false; 922 kthread_create(PRI_NONE, 0, curcpu(), smu_adjust, sc, &sc->sc_thread, 923 "fan control"); 924 } 925 926 static void 927 smu_adjust_zone(struct smu_softc *sc, int which) 928 { 929 struct smu_zone *z = &sc->sc_zones[which]; 930 struct smu_fan *f; 931 long temp, newduty, i, speed, diff; 932 933 DPRINTF("%s %d\n", __func__, which); 934 935 temp = sysmon_envsys_get_max_value(z->filter, true); 936 if (temp == 0) { 937 /* no sensor data - leave fan alone */ 938 DPRINTF("nodata\n"); 939 return; 940 } 941 DPRINTF("temp %ld ", (temp - 273150000) / 1000000); 942 diff = ((temp - z->threshold) / 1000000) * z->step; 943 944 if (diff < 0) newduty = 0; 945 else if (diff > 100) newduty = 100; 946 else newduty = diff; 947 948 DPRINTF("newduty %ld diff %ld \n", newduty, diff); 949 if (newduty == z->duty) { 950 DPRINTF("no change\n"); 951 return; 952 } 953 z->duty = newduty; 954 /* now adjust each fan to the new duty cycle */ 955 for (i = 0; i < z->nfans; i++) { 956 f = &sc->sc_fans[z->fans[i]]; 957 speed = f->min_rpm + ((f->max_rpm - f->min_rpm) * newduty) / 100; 958 DPRINTF("fan %d speed %ld ", z->fans[i], speed); 959 smu_fan_set_rpm(f, speed); 960 } 961 DPRINTF("\n"); 962 } 963 964 static void 965 smu_adjust(void *cookie) 966 { 967 struct smu_softc *sc = cookie; 968 int i; 969 970 while (!sc->sc_dying) { 971 for (i = 0; i < SMU_ZONES; i++) 972 smu_adjust_zone(sc, i); 973 kpause("fanctrl", true, mstohz(30000), NULL); 974 } 975 kthread_exit(0); 976 } 977 978 static bool is_cpu_sensor(const envsys_data_t *edata) 979 { 980 if (edata->units != ENVSYS_STEMP) 981 return false; 982 if ((strstr(edata->desc, "CPU") != NULL) && 983 (strstr(edata->desc, "DIODE") != NULL)) 984 return TRUE; 985 if (strstr(edata->desc, "TUNNEL") != NULL) 986 return TRUE; 987 return false; 988 } 989 990 static bool is_drive_sensor(const envsys_data_t *edata) 991 { 992 if (edata->units != ENVSYS_STEMP) 993 return false; 994 if (strstr(edata->desc, "DRIVE BAY") != NULL) 995 return TRUE; 996 return false; 997 } 998 999 static bool is_slots_sensor(const envsys_data_t *edata) 1000 { 1001 if (edata->units != ENVSYS_STEMP) 1002 return false; 1003 if (strstr(edata->desc, "BACKSIDE") != NULL) 1004 return TRUE; 1005 if (strstr(edata->desc, "INLET") != NULL) 1006 return TRUE; 1007 return false; 1008 } 1009