VIA/WonderMedia system timer IP can generate a watchdog reset when its
clocksource counter matches the value in the match register 0 and
watchdog function is enabled. For this to work, obvously the clock event
device must use a different match register (1~3) and respective interrupt.
Check if at least two interrupts are provided by the device tree, then use
match register 1 for system clock events and match register 0 for watchdog
respectively.
Signed-off-by: Alexey Charkov <alchark@gmail.com>
---
drivers/clocksource/Kconfig | 11 +++++++
drivers/clocksource/timer-vt8500.c | 61 ++++++++++++++++++++++++++++++++++----
2 files changed, 67 insertions(+), 5 deletions(-)
diff --git a/drivers/clocksource/Kconfig b/drivers/clocksource/Kconfig
index 487c8525996724fbf9c6e9726dabb478d86513b9..8f5e41ff23386d9ecb46b38603dae485db71cfc7 100644
--- a/drivers/clocksource/Kconfig
+++ b/drivers/clocksource/Kconfig
@@ -181,6 +181,17 @@ config VT8500_TIMER
help
Enables support for the VT8500 driver.
+config VT8500_TIMER_WATCHDOG
+ bool "Enable VT8500 watchdog functionality"
+ depends on VT8500_TIMER
+ depends on WATCHDOG && WATCHDOG_CORE=y
+ help
+ VIA/WonderMedia SoCs can use their system timer as a hardware
+ watchdog, as long as the first timer channel is free from other
+ uses and respective function is enabled in its registers. To
+ make use of it, say Y here and ensure that the device tree
+ lists at least two interrupts for the VT8500 timer device
+
config NPCM7XX_TIMER
bool "NPCM7xx timer driver" if COMPILE_TEST
depends on HAS_IOMEM
diff --git a/drivers/clocksource/timer-vt8500.c b/drivers/clocksource/timer-vt8500.c
index 9f28f30dcaf83ab4e9c89952175b0d4c75bd6b40..4128c4f19adc2e02b6df2d0aa2dca4659f62fba7 100644
--- a/drivers/clocksource/timer-vt8500.c
+++ b/drivers/clocksource/timer-vt8500.c
@@ -22,6 +22,8 @@
#include <linux/of_address.h>
#include <linux/of_irq.h>
+#include <linux/watchdog.h>
+
#define VT8500_TIMER_OFFSET 0x0100
#define VT8500_TIMER_HZ 3000000
@@ -55,6 +57,9 @@
#define MIN_OSCR_DELTA 16
static void __iomem *regbase;
+static unsigned int sys_timer_ch; /* which match register to use
+ * for the system timer
+ */
static u64 vt8500_timer_read(struct clocksource *cs)
{
@@ -81,15 +86,15 @@ static int vt8500_timer_set_next_event(unsigned long cycles,
int loops = msecs_to_loops(10);
u64 alarm = clocksource.read(&clocksource) + cycles;
- while (readl(regbase + TIMER_ACC_STS_REG) & TIMER_ACC_WR_MATCH(0)
+ while (readl(regbase + TIMER_ACC_STS_REG) & TIMER_ACC_WR_MATCH(sys_timer_ch)
&& --loops)
cpu_relax();
- writel((unsigned long)alarm, regbase + TIMER_MATCH_REG(0));
+ writel((unsigned long)alarm, regbase + TIMER_MATCH_REG(sys_timer_ch));
if ((signed)(alarm - clocksource.read(&clocksource)) <= MIN_OSCR_DELTA)
return -ETIME;
- writel(TIMER_INT_EN_MATCH(0), regbase + TIMER_INT_EN_REG);
+ writel(TIMER_INT_EN_MATCH(sys_timer_ch), regbase + TIMER_INT_EN_REG);
return 0;
}
@@ -120,6 +125,40 @@ static irqreturn_t vt8500_timer_interrupt(int irq, void *dev_id)
return IRQ_HANDLED;
}
+static int vt8500_watchdog_start(struct watchdog_device *wdd)
+{
+ u64 cycles = wdd->timeout * VT8500_TIMER_HZ;
+ u64 deadline = clocksource.read(&clocksource) + cycles;
+
+ writel((u32)deadline, regbase + TIMER_MATCH_REG(0));
+ writel(TIMER_WD_EN, regbase + TIMER_WATCHDOG_EN_REG);
+ return 0;
+}
+
+static int vt8500_watchdog_stop(struct watchdog_device *wdd)
+{
+ writel(0, regbase + TIMER_WATCHDOG_EN_REG);
+ return 0;
+}
+
+static const struct watchdog_ops vt8500_watchdog_ops = {
+ .start = vt8500_watchdog_start,
+ .stop = vt8500_watchdog_stop,
+};
+
+static const struct watchdog_info vt8500_watchdog_info = {
+ .identity = "VIA VT8500 watchdog",
+ .options = WDIOF_MAGICCLOSE |
+ WDIOF_KEEPALIVEPING |
+ WDIOF_SETTIMEOUT,
+};
+
+static struct watchdog_device vt8500_watchdog_device = {
+ .ops = &vt8500_watchdog_ops,
+ .info = &vt8500_watchdog_info,
+ .max_hw_heartbeat_ms = -1UL / (VT8500_TIMER_HZ / 1000),
+};
+
static int __init vt8500_timer_init(struct device_node *np)
{
int timer_irq, ret;
@@ -131,7 +170,9 @@ static int __init vt8500_timer_init(struct device_node *np)
return -ENXIO;
}
- timer_irq = irq_of_parse_and_map(np, 0);
+ sys_timer_ch = of_irq_count(np) > 1 ? 1 : 0;
+
+ timer_irq = irq_of_parse_and_map(np, sys_timer_ch);
if (!timer_irq) {
pr_err("%s: Missing irq description in Device Tree\n",
__func__);
@@ -140,7 +181,7 @@ static int __init vt8500_timer_init(struct device_node *np)
writel(TIMER_CTRL_ENABLE, regbase + TIMER_CTRL_REG);
writel(TIMER_STATUS_CLEARALL, regbase + TIMER_STATUS_REG);
- writel(~0, regbase + TIMER_MATCH_REG(0));
+ writel(~0, regbase + TIMER_MATCH_REG(sys_timer_ch));
ret = clocksource_register_hz(&clocksource, VT8500_TIMER_HZ);
if (ret) {
@@ -163,6 +204,16 @@ static int __init vt8500_timer_init(struct device_node *np)
clockevents_config_and_register(&clockevent, VT8500_TIMER_HZ,
MIN_OSCR_DELTA * 2, 0xf0000000);
+ if (!IS_ENABLED(CONFIG_VT8500_TIMER_WATCHDOG))
+ return 0;
+
+ if (!sys_timer_ch) {
+ pr_info("%s: Not enabling watchdog: only one irq was given\n",
+ __func__);
+ } else {
+ watchdog_register_device(&vt8500_watchdog_device);
+ }
+
return 0;
}
--
2.49.0
On Wed, May 07, 2025 at 10:58:32AM +0400, Alexey Charkov wrote: > VIA/WonderMedia system timer IP can generate a watchdog reset when its > clocksource counter matches the value in the match register 0 and > watchdog function is enabled. For this to work, obvously the clock event > device must use a different match register (1~3) and respective interrupt. > > Check if at least two interrupts are provided by the device tree, then use > match register 1 for system clock events and match register 0 for watchdog > respectively. This code falls under the watchdog umbrella not in the clocksource. It is better to find a way to make the timer and the watchdog separated. The timer-gxp.c is dynamically allocating a watchdog platform device with the shared pointer to the timer counter. IMO, it is a good example to split this up. > Signed-off-by: Alexey Charkov <alchark@gmail.com> > --- > drivers/clocksource/Kconfig | 11 +++++++ > drivers/clocksource/timer-vt8500.c | 61 ++++++++++++++++++++++++++++++++++---- > 2 files changed, 67 insertions(+), 5 deletions(-) > > diff --git a/drivers/clocksource/Kconfig b/drivers/clocksource/Kconfig > index 487c8525996724fbf9c6e9726dabb478d86513b9..8f5e41ff23386d9ecb46b38603dae485db71cfc7 100644 > --- a/drivers/clocksource/Kconfig > +++ b/drivers/clocksource/Kconfig > @@ -181,6 +181,17 @@ config VT8500_TIMER > help > Enables support for the VT8500 driver. > > +config VT8500_TIMER_WATCHDOG > + bool "Enable VT8500 watchdog functionality" > + depends on VT8500_TIMER > + depends on WATCHDOG && WATCHDOG_CORE=y if WATCHDOG_CORE=y then WATCHDOG=y because the first one can be enabled only if the second one is enabled. [ ... ] -- <http://www.linaro.org/> Linaro.org │ Open source software for ARM SoCs Follow Linaro: <http://www.facebook.com/pages/Linaro> Facebook | <http://twitter.com/#!/linaroorg> Twitter | <http://www.linaro.org/linaro-blog/> Blog
On Tue, May 13, 2025 at 5:40 PM Daniel Lezcano <daniel.lezcano@linaro.org> wrote: > > On Wed, May 07, 2025 at 10:58:32AM +0400, Alexey Charkov wrote: > > VIA/WonderMedia system timer IP can generate a watchdog reset when its > > clocksource counter matches the value in the match register 0 and > > watchdog function is enabled. For this to work, obvously the clock event > > device must use a different match register (1~3) and respective interrupt. > > > > Check if at least two interrupts are provided by the device tree, then use > > match register 1 for system clock events and match register 0 for watchdog > > respectively. > > This code falls under the watchdog umbrella not in the clocksource. It > is better to find a way to make the timer and the watchdog separated. > > The timer-gxp.c is dynamically allocating a watchdog platform device > with the shared pointer to the timer counter. IMO, it is a good > example to split this up. Thanks for the pointer Daniel! I guess in this case I'll need to pass the pointer to a full clocksource read function as platform data for the watchdog, as it's a bit more messy here than timer-gxp.c in that this hardware cannot do an atomic MMIO read of the counter and needs a synchronization request to be issued and cleared before reading the value. The clocksource code will then only instantiate a platform device for the watchdog when it can ensure that nothing uses the first match register. > > Signed-off-by: Alexey Charkov <alchark@gmail.com> > > --- > > drivers/clocksource/Kconfig | 11 +++++++ > > drivers/clocksource/timer-vt8500.c | 61 ++++++++++++++++++++++++++++++++++---- > > 2 files changed, 67 insertions(+), 5 deletions(-) > > > > diff --git a/drivers/clocksource/Kconfig b/drivers/clocksource/Kconfig > > index 487c8525996724fbf9c6e9726dabb478d86513b9..8f5e41ff23386d9ecb46b38603dae485db71cfc7 100644 > > --- a/drivers/clocksource/Kconfig > > +++ b/drivers/clocksource/Kconfig > > @@ -181,6 +181,17 @@ config VT8500_TIMER > > help > > Enables support for the VT8500 driver. > > > > +config VT8500_TIMER_WATCHDOG > > + bool "Enable VT8500 watchdog functionality" > > + depends on VT8500_TIMER > > + depends on WATCHDOG && WATCHDOG_CORE=y > > if WATCHDOG_CORE=y then WATCHDOG=y because the first one can be > enabled only if the second one is enabled. Noted, thanks. Will replace it with just WATCHDOG_CORE=y in the next version. In fact, if the watchdog functionality becomes a separate platform driver then it could also potentially be built as a module, so "=y" might also go. Best regards, Alexey
© 2016 - 2025 Red Hat, Inc.