Skip to contentSkip to conversation

watchdog: da9063: Fix setting/changing timeout

PATCH Superseded Marco Felsch 3 participants 4 messages May 17, 2018

Subsystems (2)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 watchdog: da9063: Fix setting/changing timeout

If the timeout value is set more than once the DA9063 watchdog triggers a reset signal which reset the system.

To update the timeout value we have to disable the watchdog, clear the watchdog counter value and write the new timeout value to the watchdog. Clearing the counter value is a feature to be on the safe side because the data sheet doesn't describe the behaviour of the watchdog counter value after a watchdog disabling-enable-sequence.

The patch is based on Philipp Zabel's previous patch but doesn't wait 150us because the DA9063 doesn't need this delay.

Fixes 5e9c16e37608 ("watchdog: Add DA9063 PMIC watchdog driver.")
---
 drivers/watchdog/da9063_wdt.c | 35 +++++++++++++++++++++++++++++++++--
 1 file changed, 33 insertions(+), 2 deletions(-)
drivers/watchdog/da9063_wdt.c +33 -2
index b17ac1bb1f28..06fcc662673e 100644
@@ -45,8 +45,40 @@ static unsigned int da9063_wdt_timeout_to_sel(unsigned int secs)
 	return DA9063_TWDSCALE_MAX;
 }
 
+/*
+ * Writing a '1' to the self-clearing WATCHDOG bit resets the watchdog counter
+ * value.
+ */
+static int _da9063_wdt_reset_timer(struct da9063 *da9063)
+{
+	return regmap_write(da9063->regmap, DA9063_REG_CONTROL_F,
+			    DA9063_WATCHDOG);
+}
+
 static int _da9063_wdt_set_timeout(struct da9063 *da9063, unsigned int regval)
 {
+	int ret;
+
+	/*
+	 * The watchdog triggers a reboot if a timeout value is already
+	 * programmed. The reason for this is that the timeout value
+	 * combines two functions in one: indicating the counter limit and
+	 * starting the watchdog. The watchdog must be disabled to be able to
+	 * change the timeout value if the watchdog is already running. Then
+	 * we can set the new timeout value which enables the watchdog again.
+	 */
+	ret = regmap_update_bits(da9063->regmap, DA9063_REG_CONTROL_D,
+				 DA9063_TWDSCALE_MASK, DA9063_TWDSCALE_DISABLE);
+	if (ret)
+		dev_warn(da9063->dev,
+			 "Failed to disable watchdog before setting new timeout\n");
+
+	usleep_range(150, 300);
+
+	ret = _da9063_wdt_reset_timer(da9063);
+	if (ret)
+		dev_warn(da9063->dev, "Failed to reset watchdog counter\n");
+
 	return regmap_update_bits(da9063->regmap, DA9063_REG_CONTROL_D,
 				  DA9063_TWDSCALE_MASK, regval);
 }
@@ -85,8 +117,7 @@ static int da9063_wdt_ping(struct watchdog_device *wdd)
 	struct da9063 *da9063 = watchdog_get_drvdata(wdd);
 	int ret;
 
-	ret = regmap_write(da9063->regmap, DA9063_REG_CONTROL_F,
-			   DA9063_WATCHDOG);
+	ret = _da9063_wdt_reset_timer(da9063);
 	if (ret)
 		dev_alert(da9063->dev, "Failed to ping the watchdog (err = %d)\n",
 			  ret);
Signature · 1 line
2.17.0
Acked-by on v3 1/1
#1 lore raw

Hi Marco,
Thanks.

Regards,
Stephen

Quoted 82 lines · Marco Felsch

Subject: [PATCH v3] watchdog: da9063: Fix setting/changing timeout

If the timeout value is set more than once the DA9063 watchdog triggers a reset signal which reset the system.

To update the timeout value we have to disable the watchdog, clear the watchdog counter value and write the new timeout value to the watchdog. Clearing the counter value is a feature to be on the safe side because the data sheet doesn't describe the behaviour of the watchdog counter value after a watchdog disabling-enable-sequence.

The patch is based on Philipp Zabel's previous patch but doesn't wait 150us because the DA9063 doesn't need this delay.

