Skip to contentSkip to conversation

watchdog: sbsa_gwdt: clamp WOR value to hw max

PATCH RESEND In review Juuso Rinta 2 participants 5 messages May 4 – Jun 9 Watchdog device drivers

lore
Share & tools
lore
#0 lore raw

PATCH RESEND 1/1 watchdog: sbsa_gwdt: clamp WOR value to hw max

The WOR register is 32 bits, so any tick count exceeding U32_MAX is truncated by writel(). A large requested timeout can wrap to a small value causing the watchdog to fire sooner than requested.

Clamp the calculated value to U32_MAX prior to writing the register so over-large requests will be set to the maximum timeout value.

Found by code review.

---
 drivers/watchdog/sbsa_gwdt.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/watchdog/sbsa_gwdt.c +8 -2
index 807884c5bc7..3a924cb2b9a 100644
@@ -50,6 +50,7 @@ static int sbsa_gwdt_start(struct udevice *dev, u64 timeout, ulong flags)
 {
 	struct sbsa_gwdt_priv *priv = dev_get_priv(dev);
 	u32 clk;
+	u64 tout_wdog;
 
 	/*
 	 * it work in the single stage mode in u-boot,
@@ -58,8 +59,13 @@ static int sbsa_gwdt_start(struct udevice *dev, u64 timeout, ulong flags)
 	 * to half value of timeout.
 	 */
 	clk = get_tbclk();
-	writel(clk / (2 * 1000) * timeout,
-	       priv->reg_control + SBSA_GWDT_WOR);
+
+	/* if requested timeout overflows, clamp it to u32_max */
+	tout_wdog = ((u64)clk * timeout) / (2 * 1000);
+	if (tout_wdog > U32_MAX)
+		tout_wdog = U32_MAX;
+
+	writel(tout_wdog, priv->reg_control + SBSA_GWDT_WOR);
 
 	/* writing WCS will cause an explicit watchdog refresh */
 	writel(SBSA_GWDT_WCS_EN, priv->reg_control + SBSA_GWDT_WCS);

---
base-commit: 4433253ecf2041f9362a763bb6cb79960921ac7e
change-id: 20260428-fix-sbsa-timeout-overflow-20cf84fa8a6b

Best regards,
--
Juuso Rinta <[email protected]>

Signature · 1 line
Juuso Rinta <[email protected]>
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Juuso Rinta

The WOR register is 32 bits, so any tick count exceeding U32_MAX is truncated by writel(). A large requested timeout can wrap to a small value causing the watchdog to fire sooner than requested.

Clamp the calculated value to U32_MAX prior to writing the register so over-large requests will be set to the maximum timeout value.

Found by code review.

Thanks,
Stefan

Quoted 40 lines
---
  drivers/watchdog/sbsa_gwdt.c | 10 ++++++++--
  1 file changed, 8 insertions(+), 2 deletions(-)
drivers/watchdog/sbsa_gwdt.c +8 -3
index 807884c5bc7..3a924cb2b9a 100644
@@ -50,6 +50,7 @@ static int sbsa_gwdt_start(struct udevice *dev, u64 timeout, ulong flags)
  {
  	struct sbsa_gwdt_priv *priv = dev_get_priv(dev);
  	u32 clk;
+	u64 tout_wdog;
 
  	/*
  	 * it work in the single stage mode in u-boot,
@@ -58,8 +59,13 @@ static int sbsa_gwdt_start(struct udevice *dev, u64 timeout, ulong flags)
  	 * to half value of timeout.
  	 */
  	clk = get_tbclk();
-	writel(clk / (2 * 1000) * timeout,
-	       priv->reg_control + SBSA_GWDT_WOR);
+
+	/* if requested timeout overflows, clamp it to u32_max */
+	tout_wdog = ((u64)clk * timeout) / (2 * 1000);
+	if (tout_wdog > U32_MAX)
+		tout_wdog = U32_MAX;
+
+	writel(tout_wdog, priv->reg_control + SBSA_GWDT_WOR);
 
  	/* writing WCS will cause an explicit watchdog refresh */
  	writel(SBSA_GWDT_WCS_EN, priv->reg_control + SBSA_GWDT_WCS);
 
---

base-commit: 4433253ecf2041f9362a763bb6cb79960921ac7e
change-id: 20260428-fix-sbsa-timeout-overflow-20cf84fa8a6b

Best regards,
--
Juuso Rinta <[email protected]>

author Reviewed-by Stefan Roese
#2 lore raw

Hi,

Just pinging whether this patch is all good or if something else is needed to be merged.

Thanks,
Juuso
________________________________
From: Stefan Roese <[email protected]>
Sent: Friday, 8 May 2026 14.49
To: Juuso Rinta (Nokia) <[email protected]>; [email protected] <[email protected]>
Cc: Peng Fan <[email protected]>; Tom Rini <[email protected]>; Patrice Chotard <[email protected]>; Yao Zi <[email protected]>
Subject: Re: [PATCH RESEND] watchdog: sbsa_gwdt: clamp WOR value to hw max

[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

CAUTION: This is an external email. Please be very careful when clicking links or opening attachments. See the URL nok.it/ext for additional information.

Quoted 10 lines · Juuso Rinta

The WOR register is 32 bits, so any tick count exceeding U32_MAX is truncated by writel(). A large requested timeout can wrap to a small value causing the watchdog to fire sooner than requested.

Clamp the calculated value to U32_MAX prior to writing the register so over-large requests will be set to the maximum timeout value.

Found by code review.

Thanks,
Stefan

Quoted 40 lines
---
  drivers/watchdog/sbsa_gwdt.c | 10 ++++++++--
  1 file changed, 8 insertions(+), 2 deletions(-)
drivers/watchdog/sbsa_gwdt.c +8 -3
index 807884c5bc7..3a924cb2b9a 100644
@@ -50,6 +50,7 @@ static int sbsa_gwdt_start(struct udevice *dev, u64 timeout, ulong flags)
  {
      struct sbsa_gwdt_priv *priv = dev_get_priv(dev);
      u32 clk;
+     u64 tout_wdog;
 
      /*
       * it work in the single stage mode in u-boot,
@@ -58,8 +59,13 @@ static int sbsa_gwdt_start(struct udevice *dev, u64 timeout, ulong flags)
       * to half value of timeout.
       */
      clk = get_tbclk();
-     writel(clk / (2 * 1000) * timeout,
-            priv->reg_control + SBSA_GWDT_WOR);
+
+     /* if requested timeout overflows, clamp it to u32_max */
+     tout_wdog = ((u64)clk * timeout) / (2 * 1000);
+     if (tout_wdog > U32_MAX)
+             tout_wdog = U32_MAX;
+
+     writel(tout_wdog, priv->reg_control + SBSA_GWDT_WOR);
 
      /* writing WCS will cause an explicit watchdog refresh */
      writel(SBSA_GWDT_WCS_EN, priv->reg_control + SBSA_GWDT_WCS);
 
---

base-commit: 4433253ecf2041f9362a763bb6cb79960921ac7e
change-id: 20260428-fix-sbsa-timeout-overflow-20cf84fa8a6b

Best regards,
--
Juuso Rinta <[email protected]>

#3 lore raw
In reply to Juuso Rinta
Quoted 10 lines · Juuso Rinta

The WOR register is 32 bits, so any tick count exceeding U32_MAX is truncated by writel(). A large requested timeout can wrap to a small value causing the watchdog to fire sooner than requested.

Clamp the calculated value to U32_MAX prior to writing the register so over-large requests will be set to the maximum timeout value.

Found by code review.

Applied to u-boot-watchdog/next

Thanks,
Stefan

#4 lore raw
In reply to Juuso Rinta
Quoted 10 lines · Juuso Rinta

The WOR register is 32 bits, so any tick count exceeding U32_MAX is truncated by writel(). A large requested timeout can wrap to a small value causing the watchdog to fire sooner than requested.

Clamp the calculated value to U32_MAX prior to writing the register so over-large requests will be set to the maximum timeout value.

Found by code review.

Applied to u-boot-watchdog/next

Thanks,
Stefan