Skip to contentSkip to conversation

watchdog: add get_timeout operation

PATCH In review Juuso Rinta 2 participants 28 messages Nov 6 – Nov 7 Watchdog device drivers

lore
Share & tools
lore
#0 lore raw

v2 cover watchdog: add get_timeout operation

Add an optional get_timeout operation and a wdt_get_timeout() helper to the watchdog uclass so drivers can report the timeout actually programmed in hardware. The requested value passed to wdt_start() can differ after clamping, discrete steps, or rounding (for example power-of-two periods).

After a successful ops->start(), the uclass calls the new operation via wdt_get_timeout() when the driver implements the operation and uses that value in the start-path print. The series also adds a warning when the timeout value is not longer than the servicing interval.

If wdt_get_timeout() is called while the watchdog is not running, drivers should return -ENODATA, so callers can tell that case apart from a real error. This is consistent with arm_smc_wdt, which maps a disabled watchdog to -ENODATA.

Add a 'wdt gettimeout' subcommand which exposes the same helper to the U-Boot shell. This command returns CMD_RET_SUCCESS when a timeout is available, and CMD_RET_FAILURE when the device watchdog is not running or the operation is not supported.

Implement get_timeout in designware_wdt, max6370_wdt, and sandbox_wdt. The designware and max6370 implementations have not been tested with hardware as I do not have access to those devices. Use a lookup table for the timeout selection in max6370 so that the start and get_timeout functions do not drift.

Add a timeout rounding up to whole second in sandbox_wdt driver so that the path where the requested, unclamped timeout differs from the programmed value when checked with wdt_get_timeout() is testable

Add unit tests for wdt_get_timeout(), the wdt gettimeout command and the servicing-interval warning.

This series is a follow-up to "v3 watchdog: report effective timeout"
[https://lore.kernel.org/u-boot/[email protected]/]

Patch summary:
- Patch 1: Add a get_timeout operation and wdt_get_timeout helper function for wdt-uclass. Update the wdt_start function to use this helper
- Patch 2: Add a warning print in wdt_start() if the programmed timeout reported via the get_timeout op is not longer than servicing interval
- Patch 3: Store the device sequence number instead of a pointer in cmd/wdt.c
- Patch 4: Add a gettimeout subcommand to the wdt command in cmd/wdt.c
- Patch 5: Implement the get_timeout operation for designware_wdt
- Patch 6: Use a lookup table for timeout selection in max6370_wdt.c
- Patch 7: Implement the get_timeout operation for max6370_wdt
- Patch 8: Implement the operation for sandbox_wdt, introduce timeout rounding for sandbox_wdt_start()
- Patch 9: Implement tests for wdt_get_timeout(), wdt gettimeout and the warning print

---
Changes in v2:
- Rename timeout_ms to timeout_msp in wdt_get_timeout() and the driver implementations
- drivers/watchdog/wdt-uclass.c: move wdt_get_timeout() in wdt_start()
before the comment block "If the requested timeout was clamped..."
- drivers/watchdog/wdt-uclass.c: issue warning print for timeout not
longer than servicing interval in wdt_start() on all devices instead
of just for those that support get_timeout operation
- cmd/wdt.c: store the currdev sequence number instead of a pointer
- doc/usage/cmd/wdt.rst: add a mention that the 'wdt gettimeout'
returns CMD_RET_FAILURE when the watchdog is not running or the driver
does not support the operation
- designware_wdt.c: replace hardcoded 0xF/15 value with a define, include
div64.h and use lldiv() in the designware_get_timeout() division
- max6370_wdt.c: add a lookup table for timeout selection, use it on both max6370_wdt_start() and max6370_wdt_get_timeout()
- sandbox_wdt.c: make the sandbox_wdt_start() round the requested timeout
up to a whole second so that the path where requested and unclamped
timeout differs from programmed timeout when checked with
wdt_get_timeout() is testable
- test/dm/wdt.c: move the 'wdt gettimeout' tests to their own file,
split the tests that exercise the wdt_get_timeout() function into
their own function, and the tests that test the warning print to
their own function
- test/cmd/wdt.c: test the 'wdt gettimeout' command with both the sandbox
and gpio devices (supported and unsupported path)
- Link to v1: https://patch.msgid.link/[email protected]

To: [email protected]
Cc: Stefan Roese <[email protected]>
Cc: Tom Rini <[email protected]>
Cc: Juuso Rinta <[email protected]>
Cc: Yuri Zaporozhets <[email protected]>
Cc: Jonathan GUILLOT <[email protected]>
Cc: Jonas Karlman <[email protected]>
Cc: Simon Glass <[email protected]>
Cc: "Maxin B. John" <[email protected]>
Cc: Rasmus Villemoes <[email protected]>

---
Juuso Rinta (9):
watchdog: wdt-uclass: add get_timeout operation and helper
watchdog: warn if timeout not longer than servicing interval
cmd: wdt: store the device sequence number instead of a pointer
cmd: wdt: add gettimeout command
watchdog: designware_wdt: add get_timeout operation
watchdog: max6370_wdt: use a lookup table for timeout selection
watchdog: max6370_wdt: add get_timeout operation
watchdog: sandbox_wdt: add get_timeout operation
test: wdt: add tests for get_timeout support

 cmd/wdt.c                         | 66 ++++++++++++++++++++++++-----------
 doc/usage/cmd/wdt.rst             | 12 ++++++-
 drivers/watchdog/designware_wdt.c | 23 +++++++++++--
 drivers/watchdog/max6370_wdt.c    | 49 ++++++++++++++++++--------
 drivers/watchdog/sandbox_wdt.c    | 17 ++++++++-
 drivers/watchdog/wdt-uclass.c     | 24 +++++++++++--
 include/wdt.h                     | 22 ++++++++++++
 test/cmd/Makefile                 |  3 ++
 test/cmd/wdt.c                    | 60 ++++++++++++++++++++++++++++++++
 test/dm/wdt.c                     | 72 ++++++++++++++++++++++++++++++++++++++-
 10 files changed, 307 insertions(+), 41 deletions(-)

---
base-commit: 709a97a05df66e394f40d249105d0baade46f107
change-id: 20260824-rinta-watchdog-gettimeout-f39e924f40c8

Best regards,

Signature · 1 line
Juuso Rinta <[email protected]>
#1 lore raw

PATCH v2 1/9 watchdog: wdt-uclass: add get_timeout operation and helper

Currently the wdt_start() function uses the plat data to check if a driver has reported a max_timeout_ms value and clamps the requested timeout to that value. This is not enough on devices where the effective value is not always static.

Add a uclass get_timeout operation and helper so drivers can report the effective timeout. The helper returns -ENOSYS if the driver does not implement the operation. If wdt_get_timeout() is called while the watchdog is not running, drivers should return -ENODATA. wdt_start() uses the helper to report the effective timeout when the driver supports the operation.

---
 drivers/watchdog/wdt-uclass.c | 19 ++++++++++++++++---
 include/wdt.h                 | 22 ++++++++++++++++++++++
 2 files changed, 38 insertions(+), 3 deletions(-)
drivers/watchdog/wdt-uclass.c +16 -3
index 88c5cbdf508..291f0187682 100644
@@ -116,6 +116,16 @@ int initr_watchdog(void)
 	return 0;
 }
 
