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/init.h>
23 #include <linux/interrupt.h>
24 #include <linux/uaccess.h>
25 #include <asm/blackfin.h>
27 #define stamp(fmt, args...) \
28 pr_debug("%s:%i: " fmt "\n", __func__, __LINE__, ## args)
29 #define stampit() stamp("here i am")
30 #define pr_devinit(fmt, args...) \
31 ({ static const __devinitconst char __fmt[] = fmt; \
32 printk(__fmt, ## args); })
33 #define pr_init(fmt, args...) \
34 ({ static const __initconst char __fmt[] = fmt; \
35 printk(__fmt, ## args); })
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 const 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
)
137 u32 cnt
, max_t
, sclk
;
143 stamp("maxtimeout=%us newtimeout=%lus (cnt=%#x)", max_t
, t
, cnt
);
146 printk(KERN_WARNING PFX
"timeout value is too large\n");
150 spin_lock_irqsave(&bfin_wdt_spinlock
, flags
);
152 int run
= bfin_wdt_running();
154 bfin_write_WDOG_CNT(cnt
);
158 spin_unlock_irqrestore(&bfin_wdt_spinlock
, flags
);
166 * bfin_wdt_open - Open the Device
167 * @inode: inode of device
168 * @file: file handle of device
170 * Watchdog device is opened and started.
172 static int bfin_wdt_open(struct inode
*inode
, struct file
*file
)
176 if (test_and_set_bit(0, &open_check
))
180 __module_get(THIS_MODULE
);
182 bfin_wdt_keepalive();
185 return nonseekable_open(inode
, file
);
189 * bfin_wdt_close - Close the Device
190 * @inode: inode of device
191 * @file: file handle of device
193 * Watchdog device is closed and stopped.
195 static int bfin_wdt_release(struct inode
*inode
, struct file
*file
)
199 if (expect_close
== 42)
203 "Unexpected close, not stopping watchdog!\n");
204 bfin_wdt_keepalive();
207 clear_bit(0, &open_check
);
212 * bfin_wdt_write - Write to Device
213 * @file: file handle of device
214 * @buf: buffer to write
215 * @count: length of buffer
218 * Pings the watchdog on write.
220 static ssize_t
bfin_wdt_write(struct file
*file
, const char __user
*data
,
221 size_t len
, loff_t
*ppos
)
229 /* In case it was set long ago */
232 for (i
= 0; i
!= len
; i
++) {
234 if (get_user(c
, data
+ i
))
240 bfin_wdt_keepalive();
247 * bfin_wdt_ioctl - Query Device
248 * @file: file handle of device
249 * @cmd: watchdog command
252 * Query basic information from the device or ping it, as outlined by the
255 static long bfin_wdt_ioctl(struct file
*file
,
256 unsigned int cmd
, unsigned long arg
)
258 void __user
*argp
= (void __user
*)arg
;
259 int __user
*p
= argp
;
264 case WDIOC_GETSUPPORT
:
265 if (copy_to_user(argp
, &bfin_wdt_info
, sizeof(bfin_wdt_info
)))
269 case WDIOC_GETSTATUS
:
270 case WDIOC_GETBOOTSTATUS
:
271 return put_user(!!(_bfin_swrst
& SWRST_RESET_WDOG
), p
);
272 case WDIOC_SETOPTIONS
: {
274 int options
, ret
= -EINVAL
;
276 if (get_user(options
, p
))
279 spin_lock_irqsave(&bfin_wdt_spinlock
, flags
);
280 if (options
& WDIOS_DISABLECARD
) {
284 if (options
& WDIOS_ENABLECARD
) {
288 spin_unlock_irqrestore(&bfin_wdt_spinlock
, flags
);
291 case WDIOC_KEEPALIVE
:
292 bfin_wdt_keepalive();
294 case WDIOC_SETTIMEOUT
: {
297 if (get_user(new_timeout
, p
))
299 if (bfin_wdt_set_timeout(new_timeout
))
303 case WDIOC_GETTIMEOUT
:
304 return put_user(timeout
, p
);
311 static int state_before_suspend
;
314 * bfin_wdt_suspend - suspend the watchdog
315 * @pdev: device being suspended
316 * @state: requested suspend state
318 * Remember if the watchdog was running and stop it.
319 * TODO: is this even right? Doesn't seem to be any
320 * standard in the watchdog world ...
322 static int bfin_wdt_suspend(struct platform_device
*pdev
, pm_message_t state
)
326 state_before_suspend
= bfin_wdt_running();
333 * bfin_wdt_resume - resume the watchdog
334 * @pdev: device being resumed
336 * If the watchdog was running, turn it back on.
338 static int bfin_wdt_resume(struct platform_device
*pdev
)
342 if (state_before_suspend
) {
343 bfin_wdt_set_timeout(timeout
);
350 # define bfin_wdt_suspend NULL
351 # define bfin_wdt_resume NULL
354 static const struct file_operations bfin_wdt_fops
= {
355 .owner
= THIS_MODULE
,
357 .write
= bfin_wdt_write
,
358 .unlocked_ioctl
= bfin_wdt_ioctl
,
359 .open
= bfin_wdt_open
,
360 .release
= bfin_wdt_release
,
363 static struct miscdevice bfin_wdt_miscdev
= {
364 .minor
= WATCHDOG_MINOR
,
366 .fops
= &bfin_wdt_fops
,
369 static const struct watchdog_info bfin_wdt_info
= {
370 .identity
= "Blackfin Watchdog",
371 .options
= WDIOF_SETTIMEOUT
|
372 WDIOF_KEEPALIVEPING
|
377 * bfin_wdt_probe - Initialize module
379 * Registers the misc device. Actual device
380 * initialization is handled by bfin_wdt_open().
382 static int __devinit
bfin_wdt_probe(struct platform_device
*pdev
)
386 ret
= misc_register(&bfin_wdt_miscdev
);
388 pr_devinit(KERN_ERR PFX
389 "cannot register miscdev on minor=%d (err=%d)\n",
390 WATCHDOG_MINOR
, ret
);
394 pr_devinit(KERN_INFO PFX
"initialized: timeout=%d sec (nowayout=%d)\n",
401 * bfin_wdt_remove - Initialize module
403 * Unregisters the misc device. Actual device
404 * deinitialization is handled by bfin_wdt_close().
406 static int __devexit
bfin_wdt_remove(struct platform_device
*pdev
)
408 misc_deregister(&bfin_wdt_miscdev
);
413 * bfin_wdt_shutdown - Soft Shutdown Handler
415 * Handles the soft shutdown event.
417 static void bfin_wdt_shutdown(struct platform_device
*pdev
)
424 static struct platform_device
*bfin_wdt_device
;
426 static struct platform_driver bfin_wdt_driver
= {
427 .probe
= bfin_wdt_probe
,
428 .remove
= __devexit_p(bfin_wdt_remove
),
429 .shutdown
= bfin_wdt_shutdown
,
430 .suspend
= bfin_wdt_suspend
,
431 .resume
= bfin_wdt_resume
,
433 .name
= WATCHDOG_NAME
,
434 .owner
= THIS_MODULE
,
439 * bfin_wdt_init - Initialize module
441 * Checks the module params and registers the platform device & driver.
442 * Real work is in the platform probe function.
444 static int __init
bfin_wdt_init(void)
450 /* Check that the timeout value is within range */
451 if (bfin_wdt_set_timeout(timeout
))
454 /* Since this is an on-chip device and needs no board-specific
455 * resources, we'll handle all the platform device stuff here.
457 ret
= platform_driver_register(&bfin_wdt_driver
);
459 pr_init(KERN_ERR PFX
"unable to register driver\n");
463 bfin_wdt_device
= platform_device_register_simple(WATCHDOG_NAME
,
465 if (IS_ERR(bfin_wdt_device
)) {
466 pr_init(KERN_ERR PFX
"unable to register device\n");
467 platform_driver_unregister(&bfin_wdt_driver
);
468 return PTR_ERR(bfin_wdt_device
);
475 * bfin_wdt_exit - Deinitialize module
477 * Back out the platform device & driver steps. Real work is in the
478 * platform remove function.
480 static void __exit
bfin_wdt_exit(void)
482 platform_device_unregister(bfin_wdt_device
);
483 platform_driver_unregister(&bfin_wdt_driver
);
486 module_init(bfin_wdt_init
);
487 module_exit(bfin_wdt_exit
);
489 MODULE_AUTHOR("Michele d'Amico, Mike Frysinger <vapier@gentoo.org>");
490 MODULE_DESCRIPTION("Blackfin Watchdog Device Driver");
491 MODULE_LICENSE("GPL");
492 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR
);
494 module_param(timeout
, uint
, 0);
495 MODULE_PARM_DESC(timeout
,
496 "Watchdog timeout in seconds. (1<=timeout<=((2^32)/SCLK), default="
497 __MODULE_STRING(WATCHDOG_TIMEOUT
) ")");
499 module_param(nowayout
, int, 0);
500 MODULE_PARM_DESC(nowayout
,
501 "Watchdog cannot be stopped once started (default="
502 __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");