2 * Device driver for the PMU on 68K-based Apple PowerBooks
4 * The VIA (versatile interface adapter) interfaces to the PMU,
5 * a 6805 microprocessor core whose primary function is to control
6 * battery charging and system power on the PowerBooks.
7 * The PMU also controls the ADB (Apple Desktop Bus) which connects
8 * to the keyboard and mouse, as well as the non-volatile RAM
9 * and the RTC (real time clock) chip.
11 * Adapted for 68K PMU by Joshua M. Thompson
13 * Based largely on the PowerMac PMU code by Paul Mackerras and
16 * Also based on the PMU driver from MkLinux by Apple Computer, Inc.
17 * and the Open Software Foundation, Inc.
21 #include <linux/types.h>
22 #include <linux/errno.h>
23 #include <linux/kernel.h>
24 #include <linux/delay.h>
25 #include <linux/miscdevice.h>
26 #include <linux/blkdev.h>
27 #include <linux/pci.h>
28 #include <linux/slab.h>
29 #include <linux/init.h>
30 #include <linux/interrupt.h>
32 #include <linux/adb.h>
33 #include <linux/pmu.h>
34 #include <linux/cuda.h>
36 #include <asm/macintosh.h>
37 #include <asm/macints.h>
38 #include <asm/machw.h>
39 #include <asm/mac_via.h>
41 #include <asm/pgtable.h>
42 #include <asm/system.h>
44 #include <asm/uaccess.h>
46 /* Misc minor number allocated for /dev/pmu */
49 /* VIA registers - spaced 0x200 bytes apart */
50 #define RS 0x200 /* skip between registers */
51 #define B 0 /* B-side data */
52 #define A RS /* A-side data */
53 #define DIRB (2*RS) /* B-side direction (1=output) */
54 #define DIRA (3*RS) /* A-side direction (1=output) */
55 #define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */
56 #define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */
57 #define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */
58 #define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */
59 #define T2CL (8*RS) /* Timer 2 ctr/latch (low 8 bits) */
60 #define T2CH (9*RS) /* Timer 2 counter (high 8 bits) */
61 #define SR (10*RS) /* Shift register */
62 #define ACR (11*RS) /* Auxiliary control register */
63 #define PCR (12*RS) /* Peripheral control register */
64 #define IFR (13*RS) /* Interrupt flag register */
65 #define IER (14*RS) /* Interrupt enable register */
66 #define ANH (15*RS) /* A-side data, no handshake */
68 /* Bits in B data register: both active low */
69 #define TACK 0x02 /* Transfer acknowledge (input) */
70 #define TREQ 0x04 /* Transfer request (output) */
73 #define SR_CTRL 0x1c /* Shift register control bits */
74 #define SR_EXT 0x0c /* Shift on external clock */
75 #define SR_OUT 0x10 /* Shift out if 1 */
77 /* Bits in IFR and IER */
78 #define SR_INT 0x04 /* Shift register full/empty */
79 #define CB1_INT 0x10 /* transition on CB1 input */
81 static enum pmu_state {
89 static struct adb_request *current_req;
90 static struct adb_request *last_req;
91 static struct adb_request *req_awaiting_reply;
92 static unsigned char interrupt_data[32];
93 static unsigned char *reply_ptr;
94 static int data_index;
96 static int adb_int_pending;
97 static int pmu_adb_flags;
98 static int adb_dev_map;
99 static struct adb_request bright_req_1, bright_req_2, bright_req_3;
100 static int pmu_kind = PMU_UNKNOWN;
101 static int pmu_fully_inited;
104 BLOCKING_NOTIFIER_HEAD(sleep_notifier_list);
106 static int pmu_probe(void);
107 static int pmu_init(void);
108 static void pmu_start(void);
109 static irqreturn_t pmu_interrupt(int irq, void *arg);
110 static int pmu_send_request(struct adb_request *req, int sync);
111 static int pmu_autopoll(int devs);
113 static int pmu_reset_bus(void);
115 static void pmu_start(void);
116 static void send_byte(int x);
117 static void recv_byte(void);
118 static void pmu_done(struct adb_request *req);
119 static void pmu_handle_data(unsigned char *data, int len);
120 static void set_volume(int level);
121 static void pmu_enable_backlight(int on);
122 static void pmu_set_brightness(int level);
124 struct adb_driver via_pmu_driver = {
135 * This table indicates for each PMU opcode:
136 * - the number of data bytes to be sent with the command, or -1
137 * if a length byte should be sent,
138 * - the number of response bytes which the PMU will return, or
139 * -1 if it will send a length byte.
141 static s8 pmu_data_len[256][2] = {
142 /* 0 1 2 3 4 5 6 7 */
143 /*00*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
144 /*08*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
145 /*10*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
146 /*18*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0, 0},
147 /*20*/ {-1, 0},{ 0, 0},{ 2, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},
148 /*28*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0,-1},
149 /*30*/ { 4, 0},{20, 0},{-1, 0},{ 3, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
150 /*38*/ { 0, 4},{ 0,20},{ 2,-1},{ 2, 1},{ 3,-1},{-1,-1},{-1,-1},{ 4, 0},
151 /*40*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
152 /*48*/ { 0, 1},{ 0, 1},{-1,-1},{ 1, 0},{ 1, 0},{-1,-1},{-1,-1},{-1,-1},
153 /*50*/ { 1, 0},{ 0, 0},{ 2, 0},{ 2, 0},{-1, 0},{ 1, 0},{ 3, 0},{ 1, 0},
154 /*58*/ { 0, 1},{ 1, 0},{ 0, 2},{ 0, 2},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},
155 /*60*/ { 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
156 /*68*/ { 0, 3},{ 0, 3},{ 0, 2},{ 0, 8},{ 0,-1},{ 0,-1},{-1,-1},{-1,-1},
157 /*70*/ { 1, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
158 /*78*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{ 5, 1},{ 4, 1},{ 4, 1},
159 /*80*/ { 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
160 /*88*/ { 0, 5},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
161 /*90*/ { 1, 0},{ 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
162 /*98*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
163 /*a0*/ { 2, 0},{ 2, 0},{ 2, 0},{ 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},
164 /*a8*/ { 1, 1},{ 1, 0},{ 3, 0},{ 2, 0},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
165 /*b0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
166 /*b8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
167 /*c0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
168 /*c8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
169 /*d0*/ { 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
170 /*d8*/ { 1, 1},{ 1, 1},{-1,-1},{-1,-1},{ 0, 1},{ 0,-1},{-1,-1},{-1,-1},
171 /*e0*/ {-1, 0},{ 4, 0},{ 0, 1},{-1, 0},{-1, 0},{ 4, 0},{-1, 0},{-1, 0},
172 /*e8*/ { 3,-1},{-1,-1},{ 0, 1},{-1,-1},{ 0,-1},{-1,-1},{-1,-1},{ 0, 0},
173 /*f0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
174 /*f8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
179 if (macintosh_config->adb_type == MAC_ADB_PB1) {
180 pmu_kind = PMU_68K_V1;
181 } else if (macintosh_config->adb_type == MAC_ADB_PB2) {
182 pmu_kind = PMU_68K_V2;
196 volatile struct adb_request req;
198 via2[B] |= TREQ; /* negate TREQ */
199 via2[DIRB] = (via2[DIRB] | TREQ) & ~TACK; /* TACK in, TREQ out */
201 pmu_request((struct adb_request *) &req, NULL, 2, PMU_SET_INTR_MASK, PMU_INT_ADB);
203 while (!req.complete) {
205 printk(KERN_ERR "pmu_init: no response from PMU\n");
212 /* ack all pending interrupts */
214 interrupt_data[0] = 1;
215 while (interrupt_data[0] || pmu_state != idle) {
217 printk(KERN_ERR "pmu_init: timed out acking intrs\n");
220 if (pmu_state == idle) {
222 pmu_interrupt(0, NULL);
228 pmu_request((struct adb_request *) &req, NULL, 2, PMU_SET_INTR_MASK,
229 PMU_INT_ADB_AUTO|PMU_INT_SNDBRT|PMU_INT_ADB);
231 while (!req.complete) {
233 printk(KERN_ERR "pmu_init: no response from PMU\n");
240 bright_req_1.complete = 1;
241 bright_req_2.complete = 1;
242 bright_req_3.complete = 1;
244 if (request_irq(IRQ_MAC_ADB_SR, pmu_interrupt, 0, "pmu-shift",
246 printk(KERN_ERR "pmu_init: can't get irq %d\n",
250 if (request_irq(IRQ_MAC_ADB_CL, pmu_interrupt, 0, "pmu-clock",
252 printk(KERN_ERR "pmu_init: can't get irq %d\n",
254 free_irq(IRQ_MAC_ADB_SR, pmu_interrupt);
258 pmu_fully_inited = 1;
260 /* Enable backlight */
261 pmu_enable_backlight(1);
263 printk("adb: PMU 68K driver v0.5 for Unified ADB.\n");
274 /* Send an ADB command */
276 pmu_send_request(struct adb_request *req, int sync)
280 if (!pmu_fully_inited)
288 switch (req->data[0]) {
290 for (i = 0; i < req->nbytes - 1; ++i)
291 req->data[i] = req->data[i+1];
293 if (pmu_data_len[req->data[0]][1] != 0) {
294 req->reply[0] = ADB_RET_OK;
298 ret = pmu_queue_request(req);
301 switch (req->data[1]) {
303 if (req->nbytes != 2)
305 req->data[0] = PMU_READ_RTC;
308 req->reply[0] = CUDA_PACKET;
310 req->reply[2] = CUDA_GET_TIME;
311 ret = pmu_queue_request(req);
314 if (req->nbytes != 6)
316 req->data[0] = PMU_SET_RTC;
318 for (i = 1; i <= 4; ++i)
319 req->data[i] = req->data[i+1];
321 req->reply[0] = CUDA_PACKET;
323 req->reply[2] = CUDA_SET_TIME;
324 ret = pmu_queue_request(req);
327 if (req->nbytes != 4)
329 req->data[0] = PMU_READ_NVRAM;
330 req->data[1] = req->data[2];
331 req->data[2] = req->data[3];
334 req->reply[0] = CUDA_PACKET;
336 req->reply[2] = CUDA_GET_PRAM;
337 ret = pmu_queue_request(req);
340 if (req->nbytes != 5)
342 req->data[0] = PMU_WRITE_NVRAM;
343 req->data[1] = req->data[2];
344 req->data[2] = req->data[3];
345 req->data[3] = req->data[4];
348 req->reply[0] = CUDA_PACKET;
350 req->reply[2] = CUDA_SET_PRAM;
351 ret = pmu_queue_request(req);
356 for (i = req->nbytes - 1; i > 1; --i)
357 req->data[i+2] = req->data[i];
358 req->data[3] = req->nbytes - 2;
359 req->data[2] = pmu_adb_flags;
360 /*req->data[1] = req->data[1];*/
361 req->data[0] = PMU_ADB_CMD;
363 req->reply_expected = 1;
365 ret = pmu_queue_request(req);
375 while (!req->complete)
382 /* Enable/disable autopolling */
384 pmu_autopoll(int devs)
386 struct adb_request req;
388 if (!pmu_fully_inited) return -ENXIO;
392 pmu_request(&req, NULL, 5, PMU_ADB_CMD, 0, 0x86,
393 adb_dev_map >> 8, adb_dev_map);
396 pmu_request(&req, NULL, 1, PMU_ADB_POLL_OFF);
399 while (!req.complete)
404 /* Reset the ADB bus */
408 struct adb_request req;
410 int save_autopoll = adb_dev_map;
412 if (!pmu_fully_inited) return -ENXIO;
414 /* anyone got a better idea?? */
419 req.data[0] = PMU_ADB_CMD;
421 req.data[2] = 3; /* ADB_BUSRESET ??? */
425 req.reply_expected = 1;
426 if (pmu_queue_request(&req) != 0)
428 printk(KERN_ERR "pmu_adb_reset_bus: pmu_queue_request failed\n");
431 while (!req.complete)
434 while (!req.complete) {
436 printk(KERN_ERR "pmu_adb_reset_bus (reset): no response from PMU\n");
443 if (save_autopoll != 0)
444 pmu_autopoll(save_autopoll);
449 /* Construct and send a pmu request */
451 pmu_request(struct adb_request *req, void (*done)(struct adb_request *),
457 if (nbytes < 0 || nbytes > 32) {
458 printk(KERN_ERR "pmu_request: bad nbytes (%d)\n", nbytes);
462 req->nbytes = nbytes;
464 va_start(list, nbytes);
465 for (i = 0; i < nbytes; ++i)
466 req->data[i] = va_arg(list, int);
468 if (pmu_data_len[req->data[0]][1] != 0) {
469 req->reply[0] = ADB_RET_OK;
473 req->reply_expected = 0;
474 return pmu_queue_request(req);
478 pmu_queue_request(struct adb_request *req)
483 if (req->nbytes <= 0) {
487 nsend = pmu_data_len[req->data[0]][0];
488 if (nsend >= 0 && req->nbytes != nsend + 1) {
496 local_irq_save(flags);
498 if (current_req != 0) {
499 last_req->next = req;
504 if (pmu_state == idle)
508 local_irq_restore(flags);
515 via1[ACR] |= SR_CTRL;
517 via2[B] &= ~TREQ; /* assert TREQ */
525 via1[ACR] = (via1[ACR] | SR_EXT) & ~SR_OUT;
526 c = via1[SR]; /* resets SR */
534 struct adb_request *req;
536 /* assert pmu_state == idle */
537 /* get the packet to send */
538 local_irq_save(flags);
540 if (req == 0 || pmu_state != idle
541 || (req->reply_expected && req_awaiting_reply))
546 data_len = pmu_data_len[req->data[0]][0];
548 /* set the shift register to shift out and send a byte */
549 send_byte(req->data[0]);
552 local_irq_restore(flags);
560 local_irq_save(flags);
561 if (via1[IFR] & SR_INT) {
563 pmu_interrupt(IRQ_MAC_ADB_SR, NULL);
565 if (via1[IFR] & CB1_INT) {
567 pmu_interrupt(IRQ_MAC_ADB_CL, NULL);
569 local_irq_restore(flags);
573 pmu_interrupt(int irq, void *dev_id)
575 struct adb_request *req;
576 int timeout, bite = 0; /* to prevent compiler warning */
579 printk("pmu_interrupt: irq %d state %d acr %02X, b %02X data_index %d/%d adb_int_pending %d\n",
580 irq, pmu_state, (uint) via1[ACR], (uint) via2[B], data_index, data_len, adb_int_pending);
583 if (irq == IRQ_MAC_ADB_CL) { /* CB1 interrupt */
585 } else if (irq == IRQ_MAC_ADB_SR) { /* SR interrupt */
586 if (via2[B] & TACK) {
587 printk(KERN_DEBUG "PMU: SR_INT but ack still high! (%x)\n", via2[B]);
590 /* if reading grab the byte */
591 if ((via1[ACR] & SR_OUT) == 0) bite = via1[SR];
593 /* reset TREQ and wait for TACK to go high */
596 while (!(via2[B] & TACK)) {
598 printk(KERN_ERR "PMU not responding (!ack)\n");
608 data_len = req->nbytes - 1;
612 if (data_index <= data_len) {
613 send_byte(req->data[data_index++]);
617 data_len = pmu_data_len[req->data[0]][1];
620 current_req = req->next;
621 if (req->reply_expected)
622 req_awaiting_reply = req;
628 reply_ptr = req->reply + req->reply_len;
636 pmu_state = reading_intr;
637 reply_ptr = interrupt_data;
643 if (data_len == -1) {
646 printk(KERN_ERR "PMU: bad reply len %d\n",
649 reply_ptr[data_index++] = bite;
651 if (data_index < data_len) {
656 if (pmu_state == reading_intr) {
657 pmu_handle_data(interrupt_data, data_index);
660 current_req = req->next;
661 req->reply_len += data_index;
669 printk(KERN_ERR "pmu_interrupt: unknown state %d?\n",
674 if (pmu_state == idle) {
675 if (adb_int_pending) {
677 send_byte(PMU_INT_ACK);
679 } else if (current_req) {
685 printk("pmu_interrupt: exit state %d acr %02X, b %02X data_index %d/%d adb_int_pending %d\n",
686 pmu_state, (uint) via1[ACR], (uint) via2[B], data_index, data_len, adb_int_pending);
692 pmu_done(struct adb_request *req)
699 /* Interrupt data could be the result data from an ADB cmd */
701 pmu_handle_data(unsigned char *data, int len)
703 static int show_pmu_ints = 1;
710 if (data[0] & PMU_INT_ADB) {
711 if ((data[0] & PMU_INT_ADB_AUTO) == 0) {
712 struct adb_request *req = req_awaiting_reply;
714 printk(KERN_ERR "PMU: extra ADB reply\n");
717 req_awaiting_reply = NULL;
721 memcpy(req->reply, data + 1, len - 1);
722 req->reply_len = len - 1;
726 adb_input(data+1, len-1, 1);
729 if (data[0] == 0x08 && len == 3) {
730 /* sound/brightness buttons pressed */
731 pmu_set_brightness(data[1] >> 3);
733 } else if (show_pmu_ints
734 && !(data[0] == PMU_INT_TICK && len == 1)) {
736 printk(KERN_DEBUG "pmu intr");
737 for (i = 0; i < len; ++i)
738 printk(" %.2x", data[i]);
744 int backlight_level = -1;
745 int backlight_enabled = 0;
747 #define LEVEL_TO_BRIGHT(lev) ((lev) < 1? 0x7f: 0x4a - ((lev) << 1))
750 pmu_enable_backlight(int on)
752 struct adb_request req;
755 /* first call: get current backlight value */
756 if (backlight_level < 0) {
760 pmu_request(&req, NULL, 3, PMU_READ_NVRAM, 0x14, 0xe);
761 while (!req.complete)
763 printk(KERN_DEBUG "pmu: nvram returned bright: %d\n", (int)req.reply[1]);
764 backlight_level = req.reply[1];
767 backlight_enabled = 0;
771 pmu_request(&req, NULL, 2, PMU_BACKLIGHT_BRIGHT,
772 LEVEL_TO_BRIGHT(backlight_level));
773 while (!req.complete)
776 pmu_request(&req, NULL, 2, PMU_POWER_CTRL,
777 PMU_POW_BACKLIGHT | (on ? PMU_POW_ON : PMU_POW_OFF));
778 while (!req.complete)
780 backlight_enabled = on;
784 pmu_set_brightness(int level)
788 backlight_level = level;
789 bright = LEVEL_TO_BRIGHT(level);
790 if (!backlight_enabled)
792 if (bright_req_1.complete)
793 pmu_request(&bright_req_1, NULL, 2, PMU_BACKLIGHT_BRIGHT,
795 if (bright_req_2.complete)
796 pmu_request(&bright_req_2, NULL, 2, PMU_POWER_CTRL,
797 PMU_POW_BACKLIGHT | (bright < 0x7f ? PMU_POW_ON : PMU_POW_OFF));
801 pmu_enable_irled(int on)
803 struct adb_request req;
805 pmu_request(&req, NULL, 2, PMU_POWER_CTRL, PMU_POW_IRLED |
806 (on ? PMU_POW_ON : PMU_POW_OFF));
807 while (!req.complete)
812 set_volume(int level)
819 return (pmu_kind != PMU_UNKNOWN);