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/eventhandler.h>
36 #include <sys/systm.h>
37 #include <sys/module.h>
40 #include <sys/clock.h>
41 #include <sys/ctype.h>
42 #include <sys/kernel.h>
43 #include <sys/kthread.h>
45 #include <sys/mutex.h>
46 #include <sys/reboot.h>
48 #include <sys/sysctl.h>
49 #include <sys/unistd.h>
51 #include <machine/bus.h>
52 #include <machine/intr_machdep.h>
53 #include <machine/md_var.h>
55 #include <dev/iicbus/iicbus.h>
56 #include <dev/iicbus/iiconf.h>
57 #include <dev/led/led.h>
58 #include <dev/ofw/openfirm.h>
59 #include <dev/ofw/ofw_bus.h>
60 #include <dev/ofw/ofw_bus_subr.h>
61 #include <powerpc/powermac/macgpiovar.h>
62 #include <powerpc/powermac/powermac_thermal.h>
65 #include "iicbus_if.h"
72 STAILQ_ENTRY(smu_cmd) cmd_q;
75 STAILQ_HEAD(smu_cmdq, smu_cmd);
91 /* We can read the PWM and the RPM from a PWM controlled fan.
92 * Offer both values via sysctl.
95 SMU_PWM_SYSCTL_PWM = 1 << 8,
96 SMU_PWM_SYSCTL_RPM = 2 << 8
100 struct pmac_therm therm;
116 struct resource *sc_memr;
120 bus_dma_tag_t sc_dmatag;
121 bus_space_tag_t sc_bt;
122 bus_space_handle_t sc_mailbox;
124 struct smu_cmd *sc_cmd, *sc_cur_cmd;
125 bus_addr_t sc_cmd_phys;
126 bus_dmamap_t sc_cmd_dmamap;
127 struct smu_cmdq sc_cmdq;
129 struct smu_fan *sc_fans;
132 struct smu_sensor *sc_sensors;
135 int sc_doorbellirqid;
136 struct resource *sc_doorbellirq;
137 void *sc_doorbellirqcookie;
139 struct proc *sc_fanmgt_proc;
140 time_t sc_lastuserchange;
142 /* Calibration data */
143 uint16_t sc_cpu_diode_scale;
144 int16_t sc_cpu_diode_offset;
146 uint16_t sc_cpu_volt_scale;
147 int16_t sc_cpu_volt_offset;
148 uint16_t sc_cpu_curr_scale;
149 int16_t sc_cpu_curr_offset;
151 uint16_t sc_slots_pow_scale;
152 int16_t sc_slots_pow_offset;
154 struct cdev *sc_leddev;
157 /* regular bus attachment functions */
159 static int smu_probe(device_t);
160 static int smu_attach(device_t);
161 static const struct ofw_bus_devinfo *
162 smu_get_devinfo(device_t bus, device_t dev);
164 /* cpufreq notification hooks */
166 static void smu_cpufreq_pre_change(device_t, const struct cf_level *level);
167 static void smu_cpufreq_post_change(device_t, const struct cf_level *level);
169 /* clock interface */
170 static int smu_gettime(device_t dev, struct timespec *ts);
171 static int smu_settime(device_t dev, struct timespec *ts);
173 /* utility functions */
174 static int smu_run_cmd(device_t dev, struct smu_cmd *cmd, int wait);
175 static int smu_get_datablock(device_t dev, int8_t id, uint8_t *buf,
177 static void smu_attach_i2c(device_t dev, phandle_t i2croot);
178 static void smu_attach_fans(device_t dev, phandle_t fanroot);
179 static void smu_attach_sensors(device_t dev, phandle_t sensroot);
180 static void smu_set_sleepled(void *xdev, int onoff);
181 static int smu_server_mode(SYSCTL_HANDLER_ARGS);
182 static void smu_doorbell_intr(void *xdev);
183 static void smu_shutdown(void *xdev, int howto);
185 /* where to find the doorbell GPIO */
187 static device_t smu_doorbell = NULL;
189 static device_method_t smu_methods[] = {
190 /* Device interface */
191 DEVMETHOD(device_probe, smu_probe),
192 DEVMETHOD(device_attach, smu_attach),
194 /* Clock interface */
195 DEVMETHOD(clock_gettime, smu_gettime),
196 DEVMETHOD(clock_settime, smu_settime),
198 /* ofw_bus interface */
199 DEVMETHOD(bus_child_pnpinfo_str,ofw_bus_gen_child_pnpinfo_str),
200 DEVMETHOD(ofw_bus_get_devinfo, smu_get_devinfo),
201 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
202 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
203 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
204 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
205 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
210 static driver_t smu_driver = {
213 sizeof(struct smu_softc)
216 static devclass_t smu_devclass;
218 DRIVER_MODULE(smu, ofwbus, smu_driver, smu_devclass, 0, 0);
219 static MALLOC_DEFINE(M_SMU, "smu", "SMU Sensor Information");
221 #define SMU_MAILBOX 0x8000860c
222 #define SMU_FANMGT_INTERVAL 1000 /* ms */
227 #define SMU_RPM_STATUS 0x01
228 #define SMU_RPM_SETPOINT 0x02
229 #define SMU_PWM_STATUS 0x11
230 #define SMU_PWM_SETPOINT 0x12
232 #define SMU_I2C_SIMPLE 0x00
233 #define SMU_I2C_NORMAL 0x01
234 #define SMU_I2C_COMBINED 0x02
235 #define SMU_MISC 0xee
236 #define SMU_MISC_GET_DATA 0x02
237 #define SMU_MISC_LED_CTRL 0x04
238 #define SMU_POWER 0xaa
239 #define SMU_POWER_EVENTS 0x8f
240 #define SMU_PWR_GET_POWERUP 0x00
241 #define SMU_PWR_SET_POWERUP 0x01
242 #define SMU_PWR_CLR_POWERUP 0x02
244 #define SMU_RTC_GET 0x81
245 #define SMU_RTC_SET 0x80
247 /* Power event types */
248 #define SMU_WAKEUP_KEYPRESS 0x01
249 #define SMU_WAKEUP_AC_INSERT 0x02
250 #define SMU_WAKEUP_AC_CHANGE 0x04
251 #define SMU_WAKEUP_RING 0x10
254 #define SMU_CPUTEMP_CAL 0x18
255 #define SMU_CPUVOLT_CAL 0x21
256 #define SMU_SLOTPW_CAL 0x78
259 #define SMU_PARTITION 0x3e
260 #define SMU_PARTITION_LATEST 0x01
261 #define SMU_PARTITION_BASE 0x02
262 #define SMU_PARTITION_UPDATE 0x03
265 smu_probe(device_t dev)
267 const char *name = ofw_bus_get_name(dev);
269 if (strcmp(name, "smu") != 0)
272 device_set_desc(dev, "Apple System Management Unit");
277 smu_phys_callback(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
279 struct smu_softc *sc = xsc;
281 sc->sc_cmd_phys = segs[0].ds_addr;
285 smu_attach(device_t dev)
287 struct smu_softc *sc;
288 phandle_t node, child;
291 sc = device_get_softc(dev);
293 mtx_init(&sc->sc_mtx, "smu", NULL, MTX_DEF);
294 sc->sc_cur_cmd = NULL;
295 sc->sc_doorbellirqid = -1;
298 if (OF_finddevice("/u3") != -1)
302 * Map the mailbox area. This should be determined from firmware,
303 * but I have not found a simple way to do that.
305 bus_dma_tag_create(NULL, 16, 0, BUS_SPACE_MAXADDR_32BIT,
306 BUS_SPACE_MAXADDR, NULL, NULL, PAGE_SIZE, 1, PAGE_SIZE, 0, NULL,
307 NULL, &(sc->sc_dmatag));
308 sc->sc_bt = &bs_le_tag;
309 bus_space_map(sc->sc_bt, SMU_MAILBOX, 4, 0, &sc->sc_mailbox);
312 * Allocate the command buffer. This can be anywhere in the low 4 GB
315 bus_dmamem_alloc(sc->sc_dmatag, (void **)&sc->sc_cmd, BUS_DMA_WAITOK |
316 BUS_DMA_ZERO, &sc->sc_cmd_dmamap);
317 bus_dmamap_load(sc->sc_dmatag, sc->sc_cmd_dmamap,
318 sc->sc_cmd, PAGE_SIZE, smu_phys_callback, sc, 0);
319 STAILQ_INIT(&sc->sc_cmdq);
322 * Set up handlers to change CPU voltage when CPU frequency is changed.
324 EVENTHANDLER_REGISTER(cpufreq_pre_change, smu_cpufreq_pre_change, dev,
325 EVENTHANDLER_PRI_ANY);
326 EVENTHANDLER_REGISTER(cpufreq_post_change, smu_cpufreq_post_change, dev,
327 EVENTHANDLER_PRI_ANY);
329 node = ofw_bus_get_node(dev);
331 /* Some SMUs have RPM and PWM controlled fans which do not sit
332 * under the same node. So we have to attach them separately.
334 smu_attach_fans(dev, node);
337 * Now detect and attach the other child devices.
339 for (child = OF_child(node); child != 0; child = OF_peer(child)) {
341 memset(name, 0, sizeof(name));
342 OF_getprop(child, "name", name, sizeof(name));
344 if (strncmp(name, "sensors", 8) == 0)
345 smu_attach_sensors(dev, child);
347 if (strncmp(name, "smu-i2c-control", 15) == 0)
348 smu_attach_i2c(dev, child);
351 /* Some SMUs have the I2C children directly under the bus. */
352 smu_attach_i2c(dev, node);
355 * Collect calibration constants.
357 smu_get_datablock(dev, SMU_CPUTEMP_CAL, data, sizeof(data));
358 sc->sc_cpu_diode_scale = (data[4] << 8) + data[5];
359 sc->sc_cpu_diode_offset = (data[6] << 8) + data[7];
361 smu_get_datablock(dev, SMU_CPUVOLT_CAL, data, sizeof(data));
362 sc->sc_cpu_volt_scale = (data[4] << 8) + data[5];
363 sc->sc_cpu_volt_offset = (data[6] << 8) + data[7];
364 sc->sc_cpu_curr_scale = (data[8] << 8) + data[9];
365 sc->sc_cpu_curr_offset = (data[10] << 8) + data[11];
367 smu_get_datablock(dev, SMU_SLOTPW_CAL, data, sizeof(data));
368 sc->sc_slots_pow_scale = (data[4] << 8) + data[5];
369 sc->sc_slots_pow_offset = (data[6] << 8) + data[7];
372 * Set up LED interface
374 sc->sc_leddev = led_create(smu_set_sleepled, dev, "sleepled");
377 * Reset on power loss behavior
380 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
381 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
382 "server_mode", CTLTYPE_INT | CTLFLAG_RW, dev, 0,
383 smu_server_mode, "I", "Enable reboot after power failure");
386 * Set up doorbell interrupt.
388 sc->sc_doorbellirqid = 0;
389 sc->sc_doorbellirq = bus_alloc_resource_any(smu_doorbell, SYS_RES_IRQ,
390 &sc->sc_doorbellirqid, RF_ACTIVE);
391 bus_setup_intr(smu_doorbell, sc->sc_doorbellirq,
392 INTR_TYPE_MISC | INTR_MPSAFE, NULL, smu_doorbell_intr, dev,
393 &sc->sc_doorbellirqcookie);
394 powerpc_config_intr(rman_get_start(sc->sc_doorbellirq),
395 INTR_TRIGGER_EDGE, INTR_POLARITY_LOW);
398 * Connect RTC interface.
400 clock_register(dev, 1000);
403 * Learn about shutdown events
405 EVENTHANDLER_REGISTER(shutdown_final, smu_shutdown, dev,
408 return (bus_generic_attach(dev));
411 static const struct ofw_bus_devinfo *
412 smu_get_devinfo(device_t bus, device_t dev)
415 return (device_get_ivars(dev));
419 smu_send_cmd(device_t dev, struct smu_cmd *cmd)
421 struct smu_softc *sc;
423 sc = device_get_softc(dev);
425 mtx_assert(&sc->sc_mtx, MA_OWNED);
428 powerpc_pow_enabled = 0; /* SMU cannot work if we go to NAP */
430 sc->sc_cur_cmd = cmd;
432 /* Copy the command to the mailbox */
433 sc->sc_cmd->cmd = cmd->cmd;
434 sc->sc_cmd->len = cmd->len;
435 memcpy(sc->sc_cmd->data, cmd->data, sizeof(cmd->data));
436 bus_dmamap_sync(sc->sc_dmatag, sc->sc_cmd_dmamap, BUS_DMASYNC_PREWRITE);
437 bus_space_write_4(sc->sc_bt, sc->sc_mailbox, 0, sc->sc_cmd_phys);
439 /* Flush the cacheline it is in -- SMU bypasses the cache */
440 __asm __volatile("sync; dcbf 0,%0; sync" :: "r"(sc->sc_cmd): "memory");
442 /* Ring SMU doorbell */
443 macgpio_write(smu_doorbell, GPIO_DDR_OUTPUT);
447 smu_doorbell_intr(void *xdev)
450 struct smu_softc *sc;
454 doorbell_ack = macgpio_read(smu_doorbell);
455 sc = device_get_softc(smu);
457 if (doorbell_ack != (GPIO_DDR_OUTPUT | GPIO_LEVEL_RO | GPIO_DATA))
460 mtx_lock(&sc->sc_mtx);
462 if (sc->sc_cur_cmd == NULL) /* spurious */
465 /* Check result. First invalidate the cache again... */
466 __asm __volatile("dcbf 0,%0; sync" :: "r"(sc->sc_cmd) : "memory");
468 bus_dmamap_sync(sc->sc_dmatag, sc->sc_cmd_dmamap, BUS_DMASYNC_POSTREAD);
470 sc->sc_cur_cmd->cmd = sc->sc_cmd->cmd;
471 sc->sc_cur_cmd->len = sc->sc_cmd->len;
472 memcpy(sc->sc_cur_cmd->data, sc->sc_cmd->data,
473 sizeof(sc->sc_cmd->data));
474 wakeup(sc->sc_cur_cmd);
475 sc->sc_cur_cmd = NULL;
477 powerpc_pow_enabled = 1;
480 /* Queue next command if one is pending */
481 if (STAILQ_FIRST(&sc->sc_cmdq) != NULL) {
482 sc->sc_cur_cmd = STAILQ_FIRST(&sc->sc_cmdq);
483 STAILQ_REMOVE_HEAD(&sc->sc_cmdq, cmd_q);
484 smu_send_cmd(smu, sc->sc_cur_cmd);
487 mtx_unlock(&sc->sc_mtx);
491 smu_run_cmd(device_t dev, struct smu_cmd *cmd, int wait)
493 struct smu_softc *sc;
497 sc = device_get_softc(dev);
500 mtx_lock(&sc->sc_mtx);
501 if (sc->sc_cur_cmd != NULL) {
502 STAILQ_INSERT_TAIL(&sc->sc_cmdq, cmd, cmd_q);
504 smu_send_cmd(dev, cmd);
505 mtx_unlock(&sc->sc_mtx);
510 if (sc->sc_doorbellirqid < 0) {
511 /* Poll if the IRQ has not been set up yet */
514 smu_doorbell_intr(dev);
515 } while (sc->sc_cur_cmd != NULL);
517 /* smu_doorbell_intr will wake us when the command is ACK'ed */
518 error = tsleep(cmd, 0, "smu", 800 * hz / 1000);
520 smu_doorbell_intr(dev); /* One last chance */
523 mtx_lock(&sc->sc_mtx);
524 if (cmd->cmd == cmd_code) { /* Never processed */
525 /* Abort this command if we timed out */
526 if (sc->sc_cur_cmd == cmd)
527 sc->sc_cur_cmd = NULL;
529 STAILQ_REMOVE(&sc->sc_cmdq, cmd, smu_cmd,
531 mtx_unlock(&sc->sc_mtx);
535 mtx_unlock(&sc->sc_mtx);
539 /* SMU acks the command by inverting the command bits */
540 if (cmd->cmd == ((~cmd_code) & 0xff))
549 smu_get_datablock(device_t dev, int8_t id, uint8_t *buf, size_t len)
554 cmd.cmd = SMU_PARTITION;
556 cmd.data[0] = SMU_PARTITION_LATEST;
559 smu_run_cmd(dev, &cmd, 1);
561 addr[0] = addr[1] = 0;
562 addr[2] = cmd.data[0];
563 addr[3] = cmd.data[1];
567 cmd.data[0] = SMU_MISC_GET_DATA;
568 cmd.data[1] = sizeof(addr);
569 memcpy(&cmd.data[2], addr, sizeof(addr));
572 smu_run_cmd(dev, &cmd, 1);
573 memcpy(buf, cmd.data, len);
578 smu_slew_cpu_voltage(device_t dev, int to)
593 smu_run_cmd(dev, &cmd, 1);
597 smu_cpufreq_pre_change(device_t dev, const struct cf_level *level)
600 * Make sure the CPU voltage is raised before we raise
604 if (level->rel_set[0].freq == 10000 /* max */)
605 smu_slew_cpu_voltage(dev, 0);
609 smu_cpufreq_post_change(device_t dev, const struct cf_level *level)
611 /* We are safe to reduce CPU voltage after a downward transition */
613 if (level->rel_set[0].freq < 10000 /* max */)
614 smu_slew_cpu_voltage(dev, 1); /* XXX: 1/4 voltage for 970MP? */
617 /* Routines for probing the SMU doorbell GPIO */
618 static int doorbell_probe(device_t dev);
619 static int doorbell_attach(device_t dev);
621 static device_method_t doorbell_methods[] = {
622 /* Device interface */
623 DEVMETHOD(device_probe, doorbell_probe),
624 DEVMETHOD(device_attach, doorbell_attach),
628 static driver_t doorbell_driver = {
634 static devclass_t doorbell_devclass;
636 EARLY_DRIVER_MODULE(smudoorbell, macgpio, doorbell_driver, doorbell_devclass,
637 0, 0, BUS_PASS_SUPPORTDEV);
640 doorbell_probe(device_t dev)
642 const char *name = ofw_bus_get_name(dev);
644 if (strcmp(name, "smu-doorbell") != 0)
647 device_set_desc(dev, "SMU Doorbell GPIO");
653 doorbell_attach(device_t dev)
660 * Sensor and fan management
664 smu_fan_check_old_style(struct smu_fan *fan)
666 device_t smu = fan->dev;
667 struct smu_softc *sc = device_get_softc(smu);
671 if (sc->old_style_fans != -1)
672 return (sc->old_style_fans);
675 * Apple has two fan control mechanisms. We can't distinguish
676 * them except by seeing if the new one fails. If the new one
677 * fails, use the old one.
683 cmd.data[1] = fan->reg;
686 error = smu_run_cmd(smu, &cmd, 1);
687 } while (error == EWOULDBLOCK);
689 sc->old_style_fans = (error != 0);
691 return (sc->old_style_fans);
695 smu_fan_set_rpm(struct smu_fan *fan, int rpm)
697 device_t smu = fan->dev;
704 /* Clamp to allowed range */
705 rpm = max(fan->fan.min_rpm, rpm);
706 rpm = min(fan->fan.max_rpm, rpm);
708 smu_fan_check_old_style(fan);
710 if (!fan->old_style) {
713 cmd.data[1] = fan->reg;
714 cmd.data[2] = (rpm >> 8) & 0xff;
715 cmd.data[3] = rpm & 0xff;
717 error = smu_run_cmd(smu, &cmd, 1);
718 if (error && error != EWOULDBLOCK)
722 cmd.data[0] = 0x00; /* RPM fan. */
723 cmd.data[1] = 1 << fan->reg;
724 cmd.data[2 + 2*fan->reg] = (rpm >> 8) & 0xff;
725 cmd.data[3 + 2*fan->reg] = rpm & 0xff;
726 error = smu_run_cmd(smu, &cmd, 1);
736 smu_fan_read_rpm(struct smu_fan *fan)
738 device_t smu = fan->dev;
742 smu_fan_check_old_style(fan);
744 if (!fan->old_style) {
748 cmd.data[1] = fan->reg;
750 error = smu_run_cmd(smu, &cmd, 1);
751 if (error && error != EWOULDBLOCK)
754 rpm = (cmd.data[0] << 8) | cmd.data[1];
757 if (fan->old_style) {
760 cmd.data[0] = SMU_RPM_STATUS;
762 error = smu_run_cmd(smu, &cmd, 1);
766 rpm = (cmd.data[fan->reg*2+1] << 8) | cmd.data[fan->reg*2+2];
772 smu_fan_set_pwm(struct smu_fan *fan, int pwm)
774 device_t smu = fan->dev;
781 /* Clamp to allowed range */
782 pwm = max(fan->fan.min_rpm, pwm);
783 pwm = min(fan->fan.max_rpm, pwm);
786 * Apple has two fan control mechanisms. We can't distinguish
787 * them except by seeing if the new one fails. If the new one
788 * fails, use the old one.
791 if (!fan->old_style) {
794 cmd.data[1] = fan->reg;
795 cmd.data[2] = (pwm >> 8) & 0xff;
796 cmd.data[3] = pwm & 0xff;
798 error = smu_run_cmd(smu, &cmd, 1);
799 if (error && error != EWOULDBLOCK)
803 if (fan->old_style) {
805 cmd.data[0] = 0x10; /* PWM fan. */
806 cmd.data[1] = 1 << fan->reg;
807 cmd.data[2 + 2*fan->reg] = (pwm >> 8) & 0xff;
808 cmd.data[3 + 2*fan->reg] = pwm & 0xff;
809 error = smu_run_cmd(smu, &cmd, 1);
819 smu_fan_read_pwm(struct smu_fan *fan, int *pwm, int *rpm)
821 device_t smu = fan->dev;
825 if (!fan->old_style) {
829 cmd.data[1] = fan->reg;
831 error = smu_run_cmd(smu, &cmd, 1);
832 if (error && error != EWOULDBLOCK)
835 *rpm = (cmd.data[0] << 8) | cmd.data[1];
838 if (fan->old_style) {
841 cmd.data[0] = SMU_PWM_STATUS;
843 error = smu_run_cmd(smu, &cmd, 1);
847 *rpm = (cmd.data[fan->reg*2+1] << 8) | cmd.data[fan->reg*2+2];
849 if (fan->old_style) {
852 cmd.data[0] = SMU_PWM_SETPOINT;
853 cmd.data[1] = 1 << fan->reg;
855 error = smu_run_cmd(smu, &cmd, 1);
859 *pwm = cmd.data[fan->reg*2+2];
865 smu_fanrpm_sysctl(SYSCTL_HANDLER_ARGS)
868 struct smu_softc *sc;
870 int pwm = 0, rpm, error = 0;
873 sc = device_get_softc(smu);
874 fan = &sc->sc_fans[arg2 & 0xff];
876 if (fan->type == SMU_FAN_RPM) {
877 rpm = smu_fan_read_rpm(fan);
881 error = sysctl_handle_int(oidp, &rpm, 0, req);
883 error = smu_fan_read_pwm(fan, &pwm, &rpm);
887 switch (arg2 & 0xff00) {
888 case SMU_PWM_SYSCTL_PWM:
889 error = sysctl_handle_int(oidp, &pwm, 0, req);
891 case SMU_PWM_SYSCTL_RPM:
892 error = sysctl_handle_int(oidp, &rpm, 0, req);
895 /* This should never happen */
899 /* We can only read the RPM from a PWM controlled fan, so return. */
900 if ((arg2 & 0xff00) == SMU_PWM_SYSCTL_RPM)
903 if (error || !req->newptr)
906 sc->sc_lastuserchange = time_uptime;
908 if (fan->type == SMU_FAN_RPM)
909 return (smu_fan_set_rpm(fan, rpm));
911 return (smu_fan_set_pwm(fan, pwm));
915 smu_fill_fan_prop(device_t dev, phandle_t child, int id)
918 struct smu_softc *sc;
921 sc = device_get_softc(dev);
922 fan = &sc->sc_fans[id];
924 OF_getprop(child, "device_type", type, sizeof(type));
925 /* We have either RPM or PWM controlled fans. */
926 if (strcmp(type, "fan-rpm-control") == 0)
927 fan->type = SMU_FAN_RPM;
929 fan->type = SMU_FAN_PWM;
933 OF_getprop(child, "reg", &fan->reg,
935 OF_getprop(child, "min-value", &fan->fan.min_rpm,
937 OF_getprop(child, "max-value", &fan->fan.max_rpm,
939 OF_getprop(child, "zone", &fan->fan.zone,
942 if (OF_getprop(child, "unmanaged-value",
943 &fan->fan.default_rpm,
944 sizeof(int)) != sizeof(int))
945 fan->fan.default_rpm = fan->fan.max_rpm;
947 OF_getprop(child, "location", fan->fan.name,
948 sizeof(fan->fan.name));
950 if (fan->type == SMU_FAN_RPM)
951 fan->setpoint = smu_fan_read_rpm(fan);
953 smu_fan_read_pwm(fan, &fan->setpoint, &fan->rpm);
956 /* On the first call count the number of fans. In the second call,
957 * after allocating the fan struct, fill the properties of the fans.
960 smu_count_fans(device_t dev)
962 struct smu_softc *sc;
963 phandle_t child, node, root;
966 node = ofw_bus_get_node(dev);
967 sc = device_get_softc(dev);
969 /* First find the fanroots and count the number of fans. */
970 for (root = OF_child(node); root != 0; root = OF_peer(root)) {
972 memset(name, 0, sizeof(name));
973 OF_getprop(root, "name", name, sizeof(name));
974 if (strncmp(name, "rpm-fans", 9) == 0 ||
975 strncmp(name, "pwm-fans", 9) == 0 ||
976 strncmp(name, "fans", 5) == 0)
977 for (child = OF_child(root); child != 0;
978 child = OF_peer(child)) {
980 /* When allocated, fill the fan properties. */
981 if (sc->sc_fans != NULL) {
982 smu_fill_fan_prop(dev, child,
988 device_printf(dev, "WARNING: No fans detected!\n");
995 smu_attach_fans(device_t dev, phandle_t fanroot)
998 struct smu_softc *sc;
999 struct sysctl_oid *oid, *fanroot_oid;
1000 struct sysctl_ctx_list *ctx;
1001 char sysctl_name[32];
1004 sc = device_get_softc(dev);
1006 /* Get the number of fans. */
1007 sc->sc_nfans = smu_count_fans(dev);
1008 if (sc->sc_nfans == 0)
1011 /* Now we're able to allocate memory for the fans struct. */
1012 sc->sc_fans = malloc(sc->sc_nfans * sizeof(struct smu_fan), M_SMU,
1015 /* Now fill in the properties. */
1016 smu_count_fans(dev);
1018 /* Register fans with pmac_thermal */
1019 for (i = 0; i < sc->sc_nfans; i++)
1020 pmac_thermal_fan_register(&sc->sc_fans[i].fan);
1022 ctx = device_get_sysctl_ctx(dev);
1023 fanroot_oid = SYSCTL_ADD_NODE(ctx,
1024 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "fans",
1025 CTLFLAG_RD, 0, "SMU Fan Information");
1028 for (i = 0; i < sc->sc_nfans; i++) {
1029 fan = &sc->sc_fans[i];
1030 for (j = 0; j < strlen(fan->fan.name); j++) {
1031 sysctl_name[j] = tolower(fan->fan.name[j]);
1032 if (isspace(sysctl_name[j]))
1033 sysctl_name[j] = '_';
1036 if (fan->type == SMU_FAN_RPM) {
1037 oid = SYSCTL_ADD_NODE(ctx,
1038 SYSCTL_CHILDREN(fanroot_oid),
1039 OID_AUTO, sysctl_name,
1040 CTLFLAG_RD, 0, "Fan Information");
1041 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
1042 "minrpm", CTLFLAG_RD,
1043 &fan->fan.min_rpm, 0,
1044 "Minimum allowed RPM");
1045 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
1046 "maxrpm", CTLFLAG_RD,
1047 &fan->fan.max_rpm, 0,
1048 "Maximum allowed RPM");
1049 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
1050 "rpm",CTLTYPE_INT | CTLFLAG_RW |
1051 CTLFLAG_MPSAFE, dev, i,
1052 smu_fanrpm_sysctl, "I", "Fan RPM");
1054 fan->fan.read = (int (*)(struct pmac_fan *))smu_fan_read_rpm;
1055 fan->fan.set = (int (*)(struct pmac_fan *, int))smu_fan_set_rpm;
1058 oid = SYSCTL_ADD_NODE(ctx,
1059 SYSCTL_CHILDREN(fanroot_oid),
1060 OID_AUTO, sysctl_name,
1061 CTLFLAG_RD, 0, "Fan Information");
1062 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
1063 "minpwm", CTLFLAG_RD,
1064 &fan->fan.min_rpm, 0,
1065 "Minimum allowed PWM in %");
1066 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
1067 "maxpwm", CTLFLAG_RD,
1068 &fan->fan.max_rpm, 0,
1069 "Maximum allowed PWM in %");
1070 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
1071 "pwm",CTLTYPE_INT | CTLFLAG_RW |
1072 CTLFLAG_MPSAFE, dev,
1073 SMU_PWM_SYSCTL_PWM | i,
1074 smu_fanrpm_sysctl, "I", "Fan PWM in %");
1075 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
1076 "rpm",CTLTYPE_INT | CTLFLAG_RD |
1077 CTLFLAG_MPSAFE, dev,
1078 SMU_PWM_SYSCTL_RPM | i,
1079 smu_fanrpm_sysctl, "I", "Fan RPM");
1080 fan->fan.read = NULL;
1081 fan->fan.set = (int (*)(struct pmac_fan *, int))smu_fan_set_pwm;
1085 device_printf(dev, "Fan: %s type: %d\n",
1086 fan->fan.name, fan->type);
1091 smu_sensor_read(struct smu_sensor *sens)
1093 device_t smu = sens->dev;
1095 struct smu_softc *sc;
1101 cmd.data[0] = sens->reg;
1104 error = smu_run_cmd(smu, &cmd, 1);
1108 sc = device_get_softc(smu);
1109 value = (cmd.data[0] << 8) | cmd.data[1];
1111 switch (sens->type) {
1112 case SMU_TEMP_SENSOR:
1113 value *= sc->sc_cpu_diode_scale;
1115 value += ((int64_t)sc->sc_cpu_diode_offset) << 9;
1118 /* Convert from 16.16 fixed point degC into integer 0.1 K. */
1119 value = 10*(value >> 16) + ((10*(value & 0xffff)) >> 16) + 2731;
1121 case SMU_VOLTAGE_SENSOR:
1122 value *= sc->sc_cpu_volt_scale;
1123 value += sc->sc_cpu_volt_offset;
1126 /* Convert from 16.16 fixed point V into mV. */
1131 case SMU_CURRENT_SENSOR:
1132 value *= sc->sc_cpu_curr_scale;
1133 value += sc->sc_cpu_curr_offset;
1136 /* Convert from 16.16 fixed point A into mA. */
1141 case SMU_POWER_SENSOR:
1142 value *= sc->sc_slots_pow_scale;
1143 value += sc->sc_slots_pow_offset;
1146 /* Convert from 16.16 fixed point W into mW. */
1157 smu_sensor_sysctl(SYSCTL_HANDLER_ARGS)
1160 struct smu_softc *sc;
1161 struct smu_sensor *sens;
1165 sc = device_get_softc(smu);
1166 sens = &sc->sc_sensors[arg2];
1168 value = smu_sensor_read(sens);
1172 error = sysctl_handle_int(oidp, &value, 0, req);
1178 smu_attach_sensors(device_t dev, phandle_t sensroot)
1180 struct smu_sensor *sens;
1181 struct smu_softc *sc;
1182 struct sysctl_oid *sensroot_oid;
1183 struct sysctl_ctx_list *ctx;
1188 sc = device_get_softc(dev);
1189 sc->sc_nsensors = 0;
1191 for (child = OF_child(sensroot); child != 0; child = OF_peer(child))
1194 if (sc->sc_nsensors == 0) {
1195 device_printf(dev, "WARNING: No sensors detected!\n");
1199 sc->sc_sensors = malloc(sc->sc_nsensors * sizeof(struct smu_sensor),
1200 M_SMU, M_WAITOK | M_ZERO);
1202 sens = sc->sc_sensors;
1203 sc->sc_nsensors = 0;
1205 ctx = device_get_sysctl_ctx(dev);
1206 sensroot_oid = SYSCTL_ADD_NODE(ctx,
1207 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "sensors",
1208 CTLFLAG_RD, 0, "SMU Sensor Information");
1210 for (child = OF_child(sensroot); child != 0; child = OF_peer(child)) {
1211 char sysctl_name[40], sysctl_desc[40];
1215 OF_getprop(child, "device_type", type, sizeof(type));
1217 if (strcmp(type, "current-sensor") == 0) {
1218 sens->type = SMU_CURRENT_SENSOR;
1220 } else if (strcmp(type, "temp-sensor") == 0) {
1221 sens->type = SMU_TEMP_SENSOR;
1223 } else if (strcmp(type, "voltage-sensor") == 0) {
1224 sens->type = SMU_VOLTAGE_SENSOR;
1226 } else if (strcmp(type, "power-sensor") == 0) {
1227 sens->type = SMU_POWER_SENSOR;
1233 OF_getprop(child, "reg", &sens->reg, sizeof(cell_t));
1234 OF_getprop(child, "zone", &sens->therm.zone, sizeof(int));
1235 OF_getprop(child, "location", sens->therm.name,
1236 sizeof(sens->therm.name));
1238 for (i = 0; i < strlen(sens->therm.name); i++) {
1239 sysctl_name[i] = tolower(sens->therm.name[i]);
1240 if (isspace(sysctl_name[i]))
1241 sysctl_name[i] = '_';
1245 sprintf(sysctl_desc,"%s (%s)", sens->therm.name, units);
1247 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(sensroot_oid), OID_AUTO,
1248 sysctl_name, CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
1249 dev, sc->sc_nsensors, smu_sensor_sysctl,
1250 (sens->type == SMU_TEMP_SENSOR) ? "IK" : "I", sysctl_desc);
1252 if (sens->type == SMU_TEMP_SENSOR) {
1253 /* Make up some numbers */
1254 sens->therm.target_temp = 500 + 2731; /* 50 C */
1255 sens->therm.max_temp = 900 + 2731; /* 90 C */
1258 (int (*)(struct pmac_therm *))smu_sensor_read;
1259 pmac_thermal_sensor_register(&sens->therm);
1268 smu_set_sleepled(void *xdev, int onoff)
1270 static struct smu_cmd cmd;
1271 device_t smu = xdev;
1275 cmd.data[0] = SMU_MISC_LED_CTRL;
1277 cmd.data[2] = onoff;
1279 smu_run_cmd(smu, &cmd, 0);
1283 smu_server_mode(SYSCTL_HANDLER_ARGS)
1287 device_t smu = arg1;
1290 cmd.cmd = SMU_POWER_EVENTS;
1292 cmd.data[0] = SMU_PWR_GET_POWERUP;
1294 error = smu_run_cmd(smu, &cmd, 1);
1299 server_mode = (cmd.data[1] & SMU_WAKEUP_AC_INSERT) ? 1 : 0;
1301 error = sysctl_handle_int(oidp, &server_mode, 0, req);
1303 if (error || !req->newptr)
1306 if (server_mode == 1)
1307 cmd.data[0] = SMU_PWR_SET_POWERUP;
1308 else if (server_mode == 0)
1309 cmd.data[0] = SMU_PWR_CLR_POWERUP;
1315 cmd.data[2] = SMU_WAKEUP_AC_INSERT;
1317 return (smu_run_cmd(smu, &cmd, 1));
1321 smu_shutdown(void *xdev, int howto)
1323 device_t smu = xdev;
1326 cmd.cmd = SMU_POWER;
1327 if (howto & RB_HALT)
1328 strcpy(cmd.data, "SHUTDOWN");
1330 strcpy(cmd.data, "RESTART");
1332 cmd.len = strlen(cmd.data);
1334 smu_run_cmd(smu, &cmd, 1);
1340 smu_gettime(device_t dev, struct timespec *ts)
1343 struct clocktime ct;
1347 cmd.data[0] = SMU_RTC_GET;
1349 if (smu_run_cmd(dev, &cmd, 1) != 0)
1353 ct.sec = bcd2bin(cmd.data[0]);
1354 ct.min = bcd2bin(cmd.data[1]);
1355 ct.hour = bcd2bin(cmd.data[2]);
1356 ct.dow = bcd2bin(cmd.data[3]);
1357 ct.day = bcd2bin(cmd.data[4]);
1358 ct.mon = bcd2bin(cmd.data[5]);
1359 ct.year = bcd2bin(cmd.data[6]) + 2000;
1361 return (clock_ct_to_ts(&ct, ts));
1365 smu_settime(device_t dev, struct timespec *ts)
1367 static struct smu_cmd cmd;
1368 struct clocktime ct;
1372 cmd.data[0] = SMU_RTC_SET;
1374 clock_ts_to_ct(ts, &ct);
1376 cmd.data[1] = bin2bcd(ct.sec);
1377 cmd.data[2] = bin2bcd(ct.min);
1378 cmd.data[3] = bin2bcd(ct.hour);
1379 cmd.data[4] = bin2bcd(ct.dow);
1380 cmd.data[5] = bin2bcd(ct.day);
1381 cmd.data[6] = bin2bcd(ct.mon);
1382 cmd.data[7] = bin2bcd(ct.year - 2000);
1384 return (smu_run_cmd(dev, &cmd, 0));
1387 /* SMU I2C Interface */
1389 static int smuiic_probe(device_t dev);
1390 static int smuiic_attach(device_t dev);
1391 static int smuiic_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs);
1392 static phandle_t smuiic_get_node(device_t bus, device_t dev);
1394 static device_method_t smuiic_methods[] = {
1395 /* device interface */
1396 DEVMETHOD(device_probe, smuiic_probe),
1397 DEVMETHOD(device_attach, smuiic_attach),
1399 /* iicbus interface */
1400 DEVMETHOD(iicbus_callback, iicbus_null_callback),
1401 DEVMETHOD(iicbus_transfer, smuiic_transfer),
1403 /* ofw_bus interface */
1404 DEVMETHOD(ofw_bus_get_node, smuiic_get_node),
1409 struct smuiic_softc {
1411 volatile int sc_iic_inuse;
1415 static driver_t smuiic_driver = {
1418 sizeof(struct smuiic_softc)
1420 static devclass_t smuiic_devclass;
1422 DRIVER_MODULE(smuiic, smu, smuiic_driver, smuiic_devclass, 0, 0);
1425 smu_attach_i2c(device_t smu, phandle_t i2croot)
1429 struct ofw_bus_devinfo *dinfo;
1432 for (child = OF_child(i2croot); child != 0; child = OF_peer(child)) {
1433 if (OF_getprop(child, "name", name, sizeof(name)) <= 0)
1436 if (strcmp(name, "i2c-bus") != 0 && strcmp(name, "i2c") != 0)
1439 dinfo = malloc(sizeof(struct ofw_bus_devinfo), M_SMU,
1441 if (ofw_bus_gen_setup_devinfo(dinfo, child) != 0) {
1446 cdev = device_add_child(smu, NULL, -1);
1448 device_printf(smu, "<%s>: device_add_child failed\n",
1450 ofw_bus_gen_destroy_devinfo(dinfo);
1454 device_set_ivars(cdev, dinfo);
1459 smuiic_probe(device_t dev)
1463 name = ofw_bus_get_name(dev);
1467 if (strcmp(name, "i2c-bus") == 0 || strcmp(name, "i2c") == 0) {
1468 device_set_desc(dev, "SMU I2C controller");
1476 smuiic_attach(device_t dev)
1478 struct smuiic_softc *sc = device_get_softc(dev);
1479 mtx_init(&sc->sc_mtx, "smuiic", NULL, MTX_DEF);
1480 sc->sc_iic_inuse = 0;
1482 /* Get our bus number */
1483 OF_getprop(ofw_bus_get_node(dev), "reg", &sc->sc_busno,
1484 sizeof(sc->sc_busno));
1486 /* Add the IIC bus layer */
1487 device_add_child(dev, "iicbus", -1);
1489 return (bus_generic_attach(dev));
1493 smuiic_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
1495 struct smuiic_softc *sc = device_get_softc(dev);
1499 mtx_lock(&sc->sc_mtx);
1500 while (sc->sc_iic_inuse)
1501 mtx_sleep(sc, &sc->sc_mtx, 0, "smuiic", 100);
1503 sc->sc_iic_inuse = 1;
1506 for (i = 0; i < nmsgs; i++) {
1508 cmd.data[0] = sc->sc_busno;
1509 if (msgs[i].flags & IIC_M_NOSTOP)
1510 cmd.data[1] = SMU_I2C_COMBINED;
1512 cmd.data[1] = SMU_I2C_SIMPLE;
1514 cmd.data[2] = msgs[i].slave;
1515 if (msgs[i].flags & IIC_M_RD)
1518 if (msgs[i].flags & IIC_M_NOSTOP) {
1519 KASSERT(msgs[i].len < 4,
1520 ("oversize I2C combined message"));
1522 cmd.data[3] = min(msgs[i].len, 3);
1523 memcpy(&cmd.data[4], msgs[i].buf, min(msgs[i].len, 3));
1524 i++; /* Advance to next part of message */
1527 memset(&cmd.data[4], 0, 3);
1530 cmd.data[7] = msgs[i].slave;
1531 if (msgs[i].flags & IIC_M_RD)
1534 cmd.data[8] = msgs[i].len;
1535 if (msgs[i].flags & IIC_M_RD) {
1536 memset(&cmd.data[9], 0xff, msgs[i].len);
1539 memcpy(&cmd.data[9], msgs[i].buf, msgs[i].len);
1540 cmd.len = 9 + msgs[i].len;
1543 mtx_unlock(&sc->sc_mtx);
1544 smu_run_cmd(device_get_parent(dev), &cmd, 1);
1545 mtx_lock(&sc->sc_mtx);
1547 for (j = 0; j < 10; j++) {
1551 memset(&cmd.data[1], 0xff, msgs[i].len);
1553 mtx_unlock(&sc->sc_mtx);
1554 smu_run_cmd(device_get_parent(dev), &cmd, 1);
1555 mtx_lock(&sc->sc_mtx);
1557 if (!(cmd.data[0] & 0x80))
1560 mtx_sleep(sc, &sc->sc_mtx, 0, "smuiic", 10);
1563 if (cmd.data[0] & 0x80) {
1568 memcpy(msgs[i].buf, &cmd.data[1], msgs[i].len);
1569 msgs[i].len = cmd.len - 1;
1573 sc->sc_iic_inuse = 0;
1574 mtx_unlock(&sc->sc_mtx);
1580 smuiic_get_node(device_t bus, device_t dev)
1583 return (ofw_bus_get_node(bus));