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