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