1 Add a watchdog driver for ARM MPcore processors.
3 Signed-off-by: Felix Fietkau <nbd@nbd.name>
4 --- a/drivers/watchdog/Kconfig
5 +++ b/drivers/watchdog/Kconfig
6 @@ -355,6 +355,13 @@ config KS8695_WATCHDOG
7 Watchdog timer embedded into KS8695 processor. This will reboot your
8 system when the timeout is reached.
10 +config MPCORE_WATCHDOG
11 + tristate "MPcore watchdog"
12 + depends on HAVE_ARM_TWD
13 + select WATCHDOG_CORE
15 + Watchdog timer embedded into the MPcore system
17 config HAVE_S3C2410_WATCHDOG
20 --- a/drivers/watchdog/Makefile
21 +++ b/drivers/watchdog/Makefile
22 @@ -49,6 +49,7 @@ obj-$(CONFIG_977_WATCHDOG) += wdt977.o
23 obj-$(CONFIG_GEMINI_WATCHDOG) += gemini_wdt.o
24 obj-$(CONFIG_IXP4XX_WATCHDOG) += ixp4xx_wdt.o
25 obj-$(CONFIG_KS8695_WATCHDOG) += ks8695_wdt.o
26 +obj-$(CONFIG_MPCORE_WATCHDOG) += mpcore_wdt.o
27 obj-$(CONFIG_S3C2410_WATCHDOG) += s3c2410_wdt.o
28 obj-$(CONFIG_SA1100_WATCHDOG) += sa1100_wdt.o
29 obj-$(CONFIG_SAMA5D4_WATCHDOG) += sama5d4_wdt.o
31 +++ b/drivers/watchdog/mpcore_wdt.c
34 + * Watchdog driver for ARM MPcore
36 + * Copyright (C) 2017 Felix Fietkau <nbd@nbd.name>
39 +#include <linux/export.h>
40 +#include <linux/module.h>
41 +#include <linux/kernel.h>
42 +#include <linux/watchdog.h>
43 +#include <linux/platform_device.h>
44 +#include <linux/io.h>
45 +#include <asm/smp_twd.h>
47 +static void __iomem *wdt_base;
48 +static int wdt_timeout = 60;
50 +static int mpcore_wdt_keepalive(struct watchdog_device *wdd)
55 + count = (twd_timer_get_rate() / 256) * wdt_timeout;
57 + /* Reload register needs a different value on each refresh */
61 + iowrite32(count, wdt_base + TWD_WDOG_LOAD);
66 +static int mpcore_wdt_start(struct watchdog_device *wdd)
68 + mpcore_wdt_keepalive(wdd);
70 + /* prescale = 256, mode = 1, enable = 1 */
71 + iowrite32(0x0000FF09, wdt_base + TWD_WDOG_CONTROL);
76 +static int mpcore_wdt_stop(struct watchdog_device *wdd)
78 + iowrite32(0x12345678, wdt_base + TWD_WDOG_DISABLE);
79 + iowrite32(0x87654321, wdt_base + TWD_WDOG_DISABLE);
80 + iowrite32(0x0, wdt_base + TWD_WDOG_CONTROL);
85 +static int mpcore_wdt_set_timeout(struct watchdog_device *wdd,
86 + unsigned int timeout)
88 + mpcore_wdt_stop(wdd);
89 + wdt_timeout = timeout;
90 + mpcore_wdt_start(wdd);
95 +static const struct watchdog_info mpcore_wdt_info = {
96 + .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
97 + .identity = "MPcore Watchdog",
100 +static const struct watchdog_ops mpcore_wdt_ops = {
101 + .owner = THIS_MODULE,
102 + .start = mpcore_wdt_start,
103 + .stop = mpcore_wdt_stop,
104 + .ping = mpcore_wdt_keepalive,
105 + .set_timeout = mpcore_wdt_set_timeout,
108 +static struct watchdog_device mpcore_wdt = {
109 + .info = &mpcore_wdt_info,
110 + .ops = &mpcore_wdt_ops,
112 + .max_timeout = 65535,
115 +static int mpcore_wdt_probe(struct platform_device *pdev)
117 + struct resource *res;
118 + unsigned long rate = twd_timer_get_rate();
120 + pr_info("MPCore WD init. clockrate: %lu prescaler: %u countrate: %lu timeout: %us\n", rate, 256, rate / 256, wdt_timeout);
122 + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
126 + wdt_base = devm_ioremap_resource(&pdev->dev, res);
127 + if (IS_ERR(wdt_base))
128 + return PTR_ERR(wdt_base);
130 + watchdog_register_device(&mpcore_wdt);
134 +static int mpcore_wdt_remove(struct platform_device *dev)
136 + watchdog_unregister_device(&mpcore_wdt);
140 +static struct platform_driver mpcore_wdt_driver = {
141 + .probe = mpcore_wdt_probe,
142 + .remove = mpcore_wdt_remove,
144 + .name = "mpcore_wdt",
148 +module_platform_driver(mpcore_wdt_driver);
149 +MODULE_AUTHOR("Felix Fietkau <nbd@nbd.name>");
150 +MODULE_LICENSE("GPL");
151 --- a/arch/arm/include/asm/smp_twd.h
152 +++ b/arch/arm/include/asm/smp_twd.h
153 @@ -34,5 +34,6 @@ struct twd_local_timer name __initdata =
156 int twd_local_timer_register(struct twd_local_timer *);
157 +unsigned long twd_timer_get_rate(void);
160 --- a/arch/arm/kernel/smp_twd.c
161 +++ b/arch/arm/kernel/smp_twd.c
163 #include <linux/delay.h>
164 #include <linux/device.h>
165 #include <linux/err.h>
166 +#include <linux/export.h>
167 #include <linux/smp.h>
168 #include <linux/jiffies.h>
169 #include <linux/clockchips.h>
170 @@ -380,6 +381,14 @@ int __init twd_local_timer_register(stru
171 return twd_local_timer_common_register(NULL);
174 +/* Needed by mpcore_wdt */
175 +unsigned long twd_timer_get_rate(void)
177 + return twd_timer_rate;
179 +EXPORT_SYMBOL_GPL(twd_timer_get_rate);
183 static int __init twd_local_timer_of_register(struct device_node *np)