linux/rockchip: update the USB 3.0 controller node patch
[openwrt/staging/jow.git] / target / linux / generic / pending-5.10 / 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 --- a/drivers/of/of_net.c
19 +++ b/drivers/of/of_net.c
20 @@ -11,6 +11,7 @@
21 #include <linux/phy.h>
22 #include <linux/export.h>
23 #include <linux/device.h>
24 +#include <linux/mtd/mtd.h>
25
26 /**
27 * of_get_phy_mode - Get phy mode for given device_node
28 @@ -45,7 +46,7 @@ int of_get_phy_mode(struct device_node *
29 }
30 EXPORT_SYMBOL_GPL(of_get_phy_mode);
31
32 -static const void *of_get_mac_addr(struct device_node *np, const char *name)
33 +static void *of_get_mac_addr(struct device_node *np, const char *name)
34 {
35 struct property *pp = of_find_property(np, name, NULL);
36
37 @@ -78,6 +79,70 @@ static const void *of_get_mac_addr_nvmem
38 return mac;
39 }
40
41 +static const void *of_get_mac_address_mtd(struct device_node *np)
42 +{
43 +#ifdef CONFIG_MTD
44 + struct device_node *mtd_np = NULL;
45 + struct property *prop;
46 + size_t retlen;
47 + int size, ret;
48 + struct mtd_info *mtd;
49 + const char *part;
50 + const __be32 *list;
51 + phandle phandle;
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 (!is_valid_ether_addr(mac))
78 + return NULL;
79 +
80 + addr = of_get_mac_addr(np, "mac-address");
81 + if (addr) {
82 + memcpy(addr, mac, ETH_ALEN);
83 + return addr;
84 + }
85 +
86 + prop = kzalloc(sizeof(*prop), GFP_KERNEL);
87 + if (!prop)
88 + return NULL;
89 +
90 + prop->name = "mac-address";
91 + prop->length = ETH_ALEN;
92 + prop->value = kmemdup(mac, ETH_ALEN, GFP_KERNEL);
93 + if (!prop->value || of_add_property(np, prop))
94 + goto free;
95 +
96 + return prop->value;
97 +free:
98 + kfree(prop->value);
99 + kfree(prop);
100 +#endif
101 + return NULL;
102 +}
103 +
104 +
105 /**
106 * Search the device tree for the best MAC address to use. 'mac-address' is
107 * checked first, because that is supposed to contain to "most recent" MAC
108 @@ -98,6 +163,10 @@ static const void *of_get_mac_addr_nvmem
109 * this case, the real MAC is in 'local-mac-address', and 'mac-address' exists
110 * but is all zeros.
111 *
112 + *
113 + * If a mtd-mac-address property exists, try to fetch the MAC address from the
114 + * specified mtd device, and store it as a 'mac-address' property
115 + *
116 * Return: Will be a valid pointer on success and ERR_PTR in case of error.
117 */
118 const void *of_get_mac_address(struct device_node *np)
119 @@ -116,6 +185,10 @@ const void *of_get_mac_address(struct de
120 if (addr)
121 return addr;
122
123 + addr = of_get_mac_address_mtd(np);
124 + if (addr)
125 + return addr;
126 +
127 return of_get_mac_addr_nvmem(np);
128 }
129 EXPORT_SYMBOL(of_get_mac_address);