| .. | .. |
|---|
| 12 | 12 | * |
|---|
| 13 | 13 | * 1999-08-02 (jmt) - Initial rewrite for Unified ADB. |
|---|
| 14 | 14 | * 2000-03-29 Tony Mantler <tonym@mac.linux-m68k.org> |
|---|
| 15 | | - * - Big overhaul, should actually work now. |
|---|
| 15 | + * - Big overhaul, should actually work now. |
|---|
| 16 | 16 | * 2006-12-31 Finn Thain - Another overhaul. |
|---|
| 17 | 17 | * |
|---|
| 18 | 18 | * Suggested reading: |
|---|
| .. | .. |
|---|
| 23 | 23 | * Apple's "ADB Analyzer" bus sniffer is invaluable: |
|---|
| 24 | 24 | * ftp://ftp.apple.com/developer/Tool_Chest/Devices_-_Hardware/Apple_Desktop_Bus/ |
|---|
| 25 | 25 | */ |
|---|
| 26 | | - |
|---|
| 26 | + |
|---|
| 27 | 27 | #include <stdarg.h> |
|---|
| 28 | 28 | #include <linux/types.h> |
|---|
| 29 | 29 | #include <linux/errno.h> |
|---|
| .. | .. |
|---|
| 77 | 77 | #define ST_ODD 0x20 /* ADB state: odd data byte */ |
|---|
| 78 | 78 | #define ST_IDLE 0x30 /* ADB state: idle, nothing to send */ |
|---|
| 79 | 79 | |
|---|
| 80 | | -static int macii_init_via(void); |
|---|
| 80 | +/* ADB command byte structure */ |
|---|
| 81 | +#define ADDR_MASK 0xF0 |
|---|
| 82 | +#define CMD_MASK 0x0F |
|---|
| 83 | +#define OP_MASK 0x0C |
|---|
| 84 | +#define TALK 0x0C |
|---|
| 85 | + |
|---|
| 86 | +static int macii_init_via(void); |
|---|
| 81 | 87 | static void macii_start(void); |
|---|
| 82 | 88 | static irqreturn_t macii_interrupt(int irq, void *arg); |
|---|
| 83 | 89 | static void macii_queue_poll(void); |
|---|
| .. | .. |
|---|
| 104 | 110 | idle, |
|---|
| 105 | 111 | sending, |
|---|
| 106 | 112 | reading, |
|---|
| 107 | | - read_done, |
|---|
| 108 | 113 | } macii_state; |
|---|
| 109 | 114 | |
|---|
| 110 | 115 | static struct adb_request *current_req; /* first request struct in the queue */ |
|---|
| 111 | 116 | static struct adb_request *last_req; /* last request struct in the queue */ |
|---|
| 112 | 117 | static unsigned char reply_buf[16]; /* storage for autopolled replies */ |
|---|
| 113 | 118 | static unsigned char *reply_ptr; /* next byte in reply_buf or req->reply */ |
|---|
| 114 | | -static int reading_reply; /* store reply in reply_buf else req->reply */ |
|---|
| 119 | +static bool reading_reply; /* store reply in reply_buf else req->reply */ |
|---|
| 115 | 120 | static int data_index; /* index of the next byte to send from req->data */ |
|---|
| 116 | 121 | static int reply_len; /* number of bytes received in reply_buf or req->reply */ |
|---|
| 117 | 122 | static int status; /* VIA's ADB status bits captured upon interrupt */ |
|---|
| 118 | | -static int last_status; /* status bits as at previous interrupt */ |
|---|
| 119 | | -static int srq_asserted; /* have to poll for the device that asserted it */ |
|---|
| 120 | | -static int command_byte; /* the most recent command byte transmitted */ |
|---|
| 121 | | -static int autopoll_devs; /* bits set are device addresses to be polled */ |
|---|
| 122 | | - |
|---|
| 123 | | -/* Sanity check for request queue. Doesn't check for cycles. */ |
|---|
| 124 | | -static int request_is_queued(struct adb_request *req) { |
|---|
| 125 | | - struct adb_request *cur; |
|---|
| 126 | | - unsigned long flags; |
|---|
| 127 | | - local_irq_save(flags); |
|---|
| 128 | | - cur = current_req; |
|---|
| 129 | | - while (cur) { |
|---|
| 130 | | - if (cur == req) { |
|---|
| 131 | | - local_irq_restore(flags); |
|---|
| 132 | | - return 1; |
|---|
| 133 | | - } |
|---|
| 134 | | - cur = cur->next; |
|---|
| 135 | | - } |
|---|
| 136 | | - local_irq_restore(flags); |
|---|
| 137 | | - return 0; |
|---|
| 138 | | -} |
|---|
| 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 */ |
|---|
| 125 | +static u8 last_cmd; /* the most recent command byte transmitted */ |
|---|
| 126 | +static u8 last_talk_cmd; /* the most recent Talk command byte transmitted */ |
|---|
| 127 | +static u8 last_poll_cmd; /* the most recent Talk R0 command byte transmitted */ |
|---|
| 128 | +static unsigned int autopoll_devs; /* bits set are device addresses to poll */ |
|---|
| 139 | 129 | |
|---|
| 140 | 130 | /* Check for MacII style ADB */ |
|---|
| 141 | 131 | static int macii_probe(void) |
|---|
| 142 | 132 | { |
|---|
| 143 | | - if (macintosh_config->adb_type != MAC_ADB_II) return -ENODEV; |
|---|
| 133 | + if (macintosh_config->adb_type != MAC_ADB_II) |
|---|
| 134 | + return -ENODEV; |
|---|
| 144 | 135 | |
|---|
| 145 | 136 | via = via1; |
|---|
| 146 | 137 | |
|---|
| 147 | | - printk("adb: Mac II ADB Driver v1.0 for Unified ADB\n"); |
|---|
| 138 | + pr_info("adb: Mac II ADB Driver v1.0 for Unified ADB\n"); |
|---|
| 148 | 139 | return 0; |
|---|
| 149 | 140 | } |
|---|
| 150 | 141 | |
|---|
| 151 | 142 | /* Initialize the driver */ |
|---|
| 152 | | -int macii_init(void) |
|---|
| 143 | +static int macii_init(void) |
|---|
| 153 | 144 | { |
|---|
| 154 | 145 | unsigned long flags; |
|---|
| 155 | 146 | int err; |
|---|
| 156 | | - |
|---|
| 147 | + |
|---|
| 157 | 148 | local_irq_save(flags); |
|---|
| 158 | | - |
|---|
| 149 | + |
|---|
| 159 | 150 | err = macii_init_via(); |
|---|
| 160 | | - if (err) goto out; |
|---|
| 151 | + if (err) |
|---|
| 152 | + goto out; |
|---|
| 161 | 153 | |
|---|
| 162 | 154 | err = request_irq(IRQ_MAC_ADB, macii_interrupt, 0, "ADB", |
|---|
| 163 | 155 | macii_interrupt); |
|---|
| 164 | | - if (err) goto out; |
|---|
| 156 | + if (err) |
|---|
| 157 | + goto out; |
|---|
| 165 | 158 | |
|---|
| 166 | 159 | macii_state = idle; |
|---|
| 167 | 160 | out: |
|---|
| .. | .. |
|---|
| 169 | 162 | return err; |
|---|
| 170 | 163 | } |
|---|
| 171 | 164 | |
|---|
| 172 | | -/* initialize the hardware */ |
|---|
| 165 | +/* initialize the hardware */ |
|---|
| 173 | 166 | static int macii_init_via(void) |
|---|
| 174 | 167 | { |
|---|
| 175 | 168 | unsigned char x; |
|---|
| .. | .. |
|---|
| 179 | 172 | |
|---|
| 180 | 173 | /* Set up state: idle */ |
|---|
| 181 | 174 | via[B] |= ST_IDLE; |
|---|
| 182 | | - last_status = via[B] & (ST_MASK|CTLR_IRQ); |
|---|
| 183 | 175 | |
|---|
| 184 | 176 | /* Shift register on input */ |
|---|
| 185 | 177 | via[ACR] = (via[ACR] & ~SR_CTRL) | SR_EXT; |
|---|
| .. | .. |
|---|
| 193 | 185 | /* Send an ADB poll (Talk Register 0 command prepended to the request queue) */ |
|---|
| 194 | 186 | static void macii_queue_poll(void) |
|---|
| 195 | 187 | { |
|---|
| 196 | | - /* No point polling the active device as it will never assert SRQ, so |
|---|
| 197 | | - * poll the next device in the autopoll list. This could leave us |
|---|
| 198 | | - * stuck in a polling loop if an unprobed device is asserting SRQ. |
|---|
| 199 | | - * In theory, that could only happen if a device was plugged in after |
|---|
| 200 | | - * probing started. Unplugging it again will break the cycle. |
|---|
| 201 | | - * (Simply polling the next higher device often ends up polling almost |
|---|
| 202 | | - * every device (after wrapping around), which takes too long.) |
|---|
| 203 | | - */ |
|---|
| 204 | | - int device_mask; |
|---|
| 205 | | - int next_device; |
|---|
| 206 | 188 | static struct adb_request req; |
|---|
| 189 | + unsigned char poll_command; |
|---|
| 190 | + unsigned int poll_addr; |
|---|
| 207 | 191 | |
|---|
| 208 | | - if (!autopoll_devs) return; |
|---|
| 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 | + */ |
|---|
| 197 | + if (!autopoll_devs) |
|---|
| 198 | + return; |
|---|
| 209 | 199 | |
|---|
| 210 | | - device_mask = (1 << (((command_byte & 0xF0) >> 4) + 1)) - 1; |
|---|
| 211 | | - if (autopoll_devs & ~device_mask) |
|---|
| 212 | | - next_device = ffs(autopoll_devs & ~device_mask) - 1; |
|---|
| 213 | | - else |
|---|
| 214 | | - next_device = ffs(autopoll_devs) - 1; |
|---|
| 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; |
|---|
| 215 | 209 | |
|---|
| 216 | | - BUG_ON(request_is_queued(&req)); |
|---|
| 210 | + higher_devs = autopoll_devs & -(1 << (poll_addr + 1)); |
|---|
| 211 | + poll_addr = ffs(higher_devs ? higher_devs : autopoll_devs) - 1; |
|---|
| 212 | + } |
|---|
| 217 | 213 | |
|---|
| 218 | | - adb_request(&req, NULL, ADBREQ_NOSEND, 1, |
|---|
| 219 | | - ADB_READREG(next_device, 0)); |
|---|
| 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); |
|---|
| 220 | 224 | |
|---|
| 221 | 225 | req.sent = 0; |
|---|
| 222 | 226 | req.complete = 0; |
|---|
| 223 | 227 | req.reply_len = 0; |
|---|
| 224 | 228 | req.next = current_req; |
|---|
| 225 | 229 | |
|---|
| 226 | | - if (current_req != NULL) { |
|---|
| 230 | + if (WARN_ON(current_req)) { |
|---|
| 227 | 231 | current_req = &req; |
|---|
| 228 | 232 | } else { |
|---|
| 229 | 233 | current_req = &req; |
|---|
| .. | .. |
|---|
| 235 | 239 | static int macii_send_request(struct adb_request *req, int sync) |
|---|
| 236 | 240 | { |
|---|
| 237 | 241 | int err; |
|---|
| 238 | | - unsigned long flags; |
|---|
| 239 | 242 | |
|---|
| 240 | | - BUG_ON(request_is_queued(req)); |
|---|
| 241 | | - |
|---|
| 242 | | - local_irq_save(flags); |
|---|
| 243 | 243 | err = macii_write(req); |
|---|
| 244 | | - local_irq_restore(flags); |
|---|
| 244 | + if (err) |
|---|
| 245 | + return err; |
|---|
| 245 | 246 | |
|---|
| 246 | | - if (!err && sync) { |
|---|
| 247 | | - while (!req->complete) { |
|---|
| 247 | + if (sync) |
|---|
| 248 | + while (!req->complete) |
|---|
| 248 | 249 | macii_poll(); |
|---|
| 249 | | - } |
|---|
| 250 | | - BUG_ON(request_is_queued(req)); |
|---|
| 251 | | - } |
|---|
| 252 | 250 | |
|---|
| 253 | | - return err; |
|---|
| 251 | + return 0; |
|---|
| 254 | 252 | } |
|---|
| 255 | 253 | |
|---|
| 256 | 254 | /* Send an ADB request (append to request queue) */ |
|---|
| 257 | 255 | static int macii_write(struct adb_request *req) |
|---|
| 258 | 256 | { |
|---|
| 257 | + unsigned long flags; |
|---|
| 258 | + |
|---|
| 259 | 259 | if (req->nbytes < 2 || req->data[0] != ADB_PACKET || req->nbytes > 15) { |
|---|
| 260 | 260 | req->complete = 1; |
|---|
| 261 | 261 | return -EINVAL; |
|---|
| 262 | 262 | } |
|---|
| 263 | | - |
|---|
| 263 | + |
|---|
| 264 | 264 | req->next = NULL; |
|---|
| 265 | 265 | req->sent = 0; |
|---|
| 266 | 266 | req->complete = 0; |
|---|
| 267 | 267 | req->reply_len = 0; |
|---|
| 268 | + |
|---|
| 269 | + local_irq_save(flags); |
|---|
| 268 | 270 | |
|---|
| 269 | 271 | if (current_req != NULL) { |
|---|
| 270 | 272 | last_req->next = req; |
|---|
| .. | .. |
|---|
| 272 | 274 | } else { |
|---|
| 273 | 275 | current_req = req; |
|---|
| 274 | 276 | last_req = req; |
|---|
| 275 | | - if (macii_state == idle) macii_start(); |
|---|
| 277 | + if (macii_state == idle) |
|---|
| 278 | + macii_start(); |
|---|
| 276 | 279 | } |
|---|
| 280 | + |
|---|
| 281 | + local_irq_restore(flags); |
|---|
| 282 | + |
|---|
| 277 | 283 | return 0; |
|---|
| 278 | 284 | } |
|---|
| 279 | 285 | |
|---|
| 280 | 286 | /* Start auto-polling */ |
|---|
| 281 | 287 | static int macii_autopoll(int devs) |
|---|
| 282 | 288 | { |
|---|
| 283 | | - static struct adb_request req; |
|---|
| 284 | 289 | unsigned long flags; |
|---|
| 285 | | - int err = 0; |
|---|
| 286 | | - |
|---|
| 287 | | - /* bit 1 == device 1, and so on. */ |
|---|
| 288 | | - autopoll_devs = devs & 0xFFFE; |
|---|
| 289 | | - |
|---|
| 290 | | - if (!autopoll_devs) return 0; |
|---|
| 291 | 290 | |
|---|
| 292 | 291 | local_irq_save(flags); |
|---|
| 293 | 292 | |
|---|
| 294 | | - if (current_req == NULL) { |
|---|
| 295 | | - /* Send a Talk Reg 0. The controller will repeatedly transmit |
|---|
| 296 | | - * this as long as it is idle. |
|---|
| 297 | | - */ |
|---|
| 298 | | - adb_request(&req, NULL, ADBREQ_NOSEND, 1, |
|---|
| 299 | | - ADB_READREG(ffs(autopoll_devs) - 1, 0)); |
|---|
| 300 | | - err = macii_write(&req); |
|---|
| 293 | + /* bit 1 == device 1, and so on. */ |
|---|
| 294 | + autopoll_devs = (unsigned int)devs & 0xFFFE; |
|---|
| 295 | + |
|---|
| 296 | + if (!current_req) { |
|---|
| 297 | + macii_queue_poll(); |
|---|
| 298 | + if (current_req && macii_state == idle) |
|---|
| 299 | + macii_start(); |
|---|
| 301 | 300 | } |
|---|
| 302 | 301 | |
|---|
| 303 | 302 | local_irq_restore(flags); |
|---|
| 304 | | - return err; |
|---|
| 305 | | -} |
|---|
| 306 | 303 | |
|---|
| 307 | | -static inline int need_autopoll(void) { |
|---|
| 308 | | - /* Was the last command Talk Reg 0 |
|---|
| 309 | | - * and is the target on the autopoll list? |
|---|
| 310 | | - */ |
|---|
| 311 | | - if ((command_byte & 0x0F) == 0x0C && |
|---|
| 312 | | - ((1 << ((command_byte & 0xF0) >> 4)) & autopoll_devs)) |
|---|
| 313 | | - return 0; |
|---|
| 314 | | - return 1; |
|---|
| 304 | + return 0; |
|---|
| 315 | 305 | } |
|---|
| 316 | 306 | |
|---|
| 317 | 307 | /* Prod the chip without interrupts */ |
|---|
| 318 | 308 | static void macii_poll(void) |
|---|
| 319 | 309 | { |
|---|
| 320 | | - disable_irq(IRQ_MAC_ADB); |
|---|
| 321 | 310 | macii_interrupt(0, NULL); |
|---|
| 322 | | - enable_irq(IRQ_MAC_ADB); |
|---|
| 323 | 311 | } |
|---|
| 324 | 312 | |
|---|
| 325 | 313 | /* Reset the bus */ |
|---|
| 326 | 314 | static int macii_reset_bus(void) |
|---|
| 327 | 315 | { |
|---|
| 328 | | - static struct adb_request req; |
|---|
| 329 | | - |
|---|
| 330 | | - if (request_is_queued(&req)) |
|---|
| 331 | | - return 0; |
|---|
| 316 | + struct adb_request req; |
|---|
| 332 | 317 | |
|---|
| 333 | 318 | /* Command = 0, Address = ignored */ |
|---|
| 334 | | - adb_request(&req, NULL, 0, 1, ADB_BUSRESET); |
|---|
| 319 | + adb_request(&req, NULL, ADBREQ_NOSEND, 1, ADB_BUSRESET); |
|---|
| 320 | + macii_send_request(&req, 1); |
|---|
| 335 | 321 | |
|---|
| 336 | 322 | /* Don't want any more requests during the Global Reset low time. */ |
|---|
| 337 | 323 | udelay(3000); |
|---|
| .. | .. |
|---|
| 346 | 332 | |
|---|
| 347 | 333 | req = current_req; |
|---|
| 348 | 334 | |
|---|
| 349 | | - BUG_ON(req == NULL); |
|---|
| 350 | | - |
|---|
| 351 | | - BUG_ON(macii_state != idle); |
|---|
| 352 | | - |
|---|
| 353 | 335 | /* Now send it. Be careful though, that first byte of the request |
|---|
| 354 | 336 | * is actually ADB_PACKET; the real data begins at index 1! |
|---|
| 355 | 337 | * And req->nbytes is the number of bytes of real data plus one. |
|---|
| 356 | 338 | */ |
|---|
| 357 | 339 | |
|---|
| 358 | | - /* store command byte */ |
|---|
| 359 | | - command_byte = req->data[1]; |
|---|
| 360 | 340 | /* Output mode */ |
|---|
| 361 | 341 | via[ACR] |= SR_OUT; |
|---|
| 362 | 342 | /* Load data */ |
|---|
| .. | .. |
|---|
| 366 | 346 | |
|---|
| 367 | 347 | macii_state = sending; |
|---|
| 368 | 348 | data_index = 2; |
|---|
| 349 | + |
|---|
| 350 | + bus_timeout = false; |
|---|
| 351 | + srq_asserted = false; |
|---|
| 369 | 352 | } |
|---|
| 370 | 353 | |
|---|
| 371 | 354 | /* |
|---|
| .. | .. |
|---|
| 374 | 357 | * generating shift register interrupts (SR_INT) for us. This means there has |
|---|
| 375 | 358 | * to be activity on the ADB bus. The chip will poll to achieve this. |
|---|
| 376 | 359 | * |
|---|
| 377 | | - * The basic ADB state machine was left unchanged from the original MacII code |
|---|
| 378 | | - * by Alan Cox, which was based on the CUDA driver for PowerMac. |
|---|
| 379 | | - * The syntax of the ADB status lines is totally different on MacII, |
|---|
| 380 | | - * though. MacII uses the states Command -> Even -> Odd -> Even ->...-> Idle |
|---|
| 381 | | - * for sending and Idle -> Even -> Odd -> Even ->...-> Idle for receiving. |
|---|
| 382 | | - * Start and end of a receive packet are signalled by asserting /IRQ on the |
|---|
| 383 | | - * interrupt line (/IRQ means the CTLR_IRQ bit in port B; not to be confused |
|---|
| 384 | | - * with the VIA shift register interrupt. /IRQ never actually interrupts the |
|---|
| 385 | | - * processor, it's just an ordinary input.) |
|---|
| 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 |
|---|
| 386 | 371 | */ |
|---|
| 387 | 372 | static irqreturn_t macii_interrupt(int irq, void *arg) |
|---|
| 388 | 373 | { |
|---|
| 389 | 374 | int x; |
|---|
| 390 | | - static int entered; |
|---|
| 391 | 375 | struct adb_request *req; |
|---|
| 376 | + unsigned long flags; |
|---|
| 377 | + |
|---|
| 378 | + local_irq_save(flags); |
|---|
| 392 | 379 | |
|---|
| 393 | 380 | if (!arg) { |
|---|
| 394 | 381 | /* Clear the SR IRQ flag when polling. */ |
|---|
| 395 | 382 | if (via[IFR] & SR_INT) |
|---|
| 396 | 383 | via[IFR] = SR_INT; |
|---|
| 397 | | - else |
|---|
| 384 | + else { |
|---|
| 385 | + local_irq_restore(flags); |
|---|
| 398 | 386 | return IRQ_NONE; |
|---|
| 387 | + } |
|---|
| 399 | 388 | } |
|---|
| 400 | 389 | |
|---|
| 401 | | - BUG_ON(entered++); |
|---|
| 402 | | - |
|---|
| 403 | | - last_status = status; |
|---|
| 404 | | - status = via[B] & (ST_MASK|CTLR_IRQ); |
|---|
| 390 | + status = via[B] & (ST_MASK | CTLR_IRQ); |
|---|
| 405 | 391 | |
|---|
| 406 | 392 | switch (macii_state) { |
|---|
| 407 | | - case idle: |
|---|
| 408 | | - if (reading_reply) { |
|---|
| 409 | | - reply_ptr = current_req->reply; |
|---|
| 410 | | - } else { |
|---|
| 411 | | - BUG_ON(current_req != NULL); |
|---|
| 412 | | - reply_ptr = reply_buf; |
|---|
| 413 | | - } |
|---|
| 393 | + case idle: |
|---|
| 394 | + WARN_ON((status & ST_MASK) != ST_IDLE); |
|---|
| 414 | 395 | |
|---|
| 415 | | - x = via[SR]; |
|---|
| 396 | + reply_ptr = reply_buf; |
|---|
| 397 | + reading_reply = false; |
|---|
| 416 | 398 | |
|---|
| 417 | | - if ((status & CTLR_IRQ) && (x == 0xFF)) { |
|---|
| 418 | | - /* Bus timeout without SRQ sequence: |
|---|
| 419 | | - * data is "FF" while CTLR_IRQ is "H" |
|---|
| 420 | | - */ |
|---|
| 421 | | - reply_len = 0; |
|---|
| 422 | | - srq_asserted = 0; |
|---|
| 423 | | - macii_state = read_done; |
|---|
| 424 | | - } else { |
|---|
| 425 | | - macii_state = reading; |
|---|
| 426 | | - *reply_ptr = x; |
|---|
| 427 | | - reply_len = 1; |
|---|
| 428 | | - } |
|---|
| 399 | + bus_timeout = false; |
|---|
| 400 | + srq_asserted = false; |
|---|
| 429 | 401 | |
|---|
| 430 | | - /* set ADB state = even for first data byte */ |
|---|
| 431 | | - via[B] = (via[B] & ~ST_MASK) | ST_EVEN; |
|---|
| 402 | + x = via[SR]; |
|---|
| 403 | + |
|---|
| 404 | + if (!(status & CTLR_IRQ)) { |
|---|
| 405 | + /* /CTLR_IRQ asserted in idle state means we must |
|---|
| 406 | + * read an autopoll reply from the transceiver buffer. |
|---|
| 407 | + */ |
|---|
| 408 | + macii_state = reading; |
|---|
| 409 | + *reply_ptr = x; |
|---|
| 410 | + reply_len = 1; |
|---|
| 411 | + } else { |
|---|
| 412 | + /* bus timeout */ |
|---|
| 413 | + reply_len = 0; |
|---|
| 432 | 414 | break; |
|---|
| 415 | + } |
|---|
| 433 | 416 | |
|---|
| 434 | | - case sending: |
|---|
| 435 | | - req = current_req; |
|---|
| 436 | | - if (data_index >= req->nbytes) { |
|---|
| 437 | | - req->sent = 1; |
|---|
| 438 | | - macii_state = idle; |
|---|
| 417 | + /* set ADB state = even for first data byte */ |
|---|
| 418 | + via[B] = (via[B] & ~ST_MASK) | ST_EVEN; |
|---|
| 419 | + break; |
|---|
| 439 | 420 | |
|---|
| 440 | | - if (req->reply_expected) { |
|---|
| 441 | | - reading_reply = 1; |
|---|
| 442 | | - } else { |
|---|
| 443 | | - req->complete = 1; |
|---|
| 444 | | - current_req = req->next; |
|---|
| 445 | | - if (req->done) (*req->done)(req); |
|---|
| 421 | + case sending: |
|---|
| 422 | + req = current_req; |
|---|
| 446 | 423 | |
|---|
| 447 | | - if (current_req) |
|---|
| 448 | | - macii_start(); |
|---|
| 449 | | - else |
|---|
| 450 | | - if (need_autopoll()) |
|---|
| 451 | | - macii_autopoll(autopoll_devs); |
|---|
| 452 | | - } |
|---|
| 453 | | - |
|---|
| 454 | | - if (macii_state == idle) { |
|---|
| 455 | | - /* reset to shift in */ |
|---|
| 456 | | - via[ACR] &= ~SR_OUT; |
|---|
| 457 | | - x = via[SR]; |
|---|
| 458 | | - /* set ADB state idle - might get SRQ */ |
|---|
| 459 | | - via[B] = (via[B] & ~ST_MASK) | ST_IDLE; |
|---|
| 460 | | - } |
|---|
| 461 | | - } else { |
|---|
| 462 | | - via[SR] = req->data[data_index++]; |
|---|
| 463 | | - |
|---|
| 464 | | - if ( (via[B] & ST_MASK) == ST_CMD ) { |
|---|
| 465 | | - /* just sent the command byte, set to EVEN */ |
|---|
| 466 | | - via[B] = (via[B] & ~ST_MASK) | ST_EVEN; |
|---|
| 467 | | - } else { |
|---|
| 468 | | - /* invert state bits, toggle ODD/EVEN */ |
|---|
| 469 | | - via[B] ^= ST_MASK; |
|---|
| 470 | | - } |
|---|
| 471 | | - } |
|---|
| 472 | | - break; |
|---|
| 473 | | - |
|---|
| 474 | | - case reading: |
|---|
| 475 | | - x = via[SR]; |
|---|
| 476 | | - BUG_ON((status & ST_MASK) == ST_CMD || |
|---|
| 477 | | - (status & ST_MASK) == ST_IDLE); |
|---|
| 478 | | - |
|---|
| 479 | | - /* Bus timeout with SRQ sequence: |
|---|
| 480 | | - * data is "XX FF" while CTLR_IRQ is "L L" |
|---|
| 481 | | - * End of packet without SRQ sequence: |
|---|
| 482 | | - * data is "XX...YY 00" while CTLR_IRQ is "L...H L" |
|---|
| 483 | | - * End of packet SRQ sequence: |
|---|
| 484 | | - * data is "XX...YY 00" while CTLR_IRQ is "L...L L" |
|---|
| 485 | | - * (where XX is the first response byte and |
|---|
| 486 | | - * YY is the last byte of valid response data.) |
|---|
| 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. |
|---|
| 487 | 427 | */ |
|---|
| 488 | 428 | |
|---|
| 489 | | - srq_asserted = 0; |
|---|
| 490 | | - if (!(status & CTLR_IRQ)) { |
|---|
| 491 | | - if (x == 0xFF) { |
|---|
| 492 | | - if (!(last_status & CTLR_IRQ)) { |
|---|
| 493 | | - macii_state = read_done; |
|---|
| 494 | | - reply_len = 0; |
|---|
| 495 | | - srq_asserted = 1; |
|---|
| 496 | | - } |
|---|
| 497 | | - } else if (x == 0x00) { |
|---|
| 498 | | - macii_state = read_done; |
|---|
| 499 | | - if (!(last_status & CTLR_IRQ)) |
|---|
| 500 | | - srq_asserted = 1; |
|---|
| 501 | | - } |
|---|
| 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; |
|---|
| 502 | 435 | } |
|---|
| 436 | + } |
|---|
| 503 | 437 | |
|---|
| 504 | | - if (macii_state == reading) { |
|---|
| 505 | | - BUG_ON(reply_len > 15); |
|---|
| 506 | | - reply_ptr++; |
|---|
| 507 | | - *reply_ptr = x; |
|---|
| 508 | | - reply_len++; |
|---|
| 509 | | - } |
|---|
| 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; |
|---|
| 510 | 444 | |
|---|
| 511 | | - /* invert state bits, toggle ODD/EVEN */ |
|---|
| 512 | | - via[B] ^= ST_MASK; |
|---|
| 513 | | - break; |
|---|
| 445 | + reading_reply = false; |
|---|
| 446 | + reply_ptr = reply_buf; |
|---|
| 447 | + *reply_ptr = last_talk_cmd; |
|---|
| 448 | + reply_len = 1; |
|---|
| 514 | 449 | |
|---|
| 515 | | - case read_done: |
|---|
| 450 | + /* reset to shift in */ |
|---|
| 451 | + via[ACR] &= ~SR_OUT; |
|---|
| 516 | 452 | x = via[SR]; |
|---|
| 453 | + } else if (data_index >= req->nbytes) { |
|---|
| 454 | + req->sent = 1; |
|---|
| 517 | 455 | |
|---|
| 518 | | - if (reading_reply) { |
|---|
| 519 | | - reading_reply = 0; |
|---|
| 520 | | - req = current_req; |
|---|
| 521 | | - req->reply_len = reply_len; |
|---|
| 456 | + if (req->reply_expected) { |
|---|
| 457 | + macii_state = reading; |
|---|
| 458 | + |
|---|
| 459 | + reading_reply = true; |
|---|
| 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]; |
|---|
| 466 | + } else if ((req->data[1] & OP_MASK) == TALK) { |
|---|
| 467 | + macii_state = reading; |
|---|
| 468 | + |
|---|
| 469 | + reading_reply = false; |
|---|
| 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 | + |
|---|
| 522 | 477 | req->complete = 1; |
|---|
| 523 | 478 | current_req = req->next; |
|---|
| 524 | | - if (req->done) (*req->done)(req); |
|---|
| 525 | | - } else if (reply_len && autopoll_devs) |
|---|
| 526 | | - adb_input(reply_buf, reply_len, 0); |
|---|
| 479 | + if (req->done) |
|---|
| 480 | + (*req->done)(req); |
|---|
| 481 | + } else { |
|---|
| 482 | + macii_state = idle; |
|---|
| 527 | 483 | |
|---|
| 528 | | - macii_state = idle; |
|---|
| 484 | + req->complete = 1; |
|---|
| 485 | + current_req = req->next; |
|---|
| 486 | + if (req->done) |
|---|
| 487 | + (*req->done)(req); |
|---|
| 488 | + break; |
|---|
| 489 | + } |
|---|
| 490 | + } else { |
|---|
| 491 | + via[SR] = req->data[data_index++]; |
|---|
| 492 | + } |
|---|
| 529 | 493 | |
|---|
| 530 | | - /* SRQ seen before, initiate poll now */ |
|---|
| 531 | | - if (srq_asserted) |
|---|
| 532 | | - macii_queue_poll(); |
|---|
| 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; |
|---|
| 500 | + } |
|---|
| 501 | + break; |
|---|
| 533 | 502 | |
|---|
| 534 | | - if (current_req) |
|---|
| 535 | | - macii_start(); |
|---|
| 536 | | - else |
|---|
| 537 | | - if (need_autopoll()) |
|---|
| 538 | | - macii_autopoll(autopoll_devs); |
|---|
| 503 | + case reading: |
|---|
| 504 | + x = via[SR]; |
|---|
| 505 | + WARN_ON((status & ST_MASK) == ST_CMD || |
|---|
| 506 | + (status & ST_MASK) == ST_IDLE); |
|---|
| 539 | 507 | |
|---|
| 540 | | - if (macii_state == idle) |
|---|
| 541 | | - via[B] = (via[B] & ~ST_MASK) | ST_IDLE; |
|---|
| 542 | | - break; |
|---|
| 508 | + if (!(status & CTLR_IRQ)) { |
|---|
| 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 { |
|---|
| 514 | + macii_state = idle; |
|---|
| 543 | 515 | |
|---|
| 544 | | - default: |
|---|
| 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); |
|---|
| 528 | + } else if (reply_len && autopoll_devs && |
|---|
| 529 | + reply_buf[0] == last_poll_cmd) { |
|---|
| 530 | + adb_input(reply_buf, reply_len, 1); |
|---|
| 531 | + } |
|---|
| 532 | + break; |
|---|
| 533 | + } |
|---|
| 534 | + } |
|---|
| 535 | + |
|---|
| 536 | + if (reply_len < ARRAY_SIZE(reply_buf)) { |
|---|
| 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 | + |
|---|
| 546 | + default: |
|---|
| 545 | 547 | break; |
|---|
| 546 | 548 | } |
|---|
| 547 | 549 | |
|---|
| 548 | | - entered--; |
|---|
| 550 | + if (macii_state == idle) { |
|---|
| 551 | + if (!current_req) |
|---|
| 552 | + macii_queue_poll(); |
|---|
| 553 | + |
|---|
| 554 | + if (current_req) |
|---|
| 555 | + macii_start(); |
|---|
| 556 | + |
|---|
| 557 | + if (macii_state == idle) { |
|---|
| 558 | + via[ACR] &= ~SR_OUT; |
|---|
| 559 | + x = via[SR]; |
|---|
| 560 | + via[B] = (via[B] & ~ST_MASK) | ST_IDLE; |
|---|
| 561 | + } |
|---|
| 562 | + } |
|---|
| 563 | + |
|---|
| 564 | + local_irq_restore(flags); |
|---|
| 549 | 565 | return IRQ_HANDLED; |
|---|
| 550 | 566 | } |
|---|