Module Name:    src
Committed By:   thorpej
Date:           Tue Jun 12 13:18:48 UTC 2018

Modified Files:
        src/sys/dev/i2c: gttwsi_core.c

Log Message:
When initiating a transfer, if a device isn't present, we won't
get an ACK after sending the address.  Check for this alternate
state and suppress the error message when it occurs.

Fixes PR kern/53356.


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 src/sys/dev/i2c/gttwsi_core.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/i2c/gttwsi_core.c
diff -u src/sys/dev/i2c/gttwsi_core.c:1.5 src/sys/dev/i2c/gttwsi_core.c:1.6
--- src/sys/dev/i2c/gttwsi_core.c:1.5	Mon May  7 15:03:19 2018
+++ src/sys/dev/i2c/gttwsi_core.c	Tue Jun 12 13:18:48 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: gttwsi_core.c,v 1.5 2018/05/07 15:03:19 jmcneill Exp $	*/
+/*	$NetBSD: gttwsi_core.c,v 1.6 2018/06/12 13:18:48 thorpej Exp $	*/
 /*
  * Copyright (c) 2008 Eiji Kawauchi.
  * All rights reserved.
@@ -66,7 +66,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: gttwsi_core.c,v 1.5 2018/05/07 15:03:19 jmcneill Exp $");
+__KERNEL_RCSID(0, "$NetBSD: gttwsi_core.c,v 1.6 2018/06/12 13:18:48 thorpej Exp $");
 #include "locators.h"
 
 #include <sys/param.h>
@@ -91,7 +91,8 @@ static int	gttwsi_initiate_xfer(void *v,
 static int	gttwsi_read_byte(void *v, uint8_t *valp, int flags);
 static int	gttwsi_write_byte(void *v, uint8_t val, int flags);
 
-static int	gttwsi_wait(struct gttwsi_softc *, uint32_t, uint32_t, int);
+static int	gttwsi_wait(struct gttwsi_softc *, uint32_t, uint32_t,
+			    uint32_t, int);
 
 static inline uint32_t
 gttwsi_default_read_4(struct gttwsi_softc *sc, uint32_t reg)
@@ -243,7 +244,7 @@ gttwsi_send_start(void *v, int flags)
 	else
 		expect = STAT_SCT;
 	sc->sc_started = true;
-	return gttwsi_wait(sc, CONTROL_START, expect, flags);
+	return gttwsi_wait(sc, CONTROL_START, expect, 0, flags);
 }
 
 static int
@@ -277,7 +278,7 @@ static int
 gttwsi_initiate_xfer(void *v, i2c_addr_t addr, int flags)
 {
 	struct gttwsi_softc *sc = v;
-	uint32_t data, expect;
+	uint32_t data, expect, alt;
 	int error, read;
 
 	KASSERT(sc->sc_inuse);
@@ -285,10 +286,13 @@ gttwsi_initiate_xfer(void *v, i2c_addr_t
 	gttwsi_send_start(v, flags);
 
 	read = (flags & I2C_F_READ) != 0;
-	if (read)
+	if (read) {
 		expect = STAT_ARBT_AR;
-	else
+		alt    = STAT_ARBT_ANR;
+	} else {
 		expect = STAT_AWBT_AR;
+		alt    = STAT_AWBT_ANR;
+	}
 
 	/*
 	 * First byte contains whether this xfer is a read or write.
@@ -301,23 +305,26 @@ gttwsi_initiate_xfer(void *v, i2c_addr_t
 		 */
 		data |= 0xf0 | ((addr & 0x300) >> 7);
 		gttwsi_write_4(sc, TWSI_DATA, data);
-		error = gttwsi_wait(sc, 0, expect, flags);
+		error = gttwsi_wait(sc, 0, expect, alt, flags);
 		if (error)
 			return error;
 		/*
 		 * The first address byte has been sent, now to send
 		 * the second one.
 		 */
-		if (read)
+		if (read) {
 			expect = STAT_SARBT_AR;
-		else
+			alt    = STAT_SARBT_ANR;
+		} else {
 			expect = STAT_SAWBT_AR;
+			alt    = STAT_SAWBT_ANR;
+		}
 		data = (uint8_t)addr;
 	} else
 		data |= (addr << 1);
 
 	gttwsi_write_4(sc, TWSI_DATA, data);
-	return gttwsi_wait(sc, 0, expect, flags);
+	return gttwsi_wait(sc, 0, expect, alt, flags);
 }
 
 static int
@@ -329,9 +336,9 @@ gttwsi_read_byte(void *v, uint8_t *valp,
 	KASSERT(sc->sc_inuse);
 
 	if (flags & I2C_F_LAST)
-		error = gttwsi_wait(sc, 0, STAT_MRRD_ANT, flags);
+		error = gttwsi_wait(sc, 0, STAT_MRRD_ANT, 0, flags);
 	else
-		error = gttwsi_wait(sc, CONTROL_ACK, STAT_MRRD_AT, flags);
+		error = gttwsi_wait(sc, CONTROL_ACK, STAT_MRRD_AT, 0, flags);
 	if (!error)
 		*valp = gttwsi_read_4(sc, TWSI_DATA);
 	if ((flags & (I2C_F_LAST | I2C_F_STOP)) == (I2C_F_LAST | I2C_F_STOP))
@@ -348,7 +355,7 @@ gttwsi_write_byte(void *v, uint8_t val, 
 	KASSERT(sc->sc_inuse);
 
 	gttwsi_write_4(sc, TWSI_DATA, val);
-	error = gttwsi_wait(sc, 0, STAT_MTDB_AR, flags);
+	error = gttwsi_wait(sc, 0, STAT_MTDB_AR, 0, flags);
 	if (flags & I2C_F_STOP)
 		gttwsi_send_stop(sc, flags);
 	return error;
@@ -356,7 +363,7 @@ gttwsi_write_byte(void *v, uint8_t val, 
 
 static int
 gttwsi_wait(struct gttwsi_softc *sc, uint32_t control, uint32_t expect,
-	    int flags)
+	    uint32_t alt, int flags)
 {
 	uint32_t status;
 	int timo, error = 0;
@@ -389,8 +396,16 @@ gttwsi_wait(struct gttwsi_softc *sc, uin
 
 	status = gttwsi_read_4(sc, TWSI_STATUS);
 	if (status != expect) {
-		aprint_error_dev(sc->sc_dev,
-		    "unexpected status 0x%x: expect 0x%x\n", status, expect);
+		/*
+		 * In the case of probing for a device, we are expecting
+		 * 2 different status codes: the ACK case (device exists),
+		 * or the NACK case (device does not exist).  We don't
+		 * need to report an error in the later case.
+		 */
+		if (alt != 0 && status != alt)
+			aprint_error_dev(sc->sc_dev,
+			    "unexpected status 0x%x: expect 0x%x\n", status,
+			    expect);
 		return EIO;
 	}
 	return error;

Reply via email to