[net-next v5 5/7] net: dsa: mediatek: support MDIO switch downstream of MMIO switch
From: Caleb James DeLisle
Date: Mon Nov 16 2026 - 13:35:11 EST
The MT7530 / MT7531 can connect via either MDIO or MMIO. In MMIO mode
the switch provides its own MDIO bus which connects to its integrated
PHYs, but also can be connected to other things, such as another
switch.
If the MDIO bus contains another switch, then the upstream switch
will register but it will not setup the DSA tree because the
downstream switch is missing. Without the complete DSA tree,
mt753x_setup() will not be called on the switch and therefore
mt7530_setup_mdio() is never called so the downstream switch never
registers.
However, the call to mt7530_setup_mdio() from mt753x_setup() must
also be kept because all PHYs are expected to be operational after
mt753x_setup() returns.
1. Move TOP_SIG_CTRL_NORMAL bit set to mt7530_setup() so it will only
be called after switch reset, make it unconditional since mt7530_setup
is MT7530 only.
2. Move mt7530_setup_irq() to mt7530_setup_mdio() so it can be called
early.
3. Make mt7530_setup_mdio() able to be called a second time without
side-effects.
4. Call mt7530_setup_mdio() early from mt7988_probe() if "mdio" child
DT node is present and dsa_register_switch() returns 0 (not all
switches probed yet)
5. Add lockdep classes to mutex because MDIO switch under MMIO switch
will cause one lock to be taken after the other.
Signed-off-by: Caleb James DeLisle <cjd@xxxxxxxx>
---
drivers/net/dsa/mt7530-mmio.c | 23 ++++++++++++++++++-
drivers/net/dsa/mt7530.c | 42 ++++++++++++++++++++++++++---------
drivers/net/dsa/mt7530.h | 10 +++++++++
3 files changed, 64 insertions(+), 11 deletions(-)
diff --git a/drivers/net/dsa/mt7530-mmio.c b/drivers/net/dsa/mt7530-mmio.c
index 5d467be2a32c..ab6d92ab3ec0 100644
--- a/drivers/net/dsa/mt7530-mmio.c
+++ b/drivers/net/dsa/mt7530-mmio.c
@@ -30,6 +30,7 @@ static int
mt7988_probe(struct platform_device *pdev)
{
struct mt7530_priv *priv;
+ struct device_node *mnp;
void __iomem *base_addr;
int ret;
@@ -61,7 +62,27 @@ mt7988_probe(struct platform_device *pdev)
if (IS_ERR(priv->regmap))
return PTR_ERR(priv->regmap);
- return dsa_register_switch(priv->ds);
+ ret = dsa_register_switch(priv->ds);
+
+ if (ret)
+ return ret;
+
+ /*
+ * If not all switches in the hierarchy have been probed,
+ * dsa_register_switch() returns 0 without calling mt753x_setup().
+ * Call mt7530_setup_mdio() because there's a chance that the missing
+ * switch is on the MDIO bus. If "mdio" node is not present then there
+ * is definitely no MDIO switch in the DT, and also if "mdio" is not
+ * present then phys_mii_mask is relevant and that is not set until
+ * dsa_switch_setup.
+ */
+ mnp = of_get_child_by_name(pdev->dev.of_node, "mdio");
+ if (mnp) {
+ of_node_put(mnp);
+ ret = mt7530_setup_mdio(priv);
+ }
+
+ return ret;
}
static void mt7988_remove(struct platform_device *pdev)
diff --git a/drivers/net/dsa/mt7530.c b/drivers/net/dsa/mt7530.c
index 53ac692d5ba9..4390982a5e22 100644
--- a/drivers/net/dsa/mt7530.c
+++ b/drivers/net/dsa/mt7530.c
@@ -2497,11 +2497,6 @@ mt7530_setup_irq(struct mt7530_priv *priv)
return irq ? : -EINVAL;
}
- /* This register must be set for MT7530 to properly fire interrupts */
- if (priv->id == ID_MT7530 || priv->id == ID_MT7621)
- regmap_set_bits(priv->regmap, MT7530_TOP_SIG_CTRL,
- TOP_SIG_CTRL_NORMAL);
-
ret = devm_regmap_add_irq_chip_fwnode(dev, dev_fwnode(dev),
priv->regmap, irq,
IRQF_ONESHOT,
@@ -2530,7 +2525,7 @@ mt7530_free_mdio_irq(struct mt7530_priv *priv)
}
}
-static int
+int
mt7530_setup_mdio(struct mt7530_priv *priv)
{
struct device_node *mnp, *np = priv->dev->of_node;
@@ -2540,6 +2535,14 @@ mt7530_setup_mdio(struct mt7530_priv *priv)
static int idx;
int ret = 0;
+ /* Already done */
+ if (priv->child_bus)
+ return 0;
+
+ ret = mt7530_setup_irq(priv);
+ if (ret)
+ return ret;
+
mnp = of_get_child_by_name(np, "mdio");
if (mnp && !of_device_is_available(mnp))
@@ -2581,6 +2584,7 @@ mt7530_setup_mdio(struct mt7530_priv *priv)
of_node_put(mnp);
return ret;
}
+EXPORT_SYMBOL_GPL(mt7530_setup_mdio);
static int
mt7530_setup(struct dsa_switch *ds)
@@ -2684,6 +2688,10 @@ mt7530_setup(struct dsa_switch *ds)
MT7530_CHG_TRAP | MT7530_PHY_INDIRECT_ACCESS,
MT7530_CHG_TRAP);
+ /* This register must be set for MT7530 to properly fire interrupts */
+ regmap_set_bits(priv->regmap, MT7530_TOP_SIG_CTRL,
+ TOP_SIG_CTRL_NORMAL);
+
if ((val & MT7530_XTAL_MASK) == MT7530_XTAL_40MHZ)
mt7530_pll_setup(priv);
@@ -3405,10 +3413,10 @@ mt753x_setup(struct dsa_switch *ds)
if (ret)
return ret;
- ret = mt7530_setup_irq(priv);
- if (ret)
- return ret;
-
+ /*
+ * This must be called before mt753x_setup() returns because the DSA
+ * core synchronously sets up ports and expects PHYs to be operational.
+ */
ret = mt7530_setup_mdio(priv);
if (ret)
return ret;
@@ -3834,6 +3842,15 @@ const struct mt753x_info mt753x_table[] = {
};
EXPORT_SYMBOL_GPL(mt753x_table);
+/*
+ * An MMIO switch can have another MDIO switch connected to its
+ * MDIO interface and in that case, actions on the MDIO switch will call
+ * through to mt7531_ind_c(22|45)_phy_(read|write) making a lock
+ * dependency.
+ */
+static struct lock_class_key mt7530_mmio_mutex_key;
+static struct lock_class_key mt7530_mdio_mutex_key;
+
int
mt7530_probe_common(struct mt7530_priv *priv)
{
@@ -3873,6 +3890,11 @@ mt7530_probe_common(struct mt7530_priv *priv)
spin_lock_init(&priv->stats_lock);
INIT_DELAYED_WORK(&priv->stats_work, mt7530_stats_poll);
+ if (priv->bus)
+ lockdep_set_class(&priv->reg_mutex, &mt7530_mdio_mutex_key);
+ else
+ lockdep_set_class(&priv->reg_mutex, &mt7530_mmio_mutex_key);
+
dev_set_drvdata(dev, priv);
return 0;
diff --git a/drivers/net/dsa/mt7530.h b/drivers/net/dsa/mt7530.h
index 5b7394f6383c..c86bc4bca29b 100644
--- a/drivers/net/dsa/mt7530.h
+++ b/drivers/net/dsa/mt7530.h
@@ -969,6 +969,16 @@ struct mt7530_hw_stats {
int mt7530_probe_common(struct mt7530_priv *priv);
void mt7530_remove_common(struct mt7530_priv *priv);
+/*
+ * When an MMIO based switch has an MDIO bus which contains a downstream MDIO
+ * switch, the MDIO bus is not normally setup until mt753x_setup() has been
+ * called. But mt753x_setup() is not called until DSA sees that every switch in
+ * the tree has been probed. Calling mt7530_setup_mdio() after at the end of
+ * the MMIO probe function allows the downstream switch to register, completing
+ * the tree.
+ */
+int mt7530_setup_mdio(struct mt7530_priv *priv);
+
extern const struct mt753x_info mt753x_table[];
#endif /* __MT7530_H */
--
2.39.5