681-NET-add-of_get_mac_address_mtd.patch 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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,79 @@ 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. + u32 inc_idx;
  47. +
  48. + list = of_get_property(np, "mtd-mac-address", &size);
  49. + if (!list || (size != (2 * sizeof(*list))))
  50. + return NULL;
  51. +
  52. + phandle = be32_to_cpup(list++);
  53. + if (phandle)
  54. + mtd_np = of_find_node_by_phandle(phandle);
  55. +
  56. + if (!mtd_np)
  57. + return NULL;
  58. +
  59. + part = of_get_property(mtd_np, "label", NULL);
  60. + if (!part)
  61. + part = mtd_np->name;
  62. +
  63. + mtd = get_mtd_device_nm(part);
  64. + if (IS_ERR(mtd))
  65. + return NULL;
  66. +
  67. + ret = mtd_read(mtd, be32_to_cpup(list), 6, &retlen, mac);
  68. + put_mtd_device(mtd);
  69. +
  70. + if (of_property_read_u32(np, "mtd-mac-address-increment-byte", &inc_idx))
  71. + inc_idx = 5;
  72. + if (inc_idx > 5)
  73. + return NULL;
  74. +
  75. + if (!of_property_read_u32(np, "mtd-mac-address-increment", &mac_inc))
  76. + mac[inc_idx] += mac_inc;
  77. +
  78. + if (!is_valid_ether_addr(mac))
  79. + return NULL;
  80. +
  81. + addr = of_get_mac_addr(np, "mac-address");
  82. + if (addr) {
  83. + memcpy(addr, mac, ETH_ALEN);
  84. + return addr;
  85. + }
  86. +
  87. + prop = kzalloc(sizeof(*prop), GFP_KERNEL);
  88. + if (!prop)
  89. + return NULL;
  90. +
  91. + prop->name = "mac-address";
  92. + prop->length = ETH_ALEN;
  93. + prop->value = kmemdup(mac, ETH_ALEN, GFP_KERNEL);
  94. + if (!prop->value || of_add_property(np, prop))
  95. + goto free;
  96. +
  97. + return prop->value;
  98. +free:
  99. + kfree(prop->value);
  100. + kfree(prop);
  101. +#endif
  102. + return NULL;
  103. +}
  104. +
  105. /**
  106. * Search the device tree for the best MAC address to use. 'mac-address' is
  107. * checked first, because that is supposed to contain to "most recent" MAC
  108. @@ -64,11 +138,18 @@ static const void *of_get_mac_addr(struc
  109. * addresses. Some older U-Boots only initialized 'local-mac-address'. In
  110. * this case, the real MAC is in 'local-mac-address', and 'mac-address' exists
  111. * but is all zeros.
  112. + *
  113. + * If a mtd-mac-address property exists, try to fetch the MAC address from the
  114. + * specified mtd device, and store it as a 'mac-address' property
  115. */
  116. const void *of_get_mac_address(struct device_node *np)
  117. {
  118. const void *addr;
  119. + addr = of_get_mac_address_mtd(np);
  120. + if (addr)
  121. + return addr;
  122. +
  123. addr = of_get_mac_addr(np, "mac-address");
  124. if (addr)
  125. return addr;