xref: /freebsd/sys/powerpc/powermac/smu.c (revision f7c32ed617858bcd22f8d1b03199099d50125721)
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,	ofw_bus_gen_child_pnpinfo),
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 		if (bootverbose)
1084 			device_printf(dev, "Fan: %s type: %d\n",
1085 				      fan->fan.name, fan->type);
1086 	}
1087 }
1088 
1089 static int
1090 smu_sensor_read(struct smu_sensor *sens)
1091 {
1092 	device_t smu = sens->dev;
1093 	struct smu_cmd cmd;
1094 	struct smu_softc *sc;
1095 	int64_t value;
1096 	int error;
1097 
1098 	cmd.cmd = SMU_ADC;
1099 	cmd.len = 1;
1100 	cmd.data[0] = sens->reg;
1101 	error = 0;
1102 
1103 	error = smu_run_cmd(smu, &cmd, 1);
1104 	if (error != 0)
1105 		return (-1);
1106 
1107 	sc = device_get_softc(smu);
1108 	value = (cmd.data[0] << 8) | cmd.data[1];
1109 
1110 	switch (sens->type) {
1111 	case SMU_TEMP_SENSOR:
1112 		value *= sc->sc_cpu_diode_scale;
1113 		value >>= 3;
1114 		value += ((int64_t)sc->sc_cpu_diode_offset) << 9;
1115 		value <<= 1;
1116 
1117 		/* Convert from 16.16 fixed point degC into integer 0.1 K. */
1118 		value = 10*(value >> 16) + ((10*(value & 0xffff)) >> 16) + 2731;
1119 		break;
1120 	case SMU_VOLTAGE_SENSOR:
1121 		value *= sc->sc_cpu_volt_scale;
1122 		value += sc->sc_cpu_volt_offset;
1123 		value <<= 4;
1124 
1125 		/* Convert from 16.16 fixed point V into mV. */
1126 		value *= 15625;
1127 		value /= 1024;
1128 		value /= 1000;
1129 		break;
1130 	case SMU_CURRENT_SENSOR:
1131 		value *= sc->sc_cpu_curr_scale;
1132 		value += sc->sc_cpu_curr_offset;
1133 		value <<= 4;
1134 
1135 		/* Convert from 16.16 fixed point A into mA. */
1136 		value *= 15625;
1137 		value /= 1024;
1138 		value /= 1000;
1139 		break;
1140 	case SMU_POWER_SENSOR:
1141 		value *= sc->sc_slots_pow_scale;
1142 		value += sc->sc_slots_pow_offset;
1143 		value <<= 4;
1144 
1145 		/* Convert from 16.16 fixed point W into mW. */
1146 		value *= 15625;
1147 		value /= 1024;
1148 		value /= 1000;
1149 		break;
1150 	}
1151 
1152 	return (value);
1153 }
1154 
1155 static int
1156 smu_sensor_sysctl(SYSCTL_HANDLER_ARGS)
1157 {
1158 	device_t smu;
1159 	struct smu_softc *sc;
1160 	struct smu_sensor *sens;
1161 	int value, error;
1162 
1163 	smu = arg1;
1164 	sc = device_get_softc(smu);
1165 	sens = &sc->sc_sensors[arg2];
1166 
1167 	value = smu_sensor_read(sens);
1168 	if (value < 0)
1169 		return (EBUSY);
1170 
1171 	error = sysctl_handle_int(oidp, &value, 0, req);
1172 
1173 	return (error);
1174 }
1175 
1176 static void
1177 smu_attach_sensors(device_t dev, phandle_t sensroot)
1178 {
1179 	struct smu_sensor *sens;
1180 	struct smu_softc *sc;
1181 	struct sysctl_oid *sensroot_oid;
1182 	struct sysctl_ctx_list *ctx;
1183 	phandle_t child;
1184 	char type[32];
1185 	int i;
1186 
1187 	sc = device_get_softc(dev);
1188 	sc->sc_nsensors = 0;
1189 
1190 	for (child = OF_child(sensroot); child != 0; child = OF_peer(child))
1191 		sc->sc_nsensors++;
1192 
1193 	if (sc->sc_nsensors == 0) {
1194 		device_printf(dev, "WARNING: No sensors detected!\n");
1195 		return;
1196 	}
1197 
1198 	sc->sc_sensors = malloc(sc->sc_nsensors * sizeof(struct smu_sensor),
1199 	    M_SMU, M_WAITOK | M_ZERO);
1200 
1201 	sens = sc->sc_sensors;
1202 	sc->sc_nsensors = 0;
1203 
1204 	ctx = device_get_sysctl_ctx(dev);
1205 	sensroot_oid = SYSCTL_ADD_NODE(ctx,
1206 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "sensors",
1207 	    CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "SMU Sensor Information");
1208 
1209 	for (child = OF_child(sensroot); child != 0; child = OF_peer(child)) {
1210 		char sysctl_name[40], sysctl_desc[40];
1211 		const char *units;
1212 
1213 		sens->dev = dev;
1214 		OF_getprop(child, "device_type", type, sizeof(type));
1215 
1216 		if (strcmp(type, "current-sensor") == 0) {
1217 			sens->type = SMU_CURRENT_SENSOR;
1218 			units = "mA";
1219 		} else if (strcmp(type, "temp-sensor") == 0) {
1220 			sens->type = SMU_TEMP_SENSOR;
1221 			units = "C";
1222 		} else if (strcmp(type, "voltage-sensor") == 0) {
1223 			sens->type = SMU_VOLTAGE_SENSOR;
1224 			units = "mV";
1225 		} else if (strcmp(type, "power-sensor") == 0) {
1226 			sens->type = SMU_POWER_SENSOR;
1227 			units = "mW";
1228 		} else {
1229 			continue;
1230 		}
1231 
1232 		OF_getprop(child, "reg", &sens->reg, sizeof(cell_t));
1233 		OF_getprop(child, "zone", &sens->therm.zone, sizeof(int));
1234 		OF_getprop(child, "location", sens->therm.name,
1235 		    sizeof(sens->therm.name));
1236 
1237 		for (i = 0; i < strlen(sens->therm.name); i++) {
1238 			sysctl_name[i] = tolower(sens->therm.name[i]);
1239 			if (isspace(sysctl_name[i]))
1240 				sysctl_name[i] = '_';
1241 		}
1242 		sysctl_name[i] = 0;
1243 
1244 		sprintf(sysctl_desc,"%s (%s)", sens->therm.name, units);
1245 
1246 		SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(sensroot_oid), OID_AUTO,
1247 		    sysctl_name, CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
1248 		    dev, sc->sc_nsensors, smu_sensor_sysctl,
1249 		    (sens->type == SMU_TEMP_SENSOR) ? "IK" : "I", sysctl_desc);
1250 
1251 		if (sens->type == SMU_TEMP_SENSOR) {
1252 			/* Make up some numbers */
1253 			sens->therm.target_temp = 500 + 2731; /* 50 C */
1254 			sens->therm.max_temp = 900 + 2731; /* 90 C */
1255 
1256 			sens->therm.read =
1257 			    (int (*)(struct pmac_therm *))smu_sensor_read;
1258 			pmac_thermal_sensor_register(&sens->therm);
1259 		}
1260 
1261 		sens++;
1262 		sc->sc_nsensors++;
1263 	}
1264 }
1265 
1266 static void
1267 smu_set_sleepled(void *xdev, int onoff)
1268 {
1269 	static struct smu_cmd cmd;
1270 	device_t smu = xdev;
1271 
1272 	cmd.cmd = SMU_MISC;
1273 	cmd.len = 3;
1274 	cmd.data[0] = SMU_MISC_LED_CTRL;
1275 	cmd.data[1] = 0;
1276 	cmd.data[2] = onoff;
1277 
1278 	smu_run_cmd(smu, &cmd, 0);
1279 }
1280 
1281 static int
1282 smu_server_mode(SYSCTL_HANDLER_ARGS)
1283 {
1284 	struct smu_cmd cmd;
1285 	u_int server_mode;
1286 	device_t smu = arg1;
1287 	int error;
1288 
1289 	cmd.cmd = SMU_POWER_EVENTS;
1290 	cmd.len = 1;
1291 	cmd.data[0] = SMU_PWR_GET_POWERUP;
1292 
1293 	error = smu_run_cmd(smu, &cmd, 1);
1294 
1295 	if (error)
1296 		return (error);
1297 
1298 	server_mode = (cmd.data[1] & SMU_WAKEUP_AC_INSERT) ? 1 : 0;
1299 
1300 	error = sysctl_handle_int(oidp, &server_mode, 0, req);
1301 
1302 	if (error || !req->newptr)
1303 		return (error);
1304 
1305 	if (server_mode == 1)
1306 		cmd.data[0] = SMU_PWR_SET_POWERUP;
1307 	else if (server_mode == 0)
1308 		cmd.data[0] = SMU_PWR_CLR_POWERUP;
1309 	else
1310 		return (EINVAL);
1311 
1312 	cmd.len = 3;
1313 	cmd.data[1] = 0;
1314 	cmd.data[2] = SMU_WAKEUP_AC_INSERT;
1315 
1316 	return (smu_run_cmd(smu, &cmd, 1));
1317 }
1318 
1319 static void
1320 smu_shutdown(void *xdev, int howto)
1321 {
1322 	device_t smu = xdev;
1323 	struct smu_cmd cmd;
1324 
1325 	cmd.cmd = SMU_POWER;
1326 	if (howto & RB_HALT)
1327 		strcpy(cmd.data, "SHUTDOWN");
1328 	else
1329 		strcpy(cmd.data, "RESTART");
1330 
1331 	cmd.len = strlen(cmd.data);
1332 
1333 	smu_run_cmd(smu, &cmd, 1);
1334 
1335 	for (;;);
1336 }
1337 
1338 static int
1339 smu_gettime(device_t dev, struct timespec *ts)
1340 {
1341 	struct smu_cmd cmd;
1342 	struct clocktime ct;
1343 
1344 	cmd.cmd = SMU_RTC;
1345 	cmd.len = 1;
1346 	cmd.data[0] = SMU_RTC_GET;
1347 
1348 	if (smu_run_cmd(dev, &cmd, 1) != 0)
1349 		return (ENXIO);
1350 
1351 	ct.nsec	= 0;
1352 	ct.sec	= bcd2bin(cmd.data[0]);
1353 	ct.min	= bcd2bin(cmd.data[1]);
1354 	ct.hour	= bcd2bin(cmd.data[2]);
1355 	ct.dow	= bcd2bin(cmd.data[3]);
1356 	ct.day	= bcd2bin(cmd.data[4]);
1357 	ct.mon	= bcd2bin(cmd.data[5]);
1358 	ct.year	= bcd2bin(cmd.data[6]) + 2000;
1359 
1360 	return (clock_ct_to_ts(&ct, ts));
1361 }
1362 
1363 static int
1364 smu_settime(device_t dev, struct timespec *ts)
1365 {
1366 	static struct smu_cmd cmd;
1367 	struct clocktime ct;
1368 
1369 	cmd.cmd = SMU_RTC;
1370 	cmd.len = 8;
1371 	cmd.data[0] = SMU_RTC_SET;
1372 
1373 	clock_ts_to_ct(ts, &ct);
1374 
1375 	cmd.data[1] = bin2bcd(ct.sec);
1376 	cmd.data[2] = bin2bcd(ct.min);
1377 	cmd.data[3] = bin2bcd(ct.hour);
1378 	cmd.data[4] = bin2bcd(ct.dow);
1379 	cmd.data[5] = bin2bcd(ct.day);
1380 	cmd.data[6] = bin2bcd(ct.mon);
1381 	cmd.data[7] = bin2bcd(ct.year - 2000);
1382 
1383 	return (smu_run_cmd(dev, &cmd, 0));
1384 }
1385 
1386 /* SMU I2C Interface */
1387 
1388 static int smuiic_probe(device_t dev);
1389 static int smuiic_attach(device_t dev);
1390 static int smuiic_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs);
1391 static phandle_t smuiic_get_node(device_t bus, device_t dev);
1392 
1393 static device_method_t smuiic_methods[] = {
1394 	/* device interface */
1395 	DEVMETHOD(device_probe,         smuiic_probe),
1396 	DEVMETHOD(device_attach,        smuiic_attach),
1397 
1398 	/* iicbus interface */
1399 	DEVMETHOD(iicbus_callback,      iicbus_null_callback),
1400 	DEVMETHOD(iicbus_transfer,      smuiic_transfer),
1401 
1402 	/* ofw_bus interface */
1403 	DEVMETHOD(ofw_bus_get_node,     smuiic_get_node),
1404 	{ 0, 0 }
1405 };
1406 
1407 struct smuiic_softc {
1408 	struct mtx	sc_mtx;
1409 	volatile int	sc_iic_inuse;
1410 	int		sc_busno;
1411 };
1412 
1413 static driver_t smuiic_driver = {
1414 	"iichb",
1415 	smuiic_methods,
1416 	sizeof(struct smuiic_softc)
1417 };
1418 static devclass_t smuiic_devclass;
1419 
1420 DRIVER_MODULE(smuiic, smu, smuiic_driver, smuiic_devclass, 0, 0);
1421 
1422 static void
1423 smu_attach_i2c(device_t smu, phandle_t i2croot)
1424 {
1425 	phandle_t child;
1426 	device_t cdev;
1427 	struct ofw_bus_devinfo *dinfo;
1428 	char name[32];
1429 
1430 	for (child = OF_child(i2croot); child != 0; child = OF_peer(child)) {
1431 		if (OF_getprop(child, "name", name, sizeof(name)) <= 0)
1432 			continue;
1433 
1434 		if (strcmp(name, "i2c-bus") != 0 && strcmp(name, "i2c") != 0)
1435 			continue;
1436 
1437 		dinfo = malloc(sizeof(struct ofw_bus_devinfo), M_SMU,
1438 		    M_WAITOK | M_ZERO);
1439 		if (ofw_bus_gen_setup_devinfo(dinfo, child) != 0) {
1440 			free(dinfo, M_SMU);
1441 			continue;
1442 		}
1443 
1444 		cdev = device_add_child(smu, NULL, -1);
1445 		if (cdev == NULL) {
1446 			device_printf(smu, "<%s>: device_add_child failed\n",
1447 			    dinfo->obd_name);
1448 			ofw_bus_gen_destroy_devinfo(dinfo);
1449 			free(dinfo, M_SMU);
1450 			continue;
1451 		}
1452 		device_set_ivars(cdev, dinfo);
1453 	}
1454 }
1455 
1456 static int
1457 smuiic_probe(device_t dev)
1458 {
1459 	const char *name;
1460 
1461 	name = ofw_bus_get_name(dev);
1462 	if (name == NULL)
1463 		return (ENXIO);
1464 
1465 	if (strcmp(name, "i2c-bus") == 0 || strcmp(name, "i2c") == 0) {
1466 		device_set_desc(dev, "SMU I2C controller");
1467 		return (0);
1468 	}
1469 
1470 	return (ENXIO);
1471 }
1472 
1473 static int
1474 smuiic_attach(device_t dev)
1475 {
1476 	struct smuiic_softc *sc = device_get_softc(dev);
1477 	mtx_init(&sc->sc_mtx, "smuiic", NULL, MTX_DEF);
1478 	sc->sc_iic_inuse = 0;
1479 
1480 	/* Get our bus number */
1481 	OF_getprop(ofw_bus_get_node(dev), "reg", &sc->sc_busno,
1482 	    sizeof(sc->sc_busno));
1483 
1484 	/* Add the IIC bus layer */
1485 	device_add_child(dev, "iicbus", -1);
1486 
1487 	return (bus_generic_attach(dev));
1488 }
1489 
1490 static int
1491 smuiic_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
1492 {
1493 	struct smuiic_softc *sc = device_get_softc(dev);
1494 	struct smu_cmd cmd;
1495 	int i, j, error;
1496 
1497 	mtx_lock(&sc->sc_mtx);
1498 	while (sc->sc_iic_inuse)
1499 		mtx_sleep(sc, &sc->sc_mtx, 0, "smuiic", 100);
1500 
1501 	sc->sc_iic_inuse = 1;
1502 	error = 0;
1503 
1504 	for (i = 0; i < nmsgs; i++) {
1505 		cmd.cmd = SMU_I2C;
1506 		cmd.data[0] = sc->sc_busno;
1507 		if (msgs[i].flags & IIC_M_NOSTOP)
1508 			cmd.data[1] = SMU_I2C_COMBINED;
1509 		else
1510 			cmd.data[1] = SMU_I2C_SIMPLE;
1511 
1512 		cmd.data[2] = msgs[i].slave;
1513 		if (msgs[i].flags & IIC_M_RD)
1514 			cmd.data[2] |= 1;
1515 
1516 		if (msgs[i].flags & IIC_M_NOSTOP) {
1517 			KASSERT(msgs[i].len < 4,
1518 			    ("oversize I2C combined message"));
1519 
1520 			cmd.data[3] = min(msgs[i].len, 3);
1521 			memcpy(&cmd.data[4], msgs[i].buf, min(msgs[i].len, 3));
1522 			i++; /* Advance to next part of message */
1523 		} else {
1524 			cmd.data[3] = 0;
1525 			memset(&cmd.data[4], 0, 3);
1526 		}
1527 
1528 		cmd.data[7] = msgs[i].slave;
1529 		if (msgs[i].flags & IIC_M_RD)
1530 			cmd.data[7] |= 1;
1531 
1532 		cmd.data[8] = msgs[i].len;
1533 		if (msgs[i].flags & IIC_M_RD) {
1534 			memset(&cmd.data[9], 0xff, msgs[i].len);
1535 			cmd.len = 9;
1536 		} else {
1537 			memcpy(&cmd.data[9], msgs[i].buf, msgs[i].len);
1538 			cmd.len = 9 + msgs[i].len;
1539 		}
1540 
1541 		mtx_unlock(&sc->sc_mtx);
1542 		smu_run_cmd(device_get_parent(dev), &cmd, 1);
1543 		mtx_lock(&sc->sc_mtx);
1544 
1545 		for (j = 0; j < 10; j++) {
1546 			cmd.cmd = SMU_I2C;
1547 			cmd.len = 1;
1548 			cmd.data[0] = 0;
1549 			memset(&cmd.data[1], 0xff, msgs[i].len);
1550 
1551 			mtx_unlock(&sc->sc_mtx);
1552 			smu_run_cmd(device_get_parent(dev), &cmd, 1);
1553 			mtx_lock(&sc->sc_mtx);
1554 
1555 			if (!(cmd.data[0] & 0x80))
1556 				break;
1557 
1558 			mtx_sleep(sc, &sc->sc_mtx, 0, "smuiic", 10);
1559 		}
1560 
1561 		if (cmd.data[0] & 0x80) {
1562 			error = EIO;
1563 			msgs[i].len = 0;
1564 			goto exit;
1565 		}
1566 		memcpy(msgs[i].buf, &cmd.data[1], msgs[i].len);
1567 		msgs[i].len = cmd.len - 1;
1568 	}
1569 
1570     exit:
1571 	sc->sc_iic_inuse = 0;
1572 	mtx_unlock(&sc->sc_mtx);
1573 	wakeup(sc);
1574 	return (error);
1575 }
1576 
1577 static phandle_t
1578 smuiic_get_node(device_t bus, device_t dev)
1579 {
1580 
1581 	return (ofw_bus_get_node(bus));
1582 }
1583