Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp332804iog; Fri, 24 Jun 2022 05:05:11 -0700 (PDT) X-Google-Smtp-Source: AGRyM1t3L8toNlS8S+h9JAV0bWHltEqDhkjrsmy8KxYdBmiTFzipfs75xrIAHPepHbBYX3TtrXQq X-Received: by 2002:aa7:d296:0:b0:435:7f33:38bc with SMTP id w22-20020aa7d296000000b004357f3338bcmr17119942edq.399.1656072311252; Fri, 24 Jun 2022 05:05:11 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1656072311; cv=pass; d=google.com; s=arc-20160816; b=AhYY/oM0Qb8y/LEtF3mGRJVwMySnAspAcr+PBq92J/BYEo4jot3oYpaBqHHyqZS7Ug NCJTn/6OO0tQR4S2qBTiGb58gGwbp+Wg0n9q6gz2WyA2bF9fudua7+aKONI8KSxDCJBT irk80kTJAImzXZAmKMZZjGZO/mYepCMKaWf6GY6rf1HdL1/h837iBOnTVXkslYSIoxbV ljJETXmbKvT/tmCfw7yB6ai9UL4iaScL+B/lJU90wmN8MbfBmXH4laFPZQEXLV1/ZZ22 X/HeyWofBldrQKCJF1ezFQtCKOF/lNn3UMUzAvLhw6/XbTJgHidZLIEFzajaPE97VKC4 rGdw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :envelope-to:dkim-signature; bh=a4/bCQN3O7eZp/wltAreVBuLZkQ6lrXALa+pW/PfpyI=; b=pVxByYNFdBl+TuQfHN2qU5I9Gf9Wiu1fLopWliPmniTWSWEa997ZdtnVuPqsRbK97w h/qNIG2CJXhu3HHDrB67jCLJ1kMZ7yWQ5Pbzq9SqYdvXd6eeSyYkjktE380I+ThNaJxs BzDtBRgNnLQhSbPSx9ixZD42ZIknWOpLi9RLEEbIaOFA8JLh6jM7q0eT2dgcCIKRs+SH 1vU02Uyd01Zqux3Z/XlPPh8mO81JB+X0ybZ+VTCHXSxayaDxtbJIBrD3VO69HF0hLLdi /px1P2xZ5AEloRL5Cxy32CfZpszQc8g9ijKUCTKGHc7haypYtuskXhKB6GrdFMEXe56Y Xtxg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@xilinx.onmicrosoft.com header.s=selector2-xilinx-onmicrosoft-com header.b=WRnGInPn; arc=pass (i=1 spf=pass spfdomain=xilinx.com dmarc=pass fromdomain=xilinx.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=xilinx.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id sb9-20020a1709076d8900b007248d3baefdsi2388067ejc.763.2022.06.24.05.04.46; Fri, 24 Jun 2022 05:05:11 -0700 (PDT) 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; dkim=pass header.i=@xilinx.onmicrosoft.com header.s=selector2-xilinx-onmicrosoft-com header.b=WRnGInPn; arc=pass (i=1 spf=pass spfdomain=xilinx.com dmarc=pass fromdomain=xilinx.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=xilinx.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231787AbiFXMCg (ORCPT + 99 others); Fri, 24 Jun 2022 08:02:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50870 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230312AbiFXMC3 (ORCPT ); Fri, 24 Jun 2022 08:02:29 -0400 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2053.outbound.protection.outlook.com [40.107.92.53]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 47EF47E028; Fri, 24 Jun 2022 05:02:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=BVJfDK4NOmKV0/UMQxXs9YKeqcjl3Elmfh82eljnu42eAEOxlUw8RV6Dl1jmfwBH0h++YjCiIT/7K7t/ZDvb30GBdMJAl/av+zzMW/e6g0xw7Me3twOmVDOgwTXuwdUiUJew5VB7JQCbPvG3EOoz6IvlCPv4yTsqPjb2vgw39H/jwMaZ1ayTkjM3WFp+Gq7o2a7ihGA9q7YtAcf03S/nJdxEI3rc6xoCur5GIGFbuVa58Z0J/iU/IQ65Aw9A7DaCMi8yLaVlsvha8lgHtg1gCPHNWN31B8bbyP/OG+PtwffOvJH6qUqH/hcpBbNzEAVDQZTzu3kCiW1A2EYSyxPfvg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=a4/bCQN3O7eZp/wltAreVBuLZkQ6lrXALa+pW/PfpyI=; b=bc3c0WLLxWukymhKqL0wQ/HTHVSi7y8BRJhoe4qUWnQ9QAvezRvvJDfv7cZz/J4CGl9LMvD489v3Sed6bL2X/vToAPKMivKFg+ili0J3NFAtblWXVdef7w68JkujPiEaiGo7Zc/+63LZXHXGh6/+h/9+VOVqQl6FZDjQ8fjHxICl3jtmlmYw7hEcd8QCqHlQ1xB2qhgSBXE8/NK0lsrZgiivTaLWG8YBSkTcRVAbfGBxTousaVz2zVONDZnIb8whVL9kGMKqOTi3pNp3EtMTpG+Y1tpYgXM70t9nTSiuGS8oCWPO8CSEd6QpRuXRyb5cVo2AZswaRoseCc+1pE6zNQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 149.199.62.198) smtp.rcpttodomain=towertech.it smtp.mailfrom=xilinx.com; dmarc=pass (p=none sp=none pct=100) action=none header.from=xilinx.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=xilinx.onmicrosoft.com; s=selector2-xilinx-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=a4/bCQN3O7eZp/wltAreVBuLZkQ6lrXALa+pW/PfpyI=; b=WRnGInPn9KVu1bsVcA+ZcF22ogJ7m9c6L8c9IN87bSkx0HRzVpr7BBirtzmg+lkMsc+gIauzglRkDEcrgGvE8IPg7jF+Ur0GJI5PvZgzhWziR+vk8fJ6Z9HoW+lZBgi1/zlZjHElQbjegXmGtgVoscdP3vLomo+3LmIc8krnD60= Received: from BN9PR03CA0270.namprd03.prod.outlook.com (2603:10b6:408:ff::35) by SA0PR02MB7369.namprd02.prod.outlook.com (2603:10b6:806:e3::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5373.17; Fri, 24 Jun 2022 12:02:26 +0000 Received: from BN1NAM02FT051.eop-nam02.prod.protection.outlook.com (2603:10b6:408:ff:cafe::c7) by BN9PR03CA0270.outlook.office365.com (2603:10b6:408:ff::35) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5373.15 via Frontend Transport; Fri, 24 Jun 2022 12:02:26 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 149.199.62.198) smtp.mailfrom=xilinx.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=xilinx.com; Received-SPF: Pass (protection.outlook.com: domain of xilinx.com designates 149.199.62.198 as permitted sender) receiver=protection.outlook.com; client-ip=149.199.62.198; helo=xsj-pvapexch02.xlnx.xilinx.com; pr=C Received: from xsj-pvapexch02.xlnx.xilinx.com (149.199.62.198) by BN1NAM02FT051.mail.protection.outlook.com (10.13.2.159) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5373.15 via Frontend Transport; Fri, 24 Jun 2022 12:02:25 +0000 Received: from xsj-pvapexch02.xlnx.xilinx.com (172.19.86.41) by xsj-pvapexch02.xlnx.xilinx.com (172.19.86.41) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2176.14; Fri, 24 Jun 2022 05:02:20 -0700 Received: from smtp.xilinx.com (172.19.127.95) by xsj-pvapexch02.xlnx.xilinx.com (172.19.86.41) with Microsoft SMTP Server id 15.1.2176.14 via Frontend Transport; Fri, 24 Jun 2022 05:02:20 -0700 Envelope-to: git@xilinx.com, a.zummo@towertech.it, alexandre.belloni@bootlin.com, robh+dt@kernel.org, srinivas.neeli@amd.com, neelisrinivas18@gmail.com, krzysztof.kozlowski+dt@linaro.org, devicetree@vger.kernel.org, linux-rtc@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, git@amd.com Received: from [10.140.6.39] (port=57958 helo=xhdsgoud40.xilinx.com) by smtp.xilinx.com with esmtp (Exim 4.90) (envelope-from ) id 1o4i0x-0009E0-Uh; Fri, 24 Jun 2022 05:02:20 -0700 From: Srinivas Neeli To: , , , , , , , , CC: , , , , , , Srinivas Neeli Subject: [PATCH V9 3/3] rtc: zynqmp: Add calibration set and get support Date: Fri, 24 Jun 2022 17:32:03 +0530 Message-ID: <20220624120203.2817457-3-srinivas.neeli@xilinx.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220624120203.2817457-1-srinivas.neeli@xilinx.com> References: <20220624120203.2817457-1-srinivas.neeli@xilinx.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 1daea13d-a58f-4236-8dfa-08da55d9676a X-MS-TrafficTypeDiagnostic: SA0PR02MB7369:EE_ X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: /27y6/3DPaxit2/gdeSa6x9QDDEXZGdJ2MCCtqKd2FWmQGg1gHHwKBSIDa9eUW94HR6u0VR7CilgpKPpN6jtDW7vOd0IBKQaNOdlFQoSfWtcWQHwfYTdt7duih15ttz37gHT1CwA86Q11dfW7isDP8SJK/WrH75Jfl2iVw5zcjkunMIT+xBMKFag2tXc+dQBJoxxCFhdS2IGMKmwrRygRA1xw+8dfpwCoCA7yM0mbvoFHwvdSn/4dbw5giY9fNA+3sN8wuJpt7HDB+Ip7dAFnpNONDC5Le58bxPSjxfOSTqoV068WjqqG7d7uYNwW2M1nerLCE1siVFF92Y445w3TOCbct09EalifFa2f6T7RBaHlFKHH5kpOFV5Q4Q641DiqQURe4w7a3KSfmULAIvy1oMgeRJK8CC7NdSxlVw5biHLVJhWoUXVUB6X+4+/0L39JHk8asCpL2Ok/CHmD42tmz0WxcTMIqnJF1EejouCzpfDrpCAz2xCCGpE4ZdFmaIjW6vjo4kVcwmFUwId/h2eUUgoWvoKCNjx3RUG7cCWizLU9f4TNa6bfZnpxpdz9HsQ64slO5Z6isOh0r5loH5w1HczXrk00ZuimcA8d1OA1fBjAiOwRH4r6xHSg9ppuvSyu5VB8RiA//LWbXxvr7F7Y7+VH4qzg9DMzOKd+eamu+NkoARXkLiv+/VonW1lOPaUxuS1wWiM08juxG3KKV/WFLF22usXxp2aRp9d1Asw+0MdTaxlyN0aB49yEPIeeefCj+O5iH6XG2mVRrBUJK182DV8z+NBazbyXd98egYuBXZmmIFAW/HFfHdFOoEHHOC0xu/M3G/3if/lJxqkVKVVFKCjG5xiINGlSNFSWA34uyo= X-Forefront-Antispam-Report: CIP:149.199.62.198;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:xsj-pvapexch02.xlnx.xilinx.com;PTR:unknown-62-198.xilinx.com;CAT:NONE;SFS:(13230016)(4636009)(39860400002)(396003)(346002)(376002)(136003)(40470700004)(46966006)(36840700001)(7636003)(70586007)(44832011)(70206006)(2616005)(8676002)(54906003)(4326008)(36860700001)(40480700001)(110136005)(186003)(5660300002)(82740400003)(1076003)(107886003)(7416002)(7696005)(356005)(9786002)(8936002)(6636002)(83380400001)(41300700001)(6666004)(47076005)(36756003)(336012)(316002)(26005)(426003)(2906002)(82310400005)(478600001)(40460700003)(102446001)(2101003);DIR:OUT;SFP:1101; X-OriginatorOrg: xilinx.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 24 Jun 2022 12:02:25.6240 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 1daea13d-a58f-4236-8dfa-08da55d9676a X-MS-Exchange-CrossTenant-Id: 657af505-d5df-48d0-8300-c31994686c5c X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=657af505-d5df-48d0-8300-c31994686c5c;Ip=[149.199.62.198];Helo=[xsj-pvapexch02.xlnx.xilinx.com] X-MS-Exchange-CrossTenant-AuthSource: BN1NAM02FT051.eop-nam02.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR02MB7369 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE 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 Zynqmp RTC controller has a calibration feature to compensate time deviation due to input clock inaccuracy. Set and get calibration API's are used for setting and getting calibration value from the controller calibration register. Signed-off-by: Srinivas Neeli --- Changes in V9: -None Changes in V8: -None Changes in V7: -Removed calibration default value update from this patch. Changes in V6: -None Changes in V5: -None Changes in V4: -Updated MIN and MAX calibration values. Changes in V3: -Calculated tick_mult using crystal frequency. -Calibration register updating based on crystal frequency in probe. -Supressed MIN an MAX calibration values,Will send separate patch in future. Changes in V2: -Removed unused macro. -Updated code with review comments. --- drivers/rtc/rtc-zynqmp.c | 113 ++++++++++++++++++++++++++++++++------- 1 file changed, 94 insertions(+), 19 deletions(-) diff --git a/drivers/rtc/rtc-zynqmp.c b/drivers/rtc/rtc-zynqmp.c index 5da33d760419..1dd389b891fe 100644 --- a/drivers/rtc/rtc-zynqmp.c +++ b/drivers/rtc/rtc-zynqmp.c @@ -6,6 +6,7 @@ * */ +#include #include #include #include @@ -40,13 +41,19 @@ #define RTC_CALIB_MASK 0x1FFFFF #define RTC_ALRM_MASK BIT(1) #define RTC_MSEC 1000 +#define RTC_FR_MASK 0xF0000 +#define RTC_FR_MAX_TICKS 16 +#define RTC_PPB 1000000000LL +#define RTC_MIN_OFFSET -32768000 +#define RTC_MAX_OFFSET 32767000 struct xlnx_rtc_dev { struct rtc_device *rtc; void __iomem *reg_base; int alarm_irq; int sec_irq; - unsigned int calibval; + struct clk *rtc_clk; + unsigned int freq; }; static int xlnx_rtc_set_time(struct device *dev, struct rtc_time *tm) @@ -61,13 +68,6 @@ static int xlnx_rtc_set_time(struct device *dev, struct rtc_time *tm) */ new_time = rtc_tm_to_time64(tm) + 1; - /* - * Writing into calibration register will clear the Tick Counter and - * force the next second to be signaled exactly in 1 second period - */ - xrtcdev->calibval &= RTC_CALIB_MASK; - writel(xrtcdev->calibval, (xrtcdev->reg_base + RTC_CALIB_WR)); - writel(new_time, xrtcdev->reg_base + RTC_SET_TM_WR); /* @@ -173,15 +173,76 @@ static void xlnx_init_rtc(struct xlnx_rtc_dev *xrtcdev) rtc_ctrl = readl(xrtcdev->reg_base + RTC_CTRL); rtc_ctrl |= RTC_BATT_EN; writel(rtc_ctrl, xrtcdev->reg_base + RTC_CTRL); +} - /* - * Based on crystal freq of 33.330 KHz - * set the seconds counter and enable, set fractions counter - * to default value suggested as per design spec - * to correct RTC delay in frequency over period of time. +static int xlnx_rtc_read_offset(struct device *dev, long *offset) +{ + struct xlnx_rtc_dev *xrtcdev = dev_get_drvdata(dev); + unsigned long long rtc_ppb = RTC_PPB; + unsigned int tick_mult = do_div(rtc_ppb, xrtcdev->freq); + unsigned int calibval; + long offset_val; + + calibval = readl(xrtcdev->reg_base + RTC_CALIB_RD); + /* Offset with seconds ticks */ + offset_val = calibval & RTC_TICK_MASK; + offset_val = offset_val - RTC_CALIB_DEF; + offset_val = offset_val * tick_mult; + + /* Offset with fractional ticks */ + if (calibval & RTC_FR_EN) + offset_val += ((calibval & RTC_FR_MASK) >> RTC_FR_DATSHIFT) + * (tick_mult / RTC_FR_MAX_TICKS); + *offset = offset_val; + + return 0; +} + +static int xlnx_rtc_set_offset(struct device *dev, long offset) +{ + struct xlnx_rtc_dev *xrtcdev = dev_get_drvdata(dev); + unsigned long long rtc_ppb = RTC_PPB; + unsigned int tick_mult = do_div(rtc_ppb, xrtcdev->freq); + unsigned char fract_tick; + unsigned int calibval; + short int max_tick; + int fract_offset; + + if (offset < RTC_MIN_OFFSET || offset > RTC_MAX_OFFSET) + return -ERANGE; + + /* Number ticks for given offset */ + max_tick = div_s64_rem(offset, tick_mult, &fract_offset); + + /* Number fractional ticks for given offset */ + if (fract_offset) { + if (fract_offset < 0) { + fract_offset = fract_offset + tick_mult; + max_tick--; + } + if (fract_offset > (tick_mult / RTC_FR_MAX_TICKS)) { + for (fract_tick = 1; fract_tick < 16; fract_tick++) { + if (fract_offset <= + (fract_tick * + (tick_mult / RTC_FR_MAX_TICKS))) + break; + } + } + } + + /* Zynqmp RTC uses second and fractional tick + * counters for compensation */ - xrtcdev->calibval &= RTC_CALIB_MASK; - writel(xrtcdev->calibval, (xrtcdev->reg_base + RTC_CALIB_WR)); + calibval = max_tick + RTC_CALIB_DEF; + + if (fract_tick) + calibval |= RTC_FR_EN; + + calibval |= (fract_tick << RTC_FR_DATSHIFT); + + writel(calibval, (xrtcdev->reg_base + RTC_CALIB_WR)); + + return 0; } static const struct rtc_class_ops xlnx_rtc_ops = { @@ -190,6 +251,8 @@ static const struct rtc_class_ops xlnx_rtc_ops = { .read_alarm = xlnx_rtc_read_alarm, .set_alarm = xlnx_rtc_set_alarm, .alarm_irq_enable = xlnx_rtc_alarm_irq_enable, + .read_offset = xlnx_rtc_read_offset, + .set_offset = xlnx_rtc_set_offset, }; static irqreturn_t xlnx_rtc_interrupt(int irq, void *id) @@ -255,10 +318,22 @@ static int xlnx_rtc_probe(struct platform_device *pdev) return ret; } - ret = of_property_read_u32(pdev->dev.of_node, "calibration", - &xrtcdev->calibval); - if (ret) - xrtcdev->calibval = RTC_CALIB_DEF; + /* Getting the rtc_clk info */ + xrtcdev->rtc_clk = devm_clk_get_optional(&pdev->dev, "rtc_clk"); + if (IS_ERR(xrtcdev->rtc_clk)) { + if (PTR_ERR(xrtcdev->rtc_clk) != -EPROBE_DEFER) + dev_warn(&pdev->dev, "Device clock not found.\n"); + } + xrtcdev->freq = clk_get_rate(xrtcdev->rtc_clk); + if (!xrtcdev->freq) { + ret = of_property_read_u32(pdev->dev.of_node, "calibration", + &xrtcdev->freq); + if (ret) + xrtcdev->freq = RTC_CALIB_DEF; + } + ret = readl(xrtcdev->reg_base + RTC_CALIB_RD); + if (!ret) + writel(xrtcdev->freq, (xrtcdev->reg_base + RTC_CALIB_WR)); xlnx_init_rtc(xrtcdev); -- 2.25.1