2020-10-02 15:26:05

by Christian Eggers

[permalink] [raw]
Subject: [PATCH v2 0/3] i2c: imx: Fix handling of arbitration loss

Changes in v2:
---------------
- Don't accidently clear additional status flags on Vybrid
(reported by Uwe Kleine-Koenig)


On my (noisy) system, I2C arbitration losses happen quite often. In it's
current implementation, the IAL flag is partly handled, but has a
number of shortcomings:

1. The driver runs unnecessarily in a timeout when waiting for an
interrupt.

2. The driver performs 500 ms busy-waiting without any value.

3. Arbitration loss errors may be reported one transfer later than they
occured.

Best regards
Christian



2020-10-02 15:27:18

by Christian Eggers

[permalink] [raw]
Subject: [PATCH v2 1/3] i2c: imx: Fix reset of I2SR_IAL flag

According to the "VFxxx Controller Reference Manual" (and the comment
block starting at line 97), Vybrid requires writing a one for clearing
an interrupt flag. Syncing the method for clearing I2SR_IIF in
i2c_imx_isr().

Signed-off-by: Christian Eggers <[email protected]>
Cc: [email protected]
---
drivers/i2c/busses/i2c-imx.c | 15 ++++++++++++---
1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/drivers/i2c/busses/i2c-imx.c b/drivers/i2c/busses/i2c-imx.c
index 0ab5381aa012..34648df7f1a6 100644
--- a/drivers/i2c/busses/i2c-imx.c
+++ b/drivers/i2c/busses/i2c-imx.c
@@ -424,7 +424,12 @@ static int i2c_imx_bus_busy(struct imx_i2c_struct *i2c_imx, int for_busy, bool a

/* check for arbitration lost */
if (temp & I2SR_IAL) {
- temp &= ~I2SR_IAL;
+ /*
+ * i2sr_clr_opcode is the value to clear all interrupts.
+ * Here we want to clear only I2SR_IAL, so we write
+ * ~i2sr_clr_opcode with just the I2SR_IAL bit toggled.
+ */
+ temp = ~i2c_imx->hwdata->i2sr_clr_opcode ^ I2SR_IAL;
imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2SR);
return -EAGAIN;
}
@@ -623,8 +628,12 @@ static irqreturn_t i2c_imx_isr(int irq, void *dev_id)
if (temp & I2SR_IIF) {
/* save status register */
i2c_imx->i2csr = temp;
- temp &= ~I2SR_IIF;
- temp |= (i2c_imx->hwdata->i2sr_clr_opcode & I2SR_IIF);
+ /*
+ * i2sr_clr_opcode is the value to clear all interrupts.
+ * Here we want to clear only I2SR_IIF, so we write
+ * ~i2sr_clr_opcode with just the I2SR_IIF bit toggled.
+ */
+ temp = ~i2c_imx->hwdata->i2sr_clr_opcode ^ I2SR_IIF;
imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2SR);
wake_up(&i2c_imx->queue);
return IRQ_HANDLED;
--
Christian Eggers
Embedded software developer

Arnold & Richter Cine Technik GmbH & Co. Betriebs KG
Sitz: Muenchen - Registergericht: Amtsgericht Muenchen - Handelsregisternummer: HRA 57918
Persoenlich haftender Gesellschafter: Arnold & Richter Cine Technik GmbH
Sitz: Muenchen - Registergericht: Amtsgericht Muenchen - Handelsregisternummer: HRB 54477
Geschaeftsfuehrer: Dr. Michael Neuhaeuser; Stephan Schenk; Walter Trauninger; Markus Zeiler

2020-10-02 15:27:43

by Christian Eggers

[permalink] [raw]
Subject: [PATCH v2 2/3] i2c: imx: Check for I2SR_IAL after every byte

Arbitration Lost (IAL) can happen after every single byte transfer. If
arbitration is lost, the I2C hardware will autonomously switch from
master mode to slave. If a transfer is not aborted in this state,
consecutive transfers will not be executed by the hardware and will
timeout.

Signed-off-by: Christian Eggers <[email protected]>
Cc: [email protected]
---
drivers/i2c/busses/i2c-imx.c | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)

