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