Patchwork i2c-mpc: Wait for STOP to hit the bus

login
register
mail settings
Submitter Joakim Tjernlund
Date Aug. 30, 2012, 10:40 a.m.
Message ID <1346323204-19210-1-git-send-email-Joakim.Tjernlund@transmode.se>
Download mbox | patch
Permalink /patch/180814/
State Accepted, archived
Headers show

Comments

Joakim Tjernlund - Aug. 30, 2012, 10:40 a.m.
mpc_i2c_stop() only initiates STOP but does not wait for it to
hit the I2C bus. This is a problem when using I2C devices which
uses fairly long clock stretching just before STOP if you also
have an i2c-mux which may switch to another bus before STOP has
been processed.

Signed-off-by: Joakim Tjernlund <Joakim.Tjernlund@transmode.se>
---
 drivers/i2c/busses/i2c-mpc.c |   18 +++++++++++++++++-
 1 files changed, 17 insertions(+), 1 deletions(-)
Tabi Timur-B04825 - Sept. 2, 2012, 2:48 a.m.
On Thu, Aug 30, 2012 at 5:40 AM, Joakim Tjernlund
<Joakim.Tjernlund@transmode.se> wrote:

> -       mpc_i2c_stop(i2c);
> +       mpc_i2c_stop(i2c); /* Initiate STOP */
> +       orig_jiffies = jiffies;
> +       /* Wait until STOP is seen, allow up to 1 s */
> +       while (readb(i2c->base + MPC_I2C_SR) & CSR_MBB) {
> +               if (time_after(jiffies, orig_jiffies + HZ)) {
> +                       u8 status = readb(i2c->base + MPC_I2C_SR);
> +
> +                       dev_dbg(i2c->dev, "timeout\n");
> +                       if ((status & (CSR_MCF | CSR_MBB | CSR_RXAK)) != 0) {
> +                               writeb(status & ~CSR_MAL,
> +                                      i2c->base + MPC_I2C_SR);
> +                               mpc_i2c_fixup(i2c);
> +                       }
> +                       return -EIO;
> +               }
> +               cond_resched();
> +       }

Shouldn't the while-loop be inside mpc_i2c_stop() itself?
Joakim Tjernlund - Sept. 2, 2012, 2:22 p.m.
Tabi Timur-B04825 <B04825@freescale.com> wrote on 2012/09/02 04:48:01:
> On Thu, Aug 30, 2012 at 5:40 AM, Joakim Tjernlund
> <Joakim.Tjernlund@transmode.se> wrote:
>
> > -       mpc_i2c_stop(i2c);
> > +       mpc_i2c_stop(i2c); /* Initiate STOP */
> > +       orig_jiffies = jiffies;
> > +       /* Wait until STOP is seen, allow up to 1 s */
> > +       while (readb(i2c->base + MPC_I2C_SR) & CSR_MBB) {
> > +               if (time_after(jiffies, orig_jiffies + HZ)) {
> > +                       u8 status = readb(i2c->base + MPC_I2C_SR);
> > +
> > +                       dev_dbg(i2c->dev, "timeout\n");
> > +                       if ((status & (CSR_MCF | CSR_MBB | CSR_RXAK)) != 0) {
> > +                               writeb(status & ~CSR_MAL,
> > +                                      i2c->base + MPC_I2C_SR);
> > +                               mpc_i2c_fixup(i2c);
> > +                       }
> > +                       return -EIO;
> > +               }
> > +               cond_resched();
> > +       }
>
> Shouldn't the while-loop be inside mpc_i2c_stop() itself?

Possibly but I choosed to do it this way as there is a similar loop in the beginning of mpc_xfer().
I figured it has better visibility if it is in the same function.

 Jocke
