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