net: phy: Add driver for Motorcomm yt8521 gigabit ethernet phy
Subsystems (4)
Frank
maintainerPATCH 1/1 net: phy: Add driver for Motorcomm yt8521 gigabit ethernet phy
Add a driver for the motorcomm yt8521 gigabit ethernet phy. We have verified the driver on StarFive VisionFive development board, which is developed by Shanghai StarFive Technology Co., Ltd.. On the board, yt8521 gigabit ethernet phy works in utp mode, RGMII interface, supports 1000M/100M/10M speeds, and wol(magic package).
---
patch v8.1:
Fix: This comment starts with '/**', but isn't a kernel-doc comment warning (Reported-by: kernel test robot <[email protected]>).
patch v8:
Hi Russell
Quoted 10 lines
Quoted 7 lines
Quoted 5 lines
As previously pointed out, dropping the MDIO bus lock in a phy_select_page()..phy_restore_page() region unsafe. I think you need to ensure that
yt8521_fiber_config_aneg()is safe to be called under the lock, and I suspect having a version ofgenphy_config_aneg()which can be called with the lock held would be a better approach.with your suggestion we add
yt8521_set_page()which does not hold the lock.And so you don't understand the locking issues...
Quoted 7 lines
If you drop the lock, it means that some other thread (e.g. userspace) can come in and change the page register under you, which will completely change which register(s) you are accessing.
Please implement locking properly, don't bodge around with it. I made my suggestion above specifically after having thought about the issues and what would be possible to give a correct locking implementation.
with your suggestion we removed yt8521_set_page(), then add ytphy_utp_config_aneg() and ytphy_utp_read_abilities() which can be called with the lock held.
patch v7:
Hi Russell
Quoted 5 lines
As previously pointed out, dropping the MDIO bus lock in a phy_select_page()..phy_restore_page() region unsafe. I think you need to ensure that
yt8521_fiber_config_aneg()is safe to be called under the lock, and I suspect having a version ofgenphy_config_aneg()which can be called with the lock held would be a better approach.
with your suggestion we add yt8521_set_page() which does not hold the lock.
patch v6:
Hi Andrew
Quoted 2 lines
You appear to only advertise 1G speeds. Does 100M actually work, but you don't advertise it?
yes, surely 100M fibre does work and since Fiber 100M advertise bit(ADVERTISE_100FULL) is conflict with ADVERTISE_1000XPSE_ASYM we can only advertise the latter.
Quoted 15 lines
This is another place the two line side model falls apart. You should not be modifying
phydev->supported, because how do you get back the bits your cleared when it swaps from one to the other? phylib has no concept ofphydev->supportedchanging at run time. It is set when the PHY probes, and only changed when the MAC remove what it does not support.You need
yt8521_fiber_config_aneg()to take a copy ofphydev->advertising, mask out what fibre does not support, and program the hardware based on that. You needyt8521_utp_config_aneg()to take a copy ofphydev->advertising, mask out what copper does not support, and program the hardware based on that.phydev->supportedandphydev->advertisingshould always be a superset of both copper and fibre, if both are available.
Quoted 5 lines
Maybe, in a flow up patch, you can actually look at
phydev->advertising, see all the fibre modes have been removed, fibre will never get link, and so power the fibre off. Or all the copper modes have been removed, the copper will never get link, so turn it off.
We add combo_advertising which is used for case of YT8521 in combo mode, this means that yt8521 may work in utp or fiber mode which depends on which media is connected. Advertising bits will be updated before calling yt8521_fiber_config_aneg/genphy_config_aneg.
patch v5:
Hi Jakub,Andrew,Russell
Quoted 21 lines
The user could of changed the pause settings, which are going to be ignored here. Also, you should not assume the MAC can actually do asymmetric pause, not all can.
phydev->advertisingwill be set to only include what the MAC can actually do.The whole concept of having two line sides connected to one MAC and seeing which gets link first is unsupported in Linux. In theory, you want to be able to configure each line side differently. Maybe you want autoneg on copper, but fixed on fibre, asymmetric pause with fibre, but symmetric pause on copper, etc. Since there is only one instance of phydev here, you don't have anywhere to store two sets of configuration, nor any sort of kAPI to deal with two phydev structures etc. So the user experience is not so great.
With the Marvell Switches which also have this capability, i actually ignore it, use the phy-mode it decide which should be used, either copper or fibre, and leave the other powered off so it can never get link. There is at least one Marvell PHY which does however support first up wins, so this behaviour is not new. I just don't recommend it.
Quoted 3 lines
And it gets even more interesting when the SFP is actually copper. But not supported here.
since the integration with phylink is missing in this driver, that is
1. we changed the codes for getting/advertising of pause and other capabilities, the basic mechnism is that the capabilities and advertising will changed based on current linkuped media, please review.
2. as to the fibre/copper two lines support, this phy driver will handle to ensure linkup only on one media and it is transparent to MAC and uplayer. in practice, this do help users use the feature of the phy device, especially for the combo mode design in which customer can switch between fibre and copper automatically.
patch v4.4:
Hi Jakub
Fix "warning: variable 'changed'"
patch v4.3:
Hi Jakub
Kernel 6.0-rc1 is cut on 2022-08-14, we update new patch for Kernel 6.0-rc1.
patch v4.2:
fix "=0D" and "=20" which in patch v4.1.
patch v4.1:
Hi Andrew
Thanks very much and based on your comments we modified the patch v4.1 as below.
Quoted 4 lines
You have interrupt bits defined, you enable the WoL interrupt, but you don't have an interrupt handler. PHY interrupts are generally level interrupts, so on WoL, don't you end up with an interrupt storm, because you don't have anything clearing the WoL interrupt?
in the driver, only the interrupt for WOL is used according to uplayer WOL configuration via .set_wol. If WOL is enalbed, then the WOL interrupt is enabled accordingly; you know, for a system support WOL, the WOL interrupt will be handled by user's WOL circuit, and it will not cause interrupt storm to CPU core system.
for WOL interrupt, by default, we configure to pulse trigger mode, that is a pulse from High to low change, and the low level will hold for 672ms. user's WOL circuit will detected such volt level change as a WOL event trigger.
patch v4:
Hi Andrew,Jakub
Thanks very much and based on your comments we modified the patch v4 as below.
We evaluated the Marvell 10G driver and at803x you suggested. The 2 drivers implement SFP module attach/detach functions and we think these functions do not help yt8521 to do UTP/Fiber register space arbitration which you may concern in previous patch.
Yt8521 can detect utp/fiber media link status automatically. For the case of both media are connected, driver arbitrates the priority of the media (by default, driver takes fiber as higher priority) and report the media status to up layer(MAC).
patch v3:
Hi Andrew
Thanks and based on your comments we modified the patch as below.
Quoted 5 lines
It is generally not that simple. Fibre, you probably want 1000BaseX, unless the fibre module is actually copper, and then you want SGMII. So you need something to talk to the fibre module and ask it what it is. That something is phylink. Phylink does not support both copper and fibre at the same time for one MAC.
yes, you said it and for MAC, it does not support copper and Fiber at same time. but from Physical Layer, you know, sometimes both Copper and Fiber cables are connected. in this case, Phy driver should do arbitration and report to MAC which media should be used and Link-up.
This is the reason that the driver has a "polling mode", and by default, also this driver takes fiber as first priority which matches phy chip default behavior.
patch v2:
Hi Andrew, Russell King, Peter,
Thanks and based on your comments we modified the patch as below.
Quoted 2 lines
So there's only two possible pages that can be used in the extended register space?
Yes,there is only two register space (utp and fiber).
Quoted 5 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+/* Extended Register's Data Register */ +#define YTPHY_PAGE_DATA 0x1FThese are defined exactly the same way as below. Please reuse code where possible.
Yes, code will be reuse, but "YT8511_PAGE" need to be rename like "YTPHY_PAGE_DATA",as it is common register for yt phys.
patch v1:
Add a driver for the motorcomm yt8521 gigabit ethernet phy. We have verified the driver on StarFive VisionFive development board, which is developed by Shanghai StarFive Technology Co., Ltd.. On the board, yt8521 gigabit ethernet phy works in utp mode, RGMII interface, supports 1000M/100M/10M speeds, and wol(magic package).
Thanks and BR,
Frank
MAINTAINERS | 1 + drivers/net/phy/Kconfig | 2 +- drivers/net/phy/motorcomm.c | 1637 ++++++++++++++++++++++++++++++++++- 3 files changed, 1637 insertions(+), 3 deletions(-)
MAINTAINERS +1 -0
@@ -13937,6 +13937,7 @@ F: include/uapi/linux/meye.h MOTORCOMM PHY DRIVER M: Peter Geis <[email protected]> +M: Frank <[email protected]> L: [email protected] S: Maintained F: drivers/net/phy/motorcomm.c
drivers/net/phy/Kconfig +1 -1
@@ -260,7 +260,7 @@ config MOTORCOMM_PHY tristate "Motorcomm PHYs" help Enables support for Motorcomm network PHYs. - Currently supports the YT8511 gigabit PHY. + Currently supports the YT8511, YT8521 Gigabit Ethernet PHYs. config NATIONAL_PHY tristate "National Semiconductor PHYs"
drivers/net/phy/motorcomm.c +1635 -2
1,678 lines not on this page · raw message
Signature · 1 line
2.31.0.windows.1
Paolo Abeni
maintainerHello,
On Tue, 2022-10-25 at 14:45 +0800, Frank wrote:
[...]
Quoted 61 lines
drivers/net/phy/motorcomm.c +61
-0+/** +* yt8521_read_status_paged() - determines the speed and duplex of one page +* @phydev: a pointer to a &struct phy_device +* @page: The reg page(YT8521_RSSR_FIBER_SPACE/YT8521_RSSR_UTP_SPACE) to +* operate. +* +* returns 1 (utp or fiber link),0 (no link) or negative errno code +*/ +static int yt8521_read_status_paged (struct phy_device *phydev,int page) +{ +int fiber_latch_val; +int fiber_curr_val; +int old_page; +int ret =0 ; +int status; +int link; + +linkmode_zero (phydev->lp_advertising); + phydev->duplex =DUPLEX_UNKNOWN ; + phydev->speed =SPEED_UNKNOWN ; + phydev->asym_pause =0 ; + phydev->pause =0 ; + +/* YT8521 has two reg space (utp/fiber) for linkup with utp/fiber +* respectively. but for utp/fiber combo mode, reg space should be +* arbitrated based on media priority. by default, utp takes +* priority. reg space should be properly set before read +* YTPHY_SPECIFIC_STATUS_REG. +*/ + + page &=YT8521_RSSR_SPACE_MASK ; + old_page =phy_select_page (phydev, page); +if (old_page <0 ) + goto err_restore_page; + +/* Read YTPHY_SPECIFIC_STATUS_REG, which indicates the speed and duplex +* of the PHY is actually using. +*/ + ret =__phy_read (phydev,YTPHY_SPECIFIC_STATUS_REG ); +if (ret <0 ) + goto err_restore_page; + + status = ret; + link = !!(status &YTPHY_SSR_LINK ); + +/* When PHY is in fiber mode, speed transferred from 1000Mbps to +* 100Mbps,there is not link down from YTPHY_SPECIFIC_STATUS_REG, so +* we need check MII_BMSR to identify such case. +*/ +if (page ==YT8521_RSSR_FIBER_SPACE ) { + ret =__phy_read (phydev,MII_BMSR ); +if (ret <0 ) + goto err_restore_page; + + fiber_latch_val = ret; + ret =__phy_read (phydev,MII_BMSR ); +if (ret <0 ) + goto err_restore_page; + + fiber_curr_val = ret; +if (link && fiber_latch_val != fiber_curr_val) {
drivers/net/phy/motorcomm.c static int yt8511_config_init(struct phy_device *phydev) · line 855
Quoted 10 lines
drivers/net/phy/motorcomm.c +10
-0+ link =0 ; +phydev_info (phydev, +"%s, fiber link down detect, latch = %04x, curr = %04x\n" , + __func__, fiber_latch_val, fiber_curr_val); + } + }else { +/* Read autonegotiation status */ + ret =__phy_read (phydev,MII_BMSR ); +if (ret <0 ) +return ret;
You need to restore the page even on this error path.
[...]
Quoted 32 lines
drivers/net/phy/motorcomm.c +32
-0+/** +* yt8521_modify_bmcr_paged - bits modify a PHY's BMCR register of one page +* @phydev: the phy_device struct +* @page: The reg page(YT8521_RSSR_FIBER_SPACE/YT8521_RSSR_UTP_SPACE) to operate +* @mask: bit mask of bits to clear +* @set: bit mask of bits to set +* +* NOTE: Convenience function which allows a PHY's BMCR register to be +* modified as new register value = (old register value & ~mask) | set. +* YT8521 has two space (utp/fiber) and three mode (utp/fiber/poll), each space +* has MII_BMCR. poll mode combines utp and faber,so need do both. +* If it is reset, it will wait for completion. +* +* returns 0 or negative errno code +*/ +static int yt8521_modify_bmcr_paged (struct phy_device *phydev,int page, +u16 mask,u16 set) +{ +int max_cnt =500 ;/* the max wait time of reset ~ 500 ms */ +int old_page; +int ret =0 ; + + old_page =phy_select_page (phydev, page &YT8521_RSSR_SPACE_MASK ); +if (old_page <0 ) + goto err_restore_page; + + ret =__phy_modify (phydev,MII_BMCR , mask, set); +if (ret <0 ) + goto err_restore_page; + +/* If it is reset, need to wait for the reset to complete */ +if (set ==BMCR_RESET ) {
drivers/net/phy/motorcomm.c static int yt8511_config_init(struct phy_device *phydev) · line 998
Quoted 10 lines
drivers/net/phy/motorcomm.c +10
-0+while (max_cnt--) { +usleep_range (1000 ,1100 ); + ret =__phy_read (phydev,MII_BMCR ); +if (ret <0 ) + goto err_restore_page; + +if (!(ret &BMCR_RESET )) +return phy_restore_page (phydev, old_page,0 ); + } +if (max_cnt <=0 )
The above check is not needed, the loop can terminate only when such condition is true.
Cheers,
Paolo