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.
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>
22 #include <asm/machdep.h>
24 #include <asm/macintosh.h>
25 #include <asm/macints.h>
26 #include <asm/mac_via.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) */
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 inline bool TREQ_asserted(u8 portb)
70 return !(portb & TREQ);
73 static inline void assert_TIP(void)
75 out_8(&via[B], in_8(&via[B]) & ~TIP);
78 static inline void assert_TACK(void)
80 out_8(&via[B], in_8(&via[B]) & ~TACK);
83 static inline void toggle_TACK(void)
85 out_8(&via[B], in_8(&via[B]) ^ TACK);
88 static inline void negate_TACK(void)
90 out_8(&via[B], in_8(&via[B]) | TACK);
93 static inline void negate_TIP_and_TACK(void)
95 out_8(&via[B], in_8(&via[B]) | TIP | TACK);
98 static enum cuda_state {
107 static struct adb_request *current_req;
108 static struct adb_request *last_req;
109 static unsigned char cuda_rbuf[16];
110 static unsigned char *reply_ptr;
111 static int reading_reply;
112 static int data_index;
115 static struct device_node *vias;
117 static int cuda_fully_inited;
120 static int cuda_probe(void);
121 static int cuda_send_request(struct adb_request *req, int sync);
122 static int cuda_adb_autopoll(int devs);
123 static int cuda_reset_adb_bus(void);
124 #endif /* CONFIG_ADB */
126 static int cuda_init_via(void);
127 static void cuda_start(void);
128 static irqreturn_t cuda_interrupt(int irq, void *arg);
129 static void cuda_input(unsigned char *buf, int nb);
130 void cuda_poll(void);
131 static int cuda_write(struct adb_request *req);
133 int cuda_request(struct adb_request *req,
134 void (*done)(struct adb_request *), int nbytes, ...);
137 struct adb_driver via_cuda_driver = {
140 .send_request = cuda_send_request,
141 .autopoll = cuda_adb_autopoll,
143 .reset_bus = cuda_reset_adb_bus,
145 #endif /* CONFIG_ADB */
148 int __init find_via_cuda(void)
150 struct adb_request req;
153 if (macintosh_config->adb_type != MAC_ADB_CUDA)
159 err = cuda_init_via();
161 printk(KERN_ERR "cuda_init_via() failed\n");
166 /* enable autopoll */
167 cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, 1);
168 while (!req.complete)
174 int __init find_via_cuda(void)
176 struct adb_request req;
183 vias = of_find_node_by_name(NULL, "via-cuda");
187 reg = of_get_property(vias, "reg", NULL);
189 printk(KERN_ERR "via-cuda: No \"reg\" property !\n");
192 taddr = of_translate_address(vias, reg);
194 printk(KERN_ERR "via-cuda: Can't translate address !\n");
197 via = ioremap(taddr, 0x2000);
199 printk(KERN_ERR "via-cuda: Can't map address !\n");
204 sys_ctrler = SYS_CTRLER_CUDA;
206 err = cuda_init_via();
208 printk(KERN_ERR "cuda_init_via() failed\n");
213 /* Clear and enable interrupts, but only on PPC. On 68K it's done */
214 /* for us by the main VIA driver in arch/m68k/mac/via.c */
216 out_8(&via[IFR], 0x7f); /* clear interrupts by writing 1s */
217 out_8(&via[IER], IER_SET|SR_INT); /* enable interrupt from SR */
219 /* enable autopoll */
220 cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, 1);
221 while (!req.complete)
231 #endif /* !defined CONFIG_MAC */
233 static int __init via_cuda_start(void)
239 cuda_irq = IRQ_MAC_ADB;
241 cuda_irq = irq_of_parse_and_map(vias, 0);
243 printk(KERN_ERR "via-cuda: can't map interrupts for %s\n",
249 if (request_irq(cuda_irq, cuda_interrupt, 0, "ADB", cuda_interrupt)) {
250 printk(KERN_ERR "via-cuda: can't request irq %d\n", cuda_irq);
254 pr_info("Macintosh CUDA driver v0.5 for Unified ADB.\n");
256 cuda_fully_inited = 1;
260 device_initcall(via_cuda_start);
267 if (sys_ctrler != SYS_CTRLER_CUDA)
270 if (macintosh_config->adb_type != MAC_ADB_CUDA)
277 #endif /* CONFIG_ADB */
279 #define WAIT_FOR(cond, what) \
282 for (x = 1000; !(cond); --x) { \
284 pr_err("Timeout waiting for " what "\n"); \
292 __init cuda_init_via(void)
294 out_8(&via[DIRB], (in_8(&via[DIRB]) | TACK | TIP) & ~TREQ); /* TACK & TIP out */
295 negate_TIP_and_TACK();
296 out_8(&via[ACR] ,(in_8(&via[ACR]) & ~SR_CTRL) | SR_EXT); /* SR data in */
297 (void)in_8(&via[SR]); /* clear any left-over data */
299 out_8(&via[IER], 0x7f); /* disable interrupts from VIA */
300 (void)in_8(&via[IER]);
302 out_8(&via[IER], SR_INT); /* disable SR interrupt from VIA */
305 /* delay 4ms and then clear any pending interrupt */
307 (void)in_8(&via[SR]);
308 out_8(&via[IFR], SR_INT);
310 /* sync with the CUDA - assert TACK without TIP */
313 /* wait for the CUDA to assert TREQ in response */
314 WAIT_FOR(TREQ_asserted(in_8(&via[B])), "CUDA response to sync");
316 /* wait for the interrupt and then clear it */
317 WAIT_FOR(in_8(&via[IFR]) & SR_INT, "CUDA response to sync (2)");
318 (void)in_8(&via[SR]);
319 out_8(&via[IFR], SR_INT);
321 /* finish the sync by negating TACK */
324 /* wait for the CUDA to negate TREQ and the corresponding interrupt */
325 WAIT_FOR(!TREQ_asserted(in_8(&via[B])), "CUDA response to sync (3)");
326 WAIT_FOR(in_8(&via[IFR]) & SR_INT, "CUDA response to sync (4)");
327 (void)in_8(&via[SR]);
328 out_8(&via[IFR], SR_INT);
334 /* Send an ADB command */
336 cuda_send_request(struct adb_request *req, int sync)
340 if ((via == NULL) || !cuda_fully_inited) {
345 req->reply_expected = 1;
352 while (!req->complete)
359 /* Enable/disable autopolling */
361 cuda_adb_autopoll(int devs)
363 struct adb_request req;
365 if ((via == NULL) || !cuda_fully_inited)
368 cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, (devs? 1: 0));
369 while (!req.complete)
374 /* Reset adb bus - how do we do this?? */
376 cuda_reset_adb_bus(void)
378 struct adb_request req;
380 if ((via == NULL) || !cuda_fully_inited)
383 cuda_request(&req, NULL, 2, ADB_PACKET, 0); /* maybe? */
384 while (!req.complete)
388 #endif /* CONFIG_ADB */
390 /* Construct and send a cuda request */
392 cuda_request(struct adb_request *req, void (*done)(struct adb_request *),
403 req->nbytes = nbytes;
405 va_start(list, nbytes);
406 for (i = 0; i < nbytes; ++i)
407 req->data[i] = va_arg(list, int);
409 req->reply_expected = 1;
410 return cuda_write(req);
412 EXPORT_SYMBOL(cuda_request);
415 cuda_write(struct adb_request *req)
419 if (req->nbytes < 2 || req->data[0] > CUDA_PACKET) {
428 spin_lock_irqsave(&cuda_lock, flags);
429 if (current_req != 0) {
430 last_req->next = req;
435 if (cuda_state == idle)
438 spin_unlock_irqrestore(&cuda_lock, flags);
446 /* assert cuda_state == idle */
447 if (current_req == NULL)
450 if (TREQ_asserted(in_8(&via[B])))
451 return; /* a byte is coming in from the CUDA */
453 /* set the shift register to shift out and send a byte */
454 out_8(&via[ACR], in_8(&via[ACR]) | SR_OUT);
455 out_8(&via[SR], current_req->data[data_index++]);
457 cuda_state = sent_first_byte;
463 cuda_interrupt(0, NULL);
465 EXPORT_SYMBOL(cuda_poll);
467 #define ARRAY_FULL(a, p) ((p) - (a) == ARRAY_SIZE(a))
470 cuda_interrupt(int irq, void *arg)
474 struct adb_request *req = NULL;
475 unsigned char ibuf[16];
479 spin_lock_irqsave(&cuda_lock, flags);
481 /* On powermacs, this handler is registered for the VIA IRQ. But they use
482 * just the shift register IRQ -- other VIA interrupt sources are disabled.
483 * On m68k macs, the VIA IRQ sources are dispatched individually. Unless
484 * we are polling, the shift register IRQ flag has already been cleared.
491 if ((in_8(&via[IFR]) & SR_INT) == 0) {
492 spin_unlock_irqrestore(&cuda_lock, flags);
495 out_8(&via[IFR], SR_INT);
499 status = in_8(&via[B]) & (TIP | TACK | TREQ);
501 switch (cuda_state) {
503 /* CUDA has sent us the first byte of data - unsolicited */
504 (void)in_8(&via[SR]);
506 cuda_state = reading;
507 reply_ptr = cuda_rbuf;
512 /* CUDA has sent us the first byte of data of a reply */
513 (void)in_8(&via[SR]);
515 cuda_state = reading;
516 reply_ptr = current_req->reply;
520 case sent_first_byte:
521 if (TREQ_asserted(status)) {
523 out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT);
524 (void)in_8(&via[SR]);
525 negate_TIP_and_TACK();
528 out_8(&via[SR], current_req->data[data_index++]);
530 cuda_state = sending;
536 if (data_index >= req->nbytes) {
537 out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT);
538 (void)in_8(&via[SR]);
539 negate_TIP_and_TACK();
541 if (req->reply_expected) {
542 cuda_state = awaiting_reply;
544 current_req = req->next;
546 /* not sure about this */
551 out_8(&via[SR], req->data[data_index++]);
557 if (reading_reply ? ARRAY_FULL(current_req->reply, reply_ptr)
558 : ARRAY_FULL(cuda_rbuf, reply_ptr))
559 (void)in_8(&via[SR]);
561 *reply_ptr++ = in_8(&via[SR]);
562 if (!TREQ_asserted(status)) {
563 /* that's all folks */
564 negate_TIP_and_TACK();
565 cuda_state = read_done;
572 (void)in_8(&via[SR]);
575 req->reply_len = reply_ptr - req->reply;
576 if (req->data[0] == ADB_PACKET) {
577 /* Have to adjust the reply from ADB commands */
578 if (req->reply_len <= 2 || (req->reply[1] & 2) != 0) {
579 /* the 0x2 bit indicates no response */
582 /* leave just the command and result bytes in the reply */
584 memmove(req->reply, req->reply + 2, req->reply_len);
587 current_req = req->next;
591 /* This is tricky. We must break the spinlock to call
592 * cuda_input. However, doing so means we might get
593 * re-entered from another CPU getting an interrupt
594 * or calling cuda_poll(). I ended up using the stack
595 * (it's only for 16 bytes) and moving the actual
596 * call to cuda_input to outside of the lock.
598 ibuf_len = reply_ptr - cuda_rbuf;
599 memcpy(ibuf, cuda_rbuf, ibuf_len);
601 reply_ptr = cuda_rbuf;
604 if (cuda_state == idle && TREQ_asserted(in_8(&via[B]))) {
606 cuda_state = reading;
611 pr_err("cuda_interrupt: unknown cuda_state %d?\n", cuda_state);
613 spin_unlock_irqrestore(&cuda_lock, flags);
614 if (complete && req) {
615 void (*done)(struct adb_request *) = req->done;
618 /* Here, we assume that if the request has a done member, the
619 * struct request will survive to setting req->complete to 1
625 cuda_input(ibuf, ibuf_len);
630 cuda_input(unsigned char *buf, int nb)
635 if (nb == 5 && buf[2] == 0x2c) {
636 extern int xmon_wants_key, xmon_adb_keycode;
637 if (xmon_wants_key) {
638 xmon_adb_keycode = buf[3];
642 #endif /* CONFIG_XMON */
644 adb_input(buf+2, nb-2, buf[1] & 0x40);
645 #endif /* CONFIG_ADB */
649 print_hex_dump(KERN_INFO, "cuda_input: ", DUMP_PREFIX_NONE, 32, 1,