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