xref: /freebsd/sys/powerpc/powermac/smu.c (revision 884a2a699669ec61e2366e3e358342dbc94be24a)
1 /*-
2  * Copyright (c) 2009 Nathan Whitehorn
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <sys/bus.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/reboot.h>
42 #include <sys/rman.h>
43 #include <sys/sysctl.h>
44 #include <sys/unistd.h>
45 
46 #include <machine/bus.h>
47 #include <machine/intr_machdep.h>
48 #include <machine/md_var.h>
49 
50 #include <dev/iicbus/iicbus.h>
51 #include <dev/iicbus/iiconf.h>
52 #include <dev/led/led.h>
53 #include <dev/ofw/openfirm.h>
54 #include <dev/ofw/ofw_bus.h>
55 #include <dev/ofw/ofw_bus_subr.h>
56 #include <powerpc/powermac/macgpiovar.h>
57 #include <powerpc/powermac/powermac_thermal.h>
58 
59 #include "clock_if.h"
60 #include "iicbus_if.h"
61 
62 struct smu_cmd {
63 	volatile uint8_t cmd;
64 	uint8_t		len;
65 	uint8_t		data[254];
66 
67 	STAILQ_ENTRY(smu_cmd) cmd_q;
68 };
69 
70 STAILQ_HEAD(smu_cmdq, smu_cmd);
71 
72 struct smu_fan {
73 	struct pmac_fan fan;
74 	device_t dev;
75 	cell_t	reg;
76 
77 	int	old_style;
78 	int	setpoint;
79 };
80 
81 struct smu_sensor {
82 	struct pmac_therm therm;
83 	device_t dev;
84 
85 	cell_t	reg;
86 	enum {
87 		SMU_CURRENT_SENSOR,
88 		SMU_VOLTAGE_SENSOR,
89 		SMU_POWER_SENSOR,
90 		SMU_TEMP_SENSOR
91 	} type;
92 };
93 
94 struct smu_softc {
95 	device_t	sc_dev;
96 	struct mtx	sc_mtx;
97 
98 	struct resource	*sc_memr;
99 	int		sc_memrid;
100 	int		sc_u3;
101 
102 	bus_dma_tag_t	sc_dmatag;
103 	bus_space_tag_t	sc_bt;
104 	bus_space_handle_t sc_mailbox;
105 
106 	struct smu_cmd	*sc_cmd, *sc_cur_cmd;
107 	bus_addr_t	sc_cmd_phys;
108 	bus_dmamap_t	sc_cmd_dmamap;
109 	struct smu_cmdq	sc_cmdq;
110 
111 	struct smu_fan	*sc_fans;
112 	int		sc_nfans;
113 	struct smu_sensor *sc_sensors;
114 	int		sc_nsensors;
115 
116 	int		sc_doorbellirqid;
117 	struct resource	*sc_doorbellirq;
118 	void		*sc_doorbellirqcookie;
119 
120 	struct proc	*sc_fanmgt_proc;
121 	time_t		sc_lastuserchange;
122 
123 	/* Calibration data */
124 	uint16_t	sc_cpu_diode_scale;
125 	int16_t		sc_cpu_diode_offset;
126 
127 	uint16_t	sc_cpu_volt_scale;
128 	int16_t		sc_cpu_volt_offset;
129 	uint16_t	sc_cpu_curr_scale;
130 	int16_t		sc_cpu_curr_offset;
131 
132 	uint16_t	sc_slots_pow_scale;
133 	int16_t		sc_slots_pow_offset;
134 
135 	struct cdev 	*sc_leddev;
136 };
137 
138 /* regular bus attachment functions */
139 
140 static int	smu_probe(device_t);
141 static int	smu_attach(device_t);
142 static const struct ofw_bus_devinfo *
143     smu_get_devinfo(device_t bus, device_t dev);
144 
145 /* cpufreq notification hooks */
146 
147 static void	smu_cpufreq_pre_change(device_t, const struct cf_level *level);
148 static void	smu_cpufreq_post_change(device_t, const struct cf_level *level);
149 
150 /* clock interface */
151 static int	smu_gettime(device_t dev, struct timespec *ts);
152 static int	smu_settime(device_t dev, struct timespec *ts);
153 
154 /* utility functions */
155 static int	smu_run_cmd(device_t dev, struct smu_cmd *cmd, int wait);
156 static int	smu_get_datablock(device_t dev, int8_t id, uint8_t *buf,
157 		    size_t len);
158 static void	smu_attach_i2c(device_t dev, phandle_t i2croot);
159 static void	smu_attach_fans(device_t dev, phandle_t fanroot);
160 static void	smu_attach_sensors(device_t dev, phandle_t sensroot);
161 static void	smu_set_sleepled(void *xdev, int onoff);
162 static int	smu_server_mode(SYSCTL_HANDLER_ARGS);
163 static void	smu_doorbell_intr(void *xdev);
164 static void	smu_shutdown(void *xdev, int howto);
165 
166 /* where to find the doorbell GPIO */
167 
168 static device_t	smu_doorbell = NULL;
169 
170 static device_method_t  smu_methods[] = {
171 	/* Device interface */
172 	DEVMETHOD(device_probe,		smu_probe),
173 	DEVMETHOD(device_attach,	smu_attach),
174 
175 	/* Clock interface */
176 	DEVMETHOD(clock_gettime,	smu_gettime),
177 	DEVMETHOD(clock_settime,	smu_settime),
178 
179 	/* ofw_bus interface */
180 	DEVMETHOD(bus_child_pnpinfo_str,ofw_bus_gen_child_pnpinfo_str),
181 	DEVMETHOD(ofw_bus_get_devinfo,	smu_get_devinfo),
182 	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
183 	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
184 	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
185 	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
186 	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
187 
188 	{ 0, 0 },
189 };
190 
191 static driver_t smu_driver = {
192 	"smu",
193 	smu_methods,
194 	sizeof(struct smu_softc)
195 };
196 
197 static devclass_t smu_devclass;
198 
199 DRIVER_MODULE(smu, nexus, smu_driver, smu_devclass, 0, 0);
200 MALLOC_DEFINE(M_SMU, "smu", "SMU Sensor Information");
201 
202 #define SMU_MAILBOX		0x8000860c
203 #define SMU_FANMGT_INTERVAL	1000 /* ms */
204 
205 /* Command types */
206 #define SMU_ADC			0xd8
207 #define SMU_FAN			0x4a
208 #define SMU_I2C			0x9a
209 #define  SMU_I2C_SIMPLE		0x00
210 #define  SMU_I2C_NORMAL		0x01
211 #define  SMU_I2C_COMBINED	0x02
212 #define SMU_MISC		0xee
213 #define  SMU_MISC_GET_DATA	0x02
214 #define  SMU_MISC_LED_CTRL	0x04
215 #define SMU_POWER		0xaa
216 #define SMU_POWER_EVENTS	0x8f
217 #define  SMU_PWR_GET_POWERUP	0x00
218 #define  SMU_PWR_SET_POWERUP	0x01
219 #define  SMU_PWR_CLR_POWERUP	0x02
220 #define SMU_RTC			0x8e
221 #define  SMU_RTC_GET		0x81
222 #define  SMU_RTC_SET		0x80
223 
224 /* Power event types */
225 #define SMU_WAKEUP_KEYPRESS	0x01
226 #define SMU_WAKEUP_AC_INSERT	0x02
227 #define SMU_WAKEUP_AC_CHANGE	0x04
228 #define SMU_WAKEUP_RING		0x10
229 
230 /* Data blocks */
231 #define SMU_CPUTEMP_CAL		0x18
232 #define SMU_CPUVOLT_CAL		0x21
233 #define SMU_SLOTPW_CAL		0x78
234 
235 /* Partitions */
236 #define SMU_PARTITION		0x3e
237 #define SMU_PARTITION_LATEST	0x01
238 #define SMU_PARTITION_BASE	0x02
239 #define SMU_PARTITION_UPDATE	0x03
240 
241 static int
242 smu_probe(device_t dev)
243 {
244 	const char *name = ofw_bus_get_name(dev);
245 
246 	if (strcmp(name, "smu") != 0)
247 		return (ENXIO);
248 
249 	device_set_desc(dev, "Apple System Management Unit");
250 	return (0);
251 }
252 
253 static void
254 smu_phys_callback(void *xsc, bus_dma_segment_t *segs, int nsegs, int error)
255 {
256 	struct smu_softc *sc = xsc;
257 
258 	sc->sc_cmd_phys = segs[0].ds_addr;
259 }
260 
261 static int
262 smu_attach(device_t dev)
263 {
264 	struct smu_softc *sc;
265 	phandle_t	node, child;
266 	uint8_t		data[12];
267 
268 	sc = device_get_softc(dev);
269 
270 	mtx_init(&sc->sc_mtx, "smu", NULL, MTX_DEF);
271 	sc->sc_cur_cmd = NULL;
272 	sc->sc_doorbellirqid = -1;
273 
274 	sc->sc_u3 = 0;
275 	if (OF_finddevice("/u3") != -1)
276 		sc->sc_u3 = 1;
277 
278 	/*
279 	 * Map the mailbox area. This should be determined from firmware,
280 	 * but I have not found a simple way to do that.
281 	 */
282 	bus_dma_tag_create(NULL, 16, 0, BUS_SPACE_MAXADDR_32BIT,
283 	    BUS_SPACE_MAXADDR, NULL, NULL, PAGE_SIZE, 1, PAGE_SIZE, 0, NULL,
284 	    NULL, &(sc->sc_dmatag));
285 	sc->sc_bt = &bs_le_tag;
286 	bus_space_map(sc->sc_bt, SMU_MAILBOX, 4, 0, &sc->sc_mailbox);
287 
288 	/*
289 	 * Allocate the command buffer. This can be anywhere in the low 4 GB
290 	 * of memory.
291 	 */
292 	bus_dmamem_alloc(sc->sc_dmatag, (void **)&sc->sc_cmd, BUS_DMA_WAITOK |
293 	    BUS_DMA_ZERO, &sc->sc_cmd_dmamap);
294 	bus_dmamap_load(sc->sc_dmatag, sc->sc_cmd_dmamap,
295 	    sc->sc_cmd, PAGE_SIZE, smu_phys_callback, sc, 0);
296 	STAILQ_INIT(&sc->sc_cmdq);
297 
298 	/*
299 	 * Set up handlers to change CPU voltage when CPU frequency is changed.
300 	 */
301 	EVENTHANDLER_REGISTER(cpufreq_pre_change, smu_cpufreq_pre_change, dev,
302 	    EVENTHANDLER_PRI_ANY);
303 	EVENTHANDLER_REGISTER(cpufreq_post_change, smu_cpufreq_post_change, dev,
304 	    EVENTHANDLER_PRI_ANY);
305 
306 	/*
307 	 * Detect and attach child devices.
308 	 */
309 	node = ofw_bus_get_node(dev);
310 	for (child = OF_child(node); child != 0; child = OF_peer(child)) {
311 		char name[32];
312 		memset(name, 0, sizeof(name));
313 		OF_getprop(child, "name", name, sizeof(name));
314 
315 		if (strncmp(name, "rpm-fans", 9) == 0 ||
316 		    strncmp(name, "fans", 5) == 0)
317 			smu_attach_fans(dev, child);
318 
319 		if (strncmp(name, "sensors", 8) == 0)
320 			smu_attach_sensors(dev, child);
321 
322 		if (strncmp(name, "smu-i2c-control", 15) == 0)
323 			smu_attach_i2c(dev, child);
324 	}
325 
326 	/* Some SMUs have the I2C children directly under the bus. */
327 	smu_attach_i2c(dev, node);
328 
329 	/*
330 	 * Collect calibration constants.
331 	 */
332 	smu_get_datablock(dev, SMU_CPUTEMP_CAL, data, sizeof(data));
333 	sc->sc_cpu_diode_scale = (data[4] << 8) + data[5];
334 	sc->sc_cpu_diode_offset = (data[6] << 8) + data[7];
335 
336 	smu_get_datablock(dev, SMU_CPUVOLT_CAL, data, sizeof(data));
337 	sc->sc_cpu_volt_scale = (data[4] << 8) + data[5];
338 	sc->sc_cpu_volt_offset = (data[6] << 8) + data[7];
339 	sc->sc_cpu_curr_scale = (data[8] << 8) + data[9];
340 	sc->sc_cpu_curr_offset = (data[10] << 8) + data[11];
341 
342 	smu_get_datablock(dev, SMU_SLOTPW_CAL, data, sizeof(data));
343 	sc->sc_slots_pow_scale = (data[4] << 8) + data[5];
344 	sc->sc_slots_pow_offset = (data[6] << 8) + data[7];
345 
346 	/*
347 	 * Set up LED interface
348 	 */
349 	sc->sc_leddev = led_create(smu_set_sleepled, dev, "sleepled");
350 
351 	/*
352 	 * Reset on power loss behavior
353 	 */
354 
355 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
356             SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
357 	    "server_mode", CTLTYPE_INT | CTLFLAG_RW, dev, 0,
358 	    smu_server_mode, "I", "Enable reboot after power failure");
359 
360 	/*
361 	 * Set up doorbell interrupt.
362 	 */
363 	sc->sc_doorbellirqid = 0;
364 	sc->sc_doorbellirq = bus_alloc_resource_any(smu_doorbell, SYS_RES_IRQ,
365 	    &sc->sc_doorbellirqid, RF_ACTIVE);
366 	bus_setup_intr(smu_doorbell, sc->sc_doorbellirq,
367 	    INTR_TYPE_MISC | INTR_MPSAFE, NULL, smu_doorbell_intr, dev,
368 	    &sc->sc_doorbellirqcookie);
369 	powerpc_config_intr(rman_get_start(sc->sc_doorbellirq),
370 	    INTR_TRIGGER_EDGE, INTR_POLARITY_LOW);
371 
372 	/*
373 	 * Connect RTC interface.
374 	 */
375 	clock_register(dev, 1000);
376 
377 	/*
378 	 * Learn about shutdown events
379 	 */
380 	EVENTHANDLER_REGISTER(shutdown_final, smu_shutdown, dev,
381 	    SHUTDOWN_PRI_LAST);
382 
383 	return (bus_generic_attach(dev));
384 }
385 
386 static const struct ofw_bus_devinfo *
387 smu_get_devinfo(device_t bus, device_t dev)
388 {
389 
390 	return (device_get_ivars(dev));
391 }
392 
393 static void
394 smu_send_cmd(device_t dev, struct smu_cmd *cmd)
395 {
396 	struct smu_softc *sc;
397 
398 	sc = device_get_softc(dev);
399 
400 	mtx_assert(&sc->sc_mtx, MA_OWNED);
401 
402 	if (sc->sc_u3)
403 		powerpc_pow_enabled = 0; /* SMU cannot work if we go to NAP */
404 
405 	sc->sc_cur_cmd = cmd;
406 
407 	/* Copy the command to the mailbox */
408 	sc->sc_cmd->cmd = cmd->cmd;
409 	sc->sc_cmd->len = cmd->len;
410 	memcpy(sc->sc_cmd->data, cmd->data, sizeof(cmd->data));
411 	bus_dmamap_sync(sc->sc_dmatag, sc->sc_cmd_dmamap, BUS_DMASYNC_PREWRITE);
412 	bus_space_write_4(sc->sc_bt, sc->sc_mailbox, 0, sc->sc_cmd_phys);
413 
414 	/* Flush the cacheline it is in -- SMU bypasses the cache */
415 	__asm __volatile("sync; dcbf 0,%0; sync" :: "r"(sc->sc_cmd): "memory");
416 
417 	/* Ring SMU doorbell */
418 	macgpio_write(smu_doorbell, GPIO_DDR_OUTPUT);
419 }
420 
421 static void
422 smu_doorbell_intr(void *xdev)
423 {
424 	device_t smu;
425 	struct smu_softc *sc;
426 	int doorbell_ack;
427 
428 	smu = xdev;
429 	doorbell_ack = macgpio_read(smu_doorbell);
430 	sc = device_get_softc(smu);
431 
432 	if (doorbell_ack != (GPIO_DDR_OUTPUT | GPIO_LEVEL_RO | GPIO_DATA))
433 		return;
434 
435 	mtx_lock(&sc->sc_mtx);
436 
437 	if (sc->sc_cur_cmd == NULL)	/* spurious */
438 		goto done;
439 
440 	/* Check result. First invalidate the cache again... */
441 	__asm __volatile("dcbf 0,%0; sync" :: "r"(sc->sc_cmd) : "memory");
442 
443 	bus_dmamap_sync(sc->sc_dmatag, sc->sc_cmd_dmamap, BUS_DMASYNC_POSTREAD);
444 
445 	sc->sc_cur_cmd->cmd = sc->sc_cmd->cmd;
446 	sc->sc_cur_cmd->len = sc->sc_cmd->len;
447 	memcpy(sc->sc_cur_cmd->data, sc->sc_cmd->data,
448 	    sizeof(sc->sc_cmd->data));
449 	wakeup(sc->sc_cur_cmd);
450 	sc->sc_cur_cmd = NULL;
451 	if (sc->sc_u3)
452 		powerpc_pow_enabled = 1;
453 
454     done:
455 	/* Queue next command if one is pending */
456 	if (STAILQ_FIRST(&sc->sc_cmdq) != NULL) {
457 		sc->sc_cur_cmd = STAILQ_FIRST(&sc->sc_cmdq);
458 		STAILQ_REMOVE_HEAD(&sc->sc_cmdq, cmd_q);
459 		smu_send_cmd(smu, sc->sc_cur_cmd);
460 	}
461 
462 	mtx_unlock(&sc->sc_mtx);
463 }
464 
465 static int
466 smu_run_cmd(device_t dev, struct smu_cmd *cmd, int wait)
467 {
468 	struct smu_softc *sc;
469 	uint8_t cmd_code;
470 	int error;
471 
472 	sc = device_get_softc(dev);
473 	cmd_code = cmd->cmd;
474 
475 	mtx_lock(&sc->sc_mtx);
476 	if (sc->sc_cur_cmd != NULL) {
477 		STAILQ_INSERT_TAIL(&sc->sc_cmdq, cmd, cmd_q);
478 	} else
479 		smu_send_cmd(dev, cmd);
480 	mtx_unlock(&sc->sc_mtx);
481 
482 	if (!wait)
483 		return (0);
484 
485 	if (sc->sc_doorbellirqid < 0) {
486 		/* Poll if the IRQ has not been set up yet */
487 		do {
488 			DELAY(50);
489 			smu_doorbell_intr(dev);
490 		} while (sc->sc_cur_cmd != NULL);
491 	} else {
492 		/* smu_doorbell_intr will wake us when the command is ACK'ed */
493 		error = tsleep(cmd, 0, "smu", 800 * hz / 1000);
494 		if (error != 0)
495 			smu_doorbell_intr(dev);	/* One last chance */
496 
497 		if (error != 0) {
498 		    mtx_lock(&sc->sc_mtx);
499 		    if (cmd->cmd == cmd_code) {	/* Never processed */
500 			/* Abort this command if we timed out */
501 			if (sc->sc_cur_cmd == cmd)
502 				sc->sc_cur_cmd = NULL;
503 			else
504 				STAILQ_REMOVE(&sc->sc_cmdq, cmd, smu_cmd,
505 				    cmd_q);
506 			mtx_unlock(&sc->sc_mtx);
507 			return (error);
508 		    }
509 		    error = 0;
510 		    mtx_unlock(&sc->sc_mtx);
511 		}
512 	}
513 
514 	/* SMU acks the command by inverting the command bits */
515 	if (cmd->cmd == ((~cmd_code) & 0xff))
516 		error = 0;
517 	else
518 		error = EIO;
519 
520 	return (error);
521 }
522 
523 static int
524 smu_get_datablock(device_t dev, int8_t id, uint8_t *buf, size_t len)
525 {
526 	struct smu_cmd cmd;
527 	uint8_t addr[4];
528 
529 	cmd.cmd = SMU_PARTITION;
530 	cmd.len = 2;
531 	cmd.data[0] = SMU_PARTITION_LATEST;
532 	cmd.data[1] = id;
533 
534 	smu_run_cmd(dev, &cmd, 1);
535 
536 	addr[0] = addr[1] = 0;
537 	addr[2] = cmd.data[0];
538 	addr[3] = cmd.data[1];
539 
540 	cmd.cmd = SMU_MISC;
541 	cmd.len = 7;
542 	cmd.data[0] = SMU_MISC_GET_DATA;
543 	cmd.data[1] = sizeof(addr);
544 	memcpy(&cmd.data[2], addr, sizeof(addr));
545 	cmd.data[6] = len;
546 
547 	smu_run_cmd(dev, &cmd, 1);
548 	memcpy(buf, cmd.data, len);
549 	return (0);
550 }
551 
552 static void
553 smu_slew_cpu_voltage(device_t dev, int to)
554 {
555 	struct smu_cmd cmd;
556 
557 	cmd.cmd = SMU_POWER;
558 	cmd.len = 8;
559 	cmd.data[0] = 'V';
560 	cmd.data[1] = 'S';
561 	cmd.data[2] = 'L';
562 	cmd.data[3] = 'E';
563 	cmd.data[4] = 'W';
564 	cmd.data[5] = 0xff;
565 	cmd.data[6] = 1;
566 	cmd.data[7] = to;
567 
568 	smu_run_cmd(dev, &cmd, 1);
569 }
570 
571 static void
572 smu_cpufreq_pre_change(device_t dev, const struct cf_level *level)
573 {
574 	/*
575 	 * Make sure the CPU voltage is raised before we raise
576 	 * the clock.
577 	 */
578 
579 	if (level->rel_set[0].freq == 10000 /* max */)
580 		smu_slew_cpu_voltage(dev, 0);
581 }
582 
583 static void
584 smu_cpufreq_post_change(device_t dev, const struct cf_level *level)
585 {
586 	/* We are safe to reduce CPU voltage after a downward transition */
587 
588 	if (level->rel_set[0].freq < 10000 /* max */)
589 		smu_slew_cpu_voltage(dev, 1); /* XXX: 1/4 voltage for 970MP? */
590 }
591 
592 /* Routines for probing the SMU doorbell GPIO */
593 static int doorbell_probe(device_t dev);
594 static int doorbell_attach(device_t dev);
595 
596 static device_method_t  doorbell_methods[] = {
597 	/* Device interface */
598 	DEVMETHOD(device_probe,		doorbell_probe),
599 	DEVMETHOD(device_attach,	doorbell_attach),
600 	{ 0, 0 },
601 };
602 
603 static driver_t doorbell_driver = {
604 	"smudoorbell",
605 	doorbell_methods,
606 	0
607 };
608 
609 static devclass_t doorbell_devclass;
610 
611 DRIVER_MODULE(smudoorbell, macgpio, doorbell_driver, doorbell_devclass, 0, 0);
612 
613 static int
614 doorbell_probe(device_t dev)
615 {
616 	const char *name = ofw_bus_get_name(dev);
617 
618 	if (strcmp(name, "smu-doorbell") != 0)
619 		return (ENXIO);
620 
621 	device_set_desc(dev, "SMU Doorbell GPIO");
622 	device_quiet(dev);
623 	return (0);
624 }
625 
626 static int
627 doorbell_attach(device_t dev)
628 {
629 	smu_doorbell = dev;
630 	return (0);
631 }
632 
633 /*
634  * Sensor and fan management
635  */
636 
637 static int
638 smu_fan_set_rpm(struct smu_fan *fan, int rpm)
639 {
640 	device_t smu = fan->dev;
641 	struct smu_cmd cmd;
642 	int error;
643 
644 	cmd.cmd = SMU_FAN;
645 	error = EIO;
646 
647 	/* Clamp to allowed range */
648 	rpm = max(fan->fan.min_rpm, rpm);
649 	rpm = min(fan->fan.max_rpm, rpm);
650 
651 	/*
652 	 * Apple has two fan control mechanisms. We can't distinguish
653 	 * them except by seeing if the new one fails. If the new one
654 	 * fails, use the old one.
655 	 */
656 
657 	if (!fan->old_style) {
658 		cmd.len = 4;
659 		cmd.data[0] = 0x30;
660 		cmd.data[1] = fan->reg;
661 		cmd.data[2] = (rpm >> 8) & 0xff;
662 		cmd.data[3] = rpm & 0xff;
663 
664 		error = smu_run_cmd(smu, &cmd, 1);
665 		if (error && error != EWOULDBLOCK)
666 			fan->old_style = 1;
667 	}
668 
669 	if (fan->old_style) {
670 		cmd.len = 14;
671 		cmd.data[0] = 0;
672 		cmd.data[1] = 1 << fan->reg;
673 		cmd.data[2 + 2*fan->reg] = (rpm >> 8) & 0xff;
674 		cmd.data[3 + 2*fan->reg] = rpm & 0xff;
675 		error = smu_run_cmd(smu, &cmd, 1);
676 	}
677 
678 	if (error == 0)
679 		fan->setpoint = rpm;
680 
681 	return (error);
682 }
683 
684 static int
685 smu_fan_read_rpm(struct smu_fan *fan)
686 {
687 	device_t smu = fan->dev;
688 	struct smu_cmd cmd;
689 	int rpm, error;
690 
691 	if (!fan->old_style) {
692 		cmd.cmd = SMU_FAN;
693 		cmd.len = 2;
694 		cmd.data[0] = 0x31;
695 		cmd.data[1] = fan->reg;
696 
697 		error = smu_run_cmd(smu, &cmd, 1);
698 		if (error && error != EWOULDBLOCK)
699 			fan->old_style = 1;
700 
701 		rpm = (cmd.data[0] << 8) | cmd.data[1];
702 	}
703 
704 	if (fan->old_style) {
705 		cmd.cmd = SMU_FAN;
706 		cmd.len = 1;
707 		cmd.data[0] = 1;
708 
709 		error = smu_run_cmd(smu, &cmd, 1);
710 		if (error)
711 			return (error);
712 
713 		rpm = (cmd.data[fan->reg*2+1] << 8) | cmd.data[fan->reg*2+2];
714 	}
715 
716 	return (rpm);
717 }
718 
719 static int
720 smu_fanrpm_sysctl(SYSCTL_HANDLER_ARGS)
721 {
722 	device_t smu;
723 	struct smu_softc *sc;
724 	struct smu_fan *fan;
725 	int rpm, error;
726 
727 	smu = arg1;
728 	sc = device_get_softc(smu);
729 	fan = &sc->sc_fans[arg2];
730 
731 	rpm = smu_fan_read_rpm(fan);
732 	if (rpm < 0)
733 		return (rpm);
734 
735 	error = sysctl_handle_int(oidp, &rpm, 0, req);
736 
737 	if (error || !req->newptr)
738 		return (error);
739 
740 	sc->sc_lastuserchange = time_uptime;
741 
742 	return (smu_fan_set_rpm(fan, rpm));
743 }
744 
745 static void
746 smu_attach_fans(device_t dev, phandle_t fanroot)
747 {
748 	struct smu_fan *fan;
749 	struct smu_softc *sc;
750 	struct sysctl_oid *oid, *fanroot_oid;
751 	struct sysctl_ctx_list *ctx;
752 	phandle_t child;
753 	char type[32], sysctl_name[32];
754 	int i;
755 
756 	sc = device_get_softc(dev);
757 	sc->sc_nfans = 0;
758 
759 	for (child = OF_child(fanroot); child != 0; child = OF_peer(child))
760 		sc->sc_nfans++;
761 
762 	if (sc->sc_nfans == 0) {
763 		device_printf(dev, "WARNING: No fans detected!\n");
764 		return;
765 	}
766 
767 	sc->sc_fans = malloc(sc->sc_nfans * sizeof(struct smu_fan), M_SMU,
768 	    M_WAITOK | M_ZERO);
769 
770 	fan = sc->sc_fans;
771 	sc->sc_nfans = 0;
772 
773 	ctx = device_get_sysctl_ctx(dev);
774 	fanroot_oid = SYSCTL_ADD_NODE(ctx,
775 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "fans",
776 	    CTLFLAG_RD, 0, "SMU Fan Information");
777 
778 	for (child = OF_child(fanroot); child != 0; child = OF_peer(child)) {
779 		OF_getprop(child, "device_type", type, sizeof(type));
780 		if (strcmp(type, "fan-rpm-control") != 0)
781 			continue;
782 
783 		fan->dev = dev;
784 		fan->old_style = 0;
785 		OF_getprop(child, "reg", &fan->reg, sizeof(cell_t));
786 		OF_getprop(child, "min-value", &fan->fan.min_rpm, sizeof(int));
787 		OF_getprop(child, "max-value", &fan->fan.max_rpm, sizeof(int));
788 		OF_getprop(child, "zone", &fan->fan.zone, sizeof(int));
789 
790 		if (OF_getprop(child, "unmanaged-value", &fan->fan.default_rpm,
791 		    sizeof(int)) != sizeof(int))
792 			fan->fan.default_rpm = fan->fan.max_rpm;
793 
794 		fan->setpoint = smu_fan_read_rpm(fan);
795 
796 		OF_getprop(child, "location", fan->fan.name,
797 		    sizeof(fan->fan.name));
798 
799 		/* Add sysctls */
800 		for (i = 0; i < strlen(fan->fan.name); i++) {
801 			sysctl_name[i] = tolower(fan->fan.name[i]);
802 			if (isspace(sysctl_name[i]))
803 				sysctl_name[i] = '_';
804 		}
805 		sysctl_name[i] = 0;
806 
807 		oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(fanroot_oid),
808 		    OID_AUTO, sysctl_name, CTLFLAG_RD, 0, "Fan Information");
809 		SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, "minrpm",
810 		    CTLTYPE_INT | CTLFLAG_RD, &fan->fan.min_rpm, sizeof(int),
811 		    "Minimum allowed RPM");
812 		SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, "maxrpm",
813 		    CTLTYPE_INT | CTLFLAG_RD, &fan->fan.max_rpm, sizeof(int),
814 		    "Maximum allowed RPM");
815 		SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, "rpm",
816 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, dev,
817 		    sc->sc_nfans, smu_fanrpm_sysctl, "I", "Fan RPM");
818 
819 		fan->fan.read = (int (*)(struct pmac_fan *))smu_fan_read_rpm;
820 		fan->fan.set = (int (*)(struct pmac_fan *, int))smu_fan_set_rpm;
821 		pmac_thermal_fan_register(&fan->fan);
822 
823 		fan++;
824 		sc->sc_nfans++;
825 	}
826 }
827 
828 static int
829 smu_sensor_read(struct smu_sensor *sens)
830 {
831 	device_t smu = sens->dev;
832 	struct smu_cmd cmd;
833 	struct smu_softc *sc;
834 	int64_t value;
835 	int error;
836 
837 	cmd.cmd = SMU_ADC;
838 	cmd.len = 1;
839 	cmd.data[0] = sens->reg;
840 	error = 0;
841 
842 	error = smu_run_cmd(smu, &cmd, 1);
843 	if (error != 0)
844 		return (-1);
845 
846 	sc = device_get_softc(smu);
847 	value = (cmd.data[0] << 8) | cmd.data[1];
848 
849 	switch (sens->type) {
850 	case SMU_TEMP_SENSOR:
851 		value *= sc->sc_cpu_diode_scale;
852 		value >>= 3;
853 		value += ((int64_t)sc->sc_cpu_diode_offset) << 9;
854 		value <<= 1;
855 
856 		/* Convert from 16.16 fixed point degC into integer 0.1 K. */
857 		value = 10*(value >> 16) + ((10*(value & 0xffff)) >> 16) + 2732;
858 		break;
859 	case SMU_VOLTAGE_SENSOR:
860 		value *= sc->sc_cpu_volt_scale;
861 		value += sc->sc_cpu_volt_offset;
862 		value <<= 4;
863 
864 		/* Convert from 16.16 fixed point V into mV. */
865 		value *= 15625;
866 		value /= 1024;
867 		value /= 1000;
868 		break;
869 	case SMU_CURRENT_SENSOR:
870 		value *= sc->sc_cpu_curr_scale;
871 		value += sc->sc_cpu_curr_offset;
872 		value <<= 4;
873 
874 		/* Convert from 16.16 fixed point A into mA. */
875 		value *= 15625;
876 		value /= 1024;
877 		value /= 1000;
878 		break;
879 	case SMU_POWER_SENSOR:
880 		value *= sc->sc_slots_pow_scale;
881 		value += sc->sc_slots_pow_offset;
882 		value <<= 4;
883 
884 		/* Convert from 16.16 fixed point W into mW. */
885 		value *= 15625;
886 		value /= 1024;
887 		value /= 1000;
888 		break;
889 	}
890 
891 	return (value);
892 }
893 
894 static int
895 smu_sensor_sysctl(SYSCTL_HANDLER_ARGS)
896 {
897 	device_t smu;
898 	struct smu_softc *sc;
899 	struct smu_sensor *sens;
900 	int value, error;
901 
902 	smu = arg1;
903 	sc = device_get_softc(smu);
904 	sens = &sc->sc_sensors[arg2];
905 
906 	value = smu_sensor_read(sens);
907 	if (value < 0)
908 		return (EBUSY);
909 
910 	error = sysctl_handle_int(oidp, &value, 0, req);
911 
912 	return (error);
913 }
914 
915 static void
916 smu_attach_sensors(device_t dev, phandle_t sensroot)
917 {
918 	struct smu_sensor *sens;
919 	struct smu_softc *sc;
920 	struct sysctl_oid *sensroot_oid;
921 	struct sysctl_ctx_list *ctx;
922 	phandle_t child;
923 	char type[32];
924 	int i;
925 
926 	sc = device_get_softc(dev);
927 	sc->sc_nsensors = 0;
928 
929 	for (child = OF_child(sensroot); child != 0; child = OF_peer(child))
930 		sc->sc_nsensors++;
931 
932 	if (sc->sc_nsensors == 0) {
933 		device_printf(dev, "WARNING: No sensors detected!\n");
934 		return;
935 	}
936 
937 	sc->sc_sensors = malloc(sc->sc_nsensors * sizeof(struct smu_sensor),
938 	    M_SMU, M_WAITOK | M_ZERO);
939 
940 	sens = sc->sc_sensors;
941 	sc->sc_nsensors = 0;
942 
943 	ctx = device_get_sysctl_ctx(dev);
944 	sensroot_oid = SYSCTL_ADD_NODE(ctx,
945 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "sensors",
946 	    CTLFLAG_RD, 0, "SMU Sensor Information");
947 
948 	for (child = OF_child(sensroot); child != 0; child = OF_peer(child)) {
949 		char sysctl_name[40], sysctl_desc[40];
950 		const char *units;
951 
952 		sens->dev = dev;
953 		OF_getprop(child, "device_type", type, sizeof(type));
954 
955 		if (strcmp(type, "current-sensor") == 0) {
956 			sens->type = SMU_CURRENT_SENSOR;
957 			units = "mA";
958 		} else if (strcmp(type, "temp-sensor") == 0) {
959 			sens->type = SMU_TEMP_SENSOR;
960 			units = "C";
961 		} else if (strcmp(type, "voltage-sensor") == 0) {
962 			sens->type = SMU_VOLTAGE_SENSOR;
963 			units = "mV";
964 		} else if (strcmp(type, "power-sensor") == 0) {
965 			sens->type = SMU_POWER_SENSOR;
966 			units = "mW";
967 		} else {
968 			continue;
969 		}
970 
971 		OF_getprop(child, "reg", &sens->reg, sizeof(cell_t));
972 		OF_getprop(child, "zone", &sens->therm.zone, sizeof(int));
973 		OF_getprop(child, "location", sens->therm.name,
974 		    sizeof(sens->therm.name));
975 
976 		for (i = 0; i < strlen(sens->therm.name); i++) {
977 			sysctl_name[i] = tolower(sens->therm.name[i]);
978 			if (isspace(sysctl_name[i]))
979 				sysctl_name[i] = '_';
980 		}
981 		sysctl_name[i] = 0;
982 
983 		sprintf(sysctl_desc,"%s (%s)", sens->therm.name, units);
984 
985 		SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(sensroot_oid), OID_AUTO,
986 		    sysctl_name, CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
987 		    dev, sc->sc_nsensors, smu_sensor_sysctl,
988 		    (sens->type == SMU_TEMP_SENSOR) ? "IK" : "I", sysctl_desc);
989 
990 		if (sens->type == SMU_TEMP_SENSOR) {
991 			/* Make up some numbers */
992 			sens->therm.target_temp = 500 + 2732; /* 50 C */
993 			sens->therm.max_temp = 900 + 2732; /* 90 C */
994 
995 			sens->therm.read =
996 			    (int (*)(struct pmac_therm *))smu_sensor_read;
997 			pmac_thermal_sensor_register(&sens->therm);
998 		}
999 
1000 		sens++;
1001 		sc->sc_nsensors++;
1002 	}
1003 }
1004 
1005 static void
1006 smu_set_sleepled(void *xdev, int onoff)
1007 {
1008 	static struct smu_cmd cmd;
1009 	device_t smu = xdev;
1010 
1011 	cmd.cmd = SMU_MISC;
1012 	cmd.len = 3;
1013 	cmd.data[0] = SMU_MISC_LED_CTRL;
1014 	cmd.data[1] = 0;
1015 	cmd.data[2] = onoff;
1016 
1017 	smu_run_cmd(smu, &cmd, 0);
1018 }
1019 
1020 static int
1021 smu_server_mode(SYSCTL_HANDLER_ARGS)
1022 {
1023 	struct smu_cmd cmd;
1024 	u_int server_mode;
1025 	device_t smu = arg1;
1026 	int error;
1027 
1028 	cmd.cmd = SMU_POWER_EVENTS;
1029 	cmd.len = 1;
1030 	cmd.data[0] = SMU_PWR_GET_POWERUP;
1031 
1032 	error = smu_run_cmd(smu, &cmd, 1);
1033 
1034 	if (error)
1035 		return (error);
1036 
1037 	server_mode = (cmd.data[1] & SMU_WAKEUP_AC_INSERT) ? 1 : 0;
1038 
1039 	error = sysctl_handle_int(oidp, &server_mode, 0, req);
1040 
1041 	if (error || !req->newptr)
1042 		return (error);
1043 
1044 	if (server_mode == 1)
1045 		cmd.data[0] = SMU_PWR_SET_POWERUP;
1046 	else if (server_mode == 0)
1047 		cmd.data[0] = SMU_PWR_CLR_POWERUP;
1048 	else
1049 		return (EINVAL);
1050 
1051 	cmd.len = 3;
1052 	cmd.data[1] = 0;
1053 	cmd.data[2] = SMU_WAKEUP_AC_INSERT;
1054 
1055 	return (smu_run_cmd(smu, &cmd, 1));
1056 }
1057 
1058 static void
1059 smu_shutdown(void *xdev, int howto)
1060 {
1061 	device_t smu = xdev;
1062 	struct smu_cmd cmd;
1063 
1064 	cmd.cmd = SMU_POWER;
1065 	if (howto & RB_HALT)
1066 		strcpy(cmd.data, "SHUTDOWN");
1067 	else
1068 		strcpy(cmd.data, "RESTART");
1069 
1070 	cmd.len = strlen(cmd.data);
1071 
1072 	smu_run_cmd(smu, &cmd, 1);
1073 
1074 	for (;;);
1075 }
1076 
1077 static int
1078 smu_gettime(device_t dev, struct timespec *ts)
1079 {
1080 	struct smu_cmd cmd;
1081 	struct clocktime ct;
1082 
1083 	cmd.cmd = SMU_RTC;
1084 	cmd.len = 1;
1085 	cmd.data[0] = SMU_RTC_GET;
1086 
1087 	if (smu_run_cmd(dev, &cmd, 1) != 0)
1088 		return (ENXIO);
1089 
1090 	ct.nsec	= 0;
1091 	ct.sec	= bcd2bin(cmd.data[0]);
1092 	ct.min	= bcd2bin(cmd.data[1]);
1093 	ct.hour	= bcd2bin(cmd.data[2]);
1094 	ct.dow	= bcd2bin(cmd.data[3]);
1095 	ct.day	= bcd2bin(cmd.data[4]);
1096 	ct.mon	= bcd2bin(cmd.data[5]);
1097 	ct.year	= bcd2bin(cmd.data[6]) + 2000;
1098 
1099 	return (clock_ct_to_ts(&ct, ts));
1100 }
1101 
1102 static int
1103 smu_settime(device_t dev, struct timespec *ts)
1104 {
1105 	static struct smu_cmd cmd;
1106 	struct clocktime ct;
1107 
1108 	cmd.cmd = SMU_RTC;
1109 	cmd.len = 8;
1110 	cmd.data[0] = SMU_RTC_SET;
1111 
1112 	clock_ts_to_ct(ts, &ct);
1113 
1114 	cmd.data[1] = bin2bcd(ct.sec);
1115 	cmd.data[2] = bin2bcd(ct.min);
1116 	cmd.data[3] = bin2bcd(ct.hour);
1117 	cmd.data[4] = bin2bcd(ct.dow);
1118 	cmd.data[5] = bin2bcd(ct.day);
1119 	cmd.data[6] = bin2bcd(ct.mon);
1120 	cmd.data[7] = bin2bcd(ct.year - 2000);
1121 
1122 	return (smu_run_cmd(dev, &cmd, 0));
1123 }
1124 
1125 /* SMU I2C Interface */
1126 
1127 static int smuiic_probe(device_t dev);
1128 static int smuiic_attach(device_t dev);
1129 static int smuiic_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs);
1130 static phandle_t smuiic_get_node(device_t bus, device_t dev);
1131 
1132 static device_method_t smuiic_methods[] = {
1133 	/* device interface */
1134 	DEVMETHOD(device_probe,         smuiic_probe),
1135 	DEVMETHOD(device_attach,        smuiic_attach),
1136 
1137 	/* iicbus interface */
1138 	DEVMETHOD(iicbus_callback,      iicbus_null_callback),
1139 	DEVMETHOD(iicbus_transfer,      smuiic_transfer),
1140 
1141 	/* ofw_bus interface */
1142 	DEVMETHOD(ofw_bus_get_node,     smuiic_get_node),
1143 
1144 	{ 0, 0 }
1145 };
1146 
1147 struct smuiic_softc {
1148 	struct mtx	sc_mtx;
1149 	volatile int	sc_iic_inuse;
1150 	int		sc_busno;
1151 };
1152 
1153 static driver_t smuiic_driver = {
1154 	"iichb",
1155 	smuiic_methods,
1156 	sizeof(struct smuiic_softc)
1157 };
1158 static devclass_t smuiic_devclass;
1159 
1160 DRIVER_MODULE(smuiic, smu, smuiic_driver, smuiic_devclass, 0, 0);
1161 
1162 static void
1163 smu_attach_i2c(device_t smu, phandle_t i2croot)
1164 {
1165 	phandle_t child;
1166 	device_t cdev;
1167 	struct ofw_bus_devinfo *dinfo;
1168 	char name[32];
1169 
1170 	for (child = OF_child(i2croot); child != 0; child = OF_peer(child)) {
1171 		if (OF_getprop(child, "name", name, sizeof(name)) <= 0)
1172 			continue;
1173 
1174 		if (strcmp(name, "i2c-bus") != 0 && strcmp(name, "i2c") != 0)
1175 			continue;
1176 
1177 		dinfo = malloc(sizeof(struct ofw_bus_devinfo), M_SMU,
1178 		    M_WAITOK | M_ZERO);
1179 		if (ofw_bus_gen_setup_devinfo(dinfo, child) != 0) {
1180 			free(dinfo, M_SMU);
1181 			continue;
1182 		}
1183 
1184 		cdev = device_add_child(smu, NULL, -1);
1185 		if (cdev == NULL) {
1186 			device_printf(smu, "<%s>: device_add_child failed\n",
1187 			    dinfo->obd_name);
1188 			ofw_bus_gen_destroy_devinfo(dinfo);
1189 			free(dinfo, M_SMU);
1190 			continue;
1191 		}
1192 		device_set_ivars(cdev, dinfo);
1193 	}
1194 }
1195 
1196 static int
1197 smuiic_probe(device_t dev)
1198 {
1199 	const char *name;
1200 
1201 	name = ofw_bus_get_name(dev);
1202 	if (name == NULL)
1203 		return (ENXIO);
1204 
1205 	if (strcmp(name, "i2c-bus") == 0 || strcmp(name, "i2c") == 0) {
1206 		device_set_desc(dev, "SMU I2C controller");
1207 		return (0);
1208 	}
1209 
1210 	return (ENXIO);
1211 }
1212 
1213 static int
1214 smuiic_attach(device_t dev)
1215 {
1216 	struct smuiic_softc *sc = device_get_softc(dev);
1217 	mtx_init(&sc->sc_mtx, "smuiic", NULL, MTX_DEF);
1218 	sc->sc_iic_inuse = 0;
1219 
1220 	/* Get our bus number */
1221 	OF_getprop(ofw_bus_get_node(dev), "reg", &sc->sc_busno,
1222 	    sizeof(sc->sc_busno));
1223 
1224 	/* Add the IIC bus layer */
1225 	device_add_child(dev, "iicbus", -1);
1226 
1227 	return (bus_generic_attach(dev));
1228 }
1229 
1230 static int
1231 smuiic_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
1232 {
1233 	struct smuiic_softc *sc = device_get_softc(dev);
1234 	struct smu_cmd cmd;
1235 	int i, j, error;
1236 
1237 	mtx_lock(&sc->sc_mtx);
1238 	while (sc->sc_iic_inuse)
1239 		mtx_sleep(sc, &sc->sc_mtx, 0, "smuiic", 100);
1240 
1241 	sc->sc_iic_inuse = 1;
1242 	error = 0;
1243 
1244 	for (i = 0; i < nmsgs; i++) {
1245 		cmd.cmd = SMU_I2C;
1246 		cmd.data[0] = sc->sc_busno;
1247 		if (msgs[i].flags & IIC_M_NOSTOP)
1248 			cmd.data[1] = SMU_I2C_COMBINED;
1249 		else
1250 			cmd.data[1] = SMU_I2C_SIMPLE;
1251 
1252 		cmd.data[2] = msgs[i].slave;
1253 		if (msgs[i].flags & IIC_M_RD)
1254 			cmd.data[2] |= 1;
1255 
1256 		if (msgs[i].flags & IIC_M_NOSTOP) {
1257 			KASSERT(msgs[i].len < 4,
1258 			    ("oversize I2C combined message"));
1259 
1260 			cmd.data[3] = min(msgs[i].len, 3);
1261 			memcpy(&cmd.data[4], msgs[i].buf, min(msgs[i].len, 3));
1262 			i++; /* Advance to next part of message */
1263 		} else {
1264 			cmd.data[3] = 0;
1265 			memset(&cmd.data[4], 0, 3);
1266 		}
1267 
1268 		cmd.data[7] = msgs[i].slave;
1269 		if (msgs[i].flags & IIC_M_RD)
1270 			cmd.data[7] |= 1;
1271 
1272 		cmd.data[8] = msgs[i].len;
1273 		if (msgs[i].flags & IIC_M_RD) {
1274 			memset(&cmd.data[9], 0xff, msgs[i].len);
1275 			cmd.len = 9;
1276 		} else {
1277 			memcpy(&cmd.data[9], msgs[i].buf, msgs[i].len);
1278 			cmd.len = 9 + msgs[i].len;
1279 		}
1280 
1281 		mtx_unlock(&sc->sc_mtx);
1282 		smu_run_cmd(device_get_parent(dev), &cmd, 1);
1283 		mtx_lock(&sc->sc_mtx);
1284 
1285 		for (j = 0; j < 10; j++) {
1286 			cmd.cmd = SMU_I2C;
1287 			cmd.len = 1;
1288 			cmd.data[0] = 0;
1289 			memset(&cmd.data[1], 0xff, msgs[i].len);
1290 
1291 			mtx_unlock(&sc->sc_mtx);
1292 			smu_run_cmd(device_get_parent(dev), &cmd, 1);
1293 			mtx_lock(&sc->sc_mtx);
1294 
1295 			if (!(cmd.data[0] & 0x80))
1296 				break;
1297 
1298 			mtx_sleep(sc, &sc->sc_mtx, 0, "smuiic", 10);
1299 		}
1300 
1301 		if (cmd.data[0] & 0x80) {
1302 			error = EIO;
1303 			msgs[i].len = 0;
1304 			goto exit;
1305 		}
1306 		memcpy(msgs[i].buf, &cmd.data[1], msgs[i].len);
1307 		msgs[i].len = cmd.len - 1;
1308 	}
1309 
1310     exit:
1311 	sc->sc_iic_inuse = 0;
1312 	mtx_unlock(&sc->sc_mtx);
1313 	wakeup(sc);
1314 	return (error);
1315 }
1316 
1317 static phandle_t
1318 smuiic_get_node(device_t bus, device_t dev)
1319 {
1320 
1321 	return (ofw_bus_get_node(bus));
1322 }
1323 
1324