clocksource/drivers/timer-vt8500: clean up and add watchdog function
Subsystems (4)
Alexey Charkov
maintainerv7 cover clocksource/drivers/timer-vt8500: clean up and add watchdog function
Add named defines for the registers and bits in timer-vt8500. Move the system events timer from channel 0 to channel 1 when enough information is provided by the device tree (i.e. more than one IRQ). Use channel 0 for the system watchdog, and let the watchdog restart the SoC, which the machine's own restart hook fails to do on WM8950.
---
Changes in v7:
- Rebased onto next-20261112
- Register the restart handler at priority 0, the last resort, so that
it runs after the machine's own restart hook regardless of
registration order (thanks Guenter)
- Wait for the reset to assert after arming the watchdog in the restart
handler, rather than return and let later handlers run (thanks Guenter)
- Link to v6: https://patch.msgid.link/[email protected]
Changes in v6:
- Dropped the binding patch, which Rob has applied
- Rebased onto next-20261111
- Moved the shared header under include/clocksource (thanks Daniel)
- Pass the watchdog a single register base, and keep the watchdog
register offsets in its own driver (thanks Daniel)
- Build the watchdog driver in rather than as a module: the timer it
depends on is always built in, and the driver is tiny (thanks Daniel)
- kfree the correct structure upon aux child release (thanks Daniel)
- Return -ENODEV rather than -ENOMEM if the timer failed to initialize,
and drop the message on allocation failure (thanks Daniel)
- Unmap the timer registers if its interrupt is missing, so that no
watchdog gets created on a timer that never started
- Improved code comments and commit messages (thanks Daniel)
- Give the watchdog a default timeout, so that opening the device
before setting one no longer arms it for the next counter wraparound
- Wait for a pending match register write to settle before rewriting it,
as the clock event code does
- Honour the nowayout parameter, and disallow unbinding the watchdog,
which would leave it armed with nothing to ping it
- Added a restart handler, so that reboot works on WM8950
- Link to v5: https://lore.kernel.org/r/[email protected]
Changes in v5:
- Make sure the watchdog timeout value is clamped to hardware limits
before writing it to the registers (thanks Guenter)
- Add a comment about the watchdog match value (deadline) wrapping around
the U32_MAX boundary, which is expected behavior (thanks Guenter)
- Avoid moving register definitions to the shared header file to reduce
churn: the watchdog driver gets direct pointers to the two registers it
uses anyway, so it doesn't have to know all those offsets and bits
- Put the watchdog Kconfig entry with other ARM platforms where it belongs
- Link to v4: https://lore.kernel.org/r/[email protected]
Changes in v4:
- Changed the watchdog driver to use auxiliary bus instead of platform
(thanks Guenter)
- Split out the register definitions into a header file shared between
the clocksource driver and the watchdog driver
- Replaced -1UL with U32_MAX in watchdog max_heartbeat_ms to make builds
on non-32bit platforms happier
- Link to v3: https://lore.kernel.org/r/[email protected]
Changes in v3:
- Dropped the DTS patch already applied by Krzysztof
- Rebased onto v6.15-rc5 as requested by Daniel
- Split out the watchdog code into a dedicated platform driver, like
timer-gxp does (thanks Daniel)
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Included the previously reviewed binding change that is directly related
to this series as the first patch here (thanks Krzysztof)
- Created a separate config symbol for the watchdog function to let users
build a kernel without forcing watchdog functionality upon them
(thanks Krzysztof)
- Link to the previous binding submission: https://lore.kernel.org/all/[email protected]/
---
Alexey Charkov (4):
clocksource/drivers/timer-vt8500: Add defines for magic constants clocksource/drivers/timer-vt8500: Prepare for watchdog functionality
watchdog: Add support for VIA/WonderMedia SoC watchdog functionality
watchdog: vt8500: Add a restart handler
Documentation/watchdog/watchdog-parameters.rst | 7 + MAINTAINERS | 2 + drivers/clocksource/Kconfig | 1 + drivers/clocksource/timer-vt8500.c | 179 +++++++++++++++++++++---- drivers/watchdog/Kconfig | 12 ++ drivers/watchdog/Makefile | 1 + drivers/watchdog/vt8500-wdt.c | 150 +++++++++++++++++++++ include/clocksource/vt8500-timer.h | 22 +++ 8 files changed, 347 insertions(+), 27 deletions(-)
---
base-commit: 6d582d78fad9d828e575ca0cdcff8ea47a77220b
change-id: 20250506-vt8500-timer-updates-44a0d22cd720
Best regards,
Signature · 1 line
Alexey Charkov <[email protected]>
Alexey Charkov
maintainerPATCH v7 1/4 clocksource/drivers/timer-vt8500: Add defines for magic constants
Add defines for the timer registers and their bits to make the code more self-explanatory. While at that, replace _VAL suffixes with more intuitive _REG suffixes on register offsets.
No functional changes.
--- drivers/clocksource/timer-vt8500.c | 62 ++++++++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 23 deletions(-)
drivers/clocksource/timer-vt8500.c +39 -23
@@ -24,15 +24,28 @@#define VT8500_TIMER_OFFSET 0x0100#define VT8500_TIMER_HZ 3000000 -#define TIMER_MATCH_VAL 0x0000 -#define TIMER_COUNT_VAL 0x0010 -#define TIMER_STATUS_VAL 0x0014 -#define TIMER_IER_VAL 0x001c/* interrupt enable */ -#define TIMER_CTRL_VAL 0x0020 -#define TIMER_AS_VAL 0x0024/* access status */ -#define TIMER_COUNT_R_ACTIVE (1 << 5)/* not ready for read */ -#define TIMER_COUNT_W_ACTIVE (1 << 4)/* not ready for write */ -#define TIMER_MATCH_W_ACTIVE (1 << 0)/* not ready for write */ + +#define TIMER_MATCH_REG (x) (4 * (x)) +#define TIMER_COUNT_REG 0x0010/* clocksource counter */ + +#define TIMER_STATUS_REG 0x0014 +#define TIMER_STATUS_MATCH (x) BIT((x)) +#define TIMER_STATUS_CLEARALL (TIMER_STATUS_MATCH(0) | \ + TIMER_STATUS_MATCH(1) | \ + TIMER_STATUS_MATCH(2) | \ + TIMER_STATUS_MATCH(3)) + +#define TIMER_INT_EN_REG 0x001c/* interrupt enable */ +#define TIMER_INT_EN_MATCH (x) BIT((x)) + +#define TIMER_CTRL_REG 0x0020 +#define TIMER_CTRL_ENABLE BIT(0)/* enable clocksource counter */ +#define TIMER_CTRL_RD_REQ BIT(1)/* request counter read */ + +#define TIMER_ACC_STS_REG 0x0024/* access status */ +#define TIMER_ACC_WR_MATCH (x) BIT((x))/* writing Match (x) value */ +#define TIMER_ACC_WR_COUNTER BIT(4)/* writing clocksource counter */ +#define TIMER_ACC_RD_COUNTER BIT(5)/* reading clocksource counter */ #define msecs_to_loops (t) (loops_per_jiffy / 1000 * HZ * t)
@@ -43,11 +56,12 @@ static void __iomem *regbase;static u64 vt8500_timer_read (struct clocksource *cs) {int loops =msecs_to_loops (10 ); -writel (3 , regbase +TIMER_CTRL_VAL ); -while ((readl ((regbase +TIMER_AS_VAL )) &TIMER_COUNT_R_ACTIVE ) - && --loops) + +writel (TIMER_CTRL_ENABLE |TIMER_CTRL_RD_REQ , regbase +TIMER_CTRL_REG ); +while (readl (regbase +TIMER_ACC_STS_REG ) &TIMER_ACC_RD_COUNTER && + --loops)cpu_relax (); -return readl (regbase +TIMER_COUNT_VAL ); +return readl (regbase +TIMER_COUNT_REG ); }static struct clocksource clocksource = {
@@ -63,23 +77,25 @@ 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_AS_VAL ) &TIMER_MATCH_W_ACTIVE ) - && --loops) + +while (readl (regbase +TIMER_ACC_STS_REG ) &TIMER_ACC_WR_MATCH (0 ) && + --loops)cpu_relax (); -writel ((unsigned long )alarm, regbase +TIMER_MATCH_VAL ); +writel ((unsigned long )alarm, regbase +TIMER_MATCH_REG (0 ));if ((signed )(alarm - clocksource.read (&clocksource)) <=MIN_OSCR_DELTA )return -ETIME ; -writel (1 , regbase +TIMER_IER_VAL ); +writel (TIMER_INT_EN_MATCH (0 ), regbase +TIMER_INT_EN_REG );return 0 ; }static int vt8500_shutdown (struct clock_event_device *evt) { -writel (readl (regbase +TIMER_CTRL_VAL ) |1 , regbase +TIMER_CTRL_VAL ); -writel (0 , regbase +TIMER_IER_VAL ); +writel (readl (regbase +TIMER_CTRL_REG ) |TIMER_CTRL_ENABLE , + regbase +TIMER_CTRL_REG ); +writel (0 , regbase +TIMER_INT_EN_REG );return 0 ; }
@@ -95,7 +111,7 @@ static struct clock_event_device clockevent = {static irqreturn_t vt8500_timer_interrupt (int irq,void *dev_id) {struct clock_event_device *evt = dev_id; -writel (0xf , regbase +TIMER_STATUS_VAL ); +writel (TIMER_STATUS_CLEARALL , regbase +TIMER_STATUS_REG ); evt->event_handler (evt);return IRQ_HANDLED ;
@@ -119,9 +135,9 @@ static int __init vt8500_timer_init(struct device_node *np)return -EINVAL ; } -writel (1 , regbase +TIMER_CTRL_VAL ); -writel (0xf , regbase +TIMER_STATUS_VAL ); -writel (~0 , regbase +TIMER_MATCH_VAL ); +writel (TIMER_CTRL_ENABLE , regbase +TIMER_CTRL_REG ); +writel (TIMER_STATUS_CLEARALL , regbase +TIMER_STATUS_REG ); +writel (~0 , regbase +TIMER_MATCH_REG (0 )); ret =clocksource_register_hz (&clocksource,VT8500_TIMER_HZ );if (ret) {
Signature · 1 line
2.56.0
Alexey Charkov
maintainerPATCH v7 2/4 clocksource/drivers/timer-vt8500: Prepare for watchdog functionality
In reply to Alexey CharkovVIA/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.
--- 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(-)
MAINTAINERS +1 -0
@@ -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 <[email protected]>
drivers/clocksource/Kconfig +1 -0
@@ -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.
drivers/clocksource/timer-vt8500.c +118 -9
180 lines not on this page · raw message
include/clocksource/vt8500-timer.h new file +22 -0
@@ -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_ */
Signature · 1 line
2.56.0
Alexey Charkov
maintainerPATCH v7 3/4 watchdog: Add support for VIA/WonderMedia SoC watchdog functionality
In reply to Alexey CharkovVIA/WonderMedia SoCs can use their system timer's first channel as a watchdog device which will reset the system if the clocksource counter matches the value given in its match register 0 and if the watchdog function is enabled.
Since the watchdog function is tightly coupled to the timer itself, it is implemented as an auxiliary device of the timer device. Build it in rather than as a module, as the timer it depends on is always built in and the driver is tiny.
Report a watchdog found running at probe, as one armed before a kexec would be, so that the core keeps it fed until userspace takes over.
--- Documentation/watchdog/watchdog-parameters.rst | 7 ++ MAINTAINERS | 1 + drivers/watchdog/Kconfig | 12 +++ drivers/watchdog/Makefile | 1 + drivers/watchdog/vt8500-wdt.c | 132 +++++++++++++++++++++++++ 5 files changed, 153 insertions(+)
Documentation/watchdog/watchdog-parameters.rst +7 -0
@@ -637,6 +637,13 @@ uniphier_wdt: ------------------------------------------------- +vt8500_wdt: + nowayout: + Watchdog cannot be stopped once started + (default=kernel config parameter) + +------------------------------------------------- + w83627hf_wdt: wdt_io: w83627hf/thf WDT io port (default 0x2E)
MAINTAINERS +1 -0
@@ -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: drivers/watchdog/vt8500-wdt.c F: include/clocksource/vt8500-timer.h ARM/ZTE ZX29 SOC SUPPORT
drivers/watchdog/Kconfig +12 -0
@@ -1183,6 +1183,18 @@ config SUNPLUS_WATCHDOG To compile this driver as a module, choose M here: the module will be called sunplus_wdt. +config VT8500_WATCHDOG + bool "VIA/WonderMedia VT8500 watchdog support" + depends on ARCH_VT8500 || COMPILE_TEST + select WATCHDOG_CORE + select AUXILIARY_BUS + 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. + # X86 (i386 + ia64 + x86_64) Architecture config ACQUIRE_WDT
drivers/watchdog/Makefile +1 -0
@@ -106,6 +106,7 @@ obj-$(CONFIG_SUNPLUS_WATCHDOG) += sunplus_wdt.o obj-$(CONFIG_MARVELL_GTI_WDT) += marvell_gti_wdt.o obj-$(CONFIG_GUNYAH_WATCHDOG) += gunyah_wdt.o obj-$(CONFIG_MA35D1_WDT) += ma35d1_wdt.o +obj-$(CONFIG_VT8500_WATCHDOG) += vt8500-wdt.o # X86 (i386 + ia64 + x86_64) Architecture obj-$(CONFIG_ACQUIRE_WDT) += acquirewdt.o
drivers/watchdog/vt8500-wdt.c new file +132 -0
132 lines not on this page · raw message
Signature · 1 line
2.56.0
[email protected]
AI reviewRe: [PATCH v7 3/4] watchdog: Add support for VIA/WonderMedia SoC watchdog functionality
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] watchdog: vt8500-wdt: missing kernel-parameters.txt entry for nowayout --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] watchdog: vt8500-wdt: missing kernel-parameters.txt entry for nowayout
--
commit fecd9b898fc4c8ebc86ec22e46234b476572bd19
Author: Alexey Charkov <[email protected]>
watchdog: Add support for VIA/WonderMedia SoC watchdog functionality
This commit adds a built-in auxiliary driver for the watchdog functionality provided by the system timer on VIA/WonderMedia SoCs.
Quoted 6 lines
drivers/watchdog/vt8500-wdt.c new file +0
-0@@ -0,0 +1,132 @@
[ ... ]
drivers/watchdog/vt8500-wdt.c line 31
Quoted 4 lines
drivers/watchdog/vt8500-wdt.c new file +4
-0+static bool nowayout =WATCHDOG_NOWAYOUT ; +module_param (nowayout, bool,0 ); +MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" + __stringify(WATCHDOG_NOWAYOUT) ")");
[Severity: Low]
Should vt8500-wdt.nowayout also be documented inDocumentation/admin-guide/kernel-parameters.txt?
Because CONFIG_VT8500_WATCHDOG is a bool Kconfig symbol and the driver is registered via builtin_driver(), this module_param() (with sysfs permission 0) can only be passed on the kernel boot command line.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Alexey Charkov
maintainerPATCH v7 4/4 watchdog: vt8500: Add a restart handler
In reply to Alexey CharkovReset the SoC through the watchdog when the machine's own restart hook leaves it running: the PMSR write in vt8500_restart() has no effect on WM8950, so a reboot there ends in "Reboot failed -- System halted" unless a watchdog armed from userspace happens to fire.
Arm the match a millisecond ahead of the current count, which leaves ample margin for the register writes to land, then wait for the reset to assert so that no later handler runs in the meantime. Return 0 if it never does, or if the match can't be armed at all, to let the remaining handlers have their turn, as an error would stop the restart chain.
Register at priority 0, the last resort, so that the handler runs only after the ARM machine restart hook at 128 has failed to reset the SoC.
--- drivers/watchdog/vt8500-wdt.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
drivers/watchdog/vt8500-wdt.c +18 -0
@@ -4,6 +4,7 @@#include <linux/auxiliary_bus.h> #include <linux/bits.h> #include <linux/container_of.h> +#include <linux/delay.h> #include <linux/init.h> #include <linux/io.h> #include <linux/iopoll.h>
@@ -25,6 +26,10 @@#define VT8500_WDT_DEFAULT_TIMEOUT 30/* seconds */ +/* How far ahead of the current count to arm the match for a restart */ +#define VT8500_WDT_RESTART_TICKS (VT8500_TIMER_HZ / 1000)/* 1 ms */ +#define VT8500_WDT_RESTART_WAIT_MS 10 +static bool nowayout =WATCHDOG_NOWAYOUT ;module_param (nowayout, bool,0 ); MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
@@ -75,9 +80,21 @@ static int vt8500_watchdog_stop(struct watchdog_device *wdd)return 0 ; } +static int vt8500_watchdog_restart (struct watchdog_device *wdd, +unsigned long action,void *data) +{ +struct vt8500_wdt_info *info =watchdog_get_drvdata (wdd); + +if (!vt8500_watchdog_arm (info,VT8500_WDT_RESTART_TICKS )) +mdelay (VT8500_WDT_RESTART_WAIT_MS ); + +return 0 ; +} +static const struct watchdog_ops vt8500_watchdog_ops = { .start = vt8500_watchdog_start, .stop = vt8500_watchdog_stop, + .restart = vt8500_watchdog_restart, };static const struct watchdog_info vt8500_watchdog_info = {
@@ -107,6 +124,7 @@ static int vt8500_wdt_probe(struct auxiliary_device *auxdev, info = container_of(auxdev, struct vt8500_wdt_info, auxdev);watchdog_set_drvdata (wdd, info);watchdog_set_nowayout (wdd, nowayout); +watchdog_set_restart_priority (wdd,0 );/* Let the core feed a watchdog left running, e.g. across kexec */ if (readl (info->regbase +VT8500_WDT_EN_REG ) &VT8500_WDT_EN )
Signature · 1 line
2.56.0