[U-Boot] [PATCH] Add I2C speed and divider formula for ColdFire
Tsi-Chung Liew
Tsi-Chung.Liew at freescale.com
Thu Apr 9 18:18:46 CEST 2009
From: TsiChung Liew <Tsi-Chung.Liew at freescale.com>
Implement formula to obtain I2C speed and internal bus
divider. This will provide accurate divider than fix table
divider value.
Signed-off-by: TsiChung Liew <Tsi-Chung.Liew at freescale.com>
---
drivers/i2c/fsl_i2c.c | 76 +++++++++++++++++++++++++++++++++----------------
1 files changed, 51 insertions(+), 25 deletions(-)
diff --git a/drivers/i2c/fsl_i2c.c b/drivers/i2c/fsl_i2c.c
index 6ab7d3d..7f6d864 100644
--- a/drivers/i2c/fsl_i2c.c
+++ b/drivers/i2c/fsl_i2c.c
@@ -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
--
1.5.6.4
More information about the U-Boot
mailing list