Skip to contentSkip to conversation

watchdog: da9063: Fix timeout handling

PATCH Superseded Marco Felsch 2 participants 10 messages May 24, 2018

Subsystems (2)

v7 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

v7 cover watchdog: da9063: Fix timeout handling

The current set_timeout() handling can't change the timeout value if the watchdog is already running because the watchdog must be disabled before a new timeout value can be set. This sounds a bit strange but it is the only way to change the timeout value.

The watchdog also has a second quirk. The timeout value combines the timeout and the watchdog enable signal. So a value not equal zero indicates that the watchdog is running. This leads into problems if the watchdog is disabled and a new timeout value should be set because during setting the timeout value the watchdog gets enabled. The driver must check if the watchdog is disabled. In that case the timeout value must be stored to be available for the later wdt_start() call. If the watchdog is already running the driver must write the new timeout value immediately to the watchdog.

There was also a issue during the driver probe sequence. The driver must set a new timeout reference mark if the watchdog was enabled in a earlier stage e.g. the bootloader.

Marco Felsch (5):
watchdog: da9063: Fix setting/changing timeout
watchdog: da9063: Fix setting HW_RUNNING indication during wdt_start
watchdog: da9063: Fix updating timeout value
watchdog: da9063: Fix timeout handling during probe
watchdog: da9063: rename helper function to avoid misunderstandings

 drivers/watchdog/da9063_wdt.c | 69 ++++++++++++++++++++++++++++++++---
 1 file changed, 63 insertions(+), 6 deletions(-)
Signature · 1 line
2.17.0
#1 lore raw

PATCH v7 1/5 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.

Fixes 5e9c16e37608 ("watchdog: Add DA9063 PMIC watchdog driver.")
---
 drivers/watchdog/da9063_wdt.c | 26 ++++++++++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)
drivers/watchdog/da9063_wdt.c +24 -2
index b17ac1bb1f28..c1216e61e64e 100644
@@ -45,8 +45,31 @@ static unsigned int da9063_wdt_timeout_to_sel(unsigned int secs)
 	return DA9063_TWDSCALE_MAX;
 }
 
+static int da9063_wdt_disable_timer(struct da9063 *da9063)
+{
+	return regmap_update_bits(da9063->regmap, DA9063_REG_CONTROL_D,
+				  DA9063_TWDSCALE_MASK,
+				  DA9063_TWDSCALE_DISABLE);
+}
+
 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 because 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 = da9063_wdt_disable_timer(da9063);
+	if (ret)
+		return ret;
+
+	usleep_range(150, 300);
+
 	return regmap_update_bits(da9063->regmap, DA9063_REG_CONTROL_D,
 				  DA9063_TWDSCALE_MASK, regval);
 }
@@ -71,8 +94,7 @@ static int da9063_wdt_stop(struct watchdog_device *wdd)
 	struct da9063 *da9063 = watchdog_get_drvdata(wdd);
 	int ret;
 
-	ret = regmap_update_bits(da9063->regmap, DA9063_REG_CONTROL_D,
-				 DA9063_TWDSCALE_MASK, DA9063_TWDSCALE_DISABLE);
+	ret = da9063_wdt_disable_timer(da9063);
 	if (ret)
 		dev_alert(da9063->dev, "Watchdog failed to stop (err = %d)\n",
 			  ret);
Signature · 1 line
2.17.0
#2 lore raw

PATCH v7 2/5 watchdog: da9063: Fix setting HW_RUNNING indication during wdt_start

In reply to Marco Felsch

Inform the watchdog framework that the watchog is running. Each driver have to set this bit because the watchdog core framework only clears this bit.

Fixes 5e9c16e37608 ("watchdog: Add DA9063 PMIC watchdog driver.")
---
 drivers/watchdog/da9063_wdt.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/watchdog/da9063_wdt.c +2 -0
index c1216e61e64e..1b0ccadbc248 100644
@@ -86,6 +86,8 @@ static int da9063_wdt_start(struct watchdog_device *wdd)
 		dev_err(da9063->dev, "Watchdog failed to start (err = %d)\n",
 			ret);
 
+	set_bit(WDOG_HW_RUNNING, &wdd->status);
+
 	return ret;
 }
 
Signature · 1 line
2.17.0
maintainer
#3 lore raw

Re: [PATCH v7 2/5] watchdog: da9063: Fix setting HW_RUNNING indication during wdt_start

Quoted 4 lines · Marco Felsch

