]> Git Repo - J-u-boot.git/blobdiff - net/eth.c
net: Refactor in preparation for driver model
[J-u-boot.git] / net / eth.c
index c96e767e8e22b8beef592deaee91cca983ef64e6..84919e0a76202392d1e070de3bd6dec11b0b8333 100644 (file)
--- a/net/eth.c
+++ b/net/eth.c
@@ -10,6 +10,7 @@
 #include <net.h>
 #include <miiphy.h>
 #include <phy.h>
+#include <asm/errno.h>
 
 void eth_parse_enetaddr(const char *addr, uchar *enetaddr)
 {
@@ -54,6 +55,14 @@ static inline int eth_setenv_enetaddr_by_index(const char *base_name, int index,
        return eth_setenv_enetaddr(enetvar, enetaddr);
 }
 
+static void eth_env_init(void)
+{
+       const char *s;
+
+       s = getenv("bootfile");
+       if (s != NULL)
+               copy_filename(BootFile, s, sizeof(BootFile));
+}
 
 static int eth_mac_skip(int index)
 {
@@ -63,27 +72,7 @@ static int eth_mac_skip(int index)
        return ((skip_state = getenv(enetvar)) != NULL);
 }
 
-#ifdef CONFIG_RANDOM_MACADDR
-void eth_random_enetaddr(uchar *enetaddr)
-{
-       uint32_t rval;
-
-       srand(get_timer(0));
-
-       rval = rand();
-       enetaddr[0] = rval & 0xff;
-       enetaddr[1] = (rval >> 8) & 0xff;
-       enetaddr[2] = (rval >> 16) & 0xff;
-
-       rval = rand();
-       enetaddr[3] = rval & 0xff;
-       enetaddr[4] = (rval >> 8) & 0xff;
-       enetaddr[5] = (rval >> 16) & 0xff;
-
-       /* make sure it's local and unicast */
-       enetaddr[0] = (enetaddr[0] | 0x02) & ~0x01;
-}
-#endif
+static void eth_current_changed(void);
 
 /*
  * CPU and board-specific Ethernet initializations.  Aliased function
@@ -108,6 +97,11 @@ static unsigned int eth_rcv_current, eth_rcv_last;
 static struct eth_device *eth_devices;
 struct eth_device *eth_current;
 
+static void eth_set_current_to_next(void)
+{
+       eth_current = eth_current->next;
+}
+
 struct eth_device *eth_get_dev_by_name(const char *devname)
 {
        struct eth_device *dev, *target_dev;
@@ -158,22 +152,6 @@ int eth_get_dev_index(void)
        return eth_current->index;
 }
 
-static void eth_current_changed(void)
-{
-       char *act = getenv("ethact");
-       /* update current ethernet name */
-       if (eth_current) {
-               if (act == NULL || strcmp(act, eth_current->name) != 0)
-                       setenv("ethact", eth_current->name);
-       }
-       /*
-        * remove the variable completely if there is no active
-        * interface
-        */
-       else if (act != NULL)
-               setenv("ethact", NULL);
-}
-
 int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
                   int eth_number)
 {
@@ -182,9 +160,9 @@ int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
 
        eth_getenv_enetaddr_by_index(base_name, eth_number, env_enetaddr);
 
-       if (memcmp(env_enetaddr, "\0\0\0\0\0\0", 6)) {
-               if (memcmp(dev->enetaddr, "\0\0\0\0\0\0", 6) &&
-                               memcmp(dev->enetaddr, env_enetaddr, 6)) {
+       if (!is_zero_ether_addr(env_enetaddr)) {
+               if (!is_zero_ether_addr(dev->enetaddr) &&
+                   memcmp(dev->enetaddr, env_enetaddr, 6)) {
                        printf("\nWarning: %s MAC addresses don't match:\n",
                                dev->name);
                        printf("Address in SROM is         %pM\n",
@@ -199,14 +177,22 @@ int eth_write_hwaddr(struct eth_device *dev, const char *base_name,
                                             dev->enetaddr);
                printf("\nWarning: %s using MAC address from net device\n",
                        dev->name);
+       } else if (is_zero_ether_addr(dev->enetaddr)) {
+               printf("\nError: %s address not set.\n",
+                      dev->name);
+               return -EINVAL;
        }
 
-       if (dev->write_hwaddr &&
-                       !eth_mac_skip(eth_number)) {
-               if (!is_valid_ether_addr(dev->enetaddr))
-                       return -1;
+       if (dev->write_hwaddr && !eth_mac_skip(eth_number)) {
+               if (!is_valid_ether_addr(dev->enetaddr)) {
+                       printf("\nError: %s address %pM illegal value\n",
+                                dev->name, dev->enetaddr);
+                       return -EINVAL;
+               }
 
                ret = dev->write_hwaddr(dev);
+               if (ret)
+                       printf("\nWarning: %s failed to set MAC address\n", dev->name);
        }
 
        return ret;
@@ -264,14 +250,6 @@ int eth_unregister(struct eth_device *dev)
        return 0;
 }
 
-static void eth_env_init(bd_t *bis)
-{
-       const char *s;
-
-       if ((s = getenv("bootfile")) != NULL)
-               copy_filename(BootFile, s, sizeof(BootFile));
-}
-
 int eth_initialize(bd_t *bis)
 {
        int num_devices = 0;
@@ -279,7 +257,7 @@ int eth_initialize(bd_t *bis)
        eth_current = NULL;
 
        bootstage_mark(BOOTSTAGE_ID_NET_ETH_START);
-#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
+#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) || defined(CONFIG_PHYLIB)
        miiphy_init();
 #endif
 
@@ -287,7 +265,7 @@ int eth_initialize(bd_t *bis)
        phy_init();
 #endif
 
-       eth_env_init(bis);
+       eth_env_init();
 
        /*
         * If board-specific initialization exists, call it.
@@ -325,8 +303,7 @@ int eth_initialize(bd_t *bis)
                                puts("\nWarning: eth device name has a space!"
                                        "\n");
 
-                       if (eth_write_hwaddr(dev, "eth", dev->index))
-                               puts("\nWarning: failed to set MAC address\n");
+                       eth_write_hwaddr(dev, "eth", dev->index);
 
                        dev = dev->next;
                        num_devices++;
@@ -493,6 +470,22 @@ int eth_receive(void *packet, int length)
 }
 #endif /* CONFIG_API */
 
+static void eth_current_changed(void)
+{
+       char *act = getenv("ethact");
+       /* update current ethernet name */
+       if (eth_get_dev()) {
+               if (act == NULL || strcmp(act, eth_get_name()) != 0)
+                       setenv("ethact", eth_get_name());
+       }
+       /*
+        * remove the variable completely if there is no active
+        * interface
+        */
+       else if (act != NULL)
+               setenv("ethact", NULL);
+}
+
 void eth_try_another(int first_restart)
 {
        static struct eth_device *first_failed;
@@ -506,17 +499,17 @@ void eth_try_another(int first_restart)
        if ((ethrotate != NULL) && (strcmp(ethrotate, "no") == 0))
                return;
 
-       if (!eth_current)
+       if (!eth_get_dev())
                return;
 
        if (first_restart)
-               first_failed = eth_current;
+               first_failed = eth_get_dev();
 
-       eth_current = eth_current->next;
+       eth_set_current_to_next();
 
        eth_current_changed();
 
-       if (first_failed == eth_current)
+       if (first_failed == eth_get_dev())
                NetRestartWrap = 1;
 }
 
@@ -527,7 +520,7 @@ void eth_set_current(void)
        struct eth_device *old_current;
        int     env_id;
 
-       if (!eth_current)       /* XXX no current */
+       if (!eth_get_dev())     /* XXX no current */
                return;
 
        env_id = get_env_id();
@@ -536,18 +529,18 @@ void eth_set_current(void)
                env_changed_id = env_id;
        }
        if (act != NULL) {
-               old_current = eth_current;
+               old_current = eth_get_dev();
                do {
-                       if (strcmp(eth_current->name, act) == 0)
+                       if (strcmp(eth_get_name(), act) == 0)
                                return;
-                       eth_current = eth_current->next;
-               } while (old_current != eth_current);
+                       eth_set_current_to_next();
+               } while (old_current != eth_get_dev());
        }
 
        eth_current_changed();
 }
 
-char *eth_get_name(void)
+const char *eth_get_name(void)
 {
-       return eth_current ? eth_current->name : "unknown";
+       return eth_get_dev() ? eth_get_dev()->name : "unknown";
 }
This page took 0.033226 seconds and 4 git commands to generate.