[PATCH net-next v4 2/2] net: phy: micrel: Add PTP support for lan8842

Horatiu Vultur posted 2 patches 1 month ago
There is a newer version of this series
[PATCH net-next v4 2/2] net: phy: micrel: Add PTP support for lan8842
Posted by Horatiu Vultur 1 month ago
It has the same PTP IP block as lan8814, only the number of GPIOs is
different, all the other functionality is the same. So reuse the same
functions as lan8814 for lan8842.
There is a revision of lan8842 called lan8832 which doesn't have the PTP
IP block. So make sure in that case the PTP is not initialized.

Signed-off-by: Horatiu Vultur <horatiu.vultur@microchip.com>
---
 drivers/net/phy/micrel.c | 97 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 97 insertions(+)

diff --git a/drivers/net/phy/micrel.c b/drivers/net/phy/micrel.c
index 9a90818481320..95ba1a2859a9a 100644
--- a/drivers/net/phy/micrel.c
+++ b/drivers/net/phy/micrel.c
@@ -457,6 +457,9 @@ struct lan8842_phy_stats {
 
 struct lan8842_priv {
 	struct lan8842_phy_stats phy_stats;
+	int rev;
+	struct phy_device *phydev;
+	struct kszphy_ptp_priv ptp_priv;
 };
 
 static const struct kszphy_type lan8814_type = {
@@ -5786,6 +5789,17 @@ static int ksz9131_resume(struct phy_device *phydev)
 	return kszphy_resume(phydev);
 }
 
+#define LAN8842_PTP_GPIO_NUM 16
+
+static int lan8842_ptp_probe_once(struct phy_device *phydev)
+{
+	return __lan8814_ptp_probe_once(phydev, "lan8842_ptp_pin",
+					LAN8842_PTP_GPIO_NUM);
+}
+
+#define LAN8842_STRAP_REG			0 /* 0x0 */
+#define LAN8842_STRAP_REG_PHYADDR_MASK		GENMASK(4, 0)
+#define LAN8842_SKU_REG				11 /* 0x0b */
 #define LAN8842_SELF_TEST			14 /* 0x0e */
 #define LAN8842_SELF_TEST_RX_CNT_ENA		BIT(8)
 #define LAN8842_SELF_TEST_TX_CNT_ENA		BIT(4)
@@ -5793,6 +5807,7 @@ static int ksz9131_resume(struct phy_device *phydev)
 static int lan8842_probe(struct phy_device *phydev)
 {
 	struct lan8842_priv *priv;
+	int addr;
 	int ret;
 
 	priv = devm_kzalloc(&phydev->mdio.dev, sizeof(*priv), GFP_KERNEL);
@@ -5800,6 +5815,7 @@ static int lan8842_probe(struct phy_device *phydev)
 		return -ENOMEM;
 
 	phydev->priv = priv;
+	priv->phydev = phydev;
 
 	/* Similar to lan8814 this PHY has a pin which needs to be pulled down
 	 * to enable to pass any traffic through it. Therefore use the same
@@ -5817,6 +5833,41 @@ static int lan8842_probe(struct phy_device *phydev)
 	if (ret < 0)
 		return ret;
 
+	/* Revision lan8832 doesn't have support for PTP, therefore don't add
+	 * any PTP clocks
+	 */
+	priv->rev = lanphy_read_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
+					 LAN8842_SKU_REG);
+	if (priv->rev < 0)
+		return priv->rev;
+	if (priv->rev == 0x8832)
+		return 0;
+
+	/* As the lan8814 and lan8842 has the same IP for the PTP block, the
+	 * only difference is the number of the GPIOs, then make sure that the
+	 * lan8842 initialized also the shared data pointer as this is used in
+	 * all the PTP functions for lan8814. The lan8842 doesn't have multiple
+	 * PHYs in the same package.
+	 */
+	addr = lanphy_read_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
+				    LAN8842_STRAP_REG);
+	if (addr < 0)
+		return addr;
+	addr &= LAN8842_STRAP_REG_PHYADDR_MASK;
+
+	ret = devm_phy_package_join(&phydev->mdio.dev, phydev, addr,
+				    sizeof(struct lan8814_shared_priv));
+	if (ret)
+		return ret;
+
+	if (phy_package_init_once(phydev)) {
+		ret = lan8842_ptp_probe_once(phydev);
+		if (ret)
+			return ret;
+	}
+
+	lan8814_ptp_init(phydev);
+
 	return 0;
 }
 
