1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2006 Michael Lorenz
5 * Copyright 2008 by Nathan Whitehorn
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 */
30
31 #include <sys/cdefs.h>
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/module.h>
35 #include <sys/bus.h>
36 #include <sys/conf.h>
37 #include <sys/eventhandler.h>
38 #include <sys/kernel.h>
39 #include <sys/kthread.h>
40 #include <sys/lock.h>
41 #include <sys/mutex.h>
42 #include <sys/clock.h>
43 #include <sys/proc.h>
44 #include <sys/reboot.h>
45 #include <sys/sysctl.h>
46
47 #include <dev/ofw/ofw_bus.h>
48 #include <dev/ofw/openfirm.h>
49 #include <dev/led/led.h>
50
51 #include <machine/_inttypes.h>
52 #include <machine/bus.h>
53 #include <machine/cpu.h>
54 #include <machine/hid.h>
55 #include <machine/intr_machdep.h>
56 #include <machine/md_var.h>
57 #include <machine/pcb.h>
58 #include <machine/pio.h>
59 #include <machine/resource.h>
60
61 #include <vm/vm.h>
62 #include <vm/pmap.h>
63
64 #include <sys/rman.h>
65
66 #include <dev/adb/adb.h>
67
68 #include "clock_if.h"
69 #include "pmuvar.h"
70 #include "viareg.h"
71 #include "uninorthvar.h" /* For unin_chip_sleep()/unin_chip_wake() */
72
73 #define PMU_DEFAULTS PMU_INT_TICK | PMU_INT_ADB | \
74 PMU_INT_PCEJECT | PMU_INT_SNDBRT | \
75 PMU_INT_BATTERY | PMU_INT_ENVIRONMENT
76
77 /*
78 * Bus interface
79 */
80 static int pmu_probe(device_t);
81 static int pmu_attach(device_t);
82 static int pmu_detach(device_t);
83
84 /*
85 * Clock interface
86 */
87 static int pmu_gettime(device_t dev, struct timespec *ts);
88 static int pmu_settime(device_t dev, struct timespec *ts);
89
90 /*
91 * ADB Interface
92 */
93
94 static u_int pmu_adb_send(device_t dev, u_char command_byte, int len,
95 u_char *data, u_char poll);
96 static u_int pmu_adb_autopoll(device_t dev, uint16_t mask);
97 static u_int pmu_poll(device_t dev);
98
99 /*
100 * Power interface
101 */
102
103 static void pmu_shutdown(void *xsc, int howto);
104 static void pmu_set_sleepled(void *xsc, int onoff);
105 static int pmu_server_mode(SYSCTL_HANDLER_ARGS);
106 static int pmu_acline_state(SYSCTL_HANDLER_ARGS);
107 static int pmu_query_battery(struct pmu_softc *sc, int batt,
108 struct pmu_battstate *info);
109 static int pmu_battquery_sysctl(SYSCTL_HANDLER_ARGS);
110 static int pmu_battmon(SYSCTL_HANDLER_ARGS);
111 static void pmu_battquery_proc(void);
112 static void pmu_battery_notify(struct pmu_battstate *batt,
113 struct pmu_battstate *old);
114
115 /*
116 * List of battery-related sysctls we might ask for
117 */
118
119 enum {
120 PMU_BATSYSCTL_PRESENT = 1 << 8,
121 PMU_BATSYSCTL_CHARGING = 2 << 8,
122 PMU_BATSYSCTL_CHARGE = 3 << 8,
123 PMU_BATSYSCTL_MAXCHARGE = 4 << 8,
124 PMU_BATSYSCTL_CURRENT = 5 << 8,
125 PMU_BATSYSCTL_VOLTAGE = 6 << 8,
126 PMU_BATSYSCTL_TIME = 7 << 8,
127 PMU_BATSYSCTL_LIFE = 8 << 8
128 };
129
130 static device_method_t pmu_methods[] = {
131 /* Device interface */
132 DEVMETHOD(device_probe, pmu_probe),
133 DEVMETHOD(device_attach, pmu_attach),
134 DEVMETHOD(device_detach, pmu_detach),
135 DEVMETHOD(device_shutdown, bus_generic_shutdown),
136
137 /* ADB bus interface */
138 DEVMETHOD(adb_hb_send_raw_packet, pmu_adb_send),
139 DEVMETHOD(adb_hb_controller_poll, pmu_poll),
140 DEVMETHOD(adb_hb_set_autopoll_mask, pmu_adb_autopoll),
141
142 /* Clock interface */
143 DEVMETHOD(clock_gettime, pmu_gettime),
144 DEVMETHOD(clock_settime, pmu_settime),
145
146 DEVMETHOD_END
147 };
148
149 static driver_t pmu_driver = {
150 "pmu",
151 pmu_methods,
152 sizeof(struct pmu_softc),
153 };
154
155 static devclass_t pmu_devclass;
156
157 EARLY_DRIVER_MODULE(pmu, macio, pmu_driver, pmu_devclass, 0, 0,
158 BUS_PASS_RESOURCE);
159 DRIVER_MODULE(adb, pmu, adb_driver, adb_devclass, 0, 0);
160
161 static int pmuextint_probe(device_t);
162 static int pmuextint_attach(device_t);
163
164 static device_method_t pmuextint_methods[] = {
165 /* Device interface */
166 DEVMETHOD(device_probe, pmuextint_probe),
167 DEVMETHOD(device_attach, pmuextint_attach),
168 {0,0}
169 };
170
171 static driver_t pmuextint_driver = {
172 "pmuextint",
173 pmuextint_methods,
174 0
175 };
176
177 static devclass_t pmuextint_devclass;
178
179 EARLY_DRIVER_MODULE(pmuextint, macgpio, pmuextint_driver, pmuextint_devclass,
180 0, 0, BUS_PASS_RESOURCE);
181
182 /* Make sure uhid is loaded, as it turns off some of the ADB emulation */
183 MODULE_DEPEND(pmu, usb, 1, 1, 1);
184
185 static void pmu_intr(void *arg);
186 static void pmu_in(struct pmu_softc *sc);
187 static void pmu_out(struct pmu_softc *sc);
188 static void pmu_ack_on(struct pmu_softc *sc);
189 static void pmu_ack_off(struct pmu_softc *sc);
190 static int pmu_send(void *cookie, int cmd, int length, uint8_t *in_msg,
191 int rlen, uint8_t *out_msg);
192 static uint8_t pmu_read_reg(struct pmu_softc *sc, u_int offset);
193 static void pmu_write_reg(struct pmu_softc *sc, u_int offset, uint8_t value);
194 static int pmu_intr_state(struct pmu_softc *);
195
196 /* these values shows that number of data returned after 'send' cmd is sent */
197 static signed char pm_send_cmd_type[] = {
198 -1, -1, -1, -1, -1, -1, -1, -1,
199 -1, -1, -1, -1, -1, -1, -1, -1,
200 0x01, 0x01, -1, -1, -1, -1, -1, -1,
201 0x00, 0x00, -1, -1, -1, -1, -1, 0x00,
202 -1, 0x00, 0x02, 0x01, 0x01, -1, -1, -1,
203 0x00, -1, -1, -1, -1, -1, -1, -1,
204 0x04, 0x14, -1, 0x03, -1, -1, -1, -1,
205 0x00, 0x00, 0x02, 0x02, -1, -1, -1, -1,
206 0x01, 0x01, -1, -1, -1, -1, -1, -1,
207 0x00, 0x00, -1, -1, 0x01, -1, -1, -1,
208 0x01, 0x00, 0x02, 0x02, -1, 0x01, 0x03, 0x01,
209 0x00, 0x01, 0x00, 0x00, 0x00, -1, -1, -1,
210 0x02, -1, -1, -1, -1, -1, -1, -1,
211 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -1, -1,
212 0x01, 0x01, 0x01, -1, -1, -1, -1, -1,
213 0x00, 0x00, -1, -1, -1, 0x05, 0x04, 0x04,
214 0x04, -1, 0x00, -1, -1, -1, -1, -1,
215 0x00, -1, -1, -1, -1, -1, -1, -1,
216 0x01, 0x02, -1, -1, -1, -1, -1, -1,
217 0x00, 0x00, -1, -1, -1, -1, -1, -1,
218 0x02, 0x02, 0x02, 0x04, -1, 0x00, -1, -1,
219 0x01, 0x01, 0x03, 0x02, -1, -1, -1, -1,
220 -1, -1, -1, -1, -1, -1, -1, -1,
221 -1, -1, -1, -1, -1, -1, -1, -1,
222 -1, -1, -1, -1, -1, -1, -1, -1,
223 -1, -1, -1, -1, -1, -1, -1, -1,
224 0x00, -1, -1, -1, -1, -1, -1, -1,
225 0x01, 0x01, -1, -1, 0x00, 0x00, -1, -1,
226 -1, 0x04, 0x00, -1, -1, -1, -1, -1,
227 0x03, -1, 0x00, -1, 0x00, -1, -1, 0x00,
228 -1, -1, -1, -1, -1, -1, -1, -1,
229 -1, -1, -1, -1, -1, -1, -1, -1
230 };
231
232 /* these values shows that number of data returned after 'receive' cmd is sent */
233 static signed char pm_receive_cmd_type[] = {
234 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
235 -1, -1, -1, -1, -1, -1, -1, -1,
236 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
237 0x02, 0x02, -1, -1, -1, -1, -1, 0x00,
238 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
239 -1, -1, -1, -1, -1, -1, -1, -1,
240 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
241 0x05, 0x15, -1, 0x02, -1, -1, -1, -1,
242 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
243 0x02, 0x02, -1, -1, -1, -1, -1, -1,
244 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
245 0x02, 0x00, 0x03, 0x03, -1, -1, -1, -1,
246 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
247 0x04, 0x04, 0x03, 0x09, -1, -1, -1, -1,
248 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
249 -1, -1, -1, -1, -1, 0x01, 0x01, 0x01,
250 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
251 0x06, -1, -1, -1, -1, -1, -1, -1,
252 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
253 0x02, 0x02, -1, -1, -1, -1, -1, -1,
254 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
255 0x02, 0x00, 0x00, 0x00, -1, -1, -1, -1,
256 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
257 -1, -1, -1, -1, -1, -1, -1, -1,
258 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
259 -1, -1, -1, -1, -1, -1, -1, -1,
260 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
261 0x02, 0x02, -1, -1, 0x02, -1, -1, -1,
262 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
263 -1, -1, 0x02, -1, -1, -1, -1, 0x00,
264 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
265 -1, -1, -1, -1, -1, -1, -1, -1,
266 };
267
268 static int pmu_battmon_enabled = 1;
269 static struct proc *pmubattproc;
270 static struct kproc_desc pmu_batt_kp = {
271 "pmu_batt",
272 pmu_battquery_proc,
273 &pmubattproc
274 };
275
276 /* We only have one of each device, so globals are safe */
277 static device_t pmu = NULL;
278 static device_t pmu_extint = NULL;
279
280 static int
pmuextint_probe(device_t dev)281 pmuextint_probe(device_t dev)
282 {
283 const char *type = ofw_bus_get_type(dev);
284
285 if (strcmp(type, "extint-gpio1") != 0)
286 return (ENXIO);
287
288 device_set_desc(dev, "Apple PMU99 External Interrupt");
289 return (0);
290 }
291
292 static int
pmu_probe(device_t dev)293 pmu_probe(device_t dev)
294 {
295 const char *type = ofw_bus_get_type(dev);
296
297 if (strcmp(type, "via-pmu") != 0)
298 return (ENXIO);
299
300 device_set_desc(dev, "Apple PMU99 Controller");
301 return (0);
302 }
303
304 static int
setup_pmu_intr(device_t dev,device_t extint)305 setup_pmu_intr(device_t dev, device_t extint)
306 {
307 struct pmu_softc *sc;
308 sc = device_get_softc(dev);
309
310 sc->sc_irqrid = 0;
311 sc->sc_irq = bus_alloc_resource_any(extint, SYS_RES_IRQ, &sc->sc_irqrid,
312 RF_ACTIVE);
313 if (sc->sc_irq == NULL) {
314 device_printf(dev, "could not allocate interrupt\n");
315 return (ENXIO);
316 }
317
318 if (bus_setup_intr(dev, sc->sc_irq, INTR_TYPE_MISC | INTR_MPSAFE
319 | INTR_ENTROPY, NULL, pmu_intr, dev, &sc->sc_ih) != 0) {
320 device_printf(dev, "could not setup interrupt\n");
321 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irqrid,
322 sc->sc_irq);
323 return (ENXIO);
324 }
325
326 return (0);
327 }
328
329 static int
pmuextint_attach(device_t dev)330 pmuextint_attach(device_t dev)
331 {
332 pmu_extint = dev;
333 if (pmu)
334 return (setup_pmu_intr(pmu,dev));
335
336 return (0);
337 }
338
339 static int
pmu_attach(device_t dev)340 pmu_attach(device_t dev)
341 {
342 struct pmu_softc *sc;
343
344 int i;
345 uint8_t reg;
346 uint8_t cmd[2] = {2, 0};
347 uint8_t resp[16];
348 phandle_t node,child;
349 struct sysctl_ctx_list *ctx;
350 struct sysctl_oid *tree;
351
352 sc = device_get_softc(dev);
353 sc->sc_dev = dev;
354
355 sc->sc_memrid = 0;
356 sc->sc_memr = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
357 &sc->sc_memrid, RF_ACTIVE);
358
359 mtx_init(&sc->sc_mutex,"pmu",NULL,MTX_DEF | MTX_RECURSE);
360
361 if (sc->sc_memr == NULL) {
362 device_printf(dev, "Could not alloc mem resource!\n");
363 return (ENXIO);
364 }
365
366 /*
367 * Our interrupt is attached to a GPIO pin. Depending on probe order,
368 * we may not have found it yet. If we haven't, it will find us, and
369 * attach our interrupt then.
370 */
371 pmu = dev;
372 if (pmu_extint != NULL) {
373 if (setup_pmu_intr(dev,pmu_extint) != 0)
374 return (ENXIO);
375 }
376
377 sc->sc_autopoll = 0;
378 sc->sc_batteries = 0;
379 sc->adb_bus = NULL;
380 sc->sc_leddev = NULL;
381
382 /* Init PMU */
383
384 pmu_write_reg(sc, vBufB, pmu_read_reg(sc, vBufB) | vPB4);
385 pmu_write_reg(sc, vDirB, (pmu_read_reg(sc, vDirB) | vPB4) & ~vPB3);
386
387 reg = PMU_DEFAULTS;
388 pmu_send(sc, PMU_SET_IMASK, 1, ®, 16, resp);
389
390 pmu_write_reg(sc, vIER, 0x94); /* make sure VIA interrupts are on */
391
392 pmu_send(sc, PMU_SYSTEM_READY, 1, cmd, 16, resp);
393 pmu_send(sc, PMU_GET_VERSION, 0, cmd, 16, resp);
394
395 /* Initialize child buses (ADB) */
396 node = ofw_bus_get_node(dev);
397
398 for (child = OF_child(node); child != 0; child = OF_peer(child)) {
399 char name[32];
400
401 memset(name, 0, sizeof(name));
402 OF_getprop(child, "name", name, sizeof(name));
403
404 if (bootverbose)
405 device_printf(dev, "PMU child <%s>\n",name);
406
407 if (strncmp(name, "adb", 4) == 0) {
408 sc->adb_bus = device_add_child(dev,"adb",-1);
409 }
410
411 if (strncmp(name, "power-mgt", 9) == 0) {
412 uint32_t prim_info[9];
413
414 if (OF_getprop(child, "prim-info", prim_info,
415 sizeof(prim_info)) >= 7)
416 sc->sc_batteries = (prim_info[6] >> 16) & 0xff;
417
418 if (bootverbose && sc->sc_batteries > 0)
419 device_printf(dev, "%d batteries detected\n",
420 sc->sc_batteries);
421 }
422 }
423
424 /*
425 * Set up sysctls
426 */
427
428 ctx = device_get_sysctl_ctx(dev);
429 tree = device_get_sysctl_tree(dev);
430
431 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
432 "server_mode", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
433 pmu_server_mode, "I", "Enable reboot after power failure");
434
435 if (sc->sc_batteries > 0) {
436 struct sysctl_oid *oid, *battroot;
437 char battnum[2];
438
439 /* Only start the battery monitor if we have a battery. */
440 kproc_start(&pmu_batt_kp);
441 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
442 "monitor_batteries",
443 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, sc, 0,
444 pmu_battmon, "I", "Post battery events to devd");
445
446 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
447 "acline", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
448 0, pmu_acline_state, "I", "AC Line Status");
449
450 battroot = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
451 "batteries", CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
452 "Battery Information");
453
454 for (i = 0; i < sc->sc_batteries; i++) {
455 battnum[0] = i + '0';
456 battnum[1] = '\0';
457
458 oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(battroot),
459 OID_AUTO, battnum, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
460 "Battery Information");
461
462 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
463 "present",
464 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
465 PMU_BATSYSCTL_PRESENT | i, pmu_battquery_sysctl,
466 "I", "Battery present");
467 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
468 "charging",
469 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
470 PMU_BATSYSCTL_CHARGING | i, pmu_battquery_sysctl,
471 "I", "Battery charging");
472 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
473 "charge",
474 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
475 PMU_BATSYSCTL_CHARGE | i, pmu_battquery_sysctl,
476 "I", "Battery charge (mAh)");
477 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
478 "maxcharge",
479 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
480 PMU_BATSYSCTL_MAXCHARGE | i, pmu_battquery_sysctl,
481 "I", "Maximum battery capacity (mAh)");
482 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
483 "rate",
484 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
485 PMU_BATSYSCTL_CURRENT | i, pmu_battquery_sysctl,
486 "I", "Battery discharge rate (mA)");
487 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
488 "voltage",
489 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
490 PMU_BATSYSCTL_VOLTAGE | i, pmu_battquery_sysctl,
491 "I", "Battery voltage (mV)");
492
493 /* Knobs for mental compatibility with ACPI */
494
495 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
496 "time",
497 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
498 PMU_BATSYSCTL_TIME | i, pmu_battquery_sysctl,
499 "I", "Time Remaining (minutes)");
500 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
501 "life",
502 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
503 PMU_BATSYSCTL_LIFE | i, pmu_battquery_sysctl,
504 "I", "Capacity remaining (percent)");
505 }
506 }
507
508 /*
509 * Set up LED interface
510 */
511
512 sc->sc_leddev = led_create(pmu_set_sleepled, sc, "sleepled");
513
514 /*
515 * Register RTC
516 */
517
518 clock_register(dev, 1000);
519
520 /*
521 * Register power control handler
522 */
523 EVENTHANDLER_REGISTER(shutdown_final, pmu_shutdown, sc,
524 SHUTDOWN_PRI_LAST);
525
526 return (bus_generic_attach(dev));
527 }
528
529 static int
pmu_detach(device_t dev)530 pmu_detach(device_t dev)
531 {
532 struct pmu_softc *sc;
533
534 sc = device_get_softc(dev);
535
536 if (sc->sc_leddev != NULL)
537 led_destroy(sc->sc_leddev);
538
539 bus_teardown_intr(dev, sc->sc_irq, sc->sc_ih);
540 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irqrid, sc->sc_irq);
541 bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_memrid, sc->sc_memr);
542 mtx_destroy(&sc->sc_mutex);
543
544 return (bus_generic_detach(dev));
545 }
546
547 static uint8_t
pmu_read_reg(struct pmu_softc * sc,u_int offset)548 pmu_read_reg(struct pmu_softc *sc, u_int offset)
549 {
550 return (bus_read_1(sc->sc_memr, offset));
551 }
552
553 static void
pmu_write_reg(struct pmu_softc * sc,u_int offset,uint8_t value)554 pmu_write_reg(struct pmu_softc *sc, u_int offset, uint8_t value)
555 {
556 bus_write_1(sc->sc_memr, offset, value);
557 }
558
559 static int
pmu_send_byte(struct pmu_softc * sc,uint8_t data)560 pmu_send_byte(struct pmu_softc *sc, uint8_t data)
561 {
562
563 pmu_out(sc);
564 pmu_write_reg(sc, vSR, data);
565 pmu_ack_off(sc);
566 /* wait for intr to come up */
567 /* XXX should add a timeout and bail if it expires */
568 do {} while (pmu_intr_state(sc) == 0);
569 pmu_ack_on(sc);
570 do {} while (pmu_intr_state(sc));
571 pmu_ack_on(sc);
572 return 0;
573 }
574
575 static inline int
pmu_read_byte(struct pmu_softc * sc,uint8_t * data)576 pmu_read_byte(struct pmu_softc *sc, uint8_t *data)
577 {
578 volatile uint8_t scratch;
579 pmu_in(sc);
580 scratch = pmu_read_reg(sc, vSR);
581 pmu_ack_off(sc);
582 /* wait for intr to come up */
583 do {} while (pmu_intr_state(sc) == 0);
584 pmu_ack_on(sc);
585 do {} while (pmu_intr_state(sc));
586 *data = pmu_read_reg(sc, vSR);
587 return 0;
588 }
589
590 static int
pmu_intr_state(struct pmu_softc * sc)591 pmu_intr_state(struct pmu_softc *sc)
592 {
593 return ((pmu_read_reg(sc, vBufB) & vPB3) == 0);
594 }
595
596 static int
pmu_send(void * cookie,int cmd,int length,uint8_t * in_msg,int rlen,uint8_t * out_msg)597 pmu_send(void *cookie, int cmd, int length, uint8_t *in_msg, int rlen,
598 uint8_t *out_msg)
599 {
600 struct pmu_softc *sc = cookie;
601 int i, rcv_len = -1;
602 uint8_t out_len, intreg;
603
604 intreg = pmu_read_reg(sc, vIER);
605 intreg &= 0x10;
606 pmu_write_reg(sc, vIER, intreg);
607
608 /* wait idle */
609 do {} while (pmu_intr_state(sc));
610
611 /* send command */
612 pmu_send_byte(sc, cmd);
613
614 /* send length if necessary */
615 if (pm_send_cmd_type[cmd] < 0) {
616 pmu_send_byte(sc, length);
617 }
618
619 for (i = 0; i < length; i++) {
620 pmu_send_byte(sc, in_msg[i]);
621 }
622
623 /* see if there's data to read */
624 rcv_len = pm_receive_cmd_type[cmd];
625 if (rcv_len == 0)
626 goto done;
627
628 /* read command */
629 if (rcv_len == 1) {
630 pmu_read_byte(sc, out_msg);
631 goto done;
632 } else
633 out_msg[0] = cmd;
634 if (rcv_len < 0) {
635 pmu_read_byte(sc, &out_len);
636 rcv_len = out_len + 1;
637 }
638 for (i = 1; i < min(rcv_len, rlen); i++)
639 pmu_read_byte(sc, &out_msg[i]);
640
641 done:
642 pmu_write_reg(sc, vIER, (intreg == 0) ? 0 : 0x90);
643
644 return rcv_len;
645 }
646
647 static u_int
pmu_poll(device_t dev)648 pmu_poll(device_t dev)
649 {
650 pmu_intr(dev);
651 return (0);
652 }
653
654 static void
pmu_in(struct pmu_softc * sc)655 pmu_in(struct pmu_softc *sc)
656 {
657 uint8_t reg;
658
659 reg = pmu_read_reg(sc, vACR);
660 reg &= ~vSR_OUT;
661 reg |= 0x0c;
662 pmu_write_reg(sc, vACR, reg);
663 }
664
665 static void
pmu_out(struct pmu_softc * sc)666 pmu_out(struct pmu_softc *sc)
667 {
668 uint8_t reg;
669
670 reg = pmu_read_reg(sc, vACR);
671 reg |= vSR_OUT;
672 reg |= 0x0c;
673 pmu_write_reg(sc, vACR, reg);
674 }
675
676 static void
pmu_ack_off(struct pmu_softc * sc)677 pmu_ack_off(struct pmu_softc *sc)
678 {
679 uint8_t reg;
680
681 reg = pmu_read_reg(sc, vBufB);
682 reg &= ~vPB4;
683 pmu_write_reg(sc, vBufB, reg);
684 }
685
686 static void
pmu_ack_on(struct pmu_softc * sc)687 pmu_ack_on(struct pmu_softc *sc)
688 {
689 uint8_t reg;
690
691 reg = pmu_read_reg(sc, vBufB);
692 reg |= vPB4;
693 pmu_write_reg(sc, vBufB, reg);
694 }
695
696 static void
pmu_intr(void * arg)697 pmu_intr(void *arg)
698 {
699 device_t dev;
700 struct pmu_softc *sc;
701
702 unsigned int len;
703 uint8_t resp[16];
704 uint8_t junk[16];
705
706 dev = (device_t)arg;
707 sc = device_get_softc(dev);
708
709 mtx_lock(&sc->sc_mutex);
710
711 pmu_write_reg(sc, vIFR, 0x90); /* Clear 'em */
712 len = pmu_send(sc, PMU_INT_ACK, 0, NULL, 16, resp);
713
714 mtx_unlock(&sc->sc_mutex);
715
716 if ((len < 1) || (resp[1] == 0)) {
717 return;
718 }
719
720 if (resp[1] & PMU_INT_ADB) {
721 /*
722 * the PMU will turn off autopolling after each command that
723 * it did not issue, so we assume any but TALK R0 is ours and
724 * re-enable autopoll here whenever we receive an ACK for a
725 * non TR0 command.
726 */
727 mtx_lock(&sc->sc_mutex);
728
729 if ((resp[2] & 0x0f) != (ADB_COMMAND_TALK << 2)) {
730 if (sc->sc_autopoll) {
731 uint8_t cmd[] = {0, PMU_SET_POLL_MASK,
732 (sc->sc_autopoll >> 8) & 0xff,
733 sc->sc_autopoll & 0xff};
734
735 pmu_send(sc, PMU_ADB_CMD, 4, cmd, 16, junk);
736 }
737 }
738
739 mtx_unlock(&sc->sc_mutex);
740
741 adb_receive_raw_packet(sc->adb_bus,resp[1],resp[2],
742 len - 3,&resp[3]);
743 }
744 if (resp[1] & PMU_INT_ENVIRONMENT) {
745 /* if the lid was just closed, notify devd. */
746 if ((resp[2] & PMU_ENV_LID_CLOSED) && (!sc->lid_closed)) {
747 sc->lid_closed = 1;
748 devctl_notify("PMU", "lid", "close", NULL);
749 }
750 else if (!(resp[2] & PMU_ENV_LID_CLOSED) && (sc->lid_closed)) {
751 /* if the lid was just opened, notify devd. */
752 sc->lid_closed = 0;
753 devctl_notify("PMU", "lid", "open", NULL);
754 }
755 if (resp[2] & PMU_ENV_POWER)
756 devctl_notify("PMU", "Button", "pressed", NULL);
757 }
758 }
759
760 static u_int
pmu_adb_send(device_t dev,u_char command_byte,int len,u_char * data,u_char poll)761 pmu_adb_send(device_t dev, u_char command_byte, int len, u_char *data,
762 u_char poll)
763 {
764 struct pmu_softc *sc = device_get_softc(dev);
765 int i,replen;
766 uint8_t packet[16], resp[16];
767
768 /* construct an ADB command packet and send it */
769
770 packet[0] = command_byte;
771
772 packet[1] = 0;
773 packet[2] = len;
774 for (i = 0; i < len; i++)
775 packet[i + 3] = data[i];
776
777 mtx_lock(&sc->sc_mutex);
778 replen = pmu_send(sc, PMU_ADB_CMD, len + 3, packet, 16, resp);
779 mtx_unlock(&sc->sc_mutex);
780
781 if (poll)
782 pmu_poll(dev);
783
784 return 0;
785 }
786
787 static u_int
pmu_adb_autopoll(device_t dev,uint16_t mask)788 pmu_adb_autopoll(device_t dev, uint16_t mask)
789 {
790 struct pmu_softc *sc = device_get_softc(dev);
791
792 /* magical incantation to re-enable autopolling */
793 uint8_t cmd[] = {0, PMU_SET_POLL_MASK, (mask >> 8) & 0xff, mask & 0xff};
794 uint8_t resp[16];
795
796 mtx_lock(&sc->sc_mutex);
797
798 if (sc->sc_autopoll == mask) {
799 mtx_unlock(&sc->sc_mutex);
800 return 0;
801 }
802
803 sc->sc_autopoll = mask & 0xffff;
804
805 if (mask)
806 pmu_send(sc, PMU_ADB_CMD, 4, cmd, 16, resp);
807 else
808 pmu_send(sc, PMU_ADB_POLL_OFF, 0, NULL, 16, resp);
809
810 mtx_unlock(&sc->sc_mutex);
811
812 return 0;
813 }
814
815 static void
pmu_shutdown(void * xsc,int howto)816 pmu_shutdown(void *xsc, int howto)
817 {
818 struct pmu_softc *sc = xsc;
819 uint8_t cmd[] = {'M', 'A', 'T', 'T'};
820
821 if (howto & RB_HALT)
822 pmu_send(sc, PMU_POWER_OFF, 4, cmd, 0, NULL);
823 else
824 pmu_send(sc, PMU_RESET_CPU, 0, NULL, 0, NULL);
825
826 for (;;);
827 }
828
829 static void
pmu_set_sleepled(void * xsc,int onoff)830 pmu_set_sleepled(void *xsc, int onoff)
831 {
832 struct pmu_softc *sc = xsc;
833 uint8_t cmd[] = {4, 0, 0};
834
835 cmd[2] = onoff;
836
837 mtx_lock(&sc->sc_mutex);
838 pmu_send(sc, PMU_SET_SLEEPLED, 3, cmd, 0, NULL);
839 mtx_unlock(&sc->sc_mutex);
840 }
841
842 static int
pmu_server_mode(SYSCTL_HANDLER_ARGS)843 pmu_server_mode(SYSCTL_HANDLER_ARGS)
844 {
845 struct pmu_softc *sc = arg1;
846
847 u_int server_mode = 0;
848 uint8_t getcmd[] = {PMU_PWR_GET_POWERUP_EVENTS};
849 uint8_t setcmd[] = {0, 0, PMU_PWR_WAKEUP_AC_INSERT};
850 uint8_t resp[3];
851 int error, len;
852
853 mtx_lock(&sc->sc_mutex);
854 len = pmu_send(sc, PMU_POWER_EVENTS, 1, getcmd, 3, resp);
855 mtx_unlock(&sc->sc_mutex);
856
857 if (len == 3)
858 server_mode = (resp[2] & PMU_PWR_WAKEUP_AC_INSERT) ? 1 : 0;
859
860 error = sysctl_handle_int(oidp, &server_mode, 0, req);
861
862 if (len != 3)
863 return (EINVAL);
864
865 if (error || !req->newptr)
866 return (error);
867
868 if (server_mode == 1)
869 setcmd[0] = PMU_PWR_SET_POWERUP_EVENTS;
870 else if (server_mode == 0)
871 setcmd[0] = PMU_PWR_CLR_POWERUP_EVENTS;
872 else
873 return (EINVAL);
874
875 setcmd[1] = resp[1];
876
877 mtx_lock(&sc->sc_mutex);
878 pmu_send(sc, PMU_POWER_EVENTS, 3, setcmd, 2, resp);
879 mtx_unlock(&sc->sc_mutex);
880
881 return (0);
882 }
883
884 static int
pmu_query_battery(struct pmu_softc * sc,int batt,struct pmu_battstate * info)885 pmu_query_battery(struct pmu_softc *sc, int batt, struct pmu_battstate *info)
886 {
887 uint8_t reg;
888 uint8_t resp[16];
889 int len;
890
891 reg = batt + 1;
892
893 mtx_lock(&sc->sc_mutex);
894 len = pmu_send(sc, PMU_SMART_BATTERY_STATE, 1, ®, 16, resp);
895 mtx_unlock(&sc->sc_mutex);
896
897 if (len < 3)
898 return (-1);
899
900 /* All PMU battery info replies share a common header:
901 * Byte 1 Payload Format
902 * Byte 2 Battery Flags
903 */
904
905 info->state = resp[2];
906
907 switch (resp[1]) {
908 case 3:
909 case 4:
910 /*
911 * Formats 3 and 4 appear to be the same:
912 * Byte 3 Charge
913 * Byte 4 Max Charge
914 * Byte 5 Current
915 * Byte 6 Voltage
916 */
917
918 info->charge = resp[3];
919 info->maxcharge = resp[4];
920 /* Current can be positive or negative */
921 info->current = (int8_t)resp[5];
922 info->voltage = resp[6];
923 break;
924 case 5:
925 /*
926 * Formats 5 is a wider version of formats 3 and 4
927 * Byte 3-4 Charge
928 * Byte 5-6 Max Charge
929 * Byte 7-8 Current
930 * Byte 9-10 Voltage
931 */
932
933 info->charge = (resp[3] << 8) | resp[4];
934 info->maxcharge = (resp[5] << 8) | resp[6];
935 /* Current can be positive or negative */
936 info->current = (int16_t)((resp[7] << 8) | resp[8]);
937 info->voltage = (resp[9] << 8) | resp[10];
938 break;
939 default:
940 device_printf(sc->sc_dev, "Unknown battery info format (%d)!\n",
941 resp[1]);
942 return (-1);
943 }
944
945 return (0);
946 }
947
948 static void
pmu_battery_notify(struct pmu_battstate * batt,struct pmu_battstate * old)949 pmu_battery_notify(struct pmu_battstate *batt, struct pmu_battstate *old)
950 {
951 char notify_buf[16];
952 int new_acline, old_acline;
953
954 new_acline = (batt->state & PMU_PWR_AC_PRESENT) ? 1 : 0;
955 old_acline = (old->state & PMU_PWR_AC_PRESENT) ? 1 : 0;
956
957 if (new_acline != old_acline) {
958 snprintf(notify_buf, sizeof(notify_buf),
959 "notify=0x%02x", new_acline);
960 devctl_notify("PMU", "POWER", "ACLINE", notify_buf);
961 }
962 }
963
964 static void
pmu_battquery_proc(void)965 pmu_battquery_proc(void)
966 {
967 struct pmu_softc *sc;
968 struct pmu_battstate batt;
969 struct pmu_battstate cur_batt;
970 int error;
971
972 sc = device_get_softc(pmu);
973
974 bzero(&cur_batt, sizeof(cur_batt));
975 while (1) {
976 kproc_suspend_check(curproc);
977 error = pmu_query_battery(sc, 0, &batt);
978 if (error == 0) {
979 pmu_battery_notify(&batt, &cur_batt);
980 cur_batt = batt;
981 }
982 pause("pmu_batt", hz);
983 }
984 }
985
986 static int
pmu_battmon(SYSCTL_HANDLER_ARGS)987 pmu_battmon(SYSCTL_HANDLER_ARGS)
988 {
989 struct pmu_softc *sc;
990 int error, result;
991
992 sc = arg1;
993 result = pmu_battmon_enabled;
994
995 error = sysctl_handle_int(oidp, &result, 0, req);
996
997 if (error || !req->newptr)
998 return (error);
999
1000 if (!result && pmu_battmon_enabled)
1001 error = kproc_suspend(pmubattproc, hz);
1002 else if (result && pmu_battmon_enabled == 0)
1003 error = kproc_resume(pmubattproc);
1004 pmu_battmon_enabled = (result != 0);
1005
1006 return (error);
1007 }
1008
1009 static int
pmu_acline_state(SYSCTL_HANDLER_ARGS)1010 pmu_acline_state(SYSCTL_HANDLER_ARGS)
1011 {
1012 struct pmu_softc *sc;
1013 struct pmu_battstate batt;
1014 int error, result;
1015
1016 sc = arg1;
1017
1018 /* The PMU treats the AC line status as a property of the battery */
1019 error = pmu_query_battery(sc, 0, &batt);
1020
1021 if (error != 0)
1022 return (error);
1023
1024 result = (batt.state & PMU_PWR_AC_PRESENT) ? 1 : 0;
1025 error = sysctl_handle_int(oidp, &result, 0, req);
1026
1027 return (error);
1028 }
1029
1030 static int
pmu_battquery_sysctl(SYSCTL_HANDLER_ARGS)1031 pmu_battquery_sysctl(SYSCTL_HANDLER_ARGS)
1032 {
1033 struct pmu_softc *sc;
1034 struct pmu_battstate batt;
1035 int error, result;
1036
1037 sc = arg1;
1038
1039 error = pmu_query_battery(sc, arg2 & 0x00ff, &batt);
1040
1041 if (error != 0)
1042 return (error);
1043
1044 switch (arg2 & 0xff00) {
1045 case PMU_BATSYSCTL_PRESENT:
1046 result = (batt.state & PMU_PWR_BATT_PRESENT) ? 1 : 0;
1047 break;
1048 case PMU_BATSYSCTL_CHARGING:
1049 result = (batt.state & PMU_PWR_BATT_CHARGING) ? 1 : 0;
1050 break;
1051 case PMU_BATSYSCTL_CHARGE:
1052 result = batt.charge;
1053 break;
1054 case PMU_BATSYSCTL_MAXCHARGE:
1055 result = batt.maxcharge;
1056 break;
1057 case PMU_BATSYSCTL_CURRENT:
1058 result = batt.current;
1059 break;
1060 case PMU_BATSYSCTL_VOLTAGE:
1061 result = batt.voltage;
1062 break;
1063 case PMU_BATSYSCTL_TIME:
1064 /* Time remaining until full charge/discharge, in minutes */
1065
1066 if (batt.current >= 0)
1067 result = (batt.maxcharge - batt.charge) /* mAh */ * 60
1068 / batt.current /* mA */;
1069 else
1070 result = (batt.charge /* mAh */ * 60)
1071 / (-batt.current /* mA */);
1072 break;
1073 case PMU_BATSYSCTL_LIFE:
1074 /* Battery charge fraction, in percent */
1075 result = (batt.charge * 100) / batt.maxcharge;
1076 break;
1077 default:
1078 /* This should never happen */
1079 result = -1;
1080 }
1081
1082 error = sysctl_handle_int(oidp, &result, 0, req);
1083
1084 return (error);
1085 }
1086
1087 #define DIFF19041970 2082844800
1088
1089 static int
pmu_gettime(device_t dev,struct timespec * ts)1090 pmu_gettime(device_t dev, struct timespec *ts)
1091 {
1092 struct pmu_softc *sc = device_get_softc(dev);
1093 uint8_t resp[16];
1094 uint32_t sec;
1095
1096 mtx_lock(&sc->sc_mutex);
1097 pmu_send(sc, PMU_READ_RTC, 0, NULL, 16, resp);
1098 mtx_unlock(&sc->sc_mutex);
1099
1100 memcpy(&sec, &resp[1], 4);
1101 ts->tv_sec = sec - DIFF19041970;
1102 ts->tv_nsec = 0;
1103
1104 return (0);
1105 }
1106
1107 static int
pmu_settime(device_t dev,struct timespec * ts)1108 pmu_settime(device_t dev, struct timespec *ts)
1109 {
1110 struct pmu_softc *sc = device_get_softc(dev);
1111 uint32_t sec;
1112
1113 sec = ts->tv_sec + DIFF19041970;
1114
1115 mtx_lock(&sc->sc_mutex);
1116 pmu_send(sc, PMU_SET_RTC, sizeof(sec), (uint8_t *)&sec, 0, NULL);
1117 mtx_unlock(&sc->sc_mutex);
1118
1119 return (0);
1120 }
1121
1122 int
pmu_set_speed(int low_speed)1123 pmu_set_speed(int low_speed)
1124 {
1125 struct pmu_softc *sc;
1126 uint8_t sleepcmd[] = {'W', 'O', 'O', 'F', 0};
1127 uint8_t resp[16];
1128
1129 sc = device_get_softc(pmu);
1130 pmu_write_reg(sc, vIER, 0x10);
1131 spinlock_enter();
1132 mtdec(0x7fffffff);
1133 mb();
1134 mtdec(0x7fffffff);
1135
1136 sleepcmd[4] = low_speed;
1137 pmu_send(sc, PMU_CPU_SPEED, 5, sleepcmd, 16, resp);
1138 unin_chip_sleep(NULL, 1);
1139 platform_sleep();
1140 unin_chip_wake(NULL);
1141
1142 mtdec(1); /* Force a decrementer exception */
1143 spinlock_exit();
1144 pmu_write_reg(sc, vIER, 0x90);
1145
1146 return (0);
1147 }
1148