Skip to contentSkip to conversation

i2c: imx: Fix handling of arbitration loss

PATCH Superseded Christian Eggers 4 participants 17 messages Oct 2, 2020 – Oct 6, 2020

Subsystems (4)

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v2 cover 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

#1 lore raw

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().

---
 drivers/i2c/busses/i2c-imx.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
drivers/i2c/busses/i2c-imx.c +12 -3
index 0ab5381aa012..34648df7f1a6 100644
@@ -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;
Signature · 8 lines
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
Tested-by on v2 1/3
#2 lore raw

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

Quoted 10 lines · Christian Eggers

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().

---
 drivers/i2c/busses/i2c-imx.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

Tested (not extensively) on Vybrid VF500 (Toradex VF50):

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

#3 lore raw

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

In reply to Christian Eggers
Quoted 18 lines · Christian Eggers

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().

---
 drivers/i2c/busses/i2c-imx.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
drivers/i2c/busses/i2c-imx.c +0 -0
index 0ab5381aa012..34648df7f1a6 100644
@@ -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 */

drivers/i2c/busses/i2c-imx.c static int i2c_imx_bus_busy(struct imx_i2c_struct *i2c_imx, int for_busy, bool a · line 434

Quoted 10 lines
drivers/i2c/busses/i2c-imx.c +6 -1
 		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

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

v3 cover i2c: imx: Fix handling of arbitration loss

Quoted 8 lines · Uwe Kleine-König

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

#5 lore raw

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().

---
 drivers/i2c/busses/i2c-imx.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
drivers/i2c/busses/i2c-imx.c +15 -5
index 0ab5381aa012..745f4071155a 100644
@@ -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;
 	}
Signature · 8 lines
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
#6 lore raw

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

From: Christian Eggers

Quoted 23 lines

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().

---
 drivers/i2c/busses/i2c-imx.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
drivers/i2c/busses/i2c-imx.c +2 -0
index 0ab5381aa012..745f4071155a 100644
@@ -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)
+{

drivers/i2c/busses/i2c-imx.c static void i2c_imx_dma_free(struct imx_i2c_struct *i2c_imx) · line 426

Quoted 10 lines
drivers/i2c/busses/i2c-imx.c +10 -0
+	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)

#7 lore raw

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

Hi David,

Quoted 15 lines · David Laight

From: Christian Eggers

drivers/i2c/busses/i2c-imx.c static void i2c_imx_dma_free(struct imx_i2c_struct *i2c_imx) · line 426

Quoted 12 lines

+static void i2c_imx_clear_irq(struct imx_i2c_struct *i2c_imx, unsigned int bits) +{

drivers/i2c/busses/i2c-imx.c +10 -0
+	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.

Quoted 3 lines

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.

Quoted 3 lines

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.

Quoted 1 line

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.

Quoted 4 lines

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

#8 lore raw

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

In reply to David Laight
Quoted 37 lines · David Laight

From: Christian Eggers

Quoted 23 lines

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().

---
 drivers/i2c/busses/i2c-imx.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
drivers/i2c/busses/i2c-imx.c +2 -0
index 0ab5381aa012..745f4071155a 100644
@@ -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)
+{

drivers/i2c/busses/i2c-imx.c static void i2c_imx_dma_free(struct imx_i2c_struct *i2c_imx) · line 426

Quoted 10 lines
drivers/i2c/busses/i2c-imx.c +10 -0
+	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:

Quoted 2 lines
	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

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

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

From: Uwe Kleine-König

Quoted 1 line

Sent: 06 October 2020 13:46

...

Quoted 18 lines
Quoted 15 lines

drivers/i2c/busses/i2c-imx.c static void i2c_imx_dma_free(struct imx_i2c_struct *i2c_imx) · line 426

Quoted 12 lines
drivers/i2c/busses/i2c-imx.c +12 -0
+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:

Quoted 2 lines
	imx_i2c_write_reg(bits, i2c_imx, IMX_I2C_I2SR);

enough?

Quoted 4 lines

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)

#10 lore raw

PATCH v3 2/3 i2c: imx: Check for I2SR_IAL after every byte

In reply to Christian Eggers

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.

---
 drivers/i2c/busses/i2c-imx.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
drivers/i2c/busses/i2c-imx.c +10 -0
index 745f4071155a..bf0e94814222 100644
@@ -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;
Signature · 8 lines
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
#11 lore raw

PATCH v3 3/3 i2c: imx: Don't generate STOP condition if arbitration has been lost

In reply to Christian Eggers

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.

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(-)
drivers/i2c/busses/i2c-imx.c +10 -2
index bf0e94814222..1619c78a17d7 100644
@@ -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:
Signature · 8 lines
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
#12 lore raw

PATCH v2 2/3 i2c: imx: Check for I2SR_IAL after every byte

In reply to Christian Eggers

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.

---
 drivers/i2c/busses/i2c-imx.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
drivers/i2c/busses/i2c-imx.c +18 -0
index 34648df7f1a6..1db1e2f5296e 100644
@@ -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;
Signature · 8 lines
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
Tested-by on v2 2/3
#13 lore raw

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

Quoted 6 lines · Christian Eggers

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.

Quoted 6 lines
---
 drivers/i2c/busses/i2c-imx.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

Tested (not extensively) on Vybrid VF500 (Toradex VF50):

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

#14 lore raw

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

Quoted 4 lines · Krzysztof Kozlowski

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.

Quoted 2 lines

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.

Quoted 2 lines

Best regards,
Krzysztof

Best regards
Christian

#15 lore raw

PATCH v2 3/3 i2c: imx: Don't generate STOP condition if arbitration has been lost

In reply to Christian Eggers

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.

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(-)
drivers/i2c/busses/i2c-imx.c +10 -2
index 1db1e2f5296e..ced9cb3a2665 100644
@@ -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:
Signature · 8 lines
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
Tested-by on v2 3/3
#16 lore raw

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

Quoted 7 lines · Christian Eggers

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.

Quoted 8 lines
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):

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