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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/platform_device.h>
17 #include <linux/module.h>
18 #include <linux/moduleparam.h>
19 #include <linux/types.h>
20 #include <linux/timer.h>
21 #include <linux/miscdevice.h>
22 #include <linux/watchdog.h>
24 #include <linux/init.h>
25 #include <linux/interrupt.h>
26 #include <linux/uaccess.h>
27 #include <asm/blackfin.h>
28 #include <asm/bfin_watchdog.h>
30 #define stamp(fmt, args...) \
31 pr_debug("%s:%i: " fmt "\n", __func__, __LINE__, ## args)
32 #define stampit() stamp("here i am")
34 #define WATCHDOG_NAME "bfin-wdt"
36 /* The BF561 has two watchdogs (one per core), but since Linux
37 * only runs on core A, we'll just work with that one.
40 # define bfin_read_WDOG_CTL() bfin_read_WDOGA_CTL()
41 # define bfin_read_WDOG_CNT() bfin_read_WDOGA_CNT()
42 # define bfin_read_WDOG_STAT() bfin_read_WDOGA_STAT()
43 # define bfin_write_WDOG_CTL(x) bfin_write_WDOGA_CTL(x)
44 # define bfin_write_WDOG_CNT(x) bfin_write_WDOGA_CNT(x)
45 # define bfin_write_WDOG_STAT(x) bfin_write_WDOGA_STAT(x)
49 #define WATCHDOG_TIMEOUT 20
51 static unsigned int timeout = WATCHDOG_TIMEOUT;
52 static bool nowayout = WATCHDOG_NOWAYOUT;
53 static const struct watchdog_info bfin_wdt_info;
54 static unsigned long open_check;
55 static char expect_close;
56 static DEFINE_SPINLOCK(bfin_wdt_spinlock);
59 * bfin_wdt_keepalive - Keep the Userspace Watchdog Alive
61 * The Userspace watchdog got a KeepAlive: schedule the next timeout.
63 static int bfin_wdt_keepalive(void)
66 bfin_write_WDOG_STAT(0);
71 * bfin_wdt_stop - Stop the Watchdog
73 * Stops the on-chip watchdog.
75 static int bfin_wdt_stop(void)
78 bfin_write_WDOG_CTL(WDEN_DISABLE);
83 * bfin_wdt_start - Start the Watchdog
85 * Starts the on-chip watchdog. Automatically loads WDOG_CNT
86 * into WDOG_STAT for us.
88 static int bfin_wdt_start(void)
91 bfin_write_WDOG_CTL(WDEN_ENABLE | ICTL_RESET);
96 * bfin_wdt_running - Check Watchdog status
98 * See if the watchdog is running.
100 static int bfin_wdt_running(void)
103 return ((bfin_read_WDOG_CTL() & WDEN_MASK) != WDEN_DISABLE);
107 * bfin_wdt_set_timeout - Set the Userspace Watchdog timeout
108 * @t: new timeout value (in seconds)
110 * Translate the specified timeout in seconds into System Clock
111 * terms which is what the on-chip Watchdog requires.
113 static int bfin_wdt_set_timeout(unsigned long t)
115 u32 cnt, max_t, sclk;
121 stamp("maxtimeout=%us newtimeout=%lus (cnt=%#x)", max_t, t, cnt);
124 pr_warn("timeout value is too large\n");
128 spin_lock_irqsave(&bfin_wdt_spinlock, flags);
130 int run = bfin_wdt_running();
132 bfin_write_WDOG_CNT(cnt);
136 spin_unlock_irqrestore(&bfin_wdt_spinlock, flags);
144 * bfin_wdt_open - Open the Device
145 * @inode: inode of device
146 * @file: file handle of device
148 * Watchdog device is opened and started.
150 static int bfin_wdt_open(struct inode *inode, struct file *file)
154 if (test_and_set_bit(0, &open_check))
158 __module_get(THIS_MODULE);
160 bfin_wdt_keepalive();
163 return nonseekable_open(inode, file);
167 * bfin_wdt_close - Close the Device
168 * @inode: inode of device
169 * @file: file handle of device
171 * Watchdog device is closed and stopped.
173 static int bfin_wdt_release(struct inode *inode, struct file *file)
177 if (expect_close == 42)
180 pr_crit("Unexpected close, not stopping watchdog!\n");
181 bfin_wdt_keepalive();
184 clear_bit(0, &open_check);
189 * bfin_wdt_write - Write to Device
190 * @file: file handle of device
191 * @buf: buffer to write
192 * @count: length of buffer
195 * Pings the watchdog on write.
197 static ssize_t bfin_wdt_write(struct file *file, const char __user *data,
198 size_t len, loff_t *ppos)
206 /* In case it was set long ago */
209 for (i = 0; i != len; i++) {
211 if (get_user(c, data + i))
217 bfin_wdt_keepalive();
224 * bfin_wdt_ioctl - Query Device
225 * @file: file handle of device
226 * @cmd: watchdog command
229 * Query basic information from the device or ping it, as outlined by the
232 static long bfin_wdt_ioctl(struct file *file,
233 unsigned int cmd, unsigned long arg)
235 void __user *argp = (void __user *)arg;
236 int __user *p = argp;
241 case WDIOC_GETSUPPORT:
242 if (copy_to_user(argp, &bfin_wdt_info, sizeof(bfin_wdt_info)))
246 case WDIOC_GETSTATUS:
247 case WDIOC_GETBOOTSTATUS:
248 return put_user(!!(_bfin_swrst & SWRST_RESET_WDOG), p);
249 case WDIOC_SETOPTIONS: {
251 int options, ret = -EINVAL;
253 if (get_user(options, p))
256 spin_lock_irqsave(&bfin_wdt_spinlock, flags);
257 if (options & WDIOS_DISABLECARD) {
261 if (options & WDIOS_ENABLECARD) {
265 spin_unlock_irqrestore(&bfin_wdt_spinlock, flags);
268 case WDIOC_KEEPALIVE:
269 bfin_wdt_keepalive();
271 case WDIOC_SETTIMEOUT: {
274 if (get_user(new_timeout, p))
276 if (bfin_wdt_set_timeout(new_timeout))
280 case WDIOC_GETTIMEOUT:
281 return put_user(timeout, p);
288 static int state_before_suspend;
291 * bfin_wdt_suspend - suspend the watchdog
292 * @pdev: device being suspended
293 * @state: requested suspend state
295 * Remember if the watchdog was running and stop it.
296 * TODO: is this even right? Doesn't seem to be any
297 * standard in the watchdog world ...
299 static int bfin_wdt_suspend(struct platform_device *pdev, pm_message_t state)
303 state_before_suspend = bfin_wdt_running();
310 * bfin_wdt_resume - resume the watchdog
311 * @pdev: device being resumed
313 * If the watchdog was running, turn it back on.
315 static int bfin_wdt_resume(struct platform_device *pdev)
319 if (state_before_suspend) {
320 bfin_wdt_set_timeout(timeout);
327 # define bfin_wdt_suspend NULL
328 # define bfin_wdt_resume NULL
331 static const struct file_operations bfin_wdt_fops = {
332 .owner = THIS_MODULE,
334 .write = bfin_wdt_write,
335 .unlocked_ioctl = bfin_wdt_ioctl,
336 .open = bfin_wdt_open,
337 .release = bfin_wdt_release,
340 static struct miscdevice bfin_wdt_miscdev = {
341 .minor = WATCHDOG_MINOR,
343 .fops = &bfin_wdt_fops,
346 static const struct watchdog_info bfin_wdt_info = {
347 .identity = "Blackfin Watchdog",
348 .options = WDIOF_SETTIMEOUT |
349 WDIOF_KEEPALIVEPING |
354 * bfin_wdt_probe - Initialize module
356 * Registers the misc device. Actual device
357 * initialization is handled by bfin_wdt_open().
359 static int bfin_wdt_probe(struct platform_device *pdev)
363 ret = misc_register(&bfin_wdt_miscdev);
365 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
366 WATCHDOG_MINOR, ret);
370 pr_info("initialized: timeout=%d sec (nowayout=%d)\n",
377 * bfin_wdt_remove - Initialize module
379 * Unregisters the misc device. Actual device
380 * deinitialization is handled by bfin_wdt_close().
382 static int bfin_wdt_remove(struct platform_device *pdev)
384 misc_deregister(&bfin_wdt_miscdev);
389 * bfin_wdt_shutdown - Soft Shutdown Handler
391 * Handles the soft shutdown event.
393 static void bfin_wdt_shutdown(struct platform_device *pdev)
400 static struct platform_device *bfin_wdt_device;
402 static struct platform_driver bfin_wdt_driver = {
403 .probe = bfin_wdt_probe,
404 .remove = bfin_wdt_remove,
405 .shutdown = bfin_wdt_shutdown,
406 .suspend = bfin_wdt_suspend,
407 .resume = bfin_wdt_resume,
409 .name = WATCHDOG_NAME,
410 .owner = THIS_MODULE,
415 * bfin_wdt_init - Initialize module
417 * Checks the module params and registers the platform device & driver.
418 * Real work is in the platform probe function.
420 static int __init bfin_wdt_init(void)
426 /* Check that the timeout value is within range */
427 if (bfin_wdt_set_timeout(timeout))
430 /* Since this is an on-chip device and needs no board-specific
431 * resources, we'll handle all the platform device stuff here.
433 ret = platform_driver_register(&bfin_wdt_driver);
435 pr_err("unable to register driver\n");
439 bfin_wdt_device = platform_device_register_simple(WATCHDOG_NAME,
441 if (IS_ERR(bfin_wdt_device)) {
442 pr_err("unable to register device\n");
443 platform_driver_unregister(&bfin_wdt_driver);
444 return PTR_ERR(bfin_wdt_device);
451 * bfin_wdt_exit - Deinitialize module
453 * Back out the platform device & driver steps. Real work is in the
454 * platform remove function.
456 static void __exit bfin_wdt_exit(void)
458 platform_device_unregister(bfin_wdt_device);
459 platform_driver_unregister(&bfin_wdt_driver);
462 module_init(bfin_wdt_init);
463 module_exit(bfin_wdt_exit);
465 MODULE_AUTHOR("Michele d'Amico, Mike Frysinger <vapier@gentoo.org>");
466 MODULE_DESCRIPTION("Blackfin Watchdog Device Driver");
467 MODULE_LICENSE("GPL");
468 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
470 module_param(timeout, uint, 0);
471 MODULE_PARM_DESC(timeout,
472 "Watchdog timeout in seconds. (1<=timeout<=((2^32)/SCLK), default="
473 __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
475 module_param(nowayout, bool, 0);
476 MODULE_PARM_DESC(nowayout,
477 "Watchdog cannot be stopped once started (default="
478 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");