git: 1fc756504f7c - stable/12 - twsi: do not attempt transfer if bus is not idle

From: Andriy Gapon <avg_at_FreeBSD.org>
Date: Fri, 10 Dec 2021 12:31:26 UTC
The branch stable/12 has been updated by avg:

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

commit 1fc756504f7cc017519cdb23383221ffc3d0c580
Author:     Andriy Gapon <avg@FreeBSD.org>
AuthorDate: 2021-11-26 07:30:38 +0000
Commit:     Andriy Gapon <avg@FreeBSD.org>
CommitDate: 2021-12-10 12:30:50 +0000

    twsi: do not attempt transfer if bus is not idle
    
    (cherry picked from commit cb167e1ae95473d836393a07456d8dbcfbb7f758)
---
 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;