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