2 * Blackfin On-Chip Watchdog Driver
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>
9 * Enter bugs at http://blackfin.uclinux.org/
11 * Licensed under the GPL-2 or later.
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>
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>
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); })
39 #define WATCHDOG_NAME "bfin-wdt"
40 #define PFX WATCHDOG_NAME ": "
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.
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)
54 /* Bit in SWRST that indicates boot caused by watchdog */
55 #define SWRST_RESET_WDOG 0x4000
57 /* Bit in WDOG_CTL that indicates watchdog has expired (WDR0) */
58 #define WDOG_EXPIRED 0x8000
60 /* Masks for WDEV field in WDOG_CTL register */
61 #define ICTL_RESET 0x0
67 /* Masks for WDEN field in WDOG_CTL register */
68 #define WDEN_MASK 0x0FF0
69 #define WDEN_ENABLE 0x0000
70 #define WDEN_DISABLE 0x0AD0
73 #define WATCHDOG_TIMEOUT 20
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);
83 * bfin_wdt_keepalive - Keep the Userspace Watchdog Alive
85 * The Userspace watchdog got a KeepAlive: schedule the next timeout.
87 static int bfin_wdt_keepalive(void)
90 bfin_write_WDOG_STAT(0);
95 * bfin_wdt_stop - Stop the Watchdog
97 * Stops the on-chip watchdog.
99 static int bfin_wdt_stop(void)
102 bfin_write_WDOG_CTL(WDEN_DISABLE);
107 * bfin_wdt_start - Start the Watchdog
109 * Starts the on-chip watchdog. Automatically loads WDOG_CNT
110 * into WDOG_STAT for us.
112 static int bfin_wdt_start(void)
115 bfin_write_WDOG_CTL(WDEN_ENABLE | ICTL_RESET);
120 * bfin_wdt_running - Check Watchdog status
122 * See if the watchdog is running.
124 static int bfin_wdt_running(void)
127 return ((bfin_read_WDOG_CTL() & WDEN_MASK) != WDEN_DISABLE);
131 * bfin_wdt_set_timeout - Set the Userspace Watchdog timeout
132 * @t: new timeout value (in seconds)
134 * Translate the specified timeout in seconds into System Clock
135 * terms which is what the on-chip Watchdog requires.
137 static int bfin_wdt_set_timeout(unsigned long t)
139 u32 cnt, max_t, sclk;
145 stamp("maxtimeout=%us newtimeout=%lus (cnt=%#x)", max_t, t, cnt);
148 printk(KERN_WARNING PFX "timeout value is too large\n");
152 spin_lock_irqsave(&bfin_wdt_spinlock, flags);
154 int run = bfin_wdt_running();
156 bfin_write_WDOG_CNT(cnt);
160 spin_unlock_irqrestore(&bfin_wdt_spinlock, flags);
168 * bfin_wdt_open - Open the Device
169 * @inode: inode of device
170 * @file: file handle of device
172 * Watchdog device is opened and started.
174 static int bfin_wdt_open(struct inode *inode, struct file *file)
178 if (test_and_set_bit(0, &open_check))
182 __module_get(THIS_MODULE);
184 bfin_wdt_keepalive();
187 return nonseekable_open(inode, file);
191 * bfin_wdt_close - Close the Device
192 * @inode: inode of device
193 * @file: file handle of device
195 * Watchdog device is closed and stopped.
197 static int bfin_wdt_release(struct inode *inode, struct file *file)
201 if (expect_close == 42)
205 "Unexpected close, not stopping watchdog!\n");
206 bfin_wdt_keepalive();
209 clear_bit(0, &open_check);
214 * bfin_wdt_write - Write to Device
215 * @file: file handle of device
216 * @buf: buffer to write
217 * @count: length of buffer
220 * Pings the watchdog on write.
222 static ssize_t bfin_wdt_write(struct file *file, const char __user *data,
223 size_t len, loff_t *ppos)
231 /* In case it was set long ago */
234 for (i = 0; i != len; i++) {
236 if (get_user(c, data + i))
242 bfin_wdt_keepalive();
249 * bfin_wdt_ioctl - Query Device
250 * @file: file handle of device
251 * @cmd: watchdog command
254 * Query basic information from the device or ping it, as outlined by the
257 static long bfin_wdt_ioctl(struct file *file,
258 unsigned int cmd, unsigned long arg)
260 void __user *argp = (void __user *)arg;
261 int __user *p = argp;
266 case WDIOC_GETSUPPORT:
267 if (copy_to_user(argp, &bfin_wdt_info, sizeof(bfin_wdt_info)))
271 case WDIOC_GETSTATUS:
272 case WDIOC_GETBOOTSTATUS:
273 return put_user(!!(_bfin_swrst & SWRST_RESET_WDOG), p);
274 case WDIOC_SETOPTIONS: {
276 int options, ret = -EINVAL;
278 if (get_user(options, p))
281 spin_lock_irqsave(&bfin_wdt_spinlock, flags);
282 if (options & WDIOS_DISABLECARD) {
286 if (options & WDIOS_ENABLECARD) {
290 spin_unlock_irqrestore(&bfin_wdt_spinlock, flags);
293 case WDIOC_KEEPALIVE:
294 bfin_wdt_keepalive();
296 case WDIOC_SETTIMEOUT: {
299 if (get_user(new_timeout, p))
301 if (bfin_wdt_set_timeout(new_timeout))
305 case WDIOC_GETTIMEOUT:
306 return put_user(timeout, p);
313 * bfin_wdt_notify_sys - Notifier Handler
314 * @this: notifier block
315 * @code: notifier event
318 * Handles specific events, such as turning off the watchdog during a
321 static int bfin_wdt_notify_sys(struct notifier_block *this,
322 unsigned long code, void *unused)
326 if (code == SYS_DOWN || code == SYS_HALT)
333 static int state_before_suspend;
336 * bfin_wdt_suspend - suspend the watchdog
337 * @pdev: device being suspended
338 * @state: requested suspend state
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 ...
344 static int bfin_wdt_suspend(struct platform_device *pdev, pm_message_t state)
348 state_before_suspend = bfin_wdt_running();
355 * bfin_wdt_resume - resume the watchdog
356 * @pdev: device being resumed
358 * If the watchdog was running, turn it back on.
360 static int bfin_wdt_resume(struct platform_device *pdev)
364 if (state_before_suspend) {
365 bfin_wdt_set_timeout(timeout);
372 # define bfin_wdt_suspend NULL
373 # define bfin_wdt_resume NULL
376 static const struct file_operations bfin_wdt_fops = {
377 .owner = THIS_MODULE,
379 .write = bfin_wdt_write,
380 .unlocked_ioctl = bfin_wdt_ioctl,
381 .open = bfin_wdt_open,
382 .release = bfin_wdt_release,
385 static struct miscdevice bfin_wdt_miscdev = {
386 .minor = WATCHDOG_MINOR,
388 .fops = &bfin_wdt_fops,
391 static struct watchdog_info bfin_wdt_info = {
392 .identity = "Blackfin Watchdog",
393 .options = WDIOF_SETTIMEOUT |
394 WDIOF_KEEPALIVEPING |
398 static struct notifier_block bfin_wdt_notifier = {
399 .notifier_call = bfin_wdt_notify_sys,
403 * bfin_wdt_probe - Initialize module
405 * Registers the misc device and notifier handler. Actual device
406 * initialization is handled by bfin_wdt_open().
408 static int __devinit bfin_wdt_probe(struct platform_device *pdev)
412 ret = register_reboot_notifier(&bfin_wdt_notifier);
414 pr_devinit(KERN_ERR PFX
415 "cannot register reboot notifier (err=%d)\n", ret);
419 ret = misc_register(&bfin_wdt_miscdev);
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);
428 pr_devinit(KERN_INFO PFX "initialized: timeout=%d sec (nowayout=%d)\n",
435 * bfin_wdt_remove - Initialize module
437 * Unregisters the misc device and notifier handler. Actual device
438 * deinitialization is handled by bfin_wdt_close().
440 static int __devexit bfin_wdt_remove(struct platform_device *pdev)
442 misc_deregister(&bfin_wdt_miscdev);
443 unregister_reboot_notifier(&bfin_wdt_notifier);
447 static struct platform_device *bfin_wdt_device;
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,
455 .name = WATCHDOG_NAME,
456 .owner = THIS_MODULE,
461 * bfin_wdt_init - Initialize module
463 * Checks the module params and registers the platform device & driver.
464 * Real work is in the platform probe function.
466 static int __init bfin_wdt_init(void)
472 /* Check that the timeout value is within range */
473 if (bfin_wdt_set_timeout(timeout))
476 /* Since this is an on-chip device and needs no board-specific
477 * resources, we'll handle all the platform device stuff here.
479 ret = platform_driver_register(&bfin_wdt_driver);
481 pr_init(KERN_ERR PFX "unable to register driver\n");
485 bfin_wdt_device = platform_device_register_simple(WATCHDOG_NAME,
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);
497 * bfin_wdt_exit - Deinitialize module
499 * Back out the platform device & driver steps. Real work is in the
500 * platform remove function.
502 static void __exit bfin_wdt_exit(void)
504 platform_device_unregister(bfin_wdt_device);
505 platform_driver_unregister(&bfin_wdt_driver);
508 module_init(bfin_wdt_init);
509 module_exit(bfin_wdt_exit);
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);
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) ")");
521 module_param(nowayout, int, 0);
522 MODULE_PARM_DESC(nowayout,
523 "Watchdog cannot be stopped once started (default="
524 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");