Commit 7f9d82a0 authored by Jakub Kicinski's avatar Jakub Kicinski

Merge branch 'fix-missing-phy-to-mac-rx-clock'

Romain Gantois says:

====================
Fix missing PHY-to-MAC RX clock

There is an issue with some stmmac/PHY combinations that has been reported
some time ago in a couple of different series:

Clark Wang's report:
https://lore.kernel.org/all/20230202081559.3553637-1-xiaoning.wang@nxp.com/
Clément Léger's report:
https://lore.kernel.org/linux-arm-kernel/20230116103926.276869-4-clement.leger@bootlin.com/

Stmmac controllers require an RX clock signal from the MII bus to perform
their hardware initialization successfully. This causes issues with some
PHY/PCS devices. If these devices do not bring the clock signal up before
the MAC driver initializes its hardware, then said initialization will
fail. This can happen at probe time or when the system wakes up from a
suspended state.

This series introduces new flags for phy_device and phylink_pcs. These
flags allow MAC drivers to signal to PHY/PCS drivers that the RX clock
signal should be enabled as soon as possible, and that it should always
stay enabled.

I have included specific uses of these flags that fix the RZN1 GMAC1 stmmac
driver that I am currently working on and that is not yet upstream. I have
also included changes to the at803x PHY driver that should fix the issue
that Clark Wang was having.
====================

