From: Paul Cercueil <paul@crapouillou.net>
To: Alessandro Zummo <a.zummo@towertech.it>,
Alexandre Belloni <alexandre.belloni@bootlin.com>
Cc: list@opendingux.net, linux-rtc@vger.kernel.org,
devicetree@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-mips@vger.kernel.org, Paul Cercueil <paul@crapouillou.net>
Subject: [PATCH 3/5] rtc: jz4740: Reset scratchpad register on power loss
Date: Mon, 18 Apr 2022 19:49:31 +0100 [thread overview]
Message-ID: <20220418184933.13172-4-paul@crapouillou.net> (raw)
In-Reply-To: <20220418184933.13172-1-paul@crapouillou.net>
On power loss, reading the RTC value would fail as the scratchpad lost
its magic value, until the hardware clock was set once again.
To avoid that, reset the RTC value to Epoch in the probe if we detect
that the scratchpad lost its magic value.
Signed-off-by: Paul Cercueil <paul@crapouillou.net>
---
drivers/rtc/rtc-jz4740.c | 24 +++++++++++++++++++++---
1 file changed, 21 insertions(+), 3 deletions(-)
diff --git a/drivers/rtc/rtc-jz4740.c b/drivers/rtc/rtc-jz4740.c
index 119baf168b32..aac5f68bf626 100644
--- a/drivers/rtc/rtc-jz4740.c
+++ b/drivers/rtc/rtc-jz4740.c
@@ -42,6 +42,9 @@
/* Magic value to enable writes on jz4780 */
#define JZ_RTC_WENR_MAGIC 0xA55A
+/* Value written to the scratchpad to detect power losses */
+#define JZ_RTC_SCRATCHPAD_MAGIC 0x12345678
+
#define JZ_RTC_WAKEUP_FILTER_MASK 0x0000FFE0
#define JZ_RTC_RESET_COUNTER_MASK 0x00000FE0
@@ -134,10 +137,11 @@ static int jz4740_rtc_ctrl_set_bits(struct jz4740_rtc *rtc, uint32_t mask,
static int jz4740_rtc_read_time(struct device *dev, struct rtc_time *time)
{
struct jz4740_rtc *rtc = dev_get_drvdata(dev);
- uint32_t secs, secs2;
+ uint32_t secs, secs2, magic;
int timeout = 5;
- if (jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SCRATCHPAD) != 0x12345678)
+ magic = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SCRATCHPAD);
+ if (magic != JZ_RTC_SCRATCHPAD_MAGIC)
return -EINVAL;
/* If the seconds register is read while it is updated, it can contain a
@@ -169,7 +173,8 @@ static int jz4740_rtc_set_time(struct device *dev, struct rtc_time *time)
if (ret)
return ret;
- return jz4740_rtc_reg_write(rtc, JZ_REG_RTC_SCRATCHPAD, 0x12345678);
+ return jz4740_rtc_reg_write(rtc, JZ_REG_RTC_SCRATCHPAD,
+ JZ_RTC_SCRATCHPAD_MAGIC);
}
static int jz4740_rtc_read_alarm(struct device *dev, struct rtc_wkalrm *alrm)
@@ -307,6 +312,7 @@ static int jz4740_rtc_probe(struct platform_device *pdev)
struct jz4740_rtc *rtc;
unsigned long rate;
struct clk *clk;
+ uint32_t magic;
int ret, irq;
rtc = devm_kzalloc(dev, sizeof(*rtc), GFP_KERNEL);
@@ -369,6 +375,18 @@ static int jz4740_rtc_probe(struct platform_device *pdev)
/* Each 1 Hz pulse should happen after (rate) ticks */
jz4740_rtc_reg_write(rtc, JZ_REG_RTC_REGULATOR, rate - 1);
+ magic = jz4740_rtc_reg_read(rtc, JZ_REG_RTC_SCRATCHPAD);
+ if (magic != JZ_RTC_SCRATCHPAD_MAGIC) {
+ /*
+ * If the scratchpad doesn't hold our magic value, then a
+ * power loss occurred. Reset to Epoch.
+ */
+ struct rtc_time time;
+
+ rtc_time64_to_tm(0, &time);
+ jz4740_rtc_set_time(dev, &time);
+ }
+
ret = devm_rtc_register_device(rtc->rtc);
if (ret)
return ret;
--
2.35.1
next prev parent reply other threads:[~2022-04-18 18:50 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-04-18 18:49 [PATCH 0/5] rtc: ingenic: various updates Paul Cercueil
2022-04-18 18:49 ` [PATCH 1/5] dt-bindings: rtc: Rework compatible strings and add #clock-cells Paul Cercueil
2022-04-19 6:41 ` Krzysztof Kozlowski
2022-04-19 16:55 ` Paul Cercueil
2022-04-18 18:49 ` [PATCH 2/5] rtc: jz4740: Use readl_poll_timeout Paul Cercueil
2022-04-18 18:49 ` Paul Cercueil [this message]
2022-04-19 19:35 ` [PATCH 3/5] rtc: jz4740: Reset scratchpad register on power loss Alexandre Belloni
2022-04-19 19:48 ` Paul Cercueil
2022-04-19 20:00 ` Alexandre Belloni
2022-04-19 20:53 ` Paul Cercueil
2022-05-16 13:56 ` Alexandre Belloni
2022-04-18 18:49 ` [PATCH 4/5] rtc: jz4740: Register clock provider for the CLK32K pin Paul Cercueil
2022-04-19 4:30 ` kernel test robot
2022-04-19 5:45 ` kernel test robot
2022-04-18 18:49 ` [PATCH 5/5] rtc: jz4740: Support for fine-tuning the RTC clock Paul Cercueil
2022-04-19 4:32 ` kernel test robot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220418184933.13172-4-paul@crapouillou.net \
--to=paul@crapouillou.net \
--cc=a.zummo@towertech.it \
--cc=alexandre.belloni@bootlin.com \
--cc=devicetree@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mips@vger.kernel.org \
--cc=linux-rtc@vger.kernel.org \
--cc=list@opendingux.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®