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