Add a SANDBOX_WDT_MAX_TIMEOUT_MS define to sandbox asm/state.h
to emulate a hardware-limited maximum timeout.

Clamp the timeout_ms in sandbox_wdt_start() to the defined max
timeout value for testability.

Report the maximum timeout value to the wdt uclass by setting
max_timeout_ms in the per-device uclass-plat data during
sandbox_wdt_probe().

Signed-off-by: Juuso Rinta <[email protected]>
---
 arch/sandbox/include/asm/state.h |  2 ++
 drivers/watchdog/sandbox_wdt.c   | 15 +++++++++++++++
 2 files changed, 17 insertions(+)

diff --git a/arch/sandbox/include/asm/state.h b/arch/sandbox/include/asm/state.h
index 9dea0980bfc..742e4f5c6e9 100644
--- a/arch/sandbox/include/asm/state.h
+++ b/arch/sandbox/include/asm/state.h
@@ -38,6 +38,8 @@ struct sandbox_spi_info {
        struct udevice *emul;
 };
 
+#define SANDBOX_WDT_MAX_TIMEOUT_MS     30000
+
 struct sandbox_wdt_info {
        unsigned long long counter;
        uint reset_count;
diff --git a/drivers/watchdog/sandbox_wdt.c b/drivers/watchdog/sandbox_wdt.c
index cd5eadbfd21..d64dd154c70 100644
--- a/drivers/watchdog/sandbox_wdt.c
+++ b/drivers/watchdog/sandbox_wdt.c
@@ -11,6 +11,10 @@ static int sandbox_wdt_start(struct udevice *dev, u64 
timeout, ulong flags)
 {
        struct sandbox_state *state = state_get_current();
 
+       /* Clamp to emulated hardware maximum for testability */
+       if (timeout > SANDBOX_WDT_MAX_TIMEOUT_MS)
+               timeout = SANDBOX_WDT_MAX_TIMEOUT_MS;
+
        state->wdt.counter = timeout;
        state->wdt.running = true;
 
@@ -43,6 +47,16 @@ static int sandbox_wdt_expire_now(struct udevice *dev, ulong 
flags)
        return 0;
 }
 
+static int sandbox_wdt_probe(struct udevice *dev)
+{
+       struct wdt_uc_plat *plat = dev_get_uclass_plat(dev);
+
+       /* Store the emulated hardware max timeout in uclass plat data */
+       plat->max_timeout_ms = SANDBOX_WDT_MAX_TIMEOUT_MS;
+
+       return 0;
+}
+
 static const struct wdt_ops sandbox_wdt_ops = {
        .start = sandbox_wdt_start,
        .reset = sandbox_wdt_reset,
@@ -59,5 +73,6 @@ U_BOOT_DRIVER(wdt_sandbox) = {
        .name = "wdt_sandbox",
        .id = UCLASS_WDT,
        .of_match = sandbox_wdt_ids,
+       .probe = sandbox_wdt_probe,
        .ops = &sandbox_wdt_ops,
 };

-- 
2.39.2

Reply via email to