[U-Boot] [PATCH 14/25] eeprom: Pull out the I/O code

Marek Vasut marex at denx.de
Mon Oct 20 03:48:15 CEST 2014


Pull out the code which does the I2C or SPI read/write, so that
the beefy ifdef around it is contained in a single function.

Signed-off-by: Marek Vasut <marex at denx.de>
Cc: Simon Glass <sjg at chromium.org>
Cc: Tom Rini <trini at ti.com>
Cc: Heiko Schocher <hs at denx.de>
---
 common/cmd_eeprom.c | 38 ++++++++++++++++++++++++++------------
 1 file changed, 26 insertions(+), 12 deletions(-)

diff --git a/common/cmd_eeprom.c b/common/cmd_eeprom.c
index d00598b..b956233 100644
--- a/common/cmd_eeprom.c
+++ b/common/cmd_eeprom.c
@@ -66,6 +66,29 @@ void eeprom_init(void)
 #endif
 }
 
+static int eeprom_rw_block(unsigned offset, uchar *addr, unsigned alen,
+			   uchar *buffer, unsigned len, bool read)
+{
+	int ret = 0;
+
+	/* SPI */
+#if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
+	if (read)
+		spi_read(addr, alen, buffer, len);
+	else
+		spi_write(addr, alen, buffer, len);
+#else	/* I2C */
+	if (read)
+		ret = i2c_read(addr[0], offset, alen - 1, buffer, len);
+	else
+		ret = i2c_write(addr[0], offset, alen - 1, buffer, len);
+
+	if (ret)
+		ret = 1;
+#endif
+	return ret;
+}
+
 int eeprom_read (unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt)
 {
 	unsigned end = offset + cnt;
@@ -119,12 +142,8 @@ int eeprom_read (unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt
 			len = maxlen;
 #endif
 
-#if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
-		spi_read (addr, alen, buffer, len);
-#else
-		if (i2c_read(addr[0], offset, alen - 1, buffer, len))
-			rcode = 1;
-#endif
+		rcode = eeprom_rw_block(offset, addr, alen, buffer, len, 1);
+
 		buffer += len;
 		offset += len;
 	}
@@ -199,13 +218,8 @@ int eeprom_write (unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cn
 			len = maxlen;
 #endif
 
-#if defined(CONFIG_SPI) && !defined(CONFIG_ENV_EEPROM_IS_ON_I2C)
-		spi_write (addr, alen, buffer, len);
-#else
-		if (i2c_write(addr[0], offset, alen - 1, buffer, len))
-			rcode = 1;
+		rcode = eeprom_rw_block(offset, addr, alen, buffer, len, 0);
 
-#endif
 		buffer += len;
 		offset += len;
 
-- 
2.1.1



More information about the U-Boot mailing list