Link: https://lore.kernel.org/r/20240326-rxc_bugfix-v6-0-24a74e5c761f@bootlin.comSigned-off-by: default avatarJakub Kicinski <kuba@kernel.org>
parents af352c3b 0f671b3b
...@@ -593,7 +593,7 @@ struct mac_device_info { ...@@ -593,7 +593,7 @@ struct mac_device_info {
const struct stmmac_mmc_ops *mmc; const struct stmmac_mmc_ops *mmc;
const struct stmmac_est_ops *est; const struct stmmac_est_ops *est;
struct dw_xpcs *xpcs; struct dw_xpcs *xpcs;
struct phylink_pcs *lynx_pcs; /* Lynx external PCS */ struct phylink_pcs *phylink_pcs;
struct mii_regs mii; /* MII register Addresses */ struct mii_regs mii; /* MII register Addresses */
struct mac_link link; struct mac_link link;
void __iomem *pcsr; /* vpointer to device CSRs */ void __iomem *pcsr; /* vpointer to device CSRs */
......
...@@ -479,9 +479,9 @@ static int socfpga_dwmac_probe(struct platform_device *pdev) ...@@ -479,9 +479,9 @@ static int socfpga_dwmac_probe(struct platform_device *pdev)
goto err_dvr_remove; goto err_dvr_remove;
} }
stpriv->hw->lynx_pcs = lynx_pcs_create_mdiodev(pcs_bus, 0); stpriv->hw->phylink_pcs = lynx_pcs_create_mdiodev(pcs_bus, 0);
if (IS_ERR(stpriv->hw->lynx_pcs)) { if (IS_ERR(stpriv->hw->phylink_pcs)) {
ret = PTR_ERR(stpriv->hw->lynx_pcs); ret = PTR_ERR(stpriv->hw->phylink_pcs);
goto err_dvr_remove; goto err_dvr_remove;
} }
} }
...@@ -498,7 +498,7 @@ static void socfpga_dwmac_remove(struct platform_device *pdev) ...@@ -498,7 +498,7 @@ static void socfpga_dwmac_remove(struct platform_device *pdev)
{ {
struct net_device *ndev = platform_get_drvdata(pdev); struct net_device *ndev = platform_get_drvdata(pdev);
struct stmmac_priv *priv = netdev_priv(ndev); struct stmmac_priv *priv = netdev_priv(ndev);
struct phylink_pcs *pcs = priv->hw->lynx_pcs; struct phylink_pcs *pcs = priv->hw->phylink_pcs;
stmmac_pltfr_remove(pdev); stmmac_pltfr_remove(pdev);
......
...@@ -944,10 +944,7 @@ static struct phylink_pcs *stmmac_mac_select_pcs(struct phylink_config *config, ...@@ -944,10 +944,7 @@ static struct phylink_pcs *stmmac_mac_select_pcs(struct phylink_config *config,
if (priv->hw->xpcs) if (priv->hw->xpcs)
return &priv->hw->xpcs->pcs; return &priv->hw->xpcs->pcs;
if (priv->hw->lynx_pcs) return priv->hw->phylink_pcs;
return priv->hw->lynx_pcs;
return NULL;
} }
static void stmmac_mac_config(struct phylink_config *config, unsigned int mode, static void stmmac_mac_config(struct phylink_config *config, unsigned int mode,
...@@ -1221,6 +1218,9 @@ static int stmmac_phy_setup(struct stmmac_priv *priv) ...@@ -1221,6 +1218,9 @@ static int stmmac_phy_setup(struct stmmac_priv *priv)
priv->phylink_config.type = PHYLINK_NETDEV; priv->phylink_config.type = PHYLINK_NETDEV;
priv->phylink_config.mac_managed_pm = true; priv->phylink_config.mac_managed_pm = true;
/* Stmmac always requires an RX clock for hardware initialization */
priv->phylink_config.mac_requires_rxc = true;
mdio_bus_data = priv->plat->mdio_bus_data; mdio_bus_data = priv->plat->mdio_bus_data;
if (mdio_bus_data) if (mdio_bus_data)
priv->phylink_config.ovr_an_inband = priv->phylink_config.ovr_an_inband =
...@@ -3411,6 +3411,10 @@ static int stmmac_hw_setup(struct net_device *dev, bool ptp_register) ...@@ -3411,6 +3411,10 @@ static int stmmac_hw_setup(struct net_device *dev, bool ptp_register)
u32 chan; u32 chan;
int ret; int ret;
/* Make sure RX clock is enabled */
if (priv->hw->phylink_pcs)
phylink_pcs_pre_init(priv->phylink, priv->hw->phylink_pcs);
/* DMA initialization and SW reset */ /* DMA initialization and SW reset */
ret = stmmac_init_dma_engine(priv); ret = stmmac_init_dma_engine(priv);
if (ret < 0) { if (ret < 0) {
...@@ -3960,8 +3964,7 @@ static int __stmmac_open(struct net_device *dev, ...@@ -3960,8 +3964,7 @@ static int __stmmac_open(struct net_device *dev,
if (priv->hw->pcs != STMMAC_PCS_TBI && if (priv->hw->pcs != STMMAC_PCS_TBI &&
priv->hw->pcs != STMMAC_PCS_RTBI && priv->hw->pcs != STMMAC_PCS_RTBI &&
(!priv->hw->xpcs || (!priv->hw->xpcs ||
xpcs_get_an_mode(priv->hw->xpcs, mode) != DW_AN_C73) && xpcs_get_an_mode(priv->hw->xpcs, mode) != DW_AN_C73)) {
!priv->hw->lynx_pcs) {
ret = stmmac_init_phy(dev); ret = stmmac_init_phy(dev);
if (ret) { if (ret) {
netdev_err(priv->dev, netdev_err(priv->dev,
......
...@@ -279,10 +279,38 @@ static int miic_validate(struct phylink_pcs *pcs, unsigned long *supported, ...@@ -279,10 +279,38 @@ static int miic_validate(struct phylink_pcs *pcs, unsigned long *supported,
return -EINVAL; return -EINVAL;
} }
static int miic_pre_init(struct phylink_pcs *pcs)
{
struct miic_port *miic_port = phylink_pcs_to_miic_port(pcs);
struct miic *miic = miic_port->miic;
u32 val, mask;
/* Start RX clock if required */
if (pcs->rxc_always_on) {
/* In MII through mode, the clock signals will be driven by the
* external PHY, which might not be initialized yet. Set RMII
* as default mode to ensure that a reference clock signal is
* generated.
*/
miic_port->interface = PHY_INTERFACE_MODE_RMII;
val = FIELD_PREP(MIIC_CONVCTRL_CONV_MODE, CONV_MODE_RMII) |
FIELD_PREP(MIIC_CONVCTRL_CONV_SPEED, CONV_MODE_100MBPS);
mask = MIIC_CONVCTRL_CONV_MODE | MIIC_CONVCTRL_CONV_SPEED;
miic_reg_rmw(miic, MIIC_CONVCTRL(miic_port->port), mask, val);
miic_converter_enable(miic, miic_port->port, 1);
}
return 0;
}
static const struct phylink_pcs_ops miic_phylink_ops = { static const struct phylink_pcs_ops miic_phylink_ops = {
.pcs_validate = miic_validate, .pcs_validate = miic_validate,
.pcs_config = miic_config, .pcs_config = miic_config,
.pcs_link_up = miic_link_up, .pcs_link_up = miic_link_up,
.pcs_pre_init = miic_pre_init,
}; };
struct phylink_pcs *miic_create(struct device *dev, struct device_node *np) struct phylink_pcs *miic_create(struct device *dev, struct device_node *np)
......
...@@ -1042,6 +1042,21 @@ static void phylink_pcs_poll_start(struct phylink *pl) ...@@ -1042,6 +1042,21 @@ static void phylink_pcs_poll_start(struct phylink *pl)
mod_timer(&pl->link_poll, jiffies + HZ); mod_timer(&pl->link_poll, jiffies + HZ);
} }
int phylink_pcs_pre_init(struct phylink *pl, struct phylink_pcs *pcs)
{
int ret = 0;
/* Signal to PCS driver that MAC requires RX clock for init */
if (pl->config->mac_requires_rxc)
pcs->rxc_always_on = true;
if (pcs->ops->pcs_pre_init)
ret = pcs->ops->pcs_pre_init(pcs);
return ret;
}
EXPORT_SYMBOL_GPL(phylink_pcs_pre_init);
static void phylink_mac_config(struct phylink *pl, static void phylink_mac_config(struct phylink *pl,
const struct phylink_link_state *state) const struct phylink_link_state *state)
{ {
...@@ -1923,6 +1938,8 @@ static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy, ...@@ -1923,6 +1938,8 @@ static int phylink_bringup_phy(struct phylink *pl, struct phy_device *phy,
static int phylink_attach_phy(struct phylink *pl, struct phy_device *phy, static int phylink_attach_phy(struct phylink *pl, struct phy_device *phy,
phy_interface_t interface) phy_interface_t interface)
{ {
u32 flags = 0;
if (WARN_ON(pl->cfg_link_an_mode == MLO_AN_FIXED || if (WARN_ON(pl->cfg_link_an_mode == MLO_AN_FIXED ||
(pl->cfg_link_an_mode == MLO_AN_INBAND && (pl->cfg_link_an_mode == MLO_AN_INBAND &&
phy_interface_mode_is_8023z(interface) && !pl->sfp_bus))) phy_interface_mode_is_8023z(interface) && !pl->sfp_bus)))
...@@ -1931,7 +1948,10 @@ static int phylink_attach_phy(struct phylink *pl, struct phy_device *phy, ...@@ -1931,7 +1948,10 @@ static int phylink_attach_phy(struct phylink *pl, struct phy_device *phy,
if (pl->phydev) if (pl->phydev)
return -EBUSY; return -EBUSY;
return phy_attach_direct(pl->netdev, phy, 0, interface); if (pl->config->mac_requires_rxc)
flags |= PHY_F_RXC_ALWAYS_ON;
return phy_attach_direct(pl->netdev, phy, flags, interface);
} }
/** /**
...@@ -2034,6 +2054,9 @@ int phylink_fwnode_phy_connect(struct phylink *pl, ...@@ -2034,6 +2054,9 @@ int phylink_fwnode_phy_connect(struct phylink *pl,
pl->link_config.interface = pl->link_interface; pl->link_config.interface = pl->link_interface;
} }
if (pl->config->mac_requires_rxc)
flags |= PHY_F_RXC_ALWAYS_ON;
ret = phy_attach_direct(pl->netdev, phy_dev, flags, ret = phy_attach_direct(pl->netdev, phy_dev, flags,
pl->link_interface); pl->link_interface);
phy_device_free(phy_dev); phy_device_free(phy_dev);
......
...@@ -426,7 +426,8 @@ static int at803x_hibernation_mode_config(struct phy_device *phydev) ...@@ -426,7 +426,8 @@ static int at803x_hibernation_mode_config(struct phy_device *phydev)
/* The default after hardware reset is hibernation mode enabled. After /* The default after hardware reset is hibernation mode enabled. After
* software reset, the value is retained. * software reset, the value is retained.
*/ */
if (!(priv->flags & AT803X_DISABLE_HIBERNATION_MODE)) if (!(priv->flags & AT803X_DISABLE_HIBERNATION_MODE) &&
!(phydev->dev_flags & PHY_F_RXC_ALWAYS_ON))
return 0; return 0;
return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_HIB_CTRL, return at803x_debug_reg_mask(phydev, AT803X_DEBUG_REG_HIB_CTRL,
......
...@@ -778,6 +778,7 @@ struct phy_device { ...@@ -778,6 +778,7 @@ struct phy_device {
/* Generic phy_device::dev_flags */ /* Generic phy_device::dev_flags */
#define PHY_F_NO_IRQ 0x80000000 #define PHY_F_NO_IRQ 0x80000000
#define PHY_F_RXC_ALWAYS_ON 0x40000000
static inline struct phy_device *to_phy_device(const struct device *dev) static inline struct phy_device *to_phy_device(const struct device *dev)
{ {
......
...@@ -138,6 +138,9 @@ enum phylink_op_type { ...@@ -138,6 +138,9 @@ enum phylink_op_type {
* @poll_fixed_state: if true, starts link_poll, * @poll_fixed_state: if true, starts link_poll,
* if MAC link is at %MLO_AN_FIXED mode. * if MAC link is at %MLO_AN_FIXED mode.
* @mac_managed_pm: if true, indicate the MAC driver is responsible for PHY PM. * @mac_managed_pm: if true, indicate the MAC driver is responsible for PHY PM.
* @mac_requires_rxc: if true, the MAC always requires a receive clock from PHY.
* The PHY driver should start the clock signal as soon as
* possible and avoid stopping it during suspend events.
* @ovr_an_inband: if true, override PCS to MLO_AN_INBAND * @ovr_an_inband: if true, override PCS to MLO_AN_INBAND
* @get_fixed_state: callback to execute to determine the fixed link state, * @get_fixed_state: callback to execute to determine the fixed link state,
* if MAC link is at %MLO_AN_FIXED mode. * if MAC link is at %MLO_AN_FIXED mode.
...@@ -150,6 +153,7 @@ struct phylink_config { ...@@ -150,6 +153,7 @@ struct phylink_config {
enum phylink_op_type type; enum phylink_op_type type;
bool poll_fixed_state; bool poll_fixed_state;
bool mac_managed_pm; bool mac_managed_pm;
bool mac_requires_rxc;
bool ovr_an_inband; bool ovr_an_inband;
void (*get_fixed_state)(struct phylink_config *config, void (*get_fixed_state)(struct phylink_config *config,
struct phylink_link_state *state); struct phylink_link_state *state);
...@@ -392,6 +396,10 @@ struct phylink_pcs_ops; ...@@ -392,6 +396,10 @@ struct phylink_pcs_ops;
* @phylink: pointer to &struct phylink_config * @phylink: pointer to &struct phylink_config
* @neg_mode: provide PCS neg mode via "mode" argument * @neg_mode: provide PCS neg mode via "mode" argument
* @poll: poll the PCS for link changes * @poll: poll the PCS for link changes
* @rxc_always_on: The MAC driver requires the reference clock
* to always be on. Standalone PCS drivers which
* do not have access to a PHY device can check
* this instead of PHY_F_RXC_ALWAYS_ON.
* *
* This structure is designed to be embedded within the PCS private data, * This structure is designed to be embedded within the PCS private data,
* and will be passed between phylink and the PCS. * and will be passed between phylink and the PCS.
...@@ -404,6 +412,7 @@ struct phylink_pcs { ...@@ -404,6 +412,7 @@ struct phylink_pcs {
struct phylink *phylink; struct phylink *phylink;
bool neg_mode; bool neg_mode;
bool poll; bool poll;
bool rxc_always_on;
}; };
/** /**
...@@ -418,6 +427,8 @@ struct phylink_pcs { ...@@ -418,6 +427,8 @@ struct phylink_pcs {
* @pcs_an_restart: restart 802.3z BaseX autonegotiation. * @pcs_an_restart: restart 802.3z BaseX autonegotiation.
* @pcs_link_up: program the PCS for the resolved link configuration * @pcs_link_up: program the PCS for the resolved link configuration
* (where necessary). * (where necessary).
* @pcs_pre_init: configure PCS components necessary for MAC hardware
* initialization e.g. RX clock for stmmac.
*/ */
struct phylink_pcs_ops { struct phylink_pcs_ops {
int (*pcs_validate)(struct phylink_pcs *pcs, unsigned long *supported, int (*pcs_validate)(struct phylink_pcs *pcs, unsigned long *supported,
...@@ -437,6 +448,7 @@ struct phylink_pcs_ops { ...@@ -437,6 +448,7 @@ struct phylink_pcs_ops {
void (*pcs_an_restart)(struct phylink_pcs *pcs); void (*pcs_an_restart)(struct phylink_pcs *pcs);
void (*pcs_link_up)(struct phylink_pcs *pcs, unsigned int neg_mode, void (*pcs_link_up)(struct phylink_pcs *pcs, unsigned int neg_mode,
phy_interface_t interface, int speed, int duplex); phy_interface_t interface, int speed, int duplex);
int (*pcs_pre_init)(struct phylink_pcs *pcs);
}; };
#if 0 /* For kernel-doc purposes only. */ #if 0 /* For kernel-doc purposes only. */
...@@ -542,6 +554,34 @@ void pcs_an_restart(struct phylink_pcs *pcs); ...@@ -542,6 +554,34 @@ void pcs_an_restart(struct phylink_pcs *pcs);
*/ */
void pcs_link_up(struct phylink_pcs *pcs, unsigned int neg_mode, void pcs_link_up(struct phylink_pcs *pcs, unsigned int neg_mode,
phy_interface_t interface, int speed, int duplex); phy_interface_t interface, int speed, int duplex);
/**
* pcs_pre_init() - Configure PCS components necessary for MAC initialization
* @pcs: a pointer to a &struct phylink_pcs.
*
* This function can be called by MAC drivers through the
* phylink_pcs_pre_init() wrapper, before their hardware is initialized. It
* should not be called after the link is brought up, as reconfiguring the PCS
* at this point could break the link.
*
* Some MAC devices require specific hardware initialization to be performed by
* their associated PCS device before they can properly initialize their own
* hardware. An example of this is the initialization of stmmac controllers,
* which requires an active REF_CLK signal to be provided by the PHY/PCS.
*
* By calling phylink_pcs_pre_init(), MAC drivers can ensure that the PCS is
* setup in a way that allows for successful hardware initialization.
*
* The specific configuration performed by pcs_pre_init() is dependent on the
* model of PCS and the requirements of the MAC device attached to it. PCS
* driver authors should consider whether their target device is to be used in
* conjunction with a MAC device whose driver calls phylink_pcs_pre_init(). MAC
* driver authors should document their requirements for the PCS
* pre-initialization.
*
*/
int pcs_pre_init(struct phylink_pcs *pcs);
#endif #endif
struct phylink *phylink_create(struct phylink_config *, struct phylink *phylink_create(struct phylink_config *,
...@@ -561,6 +601,8 @@ void phylink_disconnect_phy(struct phylink *); ...@@ -561,6 +601,8 @@ void phylink_disconnect_phy(struct phylink *);
void phylink_mac_change(struct phylink *, bool up); void phylink_mac_change(struct phylink *, bool up);
void phylink_pcs_change(struct phylink_pcs *, bool up); void phylink_pcs_change(struct phylink_pcs *, bool up);
int phylink_pcs_pre_init(struct phylink *pl, struct phylink_pcs *pcs);
void phylink_start(struct phylink *); void phylink_start(struct phylink *);
void phylink_stop(struct phylink *); void phylink_stop(struct phylink *);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment