Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Finn Thain | 47fd206 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 15 | * - Big overhaul, should actually work now. |
Finn Thain | d0c2c26 | 2017-03-20 11:53:09 +1100 | [diff] [blame] | 16 | * 2006-12-31 Finn Thain - Another overhaul. |
Finn Thain | d95fd5f | 2007-05-01 22:32:59 +0200 | [diff] [blame] | 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/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | */ |
Finn Thain | 47fd206 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <stdarg.h> |
| 28 | #include <linux/types.h> |
| 29 | #include <linux/errno.h> |
| 30 | #include <linux/kernel.h> |
| 31 | #include <linux/delay.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <asm/mac_via.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
| 39 | static 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 */ |
Finn Thain | d95fd5f | 2007-05-01 22:32:59 +0200 | [diff] [blame] | 61 | #define CTLR_IRQ 0x08 /* Controller rcv status (input) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Finn Thain | f93bfeb | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 80 | /* ADB command byte structure */ |
| 81 | #define ADDR_MASK 0xF0 |
| 82 | #define CMD_MASK 0x0F |
Finn Thain | b4d76c2 | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 83 | #define OP_MASK 0x0C |
| 84 | #define TALK 0x0C |
Finn Thain | f93bfeb | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 85 | |
Finn Thain | 47fd206 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 86 | static int macii_init_via(void); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | static void macii_start(void); |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 88 | static irqreturn_t macii_interrupt(int irq, void *arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | static void macii_queue_poll(void); |
| 90 | |
| 91 | static int macii_probe(void); |
| 92 | static int macii_init(void); |
| 93 | static int macii_send_request(struct adb_request *req, int sync); |
| 94 | static int macii_write(struct adb_request *req); |
| 95 | static int macii_autopoll(int devs); |
| 96 | static void macii_poll(void); |
| 97 | static int macii_reset_bus(void); |
| 98 | |
| 99 | struct adb_driver via_macii_driver = { |
Finn Thain | 3a52f6f | 2018-03-29 11:36:04 +1100 | [diff] [blame] | 100 | .name = "Mac II", |
| 101 | .probe = macii_probe, |
| 102 | .init = macii_init, |
| 103 | .send_request = macii_send_request, |
| 104 | .autopoll = macii_autopoll, |
| 105 | .poll = macii_poll, |
| 106 | .reset_bus = macii_reset_bus, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | static enum macii_state { |
| 110 | idle, |
| 111 | sending, |
| 112 | reading, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | } macii_state; |
| 114 | |
Finn Thain | d95fd5f | 2007-05-01 22:32:59 +0200 | [diff] [blame] | 115 | static struct adb_request *current_req; /* first request struct in the queue */ |
| 116 | static struct adb_request *last_req; /* last request struct in the queue */ |
| 117 | static unsigned char reply_buf[16]; /* storage for autopolled replies */ |
Finn Thain | eb4da4c | 2008-02-04 22:30:27 -0800 | [diff] [blame] | 118 | static unsigned char *reply_ptr; /* next byte in reply_buf or req->reply */ |
Finn Thain | f87a162 | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 119 | static bool reading_reply; /* store reply in reply_buf else req->reply */ |
Finn Thain | d95fd5f | 2007-05-01 22:32:59 +0200 | [diff] [blame] | 120 | static int data_index; /* index of the next byte to send from req->data */ |
| 121 | static int reply_len; /* number of bytes received in reply_buf or req->reply */ |
| 122 | static int status; /* VIA's ADB status bits captured upon interrupt */ |
Finn Thain | b4d76c2 | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 123 | static bool bus_timeout; /* no data was sent by the device */ |
| 124 | static bool srq_asserted; /* have to poll for the device that asserted it */ |
Finn Thain | f93bfeb | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 125 | static u8 last_cmd; /* the most recent command byte transmitted */ |
Finn Thain | b4d76c2 | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 126 | static u8 last_talk_cmd; /* the most recent Talk command byte transmitted */ |
Finn Thain | f93bfeb | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 127 | static u8 last_poll_cmd; /* the most recent Talk R0 command byte transmitted */ |
Finn Thain | 5c0c15a | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 128 | static unsigned int autopoll_devs; /* bits set are device addresses to poll */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | /* Check for MacII style ADB */ |
| 131 | static int macii_probe(void) |
| 132 | { |
Finn Thain | 47fd206 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 133 | if (macintosh_config->adb_type != MAC_ADB_II) |
| 134 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | |
| 136 | via = via1; |
| 137 | |
Finn Thain | 351e5ad | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 138 | pr_info("adb: Mac II ADB Driver v1.0 for Unified ADB\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | /* Initialize the driver */ |
Finn Thain | 3327e58 | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 143 | static int macii_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | { |
| 145 | unsigned long flags; |
| 146 | int err; |
Finn Thain | 47fd206 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 147 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | local_irq_save(flags); |
Finn Thain | 47fd206 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 149 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | err = macii_init_via(); |
Finn Thain | 47fd206 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 151 | if (err) |
| 152 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
Geert Uytterhoeven | 5a23945 | 2011-07-13 22:33:13 +0200 | [diff] [blame] | 154 | err = request_irq(IRQ_MAC_ADB, macii_interrupt, 0, "ADB", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | macii_interrupt); |
Finn Thain | 47fd206 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 156 | if (err) |
| 157 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
| 159 | macii_state = idle; |
Finn Thain | d95fd5f | 2007-05-01 22:32:59 +0200 | [diff] [blame] | 160 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | local_irq_restore(flags); |
Finn Thain | d95fd5f | 2007-05-01 22:32:59 +0200 | [diff] [blame] | 162 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Finn Thain | 47fd206 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 165 | /* initialize the hardware */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | static int macii_init_via(void) |
| 167 | { |
| 168 | unsigned char x; |
| 169 | |
Finn Thain | d95fd5f | 2007-05-01 22:32:59 +0200 | [diff] [blame] | 170 | /* We want CTLR_IRQ as input and ST_EVEN | ST_ODD as output lines. */ |
| 171 | via[DIRB] = (via[DIRB] | ST_EVEN | ST_ODD) & ~CTLR_IRQ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
| 173 | /* Set up state: idle */ |
| 174 | via[B] |= ST_IDLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
| 176 | /* Shift register on input */ |
| 177 | via[ACR] = (via[ACR] & ~SR_CTRL) | SR_EXT; |
| 178 | |
| 179 | /* Wipe any pending data and int */ |
| 180 | x = via[SR]; |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
Finn Thain | d95fd5f | 2007-05-01 22:32:59 +0200 | [diff] [blame] | 185 | /* Send an ADB poll (Talk Register 0 command prepended to the request queue) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | static void macii_queue_poll(void) |
| 187 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | static struct adb_request req; |
Finn Thain | f93bfeb | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 189 | unsigned char poll_command; |
| 190 | unsigned int poll_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | |
Finn Thain | f93bfeb | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 192 | /* This only polls devices in the autopoll list, which assumes that |
| 193 | * unprobed devices never assert SRQ. That could happen if a device was |
| 194 | * plugged in after the adb bus scan. Unplugging it again will resolve |
| 195 | * the problem. This behaviour is similar to MacOS. |
| 196 | */ |
Finn Thain | 47fd206 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 197 | if (!autopoll_devs) |
| 198 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
Finn Thain | f93bfeb | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 200 | /* The device most recently polled may not be the best device to poll |
| 201 | * right now. Some other device(s) may have signalled SRQ (the active |
| 202 | * device won't do that). Or the autopoll list may have been changed. |
| 203 | * Try polling the next higher address. |
| 204 | */ |
| 205 | poll_addr = (last_poll_cmd & ADDR_MASK) >> 4; |
| 206 | if ((srq_asserted && last_cmd == last_poll_cmd) || |
| 207 | !(autopoll_devs & (1 << poll_addr))) { |
| 208 | unsigned int higher_devs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
Finn Thain | f93bfeb | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 210 | higher_devs = autopoll_devs & -(1 << (poll_addr + 1)); |
| 211 | poll_addr = ffs(higher_devs ? higher_devs : autopoll_devs) - 1; |
| 212 | } |
| 213 | |
| 214 | /* Send a Talk Register 0 command */ |
| 215 | poll_command = ADB_READREG(poll_addr, 0); |
| 216 | |
| 217 | /* No need to repeat this Talk command. The transceiver will do that |
| 218 | * as long as it is idle. |
| 219 | */ |
| 220 | if (poll_command == last_cmd) |
| 221 | return; |
| 222 | |
| 223 | adb_request(&req, NULL, ADBREQ_NOSEND, 1, poll_command); |
Finn Thain | d95fd5f | 2007-05-01 22:32:59 +0200 | [diff] [blame] | 224 | |
| 225 | req.sent = 0; |
| 226 | req.complete = 0; |
| 227 | req.reply_len = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | req.next = current_req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
Finn Thain | f93bfeb | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 230 | if (WARN_ON(current_req)) { |
Finn Thain | d95fd5f | 2007-05-01 22:32:59 +0200 | [diff] [blame] | 231 | current_req = &req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | } else { |
Finn Thain | d95fd5f | 2007-05-01 22:32:59 +0200 | [diff] [blame] | 233 | current_req = &req; |
| 234 | last_req = &req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | /* Send an ADB request; if sync, poll out the reply 'till it's done */ |
| 239 | static int macii_send_request(struct adb_request *req, int sync) |
| 240 | { |
Finn Thain | d95fd5f | 2007-05-01 22:32:59 +0200 | [diff] [blame] | 241 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
Finn Thain | d95fd5f | 2007-05-01 22:32:59 +0200 | [diff] [blame] | 243 | err = macii_write(req); |
Finn Thain | 5ce6185 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 244 | if (err) |
| 245 | return err; |
Finn Thain | d95fd5f | 2007-05-01 22:32:59 +0200 | [diff] [blame] | 246 | |
Finn Thain | 5ce6185 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 247 | if (sync) |
Finn Thain | 5f93d70 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 248 | while (!req->complete) |
Finn Thain | d95fd5f | 2007-05-01 22:32:59 +0200 | [diff] [blame] | 249 | macii_poll(); |
Finn Thain | d95fd5f | 2007-05-01 22:32:59 +0200 | [diff] [blame] | 250 | |
Finn Thain | 5ce6185 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 251 | return 0; |
Finn Thain | d95fd5f | 2007-05-01 22:32:59 +0200 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | /* Send an ADB request (append to request queue) */ |
| 255 | static int macii_write(struct adb_request *req) |
| 256 | { |
Finn Thain | 5ce6185 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 257 | unsigned long flags; |
| 258 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | if (req->nbytes < 2 || req->data[0] != ADB_PACKET || req->nbytes > 15) { |
| 260 | req->complete = 1; |
| 261 | return -EINVAL; |
| 262 | } |
Finn Thain | 47fd206 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 263 | |
Al Viro | a5d361f | 2006-01-12 01:06:34 -0800 | [diff] [blame] | 264 | req->next = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | req->sent = 0; |
| 266 | req->complete = 0; |
| 267 | req->reply_len = 0; |
| 268 | |
Finn Thain | 5ce6185 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 269 | local_irq_save(flags); |
| 270 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | if (current_req != NULL) { |
| 272 | last_req->next = req; |
| 273 | last_req = req; |
| 274 | } else { |
| 275 | current_req = req; |
| 276 | last_req = req; |
Finn Thain | 47fd206 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 277 | if (macii_state == idle) |
| 278 | macii_start(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | } |
Finn Thain | 5ce6185 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 280 | |
| 281 | local_irq_restore(flags); |
| 282 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | /* Start auto-polling */ |
| 287 | static int macii_autopoll(int devs) |
| 288 | { |
Finn Thain | d95fd5f | 2007-05-01 22:32:59 +0200 | [diff] [blame] | 289 | unsigned long flags; |
Finn Thain | d95fd5f | 2007-05-01 22:32:59 +0200 | [diff] [blame] | 290 | |
Finn Thain | 59ea38f | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 291 | local_irq_save(flags); |
| 292 | |
Finn Thain | d95fd5f | 2007-05-01 22:32:59 +0200 | [diff] [blame] | 293 | /* bit 1 == device 1, and so on. */ |
Finn Thain | 5c0c15a | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 294 | autopoll_devs = (unsigned int)devs & 0xFFFE; |
Finn Thain | d95fd5f | 2007-05-01 22:32:59 +0200 | [diff] [blame] | 295 | |
Finn Thain | f93bfeb | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 296 | if (!current_req) { |
| 297 | macii_queue_poll(); |
| 298 | if (current_req && macii_state == idle) |
| 299 | macii_start(); |
Finn Thain | d95fd5f | 2007-05-01 22:32:59 +0200 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | local_irq_restore(flags); |
Finn Thain | d95fd5f | 2007-05-01 22:32:59 +0200 | [diff] [blame] | 303 | |
Finn Thain | f93bfeb | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 304 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | /* Prod the chip without interrupts */ |
| 308 | static void macii_poll(void) |
| 309 | { |
Finn Thain | d95fd5f | 2007-05-01 22:32:59 +0200 | [diff] [blame] | 310 | macii_interrupt(0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | /* Reset the bus */ |
| 314 | static int macii_reset_bus(void) |
| 315 | { |
Finn Thain | 046ace8 | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 316 | struct adb_request req; |
Finn Thain | 47fd206 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 317 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | /* Command = 0, Address = ignored */ |
Finn Thain | b52dce8 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 319 | adb_request(&req, NULL, ADBREQ_NOSEND, 1, ADB_BUSRESET); |
| 320 | macii_send_request(&req, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | |
Finn Thain | d95fd5f | 2007-05-01 22:32:59 +0200 | [diff] [blame] | 322 | /* Don't want any more requests during the Global Reset low time. */ |
| 323 | udelay(3000); |
| 324 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | /* Start sending ADB packet */ |
| 329 | static void macii_start(void) |
| 330 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | struct adb_request *req; |
| 332 | |
| 333 | req = current_req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | |
Finn Thain | d95fd5f | 2007-05-01 22:32:59 +0200 | [diff] [blame] | 335 | /* Now send it. Be careful though, that first byte of the request |
| 336 | * is actually ADB_PACKET; the real data begins at index 1! |
| 337 | * And req->nbytes is the number of bytes of real data plus one. |
| 338 | */ |
| 339 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | /* Output mode */ |
| 341 | via[ACR] |= SR_OUT; |
| 342 | /* Load data */ |
| 343 | via[SR] = req->data[1]; |
| 344 | /* set ADB state to 'command' */ |
| 345 | via[B] = (via[B] & ~ST_MASK) | ST_CMD; |
| 346 | |
| 347 | macii_state = sending; |
| 348 | data_index = 2; |
Finn Thain | b4d76c2 | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 349 | |
| 350 | bus_timeout = false; |
| 351 | srq_asserted = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | /* |
Finn Thain | d95fd5f | 2007-05-01 22:32:59 +0200 | [diff] [blame] | 355 | * The notorious ADB interrupt handler - does all of the protocol handling. |
| 356 | * Relies on the ADB controller sending and receiving data, thereby |
| 357 | * generating shift register interrupts (SR_INT) for us. This means there has |
| 358 | * to be activity on the ADB bus. The chip will poll to achieve this. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | * |
Finn Thain | b4d76c2 | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 360 | * The VIA Port B output signalling works as follows. After the ADB transceiver |
| 361 | * sees a transition on the PB4 and PB5 lines it will crank over the VIA shift |
| 362 | * register which eventually raises the SR_INT interrupt. The PB4/PB5 outputs |
| 363 | * are toggled with each byte as the ADB transaction progresses. |
| 364 | * |
| 365 | * Request with no reply expected (and empty transceiver buffer): |
| 366 | * CMD -> IDLE |
| 367 | * Request with expected reply packet (or with buffered autopoll packet): |
| 368 | * CMD -> EVEN -> ODD -> EVEN -> ... -> IDLE |
| 369 | * Unsolicited packet: |
| 370 | * IDLE -> EVEN -> ODD -> EVEN -> ... -> IDLE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 372 | static irqreturn_t macii_interrupt(int irq, void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | { |
Finn Thain | d95fd5f | 2007-05-01 22:32:59 +0200 | [diff] [blame] | 374 | int x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | struct adb_request *req; |
Finn Thain | 5ce6185 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 376 | unsigned long flags; |
| 377 | |
| 378 | local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | |
Finn Thain | d95fd5f | 2007-05-01 22:32:59 +0200 | [diff] [blame] | 380 | if (!arg) { |
| 381 | /* Clear the SR IRQ flag when polling. */ |
| 382 | if (via[IFR] & SR_INT) |
| 383 | via[IFR] = SR_INT; |
Finn Thain | 5ce6185 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 384 | else { |
| 385 | local_irq_restore(flags); |
Finn Thain | d95fd5f | 2007-05-01 22:32:59 +0200 | [diff] [blame] | 386 | return IRQ_NONE; |
Finn Thain | 5ce6185 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 387 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | } |
| 389 | |
Finn Thain | 47fd206 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 390 | status = via[B] & (ST_MASK | CTLR_IRQ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | |
| 392 | switch (macii_state) { |
Finn Thain | 47fd206 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 393 | case idle: |
Finn Thain | b4d76c2 | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 394 | WARN_ON((status & ST_MASK) != ST_IDLE); |
| 395 | |
| 396 | reply_ptr = reply_buf; |
Finn Thain | f87a162 | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 397 | reading_reply = false; |
Finn Thain | b4d76c2 | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 398 | |
| 399 | bus_timeout = false; |
| 400 | srq_asserted = false; |
Finn Thain | d95fd5f | 2007-05-01 22:32:59 +0200 | [diff] [blame] | 401 | |
Finn Thain | 47fd206 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 402 | x = via[SR]; |
Finn Thain | d95fd5f | 2007-05-01 22:32:59 +0200 | [diff] [blame] | 403 | |
Finn Thain | b4d76c2 | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 404 | if (!(status & CTLR_IRQ)) { |
| 405 | /* /CTLR_IRQ asserted in idle state means we must |
| 406 | * read an autopoll reply from the transceiver buffer. |
Finn Thain | d95fd5f | 2007-05-01 22:32:59 +0200 | [diff] [blame] | 407 | */ |
Finn Thain | 47fd206 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 408 | macii_state = reading; |
| 409 | *reply_ptr = x; |
| 410 | reply_len = 1; |
Finn Thain | b4d76c2 | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 411 | } else { |
| 412 | /* bus timeout */ |
Finn Thain | b4d76c2 | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 413 | reply_len = 0; |
Finn Thain | b16b676 | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 414 | break; |
Finn Thain | 47fd206 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 415 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | |
Finn Thain | 47fd206 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 417 | /* set ADB state = even for first data byte */ |
| 418 | via[B] = (via[B] & ~ST_MASK) | ST_EVEN; |
| 419 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | |
Finn Thain | 47fd206 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 421 | case sending: |
| 422 | req = current_req; |
Finn Thain | b4d76c2 | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 423 | |
| 424 | if (status == (ST_CMD | CTLR_IRQ)) { |
| 425 | /* /CTLR_IRQ de-asserted after the command byte means |
| 426 | * the host can continue with the transaction. |
| 427 | */ |
| 428 | |
| 429 | /* Store command byte */ |
| 430 | last_cmd = req->data[1]; |
| 431 | if ((last_cmd & OP_MASK) == TALK) { |
| 432 | last_talk_cmd = last_cmd; |
| 433 | if ((last_cmd & CMD_MASK) == ADB_READREG(0, 0)) |
| 434 | last_poll_cmd = last_cmd; |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | if (status == ST_CMD) { |
| 439 | /* /CTLR_IRQ asserted after the command byte means we |
| 440 | * must read an autopoll reply. The first byte was |
| 441 | * lost because the shift register was an output. |
| 442 | */ |
| 443 | macii_state = reading; |
| 444 | |
Finn Thain | f87a162 | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 445 | reading_reply = false; |
Finn Thain | b4d76c2 | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 446 | reply_ptr = reply_buf; |
| 447 | *reply_ptr = last_talk_cmd; |
| 448 | reply_len = 1; |
| 449 | |
| 450 | /* reset to shift in */ |
| 451 | via[ACR] &= ~SR_OUT; |
| 452 | x = via[SR]; |
| 453 | } else if (data_index >= req->nbytes) { |
Finn Thain | 47fd206 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 454 | req->sent = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | |
Finn Thain | 47fd206 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 456 | if (req->reply_expected) { |
Finn Thain | b4d76c2 | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 457 | macii_state = reading; |
| 458 | |
Finn Thain | f87a162 | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 459 | reading_reply = true; |
Finn Thain | b4d76c2 | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 460 | reply_ptr = req->reply; |
| 461 | *reply_ptr = req->data[1]; |
| 462 | reply_len = 1; |
| 463 | |
| 464 | via[ACR] &= ~SR_OUT; |
| 465 | x = via[SR]; |
Finn Thain | 624cf5b | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 466 | } else if ((req->data[1] & OP_MASK) == TALK) { |
| 467 | macii_state = reading; |
| 468 | |
Finn Thain | f87a162 | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 469 | reading_reply = false; |
Finn Thain | 624cf5b | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 470 | reply_ptr = reply_buf; |
| 471 | *reply_ptr = req->data[1]; |
| 472 | reply_len = 1; |
| 473 | |
| 474 | via[ACR] &= ~SR_OUT; |
| 475 | x = via[SR]; |
| 476 | |
| 477 | req->complete = 1; |
| 478 | current_req = req->next; |
| 479 | if (req->done) |
| 480 | (*req->done)(req); |
Finn Thain | 47fd206 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 481 | } else { |
Finn Thain | b4d76c2 | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 482 | macii_state = idle; |
| 483 | |
Finn Thain | 47fd206 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 484 | req->complete = 1; |
| 485 | current_req = req->next; |
| 486 | if (req->done) |
| 487 | (*req->done)(req); |
Finn Thain | b4d76c2 | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 488 | break; |
Finn Thain | 47fd206 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 489 | } |
| 490 | } else { |
| 491 | via[SR] = req->data[data_index++]; |
Finn Thain | b4d76c2 | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 492 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | |
Finn Thain | b4d76c2 | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 494 | if ((via[B] & ST_MASK) == ST_CMD) { |
| 495 | /* just sent the command byte, set to EVEN */ |
| 496 | via[B] = (via[B] & ~ST_MASK) | ST_EVEN; |
| 497 | } else { |
| 498 | /* invert state bits, toggle ODD/EVEN */ |
| 499 | via[B] ^= ST_MASK; |
Finn Thain | 47fd206 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 500 | } |
| 501 | break; |
| 502 | |
| 503 | case reading: |
| 504 | x = via[SR]; |
| 505 | WARN_ON((status & ST_MASK) == ST_CMD || |
| 506 | (status & ST_MASK) == ST_IDLE); |
| 507 | |
Finn Thain | 47fd206 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 508 | if (!(status & CTLR_IRQ)) { |
Finn Thain | b4d76c2 | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 509 | if (status == ST_EVEN && reply_len == 1) { |
| 510 | bus_timeout = true; |
| 511 | } else if (status == ST_ODD && reply_len == 2) { |
| 512 | srq_asserted = true; |
| 513 | } else { |
Finn Thain | b16b676 | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 514 | macii_state = idle; |
| 515 | |
| 516 | if (bus_timeout) |
| 517 | reply_len = 0; |
| 518 | |
| 519 | if (reading_reply) { |
| 520 | struct adb_request *req = current_req; |
| 521 | |
| 522 | req->reply_len = reply_len; |
| 523 | |
| 524 | req->complete = 1; |
| 525 | current_req = req->next; |
| 526 | if (req->done) |
| 527 | (*req->done)(req); |
Finn Thain | 624cf5b | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 528 | } else if (reply_len && autopoll_devs && |
| 529 | reply_buf[0] == last_poll_cmd) { |
| 530 | adb_input(reply_buf, reply_len, 1); |
Finn Thain | b16b676 | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 531 | } |
| 532 | break; |
Finn Thain | 47fd206 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | |
Finn Thain | b16b676 | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 536 | if (reply_len < ARRAY_SIZE(reply_buf)) { |
Finn Thain | 47fd206 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 537 | reply_ptr++; |
| 538 | *reply_ptr = x; |
| 539 | reply_len++; |
| 540 | } |
| 541 | |
| 542 | /* invert state bits, toggle ODD/EVEN */ |
| 543 | via[B] ^= ST_MASK; |
| 544 | break; |
| 545 | |
Finn Thain | b16b676 | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 546 | default: |
| 547 | break; |
| 548 | } |
Finn Thain | 47fd206 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 549 | |
Finn Thain | b16b676 | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 550 | if (macii_state == idle) { |
Finn Thain | f93bfeb | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 551 | if (!current_req) |
Finn Thain | 47fd206 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 552 | macii_queue_poll(); |
| 553 | |
| 554 | if (current_req) |
| 555 | macii_start(); |
Finn Thain | 47fd206 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 556 | |
Finn Thain | b16b676 | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 557 | if (macii_state == idle) { |
| 558 | via[ACR] &= ~SR_OUT; |
| 559 | x = via[SR]; |
Finn Thain | 47fd206 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 560 | via[B] = (via[B] & ~ST_MASK) | ST_IDLE; |
Finn Thain | b16b676 | 2020-06-28 14:23:12 +1000 | [diff] [blame] | 561 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | } |
Finn Thain | d95fd5f | 2007-05-01 22:32:59 +0200 | [diff] [blame] | 563 | |
Finn Thain | 5ce6185 | 2018-09-11 20:18:44 -0400 | [diff] [blame] | 564 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | return IRQ_HANDLED; |
| 566 | } |