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