diff --git a/drivers/i2c/busses/i2c-imx.c b/drivers/i2c/busses/i2c-imx.c
index 34648df7f1a6..1db1e2f5296e 100644
--- a/drivers/i2c/busses/i2c-imx.c
+++ b/drivers/i2c/busses/i2c-imx.c
@@ -483,6 +483,24 @@ static int i2c_imx_trx_complete(struct imx_i2c_struct *i2c_imx, bool atomic)
dev_dbg(&i2c_imx->adapter.dev, "<%s> Timeout\n", __func__);
return -ETIMEDOUT;
}
+
+ /* check for arbitration lost */
+ if (i2c_imx->i2csr & I2SR_IAL) {
+ unsigned int temp;
+
+ dev_dbg(&i2c_imx->adapter.dev, "<%s> Arbitration lost\n", __func__);
+ /*
+ * i2sr_clr_opcode is the value to clear all interrupts.
+ * Here we want to clear only I2SR_IAL, so we write
+ * ~i2sr_clr_opcode with just the I2SR_IAL bit toggled.
+ */
+ temp = ~i2c_imx->hwdata->i2sr_clr_opcode ^ I2SR_IAL;
+ imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2SR);
+
+ i2c_imx->i2csr = 0;
+ return -EAGAIN;
+ }
+
dev_dbg(&i2c_imx->adapter.dev, "<%s> TRX complete\n", __func__);
i2c_imx->i2csr = 0;
return 0;
--
Christian Eggers
Embedded software developer

Arnold & Richter Cine Technik GmbH & Co. Betriebs KG
Sitz: Muenchen - Registergericht: Amtsgericht Muenchen - Handelsregisternummer: HRA 57918
Persoenlich haftender Gesellschafter: Arnold & Richter Cine Technik GmbH
Sitz: Muenchen - Registergericht: Amtsgericht Muenchen - Handelsregisternummer: HRB 54477
Geschaeftsfuehrer: Dr. Michael Neuhaeuser; Stephan Schenk; Walter Trauninger; Markus Zeiler

2020-10-02 15:28:28

by Christian Eggers

[permalink] [raw]
Subject: [PATCH v2 3/3] i2c: imx: Don't generate STOP condition if arbitration has been lost

If arbitration is lost, the master automatically changes to slave mode.
I2SR_IBB may or may not be reset by hardware. Raising a STOP condition
by resetting I2CR_MSTA has no effect and will not clear I2SR_IBB.

So calling i2c_imx_bus_busy() is not required and would busy-wait until
timeout.

