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