The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH v5 0/4] i2c: designware: Improve device disable handling
@ 2026-05-11 20:06 William A. Kennington III
  2026-05-11 20:06 ` [PATCH v5 1/4] i2c: designware: Introduce shutdown exported function William A. Kennington III
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: William A. Kennington III @ 2026-05-11 20:06 UTC (permalink / raw)
  To: Mika Westerberg, Andy Shevchenko, Jan Dabros, Andi Shyti
  Cc: linux-i2c, linux-kernel, William A. Kennington III

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.

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

Additionally, it adds a shutdown hook that gracefully sets the target
disable bit before disabling the controller. This guarantees that any
incoming requests from the controller are immediately NACKed during
shutdown, preventing the bus from hanging.

Signed-off-by: William A. Kennington III <william@wkennington.com>
---
Changes in v5:
- Fixed one last inclusive language mistake
- Link to v4: https://lore.kernel.org/r/20260507-dw-i2c-v4-0-9a447b23bfdf@wkennington.com
Changes in v4:
- Updated language to use inclusive terms
- Refactored some minor comment / code nits
- Link to v3: https://lore.kernel.org/r/20260504-dw-i2c-v3-0-57e56135d602@wkennington.com
Changes in v3:
- Split the monolithic patch into 4 logical patches
- Reverted stray formatting change in the PCI driver's dw_i2c_driver struct.
Changes in v2:
- Fix description footers
- Fix emails

---
William A. Kennington III (4):
      i2c: designware: Introduce shutdown exported function
      i2c: designware: Convert PCI driver to use shutdown hook
      i2c: designware: Convert platform driver to use shutdown hook
      i2c: designware: Handle active target cleanly

 drivers/i2c/busses/i2c-designware-common.c  | 32 +++++++++++++++++++++++++++++
 drivers/i2c/busses/i2c-designware-core.h    |  1 +
 drivers/i2c/busses/i2c-designware-master.c  | 31 +++++++++++++++++-----------
 drivers/i2c/busses/i2c-designware-pcidrv.c  | 14 +++++++++++++
 drivers/i2c/busses/i2c-designware-platdrv.c | 14 +++++++++++++
 5 files changed, 80 insertions(+), 12 deletions(-)
---
base-commit: 5d6919055dec134de3c40167a490f33c74c12581
change-id: 20260504-dw-i2c-d5a1b1a0036a

Best regards,
-- 
William A. Kennington III <william@wkennington.com>


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v5 1/4] i2c: designware: Introduce shutdown exported function
  2026-05-11 20:06 [PATCH v5 0/4] i2c: designware: Improve device disable handling William A. Kennington III
@ 2026-05-11 20:06 ` William A. Kennington III
  2026-05-11 20:06 ` [PATCH v5 2/4] i2c: designware: Convert PCI driver to use shutdown hook William A. Kennington III
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: William A. Kennington III @ 2026-05-11 20:06 UTC (permalink / raw)
  To: Mika Westerberg, Andy Shevchenko, Jan Dabros, Andi Shyti
  Cc: linux-i2c, linux-kernel, William A. Kennington III

Introduce an exported shutdown function to safely shutdown the
DesignWare I2C controller.

This shutdown hook gracefully sets the target disable bit before disabling
the controller. This guarantees that any incoming requests from the
controller are immediately NACKed during shutdown, preventing the bus from
hanging.

Signed-off-by: William A. Kennington III <william@wkennington.com>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/i2c/busses/i2c-designware-common.c | 24 ++++++++++++++++++++++++
 drivers/i2c/busses/i2c-designware-core.h   |  1 +
 2 files changed, 25 insertions(+)

diff --git a/drivers/i2c/busses/i2c-designware-common.c b/drivers/i2c/busses/i2c-designware-common.c
index 4dc57fd56170..ef618d3e1ace 100644
--- a/drivers/i2c/busses/i2c-designware-common.c
+++ b/drivers/i2c/busses/i2c-designware-common.c
@@ -1028,5 +1028,29 @@ EXPORT_GPL_DEV_PM_OPS(i2c_dw_dev_pm_ops) = {
 	RUNTIME_PM_OPS(i2c_dw_runtime_suspend, i2c_dw_runtime_resume, NULL)
 };
 
+void i2c_dw_shutdown(struct dw_i2c_dev *dev)
+{
+	unsigned int con;
+
+	/*
+	 * We only need to handle shutdown for target mode to ensure
+	 * we NACK any incoming controller requests. Controller mode cleanup
+	 * is handled after each transfer in i2c_dw_xfer().
+	 */
+	if (dev->mode != DW_IC_SLAVE)
+		return;
+
+	/*
+	 * To quickly NACK the controller during shutdown, we set the target
+	 * disable bit while the controller is still enabled.
+	 */
+	regmap_read(dev->map, DW_IC_CON, &con);
+	con |= DW_IC_CON_SLAVE_DISABLE;
+	regmap_write(dev->map, DW_IC_CON, con);
+
+	i2c_dw_disable(dev);
+}
+EXPORT_SYMBOL_GPL(i2c_dw_shutdown);
+
 MODULE_DESCRIPTION("Synopsys DesignWare I2C bus adapter core");
 MODULE_LICENSE("GPL");
diff --git a/drivers/i2c/busses/i2c-designware-core.h b/drivers/i2c/busses/i2c-designware-core.h
index 9d8d104cc391..c71aa2dd368d 100644
--- a/drivers/i2c/busses/i2c-designware-core.h
+++ b/drivers/i2c/busses/i2c-designware-core.h
@@ -417,6 +417,7 @@ static inline void i2c_dw_configure(struct dw_i2c_dev *dev)
 
 int i2c_dw_probe(struct dw_i2c_dev *dev);
 int i2c_dw_init(struct dw_i2c_dev *dev);
+void i2c_dw_shutdown(struct dw_i2c_dev *dev);
 void i2c_dw_set_mode(struct dw_i2c_dev *dev, int mode);
 
 #if IS_ENABLED(CONFIG_I2C_DESIGNWARE_BAYTRAIL)

-- 
2.54.0.563.g4f69b47b94-goog


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v5 2/4] i2c: designware: Convert PCI driver to use shutdown hook
  2026-05-11 20:06 [PATCH v5 0/4] i2c: designware: Improve device disable handling William A. Kennington III
  2026-05-11 20:06 ` [PATCH v5 1/4] i2c: designware: Introduce shutdown exported function William A. Kennington III
