Skip to contentSkip to conversation

i2c: designware: Do not complete i2c read without RX_FULL interrupt

PATCH Applied Jarkko Nikula 3 participants 13 messages Dec 15, 2021 – Dec 17, 2021

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/6 i2c: designware: Do not complete i2c read without RX_FULL interrupt

From: Tamal Saha <[email protected]>

Intel Keem Bay platform supports multi-master operations over same i2c bus using Synopsys i2c DesignWare IP. When multi-masters initiate i2c operation simultaneously in a loop, SCL line is stucked low forever after few i2c operations. Following interrupt sequences are observed in:
working case: TX_EMPTY, RX_FULL and STOP_DET
non working case: TX_EMPTY, STOP_DET, RX_FULL.

DW_apb_i2c stretches the SCL line when the TX FIFO is empty or when RX FIFO is full. The DW_apb_i2c master will continue to hold the SCL line LOW until RX FIFO is read.

Linux kernel i2c DesignWare driver does not handle above non working sequence. TX_EMPTY, RX_FULL and STOP_DET routine execution are required in sequence although RX_FULL interrupt is raised after STOP_DET by hardware. Clear STOP_DET for the following conditions:
(STOP_DET ,RX_FULL, rx_outstanding)

    Write Operation: (1, 0, 0)
    Read Operation:
      RX_FULL followed by STOP_DET: (0, 1, 1) -> (1, 0, 0)
      STOP_DET followed by RX_FULL: (1, 0, 1) -> (1, 1, 0)
      RX_FULL and STOP_DET together: (1, 1, 1)
---
 drivers/i2c/busses/i2c-designware-master.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/i2c/busses/i2c-designware-master.c +5 -2
index 9b08bb5df38d..9177463c2cbb 100644
@@ -701,7 +701,8 @@ static u32 i2c_dw_read_clear_intrbits(struct dw_i2c_dev *dev)
 		regmap_read(dev->map, DW_IC_CLR_RX_DONE, &dummy);
 	if (stat & DW_IC_INTR_ACTIVITY)
 		regmap_read(dev->map, DW_IC_CLR_ACTIVITY, &dummy);
-	if (stat & DW_IC_INTR_STOP_DET)
+	if ((stat & DW_IC_INTR_STOP_DET) &&
+	    ((dev->rx_outstanding == 0) || (stat & DW_IC_INTR_RX_FULL)))
 		regmap_read(dev->map, DW_IC_CLR_STOP_DET, &dummy);
 	if (stat & DW_IC_INTR_START_DET)
 		regmap_read(dev->map, DW_IC_CLR_START_DET, &dummy);
