sf: inline data constants
[oweals/u-boot.git] / drivers / i2c / mxc_i2c.c
index 2869d7cec3d70466ab75347decf3ff277430f2af..fc68062b1183340a1d2427b6576d31fd0ba7f411 100644 (file)
@@ -37,6 +37,7 @@
 
 #include <asm/arch/clock.h>
 #include <asm/arch/imx-regs.h>
+#include <i2c.h>
 
 struct mxc_i2c_regs {
        uint32_t        iadr;
@@ -58,23 +59,10 @@ struct mxc_i2c_regs {
 #define I2SR_IIF       (1 << 1)
 #define I2SR_RX_NO_AK  (1 << 0)
 
-#if defined(CONFIG_SYS_I2C_MX31_PORT1)
-#define I2C_BASE       0x43f80000
-#define I2C_CLK_OFFSET 26
-#elif defined (CONFIG_SYS_I2C_MX31_PORT2)
-#define I2C_BASE       0x43f98000
-#define I2C_CLK_OFFSET 28
-#elif defined (CONFIG_SYS_I2C_MX31_PORT3)
-#define I2C_BASE       0x43f84000
-#define I2C_CLK_OFFSET 30
-#elif defined(CONFIG_SYS_I2C_MX53_PORT1)
-#define I2C_BASE        I2C1_BASE_ADDR
-#elif defined(CONFIG_SYS_I2C_MX53_PORT2)
-#define I2C_BASE        I2C2_BASE_ADDR
-#elif defined(CONFIG_SYS_I2C_MX35_PORT1)
-#define I2C_BASE       I2C_BASE_ADDR
+#ifdef CONFIG_SYS_I2C_BASE
+#define I2C_BASE       CONFIG_SYS_I2C_BASE
 #else
-#error "define CONFIG_SYS_I2C_MX<Processor>_PORTx to use the mx I2C driver"
+#error "define CONFIG_SYS_I2C_BASE to use the mxc_i2c driver"
 #endif
 
 #define I2C_MAX_TIMEOUT                10000
@@ -95,23 +83,21 @@ static u16 i2c_clk_div[50][2] = {
        { 3072, 0x1E }, { 3840, 0x1F }
 };
 
-static u8 clk_div;
-
 /*
  * Calculate and set proper clock divider
  */
-static void i2c_imx_set_clk(unsigned int rate)
+static uint8_t i2c_imx_get_clk(unsigned int rate)
 {
-       struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
        unsigned int i2c_clk_rate;
        unsigned int div;
+       u8 clk_div;
 
 #if defined(CONFIG_MX31)
        struct clock_control_regs *sc_regs =
                (struct clock_control_regs *)CCM_BASE;
 
        /* start the required I2C clock */
-       writel(readl(&sc_regs->cgr0) | (3 << I2C_CLK_OFFSET),
+       writel(readl(&sc_regs->cgr0) | (3 << CONFIG_SYS_I2C_CLK_OFFSET),
                &sc_regs->cgr0);
 #endif
 
@@ -127,7 +113,7 @@ static void i2c_imx_set_clk(unsigned int rate)
                        ;
 
        /* Store divider value */
-       writeb(i2c_clk_div[clk_div][1], &i2c_regs->ifdr);
+       return clk_div;
 }
 
 /*
@@ -146,7 +132,13 @@ void i2c_reset(void)
  */
 void i2c_init(int speed, int unused)
 {
-       i2c_imx_set_clk(speed);
+       struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
+       u8 clk_idx = i2c_imx_get_clk(speed);
+       u8 idx = i2c_clk_div[clk_idx][1];
+
+       /* Store divider value */
+       writeb(idx, &i2c_regs->ifdr);
+
        i2c_reset();
 }
 
@@ -164,6 +156,13 @@ int i2c_set_bus_speed(unsigned int speed)
  */
 unsigned int i2c_get_bus_speed(void)
 {
+       struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
+       u8 clk_idx = readb(&i2c_regs->ifdr);
+       u8 clk_div;
+
+       for (clk_div = 0; i2c_clk_div[clk_div][1] != clk_idx; clk_div++)
+               ;
+
        return mxc_get_clock(MXC_IPG_PERCLK) / i2c_clk_div[clk_div][0];
 }
 
@@ -233,8 +232,6 @@ int i2c_imx_start(void)
        unsigned int temp = 0;
        int result;
 
-       writeb(i2c_clk_div[clk_div][1], &i2c_regs->ifdr);
-
        /* Enable I2C controller */
        writeb(0, &i2c_regs->i2sr);
        writeb(I2CR_IEN, &i2c_regs->i2cr);
@@ -306,11 +303,10 @@ int i2c_imx_set_chip_addr(uchar chip, int read)
 int i2c_imx_set_reg_addr(uint addr, int alen)
 {
        struct mxc_i2c_regs *i2c_regs = (struct mxc_i2c_regs *)I2C_BASE;
-       int ret;
-       int i;
+       int ret = 0;
 
-       for (i = 0; i < (8 * alen); i += 8) {
-               writeb((addr >> i) & 0xff, &i2c_regs->i2dr);
+       while (alen--) {
+               writeb((addr >> (alen * 8)) & 0xff, &i2c_regs->i2dr);
 
                ret = i2c_imx_trx_complete();
                if (ret)