From patchwork Wed Apr 17 16:43:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oleksij Rempel X-Patchwork-Id: 13633598 X-Patchwork-Delegate: kuba@kernel.org Received: from metis.whiteo.stw.pengutronix.de (metis.whiteo.stw.pengutronix.de [185.203.201.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 97D57171E4C for ; Wed, 17 Apr 2024 16:43:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.201.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713372218; cv=none; b=mjotO1Zf3QWaZhR+hU8jn/v3p3KsZJQFLk/YszWLM9bO/Fseb64HETh7wahV2WMIyyj4bwILXduSMDu2C0m44cFSwwcE5F0mY3bu1ufKtu9/m5JkCj6mUWyiss5ymZK/SoIgcwRbNl22LkBdFtIgWjNmoQLay3vTgEzf0Kisp44= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713372218; c=relaxed/simple; bh=6vR2Y9mcdebA3zHhYniV/UzkOiXQ5ppDzZC/ouF26vM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=mg+69sCt0TiawIPmETnxLtXnNvfugfE8PMUkB2UsEgm0d6qVhaL4ddAuSbBBF9wwUxbNAejhxTf+DG6Q7baIBEe0E9NW+uYsCURctFl4DHn02QAL28UwsuZ/XnNEHqkwelXmZdGDDkn2KpcTB0GdKyqukl7YIz4aSJnEJx7eDZM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.201.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.whiteo.stw.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1rx8NT-0007N0-VQ; Wed, 17 Apr 2024 18:43:19 +0200 Received: from [2a0a:edc0:0:1101:1d::ac] (helo=dude04.red.stw.pengutronix.de) by drehscheibe.grey.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rx8NR-00CpCa-PX; Wed, 17 Apr 2024 18:43:17 +0200 Received: from ore by dude04.red.stw.pengutronix.de with local (Exim 4.96) (envelope-from ) id 1rx8NR-007Mds-2J; Wed, 17 Apr 2024 18:43:17 +0200 From: Oleksij Rempel To: Alexandre Torgue , Jose Abreu , "David S. Miller" , Andrew Lunn , Heiner Kallweit , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Maxime Coquelin , Woojung Huh , Arun Ramadoss , Richard Cochran , Russell King Cc: Oleksij Rempel , kernel@pengutronix.de, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, UNGLinuxDriver@microchip.com, linux-stm32@st-md-mailman.stormreply.com Subject: [PATCH net-next v1 1/4] net: phy: Add TimeSync delay query support to PHYlib API Date: Wed, 17 Apr 2024 18:43:13 +0200 Message-Id: <20240417164316.1755299-2-o.rempel@pengutronix.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240417164316.1755299-1-o.rempel@pengutronix.de> References: <20240417164316.1755299-1-o.rempel@pengutronix.de> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-SA-Exim-Connect-IP: 2a0a:edc0:0:c01:1d::a2 X-SA-Exim-Mail-From: ore@pengutronix.de X-SA-Exim-Scanned: No (on metis.whiteo.stw.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org Add a new phy_get_timesync_data_path_delays() function, to the PHY device API. This function enables querying the ingress and egress TimeSync delays for PHY devices, as specified in IEEE 802.3-2022 sections 30.13.1.3 to 30.13.1.6. The function adds the capability to obtain the average delays in nanoseconds, which can be used to compensate for time variations added by the PHY to PTP packets. Since most PHYs do not provide register-based delay information, PHY drivers should supply this data, typically dependent on the interface type (MII, RGMII, etc.) and link speed. The MAC driver, or consumer of this API, is expected to invoke this function upon link establishment to accurately compensate for any PHY-induced time discrepancies. Compensating for this delay is crucial. If it is not addressed, the PHY delays may exceed the 800ns threshold set by the 802.1as standard (the gPTP standard). This situation classifies the link as a gPTP domain boundary and excludes the device from synchronization processes. While some switches and devices allow configurations that exceed this threshold, such adjustments are non-compliant with the standard and may not operate seamlessly across different devices or configurations. Additionally, addressing the path delay asymmetry is vital. Identical PHYs may not exhibit noticeable asymmetry impacting PTP time offset; however, different PHY types and vendors can introduce significant asymmetries that require manual adjustment for each device. Signed-off-by: Oleksij Rempel --- drivers/net/phy/phy_device.c | 57 ++++++++++++++++++++++++++++++++++++ include/linux/phy.h | 31 ++++++++++++++++++++ 2 files changed, 88 insertions(+) diff --git a/drivers/net/phy/phy_device.c b/drivers/net/phy/phy_device.c index 616bd7ba46cbf..3ded9280ab831 100644 --- a/drivers/net/phy/phy_device.c +++ b/drivers/net/phy/phy_device.c @@ -3216,6 +3216,63 @@ s32 phy_get_internal_delay(struct phy_device *phydev, struct device *dev, } EXPORT_SYMBOL(phy_get_internal_delay); +/** + * phy_get_timesync_data_path_delays - get the TimeSync data path ingress/egress + * delays + * @phydev: phy_device struct + * @tx_delay_ns: pointer to the transmit delay in nanoseconds + * @rx_delay_ns: pointer to the receive delay in nanoseconds + * + * This function is used to get the TimeSync data path ingress/egress delays + * as described in IEEE 802.3-2022 sections: + * 30.13.1.3 aTimeSyncDelayTXmax, 30.13.1.4 aTimeSyncDelayTXmin, + * 30.13.1.5 aTimeSyncDelayRXmax and 30.13.1.6 aTimeSyncDelayRXmin. + * + * The delays are returned in nanoseconds and can be used to compensate time + * added by the PHY to the PTP packets. + * + * Returns 0 on success, negative value on failure. + */ +int phy_get_timesync_data_path_delays(struct phy_device *phydev, + u64 *tx_delay_ns, u64 *rx_delay_ns) +{ + struct phy_timesync_delay tsd = { 0 }; + int err; + + if (!phydev->drv->get_timesync_data_path_delays) + return -EOPNOTSUPP; + + if (!tx_delay_ns || !rx_delay_ns) + return -EINVAL; + + err = phydev->drv->get_timesync_data_path_delays(phydev, &tsd); + if (err) + return err; + + if ((!tsd.tx_max_delay_ns && !tsd.tx_min_delay_ns) || + (!tsd.rx_max_delay_ns && !tsd.rx_min_delay_ns)) { + phydev_err(phydev, "Invalid TimeSync data path delays\n"); + return -EINVAL; + } + + if (tsd.tx_max_delay_ns && tsd.tx_min_delay_ns) + *tx_delay_ns = (tsd.tx_max_delay_ns + tsd.tx_min_delay_ns) / 2; + else if (tsd.tx_max_delay_ns) + *tx_delay_ns = tsd.tx_max_delay_ns; + else + *tx_delay_ns = tsd.tx_min_delay_ns; + + if (tsd.rx_max_delay_ns && tsd.rx_min_delay_ns) + *rx_delay_ns = (tsd.rx_max_delay_ns + tsd.rx_min_delay_ns) / 2; + else if (tsd.rx_max_delay_ns) + *rx_delay_ns = tsd.rx_max_delay_ns; + else + *rx_delay_ns = tsd.rx_min_delay_ns; + + return 0; +} +EXPORT_SYMBOL(phy_get_timesync_data_path_delays); + static int phy_led_set_brightness(struct led_classdev *led_cdev, enum led_brightness value) { diff --git a/include/linux/phy.h b/include/linux/phy.h index 3ddfe7fe781aa..6021e3c6cebb2 100644 --- a/include/linux/phy.h +++ b/include/linux/phy.h @@ -893,6 +893,24 @@ struct phy_led { #define to_phy_led(d) container_of(d, struct phy_led, led_cdev) +/** + * struct phy_timesync_delay - PHY time sync delay values + * @tx_max_delay_ns: Maximum delay for transmit path in nanoseconds. Corresponds + * to IEEE 802.3 2022 - 30.13.1.3 aTimeSyncDelayTXmax. + * @tx_min_delay_ns: Minimum delay for transmit path in nanoseconds. Corresponds + * to IEEE 802.3 2022 - 30.13.1.4 aTimeSyncDelayTXmin. + * @rx_max_delay_ns: Maximum delay for receive path in nanoseconds. Corresponds + * to IEEE 802.3 2022 - 30.13.1.5 aTimeSyncDelayRXmax + * @rx_min_delay_ns: Minimum delay for receive path in nanoseconds. Corresponds + * to IEEE 802.3 2022 - 30.13.1.6 aTimeSyncDelayRXmin. + */ +struct phy_timesync_delay { + u64 tx_max_delay_ns; + u64 tx_min_delay_ns; + u64 rx_max_delay_ns; + u64 rx_min_delay_ns; +}; + /** * struct phy_driver - Driver structure for a particular PHY type * @@ -1182,6 +1200,16 @@ struct phy_driver { */ int (*led_polarity_set)(struct phy_device *dev, int index, unsigned long modes); + + /** + * @get_timesync_data_path_delays: Get the PHY time sync delay values + * @dev: PHY device + * @tsd: PHY time sync delay values + * + * Returns 0 on success, or an error code. + */ + int (*get_timesync_data_path_delays)(struct phy_device *dev, + struct phy_timesync_delay *tsd); }; #define to_phy_driver(d) container_of(to_mdio_common_driver(d), \ struct phy_driver, mdiodrv) @@ -1991,6 +2019,9 @@ void phy_get_pause(struct phy_device *phydev, bool *tx_pause, bool *rx_pause); s32 phy_get_internal_delay(struct phy_device *phydev, struct device *dev, const int *delay_values, int size, bool is_rx); +int phy_get_timesync_data_path_delays(struct phy_device *phydev, + u64 *tx_delay_ns, u64 *rx_delay_ns); + void phy_resolve_pause(unsigned long *local_adv, unsigned long *partner_adv, bool *tx_pause, bool *rx_pause); From patchwork Wed Apr 17 16:43:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oleksij Rempel X-Patchwork-Id: 13633597 X-Patchwork-Delegate: kuba@kernel.org Received: from metis.whiteo.stw.pengutronix.de (metis.whiteo.stw.pengutronix.de [185.203.201.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1F87F171677 for ; Wed, 17 Apr 2024 16:43:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.201.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713372217; cv=none; b=rKgsABYK99f28FNurlCNUltu7U958imE1a1Y16qveB7MICGs8MtEFBUTPNzQUSBVBLBU+AEux+Q6LWkfzQNrEbvx2n2ZqMBuaZcG5D8C+bBBZPPwhZvLAAFfro7n1G13kaLoPpO4WOgMfd/tW9ssfIW4hxTKYN8eMB5f7Gq7kHw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713372217; c=relaxed/simple; bh=V5GIDFcd7puL39RgacgdDIHX1FFltse/qW+4H1jXSA0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=FU3kTLV9EOZjfGbsph5OPzeT5JJKit9MFgrTb0N0B7OsLYvIDGLKkyguoFrDRJA6wdIx7IJ7a48k5JhR/Uay6ptcTlElPeiEYzM6inTaEUa0snobWUHZZTME06/OnGO+uPO6fOWuA3PNuoTXlK7Shh4Ms/tyqGKcdRpV100icTA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.201.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.whiteo.stw.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1rx8NT-0007N1-VQ; Wed, 17 Apr 2024 18:43:19 +0200 Received: from [2a0a:edc0:0:1101:1d::ac] (helo=dude04.red.stw.pengutronix.de) by drehscheibe.grey.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rx8NR-00CpCb-QE; Wed, 17 Apr 2024 18:43:17 +0200 Received: from ore by dude04.red.stw.pengutronix.de with local (Exim 4.96) (envelope-from ) id 1rx8NR-007Me2-2O; Wed, 17 Apr 2024 18:43:17 +0200 From: Oleksij Rempel To: Alexandre Torgue , Jose Abreu , "David S. Miller" , Andrew Lunn , Heiner Kallweit , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Maxime Coquelin , Woojung Huh , Arun Ramadoss , Richard Cochran , Russell King Cc: Oleksij Rempel , kernel@pengutronix.de, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, UNGLinuxDriver@microchip.com, linux-stm32@st-md-mailman.stormreply.com Subject: [PATCH net-next v1 2/4] net: phy: micrel: lan8841: set default PTP latency values Date: Wed, 17 Apr 2024 18:43:14 +0200 Message-Id: <20240417164316.1755299-3-o.rempel@pengutronix.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240417164316.1755299-1-o.rempel@pengutronix.de> References: <20240417164316.1755299-1-o.rempel@pengutronix.de> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-SA-Exim-Connect-IP: 2a0a:edc0:0:c01:1d::a2 X-SA-Exim-Mail-From: ore@pengutronix.de X-SA-Exim-Scanned: No (on metis.whiteo.stw.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org Set default PTP latency values to provide realistic path delay measurements and reflecting internal PHY latency asymetry. This values are based on ptp4l measurements for the path delay against identical PHY as link partner and latency asymmetry extracted from documented SOF Latency values of this PHY. Documented SOF Latency values are: TX 138ns/RX 430ns @ 1000Mbps TX 140ns/RX 615ns @ 100Mbps (fixed latency mode) TX 140ns/RX 488-524ns @ 100Mbps (variable latency mode) TX 654ns/227-2577ns @ 10Mbps Calculated asymmetry: 292ns @ 1000Mbps 238ns @ 100Mbps 1923ns @ 10Mbps Except of ptp4l based tests RGMII-PHY-PHY-RGMII path delay was measured to verify if values are in sane range. Following LAN8841 + LAN8841 RGMII delays are measured: 583ns @ 1000Mbps 1080ns @ 100Mbps 15200ns @ 10Mbps Without configuring compensation registers ptp4l reported following path delay results: ~467ns @ 1000Mbps ~544ns @ 100Mbps ~9688ns @ 10Mbps Magnetic + Cable + Magnetic delay in this setup is about 5ns. Signed-off-by: Oleksij Rempel --- drivers/net/phy/micrel.c | 55 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/drivers/net/phy/micrel.c b/drivers/net/phy/micrel.c index ddb50a0e2bc82..5831706e81623 100644 --- a/drivers/net/phy/micrel.c +++ b/drivers/net/phy/micrel.c @@ -3405,6 +3405,20 @@ static int lan8814_probe(struct phy_device *phydev) #define LAN8841_BTRX_POWER_DOWN_BTRX_CH_C BIT(5) #define LAN8841_BTRX_POWER_DOWN_BTRX_CH_D BIT(7) #define LAN8841_ADC_CHANNEL_MASK 198 +#define LAN8841_PTP_RX_LATENCY_10M 328 +#define LAN8841_PTP_TX_LATENCY_10M 329 +#define LAN8841_PTP_RX_LATENCY_100M 330 +#define LAN8841_PTP_TX_LATENCY_100M 331 +#define LAN8841_PTP_RX_LATENCY_1000M 332 +#define LAN8841_PTP_TX_LATENCY_1000M 333 + +#define LAN8841_PTP_RX_LATENCY_10M_VAL 5803 +#define LAN8841_PTP_TX_LATENCY_10M_VAL 3880 +#define LAN8841_PTP_RX_LATENCY_100M_VAL 443 +#define LAN8841_PTP_TX_LATENCY_100M_VAL 95 +#define LAN8841_PTP_RX_LATENCY_1000M_VAL 377 +#define LAN8841_PTP_TX_LATENCY_1000M_VAL 85 + #define LAN8841_PTP_RX_PARSE_L2_ADDR_EN 370 #define LAN8841_PTP_RX_PARSE_IP_ADDR_EN 371 #define LAN8841_PTP_RX_VERSION 374 @@ -3421,6 +3435,45 @@ static int lan8814_probe(struct phy_device *phydev) #define LAN8841_PTP_INSERT_TS_EN BIT(0) #define LAN8841_PTP_INSERT_TS_32BIT BIT(1) +static int lan8841_ptp_latency_init(struct phy_device *phydev) +{ + int ret; + + ret = phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, + LAN8841_PTP_RX_LATENCY_10M, + LAN8841_PTP_RX_LATENCY_10M_VAL); + if (ret) + return ret; + + ret = phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, + LAN8841_PTP_TX_LATENCY_10M, + LAN8841_PTP_TX_LATENCY_10M_VAL); + if (ret) + return ret; + + ret = phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, + LAN8841_PTP_RX_LATENCY_100M, + LAN8841_PTP_RX_LATENCY_100M_VAL); + if (ret) + return ret; + + ret = phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, + LAN8841_PTP_TX_LATENCY_100M, + LAN8841_PTP_TX_LATENCY_100M_VAL); + if (ret) + return ret; + + ret = phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, + LAN8841_PTP_RX_LATENCY_1000M, + LAN8841_PTP_RX_LATENCY_1000M_VAL); + if (ret) + return ret; + + return phy_write_mmd(phydev, KSZ9131RN_MMD_COMMON_CTRL_REG, + LAN8841_PTP_TX_LATENCY_1000M, + LAN8841_PTP_TX_LATENCY_1000M_VAL); +} + static int lan8841_config_init(struct phy_device *phydev) { int ret; @@ -3500,7 +3553,7 @@ static int lan8841_config_init(struct phy_device *phydev) LAN8841_MMD0_REGISTER_17_DROP_OPT(2) | LAN8841_MMD0_REGISTER_17_XMIT_TOG_TX_DIS); - return 0; + return lan8841_ptp_latency_init(phydev); } #define LAN8841_OUTPUT_CTRL 25 From patchwork Wed Apr 17 16:43:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oleksij Rempel X-Patchwork-Id: 13633596 X-Patchwork-Delegate: kuba@kernel.org Received: from metis.whiteo.stw.pengutronix.de (metis.whiteo.stw.pengutronix.de [185.203.201.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1F41414882F for ; Wed, 17 Apr 2024 16:43:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.201.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713372216; cv=none; b=EDZZAWCByZhO0yBen6+0DyawMylfvFIo1zPP3+ALH1C5oV9wbDl2KPfDLaFg21LLd7cKJyNPeS8wl/HvuYVMuDX8S6gLS/6sb8rN3ps6DykcNa5GT7zK0y0HtuN0GKHwLMJClFebbi6f2GCJvScY6NR/v8jPahF/axmf7SjShpI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713372216; c=relaxed/simple; bh=msyoR8psGA34dyrl4EIM/ln6JTeFBgyF/vyuhfWSiuQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=n5Qjit/luV2t9s2f/qPbDIJEq/nsbv/QwFhba9AC6ibgefvYJrqQ3Plyh+pUaLIbw3aHSJrA3/G67xipc5OfquF7FaplzWCTxFCPMNZhUruxscFNdszw6StDPDgTuXdNwS5OF1pm+8tWF2uzsHM6MzVg4xTRsA0h8dn9yiAcoEQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.201.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.whiteo.stw.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1rx8NT-0007N2-VR; Wed, 17 Apr 2024 18:43:19 +0200 Received: from [2a0a:edc0:0:1101:1d::ac] (helo=dude04.red.stw.pengutronix.de) by drehscheibe.grey.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rx8NR-00CpCc-RJ; Wed, 17 Apr 2024 18:43:17 +0200 Received: from ore by dude04.red.stw.pengutronix.de with local (Exim 4.96) (envelope-from ) id 1rx8NR-007MeC-2S; Wed, 17 Apr 2024 18:43:17 +0200 From: Oleksij Rempel To: Alexandre Torgue , Jose Abreu , "David S. Miller" , Andrew Lunn , Heiner Kallweit , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Maxime Coquelin , Woojung Huh , Arun Ramadoss , Richard Cochran , Russell King Cc: Oleksij Rempel , kernel@pengutronix.de, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, UNGLinuxDriver@microchip.com, linux-stm32@st-md-mailman.stormreply.com Subject: [PATCH net-next v1 3/4] net: phy: realtek: provide TimeSync data path delays for RTL8211E Date: Wed, 17 Apr 2024 18:43:15 +0200 Message-Id: <20240417164316.1755299-4-o.rempel@pengutronix.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240417164316.1755299-1-o.rempel@pengutronix.de> References: <20240417164316.1755299-1-o.rempel@pengutronix.de> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-SA-Exim-Connect-IP: 2a0a:edc0:0:c01:1d::a2 X-SA-Exim-Mail-From: ore@pengutronix.de X-SA-Exim-Scanned: No (on metis.whiteo.stw.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org Provide default data path delays for RTL8211E. The measurements was done against with iMX8MP STMMAC and LAN8841 as the link partner. This values was calculated based on RGMII-PHY-PHY-RGMII measurements, where the link partner is LAN8841. Following values was measured: - data flow from RTL8211E to LAN8841: 746ns @ 1000Mbps 1770ns @ 100Mbps 932000ns @ 10Mbps - data flow from LAN8841 to RTL8211E: 594ns @ 1000Mbps 1130ns @ 100Mbps 8920ns @ 10Mbps Before this patch ptp4l reported following path delays: ~610ns @ 1000Mbps ~942ns @ 100Mbps ~465998ns @ 10Mbps PPS offset compared to grand master was: ~ -114ns @ 1000Mbps ~ -215ns @ 100Mbps ~ -465998ns @ 10Mbps Magnetic - Cable - Magnetic - delay in this setup was about 5ns. Signed-off-by: Oleksij Rempel --- drivers/net/phy/realtek.c | 42 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/drivers/net/phy/realtek.c b/drivers/net/phy/realtek.c index 1fa70427b2a26..e39fec8d166b9 100644 --- a/drivers/net/phy/realtek.c +++ b/drivers/net/phy/realtek.c @@ -221,6 +221,47 @@ static int rtl8211e_config_intr(struct phy_device *phydev) return err; } +static int rtl8211e_get_timesync_data_path_delays(struct phy_device *phydev, + struct phy_timesync_delay *tsd) +{ + phydev_warn(phydev, "Time stamping is not supported\n"); + + switch (phydev->interface) { + case PHY_INTERFACE_MODE_RGMII: + case PHY_INTERFACE_MODE_RGMII_RXID: + case PHY_INTERFACE_MODE_RGMII_TXID: + case PHY_INTERFACE_MODE_RGMII_ID: + /* The values are measured with RTL8211E and LAN8841 as link + * partners and confirmed with i211 to be in sane range. + */ + if (phydev->speed == SPEED_1000) { + tsd->tx_min_delay_ns = 326; + tsd->rx_min_delay_ns = 406; + return 0; + } else if (phydev->speed == SPEED_100) { + tsd->tx_min_delay_ns = 703; + tsd->rx_min_delay_ns = 621; + return 0; + } else if (phydev->speed == SPEED_10) { + /* This value is suspiciously big, with atypical + * shift to Egress side. This value is confirmed + * by measuring RGMII-PHY-PHY-RGMII path delay. + * Similar results are confirmed with LAN8841 and i211 + * as link partners. + */ + tsd->tx_min_delay_ns = 920231; + tsd->rx_min_delay_ns = 1674; + return 0; + } + default: + break; + } + + phydev_warn(phydev, "Not tested or not supported modes for path delay values\n"); + + return -EOPNOTSUPP; +} + static int rtl8211f_config_intr(struct phy_device *phydev) { u16 val; @@ -935,6 +976,7 @@ static struct phy_driver realtek_drvs[] = { .resume = genphy_resume, .read_page = rtl821x_read_page, .write_page = rtl821x_write_page, + .get_timesync_data_path_delays = rtl8211e_get_timesync_data_path_delays, }, { PHY_ID_MATCH_EXACT(0x001cc916), .name = "RTL8211F Gigabit Ethernet", From patchwork Wed Apr 17 16:43:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oleksij Rempel X-Patchwork-Id: 13633595 X-Patchwork-Delegate: kuba@kernel.org Received: from metis.whiteo.stw.pengutronix.de (metis.whiteo.stw.pengutronix.de [185.203.201.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DCF0B171646 for ; Wed, 17 Apr 2024 16:43:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.203.201.7 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713372216; cv=none; b=P6v8S97tQ7TnW3k4ixPzzpMHJgNTXLpV7nrc33BQcKnltyhbTSpxyu819rnBG0P4NkaQFpBi/+bz2hSDDzyiACrKiCx2+rE3nMHPzeYSZQabzFIJFslrSmbjUEN7Do+y+JsUWgQgt5poJfAhODnYqylf5TUn4IUYYW08JDE26U4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713372216; c=relaxed/simple; bh=Z9l2/soyE5tzoITheZvO69xxT5aKufMAHBzJHx8l/e0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=V4gHVyDys3BiLZJj3+iZSOzsfZskPuQfZ0eXvx5Z//TXNUoTZN46BMcHl8dhoEzeErag0kNGJSoxWN40Ice3dVheAGoPzcgqhuZ48oShrpOF40cYEQjwePJtzA8Zs4vN7XRNv0srHNbMb5JsDcrWnINXxAD1u8dpQ1peS7CPp+g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de; spf=pass smtp.mailfrom=pengutronix.de; arc=none smtp.client-ip=185.203.201.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de Received: from drehscheibe.grey.stw.pengutronix.de ([2a0a:edc0:0:c01:1d::a2]) by metis.whiteo.stw.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1rx8NT-0007N3-VQ; Wed, 17 Apr 2024 18:43:19 +0200 Received: from [2a0a:edc0:0:1101:1d::ac] (helo=dude04.red.stw.pengutronix.de) by drehscheibe.grey.stw.pengutronix.de with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rx8NR-00CpCe-S0; Wed, 17 Apr 2024 18:43:17 +0200 Received: from ore by dude04.red.stw.pengutronix.de with local (Exim 4.96) (envelope-from ) id 1rx8NR-007MeM-2W; Wed, 17 Apr 2024 18:43:17 +0200 From: Oleksij Rempel To: Alexandre Torgue , Jose Abreu , "David S. Miller" , Andrew Lunn , Heiner Kallweit , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Maxime Coquelin , Woojung Huh , Arun Ramadoss , Richard Cochran , Russell King Cc: Oleksij Rempel , kernel@pengutronix.de, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, UNGLinuxDriver@microchip.com, linux-stm32@st-md-mailman.stormreply.com Subject: [PATCH net-next v1 4/4] net: stmmac: use delays reported by the PHY driver to correct MAC propagation delay Date: Wed, 17 Apr 2024 18:43:16 +0200 Message-Id: <20240417164316.1755299-5-o.rempel@pengutronix.de> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240417164316.1755299-1-o.rempel@pengutronix.de> References: <20240417164316.1755299-1-o.rempel@pengutronix.de> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-SA-Exim-Connect-IP: 2a0a:edc0:0:c01:1d::a2 X-SA-Exim-Mail-From: ore@pengutronix.de X-SA-Exim-Scanned: No (on metis.whiteo.stw.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: netdev@vger.kernel.org X-Patchwork-Delegate: kuba@kernel.org Now after PHY drivers are able to report data path delays, we can use them in the MAC drivers for the delay correction. Signed-off-by: Oleksij Rempel --- drivers/net/ethernet/stmicro/stmmac/stmmac.h | 2 ++ .../ethernet/stmicro/stmmac/stmmac_hwtstamp.c | 4 ++++ .../net/ethernet/stmicro/stmmac/stmmac_main.c | 17 ++++++++++++++++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/drivers/net/ethernet/stmicro/stmmac/stmmac.h b/drivers/net/ethernet/stmicro/stmmac/stmmac.h index dddcaa9220cc3..6db54ce33ea72 100644 --- a/drivers/net/ethernet/stmicro/stmmac/stmmac.h +++ b/drivers/net/ethernet/stmicro/stmmac/stmmac.h @@ -288,6 +288,8 @@ struct stmmac_priv { u32 sub_second_inc; u32 systime_flags; u32 adv_ts; + u64 phy_tx_delay_ns; + u64 phy_rx_delay_ns; int use_riwt; int irq_wake; rwlock_t ptp_lock; diff --git a/drivers/net/ethernet/stmicro/stmmac/stmmac_hwtstamp.c b/drivers/net/ethernet/stmicro/stmmac/stmmac_hwtstamp.c index f05bd757dfe52..bbf284cb7cc2a 100644 --- a/drivers/net/ethernet/stmicro/stmmac/stmmac_hwtstamp.c +++ b/drivers/net/ethernet/stmicro/stmmac/stmmac_hwtstamp.c @@ -71,6 +71,8 @@ static void hwtstamp_correct_latency(struct stmmac_priv *priv) /* MAC-internal ingress latency */ scaled_ns = readl(ioaddr + PTP_TS_INGR_LAT); + scaled_ns += priv->phy_rx_delay_ns << 16; + /* See section 11.7.2.5.3.1 "Ingress Correction" on page 4001 of * i.MX8MP Applications Processor Reference Manual Rev. 1, 06/2021 */ @@ -95,6 +97,8 @@ static void hwtstamp_correct_latency(struct stmmac_priv *priv) /* MAC-internal egress latency */ scaled_ns = readl(ioaddr + PTP_TS_EGR_LAT); + scaled_ns += priv->phy_tx_delay_ns << 16; + reg_tsec = scaled_ns >> 16; reg_tsecsns = scaled_ns & 0xff00; diff --git a/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c b/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c index fe3498e86de9d..30c7c278b7062 100644 --- a/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +++ b/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c @@ -1097,8 +1097,23 @@ static void stmmac_mac_link_up(struct phylink_config *config, if (priv->dma_cap.fpesel) stmmac_fpe_link_state_handle(priv, true); - if (priv->plat->flags & STMMAC_FLAG_HWTSTAMP_CORRECT_LATENCY) + if (priv->plat->flags & STMMAC_FLAG_HWTSTAMP_CORRECT_LATENCY) { + int ret = 0; + + if (phy) + ret = phy_get_timesync_data_path_delays(phy, + &priv->phy_tx_delay_ns, + &priv->phy_rx_delay_ns); + if (!phy || ret) { + if (ret != -EOPNOTSUPP) + netdev_err(priv->dev, "Failed to get PHY delay: %pe\n", + ERR_PTR(ret)); + priv->phy_tx_delay_ns = 0; + priv->phy_rx_delay_ns = 0; + } + stmmac_hwtstamp_correct_latency(priv, priv); + } } static const struct phylink_mac_ops stmmac_phylink_mac_ops = {