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/export.h>
22 #include <linux/device.h>
23 #include <linux/nvmem-consumer.h>
24 +#include <linux/mtd/mtd.h>
27 * of_get_phy_mode - Get phy mode for given device_node
28 @@ -89,6 +90,52 @@ static int of_get_mac_addr_nvmem(struct
32 +static int of_get_mac_address_mtd(struct device_node *np, u8 *addr)
35 + struct platform_device *pdev = of_find_device_by_node(np);
36 + struct device_node *mtd_np = NULL;
39 + struct mtd_info *mtd;
45 + list = of_get_property(np, "mtd-mac-address", &size);
46 + if (!list || (size != (2 * sizeof(*list))))
49 + phandle = be32_to_cpup(list++);
51 + mtd_np = of_find_node_by_phandle(phandle);
56 + part = of_get_property(mtd_np, "label", NULL);
58 + part = mtd_np->name;
60 + mtd = get_mtd_device_nm(part);
64 + ret = mtd_read(mtd, be32_to_cpup(list), 6, &retlen, mac);
65 + put_mtd_device(mtd);
67 + if (!is_valid_ether_addr(mac))
70 + memcpy(addr, mac, ETH_ALEN);
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 @@ -109,6 +156,10 @@ static int of_get_mac_addr_nvmem(struct
82 * this case, the real MAC is in 'local-mac-address', and 'mac-address' exists
86 + * If a mtd-mac-address property exists, try to fetch the MAC address from the
87 + * specified mtd device.
89 * Return: 0 on success and errno in case of error.
91 int of_get_mac_address(struct device_node *np, u8 *addr)
92 @@ -130,6 +181,10 @@ int of_get_mac_address(struct device_nod
96 + ret = of_get_mac_address_mtd(np, addr);
100 return of_get_mac_addr_nvmem(np, addr);
102 EXPORT_SYMBOL(of_get_mac_address);