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>
23 #include <asm/machdep.h>
24 #include <asm/pmac_feature.h>
26 #include <asm/macintosh.h>
27 #include <asm/macints.h>
28 #include <asm/mac_via.h>
31 #include <linux/init.h>
33 static volatile unsigned char __iomem *via;
34 static DEFINE_SPINLOCK(cuda_lock);
36 /* VIA registers - spaced 0x200 bytes apart */
37 #define RS 0x200 /* skip between registers */
38 #define B 0 /* B-side data */
39 #define A RS /* A-side data */
40 #define DIRB (2*RS) /* B-side direction (1=output) */
41 #define DIRA (3*RS) /* A-side direction (1=output) */
42 #define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */
43 #define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */
44 #define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */
45 #define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */
46 #define T2CL (8*RS) /* Timer 2 ctr/latch (low 8 bits) */
47 #define T2CH (9*RS) /* Timer 2 counter (high 8 bits) */
48 #define SR (10*RS) /* Shift register */
49 #define ACR (11*RS) /* Auxiliary control register */
50 #define PCR (12*RS) /* Peripheral control register */
51 #define IFR (13*RS) /* Interrupt flag register */
52 #define IER (14*RS) /* Interrupt enable register */
53 #define ANH (15*RS) /* A-side data, no handshake */
56 * When the Cuda design replaced the Egret, some signal names and
57 * logic sense changed. They all serve the same purposes, however.
60 * ----------------+------------------------------------------
61 * PB3 (input) | Transceiver session (active low)
62 * PB4 (output) | VIA full (active high)
63 * PB5 (output) | System session (active high)
66 * ----------------+------------------------------------------
67 * PB3 (input) | Transfer request (active low)
68 * PB4 (output) | Byte acknowledge (active low)
69 * PB5 (output) | Transfer in progress (active low)
72 /* Bits in Port B data register */
73 #define TREQ 0x08 /* Transfer request */
74 #define TACK 0x10 /* Transfer acknowledge */
75 #define TIP 0x20 /* Transfer in progress */
78 #define SR_CTRL 0x1c /* Shift register control bits */
79 #define SR_EXT 0x0c /* Shift on external clock */
80 #define SR_OUT 0x10 /* Shift out if 1 */
82 /* Bits in IFR and IER */
83 #define IER_SET 0x80 /* set bits in IER */
84 #define IER_CLR 0 /* clear bits in IER */
85 #define SR_INT 0x04 /* Shift register full/empty */
87 /* Duration of byte acknowledgement pulse (us) */
88 #define EGRET_TACK_ASSERTED_DELAY 300
89 #define EGRET_TACK_NEGATED_DELAY 400
91 /* Interval from interrupt to start of session (us) */
92 #define EGRET_SESSION_DELAY 450
95 #define mcu_is_egret false
97 static bool mcu_is_egret;
100 static inline bool TREQ_asserted(u8 portb)
102 return !(portb & TREQ);
105 static inline void assert_TIP(void)
108 udelay(EGRET_SESSION_DELAY);
109 out_8(&via[B], in_8(&via[B]) | TIP);
111 out_8(&via[B], in_8(&via[B]) & ~TIP);
114 static inline void assert_TIP_and_TACK(void)
117 udelay(EGRET_SESSION_DELAY);
118 out_8(&via[B], in_8(&via[B]) | TIP | TACK);
120 out_8(&via[B], in_8(&via[B]) & ~(TIP | TACK));
123 static inline void assert_TACK(void)
126 udelay(EGRET_TACK_NEGATED_DELAY);
127 out_8(&via[B], in_8(&via[B]) | TACK);
129 out_8(&via[B], in_8(&via[B]) & ~TACK);
132 static inline void toggle_TACK(void)
134 out_8(&via[B], in_8(&via[B]) ^ TACK);
137 static inline void negate_TACK(void)
140 udelay(EGRET_TACK_ASSERTED_DELAY);
141 out_8(&via[B], in_8(&via[B]) & ~TACK);
143 out_8(&via[B], in_8(&via[B]) | TACK);
146 static inline void negate_TIP_and_TACK(void)
149 udelay(EGRET_TACK_ASSERTED_DELAY);
150 out_8(&via[B], in_8(&via[B]) & ~(TIP | TACK));
152 out_8(&via[B], in_8(&via[B]) | TIP | TACK);
155 static enum cuda_state {
164 static struct adb_request *current_req;
165 static struct adb_request *last_req;
166 static unsigned char cuda_rbuf[16];
167 static unsigned char *reply_ptr;
168 static int reading_reply;
169 static int data_index;
172 static struct device_node *vias;
174 static int cuda_fully_inited;
177 static int cuda_probe(void);
178 static int cuda_send_request(struct adb_request *req, int sync);
179 static int cuda_adb_autopoll(int devs);
180 static int cuda_reset_adb_bus(void);
181 #endif /* CONFIG_ADB */
183 static int cuda_init_via(void);
184 static void cuda_start(void);
185 static irqreturn_t cuda_interrupt(int irq, void *arg);
186 static void cuda_input(unsigned char *buf, int nb);
187 void cuda_poll(void);
188 static int cuda_write(struct adb_request *req);
190 int cuda_request(struct adb_request *req,
191 void (*done)(struct adb_request *), int nbytes, ...);
194 struct adb_driver via_cuda_driver = {
197 .send_request = cuda_send_request,
198 .autopoll = cuda_adb_autopoll,
200 .reset_bus = cuda_reset_adb_bus,
202 #endif /* CONFIG_ADB */
205 int __init find_via_cuda(void)
207 struct adb_request req;
210 if (macintosh_config->adb_type != MAC_ADB_CUDA &&
211 macintosh_config->adb_type != MAC_ADB_EGRET)
216 mcu_is_egret = macintosh_config->adb_type == MAC_ADB_EGRET;
218 err = cuda_init_via();
220 printk(KERN_ERR "cuda_init_via() failed\n");
225 /* enable autopoll */
226 cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, 1);
227 while (!req.complete)
233 int __init find_via_cuda(void)
235 struct adb_request req;
242 vias = of_find_node_by_name(NULL, "via-cuda");
246 reg = of_get_property(vias, "reg", NULL);
248 printk(KERN_ERR "via-cuda: No \"reg\" property !\n");
251 taddr = of_translate_address(vias, reg);
253 printk(KERN_ERR "via-cuda: Can't translate address !\n");
256 via = ioremap(taddr, 0x2000);
258 printk(KERN_ERR "via-cuda: Can't map address !\n");
263 sys_ctrler = SYS_CTRLER_CUDA;
265 err = cuda_init_via();
267 printk(KERN_ERR "cuda_init_via() failed\n");
272 /* Clear and enable interrupts, but only on PPC. On 68K it's done */
273 /* for us by the main VIA driver in arch/m68k/mac/via.c */
275 out_8(&via[IFR], 0x7f); /* clear interrupts by writing 1s */
276 out_8(&via[IER], IER_SET|SR_INT); /* enable interrupt from SR */
278 /* enable autopoll */
279 cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, 1);
280 while (!req.complete)
290 #endif /* !defined CONFIG_MAC */
292 static int __init via_cuda_start(void)
298 cuda_irq = IRQ_MAC_ADB;
300 cuda_irq = irq_of_parse_and_map(vias, 0);
302 printk(KERN_ERR "via-cuda: can't map interrupts for %pOF\n",
308 if (request_irq(cuda_irq, cuda_interrupt, 0, "ADB", cuda_interrupt)) {
309 printk(KERN_ERR "via-cuda: can't request irq %d\n", cuda_irq);
313 pr_info("Macintosh Cuda and Egret driver.\n");
315 cuda_fully_inited = 1;
319 device_initcall(via_cuda_start);
326 if (sys_ctrler != SYS_CTRLER_CUDA)
329 if (macintosh_config->adb_type != MAC_ADB_CUDA &&
330 macintosh_config->adb_type != MAC_ADB_EGRET)
337 #endif /* CONFIG_ADB */
339 static int __init sync_egret(void)
341 if (TREQ_asserted(in_8(&via[B]))) {
342 /* Complete the inbound transfer */
343 assert_TIP_and_TACK();
347 (void)in_8(&via[SR]);
349 if (!TREQ_asserted(in_8(&via[B])))
352 negate_TIP_and_TACK();
353 } else if (in_8(&via[B]) & TIP) {
354 /* Terminate the outbound transfer */
358 negate_TIP_and_TACK();
360 /* Clear shift register interrupt */
361 if (in_8(&via[IFR]) & SR_INT)
362 (void)in_8(&via[SR]);
366 #define WAIT_FOR(cond, what) \
369 for (x = 1000; !(cond); --x) { \
371 pr_err("Timeout waiting for " what "\n"); \
379 __init cuda_init_via(void)
382 out_8(&via[IER], 0x7f); /* disable interrupts from VIA */
383 (void)in_8(&via[IER]);
385 out_8(&via[IER], SR_INT); /* disable SR interrupt from VIA */
388 out_8(&via[DIRB], (in_8(&via[DIRB]) | TACK | TIP) & ~TREQ); /* TACK & TIP out */
389 out_8(&via[ACR], (in_8(&via[ACR]) & ~SR_CTRL) | SR_EXT); /* SR data in */
390 (void)in_8(&via[SR]); /* clear any left-over data */
395 negate_TIP_and_TACK();
397 /* delay 4ms and then clear any pending interrupt */
399 (void)in_8(&via[SR]);
400 out_8(&via[IFR], SR_INT);
402 /* sync with the CUDA - assert TACK without TIP */
405 /* wait for the CUDA to assert TREQ in response */
406 WAIT_FOR(TREQ_asserted(in_8(&via[B])), "CUDA response to sync");
408 /* wait for the interrupt and then clear it */
409 WAIT_FOR(in_8(&via[IFR]) & SR_INT, "CUDA response to sync (2)");
410 (void)in_8(&via[SR]);
411 out_8(&via[IFR], SR_INT);
413 /* finish the sync by negating TACK */
416 /* wait for the CUDA to negate TREQ and the corresponding interrupt */
417 WAIT_FOR(!TREQ_asserted(in_8(&via[B])), "CUDA response to sync (3)");
418 WAIT_FOR(in_8(&via[IFR]) & SR_INT, "CUDA response to sync (4)");
419 (void)in_8(&via[SR]);
420 out_8(&via[IFR], SR_INT);
426 /* Send an ADB command */
428 cuda_send_request(struct adb_request *req, int sync)
432 if ((via == NULL) || !cuda_fully_inited) {
437 req->reply_expected = 1;
444 while (!req->complete)
451 /* Enable/disable autopolling */
453 cuda_adb_autopoll(int devs)
455 struct adb_request req;
457 if ((via == NULL) || !cuda_fully_inited)
460 cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, (devs? 1: 0));
461 while (!req.complete)
466 /* Reset adb bus - how do we do this?? */
468 cuda_reset_adb_bus(void)
470 struct adb_request req;
472 if ((via == NULL) || !cuda_fully_inited)
475 cuda_request(&req, NULL, 2, ADB_PACKET, 0); /* maybe? */
476 while (!req.complete)
480 #endif /* CONFIG_ADB */
482 /* Construct and send a cuda request */
484 cuda_request(struct adb_request *req, void (*done)(struct adb_request *),
495 req->nbytes = nbytes;
497 va_start(list, nbytes);
498 for (i = 0; i < nbytes; ++i)
499 req->data[i] = va_arg(list, int);
501 req->reply_expected = 1;
502 return cuda_write(req);
504 EXPORT_SYMBOL(cuda_request);
507 cuda_write(struct adb_request *req)
511 if (req->nbytes < 2 || req->data[0] > CUDA_PACKET) {
520 spin_lock_irqsave(&cuda_lock, flags);
521 if (current_req != 0) {
522 last_req->next = req;
527 if (cuda_state == idle)
530 spin_unlock_irqrestore(&cuda_lock, flags);
538 /* assert cuda_state == idle */
539 if (current_req == NULL)
542 if (TREQ_asserted(in_8(&via[B])))
543 return; /* a byte is coming in from the CUDA */
545 /* set the shift register to shift out and send a byte */
546 out_8(&via[ACR], in_8(&via[ACR]) | SR_OUT);
547 out_8(&via[SR], current_req->data[data_index++]);
549 assert_TIP_and_TACK();
552 cuda_state = sent_first_byte;
558 cuda_interrupt(0, NULL);
560 EXPORT_SYMBOL(cuda_poll);
562 #define ARRAY_FULL(a, p) ((p) - (a) == ARRAY_SIZE(a))
565 cuda_interrupt(int irq, void *arg)
569 struct adb_request *req = NULL;
570 unsigned char ibuf[16];
575 spin_lock_irqsave(&cuda_lock, flags);
577 /* On powermacs, this handler is registered for the VIA IRQ. But they use
578 * just the shift register IRQ -- other VIA interrupt sources are disabled.
579 * On m68k macs, the VIA IRQ sources are dispatched individually. Unless
580 * we are polling, the shift register IRQ flag has already been cleared.
587 if ((in_8(&via[IFR]) & SR_INT) == 0) {
588 spin_unlock_irqrestore(&cuda_lock, flags);
591 out_8(&via[IFR], SR_INT);
595 status = in_8(&via[B]) & (TIP | TACK | TREQ);
597 switch (cuda_state) {
599 /* System controller has unsolicited data for us */
600 (void)in_8(&via[SR]);
603 cuda_state = reading;
604 reply_ptr = cuda_rbuf;
609 /* System controller has reply data for us */
610 (void)in_8(&via[SR]);
612 cuda_state = reading;
613 reply_ptr = current_req->reply;
617 case sent_first_byte:
618 if (TREQ_asserted(status)) {
620 out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT);
621 (void)in_8(&via[SR]);
622 negate_TIP_and_TACK();
624 /* Egret does not raise an "aborted" interrupt */
628 out_8(&via[SR], current_req->data[data_index++]);
632 cuda_state = sending;
638 if (data_index >= req->nbytes) {
639 out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT);
640 (void)in_8(&via[SR]);
641 negate_TIP_and_TACK();
643 if (req->reply_expected) {
644 cuda_state = awaiting_reply;
646 current_req = req->next;
648 /* not sure about this */
653 out_8(&via[SR], req->data[data_index++]);
661 full = reading_reply ? ARRAY_FULL(current_req->reply, reply_ptr)
662 : ARRAY_FULL(cuda_rbuf, reply_ptr);
664 (void)in_8(&via[SR]);
666 *reply_ptr++ = in_8(&via[SR]);
667 if (!TREQ_asserted(status) || full) {
670 /* that's all folks */
671 negate_TIP_and_TACK();
672 cuda_state = read_done;
673 /* Egret does not raise a "read done" interrupt */
675 goto read_done_state;
684 (void)in_8(&via[SR]);
688 req->reply_len = reply_ptr - req->reply;
689 if (req->data[0] == ADB_PACKET) {
690 /* Have to adjust the reply from ADB commands */
691 if (req->reply_len <= 2 || (req->reply[1] & 2) != 0) {
692 /* the 0x2 bit indicates no response */
695 /* leave just the command and result bytes in the reply */
697 memmove(req->reply, req->reply + 2, req->reply_len);
700 current_req = req->next;
704 /* This is tricky. We must break the spinlock to call
705 * cuda_input. However, doing so means we might get
706 * re-entered from another CPU getting an interrupt
707 * or calling cuda_poll(). I ended up using the stack
708 * (it's only for 16 bytes) and moving the actual
709 * call to cuda_input to outside of the lock.
711 ibuf_len = reply_ptr - cuda_rbuf;
712 memcpy(ibuf, cuda_rbuf, ibuf_len);
714 reply_ptr = cuda_rbuf;
717 if (cuda_state == idle && TREQ_asserted(in_8(&via[B]))) {
719 cuda_state = reading;
724 pr_err("cuda_interrupt: unknown cuda_state %d?\n", cuda_state);
726 spin_unlock_irqrestore(&cuda_lock, flags);
727 if (complete && req) {
728 void (*done)(struct adb_request *) = req->done;
731 /* Here, we assume that if the request has a done member, the
732 * struct request will survive to setting req->complete to 1
738 cuda_input(ibuf, ibuf_len);
743 cuda_input(unsigned char *buf, int nb)
748 if (nb == 5 && buf[2] == 0x2c) {
749 extern int xmon_wants_key, xmon_adb_keycode;
750 if (xmon_wants_key) {
751 xmon_adb_keycode = buf[3];
755 #endif /* CONFIG_XMON */
757 adb_input(buf+2, nb-2, buf[1] & 0x40);
758 #endif /* CONFIG_ADB */
762 /* Egret sends these periodically. Might be useful as a 'heartbeat'
763 * to trigger a recovery for the VIA shift register errata.
768 print_hex_dump(KERN_INFO, "cuda_input: ", DUMP_PREFIX_NONE, 32, 1,
773 /* Offset between Unix time (1970-based) and Mac time (1904-based) */
774 #define RTC_OFFSET 2082844800
776 time64_t cuda_get_time(void)
778 struct adb_request req;
781 if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_GET_TIME) < 0)
783 while (!req.complete)
785 if (req.reply_len != 7)
786 pr_err("%s: got %d byte reply\n", __func__, req.reply_len);
787 now = (req.reply[3] << 24) + (req.reply[4] << 16) +
788 (req.reply[5] << 8) + req.reply[6];
789 return (time64_t)now - RTC_OFFSET;
792 int cuda_set_rtc_time(struct rtc_time *tm)
795 struct adb_request req;
797 now = lower_32_bits(rtc_tm_to_time64(tm) + RTC_OFFSET);
798 if (cuda_request(&req, NULL, 6, CUDA_PACKET, CUDA_SET_TIME,
799 now >> 24, now >> 16, now >> 8, now) < 0)
801 while (!req.complete)
803 if ((req.reply_len != 3) && (req.reply_len != 7))
804 pr_err("%s: got %d byte reply\n", __func__, req.reply_len);