Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / watchdog / bfin_wdt.c
blob5e3392462960318d86b008fb42cc69a29c13702c
1 /*
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>
22 #include <linux/fs.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
33 =======
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.
43 #ifdef BF561_FAMILY
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)
50 #endif
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
60 #define ICTL_NMI 0x2
61 #define ICTL_GPI 0x4
62 #define ICTL_NONE 0x6
63 #define ICTL_MASK 0x6
65 /* Masks for WDEN field in WDOG_CTL register */
66 #define WDEN_MASK 0x0FF0
67 #define WDEN_ENABLE 0x0000
68 #define WDEN_DISABLE 0x0AD0
70 /* some defaults */
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);
80 /**
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)
87 stampit();
88 bfin_write_WDOG_STAT(0);
89 return 0;
92 /**
93 * bfin_wdt_stop - Stop the Watchdog
95 * Stops the on-chip watchdog.
97 static int bfin_wdt_stop(void)
99 stampit();
100 bfin_write_WDOG_CTL(WDEN_DISABLE);
101 return 0;
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)
112 stampit();
113 bfin_write_WDOG_CTL(WDEN_ENABLE | ICTL_RESET);
114 return 0;
118 * bfin_wdt_running - Check Watchdog status
120 * See if the watchdog is running.
122 static int bfin_wdt_running(void)
124 stampit();
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;
138 unsigned long flags;
140 stampit();
142 cnt = t * get_sclk();
143 if (cnt < get_sclk()) {
144 printk(KERN_WARNING PFX "timeout value is too large\n");
145 return -EINVAL;
148 spin_lock_irqsave(&bfin_wdt_spinlock, flags);
150 int run = bfin_wdt_running();
151 bfin_wdt_stop();
152 bfin_write_WDOG_CNT(cnt);
153 if (run) bfin_wdt_start();
155 spin_unlock_irqrestore(&bfin_wdt_spinlock, flags);
157 timeout = t;
159 return 0;
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)
171 stampit();
173 if (test_and_set_bit(0, &open_check))
174 return -EBUSY;
176 if (nowayout)
177 __module_get(THIS_MODULE);
179 bfin_wdt_keepalive();
180 bfin_wdt_start();
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)
194 stampit();
196 if (expect_close == 42) {
197 bfin_wdt_stop();
198 } else {
199 printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
200 bfin_wdt_keepalive();
203 expect_close = 0;
204 clear_bit(0, &open_check);
206 return 0;
210 * bfin_wdt_write - Write to Device
211 * @file: file handle of device
212 * @buf: buffer to write
213 * @count: length of buffer
214 * @ppos: offset
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)
221 stampit();
223 if (len) {
224 if (!nowayout) {
225 size_t i;
227 /* In case it was set long ago */
228 expect_close = 0;
230 for (i = 0; i != len; i++) {
231 char c;
232 if (get_user(c, data + i))
233 return -EFAULT;
234 if (c == 'V')
235 expect_close = 42;
238 bfin_wdt_keepalive();
241 return len;
245 * bfin_wdt_ioctl - Query Device
246 * @inode: inode of device
247 * @file: file handle of device
248 * @cmd: watchdog command
249 * @arg: argument
251 * Query basic information from the device or ping it, as outlined by the
252 * watchdog API.
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;
260 stampit();
262 switch (cmd) {
263 default:
264 return -ENOTTY;
266 case WDIOC_GETSUPPORT:
267 if (copy_to_user(argp, &bfin_wdt_info, sizeof(bfin_wdt_info)))
268 return -EFAULT;
269 else
270 return 0;
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();
278 return 0;
280 case WDIOC_SETTIMEOUT: {
281 int new_timeout;
283 if (get_user(new_timeout, p))
284 return -EFAULT;
286 if (bfin_wdt_set_timeout(new_timeout))
287 return -EINVAL;
289 /* Fall */
290 case WDIOC_GETTIMEOUT:
291 return put_user(timeout, p);
293 case WDIOC_SETOPTIONS: {
294 unsigned long flags;
295 int options, ret = -EINVAL;
297 if (get_user(options, p))
298 return -EFAULT;
300 spin_lock_irqsave(&bfin_wdt_spinlock, flags);
302 if (options & WDIOS_DISABLECARD) {
303 bfin_wdt_stop();
304 ret = 0;
307 if (options & WDIOS_ENABLECARD) {
308 bfin_wdt_start();
309 ret = 0;
312 spin_unlock_irqrestore(&bfin_wdt_spinlock, flags);
314 return ret;
320 * bfin_wdt_notify_sys - Notifier Handler
321 * @this: notifier block
322 * @code: notifier event
323 * @unused: unused
325 * Handles specific events, such as turning off the watchdog during a
326 * shutdown event.
328 static int bfin_wdt_notify_sys(struct notifier_block *this, unsigned long code,
329 void *unused)
331 stampit();
333 if (code == SYS_DOWN || code == SYS_HALT)
334 bfin_wdt_stop();
336 return NOTIFY_DONE;
339 #ifdef CONFIG_PM
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)
353 stampit();
355 state_before_suspend = bfin_wdt_running();
356 bfin_wdt_stop();
358 return 0;
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)
369 stampit();
371 if (state_before_suspend) {
372 bfin_wdt_set_timeout(timeout);
373 bfin_wdt_start();
376 return 0;
378 #else
379 # define bfin_wdt_suspend NULL
380 # define bfin_wdt_resume NULL
381 #endif
383 static struct platform_device bfin_wdt_device = {
384 .name = WATCHDOG_NAME,
385 .id = -1,
388 static struct platform_driver bfin_wdt_driver = {
389 .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,
399 .llseek = no_llseek,
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,
408 .name = "watchdog",
409 .fops = &bfin_wdt_fops,
412 static struct watchdog_info bfin_wdt_info = {
413 .identity = "Blackfin Watchdog",
414 .options = WDIOF_SETTIMEOUT |
415 WDIOF_KEEPALIVEPING |
416 WDIOF_MAGICCLOSE,
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)
431 int ret;
433 stampit();
435 /* Check that the timeout value is within range */
436 if (bfin_wdt_set_timeout(timeout))
437 return -EINVAL;
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);
443 if (ret)
444 return ret;
446 ret = platform_driver_probe(&bfin_wdt_driver, NULL);
447 if (ret)
448 return ret;
450 ret = register_reboot_notifier(&bfin_wdt_notifier);
451 if (ret) {
452 <<<<<<< HEAD:drivers/watchdog/bfin_wdt.c
453 printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n", ret);
454 =======
455 pr_init(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n", ret);
456 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/watchdog/bfin_wdt.c
457 return ret;
460 ret = misc_register(&bfin_wdt_miscdev);
461 if (ret) {
462 <<<<<<< HEAD:drivers/watchdog/bfin_wdt.c
463 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
464 =======
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);
469 return ret;
472 <<<<<<< HEAD:drivers/watchdog/bfin_wdt.c
473 printk(KERN_INFO PFX "initialized: timeout=%d sec (nowayout=%d)\n",
474 =======
475 pr_init(KERN_INFO PFX "initialized: timeout=%d sec (nowayout=%d)\n",
476 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/watchdog/bfin_wdt.c
477 timeout, nowayout);
479 return 0;
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) ")");