Joakim Tjernlund - Sept. 11, 2012, 10:13 a.m.
Joakim Tjernlund/Transmode wrote on 2012/09/02 16:22:00:
>
> Tabi Timur-B04825 <B04825@freescale.com> wrote on 2012/09/02 04:48:01:
> > On Thu, Aug 30, 2012 at 5:40 AM, Joakim Tjernlund
> > <Joakim.Tjernlund@transmode.se> wrote:
> >
> > > -       mpc_i2c_stop(i2c);
> > > +       mpc_i2c_stop(i2c); /* Initiate STOP */
> > > +       orig_jiffies = jiffies;
> > > +       /* Wait until STOP is seen, allow up to 1 s */
> > > +       while (readb(i2c->base + MPC_I2C_SR) & CSR_MBB) {
> > > +               if (time_after(jiffies, orig_jiffies + HZ)) {
> > > +                       u8 status = readb(i2c->base + MPC_I2C_SR);
> > > +
> > > +                       dev_dbg(i2c->dev, "timeout\n");
> > > +                       if ((status & (CSR_MCF | CSR_MBB | CSR_RXAK)) != 0) {
> > > +                               writeb(status & ~CSR_MAL,
> > > +                                      i2c->base + MPC_I2C_SR);
> > > +                               mpc_i2c_fixup(i2c);
> > > +                       }
> > > +                       return -EIO;
> > > +               }
> > > +               cond_resched();
> > > +       }
> >
> > Shouldn't the while-loop be inside mpc_i2c_stop() itself?
>
> Possibly but I choosed to do it this way as there is a similar loop in the beginning of mpc_xfer().
> I figured it has better visibility if it is in the same function.
>
>  Jocke

Ping? Anything holding this patch back?

 Jocke
Wolfram Sang - Sept. 14, 2012, 2:02 p.m.
On Thu, Aug 30, 2012 at 12:40:04PM +0200, Joakim Tjernlund wrote:
> mpc_i2c_stop() only initiates STOP but does not wait for it to
> hit the I2C bus. This is a problem when using I2C devices which
> uses fairly long clock stretching just before STOP if you also
> have an i2c-mux which may switch to another bus before STOP has
> been processed.
> 
> Signed-off-by: Joakim Tjernlund <Joakim.Tjernlund@transmode.se>

Patch didn't apply, because it was a p0-patch. Checkpatch would have
warned you.

Fixed that and applied to -next, thanks!
Joakim Tjernlund - Sept. 14, 2012, 2:21 p.m.
Wolfram Sang <w.sang@pengutronix.de> wrote on 2012/09/14 16:02:34:
>
> On Thu, Aug 30, 2012 at 12:40:04PM +0200, Joakim Tjernlund wrote:
> > mpc_i2c_stop() only initiates STOP but does not wait for it to
> > hit the I2C bus. This is a problem when using I2C devices which
> > uses fairly long clock stretching just before STOP if you also
> > have an i2c-mux which may switch to another bus before STOP has
> > been processed.
> >
> > Signed-off-by: Joakim Tjernlund <Joakim.Tjernlund@transmode.se>
>
> Patch didn't apply, because it was a p0-patch. Checkpatch would have
> warned you.

Bugger, it is my git config setting:
[diff]
	noprefix = true

I always forget to remove that one before generating patches, I will have to remove it.

>
> Fixed that and applied to -next, thanks!

Thanks

Patch

diff --git drivers/i2c/busses/i2c-mpc.c drivers/i2c/busses/i2c-mpc.c
index 3d31879..c08f287 100644
--- drivers/i2c/busses/i2c-mpc.c
+++ drivers/i2c/busses/i2c-mpc.c
@@ -574,7 +574,23 @@  static int mpc_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
 			    mpc_write(i2c, pmsg->addr, pmsg->buf, pmsg->len, i);
 		}
 	}
-	mpc_i2c_stop(i2c);
+	mpc_i2c_stop(i2c); /* Initiate STOP */
+	orig_jiffies = jiffies;
+	/* Wait until STOP is seen, allow up to 1 s */
+	while (readb(i2c->base + MPC_I2C_SR) & CSR_MBB) {
+		if (time_after(jiffies, orig_jiffies + HZ)) {
+			u8 status = readb(i2c->base + MPC_I2C_SR);
+
+			dev_dbg(i2c->dev, "timeout\n");
+			if ((status & (CSR_MCF | CSR_MBB | CSR_RXAK)) != 0) {
+				writeb(status & ~CSR_MAL,
+				       i2c->base + MPC_I2C_SR);
+				mpc_i2c_fixup(i2c);
+			}
+			return -EIO;
+		}
+		cond_resched();
+	}
 	return (ret < 0) ? ret : num;
 }