@@ -723,6 +724,7 @@ static int i2c_dw_irq_handler_master(struct dw_i2c_dev *dev)
 	if (stat & DW_IC_INTR_TX_ABRT) {
 		dev->cmd_err |= DW_IC_ERR_TX_ABRT;
 		dev->status = STATUS_IDLE;
+		dev->rx_outstanding = 0;
 
 		/*
 		 * Anytime TX_ABRT is set, the contents of the tx/rx
@@ -745,7 +747,8 @@ static int i2c_dw_irq_handler_master(struct dw_i2c_dev *dev)
 	 */
 
 tx_aborted:
-	if ((stat & (DW_IC_INTR_TX_ABRT | DW_IC_INTR_STOP_DET)) || dev->msg_err)
+	if (((stat & (DW_IC_INTR_TX_ABRT | DW_IC_INTR_STOP_DET)) || dev->msg_err) &&
+	     (dev->rx_outstanding == 0))
 		complete(&dev->cmd_complete);
 	else if (unlikely(dev->flags & ACCESS_INTR_MASK)) {
 		/* Workaround to trigger pending interrupt */
Signature · 1 line
2.34.1
#1 lore raw

PATCH 2/6 i2c: designware-pci: Fix to change data types of hcnt and lcnt parameters

From: Lakshmi Sowjanya D <[email protected]>

The data type of hcnt and lcnt in the struct dw_i2c_dev is of type u16. It's better to have same data type in struct dw_scl_sda_cfg as well.

---
 drivers/i2c/busses/i2c-designware-pcidrv.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/i2c/busses/i2c-designware-pcidrv.c +4 -4
index 0f409a4c2da0..5b45941bcbdd 100644
@@ -39,10 +39,10 @@ enum dw_pci_ctl_id_t {
 };
 
 struct dw_scl_sda_cfg {
-	u32 ss_hcnt;
-	u32 fs_hcnt;
-	u32 ss_lcnt;
-	u32 fs_lcnt;
+	u16 ss_hcnt;
+	u16 fs_hcnt;
+	u16 ss_lcnt;
+	u16 fs_lcnt;
 	u32 sda_hold;
 };
 
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH 2/6] i2c: designware-pci: Fix to change data types of hcnt and lcnt parameters

Quoted 8 lines · Jarkko Nikula

From: Lakshmi Sowjanya D <[email protected]>

The data type of hcnt and lcnt in the struct dw_i2c_dev is of type u16. It's better to have same data type in struct dw_scl_sda_cfg as well.

Added my Reported-by and applied to for-next, thanks!

#3 lore raw

PATCH 3/6 i2c: designware-pci: Add a note about struct dw_scl_sda_cfg usage

In reply to Jarkko Nikula

From: Andy Shevchenko <[email protected]>

Add a note about struct dw_scl_sda_cfg usage to discourage people of using this structure on new platforms. Instead they should try hard to put the needed information into firmware descriptions.

---
 drivers/i2c/busses/i2c-designware-pcidrv.c | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/i2c/busses/i2c-designware-pcidrv.c +7 -0
index 5b45941bcbdd..f49c41ba5647 100644
@@ -38,6 +38,13 @@ enum dw_pci_ctl_id_t {
 	navi_amd,
 };
 
+/*
+ * This is a legacy structure to describe the hardware counters
+ * to configure signal timings on the bus. For Device Tree platforms
+ * one should use the respective properties and for ACPI there is
+ * a set of ACPI methods that provide these counters. No new
+ * platform should use this structure.
+ */
 struct dw_scl_sda_cfg {
 	u16 ss_hcnt;
 	u16 fs_hcnt;
Signature · 1 line
2.34.1
Applied
#4 lore raw

Re: [PATCH 3/6] i2c: designware-pci: Add a note about struct dw_scl_sda_cfg usage

Quoted 8 lines · Jarkko Nikula

From: Andy Shevchenko <[email protected]>

Add a note about struct dw_scl_sda_cfg usage to discourage people of using this structure on new platforms. Instead they should try hard to put the needed information into firmware descriptions.

Applied to for-next, thanks!

#5 lore raw

PATCH 4/6 i2c: designware-pci: Group MODULE_*() macros

In reply to Jarkko Nikula

From: Andy Shevchenko <[email protected]>

For better maintenance group MODULE_*() macros together.

---
 drivers/i2c/busses/i2c-designware-pcidrv.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/i2c/busses/i2c-designware-pcidrv.c +3 -4
index f49c41ba5647..021eee44fa3d 100644
@@ -359,9 +359,6 @@ static void i2c_dw_pci_remove(struct pci_dev *pdev)
 	pci_free_irq_vectors(pdev);
 }
 
-/* work with hotplug and coldplug */
-MODULE_ALIAS("i2c_designware-pci");
-
 static const struct pci_device_id i2_designware_pci_ids[] = {
 	/* Medfield */
 	{ PCI_VDEVICE(INTEL, 0x0817), medfield },
@@ -418,9 +415,11 @@ static struct pci_driver dw_i2c_driver = {
 		.pm     = &i2c_dw_pm_ops,
 	},
 };
-
 module_pci_driver(dw_i2c_driver);
 
+/* Work with hotplug and coldplug */
+MODULE_ALIAS("i2c_designware-pci");
+
 MODULE_AUTHOR("Baruch Siach <[email protected]>");
 MODULE_DESCRIPTION("Synopsys DesignWare PCI I2C bus adapter");
 MODULE_LICENSE("GPL");
Signature · 1 line
2.34.1
#6 lore raw

Re: [PATCH 4/6] i2c: designware-pci: Group MODULE_*() macros

Quoted 7 lines · Jarkko Nikula

From: Andy Shevchenko <[email protected]>

For better maintenance group MODULE_*() macros together.

---

Removed an empty line and applied to for-next, thanks!

drivers/i2c/busses/i2c-designware-pcidrv.c static struct pci_driver dw_i2c_driver = { · line 422

Quoted 2 lines
drivers/i2c/busses/i2c-designware-pcidrv.c +2 -0
+MODULE_ALIAS("i2c_designware-pci");
+

This one.

Quoted 1 line

MODULE_AUTHOR("Baruch Siach <[email protected]>");

#7 lore raw

Re: [PATCH 4/6] i2c: designware-pci: Group MODULE_*() macros

Quoted 6 lines · Wolfram Sang
Quoted 3 lines · Jarkko Nikula

From: Andy Shevchenko <[email protected]>

For better maintenance group MODULE_*() macros together.

Removed an empty line and applied to for-next, thanks!

Agree on the change and thanks!

Quoted 6 lines

drivers/i2c/busses/i2c-designware-pcidrv.c static struct pci_driver dw_i2c_driver = { · line 422

Quoted 2 lines
drivers/i2c/busses/i2c-designware-pcidrv.c +2 -0
+MODULE_ALIAS("i2c_designware-pci");
+

This one.

Quoted 1 line

MODULE_AUTHOR("Baruch Siach <[email protected]>");

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#8 lore raw

PATCH 5/6 i2c: designware-pci: use __maybe_unused for PM functions

In reply to Jarkko Nikula

From: Andy Shevchenko <[email protected]>

Use __maybe_unused for PM functions instead of ifdeffery.

---
Jarkko: In my opinion this was worth to take before cleanups so hand edited to apply to currect code.

---
 drivers/i2c/busses/i2c-designware-pcidrv.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/i2c/busses/i2c-designware-pcidrv.c +2 -4
index 021eee44fa3d..855ea666029f 100644
@@ -213,8 +213,7 @@ static struct dw_pci_controller dw_pci_controllers[] = {
 	},
 };
 
-#ifdef CONFIG_PM
-static int i2c_dw_pci_suspend(struct device *dev)
+static int __maybe_unused i2c_dw_pci_suspend(struct device *dev)
 {
 	struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
 
@@ -224,7 +223,7 @@ static int i2c_dw_pci_suspend(struct device *dev)
 	return 0;
 }
 
-static int i2c_dw_pci_resume(struct device *dev)
+static int __maybe_unused i2c_dw_pci_resume(struct device *dev)
 {
 	struct dw_i2c_dev *i_dev = dev_get_drvdata(dev);
 	int ret;
@@ -234,7 +233,6 @@ static int i2c_dw_pci_resume(struct device *dev)
 
 	return ret;
 }
-#endif
 
 static UNIVERSAL_DEV_PM_OPS(i2c_dw_pm_ops, i2c_dw_pci_suspend,
 			    i2c_dw_pci_resume, NULL);
Signature · 1 line
2.34.1
#10 lore raw

PATCH 6/6 i2c: designware-pci: Convert to use dev_err_probe()

In reply to Jarkko Nikula

From: Andy Shevchenko <[email protected]>

It's fine to call dev_err_probe() in ->probe() when error code is known. Convert the driver to use dev_err_probe().

---
Jarkko: In my opinion this was worth to take before cleanups so hand edited to apply to current code. I split the dev_err_probe() lines to have device pointer and error code in the first line and printable string in the next for shorter lines.
While at it, change "invalid" -> "Invalid" (was before Andy's change).

---
 drivers/i2c/busses/i2c-designware-pcidrv.c | 24 +++++++++-------------
 1 file changed, 10 insertions(+), 14 deletions(-)
drivers/i2c/busses/i2c-designware-pcidrv.c +10 -14
index 855ea666029f..6fde58878aa6 100644
@@ -246,28 +246,24 @@ static int i2c_dw_pci_probe(struct pci_dev *pdev,
 	struct dw_pci_controller *controller;
 	struct dw_scl_sda_cfg *cfg;
 
-	if (id->driver_data >= ARRAY_SIZE(dw_pci_controllers)) {
-		dev_err(&pdev->dev, "%s: invalid driver data %ld\n", __func__,
-			id->driver_data);
-		return -EINVAL;
-	}
+	if (id->driver_data >= ARRAY_SIZE(dw_pci_controllers))
+		return dev_err_probe(&pdev->dev, -EINVAL,
+				     "Invalid driver data %ld\n",
+				     id->driver_data);
 
 	controller = &dw_pci_controllers[id->driver_data];
 
 	r = pcim_enable_device(pdev);
-	if (r) {
-		dev_err(&pdev->dev, "Failed to enable I2C PCI device (%d)\n",
-			r);
-		return r;
-	}
+	if (r)
+		return dev_err_probe(&pdev->dev, r,
+				     "Failed to enable I2C PCI device\n");
 
 	pci_set_master(pdev);
 
 	r = pcim_iomap_regions(pdev, 1 << 0, pci_name(pdev));
-	if (r) {
-		dev_err(&pdev->dev, "I/O memory remapping failed\n");
-		return r;
-	}
+	if (r)
+		return dev_err_probe(&pdev->dev, r,
+				     "I/O memory remapping failed\n");
 
 	dev = devm_kzalloc(&pdev->dev, sizeof(struct dw_i2c_dev), GFP_KERNEL);
 	if (!dev)
Signature · 1 line
2.34.1
Applied
#12 lore raw
In reply to Jarkko Nikula
Quoted 19 lines · Jarkko Nikula

From: Tamal Saha <[email protected]>

Intel Keem Bay platform supports multi-master operations over same i2c bus using Synopsys i2c DesignWare IP. When multi-masters initiate i2c operation simultaneously in a loop, SCL line is stucked low forever after few i2c operations. Following interrupt sequences are observed in:
working case: TX_EMPTY, RX_FULL and STOP_DET
non working case: TX_EMPTY, STOP_DET, RX_FULL.

DW_apb_i2c stretches the SCL line when the TX FIFO is empty or when RX FIFO is full. The DW_apb_i2c master will continue to hold the SCL line LOW until RX FIFO is read.

Linux kernel i2c DesignWare driver does not handle above non working sequence. TX_EMPTY, RX_FULL and STOP_DET routine execution are required in sequence although RX_FULL interrupt is raised after STOP_DET by hardware. Clear STOP_DET for the following conditions:
(STOP_DET ,RX_FULL, rx_outstanding)

Quoted 8 lines
    Write Operation: (1, 0, 0)
    Read Operation:
      RX_FULL followed by STOP_DET: (0, 1, 1) -> (1, 0, 0)
      STOP_DET followed by RX_FULL: (1, 0, 1) -> (1, 1, 0)
      RX_FULL and STOP_DET together: (1, 1, 1)

Applied to for-next, thanks!