Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.171 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795483718; b=oHe3GKcKZHcKuWbLFo+3Cdp+5+mfB+zk/rB9BRwny/SO2uXdcG3QycjwTxaj/siUv+qS M4d0uYVqrtjnjSyIKzKsszX9lg+PYJrTDTFh3VFfPRw1w7Pdpmpz/y76he+ZljW7lyINK ccxU69iLfsAHvUrfFm3kOxAJAFJWWdkXgHrg8TuwQ93EFyz6z+i4RyjXsJ6SOM8412O6K hTFIm6OZaSxiVsMF+6jprKqjBczZZqezQ/9BBiIYgOai7shiQanv3dPPpi3aFPkrlIO0H +y65DRMRcxrM1aE1k1+ygaIdmdtqQAilJaXxaI1Af40rFbNfMLJNiVj/xDpUXqho1eA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795483718; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=fh0x5VQQJAWMGfm2v5ASdgMgkGoChGrLjbjXEFbyGzQ=; b=Yn/wxNiWaY9t2A+wgRNUIn0U8UwpzsxQn8jV0MY0SWQ0yhv8q3D0SAV2opsVs6rBMr/3 kpDgGBWLdK9Yoxmki5XLHHQcDkzO30k0PW8umHkc12LQoa7IDuD1d5UTgba1Yly2ydrBk PE3NuThYX4gRfxTDkWRX9aW4JH8p3qniFAQ3ff4LtvXG7ZRAsPSbPm6MHzQSKV51CiOcw TYD3Mbyt+lOgNdxW5Bib0M5HyCF0MLMr7LAF4H+ZwzOD0HN/rvmYvUK035p3WgkOfjqis hCHWYB19dg/LoEmKtwx2igB5js4W8ScNasE4SFLqrny0GWUT5tRPWOnBUOAwo6zdrCA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=olXbCxTn; arc=none smtp.remote-ip=209.85.210.171 Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.171]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 366E7606A32A for ; Tue, 24 Nov 2026 01:28:37 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 366E7606A32A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=olXbCxTn Received: by mail-pf1-f171.google.com with SMTP id d2e1a72fcca58-8c5ebbc5150so2767078b3a.3 for ; Mon, 23 Nov 2026 17:28:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795483717; x=1796088517; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=fh0x5VQQJAWMGfm2v5ASdgMgkGoChGrLjbjXEFbyGzQ=; b=olXbCxTn1RDLkyz937SafHWBqMmoo/fGOy6wXqNzAazQuj4bc2tiu4ErWt1mNcUSq+ 2oBHTvIl5WAuvE4awPGa9LsL8WVHkhal4MKXgt/hJ67Pa3huLpbQy2+J7GJobcljfr2M nMqMbHVwh5WAVutkcA8axYYS+bfxZHGhIxxhheQ1W9I00Pd2a2EFSvLPUlbz0EldSatA sm1Wx1u5Zqa0+7J4q00ey6DEe9o9hcc0IQ9XFdIQmKlfr8o+Cdlaxs32HjLym2UODKVJ Vedk6VpJM1Z6CFF6ccbGjH/e/zBKqG16B/gdD8t+21puJaUtvgof21L50kw1BZ9ZWhXP 3AWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795483717; x=1796088517; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=fh0x5VQQJAWMGfm2v5ASdgMgkGoChGrLjbjXEFbyGzQ=; b=qm4ezt0zOeAbsxCqPcfFIZjC9wvgm2BfC94IpGdapsfoaXWN8XMnDrIB9Cunc/MKzL 1tvuil5DdCUN2b4Y1GRPFdkmYAAj3bJbcG8MuVMlPOFF/g+nvbv0w2wwrUDkxu9H64YH KvFg9SE//ck+xMJk+WMAcPZ+cSsdkrMUIAMBo55uXLpcxSiZuKeEc9lcph82GmNWaTxf e9yP8j0B9CMDe9mkr+OlKg+SsW7QDkCTibIK4GlmMOam6f8JXFkmz/XkTB5Nj96MmUR5 xuzbS6Ry84fb2xgXQW1rRaKcs5hmkkxNd65SgTOAiC6k6GG4ACif+MyuOCcaivd0eyR+ CXYg== X-Forwarded-Encrypted: i=1; AL+hErFaHJhSMIjZ+AYOvNPpnqrd/ESmY6a8yauT6Yl0oS9Ca1Y5hjNj4SaIV8XKyNpvNCEf6mmeRHwC2INd@vger.kernel.org X-Gm-Message-State: APuU4Z8Msx9zMVDsNzDwGIJ1rqYMf2cHySZBuHT3g8Z6ht5bPcKDFsgI eGqV0ilHJLKT3ew9bbf1AqhgtSWo75+ApnC+DO6GNB0CMnPhw4fJo5rV X-Gm-Gg: AZFdTZc5HXMskYwGUifgKmpehmduNacTaxl/l//Dfsb/t8wA4vwzLYDPBBKbfLfcZtz m/DT6k7Drd+rUybDWqYhLDv+FH8Mfd3mw9/3tiwKwNbs7mcns6MdFCoGfM3DMzdQp5wP70wr9DB zyAtrgplytBn4ehkB6KEpfcIDhf7RZ8Uvb1v1FuNb4h3IzmcOUvOQ+kU6yZZNaMiSWjmv6WG6zV yndMZtF9cCbzj1wxx0ARP6e569Kg4bU4fww2Xs7FgLmroScJ90tKZNMZt7uZQN851wi+syTZ+tW qGYJwBNNXJv1PQZUH4079vMtjUM25gEaOuegxFaekTGN/9v38RkaN1cfi1VudRjJ5bPbMAJyOmW Rzc4sDMvENHvvU0RwxiESqktMbeXixNhYW/+8FhqIRm8wdVkTJ59gM6PHpoeDCTCVjPhByvOMHy 9wtpnlqWq6h+vacimMjVnkDwyl7i7Ws9Swdk+DsyB9M5564lOYtqb5ehfZFNol3aHwd2ej3Wjty E8jV5BH6fIrYCOzCy5PT85J+/SbGF2eLxPwOAF/QCUUFSL7yiqNYSl6a8Z4xvGHi0HVqwP86gl9 emzSLfoam2iXCBTbZSPve+7CukPtsOOR/es8qDP5rVQuO+lUGrAHple5C/U0JEH3YQRN8m9kviA H X-Received: by 2002:a05:6a00:a117:b0:883:71f6:5fb with SMTP id d2e1a72fcca58-8c87b4f42ccmr11288308b3a.13.1795483717212; Mon, 23 Nov 2026 17:28:37 -0800 (PST) Received: from d.home.yangfl.dn42 ([2a09:bac1:7680:d30::4d0:8e]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-8c872e457d1sm6115243b3a.30.2026.11.23.17.28.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Nov 2026 17:28:36 -0800 (PST) From: David Yang To: linux-watchdog@vger.kernel.org Cc: David Yang , Wim Van Sebroeck , Guenter Roeck , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Wei Xu , 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 Message-ID: <20261124012755.328774-3-mmyangfl@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261124012755.328774-1-mmyangfl@gmail.com> References: <20261124012755.328774-1-mmyangfl@gmail.com> X-Mailing-List: devicetree@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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 "); +MODULE_DESCRIPTION("HiSilicon STB Watchdog Driver"); +MODULE_LICENSE("Dual MIT/GPL"); -- 2.53.0