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

Michael Walle
2011-10-27T21:31:36Z

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. Instead use the correct index number stored within the eth_device
structure.

Signed-off-by: Michael Walle <michael@walle.cc>
Cc: Prafulla Wadaskar <prafulla@marvell.com>
---
drivers/net/mvgbe.c | 27 +++++++++++++--------------
include/net.h | 12 ++++++++++++
net/eth.c | 17 ++++++++++++++++-
3 files changed, 41 insertions(+), 15 deletions(-)

diff --git a/drivers/net/mvgbe.c b/drivers/net/mvgbe.c
index c701f43..80314ec 100644
--- a/drivers/net/mvgbe.c
+++ b/drivers/net/mvgbe.c
@@ -645,7 +645,6 @@ int mvgbe_initialize(bd_t *bis)
struct mvgbe_device *dmvgbe;
struct eth_device *dev;
int devnum;
- char *s;
u8 used_ports[MAX_MVGBE_DEVS] = CONFIG_MVGBE_PORTS;

for (devnum = 0; devnum < MAX_MVGBE_DEVS; devnum++) {
@@ -700,16 +699,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 +714,17 @@ error1:
return -1;
}

- while (!eth_getenv_enetaddr(s, dev->enetaddr)) {
+ dev->init = (void *)mvgbe_init;
+ dev->halt = (void *)mvgbe_halt;
+ dev->send = (void *)mvgbe_send;
+ dev->recv = (void *)mvgbe_recv;
+ dev->write_hwaddr = (void *)mvgbe_write_hwaddr;
+
+ eth_register(dev);
+
+ /* Extract the MAC address from the environment */
+ while (!eth_getenv_enetaddr_by_index("eth", dev->index,
+ dev->enetaddr)) {
/* Generate Private MAC addr if not set */
dev->enetaddr[0] = 0x02;
dev->enetaddr[1] = 0x50;
@@ -734,17 +740,10 @@ 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", dev->index,
+ dev->enetaddr);
}

- dev->init = (void *)mvgbe_init;
- dev->halt = (void *)mvgbe_halt;
- dev->send = (void *)mvgbe_send;
- dev->recv = (void *)mvgbe_recv;
- dev->write_hwaddr = (void *)mvgbe_write_hwaddr;
-
- eth_register(dev);
-
#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 7f9b1d1..b841f85 100644
--- a/include/net.h
+++ b/include/net.h
@@ -117,6 +117,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 b4b9b43..baa6ded 100644
--- a/net/eth.c
+++ b/net/eth.c
@@ -54,14 +54,29 @@ int eth_setenv_enetaddr(char *name, const uchar *enetaddr)
return setenv(name, buf);
}

+static void eth_get_enetaddr_env_name(char *buf, const char *base_name,
+ int index)
+{
+ sprintf(buf, index ? "%s%daddr" : "%saddr", base_name, index);
+ return buf;
+}
+
int eth_getenv_enetaddr_by_index(const char *base_name, int index,
uchar *enetaddr)
{
char enetvar[32];
- sprintf(enetvar, index ? "%s%daddr" : "%saddr", base_name, index);
+ eth_get_enetaddr_env_name(enetvar, base_name, 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];
+ eth_get_enetaddr_env_name(enetvar, base_name, index);
+ return eth_setenv_enetaddr(enetvar, enetaddr);
+}
+
static int eth_mac_skip(int index)
{
char enetvar[15];
--
1.7.2.5