Skip to contentSkip to conversation

sky2: allow mac to come from dt

PATCH Superseded Tim Harvey 3 participants 4 messages Mar 5, 2014 – Mar 7, 2014

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Tim Harvey

author
#0 lore raw

PATCH v2 1/1 sky2: allow mac to come from dt

The driver reads the mac address from the device registers which would need to have been programmed by the bootloader. This patch adds the ability to pull the mac from devicetree via the pci device dt node.

Changes since v1:
- simplified based on feedback
- fixed formatting

---
 drivers/net/ethernet/marvell/sky2.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
drivers/net/ethernet/marvell/sky2.c +14 -2
index 55a37ae..471473e 100644
@@ -44,6 +44,8 @@
 #include <linux/prefetch.h>
 #include <linux/debugfs.h>
 #include <linux/mii.h>
+#include <linux/of_device.h>
+#include <linux/of_net.h>
 
 #include <asm/irq.h>
 
@@ -4748,6 +4750,7 @@ static struct net_device *sky2_init_netdev(struct sky2_hw *hw, unsigned port,
 {
 	struct sky2_port *sky2;
 	struct net_device *dev = alloc_etherdev(sizeof(*sky2));
+	unsigned char *iap, tmpaddr[ETH_ALEN];
 
 	if (!dev)
 		return NULL;
@@ -4805,8 +4808,17 @@ static struct net_device *sky2_init_netdev(struct sky2_hw *hw, unsigned port,
 
 	dev->features |= dev->hw_features;
 
-	/* read the mac address */
-	memcpy_fromio(dev->dev_addr, hw->regs + B2_MAC_1 + port * 8, ETH_ALEN);
+	/* try to get mac address in the following order:
+	 * 1) from device tree data
+	 * 2) from internal registers set by bootloader
+	 */
+	iap = of_get_mac_address(hw->pdev->dev.of_node);
+	if (!iap) {
+		memcpy_fromio(&tmpaddr, hw->regs + B2_MAC_1 + port * 8,
+			      ETH_ALEN);
+		iap = tmpaddr;
+	}
+	ether_addr_copy(dev->dev_addr, iap);
 
 	return dev;
 }
Signature · 1 line
1.8.3.2
#1 lore raw
Quoted 11 lines · On Wed, 5 Mar 2014 14:45:12 -0800 Tim Harvey <[email protected]> wrote:
drivers/net/ethernet/marvell/sky2.c +2 -2
+	unsigned char *iap, tmpaddr[ETH_ALEN];
 
 	if (!dev)
 		return NULL;
@@ -4805,8 +4808,17 @@ static struct net_device *sky2_init_netdev(struct sky2_hw *hw, unsigned port,
 
 	dev->features |= dev->hw_features;
 
-	/* read the mac address */
-	memcpy_fromio(dev->dev_addr, hw->regs + B2_MAC_1 + port * 8, ETH_ALEN);
+	/* try to get mac address in the following order:

drivers/net/ethernet/marvell/sky2.c static struct net_device *sky2_init_netdev(struct sky2_hw *hw, unsigned port, · line 4821

Quoted 10 lines
drivers/net/ethernet/marvell/sky2.c +10 -0
+	 * 1) from device tree data
+	 * 2) from internal registers set by bootloader
+	 */
+	iap = of_get_mac_address(hw->pdev->dev.of_node);
+	if (!iap) {
+		memcpy_fromio(&tmpaddr, hw->regs + B2_MAC_1 + port * 8,
+			      ETH_ALEN);
+		iap = tmpaddr;
+	}
+	ether_addr_copy(dev->dev_addr, iap);

Can be done without stack tmpaddr.

maintainer
#2 lore raw
In reply to Tim Harvey

From: Tim Harvey <[email protected]>
Date: Wed, 5 Mar 2014 14:45:12 -0800

Quoted 15 lines
drivers/net/ethernet/marvell/sky2.c +2 -2
@@ -4748,6 +4750,7 @@ static struct net_device *sky2_init_netdev(struct sky2_hw *hw, unsigned port,
 {
 	struct sky2_port *sky2;
 	struct net_device *dev = alloc_etherdev(sizeof(*sky2));
+	unsigned char *iap, tmpaddr[ETH_ALEN];
 
 	if (!dev)
 		return NULL;
@@ -4805,8 +4808,17 @@ static struct net_device *sky2_init_netdev(struct sky2_hw *hw, unsigned port,
 
 	dev->features |= dev->hw_features;
 
-	/* read the mac address */
-	memcpy_fromio(dev->dev_addr, hw->regs + B2_MAC_1 + port * 8, ETH_ALEN);
+	/* try to get mac address in the following order:

drivers/net/ethernet/marvell/sky2.c static struct net_device *sky2_init_netdev(struct sky2_hw *hw, unsigned port, · line 4821

Quoted 10 lines
drivers/net/ethernet/marvell/sky2.c +10 -0
+	 * 1) from device tree data
+	 * 2) from internal registers set by bootloader
+	 */
+	iap = of_get_mac_address(hw->pdev->dev.of_node);
+	if (!iap) {
+		memcpy_fromio(&tmpaddr, hw->regs + B2_MAC_1 + port * 8,
+			      ETH_ALEN);
+		iap = tmpaddr;
+	}
+	ether_addr_copy(dev->dev_addr, iap);

As Stephen Hemminger suggested do this like:

	iap = of_get_mac_address(hw->pdev->dev.of_node);
	if (iap)
		memcpy(dev->dev_addr, iap, ETH_ALEN);
	else
		memcpy_fromio(dev->dev_addr, hw->regs + B2_MAC_1 + port * 8,
			      ETH_ALEN);

That way you don't need the on-stack buffer.

author
#3 lore raw
Quoted 31 lines · David Miller

From: Tim Harvey <[email protected]>
Date: Wed, 5 Mar 2014 14:45:12 -0800

Quoted 15 lines
drivers/net/ethernet/marvell/sky2.c +2 -2
@@ -4748,6 +4750,7 @@ static struct net_device *sky2_init_netdev(struct sky2_hw *hw, unsigned port,
 {
      struct sky2_port *sky2;
      struct net_device *dev = alloc_etherdev(sizeof(*sky2));
+     unsigned char *iap, tmpaddr[ETH_ALEN];
 
      if (!dev)
              return NULL;
@@ -4805,8 +4808,17 @@ static struct net_device *sky2_init_netdev(struct sky2_hw *hw, unsigned port,
 
      dev->features |= dev->hw_features;
 
-     /* read the mac address */
-     memcpy_fromio(dev->dev_addr, hw->regs + B2_MAC_1 + port * 8, ETH_ALEN);
+     /* try to get mac address in the following order:

drivers/net/ethernet/marvell/sky2.c static struct net_device *sky2_init_netdev(struct sky2_hw *hw, unsigned port, · line 4821

Quoted 10 lines
drivers/net/ethernet/marvell/sky2.c +10 -0
+      * 1) from device tree data
+      * 2) from internal registers set by bootloader
+      */
+     iap = of_get_mac_address(hw->pdev->dev.of_node);
+     if (!iap) {
+             memcpy_fromio(&tmpaddr, hw->regs + B2_MAC_1 + port * 8,
+                           ETH_ALEN);
+             iap = tmpaddr;
+     }
+     ether_addr_copy(dev->dev_addr, iap);

As Stephen Hemminger suggested do this like:

Quoted 8 lines
        iap = of_get_mac_address(hw->pdev->dev.of_node);
        if (iap)
                memcpy(dev->dev_addr, iap, ETH_ALEN);
        else
                memcpy_fromio(dev->dev_addr, hw->regs + B2_MAC_1 + port * 8,
                              ETH_ALEN);

That way you don't need the on-stack buffer.

Thanks for the suggestion - I will re-send a v3 shortly.

Tim