git: cb167e1ae954 - main - twsi: do not attempt transfer if bus is not idle

From: Andriy Gapon <avg_at_FreeBSD.org>
Date: Fri, 26 Nov 2021 14:23:59 UTC
The branch main has been updated by avg:

URL: https://cgit.FreeBSD.org/src/commit/?id=cb167e1ae95473d836393a07456d8dbcfbb7f758

commit cb167e1ae95473d836393a07456d8dbcfbb7f758
Author:     Andriy Gapon <avg@FreeBSD.org>
AuthorDate: 2021-11-26 07:30:38 +0000
Commit:     Andriy Gapon <avg@FreeBSD.org>
CommitDate: 2021-11-26 14:15:43 +0000

    twsi: do not attempt transfer if bus is not idle
    
    MFC after:      2 weeks
---
 sys/dev/iicbus/twsi/twsi.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/sys/dev/iicbus/twsi/twsi.c b/sys/dev/iicbus/twsi/twsi.c
index 05f67786cad0..5e899313ac45 100644
--- a/sys/dev/iicbus/twsi/twsi.c
+++ b/sys/dev/iicbus/twsi/twsi.c
@@ -484,6 +484,7 @@ static int
 twsi_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
 {
 	struct twsi_softc *sc;
+	uint32_t status;
 	int error;
 
 	sc = device_get_softc(dev);
@@ -496,7 +497,15 @@ twsi_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
 	    ("starting a transfer while another is active"));
 
 	debugf(sc, "transmitting %d messages\n", nmsgs);
-	debugf(sc, "status=%x\n", TWSI_READ(sc, sc->reg_status));
+	status = TWSI_READ(sc, sc->reg_status);
+	debugf(sc, "status=0x%x\n", status);
+	if (status != TWSI_STATUS_IDLE) {
+		debugf(sc, "Bad status at start of transfer\n");
+		TWSI_WRITE(sc, sc->reg_control, TWSI_CONTROL_STOP);
+		mtx_unlock(&sc->mutex);
+		return (IIC_ESTATUS);
+	}
+
 	sc->nmsgs = nmsgs;
 	sc->msgs = msgs;
 	sc->msg_idx = 0;