treewide: backport support for nvmem on non platform devices
[openwrt/openwrt.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 @@ -12,6 +12,7 @@
21 #include <linux/export.h>
22 #include <linux/device.h>
23 #include <linux/nvmem-consumer.h>
24 +#include <linux/mtd/mtd.h>
25
26 /**
27 * of_get_phy_mode - Get phy mode for given device_node
28 @@ -95,6 +96,52 @@ static int of_get_mac_addr_nvmem(struct
29 return 0;
30 }
31
32 +static int of_get_mac_address_mtd(struct device_node *np, u8 *addr)
33 +{
34 +#ifdef CONFIG_MTD
35 + struct platform_device *pdev = of_find_device_by_node(np);
36 + struct device_node *mtd_np = NULL;
37 + size_t retlen;
38 + int size, ret;
39 + struct mtd_info *mtd;
40 + const char *part;
41 + const __be32 *list;
42 + phandle phandle;
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 -ENODEV;
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 -ENODEV;
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 -ENODEV;
63 +
64 + ret = mtd_read(mtd, be32_to_cpup(list), 6, &retlen, mac);
65 + put_mtd_device(mtd);
66 +
67 + if (!is_valid_ether_addr(mac))
68 + return -EINVAL;
69 +
70 + memcpy(addr, mac, ETH_ALEN);
71 +
72 + return 0;
73 +#endif
74 + return -EINVAL;
75 +}
76 +
77 +
78 /**
79 * Search the device tree for the best MAC address to use. 'mac-address' is
80 * checked first, because that is supposed to contain to "most recent" MAC
81 @@ -115,6 +161,10 @@ static int of_get_mac_addr_nvmem(struct
82 * this case, the real MAC is in 'local-mac-address', and 'mac-address' exists
83 * but is all zeros.
84 *
85 + *
86 + * If a mtd-mac-address property exists, try to fetch the MAC address from the
87 + * specified mtd device.
88 + *
89 * Return: 0 on success and errno in case of error.
90 */
91 int of_get_mac_address(struct device_node *np, u8 *addr)
92 @@ -136,6 +186,10 @@ int of_get_mac_address(struct device_nod
93 if (!ret)
94 return 0;
95
96 + ret = of_get_mac_address_mtd(np, addr);
97 + if (!ret)
98 + return 0;
99 +
100 return of_get_mac_addr_nvmem(np, addr);
101 }
102 EXPORT_SYMBOL(of_get_mac_address);