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

1     /*
2      * Device driver for the via-cuda on Apple Powermacs.
3      *
4      * The VIA (versatile interface adapter) interfaces to the CUDA,
5      * a 6805 microprocessor core which controls the ADB (Apple Desktop
6      * Bus) which connects to the keyboard and mouse.  The CUDA also
7      * controls system power and the RTC (real time clock) chip.
8      *
9      * Copyright (C) 1996 Paul Mackerras.
10      */
11     #include <stdarg.h>
12     #include <linux/config.h>
13     #include <linux/types.h>
14     #include <linux/errno.h>
15     #include <linux/kernel.h>
16     #include <linux/delay.h>
17     #include <linux/sched.h>
18     #include <linux/adb.h>
19     #include <linux/cuda.h>
20     #ifdef CONFIG_PPC
21     #include <asm/prom.h>
22     #include <asm/machdep.h>
23     #else
24     #include <asm/macintosh.h>
25     #include <asm/macints.h>
26     #include <asm/machw.h>
27     #include <asm/mac_via.h>
28     #endif
29     #include <asm/io.h>
30     #include <asm/system.h>
31     #include <linux/init.h>
32     
33     static volatile unsigned char *via;
34     
35     #ifdef CONFIG_MAC
36     #define CUDA_IRQ IRQ_MAC_ADB
37     #define __openfirmware
38     #define eieio()
39     #else
40     #define CUDA_IRQ vias->intrs[0].line
41     #endif
42     
43     /* VIA registers - spaced 0x200 bytes apart */
44     #define RS		0x200		/* skip between registers */
45     #define B		0		/* B-side data */
46     #define A		RS		/* A-side data */
47     #define DIRB		(2*RS)		/* B-side direction (1=output) */
48     #define DIRA		(3*RS)		/* A-side direction (1=output) */
49     #define T1CL		(4*RS)		/* Timer 1 ctr/latch (low 8 bits) */
50     #define T1CH		(5*RS)		/* Timer 1 counter (high 8 bits) */
51     #define T1LL		(6*RS)		/* Timer 1 latch (low 8 bits) */
52     #define T1LH		(7*RS)		/* Timer 1 latch (high 8 bits) */
53     #define T2CL		(8*RS)		/* Timer 2 ctr/latch (low 8 bits) */
54     #define T2CH		(9*RS)		/* Timer 2 counter (high 8 bits) */
55     #define SR		(10*RS)		/* Shift register */
56     #define ACR		(11*RS)		/* Auxiliary control register */
57     #define PCR		(12*RS)		/* Peripheral control register */
58     #define IFR		(13*RS)		/* Interrupt flag register */
59     #define IER		(14*RS)		/* Interrupt enable register */
60     #define ANH		(15*RS)		/* A-side data, no handshake */
61     
62     /* Bits in B data register: all active low */
63     #define TREQ		0x08		/* Transfer request (input) */
64     #define TACK		0x10		/* Transfer acknowledge (output) */
65     #define TIP		0x20		/* Transfer in progress (output) */
66     
67     /* Bits in ACR */
68     #define SR_CTRL		0x1c		/* Shift register control bits */
69     #define SR_EXT		0x0c		/* Shift on external clock */
70     #define SR_OUT		0x10		/* Shift out if 1 */
71     
72     /* Bits in IFR and IER */
73     #define IER_SET		0x80		/* set bits in IER */
74     #define IER_CLR		0		/* clear bits in IER */
75     #define SR_INT		0x04		/* Shift register full/empty */
76     
77     static enum cuda_state {
78         idle,
79         sent_first_byte,
80         sending,
81         reading,
82         read_done,
83         awaiting_reply
84     } cuda_state;
85     
86     static struct adb_request *current_req;
87     static struct adb_request *last_req;
88     static unsigned char cuda_rbuf[16];
89     static unsigned char *reply_ptr;
90     static int reading_reply;
91     static int data_index;
92     #ifdef CONFIG_PPC
93     static struct device_node *vias;
94     #endif
95     static int cuda_fully_inited = 0;
96     
97     #ifdef CONFIG_ADB
98     static int cuda_probe(void);
99     static int cuda_init(void);
100     static int cuda_send_request(struct adb_request *req, int sync);
101     static int cuda_adb_autopoll(int devs);
102     static int cuda_reset_adb_bus(void);
103     #endif /* CONFIG_ADB */
104     
105     static int cuda_init_via(void);
106     static void cuda_start(void);
107     static void cuda_interrupt(int irq, void *arg, struct pt_regs *regs);
108     static void cuda_input(unsigned char *buf, int nb, struct pt_regs *regs);
109     void cuda_poll(void);
110     static int cuda_write(struct adb_request *req);
111     
112     int cuda_request(struct adb_request *req,
113     		 void (*done)(struct adb_request *), int nbytes, ...);
114     
115     #ifdef CONFIG_ADB
116     struct adb_driver via_cuda_driver = {
117     	"CUDA",
118     	cuda_probe,
119     	cuda_init,
120     	cuda_send_request,
121     	cuda_adb_autopoll,
122     	cuda_poll,
123     	cuda_reset_adb_bus
124     };
125     #endif /* CONFIG_ADB */
126     
127     #ifdef CONFIG_PPC
128     int
129     find_via_cuda(void)
130     {
131         int err;
132         struct adb_request req;
133     
134         if (vias != 0)
135     	return 1;
136         vias = find_devices("via-cuda");
137         if (vias == 0)
138     	return 0;
139         if (vias->next != 0)
140     	printk(KERN_WARNING "Warning: only using 1st via-cuda\n");
141     
142     #if 0
143         { int i;
144     
145         printk("find_via_cuda: node = %p, addrs =", vias->node);
146         for (i = 0; i < vias->n_addrs; ++i)
147     	printk(" %x(%x)", vias->addrs[i].address, vias->addrs[i].size);
148         printk(", intrs =");
149         for (i = 0; i < vias->n_intrs; ++i)
150     	printk(" %x", vias->intrs[i].line);
151         printk("\n"); }
152     #endif
153     
154         if (vias->n_addrs != 1 || vias->n_intrs != 1) {
155     	printk(KERN_ERR "via-cuda: expecting 1 address (%d) and 1 interrupt (%d)\n",
156     	       vias->n_addrs, vias->n_intrs);
157     	if (vias->n_addrs < 1 || vias->n_intrs < 1)
158     	    return 0;
159         }
160         via = (volatile unsigned char *) ioremap(vias->addrs->address, 0x2000);
161     
162         cuda_state = idle;
163         sys_ctrler = SYS_CTRLER_CUDA;
164     
165         err = cuda_init_via();
166         if (err) {
167     	printk(KERN_ERR "cuda_init_via() failed\n");
168     	via = NULL;
169     	return 0;
170         }
171     
172         /* Clear and enable interrupts, but only on PPC. On 68K it's done  */
173         /* for us by the main VIA driver in arch/m68k/mac/via.c        */
174     
175     #ifndef CONFIG_MAC
176         via[IFR] = 0x7f; eieio();	/* clear interrupts by writing 1s */
177         via[IER] = IER_SET|SR_INT; eieio();	/* enable interrupt from SR */
178     #endif
179     
180         /* enable autopoll */
181         cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, 1);
182         while (!req.complete)
183     	cuda_poll();
184     
185         return 1;
186     }
187     #endif /* CONFIG_PPC */
188     
189     int via_cuda_start(void)
190     {
191         if (via == NULL)
192     	return -ENODEV;
193     
194         if (request_irq(CUDA_IRQ, cuda_interrupt, 0, "ADB", cuda_interrupt)) {
195     	printk(KERN_ERR "cuda_init: can't get irq %d\n", CUDA_IRQ);
196     	return -EAGAIN;
197         }
198     
199         printk("Macintosh CUDA driver v0.5 for Unified ADB.\n");
200     
201         cuda_fully_inited = 1;
202         return 0;
203     }
204     
205     #ifdef CONFIG_ADB
206     static int
207     cuda_probe()
208     {
209     #ifdef CONFIG_PPC
210         if (sys_ctrler != SYS_CTRLER_CUDA)
211     	return -ENODEV;
212     #else
213         if (macintosh_config->adb_type != MAC_ADB_CUDA)
214     	return -ENODEV;
215         via = via1;
216     #endif
217         return 0;
218     }
219     
220     static int
221     cuda_init(void)
222     {
223         if (via == NULL)
224     	return -ENODEV;
225     #ifndef CONFIG_PPC
226         return via_cuda_start();
227     #endif
228         return 0;
229     }
230     #endif /* CONFIG_ADB */
231     
232     #define WAIT_FOR(cond, what)					\
233         do {							\
234     	for (x = 1000; !(cond); --x) {				\
235     	    if (x == 0) {					\
236     		printk("Timeout waiting for " what "\n");	\
237     		return -ENXIO;					\
238     	    }							\
239     	    udelay(100);					\
240     	}							\
241         } while (0)
242     
243     static int
244     cuda_init_via()
245     {
246         int x;
247     
248         via[DIRB] = (via[DIRB] | TACK | TIP) & ~TREQ;	/* TACK & TIP out */
249         via[B] |= TACK | TIP;				/* negate them */
250         via[ACR] = (via[ACR] & ~SR_CTRL) | SR_EXT;		/* SR data in */
251         eieio();
252         x = via[SR]; eieio();	/* clear any left-over data */
253     #ifndef CONFIG_MAC
254         via[IER] = 0x7f; eieio();	/* disable interrupts from VIA */
255     #endif
256         eieio();
257     
258         /* delay 4ms and then clear any pending interrupt */
259         mdelay(4);
260         x = via[SR]; eieio();
261     
262         /* sync with the CUDA - assert TACK without TIP */
263         via[B] &= ~TACK; eieio();
264     
265         /* wait for the CUDA to assert TREQ in response */
266         WAIT_FOR((via[B] & TREQ) == 0, "CUDA response to sync");
267     
268         /* wait for the interrupt and then clear it */
269         WAIT_FOR(via[IFR] & SR_INT, "CUDA response to sync (2)");
270         x = via[SR]; eieio();
271     
272         /* finish the sync by negating TACK */
273         via[B] |= TACK; eieio();
274     
275         /* wait for the CUDA to negate TREQ and the corresponding interrupt */
276         WAIT_FOR(via[B] & TREQ, "CUDA response to sync (3)");
277         WAIT_FOR(via[IFR] & SR_INT, "CUDA response to sync (4)");
278         x = via[SR]; eieio();
279         via[B] |= TIP; eieio();	/* should be unnecessary */
280     
281         return 0;
282     }
283     
284     #ifdef CONFIG_ADB
285     /* Send an ADB command */
286     static int
287     cuda_send_request(struct adb_request *req, int sync)
288     {
289         int i;
290     
291         if ((via == NULL) || !cuda_fully_inited) {
292     	req->complete = 1;
293     	return -ENXIO;
294         }
295       
296         req->reply_expected = 1;
297     
298         i = cuda_write(req);
299         if (i)
300     	return i;
301     
302         if (sync) {
303     	while (!req->complete)
304     	    cuda_poll();
305         }
306         return 0;
307     }
308     
309     
310     /* Enable/disable autopolling */
311     static int
312     cuda_adb_autopoll(int devs)
313     {
314         struct adb_request req;
315     
316         if ((via == NULL) || !cuda_fully_inited)
317     	return -ENXIO;
318     
319         cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, (devs? 1: 0));
320         while (!req.complete)
321     	cuda_poll();
322         return 0;
323     }
324     
325     /* Reset adb bus - how do we do this?? */
326     static int
327     cuda_reset_adb_bus(void)
328     {
329         struct adb_request req;
330     
331         if ((via == NULL) || !cuda_fully_inited)
332     	return -ENXIO;
333     
334         cuda_request(&req, NULL, 2, ADB_PACKET, 0);		/* maybe? */
335         while (!req.complete)
336     	cuda_poll();
337         return 0;
338     }
339     #endif /* CONFIG_ADB */
340     /* Construct and send a cuda request */
341     int
342     cuda_request(struct adb_request *req, void (*done)(struct adb_request *),
343     	     int nbytes, ...)
344     {
345         va_list list;
346         int i;
347     
348         if (via == NULL) {
349     	req->complete = 1;
350     	return -ENXIO;
351         }
352     
353         req->nbytes = nbytes;
354         req->done = done;
355         va_start(list, nbytes);
356         for (i = 0; i < nbytes; ++i)
357     	req->data[i] = va_arg(list, int);
358         va_end(list);
359         req->reply_expected = 1;
360         return cuda_write(req);
361     }
362     
363     static int
364     cuda_write(struct adb_request *req)
365     {
366         unsigned long flags;
367     
368         if (req->nbytes < 2 || req->data[0] > CUDA_PACKET) {
369     	req->complete = 1;
370     	return -EINVAL;
371         }
372         req->next = 0;
373         req->sent = 0;
374         req->complete = 0;
375         req->reply_len = 0;
376         save_flags(flags); cli();
377     
378         if (current_req != 0) {
379     	last_req->next = req;
380     	last_req = req;
381         } else {
382     	current_req = req;
383     	last_req = req;
384     	if (cuda_state == idle)
385     	    cuda_start();
386         }
387     
388         restore_flags(flags);
389         return 0;
390     }
391     
392     static void
393     cuda_start()
394     {
395         unsigned long flags;
396         struct adb_request *req;
397     
398         /* assert cuda_state == idle */
399         /* get the packet to send */
400         req = current_req;
401         if (req == 0)
402     	return;
403         save_flags(flags); cli();
404         if ((via[B] & TREQ) == 0) {
405     	restore_flags(flags);
406     	return;			/* a byte is coming in from the CUDA */
407         }
408     
409         /* set the shift register to shift out and send a byte */
410         via[ACR] |= SR_OUT; eieio();
411         via[SR] = req->data[0]; eieio();
412         via[B] &= ~TIP;
413         cuda_state = sent_first_byte;
414         restore_flags(flags);
415     }
416     
417     void
418     cuda_poll()
419     {
420         unsigned long flags;
421     
422         save_flags(flags);
423         cli();
424         if (via[IFR] & SR_INT)
425     	cuda_interrupt(0, 0, 0);
426         restore_flags(flags);
427     }
428     
429     static void
430     cuda_interrupt(int irq, void *arg, struct pt_regs *regs)
431     {
432         int x, status;
433         struct adb_request *req;
434     
435         if ((via[IFR] & SR_INT) == 0)
436     	return;
437     
438         status = (~via[B] & (TIP|TREQ)) | (via[ACR] & SR_OUT); eieio();
439         /* printk("cuda_interrupt: state=%d status=%x\n", cuda_state, status); */
440         switch (cuda_state) {
441         case idle:
442     	/* CUDA has sent us the first byte of data - unsolicited */
443     	if (status != TREQ)
444     	    printk("cuda: state=idle, status=%x\n", status);
445     	x = via[SR]; eieio();
446     	via[B] &= ~TIP; eieio();
447     	cuda_state = reading;
448     	reply_ptr = cuda_rbuf;
449     	reading_reply = 0;
450     	break;
451     
452         case awaiting_reply:
453     	/* CUDA has sent us the first byte of data of a reply */
454     	if (status != TREQ)
455     	    printk("cuda: state=awaiting_reply, status=%x\n", status);
456     	x = via[SR]; eieio();
457     	via[B] &= ~TIP; eieio();
458     	cuda_state = reading;
459     	reply_ptr = current_req->reply;
460     	reading_reply = 1;
461     	break;
462     
463         case sent_first_byte:
464     	if (status == TREQ + TIP + SR_OUT) {
465     	    /* collision */
466     	    via[ACR] &= ~SR_OUT; eieio();
467     	    x = via[SR]; eieio();
468     	    via[B] |= TIP | TACK; eieio();
469     	    cuda_state = idle;
470     	} else {
471     	    /* assert status == TIP + SR_OUT */
472     	    if (status != TIP + SR_OUT)
473     		printk("cuda: state=sent_first_byte status=%x\n", status);
474     	    via[SR] = current_req->data[1]; eieio();
475     	    via[B] ^= TACK; eieio();
476     	    data_index = 2;
477     	    cuda_state = sending;
478     	}
479     	break;
480     
481         case sending:
482     	req = current_req;
483     	if (data_index >= req->nbytes) {
484     	    via[ACR] &= ~SR_OUT; eieio();
485     	    x = via[SR]; eieio();
486     	    via[B] |= TACK | TIP; eieio();
487     	    req->sent = 1;
488     	    if (req->reply_expected) {
489     		cuda_state = awaiting_reply;
490     	    } else {
491     		current_req = req->next;
492     		if (req->done)
493     		    (*req->done)(req);
494     		/* not sure about this */
495     		cuda_state = idle;
496     		cuda_start();
497     	    }
498     	} else {
499     	    via[SR] = req->data[data_index++]; eieio();
500     	    via[B] ^= TACK; eieio();
501     	}
502     	break;
503     
504         case reading:
505     	*reply_ptr++ = via[SR]; eieio();
506     	if (status == TIP) {
507     	    /* that's all folks */
508     	    via[B] |= TACK | TIP; eieio();
509     	    cuda_state = read_done;
510     	} else {
511     	    /* assert status == TIP | TREQ */
512     	    if (status != TIP + TREQ)
513     		printk("cuda: state=reading status=%x\n", status);
514     	    via[B] ^= TACK; eieio();
515     	}
516     	break;
517     
518         case read_done:
519     	x = via[SR]; eieio();
520     	if (reading_reply) {
521     	    req = current_req;
522     	    req->reply_len = reply_ptr - req->reply;
523     	    if (req->data[0] == ADB_PACKET) {
524     		/* Have to adjust the reply from ADB commands */
525     		if (req->reply_len <= 2 || (req->reply[1] & 2) != 0) {
526     		    /* the 0x2 bit indicates no response */
527     		    req->reply_len = 0;
528     		} else {
529     		    /* leave just the command and result bytes in the reply */
530     		    req->reply_len -= 2;
531     		    memmove(req->reply, req->reply + 2, req->reply_len);
532     		}
533     	    }
534     	    req->complete = 1;
535     	    current_req = req->next;
536     	    if (req->done)
537     		(*req->done)(req);
538     	} else {
539     	    cuda_input(cuda_rbuf, reply_ptr - cuda_rbuf, regs);
540     	}
541     	if (status == TREQ) {
542     	    via[B] &= ~TIP; eieio();
543     	    cuda_state = reading;
544     	    reply_ptr = cuda_rbuf;
545     	    reading_reply = 0;
546     	} else {
547     	    cuda_state = idle;
548     	    cuda_start();
549     	}
550     	break;
551     
552         default:
553     	printk("cuda_interrupt: unknown cuda_state %d?\n", cuda_state);
554         }
555     }
556     
557     static void
558     cuda_input(unsigned char *buf, int nb, struct pt_regs *regs)
559     {
560         int i;
561     
562         switch (buf[0]) {
563         case ADB_PACKET:
564     #ifdef CONFIG_XMON
565     	if (nb == 5 && buf[2] == 0x2c) {
566     	    extern int xmon_wants_key, xmon_adb_keycode;
567     	    if (xmon_wants_key) {
568     		xmon_adb_keycode = buf[3];
569     		return;
570     	    }
571     	}
572     #endif /* CONFIG_XMON */
573     #ifdef CONFIG_ADB
574     	adb_input(buf+2, nb-2, regs, buf[1] & 0x40);
575     #endif /* CONFIG_ADB */
576     	break;
577     
578         default:
579     	printk("data from cuda (%d bytes):", nb);
580     	for (i = 0; i < nb; ++i)
581     	    printk(" %.2x", buf[i]);
582     	printk("\n");
583         }
584     }
585