+int wdt_get_timeout(struct udevice *dev, u64 *timeout_msp)
+{
+	const struct wdt_ops *ops = device_get_ops(dev);
+
+	if (!ops->get_timeout)
+		return -ENOSYS;
+
+	return ops->get_timeout(dev, timeout_msp);
+}
+
 int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
 {
 	const struct wdt_ops *ops = device_get_ops(dev);
@@ -153,10 +163,13 @@ int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
 
 		priv->running = true;
 
+		/* Check programmed timeout from driver if op supported */
+		if (wdt_get_timeout(dev, &timeout_ms) == 0)
+			tout_s = lldiv(timeout_ms, 1000);
 		/*
-		 * If the requested timeout was clamped, note the value
-		 * when it differs at whole-second resolution. Sub-second
-		 * rounding is ignored to avoid noise.
+		 * If the requested timeout was clamped or rounded, note
+		 * the value when it differs at whole-second resolution.
+		 * Sub-second rounding is ignored to avoid noise.
 		 */
 		req_str[0] = '\0';
 		if (req_s != tout_s)
include/wdt.h +22 -0
index db1faa321b3..dc31d2db606 100644
@@ -74,6 +74,19 @@ int wdt_reset(struct udevice *dev);
  */
 int wdt_expire_now(struct udevice *dev, ulong flags);
 
+/*
+ * Get the effective timeout in milliseconds.
+ *
+ * The effective value may differ from the value wdt_start() passed to
+ * the driver, for example if it has been rounded and/or clamped.
+ *
+ * @dev: WDT device
+ * @timeout_msp: Returns the effective timeout in ms; unchanged on error
+ * @return: 0 if OK, -ENOSYS if the driver does not support this,
+ * -ENODATA if the watchdog is not running, other -ve on error
+ */
+int wdt_get_timeout(struct udevice *dev, u64 *timeout_msp);
+
 /**
  * struct wdt_uc_plat - uclass platform data for a watchdog device
  *
@@ -131,6 +144,15 @@ struct wdt_ops {
 	 * @return 0 if OK -ve on error. May not return.
 	 */
 	int (*expire_now)(struct udevice *dev, ulong flags);
+	/*
+	 * Get the effective timeout in milliseconds (optional)
+	 *
+	 * @dev: WDT device
+	 * @timeout_msp: Timeout in milliseconds, written only on success
+	 * @return: 0 if OK, -ENODATA if the watchdog is not running,
+	 * other -ve on error
+	 */
+	int (*get_timeout)(struct udevice *dev, u64 *timeout_msp);
 };
 
 int initr_watchdog(void);
Signature · 1 line
2.39.2
Reviewed-by on v2 1/9
#2 lore raw

Re: [PATCH v2 1/9] watchdog: wdt-uclass: add get_timeout operation and helper

Quoted 5 lines · Juuso Rinta

Currently the wdt_start() function uses the plat data to check if a driver has reported a max_timeout_ms value and clamps the requested timeout to that value. This is not enough on devices where the effective value is not always static.

Quoted 8 lines

Add a uclass get_timeout operation and helper so drivers can report the effective timeout. The helper returns -ENOSYS if the driver does not implement the operation. If wdt_get_timeout() is called while the watchdog is not running, drivers should return -ENODATA. wdt_start() uses the helper to report the effective timeout when the driver supports the operation.

