Skip to content

Commit 37a0bc3

Browse files
committed
Merge branch 'davinci_emac-read-the-MAC-address-from-nvmem'
Bartosz Golaszewski says: ==================== davinci_emac: read the MAC address from nvmem This series is part of a bigger series that aims at removing the platform data structure from the at24 EEPROM driver[1]. We provide a generalized version of of_get_nvmem_mac_address(), switch the only user of the of_ variant to using it, remove the previous implementation and use the new routine in the davinci_emac driver. [1] https://lkml.org/lkml/2018/11/13/884 ==================== Signed-off-by: David S. Miller <davem@davemloft.net>
2 parents 82208d0 + 18dbfc8 commit 37a0bc3

File tree

6 files changed

+49
-51
lines changed

6 files changed

+49
-51
lines changed

drivers/net/ethernet/cadence/macb_main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4055,7 +4055,7 @@ static int macb_probe(struct platform_device *pdev)
40554055
if (mac) {
40564056
ether_addr_copy(bp->dev->dev_addr, mac);
40574057
} else {
4058-
err = of_get_nvmem_mac_address(np, bp->dev->dev_addr);
4058+
err = nvmem_get_mac_address(&pdev->dev, bp->dev->dev_addr);
40594059
if (err) {
40604060
if (err == -EPROBE_DEFER)
40614061
goto err_out_free_netdev;

drivers/net/ethernet/ti/davinci_emac.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1912,11 +1912,15 @@ static int davinci_emac_probe(struct platform_device *pdev)
19121912
ether_addr_copy(ndev->dev_addr, priv->mac_addr);
19131913

19141914
if (!is_valid_ether_addr(priv->mac_addr)) {
1915-
/* Use random MAC if none passed */
1916-
eth_hw_addr_random(ndev);
1917-
memcpy(priv->mac_addr, ndev->dev_addr, ndev->addr_len);
1918-
dev_warn(&pdev->dev, "using random MAC addr: %pM\n",
1919-
priv->mac_addr);
1915+
/* Try nvmem if MAC wasn't passed over pdata or DT. */
1916+
rc = nvmem_get_mac_address(&pdev->dev, priv->mac_addr);
1917+
if (rc) {
1918+
/* Use random MAC if still none obtained. */
1919+
eth_hw_addr_random(ndev);
1920+
memcpy(priv->mac_addr, ndev->dev_addr, ndev->addr_len);
1921+
dev_warn(&pdev->dev, "using random MAC addr: %pM\n",
1922+
priv->mac_addr);
1923+
}
19201924
}
19211925

19221926
ndev->netdev_ops = &emac_netdev_ops;

drivers/of/of_net.c

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -81,42 +81,3 @@ const void *of_get_mac_address(struct device_node *np)
8181
return of_get_mac_addr(np, "address");
8282
}
8383
EXPORT_SYMBOL(of_get_mac_address);
84-
85-
/**
86-
* Obtain the MAC address from an nvmem provider named 'mac-address' through
87-
* device tree.
88-
* On success, copies the new address into memory pointed to by addr and
89-
* returns 0. Returns a negative error code otherwise.
90-
* @np: Device tree node containing the nvmem-cells phandle
91-
* @addr: Pointer to receive the MAC address using ether_addr_copy()
92-
*/
93-
int of_get_nvmem_mac_address(struct device_node *np, void *addr)
94-
{
95-
struct nvmem_cell *cell;
96-
const void *mac;
97-
size_t len;
98-
int ret;
99-
100-
cell = of_nvmem_cell_get(np, "mac-address");
101-
if (IS_ERR(cell))
102-
return PTR_ERR(cell);
103-
104-
mac = nvmem_cell_read(cell, &len);
105-
106-
nvmem_cell_put(cell);
107-
108-
if (IS_ERR(mac))
109-
return PTR_ERR(mac);
110-
111-
if (len < ETH_ALEN || !is_valid_ether_addr(mac)) {
112-
ret = -EINVAL;
113-
} else {
114-
ether_addr_copy(addr, mac);
115-
ret = 0;
116-
}
117-
118-
kfree(mac);
119-
120-
return ret;
121-
}
122-
EXPORT_SYMBOL(of_get_nvmem_mac_address);

include/linux/etherdevice.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
struct device;
3333
int eth_platform_get_mac_address(struct device *dev, u8 *mac_addr);
3434
unsigned char *arch_get_platform_mac_address(void);
35+
int nvmem_get_mac_address(struct device *dev, void *addrbuf);
3536
u32 eth_get_headlen(void *data, unsigned int max_len);
3637
__be16 eth_type_trans(struct sk_buff *skb, struct net_device *dev);
3738
extern const struct header_ops eth_header_ops;

include/linux/of_net.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
struct net_device;
1414
extern int of_get_phy_mode(struct device_node *np);
1515
extern const void *of_get_mac_address(struct device_node *np);
16-
extern int of_get_nvmem_mac_address(struct device_node *np, void *addr);
1716
extern struct net_device *of_find_net_device_by_node(struct device_node *np);
1817
#else
1918
static inline int of_get_phy_mode(struct device_node *np)
@@ -26,11 +25,6 @@ static inline const void *of_get_mac_address(struct device_node *np)
2625
return NULL;
2726
}
2827

29-
static inline int of_get_nvmem_mac_address(struct device_node *np, void *addr)
30-
{
31-
return -ENODEV;
32-
}
33-
3428
static inline struct net_device *of_find_net_device_by_node(struct device_node *np)
3529
{
3630
return NULL;

net/ethernet/eth.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include <linux/inet.h>
4848
#include <linux/ip.h>
4949
#include <linux/netdevice.h>
50+
#include <linux/nvmem-consumer.h>
5051
#include <linux/etherdevice.h>
5152
#include <linux/skbuff.h>
5253
#include <linux/errno.h>
@@ -550,3 +551,40 @@ int eth_platform_get_mac_address(struct device *dev, u8 *mac_addr)
550551
return 0;
551552
}
552553
EXPORT_SYMBOL(eth_platform_get_mac_address);
554+
555+
/**
556+
* Obtain the MAC address from an nvmem cell named 'mac-address' associated
557+
* with given device.
558+
*
559+
* @dev: Device with which the mac-address cell is associated.
560+
* @addrbuf: Buffer to which the MAC address will be copied on success.
561+
*
562+
* Returns 0 on success or a negative error number on failure.
563+
*/
564+
int nvmem_get_mac_address(struct device *dev, void *addrbuf)
565+
{
566+
struct nvmem_cell *cell;
567+
const void *mac;
568+
size_t len;
569+
570+
cell = nvmem_cell_get(dev, "mac-address");
571+
if (IS_ERR(cell))
572+
return PTR_ERR(cell);
573+
574+
mac = nvmem_cell_read(cell, &len);
575+
nvmem_cell_put(cell);
576+
577+
if (IS_ERR(mac))
578+
return PTR_ERR(mac);
579+
580+
if (len != ETH_ALEN || !is_valid_ether_addr(mac)) {
581+
kfree(mac);
582+
return -EINVAL;
583+
}
584+
585+
ether_addr_copy(addrbuf, mac);
586+
kfree(mac);
587+
588+
return 0;
589+
}
590+
EXPORT_SYMBOL(nvmem_get_mac_address);

0 commit comments

Comments
 (0)