2 * Device driver for the IIsi-style ADB on some Mac LC and II-class machines
4 * Based on via-cuda.c and via-macii.c, as well as the original
5 * adb-bus.c, which in turn is somewhat influenced by (but uses no
6 * code from) the NetBSD HWDIRECT ADB code. Original IIsi driver work
7 * was done by Robert Thompson and integrated into the old style
8 * driver by Michael Schmitz.
10 * Original sources (c) Alan Cox, Paul Mackerras, and others.
12 * Rewritten for Unified ADB by David Huggins-Daines <dhd@debian.org>
14 * 7/13/2000- extensive changes by Andrew McPherson <andrew@macduff.dhs.org>
15 * Works about 30% of the time now.
18 #include <linux/types.h>
19 #include <linux/errno.h>
20 #include <linux/kernel.h>
21 #include <linux/sched.h>
22 #include <linux/adb.h>
23 #include <linux/cuda.h>
24 #include <linux/delay.h>
25 #include <linux/interrupt.h>
26 #include <asm/macintosh.h>
27 #include <asm/macints.h>
28 #include <asm/machw.h>
29 #include <asm/mac_via.h>
31 static volatile unsigned char *via
;
33 /* VIA registers - spaced 0x200 bytes apart - only the ones we actually use */
34 #define RS 0x200 /* skip between registers */
35 #define B 0 /* B-side data */
36 #define A RS /* A-side data */
37 #define DIRB (2*RS) /* B-side direction (1=output) */
38 #define DIRA (3*RS) /* A-side direction (1=output) */
39 #define SR (10*RS) /* Shift register */
40 #define ACR (11*RS) /* Auxiliary control register */
41 #define IFR (13*RS) /* Interrupt flag register */
42 #define IER (14*RS) /* Interrupt enable register */
44 /* Bits in B data register: all active low */
45 #define TREQ 0x08 /* Transfer request (input) */
46 #define TACK 0x10 /* Transfer acknowledge (output) */
47 #define TIP 0x20 /* Transfer in progress (output) */
48 #define ST_MASK 0x30 /* mask for selecting ADB state bits */
51 #define SR_CTRL 0x1c /* Shift register control bits */
52 #define SR_EXT 0x0c /* Shift on external clock */
53 #define SR_OUT 0x10 /* Shift out if 1 */
55 /* Bits in IFR and IER */
56 #define IER_SET 0x80 /* set bits in IER */
57 #define IER_CLR 0 /* clear bits in IER */
58 #define SR_INT 0x04 /* Shift register full/empty */
59 #define SR_DATA 0x08 /* Shift register data */
60 #define SR_CLOCK 0x10 /* Shift register clock */
64 #undef DEBUG_MACIISI_ADB
66 static struct adb_request
* current_req
= NULL
;
67 static struct adb_request
* last_req
= NULL
;
68 static unsigned char maciisi_rbuf
[16];
69 static unsigned char *reply_ptr
= NULL
;
70 static int data_index
;
71 static int reading_reply
;
76 static enum maciisi_state
{
82 static int maciisi_probe(void);
83 static int maciisi_init(void);
84 static int maciisi_send_request(struct adb_request
* req
, int sync
);
85 static void maciisi_sync(struct adb_request
*req
);
86 static int maciisi_write(struct adb_request
* req
);
87 static irqreturn_t
maciisi_interrupt(int irq
, void* arg
, struct pt_regs
* regs
);
88 static void maciisi_input(unsigned char *buf
, int nb
, struct pt_regs
*regs
);
89 static int maciisi_init_via(void);
90 static void maciisi_poll(void);
91 static int maciisi_start(void);
93 struct adb_driver via_maciisi_driver
= {
98 NULL
, /* maciisi_adb_autopoll, */
100 NULL
/* maciisi_reset_adb_bus */
106 if (macintosh_config
->adb_type
!= MAC_ADB_IISI
)
121 if ((err
= maciisi_init_via())) {
122 printk(KERN_ERR
"maciisi_init: maciisi_init_via() failed, code %d\n", err
);
127 if (request_irq(IRQ_MAC_ADB
, maciisi_interrupt
, IRQ_FLG_LOCK
| IRQ_FLG_FAST
,
128 "ADB", maciisi_interrupt
)) {
129 printk(KERN_ERR
"maciisi_init: can't get irq %d\n", IRQ_MAC_ADB
);
133 printk("adb: Mac IIsi driver v0.2 for Unified ADB.\n");
137 /* Flush data from the ADB controller */
141 int status
= via
[B
] & (TIP
|TREQ
);
144 #ifdef DEBUG_MACIISI_ADB
145 printk (KERN_DEBUG
"maciisi_stfu called with TREQ high!\n");
152 via
[IER
] = IER_CLR
| SR_INT
;
156 status
= via
[B
] & (TIP
|TREQ
);
158 if (!(status
& TREQ
))
164 int poll_timeout
= ADB_DELAY
* 5;
165 /* Poll for SR interrupt */
166 while (!(via
[IFR
] & SR_INT
) && poll_timeout
-- > 0)
167 status
= via
[B
] & (TIP
|TREQ
);
169 tmp
= via
[SR
]; /* Clear shift register */
170 #ifdef DEBUG_MACIISI_ADB
171 printk(KERN_DEBUG
"maciisi_stfu: status %x timeout %d data %x\n",
172 status
, poll_timeout
, tmp
);
188 via
[IER
] = IER_SET
| SR_INT
;
191 /* All specifically VIA-related initialization goes here */
193 maciisi_init_via(void)
197 /* Set the lines up. We want TREQ as input TACK|TIP as output */
198 via
[DIRB
] = (via
[DIRB
] | TACK
| TIP
) & ~TREQ
;
199 /* Shift register on input */
200 via
[ACR
] = (via
[ACR
] & ~SR_CTRL
) | SR_EXT
;
201 #ifdef DEBUG_MACIISI_ADB
202 printk(KERN_DEBUG
"maciisi_init_via: initial status %x\n", via
[B
] & (TIP
|TREQ
));
204 /* Wipe any pending data and int */
206 /* Enable keyboard interrupts */
207 via
[IER
] = IER_SET
| SR_INT
;
208 /* Set initial state: idle */
209 via
[B
] &= ~(TACK
|TIP
);
210 /* Clear interrupt bit */
213 for(i
= 0; i
< 60; i
++) {
221 printk(KERN_ERR
"maciisi_init_via: bus jam?\n");
223 maciisi_state
= idle
;
229 /* Send a request, possibly waiting for a reply */
231 maciisi_send_request(struct adb_request
* req
, int sync
)
235 #ifdef DEBUG_MACIISI_ADB
236 static int dump_packet
= 0;
244 #ifdef DEBUG_MACIISI_ADB
246 printk(KERN_DEBUG
"maciisi_send_request:");
247 for (i
= 0; i
< req
->nbytes
; i
++) {
248 printk(" %.2x", req
->data
[i
]);
250 printk(" sync %d\n", sync
);
254 req
->reply_expected
= 1;
256 i
= maciisi_write(req
);
259 /* Normally, if a packet requires syncing, that happens at the end of
260 * maciisi_send_request. But if the transfer fails, it will be restarted
261 * by maciisi_interrupt(). We use need_sync to tell maciisi_interrupt
262 * when to sync a packet that it sends out.
264 * Suggestions on a better way to do this are welcome.
266 if(i
== -EBUSY
&& sync
)
278 /* Poll the ADB chip until the request completes */
279 static void maciisi_sync(struct adb_request
*req
)
283 #ifdef DEBUG_MACIISI_ADB
284 printk(KERN_DEBUG
"maciisi_sync called\n");
287 /* If for some reason the ADB chip shuts up on us, we want to avoid an endless loop. */
288 while (!req
->complete
&& count
++ < 50) {
291 /* This could be BAD... when the ADB controller doesn't respond
292 * for this long, it's probably not coming back :-( */
293 if(count
>= 50) /* Hopefully shouldn't happen */
294 printk(KERN_ERR
"maciisi_send_request: poll timed out!\n");
297 /* Enqueue a request, and run the queue if possible */
299 maciisi_write(struct adb_request
* req
)
304 /* We will accept CUDA packets - the VIA sends them to us, so
305 it figures that we should be able to send them to it */
306 if (req
->nbytes
< 2 || req
->data
[0] > CUDA_PACKET
) {
307 printk(KERN_ERR
"maciisi_write: packet too small or not an ADB or CUDA packet\n");
316 local_irq_save(flags
);
319 last_req
->next
= req
;
325 if (maciisi_state
== idle
)
330 local_irq_restore(flags
);
336 #ifdef DEBUG_MACIISI_ADB
337 printk(KERN_DEBUG
"maciisi_write: would start, but state is %d\n", maciisi_state
);
339 local_irq_restore(flags
);
343 local_irq_restore(flags
);
351 struct adb_request
* req
;
354 #ifdef DEBUG_MACIISI_ADB
355 status
= via
[B
] & (TIP
| TREQ
);
357 printk(KERN_DEBUG
"maciisi_start called, state=%d, status=%x, ifr=%x\n", maciisi_state
, status
, via
[IFR
]);
360 if (maciisi_state
!= idle
) {
361 /* shouldn't happen */
362 printk(KERN_ERR
"maciisi_start: maciisi_start called when driver busy!\n");
370 status
= via
[B
] & (TIP
|TREQ
);
371 if (!(status
& TREQ
)) {
372 #ifdef DEBUG_MACIISI_ADB
373 printk(KERN_DEBUG
"maciisi_start: bus busy - aborting\n");
379 #ifdef DEBUG_MACIISI_ADB
380 printk(KERN_DEBUG
"maciisi_start: sending\n");
382 /* Set state to active */
388 /* Shift out and send */
390 via
[SR
] = req
->data
[0];
394 maciisi_state
= sending
;
404 local_irq_save(flags
);
405 if (via
[IFR
] & SR_INT
) {
406 maciisi_interrupt(0, 0, 0);
408 else /* avoid calling this function too quickly in a loop */
411 local_irq_restore(flags
);
414 /* Shift register interrupt - this is *supposed* to mean that the
415 register is either full or empty. In practice, I have no idea what
418 maciisi_interrupt(int irq
, void* arg
, struct pt_regs
* regs
)
421 struct adb_request
*req
;
422 #ifdef DEBUG_MACIISI_ADB
423 static int dump_reply
= 0;
428 local_irq_save(flags
);
430 status
= via
[B
] & (TIP
|TREQ
);
431 #ifdef DEBUG_MACIISI_ADB
432 printk(KERN_DEBUG
"state %d status %x ifr %x\n", maciisi_state
, status
, via
[IFR
]);
435 if (!(via
[IFR
] & SR_INT
)) {
436 /* Shouldn't happen, we hope */
437 printk(KERN_ERR
"maciisi_interrupt: called without interrupt flag set\n");
438 local_irq_restore(flags
);
442 /* Clear the interrupt */
443 /* via[IFR] = SR_INT; */
446 switch (maciisi_state
) {
449 printk(KERN_ERR
"maciisi_interrupt: state is idle but TIP asserted!\n");
455 /* Signal start of frame */
457 /* Clear the interrupt (throw this value on the floor, it's useless) */
459 /* ACK adb chip, high-low */
464 maciisi_state
= reading
;
466 reply_ptr
= current_req
->reply
;
468 reply_ptr
= maciisi_rbuf
;
478 if (!(status
& TREQ
)) {
480 printk(KERN_ERR
"maciisi_interrupt: send collision\n");
481 /* Set idle and input */
488 maciisi_state
= idle
;
490 /* process this now, because the IFR has been cleared */
496 if (data_index
>= req
->nbytes
) {
497 /* Sent the whole packet, put the bus back in idle state */
498 /* Shift in, we are about to read a reply (hopefully) */
504 maciisi_state
= idle
;
505 if (req
->reply_expected
) {
506 /* Note: only set this once we've
507 successfully sent the packet */
510 current_req
= req
->next
;
513 /* Do any queued requests now */
515 if(i
== 0 && need_sync
) {
516 /* Packet needs to be synced */
517 maciisi_sync(current_req
);
523 /* Sending more stuff */
527 via
[SR
] = req
->data
[data_index
++];
528 /* Signal 'byte ready' */
535 /* via[ACR] &= ~SR_OUT; */ /* Not in 2.2 */
536 if (reply_len
++ > 16) {
537 printk(KERN_ERR
"maciisi_interrupt: reply too long, aborting read\n");
540 via
[B
] &= ~(TACK
|TIP
);
541 maciisi_state
= idle
;
543 if(i
== 0 && need_sync
) {
544 /* Packet needs to be synced */
545 maciisi_sync(current_req
);
552 *reply_ptr
++ = via
[SR
];
553 status
= via
[B
] & (TIP
|TREQ
);
558 if (!(status
& TREQ
))
559 break; /* more stuff to deal with */
563 tmp
= via
[SR
]; /* That's what happens in 2.2 */
564 udelay(ADB_DELAY
); /* Give controller time to recover */
566 /* end of packet, deal with it */
569 req
->reply_len
= reply_ptr
- req
->reply
;
570 if (req
->data
[0] == ADB_PACKET
) {
571 /* Have to adjust the reply from ADB commands */
572 if (req
->reply_len
<= 2 || (req
->reply
[1] & 2) != 0) {
573 /* the 0x2 bit indicates no response */
576 /* leave just the command and result bytes in the reply */
578 memmove(req
->reply
, req
->reply
+ 2, req
->reply_len
);
581 #ifdef DEBUG_MACIISI_ADB
584 printk(KERN_DEBUG
"maciisi_interrupt: reply is ");
585 for (i
= 0; i
< req
->reply_len
; ++i
)
586 printk(" %.2x", req
->reply
[i
]);
591 current_req
= req
->next
;
594 /* Obviously, we got it */
597 maciisi_input(maciisi_rbuf
, reply_ptr
- maciisi_rbuf
, regs
);
599 maciisi_state
= idle
;
600 status
= via
[B
] & (TIP
|TREQ
);
601 if (!(status
& TREQ
)) {
602 /* Timeout?! More likely, another packet coming in already */
603 #ifdef DEBUG_MACIISI_ADB
604 printk(KERN_DEBUG
"extra data after packet: status %x ifr %x\n",
611 maciisi_state
= reading
;
613 reply_ptr
= maciisi_rbuf
;
615 /* Process the packet now */
619 /* We used to do this... but the controller might actually have data for us */
620 /* maciisi_stfu(); */
623 /* Do any queued requests now if possible */
625 if(i
== 0 && need_sync
) {
626 /* Packet needs to be synced */
627 maciisi_sync(current_req
);
635 printk("maciisi_interrupt: unknown maciisi_state %d?\n", maciisi_state
);
637 local_irq_restore(flags
);
642 maciisi_input(unsigned char *buf
, int nb
, struct pt_regs
*regs
)
644 #ifdef DEBUG_MACIISI_ADB
650 adb_input(buf
+2, nb
-2, regs
, buf
[1] & 0x40);
653 #ifdef DEBUG_MACIISI_ADB
654 printk(KERN_DEBUG
"data from IIsi ADB (%d bytes):", nb
);
655 for (i
= 0; i
< nb
; ++i
)
656 printk(" %.2x", buf
[i
]);