Signed-off-by: Christian Eggers <[email protected]>
Cc: [email protected] # Requires trivial backporting, simple remove
# the 3rd argument from the calls to
# i2c_imx_bus_busy().
---
drivers/i2c/busses/i2c-imx.c | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/drivers/i2c/busses/i2c-imx.c b/drivers/i2c/busses/i2c-imx.c
index 1db1e2f5296e..ced9cb3a2665 100644
--- a/drivers/i2c/busses/i2c-imx.c
+++ b/drivers/i2c/busses/i2c-imx.c
@@ -616,6 +616,8 @@ static void i2c_imx_stop(struct imx_i2c_struct *i2c_imx, bool atomic)
/* Stop I2C transaction */
dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
+ if (!(temp & I2CR_MSTA))
+ i2c_imx->stopped = 1;
temp &= ~(I2CR_MSTA | I2CR_MTX);
if (i2c_imx->dma)
temp &= ~I2CR_DMAEN;
@@ -785,9 +787,12 @@ static int i2c_imx_dma_read(struct imx_i2c_struct *i2c_imx,
*/
dev_dbg(dev, "<%s> clear MSTA\n", __func__);
temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
+ if (!(temp & I2CR_MSTA))
+ i2c_imx->stopped = 1;
temp &= ~(I2CR_MSTA | I2CR_MTX);
imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
- i2c_imx_bus_busy(i2c_imx, 0, false);
+ if (!i2c_imx->stopped)
+ i2c_imx_bus_busy(i2c_imx, 0, false);
} else {
/*
* For i2c master receiver repeat restart operation like:
@@ -912,9 +917,12 @@ static int i2c_imx_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs,
dev_dbg(&i2c_imx->adapter.dev,
"<%s> clear MSTA\n", __func__);
temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
+ if (!(temp & I2CR_MSTA))
+ i2c_imx->stopped = 1;
temp &= ~(I2CR_MSTA | I2CR_MTX);
imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
- i2c_imx_bus_busy(i2c_imx, 0, atomic);
+ if (!i2c_imx->stopped)
+ i2c_imx_bus_busy(i2c_imx, 0, atomic);
} else {
/*
* For i2c master receiver repeat restart operation like:
--
Christian Eggers
Embedded software developer

Arnold & Richter Cine Technik GmbH & Co. Betriebs KG
Sitz: Muenchen - Registergericht: Amtsgericht Muenchen - Handelsregisternummer: HRA 57918
Persoenlich haftender Gesellschafter: Arnold & Richter Cine Technik GmbH
Sitz: Muenchen - Registergericht: Amtsgericht Muenchen - Handelsregisternummer: HRB 54477
Geschaeftsfuehrer: Dr. Michael Neuhaeuser; Stephan Schenk; Walter Trauninger; Markus Zeiler

2020-10-05 08:07:34

by Krzysztof Kozlowski

[permalink] [raw]
Subject: Re: [PATCH v2 1/3] i2c: imx: Fix reset of I2SR_IAL flag

On Fri, Oct 02, 2020 at 05:23:03PM +0200, Christian Eggers wrote:
> According to the "VFxxx Controller Reference Manual" (and the comment
> block starting at line 97), Vybrid requires writing a one for clearing
> an interrupt flag. Syncing the method for clearing I2SR_IIF in
> i2c_imx_isr().
>
> Signed-off-by: Christian Eggers <[email protected]>
> Cc: [email protected]
> ---
> drivers/i2c/busses/i2c-imx.c | 15 ++++++++++++---
> 1 file changed, 12 insertions(+), 3 deletions(-)


Tested (not extensively) on Vybrid VF500 (Toradex VF50):
Tested-by: Krzysztof Kozlowski <[email protected]>

The I2C on Vybrid VF500 still works fine. I did not test this actual
condition (arbitration) but only a regular I2C driver (BQ27xxx fuel
gauge).

Best regards,
Krzysztof

2020-10-05 08:09:36

by Krzysztof Kozlowski

[permalink] [raw]
Subject: Re: [PATCH v2 2/3] i2c: imx: Check for I2SR_IAL after every byte

On Fri, Oct 02, 2020 at 05:23:04PM +0200, Christian Eggers wrote:
> Arbitration Lost (IAL) can happen after every single byte transfer. If
> arbitration is lost, the I2C hardware will autonomously switch from
> master mode to slave. If a transfer is not aborted in this state,
> consecutive transfers will not be executed by the hardware and will
> timeout.
>
> Signed-off-by: Christian Eggers <[email protected]>
> Cc: [email protected]
> ---
> drivers/i2c/busses/i2c-imx.c | 18 ++++++++++++++++++
> 1 file changed, 18 insertions(+)
>

Tested (not extensively) on Vybrid VF500 (Toradex VF50):
Tested-by: Krzysztof Kozlowski <[email protected]>

The I2C on Vybrid VF500 still works fine. I did not test this actual
condition (arbitration) but only a regular I2C driver (BQ27xxx fuel
gauge). Obviously this only proves that regular operation is not
broken...

Alternatively if you have a specific testing procedure (reproduction of
a problem), please share.

Best regards,
Krzysztof

2020-10-05 08:20:06

by Krzysztof Kozlowski

[permalink] [raw]
Subject: Re: [PATCH v2 3/3] i2c: imx: Don't generate STOP condition if arbitration has been lost

On Fri, Oct 02, 2020 at 05:23:05PM +0200, Christian Eggers wrote:
> If arbitration is lost, the master automatically changes to slave mode.
> I2SR_IBB may or may not be reset by hardware. Raising a STOP condition
> by resetting I2CR_MSTA has no effect and will not clear I2SR_IBB.
>
> So calling i2c_imx_bus_busy() is not required and would busy-wait until
> timeout.
>
> Signed-off-by: Christian Eggers <[email protected]>
> Cc: [email protected] # Requires trivial backporting, simple remove
> # the 3rd argument from the calls to
> # i2c_imx_bus_busy().
> ---
> drivers/i2c/busses/i2c-imx.c | 12 ++++++++++--
> 1 file changed, 10 insertions(+), 2 deletions(-)
>

Tested (not extensively) on Vybrid VF500 (Toradex VF50):
Tested-by: Krzysztof Kozlowski <[email protected]>

The I2C on Vybrid VF500 still works fine (also bigger transfers). I did
not test this actual condition (arbitration) but only a regular I2C
driver (BQ27xxx fuel gauge). Obviously this only proves that regular
operation is not broken...

Best regards,
Krzysztof

2020-10-05 09:27:40

by Christian Eggers

[permalink] [raw]
Subject: Re: [PATCH v2 2/3] i2c: imx: Check for I2SR_IAL after every byte

On Monday, 5 October 2020, 10:07:25 CEST, Krzysztof Kozlowski wrote:
> The I2C on Vybrid VF500 still works fine. I did not test this actual
> condition (arbitration) but only a regular I2C driver (BQ27xxx fuel
> gauge). Obviously this only proves that regular operation is not
> broken...
thank you very much for testing on Vybrid.

> Alternatively if you have a specific testing procedure (reproduction of
> a problem), please share.

The IAL errors happen due to noise on our I2C bus. We have our power supply
connected via I2C. The hardware designers wanted to make sure that no
high currents flow through the ground pins of the I2C interface. So they added
a series resistor (30 Ohm) in the GND line between the power supply and the
i.MX board.

If you have an I2C device on an external PCB, adding some small series
resistance in the GND line may cause IAL errors. On the other hand, if
everything else works fine, also handling if IAL should work on Vybrid.

> Best regards,
> Krzysztof
Best regards
Christian



2020-10-06 06:09:08

by Uwe Kleine-König

[permalink] [raw]
Subject: Re: [PATCH v2 1/3] i2c: imx: Fix reset of I2SR_IAL flag

On Fri, Oct 02, 2020 at 05:23:03PM +0200, Christian Eggers wrote:
> According to the "VFxxx Controller Reference Manual" (and the comment
> block starting at line 97), Vybrid requires writing a one for clearing
> an interrupt flag. Syncing the method for clearing I2SR_IIF in
> i2c_imx_isr().
>
> Signed-off-by: Christian Eggers <[email protected]>
> Cc: [email protected]
> ---
> drivers/i2c/busses/i2c-imx.c | 15 ++++++++++++---
> 1 file changed, 12 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/i2c/busses/i2c-imx.c b/drivers/i2c/busses/i2c-imx.c
> index 0ab5381aa012..34648df7f1a6 100644
> --- a/drivers/i2c/busses/i2c-imx.c
> +++ b/drivers/i2c/busses/i2c-imx.c
> @@ -424,7 +424,12 @@ static int i2c_imx_bus_busy(struct imx_i2c_struct *i2c_imx, int for_busy, bool a
>
> /* check for arbitration lost */
> if (temp & I2SR_IAL) {
> - temp &= ~I2SR_IAL;
> + /*
> + * i2sr_clr_opcode is the value to clear all interrupts.
> + * Here we want to clear only I2SR_IAL, so we write
> + * ~i2sr_clr_opcode with just the I2SR_IAL bit toggled.
> + */
> + temp = ~i2c_imx->hwdata->i2sr_clr_opcode ^ I2SR_IAL;
> imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2SR);
> return -EAGAIN;

Could we please move clearing an irq to a dedicated function? Such that
it looks like:

/* check for arbitration lost */
if (temp & I2SR_IAL) {
i2c_imx_clear_irq(i2c_imx, I2SR_IAL);
return -EAGAIN;
}

Then you also don't need to duplicate the describing comment but just
add it to the implementation of i2c_imx_clear_irq().

Best regards
Uwe

--
Pengutronix e.K. | Uwe Kleine-K?nig |
Industrial Linux Solutions | https://www.pengutronix.de/ |


Attachments:
(No filename) (1.73 kB)
signature.asc (499.00 B)
Download all attachments

2020-10-06 11:03:28

by Christian Eggers

[permalink] [raw]
Subject: [PATCH v3 0/3] i2c: imx: Fix handling of arbitration loss

On Tuesday, 6 October 2020, 08:05:28 CEST, Uwe Kleine-König wrote:
> Could we please move clearing an irq to a dedicated function? Such that
> it looks like:
>
> /* check for arbitration lost */
> if (temp & I2SR_IAL) {
> i2c_imx_clear_irq(i2c_imx, I2SR_IAL);
> return -EAGAIN;
> }
done

Changes in v2:
---------------
- Don't accidently clear additional status flags on Vybrid
(reported by Uwe Kleine-Koenig)

Changes in v3:
---------------
- dedicated function for clearing an irq


2020-10-06 11:04:43

by Christian Eggers

[permalink] [raw]
Subject: [PATCH v3 2/3] i2c: imx: Check for I2SR_IAL after every byte

Arbitration Lost (IAL) can happen after every single byte transfer. If
arbitration is lost, the I2C hardware will autonomously switch from
master mode to slave. If a transfer is not aborted in this state,
consecutive transfers will not be executed by the hardware and will
timeout.

Signed-off-by: Christian Eggers <[email protected]>
Cc: [email protected]
---
drivers/i2c/busses/i2c-imx.c | 10 ++++++++++
1 file changed, 10 insertions(+)

diff --git a/drivers/i2c/busses/i2c-imx.c b/drivers/i2c/busses/i2c-imx.c
index 745f4071155a..bf0e94814222 100644
--- a/drivers/i2c/busses/i2c-imx.c
+++ b/drivers/i2c/busses/i2c-imx.c
@@ -490,6 +490,16 @@ static int i2c_imx_trx_complete(struct imx_i2c_struct *i2c_imx, bool atomic)
dev_dbg(&i2c_imx->adapter.dev, "<%s> Timeout\n", __func__);
return -ETIMEDOUT;
}
+
+ /* check for arbitration lost */
+ if (i2c_imx->i2csr & I2SR_IAL) {
+ dev_dbg(&i2c_imx->adapter.dev, "<%s> Arbitration lost\n", __func__);
+ i2c_imx_clear_irq(i2c_imx, I2SR_IAL);
+
+ i2c_imx->i2csr = 0;
+ return -EAGAIN;
+ }
+
dev_dbg(&i2c_imx->adapter.dev, "<%s> TRX complete\n", __func__);
i2c_imx->i2csr = 0;
return 0;
--
Christian Eggers
Embedded software developer

