of_net.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. * OF helpers for network devices.
  3. *
  4. * This file is released under the GPLv2
  5. *
  6. * Initially copied out of arch/powerpc/kernel/prom_parse.c
  7. */
  8. #include <linux/etherdevice.h>
  9. #include <linux/kernel.h>
  10. #include <linux/of_net.h>
  11. /**
  12. * Search the device tree for the best MAC address to use. 'mac-address' is
  13. * checked first, because that is supposed to contain to "most recent" MAC
  14. * address. If that isn't set, then 'local-mac-address' is checked next,
  15. * because that is the default address. If that isn't set, then the obsolete
  16. * 'address' is checked, just in case we're using an old device tree.
  17. *
  18. * Note that the 'address' property is supposed to contain a virtual address of
  19. * the register set, but some DTS files have redefined that property to be the
  20. * MAC address.
  21. *
  22. * All-zero MAC addresses are rejected, because those could be properties that
  23. * exist in the device tree, but were not set by U-Boot. For example, the
  24. * DTS could define 'mac-address' and 'local-mac-address', with zero MAC
  25. * addresses. Some older U-Boots only initialized 'local-mac-address'. In
  26. * this case, the real MAC is in 'local-mac-address', and 'mac-address' exists
  27. * but is all zeros.
  28. */
  29. const void *of_get_mac_address(struct device_node *np)
  30. {
  31. struct property *pp;
  32. pp = of_find_property(np, "mac-address", NULL);
  33. if (pp && (pp->length == 6) && is_valid_ether_addr(pp->value))
  34. return pp->value;
  35. pp = of_find_property(np, "local-mac-address", NULL);
  36. if (pp && (pp->length == 6) && is_valid_ether_addr(pp->value))
  37. return pp->value;
  38. pp = of_find_property(np, "address", NULL);
  39. if (pp && (pp->length == 6) && is_valid_ether_addr(pp->value))
  40. return pp->value;
  41. return NULL;
  42. }
  43. EXPORT_SYMBOL(of_get_mac_address);