681-NET-add-mtd-mac-address-support-to-of_get_mac_addres.patch 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. From 6f8e5369ae054ec6c9265581d5a7e39738a5cd84 Mon Sep 17 00:00:00 2001
  2. From: Ansuel Smith <[email protected]>
  3. Date: Tue, 30 Mar 2021 13:16:38 +0200
  4. Subject: [PATCH 1/2] NET: add mtd-mac-address support to of_get_mac_address()
  5. Many embedded devices have information such as mac addresses stored inside mtd
  6. devices. This patch allows us to add a property inside a node describing a
  7. network interface. The new property points at a mtd partition with an offset
  8. where the mac address can be found.
  9. Signed-off-by: John Crispin <[email protected]>
  10. Signed-off-by: Felix Fietkau <[email protected]>
  11. Signed-off-by: Ansuel Smith <[email protected]>
  12. ---
  13. drivers/of/of_net.c | 75 ++++++++++++++++++++++++++++++++++++++++++++-
  14. 1 file changed, 74 insertions(+), 1 deletion(-)
  15. --- a/drivers/of/of_net.c
  16. +++ b/drivers/of/of_net.c
  17. @@ -12,6 +12,7 @@
  18. #include <linux/export.h>
  19. #include <linux/device.h>
  20. #include <linux/nvmem-consumer.h>
  21. +#include <linux/mtd/mtd.h>
  22. /**
  23. * of_get_phy_mode - Get phy mode for given device_node
  24. @@ -89,6 +90,52 @@ static int of_get_mac_addr_nvmem(struct
  25. return 0;
  26. }
  27. +static int of_get_mac_address_mtd(struct device_node *np, u8 *addr)
  28. +{
  29. +#ifdef CONFIG_MTD
  30. + struct platform_device *pdev = of_find_device_by_node(np);
  31. + struct device_node *mtd_np = NULL;
  32. + size_t retlen;
  33. + int size, ret;
  34. + struct mtd_info *mtd;
  35. + const char *part;
  36. + const __be32 *list;
  37. + phandle phandle;
  38. + u8 mac[ETH_ALEN];
  39. +
  40. + list = of_get_property(np, "mtd-mac-address", &size);
  41. + if (!list || (size != (2 * sizeof(*list))))
  42. + return -ENODEV;
  43. +
  44. + phandle = be32_to_cpup(list++);
  45. + if (phandle)
  46. + mtd_np = of_find_node_by_phandle(phandle);
  47. +
  48. + if (!mtd_np)
  49. + return -ENODEV;
  50. +
  51. + part = of_get_property(mtd_np, "label", NULL);
  52. + if (!part)
  53. + part = mtd_np->name;
  54. +
  55. + mtd = get_mtd_device_nm(part);
  56. + if (IS_ERR(mtd))
  57. + return -ENODEV;
  58. +
  59. + ret = mtd_read(mtd, be32_to_cpup(list), 6, &retlen, mac);
  60. + put_mtd_device(mtd);
  61. +
  62. + if (!is_valid_ether_addr(mac))
  63. + return -EINVAL;
  64. +
  65. + memcpy(addr, mac, ETH_ALEN);
  66. +
  67. + return 0;
  68. +#endif
  69. + return -EINVAL;
  70. +}
  71. +
  72. +
  73. /**
  74. * Search the device tree for the best MAC address to use. 'mac-address' is
  75. * checked first, because that is supposed to contain to "most recent" MAC
  76. @@ -109,6 +156,10 @@ static int of_get_mac_addr_nvmem(struct
  77. * this case, the real MAC is in 'local-mac-address', and 'mac-address' exists
  78. * but is all zeros.
  79. *
  80. + *
  81. + * If a mtd-mac-address property exists, try to fetch the MAC address from the
  82. + * specified mtd device.
  83. + *
  84. * Return: 0 on success and errno in case of error.
  85. */
  86. int of_get_mac_address(struct device_node *np, u8 *addr)
  87. @@ -130,6 +181,10 @@ int of_get_mac_address(struct device_nod
  88. if (!ret)
  89. return 0;
  90. + ret = of_get_mac_address_mtd(np, addr);
  91. + if (!ret)
  92. + return 0;
  93. +
  94. return of_get_mac_addr_nvmem(np, addr);
  95. }
  96. EXPORT_SYMBOL(of_get_mac_address);