From nobody Sun Feb 8 00:49:29 2026 Received: from mail-wm1-f42.google.com (mail-wm1-f42.google.com [209.85.128.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 76236166F29 for ; Wed, 2 Apr 2025 15:49:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743608999; cv=none; b=h74YQfXcRFeNZjL6ds12m43TvQ9PEmZSgRyzW9iUugiVKMBRtxlp+39AAEseVoaK8WobTxbBJOHCJIAlBKcaiM8hx/jAV3gl45IC4QOdDfVhv2mO/XDD1JBDefuTyjrVHy00qQ99HXyc4ctZOmhbhIiVS1wICj3QeEz+haTBFzM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743608999; c=relaxed/simple; bh=R1vtErKl2BzedEvjhWWXJCdtLNjDCmhp/prRalj0xAk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=foVrZXxoRwGkWYKd8odxSiIUbdwsBCDcksjteIIvEO+xLBchoOzxWVhR89BVzEHAfkssTaPiFcgp2dG65Sgu9XTOpKXkchpqiFvYYDa9LIhCywuROQBBw5q8YCWP/clRBOvOSI2goMZW9t14KHGyXTf7qRSLAjYKbwhPtfa6b5w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=Q1texYzu; arc=none smtp.client-ip=209.85.128.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="Q1texYzu" Received: by mail-wm1-f42.google.com with SMTP id 5b1f17b1804b1-43cf034d4abso75266415e9.3 for ; Wed, 02 Apr 2025 08:49:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1743608995; x=1744213795; 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; bh=f19WgeQ78sxdiIjTFJ2APJJGvVw1dxzyTwQ7DYBPLY0=; b=Q1texYzumRcfQRVNQeYCYZgZR6eWmf8PbQi/d259wQVf19ZERezxIvI2d/QoLlLjdK YUtPC7l4Q50GsAvmmG5q7bK0d8EXRT9UBjHBhUW/V8N55TBi9SJqWLUgc+GISkCOHnYp TW8C8ZfOl2LrCx3nMLj5njeZdhBP9ix8ll/RLgwQgERKCuY9+ZUySdKqdxhLbQrEDhcj RIS8PZuVJ9egjJ0an7TgEtZIBZJ5lmwo6pJ5BIy8p9M5my5v13LnBsAK3eSGH9Wtlbwe 6LTONMopPFe5GEIB/ApoTuq6rIi0XyRFn4YOeisRP2OeAHwXa1viVQiZU88hgR+iKEC1 Cb7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743608995; x=1744213795; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=f19WgeQ78sxdiIjTFJ2APJJGvVw1dxzyTwQ7DYBPLY0=; b=mvZa4cwG6C5E6LiCDYc8k3a84IUnC5ggHtQJzBEgtpEdssy8XPj6qQiVSPDGQL6zEC BHaXsZOLlAO0JrTbrccxpqZHcscvjJ+Mal/bVM8A3v25PX+aADSZ3gvx4a2Fye413mNI NPgbbCCXQnW1KD/5baQcFuSFeta+ygHCKLMT8P7XhNnPX7Dp8dylavE6qLXWNDaQA+aY M9B5Qx3ynjJ2aU7poSoNL1agzHuiTt1w5MVTidBr+lnL9lUsBQVbWCGMbYyK0r/+w+Ji O4YbYysnmk1Y0572hS76q+oirpCY1xso6nVeCpnGt5xgS290LK6ejaRqXnHEKRFtWaMb YA4Q== X-Forwarded-Encrypted: i=1; AJvYcCUjy0bOkhXjVufH4k1oQxOgfpCI2yMyrgLi3JYpNMzgBNcovU3yurcvD3rwHLSGlA7gNkJX7z3cwBDcOVw=@vger.kernel.org X-Gm-Message-State: AOJu0YyRaygeovNfFxdZhUG10kS9ym0upOwqzrC+AFEoJMDg7S6bK5Dx nM/pQFeK8rUgfW7rnjBoQA4ZoQWZVxunn/gwdEWUGwvLFwCusNoaTmnbYvk832o= X-Gm-Gg: ASbGncsnUOalrXJbmOl1CeEIaF9Kb4FW8Kg37WTIH+Nrw5lGAVwsf2WDSnpTFiduqd/ ioFyJyfLY6kOyMDztC0z27R4YStoqCN33gYALE6hbpB6hJKvNR262w1lcRVhyaAuCKIgtZwCgal mZgfotPm8JX21bmE6oJqipdCCUkovXclmIs2krNgzLn2NvYlkvrd1uiabxnKU6HRaCFxAwPTG8h QI03mcJIwAx0B5EK2lcLy8JJC7k1G747cDGaEEil/siUs0WnofeC9Jq4/yPSLVGV/2QudGrQhoo bbHakeSbe8+HeM9EvEdOgUI1EzjjfqDkx9CmL02aLfyTS2mMN0NRTHWtx1u5igvwhTY+h5A= X-Google-Smtp-Source: AGHT+IGqxQcz+/0qde9VdZYOmDlRDKSETydjHtHNN863PVRigirhEFS9zdQVnVdG4C0KFu+Or92Y4g== X-Received: by 2002:a05:600c:444b:b0:43c:f184:2e16 with SMTP id 5b1f17b1804b1-43eb5c18428mr23351285e9.5.1743608994610; Wed, 02 Apr 2025 08:49:54 -0700 (PDT) Received: from mai.. (146725694.box.freepro.com. [130.180.211.218]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43eb5fd1194sm24674555e9.11.2025.04.02.08.49.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Apr 2025 08:49:54 -0700 (PDT) From: Daniel Lezcano To: wim@linux-watchdog.org Cc: linux@roeck-us.net, linux-watchdog@vger.kernel.org, linux-kernel@vger.kernel.org, S32@nxp.com, ghennadi.procopciuc@nxp.com, thomas.fossati@linaro.org, robh@kernel.org, krzk+dt@kernel.org, conor+dt@kernel.org, devicetree@vger.kernel.org Subject: [PATCH v2 1/2] dt-bindings: watchdog: Add NXP Software Watchdog Timer Date: Wed, 2 Apr 2025 17:49:39 +0200 Message-ID: <20250402154942.3645283-2-daniel.lezcano@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250402154942.3645283-1-daniel.lezcano@linaro.org> References: <20250402154942.3645283-1-daniel.lezcano@linaro.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Describe the Software Watchdog Timer available on the S32G platforms. Cc: Ghennadi Procopciuc Cc: Thomas Fossati Signed-off-by: Daniel Lezcano Reviewed-by: Krzysztof Kozlowski --- .../bindings/watchdog/nxp,s32g2-swt.yaml | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Documentation/devicetree/bindings/watchdog/nxp,s32g2-sw= t.yaml diff --git a/Documentation/devicetree/bindings/watchdog/nxp,s32g2-swt.yaml = b/Documentation/devicetree/bindings/watchdog/nxp,s32g2-swt.yaml new file mode 100644 index 000000000000..d5f2513c4e8f --- /dev/null +++ b/Documentation/devicetree/bindings/watchdog/nxp,s32g2-swt.yaml @@ -0,0 +1,42 @@ +# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/watchdog/nxp,s32g2-swt.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: NXP Software Watchdog Timer (SWT) + +maintainers: + - Daniel Lezcano + +allOf: + - $ref: watchdog.yaml# + +properties: + compatible: + - const: nxp,s32g2-swt + - items: + - const: nxp,s32g3-swt + - const: nxp,s32g2-swt + + reg: + maxItems: 1 + + clocks: + maxItems: 1 + +required: + - compatible + - reg + - clocks + +unevaluatedProperties: false + +examples: + - | + watchdog@0x40100000 { + compatible =3D "nxp,s32g2-swt"; + reg =3D <0x40100000 0x1000>; + clocks =3D <&clks 0x3a>; + timeout-sec =3D <10>; + }; --=20 2.43.0 From nobody Sun Feb 8 00:49:29 2026 Received: from mail-wm1-f47.google.com (mail-wm1-f47.google.com [209.85.128.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C5D8818A6AF for ; Wed, 2 Apr 2025 15:49:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743609002; cv=none; b=KNki44S5nZ+4f+RveqeD+WEda2kx2SJvhN/u+SuSQ3TkSCXiVZCj4GwoEYFdwO4gCIBrLkGCCdt0liG6ZojoWiQMKSvXWIrydV96L6tTBerXc0RBd8uy2UakmVVKMJlsnER16aqH4WA92DInqVMbkZRx2IB15HwYOhsmnF7E5eo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743609002; c=relaxed/simple; bh=d5wzqhF1C6/brSGYtVhBYh/qx/95Dh4C+p/dbB29ox4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZKhYHW4rL6BVa0E0XLQ/oi2aRZctN1yVWabOB7fI8nBZe5g3kkmDidXKTliFu2qg+bli5BewagTZ2sDKa69BXUz7v9gMdZ6iGzMe/ytDqKXRH2/VtBb7+2euBfvo2A4r+BAGfaWoanonza3V87lg5fJ3w/ATZXDMChYkjmCqIeI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=bS2SX+i4; arc=none smtp.client-ip=209.85.128.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="bS2SX+i4" Received: by mail-wm1-f47.google.com with SMTP id 5b1f17b1804b1-43ce71582e9so51000335e9.1 for ; Wed, 02 Apr 2025 08:49:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1743608997; x=1744213797; 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; bh=VVtLkb2Bbr4/7b8+mKoLKaWYpy6EsWqHyXglOpYgfE0=; b=bS2SX+i4FHX1F/iSWKFZU2uxIUvbCGMwEgMtNrKt1YVLEFw9HkvoOoTEj/7YD3BF0t rPA9AD0Rp0hMDCehNwQ8hzdEN1y+TH13iYXwS3TuXvR1SNPa5/28ybPPGHadmOdyBqeL Od6T9FUFh+AscD7A7Kq4oSePO/cweBD4MvGnznPhf5vrYUrgCc+cxHbXUkZpsEpp0clP MqWCP5wpFTn+XWtTPORsf5QeRTQUK8den2tNR9ElHwcJw1EECpbIGpbuiXKVIZtkdevX zli3+cBr3aDkb3HJXy2SygeS5v9i6oSv1GSijhuu/Y1ijn6Jjc/yWwpt8M9NABz1yCfS lDWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1743608997; x=1744213797; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=VVtLkb2Bbr4/7b8+mKoLKaWYpy6EsWqHyXglOpYgfE0=; b=P0CXUVf6opFD0q/3sxJj6kOgd+efU8I+e+yNKvC1EM6Vc5fuFuFeiUCZZHMkgG89oP /oxYp8KdXWDXwrLN91z3606BLnKdSkOoN65grIPE/EfZI73TF8yMB48hxIe521zXnezi dG0AMsN7o45AX+XnqeKMiYgg2SdWWyo8t8HfFc0OsYJKiWtK/qH+ogD79xsA5Q5WyBMG g+LS5mpZxWvg0FGMXfnZjRgTUGNxulG2n0Cw+epDtorskhdZv40kRwKr0UaCA9EmfowW IBzLdEod2p58Q25swlxBfceDwUHwUeXkwcfCYlJowp6BpUpnhAmAFBGOo0G+xYXeLwTq 2rbg== X-Forwarded-Encrypted: i=1; AJvYcCUBnpYwlCDiEh1/ytvAJwEj7T8UJZCpL+l7gwNPsAC+O4OCgxMbyQ6E7YxmAQJdo8BnQY5/sI5wMlpABsc=@vger.kernel.org X-Gm-Message-State: AOJu0YyTmYU7NudQxsKAXtTIfM6AOUFeTF3Qy5gNNwIxXOPNQW7CwY8s lBXiVX3LtIHGB5cwkJJiHImXeqg7jLH34BHs9k5zy0vz1io4KlJUKrCgrdf97RY= X-Gm-Gg: ASbGnctMlv5soxYikm8G8NIcOzVaRjIejhW28FsEtKWYa64HTOZREwLTg+sRRAn/nM/ ttFOqIhZGghr6KNp5s3ls8LKFCSeKfCPViD1qalrlf1t8EBOdQkp69nBfxDNO48+XiTLa3LX8fD wi/6NLp2Ze+3U/pN8U5v1c786Qs+R/Xt5ZcetRhgG4l5Z62+Tf2V4C9LMjreis1wjpdE50hz2Qx 3LoygZ08ZV9U9DoxR8uGh5M0kJ9EpGLMESwy8zE+q/zbCrCgEdeXRYIyMst9lPemLNV0QQzcM8Z P8W+EBjao3Zi43IR3OJr14xvpQwvGXpsgIpwKZ1BAD4E0kK+bzVPHASdVWkfbD52ayA3i/sB+tp d/9JulA== X-Google-Smtp-Source: AGHT+IHhI7QlLg3v5yIM1Xc3dRndQsg7I8qkqKkxG29iOdf4PooPFJp2ZRsxH5pLL43aIVk2wngecw== X-Received: by 2002:a05:600c:468c:b0:43c:fa3f:8e5d with SMTP id 5b1f17b1804b1-43db61b52e5mr185578715e9.2.1743608996840; Wed, 02 Apr 2025 08:49:56 -0700 (PDT) Received: from mai.. (146725694.box.freepro.com. [130.180.211.218]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-43eb5fd1194sm24674555e9.11.2025.04.02.08.49.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Apr 2025 08:49:56 -0700 (PDT) From: Daniel Lezcano To: wim@linux-watchdog.org Cc: linux@roeck-us.net, linux-watchdog@vger.kernel.org, linux-kernel@vger.kernel.org, S32@nxp.com, ghennadi.procopciuc@nxp.com, thomas.fossati@linaro.org, robh@kernel.org, krzk+dt@kernel.org, conor+dt@kernel.org, devicetree@vger.kernel.org Subject: [PATCH v2 2/2] watchdog: Add the Watchdog Timer for the NXP S32 platform Date: Wed, 2 Apr 2025 17:49:40 +0200 Message-ID: <20250402154942.3645283-3-daniel.lezcano@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250402154942.3645283-1-daniel.lezcano@linaro.org> References: <20250402154942.3645283-1-daniel.lezcano@linaro.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The S32 platform has several Watchdog Timer available and tied with a CPU. The SWT0 is the only one which directly asserts the reset line, other SWT require an external setup to configure the reset behavior which is not part of this change. As a side note, in the NXP documentation, the s32g2 and s32g3 reference manuals refer the watchdog as the 'Software Timer Watchdog' where the name can be misleading as it is actually a hardware watchdog. The maximum watchdog timeout value depends on the clock feeding the SWT counter which is 32bits wide. On the s32g274-rb2, the clock has a rate of 51MHz which result on 83 seconds maximum timeout. The timeout can be specified via the device tree with the usual existing bindings 'timeout-sec' or via the module param timeout. The watchdog can be loaded with the 'nowayout' option, preventing the watchdog to be stopped. The watchdog can be started at boot time with the 'early-enable' option, thus letting the watchdog framework to service the watchdog counter. The watchdog support the magic character to stop when the userspace releases the device. Cc: Ghennadi Procopciuc Cc: Thomas Fossati Signed-off-by: Daniel Lezcano --- drivers/watchdog/Kconfig | 9 ++ drivers/watchdog/Makefile | 1 + drivers/watchdog/s32g_wdt.c | 313 ++++++++++++++++++++++++++++++++++++ 3 files changed, 323 insertions(+) create mode 100644 drivers/watchdog/s32g_wdt.c diff --git a/drivers/watchdog/Kconfig b/drivers/watchdog/Kconfig index f81705f8539a..4ab4275ef49f 100644 --- a/drivers/watchdog/Kconfig +++ b/drivers/watchdog/Kconfig @@ -792,6 +792,15 @@ config IMX7ULP_WDT To compile this driver as a module, choose M here: the module will be called imx7ulp_wdt. =20 +config S32G_WDT + tristate "S32G Watchdog" + depends on ARCH_S32 || COMPILE_TEST + select WATCHDOG_CORE + help + This is the driver for the hardware watchdog on the NXP + S32G platforms. If you wish to have watchdog support + enabled, say Y, otherwise say N. + config DB500_WATCHDOG tristate "ST-Ericsson DB800 watchdog" depends on MFD_DB8500_PRCMU diff --git a/drivers/watchdog/Makefile b/drivers/watchdog/Makefile index 8411626fa162..d0f9826e32c3 100644 --- a/drivers/watchdog/Makefile +++ b/drivers/watchdog/Makefile @@ -69,6 +69,7 @@ obj-$(CONFIG_TS72XX_WATCHDOG) +=3D ts72xx_wdt.o obj-$(CONFIG_IMX2_WDT) +=3D imx2_wdt.o obj-$(CONFIG_IMX_SC_WDT) +=3D imx_sc_wdt.o obj-$(CONFIG_IMX7ULP_WDT) +=3D imx7ulp_wdt.o +obj-$(CONFIG_S32G_WDT) +=3D s32g_wdt.o obj-$(CONFIG_DB500_WATCHDOG) +=3D db8500_wdt.o obj-$(CONFIG_RETU_WATCHDOG) +=3D retu_wdt.o obj-$(CONFIG_BCM2835_WDT) +=3D bcm2835_wdt.o diff --git a/drivers/watchdog/s32g_wdt.c b/drivers/watchdog/s32g_wdt.c new file mode 100644 index 000000000000..a585ff1b372c --- /dev/null +++ b/drivers/watchdog/s32g_wdt.c @@ -0,0 +1,313 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Watchdog driver for S32G SoC + * + * Copyright 2017-2019, 2021-2025 NXP. + * + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define DRIVER_NAME "s32g-swt" + +#define S32G_SWT_CR(__base) ((__base) + 0x00) /* Control Register offset */ +#define S32G_SWT_CR_SM (BIT(9) | BIT(10)) /* -> Service Mode */ +#define S32G_SWT_CR_STP BIT(2) /* -> Stop Mode Control */ +#define S32G_SWT_CR_FRZ BIT(1) /* -> Debug Mode Control */ +#define S32G_SWT_CR_WEN BIT(0) /* -> Watchdog Enable */ + +#define S32G_SWT_TO(__base) ((__base) + 0x08) /* Timeout Register offset */ + +#define S32G_SWT_SR(__base) ((__base) + 0x10) /* Service Register offset */ +#define S32G_WDT_SEQ1 0xA602 /* -> service sequence number 1 */ +#define S32G_WDT_SEQ2 0xB480 /* -> service sequence number 2 */ + +#define S32G_SWT_CO(__base) ((__base) + 0x14) /* Counter output register */ + +#define S32G_WDT_DEFAULT_TIMEOUT 30 + +struct s32g_wdt_device { + int rate; + void __iomem *base; + struct watchdog_device wdog; +}; + +static bool nowayout =3D WATCHDOG_NOWAYOUT; +module_param(nowayout, bool, 0); +MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (defau= lt=3D" + __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); + +static unsigned int timeout_param =3D S32G_WDT_DEFAULT_TIMEOUT; +module_param(timeout_param, uint, 0); +MODULE_PARM_DESC(timeout_param, "Watchdog timeout in seconds (default=3D" + __MODULE_STRING(S32G_WDT_DEFAULT_TIMEOUT) ")"); + +static bool early_enable; +module_param(early_enable, bool, 0); +MODULE_PARM_DESC(early_enable, + "Watchdog is started on module insertion (default=3Dfalse)"); + +static const struct watchdog_info s32g_wdt_info =3D { + .identity =3D "s32g watchdog", + .options =3D WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE | + WDIOC_GETTIMEOUT | WDIOC_GETTIMELEFT, +}; + +static struct s32g_wdt_device *wdd_to_s32g_wdt(struct watchdog_device *wdd) +{ + return container_of(wdd, struct s32g_wdt_device, wdog); +} + +static unsigned int wdog_sec_to_count(struct s32g_wdt_device *wdev, unsign= ed int timeout) +{ + return wdev->rate * timeout; +} + +static int s32g_wdt_ping(struct watchdog_device *wdog) +{ + struct s32g_wdt_device *wdev =3D wdd_to_s32g_wdt(wdog); + + writel(S32G_WDT_SEQ1, S32G_SWT_SR(wdev->base)); + writel(S32G_WDT_SEQ2, S32G_SWT_SR(wdev->base)); + + return 0; +} + +static int s32g_wdt_start(struct watchdog_device *wdog) +{ + struct s32g_wdt_device *wdev =3D wdd_to_s32g_wdt(wdog); + unsigned long val; + + val =3D readl(S32G_SWT_CR(wdev->base)); + + val |=3D S32G_SWT_CR_WEN; + + writel(val, S32G_SWT_CR(wdev->base)); + + return 0; +} + +static int s32g_wdt_stop(struct watchdog_device *wdog) +{ + struct s32g_wdt_device *wdev =3D wdd_to_s32g_wdt(wdog); + unsigned long val; + + val =3D readl(S32G_SWT_CR(wdev->base)); + + val &=3D ~S32G_SWT_CR_WEN; + + writel(val, S32G_SWT_CR(wdev->base)); + + return 0; +} + +static int s32g_wdt_set_timeout(struct watchdog_device *wdog, unsigned int= timeout) +{ + struct s32g_wdt_device *wdev =3D wdd_to_s32g_wdt(wdog); + + writel(wdog_sec_to_count(wdev, timeout), S32G_SWT_TO(wdev->base)); + + /* + * Conforming to the documentation, the timeout counter is + * loaded when servicing is operated (aka ping) or when the + * counter is enabled. In case the watchdog is already started + * it must be stopped and started again to update the timeout + * register or a ping can be sent to refresh the counter. Here + * we choose to send a ping to the watchdog which is harmless + * if the watchdog is stopped. + */ + return s32g_wdt_ping(wdog); +} + +static unsigned int s32g_wdt_get_timeleft(struct watchdog_device *wdog) +{ + struct s32g_wdt_device *wdev =3D wdd_to_s32g_wdt(wdog); + unsigned long counter; + bool is_running; + + /* + * The counter output can be read only if the SWT is + * disabled. Given the latency between the internal counter + * and the counter output update, there can be very small + * difference. However, we can accept this matter of fact + * given the resolution is a second based unit for the output. + */ + is_running =3D watchdog_hw_running(wdog); + + if (is_running) + s32g_wdt_stop(wdog); + + counter =3D readl(S32G_SWT_CO(wdev->base)); + + if (is_running) + s32g_wdt_start(wdog); + + return counter / wdev->rate; +} + +static const struct watchdog_ops s32g_wdt_ops =3D { + .owner =3D THIS_MODULE, + .start =3D s32g_wdt_start, + .stop =3D s32g_wdt_stop, + .ping =3D s32g_wdt_ping, + .set_timeout =3D s32g_wdt_set_timeout, + .get_timeleft =3D s32g_wdt_get_timeleft, +}; + +static void s32g_wdt_init(struct s32g_wdt_device *wdev) +{ + unsigned long val; + + /* Set the watchdog's Time-Out value */ + val =3D wdog_sec_to_count(wdev, wdev->wdog.timeout); + + writel(val, S32G_SWT_TO(wdev->base)); + + /* + * Get the control register content. We are at init time, the + * watchdog should not be started. + */ + val =3D readl(S32G_SWT_CR(wdev->base)); + + /* + * We want to allow the watchdog timer to be stopped when + * device enters debug mode. + */ + val |=3D S32G_SWT_CR_FRZ; + + /* + * However, when the CPU is in WFI or suspend mode, the + * watchdog must continue. The documentation refers it as the + * stopped mode. + */ + val &=3D ~S32G_SWT_CR_STP; + + /* + * Use Fixed Service Sequence to ping the watchdog which is + * 0x00 configuration value for the service mode. It should be + * already set because it is the default value but we reset it + * in case. + */ + val &=3D ~S32G_SWT_CR_SM; + + writel(val, S32G_SWT_CR(wdev->base)); + + /* + * When the 'early_enable' option is set, we start the + * watchdog from the kernel. + */ + if (early_enable) { + s32g_wdt_start(&wdev->wdog); + set_bit(WDOG_HW_RUNNING, &wdev->wdog.status); + } +} + +static int s32g_wdt_probe(struct platform_device *pdev) +{ + struct device *dev =3D &pdev->dev; + struct resource *res; + struct clk *clk; + struct s32g_wdt_device *wdev; + struct watchdog_device *wdog; + int ret; + + wdev =3D devm_kzalloc(dev, sizeof(*wdev), GFP_KERNEL); + if (!wdev) + return -ENOMEM; + + res =3D platform_get_resource(pdev, IORESOURCE_MEM, 0); + wdev->base =3D devm_ioremap_resource(dev, res); + if (IS_ERR(wdev->base)) + return dev_err_probe(&pdev->dev, PTR_ERR(wdev->base), "Can not get resou= rce\n"); + + clk =3D devm_clk_get_enabled(dev, NULL); + if (IS_ERR(clk)) + return dev_err_probe(dev, PTR_ERR(clk), "Can't get Watchdog clock\n"); + + wdev->rate =3D clk_get_rate(clk); + if (!wdev->rate) { + dev_err(dev, "Input clock rate is not valid\n"); + return -EINVAL; + } + + wdog =3D &wdev->wdog; + wdog->info =3D &s32g_wdt_info; + wdog->ops =3D &s32g_wdt_ops; + + /* + * The code converts the timeout into a counter a value, if + * the value is less than 0x100, then it is clamped by the SWT + * module, so it is safe to specify a zero value as the + * minimum timeout. + */ + wdog->min_timeout =3D 0; + + /* + * The counter register is a 32 bits long, so the maximum + * counter value is UINT_MAX and the timeout in second is the + * value divided by the rate. + * + * For instance, a rate of 51MHz lead to 84 seconds maximum + * timeout. + */ + wdog->max_timeout =3D UINT_MAX / wdev->rate; + + /* + * The module param and the DT 'timeout-sec' property will + * override the default value if they are specified. + */ + ret =3D watchdog_init_timeout(wdog, timeout_param, dev); + if (ret) + return ret; + + /* + * As soon as the watchdog is started, there is no way to stop + * it if the 'nowayout' option is set at boot time + */ + watchdog_set_nowayout(wdog, nowayout); + + /* + * The devm_ version of the watchdog_register_device() + * function will call watchdog_unregister_device() when the + * device is removed. + */ + watchdog_stop_on_unregister(wdog); + + s32g_wdt_init(wdev); + + ret =3D devm_watchdog_register_device(dev, wdog); + if (ret) + return dev_err_probe(dev, ret, "Cannot register watchdog device\n"); + + dev_info(dev, "S32G Watchdog Timer Registered, timeout=3D%ds, nowayout=3D= %d, early_enable=3D%d\n", + wdog->timeout, nowayout, early_enable); + + return 0; +} + +static const struct of_device_id s32g_wdt_dt_ids[] =3D { + { .compatible =3D "nxp,s32g2-swt" }, + { /* sentinel */ } +}; +MODULE_DEVICE_TABLE(of, s32g_wdt_dt_ids); + +static struct platform_driver s32g_wdt_driver =3D { + .probe =3D s32g_wdt_probe, + .driver =3D { + .name =3D DRIVER_NAME, + .of_match_table =3D s32g_wdt_dt_ids, + }, +}; + +module_platform_driver(s32g_wdt_driver); + +MODULE_AUTHOR("Daniel Lezcano "); +MODULE_DESCRIPTION("Watchdog driver for S32G SoC"); +MODULE_LICENSE("GPL"); --=20 2.43.0