Arnold & Richter Cine Technik GmbH & Co. Betriebs KG
Sitz: Muenchen - Registergericht: Amtsgericht Muenchen - Handelsregisternummer: HRA 57918
Persoenlich haftender Gesellschafter: Arnold & Richter Cine Technik GmbH
Sitz: Muenchen - Registergericht: Amtsgericht Muenchen - Handelsregisternummer: HRB 54477
Geschaeftsfuehrer: Dr. Michael Neuhaeuser; Stephan Schenk; Walter Trauninger; Markus Zeiler

2020-10-06 11:05:54

by Christian Eggers

[permalink] [raw]
Subject: [PATCH v3 1/3] i2c: imx: Fix reset of I2SR_IAL flag

According to the "VFxxx Controller Reference Manual" (and the comment
block starting at line 97), Vybrid requires writing a one for clearing
an interrupt flag. Syncing the method for clearing I2SR_IIF in
i2c_imx_isr().

Signed-off-by: Christian Eggers <[email protected]>
Cc: [email protected]
---
drivers/i2c/busses/i2c-imx.c | 20 +++++++++++++++-----
1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/drivers/i2c/busses/i2c-imx.c b/drivers/i2c/busses/i2c-imx.c
index 0ab5381aa012..745f4071155a 100644
--- a/drivers/i2c/busses/i2c-imx.c
+++ b/drivers/i2c/busses/i2c-imx.c
@@ -412,6 +412,19 @@ static void i2c_imx_dma_free(struct imx_i2c_struct *i2c_imx)
dma->chan_using = NULL;
}

