[U-Boot] [PATCH] mvgbe: fix network device indices

Michael Walle michael at walle.cc
Fri Oct 7 00:23:22 CEST 2011


Don't assume that the MAC address of egiga0 rsp. egiga1 is ethaddr rsp.
eth1addr. If there is only a egiga1 device, u-boot will enumerate it as
device 0 and therefore the MAC address is set with the environmen varibale
ethaddr.

Signed-off-by: Michael Walle <michael at walle.cc>
---
 drivers/net/mvgbe.c |   13 +++++++------
 include/net.h       |   12 ++++++++++++
 net/eth.c           |    8 ++++++++
 3 files changed, 27 insertions(+), 6 deletions(-)

diff --git a/drivers/net/mvgbe.c b/drivers/net/mvgbe.c
index c701f43..738e8d3 100644
--- a/drivers/net/mvgbe.c
+++ b/drivers/net/mvgbe.c
@@ -645,7 +645,7 @@ int mvgbe_initialize(bd_t *bis)
 	struct mvgbe_device *dmvgbe;
 	struct eth_device *dev;
 	int devnum;
-	char *s;
+	int eth_idx = 0;
 	u8 used_ports[MAX_MVGBE_DEVS] = CONFIG_MVGBE_PORTS;
 
 	for (devnum = 0; devnum < MAX_MVGBE_DEVS; devnum++) {
@@ -700,16 +700,13 @@ error1:
 		/* must be less than NAMESIZE (16) */
 		sprintf(dev->name, "egiga%d", devnum);
 
-		/* Extract the MAC address from the environment */
 		switch (devnum) {
 		case 0:
 			dmvgbe->regs = (void *)MVGBE0_BASE;
-			s = "ethaddr";
 			break;
 #if defined(MVGBE1_BASE)
 		case 1:
 			dmvgbe->regs = (void *)MVGBE1_BASE;
-			s = "eth1addr";
 			break;
 #endif
 		default:	/* this should never happen */
@@ -718,7 +715,9 @@ error1:
 			return -1;
 		}
 
-		while (!eth_getenv_enetaddr(s, dev->enetaddr)) {
+		/* Extract the MAC address from the environment */
+		while (!eth_getenv_enetaddr_by_index("eth", eth_idx,
+					dev->enetaddr)) {
 			/* Generate Private MAC addr if not set */
 			dev->enetaddr[0] = 0x02;
 			dev->enetaddr[1] = 0x50;
@@ -734,7 +733,7 @@ error1:
 			dev->enetaddr[4] = get_random_hex();
 			dev->enetaddr[5] = get_random_hex();
 #endif
-			eth_setenv_enetaddr(s, dev->enetaddr);
+			eth_setenv_enetaddr_by_index("eth", eth_idx, dev->enetaddr);
 		}
 
 		dev->init = (void *)mvgbe_init;
@@ -745,6 +744,8 @@ error1:
 
 		eth_register(dev);
 
+		eth_idx++;
+
 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
 		miiphy_register(dev->name, smi_reg_read, smi_reg_write);
 		/* Set phy address of the port */
diff --git a/include/net.h b/include/net.h
index d5d37b6..d378cd2 100644
--- a/include/net.h
+++ b/include/net.h
@@ -103,6 +103,18 @@ extern int eth_setenv_enetaddr(char *name, const uchar *enetaddr);
 extern int eth_getenv_enetaddr_by_index(const char *base_name, int index,
 					uchar *enetaddr);
 
+/*
+ * Set the hardware address for an ethernet interface.
+ * Args:
+ *	base_name - base name for device (normally "eth")
+ *	index - device index number (0 for first)
+ *	enetaddr - returns 6 byte hardware address
+ * Returns:
+ *	Return true if the environment varibable was set successfully.
+ */
+extern int eth_setenv_enetaddr_by_index(const char *base_name, int index,
+					const uchar *enetaddr);
+
 extern int usb_eth_initialize(bd_t *bi);
 extern int eth_init(bd_t *bis);			/* Initialize the device */
 extern int eth_send(volatile void *packet, int length);	   /* Send a packet */
diff --git a/net/eth.c b/net/eth.c
index 4280d6d..a8f68fc 100644
--- a/net/eth.c
+++ b/net/eth.c
@@ -62,6 +62,14 @@ int eth_getenv_enetaddr_by_index(const char *base_name, int index,
 	return eth_getenv_enetaddr(enetvar, enetaddr);
 }
 
+int eth_setenv_enetaddr_by_index(const char *base_name, int index,
+				 const uchar *enetaddr)
+{
+	char enetvar[32];
+	sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
+	return eth_setenv_enetaddr(enetvar, enetaddr);
+}
+
 static int eth_mac_skip(int index)
 {
 	char enetvar[15];
-- 
1.7.2.5



More information about the U-Boot mailing list