[brcm47xx] add watchdog driver (#5238)
[openwrt/svn-archive/archive.git] / target / linux / brcm47xx / patches-2.6.28 / 815-watchdog.patch
1 This add watchdog driver for broadcom 47xx device.
2 It uses the ssb subsytem to access embeded watchdog device.
3
4 Because the watchdog timeout is very short (about 2s), a soft timer is used
5 to increase the watchdog period.
6
7 Note : A patch for exporting the ssb_watchdog_timer_set will
8 be submitted on next linux-mips merge. Without this patch it can't
9 be build as a module.
10
11 Signed-off-by: Aleksandar Radovanovic <biblbroks@sezampro.rs>
12 Signed-off-by: Matthieu CASTET <castet.matthieu@free.fr>
13 Index: linux-2.6/drivers/watchdog/Kconfig
14 ===================================================================
15 --- linux-2.6.orig/drivers/watchdog/Kconfig 2009-05-25 22:22:02.000000000 +0200
16 +++ linux-2.6/drivers/watchdog/Kconfig 2009-06-05 22:05:19.000000000 +0200
17 @@ -764,6 +764,12 @@
18 help
19 Hardware driver for the built-in watchdog timer on TXx9 MIPS SoCs.
20
21 +config BCM47XX_WDT
22 + tristate "Broadcom BCM47xx Watchdog Timer"
23 + depends on BCM47XX
24 + help
25 + Hardware driver for the Broadcom BCM47xx Watchog Timer.
26 +
27 # PARISC Architecture
28
29 # POWERPC Architecture
30 Index: linux-2.6/drivers/watchdog/Makefile
31 ===================================================================
32 --- linux-2.6.orig/drivers/watchdog/Makefile 2009-05-25 22:22:02.000000000 +0200
33 +++ linux-2.6/drivers/watchdog/Makefile 2009-05-25 23:02:27.000000000 +0200
34 @@ -105,6 +105,7 @@
35 obj-$(CONFIG_SIBYTE_WDOG) += sb_wdog.o
36 obj-$(CONFIG_AR7_WDT) += ar7_wdt.o
37 obj-$(CONFIG_TXX9_WDT) += txx9wdt.o
38 +obj-$(CONFIG_BCM47XX_WDT) += bcm47xx_wdt.o
39
40 # PARISC Architecture
41
42 Index: linux-2.6/drivers/watchdog/bcm47xx_wdt.c
43 ===================================================================
44 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
45 +++ linux-2.6/drivers/watchdog/bcm47xx_wdt.c 2009-06-05 22:25:37.000000000 +0200
46 @@ -0,0 +1,286 @@
47 +/*
48 + * Watchdog driver for Broadcom BCM47XX
49 + *
50 + * Copyright (C) 2008 Aleksandar Radovanovic <biblbroks@sezampro.rs>
51 + * Copyright (C) 2009 Matthieu CASTET <castet.matthieu@free.fr>
52 + *
53 + * This program is free software; you can redistribute it and/or
54 + * modify it under the terms of the GNU General Public License
55 + * as published by the Free Software Foundation; either version
56 + * 2 of the License, or (at your option) any later version.
57 + */
58 +
59 +#include <linux/bitops.h>
60 +#include <linux/errno.h>
61 +#include <linux/fs.h>
62 +#include <linux/init.h>
63 +#include <linux/kernel.h>
64 +#include <linux/miscdevice.h>
65 +#include <linux/module.h>
66 +#include <linux/moduleparam.h>
67 +#include <linux/reboot.h>
68 +#include <linux/types.h>
69 +#include <linux/uaccess.h>
70 +#include <linux/watchdog.h>
71 +#include <linux/timer.h>
72 +#include <linux/jiffies.h>
73 +#include <linux/ssb/ssb_embedded.h>
74 +#include <asm/mach-bcm47xx/bcm47xx.h>
75 +
76 +#define DRV_NAME "bcm47xx_wdt"
77 +
78 +#define WDT_DEFAULT_TIME 30 /* seconds */
79 +#define WDT_MAX_TIME 256 /* seconds */
80 +
81 +static int wdt_time = WDT_DEFAULT_TIME;
82 +static int nowayout = WATCHDOG_NOWAYOUT;
83 +
84 +module_param(wdt_time, int, 0);
85 +MODULE_PARM_DESC(wdt_time, "Watchdog time in seconds. (default="
86 + __MODULE_STRING(WDT_DEFAULT_TIME) ")");
87 +
88 +#ifdef CONFIG_WATCHDOG_NOWAYOUT
89 +module_param(nowayout, int, 0);
90 +MODULE_PARM_DESC(nowayout,
91 + "Watchdog cannot be stopped once started (default="
92 + __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
93 +#endif
94 +
95 +static unsigned long bcm47xx_wdt_busy;
96 +static char expect_release;
97 +static struct timer_list wdt_timer;
98 +static atomic_t ticks;
99 +
100 +static inline void bcm47xx_wdt_hw_start(void)
101 +{
102 + /* this is 2,5s on 100Mhz clock and 2s on 133 Mhz */
103 + ssb_watchdog_timer_set(&ssb_bcm47xx, 0xfffffff);
104 +}
105 +
106 +static inline int bcm47xx_wdt_hw_stop(void)
107 +{
108 + return ssb_watchdog_timer_set(&ssb_bcm47xx, 0);
109 +}
110 +
111 +static void bcm47xx_timer_tick(unsigned long unused)
112 +{
113 + if (!atomic_dec_and_test(&ticks)) {
114 + bcm47xx_wdt_hw_start();
115 + mod_timer(&wdt_timer, jiffies + HZ);
116 + } else {
117 + printk(KERN_CRIT DRV_NAME "Watchdog will fire soon!!!\n");
118 + }
119 +}
120 +
121 +static inline void bcm47xx_wdt_pet(void)
122 +{
123 + atomic_set(&ticks, wdt_time);
124 +}
125 +
126 +static void bcm47xx_wdt_start(void)
127 +{
128 + bcm47xx_wdt_pet();
129 + bcm47xx_timer_tick(0);
130 +}
131 +
132 +static void bcm47xx_wdt_pause(void)
133 +{
134 + del_timer_sync(&wdt_timer);
135 + bcm47xx_wdt_hw_stop();
136 +}
137 +
138 +static void bcm47xx_wdt_stop(void)
139 +{
140 + bcm47xx_wdt_pause();
141 +}
142 +
143 +static int bcm47xx_wdt_settimeout(int new_time)
144 +{
145 + if ((new_time <= 0) || (new_time > WDT_MAX_TIME))
146 + return -EINVAL;
147 +
148 + wdt_time = new_time;
149 + return 0;
150 +}
151 +
152 +static int bcm47xx_wdt_open(struct inode *inode, struct file *file)
153 +{
154 + if (test_and_set_bit(0, &bcm47xx_wdt_busy))
155 + return -EBUSY;
156 +
157 + bcm47xx_wdt_start();
158 + return nonseekable_open(inode, file);
159 +}
160 +
161 +static int bcm47xx_wdt_release(struct inode *inode, struct file *file)
162 +{
163 + if (expect_release == 42) {
164 + bcm47xx_wdt_stop();
165 + } else {
166 + printk(KERN_CRIT DRV_NAME
167 + ": Unexpected close, not stopping watchdog!\n");
168 + bcm47xx_wdt_start();
169 + }
170 +
171 + clear_bit(0, &bcm47xx_wdt_busy);
172 + expect_release = 0;
173 + return 0;
174 +}
175 +
176 +static ssize_t bcm47xx_wdt_write(struct file *file, const char __user *data,
177 + size_t len, loff_t *ppos)
178 +{
179 + if (len) {
180 + if (!nowayout) {
181 + size_t i;
182 +
183 + expect_release = 0;
184 +
185 + for (i = 0; i != len; i++) {
186 + char c;
187 + if (get_user(c, data + i))
188 + return -EFAULT;
189 + if (c == 'V')
190 + expect_release = 42;
191 + }
192 + }
193 + bcm47xx_wdt_pet();
194 + }
195 + return len;
196 +}
197 +
198 +static struct watchdog_info bcm47xx_wdt_info = {
199 + .identity = DRV_NAME,
200 + .options = WDIOF_SETTIMEOUT |
201 + WDIOF_KEEPALIVEPING |
202 + WDIOF_MAGICCLOSE,
203 +};
204 +
205 +static long bcm47xx_wdt_ioctl(struct file *file,
206 + unsigned int cmd, unsigned long arg)
207 +{
208 + void __user *argp = (void __user *)arg;
209 + int __user *p = argp;
210 + int new_value, retval = -EINVAL;;
211 +
212 + switch (cmd) {
213 + case WDIOC_GETSUPPORT:
214 + return copy_to_user(argp, &bcm47xx_wdt_info,
215 + sizeof(bcm47xx_wdt_info)) ? -EFAULT : 0;
216 +
217 + case WDIOC_GETSTATUS:
218 + case WDIOC_GETBOOTSTATUS:
219 + return put_user(0, p);
220 +
221 + case WDIOC_SETOPTIONS:
222 + if (get_user(new_value, p))
223 + return -EFAULT;
224 +
225 + if (new_value & WDIOS_DISABLECARD) {
226 + bcm47xx_wdt_stop();
227 + retval = 0;
228 + }
229 +
230 + if (new_value & WDIOS_ENABLECARD) {
231 + bcm47xx_wdt_start();
232 + retval = 0;
233 + }
234 +
235 + return retval;
236 +
237 + case WDIOC_KEEPALIVE:
238 + bcm47xx_wdt_pet();
239 + return 0;
240 +
241 + case WDIOC_SETTIMEOUT:
242 + if (get_user(new_value, p))
243 + return -EFAULT;
244 +
245 + if (bcm47xx_wdt_settimeout(new_value))
246 + return -EINVAL;
247 +
248 + bcm47xx_wdt_pet();
249 +
250 + case WDIOC_GETTIMEOUT:
251 + return put_user(wdt_time, p);
252 +
253 + default:
254 + return -ENOTTY;
255 + }
256 +}
257 +
258 +static int bcm47xx_wdt_notify_sys(struct notifier_block *this,
259 + unsigned long code, void *unused)
260 +{
261 + if (code == SYS_DOWN || code == SYS_HALT)
262 + bcm47xx_wdt_stop();
263 + return NOTIFY_DONE;
264 +}
265 +
266 +static const struct file_operations bcm47xx_wdt_fops = {
267 + .owner = THIS_MODULE,
268 + .llseek = no_llseek,
269 + .unlocked_ioctl = bcm47xx_wdt_ioctl,
270 + .open = bcm47xx_wdt_open,
271 + .release = bcm47xx_wdt_release,
272 + .write = bcm47xx_wdt_write,
273 +};
274 +
275 +static struct miscdevice bcm47xx_wdt_miscdev = {
276 + .minor = WATCHDOG_MINOR,
277 + .name = "watchdog",
278 + .fops = &bcm47xx_wdt_fops,
279 +};
280 +
281 +static struct notifier_block bcm47xx_wdt_notifier = {
282 + .notifier_call = bcm47xx_wdt_notify_sys,
283 +};
284 +
285 +static int __init bcm47xx_wdt_init(void)
286 +{
287 + int ret;
288 +
289 + if (bcm47xx_wdt_hw_stop() < 0)
290 + return -ENODEV;
291 +
292 + setup_timer(&wdt_timer, bcm47xx_timer_tick, 0L);
293 +
294 + if (bcm47xx_wdt_settimeout(wdt_time)) {
295 + bcm47xx_wdt_settimeout(WDT_DEFAULT_TIME);
296 + printk(KERN_INFO DRV_NAME
297 + ": wdt_time value must be 1 <= wdt_time <= 256, using %d\n",
298 + wdt_time);
299 + }
300 +
301 + ret = register_reboot_notifier(&bcm47xx_wdt_notifier);
302 + if (ret)
303 + return ret;
304 +
305 + ret = misc_register(&bcm47xx_wdt_miscdev);
306 + if (ret) {
307 + unregister_reboot_notifier(&bcm47xx_wdt_notifier);
308 + return ret;
309 + }
310 +
311 + printk(KERN_INFO "BCM47xx Watchdog Timer enabled (%d seconds%s)\n",
312 + wdt_time, nowayout ? ", nowayout" : "");
313 + return 0;
314 +}
315 +
316 +static void __exit bcm47xx_wdt_exit(void)
317 +{
318 + if (!nowayout)
319 + bcm47xx_wdt_stop();
320 +
321 + misc_deregister(&bcm47xx_wdt_miscdev);
322 +
323 + unregister_reboot_notifier(&bcm47xx_wdt_notifier);
324 +}
325 +
326 +module_init(bcm47xx_wdt_init);
327 +module_exit(bcm47xx_wdt_exit);
328 +
329 +MODULE_AUTHOR("Aleksandar Radovanovic");
330 +MODULE_DESCRIPTION("Watchdog driver for Broadcom BCM47xx");
331 +MODULE_LICENSE("GPL");
332 +MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);