Skip to content

Commit c387e4c

Browse files
ynezzclaudiubeznea
authored andcommitted
of_net: add NVMEM support to of_get_mac_address
Many embedded devices have information such as MAC addresses stored inside NVMEMs like EEPROMs and so on. Currently there are only two drivers in the tree which benefit from NVMEM bindings. Adding support for NVMEM into every other driver would mean adding a lot of repetitive code. This patch allows us to configure MAC addresses in various devices like ethernet and wireless adapters directly from of_get_mac_address, which is already used by almost every driver in the tree. Predecessor of this patch which used directly MTD layer has originated in OpenWrt some time ago and supports already about 497 use cases in 357 device tree files. Cc: Alban Bedel <albeu@free.fr> Signed-off-by: Felix Fietkau <nbd@nbd.name> Signed-off-by: John Crispin <john@phrozen.org> Signed-off-by: Petr Štetiar <ynezz@true.cz> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent c88ee6c commit c387e4c

1 file changed

Lines changed: 51 additions & 3 deletions

File tree

drivers/of/of_net.c

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
#include <linux/kernel.h>
1010
#include <linux/nvmem-consumer.h>
1111
#include <linux/of_net.h>
12+
#include <linux/of_platform.h>
1213
#include <linux/phy.h>
1314
#include <linux/export.h>
15+
#include <linux/device.h>
1416

1517
/**
1618
* of_get_phy_mode - Get phy mode for given device_node
@@ -48,12 +50,52 @@ static const void *of_get_mac_addr(struct device_node *np, const char *name)
4850
return NULL;
4951
}
5052

53+
static const void *of_get_mac_addr_nvmem(struct device_node *np)
54+
{
55+
int ret;
56+
u8 mac[ETH_ALEN];
57+
struct property *pp;
58+
struct platform_device *pdev = of_find_device_by_node(np);
59+
60+
if (!pdev)
61+
return ERR_PTR(-ENODEV);
62+
63+
ret = nvmem_get_mac_address(&pdev->dev, &mac);
64+
if (ret)
65+
return ERR_PTR(ret);
66+
67+
pp = devm_kzalloc(&pdev->dev, sizeof(*pp), GFP_KERNEL);
68+
if (!pp)
69+
return ERR_PTR(-ENOMEM);
70+
71+
pp->name = "nvmem-mac-address";
72+
pp->length = ETH_ALEN;
73+
pp->value = devm_kmemdup(&pdev->dev, mac, ETH_ALEN, GFP_KERNEL);
74+
if (!pp->value) {
75+
ret = -ENOMEM;
76+
goto free;
77+
}
78+
79+
ret = of_add_property(np, pp);
80+
if (ret)
81+
goto free;
82+
83+
return pp->value;
84+
free:
85+
devm_kfree(&pdev->dev, pp->value);
86+
devm_kfree(&pdev->dev, pp);
87+
88+
return ERR_PTR(ret);
89+
}
90+
5191
/**
5292
* Search the device tree for the best MAC address to use. 'mac-address' is
5393
* checked first, because that is supposed to contain to "most recent" MAC
5494
* address. If that isn't set, then 'local-mac-address' is checked next,
55-
* because that is the default address. If that isn't set, then the obsolete
56-
* 'address' is checked, just in case we're using an old device tree.
95+
* because that is the default address. If that isn't set, then the obsolete
96+
* 'address' is checked, just in case we're using an old device tree. If any
97+
* of the above isn't set, then try to get MAC address from nvmem cell named
98+
* 'mac-address'.
5799
*
58100
* Note that the 'address' property is supposed to contain a virtual address of
59101
* the register set, but some DTS files have redefined that property to be the
@@ -65,6 +107,8 @@ static const void *of_get_mac_addr(struct device_node *np, const char *name)
65107
* addresses. Some older U-Boots only initialized 'local-mac-address'. In
66108
* this case, the real MAC is in 'local-mac-address', and 'mac-address' exists
67109
* but is all zeros.
110+
*
111+
* Return: Will be a valid pointer on success and ERR_PTR in case of error.
68112
*/
69113
const void *of_get_mac_address(struct device_node *np)
70114
{
@@ -78,7 +122,11 @@ const void *of_get_mac_address(struct device_node *np)
78122
if (addr)
79123
return addr;
80124

81-
return of_get_mac_addr(np, "address");
125+
addr = of_get_mac_addr(np, "address");
126+
if (addr)
127+
return addr;
128+
129+
return of_get_mac_addr_nvmem(np);
82130
}
83131
EXPORT_SYMBOL(of_get_mac_address);
84132

0 commit comments

Comments
 (0)