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