Skip to contentSkip to conversation

5200/mpc: improve i2c bus error recovery

PATCH Albrecht Dre� 3 participants 5 messages Feb 18, 2010 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

Hi Joakim:

[snip]

Quoted 21 lines
Quoted 21 lines
  static void mpc_i2c_fixup(struct mpc_i2c *i2c)
  {
(quoted diff) +9 -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);
+   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);
Quoted 8 lines

Review

Quoted 5 lines
(quoted diff) +4 -0
+      udelay(delay_val);
+      writeccr(i2c, CCR_MEN);
+      udelay(delay_val << 1);
+   }
  }

I am curious, didn't old method work with by just wrapping a for(k=9; k; k--) around it? How did the wave form look?

Sure does that work! The waveform was somewhat "streched", mainly due to the delays between some of the writeccr() calls which don't change the sda/scl lines. Unfortunately I didn't take shots from the scope.

However, for *one* cycle, the old code needed (only counting the udelay's) 150 us. For 9 cycles, it's 1.35 ms, which isn't really nice ;-). At 375 kHz real clock rate, delay_val is 3, i.e. each cycle consumes 9 us, or 81 us for the whole fixup procedure. If the clock is slower, the gain is of course a lot smaller, and at 20.5 kHz each cycle again needs 150 us...

My feeling is that the delays used in the old code are just "some" values which work for sure, to if you like, my change is basically optimisation...

BTW, related to your earlier question, I checked the timings recorded with the scope at 100 and at 20 kHz against the nxp's "I2C bus specification and user manual", rev. 03 - everything seems to be fine.

Thanks, 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

#1 lore raw

"Albrecht Dreß" <[email protected]> wrote on 2010/02/18 16:04:16:

Quoted 34 lines

Hi Joakim:

[snip]

Quoted 21 lines
Quoted 21 lines
  static void mpc_i2c_fixup(struct mpc_i2c *i2c)
  {
(quoted diff) +9 -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);
+   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);
Quoted 8 lines

Review

Quoted 5 lines
(quoted diff) +4 -0
+      udelay(delay_val);
+      writeccr(i2c, CCR_MEN);
+      udelay(delay_val << 1);
+   }
  }

I am curious, didn't old method work with by just wrapping a for(k=9; k; k--) around it? How did the wave form look?

Quoted 3 lines

Sure does that work! The waveform was somewhat "streched", mainly due to the delays between some of the writeccr() calls which don't change the sda/scl lines. Unfortunately I didn't take shots from the scope.

Yeah, the long delays has to go. So the wave form was the same but more stretched in time? I ask because I don't understand the writeccr(i2c, CCR_MSTA | CCR_MTX); is supposed to do.

Quoted 9 lines

However, for *one* cycle, the old code needed (only counting the udelay's) 150 us. For 9 cycles, it's 1.35 ms, which isn't really nice ;-). At 375 kHz real clock rate, delay_val is 3, i.e. each cycle consumes 9 us, or 81 us for the whole fixup procedure. If the clock is slower, the gain is of course a lot smaller, and at 20.5 kHz each cycle again needs 150 us...

My feeling is that the delays used in the old code are just "some" values which work for sure, to if you like, my change is basically optimisation...

The old code only works when the device is stuck at the last bit. To cope with any bit (worst case is the first bit) you need 9 cycles, 8 bits + ack = 9

Just toggling the clock 9 cycles should unlock any slave stuck in a read operation. To unlock slaves stuck in a write operation you also need to generate a START in every cycle too.

As far as I can tell your patch does all of the above so

Quoted 4 lines

BTW, related to your earlier question, I checked the timings recorded with the scope at 100 and at 20 kHz against the nxp's "I2C bus specification and user manual", rev. 03 - everything seems to be fine.

Good, thanks.

Jocke

#2 lore raw
Quoted 4 lines · Joakim Tjernlund

"Albrecht Dreß" <[email protected]> wrote on 2010/02/18 16:04:16:
As far as I can tell your patch does all of the above so

Thanks Joakim. On a point of process; "Signed-off-by" means that you've actually handled the patch and forwarded it on, either by reposting or putting it in a git tree for someone to pull. The Signed-off-by trail collects a history of all the people who have touched a patch during its development and merging.

Reviewers can use Acked-by: or Reviewed-by: to indicate their consent.
Ben can add your annotation when he merges the patch into his i2c
tree.

Cheers,
g.

#3 lore raw

[email protected] wrote on 2010/02/18 18:41:40:

Quoted 14 lines
Quoted 4 lines · Joakim Tjernlund

"Albrecht Dreß" <[email protected]> wrote on 2010/02/18 16:04:16:
As far as I can tell your patch does all of the above so

Thanks Joakim. On a point of process; "Signed-off-by" means that you've actually handled the patch and forwarded it on, either by reposting or putting it in a git tree for someone to pull. The Signed-off-by trail collects a history of all the people who have touched a patch during its development and merging.

Quoted 3 lines

Reviewers can use Acked-by: or Reviewed-by: to indicate their consent.
Ben can add your annotation when he merges the patch into his i2c
tree.

I see, then please change that to

Thanks,
Jocke

#5 lore raw
In reply to a message not in the archive

Hi Joakim:

Am 18.02.10 18:14 schrieb(en) Joakim Tjernlund:

Quoted 30 lines
Quoted 30 lines

[snip]

Quoted 28 lines
Quoted 16 lines
  static void mpc_i2c_fixup(struct mpc_i2c *i2c)
  {
(quoted diff) +4 -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);
+   int k;
+   u32 delay_val = 1000000 / i2c->real_clk + 1;
+
+   if (delay_val < 2)

Review

Quoted 10 lines
(quoted diff) +9 -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);
+   }
  }

I am curious, didn't old method work with by just wrapping a for(k=9; k; k--) around it? How did the wave form look?

Quoted 3 lines
Quoted 1 line

Sure does that work! The waveform was somewhat "streched", mainly due to the delays between some of the writeccr() calls which don't change the sda/scl lines. Unfortunately I didn't take shots from the scope.

Yeah, the long delays has to go. So the wave form was the same but more stretched in time? I ask because I don't understand the writeccr(i2c, CCR_MSTA | CCR_MTX); is supposed to do.

Afaict, this is really a no-op. The '5200 user's manual says about MEN

<snip>
* 0 module is reset and disabled. This is the Power-ON reset. When low the interface is held in reset, but registers can still be accessed.
* 1 I2C module is enabled. Bit must be set before other CR bits have any effect.
</snip>

The change in the MSTA is needed -with the proper delays- as to generate the START and STOP conditions.

Unfortunately, the data sheet is not very clear (or my English too bad), but reading it *after* seeing the signals on the scope, I can at least guess what they mean :-/

Thus, the old code will probably produce SDA and SCL held high for ~90us, then a SDA/SCL low for ~30us (plus/minus the delays the hw adds internally according to the clock setting), and then a ~30us SDA/SCL high. It is not possible to get the necessary delays from the data sheet, but as I said I empirically verified some cases to be safe.

Quoted 3 lines

The old code only works when the device is stuck at the last bit. To cope with any bit (worst case is the first bit) you need 9 cycles, 8 bits + ack = 9

Just toggling the clock 9 cycles should unlock any slave stuck in a read operation. To unlock slaves stuck in a write operation you also need to generate a START in every cycle too.

Yes, of course... this was the initial motivation of the patch, as I *have* a slave which sometimes needs more than one cycle!

Quoted 3 lines

As far as I can tell your patch does all of the above so

Thanks a lot again for your time, and your helpful comments!

Best, Albrecht.

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