+static void i2c_imx_clear_irq(struct imx_i2c_struct *i2c_imx, unsigned int bits)
+{
+ unsigned int temp;
+
+ /*
+ * i2sr_clr_opcode is the value to clear all interrupts.
+ * Here we want to clear only <bits>, so we write
+ * ~i2sr_clr_opcode with just <bits> toggled.
+ */
+ temp = ~i2c_imx->hwdata->i2sr_clr_opcode ^ bits;
+ imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2SR);
+}
+
static int i2c_imx_bus_busy(struct imx_i2c_struct *i2c_imx, int for_busy, bool atomic)
{
unsigned long orig_jiffies = jiffies;
@@ -424,8 +437,7 @@ static int i2c_imx_bus_busy(struct imx_i2c_struct *i2c_imx, int for_busy, bool a

/* check for arbitration lost */
if (temp & I2SR_IAL) {
- temp &= ~I2SR_IAL;
- imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2SR);
+ i2c_imx_clear_irq(i2c_imx, I2SR_IAL);
return -EAGAIN;
}

@@ -623,9 +635,7 @@ static irqreturn_t i2c_imx_isr(int irq, void *dev_id)
if (temp & I2SR_IIF) {
/* save status register */
i2c_imx->i2csr = temp;
- temp &= ~I2SR_IIF;
- temp |= (i2c_imx->hwdata->i2sr_clr_opcode & I2SR_IIF);
- imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2SR);
+ i2c_imx_clear_irq(i2c_imx, I2SR_IIF);
wake_up(&i2c_imx->queue);
return IRQ_HANDLED;
}
--
Christian Eggers
Embedded software developer

Arnold & Richter Cine Technik GmbH & Co. Betriebs KG
Sitz: Muenchen - Registergericht: Amtsgericht Muenchen - Handelsregisternummer: HRA 57918
Persoenlich haftender Gesellschafter: Arnold & Richter Cine Technik GmbH
Sitz: Muenchen - Registergericht: Amtsgericht Muenchen - Handelsregisternummer: HRB 54477
Geschaeftsfuehrer: Dr. Michael Neuhaeuser; Stephan Schenk; Walter Trauninger; Markus Zeiler

2020-10-06 11:08:01

by Christian Eggers

[permalink] [raw]
Subject: [PATCH v3 3/3] i2c: imx: Don't generate STOP condition if arbitration has been lost

If arbitration is lost, the master automatically changes to slave mode.
I2SR_IBB may or may not be reset by hardware. Raising a STOP condition
by resetting I2CR_MSTA has no effect and will not clear I2SR_IBB.

So calling i2c_imx_bus_busy() is not required and would busy-wait until
timeout.

Signed-off-by: Christian Eggers <[email protected]>
Cc: [email protected] # Requires trivial backporting, simple remove
# the 3rd argument from the calls to
# i2c_imx_bus_busy().
---
drivers/i2c/busses/i2c-imx.c | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/drivers/i2c/busses/i2c-imx.c b/drivers/i2c/busses/i2c-imx.c
index bf0e94814222..1619c78a17d7 100644
--- a/drivers/i2c/busses/i2c-imx.c
+++ b/drivers/i2c/busses/i2c-imx.c
@@ -615,6 +615,8 @@ static void i2c_imx_stop(struct imx_i2c_struct *i2c_imx, bool atomic)
/* Stop I2C transaction */
dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
+ if (!(temp & I2CR_MSTA))
+ i2c_imx->stopped = 1;
temp &= ~(I2CR_MSTA | I2CR_MTX);
if (i2c_imx->dma)
temp &= ~I2CR_DMAEN;
@@ -778,9 +780,12 @@ static int i2c_imx_dma_read(struct imx_i2c_struct *i2c_imx,
*/
dev_dbg(dev, "<%s> clear MSTA\n", __func__);
temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
+ if (!(temp & I2CR_MSTA))
+ i2c_imx->stopped = 1;
temp &= ~(I2CR_MSTA | I2CR_MTX);
imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
- i2c_imx_bus_busy(i2c_imx, 0, false);
+ if (!i2c_imx->stopped)
+ i2c_imx_bus_busy(i2c_imx, 0, false);
} else {
/*
* For i2c master receiver repeat restart operation like:
@@ -905,9 +910,12 @@ static int i2c_imx_read(struct imx_i2c_struct *i2c_imx, struct i2c_msg *msgs,
dev_dbg(&i2c_imx->adapter.dev,
"<%s> clear MSTA\n", __func__);
temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
+ if (!(temp & I2CR_MSTA))
+ i2c_imx->stopped = 1;
temp &= ~(I2CR_MSTA | I2CR_MTX);
imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
- i2c_imx_bus_busy(i2c_imx, 0, atomic);
+ if (!i2c_imx->stopped)
+ i2c_imx_bus_busy(i2c_imx, 0, atomic);
} else {
/*
* For i2c master receiver repeat restart operation like:
--
Christian Eggers
Embedded software developer

Arnold & Richter Cine Technik GmbH & Co. Betriebs KG
Sitz: Muenchen - Registergericht: Amtsgericht Muenchen - Handelsregisternummer: HRA 57918
Persoenlich haftender Gesellschafter: Arnold & Richter Cine Technik GmbH
Sitz: Muenchen - Registergericht: Amtsgericht Muenchen - Handelsregisternummer: HRB 54477
Geschaeftsfuehrer: Dr. Michael Neuhaeuser; Stephan Schenk; Walter Trauninger; Markus Zeiler

2020-10-06 12:08:10

by David Laight

[permalink] [raw]
Subject: RE: [PATCH v3 1/3] i2c: imx: Fix reset of I2SR_IAL flag

From: Christian Eggers
> Sent: 06 October 2020 11:52
>
> According to the "VFxxx Controller Reference Manual" (and the comment
> block starting at line 97), Vybrid requires writing a one for clearing
> an interrupt flag. Syncing the method for clearing I2SR_IIF in
> i2c_imx_isr().
>
> Signed-off-by: Christian Eggers <[email protected]>
> Cc: [email protected]
> ---
> drivers/i2c/busses/i2c-imx.c | 20 +++++++++++++++-----
> 1 file changed, 15 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/i2c/busses/i2c-imx.c b/drivers/i2c/busses/i2c-imx.c
> index 0ab5381aa012..745f4071155a 100644
> --- a/drivers/i2c/busses/i2c-imx.c
> +++ b/drivers/i2c/busses/i2c-imx.c
> @@ -412,6 +412,19 @@ static void i2c_imx_dma_free(struct imx_i2c_struct *i2c_imx)
> dma->chan_using = NULL;
> }
>
> +static void i2c_imx_clear_irq(struct imx_i2c_struct *i2c_imx, unsigned int bits)
> +{
> + unsigned int temp;
> +
> + /*
> + * i2sr_clr_opcode is the value to clear all interrupts.
> + * Here we want to clear only <bits>, so we write
> + * ~i2sr_clr_opcode with just <bits> toggled.
> + */
> + temp = ~i2c_imx->hwdata->i2sr_clr_opcode ^ bits;
> + imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2SR);
> +}

