Skip to content

Commit d01f449

Browse files
ynezzdavem330
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 <[email protected]> Signed-off-by: Felix Fietkau <[email protected]> Signed-off-by: John Crispin <[email protected]> Signed-off-by: Petr Štetiar <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 8ef5cc4 commit d01f449

File tree

1 file changed

+51
-3
lines changed

1 file changed

+51
-3
lines changed

drivers/of/of_net.c

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
#include <linux/etherdevice.h>
99
#include <linux/kernel.h>
1010
#include <linux/of_net.h>
11+
#include <linux/of_platform.h>
1112
#include <linux/phy.h>
1213
#include <linux/export.h>
14+
#include <linux/device.h>
1315

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

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

80-
return of_get_mac_addr(np, "address");
124+
addr = of_get_mac_addr(np, "address");
125+
if (addr)
126+
return addr;
127+
128+
return of_get_mac_addr_nvmem(np);
81129
}
82130
EXPORT_SYMBOL(of_get_mac_address);

0 commit comments

Comments
 (0)