]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
i2c: designware: Handle active target cleanly
authorWilliam A. Kennington III <william@wkennington.com>
Wed, 27 May 2026 20:09:52 +0000 (20:09 +0000)
committerAndi Shyti <andi.shyti@kernel.org>
Thu, 28 May 2026 22:39:12 +0000 (00:39 +0200)
When the I2C controller attempts a new transaction while the target
controller is shutting down or restarting, it can lead to bus lockups
and system bootloops if the hardware enters an inconsistent state.

Address this by ensuring that the internal state machines are properly
cleared when disabling the controller if target activity is detected.

If the controller remains active after disabling, perform a bus recovery
to reset it to a known good state.

Signed-off-by: William A. Kennington III <william@wkennington.com>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Acked-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Signed-off-by: Andi Shyti <andi.shyti@kernel.org>
Link: https://lore.kernel.org/r/20260527-dw-i2c-v5-4-3483057f8d67@wkennington.com
drivers/i2c/busses/i2c-designware-common.c
drivers/i2c/busses/i2c-designware-master.c

index d792c65d46ae313d69ec0ab951f7312a4a8407ea..e4dfa2ec58bb76e0f02e6ee9b840a37b0cc74dac 100644 (file)
@@ -633,6 +633,14 @@ void __i2c_dw_disable(struct dw_i2c_dev *dev)
 
        abort_needed = (raw_intr_stats & DW_IC_INTR_MST_ON_HOLD) ||
                        (ic_stats & DW_IC_STATUS_MASTER_HOLD_TX_FIFO_EMPTY);
+
+       /*
+        * If we are in target mode and there is activity, we should also
+        * trigger an abort to clear the internal state machines.
+        */
+       if (dev->mode == DW_IC_SLAVE && (ic_stats & DW_IC_STATUS_SLAVE_ACTIVITY))
+               abort_needed = true;
+
        if (abort_needed) {
                if (!(enable & DW_IC_ENABLE_ENABLE)) {
                        regmap_write(dev->map, DW_IC_ENABLE, DW_IC_ENABLE_ENABLE);
index de929b91d5ead396c2817cbaf0de6d246967061b..7a301c8b604ef448f24a57c278e8e8fb44d5f438 100644 (file)
@@ -785,18 +785,25 @@ __i2c_dw_xfer_one_part(struct dw_i2c_dev *dev, struct i2c_msg *msgs, size_t num)
         * IC_RAW_INTR_STAT.MASTER_ON_HOLD holding SCL low. Check if
         * controller is still ACTIVE before disabling I2C.
         */
-       if (i2c_dw_is_controller_active(dev))
-               dev_err(dev->dev, "controller active\n");
-
-       /*
-        * We must disable the adapter before returning and signaling the end
-        * of the current transfer. Otherwise the hardware might continue
-        * generating interrupts which in turn causes a race condition with
-        * the following transfer. Needs some more investigation if the
-        * additional interrupts are a hardware bug or this driver doesn't
-        * handle them correctly yet.
-        */
-       __i2c_dw_disable_nowait(dev);
+       if (i2c_dw_is_controller_active(dev)) {
+               /*
+                * If the controller is still active after the timeout, attempt a
+                * bus recovery to clear any potentially locked state.
+                */
+               dev_err(dev->dev, "controller active after xfer, recovering\n");
+               i2c_recover_bus(&dev->adapter);
+               i2c_dw_init(dev);
+       } else {
+               /*
+                * We must disable the adapter before returning and signaling the end
+                * of the current transfer. Otherwise the hardware might continue
+                * generating interrupts which in turn causes a race condition with
+                * the following transfer. Needs some more investigation if the
+                * additional interrupts are a hardware bug or this driver doesn't
+                * handle them correctly yet.
+                */
+               __i2c_dw_disable_nowait(dev);
+       }
 
        if (dev->msg_err)
                return dev->msg_err;