Inform the watchdog framework that the watchog is running. Each driver have to set this bit because the watchdog core framework only clears this bit.

This should only be necessary if the stop function does not stop the watchdog. Guess you need it because you call watchdog_hw_running() instead of watchdog_active() in the next patch, but the context, description, and use is not correct.

Guenter

Quoted 20 lines
Fixes 5e9c16e37608 ("watchdog: Add DA9063 PMIC watchdog driver.")
---
  drivers/watchdog/da9063_wdt.c | 2 ++
  1 file changed, 2 insertions(+)
drivers/watchdog/da9063_wdt.c +2 -0
index c1216e61e64e..1b0ccadbc248 100644
@@ -86,6 +86,8 @@ static int da9063_wdt_start(struct watchdog_device *wdd)
  		dev_err(da9063->dev, "Watchdog failed to start (err = %d)\n",
  			ret);
 
+	set_bit(WDOG_HW_RUNNING, &wdd->status);
+
  	return ret;
  }
 
#4 lore raw

PATCH v7 3/5 watchdog: da9063: Fix updating timeout value

In reply to Marco Felsch

The DA9063 watchdog has only one register field to store the timeout value and to enable the watchdog. The watchdog gets enabled if the value is not zero. There is no issue if the watchdog is already running but it leads into problems if the watchdog is disabled.

If the watchdog is diabled and only the timeout value should be prepared the watchdog gets enabled too. Add a check to get the current watchdog state and update the watchdog timeout value on hw-side only if the watchdog is already running.

Fixes 5e9c16e37608 ("watchdog: Add DA9063 PMIC watchdog driver.")
---
 drivers/watchdog/da9063_wdt.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
