Skip to contentSkip to conversation

wdt: Add support for watchdogs on Kendryte K210

PATCH Superseded Sean Anderson 1 participant 8 messages Mar 11, 2021 – Apr 2, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v6 cover wdt: Add support for watchdogs on Kendryte K210

Please merge! I don't want to have to keep rebasing.

Changes in v6:
- Rebase on u-boot/master

Changes in v5:
- Note dependency on "time: Fix get_ticks being non-monotonic"
- Add a few signed-off-bys which were sent for version 1
- Rebase on u-boot/master

Changes in v4:
- Fix build error without CONFIG_CLK

Changes in v2:
- Fix fls being off-by-one when compared to log_2_n_round_up
- Move watchdog enable to k210.dtsi as it does not depend on anything board-specific.

Sean Anderson (6):
wdt: dw: Switch to using fls for log2
wdt: dw: Switch to if(CONFIG()) instead of using #if
wdt: dw: Enable the clock before using it
wdt: dw: Free the clock on error
riscv: Add watchdog bindings for the k210
riscv: Enable watchdog for the k210

 arch/riscv/dts/k210.dtsi          |  1 -
 board/sipeed/maix/Kconfig         |  2 ++
 drivers/watchdog/designware_wdt.c | 37 ++++++++++++++++++++-----------
 3 files changed, 26 insertions(+), 14 deletions(-)
Signature · 1 line
2.30.1
#1 lore raw

PATCH v6 1/6 wdt: dw: Switch to using fls for log2

log_2_n_round_up is only found in arm. fls performs the same job and is generic.

---

(no changes since v2)

Changes in v2:
- Fix fls being off-by-one when compared to log_2_n_round_up

 drivers/watchdog/designware_wdt.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/watchdog/designware_wdt.c +1 -2
index c020324973..1f0c5a77c9 100644
@@ -9,7 +9,6 @@
 #include <reset.h>
 #include <wdt.h>
 #include <asm/io.h>
-#include <asm/utils.h>
 #include <linux/bitops.h>
 
 #define DW_WDT_CR	0x00
@@ -35,7 +34,7 @@ static int designware_wdt_settimeout(void __iomem *base, unsigned int clk_khz,
 	signed int i;
 
 	/* calculate the timeout range value */
-	i = log_2_n_round_up(timeout * clk_khz) - 16;
+	i = fls(timeout * clk_khz - 1) - 16;
 	i = clamp(i, 0, 15);
 
 	writel(i | (i << 4), base + DW_WDT_TORR);
Signature · 1 line
2.30.1
#2 lore raw

PATCH v6 2/6 wdt: dw: Switch to if(CONFIG()) instead of using #if

In reply to Sean Anderson

This is preferred over #if because the compiler can check syntax even if the feature is disabled. This cannot be used for CONFIG_CLK because CONFIG_DW_WDT_CLOCK_KHZ is not defined on all platforms.

---

(no changes since v1)

 drivers/watchdog/designware_wdt.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
drivers/watchdog/designware_wdt.c +9 -9
index 1f0c5a77c9..a65487d4f3 100644
@@ -136,17 +136,17 @@ static int designware_wdt_probe(struct udevice *dev)
 	priv->clk_khz = CONFIG_DW_WDT_CLOCK_KHZ;
 #endif
 
-#if CONFIG_IS_ENABLED(DM_RESET)
-	struct reset_ctl_bulk resets;
+	if (CONFIG_IS_ENABLED(DM_RESET)) {
+		struct reset_ctl_bulk resets;
 
-	ret = reset_get_bulk(dev, &resets);
-	if (ret)
-		return ret;
+		ret = reset_get_bulk(dev, &resets);
+		if (ret)
+			return ret;
 
-	ret = reset_deassert_bulk(&resets);
-	if (ret)
-		return ret;
-#endif
+		ret = reset_deassert_bulk(&resets);
+		if (ret)
+			return ret;
+	}
 
 	/* reset to disable the watchdog */
 	return designware_wdt_stop(dev);
Signature · 1 line
2.30.1
#3 lore raw

PATCH v6 3/6 wdt: dw: Enable the clock before using it

In reply to Sean Anderson

The watchdog won't work if the clock isn't enabled.

Fixes cf89ef8d10f240554541c20b2e1bdcdd58d1d7e6

---

(no changes since v1)

 drivers/watchdog/designware_wdt.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/watchdog/designware_wdt.c +4 -0
index a65487d4f3..709a67fbc3 100644
@@ -129,6 +129,10 @@ static int designware_wdt_probe(struct udevice *dev)
 	if (ret)
 		return ret;
 
+	ret = clk_enable(&clk);
+	if (ret)
+		return ret;
+
 	priv->clk_khz = clk_get_rate(&clk) / 1000;
 	if (!priv->clk_khz)
 		return -EINVAL;
Signature · 1 line
2.30.1
#4 lore raw

PATCH v6 4/6 wdt: dw: Free the clock on error

In reply to Sean Anderson

The clock subsystem requires that clk_free be called on clocks obtained via clk_get_*.

---

(no changes since v4)

Changes in v4:
- Fix build error without CONFIG_CLK

 drivers/watchdog/designware_wdt.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)
