1 From f51b3b84af840ea52170ae6444ddee26ec74f7a9 Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Mon, 22 Apr 2013 23:23:07 +0200
4 Subject: [PATCH 25/33] watchdog: adds ralink wdt
6 Adds the watchdog driver for ralink SoC.
8 Signed-off-by: John Crispin <blogic@openwrt.org>
10 arch/mips/ralink/mt7620.c | 1 +
11 drivers/watchdog/Kconfig | 7 ++
12 drivers/watchdog/Makefile | 1 +
13 drivers/watchdog/rt2880_wdt.c | 207 +++++++++++++++++++++++++++++++++++++++++
14 4 files changed, 216 insertions(+)
15 create mode 100644 drivers/watchdog/rt2880_wdt.c
17 --- a/arch/mips/ralink/mt7620.c
18 +++ b/arch/mips/ralink/mt7620.c
19 @@ -182,6 +182,7 @@ void __init ralink_clk_init(void)
21 ralink_clk_add("cpu", cpu_rate);
22 ralink_clk_add("10000100.timer", 40000000);
23 + ralink_clk_add("10000120.watchdog", 40000000);
24 ralink_clk_add("10000500.uart", 40000000);
25 ralink_clk_add("10000b00.spi", 40000000);
26 ralink_clk_add("10000c00.uartlite", 40000000);
27 --- a/drivers/watchdog/Kconfig
28 +++ b/drivers/watchdog/Kconfig
29 @@ -1104,6 +1104,13 @@ config LANTIQ_WDT
31 Hardware driver for the Lantiq SoC Watchdog Timer.
34 + tristate "Ralink SoC watchdog"
35 + select WATCHDOG_CORE
38 + Hardware driver for the Ralink SoC Watchdog Timer.
42 # POWERPC Architecture
43 --- a/drivers/watchdog/Makefile
44 +++ b/drivers/watchdog/Makefile
45 @@ -134,6 +134,7 @@ obj-$(CONFIG_TXX9_WDT) += txx9wdt.o
46 obj-$(CONFIG_OCTEON_WDT) += octeon-wdt.o
47 octeon-wdt-y := octeon-wdt-main.o octeon-wdt-nmi.o
48 obj-$(CONFIG_LANTIQ_WDT) += lantiq_wdt.o
49 +obj-$(CONFIG_RALINK_WDT) += rt2880_wdt.o
54 +++ b/drivers/watchdog/rt2880_wdt.c
57 + * Ralink RT288x/RT3xxx/MT76xx built-in hardware watchdog timer
59 + * Copyright (C) 2011 Gabor Juhos <juhosg@openwrt.org>
60 + * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
62 + * This driver was based on: drivers/watchdog/softdog.c
64 + * This program is free software; you can redistribute it and/or modify it
65 + * under the terms of the GNU General Public License version 2 as published
66 + * by the Free Software Foundation.
69 +#include <linux/clk.h>
70 +#include <linux/reset.h>
71 +#include <linux/module.h>
72 +#include <linux/kernel.h>
73 +#include <linux/watchdog.h>
74 +#include <linux/miscdevice.h>
75 +#include <linux/moduleparam.h>
76 +#include <linux/platform_device.h>
78 +#include <asm/mach-ralink/ralink_regs.h>
80 +#define SYSC_RSTSTAT 0x38
81 +#define WDT_RST_CAUSE BIT(1)
83 +#define RALINK_WDT_TIMEOUT 30
84 +#define RALINK_WDT_PRESCALE 65536
86 +#define TIMER_REG_TMR1LOAD 0x00
87 +#define TIMER_REG_TMR1CTL 0x08
89 +#define TMRSTAT_TMR1RST BIT(5)
91 +#define TMR1CTL_ENABLE BIT(7)
92 +#define TMR1CTL_MODE_SHIFT 4
93 +#define TMR1CTL_MODE_MASK 0x3
94 +#define TMR1CTL_MODE_FREE_RUNNING 0x0
95 +#define TMR1CTL_MODE_PERIODIC 0x1
96 +#define TMR1CTL_MODE_TIMEOUT 0x2
97 +#define TMR1CTL_MODE_WDT 0x3
98 +#define TMR1CTL_PRESCALE_MASK 0xf
99 +#define TMR1CTL_PRESCALE_65536 0xf
101 +static struct clk *rt288x_wdt_clk;
102 +static unsigned long rt288x_wdt_freq;
103 +static void __iomem *rt288x_wdt_base;
105 +static bool nowayout = WATCHDOG_NOWAYOUT;
106 +module_param(nowayout, bool, 0);
107 +MODULE_PARM_DESC(nowayout,
108 + "Watchdog cannot be stopped once started (default="
109 + __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
111 +static inline void rt_wdt_w32(unsigned reg, u32 val)
113 + iowrite32(val, rt288x_wdt_base + reg);
116 +static inline u32 rt_wdt_r32(unsigned reg)
118 + return ioread32(rt288x_wdt_base + reg);
121 +static int rt288x_wdt_ping(struct watchdog_device *w)
123 + rt_wdt_w32(TIMER_REG_TMR1LOAD, w->timeout * rt288x_wdt_freq);
128 +static int rt288x_wdt_start(struct watchdog_device *w)
132 + t = rt_wdt_r32(TIMER_REG_TMR1CTL);
133 + t &= ~(TMR1CTL_MODE_MASK << TMR1CTL_MODE_SHIFT |
134 + TMR1CTL_PRESCALE_MASK);
135 + t |= (TMR1CTL_MODE_WDT << TMR1CTL_MODE_SHIFT |
136 + TMR1CTL_PRESCALE_65536);
137 + rt_wdt_w32(TIMER_REG_TMR1CTL, t);
139 + rt288x_wdt_ping(w);
141 + t = rt_wdt_r32(TIMER_REG_TMR1CTL);
142 + t |= TMR1CTL_ENABLE;
143 + rt_wdt_w32(TIMER_REG_TMR1CTL, t);
148 +static int rt288x_wdt_stop(struct watchdog_device *w)
152 + rt288x_wdt_ping(w);
154 + t = rt_wdt_r32(TIMER_REG_TMR1CTL);
155 + t &= ~TMR1CTL_ENABLE;
156 + rt_wdt_w32(TIMER_REG_TMR1CTL, t);
161 +static int rt288x_wdt_set_timeout(struct watchdog_device *w, unsigned int t)
164 + rt288x_wdt_ping(w);
169 +static int rt288x_wdt_bootcause(void)
171 + if (rt_sysc_r32(SYSC_RSTSTAT) & WDT_RST_CAUSE)
172 + return WDIOF_CARDRESET;
177 +static struct watchdog_info rt288x_wdt_info = {
178 + .identity = "Ralink Watchdog",
179 + .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
182 +static struct watchdog_ops rt288x_wdt_ops = {
183 + .owner = THIS_MODULE,
184 + .start = rt288x_wdt_start,
185 + .stop = rt288x_wdt_stop,
186 + .ping = rt288x_wdt_ping,
187 + .set_timeout = rt288x_wdt_set_timeout,
190 +static struct watchdog_device rt288x_wdt_dev = {
191 + .info = &rt288x_wdt_info,
192 + .ops = &rt288x_wdt_ops,
196 +static int rt288x_wdt_probe(struct platform_device *pdev)
198 + struct resource *res;
201 + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
202 + rt288x_wdt_base = devm_request_and_ioremap(&pdev->dev, res);
203 + if (IS_ERR(rt288x_wdt_base))
204 + return PTR_ERR(rt288x_wdt_base);
206 + rt288x_wdt_clk = devm_clk_get(&pdev->dev, NULL);
207 + if (IS_ERR(rt288x_wdt_clk))
208 + return PTR_ERR(rt288x_wdt_clk);
210 + device_reset(&pdev->dev);
212 + rt288x_wdt_freq = clk_get_rate(rt288x_wdt_clk) / RALINK_WDT_PRESCALE;
214 + rt288x_wdt_dev.dev = &pdev->dev;
215 + rt288x_wdt_dev.bootstatus = rt288x_wdt_bootcause();
217 + rt288x_wdt_dev.timeout = rt288x_wdt_dev.max_timeout = (0xfffful / rt288x_wdt_freq);
219 + watchdog_set_nowayout(&rt288x_wdt_dev, nowayout);
221 + ret = watchdog_register_device(&rt288x_wdt_dev);
223 + dev_info(&pdev->dev, "Initialized\n");
228 +static int rt288x_wdt_remove(struct platform_device *pdev)
230 + watchdog_unregister_device(&rt288x_wdt_dev);
235 +static void rt288x_wdt_shutdown(struct platform_device *pdev)
237 + rt288x_wdt_stop(&rt288x_wdt_dev);
240 +static const struct of_device_id rt288x_wdt_match[] = {
241 + { .compatible = "ralink,rt2880-wdt" },
244 +MODULE_DEVICE_TABLE(of, rt288x_wdt_match);
246 +static struct platform_driver rt288x_wdt_driver = {
247 + .probe = rt288x_wdt_probe,
248 + .remove = rt288x_wdt_remove,
249 + .shutdown = rt288x_wdt_shutdown,
251 + .name = KBUILD_MODNAME,
252 + .owner = THIS_MODULE,
253 + .of_match_table = rt288x_wdt_match,
257 +module_platform_driver(rt288x_wdt_driver);
259 +MODULE_DESCRIPTION("MediaTek/Ralink RT288x/RT3xxx hardware watchdog driver");
260 +MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org");
261 +MODULE_LICENSE("GPL v2");
262 +MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);