681-NET-add-of_get_mac_address_mtd.patch 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. From: John Crispin <blogic@openwrt.org>
  2. Subject: NET: add mtd-mac-address support to of_get_mac_address()
  3. Many embedded devices have information such as mac addresses stored inside mtd
  4. devices. This patch allows us to add a property inside a node describing a
  5. network interface. The new property points at a mtd partition with an offset
  6. where the mac address can be found.
  7. Signed-off-by: John Crispin <blogic@openwrt.org>
  8. Signed-off-by: Felix Fietkau <nbd@nbd.name>
  9. ---
  10. drivers/of/of_net.c | 37 +++++++++++++++++++++++++++++++++++++
  11. include/linux/of_net.h | 1 +
  12. 2 files changed, 38 insertions(+)
  13. --- a/drivers/of/of_net.c
  14. +++ b/drivers/of/of_net.c
  15. @@ -10,6 +10,7 @@
  16. #include <linux/of_net.h>
  17. #include <linux/phy.h>
  18. #include <linux/export.h>
  19. +#include <linux/mtd/mtd.h>
  20. /**
  21. * of_get_phy_mode - Get phy mode for given device_node
  22. @@ -38,7 +39,7 @@ int of_get_phy_mode(struct device_node *
  23. }
  24. EXPORT_SYMBOL_GPL(of_get_phy_mode);
  25. -static const void *of_get_mac_addr(struct device_node *np, const char *name)
  26. +static void *of_get_mac_addr(struct device_node *np, const char *name)
  27. {
  28. struct property *pp = of_find_property(np, name, NULL);
  29. @@ -47,6 +48,73 @@ static const void *of_get_mac_addr(struc
  30. return NULL;
  31. }
  32. +static const void *of_get_mac_address_mtd(struct device_node *np)
  33. +{
  34. +#ifdef CONFIG_MTD
  35. + struct device_node *mtd_np = NULL;
  36. + struct property *prop;
  37. + size_t retlen;
  38. + int size, ret;
  39. + struct mtd_info *mtd;
  40. + const char *part;
  41. + const __be32 *list;
  42. + phandle phandle;
  43. + u32 mac_inc = 0;
  44. + u8 mac[ETH_ALEN];
  45. + void *addr;
  46. +
  47. + list = of_get_property(np, "mtd-mac-address", &size);
  48. + if (!list || (size != (2 * sizeof(*list))))
  49. + return NULL;
  50. +
  51. + phandle = be32_to_cpup(list++);
  52. + if (phandle)
  53. + mtd_np = of_find_node_by_phandle(phandle);
  54. +
  55. + if (!mtd_np)
  56. + return NULL;
  57. +
  58. + part = of_get_property(mtd_np, "label", NULL);
  59. + if (!part)
  60. + part = mtd_np->name;
  61. +
  62. + mtd = get_mtd_device_nm(part);
  63. + if (IS_ERR(mtd))
  64. + return NULL;
  65. +
  66. + ret = mtd_read(mtd, be32_to_cpup(list), 6, &retlen, mac);
  67. + put_mtd_device(mtd);
  68. +
  69. + if (!of_property_read_u32(np, "mtd-mac-address-increment", &mac_inc))
  70. + mac[5] += mac_inc;
  71. +
  72. + if (!is_valid_ether_addr(mac))
  73. + return NULL;
  74. +
  75. + addr = of_get_mac_addr(np, "mac-address");
  76. + if (addr) {
  77. + memcpy(addr, mac, ETH_ALEN);
  78. + return addr;
  79. + }
  80. +
  81. + prop = kzalloc(sizeof(*prop), GFP_KERNEL);
  82. + if (!prop)
  83. + return NULL;
  84. +
  85. + prop->name = "mac-address";
  86. + prop->length = ETH_ALEN;
  87. + prop->value = kmemdup(mac, ETH_ALEN, GFP_KERNEL);
  88. + if (!prop->value || of_add_property(np, prop))
  89. + goto free;
  90. +
  91. + return prop->value;
  92. +free:
  93. + kfree(prop->value);
  94. + kfree(prop);
  95. +#endif
  96. + return NULL;
  97. +}
  98. +
  99. /**
  100. * Search the device tree for the best MAC address to use. 'mac-address' is
  101. * checked first, because that is supposed to contain to "most recent" MAC
  102. @@ -64,11 +132,18 @@ static const void *of_get_mac_addr(struc
  103. * addresses. Some older U-Boots only initialized 'local-mac-address'. In
  104. * this case, the real MAC is in 'local-mac-address', and 'mac-address' exists
  105. * but is all zeros.
  106. + *
  107. + * If a mtd-mac-address property exists, try to fetch the MAC address from the
  108. + * specified mtd device, and store it as a 'mac-address' property
  109. */
  110. const void *of_get_mac_address(struct device_node *np)
  111. {
  112. const void *addr;
  113. + addr = of_get_mac_address_mtd(np);
  114. + if (addr)
  115. + return addr;
  116. +
  117. addr = of_get_mac_addr(np, "mac-address");
  118. if (addr)
  119. return addr;