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()
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.
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>
15 drivers/of/of_net.c | 75 ++++++++++++++++++++++++++++++++++++++++++++-
16 1 file changed, 74 insertions(+), 1 deletion(-)
18 --- a/drivers/of/of_net.c
19 +++ b/drivers/of/of_net.c
21 #include <linux/phy.h>
22 #include <linux/export.h>
23 #include <linux/device.h>
24 +#include <linux/mtd/mtd.h>
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 *
30 EXPORT_SYMBOL_GPL(of_get_phy_mode);
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)
35 struct property *pp = of_find_property(np, name, NULL);
37 @@ -78,6 +79,55 @@ static const void *of_get_mac_addr_nvmem
41 +static const void *of_get_mac_address_mtd(struct device_node *np)
44 + struct platform_device *pdev = of_find_device_by_node(np);
45 + struct device_node *mtd_np = NULL;
46 + struct property *prop;
49 + struct mtd_info *mtd;
56 + list = of_get_property(np, "mtd-mac-address", &size);
57 + if (!list || (size != (2 * sizeof(*list))))
60 + phandle = be32_to_cpup(list++);
62 + mtd_np = of_find_node_by_phandle(phandle);
67 + part = of_get_property(mtd_np, "label", NULL);
69 + part = mtd_np->name;
71 + mtd = get_mtd_device_nm(part);
75 + ret = mtd_read(mtd, be32_to_cpup(list), 6, &retlen, mac);
76 + put_mtd_device(mtd);
78 + if (!is_valid_ether_addr(mac))
81 + addr = devm_kmemdup(&pdev->dev, mac, ETH_ALEN, GFP_KERNEL);
83 + return ERR_PTR(-ENOMEM);
91 * Search the device tree for the best MAC address to use. 'mac-address' is
92 * checked first, because that is supposed to contain to "most recent" MAC
93 @@ -98,6 +148,10 @@ static const void *of_get_mac_addr_nvmem
94 * this case, the real MAC is in 'local-mac-address', and 'mac-address' exists
98 + * If a mtd-mac-address property exists, try to fetch the MAC address from the
99 + * specified mtd device.
101 * Return: Will be a valid pointer on success and ERR_PTR in case of error.
103 const void *of_get_mac_address(struct device_node *np)
104 @@ -116,6 +170,10 @@ const void *of_get_mac_address(struct de
108 + addr = of_get_mac_address_mtd(np);
112 return of_get_mac_addr_nvmem(np);
114 EXPORT_SYMBOL(of_get_mac_address);