From: David Yang <mmyangfl@gmail.com>
To: linux-watchdog@vger.kernel.org
Cc: David Yang <mmyangfl@gmail.com>,
Wim Van Sebroeck <wim@linux-watchdog.org>,
Guenter Roeck <linux@roeck-us.net>, Rob Herring <robh@kernel.org>,
Krzysztof Kozlowski <krzk+dt@kernel.org>,
Conor Dooley <conor+dt@kernel.org>, Wei Xu <xuwei5@hisilicon.com>,
devicetree@vger.kernel.org, linux-kernel@vger.kernel.org,
linux-arm-kernel@lists.infradead.org
Subject: [PATCH v2 2/4] watchdog: histb: Add HiSilicon STB watchdog driver
Date: Tue, 24 Nov 2026 09:27:36 +0800 [thread overview]
Message-ID: <20261124012755.328774-3-mmyangfl@gmail.com> (raw)
In-Reply-To: <20261124012755.328774-1-mmyangfl@gmail.com>
The watchdog integrated on HiSilicon STB SoCs reuses the register layout
of sp805, but deviates from the standard sp805 in several ways that make
the existing sp805 driver unusable on these SoCs. Add a dedicated
platform driver to handle these SoC-specific semantics natively without
regressing generic SP805 users.
Signed-off-by: David Yang <mmyangfl@gmail.com>
---
drivers/watchdog/Kconfig | 8 +
drivers/watchdog/Makefile | 1 +
drivers/watchdog/histb_wdt.c | 305 +++++++++++++++++++++++++++++++++++
3 files changed, 314 insertions(+)
create mode 100644 drivers/watchdog/histb_wdt.c
diff --git a/drivers/watchdog/Kconfig b/drivers/watchdog/Kconfig
index f7d0a0c2c0ef..d3421bb76326 100644
--- a/drivers/watchdog/Kconfig
+++ b/drivers/watchdog/Kconfig
@@ -1183,6 +1183,14 @@ config SUNPLUS_WATCHDOG
To compile this driver as a module, choose M here: the
module will be called sunplus_wdt.
+config HISTB_WATCHDOG
+ tristate "HiSilicon STB Watchdog"
+ depends on (ARCH_HISI || COMPILE_TEST) && OF
+ select WATCHDOG_CORE
+ help
+ Say Y here to include support for the watchdog timer in HiSilicon STB
+ SoCs.
+
# X86 (i386 + ia64 + x86_64) Architecture
config ACQUIRE_WDT
diff --git a/drivers/watchdog/Makefile b/drivers/watchdog/Makefile
index 9ac406677aba..d2455e54ab23 100644
--- a/drivers/watchdog/Makefile
+++ b/drivers/watchdog/Makefile
@@ -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_HISTB_WATCHDOG) += histb_wdt.o
# X86 (i386 + ia64 + x86_64) Architecture
obj-$(CONFIG_ACQUIRE_WDT) += acquirewdt.o
diff --git a/drivers/watchdog/histb_wdt.c b/drivers/watchdog/histb_wdt.c
new file mode 100644
index 000000000000..456de7e92562
--- /dev/null
+++ b/drivers/watchdog/histb_wdt.c
@@ -0,0 +1,305 @@
+// SPDX-License-Identifier: GPL-2.0+ OR MIT
+/*
+ * HiSilicon STB Watchdog Driver
+ *
+ * The watchdog integrated on HiSilicon STB SoCs reuses the register layout of
+ * sp805, but deviates from the standard sp805 in several ways that make the
+ * existing sp805 driver unusable on these SoCs:
+ * - The ID registers at 0xfe0..0xffc are unimplemented and read zero,
+ * requiring a platform device driver rather than AMBA probe;
+ * - Writing WDTLOCK freezes the counter instead of only blocking register
+ * writes, so the block must remain unlocked while running;
+ * - Writing WDTLOAD never reloads the running counter; the counter takes
+ * WDTLOAD when re-armed by WDTINTCLR or by enabling WDTCONTROL;
+ * - Writing WDTLOAD with 0 results in a counter starting at 0xffffffff,
+ * resetting after 2*179s (~6min) at 24 MHz;
+ * - The counter only advances while unlocked and INT_ENABLE is set.
+ *
+ * Copyright (c) 2026 David Yang
+ */
+
+#include <linux/bitops.h>
+#include <linux/clk.h>
+#include <linux/delay.h>
+#include <linux/io.h>
+#include <linux/ioport.h>
+#include <linux/kernel.h>
+#include <linux/math64.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/pm.h>
+#include <linux/spinlock.h>
+#include <linux/watchdog.h>
+
+#define HISTB_WDT_LOAD 0x000
+#define HISTB_WDT_VALUE 0x004
+#define HISTB_WDT_CONTROL 0x008
+#define HISTB_WDT_RESET_ENABLE BIT(1)
+#define HISTB_WDT_INT_ENABLE BIT(0)
+#define HISTB_WDT_INTCLR 0x00c
+#define HISTB_WDT_RIS 0x010
+#define HISTB_WDT_MIS 0x014
+#define HISTB_WDT_INT_MASK BIT(0)
+#define HISTB_WDT_LOCK 0xc00
+#define HISTB_WDT_UNLOCK_KEY 0x1acce551
+#define HISTB_WDT_LOCK_KEY 0x00000001
+
+#define MHz 1000000
+
+/* default timeout in seconds */
+#define HISTB_WDT_DEFAULT_TIMEOUT 60
+
+struct histb_wdt {
+ struct watchdog_device wdd;
+
+ void __iomem *base;
+ struct clk *clk;
+ spinlock_t lock;
+ u32 rate;
+ u32 load_val;
+};
+
+/* load cannot be 0, see documentation */
+static void histb_wdt_arm(struct histb_wdt *wdt, u32 load)
+{
+ void __iomem *base = wdt->base;
+
+ writel_relaxed(HISTB_WDT_UNLOCK_KEY, base + HISTB_WDT_LOCK);
+ writel_relaxed(0, base + HISTB_WDT_CONTROL);
+ writel_relaxed(HISTB_WDT_INT_MASK, base + HISTB_WDT_INTCLR);
+ writel_relaxed(load, base + HISTB_WDT_LOAD);
+ writel_relaxed(HISTB_WDT_INT_ENABLE | HISTB_WDT_RESET_ENABLE,
+ base + HISTB_WDT_CONTROL);
+
+ /* Flush posted writes. */
+ readl_relaxed(base + HISTB_WDT_LOCK);
+}
+
+static int histb_wdt_start(struct watchdog_device *wdd)
+{
+ struct histb_wdt *wdt = watchdog_get_drvdata(wdd);
+ int res;
+
+ res = clk_enable(wdt->clk);
+ if (res)
+ return res;
+
+ spin_lock(&wdt->lock);
+ histb_wdt_arm(wdt, wdt->load_val);
+ spin_unlock(&wdt->lock);
+
+ return 0;
+}
+
+static int histb_wdt_stop(struct watchdog_device *wdd)
+{
+ struct histb_wdt *wdt = watchdog_get_drvdata(wdd);
+ void __iomem *base = wdt->base;
+
+ spin_lock(&wdt->lock);
+ writel_relaxed(HISTB_WDT_UNLOCK_KEY, base + HISTB_WDT_LOCK);
+ writel_relaxed(0, base + HISTB_WDT_CONTROL);
+ writel_relaxed(HISTB_WDT_LOCK_KEY, base + HISTB_WDT_LOCK);
+ readl_relaxed(base + HISTB_WDT_LOCK);
+ spin_unlock(&wdt->lock);
+
+ clk_disable(wdt->clk);
+
+ return 0;
+}
+
+static int histb_wdt_ping(struct watchdog_device *wdd)
+{
+ struct histb_wdt *wdt = watchdog_get_drvdata(wdd);
+ void __iomem *base = wdt->base;
+
+ spin_lock(&wdt->lock);
+ writel_relaxed(HISTB_WDT_INT_MASK, base + HISTB_WDT_INTCLR);
+ writel_relaxed(wdt->load_val, base + HISTB_WDT_LOAD);
+ readl_relaxed(base + HISTB_WDT_LOCK);
+ spin_unlock(&wdt->lock);
+
+ return 0;
+}
+
+static int
+histb_wdt_set_timeout(struct watchdog_device *wdd, unsigned int timeout)
+{
+ struct histb_wdt *wdt = watchdog_get_drvdata(wdd);
+ u64 rate = wdt->rate;
+ u64 load;
+
+ /*
+ * histb-wdt runs counter with given value twice, after the end of first
+ * counter it gives an interrupt and then starts counter again. If
+ * interrupt already occurred then it resets the system. This is why
+ * load is half of what should be required.
+ */
+ load = div_u64(rate * timeout, 2) - 1;
+ load = clamp_val(load, 1, U32_MAX);
+
+ wdt->load_val = load;
+ /* roundup timeout to closest positive integer value */
+ wdd->timeout = div_u64((load + 1) * 2 + (rate / 2), rate);
+
+ if (watchdog_active(wdd)) {
+ spin_lock(&wdt->lock);
+ histb_wdt_arm(wdt, wdt->load_val);
+ spin_unlock(&wdt->lock);
+ }
+
+ return 0;
+}
+
+static unsigned int histb_wdt_get_timeleft(struct watchdog_device *wdd)
+{
+ struct histb_wdt *wdt = watchdog_get_drvdata(wdd);
+ u64 value;
+
+ spin_lock(&wdt->lock);
+ value = readl_relaxed(wdt->base + HISTB_WDT_VALUE);
+ if (!(readl_relaxed(wdt->base + HISTB_WDT_RIS) & HISTB_WDT_INT_MASK))
+ value += (u64)wdt->load_val + 1;
+ spin_unlock(&wdt->lock);
+
+ return div_u64(value, wdt->rate);
+}
+
+static int
+histb_wdt_restart(struct watchdog_device *wdd, unsigned long mode, void *cmd)
+{
+ struct histb_wdt *wdt = watchdog_get_drvdata(wdd);
+
+ /*
+ * For reset, we start the WDT clock and leave it running.
+ * The clock was prepared at probe time; only clk_enable() is
+ * safe in this atomic (restart notifier) context.
+ */
+ clk_enable(wdt->clk);
+ /*
+ * Other CPUs may have been stopped while holding wdt->lock, arm without
+ * taking it.
+ */
+ histb_wdt_arm(wdt, 1);
+
+ mdelay(1);
+
+ return 0;
+}
+
+static const struct watchdog_info histb_wdt_info = {
+ .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
+ .identity = "HiSilicon STB Watchdog",
+};
+
+static const struct watchdog_ops histb_wdt_ops = {
+ .owner = THIS_MODULE,
+ .start = histb_wdt_start,
+ .stop = histb_wdt_stop,
+ .ping = histb_wdt_ping,
+ .set_timeout = histb_wdt_set_timeout,
+ .get_timeleft = histb_wdt_get_timeleft,
+ .restart = histb_wdt_restart,
+};
+
+static int histb_wdt_probe(struct platform_device *pdev)
+{
+ struct device *dev = &pdev->dev;
+ struct histb_wdt *wdt;
+ struct watchdog_device *wdd;
+ int res;
+
+ wdt = devm_kzalloc_obj(dev, *wdt);
+ if (!wdt)
+ return -ENOMEM;
+
+ wdt->base = devm_platform_ioremap_resource(pdev, 0);
+ if (IS_ERR(wdt->base))
+ return PTR_ERR(wdt->base);
+
+ /*
+ * Keep the clock prepared for the lifetime of the driver:
+ * .restart() runs in atomic context and may only call clk_enable().
+ */
+ wdt->clk = devm_clk_get_optional_prepared(dev, NULL);
+ if (IS_ERR(wdt->clk))
+ return dev_err_probe(dev, PTR_ERR(wdt->clk),
+ "Failed to get clock\n");
+
+ if (wdt->clk)
+ wdt->rate = clk_get_rate(wdt->clk);
+ if (!wdt->rate)
+ device_property_read_u32(dev, "clock-frequency", &wdt->rate);
+ if (!wdt->rate)
+ /* 24 MHz crystal clock default on HiSTB */
+ wdt->rate = 24 * MHz;
+
+ wdd = &wdt->wdd;
+ wdd->parent = dev;
+ wdd->info = &histb_wdt_info;
+ wdd->ops = &histb_wdt_ops;
+ wdd->timeout = HISTB_WDT_DEFAULT_TIMEOUT;
+ wdd->min_timeout = 1;
+ wdd->max_timeout = div_u64(((u64)U32_MAX + 1) * 2, wdt->rate);
+ watchdog_stop_on_reboot(wdd);
+ watchdog_stop_on_unregister(wdd);
+ watchdog_init_timeout(wdd, 0, dev);
+ watchdog_set_drvdata(wdd, wdt);
+
+ spin_lock_init(&wdt->lock);
+
+ histb_wdt_set_timeout(wdd, wdd->timeout);
+
+ platform_set_drvdata(pdev, wdt);
+
+ res = devm_watchdog_register_device(dev, wdd);
+ if (res)
+ return dev_err_probe(dev, res,
+ "Failed to register watchdog device\n");
+
+ return 0;
+}
+
+static int __maybe_unused histb_wdt_suspend(struct device *dev)
+{
+ struct histb_wdt *wdt = dev_get_drvdata(dev);
+
+ if (watchdog_active(&wdt->wdd))
+ histb_wdt_stop(&wdt->wdd);
+
+ return 0;
+}
+
+static int __maybe_unused histb_wdt_resume(struct device *dev)
+{
+ struct histb_wdt *wdt = dev_get_drvdata(dev);
+
+ if (watchdog_active(&wdt->wdd))
+ return histb_wdt_start(&wdt->wdd);
+
+ return 0;
+}
+
+static DEFINE_SIMPLE_DEV_PM_OPS(histb_wdt_pm_ops, histb_wdt_suspend,
+ histb_wdt_resume);
+
+static const struct of_device_id histb_wdt_match[] = {
+ { .compatible = "hisilicon,histb-wdt" },
+ {}
+};
+MODULE_DEVICE_TABLE(of, histb_wdt_match);
+
+static struct platform_driver histb_wdt_driver = {
+ .probe = histb_wdt_probe,
+ .driver = {
+ .name = "histb-wdt",
+ .pm = &histb_wdt_pm_ops,
+ .of_match_table = histb_wdt_match,
+ },
+};
+module_platform_driver(histb_wdt_driver);
+
+MODULE_AUTHOR("David Yang <mmyangfl@gmail.com>");
+MODULE_DESCRIPTION("HiSilicon STB Watchdog Driver");
+MODULE_LICENSE("Dual MIT/GPL");
--
2.53.0
next prev parent reply other threads:[~2026-11-24 1:42 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-24 1:27 [PATCH v2 0/4] watchdog: histb: Add HiSilicon STB watchdog driver David Yang
2026-11-24 1:27 ` [PATCH v2 1/4] dt-bindings: watchdog: hisilicon,histb-wdt: Add HiSilicon STB watchdog David Yang
2026-11-24 1:27 ` David Yang [this message]
2026-11-24 1:27 ` [PATCH v2 3/4] arm64: dts: hisilicon: hi3798cv200: Add watchdog node David Yang
2026-11-24 1:27 ` [PATCH v2 4/4] MAINTAINERS: Add HiSilicon STB drivers entry David Yang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261124012755.328774-3-mmyangfl@gmail.com \
--to=mmyangfl@gmail.com \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=krzk+dt@kernel.org \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-watchdog@vger.kernel.org \
--cc=linux@roeck-us.net \
--cc=robh@kernel.org \
--cc=wim@linux-watchdog.org \
--cc=xuwei5@hisilicon.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox