2 * Copyright (c) 2009 Nathan Whitehorn
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/module.h>
37 #include <sys/clock.h>
38 #include <sys/ctype.h>
39 #include <sys/kernel.h>
40 #include <sys/kthread.h>
41 #include <sys/reboot.h>
43 #include <sys/sysctl.h>
44 #include <sys/unistd.h>
46 #include <machine/bus.h>
47 #include <machine/intr_machdep.h>
48 #include <machine/md_var.h>
50 #include <dev/iicbus/iicbus.h>
51 #include <dev/iicbus/iiconf.h>
52 #include <dev/led/led.h>
53 #include <dev/ofw/openfirm.h>
54 #include <dev/ofw/ofw_bus.h>
55 #include <dev/ofw/ofw_bus_subr.h>
56 #include <powerpc/powermac/macgpiovar.h>
59 #include "iicbus_if.h"
66 STAILQ_ENTRY(smu_cmd) cmd_q;
69 STAILQ_HEAD(smu_cmdq, smu_cmd);
97 struct resource *sc_memr;
101 bus_dma_tag_t sc_dmatag;
102 bus_space_tag_t sc_bt;
103 bus_space_handle_t sc_mailbox;
105 struct smu_cmd *sc_cmd, *sc_cur_cmd;
106 bus_addr_t sc_cmd_phys;
107 bus_dmamap_t sc_cmd_dmamap;
108 struct smu_cmdq sc_cmdq;
110 struct smu_fan *sc_fans;
112 struct smu_sensor *sc_sensors;
115 int sc_doorbellirqid;
116 struct resource *sc_doorbellirq;
117 void *sc_doorbellirqcookie;
119 struct proc *sc_fanmgt_proc;
120 time_t sc_lastuserchange;
122 /* Calibration data */
123 uint16_t sc_cpu_diode_scale;
124 int16_t sc_cpu_diode_offset;
126 uint16_t sc_cpu_volt_scale;
127 int16_t sc_cpu_volt_offset;
128 uint16_t sc_cpu_curr_scale;
129 int16_t sc_cpu_curr_offset;
131 uint16_t sc_slots_pow_scale;
132 int16_t sc_slots_pow_offset;
134 /* Thermal management parameters */
135 int sc_target_temp; /* Default 55 C */
136 int sc_critical_temp; /* Default 90 C */
138 struct cdev *sc_leddev;
141 /* regular bus attachment functions */
143 static int smu_probe(device_t);
144 static int smu_attach(device_t);
145 static const struct ofw_bus_devinfo *
146 smu_get_devinfo(device_t bus, device_t dev);
148 /* cpufreq notification hooks */
150 static void smu_cpufreq_pre_change(device_t, const struct cf_level *level);
151 static void smu_cpufreq_post_change(device_t, const struct cf_level *level);
153 /* clock interface */
154 static int smu_gettime(device_t dev, struct timespec *ts);
155 static int smu_settime(device_t dev, struct timespec *ts);
157 /* utility functions */
158 static int smu_run_cmd(device_t dev, struct smu_cmd *cmd, int wait);
159 static int smu_get_datablock(device_t dev, int8_t id, uint8_t *buf,
161 static void smu_attach_i2c(device_t dev, phandle_t i2croot);
162 static void smu_attach_fans(device_t dev, phandle_t fanroot);
163 static void smu_attach_sensors(device_t dev, phandle_t sensroot);
164 static void smu_fan_management_proc(void *xdev);
165 static void smu_manage_fans(device_t smu);
166 static void smu_set_sleepled(void *xdev, int onoff);
167 static int smu_server_mode(SYSCTL_HANDLER_ARGS);
168 static void smu_doorbell_intr(void *xdev);
170 /* where to find the doorbell GPIO */
172 static device_t smu_doorbell = NULL;
174 static device_method_t smu_methods[] = {
175 /* Device interface */
176 DEVMETHOD(device_probe, smu_probe),
177 DEVMETHOD(device_attach, smu_attach),
179 /* Clock interface */
180 DEVMETHOD(clock_gettime, smu_gettime),
181 DEVMETHOD(clock_settime, smu_settime),
183 /* ofw_bus interface */
184 DEVMETHOD(bus_child_pnpinfo_str,ofw_bus_gen_child_pnpinfo_str),
185 DEVMETHOD(ofw_bus_get_devinfo, smu_get_devinfo),
186 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
187 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
188 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
189 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
190 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
195 static driver_t smu_driver = {
198 sizeof(struct smu_softc)
201 static devclass_t smu_devclass;
203 DRIVER_MODULE(smu, nexus, smu_driver, smu_devclass, 0, 0);
204 MALLOC_DEFINE(M_SMU, "smu", "SMU Sensor Information");
206 #define SMU_MAILBOX 0x8000860c
207 #define SMU_FANMGT_INTERVAL 1000 /* ms */
213 #define SMU_I2C_SIMPLE 0x00
214 #define SMU_I2C_NORMAL 0x01
215 #define SMU_I2C_COMBINED 0x02
216 #define SMU_MISC 0xee
217 #define SMU_MISC_GET_DATA 0x02
218 #define SMU_MISC_LED_CTRL 0x04
219 #define SMU_POWER 0xaa
220 #define SMU_POWER_EVENTS 0x8f
221 #define SMU_PWR_GET_POWERUP 0x00
222 #define SMU_PWR_SET_POWERUP 0x01
223 #define SMU_PWR_CLR_POWERUP 0x02
225 #define SMU_RTC_GET 0x81
226 #define SMU_RTC_SET 0x80
228 /* Power event types */
229 #define SMU_WAKEUP_KEYPRESS 0x01
230 #define SMU_WAKEUP_AC_INSERT 0x02
231 #define SMU_WAKEUP_AC_CHANGE 0x04
232 #define SMU_WAKEUP_RING 0x10
235 #define SMU_CPUTEMP_CAL 0x18
236 #define SMU_CPUVOLT_CAL 0x21
237 #define SMU_SLOTPW_CAL 0x78
240 #define SMU_PARTITION 0x3e
241 #define SMU_PARTITION_LATEST 0x01
242 #define SMU_PARTITION_BASE 0x02
243 #define SMU_PARTITION_UPDATE 0x03
246 smu_probe(device_t dev)
248 const char *name = ofw_bus_get_name(dev);
250 if (strcmp(name, "smu") != 0)
253 device_set_desc(dev, "Apple System Management Unit");
258 smu_phys_callback(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
260 struct smu_softc *sc = xsc;
262 sc->sc_cmd_phys = segs[0].ds_addr;
266 smu_attach(device_t dev)
268 struct smu_softc *sc;
269 phandle_t node, child;
272 sc = device_get_softc(dev);
274 mtx_init(&sc->sc_mtx, "smu", NULL, MTX_DEF);
275 sc->sc_cur_cmd = NULL;
276 sc->sc_doorbellirqid = -1;
279 if (OF_finddevice("/u3") != -1)
283 * Map the mailbox area. This should be determined from firmware,
284 * but I have not found a simple way to do that.
286 bus_dma_tag_create(NULL, 16, 0, BUS_SPACE_MAXADDR_32BIT,
287 BUS_SPACE_MAXADDR, NULL, NULL, PAGE_SIZE, 1, PAGE_SIZE, 0, NULL,
288 NULL, &(sc->sc_dmatag));
289 sc->sc_bt = &bs_le_tag;
290 bus_space_map(sc->sc_bt, SMU_MAILBOX, 4, 0, &sc->sc_mailbox);
293 * Allocate the command buffer. This can be anywhere in the low 4 GB
296 bus_dmamem_alloc(sc->sc_dmatag, (void **)&sc->sc_cmd, BUS_DMA_WAITOK |
297 BUS_DMA_ZERO, &sc->sc_cmd_dmamap);
298 bus_dmamap_load(sc->sc_dmatag, sc->sc_cmd_dmamap,
299 sc->sc_cmd, PAGE_SIZE, smu_phys_callback, sc, 0);
300 STAILQ_INIT(&sc->sc_cmdq);
303 * Set up handlers to change CPU voltage when CPU frequency is changed.
305 EVENTHANDLER_REGISTER(cpufreq_pre_change, smu_cpufreq_pre_change, dev,
306 EVENTHANDLER_PRI_ANY);
307 EVENTHANDLER_REGISTER(cpufreq_post_change, smu_cpufreq_post_change, dev,
308 EVENTHANDLER_PRI_ANY);
311 * Detect and attach child devices.
313 node = ofw_bus_get_node(dev);
314 for (child = OF_child(node); child != 0; child = OF_peer(child)) {
316 memset(name, 0, sizeof(name));
317 OF_getprop(child, "name", name, sizeof(name));
319 if (strncmp(name, "rpm-fans", 9) == 0 ||
320 strncmp(name, "fans", 5) == 0)
321 smu_attach_fans(dev, child);
323 if (strncmp(name, "sensors", 8) == 0)
324 smu_attach_sensors(dev, child);
326 if (strncmp(name, "smu-i2c-control", 15) == 0)
327 smu_attach_i2c(dev, child);
330 /* Some SMUs have the I2C children directly under the bus. */
331 smu_attach_i2c(dev, node);
334 * Collect calibration constants.
336 smu_get_datablock(dev, SMU_CPUTEMP_CAL, data, sizeof(data));
337 sc->sc_cpu_diode_scale = (data[4] << 8) + data[5];
338 sc->sc_cpu_diode_offset = (data[6] << 8) + data[7];
340 smu_get_datablock(dev, SMU_CPUVOLT_CAL, data, sizeof(data));
341 sc->sc_cpu_volt_scale = (data[4] << 8) + data[5];
342 sc->sc_cpu_volt_offset = (data[6] << 8) + data[7];
343 sc->sc_cpu_curr_scale = (data[8] << 8) + data[9];
344 sc->sc_cpu_curr_offset = (data[10] << 8) + data[11];
346 smu_get_datablock(dev, SMU_SLOTPW_CAL, data, sizeof(data));
347 sc->sc_slots_pow_scale = (data[4] << 8) + data[5];
348 sc->sc_slots_pow_offset = (data[6] << 8) + data[7];
351 * Set up simple-minded thermal management.
353 sc->sc_target_temp = 55;
354 sc->sc_critical_temp = 90;
356 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
357 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
358 "target_temp", CTLTYPE_INT | CTLFLAG_RW, &sc->sc_target_temp,
359 sizeof(int), "Target temperature (C)");
360 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
361 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
362 "critical_temp", CTLTYPE_INT | CTLFLAG_RW,
363 &sc->sc_critical_temp, sizeof(int), "Critical temperature (C)");
365 kproc_create(smu_fan_management_proc, dev, &sc->sc_fanmgt_proc,
366 RFHIGHPID, 0, "smu_thermal");
369 * Set up LED interface
371 sc->sc_leddev = led_create(smu_set_sleepled, dev, "sleepled");
374 * Reset on power loss behavior
377 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
378 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
379 "server_mode", CTLTYPE_INT | CTLFLAG_RW, dev, 0,
380 smu_server_mode, "I", "Enable reboot after power failure");
383 * Set up doorbell interrupt.
385 sc->sc_doorbellirqid = 0;
386 sc->sc_doorbellirq = bus_alloc_resource_any(smu_doorbell, SYS_RES_IRQ,
387 &sc->sc_doorbellirqid, RF_ACTIVE);
388 bus_setup_intr(smu_doorbell, sc->sc_doorbellirq,
389 INTR_TYPE_MISC | INTR_MPSAFE, NULL, smu_doorbell_intr, dev,
390 &sc->sc_doorbellirqcookie);
391 powerpc_config_intr(rman_get_start(sc->sc_doorbellirq),
392 INTR_TRIGGER_EDGE, INTR_POLARITY_LOW);
395 * Connect RTC interface.
397 clock_register(dev, 1000);
399 return (bus_generic_attach(dev));
402 static const struct ofw_bus_devinfo *
403 smu_get_devinfo(device_t bus, device_t dev)
406 return (device_get_ivars(dev));
410 smu_send_cmd(device_t dev, struct smu_cmd *cmd)
412 struct smu_softc *sc;
414 sc = device_get_softc(dev);
416 mtx_assert(&sc->sc_mtx, MA_OWNED);
419 powerpc_pow_enabled = 0; /* SMU cannot work if we go to NAP */
421 sc->sc_cur_cmd = cmd;
423 /* Copy the command to the mailbox */
424 sc->sc_cmd->cmd = cmd->cmd;
425 sc->sc_cmd->len = cmd->len;
426 memcpy(sc->sc_cmd->data, cmd->data, sizeof(cmd->data));
427 bus_dmamap_sync(sc->sc_dmatag, sc->sc_cmd_dmamap, BUS_DMASYNC_PREWRITE);
428 bus_space_write_4(sc->sc_bt, sc->sc_mailbox, 0, sc->sc_cmd_phys);
430 /* Flush the cacheline it is in -- SMU bypasses the cache */
431 __asm __volatile("sync; dcbf 0,%0; sync" :: "r"(sc->sc_cmd): "memory");
433 /* Ring SMU doorbell */
434 macgpio_write(smu_doorbell, GPIO_DDR_OUTPUT);
438 smu_doorbell_intr(void *xdev)
441 struct smu_softc *sc;
445 doorbell_ack = macgpio_read(smu_doorbell);
446 sc = device_get_softc(smu);
448 if (doorbell_ack != (GPIO_DDR_OUTPUT | GPIO_LEVEL_RO | GPIO_DATA))
451 mtx_lock(&sc->sc_mtx);
453 if (sc->sc_cur_cmd == NULL) /* spurious */
456 /* Check result. First invalidate the cache again... */
457 __asm __volatile("dcbf 0,%0; sync" :: "r"(sc->sc_cmd) : "memory");
459 bus_dmamap_sync(sc->sc_dmatag, sc->sc_cmd_dmamap, BUS_DMASYNC_POSTREAD);
461 sc->sc_cur_cmd->cmd = sc->sc_cmd->cmd;
462 sc->sc_cur_cmd->len = sc->sc_cmd->len;
463 memcpy(sc->sc_cur_cmd->data, sc->sc_cmd->data,
464 sizeof(sc->sc_cmd->data));
465 wakeup(sc->sc_cur_cmd);
466 sc->sc_cur_cmd = NULL;
468 powerpc_pow_enabled = 1;
471 /* Queue next command if one is pending */
472 if (STAILQ_FIRST(&sc->sc_cmdq) != NULL) {
473 sc->sc_cur_cmd = STAILQ_FIRST(&sc->sc_cmdq);
474 STAILQ_REMOVE_HEAD(&sc->sc_cmdq, cmd_q);
475 smu_send_cmd(smu, sc->sc_cur_cmd);
478 mtx_unlock(&sc->sc_mtx);
482 smu_run_cmd(device_t dev, struct smu_cmd *cmd, int wait)
484 struct smu_softc *sc;
488 sc = device_get_softc(dev);
491 mtx_lock(&sc->sc_mtx);
492 if (sc->sc_cur_cmd != NULL) {
493 STAILQ_INSERT_TAIL(&sc->sc_cmdq, cmd, cmd_q);
495 smu_send_cmd(dev, cmd);
496 mtx_unlock(&sc->sc_mtx);
501 if (sc->sc_doorbellirqid < 0) {
502 /* Poll if the IRQ has not been set up yet */
505 smu_doorbell_intr(dev);
506 } while (sc->sc_cur_cmd != NULL);
508 /* smu_doorbell_intr will wake us when the command is ACK'ed */
509 error = tsleep(cmd, 0, "smu", 800 * hz / 1000);
511 smu_doorbell_intr(dev); /* One last chance */
514 mtx_lock(&sc->sc_mtx);
515 if (cmd->cmd == cmd_code) { /* Never processed */
516 /* Abort this command if we timed out */
517 if (sc->sc_cur_cmd == cmd)
518 sc->sc_cur_cmd = NULL;
520 STAILQ_REMOVE(&sc->sc_cmdq, cmd, smu_cmd,
522 mtx_unlock(&sc->sc_mtx);
526 mtx_unlock(&sc->sc_mtx);
530 /* SMU acks the command by inverting the command bits */
531 if (cmd->cmd == ((~cmd_code) & 0xff))
540 smu_get_datablock(device_t dev, int8_t id, uint8_t *buf, size_t len)
545 cmd.cmd = SMU_PARTITION;
547 cmd.data[0] = SMU_PARTITION_LATEST;
550 smu_run_cmd(dev, &cmd, 1);
552 addr[0] = addr[1] = 0;
553 addr[2] = cmd.data[0];
554 addr[3] = cmd.data[1];
558 cmd.data[0] = SMU_MISC_GET_DATA;
559 cmd.data[1] = sizeof(addr);
560 memcpy(&cmd.data[2], addr, sizeof(addr));
563 smu_run_cmd(dev, &cmd, 1);
564 memcpy(buf, cmd.data, len);
569 smu_slew_cpu_voltage(device_t dev, int to)
584 smu_run_cmd(dev, &cmd, 1);
588 smu_cpufreq_pre_change(device_t dev, const struct cf_level *level)
591 * Make sure the CPU voltage is raised before we raise
595 if (level->rel_set[0].freq == 10000 /* max */)
596 smu_slew_cpu_voltage(dev, 0);
600 smu_cpufreq_post_change(device_t dev, const struct cf_level *level)
602 /* We are safe to reduce CPU voltage after a downward transition */
604 if (level->rel_set[0].freq < 10000 /* max */)
605 smu_slew_cpu_voltage(dev, 1); /* XXX: 1/4 voltage for 970MP? */
608 /* Routines for probing the SMU doorbell GPIO */
609 static int doorbell_probe(device_t dev);
610 static int doorbell_attach(device_t dev);
612 static device_method_t doorbell_methods[] = {
613 /* Device interface */
614 DEVMETHOD(device_probe, doorbell_probe),
615 DEVMETHOD(device_attach, doorbell_attach),
619 static driver_t doorbell_driver = {
625 static devclass_t doorbell_devclass;
627 DRIVER_MODULE(smudoorbell, macgpio, doorbell_driver, doorbell_devclass, 0, 0);
630 doorbell_probe(device_t dev)
632 const char *name = ofw_bus_get_name(dev);
634 if (strcmp(name, "smu-doorbell") != 0)
637 device_set_desc(dev, "SMU Doorbell GPIO");
643 doorbell_attach(device_t dev)
650 * Sensor and fan management
654 smu_fan_set_rpm(device_t smu, struct smu_fan *fan, int rpm)
662 /* Clamp to allowed range */
663 rpm = max(fan->min_rpm, rpm);
664 rpm = min(fan->max_rpm, rpm);
667 * Apple has two fan control mechanisms. We can't distinguish
668 * them except by seeing if the new one fails. If the new one
669 * fails, use the old one.
672 if (!fan->old_style) {
675 cmd.data[1] = fan->reg;
676 cmd.data[2] = (rpm >> 8) & 0xff;
677 cmd.data[3] = rpm & 0xff;
679 error = smu_run_cmd(smu, &cmd, 1);
684 if (fan->old_style) {
687 cmd.data[1] = 1 << fan->reg;
688 cmd.data[2 + 2*fan->reg] = (rpm >> 8) & 0xff;
689 cmd.data[3 + 2*fan->reg] = rpm & 0xff;
690 error = smu_run_cmd(smu, &cmd, 1);
700 smu_fan_read_rpm(device_t smu, struct smu_fan *fan)
705 if (!fan->old_style) {
709 cmd.data[1] = fan->reg;
711 error = smu_run_cmd(smu, &cmd, 1);
715 rpm = (cmd.data[0] << 8) | cmd.data[1];
718 if (fan->old_style) {
723 error = smu_run_cmd(smu, &cmd, 1);
727 rpm = (cmd.data[fan->reg*2+1] << 8) | cmd.data[fan->reg*2+2];
734 smu_fanrpm_sysctl(SYSCTL_HANDLER_ARGS)
737 struct smu_softc *sc;
742 sc = device_get_softc(smu);
743 fan = &sc->sc_fans[arg2];
745 rpm = smu_fan_read_rpm(smu, fan);
749 error = sysctl_handle_int(oidp, &rpm, 0, req);
751 if (error || !req->newptr)
754 sc->sc_lastuserchange = time_uptime;
756 return (smu_fan_set_rpm(smu, fan, rpm));
760 smu_attach_fans(device_t dev, phandle_t fanroot)
763 struct smu_softc *sc;
764 struct sysctl_oid *oid, *fanroot_oid;
765 struct sysctl_ctx_list *ctx;
767 char type[32], sysctl_name[32];
770 sc = device_get_softc(dev);
773 for (child = OF_child(fanroot); child != 0; child = OF_peer(child))
776 if (sc->sc_nfans == 0) {
777 device_printf(dev, "WARNING: No fans detected!\n");
781 sc->sc_fans = malloc(sc->sc_nfans * sizeof(struct smu_fan), M_SMU,
787 ctx = device_get_sysctl_ctx(dev);
788 fanroot_oid = SYSCTL_ADD_NODE(ctx,
789 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "fans",
790 CTLFLAG_RD, 0, "SMU Fan Information");
792 for (child = OF_child(fanroot); child != 0; child = OF_peer(child)) {
793 OF_getprop(child, "device_type", type, sizeof(type));
794 if (strcmp(type, "fan-rpm-control") != 0)
798 OF_getprop(child, "reg", &fan->reg, sizeof(cell_t));
799 OF_getprop(child, "min-value", &fan->min_rpm, sizeof(cell_t));
800 OF_getprop(child, "max-value", &fan->max_rpm, sizeof(cell_t));
802 if (OF_getprop(child, "unmanaged-value", &fan->unmanaged_rpm,
803 sizeof(cell_t)) != sizeof(cell_t))
804 fan->unmanaged_rpm = fan->max_rpm;
806 fan->setpoint = smu_fan_read_rpm(dev, fan);
808 OF_getprop(child, "location", fan->location,
809 sizeof(fan->location));
812 for (i = 0; i < strlen(fan->location); i++) {
813 sysctl_name[i] = tolower(fan->location[i]);
814 if (isspace(sysctl_name[i]))
815 sysctl_name[i] = '_';
819 oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(fanroot_oid),
820 OID_AUTO, sysctl_name, CTLFLAG_RD, 0, "Fan Information");
821 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, "minrpm",
822 CTLTYPE_INT | CTLFLAG_RD, &fan->min_rpm, sizeof(cell_t),
823 "Minimum allowed RPM");
824 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, "maxrpm",
825 CTLTYPE_INT | CTLFLAG_RD, &fan->max_rpm, sizeof(cell_t),
826 "Maximum allowed RPM");
827 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, "rpm",
828 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, dev,
829 sc->sc_nfans, smu_fanrpm_sysctl, "I", "Fan RPM");
837 smu_sensor_read(device_t smu, struct smu_sensor *sens, int *val)
840 struct smu_softc *sc;
846 cmd.data[0] = sens->reg;
849 error = smu_run_cmd(smu, &cmd, 1);
853 sc = device_get_softc(smu);
854 value = (cmd.data[0] << 8) | cmd.data[1];
856 switch (sens->type) {
857 case SMU_TEMP_SENSOR:
858 value *= sc->sc_cpu_diode_scale;
860 value += ((int64_t)sc->sc_cpu_diode_offset) << 9;
863 /* Convert from 16.16 fixed point degC into integer C. */
866 case SMU_VOLTAGE_SENSOR:
867 value *= sc->sc_cpu_volt_scale;
868 value += sc->sc_cpu_volt_offset;
871 /* Convert from 16.16 fixed point V into mV. */
876 case SMU_CURRENT_SENSOR:
877 value *= sc->sc_cpu_curr_scale;
878 value += sc->sc_cpu_curr_offset;
881 /* Convert from 16.16 fixed point A into mA. */
886 case SMU_POWER_SENSOR:
887 value *= sc->sc_slots_pow_scale;
888 value += sc->sc_slots_pow_offset;
891 /* Convert from 16.16 fixed point W into mW. */
903 smu_sensor_sysctl(SYSCTL_HANDLER_ARGS)
906 struct smu_softc *sc;
907 struct smu_sensor *sens;
911 sc = device_get_softc(smu);
912 sens = &sc->sc_sensors[arg2];
914 error = smu_sensor_read(smu, sens, &value);
918 error = sysctl_handle_int(oidp, &value, 0, req);
924 smu_attach_sensors(device_t dev, phandle_t sensroot)
926 struct smu_sensor *sens;
927 struct smu_softc *sc;
928 struct sysctl_oid *sensroot_oid;
929 struct sysctl_ctx_list *ctx;
934 sc = device_get_softc(dev);
937 for (child = OF_child(sensroot); child != 0; child = OF_peer(child))
940 if (sc->sc_nsensors == 0) {
941 device_printf(dev, "WARNING: No sensors detected!\n");
945 sc->sc_sensors = malloc(sc->sc_nsensors * sizeof(struct smu_sensor),
946 M_SMU, M_WAITOK | M_ZERO);
948 sens = sc->sc_sensors;
951 ctx = device_get_sysctl_ctx(dev);
952 sensroot_oid = SYSCTL_ADD_NODE(ctx,
953 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "sensors",
954 CTLFLAG_RD, 0, "SMU Sensor Information");
956 for (child = OF_child(sensroot); child != 0; child = OF_peer(child)) {
957 char sysctl_name[40], sysctl_desc[40];
960 OF_getprop(child, "device_type", type, sizeof(type));
962 if (strcmp(type, "current-sensor") == 0) {
963 sens->type = SMU_CURRENT_SENSOR;
965 } else if (strcmp(type, "temp-sensor") == 0) {
966 sens->type = SMU_TEMP_SENSOR;
968 } else if (strcmp(type, "voltage-sensor") == 0) {
969 sens->type = SMU_VOLTAGE_SENSOR;
971 } else if (strcmp(type, "power-sensor") == 0) {
972 sens->type = SMU_POWER_SENSOR;
978 OF_getprop(child, "reg", &sens->reg, sizeof(cell_t));
979 OF_getprop(child, "location", sens->location,
980 sizeof(sens->location));
982 for (i = 0; i < strlen(sens->location); i++) {
983 sysctl_name[i] = tolower(sens->location[i]);
984 if (isspace(sysctl_name[i]))
985 sysctl_name[i] = '_';
989 sprintf(sysctl_desc,"%s (%s)", sens->location, units);
991 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(sensroot_oid), OID_AUTO,
992 sysctl_name, CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
993 dev, sc->sc_nsensors, smu_sensor_sysctl, "I", sysctl_desc);
1001 smu_fan_management_proc(void *xdev)
1003 device_t smu = xdev;
1006 smu_manage_fans(smu);
1007 pause("smu", SMU_FANMGT_INTERVAL * hz / 1000);
1012 smu_manage_fans(device_t smu)
1014 struct smu_softc *sc;
1015 int i, maxtemp, temp, factor, error;
1017 sc = device_get_softc(smu);
1020 for (i = 0; i < sc->sc_nsensors; i++) {
1021 if (sc->sc_sensors[i].type != SMU_TEMP_SENSOR)
1024 error = smu_sensor_read(smu, &sc->sc_sensors[i], &temp);
1025 if (error == 0 && temp > maxtemp)
1029 if (maxtemp > sc->sc_critical_temp) {
1030 device_printf(smu, "WARNING: Current system temperature (%d C) "
1031 "exceeds critical temperature (%d C)! Shutting down!\n",
1032 maxtemp, sc->sc_critical_temp);
1033 shutdown_nice(RB_POWEROFF);
1036 if (maxtemp - sc->sc_target_temp > 20)
1037 device_printf(smu, "WARNING: Current system temperature (%d C) "
1038 "more than 20 degrees over target temperature (%d C)!\n",
1039 maxtemp, sc->sc_target_temp);
1041 if (time_uptime - sc->sc_lastuserchange < 3) {
1043 * If we have heard from a user process in the last 3 seconds,
1050 if (maxtemp < 10) { /* Bail if no good sensors */
1051 for (i = 0; i < sc->sc_nfans; i++)
1052 smu_fan_set_rpm(smu, &sc->sc_fans[i],
1053 sc->sc_fans[i].unmanaged_rpm);
1057 if (maxtemp - sc->sc_target_temp > 4)
1059 else if (maxtemp - sc->sc_target_temp > 1)
1061 else if (sc->sc_target_temp - maxtemp > 4)
1063 else if (sc->sc_target_temp - maxtemp > 1)
1068 for (i = 0; i < sc->sc_nfans; i++)
1069 smu_fan_set_rpm(smu, &sc->sc_fans[i],
1070 (sc->sc_fans[i].setpoint * factor) / 100);
1074 smu_set_sleepled(void *xdev, int onoff)
1076 static struct smu_cmd cmd;
1077 device_t smu = xdev;
1081 cmd.data[0] = SMU_MISC_LED_CTRL;
1083 cmd.data[2] = onoff;
1085 smu_run_cmd(smu, &cmd, 0);
1089 smu_server_mode(SYSCTL_HANDLER_ARGS)
1093 device_t smu = arg1;
1096 cmd.cmd = SMU_POWER_EVENTS;
1098 cmd.data[0] = SMU_PWR_GET_POWERUP;
1100 error = smu_run_cmd(smu, &cmd, 1);
1105 server_mode = (cmd.data[1] & SMU_WAKEUP_AC_INSERT) ? 1 : 0;
1107 error = sysctl_handle_int(oidp, &server_mode, 0, req);
1109 if (error || !req->newptr)
1112 if (server_mode == 1)
1113 cmd.data[0] = SMU_PWR_SET_POWERUP;
1114 else if (server_mode == 0)
1115 cmd.data[0] = SMU_PWR_CLR_POWERUP;
1121 cmd.data[2] = SMU_WAKEUP_AC_INSERT;
1123 return (smu_run_cmd(smu, &cmd, 1));
1127 smu_gettime(device_t dev, struct timespec *ts)
1130 struct clocktime ct;
1134 cmd.data[0] = SMU_RTC_GET;
1136 if (smu_run_cmd(dev, &cmd, 1) != 0)
1140 ct.sec = bcd2bin(cmd.data[0]);
1141 ct.min = bcd2bin(cmd.data[1]);
1142 ct.hour = bcd2bin(cmd.data[2]);
1143 ct.dow = bcd2bin(cmd.data[3]);
1144 ct.day = bcd2bin(cmd.data[4]);
1145 ct.mon = bcd2bin(cmd.data[5]);
1146 ct.year = bcd2bin(cmd.data[6]) + 2000;
1148 return (clock_ct_to_ts(&ct, ts));
1152 smu_settime(device_t dev, struct timespec *ts)
1155 struct clocktime ct;
1159 cmd.data[0] = SMU_RTC_SET;
1161 clock_ts_to_ct(ts, &ct);
1163 cmd.data[1] = bin2bcd(ct.sec);
1164 cmd.data[2] = bin2bcd(ct.min);
1165 cmd.data[3] = bin2bcd(ct.hour);
1166 cmd.data[4] = bin2bcd(ct.dow);
1167 cmd.data[5] = bin2bcd(ct.day);
1168 cmd.data[6] = bin2bcd(ct.mon);
1169 cmd.data[7] = bin2bcd(ct.year - 2000);
1171 return (smu_run_cmd(dev, &cmd, 1));
1174 /* SMU I2C Interface */
1176 static int smuiic_probe(device_t dev);
1177 static int smuiic_attach(device_t dev);
1178 static int smuiic_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs);
1179 static phandle_t smuiic_get_node(device_t bus, device_t dev);
1181 static device_method_t smuiic_methods[] = {
1182 /* device interface */
1183 DEVMETHOD(device_probe, smuiic_probe),
1184 DEVMETHOD(device_attach, smuiic_attach),
1186 /* iicbus interface */
1187 DEVMETHOD(iicbus_callback, iicbus_null_callback),
1188 DEVMETHOD(iicbus_transfer, smuiic_transfer),
1190 /* ofw_bus interface */
1191 DEVMETHOD(ofw_bus_get_node, smuiic_get_node),
1196 struct smuiic_softc {
1198 volatile int sc_iic_inuse;
1202 static driver_t smuiic_driver = {
1205 sizeof(struct smuiic_softc)
1207 static devclass_t smuiic_devclass;
1209 DRIVER_MODULE(smuiic, smu, smuiic_driver, smuiic_devclass, 0, 0);
1212 smu_attach_i2c(device_t smu, phandle_t i2croot)
1216 struct ofw_bus_devinfo *dinfo;
1219 for (child = OF_child(i2croot); child != 0; child = OF_peer(child)) {
1220 if (OF_getprop(child, "name", name, sizeof(name)) <= 0)
1223 if (strcmp(name, "i2c-bus") != 0 && strcmp(name, "i2c") != 0)
1226 dinfo = malloc(sizeof(struct ofw_bus_devinfo), M_SMU,
1228 if (ofw_bus_gen_setup_devinfo(dinfo, child) != 0) {
1233 cdev = device_add_child(smu, NULL, -1);
1235 device_printf(smu, "<%s>: device_add_child failed\n",
1237 ofw_bus_gen_destroy_devinfo(dinfo);
1241 device_set_ivars(cdev, dinfo);
1246 smuiic_probe(device_t dev)
1250 name = ofw_bus_get_name(dev);
1254 if (strcmp(name, "i2c-bus") == 0 || strcmp(name, "i2c") == 0) {
1255 device_set_desc(dev, "SMU I2C controller");
1263 smuiic_attach(device_t dev)
1265 struct smuiic_softc *sc = device_get_softc(dev);
1266 mtx_init(&sc->sc_mtx, "smuiic", NULL, MTX_DEF);
1267 sc->sc_iic_inuse = 0;
1269 /* Get our bus number */
1270 OF_getprop(ofw_bus_get_node(dev), "reg", &sc->sc_busno,
1271 sizeof(sc->sc_busno));
1273 /* Add the IIC bus layer */
1274 device_add_child(dev, "iicbus", -1);
1276 return (bus_generic_attach(dev));
1280 smuiic_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
1282 struct smuiic_softc *sc = device_get_softc(dev);
1286 mtx_lock(&sc->sc_mtx);
1287 while (sc->sc_iic_inuse)
1288 mtx_sleep(sc, &sc->sc_mtx, 0, "smuiic", 100);
1290 sc->sc_iic_inuse = 1;
1293 for (i = 0; i < nmsgs; i++) {
1295 cmd.data[0] = sc->sc_busno;
1296 if (msgs[i].flags & IIC_M_NOSTOP)
1297 cmd.data[1] = SMU_I2C_COMBINED;
1299 cmd.data[1] = SMU_I2C_SIMPLE;
1301 cmd.data[2] = msgs[i].slave;
1302 if (msgs[i].flags & IIC_M_RD)
1305 if (msgs[i].flags & IIC_M_NOSTOP) {
1306 KASSERT(msgs[i].len < 4,
1307 ("oversize I2C combined message"));
1309 cmd.data[3] = min(msgs[i].len, 3);
1310 memcpy(&cmd.data[4], msgs[i].buf, min(msgs[i].len, 3));
1311 i++; /* Advance to next part of message */
1314 memset(&cmd.data[4], 0, 3);
1317 cmd.data[7] = msgs[i].slave;
1318 if (msgs[i].flags & IIC_M_RD)
1321 cmd.data[8] = msgs[i].len;
1322 if (msgs[i].flags & IIC_M_RD) {
1323 memset(&cmd.data[9], 0xff, msgs[i].len);
1326 memcpy(&cmd.data[9], msgs[i].buf, msgs[i].len);
1327 cmd.len = 9 + msgs[i].len;
1330 mtx_unlock(&sc->sc_mtx);
1331 smu_run_cmd(device_get_parent(dev), &cmd, 1);
1332 mtx_lock(&sc->sc_mtx);
1334 for (j = 0; j < 10; j++) {
1338 memset(&cmd.data[1], 0xff, msgs[i].len);
1340 mtx_unlock(&sc->sc_mtx);
1341 smu_run_cmd(device_get_parent(dev), &cmd, 1);
1342 mtx_lock(&sc->sc_mtx);
1344 if (!(cmd.data[0] & 0x80))
1347 mtx_sleep(sc, &sc->sc_mtx, 0, "smuiic", 10);
1350 if (cmd.data[0] & 0x80) {
1355 memcpy(msgs[i].buf, &cmd.data[1], msgs[i].len);
1356 msgs[i].len = cmd.len - 1;
1360 sc->sc_iic_inuse = 0;
1361 mtx_unlock(&sc->sc_mtx);
1367 smuiic_get_node(device_t bus, device_t dev)
1370 return (ofw_bus_get_node(bus));