perf bench futex: Cache align the worker struct
[linux/fpc-iii.git] / drivers / macintosh / via-cuda.c
blob2088e23a80020d9c49ed85b3376f82426d3cb74f
1 /*
2 * Device driver for the via-cuda on Apple Powermacs.
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.
9 * Copyright (C) 1996 Paul Mackerras.
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>
31 static volatile unsigned char __iomem *via;
32 static DEFINE_SPINLOCK(cuda_lock);
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 */
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) */
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 */
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 */
68 static enum cuda_state {
69 idle,
70 sent_first_byte,
71 sending,
72 reading,
73 read_done,
74 awaiting_reply
75 } cuda_state;
77 static struct adb_request *current_req;
78 static struct adb_request *last_req;
79 static unsigned char cuda_rbuf[16];
80 static unsigned char *reply_ptr;
81 static int reading_reply;
82 static int data_index;
83 static int cuda_irq;
84 #ifdef CONFIG_PPC
85 static struct device_node *vias;
86 #endif
87 static int cuda_fully_inited;
89 #ifdef CONFIG_ADB
90 static int cuda_probe(void);
91 static int cuda_send_request(struct adb_request *req, int sync);
92 static int cuda_adb_autopoll(int devs);
93 static int cuda_reset_adb_bus(void);
94 #endif /* CONFIG_ADB */
96 static int cuda_init_via(void);
97 static void cuda_start(void);
98 static irqreturn_t cuda_interrupt(int irq, void *arg);
99 static void cuda_input(unsigned char *buf, int nb);
100 void cuda_poll(void);
101 static int cuda_write(struct adb_request *req);
103 int cuda_request(struct adb_request *req,
104 void (*done)(struct adb_request *), int nbytes, ...);
106 #ifdef CONFIG_ADB
107 struct 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,
115 #endif /* CONFIG_ADB */
117 #ifdef CONFIG_MAC
118 int __init find_via_cuda(void)
120 struct adb_request req;
121 int err;
123 if (macintosh_config->adb_type != MAC_ADB_CUDA)
124 return 0;
126 via = via1;
127 cuda_state = idle;
129 err = cuda_init_via();
130 if (err) {
131 printk(KERN_ERR "cuda_init_via() failed\n");
132 via = NULL;
133 return 0;
136 /* enable autopoll */
137 cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, 1);
138 while (!req.complete)
139 cuda_poll();
141 return 1;
143 #else
144 int __init find_via_cuda(void)
146 struct adb_request req;
147 phys_addr_t taddr;
148 const u32 *reg;
149 int err;
151 if (vias != 0)
152 return 1;
153 vias = of_find_node_by_name(NULL, "via-cuda");
154 if (vias == 0)
155 return 0;
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;
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;
167 via = ioremap(taddr, 0x2000);
168 if (via == NULL) {
169 printk(KERN_ERR "via-cuda: Can't map address !\n");
170 goto fail;
173 cuda_state = idle;
174 sys_ctrler = SYS_CTRLER_CUDA;
176 err = cuda_init_via();
177 if (err) {
178 printk(KERN_ERR "cuda_init_via() failed\n");
179 via = NULL;
180 return 0;
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 */
186 out_8(&via[IFR], 0x7f); /* clear interrupts by writing 1s */
187 out_8(&via[IER], IER_SET|SR_INT); /* enable interrupt from SR */
189 /* enable autopoll */
190 cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, 1);
191 while (!req.complete)
192 cuda_poll();
194 return 1;
196 fail:
197 of_node_put(vias);
198 vias = NULL;
199 return 0;
201 #endif /* !defined CONFIG_MAC */
203 static int __init via_cuda_start(void)
205 if (via == NULL)
206 return -ENODEV;
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) {
213 printk(KERN_ERR "via-cuda: can't map interrupts for %s\n",
214 vias->full_name);
215 return -ENODEV;
217 #endif
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;
224 printk("Macintosh CUDA driver v0.5 for Unified ADB.\n");
226 cuda_fully_inited = 1;
227 return 0;
230 device_initcall(via_cuda_start);
232 #ifdef CONFIG_ADB
233 static int
234 cuda_probe(void)
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;
247 #endif /* CONFIG_ADB */
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; \
257 udelay(100); \
259 } while (0)
261 static int
262 __init cuda_init_via(void)
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
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);
280 /* sync with the CUDA - assert TACK without TIP */
281 out_8(&via[B], in_8(&via[B]) & ~TACK);
283 /* wait for the CUDA to assert TREQ in response */
284 WAIT_FOR((in_8(&via[B]) & TREQ) == 0, "CUDA response to sync");
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);
291 /* finish the sync by negating TACK */
292 out_8(&via[B], in_8(&via[B]) | TACK);
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 */
301 return 0;
304 #ifdef CONFIG_ADB
305 /* Send an ADB command */
306 static int
307 cuda_send_request(struct adb_request *req, int sync)
309 int i;
311 if ((via == NULL) || !cuda_fully_inited) {
312 req->complete = 1;
313 return -ENXIO;
316 req->reply_expected = 1;
318 i = cuda_write(req);
319 if (i)
320 return i;
322 if (sync) {
323 while (!req->complete)
324 cuda_poll();
326 return 0;
330 /* Enable/disable autopolling */
331 static int
332 cuda_adb_autopoll(int devs)
334 struct adb_request req;
336 if ((via == NULL) || !cuda_fully_inited)
337 return -ENXIO;
339 cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, (devs? 1: 0));
340 while (!req.complete)
341 cuda_poll();
342 return 0;
345 /* Reset adb bus - how do we do this?? */
346 static int
347 cuda_reset_adb_bus(void)
349 struct adb_request req;
351 if ((via == NULL) || !cuda_fully_inited)
352 return -ENXIO;
354 cuda_request(&req, NULL, 2, ADB_PACKET, 0); /* maybe? */
355 while (!req.complete)
356 cuda_poll();
357 return 0;
359 #endif /* CONFIG_ADB */
360 /* Construct and send a cuda request */
362 cuda_request(struct adb_request *req, void (*done)(struct adb_request *),
363 int nbytes, ...)
365 va_list list;
366 int i;
368 if (via == NULL) {
369 req->complete = 1;
370 return -ENXIO;
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);
382 EXPORT_SYMBOL(cuda_request);
384 static int
385 cuda_write(struct adb_request *req)
387 unsigned long flags;
389 if (req->nbytes < 2 || req->data[0] > CUDA_PACKET) {
390 req->complete = 1;
391 return -EINVAL;
393 req->next = NULL;
394 req->sent = 0;
395 req->complete = 0;
396 req->reply_len = 0;
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();
408 spin_unlock_irqrestore(&cuda_lock, flags);
410 return 0;
413 static void
414 cuda_start(void)
416 struct adb_request *req;
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 */
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;
433 void
434 cuda_poll(void)
436 /* cuda_interrupt only takes a normal lock, we disable
437 * interrupts here to avoid re-entering and thus deadlocking.
439 if (cuda_irq)
440 disable_irq(cuda_irq);
441 cuda_interrupt(0, NULL);
442 if (cuda_irq)
443 enable_irq(cuda_irq);
445 EXPORT_SYMBOL(cuda_poll);
447 static irqreturn_t
448 cuda_interrupt(int irq, void *arg)
450 int status;
451 struct adb_request *req = NULL;
452 unsigned char ibuf[16];
453 int ibuf_len = 0;
454 int complete = 0;
456 spin_lock(&cuda_lock);
458 /* On powermacs, this handler is registered for the VIA IRQ. But they use
459 * just the shift register IRQ -- other VIA interrupt sources are disabled.
460 * On m68k macs, the VIA IRQ sources are dispatched individually. Unless
461 * we are polling, the shift register IRQ flag has already been cleared.
464 #ifdef CONFIG_MAC
465 if (!arg)
466 #endif
468 if ((in_8(&via[IFR]) & SR_INT) == 0) {
469 spin_unlock(&cuda_lock);
470 return IRQ_NONE;
471 } else {
472 out_8(&via[IFR], SR_INT);
476 status = (~in_8(&via[B]) & (TIP|TREQ)) | (in_8(&via[ACR]) & SR_OUT);
477 /* printk("cuda_interrupt: state=%d status=%x\n", cuda_state, status); */
478 switch (cuda_state) {
479 case idle:
480 /* CUDA has sent us the first byte of data - unsolicited */
481 if (status != TREQ)
482 printk("cuda: state=idle, status=%x\n", status);
483 (void)in_8(&via[SR]);
484 out_8(&via[B], in_8(&via[B]) & ~TIP);
485 cuda_state = reading;
486 reply_ptr = cuda_rbuf;
487 reading_reply = 0;
488 break;
490 case awaiting_reply:
491 /* CUDA has sent us the first byte of data of a reply */
492 if (status != TREQ)
493 printk("cuda: state=awaiting_reply, status=%x\n", status);
494 (void)in_8(&via[SR]);
495 out_8(&via[B], in_8(&via[B]) & ~TIP);
496 cuda_state = reading;
497 reply_ptr = current_req->reply;
498 reading_reply = 1;
499 break;
501 case sent_first_byte:
502 if (status == TREQ + TIP + SR_OUT) {
503 /* collision */
504 out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT);
505 (void)in_8(&via[SR]);
506 out_8(&via[B], in_8(&via[B]) | TIP | TACK);
507 cuda_state = idle;
508 } else {
509 /* assert status == TIP + SR_OUT */
510 if (status != TIP + SR_OUT)
511 printk("cuda: state=sent_first_byte status=%x\n", status);
512 out_8(&via[SR], current_req->data[1]);
513 out_8(&via[B], in_8(&via[B]) ^ TACK);
514 data_index = 2;
515 cuda_state = sending;
517 break;
519 case sending:
520 req = current_req;
521 if (data_index >= req->nbytes) {
522 out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT);
523 (void)in_8(&via[SR]);
524 out_8(&via[B], in_8(&via[B]) | TACK | TIP);
525 req->sent = 1;
526 if (req->reply_expected) {
527 cuda_state = awaiting_reply;
528 } else {
529 current_req = req->next;
530 complete = 1;
531 /* not sure about this */
532 cuda_state = idle;
533 cuda_start();
535 } else {
536 out_8(&via[SR], req->data[data_index++]);
537 out_8(&via[B], in_8(&via[B]) ^ TACK);
539 break;
541 case reading:
542 *reply_ptr++ = in_8(&via[SR]);
543 if (status == TIP) {
544 /* that's all folks */
545 out_8(&via[B], in_8(&via[B]) | TACK | TIP);
546 cuda_state = read_done;
547 } else {
548 /* assert status == TIP | TREQ */
549 if (status != TIP + TREQ)
550 printk("cuda: state=reading status=%x\n", status);
551 out_8(&via[B], in_8(&via[B]) ^ TACK);
553 break;
555 case read_done:
556 (void)in_8(&via[SR]);
557 if (reading_reply) {
558 req = current_req;
559 req->reply_len = reply_ptr - req->reply;
560 if (req->data[0] == ADB_PACKET) {
561 /* Have to adjust the reply from ADB commands */
562 if (req->reply_len <= 2 || (req->reply[1] & 2) != 0) {
563 /* the 0x2 bit indicates no response */
564 req->reply_len = 0;
565 } else {
566 /* leave just the command and result bytes in the reply */
567 req->reply_len -= 2;
568 memmove(req->reply, req->reply + 2, req->reply_len);
571 current_req = req->next;
572 complete = 1;
573 } else {
574 /* This is tricky. We must break the spinlock to call
575 * cuda_input. However, doing so means we might get
576 * re-entered from another CPU getting an interrupt
577 * or calling cuda_poll(). I ended up using the stack
578 * (it's only for 16 bytes) and moving the actual
579 * call to cuda_input to outside of the lock.
581 ibuf_len = reply_ptr - cuda_rbuf;
582 memcpy(ibuf, cuda_rbuf, ibuf_len);
584 if (status == TREQ) {
585 out_8(&via[B], in_8(&via[B]) & ~TIP);
586 cuda_state = reading;
587 reply_ptr = cuda_rbuf;
588 reading_reply = 0;
589 } else {
590 cuda_state = idle;
591 cuda_start();
593 break;
595 default:
596 printk("cuda_interrupt: unknown cuda_state %d?\n", cuda_state);
598 spin_unlock(&cuda_lock);
599 if (complete && req) {
600 void (*done)(struct adb_request *) = req->done;
601 mb();
602 req->complete = 1;
603 /* Here, we assume that if the request has a done member, the
604 * struct request will survive to setting req->complete to 1
606 if (done)
607 (*done)(req);
609 if (ibuf_len)
610 cuda_input(ibuf, ibuf_len);
611 return IRQ_HANDLED;
614 static void
615 cuda_input(unsigned char *buf, int nb)
617 int i;
619 switch (buf[0]) {
620 case ADB_PACKET:
621 #ifdef CONFIG_XMON
622 if (nb == 5 && buf[2] == 0x2c) {
623 extern int xmon_wants_key, xmon_adb_keycode;
624 if (xmon_wants_key) {
625 xmon_adb_keycode = buf[3];
626 return;
629 #endif /* CONFIG_XMON */
630 #ifdef CONFIG_ADB
631 adb_input(buf+2, nb-2, buf[1] & 0x40);
632 #endif /* CONFIG_ADB */
633 break;
635 default:
636 printk("data from cuda (%d bytes):", nb);
637 for (i = 0; i < nb; ++i)
638 printk(" %.2x", buf[i]);
639 printk("\n");