Skip to contentSkip to conversation

5200: improve i2c bus error recovery

PATCH Superseded Albrecht Dreß 5 participants 7 messages Jan 22, 2010 – Feb 16, 2010 2 messages not in the archive

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 5200: improve i2c bus error recovery

Improve the recovery of the MPC5200B's I2C bus from errors like bus hangs.

---

This patch introduces several improvements to the MPC5200B's I2C driver as to improve the recovery from error conditions I encountered when testing a custom board with several I2C devices attached (eeprom, io expander, rtc, sensors). The error conditions included cases where the bus if logic of one slave apparently went south, blocking the bus completely.

My fixes include:
1. make the bus timeout configurable in fsl_i2c_probe(); the default of one second is *way* too long for my use case;
2. if a timeout condition occurs in mpc_xfer(), mpc_i2c_fixup() the bus if *any* of the CF, BB and RXAK flags in the MSR is 1. I actually saw different combinations with hangs, not only all three set;
3. improve the fixup procedure by calculating the timing needed from the real (configured) bus clock, calculated in mpc_i2c_setclock_52xx(). Furthermore, I issue 9 instead of one cycle, as I experienced cases where the single one is not enough (found this tip in a forum). As a side effect, the new scheme needs only 81us @375kHz bus clock instead of 150us. I recorded waveforms for 18.4kHz, 85.9kHz and 375kHz, all looking fine, which I can provide if anyone is interested.

