linux-watchdog.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Christophe Roullier <christophe.roullier@st.com>
To: <wim@linux-watchdog.org>, <linux@roeck-us.net>,
	<mcoquelin.stm32@gmail.com>, <alexandre.torgue@st.com>
Cc: <linux-watchdog@vger.kernel.org>, <christophe.roullier@st.com>,
	<linux-kernel@vger.kernel.org>,
	<linux-stm32@st-md-mailman.stormreply.com>,
	<linux-arm-kernel@lists.infradead.org>
Subject: [PATCH  1/1] drivers: watchdog: stm32_iwdg: set WDOG_HW_RUNNING at probe
Date: Thu, 21 Nov 2019 09:28:13 +0100	[thread overview]
Message-ID: <20191121082813.29267-2-christophe.roullier@st.com> (raw)
In-Reply-To: <20191121082813.29267-1-christophe.roullier@st.com>

If the watchdog hardware is already enabled during the boot process,
when the Linux watchdog driver loads, it should reset the watchdog and
tell the watchdog framework. As a result, ping can be generated from
the watchdog framework (if CONFIG_WATCHDOG_HANDLE_BOOT_ENABLED is set),
until the userspace watchdog daemon takes over control

Fixes:4332d113c66a ("watchdog: Add STM32 IWDG driver")
Signed-off-by: Christophe Roullier <christophe.roullier@st.com>
---
 drivers/watchdog/stm32_iwdg.c | 57 ++++++++++++++++++++++++-----------
 1 file changed, 40 insertions(+), 17 deletions(-)

diff --git a/drivers/watchdog/stm32_iwdg.c b/drivers/watchdog/stm32_iwdg.c
index a3a329011a06..2b3be3b1c15b 100644
--- a/drivers/watchdog/stm32_iwdg.c
+++ b/drivers/watchdog/stm32_iwdg.c
@@ -87,8 +87,23 @@ static inline void reg_write(void __iomem *base, u32 reg, u32 val)
 static int stm32_iwdg_start(struct watchdog_device *wdd)
 {
 	struct stm32_iwdg *wdt = watchdog_get_drvdata(wdd);
-	u32 tout, presc, iwdg_rlr, iwdg_pr, iwdg_sr;
-	int ret;
+
+	dev_dbg(wdd->parent, "%s\n", __func__);
+
+	/*  Start the watchdog */
+	reg_write(wdt->regs, IWDG_KR, KR_KEY_ENABLE);
+
+	/* reload watchdog */
+	reg_write(wdt->regs, IWDG_KR, KR_KEY_RELOAD);
+
+	set_bit(WDOG_HW_RUNNING, &wdd->status);
+	return 0;
+}
+
+static int stm32_iwdg_setprescaler(struct watchdog_device *wdd)
+{
+	struct stm32_iwdg *wdt = watchdog_get_drvdata(wdd);
+	u32 tout, presc, iwdg_rlr, iwdg_pr;
 
 	dev_dbg(wdd->parent, "%s\n", __func__);
 
@@ -108,19 +123,6 @@ static int stm32_iwdg_start(struct watchdog_device *wdd)
 	/* set prescaler & reload registers */
 	reg_write(wdt->regs, IWDG_PR, iwdg_pr);
 	reg_write(wdt->regs, IWDG_RLR, iwdg_rlr);
-	reg_write(wdt->regs, IWDG_KR, KR_KEY_ENABLE);
-
-	/* wait for the registers to be updated (max 100ms) */
-	ret = readl_relaxed_poll_timeout(wdt->regs + IWDG_SR, iwdg_sr,
-					 !(iwdg_sr & (SR_PVU | SR_RVU)),
-					 SLEEP_US, TIMEOUT_US);
-	if (ret) {
-		dev_err(wdd->parent, "Fail to set prescaler, reload regs\n");
-		return ret;
-	}
-
-	/* reload watchdog */
-	reg_write(wdt->regs, IWDG_KR, KR_KEY_RELOAD);
 
 	return 0;
 }
@@ -131,6 +133,9 @@ static int stm32_iwdg_ping(struct watchdog_device *wdd)
 
 	dev_dbg(wdd->parent, "%s\n", __func__);
 
+	/*  Start the watchdog */
+	reg_write(wdt->regs, IWDG_KR, KR_KEY_ENABLE);
+
 	/* reload watchdog */
 	reg_write(wdt->regs, IWDG_KR, KR_KEY_RELOAD);
 
@@ -140,12 +145,21 @@ static int stm32_iwdg_ping(struct watchdog_device *wdd)
 static int stm32_iwdg_set_timeout(struct watchdog_device *wdd,
 				  unsigned int timeout)
 {
+	int ret;
+
 	dev_dbg(wdd->parent, "%s timeout: %d sec\n", __func__, timeout);
 
 	wdd->timeout = timeout;
 
-	if (watchdog_active(wdd))
-		return stm32_iwdg_start(wdd);
+	if (watchdog_active(wdd)) {
+		ret = stm32_iwdg_setprescaler(wdd);
+		if (ret) {
+			dev_err(wdd->parent, "failed to set prescaler\n");
+			return ret;
+		} else {
+			return stm32_iwdg_start(wdd);
+		}
+	}
 
 	return 0;
 }
@@ -262,12 +276,21 @@ static int stm32_iwdg_probe(struct platform_device *pdev)
 	watchdog_set_nowayout(wdd, WATCHDOG_NOWAYOUT);
 	watchdog_init_timeout(wdd, 0, dev);
 
+	/* Make sure the watchdog is serviced */
+	set_bit(WDOG_HW_RUNNING, &wdd->status);
+
 	ret = devm_watchdog_register_device(dev, wdd);
 	if (ret)
 		return ret;
 
 	platform_set_drvdata(pdev, wdt);
 
+	ret = stm32_iwdg_setprescaler(wdd);
+	if (ret) {
+		dev_err(dev, "failed to set prescaler\n");
+		return ret;
+	}
+
 	return 0;
 }
 
-- 
2.17.1


  reply	other threads:[~2019-11-21  8:29 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-21  8:28 [PATCH 0/1] stm32_iwdg: set WDOG_HW_RUNNING at probe Christophe Roullier
2019-11-21  8:28 ` Christophe Roullier [this message]
2019-11-21  9:53   ` [PATCH 1/1] drivers: watchdog: " Guenter Roeck
2019-11-21 13:45     ` Christophe ROULLIER
2019-11-21 14:21       ` Guenter Roeck

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=20191121082813.29267-2-christophe.roullier@st.com \
    --to=christophe.roullier@st.com \
    --cc=alexandre.torgue@st.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-stm32@st-md-mailman.stormreply.com \
    --cc=linux-watchdog@vger.kernel.org \
    --cc=linux@roeck-us.net \
    --cc=mcoquelin.stm32@gmail.com \
    --cc=wim@linux-watchdog.org \
    /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;
as well as URLs for NNTP newsgroup(s).