[WATCHDOG] Some more general cleanup
[linux-2.6.git] / drivers / watchdog / mpcore_wdt.c
1 /*
2  *      Watchdog driver for the mpcore watchdog timer
3  *
4  *      (c) Copyright 2004 ARM Limited
5  *
6  *      Based on the SoftDog driver:
7  *      (c) Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>,
8  *                                              All Rights Reserved.
9  *
10  *      This program is free software; you can redistribute it and/or
11  *      modify it under the terms of the GNU General Public License
12  *      as published by the Free Software Foundation; either version
13  *      2 of the License, or (at your option) any later version.
14  *
15  *      Neither Alan Cox nor CymruNet Ltd. admit liability nor provide
16  *      warranty for any of this software. This material is provided
17  *      "AS-IS" and at no charge.
18  *
19  *      (c) Copyright 1995    Alan Cox <alan@lxorguk.ukuu.org.uk>
20  *
21  */
22 #include <linux/module.h>
23 #include <linux/moduleparam.h>
24 #include <linux/types.h>
25 #include <linux/miscdevice.h>
26 #include <linux/watchdog.h>
27 #include <linux/fs.h>
28 #include <linux/reboot.h>
29 #include <linux/init.h>
30 #include <linux/interrupt.h>
31 #include <linux/platform_device.h>
32 #include <linux/uaccess.h>
33
34 #include <asm/hardware/arm_twd.h>
35
36 struct mpcore_wdt {
37         unsigned long   timer_alive;
38         struct device   *dev;
39         void __iomem    *base;
40         int             irq;
41         unsigned int    perturb;
42         char            expect_close;
43 };
44
45 static struct platform_device *mpcore_wdt_dev;
46 extern unsigned int mpcore_timer_rate;
47
48 #define TIMER_MARGIN    60
49 static int mpcore_margin = TIMER_MARGIN;
50 module_param(mpcore_margin, int, 0);
51 MODULE_PARM_DESC(mpcore_margin,
52         "MPcore timer margin in seconds. (0 < mpcore_margin < 65536, default="
53                                 __MODULE_STRING(TIMER_MARGIN) ")");
54
55 static int nowayout = WATCHDOG_NOWAYOUT;
56 module_param(nowayout, int, 0);
57 MODULE_PARM_DESC(nowayout,
58         "Watchdog cannot be stopped once started (default="
59                                 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
60
61 #define ONLY_TESTING    0
62 static int mpcore_noboot = ONLY_TESTING;
63 module_param(mpcore_noboot, int, 0);
64 MODULE_PARM_DESC(mpcore_noboot, "MPcore watchdog action, "
65         "set to 1 to ignore reboots, 0 to reboot (default="
66                                         __MODULE_STRING(ONLY_TESTING) ")");
67
68 /*
69  *      This is the interrupt handler.  Note that we only use this
70  *      in testing mode, so don't actually do a reboot here.
71  */
72 static irqreturn_t mpcore_wdt_fire(int irq, void *arg)
73 {
74         struct mpcore_wdt *wdt = arg;
75
76         /* Check it really was our interrupt */
77         if (readl(wdt->base + TWD_WDOG_INTSTAT)) {
78                 dev_printk(KERN_CRIT, wdt->dev,
79                                         "Triggered - Reboot ignored.\n");
80                 /* Clear the interrupt on the watchdog */
81                 writel(1, wdt->base + TWD_WDOG_INTSTAT);
82                 return IRQ_HANDLED;
83         }
84         return IRQ_NONE;
85 }
86
87 /*
88  *      mpcore_wdt_keepalive - reload the timer
89  *
90  *      Note that the spec says a DIFFERENT value must be written to the reload
91  *      register each time.  The "perturb" variable deals with this by adding 1
92  *      to the count every other time the function is called.
93  */
94 static void mpcore_wdt_keepalive(struct mpcore_wdt *wdt)
95 {
96         unsigned int count;
97
98         /* Assume prescale is set to 256 */
99         count = (mpcore_timer_rate / 256) * mpcore_margin;
100
101         /* Reload the counter */
102         spin_lock(&wdt_lock);
103         writel(count + wdt->perturb, wdt->base + TWD_WDOG_LOAD);
104         wdt->perturb = wdt->perturb ? 0 : 1;
105         spin_unlock(&wdt_lock);
106 }
107
108 static void mpcore_wdt_stop(struct mpcore_wdt *wdt)
109 {
110         spin_lock(&wdt_lock);
111         writel(0x12345678, wdt->base + TWD_WDOG_DISABLE);
112         writel(0x87654321, wdt->base + TWD_WDOG_DISABLE);
113         writel(0x0, wdt->base + TWD_WDOG_CONTROL);
114         spin_unlock(&wdt_lock);
115 }
116
117 static void mpcore_wdt_start(struct mpcore_wdt *wdt)
118 {
119         dev_printk(KERN_INFO, wdt->dev, "enabling watchdog.\n");
120
121         spin_lock(&wdt_lock);
122         /* This loads the count register but does NOT start the count yet */
123         mpcore_wdt_keepalive(wdt);
124
125         if (mpcore_noboot) {
126                 /* Enable watchdog - prescale=256, watchdog mode=0, enable=1 */
127                 writel(0x0000FF01, wdt->base + TWD_WDOG_CONTROL);
128         } else {
129                 /* Enable watchdog - prescale=256, watchdog mode=1, enable=1 */
130                 writel(0x0000FF09, wdt->base + TWD_WDOG_CONTROL);
131         }
132         spin_unlock(&wdt_lock);
133 }
134
135 static int mpcore_wdt_set_heartbeat(int t)
136 {
137         if (t < 0x0001 || t > 0xFFFF)
138                 return -EINVAL;
139
140         mpcore_margin = t;
141         return 0;
142 }
143
144 /*
145  *      /dev/watchdog handling
146  */
147 static int mpcore_wdt_open(struct inode *inode, struct file *file)
148 {
149         struct mpcore_wdt *wdt = platform_get_drvdata(mpcore_wdt_dev);
150
151         if (test_and_set_bit(0, &wdt->timer_alive))
152                 return -EBUSY;
153
154         if (nowayout)
155                 __module_get(THIS_MODULE);
156
157         file->private_data = wdt;
158
159         /*
160          *      Activate timer
161          */
162         mpcore_wdt_start(wdt);
163
164         return nonseekable_open(inode, file);
165 }
166
167 static int mpcore_wdt_release(struct inode *inode, struct file *file)
168 {
169         struct mpcore_wdt *wdt = file->private_data;
170
171         /*
172          *      Shut off the timer.
173          *      Lock it in if it's a module and we set nowayout
174          */
175         if (wdt->expect_close == 42)
176                 mpcore_wdt_stop(wdt);
177         else {
178                 dev_printk(KERN_CRIT, wdt->dev,
179                                 "unexpected close, not stopping watchdog!\n");
180                 mpcore_wdt_keepalive(wdt);
181         }
182         clear_bit(0, &wdt->timer_alive);
183         wdt->expect_close = 0;
184         return 0;
185 }
186
187 static ssize_t mpcore_wdt_write(struct file *file, const char *data,
188                                                 size_t len, loff_t *ppos)
189 {
190         struct mpcore_wdt *wdt = file->private_data;
191
192         /*
193          *      Refresh the timer.
194          */
195         if (len) {
196                 if (!nowayout) {
197                         size_t i;
198
199                         /* In case it was set long ago */
200                         wdt->expect_close = 0;
201
202                         for (i = 0; i != len; i++) {
203                                 char c;
204
205                                 if (get_user(c, data + i))
206                                         return -EFAULT;
207                                 if (c == 'V')
208                                         wdt->expect_close = 42;
209                         }
210                 }
211                 mpcore_wdt_keepalive(wdt);
212         }
213         return len;
214 }
215
216 static struct watchdog_info ident = {
217         .options                = WDIOF_SETTIMEOUT |
218                                   WDIOF_KEEPALIVEPING |
219                                   WDIOF_MAGICCLOSE,
220         .identity               = "MPcore Watchdog",
221 };
222
223 static long mpcore_wdt_ioctl(struct file *file, unsigned int cmd,
224                                                         unsigned long arg)
225 {
226         struct mpcore_wdt *wdt = file->private_data;
227         int ret;
228         union {
229                 struct watchdog_info ident;
230                 int i;
231         } uarg;
232
233         if (_IOC_DIR(cmd) && _IOC_SIZE(cmd) > sizeof(uarg))
234                 return -ENOTTY;
235
236         if (_IOC_DIR(cmd) & _IOC_WRITE) {
237                 ret = copy_from_user(&uarg, (void __user *)arg, _IOC_SIZE(cmd));
238                 if (ret)
239                         return -EFAULT;
240         }
241
242         switch (cmd) {
243         case WDIOC_GETSUPPORT:
244                 uarg.ident = ident;
245                 ret = 0;
246                 break;
247
248         case WDIOC_GETSTATUS:
249         case WDIOC_GETBOOTSTATUS:
250                 uarg.i = 0;
251                 ret = 0;
252                 break;
253
254         case WDIOC_SETOPTIONS:
255                 ret = -EINVAL;
256                 if (uarg.i & WDIOS_DISABLECARD) {
257                         mpcore_wdt_stop(wdt);
258                         ret = 0;
259                 }
260                 if (uarg.i & WDIOS_ENABLECARD) {
261                         mpcore_wdt_start(wdt);
262                         ret = 0;
263                 }
264                 break;
265
266         case WDIOC_KEEPALIVE:
267                 mpcore_wdt_keepalive(wdt);
268                 ret = 0;
269                 break;
270
271         case WDIOC_SETTIMEOUT:
272                 ret = mpcore_wdt_set_heartbeat(uarg.i);
273                 if (ret)
274                         break;
275
276                 mpcore_wdt_keepalive(wdt);
277                 /* Fall */
278         case WDIOC_GETTIMEOUT:
279                 uarg.i = mpcore_margin;
280                 ret = 0;
281                 break;
282
283         default:
284                 return -ENOTTY;
285         }
286
287         if (ret == 0 && _IOC_DIR(cmd) & _IOC_READ) {
288                 ret = copy_to_user((void __user *)arg, &uarg, _IOC_SIZE(cmd));
289                 if (ret)
290                         ret = -EFAULT;
291         }
292         return ret;
293 }
294
295 /*
296  *      System shutdown handler.  Turn off the watchdog if we're
297  *      restarting or halting the system.
298  */
299 static void mpcore_wdt_shutdown(struct platform_device *dev)
300 {
301         struct mpcore_wdt *wdt = platform_get_drvdata(dev);
302
303         if (system_state == SYSTEM_RESTART || system_state == SYSTEM_HALT)
304                 mpcore_wdt_stop(wdt);
305 }
306
307 /*
308  *      Kernel Interfaces
309  */
310 static const struct file_operations mpcore_wdt_fops = {
311         .owner          = THIS_MODULE,
312         .llseek         = no_llseek,
313         .write          = mpcore_wdt_write,
314         .unlocked_ioctl = mpcore_wdt_ioctl,
315         .open           = mpcore_wdt_open,
316         .release        = mpcore_wdt_release,
317 };
318
319 static struct miscdevice mpcore_wdt_miscdev = {
320         .minor          = WATCHDOG_MINOR,
321         .name           = "watchdog",
322         .fops           = &mpcore_wdt_fops,
323 };
324
325 static int __devinit mpcore_wdt_probe(struct platform_device *dev)
326 {
327         struct mpcore_wdt *wdt;
328         struct resource *res;
329         int ret;
330
331         /* We only accept one device, and it must have an id of -1 */
332         if (dev->id != -1)
333                 return -ENODEV;
334
335         res = platform_get_resource(dev, IORESOURCE_MEM, 0);
336         if (!res) {
337                 ret = -ENODEV;
338                 goto err_out;
339         }
340
341         wdt = kzalloc(sizeof(struct mpcore_wdt), GFP_KERNEL);
342         if (!wdt) {
343                 ret = -ENOMEM;
344                 goto err_out;
345         }
346
347         wdt->dev = &dev->dev;
348         wdt->irq = platform_get_irq(dev, 0);
349         if (wdt->irq < 0) {
350                 ret = -ENXIO;
351                 goto err_free;
352         }
353         wdt->base = ioremap(res->start, res->end - res->start + 1);
354         if (!wdt->base) {
355                 ret = -ENOMEM;
356                 goto err_free;
357         }
358
359         mpcore_wdt_miscdev.parent = &dev->dev;
360         ret = misc_register(&mpcore_wdt_miscdev);
361         if (ret) {
362                 dev_printk(KERN_ERR, _dev,
363                         "cannot register miscdev on minor=%d (err=%d)\n",
364                                                         WATCHDOG_MINOR, ret);
365                 goto err_misc;
366         }
367
368         ret = request_irq(wdt->irq, mpcore_wdt_fire, IRQF_DISABLED,
369                                                         "mpcore_wdt", wdt);
370         if (ret) {
371                 dev_printk(KERN_ERR, _dev,
372                         "cannot register IRQ%d for watchdog\n", wdt->irq);
373                 goto err_irq;
374         }
375
376         mpcore_wdt_stop(wdt);
377         platform_set_drvdata(&dev->dev, wdt);
378         mpcore_wdt_dev = dev;
379
380         return 0;
381
382 err_irq:
383         misc_deregister(&mpcore_wdt_miscdev);
384 err_misc:
385         iounmap(wdt->base);
386 err_free:
387         kfree(wdt);
388 err_out:
389         return ret;
390 }
391
392 static int __devexit mpcore_wdt_remove(struct platform_device *dev)
393 {
394         struct mpcore_wdt *wdt = platform_get_drvdata(dev);
395
396         platform_set_drvdata(dev, NULL);
397
398         misc_deregister(&mpcore_wdt_miscdev);
399
400         mpcore_wdt_dev = NULL;
401
402         free_irq(wdt->irq, wdt);
403         iounmap(wdt->base);
404         kfree(wdt);
405         return 0;
406 }
407
408 /* work with hotplug and coldplug */
409 MODULE_ALIAS("platform:mpcore_wdt");
410
411 static struct platform_driver mpcore_wdt_driver = {
412         .probe          = mpcore_wdt_probe,
413         .remove         = __devexit_p(mpcore_wdt_remove),
414         .shutdown       = mpcore_wdt_shutdown,
415         .driver         = {
416                 .owner  = THIS_MODULE,
417                 .name   = "mpcore_wdt",
418         },
419 };
420
421 static char banner[] __initdata = KERN_INFO "MPcore Watchdog Timer: 0.1. "
422                 "mpcore_noboot=%d mpcore_margin=%d sec (nowayout= %d)\n";
423
424 static int __init mpcore_wdt_init(void)
425 {
426         /*
427          * Check that the margin value is within it's range;
428          * if not reset to the default
429          */
430         if (mpcore_wdt_set_heartbeat(mpcore_margin)) {
431                 mpcore_wdt_set_heartbeat(TIMER_MARGIN);
432                 printk(KERN_INFO "mpcore_margin value must be 0 < mpcore_margin < 65536, using %d\n",
433                         TIMER_MARGIN);
434         }
435
436         printk(banner, mpcore_noboot, mpcore_margin, nowayout);
437
438         return platform_driver_register(&mpcore_wdt_driver);
439 }
440
441 static void __exit mpcore_wdt_exit(void)
442 {
443         platform_driver_unregister(&mpcore_wdt_driver);
444 }
445
446 module_init(mpcore_wdt_init);
447 module_exit(mpcore_wdt_exit);
448
449 MODULE_AUTHOR("ARM Limited");
450 MODULE_DESCRIPTION("MPcore Watchdog Device Driver");
451 MODULE_LICENSE("GPL");
452 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);