kernel/3.18: update to version 3.18.25
[openwrt/openwrt.git] / target / linux / generic / patches-4.3 / 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@openwrt.org>
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 @@ -47,6 +48,66 @@ static const void *of_get_mac_addr(struc
28 return NULL;
29 }
30
31 +static const void *of_get_mac_address_mtd(struct device_node *np)
32 +{
33 +#ifdef CONFIG_MTD
34 + struct device_node *mtd_np = NULL;
35 + struct property *prop;
36 + size_t retlen;
37 + int size, ret;
38 + struct mtd_info *mtd;
39 + const char *part;
40 + const __be32 *list;
41 + phandle phandle;
42 + u32 mac_inc = 0;
43 + u8 mac[ETH_ALEN];
44 +
45 + list = of_get_property(np, "mtd-mac-address", &size);
46 + if (!list || (size != (2 * sizeof(*list))))
47 + return NULL;
48 +
49 + phandle = be32_to_cpup(list++);
50 + if (phandle)
51 + mtd_np = of_find_node_by_phandle(phandle);
52 +
53 + if (!mtd_np)
54 + return NULL;
55 +
56 + part = of_get_property(mtd_np, "label", NULL);
57 + if (!part)
58 + part = mtd_np->name;
59 +
60 + mtd = get_mtd_device_nm(part);
61 + if (IS_ERR(mtd))
62 + return NULL;
63 +
64 + ret = mtd_read(mtd, be32_to_cpup(list), 6, &retlen, mac);
65 + put_mtd_device(mtd);
66 +
67 + if (!of_property_read_u32(np, "mtd-mac-address-increment", &mac_inc))
68 + mac[5] += mac_inc;
69 +
70 + if (!is_valid_ether_addr(mac))
71 + return NULL;
72 +
73 + prop = kzalloc(sizeof(*prop), GFP_KERNEL);
74 + if (!prop)
75 + return NULL;
76 +
77 + prop->name = "mac-address";
78 + prop->length = ETH_ALEN;
79 + prop->value = kmemdup(mac, ETH_ALEN, GFP_KERNEL);
80 + if (!prop->value || of_add_property(np, prop))
81 + goto free;
82 +
83 + return prop->value;
84 +free:
85 + kfree(prop->value);
86 + kfree(prop);
87 +#endif
88 + return NULL;
89 +}
90 +
91 /**
92 * Search the device tree for the best MAC address to use. 'mac-address' is
93 * checked first, because that is supposed to contain to "most recent" MAC
94 @@ -64,6 +125,9 @@ static const void *of_get_mac_addr(struc
95 * addresses. Some older U-Boots only initialized 'local-mac-address'. In
96 * this case, the real MAC is in 'local-mac-address', and 'mac-address' exists
97 * but is all zeros.
98 + *
99 + * If a mtd-mac-address property exists, try to fetch the MAC address from the
100 + * specified mtd device, and store it as a 'mac-address' property
101 */
102 const void *of_get_mac_address(struct device_node *np)
103 {
104 @@ -77,6 +141,10 @@ const void *of_get_mac_address(struct de
105 if (addr)
106 return addr;
107
108 - return of_get_mac_addr(np, "address");
109 + addr = of_get_mac_addr(np, "address");
110 + if (addr)
111 + return addr;
112 +
113 + return of_get_mac_address_mtd(np);
114 }
115 EXPORT_SYMBOL(of_get_mac_address);