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>
26 #include <asm/bfin_watchdog.h>
28 #define stamp(fmt, args...) \
29 pr_debug("%s:%i: " fmt "\n", __func__, __LINE__, ## args)
30 #define stampit() stamp("here i am")
31 #define pr_devinit(fmt, args...) \
32 ({ static const __devinitconst char __fmt[] = fmt; \
33 printk(__fmt, ## args); })
34 #define pr_init(fmt, args...) \
35 ({ static const __initconst char __fmt[] = fmt; \
36 printk(__fmt, ## args); })
38 #define WATCHDOG_NAME "bfin-wdt"
39 #define PFX WATCHDOG_NAME ": "
41 /* The BF561 has two watchdogs (one per core), but since Linux
42 * only runs on core A, we'll just work with that one.
45 # define bfin_read_WDOG_CTL() bfin_read_WDOGA_CTL()
46 # define bfin_read_WDOG_CNT() bfin_read_WDOGA_CNT()
47 # define bfin_read_WDOG_STAT() bfin_read_WDOGA_STAT()
48 # define bfin_write_WDOG_CTL(x) bfin_write_WDOGA_CTL(x)
49 # define bfin_write_WDOG_CNT(x) bfin_write_WDOGA_CNT(x)
50 # define bfin_write_WDOG_STAT(x) bfin_write_WDOGA_STAT(x)
54 #define WATCHDOG_TIMEOUT 20
56 static unsigned int timeout
= WATCHDOG_TIMEOUT
;
57 static int nowayout
= WATCHDOG_NOWAYOUT
;
58 static const struct watchdog_info bfin_wdt_info
;
59 static unsigned long open_check
;
60 static char expect_close
;
61 static DEFINE_SPINLOCK(bfin_wdt_spinlock
);
64 * bfin_wdt_keepalive - Keep the Userspace Watchdog Alive
66 * The Userspace watchdog got a KeepAlive: schedule the next timeout.
68 static int bfin_wdt_keepalive(void)
71 bfin_write_WDOG_STAT(0);
76 * bfin_wdt_stop - Stop the Watchdog
78 * Stops the on-chip watchdog.
80 static int bfin_wdt_stop(void)
83 bfin_write_WDOG_CTL(WDEN_DISABLE
);
88 * bfin_wdt_start - Start the Watchdog
90 * Starts the on-chip watchdog. Automatically loads WDOG_CNT
91 * into WDOG_STAT for us.
93 static int bfin_wdt_start(void)
96 bfin_write_WDOG_CTL(WDEN_ENABLE
| ICTL_RESET
);
101 * bfin_wdt_running - Check Watchdog status
103 * See if the watchdog is running.
105 static int bfin_wdt_running(void)
108 return ((bfin_read_WDOG_CTL() & WDEN_MASK
) != WDEN_DISABLE
);
112 * bfin_wdt_set_timeout - Set the Userspace Watchdog timeout
113 * @t: new timeout value (in seconds)
115 * Translate the specified timeout in seconds into System Clock
116 * terms which is what the on-chip Watchdog requires.
118 static int bfin_wdt_set_timeout(unsigned long t
)
120 u32 cnt
, max_t
, sclk
;
126 stamp("maxtimeout=%us newtimeout=%lus (cnt=%#x)", max_t
, t
, cnt
);
129 printk(KERN_WARNING PFX
"timeout value is too large\n");
133 spin_lock_irqsave(&bfin_wdt_spinlock
, flags
);
135 int run
= bfin_wdt_running();
137 bfin_write_WDOG_CNT(cnt
);
141 spin_unlock_irqrestore(&bfin_wdt_spinlock
, flags
);
149 * bfin_wdt_open - Open the Device
150 * @inode: inode of device
151 * @file: file handle of device
153 * Watchdog device is opened and started.
155 static int bfin_wdt_open(struct inode
*inode
, struct file
*file
)
159 if (test_and_set_bit(0, &open_check
))
163 __module_get(THIS_MODULE
);
165 bfin_wdt_keepalive();
168 return nonseekable_open(inode
, file
);
172 * bfin_wdt_close - Close the Device
173 * @inode: inode of device
174 * @file: file handle of device
176 * Watchdog device is closed and stopped.
178 static int bfin_wdt_release(struct inode
*inode
, struct file
*file
)
182 if (expect_close
== 42)
186 "Unexpected close, not stopping watchdog!\n");
187 bfin_wdt_keepalive();
190 clear_bit(0, &open_check
);
195 * bfin_wdt_write - Write to Device
196 * @file: file handle of device
197 * @buf: buffer to write
198 * @count: length of buffer
201 * Pings the watchdog on write.
203 static ssize_t
bfin_wdt_write(struct file
*file
, const char __user
*data
,
204 size_t len
, loff_t
*ppos
)
212 /* In case it was set long ago */
215 for (i
= 0; i
!= len
; i
++) {
217 if (get_user(c
, data
+ i
))
223 bfin_wdt_keepalive();
230 * bfin_wdt_ioctl - Query Device
231 * @file: file handle of device
232 * @cmd: watchdog command
235 * Query basic information from the device or ping it, as outlined by the
238 static long bfin_wdt_ioctl(struct file
*file
,
239 unsigned int cmd
, unsigned long arg
)
241 void __user
*argp
= (void __user
*)arg
;
242 int __user
*p
= argp
;
247 case WDIOC_GETSUPPORT
:
248 if (copy_to_user(argp
, &bfin_wdt_info
, sizeof(bfin_wdt_info
)))
252 case WDIOC_GETSTATUS
:
253 case WDIOC_GETBOOTSTATUS
:
254 return put_user(!!(_bfin_swrst
& SWRST_RESET_WDOG
), p
);
255 case WDIOC_SETOPTIONS
: {
257 int options
, ret
= -EINVAL
;
259 if (get_user(options
, p
))
262 spin_lock_irqsave(&bfin_wdt_spinlock
, flags
);
263 if (options
& WDIOS_DISABLECARD
) {
267 if (options
& WDIOS_ENABLECARD
) {
271 spin_unlock_irqrestore(&bfin_wdt_spinlock
, flags
);
274 case WDIOC_KEEPALIVE
:
275 bfin_wdt_keepalive();
277 case WDIOC_SETTIMEOUT
: {
280 if (get_user(new_timeout
, p
))
282 if (bfin_wdt_set_timeout(new_timeout
))
286 case WDIOC_GETTIMEOUT
:
287 return put_user(timeout
, p
);
294 static int state_before_suspend
;
297 * bfin_wdt_suspend - suspend the watchdog
298 * @pdev: device being suspended
299 * @state: requested suspend state
301 * Remember if the watchdog was running and stop it.
302 * TODO: is this even right? Doesn't seem to be any
303 * standard in the watchdog world ...
305 static int bfin_wdt_suspend(struct platform_device
*pdev
, pm_message_t state
)
309 state_before_suspend
= bfin_wdt_running();
316 * bfin_wdt_resume - resume the watchdog
317 * @pdev: device being resumed
319 * If the watchdog was running, turn it back on.
321 static int bfin_wdt_resume(struct platform_device
*pdev
)
325 if (state_before_suspend
) {
326 bfin_wdt_set_timeout(timeout
);
333 # define bfin_wdt_suspend NULL
334 # define bfin_wdt_resume NULL
337 static const struct file_operations bfin_wdt_fops
= {
338 .owner
= THIS_MODULE
,
340 .write
= bfin_wdt_write
,
341 .unlocked_ioctl
= bfin_wdt_ioctl
,
342 .open
= bfin_wdt_open
,
343 .release
= bfin_wdt_release
,
346 static struct miscdevice bfin_wdt_miscdev
= {
347 .minor
= WATCHDOG_MINOR
,
349 .fops
= &bfin_wdt_fops
,
352 static const struct watchdog_info bfin_wdt_info
= {
353 .identity
= "Blackfin Watchdog",
354 .options
= WDIOF_SETTIMEOUT
|
355 WDIOF_KEEPALIVEPING
|
360 * bfin_wdt_probe - Initialize module
362 * Registers the misc device. Actual device
363 * initialization is handled by bfin_wdt_open().
365 static int __devinit
bfin_wdt_probe(struct platform_device
*pdev
)
369 ret
= misc_register(&bfin_wdt_miscdev
);
371 pr_devinit(KERN_ERR PFX
372 "cannot register miscdev on minor=%d (err=%d)\n",
373 WATCHDOG_MINOR
, ret
);
377 pr_devinit(KERN_INFO PFX
"initialized: timeout=%d sec (nowayout=%d)\n",
384 * bfin_wdt_remove - Initialize module
386 * Unregisters the misc device. Actual device
387 * deinitialization is handled by bfin_wdt_close().
389 static int __devexit
bfin_wdt_remove(struct platform_device
*pdev
)
391 misc_deregister(&bfin_wdt_miscdev
);
396 * bfin_wdt_shutdown - Soft Shutdown Handler
398 * Handles the soft shutdown event.
400 static void bfin_wdt_shutdown(struct platform_device
*pdev
)
407 static struct platform_device
*bfin_wdt_device
;
409 static struct platform_driver bfin_wdt_driver
= {
410 .probe
= bfin_wdt_probe
,
411 .remove
= __devexit_p(bfin_wdt_remove
),
412 .shutdown
= bfin_wdt_shutdown
,
413 .suspend
= bfin_wdt_suspend
,
414 .resume
= bfin_wdt_resume
,
416 .name
= WATCHDOG_NAME
,
417 .owner
= THIS_MODULE
,
422 * bfin_wdt_init - Initialize module
424 * Checks the module params and registers the platform device & driver.
425 * Real work is in the platform probe function.
427 static int __init
bfin_wdt_init(void)
433 /* Check that the timeout value is within range */
434 if (bfin_wdt_set_timeout(timeout
))
437 /* Since this is an on-chip device and needs no board-specific
438 * resources, we'll handle all the platform device stuff here.
440 ret
= platform_driver_register(&bfin_wdt_driver
);
442 pr_init(KERN_ERR PFX
"unable to register driver\n");
446 bfin_wdt_device
= platform_device_register_simple(WATCHDOG_NAME
,
448 if (IS_ERR(bfin_wdt_device
)) {
449 pr_init(KERN_ERR PFX
"unable to register device\n");
450 platform_driver_unregister(&bfin_wdt_driver
);
451 return PTR_ERR(bfin_wdt_device
);
458 * bfin_wdt_exit - Deinitialize module
460 * Back out the platform device & driver steps. Real work is in the
461 * platform remove function.
463 static void __exit
bfin_wdt_exit(void)
465 platform_device_unregister(bfin_wdt_device
);
466 platform_driver_unregister(&bfin_wdt_driver
);
469 module_init(bfin_wdt_init
);
470 module_exit(bfin_wdt_exit
);
472 MODULE_AUTHOR("Michele d'Amico, Mike Frysinger <vapier@gentoo.org>");
473 MODULE_DESCRIPTION("Blackfin Watchdog Device Driver");
474 MODULE_LICENSE("GPL");
475 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR
);
477 module_param(timeout
, uint
, 0);
478 MODULE_PARM_DESC(timeout
,
479 "Watchdog timeout in seconds. (1<=timeout<=((2^32)/SCLK), default="
480 __MODULE_STRING(WATCHDOG_TIMEOUT
) ")");
482 module_param(nowayout
, int, 0);
483 MODULE_PARM_DESC(nowayout
,
484 "Watchdog cannot be stopped once started (default="
485 __MODULE_STRING(WATCHDOG_NOWAYOUT
) ")");