That looks either wrong or maybe just overcomplicated.
Why isn't:
imx_i2c_write_reg(bits, i2c_imx, IMX_I2C_I2SR);
enough?

More usually you just write back the read value of such
'write 1 to clear' status registers and then act on all
the set bits.
That ensures you clear all interrupts that were pending.

If you need to avoid writes of bits that aren't in the
'clear all interrupts' value then you just need:
bits &= i2c_imx->hwdata->i2sr_clr_opcode;
prior to the write.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)

2020-10-06 12:33:32

by Christian Eggers

[permalink] [raw]
Subject: Re: [PATCH v3 1/3] i2c: imx: Fix reset of I2SR_IAL flag

Hi David,

On Tuesday, 6 October 2020, 14:06:36 CEST, David Laight wrote:
> From: Christian Eggers
> > +static void i2c_imx_clear_irq(struct imx_i2c_struct *i2c_imx, unsigned
> > int bits) +{
> > + unsigned int temp;
> > +
> > + /*
> > + * i2sr_clr_opcode is the value to clear all interrupts.
> > + * Here we want to clear only <bits>, so we write
> > + * ~i2sr_clr_opcode with just <bits> toggled.
> > + */
> > + temp = ~i2c_imx->hwdata->i2sr_clr_opcode ^ bits;
> > + imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2SR);
> > +}
>
> That looks either wrong or maybe just overcomplicated.
Yes, it looks so.

> Why isn't:
> imx_i2c_write_reg(bits, i2c_imx, IMX_I2C_I2SR);
> enough?
i.MX requires W1C and Vybrid requires W0C in order to clear status bits.

> More usually you just write back the read value of such
> 'write 1 to clear' status registers and then act on all
> the set bits.
This pattern has been suggested by Uwe Klein-Koenig. It works because write
access to read-only register bits is ignored, independent whether 0 or 1 is
written. W0C is quite unusual, but I didn't design the hardware... The pattern
ensures that not accidentally more status bits are cleared than desired.

> That ensures you clear all interrupts that were pending.
I think that Uwe's intention was not clearing bits which are not handled at
this place. Otherwise events may get lost.

