File: /usr/src/linux/drivers/macintosh/via-macii.c

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