mfd: Convert WM8350 to genirq
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / watchdog / bfin_wdt.c
blob2159e668751cd7db5e9ddfb39c96984573801539
1 /*
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>
21 #include <linux/fs.h>
22 #include <linux/notifier.h>
23 #include <linux/reboot.h>
24 #include <linux/init.h>
25 #include <linux/interrupt.h>
26 #include <linux/uaccess.h>
27 #include <asm/blackfin.h>
29 #define stamp(fmt, args...) \
30 pr_debug("%s:%i: " fmt "\n", __func__, __LINE__, ## args)
31 #define stampit() stamp("here i am")
32 #define pr_devinit(fmt, args...) \
33 ({ static const __devinitconst char __fmt[] = fmt; \
34 printk(__fmt, ## args); })
35 #define pr_init(fmt, args...) \
36 ({ static const __initconst char __fmt[] = fmt; \
37 printk(__fmt, ## args); })
39 #define WATCHDOG_NAME "bfin-wdt"
40 #define PFX WATCHDOG_NAME ": "
42 /* The BF561 has two watchdogs (one per core), but since Linux
43 * only runs on core A, we'll just work with that one.
45 #ifdef BF561_FAMILY
46 # define bfin_read_WDOG_CTL() bfin_read_WDOGA_CTL()
47 # define bfin_read_WDOG_CNT() bfin_read_WDOGA_CNT()
48 # define bfin_read_WDOG_STAT() bfin_read_WDOGA_STAT()
49 # define bfin_write_WDOG_CTL(x) bfin_write_WDOGA_CTL(x)
50 # define bfin_write_WDOG_CNT(x) bfin_write_WDOGA_CNT(x)
51 # define bfin_write_WDOG_STAT(x) bfin_write_WDOGA_STAT(x)
52 #endif
54 /* Bit in SWRST that indicates boot caused by watchdog */
55 #define SWRST_RESET_WDOG 0x4000
57 /* Bit in WDOG_CTL that indicates watchdog has expired (WDR0) */
58 #define WDOG_EXPIRED 0x8000
60 /* Masks for WDEV field in WDOG_CTL register */
61 #define ICTL_RESET 0x0
62 #define ICTL_NMI 0x2
63 #define ICTL_GPI 0x4
64 #define ICTL_NONE 0x6
65 #define ICTL_MASK 0x6
67 /* Masks for WDEN field in WDOG_CTL register */
68 #define WDEN_MASK 0x0FF0
69 #define WDEN_ENABLE 0x0000
70 #define WDEN_DISABLE 0x0AD0
72 /* some defaults */
73 #define WATCHDOG_TIMEOUT 20
75 static unsigned int timeout = WATCHDOG_TIMEOUT;
76 static int nowayout = WATCHDOG_NOWAYOUT;
77 static struct watchdog_info bfin_wdt_info;
78 static unsigned long open_check;
79 static char expect_close;
80 static DEFINE_SPINLOCK(bfin_wdt_spinlock);
82 /**
83 * bfin_wdt_keepalive - Keep the Userspace Watchdog Alive
85 * The Userspace watchdog got a KeepAlive: schedule the next timeout.
87 static int bfin_wdt_keepalive(void)
89 stampit();
90 bfin_write_WDOG_STAT(0);
91 return 0;
94 /**
95 * bfin_wdt_stop - Stop the Watchdog
97 * Stops the on-chip watchdog.
99 static int bfin_wdt_stop(void)
101 stampit();
102 bfin_write_WDOG_CTL(WDEN_DISABLE);
103 return 0;
107 * bfin_wdt_start - Start the Watchdog
109 * Starts the on-chip watchdog. Automatically loads WDOG_CNT
110 * into WDOG_STAT for us.
112 static int bfin_wdt_start(void)
114 stampit();
115 bfin_write_WDOG_CTL(WDEN_ENABLE | ICTL_RESET);
116 return 0;
120 * bfin_wdt_running - Check Watchdog status
122 * See if the watchdog is running.
124 static int bfin_wdt_running(void)
126 stampit();
127 return ((bfin_read_WDOG_CTL() & WDEN_MASK) != WDEN_DISABLE);
131 * bfin_wdt_set_timeout - Set the Userspace Watchdog timeout
132 * @t: new timeout value (in seconds)
134 * Translate the specified timeout in seconds into System Clock
135 * terms which is what the on-chip Watchdog requires.
137 static int bfin_wdt_set_timeout(unsigned long t)
139 u32 cnt, max_t, sclk;
140 unsigned long flags;
142 sclk = get_sclk();
143 max_t = -1 / sclk;
144 cnt = t * sclk;
145 stamp("maxtimeout=%us newtimeout=%lus (cnt=%#x)", max_t, t, cnt);
147 if (t > max_t) {
148 printk(KERN_WARNING PFX "timeout value is too large\n");
149 return -EINVAL;
152 spin_lock_irqsave(&bfin_wdt_spinlock, flags);
154 int run = bfin_wdt_running();
155 bfin_wdt_stop();
156 bfin_write_WDOG_CNT(cnt);
157 if (run)
158 bfin_wdt_start();
160 spin_unlock_irqrestore(&bfin_wdt_spinlock, flags);
162 timeout = t;
164 return 0;
168 * bfin_wdt_open - Open the Device
169 * @inode: inode of device
170 * @file: file handle of device
172 * Watchdog device is opened and started.
174 static int bfin_wdt_open(struct inode *inode, struct file *file)
176 stampit();
178 if (test_and_set_bit(0, &open_check))
179 return -EBUSY;
181 if (nowayout)
182 __module_get(THIS_MODULE);
184 bfin_wdt_keepalive();
185 bfin_wdt_start();
187 return nonseekable_open(inode, file);
191 * bfin_wdt_close - Close the Device
192 * @inode: inode of device
193 * @file: file handle of device
195 * Watchdog device is closed and stopped.
197 static int bfin_wdt_release(struct inode *inode, struct file *file)
199 stampit();
201 if (expect_close == 42)
202 bfin_wdt_stop();
203 else {
204 printk(KERN_CRIT PFX
205 "Unexpected close, not stopping watchdog!\n");
206 bfin_wdt_keepalive();
208 expect_close = 0;
209 clear_bit(0, &open_check);
210 return 0;
214 * bfin_wdt_write - Write to Device
215 * @file: file handle of device
216 * @buf: buffer to write
217 * @count: length of buffer
218 * @ppos: offset
220 * Pings the watchdog on write.
222 static ssize_t bfin_wdt_write(struct file *file, const char __user *data,
223 size_t len, loff_t *ppos)
225 stampit();
227 if (len) {
228 if (!nowayout) {
229 size_t i;
231 /* In case it was set long ago */
232 expect_close = 0;
234 for (i = 0; i != len; i++) {
235 char c;
236 if (get_user(c, data + i))
237 return -EFAULT;
238 if (c == 'V')
239 expect_close = 42;
242 bfin_wdt_keepalive();
245 return len;
249 * bfin_wdt_ioctl - Query Device
250 * @file: file handle of device
251 * @cmd: watchdog command
252 * @arg: argument
254 * Query basic information from the device or ping it, as outlined by the
255 * watchdog API.
257 static long bfin_wdt_ioctl(struct file *file,
258 unsigned int cmd, unsigned long arg)
260 void __user *argp = (void __user *)arg;
261 int __user *p = argp;
263 stampit();
265 switch (cmd) {
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;
271 case WDIOC_GETSTATUS:
272 case WDIOC_GETBOOTSTATUS:
273 return put_user(!!(_bfin_swrst & SWRST_RESET_WDOG), p);
274 case WDIOC_SETOPTIONS: {
275 unsigned long flags;
276 int options, ret = -EINVAL;
278 if (get_user(options, p))
279 return -EFAULT;
281 spin_lock_irqsave(&bfin_wdt_spinlock, flags);
282 if (options & WDIOS_DISABLECARD) {
283 bfin_wdt_stop();
284 ret = 0;
286 if (options & WDIOS_ENABLECARD) {
287 bfin_wdt_start();
288 ret = 0;
290 spin_unlock_irqrestore(&bfin_wdt_spinlock, flags);
291 return ret;
293 case WDIOC_KEEPALIVE:
294 bfin_wdt_keepalive();
295 return 0;
296 case WDIOC_SETTIMEOUT: {
297 int new_timeout;
299 if (get_user(new_timeout, p))
300 return -EFAULT;
301 if (bfin_wdt_set_timeout(new_timeout))
302 return -EINVAL;
304 /* Fall */
305 case WDIOC_GETTIMEOUT:
306 return put_user(timeout, p);
307 default:
308 return -ENOTTY;
313 * bfin_wdt_notify_sys - Notifier Handler
314 * @this: notifier block
315 * @code: notifier event
316 * @unused: unused
318 * Handles specific events, such as turning off the watchdog during a
319 * shutdown event.
321 static int bfin_wdt_notify_sys(struct notifier_block *this,
322 unsigned long code, void *unused)
324 stampit();
326 if (code == SYS_DOWN || code == SYS_HALT)
327 bfin_wdt_stop();
329 return NOTIFY_DONE;
332 #ifdef CONFIG_PM
333 static int state_before_suspend;
336 * bfin_wdt_suspend - suspend the watchdog
337 * @pdev: device being suspended
338 * @state: requested suspend state
340 * Remember if the watchdog was running and stop it.
341 * TODO: is this even right? Doesn't seem to be any
342 * standard in the watchdog world ...
344 static int bfin_wdt_suspend(struct platform_device *pdev, pm_message_t state)
346 stampit();
348 state_before_suspend = bfin_wdt_running();
349 bfin_wdt_stop();
351 return 0;
355 * bfin_wdt_resume - resume the watchdog
356 * @pdev: device being resumed
358 * If the watchdog was running, turn it back on.
360 static int bfin_wdt_resume(struct platform_device *pdev)
362 stampit();
364 if (state_before_suspend) {
365 bfin_wdt_set_timeout(timeout);
366 bfin_wdt_start();
369 return 0;
371 #else
372 # define bfin_wdt_suspend NULL
373 # define bfin_wdt_resume NULL
374 #endif
376 static const struct file_operations bfin_wdt_fops = {
377 .owner = THIS_MODULE,
378 .llseek = no_llseek,
379 .write = bfin_wdt_write,
380 .unlocked_ioctl = bfin_wdt_ioctl,
381 .open = bfin_wdt_open,
382 .release = bfin_wdt_release,
385 static struct miscdevice bfin_wdt_miscdev = {
386 .minor = WATCHDOG_MINOR,
387 .name = "watchdog",
388 .fops = &bfin_wdt_fops,
391 static struct watchdog_info bfin_wdt_info = {
392 .identity = "Blackfin Watchdog",
393 .options = WDIOF_SETTIMEOUT |
394 WDIOF_KEEPALIVEPING |
395 WDIOF_MAGICCLOSE,
398 static struct notifier_block bfin_wdt_notifier = {
399 .notifier_call = bfin_wdt_notify_sys,
403 * bfin_wdt_probe - Initialize module
405 * Registers the misc device and notifier handler. Actual device
406 * initialization is handled by bfin_wdt_open().
408 static int __devinit bfin_wdt_probe(struct platform_device *pdev)
410 int ret;
412 ret = register_reboot_notifier(&bfin_wdt_notifier);
413 if (ret) {
414 pr_devinit(KERN_ERR PFX
415 "cannot register reboot notifier (err=%d)\n", ret);
416 return ret;
419 ret = misc_register(&bfin_wdt_miscdev);
420 if (ret) {
421 pr_devinit(KERN_ERR PFX
422 "cannot register miscdev on minor=%d (err=%d)\n",
423 WATCHDOG_MINOR, ret);
424 unregister_reboot_notifier(&bfin_wdt_notifier);
425 return ret;
428 pr_devinit(KERN_INFO PFX "initialized: timeout=%d sec (nowayout=%d)\n",
429 timeout, nowayout);
431 return 0;
435 * bfin_wdt_remove - Initialize module
437 * Unregisters the misc device and notifier handler. Actual device
438 * deinitialization is handled by bfin_wdt_close().
440 static int __devexit bfin_wdt_remove(struct platform_device *pdev)
442 misc_deregister(&bfin_wdt_miscdev);
443 unregister_reboot_notifier(&bfin_wdt_notifier);
444 return 0;
447 static struct platform_device *bfin_wdt_device;
449 static struct platform_driver bfin_wdt_driver = {
450 .probe = bfin_wdt_probe,
451 .remove = __devexit_p(bfin_wdt_remove),
452 .suspend = bfin_wdt_suspend,
453 .resume = bfin_wdt_resume,
454 .driver = {
455 .name = WATCHDOG_NAME,
456 .owner = THIS_MODULE,
461 * bfin_wdt_init - Initialize module
463 * Checks the module params and registers the platform device & driver.
464 * Real work is in the platform probe function.
466 static int __init bfin_wdt_init(void)
468 int ret;
470 stampit();
472 /* Check that the timeout value is within range */
473 if (bfin_wdt_set_timeout(timeout))
474 return -EINVAL;
476 /* Since this is an on-chip device and needs no board-specific
477 * resources, we'll handle all the platform device stuff here.
479 ret = platform_driver_register(&bfin_wdt_driver);
480 if (ret) {
481 pr_init(KERN_ERR PFX "unable to register driver\n");
482 return ret;
485 bfin_wdt_device = platform_device_register_simple(WATCHDOG_NAME,
486 -1, NULL, 0);
487 if (IS_ERR(bfin_wdt_device)) {
488 pr_init(KERN_ERR PFX "unable to register device\n");
489 platform_driver_unregister(&bfin_wdt_driver);
490 return PTR_ERR(bfin_wdt_device);
493 return 0;
497 * bfin_wdt_exit - Deinitialize module
499 * Back out the platform device & driver steps. Real work is in the
500 * platform remove function.
502 static void __exit bfin_wdt_exit(void)
504 platform_device_unregister(bfin_wdt_device);
505 platform_driver_unregister(&bfin_wdt_driver);
508 module_init(bfin_wdt_init);
509 module_exit(bfin_wdt_exit);
511 MODULE_AUTHOR("Michele d'Amico, Mike Frysinger <vapier@gentoo.org>");
512 MODULE_DESCRIPTION("Blackfin Watchdog Device Driver");
513 MODULE_LICENSE("GPL");
514 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
516 module_param(timeout, uint, 0);
517 MODULE_PARM_DESC(timeout,
518 "Watchdog timeout in seconds. (1<=timeout<=((2^32)/SCLK), default="
519 __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
521 module_param(nowayout, int, 0);
522 MODULE_PARM_DESC(nowayout,
523 "Watchdog cannot be stopped once started (default="
524 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");