Detect the link status on the w5500 using the Link Status bit from the PHY Configuration register (PHYCFGR). On w5100/w5200, which lack this register, the link is always reported as up. Also add an optional interrupt, wired to the LINKLED pin of the w5500, to detect link status changes and read the PHYCFGR register. Commit dacf281771a9 ("w5100: remove unused gpio link detection") dropped the link_gpio/link_irq handling on the grounds that no devicetree user passed a "link" gpio at the time and that it used the old gpio interface. This isn't a plain revert of that removal: link detection is now done using a second interrupt rather than a gpio with a documented DT binding. Signed-off-by: Arthur Crépin Leblond --- drivers/net/ethernet/wiznet/w5100.c | 68 +++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/drivers/net/ethernet/wiznet/w5100.c b/drivers/net/ethernet/wiznet/w5100.c index 53d8dc642fbd..ca5054c20993 100644 --- a/drivers/net/ethernet/wiznet/w5100.c +++ b/drivers/net/ethernet/wiznet/w5100.c @@ -21,6 +21,7 @@ #include #include #include +#include #include #include "w5100.h" @@ -124,6 +125,8 @@ MODULE_LICENSE("GPL"); */ #define W5500_SIMR 0x0018 /* Socket Interrupt Mask Register */ #define W5500_RTR 0x0019 /* Retry Time-value Register */ +#define W5500_PHYCFGR 0x002e /* PHY Configuration Register */ +#define PHYCFGR_LNK 0x01 /* link status */ #define W5500_S0_REGS 0x10000 @@ -154,6 +157,7 @@ struct w5100_priv { u16 s0_rx_buf_size; int irq; + int link_irq; struct napi_struct napi; struct net_device *ndev; @@ -414,6 +418,20 @@ static void w5100_get_drvinfo(struct net_device *ndev, sizeof(info->bus_info)); } +static u32 w5100_get_link(struct net_device *ndev) +{ + struct w5100_priv *priv = netdev_priv(ndev); + + if (priv->ops->chip_id == W5500) { + int ret = w5100_read(priv, W5500_PHYCFGR); + + if (ret >= 0) + return ret & PHYCFGR_LNK; + } + + return 1; +} + static u32 w5100_get_msglevel(struct net_device *ndev) { struct w5100_priv *priv = netdev_priv(ndev); @@ -616,6 +634,24 @@ static irqreturn_t w5100_interrupt(int irq, void *ndev_instance) return IRQ_HANDLED; } +static irqreturn_t w5100_detect_link(int irq, void *ndev_instance) +{ + struct net_device *ndev = ndev_instance; + struct w5100_priv *priv = netdev_priv(ndev); + + if (netif_running(ndev)) { + if (w5100_get_link(ndev)) { + netif_info(priv, link, ndev, "link is up\n"); + netif_carrier_on(ndev); + } else { + netif_info(priv, link, ndev, "link is down\n"); + netif_carrier_off(ndev); + } + } + + return IRQ_HANDLED; +} + static void w5100_setrx_work(struct work_struct *work) { struct w5100_priv *priv = container_of(work, struct w5100_priv, @@ -659,6 +695,10 @@ static int w5100_open(struct net_device *ndev) w5100_hw_start(priv); napi_enable(&priv->napi); netif_start_queue(ndev); + + if (priv->link_irq < 0 || w5100_get_link(ndev)) + netif_carrier_on(ndev); + return 0; } @@ -678,6 +718,7 @@ static const struct ethtool_ops w5100_ethtool_ops = { .get_drvinfo = w5100_get_drvinfo, .get_msglevel = w5100_get_msglevel, .set_msglevel = w5100_set_msglevel, + .get_link = w5100_get_link, .get_regs_len = w5100_get_regs_len, .get_regs = w5100_get_regs, }; @@ -751,6 +792,8 @@ int w5100_probe(struct device *dev, const struct w5100_ops *ops, priv->ndev = ndev; priv->ops = ops; priv->irq = irq; + priv->link_irq = ops->chip_id == W5500 ? + fwnode_irq_get(dev_fwnode(dev), 1) : -ENODEV; ndev->netdev_ops = &w5100_netdev_ops; ndev->ethtool_ops = &w5100_ethtool_ops; @@ -803,8 +846,21 @@ int w5100_probe(struct device *dev, const struct w5100_ops *ops, if (err) goto err_hw; + if (priv->link_irq >= 0) { + err = request_threaded_irq(priv->link_irq, NULL, + w5100_detect_link, + IRQF_TRIGGER_RISING | + IRQF_TRIGGER_FALLING | + IRQF_ONESHOT, + "w5100-link", priv->ndev); + if (err < 0) + goto err_link_irq; + } + return 0; +err_link_irq: + free_irq(priv->irq, ndev); err_hw: destroy_workqueue(priv->xfer_wq); err_wq: @@ -820,6 +876,9 @@ void w5100_remove(struct device *dev) struct net_device *ndev = dev_get_drvdata(dev); struct w5100_priv *priv = netdev_priv(ndev); + if (priv->link_irq >= 0) + free_irq(priv->link_irq, ndev); + w5100_hw_reset(priv); free_irq(priv->irq, ndev); @@ -842,6 +901,9 @@ static int w5100_suspend(struct device *dev) netif_carrier_off(ndev); netif_device_detach(ndev); + if (priv->link_irq >= 0) + disable_irq(priv->link_irq); + w5100_hw_close(priv); } return 0; @@ -856,7 +918,13 @@ static int w5100_resume(struct device *dev) w5100_hw_reset(priv); w5100_hw_start(priv); + if (priv->link_irq >= 0) + enable_irq(priv->link_irq); + netif_device_attach(ndev); + + if (priv->link_irq < 0 || w5100_get_link(ndev)) + netif_carrier_on(ndev); } return 0; } -- 2.55.0