of_net.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/nvmem-consumer.h>
  11. #include <linux/of_net.h>
  12. #include <linux/phy.h>
  13. #include <linux/export.h>
  14. /**
  15. * of_get_phy_mode - Get phy mode for given device_node
  16. * @np: Pointer to the given device_node
  17. *
  18. * The function gets phy interface string from property 'phy-mode' or
  19. * 'phy-connection-type', and return its index in phy_modes table, or errno in
  20. * error case.
  21. */
  22. int of_get_phy_mode(struct device_node *np)
  23. {
  24. const char *pm;
  25. int err, i;
  26. err = of_property_read_string(np, "phy-mode", &pm);
  27. if (err < 0)
  28. err = of_property_read_string(np, "phy-connection-type", &pm);
  29. if (err < 0)
  30. return err;
  31. for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
  32. if (!strcasecmp(pm, phy_modes(i)))
  33. return i;
  34. return -ENODEV;
  35. }
  36. EXPORT_SYMBOL_GPL(of_get_phy_mode);
  37. static const void *of_get_mac_addr(struct device_node *np, const char *name)
  38. {
  39. struct property *pp = of_find_property(np, name, NULL);
  40. if (pp && pp->length == ETH_ALEN && is_valid_ether_addr(pp->value))
  41. return pp->value;
  42. return NULL;
  43. }
  44. /**
  45. * Search the device tree for the best MAC address to use. 'mac-address' is
  46. * checked first, because that is supposed to contain to "most recent" MAC
  47. * address. If that isn't set, then 'local-mac-address' is checked next,
  48. * because that is the default address. If that isn't set, then the obsolete
  49. * 'address' is checked, just in case we're using an old device tree.
  50. *
  51. * Note that the 'address' property is supposed to contain a virtual address of
  52. * the register set, but some DTS files have redefined that property to be the
  53. * MAC address.
  54. *
  55. * All-zero MAC addresses are rejected, because those could be properties that
  56. * exist in the device tree, but were not set by U-Boot. For example, the
  57. * DTS could define 'mac-address' and 'local-mac-address', with zero MAC
  58. * addresses. Some older U-Boots only initialized 'local-mac-address'. In
  59. * this case, the real MAC is in 'local-mac-address', and 'mac-address' exists
  60. * but is all zeros.
  61. */
  62. const void *of_get_mac_address(struct device_node *np)
  63. {
  64. const void *addr;
  65. addr = of_get_mac_addr(np, "mac-address");
  66. if (addr)
  67. return addr;
  68. addr = of_get_mac_addr(np, "local-mac-address");
  69. if (addr)
  70. return addr;
  71. return of_get_mac_addr(np, "address");
  72. }
  73. EXPORT_SYMBOL(of_get_mac_address);
  74. /**
  75. * Obtain the MAC address from an nvmem provider named 'mac-address' through
  76. * device tree.
  77. * On success, copies the new address into memory pointed to by addr and
  78. * returns 0. Returns a negative error code otherwise.
  79. * @np: Device tree node containing the nvmem-cells phandle
  80. * @addr: Pointer to receive the MAC address using ether_addr_copy()
  81. */
  82. int of_get_nvmem_mac_address(struct device_node *np, void *addr)
  83. {
  84. struct nvmem_cell *cell;
  85. const void *mac;
  86. size_t len;
  87. int ret;
  88. cell = of_nvmem_cell_get(np, "mac-address");
  89. if (IS_ERR(cell))
  90. return PTR_ERR(cell);
  91. mac = nvmem_cell_read(cell, &len);
  92. nvmem_cell_put(cell);
  93. if (IS_ERR(mac))
  94. return PTR_ERR(mac);
  95. if (len < ETH_ALEN || !is_valid_ether_addr(mac)) {
  96. ret = -EINVAL;
  97. } else {
  98. ether_addr_copy(addr, mac);
  99. ret = 0;
  100. }
  101. kfree(mac);
  102. return ret;
  103. }
  104. EXPORT_SYMBOL(of_get_nvmem_mac_address);