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