From 855e835353a0e37e1f40c73913a2d00b64a6c65c Mon Sep 17 00:00:00 2001 From: Pip Cet Date: Sun, 8 Aug 2021 18:43:55 +0000 Subject: [PATCH 1/3] dt-bindings: watchdog: bindings for Apple M1 WDT This is the watchdog timer on Apple M1 systems. It is the standard way of rebooting these systems. Signed-off-by: Pip Cet --- .../bindings/watchdog/apple,wdt.yaml | 39 +++++++++++++++++++ MAINTAINERS | 1 + 2 files changed, 40 insertions(+) create mode 100644 Documentation/devicetree/bindings/watchdog/apple,wdt.yaml diff --git a/Documentation/devicetree/bindings/watchdog/apple,wdt.yaml b/Documentation/devicetree/bindings/watchdog/apple,wdt.yaml new file mode 100644 index 00000000000000..4751ad8049dca2 --- /dev/null +++ b/Documentation/devicetree/bindings/watchdog/apple,wdt.yaml @@ -0,0 +1,39 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/watchdog/apple,wdt.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Apple M1 Watchdog Timer Device Tree Bindings + +maintainers: + - Pip Cet + +allOf: + - $ref: watchdog.yaml# + +properties: + compatible: + enum: + - apple,t8103-wdt + + reg: + maxItems: 1 + + clocks: + maxItems: 1 + +required: + - compatible + - clocks + - reg + +additionalProperties: false + +examples: + - | + watchdog@3bd20000 { + compatible = "apple,t8103-wdt"; + reg = <0x3bd20000 0x4000>; + clocks = <&clk24>; + }; diff --git a/MAINTAINERS b/MAINTAINERS index 5f3ef429859456..71d82a3fbe2d4b 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -1702,6 +1702,7 @@ T: git https://github.com/AsahiLinux/linux.git F: Documentation/devicetree/bindings/arm/apple.yaml F: Documentation/devicetree/bindings/interrupt-controller/apple,aic.yaml F: Documentation/devicetree/bindings/pinctrl/apple,pinctrl.yaml +F: Documentation/devicetree/bindings/watchdog/apple,wdt.yaml F: arch/arm64/boot/dts/apple/ F: drivers/irqchip/irq-apple-aic.c F: include/dt-bindings/interrupt-controller/apple-aic.h From 68aacc32ba2d7511d27b2d010abe33afe64e897d Mon Sep 17 00:00:00 2001 From: Pip Cet Date: Sun, 8 Aug 2021 15:37:05 +0000 Subject: [PATCH 2/3] watchdog: apple_wdt: Add support for Apple M1 WDT This is the watchdog timer on Apple M1 systems. It is the standard way of rebooting these systems. Signed-off-by: Pip Cet --- MAINTAINERS | 1 + drivers/watchdog/Kconfig | 12 +++ drivers/watchdog/Makefile | 1 + drivers/watchdog/apple_wdt.c | 189 +++++++++++++++++++++++++++++++++++ 4 files changed, 203 insertions(+) create mode 100644 drivers/watchdog/apple_wdt.c diff --git a/MAINTAINERS b/MAINTAINERS index 71d82a3fbe2d4b..70474970eb0b70 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -1705,6 +1705,7 @@ F: Documentation/devicetree/bindings/pinctrl/apple,pinctrl.yaml F: Documentation/devicetree/bindings/watchdog/apple,wdt.yaml F: arch/arm64/boot/dts/apple/ F: drivers/irqchip/irq-apple-aic.c +F: drivers/watchdog/apple_wdt.c F: include/dt-bindings/interrupt-controller/apple-aic.h F: include/dt-bindings/pinctrl/apple.h diff --git a/drivers/watchdog/Kconfig b/drivers/watchdog/Kconfig index 546dfc1e2349c2..80cf24d83f0223 100644 --- a/drivers/watchdog/Kconfig +++ b/drivers/watchdog/Kconfig @@ -1001,6 +1001,18 @@ config MSC313E_WATCHDOG To compile this driver as a module, choose M here: the module will be called msc313e_wdt. +config APPLE_M1_WATCHDOG + tristate "Apple M1 watchdog timer support" + depends on ARCH_APPLE && OF || COMPILE_TEST + default ARCH_APPLE + select WATCHDOG_CORE + help + Say Y here to include support for the watchdog timer on Apple M1 + systems. This is the standard way of rebooting these systems. + + To compile this driver as a module, choose M here: the + module will be called apple_wdt. + # X86 (i386 + ia64 + x86_64) Architecture config ACQUIRE_WDT diff --git a/drivers/watchdog/Makefile b/drivers/watchdog/Makefile index abaf2ebd814e03..7c75f799f56955 100644 --- a/drivers/watchdog/Makefile +++ b/drivers/watchdog/Makefile @@ -94,6 +94,7 @@ obj-$(CONFIG_PM8916_WATCHDOG) += pm8916_wdt.o obj-$(CONFIG_ARM_SMC_WATCHDOG) += arm_smc_wdt.o obj-$(CONFIG_VISCONTI_WATCHDOG) += visconti_wdt.o obj-$(CONFIG_MSC313E_WATCHDOG) += msc313e_wdt.o +obj-$(CONFIG_APPLE_M1_WATCHDOG) += apple_wdt.o # X86 (i386 + ia64 + x86_64) Architecture obj-$(CONFIG_ACQUIRE_WDT) += acquirewdt.o diff --git a/drivers/watchdog/apple_wdt.c b/drivers/watchdog/apple_wdt.c new file mode 100644 index 00000000000000..fdb19c26c56ced --- /dev/null +++ b/drivers/watchdog/apple_wdt.c @@ -0,0 +1,189 @@ +// SPDX-License-Identifier: GPL-2.0+ OR MIT +/* + * Driver for Apple M1 WDT + * + * The Apple M1 WDT exposes a simple watchdog timer interface; there + * are also additional, more complicated features that haven't been + * fully reverse-engineered. + * + * This driver requires a clock, such as the fixed refclk at 24 MHz. + * + * Hardware documentation: + * + * https://github.com/AsahiLinux/docs/wiki/HW:WDT + * + * Copyright (C) 2021 Pip Cet + */ + +#include +#include +#include +#include +#include +#include +#include + +#define WDT_COUNT 0x10 +#define WDT_COMPARATOR 0x14 +#define WDT_CONTROL 0x1c +#define WDT_CONTROL_TRIGGER BIT(2) + +struct apple_wdt { + void __iomem *reg; + struct clk *clk; + u32 rate; +}; + +static int apple_wdt_start(struct watchdog_device *w) +{ + struct apple_wdt *wdt = watchdog_get_drvdata(w); + + writel(0, wdt->reg + WDT_COUNT); + writel(U32_MAX, wdt->reg + WDT_COMPARATOR); + writel(WDT_CONTROL_TRIGGER, wdt->reg + WDT_CONTROL); + + return 0; +} + +static int apple_wdt_stop(struct watchdog_device *w) +{ + struct apple_wdt *wdt = watchdog_get_drvdata(w); + + writel(0, wdt->reg + WDT_COUNT); + writel(U32_MAX, wdt->reg + WDT_COMPARATOR); + writel(0, wdt->reg + WDT_CONTROL); + + return 0; +} + +static int apple_wdt_ping(struct watchdog_device *w) +{ + struct apple_wdt *wdt = watchdog_get_drvdata(w); + + writel(0, wdt->reg + WDT_COUNT); + + return 0; +} + +static int apple_wdt_set_timeout(struct watchdog_device *w, unsigned int s) +{ + struct apple_wdt *wdt = watchdog_get_drvdata(w); + u64 comparator; + + comparator = mul_u32_u32(wdt->rate, s); + if (comparator > U32_MAX) + comparator = U32_MAX; + + writel(comparator, wdt->reg + WDT_COMPARATOR); + + return 0; +} + +static unsigned int apple_wdt_get_timeleft(struct watchdog_device *w) +{ + struct apple_wdt *wdt = watchdog_get_drvdata(w); + u32 comparator = readl(wdt->reg + WDT_COMPARATOR); + u32 count = readl(wdt->reg + WDT_COUNT); + + return (comparator - count) / wdt->rate; +} + +static int apple_wdt_restart(struct watchdog_device *w, unsigned long mode, + void *cmd) +{ + struct apple_wdt *wdt = watchdog_get_drvdata(w); + + writel(0, wdt->reg + WDT_COUNT); + writel(U32_MAX, wdt->reg + WDT_COMPARATOR); + writel(WDT_CONTROL_TRIGGER, wdt->reg + WDT_CONTROL); + writel(0, wdt->reg + WDT_COMPARATOR); + + return 0; +} + +static struct watchdog_ops apple_wdt_ops = { + .start = apple_wdt_start, + .stop = apple_wdt_stop, + .ping = apple_wdt_ping, + .set_timeout = apple_wdt_set_timeout, + .get_timeleft = apple_wdt_get_timeleft, + .restart = apple_wdt_restart, +}; + +static struct watchdog_info apple_wdt_info = { + .identity = "Apple WDT", + .options = WDIOF_SETTIMEOUT, +}; + +static int apple_wdt_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct apple_wdt *wdt = devm_kzalloc(dev, sizeof(*wdt), GFP_KERNEL); + struct watchdog_device *wd = devm_kzalloc(dev, sizeof(*wd), GFP_KERNEL); + int ret; + + if (!wdt || !wd) + return -ENOMEM; + + wdt->clk = devm_clk_get(dev, NULL); + if (IS_ERR(wdt->clk)) + return PTR_ERR(wdt->clk); + + wd->ops = &apple_wdt_ops; + wd->info = &apple_wdt_info; + + wdt->reg = devm_platform_ioremap_resource(pdev, 0); + if (IS_ERR(wdt->reg)) + return PTR_ERR(wdt->reg); + + ret = clk_prepare_enable(wdt->clk); + if (ret) + return ret; + + wdt->rate = clk_get_rate(wdt->clk); + + if (readl(wdt->reg + WDT_CONTROL) & WDT_CONTROL_TRIGGER) + wd->status |= WDOG_HW_RUNNING; + + ret = devm_watchdog_register_device(dev, wd); + if (ret < 0) { + clk_disable_unprepare(wdt->clk); + return ret; + } + + watchdog_set_drvdata(wd, wdt); + platform_set_drvdata(pdev, wd); + + return 0; +} + +static int apple_wdt_remove(struct platform_device *pdev) +{ + struct watchdog_device *wd = platform_get_drvdata(pdev); + struct apple_wdt *wdt = watchdog_get_drvdata(wd); + + clk_disable_unprepare(wdt->clk); + + return 0; +} + +static const struct of_device_id apple_wdt_of_match[] = { + { .compatible = "apple,t8103-wdt" }, + { }, +}; + +MODULE_DEVICE_TABLE(of, apple_wdt_of_match); + +static struct platform_driver apple_wdt_driver = { + .driver = { + .name = "apple-wdt", + .of_match_table = of_match_ptr(apple_wdt_of_match), + }, + .probe = apple_wdt_probe, + .remove = apple_wdt_remove, +}; +module_platform_driver(apple_wdt_driver); + +MODULE_AUTHOR("Pip Cet "); +MODULE_DESCRIPTION("Watchdog Timer driver for Apple M1"); +MODULE_LICENSE("Dual MIT/GPL"); From ff529ce09e3fd5869ff32649673e6a09c7758903 Mon Sep 17 00:00:00 2001 From: Pip Cet Date: Sun, 8 Aug 2021 22:58:27 +0000 Subject: [PATCH 3/3] arm64: apple: Add WDT to devicetree This is the watchdog timer on Apple M1 systems. It is the standard way of rebooting these systems. Signed-off-by: Pip Cet --- arch/arm64/boot/dts/apple/t8103.dtsi | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/arch/arm64/boot/dts/apple/t8103.dtsi b/arch/arm64/boot/dts/apple/t8103.dtsi index f0c10d1c6cf2ff..e3efd47bfb2844 100644 --- a/arch/arm64/boot/dts/apple/t8103.dtsi +++ b/arch/arm64/boot/dts/apple/t8103.dtsi @@ -182,5 +182,11 @@ dr_mode = "host"; iommus = <&dwc3_1_dart_0 0>, <&dwc3_1_dart_1 1>; }; + + watchdog@23d2b0000 { + compatible = "apple,t8103-wdt"; + reg = <0x2 0x3d2b0000 0x0 0x4000>; + clocks = <&clk24>; + }; }; };