Open questions:
- is the approach correct at all, in particular the interpretation of the flags (#2)?
- could this code also be used on non-5200 processors?

drivers/i2c/busses/i2c-mpc.c +51 -17
@@ -59,6 +59,7 @@ struct mpc_i2c {
 	wait_queue_head_t queue;
 	struct i2c_adapter adap;
 	int irq;
+	u32 real_clk;
 };
 
 struct mpc_i2c_divider {
@@ -97,16 +98,32 @@ static irqreturn_t mpc_i2c_isr(int irq, 
  */
 static void mpc_i2c_fixup(struct mpc_i2c *i2c)
 {
-	writeccr(i2c, 0);
-	udelay(30);
-	writeccr(i2c, CCR_MEN);
-	udelay(30);
-	writeccr(i2c, CCR_MSTA | CCR_MTX);
-	udelay(30);
-	writeccr(i2c, CCR_MSTA | CCR_MTX | CCR_MEN);
-	udelay(30);
-	writeccr(i2c, CCR_MEN);
-	udelay(30);
+	if (i2c->real_clk == 0) {
+		writeccr(i2c, 0);
+		udelay(30);
+		writeccr(i2c, CCR_MEN);
+		udelay(30);
+		writeccr(i2c, CCR_MSTA | CCR_MTX);
+		udelay(30);
+		writeccr(i2c, CCR_MSTA | CCR_MTX | CCR_MEN);
+		udelay(30);
+		writeccr(i2c, CCR_MEN);
+		udelay(30);
+	} else {
+		int k;
+		u32 delay_val = 1000000 / i2c->real_clk + 1;
+
+		if (delay_val < 2)
+			delay_val = 2;
+
+		for (k = 9; k; k--) {
+			writeccr(i2c, 0);
+			writeccr(i2c, CCR_MSTA | CCR_MTX | CCR_MEN);
+			udelay(delay_val);
+			writeccr(i2c, CCR_MEN);
+			udelay(delay_val << 1);
+		}
+	}
 }
 
 static int i2c_wait(struct mpc_i2c *i2c, unsigned timeout, int writing)
@@ -186,15 +203,18 @@ static const struct mpc_i2c_divider mpc_
 	{10240, 0x9d}, {12288, 0x9e}, {15360, 0x9f}
 };
 
-int mpc_i2c_get_fdr_52xx(struct device_node *node, u32 clock, int prescaler)
+int mpc_i2c_get_fdr_52xx(struct device_node *node, u32 clock, int prescaler,
+			 u32 *real_clk)
 {
 	const struct mpc_i2c_divider *div = NULL;
 	unsigned int pvr = mfspr(SPRN_PVR);
 	u32 divider;
 	int i;
 
-	if (!clock)
+	if (!clock) {
+		*real_clk = 0;
 		return -EINVAL;
+	}
 
 	/* Determine divider value */
 	divider = mpc5xxx_get_bus_frequency(node) / clock;
@@ -212,7 +232,8 @@ int mpc_i2c_get_fdr_52xx(struct device_n
 			break;
 	}
 
-	return div ? (int)div->fdr : -EINVAL;
+	*real_clk = mpc5xxx_get_bus_frequency(node) / div->divider;
+	return (int)div->fdr;
 }
 
 static void mpc_i2c_setclock_52xx(struct device_node *node,
@@ -221,13 +242,14 @@ static void mpc_i2c_setclock_52xx(struct
 {
 	int ret, fdr;
 
-	ret = mpc_i2c_get_fdr_52xx(node, clock, prescaler);
+	ret = mpc_i2c_get_fdr_52xx(node, clock, prescaler, &i2c->real_clk);
 	fdr = (ret >= 0) ? ret : 0x3f; /* backward compatibility */
 
 	writeb(fdr & 0xff, i2c->base + MPC_I2C_FDR);
 
 	if (ret >= 0)
-		dev_info(i2c->dev, "clock %d Hz (fdr=%d)\n", clock, fdr);
+		dev_info(i2c->dev, "clock %u Hz (fdr=%d)\n", i2c->real_clk,
+			 fdr);
 }
 #else /* !CONFIG_PPC_MPC52xx */
 static void mpc_i2c_setclock_52xx(struct device_node *node,
@@ -446,10 +468,14 @@ static int mpc_xfer(struct i2c_adapter *
 			return -EINTR;
 		}
 		if (time_after(jiffies, orig_jiffies + HZ)) {
+			u8 status = readb(i2c->base + MPC_I2C_SR);
+
 			dev_dbg(i2c->dev, "timeout\n");
-			if (readb(i2c->base + MPC_I2C_SR) ==
-			    (CSR_MCF | CSR_MBB | CSR_RXAK))
+			if ((status & (CSR_MCF | CSR_MBB | CSR_RXAK)) != 0) {
+				writeb(status & ~CSR_MAL,
+				       i2c->base + MPC_I2C_SR);
 				mpc_i2c_fixup(i2c);
+			}
 			return -EIO;
 		}
 		schedule();
@@ -540,6 +566,14 @@ static int __devinit fsl_i2c_probe(struc
 		}
 	}
 
+	prop = of_get_property(op->node, "timeout", &plen);
+	if (prop && plen == sizeof(u32)) {
+		mpc_ops.timeout = *prop * HZ / 1000000;
+		if (mpc_ops.timeout < 5)
+			mpc_ops.timeout = 5;
+	}
+	dev_info(i2c->dev, "timeout %u us\n", mpc_ops.timeout * 1000000 / HZ);
+
 	dev_set_drvdata(&op->dev, i2c);
 
 	i2c->adap = mpc_ops;
#1 lore raw
Quoted 2 lines · Albrecht Dreß

Improve the recovery of the MPC5200B's I2C bus from errors like bus hangs.

This is very sparse comapred to the large comment below the --- line, maybe some more description should be living up here.

Is thios a candidate for an -rc or should it be left to merge window?

Quoted 163 lines

---

This patch introduces several improvements to the MPC5200B's I2C driver as to improve the recovery from error conditions I encountered when testing a custom board with several I2C devices attached (eeprom, io expander, rtc, sensors). The error conditions included cases where the bus if logic of one slave apparently went south, blocking the bus completely.

My fixes include:
1. make the bus timeout configurable in fsl_i2c_probe(); the default of one second is *way* too long for my use case;
2. if a timeout condition occurs in mpc_xfer(), mpc_i2c_fixup() the bus if *any* of the CF, BB and RXAK flags in the MSR is 1. I actually saw different combinations with hangs, not only all three set;
3. improve the fixup procedure by calculating the timing needed from the real (configured) bus clock, calculated in mpc_i2c_setclock_52xx(). Furthermore, I issue 9 instead of one cycle, as I experienced cases where the single one is not enough (found this tip in a forum). As a side effect, the new scheme needs only 81us @375kHz bus clock instead of 150us. I recorded waveforms for 18.4kHz, 85.9kHz and 375kHz, all looking fine, which I can provide if anyone is interested.

Open questions:
- is the approach correct at all, in particular the interpretation of the flags (#2)?
- could this code also be used on non-5200 processors?

drivers/i2c/busses/i2c-mpc.c +51 -17
@@ -59,6 +59,7 @@ struct mpc_i2c {
 	wait_queue_head_t queue;
 	struct i2c_adapter adap;
 	int irq;
+	u32 real_clk;
 };
 
 struct mpc_i2c_divider {
@@ -97,16 +98,32 @@ static irqreturn_t mpc_i2c_isr(int irq, 
  */
 static void mpc_i2c_fixup(struct mpc_i2c *i2c)
 {
-	writeccr(i2c, 0);
-	udelay(30);
-	writeccr(i2c, CCR_MEN);
-	udelay(30);
-	writeccr(i2c, CCR_MSTA | CCR_MTX);
-	udelay(30);
-	writeccr(i2c, CCR_MSTA | CCR_MTX | CCR_MEN);
-	udelay(30);
-	writeccr(i2c, CCR_MEN);
-	udelay(30);
+	if (i2c->real_clk == 0) {
+		writeccr(i2c, 0);
+		udelay(30);
+		writeccr(i2c, CCR_MEN);
+		udelay(30);
+		writeccr(i2c, CCR_MSTA | CCR_MTX);
+		udelay(30);
+		writeccr(i2c, CCR_MSTA | CCR_MTX | CCR_MEN);
+		udelay(30);
+		writeccr(i2c, CCR_MEN);
+		udelay(30);
+	} else {
+		int k;
+		u32 delay_val = 1000000 / i2c->real_clk + 1;
+
+		if (delay_val < 2)
+			delay_val = 2;
+
+		for (k = 9; k; k--) {
+			writeccr(i2c, 0);
+			writeccr(i2c, CCR_MSTA | CCR_MTX | CCR_MEN);
+			udelay(delay_val);
+			writeccr(i2c, CCR_MEN);
+			udelay(delay_val << 1);
+		}
+	}
 }
 
 static int i2c_wait(struct mpc_i2c *i2c, unsigned timeout, int writing)
@@ -186,15 +203,18 @@ static const struct mpc_i2c_divider mpc_
 	{10240, 0x9d}, {12288, 0x9e}, {15360, 0x9f}
 };
 
-int mpc_i2c_get_fdr_52xx(struct device_node *node, u32 clock, int prescaler)
+int mpc_i2c_get_fdr_52xx(struct device_node *node, u32 clock, int prescaler,
+			 u32 *real_clk)
 {
 	const struct mpc_i2c_divider *div = NULL;
 	unsigned int pvr = mfspr(SPRN_PVR);
 	u32 divider;
 	int i;
 
-	if (!clock)
+	if (!clock) {
+		*real_clk = 0;
 		return -EINVAL;
+	}
 
 	/* Determine divider value */
 	divider = mpc5xxx_get_bus_frequency(node) / clock;
@@ -212,7 +232,8 @@ int mpc_i2c_get_fdr_52xx(struct device_n
 			break;
 	}
 
-	return div ? (int)div->fdr : -EINVAL;
+	*real_clk = mpc5xxx_get_bus_frequency(node) / div->divider;
+	return (int)div->fdr;
 }
 
 static void mpc_i2c_setclock_52xx(struct device_node *node,
@@ -221,13 +242,14 @@ static void mpc_i2c_setclock_52xx(struct
 {
 	int ret, fdr;
 
-	ret = mpc_i2c_get_fdr_52xx(node, clock, prescaler);
+	ret = mpc_i2c_get_fdr_52xx(node, clock, prescaler, &i2c->real_clk);
 	fdr = (ret >= 0) ? ret : 0x3f; /* backward compatibility */
 
 	writeb(fdr & 0xff, i2c->base + MPC_I2C_FDR);
 
 	if (ret >= 0)
-		dev_info(i2c->dev, "clock %d Hz (fdr=%d)\n", clock, fdr);
+		dev_info(i2c->dev, "clock %u Hz (fdr=%d)\n", i2c->real_clk,
+			 fdr);
 }
 #else /* !CONFIG_PPC_MPC52xx */
 static void mpc_i2c_setclock_52xx(struct device_node *node,
@@ -446,10 +468,14 @@ static int mpc_xfer(struct i2c_adapter *
 			return -EINTR;
 		}
 		if (time_after(jiffies, orig_jiffies + HZ)) {
+			u8 status = readb(i2c->base + MPC_I2C_SR);
+
 			dev_dbg(i2c->dev, "timeout\n");
-			if (readb(i2c->base + MPC_I2C_SR) ==
-			    (CSR_MCF | CSR_MBB | CSR_RXAK))
+			if ((status & (CSR_MCF | CSR_MBB | CSR_RXAK)) != 0) {
+				writeb(status & ~CSR_MAL,
+				       i2c->base + MPC_I2C_SR);
 				mpc_i2c_fixup(i2c);
+			}
 			return -EIO;
 		}
 		schedule();
@@ -540,6 +566,14 @@ static int __devinit fsl_i2c_probe(struc
 		}
 	}
 
+	prop = of_get_property(op->node, "timeout", &plen);
+	if (prop && plen == sizeof(u32)) {
+		mpc_ops.timeout = *prop * HZ / 1000000;
+		if (mpc_ops.timeout < 5)
+			mpc_ops.timeout = 5;
+	}
+	dev_info(i2c->dev, "timeout %u us\n", mpc_ops.timeout * 1000000 / HZ);
+
 	dev_set_drvdata(&op->dev, i2c);
 
 	i2c->adap = mpc_ops;
Signature · 4 lines
Ben

Q:      What's a light-year?
A:      One-third less calories than a regular year.
#2 lore raw

Am 25.01.10 05:06 schrieb(en) Ben Dooks:

Quoted 4 lines · Albrecht Dreß
Quoted 1 line

Improve the recovery of the MPC5200B's I2C bus from errors like bus hangs.

This is very sparse comapred to the large comment below the --- line, maybe some more description should be living up here.

Hmm, that was my interpretation of #15 in Documentation/SubmittingPatches... ;-) Maybe it should read

<snip>
Improve the recovery of the MPC5200B's I2C bus from errors like bus hangs. This includes making the bus timeout configurable, a better detection of cases where the bus has to be "fixed" after a timeout, and a more thorough fixup sequence. </snip>

Quoted 1 line

Is thios a candidate for an -rc or should it be left to merge window?

Well, basically it was a rfc. I apparently need it on my 5200B board, but I hoped to get some more insight from the Freescale/I2C gurus (see "open questions" in the post). Thus merge window, IMHO...

Thanks, Albrecht.

Quoted 33 lines
Quoted 33 lines

---

This patch introduces several improvements to the MPC5200B's I2C driver as to improve the recovery from error conditions I encountered when testing a custom board with several I2C devices attached (eeprom, io expander, rtc, sensors). The error conditions included cases where the bus if logic of one slave apparently went south, blocking the bus completely.

My fixes include:
1. make the bus timeout configurable in fsl_i2c_probe(); the default of one second is *way* too long for my use case;
2. if a timeout condition occurs in mpc_xfer(), mpc_i2c_fixup() the bus if *any* of the CF, BB and RXAK flags in the MSR is 1. I actually saw different combinations with hangs, not only all three set;
3. improve the fixup procedure by calculating the timing needed from the real (configured) bus clock, calculated in mpc_i2c_setclock_52xx(). Furthermore, I issue 9 instead of one cycle, as I experienced cases where the single one is not enough (found this tip in a forum). As a side effect, the new scheme needs only 81us @375kHz bus clock instead of 150us. I recorded waveforms for 18.4kHz, 85.9kHz and 375kHz, all looking fine, which I can provide if anyone is interested.

Open questions:
- is the approach correct at all, in particular the interpretation of the flags (#2)?
- could this code also be used on non-5200 processors?

drivers/i2c/busses/i2c-mpc.c +0 -0

[snip]

_______________________________________________
Linuxppc-dev mailing list
[email protected]
https://lists.ozlabs.org/listinfo/linuxppc-dev

#3 lore raw
In reply to Albrecht Dreß

Hi Albrecht,

Comments below.

Quoted 14 lines · Albrecht Dreß

Improve the recovery of the MPC5200B's I2C bus from errors like bus hangs.

---

This patch introduces several improvements to the MPC5200B's I2C driver as to improve the recovery from error conditions I encountered when testing a custom board with several I2C devices attached (eeprom, io expander, rtc, sensors).  The error conditions included cases where the bus if logic of one slave apparently went south, blocking the bus completely.

Quoted 13 lines

My fixes include:
1. make the bus timeout configurable in fsl_i2c_probe(); the default of one second is *way* too long for my use case;
2. if a timeout condition occurs in mpc_xfer(), mpc_i2c_fixup() the bus if *any* of the CF, BB and RXAK flags in the MSR is 1.  I actually saw different combinations with hangs, not only all three set;
3. improve the fixup procedure by calculating the timing needed from the real (configured) bus clock, calculated in mpc_i2c_setclock_52xx(). Furthermore, I issue 9 instead of one cycle, as I experienced cases where the single one is not enough (found this tip in a forum).  As a side effect, the new scheme needs only 81us @375kHz bus clock instead of 150us.  I recorded waveforms for 18.4kHz, 85.9kHz and 375kHz, all looking fine, which I can provide if anyone is interested.

These are three separate fixes. Ideally you should submit them in separate patches to make it easy on poor old reviewers like me. And, as Ben mentions, this descriptions should be above the '---' line so it appears in the commit text.

Quoted 46 lines

Open questions:
- is the approach correct at all, in particular the interpretation of the flags (#2)?
- could this code also be used on non-5200 processors?

drivers/i2c/busses/i2c-mpc.c   2010-01-22 16:05:13.000000000 +0100 renamed from drivers/i2c/busses/i2c-mpc.c      2009-12-03 04:51:21.000000000 +0100 +17 -10
@@ -59,6 +59,7 @@ struct mpc_i2c {
        wait_queue_head_t queue;
        struct i2c_adapter adap;
        int irq;
+       u32 real_clk;
  };
 
  struct mpc_i2c_divider {
@@ -97,16 +98,32 @@ static irqreturn_t mpc_i2c_isr(int irq,
  */
  static void mpc_i2c_fixup(struct mpc_i2c *i2c)
  {
-       writeccr(i2c, 0);
-       udelay(30);
-       writeccr(i2c, CCR_MEN);
-       udelay(30);
-       writeccr(i2c, CCR_MSTA | CCR_MTX);
-       udelay(30);
-       writeccr(i2c, CCR_MSTA | CCR_MTX | CCR_MEN);
-       udelay(30);
-       writeccr(i2c, CCR_MEN);
-       udelay(30);
+       if (i2c->real_clk == 0) {
+               writeccr(i2c, 0);
+               udelay(30);
+               writeccr(i2c, CCR_MEN);
+               udelay(30);
+               writeccr(i2c, CCR_MSTA | CCR_MTX);
+               udelay(30);
+               writeccr(i2c, CCR_MSTA | CCR_MTX | CCR_MEN);
+               udelay(30);
+               writeccr(i2c, CCR_MEN);
+               udelay(30);
+       } else {
+               int k;
+               u32 delay_val = 1000000 / i2c->real_clk + 1;
+
+               if (delay_val < 2)

drivers/i2c/busses/i2c-mpc.c   2010-01-22 16:05:13.000000000 +0100 static irqreturn_t mpc_i2c_isr(int irq, · line 126

Quoted 10 lines
drivers/i2c/busses/i2c-mpc.c   2010-01-22 16:05:13.000000000 +0100 renamed from drivers/i2c/busses/i2c-mpc.c      2009-12-03 04:51:21.000000000 +0100 +10 -0
+                       delay_val = 2;
+
+               for (k = 9; k; k--) {
+                       writeccr(i2c, 0);
+                       writeccr(i2c, CCR_MSTA | CCR_MTX | CCR_MEN);
+                       udelay(delay_val);
+                       writeccr(i2c, CCR_MEN);
+                       udelay(delay_val << 1);
+               }
+       }

This doesn't look right. Why is the old code being preserved? Isn't it not as reliable? It looks to me that the new block should be the only path, with delay_val getting hard set to a sane value if real_clk == 0. This approach looks to add complexity to the driver without a reason other than fear it *might* breaking something.

If the new code is better, then be strong, stand tall, and say in a loud voice, "this old code is crap. The new stuff is much better."

g.

Quoted 89 lines
drivers/i2c/busses/i2c-mpc.c   2010-01-22 16:05:13.000000000 +0100 renamed from drivers/i2c/busses/i2c-mpc.c      2009-12-03 04:51:21.000000000 +0100 +8 -4
  }
 
  static int i2c_wait(struct mpc_i2c *i2c, unsigned timeout, int writing)
@@ -186,15 +203,18 @@ static const struct mpc_i2c_divider mpc_
        {10240, 0x9d}, {12288, 0x9e}, {15360, 0x9f}
  };
 
-int mpc_i2c_get_fdr_52xx(struct device_node *node, u32 clock, int prescaler)
+int mpc_i2c_get_fdr_52xx(struct device_node *node, u32 clock, int prescaler,
+                        u32 *real_clk)
  {
        const struct mpc_i2c_divider *div = NULL;
        unsigned int pvr = mfspr(SPRN_PVR);
        u32 divider;
        int i;
 
-       if (!clock)
+       if (!clock) {
+               *real_clk = 0;
                return -EINVAL;
+       }
 
        /* Determine divider value */
        divider = mpc5xxx_get_bus_frequency(node) / clock;
@@ -212,7 +232,8 @@ int mpc_i2c_get_fdr_52xx(struct device_n
                        break;
        }
 
-       return div ? (int)div->fdr : -EINVAL;
+       *real_clk = mpc5xxx_get_bus_frequency(node) / div->divider;
+       return (int)div->fdr;
  }
 
  static void mpc_i2c_setclock_52xx(struct device_node *node,
@@ -221,13 +242,14 @@ static void mpc_i2c_setclock_52xx(struct
  {
        int ret, fdr;
 
-       ret = mpc_i2c_get_fdr_52xx(node, clock, prescaler);
+       ret = mpc_i2c_get_fdr_52xx(node, clock, prescaler, &i2c->real_clk);

       fdr = (ret >= 0) ? ret : 0x3f; /* backward compatibility */

       writeb(fdr & 0xff, i2c->base + MPC_I2C_FDR);

       if (ret >= 0)
-               dev_info(i2c->dev, "clock %d Hz (fdr=%d)\n", clock, fdr);
+               dev_info(i2c->dev, "clock %u Hz (fdr=%d)\n", i2c->real_clk,
+                        fdr);
 }
 #else /* !CONFIG_PPC_MPC52xx */
 static void mpc_i2c_setclock_52xx(struct device_node *node,

drivers/i2c/busses/i2c-mpc.c   2010-01-22 16:05:13.000000000 +0100 renamed from drivers/i2c/busses/i2c-mpc.c      2009-12-03 04:51:21.000000000 +0100 +14 -2
@@ -446,10 +468,14 @@ static int mpc_xfer(struct i2c_adapter *
                        return -EINTR;
                }
                if (time_after(jiffies, orig_jiffies + HZ)) {
+                       u8 status = readb(i2c->base + MPC_I2C_SR);
+
                        dev_dbg(i2c->dev, "timeout\n");
-                       if (readb(i2c->base + MPC_I2C_SR) ==
-                           (CSR_MCF | CSR_MBB | CSR_RXAK))
+                       if ((status & (CSR_MCF | CSR_MBB | CSR_RXAK)) != 0) {
+                               writeb(status & ~CSR_MAL,
+                                      i2c->base + MPC_I2C_SR);
                                mpc_i2c_fixup(i2c);
+                       }
                        return -EIO;
                }
                schedule();
@@ -540,6 +566,14 @@ static int __devinit fsl_i2c_probe(struc
                }
        }
 
+       prop = of_get_property(op->node, "timeout", &plen);
+       if (prop && plen == sizeof(u32)) {
+               mpc_ops.timeout = *prop * HZ / 1000000;
+               if (mpc_ops.timeout < 5)
+                       mpc_ops.timeout = 5;
+       }
+       dev_info(i2c->dev, "timeout %u us\n", mpc_ops.timeout * 1000000 / HZ);
+
        dev_set_drvdata(&op->dev, i2c);
 
        i2c->adap = mpc_ops;

_______________________________________________
devicetree-discuss mailing list devicetree-discuss-uLR06cmDAlY/[email protected]
https://lists.ozlabs.org/listinfo/devicetree-discuss

Signature · 2 lines
Grant Likely, B.Sc., P.Eng.
Secret Lab Technologies Ltd.
#4 lore raw
In reply to Albrecht Dreß
Quoted 14 lines · Albrecht Dreß

Improve the recovery of the MPC5200B's I2C bus from errors like bus hangs.

---

This patch introduces several improvements to the MPC5200B's I2C driver as to improve the recovery from error conditions I encountered when testing a custom board with several I2C devices attached (eeprom, io expander, rtc, sensors). The error conditions included cases where the bus if logic of one slave apparently went south, blocking the bus completely.

Quoted 6 lines

My fixes include:
1. make the bus timeout configurable in fsl_i2c_probe(); the default of one second is *way* too long for my use case;
2. if a timeout condition occurs in mpc_xfer(), mpc_i2c_fixup() the bus if *any* of the CF, BB and RXAK flags in the MSR is 1. I actually saw different combinations with hangs, not only all three set;

Hello Albrecht,

I see this exact hang on a MPC8349EA board. I poll my i2c sensors every 500ms, and it takes around 12 hours to produce a hang. The usual hang has (CF | BB) set, however I have seen a hang with just BB (only once so far in about 2 weeks).

I think the fixup should be run on 8349 as well, if not all processors. I'm happy to test patches. I have a way to reliably trigger a lockup, using another master on the i2c bus.

Quoted 55 lines

3. improve the fixup procedure by calculating the timing needed from the real (configured) bus clock, calculated in mpc_i2c_setclock_52xx(). Furthermore, I issue 9 instead of one cycle, as I experienced cases where the single one is not enough (found this tip in a forum). As a side effect, the new scheme needs only 81us @375kHz bus clock instead of 150us. I recorded waveforms for 18.4kHz, 85.9kHz and 375kHz, all looking fine, which I can provide if anyone is interested.

Open questions:
- is the approach correct at all, in particular the interpretation of the flags (#2)?
- could this code also be used on non-5200 processors?

drivers/i2c/busses/i2c-mpc.c +19 -10
@@ -59,6 +59,7 @@ struct mpc_i2c {
 	wait_queue_head_t queue;
 	struct i2c_adapter adap;
 	int irq;
+	u32 real_clk;
 };
 
 struct mpc_i2c_divider {
@@ -97,16 +98,32 @@ static irqreturn_t mpc_i2c_isr(int irq, 
  */
 static void mpc_i2c_fixup(struct mpc_i2c *i2c)
 {
-	writeccr(i2c, 0);
-	udelay(30);
-	writeccr(i2c, CCR_MEN);
-	udelay(30);
-	writeccr(i2c, CCR_MSTA | CCR_MTX);
-	udelay(30);
-	writeccr(i2c, CCR_MSTA | CCR_MTX | CCR_MEN);
-	udelay(30);
-	writeccr(i2c, CCR_MEN);
-	udelay(30);
+	if (i2c->real_clk == 0) {
+		writeccr(i2c, 0);
+		udelay(30);
+		writeccr(i2c, CCR_MEN);
+		udelay(30);
+		writeccr(i2c, CCR_MSTA | CCR_MTX);
+		udelay(30);
+		writeccr(i2c, CCR_MSTA | CCR_MTX | CCR_MEN);
+		udelay(30);
+		writeccr(i2c, CCR_MEN);
+		udelay(30);
+	} else {
+		int k;
+		u32 delay_val = 1000000 / i2c->real_clk + 1;
+
+		if (delay_val < 2)
+			delay_val = 2;
+

drivers/i2c/busses/i2c-mpc.c static irqreturn_t mpc_i2c_isr(int irq, · line 128

Quoted 10 lines
drivers/i2c/busses/i2c-mpc.c +8 -0
+		for (k = 9; k; k--) {
+			writeccr(i2c, 0);
+			writeccr(i2c, CCR_MSTA | CCR_MTX | CCR_MEN);
+			udelay(delay_val);
+			writeccr(i2c, CCR_MEN);
+			udelay(delay_val << 1);
+		}
+	}
 }
 

The old sequence has always un-hung the bus for me. Yours might be better. I'll try it next time the bus wedges up on me.

Quoted 56 lines
drivers/i2c/busses/i2c-mpc.c +12 -5
 static int i2c_wait(struct mpc_i2c *i2c, unsigned timeout, int writing)
@@ -186,15 +203,18 @@ static const struct mpc_i2c_divider mpc_
 	{10240, 0x9d}, {12288, 0x9e}, {15360, 0x9f}
 };
 
-int mpc_i2c_get_fdr_52xx(struct device_node *node, u32 clock, int prescaler)
+int mpc_i2c_get_fdr_52xx(struct device_node *node, u32 clock, int prescaler,
+			 u32 *real_clk)
 {
 	const struct mpc_i2c_divider *div = NULL;
 	unsigned int pvr = mfspr(SPRN_PVR);
 	u32 divider;
 	int i;
 
-	if (!clock)
+	if (!clock) {
+		*real_clk = 0;
 		return -EINVAL;
+	}
 
 	/* Determine divider value */
 	divider = mpc5xxx_get_bus_frequency(node) / clock;
@@ -212,7 +232,8 @@ int mpc_i2c_get_fdr_52xx(struct device_n
 			break;
 	}
 
-	return div ? (int)div->fdr : -EINVAL;
+	*real_clk = mpc5xxx_get_bus_frequency(node) / div->divider;
+	return (int)div->fdr;
 }
 
 static void mpc_i2c_setclock_52xx(struct device_node *node,
@@ -221,13 +242,14 @@ static void mpc_i2c_setclock_52xx(struct
 {
 	int ret, fdr;
 
-	ret = mpc_i2c_get_fdr_52xx(node, clock, prescaler);
+	ret = mpc_i2c_get_fdr_52xx(node, clock, prescaler, &i2c->real_clk);
 	fdr = (ret >= 0) ? ret : 0x3f; /* backward compatibility */
 
 	writeb(fdr & 0xff, i2c->base + MPC_I2C_FDR);
 
 	if (ret >= 0)
-		dev_info(i2c->dev, "clock %d Hz (fdr=%d)\n", clock, fdr);
+		dev_info(i2c->dev, "clock %u Hz (fdr=%d)\n", i2c->real_clk,
+			 fdr);
 }
 #else /* !CONFIG_PPC_MPC52xx */
 static void mpc_i2c_setclock_52xx(struct device_node *node,
@@ -446,10 +468,14 @@ static int mpc_xfer(struct i2c_adapter *
 			return -EINTR;
 		}
 		if (time_after(jiffies, orig_jiffies + HZ)) {
+			u8 status = readb(i2c->base + MPC_I2C_SR);
+
 			dev_dbg(i2c->dev, "timeout\n");

drivers/i2c/busses/i2c-mpc.c static int mpc_xfer(struct i2c_adapter * · line 481

Quoted 10 lines
drivers/i2c/busses/i2c-mpc.c +4 -2
-			if (readb(i2c->base + MPC_I2C_SR) ==
-			    (CSR_MCF | CSR_MBB | CSR_RXAK))
+			if ((status & (CSR_MCF | CSR_MBB | CSR_RXAK)) != 0) {
+				writeb(status & ~CSR_MAL,
+				       i2c->base + MPC_I2C_SR);
 				mpc_i2c_fixup(i2c);
+			}
 			return -EIO;
 		}
 		schedule();

This hunk looks good to me. It is basically what I did, except I didn't clear the MAL bit. Judging by the manual, it should be cleared.

Quoted 6 lines
drivers/i2c/busses/i2c-mpc.c +1 -0
@@ -540,6 +566,14 @@ static int __devinit fsl_i2c_probe(struc
 		}
 	}
 
+	prop = of_get_property(op->node, "timeout", &plen);

drivers/i2c/busses/i2c-mpc.c static int __devinit fsl_i2c_probe(struc · line 579

Quoted 10 lines
drivers/i2c/busses/i2c-mpc.c +7 -0
+	if (prop && plen == sizeof(u32)) {
+		mpc_ops.timeout = *prop * HZ / 1000000;
+		if (mpc_ops.timeout < 5)
+			mpc_ops.timeout = 5;
+	}
+	dev_info(i2c->dev, "timeout %u us\n", mpc_ops.timeout * 1000000 / HZ);
+
 	dev_set_drvdata(&op->dev, i2c);
 
 	i2c->adap = mpc_ops;

Ira

A message that is not in the archive (sent off-list or not yet received): fa686aa41002161131j7329a05cx16e66125f82fec39-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org

#6 lore raw

Hi Grant & Ira:

Thanks a lot for reviewing the patch, and for the encouraging comments! I will re-submit a new version according according to them, hopefully tomorrow or on Thursday.

Best, Albrecht.

----- Original Nachricht ----

Von:     Grant Likely <grant.likely-s3s/[email protected]>
An:      Albrecht Dreß <[email protected]>
Datum:   16.02.2010 20:31
Betreff: Re: [PATCH/RFC 1/2] 5200: improve i2c bus error recovery
Quoted 34 lines

Hi Albrecht,

Comments below.

Quoted 14 lines · Albrecht Dreß

Improve the recovery of the MPC5200B's I2C bus from errors like bus hangs.

---

This patch introduces several improvements to the MPC5200B's I2C driver as to improve the recovery from error conditions I encountered when testing a custom board with several I2C devices attached (eeprom, io expander, rtc, sensors).  The error conditions included cases where the bus if logic of one slave apparently went south, blocking the bus completely.

Quoted 13 lines

My fixes include:
1. make the bus timeout configurable in fsl_i2c_probe(); the default of one second is *way* too long for my use case;
2. if a timeout condition occurs in mpc_xfer(), mpc_i2c_fixup() the bus if *any* of the CF, BB and RXAK flags in the MSR is 1.  I actually saw different combinations with hangs, not only all three set;
3. improve the fixup procedure by calculating the timing needed from the real (configured) bus clock, calculated in mpc_i2c_setclock_52xx(). Furthermore, I issue 9 instead of one cycle, as I experienced cases where the single one is not enough (found this tip in a forum).  As a side effect, the new scheme needs only 81us @375kHz bus clock instead of 150us.  I recorded waveforms for 18.4kHz, 85.9kHz and 375kHz, all looking fine, which I can provide if anyone is interested.

Quoted 4 lines

These are three separate fixes. Ideally you should submit them in separate patches to make it easy on poor old reviewers like me. And, as Ben mentions, this descriptions should be above the '---' line so it appears in the commit text.

OK, will do.

Quoted 66 lines
Quoted 8 lines

Open questions:
- is the approach correct at all, in particular the interpretation of the flags (#2)?
- could this code also be used on non-5200 processors?

--- linux-2.6.32-orig/drivers/i2c/busses/i2c-mpc.c      2009-12-03

04:51:21.000000000 +0100

Quoted 1 line

+++ linux-2.6.32/drivers/i2c/busses/i2c-mpc.c   2010-01-22

16:05:13.000000000 +0100

Quoted 38 lines
drivers/i2c/busses/i2c-mpc.c +17 -10
@@ -59,6 +59,7 @@ struct mpc_i2c {
        wait_queue_head_t queue;
        struct i2c_adapter adap;
        int irq;
+       u32 real_clk;
  };
 
  struct mpc_i2c_divider {
@@ -97,16 +98,32 @@ static irqreturn_t mpc_i2c_isr(int irq,
  */
  static void mpc_i2c_fixup(struct mpc_i2c *i2c)
  {
-       writeccr(i2c, 0);
-       udelay(30);
-       writeccr(i2c, CCR_MEN);
-       udelay(30);
-       writeccr(i2c, CCR_MSTA | CCR_MTX);
-       udelay(30);
-       writeccr(i2c, CCR_MSTA | CCR_MTX | CCR_MEN);
-       udelay(30);
-       writeccr(i2c, CCR_MEN);
-       udelay(30);
+       if (i2c->real_clk == 0) {
+               writeccr(i2c, 0);
+               udelay(30);
+               writeccr(i2c, CCR_MEN);
+               udelay(30);
+               writeccr(i2c, CCR_MSTA | CCR_MTX);
+               udelay(30);
+               writeccr(i2c, CCR_MSTA | CCR_MTX | CCR_MEN);
+               udelay(30);
+               writeccr(i2c, CCR_MEN);
+               udelay(30);
+       } else {
+               int k;
+               u32 delay_val = 1000000 / i2c->real_clk + 1;
+
+               if (delay_val < 2)

drivers/i2c/busses/i2c-mpc.c static irqreturn_t mpc_i2c_isr(int irq, · line 126

Quoted 10 lines
drivers/i2c/busses/i2c-mpc.c +10 -0
+                       delay_val = 2;
+
+               for (k = 9; k; k--) {
+                       writeccr(i2c, 0);
+                       writeccr(i2c, CCR_MSTA | CCR_MTX | CCR_MEN);
+                       udelay(delay_val);
+                       writeccr(i2c, CCR_MEN);
+                       udelay(delay_val << 1);
+               }
+       }

This doesn't look right. Why is the old code being preserved? Isn't it not as reliable? It looks to me that the new block should be the only path, with delay_val getting hard set to a sane value if real_clk == 0. This approach looks to add complexity to the driver without a reason other than fear it *might* breaking something.

Quoted 2 lines

If the new code is better, then be strong, stand tall, and say in a loud voice, "this old code is crap. The new stuff is much better."

:-) Ok...

Quoted 91 lines

g.

drivers/i2c/busses/i2c-mpc.c static const struct mpc_i2c_divider mpc_ · line 189

Quoted 8 lines
drivers/i2c/busses/i2c-mpc.c +0 -1
  }
 
  static int i2c_wait(struct mpc_i2c *i2c, unsigned timeout, int writing)
@@ -186,15 +203,18 @@ static const struct mpc_i2c_divider mpc_
        {10240, 0x9d}, {12288, 0x9e}, {15360, 0x9f}
  };
 
-int mpc_i2c_get_fdr_52xx(struct device_node *node, u32 clock, int

prescaler)

Review static const struct mpc_i2c_divider mpc_ · line 206

Quoted 1 line
(quoted diff) +1 -0
+int mpc_i2c_get_fdr_52xx(struct device_node *node, u32 clock, int

prescaler,

Quoted 26 lines
drivers/i2c/busses/i2c-mpc.c +6 -2
+                        u32 *real_clk)
  {
        const struct mpc_i2c_divider *div = NULL;
        unsigned int pvr = mfspr(SPRN_PVR);
        u32 divider;
        int i;
 
-       if (!clock)
+       if (!clock) {
+               *real_clk = 0;
                return -EINVAL;
+       }
 
        /* Determine divider value */
        divider = mpc5xxx_get_bus_frequency(node) / clock;
@@ -212,7 +232,8 @@ int mpc_i2c_get_fdr_52xx(struct device_n
                        break;
        }
 
-       return div ? (int)div->fdr : -EINVAL;
+       *real_clk = mpc5xxx_get_bus_frequency(node) / div->divider;
+       return (int)div->fdr;
  }
 
  static void mpc_i2c_setclock_52xx(struct device_node *node,

drivers/i2c/busses/i2c-mpc.c static void mpc_i2c_setclock_52xx(struct · line 245

Quoted 5 lines
drivers/i2c/busses/i2c-mpc.c +1 -1
@@ -221,13 +242,14 @@ static void mpc_i2c_setclock_52xx(struct
  {
        int ret, fdr;
 
-       ret = mpc_i2c_get_fdr_52xx(node, clock, prescaler);
+       ret = mpc_i2c_get_fdr_52xx(node, clock, prescaler,

&i2c->real_clk);

Quoted 7 lines

       fdr = (ret >= 0) ? ret : 0x3f; /* backward compatibility */

       writeb(fdr & 0xff, i2c->base + MPC_I2C_FDR);

       if (ret >= 0)
-               dev_info(i2c->dev, "clock %d Hz (fdr=%d)\n", clock, fdr);
+               dev_info(i2c->dev, "clock %u Hz (fdr=%d)\n",

i2c->real_clk,

Quoted 5 lines

+                        fdr);
 }
 #else /* !CONFIG_PPC_MPC52xx */
 static void mpc_i2c_setclock_52xx(struct device_node *node,

drivers/i2c/busses/i2c-mpc.c static int mpc_xfer(struct i2c_adapter * · line 474

Quoted 9 lines
drivers/i2c/busses/i2c-mpc.c +3 -2
@@ -446,10 +468,14 @@ static int mpc_xfer(struct i2c_adapter *
                        return -EINTR;
                }
                if (time_after(jiffies, orig_jiffies + HZ)) {
+                       u8 status = readb(i2c->base + MPC_I2C_SR);
+
                        dev_dbg(i2c->dev, "timeout\n");
-                       if (readb(i2c->base + MPC_I2C_SR) ==
-                           (CSR_MCF | CSR_MBB | CSR_RXAK))
+                       if ((status & (CSR_MCF | CSR_MBB | CSR_RXAK)) !=

0) {

Quoted 8 lines
drivers/i2c/busses/i2c-mpc.c +3 -0
+                               writeb(status & ~CSR_MAL,
+                                      i2c->base + MPC_I2C_SR);
                                mpc_i2c_fixup(i2c);
+                       }
                        return -EIO;
                }
                schedule();

drivers/i2c/busses/i2c-mpc.c static int __devinit fsl_i2c_probe(struc · line 575

Quoted 10 lines
drivers/i2c/busses/i2c-mpc.c +7 -0
@@ -540,6 +566,14 @@ static int __devinit fsl_i2c_probe(struc
                }
        }
 
+       prop = of_get_property(op->node, "timeout", &plen);
+       if (prop && plen == sizeof(u32)) {
+               mpc_ops.timeout = *prop * HZ / 1000000;
+               if (mpc_ops.timeout < 5)
+                       mpc_ops.timeout = 5;
+       }
+       dev_info(i2c->dev, "timeout %u us\n", mpc_ops.timeout * 1000000 /

HZ);

Quoted 3 lines
drivers/i2c/busses/i2c-mpc.c +1 -0
+
        dev_set_drvdata(&op->dev, i2c);
 
Quoted 18 lines
Quoted 7 lines
drivers/i2c/busses/i2c-mpc.c +0 -0
        i2c->adap = mpc_ops;

_______________________________________________
devicetree-discuss mailing list devicetree-discuss-uLR06cmDAlY/[email protected]
https://lists.ozlabs.org/listinfo/devicetree-discuss

Signature · 6 lines
Grant Likely, B.Sc., P.Eng.
Secret Lab Technologies Ltd.
_______________________________________________
Linuxppc-dev mailing list
Linuxppc-dev-uLR06cmDAlY/[email protected]
https://lists.ozlabs.org/listinfo/linuxppc-dev

Immer auf dem Laufenden! Sport, Auto, Reise, Politik und Promis. Von uns für Sie: der neue Arcor.de-Newsletter! Jetzt anmelden und einfach alles wissen: http://www.arcor.de/rd/footer.newsletter

A message that is not in the archive (sent off-list or not yet received): [email protected]

#8 lore raw

Hi Ira:

[snip]

Quoted 8 lines

I see this exact hang on a MPC8349EA board. I poll my i2c sensors every 500ms, and it takes around 12 hours to produce a hang. The usual hang has (CF | BB) set, however I have seen a hang with just BB (only once so far in about 2 weeks).

I think the fixup should be run on 8349 as well, if not all processors. I'm happy to test patches. I have a way to reliably trigger a lockup, using another master on the i2c bus.

See my other post - hopefully a new patch is ready tomorrow/Thursday...

[snip]

Quoted 35 lines
Quoted 35 lines

static void mpc_i2c_fixup(struct mpc_i2c *i2c)
{

drivers/i2c/busses/i2c-mpc.c +23 -10
-	writeccr(i2c, 0);
-	udelay(30);
-	writeccr(i2c, CCR_MEN);
-	udelay(30);
-	writeccr(i2c, CCR_MSTA | CCR_MTX);
-	udelay(30);
-	writeccr(i2c, CCR_MSTA | CCR_MTX | CCR_MEN);
-	udelay(30);
-	writeccr(i2c, CCR_MEN);
-	udelay(30);
+	if (i2c->real_clk == 0) {
+		writeccr(i2c, 0);
+		udelay(30);
+		writeccr(i2c, CCR_MEN);
+		udelay(30);
+		writeccr(i2c, CCR_MSTA | CCR_MTX);
+		udelay(30);
+		writeccr(i2c, CCR_MSTA | CCR_MTX | CCR_MEN);
+		udelay(30);
+		writeccr(i2c, CCR_MEN);
+		udelay(30);
+	} else {
+		int k;
+		u32 delay_val = 1000000 / i2c->real_clk + 1;
+
+		if (delay_val < 2)
+			delay_val = 2;
+
+		for (k = 9; k; k--) {
+			writeccr(i2c, 0);
+			writeccr(i2c, CCR_MSTA | CCR_MTX | CCR_MEN);
+			udelay(delay_val);
+			writeccr(i2c, CCR_MEN);
Quoted 8 lines

drivers/i2c/busses/i2c-mpc.c static irqreturn_t mpc_i2c_isr(int irq, · line 127

Quoted 5 lines
drivers/i2c/busses/i2c-mpc.c +3 -0
+			udelay(delay_val << 1);
+		}
+	}
 }

The old sequence has always un-hung the bus for me. Yours might be better. I'll try it next time the bus wedges up on me.

The easiest way to verify the /waveforms/ is to simply call the function somewhere else (e.g. in a i2c write call), and then record SCK and SDA with a scope. I saw *huge* unnecessary delays in the original code, and by some creative interpretation of the (not very clear) 5200 data sheet I came to the write's and delays above. If you have a scope, you might want to repeat the test with your '8349.

Best, Albrecht.

Immer auf dem Laufenden! Sport, Auto, Reise, Politik und Promis. Von uns für Sie: der neue Arcor.de-Newsletter! Jetzt anmelden und einfach alles wissen: http://www.arcor.de/rd/footer.newsletter