During PHY probing and configuration complex configuration sequences might be issued and firmware might be loaded. Hardware polling can interfere badly with that. E.g. a hardware polling MMD c45 over c22 request might break an ongoing firmware loading sequence. To avoid such issues the polling of the Realtek Otto switches can be (de)activated with one or two 32 bit mask registers. Each bit enables (=1) or disables (=0) the polling of the corresponding port. Make use of this as follows: - Disable polling for all ports when the MDIO driver starts. - Reenable polling just after the PHY has been attached. - Disable polling just before the PHY is being detached. The different devices will need an individual polling setup. For this provide two callbacks that will be used later for coding similar to [1] or [2]. - init_polling(): After polling has been disabled during probing. - tune_polling(): Before polling gets reactivated for one PHY. This synchronizes the kernel and hardware polling to some extent. It gracefully handles deferred probing of PHYs in case the driver is loaded asynchronously during boot. Additionally it brings the hardware polling into a consistent operation mode for devices where U-Boot does not take care. [1] https://github.com/openwrt/openwrt/blob/main/target/linux/realtek/files-6.18/drivers/net/mdio/mdio-realtek-otto.c#L818 [2] https://lore.kernel.org/netdev/680696024a8648535ce6dee771fe4de67802e0e8.1769053496.git.daniel@makrotopia.org/ Signed-off-by: Markus Stockhausen --- drivers/net/mdio/mdio-realtek-rtl9300.c | 87 +++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/drivers/net/mdio/mdio-realtek-rtl9300.c b/drivers/net/mdio/mdio-realtek-rtl9300.c index 616edcde15d9..a8e9a497a0dc 100644 --- a/drivers/net/mdio/mdio-realtek-rtl9300.c +++ b/drivers/net/mdio/mdio-realtek-rtl9300.c @@ -137,6 +137,7 @@ #define RTL9300_PHY_CTRL_INDATA GENMASK(31, 16) #define RTL9300_PHY_CTRL_DATA GENMASK(15, 0) #define RTL9300_SMI_ACCESS_PHY_CTRL_3 0xcb7c +#define RTL9300_SMI_POLL_CTRL 0xca90 #define RTL9300_SMI_PORT0_5_ADDR_CTRL 0xcb80 #define RTL9310_NUM_BUSES 4 @@ -162,6 +163,7 @@ #define RTL9310_PHY_CTRL_INDATA GENMASK(15, 0) #define RTL9310_SMI_INDRT_ACCESS_MMD_CTRL 0x0c18 #define RTL9310_SMI_PORT_ADDR_CTRL 0x0c74 +#define RTL9310_SMI_PORT_POLLING_CTRL 0x0ccc #define RTL9310_SMI_PORT_POLLING_SEL 0x0c9c #define PHY_CTRL_CMD BIT(0) @@ -192,6 +194,7 @@ struct otto_emdio_priv { const struct otto_emdio_info *info; struct regmap *regmap; struct mutex lock; /* protect HW access */ + DECLARE_BITMAP(phy_poll, MAX_PORTS); DECLARE_BITMAP(valid_ports, MAX_PORTS); u16 page[MAX_PORTS]; u8 smi_bus[MAX_PORTS]; @@ -210,6 +213,9 @@ struct otto_emdio_info { u8 num_buses; u8 num_ports; u16 num_pages; + u32 poll_ctrl; + int (*init_polling)(int port); + int (*tune_polling)(struct phy_device *phydev); int (*setup_controller)(struct otto_emdio_priv *priv); int (*read_c22)(struct mii_bus *bus, int port, int regnum, u32 *value); int (*read_c45)(struct mii_bus *bus, int port, int dev_addr, int regnum, u32 *value); @@ -245,6 +251,14 @@ static struct otto_emdio_priv *otto_emdio_bus_to_priv(struct mii_bus *bus) return chan->priv; } +static int otto_emdio_set_port_polling(struct otto_emdio_priv *priv, int port, bool active) +{ + lockdep_assert_held(&priv->lock); + + return regmap_assign_bits(priv->regmap, priv->info->poll_ctrl + (port / 32) * 4, + BIT(port % 32), active); +} + static int otto_emdio_run_cmd(struct mii_bus *bus, u32 cmd, struct otto_emdio_cmd_regs *cmd_data) { @@ -588,6 +602,49 @@ static int otto_emdio_9310_setup_controller(struct otto_emdio_priv *priv) return 0; } +static int otto_emdio_notify_phy_attach(struct phy_device *phydev) +{ + struct otto_emdio_priv *priv = otto_emdio_bus_to_priv(phydev->mdio.bus); + int port = otto_emdio_phy_to_port(phydev->mdio.bus, phydev->mdio.addr); + int ret; + + if (port < 0) + return port; + + if (test_bit(port, priv->phy_poll)) + return 0; + + scoped_guard(mutex, &priv->lock) { + if (priv->info->tune_polling) { + ret = priv->info->tune_polling(phydev); + if (ret) + return ret; + } + + ret = otto_emdio_set_port_polling(priv, port, true); + if (!ret) + __set_bit(port, priv->phy_poll); + } + + return ret; +} + +static void otto_emdio_notify_phy_detach(struct phy_device *phydev) +{ + struct otto_emdio_priv *priv = otto_emdio_bus_to_priv(phydev->mdio.bus); + int port = otto_emdio_phy_to_port(phydev->mdio.bus, phydev->mdio.addr); + struct mii_bus *bus = phydev->mdio.bus; + + if (port < 0) + return; + + scoped_guard(mutex, &priv->lock) { + __clear_bit(port, priv->phy_poll); + if (otto_emdio_set_port_polling(priv, port, false)) + dev_err(bus->parent, "failed to disable polling for port %d\n", port); + } +} + static int otto_emdio_probe_one(struct device *dev, struct otto_emdio_priv *priv, struct fwnode_handle *node) { @@ -617,6 +674,9 @@ static int otto_emdio_probe_one(struct device *dev, struct otto_emdio_priv *priv bus->write = otto_emdio_write_c22; } bus->parent = dev; + bus->notify_phy_attach = otto_emdio_notify_phy_attach; + bus->notify_phy_detach = otto_emdio_notify_phy_detach; + chan = bus->priv; chan->mdio_bus = mdio_bus; chan->priv = priv; @@ -733,6 +793,27 @@ static int otto_emdio_map_ports(struct device *dev) return err; } +static int otto_emdio_init_polling(struct otto_emdio_priv *priv) +{ + int err; + + scoped_guard(mutex, &priv->lock) { + for (int port = 0; port < priv->info->num_ports; port++) { + err = otto_emdio_set_port_polling(priv, port, false); + if (err) + return err; + + if (priv->info->init_polling) { + err = priv->info->init_polling(port); + if (err) + return err; + } + } + } + + return 0; +} + static int otto_emdio_probe(struct platform_device *pdev) { struct device *dev = &pdev->dev; @@ -752,6 +833,10 @@ static int otto_emdio_probe(struct platform_device *pdev) if (IS_ERR(priv->regmap)) return PTR_ERR(priv->regmap); + err = otto_emdio_init_polling(priv); + if (err) + return err; + platform_set_drvdata(pdev, priv); err = otto_emdio_map_ports(dev); @@ -792,6 +877,7 @@ static const struct otto_emdio_info otto_emdio_9300_info = { .num_buses = RTL9300_NUM_BUSES, .num_ports = RTL9300_NUM_PORTS, .num_pages = RTL9300_NUM_PAGES, + .poll_ctrl = RTL9300_SMI_POLL_CTRL, .setup_controller = otto_emdio_9300_setup_controller, .read_c22 = otto_emdio_9300_read_c22, .read_c45 = otto_emdio_9300_read_c45, @@ -817,6 +903,7 @@ static const struct otto_emdio_info otto_emdio_9310_info = { .num_buses = RTL9310_NUM_BUSES, .num_pages = RTL9310_NUM_PAGES, .num_ports = RTL9310_NUM_PORTS, + .poll_ctrl = RTL9310_SMI_PORT_POLLING_CTRL, .setup_controller = otto_emdio_9310_setup_controller, .read_c22 = otto_emdio_9310_read_c22, .read_c45 = otto_emdio_9310_read_c45, -- 2.54.0