From patchwork Fri Jan 20 19:01:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Doug Berger X-Patchwork-Id: 13110413 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 1FA98C05027 for ; Fri, 20 Jan 2023 19:03:57 +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=0noBd+1T/8dBfN7G5Mx4WEr1IAjZPdgFIcM6J1CPQoc=; b=A23kIt/uueKHXd OIoRkr9gb+onAcybiy+nPh+FOyRtGZ35Yr3pCyKm8eT3ebrasdKEG+2HbDrl3yJh2W0qvg7hYXLXS asOlz7aYKu7UGPoYVyECkQR/ILcPf99yPkdQnz4Kea/Y4BrxCjMkuD9mnNSpZZwAKF6KE5NG9ZPza zJcHxE7eTHU26AAfUWZMV4JhWewJ57rwrX8JqoV4Jv74SQoTZoboivdM7PsxjVBUALzrgW3/cahMK mPM2ymHBwnylG+aN+46njg1uflywOFabUs/dOgvuDaTgyDcZg0jLWCGiuYvZ6EzgtFPcytLE7tP7k zVyhpNDJX/HTJ9RsvjjQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1pIwfE-00BwqE-1W; Fri, 20 Jan 2023 19:03:00 +0000 Received: from mail-il1-x129.google.com ([2607:f8b0:4864:20::129]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1pIwek-00BwfD-5S for linux-arm-kernel@lists.infradead.org; Fri, 20 Jan 2023 19:02:31 +0000 Received: by mail-il1-x129.google.com with SMTP id b15so1035752ils.11 for ; Fri, 20 Jan 2023 11:02:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=5rhB+jCWXlDHeyaWPTfhoKZG0YZcAyGqC3yxhLv7U6w=; b=lknvwgacWgowNZLC4XECbtZKNlv6HqjFRHYejN+rgvjaZ5WNBGQWmWzQGZ+QtDTGRk Ca/IdGy/Fp/yIQEIdXDjKJas19zHXmPUiJgkKEyInmq/R1f/nIo/PkFD1Lwa0AG9NNTd b1jjB77CEuqFd5Z47Rn1DLAuM2pzQ71u/gUwp1bhMiRdLAcWPnNS/NMNBQnEhOtmLsPC 2p5Pg0OMGn4IewYGDV9wTo8Sk5SkX02wnx3TlsEkIuEFcSuD2IsTdwI9kXUkq7zWXMq8 +ccujkOLxcCK6C8lVneugYs6LpdYvkYuXnazLtwvjRG3osyWhJ6FbpndCJlfIup/+bVy h0MQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=5rhB+jCWXlDHeyaWPTfhoKZG0YZcAyGqC3yxhLv7U6w=; b=WeGgvvJdBn6yJxcwS1iokk6YSrxT01/Au/F+yzDyvdRqPccTuFu3ebiDsDXEGHVtjK bNRU4FtOeQp9i3iZDIfZkK7ZJthL6vQOFU1M75I71R1WGbY01mbTNWpV0r9IHvyjH0Dt JChclGDndIfraN8Zc/N7tr8+SLD7rysIUXw/gHeE+BuC10TQH8PzWr0Y7ut/IPJtz3i8 tmmkID46g9oZ21knD4gFxs0zWLSsAN6C4oi8ok10T4ZY/cqPT16DfTH231/2hmVeYeyV TEATXkfmQ9xc3UYYg4E4fRfBgwHXIxnkIWgxwSnrJZKtB9ZE1VuiczU7URdvJeabKkS3 M6ag== X-Gm-Message-State: AFqh2kqKO9DtZf2jJUqb9gMw+1dTglrAXOcoosynRom0t2ktK1UNj9ZK zvecRji9qFYOd8nqAoIf0+U= X-Google-Smtp-Source: AMrXdXv80xjIQ0RsNbyGUUz57F77A94w0EL9s2R0Rrmto+KRjhEpamBGGAuY5ReJGlNNSVACcPF0Ow== X-Received: by 2002:a05:6e02:6cc:b0:30c:4846:57c3 with SMTP id p12-20020a056e0206cc00b0030c484657c3mr12050911ils.4.1674241348972; Fri, 20 Jan 2023 11:02:28 -0800 (PST) Received: from stbirv-lnx-3.igp.broadcom.net ([192.19.223.252]) by smtp.gmail.com with ESMTPSA id y13-20020a37f60d000000b006fa2b1c3c1esm10379806qkj.58.2023.01.20.11.02.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Jan 2023 11:02:28 -0800 (PST) From: Doug Berger To: Alessandro Zummo , Alexandre Belloni Cc: Brian Norris , Markus Mayer , Rob Herring , Krzysztof Kozlowski , Florian Fainelli , Broadcom internal kernel review list , linux-rtc@vger.kernel.org, devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Doug Berger Subject: [PATCH 3/6] rtc: brcmstb-waketimer: compensate for lack of wktmr disable Date: Fri, 20 Jan 2023 11:01:44 -0800 Message-Id: <20230120190147.718976-4-opendmb@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230120190147.718976-1-opendmb@gmail.com> References: <20230120190147.718976-1-opendmb@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230120_110230_263071_11E71A08 X-CRM114-Status: GOOD ( 22.47 ) 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 Since the WKTMR hardware block cannot be disabled it is necessary for the driver to accommodate for associated timing hazards. This commit targets the following possibilities: A possible race between clearing a wktmr event and the alarm expiring is made one-sided by setting the alarm to its maximum value before clearing the event. Programming alarm values close to the current time may not trigger events if the counter advances while the alarm is being programmed. After programming an alarm, a check is made to ensure that it is either in the future or an expiration event is pending. Signed-off-by: Doug Berger Acked-by: Florian Fainelli --- drivers/rtc/rtc-brcmstb-waketimer.c | 52 +++++++++++++++++++---------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/drivers/rtc/rtc-brcmstb-waketimer.c b/drivers/rtc/rtc-brcmstb-waketimer.c index 582c23793550..c791e92532b8 100644 --- a/drivers/rtc/rtc-brcmstb-waketimer.c +++ b/drivers/rtc/rtc-brcmstb-waketimer.c @@ -31,6 +31,8 @@ struct brcmstb_waketmr { struct notifier_block reboot_notifier; struct clk *clk; u32 rate; + unsigned long rtc_alarm; + bool alarm_en; }; #define BRCMSTB_WKTMR_EVENT 0x00 @@ -52,6 +54,11 @@ static inline bool brcmstb_waketmr_is_pending(struct brcmstb_waketmr *timer) static inline void brcmstb_waketmr_clear_alarm(struct brcmstb_waketmr *timer) { + u32 reg; + + timer->alarm_en = false; + reg = readl_relaxed(timer->base + BRCMSTB_WKTMR_COUNTER); + writel_relaxed(reg - 1, timer->base + BRCMSTB_WKTMR_ALARM); writel_relaxed(WKTMR_ALARM_EVENT, timer->base + BRCMSTB_WKTMR_EVENT); (void)readl_relaxed(timer->base + BRCMSTB_WKTMR_EVENT); } @@ -59,12 +66,22 @@ static inline void brcmstb_waketmr_clear_alarm(struct brcmstb_waketmr *timer) static void brcmstb_waketmr_set_alarm(struct brcmstb_waketmr *timer, unsigned int secs) { + unsigned int now; + brcmstb_waketmr_clear_alarm(timer); /* Make sure we are actually counting in seconds */ writel_relaxed(timer->rate, timer->base + BRCMSTB_WKTMR_PRESCALER); - writel_relaxed(secs + 1, timer->base + BRCMSTB_WKTMR_ALARM); + writel_relaxed(secs, timer->base + BRCMSTB_WKTMR_ALARM); + now = readl_relaxed(timer->base + BRCMSTB_WKTMR_COUNTER); + + while ((int)(secs - now) <= 0 && + !brcmstb_waketmr_is_pending(timer)) { + secs = now + 1; + writel_relaxed(secs, timer->base + BRCMSTB_WKTMR_ALARM); + now = readl_relaxed(timer->base + BRCMSTB_WKTMR_COUNTER); + } } static irqreturn_t brcmstb_waketmr_irq(int irq, void *data) @@ -155,27 +172,30 @@ static int brcmstb_waketmr_getalarm(struct device *dev, struct rtc_wkalrm *alarm) { struct brcmstb_waketmr *timer = dev_get_drvdata(dev); - time64_t sec; - sec = readl_relaxed(timer->base + BRCMSTB_WKTMR_ALARM); - if (sec != 0) { - /* Alarm is enabled */ - alarm->enabled = 1; - rtc_time64_to_tm(sec, &alarm->time); - } + alarm->enabled = timer->alarm_en; + rtc_time64_to_tm(timer->rtc_alarm, &alarm->time); alarm->pending = brcmstb_waketmr_is_pending(timer); return 0; } -/* - * Does not do much but keep the RTC class happy. We always support - * alarms. - */ static int brcmstb_waketmr_alarm_enable(struct device *dev, unsigned int enabled) { + struct brcmstb_waketmr *timer = dev_get_drvdata(dev); + + if (enabled && !timer->alarm_en) { + if ((int)(readl_relaxed(timer->base + BRCMSTB_WKTMR_COUNTER) - + readl_relaxed(timer->base + BRCMSTB_WKTMR_ALARM)) >= 0 && + !brcmstb_waketmr_is_pending(timer)) + return -EINVAL; + timer->alarm_en = true; + } else if (!enabled && timer->alarm_en) { + timer->alarm_en = false; + } + return 0; } @@ -183,14 +203,10 @@ static int brcmstb_waketmr_setalarm(struct device *dev, struct rtc_wkalrm *alarm) { struct brcmstb_waketmr *timer = dev_get_drvdata(dev); - time64_t sec; - if (alarm->enabled) - sec = rtc_tm_to_time64(&alarm->time); - else - sec = 0; + timer->rtc_alarm = rtc_tm_to_time64(&alarm->time); - brcmstb_waketmr_set_alarm(timer, sec); + brcmstb_waketmr_set_alarm(timer, timer->rtc_alarm); return brcmstb_waketmr_alarm_enable(dev, alarm->enabled); }