Staging: strip: delete the driver
[linux/fpc-iii.git] / drivers / watchdog / bfin_wdt.c
blob9c7ccd1e9088fbd5f108fe7c96980d7cda07d8e3
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/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.
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 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);
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, max_t, sclk;
138 unsigned long flags;
140 sclk = get_sclk();
141 max_t = -1 / sclk;
142 cnt = t * sclk;
143 stamp("maxtimeout=%us newtimeout=%lus (cnt=%#x)", max_t, t, cnt);
145 if (t > max_t) {
146 printk(KERN_WARNING PFX "timeout value is too large\n");
147 return -EINVAL;
150 spin_lock_irqsave(&bfin_wdt_spinlock, flags);
152 int run = bfin_wdt_running();
153 bfin_wdt_stop();
154 bfin_write_WDOG_CNT(cnt);
155 if (run)
156 bfin_wdt_start();
158 spin_unlock_irqrestore(&bfin_wdt_spinlock, flags);
160 timeout = t;
162 return 0;
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)
174 stampit();
176 if (test_and_set_bit(0, &open_check))
177 return -EBUSY;
179 if (nowayout)
180 __module_get(THIS_MODULE);
182 bfin_wdt_keepalive();
183 bfin_wdt_start();
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)
197 stampit();
199 if (expect_close == 42)
200 bfin_wdt_stop();
201 else {
202 printk(KERN_CRIT PFX
203 "Unexpected close, not stopping watchdog!\n");
204 bfin_wdt_keepalive();
206 expect_close = 0;
207 clear_bit(0, &open_check);
208 return 0;
212 * bfin_wdt_write - Write to Device
213 * @file: file handle of device
214 * @buf: buffer to write
215 * @count: length of buffer
216 * @ppos: offset
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)
223 stampit();
225 if (len) {
226 if (!nowayout) {
227 size_t i;
229 /* In case it was set long ago */
230 expect_close = 0;
232 for (i = 0; i != len; i++) {
233 char c;
234 if (get_user(c, data + i))
235 return -EFAULT;
236 if (c == 'V')
237 expect_close = 42;
240 bfin_wdt_keepalive();
243 return len;
247 * bfin_wdt_ioctl - Query Device
248 * @file: file handle of device
249 * @cmd: watchdog command
250 * @arg: argument
252 * Query basic information from the device or ping it, as outlined by the
253 * watchdog API.
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;
261 stampit();
263 switch (cmd) {
264 case WDIOC_GETSUPPORT:
265 if (copy_to_user(argp, &bfin_wdt_info, sizeof(bfin_wdt_info)))
266 return -EFAULT;
267 else
268 return 0;
269 case WDIOC_GETSTATUS:
270 case WDIOC_GETBOOTSTATUS:
271 return put_user(!!(_bfin_swrst & SWRST_RESET_WDOG), p);
272 case WDIOC_SETOPTIONS: {
273 unsigned long flags;
274 int options, ret = -EINVAL;
276 if (get_user(options, p))
277 return -EFAULT;
279 spin_lock_irqsave(&bfin_wdt_spinlock, flags);
280 if (options & WDIOS_DISABLECARD) {
281 bfin_wdt_stop();
282 ret = 0;
284 if (options & WDIOS_ENABLECARD) {
285 bfin_wdt_start();
286 ret = 0;
288 spin_unlock_irqrestore(&bfin_wdt_spinlock, flags);
289 return ret;
291 case WDIOC_KEEPALIVE:
292 bfin_wdt_keepalive();
293 return 0;
294 case WDIOC_SETTIMEOUT: {
295 int new_timeout;
297 if (get_user(new_timeout, p))
298 return -EFAULT;
299 if (bfin_wdt_set_timeout(new_timeout))
300 return -EINVAL;
302 /* Fall */
303 case WDIOC_GETTIMEOUT:
304 return put_user(timeout, p);
305 default:
306 return -ENOTTY;
310 #ifdef CONFIG_PM
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)
324 stampit();
326 state_before_suspend = bfin_wdt_running();
327 bfin_wdt_stop();
329 return 0;
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)
340 stampit();
342 if (state_before_suspend) {
343 bfin_wdt_set_timeout(timeout);
344 bfin_wdt_start();
347 return 0;
349 #else
350 # define bfin_wdt_suspend NULL
351 # define bfin_wdt_resume NULL
352 #endif
354 static const struct file_operations bfin_wdt_fops = {
355 .owner = THIS_MODULE,
356 .llseek = no_llseek,
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,
365 .name = "watchdog",
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 |
373 WDIOF_MAGICCLOSE,
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)
384 int ret;
386 ret = misc_register(&bfin_wdt_miscdev);
387 if (ret) {
388 pr_devinit(KERN_ERR PFX
389 "cannot register miscdev on minor=%d (err=%d)\n",
390 WATCHDOG_MINOR, ret);
391 return ret;
394 pr_devinit(KERN_INFO PFX "initialized: timeout=%d sec (nowayout=%d)\n",
395 timeout, nowayout);
397 return 0;
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);
409 return 0;
413 * bfin_wdt_shutdown - Soft Shutdown Handler
415 * Handles the soft shutdown event.
417 static void bfin_wdt_shutdown(struct platform_device *pdev)
419 stampit();
421 bfin_wdt_stop();
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,
432 .driver = {
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)
446 int ret;
448 stampit();
450 /* Check that the timeout value is within range */
451 if (bfin_wdt_set_timeout(timeout))
452 return -EINVAL;
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);
458 if (ret) {
459 pr_init(KERN_ERR PFX "unable to register driver\n");
460 return ret;
463 bfin_wdt_device = platform_device_register_simple(WATCHDOG_NAME,
464 -1, NULL, 0);
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);
471 return 0;
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) ")");