blob: 6aa903529570ddc3af184981cf7d4900f789770c [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Device driver for the via ADB on (many) Mac II-class machines
4 *
5 * Based on the original ADB keyboard handler Copyright (c) 1997 Alan Cox
6 * Also derived from code Copyright (C) 1996 Paul Mackerras.
7 *
8 * With various updates provided over the years by Michael Schmitz,
9 * Guideo Koerber and others.
10 *
11 * Rewrite for Unified ADB by Joshua M. Thompson (funaho@jurai.org)
12 *
13 * 1999-08-02 (jmt) - Initial rewrite for Unified ADB.
14 * 2000-03-29 Tony Mantler <tonym@mac.linux-m68k.org>
David Brazdil0f672f62019-12-10 10:32:29 +000015 * - Big overhaul, should actually work now.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000016 * 2006-12-31 Finn Thain - Another overhaul.
17 *
18 * Suggested reading:
19 * Inside Macintosh, ch. 5 ADB Manager
20 * Guide to the Macinstosh Family Hardware, ch. 8 Apple Desktop Bus
21 * Rockwell R6522 VIA datasheet
22 *
23 * Apple's "ADB Analyzer" bus sniffer is invaluable:
24 * ftp://ftp.apple.com/developer/Tool_Chest/Devices_-_Hardware/Apple_Desktop_Bus/
25 */
David Brazdil0f672f62019-12-10 10:32:29 +000026
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000027#include <stdarg.h>
28#include <linux/types.h>
29#include <linux/errno.h>
30#include <linux/kernel.h>
31#include <linux/delay.h>
32#include <linux/adb.h>
33#include <linux/interrupt.h>
34#include <linux/init.h>
35#include <asm/macintosh.h>
36#include <asm/macints.h>
37#include <asm/mac_via.h>
38
39static volatile unsigned char *via;
40
41/* VIA registers - spaced 0x200 bytes apart */
42#define RS 0x200 /* skip between registers */
43#define B 0 /* B-side data */
44#define A RS /* A-side data */
45#define DIRB (2*RS) /* B-side direction (1=output) */
46#define DIRA (3*RS) /* A-side direction (1=output) */
47#define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */
48#define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */
49#define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */
50#define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */
51#define T2CL (8*RS) /* Timer 2 ctr/latch (low 8 bits) */
52#define T2CH (9*RS) /* Timer 2 counter (high 8 bits) */
53#define SR (10*RS) /* Shift register */
54#define ACR (11*RS) /* Auxiliary control register */
55#define PCR (12*RS) /* Peripheral control register */
56#define IFR (13*RS) /* Interrupt flag register */
57#define IER (14*RS) /* Interrupt enable register */
58#define ANH (15*RS) /* A-side data, no handshake */
59
60/* Bits in B data register: all active low */
61#define CTLR_IRQ 0x08 /* Controller rcv status (input) */
62#define ST_MASK 0x30 /* mask for selecting ADB state bits */
63
64/* Bits in ACR */
65#define SR_CTRL 0x1c /* Shift register control bits */
66#define SR_EXT 0x0c /* Shift on external clock */
67#define SR_OUT 0x10 /* Shift out if 1 */
68
69/* Bits in IFR and IER */
70#define IER_SET 0x80 /* set bits in IER */
71#define IER_CLR 0 /* clear bits in IER */
72#define SR_INT 0x04 /* Shift register full/empty */
73
74/* ADB transaction states according to GMHW */
75#define ST_CMD 0x00 /* ADB state: command byte */
76#define ST_EVEN 0x10 /* ADB state: even data byte */
77#define ST_ODD 0x20 /* ADB state: odd data byte */
78#define ST_IDLE 0x30 /* ADB state: idle, nothing to send */
79
David Brazdil0f672f62019-12-10 10:32:29 +000080static int macii_init_via(void);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000081static void macii_start(void);
82static irqreturn_t macii_interrupt(int irq, void *arg);
83static void macii_queue_poll(void);
84
85static int macii_probe(void);
86static int macii_init(void);
87static int macii_send_request(struct adb_request *req, int sync);
88static int macii_write(struct adb_request *req);
89static int macii_autopoll(int devs);
90static void macii_poll(void);
91static int macii_reset_bus(void);
92
93struct adb_driver via_macii_driver = {
94 .name = "Mac II",
95 .probe = macii_probe,
96 .init = macii_init,
97 .send_request = macii_send_request,
98 .autopoll = macii_autopoll,
99 .poll = macii_poll,
100 .reset_bus = macii_reset_bus,
101};
102
103static enum macii_state {
104 idle,
105 sending,
106 reading,
107 read_done,
108} macii_state;
109
110static struct adb_request *current_req; /* first request struct in the queue */
111static struct adb_request *last_req; /* last request struct in the queue */
112static unsigned char reply_buf[16]; /* storage for autopolled replies */
113static unsigned char *reply_ptr; /* next byte in reply_buf or req->reply */
114static int reading_reply; /* store reply in reply_buf else req->reply */
115static int data_index; /* index of the next byte to send from req->data */
116static int reply_len; /* number of bytes received in reply_buf or req->reply */
117static int status; /* VIA's ADB status bits captured upon interrupt */
118static int last_status; /* status bits as at previous interrupt */
119static int srq_asserted; /* have to poll for the device that asserted it */
120static int command_byte; /* the most recent command byte transmitted */
121static int autopoll_devs; /* bits set are device addresses to be polled */
122
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000123/* Check for MacII style ADB */
124static int macii_probe(void)
125{
David Brazdil0f672f62019-12-10 10:32:29 +0000126 if (macintosh_config->adb_type != MAC_ADB_II)
127 return -ENODEV;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000128
129 via = via1;
130
David Brazdil0f672f62019-12-10 10:32:29 +0000131 pr_info("adb: Mac II ADB Driver v1.0 for Unified ADB\n");
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000132 return 0;
133}
134
135/* Initialize the driver */
136int macii_init(void)
137{
138 unsigned long flags;
139 int err;
David Brazdil0f672f62019-12-10 10:32:29 +0000140
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000141 local_irq_save(flags);
David Brazdil0f672f62019-12-10 10:32:29 +0000142
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000143 err = macii_init_via();
David Brazdil0f672f62019-12-10 10:32:29 +0000144 if (err)
145 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000146
147 err = request_irq(IRQ_MAC_ADB, macii_interrupt, 0, "ADB",
148 macii_interrupt);
David Brazdil0f672f62019-12-10 10:32:29 +0000149 if (err)
150 goto out;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000151
152 macii_state = idle;
153out:
154 local_irq_restore(flags);
155 return err;
156}
157
David Brazdil0f672f62019-12-10 10:32:29 +0000158/* initialize the hardware */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000159static int macii_init_via(void)
160{
161 unsigned char x;
162
163 /* We want CTLR_IRQ as input and ST_EVEN | ST_ODD as output lines. */
164 via[DIRB] = (via[DIRB] | ST_EVEN | ST_ODD) & ~CTLR_IRQ;
165
166 /* Set up state: idle */
167 via[B] |= ST_IDLE;
David Brazdil0f672f62019-12-10 10:32:29 +0000168 last_status = via[B] & (ST_MASK | CTLR_IRQ);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000169
170 /* Shift register on input */
171 via[ACR] = (via[ACR] & ~SR_CTRL) | SR_EXT;
172
173 /* Wipe any pending data and int */
174 x = via[SR];
175
176 return 0;
177}
178
179/* Send an ADB poll (Talk Register 0 command prepended to the request queue) */
180static void macii_queue_poll(void)
181{
182 /* No point polling the active device as it will never assert SRQ, so
183 * poll the next device in the autopoll list. This could leave us
184 * stuck in a polling loop if an unprobed device is asserting SRQ.
185 * In theory, that could only happen if a device was plugged in after
186 * probing started. Unplugging it again will break the cycle.
187 * (Simply polling the next higher device often ends up polling almost
188 * every device (after wrapping around), which takes too long.)
189 */
190 int device_mask;
191 int next_device;
192 static struct adb_request req;
193
David Brazdil0f672f62019-12-10 10:32:29 +0000194 if (!autopoll_devs)
195 return;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000196
197 device_mask = (1 << (((command_byte & 0xF0) >> 4) + 1)) - 1;
198 if (autopoll_devs & ~device_mask)
199 next_device = ffs(autopoll_devs & ~device_mask) - 1;
200 else
201 next_device = ffs(autopoll_devs) - 1;
202
David Brazdil0f672f62019-12-10 10:32:29 +0000203 adb_request(&req, NULL, ADBREQ_NOSEND, 1, ADB_READREG(next_device, 0));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000204
205 req.sent = 0;
206 req.complete = 0;
207 req.reply_len = 0;
208 req.next = current_req;
209
210 if (current_req != NULL) {
211 current_req = &req;
212 } else {
213 current_req = &req;
214 last_req = &req;
215 }
216}
217
218/* Send an ADB request; if sync, poll out the reply 'till it's done */
219static int macii_send_request(struct adb_request *req, int sync)
220{
221 int err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000222
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000223 err = macii_write(req);
David Brazdil0f672f62019-12-10 10:32:29 +0000224 if (err)
225 return err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000226
David Brazdil0f672f62019-12-10 10:32:29 +0000227 if (sync)
228 while (!req->complete)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000229 macii_poll();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000230
David Brazdil0f672f62019-12-10 10:32:29 +0000231 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000232}
233
234/* Send an ADB request (append to request queue) */
235static int macii_write(struct adb_request *req)
236{
David Brazdil0f672f62019-12-10 10:32:29 +0000237 unsigned long flags;
238
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000239 if (req->nbytes < 2 || req->data[0] != ADB_PACKET || req->nbytes > 15) {
240 req->complete = 1;
241 return -EINVAL;
242 }
David Brazdil0f672f62019-12-10 10:32:29 +0000243
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000244 req->next = NULL;
245 req->sent = 0;
246 req->complete = 0;
247 req->reply_len = 0;
248
David Brazdil0f672f62019-12-10 10:32:29 +0000249 local_irq_save(flags);
250
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000251 if (current_req != NULL) {
252 last_req->next = req;
253 last_req = req;
254 } else {
255 current_req = req;
256 last_req = req;
David Brazdil0f672f62019-12-10 10:32:29 +0000257 if (macii_state == idle)
258 macii_start();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000259 }
David Brazdil0f672f62019-12-10 10:32:29 +0000260
261 local_irq_restore(flags);
262
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000263 return 0;
264}
265
266/* Start auto-polling */
267static int macii_autopoll(int devs)
268{
269 static struct adb_request req;
270 unsigned long flags;
271 int err = 0;
272
Olivier Deprez0e641232021-09-23 10:07:05 +0200273 local_irq_save(flags);
274
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000275 /* bit 1 == device 1, and so on. */
276 autopoll_devs = devs & 0xFFFE;
277
Olivier Deprez0e641232021-09-23 10:07:05 +0200278 if (autopoll_devs && !current_req) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000279 /* Send a Talk Reg 0. The controller will repeatedly transmit
280 * this as long as it is idle.
281 */
282 adb_request(&req, NULL, ADBREQ_NOSEND, 1,
283 ADB_READREG(ffs(autopoll_devs) - 1, 0));
284 err = macii_write(&req);
285 }
286
287 local_irq_restore(flags);
288 return err;
289}
290
David Brazdil0f672f62019-12-10 10:32:29 +0000291static inline int need_autopoll(void)
292{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000293 /* Was the last command Talk Reg 0
294 * and is the target on the autopoll list?
295 */
296 if ((command_byte & 0x0F) == 0x0C &&
297 ((1 << ((command_byte & 0xF0) >> 4)) & autopoll_devs))
298 return 0;
299 return 1;
300}
301
302/* Prod the chip without interrupts */
303static void macii_poll(void)
304{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000305 macii_interrupt(0, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000306}
307
308/* Reset the bus */
309static int macii_reset_bus(void)
310{
311 static struct adb_request req;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000312
313 /* Command = 0, Address = ignored */
David Brazdil0f672f62019-12-10 10:32:29 +0000314 adb_request(&req, NULL, ADBREQ_NOSEND, 1, ADB_BUSRESET);
315 macii_send_request(&req, 1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000316
317 /* Don't want any more requests during the Global Reset low time. */
318 udelay(3000);
319
320 return 0;
321}
322
323/* Start sending ADB packet */
324static void macii_start(void)
325{
326 struct adb_request *req;
327
328 req = current_req;
329
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000330 /* Now send it. Be careful though, that first byte of the request
331 * is actually ADB_PACKET; the real data begins at index 1!
332 * And req->nbytes is the number of bytes of real data plus one.
333 */
334
335 /* store command byte */
336 command_byte = req->data[1];
337 /* Output mode */
338 via[ACR] |= SR_OUT;
339 /* Load data */
340 via[SR] = req->data[1];
341 /* set ADB state to 'command' */
342 via[B] = (via[B] & ~ST_MASK) | ST_CMD;
343
344 macii_state = sending;
345 data_index = 2;
346}
347
348/*
349 * The notorious ADB interrupt handler - does all of the protocol handling.
350 * Relies on the ADB controller sending and receiving data, thereby
351 * generating shift register interrupts (SR_INT) for us. This means there has
352 * to be activity on the ADB bus. The chip will poll to achieve this.
353 *
354 * The basic ADB state machine was left unchanged from the original MacII code
David Brazdil0f672f62019-12-10 10:32:29 +0000355 * by Alan Cox, which was based on the CUDA driver for PowerMac.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000356 * The syntax of the ADB status lines is totally different on MacII,
357 * though. MacII uses the states Command -> Even -> Odd -> Even ->...-> Idle
358 * for sending and Idle -> Even -> Odd -> Even ->...-> Idle for receiving.
359 * Start and end of a receive packet are signalled by asserting /IRQ on the
360 * interrupt line (/IRQ means the CTLR_IRQ bit in port B; not to be confused
361 * with the VIA shift register interrupt. /IRQ never actually interrupts the
362 * processor, it's just an ordinary input.)
363 */
364static irqreturn_t macii_interrupt(int irq, void *arg)
365{
366 int x;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000367 struct adb_request *req;
David Brazdil0f672f62019-12-10 10:32:29 +0000368 unsigned long flags;
369
370 local_irq_save(flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000371
372 if (!arg) {
373 /* Clear the SR IRQ flag when polling. */
374 if (via[IFR] & SR_INT)
375 via[IFR] = SR_INT;
David Brazdil0f672f62019-12-10 10:32:29 +0000376 else {
377 local_irq_restore(flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000378 return IRQ_NONE;
David Brazdil0f672f62019-12-10 10:32:29 +0000379 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000380 }
381
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000382 last_status = status;
David Brazdil0f672f62019-12-10 10:32:29 +0000383 status = via[B] & (ST_MASK | CTLR_IRQ);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000384
385 switch (macii_state) {
David Brazdil0f672f62019-12-10 10:32:29 +0000386 case idle:
387 if (reading_reply) {
388 reply_ptr = current_req->reply;
389 } else {
390 WARN_ON(current_req);
391 reply_ptr = reply_buf;
392 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000393
David Brazdil0f672f62019-12-10 10:32:29 +0000394 x = via[SR];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000395
David Brazdil0f672f62019-12-10 10:32:29 +0000396 if ((status & CTLR_IRQ) && (x == 0xFF)) {
397 /* Bus timeout without SRQ sequence:
398 * data is "FF" while CTLR_IRQ is "H"
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000399 */
David Brazdil0f672f62019-12-10 10:32:29 +0000400 reply_len = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000401 srq_asserted = 0;
David Brazdil0f672f62019-12-10 10:32:29 +0000402 macii_state = read_done;
403 } else {
404 macii_state = reading;
405 *reply_ptr = x;
406 reply_len = 1;
407 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000408
David Brazdil0f672f62019-12-10 10:32:29 +0000409 /* set ADB state = even for first data byte */
410 via[B] = (via[B] & ~ST_MASK) | ST_EVEN;
411 break;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000412
David Brazdil0f672f62019-12-10 10:32:29 +0000413 case sending:
414 req = current_req;
415 if (data_index >= req->nbytes) {
416 req->sent = 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000417 macii_state = idle;
418
David Brazdil0f672f62019-12-10 10:32:29 +0000419 if (req->reply_expected) {
420 reading_reply = 1;
421 } else {
422 req->complete = 1;
423 current_req = req->next;
424 if (req->done)
425 (*req->done)(req);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000426
David Brazdil0f672f62019-12-10 10:32:29 +0000427 if (current_req)
428 macii_start();
429 else if (need_autopoll())
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000430 macii_autopoll(autopoll_devs);
David Brazdil0f672f62019-12-10 10:32:29 +0000431 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000432
David Brazdil0f672f62019-12-10 10:32:29 +0000433 if (macii_state == idle) {
434 /* reset to shift in */
435 via[ACR] &= ~SR_OUT;
436 x = via[SR];
437 /* set ADB state idle - might get SRQ */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000438 via[B] = (via[B] & ~ST_MASK) | ST_IDLE;
David Brazdil0f672f62019-12-10 10:32:29 +0000439 }
440 } else {
441 via[SR] = req->data[data_index++];
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000442
David Brazdil0f672f62019-12-10 10:32:29 +0000443 if ((via[B] & ST_MASK) == ST_CMD) {
444 /* just sent the command byte, set to EVEN */
445 via[B] = (via[B] & ~ST_MASK) | ST_EVEN;
446 } else {
447 /* invert state bits, toggle ODD/EVEN */
448 via[B] ^= ST_MASK;
449 }
450 }
451 break;
452
453 case reading:
454 x = via[SR];
455 WARN_ON((status & ST_MASK) == ST_CMD ||
456 (status & ST_MASK) == ST_IDLE);
457
458 /* Bus timeout with SRQ sequence:
459 * data is "XX FF" while CTLR_IRQ is "L L"
460 * End of packet without SRQ sequence:
461 * data is "XX...YY 00" while CTLR_IRQ is "L...H L"
462 * End of packet SRQ sequence:
463 * data is "XX...YY 00" while CTLR_IRQ is "L...L L"
464 * (where XX is the first response byte and
465 * YY is the last byte of valid response data.)
466 */
467
468 srq_asserted = 0;
469 if (!(status & CTLR_IRQ)) {
470 if (x == 0xFF) {
471 if (!(last_status & CTLR_IRQ)) {
472 macii_state = read_done;
473 reply_len = 0;
474 srq_asserted = 1;
475 }
476 } else if (x == 0x00) {
477 macii_state = read_done;
478 if (!(last_status & CTLR_IRQ))
479 srq_asserted = 1;
480 }
481 }
482
483 if (macii_state == reading &&
484 reply_len < ARRAY_SIZE(reply_buf)) {
485 reply_ptr++;
486 *reply_ptr = x;
487 reply_len++;
488 }
489
490 /* invert state bits, toggle ODD/EVEN */
491 via[B] ^= ST_MASK;
492 break;
493
494 case read_done:
495 x = via[SR];
496
497 if (reading_reply) {
498 reading_reply = 0;
499 req = current_req;
500 req->reply_len = reply_len;
501 req->complete = 1;
502 current_req = req->next;
503 if (req->done)
504 (*req->done)(req);
505 } else if (reply_len && autopoll_devs)
506 adb_input(reply_buf, reply_len, 0);
507
508 macii_state = idle;
509
510 /* SRQ seen before, initiate poll now */
511 if (srq_asserted)
512 macii_queue_poll();
513
514 if (current_req)
515 macii_start();
516 else if (need_autopoll())
517 macii_autopoll(autopoll_devs);
518
519 if (macii_state == idle)
520 via[B] = (via[B] & ~ST_MASK) | ST_IDLE;
521 break;
522
523 default:
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000524 break;
525 }
526
David Brazdil0f672f62019-12-10 10:32:29 +0000527 local_irq_restore(flags);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000528 return IRQ_HANDLED;
529}