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