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