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 + 8) 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 int sc_num_iicbus; 117 struct smu_iicbus sc_iicbus[SMU_MAX_IICBUS]; 118 119 struct todr_chip_handle sc_todr; 120 121 struct sysmon_envsys *sc_sme; 122 envsys_data_t sc_sme_sensors[SMU_MAX_SME_SENSORS]; 123 uint32_t cpu_m; 124 int32_t cpu_b; 125 126 struct smu_zone sc_zones[SMU_ZONES]; 127 lwp_t *sc_thread; 128 bool sc_dying; 129 }; 130 131 #define SMU_CMD_FAN 0x4a 132 #define SMU_CMD_RTC 0x8e 133 #define SMU_CMD_I2C 0x9a 134 #define SMU_CMD_POWER 0xaa 135 #define SMU_CMD_ADC 0xd8 136 #define SMU_MISC 0xee 137 #define SMU_MISC_GET_DATA 0x02 138 #define SMU_MISC_LED_CTRL 0x04 139 140 #define SMU_CPUTEMP_CAL 0x18 141 #define SMU_CPUVOLT_CAL 0x21 142 #define SMU_SLOTPW_CAL 0x78 143 144 #define SMU_PARTITION 0x3e 145 #define SMU_PARTITION_LATEST 0x01 146 #define SMU_PARTITION_BASE 0x02 147 #define SMU_PARTITION_UPDATE 0x03 148 149 #ifdef SMU_DEBUG 150 #define DPRINTF printf 151 #else 152 #define DPRINTF while (0) printf 153 #endif 154 155 static int smu_match(device_t, struct cfdata *, void *); 156 static void smu_attach(device_t, device_t, void *); 157 static int smu_setup_doorbell(struct smu_softc *); 158 static void smu_setup_fans(struct smu_softc *); 159 static void smu_setup_iicbus(struct smu_softc *); 160 static void smu_setup_sme(struct smu_softc *); 161 static int smu_iicbus_print(void *, const char *); 162 static void smu_sme_refresh(struct sysmon_envsys *, envsys_data_t *); 163 static int smu_do_cmd(struct smu_softc *, struct smu_cmd *, int); 164 static int smu_dbell_gpio_intr(void *); 165 static int smu_todr_gettime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *); 166 static int smu_todr_settime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *); 167 static int smu_fan_update_rpm(struct smu_fan *); 168 static int smu_fan_get_rpm(struct smu_fan *, int *); 169 static int smu_fan_set_rpm(struct smu_fan *, int); 170 static int smu_read_adc(struct smu_softc *, int); 171 172 static int smu_iicbus_exec(void *, i2c_op_t, i2c_addr_t, const void *, 173 size_t, void *, size_t, int); 174 static int smu_sysctl_fan_rpm(SYSCTLFN_ARGS); 175 176 static void smu_setup_zones(struct smu_softc *); 177 static void smu_adjust_zone(struct smu_softc *, int); 178 static void smu_adjust(void *); 179 static bool is_cpu_sensor(const envsys_data_t *); 180 static bool is_drive_sensor(const envsys_data_t *); 181 static bool is_slots_sensor(const envsys_data_t *); 182 183 int smu_get_datablock(int, uint8_t *, size_t); 184 185 CFATTACH_DECL_NEW(smu, sizeof(struct smu_softc), 186 smu_match, smu_attach, NULL, NULL); 187 188 static struct smu_softc *smu0 = NULL; 189 190 static int 191 smu_match(device_t parent, struct cfdata *cf, void *aux) 192 { 193 struct confargs *ca = aux; 194 195 if (strcmp(ca->ca_name, "smu") == 0) 196 return 5; 197 198 return 0; 199 } 200 201 static void 202 smu_attach(device_t parent, device_t self, void *aux) 203 { 204 struct confargs *ca = aux; 205 struct smu_softc *sc = device_private(self); 206 uint16_t data[4]; 207 208 sc->sc_dev = self; 209 sc->sc_node = ca->ca_node; 210 211 if (smu0 == NULL) 212 smu0 = sc; 213 214 sysctl_createv(NULL, 0, NULL, (void *) &sc->sc_sysctl_me, 215 CTLFLAG_READWRITE, 216 CTLTYPE_NODE, device_xname(sc->sc_dev), NULL, 217 NULL, 0, NULL, 0, 218 CTL_MACHDEP, CTL_CREATE, CTL_EOL); 219 220 if (smu_setup_doorbell(sc) != 0) { 221 aprint_normal(": unable to set up doorbell\n"); 222 return; 223 } 224 225 aprint_normal("\n"); 226 227 smu_setup_fans(sc); 228 smu_setup_iicbus(sc); 229 230 sc->sc_todr.todr_gettime_ymdhms = smu_todr_gettime_ymdhms; 231 sc->sc_todr.todr_settime_ymdhms = smu_todr_settime_ymdhms; 232 sc->sc_todr.cookie = sc; 233 todr_attach(&sc->sc_todr); 234 235 /* calibration data */ 236 memset(data, 0, 8); 237 smu_get_datablock(SMU_CPUTEMP_CAL, (void *)data, 8); 238 DPRINTF("data %04x %04x %04x %04x\n", data[0], data[1], data[2], data[3]); 239 sc->cpu_m = data[2]; 240 sc->cpu_b = (int16_t)data[3]; 241 242 smu_setup_sme(sc); 243 244 smu_setup_zones(sc); 245 } 246 247 static int 248 smu_setup_doorbell(struct smu_softc *sc) 249 { 250 int node, parent, reg[4], gpio_base, irq; 251 252 mutex_init(&sc->sc_cmd_lock, MUTEX_DEFAULT, IPL_NONE); 253 sc->sc_cmd = malloc(4096, M_DEVBUF, M_WAITOK); 254 sc->sc_cmd_paddr = vtophys((vaddr_t) sc->sc_cmd); 255 256 DPRINTF("%s: cmd vaddr 0x%x paddr 0x%x\n", 257 __func__, (unsigned int) sc->sc_cmd, 258 (unsigned int) sc->sc_cmd_paddr); 259 260 if (OF_getprop(sc->sc_node, "platform-doorbell-buff", 261 &node, sizeof(node)) <= 0) 262 return -1; 263 264 if (OF_getprop(node, "platform-do-doorbell-buff", 265 reg, sizeof(reg)) < sizeof(reg)) 266 return -1; 267 268 sc->sc_dbell_mbox = reg[3]; 269 270 if (OF_getprop(sc->sc_node, "platform-doorbell-ack", 271 &node, sizeof(node)) <= 0) 272 return -1; 273 274 parent = OF_parent(node); 275 if (parent == 0) 276 return -1; 277 278 if (OF_getprop(parent, "reg", &gpio_base, sizeof(gpio_base)) <= 0) 279 return -1; 280 281 if (OF_getprop(node, "reg", reg, sizeof(reg)) <= 0) 282 return -1; 283 284 if (OF_getprop(node, "interrupts", &irq, sizeof(irq)) <= 0) 285 return -1; 286 287 sc->sc_dbell_gpio = gpio_base + reg[0]; 288 289 aprint_normal(" mbox 0x%x gpio 0x%x irq %d", 290 sc->sc_dbell_mbox, sc->sc_dbell_gpio, irq); 291 292 intr_establish_xname(irq, IST_EDGE_FALLING, IPL_TTY, 293 smu_dbell_gpio_intr, sc, device_xname(sc->sc_dev)); 294 295 return 0; 296 } 297 298 static void 299 smu_setup_fans(struct smu_softc *sc) 300 { 301 struct smu_fan *fan; 302 struct sysctlnode *sysctl_fans, *sysctl_fan, *sysctl_node; 303 char type[32], sysctl_fan_name[32]; 304 int node, i, j; 305 const char *fans[] = { "fans", "rpm-fans", 0 }; 306 int n = 0; 307 308 while (fans[n][0] != 0) { 309 node = of_getnode_byname(sc->sc_node, fans[n]); 310 for (node = OF_child(node); 311 (node != 0) && (sc->sc_num_fans < SMU_MAX_FANS); 312 node = OF_peer(node)) { 313 fan = &sc->sc_fans[sc->sc_num_fans]; 314 fan->sc = sc; 315 316 memset(fan->location, 0, sizeof(fan->location)); 317 OF_getprop(node, "location", fan->location, 318 sizeof(fan->location)); 319 320 if (OF_getprop(node, "reg", &fan->reg, 321 sizeof(fan->reg)) <= 0) 322 continue; 323 324 if (OF_getprop(node, "zone", &fan->zone , 325 sizeof(fan->zone)) <= 0) 326 continue; 327 328 memset(type, 0, sizeof(type)); 329 OF_getprop(node, "device_type", type, sizeof(type)); 330 if (strcmp(type, "fan-rpm-control") == 0) 331 fan->rpm_ctl = 1; 332 else 333 fan->rpm_ctl = 0; 334 335 if (OF_getprop(node, "min-value", &fan->min_rpm, 336 sizeof(fan->min_rpm)) <= 0) 337 fan->min_rpm = 0; 338 339 if (OF_getprop(node, "max-value", &fan->max_rpm, 340 sizeof(fan->max_rpm)) <= 0) 341 fan->max_rpm = 0xffff; 342 343 if (OF_getprop(node, "unmanage-value", &fan->default_rpm, 344 sizeof(fan->default_rpm)) <= 0) 345 fan->default_rpm = fan->max_rpm; 346 347 DPRINTF("fan: location %s reg %x zone %d rpm_ctl %d " 348 "min_rpm %d max_rpm %d default_rpm %d\n", 349 fan->location, fan->reg, fan->zone, fan->rpm_ctl, 350 fan->min_rpm, fan->max_rpm, fan->default_rpm); 351 352 sc->sc_num_fans++; 353 } 354 n++; 355 } 356 357 for (i = 0; i < sc->sc_num_fans; i++) { 358 fan = &sc->sc_fans[i]; 359 smu_fan_set_rpm(fan, fan->default_rpm); 360 smu_fan_get_rpm(fan, &fan->current_rpm); 361 } 362 363 /* Create sysctl nodes for each fan */ 364 365 sysctl_createv(NULL, 0, NULL, (void *) &sysctl_fans, 366 CTLFLAG_READWRITE | CTLFLAG_OWNDESC, 367 CTLTYPE_NODE, "fans", NULL, 368 NULL, 0, NULL, 0, 369 CTL_MACHDEP, 370 sc->sc_sysctl_me->sysctl_num, 371 CTL_CREATE, CTL_EOL); 372 373 for (i = 0; i < sc->sc_num_fans; i++) { 374 fan = &sc->sc_fans[i]; 375 376 for (j = 0; j < strlen(fan->location); j++) { 377 sysctl_fan_name[j] = tolower(fan->location[j]); 378 if (sysctl_fan_name[j] == ' ') 379 sysctl_fan_name[j] = '_'; 380 } 381 sysctl_fan_name[j] = '\0'; 382 383 sysctl_createv(NULL, 0, NULL, (void *) &sysctl_fan, 384 CTLFLAG_READWRITE | CTLFLAG_OWNDESC, 385 CTLTYPE_NODE, sysctl_fan_name, "fan information", 386 NULL, 0, NULL, 0, 387 CTL_MACHDEP, 388 sc->sc_sysctl_me->sysctl_num, 389 sysctl_fans->sysctl_num, 390 CTL_CREATE, CTL_EOL); 391 392 sysctl_createv(NULL, 0, NULL, (void *) &sysctl_node, 393 CTLFLAG_READONLY | CTLFLAG_OWNDESC, 394 CTLTYPE_INT, "zone", "fan zone", 395 NULL, 0, &fan->zone, 0, 396 CTL_MACHDEP, 397 sc->sc_sysctl_me->sysctl_num, 398 sysctl_fans->sysctl_num, 399 sysctl_fan->sysctl_num, 400 CTL_CREATE, CTL_EOL); 401 402 sysctl_createv(NULL, 0, NULL, (void *) &sysctl_node, 403 CTLFLAG_READONLY | CTLFLAG_OWNDESC, 404 CTLTYPE_INT, "min_rpm", "fan minimum rpm", 405 NULL, 0, &fan->min_rpm, 0, 406 CTL_MACHDEP, 407 sc->sc_sysctl_me->sysctl_num, 408 sysctl_fans->sysctl_num, 409 sysctl_fan->sysctl_num, 410 CTL_CREATE, CTL_EOL); 411 412 sysctl_createv(NULL, 0, NULL, (void *) &sysctl_node, 413 CTLFLAG_READONLY | CTLFLAG_OWNDESC, 414 CTLTYPE_INT, "max_rpm", "fan maximum rpm", 415 NULL, 0, &fan->max_rpm, 0, 416 CTL_MACHDEP, 417 sc->sc_sysctl_me->sysctl_num, 418 sysctl_fans->sysctl_num, 419 sysctl_fan->sysctl_num, 420 CTL_CREATE, CTL_EOL); 421 422 sysctl_createv(NULL, 0, NULL, (void *) &sysctl_node, 423 CTLFLAG_READONLY | CTLFLAG_OWNDESC, 424 CTLTYPE_INT, "default_rpm", "fan default rpm", 425 NULL, 0, &fan->default_rpm, 0, 426 CTL_MACHDEP, 427 sc->sc_sysctl_me->sysctl_num, 428 sysctl_fans->sysctl_num, 429 sysctl_fan->sysctl_num, 430 CTL_CREATE, CTL_EOL); 431 432 sysctl_createv(NULL, 0, NULL, (void *) &sysctl_node, 433 CTLFLAG_READWRITE | CTLFLAG_OWNDESC, 434 CTLTYPE_INT, "rpm", "fan current rpm", 435 smu_sysctl_fan_rpm, 0, (void *) fan, 0, 436 CTL_MACHDEP, 437 sc->sc_sysctl_me->sysctl_num, 438 sysctl_fans->sysctl_num, 439 sysctl_fan->sysctl_num, 440 CTL_CREATE, CTL_EOL); 441 } 442 } 443 444 static void 445 smu_setup_iicbus(struct smu_softc *sc) 446 { 447 struct smu_iicbus *iicbus; 448 struct i2c_controller *i2c; 449 struct smu_iicbus_confargs ca; 450 int node; 451 char name[32]; 452 453 node = of_getnode_byname(sc->sc_node, "smu-i2c-control"); 454 if (node == 0) node = sc->sc_node; 455 for (node = OF_child(node); 456 (node != 0) && (sc->sc_num_iicbus < SMU_MAX_IICBUS); 457 node = OF_peer(node)) { 458 memset(name, 0, sizeof(name)); 459 OF_getprop(node, "name", name, sizeof(name)); 460 if ((strcmp(name, "i2c-bus") != 0) && 461 (strcmp(name, "i2c") != 0)) 462 continue; 463 464 iicbus = &sc->sc_iicbus[sc->sc_num_iicbus]; 465 iicbus->sc = sc; 466 i2c = &iicbus->i2c; 467 468 if (OF_getprop(node, "reg", &iicbus->reg, sizeof(iicbus->reg)) <= 0) 469 continue; 470 471 DPRINTF("iicbus: reg %x\n", iicbus->reg); 472 473 iic_tag_init(i2c); 474 i2c->ic_cookie = iicbus; 475 i2c->ic_exec = smu_iicbus_exec; 476 477 ca.ca_name = name; 478 ca.ca_node = node; 479 ca.ca_tag = i2c; 480 config_found_ia(sc->sc_dev, "smu", &ca, smu_iicbus_print); 481 482 sc->sc_num_iicbus++; 483 } 484 } 485 486 static void 487 smu_setup_sme(struct smu_softc *sc) 488 { 489 struct smu_fan *fan; 490 envsys_data_t *sme_sensor; 491 int i, sensors, child, reg; 492 char loc[32], type[32]; 493 494 sc->sc_sme = sysmon_envsys_create(); 495 496 for (i = 0; i < sc->sc_num_fans; i++) { 497 sme_sensor = &sc->sc_sme_sensors[i]; 498 fan = &sc->sc_fans[i]; 499 500 sme_sensor->units = ENVSYS_SFANRPM; 501 sme_sensor->state = ENVSYS_SINVALID; 502 snprintf(sme_sensor->desc, sizeof(sme_sensor->desc), 503 "%s", fan->location); 504 505 if (sysmon_envsys_sensor_attach(sc->sc_sme, sme_sensor)) { 506 sysmon_envsys_destroy(sc->sc_sme); 507 return; 508 } 509 } 510 sensors = OF_finddevice("/smu/sensors"); 511 child = OF_child(sensors); 512 while (child != 0) { 513 sme_sensor = &sc->sc_sme_sensors[i]; 514 if (OF_getprop(child, "location", loc, 32) == 0) goto next; 515 if (OF_getprop(child, "device_type", type, 32) == 0) goto next; 516 if (OF_getprop(child, "reg", ®, 4) == 0) goto next; 517 if (strcmp(type, "temp-sensor") == 0) { 518 sme_sensor->units = ENVSYS_STEMP; 519 sme_sensor->state = ENVSYS_SINVALID; 520 strncpy(sme_sensor->desc, loc, sizeof(sme_sensor->desc)); 521 sme_sensor->private = reg; 522 sysmon_envsys_sensor_attach(sc->sc_sme, sme_sensor); 523 i++; 524 printf("%s: %s@%x\n", loc, type, reg); 525 } 526 next: 527 child = OF_peer(child); 528 } 529 530 sc->sc_sme->sme_name = device_xname(sc->sc_dev); 531 sc->sc_sme->sme_cookie = sc; 532 sc->sc_sme->sme_refresh = smu_sme_refresh; 533 534 if (sysmon_envsys_register(sc->sc_sme)) { 535 aprint_error_dev(sc->sc_dev, 536 "unable to register with sysmon\n"); 537 sysmon_envsys_destroy(sc->sc_sme); 538 } 539 } 540 541 static int 542 smu_iicbus_print(void *aux, const char *smu) 543 { 544 struct smu_iicbus_confargs *ca = aux; 545 546 if (smu) 547 aprint_normal("%s at %s", ca->ca_name, smu); 548 549 return UNCONF; 550 } 551 552 static void 553 smu_sme_refresh(struct sysmon_envsys *sme, envsys_data_t *edata) 554 { 555 struct smu_softc *sc = sme->sme_cookie; 556 struct smu_fan *fan; 557 int which = edata->sensor; 558 int ret; 559 560 edata->state = ENVSYS_SINVALID; 561 562 if (which < sc->sc_num_fans) { 563 fan = &sc->sc_fans[which]; 564 565 ret = smu_fan_get_rpm(fan, &fan->current_rpm); 566 if (ret == 0) { 567 edata->value_cur = fan->current_rpm; 568 edata->state = ENVSYS_SVALID; 569 } 570 } else if (edata->private > 0) { 571 /* this works only for the CPU diode */ 572 int64_t r = smu_read_adc(sc, edata->private); 573 if (r != -1) { 574 r = r * sc->cpu_m; 575 r >>= 3; 576 r += (int64_t)sc->cpu_b << 9; 577 r <<= 1; 578 r *= 15625; 579 r /= 1024; 580 edata->value_cur = r + 273150000; 581 edata->state = ENVSYS_SVALID; 582 } 583 } 584 } 585 586 static int 587 smu_do_cmd(struct smu_softc *sc, struct smu_cmd *cmd, int timo) 588 { 589 int gpio, ret, bail; 590 u_char ack; 591 592 mutex_enter(&sc->sc_cmd_lock); 593 594 DPRINTF("%s: cmd %02x len %02x\n", __func__, cmd->cmd, cmd->len); 595 DPRINTF("%s: data %02x %02x %02x %02x %02x %02x %02x %02x\n", __func__, 596 cmd->data[0], cmd->data[1], cmd->data[2], cmd->data[3], 597 cmd->data[4], cmd->data[5], cmd->data[6], cmd->data[7]); 598 599 sc->sc_cmd->cmd = cmd->cmd; 600 sc->sc_cmd->len = cmd->len; 601 memcpy(sc->sc_cmd->data, cmd->data, cmd->len); 602 603 __asm volatile ("dcbf 0,%0; sync" :: "r"(sc->sc_cmd) : "memory"); 604 605 obio_write_4(sc->sc_dbell_mbox, sc->sc_cmd_paddr); 606 obio_write_1(sc->sc_dbell_gpio, 0x04); 607 608 bail = 0; 609 610 gpio = obio_read_1(sc->sc_dbell_gpio); 611 612 while (((gpio & 0x07) != 0x07) && (bail < timo)) { 613 ret = tsleep(sc->sc_cmd, PWAIT, "smu_cmd", mstohz(10)); 614 if (ret != 0) { 615 bail++; 616 } 617 gpio = obio_read_1(sc->sc_dbell_gpio); 618 } 619 620 if ((gpio & 0x07) != 0x07) { 621 mutex_exit(&sc->sc_cmd_lock); 622 return EWOULDBLOCK; 623 } 624 625 __asm volatile ("dcbf 0,%0; sync" :: "r"(sc->sc_cmd) : "memory"); 626 627 ack = (~cmd->cmd) & 0xff; 628 if (sc->sc_cmd->cmd != ack) { 629 DPRINTF("%s: invalid ack, got %x expected %x\n", 630 __func__, sc->sc_cmd->cmd, ack); 631 mutex_exit(&sc->sc_cmd_lock); 632 return EIO; 633 } 634 635 cmd->cmd = sc->sc_cmd->cmd; 636 cmd->len = sc->sc_cmd->len; 637 memcpy(cmd->data, sc->sc_cmd->data, sc->sc_cmd->len); 638 639 mutex_exit(&sc->sc_cmd_lock); 640 641 return 0; 642 } 643 644 645 static int 646 smu_dbell_gpio_intr(void *arg) 647 { 648 struct smu_softc *sc = arg; 649 650 DPRINTF("%s\n", __func__); 651 652 wakeup(sc->sc_cmd); 653 654 return 1; 655 } 656 657 void 658 smu_poweroff(void) 659 { 660 struct smu_cmd cmd; 661 662 if (smu0 == NULL) 663 return; 664 665 cmd.cmd = SMU_CMD_POWER; 666 strcpy(cmd.data, "SHUTDOWN"); 667 cmd.len = strlen(cmd.data) + 1; 668 smu_do_cmd(smu0, &cmd, 800); 669 670 for (;;); 671 } 672 673 void 674 smu_restart(void) 675 { 676 struct smu_cmd cmd; 677 678 if (smu0 == NULL) 679 return; 680 681 cmd.cmd = SMU_CMD_POWER; 682 strcpy(cmd.data, "RESTART"); 683 cmd.len = strlen(cmd.data) + 1; 684 smu_do_cmd(smu0, &cmd, 800); 685 686 for (;;); 687 } 688 689 static int 690 smu_todr_gettime_ymdhms(todr_chip_handle_t tch, struct clock_ymdhms *dt) 691 { 692 struct smu_softc *sc = tch->cookie; 693 struct smu_cmd cmd; 694 int ret; 695 696 cmd.cmd = SMU_CMD_RTC; 697 cmd.len = 1; 698 cmd.data[0] = 0x81; 699 700 ret = smu_do_cmd(sc, &cmd, 800); 701 if (ret != 0) 702 return ret; 703 704 dt->dt_sec = bcdtobin(cmd.data[0]); 705 dt->dt_min = bcdtobin(cmd.data[1]); 706 dt->dt_hour = bcdtobin(cmd.data[2]); 707 dt->dt_wday = bcdtobin(cmd.data[3]); 708 dt->dt_day = bcdtobin(cmd.data[4]); 709 dt->dt_mon = bcdtobin(cmd.data[5]); 710 dt->dt_year = bcdtobin(cmd.data[6]) + 2000; 711 712 return 0; 713 } 714 715 static int 716 smu_todr_settime_ymdhms(todr_chip_handle_t tch, struct clock_ymdhms *dt) 717 { 718 struct smu_softc *sc = tch->cookie; 719 struct smu_cmd cmd; 720 721 cmd.cmd = SMU_CMD_RTC; 722 cmd.len = 8; 723 cmd.data[0] = 0x80; 724 cmd.data[1] = bintobcd(dt->dt_sec); 725 cmd.data[2] = bintobcd(dt->dt_min); 726 cmd.data[3] = bintobcd(dt->dt_hour); 727 cmd.data[4] = bintobcd(dt->dt_wday); 728 cmd.data[5] = bintobcd(dt->dt_day); 729 cmd.data[6] = bintobcd(dt->dt_mon); 730 cmd.data[7] = bintobcd(dt->dt_year - 2000); 731 732 return smu_do_cmd(sc, &cmd, 800); 733 } 734 735 static int 736 smu_fan_update_rpm(struct smu_fan *fan) 737 { 738 struct smu_softc *sc = fan->sc; 739 struct smu_cmd cmd; 740 int ret; 741 742 cmd.cmd = SMU_CMD_FAN; 743 cmd.len = 2; 744 cmd.data[0] = 0x31; 745 cmd.data[1] = fan->reg; 746 747 ret = smu_do_cmd(sc, &cmd, 800); 748 if (ret == 0) { 749 fan->last_update = time_uptime; 750 fan->current_rpm = (cmd.data[0] << 8) | cmd.data[1]; 751 } else { 752 cmd.cmd = SMU_CMD_FAN; 753 cmd.len = 1; 754 cmd.data[0] = 0x01; 755 756 ret = smu_do_cmd(sc, &cmd, 800); 757 if (ret == 0) { 758 fan->last_update = time_uptime; 759 fan->current_rpm = (cmd.data[1 + fan->reg * 2] << 8) | 760 cmd.data[2 + fan->reg * 2]; 761 } 762 } 763 764 return ret; 765 } 766 767 static int 768 smu_fan_get_rpm(struct smu_fan *fan, int *rpm) 769 { 770 int ret; 771 ret = 0; 772 773 if (time_uptime - fan->last_update > 1) { 774 ret = smu_fan_update_rpm(fan); 775 if (ret != 0) 776 return ret; 777 } 778 779 *rpm = fan->current_rpm; 780 781 return ret; 782 } 783 784 static int 785 smu_fan_set_rpm(struct smu_fan *fan, int rpm) 786 { 787 struct smu_softc *sc = fan->sc; 788 struct smu_cmd cmd; 789 int ret; 790 791 DPRINTF("%s: fan %s rpm %d\n", __func__, fan->location, rpm); 792 793 rpm = uimax(fan->min_rpm, rpm); 794 rpm = uimin(fan->max_rpm, rpm); 795 796 cmd.cmd = SMU_CMD_FAN; 797 cmd.len = 4; 798 cmd.data[0] = 0x30; 799 cmd.data[1] = fan->reg; 800 cmd.data[2] = (rpm >> 8) & 0xff; 801 cmd.data[3] = rpm & 0xff; 802 803 ret = smu_do_cmd(sc, &cmd, 800); 804 if (ret != 0) { 805 cmd.cmd = SMU_CMD_FAN; 806 cmd.len = 14; 807 cmd.data[0] = fan->rpm_ctl ? 0x00 : 0x10; 808 cmd.data[1] = 1 << fan->reg; 809 cmd.data[2] = cmd.data[2 + fan->reg * 2] = (rpm >> 8) & 0xff; 810 cmd.data[3] = cmd.data[3 + fan->reg * 2] = rpm & 0xff; 811 812 ret = smu_do_cmd(sc, &cmd, 800); 813 } 814 815 return ret; 816 } 817 818 static int 819 smu_read_adc(struct smu_softc *sc, int id) 820 { 821 struct smu_cmd cmd; 822 int ret; 823 824 cmd.cmd = SMU_CMD_ADC; 825 cmd.len = 1; 826 cmd.data[0] = id; 827 828 ret = smu_do_cmd(sc, &cmd, 800); 829 if (ret == 0) { 830 return cmd.data[0] << 8 | cmd.data[1]; 831 } 832 return -1; 833 } 834 835 static int 836 smu_iicbus_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *send, 837 size_t send_len, void *recv, size_t recv_len, int flags) 838 { 839 struct smu_iicbus *iicbus = cookie; 840 struct smu_softc *sc = iicbus->sc; 841 struct smu_cmd cmd; 842 int retries, ret; 843 844 DPRINTF("%s: op %x addr %x send_len %d recv_len %d\n", 845 __func__, op, addr, send_len, recv_len); 846 847 cmd.cmd = SMU_CMD_I2C; 848 cmd.len = 9 + recv_len; 849 cmd.data[0] = iicbus->reg; 850 cmd.data[1] = I2C_OP_READ_P(op) ? 0x02 : 0x00; 851 cmd.data[2] = addr << 1; 852 cmd.data[3] = send_len; 853 memcpy(&cmd.data[4], send, send_len); 854 cmd.data[7] = addr << 1; 855 if (I2C_OP_READ_P(op)) 856 cmd.data[7] |= 0x01; 857 cmd.data[8] = recv_len; 858 memcpy(&cmd.data[9], recv, recv_len); 859 860 ret = smu_do_cmd(sc, &cmd, 800); 861 if (ret != 0) 862 return (ret); 863 864 for (retries = 0; retries < 10; retries++) { 865 cmd.cmd = SMU_CMD_I2C; 866 cmd.len = 1; 867 cmd.data[0] = 0x00; 868 memset(&cmd.data[1], 0xff, recv_len); 869 870 ret = smu_do_cmd(sc, &cmd, 800); 871 872 DPRINTF("%s: cmd data[0] %x\n", __func__, cmd.data[0]); 873 874 if (ret == 0 && (cmd.data[0] & 0x80) == 0) 875 break; 876 877 DELAY(10000); 878 } 879 880 if (cmd.data[0] & 0x80) 881 return EIO; 882 883 if (I2C_OP_READ_P(op)) 884 memcpy(recv, &cmd.data[1], recv_len); 885 886 return 0; 887 } 888 889 static int 890 smu_sysctl_fan_rpm(SYSCTLFN_ARGS) 891 { 892 struct sysctlnode node = *rnode; 893 struct smu_fan *fan = node.sysctl_data; 894 int rpm = 0; 895 int ret; 896 897 node.sysctl_data = &rpm; 898 899 if (newp) { 900 if (sysctl_lookup(SYSCTLFN_CALL(&node)) == 0) { 901 rpm = *(int *) node.sysctl_data; 902 return smu_fan_set_rpm(fan, rpm); 903 } 904 return EINVAL; 905 } else { 906 ret = smu_fan_get_rpm(fan, &rpm); 907 if (ret != 0) 908 return (ret); 909 910 return sysctl_lookup(SYSCTLFN_CALL(&node)); 911 } 912 913 return 0; 914 } 915 916 SYSCTL_SETUP(smu_sysctl_setup, "SMU sysctl subtree setup") 917 { 918 sysctl_createv(NULL, 0, NULL, NULL, 919 CTLFLAG_PERMANENT, CTLTYPE_NODE, "machdep", NULL, 920 NULL, 0, NULL, 0, CTL_MACHDEP, CTL_EOL); 921 } 922 923 static void 924 smu_setup_zones(struct smu_softc *sc) 925 { 926 struct smu_zone *z; 927 struct smu_fan *f; 928 int i; 929 930 /* find CPU fans */ 931 z = &sc->sc_zones[SMU_ZONE_CPUS]; 932 z->nfans = 0; 933 for (i = 0; i < SMU_MAX_FANS; i++) { 934 f = &sc->sc_fans[i]; 935 if ((strstr(f->location, "CPU") != NULL) || 936 (strstr(f->location, "System") != NULL)) { 937 z->fans[z->nfans] = i; 938 z->nfans++; 939 } 940 } 941 aprint_normal_dev(sc->sc_dev, 942 "using %d fans for CPU zone\n", z->nfans); 943 z->threshold = C_TO_uK(45); 944 z->duty = 150; 945 z->step = 3; 946 z->filter = is_cpu_sensor; 947 948 z = &sc->sc_zones[SMU_ZONE_DRIVES]; 949 z->nfans = 0; 950 for (i = 0; i < SMU_MAX_FANS; i++) { 951 f = &sc->sc_fans[i]; 952 if ((strstr(f->location, "DRIVE") != NULL) || 953 (strstr(f->location, "Drive") != NULL)) { 954 z->fans[z->nfans] = i; 955 z->nfans++; 956 } 957 } 958 aprint_normal_dev(sc->sc_dev, 959 "using %d fans for drive bay zone\n", z->nfans); 960 z->threshold = C_TO_uK(40); 961 z->duty = 150; 962 z->step = 2; 963 z->filter = is_drive_sensor; 964 965 z = &sc->sc_zones[SMU_ZONE_SLOTS]; 966 z->nfans = 0; 967 for (i = 0; i < SMU_MAX_FANS; i++) { 968 f = &sc->sc_fans[i]; 969 if ((strstr(f->location, "BACKSIDE") != NULL) || 970 (strstr(f->location, "SLOTS") != NULL)) { 971 z->fans[z->nfans] = i; 972 z->nfans++; 973 } 974 } 975 aprint_normal_dev(sc->sc_dev, 976 "using %d fans for expansion slots zone\n", z->nfans); 977 z->threshold = C_TO_uK(40); 978 z->duty = 150; 979 z->step = 2; 980 z->filter = is_slots_sensor; 981 982 sc->sc_dying = false; 983 kthread_create(PRI_NONE, 0, curcpu(), smu_adjust, sc, &sc->sc_thread, 984 "fan control"); 985 } 986 987 static void 988 smu_adjust_zone(struct smu_softc *sc, int which) 989 { 990 struct smu_zone *z = &sc->sc_zones[which]; 991 struct smu_fan *f; 992 long temp, newduty, i, speed, diff; 993 994 DPRINTF("%s %d\n", __func__, which); 995 996 temp = sysmon_envsys_get_max_value(z->filter, true); 997 if (temp == 0) { 998 /* no sensor data - leave fan alone */ 999 DPRINTF("nodata\n"); 1000 return; 1001 } 1002 DPRINTF("temp %ld ", (temp - 273150000) / 1000000); 1003 diff = ((temp - z->threshold) / 1000000) * z->step; 1004 1005 if (diff < 0) newduty = 0; 1006 else if (diff > 100) newduty = 100; 1007 else newduty = diff; 1008 1009 DPRINTF("newduty %ld diff %ld \n", newduty, diff); 1010 if (newduty == z->duty) { 1011 DPRINTF("no change\n"); 1012 return; 1013 } 1014 z->duty = newduty; 1015 /* now adjust each fan to the new duty cycle */ 1016 for (i = 0; i < z->nfans; i++) { 1017 f = &sc->sc_fans[z->fans[i]]; 1018 speed = f->min_rpm + ((f->max_rpm - f->min_rpm) * newduty) / 100; 1019 DPRINTF("fan %d speed %ld ", z->fans[i], speed); 1020 smu_fan_set_rpm(f, speed); 1021 } 1022 DPRINTF("\n"); 1023 } 1024 1025 static void 1026 smu_adjust(void *cookie) 1027 { 1028 struct smu_softc *sc = cookie; 1029 int i; 1030 1031 while (!sc->sc_dying) { 1032 for (i = 0; i < SMU_ZONES; i++) 1033 smu_adjust_zone(sc, i); 1034 kpause("fanctrl", true, mstohz(3000), NULL); 1035 } 1036 kthread_exit(0); 1037 } 1038 1039 static bool is_cpu_sensor(const envsys_data_t *edata) 1040 { 1041 if (edata->units != ENVSYS_STEMP) 1042 return false; 1043 if (strstr(edata->desc, "CPU") != NULL) 1044 return TRUE; 1045 return false; 1046 } 1047 1048 static bool is_drive_sensor(const envsys_data_t *edata) 1049 { 1050 if (edata->units != ENVSYS_STEMP) 1051 return false; 1052 if (strstr(edata->desc, "DRIVE") != NULL) 1053 return TRUE; 1054 if (strstr(edata->desc, "drive") != NULL) 1055 return TRUE; 1056 return false; 1057 } 1058 1059 static bool is_slots_sensor(const envsys_data_t *edata) 1060 { 1061 if (edata->units != ENVSYS_STEMP) 1062 return false; 1063 if (strstr(edata->desc, "BACKSIDE") != NULL) 1064 return TRUE; 1065 if (strstr(edata->desc, "INLET") != NULL) 1066 return TRUE; 1067 if (strstr(edata->desc, "DIODE") != NULL) 1068 return TRUE; 1069 if (strstr(edata->desc, "TUNNEL") != NULL) 1070 return TRUE; 1071 return false; 1072 } 1073 1074 int 1075 smu_get_datablock(int id, uint8_t *buf, size_t len) 1076 { 1077 struct smu_cmd cmd; 1078 1079 cmd.cmd = SMU_PARTITION; 1080 cmd.len = 2; 1081 cmd.data[0] = SMU_PARTITION_LATEST; 1082 cmd.data[1] = id; 1083 smu_do_cmd(smu0, &cmd, 100); 1084 1085 cmd.data[4] = cmd.data[0]; 1086 cmd.data[5] = cmd.data[1]; 1087 1088 cmd.cmd = SMU_MISC; 1089 cmd.len = 7; 1090 cmd.data[0] = SMU_MISC_GET_DATA; 1091 cmd.data[1] = 4; 1092 cmd.data[2] = 0; 1093 cmd.data[3] = 0; 1094 cmd.data[6] = len; 1095 smu_do_cmd(smu0, &cmd, 100); 1096 1097 memcpy(buf, cmd.data, len); 1098 return 0; 1099 } 1100