Merge branch 'master' of git://git.denx.de/u-boot
[oweals/u-boot.git] / net / eth-uclass.c
index fa3f5497a2902577f2bbb84947d918836b7bcbab..8bf2eabe9026df405825ddf3b78aedf5d417f94b 100644 (file)
@@ -7,10 +7,11 @@
 
 #include <common.h>
 #include <dm.h>
-#include <environment.h>
+#include <env.h>
 #include <net.h>
 #include <dm/device-internal.h>
 #include <dm/uclass-internal.h>
+#include <net/pcap.h>
 #include "eth_internal.h"
 
 DECLARE_GLOBAL_DATA_PTR;
@@ -226,7 +227,7 @@ static int on_ethaddr(const char *name, const char *value, enum env_op op,
                switch (op) {
                case env_op_create:
                case env_op_overwrite:
-                       eth_parse_enetaddr(value, pdata->enetaddr);
+                       string_to_enetaddr(value, pdata->enetaddr);
                        eth_write_hwaddr(dev);
                        break;
                case env_op_delete:
@@ -312,7 +313,8 @@ void eth_halt(void)
 
        eth_get_ops(current)->stop(current);
        priv = current->uclass_priv;
-       priv->state = ETH_STATE_PASSIVE;
+       if (priv)
+               priv->state = ETH_STATE_PASSIVE;
 }
 
 int eth_is_active(struct udevice *dev)
@@ -343,6 +345,10 @@ int eth_send(void *packet, int length)
                /* We cannot completely return the error at present */
                debug("%s: send() returned error %d\n", __func__, ret);
        }
+#if defined(CONFIG_CMD_PCAP)
+       if (ret >= 0)
+               pcap_post(packet, length, true);
+#endif
        return ret;
 }
 
@@ -414,20 +420,25 @@ int eth_initialize(void)
 
                bootstage_mark(BOOTSTAGE_ID_NET_ETH_INIT);
                do {
-                       if (num_devices)
-                               printf(", ");
+                       if (dev->seq != -1) {
+                               if (num_devices)
+                                       printf(", ");
 
-                       printf("eth%d: %s", dev->seq, dev->name);
+                               printf("eth%d: %s", dev->seq, dev->name);
 
-                       if (ethprime && dev == prime_dev)
-                               printf(" [PRIME]");
+                               if (ethprime && dev == prime_dev)
+                                       printf(" [PRIME]");
+                       }
 
                        eth_write_hwaddr(dev);
 
+                       if (dev->seq != -1)
+                               num_devices++;
                        uclass_next_device_check(&dev);
-                       num_devices++;
                } while (dev);
 
+               if (!num_devices)
+                       printf("No ethernet found.\n");
                putc('\n');
        }
 
@@ -454,11 +465,32 @@ static int eth_pre_unbind(struct udevice *dev)
        return 0;
 }
 
+static bool eth_dev_get_mac_address(struct udevice *dev, u8 mac[ARP_HLEN])
+{
+#if IS_ENABLED(CONFIG_OF_CONTROL)
+       const uint8_t *p;
+
+       p = dev_read_u8_array_ptr(dev, "mac-address", ARP_HLEN);
+       if (!p)
+               p = dev_read_u8_array_ptr(dev, "local-mac-address", ARP_HLEN);
+
+       if (!p)
+               return false;
+
+       memcpy(mac, p, ARP_HLEN);
+
+       return true;
+#else
+       return false;
+#endif
+}
+
 static int eth_post_probe(struct udevice *dev)
 {
        struct eth_device_priv *priv = dev->uclass_priv;
        struct eth_pdata *pdata = dev->platdata;
        unsigned char env_enetaddr[ARP_HLEN];
+       char *source = "DT";
 
 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
        struct eth_ops *ops = eth_get_ops(dev);
@@ -475,10 +507,8 @@ static int eth_post_probe(struct udevice *dev)
                        ops->free_pkt += gd->reloc_off;
                if (ops->stop)
                        ops->stop += gd->reloc_off;
-#ifdef CONFIG_MCAST_TFTP
                if (ops->mcast)
                        ops->mcast += gd->reloc_off;
-#endif
                if (ops->write_hwaddr)
                        ops->write_hwaddr += gd->reloc_off;
                if (ops->read_rom_hwaddr)
@@ -490,9 +520,14 @@ static int eth_post_probe(struct udevice *dev)
 
        priv->state = ETH_STATE_INIT;
 
-       /* Check if the device has a MAC address in ROM */
-       if (eth_get_ops(dev)->read_rom_hwaddr)
-               eth_get_ops(dev)->read_rom_hwaddr(dev);
+       /* Check if the device has a valid MAC address in device tree */
+       if (!eth_dev_get_mac_address(dev, pdata->enetaddr) ||
+           !is_valid_ethaddr(pdata->enetaddr)) {
+               source = "ROM";
+               /* Check if the device has a MAC address in ROM */
+               if (eth_get_ops(dev)->read_rom_hwaddr)
+                       eth_get_ops(dev)->read_rom_hwaddr(dev);
+       }
 
        eth_env_get_enetaddr_by_index("eth", dev->seq, env_enetaddr);
        if (!is_zero_ethaddr(env_enetaddr)) {
@@ -500,9 +535,9 @@ static int eth_post_probe(struct udevice *dev)
                    memcmp(pdata->enetaddr, env_enetaddr, ARP_HLEN)) {
                        printf("\nWarning: %s MAC addresses don't match:\n",
                               dev->name);
-                       printf("Address in ROM is          %pM\n",
-                              pdata->enetaddr);
-                       printf("Address in environment is  %pM\n",
+                       printf("Address in %s is\t\t%pM\n",
+                              source, pdata->enetaddr);
+                       printf("Address in environment is\t%pM\n",
                               env_enetaddr);
                }
 
@@ -510,8 +545,8 @@ static int eth_post_probe(struct udevice *dev)
                memcpy(pdata->enetaddr, env_enetaddr, ARP_HLEN);
        } else if (is_valid_ethaddr(pdata->enetaddr)) {
                eth_env_set_enetaddr_by_index("eth", dev->seq, pdata->enetaddr);
-               printf("\nWarning: %s using MAC address from ROM\n",
-                      dev->name);
+               printf("\nWarning: %s using MAC address from %s\n",
+                      dev->name, source);
        } else if (is_zero_ethaddr(pdata->enetaddr) ||
                   !is_valid_ethaddr(pdata->enetaddr)) {
 #ifdef CONFIG_NET_RANDOM_ETHADDR
@@ -525,6 +560,8 @@ static int eth_post_probe(struct udevice *dev)
 #endif
        }
 
+       eth_write_hwaddr(dev);
+
        return 0;
 }