git: 5daad0ba461e - main - twsi: Wait for the bus to go idle before starting a transfer

From: Emmanuel Vadot <manu_at_FreeBSD.org>
Date: Tue, 17 Nov 2026 07:54:35 UTC
The branch main has been updated by manu:

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

commit 5daad0ba461e063efbee8317a4244497d52cebb0
Author:     Emmanuel Vadot <manu@FreeBSD.org>
AuthorDate: 2026-11-17 07:52:05 +0000
Commit:     Emmanuel Vadot <manu@FreeBSD.org>
CommitDate: 2026-11-17 07:53:59 +0000

    twsi: Wait for the bus to go idle before starting a transfer
    
    The status register does not read as idle (0xf8) right after the
    previous transfer ended and the module was disabled. On fast
    (optimised) kernels the next transfer starts too early, sees a
    different status and fails with EIO ("Bad status at start of
    transfer").
    
    On the Pine H64 this made the AXP805 write that disables the PHY
    supply fail from awg_shutdown(), leaving the PHY powered and the
    board hanging on warm reboot. Poll the status register for up to 1ms
    before declaring the bus not idle.
    
    Tested on:      Pine H64 (5 warm reboots with a NODEBUG kernel)
    Fixes:  cb167e1ae954 ("twsi: do not attempt transfer if bus is not idle")
---
 sys/dev/iicbus/controller/twsi/twsi.c | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/sys/dev/iicbus/controller/twsi/twsi.c b/sys/dev/iicbus/controller/twsi/twsi.c
index 570f3f81c37d..2caf1faa9924 100644
--- a/sys/dev/iicbus/controller/twsi/twsi.c
+++ b/sys/dev/iicbus/controller/twsi/twsi.c
@@ -79,6 +79,7 @@
 #define	TWSI_STATUS_DATA_RD_ACK		0x50
 #define	TWSI_STATUS_DATA_RD_NOACK	0x58
 #define	TWSI_STATUS_IDLE		0xf8
+#define	TWSI_IDLE_RETRY			100
 
 #define	TWSI_DEBUG
 #undef TWSI_DEBUG
@@ -488,6 +489,28 @@ twsi_error(struct twsi_softc *sc, int err)
 	TWSI_WRITE(sc, sc->reg_control, sc->control_val | TWSI_CONTROL_STOP);
 }
 
+/*
+ * The status register takes a short time to go back to the idle value after
+ * the module was disabled at the end of the previous transfer, and it reads
+ * other values in the meantime.  Wait a little before saying that the bus is
+ * not idle.
+ */
+static uint32_t
+twsi_wait_idle(struct twsi_softc *sc)
+{
+	uint32_t status;
+	int retry;
+
+	status = TWSI_READ(sc, sc->reg_status);
+	for (retry = TWSI_IDLE_RETRY;
+	    status != TWSI_STATUS_IDLE && retry > 0; retry--) {
+		DELAY(10);
+		status = TWSI_READ(sc, sc->reg_status);
+	}
+
+	return (status);
+}
+
 static int
 twsi_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
 {
@@ -505,7 +528,7 @@ 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);
-	status = TWSI_READ(sc, sc->reg_status);
+	status = twsi_wait_idle(sc);
 	debugf(sc, "status=0x%x\n", status);
 	if (status != TWSI_STATUS_IDLE) {
 		debugf(sc, "Bad status at start of transfer\n");