1 Add a watchdog driver for ARM MPcore processors.
3 Signed-off-by: Felix Fietkau <nbd@nbd.name>
5 --- a/drivers/watchdog/Kconfig
6 +++ b/drivers/watchdog/Kconfig
7 @@ -324,6 +324,13 @@ config KS8695_WATCHDOG
8 Watchdog timer embedded into KS8695 processor. This will reboot your
9 system when the timeout is reached.
11 +config MPCORE_WATCHDOG
12 + tristate "MPcore watchdog"
13 + depends on HAVE_ARM_TWD
14 + select WATCHDOG_CORE
16 + Watchdog timer embedded into the MPcore system
18 config HAVE_S3C2410_WATCHDOG
21 --- a/drivers/watchdog/Makefile
22 +++ b/drivers/watchdog/Makefile
23 @@ -47,6 +47,7 @@ obj-$(CONFIG_21285_WATCHDOG) += wdt285.o
24 obj-$(CONFIG_977_WATCHDOG) += wdt977.o
25 obj-$(CONFIG_IXP4XX_WATCHDOG) += ixp4xx_wdt.o
26 obj-$(CONFIG_KS8695_WATCHDOG) += ks8695_wdt.o
27 +obj-$(CONFIG_MPCORE_WATCHDOG) += mpcore_wdt.o
28 obj-$(CONFIG_S3C2410_WATCHDOG) += s3c2410_wdt.o
29 obj-$(CONFIG_SA1100_WATCHDOG) += sa1100_wdt.o
30 obj-$(CONFIG_SAMA5D4_WATCHDOG) += sama5d4_wdt.o
32 +++ b/drivers/watchdog/mpcore_wdt.c
35 + * Watchdog driver for ARM MPcore
37 + * Copyright (C) 2017 Felix Fietkau <nbd@nbd.name>
40 +#include <linux/export.h>
41 +#include <linux/module.h>
42 +#include <linux/kernel.h>
43 +#include <linux/watchdog.h>
44 +#include <linux/platform_device.h>
45 +#include <linux/io.h>
46 +#include <asm/smp_twd.h>
48 +static void __iomem *wdt_base;
49 +static int wdt_timeout = 60;
51 +static int mpcore_wdt_keepalive(struct watchdog_device *wdd)
56 + count = (twd_timer_get_rate() / 256) * wdt_timeout;
58 + /* Reload register needs a different value on each refresh */
62 + iowrite32(count, wdt_base + TWD_WDOG_LOAD);
67 +static int mpcore_wdt_start(struct watchdog_device *wdd)
69 + mpcore_wdt_keepalive(wdd);
71 + /* prescale = 256, mode = 1, enable = 1 */
72 + iowrite32(0x0000FF09, wdt_base + TWD_WDOG_CONTROL);
77 +static int mpcore_wdt_stop(struct watchdog_device *wdd)
79 + iowrite32(0x12345678, wdt_base + TWD_WDOG_DISABLE);
80 + iowrite32(0x87654321, wdt_base + TWD_WDOG_DISABLE);
81 + iowrite32(0x0, wdt_base + TWD_WDOG_CONTROL);
86 +static int mpcore_wdt_set_timeout(struct watchdog_device *wdd,
87 + unsigned int timeout)
89 + mpcore_wdt_stop(wdd);
90 + wdt_timeout = timeout;
91 + mpcore_wdt_start(wdd);
96 +static const struct watchdog_info mpcore_wdt_info = {
97 + .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
98 + .identity = "MPcore Watchdog",
101 +static const struct watchdog_ops mpcore_wdt_ops = {
102 + .owner = THIS_MODULE,
103 + .start = mpcore_wdt_start,
104 + .stop = mpcore_wdt_stop,
105 + .ping = mpcore_wdt_keepalive,
106 + .set_timeout = mpcore_wdt_set_timeout,
109 +static struct watchdog_device mpcore_wdt = {
110 + .info = &mpcore_wdt_info,
111 + .ops = &mpcore_wdt_ops,
113 + .max_timeout = 65535,
116 +static int mpcore_wdt_probe(struct platform_device *pdev)
118 + struct resource *res;
119 + unsigned long rate = twd_timer_get_rate();
121 + pr_info("MPCore WD init. clockrate: %u prescaler: %u countrate: %u timeout: %us\n", rate, 256, rate / 256, wdt_timeout);
123 + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
127 + wdt_base = devm_ioremap_resource(&pdev->dev, res);
128 + if (IS_ERR(wdt_base))
129 + return PTR_ERR(wdt_base);
131 + watchdog_register_device(&mpcore_wdt);
135 +static int mpcore_wdt_remove(struct platform_device *dev)
137 + watchdog_unregister_device(&mpcore_wdt);
141 +static struct platform_driver mpcore_wdt_driver = {
142 + .probe = mpcore_wdt_probe,
143 + .remove = mpcore_wdt_remove,
145 + .name = "mpcore_wdt",
149 +module_platform_driver(mpcore_wdt_driver);
150 +MODULE_AUTHOR("Felix Fietkau <nbd@nbd.name>");
151 +MODULE_LICENSE("GPL");
152 --- a/arch/arm/include/asm/smp_twd.h
153 +++ b/arch/arm/include/asm/smp_twd.h
154 @@ -33,5 +33,6 @@ struct twd_local_timer name __initdata =
157 int twd_local_timer_register(struct twd_local_timer *);
158 +unsigned long twd_timer_get_rate(void);
161 --- a/arch/arm/kernel/smp_twd.c
162 +++ b/arch/arm/kernel/smp_twd.c
164 #include <linux/delay.h>
165 #include <linux/device.h>
166 #include <linux/err.h>
167 +#include <linux/export.h>
168 #include <linux/smp.h>
169 #include <linux/jiffies.h>
170 #include <linux/clockchips.h>
171 @@ -380,6 +381,14 @@ int __init twd_local_timer_register(stru
172 return twd_local_timer_common_register(NULL);
175 +/* Needed by mpcore_wdt */
176 +unsigned long twd_timer_get_rate(void)
178 + return twd_timer_rate;
180 +EXPORT_SYMBOL_GPL(twd_timer_get_rate);
184 static int __init twd_local_timer_of_register(struct device_node *np)