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