pmu.c revision 1.36 1 /* $NetBSD: pmu.c,v 1.36 2021/03/05 07:15:53 rin 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.36 2021/03/05 07:15:53 rin 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;
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 prop_array_add(cfg, dev);
376 prop_object_release(dev);
377 skip:
378 devs = OF_peer(devs);
379 }
380 memset(&iba, 0, sizeof(iba));
381 iba.iba_tag = &sc->sc_i2c;
382 iic_tag_init(&sc->sc_i2c);
383 sc->sc_i2c.ic_cookie = sc;
384 sc->sc_i2c.ic_exec = pmu_i2c_exec;
385 config_found_ia(sc->sc_dev, "i2cbus", &iba,
386 iicbus_print);
387 goto next;
388 }
389 if (strncmp(name, "adb", 4) == 0) {
390 aprint_normal_dev(self, "initializing ADB\n");
391 sc->sc_adbops.cookie = sc;
392 sc->sc_adbops.send = pmu_adb_send;
393 sc->sc_adbops.poll = pmu_adb_poll;
394 sc->sc_adbops.autopoll = pmu_autopoll;
395 sc->sc_adbops.set_handler = pmu_adb_set_handler;
396 #if NNADB > 0
397 config_found_ia(self, "adb_bus", &sc->sc_adbops,
398 nadb_print);
399 #endif
400 goto next;
401 }
402 if (strncmp(name, "rtc", 4) == 0) {
403
404 aprint_normal_dev(self, "initializing RTC\n");
405 sc->sc_todr.todr_gettime = pmu_todr_get;
406 sc->sc_todr.todr_settime = pmu_todr_set;
407 sc->sc_todr.cookie = sc;
408 todr_attach(&sc->sc_todr);
409 goto next;
410 }
411 if (strncmp(name, "battery", 8) == 0)
412 goto next;
413
414 aprint_normal_dev(self, "%s not configured\n", name);
415 next:
416 node = OF_peer(node);
417 }
418
419 if (OF_finddevice("/bandit/ohare") != -1) {
420 aprint_normal_dev(self, "enabling ohare backlight control\n");
421 sc->sc_flags |= PMU_HAS_BACKLIGHT_CONTROL;
422 cmd[0] = 0;
423 cmd[1] = 0;
424 memset(resp, 0, 6);
425 if (pmu_send(sc, PMU_READ_BRIGHTNESS, 1, cmd, 16, resp) > 1) {
426 sc->sc_brightness_wanted = resp[1];
427 pmu_update_brightness(sc);
428 }
429 }
430
431 /* attach batteries */
432 if (of_compatible(root_node, has_legacy_battery)) {
433
434 pmu_attach_legacy_battery(sc);
435 } else if (of_compatible(root_node, has_two_smart_batteries)) {
436
437 pmu_attach_smart_battery(sc, 0);
438 pmu_attach_smart_battery(sc, 1);
439 } else {
440
441 /* check how many batteries we have */
442 pmnode = of_getnode_byname(ca->ca_node, "power-mgt");
443 if (pmnode == -1)
444 goto bat_done;
445 if (OF_getprop(pmnode, "prim-info", regs, sizeof(regs)) < 24)
446 goto bat_done;
447 nbat = regs[6] >> 16;
448 for (i = 0; i < nbat; i++)
449 pmu_attach_smart_battery(sc, i);
450 }
451 bat_done:
452
453 if (kthread_create(PRI_NONE, 0, NULL, pmu_thread, sc, &sc->sc_thread,
454 "%s", "pmu") != 0) {
455 aprint_error_dev(self, "unable to create event kthread\n");
456 }
457
458 sc->sc_lidswitch.smpsw_name = "Lid switch";
459 sc->sc_lidswitch.smpsw_type = PSWITCH_TYPE_LID;
460 if (sysmon_pswitch_register(&sc->sc_lidswitch) != 0)
461 aprint_error_dev(self,
462 "unable to register lid switch with sysmon\n");
463
464 sc->sc_powerbutton.smpsw_name = "Power button";
465 sc->sc_powerbutton.smpsw_type = PSWITCH_TYPE_POWER;
466 if (sysmon_pswitch_register(&sc->sc_powerbutton) != 0)
467 aprint_error_dev(self,
468 "unable to register power button with sysmon\n");
469 }
470
471 static void
472 pmu_register_callback(void *pmu_cookie, void (*cb)(void *), void *cookie)
473 {
474 struct pmu_softc *sc = pmu_cookie;
475
476 sc->sc_callback = cb;
477 sc->sc_cb_cookie = cookie;
478 }
479
480 static void
481 pmu_init(struct pmu_softc *sc)
482 {
483 uint8_t pmu_imask, resp[16];
484
485 pmu_imask =
486 PMU_INT_PCEJECT | PMU_INT_SNDBRT | PMU_INT_ADB/* | PMU_INT_TICK*/;
487 pmu_imask |= PMU_INT_BATTERY;
488 pmu_imask |= PMU_INT_ENVIRONMENT;
489 pmu_send(sc, PMU_SET_IMASK, 1, &pmu_imask, 16, resp);
490
491 pmu_write_reg(sc, vIER, 0x90); /* enable VIA interrupts */
492 }
493
494 static inline void
495 pmu_write_reg(struct pmu_softc *sc, int offset, uint8_t value)
496 {
497
498 bus_space_write_1(sc->sc_memt, sc->sc_memh, offset, value);
499 }
500
501 static inline uint8_t
502 pmu_read_reg(struct pmu_softc *sc, int offset)
503 {
504
505 return bus_space_read_1(sc->sc_memt, sc->sc_memh, offset);
506 }
507
508 static inline int
509 pmu_send_byte(struct pmu_softc *sc, uint8_t data)
510 {
511
512 pmu_out(sc);
513 pmu_write_reg(sc, vSR, data);
514 pmu_ack_off(sc);
515 /* wait for intr to come up */
516 /* XXX should add a timeout and bail if it expires */
517 do {} while (pmu_intr_state(sc) == 0);
518 pmu_ack_on(sc);
519 do {} while (pmu_intr_state(sc));
520 pmu_ack_on(sc);
521 DPRINTF(" %02x>", data);
522 return 0;
523 }
524
525 static inline int
526 pmu_read_byte(struct pmu_softc *sc, uint8_t *data)
527 {
528 pmu_in(sc);
529 (void)pmu_read_reg(sc, vSR);
530 pmu_ack_off(sc);
531 /* wait for intr to come up */
532 do {} while (pmu_intr_state(sc) == 0);
533 pmu_ack_on(sc);
534 do {} while (pmu_intr_state(sc));
535 *data = pmu_read_reg(sc, vSR);
536 DPRINTF(" <%02x", *data);
537 return 0;
538 }
539
540 static int
541 pmu_send(void *cookie, int cmd, int length, uint8_t *in_msg, int rlen,
542 uint8_t *out_msg)
543 {
544 struct pmu_softc *sc = cookie;
545 int i, rcv_len = -1, s;
546 uint8_t out_len, intreg;
547
548 DPRINTF("pmu_send: ");
549
550 s = splhigh();
551 intreg = pmu_read_reg(sc, vIER);
552 intreg &= 0x10;
553 pmu_write_reg(sc, vIER, intreg);
554
555 /* wait idle */
556 do {} while (pmu_intr_state(sc));
557 sc->sc_error = 0;
558
559 /* send command */
560 pmu_send_byte(sc, cmd);
561
562 /* send length if necessary */
563 if (pm_send_cmd_type[cmd] < 0) {
564 pmu_send_byte(sc, length);
565 }
566
567 for (i = 0; i < length; i++) {
568 pmu_send_byte(sc, in_msg[i]);
569 DPRINTF(" next ");
570 }
571 DPRINTF("done sending\n");
572
573 /* see if there's data to read */
574 rcv_len = pm_receive_cmd_type[cmd];
575 if (rcv_len == 0)
576 goto done;
577
578 /* read command */
579 if (rcv_len == 1) {
580 pmu_read_byte(sc, out_msg);
581 goto done;
582 } else
583 out_msg[0] = cmd;
584 if (rcv_len < 0) {
585 pmu_read_byte(sc, &out_len);
586 rcv_len = out_len + 1;
587 }
588 for (i = 1; i < uimin(rcv_len, rlen); i++)
589 pmu_read_byte(sc, &out_msg[i]);
590
591 done:
592 DPRINTF("\n");
593 pmu_write_reg(sc, vIER, (intreg == 0) ? 0 : 0x90);
594 splx(s);
595
596 return rcv_len;
597 }
598
599 static void
600 pmu_adb_poll(void *cookie)
601 {
602 struct pmu_softc *sc = cookie;
603 int s;
604
605 s = spltty();
606 pmu_intr(sc);
607 splx(s);
608 }
609
610 static void
611 pmu_in(struct pmu_softc *sc)
612 {
613 uint8_t reg;
614
615 reg = pmu_read_reg(sc, vACR);
616 reg &= ~vSR_OUT;
617 reg |= 0x0c;
618 pmu_write_reg(sc, vACR, reg);
619 }
620
621 static void
622 pmu_out(struct pmu_softc *sc)
623 {
624 uint8_t reg;
625
626 reg = pmu_read_reg(sc, vACR);
627 reg |= vSR_OUT;
628 reg |= 0x0c;
629 pmu_write_reg(sc, vACR, reg);
630 }
631
632 static void
633 pmu_ack_off(struct pmu_softc *sc)
634 {
635 uint8_t reg;
636
637 reg = pmu_read_reg(sc, vBufB);
638 reg &= ~vPB4;
639 pmu_write_reg(sc, vBufB, reg);
640 }
641
642 static void
643 pmu_ack_on(struct pmu_softc *sc)
644 {
645 uint8_t reg;
646
647 reg = pmu_read_reg(sc, vBufB);
648 reg |= vPB4;
649 pmu_write_reg(sc, vBufB, reg);
650 }
651
652 static int
653 pmu_intr_state(struct pmu_softc *sc)
654 {
655 return ((pmu_read_reg(sc, vBufB) & vPB3) == 0);
656 }
657
658 static int
659 pmu_intr(void *arg)
660 {
661 struct pmu_softc *sc = arg;
662 unsigned int len, i;
663 uint8_t resp[16];
664
665 DPRINTF(":");
666
667 pmu_write_reg(sc, vIFR, 0x90); /* Clear 'em */
668 len = pmu_send(sc, PMU_INT_ACK, 0, NULL, 16, resp);
669 if ((len < 1) || (resp[1] == 0))
670 goto done;
671 #ifdef PMU_DEBUG
672 {
673 DPRINTF("intr: %02x", resp[0]);
674 for (i = 1; i < len; i++)
675 DPRINTF(" %02x", resp[i]);
676 DPRINTF("\n");
677 }
678 #endif
679 if (resp[1] & PMU_INT_ADB) {
680 pmu_adb_handler(sc, len - 1, &resp[1]);
681 goto done;
682 }
683 if (resp[1] & PMU_INT_SNDBRT) {
684 /* deal with the brightness / volume control buttons */
685 DPRINTF("brightness: %d volume %d\n", resp[2], resp[3]);
686 sc->sc_brightness_wanted = resp[2];
687 sc->sc_volume_wanted = resp[3];
688 wakeup(&sc->sc_event);
689 goto done;
690 }
691 if (resp[1] & PMU_INT_PCEJECT) {
692 /* deal with PCMCIA eject buttons */
693 DPRINTF("card eject %d\n", resp[3]);
694 atomic_or_32(&sc->sc_pending, (resp[3] & 3));
695 wakeup(&sc->sc_event);
696 goto done;
697 }
698 if (resp[1] & PMU_INT_BATTERY) {
699 /* deal with battery messages */
700 printf("battery:");
701 for (i = 2; i < len; i++)
702 printf(" %02x", resp[i]);
703 printf("\n");
704 goto done;
705 }
706 if (resp[1] & PMU_INT_ENVIRONMENT) {
707 uint8_t diff;
708 #ifdef PMU_VERBOSE
709 /* deal with environment messages */
710 printf("environment:");
711 for (i = 2; i < len; i++)
712 printf(" %02x", resp[i]);
713 printf("\n");
714 #endif
715 diff = (resp[2] ^ sc->sc_env_old ) & sc->sc_env_mask;
716 if (diff == 0) goto done;
717 sc->sc_env_old = resp[2];
718 if (diff & PMU_ENV_LID_CLOSED) {
719 sc->sc_lid_closed = (resp[2] & PMU_ENV_LID_CLOSED) != 0;
720 atomic_or_32(&sc->sc_pending, PMU_EV_LID);
721 wakeup(&sc->sc_event);
722 }
723 if (diff & PMU_ENV_POWER_BUTTON) {
724 sc->sc_button = (resp[2] & PMU_ENV_POWER_BUTTON) != 0;
725 atomic_or_32(&sc->sc_pending, PMU_EV_BUTTON);
726 wakeup(&sc->sc_event);
727 }
728 goto done;
729 }
730 if (resp[1] & PMU_INT_TICK) {
731 /* don't bother */
732 goto done;
733 }
734
735 /* unknown interrupt code?! */
736 #ifdef PMU_DEBUG
737 printf("pmu intr: %02x:", resp[1]);
738 for (i = 2; i < len; i++)
739 printf(" %02x", resp[i]);
740 printf("\n");
741 #endif
742 done:
743 return 1;
744 }
745
746 #if 0
747 static int
748 pmu_error_handler(void *cookie, int len, uint8_t *data)
749 {
750 struct pmu_softc *sc = cookie;
751
752 /*
753 * something went wrong
754 * byte 3 seems to be the failed command
755 */
756 sc->sc_error = 1;
757 wakeup(&sc->sc_todev);
758 return 0;
759 }
760 #endif
761 #define DIFF19041970 2082844800
762
763 static int
764 pmu_todr_get(todr_chip_handle_t tch, struct timeval *tvp)
765 {
766 struct pmu_softc *sc = tch->cookie;
767 uint32_t sec;
768 int count = 10;
769 int ok = FALSE;
770 uint8_t resp[16];
771
772 DPRINTF("pmu_todr_get\n");
773 while ((count > 0) && (!ok)) {
774 pmu_send(sc, PMU_READ_RTC, 0, NULL, 16, resp);
775
776 memcpy(&sec, &resp[1], 4);
777 tvp->tv_sec = sec - DIFF19041970;
778 ok = (sec > DIFF19041970) && (sec < 0xf0000000);
779 if (!ok) aprint_error_dev(sc->sc_dev,
780 "got garbage from rtc (%08x)\n", sec);
781 count--;
782 }
783 if (count == 0) {
784 aprint_error_dev(sc->sc_dev,
785 "unable to get a sane time value\n");
786 tvp->tv_sec = 0;
787 }
788 DPRINTF("tod: %" PRIo64 "\n", tvp->tv_sec);
789 tvp->tv_usec = 0;
790 return 0;
791 }
792
793 static int
794 pmu_todr_set(todr_chip_handle_t tch, struct timeval *tvp)
795 {
796 struct pmu_softc *sc = tch->cookie;
797 uint32_t sec;
798 uint8_t resp[16];
799
800 sec = tvp->tv_sec + DIFF19041970;
801 if (pmu_send(sc, PMU_SET_RTC, 4, (uint8_t *)&sec, 16, resp) >= 0)
802 return 0;
803 return -1;
804 }
805
806 void
807 pmu_poweroff(void)
808 {
809 struct pmu_softc *sc;
810 uint8_t cmd[] = {'M', 'A', 'T', 'T'};
811 uint8_t resp[16];
812
813 if (pmu0 == NULL)
814 return;
815 sc = pmu0;
816 if (pmu_send(sc, PMU_POWER_OFF, 4, cmd, 16, resp) >= 0)
817 while (1);
818 }
819
820 void
821 pmu_restart(void)
822 {
823 struct pmu_softc *sc;
824 uint8_t resp[16];
825
826 if (pmu0 == NULL)
827 return;
828 sc = pmu0;
829 if (pmu_send(sc, PMU_RESET_CPU, 0, NULL, 16, resp) >= 0)
830 while (1);
831 }
832
833 void
834 pmu_modem(int on)
835 {
836 struct pmu_softc *sc;
837 uint8_t resp[16], cmd[2] = {0, 0};
838
839 if (pmu0 == NULL)
840 return;
841
842 sc = pmu0;
843 cmd[0] = PMU_POW0_MODEM | (on ? PMU_POW0_ON : 0);
844 pmu_send(sc, PMU_POWER_CTRL0, 1, cmd, 16, resp);
845 }
846
847 static void
848 pmu_autopoll(void *cookie, int flag)
849 {
850 struct pmu_softc *sc = cookie;
851 /* magical incantation to re-enable autopolling */
852 uint8_t cmd[] = {0, PMU_SET_POLL_MASK, (flag >> 8) & 0xff, flag & 0xff};
853 uint8_t resp[16];
854
855 if (sc->sc_autopoll == flag)
856 return;
857
858 if (flag) {
859 pmu_send(sc, PMU_ADB_CMD, 4, cmd, 16, resp);
860 } else {
861 pmu_send(sc, PMU_ADB_POLL_OFF, 0, NULL, 16, resp);
862 }
863 sc->sc_autopoll = flag & 0xffff;
864 }
865
866 static int
867 pmu_adb_handler(void *cookie, int len, uint8_t *data)
868 {
869 struct pmu_softc *sc = cookie;
870 uint8_t resp[16];
871
872 if (sc->sc_adb_handler != NULL) {
873 sc->sc_adb_handler(sc->sc_adb_cookie, len, data);
874 /*
875 * the PMU will turn off autopolling after each LISTEN so we
876 * need to re-enable it here whenever we receive an ACK for a
877 * LISTEN command
878 */
879 if ((data[1] & 0x0c) == 0x08) {
880 uint8_t cmd[] = {0, 0x86, (sc->sc_autopoll >> 8) & 0xff,
881 sc->sc_autopoll & 0xff};
882 pmu_send(sc, PMU_ADB_CMD, 4, cmd, 16, resp);
883 }
884 return 0;
885 }
886 return -1;
887 }
888
889 static int
890 pmu_adb_send(void *cookie, int poll, int command, int len, uint8_t *data)
891 {
892 struct pmu_softc *sc = cookie;
893 int i;
894 uint8_t packet[16], resp[16];
895
896 /* construct an ADB command packet and send it */
897 packet[0] = command;
898 packet[1] = 0;
899 packet[2] = len;
900 for (i = 0; i < len; i++)
901 packet[i + 3] = data[i];
902 (void)pmu_send(sc, PMU_ADB_CMD, len + 3, packet, 16, resp);
903
904 return 0;
905 }
906
907 static int
908 pmu_adb_set_handler(void *cookie, void (*handler)(void *, int, uint8_t *),
909 void *hcookie)
910 {
911 struct pmu_softc *sc = cookie;
912
913 /* register a callback for incoming ADB messages */
914 sc->sc_adb_handler = handler;
915 sc->sc_adb_cookie = hcookie;
916 return 0;
917 }
918
919 static int
920 pmu_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *_send,
921 size_t send_len, void *_recv, size_t recv_len, int flags)
922 {
923 struct pmu_softc *sc = cookie;
924 const uint8_t *send = _send;
925 uint8_t command[32] = {1, /* bus number */
926 PMU_I2C_MODE_SIMPLE,
927 0, /* bus2 */
928 addr,
929 0, /* sub address */
930 0, /* comb address */
931 0, /* count */
932 0 /* data */
933 };
934 uint8_t resp[16];
935 int len, rw;
936
937 rw = addr << 1;
938 command[3] = rw;
939 if (send_len > 0) {
940 command[6] = send_len;
941 memcpy(&command[7], send, send_len);
942 len = send_len + 7;
943 DPRINTF("pmu_i2c_exec(%02x, %d)\n", addr, send_len);
944
945 len = pmu_send(sc, PMU_I2C_CMD, len, command, 16, resp);
946 DPRINTF("resp(%d): %2x %2x\n", len, resp[0], resp[1]);
947
948 if (resp[1] != PMU_I2C_STATUS_OK) {
949 DPRINTF("%s: iic error %d\n", __func__, resp[1]);
950 return -1;
951 }
952 }
953 /* see if we're supposed to read */
954 if (I2C_OP_READ_P(op)) {
955 rw |= 1;
956 command[3] = rw;
957 command[6] = recv_len;
958 len = pmu_send(sc, PMU_I2C_CMD, 7, command, 16, resp);
959 DPRINTF("resp2(%d): %2x %2x\n", len, resp[0], resp[1]);
960
961 command[0] = 0;
962 len = pmu_send(sc, PMU_I2C_CMD, 1, command, 16, resp);
963 DPRINTF("resp3(%d): %2x %2x %2x\n", len, resp[0], resp[1],
964 resp[2]);
965 if ((len - 2) != recv_len) {
966 DPRINTF("%s: %s(%d) - got %d\n",
967 device_xname(sc->sc_dev),
968 __func__, recv_len, len - 2);
969 return -1;
970 }
971 memcpy(_recv, &resp[2], len - 2);
972 return 0;
973 };
974 return 0;
975 }
976
977 static void
978 pmu_eject_card(struct pmu_softc *sc, int socket)
979 {
980 uint8_t buf[] = {socket | 4};
981 uint8_t res[4];
982
983 atomic_and_32(&sc->sc_pending, ~socket);
984 pmu_send(sc, PMU_EJECT_PCMCIA, 1, buf, 4, res);
985 }
986
987 static void
988 pmu_update_brightness(struct pmu_softc *sc)
989 {
990 int val;
991 uint8_t cmd[2], resp[16];
992
993 if (sc->sc_brightness == sc->sc_brightness_wanted)
994 return;
995
996 if ((sc->sc_flags & PMU_HAS_BACKLIGHT_CONTROL) == 0) {
997
998 aprint_normal_dev(sc->sc_dev,
999 "this PMU doesn't support backlight control\n");
1000 sc->sc_brightness = sc->sc_brightness_wanted;
1001 return;
1002 }
1003
1004 if (sc->sc_brightness_wanted == 0) {
1005
1006 /* turn backlight off completely */
1007 cmd[0] = PMU_POW_OFF | PMU_POW_BACKLIGHT;
1008 pmu_send(sc, PMU_POWER_CTRL, 1, cmd, 16, resp);
1009 sc->sc_brightness = sc->sc_brightness_wanted;
1010
1011 /* don't bother with brightness */
1012 return;
1013 }
1014
1015 /* turn backlight on if needed */
1016 if (sc->sc_brightness == 0) {
1017 cmd[0] = PMU_POW_ON | PMU_POW_BACKLIGHT;
1018 pmu_send(sc, PMU_POWER_CTRL, 1, cmd, 16, resp);
1019 }
1020
1021 DPRINTF("pmu_update_brightness: %d -> %d\n", sc->sc_brightness,
1022 sc->sc_brightness_wanted);
1023
1024 val = 0x7f - (sc->sc_brightness_wanted >> 1);
1025 if (val < 0x08)
1026 val = 0x08;
1027 if (val > 0x78)
1028 val = 0x78;
1029 cmd[0] = val;
1030 pmu_send(sc, PMU_SET_BRIGHTNESS, 1, cmd, 16, resp);
1031
1032 sc->sc_brightness = sc->sc_brightness_wanted;
1033 }
1034
1035 static void
1036 pmu_thread(void *cookie)
1037 {
1038 struct pmu_softc *sc = cookie;
1039 //time_t time_bat = time_second;
1040 int ticks = hz, i;
1041
1042 while (1) {
1043 tsleep(&sc->sc_event, PWAIT, "wait", ticks);
1044 if ((sc->sc_pending & 3) != 0) {
1045 DPRINTF("eject %d\n", sc->sc_pending & 3);
1046 for (i = 1; i < 3; i++) {
1047 if (i & sc->sc_pending)
1048 pmu_eject_card(sc, i);
1049 }
1050 }
1051
1052 /* see if we need to update brightness */
1053 if (sc->sc_brightness_wanted != sc->sc_brightness) {
1054 pmu_update_brightness(sc);
1055 }
1056
1057 /* see if we need to update audio volume */
1058 if (sc->sc_volume_wanted != sc->sc_volume) {
1059 #if 0
1060 set_volume(sc->sc_volume_wanted);
1061 #endif
1062 sc->sc_volume = sc->sc_volume_wanted;
1063 }
1064
1065 if (sc->sc_pending & PMU_EV_LID) {
1066 atomic_and_32(&sc->sc_pending, ~PMU_EV_LID);
1067 sysmon_pswitch_event(&sc->sc_lidswitch,
1068 sc->sc_lid_closed ? PSWITCH_EVENT_PRESSED :
1069 PSWITCH_EVENT_RELEASED);
1070 }
1071
1072 if (sc->sc_pending & PMU_EV_BUTTON) {
1073 atomic_and_32(&sc->sc_pending, ~PMU_EV_BUTTON);
1074 sysmon_pswitch_event(&sc->sc_powerbutton,
1075 sc->sc_button ? PSWITCH_EVENT_PRESSED :
1076 PSWITCH_EVENT_RELEASED);
1077 }
1078
1079 if (sc->sc_callback != NULL)
1080 sc->sc_callback(sc->sc_cb_cookie);
1081 }
1082 }
1083
1084 static int
1085 pmu_print(void *aux, const char *what)
1086 {
1087
1088 return 0;
1089 }
1090
1091 static void
1092 pmu_attach_legacy_battery(struct pmu_softc *sc)
1093 {
1094 struct battery_attach_args baa;
1095
1096 baa.baa_type = BATTERY_TYPE_LEGACY;
1097 baa.baa_pmu_ops = &sc->sc_pmu_ops;
1098 config_found_ia(sc->sc_dev, "pmu_bus", &baa, pmu_print);
1099 }
1100
1101 static void
1102 pmu_attach_smart_battery(struct pmu_softc *sc, int num)
1103 {
1104 struct battery_attach_args baa;
1105
1106 baa.baa_type = BATTERY_TYPE_SMART;
1107 baa.baa_pmu_ops = &sc->sc_pmu_ops;
1108 baa.baa_num = num;
1109 config_found_ia(sc->sc_dev, "pmu_bus", &baa, pmu_print);
1110 }
1111