Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp1604016rwb; Thu, 19 Jan 2023 12:55:54 -0800 (PST) X-Google-Smtp-Source: AMrXdXvz9kC/7tSj/j1/Vl9ha1hxk6hCJVIBaN7WTmP3y3vZRIl54aPcRxaQvAbBdjYPY0mWNvez X-Received: by 2002:a17:907:d043:b0:870:2cc7:e8df with SMTP id vb3-20020a170907d04300b008702cc7e8dfmr8351220ejc.8.1674161754507; Thu, 19 Jan 2023 12:55:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674161754; cv=none; d=google.com; s=arc-20160816; b=GF5GhoH0TZbsgsO+ukwRwgKaIU5CsdfsKHCIsvqbskWqQTco31wlXlV0k25mNQ9oNm 6yJB+DITe/BRmSmPJNX1h7jrA9lX8eGtLM0beLBCpN8g5s0eiICa1UfH0FJT4aboPDom /ZGRC6KIIDGR/BhSONFcv2JVj7N5czMlg0lPkYee3TC8Mfl+cQpoZ1WIpLPJ1l41id18 8r5sWI6J2MG49+MGGnx8OX/EsFRxgo5wwJefoWTKt19GtI0RqIVKwbLhuaS2tIpnFwLX fD3t9W3SNvKRAjXis1E+VcCBjIVXkN4Q1ylfT64Nbj00FxbweYbQKg+T+2W8TTLHN+zZ 5nsw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:message-id:date:subject:cc:to:from; bh=hX2eQ/aYfMsKWZuEANkDOKD9S3hrwA5jsafVpyXcFc0=; b=jWhc9dgtuHRwVsW0JdQTvft4UE4DRTfcufy/mLQOiWGFHJHSgOHb5BbWAuGkBmn7St 1lqDTYPcDX/itRFRPHYJXGIABWCt4X21503t8i8BfAKmTWV3UO8/6WHsNhTEWHgTdSrf UfDsAMc6DqacCMHgAIgf+QsqJXiDtMGElImaUFnNJ5bTgtcqOf75PlQoeSgmGNaggrAD fGCjrtIcFQw4lRxwA5MEML/BhLWZLC72wPJSHZzYf9vZRvuGcGZmxJBPlo+wlHB7wXjx TmXFXMGXJIRCEu/HTsRAF3CApRKP7XsxVbRM9rw0Y4SWUosC6yStQ3JJuzYtRo7HECMT Z6BA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=nxp.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id hu8-20020a170907a08800b0087787cec039si1221807ejc.311.2023.01.19.12.55.42; Thu, 19 Jan 2023 12:55:54 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=nxp.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229711AbjASUlE (ORCPT + 48 others); Thu, 19 Jan 2023 15:41:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44364 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230249AbjASUkz (ORCPT ); Thu, 19 Jan 2023 15:40:55 -0500 Received: from inva021.nxp.com (inva021.nxp.com [92.121.34.21]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DC34190845; Thu, 19 Jan 2023 12:40:38 -0800 (PST) Received: from inva021.nxp.com (localhost [127.0.0.1]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id 6A365201829; Thu, 19 Jan 2023 21:40:37 +0100 (CET) Received: from aprdc01srsp001v.ap-rdc01.nxp.com (aprdc01srsp001v.ap-rdc01.nxp.com [165.114.16.16]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id 081172017ED; Thu, 19 Jan 2023 21:40:37 +0100 (CET) Received: from lsv03267.swis.in-blr01.nxp.com (lsv03267.swis.in-blr01.nxp.com [92.120.147.107]) by aprdc01srsp001v.ap-rdc01.nxp.com (Postfix) with ESMTP id EBCB6180327D; Fri, 20 Jan 2023 04:40:35 +0800 (+08) From: nikhil.gupta@nxp.com To: linux-arm-kernel@lists.infradead.org, Yangbo Lu , vladimir.oltean@nxp.com, richardcochran@gmail.com, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Cc: vakul.garg@nxp.com, rajan.gupta@nxp.com, Nikhil Gupta Subject: [PATCH v2] ptp_qoriq: fix latency in ptp_qoriq_adjtime() operation Date: Fri, 20 Jan 2023 02:10:34 +0530 Message-Id: <20230119204034.7969-1-nikhil.gupta@nxp.com> X-Mailer: git-send-email 2.17.1 X-Virus-Scanned: ClamAV using ClamSMTP X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Nikhil Gupta 1588 driver loses about 1us in adjtime operation at PTP slave This is because adjtime operation uses a slow non-atomic tmr_cnt_read() followed by tmr_cnt_write() operation. In the above sequence, since the timer counter operation keeps incrementing, it leads to latency. The tmr_offset register (which is added to TMR_CNT_H/L register giving the current time) must be programmed with the delta nanoseconds. Signed-off-by: Nikhil Gupta --- v1->v2: prevent TMR_OFF adjustment in case of eTSEC drivers/ptp/ptp_qoriq.c | 50 ++++++++++++++++++++++++++++++----- include/linux/fsl/ptp_qoriq.h | 1 + 2 files changed, 44 insertions(+), 7 deletions(-) diff --git a/drivers/ptp/ptp_qoriq.c b/drivers/ptp/ptp_qoriq.c index 08f4cf0ad9e3..61530167efe4 100644 --- a/drivers/ptp/ptp_qoriq.c +++ b/drivers/ptp/ptp_qoriq.c @@ -48,6 +48,29 @@ static void tmr_cnt_write(struct ptp_qoriq *ptp_qoriq, u64 ns) ptp_qoriq->write(®s->ctrl_regs->tmr_cnt_h, hi); } +static u64 tmr_offset_read(struct ptp_qoriq *ptp_qoriq) +{ + struct ptp_qoriq_registers *regs = &ptp_qoriq->regs; + u32 lo, hi; + u64 ns; + + lo = ptp_qoriq->read(®s->ctrl_regs->tmroff_l); + hi = ptp_qoriq->read(®s->ctrl_regs->tmroff_h); + ns = ((u64) hi) << 32; + ns |= lo; + return ns; +} + +static void tmr_offset_write(struct ptp_qoriq *ptp_qoriq, u64 delta_ns) +{ + struct ptp_qoriq_registers *regs = &ptp_qoriq->regs; + u32 lo = delta_ns & 0xffffffff; + u32 hi = delta_ns >> 32; + + ptp_qoriq->write(®s->ctrl_regs->tmroff_l, lo); + ptp_qoriq->write(®s->ctrl_regs->tmroff_h, hi); +} + /* Caller must hold ptp_qoriq->lock. */ static void set_alarm(struct ptp_qoriq *ptp_qoriq) { @@ -55,7 +78,9 @@ static void set_alarm(struct ptp_qoriq *ptp_qoriq) u64 ns; u32 lo, hi; - ns = tmr_cnt_read(ptp_qoriq) + 1500000000ULL; + ns = tmr_cnt_read(ptp_qoriq) + tmr_offset_read(ptp_qoriq) + + 1500000000ULL; + ns = div_u64(ns, 1000000000UL) * 1000000000ULL; ns -= ptp_qoriq->tclk_period; hi = ns >> 32; @@ -207,15 +232,24 @@ EXPORT_SYMBOL_GPL(ptp_qoriq_adjfine); int ptp_qoriq_adjtime(struct ptp_clock_info *ptp, s64 delta) { - s64 now; - unsigned long flags; struct ptp_qoriq *ptp_qoriq = container_of(ptp, struct ptp_qoriq, caps); + s64 now, curr_delta; + unsigned long flags; spin_lock_irqsave(&ptp_qoriq->lock, flags); - now = tmr_cnt_read(ptp_qoriq); - now += delta; - tmr_cnt_write(ptp_qoriq, now); + /* On LS1021A, eTSEC2 and eTSEC3 do not take into account the TMR_OFF + * adjustment + */ + if (ptp_qoriq->etsec) { + now = tmr_cnt_read(ptp_qoriq); + now += delta; + tmr_cnt_write(ptp_qoriq, now); + } else { + curr_delta = tmr_offset_read(ptp_qoriq); + curr_delta += delta; + tmr_offset_write(ptp_qoriq, curr_delta); + } set_fipers(ptp_qoriq); spin_unlock_irqrestore(&ptp_qoriq->lock, flags); @@ -232,7 +266,7 @@ int ptp_qoriq_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts) spin_lock_irqsave(&ptp_qoriq->lock, flags); - ns = tmr_cnt_read(ptp_qoriq); + ns = tmr_cnt_read(ptp_qoriq) + tmr_offset_read(ptp_qoriq); spin_unlock_irqrestore(&ptp_qoriq->lock, flags); @@ -253,6 +287,7 @@ int ptp_qoriq_settime(struct ptp_clock_info *ptp, spin_lock_irqsave(&ptp_qoriq->lock, flags); + tmr_offset_write(ptp_qoriq, 0); tmr_cnt_write(ptp_qoriq, ns); set_fipers(ptp_qoriq); @@ -488,6 +523,7 @@ int ptp_qoriq_init(struct ptp_qoriq *ptp_qoriq, void __iomem *base, /* The eTSEC uses differnt memory map with DPAA/ENETC */ if (of_device_is_compatible(node, "fsl,etsec-ptp")) { + ptp_qoriq->etsec = true; ptp_qoriq->regs.ctrl_regs = base + ETSEC_CTRL_REGS_OFFSET; ptp_qoriq->regs.alarm_regs = base + ETSEC_ALARM_REGS_OFFSET; ptp_qoriq->regs.fiper_regs = base + ETSEC_FIPER_REGS_OFFSET; diff --git a/include/linux/fsl/ptp_qoriq.h b/include/linux/fsl/ptp_qoriq.h index 01acebe37fab..b301bf7199d3 100644 --- a/include/linux/fsl/ptp_qoriq.h +++ b/include/linux/fsl/ptp_qoriq.h @@ -149,6 +149,7 @@ struct ptp_qoriq { struct device *dev; bool extts_fifo_support; bool fiper3_support; + bool etsec; int irq; int phc_index; u32 tclk_period; /* nanoseconds */ -- 2.17.1