drivers/watchdog/da9063_wdt.c +15 -2
index 1b0ccadbc248..54c526436304 100644
@@ -123,10 +123,23 @@ static int da9063_wdt_set_timeout(struct watchdog_device *wdd,
 {
 	struct da9063 *da9063 = watchdog_get_drvdata(wdd);
 	unsigned int selector;
-	int ret;
+	int ret = 0;
 
 	selector = da9063_wdt_timeout_to_sel(timeout);
-	ret = _da9063_wdt_set_timeout(da9063, selector);
+
+	/*
+	 * There are two cases when a set_timeout() will be called:
+	 * 1. The watchdog is off and someone wants to set the timeout for the
+	 *    further use.
+	 * 2. The watchdog is already running and a new timeout value should be
+	 *    set.
+	 *
+	 * The watchdog can't store a timeout value not equal zero without
+	 * enabling the watchdog, so the timeout must be buffered by the driver.
+	 */
+	if (watchdog_hw_running(wdd))
+		ret = _da9063_wdt_set_timeout(da9063, selector);
+
 	if (ret)
 		dev_err(da9063->dev, "Failed to set watchdog timeout (err = %d)\n",
 			ret);
Signature · 1 line
2.17.0
maintainer
#5 lore raw

Re: [PATCH v7 3/5] watchdog: da9063: Fix updating timeout value

Quoted 6 lines · Marco Felsch

The DA9063 watchdog has only one register field to store the timeout value and to enable the watchdog. The watchdog gets enabled if the value is not zero. There is no issue if the watchdog is already running but it leads into problems if the watchdog is disabled.

If the watchdog is diabled and only the timeout value should be prepared

disabled

Quoted 25 lines

the watchdog gets enabled too. Add a check to get the current watchdog state and update the watchdog timeout value on hw-side only if the watchdog is already running.

Fixes 5e9c16e37608 ("watchdog: Add DA9063 PMIC watchdog driver.")
---
  drivers/watchdog/da9063_wdt.c | 17 +++++++++++++++--
  1 file changed, 15 insertions(+), 2 deletions(-)
drivers/watchdog/da9063_wdt.c +3 -2
index 1b0ccadbc248..54c526436304 100644
@@ -123,10 +123,23 @@ static int da9063_wdt_set_timeout(struct watchdog_device *wdd,
  {
  	struct da9063 *da9063 = watchdog_get_drvdata(wdd);
  	unsigned int selector;
-	int ret;
+	int ret = 0;
 
  	selector = da9063_wdt_timeout_to_sel(timeout);
-	ret = _da9063_wdt_set_timeout(da9063, selector);
+
+	/*

drivers/watchdog/da9063_wdt.c static int da9063_wdt_set_timeout(struct watchdog_device *wdd, · line 140

Quoted 10 lines
drivers/watchdog/da9063_wdt.c +10 -0
+	 * There are two cases when a set_timeout() will be called:
+	 * 1. The watchdog is off and someone wants to set the timeout for the
+	 *    further use.
+	 * 2. The watchdog is already running and a new timeout value should be
+	 *    set.
+	 *
+	 * The watchdog can't store a timeout value not equal zero without
+	 * enabling the watchdog, so the timeout must be buffered by the driver.
+	 */
+	if (watchdog_hw_running(wdd))

Normally one would call watchdog_active() here.

Guenter

Quoted 6 lines
drivers/watchdog/da9063_wdt.c +2 -0
+		ret = _da9063_wdt_set_timeout(da9063, selector);
+
  	if (ret)
  		dev_err(da9063->dev, "Failed to set watchdog timeout (err = %d)\n",
  			ret);
#6 lore raw

PATCH v7 4/5 watchdog: da9063: Fix timeout handling during probe

In reply to Marco Felsch

The watchdog can be enabled in previous steps (e.g. the bootloader). Check if the watchdog is already running, retrieve the set timeout value and set it again to set the new timeout reference mark.

Fixes 5e9c16e37608 ("watchdog: Add DA9063 PMIC watchdog driver.")
---
 drivers/watchdog/da9063_wdt.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
drivers/watchdog/da9063_wdt.c +20 -0
index 54c526436304..9056fa945c01 100644
@@ -45,6 +45,19 @@ static unsigned int da9063_wdt_timeout_to_sel(unsigned int secs)
 	return DA9063_TWDSCALE_MAX;
 }
 
+/*
+ * Return 0 if watchdog is disabled, else non zero.
+ */
+static int da9063_wdt_is_running(struct da9063 *da9063)
+{
+	unsigned int val;
+
+	regmap_read(da9063->regmap, DA9063_REG_CONTROL_D, &val);
+	val &= DA9063_TWDSCALE_MASK;
+
+	return wdt_timeout[val];
+}
+
 static int da9063_wdt_disable_timer(struct da9063 *da9063)
 {
 	return regmap_update_bits(da9063->regmap, DA9063_REG_CONTROL_D,
@@ -182,6 +195,7 @@ static int da9063_wdt_probe(struct platform_device *pdev)
 {
 	struct da9063 *da9063;
 	struct watchdog_device *wdd;
+	unsigned int cur_timeout;
 
 	if (!pdev->dev.parent)
 		return -EINVAL;
@@ -208,6 +222,12 @@ static int da9063_wdt_probe(struct platform_device *pdev)
 
 	watchdog_set_drvdata(wdd, da9063);
 
+	cur_timeout = da9063_wdt_is_running(da9063);
+	if (cur_timeout) {
+		set_bit(WDOG_HW_RUNNING, &wdd->status);
+		da9063_wdt_set_timeout(wdd, cur_timeout);
+	}
+
 	return devm_watchdog_register_device(&pdev->dev, wdd);
 }
 
Signature · 1 line
2.17.0
maintainer
#7 lore raw

Re: [PATCH v7 4/5] watchdog: da9063: Fix timeout handling during probe

Quoted 43 lines · Marco Felsch

The watchdog can be enabled in previous steps (e.g. the bootloader). Check if the watchdog is already running, retrieve the set timeout value and set it again to set the new timeout reference mark.

Fixes 5e9c16e37608 ("watchdog: Add DA9063 PMIC watchdog driver.")
---
  drivers/watchdog/da9063_wdt.c | 20 ++++++++++++++++++++
  1 file changed, 20 insertions(+)
drivers/watchdog/da9063_wdt.c +14 -0
index 54c526436304..9056fa945c01 100644
@@ -45,6 +45,19 @@ static unsigned int da9063_wdt_timeout_to_sel(unsigned int secs)
  	return DA9063_TWDSCALE_MAX;
  }
 
+/*
+ * Return 0 if watchdog is disabled, else non zero.
+ */
+static int da9063_wdt_is_running(struct da9063 *da9063)
+{
+	unsigned int val;
+
+	regmap_read(da9063->regmap, DA9063_REG_CONTROL_D, &val);
+	val &= DA9063_TWDSCALE_MASK;
+
+	return wdt_timeout[val];
+}
+
  static int da9063_wdt_disable_timer(struct da9063 *da9063)
  {
  	return regmap_update_bits(da9063->regmap, DA9063_REG_CONTROL_D,
@@ -182,6 +195,7 @@ static int da9063_wdt_probe(struct platform_device *pdev)
  {
  	struct da9063 *da9063;
  	struct watchdog_device *wdd;
+	unsigned int cur_timeout;
 
  	if (!pdev->dev.parent)
  		return -EINVAL;

drivers/watchdog/da9063_wdt.c static int da9063_wdt_probe(struct platform_device *pdev) · line 228

Quoted 7 lines
drivers/watchdog/da9063_wdt.c +4 -0
@@ -208,6 +222,12 @@ static int da9063_wdt_probe(struct platform_device *pdev)
 
  	watchdog_set_drvdata(wdd, da9063);
 
+	cur_timeout = da9063_wdt_is_running(da9063);
+	if (cur_timeout) {
+		set_bit(WDOG_HW_RUNNING, &wdd->status);
+		da9063_wdt_set_timeout(wdd, cur_timeout);

You could call _da9063_wdt_set_timeout() here.

Guenter

Quoted 6 lines
drivers/watchdog/da9063_wdt.c +2 -0
+	}
+
  	return devm_watchdog_register_device(&pdev->dev, wdd);
  }
 
#8 lore raw

PATCH v7 5/5 watchdog: da9063: rename helper function to avoid misunderstandings

In reply to Marco Felsch

_da9063_wdt_set_timeout() is called from da9063_wdg_set_timeout() and from da9063_wdg_start() but the name expect to be called only from da9063_wdg_set_timeout(). Rename the function to avoid misunderstandings.

---
 drivers/watchdog/da9063_wdt.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/watchdog/da9063_wdt.c +3 -3
index 9056fa945c01..5a2a8ef39491 100644
@@ -65,7 +65,7 @@ static int da9063_wdt_disable_timer(struct da9063 *da9063)
 				  DA9063_TWDSCALE_DISABLE);
 }
 
-static int _da9063_wdt_set_timeout(struct da9063 *da9063, unsigned int regval)
+static int da9063_wdt_update_timeout(struct da9063 *da9063, unsigned int regval)
 {
 	int ret;
 
@@ -94,7 +94,7 @@ static int da9063_wdt_start(struct watchdog_device *wdd)
 	int ret;
 
 	selector = da9063_wdt_timeout_to_sel(wdd->timeout);
-	ret = _da9063_wdt_set_timeout(da9063, selector);
+	ret = da9063_wdt_update_timeout(da9063, selector);
 	if (ret)
 		dev_err(da9063->dev, "Watchdog failed to start (err = %d)\n",
 			ret);
@@ -151,7 +151,7 @@ static int da9063_wdt_set_timeout(struct watchdog_device *wdd,
 	 * enabling the watchdog, so the timeout must be buffered by the driver.
 	 */
 	if (watchdog_hw_running(wdd))
-		ret = _da9063_wdt_set_timeout(da9063, selector);
+		ret = da9063_wdt_update_timeout(da9063, selector);
 
 	if (ret)
 		dev_err(da9063->dev, "Failed to set watchdog timeout (err = %d)\n",
Signature · 1 line
2.17.0
maintainer
#9 lore raw
In reply to Marco Felsch
Quoted 26 lines · Marco Felsch

The current set_timeout() handling can't change the timeout value if the watchdog is already running because the watchdog must be disabled before a new timeout value can be set. This sounds a bit strange but it is the only way to change the timeout value.

The watchdog also has a second quirk. The timeout value combines the timeout and the watchdog enable signal. So a value not equal zero indicates that the watchdog is running. This leads into problems if the watchdog is disabled and a new timeout value should be set because during setting the timeout value the watchdog gets enabled. The driver must check if the watchdog is disabled. In that case the timeout value must be stored to be available for the later wdt_start() call. If the watchdog is already running the driver must write the new timeout value immediately to the watchdog.

There was also a issue during the driver probe sequence. The driver must set a new timeout reference mark if the watchdog was enabled in a earlier stage e.g. the bootloader.

Marco Felsch (5):
watchdog: da9063: Fix setting/changing timeout
watchdog: da9063: Fix setting HW_RUNNING indication during wdt_start
watchdog: da9063: Fix updating timeout value
watchdog: da9063: Fix timeout handling during probe
watchdog: da9063: rename helper function to avoid misunderstandings

Quoted 3 lines
  drivers/watchdog/da9063_wdt.c | 69 ++++++++++++++++++++++++++++++++---
  1 file changed, 63 insertions(+), 6 deletions(-)

I am really missing the change log.

Guenter