blob: b9fa9b71583a68b49ed1e8026329c1cc24bb431c [file] [log] [blame]
Bryan Wu1e6d3202007-07-15 02:50:02 +08001/*
2 * Blackfin On-Chip Watchdog Driver
Bryan Wu1e6d3202007-07-15 02:50:02 +08003 *
4 * Originally based on softdog.c
Mike Frysinger3dae93e2010-02-15 19:32:25 -05005 * Copyright 2006-2010 Analog Devices Inc.
Bryan Wu1e6d3202007-07-15 02:50:02 +08006 * Copyright 2006-2007 Michele d'Amico
Alan Cox29fa0582008-10-27 15:17:56 +00007 * Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>
Bryan Wu1e6d3202007-07-15 02:50:02 +08008 *
9 * Enter bugs at http://blackfin.uclinux.org/
10 *
11 * Licensed under the GPL-2 or later.
12 */
13
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>
Bryan Wu1e6d3202007-07-15 02:50:02 +080022#include <linux/init.h>
23#include <linux/interrupt.h>
Alan Cox9a5f50d2008-05-19 14:06:30 +010024#include <linux/uaccess.h>
Wim Van Sebroeck089ab072008-07-15 11:46:11 +000025#include <asm/blackfin.h>
Mike Frysinger42bd5d42010-03-09 11:07:40 -050026#include <asm/bfin_watchdog.h>
Bryan Wu1e6d3202007-07-15 02:50:02 +080027
Wim Van Sebroecka77dba72009-04-14 20:20:07 +000028#define stamp(fmt, args...) \
29 pr_debug("%s:%i: " fmt "\n", __func__, __LINE__, ## args)
Bryan Wu1e6d3202007-07-15 02:50:02 +080030#define stampit() stamp("here i am")
Wim Van Sebroecka77dba72009-04-14 20:20:07 +000031#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); })
Bryan Wu1e6d3202007-07-15 02:50:02 +080037
38#define WATCHDOG_NAME "bfin-wdt"
39#define PFX WATCHDOG_NAME ": "
40
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.
43 */
44#ifdef BF561_FAMILY
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)
51#endif
52
Bryan Wu1e6d3202007-07-15 02:50:02 +080053/* some defaults */
54#define WATCHDOG_TIMEOUT 20
55
56static unsigned int timeout = WATCHDOG_TIMEOUT;
57static int nowayout = WATCHDOG_NOWAYOUT;
Wim Van Sebroeck42747d72009-12-26 18:55:22 +000058static const struct watchdog_info bfin_wdt_info;
Bryan Wu1e6d3202007-07-15 02:50:02 +080059static unsigned long open_check;
60static char expect_close;
Jiri Slabybf6350a2007-11-10 05:58:44 +010061static DEFINE_SPINLOCK(bfin_wdt_spinlock);
Bryan Wu1e6d3202007-07-15 02:50:02 +080062
63/**
64 * bfin_wdt_keepalive - Keep the Userspace Watchdog Alive
65 *
Wim Van Sebroeck5f3b2752011-02-23 20:04:38 +000066 * The Userspace watchdog got a KeepAlive: schedule the next timeout.
Bryan Wu1e6d3202007-07-15 02:50:02 +080067 */
68static int bfin_wdt_keepalive(void)
69{
70 stampit();
71 bfin_write_WDOG_STAT(0);
72 return 0;
73}
74
75/**
76 * bfin_wdt_stop - Stop the Watchdog
77 *
78 * Stops the on-chip watchdog.
79 */
80static int bfin_wdt_stop(void)
81{
82 stampit();
83 bfin_write_WDOG_CTL(WDEN_DISABLE);
84 return 0;
85}
86
87/**
88 * bfin_wdt_start - Start the Watchdog
89 *
90 * Starts the on-chip watchdog. Automatically loads WDOG_CNT
91 * into WDOG_STAT for us.
92 */
93static int bfin_wdt_start(void)
94{
95 stampit();
96 bfin_write_WDOG_CTL(WDEN_ENABLE | ICTL_RESET);
97 return 0;
98}
99
100/**
101 * bfin_wdt_running - Check Watchdog status
102 *
103 * See if the watchdog is running.
104 */
105static int bfin_wdt_running(void)
106{
107 stampit();
108 return ((bfin_read_WDOG_CTL() & WDEN_MASK) != WDEN_DISABLE);
109}
110
111/**
112 * bfin_wdt_set_timeout - Set the Userspace Watchdog timeout
113 * @t: new timeout value (in seconds)
114 *
115 * Translate the specified timeout in seconds into System Clock
116 * terms which is what the on-chip Watchdog requires.
117 */
118static int bfin_wdt_set_timeout(unsigned long t)
119{
Mike Frysinger3dae93e2010-02-15 19:32:25 -0500120 u32 cnt, max_t, sclk;
Bryan Wu1e6d3202007-07-15 02:50:02 +0800121 unsigned long flags;
122
Mike Frysinger3dae93e2010-02-15 19:32:25 -0500123 sclk = get_sclk();
124 max_t = -1 / sclk;
125 cnt = t * sclk;
126 stamp("maxtimeout=%us newtimeout=%lus (cnt=%#x)", max_t, t, cnt);
Bryan Wu1e6d3202007-07-15 02:50:02 +0800127
Mike Frysinger3dae93e2010-02-15 19:32:25 -0500128 if (t > max_t) {
Bryan Wu1e6d3202007-07-15 02:50:02 +0800129 printk(KERN_WARNING PFX "timeout value is too large\n");
130 return -EINVAL;
131 }
132
133 spin_lock_irqsave(&bfin_wdt_spinlock, flags);
134 {
135 int run = bfin_wdt_running();
136 bfin_wdt_stop();
137 bfin_write_WDOG_CNT(cnt);
Alan Cox9a5f50d2008-05-19 14:06:30 +0100138 if (run)
139 bfin_wdt_start();
Bryan Wu1e6d3202007-07-15 02:50:02 +0800140 }
141 spin_unlock_irqrestore(&bfin_wdt_spinlock, flags);
142
143 timeout = t;
144
145 return 0;
146}
147
148/**
149 * bfin_wdt_open - Open the Device
150 * @inode: inode of device
151 * @file: file handle of device
152 *
153 * Watchdog device is opened and started.
154 */
155static int bfin_wdt_open(struct inode *inode, struct file *file)
156{
157 stampit();
158
159 if (test_and_set_bit(0, &open_check))
160 return -EBUSY;
161
162 if (nowayout)
163 __module_get(THIS_MODULE);
164
165 bfin_wdt_keepalive();
166 bfin_wdt_start();
167
168 return nonseekable_open(inode, file);
169}
170
171/**
172 * bfin_wdt_close - Close the Device
173 * @inode: inode of device
174 * @file: file handle of device
175 *
176 * Watchdog device is closed and stopped.
177 */
178static int bfin_wdt_release(struct inode *inode, struct file *file)
179{
180 stampit();
181
Alan Cox9a5f50d2008-05-19 14:06:30 +0100182 if (expect_close == 42)
Bryan Wu1e6d3202007-07-15 02:50:02 +0800183 bfin_wdt_stop();
Alan Cox9a5f50d2008-05-19 14:06:30 +0100184 else {
185 printk(KERN_CRIT PFX
186 "Unexpected close, not stopping watchdog!\n");
Bryan Wu1e6d3202007-07-15 02:50:02 +0800187 bfin_wdt_keepalive();
188 }
Bryan Wu1e6d3202007-07-15 02:50:02 +0800189 expect_close = 0;
190 clear_bit(0, &open_check);
Bryan Wu1e6d3202007-07-15 02:50:02 +0800191 return 0;
192}
193
194/**
195 * bfin_wdt_write - Write to Device
196 * @file: file handle of device
197 * @buf: buffer to write
198 * @count: length of buffer
199 * @ppos: offset
200 *
201 * Pings the watchdog on write.
202 */
203static ssize_t bfin_wdt_write(struct file *file, const char __user *data,
Alan Cox9a5f50d2008-05-19 14:06:30 +0100204 size_t len, loff_t *ppos)
Bryan Wu1e6d3202007-07-15 02:50:02 +0800205{
206 stampit();
207
208 if (len) {
209 if (!nowayout) {
210 size_t i;
211
212 /* In case it was set long ago */
213 expect_close = 0;
214
215 for (i = 0; i != len; i++) {
216 char c;
217 if (get_user(c, data + i))
218 return -EFAULT;
219 if (c == 'V')
220 expect_close = 42;
221 }
222 }
223 bfin_wdt_keepalive();
224 }
225
226 return len;
227}
228
229/**
230 * bfin_wdt_ioctl - Query Device
Bryan Wu1e6d3202007-07-15 02:50:02 +0800231 * @file: file handle of device
232 * @cmd: watchdog command
233 * @arg: argument
234 *
235 * Query basic information from the device or ping it, as outlined by the
236 * watchdog API.
237 */
Alan Cox9a5f50d2008-05-19 14:06:30 +0100238static long bfin_wdt_ioctl(struct file *file,
239 unsigned int cmd, unsigned long arg)
Bryan Wu1e6d3202007-07-15 02:50:02 +0800240{
241 void __user *argp = (void __user *)arg;
242 int __user *p = argp;
243
244 stampit();
245
246 switch (cmd) {
Alan Cox9a5f50d2008-05-19 14:06:30 +0100247 case WDIOC_GETSUPPORT:
248 if (copy_to_user(argp, &bfin_wdt_info, sizeof(bfin_wdt_info)))
249 return -EFAULT;
250 else
Bryan Wu1e6d3202007-07-15 02:50:02 +0800251 return 0;
Alan Cox9a5f50d2008-05-19 14:06:30 +0100252 case WDIOC_GETSTATUS:
253 case WDIOC_GETBOOTSTATUS:
254 return put_user(!!(_bfin_swrst & SWRST_RESET_WDOG), p);
Alan Cox9a5f50d2008-05-19 14:06:30 +0100255 case WDIOC_SETOPTIONS: {
256 unsigned long flags;
257 int options, ret = -EINVAL;
Bryan Wu1e6d3202007-07-15 02:50:02 +0800258
Alan Cox9a5f50d2008-05-19 14:06:30 +0100259 if (get_user(options, p))
260 return -EFAULT;
Bryan Wu1e6d3202007-07-15 02:50:02 +0800261
Alan Cox9a5f50d2008-05-19 14:06:30 +0100262 spin_lock_irqsave(&bfin_wdt_spinlock, flags);
263 if (options & WDIOS_DISABLECARD) {
264 bfin_wdt_stop();
265 ret = 0;
Bryan Wu1e6d3202007-07-15 02:50:02 +0800266 }
Alan Cox9a5f50d2008-05-19 14:06:30 +0100267 if (options & WDIOS_ENABLECARD) {
268 bfin_wdt_start();
269 ret = 0;
Bryan Wu1e6d3202007-07-15 02:50:02 +0800270 }
Alan Cox9a5f50d2008-05-19 14:06:30 +0100271 spin_unlock_irqrestore(&bfin_wdt_spinlock, flags);
272 return ret;
273 }
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000274 case WDIOC_KEEPALIVE:
275 bfin_wdt_keepalive();
276 return 0;
277 case WDIOC_SETTIMEOUT: {
278 int new_timeout;
279
280 if (get_user(new_timeout, p))
281 return -EFAULT;
282 if (bfin_wdt_set_timeout(new_timeout))
283 return -EINVAL;
284 }
285 /* Fall */
286 case WDIOC_GETTIMEOUT:
287 return put_user(timeout, p);
Alan Cox9a5f50d2008-05-19 14:06:30 +0100288 default:
289 return -ENOTTY;
Bryan Wu1e6d3202007-07-15 02:50:02 +0800290 }
291}
292
Bryan Wu1e6d3202007-07-15 02:50:02 +0800293#ifdef CONFIG_PM
294static int state_before_suspend;
295
296/**
297 * bfin_wdt_suspend - suspend the watchdog
298 * @pdev: device being suspended
299 * @state: requested suspend state
300 *
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 ...
304 */
305static int bfin_wdt_suspend(struct platform_device *pdev, pm_message_t state)
306{
307 stampit();
308
309 state_before_suspend = bfin_wdt_running();
310 bfin_wdt_stop();
311
312 return 0;
313}
314
315/**
316 * bfin_wdt_resume - resume the watchdog
317 * @pdev: device being resumed
318 *
319 * If the watchdog was running, turn it back on.
320 */
321static int bfin_wdt_resume(struct platform_device *pdev)
322{
323 stampit();
324
325 if (state_before_suspend) {
326 bfin_wdt_set_timeout(timeout);
327 bfin_wdt_start();
328 }
329
330 return 0;
331}
332#else
333# define bfin_wdt_suspend NULL
334# define bfin_wdt_resume NULL
335#endif
336
Jan Engelhardtb47a1662008-01-22 20:48:10 +0100337static const struct file_operations bfin_wdt_fops = {
Alan Cox9a5f50d2008-05-19 14:06:30 +0100338 .owner = THIS_MODULE,
339 .llseek = no_llseek,
Wim Van Sebroeck5f3b2752011-02-23 20:04:38 +0000340 .write = bfin_wdt_write,
Alan Cox9a5f50d2008-05-19 14:06:30 +0100341 .unlocked_ioctl = bfin_wdt_ioctl,
342 .open = bfin_wdt_open,
343 .release = bfin_wdt_release,
Bryan Wu1e6d3202007-07-15 02:50:02 +0800344};
345
346static struct miscdevice bfin_wdt_miscdev = {
347 .minor = WATCHDOG_MINOR,
348 .name = "watchdog",
349 .fops = &bfin_wdt_fops,
350};
351
Wim Van Sebroeck42747d72009-12-26 18:55:22 +0000352static const struct watchdog_info bfin_wdt_info = {
Bryan Wu1e6d3202007-07-15 02:50:02 +0800353 .identity = "Blackfin Watchdog",
354 .options = WDIOF_SETTIMEOUT |
Alan Cox9a5f50d2008-05-19 14:06:30 +0100355 WDIOF_KEEPALIVEPING |
356 WDIOF_MAGICCLOSE,
Bryan Wu1e6d3202007-07-15 02:50:02 +0800357};
358
Bryan Wu1e6d3202007-07-15 02:50:02 +0800359/**
Mike Frysinger93539b12008-03-27 11:53:32 -0700360 * bfin_wdt_probe - Initialize module
361 *
Wim Van Sebroeck168b5252009-12-26 19:13:00 +0000362 * Registers the misc device. Actual device
Mike Frysinger93539b12008-03-27 11:53:32 -0700363 * initialization is handled by bfin_wdt_open().
364 */
365static int __devinit bfin_wdt_probe(struct platform_device *pdev)
366{
367 int ret;
368
Mike Frysinger93539b12008-03-27 11:53:32 -0700369 ret = misc_register(&bfin_wdt_miscdev);
370 if (ret) {
Alan Cox9a5f50d2008-05-19 14:06:30 +0100371 pr_devinit(KERN_ERR PFX
372 "cannot register miscdev on minor=%d (err=%d)\n",
373 WATCHDOG_MINOR, ret);
Mike Frysinger93539b12008-03-27 11:53:32 -0700374 return ret;
375 }
376
377 pr_devinit(KERN_INFO PFX "initialized: timeout=%d sec (nowayout=%d)\n",
378 timeout, nowayout);
379
380 return 0;
381}
382
383/**
384 * bfin_wdt_remove - Initialize module
385 *
Wim Van Sebroeck168b5252009-12-26 19:13:00 +0000386 * Unregisters the misc device. Actual device
Mike Frysinger93539b12008-03-27 11:53:32 -0700387 * deinitialization is handled by bfin_wdt_close().
388 */
389static int __devexit bfin_wdt_remove(struct platform_device *pdev)
390{
391 misc_deregister(&bfin_wdt_miscdev);
Mike Frysinger93539b12008-03-27 11:53:32 -0700392 return 0;
393}
394
Wim Van Sebroeck168b5252009-12-26 19:13:00 +0000395/**
396 * bfin_wdt_shutdown - Soft Shutdown Handler
397 *
398 * Handles the soft shutdown event.
399 */
400static void bfin_wdt_shutdown(struct platform_device *pdev)
401{
402 stampit();
403
404 bfin_wdt_stop();
405}
406
Mike Frysinger93539b12008-03-27 11:53:32 -0700407static struct platform_device *bfin_wdt_device;
408
409static struct platform_driver bfin_wdt_driver = {
410 .probe = bfin_wdt_probe,
411 .remove = __devexit_p(bfin_wdt_remove),
Wim Van Sebroeck168b5252009-12-26 19:13:00 +0000412 .shutdown = bfin_wdt_shutdown,
Mike Frysinger93539b12008-03-27 11:53:32 -0700413 .suspend = bfin_wdt_suspend,
414 .resume = bfin_wdt_resume,
415 .driver = {
416 .name = WATCHDOG_NAME,
417 .owner = THIS_MODULE,
418 },
419};
420
421/**
Bryan Wu1e6d3202007-07-15 02:50:02 +0800422 * bfin_wdt_init - Initialize module
423 *
Mike Frysinger93539b12008-03-27 11:53:32 -0700424 * Checks the module params and registers the platform device & driver.
425 * Real work is in the platform probe function.
Bryan Wu1e6d3202007-07-15 02:50:02 +0800426 */
427static int __init bfin_wdt_init(void)
428{
429 int ret;
430
431 stampit();
432
433 /* Check that the timeout value is within range */
434 if (bfin_wdt_set_timeout(timeout))
435 return -EINVAL;
436
437 /* Since this is an on-chip device and needs no board-specific
438 * resources, we'll handle all the platform device stuff here.
439 */
Mike Frysinger93539b12008-03-27 11:53:32 -0700440 ret = platform_driver_register(&bfin_wdt_driver);
Bryan Wu1e6d3202007-07-15 02:50:02 +0800441 if (ret) {
Mike Frysinger93539b12008-03-27 11:53:32 -0700442 pr_init(KERN_ERR PFX "unable to register driver\n");
Bryan Wu1e6d3202007-07-15 02:50:02 +0800443 return ret;
444 }
445
Wim Van Sebroecka77dba72009-04-14 20:20:07 +0000446 bfin_wdt_device = platform_device_register_simple(WATCHDOG_NAME,
447 -1, NULL, 0);
Mike Frysinger93539b12008-03-27 11:53:32 -0700448 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);
Bryan Wu1e6d3202007-07-15 02:50:02 +0800452 }
453
Bryan Wu1e6d3202007-07-15 02:50:02 +0800454 return 0;
455}
456
457/**
458 * bfin_wdt_exit - Deinitialize module
459 *
Mike Frysinger93539b12008-03-27 11:53:32 -0700460 * Back out the platform device & driver steps. Real work is in the
461 * platform remove function.
Bryan Wu1e6d3202007-07-15 02:50:02 +0800462 */
463static void __exit bfin_wdt_exit(void)
464{
Mike Frysinger93539b12008-03-27 11:53:32 -0700465 platform_device_unregister(bfin_wdt_device);
466 platform_driver_unregister(&bfin_wdt_driver);
Bryan Wu1e6d3202007-07-15 02:50:02 +0800467}
468
469module_init(bfin_wdt_init);
470module_exit(bfin_wdt_exit);
471
472MODULE_AUTHOR("Michele d'Amico, Mike Frysinger <vapier@gentoo.org>");
473MODULE_DESCRIPTION("Blackfin Watchdog Device Driver");
474MODULE_LICENSE("GPL");
475MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
476
477module_param(timeout, uint, 0);
Alan Cox9a5f50d2008-05-19 14:06:30 +0100478MODULE_PARM_DESC(timeout,
479 "Watchdog timeout in seconds. (1<=timeout<=((2^32)/SCLK), default="
480 __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
Bryan Wu1e6d3202007-07-15 02:50:02 +0800481
482module_param(nowayout, int, 0);
Alan Cox9a5f50d2008-05-19 14:06:30 +0100483MODULE_PARM_DESC(nowayout,
484 "Watchdog cannot be stopped once started (default="
485 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");