From patchwork Thu Jun 15 09:27:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Valentin Caron X-Patchwork-Id: 13280951 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 662CFEB64DD for ; Thu, 15 Jun 2023 09:32:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:CC:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=KLQjkedVgA7MJVs4FZTR/Ejh3GFZbzJFACGsQ7otsbc=; b=DgKPrU43mbuvp5 5WZzQqF0w7An6O0uFXCvVRhq+n6klRDlDURVBmJjyW76AhatUg7RnX5kvrL0DdC3rTBjNxnpKuYER hrWB14bYOgRiVvxxP9m/Oybnc0PRQFiSh0mneWcrEYesEaGZ6BA0ZvWzdz4I130GRyPA7AcC3jaS4 bBXxe7ZV55aGQ3HLg2NukBk0k0000pujbElAESvf/u5ot3SAR4uWH+LzakR2qDbyLPGuthUvYn7kV gAdl02zGc2zX5sXel9YjiUcWSFPhLKW77wh4caL3x4X43bKTVeSKeqtw0f2zP0hCZc4KSorira+pz I93oegzpqwlDQYKNkfbw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1q9jLH-00EJ4p-0C; Thu, 15 Jun 2023 09:32:35 +0000 Received: from mx08-00178001.pphosted.com ([91.207.212.93] helo=mx07-00178001.pphosted.com) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1q9jL8-00EJ1U-2x for linux-arm-kernel@lists.infradead.org; Thu, 15 Jun 2023 09:32:28 +0000 Received: from pps.filterd (m0046661.ppops.net [127.0.0.1]) by mx07-00178001.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 35F8aR2C016209; Thu, 15 Jun 2023 11:32:24 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foss.st.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=selector1; bh=tu9jBOtEovYr093HJ36dUdSrKpaYVFSBiOffzxwZiQY=; b=A1fYt0Vd8zff1LwOFARqbj6u2wbqg56dG5hng2YHJucoZaUm+wZ4+Hy1Anp5uUdT17/P ucuNi5eEztNtkzhhHIQRHKll2xjF4Lyl4KTNspvuin3E9dDegNyZa/gqJEoY+SzvUPbZ LyRgmgb/Yg/tdOVtEnl2L+Ahk6TczNko8ttqflixd+zxf/R/ysmt3SHLSu0VBWCmDb9J qeWawi4mGhXzhws5Pzd/hX/OMil9fUr6uQrZ35tkdPEWvlE27hKRPNmhYekHaDs8nx28 pIy11ZkPL+tovyhX+uG7lO89vNWnFs5hbXR/vl+CjF3hnZzOiyDxJb0Qo0YqV0v1/A+E 4g== Received: from beta.dmz-eu.st.com (beta.dmz-eu.st.com [164.129.1.35]) by mx07-00178001.pphosted.com (PPS) with ESMTPS id 3r7wgx1h99-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 15 Jun 2023 11:32:24 +0200 Received: from euls16034.sgp.st.com (euls16034.sgp.st.com [10.75.44.20]) by beta.dmz-eu.st.com (STMicroelectronics) with ESMTP id CF07210004A; Thu, 15 Jun 2023 11:32:21 +0200 (CEST) Received: from Webmail-eu.st.com (shfdag1node1.st.com [10.75.129.69]) by euls16034.sgp.st.com (STMicroelectronics) with ESMTP id C875221A91B; Thu, 15 Jun 2023 11:32:21 +0200 (CEST) Received: from localhost (10.252.8.64) by SHFDAG1NODE1.st.com (10.75.129.69) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.21; Thu, 15 Jun 2023 11:32:21 +0200 From: Valentin Caron To: Alessandro Zummo , Alexandre Belloni CC: Alexandre Torgue , Antonio Borneo , Christophe Guibout , Gabriel Fernandez , , , , , Valentin Caron Subject: [PATCH 1/7] rtc: stm32: use the proper register sequence to read date/time Date: Thu, 15 Jun 2023 11:27:47 +0200 Message-ID: <20230615092753.323844-2-valentin.caron@foss.st.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230615092753.323844-1-valentin.caron@foss.st.com> References: <20230615092753.323844-1-valentin.caron@foss.st.com> MIME-Version: 1.0 X-Originating-IP: [10.252.8.64] X-ClientProxiedBy: SHFCAS1NODE2.st.com (10.75.129.73) To SHFDAG1NODE1.st.com (10.75.129.69) X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.957,Hydra:6.0.573,FMLib:17.11.176.26 definitions=2023-06-15_06,2023-06-14_02,2023-05-22_02 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230615_023227_247631_8C00FB4D X-CRM114-Status: GOOD ( 20.50 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Antonio Borneo Date and time are read from two separate RTC registers. To ensure consistency between the two registers, reading the time register locks the values in the shadow date register until the date register is read. Thus, the whole date/time read requires reading the time register first, followed by reading the date register. If the reads are done in reversed order, the shadow date register will remain locked until a future read operation. The future read will read the former date value that could be already invalid. Fix the read order of date/time registers in stm32_rtc_valid_alrm() Signed-off-by: Antonio Borneo Signed-off-by: Valentin Caron --- drivers/rtc/rtc-stm32.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/rtc/rtc-stm32.c b/drivers/rtc/rtc-stm32.c index 3d36e11cff80..abb77ad774a1 100644 --- a/drivers/rtc/rtc-stm32.c +++ b/drivers/rtc/rtc-stm32.c @@ -429,8 +429,8 @@ static int stm32_rtc_valid_alrm(struct stm32_rtc *rtc, struct rtc_time *tm) { const struct stm32_rtc_registers *regs = &rtc->data->regs; int cur_day, cur_mon, cur_year, cur_hour, cur_min, cur_sec; - unsigned int dr = readl_relaxed(rtc->base + regs->dr); unsigned int tr = readl_relaxed(rtc->base + regs->tr); + unsigned int dr = readl_relaxed(rtc->base + regs->dr); cur_day = (dr & STM32_RTC_DR_DATE) >> STM32_RTC_DR_DATE_SHIFT; cur_mon = (dr & STM32_RTC_DR_MONTH) >> STM32_RTC_DR_MONTH_SHIFT; From patchwork Thu Jun 15 09:27:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Valentin Caron X-Patchwork-Id: 13280950 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C0036EB64DC for ; Thu, 15 Jun 2023 09:32:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:CC:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=b+NAQoXRvTjixU5GMUbYQmXOARfPYirZKj8DfllFxGs=; b=KxJtuF9IanyZPe Odh7lXZ8r84qrWnqDephgKiD+7TjtvAC7dAewe5FuJrxmO3SfrXJzYHD+uuSML2zDMUm/ocadT0Pr uXYXfOn3EiNs7Y5QJsGH6bDc+RJz/Vuy/RwpvEGmvjj5SQ7GB+p3zTeUU9Dpe8a0tzIT2GQevx/7Y ak1z+WffNXpmWJGaJrp7fA7/nVa5q2OHT/3I4HmSLKi90n2BypoiKHrrKyNotQmN556v5dgeR6xzQ ZIA85r+BtAoI4uJ07mCjW2LoKHYvud3ugStK8qjBdNdUogGQ7J8EXAJ8FTqa6dcRNUtT5rVVQbldr zn5z3kA8Zl528kYbaMfQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1q9jLG-00EJ4F-0T; Thu, 15 Jun 2023 09:32:34 +0000 Received: from mx07-00178001.pphosted.com ([185.132.182.106]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1q9jL7-00EJ17-1q for linux-arm-kernel@lists.infradead.org; Thu, 15 Jun 2023 09:32:27 +0000 Received: from pps.filterd (m0241204.ppops.net [127.0.0.1]) by mx07-00178001.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 35F8YpBa021622; Thu, 15 Jun 2023 11:32:23 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foss.st.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=selector1; bh=3uBLJmxq5VVWC2o5xkCW/MuK8POUJa1lY65SLOrbAlI=; b=cVtd5PJjhGLfmF4RwGm+bP1LzN007yc/3l6k9Ad4UD0tlSxgBDnOniSQTPbM9gKQ0mH4 05m34vL7As4a8I0I4ivY5z3Irgru2n7k9pwnxzztngJD6UgLRHAhPdPDsgYWuCQEc8y4 ZlCnhZcv5PB5UO3roTqkSyh1TLgbJpsJuSsw0oomyBwU6chgQBGYcpWbLrACPnzgOrr+ ncMjNT9SXEkhdi7Gt5g3wP0HaRcq7+/ZPNZewLvSgeqTmXPluL1KPNGzI47g/7FRACgF QrkKiuwPulr5/0D7x1hVLOk+xhqK56f+Y3xPx/lkpsOOGoXlXHlIpLZDmIjJANToBoLh cA== Received: from beta.dmz-eu.st.com (beta.dmz-eu.st.com [164.129.1.35]) by mx07-00178001.pphosted.com (PPS) with ESMTPS id 3r7y8sge46-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 15 Jun 2023 11:32:23 +0200 Received: from euls16034.sgp.st.com (euls16034.sgp.st.com [10.75.44.20]) by beta.dmz-eu.st.com (STMicroelectronics) with ESMTP id E441810002A; Thu, 15 Jun 2023 11:32:22 +0200 (CEST) Received: from Webmail-eu.st.com (shfdag1node1.st.com [10.75.129.69]) by euls16034.sgp.st.com (STMicroelectronics) with ESMTP id DCD6421A91B; Thu, 15 Jun 2023 11:32:22 +0200 (CEST) Received: from localhost (10.252.8.64) by SHFDAG1NODE1.st.com (10.75.129.69) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.21; Thu, 15 Jun 2023 11:32:22 +0200 From: Valentin Caron To: Alessandro Zummo , Alexandre Belloni CC: Alexandre Torgue , Antonio Borneo , Christophe Guibout , Gabriel Fernandez , , , , , Valentin Caron Subject: [PATCH 2/7] rtc: stm32: don't stop time counter if not needed Date: Thu, 15 Jun 2023 11:27:48 +0200 Message-ID: <20230615092753.323844-3-valentin.caron@foss.st.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230615092753.323844-1-valentin.caron@foss.st.com> References: <20230615092753.323844-1-valentin.caron@foss.st.com> MIME-Version: 1.0 X-Originating-IP: [10.252.8.64] X-ClientProxiedBy: SHFCAS1NODE2.st.com (10.75.129.73) To SHFDAG1NODE1.st.com (10.75.129.69) X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.957,Hydra:6.0.573,FMLib:17.11.176.26 definitions=2023-06-15_06,2023-06-14_02,2023-05-22_02 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230615_023225_919431_C8AEEA91 X-CRM114-Status: GOOD ( 21.99 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Antonio Borneo RTC counters are stopped when INIT bit in ISR register is set and start counting from the (eventual) new value when INIT is reset. In stm32_rtc_init(), called during probe, the INIT bit is set to program the prescaler and the 24h mode. This halts the RTC counter at each probe tentative causing the RTC time to loose from 0.3s to 0.8s at each kernel boot. If the RTC is battery powered, both prescaler value and 24h mode are kept during power cycle and there is no need to program them again. Check if the desired prescaler value and the 24h mode are already programmed, then skip reprogramming them to avoid halting the time counter. Signed-off-by: Antonio Borneo Signed-off-by: Valentin Caron --- drivers/rtc/rtc-stm32.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/drivers/rtc/rtc-stm32.c b/drivers/rtc/rtc-stm32.c index abb77ad774a1..bd7a59a07537 100644 --- a/drivers/rtc/rtc-stm32.c +++ b/drivers/rtc/rtc-stm32.c @@ -628,7 +628,7 @@ static int stm32_rtc_init(struct platform_device *pdev, const struct stm32_rtc_registers *regs = &rtc->data->regs; unsigned int prer, pred_a, pred_s, pred_a_max, pred_s_max, cr; unsigned int rate; - int ret = 0; + int ret; rate = clk_get_rate(rtc->rtc_ck); @@ -656,6 +656,20 @@ static int stm32_rtc_init(struct platform_device *pdev, "fast" : "slow"); } + cr = readl_relaxed(rtc->base + regs->cr); + + prer = readl_relaxed(rtc->base + regs->prer); + prer &= STM32_RTC_PRER_PRED_S | STM32_RTC_PRER_PRED_A; + + pred_s = (pred_s << STM32_RTC_PRER_PRED_S_SHIFT) & + STM32_RTC_PRER_PRED_S; + pred_a = (pred_a << STM32_RTC_PRER_PRED_A_SHIFT) & + STM32_RTC_PRER_PRED_A; + + /* quit if there is nothing to initialize */ + if ((cr & STM32_RTC_CR_FMT) == 0 && prer == (pred_s | pred_a)) + return 0; + stm32_rtc_wpr_unlock(rtc); ret = stm32_rtc_enter_init_mode(rtc); @@ -665,13 +679,10 @@ static int stm32_rtc_init(struct platform_device *pdev, goto end; } - prer = (pred_s << STM32_RTC_PRER_PRED_S_SHIFT) & STM32_RTC_PRER_PRED_S; - writel_relaxed(prer, rtc->base + regs->prer); - prer |= (pred_a << STM32_RTC_PRER_PRED_A_SHIFT) & STM32_RTC_PRER_PRED_A; - writel_relaxed(prer, rtc->base + regs->prer); + writel_relaxed(pred_s, rtc->base + regs->prer); + writel_relaxed(pred_a | pred_s, rtc->base + regs->prer); /* Force 24h time format */ - cr = readl_relaxed(rtc->base + regs->cr); cr &= ~STM32_RTC_CR_FMT; writel_relaxed(cr, rtc->base + regs->cr); From patchwork Thu Jun 15 09:27:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Valentin Caron X-Patchwork-Id: 13280952 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 89974EB64DB for ; Thu, 15 Jun 2023 09:32:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:CC:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=3tAWgBTmzIyu9nn9uaGW8YUqSF86NKSno5bHPqZrSAg=; b=C3cIb842jxKZEZ 3TukN6feo9cXS/vvmrPCwyA/4QhbPYdHasTuKBd48HdIDtmjFyMOmOuMMMpTHz++8H+UMTIt0Ndfx Absv4W4C6pFgb7Mivi3y+8COzmocQLj2apBHUtp4Yuw3KQ7Qt8FORVzUgfjdeauQY5so89L/NpxUd x74bObA0flP5Z06JLzJ/7vAfCFhgy3dUIQcrMRB5F2g1mYb+O0oRE6KMPCNisDCCPsAYG4++OGa4d St2xr6ljKNUw+qKQAKo112uP8LFuZTMmS/m2+YtTJHzvIjBiAdyP4vqwMXeWML5rMDfLeidrnwdrH 356EcRi8IyC5d5eKLVsA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1q9jLG-00EJ4W-1v; Thu, 15 Jun 2023 09:32:34 +0000 Received: from mx07-00178001.pphosted.com ([185.132.182.106]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1q9jL8-00EJ1T-2X for linux-arm-kernel@lists.infradead.org; Thu, 15 Jun 2023 09:32:28 +0000 Received: from pps.filterd (m0288072.ppops.net [127.0.0.1]) by mx07-00178001.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 35F8I2F3009956; Thu, 15 Jun 2023 11:32:24 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foss.st.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=selector1; bh=Xy/hUYzK/4Hs4UPMJUFskvFcKLM1GyUKIZn77IV+Fes=; b=ykj2xVGd4l8SPjOC+WaEsyn1vfQMeJB19nja7j0WMoq7jGqLyuTksVeSG1ypwhQ7oWbj qdqRfPA/+c69YhcQSIiZss7N5gdsd2LRwaPwCf3S0NsBabQCZB1DW652Ne/wrKke5vPc 0ypd32toBFM/zNCbKNuW+BCS3Rg9WG2DirmEDhKoXq/4hQ2r7/fH//5/YJaDJBDB0nbs Qm7CNUebjRIch7m1yqK6MQVEV6CvmHJrAZtnPzQZLE6sVqkKnd+n11y8fsuJGXR9lZ5G Dqd0JRs+nBQD3fWx4ydRRCxFE1yPK0Uex8nlR/bSzh2cVQGkIeOv+bIKFvVhwZgJVmV8 eA== Received: from beta.dmz-eu.st.com (beta.dmz-eu.st.com [164.129.1.35]) by mx07-00178001.pphosted.com (PPS) with ESMTPS id 3r7s1maxs3-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 15 Jun 2023 11:32:24 +0200 Received: from euls16034.sgp.st.com (euls16034.sgp.st.com [10.75.44.20]) by beta.dmz-eu.st.com (STMicroelectronics) with ESMTP id 004F810002A; Thu, 15 Jun 2023 11:32:23 +0200 (CEST) Received: from Webmail-eu.st.com (shfdag1node1.st.com [10.75.129.69]) by euls16034.sgp.st.com (STMicroelectronics) with ESMTP id EBF8721A91B; Thu, 15 Jun 2023 11:32:23 +0200 (CEST) Received: from localhost (10.252.8.64) by SHFDAG1NODE1.st.com (10.75.129.69) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.21; Thu, 15 Jun 2023 11:32:23 +0200 From: Valentin Caron To: Alessandro Zummo , Alexandre Belloni CC: Alexandre Torgue , Antonio Borneo , Christophe Guibout , Gabriel Fernandez , , , , , Valentin Caron Subject: [PATCH 3/7] rtc: stm32: improve rtc precision Date: Thu, 15 Jun 2023 11:27:49 +0200 Message-ID: <20230615092753.323844-4-valentin.caron@foss.st.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230615092753.323844-1-valentin.caron@foss.st.com> References: <20230615092753.323844-1-valentin.caron@foss.st.com> MIME-Version: 1.0 X-Originating-IP: [10.252.8.64] X-ClientProxiedBy: SHFCAS1NODE2.st.com (10.75.129.73) To SHFDAG1NODE1.st.com (10.75.129.69) X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.957,Hydra:6.0.573,FMLib:17.11.176.26 definitions=2023-06-15_06,2023-06-14_02,2023-05-22_02 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230615_023227_137636_14942FC3 X-CRM114-Status: GOOD ( 22.76 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Christophe Guibout The rtc is used to update the stgen counter on wake up from low power modes, so it needs to be as much accurate as possible. The maximization of asynchronous divider leads to a 4ms rtc precision clock. By decreasing pred_a to 0, it will have pred_s=32767 (when need_accuracy is true), so stgen clock becomes more accurate with 30us precision. Nevertheless this will leads to an increase of power consumption. Signed-off-by: Christophe Guibout Signed-off-by: Valentin Caron --- drivers/rtc/rtc-stm32.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/drivers/rtc/rtc-stm32.c b/drivers/rtc/rtc-stm32.c index bd7a59a07537..cad88668bcfb 100644 --- a/drivers/rtc/rtc-stm32.c +++ b/drivers/rtc/rtc-stm32.c @@ -114,6 +114,7 @@ struct stm32_rtc_data { void (*clear_events)(struct stm32_rtc *rtc, unsigned int flags); bool has_pclk; bool need_dbp; + bool need_accuracy; }; struct stm32_rtc { @@ -545,6 +546,7 @@ static void stm32_rtc_clear_events(struct stm32_rtc *rtc, static const struct stm32_rtc_data stm32_rtc_data = { .has_pclk = false, .need_dbp = true, + .need_accuracy = false, .regs = { .tr = 0x00, .dr = 0x04, @@ -566,6 +568,7 @@ static const struct stm32_rtc_data stm32_rtc_data = { static const struct stm32_rtc_data stm32h7_rtc_data = { .has_pclk = true, .need_dbp = true, + .need_accuracy = false, .regs = { .tr = 0x00, .dr = 0x04, @@ -596,6 +599,7 @@ static void stm32mp1_rtc_clear_events(struct stm32_rtc *rtc, static const struct stm32_rtc_data stm32mp1_data = { .has_pclk = true, .need_dbp = false, + .need_accuracy = true, .regs = { .tr = 0x00, .dr = 0x04, @@ -636,11 +640,25 @@ static int stm32_rtc_init(struct platform_device *pdev, pred_a_max = STM32_RTC_PRER_PRED_A >> STM32_RTC_PRER_PRED_A_SHIFT; pred_s_max = STM32_RTC_PRER_PRED_S >> STM32_RTC_PRER_PRED_S_SHIFT; - for (pred_a = pred_a_max; pred_a + 1 > 0; pred_a--) { - pred_s = (rate / (pred_a + 1)) - 1; + if (rate > (pred_a_max + 1) * (pred_s_max + 1)) { + dev_err(&pdev->dev, "rtc_ck rate is too high: %dHz\n", rate); + return -EINVAL; + } + + if (rtc->data->need_accuracy) { + for (pred_a = 0; pred_a <= pred_a_max; pred_a++) { + pred_s = (rate / (pred_a + 1)) - 1; - if (((pred_s + 1) * (pred_a + 1)) == rate) - break; + if (pred_s <= pred_s_max && ((pred_s + 1) * (pred_a + 1)) == rate) + break; + } + } else { + for (pred_a = pred_a_max; pred_a + 1 > 0; pred_a--) { + pred_s = (rate / (pred_a + 1)) - 1; + + if (((pred_s + 1) * (pred_a + 1)) == rate) + break; + } } /* From patchwork Thu Jun 15 09:27:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Valentin Caron X-Patchwork-Id: 13280949 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 17E84EB64D9 for ; Thu, 15 Jun 2023 09:32:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:CC:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=F3dK8/kLa+kxVsoT2MT9oLyh+w0g9BLtkI5a4LRe3RA=; b=c9PGZG3tO2qUqd 5YS56ESJgTS/clJTtElkVPR1eTabYX1j3IitAIWOJ07T4ePTFR80xFOy+y0B0zBMHfDKfmiHwowkL OkMyYU+/QmmUfLtsmwa1nZ55q0KwRjQ2zwKphC9KKeafI2V2Xm5eYN9Pn1dqteGTSlD8TG8TuiY7Q n45STSPKbKBxKAU+nFuWfhrBtAdZy/MqYS/+CBr8mvlIER7F4vp67+lRwQ7cq/7AM+nj6LQF5+uXW 2ZfXx3eJhR1j0aZg60KP9NXaZ76Vajw5G8v097DGJYJreGX0bsPMRWNfiFqgx5L+D3fwVXs6Qu3+K j7SqrVlIA80XWkfBLKFg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1q9jLH-00EJ5A-1h; Thu, 15 Jun 2023 09:32:35 +0000 Received: from mx07-00178001.pphosted.com ([185.132.182.106]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1q9jLA-00EJ1r-0b for linux-arm-kernel@lists.infradead.org; Thu, 15 Jun 2023 09:32:29 +0000 Received: from pps.filterd (m0288072.ppops.net [127.0.0.1]) by mx07-00178001.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 35F8I2F5009956; Thu, 15 Jun 2023 11:32:25 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foss.st.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=selector1; bh=MXa5UGOKwnuvRKspTklCvl9TbHyucJqEV4w5bCIDszM=; b=waacJ3UCN+Myd982zuzVHXWxobMlJZWjDYRvNtKru0DTY2sPGXUmXeADKwohP8wLFljV 37I6Gg6EGilpEjVD7Wb0oXbCEXM5KTyzmd0j54S7L84cs8V3n+Bp3acQ4sSVj+pEgUwO UVcvC8wZJH0H670tumVZ9FRQ8O9F2DQF1KYisADg/ix5CsIwoOZFiV3qyuN26ryPm1iy h014RaO/gc4DoWyW/4ZbJCbf6+yrjPw66Z4Iv60moeRMnvsCD5hGZkKGRJfylDNGMCMZ aYLug9v5joCeMUoTtZx66XixwYtBR3ScZuaGm1UAFl/407Zf/eU446M2hf0a0dA4HIce eQ== Received: from beta.dmz-eu.st.com (beta.dmz-eu.st.com [164.129.1.35]) by mx07-00178001.pphosted.com (PPS) with ESMTPS id 3r7s1maxsr-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 15 Jun 2023 11:32:25 +0200 Received: from euls16034.sgp.st.com (euls16034.sgp.st.com [10.75.44.20]) by beta.dmz-eu.st.com (STMicroelectronics) with ESMTP id 2EABC100046; Thu, 15 Jun 2023 11:32:25 +0200 (CEST) Received: from Webmail-eu.st.com (shfdag1node1.st.com [10.75.129.69]) by euls16034.sgp.st.com (STMicroelectronics) with ESMTP id 1C51821A91B; Thu, 15 Jun 2023 11:32:25 +0200 (CEST) Received: from localhost (10.252.8.64) by SHFDAG1NODE1.st.com (10.75.129.69) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.21; Thu, 15 Jun 2023 11:32:24 +0200 From: Valentin Caron To: Alessandro Zummo , Alexandre Belloni CC: Alexandre Torgue , Antonio Borneo , Christophe Guibout , Gabriel Fernandez , , , , , Valentin Caron Subject: [PATCH 4/7] rtc: stm32: don't print an error on probe deferral Date: Thu, 15 Jun 2023 11:27:50 +0200 Message-ID: <20230615092753.323844-5-valentin.caron@foss.st.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230615092753.323844-1-valentin.caron@foss.st.com> References: <20230615092753.323844-1-valentin.caron@foss.st.com> MIME-Version: 1.0 X-Originating-IP: [10.252.8.64] X-ClientProxiedBy: SHFCAS1NODE2.st.com (10.75.129.73) To SHFDAG1NODE1.st.com (10.75.129.69) X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.957,Hydra:6.0.573,FMLib:17.11.176.26 definitions=2023-06-15_06,2023-06-14_02,2023-05-22_02 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230615_023228_558907_386BC9DF X-CRM114-Status: GOOD ( 18.14 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Change stm32-rtc driver to not generate an error message when device probe operation is deferred for a clock. Signed-off-by: Etienne Carriere Signed-off-by: Valentin Caron --- drivers/rtc/rtc-stm32.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/drivers/rtc/rtc-stm32.c b/drivers/rtc/rtc-stm32.c index cad88668bcfb..5ebf0b8e75f9 100644 --- a/drivers/rtc/rtc-stm32.c +++ b/drivers/rtc/rtc-stm32.c @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -759,16 +760,13 @@ static int stm32_rtc_probe(struct platform_device *pdev) rtc->rtc_ck = devm_clk_get(&pdev->dev, NULL); } else { rtc->pclk = devm_clk_get(&pdev->dev, "pclk"); - if (IS_ERR(rtc->pclk)) { - dev_err(&pdev->dev, "no pclk clock"); - return PTR_ERR(rtc->pclk); - } + if (IS_ERR(rtc->pclk)) + return dev_err_probe(&pdev->dev, PTR_ERR(rtc->pclk), "no pclk clock"); + rtc->rtc_ck = devm_clk_get(&pdev->dev, "rtc_ck"); } - if (IS_ERR(rtc->rtc_ck)) { - dev_err(&pdev->dev, "no rtc_ck clock"); - return PTR_ERR(rtc->rtc_ck); - } + if (IS_ERR(rtc->rtc_ck)) + return dev_err_probe(&pdev->dev, PTR_ERR(rtc->rtc_ck), "no rtc_ck clock"); if (rtc->data->has_pclk) { ret = clk_prepare_enable(rtc->pclk); From patchwork Thu Jun 15 09:27:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Valentin Caron X-Patchwork-Id: 13280953 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 3B3DFEB64D9 for ; Thu, 15 Jun 2023 09:33:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:CC:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=tPo5tp3R1wuOSIy8ceEh1CRFwjckdzcgKw/0BCcQMbU=; b=3Ke1uLZOmm1h5/ lIlcelyO2P38ya0xBuj8XA0wwJinna6A0onylu5wcMTPBuKl5bmtCUFRM43ytm1ij7X/oeUTrmHoc K52WdyuOzgS2RoBQ/L4NeRrbat3ky94ovJvIe2JSQMyhFl5AD+YbIM/5MLGKJVkcETBSHJswORQSm /minqmzbvOkxHj0Z6Usoyb0XWq/I6t6QeGgaENe7RAHmmGdMg28DTD8lcTxy7to8N1789ybNvgo9y ftnJAdwf2FQYPUMAjKxwCC7NMj6StLoCTNd9NRzknimc60n9zM9y1IKXfihZtpy4BsXaT/7Zj9DUL M2QbiKo3Px0jZQmvHJoQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1q9jME-00EJNk-0e; Thu, 15 Jun 2023 09:33:34 +0000 Received: from mx07-00178001.pphosted.com ([185.132.182.106]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1q9jM9-00EJLu-0w for linux-arm-kernel@lists.infradead.org; Thu, 15 Jun 2023 09:33:32 +0000 Received: from pps.filterd (m0288072.ppops.net [127.0.0.1]) by mx07-00178001.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 35F8IT69007631; Thu, 15 Jun 2023 11:33:27 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foss.st.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=selector1; bh=YzoRM1g1BfI9MnBB08CDwkG8AHGMKRSS788lXSXjecw=; b=nUR3E5hnlBrZqsJJkT3t4/7iH8fWx7QBNH+eiwEd/kwlFzZubambDIA9GNHAUqJGsyJS HyLi0r77FrdlM+8LYdJNtHBa+tkAZt6CcrpEeNfbczaDApFMlJHwXpIgP0hqm/ngLS1/ CUGvIDSWGVaZDTPFnqPmtVvdXOMDwKRpMVq0kJaSAcbVuWmcxKicfCFnuZ4I3RbgxZr9 1G71wrIekaSl1Km/6dADk3ytGXs1yX/Hf7tSL+SYepmh8/+v7AnPE80o+0tYwN6PkWZf EZDBAW57HD1QuYc8qRCNCgRE0EyB1jhZQ2sACyiqNqbRAlONp4dEHfFEHOo87OGvuzfA bQ== Received: from beta.dmz-eu.st.com (beta.dmz-eu.st.com [164.129.1.35]) by mx07-00178001.pphosted.com (PPS) with ESMTPS id 3r7s1may1v-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 15 Jun 2023 11:33:27 +0200 Received: from euls16034.sgp.st.com (euls16034.sgp.st.com [10.75.44.20]) by beta.dmz-eu.st.com (STMicroelectronics) with ESMTP id 3752610002A; Thu, 15 Jun 2023 11:33:26 +0200 (CEST) Received: from Webmail-eu.st.com (shfdag1node1.st.com [10.75.129.69]) by euls16034.sgp.st.com (STMicroelectronics) with ESMTP id 2E58421A91B; Thu, 15 Jun 2023 11:33:26 +0200 (CEST) Received: from localhost (10.252.8.64) by SHFDAG1NODE1.st.com (10.75.129.69) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.21; Thu, 15 Jun 2023 11:33:25 +0200 From: Valentin Caron To: Alessandro Zummo , Alexandre Belloni CC: Alexandre Torgue , Antonio Borneo , Christophe Guibout , Gabriel Fernandez , , , , , Valentin Caron Subject: [PATCH 5/7] rtc: stm32: change PM callbacks to "_noirq()" Date: Thu, 15 Jun 2023 11:27:51 +0200 Message-ID: <20230615092753.323844-6-valentin.caron@foss.st.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230615092753.323844-1-valentin.caron@foss.st.com> References: <20230615092753.323844-1-valentin.caron@foss.st.com> MIME-Version: 1.0 X-Originating-IP: [10.252.8.64] X-ClientProxiedBy: SHFCAS1NODE2.st.com (10.75.129.73) To SHFDAG1NODE1.st.com (10.75.129.69) X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.957,Hydra:6.0.573,FMLib:17.11.176.26 definitions=2023-06-15_06,2023-06-14_02,2023-05-22_02 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230615_023329_659475_BF6E98A0 X-CRM114-Status: GOOD ( 18.45 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Gabriel Fernandez The RTC driver stops the RTCAPB clock during suspend, but the irq handler from RTC is called before starting clock. Then we are blocked while accessing RTC registers. We changes PM callbacks to '_no_irq()' to disable irq during resume callback and so irq handler will be called after the enable of RTCAPB clock. Signed-off-by: Gabriel Fernandez Signed-off-by: Valentin Caron --- drivers/rtc/rtc-stm32.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/drivers/rtc/rtc-stm32.c b/drivers/rtc/rtc-stm32.c index 5ebf0b8e75f9..17e549806784 100644 --- a/drivers/rtc/rtc-stm32.c +++ b/drivers/rtc/rtc-stm32.c @@ -919,8 +919,9 @@ static int stm32_rtc_resume(struct device *dev) } #endif -static SIMPLE_DEV_PM_OPS(stm32_rtc_pm_ops, - stm32_rtc_suspend, stm32_rtc_resume); +static const struct dev_pm_ops stm32_rtc_pm_ops = { + SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(stm32_rtc_suspend, stm32_rtc_resume) +}; static struct platform_driver stm32_rtc_driver = { .probe = stm32_rtc_probe, From patchwork Thu Jun 15 09:27:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Valentin Caron X-Patchwork-Id: 13280954 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 46896EB64DC for ; Thu, 15 Jun 2023 09:33:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:CC:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=7PxBmbUjIfTkE4cJOQdLeQ0JdTxDrNkyUilagT5B/qI=; b=ICIK9P+7ay6vCR kZe9zfw9CEpDBUXXZMWFtMWYjVcu3+8QdYLJjPzHV1FlFHuYTQt/yNLve1vhdGduWDWecTbqZRK8q bQfOwTOzL4CE4FY/gC1UHpDaFCK4ZjhWYl1TB2ARU26LnjQUzcnYK8G0EjNMM7aOQ2Kn5kM2zRud4 wrCyjstlGLdhSXLrnN2S1F6uws2thbh2EyrcarPkBQcD4vhogsQTVBPYbw7j53BMz5nHoV2qcin6C kHiucQ9cRm1dsyb3puxM2yl9oZLFMLeLA0lmYfjhOAwaBAhQuwhKtEQ4n82DRJxxuwKV87slhYUmF GmNKnSwOrops9u8RhDlg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1q9jME-00EJO5-2D; Thu, 15 Jun 2023 09:33:34 +0000 Received: from mx07-00178001.pphosted.com ([185.132.182.106]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1q9jMA-00EJM4-0g for linux-arm-kernel@lists.infradead.org; Thu, 15 Jun 2023 09:33:32 +0000 Received: from pps.filterd (m0241204.ppops.net [127.0.0.1]) by mx07-00178001.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 35F8Yr9O021704; Thu, 15 Jun 2023 11:33:28 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foss.st.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=selector1; bh=4u7WnsfFSTX7SSmQl5Vo2PZjRXDH79xbQ6aVufDM0rY=; b=E5E8gkoYbihkBR1/QigXPsxLbKeuXTvqdQnCvhOLyIJQRVV7vpWTdP6OjWJEd+bYc+KZ aypLPTWyYh7ZhykvL9v1VPoemmRfkz7pNwDc0XZUQasSklgd7QArnHoEXDPLLOQ7KPL1 BedtARqF303jjZh+X9jodCA3PAMjC6V88zHMazpkRUZ2GQIWwqq0XIgrQuryLaSIAHTE 211L0s7KXBX6HbbtrTBrr8SGc74L4YrMgD2ly7F7HCZhKLANTPGBLUehrvqYedz1vMsP pbNUZowvvaupNkZ72tTsFDB5eC34HXfidNkHwoT6CWVF0o4/c2W1RhUDi9WBgINYphU4 bA== Received: from beta.dmz-eu.st.com (beta.dmz-eu.st.com [164.129.1.35]) by mx07-00178001.pphosted.com (PPS) with ESMTPS id 3r7y8sgedt-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 15 Jun 2023 11:33:27 +0200 Received: from euls16034.sgp.st.com (euls16034.sgp.st.com [10.75.44.20]) by beta.dmz-eu.st.com (STMicroelectronics) with ESMTP id 45B82100046; Thu, 15 Jun 2023 11:33:27 +0200 (CEST) Received: from Webmail-eu.st.com (shfdag1node1.st.com [10.75.129.69]) by euls16034.sgp.st.com (STMicroelectronics) with ESMTP id 3EA0E21A91B; Thu, 15 Jun 2023 11:33:27 +0200 (CEST) Received: from localhost (10.252.8.64) by SHFDAG1NODE1.st.com (10.75.129.69) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.21; Thu, 15 Jun 2023 11:33:26 +0200 From: Valentin Caron To: Alessandro Zummo , Alexandre Belloni CC: Alexandre Torgue , Antonio Borneo , Christophe Guibout , Gabriel Fernandez , , , , , Valentin Caron Subject: [PATCH 6/7] rtc: stm32: fix unnecessary parentheses Date: Thu, 15 Jun 2023 11:27:52 +0200 Message-ID: <20230615092753.323844-7-valentin.caron@foss.st.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230615092753.323844-1-valentin.caron@foss.st.com> References: <20230615092753.323844-1-valentin.caron@foss.st.com> MIME-Version: 1.0 X-Originating-IP: [10.252.8.64] X-ClientProxiedBy: SHFCAS1NODE2.st.com (10.75.129.73) To SHFDAG1NODE1.st.com (10.75.129.69) X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.957,Hydra:6.0.573,FMLib:17.11.176.26 definitions=2023-06-15_06,2023-06-14_02,2023-05-22_02 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230615_023330_568749_36F095EA X-CRM114-Status: GOOD ( 19.18 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Fix a few style issues reported by checkpatch.pl: - Unnecessary parentheses - Lines should not end with a '(' Signed-off-by: Valentin Caron --- drivers/rtc/rtc-stm32.c | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/drivers/rtc/rtc-stm32.c b/drivers/rtc/rtc-stm32.c index 17e549806784..30c5004d6902 100644 --- a/drivers/rtc/rtc-stm32.c +++ b/drivers/rtc/rtc-stm32.c @@ -160,10 +160,9 @@ static int stm32_rtc_enter_init_mode(struct stm32_rtc *rtc) * slowest rtc_ck frequency may be 32kHz and highest should be * 1MHz, we poll every 10 us with a timeout of 100ms. */ - return readl_relaxed_poll_timeout_atomic( - rtc->base + regs->isr, - isr, (isr & STM32_RTC_ISR_INITF), - 10, 100000); + return readl_relaxed_poll_timeout_atomic(rtc->base + regs->isr, isr, + (isr & STM32_RTC_ISR_INITF), + 10, 100000); } return 0; @@ -448,16 +447,16 @@ static int stm32_rtc_valid_alrm(struct stm32_rtc *rtc, struct rtc_time *tm) * M-D-Y H:M:S < alarm <= (M+1)-D-Y H:M:S * with a specific case for December... */ - if ((((tm->tm_year > cur_year) && - (tm->tm_mon == 0x1) && (cur_mon == 0x12)) || - ((tm->tm_year == cur_year) && - (tm->tm_mon <= cur_mon + 1))) && - ((tm->tm_mday > cur_day) || - ((tm->tm_mday == cur_day) && - ((tm->tm_hour > cur_hour) || - ((tm->tm_hour == cur_hour) && (tm->tm_min > cur_min)) || - ((tm->tm_hour == cur_hour) && (tm->tm_min == cur_min) && - (tm->tm_sec >= cur_sec)))))) + if (((tm->tm_year > cur_year && + tm->tm_mon == 0x1 && cur_mon == 0x12) || + (tm->tm_year == cur_year && + tm->tm_mon <= cur_mon + 1)) && + (tm->tm_mday > cur_day || + (tm->tm_mday == cur_day && + (tm->tm_hour > cur_hour || + (tm->tm_hour == cur_hour && tm->tm_min > cur_min) || + (tm->tm_hour == cur_hour && tm->tm_min == cur_min && + tm->tm_sec >= cur_sec))))) return 0; return -EINVAL; @@ -666,7 +665,7 @@ static int stm32_rtc_init(struct platform_device *pdev, * Can't find a 1Hz, so give priority to RTC power consumption * by choosing the higher possible value for prediv_a */ - if ((pred_s > pred_s_max) || (pred_a > pred_a_max)) { + if (pred_s > pred_s_max || pred_a > pred_a_max) { pred_a = pred_a_max; pred_s = (rate / (pred_a + 1)) - 1; From patchwork Thu Jun 15 09:27:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Valentin Caron X-Patchwork-Id: 13280955 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 7FE48EB64DB for ; Thu, 15 Jun 2023 09:33:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:CC:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=t6Hp0GHMtb8CRPelQpfay55q/8bAm7fYhWbG45ESOVU=; b=zODpf2hse07ySf sbR3kbcySrCcCIXzletYArjxxbEC/C0nBYqgwLrSqr3GkrI2OUBN9MHp/xpdUafWRG4dJmapk8nsc kJRCsqiTbdOeB0vIZOHsWkCxBUiX0slVCHkV4qoBmtfxGiFa0+7xJq60pu09HzeiRetwFOOuLWEbv ULGP82JCOKfN3VXINhRPMN+bi3uBu3LVo6yF4dg0Ec0LdkaNpKjc6CCTJbGJr1/joSUMQ6zQo4Ugq +c1nacMK8Gzgw6MfHiJji5fSGUR5EbeJiG2LdJEgHjS7IU7jASlMvz7At8FTbhlmtiiydzdN8gDI3 tWajKYNP3Y936sBV3E1w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1q9jMF-00EJOR-0W; Thu, 15 Jun 2023 09:33:35 +0000 Received: from mx08-00178001.pphosted.com ([91.207.212.93] helo=mx07-00178001.pphosted.com) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1q9jMA-00EJME-2H for linux-arm-kernel@lists.infradead.org; Thu, 15 Jun 2023 09:33:32 +0000 Received: from pps.filterd (m0046660.ppops.net [127.0.0.1]) by mx07-00178001.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 35F7x49D022782; Thu, 15 Jun 2023 11:33:28 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foss.st.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=selector1; bh=KGfRr9Mz7eZ6U4XP0pLjyw89b0Du0XVaJReLW6qPtx4=; b=mk1/mPY1HSWhbRxWPlj0lTUPngKYGkwjV+MHtygtAP72leBZV5yWBpCgtF42uyOA99NO Pg80w7pNf07wrmHKvJYwyN7aShRqOul+nnxW4oVQMegg1tM+xo7Eu5sNt5LYIA17/3I4 6/19sf61NegZvoesTiMRFCNETwlris7UuM2dhi5m15gPqjJPxjyBykLiujn/4aku4JI7 bu5P/dywewnsrG0lyzNsndqrs/TX+A6WyYvv07p65ZQD+gUBliNFmUmFgsb4hqdn/qc8 +LVJIMB9V1+dH5nVUuLdOg0wMVm0OyDNhsylkPsdh4SYzKaTkK46Qb2ExchfsD3MoJ6l PA== Received: from beta.dmz-eu.st.com (beta.dmz-eu.st.com [164.129.1.35]) by mx07-00178001.pphosted.com (PPS) with ESMTPS id 3r7vkfst7t-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 15 Jun 2023 11:33:28 +0200 Received: from euls16034.sgp.st.com (euls16034.sgp.st.com [10.75.44.20]) by beta.dmz-eu.st.com (STMicroelectronics) with ESMTP id 610F110002A; Thu, 15 Jun 2023 11:33:28 +0200 (CEST) Received: from Webmail-eu.st.com (shfdag1node1.st.com [10.75.129.69]) by euls16034.sgp.st.com (STMicroelectronics) with ESMTP id 59AC221A91B; Thu, 15 Jun 2023 11:33:28 +0200 (CEST) Received: from localhost (10.252.8.64) by SHFDAG1NODE1.st.com (10.75.129.69) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.21; Thu, 15 Jun 2023 11:33:27 +0200 From: Valentin Caron To: Alessandro Zummo , Alexandre Belloni CC: Alexandre Torgue , Antonio Borneo , Christophe Guibout , Gabriel Fernandez , , , , , Valentin Caron Subject: [PATCH 7/7] rtc: stm32: fix issues of stm32_rtc_valid_alrm function Date: Thu, 15 Jun 2023 11:27:53 +0200 Message-ID: <20230615092753.323844-8-valentin.caron@foss.st.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230615092753.323844-1-valentin.caron@foss.st.com> References: <20230615092753.323844-1-valentin.caron@foss.st.com> MIME-Version: 1.0 X-Originating-IP: [10.252.8.64] X-ClientProxiedBy: SHFCAS1NODE2.st.com (10.75.129.73) To SHFDAG1NODE1.st.com (10.75.129.69) X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.957,Hydra:6.0.573,FMLib:17.11.176.26 definitions=2023-06-15_06,2023-06-14_02,2023-05-22_02 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230615_023331_031849_307639FF X-CRM114-Status: GOOD ( 26.65 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org stm32_rtc_valid_alrm function has some issues : - arithmetical operations are impossible on BCD values - "cur_mon + 1" can overflow - the use case with the next month, the same day/hour/minutes went wrong To solve that, we prefer to use timestamp comparison. e.g. : On 5 Dec. 2021, the alarm limit is 5 Jan. 2022 (+31 days) On 31 Jan 2021, the alarm limit is 28 Feb. 2022 (+28 days) Signed-off-by: Valentin Caron --- drivers/rtc/rtc-stm32.c | 61 ++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/drivers/rtc/rtc-stm32.c b/drivers/rtc/rtc-stm32.c index 30c5004d6902..85689192fa7a 100644 --- a/drivers/rtc/rtc-stm32.c +++ b/drivers/rtc/rtc-stm32.c @@ -90,6 +90,9 @@ /* Max STM32 RTC register offset is 0x3FC */ #define UNDEF_REG 0xFFFF +/* STM32 RTC driver time helpers */ +#define SEC_PER_DAY (24 * 60 * 60) + struct stm32_rtc; struct stm32_rtc_registers { @@ -426,40 +429,42 @@ static int stm32_rtc_alarm_irq_enable(struct device *dev, unsigned int enabled) return 0; } -static int stm32_rtc_valid_alrm(struct stm32_rtc *rtc, struct rtc_time *tm) +static int stm32_rtc_valid_alrm(struct device *dev, struct rtc_time *tm) { - const struct stm32_rtc_registers *regs = &rtc->data->regs; - int cur_day, cur_mon, cur_year, cur_hour, cur_min, cur_sec; - unsigned int tr = readl_relaxed(rtc->base + regs->tr); - unsigned int dr = readl_relaxed(rtc->base + regs->dr); - - cur_day = (dr & STM32_RTC_DR_DATE) >> STM32_RTC_DR_DATE_SHIFT; - cur_mon = (dr & STM32_RTC_DR_MONTH) >> STM32_RTC_DR_MONTH_SHIFT; - cur_year = (dr & STM32_RTC_DR_YEAR) >> STM32_RTC_DR_YEAR_SHIFT; - cur_sec = (tr & STM32_RTC_TR_SEC) >> STM32_RTC_TR_SEC_SHIFT; - cur_min = (tr & STM32_RTC_TR_MIN) >> STM32_RTC_TR_MIN_SHIFT; - cur_hour = (tr & STM32_RTC_TR_HOUR) >> STM32_RTC_TR_HOUR_SHIFT; + static struct rtc_time now; + time64_t max_alarm_time64; + int max_day_forward; + int next_month; + int next_year; /* * Assuming current date is M-D-Y H:M:S. * RTC alarm can't be set on a specific month and year. * So the valid alarm range is: * M-D-Y H:M:S < alarm <= (M+1)-D-Y H:M:S - * with a specific case for December... */ - if (((tm->tm_year > cur_year && - tm->tm_mon == 0x1 && cur_mon == 0x12) || - (tm->tm_year == cur_year && - tm->tm_mon <= cur_mon + 1)) && - (tm->tm_mday > cur_day || - (tm->tm_mday == cur_day && - (tm->tm_hour > cur_hour || - (tm->tm_hour == cur_hour && tm->tm_min > cur_min) || - (tm->tm_hour == cur_hour && tm->tm_min == cur_min && - tm->tm_sec >= cur_sec))))) - return 0; + stm32_rtc_read_time(dev, &now); + + /* + * Find the next month and the year of the next month. + * Note: tm_mon and next_month are from 0 to 11 + */ + next_month = now.tm_mon + 1; + if (next_month == 12) { + next_month = 0; + next_year = now.tm_year + 1; + } else { + next_year = now.tm_year; + } - return -EINVAL; + /* Find the maximum limit of alarm in days. */ + max_day_forward = rtc_month_days(now.tm_mon, now.tm_year) + - now.tm_mday + + min(rtc_month_days(next_month, next_year), now.tm_mday); + + /* Convert to timestamp and compare the alarm time and its upper limit */ + max_alarm_time64 = rtc_tm_to_time64(&now) + max_day_forward * SEC_PER_DAY; + return rtc_tm_to_time64(tm) <= max_alarm_time64 ? 0 : -EINVAL; } static int stm32_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) @@ -470,17 +475,17 @@ static int stm32_rtc_set_alarm(struct device *dev, struct rtc_wkalrm *alrm) unsigned int cr, isr, alrmar; int ret = 0; - tm2bcd(tm); - /* * RTC alarm can't be set on a specific date, unless this date is * up to the same day of month next month. */ - if (stm32_rtc_valid_alrm(rtc, tm) < 0) { + if (stm32_rtc_valid_alrm(dev, tm) < 0) { dev_err(dev, "Alarm can be set only on upcoming month.\n"); return -EINVAL; } + tm2bcd(tm); + alrmar = 0; /* tm_year and tm_mon are not used because not supported by RTC */ alrmar |= (tm->tm_mday << STM32_RTC_ALRMXR_DATE_SHIFT) &