[PATCH v7 2/4] clocksource/drivers/timer-vt8500: Prepare for watchdog functionality

From: Alexey Charkov

Date: Fri Nov 13 2026 - 10:03:05 EST


VIA/WonderMedia system timer 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, obviously 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 reserve match register 0 for
the watchdog. Keep match register 0 for clock events on device trees that
list a single interrupt, and offer no watchdog there.

Instantiate the watchdog as an auxiliary device of the timer from a
platform driver bound to the timer node, as the timer itself comes up too
early to have a struct device. Share what the watchdog needs through a new
header, include/clocksource/vt8500-timer.h.

Unmap the registers if the timer interrupt is missing, so that no watchdog
gets created on a timer whose counter never started.

Signed-off-by: Alexey Charkov <alchark@xxxxxxxxx>
---
MAINTAINERS | 1 +
drivers/clocksource/Kconfig | 1 +
drivers/clocksource/timer-vt8500.c | 127 ++++++++++++++++++++++++++++++++++---
include/clocksource/vt8500-timer.h | 22 +++++++
4 files changed, 142 insertions(+), 9 deletions(-)

diff --git a/MAINTAINERS b/MAINTAINERS
index 7070cf5dbfe9..e8c2b1101b68 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -4010,6 +4010,7 @@ F: drivers/tty/serial/vt8500_serial.c
F: drivers/video/fbdev/vt8500lcdfb.*
F: drivers/video/fbdev/wm8505fb*
F: drivers/video/fbdev/wmt_ge_rops.*
+F: include/clocksource/vt8500-timer.h

ARM/ZTE ZX29 SOC SUPPORT
M: Stefan Dösinger <stefandoesinger@xxxxxxxxx>
diff --git a/drivers/clocksource/Kconfig b/drivers/clocksource/Kconfig
index da83483296d5..d060863fcc07 100644
--- a/drivers/clocksource/Kconfig
+++ b/drivers/clocksource/Kconfig
@@ -197,6 +197,7 @@ config TEGRA186_TIMER
config VT8500_TIMER
bool "VT8500 timer driver" if COMPILE_TEST
depends on HAS_IOMEM
+ select AUXILIARY_BUS
help
Enables support for the VT8500 driver.

diff --git a/drivers/clocksource/timer-vt8500.c b/drivers/clocksource/timer-vt8500.c
index c4694cf8bea6..b54ed5d692d7 100644
--- a/drivers/clocksource/timer-vt8500.c
+++ b/drivers/clocksource/timer-vt8500.c
@@ -11,6 +11,7 @@
* Alexey Charkov. Minor changes have been made for Device Tree Support.
*/

+#include <linux/auxiliary_bus.h>
#include <linux/io.h>
#include <linux/irq.h>
#include <linux/interrupt.h>
@@ -21,9 +22,9 @@
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_irq.h>
+#include <linux/platform_device.h>

-#define VT8500_TIMER_OFFSET 0x0100
-#define VT8500_TIMER_HZ 3000000
+#include <clocksource/vt8500-timer.h>

#define TIMER_MATCH_REG(x) (4 * (x))
#define TIMER_COUNT_REG 0x0010 /* clocksource counter */
@@ -53,6 +54,9 @@

static void __iomem *regbase;

+/* Which match register to use for the system timer */
+static unsigned int sys_timer_ch;
+
static u64 vt8500_timer_read(struct clocksource *cs)
{
int loops = msecs_to_loops(10);
@@ -72,21 +76,26 @@ static struct clocksource clocksource = {
.flags = CLOCK_SOURCE_IS_CONTINUOUS,
};

+static u64 vt8500_timer_next(u64 cycles)
+{
+ return clocksource.read(&clocksource) + cycles;
+}
+
static int vt8500_timer_set_next_event(unsigned long cycles,
struct clock_event_device *evt)
{
int loops = msecs_to_loops(10);
- u64 alarm = clocksource.read(&clocksource) + cycles;
+ u64 alarm = vt8500_timer_next(cycles);

- while (readl(regbase + TIMER_ACC_STS_REG) & TIMER_ACC_WR_MATCH(0) &&
- --loops)
+ 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;
}
@@ -128,16 +137,29 @@ static int __init vt8500_timer_init(struct device_node *np)
return -ENXIO;
}

