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/init.h>
29 #include <linux/interrupt.h>
31 #include <linux/adb.h>
32 #include <linux/pmu.h>
33 #include <linux/cuda.h>
35 #include <asm/macintosh.h>
36 #include <asm/macints.h>
37 #include <asm/mac_via.h>
39 #include <asm/pgtable.h>
41 #include <linux/uaccess.h>
43 /* Misc minor number allocated for /dev/pmu */
46 /* VIA registers - spaced 0x200 bytes apart */
47 #define RS 0x200 /* skip between registers */
48 #define B 0 /* B-side data */
49 #define A RS /* A-side data */
50 #define DIRB (2*RS) /* B-side direction (1=output) */
51 #define DIRA (3*RS) /* A-side direction (1=output) */
52 #define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */
53 #define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */
54 #define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */
55 #define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */
56 #define T2CL (8*RS) /* Timer 2 ctr/latch (low 8 bits) */
57 #define T2CH (9*RS) /* Timer 2 counter (high 8 bits) */
58 #define SR (10*RS) /* Shift register */
59 #define ACR (11*RS) /* Auxiliary control register */
60 #define PCR (12*RS) /* Peripheral control register */
61 #define IFR (13*RS) /* Interrupt flag register */
62 #define IER (14*RS) /* Interrupt enable register */
63 #define ANH (15*RS) /* A-side data, no handshake */
65 /* Bits in B data register: both active low */
66 #define TACK 0x02 /* Transfer acknowledge (input) */
67 #define TREQ 0x04 /* Transfer request (output) */
70 #define SR_CTRL 0x1c /* Shift register control bits */
71 #define SR_EXT 0x0c /* Shift on external clock */
72 #define SR_OUT 0x10 /* Shift out if 1 */
74 /* Bits in IFR and IER */
75 #define SR_INT 0x04 /* Shift register full/empty */
76 #define CB1_INT 0x10 /* transition on CB1 input */
78 static enum pmu_state
{
86 static struct adb_request
*current_req
;
87 static struct adb_request
*last_req
;
88 static struct adb_request
*req_awaiting_reply
;
89 static unsigned char interrupt_data
[32];
90 static unsigned char *reply_ptr
;
91 static int data_index
;
93 static int adb_int_pending
;
94 static int pmu_adb_flags
;
95 static int adb_dev_map
;
96 static struct adb_request bright_req_1
, bright_req_2
, bright_req_3
;
97 static int pmu_kind
= PMU_UNKNOWN
;
98 static int pmu_fully_inited
;
102 static int pmu_probe(void);
103 static int pmu_init(void);
104 static void pmu_start(void);
105 static irqreturn_t
pmu_interrupt(int irq
, void *arg
);
106 static int pmu_send_request(struct adb_request
*req
, int sync
);
107 static int pmu_autopoll(int devs
);
109 static int pmu_reset_bus(void);
111 static void pmu_start(void);
112 static void send_byte(int x
);
113 static void recv_byte(void);
114 static void pmu_done(struct adb_request
*req
);
115 static void pmu_handle_data(unsigned char *data
, int len
);
116 static void set_volume(int level
);
117 static void pmu_enable_backlight(int on
);
118 static void pmu_set_brightness(int level
);
120 struct adb_driver via_pmu_driver
= {
131 * This table indicates for each PMU opcode:
132 * - the number of data bytes to be sent with the command, or -1
133 * if a length byte should be sent,
134 * - the number of response bytes which the PMU will return, or
135 * -1 if it will send a length byte.
137 static s8 pmu_data_len
[256][2] = {
138 /* 0 1 2 3 4 5 6 7 */
139 /*00*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
140 /*08*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
141 /*10*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
142 /*18*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0, 0},
143 /*20*/ {-1, 0},{ 0, 0},{ 2, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},
144 /*28*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{ 0,-1},
145 /*30*/ { 4, 0},{20, 0},{-1, 0},{ 3, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
146 /*38*/ { 0, 4},{ 0,20},{ 2,-1},{ 2, 1},{ 3,-1},{-1,-1},{-1,-1},{ 4, 0},
147 /*40*/ { 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
148 /*48*/ { 0, 1},{ 0, 1},{-1,-1},{ 1, 0},{ 1, 0},{-1,-1},{-1,-1},{-1,-1},
149 /*50*/ { 1, 0},{ 0, 0},{ 2, 0},{ 2, 0},{-1, 0},{ 1, 0},{ 3, 0},{ 1, 0},
150 /*58*/ { 0, 1},{ 1, 0},{ 0, 2},{ 0, 2},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},
151 /*60*/ { 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
152 /*68*/ { 0, 3},{ 0, 3},{ 0, 2},{ 0, 8},{ 0,-1},{ 0,-1},{-1,-1},{-1,-1},
153 /*70*/ { 1, 0},{ 1, 0},{ 1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
154 /*78*/ { 0,-1},{ 0,-1},{-1,-1},{-1,-1},{-1,-1},{ 5, 1},{ 4, 1},{ 4, 1},
155 /*80*/ { 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
156 /*88*/ { 0, 5},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
157 /*90*/ { 1, 0},{ 2, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
158 /*98*/ { 0, 1},{ 0, 1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
159 /*a0*/ { 2, 0},{ 2, 0},{ 2, 0},{ 4, 0},{-1, 0},{ 0, 0},{-1, 0},{-1, 0},
160 /*a8*/ { 1, 1},{ 1, 0},{ 3, 0},{ 2, 0},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
161 /*b0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
162 /*b8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
163 /*c0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
164 /*c8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
165 /*d0*/ { 0, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
166 /*d8*/ { 1, 1},{ 1, 1},{-1,-1},{-1,-1},{ 0, 1},{ 0,-1},{-1,-1},{-1,-1},
167 /*e0*/ {-1, 0},{ 4, 0},{ 0, 1},{-1, 0},{-1, 0},{ 4, 0},{-1, 0},{-1, 0},
168 /*e8*/ { 3,-1},{-1,-1},{ 0, 1},{-1,-1},{ 0,-1},{-1,-1},{-1,-1},{ 0, 0},
169 /*f0*/ {-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},{-1, 0},
170 /*f8*/ {-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},
175 if (macintosh_config
->adb_type
== MAC_ADB_PB1
) {
176 pmu_kind
= PMU_68K_V1
;
177 } else if (macintosh_config
->adb_type
== MAC_ADB_PB2
) {
178 pmu_kind
= PMU_68K_V2
;
192 volatile struct adb_request req
;
194 via2
[B
] |= TREQ
; /* negate TREQ */
195 via2
[DIRB
] = (via2
[DIRB
] | TREQ
) & ~TACK
; /* TACK in, TREQ out */
197 pmu_request((struct adb_request
*) &req
, NULL
, 2, PMU_SET_INTR_MASK
, PMU_INT_ADB
);
199 while (!req
.complete
) {
201 printk(KERN_ERR
"pmu_init: no response from PMU\n");
208 /* ack all pending interrupts */
210 interrupt_data
[0] = 1;
211 while (interrupt_data
[0] || pmu_state
!= idle
) {
213 printk(KERN_ERR
"pmu_init: timed out acking intrs\n");
216 if (pmu_state
== idle
) {
218 pmu_interrupt(0, NULL
);
224 pmu_request((struct adb_request
*) &req
, NULL
, 2, PMU_SET_INTR_MASK
,
225 PMU_INT_ADB_AUTO
|PMU_INT_SNDBRT
|PMU_INT_ADB
);
227 while (!req
.complete
) {
229 printk(KERN_ERR
"pmu_init: no response from PMU\n");
236 bright_req_1
.complete
= 1;
237 bright_req_2
.complete
= 1;
238 bright_req_3
.complete
= 1;
240 if (request_irq(IRQ_MAC_ADB_SR
, pmu_interrupt
, 0, "pmu-shift",
242 printk(KERN_ERR
"pmu_init: can't get irq %d\n",
246 if (request_irq(IRQ_MAC_ADB_CL
, pmu_interrupt
, 0, "pmu-clock",
248 printk(KERN_ERR
"pmu_init: can't get irq %d\n",
250 free_irq(IRQ_MAC_ADB_SR
, pmu_interrupt
);
254 pmu_fully_inited
= 1;
256 /* Enable backlight */
257 pmu_enable_backlight(1);
259 printk("adb: PMU 68K driver v0.5 for Unified ADB.\n");
270 /* Send an ADB command */
272 pmu_send_request(struct adb_request
*req
, int sync
)
276 if (!pmu_fully_inited
)
284 switch (req
->data
[0]) {
286 for (i
= 0; i
< req
->nbytes
- 1; ++i
)
287 req
->data
[i
] = req
->data
[i
+1];
289 if (pmu_data_len
[req
->data
[0]][1] != 0) {
290 req
->reply
[0] = ADB_RET_OK
;
294 ret
= pmu_queue_request(req
);
297 switch (req
->data
[1]) {
299 if (req
->nbytes
!= 2)
301 req
->data
[0] = PMU_READ_RTC
;
304 req
->reply
[0] = CUDA_PACKET
;
306 req
->reply
[2] = CUDA_GET_TIME
;
307 ret
= pmu_queue_request(req
);
310 if (req
->nbytes
!= 6)
312 req
->data
[0] = PMU_SET_RTC
;
314 for (i
= 1; i
<= 4; ++i
)
315 req
->data
[i
] = req
->data
[i
+1];
317 req
->reply
[0] = CUDA_PACKET
;
319 req
->reply
[2] = CUDA_SET_TIME
;
320 ret
= pmu_queue_request(req
);
323 if (req
->nbytes
!= 4)
325 req
->data
[0] = PMU_READ_NVRAM
;
326 req
->data
[1] = req
->data
[2];
327 req
->data
[2] = req
->data
[3];
330 req
->reply
[0] = CUDA_PACKET
;
332 req
->reply
[2] = CUDA_GET_PRAM
;
333 ret
= pmu_queue_request(req
);
336 if (req
->nbytes
!= 5)
338 req
->data
[0] = PMU_WRITE_NVRAM
;
339 req
->data
[1] = req
->data
[2];
340 req
->data
[2] = req
->data
[3];
341 req
->data
[3] = req
->data
[4];
344 req
->reply
[0] = CUDA_PACKET
;
346 req
->reply
[2] = CUDA_SET_PRAM
;
347 ret
= pmu_queue_request(req
);
352 for (i
= req
->nbytes
- 1; i
> 1; --i
)
353 req
->data
[i
+2] = req
->data
[i
];
354 req
->data
[3] = req
->nbytes
- 2;
355 req
->data
[2] = pmu_adb_flags
;
356 /*req->data[1] = req->data[1];*/
357 req
->data
[0] = PMU_ADB_CMD
;
359 req
->reply_expected
= 1;
361 ret
= pmu_queue_request(req
);
371 while (!req
->complete
)
378 /* Enable/disable autopolling */
380 pmu_autopoll(int devs
)
382 struct adb_request req
;
384 if (!pmu_fully_inited
) return -ENXIO
;
388 pmu_request(&req
, NULL
, 5, PMU_ADB_CMD
, 0, 0x86,
389 adb_dev_map
>> 8, adb_dev_map
);
392 pmu_request(&req
, NULL
, 1, PMU_ADB_POLL_OFF
);
395 while (!req
.complete
)
400 /* Reset the ADB bus */
404 struct adb_request req
;
406 int save_autopoll
= adb_dev_map
;
408 if (!pmu_fully_inited
) return -ENXIO
;
410 /* anyone got a better idea?? */
415 req
.data
[0] = PMU_ADB_CMD
;
417 req
.data
[2] = 3; /* ADB_BUSRESET ??? */
421 req
.reply_expected
= 1;
422 if (pmu_queue_request(&req
) != 0)
424 printk(KERN_ERR
"pmu_adb_reset_bus: pmu_queue_request failed\n");
427 while (!req
.complete
)
430 while (!req
.complete
) {
432 printk(KERN_ERR
"pmu_adb_reset_bus (reset): no response from PMU\n");
439 if (save_autopoll
!= 0)
440 pmu_autopoll(save_autopoll
);
445 /* Construct and send a pmu request */
447 pmu_request(struct adb_request
*req
, void (*done
)(struct adb_request
*),
453 if (nbytes
< 0 || nbytes
> 32) {
454 printk(KERN_ERR
"pmu_request: bad nbytes (%d)\n", nbytes
);
458 req
->nbytes
= nbytes
;
460 va_start(list
, nbytes
);
461 for (i
= 0; i
< nbytes
; ++i
)
462 req
->data
[i
] = va_arg(list
, int);
464 if (pmu_data_len
[req
->data
[0]][1] != 0) {
465 req
->reply
[0] = ADB_RET_OK
;
469 req
->reply_expected
= 0;
470 return pmu_queue_request(req
);
474 pmu_queue_request(struct adb_request
*req
)
479 if (req
->nbytes
<= 0) {
483 nsend
= pmu_data_len
[req
->data
[0]][0];
484 if (nsend
>= 0 && req
->nbytes
!= nsend
+ 1) {
492 local_irq_save(flags
);
494 if (current_req
!= 0) {
495 last_req
->next
= req
;
500 if (pmu_state
== idle
)
504 local_irq_restore(flags
);
511 via1
[ACR
] |= SR_CTRL
;
513 via2
[B
] &= ~TREQ
; /* assert TREQ */
521 via1
[ACR
] = (via1
[ACR
] | SR_EXT
) & ~SR_OUT
;
522 c
= via1
[SR
]; /* resets SR */
530 struct adb_request
*req
;
532 /* assert pmu_state == idle */
533 /* get the packet to send */
534 local_irq_save(flags
);
536 if (req
== 0 || pmu_state
!= idle
537 || (req
->reply_expected
&& req_awaiting_reply
))
542 data_len
= pmu_data_len
[req
->data
[0]][0];
544 /* set the shift register to shift out and send a byte */
545 send_byte(req
->data
[0]);
548 local_irq_restore(flags
);
556 local_irq_save(flags
);
557 if (via1
[IFR
] & SR_INT
) {
559 pmu_interrupt(IRQ_MAC_ADB_SR
, NULL
);
561 if (via1
[IFR
] & CB1_INT
) {
563 pmu_interrupt(IRQ_MAC_ADB_CL
, NULL
);
565 local_irq_restore(flags
);
569 pmu_interrupt(int irq
, void *dev_id
)
571 struct adb_request
*req
;
572 int timeout
, bite
= 0; /* to prevent compiler warning */
575 printk("pmu_interrupt: irq %d state %d acr %02X, b %02X data_index %d/%d adb_int_pending %d\n",
576 irq
, pmu_state
, (uint
) via1
[ACR
], (uint
) via2
[B
], data_index
, data_len
, adb_int_pending
);
579 if (irq
== IRQ_MAC_ADB_CL
) { /* CB1 interrupt */
581 } else if (irq
== IRQ_MAC_ADB_SR
) { /* SR interrupt */
582 if (via2
[B
] & TACK
) {
583 printk(KERN_DEBUG
"PMU: SR_INT but ack still high! (%x)\n", via2
[B
]);
586 /* if reading grab the byte */
587 if ((via1
[ACR
] & SR_OUT
) == 0) bite
= via1
[SR
];
589 /* reset TREQ and wait for TACK to go high */
592 while (!(via2
[B
] & TACK
)) {
594 printk(KERN_ERR
"PMU not responding (!ack)\n");
604 data_len
= req
->nbytes
- 1;
608 if (data_index
<= data_len
) {
609 send_byte(req
->data
[data_index
++]);
613 data_len
= pmu_data_len
[req
->data
[0]][1];
616 current_req
= req
->next
;
617 if (req
->reply_expected
)
618 req_awaiting_reply
= req
;
624 reply_ptr
= req
->reply
+ req
->reply_len
;
632 pmu_state
= reading_intr
;
633 reply_ptr
= interrupt_data
;
639 if (data_len
== -1) {
642 printk(KERN_ERR
"PMU: bad reply len %d\n",
645 reply_ptr
[data_index
++] = bite
;
647 if (data_index
< data_len
) {
652 if (pmu_state
== reading_intr
) {
653 pmu_handle_data(interrupt_data
, data_index
);
656 current_req
= req
->next
;
657 req
->reply_len
+= data_index
;
665 printk(KERN_ERR
"pmu_interrupt: unknown state %d?\n",
670 if (pmu_state
== idle
) {
671 if (adb_int_pending
) {
673 send_byte(PMU_INT_ACK
);
675 } else if (current_req
) {
681 printk("pmu_interrupt: exit state %d acr %02X, b %02X data_index %d/%d adb_int_pending %d\n",
682 pmu_state
, (uint
) via1
[ACR
], (uint
) via2
[B
], data_index
, data_len
, adb_int_pending
);
688 pmu_done(struct adb_request
*req
)
695 /* Interrupt data could be the result data from an ADB cmd */
697 pmu_handle_data(unsigned char *data
, int len
)
699 static int show_pmu_ints
= 1;
706 if (data
[0] & PMU_INT_ADB
) {
707 if ((data
[0] & PMU_INT_ADB_AUTO
) == 0) {
708 struct adb_request
*req
= req_awaiting_reply
;
710 printk(KERN_ERR
"PMU: extra ADB reply\n");
713 req_awaiting_reply
= NULL
;
717 memcpy(req
->reply
, data
+ 1, len
- 1);
718 req
->reply_len
= len
- 1;
722 adb_input(data
+1, len
-1, 1);
725 if (data
[0] == 0x08 && len
== 3) {
726 /* sound/brightness buttons pressed */
727 pmu_set_brightness(data
[1] >> 3);
729 } else if (show_pmu_ints
730 && !(data
[0] == PMU_INT_TICK
&& len
== 1)) {
732 printk(KERN_DEBUG
"pmu intr");
733 for (i
= 0; i
< len
; ++i
)
734 printk(" %.2x", data
[i
]);
740 static int backlight_level
= -1;
741 static int backlight_enabled
= 0;
743 #define LEVEL_TO_BRIGHT(lev) ((lev) < 1? 0x7f: 0x4a - ((lev) << 1))
746 pmu_enable_backlight(int on
)
748 struct adb_request req
;
751 /* first call: get current backlight value */
752 if (backlight_level
< 0) {
756 pmu_request(&req
, NULL
, 3, PMU_READ_NVRAM
, 0x14, 0xe);
757 while (!req
.complete
)
759 printk(KERN_DEBUG
"pmu: nvram returned bright: %d\n", (int)req
.reply
[1]);
760 backlight_level
= req
.reply
[1];
763 backlight_enabled
= 0;
767 pmu_request(&req
, NULL
, 2, PMU_BACKLIGHT_BRIGHT
,
768 LEVEL_TO_BRIGHT(backlight_level
));
769 while (!req
.complete
)
772 pmu_request(&req
, NULL
, 2, PMU_POWER_CTRL
,
773 PMU_POW_BACKLIGHT
| (on
? PMU_POW_ON
: PMU_POW_OFF
));
774 while (!req
.complete
)
776 backlight_enabled
= on
;
780 pmu_set_brightness(int level
)
784 backlight_level
= level
;
785 bright
= LEVEL_TO_BRIGHT(level
);
786 if (!backlight_enabled
)
788 if (bright_req_1
.complete
)
789 pmu_request(&bright_req_1
, NULL
, 2, PMU_BACKLIGHT_BRIGHT
,
791 if (bright_req_2
.complete
)
792 pmu_request(&bright_req_2
, NULL
, 2, PMU_POWER_CTRL
,
793 PMU_POW_BACKLIGHT
| (bright
< 0x7f ? PMU_POW_ON
: PMU_POW_OFF
));
797 pmu_enable_irled(int on
)
799 struct adb_request req
;
801 pmu_request(&req
, NULL
, 2, PMU_POWER_CTRL
, PMU_POW_IRLED
|
802 (on
? PMU_POW_ON
: PMU_POW_OFF
));
803 while (!req
.complete
)
808 set_volume(int level
)
815 return (pmu_kind
!= PMU_UNKNOWN
);