pmu.c revision 1.38 1 /* $NetBSD: pmu.c,v 1.38 2021/08/07 06:04:26 macallan Exp $ */
2
3 /*-
4 * Copyright (c) 2006 Michael Lorenz
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: pmu.c,v 1.38 2021/08/07 06:04:26 macallan Exp $");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/device.h>
36 #include <sys/proc.h>
37 #include <sys/kthread.h>
38 #include <sys/atomic.h>
39 #include <sys/mutex.h>
40
41 #include <sys/bus.h>
42 #include <machine/pio.h>
43 #include <machine/autoconf.h>
44 #include <dev/clock_subr.h>
45 #include <dev/i2c/i2cvar.h>
46
47 #include <dev/sysmon/sysmonvar.h>
48
49 #include <macppc/dev/viareg.h>
50 #include <macppc/dev/pmuvar.h>
51 #include <macppc/dev/batteryvar.h>
52
53 #include <dev/ofw/openfirm.h>
54 #include <dev/adb/adbvar.h>
55 #include "opt_pmu.h"
56 #include "nadb.h"
57
58 #ifdef PMU_DEBUG
59 #define DPRINTF printf
60 #else
61 #define DPRINTF while (0) printf
62 #endif
63
64 #define PMU_NOTREADY 0x1 /* has not been initialized yet */
65 #define PMU_IDLE 0x2 /* the bus is currently idle */
66 #define PMU_OUT 0x3 /* sending out a command */
67 #define PMU_IN 0x4 /* receiving data */
68
69 static void pmu_attach(device_t, device_t, void *);
70 static int pmu_match(device_t, cfdata_t, void *);
71 static void pmu_autopoll(void *, int);
72
73 static int pmu_intr(void *);
74
75
76 /* bits for sc_pending, as signals to the event thread */
77 #define PMU_EV_CARD0 1
78 #define PMU_EV_CARD1 2
79 #define PMU_EV_BUTTON 4
80 #define PMU_EV_LID 8
81
82 struct pmu_softc {
83 device_t sc_dev;
84 void *sc_ih;
85 struct todr_chip_handle sc_todr;
86 struct adb_bus_accessops sc_adbops;
87 struct i2c_controller sc_i2c;
88 struct pmu_ops sc_pmu_ops;
89 struct sysmon_pswitch sc_lidswitch;
90 struct sysmon_pswitch sc_powerbutton;
91 bus_space_tag_t sc_memt;
92 bus_space_handle_t sc_memh;
93 uint32_t sc_flags;
94 #define PMU_HAS_BACKLIGHT_CONTROL 1
95 int sc_node;
96 int sc_error;
97 int sc_autopoll;
98 int sc_brightness, sc_brightness_wanted;
99 int sc_volume, sc_volume_wanted;
100 int sc_lid_closed;
101 int sc_button;
102 uint8_t sc_env_old;
103 uint8_t sc_env_mask;
104 /* deferred processing */
105 lwp_t *sc_thread;
106 int sc_pending;
107 /* signalling the event thread */
108 int sc_event;
109 /* ADB */
110 void (*sc_adb_handler)(void *, int, uint8_t *);
111 void *sc_adb_cookie;
112 void (*sc_callback)(void *);
113 void *sc_cb_cookie;
114 };
115
116 CFATTACH_DECL_NEW(pmu, sizeof(struct pmu_softc),
117 pmu_match, pmu_attach, NULL, NULL);
118
119 static inline void pmu_write_reg(struct pmu_softc *, int, uint8_t);
120 static inline uint8_t pmu_read_reg(struct pmu_softc *, int);
121 static void pmu_in(struct pmu_softc *);
122 static void pmu_out(struct pmu_softc *);
123 static void pmu_ack_off(struct pmu_softc *);
124 static void pmu_ack_on(struct pmu_softc *);
125 static int pmu_intr_state(struct pmu_softc *);
126
127 static void pmu_init(struct pmu_softc *);
128 static void pmu_thread(void *);
129 static void pmu_eject_card(struct pmu_softc *, int);
130 static void pmu_update_brightness(struct pmu_softc *);
131 static void pmu_register_callback(void *, void (*)(void *), void *);
132 /*
133 * send a message to the PMU.
134 */
135 static int pmu_send(void *, int, int, uint8_t *, int, uint8_t *);
136 static void pmu_adb_poll(void *);
137 static int pmu_todr_set(todr_chip_handle_t, struct timeval *);
138 static int pmu_todr_get(todr_chip_handle_t, struct timeval *);
139
140 static int pmu_adb_handler(void *, int, uint8_t *);
141
142 static struct pmu_softc *pmu0 = NULL;
143
144 /* ADB bus attachment stuff */
145 static int pmu_adb_send(void *, int, int, int, uint8_t *);
146 static int pmu_adb_set_handler(void *, void (*)(void *, int, uint8_t *), void *);
147
148 /* i2c stuff */
149 static int pmu_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *, size_t,
150 void *, size_t, int);
151
152 static void pmu_attach_legacy_battery(struct pmu_softc *);
153 static void pmu_attach_smart_battery(struct pmu_softc *, int);
154 static int pmu_print(void *, const char *);
155
156 /* these values shows that number of data returned after 'send' cmd is sent */
157 static signed char pm_send_cmd_type[] = {
158 -1, -1, -1, -1, -1, -1, -1, -1,
159 -1, -1, -1, -1, -1, -1, -1, -1,
160 0x01, 0x01, -1, -1, -1, -1, -1, -1,
161 0x00, 0x00, -1, -1, -1, -1, -1, 0x00,
162 -1, 0x00, 0x02, 0x01, 0x01, -1, -1, -1,
163 0x00, -1, -1, -1, -1, -1, -1, -1,
164 0x04, 0x14, -1, 0x03, -1, -1, -1, -1,
165 0x00, 0x00, 0x02, 0x02, -1, -1, -1, -1,
166 0x01, 0x01, -1, -1, -1, -1, -1, -1,
167 0x00, 0x00, -1, -1, 0x01, -1, -1, -1,
168 0x01, 0x00, 0x02, 0x02, -1, 0x01, 0x03, 0x01,
169 0x00, 0x01, 0x00, 0x00, 0x00, -1, -1, -1,
170 0x02, -1, -1, -1, -1, -1, -1, -1,
171 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -1, -1,
172 0x01, 0x01, 0x01, -1, -1, -1, -1, -1,
173 0x00, 0x00, -1, -1, -1, -1, 0x04, 0x04,
174 0x04, -1, 0x00, -1, -1, -1, -1, -1,
175 0x00, -1, -1, -1, -1, -1, -1, -1,
176 0x01, 0x02, -1, -1, -1, -1, -1, -1,
177 0x00, 0x00, -1, -1, -1, -1, -1, -1,
178 0x02, 0x02, 0x02, 0x04, -1, 0x00, -1, -1,
179 0x01, 0x01, 0x03, 0x02, -1, -1, -1, -1,
180 -1, -1, -1, -1, -1, -1, -1, -1,
181 -1, -1, -1, -1, -1, -1, -1, -1,
182 -1, -1, -1, -1, -1, -1, -1, -1,
183 -1, -1, -1, -1, -1, -1, -1, -1,
184 0x00, -1, -1, -1, -1, -1, -1, -1,
185 0x01, 0x01, -1, -1, 0x00, 0x00, -1, -1,
186 -1, 0x04, 0x00, -1, -1, -1, -1, -1,
187 0x03, -1, 0x00, -1, 0x00, -1, -1, 0x00,
188 -1, -1, -1, -1, -1, -1, -1, -1,
189 -1, -1, -1, -1, -1, -1, -1, -1
190 };
191
192 /* these values shows that number of data returned after 'receive' cmd is sent */
193 static signed char pm_receive_cmd_type[] = {
194 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
195 -1, -1, -1, -1, -1, -1, -1, -1,
196 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
197 0x02, 0x02, -1, -1, -1, -1, -1, 0x00,
198 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
199 -1, -1, -1, -1, -1, -1, -1, -1,
200 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
201 0x05, 0x15, -1, 0x02, -1, -1, -1, -1,
202 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
203 0x02, 0x02, -1, -1, -1, -1, -1, -1,
204 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
205 0x02, 0x00, 0x03, 0x03, -1, -1, -1, -1,
206 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
207 0x04, 0x04, 0x03, 0x09, -1, -1, -1, -1,
208 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
209 -1, -1, -1, -1, -1, -1, 0x01, 0x01,
210 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
211 0x06, -1, -1, -1, -1, -1, -1, -1,
212 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
213 0x02, 0x02, -1, -1, -1, -1, -1, -1,
214 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
215 0x02, 0x00, 0x00, 0x00, -1, -1, -1, -1,
216 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
217 -1, -1, -1, -1, -1, -1, -1, -1,
218 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
219 -1, -1, -1, -1, -1, -1, -1, -1,
220 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
221 0x02, 0x02, -1, -1, 0x02, -1, -1, -1,
222 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
223 -1, -1, 0x02, -1, -1, -1, -1, 0x00,
224 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
225 -1, -1, -1, -1, -1, -1, -1, -1,
226 };
227
228 static const char *has_legacy_battery[] = {
229 "AAPL,3500",
230 "AAPL,3400/2400",
231 NULL };
232
233 static const char *has_two_smart_batteries[] = {
234 "AAPL,PowerBook1998",
235 "PowerBook1,1",
236 NULL };
237
238 static int
239 pmu_match(device_t parent, cfdata_t cf, void *aux)
240 {
241 struct confargs *ca = aux;
242
243 if (ca->ca_nreg < 8)
244 return 0;
245
246 if (ca->ca_nintr < 4)
247 return 0;
248
249 if (strcmp(ca->ca_name, "via-pmu") == 0) {
250 return 10;
251 }
252
253 return 0;
254 }
255
256 static void
257 pmu_attach(device_t parent, device_t self, void *aux)
258 {
259 struct confargs *ca = aux;
260 struct pmu_softc *sc = device_private(self);
261 struct i2cbus_attach_args iba;
262 uint32_t regs[16];
263 int irq = ca->ca_intr[0];
264 int node, extint_node, root_node;
265 int nbat = 1, i, pmnode;
266 int type = IST_EDGE;
267 uint8_t cmd[2] = {2, 0};
268 uint8_t resp[16];
269 char name[256], model[32];
270 prop_dictionary_t dict = device_properties(self);
271
272 extint_node = of_getnode_byname(OF_parent(ca->ca_node), "extint-gpio1");
273 if (extint_node) {
274
275 OF_getprop(extint_node, "interrupts", &irq, 4);
276 type = IST_LEVEL;
277 }
278
279 aprint_normal(" irq %d: ", irq);
280
281 sc->sc_dev = self;
282 sc->sc_node = ca->ca_node;
283 sc->sc_memt = ca->ca_tag;
284
285 root_node = OF_finddevice("/");
286
287 sc->sc_error = 0;
288 sc->sc_autopoll = 0;
289 sc->sc_pending = 0;
290 sc->sc_env_old = 0;
291 sc->sc_brightness = sc->sc_brightness_wanted = 0x80;
292 sc->sc_volume = sc->sc_volume_wanted = 0x80;
293 sc->sc_flags = 0;
294 sc->sc_callback = NULL;
295 sc->sc_lid_closed = 0;
296 sc->sc_button = 0;
297 sc->sc_env_mask = 0xff;
298
299 /*
300 * core99 PowerMacs like to send environment messages with the lid
301 * switch bit set - since that doesn't make any sense here and it
302 * probably means something else anyway we mask it out
303 */
304
305 if (OF_getprop(root_node, "model", model, 32) != 0) {
306 if (strncmp(model, "PowerMac", 8) == 0) {
307 sc->sc_env_mask = PMU_ENV_POWER_BUTTON;
308 }
309 }
310
311 if (bus_space_map(sc->sc_memt, ca->ca_reg[0] + ca->ca_baseaddr,
312 ca->ca_reg[1], 0, &sc->sc_memh) != 0) {
313 aprint_error_dev(self, "unable to map registers\n");
314 return;
315 }
316 sc->sc_ih = intr_establish_xname(irq, type, IPL_TTY, pmu_intr, sc,
317 device_xname(self));
318
319 pmu_init(sc);
320
321 sc->sc_pmu_ops.cookie = sc;
322 sc->sc_pmu_ops.do_command = pmu_send;
323 sc->sc_pmu_ops.register_callback = pmu_register_callback;
324
325 if (pmu0 == NULL)
326 pmu0 = sc;
327
328 pmu_send(sc, PMU_SYSTEM_READY, 1, cmd, 16, resp);
329
330 /* check what kind of PMU we're talking to */
331 if (pmu_send(sc, PMU_GET_VERSION, 0, cmd, 16, resp) > 1)
332 aprint_normal(" rev. %d", resp[1]);
333 aprint_normal("\n");
334
335 node = OF_child(sc->sc_node);
336
337 while (node != 0) {
338
339 if (OF_getprop(node, "name", name, 256) <= 0)
340 goto next;
341
342 if (strncmp(name, "pmu-i2c", 8) == 0) {
343 int devs, sensors;
344 uint32_t addr;
345 char compat[256];
346 prop_array_t cfg;
347 prop_dictionary_t dev;
348 prop_data_t data;
349
350 aprint_normal_dev(self, "initializing IIC bus\n");
351
352 cfg = prop_array_create();
353 prop_dictionary_set(dict, "i2c-child-devices", cfg);
354 prop_object_release(cfg);
355
356 /* look for i2c devices */
357 devs = OF_child(node);
358 while (devs != 0) {
359 if (OF_getprop(devs, "name", name, 256) <= 0)
360 goto skip;
361 if (OF_getprop(devs, "compatible",
362 compat, 256) <= 0)
363 goto skip;
364 if (OF_getprop(devs, "reg", &addr, 4) <= 0)
365 goto skip;
366 addr = (addr & 0xff) >> 1;
367 DPRINTF("-> %s@%x\n", name, addr);
368 dev = prop_dictionary_create();
369 prop_dictionary_set_string(dev, "name", name);
370 data = prop_data_create_copy(compat, strlen(compat)+1);
371 prop_dictionary_set(dev, "compatible", data);
372 prop_object_release(data);
373 prop_dictionary_set_uint32(dev, "addr", addr);
374 prop_dictionary_set_uint64(dev, "cookie", devs);
375 sensors = OF_child(devs);
376 while (sensors != 0) {
377 int reg;
378 char loc[64];
379 char pname[8];
380 if (OF_getprop(sensors, "reg", ®, 4) != 4)
381 goto nope;
382 if (OF_getprop(sensors, "location", loc, 63) <= 0)
383 goto nope;
384 snprintf(pname, 7, "s%02x", reg);
385 prop_dictionary_set_string(dev, pname, loc);
386 nope:
387 sensors = OF_peer(sensors);
388 }
389 prop_array_add(cfg, dev);
390 prop_object_release(dev);
391 skip:
392 devs = OF_peer(devs);
393 }
394 memset(&iba, 0, sizeof(iba));
395 iba.iba_tag = &sc->sc_i2c;
396 iic_tag_init(&sc->sc_i2c);
397 sc->sc_i2c.ic_cookie = sc;
398 sc->sc_i2c.ic_exec = pmu_i2c_exec;
399 config_found(sc->sc_dev, &iba, iicbus_print,
400 CFARG_IATTR, "i2cbus",
401 CFARG_EOL);
402 goto next;
403 }
404 if (strncmp(name, "adb", 4) == 0) {
405 aprint_normal_dev(self, "initializing ADB\n");
406 sc->sc_adbops.cookie = sc;
407 sc->sc_adbops.send = pmu_adb_send;
408 sc->sc_adbops.poll = pmu_adb_poll;
409 sc->sc_adbops.autopoll = pmu_autopoll;
410 sc->sc_adbops.set_handler = pmu_adb_set_handler;
411 #if NNADB > 0
412 config_found(self, &sc->sc_adbops, nadb_print,
413 CFARG_IATTR, "adb_bus",
414 CFARG_EOL);
415 #endif
416 goto next;
417 }
418 if (strncmp(name, "rtc", 4) == 0) {
419
420 aprint_normal_dev(self, "initializing RTC\n");
421 sc->sc_todr.todr_gettime = pmu_todr_get;
422 sc->sc_todr.todr_settime = pmu_todr_set;
423 sc->sc_todr.cookie = sc;
424 todr_attach(&sc->sc_todr);
425 goto next;
426 }
427 if (strncmp(name, "battery", 8) == 0)
428 goto next;
429
430 aprint_normal_dev(self, "%s not configured\n", name);
431 next:
432 node = OF_peer(node);
433 }
434
435 if (OF_finddevice("/bandit/ohare") != -1) {
436 aprint_normal_dev(self, "enabling ohare backlight control\n");
437 sc->sc_flags |= PMU_HAS_BACKLIGHT_CONTROL;
438 cmd[0] = 0;
439 cmd[1] = 0;
440 memset(resp, 0, 6);
441 if (pmu_send(sc, PMU_READ_BRIGHTNESS, 1, cmd, 16, resp) > 1) {
442 sc->sc_brightness_wanted = resp[1];
443 pmu_update_brightness(sc);
444 }
445 }
446
447 /* attach batteries */
448 if (of_compatible(root_node, has_legacy_battery)) {
449
450 pmu_attach_legacy_battery(sc);
451 } else if (of_compatible(root_node, has_two_smart_batteries)) {
452
453 pmu_attach_smart_battery(sc, 0);
454 pmu_attach_smart_battery(sc, 1);
455 } else {
456
457 /* check how many batteries we have */
458 pmnode = of_getnode_byname(ca->ca_node, "power-mgt");
459 if (pmnode == -1)
460 goto bat_done;
461 if (OF_getprop(pmnode, "prim-info", regs, sizeof(regs)) < 24)
462 goto bat_done;
463 nbat = regs[6] >> 16;
464 for (i = 0; i < nbat; i++)
465 pmu_attach_smart_battery(sc, i);
466 }
467 bat_done:
468
469 if (kthread_create(PRI_NONE, 0, NULL, pmu_thread, sc, &sc->sc_thread,
470 "%s", "pmu") != 0) {
471 aprint_error_dev(self, "unable to create event kthread\n");
472 }
473
474 sc->sc_lidswitch.smpsw_name = "Lid switch";
475 sc->sc_lidswitch.smpsw_type = PSWITCH_TYPE_LID;
476 if (sysmon_pswitch_register(&sc->sc_lidswitch) != 0)
477 aprint_error_dev(self,
478 "unable to register lid switch with sysmon\n");
479
480 sc->sc_powerbutton.smpsw_name = "Power button";
481 sc->sc_powerbutton.smpsw_type = PSWITCH_TYPE_POWER;
482 if (sysmon_pswitch_register(&sc->sc_powerbutton) != 0)
483 aprint_error_dev(self,
484 "unable to register power button with sysmon\n");
485 }
486
487 static void
488 pmu_register_callback(void *pmu_cookie, void (*cb)(void *), void *cookie)
489 {
490 struct pmu_softc *sc = pmu_cookie;
491
492 sc->sc_callback = cb;
493 sc->sc_cb_cookie = cookie;
494 }
495
496 static void
497 pmu_init(struct pmu_softc *sc)
498 {
499 uint8_t pmu_imask, resp[16];
500
501 pmu_imask =
502 PMU_INT_PCEJECT | PMU_INT_SNDBRT | PMU_INT_ADB/* | PMU_INT_TICK*/;
503 pmu_imask |= PMU_INT_BATTERY;
504 pmu_imask |= PMU_INT_ENVIRONMENT;
505 pmu_send(sc, PMU_SET_IMASK, 1, &pmu_imask, 16, resp);
506
507 pmu_write_reg(sc, vIER, 0x90); /* enable VIA interrupts */
508 }
509
510 static inline void
511 pmu_write_reg(struct pmu_softc *sc, int offset, uint8_t value)
512 {
513
514 bus_space_write_1(sc->sc_memt, sc->sc_memh, offset, value);
515 }
516
517 static inline uint8_t
518 pmu_read_reg(struct pmu_softc *sc, int offset)
519 {
520
521 return bus_space_read_1(sc->sc_memt, sc->sc_memh, offset);
522 }
523
524 static inline int
525 pmu_send_byte(struct pmu_softc *sc, uint8_t data)
526 {
527
528 pmu_out(sc);
529 pmu_write_reg(sc, vSR, data);
530 pmu_ack_off(sc);
531 /* wait for intr to come up */
532 /* XXX should add a timeout and bail if it expires */
533 do {} while (pmu_intr_state(sc) == 0);
534 pmu_ack_on(sc);
535 do {} while (pmu_intr_state(sc));
536 pmu_ack_on(sc);
537 DPRINTF(" %02x>", data);
538 return 0;
539 }
540
541 static inline int
542 pmu_read_byte(struct pmu_softc *sc, uint8_t *data)
543 {
544 pmu_in(sc);
545 (void)pmu_read_reg(sc, vSR);
546 pmu_ack_off(sc);
547 /* wait for intr to come up */
548 do {} while (pmu_intr_state(sc) == 0);
549 pmu_ack_on(sc);
550 do {} while (pmu_intr_state(sc));
551 *data = pmu_read_reg(sc, vSR);
552 DPRINTF(" <%02x", *data);
553 return 0;
554 }
555
556 static int
557 pmu_send(void *cookie, int cmd, int length, uint8_t *in_msg, int rlen,
558 uint8_t *out_msg)
559 {
560 struct pmu_softc *sc = cookie;
561 int i, rcv_len = -1, s;
562 uint8_t out_len, intreg;
563
564 DPRINTF("pmu_send: ");
565
566 s = splhigh();
567 intreg = pmu_read_reg(sc, vIER);
568 intreg &= 0x10;
569 pmu_write_reg(sc, vIER, intreg);
570
571 /* wait idle */
572 do {} while (pmu_intr_state(sc));
573 sc->sc_error = 0;
574
575 /* send command */
576 pmu_send_byte(sc, cmd);
577
578 /* send length if necessary */
579 if (pm_send_cmd_type[cmd] < 0) {
580 pmu_send_byte(sc, length);
581 }
582
583 for (i = 0; i < length; i++) {
584 pmu_send_byte(sc, in_msg[i]);
585 DPRINTF(" next ");
586 }
587 DPRINTF("done sending\n");
588
589 /* see if there's data to read */
590 rcv_len = pm_receive_cmd_type[cmd];
591 if (rcv_len == 0)
592 goto done;
593
594 /* read command */
595 if (rcv_len == 1) {
596 pmu_read_byte(sc, out_msg);
597 goto done;
598 } else
599 out_msg[0] = cmd;
600 if (rcv_len < 0) {
601 pmu_read_byte(sc, &out_len);
602 rcv_len = out_len + 1;
603 }
604 for (i = 1; i < uimin(rcv_len, rlen); i++)
605 pmu_read_byte(sc, &out_msg[i]);
606
607 done:
608 DPRINTF("\n");
609 pmu_write_reg(sc, vIER, (intreg == 0) ? 0 : 0x90);
610 splx(s);
611
612 return rcv_len;
613 }
614
615 static void
616 pmu_adb_poll(void *cookie)
617 {
618 struct pmu_softc *sc = cookie;
619 int s;
620
621 s = spltty();
622 pmu_intr(sc);
623 splx(s);
624 }
625
626 static void
627 pmu_in(struct pmu_softc *sc)
628 {
629 uint8_t reg;
630
631 reg = pmu_read_reg(sc, vACR);
632 reg &= ~vSR_OUT;
633 reg |= 0x0c;
634 pmu_write_reg(sc, vACR, reg);
635 }
636
637 static void
638 pmu_out(struct pmu_softc *sc)
639 {
640 uint8_t reg;
641
642 reg = pmu_read_reg(sc, vACR);
643 reg |= vSR_OUT;
644 reg |= 0x0c;
645 pmu_write_reg(sc, vACR, reg);
646 }
647
648 static void
649 pmu_ack_off(struct pmu_softc *sc)
650 {
651 uint8_t reg;
652
653 reg = pmu_read_reg(sc, vBufB);
654 reg &= ~vPB4;
655 pmu_write_reg(sc, vBufB, reg);
656 }
657
658 static void
659 pmu_ack_on(struct pmu_softc *sc)
660 {
661 uint8_t reg;
662
663 reg = pmu_read_reg(sc, vBufB);
664 reg |= vPB4;
665 pmu_write_reg(sc, vBufB, reg);
666 }
667
668 static int
669 pmu_intr_state(struct pmu_softc *sc)
670 {
671 return ((pmu_read_reg(sc, vBufB) & vPB3) == 0);
672 }
673
674 static int
675 pmu_intr(void *arg)
676 {
677 struct pmu_softc *sc = arg;
678 unsigned int len, i;
679 uint8_t resp[16];
680
681 DPRINTF(":");
682
683 pmu_write_reg(sc, vIFR, 0x90); /* Clear 'em */
684 len = pmu_send(sc, PMU_INT_ACK, 0, NULL, 16, resp);
685 if ((len < 1) || (resp[1] == 0))
686 goto done;
687 #ifdef PMU_DEBUG
688 {
689 DPRINTF("intr: %02x", resp[0]);
690 for (i = 1; i < len; i++)
691 DPRINTF(" %02x", resp[i]);
692 DPRINTF("\n");
693 }
694 #endif
695 if (resp[1] & PMU_INT_ADB) {
696 pmu_adb_handler(sc, len - 1, &resp[1]);
697 goto done;
698 }
699 if (resp[1] & PMU_INT_SNDBRT) {
700 /* deal with the brightness / volume control buttons */
701 DPRINTF("brightness: %d volume %d\n", resp[2], resp[3]);
702 sc->sc_brightness_wanted = resp[2];
703 sc->sc_volume_wanted = resp[3];
704 wakeup(&sc->sc_event);
705 goto done;
706 }
707 if (resp[1] & PMU_INT_PCEJECT) {
708 /* deal with PCMCIA eject buttons */
709 DPRINTF("card eject %d\n", resp[3]);
710 atomic_or_32(&sc->sc_pending, (resp[3] & 3));
711 wakeup(&sc->sc_event);
712 goto done;
713 }
714 if (resp[1] & PMU_INT_BATTERY) {
715 /* deal with battery messages */
716 printf("battery:");
717 for (i = 2; i < len; i++)
718 printf(" %02x", resp[i]);
719 printf("\n");
720 goto done;
721 }
722 if (resp[1] & PMU_INT_ENVIRONMENT) {
723 uint8_t diff;
724 #ifdef PMU_VERBOSE
725 /* deal with environment messages */
726 printf("environment:");
727 for (i = 2; i < len; i++)
728 printf(" %02x", resp[i]);
729 printf("\n");
730 #endif
731 diff = (resp[2] ^ sc->sc_env_old ) & sc->sc_env_mask;
732 if (diff == 0) goto done;
733 sc->sc_env_old = resp[2];
734 if (diff & PMU_ENV_LID_CLOSED) {
735 sc->sc_lid_closed = (resp[2] & PMU_ENV_LID_CLOSED) != 0;
736 atomic_or_32(&sc->sc_pending, PMU_EV_LID);
737 wakeup(&sc->sc_event);
738 }
739 if (diff & PMU_ENV_POWER_BUTTON) {
740 sc->sc_button = (resp[2] & PMU_ENV_POWER_BUTTON) != 0;
741 atomic_or_32(&sc->sc_pending, PMU_EV_BUTTON);
742 wakeup(&sc->sc_event);
743 }
744 goto done;
745 }
746 if (resp[1] & PMU_INT_TICK) {
747 /* don't bother */
748 goto done;
749 }
750
751 /* unknown interrupt code?! */
752 #ifdef PMU_DEBUG
753 printf("pmu intr: %02x:", resp[1]);
754 for (i = 2; i < len; i++)
755 printf(" %02x", resp[i]);
756 printf("\n");
757 #endif
758 done:
759 return 1;
760 }
761
762 #if 0
763 static int
764 pmu_error_handler(void *cookie, int len, uint8_t *data)
765 {
766 struct pmu_softc *sc = cookie;
767
768 /*
769 * something went wrong
770 * byte 3 seems to be the failed command
771 */
772 sc->sc_error = 1;
773 wakeup(&sc->sc_todev);
774 return 0;
775 }
776 #endif
777 #define DIFF19041970 2082844800
778
779 static int
780 pmu_todr_get(todr_chip_handle_t tch, struct timeval *tvp)
781 {
782 struct pmu_softc *sc = tch->cookie;
783 uint32_t sec;
784 int count = 10;
785 int ok = FALSE;
786 uint8_t resp[16];
787
788 DPRINTF("pmu_todr_get\n");
789 while ((count > 0) && (!ok)) {
790 pmu_send(sc, PMU_READ_RTC, 0, NULL, 16, resp);
791
792 memcpy(&sec, &resp[1], 4);
793 tvp->tv_sec = sec - DIFF19041970;
794 ok = (sec > DIFF19041970) && (sec < 0xf0000000);
795 if (!ok) aprint_error_dev(sc->sc_dev,
796 "got garbage from rtc (%08x)\n", sec);
797 count--;
798 }
799 if (count == 0) {
800 aprint_error_dev(sc->sc_dev,
801 "unable to get a sane time value\n");
802 tvp->tv_sec = 0;
803 }
804 DPRINTF("tod: %" PRIo64 "\n", tvp->tv_sec);
805 tvp->tv_usec = 0;
806 return 0;
807 }
808
809 static int
810 pmu_todr_set(todr_chip_handle_t tch, struct timeval *tvp)
811 {
812 struct pmu_softc *sc = tch->cookie;
813 uint32_t sec;
814 uint8_t resp[16];
815
816 sec = tvp->tv_sec + DIFF19041970;
817 if (pmu_send(sc, PMU_SET_RTC, 4, (uint8_t *)&sec, 16, resp) >= 0)
818 return 0;
819 return -1;
820 }
821
822 void
823 pmu_poweroff(void)
824 {
825 struct pmu_softc *sc;
826 uint8_t cmd[] = {'M', 'A', 'T', 'T'};
827 uint8_t resp[16];
828
829 if (pmu0 == NULL)
830 return;
831 sc = pmu0;
832 if (pmu_send(sc, PMU_POWER_OFF, 4, cmd, 16, resp) >= 0)
833 while (1);
834 }
835
836 void
837 pmu_restart(void)
838 {
839 struct pmu_softc *sc;
840 uint8_t resp[16];
841
842 if (pmu0 == NULL)
843 return;
844 sc = pmu0;
845 if (pmu_send(sc, PMU_RESET_CPU, 0, NULL, 16, resp) >= 0)
846 while (1);
847 }
848
849 void
850 pmu_modem(int on)
851 {
852 struct pmu_softc *sc;
853 uint8_t resp[16], cmd[2] = {0, 0};
854
855 if (pmu0 == NULL)
856 return;
857
858 sc = pmu0;
859 cmd[0] = PMU_POW0_MODEM | (on ? PMU_POW0_ON : 0);
860 pmu_send(sc, PMU_POWER_CTRL0, 1, cmd, 16, resp);
861 }
862
863 static void
864 pmu_autopoll(void *cookie, int flag)
865 {
866 struct pmu_softc *sc = cookie;
867 /* magical incantation to re-enable autopolling */
868 uint8_t cmd[] = {0, PMU_SET_POLL_MASK, (flag >> 8) & 0xff, flag & 0xff};
869 uint8_t resp[16];
870
871 if (sc->sc_autopoll == flag)
872 return;
873
874 if (flag) {
875 pmu_send(sc, PMU_ADB_CMD, 4, cmd, 16, resp);
876 } else {
877 pmu_send(sc, PMU_ADB_POLL_OFF, 0, NULL, 16, resp);
878 }
879 sc->sc_autopoll = flag & 0xffff;
880 }
881
882 static int
883 pmu_adb_handler(void *cookie, int len, uint8_t *data)
884 {
885 struct pmu_softc *sc = cookie;
886 uint8_t resp[16];
887
888 if (sc->sc_adb_handler != NULL) {
889 sc->sc_adb_handler(sc->sc_adb_cookie, len, data);
890 /*
891 * the PMU will turn off autopolling after each LISTEN so we
892 * need to re-enable it here whenever we receive an ACK for a
893 * LISTEN command
894 */
895 if ((data[1] & 0x0c) == 0x08) {
896 uint8_t cmd[] = {0, 0x86, (sc->sc_autopoll >> 8) & 0xff,
897 sc->sc_autopoll & 0xff};
898 pmu_send(sc, PMU_ADB_CMD, 4, cmd, 16, resp);
899 }
900 return 0;
901 }
902 return -1;
903 }
904
905 static int
906 pmu_adb_send(void *cookie, int poll, int command, int len, uint8_t *data)
907 {
908 struct pmu_softc *sc = cookie;
909 int i;
910 uint8_t packet[16], resp[16];
911
912 /* construct an ADB command packet and send it */
913 packet[0] = command;
914 packet[1] = 0;
915 packet[2] = len;
916 for (i = 0; i < len; i++)
917 packet[i + 3] = data[i];
918 (void)pmu_send(sc, PMU_ADB_CMD, len + 3, packet, 16, resp);
919
920 return 0;
921 }
922
923 static int
924 pmu_adb_set_handler(void *cookie, void (*handler)(void *, int, uint8_t *),
925 void *hcookie)
926 {
927 struct pmu_softc *sc = cookie;
928
929 /* register a callback for incoming ADB messages */
930 sc->sc_adb_handler = handler;
931 sc->sc_adb_cookie = hcookie;
932 return 0;
933 }
934
935 static int
936 pmu_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *_send,
937 size_t send_len, void *_recv, size_t recv_len, int flags)
938 {
939 struct pmu_softc *sc = cookie;
940 const uint8_t *send = _send;
941 uint8_t command[32] = {1, /* bus number */
942 PMU_I2C_MODE_SIMPLE,
943 0, /* bus2 */
944 addr,
945 0, /* sub address */
946 0, /* comb address */
947 0, /* count */
948 0 /* data */
949 };
950 uint8_t resp[16];
951 int len, rw;
952
953 rw = addr << 1;
954 command[3] = rw;
955 if (send_len > 0) {
956 command[6] = send_len;
957 memcpy(&command[7], send, send_len);
958 len = send_len + 7;
959 DPRINTF("pmu_i2c_exec(%02x, %d)\n", addr, send_len);
960
961 len = pmu_send(sc, PMU_I2C_CMD, len, command, 16, resp);
962 DPRINTF("resp(%d): %2x %2x\n", len, resp[0], resp[1]);
963
964 if (resp[1] != PMU_I2C_STATUS_OK) {
965 DPRINTF("%s: iic error %d\n", __func__, resp[1]);
966 return -1;
967 }
968 }
969 /* see if we're supposed to read */
970 if (I2C_OP_READ_P(op)) {
971 rw |= 1;
972 command[3] = rw;
973 command[6] = recv_len;
974 len = pmu_send(sc, PMU_I2C_CMD, 7, command, 16, resp);
975 DPRINTF("resp2(%d): %2x %2x\n", len, resp[0], resp[1]);
976
977 command[0] = 0;
978 len = pmu_send(sc, PMU_I2C_CMD, 1, command, 16, resp);
979 DPRINTF("resp3(%d): %2x %2x %2x\n", len, resp[0], resp[1],
980 resp[2]);
981 if ((len - 2) != recv_len) {
982 DPRINTF("%s: %s(%d) - got %d\n",
983 device_xname(sc->sc_dev),
984 __func__, recv_len, len - 2);
985 return -1;
986 }
987 memcpy(_recv, &resp[2], len - 2);
988 return 0;
989 };
990 return 0;
991 }
992
993 static void
994 pmu_eject_card(struct pmu_softc *sc, int socket)
995 {
996 uint8_t buf[] = {socket | 4};
997 uint8_t res[4];
998
999 atomic_and_32(&sc->sc_pending, ~socket);
1000 pmu_send(sc, PMU_EJECT_PCMCIA, 1, buf, 4, res);
1001 }
1002
1003 static void
1004 pmu_update_brightness(struct pmu_softc *sc)
1005 {
1006 int val;
1007 uint8_t cmd[2], resp[16];
1008
1009 if (sc->sc_brightness == sc->sc_brightness_wanted)
1010 return;
1011
1012 if ((sc->sc_flags & PMU_HAS_BACKLIGHT_CONTROL) == 0) {
1013
1014 aprint_normal_dev(sc->sc_dev,
1015 "this PMU doesn't support backlight control\n");
1016 sc->sc_brightness = sc->sc_brightness_wanted;
1017 return;
1018 }
1019
1020 if (sc->sc_brightness_wanted == 0) {
1021
1022 /* turn backlight off completely */
1023 cmd[0] = PMU_POW_OFF | PMU_POW_BACKLIGHT;
1024 pmu_send(sc, PMU_POWER_CTRL, 1, cmd, 16, resp);
1025 sc->sc_brightness = sc->sc_brightness_wanted;
1026
1027 /* don't bother with brightness */
1028 return;
1029 }
1030
1031 /* turn backlight on if needed */
1032 if (sc->sc_brightness == 0) {
1033 cmd[0] = PMU_POW_ON | PMU_POW_BACKLIGHT;
1034 pmu_send(sc, PMU_POWER_CTRL, 1, cmd, 16, resp);
1035 }
1036
1037 DPRINTF("pmu_update_brightness: %d -> %d\n", sc->sc_brightness,
1038 sc->sc_brightness_wanted);
1039
1040 val = 0x7f - (sc->sc_brightness_wanted >> 1);
1041 if (val < 0x08)
1042 val = 0x08;
1043 if (val > 0x78)
1044 val = 0x78;
1045 cmd[0] = val;
1046 pmu_send(sc, PMU_SET_BRIGHTNESS, 1, cmd, 16, resp);
1047
1048 sc->sc_brightness = sc->sc_brightness_wanted;
1049 }
1050
1051 static void
1052 pmu_thread(void *cookie)
1053 {
1054 struct pmu_softc *sc = cookie;
1055 //time_t time_bat = time_second;
1056 int ticks = hz, i;
1057
1058 while (1) {
1059 tsleep(&sc->sc_event, PWAIT, "wait", ticks);
1060 if ((sc->sc_pending & 3) != 0) {
1061 DPRINTF("eject %d\n", sc->sc_pending & 3);
1062 for (i = 1; i < 3; i++) {
1063 if (i & sc->sc_pending)
1064 pmu_eject_card(sc, i);
1065 }
1066 }
1067
1068 /* see if we need to update brightness */
1069 if (sc->sc_brightness_wanted != sc->sc_brightness) {
1070 pmu_update_brightness(sc);
1071 }
1072
1073 /* see if we need to update audio volume */
1074 if (sc->sc_volume_wanted != sc->sc_volume) {
1075 #if 0
1076 set_volume(sc->sc_volume_wanted);
1077 #endif
1078 sc->sc_volume = sc->sc_volume_wanted;
1079 }
1080
1081 if (sc->sc_pending & PMU_EV_LID) {
1082 atomic_and_32(&sc->sc_pending, ~PMU_EV_LID);
1083 sysmon_pswitch_event(&sc->sc_lidswitch,
1084 sc->sc_lid_closed ? PSWITCH_EVENT_PRESSED :
1085 PSWITCH_EVENT_RELEASED);
1086 }
1087
1088 if (sc->sc_pending & PMU_EV_BUTTON) {
1089 atomic_and_32(&sc->sc_pending, ~PMU_EV_BUTTON);
1090 sysmon_pswitch_event(&sc->sc_powerbutton,
1091 sc->sc_button ? PSWITCH_EVENT_PRESSED :
1092 PSWITCH_EVENT_RELEASED);
1093 }
1094
1095 if (sc->sc_callback != NULL)
1096 sc->sc_callback(sc->sc_cb_cookie);
1097 }
1098 }
1099
1100 static int
1101 pmu_print(void *aux, const char *what)
1102 {
1103
1104 return 0;
1105 }
1106
1107 static void
1108 pmu_attach_legacy_battery(struct pmu_softc *sc)
1109 {
1110 struct battery_attach_args baa;
1111
1112 baa.baa_type = BATTERY_TYPE_LEGACY;
1113 baa.baa_pmu_ops = &sc->sc_pmu_ops;
1114 config_found(sc->sc_dev, &baa, pmu_print,
1115 CFARG_IATTR, "pmu_bus",
1116 CFARG_EOL);
1117 }
1118
1119 static void
1120 pmu_attach_smart_battery(struct pmu_softc *sc, int num)
1121 {
1122 struct battery_attach_args baa;
1123
1124 baa.baa_type = BATTERY_TYPE_SMART;
1125 baa.baa_pmu_ops = &sc->sc_pmu_ops;
1126 baa.baa_num = num;
1127 config_found(sc->sc_dev, &baa, pmu_print,
1128 CFARG_IATTR, "pmu_bus",
1129 CFARG_EOL);
1130 }
1131