[WATCHDOG] bfin: fix max timeout calculation
[safe/jmp/linux-2.6] / drivers / watchdog / bfin_wdt.c
1 /*
2  * Blackfin On-Chip Watchdog Driver
3  *
4  * Originally based on softdog.c
5  * Copyright 2006-2010 Analog Devices Inc.
6  * Copyright 2006-2007 Michele d'Amico
7  * Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>
8  *
9  * Enter bugs at http://blackfin.uclinux.org/
10  *
11  * Licensed under the GPL-2 or later.
12  */
13
14 #include <linux/platform_device.h>
15 #include <linux/module.h>
16 #include <linux/moduleparam.h>
17 #include <linux/types.h>
18 #include <linux/timer.h>
19 #include <linux/miscdevice.h>
20 #include <linux/watchdog.h>
21 #include <linux/fs.h>
22 #include <linux/notifier.h>
23 #include <linux/reboot.h>
24 #include <linux/init.h>
25 #include <linux/interrupt.h>
26 #include <linux/uaccess.h>
27 #include <asm/blackfin.h>
28
29 #define stamp(fmt, args...) \
30         pr_debug("%s:%i: " fmt "\n", __func__, __LINE__, ## args)
31 #define stampit() stamp("here i am")
32 #define pr_devinit(fmt, args...) \
33         ({ static const __devinitconst char __fmt[] = fmt; \
34         printk(__fmt, ## args); })
35 #define pr_init(fmt, args...) \
36         ({ static const __initconst char __fmt[] = fmt; \
37         printk(__fmt, ## args); })
38
39 #define WATCHDOG_NAME "bfin-wdt"
40 #define PFX WATCHDOG_NAME ": "
41
42 /* The BF561 has two watchdogs (one per core), but since Linux
43  * only runs on core A, we'll just work with that one.
44  */
45 #ifdef BF561_FAMILY
46 # define bfin_read_WDOG_CTL()    bfin_read_WDOGA_CTL()
47 # define bfin_read_WDOG_CNT()    bfin_read_WDOGA_CNT()
48 # define bfin_read_WDOG_STAT()   bfin_read_WDOGA_STAT()
49 # define bfin_write_WDOG_CTL(x)  bfin_write_WDOGA_CTL(x)
50 # define bfin_write_WDOG_CNT(x)  bfin_write_WDOGA_CNT(x)
51 # define bfin_write_WDOG_STAT(x) bfin_write_WDOGA_STAT(x)
52 #endif
53
54 /* Bit in SWRST that indicates boot caused by watchdog */
55 #define SWRST_RESET_WDOG 0x4000
56
57 /* Bit in WDOG_CTL that indicates watchdog has expired (WDR0) */
58 #define WDOG_EXPIRED 0x8000
59
60 /* Masks for WDEV field in WDOG_CTL register */
61 #define ICTL_RESET   0x0
62 #define ICTL_NMI     0x2
63 #define ICTL_GPI     0x4
64 #define ICTL_NONE    0x6
65 #define ICTL_MASK    0x6
66
67 /* Masks for WDEN field in WDOG_CTL register */
68 #define WDEN_MASK    0x0FF0
69 #define WDEN_ENABLE  0x0000
70 #define WDEN_DISABLE 0x0AD0
71
72 /* some defaults */
73 #define WATCHDOG_TIMEOUT 20
74
75 static unsigned int timeout = WATCHDOG_TIMEOUT;
76 static int nowayout = WATCHDOG_NOWAYOUT;
77 static struct watchdog_info bfin_wdt_info;
78 static unsigned long open_check;
79 static char expect_close;
80 static DEFINE_SPINLOCK(bfin_wdt_spinlock);
81
82 /**
83  *      bfin_wdt_keepalive - Keep the Userspace Watchdog Alive
84  *
85  *      The Userspace watchdog got a KeepAlive: schedule the next timeout.
86  */
87 static int bfin_wdt_keepalive(void)
88 {
89         stampit();
90         bfin_write_WDOG_STAT(0);
91         return 0;
92 }
93
94 /**
95  *      bfin_wdt_stop - Stop the Watchdog
96  *
97  *      Stops the on-chip watchdog.
98  */
99 static int bfin_wdt_stop(void)
100 {
101         stampit();
102         bfin_write_WDOG_CTL(WDEN_DISABLE);
103         return 0;
104 }
105
106 /**
107  *      bfin_wdt_start - Start the Watchdog
108  *
109  *      Starts the on-chip watchdog.  Automatically loads WDOG_CNT
110  *      into WDOG_STAT for us.
111  */
112 static int bfin_wdt_start(void)
113 {
114         stampit();
115         bfin_write_WDOG_CTL(WDEN_ENABLE | ICTL_RESET);
116         return 0;
117 }
118
119 /**
120  *      bfin_wdt_running - Check Watchdog status
121  *
122  *      See if the watchdog is running.
123  */
124 static int bfin_wdt_running(void)
125 {
126         stampit();
127         return ((bfin_read_WDOG_CTL() & WDEN_MASK) != WDEN_DISABLE);
128 }
129
130 /**
131  *      bfin_wdt_set_timeout - Set the Userspace Watchdog timeout
132  *      @t: new timeout value (in seconds)
133  *
134  *      Translate the specified timeout in seconds into System Clock
135  *      terms which is what the on-chip Watchdog requires.
136  */
137 static int bfin_wdt_set_timeout(unsigned long t)
138 {
139         u32 cnt, max_t, sclk;
140         unsigned long flags;
141
142         sclk = get_sclk();
143         max_t = -1 / sclk;
144         cnt = t * sclk;
145         stamp("maxtimeout=%us newtimeout=%lus (cnt=%#x)", max_t, t, cnt);
146
147         if (t > max_t) {
148                 printk(KERN_WARNING PFX "timeout value is too large\n");
149                 return -EINVAL;
150         }
151
152         spin_lock_irqsave(&bfin_wdt_spinlock, flags);
153         {
154                 int run = bfin_wdt_running();
155                 bfin_wdt_stop();
156                 bfin_write_WDOG_CNT(cnt);
157                 if (run)
158                         bfin_wdt_start();
159         }
160         spin_unlock_irqrestore(&bfin_wdt_spinlock, flags);
161
162         timeout = t;
163
164         return 0;
165 }
166
167 /**
168  *      bfin_wdt_open - Open the Device
169  *      @inode: inode of device
170  *      @file: file handle of device
171  *
172  *      Watchdog device is opened and started.
173  */
174 static int bfin_wdt_open(struct inode *inode, struct file *file)
175 {
176         stampit();
177
178         if (test_and_set_bit(0, &open_check))
179                 return -EBUSY;
180
181         if (nowayout)
182                 __module_get(THIS_MODULE);
183
184         bfin_wdt_keepalive();
185         bfin_wdt_start();
186
187         return nonseekable_open(inode, file);
188 }
189
190 /**
191  *      bfin_wdt_close - Close the Device
192  *      @inode: inode of device
193  *      @file: file handle of device
194  *
195  *      Watchdog device is closed and stopped.
196  */
197 static int bfin_wdt_release(struct inode *inode, struct file *file)
198 {
199         stampit();
200
201         if (expect_close == 42)
202                 bfin_wdt_stop();
203         else {
204                 printk(KERN_CRIT PFX
205                         "Unexpected close, not stopping watchdog!\n");
206                 bfin_wdt_keepalive();
207         }
208         expect_close = 0;
209         clear_bit(0, &open_check);
210         return 0;
211 }
212
213 /**
214  *      bfin_wdt_write - Write to Device
215  *      @file: file handle of device
216  *      @buf: buffer to write
217  *      @count: length of buffer
218  *      @ppos: offset
219  *
220  *      Pings the watchdog on write.
221  */
222 static ssize_t bfin_wdt_write(struct file *file, const char __user *data,
223                                                 size_t len, loff_t *ppos)
224 {
225         stampit();
226
227         if (len) {
228                 if (!nowayout) {
229                         size_t i;
230
231                         /* In case it was set long ago */
232                         expect_close = 0;
233
234                         for (i = 0; i != len; i++) {
235                                 char c;
236                                 if (get_user(c, data + i))
237                                         return -EFAULT;
238                                 if (c == 'V')
239                                         expect_close = 42;
240                         }
241                 }
242                 bfin_wdt_keepalive();
243         }
244
245         return len;
246 }
247
248 /**
249  *      bfin_wdt_ioctl - Query Device
250  *      @file: file handle of device
251  *      @cmd: watchdog command
252  *      @arg: argument
253  *
254  *      Query basic information from the device or ping it, as outlined by the
255  *      watchdog API.
256  */
257 static long bfin_wdt_ioctl(struct file *file,
258                                 unsigned int cmd, unsigned long arg)
259 {
260         void __user *argp = (void __user *)arg;
261         int __user *p = argp;
262
263         stampit();
264
265         switch (cmd) {
266         case WDIOC_GETSUPPORT:
267                 if (copy_to_user(argp, &bfin_wdt_info, sizeof(bfin_wdt_info)))
268                         return -EFAULT;
269                 else
270                         return 0;
271         case WDIOC_GETSTATUS:
272         case WDIOC_GETBOOTSTATUS:
273                 return put_user(!!(_bfin_swrst & SWRST_RESET_WDOG), p);
274         case WDIOC_SETOPTIONS: {
275                 unsigned long flags;
276                 int options, ret = -EINVAL;
277
278                 if (get_user(options, p))
279                         return -EFAULT;
280
281                 spin_lock_irqsave(&bfin_wdt_spinlock, flags);
282                 if (options & WDIOS_DISABLECARD) {
283                         bfin_wdt_stop();
284                         ret = 0;
285                 }
286                 if (options & WDIOS_ENABLECARD) {
287                         bfin_wdt_start();
288                         ret = 0;
289                 }
290                 spin_unlock_irqrestore(&bfin_wdt_spinlock, flags);
291                 return ret;
292         }
293         case WDIOC_KEEPALIVE:
294                 bfin_wdt_keepalive();
295                 return 0;
296         case WDIOC_SETTIMEOUT: {
297                 int new_timeout;
298
299                 if (get_user(new_timeout, p))
300                         return -EFAULT;
301                 if (bfin_wdt_set_timeout(new_timeout))
302                         return -EINVAL;
303         }
304         /* Fall */
305         case WDIOC_GETTIMEOUT:
306                 return put_user(timeout, p);
307         default:
308                 return -ENOTTY;
309         }
310 }
311
312 /**
313  *      bfin_wdt_notify_sys - Notifier Handler
314  *      @this: notifier block
315  *      @code: notifier event
316  *      @unused: unused
317  *
318  *      Handles specific events, such as turning off the watchdog during a
319  *      shutdown event.
320  */
321 static int bfin_wdt_notify_sys(struct notifier_block *this,
322                                         unsigned long code, void *unused)
323 {
324         stampit();
325
326         if (code == SYS_DOWN || code == SYS_HALT)
327                 bfin_wdt_stop();
328
329         return NOTIFY_DONE;
330 }
331
332 #ifdef CONFIG_PM
333 static int state_before_suspend;
334
335 /**
336  *      bfin_wdt_suspend - suspend the watchdog
337  *      @pdev: device being suspended
338  *      @state: requested suspend state
339  *
340  *      Remember if the watchdog was running and stop it.
341  *      TODO: is this even right?  Doesn't seem to be any
342  *            standard in the watchdog world ...
343  */
344 static int bfin_wdt_suspend(struct platform_device *pdev, pm_message_t state)
345 {
346         stampit();
347
348         state_before_suspend = bfin_wdt_running();
349         bfin_wdt_stop();
350
351         return 0;
352 }
353
354 /**
355  *      bfin_wdt_resume - resume the watchdog
356  *      @pdev: device being resumed
357  *
358  *      If the watchdog was running, turn it back on.
359  */
360 static int bfin_wdt_resume(struct platform_device *pdev)
361 {
362         stampit();
363
364         if (state_before_suspend) {
365                 bfin_wdt_set_timeout(timeout);
366                 bfin_wdt_start();
367         }
368
369         return 0;
370 }
371 #else
372 # define bfin_wdt_suspend NULL
373 # define bfin_wdt_resume NULL
374 #endif
375
376 static const struct file_operations bfin_wdt_fops = {
377         .owner          = THIS_MODULE,
378         .llseek         = no_llseek,
379         .write          = bfin_wdt_write,
380         .unlocked_ioctl = bfin_wdt_ioctl,
381         .open           = bfin_wdt_open,
382         .release        = bfin_wdt_release,
383 };
384
385 static struct miscdevice bfin_wdt_miscdev = {
386         .minor    = WATCHDOG_MINOR,
387         .name     = "watchdog",
388         .fops     = &bfin_wdt_fops,
389 };
390
391 static struct watchdog_info bfin_wdt_info = {
392         .identity = "Blackfin Watchdog",
393         .options  = WDIOF_SETTIMEOUT |
394                     WDIOF_KEEPALIVEPING |
395                     WDIOF_MAGICCLOSE,
396 };
397
398 static struct notifier_block bfin_wdt_notifier = {
399         .notifier_call = bfin_wdt_notify_sys,
400 };
401
402 /**
403  *      bfin_wdt_probe - Initialize module
404  *
405  *      Registers the misc device and notifier handler.  Actual device
406  *      initialization is handled by bfin_wdt_open().
407  */
408 static int __devinit bfin_wdt_probe(struct platform_device *pdev)
409 {
410         int ret;
411
412         ret = register_reboot_notifier(&bfin_wdt_notifier);
413         if (ret) {
414                 pr_devinit(KERN_ERR PFX
415                         "cannot register reboot notifier (err=%d)\n", ret);
416                 return ret;
417         }
418
419         ret = misc_register(&bfin_wdt_miscdev);
420         if (ret) {
421                 pr_devinit(KERN_ERR PFX
422                         "cannot register miscdev on minor=%d (err=%d)\n",
423                                 WATCHDOG_MINOR, ret);
424                 unregister_reboot_notifier(&bfin_wdt_notifier);
425                 return ret;
426         }
427
428         pr_devinit(KERN_INFO PFX "initialized: timeout=%d sec (nowayout=%d)\n",
429                timeout, nowayout);
430
431         return 0;
432 }
433
434 /**
435  *      bfin_wdt_remove - Initialize module
436  *
437  *      Unregisters the misc device and notifier handler.  Actual device
438  *      deinitialization is handled by bfin_wdt_close().
439  */
440 static int __devexit bfin_wdt_remove(struct platform_device *pdev)
441 {
442         misc_deregister(&bfin_wdt_miscdev);
443         unregister_reboot_notifier(&bfin_wdt_notifier);
444         return 0;
445 }
446
447 static struct platform_device *bfin_wdt_device;
448
449 static struct platform_driver bfin_wdt_driver = {
450         .probe     = bfin_wdt_probe,
451         .remove    = __devexit_p(bfin_wdt_remove),
452         .suspend   = bfin_wdt_suspend,
453         .resume    = bfin_wdt_resume,
454         .driver    = {
455                 .name  = WATCHDOG_NAME,
456                 .owner = THIS_MODULE,
457         },
458 };
459
460 /**
461  *      bfin_wdt_init - Initialize module
462  *
463  *      Checks the module params and registers the platform device & driver.
464  *      Real work is in the platform probe function.
465  */
466 static int __init bfin_wdt_init(void)
467 {
468         int ret;
469
470         stampit();
471
472         /* Check that the timeout value is within range */
473         if (bfin_wdt_set_timeout(timeout))
474                 return -EINVAL;
475
476         /* Since this is an on-chip device and needs no board-specific
477          * resources, we'll handle all the platform device stuff here.
478          */
479         ret = platform_driver_register(&bfin_wdt_driver);
480         if (ret) {
481                 pr_init(KERN_ERR PFX "unable to register driver\n");
482                 return ret;
483         }
484
485         bfin_wdt_device = platform_device_register_simple(WATCHDOG_NAME,
486                                                                 -1, NULL, 0);
487         if (IS_ERR(bfin_wdt_device)) {
488                 pr_init(KERN_ERR PFX "unable to register device\n");
489                 platform_driver_unregister(&bfin_wdt_driver);
490                 return PTR_ERR(bfin_wdt_device);
491         }
492
493         return 0;
494 }
495
496 /**
497  *      bfin_wdt_exit - Deinitialize module
498  *
499  *      Back out the platform device & driver steps.  Real work is in the
500  *      platform remove function.
501  */
502 static void __exit bfin_wdt_exit(void)
503 {
504         platform_device_unregister(bfin_wdt_device);
505         platform_driver_unregister(&bfin_wdt_driver);
506 }
507
508 module_init(bfin_wdt_init);
509 module_exit(bfin_wdt_exit);
510
511 MODULE_AUTHOR("Michele d'Amico, Mike Frysinger <vapier@gentoo.org>");
512 MODULE_DESCRIPTION("Blackfin Watchdog Device Driver");
513 MODULE_LICENSE("GPL");
514 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
515
516 module_param(timeout, uint, 0);
517 MODULE_PARM_DESC(timeout,
518         "Watchdog timeout in seconds. (1<=timeout<=((2^32)/SCLK), default="
519                 __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
520
521 module_param(nowayout, int, 0);
522 MODULE_PARM_DESC(nowayout,
523         "Watchdog cannot be stopped once started (default="
524                 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");