From patchwork Mon Mar 5 11:45:41 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joel Stanley X-Patchwork-Id: 130632 Delivered-To: patch@linaro.org Received: by 10.46.66.2 with SMTP id p2csp2587487lja; Mon, 5 Mar 2018 03:46:27 -0800 (PST) X-Google-Smtp-Source: AG47ELsP5vk7i0YHBLZTMyARdVKiPSQRL+vxw+lrZn1+nS+4rdlu7iuf+5eBYWS20kcoGaa9Nhgz X-Received: by 10.101.65.5 with SMTP id w5mr12029539pgp.214.1520250387846; Mon, 05 Mar 2018 03:46:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520250387; cv=none; d=google.com; s=arc-20160816; b=H4H4mJrZMkyV4KbMbm0N0sTQqTBi2Xy1UEG3h8MGL0xXZ+EwfE8AXbb/4sxB5oG987 BejxSHY/IqSEGigeyV8vg+E3lPv3u9v69U1ClJTy94So5EVw4cQn/10uDUEC0df0ex6M aoxBCek6q+DZga7Xuab9XGjpuEmKVtfXZPb5dqyiiyWh3ffcgCHpk8PLvGLxAzfmoC56 +RAHxU8YnAYvPdI8vm9EIjGVuUvR/M72TDgJvgSPJ43n6mYViZZQReIlCiqd5CXzDR3H 91VTPW1G42/MtBPwLCaZvRpShLDQlZJ4bD2C1JycWIkuToL7c0GsIx2WWmVvfs/U6b9B Huaw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=5f/GpU7i2ezGZ/l+RGq+FNKhscT2c8Sdi2ySuz+CylI=; b=mc9zfMxFA4GJ1idp/9Y+w8iZZxOoJrKbdg1qTuwAyY4KonxuvX0EY6swda/oq8OjpF iCey8Fxd+KfKwdMwir9GUyLcHRe+sFKbp+4wRfFlrLIkvuXooOKA75NaOFtoe+ZDPPpJ 6tu/6IXtKZzP8OtT9WLej9oXnR4x/Jq+Xqc3PnQVEH6TuXuIO3NOPE5KW09sb3EzEA7+ FtBFTXZ5OvwHFGUl3FxqpczeZY2cfw4QPgyCXb+JOAAybTlBASp1f8knd10p90Z4SQYA btDGx+65epPXxRI22xRaYB/ac4qJCo0C08vdOAg4hgPVd9R1ZaltgAhDX6c6N6oXIC/8 e8VA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=g5euFJnh; spf=pass (google.com: best guess record for domain of linux-watchdog-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-watchdog-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b8si1086794pgt.710.2018.03.05.03.46.27 for ; Mon, 05 Mar 2018 03:46:27 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-watchdog-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20161025 header.b=g5euFJnh; spf=pass (google.com: best guess record for domain of linux-watchdog-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-watchdog-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752679AbeCELq0 (ORCPT ); Mon, 5 Mar 2018 06:46:26 -0500 Received: from mail-pf0-f195.google.com ([209.85.192.195]:39659 "EHLO mail-pf0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752677AbeCELqV (ORCPT ); Mon, 5 Mar 2018 06:46:21 -0500 Received: by mail-pf0-f195.google.com with SMTP id u5so7069073pfh.6; Mon, 05 Mar 2018 03:46:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=IzDndUcZlu6mSmHo8/ONFdTqbWQc7l+ehwquiaRqPYI=; b=g5euFJnhFNIB/M7rcCMCy0SLOu2gzmpqY+iMagD3h8tO4IV8SfAxGOqS8VtCuXcrhp BU5crV4c1m25hlMxbl2dxN+6bAvMd1nrOCtcfr30axPcpqhGwB+KqaNJfhnlYDisRtVZ qUcpWbmAsTtAmrK2Gbw7pPYoYtkuaiVaQx25DBbdSiSkPeQJyRn77cyxM4ba7m/HVgTD dsCQTM0b49xg6s34lv5BFVk7FfdjZG3TcqTz2ymvVyLQ+HQEqpbkiQ+QBK5qmVH5hgt7 hZjOcXq+z1xFFO37CGglLdUbQ2Ais0Y/FU2Hb2selrUjOGommpHkxYeelO0kBCwBkfyN ai3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=IzDndUcZlu6mSmHo8/ONFdTqbWQc7l+ehwquiaRqPYI=; b=WKWJ34b+Ix+mnu7FI+d4t/pVR6l8e7Nf7nte9J14vBoFi1Gk2bQLDKnA6y9hkVXxhv +VViold9VhApyXR0ND2JA/P4I20luWR4UjpnBD41J48Xdg3sBPI0Q4O4W3ZAhwBgjldO Z4I4QEmJUe3ZtctMyWqmurHhr6wrh//5XFV6qefLiU2iVrn1FvnxdNTnZCLzOBMuGkKN 0CBXRbMg8TeHKeylF45x4aR5xJQ7cDERDQqIvwMXvCne6pggKd3Z+tx2Q7pVyuCgFEBS BOTlvZO0gX/MlyIFTjvKKw+H1X8StTOPKLp1q9VL7Sp7phxZUaRdX0aeQotWIsT73xn1 49uA== X-Gm-Message-State: APf1xPBS3KySayhWHsNsTe0gZEUSATHyYH/yHywkQxfS1BVcDHqnDRuS XwZeYzPztsZP+mHPchkJ+BM= X-Received: by 10.99.110.133 with SMTP id j127mr12118464pgc.79.1520250380253; Mon, 05 Mar 2018 03:46:20 -0800 (PST) Received: from aurora.jms.id.au ([45.124.203.19]) by smtp.gmail.com with ESMTPSA id d12sm22676405pfn.42.2018.03.05.03.46.13 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 05 Mar 2018 03:46:18 -0800 (PST) Received: by aurora.jms.id.au (sSMTP sendmail emulation); Mon, 05 Mar 2018 22:16:11 +1030 From: Joel Stanley To: Wim Van Sebroeck , Guenter Roeck , Rob Herring , Mark Rutland , Marcus Folkesson Cc: linux-watchdog@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Tomer Maimon , Avi Fishman , Brendan Higgins Subject: [PATCH v2 2/2] watchdog: Add Nuvoton NPCM watchdog driver Date: Mon, 5 Mar 2018 22:15:41 +1030 Message-Id: <20180305114541.9917-3-joel@jms.id.au> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20180305114541.9917-1-joel@jms.id.au> References: <20180305114541.9917-1-joel@jms.id.au> Sender: linux-watchdog-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-watchdog@vger.kernel.org The Nuvoton NPCM750 has a watchdog implemented as a single register inside the timer peripheral. This driver exposes that watchdog as a standard watchdog device with coarse timeout intervals, limited by the combination of prescaler and counter that is provided by the hardware. The calculation is taken from the Nuvoton vendor tree. There is a pre-timeout IRQ that is wired up. This timeout always occurs 1024 clocks before the timeout. Signed-off-by: Joel Stanley --- v2: - Make MODULE_LICENCE gpl v2 to match SPDX - Remove unused struct device pointer - Remove unused setting of drvdata - Add linux/bitops.h - Sort includes - Remove unused fiq include - Update timeout with achieved value --- drivers/watchdog/Kconfig | 11 +++ drivers/watchdog/Makefile | 1 + drivers/watchdog/npcm_wdt.c | 230 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 242 insertions(+) create mode 100644 drivers/watchdog/npcm_wdt.c -- 2.15.1 -- To unsubscribe from this list: send the line "unsubscribe linux-watchdog" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/drivers/watchdog/Kconfig b/drivers/watchdog/Kconfig index aff773bcebdb..0c1cc68894e6 100644 --- a/drivers/watchdog/Kconfig +++ b/drivers/watchdog/Kconfig @@ -513,6 +513,17 @@ config COH901327_WATCHDOG This watchdog is used to reset the system and thus cannot be compiled as a module. +config NPCM7XX_WATCHDOG + bool "NPCM750 watchdog" + depends on ARCH_NPCM || COMPILE_TEST + default y if ARCH_NPCM750 + select WATCHDOG_CORE + help + Say Y here to include Watchdog timer support for the + watchdog embedded into the NPCM7xx. + This watchdog is used to reset the system and thus cannot be + compiled as a module. + config TWL4030_WATCHDOG tristate "TWL4030 Watchdog" depends on TWL4030_CORE diff --git a/drivers/watchdog/Makefile b/drivers/watchdog/Makefile index 0474d38aa854..97a5afb5cad2 100644 --- a/drivers/watchdog/Makefile +++ b/drivers/watchdog/Makefile @@ -61,6 +61,7 @@ obj-$(CONFIG_ORION_WATCHDOG) += orion_wdt.o obj-$(CONFIG_SUNXI_WATCHDOG) += sunxi_wdt.o obj-$(CONFIG_RN5T618_WATCHDOG) += rn5t618_wdt.o obj-$(CONFIG_COH901327_WATCHDOG) += coh901327_wdt.o +obj-$(CONFIG_NPCM7XX_WATCHDOG) += npcm_wdt.o obj-$(CONFIG_STMP3XXX_RTC_WATCHDOG) += stmp3xxx_rtc_wdt.o obj-$(CONFIG_NUC900_WATCHDOG) += nuc900_wdt.o obj-$(CONFIG_TS4800_WATCHDOG) += ts4800_wdt.o diff --git a/drivers/watchdog/npcm_wdt.c b/drivers/watchdog/npcm_wdt.c new file mode 100644 index 000000000000..2cfc58714d8d --- /dev/null +++ b/drivers/watchdog/npcm_wdt.c @@ -0,0 +1,230 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (c) 2018 Nuvoton Technology corporation. +// Copyright (c) 2018 IBM Corp. + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define NPCM_WTCR 0x1C + +#define NPCM_WTCLK (BIT(10) | BIT(11)) /* Clock divider */ +#define NPCM_WTE BIT(7) /* Enable */ +#define NPCM_WTIE BIT(6) /* Enable irq */ +#define NPCM_WTIS (BIT(4) | BIT(5)) /* Interval selection */ +#define NPCM_WTIF BIT(3) /* Interrupt flag*/ +#define NPCM_WTRF BIT(2) /* Reset flag */ +#define NPCM_WTRE BIT(1) /* Reset enable */ +#define NPCM_WTR BIT(0) /* Reset counter */ + +/* + * Watchdog timeouts + * + * 170 msec: WTCLK=01 WTIS=00 VAL= 0x400 + * 670 msec: WTCLK=01 WTIS=01 VAL= 0x410 + * 1360 msec: WTCLK=10 WTIS=00 VAL= 0x800 + * 2700 msec: WTCLK=01 WTIS=10 VAL= 0x420 + * 5360 msec: WTCLK=10 WTIS=01 VAL= 0x810 + * 10700 msec: WTCLK=01 WTIS=11 VAL= 0x430 + * 21600 msec: WTCLK=10 WTIS=10 VAL= 0x820 + * 43000 msec: WTCLK=11 WTIS=00 VAL= 0xC00 + * 85600 msec: WTCLK=10 WTIS=11 VAL= 0x830 + * 172000 msec: WTCLK=11 WTIS=01 VAL= 0xC10 + * 687000 msec: WTCLK=11 WTIS=10 VAL= 0xC20 + * 2750000 msec: WTCLK=11 WTIS=11 VAL= 0xC30 + */ + +struct npcm_wdt { + struct watchdog_device wdd; + void __iomem *reg; +}; + +static inline struct npcm_wdt *to_npcm_wdt(struct watchdog_device *wdd) +{ + return container_of(wdd, struct npcm_wdt, wdd); +} + +static int npcm_wdt_ping(struct watchdog_device *wdd) +{ + struct npcm_wdt *wdt = to_npcm_wdt(wdd); + u32 val; + + val = readl(wdt->reg); + writel(val | NPCM_WTR, wdt->reg); + + return 0; +} + +static int npcm_wdt_start(struct watchdog_device *wdd) +{ + struct npcm_wdt *wdt = to_npcm_wdt(wdd); + u32 val; + + val = NPCM_WTRE | NPCM_WTE | NPCM_WTR | NPCM_WTIE; + + if (wdd->timeout < 2) { + val |= 0x800; + wdd->timeout = 1; + } else if (wdd->timeout < 3) { + val |= 0x420; + wdd->timeout = 2; + } else if (wdd->timeout < 6) { + val |= 0x810; + wdd->timeout = 5; + } else if (wdd->timeout < 11) { + val |= 0x430; + wdd->timeout = 10; + } else if (wdd->timeout < 22) { + val |= 0x820; + wdd->timeout = 21; + } else if (wdd->timeout < 44) { + val |= 0xC00; + wdd->timeout = 43; + } else if (wdd->timeout < 87) { + val |= 0x830; + wdd->timeout = 86; + } else if (wdd->timeout < 173) { + val |= 0xC10; + wdd->timeout = 172; + } else if (wdd->timeout < 688) { + val |= 0xC20; + wdd->timeout = 687; + } else { + val |= 0xC30; + wdd->timeout = 2750; + } + + writel(val, wdt->reg); + + return 0; +} + +static int npcm_wdt_stop(struct watchdog_device *wdd) +{ + struct npcm_wdt *wdt = to_npcm_wdt(wdd); + + writel(0, wdt->reg); + + return 0; +} + + +static int npcm_wdt_set_timeout(struct watchdog_device *wdd, + unsigned int timeout) +{ + /* timeout is updated with the achieved value in npcm_wdt_start */ + wdd->timeout = timeout; + if (watchdog_active(wdd)) + npcm_wdt_start(wdd); + + return 0; +} + +static irqreturn_t npcm_wdt_interrupt(int irq, void *data) +{ + struct npcm_wdt *wdt = data; + + watchdog_notify_pretimeout(&wdt->wdd); + + return IRQ_HANDLED; +} + +static int npcm_wdt_restart(struct watchdog_device *wdd, + unsigned long action, void *data) +{ + struct npcm_wdt *wdt = to_npcm_wdt(wdd); + + writel(NPCM_WTR | NPCM_WTRE | NPCM_WTE, wdt->reg); + udelay(1000); + + return 0; +} + +static const struct watchdog_info npcm_wdt_info = { + .identity = KBUILD_MODNAME, + .options = WDIOF_SETTIMEOUT + | WDIOF_KEEPALIVEPING + | WDIOF_MAGICCLOSE, +}; + +static const struct watchdog_ops npcm_wdt_ops = { + .owner = THIS_MODULE, + .start = npcm_wdt_start, + .stop = npcm_wdt_stop, + .ping = npcm_wdt_ping, + .set_timeout = npcm_wdt_set_timeout, + .restart = npcm_wdt_restart, +}; + +static int npcm_wdt_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct npcm_wdt *wdt; + struct resource *res; + int irq; + int ret; + + wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL); + if (!wdt) + return -ENOMEM; + + res = platform_get_resource(pdev, IORESOURCE_MEM, 0); + wdt->reg = devm_ioremap_resource(dev, res); + if (IS_ERR(wdt->reg)) + return PTR_ERR(wdt->reg); + + irq = platform_get_irq(pdev, 0); + if (!irq) + return -EINVAL; + + wdt->wdd.info = &npcm_wdt_info; + wdt->wdd.ops = &npcm_wdt_ops; + wdt->wdd.min_timeout = 1; + wdt->wdd.max_timeout = 2750; + wdt->wdd.parent = dev; + + wdt->wdd.timeout = 86; + watchdog_init_timeout(&wdt->wdd, 0, dev); + + ret = devm_request_irq(dev, irq, npcm_wdt_interrupt, 0, + "watchdog", wdt); + if (ret) + return ret; + + ret = devm_watchdog_register_device(dev, &wdt->wdd); + if (ret) { + dev_err(dev, "failed to register watchdog\n"); + return ret; + } + + dev_info(dev, "NPCM watchdog driver enabled\n"); + + return 0; +} + +#ifdef CONFIG_OF +static const struct of_device_id npcm_wdt_match[] = { + {.compatible = "nuvoton,npcm750-wdt"}, + {}, +}; +MODULE_DEVICE_TABLE(of, npcm_wdt_match); +#endif + +static struct platform_driver npcm_wdt_driver = { + .probe = npcm_wdt_probe, + .driver = { + .name = "npcm-wdt", + .of_match_table = of_match_ptr(npcm_wdt_match), + }, +}; +module_platform_driver(npcm_wdt_driver); + +MODULE_AUTHOR("Joel Stanley"); +MODULE_DESCRIPTION("Watchdog driver for NPCM"); +MODULE_LICENSE("GPL v2");