@ 2026-05-11 20:06 ` William A. Kennington III
  2026-05-11 20:06 ` [PATCH v5 3/4] i2c: designware: Convert platform " William A. Kennington III
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: William A. Kennington III @ 2026-05-11 20:06 UTC (permalink / raw)
  To: Mika Westerberg, Andy Shevchenko, Jan Dabros, Andi Shyti
  Cc: linux-i2c, linux-kernel, William A. Kennington III

Convert the PCI driver to use the new i2c_dw_shutdown() hook, allowing
the controller to gracefully NACK controller requests during system
shutdown.

Signed-off-by: William A. Kennington III <william@wkennington.com>
---
 drivers/i2c/busses/i2c-designware-pcidrv.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/drivers/i2c/busses/i2c-designware-pcidrv.c b/drivers/i2c/busses/i2c-designware-pcidrv.c
index f21f9877c040..ab21d4414681 100644
--- a/drivers/i2c/busses/i2c-designware-pcidrv.c
+++ b/drivers/i2c/busses/i2c-designware-pcidrv.c
@@ -356,10 +356,24 @@ static const struct pci_device_id i2c_designware_pci_ids[] = {
 };
 MODULE_DEVICE_TABLE(pci, i2c_designware_pci_ids);
 
+static void i2c_dw_pci_shutdown(struct pci_dev *pdev)
+{
+	struct dw_i2c_dev *i_dev;
+
+	i_dev = pci_get_drvdata(pdev);
+	if (!i_dev)
+		return;
+
+	pm_runtime_disable(&pdev->dev);
+	if (!pm_runtime_status_suspended(&pdev->dev))
+		i2c_dw_shutdown(i_dev);
+}
+
 static struct pci_driver dw_i2c_driver = {
 	.name		= DRIVER_NAME,
 	.probe		= i2c_dw_pci_probe,
 	.remove		= i2c_dw_pci_remove,
+	.shutdown	= i2c_dw_pci_shutdown,
 	.driver         = {
 		.pm	= pm_ptr(&i2c_dw_dev_pm_ops),
 	},

-- 
2.54.0.563.g4f69b47b94-goog


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v5 3/4] i2c: designware: Convert platform driver to use shutdown hook
  2026-05-11 20:06 [PATCH v5 0/4] i2c: designware: Improve device disable handling William A. Kennington III
  2026-05-11 20:06 ` [PATCH v5 1/4] i2c: designware: Introduce shutdown exported function William A. Kennington III
  2026-05-11 20:06 ` [PATCH v5 2/4] i2c: designware: Convert PCI driver to use shutdown hook William A. Kennington III
