git: 5e62bf340337 - main - twsi: Do not clear the interrupt flag when changing the control bits

From: Emmanuel Vadot <manu_at_FreeBSD.org>
Date: Tue, 10 Nov 2026 13:42:50 UTC
The branch main has been updated by manu:

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

commit 5e62bf3403379c92cf42fc33bb462b6fb7e54425
Author:     Emmanuel Vadot <manu@FreeBSD.org>
AuthorDate: 2026-11-10 09:09:07 +0000
Commit:     Emmanuel Vadot <manu@FreeBSD.org>
CommitDate: 2026-11-10 13:42:44 +0000

    twsi: Do not clear the interrupt flag when changing the control bits
    
    twsi_control_set() and twsi_control_clear() read the control register,
    change some bits and write it back.  On the controllers where the
    interrupt flag is cleared by writing a 1 to it (iflag_w1c), this clears
    it as the flag is read as set.
    
    In the polled mode this breaks the repeated start: START is set with
    the flag cleared by the write, the hardware sends the repeated start at
    once, and the flag is then cleared a second time, which loses the event
    and leaves the controller with the status of the previous transfer.
    Transfers with a repeated start fail with IIC_ESTATUS on an Allwinner
    H6 until the controller works with interrupts.
    
    Mask the flag out of the value that is written back, unless the caller
    asks for it.
    
    Tested on: H6
---
 sys/dev/iicbus/controller/twsi/twsi.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/sys/dev/iicbus/controller/twsi/twsi.c b/sys/dev/iicbus/controller/twsi/twsi.c
index 73412e87d61e..570f3f81c37d 100644
--- a/sys/dev/iicbus/controller/twsi/twsi.c
+++ b/sys/dev/iicbus/controller/twsi/twsi.c
@@ -120,6 +120,9 @@ twsi_control_clear(struct twsi_softc *sc, uint32_t mask)
 	val = TWSI_READ(sc, sc->reg_control);
 	debugf(sc, "read val=%x\n", val);
 	val &= ~(TWSI_CONTROL_STOP | TWSI_CONTROL_START);
+	/* Do not clear the interrupt flag by writing it back */
+	if (sc->iflag_w1c)
+		val &= ~TWSI_CONTROL_IFLG;
 	val &= ~mask;
 	debugf(sc, "write val=%x\n", val);
 	TWSI_WRITE(sc, sc->reg_control, val);
@@ -133,6 +136,9 @@ twsi_control_set(struct twsi_softc *sc, uint32_t mask)
 	val = TWSI_READ(sc, sc->reg_control);
 	debugf(sc, "read val=%x\n", val);
 	val &= ~(TWSI_CONTROL_STOP | TWSI_CONTROL_START);
+	/* Do not clear the interrupt flag by writing it back */
+	if (sc->iflag_w1c)
+		val &= ~TWSI_CONTROL_IFLG;
 	val |= mask;
 	debugf(sc, "write val=%x\n", val);
 	TWSI_WRITE(sc, sc->reg_control, val);