dwmac-sun55i currently hardcodes A523 syscon lookups, glue register offsets, 100 ps delay steps, and stmmac flags, and writes the glue register only once during probe. Introduce struct sun55i_gmac_data and struct sun55i_gmac, and move the glue write into plat_dat->init so settings are restored on resume and A733 GMAC210 support can be added without duplicating probe logic. A523 behavior remains unchanged via sun55i_a523_gmac200_data. Assisted-by: LLM Signed-off-by: Alastair D'Silva --- .../ethernet/stmicro/stmmac/dwmac-sun55i.c | 136 +++++++++++++----- 1 file changed, 103 insertions(+), 33 deletions(-) diff --git a/drivers/net/ethernet/stmicro/stmmac/dwmac-sun55i.c b/drivers/net/ethernet/stmicro/stmmac/dwmac-sun55i.c index 862df173d963..e872e241a220 100644 --- a/drivers/net/ethernet/stmicro/stmmac/dwmac-sun55i.c +++ b/drivers/net/ethernet/stmicro/stmmac/dwmac-sun55i.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -23,8 +24,6 @@ #include "stmmac.h" #include "stmmac_platform.h" -#define SYSCON_REG 0x34 - /* RMII specific bits */ #define SYSCON_RMII_EN BIT(13) /* 1: enable RMII (overrides EPIT) */ /* Generic system control EMAC_CLK bits */ @@ -37,42 +36,78 @@ #define SYSCON_ETCS_EXT_GMII 0x1 #define SYSCON_ETCS_INT_GMII 0x2 -static int sun55i_gmac200_set_syscon(struct device *dev, - struct plat_stmmacenet_data *plat) +struct sun55i_gmac; + +struct sun55i_gmac_data { + int (*init_resources)(struct platform_device *pdev, + struct sun55i_gmac *gmac); + unsigned int flags; + u32 txdly_step_ps; + u32 rxdly_step_ps; + u32 offset; +}; + +struct sun55i_gmac { + const struct sun55i_gmac_data *data; + struct regmap *regmap; + u32 syscon_val; +}; + +static int sun55i_gmac200_init_resources(struct platform_device *pdev, + struct sun55i_gmac *gmac) +{ + gmac->regmap = syscon_regmap_lookup_by_phandle(pdev->dev.of_node, + "syscon"); + if (IS_ERR(gmac->regmap)) + return dev_err_probe(&pdev->dev, PTR_ERR(gmac->regmap), + "Unable to map syscon\n"); + + return 0; +} + +static int sun55i_gmac200_validate_delay(struct device *dev, const char *dir, + u32 *val, u32 step, u32 max) +{ + if (*val % step) + return dev_err_probe(dev, -EINVAL, + "%s-delay must be a multiple of %ups\n", + dir, step); + max *= step; + if (*val > max) + return dev_err_probe(dev, -EINVAL, + "%s clock delay exceeds maximum (%ups > %ups)\n", + dir, *val, max); + *val /= step; + dev_dbg(dev, "set %s-delay to %x\n", dir, *val); + + return 0; +} + +static int sun55i_gmac200_parse_config(struct device *dev, + struct plat_stmmacenet_data *plat, + struct sun55i_gmac *gmac) { + const struct sun55i_gmac_data *data = gmac->data; struct device_node *node = dev->of_node; - struct regmap *regmap; u32 val, reg = 0; int ret; - regmap = syscon_regmap_lookup_by_phandle(node, "syscon"); - if (IS_ERR(regmap)) - return dev_err_probe(dev, PTR_ERR(regmap), "Unable to map syscon\n"); - if (!of_property_read_u32(node, "tx-internal-delay-ps", &val)) { - if (val % 100) - return dev_err_probe(dev, -EINVAL, - "tx-delay must be a multiple of 100ps\n"); - val /= 100; - dev_dbg(dev, "set tx-delay to %x\n", val); - if (!FIELD_FIT(SYSCON_ETXDC_MASK, val)) - return dev_err_probe(dev, -EINVAL, - "TX clock delay exceeds maximum (%u00ps > %lu00ps)\n", - val, FIELD_MAX(SYSCON_ETXDC_MASK)); + ret = sun55i_gmac200_validate_delay(dev, "tx", &val, + data->txdly_step_ps, + FIELD_MAX(SYSCON_ETXDC_MASK)); + if (ret) + return ret; reg |= FIELD_PREP(SYSCON_ETXDC_MASK, val); } if (!of_property_read_u32(node, "rx-internal-delay-ps", &val)) { - if (val % 100) - return dev_err_probe(dev, -EINVAL, - "rx-delay must be a multiple of 100ps\n"); - val /= 100; - dev_dbg(dev, "set rx-delay to %x\n", val); - if (!FIELD_FIT(SYSCON_ERXDC_MASK, val)) - return dev_err_probe(dev, -EINVAL, - "RX clock delay exceeds maximum (%u00ps > %lu00ps)\n", - val, FIELD_MAX(SYSCON_ERXDC_MASK)); + ret = sun55i_gmac200_validate_delay(dev, "rx", &val, + data->rxdly_step_ps, + FIELD_MAX(SYSCON_ERXDC_MASK)); + if (ret) + return ret; reg |= FIELD_PREP(SYSCON_ERXDC_MASK, val); } @@ -95,9 +130,20 @@ static int sun55i_gmac200_set_syscon(struct device *dev, phy_modes(plat->phy_interface)); } - ret = regmap_write(regmap, SYSCON_REG, reg); + gmac->syscon_val = reg; + + return 0; +} + +static int sun55i_gmac200_init(struct device *dev, void *priv) +{ + struct sun55i_gmac *gmac = priv; + int ret; + + ret = regmap_write(gmac->regmap, gmac->data->offset, gmac->syscon_val); if (ret < 0) - return dev_err_probe(dev, ret, "Failed to write to syscon\n"); + return dev_err_probe(dev, ret, + "Failed to write glue configuration\n"); return 0; } @@ -107,25 +153,40 @@ static int sun55i_gmac200_probe(struct platform_device *pdev) struct plat_stmmacenet_data *plat_dat; struct stmmac_resources stmmac_res; struct device *dev = &pdev->dev; + struct sun55i_gmac *gmac; struct clk *clk; int ret; + gmac = devm_kzalloc(dev, sizeof(*gmac), GFP_KERNEL); + if (!gmac) + return -ENOMEM; + + gmac->data = device_get_match_data(dev); + if (!gmac->data) + return -EINVAL; + ret = stmmac_get_platform_resources(pdev, &stmmac_res); if (ret) return ret; + ret = gmac->data->init_resources(pdev, gmac); + if (ret) + return ret; + plat_dat = devm_stmmac_probe_config_dt(pdev, stmmac_res.mac); if (IS_ERR(plat_dat)) return PTR_ERR(plat_dat); - /* BSP disables it */ - plat_dat->flags |= STMMAC_FLAG_SPH_DISABLE; + plat_dat->flags |= gmac->data->flags; plat_dat->host_dma_width = 32; - ret = sun55i_gmac200_set_syscon(dev, plat_dat); + ret = sun55i_gmac200_parse_config(dev, plat_dat, gmac); if (ret) return ret; + plat_dat->bsp_priv = gmac; + plat_dat->init = sun55i_gmac200_init; + clk = devm_clk_get_enabled(dev, "mbus"); if (IS_ERR(clk)) return dev_err_probe(dev, PTR_ERR(clk), @@ -138,8 +199,17 @@ static int sun55i_gmac200_probe(struct platform_device *pdev) return devm_stmmac_pltfr_probe(pdev, plat_dat, &stmmac_res); } +static const struct sun55i_gmac_data sun55i_a523_gmac200_data = { + .init_resources = sun55i_gmac200_init_resources, + .flags = STMMAC_FLAG_SPH_DISABLE, + .offset = 0x34, + .txdly_step_ps = 100, + .rxdly_step_ps = 100, +}; + static const struct of_device_id sun55i_gmac200_match[] = { - { .compatible = "allwinner,sun55i-a523-gmac200" }, + { .compatible = "allwinner,sun55i-a523-gmac200", + .data = &sun55i_a523_gmac200_data }, { } }; MODULE_DEVICE_TABLE(of, sun55i_gmac200_match); -- 2.53.0