of_net.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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/phy.h>
  12. #include <linux/export.h>
  13. #include <linux/device.h>
  14. /**
  15. * of_get_phy_mode - Get phy mode for given device_node
  16. * @np: Pointer to the given device_node
  17. * @interface: Pointer to the result
  18. *
  19. * The function gets phy interface string from property 'phy-mode' or
  20. * 'phy-connection-type'. The index in phy_modes table is set in
  21. * interface and 0 returned. In case of error interface is set to
  22. * PHY_INTERFACE_MODE_NA and an errno is returned, e.g. -ENODEV.
  23. */
  24. int of_get_phy_mode(struct device_node *np, phy_interface_t *interface)
  25. {
  26. const char *pm;
  27. int err, i;
  28. *interface = PHY_INTERFACE_MODE_NA;
  29. err = of_property_read_string(np, "phy-mode", &pm);
  30. if (err < 0)
  31. err = of_property_read_string(np, "phy-connection-type", &pm);
  32. if (err < 0)
  33. return err;
  34. for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++)
  35. if (!strcasecmp(pm, phy_modes(i))) {
  36. *interface = i;
  37. return 0;
  38. }
  39. return -ENODEV;
  40. }
  41. EXPORT_SYMBOL_GPL(of_get_phy_mode);
  42. static const void *of_get_mac_addr(struct device_node *np, const char *name)
  43. {
  44. struct property *pp = of_find_property(np, name, NULL);
  45. if (pp && pp->length == ETH_ALEN && is_valid_ether_addr(pp->value))
  46. return pp->value;
  47. return NULL;
  48. }
  49. static const void *of_get_mac_addr_nvmem(struct device_node *np)
  50. {
  51. int ret;
  52. const void *mac;
  53. u8 nvmem_mac[ETH_ALEN];
  54. struct platform_device *pdev = of_find_device_by_node(np);
  55. if (!pdev)
  56. return ERR_PTR(-ENODEV);
  57. ret = nvmem_get_mac_address(&pdev->dev, &nvmem_mac);
  58. if (ret) {
  59. put_device(&pdev->dev);
  60. return ERR_PTR(ret);
  61. }
  62. mac = devm_kmemdup(&pdev->dev, nvmem_mac, ETH_ALEN, GFP_KERNEL);
  63. put_device(&pdev->dev);
  64. if (!mac)
  65. return ERR_PTR(-ENOMEM);
  66. return mac;
  67. }
  68. /**
  69. * Search the device tree for the best MAC address to use. 'mac-address' is
  70. * checked first, because that is supposed to contain to "most recent" MAC
  71. * address. If that isn't set, then 'local-mac-address' is checked next,
  72. * because that is the default address. If that isn't set, then the obsolete
  73. * 'address' is checked, just in case we're using an old device tree. If any
  74. * of the above isn't set, then try to get MAC address from nvmem cell named
  75. * 'mac-address'.
  76. *
  77. * Note that the 'address' property is supposed to contain a virtual address of
  78. * the register set, but some DTS files have redefined that property to be the
  79. * MAC address.
  80. *
  81. * All-zero MAC addresses are rejected, because those could be properties that
  82. * exist in the device tree, but were not set by U-Boot. For example, the
  83. * DTS could define 'mac-address' and 'local-mac-address', with zero MAC
  84. * addresses. Some older U-Boots only initialized 'local-mac-address'. In
  85. * this case, the real MAC is in 'local-mac-address', and 'mac-address' exists
  86. * but is all zeros.
  87. *
  88. * Return: Will be a valid pointer on success and ERR_PTR in case of error.
  89. */
  90. const void *of_get_mac_address(struct device_node *np)
  91. {
  92. const void *addr;
  93. addr = of_get_mac_addr(np, "mac-address");
  94. if (addr)
  95. return addr;
  96. addr = of_get_mac_addr(np, "local-mac-address");
  97. if (addr)
  98. return addr;
  99. addr = of_get_mac_addr(np, "address");
  100. if (addr)
  101. return addr;
  102. addr = of_get_mac_addr(np, "nvmem-mac-address");
  103. if (addr)
  104. return addr;
  105. return of_get_mac_addr_nvmem(np);
  106. }
  107. EXPORT_SYMBOL(of_get_mac_address);