drivers/watchdog/designware_wdt.c +13 -5
index 709a67fbc3..9e5487168c 100644
@@ -131,11 +131,13 @@ static int designware_wdt_probe(struct udevice *dev)
 
 	ret = clk_enable(&clk);
 	if (ret)
-		return ret;
+		goto err;
 
 	priv->clk_khz = clk_get_rate(&clk) / 1000;
-	if (!priv->clk_khz)
-		return -EINVAL;
+	if (!priv->clk_khz) {
+		ret = -EINVAL;
+		goto err;
+	}
 #else
 	priv->clk_khz = CONFIG_DW_WDT_CLOCK_KHZ;
 #endif
@@ -145,15 +147,21 @@ static int designware_wdt_probe(struct udevice *dev)
 
 		ret = reset_get_bulk(dev, &resets);
 		if (ret)
-			return ret;
+			goto err;
 
 		ret = reset_deassert_bulk(&resets);
 		if (ret)
-			return ret;
+			goto err;
 	}
 
 	/* reset to disable the watchdog */
 	return designware_wdt_stop(dev);
+
+err:
+#if CONFIG_IS_ENABLED(CLK)
+	clk_free(&clk);
+#endif
+	return ret;
 }
 
 static const struct wdt_ops designware_wdt_ops = {
Signature · 1 line
2.30.1
#5 lore raw

PATCH v6 5/6 riscv: Add watchdog bindings for the k210

In reply to Sean Anderson

This adds the necessary bindings. Most of them are already there.

---

(no changes since v2)

Changes in v2:
- Move watchdog enable to k210.dtsi as it does not depend on anything board-specific.

 arch/riscv/dts/k210.dtsi | 1 -
 1 file changed, 1 deletion(-)
arch/riscv/dts/k210.dtsi +0 -1
index 81b04018c6..0b79a29600 100644
@@ -439,7 +439,6 @@
 				interrupts = <21>;
 				clocks = <&sysclk K210_CLK_WDT0>;
 				resets = <&sysrst K210_RST_WDT0>;
-				status = "disabled";
 			};
 
 			wdt1: watchdog at 50410000 {
Signature · 1 line
2.30.1
#6 lore raw

PATCH v6 6/6 riscv: Enable watchdog for the k210

In reply to Sean Anderson

This enables the necessary config options.

---

(no changes since v1)

 board/sipeed/maix/Kconfig | 2 ++
 1 file changed, 2 insertions(+)
board/sipeed/maix/Kconfig +2 -0
index 2cdea8ea81..adf6abb572 100644
@@ -69,4 +69,6 @@ config BOARD_SPECIFIC_OPTIONS
 	imply EFI_PARTITION
 	imply CMD_PART
 	imply CMD_FS_GENERIC
+	imply WDT
+	imply DESIGNWARE_WATCHDOG
 endif
Signature · 1 line
2.30.1
#7 lore raw

v6 cover wdt: Add support for watchdogs on Kendryte K210

In reply to Sean Anderson
Quoted 27 lines · Sean Anderson

Please merge! I don't want to have to keep rebasing.

Changes in v6:
- Rebase on u-boot/master

Changes in v5:
- Note dependency on "time: Fix get_ticks being non-monotonic"
- Add a few signed-off-bys which were sent for version 1
- Rebase on u-boot/master

Changes in v4:
- Fix build error without CONFIG_CLK

Changes in v2:
- Fix fls being off-by-one when compared to log_2_n_round_up
- Move watchdog enable to k210.dtsi as it does not depend on anything board-specific.

Sean Anderson (6):
wdt: dw: Switch to using fls for log2
wdt: dw: Switch to if(CONFIG()) instead of using #if
wdt: dw: Enable the clock before using it
wdt: dw: Free the clock on error
riscv: Add watchdog bindings for the k210
riscv: Enable watchdog for the k210

Quoted 5 lines
  arch/riscv/dts/k210.dtsi          |  1 -
  board/sipeed/maix/Kconfig         |  2 ++
  drivers/watchdog/designware_wdt.c | 37 ++++++++++++++++++++-----------
  3 files changed, 26 insertions(+), 14 deletions(-)

Rick, can you pick this up for v2021.07? I would like to note that there have been no functional changes since v4, which I sent last September.

--Sean