Loading...
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/types.h>
13#include <linux/errno.h>
14#include <linux/kernel.h>
15#include <linux/delay.h>
16#include <linux/adb.h>
17#include <linux/cuda.h>
18#include <linux/spinlock.h>
19#include <linux/interrupt.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/mac_via.h>
27#endif
28#include <asm/io.h>
29#include <asm/system.h>
30#include <linux/init.h>
31
32static volatile unsigned char __iomem *via;
33static DEFINE_SPINLOCK(cuda_lock);
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
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
69static enum cuda_state {
70 idle,
71 sent_first_byte,
72 sending,
73 reading,
74 read_done,
75 awaiting_reply
76} cuda_state;
77
78static struct adb_request *current_req;
79static struct adb_request *last_req;
80static unsigned char cuda_rbuf[16];
81static unsigned char *reply_ptr;
82static int reading_reply;
83static int data_index;
84static int cuda_irq;
85#ifdef CONFIG_PPC
86static struct device_node *vias;
87#endif
88static int cuda_fully_inited;
89
90#ifdef CONFIG_ADB
91static int cuda_probe(void);
92static int cuda_send_request(struct adb_request *req, int sync);
93static int cuda_adb_autopoll(int devs);
94static int cuda_reset_adb_bus(void);
95#endif /* CONFIG_ADB */
96
97static int cuda_init_via(void);
98static void cuda_start(void);
99static irqreturn_t cuda_interrupt(int irq, void *arg);
100static void cuda_input(unsigned char *buf, int nb);
101void cuda_poll(void);
102static int cuda_write(struct adb_request *req);
103
104int cuda_request(struct adb_request *req,
105 void (*done)(struct adb_request *), int nbytes, ...);
106
107#ifdef CONFIG_ADB
108struct adb_driver via_cuda_driver = {
109 .name = "CUDA",
110 .probe = cuda_probe,
111 .send_request = cuda_send_request,
112 .autopoll = cuda_adb_autopoll,
113 .poll = cuda_poll,
114 .reset_bus = cuda_reset_adb_bus,
115};
116#endif /* CONFIG_ADB */
117
118#ifdef CONFIG_MAC
119int __init find_via_cuda(void)
120{
121 struct adb_request req;
122 int err;
123
124 if (macintosh_config->adb_type != MAC_ADB_CUDA)
125 return 0;
126
127 via = via1;
128 cuda_state = idle;
129
130 err = cuda_init_via();
131 if (err) {
132 printk(KERN_ERR "cuda_init_via() failed\n");
133 via = NULL;
134 return 0;
135 }
136
137 /* enable autopoll */
138 cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, 1);
139 while (!req.complete)
140 cuda_poll();
141
142 return 1;
143}
144#else
145int __init find_via_cuda(void)
146{
147 struct adb_request req;
148 phys_addr_t taddr;
149 const u32 *reg;
150 int err;
151
152 if (vias != 0)
153 return 1;
154 vias = of_find_node_by_name(NULL, "via-cuda");
155 if (vias == 0)
156 return 0;
157
158 reg = of_get_property(vias, "reg", NULL);
159 if (reg == NULL) {
160 printk(KERN_ERR "via-cuda: No \"reg\" property !\n");
161 goto fail;
162 }
163 taddr = of_translate_address(vias, reg);
164 if (taddr == 0) {
165 printk(KERN_ERR "via-cuda: Can't translate address !\n");
166 goto fail;
167 }
168 via = ioremap(taddr, 0x2000);
169 if (via == NULL) {
170 printk(KERN_ERR "via-cuda: Can't map address !\n");
171 goto fail;
172 }
173
174 cuda_state = idle;
175 sys_ctrler = SYS_CTRLER_CUDA;
176
177 err = cuda_init_via();
178 if (err) {
179 printk(KERN_ERR "cuda_init_via() failed\n");
180 via = NULL;
181 return 0;
182 }
183
184 /* Clear and enable interrupts, but only on PPC. On 68K it's done */
185 /* for us by the main VIA driver in arch/m68k/mac/via.c */
186
187 out_8(&via[IFR], 0x7f); /* clear interrupts by writing 1s */
188 out_8(&via[IER], IER_SET|SR_INT); /* enable interrupt from SR */
189
190 /* enable autopoll */
191 cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, 1);
192 while (!req.complete)
193 cuda_poll();
194
195 return 1;
196
197 fail:
198 of_node_put(vias);
199 vias = NULL;
200 return 0;
201}
202#endif /* !defined CONFIG_MAC */
203
204static int __init via_cuda_start(void)
205{
206 if (via == NULL)
207 return -ENODEV;
208
209#ifdef CONFIG_MAC
210 cuda_irq = IRQ_MAC_ADB;
211#else
212 cuda_irq = irq_of_parse_and_map(vias, 0);
213 if (cuda_irq == NO_IRQ) {
214 printk(KERN_ERR "via-cuda: can't map interrupts for %s\n",
215 vias->full_name);
216 return -ENODEV;
217 }
218#endif
219
220 if (request_irq(cuda_irq, cuda_interrupt, 0, "ADB", cuda_interrupt)) {
221 printk(KERN_ERR "via-cuda: can't request irq %d\n", cuda_irq);
222 return -EAGAIN;
223 }
224
225 printk("Macintosh CUDA driver v0.5 for Unified ADB.\n");
226
227 cuda_fully_inited = 1;
228 return 0;
229}
230
231device_initcall(via_cuda_start);
232
233#ifdef CONFIG_ADB
234static int
235cuda_probe(void)
236{
237#ifdef CONFIG_PPC
238 if (sys_ctrler != SYS_CTRLER_CUDA)
239 return -ENODEV;
240#else
241 if (macintosh_config->adb_type != MAC_ADB_CUDA)
242 return -ENODEV;
243#endif
244 if (via == NULL)
245 return -ENODEV;
246 return 0;
247}
248#endif /* CONFIG_ADB */
249
250#define WAIT_FOR(cond, what) \
251 do { \
252 int x; \
253 for (x = 1000; !(cond); --x) { \
254 if (x == 0) { \
255 printk("Timeout waiting for " what "\n"); \
256 return -ENXIO; \
257 } \
258 udelay(100); \
259 } \
260 } while (0)
261
262static int
263cuda_init_via(void)
264{
265 out_8(&via[DIRB], (in_8(&via[DIRB]) | TACK | TIP) & ~TREQ); /* TACK & TIP out */
266 out_8(&via[B], in_8(&via[B]) | TACK | TIP); /* negate them */
267 out_8(&via[ACR] ,(in_8(&via[ACR]) & ~SR_CTRL) | SR_EXT); /* SR data in */
268 (void)in_8(&via[SR]); /* clear any left-over data */
269#ifdef CONFIG_PPC
270 out_8(&via[IER], 0x7f); /* disable interrupts from VIA */
271 (void)in_8(&via[IER]);
272#else
273 out_8(&via[IER], SR_INT); /* disable SR interrupt from VIA */
274#endif
275
276 /* delay 4ms and then clear any pending interrupt */
277 mdelay(4);
278 (void)in_8(&via[SR]);
279 out_8(&via[IFR], SR_INT);
280
281 /* sync with the CUDA - assert TACK without TIP */
282 out_8(&via[B], in_8(&via[B]) & ~TACK);
283
284 /* wait for the CUDA to assert TREQ in response */
285 WAIT_FOR((in_8(&via[B]) & TREQ) == 0, "CUDA response to sync");
286
287 /* wait for the interrupt and then clear it */
288 WAIT_FOR(in_8(&via[IFR]) & SR_INT, "CUDA response to sync (2)");
289 (void)in_8(&via[SR]);
290 out_8(&via[IFR], SR_INT);
291
292 /* finish the sync by negating TACK */
293 out_8(&via[B], in_8(&via[B]) | TACK);
294
295 /* wait for the CUDA to negate TREQ and the corresponding interrupt */
296 WAIT_FOR(in_8(&via[B]) & TREQ, "CUDA response to sync (3)");
297 WAIT_FOR(in_8(&via[IFR]) & SR_INT, "CUDA response to sync (4)");
298 (void)in_8(&via[SR]);
299 out_8(&via[IFR], SR_INT);
300 out_8(&via[B], in_8(&via[B]) | TIP); /* should be unnecessary */
301
302 return 0;
303}
304
305#ifdef CONFIG_ADB
306/* Send an ADB command */
307static int
308cuda_send_request(struct adb_request *req, int sync)
309{
310 int i;
311
312 if ((via == NULL) || !cuda_fully_inited) {
313 req->complete = 1;
314 return -ENXIO;
315 }
316
317 req->reply_expected = 1;
318
319 i = cuda_write(req);
320 if (i)
321 return i;
322
323 if (sync) {
324 while (!req->complete)
325 cuda_poll();
326 }
327 return 0;
328}
329
330
331/* Enable/disable autopolling */
332static int
333cuda_adb_autopoll(int devs)
334{
335 struct adb_request req;
336
337 if ((via == NULL) || !cuda_fully_inited)
338 return -ENXIO;
339
340 cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, (devs? 1: 0));
341 while (!req.complete)
342 cuda_poll();
343 return 0;
344}
345
346/* Reset adb bus - how do we do this?? */
347static int
348cuda_reset_adb_bus(void)
349{
350 struct adb_request req;
351
352 if ((via == NULL) || !cuda_fully_inited)
353 return -ENXIO;
354
355 cuda_request(&req, NULL, 2, ADB_PACKET, 0); /* maybe? */
356 while (!req.complete)
357 cuda_poll();
358 return 0;
359}
360#endif /* CONFIG_ADB */
361/* Construct and send a cuda request */
362int
363cuda_request(struct adb_request *req, void (*done)(struct adb_request *),
364 int nbytes, ...)
365{
366 va_list list;
367 int i;
368
369 if (via == NULL) {
370 req->complete = 1;
371 return -ENXIO;
372 }
373
374 req->nbytes = nbytes;
375 req->done = done;
376 va_start(list, nbytes);
377 for (i = 0; i < nbytes; ++i)
378 req->data[i] = va_arg(list, int);
379 va_end(list);
380 req->reply_expected = 1;
381 return cuda_write(req);
382}
383
384static int
385cuda_write(struct adb_request *req)
386{
387 unsigned long flags;
388
389 if (req->nbytes < 2 || req->data[0] > CUDA_PACKET) {
390 req->complete = 1;
391 return -EINVAL;
392 }
393 req->next = NULL;
394 req->sent = 0;
395 req->complete = 0;
396 req->reply_len = 0;
397
398 spin_lock_irqsave(&cuda_lock, flags);
399 if (current_req != 0) {
400 last_req->next = req;
401 last_req = req;
402 } else {
403 current_req = req;
404 last_req = req;
405 if (cuda_state == idle)
406 cuda_start();
407 }
408 spin_unlock_irqrestore(&cuda_lock, flags);
409
410 return 0;
411}
412
413static void
414cuda_start(void)
415{
416 struct adb_request *req;
417
418 /* assert cuda_state == idle */
419 /* get the packet to send */
420 req = current_req;
421 if (req == 0)
422 return;
423 if ((in_8(&via[B]) & TREQ) == 0)
424 return; /* a byte is coming in from the CUDA */
425
426 /* set the shift register to shift out and send a byte */
427 out_8(&via[ACR], in_8(&via[ACR]) | SR_OUT);
428 out_8(&via[SR], req->data[0]);
429 out_8(&via[B], in_8(&via[B]) & ~TIP);
430 cuda_state = sent_first_byte;
431}
432
433void
434cuda_poll(void)
435{
436 /* cuda_interrupt only takes a normal lock, we disable
437 * interrupts here to avoid re-entering and thus deadlocking.
438 */
439 if (cuda_irq)
440 disable_irq(cuda_irq);
441 cuda_interrupt(0, NULL);
442 if (cuda_irq)
443 enable_irq(cuda_irq);
444}
445
446static irqreturn_t
447cuda_interrupt(int irq, void *arg)
448{
449 int status;
450 struct adb_request *req = NULL;
451 unsigned char ibuf[16];
452 int ibuf_len = 0;
453 int complete = 0;
454
455 spin_lock(&cuda_lock);
456
457 /* On powermacs, this handler is registered for the VIA IRQ. But they use
458 * just the shift register IRQ -- other VIA interrupt sources are disabled.
459 * On m68k macs, the VIA IRQ sources are dispatched individually. Unless
460 * we are polling, the shift register IRQ flag has already been cleared.
461 */
462
463#ifdef CONFIG_MAC
464 if (!arg)
465#endif
466 {
467 if ((in_8(&via[IFR]) & SR_INT) == 0) {
468 spin_unlock(&cuda_lock);
469 return IRQ_NONE;
470 } else {
471 out_8(&via[IFR], SR_INT);
472 }
473 }
474
475 status = (~in_8(&via[B]) & (TIP|TREQ)) | (in_8(&via[ACR]) & SR_OUT);
476 /* printk("cuda_interrupt: state=%d status=%x\n", cuda_state, status); */
477 switch (cuda_state) {
478 case idle:
479 /* CUDA has sent us the first byte of data - unsolicited */
480 if (status != TREQ)
481 printk("cuda: state=idle, status=%x\n", status);
482 (void)in_8(&via[SR]);
483 out_8(&via[B], in_8(&via[B]) & ~TIP);
484 cuda_state = reading;
485 reply_ptr = cuda_rbuf;
486 reading_reply = 0;
487 break;
488
489 case awaiting_reply:
490 /* CUDA has sent us the first byte of data of a reply */
491 if (status != TREQ)
492 printk("cuda: state=awaiting_reply, status=%x\n", status);
493 (void)in_8(&via[SR]);
494 out_8(&via[B], in_8(&via[B]) & ~TIP);
495 cuda_state = reading;
496 reply_ptr = current_req->reply;
497 reading_reply = 1;
498 break;
499
500 case sent_first_byte:
501 if (status == TREQ + TIP + SR_OUT) {
502 /* collision */
503 out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT);
504 (void)in_8(&via[SR]);
505 out_8(&via[B], in_8(&via[B]) | TIP | TACK);
506 cuda_state = idle;
507 } else {
508 /* assert status == TIP + SR_OUT */
509 if (status != TIP + SR_OUT)
510 printk("cuda: state=sent_first_byte status=%x\n", status);
511 out_8(&via[SR], current_req->data[1]);
512 out_8(&via[B], in_8(&via[B]) ^ TACK);
513 data_index = 2;
514 cuda_state = sending;
515 }
516 break;
517
518 case sending:
519 req = current_req;
520 if (data_index >= req->nbytes) {
521 out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT);
522 (void)in_8(&via[SR]);
523 out_8(&via[B], in_8(&via[B]) | TACK | TIP);
524 req->sent = 1;
525 if (req->reply_expected) {
526 cuda_state = awaiting_reply;
527 } else {
528 current_req = req->next;
529 complete = 1;
530 /* not sure about this */
531 cuda_state = idle;
532 cuda_start();
533 }
534 } else {
535 out_8(&via[SR], req->data[data_index++]);
536 out_8(&via[B], in_8(&via[B]) ^ TACK);
537 }
538 break;
539
540 case reading:
541 *reply_ptr++ = in_8(&via[SR]);
542 if (status == TIP) {
543 /* that's all folks */
544 out_8(&via[B], in_8(&via[B]) | TACK | TIP);
545 cuda_state = read_done;
546 } else {
547 /* assert status == TIP | TREQ */
548 if (status != TIP + TREQ)
549 printk("cuda: state=reading status=%x\n", status);
550 out_8(&via[B], in_8(&via[B]) ^ TACK);
551 }
552 break;
553
554 case read_done:
555 (void)in_8(&via[SR]);
556 if (reading_reply) {
557 req = current_req;
558 req->reply_len = reply_ptr - req->reply;
559 if (req->data[0] == ADB_PACKET) {
560 /* Have to adjust the reply from ADB commands */
561 if (req->reply_len <= 2 || (req->reply[1] & 2) != 0) {
562 /* the 0x2 bit indicates no response */
563 req->reply_len = 0;
564 } else {
565 /* leave just the command and result bytes in the reply */
566 req->reply_len -= 2;
567 memmove(req->reply, req->reply + 2, req->reply_len);
568 }
569 }
570 current_req = req->next;
571 complete = 1;
572 } else {
573 /* This is tricky. We must break the spinlock to call
574 * cuda_input. However, doing so means we might get
575 * re-entered from another CPU getting an interrupt
576 * or calling cuda_poll(). I ended up using the stack
577 * (it's only for 16 bytes) and moving the actual
578 * call to cuda_input to outside of the lock.
579 */
580 ibuf_len = reply_ptr - cuda_rbuf;
581 memcpy(ibuf, cuda_rbuf, ibuf_len);
582 }
583 if (status == TREQ) {
584 out_8(&via[B], in_8(&via[B]) & ~TIP);
585 cuda_state = reading;
586 reply_ptr = cuda_rbuf;
587 reading_reply = 0;
588 } else {
589 cuda_state = idle;
590 cuda_start();
591 }
592 break;
593
594 default:
595 printk("cuda_interrupt: unknown cuda_state %d?\n", cuda_state);
596 }
597 spin_unlock(&cuda_lock);
598 if (complete && req) {
599 void (*done)(struct adb_request *) = req->done;
600 mb();
601 req->complete = 1;
602 /* Here, we assume that if the request has a done member, the
603 * struct request will survive to setting req->complete to 1
604 */
605 if (done)
606 (*done)(req);
607 }
608 if (ibuf_len)
609 cuda_input(ibuf, ibuf_len);
610 return IRQ_HANDLED;
611}
612
613static void
614cuda_input(unsigned char *buf, int nb)
615{
616 int i;
617
618 switch (buf[0]) {
619 case ADB_PACKET:
620#ifdef CONFIG_XMON
621 if (nb == 5 && buf[2] == 0x2c) {
622 extern int xmon_wants_key, xmon_adb_keycode;
623 if (xmon_wants_key) {
624 xmon_adb_keycode = buf[3];
625 return;
626 }
627 }
628#endif /* CONFIG_XMON */
629#ifdef CONFIG_ADB
630 adb_input(buf+2, nb-2, buf[1] & 0x40);
631#endif /* CONFIG_ADB */
632 break;
633
634 default:
635 printk("data from cuda (%d bytes):", nb);
636 for (i = 0; i < nb; ++i)
637 printk(" %.2x", buf[i]);
638 printk("\n");
639 }
640}
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/types.h>
13#include <linux/errno.h>
14#include <linux/kernel.h>
15#include <linux/delay.h>
16#include <linux/adb.h>
17#include <linux/cuda.h>
18#include <linux/spinlock.h>
19#include <linux/interrupt.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/mac_via.h>
27#endif
28#include <asm/io.h>
29#include <linux/init.h>
30
31static volatile unsigned char __iomem *via;
32static DEFINE_SPINLOCK(cuda_lock);
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
58/* Bits in ACR */
59#define SR_CTRL 0x1c /* Shift register control bits */
60#define SR_EXT 0x0c /* Shift on external clock */
61#define SR_OUT 0x10 /* Shift out if 1 */
62
63/* Bits in IFR and IER */
64#define IER_SET 0x80 /* set bits in IER */
65#define IER_CLR 0 /* clear bits in IER */
66#define SR_INT 0x04 /* Shift register full/empty */
67
68static enum cuda_state {
69 idle,
70 sent_first_byte,
71 sending,
72 reading,
73 read_done,
74 awaiting_reply
75} cuda_state;
76
77static struct adb_request *current_req;
78static struct adb_request *last_req;
79static unsigned char cuda_rbuf[16];
80static unsigned char *reply_ptr;
81static int reading_reply;
82static int data_index;
83static int cuda_irq;
84#ifdef CONFIG_PPC
85static struct device_node *vias;
86#endif
87static int cuda_fully_inited;
88
89#ifdef CONFIG_ADB
90static int cuda_probe(void);
91static int cuda_send_request(struct adb_request *req, int sync);
92static int cuda_adb_autopoll(int devs);
93static int cuda_reset_adb_bus(void);
94#endif /* CONFIG_ADB */
95
96static int cuda_init_via(void);
97static void cuda_start(void);
98static irqreturn_t cuda_interrupt(int irq, void *arg);
99static void cuda_input(unsigned char *buf, int nb);
100void cuda_poll(void);
101static int cuda_write(struct adb_request *req);
102
103int cuda_request(struct adb_request *req,
104 void (*done)(struct adb_request *), int nbytes, ...);
105
106#ifdef CONFIG_ADB
107struct adb_driver via_cuda_driver = {
108 .name = "CUDA",
109 .probe = cuda_probe,
110 .send_request = cuda_send_request,
111 .autopoll = cuda_adb_autopoll,
112 .poll = cuda_poll,
113 .reset_bus = cuda_reset_adb_bus,
114};
115#endif /* CONFIG_ADB */
116
117#ifdef CONFIG_MAC
118int __init find_via_cuda(void)
119{
120 struct adb_request req;
121 int err;
122
123 if (macintosh_config->adb_type != MAC_ADB_CUDA)
124 return 0;
125
126 via = via1;
127 cuda_state = idle;
128
129 err = cuda_init_via();
130 if (err) {
131 printk(KERN_ERR "cuda_init_via() failed\n");
132 via = NULL;
133 return 0;
134 }
135
136 /* enable autopoll */
137 cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, 1);
138 while (!req.complete)
139 cuda_poll();
140
141 return 1;
142}
143#else
144int __init find_via_cuda(void)
145{
146 struct adb_request req;
147 phys_addr_t taddr;
148 const u32 *reg;
149 int err;
150
151 if (vias != 0)
152 return 1;
153 vias = of_find_node_by_name(NULL, "via-cuda");
154 if (vias == 0)
155 return 0;
156
157 reg = of_get_property(vias, "reg", NULL);
158 if (reg == NULL) {
159 printk(KERN_ERR "via-cuda: No \"reg\" property !\n");
160 goto fail;
161 }
162 taddr = of_translate_address(vias, reg);
163 if (taddr == 0) {
164 printk(KERN_ERR "via-cuda: Can't translate address !\n");
165 goto fail;
166 }
167 via = ioremap(taddr, 0x2000);
168 if (via == NULL) {
169 printk(KERN_ERR "via-cuda: Can't map address !\n");
170 goto fail;
171 }
172
173 cuda_state = idle;
174 sys_ctrler = SYS_CTRLER_CUDA;
175
176 err = cuda_init_via();
177 if (err) {
178 printk(KERN_ERR "cuda_init_via() failed\n");
179 via = NULL;
180 return 0;
181 }
182
183 /* Clear and enable interrupts, but only on PPC. On 68K it's done */
184 /* for us by the main VIA driver in arch/m68k/mac/via.c */
185
186 out_8(&via[IFR], 0x7f); /* clear interrupts by writing 1s */
187 out_8(&via[IER], IER_SET|SR_INT); /* enable interrupt from SR */
188
189 /* enable autopoll */
190 cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, 1);
191 while (!req.complete)
192 cuda_poll();
193
194 return 1;
195
196 fail:
197 of_node_put(vias);
198 vias = NULL;
199 return 0;
200}
201#endif /* !defined CONFIG_MAC */
202
203static int __init via_cuda_start(void)
204{
205 if (via == NULL)
206 return -ENODEV;
207
208#ifdef CONFIG_MAC
209 cuda_irq = IRQ_MAC_ADB;
210#else
211 cuda_irq = irq_of_parse_and_map(vias, 0);
212 if (cuda_irq == NO_IRQ) {
213 printk(KERN_ERR "via-cuda: can't map interrupts for %s\n",
214 vias->full_name);
215 return -ENODEV;
216 }
217#endif
218
219 if (request_irq(cuda_irq, cuda_interrupt, 0, "ADB", cuda_interrupt)) {
220 printk(KERN_ERR "via-cuda: can't request irq %d\n", cuda_irq);
221 return -EAGAIN;
222 }
223
224 printk("Macintosh CUDA driver v0.5 for Unified ADB.\n");
225
226 cuda_fully_inited = 1;
227 return 0;
228}
229
230device_initcall(via_cuda_start);
231
232#ifdef CONFIG_ADB
233static int
234cuda_probe(void)
235{
236#ifdef CONFIG_PPC
237 if (sys_ctrler != SYS_CTRLER_CUDA)
238 return -ENODEV;
239#else
240 if (macintosh_config->adb_type != MAC_ADB_CUDA)
241 return -ENODEV;
242#endif
243 if (via == NULL)
244 return -ENODEV;
245 return 0;
246}
247#endif /* CONFIG_ADB */
248
249#define WAIT_FOR(cond, what) \
250 do { \
251 int x; \
252 for (x = 1000; !(cond); --x) { \
253 if (x == 0) { \
254 printk("Timeout waiting for " what "\n"); \
255 return -ENXIO; \
256 } \
257 udelay(100); \
258 } \
259 } while (0)
260
261static int
262__init cuda_init_via(void)
263{
264 out_8(&via[DIRB], (in_8(&via[DIRB]) | TACK | TIP) & ~TREQ); /* TACK & TIP out */
265 out_8(&via[B], in_8(&via[B]) | TACK | TIP); /* negate them */
266 out_8(&via[ACR] ,(in_8(&via[ACR]) & ~SR_CTRL) | SR_EXT); /* SR data in */
267 (void)in_8(&via[SR]); /* clear any left-over data */
268#ifdef CONFIG_PPC
269 out_8(&via[IER], 0x7f); /* disable interrupts from VIA */
270 (void)in_8(&via[IER]);
271#else
272 out_8(&via[IER], SR_INT); /* disable SR interrupt from VIA */
273#endif
274
275 /* delay 4ms and then clear any pending interrupt */
276 mdelay(4);
277 (void)in_8(&via[SR]);
278 out_8(&via[IFR], SR_INT);
279
280 /* sync with the CUDA - assert TACK without TIP */
281 out_8(&via[B], in_8(&via[B]) & ~TACK);
282
283 /* wait for the CUDA to assert TREQ in response */
284 WAIT_FOR((in_8(&via[B]) & TREQ) == 0, "CUDA response to sync");
285
286 /* wait for the interrupt and then clear it */
287 WAIT_FOR(in_8(&via[IFR]) & SR_INT, "CUDA response to sync (2)");
288 (void)in_8(&via[SR]);
289 out_8(&via[IFR], SR_INT);
290
291 /* finish the sync by negating TACK */
292 out_8(&via[B], in_8(&via[B]) | TACK);
293
294 /* wait for the CUDA to negate TREQ and the corresponding interrupt */
295 WAIT_FOR(in_8(&via[B]) & TREQ, "CUDA response to sync (3)");
296 WAIT_FOR(in_8(&via[IFR]) & SR_INT, "CUDA response to sync (4)");
297 (void)in_8(&via[SR]);
298 out_8(&via[IFR], SR_INT);
299 out_8(&via[B], in_8(&via[B]) | TIP); /* should be unnecessary */
300
301 return 0;
302}
303
304#ifdef CONFIG_ADB
305/* Send an ADB command */
306static int
307cuda_send_request(struct adb_request *req, int sync)
308{
309 int i;
310
311 if ((via == NULL) || !cuda_fully_inited) {
312 req->complete = 1;
313 return -ENXIO;
314 }
315
316 req->reply_expected = 1;
317
318 i = cuda_write(req);
319 if (i)
320 return i;
321
322 if (sync) {
323 while (!req->complete)
324 cuda_poll();
325 }
326 return 0;
327}
328
329
330/* Enable/disable autopolling */
331static int
332cuda_adb_autopoll(int devs)
333{
334 struct adb_request req;
335
336 if ((via == NULL) || !cuda_fully_inited)
337 return -ENXIO;
338
339 cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, (devs? 1: 0));
340 while (!req.complete)
341 cuda_poll();
342 return 0;
343}
344
345/* Reset adb bus - how do we do this?? */
346static int
347cuda_reset_adb_bus(void)
348{
349 struct adb_request req;
350
351 if ((via == NULL) || !cuda_fully_inited)
352 return -ENXIO;
353
354 cuda_request(&req, NULL, 2, ADB_PACKET, 0); /* maybe? */
355 while (!req.complete)
356 cuda_poll();
357 return 0;
358}
359#endif /* CONFIG_ADB */
360/* Construct and send a cuda request */
361int
362cuda_request(struct adb_request *req, void (*done)(struct adb_request *),
363 int nbytes, ...)
364{
365 va_list list;
366 int i;
367
368 if (via == NULL) {
369 req->complete = 1;
370 return -ENXIO;
371 }
372
373 req->nbytes = nbytes;
374 req->done = done;
375 va_start(list, nbytes);
376 for (i = 0; i < nbytes; ++i)
377 req->data[i] = va_arg(list, int);
378 va_end(list);
379 req->reply_expected = 1;
380 return cuda_write(req);
381}
382
383static int
384cuda_write(struct adb_request *req)
385{
386 unsigned long flags;
387
388 if (req->nbytes < 2 || req->data[0] > CUDA_PACKET) {
389 req->complete = 1;
390 return -EINVAL;
391 }
392 req->next = NULL;
393 req->sent = 0;
394 req->complete = 0;
395 req->reply_len = 0;
396
397 spin_lock_irqsave(&cuda_lock, flags);
398 if (current_req != 0) {
399 last_req->next = req;
400 last_req = req;
401 } else {
402 current_req = req;
403 last_req = req;
404 if (cuda_state == idle)
405 cuda_start();
406 }
407 spin_unlock_irqrestore(&cuda_lock, flags);
408
409 return 0;
410}
411
412static void
413cuda_start(void)
414{
415 struct adb_request *req;
416
417 /* assert cuda_state == idle */
418 /* get the packet to send */
419 req = current_req;
420 if (req == 0)
421 return;
422 if ((in_8(&via[B]) & TREQ) == 0)
423 return; /* a byte is coming in from the CUDA */
424
425 /* set the shift register to shift out and send a byte */
426 out_8(&via[ACR], in_8(&via[ACR]) | SR_OUT);
427 out_8(&via[SR], req->data[0]);
428 out_8(&via[B], in_8(&via[B]) & ~TIP);
429 cuda_state = sent_first_byte;
430}
431
432void
433cuda_poll(void)
434{
435 /* cuda_interrupt only takes a normal lock, we disable
436 * interrupts here to avoid re-entering and thus deadlocking.
437 */
438 if (cuda_irq)
439 disable_irq(cuda_irq);
440 cuda_interrupt(0, NULL);
441 if (cuda_irq)
442 enable_irq(cuda_irq);
443}
444
445static irqreturn_t
446cuda_interrupt(int irq, void *arg)
447{
448 int status;
449 struct adb_request *req = NULL;
450 unsigned char ibuf[16];
451 int ibuf_len = 0;
452 int complete = 0;
453
454 spin_lock(&cuda_lock);
455
456 /* On powermacs, this handler is registered for the VIA IRQ. But they use
457 * just the shift register IRQ -- other VIA interrupt sources are disabled.
458 * On m68k macs, the VIA IRQ sources are dispatched individually. Unless
459 * we are polling, the shift register IRQ flag has already been cleared.
460 */
461
462#ifdef CONFIG_MAC
463 if (!arg)
464#endif
465 {
466 if ((in_8(&via[IFR]) & SR_INT) == 0) {
467 spin_unlock(&cuda_lock);
468 return IRQ_NONE;
469 } else {
470 out_8(&via[IFR], SR_INT);
471 }
472 }
473
474 status = (~in_8(&via[B]) & (TIP|TREQ)) | (in_8(&via[ACR]) & SR_OUT);
475 /* printk("cuda_interrupt: state=%d status=%x\n", cuda_state, status); */
476 switch (cuda_state) {
477 case idle:
478 /* CUDA has sent us the first byte of data - unsolicited */
479 if (status != TREQ)
480 printk("cuda: state=idle, status=%x\n", status);
481 (void)in_8(&via[SR]);
482 out_8(&via[B], in_8(&via[B]) & ~TIP);
483 cuda_state = reading;
484 reply_ptr = cuda_rbuf;
485 reading_reply = 0;
486 break;
487
488 case awaiting_reply:
489 /* CUDA has sent us the first byte of data of a reply */
490 if (status != TREQ)
491 printk("cuda: state=awaiting_reply, status=%x\n", status);
492 (void)in_8(&via[SR]);
493 out_8(&via[B], in_8(&via[B]) & ~TIP);
494 cuda_state = reading;
495 reply_ptr = current_req->reply;
496 reading_reply = 1;
497 break;
498
499 case sent_first_byte:
500 if (status == TREQ + TIP + SR_OUT) {
501 /* collision */
502 out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT);
503 (void)in_8(&via[SR]);
504 out_8(&via[B], in_8(&via[B]) | TIP | TACK);
505 cuda_state = idle;
506 } else {
507 /* assert status == TIP + SR_OUT */
508 if (status != TIP + SR_OUT)
509 printk("cuda: state=sent_first_byte status=%x\n", status);
510 out_8(&via[SR], current_req->data[1]);
511 out_8(&via[B], in_8(&via[B]) ^ TACK);
512 data_index = 2;
513 cuda_state = sending;
514 }
515 break;
516
517 case sending:
518 req = current_req;
519 if (data_index >= req->nbytes) {
520 out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT);
521 (void)in_8(&via[SR]);
522 out_8(&via[B], in_8(&via[B]) | TACK | TIP);
523 req->sent = 1;
524 if (req->reply_expected) {
525 cuda_state = awaiting_reply;
526 } else {
527 current_req = req->next;
528 complete = 1;
529 /* not sure about this */
530 cuda_state = idle;
531 cuda_start();
532 }
533 } else {
534 out_8(&via[SR], req->data[data_index++]);
535 out_8(&via[B], in_8(&via[B]) ^ TACK);
536 }
537 break;
538
539 case reading:
540 *reply_ptr++ = in_8(&via[SR]);
541 if (status == TIP) {
542 /* that's all folks */
543 out_8(&via[B], in_8(&via[B]) | TACK | TIP);
544 cuda_state = read_done;
545 } else {
546 /* assert status == TIP | TREQ */
547 if (status != TIP + TREQ)
548 printk("cuda: state=reading status=%x\n", status);
549 out_8(&via[B], in_8(&via[B]) ^ TACK);
550 }
551 break;
552
553 case read_done:
554 (void)in_8(&via[SR]);
555 if (reading_reply) {
556 req = current_req;
557 req->reply_len = reply_ptr - req->reply;
558 if (req->data[0] == ADB_PACKET) {
559 /* Have to adjust the reply from ADB commands */
560 if (req->reply_len <= 2 || (req->reply[1] & 2) != 0) {
561 /* the 0x2 bit indicates no response */
562 req->reply_len = 0;
563 } else {
564 /* leave just the command and result bytes in the reply */
565 req->reply_len -= 2;
566 memmove(req->reply, req->reply + 2, req->reply_len);
567 }
568 }
569 current_req = req->next;
570 complete = 1;
571 } else {
572 /* This is tricky. We must break the spinlock to call
573 * cuda_input. However, doing so means we might get
574 * re-entered from another CPU getting an interrupt
575 * or calling cuda_poll(). I ended up using the stack
576 * (it's only for 16 bytes) and moving the actual
577 * call to cuda_input to outside of the lock.
578 */
579 ibuf_len = reply_ptr - cuda_rbuf;
580 memcpy(ibuf, cuda_rbuf, ibuf_len);
581 }
582 if (status == TREQ) {
583 out_8(&via[B], in_8(&via[B]) & ~TIP);
584 cuda_state = reading;
585 reply_ptr = cuda_rbuf;
586 reading_reply = 0;
587 } else {
588 cuda_state = idle;
589 cuda_start();
590 }
591 break;
592
593 default:
594 printk("cuda_interrupt: unknown cuda_state %d?\n", cuda_state);
595 }
596 spin_unlock(&cuda_lock);
597 if (complete && req) {
598 void (*done)(struct adb_request *) = req->done;
599 mb();
600 req->complete = 1;
601 /* Here, we assume that if the request has a done member, the
602 * struct request will survive to setting req->complete to 1
603 */
604 if (done)
605 (*done)(req);
606 }
607 if (ibuf_len)
608 cuda_input(ibuf, ibuf_len);
609 return IRQ_HANDLED;
610}
611
612static void
613cuda_input(unsigned char *buf, int nb)
614{
615 int i;
616
617 switch (buf[0]) {
618 case ADB_PACKET:
619#ifdef CONFIG_XMON
620 if (nb == 5 && buf[2] == 0x2c) {
621 extern int xmon_wants_key, xmon_adb_keycode;
622 if (xmon_wants_key) {
623 xmon_adb_keycode = buf[3];
624 return;
625 }
626 }
627#endif /* CONFIG_XMON */
628#ifdef CONFIG_ADB
629 adb_input(buf+2, nb-2, buf[1] & 0x40);
630#endif /* CONFIG_ADB */
631 break;
632
633 default:
634 printk("data from cuda (%d bytes):", nb);
635 for (i = 0; i < nb; ++i)
636 printk(" %.2x", buf[i]);
637 printk("\n");
638 }
639}