[PATCH net-next v3 04/10] net: macb: rework usrio refclk selection code

Conor Dooley
2026-03-10T17:19:17Z

The USRIO based refclk selection code abuses a capability flag to set
the refclk to an external source based on match data/compatible on
sama7g5-emac and use an internal source for the gmac.

Ryan previously added a property in an attempt to decouple the refclk
source from the compatible, because this is not fixed by compatible
and there's variance based on the choices made by board designers.

Originally when Ryan added it, he removed the capability flag entirely
from match data, but this changed the default for the sama7g5-emac and
the removal had to be reverted for these devices. Because these devices
default to an external refclk, and the current property is only capable
of communicating external refclks, there's no way to make the
sama7g5-emac use an internal refclk.

Additionally, this property has no limiting based on compatible, and
if used on a platform with an external refclk that is not controlled
by USRIO the capability would be erroneously set. Because of the reuse
of the at91_default_usrio struct by non-at91 devices, this could cause
the refclk bit to be set in error, on a system where the refclk is
externally provided without usrio settings being required.

Change the new capability flag so that it actually represents the
hardware being capable of controlling the refclk source via USRIO,
and move the selection of default behaviour into the macb_usrio_config
struct provided as part of match data.
Modify the devicetree code to support a new property,
"cdns,refclk-source" which will support devices with either default,
retaining support for "cdns,refclk-external" for compatibility reasons.

Signed-off-by: Conor Dooley <conor.dooley@microchip.com>
---
drivers/net/ethernet/cadence/macb.h | 1 +
drivers/net/ethernet/cadence/macb_main.c | 55 ++++++++++++++++++------
2 files changed, 44 insertions(+), 12 deletions(-)

diff --git a/drivers/net/ethernet/cadence/macb.h b/drivers/net/ethernet/cadence/macb.h
index baf48f02d7e27..9cd565cb87e4c 100644
--- a/drivers/net/ethernet/cadence/macb.h
+++ b/drivers/net/ethernet/cadence/macb.h
@@ -1214,6 +1214,7 @@ struct macb_usrio_config {
u32 refclk;
u32 clken;
u32 hdfctlen;
+ bool refclk_default_external;
};

struct macb_config {
diff --git a/drivers/net/ethernet/cadence/macb_main.c b/drivers/net/ethernet/cadence/macb_main.c
index d908850c59498..1f9e46cf7e1cb 100644
--- a/drivers/net/ethernet/cadence/macb_main.c
+++ b/drivers/net/ethernet/cadence/macb_main.c
@@ -4318,12 +4318,8 @@ static const struct net_device_ops macb_netdev_ops = {
static void macb_configure_caps(struct macb *bp,
const struct macb_config *dt_conf)
{
- struct device_node *np = bp->pdev->dev.of_node;
- bool refclk_ext;
u32 dcfg;

- refclk_ext = of_property_read_bool(np, "cdns,refclk-ext");
-
if (dt_conf)
bp->caps = dt_conf->caps;

@@ -4356,9 +4352,6 @@ static void macb_configure_caps(struct macb *bp,
}
}

- if (refclk_ext)
- bp->caps |= MACB_CAPS_USRIO_HAS_REFCLK_SOURCE;
-
dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
}

@@ -4626,8 +4619,36 @@ static int macb_init(struct platform_device *pdev)
if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
val |= bp->usrio->clken;

- if (bp->caps & MACB_CAPS_USRIO_HAS_REFCLK_SOURCE)
- val |= bp->usrio->refclk;
+ if (bp->caps & MACB_CAPS_USRIO_HAS_REFCLK_SOURCE) {
+ const char *prop;
+ bool refclk_ext;
+ int ret;
+
+ /* Default to whatever was set in the match data for
+ * this device. There's two properties for refclk
+ * control, but the boolean one is deprecated so is
+ * a lower priority to check, no device should have
+ * both.
+ */
+ refclk_ext = bp->usrio->refclk_default_external;
+
+ ret = of_property_read_string(pdev->dev.of_node,
+ "cdns,refclk-source", &prop);
+ if (!ret) {
+ if (!strcmp(prop, "external"))
+ refclk_ext = true;
+ else
+ refclk_ext = false;
+ } else {
+ ret = of_property_read_bool(pdev->dev.of_node,
+ "cdns,refclk-ext");
+ if (ret)
+ refclk_ext = true;
+ }
+
+ if (refclk_ext)
+ val |= bp->usrio->refclk;
+ }

macb_or_gem_writel(bp, USRIO, val);
}
@@ -5223,11 +5244,21 @@ static const struct macb_usrio_config at91_default_usrio = {
.clken = MACB_BIT(CLKEN),
};

-static const struct macb_usrio_config sama7g5_usrio = {
+static const struct macb_usrio_config sama7g5_gem_usrio = {
.mii = 0,
.rmii = 1,
.rgmii = 2,
.refclk = BIT(2),
+ .refclk_default_external = false,
+ .hdfctlen = BIT(6),
+};
+
+static const struct macb_usrio_config sama7g5_emac_usrio = {
+ .mii = 0,
+ .rmii = 1,
+ .rgmii = 2,
+ .refclk = BIT(2),
+ .refclk_default_external = true,
.hdfctlen = BIT(6),
};

@@ -5364,7 +5395,7 @@ static const struct macb_config sama7g5_gem_config = {
.dma_burst_length = 16,
.clk_init = macb_clk_init,
.init = macb_init,
- .usrio = &sama7g5_usrio,
+ .usrio = &sama7g5_gem_usrio,
};

static const struct macb_config sama7g5_emac_config = {
@@ -5376,7 +5407,7 @@ static const struct macb_config sama7g5_emac_config = {
.dma_burst_length = 16,
.clk_init = macb_clk_init,
.init = macb_init,
- .usrio = &sama7g5_usrio,
+ .usrio = &sama7g5_emac_usrio,
};

static const struct macb_config versal_config = {