From mboxrd@z Thu Jan 1 00:00:00 1970 From: Phil Elwell Subject: [PATCH 1/4] lan78xx: Read MAC address from DT if present Date: Thu, 12 Apr 2018 14:55:33 +0100 Message-ID: <1523541336-145953-2-git-send-email-phil@raspberrypi.org> References: <1523541336-145953-1-git-send-email-phil@raspberrypi.org> Cc: Phil Elwell To: Woojung Huh , Microchip Linux Driver Support , Rob Herring , Mark Rutland , "David S. Miller" , Mauro Carvalho Chehab , Greg Kroah-Hartman , Linus Walleij , Andrew Morton , Randy Dunlap , netdev@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org Return-path: Received: from mx08-00252a01.pphosted.com ([91.207.212.211]:40332 "EHLO mx08-00252a01.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752396AbeDLNzu (ORCPT ); Thu, 12 Apr 2018 09:55:50 -0400 Received: from pps.filterd (m0102629.ppops.net [127.0.0.1]) by mx08-00252a01.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id w3CDrYcJ012269 for ; Thu, 12 Apr 2018 14:55:49 +0100 Received: from mail-wm0-f71.google.com (mail-wm0-f71.google.com [74.125.82.71]) by mx08-00252a01.pphosted.com with ESMTP id 2h6trdtjrf-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=OK) for ; Thu, 12 Apr 2018 14:55:49 +0100 Received: by mail-wm0-f71.google.com with SMTP id m125so2802297wma.9 for ; Thu, 12 Apr 2018 06:55:49 -0700 (PDT) In-Reply-To: <1523541336-145953-1-git-send-email-phil@raspberrypi.org> Sender: netdev-owner@vger.kernel.org List-ID: There is a standard mechanism for locating and using a MAC address from the Device Tree. Use this facility in the lan78xx driver to support applications without programmed EEPROM or OTP. At the same time, regularise the handling of the different address sources. Signed-off-by: Phil Elwell --- drivers/net/usb/lan78xx.c | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/drivers/net/usb/lan78xx.c b/drivers/net/usb/lan78xx.c index 55a78eb..d2727b5 100644 --- a/drivers/net/usb/lan78xx.c +++ b/drivers/net/usb/lan78xx.c @@ -37,6 +37,7 @@ #include #include #include +#include #include "lan78xx.h" #define DRIVER_AUTHOR "WOOJUNG HUH " @@ -1651,34 +1652,35 @@ static void lan78xx_init_mac_address(struct lan78xx_net *dev) addr[5] = (addr_hi >> 8) & 0xFF; if (!is_valid_ether_addr(addr)) { - /* reading mac address from EEPROM or OTP */ - if ((lan78xx_read_eeprom(dev, EEPROM_MAC_OFFSET, ETH_ALEN, - addr) == 0) || - (lan78xx_read_otp(dev, EEPROM_MAC_OFFSET, ETH_ALEN, - addr) == 0)) { - if (is_valid_ether_addr(addr)) { - /* eeprom values are valid so use them */ - netif_dbg(dev, ifup, dev->net, - "MAC address read from EEPROM"); - } else { - /* generate random MAC */ - random_ether_addr(addr); - netif_dbg(dev, ifup, dev->net, - "MAC address set to random addr"); - } + const u8 *mac_addr; - addr_lo = addr[0] | (addr[1] << 8) | - (addr[2] << 16) | (addr[3] << 24); - addr_hi = addr[4] | (addr[5] << 8); - - ret = lan78xx_write_reg(dev, RX_ADDRL, addr_lo); - ret = lan78xx_write_reg(dev, RX_ADDRH, addr_hi); + mac_addr = of_get_mac_address(dev->udev->dev.of_node); + if (mac_addr) { + /* valid address present in Device Tree */ + ether_addr_copy(addr, mac_addr); + netif_dbg(dev, ifup, dev->net, + "MAC address read from Device Tree"); + } else if (((lan78xx_read_eeprom(dev, EEPROM_MAC_OFFSET, + ETH_ALEN, addr) == 0) || + (lan78xx_read_otp(dev, EEPROM_MAC_OFFSET, + ETH_ALEN, addr) == 0)) && + is_valid_ether_addr(addr)) { + /* eeprom values are valid so use them */ + netif_dbg(dev, ifup, dev->net, + "MAC address read from EEPROM"); } else { /* generate random MAC */ random_ether_addr(addr); netif_dbg(dev, ifup, dev->net, "MAC address set to random addr"); } + + addr_lo = addr[0] | (addr[1] << 8) | + (addr[2] << 16) | (addr[3] << 24); + addr_hi = addr[4] | (addr[5] << 8); + + ret = lan78xx_write_reg(dev, RX_ADDRL, addr_lo); + ret = lan78xx_write_reg(dev, RX_ADDRH, addr_hi); } ret = lan78xx_write_reg(dev, MAF_LO(0), addr_lo); -- 2.7.4