]> git.sur5r.net Git - u-boot/blobdiff - drivers/i2c/mvtwsi.c
i2c: mvtwsi.c: Avoid NULL dereference
[u-boot] / drivers / i2c / mvtwsi.c
index ab7481a0d4a9ec80e6d3e792715679b893270424..30bee0d52e598d281df2679c7a5a0bce7865e70a 100644 (file)
@@ -10,7 +10,7 @@
 
 #include <common.h>
 #include <i2c.h>
-#include <asm/errno.h>
+#include <linux/errno.h>
 #include <asm/io.h>
 #include <linux/compat.h>
 #ifdef CONFIG_DM_I2C
@@ -29,18 +29,26 @@ DECLARE_GLOBAL_DATA_PTR;
 #include <asm/arch/orion5x.h>
 #elif (defined(CONFIG_KIRKWOOD) || defined(CONFIG_ARCH_MVEBU))
 #include <asm/arch/soc.h>
-#elif defined(CONFIG_SUNXI)
+#elif defined(CONFIG_ARCH_SUNXI)
 #include <asm/arch/i2c.h>
 #else
 #error Driver mvtwsi not supported by SoC or board
 #endif
 #endif /* CONFIG_DM_I2C */
 
+/*
+ * On SUNXI, we get CONFIG_SYS_TCLK from this include, so we want to
+ * always have it.
+ */
+#if defined(CONFIG_DM_I2C) && defined(CONFIG_ARCH_SUNXI)
+#include <asm/arch/i2c.h>
+#endif
+
 /*
  * TWSI register structure
  */
 
-#ifdef CONFIG_SUNXI
+#ifdef CONFIG_ARCH_SUNXI
 
 struct  mvtwsi_registers {
        u32 slave_address;
@@ -399,7 +407,7 @@ static int twsi_stop(struct mvtwsi_registers *twsi, uint tick)
  */
 static uint twsi_calc_freq(const int n, const int m)
 {
-#ifdef CONFIG_SUNXI
+#ifdef CONFIG_ARCH_SUNXI
        return CONFIG_SYS_TCLK / (10 * (m + 1) * (1 << n));
 #else
        return CONFIG_SYS_TCLK / (10 * (m + 1) * (2 << n));
@@ -482,7 +490,8 @@ static void __twsi_i2c_init(struct mvtwsi_registers *twsi, int speed,
        /* Reset controller */
        twsi_reset(twsi);
        /* Set speed */
-       *actual_speed = __twsi_i2c_set_bus_speed(twsi, speed);
+       if (actual_speed)
+               *actual_speed = __twsi_i2c_set_bus_speed(twsi, speed);
        /* Set slave address; even though we don't use it */
        writel(slaveadd, &twsi->slave_address);
        writel(0, &twsi->xtnd_slave_addr);
@@ -595,7 +604,7 @@ static int __twsi_i2c_read(struct mvtwsi_registers *twsi, uchar chip,
                status = i2c_begin(twsi, expected_start, (chip << 1), tick);
                /* Send address bytes */
                while ((status == 0) && alen--)
-                       status = twsi_send(twsi, *(addr++),
+                       status = twsi_send(twsi, addr[alen],
                                           MVTWSI_STATUS_DATA_W_ACK, tick);
                /* Send repeated STARTs after the initial START */
                expected_start = MVTWSI_STATUS_REPEATED_START;
@@ -642,7 +651,7 @@ static int __twsi_i2c_write(struct mvtwsi_registers *twsi, uchar chip,
        status = i2c_begin(twsi, MVTWSI_STATUS_START, (chip << 1), tick);
        /* Send address bytes */
        while ((status == 0) && (alen-- > 0))
-               status = twsi_send(twsi, *(addr++), MVTWSI_STATUS_DATA_W_ACK,
+               status = twsi_send(twsi, addr[alen], MVTWSI_STATUS_DATA_W_ACK,
                                   tick);
        /* Send data bytes */
        while ((status == 0) && (length-- > 0))
@@ -770,16 +779,16 @@ static int mvtwsi_i2c_ofdata_to_platdata(struct udevice *bus)
 {
        struct mvtwsi_i2c_dev *dev = dev_get_priv(bus);
 
-       dev->base = dev_get_addr_ptr(bus);
+       dev->base = devfdt_get_addr_ptr(bus);
 
        if (!dev->base)
                return -ENOMEM;
 
-       dev->index = fdtdec_get_int(gd->fdt_blob, bus->of_offset,
+       dev->index = fdtdec_get_int(gd->fdt_blob, dev_of_offset(bus),
                                    "cell-index", -1);
-       dev->slaveadd = fdtdec_get_int(gd->fdt_blob, bus->of_offset,
+       dev->slaveadd = fdtdec_get_int(gd->fdt_blob, dev_of_offset(bus),
                                       "u-boot,i2c-slave-addr", 0x0);
-       dev->speed = fdtdec_get_int(gd->fdt_blob, bus->of_offset,
+       dev->speed = fdtdec_get_int(gd->fdt_blob, dev_of_offset(bus),
                                    "clock-frequency", 100000);
        return 0;
 }
@@ -830,6 +839,8 @@ static const struct dm_i2c_ops mvtwsi_i2c_ops = {
 
 static const struct udevice_id mvtwsi_i2c_ids[] = {
        { .compatible = "marvell,mv64xxx-i2c", },
+       { .compatible = "marvell,mv78230-i2c", },
+       { .compatible = "allwinner,sun6i-a31-i2c", },
        { /* sentinel */ }
 };