aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/macintosh
diff options
context:
space:
mode:
authorFinn Thain <fthain@telegraphics.com.au>2007-05-01 16:32:59 -0400
committerLinus Torvalds <torvalds@woody.linux-foundation.org>2007-05-04 20:59:07 -0400
commitd95fd5fce88f05fd36004f2a0c317e665549e54c (patch)
treeaa154c94a232800e913dd6bf5cca02cca7d2d434 /drivers/macintosh
parent217f6710c275118af1008bb1447d244be52d2dc3 (diff)
m68k: Mac II ADB fixes
Fix a crash caused by requests placed in the queue with the completed flag already set. This lead to some ADB_SYNC requests returning early and their request structs being popped off the stack while still queued. Stack corruption ensued or an invalid request callback pointer was invoked or both. Eliminate macii_retransmit() and its buggy implementation of macii_write(). Have macii_queue_poll() fully initialise the request queues. Fix a bug in macii_queue_poll() where the last_req pointer was not being set. This caused some requests to leave the queue before being completed (and would also corrupt the stack under certain conditions). Fix a race in macii_start that could set the state machine to "reading" while current_req was null. No longer send poll commands with the ADBREQ_REPLY flag -- doing that caused the replies to be stored in the request buffer where they were forgotten about. Don't autopoll by continuously sending new Talk commands. Get the controller to do that for us. This reduces the ADB interrupt rate on an idle bus to about 5 per second. Only autopoll the devices that were probed. Explicitly clear the interrupt flag when polling. Use disable_irq rather than local_irq_save when polling. Remove excess local_irq_save/restore pairs. Improve bus timeout and service request detection. Remove unused code (last_reply, adb_dir etc) and unneeded code (prefix_len, first_byte etc). Change TIP and TACK to their correct names on this ADB controller (ST_EVEN and ST_ODD). Add some commentry. Add a generous quantity of sanity checks (BUG_ONs). Let m68k macs use the adb_sync boot param too. Tested on Mac II, Mac IIci, Quadra 650, Quadra 700 etc. Signed-off-by: Finn Thain <fthain@telegraphics.com.au> Signed-off-by: Geert Uytterhoeven <geert@linux-m68k.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Diffstat (limited to 'drivers/macintosh')
-rw-r--r--drivers/macintosh/via-macii.c582
1 files changed, 241 insertions, 341 deletions
diff --git a/drivers/macintosh/via-macii.c b/drivers/macintosh/via-macii.c
index 1b3bad62a1be..01b8eca7ccd5 100644
--- a/drivers/macintosh/via-macii.c
+++ b/drivers/macintosh/via-macii.c
@@ -12,6 +12,15 @@
12 * 1999-08-02 (jmt) - Initial rewrite for Unified ADB. 12 * 1999-08-02 (jmt) - Initial rewrite for Unified ADB.
13 * 2000-03-29 Tony Mantler <tonym@mac.linux-m68k.org> 13 * 2000-03-29 Tony Mantler <tonym@mac.linux-m68k.org>
14 * - Big overhaul, should actually work now. 14 * - Big overhaul, should actually work now.
15 * 2006-12-31 Finn Thain <fthain@telegraphics.com.au> - Another overhaul.
16 *
17 * Suggested reading:
18 * Inside Macintosh, ch. 5 ADB Manager
19 * Guide to the Macinstosh Family Hardware, ch. 8 Apple Desktop Bus
20 * Rockwell R6522 VIA datasheet
21 *
22 * Apple's "ADB Analyzer" bus sniffer is invaluable:
23 * ftp://ftp.apple.com/developer/Tool_Chest/Devices_-_Hardware/Apple_Desktop_Bus/
15 */ 24 */
16 25
17#include <stdarg.h> 26#include <stdarg.h>
@@ -26,7 +35,6 @@
26#include <asm/macints.h> 35#include <asm/macints.h>
27#include <asm/machw.h> 36#include <asm/machw.h>
28#include <asm/mac_via.h> 37#include <asm/mac_via.h>
29#include <asm/io.h>
30#include <asm/system.h> 38#include <asm/system.h>
31 39
32static volatile unsigned char *via; 40static volatile unsigned char *via;
@@ -51,9 +59,7 @@ static volatile unsigned char *via;
51#define ANH (15*RS) /* A-side data, no handshake */ 59#define ANH (15*RS) /* A-side data, no handshake */
52 60
53/* Bits in B data register: all active low */ 61/* Bits in B data register: all active low */
54#define TREQ 0x08 /* Transfer request (input) */ 62#define CTLR_IRQ 0x08 /* Controller rcv status (input) */
55#define TACK 0x10 /* Transfer acknowledge (output) */
56#define TIP 0x20 /* Transfer in progress (output) */
57#define ST_MASK 0x30 /* mask for selecting ADB state bits */ 63#define ST_MASK 0x30 /* mask for selecting ADB state bits */
58 64
59/* Bits in ACR */ 65/* Bits in ACR */
@@ -65,8 +71,6 @@ static volatile unsigned char *via;
65#define IER_SET 0x80 /* set bits in IER */ 71#define IER_SET 0x80 /* set bits in IER */
66#define IER_CLR 0 /* clear bits in IER */ 72#define IER_CLR 0 /* clear bits in IER */
67#define SR_INT 0x04 /* Shift register full/empty */ 73#define SR_INT 0x04 /* Shift register full/empty */
68#define SR_DATA 0x08 /* Shift register data */
69#define SR_CLOCK 0x10 /* Shift register clock */
70 74
71/* ADB transaction states according to GMHW */ 75/* ADB transaction states according to GMHW */
72#define ST_CMD 0x00 /* ADB state: command byte */ 76#define ST_CMD 0x00 /* ADB state: command byte */
@@ -77,7 +81,6 @@ static volatile unsigned char *via;
77static int macii_init_via(void); 81static int macii_init_via(void);
78static void macii_start(void); 82static void macii_start(void);
79static irqreturn_t macii_interrupt(int irq, void *arg); 83static irqreturn_t macii_interrupt(int irq, void *arg);
80static void macii_retransmit(int);
81static void macii_queue_poll(void); 84static void macii_queue_poll(void);
82 85
83static int macii_probe(void); 86static int macii_probe(void);
@@ -103,29 +106,37 @@ static enum macii_state {
103 sending, 106 sending,
104 reading, 107 reading,
105 read_done, 108 read_done,
106 awaiting_reply
107} macii_state; 109} macii_state;
108 110
109static int need_poll; 111static struct adb_request *current_req; /* first request struct in the queue */
110static int command_byte; 112static struct adb_request *last_req; /* last request struct in the queue */
111static int last_reply; 113static unsigned char reply_buf[16]; /* storage for autopolled replies */
112static int last_active; 114static unsigned char *reply_ptr; /* next byte in req->data or reply_buf */
113 115static int reading_reply; /* store reply in reply_buf else req->reply */
114static struct adb_request *current_req; 116static int data_index; /* index of the next byte to send from req->data */
115static struct adb_request *last_req; 117static int reply_len; /* number of bytes received in reply_buf or req->reply */
116static struct adb_request *retry_req; 118static int status; /* VIA's ADB status bits captured upon interrupt */
117static unsigned char reply_buf[16]; 119static int last_status; /* status bits as at previous interrupt */
118static unsigned char *reply_ptr; 120static int srq_asserted; /* have to poll for the device that asserted it */
119static int reply_len; 121static int command_byte; /* the most recent command byte transmitted */
120static int reading_reply; 122static int autopoll_devs; /* bits set are device addresses to be polled */
121static int data_index; 123
122static int first_byte; 124/* Sanity check for request queue. Doesn't check for cycles. */
123static int prefix_len; 125static int request_is_queued(struct adb_request *req) {
124static int status = ST_IDLE|TREQ; 126 struct adb_request *cur;
125static int last_status; 127 unsigned long flags;
126static int driver_running; 128 local_irq_save(flags);
127 129 cur = current_req;
128/* debug level 10 required for ADB logging (should be && debug_adb, ideally) */ 130 while (cur) {
131 if (cur == req) {
132 local_irq_restore(flags);
133 return 1;
134 }
135 cur = cur->next;
136 }
137 local_irq_restore(flags);
138 return 0;
139}
129 140
130/* Check for MacII style ADB */ 141/* Check for MacII style ADB */
131static int macii_probe(void) 142static int macii_probe(void)
@@ -147,15 +158,16 @@ int macii_init(void)
147 local_irq_save(flags); 158 local_irq_save(flags);
148 159
149 err = macii_init_via(); 160 err = macii_init_via();
150 if (err) return err; 161 if (err) goto out;
151 162
152 err = request_irq(IRQ_MAC_ADB, macii_interrupt, IRQ_FLG_LOCK, "ADB", 163 err = request_irq(IRQ_MAC_ADB, macii_interrupt, IRQ_FLG_LOCK, "ADB",
153 macii_interrupt); 164 macii_interrupt);
154 if (err) return err; 165 if (err) goto out;
155 166
156 macii_state = idle; 167 macii_state = idle;
168out:
157 local_irq_restore(flags); 169 local_irq_restore(flags);
158 return 0; 170 return err;
159} 171}
160 172
161/* initialize the hardware */ 173/* initialize the hardware */
@@ -163,12 +175,12 @@ static int macii_init_via(void)
163{ 175{
164 unsigned char x; 176 unsigned char x;
165 177
166 /* Set the lines up. We want TREQ as input TACK|TIP as output */ 178 /* We want CTLR_IRQ as input and ST_EVEN | ST_ODD as output lines. */
167 via[DIRB] = (via[DIRB] | TACK | TIP) & ~TREQ; 179 via[DIRB] = (via[DIRB] | ST_EVEN | ST_ODD) & ~CTLR_IRQ;
168 180
169 /* Set up state: idle */ 181 /* Set up state: idle */
170 via[B] |= ST_IDLE; 182 via[B] |= ST_IDLE;
171 last_status = via[B] & (ST_MASK|TREQ); 183 last_status = via[B] & (ST_MASK|CTLR_IRQ);
172 184
173 /* Shift register on input */ 185 /* Shift register on input */
174 via[ACR] = (via[ACR] & ~SR_CTRL) | SR_EXT; 186 via[ACR] = (via[ACR] & ~SR_CTRL) | SR_EXT;
@@ -179,81 +191,72 @@ static int macii_init_via(void)
179 return 0; 191 return 0;
180} 192}
181 193
182/* Send an ADB poll (Talk Register 0 command, tagged on the front of the request queue) */ 194/* Send an ADB poll (Talk Register 0 command prepended to the request queue) */
183static void macii_queue_poll(void) 195static void macii_queue_poll(void)
184{ 196{
185 static int device = 0; 197 /* No point polling the active device as it will never assert SRQ, so
186 static int in_poll=0; 198 * poll the next device in the autopoll list. This could leave us
199 * stuck in a polling loop if an unprobed device is asserting SRQ.
200 * In theory, that could only happen if a device was plugged in after
201 * probing started. Unplugging it again will break the cycle.
202 * (Simply polling the next higher device often ends up polling almost
203 * every device (after wrapping around), which takes too long.)
204 */
205 int device_mask;
206 int next_device;
187 static struct adb_request req; 207 static struct adb_request req;
188 unsigned long flags;
189
190 if (in_poll) printk("macii_queue_poll: double poll!\n");
191
192 in_poll++;
193 if (++device > 15) device = 1;
194
195 adb_request(&req, NULL, ADBREQ_REPLY|ADBREQ_NOSEND, 1,
196 ADB_READREG(device, 0));
197
198 local_irq_save(flags);
199
200 req.next = current_req;
201 current_req = &req;
202 208
203 local_irq_restore(flags); 209 if (!autopoll_devs) return;
204 macii_start();
205 in_poll--;
206}
207 210
208/* Send an ADB retransmit (Talk, appended to the request queue) */ 211 device_mask = (1 << (((command_byte & 0xF0) >> 4) + 1)) - 1;
209static void macii_retransmit(int device) 212 if (autopoll_devs & ~device_mask)
210{ 213 next_device = ffs(autopoll_devs & ~device_mask) - 1;
211 static int in_retransmit = 0; 214 else
212 static struct adb_request rt; 215 next_device = ffs(autopoll_devs) - 1;
213 unsigned long flags;
214
215 if (in_retransmit) printk("macii_retransmit: double retransmit!\n");
216 216
217 in_retransmit++; 217 BUG_ON(request_is_queued(&req));
218 218
219 adb_request(&rt, NULL, ADBREQ_REPLY|ADBREQ_NOSEND, 1, 219 adb_request(&req, NULL, ADBREQ_NOSEND, 1,
220 ADB_READREG(device, 0)); 220 ADB_READREG(next_device, 0));
221 221
222 local_irq_save(flags); 222 req.sent = 0;
223 req.complete = 0;
224 req.reply_len = 0;
225 req.next = current_req;
223 226
224 if (current_req != NULL) { 227 if (current_req != NULL) {
225 last_req->next = &rt; 228 current_req = &req;
226 last_req = &rt;
227 } else { 229 } else {
228 current_req = &rt; 230 current_req = &req;
229 last_req = &rt; 231 last_req = &req;
230 } 232 }
231
232 if (macii_state == idle) macii_start();
233
234 local_irq_restore(flags);
235 in_retransmit--;
236} 233}
237 234
238/* Send an ADB request; if sync, poll out the reply 'till it's done */ 235/* Send an ADB request; if sync, poll out the reply 'till it's done */
239static int macii_send_request(struct adb_request *req, int sync) 236static int macii_send_request(struct adb_request *req, int sync)
240{ 237{
241 int i; 238 int err;
239 unsigned long flags;
242 240
243 i = macii_write(req); 241 BUG_ON(request_is_queued(req));
244 if (i) return i;
245 242
246 if (sync) { 243 local_irq_save(flags);
247 while (!req->complete) macii_poll(); 244 err = macii_write(req);
245 local_irq_restore(flags);
246
247 if (!err && sync) {
248 while (!req->complete) {
249 macii_poll();
250 }
251 BUG_ON(request_is_queued(req));
248 } 252 }
249 return 0; 253
254 return err;
250} 255}
251 256
252/* Send an ADB request */ 257/* Send an ADB request (append to request queue) */
253static int macii_write(struct adb_request *req) 258static int macii_write(struct adb_request *req)
254{ 259{
255 unsigned long flags;
256
257 if (req->nbytes < 2 || req->data[0] != ADB_PACKET || req->nbytes > 15) { 260 if (req->nbytes < 2 || req->data[0] != ADB_PACKET || req->nbytes > 15) {
258 req->complete = 1; 261 req->complete = 1;
259 return -EINVAL; 262 return -EINVAL;
@@ -264,8 +267,6 @@ static int macii_write(struct adb_request *req)
264 req->complete = 0; 267 req->complete = 0;
265 req->reply_len = 0; 268 req->reply_len = 0;
266 269
267 local_irq_save(flags);
268
269 if (current_req != NULL) { 270 if (current_req != NULL) {
270 last_req->next = req; 271 last_req->next = req;
271 last_req = req; 272 last_req = req;
@@ -274,28 +275,52 @@ static int macii_write(struct adb_request *req)
274 last_req = req; 275 last_req = req;
275 if (macii_state == idle) macii_start(); 276 if (macii_state == idle) macii_start();
276 } 277 }
277
278 local_irq_restore(flags);
279 return 0; 278 return 0;
280} 279}
281 280
282/* Start auto-polling */ 281/* Start auto-polling */
283static int macii_autopoll(int devs) 282static int macii_autopoll(int devs)
284{ 283{
285 /* Just ping a random default address */ 284 static struct adb_request req;
286 if (!(current_req || retry_req)) 285 unsigned long flags;
287 macii_retransmit( (last_active < 16 && last_active > 0) ? last_active : 3); 286 int err = 0;
288 return 0; 287
288 /* bit 1 == device 1, and so on. */
289 autopoll_devs = devs & 0xFFFE;
290
291 if (!autopoll_devs) return 0;
292
293 local_irq_save(flags);
294
295 if (current_req == NULL) {
296 /* Send a Talk Reg 0. The controller will repeatedly transmit
297 * this as long as it is idle.
298 */
299 adb_request(&req, NULL, ADBREQ_NOSEND, 1,
300 ADB_READREG(ffs(autopoll_devs) - 1, 0));
301 err = macii_write(&req);
302 }
303
304 local_irq_restore(flags);
305 return err;
306}
307
308static inline int need_autopoll(void) {
309 /* Was the last command Talk Reg 0
310 * and is the target on the autopoll list?
311 */
312 if ((command_byte & 0x0F) == 0x0C &&
313 ((1 << ((command_byte & 0xF0) >> 4)) & autopoll_devs))
314 return 0;
315 return 1;
289} 316}
290 317
291/* Prod the chip without interrupts */ 318/* Prod the chip without interrupts */
292static void macii_poll(void) 319static void macii_poll(void)
293{ 320{
294 unsigned long flags; 321 disable_irq(IRQ_MAC_ADB);
295 322 macii_interrupt(0, NULL);
296 local_irq_save(flags); 323 enable_irq(IRQ_MAC_ADB);
297 if (via[IFR] & SR_INT) macii_interrupt(0, NULL);
298 local_irq_restore(flags);
299} 324}
300 325
301/* Reset the bus */ 326/* Reset the bus */
@@ -303,73 +328,34 @@ static int macii_reset_bus(void)
303{ 328{
304 static struct adb_request req; 329 static struct adb_request req;
305 330
331 if (request_is_queued(&req))
332 return 0;
333
306 /* Command = 0, Address = ignored */ 334 /* Command = 0, Address = ignored */
307 adb_request(&req, NULL, 0, 1, ADB_BUSRESET); 335 adb_request(&req, NULL, 0, 1, ADB_BUSRESET);
308 336
337 /* Don't want any more requests during the Global Reset low time. */
338 udelay(3000);
339
309 return 0; 340 return 0;
310} 341}
311 342
312/* Start sending ADB packet */ 343/* Start sending ADB packet */
313static void macii_start(void) 344static void macii_start(void)
314{ 345{
315 unsigned long flags;
316 struct adb_request *req; 346 struct adb_request *req;
317 347
318 req = current_req; 348 req = current_req;
319 if (!req) return;
320
321 /* assert macii_state == idle */
322 if (macii_state != idle) {
323 printk("macii_start: called while driver busy (%p %x %x)!\n",
324 req, macii_state, (uint) via1[B] & (ST_MASK|TREQ));
325 return;
326 }
327 349
328 local_irq_save(flags); 350 BUG_ON(req == NULL);
329 351
330 /* 352 BUG_ON(macii_state != idle);
331 * IRQ signaled ?? (means ADB controller wants to send, or might 353
332 * be end of packet if we were reading) 354 /* Now send it. Be careful though, that first byte of the request
333 */ 355 * is actually ADB_PACKET; the real data begins at index 1!
334#if 0 /* FIXME: This is broke broke broke, for some reason */ 356 * And req->nbytes is the number of bytes of real data plus one.
335 if ((via[B] & TREQ) == 0) {
336 printk("macii_start: weird poll stuff. huh?\n");
337 /*
338 * FIXME - we need to restart this on a timer
339 * or a collision at boot hangs us.
340 * Never set macii_state to idle here, or macii_start
341 * won't be called again from send_request!
342 * (need to re-check other cases ...)
343 */
344 /*
345 * if the interrupt handler set the need_poll
346 * flag, it's hopefully a SRQ poll or re-Talk
347 * so we try to send here anyway
348 */
349 if (!need_poll) {
350 if (console_loglevel == 10)
351 printk("macii_start: device busy - retry %p state %d status %x!\n",
352 req, macii_state,
353 (uint) via[B] & (ST_MASK|TREQ));
354 retry_req = req;
355 /* set ADB status here ? */
356 local_irq_restore(flags);
357 return;
358 } else {
359 need_poll = 0;
360 }
361 }
362#endif
363 /*
364 * Another retry pending? (sanity check)
365 */ 357 */
366 if (retry_req) {
367 retry_req = NULL;
368 }
369 358
370 /* Now send it. Be careful though, that first byte of the request */
371 /* is actually ADB_PACKET; the real data begins at index 1! */
372
373 /* store command byte */ 359 /* store command byte */
374 command_byte = req->data[1]; 360 command_byte = req->data[1];
375 /* Output mode */ 361 /* Output mode */
@@ -381,115 +367,97 @@ static void macii_start(void)
381 367
382 macii_state = sending; 368 macii_state = sending;
383 data_index = 2; 369 data_index = 2;
384
385 local_irq_restore(flags);
386} 370}
387 371
388/* 372/*
389 * The notorious ADB interrupt handler - does all of the protocol handling, 373 * The notorious ADB interrupt handler - does all of the protocol handling.
390 * except for starting new send operations. Relies heavily on the ADB 374 * Relies on the ADB controller sending and receiving data, thereby
391 * controller sending and receiving data, thereby generating SR interrupts 375 * generating shift register interrupts (SR_INT) for us. This means there has
392 * for us. This means there has to be always activity on the ADB bus, otherwise 376 * to be activity on the ADB bus. The chip will poll to achieve this.
393 * the whole process dies and has to be re-kicked by sending TALK requests ...
394 * CUDA-based Macs seem to solve this with the autopoll option, for MacII-type
395 * ADB the problem isn't solved yet (retransmit of the latest active TALK seems
396 * a good choice; either on timeout or on a timer interrupt).
397 * 377 *
398 * The basic ADB state machine was left unchanged from the original MacII code 378 * The basic ADB state machine was left unchanged from the original MacII code
399 * by Alan Cox, which was based on the CUDA driver for PowerMac. 379 * by Alan Cox, which was based on the CUDA driver for PowerMac.
400 * The syntax of the ADB status lines seems to be totally different on MacII, 380 * The syntax of the ADB status lines is totally different on MacII,
401 * though. MacII uses the states Command -> Even -> Odd -> Even ->...-> Idle for 381 * though. MacII uses the states Command -> Even -> Odd -> Even ->...-> Idle
402 * sending, and Idle -> Even -> Odd -> Even ->...-> Idle for receiving. Start 382 * for sending and Idle -> Even -> Odd -> Even ->...-> Idle for receiving.
403 * and end of a receive packet are signaled by asserting /IRQ on the interrupt 383 * Start and end of a receive packet are signalled by asserting /IRQ on the
404 * line. Timeouts are signaled by a sequence of 4 0xFF, with /IRQ asserted on 384 * interrupt line (/IRQ means the CTLR_IRQ bit in port B; not to be confused
405 * every other byte. SRQ is probably signaled by 3 or more 0xFF tacked on the 385 * with the VIA shift register interrupt. /IRQ never actually interrupts the
406 * end of a packet. (Thanks to Guido Koerber for eavesdropping on the ADB 386 * processor, it's just an ordinary input.)
407 * protocol with a logic analyzer!!)
408 *
409 * Note: As of 21/10/97, the MacII ADB part works including timeout detection
410 * and retransmit (Talk to the last active device).
411 */ 387 */
412static irqreturn_t macii_interrupt(int irq, void *arg) 388static irqreturn_t macii_interrupt(int irq, void *arg)
413{ 389{
414 int x, adbdir; 390 int x;
415 unsigned long flags; 391 static int entered;
416 struct adb_request *req; 392 struct adb_request *req;
417 393
418 last_status = status; 394 if (!arg) {
419 395 /* Clear the SR IRQ flag when polling. */
420 /* prevent races due to SCSI enabling ints */ 396 if (via[IFR] & SR_INT)
421 local_irq_save(flags); 397 via[IFR] = SR_INT;
422 398 else
423 if (driver_running) { 399 return IRQ_NONE;
424 local_irq_restore(flags);
425 return IRQ_NONE;
426 } 400 }
427 401
428 driver_running = 1; 402 BUG_ON(entered++);
429 403
430 status = via[B] & (ST_MASK|TREQ); 404 last_status = status;
431 adbdir = via[ACR] & SR_OUT; 405 status = via[B] & (ST_MASK|CTLR_IRQ);
432 406
433 switch (macii_state) { 407 switch (macii_state) {
434 case idle: 408 case idle:
409 if (reading_reply) {
410 reply_ptr = current_req->reply;
411 } else {
412 BUG_ON(current_req != NULL);
413 reply_ptr = reply_buf;
414 }
415
435 x = via[SR]; 416 x = via[SR];
436 first_byte = x;
437 /* set ADB state = even for first data byte */
438 via[B] = (via[B] & ~ST_MASK) | ST_EVEN;
439 417
440 reply_buf[0] = first_byte; /* was command_byte?? */ 418 if ((status & CTLR_IRQ) && (x == 0xFF)) {
441 reply_ptr = reply_buf + 1; 419 /* Bus timeout without SRQ sequence:
442 reply_len = 1; 420 * data is "FF" while CTLR_IRQ is "H"
443 prefix_len = 1; 421 */
444 reading_reply = 0; 422 reply_len = 0;
445 423 srq_asserted = 0;
446 macii_state = reading; 424 macii_state = read_done;
447 break; 425 } else {
426 macii_state = reading;
427 *reply_ptr = x;
428 reply_len = 1;
429 }
448 430
449 case awaiting_reply:
450 /* handshake etc. for II ?? */
451 x = via[SR];
452 first_byte = x;
453 /* set ADB state = even for first data byte */ 431 /* set ADB state = even for first data byte */
454 via[B] = (via[B] & ~ST_MASK) | ST_EVEN; 432 via[B] = (via[B] & ~ST_MASK) | ST_EVEN;
455
456 current_req->reply[0] = first_byte;
457 reply_ptr = current_req->reply + 1;
458 reply_len = 1;
459 prefix_len = 1;
460 reading_reply = 1;
461
462 macii_state = reading;
463 break; 433 break;
464 434
465 case sending: 435 case sending:
466 req = current_req; 436 req = current_req;
467 if (data_index >= req->nbytes) { 437 if (data_index >= req->nbytes) {
468 /* print an error message if a listen command has no data */
469 if (((command_byte & 0x0C) == 0x08)
470 /* && (console_loglevel == 10) */
471 && (data_index == 2))
472 printk("MacII ADB: listen command with no data: %x!\n",
473 command_byte);
474 /* reset to shift in */
475 via[ACR] &= ~SR_OUT;
476 x = via[SR];
477 /* set ADB state idle - might get SRQ */
478 via[B] = (via[B] & ~ST_MASK) | ST_IDLE;
479
480 req->sent = 1; 438 req->sent = 1;
439 macii_state = idle;
481 440
482 if (req->reply_expected) { 441 if (req->reply_expected) {
483 macii_state = awaiting_reply; 442 reading_reply = 1;
484 } else { 443 } else {
485 req->complete = 1; 444 req->complete = 1;
486 current_req = req->next; 445 current_req = req->next;
487 if (req->done) (*req->done)(req); 446 if (req->done) (*req->done)(req);
488 macii_state = idle; 447
489 if (current_req || retry_req) 448 if (current_req)
490 macii_start(); 449 macii_start();
491 else 450 else
492 macii_retransmit((command_byte & 0xF0) >> 4); 451 if (need_autopoll())
452 macii_autopoll(autopoll_devs);
453 }
454
455 if (macii_state == idle) {
456 /* reset to shift in */
457 via[ACR] &= ~SR_OUT;
458 x = via[SR];
459 /* set ADB state idle - might get SRQ */
460 via[B] = (via[B] & ~ST_MASK) | ST_IDLE;
493 } 461 }
494 } else { 462 } else {
495 via[SR] = req->data[data_index++]; 463 via[SR] = req->data[data_index++];
@@ -505,147 +473,79 @@ static irqreturn_t macii_interrupt(int irq, void *arg)
505 break; 473 break;
506 474
507 case reading: 475 case reading:
476 x = via[SR];
477 BUG_ON((status & ST_MASK) == ST_CMD ||
478 (status & ST_MASK) == ST_IDLE);
479
480 /* Bus timeout with SRQ sequence:
481 * data is "XX FF" while CTLR_IRQ is "L L"
482 * End of packet without SRQ sequence:
483 * data is "XX...YY 00" while CTLR_IRQ is "L...H L"
484 * End of packet SRQ sequence:
485 * data is "XX...YY 00" while CTLR_IRQ is "L...L L"
486 * (where XX is the first response byte and
487 * YY is the last byte of valid response data.)
488 */
508 489
509 /* timeout / SRQ handling for II hw */ 490 srq_asserted = 0;
510 if( (first_byte == 0xFF && (reply_len-prefix_len)==2 491 if (!(status & CTLR_IRQ)) {
511 && memcmp(reply_ptr-2,"\xFF\xFF",2)==0) || 492 if (x == 0xFF) {
512 ((reply_len-prefix_len)==3 493 if (!(last_status & CTLR_IRQ)) {
513 && memcmp(reply_ptr-3,"\xFF\xFF\xFF",3)==0)) 494 macii_state = read_done;
514 { 495 reply_len = 0;
515 /* 496 srq_asserted = 1;
516 * possible timeout (in fact, most probably a 497 }
517 * timeout, since SRQ can't be signaled without 498 } else if (x == 0x00) {
518 * transfer on the bus). 499 macii_state = read_done;
519 * The last three bytes seen were FF, together 500 if (!(last_status & CTLR_IRQ))
520 * with the starting byte (in case we started 501 srq_asserted = 1;
521 * on 'idle' or 'awaiting_reply') this probably
522 * makes four. So this is mostl likely #5!
523 * The timeout signal is a pattern 1 0 1 0 0..
524 * on /INT, meaning we missed it :-(
525 */
526 x = via[SR];
527 if (x != 0xFF) printk("MacII ADB: mistaken timeout/SRQ!\n");
528
529 if ((status & TREQ) == (last_status & TREQ)) {
530 /* Not a timeout. Unsolicited SRQ? weird. */
531 /* Terminate the SRQ packet and poll */
532 need_poll = 1;
533 } 502 }
534 /* There's no packet to get, so reply is blank */ 503 }
535 via[B] ^= ST_MASK; 504
536 reply_ptr -= (reply_len-prefix_len); 505 if (macii_state == reading) {
537 reply_len = prefix_len; 506 BUG_ON(reply_len > 15);
538 macii_state = read_done;
539 break;
540 } /* end timeout / SRQ handling for II hw. */
541
542 if((reply_len-prefix_len)>3
543 && memcmp(reply_ptr-3,"\xFF\xFF\xFF",3)==0)
544 {
545 /* SRQ tacked on data packet */
546 /* Terminate the packet (SRQ never ends) */
547 x = via[SR];
548 macii_state = read_done;
549 reply_len -= 3;
550 reply_ptr -= 3;
551 need_poll = 1;
552 /* need to continue; next byte not seen else */
553 } else {
554 /* Sanity check */
555 if (reply_len > 15) reply_len = 0;
556 /* read byte */
557 x = via[SR];
558 *reply_ptr = x;
559 reply_ptr++; 507 reply_ptr++;
508 *reply_ptr = x;
560 reply_len++; 509 reply_len++;
561 } 510 }
562 /* The usual handshake ... */
563
564 /*
565 * NetBSD hints that the next to last byte
566 * is sent with IRQ !!
567 * Guido found out it's the last one (0x0),
568 * but IRQ should be asserted already.
569 * Problem with timeout detection: First
570 * transition to /IRQ might be second
571 * byte of timeout packet!
572 * Timeouts are signaled by 4x FF.
573 */
574 if (((status & TREQ) == 0) && (x == 0x00)) { /* != 0xFF */
575 /* invert state bits, toggle ODD/EVEN */
576 via[B] ^= ST_MASK;
577 511
578 /* adjust packet length */ 512 /* invert state bits, toggle ODD/EVEN */
579 reply_len--; 513 via[B] ^= ST_MASK;
580 reply_ptr--;
581 macii_state = read_done;
582 } else {
583 /* not caught: ST_CMD */
584 /* required for re-entry 'reading'! */
585 if ((status & ST_MASK) == ST_IDLE) {
586 /* (in)sanity check - set even */
587 via[B] = (via[B] & ~ST_MASK) | ST_EVEN;
588 } else {
589 /* invert state bits */
590 via[B] ^= ST_MASK;
591 }
592 }
593 break; 514 break;
594 515
595 case read_done: 516 case read_done:
596 x = via[SR]; 517 x = via[SR];
518
597 if (reading_reply) { 519 if (reading_reply) {
520 reading_reply = 0;
598 req = current_req; 521 req = current_req;
599 req->reply_len = reply_ptr - req->reply; 522 req->reply_len = reply_len;
600 req->complete = 1; 523 req->complete = 1;
601 current_req = req->next; 524 current_req = req->next;
602 if (req->done) (*req->done)(req); 525 if (req->done) (*req->done)(req);
603 } else { 526 } else if (reply_len && autopoll_devs)
604 adb_input(reply_buf, reply_ptr - reply_buf, 0); 527 adb_input(reply_buf, reply_len, 0);
605 }
606 528
607 /* 529 macii_state = idle;
608 * remember this device ID; it's the latest we got a
609 * reply from!
610 */
611 last_reply = command_byte;
612 last_active = (command_byte & 0xF0) >> 4;
613 530
614 /* SRQ seen before, initiate poll now */ 531 /* SRQ seen before, initiate poll now */
615 if (need_poll) { 532 if (srq_asserted)
616 macii_state = idle;
617 macii_queue_poll(); 533 macii_queue_poll();
618 need_poll = 0;
619 break;
620 }
621
622 /* set ADB state to idle */
623 via[B] = (via[B] & ~ST_MASK) | ST_IDLE;
624
625 /* /IRQ seen, so the ADB controller has data for us */
626 if ((via[B] & TREQ) != 0) {
627 macii_state = reading;
628 534
629 reply_buf[0] = command_byte; 535 if (current_req)
630 reply_ptr = reply_buf + 1; 536 macii_start();
631 reply_len = 1; 537 else
632 prefix_len = 1; 538 if (need_autopoll())
633 reading_reply = 0; 539 macii_autopoll(autopoll_devs);
634 } else { 540
635 /* no IRQ, send next packet or wait */ 541 if (macii_state == idle)
636 macii_state = idle; 542 via[B] = (via[B] & ~ST_MASK) | ST_IDLE;
637 if (current_req)
638 macii_start();
639 else
640 macii_retransmit(last_active);
641 }
642 break; 543 break;
643 544
644 default: 545 default:
645 break; 546 break;
646 } 547 }
647 /* reset mutex and interrupts */ 548
648 driver_running = 0; 549 entered--;
649 local_irq_restore(flags);
650 return IRQ_HANDLED; 550 return IRQ_HANDLED;
651} 551}