From d4efa05303408d84a347c1c560ea860cf9ad953c Mon Sep 17 00:00:00 2001 From: Ahmad Fatoum Date: Sat, 19 Jun 2021 05:45:15 +0200 Subject: net: consult device tree for ethernet address in NVMEM as fall-back While barebox fixes up the mac-address into the device tree, it doesn't care much for extracting a mac address _from_ the device tree, whether it be from local-mac-address property or from a mac-address nvmem cell. Fix the latter by calling of_get_mac_addr_nvmem for each Ethernet adapter. We do this in a very late initcall, because we don't want to enforce a probe a probe order between nvmem providers and network devices. We can't do it at randomization time, because we need to fixup Ethernet mac addresses, even when barebox itself doesn't ifup the netdev. of_get_mac_addr_nvmem could be replaced by of_get_mac_address to also parse local-mac-address and brethern, but justifying this change is left as a future exercise. Cc: Michael Grzeschik Signed-off-by: Ahmad Fatoum Link: https://lore.barebox.org/20210619034516.6737-13-a.fatoum@pengutronix.de Signed-off-by: Sascha Hauer --- net/eth.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'net/eth.c') diff --git a/net/eth.c b/net/eth.c index 84f99d3aa8..762c5dfb8a 100644 --- a/net/eth.c +++ b/net/eth.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -504,3 +505,26 @@ void led_trigger_network(enum led_trigger trigger) led_trigger(trigger, TRIGGER_FLASH); led_trigger(LED_TRIGGER_NET_TXRX, TRIGGER_FLASH); } + +static int of_populate_ethaddr(void) +{ + char str[sizeof("xx:xx:xx:xx:xx:xx")]; + struct eth_device *edev; + int ret; + + list_for_each_entry(edev, &netdev_list, list) { + if (!edev->parent || is_valid_ether_addr(edev->ethaddr)) + continue; + + ret = of_get_mac_addr_nvmem(edev->parent->device_node, edev->ethaddr); + if (ret) + continue; + + ethaddr_to_string(edev->ethaddr, str); + dev_info(&edev->dev, "Got preset MAC address from device tree: %s\n", str); + eth_set_ethaddr(edev, edev->ethaddr); + } + + return 0; +} +postenvironment_initcall(of_populate_ethaddr); -- cgit v1.2.3