ramips: select kmod-rt2800-soc by default (if available)
[openwrt.git] / target / linux / ramips / patches-3.10 / 0004-watchdog-MIPS-add-ralink-watchdog-driver.patch
1 From 78046b68c1fc757162e32c83f59c3a94e794bf2e Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Sun, 28 Jul 2013 13:51:58 +0200
4 Subject: [PATCH 04/25] watchdog: MIPS: add ralink watchdog driver
5
6 Add a driver for the watchdog timer found on Ralink SoC
7
8 Signed-off-by: John Crispin <blogic@openwrt.org>
9 Cc: linux-watchdog@vger.kernel.org
10 Cc: linux-mips@linux-mips.org
11 ---
12  drivers/watchdog/Kconfig      |    7 ++
13  drivers/watchdog/Makefile     |    1 +
14  drivers/watchdog/rt2880_wdt.c |  208 +++++++++++++++++++++++++++++++++++++++++
15  3 files changed, 216 insertions(+)
16  create mode 100644 drivers/watchdog/rt2880_wdt.c
17
18 --- a/drivers/watchdog/Kconfig
19 +++ b/drivers/watchdog/Kconfig
20 @@ -1113,6 +1113,13 @@ config LANTIQ_WDT
21         help
22           Hardware driver for the Lantiq SoC Watchdog Timer.
23  
24 +config RALINK_WDT
25 +       tristate "Ralink SoC watchdog"
26 +       select WATCHDOG_CORE
27 +       depends on RALINK
28 +       help
29 +         Hardware driver for the Ralink SoC Watchdog Timer.
30 +
31  # PARISC Architecture
32  
33  # POWERPC Architecture
34 --- a/drivers/watchdog/Makefile
35 +++ b/drivers/watchdog/Makefile
36 @@ -135,6 +135,7 @@ obj-$(CONFIG_TXX9_WDT) += txx9wdt.o
37  obj-$(CONFIG_OCTEON_WDT) += octeon-wdt.o
38  octeon-wdt-y := octeon-wdt-main.o octeon-wdt-nmi.o
39  obj-$(CONFIG_LANTIQ_WDT) += lantiq_wdt.o
40 +obj-$(CONFIG_RALINK_WDT) += rt2880_wdt.o
41  
42  # PARISC Architecture
43  
44 --- /dev/null
45 +++ b/drivers/watchdog/rt2880_wdt.c
46 @@ -0,0 +1,208 @@
47 +/*
48 + * Ralink RT288x/RT3xxx/MT76xx built-in hardware watchdog timer
49 + *
50 + * Copyright (C) 2011 Gabor Juhos <juhosg@openwrt.org>
51 + * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
52 + *
53 + * This driver was based on: drivers/watchdog/softdog.c
54 + *
55 + * This program is free software; you can redistribute it and/or modify it
56 + * under the terms of the GNU General Public License version 2 as published
57 + * by the Free Software Foundation.
58 + */
59 +
60 +#include <linux/clk.h>
61 +#include <linux/reset.h>
62 +#include <linux/module.h>
63 +#include <linux/kernel.h>
64 +#include <linux/watchdog.h>
65 +#include <linux/miscdevice.h>
66 +#include <linux/moduleparam.h>
67 +#include <linux/platform_device.h>
68 +
69 +#include <asm/mach-ralink/ralink_regs.h>
70 +
71 +#define SYSC_RSTSTAT                   0x38
72 +#define WDT_RST_CAUSE                  BIT(1)
73 +
74 +#define RALINK_WDT_TIMEOUT             30
75 +#define RALINK_WDT_PRESCALE            65536
76 +
77 +#define TIMER_REG_TMR1LOAD             0x00
78 +#define TIMER_REG_TMR1CTL              0x08
79 +
80 +#define TMRSTAT_TMR1RST                        BIT(5)
81 +
82 +#define TMR1CTL_ENABLE                 BIT(7)
83 +#define TMR1CTL_MODE_SHIFT             4
84 +#define TMR1CTL_MODE_MASK              0x3
85 +#define TMR1CTL_MODE_FREE_RUNNING      0x0
86 +#define TMR1CTL_MODE_PERIODIC          0x1
87 +#define TMR1CTL_MODE_TIMEOUT           0x2
88 +#define TMR1CTL_MODE_WDT               0x3
89 +#define TMR1CTL_PRESCALE_MASK          0xf
90 +#define TMR1CTL_PRESCALE_65536         0xf
91 +
92 +static struct clk *rt288x_wdt_clk;
93 +static unsigned long rt288x_wdt_freq;
94 +static void __iomem *rt288x_wdt_base;
95 +
96 +static bool nowayout = WATCHDOG_NOWAYOUT;
97 +module_param(nowayout, bool, 0);
98 +MODULE_PARM_DESC(nowayout,
99 +               "Watchdog cannot be stopped once started (default="
100 +               __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
101 +
102 +static inline void rt_wdt_w32(unsigned reg, u32 val)
103 +{
104 +       iowrite32(val, rt288x_wdt_base + reg);
105 +}
106 +
107 +static inline u32 rt_wdt_r32(unsigned reg)
108 +{
109 +       return ioread32(rt288x_wdt_base + reg);
110 +}
111 +
112 +static int rt288x_wdt_ping(struct watchdog_device *w)
113 +{
114 +       rt_wdt_w32(TIMER_REG_TMR1LOAD, w->timeout * rt288x_wdt_freq);
115 +
116 +       return 0;
117 +}
118 +
119 +static int rt288x_wdt_start(struct watchdog_device *w)
120 +{
121 +       u32 t;
122 +
123 +       t = rt_wdt_r32(TIMER_REG_TMR1CTL);
124 +       t &= ~(TMR1CTL_MODE_MASK << TMR1CTL_MODE_SHIFT |
125 +               TMR1CTL_PRESCALE_MASK);
126 +       t |= (TMR1CTL_MODE_WDT << TMR1CTL_MODE_SHIFT |
127 +               TMR1CTL_PRESCALE_65536);
128 +       rt_wdt_w32(TIMER_REG_TMR1CTL, t);
129 +
130 +       rt288x_wdt_ping(w);
131 +
132 +       t = rt_wdt_r32(TIMER_REG_TMR1CTL);
133 +       t |= TMR1CTL_ENABLE;
134 +       rt_wdt_w32(TIMER_REG_TMR1CTL, t);
135 +
136 +       return 0;
137 +}
138 +
139 +static int rt288x_wdt_stop(struct watchdog_device *w)
140 +{
141 +       u32 t;
142 +
143 +       rt288x_wdt_ping(w);
144 +
145 +       t = rt_wdt_r32(TIMER_REG_TMR1CTL);
146 +       t &= ~TMR1CTL_ENABLE;
147 +       rt_wdt_w32(TIMER_REG_TMR1CTL, t);
148 +
149 +       return 0;
150 +}
151 +
152 +static int rt288x_wdt_set_timeout(struct watchdog_device *w, unsigned int t)
153 +{
154 +       w->timeout = t;
155 +       rt288x_wdt_ping(w);
156 +
157 +       return 0;
158 +}
159 +
160 +static int rt288x_wdt_bootcause(void)
161 +{
162 +       if (rt_sysc_r32(SYSC_RSTSTAT) & WDT_RST_CAUSE)
163 +               return WDIOF_CARDRESET;
164 +
165 +       return 0;
166 +}
167 +
168 +static struct watchdog_info rt288x_wdt_info = {
169 +       .identity = "Ralink Watchdog",
170 +       .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
171 +};
172 +
173 +static struct watchdog_ops rt288x_wdt_ops = {
174 +       .owner = THIS_MODULE,
175 +       .start = rt288x_wdt_start,
176 +       .stop = rt288x_wdt_stop,
177 +       .ping = rt288x_wdt_ping,
178 +       .set_timeout = rt288x_wdt_set_timeout,
179 +};
180 +
181 +static struct watchdog_device rt288x_wdt_dev = {
182 +       .info = &rt288x_wdt_info,
183 +       .ops = &rt288x_wdt_ops,
184 +       .min_timeout = 1,
185 +};
186 +
187 +static int rt288x_wdt_probe(struct platform_device *pdev)
188 +{
189 +       struct resource *res;
190 +       int ret;
191 +
192 +       res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
193 +       rt288x_wdt_base = devm_request_and_ioremap(&pdev->dev, res);
194 +       if (IS_ERR(rt288x_wdt_base))
195 +               return PTR_ERR(rt288x_wdt_base);
196 +
197 +       rt288x_wdt_clk = devm_clk_get(&pdev->dev, NULL);
198 +       if (IS_ERR(rt288x_wdt_clk))
199 +               return PTR_ERR(rt288x_wdt_clk);
200 +
201 +       device_reset(&pdev->dev);
202 +
203 +       rt288x_wdt_freq = clk_get_rate(rt288x_wdt_clk) / RALINK_WDT_PRESCALE;
204 +
205 +       rt288x_wdt_dev.dev = &pdev->dev;
206 +       rt288x_wdt_dev.bootstatus = rt288x_wdt_bootcause();
207 +
208 +       rt288x_wdt_dev.max_timeout = (0xfffful / rt288x_wdt_freq);
209 +       rt288x_wdt_dev.timeout = rt288x_wdt_dev.max_timeout;
210 +
211 +       watchdog_set_nowayout(&rt288x_wdt_dev, nowayout);
212 +
213 +       ret = watchdog_register_device(&rt288x_wdt_dev);
214 +       if (!ret)
215 +               dev_info(&pdev->dev, "Initialized\n");
216 +
217 +       return 0;
218 +}
219 +
220 +static int rt288x_wdt_remove(struct platform_device *pdev)
221 +{
222 +       watchdog_unregister_device(&rt288x_wdt_dev);
223 +
224 +       return 0;
225 +}
226 +
227 +static void rt288x_wdt_shutdown(struct platform_device *pdev)
228 +{
229 +       rt288x_wdt_stop(&rt288x_wdt_dev);
230 +}
231 +
232 +static const struct of_device_id rt288x_wdt_match[] = {
233 +       { .compatible = "ralink,rt2880-wdt" },
234 +       {},
235 +};
236 +MODULE_DEVICE_TABLE(of, rt288x_wdt_match);
237 +
238 +static struct platform_driver rt288x_wdt_driver = {
239 +       .probe          = rt288x_wdt_probe,
240 +       .remove         = rt288x_wdt_remove,
241 +       .shutdown       = rt288x_wdt_shutdown,
242 +       .driver         = {
243 +               .name           = KBUILD_MODNAME,
244 +               .owner          = THIS_MODULE,
245 +               .of_match_table = rt288x_wdt_match,
246 +       },
247 +};
248 +
249 +module_platform_driver(rt288x_wdt_driver);
250 +
251 +MODULE_DESCRIPTION("MediaTek/Ralink RT288x/RT3xxx hardware watchdog driver");
252 +MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org");
253 +MODULE_LICENSE("GPL v2");
254 +MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);