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