Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E4294C0015E for ; Wed, 26 Jul 2023 17:22:49 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id C9A8D86878; Wed, 26 Jul 2023 19:22:18 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=hodcarrier.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Received: by phobos.denx.de (Postfix, from userid 109) id 7454A86823; Wed, 26 Jul 2023 19:12:40 +0200 (CEST) Received: from p3plsmtpa12-08.prod.phx3.secureserver.net (p3plsmtpa12-08.prod.phx3.secureserver.net [68.178.252.237]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 089EF86845 for ; Wed, 26 Jul 2023 19:12:36 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=hodcarrier.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=dhu@hodcarrier.org Received: from localhost.localdomain ([14.16.17.17]) by :SMTPAUTH: with ESMTPSA id Odf0qOS57nXwFOdfKqpWVM; Wed, 26 Jul 2023 05:30:56 -0700 X-CMAE-Analysis: v=2.4 cv=NbEglUP4 c=1 sm=1 tr=0 ts=64c11201 a=34c9DTqauJ9c0AOVdCw1kg==:117 a=34c9DTqauJ9c0AOVdCw1kg==:17 a=_twTT5zqAAAA:8 a=mpaa-ttXAAAA:8 a=zNf1Ww-9SZkPJvXkbD4A:9 a=ILoXdGDbYT3DTB7Z0gVI:22 a=6heAxKwa5pAsJatQ0mat:22 X-SECURESERVER-ACCT: dhu@hodcarrier.org From: Du Huanpeng To: u-boot@lists.denx.de Cc: u74147@gmail.com, Du Huanpeng Subject: [PATCH v5 5/6] mips: ls1c300: add watchdog support Date: Wed, 26 Jul 2023 20:30:26 +0800 Message-Id: <20230726123027.1536-6-dhu@hodcarrier.org> X-Mailer: git-send-email 2.40.0.windows.1 In-Reply-To: <20230726123027.1536-1-dhu@hodcarrier.org> References: <20230726123027.1536-1-dhu@hodcarrier.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CMAE-Envelope: MS4xfIIKRRDI7fXKZ0DfC3IxA80TCEbtbynwKoFZsXmFk590M1SxasBBYmUiYY/qEGmU7SNrd11irDXM/lvn62FS69oUZkTbM6FvEup/SjZsSCf+TQ1IJi+G qSAPbzx1MTXcMHPl4NhjeQvKZq9OEd8ME2knCX5Gu0qL5xlGuTcG9sArtynORtdFU56vwK2VJUnyRUeaXBVty/ljk2uS1ntafMw2+EBM/uzvmS0qBCNobbtJ S52cJjMlIgyZU00qZIVlpw== X-Mailman-Approved-At: Wed, 26 Jul 2023 19:21:57 +0200 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean - watchdog driver for ls1c300 with devicetree support Signed-off-by: Du Huanpeng --- drivers/watchdog/Kconfig | 8 +++ drivers/watchdog/Makefile | 1 + drivers/watchdog/lsmips_wdt.c | 128 ++++++++++++++++++++++++++++++++++ 3 files changed, 137 insertions(+) create mode 100644 drivers/watchdog/lsmips_wdt.c diff --git a/drivers/watchdog/Kconfig b/drivers/watchdog/Kconfig index 646663528a..f60011e5b3 100644 --- a/drivers/watchdog/Kconfig +++ b/drivers/watchdog/Kconfig @@ -171,6 +171,14 @@ config WDT_GPIO doc/device-tree-bindings/watchdog/gpio-wdt.txt for information on how to describe the watchdog in device tree. +config WDT_LSMIPS + bool "Loongson MIPS watchdog timer support" + depends on WDT + help + Select this to enable watchdog timer for Loongson SoCs. + The watchdog timer is stopped when initialized. + It performs full SoC reset. + config WDT_MAX6370 bool "MAX6370 watchdog timer support" depends on WDT diff --git a/drivers/watchdog/Makefile b/drivers/watchdog/Makefile index fd5d9c7376..8afd61a4fc 100644 --- a/drivers/watchdog/Makefile +++ b/drivers/watchdog/Makefile @@ -30,6 +30,7 @@ obj-$(CONFIG_WDT_ORION) += orion_wdt.o obj-$(CONFIG_WDT_CDNS) += cdns_wdt.o obj-$(CONFIG_WDT_FTWDT010) += ftwdt010_wdt.o obj-$(CONFIG_WDT_GPIO) += gpio_wdt.o +obj-$(CONFIG_WDT_LSMIPS) += lsmips_wdt.o obj-$(CONFIG_WDT_MAX6370) += max6370_wdt.o obj-$(CONFIG_WDT_MESON_GXBB) += meson_gxbb_wdt.o obj-$(CONFIG_WDT_MPC8xxx) += mpc8xxx_wdt.o diff --git a/drivers/watchdog/lsmips_wdt.c b/drivers/watchdog/lsmips_wdt.c new file mode 100644 index 0000000000..4a813a40bb --- /dev/null +++ b/drivers/watchdog/lsmips_wdt.c @@ -0,0 +1,128 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Watchdog driver for MediaTek SoCs + * + * Copyright (C) 2018 MediaTek Inc. + * Author: Ryder Lee + * + * based on: drivers/watchdog/mtk_wdt.c + * Copyright (C) 2020-2023 Du Huanpeng + */ + +#include +#include +#include +#include +#include +#include +#include + + +struct lsmips_wdt_priv { + void __iomem *base; +#define WDT_EN 0 +#define WDT_TIMER 4 +#define WDT_SET 8 + ulong clock; + unsigned long timeout; +}; + +static int lsmips_wdt_reset(struct udevice *dev) +{ + struct lsmips_wdt_priv *priv = dev_get_priv(dev); + + writel(priv->timeout, priv->base + WDT_TIMER); + writel(1, priv->base + WDT_SET); + + return 0; +} + +static int lsmips_wdt_stop(struct udevice *dev) +{ + struct lsmips_wdt_priv *priv = dev_get_priv(dev); + + writel(0, priv->base + WDT_EN); + return 0; +} + +static int lsmips_wdt_expire_now(struct udevice *dev, ulong flags) +{ + struct lsmips_wdt_priv *priv = dev_get_priv(dev); + + writel(1, priv->base + WDT_EN); + writel(1, priv->base + WDT_TIMER); + writel(1, priv->base + WDT_SET); + + hang(); + return 0; +} + +static int lsmips_wdt_start(struct udevice *dev, u64 timeout_ms, ulong flags) +{ + struct lsmips_wdt_priv *priv = dev_get_priv(dev); + unsigned int timeout; + + timeout = U32_MAX / (priv->clock / 1000UL); + + if (timeout < timeout_ms) + timeout = U32_MAX; + else + timeout = timeout_ms * (priv->clock / 1000UL); + + debug("WDT: reload = %08x\n", timeout); + + writel(1, priv->base + WDT_EN); + writel(timeout, priv->base + WDT_TIMER); + writel(1, priv->base + WDT_SET); + + priv->timeout = timeout; + + return 0; +} + +static int lsmips_wdt_probe(struct udevice *dev) +{ + struct lsmips_wdt_priv *priv = dev_get_priv(dev); + struct clk cl; + ulong clock; + + priv->base = dev_remap_addr(dev); + if (!priv->base) + return -ENOENT; + + if (clk_get_by_index(dev, 0, &cl) == 0) + clock = clk_get_rate(&cl); + + debug("WDT: clock = %ld\n", clock); + + if (IS_ERR_VALUE(clock)) { + dev_err(dev, "failed to get rate\n"); + return clock; + } + + priv->clock = clock; + writel(0, priv->base + WDT_EN); + return 0; +} + +static const struct wdt_ops lsmips_wdt_ops = { + .start = lsmips_wdt_start, + .reset = lsmips_wdt_reset, + .stop = lsmips_wdt_stop, + .expire_now = lsmips_wdt_expire_now, +}; + +static const struct udevice_id lsmips_wdt_ids[] = { + { .compatible = "loongson,ls1c300-wdt"}, + {} +}; + +U_BOOT_DRIVER(lsmips_wdt) = { + .name = "lsmips_wdt", + .id = UCLASS_WDT, + .of_match = lsmips_wdt_ids, + .priv_auto = sizeof(struct lsmips_wdt_priv), + .probe = lsmips_wdt_probe, + .ops = &lsmips_wdt_ops, + .flags = DM_FLAG_PRE_RELOC, +}; -- 2.34.1