Skip to contentSkip to conversation

i2c: imx: Fix and enable DMA support for LS1021A

PATCH Superseded Esben Haabendal 4 participants 11 messages May 23, 2018 – Jul 9, 2018

Subsystems (4)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#1 lore raw

PATCH 1/4 i2c: imx: Fix reinit_completion() use

In reply to Esben Haabendal

From: Esben Haabendal <[email protected]>

Make sure to call reinit_completion() before dma is started to avoid race condition where reinit_compleition() is called after complete() and before wait_for_completion_timeout().

---
 drivers/i2c/busses/i2c-imx.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/i2c/busses/i2c-imx.c +1 -2
index d7267dd9c7bf..6fca5e64cffb 100644
@@ -377,6 +377,7 @@ static int i2c_imx_dma_xfer(struct imx_i2c_struct *i2c_imx,
 		goto err_desc;
 	}
 
+	reinit_completion(&dma->cmd_complete);
 	txdesc->callback = i2c_imx_dma_callback;
 	txdesc->callback_param = i2c_imx;
 	if (dma_submit_error(dmaengine_submit(txdesc))) {
@@ -631,7 +632,6 @@ static int i2c_imx_dma_write(struct imx_i2c_struct *i2c_imx,
 	 * The first byte must be transmitted by the CPU.
 	 */
 	imx_i2c_write_reg(msgs->addr << 1, i2c_imx, IMX_I2C_I2DR);
-	reinit_completion(&i2c_imx->dma->cmd_complete);
 	time_left = wait_for_completion_timeout(
 				&i2c_imx->dma->cmd_complete,
 				msecs_to_jiffies(DMA_TIMEOUT));
@@ -690,7 +690,6 @@ static int i2c_imx_dma_read(struct imx_i2c_struct *i2c_imx,
 	if (result)
 		return result;
 
-	reinit_completion(&i2c_imx->dma->cmd_complete);
 	time_left = wait_for_completion_timeout(
 				&i2c_imx->dma->cmd_complete,
 				msecs_to_jiffies(DMA_TIMEOUT));
Signature · 1 line
2.17.0
#4 lore raw

PATCH 2/4 i2c: imx: Fix race condition in dma read

In reply to Esben Haabendal

From: Esben Haabendal <[email protected]>

This fixes a race condition, where the DMAEN bit ends up being set after I2C slave has transmitted a byte following the dummy read. When that happens, an interrupt is generated instead, and no DMA request is generated to kickstart the DMA read, and a timeout happens after DMA_TIMEOUT (1 sec).

Fixed by setting the DMAEN bit before the dummy read.

---
 drivers/i2c/busses/i2c-imx.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/i2c/busses/i2c-imx.c +5 -3
index 6fca5e64cffb..742b548437af 100644
@@ -677,9 +677,6 @@ static int i2c_imx_dma_read(struct imx_i2c_struct *i2c_imx,
 	struct imx_i2c_dma *dma = i2c_imx->dma;
 	struct device *dev = &i2c_imx->adapter.dev;
 
-	temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
-	temp |= I2CR_DMAEN;
-	imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
 
 	dma->chan_using = dma->chan_rx;
 	dma->dma_transfer_dir = DMA_DEV_TO_MEM;
@@ -819,6 +816,11 @@ static int i2c_imx_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs, bo
 	if ((msgs->len - 1) || block_data)
 		temp &= ~I2CR_TXAK;
 	imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
+	if (i2c_imx->dma && msgs->len >= DMA_THRESHOLD && !block_data) {
+		temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
+		temp |= I2CR_DMAEN;
+		imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
+	}
 	imx_i2c_read_reg(i2c_imx, IMX_I2C_I2DR); /* dummy read */
 
 	dev_dbg(&i2c_imx->adapter.dev, "<%s> read data\n", __func__);
Signature · 1 line
2.17.0
#7 lore raw

PATCH 3/4 i2c: imx: Simplify stopped state tracking

In reply to Esben Haabendal

From: Esben Haabendal <[email protected]>

Always update the stopped state when busy status have been checked. This is identical to what was done before, with the exception of error handling.
Without this change, some errors cause the stopped state to be left in incorrect state in i2c_imx_stop(), i2c_imx_dma_read(), i2c_imx_read() and i2c_imx_xfer().

---
 drivers/i2c/busses/i2c-imx.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/i2c/busses/i2c-imx.c +6 -6
index 742b548437af..f9c99b123188 100644
@@ -430,10 +430,14 @@ static int i2c_imx_bus_busy(struct imx_i2c_struct *i2c_imx, int for_busy)
 			return -EAGAIN;
 		}
 
-		if (for_busy && (temp & I2SR_IBB))
+		if (for_busy && (temp & I2SR_IBB)) {
+			i2c_imx->stopped = 0;
 			break;
-		if (!for_busy && !(temp & I2SR_IBB))
+		}
+		if (!for_busy && !(temp & I2SR_IBB)) {
+			i2c_imx->stopped = 1;
 			break;
+		}
 		if (time_after(jiffies, orig_jiffies + msecs_to_jiffies(500))) {
 			dev_dbg(&i2c_imx->adapter.dev,
 				"<%s> I2C bus is busy\n", __func__);
@@ -547,7 +551,6 @@ static int i2c_imx_start(struct imx_i2c_struct *i2c_imx)
 	result = i2c_imx_bus_busy(i2c_imx, 1);
 	if (result)
 		return result;
-	i2c_imx->stopped = 0;
 
 	temp |= I2CR_IIEN | I2CR_MTX | I2CR_TXAK;
 	temp &= ~I2CR_DMAEN;
@@ -578,7 +581,6 @@ static void i2c_imx_stop(struct imx_i2c_struct *i2c_imx)
 
 	if (!i2c_imx->stopped) {
 		i2c_imx_bus_busy(i2c_imx, 0);
-		i2c_imx->stopped = 1;
 	}
 
 	/* Disable I2C controller */
@@ -733,7 +735,6 @@ static int i2c_imx_dma_read(struct imx_i2c_struct *i2c_imx,
 		temp &= ~(I2CR_MSTA | I2CR_MTX);
 		imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
 		i2c_imx_bus_busy(i2c_imx, 0);
-		i2c_imx->stopped = 1;
 	} else {
 		/*
 		 * For i2c master receiver repeat restart operation like:
@@ -861,7 +862,6 @@ static int i2c_imx_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs, bo
 				temp &= ~(I2CR_MSTA | I2CR_MTX);
 				imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
 				i2c_imx_bus_busy(i2c_imx, 0);
-				i2c_imx->stopped = 1;
 			} else {
 				/*
 				 * For i2c master receiver repeat restart operation like:
Signature · 1 line
2.17.0
#8 lore raw

PATCH 4/4 arm: dts: ls1021a: Enable I2C DMA support

In reply to Esben Haabendal

From: Esben Haabendal <[email protected]>

Gives substantial performance improvement for transfers larger than 16 bytes (DMA_THRESHOLD). Smaller transfers are unaffected.

---
 arch/arm/boot/dts/ls1021a.dtsi | 6 ++++++
 1 file changed, 6 insertions(+)
arch/arm/boot/dts/ls1021a.dtsi +6 -0
index c55d479971cc..1e5640701c65 100644
@@ -363,6 +363,8 @@
 			interrupts = <GIC_SPI 88 IRQ_TYPE_LEVEL_HIGH>;
 			clock-names = "i2c";
 			clocks = <&clockgen 4 1>;
+			dma-names = "tx", "rx";
+			dmas = <&edma0 1 39>, <&edma0 1 38>;
 			status = "disabled";
 		};
 
@@ -374,6 +376,8 @@
 			interrupts = <GIC_SPI 89 IRQ_TYPE_LEVEL_HIGH>;
 			clock-names = "i2c";
 			clocks = <&clockgen 4 1>;
+			dma-names = "tx", "rx";
+			dmas = <&edma0 1 37>, <&edma0 1 36>;
 			status = "disabled";
 		};
 
@@ -385,6 +389,8 @@
 			interrupts = <GIC_SPI 90 IRQ_TYPE_LEVEL_HIGH>;
 			clock-names = "i2c";
 			clocks = <&clockgen 4 1>;
+			dma-names = "tx", "rx";
+			dmas = <&edma0 1 35>, <&edma0 1 34>;
 			status = "disabled";
 		};
 
Signature · 1 line
2.17.0
#0 lore raw

cover i2c: imx: Fix and enable DMA support for LS1021A

From: Esben Haabendal <[email protected]>

This patch series fixes two race conditions and minor issues with tracking the stopped state when something goes wrong.

With that in place, DMA support works with NXP LS1021A, so it is enabled in the last patch.

Esben Haabendal (4):
i2c: imx: Fix reinit_completion() use
i2c: imx: Fix race condition in dma read
i2c: imx: Simplify stopped state tracking
arm: dts: ls1021a: Enable I2C DMA support

 arch/arm/boot/dts/ls1021a.dtsi |  6 ++++++
 drivers/i2c/busses/i2c-imx.c   | 23 ++++++++++++-----------
 2 files changed, 18 insertions(+), 11 deletions(-)
Signature · 1 line
2.17.0
#9 lore raw
Quoted 14 lines · Esben Haabendal

From: Esben Haabendal <[email protected]>

This patch series fixes two race conditions and minor issues with tracking the stopped state when something goes wrong.

With that in place, DMA support works with NXP LS1021A, so it is enabled in the last patch.

Esben Haabendal (4):
i2c: imx: Fix reinit_completion() use
i2c: imx: Fix race condition in dma read
i2c: imx: Simplify stopped state tracking
arm: dts: ls1021a: Enable I2C DMA support

Quoted 3 lines
 arch/arm/boot/dts/ls1021a.dtsi |  6 ++++++
 drivers/i2c/busses/i2c-imx.c   | 23 ++++++++++++-----------
 2 files changed, 18 insertions(+), 11 deletions(-)

Hi

Is it possible that someone could find time to take a look at these patches?

Best regards,
Esben Haabendal

#10 lore raw
Quoted 16 lines · Esben Haabendal
Quoted 14 lines · Esben Haabendal

From: Esben Haabendal <[email protected]>

This patch series fixes two race conditions and minor issues with tracking the stopped state when something goes wrong.

With that in place, DMA support works with NXP LS1021A, so it is enabled in the last patch.

Esben Haabendal (4):
i2c: imx: Fix reinit_completion() use
i2c: imx: Fix race condition in dma read
i2c: imx: Simplify stopped state tracking
arm: dts: ls1021a: Enable I2C DMA support

Quoted 8 lines
Quoted 3 lines
 arch/arm/boot/dts/ls1021a.dtsi |  6 ++++++
 drivers/i2c/busses/i2c-imx.c   | 23 ++++++++++++-----------
 2 files changed, 18 insertions(+), 11 deletions(-)

Hi

Is it possible that someone could find time to take a look at these patches?

Uwe maybe? Or someone from PTX?

Reviewed-by on v1 1/4
#2 lore raw

Re: [PATCH 1/4] i2c: imx: Fix reinit_completion() use

In reply to Esben Haabendal

Cc += Yuan Yao who authored DMA support and the NXP team.

Quoted 4 lines · Esben Haabendal

From: Esben Haabendal <[email protected]>

Make sure to call reinit_completion() before dma is started to avoid race condition where reinit_compleition() is called after complete() and before

s/compleition/completion/

Quoted 1 line

wait_for_completion_timeout().

Is this a theoretical problem, or did it trigger on your side?

Quoted 1 line
Fixes ce1a78840ff7 ("i2c: imx: add DMA support for freescale i2c driver")
Quoted 25 lines
---
 drivers/i2c/busses/i2c-imx.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/i2c/busses/i2c-imx.c +1 -1
index d7267dd9c7bf..6fca5e64cffb 100644
@@ -377,6 +377,7 @@ static int i2c_imx_dma_xfer(struct imx_i2c_struct *i2c_imx,
 		goto err_desc;
 	}
 
+	reinit_completion(&dma->cmd_complete);
 	txdesc->callback = i2c_imx_dma_callback;
 	txdesc->callback_param = i2c_imx;
 	if (dma_submit_error(dmaengine_submit(txdesc))) {
@@ -631,7 +632,6 @@ static int i2c_imx_dma_write(struct imx_i2c_struct *i2c_imx,
 	 * The first byte must be transmitted by the CPU.
 	 */
 	imx_i2c_write_reg(msgs->addr << 1, i2c_imx, IMX_I2C_I2DR);
-	reinit_completion(&i2c_imx->dma->cmd_complete);
 	time_left = wait_for_completion_timeout(
 				&i2c_imx->dma->cmd_complete,
 				msecs_to_jiffies(DMA_TIMEOUT));

drivers/i2c/busses/i2c-imx.c static int i2c_imx_dma_read(struct imx_i2c_struct *i2c_imx, · line 695

Quoted 7 lines
drivers/i2c/busses/i2c-imx.c +0 -1
@@ -690,7 +690,6 @@ static int i2c_imx_dma_read(struct imx_i2c_struct *i2c_imx,
 	if (result)
 		return result;
 
-	reinit_completion(&i2c_imx->dma->cmd_complete);
 	time_left = wait_for_completion_timeout(
 				&i2c_imx->dma->cmd_complete,
 				msecs_to_jiffies(DMA_TIMEOUT));

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
#5 lore raw

Re: [PATCH 2/4] i2c: imx: Fix race condition in dma read

In reply to Esben Haabendal
Quoted 8 lines · Esben Haabendal

From: Esben Haabendal <[email protected]>

This fixes a race condition, where the DMAEN bit ends up being set after I2C slave has transmitted a byte following the dummy read. When that happens, an interrupt is generated instead, and no DMA request is generated to kickstart the DMA read, and a timeout happens after DMA_TIMEOUT (1 sec).

Fixed by setting the DMAEN bit before the dummy read.

Does this fix the problem or just narrow the race window?

Best regards
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | http://www.pengutronix.de/  |
#3 lore raw

Re: [PATCH 1/4] i2c: imx: Fix reinit_completion() use

In reply to Uwe Kleine-König
Quoted 9 lines · Uwe Kleine-König

Cc += Yuan Yao who authored DMA support and the NXP team.

Quoted 4 lines · Esben Haabendal

From: Esben Haabendal <[email protected]>

Make sure to call reinit_completion() before dma is started to avoid race condition where reinit_compleition() is called after complete() and before

s/compleition/completion/

Will fix in v2.

Quoted 4 lines
Quoted 2 lines

wait_for_completion_timeout().

Is this a theoretical problem, or did it trigger on your side?

I thought I did trigger it, but haven't been able to reproduce. So it might or might not be theoretical based on my experiences, but it does look like something that can (and thus should) trigger sometimes.

/Esben

#6 lore raw

Re: [PATCH 2/4] i2c: imx: Fix race condition in dma read

In reply to Uwe Kleine-König
Quoted 11 lines · Uwe Kleine-König
Quoted 8 lines · Esben Haabendal

From: Esben Haabendal <[email protected]>

This fixes a race condition, where the DMAEN bit ends up being set after I2C slave has transmitted a byte following the dummy read. When that happens, an interrupt is generated instead, and no DMA request is generated to kickstart the DMA read, and a timeout happens after DMA_TIMEOUT (1 sec).

Fixed by setting the DMAEN bit before the dummy read.

Does this fix the problem or just narrow the race window?

It should fix the problem.

/Esben