net: stmmac: dwmac-sophgo: Add phy interface filter
Subsystems (7)
Inochi Amaoto
maintainerv7 cover net: stmmac: dwmac-sophgo: Add phy interface filter
As the SG2042 has an internal rx delay, the delay should be remove when init the mac, otherwise the phy will be misconfigurated.
Since this delay fix is common for other MACs, add a common helper for it. And use it to fix SG2042.
Change from v6:
- https://lore.kernel.org/all/[email protected]
1. patch 2: fixed kdoc warning
Change from v5:
- https://lore.kernel.org/all/[email protected]
1. patch 1: remove duplicate empty line
Change from v4:
- https://lore.kernel.org/all/[email protected]
1. patch 3: add const qualifier to struct sg2042_dwmac_data
Change from v3:
- https://lore.kernel.org/all/[email protected]
1. patch 1: fix binding check error
Change from v2:
- https://lore.kernel.org/all/[email protected]
1. patch 3: fix comment typo
2. patch 3: add check for PHY_INTERFACE_MODE_NA.
Change from v1:
- https://lore.kernel.org/all/[email protected]
1. Add phy-mode property to dt-bindings of sophgo,sg2044-dwmac
2. Add common helper for fixing RGMII phy mode
3. Use struct to hold the compatiable data.
Inochi Amaoto (3):
dt-bindings: net: sophgo,sg2044-dwmac: add phy mode restriction
net: phy: Add helper for fixing RGMII PHY mode based on internal mac
delay
net: stmmac: dwmac-sophgo: Add phy interface filter
.../bindings/net/sophgo,sg2044-dwmac.yaml | 19 ++++++++ .../ethernet/stmicro/stmmac/dwmac-sophgo.c | 20 ++++++++- drivers/net/phy/phy-core.c | 43 +++++++++++++++++++ include/linux/phy.h | 3 ++ 4 files changed, 84 insertions(+), 1 deletion(-)
--
2.51.2
Inochi Amaoto
maintainerPATCH v7 1/3 dt-bindings: net: sophgo,sg2044-dwmac: add phy mode restriction
As the ethernet controller of SG2044 and SG2042 only supports RGMII phy. Add phy-mode property to restrict the value.
Also, since SG2042 has internal rx delay in its mac, make only "rgmii-txid" and "rgmii-id" valid for phy-mode.
--- .../bindings/net/sophgo,sg2044-dwmac.yaml | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
Documentation/devicetree/bindings/net/sophgo,sg2044-dwmac.yaml +19 -0
@@ -70,6 +70,25 @@ required: allOf: - $ref: snps,dwmac.yaml# + - if: + properties: + compatible: + contains: + const: sophgo,sg2042-dwmac + then: + properties: + phy-mode: + enum: + - rgmii-txid + - rgmii-id + else: + properties: + phy-mode: + enum: + - rgmii + - rgmii-rxid + - rgmii-txid + - rgmii-id unevaluatedProperties: false
Signature · 1 line
2.51.2
Inochi Amaoto
maintainerPATCH v7 2/3 net: phy: Add helper for fixing RGMII PHY mode based on internal mac delay
In reply to Inochi AmaotoThe "phy-mode" property of devicetree indicates whether the PCB has delay now, which means the mac needs to modify the PHY mode based on whether there is an internal delay in the mac.
This modification is similar for many ethernet drivers. To simplify code, define the helper phy_fix_phy_mode_for_mac_delays(speed, mac_txid, mac_rxid) to fix PHY mode based on whether mac adds internal delay.
--- drivers/net/phy/phy-core.c | 43 ++++++++++++++++++++++++++++++++++++++ include/linux/phy.h | 3 +++ 2 files changed, 46 insertions(+)
drivers/net/phy/phy-core.c +43 -0
@@ -101,6 +101,49 @@ const char *phy_rate_matching_to_str(int rate_matching) }EXPORT_SYMBOL_GPL (phy_rate_matching_to_str); +/** +* phy_fix_phy_mode_for_mac_delays - Convenience function for fixing PHY +* mode based on whether mac adds internal delay +* +* @interface: The current interface mode of the port +* @mac_txid: True if the mac adds internal tx delay +* @mac_rxid: True if the mac adds internal rx delay +* +* Return: fixed PHY mode, or PHY_INTERFACE_MODE_NA if the interface can +* not apply the internal delay +*/ +phy_interface_t phy_fix_phy_mode_for_mac_delays (phy_interface_t interface, +bool mac_txid,bool mac_rxid) +{ +if (!phy_interface_mode_is_rgmii (interface)) +return interface; + +if (mac_txid && mac_rxid) { +if (interface ==PHY_INTERFACE_MODE_RGMII_ID ) +return PHY_INTERFACE_MODE_RGMII ; +return PHY_INTERFACE_MODE_NA ; + } + +if (mac_txid) { +if (interface ==PHY_INTERFACE_MODE_RGMII_ID ) +return PHY_INTERFACE_MODE_RGMII_RXID ; +if (interface ==PHY_INTERFACE_MODE_RGMII_TXID ) +return PHY_INTERFACE_MODE_RGMII ; +return PHY_INTERFACE_MODE_NA ; + } + +if (mac_rxid) { +if (interface ==PHY_INTERFACE_MODE_RGMII_ID ) +return PHY_INTERFACE_MODE_RGMII_TXID ; +if (interface ==PHY_INTERFACE_MODE_RGMII_RXID ) +return PHY_INTERFACE_MODE_RGMII ; +return PHY_INTERFACE_MODE_NA ; + } + +return interface; +} +EXPORT_SYMBOL_GPL (phy_fix_phy_mode_for_mac_delays); +/** * phy_interface_num_ports - Return the number of links that can be carried by * a given MAC-PHY physical link. Returns 0 if this is
include/linux/phy.h +3 -0
@@ -1813,6 +1813,9 @@ static inline bool phy_is_pseudo_fixed_link(struct phy_device *phydev)return phydev->is_pseudo_fixed_link; } +phy_interface_t phy_fix_phy_mode_for_mac_delays (phy_interface_t interface, +bool mac_txid,bool mac_rxid); +int phy_save_page (struct phy_device *phydev);int phy_select_page (struct phy_device *phydev,int page);int phy_restore_page (struct phy_device *phydev,int oldpage,int ret);
Signature · 1 line
2.51.2
Inochi Amaoto
maintainerPATCH v7 3/3 net: stmmac: dwmac-sophgo: Add phy interface filter
In reply to Inochi AmaotoAs the SG2042 has an internal rx delay, the delay should be removed when initializing the mac, otherwise the phy will be misconfigurated.
--- .../ethernet/stmicro/stmmac/dwmac-sophgo.c | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-)
drivers/net/ethernet/stmicro/stmmac/dwmac-sophgo.c +19 -1
@@ -7,11 +7,16 @@#include <linux/clk.h> #include <linux/module.h> +#include <linux/property.h> #include <linux/mod_devicetable.h> #include <linux/platform_device.h> #include "stmmac_platform.h" +struct sophgo_dwmac_data { +bool has_internal_rx_delay; +}; +static int sophgo_sg2044_dwmac_init (struct platform_device *pdev,struct plat_stmmacenet_data *plat_dat,struct stmmac_resources *stmmac_res)
@@ -32,6 +37,7 @@ static int sophgo_sg2044_dwmac_init(struct platform_device *pdev,static int sophgo_dwmac_probe (struct platform_device *pdev) {struct plat_stmmacenet_data *plat_dat; +const struct sophgo_dwmac_data *data;struct stmmac_resources stmmac_res;struct device *dev = &pdev->dev;int ret;
@@ -50,11 +56,23 @@ static int sophgo_dwmac_probe(struct platform_device *pdev)if (ret)return ret; + data =device_get_match_data (&pdev->dev); +if (data && data->has_internal_rx_delay) { + plat_dat->phy_interface =phy_fix_phy_mode_for_mac_delays (plat_dat->phy_interface, + false, true); +if (plat_dat->phy_interface ==PHY_INTERFACE_MODE_NA ) +return -EINVAL ; + } +return stmmac_dvr_probe (dev, plat_dat, &stmmac_res); } +static const struct sophgo_dwmac_data sg2042_dwmac_data = { + .has_internal_rx_delay = true, +}; +static const struct of_device_id sophgo_dwmac_match[] = { - { .compatible ="sophgo,sg2042-dwmac" }, + { .compatible ="sophgo,sg2042-dwmac" , .data = &sg2042_dwmac_data }, { .compatible ="sophgo,sg2044-dwmac" }, {/* sentinel */ } };
Signature · 1 line
2.51.2
Simon Horman
reviewerQuoted 12 lines · Inochi Amaoto
As the SG2042 has an internal rx delay, the delay should be removed when initializing the mac, otherwise the phy will be misconfigurated.
Fixes 543009e2d4cd ("net: stmmac: dwmac-sophgo: Add support for Sophgo SG2042 SoC")Signed-off-by Inochi Amaoto <[email protected]>Tested-by Han Gao <[email protected]>Reviewed-by Andrew Lunn <[email protected]>--- .../ethernet/stmicro/stmmac/dwmac-sophgo.c | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-)drivers/net/ethernet/stmicro/stmmac/dwmac-sophgo.c +0
-0
...
drivers/net/ethernet/stmicro/stmmac/dwmac-sophgo.c static int sophgo_dwmac_probe(struct platform_device *pdev) · line 64
Quoted 10 lines
drivers/net/ethernet/stmicro/stmmac/dwmac-sophgo.c +6
-0@@ -50,11 +56,23 @@ static int sophgo_dwmac_probe(struct platform_device *pdev)if (ret)return ret; + data =device_get_match_data (&pdev->dev); +if (data && data->has_internal_rx_delay) { + plat_dat->phy_interface =phy_fix_phy_mode_for_mac_delays (plat_dat->phy_interface, + false, true); +if (plat_dat->phy_interface ==PHY_INTERFACE_MODE_NA ) +return -EINVAL ;
I'm sorry if this is a false positive. Because, more so than Russell [1], I confused about how about the treatment of phy_interface. But it seems that there is a miss match between the use of phy_fix_phy_mode_for_mac_delays() above and the binding.
The call to phy_fix_phy_mode_for_mac_delays() above will return PHY_INTERFACE_MODE_NA unless phy_interface is PHY_INTERFACE_MODE_RGMII_ID or PHY_INTERFACE_MODE_RGMII_RXID.
phy_interface_t phy_fix_phy_mode_for_mac_delays(phy_interface_t interface,
bool mac_txid, bool mac_rxid)
...
if (mac_rxid) {
if (interface == PHY_INTERFACE_MODE_RGMII_ID)
return PHY_INTERFACE_MODE_RGMII_TXID;
if (interface == PHY_INTERFACE_MODE_RGMII_RXID)
return PHY_INTERFACE_MODE_RGMII;
return PHY_INTERFACE_MODE_NA;
}...
Looking at phy_modes(), unsurprisingly, the following mappings occur:
* "rgmii" -> PHY_INTERFACE_MODE_RGMII
* "rgmii-id" -> PHY_INTERFACE_MODE_RGMII_ID
* "rgmii-rxid" -> PHY_INTERFACE_MODE_RGMII_RXID
* "rgmii-txid" -> PHY_INTERFACE_MODE_RGMII_TXID
And in the binding, patch 1/3 of this series, only phy-mode rgmii-txid or rgmii-id is allowed.
But if rgmii-txid is used, PHY_INTERFACE_MODE_RGMII_TXID will be passed to phy_fix_phy_mode_for_mac_delays(), which will return PHY_INTERFACE_MODE_NA.
Again, I'm confused about the mapping in phy_fix_phy_mode_for_mac_delays(). But there does seem to be some inconsistency between the binding and the driver implementation here.
Flagged by Claude Code with https://github.com/masoncl/review-prompts/
[1] https://lore.kernel.org/all/[email protected]/
...
Inochi Amaoto
maintainerQuoted 48 lines · Simon Horman
Quoted 12 lines · Inochi Amaoto
As the SG2042 has an internal rx delay, the delay should be removed when initializing the mac, otherwise the phy will be misconfigurated.
Fixes 543009e2d4cd ("net: stmmac: dwmac-sophgo: Add support for Sophgo SG2042 SoC")Signed-off-by Inochi Amaoto <[email protected]>Tested-by Han Gao <[email protected]>Reviewed-by Andrew Lunn <[email protected]>--- .../ethernet/stmicro/stmmac/dwmac-sophgo.c | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-)drivers/net/ethernet/stmicro/stmmac/dwmac-sophgo.c +0
-0...
drivers/net/ethernet/stmicro/stmmac/dwmac-sophgo.c static int sophgo_dwmac_probe(struct platform_device *pdev) · line 64
Quoted 10 lines
drivers/net/ethernet/stmicro/stmmac/dwmac-sophgo.c +6
-0@@ -50,11 +56,23 @@ static int sophgo_dwmac_probe(struct platform_device *pdev)if (ret)return ret; + data =device_get_match_data (&pdev->dev); +if (data && data->has_internal_rx_delay) { + plat_dat->phy_interface =phy_fix_phy_mode_for_mac_delays (plat_dat->phy_interface, + false, true); +if (plat_dat->phy_interface ==PHY_INTERFACE_MODE_NA ) +return -EINVAL ;I'm sorry if this is a false positive. Because, more so than Russell [1], I confused about how about the treatment of
phy_interface. But it seems that there is a miss match between the use ofphy_fix_phy_mode_for_mac_delays()above and the binding.The call to
phy_fix_phy_mode_for_mac_delays()above will returnPHY_INTERFACE_MODE_NAunlessphy_interfaceisPHY_INTERFACE_MODE_RGMII_IDorPHY_INTERFACE_MODE_RGMII_RXID.
phy_interface_tphy_fix_phy_mode_for_mac_delays(phy_interface_t interface,
boolmac_txid, boolmac_rxid)
...if (mac_rxid) { if (interface == PHY_INTERFACE_MODE_RGMII_ID) return PHY_INTERFACE_MODE_RGMII_TXID; if (interface == PHY_INTERFACE_MODE_RGMII_RXID) return PHY_INTERFACE_MODE_RGMII; return PHY_INTERFACE_MODE_NA; }...
Quoted 9 lines
Looking at
phy_modes(), unsurprisingly, the following mappings occur:
* "rgmii" ->PHY_INTERFACE_MODE_RGMII
* "rgmii-id" ->PHY_INTERFACE_MODE_RGMII_ID
* "rgmii-rxid" ->PHY_INTERFACE_MODE_RGMII_RXID
* "rgmii-txid" ->PHY_INTERFACE_MODE_RGMII_TXIDAnd in the binding, patch 1/3 of this series, only phy-mode rgmii-txid or rgmii-id is allowed.
rgmii-txid is a mistake and should be rgmii-rxid. This is because the mac of SG2042 add rx delay, and the phy can only add tx delay or no delay. So the phy-mode can only be rgmii-id or rgmii-rxid. I will fix it in the next version.
Quoted 7 lines
But if rgmii-txid is used,
PHY_INTERFACE_MODE_RGMII_TXIDwill be passed tophy_fix_phy_mode_for_mac_delays(), which will returnPHY_INTERFACE_MODE_NA.Again, I'm confused about the mapping in
phy_fix_phy_mode_for_mac_delays(). But there does seem to be some inconsistency between the binding and the driver implementation here.
I think this inconsistency begin with the change for ethernet-controller binding.
https://lore.kernel.org/all/[email protected]/
And this function serves as an helper so the driver can only add the delay once.
Quoted 5 lines
Flagged by Claude Code with https://github.com/masoncl/review-prompts/
[1] https://lore.kernel.org/all/[email protected]/
...
Regards,
Inochi