[PATCH net-next 3/3] net: fec: add fec_set_hw_mac_addr() helper function

Wei Fang posted 3 patches 2 months, 4 weeks ago
There is a newer version of this series
[PATCH net-next 3/3] net: fec: add fec_set_hw_mac_addr() helper function
Posted by Wei Fang 2 months, 4 weeks ago
In the current driver, the MAC address is set in both fec_restart() and
fec_set_mac_address(), so a generic helper function fec_set_hw_mac_addr()
is added to set the hardware MAC address to make the code more compact.

Signed-off-by: Wei Fang <wei.fang@nxp.com>
---
 drivers/net/ethernet/freescale/fec_main.c | 27 +++++++++++++----------
 1 file changed, 15 insertions(+), 12 deletions(-)

diff --git a/drivers/net/ethernet/freescale/fec_main.c b/drivers/net/ethernet/freescale/fec_main.c
index 00f8be4119ed..883b28e59a3c 100644
--- a/drivers/net/ethernet/freescale/fec_main.c
+++ b/drivers/net/ethernet/freescale/fec_main.c
@@ -1123,6 +1123,18 @@ static void fec_ctrl_reset(struct fec_enet_private *fep, bool allow_wol)
 	}
 }
 
+static void fec_set_hw_mac_addr(struct net_device *ndev)
+{
+	struct fec_enet_private *fep = netdev_priv(ndev);
+	u32 temp_mac[2];
+
+	memcpy(temp_mac, ndev->dev_addr, ETH_ALEN);
+	writel((__force u32)cpu_to_be32(temp_mac[0]),
+	       fep->hwp + FEC_ADDR_LOW);
+	writel((__force u32)cpu_to_be32(temp_mac[1]),
+	       fep->hwp + FEC_ADDR_HIGH);
+}
+
 /*
  * This function is called to start or restart the FEC during a link
  * change, transmit timeout, or to reconfigure the FEC.  The network
@@ -1132,7 +1144,6 @@ static void
 fec_restart(struct net_device *ndev)
 {
 	struct fec_enet_private *fep = netdev_priv(ndev);
-	u32 temp_mac[2];
 	u32 rcntl = OPT_FRAME_SIZE | FEC_RCR_MII;
 	u32 ecntl = FEC_ECR_ETHEREN;
 
@@ -1145,11 +1156,7 @@ fec_restart(struct net_device *ndev)
 	 * enet-mac reset will reset mac address registers too,
 	 * so need to reconfigure it.
 	 */
-	memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN);
-	writel((__force u32)cpu_to_be32(temp_mac[0]),
-	       fep->hwp + FEC_ADDR_LOW);
-	writel((__force u32)cpu_to_be32(temp_mac[1]),
-	       fep->hwp + FEC_ADDR_HIGH);
+	fec_set_hw_mac_addr(ndev);
 
 	/* Clear any outstanding interrupt, except MDIO. */
 	writel((0xffffffff & ~FEC_ENET_MII), fep->hwp + FEC_IEVENT);
@@ -3693,7 +3700,6 @@ static void set_multicast_list(struct net_device *ndev)
 static int
 fec_set_mac_address(struct net_device *ndev, void *p)
 {
-	struct fec_enet_private *fep = netdev_priv(ndev);
 	struct sockaddr *addr = p;
 
 	if (addr) {
@@ -3710,11 +3716,8 @@ fec_set_mac_address(struct net_device *ndev, void *p)
 	if (!netif_running(ndev))
 		return 0;
 
-	writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
-		(ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
-		fep->hwp + FEC_ADDR_LOW);
-	writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
-		fep->hwp + FEC_ADDR_HIGH);
+	fec_set_hw_mac_addr(ndev);
+
 	return 0;
 }
 
-- 
2.34.1
Re: [PATCH net-next 3/3] net: fec: add fec_set_hw_mac_addr() helper function
Posted by Maxime Chevallier 2 months, 4 weeks ago
Hi,

On Thu, 10 Jul 2025 17:09:02 +0800
Wei Fang <wei.fang@nxp.com> wrote:

> In the current driver, the MAC address is set in both fec_restart() and
> fec_set_mac_address(), so a generic helper function fec_set_hw_mac_addr()
> is added to set the hardware MAC address to make the code more compact.
> 
> Signed-off-by: Wei Fang <wei.fang@nxp.com>
> ---
>  drivers/net/ethernet/freescale/fec_main.c | 27 +++++++++++++----------
>  1 file changed, 15 insertions(+), 12 deletions(-)
> 
> diff --git a/drivers/net/ethernet/freescale/fec_main.c b/drivers/net/ethernet/freescale/fec_main.c
> index 00f8be4119ed..883b28e59a3c 100644
> --- a/drivers/net/ethernet/freescale/fec_main.c
> +++ b/drivers/net/ethernet/freescale/fec_main.c
> @@ -1123,6 +1123,18 @@ static void fec_ctrl_reset(struct fec_enet_private *fep, bool allow_wol)
>  	}
>  }
>  
> +static void fec_set_hw_mac_addr(struct net_device *ndev)
> +{
> +	struct fec_enet_private *fep = netdev_priv(ndev);
> +	u32 temp_mac[2];
> +
> +	memcpy(temp_mac, ndev->dev_addr, ETH_ALEN);
> +	writel((__force u32)cpu_to_be32(temp_mac[0]),
> +	       fep->hwp + FEC_ADDR_LOW);
> +	writel((__force u32)cpu_to_be32(temp_mac[1]),
> +	       fep->hwp + FEC_ADDR_HIGH);
> +}

[ ... ]

> -	writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
> -		(ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
> -		fep->hwp + FEC_ADDR_LOW);
> -	writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
> -		fep->hwp + FEC_ADDR_HIGH);
> +	fec_set_hw_mac_addr(ndev);

It's more of a personal preference, but I find this implementation to
be much more readable than the one based on

  writel((__force u32)cpu_to_be32(temp_mac[...]), ...);

Maxime
Re: [PATCH net-next 3/3] net: fec: add fec_set_hw_mac_addr() helper function
Posted by Andrew Lunn 2 months, 4 weeks ago
> > -	writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) |
> > -		(ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24),
> > -		fep->hwp + FEC_ADDR_LOW);
> > -	writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24),
> > -		fep->hwp + FEC_ADDR_HIGH);
> > +	fec_set_hw_mac_addr(ndev);
> 
> It's more of a personal preference, but I find this implementation to
> be much more readable than the one based on
> 
>   writel((__force u32)cpu_to_be32(temp_mac[...]), ...);

It also avoids the __force, which is good.

	Andrew