@@ -5896,8 +5947,34 @@ static int lan8842_config_inband(struct phy_device *phydev, unsigned int modes)
 				      enable ? LAN8814_QSGMII_PCS1G_ANEG_CONFIG_ANEG_ENA : 0);
 }
 
+static void lan8842_handle_ptp_interrupt(struct phy_device *phydev, u16 status)
+{
+	struct kszphy_ptp_priv *ptp_priv;
+	struct lan8842_priv *priv;
+
+	priv = phydev->priv;
+	ptp_priv = &priv->ptp_priv;
+
+	if (status & PTP_TSU_INT_STS_PTP_TX_TS_EN_)
+		lan8814_get_tx_ts(ptp_priv);
+
+	if (status & PTP_TSU_INT_STS_PTP_RX_TS_EN_)
+		lan8814_get_rx_ts(ptp_priv);
+
+	if (status & PTP_TSU_INT_STS_PTP_TX_TS_OVRFL_INT_) {
+		lan8814_flush_fifo(phydev, true);
+		skb_queue_purge(&ptp_priv->tx_queue);
+	}
+
+	if (status & PTP_TSU_INT_STS_PTP_RX_TS_OVRFL_INT_) {
+		lan8814_flush_fifo(phydev, false);
+		skb_queue_purge(&ptp_priv->rx_queue);
+	}
+}
+
 static irqreturn_t lan8842_handle_interrupt(struct phy_device *phydev)
 {
+	struct lan8842_priv *priv = phydev->priv;
 	int ret = IRQ_NONE;
 	int irq_status;
 
@@ -5912,6 +5989,26 @@ static irqreturn_t lan8842_handle_interrupt(struct phy_device *phydev)
 		ret = IRQ_HANDLED;
 	}
 
+	/* Phy revision lan8832 doesn't have support for PTP threrefore there is
+	 * not need to check the PTP and GPIO interrupts
+	 */
+	if (priv->rev == 0x8832)
+		goto out;
+
+	while (true) {
+		irq_status = lanphy_read_page_reg(phydev, LAN8814_PAGE_PORT_REGS,
+						  PTP_TSU_INT_STS);
+		if (!irq_status)
+			break;
+
+		lan8842_handle_ptp_interrupt(phydev, irq_status);
+		ret = IRQ_HANDLED;
+	}
+
+	if (!lan8814_handle_gpio_interrupt(phydev, irq_status))
+		ret = IRQ_HANDLED;
+
+out:
 	return ret;
 }
 
-- 
2.34.1
Re: [PATCH net-next v4 2/2] net: phy: micrel: Add PTP support for lan8842
Posted by Russell King (Oracle) 1 month ago
Hi,

Just a few comments.

