watchdog: add get_timeout operation
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 programmed timeout value is not longer than the servicing interval. For drivers that do not implement get_timeout, behaviour is unchanged.
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.
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.
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: Add a gettimeout subcommand to the wdt command in cmd/wdt.c
- Patch 4: Implement the get_timeout operation for designware_wdt
- Patch 5: Implement the operation for max6370_wdt
- Patch 6: Implement the operation for sandbox_wdt
- Patch 7: Implement tests for wdt_get_timeout(), wdt gettimeout and the warning print
---
Juuso Rinta (7):
watchdog: wdt-uclass: add get_timeout operation and helper
watchdog: warn if programmed timeout not longer than servicing interval
cmd: wdt: add gettimeout command
watchdog: designware_wdt: add get_timeout operation
watchdog: max6370_wdt: add get_timeout operation
watchdog: sandbox_wdt: add get_timeout operation
test: wdt: add tests for wdt_get_timeout(), wdt gettimeout and warning print
cmd/wdt.c | 25 ++++++++++++++++++++++ doc/usage/cmd/wdt.rst | 10 ++++++++- drivers/watchdog/designware_wdt.c | 21 +++++++++++++++++- drivers/watchdog/max6370_wdt.c | 39 +++++++++++++++++++++++++++++++++ drivers/watchdog/sandbox_wdt.c | 13 +++++++++++ drivers/watchdog/wdt-uclass.c | 21 ++++++++++++++++++ include/wdt.h | 22 +++++++++++++++++++ test/dm/wdt.c | 45 +++++++++++++++++++++++++++++++++++++++ 8 files changed, 194 insertions(+), 2 deletions(-)
---
base-commit: 62c9805d8d57cad407f0295879cc9a6182da464a
change-id: 20260824-rinta-watchdog-gettimeout-f39e924f40c8
Best regards,
Signature · 1 line
Juuso Rinta <[email protected]>
PATCH 1/7 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 | 13 +++++++++++++ include/wdt.h | 22 ++++++++++++++++++++++ 2 files changed, 35 insertions(+)
drivers/watchdog/wdt-uclass.c +13 -0
@@ -116,6 +116,16 @@ int initr_watchdog(void)return 0 ; } +int wdt_get_timeout (struct udevice *dev,u64 *timeout_ms) +{ +const struct wdt_ops *ops =device_get_ops (dev); + +if (!ops->get_timeout) +return -ENOSYS ; + +return ops->get_timeout (dev, timeout_ms); +} +int wdt_start (struct udevice *dev,u64 timeout_ms,ulong flags) {const struct wdt_ops *ops =device_get_ops (dev);
@@ -159,6 +169,9 @@ int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)* rounding is ignored to avoid noise. */ req_str[0 ] ='\0' ; +/* Check programmed timeout from driver if op supported */ +if (wdt_get_timeout (dev, &timeout_ms) ==0 ) + tout_s =lldiv (timeout_ms,1000 );if (req_s != tout_s)snprintf (req_str,sizeof (req_str),", requested %ds" , req_s);
include/wdt.h +22 -0
@@ -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_ms: 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_ms); +/** * 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_ms: 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_ms); };int initr_watchdog (void );
Signature · 1 line
2.39.2
Re: [PATCH 1/7] watchdog: wdt-uclass: add get_timeout operation and helper
Hi Juuso,
Quoted 22 lines · Juuso Rinta
watchdog: wdt-uclass: add
get_timeoutoperation and helperCurrently the
wdt_start()function uses the plat data to check if a driver has reported amax_timeout_msvalue 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_timeoutoperation and helper so drivers can report the effective timeout. The helper returns-ENOSYSif the driver does not implement the operation. Ifwdt_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.Signed-off-by Juuso Rinta <[email protected]>drivers/watchdog/wdt-uclass.c | 13 +++++++++++++ include/wdt.h | 22 ++++++++++++++++++++++ 2 files changed, 35 insertions(+)
drivers/watchdog/wdt-uclass.c int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags) · line 173
Quoted 5 lines
drivers/watchdog/wdt-uclass.c +3
-0@@ -159,6 +169,9 @@ int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags) req_str[0 ] ='\0' ; +/* Check programmed timeout from driver if op supported */ +if (wdt_get_timeout (dev, &timeout_ms) ==0 ) + tout_s =lldiv (timeout_ms,1000 );if (req_s != tout_s)
This separates the existing comment ("If the requested timeout was clamped, note the value...") from the code it describes. That comment is also now incomplete, since the difference may come from driver rounding as well as clamping. Please can you move the wdt_get_timeout() call above the comment and update it to mention both cases?
include/wdt.h int wdt_reset(struct udevice *dev); · line 79
Quoted 8 lines
include/wdt.h +6
-0@@ -74,6 +74,19 @@ int wdt_reset(struct udevice *dev); +* @dev: WDT device +* @timeout_ms: 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_ms);
U-Boot normally uses a 'p' suffix for output parameters, so this should be 'timeout_msp' here, in the op and in the drivers. It also distinguishes it from the 'timeout_ms' input parameter of wdt_start() just above.
Regards,
Simon
PATCH 2/7 watchdog: warn if programmed timeout not longer than servicing interval
In reply to Juuso RintaWhen a driver supports the get_timeout operation, wdt_start() can print a warning in cases where the programmed timeout is shorter than or equal to the servicing interval.
Suggested by Stefan Roese on the series "watchdog: report effective timeout"
[https://lore.kernel.org/u-boot/[email protected]/]
--- drivers/watchdog/wdt-uclass.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
drivers/watchdog/wdt-uclass.c +9 -1
@@ -146,6 +146,7 @@ int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)char req_str[32 ];u32 req_s =lldiv (req_timeout_ms,1000 );u32 tout_s =lldiv (timeout_ms,1000 ); +int get_timeout_ret; svc_str[0 ] ='\0' ;if (IS_ENABLED (CONFIG_WATCHDOG )) {
@@ -170,7 +171,8 @@ int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags)*/ req_str[0 ] ='\0' ;/* Check programmed timeout from driver if op supported */ -if (wdt_get_timeout (dev, &timeout_ms) ==0 ) + get_timeout_ret =wdt_get_timeout (dev, &timeout_ms); +if (get_timeout_ret ==0 ) tout_s =lldiv (timeout_ms,1000 );if (req_s != tout_s)snprintf (req_str,sizeof (req_str),", requested %ds" , req_s);
@@ -178,6 +180,12 @@ 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 programmed timeout is not longer than servicing interval */ +if (IS_ENABLED (CONFIG_WATCHDOG ) && get_timeout_ret ==0 && + timeout_ms <= priv->reset_period) +printf ("WDT: timeout %llums is shorter than or equal to servicing interval %ldms\n" , + timeout_ms, priv->reset_period); }return ret;
Signature · 1 line
2.39.2
Re: [PATCH 2/7] watchdog: warn if programmed timeout not longer than servicing interval
Hi Juuso,
Quoted 5 lines · Juuso Rinta
watchdog: warn if programmed timeout not longer than servicing interval
When a driver supports the
get_timeoutoperation,wdt_start()can print a warning in cases where the programmed timeout is shorter than or equal to the servicing interval.
Please can you start with the motivation (the board resets unexpectedly because the watchdog expires before schedule() services it), then say what the patch does in the imperative, e.g. "Print a warning in wdt_start() when..."
drivers/watchdog/wdt-uclass.c int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags) · line 182
Quoted 5 lines
drivers/watchdog/wdt-uclass.c +3
-0@@ -178,6 +180,12 @@ int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags) +/* Warn if programmed timeout is not longer than servicing interval */ +if (IS_ENABLED (CONFIG_WATCHDOG ) && get_timeout_ret ==0 && + timeout_ms <= priv->reset_period)
Why only warn when the driver implements get_timeout? Otherwise timeout_ms still holds the clamped value passed to ops->start(), which is the best value we have, and the problem is just as real. Dropping the 'get_timeout_ret == 0' condition would cover all drivers and remove the new variable. What do you think?
drivers/watchdog/wdt-uclass.c int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags) · line 181
Quoted 4 lines
drivers/watchdog/wdt-uclass.c +2
-0@@ -178,6 +180,12 @@ int wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags) +printf ("WDT: timeout %llums is shorter than or equal to servicing interval %ldms\n" , + timeout_ms, priv->reset_period);
The message doesn't say that anything is wrong. Something like "WDT:
Warning: %llums timeout is not longer than %ldms servicing interval" is shorter and clearer. Also, reset_period is a ulong, so use %lu rather than %ld here. Please update the expected string in the test in patch 7 too.
Quoted 9 lines
Suggested by Stefan Roese on the series "watchdog: report effective timeout"
[https://lore.kernel.org/u-boot/[email protected]/]Suggested-by Stefan Roese <[email protected]>Signed-off-by Juuso Rinta <[email protected]>drivers/watchdog/wdt-uclass.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
Regards,
Simon
Re: [PATCH 2/7] watchdog: warn if programmed timeout not longer than servicing interval
Hi Simon,
Thanks again for your review. I'm working on a v2 now, following up on few questions raised in your review.
Quoted 5 lines
Why only warn when the driver implements
get_timeout? Otherwisetimeout_msstill holds the clamped value passed toops->start(), which is the best value we have, and the problem is just as real. Dropping the 'get_timeout_ret== 0' condition would cover all drivers and remove the new variable. What do you think?
I agree and would drop this condition for v2. But I think this goes a bit beyond Stefan's original suggestion ("once the uclass knows the real timeout it can warn"). For drivers that implement neither get_timeout nor max_timeout_ms, the compared value is only the requested timeout. Would you, Stefan, rather keep the warning limited to drivers implementing the get_timeout operation or drop the condition as suggested here?
Best,
Juuso
Re: [PATCH 2/7] watchdog: warn if programmed timeout not longer than servicing interval
Hi Juuso,
Quoted 6 lines · [email protected] <> wrote:
I agree and would drop this condition for v2. But I think this goes a bit beyond Stefan's original suggestion ("once the uclass knows the real timeout it can warn"). For drivers that implement neither
get_timeoutnormax_timeout_ms, the compared value is only the requested timeout. Would you, Stefan, rather keep the warning limited to drivers implementing theget_timeoutoperation or drop the condition as suggested here?
Even when we only have the requested timeout, a timeout that is no longer than the servicing interval is a configuration mistake. The board will reset unless the hardware happens to round the timeout well upwards. So the warning is still worth printing in that case. If you want to be precise, the message could say 'requested' when get_timeout() is not available, but I don't think that is needed. Stefan has the final say, of course.
Regards,
Simon
PATCH 3/7 cmd: wdt: add gettimeout command
In reply to Juuso RintaAdd 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 | 10 +++++++++- 2 files changed, 34 insertions(+), 1 deletion(-)
cmd/wdt.c +25 -0
@@ -131,6 +131,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; + + ret =check_currdev (); +if (ret) +return ret; + + 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[]) {
@@ -162,6 +185,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,
@@ -170,4 +194,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 +9 -1
@@ -16,6 +16,7 @@ Synopsis wdt start <timeout_ms> [flags] wdt stop wdt reset + wdt gettimeout wdt expire [flags] Description
@@ -34,6 +35,9 @@ 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 'wdt expire' command let's the current watchdog timer expire immediately. This will lead to a reset.
@@ -60,14 +64,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
Re: [PATCH 3/7] cmd: wdt: add gettimeout command
Hi Juuso,
doc/usage/cmd/wdt.rst The 'wdt stop' command stops the current watchdog timer. · line 36
Quoted 4 lines · Juuso Rinta
doc/usage/cmd/wdt.rst +2
-0@@ -34,6 +35,9 @@ The 'wdt stop' command stops the current watchdog timer. +The 'wdt gettimeout' command reports current watchdog device's programmed +timeout value in milliseconds when supported by the driver.
It would be worth saying that the command fails if the watchdog is not running or the driver does not support it, since scripts may rely on the return value.
Regards,
Simon
PATCH 4/7 watchdog: designware_wdt: add get_timeout operation
In reply to Juuso RintaImplement the get_timeout operation for the designware watchdog driver.
Add a define for the timeout shift used by both the getter and the setter instead of the hardcoded 16.
--- drivers/watchdog/designware_wdt.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-)
drivers/watchdog/designware_wdt.c +20 -1
@@ -18,6 +18,8 @@#define DW_WDT_CR_RMOD_OFFSET 0x01#define DW_WDT_CRR_RESTART_VAL 0x76 +#define DW_WDT_TIMEOUT_SHIFT 16 +struct designware_wdt_priv {void __iomem *base;unsigned int clk_khz;
@@ -34,7 +36,7 @@ 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 =fls (timeout * clk_khz -1 ) -DW_WDT_TIMEOUT_SHIFT ; i =clamp (i,0 ,15 );writel (i | (i <<4 ), base +DW_WDT_TORR );
@@ -90,6 +92,22 @@ static int designware_wdt_stop(struct udevice *dev)return 0 ; } +static int designware_wdt_get_timeout (struct udevice *dev,u64 *timeout_ms) +{ +struct designware_wdt_priv *priv =dev_get_priv (dev); +u32 top; + +if (!designware_wdt_is_enabled (priv->base)) +return -ENODATA ; +if (!priv->clk_khz) +return -EINVAL ; + + top =readl (priv->base +DW_WDT_TORR ) &0xF ; + *timeout_ms = (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
Re: [PATCH 4/7] watchdog: designware_wdt: add get_timeout operation
Hi Juuso,
Quoted 14 lines · Juuso Rinta
watchdog:
designware_wdt: addget_timeoutoperationImplement the
get_timeoutoperation for the designware watchdog driver.Add a define for the timeout shift used by both the getter and the setter instead of the hardcoded 16.
Signed-off-by Juuso Rinta <[email protected]>drivers/watchdog/designware_wdt.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-)
drivers/watchdog/designware_wdt.c static int designware_wdt_stop(struct udevice *dev) · line 95
Quoted 4 lines
drivers/watchdog/designware_wdt.c +4
-0@@ -90,6 +92,22 @@ static int designware_wdt_stop(struct udevice *dev) +if (!designware_wdt_is_enabled (priv->base)) +return -ENODATA ; +if (!priv->clk_khz) +return -EINVAL ;
I don't think the clk_khz check can trigger. With CLK enabled, probe() already fails with -EINVAL if the rate is zero; otherwise clk_khz comes from CFG_DW_WDT_CLOCK_KHZ. The setter uses clk_khz without a check too. Please can you drop it, unless you have a case in mind?
drivers/watchdog/designware_wdt.c static int designware_wdt_stop(struct udevice *dev) · line 106
Quoted 2 lines
drivers/watchdog/designware_wdt.c +2
-0+ top =readl (priv->base +DW_WDT_TORR ) &0xF ; + *timeout_ms = (1ULL << (DW_WDT_TIMEOUT_SHIFT + top)) / priv->clk_khz;
Since you are replacing the hard-coded 16 with a define, it would be worth doing the same for the 0xF mask, e.g. DW_WDT_TORR_TOP_MASK using GENMASK(3, 0). The 15 in the setter's clamp() is the same limit.
The calculation looks right: 2^(16 + TOP) cycles, matching dw_wdt_top_in_seconds() in Linux. Rounding down seems fine, since it makes the servicing-interval warning err on the safe side.
Regards,
Simon
PATCH 5/7 watchdog: max6370_wdt: add get_timeout operation
In reply to Juuso RintaImplement the get_timeout operation for the max6370_wdt watchdog driver.
--- drivers/watchdog/max6370_wdt.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+)
drivers/watchdog/max6370_wdt.c +39 -0
@@ -65,6 +65,44 @@ static int max6370_wdt_stop(struct udevice *dev)return 0 ; } +static int max6370_wdt_get_timeout (struct udevice *dev,u64 *timeout_ms) +{ +struct max6370_wdt *wdt =dev_get_priv (dev); +u8 val =readb (wdt->reg) &MAX6370_SET_MASK ; +int ret =0 ; + +switch (val) { +case MAX6370_SET_1MS : + *timeout_ms =1 ; +break ; +case MAX6370_SET_10MS : + *timeout_ms =10 ; +break ; +case MAX6370_SET_30MS : + *timeout_ms =30 ; +break ; +case MAX6370_SET_100MS : + *timeout_ms =100 ; +break ; +case MAX6370_SET_1S : + *timeout_ms =1000 ; +break ; +case MAX6370_SET_10S : + *timeout_ms =10000 ; +break ; +case MAX6370_SET_60S : + *timeout_ms =60000 ; +break ; +case MAX6370_SET_DISABLE : + ret = -ENODATA ; +break ; +default : + ret = -EINVAL ; + } + +return ret; +} +static int max6370_wdt_reset (struct udevice *dev) {struct max6370_wdt *wdt =dev_get_priv (dev);
@@ -101,6 +139,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
Re: [PATCH 5/7] watchdog: max6370_wdt: add get_timeout operation
Hi Juuso,
Quoted 3 lines · Juuso Rinta
watchdog:
max6370_wdt: addget_timeoutoperationImplement the
get_timeoutoperation for themax6370_wdtwatchdog driver.
It would be worth saying why this is useful: the device only supports a fixed set of periods, so max6370_wdt_start() rounds the requested value up to the next one.
Quoted 26 lines
Signed-off-by Juuso Rinta <[email protected]>drivers/watchdog/max6370_wdt.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+)drivers/watchdog/max6370_wdt.c +18
-0@@ -65,6 +65,44 @@ static int max6370_wdt_stop(struct udevice *dev) +switch (val) { +case MAX6370_SET_1MS : + *timeout_ms =1 ; +break ; +case MAX6370_SET_10MS : + *timeout_ms =10 ; +break ; +case MAX6370_SET_30MS : + *timeout_ms =30 ; +break ; +case MAX6370_SET_100MS : + *timeout_ms =100 ; +break ; +case MAX6370_SET_1S : + *timeout_ms =1000 ; +break ; +case MAX6370_SET_10S : + *timeout_ms =10000 ;
drivers/watchdog/max6370_wdt.c static int max6370_wdt_stop(struct udevice *dev) · line 92
Quoted 10 lines
drivers/watchdog/max6370_wdt.c +10
-0+break ; +case MAX6370_SET_60S : + *timeout_ms =60000 ; +break ; +case MAX6370_SET_DISABLE : + ret = -ENODATA ; +break ; +default : + ret = -EINVAL ; + }
MAX6370_SET_MASK is 0x7 and all eight values are handled, so the default case is unreachable. Since the setting is just a 3-bit index, a lookup table would be shorter and drop the 'ret' variable:
static const u16 max6370_timeouts_ms[] = {
[MAX6370_SET_1MS] = 1,
[MAX6370_SET_10MS] = 10,
...
[MAX6370_SET_60S] = 60000,
};
if (val == MAX6370_SET_DISABLE)
return -ENODATA;
*timeout_ms = max6370_timeouts_ms[val];
return 0;max6370_wdt_start() could then use the same table instead of its if-else chain, so the two cannot drift apart. What do you think?
Regards,
Simon
Re: [PATCH 5/7] watchdog: max6370_wdt: add get_timeout operation
From: Juuso Rinta <[email protected]>
Quoted 2 lines
max6370_wdt_start()could then use the same table instead of its if-else chain, so the two cannot drift apart. What do you think?
Agreed, I'll do the lookup table approach for v2 and have max6370_wdt_start() also use the table.
BR,
Juuso
PATCH 6/7 watchdog: sandbox_wdt: add get_timeout operation
In reply to Juuso RintaImplement the get_timeout operation for the sandbox watchdog driver.
--- drivers/watchdog/sandbox_wdt.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
drivers/watchdog/sandbox_wdt.c +13 -0
@@ -7,6 +7,18 @@#include <wdt.h> #include <asm/state.h> +static int sandbox_wdt_get_timeout (struct udevice *dev,u64 *timeout_ms) +{ +struct sandbox_state *state =state_get_current (); + +if (!state->wdt.running) +return -ENODATA ; + + *timeout_ms = state->wdt.counter; + +return 0 ; +} +static int sandbox_wdt_start (struct udevice *dev,u64 timeout,ulong flags) {struct sandbox_state *state =state_get_current ();
@@ -58,6 +70,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
Re: [PATCH 6/7] watchdog: sandbox_wdt: add get_timeout operation
Hi Juuso,
Quoted 14 lines · Juuso Rinta
watchdog:
sandbox_wdt: addget_timeoutoperationImplement the
get_timeoutoperation for the sandbox watchdog driver.Signed-off-by Juuso Rinta <[email protected]>drivers/watchdog/sandbox_wdt.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)drivers/watchdog/sandbox_wdt.c +3
-0@@ -7,6 +7,18 @@ +static int sandbox_wdt_get_timeout (struct udevice *dev,u64 *timeout_ms) +{ +struct sandbox_state *state =state_get_current ();
drivers/watchdog/sandbox_wdt.c line 19
Quoted 10 lines
drivers/watchdog/sandbox_wdt.c +9
-0+ +if (!state->wdt.running) +return -ENODATA ; + + *timeout_ms = state->wdt.counter; + +return 0 ; +} +static int sandbox_wdt_start (struct udevice *dev,u64 timeout,ulong flags)
Please can you move this after sandbox_wdt_expire_now() so the functions follow the order of the ops struct.
This returns exactly the value that start() stored, so sandbox never reports a timeout different from the one requested, which is the case the series is for. The test in patch 7 therefore cannot reach the ', requested %ds' path through wdt_get_timeout(); the clamping test only covers it because the uclass clamps to max_timeout_ms first, and passes with or without get_timeout(). It would be worth having the emulated hardware adjust the value in sandbox_wdt_start(), e.g. rounding up to a whole second or a power of two, or to fixed steps as max6370 does. A test could then check that wdt_get_timeout() returns the adjusted value and that the start message reports both values. What do you think?
Regards,
Simon
Re: [PATCH 6/7] watchdog: sandbox_wdt: add get_timeout operation
Quoted 11 lines
This returns exactly the value that
start()stored, so sandbox never reports a timeout different from the one requested, which is the case the series is for. The test in patch 7 therefore cannot reach the ', requested %ds' path throughwdt_get_timeout(); the clamping test only covers it because the uclass clamps tomax_timeout_msfirst, and passes with or withoutget_timeout(). It would be worth having the emulated hardware adjust the value insandbox_wdt_start(), e.g. rounding up to a whole second or a power of two, or to fixed steps as max6370 does. A test could then check thatwdt_get_timeout()returns the adjusted value and that the start message reports both values. What do you think?
Agreed, I will make the sandbox driver round up to a whole second and adjust the tests accordignly.
Regards,
Juuso
PATCH 7/7 test: wdt: add tests for wdt_get_timeout(), wdt gettimeout and warning print
In reply to Juuso RintaAdd a test for the wdt_get_timeout() helper and the subcommand wdt gettimeout which exposes this helper to the shell interface.
Add a test for the warning print when requested timeout is shorter than the servicing interval.
Sandbox driver tests the success-path and the not-running case, and GPIO driver tests the not-supported path.
--- test/dm/wdt.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+)
test/dm/wdt.c +45 -0
@@ -25,6 +25,7 @@ static int dm_test_wdt_base(struct unit_test_state *uts)struct udevice *dev;struct wdt_uc_plat *plat;const u64 timeout =4000 ; +u64 get_timeout_val;ut_assertok (uclass_get_device_by_driver (UCLASS_WDT ,DM_DRIVER_GET (wdt_sandbox), &dev));
@@ -68,8 +69,42 @@ static int dm_test_wdt_base(struct unit_test_state *uts)ut_assert_nextline ("WDT: Started %s with servicing every 50ms (%ds timeout)" , dev->name,SANDBOX_WDT_MAX_TIMEOUT_MS /1000 );ut_assert_console_end (); + +/* wdt_get_timeout() helper */ ut_assertok (wdt_stop (dev));ut_asserteq (false, state->wdt.running); +ut_asserteq (-ENODATA ,wdt_get_timeout (dev, &get_timeout_val)); +ut_assertok (wdt_start (dev, timeout,0 )); +ut_asserteq (true, state->wdt.running); +ut_asserteq (timeout, state->wdt.counter); +ut_assertok (wdt_get_timeout (dev, &get_timeout_val)); +ut_asserteq (timeout, get_timeout_val); + +/* wdt gettimeout command */ +ut_assertok (wdt_stop (dev)); +ut_assertok (run_commandf ("wdt dev %s" , dev->name)); +console_record_reset (); +ut_asserteq (CMD_RET_FAILURE ,run_commandf ("wdt gettimeout" )); +ut_assert_nextline ("Watchdog not running." ); +ut_assert_console_end (); + +ut_assertok (wdt_start (dev, timeout,0 )); +console_record_reset (); +ut_assertok (run_commandf ("wdt gettimeout" )); +ut_assert_nextline ("Timeout: %llums" , timeout); +ut_assert_console_end (); +ut_assertok (wdt_stop (dev)); + +/* warning print when timeout smaller than servicing interval (50ms) */ +console_record_reset (); +ut_assertok (wdt_start (dev,21 ,0 )); +ut_asserteq (21 , state->wdt.counter); +ut_assert_nextline ("WDT: Started %s with servicing every 50ms (%ds timeout)" , + dev->name,21 /1000 ); +ut_assert_nextline ( +"WDT: timeout %dms is shorter than or equal to servicing interval 50ms" ,21 ); +ut_assert_console_end (); +ut_assertok (wdt_stop (dev));return 0 ; }
@@ -87,6 +122,7 @@ static int dm_test_wdt_gpio_toggle(struct unit_test_state *uts)const u64 timeout =4000 ;const int offset =8 ;int val; +u64 get_timeout_val;ut_assertok (uclass_get_device_by_name (UCLASS_WDT ,"wdt-gpio-toggle" , &wdt));
@@ -104,6 +140,15 @@ static int dm_test_wdt_gpio_toggle(struct unit_test_state *uts) wdt->name, (u32)lldiv(timeout, 1000));ut_assert_console_end (); +/* GPIO driver does not implement get_timeout op */ +ut_asserteq (-ENOSYS ,wdt_get_timeout (wdt, &get_timeout_val)); +ut_assertok (run_commandf ("wdt dev %s" , wdt->name)); + +console_record_reset (); +ut_asserteq (CMD_RET_FAILURE ,run_commandf ("wdt gettimeout" )); +ut_assert_nextline ("Getting watchdog timeout not supported." ); +ut_assert_console_end (); + val =sandbox_gpio_get_value (gpio, offset);ut_assertok (wdt_reset (wdt));ut_asserteq (!val,sandbox_gpio_get_value (gpio, offset));
Signature · 1 line
2.39.2
Re: [PATCH 7/7] test: wdt: add tests for wdt_get_timeout(), wdt gettimeout and warning print
Hi Juuso,
Quoted 7 lines · Juuso Rinta
test: wdt: add tests for
wdt_get_timeout(), wdt gettimeout and warning printAdd a test for the
wdt_get_timeout()helper and the subcommand wdt gettimeout which exposes this helper to the shell interface.Add a test for the warning print when requested timeout is shorter than the servicing interval.
The warning is about the programmed timeout (as reported by get_timeout), not the requested one, and it also fires when they are equal. Please can you reword this to match patch 2? The subject is also long; 'test: wdt: Add tests for get_timeout support' would do.
Quoted 11 lines
Sandbox driver tests the success-path and the not-running case, and GPIO driver tests the not-supported path.
Signed-off-by Juuso Rinta <[email protected]>test/dm/wdt.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+)
test/dm/wdt.c static int dm_test_wdt_base(struct unit_test_state *uts) · line 73
Quoted 5 lines
test/dm/wdt.c +3
-0@@ -68,8 +69,42 @@ static int dm_test_wdt_base(struct unit_test_state *uts) + +/* wdt_get_timeout() helper */ ut_assertok (wdt_stop (dev));ut_asserteq (false, state->wdt.running); +ut_asserteq (-ENODATA ,wdt_get_timeout (dev, &get_timeout_val));
dm_test_wdt_base() is getting long. It would be worth putting the get_timeout checks in their own test, e.g. dm_test_wdt_get_timeout(), and the 'wdt gettimeout' command checks in another, so a failure points straight at what broke. What do you think?
test/dm/wdt.c static int dm_test_wdt_base(struct unit_test_state *uts) · line 70
Quoted 4 lines
test/dm/wdt.c +2
-0@@ -68,8 +69,42 @@ static int dm_test_wdt_base(struct unit_test_state *uts) +ut_assertok (wdt_get_timeout (dev, &get_timeout_val)); +ut_asserteq (timeout, get_timeout_val);
ut_asserteq() casts both sides to unsigned int, so please use ut_asserteq_64() for the u64 value.
test/dm/wdt.c static int dm_test_wdt_base(struct unit_test_state *uts) · line 71
Quoted 5 lines
test/dm/wdt.c +3
-0@@ -68,8 +69,42 @@ static int dm_test_wdt_base(struct unit_test_state *uts) +/* wdt gettimeout command */ +ut_assertok (wdt_stop (dev)); +ut_assertok (run_commandf ("wdt dev %s" , dev->name));
Just to check: cmd/wdt.c keeps the device in a static 'currdev' pointer. After this test the driver model tree is torn down, so that pointer refers to a freed device, and a later 'wdt' command without 'wdt dev' (in another test, or at the prompt after 'ut dm') would use freed memory. The command already has this problem..., but these are the first tests to set 'currdev', so they expose it.
One work-around would be to clear the pointer at the end of the test.
test/dm/wdt.c static int dm_test_wdt_base(struct unit_test_state *uts) · line 76
Quoted 10 lines
test/dm/wdt.c +8
-0@@ -68,8 +69,42 @@ static int dm_test_wdt_base(struct unit_test_state *uts) +/* warning print when timeout smaller than servicing interval (50ms) */ +console_record_reset (); +ut_assertok (wdt_start (dev,21 ,0 )); +ut_asserteq (21 , state->wdt.counter); +ut_assert_nextline ("WDT: Started %s with servicing every 50ms (%ds timeout)" , + dev->name,21 /1000 ); +ut_assert_nextline ( +"WDT: timeout %dms is shorter than or equal to servicing interval 50ms" ,21 );
The constants can go straight in the strings, i.e. "(0s timeout)" and "timeout 21ms ...", which is easier to read than '21 / 1000' and a '%d' with a literal. It would also be good to check exactly 50ms, the boundary of the <= comparison in wdt_start(), and that a value just above it (e.g. 51) gives no warning.
Regards,
Simon
Re: [PATCH 7/7] test: wdt: add tests for wdt_get_timeout(), wdt gettimeout and warning print
Quoted 4 lines
dm_test_wdt_base()is getting long. It would be worth putting theget_timeoutchecks in their own test, e.g.dm_test_wdt_get_timeout(), and the 'wdt gettimeout' command checks in another, so a failure points straight at what broke. What do you think?
Will do.
Quoted 8 lines
Just to check:
cmd/wdt.ckeeps the device in a static 'currdev' pointer. After this test the driver model tree is torn down, so that pointer refers to a freed device, and a later 'wdt' command without 'wdt dev' (in another test, or at the prompt after 'ut dm') would use freed memory. The command already has this problem..., but these are the first tests to set 'currdev', so they expose it.One work-around would be to clear the pointer at the end of the test.
What do you think would be the way to go here? Since currdev is static in cmd/wdt.c, clearing it from the test would need a small helper in cmd/wdt.c. Alternatively I could also replace the static pointer with a static char currdev_name[64] and resolve the device by name in each subcommand, rejecting names that don't fit. That would fix the stale pointer in the command itself. It touches more of cmd/wdt.c, but I could add it as a separate patch before the tests in v2. What do you think?
Regards,
Juuso
Re: [PATCH 7/7] test: wdt: add tests for wdt_get_timeout(), wdt gettimeout and warning print
Hi Juuso,
Quoted 7 lines · Juuso Rinta
What do you think would be the way to go here? Since currdev is static in
cmd/wdt.c, clearing it from the test would need a small helper incmd/wdt.c. Alternatively I could also replace the static pointer with a static char currdev_name[64] and resolve the device by name in each subcommand, rejecting names that don't fit. That would fix the stale pointer in the command itself. It touches more ofcmd/wdt.c, but I could add it as a separate patch before the tests in v2. What do you think?
Yes, it's better to fix it in the command with a separate patch than to add a test-only helper.
I suggest storing the sequence number instead of the name. The nvme, scsi and virtio commands already work this way. Use 'static int currdev_seq = -1', set it from dev_seq() in 'wdt dev' and have check_currdev() call uclass_get_device_by_seq() to return the device. That way there is no fixed-size buffer and no length check. If the device has gone away, the lookup fails cleanly and you can print the existing 'use wdt dev first' message.
pmic, regulator, pinmux and tcpm have the same static-pointer pattern. You don't need to fix them in this series. For now, test/cmd/pmic.c selects the device at the start of every test, so please do the same in the new wdt tests rather than relying on state from an earlier one.
Regards,
Simon
Hi Juuso,
Quoted 4 lines · Juuso Rinta
Add an optional
get_timeoutoperation and awdt_get_timeout()helper to the watchdog uclass so drivers can report the timeout actually programmed in hardware. The requested value passed towdt_start()can differ after clamping, discrete steps, or rounding (for example power-of-two periods).
The real implementations (designware and max6370) have not been tried on hardware, so sandbox is the only coverage. At present it never reports a timeout that differs from the requested one, which is the case this series is for. See my comment on patch 6.
Regards,
Simon