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>
105 lines
2.3 KiB
C
105 lines
2.3 KiB
C
// SPDX-License-Identifier: GPL-2.0-only
|
|
/*
|
|
* Amlogic Meson6 and Meson8 DWMAC glue layer
|
|
*
|
|
* Copyright (C) 2014 Beniamino Galvani <b.galvani@gmail.com>
|
|
*/
|
|
|
|
#include <linux/device.h>
|
|
#include <linux/ethtool.h>
|
|
#include <linux/io.h>
|
|
#include <linux/ioport.h>
|
|
#include <linux/module.h>
|
|
#include <linux/platform_device.h>
|
|
#include <linux/stmmac.h>
|
|
|
|
#include "stmmac_platform.h"
|
|
|
|
#define ETHMAC_SPEED_100 BIT(1)
|
|
|
|
struct meson_dwmac {
|
|
struct device *dev;
|
|
void __iomem *reg;
|
|
};
|
|
|
|
static void meson6_dwmac_fix_mac_speed(void *priv, unsigned int speed)
|
|
{
|
|
struct meson_dwmac *dwmac = priv;
|
|
unsigned int val;
|
|
|
|
val = readl(dwmac->reg);
|
|
|
|
switch (speed) {
|
|
case SPEED_10:
|
|
val &= ~ETHMAC_SPEED_100;
|
|
break;
|
|
case SPEED_100:
|
|
val |= ETHMAC_SPEED_100;
|
|
break;
|
|
}
|
|
|
|
writel(val, dwmac->reg);
|
|
}
|
|
|
|
static int meson6_dwmac_probe(struct platform_device *pdev)
|
|
{
|
|
struct plat_stmmacenet_data *plat_dat;
|
|
struct stmmac_resources stmmac_res;
|
|
struct meson_dwmac *dwmac;
|
|
int ret;
|
|
|
|
ret = stmmac_get_platform_resources(pdev, &stmmac_res);
|
|
if (ret)
|
|
return ret;
|
|
|
|
plat_dat = stmmac_probe_config_dt(pdev, stmmac_res.mac);
|
|
if (IS_ERR(plat_dat))
|
|
return PTR_ERR(plat_dat);
|
|
|
|
dwmac = devm_kzalloc(&pdev->dev, sizeof(*dwmac), GFP_KERNEL);
|
|
if (!dwmac) {
|
|
ret = -ENOMEM;
|
|
goto err_remove_config_dt;
|
|
}
|
|
|
|
dwmac->reg = devm_platform_ioremap_resource(pdev, 1);
|
|
if (IS_ERR(dwmac->reg)) {
|
|
ret = PTR_ERR(dwmac->reg);
|
|
goto err_remove_config_dt;
|
|
}
|
|
|
|
plat_dat->bsp_priv = dwmac;
|
|
plat_dat->fix_mac_speed = meson6_dwmac_fix_mac_speed;
|
|
|
|
ret = stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res);
|
|
if (ret)
|
|
goto err_remove_config_dt;
|
|
|
|
return 0;
|
|
|
|
err_remove_config_dt:
|
|
stmmac_remove_config_dt(pdev, plat_dat);
|
|
|
|
return ret;
|
|
}
|
|
|
|
static const struct of_device_id meson6_dwmac_match[] = {
|
|
{ .compatible = "amlogic,meson6-dwmac" },
|
|
{ }
|
|
};
|
|
MODULE_DEVICE_TABLE(of, meson6_dwmac_match);
|
|
|
|
static struct platform_driver meson6_dwmac_driver = {
|
|
.probe = meson6_dwmac_probe,
|
|
.remove = stmmac_pltfr_remove,
|
|
.driver = {
|
|
.name = "meson6-dwmac",
|
|
.pm = &stmmac_pltfr_pm_ops,
|
|
.of_match_table = meson6_dwmac_match,
|
|
},
|
|
};
|
|
module_platform_driver(meson6_dwmac_driver);
|
|
|
|
MODULE_AUTHOR("Beniamino Galvani <b.galvani@gmail.com>");
|
|
MODULE_DESCRIPTION("Amlogic Meson6 and Meson8 DWMAC glue layer");
|
|
MODULE_LICENSE("GPL v2");
|