> If you need to avoid writes of bits that aren't in the
> 'clear all interrupts' value then you just need:
> bits &= i2c_imx->hwdata->i2sr_clr_opcode;
> prior to the write.
I think this wouldn't fit the W0C case for Vybrid.

Best regards
Christian



2020-10-06 12:49:49

by Uwe Kleine-König

[permalink] [raw]
Subject: Re: [PATCH v3 1/3] i2c: imx: Fix reset of I2SR_IAL flag

On Tue, Oct 06, 2020 at 12:06:36PM +0000, David Laight wrote:
> From: Christian Eggers
> > Sent: 06 October 2020 11:52
> >
> > According to the "VFxxx Controller Reference Manual" (and the comment
> > block starting at line 97), Vybrid requires writing a one for clearing
> > an interrupt flag. Syncing the method for clearing I2SR_IIF in
> > i2c_imx_isr().
> >
> > Signed-off-by: Christian Eggers <[email protected]>
> > Cc: [email protected]
> > ---
> > drivers/i2c/busses/i2c-imx.c | 20 +++++++++++++++-----
> > 1 file changed, 15 insertions(+), 5 deletions(-)
> >
> > diff --git a/drivers/i2c/busses/i2c-imx.c b/drivers/i2c/busses/i2c-imx.c
> > index 0ab5381aa012..745f4071155a 100644
> > --- a/drivers/i2c/busses/i2c-imx.c
> > +++ b/drivers/i2c/busses/i2c-imx.c
> > @@ -412,6 +412,19 @@ static void i2c_imx_dma_free(struct imx_i2c_struct *i2c_imx)
> > dma->chan_using = NULL;
> > }
> >
> > +static void i2c_imx_clear_irq(struct imx_i2c_struct *i2c_imx, unsigned int bits)
> > +{
> > + unsigned int temp;
> > +
> > + /*
> > + * i2sr_clr_opcode is the value to clear all interrupts.
> > + * Here we want to clear only <bits>, so we write
> > + * ~i2sr_clr_opcode with just <bits> toggled.
> > + */
> > + temp = ~i2c_imx->hwdata->i2sr_clr_opcode ^ bits;
> > + imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2SR);
> > +}
>
> That looks either wrong or maybe just overcomplicated.
> Why isn't:
> imx_i2c_write_reg(bits, i2c_imx, IMX_I2C_I2SR);
> enough?

Your question suggests you either didn't read the comment or the comment
is not good enough. Maybe once you understood the complication (see
Christian's mail) you could suggest a better wording? Maybe we have to
mention that this handles both W1C and W0C.

Best regards
Uwe

--
Pengutronix e.K. | Uwe Kleine-K?nig |
Industrial Linux Solutions | https://www.pengutronix.de/ |


Attachments:
(No filename) (1.90 kB)
signature.asc (499.00 B)
Download all attachments

2020-10-06 12:54:12

by David Laight

[permalink] [raw]
Subject: RE: [PATCH v3 1/3] i2c: imx: Fix reset of I2SR_IAL flag

From: Uwe Kleine-König
> Sent: 06 October 2020 13:46
...
> > > +static void i2c_imx_clear_irq(struct imx_i2c_struct *i2c_imx, unsigned int bits)
> > > +{
> > > + unsigned int temp;
> > > +
> > > + /*
> > > + * i2sr_clr_opcode is the value to clear all interrupts.
> > > + * Here we want to clear only <bits>, so we write
> > > + * ~i2sr_clr_opcode with just <bits> toggled.
> > > + */
> > > + temp = ~i2c_imx->hwdata->i2sr_clr_opcode ^ bits;
> > > + imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2SR);
> > > +}
> >
> > That looks either wrong or maybe just overcomplicated.
> > Why isn't:
> > imx_i2c_write_reg(bits, i2c_imx, IMX_I2C_I2SR);
> > enough?
>
> Your question suggests you either didn't read the comment or the comment
> is not good enough. Maybe once you understood the complication (see
> Christian's mail) you could suggest a better wording? Maybe we have to
> mention that this handles both W1C and W0C.

Yes, the comment should just say that some devices are W1C and
other W0C.
It isn't obvious from that code fragment at all.

Now for the 3rd variant which zeros the bits when they are read :-)

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)