* [PATCH v5] watchdog: Add ARM TWD (MPCore private) watchdog driver
@ 2026-11-18 8:50 ` Fil Dunsky
0 siblings, 0 replies; 3+ messages in thread
From: Fil Dunsky via B4 Relay @ 2026-11-18 8:50 UTC (permalink / raw)
To: Guenter Roeck, Wim Van Sebroeck
Cc: Fil Dunsky, linux-kernel, linux-watchdog, Russell King,
linux-arm-kernel
From: Fil Dunsky <filipp.dunsky@gmail.com>
ARM11 MPCore, Cortex-A5 and Cortex-A9 MPCore systems have a watchdog
next to the local timer of every CPU, banked at the same address. The
binding "arm,cortex-a9-twd-wdt" exists, but there has been no driver
since mpcore_wdt was removed in v3.11, among other reasons because it
only ever programmed the CPU that ran the ioctl.
Use the watchdog of CPU0 as the system watchdog: start, stop, ping and
get_timeleft run there through smp_call_function_single(). CPU0 cannot
be taken offline on 32-bit ARM unless the platform allows it, the driver
does not probe then. The watchdogs of the other CPUs are left alone. The
watchdog is used in reset mode only, its interrupt is not needed.
Bootloaders such as the one of the Securifi Almond+ start the watchdog
before booting the kernel. A running watchdog is marked WDOG_HW_RUNNING
and taken over with the driver's timeout without being stopped, unless
it runs with a different prescaler: the prescaler cannot be changed in
watchdog mode. Once the watchdog mode is set it can only be left through
the disable register sequence, which is what stop does.
The TWD clock follows the CPU clock on many systems, so rate changes are
followed through a clock notifier. If CPU0 lost its watchdog in a deep
idle state it is set up again on CPU PM exit, and on resume from
suspend. The time of the last keepalive is kept and the watchdog is
always loaded with the time left from it, so none of these extends the
timeout.
The watchdog is also registered as a restart handler with the default
priority, for systems that have no other way to restart: the CPU that
restarts the system, the only one left running, lets its own watchdog
expire right away.
Signed-off-by: Fil Dunsky <filipp.dunsky@gmail.com>
---
ARM11 MPCore, Cortex-A5 and Cortex-A9 MPCore systems have a watchdog next
to the local timer of every CPU. The binding "arm,cortex-a9-twd-wdt"
(watchdog/arm,twd-wdt.yaml) exists and 15 dts/dtsi files describe the
device, but there has been no driver since mpcore_wdt was removed in
v3.11 ("watchdog: delete mpcore_wdt driver").
As discussed on v4, this version uses the watchdog of CPU0 only, as the
system watchdog. CPU0 cannot be taken offline on 32-bit ARM unless the
platform allows it, and the driver does not probe then. The watchdogs of
the other CPUs are not touched.
The driver was written for the Cortina Systems CS75xx / Securifi Almond+,
whose boot loader arms the watchdog of CPU0: the board resets ~300 s
after boot unless it is serviced. The driver also registers a restart
handler with the default (last resort) priority, since the CS75xx
platform has no other way to restart.
Tested on a Securifi Almond+ (Cortex-A9, two CPUs), also with
PROVE_LOCKING: the watchdog left running was taken over; pinged from
userspace for 60 s and then left alone, it reset the board when timeleft
reached zero; reboot(2) issued from CPU1 restarted the board through the
restart handler. Not tested: CPU PM and suspend, the platform has neither.
Open questions:
1. The binding marks clocks as optional, the driver requires them (the
rate is needed to compute the timeout). Should the binding be
tightened, or should the driver fall back to something when there is
no clock? Several of the existing dts files have no clocks property;
the driver does not probe there.
2. Is a separate MAINTAINERS entry for this driver the right thing, or
should it only be covered by the watchdog subsystem entry?
---
Changes in v5:
- use the watchdog of CPU0 only instead of the watchdogs of all CPUs,
which overlapped with the lockup detector (Guenter): no CPU hotplug
state, start, stop, ping and get_timeleft run on CPU0 through
smp_call_function_single(); do not probe if CPU0 is hotpluggable
- keep the time of the last keepalive and load the watchdog with the time
left from it, so a clock rate change or CPU PM exit no longer extends
the timeout (sashiko-bot)
- serialize the watchdog operations and the clock notifier with a mutex
- stop the watchdog when the driver is removed (sashiko-bot)
- fix the comment on suspend (sashiko-bot)
- ignore clock notifications that do not change the rate
- Link to v4: https://patch.msgid.link/20261114-arm-twd-wdt-v4-1-5a3104b36331@gmail.com
Changes in v4:
- rebase on v7.4-rc1
- sent on its own, split out of "Cortina Systems CS75xx: interrupt
controller, GPIO, I2C and TWD watchdog"
- do not take cpus_read_lock() in the clock notifier, which can be
called from CPU hotplug through cpufreq: publish the armed state and
the rate with WRITE_ONCE() and a full barrier first, then apply them
with on_each_cpu(), so that a rate change racing with start is never
missed (sashiko-bot)
- restore the watchdog of a CPU on CPU PM exit when its power domain
went down in idle (sashiko-bot)
- register the restart handler with the default priority instead of
128, so that it only runs when nothing else can restart the system
- print the timeout and the clock rate only as debug messages
- Link to v3: https://patch.msgid.link/20261013-b4-cs75xx-s2-v3-0-181bb0d23f4c@gmail.com
Changes in v3:
- follow rate changes of the clock, which on many MPCore systems scales
with the CPU clock, and restore the watchdog of the boot CPU on resume
(sashiko-bot)
- Link to v2: https://patch.msgid.link/20261013-b4-cs75xx-s2-v2-0-94f8c0a90cda@gmail.com
Changes in v2:
- do not include <linux/mod_devicetable.h> (Uwe)
- the prescaler cannot be changed in watchdog mode (verified on the
board), so a watchdog left running by the boot loader with a different
prescaler is stopped before it is reprogrammed (sashiko-bot)
- Link to v1: https://patch.msgid.link/20261012-b4-cs75xx-s2-v1-0-8a7641b7b79c@gmail.com
To: Wim Van Sebroeck <wim@linux-watchdog.org>
To: Guenter Roeck <linux@roeck-us.net>
Cc: linux-kernel@vger.kernel.org
Cc: linux-watchdog@vger.kernel.org
Cc: Russell King <linux@armlinux.org.uk>
Cc: linux-arm-kernel@lists.infradead.org
---
MAINTAINERS | 6 +
drivers/watchdog/Kconfig | 15 ++
drivers/watchdog/Makefile | 1 +
drivers/watchdog/arm_twd_wdt.c | 465 +++++++++++++++++++++++++++++++++++++++++
4 files changed, 487 insertions(+)
diff --git a/MAINTAINERS b/MAINTAINERS
index 93abbb315..245d80c13 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -2544,6 +2544,12 @@ T: git git://git.kernel.org/pub/scm/linux/kernel/git/soc/soc.git
F: arch/arm/mach-*/
F: arch/arm/plat-*/
+ARM TWD WATCHDOG DRIVER
+M: Fil Dunsky <filipp.dunsky@gmail.com>
+L: linux-watchdog@vger.kernel.org
+S: Maintained
+F: drivers/watchdog/arm_twd_wdt.c
+
ARM/ACTIONS SEMI ARCHITECTURE
M: Andreas Färber <afaerber@suse.de>
M: Manivannan Sadhasivam <mani@kernel.org>
diff --git a/drivers/watchdog/Kconfig b/drivers/watchdog/Kconfig
index f7d0a0c2c..326546e24 100644
--- a/drivers/watchdog/Kconfig
+++ b/drivers/watchdog/Kconfig
@@ -465,6 +465,21 @@ config ARM_SP805_WATCHDOG
ARM Primecell SP805 Watchdog timer. This will reboot your system when
the timeout is reached.
+config ARM_TWD_WATCHDOG
+ tristate "ARM TWD (MPCore private) Watchdog"
+ depends on HAVE_ARM_TWD || COMPILE_TEST
+ depends on HAS_IOMEM
+ select WATCHDOG_CORE
+ help
+ Say Y here to include support for the private watchdog found next
+ to the local (TWD) timer of every CPU in ARM11 MPCore, Cortex-A5
+ and Cortex-A9 MPCore systems. The watchdog of CPU0 is used as the
+ system watchdog. A watchdog left running by the bootloader is
+ taken over.
+
+ To compile this driver as a module, choose M here: the
+ module will be called arm_twd_wdt.
+
config ARM_SBSA_WATCHDOG
tristate "ARM SBSA Generic Watchdog"
depends on ARM64
diff --git a/drivers/watchdog/Makefile b/drivers/watchdog/Makefile
index 9ac406677..4ff0bced5 100644
--- a/drivers/watchdog/Makefile
+++ b/drivers/watchdog/Makefile
@@ -40,6 +40,7 @@ obj-$(CONFIG_USBPCWATCHDOG) += pcwd_usb.o
# ARM Architecture
obj-$(CONFIG_ARM_SP805_WATCHDOG) += sp805_wdt.o
obj-$(CONFIG_ARM_SBSA_WATCHDOG) += sbsa_gwdt.o
+obj-$(CONFIG_ARM_TWD_WATCHDOG) += arm_twd_wdt.o
obj-$(CONFIG_ARMADA_37XX_WATCHDOG) += armada_37xx_wdt.o
obj-$(CONFIG_AIROHA_WATCHDOG) += airoha_wdt.o
obj-$(CONFIG_ASM9260_WATCHDOG) += asm9260_wdt.o
diff --git a/drivers/watchdog/arm_twd_wdt.c b/drivers/watchdog/arm_twd_wdt.c
new file mode 100644
index 000000000..068b6483a
--- /dev/null
+++ b/drivers/watchdog/arm_twd_wdt.c
@@ -0,0 +1,465 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Watchdog driver for the ARM Cortex-A9/A5 and ARM11 MPCore private
+ * (TWD) watchdog
+ *
+ * Every CPU has its own watchdog, banked at the same address, and any of
+ * them resets the system when it expires. The driver uses the watchdog of
+ * CPU0 as the system watchdog and leaves the others alone: CPU0 cannot be
+ * taken offline, so the registers are always reached through it. Once the
+ * watchdog mode bit is set it can only be cleared by writing the disable
+ * sequence to the disable register.
+ */
+
+#include <linux/bitfield.h>
+#include <linux/bits.h>
+#include <linux/cleanup.h>
+#include <linux/clk.h>
+#include <linux/cpu.h>
+#include <linux/cpu_pm.h>
+#include <linux/delay.h>
+#include <linux/io.h>
+#include <linux/ktime.h>
+#include <linux/math64.h>
+#include <linux/minmax.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/platform_device.h>
+#include <linux/pm.h>
+#include <linux/smp.h>
+#include <linux/watchdog.h>
+
+#define TWD_WDT_LOAD 0x00
+#define TWD_WDT_COUNTER 0x04
+#define TWD_WDT_CONTROL 0x08
+#define TWD_WDT_RESET_STATUS 0x10
+#define TWD_WDT_DISABLE 0x14
+
+#define TWD_WDT_CTRL_ENABLE BIT(0)
+#define TWD_WDT_CTRL_WD_MODE BIT(3)
+#define TWD_WDT_CTRL_PRESCALER GENMASK(15, 8)
+
+#define TWD_WDT_RESET_FLAG BIT(0)
+
+#define TWD_WDT_DISABLE_KEY1 0x12345678
+#define TWD_WDT_DISABLE_KEY2 0x87654321
+
+/* Largest prescaler, the counter clock is the input clock divided by 256 */
+#define TWD_WDT_PRESCALE 255
+#define TWD_WDT_DEFAULT_TIMEOUT 30
+
+/* The CPU whose watchdog is used */
+#define TWD_WDT_CPU 0
+
+static bool nowayout = WATCHDOG_NOWAYOUT;
+module_param(nowayout, bool, 0);
+MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
+ __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
+
+static unsigned int timeout;
+module_param(timeout, uint, 0);
+MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds");
+
+struct twd_wdt {
+ struct watchdog_device wdd;
+ void __iomem *base;
+ struct notifier_block clk_nb;
+ struct notifier_block pm_nb;
+ /* Serializes the watchdog operations and the clock notifier */
+ struct mutex lock;
+ /* Also read by the CPU PM notifier, which cannot take the lock */
+ unsigned long rate;
+ bool armed;
+ /* Time of the last start or ping in ns */
+ atomic64_t keepalive;
+ bool resume_armed;
+ u32 counter;
+ bool running;
+ bool reset;
+};
+
+/*
+ * Counter value for the time left until the watchdog has to expire: the
+ * watchdog is always loaded with it, so neither a clock rate change nor
+ * setting it up again after a CPU power down extends the timeout.
+ */
+static u32 twd_wdt_ticks(struct twd_wdt *wdt)
+{
+ s64 expires = atomic64_read(&wdt->keepalive) +
+ (s64)wdt->wdd.timeout * NSEC_PER_SEC;
+ s64 left = expires - ktime_get_ns();
+ u64 ticks;
+
+ if (left <= 0)
+ return 1;
+
+ ticks = mul_u64_u64_div_u64(READ_ONCE(wdt->rate), left,
+ (u64)NSEC_PER_SEC * (TWD_WDT_PRESCALE + 1));
+
+ return clamp_t(u64, ticks, 1, U32_MAX);
+}
+
+/* The following run on TWD_WDT_CPU */
+static void twd_wdt_cpu_stop(void *data)
+{
+ struct twd_wdt *wdt = data;
+
+ writel(TWD_WDT_DISABLE_KEY1, wdt->base + TWD_WDT_DISABLE);
+ writel(TWD_WDT_DISABLE_KEY2, wdt->base + TWD_WDT_DISABLE);
+ writel(0, wdt->base + TWD_WDT_CONTROL);
+}
+
+static void twd_wdt_cpu_start(void *data)
+{
+ struct twd_wdt *wdt = data;
+ u32 ctrl = readl(wdt->base + TWD_WDT_CONTROL);
+
+ /*
+ * In watchdog mode the prescaler cannot be changed, so a watchdog
+ * started by the bootloader with a different one is stopped first.
+ */
+ if ((ctrl & TWD_WDT_CTRL_WD_MODE) &&
+ FIELD_GET(TWD_WDT_CTRL_PRESCALER, ctrl) != TWD_WDT_PRESCALE)
+ twd_wdt_cpu_stop(wdt);
+
+ /*
+ * Load the counter before enabling, it may still be zero. A watchdog
+ * that is already running is reloaded and keeps running.
+ */
+ writel(twd_wdt_ticks(wdt), wdt->base + TWD_WDT_LOAD);
+ writel(FIELD_PREP(TWD_WDT_CTRL_PRESCALER, TWD_WDT_PRESCALE) |
+ TWD_WDT_CTRL_WD_MODE | TWD_WDT_CTRL_ENABLE,
+ wdt->base + TWD_WDT_CONTROL);
+}
+
+static void twd_wdt_cpu_ping(void *data)
+{
+ struct twd_wdt *wdt = data;
+
+ /* In watchdog mode a write to the load register reloads the counter */
+ writel(twd_wdt_ticks(wdt), wdt->base + TWD_WDT_LOAD);
+}
+
+static void twd_wdt_cpu_counter(void *data)
+{
+ struct twd_wdt *wdt = data;
+
+ wdt->counter = readl(wdt->base + TWD_WDT_COUNTER);
+}
+
+static void twd_wdt_cpu_probe(void *data)
+{
+ struct twd_wdt *wdt = data;
+ u32 ctrl = readl(wdt->base + TWD_WDT_CONTROL);
+
+ wdt->running = (ctrl & (TWD_WDT_CTRL_WD_MODE | TWD_WDT_CTRL_ENABLE)) ==
+ (TWD_WDT_CTRL_WD_MODE | TWD_WDT_CTRL_ENABLE);
+
+ if (readl(wdt->base + TWD_WDT_RESET_STATUS) & TWD_WDT_RESET_FLAG) {
+ wdt->reset = true;
+ /* The flag is cleared by writing one */
+ writel(TWD_WDT_RESET_FLAG, wdt->base + TWD_WDT_RESET_STATUS);
+ }
+}
+
+static void twd_wdt_call(struct twd_wdt *wdt, smp_call_func_t func)
+{
+ smp_call_function_single(TWD_WDT_CPU, func, wdt, 1);
+}
+
+static void twd_wdt_set_armed(struct twd_wdt *wdt, bool armed)
+{
+ guard(mutex)(&wdt->lock);
+
+ atomic64_set(&wdt->keepalive, ktime_get_ns());
+ WRITE_ONCE(wdt->armed, armed);
+ twd_wdt_call(wdt, armed ? twd_wdt_cpu_start : twd_wdt_cpu_stop);
+}
+
+static int twd_wdt_start(struct watchdog_device *wdd)
+{
+ twd_wdt_set_armed(watchdog_get_drvdata(wdd), true);
+
+ return 0;
+}
+
+static int twd_wdt_stop(struct watchdog_device *wdd)
+{
+ twd_wdt_set_armed(watchdog_get_drvdata(wdd), false);
+
+ return 0;
+}
+
+static int twd_wdt_ping(struct watchdog_device *wdd)
+{
+ struct twd_wdt *wdt = watchdog_get_drvdata(wdd);
+
+ guard(mutex)(&wdt->lock);
+
+ atomic64_set(&wdt->keepalive, ktime_get_ns());
+ twd_wdt_call(wdt, twd_wdt_cpu_ping);
+
+ return 0;
+}
+
+static int twd_wdt_set_timeout(struct watchdog_device *wdd, unsigned int t)
+{
+ wdd->timeout = t;
+ if (watchdog_active(wdd) || watchdog_hw_running(wdd))
+ twd_wdt_ping(wdd);
+
+ return 0;
+}
+
+static unsigned int twd_wdt_get_timeleft(struct watchdog_device *wdd)
+{
+ struct twd_wdt *wdt = watchdog_get_drvdata(wdd);
+ u64 ticks;
+
+ guard(mutex)(&wdt->lock);
+
+ twd_wdt_call(wdt, twd_wdt_cpu_counter);
+ ticks = (u64)wdt->counter * (TWD_WDT_PRESCALE + 1);
+
+ return div_u64(ticks, wdt->rate);
+}
+
+/*
+ * The TWD runs from the peripheral clock, which follows the CPU clock. While
+ * the rate goes up, count with the new rate already: the watchdog then just
+ * fires later than asked until the change is done.
+ */
+static int twd_wdt_clk_notify(struct notifier_block *nb, unsigned long event,
+ void *data)
+{
+ struct twd_wdt *wdt = container_of(nb, struct twd_wdt, clk_nb);
+ struct clk_notifier_data *cnd = data;
+ unsigned long rate;
+
+ switch (event) {
+ case PRE_RATE_CHANGE:
+ if (cnd->new_rate <= cnd->old_rate)
+ return NOTIFY_OK;
+ rate = cnd->new_rate;
+ break;
+ case POST_RATE_CHANGE:
+ rate = cnd->new_rate;
+ break;
+ case ABORT_RATE_CHANGE:
+ rate = cnd->old_rate;
+ break;
+ default:
+ return NOTIFY_DONE;
+ }
+
+ guard(mutex)(&wdt->lock);
+
+ if (!rate || rate == wdt->rate)
+ return NOTIFY_OK;
+
+ WRITE_ONCE(wdt->rate, rate);
+ if (wdt->armed)
+ twd_wdt_call(wdt, twd_wdt_cpu_ping);
+
+ return NOTIFY_OK;
+}
+
+/*
+ * A CPU whose power domain was turned off in idle comes back with its
+ * watchdog stopped. Only then set it up again: reloading a watchdog that
+ * kept running would ping it on every idle exit.
+ */
+static int twd_wdt_cpu_pm_notify(struct notifier_block *nb,
+ unsigned long action, void *data)
+{
+ struct twd_wdt *wdt = container_of(nb, struct twd_wdt, pm_nb);
+
+ if (action != CPU_PM_EXIT && action != CPU_PM_ENTER_FAILED)
+ return NOTIFY_DONE;
+
+ if (smp_processor_id() == TWD_WDT_CPU && READ_ONCE(wdt->armed) &&
+ !(readl(wdt->base + TWD_WDT_CONTROL) & TWD_WDT_CTRL_WD_MODE))
+ twd_wdt_cpu_start(wdt);
+
+ return NOTIFY_OK;
+}
+
+static int twd_wdt_restart(struct watchdog_device *wdd, unsigned long action,
+ void *data)
+{
+ struct twd_wdt *wdt = watchdog_get_drvdata(wdd);
+
+ /*
+ * Only this CPU is still running, and it may not be TWD_WDT_CPU. The
+ * watchdog of any CPU resets the system: let its own expire right away.
+ */
+ writel(1, wdt->base + TWD_WDT_LOAD);
+ writel(TWD_WDT_CTRL_WD_MODE | TWD_WDT_CTRL_ENABLE,
+ wdt->base + TWD_WDT_CONTROL);
+ mdelay(500);
+
+ return 0;
+}
+
+static const struct watchdog_ops twd_wdt_ops = {
+ .owner = THIS_MODULE,
+ .start = twd_wdt_start,
+ .stop = twd_wdt_stop,
+ .ping = twd_wdt_ping,
+ .set_timeout = twd_wdt_set_timeout,
+ .get_timeleft = twd_wdt_get_timeleft,
+ .restart = twd_wdt_restart,
+};
+
+static const struct watchdog_info twd_wdt_info = {
+ .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
+ WDIOF_MAGICCLOSE | WDIOF_CARDRESET,
+ .identity = "ARM TWD watchdog",
+};
+
+/* A watchdog left running would not be pinged any more */
+static void twd_wdt_disable(void *data)
+{
+ struct twd_wdt *wdt = data;
+
+ twd_wdt_set_armed(wdt, false);
+}
+
+static void twd_wdt_remove_pm_notifier(void *data)
+{
+ struct twd_wdt *wdt = data;
+
+ cpu_pm_unregister_notifier(&wdt->pm_nb);
+}
+
+static int twd_wdt_probe(struct platform_device *pdev)
+{
+ struct device *dev = &pdev->dev;
+ struct twd_wdt *wdt;
+ struct clk *clk;
+ int ret;
+
+ if (cpu_is_hotpluggable(TWD_WDT_CPU))
+ return dev_err_probe(dev, -EOPNOTSUPP,
+ "CPU%d can be taken offline\n", TWD_WDT_CPU);
+
+ wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
+ if (!wdt)
+ return -ENOMEM;
+
+ ret = devm_mutex_init(dev, &wdt->lock);
+ if (ret)
+ return ret;
+
+ wdt->base = devm_platform_ioremap_resource(pdev, 0);
+ if (IS_ERR(wdt->base))
+ return PTR_ERR(wdt->base);
+
+ clk = devm_clk_get_enabled(dev, NULL);
+ if (IS_ERR(clk))
+ return dev_err_probe(dev, PTR_ERR(clk), "failed to get clock\n");
+
+ wdt->rate = clk_get_rate(clk);
+ if (!wdt->rate)
+ return dev_err_probe(dev, -EINVAL, "clock rate is zero\n");
+
+ wdt->clk_nb.notifier_call = twd_wdt_clk_notify;
+ ret = devm_clk_notifier_register(dev, clk, &wdt->clk_nb);
+ if (ret)
+ return dev_err_probe(dev, ret, "failed to register clock notifier\n");
+ platform_set_drvdata(pdev, wdt);
+
+ wdt->wdd.info = &twd_wdt_info;
+ wdt->wdd.ops = &twd_wdt_ops;
+ wdt->wdd.parent = dev;
+ wdt->wdd.min_timeout = 1;
+ wdt->wdd.max_timeout = div_u64((u64)U32_MAX * (TWD_WDT_PRESCALE + 1),
+ wdt->rate);
+ wdt->wdd.timeout = min_t(unsigned int, TWD_WDT_DEFAULT_TIMEOUT,
+ wdt->wdd.max_timeout);
+ watchdog_init_timeout(&wdt->wdd, timeout, dev);
+ watchdog_set_nowayout(&wdt->wdd, nowayout);
+ watchdog_stop_on_reboot(&wdt->wdd);
+ watchdog_stop_on_unregister(&wdt->wdd);
+ watchdog_set_drvdata(&wdt->wdd, wdt);
+
+ twd_wdt_call(wdt, twd_wdt_cpu_probe);
+ if (wdt->reset) {
+ wdt->wdd.bootstatus = WDIOF_CARDRESET;
+ dev_info(dev, "last reset caused by the watchdog\n");
+ }
+
+ if (wdt->running) {
+ /* Left running by the bootloader: take it over without a stop */
+ dev_info(dev, "running, taking over\n");
+ twd_wdt_start(&wdt->wdd);
+ set_bit(WDOG_HW_RUNNING, &wdt->wdd.status);
+ }
+
+ wdt->pm_nb.notifier_call = twd_wdt_cpu_pm_notify;
+ ret = cpu_pm_register_notifier(&wdt->pm_nb);
+ if (ret)
+ return ret;
+
+ ret = devm_add_action_or_reset(dev, twd_wdt_remove_pm_notifier, wdt);
+ if (ret)
+ return ret;
+
+ ret = devm_add_action_or_reset(dev, twd_wdt_disable, wdt);
+ if (ret)
+ return ret;
+
+ ret = devm_watchdog_register_device(dev, &wdt->wdd);
+ if (ret)
+ return ret;
+
+ dev_dbg(dev, "timeout %u s (max %u s), clock %lu Hz\n",
+ wdt->wdd.timeout, wdt->wdd.max_timeout, wdt->rate);
+
+ return 0;
+}
+
+static const struct of_device_id twd_wdt_of_match[] = {
+ { .compatible = "arm,arm11mp-twd-wdt" },
+ { .compatible = "arm,cortex-a5-twd-wdt" },
+ { .compatible = "arm,cortex-a9-twd-wdt" },
+ { }
+};
+MODULE_DEVICE_TABLE(of, twd_wdt_of_match);
+
+/* The watchdog loses its state in suspend, set it up again at resume */
+static int twd_wdt_suspend(struct device *dev)
+{
+ struct twd_wdt *wdt = dev_get_drvdata(dev);
+
+ wdt->resume_armed = wdt->armed;
+ if (wdt->resume_armed)
+ twd_wdt_stop(&wdt->wdd);
+
+ return 0;
+}
+
+static int twd_wdt_resume(struct device *dev)
+{
+ struct twd_wdt *wdt = dev_get_drvdata(dev);
+
+ if (wdt->resume_armed)
+ twd_wdt_start(&wdt->wdd);
+
+ return 0;
+}
+
+static DEFINE_SIMPLE_DEV_PM_OPS(twd_wdt_pm_ops, twd_wdt_suspend, twd_wdt_resume);
+
+static struct platform_driver twd_wdt_driver = {
+ .probe = twd_wdt_probe,
+ .driver = {
+ .name = "arm-twd-wdt",
+ .of_match_table = twd_wdt_of_match,
+ .pm = pm_sleep_ptr(&twd_wdt_pm_ops),
+ },
+};
+module_platform_driver(twd_wdt_driver);
+
+MODULE_DESCRIPTION("ARM TWD (MPCore private) watchdog driver");
+MODULE_LICENSE("GPL");
---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20261015-arm-twd-wdt-71ff5cef355b
Best regards,
--
Fil Dunsky <filipp.dunsky@gmail.com>
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH v5] watchdog: Add ARM TWD (MPCore private) watchdog driver
@ 2026-11-18 8:50 ` Fil Dunsky
0 siblings, 0 replies; 3+ messages in thread
From: Fil Dunsky @ 2026-11-18 8:50 UTC (permalink / raw)
To: Guenter Roeck, Wim Van Sebroeck
Cc: Fil Dunsky, linux-kernel, linux-watchdog, Russell King,
linux-arm-kernel
ARM11 MPCore, Cortex-A5 and Cortex-A9 MPCore systems have a watchdog
next to the local timer of every CPU, banked at the same address. The
binding "arm,cortex-a9-twd-wdt" exists, but there has been no driver
since mpcore_wdt was removed in v3.11, among other reasons because it
only ever programmed the CPU that ran the ioctl.
Use the watchdog of CPU0 as the system watchdog: start, stop, ping and
get_timeleft run there through smp_call_function_single(). CPU0 cannot
be taken offline on 32-bit ARM unless the platform allows it, the driver
does not probe then. The watchdogs of the other CPUs are left alone. The
watchdog is used in reset mode only, its interrupt is not needed.
Bootloaders such as the one of the Securifi Almond+ start the watchdog
before booting the kernel. A running watchdog is marked WDOG_HW_RUNNING
and taken over with the driver's timeout without being stopped, unless
it runs with a different prescaler: the prescaler cannot be changed in
watchdog mode. Once the watchdog mode is set it can only be left through
the disable register sequence, which is what stop does.
The TWD clock follows the CPU clock on many systems, so rate changes are
followed through a clock notifier. If CPU0 lost its watchdog in a deep
idle state it is set up again on CPU PM exit, and on resume from
suspend. The time of the last keepalive is kept and the watchdog is
always loaded with the time left from it, so none of these extends the
timeout.
The watchdog is also registered as a restart handler with the default
priority, for systems that have no other way to restart: the CPU that
restarts the system, the only one left running, lets its own watchdog
expire right away.
Signed-off-by: Fil Dunsky <filipp.dunsky@gmail.com>
---
ARM11 MPCore, Cortex-A5 and Cortex-A9 MPCore systems have a watchdog next
to the local timer of every CPU. The binding "arm,cortex-a9-twd-wdt"
(watchdog/arm,twd-wdt.yaml) exists and 15 dts/dtsi files describe the
device, but there has been no driver since mpcore_wdt was removed in
v3.11 ("watchdog: delete mpcore_wdt driver").
As discussed on v4, this version uses the watchdog of CPU0 only, as the
system watchdog. CPU0 cannot be taken offline on 32-bit ARM unless the
platform allows it, and the driver does not probe then. The watchdogs of
the other CPUs are not touched.
The driver was written for the Cortina Systems CS75xx / Securifi Almond+,
whose boot loader arms the watchdog of CPU0: the board resets ~300 s
after boot unless it is serviced. The driver also registers a restart
handler with the default (last resort) priority, since the CS75xx
platform has no other way to restart.
Tested on a Securifi Almond+ (Cortex-A9, two CPUs), also with
PROVE_LOCKING: the watchdog left running was taken over; pinged from
userspace for 60 s and then left alone, it reset the board when timeleft
reached zero; reboot(2) issued from CPU1 restarted the board through the
restart handler. Not tested: CPU PM and suspend, the platform has neither.
Open questions:
1. The binding marks clocks as optional, the driver requires them (the
rate is needed to compute the timeout). Should the binding be
tightened, or should the driver fall back to something when there is
no clock? Several of the existing dts files have no clocks property;
the driver does not probe there.
2. Is a separate MAINTAINERS entry for this driver the right thing, or
should it only be covered by the watchdog subsystem entry?
---
Changes in v5:
- use the watchdog of CPU0 only instead of the watchdogs of all CPUs,
which overlapped with the lockup detector (Guenter): no CPU hotplug
state, start, stop, ping and get_timeleft run on CPU0 through
smp_call_function_single(); do not probe if CPU0 is hotpluggable
- keep the time of the last keepalive and load the watchdog with the time
left from it, so a clock rate change or CPU PM exit no longer extends
the timeout (sashiko-bot)
- serialize the watchdog operations and the clock notifier with a mutex
- stop the watchdog when the driver is removed (sashiko-bot)
- fix the comment on suspend (sashiko-bot)
- ignore clock notifications that do not change the rate
- Link to v4: https://patch.msgid.link/20261114-arm-twd-wdt-v4-1-5a3104b36331@gmail.com
Changes in v4:
- rebase on v7.4-rc1
- sent on its own, split out of "Cortina Systems CS75xx: interrupt
controller, GPIO, I2C and TWD watchdog"
- do not take cpus_read_lock() in the clock notifier, which can be
called from CPU hotplug through cpufreq: publish the armed state and
the rate with WRITE_ONCE() and a full barrier first, then apply them
with on_each_cpu(), so that a rate change racing with start is never
missed (sashiko-bot)
- restore the watchdog of a CPU on CPU PM exit when its power domain
went down in idle (sashiko-bot)
- register the restart handler with the default priority instead of
128, so that it only runs when nothing else can restart the system
- print the timeout and the clock rate only as debug messages
- Link to v3: https://patch.msgid.link/20261013-b4-cs75xx-s2-v3-0-181bb0d23f4c@gmail.com
Changes in v3:
- follow rate changes of the clock, which on many MPCore systems scales
with the CPU clock, and restore the watchdog of the boot CPU on resume
(sashiko-bot)
- Link to v2: https://patch.msgid.link/20261013-b4-cs75xx-s2-v2-0-94f8c0a90cda@gmail.com
Changes in v2:
- do not include <linux/mod_devicetable.h> (Uwe)
- the prescaler cannot be changed in watchdog mode (verified on the
board), so a watchdog left running by the boot loader with a different
prescaler is stopped before it is reprogrammed (sashiko-bot)
- Link to v1: https://patch.msgid.link/20261012-b4-cs75xx-s2-v1-0-8a7641b7b79c@gmail.com
To: Wim Van Sebroeck <wim@linux-watchdog.org>
To: Guenter Roeck <linux@roeck-us.net>
Cc: linux-kernel@vger.kernel.org
Cc: linux-watchdog@vger.kernel.org
Cc: Russell King <linux@armlinux.org.uk>
Cc: linux-arm-kernel@lists.infradead.org
---
MAINTAINERS | 6 +
drivers/watchdog/Kconfig | 15 ++
drivers/watchdog/Makefile | 1 +
drivers/watchdog/arm_twd_wdt.c | 465 +++++++++++++++++++++++++++++++++++++++++
4 files changed, 487 insertions(+)
diff --git a/MAINTAINERS b/MAINTAINERS
index 93abbb315..245d80c13 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -2544,6 +2544,12 @@ T: git git://git.kernel.org/pub/scm/linux/kernel/git/soc/soc.git
F: arch/arm/mach-*/
F: arch/arm/plat-*/
+ARM TWD WATCHDOG DRIVER
+M: Fil Dunsky <filipp.dunsky@gmail.com>
+L: linux-watchdog@vger.kernel.org
+S: Maintained
+F: drivers/watchdog/arm_twd_wdt.c
+
ARM/ACTIONS SEMI ARCHITECTURE
M: Andreas Färber <afaerber@suse.de>
M: Manivannan Sadhasivam <mani@kernel.org>
diff --git a/drivers/watchdog/Kconfig b/drivers/watchdog/Kconfig
index f7d0a0c2c..326546e24 100644
--- a/drivers/watchdog/Kconfig
+++ b/drivers/watchdog/Kconfig
@@ -465,6 +465,21 @@ config ARM_SP805_WATCHDOG
ARM Primecell SP805 Watchdog timer. This will reboot your system when
the timeout is reached.
+config ARM_TWD_WATCHDOG
+ tristate "ARM TWD (MPCore private) Watchdog"
+ depends on HAVE_ARM_TWD || COMPILE_TEST
+ depends on HAS_IOMEM
+ select WATCHDOG_CORE
+ help
+ Say Y here to include support for the private watchdog found next
+ to the local (TWD) timer of every CPU in ARM11 MPCore, Cortex-A5
+ and Cortex-A9 MPCore systems. The watchdog of CPU0 is used as the
+ system watchdog. A watchdog left running by the bootloader is
+ taken over.
+
+ To compile this driver as a module, choose M here: the
+ module will be called arm_twd_wdt.
+
config ARM_SBSA_WATCHDOG
tristate "ARM SBSA Generic Watchdog"
depends on ARM64
diff --git a/drivers/watchdog/Makefile b/drivers/watchdog/Makefile
index 9ac406677..4ff0bced5 100644
--- a/drivers/watchdog/Makefile
+++ b/drivers/watchdog/Makefile
@@ -40,6 +40,7 @@ obj-$(CONFIG_USBPCWATCHDOG) += pcwd_usb.o
# ARM Architecture
obj-$(CONFIG_ARM_SP805_WATCHDOG) += sp805_wdt.o
obj-$(CONFIG_ARM_SBSA_WATCHDOG) += sbsa_gwdt.o
+obj-$(CONFIG_ARM_TWD_WATCHDOG) += arm_twd_wdt.o
obj-$(CONFIG_ARMADA_37XX_WATCHDOG) += armada_37xx_wdt.o
obj-$(CONFIG_AIROHA_WATCHDOG) += airoha_wdt.o
obj-$(CONFIG_ASM9260_WATCHDOG) += asm9260_wdt.o
diff --git a/drivers/watchdog/arm_twd_wdt.c b/drivers/watchdog/arm_twd_wdt.c
new file mode 100644
index 000000000..068b6483a
--- /dev/null
+++ b/drivers/watchdog/arm_twd_wdt.c
@@ -0,0 +1,465 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Watchdog driver for the ARM Cortex-A9/A5 and ARM11 MPCore private
+ * (TWD) watchdog
+ *
+ * Every CPU has its own watchdog, banked at the same address, and any of
+ * them resets the system when it expires. The driver uses the watchdog of
+ * CPU0 as the system watchdog and leaves the others alone: CPU0 cannot be
+ * taken offline, so the registers are always reached through it. Once the
+ * watchdog mode bit is set it can only be cleared by writing the disable
+ * sequence to the disable register.
+ */
+
+#include <linux/bitfield.h>
+#include <linux/bits.h>
+#include <linux/cleanup.h>
+#include <linux/clk.h>
+#include <linux/cpu.h>
+#include <linux/cpu_pm.h>
+#include <linux/delay.h>
+#include <linux/io.h>
+#include <linux/ktime.h>
+#include <linux/math64.h>
+#include <linux/minmax.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/platform_device.h>
+#include <linux/pm.h>
+#include <linux/smp.h>
+#include <linux/watchdog.h>
+
+#define TWD_WDT_LOAD 0x00
+#define TWD_WDT_COUNTER 0x04
+#define TWD_WDT_CONTROL 0x08
+#define TWD_WDT_RESET_STATUS 0x10
+#define TWD_WDT_DISABLE 0x14
+
+#define TWD_WDT_CTRL_ENABLE BIT(0)
+#define TWD_WDT_CTRL_WD_MODE BIT(3)
+#define TWD_WDT_CTRL_PRESCALER GENMASK(15, 8)
+
+#define TWD_WDT_RESET_FLAG BIT(0)
+
+#define TWD_WDT_DISABLE_KEY1 0x12345678
+#define TWD_WDT_DISABLE_KEY2 0x87654321
+
+/* Largest prescaler, the counter clock is the input clock divided by 256 */
+#define TWD_WDT_PRESCALE 255
+#define TWD_WDT_DEFAULT_TIMEOUT 30
+
+/* The CPU whose watchdog is used */
+#define TWD_WDT_CPU 0
+
+static bool nowayout = WATCHDOG_NOWAYOUT;
+module_param(nowayout, bool, 0);
+MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
+ __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
+
+static unsigned int timeout;
+module_param(timeout, uint, 0);
+MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds");
+
+struct twd_wdt {
+ struct watchdog_device wdd;
+ void __iomem *base;
+ struct notifier_block clk_nb;
+ struct notifier_block pm_nb;
+ /* Serializes the watchdog operations and the clock notifier */
+ struct mutex lock;
+ /* Also read by the CPU PM notifier, which cannot take the lock */
+ unsigned long rate;
+ bool armed;
+ /* Time of the last start or ping in ns */
+ atomic64_t keepalive;
+ bool resume_armed;
+ u32 counter;
+ bool running;
+ bool reset;
+};
+
+/*
+ * Counter value for the time left until the watchdog has to expire: the
+ * watchdog is always loaded with it, so neither a clock rate change nor
+ * setting it up again after a CPU power down extends the timeout.
+ */
+static u32 twd_wdt_ticks(struct twd_wdt *wdt)
+{
+ s64 expires = atomic64_read(&wdt->keepalive) +
+ (s64)wdt->wdd.timeout * NSEC_PER_SEC;
+ s64 left = expires - ktime_get_ns();
+ u64 ticks;
+
+ if (left <= 0)
+ return 1;
+
+ ticks = mul_u64_u64_div_u64(READ_ONCE(wdt->rate), left,
+ (u64)NSEC_PER_SEC * (TWD_WDT_PRESCALE + 1));
+
+ return clamp_t(u64, ticks, 1, U32_MAX);
+}
+
+/* The following run on TWD_WDT_CPU */
+static void twd_wdt_cpu_stop(void *data)
+{
+ struct twd_wdt *wdt = data;
+
+ writel(TWD_WDT_DISABLE_KEY1, wdt->base + TWD_WDT_DISABLE);
+ writel(TWD_WDT_DISABLE_KEY2, wdt->base + TWD_WDT_DISABLE);
+ writel(0, wdt->base + TWD_WDT_CONTROL);
+}
+
+static void twd_wdt_cpu_start(void *data)
+{
+ struct twd_wdt *wdt = data;
+ u32 ctrl = readl(wdt->base + TWD_WDT_CONTROL);
+
+ /*
+ * In watchdog mode the prescaler cannot be changed, so a watchdog
+ * started by the bootloader with a different one is stopped first.
+ */
+ if ((ctrl & TWD_WDT_CTRL_WD_MODE) &&
+ FIELD_GET(TWD_WDT_CTRL_PRESCALER, ctrl) != TWD_WDT_PRESCALE)
+ twd_wdt_cpu_stop(wdt);
+
+ /*
+ * Load the counter before enabling, it may still be zero. A watchdog
+ * that is already running is reloaded and keeps running.
+ */
+ writel(twd_wdt_ticks(wdt), wdt->base + TWD_WDT_LOAD);
+ writel(FIELD_PREP(TWD_WDT_CTRL_PRESCALER, TWD_WDT_PRESCALE) |
+ TWD_WDT_CTRL_WD_MODE | TWD_WDT_CTRL_ENABLE,
+ wdt->base + TWD_WDT_CONTROL);
+}
+
+static void twd_wdt_cpu_ping(void *data)
+{
+ struct twd_wdt *wdt = data;
+
+ /* In watchdog mode a write to the load register reloads the counter */
+ writel(twd_wdt_ticks(wdt), wdt->base + TWD_WDT_LOAD);
+}
+
+static void twd_wdt_cpu_counter(void *data)
+{
+ struct twd_wdt *wdt = data;
+
+ wdt->counter = readl(wdt->base + TWD_WDT_COUNTER);
+}
+
+static void twd_wdt_cpu_probe(void *data)
+{
+ struct twd_wdt *wdt = data;
+ u32 ctrl = readl(wdt->base + TWD_WDT_CONTROL);
+
+ wdt->running = (ctrl & (TWD_WDT_CTRL_WD_MODE | TWD_WDT_CTRL_ENABLE)) ==
+ (TWD_WDT_CTRL_WD_MODE | TWD_WDT_CTRL_ENABLE);
+
+ if (readl(wdt->base + TWD_WDT_RESET_STATUS) & TWD_WDT_RESET_FLAG) {
+ wdt->reset = true;
+ /* The flag is cleared by writing one */
+ writel(TWD_WDT_RESET_FLAG, wdt->base + TWD_WDT_RESET_STATUS);
+ }
+}
+
+static void twd_wdt_call(struct twd_wdt *wdt, smp_call_func_t func)
+{
+ smp_call_function_single(TWD_WDT_CPU, func, wdt, 1);
+}
+
+static void twd_wdt_set_armed(struct twd_wdt *wdt, bool armed)
+{
+ guard(mutex)(&wdt->lock);
+
+ atomic64_set(&wdt->keepalive, ktime_get_ns());
+ WRITE_ONCE(wdt->armed, armed);
+ twd_wdt_call(wdt, armed ? twd_wdt_cpu_start : twd_wdt_cpu_stop);
+}
+
+static int twd_wdt_start(struct watchdog_device *wdd)
+{
+ twd_wdt_set_armed(watchdog_get_drvdata(wdd), true);
+
+ return 0;
+}
+
+static int twd_wdt_stop(struct watchdog_device *wdd)
+{
+ twd_wdt_set_armed(watchdog_get_drvdata(wdd), false);
+
+ return 0;
+}
+
+static int twd_wdt_ping(struct watchdog_device *wdd)
+{
+ struct twd_wdt *wdt = watchdog_get_drvdata(wdd);
+
+ guard(mutex)(&wdt->lock);
+
+ atomic64_set(&wdt->keepalive, ktime_get_ns());
+ twd_wdt_call(wdt, twd_wdt_cpu_ping);
+
+ return 0;
+}
+
+static int twd_wdt_set_timeout(struct watchdog_device *wdd, unsigned int t)
+{
+ wdd->timeout = t;
+ if (watchdog_active(wdd) || watchdog_hw_running(wdd))
+ twd_wdt_ping(wdd);
+
+ return 0;
+}
+
+static unsigned int twd_wdt_get_timeleft(struct watchdog_device *wdd)
+{
+ struct twd_wdt *wdt = watchdog_get_drvdata(wdd);
+ u64 ticks;
+
+ guard(mutex)(&wdt->lock);
+
+ twd_wdt_call(wdt, twd_wdt_cpu_counter);
+ ticks = (u64)wdt->counter * (TWD_WDT_PRESCALE + 1);
+
+ return div_u64(ticks, wdt->rate);
+}
+
+/*
+ * The TWD runs from the peripheral clock, which follows the CPU clock. While
+ * the rate goes up, count with the new rate already: the watchdog then just
+ * fires later than asked until the change is done.
+ */
+static int twd_wdt_clk_notify(struct notifier_block *nb, unsigned long event,
+ void *data)
+{
+ struct twd_wdt *wdt = container_of(nb, struct twd_wdt, clk_nb);
+ struct clk_notifier_data *cnd = data;
+ unsigned long rate;
+
+ switch (event) {
+ case PRE_RATE_CHANGE:
+ if (cnd->new_rate <= cnd->old_rate)
+ return NOTIFY_OK;
+ rate = cnd->new_rate;
+ break;
+ case POST_RATE_CHANGE:
+ rate = cnd->new_rate;
+ break;
+ case ABORT_RATE_CHANGE:
+ rate = cnd->old_rate;
+ break;
+ default:
+ return NOTIFY_DONE;
+ }
+
+ guard(mutex)(&wdt->lock);
+
+ if (!rate || rate == wdt->rate)
+ return NOTIFY_OK;
+
+ WRITE_ONCE(wdt->rate, rate);
+ if (wdt->armed)
+ twd_wdt_call(wdt, twd_wdt_cpu_ping);
+
+ return NOTIFY_OK;
+}
+
+/*
+ * A CPU whose power domain was turned off in idle comes back with its
+ * watchdog stopped. Only then set it up again: reloading a watchdog that
+ * kept running would ping it on every idle exit.
+ */
+static int twd_wdt_cpu_pm_notify(struct notifier_block *nb,
+ unsigned long action, void *data)
+{
+ struct twd_wdt *wdt = container_of(nb, struct twd_wdt, pm_nb);
+
+ if (action != CPU_PM_EXIT && action != CPU_PM_ENTER_FAILED)
+ return NOTIFY_DONE;
+
+ if (smp_processor_id() == TWD_WDT_CPU && READ_ONCE(wdt->armed) &&
+ !(readl(wdt->base + TWD_WDT_CONTROL) & TWD_WDT_CTRL_WD_MODE))
+ twd_wdt_cpu_start(wdt);
+
+ return NOTIFY_OK;
+}
+
+static int twd_wdt_restart(struct watchdog_device *wdd, unsigned long action,
+ void *data)
+{
+ struct twd_wdt *wdt = watchdog_get_drvdata(wdd);
+
+ /*
+ * Only this CPU is still running, and it may not be TWD_WDT_CPU. The
+ * watchdog of any CPU resets the system: let its own expire right away.
+ */
+ writel(1, wdt->base + TWD_WDT_LOAD);
+ writel(TWD_WDT_CTRL_WD_MODE | TWD_WDT_CTRL_ENABLE,
+ wdt->base + TWD_WDT_CONTROL);
+ mdelay(500);
+
+ return 0;
+}
+
+static const struct watchdog_ops twd_wdt_ops = {
+ .owner = THIS_MODULE,
+ .start = twd_wdt_start,
+ .stop = twd_wdt_stop,
+ .ping = twd_wdt_ping,
+ .set_timeout = twd_wdt_set_timeout,
+ .get_timeleft = twd_wdt_get_timeleft,
+ .restart = twd_wdt_restart,
+};
+
+static const struct watchdog_info twd_wdt_info = {
+ .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
+ WDIOF_MAGICCLOSE | WDIOF_CARDRESET,
+ .identity = "ARM TWD watchdog",
+};
+
+/* A watchdog left running would not be pinged any more */
+static void twd_wdt_disable(void *data)
+{
+ struct twd_wdt *wdt = data;
+
+ twd_wdt_set_armed(wdt, false);
+}
+
+static void twd_wdt_remove_pm_notifier(void *data)
+{
+ struct twd_wdt *wdt = data;
+
+ cpu_pm_unregister_notifier(&wdt->pm_nb);
+}
+
+static int twd_wdt_probe(struct platform_device *pdev)
+{
+ struct device *dev = &pdev->dev;
+ struct twd_wdt *wdt;
+ struct clk *clk;
+ int ret;
+
+ if (cpu_is_hotpluggable(TWD_WDT_CPU))
+ return dev_err_probe(dev, -EOPNOTSUPP,
+ "CPU%d can be taken offline\n", TWD_WDT_CPU);
+
+ wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL);
+ if (!wdt)
+ return -ENOMEM;
+
+ ret = devm_mutex_init(dev, &wdt->lock);
+ if (ret)
+ return ret;
+
+ wdt->base = devm_platform_ioremap_resource(pdev, 0);
+ if (IS_ERR(wdt->base))
+ return PTR_ERR(wdt->base);
+
+ clk = devm_clk_get_enabled(dev, NULL);
+ if (IS_ERR(clk))
+ return dev_err_probe(dev, PTR_ERR(clk), "failed to get clock\n");
+
+ wdt->rate = clk_get_rate(clk);
+ if (!wdt->rate)
+ return dev_err_probe(dev, -EINVAL, "clock rate is zero\n");
+
+ wdt->clk_nb.notifier_call = twd_wdt_clk_notify;
+ ret = devm_clk_notifier_register(dev, clk, &wdt->clk_nb);
+ if (ret)
+ return dev_err_probe(dev, ret, "failed to register clock notifier\n");
+ platform_set_drvdata(pdev, wdt);
+
+ wdt->wdd.info = &twd_wdt_info;
+ wdt->wdd.ops = &twd_wdt_ops;
+ wdt->wdd.parent = dev;
+ wdt->wdd.min_timeout = 1;
+ wdt->wdd.max_timeout = div_u64((u64)U32_MAX * (TWD_WDT_PRESCALE + 1),
+ wdt->rate);
+ wdt->wdd.timeout = min_t(unsigned int, TWD_WDT_DEFAULT_TIMEOUT,
+ wdt->wdd.max_timeout);
+ watchdog_init_timeout(&wdt->wdd, timeout, dev);
+ watchdog_set_nowayout(&wdt->wdd, nowayout);
+ watchdog_stop_on_reboot(&wdt->wdd);
+ watchdog_stop_on_unregister(&wdt->wdd);
+ watchdog_set_drvdata(&wdt->wdd, wdt);
+
+ twd_wdt_call(wdt, twd_wdt_cpu_probe);
+ if (wdt->reset) {
+ wdt->wdd.bootstatus = WDIOF_CARDRESET;
+ dev_info(dev, "last reset caused by the watchdog\n");
+ }
+
+ if (wdt->running) {
+ /* Left running by the bootloader: take it over without a stop */
+ dev_info(dev, "running, taking over\n");
+ twd_wdt_start(&wdt->wdd);
+ set_bit(WDOG_HW_RUNNING, &wdt->wdd.status);
+ }
+
+ wdt->pm_nb.notifier_call = twd_wdt_cpu_pm_notify;
+ ret = cpu_pm_register_notifier(&wdt->pm_nb);
+ if (ret)
+ return ret;
+
+ ret = devm_add_action_or_reset(dev, twd_wdt_remove_pm_notifier, wdt);
+ if (ret)
+ return ret;
+
+ ret = devm_add_action_or_reset(dev, twd_wdt_disable, wdt);
+ if (ret)
+ return ret;
+
+ ret = devm_watchdog_register_device(dev, &wdt->wdd);
+ if (ret)
+ return ret;
+
+ dev_dbg(dev, "timeout %u s (max %u s), clock %lu Hz\n",
+ wdt->wdd.timeout, wdt->wdd.max_timeout, wdt->rate);
+
+ return 0;
+}
+
+static const struct of_device_id twd_wdt_of_match[] = {
+ { .compatible = "arm,arm11mp-twd-wdt" },
+ { .compatible = "arm,cortex-a5-twd-wdt" },
+ { .compatible = "arm,cortex-a9-twd-wdt" },
+ { }
+};
+MODULE_DEVICE_TABLE(of, twd_wdt_of_match);
+
+/* The watchdog loses its state in suspend, set it up again at resume */
+static int twd_wdt_suspend(struct device *dev)
+{
+ struct twd_wdt *wdt = dev_get_drvdata(dev);
+
+ wdt->resume_armed = wdt->armed;
+ if (wdt->resume_armed)
+ twd_wdt_stop(&wdt->wdd);
+
+ return 0;
+}
+
+static int twd_wdt_resume(struct device *dev)
+{
+ struct twd_wdt *wdt = dev_get_drvdata(dev);
+
+ if (wdt->resume_armed)
+ twd_wdt_start(&wdt->wdd);
+
+ return 0;
+}
+
+static DEFINE_SIMPLE_DEV_PM_OPS(twd_wdt_pm_ops, twd_wdt_suspend, twd_wdt_resume);
+
+static struct platform_driver twd_wdt_driver = {
+ .probe = twd_wdt_probe,
+ .driver = {
+ .name = "arm-twd-wdt",
+ .of_match_table = twd_wdt_of_match,
+ .pm = pm_sleep_ptr(&twd_wdt_pm_ops),
+ },
+};
+module_platform_driver(twd_wdt_driver);
+
+MODULE_DESCRIPTION("ARM TWD (MPCore private) watchdog driver");
+MODULE_LICENSE("GPL");
---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20261015-arm-twd-wdt-71ff5cef355b
Best regards,
--
Fil Dunsky <filipp.dunsky@gmail.com>
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH v5] watchdog: Add ARM TWD (MPCore private) watchdog driver
2026-11-18 8:50 ` Fil Dunsky
(?)
@ 2026-11-18 9:12 ` sashiko-bot
-1 siblings, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-11-18 9:12 UTC (permalink / raw)
To: Fil Dunsky; +Cc: linux-watchdog
Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [Medium] watchdog: arm_twd_wdt: race in twd_wdt_set_timeout() causes
immediate reset
- [Medium] watchdog: arm_twd_wdt: twd_wdt_probe() arms watchdog before
cleanup setup
- [Medium] watchdog: arm_twd_wdt: keepalive reset and active ping worker
on suspend
- [Medium] watchdog: arm_twd_wdt: missing barrier between keepalive and
armed state
- [Low] watchdog: arm_twd_wdt: use of obsolete __MODULE_STRING() macro
--
commit 2b76b6ecd425333171034f709700b36407dec65d
Author: Fil Dunsky <filipp.dunsky@gmail.com>
watchdog: Add ARM TWD (MPCore private) watchdog driver
This commit adds a watchdog driver for the ARM11 MPCore, Cortex-A5 and
Cortex-A9 private (TWD) watchdog, using CPU0's watchdog as the system
watchdog and registering a restart handler.
> diff --git a/drivers/watchdog/arm_twd_wdt.c b/drivers/watchdog/arm_twd_wdt.c
> new file mode 100644
> index 0000000000000..068b6483a8ac3
> --- /dev/null
> +++ b/drivers/watchdog/arm_twd_wdt.c
> @@ -0,0 +1,465 @@
[ ... ]
> +static bool nowayout = WATCHDOG_NOWAYOUT;
> +module_param(nowayout, bool, 0);
> +MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
> + __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
[Severity: Low]
__MODULE_STRING() is marked obsolete in include/linux/module.h, and commit
b152ca999c5f ("watchdog: Replace __MODULE_STRING() with __stringify()")
removed all other uses in drivers/watchdog/. Could this include
<linux/stringify.h> and use __stringify(WATCHDOG_NOWAYOUT) instead?
[ ... ]
> +static u32 twd_wdt_ticks(struct twd_wdt *wdt)
> +{
> + s64 expires = atomic64_read(&wdt->keepalive) +
> + (s64)wdt->wdd.timeout * NSEC_PER_SEC;
> + s64 left = expires - ktime_get_ns();
> + u64 ticks;
> +
> + if (left <= 0)
> + return 1;
> +
> + ticks = mul_u64_u64_div_u64(READ_ONCE(wdt->rate), left,
> + (u64)NSEC_PER_SEC * (TWD_WDT_PRESCALE + 1));
> +
> + return clamp_t(u64, ticks, 1, U32_MAX);
> +}
[ ... ]
> +static void twd_wdt_set_armed(struct twd_wdt *wdt, bool armed)
> +{
> + guard(mutex)(&wdt->lock);
> +
> + atomic64_set(&wdt->keepalive, ktime_get_ns());
> + WRITE_ONCE(wdt->armed, armed);
> + twd_wdt_call(wdt, armed ? twd_wdt_cpu_start : twd_wdt_cpu_stop);
> +}
[Severity: Medium]
Can CPU0 in twd_wdt_cpu_pm_notify() observe wdt->armed == true before the
new wdt->keepalive value and trigger an immediate reset?
On 32-bit ARM, atomic64_set() and atomic64_read() are relaxed and include
no memory barriers. If twd_wdt_start() runs on CPU1 while CPU0 is exiting
a power-down idle state with interrupts disabled, the IPI from
twd_wdt_call() waits until CPU0 re-enables interrupts:
CPU1 CPU0 (IRQs disabled)
twd_wdt_start() cpu_pm_exit()
twd_wdt_set_armed(..., true) twd_wdt_cpu_pm_notify()
atomic64_set(&wdt->keepalive, if (READ_ONCE(wdt->armed) && ...)
now) twd_wdt_cpu_start()
WRITE_ONCE(wdt->armed, true) twd_wdt_ticks()
twd_wdt_call(...) atomic64_read(&wdt->keepalive)
--> sees 0 or stale timestamp
left <= 0 -> returns 1
writel(1, TWD_WDT_LOAD) <-- reset
Pairing smp_store_release(&wdt->armed, armed) in twd_wdt_set_armed() with
smp_load_acquire(&wdt->armed) in twd_wdt_cpu_pm_notify() would order the
keepalive store before wdt->armed becomes visible.
[ ... ]
> +static int twd_wdt_set_timeout(struct watchdog_device *wdd, unsigned int t)
> +{
> + wdd->timeout = t;
> + if (watchdog_active(wdd) || watchdog_hw_running(wdd))
> + twd_wdt_ping(wdd);
> +
> + return 0;
> +}
[Severity: Medium]
Can decreasing wdd->timeout in twd_wdt_set_timeout() reset the board
immediately if a clock rate change or CPU0 idle exit happens before
twd_wdt_ping() updates wdt->keepalive?
watchdog_set_timeout() holds wd_data->lock, not wdt->lock. When userspace
reduces the timeout (for example from 60s to 30s when 35s have elapsed
since the last keepalive), wdd->timeout is updated before twd_wdt_ping()
acquires wdt->lock and refreshes wdt->keepalive:
CPU0 CPU1
twd_wdt_clk_notify() twd_wdt_set_timeout(wdd, 30)
guard(mutex)(&wdt->lock) wdd->timeout = 30;
twd_wdt_ping()
guard(mutex)(&wdt->lock) [blocks]
twd_wdt_call(...,
twd_wdt_cpu_ping)
twd_wdt_ticks()
expires = old_keepalive + 30s
left = -5s <= 0 -> returns 1
writel(1, TWD_WDT_LOAD) <-- immediate reset
In addition, watchdog_ioctl() for WDIOC_SETTIMEOUT in
drivers/watchdog/watchdog_dev.c already calls watchdog_ping(wdd) right
after watchdog_set_timeout(), so calling twd_wdt_ping() here sends a
redundant IPI to CPU0 on every WDIOC_SETTIMEOUT ioctl.
[ ... ]
> + if (wdt->running) {
> + /* Left running by the bootloader: take it over without a stop */
> + dev_info(dev, "running, taking over\n");
> + twd_wdt_start(&wdt->wdd);
> + set_bit(WDOG_HW_RUNNING, &wdt->wdd.status);
> + }
> +
> + wdt->pm_nb.notifier_call = twd_wdt_cpu_pm_notify;
> + ret = cpu_pm_register_notifier(&wdt->pm_nb);
> + if (ret)
> + return ret;
> +
> + ret = devm_add_action_or_reset(dev, twd_wdt_remove_pm_notifier, wdt);
> + if (ret)
> + return ret;
> +
> + ret = devm_add_action_or_reset(dev, twd_wdt_disable, wdt);
> + if (ret)
> + return ret;
[Severity: Medium]
If cpu_pm_register_notifier() or the first devm_add_action_or_reset()
fails when wdt->running is true, does twd_wdt_probe() return with the
reprogrammed watchdog still armed and its clock disabled by devres?
twd_wdt_start() reprograms the hardware counter to wdd.timeout (default
30s, which may be much shorter than the bootloader's timeout) before the
twd_wdt_disable cleanup action and CPU PM notifier are registered.
Registering the CPU PM notifier and twd_wdt_disable action before taking
over the running watchdog would avoid leaving it armed on error or losing
its state if CPU0 enters idle during probe.
[ ... ]
> +/* The watchdog loses its state in suspend, set it up again at resume */
> +static int twd_wdt_suspend(struct device *dev)
> +{
> + struct twd_wdt *wdt = dev_get_drvdata(dev);
> +
> + wdt->resume_armed = wdt->armed;
> + if (wdt->resume_armed)
> + twd_wdt_stop(&wdt->wdd);
> +
> + return 0;
> +}
> +
> +static int twd_wdt_resume(struct device *dev)
> +{
> + struct twd_wdt *wdt = dev_get_drvdata(dev);
> +
> + if (wdt->resume_armed)
> + twd_wdt_start(&wdt->wdd);
> +
> + return 0;
> +}
[Severity: Medium]
Does calling twd_wdt_stop() and twd_wdt_start() across suspend and resume
reset wdt->keepalive to ktime_get_ns(), contrary to the commit message?
The commit message notes that on resume from suspend the time of the last
keepalive is kept and the watchdog is loaded with the time left from it so
the timeout is not extended. However, twd_wdt_stop() and twd_wdt_start()
both call twd_wdt_set_armed(), which overwrites wdt->keepalive with
ktime_get_ns() and reloads a full timeout on resume.
Also, twd_wdt_probe() does not call watchdog_stop_ping_on_suspend(), and
twd_wdt_suspend() reads wdt->armed without wdt->lock while leaving
WDOG_HW_RUNNING set, so watchdog_ping_work() can run after
twd_wdt_suspend() and write TWD_WDT_LOAD while the device is suspended.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261118-arm-twd-wdt-v5-1-886265008e78@gmail.com?part=1
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-18 9:12 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-18 8:50 [PATCH v5] watchdog: Add ARM TWD (MPCore private) watchdog driver Fil Dunsky via B4 Relay
2026-11-18 8:50 ` Fil Dunsky
2026-11-18 9:12 ` sashiko-bot
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.