Fixes 5e9c16e37608 ("watchdog: Add DA9063 PMIC watchdog driver.")
---
 drivers/watchdog/da9063_wdt.c | 35
+++++++++++++++++++++++++++++++++--
 1 file changed, 33 insertions(+), 2 deletions(-)
drivers/watchdog/da9063_wdt.c +32 -0
index b17ac1bb1f28..06fcc662673e 100644
@@ -45,8 +45,40 @@ static unsigned int
 da9063_wdt_timeout_to_sel(unsigned int secs)
 	return DA9063_TWDSCALE_MAX;
 }
 
+/*
+ * Writing a '1' to the self-clearing WATCHDOG bit resets the watchdog counter
+ * value.
+ */
+static int _da9063_wdt_reset_timer(struct da9063 *da9063)
+{
+	return regmap_write(da9063->regmap, DA9063_REG_CONTROL_F,
+			    DA9063_WATCHDOG);
+}
+
 static int _da9063_wdt_set_timeout(struct da9063 *da9063, unsigned int regval)
 {
+	int ret;
+
+	/*
+	 * The watchdog triggers a reboot if a timeout value is already
+	 * programmed. The reason for this is that the timeout value
+	 * combines two functions in one: indicating the counter limit and
+	 * starting the watchdog. The watchdog must be disabled to be able to
+	 * change the timeout value if the watchdog is already running. Then
+	 * we can set the new timeout value which enables the watchdog again.
+	 */
+	ret = regmap_update_bits(da9063->regmap, DA9063_REG_CONTROL_D,
+				 DA9063_TWDSCALE_MASK, DA9063_TWDSCALE_DISABLE);
+	if (ret)
+		dev_warn(da9063->dev,
+			 "Failed to disable watchdog before setting new timeout\n");
+
+	usleep_range(150, 300);
+
+	ret = _da9063_wdt_reset_timer(da9063);
+	if (ret)
+		dev_warn(da9063->dev, "Failed to reset watchdog counter\n");
+
 	return regmap_update_bits(da9063->regmap, DA9063_REG_CONTROL_D,
 				  DA9063_TWDSCALE_MASK, regval);

}

drivers/watchdog/da9063_wdt.c +1 -2
@@ -85,8 +117,7 @@ static int da9063_wdt_ping(struct watchdog_device
 *wdd)
 	struct da9063 *da9063 = watchdog_get_drvdata(wdd);
 	int ret;
 
-	ret = regmap_write(da9063->regmap, DA9063_REG_CONTROL_F,
-			   DA9063_WATCHDOG);
+	ret = _da9063_wdt_reset_timer(da9063);
 	if (ret)
 		dev_alert(da9063->dev, "Failed to ping the watchdog (err =

%d)\n",
ret);
--
2.17.0

Reviewed-by on v3 1/1
#2 lore raw
In reply to Marco Felsch
Quoted 11 lines · Marco Felsch

If the timeout value is set more than once the DA9063 watchdog triggers a reset signal which reset the system.

To update the timeout value we have to disable the watchdog, clear the watchdog counter value and write the new timeout value to the watchdog. Clearing the counter value is a feature to be on the safe side because the data sheet doesn't describe the behaviour of the watchdog counter value after a watchdog disabling-enable-sequence.

The patch is based on Philipp Zabel's previous patch but doesn't wait 150us because the DA9063 doesn't need this delay.

Actually, the delay is back now.

Otherwise,

regards
Philipp

#3 lore raw
Quoted 10 lines · Philipp Zabel
Quoted 9 lines · Marco Felsch

If the timeout value is set more than once the DA9063 watchdog triggers a reset signal which reset the system.

To update the timeout value we have to disable the watchdog, clear the watchdog counter value and write the new timeout value to the watchdog. Clearing the counter value is a feature to be on the safe side because the data sheet doesn't describe the behaviour of the watchdog counter value after a watchdog disabling-enable-sequence.

Quoted 5 lines
Quoted 2 lines

The patch is based on Philipp Zabel's previous patch but doesn't wait 150us because the DA9063 doesn't need this delay.

Actually, the delay is back now.

Yes, my mistake.
I make a v4 to finally fix this.

Quoted 8 lines

Otherwise,

regards
Philipp

Regards,
Marco