[U-Boot] [PATCH 1/2] board: sama5d2_xplained: Move setting ethaddr to common

Wenyou Yang wenyou.yang at microchip.com
Fri Jul 28 05:04:08 UTC 2017


The code to set ethernet mac address can be shared by other boards,
so move such code to board/$(VENDOR)/common directory.

Signed-off-by: Wenyou Yang <wenyou.yang at microchip.com>
---

 arch/arm/mach-at91/include/mach/at91_common.h   |  2 +
 board/atmel/common/Makefile                     |  8 ++++
 board/atmel/common/i2c_ethaddr.c                | 35 ++++++++++++++++++
 board/atmel/sama5d2_xplained/sama5d2_xplained.c | 49 +++----------------------
 include/configs/sama5d2_xplained.h              |  4 --
 5 files changed, 51 insertions(+), 47 deletions(-)
 create mode 100644 board/atmel/common/Makefile
 create mode 100644 board/atmel/common/i2c_ethaddr.c

diff --git a/arch/arm/mach-at91/include/mach/at91_common.h b/arch/arm/mach-at91/include/mach/at91_common.h
index 0742ffc56f..03d7b4cb0f 100644
--- a/arch/arm/mach-at91/include/mach/at91_common.h
+++ b/arch/arm/mach-at91/include/mach/at91_common.h
@@ -36,4 +36,6 @@ void matrix_init(void);
 void redirect_int_from_saic_to_aic(void);
 void configure_2nd_sram_as_l2_cache(void);
 
+int at91_set_ethaddr(int busnum, int address, int offset);
+
 #endif /* AT91_COMMON_H */
diff --git a/board/atmel/common/Makefile b/board/atmel/common/Makefile
new file mode 100644
index 0000000000..0fa147b86f
--- /dev/null
+++ b/board/atmel/common/Makefile
@@ -0,0 +1,8 @@
+#
+# Copyright (C) 2017 Microchip
+#		      Wenyou Yang <wenyou.yang at microchip.com>
+#
+# SPDX-License-Identifier:	GPL-2.0+
+#
+
+obj-$(CONFIG_DM_I2C) += i2c_ethaddr.o
diff --git a/board/atmel/common/i2c_ethaddr.c b/board/atmel/common/i2c_ethaddr.c
new file mode 100644
index 0000000000..810d22e5e6
--- /dev/null
+++ b/board/atmel/common/i2c_ethaddr.c
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) 2017 Microchip
+ *		      Wenyou Yang <wenyou.yang at microchip.com>
+ *
+ * SPDX-License-Identifier:	GPL-2.0+
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <i2c.h>
+
+int at91_set_ethaddr(int busnum, int address, int offset)
+{
+	const int ETH_ADDR_LEN = 6;
+	unsigned char ethaddr[ETH_ADDR_LEN];
+	const char *ETHADDR_NAME = "ethaddr";
+	struct udevice *bus, *dev;
+
+	if (getenv(ETHADDR_NAME))
+		return 0;
+
+	if (uclass_get_device_by_seq(UCLASS_I2C, busnum, &bus))
+		return -1;
+
+	if (dm_i2c_probe(bus, address, 0, &dev))
+		return -1;
+
+	if (dm_i2c_read(dev, offset, ethaddr, ETH_ADDR_LEN))
+		return -1;
+
+	if (!is_valid_ethaddr(ethaddr))
+		return -1;
+
+	return eth_setenv_enetaddr(ETHADDR_NAME, ethaddr);
+}
diff --git a/board/atmel/sama5d2_xplained/sama5d2_xplained.c b/board/atmel/sama5d2_xplained/sama5d2_xplained.c
index 48f45b35ce..ceb421d4fb 100644
--- a/board/atmel/sama5d2_xplained/sama5d2_xplained.c
+++ b/board/atmel/sama5d2_xplained/sama5d2_xplained.c
@@ -8,8 +8,6 @@
 #include <common.h>
 #include <atmel_hlcdc.h>
 #include <debug_uart.h>
-#include <dm.h>
-#include <i2c.h>
 #include <lcd.h>
 #include <version.h>
 #include <asm/io.h>
@@ -161,51 +159,16 @@ int dram_init(void)
 	return 0;
 }
 
-#ifdef CONFIG_CMD_I2C
-static int set_ethaddr_from_eeprom(void)
-{
-	const int ETH_ADDR_LEN = 6;
-	unsigned char ethaddr[ETH_ADDR_LEN];
-	const char *ETHADDR_NAME = "ethaddr";
-	struct udevice *bus, *dev;
-
-	if (getenv(ETHADDR_NAME))
-		return 0;
-
-	if (uclass_get_device_by_seq(UCLASS_I2C, 1, &bus)) {
-		printf("Cannot find I2C bus 1\n");
-		return -1;
-	}
-
-	if (dm_i2c_probe(bus, AT24MAC_ADDR, 0, &dev)) {
-		printf("Failed to probe I2C chip\n");
-		return -1;
-	}
-
-	if (dm_i2c_read(dev, AT24MAC_REG, ethaddr, ETH_ADDR_LEN)) {
-		printf("Failed to read ethernet address from EEPROM\n");
-		return -1;
-	}
-
-	if (!is_valid_ethaddr(ethaddr)) {
-		printf("The ethernet address read from EEPROM is not valid!\n");
-		return -1;
-	}
-
-	return eth_setenv_enetaddr(ETHADDR_NAME, ethaddr);
-}
-#else
-static int set_ethaddr_from_eeprom(void)
-{
-	return 0;
-}
-#endif
+#define AT24MAC_ON_I2C_BUS	1
+#define AT24MAC_ADDR		0x5c
+#define AT24MAC_REG		0x9a
 
 #ifdef CONFIG_MISC_INIT_R
 int misc_init_r(void)
 {
-	set_ethaddr_from_eeprom();
-
+#ifdef CONFIG_DM_I2C
+	at91_set_ethaddr(AT24MAC_ON_I2C_BUS, AT24MAC_ADDR, AT24MAC_REG);
+#endif
 	return 0;
 }
 #endif
diff --git a/include/configs/sama5d2_xplained.h b/include/configs/sama5d2_xplained.h
index 42fb1e11d0..bcddd5b0fe 100644
--- a/include/configs/sama5d2_xplained.h
+++ b/include/configs/sama5d2_xplained.h
@@ -38,10 +38,6 @@
 /* NAND flash */
 #undef CONFIG_CMD_NAND
 
-/* I2C */
-#define AT24MAC_ADDR		0x5c
-#define AT24MAC_REG		0x9a
-
 /* LCD */
 
 #ifdef CONFIG_LCD
-- 
2.13.0



More information about the U-Boot mailing list