681-NET-add-of_get_mac_address_mtd.patch 3.4 KB

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