Merge branch '2020-06-03-misc-bugfixes'
[oweals/u-boot.git] / drivers / i2c / fsl_i2c.c
index cb0f5ea2333fbe9bb0f6c402fa0a76597ec7162c..da933452f5156c16b2195dff904cc0cd78ae28aa 100644 (file)
@@ -1,19 +1,22 @@
+// SPDX-License-Identifier: GPL-2.0
 /*
  * Copyright 2006,2009 Freescale Semiconductor, Inc.
  *
  * 2012, Heiko Schocher, DENX Software Engineering, hs@denx.de.
  * Changes for multibus/multiadapter I2C support.
- *
- * SPDX-License-Identifier:    GPL-2.0
  */
 
 #include <common.h>
 #include <command.h>
 #include <i2c.h>               /* Functional interface */
+#include <log.h>
+#include <time.h>
 #include <asm/io.h>
 #include <asm/fsl_i2c.h>       /* HW definitions */
+#include <clk.h>
 #include <dm.h>
 #include <mapmem.h>
+#include <linux/delay.h>
 
 /* 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
@@ -573,11 +576,9 @@ static int fsl_i2c_set_bus_speed(struct udevice *bus, uint speed)
 static int fsl_i2c_ofdata_to_platdata(struct udevice *bus)
 {
        struct fsl_i2c_dev *dev = dev_get_priv(bus);
-       fdt_addr_t addr;
-
-       addr = dev_read_u32_default(bus, "reg", -1);
+       struct clk clock;
 
-       dev->base = map_sysmem(CONFIG_SYS_IMMR + addr, sizeof(struct fsl_i2c_base));
+       dev->base = map_sysmem(dev_read_addr(bus), sizeof(struct fsl_i2c_base));
 
        if (!dev->base)
                return -ENOMEM;
@@ -585,9 +586,14 @@ static int fsl_i2c_ofdata_to_platdata(struct udevice *bus)
        dev->index = dev_read_u32_default(bus, "cell-index", -1);
        dev->slaveadd = dev_read_u32_default(bus, "u-boot,i2c-slave-addr",
                                             0x7f);
-       dev->speed = dev_read_u32_default(bus, "clock-frequency", 400000);
+       dev->speed = dev_read_u32_default(bus, "clock-frequency",
+                                         I2C_SPEED_FAST_RATE);
 
-       dev->i2c_clk = dev->index ? gd->arch.i2c2_clk : gd->arch.i2c1_clk;
+       if (!clk_get_by_index(bus, 0, &clock))
+               dev->i2c_clk = clk_get_rate(&clock);
+       else
+               dev->i2c_clk = dev->index ? gd->arch.i2c2_clk :
+                                           gd->arch.i2c1_clk;
 
        return 0;
 }