Skip to contentSkip to conversation

Add I2C speed and divider formula for ColdFire

PATCH New Tsi-Chung Liew 3 participants 3 messages Apr 9, 2009 – Apr 15, 2009 I2C

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add I2C speed and divider formula for ColdFire

From: TsiChung Liew <[email protected]>

Implement formula to obtain I2C speed and internal bus divider. This will provide accurate divider than fix table divider value.

---
 drivers/i2c/fsl_i2c.c |   76 +++++++++++++++++++++++++++++++++----------------
 1 files changed, 51 insertions(+), 25 deletions(-)
drivers/i2c/fsl_i2c.c +51 -25
index 6ab7d3d..7f6d864 100644
@@ -84,14 +84,12 @@ static const struct fsl_i2c *i2c_dev[2] = {
  * A different table is defined and are based on MCF5xxx user manual.
  *
  */
+#ifdef __PPC__
 static const struct {
 	unsigned short divider;
-#ifdef __PPC__
 	u8 dfsr;
-#endif
 	u8 fdr;
 } fsl_i2c_speed_map[] = {
-#ifdef __PPC__
 	{160, 1, 32}, {192, 1, 33}, {224, 1, 34}, {256, 1, 35},
 	{288, 1, 0}, {320, 1, 1}, {352, 6, 1}, {384, 1, 2}, {416, 6, 2},
 	{448, 1, 38}, {480, 1, 3}, {512, 1, 39}, {544, 11, 3}, {576, 1, 4},
@@ -108,26 +106,22 @@ static const struct {
 	{20480, 1, 25}, {24576, 1, 26}, {28672, 1, 62}, {30720, 1, 27},
 	{32768, 1, 63}, {36864, 1, 28}, {40960, 1, 29}, {49152, 1, 30},
 	{61440, 1, 31}, {-1, 1, 31}
+};
 #elif defined(__M68K__)
-	{20, 32}, {22, 33}, {24, 34}, {26, 35},
-	{28, 0}, {28, 36}, {30, 1}, {32, 37},
-	{34, 2}, {36, 38}, {40, 3}, {40, 39},
-	{44, 4}, {48, 5}, {48, 40}, {56, 6},
-	{56, 41}, {64, 42}, {68, 7}, {72, 43},
-	{80, 8}, {80, 44}, {88, 9}, {96, 41},
-	{104, 10}, {112, 42}, {128, 11}, {128, 43},
-	{144, 12}, {160, 13}, {160, 48}, {192, 14},
-	{192, 49}, {224, 50}, {240, 15}, {256, 51},
-	{288, 16}, {320, 17}, {320, 52}, {384, 18},
-	{384, 53}, {448, 54}, {480, 19}, {512, 55},
-	{576, 20}, {640, 21}, {640, 56}, {768, 22},
-	{768, 57}, {960, 23}, {896, 58}, {1024, 59},
-	{1152, 24}, {1280, 25}, {1280, 60}, {1536, 26},
-	{1536, 61}, {1792, 62}, {1920, 27}, {2048, 63},
-	{2304, 28}, {2560, 29}, {3072, 30}, {3840, 31},
-	{-1, 31}
-#endif
+struct coldfire_i2c_tap {
+	int scl2tap;
+	int tap2tap;
+} scltap[] = {
+	{4, 1},
+	{4, 2},
+	{6, 4},
+	{6, 8},
+	{14, 16},
+	{30, 32},
+	{62, 64},
+	{126, 128}
 };
+#endif
 
 /**
  * Set the I2C bus speed for a given I2C device
@@ -143,6 +137,7 @@ static const struct {
 static unsigned int set_i2c_bus_speed(const struct fsl_i2c *dev,
 	unsigned int i2c_clk, unsigned int speed)
 {
+#ifdef __PPC__
 	unsigned short divider = min(i2c_clk / speed, (unsigned short) -1);
 	unsigned int i;
 
@@ -156,20 +151,51 @@ static unsigned int set_i2c_bus_speed(const struct fsl_i2c *dev,
 	for (i = 0; i < ARRAY_SIZE(fsl_i2c_speed_map); i++)
 		if (fsl_i2c_speed_map[i].divider >= divider) {
 			u8 fdr;
-#ifdef __PPC__
 			u8 dfsr;
 			dfsr = fsl_i2c_speed_map[i].dfsr;
-#endif
 			fdr = fsl_i2c_speed_map[i].fdr;
 			speed = i2c_clk / fsl_i2c_speed_map[i].divider;
 			writeb(fdr, &dev->fdr);		/* set bus speed */
-#ifdef __PPC__
 			writeb(dfsr, &dev->dfsrr);	/* set default filter */
-#endif
 			break;
 		}
 
 	return speed;
+#elif defined(__M68K__)
+	int fdr = -1;
+	ulong best_speed = 0;
+	ulong divider;
+	ulong ipb, scl;
+	ulong bestmatch = 0xffffffffUL;
+	int best_i = 0, best_j = 0, i, j;
+	int SCL_Tap[] = { 9, 10, 12, 15, 5, 6, 7, 8 };
+
+	ipb = gd->bus_clk;
+	for (i = 7; i >= 0; i--) {
+		for (j = 7; j >= 0; j--) {
+			scl = 2 * (scltap[j].scl2tap +
+				   (SCL_Tap[i] - 1) * scltap[j].tap2tap + 2);
+			if (ipb <= speed * scl) {
+				if ((speed * scl - ipb) < bestmatch) {
+					bestmatch = speed * scl - ipb;
+					best_i = i;
+					best_j = j;
+					best_speed = ipb / scl;
+				}
+			}
+		}
+	}
+
+	divider = (best_i & 3) | ((best_i & 4) << 3) | (best_j << 2);
+	if (gd->flags & GD_FLG_RELOC)
+		fdr = divider;
+	else
+		printf("%ld kHz, ", best_speed / 1000);
+
+	speed = best_speed;
+
+	return speed;
+#endif
 }
 
 void
Signature · 1 line
1.5.6.4
#1 lore raw

PATCH 1/1 Add I2C speed and divider formula for ColdFire

Quoted 5 lines · Tsi-Chung Liew

From: TsiChung Liew <[email protected]>

Implement formula to obtain I2C speed and internal bus divider. This will provide accurate divider than fix table divider value.

Can you prove this statement? I don't think accuracy is that important. The advantage of a table is that it's easier to debug and the code is simpler.

I'm inclined to NACK this patch.

Signature · 2 lines
Timur Tabi
Linux kernel developer at Freescale
#2 lore raw

PATCH 1/1 Add I2C speed and divider formula for ColdFire

Hi TC,

Sorry for the delay, but I was the original complainer about this issue and finally got a chance to try it out.

Quoted 9 lines · Timur Tabi
Quoted 5 lines · Tsi-Chung Liew

From: TsiChung Liew <[email protected]>

Implement formula to obtain I2C speed and internal bus divider. This will provide accurate divider than fix table divider value.

Can you prove this statement?

And the code and boot message thinks it is 390 KHz

U-Boot 2009.03dvl-00089-g61e51f7 (Apr 15 2009 - 11:20:06)

CPU:   Freescale ColdFire MCF5270 rev. 1, at 150 MHz
I2C:   390 kHz, ready

[snip]
=> i2c speed
Current bus speed=390625

My probed i2c clock changes from 390KHz (with my patch) to 2MHz with your patch (the ideal being 400KHz)

so NAK from me as well.

- Richard