of_net.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * OF helpers for network devices.
  4. *
  5. * Initially copied out of arch/powerpc/kernel/prom_parse.c
  6. */
  7. #include <linux/etherdevice.h>
  8. #include <linux/kernel.h>
  9. #include <linux/of_net.h>
  10. #include <linux/of_platform.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/phy.h>
  13. #include <linux/export.h>
  14. #include <linux/device.h>
  15. #include <linux/nvmem-consumer.h>
  16. /**
  17. * of_get_phy_mode - Get phy mode for given device_node
  18. * @np: Pointer to the given device_node
  19. * @interface: Pointer to the result
  20. *
  21. * The function gets phy interface string from property 'phy-mode' or
  22. * 'phy-connection-type'. The index in phy_modes table is set in
  23. * interface and 0 returned. In case of error interface is set to
  24. * PHY_INTERFACE_MODE_NA and an errno is returned, e.g. -ENODEV.
  25. */
  26. int of_get_phy_mode(struct device_node *np, phy_interface_t *interface)
  27. {
  28. const char *pm;
  29. int err, i;
  30. *interface = PHY_INTERFACE_MODE_NA;
  31. err = of_property_read_string(np, "phy-mode", &pm);
  32. if (err < 0)
  33. err = of_property_read_string(np, "phy-connection-type", &pm);
  34. if (err < 0)
  35. return err;
  36. for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
  37. if (!strcasecmp(pm, phy_modes(i))) {
  38. *interface = i;
  39. return 0;
  40. }
  41. return -ENODEV;
  42. }
  43. EXPORT_SYMBOL_GPL(of_get_phy_mode);
  44. static int of_get_mac_addr(struct device_node *np, const char *name, u8 *addr)
  45. {
  46. struct property *pp = of_find_property(np, name, NULL);
  47. if (pp && pp->length == ETH_ALEN && is_valid_ether_addr(pp->value)) {
  48. memcpy(addr, pp->value, ETH_ALEN);
  49. return 0;
  50. }
  51. return -ENODEV;
  52. }
  53. int of_get_mac_address_nvmem(struct device_node *np, u8 *addr)
  54. {
  55. struct platform_device *pdev = of_find_device_by_node(np);
  56. struct nvmem_cell *cell;
  57. const void *mac;
  58. size_t len;
  59. int ret;
  60. /* Try lookup by device first, there might be a nvmem_cell_lookup
  61. * associated with a given device.
  62. */
  63. if (pdev) {
  64. ret = nvmem_get_mac_address(&pdev->dev, addr);
  65. put_device(&pdev->dev);
  66. return ret;
  67. }
  68. cell = of_nvmem_cell_get(np, "mac-address");
  69. if (IS_ERR(cell))
  70. return PTR_ERR(cell);
  71. mac = nvmem_cell_read(cell, &len);
  72. nvmem_cell_put(cell);
  73. if (IS_ERR(mac))
  74. return PTR_ERR(mac);
  75. if (len != ETH_ALEN || !is_valid_ether_addr(mac)) {
  76. kfree(mac);
  77. return -EINVAL;
  78. }
  79. memcpy(addr, mac, ETH_ALEN);
  80. kfree(mac);
  81. return 0;
  82. }
  83. EXPORT_SYMBOL(of_get_mac_address_nvmem);
  84. /**
  85. * of_get_mac_address()
  86. * @np: Caller's Device Node
  87. * @addr: Pointer to a six-byte array for the result
  88. *
  89. * Search the device tree for the best MAC address to use. 'mac-address' is
  90. * checked first, because that is supposed to contain to "most recent" MAC
  91. * address. If that isn't set, then 'local-mac-address' is checked next,
  92. * because that is the default address. If that isn't set, then the obsolete
  93. * 'address' is checked, just in case we're using an old device tree. If any
  94. * of the above isn't set, then try to get MAC address from nvmem cell named
  95. * 'mac-address'.
  96. *
  97. * Note that the 'address' property is supposed to contain a virtual address of
  98. * the register set, but some DTS files have redefined that property to be the
  99. * MAC address.
  100. *
  101. * All-zero MAC addresses are rejected, because those could be properties that
  102. * exist in the device tree, but were not set by U-Boot. For example, the
  103. * DTS could define 'mac-address' and 'local-mac-address', with zero MAC
  104. * addresses. Some older U-Boots only initialized 'local-mac-address'. In
  105. * this case, the real MAC is in 'local-mac-address', and 'mac-address' exists
  106. * but is all zeros.
  107. *
  108. * Return: 0 on success and errno in case of error.
  109. */
  110. int of_get_mac_address(struct device_node *np, u8 *addr)
  111. {
  112. int ret;
  113. if (!np)
  114. return -ENODEV;
  115. ret = of_get_mac_addr(np, "mac-address", addr);
  116. if (!ret)
  117. return 0;
  118. ret = of_get_mac_addr(np, "local-mac-address", addr);
  119. if (!ret)
  120. return 0;
  121. ret = of_get_mac_addr(np, "address", addr);
  122. if (!ret)
  123. return 0;
  124. return of_get_mac_address_nvmem(np, addr);
  125. }
  126. EXPORT_SYMBOL(of_get_mac_address);
  127. /**
  128. * of_get_ethdev_address()
  129. * @np: Caller's Device Node
  130. * @dev: Pointer to netdevice which address will be updated
  131. *
  132. * Search the device tree for the best MAC address to use.
  133. * If found set @dev->dev_addr to that address.
  134. *
  135. * See documentation of of_get_mac_address() for more information on how
  136. * the best address is determined.
  137. *
  138. * Return: 0 on success and errno in case of error.
  139. */
  140. int of_get_ethdev_address(struct device_node *np, struct net_device *dev)
  141. {
  142. u8 addr[ETH_ALEN];
  143. int ret;
  144. ret = of_get_mac_address(np, addr);
  145. if (!ret)
  146. eth_hw_addr_set(dev, addr);
  147. return ret;
  148. }
  149. EXPORT_SYMBOL(of_get_ethdev_address);