2 * Device driver for the Cuda and Egret system controllers found on PowerMacs
5 * The Cuda or Egret is a 6805 microcontroller interfaced to the 6522 VIA.
6 * This MCU controls system power, Parameter RAM, Real Time Clock and the
7 * Apple Desktop Bus (ADB) that connects to the keyboard and mouse.
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 */
54 * When the Cuda design replaced the Egret, some signal names and
55 * logic sense changed. They all serve the same purposes, however.
58 * ----------------+------------------------------------------
59 * PB3 (input) | Transceiver session (active low)
60 * PB4 (output) | VIA full (active high)
61 * PB5 (output) | System session (active high)
64 * ----------------+------------------------------------------
65 * PB3 (input) | Transfer request (active low)
66 * PB4 (output) | Byte acknowledge (active low)
67 * PB5 (output) | Transfer in progress (active low)
70 /* Bits in Port B data register */
71 #define TREQ 0x08 /* Transfer request */
72 #define TACK 0x10 /* Transfer acknowledge */
73 #define TIP 0x20 /* Transfer in progress */
76 #define SR_CTRL 0x1c /* Shift register control bits */
77 #define SR_EXT 0x0c /* Shift on external clock */
78 #define SR_OUT 0x10 /* Shift out if 1 */
80 /* Bits in IFR and IER */
81 #define IER_SET 0x80 /* set bits in IER */
82 #define IER_CLR 0 /* clear bits in IER */
83 #define SR_INT 0x04 /* Shift register full/empty */
85 /* Duration of byte acknowledgement pulse (us) */
86 #define EGRET_TACK_ASSERTED_DELAY 300
87 #define EGRET_TACK_NEGATED_DELAY 400
89 /* Interval from interrupt to start of session (us) */
90 #define EGRET_SESSION_DELAY 450
93 #define mcu_is_egret false
95 static bool mcu_is_egret;
98 static inline bool TREQ_asserted(u8 portb)
100 return !(portb & TREQ);
103 static inline void assert_TIP(void)
106 udelay(EGRET_SESSION_DELAY);
107 out_8(&via[B], in_8(&via[B]) | TIP);
109 out_8(&via[B], in_8(&via[B]) & ~TIP);
112 static inline void assert_TIP_and_TACK(void)
115 udelay(EGRET_SESSION_DELAY);
116 out_8(&via[B], in_8(&via[B]) | TIP | TACK);
118 out_8(&via[B], in_8(&via[B]) & ~(TIP | TACK));
121 static inline void assert_TACK(void)
124 udelay(EGRET_TACK_NEGATED_DELAY);
125 out_8(&via[B], in_8(&via[B]) | TACK);
127 out_8(&via[B], in_8(&via[B]) & ~TACK);
130 static inline void toggle_TACK(void)
132 out_8(&via[B], in_8(&via[B]) ^ TACK);
135 static inline void negate_TACK(void)
138 udelay(EGRET_TACK_ASSERTED_DELAY);
139 out_8(&via[B], in_8(&via[B]) & ~TACK);
141 out_8(&via[B], in_8(&via[B]) | TACK);
144 static inline void negate_TIP_and_TACK(void)
147 udelay(EGRET_TACK_ASSERTED_DELAY);
148 out_8(&via[B], in_8(&via[B]) & ~(TIP | TACK));
150 out_8(&via[B], in_8(&via[B]) | TIP | TACK);
153 static enum cuda_state {
162 static struct adb_request *current_req;
163 static struct adb_request *last_req;
164 static unsigned char cuda_rbuf[16];
165 static unsigned char *reply_ptr;
166 static int reading_reply;
167 static int data_index;
170 static struct device_node *vias;
172 static int cuda_fully_inited;
175 static int cuda_probe(void);
176 static int cuda_send_request(struct adb_request *req, int sync);
177 static int cuda_adb_autopoll(int devs);
178 static int cuda_reset_adb_bus(void);
179 #endif /* CONFIG_ADB */
181 static int cuda_init_via(void);
182 static void cuda_start(void);
183 static irqreturn_t cuda_interrupt(int irq, void *arg);
184 static void cuda_input(unsigned char *buf, int nb);
185 void cuda_poll(void);
186 static int cuda_write(struct adb_request *req);
188 int cuda_request(struct adb_request *req,
189 void (*done)(struct adb_request *), int nbytes, ...);
192 struct adb_driver via_cuda_driver = {
195 .send_request = cuda_send_request,
196 .autopoll = cuda_adb_autopoll,
198 .reset_bus = cuda_reset_adb_bus,
200 #endif /* CONFIG_ADB */
203 int __init find_via_cuda(void)
205 struct adb_request req;
208 if (macintosh_config->adb_type != MAC_ADB_CUDA &&
209 macintosh_config->adb_type != MAC_ADB_EGRET)
214 mcu_is_egret = macintosh_config->adb_type == MAC_ADB_EGRET;
216 err = cuda_init_via();
218 printk(KERN_ERR "cuda_init_via() failed\n");
223 /* enable autopoll */
224 cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, 1);
225 while (!req.complete)
231 int __init find_via_cuda(void)
233 struct adb_request req;
240 vias = of_find_node_by_name(NULL, "via-cuda");
244 reg = of_get_property(vias, "reg", NULL);
246 printk(KERN_ERR "via-cuda: No \"reg\" property !\n");
249 taddr = of_translate_address(vias, reg);
251 printk(KERN_ERR "via-cuda: Can't translate address !\n");
254 via = ioremap(taddr, 0x2000);
256 printk(KERN_ERR "via-cuda: Can't map address !\n");
261 sys_ctrler = SYS_CTRLER_CUDA;
263 err = cuda_init_via();
265 printk(KERN_ERR "cuda_init_via() failed\n");
270 /* Clear and enable interrupts, but only on PPC. On 68K it's done */
271 /* for us by the main VIA driver in arch/m68k/mac/via.c */
273 out_8(&via[IFR], 0x7f); /* clear interrupts by writing 1s */
274 out_8(&via[IER], IER_SET|SR_INT); /* enable interrupt from SR */
276 /* enable autopoll */
277 cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, 1);
278 while (!req.complete)
288 #endif /* !defined CONFIG_MAC */
290 static int __init via_cuda_start(void)
296 cuda_irq = IRQ_MAC_ADB;
298 cuda_irq = irq_of_parse_and_map(vias, 0);
300 printk(KERN_ERR "via-cuda: can't map interrupts for %pOF\n",
306 if (request_irq(cuda_irq, cuda_interrupt, 0, "ADB", cuda_interrupt)) {
307 printk(KERN_ERR "via-cuda: can't request irq %d\n", cuda_irq);
311 pr_info("Macintosh Cuda and Egret driver.\n");
313 cuda_fully_inited = 1;
317 device_initcall(via_cuda_start);
324 if (sys_ctrler != SYS_CTRLER_CUDA)
327 if (macintosh_config->adb_type != MAC_ADB_CUDA &&
328 macintosh_config->adb_type != MAC_ADB_EGRET)
335 #endif /* CONFIG_ADB */
337 static int __init sync_egret(void)
339 if (TREQ_asserted(in_8(&via[B]))) {
340 /* Complete the inbound transfer */
341 assert_TIP_and_TACK();
345 (void)in_8(&via[SR]);
347 if (!TREQ_asserted(in_8(&via[B])))
350 negate_TIP_and_TACK();
351 } else if (in_8(&via[B]) & TIP) {
352 /* Terminate the outbound transfer */
356 negate_TIP_and_TACK();
358 /* Clear shift register interrupt */
359 if (in_8(&via[IFR]) & SR_INT)
360 (void)in_8(&via[SR]);
364 #define WAIT_FOR(cond, what) \
367 for (x = 1000; !(cond); --x) { \
369 pr_err("Timeout waiting for " what "\n"); \
377 __init cuda_init_via(void)
380 out_8(&via[IER], 0x7f); /* disable interrupts from VIA */
381 (void)in_8(&via[IER]);
383 out_8(&via[IER], SR_INT); /* disable SR interrupt from VIA */
386 out_8(&via[DIRB], (in_8(&via[DIRB]) | TACK | TIP) & ~TREQ); /* TACK & TIP out */
387 out_8(&via[ACR], (in_8(&via[ACR]) & ~SR_CTRL) | SR_EXT); /* SR data in */
388 (void)in_8(&via[SR]); /* clear any left-over data */
393 negate_TIP_and_TACK();
395 /* delay 4ms and then clear any pending interrupt */
397 (void)in_8(&via[SR]);
398 out_8(&via[IFR], SR_INT);
400 /* sync with the CUDA - assert TACK without TIP */
403 /* wait for the CUDA to assert TREQ in response */
404 WAIT_FOR(TREQ_asserted(in_8(&via[B])), "CUDA response to sync");
406 /* wait for the interrupt and then clear it */
407 WAIT_FOR(in_8(&via[IFR]) & SR_INT, "CUDA response to sync (2)");
408 (void)in_8(&via[SR]);
409 out_8(&via[IFR], SR_INT);
411 /* finish the sync by negating TACK */
414 /* wait for the CUDA to negate TREQ and the corresponding interrupt */
415 WAIT_FOR(!TREQ_asserted(in_8(&via[B])), "CUDA response to sync (3)");
416 WAIT_FOR(in_8(&via[IFR]) & SR_INT, "CUDA response to sync (4)");
417 (void)in_8(&via[SR]);
418 out_8(&via[IFR], SR_INT);
424 /* Send an ADB command */
426 cuda_send_request(struct adb_request *req, int sync)
430 if ((via == NULL) || !cuda_fully_inited) {
435 req->reply_expected = 1;
442 while (!req->complete)
449 /* Enable/disable autopolling */
451 cuda_adb_autopoll(int devs)
453 struct adb_request req;
455 if ((via == NULL) || !cuda_fully_inited)
458 cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, (devs? 1: 0));
459 while (!req.complete)
464 /* Reset adb bus - how do we do this?? */
466 cuda_reset_adb_bus(void)
468 struct adb_request req;
470 if ((via == NULL) || !cuda_fully_inited)
473 cuda_request(&req, NULL, 2, ADB_PACKET, 0); /* maybe? */
474 while (!req.complete)
478 #endif /* CONFIG_ADB */
480 /* Construct and send a cuda request */
482 cuda_request(struct adb_request *req, void (*done)(struct adb_request *),
493 req->nbytes = nbytes;
495 va_start(list, nbytes);
496 for (i = 0; i < nbytes; ++i)
497 req->data[i] = va_arg(list, int);
499 req->reply_expected = 1;
500 return cuda_write(req);
502 EXPORT_SYMBOL(cuda_request);
505 cuda_write(struct adb_request *req)
509 if (req->nbytes < 2 || req->data[0] > CUDA_PACKET) {
518 spin_lock_irqsave(&cuda_lock, flags);
519 if (current_req != 0) {
520 last_req->next = req;
525 if (cuda_state == idle)
528 spin_unlock_irqrestore(&cuda_lock, flags);
536 /* assert cuda_state == idle */
537 if (current_req == NULL)
540 if (TREQ_asserted(in_8(&via[B])))
541 return; /* a byte is coming in from the CUDA */
543 /* set the shift register to shift out and send a byte */
544 out_8(&via[ACR], in_8(&via[ACR]) | SR_OUT);
545 out_8(&via[SR], current_req->data[data_index++]);
547 assert_TIP_and_TACK();
550 cuda_state = sent_first_byte;
556 cuda_interrupt(0, NULL);
558 EXPORT_SYMBOL(cuda_poll);
560 #define ARRAY_FULL(a, p) ((p) - (a) == ARRAY_SIZE(a))
563 cuda_interrupt(int irq, void *arg)
567 struct adb_request *req = NULL;
568 unsigned char ibuf[16];
572 spin_lock_irqsave(&cuda_lock, flags);
574 /* On powermacs, this handler is registered for the VIA IRQ. But they use
575 * just the shift register IRQ -- other VIA interrupt sources are disabled.
576 * On m68k macs, the VIA IRQ sources are dispatched individually. Unless
577 * we are polling, the shift register IRQ flag has already been cleared.
584 if ((in_8(&via[IFR]) & SR_INT) == 0) {
585 spin_unlock_irqrestore(&cuda_lock, flags);
588 out_8(&via[IFR], SR_INT);
592 status = in_8(&via[B]) & (TIP | TACK | TREQ);
594 switch (cuda_state) {
596 /* System controller has unsolicited data for us */
597 (void)in_8(&via[SR]);
600 cuda_state = reading;
601 reply_ptr = cuda_rbuf;
606 /* System controller has reply data for us */
607 (void)in_8(&via[SR]);
609 cuda_state = reading;
610 reply_ptr = current_req->reply;
614 case sent_first_byte:
615 if (TREQ_asserted(status)) {
617 out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT);
618 (void)in_8(&via[SR]);
619 negate_TIP_and_TACK();
621 /* Egret does not raise an "aborted" interrupt */
625 out_8(&via[SR], current_req->data[data_index++]);
629 cuda_state = sending;
635 if (data_index >= req->nbytes) {
636 out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT);
637 (void)in_8(&via[SR]);
638 negate_TIP_and_TACK();
640 if (req->reply_expected) {
641 cuda_state = awaiting_reply;
643 current_req = req->next;
645 /* not sure about this */
650 out_8(&via[SR], req->data[data_index++]);
658 if (reading_reply ? ARRAY_FULL(current_req->reply, reply_ptr)
659 : ARRAY_FULL(cuda_rbuf, reply_ptr))
660 (void)in_8(&via[SR]);
662 *reply_ptr++ = in_8(&via[SR]);
663 if (!TREQ_asserted(status)) {
666 /* that's all folks */
667 negate_TIP_and_TACK();
668 cuda_state = read_done;
669 /* Egret does not raise a "read done" interrupt */
671 goto read_done_state;
680 (void)in_8(&via[SR]);
684 req->reply_len = reply_ptr - req->reply;
685 if (req->data[0] == ADB_PACKET) {
686 /* Have to adjust the reply from ADB commands */
687 if (req->reply_len <= 2 || (req->reply[1] & 2) != 0) {
688 /* the 0x2 bit indicates no response */
691 /* leave just the command and result bytes in the reply */
693 memmove(req->reply, req->reply + 2, req->reply_len);
696 current_req = req->next;
700 /* This is tricky. We must break the spinlock to call
701 * cuda_input. However, doing so means we might get
702 * re-entered from another CPU getting an interrupt
703 * or calling cuda_poll(). I ended up using the stack
704 * (it's only for 16 bytes) and moving the actual
705 * call to cuda_input to outside of the lock.
707 ibuf_len = reply_ptr - cuda_rbuf;
708 memcpy(ibuf, cuda_rbuf, ibuf_len);
710 reply_ptr = cuda_rbuf;
713 if (cuda_state == idle && TREQ_asserted(in_8(&via[B]))) {
715 cuda_state = reading;
720 pr_err("cuda_interrupt: unknown cuda_state %d?\n", cuda_state);
722 spin_unlock_irqrestore(&cuda_lock, flags);
723 if (complete && req) {
724 void (*done)(struct adb_request *) = req->done;
727 /* Here, we assume that if the request has a done member, the
728 * struct request will survive to setting req->complete to 1
734 cuda_input(ibuf, ibuf_len);
739 cuda_input(unsigned char *buf, int nb)
744 if (nb == 5 && buf[2] == 0x2c) {
745 extern int xmon_wants_key, xmon_adb_keycode;
746 if (xmon_wants_key) {
747 xmon_adb_keycode = buf[3];
751 #endif /* CONFIG_XMON */
753 adb_input(buf+2, nb-2, buf[1] & 0x40);
754 #endif /* CONFIG_ADB */
758 /* Egret sends these periodically. Might be useful as a 'heartbeat'
759 * to trigger a recovery for the VIA shift register errata.
764 print_hex_dump(KERN_INFO, "cuda_input: ", DUMP_PREFIX_NONE, 32, 1,