package: fix insmod on install
[openwrt/openwrt.git] / target / linux / ramips / patches-3.8 / 0137-watchdog-adds-ralink-wdt.patch
1 From 14c1b064274d28cf88113a685c58374a515f3018 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 137/137] watchdog: adds ralink wdt
5
6 Adds the watchdog driver for ralink SoC.
7
8 Signed-off-by: John Crispin <blogic@openwrt.org>
9 ---
10 drivers/watchdog/Kconfig | 6 +
11 drivers/watchdog/Makefile | 1 +
12 drivers/watchdog/ralink_wdt.c | 352 +++++++++++++++++++++++++++++++++++++++++
13 3 files changed, 359 insertions(+)
14 create mode 100644 drivers/watchdog/ralink_wdt.c
15
16 --- a/drivers/watchdog/Kconfig
17 +++ b/drivers/watchdog/Kconfig
18 @@ -1077,6 +1077,12 @@ config LANTIQ_WDT
19 help
20 Hardware driver for the Lantiq SoC Watchdog Timer.
21
22 +config RALINK_WDT
23 + tristate "Ralink SoC watchdog"
24 + depends on RALINK
25 + help
26 + Hardware driver for the Ralink SoC Watchdog Timer.
27 +
28 # PARISC Architecture
29
30 # POWERPC Architecture
31 --- a/drivers/watchdog/Makefile
32 +++ b/drivers/watchdog/Makefile
33 @@ -132,6 +132,7 @@ obj-$(CONFIG_TXX9_WDT) += txx9wdt.o
34 obj-$(CONFIG_OCTEON_WDT) += octeon-wdt.o
35 octeon-wdt-y := octeon-wdt-main.o octeon-wdt-nmi.o
36 obj-$(CONFIG_LANTIQ_WDT) += lantiq_wdt.o
37 +obj-$(CONFIG_RALINK_WDT) += ralink_wdt.o
38
39 # PARISC Architecture
40
41 --- /dev/null
42 +++ b/drivers/watchdog/ralink_wdt.c
43 @@ -0,0 +1,352 @@
44 +/*
45 + * Ralink RT288X/RT305X built-in hardware watchdog timer
46 + *
47 + * Copyright (C) 2011 Gabor Juhos <juhosg@openwrt.org>
48 + *
49 + * This driver was based on: drivers/watchdog/ixp4xx_wdt.c
50 + * Author: Deepak Saxena <dsaxena@plexity.net>
51 + * Copyright 2004 (c) MontaVista, Software, Inc.
52 + *
53 + * which again was based on sa1100 driver,
54 + * Copyright (C) 2000 Oleg Drokin <green@crimea.edu>
55 + *
56 + * parts of the driver are based on Ralink's 2.6.21 BSP
57 + *
58 + * This program is free software; you can redistribute it and/or modify it
59 + * under the terms of the GNU General Public License version 2 as published
60 + * by the Free Software Foundation.
61 + */
62 +
63 +#include <linux/bitops.h>
64 +#include <linux/errno.h>
65 +#include <linux/fs.h>
66 +#include <linux/init.h>
67 +#include <linux/kernel.h>
68 +#include <linux/miscdevice.h>
69 +#include <linux/module.h>
70 +#include <linux/moduleparam.h>
71 +#include <linux/platform_device.h>
72 +#include <linux/types.h>
73 +#include <linux/watchdog.h>
74 +#include <linux/clk.h>
75 +#include <linux/err.h>
76 +
77 +#define DRIVER_NAME "ralink-wdt"
78 +
79 +#define RALINK_WDT_TIMEOUT 30 /* seconds */
80 +#define RALINK_WDT_PRESCALE 65536
81 +
82 +#define TIMER_REG_TMR1LOAD 0x00
83 +#define TIMER_REG_TMR1CTL 0x08
84 +
85 +#define TMRSTAT_TMR1RST BIT(5)
86 +
87 +#define TMR1CTL_ENABLE BIT(7)
88 +#define TMR1CTL_MODE_SHIFT 4
89 +#define TMR1CTL_MODE_MASK 0x3
90 +#define TMR1CTL_MODE_FREE_RUNNING 0x0
91 +#define TMR1CTL_MODE_PERIODIC 0x1
92 +#define TMR1CTL_MODE_TIMEOUT 0x2
93 +#define TMR1CTL_MODE_WDT 0x3
94 +#define TMR1CTL_PRESCALE_MASK 0xf
95 +#define TMR1CTL_PRESCALE_65536 0xf
96 +
97 +static int nowayout = WATCHDOG_NOWAYOUT;
98 +module_param(nowayout, int, 0);
99 +MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started "
100 + "(default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
101 +
102 +static int ralink_wdt_timeout = RALINK_WDT_TIMEOUT;
103 +module_param_named(timeout, ralink_wdt_timeout, int, 0);
104 +MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds, 0 means use maximum "
105 + "(default=" __MODULE_STRING(RALINK_WDT_TIMEOUT) "s)");
106 +
107 +static unsigned long ralink_wdt_flags;
108 +
109 +#define WDT_FLAGS_BUSY 0
110 +#define WDT_FLAGS_EXPECT_CLOSE 1
111 +
112 +static struct clk *ralink_wdt_clk;
113 +static unsigned long ralink_wdt_freq;
114 +static int ralink_wdt_max_timeout;
115 +static void __iomem *ralink_wdt_base;
116 +
117 +static inline void rt_wdt_w32(unsigned reg, u32 val)
118 +{
119 + __raw_writel(val, ralink_wdt_base + reg);
120 +}
121 +
122 +static inline u32 rt_wdt_r32(unsigned reg)
123 +{
124 + return __raw_readl(ralink_wdt_base + reg);
125 +}
126 +
127 +static inline void ralink_wdt_keepalive(void)
128 +{
129 + rt_wdt_w32(TIMER_REG_TMR1LOAD, ralink_wdt_timeout * ralink_wdt_freq);
130 +}
131 +
132 +static inline void ralink_wdt_enable(void)
133 +{
134 + u32 t;
135 +
136 + ralink_wdt_keepalive();
137 +
138 + t = rt_wdt_r32(TIMER_REG_TMR1CTL);
139 + t |= TMR1CTL_ENABLE;
140 + rt_wdt_w32(TIMER_REG_TMR1CTL, t);
141 +}
142 +
143 +static inline void ralink_wdt_disable(void)
144 +{
145 + u32 t;
146 +
147 + ralink_wdt_keepalive();
148 +
149 + t = rt_wdt_r32(TIMER_REG_TMR1CTL);
150 + t &= ~TMR1CTL_ENABLE;
151 + rt_wdt_w32(TIMER_REG_TMR1CTL, t);
152 +}
153 +
154 +static int ralink_wdt_set_timeout(int val)
155 +{
156 + if (val < 1 || val > ralink_wdt_max_timeout) {
157 + pr_warn(DRIVER_NAME
158 + ": timeout value %d must be 0 < timeout <= %d, using %d\n",
159 + val, ralink_wdt_max_timeout, ralink_wdt_timeout);
160 + return -EINVAL;
161 + }
162 +
163 + ralink_wdt_timeout = val;
164 + ralink_wdt_keepalive();
165 +
166 + return 0;
167 +}
168 +
169 +static int ralink_wdt_open(struct inode *inode, struct file *file)
170 +{
171 + u32 t;
172 +
173 + if (test_and_set_bit(WDT_FLAGS_BUSY, &ralink_wdt_flags))
174 + return -EBUSY;
175 +
176 + clear_bit(WDT_FLAGS_EXPECT_CLOSE, &ralink_wdt_flags);
177 +
178 + t = rt_wdt_r32(TIMER_REG_TMR1CTL);
179 + t &= ~(TMR1CTL_MODE_MASK << TMR1CTL_MODE_SHIFT |
180 + TMR1CTL_PRESCALE_MASK);
181 + t |= (TMR1CTL_MODE_WDT << TMR1CTL_MODE_SHIFT |
182 + TMR1CTL_PRESCALE_65536);
183 + rt_wdt_w32(TIMER_REG_TMR1CTL, t);
184 +
185 + ralink_wdt_enable();
186 +
187 + return nonseekable_open(inode, file);
188 +}
189 +
190 +static int ralink_wdt_release(struct inode *inode, struct file *file)
191 +{
192 + if (test_bit(WDT_FLAGS_EXPECT_CLOSE, &ralink_wdt_flags))
193 + ralink_wdt_disable();
194 + else {
195 + pr_crit(DRIVER_NAME ": device closed unexpectedly, "
196 + "watchdog timer will not stop!\n");
197 + ralink_wdt_keepalive();
198 + }
199 +
200 + clear_bit(WDT_FLAGS_BUSY, &ralink_wdt_flags);
201 + clear_bit(WDT_FLAGS_EXPECT_CLOSE, &ralink_wdt_flags);
202 +
203 + return 0;
204 +}
205 +
206 +static ssize_t rt_wdt_w32ite(struct file *file, const char *data,
207 + size_t len, loff_t *ppos)
208 +{
209 + if (len) {
210 + if (!nowayout) {
211 + size_t i;
212 +
213 + clear_bit(WDT_FLAGS_EXPECT_CLOSE, &ralink_wdt_flags);
214 +
215 + for (i = 0; i != len; i++) {
216 + char c;
217 +
218 + if (get_user(c, data + i))
219 + return -EFAULT;
220 +
221 + if (c == 'V')
222 + set_bit(WDT_FLAGS_EXPECT_CLOSE,
223 + &ralink_wdt_flags);
224 + }
225 + }
226 +
227 + ralink_wdt_keepalive();
228 + }
229 +
230 + return len;
231 +}
232 +
233 +static const struct watchdog_info ralink_wdt_info = {
234 + .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING |
235 + WDIOF_MAGICCLOSE,
236 + .firmware_version = 0,
237 + .identity = "RALINK watchdog",
238 +};
239 +
240 +static long ralink_wdt_ioctl(struct file *file, unsigned int cmd,
241 + unsigned long arg)
242 +{
243 + void __user *argp = (void __user *)arg;
244 + int __user *p = argp;
245 + int err;
246 + int t;
247 +
248 + switch (cmd) {
249 + case WDIOC_GETSUPPORT:
250 + err = copy_to_user(argp, &ralink_wdt_info,
251 + sizeof(ralink_wdt_info)) ? -EFAULT : 0;
252 + break;
253 +
254 + case WDIOC_GETSTATUS:
255 + err = put_user(0, p);
256 + break;
257 +
258 + case WDIOC_KEEPALIVE:
259 + ralink_wdt_keepalive();
260 + err = 0;
261 + break;
262 +
263 + case WDIOC_SETTIMEOUT:
264 + err = get_user(t, p);
265 + if (err)
266 + break;
267 +
268 + err = ralink_wdt_set_timeout(t);
269 + if (err)
270 + break;
271 +
272 + /* fallthrough */
273 + case WDIOC_GETTIMEOUT:
274 + err = put_user(ralink_wdt_timeout, p);
275 + break;
276 +
277 + default:
278 + err = -ENOTTY;
279 + break;
280 + }
281 +
282 + return err;
283 +}
284 +
285 +static const struct file_operations ralink_wdt_fops = {
286 + .owner = THIS_MODULE,
287 + .llseek = no_llseek,
288 + .write = rt_wdt_w32ite,
289 + .unlocked_ioctl = ralink_wdt_ioctl,
290 + .open = ralink_wdt_open,
291 + .release = ralink_wdt_release,
292 +};
293 +
294 +static struct miscdevice ralink_wdt_miscdev = {
295 + .minor = WATCHDOG_MINOR,
296 + .name = "watchdog",
297 + .fops = &ralink_wdt_fops,
298 +};
299 +
300 +static int ralink_wdt_probe(struct platform_device *pdev)
301 +{
302 + struct resource *res;
303 + int err;
304 +
305 + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
306 + if (!res) {
307 + dev_err(&pdev->dev, "no memory resource found\n");
308 + return -EINVAL;
309 + }
310 +
311 + ralink_wdt_base = ioremap(res->start, resource_size(res));
312 + if (!ralink_wdt_base)
313 + return -ENOMEM;
314 +
315 + ralink_wdt_clk = clk_get(&pdev->dev, NULL);
316 + if (IS_ERR(ralink_wdt_clk)) {
317 + err = PTR_ERR(ralink_wdt_clk);
318 + goto err_unmap;
319 + }
320 +
321 + err = clk_enable(ralink_wdt_clk);
322 + if (err)
323 + goto err_clk_put;
324 +
325 + ralink_wdt_freq = clk_get_rate(ralink_wdt_clk) / RALINK_WDT_PRESCALE;
326 + if (!ralink_wdt_freq) {
327 + err = -EINVAL;
328 + goto err_clk_disable;
329 + }
330 +
331 + ralink_wdt_max_timeout = (0xfffful / ralink_wdt_freq);
332 + if (ralink_wdt_timeout < 1 ||
333 + ralink_wdt_timeout > ralink_wdt_max_timeout) {
334 + ralink_wdt_timeout = ralink_wdt_max_timeout;
335 + dev_info(&pdev->dev,
336 + "timeout value must be 0 < timeout <= %d, using %d\n",
337 + ralink_wdt_max_timeout, ralink_wdt_timeout);
338 + }
339 +
340 + err = misc_register(&ralink_wdt_miscdev);
341 + if (err) {
342 + dev_err(&pdev->dev,
343 + "unable to register misc device, err=%d\n", err);
344 + goto err_clk_disable;
345 + }
346 +
347 + return 0;
348 +
349 +err_clk_disable:
350 + clk_disable(ralink_wdt_clk);
351 +err_clk_put:
352 + clk_put(ralink_wdt_clk);
353 +err_unmap:
354 + iounmap(ralink_wdt_base);
355 + return err;
356 +}
357 +
358 +static int ralink_wdt_remove(struct platform_device *pdev)
359 +{
360 + misc_deregister(&ralink_wdt_miscdev);
361 + clk_disable(ralink_wdt_clk);
362 + clk_put(ralink_wdt_clk);
363 + iounmap(ralink_wdt_base);
364 + return 0;
365 +}
366 +
367 +static void ralink_wdt_shutdown(struct platform_device *pdev)
368 +{
369 + ralink_wdt_disable();
370 +}
371 +
372 +static const struct of_device_id ralink_wdt_match[] = {
373 + { .compatible = "ralink,rt2880-wdt" },
374 + {},
375 +};
376 +MODULE_DEVICE_TABLE(of, ralink_wdt_match);
377 +
378 +static struct platform_driver ralink_wdt_driver = {
379 + .probe = ralink_wdt_probe,
380 + .remove = ralink_wdt_remove,
381 + .shutdown = ralink_wdt_shutdown,
382 + .driver = {
383 + .name = DRIVER_NAME,
384 + .owner = THIS_MODULE,
385 + .of_match_table = ralink_wdt_match,
386 + },
387 +};
388 +
389 +module_platform_driver(ralink_wdt_driver);
390 +
391 +MODULE_DESCRIPTION("MediaTek/Ralink RT288X/RT305X hardware watchdog driver");
392 +MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org");
393 +MODULE_LICENSE("GPL v2");
394 +MODULE_ALIAS("platform:" DRIVER_NAME);
395 +MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);