m68k: Mac IRQ cleanup
[linux-2.6-block.git] / drivers / macintosh / via-macii.c
CommitLineData
1da177e4
LT
1/*
2 * Device driver for the via ADB on (many) Mac II-class machines
3 *
4 * Based on the original ADB keyboard handler Copyright (c) 1997 Alan Cox
5 * Also derived from code Copyright (C) 1996 Paul Mackerras.
6 *
7 * With various updates provided over the years by Michael Schmitz,
8 * Guideo Koerber and others.
9 *
10 * Rewrite for Unified ADB by Joshua M. Thompson (funaho@jurai.org)
11 *
12 * 1999-08-02 (jmt) - Initial rewrite for Unified ADB.
13 * 2000-03-29 Tony Mantler <tonym@mac.linux-m68k.org>
14 * - Big overhaul, should actually work now.
15 */
16
17#include <stdarg.h>
18#include <linux/types.h>
19#include <linux/errno.h>
20#include <linux/kernel.h>
21#include <linux/delay.h>
1da177e4
LT
22#include <linux/adb.h>
23#include <linux/interrupt.h>
24#include <linux/init.h>
25#include <asm/macintosh.h>
26#include <asm/macints.h>
27#include <asm/machw.h>
28#include <asm/mac_via.h>
29#include <asm/io.h>
30#include <asm/system.h>
31
32static volatile unsigned char *via;
33
34/* VIA registers - spaced 0x200 bytes apart */
35#define RS 0x200 /* skip between registers */
36#define B 0 /* B-side data */
37#define A RS /* A-side data */
38#define DIRB (2*RS) /* B-side direction (1=output) */
39#define DIRA (3*RS) /* A-side direction (1=output) */
40#define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */
41#define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */
42#define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */
43#define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */
44#define T2CL (8*RS) /* Timer 2 ctr/latch (low 8 bits) */
45#define T2CH (9*RS) /* Timer 2 counter (high 8 bits) */
46#define SR (10*RS) /* Shift register */
47#define ACR (11*RS) /* Auxiliary control register */
48#define PCR (12*RS) /* Peripheral control register */
49#define IFR (13*RS) /* Interrupt flag register */
50#define IER (14*RS) /* Interrupt enable register */
51#define ANH (15*RS) /* A-side data, no handshake */
52
53/* Bits in B data register: all active low */
54#define TREQ 0x08 /* Transfer request (input) */
55#define TACK 0x10 /* Transfer acknowledge (output) */
56#define TIP 0x20 /* Transfer in progress (output) */
57#define ST_MASK 0x30 /* mask for selecting ADB state bits */
58
59/* Bits in ACR */
60#define SR_CTRL 0x1c /* Shift register control bits */
61#define SR_EXT 0x0c /* Shift on external clock */
62#define SR_OUT 0x10 /* Shift out if 1 */
63
64/* Bits in IFR and IER */
65#define IER_SET 0x80 /* set bits in IER */
66#define IER_CLR 0 /* clear bits in IER */
67#define SR_INT 0x04 /* Shift register full/empty */
68#define SR_DATA 0x08 /* Shift register data */
69#define SR_CLOCK 0x10 /* Shift register clock */
70
71/* ADB transaction states according to GMHW */
72#define ST_CMD 0x00 /* ADB state: command byte */
73#define ST_EVEN 0x10 /* ADB state: even data byte */
74#define ST_ODD 0x20 /* ADB state: odd data byte */
75#define ST_IDLE 0x30 /* ADB state: idle, nothing to send */
76
77static int macii_init_via(void);
78static void macii_start(void);
7d12e780 79static irqreturn_t macii_interrupt(int irq, void *arg);
1da177e4
LT
80static void macii_retransmit(int);
81static void macii_queue_poll(void);
82
83static int macii_probe(void);
84static int macii_init(void);
85static int macii_send_request(struct adb_request *req, int sync);
86static int macii_write(struct adb_request *req);
87static int macii_autopoll(int devs);
88static void macii_poll(void);
89static int macii_reset_bus(void);
90
91struct adb_driver via_macii_driver = {
92 "Mac II",
93 macii_probe,
94 macii_init,
95 macii_send_request,
96 macii_autopoll,
97 macii_poll,
98 macii_reset_bus
99};
100
101static enum macii_state {
102 idle,
103 sending,
104 reading,
105 read_done,
106 awaiting_reply
107} macii_state;
108
87275856
OH
109static int need_poll;
110static int command_byte;
111static int last_reply;
112static int last_active;
1da177e4
LT
113
114static struct adb_request *current_req;
115static struct adb_request *last_req;
116static struct adb_request *retry_req;
117static unsigned char reply_buf[16];
118static unsigned char *reply_ptr;
119static int reply_len;
120static int reading_reply;
121static int data_index;
122static int first_byte;
123static int prefix_len;
124static int status = ST_IDLE|TREQ;
125static int last_status;
87275856 126static int driver_running;
1da177e4
LT
127
128/* debug level 10 required for ADB logging (should be && debug_adb, ideally) */
129
130/* Check for MacII style ADB */
131static int macii_probe(void)
132{
133 if (macintosh_config->adb_type != MAC_ADB_II) return -ENODEV;
134
135 via = via1;
136
137 printk("adb: Mac II ADB Driver v1.0 for Unified ADB\n");
138 return 0;
139}
140
141/* Initialize the driver */
142int macii_init(void)
143{
144 unsigned long flags;
145 int err;
146
147 local_irq_save(flags);
148
149 err = macii_init_via();
150 if (err) return err;
151
152 err = request_irq(IRQ_MAC_ADB, macii_interrupt, IRQ_FLG_LOCK, "ADB",
153 macii_interrupt);
154 if (err) return err;
155
156 macii_state = idle;
157 local_irq_restore(flags);
158 return 0;
159}
160
161/* initialize the hardware */
162static int macii_init_via(void)
163{
164 unsigned char x;
165
166 /* Set the lines up. We want TREQ as input TACK|TIP as output */
167 via[DIRB] = (via[DIRB] | TACK | TIP) & ~TREQ;
168
169 /* Set up state: idle */
170 via[B] |= ST_IDLE;
171 last_status = via[B] & (ST_MASK|TREQ);
172
173 /* Shift register on input */
174 via[ACR] = (via[ACR] & ~SR_CTRL) | SR_EXT;
175
176 /* Wipe any pending data and int */
177 x = via[SR];
178
179 return 0;
180}
181
182/* Send an ADB poll (Talk Register 0 command, tagged on the front of the request queue) */
183static void macii_queue_poll(void)
184{
185 static int device = 0;
186 static int in_poll=0;
187 static struct adb_request req;
188 unsigned long flags;
189
190 if (in_poll) printk("macii_queue_poll: double poll!\n");
191
192 in_poll++;
193 if (++device > 15) device = 1;
194
195 adb_request(&req, NULL, ADBREQ_REPLY|ADBREQ_NOSEND, 1,
196 ADB_READREG(device, 0));
197
198 local_irq_save(flags);
199
200 req.next = current_req;
201 current_req = &req;
202
203 local_irq_restore(flags);
204 macii_start();
205 in_poll--;
206}
207
208/* Send an ADB retransmit (Talk, appended to the request queue) */
209static void macii_retransmit(int device)
210{
211 static int in_retransmit = 0;
212 static struct adb_request rt;
213 unsigned long flags;
214
215 if (in_retransmit) printk("macii_retransmit: double retransmit!\n");
216
217 in_retransmit++;
218
219 adb_request(&rt, NULL, ADBREQ_REPLY|ADBREQ_NOSEND, 1,
220 ADB_READREG(device, 0));
221
222 local_irq_save(flags);
223
224 if (current_req != NULL) {
225 last_req->next = &rt;
226 last_req = &rt;
227 } else {
228 current_req = &rt;
229 last_req = &rt;
230 }
231
232 if (macii_state == idle) macii_start();
233
234 local_irq_restore(flags);
235 in_retransmit--;
236}
237
238/* Send an ADB request; if sync, poll out the reply 'till it's done */
239static int macii_send_request(struct adb_request *req, int sync)
240{
241 int i;
242
243 i = macii_write(req);
244 if (i) return i;
245
246 if (sync) {
247 while (!req->complete) macii_poll();
248 }
249 return 0;
250}
251
252/* Send an ADB request */
253static int macii_write(struct adb_request *req)
254{
255 unsigned long flags;
256
257 if (req->nbytes < 2 || req->data[0] != ADB_PACKET || req->nbytes > 15) {
258 req->complete = 1;
259 return -EINVAL;
260 }
261
a5d361fc 262 req->next = NULL;
1da177e4
LT
263 req->sent = 0;
264 req->complete = 0;
265 req->reply_len = 0;
266
267 local_irq_save(flags);
268
269 if (current_req != NULL) {
270 last_req->next = req;
271 last_req = req;
272 } else {
273 current_req = req;
274 last_req = req;
275 if (macii_state == idle) macii_start();
276 }
277
278 local_irq_restore(flags);
279 return 0;
280}
281
282/* Start auto-polling */
283static int macii_autopoll(int devs)
284{
285 /* Just ping a random default address */
286 if (!(current_req || retry_req))
287 macii_retransmit( (last_active < 16 && last_active > 0) ? last_active : 3);
288 return 0;
289}
290
291/* Prod the chip without interrupts */
292static void macii_poll(void)
293{
294 unsigned long flags;
295
296 local_irq_save(flags);
2850bc27 297 if (via[IFR] & SR_INT) macii_interrupt(0, NULL);
1da177e4
LT
298 local_irq_restore(flags);
299}
300
301/* Reset the bus */
302static int macii_reset_bus(void)
303{
304 static struct adb_request req;
305
306 /* Command = 0, Address = ignored */
307 adb_request(&req, NULL, 0, 1, ADB_BUSRESET);
308
309 return 0;
310}
311
312/* Start sending ADB packet */
313static void macii_start(void)
314{
315 unsigned long flags;
316 struct adb_request *req;
317
318 req = current_req;
319 if (!req) return;
320
321 /* assert macii_state == idle */
322 if (macii_state != idle) {
323 printk("macii_start: called while driver busy (%p %x %x)!\n",
324 req, macii_state, (uint) via1[B] & (ST_MASK|TREQ));
325 return;
326 }
327
328 local_irq_save(flags);
329
330 /*
331 * IRQ signaled ?? (means ADB controller wants to send, or might
332 * be end of packet if we were reading)
333 */
334#if 0 /* FIXME: This is broke broke broke, for some reason */
335 if ((via[B] & TREQ) == 0) {
336 printk("macii_start: weird poll stuff. huh?\n");
337 /*
338 * FIXME - we need to restart this on a timer
339 * or a collision at boot hangs us.
340 * Never set macii_state to idle here, or macii_start
341 * won't be called again from send_request!
342 * (need to re-check other cases ...)
343 */
344 /*
345 * if the interrupt handler set the need_poll
346 * flag, it's hopefully a SRQ poll or re-Talk
347 * so we try to send here anyway
348 */
349 if (!need_poll) {
350 if (console_loglevel == 10)
351 printk("macii_start: device busy - retry %p state %d status %x!\n",
352 req, macii_state,
353 (uint) via[B] & (ST_MASK|TREQ));
354 retry_req = req;
355 /* set ADB status here ? */
356 local_irq_restore(flags);
357 return;
358 } else {
359 need_poll = 0;
360 }
361 }
362#endif
363 /*
364 * Another retry pending? (sanity check)
365 */
366 if (retry_req) {
367 retry_req = NULL;
368 }
369
370 /* Now send it. Be careful though, that first byte of the request */
371 /* is actually ADB_PACKET; the real data begins at index 1! */
372
373 /* store command byte */
374 command_byte = req->data[1];
375 /* Output mode */
376 via[ACR] |= SR_OUT;
377 /* Load data */
378 via[SR] = req->data[1];
379 /* set ADB state to 'command' */
380 via[B] = (via[B] & ~ST_MASK) | ST_CMD;
381
382 macii_state = sending;
383 data_index = 2;
384
385 local_irq_restore(flags);
386}
387
388/*
389 * The notorious ADB interrupt handler - does all of the protocol handling,
390 * except for starting new send operations. Relies heavily on the ADB
391 * controller sending and receiving data, thereby generating SR interrupts
392 * for us. This means there has to be always activity on the ADB bus, otherwise
393 * the whole process dies and has to be re-kicked by sending TALK requests ...
394 * CUDA-based Macs seem to solve this with the autopoll option, for MacII-type
395 * ADB the problem isn't solved yet (retransmit of the latest active TALK seems
396 * a good choice; either on timeout or on a timer interrupt).
397 *
398 * The basic ADB state machine was left unchanged from the original MacII code
399 * by Alan Cox, which was based on the CUDA driver for PowerMac.
400 * The syntax of the ADB status lines seems to be totally different on MacII,
401 * though. MacII uses the states Command -> Even -> Odd -> Even ->...-> Idle for
402 * sending, and Idle -> Even -> Odd -> Even ->...-> Idle for receiving. Start
403 * and end of a receive packet are signaled by asserting /IRQ on the interrupt
404 * line. Timeouts are signaled by a sequence of 4 0xFF, with /IRQ asserted on
405 * every other byte. SRQ is probably signaled by 3 or more 0xFF tacked on the
406 * end of a packet. (Thanks to Guido Koerber for eavesdropping on the ADB
407 * protocol with a logic analyzer!!)
408 *
409 * Note: As of 21/10/97, the MacII ADB part works including timeout detection
410 * and retransmit (Talk to the last active device).
411 */
7d12e780 412static irqreturn_t macii_interrupt(int irq, void *arg)
1da177e4
LT
413{
414 int x, adbdir;
415 unsigned long flags;
416 struct adb_request *req;
417
418 last_status = status;
419
420 /* prevent races due to SCSI enabling ints */
421 local_irq_save(flags);
422
423 if (driver_running) {
424 local_irq_restore(flags);
425 return IRQ_NONE;
426 }
427
428 driver_running = 1;
429
430 status = via[B] & (ST_MASK|TREQ);
431 adbdir = via[ACR] & SR_OUT;
432
433 switch (macii_state) {
434 case idle:
435 x = via[SR];
436 first_byte = x;
437 /* set ADB state = even for first data byte */
438 via[B] = (via[B] & ~ST_MASK) | ST_EVEN;
439
440 reply_buf[0] = first_byte; /* was command_byte?? */
441 reply_ptr = reply_buf + 1;
442 reply_len = 1;
443 prefix_len = 1;
444 reading_reply = 0;
445
446 macii_state = reading;
447 break;
448
449 case awaiting_reply:
450 /* handshake etc. for II ?? */
451 x = via[SR];
452 first_byte = x;
453 /* set ADB state = even for first data byte */
454 via[B] = (via[B] & ~ST_MASK) | ST_EVEN;
455
456 current_req->reply[0] = first_byte;
457 reply_ptr = current_req->reply + 1;
458 reply_len = 1;
459 prefix_len = 1;
460 reading_reply = 1;
461
462 macii_state = reading;
463 break;
464
465 case sending:
466 req = current_req;
467 if (data_index >= req->nbytes) {
468 /* print an error message if a listen command has no data */
469 if (((command_byte & 0x0C) == 0x08)
470 /* && (console_loglevel == 10) */
471 && (data_index == 2))
472 printk("MacII ADB: listen command with no data: %x!\n",
473 command_byte);
474 /* reset to shift in */
475 via[ACR] &= ~SR_OUT;
476 x = via[SR];
477 /* set ADB state idle - might get SRQ */
478 via[B] = (via[B] & ~ST_MASK) | ST_IDLE;
479
480 req->sent = 1;
481
482 if (req->reply_expected) {
483 macii_state = awaiting_reply;
484 } else {
485 req->complete = 1;
486 current_req = req->next;
487 if (req->done) (*req->done)(req);
488 macii_state = idle;
489 if (current_req || retry_req)
490 macii_start();
491 else
492 macii_retransmit((command_byte & 0xF0) >> 4);
493 }
494 } else {
495 via[SR] = req->data[data_index++];
496
497 if ( (via[B] & ST_MASK) == ST_CMD ) {
498 /* just sent the command byte, set to EVEN */
499 via[B] = (via[B] & ~ST_MASK) | ST_EVEN;
500 } else {
501 /* invert state bits, toggle ODD/EVEN */
502 via[B] ^= ST_MASK;
503 }
504 }
505 break;
506
507 case reading:
508
509 /* timeout / SRQ handling for II hw */
510 if( (first_byte == 0xFF && (reply_len-prefix_len)==2
511 && memcmp(reply_ptr-2,"\xFF\xFF",2)==0) ||
512 ((reply_len-prefix_len)==3
513 && memcmp(reply_ptr-3,"\xFF\xFF\xFF",3)==0))
514 {
515 /*
516 * possible timeout (in fact, most probably a
517 * timeout, since SRQ can't be signaled without
518 * transfer on the bus).
519 * The last three bytes seen were FF, together
520 * with the starting byte (in case we started
521 * on 'idle' or 'awaiting_reply') this probably
522 * makes four. So this is mostl likely #5!
523 * The timeout signal is a pattern 1 0 1 0 0..
524 * on /INT, meaning we missed it :-(
525 */
526 x = via[SR];
527 if (x != 0xFF) printk("MacII ADB: mistaken timeout/SRQ!\n");
528
529 if ((status & TREQ) == (last_status & TREQ)) {
530 /* Not a timeout. Unsolicited SRQ? weird. */
531 /* Terminate the SRQ packet and poll */
532 need_poll = 1;
533 }
534 /* There's no packet to get, so reply is blank */
535 via[B] ^= ST_MASK;
536 reply_ptr -= (reply_len-prefix_len);
537 reply_len = prefix_len;
538 macii_state = read_done;
539 break;
540 } /* end timeout / SRQ handling for II hw. */
541
542 if((reply_len-prefix_len)>3
543 && memcmp(reply_ptr-3,"\xFF\xFF\xFF",3)==0)
544 {
545 /* SRQ tacked on data packet */
546 /* Terminate the packet (SRQ never ends) */
547 x = via[SR];
548 macii_state = read_done;
549 reply_len -= 3;
550 reply_ptr -= 3;
551 need_poll = 1;
552 /* need to continue; next byte not seen else */
553 } else {
554 /* Sanity check */
555 if (reply_len > 15) reply_len = 0;
556 /* read byte */
557 x = via[SR];
558 *reply_ptr = x;
559 reply_ptr++;
560 reply_len++;
561 }
562 /* The usual handshake ... */
563
564 /*
565 * NetBSD hints that the next to last byte
566 * is sent with IRQ !!
567 * Guido found out it's the last one (0x0),
568 * but IRQ should be asserted already.
569 * Problem with timeout detection: First
570 * transition to /IRQ might be second
571 * byte of timeout packet!
572 * Timeouts are signaled by 4x FF.
573 */
574 if (((status & TREQ) == 0) && (x == 0x00)) { /* != 0xFF */
575 /* invert state bits, toggle ODD/EVEN */
576 via[B] ^= ST_MASK;
577
578 /* adjust packet length */
579 reply_len--;
580 reply_ptr--;
581 macii_state = read_done;
582 } else {
583 /* not caught: ST_CMD */
584 /* required for re-entry 'reading'! */
585 if ((status & ST_MASK) == ST_IDLE) {
586 /* (in)sanity check - set even */
587 via[B] = (via[B] & ~ST_MASK) | ST_EVEN;
588 } else {
589 /* invert state bits */
590 via[B] ^= ST_MASK;
591 }
592 }
593 break;
594
595 case read_done:
596 x = via[SR];
597 if (reading_reply) {
598 req = current_req;
599 req->reply_len = reply_ptr - req->reply;
600 req->complete = 1;
601 current_req = req->next;
602 if (req->done) (*req->done)(req);
603 } else {
7d12e780 604 adb_input(reply_buf, reply_ptr - reply_buf, 0);
1da177e4
LT
605 }
606
607 /*
608 * remember this device ID; it's the latest we got a
609 * reply from!
610 */
611 last_reply = command_byte;
612 last_active = (command_byte & 0xF0) >> 4;
613
614 /* SRQ seen before, initiate poll now */
615 if (need_poll) {
616 macii_state = idle;
617 macii_queue_poll();
618 need_poll = 0;
619 break;
620 }
621
622 /* set ADB state to idle */
623 via[B] = (via[B] & ~ST_MASK) | ST_IDLE;
624
625 /* /IRQ seen, so the ADB controller has data for us */
626 if ((via[B] & TREQ) != 0) {
627 macii_state = reading;
628
629 reply_buf[0] = command_byte;
630 reply_ptr = reply_buf + 1;
631 reply_len = 1;
632 prefix_len = 1;
633 reading_reply = 0;
634 } else {
635 /* no IRQ, send next packet or wait */
636 macii_state = idle;
637 if (current_req)
638 macii_start();
639 else
640 macii_retransmit(last_active);
641 }
642 break;
643
644 default:
645 break;
646 }
647 /* reset mutex and interrupts */
648 driver_running = 0;
649 local_irq_restore(flags);
650 return IRQ_HANDLED;
651}