blob: 2159e668751cd7db5e9ddfb39c96984573801539 [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>
22#include <linux/notifier.h>
23#include <linux/reboot.h>
24#include <linux/init.h>
25#include <linux/interrupt.h>
Alan Cox9a5f50d2008-05-19 14:06:30 +010026#include <linux/uaccess.h>
Wim Van Sebroeck089ab072008-07-15 11:46:11 +000027#include <asm/blackfin.h>
Bryan Wu1e6d3202007-07-15 02:50:02 +080028
Wim Van Sebroecka77dba72009-04-14 20:20:07 +000029#define stamp(fmt, args...) \
30 pr_debug("%s:%i: " fmt "\n", __func__, __LINE__, ## args)
Bryan Wu1e6d3202007-07-15 02:50:02 +080031#define stampit() stamp("here i am")
Wim Van Sebroecka77dba72009-04-14 20:20:07 +000032#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); })
Bryan Wu1e6d3202007-07-15 02:50:02 +080038
39#define WATCHDOG_NAME "bfin-wdt"
40#define PFX WATCHDOG_NAME ": "
41
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.
44 */
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
53
54/* Bit in SWRST that indicates boot caused by watchdog */
55#define SWRST_RESET_WDOG 0x4000
56
57/* Bit in WDOG_CTL that indicates watchdog has expired (WDR0) */
58#define WDOG_EXPIRED 0x8000
59
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
66
67/* Masks for WDEN field in WDOG_CTL register */
68#define WDEN_MASK 0x0FF0
69#define WDEN_ENABLE 0x0000
70#define WDEN_DISABLE 0x0AD0
71
72/* some defaults */
73#define WATCHDOG_TIMEOUT 20
74
75static unsigned int timeout = WATCHDOG_TIMEOUT;
76static int nowayout = WATCHDOG_NOWAYOUT;
77static struct watchdog_info bfin_wdt_info;
78static unsigned long open_check;
79static char expect_close;
Jiri Slabybf6350a2007-11-10 05:58:44 +010080static DEFINE_SPINLOCK(bfin_wdt_spinlock);
Bryan Wu1e6d3202007-07-15 02:50:02 +080081
82/**
83 * bfin_wdt_keepalive - Keep the Userspace Watchdog Alive
84 *
85 * The Userspace watchdog got a KeepAlive: schedule the next timeout.
86 */
87static int bfin_wdt_keepalive(void)
88{
89 stampit();
90 bfin_write_WDOG_STAT(0);
91 return 0;
92}
93
94/**
95 * bfin_wdt_stop - Stop the Watchdog
96 *
97 * Stops the on-chip watchdog.
98 */
99static int bfin_wdt_stop(void)
100{
101 stampit();
102 bfin_write_WDOG_CTL(WDEN_DISABLE);
103 return 0;
104}
105
106/**
107 * bfin_wdt_start - Start the Watchdog
108 *
109 * Starts the on-chip watchdog. Automatically loads WDOG_CNT
110 * into WDOG_STAT for us.
111 */
112static int bfin_wdt_start(void)
113{
114 stampit();
115 bfin_write_WDOG_CTL(WDEN_ENABLE | ICTL_RESET);
116 return 0;
117}
118
119/**
120 * bfin_wdt_running - Check Watchdog status
121 *
122 * See if the watchdog is running.
123 */
124static int bfin_wdt_running(void)
125{
126 stampit();
127 return ((bfin_read_WDOG_CTL() & WDEN_MASK) != WDEN_DISABLE);
128}
129
130/**
131 * bfin_wdt_set_timeout - Set the Userspace Watchdog timeout
132 * @t: new timeout value (in seconds)
133 *
134 * Translate the specified timeout in seconds into System Clock
135 * terms which is what the on-chip Watchdog requires.
136 */
137static int bfin_wdt_set_timeout(unsigned long t)
138{
Mike Frysinger3dae93e2010-02-15 19:32:25 -0500139 u32 cnt, max_t, sclk;
Bryan Wu1e6d3202007-07-15 02:50:02 +0800140 unsigned long flags;
141
Mike Frysinger3dae93e2010-02-15 19:32:25 -0500142 sclk = get_sclk();
143 max_t = -1 / sclk;
144 cnt = t * sclk;
145 stamp("maxtimeout=%us newtimeout=%lus (cnt=%#x)", max_t, t, cnt);
Bryan Wu1e6d3202007-07-15 02:50:02 +0800146
Mike Frysinger3dae93e2010-02-15 19:32:25 -0500147 if (t > max_t) {
Bryan Wu1e6d3202007-07-15 02:50:02 +0800148 printk(KERN_WARNING PFX "timeout value is too large\n");
149 return -EINVAL;
150 }
151
152 spin_lock_irqsave(&bfin_wdt_spinlock, flags);
153 {
154 int run = bfin_wdt_running();
155 bfin_wdt_stop();
156 bfin_write_WDOG_CNT(cnt);
Alan Cox9a5f50d2008-05-19 14:06:30 +0100157 if (run)
158 bfin_wdt_start();
Bryan Wu1e6d3202007-07-15 02:50:02 +0800159 }
160 spin_unlock_irqrestore(&bfin_wdt_spinlock, flags);
161
162 timeout = t;
163
164 return 0;
165}
166
167/**
168 * bfin_wdt_open - Open the Device
169 * @inode: inode of device
170 * @file: file handle of device
171 *
172 * Watchdog device is opened and started.
173 */
174static int bfin_wdt_open(struct inode *inode, struct file *file)
175{
176 stampit();
177
178 if (test_and_set_bit(0, &open_check))
179 return -EBUSY;
180
181 if (nowayout)
182 __module_get(THIS_MODULE);
183
184 bfin_wdt_keepalive();
185 bfin_wdt_start();
186
187 return nonseekable_open(inode, file);
188}
189
190/**
191 * bfin_wdt_close - Close the Device
192 * @inode: inode of device
193 * @file: file handle of device
194 *
195 * Watchdog device is closed and stopped.
196 */
197static int bfin_wdt_release(struct inode *inode, struct file *file)
198{
199 stampit();
200
Alan Cox9a5f50d2008-05-19 14:06:30 +0100201 if (expect_close == 42)
Bryan Wu1e6d3202007-07-15 02:50:02 +0800202 bfin_wdt_stop();
Alan Cox9a5f50d2008-05-19 14:06:30 +0100203 else {
204 printk(KERN_CRIT PFX
205 "Unexpected close, not stopping watchdog!\n");
Bryan Wu1e6d3202007-07-15 02:50:02 +0800206 bfin_wdt_keepalive();
207 }
Bryan Wu1e6d3202007-07-15 02:50:02 +0800208 expect_close = 0;
209 clear_bit(0, &open_check);
Bryan Wu1e6d3202007-07-15 02:50:02 +0800210 return 0;
211}
212
213/**
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
219 *
220 * Pings the watchdog on write.
221 */
222static ssize_t bfin_wdt_write(struct file *file, const char __user *data,
Alan Cox9a5f50d2008-05-19 14:06:30 +0100223 size_t len, loff_t *ppos)
Bryan Wu1e6d3202007-07-15 02:50:02 +0800224{
225 stampit();
226
227 if (len) {
228 if (!nowayout) {
229 size_t i;
230
231 /* In case it was set long ago */
232 expect_close = 0;
233
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;
240 }
241 }
242 bfin_wdt_keepalive();
243 }
244
245 return len;
246}
247
248/**
249 * bfin_wdt_ioctl - Query Device
Bryan Wu1e6d3202007-07-15 02:50:02 +0800250 * @file: file handle of device
251 * @cmd: watchdog command
252 * @arg: argument
253 *
254 * Query basic information from the device or ping it, as outlined by the
255 * watchdog API.
256 */
Alan Cox9a5f50d2008-05-19 14:06:30 +0100257static long bfin_wdt_ioctl(struct file *file,
258 unsigned int cmd, unsigned long arg)
Bryan Wu1e6d3202007-07-15 02:50:02 +0800259{
260 void __user *argp = (void __user *)arg;
261 int __user *p = argp;
262
263 stampit();
264
265 switch (cmd) {
Alan Cox9a5f50d2008-05-19 14:06:30 +0100266 case WDIOC_GETSUPPORT:
267 if (copy_to_user(argp, &bfin_wdt_info, sizeof(bfin_wdt_info)))
268 return -EFAULT;
269 else
Bryan Wu1e6d3202007-07-15 02:50:02 +0800270 return 0;
Alan Cox9a5f50d2008-05-19 14:06:30 +0100271 case WDIOC_GETSTATUS:
272 case WDIOC_GETBOOTSTATUS:
273 return put_user(!!(_bfin_swrst & SWRST_RESET_WDOG), p);
Alan Cox9a5f50d2008-05-19 14:06:30 +0100274 case WDIOC_SETOPTIONS: {
275 unsigned long flags;
276 int options, ret = -EINVAL;
Bryan Wu1e6d3202007-07-15 02:50:02 +0800277
Alan Cox9a5f50d2008-05-19 14:06:30 +0100278 if (get_user(options, p))
279 return -EFAULT;
Bryan Wu1e6d3202007-07-15 02:50:02 +0800280
Alan Cox9a5f50d2008-05-19 14:06:30 +0100281 spin_lock_irqsave(&bfin_wdt_spinlock, flags);
282 if (options & WDIOS_DISABLECARD) {
283 bfin_wdt_stop();
284 ret = 0;
Bryan Wu1e6d3202007-07-15 02:50:02 +0800285 }
Alan Cox9a5f50d2008-05-19 14:06:30 +0100286 if (options & WDIOS_ENABLECARD) {
287 bfin_wdt_start();
288 ret = 0;
Bryan Wu1e6d3202007-07-15 02:50:02 +0800289 }
Alan Cox9a5f50d2008-05-19 14:06:30 +0100290 spin_unlock_irqrestore(&bfin_wdt_spinlock, flags);
291 return ret;
292 }
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000293 case WDIOC_KEEPALIVE:
294 bfin_wdt_keepalive();
295 return 0;
296 case WDIOC_SETTIMEOUT: {
297 int new_timeout;
298
299 if (get_user(new_timeout, p))
300 return -EFAULT;
301 if (bfin_wdt_set_timeout(new_timeout))
302 return -EINVAL;
303 }
304 /* Fall */
305 case WDIOC_GETTIMEOUT:
306 return put_user(timeout, p);
Alan Cox9a5f50d2008-05-19 14:06:30 +0100307 default:
308 return -ENOTTY;
Bryan Wu1e6d3202007-07-15 02:50:02 +0800309 }
310}
311
312/**
313 * bfin_wdt_notify_sys - Notifier Handler
314 * @this: notifier block
315 * @code: notifier event
316 * @unused: unused
317 *
318 * Handles specific events, such as turning off the watchdog during a
319 * shutdown event.
320 */
Alan Cox9a5f50d2008-05-19 14:06:30 +0100321static int bfin_wdt_notify_sys(struct notifier_block *this,
322 unsigned long code, void *unused)
Bryan Wu1e6d3202007-07-15 02:50:02 +0800323{
324 stampit();
325
326 if (code == SYS_DOWN || code == SYS_HALT)
327 bfin_wdt_stop();
328
329 return NOTIFY_DONE;
330}
331
332#ifdef CONFIG_PM
333static int state_before_suspend;
334
335/**
336 * bfin_wdt_suspend - suspend the watchdog
337 * @pdev: device being suspended
338 * @state: requested suspend state
339 *
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 ...
343 */
344static int bfin_wdt_suspend(struct platform_device *pdev, pm_message_t state)
345{
346 stampit();
347
348 state_before_suspend = bfin_wdt_running();
349 bfin_wdt_stop();
350
351 return 0;
352}
353
354/**
355 * bfin_wdt_resume - resume the watchdog
356 * @pdev: device being resumed
357 *
358 * If the watchdog was running, turn it back on.
359 */
360static int bfin_wdt_resume(struct platform_device *pdev)
361{
362 stampit();
363
364 if (state_before_suspend) {
365 bfin_wdt_set_timeout(timeout);
366 bfin_wdt_start();
367 }
368
369 return 0;
370}
371#else
372# define bfin_wdt_suspend NULL
373# define bfin_wdt_resume NULL
374#endif
375
Jan Engelhardtb47a1662008-01-22 20:48:10 +0100376static const struct file_operations bfin_wdt_fops = {
Alan Cox9a5f50d2008-05-19 14:06:30 +0100377 .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,
Bryan Wu1e6d3202007-07-15 02:50:02 +0800383};
384
385static struct miscdevice bfin_wdt_miscdev = {
386 .minor = WATCHDOG_MINOR,
387 .name = "watchdog",
388 .fops = &bfin_wdt_fops,
389};
390
391static struct watchdog_info bfin_wdt_info = {
392 .identity = "Blackfin Watchdog",
393 .options = WDIOF_SETTIMEOUT |
Alan Cox9a5f50d2008-05-19 14:06:30 +0100394 WDIOF_KEEPALIVEPING |
395 WDIOF_MAGICCLOSE,
Bryan Wu1e6d3202007-07-15 02:50:02 +0800396};
397
398static struct notifier_block bfin_wdt_notifier = {
399 .notifier_call = bfin_wdt_notify_sys,
400};
401
402/**
Mike Frysinger93539b12008-03-27 11:53:32 -0700403 * bfin_wdt_probe - Initialize module
404 *
405 * Registers the misc device and notifier handler. Actual device
406 * initialization is handled by bfin_wdt_open().
407 */
408static int __devinit bfin_wdt_probe(struct platform_device *pdev)
409{
410 int ret;
411
412 ret = register_reboot_notifier(&bfin_wdt_notifier);
413 if (ret) {
Alan Cox9a5f50d2008-05-19 14:06:30 +0100414 pr_devinit(KERN_ERR PFX
415 "cannot register reboot notifier (err=%d)\n", ret);
Mike Frysinger93539b12008-03-27 11:53:32 -0700416 return ret;
417 }
418
419 ret = misc_register(&bfin_wdt_miscdev);
420 if (ret) {
Alan Cox9a5f50d2008-05-19 14:06:30 +0100421 pr_devinit(KERN_ERR PFX
422 "cannot register miscdev on minor=%d (err=%d)\n",
423 WATCHDOG_MINOR, ret);
Mike Frysinger93539b12008-03-27 11:53:32 -0700424 unregister_reboot_notifier(&bfin_wdt_notifier);
425 return ret;
426 }
427
428 pr_devinit(KERN_INFO PFX "initialized: timeout=%d sec (nowayout=%d)\n",
429 timeout, nowayout);
430
431 return 0;
432}
433
434/**
435 * bfin_wdt_remove - Initialize module
436 *
437 * Unregisters the misc device and notifier handler. Actual device
438 * deinitialization is handled by bfin_wdt_close().
439 */
440static int __devexit bfin_wdt_remove(struct platform_device *pdev)
441{
442 misc_deregister(&bfin_wdt_miscdev);
443 unregister_reboot_notifier(&bfin_wdt_notifier);
444 return 0;
445}
446
447static struct platform_device *bfin_wdt_device;
448
449static 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,
457 },
458};
459
460/**
Bryan Wu1e6d3202007-07-15 02:50:02 +0800461 * bfin_wdt_init - Initialize module
462 *
Mike Frysinger93539b12008-03-27 11:53:32 -0700463 * Checks the module params and registers the platform device & driver.
464 * Real work is in the platform probe function.
Bryan Wu1e6d3202007-07-15 02:50:02 +0800465 */
466static int __init bfin_wdt_init(void)
467{
468 int ret;
469
470 stampit();
471
472 /* Check that the timeout value is within range */
473 if (bfin_wdt_set_timeout(timeout))
474 return -EINVAL;
475
476 /* Since this is an on-chip device and needs no board-specific
477 * resources, we'll handle all the platform device stuff here.
478 */
Mike Frysinger93539b12008-03-27 11:53:32 -0700479 ret = platform_driver_register(&bfin_wdt_driver);
Bryan Wu1e6d3202007-07-15 02:50:02 +0800480 if (ret) {
Mike Frysinger93539b12008-03-27 11:53:32 -0700481 pr_init(KERN_ERR PFX "unable to register driver\n");
Bryan Wu1e6d3202007-07-15 02:50:02 +0800482 return ret;
483 }
484
Wim Van Sebroecka77dba72009-04-14 20:20:07 +0000485 bfin_wdt_device = platform_device_register_simple(WATCHDOG_NAME,
486 -1, NULL, 0);
Mike Frysinger93539b12008-03-27 11:53:32 -0700487 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);
Bryan Wu1e6d3202007-07-15 02:50:02 +0800491 }
492
Bryan Wu1e6d3202007-07-15 02:50:02 +0800493 return 0;
494}
495
496/**
497 * bfin_wdt_exit - Deinitialize module
498 *
Mike Frysinger93539b12008-03-27 11:53:32 -0700499 * Back out the platform device & driver steps. Real work is in the
500 * platform remove function.
Bryan Wu1e6d3202007-07-15 02:50:02 +0800501 */
502static void __exit bfin_wdt_exit(void)
503{
Mike Frysinger93539b12008-03-27 11:53:32 -0700504 platform_device_unregister(bfin_wdt_device);
505 platform_driver_unregister(&bfin_wdt_driver);
Bryan Wu1e6d3202007-07-15 02:50:02 +0800506}
507
508module_init(bfin_wdt_init);
509module_exit(bfin_wdt_exit);
510
511MODULE_AUTHOR("Michele d'Amico, Mike Frysinger <vapier@gentoo.org>");
512MODULE_DESCRIPTION("Blackfin Watchdog Device Driver");
513MODULE_LICENSE("GPL");
514MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
515
516module_param(timeout, uint, 0);
Alan Cox9a5f50d2008-05-19 14:06:30 +0100517MODULE_PARM_DESC(timeout,
518 "Watchdog timeout in seconds. (1<=timeout<=((2^32)/SCLK), default="
519 __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
Bryan Wu1e6d3202007-07-15 02:50:02 +0800520
521module_param(nowayout, int, 0);
Alan Cox9a5f50d2008-05-19 14:06:30 +0100522MODULE_PARM_DESC(nowayout,
523 "Watchdog cannot be stopped once started (default="
524 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");