2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009 Nathan Whitehorn
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/module.h>
39 #include <sys/clock.h>
40 #include <sys/ctype.h>
41 #include <sys/kernel.h>
42 #include <sys/kthread.h>
43 #include <sys/reboot.h>
45 #include <sys/sysctl.h>
46 #include <sys/unistd.h>
48 #include <machine/bus.h>
49 #include <machine/intr_machdep.h>
50 #include <machine/md_var.h>
52 #include <dev/iicbus/iicbus.h>
53 #include <dev/iicbus/iiconf.h>
54 #include <dev/led/led.h>
55 #include <dev/ofw/openfirm.h>
56 #include <dev/ofw/ofw_bus.h>
57 #include <dev/ofw/ofw_bus_subr.h>
58 #include <powerpc/powermac/macgpiovar.h>
59 #include <powerpc/powermac/powermac_thermal.h>
62 #include "iicbus_if.h"
69 STAILQ_ENTRY(smu_cmd) cmd_q;
72 STAILQ_HEAD(smu_cmdq, smu_cmd);
88 /* We can read the PWM and the RPM from a PWM controlled fan.
89 * Offer both values via sysctl.
92 SMU_PWM_SYSCTL_PWM = 1 << 8,
93 SMU_PWM_SYSCTL_RPM = 2 << 8
97 struct pmac_therm therm;
113 struct resource *sc_memr;
117 bus_dma_tag_t sc_dmatag;
118 bus_space_tag_t sc_bt;
119 bus_space_handle_t sc_mailbox;
121 struct smu_cmd *sc_cmd, *sc_cur_cmd;
122 bus_addr_t sc_cmd_phys;
123 bus_dmamap_t sc_cmd_dmamap;
124 struct smu_cmdq sc_cmdq;
126 struct smu_fan *sc_fans;
129 struct smu_sensor *sc_sensors;
132 int sc_doorbellirqid;
133 struct resource *sc_doorbellirq;
134 void *sc_doorbellirqcookie;
136 struct proc *sc_fanmgt_proc;
137 time_t sc_lastuserchange;
139 /* Calibration data */
140 uint16_t sc_cpu_diode_scale;
141 int16_t sc_cpu_diode_offset;
143 uint16_t sc_cpu_volt_scale;
144 int16_t sc_cpu_volt_offset;
145 uint16_t sc_cpu_curr_scale;
146 int16_t sc_cpu_curr_offset;
148 uint16_t sc_slots_pow_scale;
149 int16_t sc_slots_pow_offset;
151 struct cdev *sc_leddev;
154 /* regular bus attachment functions */
156 static int smu_probe(device_t);
157 static int smu_attach(device_t);
158 static const struct ofw_bus_devinfo *
159 smu_get_devinfo(device_t bus, device_t dev);
161 /* cpufreq notification hooks */
163 static void smu_cpufreq_pre_change(device_t, const struct cf_level *level);
164 static void smu_cpufreq_post_change(device_t, const struct cf_level *level);
166 /* clock interface */
167 static int smu_gettime(device_t dev, struct timespec *ts);
168 static int smu_settime(device_t dev, struct timespec *ts);
170 /* utility functions */
171 static int smu_run_cmd(device_t dev, struct smu_cmd *cmd, int wait);
172 static int smu_get_datablock(device_t dev, int8_t id, uint8_t *buf,
174 static void smu_attach_i2c(device_t dev, phandle_t i2croot);
175 static void smu_attach_fans(device_t dev, phandle_t fanroot);
176 static void smu_attach_sensors(device_t dev, phandle_t sensroot);
177 static void smu_set_sleepled(void *xdev, int onoff);
178 static int smu_server_mode(SYSCTL_HANDLER_ARGS);
179 static void smu_doorbell_intr(void *xdev);
180 static void smu_shutdown(void *xdev, int howto);
182 /* where to find the doorbell GPIO */
184 static device_t smu_doorbell = NULL;
186 static device_method_t smu_methods[] = {
187 /* Device interface */
188 DEVMETHOD(device_probe, smu_probe),
189 DEVMETHOD(device_attach, smu_attach),
191 /* Clock interface */
192 DEVMETHOD(clock_gettime, smu_gettime),
193 DEVMETHOD(clock_settime, smu_settime),
195 /* ofw_bus interface */
196 DEVMETHOD(bus_child_pnpinfo_str,ofw_bus_gen_child_pnpinfo_str),
197 DEVMETHOD(ofw_bus_get_devinfo, smu_get_devinfo),
198 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
199 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
200 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
201 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
202 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
207 static driver_t smu_driver = {
210 sizeof(struct smu_softc)
213 static devclass_t smu_devclass;
215 DRIVER_MODULE(smu, ofwbus, smu_driver, smu_devclass, 0, 0);
216 static MALLOC_DEFINE(M_SMU, "smu", "SMU Sensor Information");
218 #define SMU_MAILBOX 0x8000860c
219 #define SMU_FANMGT_INTERVAL 1000 /* ms */
224 #define SMU_RPM_STATUS 0x01
225 #define SMU_RPM_SETPOINT 0x02
226 #define SMU_PWM_STATUS 0x11
227 #define SMU_PWM_SETPOINT 0x12
229 #define SMU_I2C_SIMPLE 0x00
230 #define SMU_I2C_NORMAL 0x01
231 #define SMU_I2C_COMBINED 0x02
232 #define SMU_MISC 0xee
233 #define SMU_MISC_GET_DATA 0x02
234 #define SMU_MISC_LED_CTRL 0x04
235 #define SMU_POWER 0xaa
236 #define SMU_POWER_EVENTS 0x8f
237 #define SMU_PWR_GET_POWERUP 0x00
238 #define SMU_PWR_SET_POWERUP 0x01
239 #define SMU_PWR_CLR_POWERUP 0x02
241 #define SMU_RTC_GET 0x81
242 #define SMU_RTC_SET 0x80
244 /* Power event types */
245 #define SMU_WAKEUP_KEYPRESS 0x01
246 #define SMU_WAKEUP_AC_INSERT 0x02
247 #define SMU_WAKEUP_AC_CHANGE 0x04
248 #define SMU_WAKEUP_RING 0x10
251 #define SMU_CPUTEMP_CAL 0x18
252 #define SMU_CPUVOLT_CAL 0x21
253 #define SMU_SLOTPW_CAL 0x78
256 #define SMU_PARTITION 0x3e
257 #define SMU_PARTITION_LATEST 0x01
258 #define SMU_PARTITION_BASE 0x02
259 #define SMU_PARTITION_UPDATE 0x03
262 smu_probe(device_t dev)
264 const char *name = ofw_bus_get_name(dev);
266 if (strcmp(name, "smu") != 0)
269 device_set_desc(dev, "Apple System Management Unit");
274 smu_phys_callback(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
276 struct smu_softc *sc = xsc;
278 sc->sc_cmd_phys = segs[0].ds_addr;
282 smu_attach(device_t dev)
284 struct smu_softc *sc;
285 phandle_t node, child;
288 sc = device_get_softc(dev);
290 mtx_init(&sc->sc_mtx, "smu", NULL, MTX_DEF);
291 sc->sc_cur_cmd = NULL;
292 sc->sc_doorbellirqid = -1;
295 if (OF_finddevice("/u3") != -1)
299 * Map the mailbox area. This should be determined from firmware,
300 * but I have not found a simple way to do that.
302 bus_dma_tag_create(NULL, 16, 0, BUS_SPACE_MAXADDR_32BIT,
303 BUS_SPACE_MAXADDR, NULL, NULL, PAGE_SIZE, 1, PAGE_SIZE, 0, NULL,
304 NULL, &(sc->sc_dmatag));
305 sc->sc_bt = &bs_le_tag;
306 bus_space_map(sc->sc_bt, SMU_MAILBOX, 4, 0, &sc->sc_mailbox);
309 * Allocate the command buffer. This can be anywhere in the low 4 GB
312 bus_dmamem_alloc(sc->sc_dmatag, (void **)&sc->sc_cmd, BUS_DMA_WAITOK |
313 BUS_DMA_ZERO, &sc->sc_cmd_dmamap);
314 bus_dmamap_load(sc->sc_dmatag, sc->sc_cmd_dmamap,
315 sc->sc_cmd, PAGE_SIZE, smu_phys_callback, sc, 0);
316 STAILQ_INIT(&sc->sc_cmdq);
319 * Set up handlers to change CPU voltage when CPU frequency is changed.
321 EVENTHANDLER_REGISTER(cpufreq_pre_change, smu_cpufreq_pre_change, dev,
322 EVENTHANDLER_PRI_ANY);
323 EVENTHANDLER_REGISTER(cpufreq_post_change, smu_cpufreq_post_change, dev,
324 EVENTHANDLER_PRI_ANY);
326 node = ofw_bus_get_node(dev);
328 /* Some SMUs have RPM and PWM controlled fans which do not sit
329 * under the same node. So we have to attach them separately.
331 smu_attach_fans(dev, node);
334 * Now detect and attach the other child devices.
336 for (child = OF_child(node); child != 0; child = OF_peer(child)) {
338 memset(name, 0, sizeof(name));
339 OF_getprop(child, "name", name, sizeof(name));
341 if (strncmp(name, "sensors", 8) == 0)
342 smu_attach_sensors(dev, child);
344 if (strncmp(name, "smu-i2c-control", 15) == 0)
345 smu_attach_i2c(dev, child);
348 /* Some SMUs have the I2C children directly under the bus. */
349 smu_attach_i2c(dev, node);
352 * Collect calibration constants.
354 smu_get_datablock(dev, SMU_CPUTEMP_CAL, data, sizeof(data));
355 sc->sc_cpu_diode_scale = (data[4] << 8) + data[5];
356 sc->sc_cpu_diode_offset = (data[6] << 8) + data[7];
358 smu_get_datablock(dev, SMU_CPUVOLT_CAL, data, sizeof(data));
359 sc->sc_cpu_volt_scale = (data[4] << 8) + data[5];
360 sc->sc_cpu_volt_offset = (data[6] << 8) + data[7];
361 sc->sc_cpu_curr_scale = (data[8] << 8) + data[9];
362 sc->sc_cpu_curr_offset = (data[10] << 8) + data[11];
364 smu_get_datablock(dev, SMU_SLOTPW_CAL, data, sizeof(data));
365 sc->sc_slots_pow_scale = (data[4] << 8) + data[5];
366 sc->sc_slots_pow_offset = (data[6] << 8) + data[7];
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);
400 * Learn about shutdown events
402 EVENTHANDLER_REGISTER(shutdown_final, smu_shutdown, dev,
405 return (bus_generic_attach(dev));
408 static const struct ofw_bus_devinfo *
409 smu_get_devinfo(device_t bus, device_t dev)
412 return (device_get_ivars(dev));
416 smu_send_cmd(device_t dev, struct smu_cmd *cmd)
418 struct smu_softc *sc;
420 sc = device_get_softc(dev);
422 mtx_assert(&sc->sc_mtx, MA_OWNED);
425 powerpc_pow_enabled = 0; /* SMU cannot work if we go to NAP */
427 sc->sc_cur_cmd = cmd;
429 /* Copy the command to the mailbox */
430 sc->sc_cmd->cmd = cmd->cmd;
431 sc->sc_cmd->len = cmd->len;
432 memcpy(sc->sc_cmd->data, cmd->data, sizeof(cmd->data));
433 bus_dmamap_sync(sc->sc_dmatag, sc->sc_cmd_dmamap, BUS_DMASYNC_PREWRITE);
434 bus_space_write_4(sc->sc_bt, sc->sc_mailbox, 0, sc->sc_cmd_phys);
436 /* Flush the cacheline it is in -- SMU bypasses the cache */
437 __asm __volatile("sync; dcbf 0,%0; sync" :: "r"(sc->sc_cmd): "memory");
439 /* Ring SMU doorbell */
440 macgpio_write(smu_doorbell, GPIO_DDR_OUTPUT);
444 smu_doorbell_intr(void *xdev)
447 struct smu_softc *sc;
451 doorbell_ack = macgpio_read(smu_doorbell);
452 sc = device_get_softc(smu);
454 if (doorbell_ack != (GPIO_DDR_OUTPUT | GPIO_LEVEL_RO | GPIO_DATA))
457 mtx_lock(&sc->sc_mtx);
459 if (sc->sc_cur_cmd == NULL) /* spurious */
462 /* Check result. First invalidate the cache again... */
463 __asm __volatile("dcbf 0,%0; sync" :: "r"(sc->sc_cmd) : "memory");
465 bus_dmamap_sync(sc->sc_dmatag, sc->sc_cmd_dmamap, BUS_DMASYNC_POSTREAD);
467 sc->sc_cur_cmd->cmd = sc->sc_cmd->cmd;
468 sc->sc_cur_cmd->len = sc->sc_cmd->len;
469 memcpy(sc->sc_cur_cmd->data, sc->sc_cmd->data,
470 sizeof(sc->sc_cmd->data));
471 wakeup(sc->sc_cur_cmd);
472 sc->sc_cur_cmd = NULL;
474 powerpc_pow_enabled = 1;
477 /* Queue next command if one is pending */
478 if (STAILQ_FIRST(&sc->sc_cmdq) != NULL) {
479 sc->sc_cur_cmd = STAILQ_FIRST(&sc->sc_cmdq);
480 STAILQ_REMOVE_HEAD(&sc->sc_cmdq, cmd_q);
481 smu_send_cmd(smu, sc->sc_cur_cmd);
484 mtx_unlock(&sc->sc_mtx);
488 smu_run_cmd(device_t dev, struct smu_cmd *cmd, int wait)
490 struct smu_softc *sc;
494 sc = device_get_softc(dev);
497 mtx_lock(&sc->sc_mtx);
498 if (sc->sc_cur_cmd != NULL) {
499 STAILQ_INSERT_TAIL(&sc->sc_cmdq, cmd, cmd_q);
501 smu_send_cmd(dev, cmd);
502 mtx_unlock(&sc->sc_mtx);
507 if (sc->sc_doorbellirqid < 0) {
508 /* Poll if the IRQ has not been set up yet */
511 smu_doorbell_intr(dev);
512 } while (sc->sc_cur_cmd != NULL);
514 /* smu_doorbell_intr will wake us when the command is ACK'ed */
515 error = tsleep(cmd, 0, "smu", 800 * hz / 1000);
517 smu_doorbell_intr(dev); /* One last chance */
520 mtx_lock(&sc->sc_mtx);
521 if (cmd->cmd == cmd_code) { /* Never processed */
522 /* Abort this command if we timed out */
523 if (sc->sc_cur_cmd == cmd)
524 sc->sc_cur_cmd = NULL;
526 STAILQ_REMOVE(&sc->sc_cmdq, cmd, smu_cmd,
528 mtx_unlock(&sc->sc_mtx);
532 mtx_unlock(&sc->sc_mtx);
536 /* SMU acks the command by inverting the command bits */
537 if (cmd->cmd == ((~cmd_code) & 0xff))
546 smu_get_datablock(device_t dev, int8_t id, uint8_t *buf, size_t len)
551 cmd.cmd = SMU_PARTITION;
553 cmd.data[0] = SMU_PARTITION_LATEST;
556 smu_run_cmd(dev, &cmd, 1);
558 addr[0] = addr[1] = 0;
559 addr[2] = cmd.data[0];
560 addr[3] = cmd.data[1];
564 cmd.data[0] = SMU_MISC_GET_DATA;
565 cmd.data[1] = sizeof(addr);
566 memcpy(&cmd.data[2], addr, sizeof(addr));
569 smu_run_cmd(dev, &cmd, 1);
570 memcpy(buf, cmd.data, len);
575 smu_slew_cpu_voltage(device_t dev, int to)
590 smu_run_cmd(dev, &cmd, 1);
594 smu_cpufreq_pre_change(device_t dev, const struct cf_level *level)
597 * Make sure the CPU voltage is raised before we raise
601 if (level->rel_set[0].freq == 10000 /* max */)
602 smu_slew_cpu_voltage(dev, 0);
606 smu_cpufreq_post_change(device_t dev, const struct cf_level *level)
608 /* We are safe to reduce CPU voltage after a downward transition */
610 if (level->rel_set[0].freq < 10000 /* max */)
611 smu_slew_cpu_voltage(dev, 1); /* XXX: 1/4 voltage for 970MP? */
614 /* Routines for probing the SMU doorbell GPIO */
615 static int doorbell_probe(device_t dev);
616 static int doorbell_attach(device_t dev);
618 static device_method_t doorbell_methods[] = {
619 /* Device interface */
620 DEVMETHOD(device_probe, doorbell_probe),
621 DEVMETHOD(device_attach, doorbell_attach),
625 static driver_t doorbell_driver = {
631 static devclass_t doorbell_devclass;
633 DRIVER_MODULE(smudoorbell, macgpio, doorbell_driver, doorbell_devclass, 0, 0);
636 doorbell_probe(device_t dev)
638 const char *name = ofw_bus_get_name(dev);
640 if (strcmp(name, "smu-doorbell") != 0)
643 device_set_desc(dev, "SMU Doorbell GPIO");
649 doorbell_attach(device_t dev)
656 * Sensor and fan management
660 smu_fan_check_old_style(struct smu_fan *fan)
662 device_t smu = fan->dev;
663 struct smu_softc *sc = device_get_softc(smu);
667 if (sc->old_style_fans != -1)
668 return (sc->old_style_fans);
671 * Apple has two fan control mechanisms. We can't distinguish
672 * them except by seeing if the new one fails. If the new one
673 * fails, use the old one.
679 cmd.data[1] = fan->reg;
682 error = smu_run_cmd(smu, &cmd, 1);
683 } while (error == EWOULDBLOCK);
685 sc->old_style_fans = (error != 0);
687 return (sc->old_style_fans);
691 smu_fan_set_rpm(struct smu_fan *fan, int rpm)
693 device_t smu = fan->dev;
700 /* Clamp to allowed range */
701 rpm = max(fan->fan.min_rpm, rpm);
702 rpm = min(fan->fan.max_rpm, rpm);
704 smu_fan_check_old_style(fan);
706 if (!fan->old_style) {
709 cmd.data[1] = fan->reg;
710 cmd.data[2] = (rpm >> 8) & 0xff;
711 cmd.data[3] = rpm & 0xff;
713 error = smu_run_cmd(smu, &cmd, 1);
714 if (error && error != EWOULDBLOCK)
718 cmd.data[0] = 0x00; /* RPM fan. */
719 cmd.data[1] = 1 << fan->reg;
720 cmd.data[2 + 2*fan->reg] = (rpm >> 8) & 0xff;
721 cmd.data[3 + 2*fan->reg] = rpm & 0xff;
722 error = smu_run_cmd(smu, &cmd, 1);
732 smu_fan_read_rpm(struct smu_fan *fan)
734 device_t smu = fan->dev;
738 smu_fan_check_old_style(fan);
740 if (!fan->old_style) {
744 cmd.data[1] = fan->reg;
746 error = smu_run_cmd(smu, &cmd, 1);
747 if (error && error != EWOULDBLOCK)
750 rpm = (cmd.data[0] << 8) | cmd.data[1];
753 if (fan->old_style) {
756 cmd.data[0] = SMU_RPM_STATUS;
758 error = smu_run_cmd(smu, &cmd, 1);
762 rpm = (cmd.data[fan->reg*2+1] << 8) | cmd.data[fan->reg*2+2];
768 smu_fan_set_pwm(struct smu_fan *fan, int pwm)
770 device_t smu = fan->dev;
777 /* Clamp to allowed range */
778 pwm = max(fan->fan.min_rpm, pwm);
779 pwm = min(fan->fan.max_rpm, pwm);
782 * Apple has two fan control mechanisms. We can't distinguish
783 * them except by seeing if the new one fails. If the new one
784 * fails, use the old one.
787 if (!fan->old_style) {
790 cmd.data[1] = fan->reg;
791 cmd.data[2] = (pwm >> 8) & 0xff;
792 cmd.data[3] = pwm & 0xff;
794 error = smu_run_cmd(smu, &cmd, 1);
795 if (error && error != EWOULDBLOCK)
799 if (fan->old_style) {
801 cmd.data[0] = 0x10; /* PWM fan. */
802 cmd.data[1] = 1 << fan->reg;
803 cmd.data[2 + 2*fan->reg] = (pwm >> 8) & 0xff;
804 cmd.data[3 + 2*fan->reg] = pwm & 0xff;
805 error = smu_run_cmd(smu, &cmd, 1);
815 smu_fan_read_pwm(struct smu_fan *fan, int *pwm, int *rpm)
817 device_t smu = fan->dev;
821 if (!fan->old_style) {
825 cmd.data[1] = fan->reg;
827 error = smu_run_cmd(smu, &cmd, 1);
828 if (error && error != EWOULDBLOCK)
831 *rpm = (cmd.data[0] << 8) | cmd.data[1];
834 if (fan->old_style) {
837 cmd.data[0] = SMU_PWM_STATUS;
839 error = smu_run_cmd(smu, &cmd, 1);
843 *rpm = (cmd.data[fan->reg*2+1] << 8) | cmd.data[fan->reg*2+2];
845 if (fan->old_style) {
848 cmd.data[0] = SMU_PWM_SETPOINT;
849 cmd.data[1] = 1 << fan->reg;
851 error = smu_run_cmd(smu, &cmd, 1);
855 *pwm = cmd.data[fan->reg*2+2];
861 smu_fanrpm_sysctl(SYSCTL_HANDLER_ARGS)
864 struct smu_softc *sc;
866 int pwm = 0, rpm, error = 0;
869 sc = device_get_softc(smu);
870 fan = &sc->sc_fans[arg2 & 0xff];
872 if (fan->type == SMU_FAN_RPM) {
873 rpm = smu_fan_read_rpm(fan);
877 error = sysctl_handle_int(oidp, &rpm, 0, req);
879 error = smu_fan_read_pwm(fan, &pwm, &rpm);
883 switch (arg2 & 0xff00) {
884 case SMU_PWM_SYSCTL_PWM:
885 error = sysctl_handle_int(oidp, &pwm, 0, req);
887 case SMU_PWM_SYSCTL_RPM:
888 error = sysctl_handle_int(oidp, &rpm, 0, req);
891 /* This should never happen */
895 /* We can only read the RPM from a PWM controlled fan, so return. */
896 if ((arg2 & 0xff00) == SMU_PWM_SYSCTL_RPM)
899 if (error || !req->newptr)
902 sc->sc_lastuserchange = time_uptime;
904 if (fan->type == SMU_FAN_RPM)
905 return (smu_fan_set_rpm(fan, rpm));
907 return (smu_fan_set_pwm(fan, pwm));
911 smu_fill_fan_prop(device_t dev, phandle_t child, int id)
914 struct smu_softc *sc;
917 sc = device_get_softc(dev);
918 fan = &sc->sc_fans[id];
920 OF_getprop(child, "device_type", type, sizeof(type));
921 /* We have either RPM or PWM controlled fans. */
922 if (strcmp(type, "fan-rpm-control") == 0)
923 fan->type = SMU_FAN_RPM;
925 fan->type = SMU_FAN_PWM;
929 OF_getprop(child, "reg", &fan->reg,
931 OF_getprop(child, "min-value", &fan->fan.min_rpm,
933 OF_getprop(child, "max-value", &fan->fan.max_rpm,
935 OF_getprop(child, "zone", &fan->fan.zone,
938 if (OF_getprop(child, "unmanaged-value",
939 &fan->fan.default_rpm,
940 sizeof(int)) != sizeof(int))
941 fan->fan.default_rpm = fan->fan.max_rpm;
943 OF_getprop(child, "location", fan->fan.name,
944 sizeof(fan->fan.name));
946 if (fan->type == SMU_FAN_RPM)
947 fan->setpoint = smu_fan_read_rpm(fan);
949 smu_fan_read_pwm(fan, &fan->setpoint, &fan->rpm);
952 /* On the first call count the number of fans. In the second call,
953 * after allocating the fan struct, fill the properties of the fans.
956 smu_count_fans(device_t dev)
958 struct smu_softc *sc;
959 phandle_t child, node, root;
962 node = ofw_bus_get_node(dev);
963 sc = device_get_softc(dev);
965 /* First find the fanroots and count the number of fans. */
966 for (root = OF_child(node); root != 0; root = OF_peer(root)) {
968 memset(name, 0, sizeof(name));
969 OF_getprop(root, "name", name, sizeof(name));
970 if (strncmp(name, "rpm-fans", 9) == 0 ||
971 strncmp(name, "pwm-fans", 9) == 0 ||
972 strncmp(name, "fans", 5) == 0)
973 for (child = OF_child(root); child != 0;
974 child = OF_peer(child)) {
976 /* When allocated, fill the fan properties. */
977 if (sc->sc_fans != NULL) {
978 smu_fill_fan_prop(dev, child,
984 device_printf(dev, "WARNING: No fans detected!\n");
991 smu_attach_fans(device_t dev, phandle_t fanroot)
994 struct smu_softc *sc;
995 struct sysctl_oid *oid, *fanroot_oid;
996 struct sysctl_ctx_list *ctx;
997 char sysctl_name[32];
1000 sc = device_get_softc(dev);
1002 /* Get the number of fans. */
1003 sc->sc_nfans = smu_count_fans(dev);
1004 if (sc->sc_nfans == 0)
1007 /* Now we're able to allocate memory for the fans struct. */
1008 sc->sc_fans = malloc(sc->sc_nfans * sizeof(struct smu_fan), M_SMU,
1011 /* Now fill in the properties. */
1012 smu_count_fans(dev);
1014 /* Register fans with pmac_thermal */
1015 for (i = 0; i < sc->sc_nfans; i++)
1016 pmac_thermal_fan_register(&sc->sc_fans[i].fan);
1018 ctx = device_get_sysctl_ctx(dev);
1019 fanroot_oid = SYSCTL_ADD_NODE(ctx,
1020 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "fans",
1021 CTLFLAG_RD, 0, "SMU Fan Information");
1024 for (i = 0; i < sc->sc_nfans; i++) {
1025 fan = &sc->sc_fans[i];
1026 for (j = 0; j < strlen(fan->fan.name); j++) {
1027 sysctl_name[j] = tolower(fan->fan.name[j]);
1028 if (isspace(sysctl_name[j]))
1029 sysctl_name[j] = '_';
1032 if (fan->type == SMU_FAN_RPM) {
1033 oid = SYSCTL_ADD_NODE(ctx,
1034 SYSCTL_CHILDREN(fanroot_oid),
1035 OID_AUTO, sysctl_name,
1036 CTLFLAG_RD, 0, "Fan Information");
1037 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
1038 "minrpm", CTLFLAG_RD,
1039 &fan->fan.min_rpm, 0,
1040 "Minimum allowed RPM");
1041 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
1042 "maxrpm", CTLFLAG_RD,
1043 &fan->fan.max_rpm, 0,
1044 "Maximum allowed RPM");
1045 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
1046 "rpm",CTLTYPE_INT | CTLFLAG_RW |
1047 CTLFLAG_MPSAFE, dev, i,
1048 smu_fanrpm_sysctl, "I", "Fan RPM");
1050 fan->fan.read = (int (*)(struct pmac_fan *))smu_fan_read_rpm;
1051 fan->fan.set = (int (*)(struct pmac_fan *, int))smu_fan_set_rpm;
1054 oid = SYSCTL_ADD_NODE(ctx,
1055 SYSCTL_CHILDREN(fanroot_oid),
1056 OID_AUTO, sysctl_name,
1057 CTLFLAG_RD, 0, "Fan Information");
1058 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
1059 "minpwm", CTLFLAG_RD,
1060 &fan->fan.min_rpm, 0,
1061 "Minimum allowed PWM in %");
1062 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
1063 "maxpwm", CTLFLAG_RD,
1064 &fan->fan.max_rpm, 0,
1065 "Maximum allowed PWM in %");
1066 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
1067 "pwm",CTLTYPE_INT | CTLFLAG_RW |
1068 CTLFLAG_MPSAFE, dev,
1069 SMU_PWM_SYSCTL_PWM | i,
1070 smu_fanrpm_sysctl, "I", "Fan PWM in %");
1071 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
1072 "rpm",CTLTYPE_INT | CTLFLAG_RD |
1073 CTLFLAG_MPSAFE, dev,
1074 SMU_PWM_SYSCTL_RPM | i,
1075 smu_fanrpm_sysctl, "I", "Fan RPM");
1076 fan->fan.read = NULL;
1077 fan->fan.set = (int (*)(struct pmac_fan *, int))smu_fan_set_pwm;
1081 device_printf(dev, "Fan: %s type: %d\n",
1082 fan->fan.name, fan->type);
1087 smu_sensor_read(struct smu_sensor *sens)
1089 device_t smu = sens->dev;
1091 struct smu_softc *sc;
1097 cmd.data[0] = sens->reg;
1100 error = smu_run_cmd(smu, &cmd, 1);
1104 sc = device_get_softc(smu);
1105 value = (cmd.data[0] << 8) | cmd.data[1];
1107 switch (sens->type) {
1108 case SMU_TEMP_SENSOR:
1109 value *= sc->sc_cpu_diode_scale;
1111 value += ((int64_t)sc->sc_cpu_diode_offset) << 9;
1114 /* Convert from 16.16 fixed point degC into integer 0.1 K. */
1115 value = 10*(value >> 16) + ((10*(value & 0xffff)) >> 16) + 2731;
1117 case SMU_VOLTAGE_SENSOR:
1118 value *= sc->sc_cpu_volt_scale;
1119 value += sc->sc_cpu_volt_offset;
1122 /* Convert from 16.16 fixed point V into mV. */
1127 case SMU_CURRENT_SENSOR:
1128 value *= sc->sc_cpu_curr_scale;
1129 value += sc->sc_cpu_curr_offset;
1132 /* Convert from 16.16 fixed point A into mA. */
1137 case SMU_POWER_SENSOR:
1138 value *= sc->sc_slots_pow_scale;
1139 value += sc->sc_slots_pow_offset;
1142 /* Convert from 16.16 fixed point W into mW. */
1153 smu_sensor_sysctl(SYSCTL_HANDLER_ARGS)
1156 struct smu_softc *sc;
1157 struct smu_sensor *sens;
1161 sc = device_get_softc(smu);
1162 sens = &sc->sc_sensors[arg2];
1164 value = smu_sensor_read(sens);
1168 error = sysctl_handle_int(oidp, &value, 0, req);
1174 smu_attach_sensors(device_t dev, phandle_t sensroot)
1176 struct smu_sensor *sens;
1177 struct smu_softc *sc;
1178 struct sysctl_oid *sensroot_oid;
1179 struct sysctl_ctx_list *ctx;
1184 sc = device_get_softc(dev);
1185 sc->sc_nsensors = 0;
1187 for (child = OF_child(sensroot); child != 0; child = OF_peer(child))
1190 if (sc->sc_nsensors == 0) {
1191 device_printf(dev, "WARNING: No sensors detected!\n");
1195 sc->sc_sensors = malloc(sc->sc_nsensors * sizeof(struct smu_sensor),
1196 M_SMU, M_WAITOK | M_ZERO);
1198 sens = sc->sc_sensors;
1199 sc->sc_nsensors = 0;
1201 ctx = device_get_sysctl_ctx(dev);
1202 sensroot_oid = SYSCTL_ADD_NODE(ctx,
1203 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "sensors",
1204 CTLFLAG_RD, 0, "SMU Sensor Information");
1206 for (child = OF_child(sensroot); child != 0; child = OF_peer(child)) {
1207 char sysctl_name[40], sysctl_desc[40];
1211 OF_getprop(child, "device_type", type, sizeof(type));
1213 if (strcmp(type, "current-sensor") == 0) {
1214 sens->type = SMU_CURRENT_SENSOR;
1216 } else if (strcmp(type, "temp-sensor") == 0) {
1217 sens->type = SMU_TEMP_SENSOR;
1219 } else if (strcmp(type, "voltage-sensor") == 0) {
1220 sens->type = SMU_VOLTAGE_SENSOR;
1222 } else if (strcmp(type, "power-sensor") == 0) {
1223 sens->type = SMU_POWER_SENSOR;
1229 OF_getprop(child, "reg", &sens->reg, sizeof(cell_t));
1230 OF_getprop(child, "zone", &sens->therm.zone, sizeof(int));
1231 OF_getprop(child, "location", sens->therm.name,
1232 sizeof(sens->therm.name));
1234 for (i = 0; i < strlen(sens->therm.name); i++) {
1235 sysctl_name[i] = tolower(sens->therm.name[i]);
1236 if (isspace(sysctl_name[i]))
1237 sysctl_name[i] = '_';
1241 sprintf(sysctl_desc,"%s (%s)", sens->therm.name, units);
1243 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(sensroot_oid), OID_AUTO,
1244 sysctl_name, CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
1245 dev, sc->sc_nsensors, smu_sensor_sysctl,
1246 (sens->type == SMU_TEMP_SENSOR) ? "IK" : "I", sysctl_desc);
1248 if (sens->type == SMU_TEMP_SENSOR) {
1249 /* Make up some numbers */
1250 sens->therm.target_temp = 500 + 2731; /* 50 C */
1251 sens->therm.max_temp = 900 + 2731; /* 90 C */
1254 (int (*)(struct pmac_therm *))smu_sensor_read;
1255 pmac_thermal_sensor_register(&sens->therm);
1264 smu_set_sleepled(void *xdev, int onoff)
1266 static struct smu_cmd cmd;
1267 device_t smu = xdev;
1271 cmd.data[0] = SMU_MISC_LED_CTRL;
1273 cmd.data[2] = onoff;
1275 smu_run_cmd(smu, &cmd, 0);
1279 smu_server_mode(SYSCTL_HANDLER_ARGS)
1283 device_t smu = arg1;
1286 cmd.cmd = SMU_POWER_EVENTS;
1288 cmd.data[0] = SMU_PWR_GET_POWERUP;
1290 error = smu_run_cmd(smu, &cmd, 1);
1295 server_mode = (cmd.data[1] & SMU_WAKEUP_AC_INSERT) ? 1 : 0;
1297 error = sysctl_handle_int(oidp, &server_mode, 0, req);
1299 if (error || !req->newptr)
1302 if (server_mode == 1)
1303 cmd.data[0] = SMU_PWR_SET_POWERUP;
1304 else if (server_mode == 0)
1305 cmd.data[0] = SMU_PWR_CLR_POWERUP;
1311 cmd.data[2] = SMU_WAKEUP_AC_INSERT;
1313 return (smu_run_cmd(smu, &cmd, 1));
1317 smu_shutdown(void *xdev, int howto)
1319 device_t smu = xdev;
1322 cmd.cmd = SMU_POWER;
1323 if (howto & RB_HALT)
1324 strcpy(cmd.data, "SHUTDOWN");
1326 strcpy(cmd.data, "RESTART");
1328 cmd.len = strlen(cmd.data);
1330 smu_run_cmd(smu, &cmd, 1);
1336 smu_gettime(device_t dev, struct timespec *ts)
1339 struct clocktime ct;
1343 cmd.data[0] = SMU_RTC_GET;
1345 if (smu_run_cmd(dev, &cmd, 1) != 0)
1349 ct.sec = bcd2bin(cmd.data[0]);
1350 ct.min = bcd2bin(cmd.data[1]);
1351 ct.hour = bcd2bin(cmd.data[2]);
1352 ct.dow = bcd2bin(cmd.data[3]);
1353 ct.day = bcd2bin(cmd.data[4]);
1354 ct.mon = bcd2bin(cmd.data[5]);
1355 ct.year = bcd2bin(cmd.data[6]) + 2000;
1357 return (clock_ct_to_ts(&ct, ts));
1361 smu_settime(device_t dev, struct timespec *ts)
1363 static struct smu_cmd cmd;
1364 struct clocktime ct;
1368 cmd.data[0] = SMU_RTC_SET;
1370 clock_ts_to_ct(ts, &ct);
1372 cmd.data[1] = bin2bcd(ct.sec);
1373 cmd.data[2] = bin2bcd(ct.min);
1374 cmd.data[3] = bin2bcd(ct.hour);
1375 cmd.data[4] = bin2bcd(ct.dow);
1376 cmd.data[5] = bin2bcd(ct.day);
1377 cmd.data[6] = bin2bcd(ct.mon);
1378 cmd.data[7] = bin2bcd(ct.year - 2000);
1380 return (smu_run_cmd(dev, &cmd, 0));
1383 /* SMU I2C Interface */
1385 static int smuiic_probe(device_t dev);
1386 static int smuiic_attach(device_t dev);
1387 static int smuiic_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs);
1388 static phandle_t smuiic_get_node(device_t bus, device_t dev);
1390 static device_method_t smuiic_methods[] = {
1391 /* device interface */
1392 DEVMETHOD(device_probe, smuiic_probe),
1393 DEVMETHOD(device_attach, smuiic_attach),
1395 /* iicbus interface */
1396 DEVMETHOD(iicbus_callback, iicbus_null_callback),
1397 DEVMETHOD(iicbus_transfer, smuiic_transfer),
1399 /* ofw_bus interface */
1400 DEVMETHOD(ofw_bus_get_node, smuiic_get_node),
1405 struct smuiic_softc {
1407 volatile int sc_iic_inuse;
1411 static driver_t smuiic_driver = {
1414 sizeof(struct smuiic_softc)
1416 static devclass_t smuiic_devclass;
1418 DRIVER_MODULE(smuiic, smu, smuiic_driver, smuiic_devclass, 0, 0);
1421 smu_attach_i2c(device_t smu, phandle_t i2croot)
1425 struct ofw_bus_devinfo *dinfo;
1428 for (child = OF_child(i2croot); child != 0; child = OF_peer(child)) {
1429 if (OF_getprop(child, "name", name, sizeof(name)) <= 0)
1432 if (strcmp(name, "i2c-bus") != 0 && strcmp(name, "i2c") != 0)
1435 dinfo = malloc(sizeof(struct ofw_bus_devinfo), M_SMU,
1437 if (ofw_bus_gen_setup_devinfo(dinfo, child) != 0) {
1442 cdev = device_add_child(smu, NULL, -1);
1444 device_printf(smu, "<%s>: device_add_child failed\n",
1446 ofw_bus_gen_destroy_devinfo(dinfo);
1450 device_set_ivars(cdev, dinfo);
1455 smuiic_probe(device_t dev)
1459 name = ofw_bus_get_name(dev);
1463 if (strcmp(name, "i2c-bus") == 0 || strcmp(name, "i2c") == 0) {
1464 device_set_desc(dev, "SMU I2C controller");
1472 smuiic_attach(device_t dev)
1474 struct smuiic_softc *sc = device_get_softc(dev);
1475 mtx_init(&sc->sc_mtx, "smuiic", NULL, MTX_DEF);
1476 sc->sc_iic_inuse = 0;
1478 /* Get our bus number */
1479 OF_getprop(ofw_bus_get_node(dev), "reg", &sc->sc_busno,
1480 sizeof(sc->sc_busno));
1482 /* Add the IIC bus layer */
1483 device_add_child(dev, "iicbus", -1);
1485 return (bus_generic_attach(dev));
1489 smuiic_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
1491 struct smuiic_softc *sc = device_get_softc(dev);
1495 mtx_lock(&sc->sc_mtx);
1496 while (sc->sc_iic_inuse)
1497 mtx_sleep(sc, &sc->sc_mtx, 0, "smuiic", 100);
1499 sc->sc_iic_inuse = 1;
1502 for (i = 0; i < nmsgs; i++) {
1504 cmd.data[0] = sc->sc_busno;
1505 if (msgs[i].flags & IIC_M_NOSTOP)
1506 cmd.data[1] = SMU_I2C_COMBINED;
1508 cmd.data[1] = SMU_I2C_SIMPLE;
1510 cmd.data[2] = msgs[i].slave;
1511 if (msgs[i].flags & IIC_M_RD)
1514 if (msgs[i].flags & IIC_M_NOSTOP) {
1515 KASSERT(msgs[i].len < 4,
1516 ("oversize I2C combined message"));
1518 cmd.data[3] = min(msgs[i].len, 3);
1519 memcpy(&cmd.data[4], msgs[i].buf, min(msgs[i].len, 3));
1520 i++; /* Advance to next part of message */
1523 memset(&cmd.data[4], 0, 3);
1526 cmd.data[7] = msgs[i].slave;
1527 if (msgs[i].flags & IIC_M_RD)
1530 cmd.data[8] = msgs[i].len;
1531 if (msgs[i].flags & IIC_M_RD) {
1532 memset(&cmd.data[9], 0xff, msgs[i].len);
1535 memcpy(&cmd.data[9], msgs[i].buf, msgs[i].len);
1536 cmd.len = 9 + msgs[i].len;
1539 mtx_unlock(&sc->sc_mtx);
1540 smu_run_cmd(device_get_parent(dev), &cmd, 1);
1541 mtx_lock(&sc->sc_mtx);
1543 for (j = 0; j < 10; j++) {
1547 memset(&cmd.data[1], 0xff, msgs[i].len);
1549 mtx_unlock(&sc->sc_mtx);
1550 smu_run_cmd(device_get_parent(dev), &cmd, 1);
1551 mtx_lock(&sc->sc_mtx);
1553 if (!(cmd.data[0] & 0x80))
1556 mtx_sleep(sc, &sc->sc_mtx, 0, "smuiic", 10);
1559 if (cmd.data[0] & 0x80) {
1564 memcpy(msgs[i].buf, &cmd.data[1], msgs[i].len);
1565 msgs[i].len = cmd.len - 1;
1569 sc->sc_iic_inuse = 0;
1570 mtx_unlock(&sc->sc_mtx);
1576 smuiic_get_node(device_t bus, device_t dev)
1579 return (ofw_bus_get_node(bus));