- timer_irq = irq_of_parse_and_map(np, 0);
+ /*
+ * The watchdog function is available on channel 0, but not on other
+ * channels. So to use it, 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 reserve match
+ * register 0 for the watchdog.
+ */
+ 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__);
+ iounmap(regbase);
+ regbase = NULL;
return -EINVAL;
}

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,4 +185,91 @@ static int __init vt8500_timer_init(struct device_node *np)
return 0;
}

+static void vt8500_timer_aux_uninit(void *data)
+{
+ auxiliary_device_uninit(data);
+}
+
+static void vt8500_timer_aux_delete(void *data)
+{
+ auxiliary_device_delete(data);
+}
+
+static void vt8500_timer_aux_release(struct device *dev)
+{
+ struct vt8500_wdt_info *wdt_info;
+ struct auxiliary_device *auxdev;
+
+ auxdev = container_of(dev, struct auxiliary_device, dev);
+ wdt_info = container_of(auxdev, struct vt8500_wdt_info, auxdev);
+ kfree(wdt_info);
+}
+
+/*
+ * This probe gets called after vt8500_timer_init(), which leaves regbase set
+ * only once the clocksource counter is running: that counter and match
+ * register 0 are all the watchdog needs, even if registering the clocksource
+ * or the clock event device failed afterwards. Create the watchdog device as
+ * a child, since the registers are shared.
+ */
+static int vt8500_timer_probe(struct platform_device *pdev)
+{
+ struct vt8500_wdt_info *wdt_info;
+ struct device *dev = &pdev->dev;
+ int ret;
+
+ if (!regbase)
+ return dev_err_probe(dev, -ENODEV,
+ "Timer not initialized, cannot create watchdog\n");
+
+ if (!sys_timer_ch) {
+ dev_info(dev, "Not enabling watchdog: only one irq was given\n");
+ return 0;
+ }
+
+ wdt_info = kzalloc_obj(*wdt_info);
+ if (!wdt_info)
+ return -ENOMEM;
+
+ wdt_info->timer_next = &vt8500_timer_next;
+ wdt_info->regbase = regbase;
+ wdt_info->auxdev.name = "vt8500-wdt";
+ wdt_info->auxdev.dev.parent = dev;
+ wdt_info->auxdev.dev.release = &vt8500_timer_aux_release;
+
+ ret = auxiliary_device_init(&wdt_info->auxdev);
+ if (ret) {
+ kfree(wdt_info);
+ return ret;
+ }
+
+ ret = devm_add_action_or_reset(dev, vt8500_timer_aux_uninit,
+ &wdt_info->auxdev);
+ if (ret)
+ return ret;
+
+ ret = auxiliary_device_add(&wdt_info->auxdev);
+ if (ret)
+ return ret;
+
+ return devm_add_action_or_reset(dev, vt8500_timer_aux_delete,
+ &wdt_info->auxdev);
+}
+
+static const struct of_device_id vt8500_timer_of_match[] = {
+ { .compatible = "via,vt8500-timer", },
+ {},
+};
+
+static struct platform_driver vt8500_timer_driver = {
+ .probe = vt8500_timer_probe,
+ .driver = {
+ .name = "vt8500-timer",
+ .of_match_table = vt8500_timer_of_match,
+ .suppress_bind_attrs = true,
+ },
+};
+
+builtin_platform_driver(vt8500_timer_driver);
+
TIMER_OF_DECLARE(vt8500, "via,vt8500-timer", vt8500_timer_init);
diff --git a/include/clocksource/vt8500-timer.h b/include/clocksource/vt8500-timer.h
new file mode 100644
index 000000000000..956863d50d02
--- /dev/null
+++ b/include/clocksource/vt8500-timer.h
@@ -0,0 +1,22 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef CLOCKSOURCE_VT8500_TIMER_H_
+#define CLOCKSOURCE_VT8500_TIMER_H_
+
+#include <linux/auxiliary_bus.h>
+#include <linux/types.h>
+
+#define VT8500_TIMER_HZ 3000000
+
+/**
+ * struct vt8500_wdt_info - timer resources shared with the watchdog
+ * @auxdev: auxiliary device the watchdog driver binds to
+ * @timer_next: returns the clocksource counter value @cycles from now
+ * @regbase: timer register block; the watchdog uses match register 0
+ */
+struct vt8500_wdt_info {
+ struct auxiliary_device auxdev;
+ u64 (*timer_next)(u64 cycles);
+ void __iomem *regbase;
+};
+
+#endif /* CLOCKSOURCE_VT8500_TIMER_H_ */

--
2.56.0