Thanks,
Stefan

#3 lore raw

Re: [PATCH v2 1/9] watchdog: wdt-uclass: add get_timeout operation and helper

In reply to Juuso Rinta
Quoted 5 lines · Juuso Rinta

Currently the wdt_start() function uses the plat data to check if a driver has reported a max_timeout_ms value and clamps the requested timeout to that value. This is not enough on devices where the effective value is not always static.

Quoted 8 lines

Add a uclass get_timeout operation and helper so drivers can report the effective timeout. The helper returns -ENOSYS if the driver does not implement the operation. If wdt_get_timeout() is called while the watchdog is not running, drivers should return -ENODATA. wdt_start() uses the helper to report the effective timeout when the driver supports the operation.

Applied to u-boot-watchdog/main

Thanks,
Stefan

#4 lore raw

PATCH v2 2/9 watchdog: warn if timeout not longer than servicing interval

In reply to Juuso Rinta

If watchdog timeout is not longer than servicing interval, the board resets unexpectedly because the watchdog expires before schedule() services it.

Print a warning in wdt_start() when the timeout is not longer than the servicing interval. If the driver implements get_timeout, the effective value is used, otherwise the requested value or the clamped value via max_timeout_ms.

Suggested on the series "watchdog: report effective timeout"
[https://lore.kernel.org/u-boot/[email protected]/]

---
 drivers/watchdog/wdt-uclass.c | 5 +++++
 1 file changed, 5 insertions(+)
drivers/watchdog/wdt-uclass.c +5 -0
index 291f0187682..bf6f2a4ff96 100644
@@ -178,6 +178,11 @@ int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)
 		printf("WDT:   Started %s with%s servicing %s (%ds timeout%s)\n",
 		       dev->name, IS_ENABLED(CONFIG_WATCHDOG) ? "" : "out",
 		       svc_str, tout_s, req_str);
+
+		/* Warn if timeout is not longer than servicing interval */
+		if (IS_ENABLED(CONFIG_WATCHDOG) && timeout_ms <= priv->reset_period)
+			printf("WDT:   Warning: timeout %llums is not longer than servicing interval %lums\n",
+			       timeout_ms, priv->reset_period);
 	}
 
 	return ret;
Signature · 1 line
2.39.2
Reviewed-by on v2 2/9
#5 lore raw

Re: [PATCH v2 2/9] watchdog: warn if timeout not longer than servicing interval

Quoted 7 lines · Juuso Rinta

If watchdog timeout is not longer than servicing interval, the board resets unexpectedly because the watchdog expires before schedule() services it.

Print a warning in wdt_start() when the timeout is not longer than the servicing interval. If the driver implements get_timeout, the effective value is used, otherwise the requested value or the clamped value via max_timeout_ms.

Quoted 5 lines

Suggested on the series "watchdog: report effective timeout"
[https://lore.kernel.org/u-boot/[email protected]/]

Thanks,
Stefan

#6 lore raw

Re: [PATCH v2 2/9] watchdog: warn if timeout not longer than servicing interval

In reply to Juuso Rinta
Quoted 7 lines · Juuso Rinta

If watchdog timeout is not longer than servicing interval, the board resets unexpectedly because the watchdog expires before schedule() services it.

Print a warning in wdt_start() when the timeout is not longer than the servicing interval. If the driver implements get_timeout, the effective value is used, otherwise the requested value or the clamped value via max_timeout_ms.

Quoted 5 lines

Suggested on the series "watchdog: report effective timeout"
[https://lore.kernel.org/u-boot/[email protected]/]

Applied to u-boot-watchdog/main

Thanks,
Stefan

#7 lore raw

PATCH v2 3/9 cmd: wdt: store the device sequence number instead of a pointer

In reply to Juuso Rinta

The wdt command keeps the selected device in a static struct udevice pointer. If the driver model is torn down and rebuilt, for example after running 'ut dm', the pointer refers to freed device and any later 'wdt' subcommand that is run without 'wdt dev' uses freed memory.

Store the device sequence number instead and look the device up with uclass_get_device_by_seq() each time a subcommand runs. If the device has gone away the lookup fails and the existing "use 'wdt dev' first" message is printed, with the selection reset, so no stale state is kept.

---
 cmd/wdt.c | 41 ++++++++++++++++++++++-------------------
 1 file changed, 22 insertions(+), 19 deletions(-)
cmd/wdt.c +22 -19
index c7a06cca181..b5f7bcdc35b 100644
@@ -9,7 +9,7 @@
 #include <dm.h>
 #include <wdt.h>
 
-static struct udevice *currdev;
+static int currdev_seq = -1;
 
 static int do_wdt_list(struct cmd_tbl *cmdtp, int flag, int argc,
 		       char *const argv[])
@@ -31,16 +31,18 @@ static int do_wdt_list(struct cmd_tbl *cmdtp, int flag, int argc,
 static int do_wdt_dev(struct cmd_tbl *cmdtp, int flag, int argc,
 		      char *const argv[])
 {
-	int ret;
+	struct udevice *currdev;
 
 	if (argc > 1) {
-		ret = uclass_get_device_by_name(UCLASS_WDT, argv[1], &currdev);
-		if (ret) {
+		if (uclass_get_device_by_name(UCLASS_WDT, argv[1], &currdev)) {
+			currdev_seq = -1;
 			printf("Can't get the watchdog timer: %s\n", argv[1]);
 			return CMD_RET_FAILURE;
 		}
+		currdev_seq = dev_seq(currdev);
 	} else {
-		if (!currdev) {
+		if (uclass_get_device_by_seq(UCLASS_WDT, currdev_seq, &currdev)) {
+			currdev_seq = -1;
 			printf("No watchdog timer device set!\n");
 			return CMD_RET_FAILURE;
 		}
@@ -50,9 +52,10 @@ static int do_wdt_dev(struct cmd_tbl *cmdtp, int flag, int argc,
 	return CMD_RET_SUCCESS;
 }
 
-static int check_currdev(void)
+static int check_currdev(struct udevice **devp)
 {
-	if (!currdev) {
+	if (uclass_get_device_by_seq(UCLASS_WDT, currdev_seq, devp)) {
+		currdev_seq = -1;
 		printf("No device set, use 'wdt dev' first\n");
 		return CMD_RET_FAILURE;
 	}
@@ -65,13 +68,13 @@ static int do_wdt_start(struct cmd_tbl *cmdtp, int flag, int argc,
 	int ret;
 	u64 timeout;
 	ulong flags = 0;
+	struct udevice *currdev;
 
 	if (argc < 2)
 		return CMD_RET_USAGE;
 
-	ret = check_currdev();
-	if (ret)
-		return ret;
+	if (check_currdev(&currdev))
+		return CMD_RET_FAILURE;
 
 	timeout = simple_strtoull(argv[1], NULL, 0);
 	if (argc > 2)
@@ -93,10 +96,10 @@ static int do_wdt_stop(struct cmd_tbl *cmdtp, int flag, int argc,
 		       char *const argv[])
 {
 	int ret;
+	struct udevice *currdev;
 
-	ret = check_currdev();
-	if (ret)
-		return ret;
+	if (check_currdev(&currdev))
+		return CMD_RET_FAILURE;
 
 	ret = wdt_stop(currdev);
 	if (ret == -ENOSYS) {
@@ -114,10 +117,10 @@ static int do_wdt_reset(struct cmd_tbl *cmdtp, int flag, int argc,
 			char *const argv[])
 {
 	int ret;
+	struct udevice *currdev;
 
-	ret = check_currdev();
-	if (ret)
-		return ret;
+	if (check_currdev(&currdev))
+		return CMD_RET_FAILURE;
 
 	ret = wdt_reset(currdev);
 	if (ret == -ENOSYS) {
@@ -136,10 +139,10 @@ static int do_wdt_expire(struct cmd_tbl *cmdtp, int flag, int argc,
 {
 	int ret;
 	ulong flags = 0;
+	struct udevice *currdev;
 
-	ret = check_currdev();
-	if (ret)
-		return ret;
+	if (check_currdev(&currdev))
+		return CMD_RET_FAILURE;
 
 	if (argc > 1)
 		flags = simple_strtoul(argv[1], NULL, 0);
Signature · 1 line
2.39.2
Reviewed-by on v2 3/9
#8 lore raw

Re: [PATCH v2 3/9] cmd: wdt: store the device sequence number instead of a pointer

Quoted 5 lines · Juuso Rinta

The wdt command keeps the selected device in a static struct udevice pointer. If the driver model is torn down and rebuilt, for example after running 'ut dm', the pointer refers to freed device and any later 'wdt' subcommand that is run without 'wdt dev' uses freed memory.

Quoted 8 lines

Store the device sequence number instead and look the device up with uclass_get_device_by_seq() each time a subcommand runs. If the device has gone away the lookup fails and the existing "use 'wdt dev' first" message is printed, with the selection reset, so no stale state is kept.

Thanks,
Stefan

#9 lore raw

Re: [PATCH v2 3/9] cmd: wdt: store the device sequence number instead of a pointer

In reply to Juuso Rinta
Quoted 5 lines · Juuso Rinta

The wdt command keeps the selected device in a static struct udevice pointer. If the driver model is torn down and rebuilt, for example after running 'ut dm', the pointer refers to freed device and any later 'wdt' subcommand that is run without 'wdt dev' uses freed memory.

Quoted 8 lines

Store the device sequence number instead and look the device up with uclass_get_device_by_seq() each time a subcommand runs. If the device has gone away the lookup fails and the existing "use 'wdt dev' first" message is printed, with the selection reset, so no stale state is kept.

Applied to u-boot-watchdog/main

Thanks,
Stefan

#10 lore raw

PATCH v2 4/9 cmd: wdt: add gettimeout command

In reply to Juuso Rinta

Add support for the subcommand wdt gettimeout in the U-Boot shell. The command exposes the wdt_get_timeout() helper to the shell interface.

Command reports 'not supported' (-ENOSYS) and 'not running' (-ENODATA) distinctly from other errors.

Add the command to the documentation.

---
 cmd/wdt.c             | 25 +++++++++++++++++++++++++
 doc/usage/cmd/wdt.rst | 12 +++++++++++-
 2 files changed, 36 insertions(+), 1 deletion(-)
cmd/wdt.c +25 -0
index b5f7bcdc35b..a5a5d678542 100644
@@ -134,6 +134,29 @@ static int do_wdt_reset(struct cmd_tbl *cmdtp, int flag, int argc,
 	return CMD_RET_SUCCESS;
 }
 
+static int do_wdt_gettimeout(struct cmd_tbl *cmdtp, int flag, int argc,
+			     char *const argv[])
+{
+	int ret;
+	u64 timeout_ms;
+	struct udevice *currdev;
+
+	if (check_currdev(&currdev))
+		return CMD_RET_FAILURE;
+
+	ret = wdt_get_timeout(currdev, &timeout_ms);
+	if (ret == -ENOSYS)
+		printf("Getting watchdog timeout not supported.\n");
+	else if (ret == -ENODATA)
+		printf("Watchdog not running.\n");
+	else if (ret)
+		printf("Getting watchdog timeout failed (%d)\n", ret);
+	else
+		printf("Timeout: %llums\n", timeout_ms);
+
+	return ret ? CMD_RET_FAILURE : CMD_RET_SUCCESS;
+}
+
 static int do_wdt_expire(struct cmd_tbl *cmdtp, int flag, int argc,
 			 char *const argv[])
 {
@@ -165,6 +188,7 @@ U_BOOT_LONGHELP(wdt,
 	"wdt start <timeout ms> [flags] - start watchdog timer\n"
 	"wdt stop - stop watchdog timer\n"
 	"wdt reset - reset watchdog timer\n"
+	"wdt gettimeout - get device timeout in ms\n"
 	"wdt expire [flags] - expire watchdog timer immediately\n");
 
 U_BOOT_CMD_WITH_SUBCMDS(wdt, "Watchdog sub-system", wdt_help_text,
@@ -173,4 +197,5 @@ U_BOOT_CMD_WITH_SUBCMDS(wdt, "Watchdog sub-system", wdt_help_text,
 	U_BOOT_SUBCMD_MKENT(start, 3, 1, do_wdt_start),
 	U_BOOT_SUBCMD_MKENT(stop, 1, 1, do_wdt_stop),
 	U_BOOT_SUBCMD_MKENT(reset, 1, 1, do_wdt_reset),
+	U_BOOT_SUBCMD_MKENT(gettimeout, 1, 1, do_wdt_gettimeout),
 	U_BOOT_SUBCMD_MKENT(expire, 2, 1, do_wdt_expire));
doc/usage/cmd/wdt.rst +11 -1
index 28abf98def9..bb0b402aa2f 100644
@@ -16,6 +16,7 @@ Synopsis
     wdt start <timeout_ms> [flags]
     wdt stop
     wdt reset
+    wdt gettimeout
     wdt expire [flags]
 
 Description
@@ -34,6 +35,11 @@ The 'wdt stop' command stops the current watchdog timer.
 
 The 'wdt reset' command resets the current watchdog timer without stopping it.
 
+The 'wdt gettimeout' command reports current watchdog device's programmed
+timeout value in milliseconds when supported by the driver. The command returns
+`CMD_RET_FAILURE` if the watchdog is not running or the driver does not
+support it.
+
 The 'wdt expire' command let's the current watchdog timer expire immediately.
 This will lead to a reset.
 
@@ -60,14 +66,18 @@ Example
     => wdt dev
     dev: watchdog@1c20ca0
     => wdt start 3000
+    => wdt gettimeout
+    Timeout: 3000ms
     => wdt reset
     => wdt stop
+    => wdt gettimeout
+    Watchdog not running.
     => wdt expire
 
     U-Boot SPL 2022.04-rc3 (Mar 25 2022 - 13:48:33 +0000)
 
  In the example above '(sunxi_wdt)' refers to the driver for the watchdog
- device.
+ device. The gettimeout example assumes the driver supports the operation.
 
 Configuration
 -------------
Signature · 1 line
2.39.2
Reviewed-by on v2 4/9
#11 lore raw

Re: [PATCH v2 4/9] cmd: wdt: add gettimeout command

Quoted 9 lines · Juuso Rinta

Add support for the subcommand wdt gettimeout in the U-Boot shell. The command exposes the wdt_get_timeout() helper to the shell interface.

Command reports 'not supported' (-ENOSYS) and 'not running' (-ENODATA) distinctly from other errors.

Add the command to the documentation.

Thanks,
Stefan

#12 lore raw

Re: [PATCH v2 4/9] cmd: wdt: add gettimeout command

In reply to Juuso Rinta
Quoted 9 lines · Juuso Rinta

Add support for the subcommand wdt gettimeout in the U-Boot shell. The command exposes the wdt_get_timeout() helper to the shell interface.

Command reports 'not supported' (-ENOSYS) and 'not running' (-ENODATA) distinctly from other errors.

Add the command to the documentation.

Applied to u-boot-watchdog/main

Thanks,
Stefan

#13 lore raw

PATCH v2 5/9 watchdog: designware_wdt: add get_timeout operation

In reply to Juuso Rinta

Implement the get_timeout operation for the designware watchdog driver.

Add defines for the timeout shift (16) and for the mask (15) used by both the getter and the setter.

---
 drivers/watchdog/designware_wdt.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
drivers/watchdog/designware_wdt.c +21 -2
index 91228de5e8e..0d7f69002ac 100644
@@ -4,6 +4,7 @@
  */
 
 #include <clk.h>
+#include <div64.h>
 #include <dm.h>
 #include <reset.h>
 #include <wdt.h>
@@ -18,6 +19,9 @@
 #define DW_WDT_CR_RMOD_OFFSET	0x01
 #define DW_WDT_CRR_RESTART_VAL	0x76
 
+#define DW_WDT_TIMEOUT_SHIFT	16
+#define DW_WDT_TORR_TOP_MASK	GENMASK(3, 0)
+
 struct designware_wdt_priv {
 	void __iomem	*base;
 	unsigned int	clk_khz;
@@ -34,8 +38,8 @@ static int designware_wdt_settimeout(void __iomem *base, unsigned int clk_khz,
 	signed int i;
 
 	/* calculate the timeout range value */
-	i = fls(timeout * clk_khz - 1) - 16;
-	i = clamp(i, 0, 15);
+	i = fls(timeout * clk_khz - 1) - DW_WDT_TIMEOUT_SHIFT;
+	i = clamp_t(int, i, 0, DW_WDT_TORR_TOP_MASK);
 
 	writel(i | (i << 4), base + DW_WDT_TORR);
 
@@ -90,6 +94,20 @@ static int designware_wdt_stop(struct udevice *dev)
 	return 0;
 }
 
+static int designware_wdt_get_timeout(struct udevice *dev, u64 *timeout_msp)
+{
+	struct designware_wdt_priv *priv = dev_get_priv(dev);
+	u32 top;
+
+	if (!designware_wdt_is_enabled(priv->base))
+		return -ENODATA;
+
+	top = readl(priv->base + DW_WDT_TORR) & DM_WDT_TORR_TOP_MASK;
+	*timeout_msp = lldiv(1ULL << (DW_WDT_TIMEOUT_SHIFT + top), priv->clk_khz);
+
+	return 0;
+}
+
 static int designware_wdt_start(struct udevice *dev, u64 timeout, ulong flags)
 {
 	struct designware_wdt_priv *priv = dev_get_priv(dev);
@@ -151,6 +169,7 @@ static const struct wdt_ops designware_wdt_ops = {
 	.start = designware_wdt_start,
 	.reset = designware_wdt_reset,
 	.stop = designware_wdt_stop,
+	.get_timeout = designware_wdt_get_timeout,
 };
 
 static const struct udevice_id designware_wdt_ids[] = {
Signature · 1 line
2.39.2
Reviewed-by on v2 5/9
#14 lore raw

Re: [PATCH v2 5/9] watchdog: designware_wdt: add get_timeout operation

Hi Juuso,

Quoted 6 lines · Juuso Rinta

Implement the get_timeout operation for the designware watchdog driver.

Add defines for the timeout shift (16) and for the mask (15) used by both the getter and the setter.

[...]

drivers/watchdog/designware_wdt.c static int designware_wdt_stop(struct udevice *dev) · line 105

Quoted 1 line
drivers/watchdog/designware_wdt.c +1 -0
+	top = readl(priv->base + DW_WDT_TORR) & DM_WDT_TORR_TOP_MASK;

This doesn't compile (e.g. socfpga_stratix10_defconfig), the define is DW_WDT_TORR_TOP_MASK. No need for a v3 because of this, I'll fix it while applying. With this fixed:

Thanks,
Stefan

#15 lore raw

Re: [PATCH v2 5/9] watchdog: designware_wdt: add get_timeout operation

In reply to Juuso Rinta
Quoted 6 lines · Juuso Rinta

Implement the get_timeout operation for the designware watchdog driver.

Add defines for the timeout shift (16) and for the mask (15) used by both the getter and the setter.

Applied to u-boot-watchdog/main, with a fixup while applying:

[sr: fix DM_WDT_TORR_TOP_MASK typo, s/DM_/DW_/]

Thanks,
Stefan

#16 lore raw

PATCH v2 6/9 watchdog: max6370_wdt: use a lookup table for timeout selection

In reply to Juuso Rinta

Add a lookup table for the possible timeout values. Replace the if-else chain with this table in max6370_wdt_start().

---
 drivers/watchdog/max6370_wdt.c | 35 +++++++++++++++++++++--------------
 1 file changed, 21 insertions(+), 14 deletions(-)
drivers/watchdog/max6370_wdt.c +21 -14
index 584a4ed468c..fadf4d41e1d 100644
@@ -19,6 +19,16 @@
 
 #define MAX6370_WDI		0x8
 
+static const u16 max6370_timeouts_ms[] = {
+	[MAX6370_SET_1MS]	= 1,
+	[MAX6370_SET_10MS]	= 10,
+	[MAX6370_SET_30MS]	= 30,
+	[MAX6370_SET_100MS]	= 100,
+	[MAX6370_SET_1S]	= 1000,
+	[MAX6370_SET_10S]	= 10000,
+	[MAX6370_SET_60S]	= 60000,
+};
+
 struct max6370_wdt {
 	void __iomem *reg;
 	struct gpio_desc gpio_wdi;
@@ -28,24 +38,21 @@ static int max6370_wdt_start(struct udevice *dev, u64 ms, ulong flags)
 {
 	struct max6370_wdt *wdt = dev_get_priv(dev);
 	u8 val;
+	int sel = MAX6370_SET_60S;
 
 	val = readb(wdt->reg);
 	val &= ~MAX6370_SET_MASK;
 
-	if (ms <= 1)
-		val |= MAX6370_SET_1MS;
-	else if (ms <= 10)
-		val |= MAX6370_SET_10MS;
-	else if (ms <= 30)
-		val |= MAX6370_SET_30MS;
-	else if (ms <= 100)
-		val |= MAX6370_SET_100MS;
-	else if (ms <= 1000)
-		val |= MAX6370_SET_1S;
-	else if (ms <= 10000)
-		val |= MAX6370_SET_10S;
-	else
-		val |= MAX6370_SET_60S;
+	for (size_t i = 0; i < ARRAY_SIZE(max6370_timeouts_ms); i++) {
+		if (i == MAX6370_SET_DISABLE)
+			continue;
+		if (ms <= max6370_timeouts_ms[i]) {
+			sel = i;
+			break;
+		}
+	}
+
+	val |= sel;
 
 	writeb(val, wdt->reg);
 
Signature · 1 line
2.39.2
#18 lore raw

Re: [PATCH v2 6/9] watchdog: max6370_wdt: use a lookup table for timeout selection

In reply to Juuso Rinta
Quoted 4 lines · Juuso Rinta

Add a lookup table for the possible timeout values. Replace the if-else chain with this table in max6370_wdt_start().

Applied to u-boot-watchdog/main

Thanks,
Stefan

#19 lore raw

PATCH v2 7/9 watchdog: max6370_wdt: add get_timeout operation

In reply to Juuso Rinta

Implement the get_timeout operation for the max6370_wdt watchdog driver. This is useful because the device supports a fixed set of periods, so max6370_wdt_start() rounds the requested value up to the next one.

---
 drivers/watchdog/max6370_wdt.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
drivers/watchdog/max6370_wdt.c +14 -0
index fadf4d41e1d..48866a5880f 100644
@@ -72,6 +72,19 @@ static int max6370_wdt_stop(struct udevice *dev)
 	return 0;
 }
 
+static int max6370_wdt_get_timeout(struct udevice *dev, u64 *timeout_msp)
+{
+	struct max6370_wdt *wdt = dev_get_priv(dev);
+	u8 val = readb(wdt->reg) & MAX6370_SET_MASK;
+
+	if (val == MAX6370_SET_DISABLE)
+		return -ENODATA;
+
+	*timeout_msp = max6370_timeouts_ms[val];
+
+	return 0;
+}
+
 static int max6370_wdt_reset(struct udevice *dev)
 {
 	struct max6370_wdt *wdt = dev_get_priv(dev);
@@ -108,6 +121,7 @@ static const struct wdt_ops max6370_wdt_ops = {
 	.start = max6370_wdt_start,
 	.stop = max6370_wdt_stop,
 	.reset = max6370_wdt_reset,
+	.get_timeout = max6370_wdt_get_timeout,
 };
 
 static const struct udevice_id max6370_wdt_ids[] = {
Signature · 1 line
2.39.2
Reviewed-by on v2 7/9
#20 lore raw

Re: [PATCH v2 7/9] watchdog: max6370_wdt: add get_timeout operation

Quoted 5 lines · Juuso Rinta

Implement the get_timeout operation for the max6370_wdt watchdog driver. This is useful because the device supports a fixed set of periods, so max6370_wdt_start() rounds the requested value up to the next one.

Thanks,
Stefan

#21 lore raw

Re: [PATCH v2 7/9] watchdog: max6370_wdt: add get_timeout operation

In reply to Juuso Rinta
Quoted 5 lines · Juuso Rinta

Implement the get_timeout operation for the max6370_wdt watchdog driver. This is useful because the device supports a fixed set of periods, so max6370_wdt_start() rounds the requested value up to the next one.

Applied to u-boot-watchdog/main

Thanks,
Stefan

#22 lore raw

PATCH v2 8/9 watchdog: sandbox_wdt: add get_timeout operation

In reply to Juuso Rinta

Implement the get_timeout operation for the sandbox watchdog driver.

Round the timeout in sandbox_wdt_start() up to a whole second to emulate hardware with 1s granularity, so that the effective timeout can differ from the requested one and the tests can check that.

---
 drivers/watchdog/sandbox_wdt.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
drivers/watchdog/sandbox_wdt.c +16 -1
index 9a0cdac232a..7b1acb69a64 100644
@@ -3,6 +3,7 @@
  * Copyright 2017 Google, Inc
  */
 
+#include <div64.h>
 #include <dm.h>
 #include <wdt.h>
 #include <asm/state.h>
@@ -11,7 +12,8 @@ static int sandbox_wdt_start(struct udevice *dev, u64 timeout, ulong flags)
 {
 	struct sandbox_state *state = state_get_current();
 
-	state->wdt.counter = timeout;
+	/* Emulate hardware with 1s granularity, round up to a whole second */
+	state->wdt.counter = DIV_ROUND_UP_ULL(timeout, 1000) * 1000;
 	state->wdt.running = true;
 
 	return 0;
@@ -43,6 +45,18 @@ static int sandbox_wdt_expire_now(struct udevice *dev, ulong flags)
 	return 0;
 }
 
+static int sandbox_wdt_get_timeout(struct udevice *dev, u64 *timeout_msp)
+{
+	struct sandbox_state *state = state_get_current();
+
+	if (!state->wdt.running)
+		return -ENODATA;
+
+	*timeout_msp = state->wdt.counter;
+
+	return 0;
+}
+
 static int sandbox_wdt_probe(struct udevice *dev)
 {
 	struct wdt_uc_plat *plat = dev_get_uclass_plat(dev);
@@ -58,6 +72,7 @@ static const struct wdt_ops sandbox_wdt_ops = {
 	.reset = sandbox_wdt_reset,
 	.stop = sandbox_wdt_stop,
 	.expire_now = sandbox_wdt_expire_now,
+	.get_timeout = sandbox_wdt_get_timeout,
 };
 
 static const struct udevice_id sandbox_wdt_ids[] = {
Signature · 1 line
2.39.2
Reviewed-by on v2 8/9
#23 lore raw

Re: [PATCH v2 8/9] watchdog: sandbox_wdt: add get_timeout operation

Quoted 7 lines · Juuso Rinta

Implement the get_timeout operation for the sandbox watchdog driver.

Round the timeout in sandbox_wdt_start() up to a whole second to emulate hardware with 1s granularity, so that the effective timeout can differ from the requested one and the tests can check that.

Thanks,
Stefan

#24 lore raw

Re: [PATCH v2 8/9] watchdog: sandbox_wdt: add get_timeout operation

In reply to Juuso Rinta
Quoted 7 lines · Juuso Rinta

Implement the get_timeout operation for the sandbox watchdog driver.

Round the timeout in sandbox_wdt_start() up to a whole second to emulate hardware with 1s granularity, so that the effective timeout can differ from the requested one and the tests can check that.

Applied to u-boot-watchdog/main

Thanks,
Stefan

#25 lore raw

PATCH v2 9/9 test: wdt: add tests for get_timeout support

In reply to Juuso Rinta

Add tests for (1) get_timeout operation, (2) warning print when the timeout is not longer than the servicing interval, and (3) the wdt gettimeout command

The get_timeout success-path and not-running case is tested with the sandbox driver, and the not-supported path through the GPIO driver.

Warning print is tested via the gpio driver. The sandbox driver rounds the requested timeout up to a whole second so the timeout is never set lower than or equal to the 50ms servicing interval.

For the 'wdt gettimeout' command tests, a new test/cmd/wdt.c file is added and the file is included to test/cmd/Makefile.

---
 test/cmd/Makefile |  3 +++
 test/cmd/wdt.c    | 60 ++++++++++++++++++++++++++++++++++++++++++++++
 test/dm/wdt.c     | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 134 insertions(+), 1 deletion(-)
test/cmd/Makefile +3 -0
index 79cbb291250..9ecc665262a 100644
@@ -43,6 +43,9 @@ obj-$(CONFIG_CMD_PWM) += pwm.o
 obj-$(CONFIG_CMD_READ) += rw.o
 obj-$(CONFIG_CMD_SETEXPR) += setexpr.o
 obj-$(CONFIG_CMD_TEMPERATURE) += temperature.o
+ifeq ($(CONFIG_CMD_WDT)$(CONFIG_WDT_GPIO)$(CONFIG_WDT_SANDBOX),yyy)
+obj-y += wdt.o
+endif
 ifdef CONFIG_NET_LEGACY
 obj-$(CONFIG_CMD_WGET) += wget.o
 endif
test/cmd/wdt.c new file +60 -0

60 lines not on this page · raw message

test/dm/wdt.c +71 -1

96 lines not on this page · raw message

Signature · 1 line
2.39.2
Reviewed-by on v2 9/9
#26 lore raw

Re: [PATCH v2 9/9] test: wdt: add tests for get_timeout support

Quoted 8 lines · Juuso Rinta

Add tests for (1) get_timeout operation, (2) warning print when the timeout is not longer than the servicing interval, and (3) the wdt gettimeout command

The get_timeout success-path and not-running case is tested with the sandbox driver, and the not-supported path through the GPIO driver.

Quoted 8 lines

Warning print is tested via the gpio driver. The sandbox driver rounds the requested timeout up to a whole second so the timeout is never set lower than or equal to the 50ms servicing interval.

For the 'wdt gettimeout' command tests, a new test/cmd/wdt.c file is added and the file is included to test/cmd/Makefile.

Thanks,
Stefan

#27 lore raw

Re: [PATCH v2 9/9] test: wdt: add tests for get_timeout support

In reply to Juuso Rinta
Quoted 8 lines · Juuso Rinta

Add tests for (1) get_timeout operation, (2) warning print when the timeout is not longer than the servicing interval, and (3) the wdt gettimeout command

The get_timeout success-path and not-running case is tested with the sandbox driver, and the not-supported path through the GPIO driver.

Quoted 8 lines

Warning print is tested via the gpio driver. The sandbox driver rounds the requested timeout up to a whole second so the timeout is never set lower than or equal to the 50ms servicing interval.

For the 'wdt gettimeout' command tests, a new test/cmd/wdt.c file is added and the file is included to test/cmd/Makefile.

Applied to u-boot-watchdog/main

Thanks,
Stefan