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");
 
 298 maciisi_request(struct adb_request *req, void (*done)(struct adb_request *),
 
 304         req->nbytes = nbytes;
 
 306         req->reply_expected = 0;
 
 307         va_start(list, nbytes);
 
 308         for (i = 0; i < nbytes; i++)
 
 309                 req->data[i++] = va_arg(list, int);
 
 312         return maciisi_send_request(req, 1);
 
 315 /* Enqueue a request, and run the queue if possible */
 
 317 maciisi_write(struct adb_request* req)
 
 322         /* We will accept CUDA packets - the VIA sends them to us, so
 
 323            it figures that we should be able to send them to it */
 
 324         if (req->nbytes < 2 || req->data[0] > CUDA_PACKET) {
 
 325                 printk(KERN_ERR "maciisi_write: packet too small or not an ADB or CUDA packet\n");
 
 334         local_irq_save(flags);
 
 337                 last_req->next = req;
 
 343         if (maciisi_state == idle)
 
 348                         local_irq_restore(flags);
 
 354 #ifdef DEBUG_MACIISI_ADB
 
 355                 printk(KERN_DEBUG "maciisi_write: would start, but state is %d\n", maciisi_state);
 
 357                 local_irq_restore(flags);
 
 361         local_irq_restore(flags);
 
 369         struct adb_request* req;
 
 372 #ifdef DEBUG_MACIISI_ADB
 
 373         status = via[B] & (TIP | TREQ);
 
 375         printk(KERN_DEBUG "maciisi_start called, state=%d, status=%x, ifr=%x\n", maciisi_state, status, via[IFR]);
 
 378         if (maciisi_state != idle) {
 
 379                 /* shouldn't happen */
 
 380                 printk(KERN_ERR "maciisi_start: maciisi_start called when driver busy!\n");
 
 388         status = via[B] & (TIP|TREQ);
 
 389         if (!(status & TREQ)) {
 
 390 #ifdef DEBUG_MACIISI_ADB
 
 391                 printk(KERN_DEBUG "maciisi_start: bus busy - aborting\n");
 
 397 #ifdef DEBUG_MACIISI_ADB
 
 398         printk(KERN_DEBUG "maciisi_start: sending\n");
 
 400         /* Set state to active */
 
 406         /* Shift out and send */
 
 408         via[SR] = req->data[0];
 
 412         maciisi_state = sending;
 
 422         local_irq_save(flags);
 
 423         if (via[IFR] & SR_INT) {
 
 424                 maciisi_interrupt(0, NULL, NULL);
 
 426         else /* avoid calling this function too quickly in a loop */
 
 429         local_irq_restore(flags);
 
 432 /* Shift register interrupt - this is *supposed* to mean that the
 
 433    register is either full or empty. In practice, I have no idea what
 
 436 maciisi_interrupt(int irq, void* arg, struct pt_regs* regs)
 
 439         struct adb_request *req;
 
 440 #ifdef DEBUG_MACIISI_ADB
 
 441         static int dump_reply = 0;
 
 446         local_irq_save(flags);
 
 448         status = via[B] & (TIP|TREQ);
 
 449 #ifdef DEBUG_MACIISI_ADB
 
 450         printk(KERN_DEBUG "state %d status %x ifr %x\n", maciisi_state, status, via[IFR]);
 
 453         if (!(via[IFR] & SR_INT)) {
 
 454                 /* Shouldn't happen, we hope */
 
 455                 printk(KERN_ERR "maciisi_interrupt: called without interrupt flag set\n");
 
 456                 local_irq_restore(flags);
 
 460         /* Clear the interrupt */
 
 461         /* via[IFR] = SR_INT; */
 
 464         switch (maciisi_state) {
 
 467                         printk(KERN_ERR "maciisi_interrupt: state is idle but TIP asserted!\n");
 
 473                 /* Signal start of frame */
 
 475                 /* Clear the interrupt (throw this value on the floor, it's useless) */
 
 477                 /* ACK adb chip, high-low */
 
 482                 maciisi_state = reading;
 
 484                         reply_ptr = current_req->reply;
 
 486                         reply_ptr = maciisi_rbuf;
 
 496                 if (!(status & TREQ)) {
 
 498                         printk(KERN_ERR "maciisi_interrupt: send collision\n");
 
 499                         /* Set idle and input */
 
 506                         maciisi_state = idle;
 
 508                         /* process this now, because the IFR has been cleared */
 
 514                 if (data_index >= req->nbytes) {
 
 515                         /* Sent the whole packet, put the bus back in idle state */
 
 516                         /* Shift in, we are about to read a reply (hopefully) */
 
 522                         maciisi_state = idle;
 
 523                         if (req->reply_expected) {
 
 524                                 /* Note: only set this once we've
 
 525                                    successfully sent the packet */
 
 528                                 current_req = req->next;
 
 531                                 /* Do any queued requests now */
 
 533                                 if(i == 0 && need_sync) {
 
 534                                         /* Packet needs to be synced */
 
 535                                         maciisi_sync(current_req);
 
 541                         /* Sending more stuff */
 
 545                         via[SR] = req->data[data_index++];
 
 546                         /* Signal 'byte ready' */
 
 553                 /* via[ACR] &= ~SR_OUT; */ /* Not in 2.2 */
 
 554                 if (reply_len++ > 16) {
 
 555                         printk(KERN_ERR "maciisi_interrupt: reply too long, aborting read\n");
 
 558                         via[B] &= ~(TACK|TIP);
 
 559                         maciisi_state = idle;
 
 561                         if(i == 0 && need_sync) {
 
 562                                 /* Packet needs to be synced */
 
 563                                 maciisi_sync(current_req);
 
 570                 *reply_ptr++ = via[SR];
 
 571                 status = via[B] & (TIP|TREQ);
 
 576                 if (!(status & TREQ))
 
 577                         break; /* more stuff to deal with */
 
 581                 tmp = via[SR]; /* That's what happens in 2.2 */
 
 582                 udelay(ADB_DELAY); /* Give controller time to recover */
 
 584                 /* end of packet, deal with it */
 
 587                         req->reply_len = reply_ptr - req->reply;
 
 588                         if (req->data[0] == ADB_PACKET) {
 
 589                                 /* Have to adjust the reply from ADB commands */
 
 590                                 if (req->reply_len <= 2 || (req->reply[1] & 2) != 0) {
 
 591                                         /* the 0x2 bit indicates no response */
 
 594                                         /* leave just the command and result bytes in the reply */
 
 596                                         memmove(req->reply, req->reply + 2, req->reply_len);
 
 599 #ifdef DEBUG_MACIISI_ADB
 
 602                                 printk(KERN_DEBUG "maciisi_interrupt: reply is ");
 
 603                                 for (i = 0; i < req->reply_len; ++i)
 
 604                                         printk(" %.2x", req->reply[i]);
 
 609                         current_req = req->next;
 
 612                         /* Obviously, we got it */
 
 615                         maciisi_input(maciisi_rbuf, reply_ptr - maciisi_rbuf, regs);
 
 617                 maciisi_state = idle;
 
 618                 status = via[B] & (TIP|TREQ);
 
 619                 if (!(status & TREQ)) {
 
 620                         /* Timeout?! More likely, another packet coming in already */
 
 621 #ifdef DEBUG_MACIISI_ADB
 
 622                         printk(KERN_DEBUG "extra data after packet: status %x ifr %x\n",
 
 629                         maciisi_state = reading;
 
 631                         reply_ptr = maciisi_rbuf;
 
 633                         /* Process the packet now */
 
 637                         /* We used to do this... but the controller might actually have data for us */
 
 638                         /* maciisi_stfu(); */
 
 641                         /* Do any queued requests now if possible */
 
 643                         if(i == 0 && need_sync) {
 
 644                                 /* Packet needs to be synced */
 
 645                                 maciisi_sync(current_req);
 
 653                 printk("maciisi_interrupt: unknown maciisi_state %d?\n", maciisi_state);
 
 655         local_irq_restore(flags);
 
 660 maciisi_input(unsigned char *buf, int nb, struct pt_regs *regs)
 
 662 #ifdef DEBUG_MACIISI_ADB
 
 668             adb_input(buf+2, nb-2, regs, buf[1] & 0x40);
 
 671 #ifdef DEBUG_MACIISI_ADB
 
 672             printk(KERN_DEBUG "data from IIsi ADB (%d bytes):", nb);
 
 673             for (i = 0; i < nb; ++i)
 
 674                     printk(" %.2x", buf[i]);