2 * Blackfin On-Chip Watchdog Driver
3 * Supports BF53[123]/BF53[467]/BF54[2489]/BF561
5 * Originally based on softdog.c
6 * Copyright 2006-2007 Analog Devices Inc.
7 * Copyright 2006-2007 Michele d'Amico
8 * Copyright 1996 Alan Cox <alan@redhat.com>
10 * Enter bugs at http://blackfin.uclinux.org/
12 * Licensed under the GPL-2 or later.
15 #include <linux/platform_device.h>
16 #include <linux/module.h>
17 #include <linux/moduleparam.h>
18 #include <linux/types.h>
19 #include <linux/timer.h>
20 #include <linux/miscdevice.h>
21 #include <linux/watchdog.h>
23 #include <linux/notifier.h>
24 #include <linux/reboot.h>
25 #include <linux/init.h>
26 #include <linux/interrupt.h>
27 #include <asm/blackfin.h>
28 #include <asm/uaccess.h>
30 #define stamp(fmt, args...) pr_debug("%s:%i: " fmt "\n", __func__, __LINE__, ## args)
31 #define stampit() stamp("here i am")
32 <<<<<<< HEAD
:drivers
/watchdog
/bfin_wdt
.c
34 #define pr_init(fmt, args...) ({ static const __initdata char __fmt[] = fmt; printk(__fmt, ## args); })
35 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/watchdog
/bfin_wdt
.c
37 #define WATCHDOG_NAME "bfin-wdt"
38 #define PFX WATCHDOG_NAME ": "
40 /* The BF561 has two watchdogs (one per core), but since Linux
41 * only runs on core A, we'll just work with that one.
44 # define bfin_read_WDOG_CTL() bfin_read_WDOGA_CTL()
45 # define bfin_read_WDOG_CNT() bfin_read_WDOGA_CNT()
46 # define bfin_read_WDOG_STAT() bfin_read_WDOGA_STAT()
47 # define bfin_write_WDOG_CTL(x) bfin_write_WDOGA_CTL(x)
48 # define bfin_write_WDOG_CNT(x) bfin_write_WDOGA_CNT(x)
49 # define bfin_write_WDOG_STAT(x) bfin_write_WDOGA_STAT(x)
52 /* Bit in SWRST that indicates boot caused by watchdog */
53 #define SWRST_RESET_WDOG 0x4000
55 /* Bit in WDOG_CTL that indicates watchdog has expired (WDR0) */
56 #define WDOG_EXPIRED 0x8000
58 /* Masks for WDEV field in WDOG_CTL register */
59 #define ICTL_RESET 0x0
65 /* Masks for WDEN field in WDOG_CTL register */
66 #define WDEN_MASK 0x0FF0
67 #define WDEN_ENABLE 0x0000
68 #define WDEN_DISABLE 0x0AD0
71 #define WATCHDOG_TIMEOUT 20
73 static unsigned int timeout
= WATCHDOG_TIMEOUT
;
74 static int nowayout
= WATCHDOG_NOWAYOUT
;
75 static struct watchdog_info bfin_wdt_info
;
76 static unsigned long open_check
;
77 static char expect_close
;
78 static DEFINE_SPINLOCK(bfin_wdt_spinlock
);
81 * bfin_wdt_keepalive - Keep the Userspace Watchdog Alive
83 * The Userspace watchdog got a KeepAlive: schedule the next timeout.
85 static int bfin_wdt_keepalive(void)
88 bfin_write_WDOG_STAT(0);
93 * bfin_wdt_stop - Stop the Watchdog
95 * Stops the on-chip watchdog.
97 static int bfin_wdt_stop(void)
100 bfin_write_WDOG_CTL(WDEN_DISABLE
);
105 * bfin_wdt_start - Start the Watchdog
107 * Starts the on-chip watchdog. Automatically loads WDOG_CNT
108 * into WDOG_STAT for us.
110 static int bfin_wdt_start(void)
113 bfin_write_WDOG_CTL(WDEN_ENABLE
| ICTL_RESET
);
118 * bfin_wdt_running - Check Watchdog status
120 * See if the watchdog is running.
122 static int bfin_wdt_running(void)
125 return ((bfin_read_WDOG_CTL() & WDEN_MASK
) != WDEN_DISABLE
);
129 * bfin_wdt_set_timeout - Set the Userspace Watchdog timeout
130 * @t: new timeout value (in seconds)
132 * Translate the specified timeout in seconds into System Clock
133 * terms which is what the on-chip Watchdog requires.
135 static int bfin_wdt_set_timeout(unsigned long t
)
142 cnt
= t
* get_sclk();
143 if (cnt
< get_sclk()) {
144 printk(KERN_WARNING PFX
"timeout value is too large\n");
148 spin_lock_irqsave(&bfin_wdt_spinlock
, flags
);
150 int run
= bfin_wdt_running();
152 bfin_write_WDOG_CNT(cnt
);
153 if (run
) bfin_wdt_start();
155 spin_unlock_irqrestore(&bfin_wdt_spinlock
, flags
);
163 * bfin_wdt_open - Open the Device
164 * @inode: inode of device
165 * @file: file handle of device
167 * Watchdog device is opened and started.
169 static int bfin_wdt_open(struct inode
*inode
, struct file
*file
)
173 if (test_and_set_bit(0, &open_check
))
177 __module_get(THIS_MODULE
);
179 bfin_wdt_keepalive();
182 return nonseekable_open(inode
, file
);
186 * bfin_wdt_close - Close the Device
187 * @inode: inode of device
188 * @file: file handle of device
190 * Watchdog device is closed and stopped.
192 static int bfin_wdt_release(struct inode
*inode
, struct file
*file
)
196 if (expect_close
== 42) {
199 printk(KERN_CRIT PFX
"Unexpected close, not stopping watchdog!\n");
200 bfin_wdt_keepalive();
204 clear_bit(0, &open_check
);
210 * bfin_wdt_write - Write to Device
211 * @file: file handle of device
212 * @buf: buffer to write
213 * @count: length of buffer
216 * Pings the watchdog on write.
218 static ssize_t
bfin_wdt_write(struct file
*file
, const char __user
*data
,
219 size_t len
, loff_t
*ppos
)
227 /* In case it was set long ago */
230 for (i
= 0; i
!= len
; i
++) {
232 if (get_user(c
, data
+ i
))
238 bfin_wdt_keepalive();
245 * bfin_wdt_ioctl - Query Device
246 * @inode: inode of device
247 * @file: file handle of device
248 * @cmd: watchdog command
251 * Query basic information from the device or ping it, as outlined by the
254 static int bfin_wdt_ioctl(struct inode
*inode
, struct file
*file
,
255 unsigned int cmd
, unsigned long arg
)
257 void __user
*argp
= (void __user
*)arg
;
258 int __user
*p
= argp
;
266 case WDIOC_GETSUPPORT
:
267 if (copy_to_user(argp
, &bfin_wdt_info
, sizeof(bfin_wdt_info
)))
272 case WDIOC_GETSTATUS
:
273 case WDIOC_GETBOOTSTATUS
:
274 return put_user(!!(_bfin_swrst
& SWRST_RESET_WDOG
), p
);
276 case WDIOC_KEEPALIVE
:
277 bfin_wdt_keepalive();
280 case WDIOC_SETTIMEOUT
: {
283 if (get_user(new_timeout
, p
))
286 if (bfin_wdt_set_timeout(new_timeout
))
290 case WDIOC_GETTIMEOUT
:
291 return put_user(timeout
, p
);
293 case WDIOC_SETOPTIONS
: {
295 int options
, ret
= -EINVAL
;
297 if (get_user(options
, p
))
300 spin_lock_irqsave(&bfin_wdt_spinlock
, flags
);
302 if (options
& WDIOS_DISABLECARD
) {
307 if (options
& WDIOS_ENABLECARD
) {
312 spin_unlock_irqrestore(&bfin_wdt_spinlock
, flags
);
320 * bfin_wdt_notify_sys - Notifier Handler
321 * @this: notifier block
322 * @code: notifier event
325 * Handles specific events, such as turning off the watchdog during a
328 static int bfin_wdt_notify_sys(struct notifier_block
*this, unsigned long code
,
333 if (code
== SYS_DOWN
|| code
== SYS_HALT
)
340 static int state_before_suspend
;
343 * bfin_wdt_suspend - suspend the watchdog
344 * @pdev: device being suspended
345 * @state: requested suspend state
347 * Remember if the watchdog was running and stop it.
348 * TODO: is this even right? Doesn't seem to be any
349 * standard in the watchdog world ...
351 static int bfin_wdt_suspend(struct platform_device
*pdev
, pm_message_t state
)
355 state_before_suspend
= bfin_wdt_running();
362 * bfin_wdt_resume - resume the watchdog
363 * @pdev: device being resumed
365 * If the watchdog was running, turn it back on.
367 static int bfin_wdt_resume(struct platform_device
*pdev
)
371 if (state_before_suspend
) {
372 bfin_wdt_set_timeout(timeout
);
379 # define bfin_wdt_suspend NULL
380 # define bfin_wdt_resume NULL
383 static struct platform_device bfin_wdt_device
= {
384 .name
= WATCHDOG_NAME
,
388 static struct platform_driver bfin_wdt_driver
= {
390 .name
= WATCHDOG_NAME
,
391 .owner
= THIS_MODULE
,
393 .suspend
= bfin_wdt_suspend
,
394 .resume
= bfin_wdt_resume
,
397 static const struct file_operations bfin_wdt_fops
= {
398 .owner
= THIS_MODULE
,
400 .write
= bfin_wdt_write
,
401 .ioctl
= bfin_wdt_ioctl
,
402 .open
= bfin_wdt_open
,
403 .release
= bfin_wdt_release
,
406 static struct miscdevice bfin_wdt_miscdev
= {
407 .minor
= WATCHDOG_MINOR
,
409 .fops
= &bfin_wdt_fops
,
412 static struct watchdog_info bfin_wdt_info
= {
413 .identity
= "Blackfin Watchdog",
414 .options
= WDIOF_SETTIMEOUT
|
415 WDIOF_KEEPALIVEPING
|
419 static struct notifier_block bfin_wdt_notifier
= {
420 .notifier_call
= bfin_wdt_notify_sys
,
424 * bfin_wdt_init - Initialize module
426 * Registers the device and notifier handler. Actual device
427 * initialization is handled by bfin_wdt_open().
429 static int __init
bfin_wdt_init(void)
435 /* Check that the timeout value is within range */
436 if (bfin_wdt_set_timeout(timeout
))
439 /* Since this is an on-chip device and needs no board-specific
440 * resources, we'll handle all the platform device stuff here.
442 ret
= platform_device_register(&bfin_wdt_device
);
446 ret
= platform_driver_probe(&bfin_wdt_driver
, NULL
);
450 ret
= register_reboot_notifier(&bfin_wdt_notifier
);
452 <<<<<<< HEAD
:drivers
/watchdog
/bfin_wdt
.c
453 printk(KERN_ERR PFX
"cannot register reboot notifier (err=%d)\n", ret
);
455 pr_init(KERN_ERR PFX
"cannot register reboot notifier (err=%d)\n", ret
);
456 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/watchdog
/bfin_wdt
.c
460 ret
= misc_register(&bfin_wdt_miscdev
);
462 <<<<<<< HEAD
:drivers
/watchdog
/bfin_wdt
.c
463 printk(KERN_ERR PFX
"cannot register miscdev on minor=%d (err=%d)\n",
465 pr_init(KERN_ERR PFX
"cannot register miscdev on minor=%d (err=%d)\n",
466 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/watchdog
/bfin_wdt
.c
467 WATCHDOG_MINOR
, ret
);
468 unregister_reboot_notifier(&bfin_wdt_notifier
);
472 <<<<<<< HEAD
:drivers
/watchdog
/bfin_wdt
.c
473 printk(KERN_INFO PFX
"initialized: timeout=%d sec (nowayout=%d)\n",
475 pr_init(KERN_INFO PFX
"initialized: timeout=%d sec (nowayout=%d)\n",
476 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/watchdog
/bfin_wdt
.c
483 * bfin_wdt_exit - Deinitialize module
485 * Unregisters the device and notifier handler. Actual device
486 * deinitialization is handled by bfin_wdt_close().
488 static void __exit
bfin_wdt_exit(void)
490 misc_deregister(&bfin_wdt_miscdev
);
491 unregister_reboot_notifier(&bfin_wdt_notifier
);
494 module_init(bfin_wdt_init
);
495 module_exit(bfin_wdt_exit
);
497 MODULE_AUTHOR("Michele d'Amico, Mike Frysinger <vapier@gentoo.org>");
498 MODULE_DESCRIPTION("Blackfin Watchdog Device Driver");
499 MODULE_LICENSE("GPL");
500 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR
);
502 module_param(timeout
, uint
, 0);
503 MODULE_PARM_DESC(timeout
, "Watchdog timeout in seconds. (1<=timeout<=((2^32)/SCLK), default=" __MODULE_STRING(WATCHDOG_TIMEOUT
) ")");
505 module_param(nowayout
, int, 0);
506 MODULE_PARM_DESC(nowayout
, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");