From patchwork Fri Jan 20 13:03:16 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baoyou Xie X-Patchwork-Id: 92078 Delivered-To: patch@linaro.org Received: by 10.140.20.99 with SMTP id 90csp5652qgi; Fri, 20 Jan 2017 05:04:34 -0800 (PST) X-Received: by 10.99.131.65 with SMTP id h62mr16664055pge.41.1484917474212; Fri, 20 Jan 2017 05:04:34 -0800 (PST) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id u11si6785969pfa.270.2017.01.20.05.04.34 for ; Fri, 20 Jan 2017 05:04:34 -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=@linaro.org; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752028AbdATNEd (ORCPT ); Fri, 20 Jan 2017 08:04:33 -0500 Received: from mail-pg0-f49.google.com ([74.125.83.49]:33215 "EHLO mail-pg0-f49.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752004AbdATNEc (ORCPT ); Fri, 20 Jan 2017 08:04:32 -0500 Received: by mail-pg0-f49.google.com with SMTP id 204so23766877pge.0 for ; Fri, 20 Jan 2017 05:04:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=eHb9koh7uBC4S5wCHbu5CzfYU9U/2ARcYTXKiFtBvzk=; b=ZtEb3enHDQ2qqcji0E7tLWVRk4iTViUJ/E9l6FcipdHzZg4iv7lP8+Z5NfoXx2xr52 iFmXqjEoW9ONzr6y+Lpk62BcwU+uT83aURq70MCAU5mRbBKkAQG7+7iS4z49oezddR8P siZGUllOQGE+AiknC3iIzjE8YaqnOLZ1afNig= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=eHb9koh7uBC4S5wCHbu5CzfYU9U/2ARcYTXKiFtBvzk=; b=jZvwofJ8/rlzlk1rK84chipeXFmTD4s3jzUMQvlQwwy2Lx7JfkJO1nLSf2e9ryBbp/ eyP0x/nwCBZjCtA0Anu2xPXWOZ3v9f0jl+ssC1PksObDqn3gkZVYRrR9VnCnZYDvdLW5 AcnEih4qbcLpZHyZIdnHfxDaCBEV69igfMoOKoG2M2nKalDL+QB6SRE7axMiIJdXGmQh YGxmYn7PYAB3CNTTDcXRSiqrbzdAP1Ymj+6waN3tEDIL57Ewp6z8ZFX0c/GFbKbr1I27 j44sQt0bAzp3K1GMxXbK/2mr+5BtcgIhkftHrmBf17rLDgZi+WtD+sbiwgZVMYSA06pk ZC2A== X-Gm-Message-State: AIkVDXKPPRsKrGQdpiqWWuyVYMj7qxv7GHvChU/uhRoHfJ8MC3OQcEVcLpQ3kxjqJzyVh3nZ X-Received: by 10.99.137.199 with SMTP id v190mr14692926pgd.120.1484917471965; Fri, 20 Jan 2017 05:04:31 -0800 (PST) Received: from localhost.localdomain ([104.237.91.228]) by smtp.gmail.com with ESMTPSA id b10sm16910377pga.21.2017.01.20.05.04.12 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 20 Jan 2017 05:04:31 -0800 (PST) From: Baoyou Xie To: jun.nie@linaro.org, wim@iguana.be, linux@roeck-us.net, robh+dt@kernel.org, mark.rutland@arm.com Cc: linux-arm-kernel@lists.infradead.org, linux-watchdog@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, shawnguo@kernel.org, baoyou.xie@linaro.org, xie.baoyou@zte.com.cn, chen.chaokai@zte.com.cn, wang.qiang01@zte.com.cn Subject: [PATCH v3 1/3] dt: bindings: add documentation for zx2967 family watchdog controller Date: Fri, 20 Jan 2017 21:03:16 +0800 Message-Id: <1484917398-15604-1-git-send-email-baoyou.xie@linaro.org> X-Mailer: git-send-email 2.7.4 Sender: linux-watchdog-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-watchdog@vger.kernel.org This patch adds dt-binding documentation for zx2967 family watchdog controller. Signed-off-by: Baoyou Xie --- .../bindings/watchdog/zte,zx2967-wdt.txt | 31 ++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Documentation/devicetree/bindings/watchdog/zte,zx2967-wdt.txt -- 2.7.4 -- 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/Documentation/devicetree/bindings/watchdog/zte,zx2967-wdt.txt b/Documentation/devicetree/bindings/watchdog/zte,zx2967-wdt.txt new file mode 100644 index 0000000..772403a --- /dev/null +++ b/Documentation/devicetree/bindings/watchdog/zte,zx2967-wdt.txt @@ -0,0 +1,31 @@ +ZTE zx2967 Watchdog timer + +Required properties: + +- compatible : should be one of the following. + * zte,zx296718-wdt +- reg : Specifies base physical address and size of the registers. +- clocks : Pairs of phandle and specifier referencing the controller's clocks. +- resets : Reference to the reset controller controlling the watchdog + controller. + +Optional properties: + +- zte,wdt-reset-sysctrl : Directs how to reset system by the watchdog. + if we don't want to restart system when watchdog been triggered, + it's not required, vice versa. + It should include following fields. + * phandle of aon-sysctrl. + * offset of register that be written, should be 0xb0. + * configure value that be written to aon-sysctrl. + * bit mask, corresponding bits will be affected. + +Example: + +wdt: watchdog@1465000 { + compatible = "zte,zx296718-wdt"; + reg = <0x1465000 0x1000>; + clocks = <&topcrm WDT_WCLK>; + resets = <&toprst 35>; + zte,wdt-reset-sysctrl = <&aon_sysctrl 0xb0 1 0x115>; +}; From patchwork Fri Jan 20 13:03:17 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baoyou Xie X-Patchwork-Id: 92079 Delivered-To: patch@linaro.org Received: by 10.140.20.99 with SMTP id 90csp5789qgi; Fri, 20 Jan 2017 05:04:49 -0800 (PST) X-Received: by 10.98.74.84 with SMTP id x81mr16312065pfa.172.1484917489012; Fri, 20 Jan 2017 05:04:49 -0800 (PST) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q4si6836841pfg.22.2017.01.20.05.04.48 for ; Fri, 20 Jan 2017 05:04:49 -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=@linaro.org; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752100AbdATNEs (ORCPT ); Fri, 20 Jan 2017 08:04:48 -0500 Received: from mail-pf0-f178.google.com ([209.85.192.178]:35481 "EHLO mail-pf0-f178.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751993AbdATNEr (ORCPT ); Fri, 20 Jan 2017 08:04:47 -0500 Received: by mail-pf0-f178.google.com with SMTP id f144so22597765pfa.2 for ; Fri, 20 Jan 2017 05:04:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Bp3j5DodJQ5kA+onWaBYsyhHHbKAkehXcC9DybKAhV4=; b=CAcGuUISWdgEKEt5u2XsfpU5ZFRt0jV3V9+0tW5OiP7S5hxbti4v4IHfOwlYLilr6J xfNmtbBKdhvay/xVNHOFtwMPtD7GsOE8SJUrj8oYh44k10a8GwsJSKNfwMyxNyUhoK5+ Ba/L4UB1iY1V0/FtMqwc1zXNYbUt3M4N2s+6s= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=Bp3j5DodJQ5kA+onWaBYsyhHHbKAkehXcC9DybKAhV4=; b=sk4qbpxkl2IP8ox5MN+GX7dfEafR7KidMx0mVyS97aLEIEig4ITtSS7rLZuqJuRa+9 +N0p7c45Y7cJy11GrVmmY0IuzrWfsQFaC6rG74J9gAuiIHoGruhGWtJ7a5AxrDnXX/wF b4LX8XavysQxr++EzTDxzIKWVEpz6aHF7stbuDdsxmbh1GKI+Wu/s0oIPy3RwCrZdeJC yTGqjGJYSfpMMuUwD5Eczj+y88TVvWeltMLBJylaSwP+AEKC4mVGA2070La3waQmazmu DEXwTCnnFk3in9LSCZapAVwffcvsAF/QCtX4L6Ib6qOaSo3WDCWLwr6AjpQFSJlW0kZe LJcQ== X-Gm-Message-State: AIkVDXJgdNzge/36Qfp3wkanbsFw42aF0xOSD+TxyLunzyEoBRzfOBnBI97KvPKDMI8Wa3lK X-Received: by 10.98.147.78 with SMTP id b75mr16542106pfe.128.1484917486402; Fri, 20 Jan 2017 05:04:46 -0800 (PST) Received: from localhost.localdomain ([104.237.91.228]) by smtp.gmail.com with ESMTPSA id b10sm16910377pga.21.2017.01.20.05.04.32 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 20 Jan 2017 05:04:45 -0800 (PST) From: Baoyou Xie To: jun.nie@linaro.org, wim@iguana.be, linux@roeck-us.net, robh+dt@kernel.org, mark.rutland@arm.com Cc: linux-arm-kernel@lists.infradead.org, linux-watchdog@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, shawnguo@kernel.org, baoyou.xie@linaro.org, xie.baoyou@zte.com.cn, chen.chaokai@zte.com.cn, wang.qiang01@zte.com.cn Subject: [PATCH v3 2/3] MAINTAINERS: add zx2967 watchdog controller driver to ARM ZTE architecture Date: Fri, 20 Jan 2017 21:03:17 +0800 Message-Id: <1484917398-15604-2-git-send-email-baoyou.xie@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1484917398-15604-1-git-send-email-baoyou.xie@linaro.org> References: <1484917398-15604-1-git-send-email-baoyou.xie@linaro.org> Sender: linux-watchdog-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-watchdog@vger.kernel.org Add the zx2967 watchdog controller driver as maintained by ARM ZTE architecture maintainers, as they're parts of the core IP. Signed-off-by: Baoyou Xie --- MAINTAINERS | 2 ++ 1 file changed, 2 insertions(+) -- 2.7.4 -- 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/MAINTAINERS b/MAINTAINERS index edfdea3..275c434 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -1990,11 +1990,13 @@ F: drivers/clk/zte/ F: drivers/reset/reset-zx2967.c F: drivers/soc/zte/ F: drivers/thermal/zx* +F: drivers/watchdog/zx2967_wdt.c F: Documentation/devicetree/bindings/arm/zte.txt F: Documentation/devicetree/bindings/clock/zx296702-clk.txt F: Documentation/devicetree/bindings/reset/zte,zx2967-reset.txt F: Documentation/devicetree/bindings/soc/zte/ F: Documentation/devicetree/bindings/thermal/zx* +F: Documentation/devicetree/bindings/watchdog/zte,zx2967-wdt.txt F: include/dt-bindings/soc/zx*.h ARM/ZYNQ ARCHITECTURE From patchwork Fri Jan 20 13:03:18 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Baoyou Xie X-Patchwork-Id: 92080 Delivered-To: patch@linaro.org Received: by 10.140.20.99 with SMTP id 90csp5996qgi; Fri, 20 Jan 2017 05:05:14 -0800 (PST) X-Received: by 10.98.159.80 with SMTP id g77mr16105873pfe.34.1484917514580; Fri, 20 Jan 2017 05:05:14 -0800 (PST) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a3si5292822pld.64.2017.01.20.05.05.14 for ; Fri, 20 Jan 2017 05:05:14 -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=@linaro.org; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752143AbdATNFG (ORCPT ); Fri, 20 Jan 2017 08:05:06 -0500 Received: from mail-pg0-f54.google.com ([74.125.83.54]:34255 "EHLO mail-pg0-f54.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750893AbdATNFE (ORCPT ); Fri, 20 Jan 2017 08:05:04 -0500 Received: by mail-pg0-f54.google.com with SMTP id 14so23722687pgg.1 for ; Fri, 20 Jan 2017 05:05:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=noi2nCS+aUAXLdpA7+Oz0wsDY3h1xX98Jl79IceG5wQ=; b=JVndm+o6wsLn3unNxMZG8OLxzvq5bZWqpoVQlP6npeSPeEvPp2iRUZ4qChnYDE0BZ+ b+//Ml65j8SJFe4llAii11zk/NgKjTD1qG5oT8xWC45sMHb/yBjgnd+24KxWer+HWOwl zGJVVdIZ3Q+ZPgomK4EjK7ut2tBa2cM9o+Y5g= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=noi2nCS+aUAXLdpA7+Oz0wsDY3h1xX98Jl79IceG5wQ=; b=JM9QVk0BhhykayRiFo69jsghF/UaNQIj/VElQo9Oy8t5izcaLYJexlfN6lmPfsEOiu OapSoV5Fj6GT0pK+FfPRcNO7MC3bVYif3YRJvC+rZlOEwElkdYjx7Fyzgz9RYktKVuOC aFNuxFC/Lmhy7HmEpZUrbkNqDAql9KVAs0nHcZdP9RtqFcGkNk9Th8ZI8e9yGzh3MENM yQws/lB3/zNBVenoY54yjboQ88RGyJMqAt+E9Q2u7SVCXBXQ/pkPQTWNlI67gS7ItmRf 89OD4cqX8iugV8MRjOpkFTqwU+UbHzKtBp3z98BOBNoVTnDX6DscbT/PzhlOEyrLzrA1 f+7A== X-Gm-Message-State: AIkVDXLf1ouVZ8coC44G43lWMNyQQv8nLK3k9TyL1dOT3L5OUHKQgYWHOKUW9IT3FjfcLyVK X-Received: by 10.84.231.142 with SMTP id g14mr21632500plk.13.1484917503150; Fri, 20 Jan 2017 05:05:03 -0800 (PST) Received: from localhost.localdomain ([104.237.91.228]) by smtp.gmail.com with ESMTPSA id b10sm16910377pga.21.2017.01.20.05.04.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Fri, 20 Jan 2017 05:05:02 -0800 (PST) From: Baoyou Xie To: jun.nie@linaro.org, wim@iguana.be, linux@roeck-us.net, robh+dt@kernel.org, mark.rutland@arm.com Cc: linux-arm-kernel@lists.infradead.org, linux-watchdog@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, shawnguo@kernel.org, baoyou.xie@linaro.org, xie.baoyou@zte.com.cn, chen.chaokai@zte.com.cn, wang.qiang01@zte.com.cn Subject: [PATCH v3 3/3] watchdog: zx2967: add watchdog controller driver for ZTE's zx2967 family Date: Fri, 20 Jan 2017 21:03:18 +0800 Message-Id: <1484917398-15604-3-git-send-email-baoyou.xie@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1484917398-15604-1-git-send-email-baoyou.xie@linaro.org> References: <1484917398-15604-1-git-send-email-baoyou.xie@linaro.org> Sender: linux-watchdog-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-watchdog@vger.kernel.org This patch adds watchdog controller driver for ZTE's zx2967 family. Signed-off-by: Baoyou Xie --- drivers/watchdog/Kconfig | 10 ++ drivers/watchdog/Makefile | 1 + drivers/watchdog/zx2967_wdt.c | 376 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 387 insertions(+) create mode 100644 drivers/watchdog/zx2967_wdt.c -- 2.7.4 -- 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 acb00b5..05093a2 100644 --- a/drivers/watchdog/Kconfig +++ b/drivers/watchdog/Kconfig @@ -714,6 +714,16 @@ config ASPEED_WATCHDOG To compile this driver as a module, choose M here: the module will be called aspeed_wdt. +config ZX2967_WATCHDOG + tristate "ZTE zx2967 SoCs watchdog support" + depends on ARCH_ZX + select WATCHDOG_CORE + help + Say Y here to include support for the watchdog timer + in ZTE zx2967 SoCs. + To compile this driver as a module, choose M here: the + module will be called zx2967_wdt. + # AVR32 Architecture config AT32AP700X_WDT diff --git a/drivers/watchdog/Makefile b/drivers/watchdog/Makefile index 0c3d35e..bf2d296 100644 --- a/drivers/watchdog/Makefile +++ b/drivers/watchdog/Makefile @@ -82,6 +82,7 @@ obj-$(CONFIG_BCM7038_WDT) += bcm7038_wdt.o obj-$(CONFIG_ATLAS7_WATCHDOG) += atlas7_wdt.o obj-$(CONFIG_RENESAS_WDT) += renesas_wdt.o obj-$(CONFIG_ASPEED_WATCHDOG) += aspeed_wdt.o +obj-$(CONFIG_ZX2967_WATCHDOG) += zx2967_wdt.o # AVR32 Architecture obj-$(CONFIG_AT32AP700X_WDT) += at32ap700x_wdt.o diff --git a/drivers/watchdog/zx2967_wdt.c b/drivers/watchdog/zx2967_wdt.c new file mode 100644 index 0000000..a5656d0 --- /dev/null +++ b/drivers/watchdog/zx2967_wdt.c @@ -0,0 +1,376 @@ +/* + * watchdog driver for ZTE's zx2967 family + * + * Copyright (C) 2017 ZTE Ltd. + * + * Author: Baoyou Xie + * + * License terms: GNU General Public License (GPL) version 2 + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define ZX2967_WDT_CFG_REG 0x4 +#define ZX2967_WDT_LOAD_REG 0x8 +#define ZX2967_WDT_REFRESH_REG 0x18 +#define ZX2967_WDT_START_REG 0x1c + +#define ZX2967_WDT_REFRESH_MASK 0x3f + +#define ZX2967_WDT_CFG_DIV(n) ((((n) & 0xff) - 1) << 8) +#define ZX2967_WDT_START_EN 0x1 + +#define ZX2967_WDT_WRITEKEY 0x12340000 + +#define ZX2967_WDT_DIV_DEFAULT 16 +#define ZX2967_WDT_DEFAULT_TIMEOUT 32 +#define ZX2967_WDT_MIN_TIMEOUT 1 +#define ZX2967_WDT_MAX_TIMEOUT 500 +#define ZX2967_WDT_MAX_COUNT 0xffff + +#define ZX2967_WDT_FLAG_REBOOT_MON (1 << 0) + +struct zx2967_wdt { + struct device *dev; + struct clk *clock; + void __iomem *reg_base; + unsigned int conf; + unsigned int load; + unsigned int flags; + struct watchdog_device wdt_device; + struct notifier_block restart_handler; + struct notifier_block reboot_handler; +}; + +static inline u32 zx2967_wdt_readl(struct zx2967_wdt *wdt, u16 reg) +{ + return readl_relaxed(wdt->reg_base + reg); +} + +static inline void zx2967_wdt_writel(struct zx2967_wdt *wdt, u16 reg, u32 val) +{ + writel_relaxed(val | ZX2967_WDT_WRITEKEY, wdt->reg_base + reg); +} + +static void zx2967_wdt_refresh(struct zx2967_wdt *wdt) +{ + u32 val; + + val = zx2967_wdt_readl(wdt, ZX2967_WDT_REFRESH_REG); + val ^= ZX2967_WDT_REFRESH_MASK; + zx2967_wdt_writel(wdt, ZX2967_WDT_REFRESH_REG, val); +} + +static unsigned int +__zx2967_wdt_set_timeout(struct zx2967_wdt *wdt, unsigned int timeout) +{ + unsigned int freq = clk_get_rate(wdt->clock); + unsigned int divisor = ZX2967_WDT_DIV_DEFAULT; + unsigned int count; + + count = timeout * freq; + if (count > divisor * ZX2967_WDT_MAX_COUNT) + divisor = DIV_ROUND_UP(count, ZX2967_WDT_MAX_COUNT); + count = DIV_ROUND_UP(count, divisor); + zx2967_wdt_writel(wdt, ZX2967_WDT_CFG_REG, ZX2967_WDT_CFG_DIV(divisor)); + zx2967_wdt_writel(wdt, ZX2967_WDT_LOAD_REG, count); + zx2967_wdt_refresh(wdt); + wdt->load = count; + + return (count * divisor) / freq; +} + +static int zx2967_wdt_set_timeout(struct watchdog_device *wdd, + unsigned int timeout) +{ + struct zx2967_wdt *wdt = watchdog_get_drvdata(wdd); + + if (watchdog_timeout_invalid(&wdt->wdt_device, timeout)) { + dev_err(wdt->dev, "timeout %d is invalid\n", timeout); + return -EINVAL; + } + + wdd->timeout = __zx2967_wdt_set_timeout(wdt, timeout); + + return 0; +} + +static void __zx2967_wdt_start(struct zx2967_wdt *wdt) +{ + u32 val; + + val = zx2967_wdt_readl(wdt, ZX2967_WDT_START_REG); + val |= ZX2967_WDT_START_EN; + zx2967_wdt_writel(wdt, ZX2967_WDT_START_REG, val); +} + +static void __zx2967_wdt_stop(struct zx2967_wdt *wdt) +{ + u32 val; + + val = zx2967_wdt_readl(wdt, ZX2967_WDT_START_REG); + val &= ~ZX2967_WDT_START_EN; + zx2967_wdt_writel(wdt, ZX2967_WDT_START_REG, val); +} + +static int zx2967_wdt_start(struct watchdog_device *wdd) +{ + struct zx2967_wdt *wdt = watchdog_get_drvdata(wdd); + + __zx2967_wdt_stop(wdt); + zx2967_wdt_set_timeout(wdd, wdd->timeout); + __zx2967_wdt_start(wdt); + + return 0; +} + +static int zx2967_wdt_stop(struct watchdog_device *wdd) +{ + struct zx2967_wdt *wdt = watchdog_get_drvdata(wdd); + + __zx2967_wdt_stop(wdt); + + return 0; +} + +static int zx2967_wdt_keepalive(struct watchdog_device *wdd) +{ + struct zx2967_wdt *wdt = watchdog_get_drvdata(wdd); + + zx2967_wdt_refresh(wdt); + + return 0; +} + +#define ZX2967_WDT_OPTIONS \ + (WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE) +static const struct watchdog_info zx2967_wdt_ident = { + .options = ZX2967_WDT_OPTIONS, + .firmware_version = 0, + .identity = "zx2967 watchdog", +}; + +static struct watchdog_ops zx2967_wdt_ops = { + .owner = THIS_MODULE, + .start = zx2967_wdt_start, + .stop = zx2967_wdt_stop, + .ping = zx2967_wdt_keepalive, + .set_timeout = zx2967_wdt_set_timeout, +}; + +static void zx2967_wdt_fix_sysdown(struct zx2967_wdt *wdt) +{ + __zx2967_wdt_stop(wdt); + __zx2967_wdt_set_timeout(wdt, 15); + __zx2967_wdt_start(wdt); +} + +static int zx2967_wdt_notify_sys(struct notifier_block *this, + unsigned long code, void *unused) +{ + struct zx2967_wdt *wdt = container_of(this, struct zx2967_wdt, + reboot_handler); + + wdt->flags |= ZX2967_WDT_FLAG_REBOOT_MON; + switch (code) { + case SYS_HALT: + case SYS_POWER_OFF: + case SYS_RESTART: + zx2967_wdt_fix_sysdown(wdt); + break; + default: + break; + } + + return 0; +} + +static int zx2967_wdt_restart(struct notifier_block *this, + unsigned long mode, void *cmd) +{ + struct zx2967_wdt *wdt; + + wdt = container_of(this, struct zx2967_wdt, restart_handler); + + zx2967_wdt_stop(&wdt->wdt_device); + + zx2967_wdt_writel(wdt, ZX2967_WDT_LOAD_REG, 0x80); + zx2967_wdt_refresh(wdt); + zx2967_wdt_writel(wdt, ZX2967_WDT_START_REG, ZX2967_WDT_START_EN); + + zx2967_wdt_start(&wdt->wdt_device); + /* wait for reset*/ + mdelay(500); + + return NOTIFY_DONE; +} + +static void zx2967_wdt_reset_sysctrl(struct device *dev) +{ + int ret; + struct device_node *np = NULL; + void __iomem *regmap; + unsigned int offset, mask, config; + struct of_phandle_args out_args; + + ret = of_parse_phandle_with_fixed_args(dev->of_node, + "zte,wdt-reset-sysctrl", 3, 0, &out_args); + if (ret) { + dev_info(dev, "failed to parse zte,wdt-reset-sysctrl"); + return; + } + offset = out_args.args[0]; + config = out_args.args[1]; + mask = out_args.args[2]; + + regmap = syscon_node_to_regmap(out_args.np); + if (IS_ERR(regmap)) + goto out; + + regmap_update_bits(regmap, offset, mask, config); +out: + of_node_put(np); +} + +static int zx2967_wdt_probe(struct platform_device *pdev) +{ + struct device *dev; + struct zx2967_wdt *wdt; + struct resource *base; + int ret = 0; + + struct reset_control *rstc; + + dev = &pdev->dev; + + wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL); + if (!wdt) + return -ENOMEM; + + platform_set_drvdata(pdev, wdt); + + wdt->dev = dev; + wdt->wdt_device.info = &zx2967_wdt_ident; + wdt->wdt_device.ops = &zx2967_wdt_ops; + wdt->wdt_device.timeout = ZX2967_WDT_DEFAULT_TIMEOUT; + wdt->wdt_device.max_timeout = ZX2967_WDT_MAX_TIMEOUT; + wdt->wdt_device.min_timeout = ZX2967_WDT_MIN_TIMEOUT; + wdt->wdt_device.parent = &pdev->dev; + + base = platform_get_resource(pdev, IORESOURCE_MEM, 0); + wdt->reg_base = devm_ioremap_resource(dev, base); + if (IS_ERR(wdt->reg_base)) { + dev_err(dev, "ioremap failed\n"); + return PTR_ERR(wdt->reg_base); + } + + zx2967_wdt_reset_sysctrl(dev); + + wdt->reboot_handler.notifier_call = zx2967_wdt_notify_sys; + register_reboot_notifier(&wdt->reboot_handler); + wdt->clock = devm_clk_get(dev, NULL); + if (IS_ERR(wdt->clock)) { + dev_err(dev, "failed to find watchdog clock source\n"); + return PTR_ERR(wdt->clock); + } + + ret = clk_prepare_enable(wdt->clock); + if (ret < 0) { + dev_err(dev, "failed to enable clock\n"); + return ret; + } + clk_set_rate(wdt->clock, 32768); + + rstc = devm_reset_control_get(dev, NULL); + if (IS_ERR(rstc)) { + dev_err(dev, "failed to get rstc"); + ret = PTR_ERR(rstc); + goto fail_get_reset_control; + } + + reset_control_assert(rstc); + mdelay(10); + reset_control_deassert(rstc); + + watchdog_set_drvdata(&wdt->wdt_device, wdt); + + watchdog_init_timeout(&wdt->wdt_device, + ZX2967_WDT_DEFAULT_TIMEOUT, dev); + watchdog_set_nowayout(&wdt->wdt_device, WATCHDOG_NOWAYOUT); + + zx2967_wdt_stop(&wdt->wdt_device); + + ret = watchdog_register_device(&wdt->wdt_device); + if (ret) + goto fail_register; + + wdt->restart_handler.notifier_call = zx2967_wdt_restart; + wdt->restart_handler.priority = 128; + ret = register_restart_handler(&wdt->restart_handler); + if (ret) { + dev_err(dev, "cannot register restart handler, %d\n", ret); + goto fail_restart; + } + + dev_info(dev, "watchdog enabled (timeout=%d sec, nowayout=%d)", + wdt->wdt_device.timeout, WATCHDOG_NOWAYOUT); + + return 0; + +fail_get_reset_control: +fail_restart: + watchdog_unregister_device(&wdt->wdt_device); +fail_register: + clk_disable_unprepare(wdt->clock); + return ret; +} + +static int zx2967_wdt_remove(struct platform_device *pdev) +{ + struct zx2967_wdt *wdt = platform_get_drvdata(pdev); + + unregister_restart_handler(&wdt->restart_handler); + watchdog_unregister_device(&wdt->wdt_device); + clk_disable_unprepare(wdt->clock); + + return 0; +} + +static void zx2967_wdt_shutdown(struct platform_device *pdev) +{ + struct zx2967_wdt *wdt = platform_get_drvdata(pdev); + + if (!(wdt->flags & ZX2967_WDT_FLAG_REBOOT_MON)) + zx2967_wdt_stop(&wdt->wdt_device); +} + +static const struct of_device_id zx2967_wdt_match[] = { + { .compatible = "zte,zx296718-wdt", }, + {} +}; +MODULE_DEVICE_TABLE(of, zx2967_wdt_match); + +static struct platform_driver zx2967_wdt_driver = { + .probe = zx2967_wdt_probe, + .remove = zx2967_wdt_remove, + .shutdown = zx2967_wdt_shutdown, + .driver = { + .name = "zx2967-wdt", + .of_match_table = of_match_ptr(zx2967_wdt_match), + }, +}; +module_platform_driver(zx2967_wdt_driver); + +MODULE_AUTHOR("Baoyou Xie "); +MODULE_DESCRIPTION("ZTE zx2967 Watchdog Device Driver"); +MODULE_LICENSE("GPL v2");