]> git.sur5r.net Git - u-boot/blobdiff - drivers/i2c/mv_i2c.c
Merge branch 'master' of git://git.denx.de/u-boot-uniphier
[u-boot] / drivers / i2c / mv_i2c.c
index 341126a0ab1651fc156fb7bce26cbbc170701e7f..8a56ef9a245977d788ca4c29934774bcd6cd5a82 100644 (file)
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * (C) Copyright 2000
  * Paolo Scaffardi, AIRVENT SAM s.p.a - RIMINI(ITALY), arsenio@tin.it
@@ -11,8 +12,6 @@
  * (C) Copyright 2011 Marvell Inc.
  * Lei Wen <leiwen@marvell.com>
  *
- * SPDX-License-Identifier:    GPL-2.0+
- *
  * Back ported to the 8xx platform (from the 8260 platform) by
  * Murray.Jensen@cmst.csiro.au, 27-Jan-01.
  */
@@ -68,6 +67,10 @@ __weak void i2c_clk_enable(void)
  */
 static void i2c_reset(struct mv_i2c *base)
 {
+       u32 icr_mode;
+
+       /* Save bus mode (standard or fast speed) for later use */
+       icr_mode = readl(&base->icr) & ICR_MODE_MASK;
        writel(readl(&base->icr) & ~ICR_IUE, &base->icr); /* disable unit */
        writel(readl(&base->icr) | ICR_UR, &base->icr);   /* reset the unit */
        udelay(100);
@@ -76,7 +79,8 @@ static void i2c_reset(struct mv_i2c *base)
        i2c_clk_enable();
 
        writel(CONFIG_SYS_I2C_SLAVE, &base->isar); /* set our slave address */
-       writel(I2C_ICR_INIT, &base->icr); /* set control reg values */
+       /* set control reg values */
+       writel(I2C_ICR_INIT | icr_mode, &base->icr);
        writel(I2C_ISR_INIT, &base->isr); /* set clear interrupt bits */
        writel(readl(&base->icr) | ICR_IUE, &base->icr); /* enable unit */
        udelay(100);
@@ -238,6 +242,11 @@ static int __i2c_read(struct mv_i2c *base, uchar chip, u8 *addr, int alen,
        debug("i2c_read(chip=0x%02x, addr=0x%02x, alen=0x%02x, "
              "len=0x%02x)\n", chip, *addr, alen, len);
 
+       if (len == 0) {
+               printf("reading zero byte is invalid\n");
+               return -EINVAL;
+       }
+
        i2c_reset(base);
 
        /* dummy chip address write */
@@ -260,7 +269,7 @@ static int __i2c_read(struct mv_i2c *base, uchar chip, u8 *addr, int alen,
                msg.condition = I2C_COND_NORMAL;
                msg.acknack   = I2C_ACKNAK_WAITACK;
                msg.direction = I2C_WRITE;
-               msg.data      = *(addr++);
+               msg.data      = addr[alen];
                if (i2c_transfer(base, &msg))
                        return -1;
        }
@@ -331,7 +340,7 @@ static int __i2c_write(struct mv_i2c *base, uchar chip, u8 *addr, int alen,
                msg.condition = I2C_COND_NORMAL;
                msg.acknack   = I2C_ACKNAK_WAITACK;
                msg.direction = I2C_WRITE;
-               msg.data      = *(addr++);
+               msg.data      = addr[alen];
                if (i2c_transfer(base, &msg))
                        return -1;
        }
@@ -416,6 +425,8 @@ unsigned int i2c_get_bus_num(void)
 /* API Functions */
 void i2c_init(int speed, int slaveaddr)
 {
+       u32 val;
+
 #ifdef CONFIG_I2C_MULTI_BUS
        current_bus = 0;
        base_glob = (struct mv_i2c *)i2c_regs[current_bus];
@@ -423,6 +434,12 @@ void i2c_init(int speed, int slaveaddr)
        base_glob = (struct mv_i2c *)CONFIG_MV_I2C_REG;
 #endif
 
+       if (speed > 100000)
+               val = ICR_FM;
+       else
+               val = ICR_SM;
+       clrsetbits_le32(&base_glob->icr, ICR_MODE_MASK, val);
+
        i2c_board_init(base_glob);
 }
 
@@ -543,17 +560,32 @@ static int mv_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
                                   omsg->len, dmsg->buf, dmsg->len);
 }
 
+static int mv_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
+{
+       struct mv_i2c_priv *priv = dev_get_priv(bus);
+       u32 val;
+
+       if (speed > 100000)
+               val = ICR_FM;
+       else
+               val = ICR_SM;
+       clrsetbits_le32(&priv->base->icr, ICR_MODE_MASK, val);
+
+       return 0;
+}
+
 static int mv_i2c_probe(struct udevice *bus)
 {
        struct mv_i2c_priv *priv = dev_get_priv(bus);
 
-       priv->base = (void *)dev_get_addr_ptr(bus);
+       priv->base = (void *)devfdt_get_addr_ptr(bus);
 
        return 0;
 }
 
 static const struct dm_i2c_ops mv_i2c_ops = {
        .xfer           = mv_i2c_xfer,
+       .set_bus_speed  = mv_i2c_set_bus_speed,
 };
 
 static const struct udevice_id mv_i2c_ids[] = {