thermal: imx_thermal: Do not print on error
[oweals/u-boot.git] / drivers / i2c / fsl_i2c.c
index 38455e1c664ba0dcaaa6511f2afc65b5301e2180..ff7f25a0ef7cdf2ce46cecc7954e654bc0d0d853 100644 (file)
@@ -38,7 +38,7 @@
  * generic value.
  */
 #ifndef CONFIG_I2C_TIMEOUT
-#define CONFIG_I2C_TIMEOUT     10000
+#define CONFIG_I2C_TIMEOUT     100000
 #endif
 
 #define I2C_READ_BIT  1
 
 DECLARE_GLOBAL_DATA_PTR;
 
-static const struct fsl_i2c *i2c_dev[2] = {
+static const struct fsl_i2c *i2c_dev[4] = {
        (struct fsl_i2c *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C_OFFSET),
 #ifdef CONFIG_SYS_FSL_I2C2_OFFSET
-       (struct fsl_i2c *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C2_OFFSET)
+       (struct fsl_i2c *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C2_OFFSET),
+#endif
+#ifdef CONFIG_SYS_FSL_I2C3_OFFSET
+       (struct fsl_i2c *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C3_OFFSET),
+#endif
+#ifdef CONFIG_SYS_FSL_I2C4_OFFSET
+       (struct fsl_i2c *)(CONFIG_SYS_IMMR + CONFIG_SYS_FSL_I2C4_OFFSET)
 #endif
 };
 
@@ -121,7 +127,7 @@ static const struct {
 static unsigned int set_i2c_bus_speed(const struct fsl_i2c *dev,
        unsigned int i2c_clk, unsigned int speed)
 {
-       unsigned short divider = min(i2c_clk / speed, (unsigned short) -1);
+       unsigned short divider = min(i2c_clk / speed, (unsigned int)USHRT_MAX);
 
        /*
         * We want to choose an FDR/DFSR that generates an I2C bus speed that
@@ -206,9 +212,58 @@ static unsigned int get_i2c_clock(int bus)
                return gd->arch.i2c1_clk;       /* I2C1 clock */
 }
 
+static int fsl_i2c_fixup(const struct fsl_i2c *dev)
+{
+       const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT);
+       unsigned long long timeval = 0;
+       int ret = -1;
+       unsigned int flags = 0;
+
+#ifdef CONFIG_SYS_FSL_ERRATUM_I2C_A004447
+       unsigned int svr = get_svr();
+       if ((SVR_SOC_VER(svr) == SVR_8548 && IS_SVR_REV(svr, 3, 1)) ||
+           (SVR_REV(svr) <= CONFIG_SYS_FSL_A004447_SVR_REV))
+               flags = I2C_CR_BIT6;
+#endif
+
+       writeb(I2C_CR_MEN | I2C_CR_MSTA, &dev->cr);
+
+       timeval = get_ticks();
+       while (!(readb(&dev->sr) & I2C_SR_MBB)) {
+               if ((get_ticks() - timeval) > timeout)
+                       goto err;
+       }
+
+       if (readb(&dev->sr) & I2C_SR_MAL) {
+               /* SDA is stuck low */
+               writeb(0, &dev->cr);
+               udelay(100);
+               writeb(I2C_CR_MSTA | flags, &dev->cr);
+               writeb(I2C_CR_MEN | I2C_CR_MSTA | flags, &dev->cr);
+       }
+
+       readb(&dev->dr);
+
+       timeval = get_ticks();
+       while (!(readb(&dev->sr) & I2C_SR_MIF)) {
+               if ((get_ticks() - timeval) > timeout)
+                       goto err;
+       }
+       ret = 0;
+
+err:
+       writeb(I2C_CR_MEN | flags, &dev->cr);
+       writeb(0, &dev->sr);
+       udelay(100);
+
+       return ret;
+}
+
 static void fsl_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
 {
        const struct fsl_i2c *dev;
+       const unsigned long long timeout = usec2ticks(CONFIG_I2C_MBB_TIMEOUT);
+       unsigned long long timeval;
 
 #ifdef CONFIG_SYS_I2C_INIT_BOARD
        /* Call board specific i2c bus reset routine before accessing the
@@ -226,6 +281,18 @@ static void fsl_i2c_init(struct i2c_adapter *adap, int speed, int slaveadd)
        writeb(0x0, &dev->sr);          /* clear status register */
        writeb(I2C_CR_MEN, &dev->cr);   /* start I2C controller */
 
+       timeval = get_ticks();
+       while (readb(&dev->sr) & I2C_SR_MBB) {
+               if ((get_ticks() - timeval) < timeout)
+                       continue;
+
+               if (fsl_i2c_fixup(dev))
+                       debug("i2c_init: BUS#%d failed to init\n",
+                             adap->hwadapnr);
+
+               break;
+       }
+
 #ifdef CONFIG_SYS_I2C_BOARD_LATE_INIT
        /* Call board specific i2c bus reset routine AFTER the bus has been
         * initialized. Use either this callpoint or i2c_init_board;
@@ -362,18 +429,45 @@ fsl_i2c_read(struct i2c_adapter *adap, u8 dev, uint addr, int alen, u8 *data,
        struct fsl_i2c *device = (struct fsl_i2c *)i2c_dev[adap->hwadapnr];
        int i = -1; /* signal error */
        u8 *a = (u8*)&addr;
+       int len = alen * -1;
 
        if (i2c_wait4bus(adap) < 0)
                return -1;
 
-       if ((!length || alen > 0)
-           && i2c_write_addr(adap, dev, I2C_WRITE_BIT, 0) != 0
-           && __i2c_write(adap, &a[4 - alen], alen) == alen)
-               i = 0; /* No error so far */
+       /* To handle the need of I2C devices that require to write few bytes
+        * (more than 4 bytes of address as in the case of else part)
+        * of data before reading, Negative equivalent of length(bytes to write)
+        * is passed, but used the +ve part of len for writing data
+        */
+       if (alen < 0) {
+               /* Generate a START and send the Address and
+                * the Tx Bytes to the slave.
+                * "START: Address: Write bytes data[len]"
+                * IF part supports writing any number of bytes in contrast
+                * to the else part, which supports writing address offset
+                * of upto 4 bytes only.
+                * bytes that need to be written are passed in
+                * "data", which will eventually keep the data READ,
+                * after writing the len bytes out of it
+                */
+               if (i2c_write_addr(adap, dev, I2C_WRITE_BIT, 0) != 0)
+                       i = __i2c_write(adap, data, len);
+
+               if (i != len)
+                       return -1;
 
-       if (length &&
-           i2c_write_addr(adap, dev, I2C_READ_BIT, alen ? 1 : 0) != 0)
-               i = __i2c_read(adap, data, length);
+               if (length && i2c_write_addr(adap, dev, I2C_READ_BIT, 1) != 0)
+                       i = __i2c_read(adap, data, length);
+       } else {
+               if ((!length || alen > 0) &&
+                   i2c_write_addr(adap, dev, I2C_WRITE_BIT, 0) != 0  &&
+                   __i2c_write(adap, &a[4 - alen], alen) == alen)
+                       i = 0; /* No error so far */
+
+               if (length &&
+                   i2c_write_addr(adap, dev, I2C_READ_BIT, alen ? 1 : 0) != 0)
+                       i = __i2c_read(adap, data, length);
+       }
 
        writeb(I2C_CR_MEN, &device->cr);
 
@@ -394,8 +488,10 @@ fsl_i2c_write(struct i2c_adapter *adap, u8 dev, uint addr, int alen,
        int i = -1; /* signal error */
        u8 *a = (u8*)&addr;
 
-       if (i2c_wait4bus(adap) >= 0 &&
-           i2c_write_addr(adap, dev, I2C_WRITE_BIT, 0) != 0 &&
+       if (i2c_wait4bus(adap) < 0)
+               return -1;
+
+       if (i2c_write_addr(adap, dev, I2C_WRITE_BIT, 0) != 0 &&
            __i2c_write(adap, &a[4 - alen], alen) == alen) {
                i = __i2c_write(adap, data, length);
        }
@@ -449,3 +545,15 @@ U_BOOT_I2C_ADAP_COMPLETE(fsl_1, fsl_i2c_init, fsl_i2c_probe, fsl_i2c_read,
                         CONFIG_SYS_FSL_I2C2_SPEED, CONFIG_SYS_FSL_I2C2_SLAVE,
                         1)
 #endif
+#ifdef CONFIG_SYS_FSL_I2C3_OFFSET
+U_BOOT_I2C_ADAP_COMPLETE(fsl_2, fsl_i2c_init, fsl_i2c_probe, fsl_i2c_read,
+                        fsl_i2c_write, fsl_i2c_set_bus_speed,
+                        CONFIG_SYS_FSL_I2C3_SPEED, CONFIG_SYS_FSL_I2C3_SLAVE,
+                        2)
+#endif
+#ifdef CONFIG_SYS_FSL_I2C4_OFFSET
+U_BOOT_I2C_ADAP_COMPLETE(fsl_3, fsl_i2c_init, fsl_i2c_probe, fsl_i2c_read,
+                        fsl_i2c_write, fsl_i2c_set_bus_speed,
+                        CONFIG_SYS_FSL_I2C4_SPEED, CONFIG_SYS_FSL_I2C4_SLAVE,
+                        3)
+#endif