Commit 2152e7a2 authored by Claudiu Manoil's avatar Claudiu Manoil Committed by David S. Miller

enetc: Clean up local mdio bus allocation

What's needed is basically a pointer to the mdio registers.
This is one way to store it inside bus->priv allocated space,
without upsetting sparse.
Reworked accessors to avoid __iomem casting.
Used devm_* variant to further clean up the init error /
remove paths.

Fixes following sparse warning:
 warning: incorrect type in assignment (different address spaces)
    expected void *priv
    got struct enetc_mdio_regs [noderef] <asn:2>*[assigned] regs

Fixes: ebfcb23d ("enetc: Add ENETC PF level external MDIO support")
Signed-off-by: default avatarClaudiu Manoil <claudiu.manoil@nxp.com>
Reviewed-by: default avatarAndrew Lunn <andrew@lunn.ch>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 4a986617
...@@ -8,16 +8,22 @@ ...@@ -8,16 +8,22 @@
#include "enetc_pf.h" #include "enetc_pf.h"
struct enetc_mdio_regs { #define ENETC_MDIO_REG_OFFSET 0x1c00
u32 mdio_cfg; /* MDIO configuration and status */ #define ENETC_MDIO_CFG 0x0 /* MDIO configuration and status */
u32 mdio_ctl; /* MDIO control */ #define ENETC_MDIO_CTL 0x4 /* MDIO control */
u32 mdio_data; /* MDIO data */ #define ENETC_MDIO_DATA 0x8 /* MDIO data */
u32 mdio_addr; /* MDIO address */ #define ENETC_MDIO_ADDR 0xc /* MDIO address */
#define enetc_mdio_rd(hw, off) \
enetc_port_rd(hw, ENETC_##off + ENETC_MDIO_REG_OFFSET)
#define enetc_mdio_wr(hw, off, val) \
enetc_port_wr(hw, ENETC_##off + ENETC_MDIO_REG_OFFSET, val)
#define enetc_mdio_rd_reg(off) enetc_mdio_rd(hw, off)
struct enetc_mdio_priv {
struct enetc_hw *hw;
}; };
#define bus_to_enetc_regs(bus) (struct enetc_mdio_regs __iomem *)((bus)->priv)
#define ENETC_MDIO_REG_OFFSET 0x1c00
#define ENETC_MDC_DIV 258 #define ENETC_MDC_DIV 258
#define MDIO_CFG_CLKDIV(x) ((((x) >> 1) & 0xff) << 8) #define MDIO_CFG_CLKDIV(x) ((((x) >> 1) & 0xff) << 8)
...@@ -33,18 +39,19 @@ struct enetc_mdio_regs { ...@@ -33,18 +39,19 @@ struct enetc_mdio_regs {
#define MDIO_DATA(x) ((x) & 0xffff) #define MDIO_DATA(x) ((x) & 0xffff)
#define TIMEOUT 1000 #define TIMEOUT 1000
static int enetc_mdio_wait_complete(struct enetc_mdio_regs __iomem *regs) static int enetc_mdio_wait_complete(struct enetc_hw *hw)
{ {
u32 val; u32 val;
return readx_poll_timeout(enetc_rd_reg, &regs->mdio_cfg, val, return readx_poll_timeout(enetc_mdio_rd_reg, MDIO_CFG, val,
!(val & MDIO_CFG_BSY), 10, 10 * TIMEOUT); !(val & MDIO_CFG_BSY), 10, 10 * TIMEOUT);
} }
static int enetc_mdio_write(struct mii_bus *bus, int phy_id, int regnum, static int enetc_mdio_write(struct mii_bus *bus, int phy_id, int regnum,
u16 value) u16 value)
{ {
struct enetc_mdio_regs __iomem *regs = bus_to_enetc_regs(bus); struct enetc_mdio_priv *mdio_priv = bus->priv;
struct enetc_hw *hw = mdio_priv->hw;
u32 mdio_ctl, mdio_cfg; u32 mdio_ctl, mdio_cfg;
u16 dev_addr; u16 dev_addr;
int ret; int ret;
...@@ -59,29 +66,29 @@ static int enetc_mdio_write(struct mii_bus *bus, int phy_id, int regnum, ...@@ -59,29 +66,29 @@ static int enetc_mdio_write(struct mii_bus *bus, int phy_id, int regnum,
mdio_cfg &= ~MDIO_CFG_ENC45; mdio_cfg &= ~MDIO_CFG_ENC45;
} }
enetc_wr_reg(&regs->mdio_cfg, mdio_cfg); enetc_mdio_wr(hw, MDIO_CFG, mdio_cfg);
ret = enetc_mdio_wait_complete(regs); ret = enetc_mdio_wait_complete(hw);
if (ret) if (ret)
return ret; return ret;
/* set port and dev addr */ /* set port and dev addr */
mdio_ctl = MDIO_CTL_PORT_ADDR(phy_id) | MDIO_CTL_DEV_ADDR(dev_addr); mdio_ctl = MDIO_CTL_PORT_ADDR(phy_id) | MDIO_CTL_DEV_ADDR(dev_addr);
enetc_wr_reg(&regs->mdio_ctl, mdio_ctl); enetc_mdio_wr(hw, MDIO_CTL, mdio_ctl);
/* set the register address */ /* set the register address */
if (regnum & MII_ADDR_C45) { if (regnum & MII_ADDR_C45) {
enetc_wr_reg(&regs->mdio_addr, regnum & 0xffff); enetc_mdio_wr(hw, MDIO_ADDR, regnum & 0xffff);
ret = enetc_mdio_wait_complete(regs); ret = enetc_mdio_wait_complete(hw);
if (ret) if (ret)
return ret; return ret;
} }
/* write the value */ /* write the value */
enetc_wr_reg(&regs->mdio_data, MDIO_DATA(value)); enetc_mdio_wr(hw, MDIO_DATA, MDIO_DATA(value));
ret = enetc_mdio_wait_complete(regs); ret = enetc_mdio_wait_complete(hw);
if (ret) if (ret)
return ret; return ret;
...@@ -90,7 +97,8 @@ static int enetc_mdio_write(struct mii_bus *bus, int phy_id, int regnum, ...@@ -90,7 +97,8 @@ static int enetc_mdio_write(struct mii_bus *bus, int phy_id, int regnum,
static int enetc_mdio_read(struct mii_bus *bus, int phy_id, int regnum) static int enetc_mdio_read(struct mii_bus *bus, int phy_id, int regnum)
{ {
struct enetc_mdio_regs __iomem *regs = bus_to_enetc_regs(bus); struct enetc_mdio_priv *mdio_priv = bus->priv;
struct enetc_hw *hw = mdio_priv->hw;
u32 mdio_ctl, mdio_cfg; u32 mdio_ctl, mdio_cfg;
u16 dev_addr, value; u16 dev_addr, value;
int ret; int ret;
...@@ -104,41 +112,41 @@ static int enetc_mdio_read(struct mii_bus *bus, int phy_id, int regnum) ...@@ -104,41 +112,41 @@ static int enetc_mdio_read(struct mii_bus *bus, int phy_id, int regnum)
mdio_cfg &= ~MDIO_CFG_ENC45; mdio_cfg &= ~MDIO_CFG_ENC45;
} }
enetc_wr_reg(&regs->mdio_cfg, mdio_cfg); enetc_mdio_wr(hw, MDIO_CFG, mdio_cfg);
ret = enetc_mdio_wait_complete(regs); ret = enetc_mdio_wait_complete(hw);
if (ret) if (ret)
return ret; return ret;
/* set port and device addr */ /* set port and device addr */
mdio_ctl = MDIO_CTL_PORT_ADDR(phy_id) | MDIO_CTL_DEV_ADDR(dev_addr); mdio_ctl = MDIO_CTL_PORT_ADDR(phy_id) | MDIO_CTL_DEV_ADDR(dev_addr);
enetc_wr_reg(&regs->mdio_ctl, mdio_ctl); enetc_mdio_wr(hw, MDIO_CTL, mdio_ctl);
/* set the register address */ /* set the register address */
if (regnum & MII_ADDR_C45) { if (regnum & MII_ADDR_C45) {
enetc_wr_reg(&regs->mdio_addr, regnum & 0xffff); enetc_mdio_wr(hw, MDIO_ADDR, regnum & 0xffff);
ret = enetc_mdio_wait_complete(regs); ret = enetc_mdio_wait_complete(hw);
if (ret) if (ret)
return ret; return ret;
} }
/* initiate the read */ /* initiate the read */
enetc_wr_reg(&regs->mdio_ctl, mdio_ctl | MDIO_CTL_READ); enetc_mdio_wr(hw, MDIO_CTL, mdio_ctl | MDIO_CTL_READ);
ret = enetc_mdio_wait_complete(regs); ret = enetc_mdio_wait_complete(hw);
if (ret) if (ret)
return ret; return ret;
/* return all Fs if nothing was there */ /* return all Fs if nothing was there */
if (enetc_rd_reg(&regs->mdio_cfg) & MDIO_CFG_RD_ER) { if (enetc_mdio_rd(hw, MDIO_CFG) & MDIO_CFG_RD_ER) {
dev_dbg(&bus->dev, dev_dbg(&bus->dev,
"Error while reading PHY%d reg at %d.%hhu\n", "Error while reading PHY%d reg at %d.%hhu\n",
phy_id, dev_addr, regnum); phy_id, dev_addr, regnum);
return 0xffff; return 0xffff;
} }
value = enetc_rd_reg(&regs->mdio_data) & 0xffff; value = enetc_mdio_rd(hw, MDIO_DATA) & 0xffff;
return value; return value;
} }
...@@ -146,12 +154,12 @@ static int enetc_mdio_read(struct mii_bus *bus, int phy_id, int regnum) ...@@ -146,12 +154,12 @@ static int enetc_mdio_read(struct mii_bus *bus, int phy_id, int regnum)
int enetc_mdio_probe(struct enetc_pf *pf) int enetc_mdio_probe(struct enetc_pf *pf)
{ {
struct device *dev = &pf->si->pdev->dev; struct device *dev = &pf->si->pdev->dev;
struct enetc_mdio_regs __iomem *regs; struct enetc_mdio_priv *mdio_priv;
struct device_node *np; struct device_node *np;
struct mii_bus *bus; struct mii_bus *bus;
int ret; int err;
bus = mdiobus_alloc_size(sizeof(regs)); bus = devm_mdiobus_alloc_size(dev, sizeof(*mdio_priv));
if (!bus) if (!bus)
return -ENOMEM; return -ENOMEM;
...@@ -159,41 +167,31 @@ int enetc_mdio_probe(struct enetc_pf *pf) ...@@ -159,41 +167,31 @@ int enetc_mdio_probe(struct enetc_pf *pf)
bus->read = enetc_mdio_read; bus->read = enetc_mdio_read;
bus->write = enetc_mdio_write; bus->write = enetc_mdio_write;
bus->parent = dev; bus->parent = dev;
mdio_priv = bus->priv;
mdio_priv->hw = &pf->si->hw;
snprintf(bus->id, MII_BUS_ID_SIZE, "%s", dev_name(dev)); snprintf(bus->id, MII_BUS_ID_SIZE, "%s", dev_name(dev));
/* store the enetc mdio base address for this bus */
regs = pf->si->hw.port + ENETC_MDIO_REG_OFFSET;
bus->priv = regs;
np = of_get_child_by_name(dev->of_node, "mdio"); np = of_get_child_by_name(dev->of_node, "mdio");
if (!np) { if (!np) {
dev_err(dev, "MDIO node missing\n"); dev_err(dev, "MDIO node missing\n");
ret = -EINVAL; return -EINVAL;
goto err_registration;
} }
ret = of_mdiobus_register(bus, np); err = of_mdiobus_register(bus, np);
if (ret) { if (err) {
of_node_put(np); of_node_put(np);
dev_err(dev, "cannot register MDIO bus\n"); dev_err(dev, "cannot register MDIO bus\n");
goto err_registration; return err;
} }
of_node_put(np); of_node_put(np);
pf->mdio = bus; pf->mdio = bus;
return 0; return 0;
err_registration:
mdiobus_free(bus);
return ret;
} }
void enetc_mdio_remove(struct enetc_pf *pf) void enetc_mdio_remove(struct enetc_pf *pf)
{ {
if (pf->mdio) { if (pf->mdio)
mdiobus_unregister(pf->mdio); mdiobus_unregister(pf->mdio);
mdiobus_free(pf->mdio);
}
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment