1 // SPDX-License-Identifier: GPL-2.0
3 * Device driver for the Cuda and Egret system controllers found on PowerMacs
6 * The Cuda or Egret is a 6805 microcontroller interfaced to the 6522 VIA.
7 * This MCU controls system power, Parameter RAM, Real Time Clock and the
8 * Apple Desktop Bus (ADB) that connects to the keyboard and mouse.
10 * Copyright (C) 1996 Paul Mackerras.
12 #include <linux/stdarg.h>
13 #include <linux/types.h>
14 #include <linux/errno.h>
15 #include <linux/kernel.h>
16 #include <linux/delay.h>
17 #include <linux/adb.h>
18 #include <linux/cuda.h>
19 #include <linux/spinlock.h>
20 #include <linux/interrupt.h>
21 #include <linux/of_address.h>
22 #include <linux/of_irq.h>
25 #include <asm/machdep.h>
26 #include <asm/pmac_feature.h>
28 #include <asm/macintosh.h>
29 #include <asm/macints.h>
30 #include <asm/mac_via.h>
33 #include <linux/init.h>
35 static volatile unsigned char __iomem *via;
36 static DEFINE_SPINLOCK(cuda_lock);
38 /* VIA registers - spaced 0x200 bytes apart */
39 #define RS 0x200 /* skip between registers */
40 #define B 0 /* B-side data */
41 #define A RS /* A-side data */
42 #define DIRB (2*RS) /* B-side direction (1=output) */
43 #define DIRA (3*RS) /* A-side direction (1=output) */
44 #define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */
45 #define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */
46 #define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */
47 #define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */
48 #define T2CL (8*RS) /* Timer 2 ctr/latch (low 8 bits) */
49 #define T2CH (9*RS) /* Timer 2 counter (high 8 bits) */
50 #define SR (10*RS) /* Shift register */
51 #define ACR (11*RS) /* Auxiliary control register */
52 #define PCR (12*RS) /* Peripheral control register */
53 #define IFR (13*RS) /* Interrupt flag register */
54 #define IER (14*RS) /* Interrupt enable register */
55 #define ANH (15*RS) /* A-side data, no handshake */
58 * When the Cuda design replaced the Egret, some signal names and
59 * logic sense changed. They all serve the same purposes, however.
62 * ----------------+------------------------------------------
63 * PB3 (input) | Transceiver session (active low)
64 * PB4 (output) | VIA full (active high)
65 * PB5 (output) | System session (active high)
68 * ----------------+------------------------------------------
69 * PB3 (input) | Transfer request (active low)
70 * PB4 (output) | Byte acknowledge (active low)
71 * PB5 (output) | Transfer in progress (active low)
74 /* Bits in Port B data register */
75 #define TREQ 0x08 /* Transfer request */
76 #define TACK 0x10 /* Transfer acknowledge */
77 #define TIP 0x20 /* Transfer in progress */
80 #define SR_CTRL 0x1c /* Shift register control bits */
81 #define SR_EXT 0x0c /* Shift on external clock */
82 #define SR_OUT 0x10 /* Shift out if 1 */
84 /* Bits in IFR and IER */
85 #define IER_SET 0x80 /* set bits in IER */
86 #define IER_CLR 0 /* clear bits in IER */
87 #define SR_INT 0x04 /* Shift register full/empty */
89 /* Duration of byte acknowledgement pulse (us) */
90 #define EGRET_TACK_ASSERTED_DELAY 300
91 #define EGRET_TACK_NEGATED_DELAY 400
93 /* Interval from interrupt to start of session (us) */
94 #define EGRET_SESSION_DELAY 450
97 #define mcu_is_egret false
99 static bool mcu_is_egret;
102 static inline bool TREQ_asserted(u8 portb)
104 return !(portb & TREQ);
107 static inline void assert_TIP(void)
110 udelay(EGRET_SESSION_DELAY);
111 out_8(&via[B], in_8(&via[B]) | TIP);
113 out_8(&via[B], in_8(&via[B]) & ~TIP);
116 static inline void assert_TIP_and_TACK(void)
119 udelay(EGRET_SESSION_DELAY);
120 out_8(&via[B], in_8(&via[B]) | TIP | TACK);
122 out_8(&via[B], in_8(&via[B]) & ~(TIP | TACK));
125 static inline void assert_TACK(void)
128 udelay(EGRET_TACK_NEGATED_DELAY);
129 out_8(&via[B], in_8(&via[B]) | TACK);
131 out_8(&via[B], in_8(&via[B]) & ~TACK);
134 static inline void toggle_TACK(void)
136 out_8(&via[B], in_8(&via[B]) ^ TACK);
139 static inline void negate_TACK(void)
142 udelay(EGRET_TACK_ASSERTED_DELAY);
143 out_8(&via[B], in_8(&via[B]) & ~TACK);
145 out_8(&via[B], in_8(&via[B]) | TACK);
148 static inline void negate_TIP_and_TACK(void)
151 udelay(EGRET_TACK_ASSERTED_DELAY);
152 out_8(&via[B], in_8(&via[B]) & ~(TIP | TACK));
154 out_8(&via[B], in_8(&via[B]) | TIP | TACK);
157 static enum cuda_state {
166 static struct adb_request *current_req;
167 static struct adb_request *last_req;
168 static unsigned char cuda_rbuf[16];
169 static unsigned char *reply_ptr;
170 static int reading_reply;
171 static int data_index;
174 static struct device_node *vias;
176 static int cuda_fully_inited;
179 static int cuda_probe(void);
180 static int cuda_send_request(struct adb_request *req, int sync);
181 static int cuda_adb_autopoll(int devs);
182 static int cuda_reset_adb_bus(void);
183 #endif /* CONFIG_ADB */
185 static int cuda_init_via(void);
186 static void cuda_start(void);
187 static irqreturn_t cuda_interrupt(int irq, void *arg);
188 static void cuda_input(unsigned char *buf, int nb);
189 void cuda_poll(void);
190 static int cuda_write(struct adb_request *req);
192 int cuda_request(struct adb_request *req,
193 void (*done)(struct adb_request *), int nbytes, ...);
196 struct adb_driver via_cuda_driver = {
199 .send_request = cuda_send_request,
200 .autopoll = cuda_adb_autopoll,
202 .reset_bus = cuda_reset_adb_bus,
204 #endif /* CONFIG_ADB */
207 int __init find_via_cuda(void)
209 struct adb_request req;
212 if (macintosh_config->adb_type != MAC_ADB_CUDA &&
213 macintosh_config->adb_type != MAC_ADB_EGRET)
218 mcu_is_egret = macintosh_config->adb_type == MAC_ADB_EGRET;
220 err = cuda_init_via();
222 printk(KERN_ERR "cuda_init_via() failed\n");
227 /* enable autopoll */
228 cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, 1);
229 while (!req.complete)
235 int __init find_via_cuda(void)
237 struct adb_request req;
244 vias = of_find_node_by_name(NULL, "via-cuda");
248 reg = of_get_property(vias, "reg", NULL);
250 printk(KERN_ERR "via-cuda: No \"reg\" property !\n");
253 taddr = of_translate_address(vias, reg);
255 printk(KERN_ERR "via-cuda: Can't translate address !\n");
258 via = ioremap(taddr, 0x2000);
260 printk(KERN_ERR "via-cuda: Can't map address !\n");
265 sys_ctrler = SYS_CTRLER_CUDA;
267 err = cuda_init_via();
269 printk(KERN_ERR "cuda_init_via() failed\n");
274 /* Clear and enable interrupts, but only on PPC. On 68K it's done */
275 /* for us by the main VIA driver in arch/m68k/mac/via.c */
277 out_8(&via[IFR], 0x7f); /* clear interrupts by writing 1s */
278 out_8(&via[IER], IER_SET|SR_INT); /* enable interrupt from SR */
280 /* enable autopoll */
281 cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, 1);
282 while (!req.complete)
292 #endif /* !defined CONFIG_MAC */
294 static int __init via_cuda_start(void)
300 cuda_irq = IRQ_MAC_ADB;
302 cuda_irq = irq_of_parse_and_map(vias, 0);
304 printk(KERN_ERR "via-cuda: can't map interrupts for %pOF\n",
310 if (request_irq(cuda_irq, cuda_interrupt, 0, "ADB", cuda_interrupt)) {
311 printk(KERN_ERR "via-cuda: can't request irq %d\n", cuda_irq);
315 pr_info("Macintosh Cuda and Egret driver.\n");
317 cuda_fully_inited = 1;
321 device_initcall(via_cuda_start);
328 if (sys_ctrler != SYS_CTRLER_CUDA)
331 if (macintosh_config->adb_type != MAC_ADB_CUDA &&
332 macintosh_config->adb_type != MAC_ADB_EGRET)
339 #endif /* CONFIG_ADB */
341 static int __init sync_egret(void)
343 if (TREQ_asserted(in_8(&via[B]))) {
344 /* Complete the inbound transfer */
345 assert_TIP_and_TACK();
349 (void)in_8(&via[SR]);
351 if (!TREQ_asserted(in_8(&via[B])))
354 negate_TIP_and_TACK();
355 } else if (in_8(&via[B]) & TIP) {
356 /* Terminate the outbound transfer */
360 negate_TIP_and_TACK();
362 /* Clear shift register interrupt */
363 if (in_8(&via[IFR]) & SR_INT)
364 (void)in_8(&via[SR]);
368 #define WAIT_FOR(cond, what) \
371 for (x = 1000; !(cond); --x) { \
373 pr_err("Timeout waiting for " what "\n"); \
381 __init cuda_init_via(void)
384 out_8(&via[IER], 0x7f); /* disable interrupts from VIA */
385 (void)in_8(&via[IER]);
387 out_8(&via[IER], SR_INT); /* disable SR interrupt from VIA */
390 out_8(&via[DIRB], (in_8(&via[DIRB]) | TACK | TIP) & ~TREQ); /* TACK & TIP out */
391 out_8(&via[ACR], (in_8(&via[ACR]) & ~SR_CTRL) | SR_EXT); /* SR data in */
392 (void)in_8(&via[SR]); /* clear any left-over data */
397 negate_TIP_and_TACK();
399 /* delay 4ms and then clear any pending interrupt */
401 (void)in_8(&via[SR]);
402 out_8(&via[IFR], SR_INT);
404 /* sync with the CUDA - assert TACK without TIP */
407 /* wait for the CUDA to assert TREQ in response */
408 WAIT_FOR(TREQ_asserted(in_8(&via[B])), "CUDA response to sync");
410 /* wait for the interrupt and then clear it */
411 WAIT_FOR(in_8(&via[IFR]) & SR_INT, "CUDA response to sync (2)");
412 (void)in_8(&via[SR]);
413 out_8(&via[IFR], SR_INT);
415 /* finish the sync by negating TACK */
418 /* wait for the CUDA to negate TREQ and the corresponding interrupt */
419 WAIT_FOR(!TREQ_asserted(in_8(&via[B])), "CUDA response to sync (3)");
420 WAIT_FOR(in_8(&via[IFR]) & SR_INT, "CUDA response to sync (4)");
421 (void)in_8(&via[SR]);
422 out_8(&via[IFR], SR_INT);
428 /* Send an ADB command */
430 cuda_send_request(struct adb_request *req, int sync)
434 if ((via == NULL) || !cuda_fully_inited) {
439 req->reply_expected = 1;
446 while (!req->complete)
453 /* Enable/disable autopolling */
455 cuda_adb_autopoll(int devs)
457 struct adb_request req;
459 if ((via == NULL) || !cuda_fully_inited)
462 cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, (devs? 1: 0));
463 while (!req.complete)
468 /* Reset adb bus - how do we do this?? */
470 cuda_reset_adb_bus(void)
472 struct adb_request req;
474 if ((via == NULL) || !cuda_fully_inited)
477 cuda_request(&req, NULL, 2, ADB_PACKET, 0); /* maybe? */
478 while (!req.complete)
482 #endif /* CONFIG_ADB */
484 /* Construct and send a cuda request */
486 cuda_request(struct adb_request *req, void (*done)(struct adb_request *),
497 req->nbytes = nbytes;
499 va_start(list, nbytes);
500 for (i = 0; i < nbytes; ++i)
501 req->data[i] = va_arg(list, int);
503 req->reply_expected = 1;
504 return cuda_write(req);
506 EXPORT_SYMBOL(cuda_request);
509 cuda_write(struct adb_request *req)
513 if (req->nbytes < 2 || req->data[0] > CUDA_PACKET) {
522 spin_lock_irqsave(&cuda_lock, flags);
524 last_req->next = req;
529 if (cuda_state == idle)
532 spin_unlock_irqrestore(&cuda_lock, flags);
540 /* assert cuda_state == idle */
541 if (current_req == NULL)
544 if (TREQ_asserted(in_8(&via[B])))
545 return; /* a byte is coming in from the CUDA */
547 /* set the shift register to shift out and send a byte */
548 out_8(&via[ACR], in_8(&via[ACR]) | SR_OUT);
549 out_8(&via[SR], current_req->data[data_index++]);
551 assert_TIP_and_TACK();
554 cuda_state = sent_first_byte;
560 cuda_interrupt(0, NULL);
562 EXPORT_SYMBOL(cuda_poll);
564 #define ARRAY_FULL(a, p) ((p) - (a) == ARRAY_SIZE(a))
567 cuda_interrupt(int irq, void *arg)
571 struct adb_request *req = NULL;
572 unsigned char ibuf[16];
577 spin_lock_irqsave(&cuda_lock, flags);
579 /* On powermacs, this handler is registered for the VIA IRQ. But they use
580 * just the shift register IRQ -- other VIA interrupt sources are disabled.
581 * On m68k macs, the VIA IRQ sources are dispatched individually. Unless
582 * we are polling, the shift register IRQ flag has already been cleared.
589 if ((in_8(&via[IFR]) & SR_INT) == 0) {
590 spin_unlock_irqrestore(&cuda_lock, flags);
593 out_8(&via[IFR], SR_INT);
597 status = in_8(&via[B]) & (TIP | TACK | TREQ);
599 switch (cuda_state) {
601 /* System controller has unsolicited data for us */
602 (void)in_8(&via[SR]);
605 cuda_state = reading;
606 reply_ptr = cuda_rbuf;
611 /* System controller has reply data for us */
612 (void)in_8(&via[SR]);
614 cuda_state = reading;
615 reply_ptr = current_req->reply;
619 case sent_first_byte:
620 if (TREQ_asserted(status)) {
622 out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT);
623 (void)in_8(&via[SR]);
624 negate_TIP_and_TACK();
626 /* Egret does not raise an "aborted" interrupt */
630 out_8(&via[SR], current_req->data[data_index++]);
634 cuda_state = sending;
640 if (data_index >= req->nbytes) {
641 out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT);
642 (void)in_8(&via[SR]);
643 negate_TIP_and_TACK();
645 if (req->reply_expected) {
646 cuda_state = awaiting_reply;
648 current_req = req->next;
650 /* not sure about this */
655 out_8(&via[SR], req->data[data_index++]);
663 full = reading_reply ? ARRAY_FULL(current_req->reply, reply_ptr)
664 : ARRAY_FULL(cuda_rbuf, reply_ptr);
666 (void)in_8(&via[SR]);
668 *reply_ptr++ = in_8(&via[SR]);
669 if (!TREQ_asserted(status) || full) {
672 /* that's all folks */
673 negate_TIP_and_TACK();
674 cuda_state = read_done;
675 /* Egret does not raise a "read done" interrupt */
677 goto read_done_state;
686 (void)in_8(&via[SR]);
690 req->reply_len = reply_ptr - req->reply;
691 if (req->data[0] == ADB_PACKET) {
692 /* Have to adjust the reply from ADB commands */
693 if (req->reply_len <= 2 || (req->reply[1] & 2) != 0) {
694 /* the 0x2 bit indicates no response */
697 /* leave just the command and result bytes in the reply */
699 memmove(req->reply, req->reply + 2, req->reply_len);
702 current_req = req->next;
706 /* This is tricky. We must break the spinlock to call
707 * cuda_input. However, doing so means we might get
708 * re-entered from another CPU getting an interrupt
709 * or calling cuda_poll(). I ended up using the stack
710 * (it's only for 16 bytes) and moving the actual
711 * call to cuda_input to outside of the lock.
713 ibuf_len = reply_ptr - cuda_rbuf;
714 memcpy(ibuf, cuda_rbuf, ibuf_len);
716 reply_ptr = cuda_rbuf;
719 if (cuda_state == idle && TREQ_asserted(in_8(&via[B]))) {
721 cuda_state = reading;
726 pr_err("cuda_interrupt: unknown cuda_state %d?\n", cuda_state);
728 spin_unlock_irqrestore(&cuda_lock, flags);
729 if (complete && req) {
730 void (*done)(struct adb_request *) = req->done;
733 /* Here, we assume that if the request has a done member, the
734 * struct request will survive to setting req->complete to 1
740 cuda_input(ibuf, ibuf_len);
745 cuda_input(unsigned char *buf, int nb)
750 if (nb == 5 && buf[2] == 0x2c) {
751 extern int xmon_wants_key, xmon_adb_keycode;
752 if (xmon_wants_key) {
753 xmon_adb_keycode = buf[3];
757 #endif /* CONFIG_XMON */
759 adb_input(buf+2, nb-2, buf[1] & 0x40);
760 #endif /* CONFIG_ADB */
764 /* Egret sends these periodically. Might be useful as a 'heartbeat'
765 * to trigger a recovery for the VIA shift register errata.
770 print_hex_dump(KERN_INFO, "cuda_input: ", DUMP_PREFIX_NONE, 32, 1,
775 /* Offset between Unix time (1970-based) and Mac time (1904-based) */
776 #define RTC_OFFSET 2082844800
778 time64_t cuda_get_time(void)
780 struct adb_request req;
783 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_GET_TIME) < 0)
785 while (!req.complete)
787 if (req.reply_len != 7)
788 pr_err("%s: got %d byte reply\n", __func__, req.reply_len);
789 now = (req.reply[3] << 24) + (req.reply[4] << 16) +
790 (req.reply[5] << 8) + req.reply[6];
791 return (time64_t)now - RTC_OFFSET;
794 int cuda_set_rtc_time(struct rtc_time *tm)
797 struct adb_request req;
799 now = lower_32_bits(rtc_tm_to_time64(tm) + RTC_OFFSET);
800 if (cuda_request(&req, NULL, 6, CUDA_PACKET, CUDA_SET_TIME,
801 now >> 24, now >> 16, now >> 8, now) < 0)
803 while (!req.complete)
805 if ((req.reply_len != 3) && (req.reply_len != 7))
806 pr_err("%s: got %d byte reply\n", __func__, req.reply_len);