common: Drop flash.h from common header
[oweals/u-boot.git] / cmd / eeprom.c
index e43566bc56db6baaec5c3324319a5d6d4919a083..792415ef93b6848b7348611de7da436624127e76 100644 (file)
@@ -1,8 +1,7 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * (C) Copyright 2000, 2001
  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 /*
@@ -23,6 +22,7 @@
 #include <common.h>
 #include <config.h>
 #include <command.h>
+#include <eeprom.h>
 #include <i2c.h>
 #include <eeprom_layout.h>
 
 #endif
 #endif
 
+#if defined(CONFIG_DM_I2C)
+static int eeprom_i2c_bus;
+#endif
+
 __weak int eeprom_write_enable(unsigned dev_addr, int state)
 {
        return 0;
@@ -67,13 +71,10 @@ __weak int eeprom_write_enable(unsigned dev_addr, int state)
 
 void eeprom_init(int bus)
 {
-       /* SPI EEPROM */
-#if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
-       spi_init_f();
-#endif
-
        /* I2C EEPROM */
-#if defined(CONFIG_SYS_I2C)
+#if defined(CONFIG_DM_I2C)
+       eeprom_i2c_bus = bus;
+#elif defined(CONFIG_SYS_I2C)
        if (bus >= 0)
                i2c_set_bus_num(bus);
        i2c_init(CONFIG_SYS_I2C_SPEED, CONFIG_SYS_I2C_SLAVE);
@@ -108,7 +109,7 @@ static int eeprom_len(unsigned offset, unsigned end)
 
        /*
         * For a FRAM device there is no limit on the number of the
-        * bytes that can be ccessed with the single read or write
+        * bytes that can be accessed with the single read or write
         * operation.
         */
 #if !defined(CONFIG_SYS_I2C_FRAM)
@@ -130,26 +131,32 @@ static int eeprom_rw_block(unsigned offset, uchar *addr, unsigned alen,
 {
        int ret = 0;
 
-       /* SPI */
-#if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
+#if defined(CONFIG_DM_I2C)
+       struct udevice *dev;
+
+       ret = i2c_get_chip_for_busnum(eeprom_i2c_bus, addr[0],
+                                     alen - 1, &dev);
+       if (ret) {
+               printf("%s: Cannot find udev for a bus %d\n", __func__,
+                      eeprom_i2c_bus);
+               return CMD_RET_FAILURE;
+       }
+
        if (read)
-               spi_read(addr, alen, buffer, len);
+               ret = dm_i2c_read(dev, offset, buffer, len);
        else
-               spi_write(addr, alen, buffer, len);
-#else  /* I2C */
+               ret = dm_i2c_write(dev, offset, buffer, len);
 
-#if defined(CONFIG_SYS_I2C_EEPROM_BUS)
-       i2c_set_bus_num(CONFIG_SYS_I2C_EEPROM_BUS);
-#endif
+#else /* Non DM I2C support - will be removed */
 
        if (read)
                ret = i2c_read(addr[0], offset, alen - 1, buffer, len);
        else
                ret = i2c_write(addr[0], offset, alen - 1, buffer, len);
-
+#endif /* CONFIG_DM_I2C */
        if (ret)
-               ret = 1;
-#endif
+               ret = CMD_RET_FAILURE;
+
        return ret;
 }
 
@@ -161,6 +168,10 @@ static int eeprom_rw(unsigned dev_addr, unsigned offset, uchar *buffer,
        int rcode = 0;
        uchar addr[3];
 
+#if defined(CONFIG_SYS_I2C_EEPROM_BUS)
+       eeprom_init(CONFIG_SYS_I2C_EEPROM_BUS);
+#endif
+
        while (offset < end) {
                alen = eeprom_addr(dev_addr, offset, addr);
 
@@ -265,10 +276,6 @@ __weak int eeprom_parse_layout_version(char *str)
 
 static unsigned char eeprom_buf[CONFIG_SYS_EEPROM_SIZE];
 
-#ifndef CONFIG_EEPROM_LAYOUT_HELP_STRING
-#define CONFIG_EEPROM_LAYOUT_HELP_STRING "<not defined>"
-#endif
-
 #endif
 
 enum eeprom_action {
@@ -301,7 +308,7 @@ static int eeprom_execute_command(enum eeprom_action action, int i2c_bus,
 {
        int rcode = 0;
        const char *const fmt =
-               "\nEEPROM @0x%lX %s: addr %08lx  off %04lx  count %ld ... ";
+               "\nEEPROM @0x%lX %s: addr 0x%08lx  off 0x%04lx  count %ld ... ";
 #ifdef CONFIG_CMD_EEPROM_LAYOUT
        struct eeprom_layout layout;
 #endif