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/adb.h>
22 #include <linux/cuda.h>
23 #include <linux/delay.h>
24 #include <linux/interrupt.h>
25 #include <asm/macintosh.h>
26 #include <asm/macints.h>
27 #include <asm/mac_via.h>
29 static volatile unsigned char *via;
31 /* VIA registers - spaced 0x200 bytes apart - only the ones we actually use */
32 #define RS 0x200 /* skip between registers */
33 #define B 0 /* B-side data */
34 #define A RS /* A-side data */
35 #define DIRB (2*RS) /* B-side direction (1=output) */
36 #define DIRA (3*RS) /* A-side direction (1=output) */
37 #define SR (10*RS) /* Shift register */
38 #define ACR (11*RS) /* Auxiliary control register */
39 #define IFR (13*RS) /* Interrupt flag register */
40 #define IER (14*RS) /* Interrupt enable register */
42 /* Bits in B data register: all active low */
43 #define TREQ 0x08 /* Transfer request (input) */
44 #define TACK 0x10 /* Transfer acknowledge (output) */
45 #define TIP 0x20 /* Transfer in progress (output) */
46 #define ST_MASK 0x30 /* mask for selecting ADB state bits */
49 #define SR_CTRL 0x1c /* Shift register control bits */
50 #define SR_EXT 0x0c /* Shift on external clock */
51 #define SR_OUT 0x10 /* Shift out if 1 */
53 /* Bits in IFR and IER */
54 #define IER_SET 0x80 /* set bits in IER */
55 #define IER_CLR 0 /* clear bits in IER */
56 #define SR_INT 0x04 /* Shift register full/empty */
57 #define SR_DATA 0x08 /* Shift register data */
58 #define SR_CLOCK 0x10 /* Shift register clock */
62 #undef DEBUG_MACIISI_ADB
64 static struct adb_request* current_req;
65 static struct adb_request* last_req;
66 static unsigned char maciisi_rbuf[16];
67 static unsigned char *reply_ptr;
68 static int data_index;
69 static int reading_reply;
74 static enum maciisi_state {
80 static int maciisi_probe(void);
81 static int maciisi_init(void);
82 static int maciisi_send_request(struct adb_request* req, int sync);
83 static void maciisi_sync(struct adb_request *req);
84 static int maciisi_write(struct adb_request* req);
85 static irqreturn_t maciisi_interrupt(int irq, void* arg);
86 static void maciisi_input(unsigned char *buf, int nb);
87 static int maciisi_init_via(void);
88 static void maciisi_poll(void);
89 static int maciisi_start(void);
91 struct adb_driver via_maciisi_driver = {
96 NULL, /* maciisi_adb_autopoll, */
98 NULL /* maciisi_reset_adb_bus */
104 if (macintosh_config->adb_type != MAC_ADB_IISI)
119 if ((err = maciisi_init_via())) {
120 printk(KERN_ERR "maciisi_init: maciisi_init_via() failed, code %d\n", err);
125 if (request_irq(IRQ_MAC_ADB, maciisi_interrupt, IRQ_FLG_LOCK | IRQ_FLG_FAST,
126 "ADB", maciisi_interrupt)) {
127 printk(KERN_ERR "maciisi_init: can't get irq %d\n", IRQ_MAC_ADB);
131 printk("adb: Mac IIsi driver v0.2 for Unified ADB.\n");
135 /* Flush data from the ADB controller */
139 int status = via[B] & (TIP|TREQ);
142 #ifdef DEBUG_MACIISI_ADB
143 printk (KERN_DEBUG "maciisi_stfu called with TREQ high!\n");
150 via[IER] = IER_CLR | SR_INT;
154 status = via[B] & (TIP|TREQ);
156 if (!(status & TREQ))
162 int poll_timeout = ADB_DELAY * 5;
163 /* Poll for SR interrupt */
164 while (!(via[IFR] & SR_INT) && poll_timeout-- > 0)
165 status = via[B] & (TIP|TREQ);
167 tmp = via[SR]; /* Clear shift register */
168 #ifdef DEBUG_MACIISI_ADB
169 printk(KERN_DEBUG "maciisi_stfu: status %x timeout %d data %x\n",
170 status, poll_timeout, tmp);
186 via[IER] = IER_SET | SR_INT;
189 /* All specifically VIA-related initialization goes here */
191 maciisi_init_via(void)
195 /* Set the lines up. We want TREQ as input TACK|TIP as output */
196 via[DIRB] = (via[DIRB] | TACK | TIP) & ~TREQ;
197 /* Shift register on input */
198 via[ACR] = (via[ACR] & ~SR_CTRL) | SR_EXT;
199 #ifdef DEBUG_MACIISI_ADB
200 printk(KERN_DEBUG "maciisi_init_via: initial status %x\n", via[B] & (TIP|TREQ));
202 /* Wipe any pending data and int */
204 /* Enable keyboard interrupts */
205 via[IER] = IER_SET | SR_INT;
206 /* Set initial state: idle */
207 via[B] &= ~(TACK|TIP);
208 /* Clear interrupt bit */
211 for(i = 0; i < 60; i++) {
219 printk(KERN_ERR "maciisi_init_via: bus jam?\n");
221 maciisi_state = idle;
227 /* Send a request, possibly waiting for a reply */
229 maciisi_send_request(struct adb_request* req, int sync)
233 #ifdef DEBUG_MACIISI_ADB
234 static int dump_packet = 0;
242 #ifdef DEBUG_MACIISI_ADB
244 printk(KERN_DEBUG "maciisi_send_request:");
245 for (i = 0; i < req->nbytes; i++) {
246 printk(" %.2x", req->data[i]);
248 printk(" sync %d\n", sync);
252 req->reply_expected = 1;
254 i = maciisi_write(req);
257 /* Normally, if a packet requires syncing, that happens at the end of
258 * maciisi_send_request. But if the transfer fails, it will be restarted
259 * by maciisi_interrupt(). We use need_sync to tell maciisi_interrupt
260 * when to sync a packet that it sends out.
262 * Suggestions on a better way to do this are welcome.
264 if(i == -EBUSY && sync)
276 /* Poll the ADB chip until the request completes */
277 static void maciisi_sync(struct adb_request *req)
281 #ifdef DEBUG_MACIISI_ADB
282 printk(KERN_DEBUG "maciisi_sync called\n");
285 /* If for some reason the ADB chip shuts up on us, we want to avoid an endless loop. */
286 while (!req->complete && count++ < 50) {
289 /* This could be BAD... when the ADB controller doesn't respond
290 * for this long, it's probably not coming back :-( */
291 if(count >= 50) /* Hopefully shouldn't happen */
292 printk(KERN_ERR "maciisi_send_request: poll timed out!\n");
296 maciisi_request(struct adb_request *req, void (*done)(struct adb_request *),
302 req->nbytes = nbytes;
304 req->reply_expected = 0;
305 va_start(list, nbytes);
306 for (i = 0; i < nbytes; i++)
307 req->data[i++] = va_arg(list, int);
310 return maciisi_send_request(req, 1);
313 /* Enqueue a request, and run the queue if possible */
315 maciisi_write(struct adb_request* req)
320 /* We will accept CUDA packets - the VIA sends them to us, so
321 it figures that we should be able to send them to it */
322 if (req->nbytes < 2 || req->data[0] > CUDA_PACKET) {
323 printk(KERN_ERR "maciisi_write: packet too small or not an ADB or CUDA packet\n");
332 local_irq_save(flags);
335 last_req->next = req;
341 if (maciisi_state == idle)
346 local_irq_restore(flags);
352 #ifdef DEBUG_MACIISI_ADB
353 printk(KERN_DEBUG "maciisi_write: would start, but state is %d\n", maciisi_state);
355 local_irq_restore(flags);
359 local_irq_restore(flags);
367 struct adb_request* req;
370 #ifdef DEBUG_MACIISI_ADB
371 status = via[B] & (TIP | TREQ);
373 printk(KERN_DEBUG "maciisi_start called, state=%d, status=%x, ifr=%x\n", maciisi_state, status, via[IFR]);
376 if (maciisi_state != idle) {
377 /* shouldn't happen */
378 printk(KERN_ERR "maciisi_start: maciisi_start called when driver busy!\n");
386 status = via[B] & (TIP|TREQ);
387 if (!(status & TREQ)) {
388 #ifdef DEBUG_MACIISI_ADB
389 printk(KERN_DEBUG "maciisi_start: bus busy - aborting\n");
395 #ifdef DEBUG_MACIISI_ADB
396 printk(KERN_DEBUG "maciisi_start: sending\n");
398 /* Set state to active */
404 /* Shift out and send */
406 via[SR] = req->data[0];
410 maciisi_state = sending;
420 local_irq_save(flags);
421 if (via[IFR] & SR_INT) {
422 maciisi_interrupt(0, NULL);
424 else /* avoid calling this function too quickly in a loop */
427 local_irq_restore(flags);
430 /* Shift register interrupt - this is *supposed* to mean that the
431 register is either full or empty. In practice, I have no idea what
434 maciisi_interrupt(int irq, void* arg)
437 struct adb_request *req;
438 #ifdef DEBUG_MACIISI_ADB
439 static int dump_reply = 0;
444 local_irq_save(flags);
446 status = via[B] & (TIP|TREQ);
447 #ifdef DEBUG_MACIISI_ADB
448 printk(KERN_DEBUG "state %d status %x ifr %x\n", maciisi_state, status, via[IFR]);
451 if (!(via[IFR] & SR_INT)) {
452 /* Shouldn't happen, we hope */
453 printk(KERN_ERR "maciisi_interrupt: called without interrupt flag set\n");
454 local_irq_restore(flags);
458 /* Clear the interrupt */
459 /* via[IFR] = SR_INT; */
462 switch (maciisi_state) {
465 printk(KERN_ERR "maciisi_interrupt: state is idle but TIP asserted!\n");
471 /* Signal start of frame */
473 /* Clear the interrupt (throw this value on the floor, it's useless) */
475 /* ACK adb chip, high-low */
480 maciisi_state = reading;
482 reply_ptr = current_req->reply;
484 reply_ptr = maciisi_rbuf;
494 if (!(status & TREQ)) {
496 printk(KERN_ERR "maciisi_interrupt: send collision\n");
497 /* Set idle and input */
504 maciisi_state = idle;
506 /* process this now, because the IFR has been cleared */
512 if (data_index >= req->nbytes) {
513 /* Sent the whole packet, put the bus back in idle state */
514 /* Shift in, we are about to read a reply (hopefully) */
520 maciisi_state = idle;
521 if (req->reply_expected) {
522 /* Note: only set this once we've
523 successfully sent the packet */
526 current_req = req->next;
529 /* Do any queued requests now */
531 if(i == 0 && need_sync) {
532 /* Packet needs to be synced */
533 maciisi_sync(current_req);
539 /* Sending more stuff */
543 via[SR] = req->data[data_index++];
544 /* Signal 'byte ready' */
551 /* via[ACR] &= ~SR_OUT; */ /* Not in 2.2 */
552 if (reply_len++ > 16) {
553 printk(KERN_ERR "maciisi_interrupt: reply too long, aborting read\n");
556 via[B] &= ~(TACK|TIP);
557 maciisi_state = idle;
559 if(i == 0 && need_sync) {
560 /* Packet needs to be synced */
561 maciisi_sync(current_req);
568 *reply_ptr++ = via[SR];
569 status = via[B] & (TIP|TREQ);
574 if (!(status & TREQ))
575 break; /* more stuff to deal with */
579 tmp = via[SR]; /* That's what happens in 2.2 */
580 udelay(ADB_DELAY); /* Give controller time to recover */
582 /* end of packet, deal with it */
585 req->reply_len = reply_ptr - req->reply;
586 if (req->data[0] == ADB_PACKET) {
587 /* Have to adjust the reply from ADB commands */
588 if (req->reply_len <= 2 || (req->reply[1] & 2) != 0) {
589 /* the 0x2 bit indicates no response */
592 /* leave just the command and result bytes in the reply */
594 memmove(req->reply, req->reply + 2, req->reply_len);
597 #ifdef DEBUG_MACIISI_ADB
600 printk(KERN_DEBUG "maciisi_interrupt: reply is ");
601 for (i = 0; i < req->reply_len; ++i)
602 printk(" %.2x", req->reply[i]);
607 current_req = req->next;
610 /* Obviously, we got it */
613 maciisi_input(maciisi_rbuf, reply_ptr - maciisi_rbuf);
615 maciisi_state = idle;
616 status = via[B] & (TIP|TREQ);
617 if (!(status & TREQ)) {
618 /* Timeout?! More likely, another packet coming in already */
619 #ifdef DEBUG_MACIISI_ADB
620 printk(KERN_DEBUG "extra data after packet: status %x ifr %x\n",
627 maciisi_state = reading;
629 reply_ptr = maciisi_rbuf;
631 /* Process the packet now */
635 /* We used to do this... but the controller might actually have data for us */
636 /* maciisi_stfu(); */
639 /* Do any queued requests now if possible */
641 if(i == 0 && need_sync) {
642 /* Packet needs to be synced */
643 maciisi_sync(current_req);
651 printk("maciisi_interrupt: unknown maciisi_state %d?\n", maciisi_state);
653 local_irq_restore(flags);
658 maciisi_input(unsigned char *buf, int nb)
660 #ifdef DEBUG_MACIISI_ADB
666 adb_input(buf+2, nb-2, buf[1] & 0x40);
669 #ifdef DEBUG_MACIISI_ADB
670 printk(KERN_DEBUG "data from IIsi ADB (%d bytes):", nb);
671 for (i = 0; i < nb; ++i)
672 printk(" %.2x", buf[i]);