On Thu, Aug 28, 2025 at 09:34:25AM +0200, Horatiu Vultur wrote:
> diff --git a/drivers/net/phy/micrel.c b/drivers/net/phy/micrel.c
> index 9a90818481320..95ba1a2859a9a 100644
> --- a/drivers/net/phy/micrel.c
> +++ b/drivers/net/phy/micrel.c
> @@ -457,6 +457,9 @@ struct lan8842_phy_stats {
>  
>  struct lan8842_priv {
>  	struct lan8842_phy_stats phy_stats;
> +	int rev;

Maybe make this a u16 and move it at the end of the struct?

> +	struct phy_device *phydev;

Why do you need this member? In this patch, you initialise this, but
never use it.

> @@ -5817,6 +5833,41 @@ static int lan8842_probe(struct phy_device *phydev)
>  	if (ret < 0)
>  		return ret;
>  
> +	/* Revision lan8832 doesn't have support for PTP, therefore don't add
> +	 * any PTP clocks
> +	 */
> +	priv->rev = lanphy_read_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
> +					 LAN8842_SKU_REG);
> +	if (priv->rev < 0)
> +		return priv->rev;

	ret = lanphy_read_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
				   LAN8842_SKU_REG);
	if (ret < 0)
		return ret;

	priv->rev = ret;

Thanks.

-- 
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!
Re: [PATCH net-next v4 2/2] net: phy: micrel: Add PTP support for lan8842
Posted by Horatiu Vultur 1 month ago
The 08/28/2025 09:27, Russell King (Oracle) wrote:
> Hi,

Hi Russell,

> 
> Just a few comments.
> 
> On Thu, Aug 28, 2025 at 09:34:25AM +0200, Horatiu Vultur wrote:
> > diff --git a/drivers/net/phy/micrel.c b/drivers/net/phy/micrel.c
> > index 9a90818481320..95ba1a2859a9a 100644
> > --- a/drivers/net/phy/micrel.c
> > +++ b/drivers/net/phy/micrel.c
> > @@ -457,6 +457,9 @@ struct lan8842_phy_stats {
> >
> >  struct lan8842_priv {
> >       struct lan8842_phy_stats phy_stats;
> > +     int rev;
> 
> Maybe make this a u16 and move it at the end of the struct?

Yes, I can make it u16 and move it at the end.
Just for understanding, do you want to move it at the end not to have
any holes in the struct?

> 
> > +     struct phy_device *phydev;
> 
> Why do you need this member? In this patch, you initialise this, but
> never use it.

Actually is not needed, I forgot to remove it before sending the first
version.

> 
> > @@ -5817,6 +5833,41 @@ static int lan8842_probe(struct phy_device *phydev)
> >       if (ret < 0)
> >               return ret;
> >
> > +     /* Revision lan8832 doesn't have support for PTP, therefore don't add
> > +      * any PTP clocks
> > +      */
> > +     priv->rev = lanphy_read_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
> > +                                      LAN8842_SKU_REG);
> > +     if (priv->rev < 0)
> > +             return priv->rev;
> 
>         ret = lanphy_read_page_reg(phydev, LAN8814_PAGE_COMMON_REGS,
>                                    LAN8842_SKU_REG);
>         if (ret < 0)
>                 return ret;
> 
>         priv->rev = ret;

Yes, I will update it like you suggested in the next version.

> 
> Thanks.
> 
> --
> RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
> FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!

-- 
/Horatiu
Re: [PATCH net-next v4 2/2] net: phy: micrel: Add PTP support for lan8842
Posted by Russell King (Oracle) 1 month ago
On Fri, Aug 29, 2025 at 08:26:54AM +0200, Horatiu Vultur wrote:
> The 08/28/2025 09:27, Russell King (Oracle) wrote:
> > On Thu, Aug 28, 2025 at 09:34:25AM +0200, Horatiu Vultur wrote:
> > > @@ -457,6 +457,9 @@ struct lan8842_phy_stats {
> > >
> > >  struct lan8842_priv {
> > >       struct lan8842_phy_stats phy_stats;
> > > +     int rev;
> > 
> > Maybe make this a u16 and move it at the end of the struct?
> 
> Yes, I can make it u16 and move it at the end.
> Just for understanding, do you want to move it at the end not to have
> any holes in the struct?

Correct. It makes sense to avoid holes where it doesn't detract from
readability.

Thanks.

-- 
RMK's Patch system: https://www.armlinux.org.uk/developer/patches/
FTTP is here! 80Mbps down 10Mbps up. Decent connectivity at last!