]> Git Repo - u-boot.git/blobdiff - drivers/i2c/fsl_i2c.c
fsl_i2c: increase I2C timeout values and make them configurable
[u-boot.git] / drivers / i2c / fsl_i2c.c
index ce646fd7ed16f66bf208bdc9c6ed9979b77d9861..47bbf792c40702c68461829014ddfa7e03585f70 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2006 Freescale Semiconductor, Inc.
+ * Copyright 2006,2009 Freescale Semiconductor, Inc.
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
 #include <asm/io.h>
 #include <asm/fsl_i2c.h>       /* HW definitions */
 
-#define I2C_TIMEOUT    (CONFIG_SYS_HZ / 4)
+/* The maximum number of microseconds we will wait until another master has
+ * released the bus.  If not defined in the board header file, then use a
+ * generic value.
+ */
+#ifndef CONFIG_I2C_MBB_TIMEOUT
+#define CONFIG_I2C_MBB_TIMEOUT 100000
+#endif
+
+/* The maximum number of microseconds we will wait for a read or write
+ * operation to complete.  If not defined in the board header file, then use a
+ * generic value.
+ */
+#ifndef CONFIG_I2C_TIMEOUT
+#define CONFIG_I2C_TIMEOUT     10000
+#endif
 
 #define I2C_READ_BIT  1
 #define I2C_WRITE_BIT 0
@@ -42,6 +56,9 @@ DECLARE_GLOBAL_DATA_PTR;
 #define CONFIG_SYS_SPD_BUS_NUM 0
 #endif
 static unsigned int i2c_bus_num __attribute__ ((section (".data"))) = CONFIG_SYS_SPD_BUS_NUM;
+#if defined(CONFIG_I2C_MUX)
+static unsigned int i2c_bus_num_mux __attribute__ ((section ("data"))) = 0;
+#endif
 
 static unsigned int i2c_bus_speed[2] = {CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SPEED};
 
@@ -175,6 +192,12 @@ i2c_init(int speed, int slaveadd)
        struct fsl_i2c *dev;
        unsigned int temp;
 
+#ifdef CONFIG_SYS_I2C_INIT_BOARD
+       /* call board specific i2c bus reset routine before accessing the   */
+       /* environment, which might be in a chip on that bus. For details   */
+       /* about this problem see doc/I2C_Edge_Conditions.                  */
+       i2c_init_board();
+#endif
        dev = (struct fsl_i2c *) (CONFIG_SYS_IMMR + CONFIG_SYS_I2C_OFFSET);
 
        writeb(0, &dev->cr);                    /* stop I2C controller */
@@ -204,9 +227,10 @@ static __inline__ int
 i2c_wait4bus(void)
 {
        unsigned long long timeval = get_ticks();
+       const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT);
 
        while (readb(&i2c_dev[i2c_bus_num]->sr) & I2C_SR_MBB) {
-               if ((get_ticks() - timeval) > usec2ticks(I2C_TIMEOUT))
+               if ((get_ticks() - timeval) > timeout)
                        return -1;
        }
 
@@ -218,6 +242,7 @@ i2c_wait(int write)
 {
        u32 csr;
        unsigned long long timeval = get_ticks();
+       const unsigned long long timeout = usec2ticks(CONFIG_I2C_TIMEOUT);
 
        do {
                csr = readb(&i2c_dev[i2c_bus_num]->sr);
@@ -242,7 +267,7 @@ i2c_wait(int write)
                }
 
                return 0;
-       } while ((get_ticks() - timeval) < usec2ticks(I2C_TIMEOUT));
+       } while ((get_ticks() - timeval) < timeout);
 
        debug("i2c_wait: timed out\n");
        return -1;
@@ -369,6 +394,19 @@ i2c_probe(uchar chip)
 
 int i2c_set_bus_num(unsigned int bus)
 {
+#if defined(CONFIG_I2C_MUX)
+       if (bus < CONFIG_SYS_MAX_I2C_BUS) {
+               i2c_bus_num = bus;
+       } else {
+               int     ret;
+
+               ret = i2x_mux_select_mux(bus);
+               if (ret)
+                       return ret;
+               i2c_bus_num = 0;
+       }
+       i2c_bus_num_mux = bus;
+#else
 #ifdef CONFIG_SYS_I2C2_OFFSET
        if (bus > 1) {
 #else
@@ -378,7 +416,7 @@ int i2c_set_bus_num(unsigned int bus)
        }
 
        i2c_bus_num = bus;
-
+#endif
        return 0;
 }
 
@@ -396,7 +434,11 @@ int i2c_set_bus_speed(unsigned int speed)
 
 unsigned int i2c_get_bus_num(void)
 {
+#if defined(CONFIG_I2C_MUX)
+       return i2c_bus_num_mux;
+#else
        return i2c_bus_num;
+#endif
 }
 
 unsigned int i2c_get_bus_speed(void)
This page took 0.04064 seconds and 4 git commands to generate.