1
0
Fork 0
mirror of synced 2025-03-06 20:59:54 +01:00
linux/drivers/net/ethernet/stmicro/stmmac/dwmac-anarion.c
Michael Walle 83216e3988 of: net: pass the dst buffer to of_get_mac_address()
of_get_mac_address() returns a "const void*" pointer to a MAC address.
Lately, support to fetch the MAC address by an NVMEM provider was added.
But this will only work with platform devices. It will not work with
PCI devices (e.g. of an integrated root complex) and esp. not with DSA
ports.

There is an of_* variant of the nvmem binding which works without
devices. The returned data of a nvmem_cell_read() has to be freed after
use. On the other hand the return of_get_mac_address() points to some
static data without a lifetime. The trick for now, was to allocate a
device resource managed buffer which is then returned. This will only
work if we have an actual device.

Change it, so that the caller of of_get_mac_address() has to supply a
buffer where the MAC address is written to. Unfortunately, this will
touch all drivers which use the of_get_mac_address().

Usually the code looks like:

  const char *addr;
  addr = of_get_mac_address(np);
  if (!IS_ERR(addr))
    ether_addr_copy(ndev->dev_addr, addr);

This can then be simply rewritten as:

  of_get_mac_address(np, ndev->dev_addr);

Sometimes is_valid_ether_addr() is used to test the MAC address.
of_get_mac_address() already makes sure, it just returns a valid MAC
address. Thus we can just test its return code. But we have to be
careful if there are still other sources for the MAC address before the
of_get_mac_address(). In this case we have to keep the
is_valid_ether_addr() call.

The following coccinelle patch was used to convert common cases to the
new style. Afterwards, I've manually gone over the drivers and fixed the
return code variable: either used a new one or if one was already
available use that. Mansour Moufid, thanks for that coccinelle patch!

<spml>
@a@
identifier x;
expression y, z;
@@
- x = of_get_mac_address(y);
+ x = of_get_mac_address(y, z);
  <...
- ether_addr_copy(z, x);
  ...>

@@
identifier a.x;
@@
- if (<+... x ...+>) {}

@@
identifier a.x;
@@
  if (<+... x ...+>) {
      ...
  }
- else {}

@@
identifier a.x;
expression e;
@@
- if (<+... x ...+>@e)
-     {}
- else
+ if (!(e))
      {...}

@@
expression x, y, z;
@@
- x = of_get_mac_address(y, z);
+ of_get_mac_address(y, z);
  ... when != x
</spml>

All drivers, except drivers/net/ethernet/aeroflex/greth.c, were
compile-time tested.

Suggested-by: Andrew Lunn <andrew@lunn.ch>
Signed-off-by: Michael Walle <michael@walle.cc>
Reviewed-by: Andrew Lunn <andrew@lunn.ch>
Signed-off-by: David S. Miller <davem@davemloft.net>
2021-04-13 14:35:02 -07:00

155 lines
3.7 KiB
C

// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Adaptrum Anarion DWMAC glue layer
*
* Copyright (C) 2017, Adaptrum, Inc.
* (Written by Alexandru Gagniuc <alex.g at adaptrum.com> for Adaptrum, Inc.)
*/
#include <linux/io.h>
#include <linux/of.h>
#include <linux/of_net.h>
#include <linux/stmmac.h>
#include "stmmac.h"
#include "stmmac_platform.h"
#define GMAC_RESET_CONTROL_REG 0
#define GMAC_SW_CONFIG_REG 4
#define GMAC_CONFIG_INTF_SEL_MASK (0x7 << 0)
#define GMAC_CONFIG_INTF_RGMII (0x1 << 0)
struct anarion_gmac {
uintptr_t ctl_block;
uint32_t phy_intf_sel;
};
static uint32_t gmac_read_reg(struct anarion_gmac *gmac, uint8_t reg)
{
return readl((void *)(gmac->ctl_block + reg));
};
static void gmac_write_reg(struct anarion_gmac *gmac, uint8_t reg, uint32_t val)
{
writel(val, (void *)(gmac->ctl_block + reg));
}
static int anarion_gmac_init(struct platform_device *pdev, void *priv)
{
uint32_t sw_config;
struct anarion_gmac *gmac = priv;
/* Reset logic, configure interface mode, then release reset. SIMPLE! */
gmac_write_reg(gmac, GMAC_RESET_CONTROL_REG, 1);
sw_config = gmac_read_reg(gmac, GMAC_SW_CONFIG_REG);
sw_config &= ~GMAC_CONFIG_INTF_SEL_MASK;
sw_config |= (gmac->phy_intf_sel & GMAC_CONFIG_INTF_SEL_MASK);
gmac_write_reg(gmac, GMAC_SW_CONFIG_REG, sw_config);
gmac_write_reg(gmac, GMAC_RESET_CONTROL_REG, 0);
return 0;
}
static void anarion_gmac_exit(struct platform_device *pdev, void *priv)
{
struct anarion_gmac *gmac = priv;
gmac_write_reg(gmac, GMAC_RESET_CONTROL_REG, 1);
}
static struct anarion_gmac *anarion_config_dt(struct platform_device *pdev)
{
struct anarion_gmac *gmac;
phy_interface_t phy_mode;
void __iomem *ctl_block;
int err;
ctl_block = devm_platform_ioremap_resource(pdev, 1);
if (IS_ERR(ctl_block)) {
dev_err(&pdev->dev, "Cannot get reset region (%ld)!\n",
PTR_ERR(ctl_block));
return ctl_block;
}
gmac = devm_kzalloc(&pdev->dev, sizeof(*gmac), GFP_KERNEL);
if (!gmac)
return ERR_PTR(-ENOMEM);
gmac->ctl_block = (uintptr_t)ctl_block;
err = of_get_phy_mode(pdev->dev.of_node, &phy_mode);
if (err)
return ERR_PTR(err);
switch (phy_mode) {
case PHY_INTERFACE_MODE_RGMII:
fallthrough;
case PHY_INTERFACE_MODE_RGMII_ID:
case PHY_INTERFACE_MODE_RGMII_RXID:
case PHY_INTERFACE_MODE_RGMII_TXID:
gmac->phy_intf_sel = GMAC_CONFIG_INTF_RGMII;
break;
default:
dev_err(&pdev->dev, "Unsupported phy-mode (%d)\n",
phy_mode);
return ERR_PTR(-ENOTSUPP);
}
return gmac;
}
static int anarion_dwmac_probe(struct platform_device *pdev)
{
int ret;
struct anarion_gmac *gmac;
struct plat_stmmacenet_data *plat_dat;
struct stmmac_resources stmmac_res;
ret = stmmac_get_platform_resources(pdev, &stmmac_res);
if (ret)
return ret;
gmac = anarion_config_dt(pdev);
if (IS_ERR(gmac))
return PTR_ERR(gmac);
plat_dat = stmmac_probe_config_dt(pdev, stmmac_res.mac);
if (IS_ERR(plat_dat))
return PTR_ERR(plat_dat);
plat_dat->init = anarion_gmac_init;
plat_dat->exit = anarion_gmac_exit;
anarion_gmac_init(pdev, gmac);
plat_dat->bsp_priv = gmac;
ret = stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res);
if (ret) {
stmmac_remove_config_dt(pdev, plat_dat);
return ret;
}
return 0;
}
static const struct of_device_id anarion_dwmac_match[] = {
{ .compatible = "adaptrum,anarion-gmac" },
{ }
};
MODULE_DEVICE_TABLE(of, anarion_dwmac_match);
static struct platform_driver anarion_dwmac_driver = {
.probe = anarion_dwmac_probe,
.remove = stmmac_pltfr_remove,
.driver = {
.name = "anarion-dwmac",
.pm = &stmmac_pltfr_pm_ops,
.of_match_table = anarion_dwmac_match,
},
};
module_platform_driver(anarion_dwmac_driver);
MODULE_DESCRIPTION("Adaptrum Anarion DWMAC specific glue layer");
MODULE_AUTHOR("Alexandru Gagniuc <mr.nuke.me@gmail.com>");
MODULE_LICENSE("GPL v2");