@@ -80,6 +80,8 @@ static volatile unsigned char *via;
8080/* ADB command byte structure */
8181#define ADDR_MASK 0xF0
8282#define CMD_MASK 0x0F
83+ #define OP_MASK 0x0C
84+ #define TALK 0x0C
8385
8486static int macii_init_via (void );
8587static void macii_start (void );
@@ -119,9 +121,10 @@ static int reading_reply; /* store reply in reply_buf else req->reply */
119121static int data_index ; /* index of the next byte to send from req->data */
120122static int reply_len ; /* number of bytes received in reply_buf or req->reply */
121123static int status ; /* VIA's ADB status bits captured upon interrupt */
122- static int last_status ; /* status bits as at previous interrupt */
123- static int srq_asserted ; /* have to poll for the device that asserted it */
124+ static bool bus_timeout ; /* no data was sent by the device */
125+ static bool srq_asserted ; /* have to poll for the device that asserted it */
124126static u8 last_cmd ; /* the most recent command byte transmitted */
127+ static u8 last_talk_cmd ; /* the most recent Talk command byte transmitted */
125128static u8 last_poll_cmd ; /* the most recent Talk R0 command byte transmitted */
126129static int autopoll_devs ; /* bits set are device addresses to be polled */
127130
@@ -170,7 +173,6 @@ static int macii_init_via(void)
170173
171174 /* Set up state: idle */
172175 via [B ] |= ST_IDLE ;
173- last_status = via [B ] & (ST_MASK | CTLR_IRQ );
174176
175177 /* Shift register on input */
176178 via [ACR ] = (via [ACR ] & ~SR_CTRL ) | SR_EXT ;
@@ -336,13 +338,6 @@ static void macii_start(void)
336338 * And req->nbytes is the number of bytes of real data plus one.
337339 */
338340
339- /* store command byte */
340- last_cmd = req -> data [1 ];
341-
342- /* If this is a Talk Register 0 command, store the command byte */
343- if ((last_cmd & CMD_MASK ) == ADB_READREG (0 , 0 ))
344- last_poll_cmd = last_cmd ;
345-
346341 /* Output mode */
347342 via [ACR ] |= SR_OUT ;
348343 /* Load data */
@@ -352,6 +347,9 @@ static void macii_start(void)
352347
353348 macii_state = sending ;
354349 data_index = 2 ;
350+
351+ bus_timeout = false;
352+ srq_asserted = false;
355353}
356354
357355/*
@@ -360,15 +358,17 @@ static void macii_start(void)
360358 * generating shift register interrupts (SR_INT) for us. This means there has
361359 * to be activity on the ADB bus. The chip will poll to achieve this.
362360 *
363- * The basic ADB state machine was left unchanged from the original MacII code
364- * by Alan Cox, which was based on the CUDA driver for PowerMac.
365- * The syntax of the ADB status lines is totally different on MacII,
366- * though. MacII uses the states Command -> Even -> Odd -> Even ->...-> Idle
367- * for sending and Idle -> Even -> Odd -> Even ->...-> Idle for receiving.
368- * Start and end of a receive packet are signalled by asserting /IRQ on the
369- * interrupt line (/IRQ means the CTLR_IRQ bit in port B; not to be confused
370- * with the VIA shift register interrupt. /IRQ never actually interrupts the
371- * processor, it's just an ordinary input.)
361+ * The VIA Port B output signalling works as follows. After the ADB transceiver
362+ * sees a transition on the PB4 and PB5 lines it will crank over the VIA shift
363+ * register which eventually raises the SR_INT interrupt. The PB4/PB5 outputs
364+ * are toggled with each byte as the ADB transaction progresses.
365+ *
366+ * Request with no reply expected (and empty transceiver buffer):
367+ * CMD -> IDLE
368+ * Request with expected reply packet (or with buffered autopoll packet):
369+ * CMD -> EVEN -> ODD -> EVEN -> ... -> IDLE
370+ * Unsolicited packet:
371+ * IDLE -> EVEN -> ODD -> EVEN -> ... -> IDLE
372372 */
373373static irqreturn_t macii_interrupt (int irq , void * arg )
374374{
@@ -388,31 +388,31 @@ static irqreturn_t macii_interrupt(int irq, void *arg)
388388 }
389389 }
390390
391- last_status = status ;
392391 status = via [B ] & (ST_MASK | CTLR_IRQ );
393392
394393 switch (macii_state ) {
395394 case idle :
396- if (reading_reply ) {
397- reply_ptr = current_req -> reply ;
398- } else {
399- WARN_ON (current_req );
400- reply_ptr = reply_buf ;
401- }
395+ WARN_ON ((status & ST_MASK ) != ST_IDLE );
396+
397+ reply_ptr = reply_buf ;
398+ reading_reply = 0 ;
399+
400+ bus_timeout = false;
401+ srq_asserted = false;
402402
403403 x = via [SR ];
404404
405- if ((status & CTLR_IRQ ) && ( x == 0xFF )) {
406- /* Bus timeout without SRQ sequence:
407- * data is "FF" while CTLR_IRQ is "H"
405+ if (! (status & CTLR_IRQ )) {
406+ /* /CTLR_IRQ asserted in idle state means we must
407+ * read an autopoll reply from the transceiver buffer.
408408 */
409- reply_len = 0 ;
410- srq_asserted = 0 ;
411- macii_state = read_done ;
412- } else {
413409 macii_state = reading ;
414410 * reply_ptr = x ;
415411 reply_len = 1 ;
412+ } else {
413+ /* bus timeout */
414+ macii_state = read_done ;
415+ reply_len = 0 ;
416416 }
417417
418418 /* set ADB state = even for first data byte */
@@ -421,13 +421,52 @@ static irqreturn_t macii_interrupt(int irq, void *arg)
421421
422422 case sending :
423423 req = current_req ;
424- if (data_index >= req -> nbytes ) {
424+
425+ if (status == (ST_CMD | CTLR_IRQ )) {
426+ /* /CTLR_IRQ de-asserted after the command byte means
427+ * the host can continue with the transaction.
428+ */
429+
430+ /* Store command byte */
431+ last_cmd = req -> data [1 ];
432+ if ((last_cmd & OP_MASK ) == TALK ) {
433+ last_talk_cmd = last_cmd ;
434+ if ((last_cmd & CMD_MASK ) == ADB_READREG (0 , 0 ))
435+ last_poll_cmd = last_cmd ;
436+ }
437+ }
438+
439+ if (status == ST_CMD ) {
440+ /* /CTLR_IRQ asserted after the command byte means we
441+ * must read an autopoll reply. The first byte was
442+ * lost because the shift register was an output.
443+ */
444+ macii_state = reading ;
445+
446+ reading_reply = 0 ;
447+ reply_ptr = reply_buf ;
448+ * reply_ptr = last_talk_cmd ;
449+ reply_len = 1 ;
450+
451+ /* reset to shift in */
452+ via [ACR ] &= ~SR_OUT ;
453+ x = via [SR ];
454+ } else if (data_index >= req -> nbytes ) {
425455 req -> sent = 1 ;
426- macii_state = idle ;
427456
428457 if (req -> reply_expected ) {
458+ macii_state = reading ;
459+
429460 reading_reply = 1 ;
461+ reply_ptr = req -> reply ;
462+ * reply_ptr = req -> data [1 ];
463+ reply_len = 1 ;
464+
465+ via [ACR ] &= ~SR_OUT ;
466+ x = via [SR ];
430467 } else {
468+ macii_state = idle ;
469+
431470 req -> complete = 1 ;
432471 current_req = req -> next ;
433472 if (req -> done )
@@ -438,25 +477,26 @@ static irqreturn_t macii_interrupt(int irq, void *arg)
438477
439478 if (current_req && macii_state == idle )
440479 macii_start ();
441- }
442480
443- if (macii_state == idle ) {
444- /* reset to shift in */
445- via [ACR ] &= ~SR_OUT ;
446- x = via [SR ];
447- /* set ADB state idle - might get SRQ */
448- via [B ] = (via [B ] & ~ST_MASK ) | ST_IDLE ;
481+ if (macii_state == idle ) {
482+ /* reset to shift in */
483+ via [ACR ] &= ~SR_OUT ;
484+ x = via [SR ];
485+ /* set ADB state idle - might get SRQ */
486+ via [B ] = (via [B ] & ~ST_MASK ) | ST_IDLE ;
487+ }
488+ break ;
449489 }
450490 } else {
451491 via [SR ] = req -> data [data_index ++ ];
492+ }
452493
453- if ((via [B ] & ST_MASK ) == ST_CMD ) {
454- /* just sent the command byte, set to EVEN */
455- via [B ] = (via [B ] & ~ST_MASK ) | ST_EVEN ;
456- } else {
457- /* invert state bits, toggle ODD/EVEN */
458- via [B ] ^= ST_MASK ;
459- }
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 ;
460500 }
461501 break ;
462502
@@ -465,28 +505,13 @@ static irqreturn_t macii_interrupt(int irq, void *arg)
465505 WARN_ON ((status & ST_MASK ) == ST_CMD ||
466506 (status & ST_MASK ) == ST_IDLE );
467507
468- /* Bus timeout with SRQ sequence:
469- * data is "XX FF" while CTLR_IRQ is "L L"
470- * End of packet without SRQ sequence:
471- * data is "XX...YY 00" while CTLR_IRQ is "L...H L"
472- * End of packet SRQ sequence:
473- * data is "XX...YY 00" while CTLR_IRQ is "L...L L"
474- * (where XX is the first response byte and
475- * YY is the last byte of valid response data.)
476- */
477-
478- srq_asserted = 0 ;
479508 if (!(status & CTLR_IRQ )) {
480- if (x == 0xFF ) {
481- if (!(last_status & CTLR_IRQ )) {
482- macii_state = read_done ;
483- reply_len = 0 ;
484- srq_asserted = 1 ;
485- }
486- } else if (x == 0x00 ) {
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 {
487514 macii_state = read_done ;
488- if (!(last_status & CTLR_IRQ ))
489- srq_asserted = 1 ;
490515 }
491516 }
492517
@@ -504,6 +529,9 @@ static irqreturn_t macii_interrupt(int irq, void *arg)
504529 case read_done :
505530 x = via [SR ];
506531
532+ if (bus_timeout )
533+ reply_len = 0 ;
534+
507535 if (reading_reply ) {
508536 reading_reply = 0 ;
509537 req = current_req ;
0 commit comments