]> git.sur5r.net Git - u-boot/blobdiff - drivers/fsl_i2c.c
Fix two typos.
[u-boot] / drivers / fsl_i2c.c
index f00e8026bc62fcd4e3f6ce9ab6a553b53019bb4d..22485ea916f4a958d31892192c277a48e29a05ae 100644 (file)
@@ -29,6 +29,9 @@
 
 #define I2C_TIMEOUT    (CFG_HZ / 4)
 
+#define I2C_READ_BIT  1
+#define I2C_WRITE_BIT 0
+
 /* Initialize the bus pointer to whatever one the SPD EEPROM is on.
  * Default is bus 0.  This is necessary because the DDR initialization
  * runs from ROM, and we can't switch buses because we can't modify
@@ -55,6 +58,7 @@ i2c_init(int speed, int slaveadd)
        dev = (struct fsl_i2c *) (CFG_IMMR + CFG_I2C_OFFSET);
 
        writeb(0, &dev->cr);                    /* stop I2C controller */
+       udelay(5);                              /* let it shutdown in peace */
        writeb(0x3F, &dev->fdr);                /* set bus speed */
        writeb(0x3F, &dev->dfsrr);              /* set default filter */
        writeb(slaveadd << 1, &dev->adr);       /* write slave address */
@@ -65,9 +69,10 @@ i2c_init(int speed, int slaveadd)
        dev = (struct fsl_i2c *) (CFG_IMMR + CFG_I2C2_OFFSET);
 
        writeb(0, &dev->cr);                    /* stop I2C controller */
+       udelay(5);                              /* let it shutdown in peace */
        writeb(0x3F, &dev->fdr);                /* set bus speed */
        writeb(0x3F, &dev->dfsrr);              /* set default filter */
-       writeb(slaveadd, &dev->adr);            /* write slave address */
+       writeb(slaveadd << 1, &dev->adr);       /* write slave address */
        writeb(0x0, &dev->sr);                  /* clear status register */
        writeb(I2C_CR_MEN, &dev->cr);           /* start I2C controller */
 #endif /* CFG_I2C2_OFFSET */
@@ -110,7 +115,7 @@ i2c_wait(int write)
                        return -1;
                }
 
-               if (write == I2C_WRITE && (csr & I2C_SR_RXAK)) {
+               if (write == I2C_WRITE_BIT && (csr & I2C_SR_RXAK)) {
                        debug("i2c_wait: No RXACK\n");
                        return -1;
                }
@@ -131,7 +136,7 @@ i2c_write_addr (u8 dev, u8 dir, int rsta)
 
        writeb((dev << 1) | dir, &i2c_dev[i2c_bus_num]->dr);
 
-       if (i2c_wait(I2C_WRITE) < 0)
+       if (i2c_wait(I2C_WRITE_BIT) < 0)
                return 0;
 
        return 1;
@@ -148,7 +153,7 @@ __i2c_write(u8 *data, int length)
        for (i = 0; i < length; i++) {
                writeb(data[i], &i2c_dev[i2c_bus_num]->dr);
 
-               if (i2c_wait(I2C_WRITE) < 0)
+               if (i2c_wait(I2C_WRITE_BIT) < 0)
                        break;
        }
 
@@ -167,7 +172,7 @@ __i2c_read(u8 *data, int length)
        readb(&i2c_dev[i2c_bus_num]->dr);
 
        for (i = 0; i < length; i++) {
-               if (i2c_wait(I2C_READ) < 0)
+               if (i2c_wait(I2C_READ_BIT) < 0)
                        break;
 
                /* Generate ack on last next to last byte */
@@ -188,15 +193,17 @@ __i2c_read(u8 *data, int length)
 int
 i2c_read(u8 dev, uint addr, int alen, u8 *data, int length)
 {
-       int i = 0;
+       int i = -1; /* signal error */
        u8 *a = (u8*)&addr;
 
        if (i2c_wait4bus() >= 0
-           && i2c_write_addr(dev, I2C_WRITE, 0) != 0
-           && __i2c_write(&a[4 - alen], alen) == alen
-           && i2c_write_addr(dev, I2C_READ, 1) != 0) {
+           && i2c_write_addr(dev, I2C_WRITE_BIT, 0) != 0
+           && __i2c_write(&a[4 - alen], alen) == alen)
+               i = 0; /* No error so far */
+
+       if (length
+           && i2c_write_addr(dev, I2C_READ_BIT, 1) != 0)
                i = __i2c_read(data, length);
-       }
 
        writeb(I2C_CR_MEN, &i2c_dev[i2c_bus_num]->cr);
 
@@ -209,11 +216,11 @@ i2c_read(u8 dev, uint addr, int alen, u8 *data, int length)
 int
 i2c_write(u8 dev, uint addr, int alen, u8 *data, int length)
 {
-       int i = 0;
+       int i = -1; /* signal error */
        u8 *a = (u8*)&addr;
 
        if (i2c_wait4bus() >= 0
-           && i2c_write_addr(dev, I2C_WRITE, 0) != 0
+           && i2c_write_addr(dev, I2C_WRITE_BIT, 0) != 0
            && __i2c_write(&a[4 - alen], alen) == alen) {
                i = __i2c_write(data, length);
        }
@@ -229,16 +236,14 @@ i2c_write(u8 dev, uint addr, int alen, u8 *data, int length)
 int
 i2c_probe(uchar chip)
 {
-       int tmp;
-
-       /*
-        * Try to read the first location of the chip.  The underlying
-        * driver doesn't appear to support sending just the chip address
-        * and looking for an <ACK> back.
+       /* For unknow reason the controller will ACK when
+        * probing for a slave with the same address, so skip
+        * it.
         */
-       udelay(10000);
+       if (chip == (readb(&i2c_dev[i2c_bus_num]->adr) >> 1))
+               return -1;
 
-       return i2c_read(chip, 0, 1, (uchar *)&tmp, 1);
+       return i2c_read(chip, 0, 0, NULL, 0);
 }
 
 uchar