xref: /freebsd/sys/powerpc/powermac/smu.c (revision 0957b409)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2009 Nathan Whitehorn
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  *
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/bus.h>
35 #include <sys/systm.h>
36 #include <sys/module.h>
37 #include <sys/conf.h>
38 #include <sys/cpu.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>
44 #include <sys/rman.h>
45 #include <sys/sysctl.h>
46 #include <sys/unistd.h>
47 
48 #include <machine/bus.h>
49 #include <machine/intr_machdep.h>
50 #include <machine/md_var.h>
51 
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>
60 
61 #include "clock_if.h"
62 #include "iicbus_if.h"
63 
64 struct smu_cmd {
65 	volatile uint8_t cmd;
66 	uint8_t		len;
67 	uint8_t		data[254];
68 
69 	STAILQ_ENTRY(smu_cmd) cmd_q;
70 };
71 
72 STAILQ_HEAD(smu_cmdq, smu_cmd);
73 
74 struct smu_fan {
75 	struct pmac_fan fan;
76 	device_t dev;
77 	cell_t	reg;
78 
79 	enum {
80 		SMU_FAN_RPM,
81 		SMU_FAN_PWM
82 	} type;
83 	int	setpoint;
84 	int	old_style;
85 	int     rpm;
86 };
87 
88 /* We can read the PWM and the RPM from a PWM controlled fan.
89  * Offer both values via sysctl.
90  */
91 enum {
92 	SMU_PWM_SYSCTL_PWM   = 1 << 8,
93 	SMU_PWM_SYSCTL_RPM   = 2 << 8
94 };
95 
96 struct smu_sensor {
97 	struct pmac_therm therm;
98 	device_t dev;
99 
100 	cell_t	reg;
101 	enum {
102 		SMU_CURRENT_SENSOR,
103 		SMU_VOLTAGE_SENSOR,
104 		SMU_POWER_SENSOR,
105 		SMU_TEMP_SENSOR
106 	} type;
107 };
108 
109 struct smu_softc {
110 	device_t	sc_dev;
111 	struct mtx	sc_mtx;
112 
113 	struct resource	*sc_memr;
114 	int		sc_memrid;
115 	int		sc_u3;
116 
117 	bus_dma_tag_t	sc_dmatag;
118 	bus_space_tag_t	sc_bt;
119 	bus_space_handle_t sc_mailbox;
120 
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;
125 
126 	struct smu_fan	*sc_fans;
127 	int		sc_nfans;
128 	int		old_style_fans;
129 	struct smu_sensor *sc_sensors;
130 	int		sc_nsensors;
131 
132 	int		sc_doorbellirqid;
133 	struct resource	*sc_doorbellirq;
134 	void		*sc_doorbellirqcookie;
135 
136 	struct proc	*sc_fanmgt_proc;
137 	time_t		sc_lastuserchange;
138 
139 	/* Calibration data */
140 	uint16_t	sc_cpu_diode_scale;
141 	int16_t		sc_cpu_diode_offset;
142 
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;
147 
148 	uint16_t	sc_slots_pow_scale;
149 	int16_t		sc_slots_pow_offset;
150 
151 	struct cdev 	*sc_leddev;
152 };
153 
154 /* regular bus attachment functions */
155 
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);
160 
161 /* cpufreq notification hooks */
162 
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);
165 
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);
169 
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,
173 		    size_t len);
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);
181 
182 /* where to find the doorbell GPIO */
183 
184 static device_t	smu_doorbell = NULL;
185 
186 static device_method_t  smu_methods[] = {
187 	/* Device interface */
188 	DEVMETHOD(device_probe,		smu_probe),
189 	DEVMETHOD(device_attach,	smu_attach),
190 
191 	/* Clock interface */
192 	DEVMETHOD(clock_gettime,	smu_gettime),
193 	DEVMETHOD(clock_settime,	smu_settime),
194 
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),
203 
204 	{ 0, 0 },
205 };
206 
207 static driver_t smu_driver = {
208 	"smu",
209 	smu_methods,
210 	sizeof(struct smu_softc)
211 };
212 
213 static devclass_t smu_devclass;
214 
215 DRIVER_MODULE(smu, ofwbus, smu_driver, smu_devclass, 0, 0);
216 static MALLOC_DEFINE(M_SMU, "smu", "SMU Sensor Information");
217 
218 #define SMU_MAILBOX		0x8000860c
219 #define SMU_FANMGT_INTERVAL	1000 /* ms */
220 
221 /* Command types */
222 #define SMU_ADC			0xd8
223 #define SMU_FAN			0x4a
224 #define SMU_RPM_STATUS		0x01
225 #define SMU_RPM_SETPOINT	0x02
226 #define SMU_PWM_STATUS		0x11
227 #define SMU_PWM_SETPOINT	0x12
228 #define SMU_I2C			0x9a
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
240 #define SMU_RTC			0x8e
241 #define  SMU_RTC_GET		0x81
242 #define  SMU_RTC_SET		0x80
243 
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
249 
250 /* Data blocks */
251 #define SMU_CPUTEMP_CAL		0x18
252 #define SMU_CPUVOLT_CAL		0x21
253 #define SMU_SLOTPW_CAL		0x78
254 
255 /* Partitions */
256 #define SMU_PARTITION		0x3e
257 #define SMU_PARTITION_LATEST	0x01
258 #define SMU_PARTITION_BASE	0x02
259 #define SMU_PARTITION_UPDATE	0x03
260 
261 static int
262 smu_probe(device_t dev)
263 {
264 	const char *name = ofw_bus_get_name(dev);
265 
266 	if (strcmp(name, "smu") != 0)
267 		return (ENXIO);
268 
269 	device_set_desc(dev, "Apple System Management Unit");
270 	return (0);
271 }
272 
273 static void
274 smu_phys_callback(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
275 {
276 	struct smu_softc *sc = xsc;
277 
278 	sc->sc_cmd_phys = segs[0].ds_addr;
279 }
280 
281 static int
282 smu_attach(device_t dev)
283 {
284 	struct smu_softc *sc;
285 	phandle_t	node, child;
286 	uint8_t		data[12];
287 
288 	sc = device_get_softc(dev);
289 
290 	mtx_init(&sc->sc_mtx, "smu", NULL, MTX_DEF);
291 	sc->sc_cur_cmd = NULL;
292 	sc->sc_doorbellirqid = -1;
293 
294 	sc->sc_u3 = 0;
295 	if (OF_finddevice("/u3") != -1)
296 		sc->sc_u3 = 1;
297 
298 	/*
299 	 * Map the mailbox area. This should be determined from firmware,
300 	 * but I have not found a simple way to do that.
301 	 */
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);
307 
308 	/*
309 	 * Allocate the command buffer. This can be anywhere in the low 4 GB
310 	 * of memory.
311 	 */
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);
317 
318 	/*
319 	 * Set up handlers to change CPU voltage when CPU frequency is changed.
320 	 */
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);
325 
326 	node = ofw_bus_get_node(dev);
327 
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.
330 	 */
331 	smu_attach_fans(dev, node);
332 
333 	/*
334 	 * Now detect and attach the other child devices.
335 	 */
336 	for (child = OF_child(node); child != 0; child = OF_peer(child)) {
337 		char name[32];
338 		memset(name, 0, sizeof(name));
339 		OF_getprop(child, "name", name, sizeof(name));
340 
341 		if (strncmp(name, "sensors", 8) == 0)
342 			smu_attach_sensors(dev, child);
343 
344 		if (strncmp(name, "smu-i2c-control", 15) == 0)
345 			smu_attach_i2c(dev, child);
346 	}
347 
348 	/* Some SMUs have the I2C children directly under the bus. */
349 	smu_attach_i2c(dev, node);
350 
351 	/*
352 	 * Collect calibration constants.
353 	 */
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];
357 
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];
363 
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];
367 
368 	/*
369 	 * Set up LED interface
370 	 */
371 	sc->sc_leddev = led_create(smu_set_sleepled, dev, "sleepled");
372 
373 	/*
374 	 * Reset on power loss behavior
375 	 */
376 
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");
381 
382 	/*
383 	 * Set up doorbell interrupt.
384 	 */
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);
393 
394 	/*
395 	 * Connect RTC interface.
396 	 */
397 	clock_register(dev, 1000);
398 
399 	/*
400 	 * Learn about shutdown events
401 	 */
402 	EVENTHANDLER_REGISTER(shutdown_final, smu_shutdown, dev,
403 	    SHUTDOWN_PRI_LAST);
404 
405 	return (bus_generic_attach(dev));
406 }
407 
408 static const struct ofw_bus_devinfo *
409 smu_get_devinfo(device_t bus, device_t dev)
410 {
411 
412 	return (device_get_ivars(dev));
413 }
414 
415 static void
416 smu_send_cmd(device_t dev, struct smu_cmd *cmd)
417 {
418 	struct smu_softc *sc;
419 
420 	sc = device_get_softc(dev);
421 
422 	mtx_assert(&sc->sc_mtx, MA_OWNED);
423 
424 	if (sc->sc_u3)
425 		powerpc_pow_enabled = 0; /* SMU cannot work if we go to NAP */
426 
427 	sc->sc_cur_cmd = cmd;
428 
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);
435 
436 	/* Flush the cacheline it is in -- SMU bypasses the cache */
437 	__asm __volatile("sync; dcbf 0,%0; sync" :: "r"(sc->sc_cmd): "memory");
438 
439 	/* Ring SMU doorbell */
440 	macgpio_write(smu_doorbell, GPIO_DDR_OUTPUT);
441 }
442 
443 static void
444 smu_doorbell_intr(void *xdev)
445 {
446 	device_t smu;
447 	struct smu_softc *sc;
448 	int doorbell_ack;
449 
450 	smu = xdev;
451 	doorbell_ack = macgpio_read(smu_doorbell);
452 	sc = device_get_softc(smu);
453 
454 	if (doorbell_ack != (GPIO_DDR_OUTPUT | GPIO_LEVEL_RO | GPIO_DATA))
455 		return;
456 
457 	mtx_lock(&sc->sc_mtx);
458 
459 	if (sc->sc_cur_cmd == NULL)	/* spurious */
460 		goto done;
461 
462 	/* Check result. First invalidate the cache again... */
463 	__asm __volatile("dcbf 0,%0; sync" :: "r"(sc->sc_cmd) : "memory");
464 
465 	bus_dmamap_sync(sc->sc_dmatag, sc->sc_cmd_dmamap, BUS_DMASYNC_POSTREAD);
466 
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;
473 	if (sc->sc_u3)
474 		powerpc_pow_enabled = 1;
475 
476     done:
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);
482 	}
483 
484 	mtx_unlock(&sc->sc_mtx);
485 }
486 
487 static int
488 smu_run_cmd(device_t dev, struct smu_cmd *cmd, int wait)
489 {
490 	struct smu_softc *sc;
491 	uint8_t cmd_code;
492 	int error;
493 
494 	sc = device_get_softc(dev);
495 	cmd_code = cmd->cmd;
496 
497 	mtx_lock(&sc->sc_mtx);
498 	if (sc->sc_cur_cmd != NULL) {
499 		STAILQ_INSERT_TAIL(&sc->sc_cmdq, cmd, cmd_q);
500 	} else
501 		smu_send_cmd(dev, cmd);
502 	mtx_unlock(&sc->sc_mtx);
503 
504 	if (!wait)
505 		return (0);
506 
507 	if (sc->sc_doorbellirqid < 0) {
508 		/* Poll if the IRQ has not been set up yet */
509 		do {
510 			DELAY(50);
511 			smu_doorbell_intr(dev);
512 		} while (sc->sc_cur_cmd != NULL);
513 	} else {
514 		/* smu_doorbell_intr will wake us when the command is ACK'ed */
515 		error = tsleep(cmd, 0, "smu", 800 * hz / 1000);
516 		if (error != 0)
517 			smu_doorbell_intr(dev);	/* One last chance */
518 
519 		if (error != 0) {
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;
525 			else
526 				STAILQ_REMOVE(&sc->sc_cmdq, cmd, smu_cmd,
527 				    cmd_q);
528 			mtx_unlock(&sc->sc_mtx);
529 			return (error);
530 		    }
531 		    error = 0;
532 		    mtx_unlock(&sc->sc_mtx);
533 		}
534 	}
535 
536 	/* SMU acks the command by inverting the command bits */
537 	if (cmd->cmd == ((~cmd_code) & 0xff))
538 		error = 0;
539 	else
540 		error = EIO;
541 
542 	return (error);
543 }
544 
545 static int
546 smu_get_datablock(device_t dev, int8_t id, uint8_t *buf, size_t len)
547 {
548 	struct smu_cmd cmd;
549 	uint8_t addr[4];
550 
551 	cmd.cmd = SMU_PARTITION;
552 	cmd.len = 2;
553 	cmd.data[0] = SMU_PARTITION_LATEST;
554 	cmd.data[1] = id;
555 
556 	smu_run_cmd(dev, &cmd, 1);
557 
558 	addr[0] = addr[1] = 0;
559 	addr[2] = cmd.data[0];
560 	addr[3] = cmd.data[1];
561 
562 	cmd.cmd = SMU_MISC;
563 	cmd.len = 7;
564 	cmd.data[0] = SMU_MISC_GET_DATA;
565 	cmd.data[1] = sizeof(addr);
566 	memcpy(&cmd.data[2], addr, sizeof(addr));
567 	cmd.data[6] = len;
568 
569 	smu_run_cmd(dev, &cmd, 1);
570 	memcpy(buf, cmd.data, len);
571 	return (0);
572 }
573 
574 static void
575 smu_slew_cpu_voltage(device_t dev, int to)
576 {
577 	struct smu_cmd cmd;
578 
579 	cmd.cmd = SMU_POWER;
580 	cmd.len = 8;
581 	cmd.data[0] = 'V';
582 	cmd.data[1] = 'S';
583 	cmd.data[2] = 'L';
584 	cmd.data[3] = 'E';
585 	cmd.data[4] = 'W';
586 	cmd.data[5] = 0xff;
587 	cmd.data[6] = 1;
588 	cmd.data[7] = to;
589 
590 	smu_run_cmd(dev, &cmd, 1);
591 }
592 
593 static void
594 smu_cpufreq_pre_change(device_t dev, const struct cf_level *level)
595 {
596 	/*
597 	 * Make sure the CPU voltage is raised before we raise
598 	 * the clock.
599 	 */
600 
601 	if (level->rel_set[0].freq == 10000 /* max */)
602 		smu_slew_cpu_voltage(dev, 0);
603 }
604 
605 static void
606 smu_cpufreq_post_change(device_t dev, const struct cf_level *level)
607 {
608 	/* We are safe to reduce CPU voltage after a downward transition */
609 
610 	if (level->rel_set[0].freq < 10000 /* max */)
611 		smu_slew_cpu_voltage(dev, 1); /* XXX: 1/4 voltage for 970MP? */
612 }
613 
614 /* Routines for probing the SMU doorbell GPIO */
615 static int doorbell_probe(device_t dev);
616 static int doorbell_attach(device_t dev);
617 
618 static device_method_t  doorbell_methods[] = {
619 	/* Device interface */
620 	DEVMETHOD(device_probe,		doorbell_probe),
621 	DEVMETHOD(device_attach,	doorbell_attach),
622 	{ 0, 0 },
623 };
624 
625 static driver_t doorbell_driver = {
626 	"smudoorbell",
627 	doorbell_methods,
628 	0
629 };
630 
631 static devclass_t doorbell_devclass;
632 
633 EARLY_DRIVER_MODULE(smudoorbell, macgpio, doorbell_driver, doorbell_devclass,
634     0, 0, BUS_PASS_SUPPORTDEV);
635 
636 static int
637 doorbell_probe(device_t dev)
638 {
639 	const char *name = ofw_bus_get_name(dev);
640 
641 	if (strcmp(name, "smu-doorbell") != 0)
642 		return (ENXIO);
643 
644 	device_set_desc(dev, "SMU Doorbell GPIO");
645 	device_quiet(dev);
646 	return (0);
647 }
648 
649 static int
650 doorbell_attach(device_t dev)
651 {
652 	smu_doorbell = dev;
653 	return (0);
654 }
655 
656 /*
657  * Sensor and fan management
658  */
659 
660 static int
661 smu_fan_check_old_style(struct smu_fan *fan)
662 {
663 	device_t smu = fan->dev;
664 	struct smu_softc *sc = device_get_softc(smu);
665 	struct smu_cmd cmd;
666 	int error;
667 
668 	if (sc->old_style_fans != -1)
669 		return (sc->old_style_fans);
670 
671 	/*
672 	 * Apple has two fan control mechanisms. We can't distinguish
673 	 * them except by seeing if the new one fails. If the new one
674 	 * fails, use the old one.
675 	 */
676 
677 	cmd.cmd = SMU_FAN;
678 	cmd.len = 2;
679 	cmd.data[0] = 0x31;
680 	cmd.data[1] = fan->reg;
681 
682 	do {
683 		error = smu_run_cmd(smu, &cmd, 1);
684 	} while (error == EWOULDBLOCK);
685 
686 	sc->old_style_fans = (error != 0);
687 
688 	return (sc->old_style_fans);
689 }
690 
691 static int
692 smu_fan_set_rpm(struct smu_fan *fan, int rpm)
693 {
694 	device_t smu = fan->dev;
695 	struct smu_cmd cmd;
696 	int error;
697 
698 	cmd.cmd = SMU_FAN;
699 	error = EIO;
700 
701 	/* Clamp to allowed range */
702 	rpm = max(fan->fan.min_rpm, rpm);
703 	rpm = min(fan->fan.max_rpm, rpm);
704 
705 	smu_fan_check_old_style(fan);
706 
707 	if (!fan->old_style) {
708 		cmd.len = 4;
709 		cmd.data[0] = 0x30;
710 		cmd.data[1] = fan->reg;
711 		cmd.data[2] = (rpm >> 8) & 0xff;
712 		cmd.data[3] = rpm & 0xff;
713 
714 		error = smu_run_cmd(smu, &cmd, 1);
715 		if (error && error != EWOULDBLOCK)
716 			fan->old_style = 1;
717 	} else {
718 		cmd.len = 14;
719 		cmd.data[0] = 0x00; /* RPM fan. */
720 		cmd.data[1] = 1 << fan->reg;
721 		cmd.data[2 + 2*fan->reg] = (rpm >> 8) & 0xff;
722 		cmd.data[3 + 2*fan->reg] = rpm & 0xff;
723 		error = smu_run_cmd(smu, &cmd, 1);
724 	}
725 
726 	if (error == 0)
727 		fan->setpoint = rpm;
728 
729 	return (error);
730 }
731 
732 static int
733 smu_fan_read_rpm(struct smu_fan *fan)
734 {
735 	device_t smu = fan->dev;
736 	struct smu_cmd cmd;
737 	int rpm, error;
738 
739 	smu_fan_check_old_style(fan);
740 
741 	if (!fan->old_style) {
742 		cmd.cmd = SMU_FAN;
743 		cmd.len = 2;
744 		cmd.data[0] = 0x31;
745 		cmd.data[1] = fan->reg;
746 
747 		error = smu_run_cmd(smu, &cmd, 1);
748 		if (error && error != EWOULDBLOCK)
749 			fan->old_style = 1;
750 
751 		rpm = (cmd.data[0] << 8) | cmd.data[1];
752 	}
753 
754 	if (fan->old_style) {
755 		cmd.cmd = SMU_FAN;
756 		cmd.len = 1;
757 		cmd.data[0] = SMU_RPM_STATUS;
758 
759 		error = smu_run_cmd(smu, &cmd, 1);
760 		if (error)
761 			return (error);
762 
763 		rpm = (cmd.data[fan->reg*2+1] << 8) | cmd.data[fan->reg*2+2];
764 	}
765 
766 	return (rpm);
767 }
768 static int
769 smu_fan_set_pwm(struct smu_fan *fan, int pwm)
770 {
771 	device_t smu = fan->dev;
772 	struct smu_cmd cmd;
773 	int error;
774 
775 	cmd.cmd = SMU_FAN;
776 	error = EIO;
777 
778 	/* Clamp to allowed range */
779 	pwm = max(fan->fan.min_rpm, pwm);
780 	pwm = min(fan->fan.max_rpm, pwm);
781 
782 	/*
783 	 * Apple has two fan control mechanisms. We can't distinguish
784 	 * them except by seeing if the new one fails. If the new one
785 	 * fails, use the old one.
786 	 */
787 
788 	if (!fan->old_style) {
789 		cmd.len = 4;
790 		cmd.data[0] = 0x30;
791 		cmd.data[1] = fan->reg;
792 		cmd.data[2] = (pwm >> 8) & 0xff;
793 		cmd.data[3] = pwm & 0xff;
794 
795 		error = smu_run_cmd(smu, &cmd, 1);
796 		if (error && error != EWOULDBLOCK)
797 			fan->old_style = 1;
798 	}
799 
800 	if (fan->old_style) {
801 		cmd.len = 14;
802 		cmd.data[0] = 0x10; /* PWM fan. */
803 		cmd.data[1] = 1 << fan->reg;
804 		cmd.data[2 + 2*fan->reg] = (pwm >> 8) & 0xff;
805 		cmd.data[3 + 2*fan->reg] = pwm & 0xff;
806 		error = smu_run_cmd(smu, &cmd, 1);
807 	}
808 
809 	if (error == 0)
810 		fan->setpoint = pwm;
811 
812 	return (error);
813 }
814 
815 static int
816 smu_fan_read_pwm(struct smu_fan *fan, int *pwm, int *rpm)
817 {
818 	device_t smu = fan->dev;
819 	struct smu_cmd cmd;
820 	int error;
821 
822 	if (!fan->old_style) {
823 		cmd.cmd = SMU_FAN;
824 		cmd.len = 2;
825 		cmd.data[0] = 0x31;
826 		cmd.data[1] = fan->reg;
827 
828 		error = smu_run_cmd(smu, &cmd, 1);
829 		if (error && error != EWOULDBLOCK)
830 			fan->old_style = 1;
831 
832 		*rpm = (cmd.data[0] << 8) | cmd.data[1];
833 	}
834 
835 	if (fan->old_style) {
836 		cmd.cmd = SMU_FAN;
837 		cmd.len = 1;
838 		cmd.data[0] = SMU_PWM_STATUS;
839 
840 		error = smu_run_cmd(smu, &cmd, 1);
841 		if (error)
842 			return (error);
843 
844 		*rpm = (cmd.data[fan->reg*2+1] << 8) | cmd.data[fan->reg*2+2];
845 	}
846 	if (fan->old_style) {
847 		cmd.cmd = SMU_FAN;
848 		cmd.len = 14;
849 		cmd.data[0] = SMU_PWM_SETPOINT;
850 		cmd.data[1] = 1 << fan->reg;
851 
852 		error = smu_run_cmd(smu, &cmd, 1);
853 		if (error)
854 			return (error);
855 
856 		*pwm = cmd.data[fan->reg*2+2];
857 	}
858 	return (0);
859 }
860 
861 static int
862 smu_fanrpm_sysctl(SYSCTL_HANDLER_ARGS)
863 {
864 	device_t smu;
865 	struct smu_softc *sc;
866 	struct smu_fan *fan;
867 	int pwm = 0, rpm, error = 0;
868 
869 	smu = arg1;
870 	sc = device_get_softc(smu);
871 	fan = &sc->sc_fans[arg2 & 0xff];
872 
873 	if (fan->type == SMU_FAN_RPM) {
874 		rpm = smu_fan_read_rpm(fan);
875 		if (rpm < 0)
876 			return (rpm);
877 
878 		error = sysctl_handle_int(oidp, &rpm, 0, req);
879 	} else {
880 		error = smu_fan_read_pwm(fan, &pwm, &rpm);
881 		if (error < 0)
882 			return (EIO);
883 
884 		switch (arg2 & 0xff00) {
885 		case SMU_PWM_SYSCTL_PWM:
886 			error = sysctl_handle_int(oidp, &pwm, 0, req);
887 			break;
888 		case SMU_PWM_SYSCTL_RPM:
889 			error = sysctl_handle_int(oidp, &rpm, 0, req);
890 			break;
891 		default:
892 			/* This should never happen */
893 			return (EINVAL);
894 		}
895 	}
896 	/* We can only read the RPM from a PWM controlled fan, so return. */
897 	if ((arg2 & 0xff00) == SMU_PWM_SYSCTL_RPM)
898 		return (0);
899 
900 	if (error || !req->newptr)
901 		return (error);
902 
903 	sc->sc_lastuserchange = time_uptime;
904 
905 	if (fan->type == SMU_FAN_RPM)
906 		return (smu_fan_set_rpm(fan, rpm));
907 	else
908 		return (smu_fan_set_pwm(fan, pwm));
909 }
910 
911 static void
912 smu_fill_fan_prop(device_t dev, phandle_t child, int id)
913 {
914 	struct smu_fan *fan;
915 	struct smu_softc *sc;
916 	char type[32];
917 
918 	sc = device_get_softc(dev);
919 	fan = &sc->sc_fans[id];
920 
921 	OF_getprop(child, "device_type", type, sizeof(type));
922 	/* We have either RPM or PWM controlled fans. */
923 	if (strcmp(type, "fan-rpm-control") == 0)
924 		fan->type = SMU_FAN_RPM;
925 	else
926 		fan->type = SMU_FAN_PWM;
927 
928 	fan->dev = dev;
929 	fan->old_style = 0;
930 	OF_getprop(child, "reg", &fan->reg,
931 		   sizeof(cell_t));
932 	OF_getprop(child, "min-value", &fan->fan.min_rpm,
933 		   sizeof(int));
934 	OF_getprop(child, "max-value", &fan->fan.max_rpm,
935 		   sizeof(int));
936 	OF_getprop(child, "zone", &fan->fan.zone,
937 		   sizeof(int));
938 
939 	if (OF_getprop(child, "unmanaged-value",
940 		       &fan->fan.default_rpm,
941 		       sizeof(int)) != sizeof(int))
942 		fan->fan.default_rpm = fan->fan.max_rpm;
943 
944 	OF_getprop(child, "location", fan->fan.name,
945 		   sizeof(fan->fan.name));
946 
947 	if (fan->type == SMU_FAN_RPM)
948 		fan->setpoint = smu_fan_read_rpm(fan);
949 	else
950 		smu_fan_read_pwm(fan, &fan->setpoint, &fan->rpm);
951 }
952 
953 /* On the first call count the number of fans. In the second call,
954  * after allocating the fan struct, fill the properties of the fans.
955  */
956 static int
957 smu_count_fans(device_t dev)
958 {
959 	struct smu_softc *sc;
960 	phandle_t child, node, root;
961 	int nfans = 0;
962 
963 	node = ofw_bus_get_node(dev);
964 	sc = device_get_softc(dev);
965 
966 	/* First find the fanroots and count the number of fans. */
967 	for (root = OF_child(node); root != 0; root = OF_peer(root)) {
968 		char name[32];
969 		memset(name, 0, sizeof(name));
970 		OF_getprop(root, "name", name, sizeof(name));
971 		if (strncmp(name, "rpm-fans", 9) == 0 ||
972 		    strncmp(name, "pwm-fans", 9) == 0 ||
973 		    strncmp(name, "fans", 5) == 0)
974 			for (child = OF_child(root); child != 0;
975 			     child = OF_peer(child)) {
976 				nfans++;
977 				/* When allocated, fill the fan properties. */
978 				if (sc->sc_fans != NULL) {
979 					smu_fill_fan_prop(dev, child,
980 							  nfans - 1);
981 				}
982 			}
983 	}
984 	if (nfans == 0) {
985 		device_printf(dev, "WARNING: No fans detected!\n");
986 		return (0);
987 	}
988 	return (nfans);
989 }
990 
991 static void
992 smu_attach_fans(device_t dev, phandle_t fanroot)
993 {
994 	struct smu_fan *fan;
995 	struct smu_softc *sc;
996 	struct sysctl_oid *oid, *fanroot_oid;
997 	struct sysctl_ctx_list *ctx;
998 	char sysctl_name[32];
999 	int i, j;
1000 
1001 	sc = device_get_softc(dev);
1002 
1003 	/* Get the number of fans. */
1004 	sc->sc_nfans = smu_count_fans(dev);
1005 	if (sc->sc_nfans == 0)
1006 		return;
1007 
1008 	/* Now we're able to allocate memory for the fans struct. */
1009 	sc->sc_fans = malloc(sc->sc_nfans * sizeof(struct smu_fan), M_SMU,
1010 	    M_WAITOK | M_ZERO);
1011 
1012 	/* Now fill in the properties. */
1013 	smu_count_fans(dev);
1014 
1015 	/* Register fans with pmac_thermal */
1016 	for (i = 0; i < sc->sc_nfans; i++)
1017 		pmac_thermal_fan_register(&sc->sc_fans[i].fan);
1018 
1019 	ctx = device_get_sysctl_ctx(dev);
1020 	fanroot_oid = SYSCTL_ADD_NODE(ctx,
1021 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "fans",
1022 	    CTLFLAG_RD, 0, "SMU Fan Information");
1023 
1024 	/* Add sysctls */
1025 	for (i = 0; i < sc->sc_nfans; i++) {
1026 		fan = &sc->sc_fans[i];
1027 		for (j = 0; j < strlen(fan->fan.name); j++) {
1028 			sysctl_name[j] = tolower(fan->fan.name[j]);
1029 			if (isspace(sysctl_name[j]))
1030 				sysctl_name[j] = '_';
1031 		}
1032 		sysctl_name[j] = 0;
1033 		if (fan->type == SMU_FAN_RPM) {
1034 			oid = SYSCTL_ADD_NODE(ctx,
1035 					      SYSCTL_CHILDREN(fanroot_oid),
1036 					      OID_AUTO, sysctl_name,
1037 					      CTLFLAG_RD, 0, "Fan Information");
1038 			SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
1039 				       "minrpm", CTLFLAG_RD,
1040 				       &fan->fan.min_rpm, 0,
1041 				       "Minimum allowed RPM");
1042 			SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
1043 				       "maxrpm", CTLFLAG_RD,
1044 				       &fan->fan.max_rpm, 0,
1045 				       "Maximum allowed RPM");
1046 			SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
1047 					"rpm",CTLTYPE_INT | CTLFLAG_RW |
1048 					CTLFLAG_MPSAFE, dev, i,
1049 					smu_fanrpm_sysctl, "I", "Fan RPM");
1050 
1051 			fan->fan.read = (int (*)(struct pmac_fan *))smu_fan_read_rpm;
1052 			fan->fan.set = (int (*)(struct pmac_fan *, int))smu_fan_set_rpm;
1053 
1054 		} else {
1055 			oid = SYSCTL_ADD_NODE(ctx,
1056 					      SYSCTL_CHILDREN(fanroot_oid),
1057 					      OID_AUTO, sysctl_name,
1058 					      CTLFLAG_RD, 0, "Fan Information");
1059 			SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
1060 				       "minpwm", CTLFLAG_RD,
1061 				       &fan->fan.min_rpm, 0,
1062 				       "Minimum allowed PWM in %");
1063 			SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
1064 				       "maxpwm", CTLFLAG_RD,
1065 				       &fan->fan.max_rpm, 0,
1066 				       "Maximum allowed PWM in %");
1067 			SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
1068 					"pwm",CTLTYPE_INT | CTLFLAG_RW |
1069 					CTLFLAG_MPSAFE, dev,
1070 					SMU_PWM_SYSCTL_PWM | i,
1071 					smu_fanrpm_sysctl, "I", "Fan PWM in %");
1072 			SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
1073 					"rpm",CTLTYPE_INT | CTLFLAG_RD |
1074 					CTLFLAG_MPSAFE, dev,
1075 					SMU_PWM_SYSCTL_RPM | i,
1076 					smu_fanrpm_sysctl, "I", "Fan RPM");
1077 			fan->fan.read = NULL;
1078 			fan->fan.set = (int (*)(struct pmac_fan *, int))smu_fan_set_pwm;
1079 
1080 		}
1081 		if (bootverbose)
1082 			device_printf(dev, "Fan: %s type: %d\n",
1083 				      fan->fan.name, fan->type);
1084 	}
1085 }
1086 
1087 static int
1088 smu_sensor_read(struct smu_sensor *sens)
1089 {
1090 	device_t smu = sens->dev;
1091 	struct smu_cmd cmd;
1092 	struct smu_softc *sc;
1093 	int64_t value;
1094 	int error;
1095 
1096 	cmd.cmd = SMU_ADC;
1097 	cmd.len = 1;
1098 	cmd.data[0] = sens->reg;
1099 	error = 0;
1100 
1101 	error = smu_run_cmd(smu, &cmd, 1);
1102 	if (error != 0)
1103 		return (-1);
1104 
1105 	sc = device_get_softc(smu);
1106 	value = (cmd.data[0] << 8) | cmd.data[1];
1107 
1108 	switch (sens->type) {
1109 	case SMU_TEMP_SENSOR:
1110 		value *= sc->sc_cpu_diode_scale;
1111 		value >>= 3;
1112 		value += ((int64_t)sc->sc_cpu_diode_offset) << 9;
1113 		value <<= 1;
1114 
1115 		/* Convert from 16.16 fixed point degC into integer 0.1 K. */
1116 		value = 10*(value >> 16) + ((10*(value & 0xffff)) >> 16) + 2731;
1117 		break;
1118 	case SMU_VOLTAGE_SENSOR:
1119 		value *= sc->sc_cpu_volt_scale;
1120 		value += sc->sc_cpu_volt_offset;
1121 		value <<= 4;
1122 
1123 		/* Convert from 16.16 fixed point V into mV. */
1124 		value *= 15625;
1125 		value /= 1024;
1126 		value /= 1000;
1127 		break;
1128 	case SMU_CURRENT_SENSOR:
1129 		value *= sc->sc_cpu_curr_scale;
1130 		value += sc->sc_cpu_curr_offset;
1131 		value <<= 4;
1132 
1133 		/* Convert from 16.16 fixed point A into mA. */
1134 		value *= 15625;
1135 		value /= 1024;
1136 		value /= 1000;
1137 		break;
1138 	case SMU_POWER_SENSOR:
1139 		value *= sc->sc_slots_pow_scale;
1140 		value += sc->sc_slots_pow_offset;
1141 		value <<= 4;
1142 
1143 		/* Convert from 16.16 fixed point W into mW. */
1144 		value *= 15625;
1145 		value /= 1024;
1146 		value /= 1000;
1147 		break;
1148 	}
1149 
1150 	return (value);
1151 }
1152 
1153 static int
1154 smu_sensor_sysctl(SYSCTL_HANDLER_ARGS)
1155 {
1156 	device_t smu;
1157 	struct smu_softc *sc;
1158 	struct smu_sensor *sens;
1159 	int value, error;
1160 
1161 	smu = arg1;
1162 	sc = device_get_softc(smu);
1163 	sens = &sc->sc_sensors[arg2];
1164 
1165 	value = smu_sensor_read(sens);
1166 	if (value < 0)
1167 		return (EBUSY);
1168 
1169 	error = sysctl_handle_int(oidp, &value, 0, req);
1170 
1171 	return (error);
1172 }
1173 
1174 static void
1175 smu_attach_sensors(device_t dev, phandle_t sensroot)
1176 {
1177 	struct smu_sensor *sens;
1178 	struct smu_softc *sc;
1179 	struct sysctl_oid *sensroot_oid;
1180 	struct sysctl_ctx_list *ctx;
1181 	phandle_t child;
1182 	char type[32];
1183 	int i;
1184 
1185 	sc = device_get_softc(dev);
1186 	sc->sc_nsensors = 0;
1187 
1188 	for (child = OF_child(sensroot); child != 0; child = OF_peer(child))
1189 		sc->sc_nsensors++;
1190 
1191 	if (sc->sc_nsensors == 0) {
1192 		device_printf(dev, "WARNING: No sensors detected!\n");
1193 		return;
1194 	}
1195 
1196 	sc->sc_sensors = malloc(sc->sc_nsensors * sizeof(struct smu_sensor),
1197 	    M_SMU, M_WAITOK | M_ZERO);
1198 
1199 	sens = sc->sc_sensors;
1200 	sc->sc_nsensors = 0;
1201 
1202 	ctx = device_get_sysctl_ctx(dev);
1203 	sensroot_oid = SYSCTL_ADD_NODE(ctx,
1204 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "sensors",
1205 	    CTLFLAG_RD, 0, "SMU Sensor Information");
1206 
1207 	for (child = OF_child(sensroot); child != 0; child = OF_peer(child)) {
1208 		char sysctl_name[40], sysctl_desc[40];
1209 		const char *units;
1210 
1211 		sens->dev = dev;
1212 		OF_getprop(child, "device_type", type, sizeof(type));
1213 
1214 		if (strcmp(type, "current-sensor") == 0) {
1215 			sens->type = SMU_CURRENT_SENSOR;
1216 			units = "mA";
1217 		} else if (strcmp(type, "temp-sensor") == 0) {
1218 			sens->type = SMU_TEMP_SENSOR;
1219 			units = "C";
1220 		} else if (strcmp(type, "voltage-sensor") == 0) {
1221 			sens->type = SMU_VOLTAGE_SENSOR;
1222 			units = "mV";
1223 		} else if (strcmp(type, "power-sensor") == 0) {
1224 			sens->type = SMU_POWER_SENSOR;
1225 			units = "mW";
1226 		} else {
1227 			continue;
1228 		}
1229 
1230 		OF_getprop(child, "reg", &sens->reg, sizeof(cell_t));
1231 		OF_getprop(child, "zone", &sens->therm.zone, sizeof(int));
1232 		OF_getprop(child, "location", sens->therm.name,
1233 		    sizeof(sens->therm.name));
1234 
1235 		for (i = 0; i < strlen(sens->therm.name); i++) {
1236 			sysctl_name[i] = tolower(sens->therm.name[i]);
1237 			if (isspace(sysctl_name[i]))
1238 				sysctl_name[i] = '_';
1239 		}
1240 		sysctl_name[i] = 0;
1241 
1242 		sprintf(sysctl_desc,"%s (%s)", sens->therm.name, units);
1243 
1244 		SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(sensroot_oid), OID_AUTO,
1245 		    sysctl_name, CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
1246 		    dev, sc->sc_nsensors, smu_sensor_sysctl,
1247 		    (sens->type == SMU_TEMP_SENSOR) ? "IK" : "I", sysctl_desc);
1248 
1249 		if (sens->type == SMU_TEMP_SENSOR) {
1250 			/* Make up some numbers */
1251 			sens->therm.target_temp = 500 + 2731; /* 50 C */
1252 			sens->therm.max_temp = 900 + 2731; /* 90 C */
1253 
1254 			sens->therm.read =
1255 			    (int (*)(struct pmac_therm *))smu_sensor_read;
1256 			pmac_thermal_sensor_register(&sens->therm);
1257 		}
1258 
1259 		sens++;
1260 		sc->sc_nsensors++;
1261 	}
1262 }
1263 
1264 static void
1265 smu_set_sleepled(void *xdev, int onoff)
1266 {
1267 	static struct smu_cmd cmd;
1268 	device_t smu = xdev;
1269 
1270 	cmd.cmd = SMU_MISC;
1271 	cmd.len = 3;
1272 	cmd.data[0] = SMU_MISC_LED_CTRL;
1273 	cmd.data[1] = 0;
1274 	cmd.data[2] = onoff;
1275 
1276 	smu_run_cmd(smu, &cmd, 0);
1277 }
1278 
1279 static int
1280 smu_server_mode(SYSCTL_HANDLER_ARGS)
1281 {
1282 	struct smu_cmd cmd;
1283 	u_int server_mode;
1284 	device_t smu = arg1;
1285 	int error;
1286 
1287 	cmd.cmd = SMU_POWER_EVENTS;
1288 	cmd.len = 1;
1289 	cmd.data[0] = SMU_PWR_GET_POWERUP;
1290 
1291 	error = smu_run_cmd(smu, &cmd, 1);
1292 
1293 	if (error)
1294 		return (error);
1295 
1296 	server_mode = (cmd.data[1] & SMU_WAKEUP_AC_INSERT) ? 1 : 0;
1297 
1298 	error = sysctl_handle_int(oidp, &server_mode, 0, req);
1299 
1300 	if (error || !req->newptr)
1301 		return (error);
1302 
1303 	if (server_mode == 1)
1304 		cmd.data[0] = SMU_PWR_SET_POWERUP;
1305 	else if (server_mode == 0)
1306 		cmd.data[0] = SMU_PWR_CLR_POWERUP;
1307 	else
1308 		return (EINVAL);
1309 
1310 	cmd.len = 3;
1311 	cmd.data[1] = 0;
1312 	cmd.data[2] = SMU_WAKEUP_AC_INSERT;
1313 
1314 	return (smu_run_cmd(smu, &cmd, 1));
1315 }
1316 
1317 static void
1318 smu_shutdown(void *xdev, int howto)
1319 {
1320 	device_t smu = xdev;
1321 	struct smu_cmd cmd;
1322 
1323 	cmd.cmd = SMU_POWER;
1324 	if (howto & RB_HALT)
1325 		strcpy(cmd.data, "SHUTDOWN");
1326 	else
1327 		strcpy(cmd.data, "RESTART");
1328 
1329 	cmd.len = strlen(cmd.data);
1330 
1331 	smu_run_cmd(smu, &cmd, 1);
1332 
1333 	for (;;);
1334 }
1335 
1336 static int
1337 smu_gettime(device_t dev, struct timespec *ts)
1338 {
1339 	struct smu_cmd cmd;
1340 	struct clocktime ct;
1341 
1342 	cmd.cmd = SMU_RTC;
1343 	cmd.len = 1;
1344 	cmd.data[0] = SMU_RTC_GET;
1345 
1346 	if (smu_run_cmd(dev, &cmd, 1) != 0)
1347 		return (ENXIO);
1348 
1349 	ct.nsec	= 0;
1350 	ct.sec	= bcd2bin(cmd.data[0]);
1351 	ct.min	= bcd2bin(cmd.data[1]);
1352 	ct.hour	= bcd2bin(cmd.data[2]);
1353 	ct.dow	= bcd2bin(cmd.data[3]);
1354 	ct.day	= bcd2bin(cmd.data[4]);
1355 	ct.mon	= bcd2bin(cmd.data[5]);
1356 	ct.year	= bcd2bin(cmd.data[6]) + 2000;
1357 
1358 	return (clock_ct_to_ts(&ct, ts));
1359 }
1360 
1361 static int
1362 smu_settime(device_t dev, struct timespec *ts)
1363 {
1364 	static struct smu_cmd cmd;
1365 	struct clocktime ct;
1366 
1367 	cmd.cmd = SMU_RTC;
1368 	cmd.len = 8;
1369 	cmd.data[0] = SMU_RTC_SET;
1370 
1371 	clock_ts_to_ct(ts, &ct);
1372 
1373 	cmd.data[1] = bin2bcd(ct.sec);
1374 	cmd.data[2] = bin2bcd(ct.min);
1375 	cmd.data[3] = bin2bcd(ct.hour);
1376 	cmd.data[4] = bin2bcd(ct.dow);
1377 	cmd.data[5] = bin2bcd(ct.day);
1378 	cmd.data[6] = bin2bcd(ct.mon);
1379 	cmd.data[7] = bin2bcd(ct.year - 2000);
1380 
1381 	return (smu_run_cmd(dev, &cmd, 0));
1382 }
1383 
1384 /* SMU I2C Interface */
1385 
1386 static int smuiic_probe(device_t dev);
1387 static int smuiic_attach(device_t dev);
1388 static int smuiic_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs);
1389 static phandle_t smuiic_get_node(device_t bus, device_t dev);
1390 
1391 static device_method_t smuiic_methods[] = {
1392 	/* device interface */
1393 	DEVMETHOD(device_probe,         smuiic_probe),
1394 	DEVMETHOD(device_attach,        smuiic_attach),
1395 
1396 	/* iicbus interface */
1397 	DEVMETHOD(iicbus_callback,      iicbus_null_callback),
1398 	DEVMETHOD(iicbus_transfer,      smuiic_transfer),
1399 
1400 	/* ofw_bus interface */
1401 	DEVMETHOD(ofw_bus_get_node,     smuiic_get_node),
1402 
1403 	{ 0, 0 }
1404 };
1405 
1406 struct smuiic_softc {
1407 	struct mtx	sc_mtx;
1408 	volatile int	sc_iic_inuse;
1409 	int		sc_busno;
1410 };
1411 
1412 static driver_t smuiic_driver = {
1413 	"iichb",
1414 	smuiic_methods,
1415 	sizeof(struct smuiic_softc)
1416 };
1417 static devclass_t smuiic_devclass;
1418 
1419 DRIVER_MODULE(smuiic, smu, smuiic_driver, smuiic_devclass, 0, 0);
1420 
1421 static void
1422 smu_attach_i2c(device_t smu, phandle_t i2croot)
1423 {
1424 	phandle_t child;
1425 	device_t cdev;
1426 	struct ofw_bus_devinfo *dinfo;
1427 	char name[32];
1428 
1429 	for (child = OF_child(i2croot); child != 0; child = OF_peer(child)) {
1430 		if (OF_getprop(child, "name", name, sizeof(name)) <= 0)
1431 			continue;
1432 
1433 		if (strcmp(name, "i2c-bus") != 0 && strcmp(name, "i2c") != 0)
1434 			continue;
1435 
1436 		dinfo = malloc(sizeof(struct ofw_bus_devinfo), M_SMU,
1437 		    M_WAITOK | M_ZERO);
1438 		if (ofw_bus_gen_setup_devinfo(dinfo, child) != 0) {
1439 			free(dinfo, M_SMU);
1440 			continue;
1441 		}
1442 
1443 		cdev = device_add_child(smu, NULL, -1);
1444 		if (cdev == NULL) {
1445 			device_printf(smu, "<%s>: device_add_child failed\n",
1446 			    dinfo->obd_name);
1447 			ofw_bus_gen_destroy_devinfo(dinfo);
1448 			free(dinfo, M_SMU);
1449 			continue;
1450 		}
1451 		device_set_ivars(cdev, dinfo);
1452 	}
1453 }
1454 
1455 static int
1456 smuiic_probe(device_t dev)
1457 {
1458 	const char *name;
1459 
1460 	name = ofw_bus_get_name(dev);
1461 	if (name == NULL)
1462 		return (ENXIO);
1463 
1464 	if (strcmp(name, "i2c-bus") == 0 || strcmp(name, "i2c") == 0) {
1465 		device_set_desc(dev, "SMU I2C controller");
1466 		return (0);
1467 	}
1468 
1469 	return (ENXIO);
1470 }
1471 
1472 static int
1473 smuiic_attach(device_t dev)
1474 {
1475 	struct smuiic_softc *sc = device_get_softc(dev);
1476 	mtx_init(&sc->sc_mtx, "smuiic", NULL, MTX_DEF);
1477 	sc->sc_iic_inuse = 0;
1478 
1479 	/* Get our bus number */
1480 	OF_getprop(ofw_bus_get_node(dev), "reg", &sc->sc_busno,
1481 	    sizeof(sc->sc_busno));
1482 
1483 	/* Add the IIC bus layer */
1484 	device_add_child(dev, "iicbus", -1);
1485 
1486 	return (bus_generic_attach(dev));
1487 }
1488 
1489 static int
1490 smuiic_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
1491 {
1492 	struct smuiic_softc *sc = device_get_softc(dev);
1493 	struct smu_cmd cmd;
1494 	int i, j, error;
1495 
1496 	mtx_lock(&sc->sc_mtx);
1497 	while (sc->sc_iic_inuse)
1498 		mtx_sleep(sc, &sc->sc_mtx, 0, "smuiic", 100);
1499 
1500 	sc->sc_iic_inuse = 1;
1501 	error = 0;
1502 
1503 	for (i = 0; i < nmsgs; i++) {
1504 		cmd.cmd = SMU_I2C;
1505 		cmd.data[0] = sc->sc_busno;
1506 		if (msgs[i].flags & IIC_M_NOSTOP)
1507 			cmd.data[1] = SMU_I2C_COMBINED;
1508 		else
1509 			cmd.data[1] = SMU_I2C_SIMPLE;
1510 
1511 		cmd.data[2] = msgs[i].slave;
1512 		if (msgs[i].flags & IIC_M_RD)
1513 			cmd.data[2] |= 1;
1514 
1515 		if (msgs[i].flags & IIC_M_NOSTOP) {
1516 			KASSERT(msgs[i].len < 4,
1517 			    ("oversize I2C combined message"));
1518 
1519 			cmd.data[3] = min(msgs[i].len, 3);
1520 			memcpy(&cmd.data[4], msgs[i].buf, min(msgs[i].len, 3));
1521 			i++; /* Advance to next part of message */
1522 		} else {
1523 			cmd.data[3] = 0;
1524 			memset(&cmd.data[4], 0, 3);
1525 		}
1526 
1527 		cmd.data[7] = msgs[i].slave;
1528 		if (msgs[i].flags & IIC_M_RD)
1529 			cmd.data[7] |= 1;
1530 
1531 		cmd.data[8] = msgs[i].len;
1532 		if (msgs[i].flags & IIC_M_RD) {
1533 			memset(&cmd.data[9], 0xff, msgs[i].len);
1534 			cmd.len = 9;
1535 		} else {
1536 			memcpy(&cmd.data[9], msgs[i].buf, msgs[i].len);
1537 			cmd.len = 9 + msgs[i].len;
1538 		}
1539 
1540 		mtx_unlock(&sc->sc_mtx);
1541 		smu_run_cmd(device_get_parent(dev), &cmd, 1);
1542 		mtx_lock(&sc->sc_mtx);
1543 
1544 		for (j = 0; j < 10; j++) {
1545 			cmd.cmd = SMU_I2C;
1546 			cmd.len = 1;
1547 			cmd.data[0] = 0;
1548 			memset(&cmd.data[1], 0xff, msgs[i].len);
1549 
1550 			mtx_unlock(&sc->sc_mtx);
1551 			smu_run_cmd(device_get_parent(dev), &cmd, 1);
1552 			mtx_lock(&sc->sc_mtx);
1553 
1554 			if (!(cmd.data[0] & 0x80))
1555 				break;
1556 
1557 			mtx_sleep(sc, &sc->sc_mtx, 0, "smuiic", 10);
1558 		}
1559 
1560 		if (cmd.data[0] & 0x80) {
1561 			error = EIO;
1562 			msgs[i].len = 0;
1563 			goto exit;
1564 		}
1565 		memcpy(msgs[i].buf, &cmd.data[1], msgs[i].len);
1566 		msgs[i].len = cmd.len - 1;
1567 	}
1568 
1569     exit:
1570 	sc->sc_iic_inuse = 0;
1571 	mtx_unlock(&sc->sc_mtx);
1572 	wakeup(sc);
1573 	return (error);
1574 }
1575 
1576 static phandle_t
1577 smuiic_get_node(device_t bus, device_t dev)
1578 {
1579 
1580 	return (ofw_bus_get_node(bus));
1581 }
1582 
1583