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.
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>
25 #include <asm/macintosh.h>
26 #include <asm/macints.h>
27 #include <asm/mac_via.h>
30 #include <linux/init.h>
32 static volatile unsigned char __iomem
*via
;
33 static DEFINE_SPINLOCK(cuda_lock
);
35 /* VIA registers - spaced 0x200 bytes apart */
36 #define RS 0x200 /* skip between registers */
37 #define B 0 /* B-side data */
38 #define A RS /* A-side data */
39 #define DIRB (2*RS) /* B-side direction (1=output) */
40 #define DIRA (3*RS) /* A-side direction (1=output) */
41 #define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */
42 #define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */
43 #define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */
44 #define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */
45 #define T2CL (8*RS) /* Timer 2 ctr/latch (low 8 bits) */
46 #define T2CH (9*RS) /* Timer 2 counter (high 8 bits) */
47 #define SR (10*RS) /* Shift register */
48 #define ACR (11*RS) /* Auxiliary control register */
49 #define PCR (12*RS) /* Peripheral control register */
50 #define IFR (13*RS) /* Interrupt flag register */
51 #define IER (14*RS) /* Interrupt enable register */
52 #define ANH (15*RS) /* A-side data, no handshake */
55 * When the Cuda design replaced the Egret, some signal names and
56 * logic sense changed. They all serve the same purposes, however.
59 * ----------------+------------------------------------------
60 * PB3 (input) | Transceiver session (active low)
61 * PB4 (output) | VIA full (active high)
62 * PB5 (output) | System session (active high)
65 * ----------------+------------------------------------------
66 * PB3 (input) | Transfer request (active low)
67 * PB4 (output) | Byte acknowledge (active low)
68 * PB5 (output) | Transfer in progress (active low)
71 /* Bits in Port B data register */
72 #define TREQ 0x08 /* Transfer request */
73 #define TACK 0x10 /* Transfer acknowledge */
74 #define TIP 0x20 /* Transfer in progress */
77 #define SR_CTRL 0x1c /* Shift register control bits */
78 #define SR_EXT 0x0c /* Shift on external clock */
79 #define SR_OUT 0x10 /* Shift out if 1 */
81 /* Bits in IFR and IER */
82 #define IER_SET 0x80 /* set bits in IER */
83 #define IER_CLR 0 /* clear bits in IER */
84 #define SR_INT 0x04 /* Shift register full/empty */
86 /* Duration of byte acknowledgement pulse (us) */
87 #define EGRET_TACK_ASSERTED_DELAY 300
88 #define EGRET_TACK_NEGATED_DELAY 400
90 /* Interval from interrupt to start of session (us) */
91 #define EGRET_SESSION_DELAY 450
94 #define mcu_is_egret false
96 static bool mcu_is_egret
;
99 static inline bool TREQ_asserted(u8 portb
)
101 return !(portb
& TREQ
);
104 static inline void assert_TIP(void)
107 udelay(EGRET_SESSION_DELAY
);
108 out_8(&via
[B
], in_8(&via
[B
]) | TIP
);
110 out_8(&via
[B
], in_8(&via
[B
]) & ~TIP
);
113 static inline void assert_TIP_and_TACK(void)
116 udelay(EGRET_SESSION_DELAY
);
117 out_8(&via
[B
], in_8(&via
[B
]) | TIP
| TACK
);
119 out_8(&via
[B
], in_8(&via
[B
]) & ~(TIP
| TACK
));
122 static inline void assert_TACK(void)
125 udelay(EGRET_TACK_NEGATED_DELAY
);
126 out_8(&via
[B
], in_8(&via
[B
]) | TACK
);
128 out_8(&via
[B
], in_8(&via
[B
]) & ~TACK
);
131 static inline void toggle_TACK(void)
133 out_8(&via
[B
], in_8(&via
[B
]) ^ TACK
);
136 static inline void negate_TACK(void)
139 udelay(EGRET_TACK_ASSERTED_DELAY
);
140 out_8(&via
[B
], in_8(&via
[B
]) & ~TACK
);
142 out_8(&via
[B
], in_8(&via
[B
]) | TACK
);
145 static inline void negate_TIP_and_TACK(void)
148 udelay(EGRET_TACK_ASSERTED_DELAY
);
149 out_8(&via
[B
], in_8(&via
[B
]) & ~(TIP
| TACK
));
151 out_8(&via
[B
], in_8(&via
[B
]) | TIP
| TACK
);
154 static enum cuda_state
{
163 static struct adb_request
*current_req
;
164 static struct adb_request
*last_req
;
165 static unsigned char cuda_rbuf
[16];
166 static unsigned char *reply_ptr
;
167 static int reading_reply
;
168 static int data_index
;
171 static struct device_node
*vias
;
173 static int cuda_fully_inited
;
176 static int cuda_probe(void);
177 static int cuda_send_request(struct adb_request
*req
, int sync
);
178 static int cuda_adb_autopoll(int devs
);
179 static int cuda_reset_adb_bus(void);
180 #endif /* CONFIG_ADB */
182 static int cuda_init_via(void);
183 static void cuda_start(void);
184 static irqreturn_t
cuda_interrupt(int irq
, void *arg
);
185 static void cuda_input(unsigned char *buf
, int nb
);
186 void cuda_poll(void);
187 static int cuda_write(struct adb_request
*req
);
189 int cuda_request(struct adb_request
*req
,
190 void (*done
)(struct adb_request
*), int nbytes
, ...);
193 struct adb_driver via_cuda_driver
= {
196 .send_request
= cuda_send_request
,
197 .autopoll
= cuda_adb_autopoll
,
199 .reset_bus
= cuda_reset_adb_bus
,
201 #endif /* CONFIG_ADB */
204 int __init
find_via_cuda(void)
206 struct adb_request req
;
209 if (macintosh_config
->adb_type
!= MAC_ADB_CUDA
&&
210 macintosh_config
->adb_type
!= MAC_ADB_EGRET
)
215 mcu_is_egret
= macintosh_config
->adb_type
== MAC_ADB_EGRET
;
217 err
= cuda_init_via();
219 printk(KERN_ERR
"cuda_init_via() failed\n");
224 /* enable autopoll */
225 cuda_request(&req
, NULL
, 3, CUDA_PACKET
, CUDA_AUTOPOLL
, 1);
226 while (!req
.complete
)
232 int __init
find_via_cuda(void)
234 struct adb_request req
;
241 vias
= of_find_node_by_name(NULL
, "via-cuda");
245 reg
= of_get_property(vias
, "reg", NULL
);
247 printk(KERN_ERR
"via-cuda: No \"reg\" property !\n");
250 taddr
= of_translate_address(vias
, reg
);
252 printk(KERN_ERR
"via-cuda: Can't translate address !\n");
255 via
= ioremap(taddr
, 0x2000);
257 printk(KERN_ERR
"via-cuda: Can't map address !\n");
262 sys_ctrler
= SYS_CTRLER_CUDA
;
264 err
= cuda_init_via();
266 printk(KERN_ERR
"cuda_init_via() failed\n");
271 /* Clear and enable interrupts, but only on PPC. On 68K it's done */
272 /* for us by the main VIA driver in arch/m68k/mac/via.c */
274 out_8(&via
[IFR
], 0x7f); /* clear interrupts by writing 1s */
275 out_8(&via
[IER
], IER_SET
|SR_INT
); /* enable interrupt from SR */
277 /* enable autopoll */
278 cuda_request(&req
, NULL
, 3, CUDA_PACKET
, CUDA_AUTOPOLL
, 1);
279 while (!req
.complete
)
289 #endif /* !defined CONFIG_MAC */
291 static int __init
via_cuda_start(void)
297 cuda_irq
= IRQ_MAC_ADB
;
299 cuda_irq
= irq_of_parse_and_map(vias
, 0);
301 printk(KERN_ERR
"via-cuda: can't map interrupts for %pOF\n",
307 if (request_irq(cuda_irq
, cuda_interrupt
, 0, "ADB", cuda_interrupt
)) {
308 printk(KERN_ERR
"via-cuda: can't request irq %d\n", cuda_irq
);
312 pr_info("Macintosh Cuda and Egret driver.\n");
314 cuda_fully_inited
= 1;
318 device_initcall(via_cuda_start
);
325 if (sys_ctrler
!= SYS_CTRLER_CUDA
)
328 if (macintosh_config
->adb_type
!= MAC_ADB_CUDA
&&
329 macintosh_config
->adb_type
!= MAC_ADB_EGRET
)
336 #endif /* CONFIG_ADB */
338 static int __init
sync_egret(void)
340 if (TREQ_asserted(in_8(&via
[B
]))) {
341 /* Complete the inbound transfer */
342 assert_TIP_and_TACK();
346 (void)in_8(&via
[SR
]);
348 if (!TREQ_asserted(in_8(&via
[B
])))
351 negate_TIP_and_TACK();
352 } else if (in_8(&via
[B
]) & TIP
) {
353 /* Terminate the outbound transfer */
357 negate_TIP_and_TACK();
359 /* Clear shift register interrupt */
360 if (in_8(&via
[IFR
]) & SR_INT
)
361 (void)in_8(&via
[SR
]);
365 #define WAIT_FOR(cond, what) \
368 for (x = 1000; !(cond); --x) { \
370 pr_err("Timeout waiting for " what "\n"); \
378 __init
cuda_init_via(void)
381 out_8(&via
[IER
], 0x7f); /* disable interrupts from VIA */
382 (void)in_8(&via
[IER
]);
384 out_8(&via
[IER
], SR_INT
); /* disable SR interrupt from VIA */
387 out_8(&via
[DIRB
], (in_8(&via
[DIRB
]) | TACK
| TIP
) & ~TREQ
); /* TACK & TIP out */
388 out_8(&via
[ACR
], (in_8(&via
[ACR
]) & ~SR_CTRL
) | SR_EXT
); /* SR data in */
389 (void)in_8(&via
[SR
]); /* clear any left-over data */
394 negate_TIP_and_TACK();
396 /* delay 4ms and then clear any pending interrupt */
398 (void)in_8(&via
[SR
]);
399 out_8(&via
[IFR
], SR_INT
);
401 /* sync with the CUDA - assert TACK without TIP */
404 /* wait for the CUDA to assert TREQ in response */
405 WAIT_FOR(TREQ_asserted(in_8(&via
[B
])), "CUDA response to sync");
407 /* wait for the interrupt and then clear it */
408 WAIT_FOR(in_8(&via
[IFR
]) & SR_INT
, "CUDA response to sync (2)");
409 (void)in_8(&via
[SR
]);
410 out_8(&via
[IFR
], SR_INT
);
412 /* finish the sync by negating TACK */
415 /* wait for the CUDA to negate TREQ and the corresponding interrupt */
416 WAIT_FOR(!TREQ_asserted(in_8(&via
[B
])), "CUDA response to sync (3)");
417 WAIT_FOR(in_8(&via
[IFR
]) & SR_INT
, "CUDA response to sync (4)");
418 (void)in_8(&via
[SR
]);
419 out_8(&via
[IFR
], SR_INT
);
425 /* Send an ADB command */
427 cuda_send_request(struct adb_request
*req
, int sync
)
431 if ((via
== NULL
) || !cuda_fully_inited
) {
436 req
->reply_expected
= 1;
443 while (!req
->complete
)
450 /* Enable/disable autopolling */
452 cuda_adb_autopoll(int devs
)
454 struct adb_request req
;
456 if ((via
== NULL
) || !cuda_fully_inited
)
459 cuda_request(&req
, NULL
, 3, CUDA_PACKET
, CUDA_AUTOPOLL
, (devs
? 1: 0));
460 while (!req
.complete
)
465 /* Reset adb bus - how do we do this?? */
467 cuda_reset_adb_bus(void)
469 struct adb_request req
;
471 if ((via
== NULL
) || !cuda_fully_inited
)
474 cuda_request(&req
, NULL
, 2, ADB_PACKET
, 0); /* maybe? */
475 while (!req
.complete
)
479 #endif /* CONFIG_ADB */
481 /* Construct and send a cuda request */
483 cuda_request(struct adb_request
*req
, void (*done
)(struct adb_request
*),
494 req
->nbytes
= nbytes
;
496 va_start(list
, nbytes
);
497 for (i
= 0; i
< nbytes
; ++i
)
498 req
->data
[i
] = va_arg(list
, int);
500 req
->reply_expected
= 1;
501 return cuda_write(req
);
503 EXPORT_SYMBOL(cuda_request
);
506 cuda_write(struct adb_request
*req
)
510 if (req
->nbytes
< 2 || req
->data
[0] > CUDA_PACKET
) {
519 spin_lock_irqsave(&cuda_lock
, flags
);
520 if (current_req
!= 0) {
521 last_req
->next
= req
;
526 if (cuda_state
== idle
)
529 spin_unlock_irqrestore(&cuda_lock
, flags
);
537 /* assert cuda_state == idle */
538 if (current_req
== NULL
)
541 if (TREQ_asserted(in_8(&via
[B
])))
542 return; /* a byte is coming in from the CUDA */
544 /* set the shift register to shift out and send a byte */
545 out_8(&via
[ACR
], in_8(&via
[ACR
]) | SR_OUT
);
546 out_8(&via
[SR
], current_req
->data
[data_index
++]);
548 assert_TIP_and_TACK();
551 cuda_state
= sent_first_byte
;
557 cuda_interrupt(0, NULL
);
559 EXPORT_SYMBOL(cuda_poll
);
561 #define ARRAY_FULL(a, p) ((p) - (a) == ARRAY_SIZE(a))
564 cuda_interrupt(int irq
, void *arg
)
568 struct adb_request
*req
= NULL
;
569 unsigned char ibuf
[16];
574 spin_lock_irqsave(&cuda_lock
, flags
);
576 /* On powermacs, this handler is registered for the VIA IRQ. But they use
577 * just the shift register IRQ -- other VIA interrupt sources are disabled.
578 * On m68k macs, the VIA IRQ sources are dispatched individually. Unless
579 * we are polling, the shift register IRQ flag has already been cleared.
586 if ((in_8(&via
[IFR
]) & SR_INT
) == 0) {
587 spin_unlock_irqrestore(&cuda_lock
, flags
);
590 out_8(&via
[IFR
], SR_INT
);
594 status
= in_8(&via
[B
]) & (TIP
| TACK
| TREQ
);
596 switch (cuda_state
) {
598 /* System controller has unsolicited data for us */
599 (void)in_8(&via
[SR
]);
602 cuda_state
= reading
;
603 reply_ptr
= cuda_rbuf
;
608 /* System controller has reply data for us */
609 (void)in_8(&via
[SR
]);
611 cuda_state
= reading
;
612 reply_ptr
= current_req
->reply
;
616 case sent_first_byte
:
617 if (TREQ_asserted(status
)) {
619 out_8(&via
[ACR
], in_8(&via
[ACR
]) & ~SR_OUT
);
620 (void)in_8(&via
[SR
]);
621 negate_TIP_and_TACK();
623 /* Egret does not raise an "aborted" interrupt */
627 out_8(&via
[SR
], current_req
->data
[data_index
++]);
631 cuda_state
= sending
;
637 if (data_index
>= req
->nbytes
) {
638 out_8(&via
[ACR
], in_8(&via
[ACR
]) & ~SR_OUT
);
639 (void)in_8(&via
[SR
]);
640 negate_TIP_and_TACK();
642 if (req
->reply_expected
) {
643 cuda_state
= awaiting_reply
;
645 current_req
= req
->next
;
647 /* not sure about this */
652 out_8(&via
[SR
], req
->data
[data_index
++]);
660 full
= reading_reply
? ARRAY_FULL(current_req
->reply
, reply_ptr
)
661 : ARRAY_FULL(cuda_rbuf
, reply_ptr
);
663 (void)in_8(&via
[SR
]);
665 *reply_ptr
++ = in_8(&via
[SR
]);
666 if (!TREQ_asserted(status
) || full
) {
669 /* that's all folks */
670 negate_TIP_and_TACK();
671 cuda_state
= read_done
;
672 /* Egret does not raise a "read done" interrupt */
674 goto read_done_state
;
683 (void)in_8(&via
[SR
]);
687 req
->reply_len
= reply_ptr
- req
->reply
;
688 if (req
->data
[0] == ADB_PACKET
) {
689 /* Have to adjust the reply from ADB commands */
690 if (req
->reply_len
<= 2 || (req
->reply
[1] & 2) != 0) {
691 /* the 0x2 bit indicates no response */
694 /* leave just the command and result bytes in the reply */
696 memmove(req
->reply
, req
->reply
+ 2, req
->reply_len
);
699 current_req
= req
->next
;
703 /* This is tricky. We must break the spinlock to call
704 * cuda_input. However, doing so means we might get
705 * re-entered from another CPU getting an interrupt
706 * or calling cuda_poll(). I ended up using the stack
707 * (it's only for 16 bytes) and moving the actual
708 * call to cuda_input to outside of the lock.
710 ibuf_len
= reply_ptr
- cuda_rbuf
;
711 memcpy(ibuf
, cuda_rbuf
, ibuf_len
);
713 reply_ptr
= cuda_rbuf
;
716 if (cuda_state
== idle
&& TREQ_asserted(in_8(&via
[B
]))) {
718 cuda_state
= reading
;
723 pr_err("cuda_interrupt: unknown cuda_state %d?\n", cuda_state
);
725 spin_unlock_irqrestore(&cuda_lock
, flags
);
726 if (complete
&& req
) {
727 void (*done
)(struct adb_request
*) = req
->done
;
730 /* Here, we assume that if the request has a done member, the
731 * struct request will survive to setting req->complete to 1
737 cuda_input(ibuf
, ibuf_len
);
742 cuda_input(unsigned char *buf
, int nb
)
747 if (nb
== 5 && buf
[2] == 0x2c) {
748 extern int xmon_wants_key
, xmon_adb_keycode
;
749 if (xmon_wants_key
) {
750 xmon_adb_keycode
= buf
[3];
754 #endif /* CONFIG_XMON */
756 adb_input(buf
+2, nb
-2, buf
[1] & 0x40);
757 #endif /* CONFIG_ADB */
761 /* Egret sends these periodically. Might be useful as a 'heartbeat'
762 * to trigger a recovery for the VIA shift register errata.
767 print_hex_dump(KERN_INFO
, "cuda_input: ", DUMP_PREFIX_NONE
, 32, 1,
772 /* Offset between Unix time (1970-based) and Mac time (1904-based) */
773 #define RTC_OFFSET 2082844800
775 time64_t
cuda_get_time(void)
777 struct adb_request req
;
780 if (cuda_request(&req
, NULL
, 2, CUDA_PACKET
, CUDA_GET_TIME
) < 0)
782 while (!req
.complete
)
784 if (req
.reply_len
!= 7)
785 pr_err("%s: got %d byte reply\n", __func__
, req
.reply_len
);
786 now
= (req
.reply
[3] << 24) + (req
.reply
[4] << 16) +
787 (req
.reply
[5] << 8) + req
.reply
[6];
788 return (time64_t
)now
- RTC_OFFSET
;
791 int cuda_set_rtc_time(struct rtc_time
*tm
)
794 struct adb_request req
;
796 now
= lower_32_bits(rtc_tm_to_time64(tm
) + RTC_OFFSET
);
797 if (cuda_request(&req
, NULL
, 6, CUDA_PACKET
, CUDA_SET_TIME
,
798 now
>> 24, now
>> 16, now
>> 8, now
) < 0)
800 while (!req
.complete
)
802 if ((req
.reply_len
!= 3) && (req
.reply_len
!= 7))
803 pr_err("%s: got %d byte reply\n", __func__
, req
.reply_len
);