treewide: convert mtd-mac-address-increment* to generic implementation
[openwrt/staging/wigyori.git] / target / linux / generic / pending-5.4 / 681-NET-add-mtd-mac-address-support-to-of_get_mac_addres.patch
1 From 6f8e5369ae054ec6c9265581d5a7e39738a5cd84 Mon Sep 17 00:00:00 2001
2 From: Ansuel Smith <ansuelsmth@gmail.com>
3 Date: Tue, 30 Mar 2021 13:16:38 +0200
4 Subject: [PATCH 1/2] NET: add mtd-mac-address support to of_get_mac_address()
5
6 Many embedded devices have information such as mac addresses stored inside mtd
7 devices. This patch allows us to add a property inside a node describing a
8 network interface. The new property points at a mtd partition with an offset
9 where the mac address can be found.
10
11 Signed-off-by: John Crispin <blogic@openwrt.org>
12 Signed-off-by: Felix Fietkau <nbd@nbd.name>
13 Signed-off-by: Ansuel Smith <ansuelsmth@gmail.com>
14 ---
15 drivers/of/of_net.c | 75 ++++++++++++++++++++++++++++++++++++++++++++-
16 1 file changed, 74 insertions(+), 1 deletion(-)
17
18 diff --git a/drivers/of/of_net.c b/drivers/of/of_net.c
19 index 6e411821583e..f072e2509cc9 100644
20 --- a/drivers/of/of_net.c
21 +++ b/drivers/of/of_net.c
22 @@ -11,6 +11,7 @@
23 #include <linux/phy.h>
24 #include <linux/export.h>
25 #include <linux/device.h>
26 +#include <linux/mtd/mtd.h>
27
28 /**
29 * of_get_phy_mode - Get phy mode for given device_node
30 @@ -45,7 +46,7 @@ int of_get_phy_mode(struct device_node *np, phy_interface_t *interface)
31 }
32 EXPORT_SYMBOL_GPL(of_get_phy_mode);
33
34 -static const void *of_get_mac_addr(struct device_node *np, const char *name)
35 +static void *of_get_mac_addr(struct device_node *np, const char *name)
36 {
37 struct property *pp = of_find_property(np, name, NULL);
38
39 @@ -78,6 +79,70 @@ static const void *of_get_mac_addr_nvmem(struct device_node *np)
40 return mac;
41 }
42
43 +static const void *of_get_mac_address_mtd(struct device_node *np)
44 +{
45 +#ifdef CONFIG_MTD
46 + struct device_node *mtd_np = NULL;
47 + struct property *prop;
48 + size_t retlen;
49 + int size, ret;
50 + struct mtd_info *mtd;
51 + const char *part;
52 + const __be32 *list;
53 + phandle phandle;
54 + u8 mac[ETH_ALEN];
55 + void *addr;
56 +
57 + list = of_get_property(np, "mtd-mac-address", &size);
58 + if (!list || (size != (2 * sizeof(*list))))
59 + return NULL;
60 +
61 + phandle = be32_to_cpup(list++);
62 + if (phandle)
63 + mtd_np = of_find_node_by_phandle(phandle);
64 +
65 + if (!mtd_np)
66 + return NULL;
67 +
68 + part = of_get_property(mtd_np, "label", NULL);
69 + if (!part)
70 + part = mtd_np->name;
71 +
72 + mtd = get_mtd_device_nm(part);
73 + if (IS_ERR(mtd))
74 + return NULL;
75 +
76 + ret = mtd_read(mtd, be32_to_cpup(list), 6, &retlen, mac);
77 + put_mtd_device(mtd);
78 +
79 + if (!is_valid_ether_addr(mac))
80 + return NULL;
81 +
82 + addr = of_get_mac_addr(np, "mac-address");
83 + if (addr) {
84 + memcpy(addr, mac, ETH_ALEN);
85 + return addr;
86 + }
87 +
88 + prop = kzalloc(sizeof(*prop), GFP_KERNEL);
89 + if (!prop)
90 + return NULL;
91 +
92 + prop->name = "mac-address";
93 + prop->length = ETH_ALEN;
94 + prop->value = kmemdup(mac, ETH_ALEN, GFP_KERNEL);
95 + if (!prop->value || of_add_property(np, prop))
96 + goto free;
97 +
98 + return prop->value;
99 +free:
100 + kfree(prop->value);
101 + kfree(prop);
102 +#endif
103 + return NULL;
104 +}
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 @@ -98,6 +163,10 @@ static const void *of_get_mac_addr_nvmem(struct device_node *np)
111 * this case, the real MAC is in 'local-mac-address', and 'mac-address' exists
112 * but is all zeros.
113 *
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 * Return: Will be a valid pointer on success and ERR_PTR in case of error.
119 */
120 const void *of_get_mac_address(struct device_node *np)
121 @@ -116,6 +185,10 @@ const void *of_get_mac_address(struct device_node *np)
122 if (addr)
123 return addr;
124
125 + addr = of_get_mac_address_mtd(np);
126 + if (addr)
127 + return addr;
128 +
129 return of_get_mac_addr_nvmem(np);
130 }
131 EXPORT_SYMBOL(of_get_mac_address);
132 --
133 2.30.2
134