@ 2026-05-11 20:06 ` William A. Kennington III
  2026-05-11 20:06 ` [PATCH v5 4/4] i2c: designware: Handle active target cleanly William A. Kennington III
  2026-05-12 10:27 ` [PATCH v5 0/4] i2c: designware: Improve device disable handling Mika Westerberg
  4 siblings, 0 replies; 6+ messages in thread
From: William A. Kennington III @ 2026-05-11 20:06 UTC (permalink / raw)
  To: Mika Westerberg, Andy Shevchenko, Jan Dabros, Andi Shyti
  Cc: linux-i2c, linux-kernel, William A. Kennington III

Convert the platform driver to use the new i2c_dw_shutdown() hook,
allowing the controller to gracefully NACK controllers requests during
system shutdown.

Signed-off-by: William A. Kennington III <william@wkennington.com>
---
 drivers/i2c/busses/i2c-designware-platdrv.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/drivers/i2c/busses/i2c-designware-platdrv.c b/drivers/i2c/busses/i2c-designware-platdrv.c
index 3351c4a9ef11..da2babd6188b 100644
--- a/drivers/i2c/busses/i2c-designware-platdrv.c
+++ b/drivers/i2c/busses/i2c-designware-platdrv.c
@@ -289,9 +289,23 @@ static const struct platform_device_id dw_i2c_platform_ids[] = {
 };
 MODULE_DEVICE_TABLE(platform, dw_i2c_platform_ids);
 
+static void dw_i2c_plat_shutdown(struct platform_device *pdev)
+{
+	struct dw_i2c_dev *i_dev;
+
+	i_dev = platform_get_drvdata(pdev);
+	if (!i_dev)
+		return;
+
+	pm_runtime_disable(&pdev->dev);
+	if (!pm_runtime_status_suspended(&pdev->dev))
+		i2c_dw_shutdown(i_dev);
+}
+
 static struct platform_driver dw_i2c_driver = {
 	.probe = dw_i2c_plat_probe,
 	.remove = dw_i2c_plat_remove,
+	.shutdown = dw_i2c_plat_shutdown,
 	.driver		= {
 		.name	= "i2c_designware",
 		.of_match_table = dw_i2c_of_match,

-- 
2.54.0.563.g4f69b47b94-goog


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v5 4/4] i2c: designware: Handle active target cleanly
  2026-05-11 20:06 [PATCH v5 0/4] i2c: designware: Improve device disable handling William A. Kennington III
                   ` (2 preceding siblings ...)
  2026-05-11 20:06 ` [PATCH v5 3/4] i2c: designware: Convert platform " William A. Kennington III
@ 2026-05-11 20:06 ` William A. Kennington III
  2026-05-12 10:27 ` [PATCH v5 0/4] i2c: designware: Improve device disable handling Mika Westerberg
  4 siblings, 0 replies; 6+ messages in thread
From: William A. Kennington III @ 2026-05-11 20:06 UTC (permalink / raw)
  To: Mika Westerberg, Andy Shevchenko, Jan Dabros, Andi Shyti
  Cc: linux-i2c, linux-kernel, William A. Kennington III

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>
---
 drivers/i2c/busses/i2c-designware-common.c |  8 ++++++++
 drivers/i2c/busses/i2c-designware-master.c | 31 ++++++++++++++++++------------
 2 files changed, 27 insertions(+), 12 deletions(-)

diff --git a/drivers/i2c/busses/i2c-designware-common.c b/drivers/i2c/busses/i2c-designware-common.c
index ef618d3e1ace..7d9c6a5148cd 100644
--- a/drivers/i2c/busses/i2c-designware-common.c
+++ b/drivers/i2c/busses/i2c-designware-common.c
@@ -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);
diff --git a/drivers/i2c/busses/i2c-designware-master.c b/drivers/i2c/busses/i2c-designware-master.c
index de929b91d5ea..7a301c8b604e 100644
--- a/drivers/i2c/busses/i2c-designware-master.c
+++ b/drivers/i2c/busses/i2c-designware-master.c
@@ -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;

-- 
2.54.0.563.g4f69b47b94-goog


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v5 0/4] i2c: designware: Improve device disable handling
  2026-05-11 20:06 [PATCH v5 0/4] i2c: designware: Improve device disable handling William A. Kennington III
                   ` (3 preceding siblings ...)
  2026-05-11 20:06 ` [PATCH v5 4/4] i2c: designware: Handle active target cleanly William A. Kennington III
@ 2026-05-12 10:27 ` Mika Westerberg
  4 siblings, 0 replies; 6+ messages in thread
From: Mika Westerberg @ 2026-05-12 10:27 UTC (permalink / raw)
  To: William A. Kennington III
  Cc: Andy Shevchenko, Jan Dabros, Andi Shyti, linux-i2c, linux-kernel

On Mon, May 11, 2026 at 08:06:25PM +0000, William A. Kennington III wrote:
> 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.
> 
> This patch series addresses this by ensuring that the internal state
> machines are properly cleared when disabling the controller if target
> activity is detected.
> 
> Additionally, it adds a shutdown hook that gracefully sets the target
> disable bit before disabling the controller. This guarantees that any
> incoming requests from the controller are immediately NACKed during
> shutdown, preventing the bus from hanging.
> 
> Signed-off-by: William A. Kennington III <william@wkennington.com>

All look good to me,

Acked-by: Mika Westerberg <mika.westerberg@linux.intel.com>

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-05-12 10:27 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-11 20:06 [PATCH v5 0/4] i2c: designware: Improve device disable handling William A. Kennington III
2026-05-11 20:06 ` [PATCH v5 1/4] i2c: designware: Introduce shutdown exported function William A. Kennington III
2026-05-11 20:06 ` [PATCH v5 2/4] i2c: designware: Convert PCI driver to use shutdown hook William A. Kennington III
2026-05-11 20:06 ` [PATCH v5 3/4] i2c: designware: Convert platform " William A. Kennington III
2026-05-11 20:06 ` [PATCH v5 4/4] i2c: designware: Handle active target cleanly William A. Kennington III
2026-05-12 10:27 ` [PATCH v5 0/4] i2c: designware: Improve device disable handling Mika Westerberg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox