blob: 9c7ccd1e9088fbd5f108fe7c96980d7cda07d8e3 [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>
Bryan Wu1e6d3202007-07-15 02:50:02 +080026
Wim Van Sebroecka77dba72009-04-14 20:20:07 +000027#define stamp(fmt, args...) \
28 pr_debug("%s:%i: " fmt "\n", __func__, __LINE__, ## args)
Bryan Wu1e6d3202007-07-15 02:50:02 +080029#define stampit() stamp("here i am")
Wim Van Sebroecka77dba72009-04-14 20:20:07 +000030#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); })
Bryan Wu1e6d3202007-07-15 02:50:02 +080036
37#define WATCHDOG_NAME "bfin-wdt"
38#define PFX WATCHDOG_NAME ": "
39
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.
42 */
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
51
52/* Bit in SWRST that indicates boot caused by watchdog */
53#define SWRST_RESET_WDOG 0x4000
54
55/* Bit in WDOG_CTL that indicates watchdog has expired (WDR0) */
56#define WDOG_EXPIRED 0x8000
57
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
64
65/* Masks for WDEN field in WDOG_CTL register */
66#define WDEN_MASK 0x0FF0
67#define WDEN_ENABLE 0x0000
68#define WDEN_DISABLE 0x0AD0
69
70/* some defaults */
71#define WATCHDOG_TIMEOUT 20
72
73static unsigned int timeout = WATCHDOG_TIMEOUT;
74static int nowayout = WATCHDOG_NOWAYOUT;
Wim Van Sebroeck42747d72009-12-26 18:55:22 +000075static const struct watchdog_info bfin_wdt_info;
Bryan Wu1e6d3202007-07-15 02:50:02 +080076static unsigned long open_check;
77static char expect_close;
Jiri Slabybf6350a2007-11-10 05:58:44 +010078static DEFINE_SPINLOCK(bfin_wdt_spinlock);
Bryan Wu1e6d3202007-07-15 02:50:02 +080079
80/**
81 * bfin_wdt_keepalive - Keep the Userspace Watchdog Alive
82 *
83 * The Userspace watchdog got a KeepAlive: schedule the next timeout.
84 */
85static int bfin_wdt_keepalive(void)
86{
87 stampit();
88 bfin_write_WDOG_STAT(0);
89 return 0;
90}
91
92/**
93 * bfin_wdt_stop - Stop the Watchdog
94 *
95 * Stops the on-chip watchdog.
96 */
97static int bfin_wdt_stop(void)
98{
99 stampit();
100 bfin_write_WDOG_CTL(WDEN_DISABLE);
101 return 0;
102}
103
104/**
105 * bfin_wdt_start - Start the Watchdog
106 *
107 * Starts the on-chip watchdog. Automatically loads WDOG_CNT
108 * into WDOG_STAT for us.
109 */
110static int bfin_wdt_start(void)
111{
112 stampit();
113 bfin_write_WDOG_CTL(WDEN_ENABLE | ICTL_RESET);
114 return 0;
115}
116
117/**
118 * bfin_wdt_running - Check Watchdog status
119 *
120 * See if the watchdog is running.
121 */
122static int bfin_wdt_running(void)
123{
124 stampit();
125 return ((bfin_read_WDOG_CTL() & WDEN_MASK) != WDEN_DISABLE);
126}
127
128/**
129 * bfin_wdt_set_timeout - Set the Userspace Watchdog timeout
130 * @t: new timeout value (in seconds)
131 *
132 * Translate the specified timeout in seconds into System Clock
133 * terms which is what the on-chip Watchdog requires.
134 */
135static int bfin_wdt_set_timeout(unsigned long t)
136{
Mike Frysinger3dae93e2010-02-15 19:32:25 -0500137 u32 cnt, max_t, sclk;
Bryan Wu1e6d3202007-07-15 02:50:02 +0800138 unsigned long flags;
139
Mike Frysinger3dae93e2010-02-15 19:32:25 -0500140 sclk = get_sclk();
141 max_t = -1 / sclk;
142 cnt = t * sclk;
143 stamp("maxtimeout=%us newtimeout=%lus (cnt=%#x)", max_t, t, cnt);
Bryan Wu1e6d3202007-07-15 02:50:02 +0800144
Mike Frysinger3dae93e2010-02-15 19:32:25 -0500145 if (t > max_t) {
Bryan Wu1e6d3202007-07-15 02:50:02 +0800146 printk(KERN_WARNING PFX "timeout value is too large\n");
147 return -EINVAL;
148 }
149
150 spin_lock_irqsave(&bfin_wdt_spinlock, flags);
151 {
152 int run = bfin_wdt_running();
153 bfin_wdt_stop();
154 bfin_write_WDOG_CNT(cnt);
Alan Cox9a5f50d2008-05-19 14:06:30 +0100155 if (run)
156 bfin_wdt_start();
Bryan Wu1e6d3202007-07-15 02:50:02 +0800157 }
158 spin_unlock_irqrestore(&bfin_wdt_spinlock, flags);
159
160 timeout = t;
161
162 return 0;
163}
164
165/**
166 * bfin_wdt_open - Open the Device
167 * @inode: inode of device
168 * @file: file handle of device
169 *
170 * Watchdog device is opened and started.
171 */
172static int bfin_wdt_open(struct inode *inode, struct file *file)
173{
174 stampit();
175
176 if (test_and_set_bit(0, &open_check))
177 return -EBUSY;
178
179 if (nowayout)
180 __module_get(THIS_MODULE);
181
182 bfin_wdt_keepalive();
183 bfin_wdt_start();
184
185 return nonseekable_open(inode, file);
186}
187
188/**
189 * bfin_wdt_close - Close the Device
190 * @inode: inode of device
191 * @file: file handle of device
192 *
193 * Watchdog device is closed and stopped.
194 */
195static int bfin_wdt_release(struct inode *inode, struct file *file)
196{
197 stampit();
198
Alan Cox9a5f50d2008-05-19 14:06:30 +0100199 if (expect_close == 42)
Bryan Wu1e6d3202007-07-15 02:50:02 +0800200 bfin_wdt_stop();
Alan Cox9a5f50d2008-05-19 14:06:30 +0100201 else {
202 printk(KERN_CRIT PFX
203 "Unexpected close, not stopping watchdog!\n");
Bryan Wu1e6d3202007-07-15 02:50:02 +0800204 bfin_wdt_keepalive();
205 }
Bryan Wu1e6d3202007-07-15 02:50:02 +0800206 expect_close = 0;
207 clear_bit(0, &open_check);
Bryan Wu1e6d3202007-07-15 02:50:02 +0800208 return 0;
209}
210
211/**
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
217 *
218 * Pings the watchdog on write.
219 */
220static ssize_t bfin_wdt_write(struct file *file, const char __user *data,
Alan Cox9a5f50d2008-05-19 14:06:30 +0100221 size_t len, loff_t *ppos)
Bryan Wu1e6d3202007-07-15 02:50:02 +0800222{
223 stampit();
224
225 if (len) {
226 if (!nowayout) {
227 size_t i;
228
229 /* In case it was set long ago */
230 expect_close = 0;
231
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;
238 }
239 }
240 bfin_wdt_keepalive();
241 }
242
243 return len;
244}
245
246/**
247 * bfin_wdt_ioctl - Query Device
Bryan Wu1e6d3202007-07-15 02:50:02 +0800248 * @file: file handle of device
249 * @cmd: watchdog command
250 * @arg: argument
251 *
252 * Query basic information from the device or ping it, as outlined by the
253 * watchdog API.
254 */
Alan Cox9a5f50d2008-05-19 14:06:30 +0100255static long bfin_wdt_ioctl(struct file *file,
256 unsigned int cmd, unsigned long arg)
Bryan Wu1e6d3202007-07-15 02:50:02 +0800257{
258 void __user *argp = (void __user *)arg;
259 int __user *p = argp;
260
261 stampit();
262
263 switch (cmd) {
Alan Cox9a5f50d2008-05-19 14:06:30 +0100264 case WDIOC_GETSUPPORT:
265 if (copy_to_user(argp, &bfin_wdt_info, sizeof(bfin_wdt_info)))
266 return -EFAULT;
267 else
Bryan Wu1e6d3202007-07-15 02:50:02 +0800268 return 0;
Alan Cox9a5f50d2008-05-19 14:06:30 +0100269 case WDIOC_GETSTATUS:
270 case WDIOC_GETBOOTSTATUS:
271 return put_user(!!(_bfin_swrst & SWRST_RESET_WDOG), p);
Alan Cox9a5f50d2008-05-19 14:06:30 +0100272 case WDIOC_SETOPTIONS: {
273 unsigned long flags;
274 int options, ret = -EINVAL;
Bryan Wu1e6d3202007-07-15 02:50:02 +0800275
Alan Cox9a5f50d2008-05-19 14:06:30 +0100276 if (get_user(options, p))
277 return -EFAULT;
Bryan Wu1e6d3202007-07-15 02:50:02 +0800278
Alan Cox9a5f50d2008-05-19 14:06:30 +0100279 spin_lock_irqsave(&bfin_wdt_spinlock, flags);
280 if (options & WDIOS_DISABLECARD) {
281 bfin_wdt_stop();
282 ret = 0;
Bryan Wu1e6d3202007-07-15 02:50:02 +0800283 }
Alan Cox9a5f50d2008-05-19 14:06:30 +0100284 if (options & WDIOS_ENABLECARD) {
285 bfin_wdt_start();
286 ret = 0;
Bryan Wu1e6d3202007-07-15 02:50:02 +0800287 }
Alan Cox9a5f50d2008-05-19 14:06:30 +0100288 spin_unlock_irqrestore(&bfin_wdt_spinlock, flags);
289 return ret;
290 }
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000291 case WDIOC_KEEPALIVE:
292 bfin_wdt_keepalive();
293 return 0;
294 case WDIOC_SETTIMEOUT: {
295 int new_timeout;
296
297 if (get_user(new_timeout, p))
298 return -EFAULT;
299 if (bfin_wdt_set_timeout(new_timeout))
300 return -EINVAL;
301 }
302 /* Fall */
303 case WDIOC_GETTIMEOUT:
304 return put_user(timeout, p);
Alan Cox9a5f50d2008-05-19 14:06:30 +0100305 default:
306 return -ENOTTY;
Bryan Wu1e6d3202007-07-15 02:50:02 +0800307 }
308}
309
Bryan Wu1e6d3202007-07-15 02:50:02 +0800310#ifdef CONFIG_PM
311static int state_before_suspend;
312
313/**
314 * bfin_wdt_suspend - suspend the watchdog
315 * @pdev: device being suspended
316 * @state: requested suspend state
317 *
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 ...
321 */
322static int bfin_wdt_suspend(struct platform_device *pdev, pm_message_t state)
323{
324 stampit();
325
326 state_before_suspend = bfin_wdt_running();
327 bfin_wdt_stop();
328
329 return 0;
330}
331
332/**
333 * bfin_wdt_resume - resume the watchdog
334 * @pdev: device being resumed
335 *
336 * If the watchdog was running, turn it back on.
337 */
338static int bfin_wdt_resume(struct platform_device *pdev)
339{
340 stampit();
341
342 if (state_before_suspend) {
343 bfin_wdt_set_timeout(timeout);
344 bfin_wdt_start();
345 }
346
347 return 0;
348}
349#else
350# define bfin_wdt_suspend NULL
351# define bfin_wdt_resume NULL
352#endif
353
Jan Engelhardtb47a1662008-01-22 20:48:10 +0100354static const struct file_operations bfin_wdt_fops = {
Alan Cox9a5f50d2008-05-19 14:06:30 +0100355 .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,
Bryan Wu1e6d3202007-07-15 02:50:02 +0800361};
362
363static struct miscdevice bfin_wdt_miscdev = {
364 .minor = WATCHDOG_MINOR,
365 .name = "watchdog",
366 .fops = &bfin_wdt_fops,
367};
368
Wim Van Sebroeck42747d72009-12-26 18:55:22 +0000369static const struct watchdog_info bfin_wdt_info = {
Bryan Wu1e6d3202007-07-15 02:50:02 +0800370 .identity = "Blackfin Watchdog",
371 .options = WDIOF_SETTIMEOUT |
Alan Cox9a5f50d2008-05-19 14:06:30 +0100372 WDIOF_KEEPALIVEPING |
373 WDIOF_MAGICCLOSE,
Bryan Wu1e6d3202007-07-15 02:50:02 +0800374};
375
Bryan Wu1e6d3202007-07-15 02:50:02 +0800376/**
Mike Frysinger93539b12008-03-27 11:53:32 -0700377 * bfin_wdt_probe - Initialize module
378 *
Wim Van Sebroeck168b5252009-12-26 19:13:00 +0000379 * Registers the misc device. Actual device
Mike Frysinger93539b12008-03-27 11:53:32 -0700380 * initialization is handled by bfin_wdt_open().
381 */
382static int __devinit bfin_wdt_probe(struct platform_device *pdev)
383{
384 int ret;
385
Mike Frysinger93539b12008-03-27 11:53:32 -0700386 ret = misc_register(&bfin_wdt_miscdev);
387 if (ret) {
Alan Cox9a5f50d2008-05-19 14:06:30 +0100388 pr_devinit(KERN_ERR PFX
389 "cannot register miscdev on minor=%d (err=%d)\n",
390 WATCHDOG_MINOR, ret);
Mike Frysinger93539b12008-03-27 11:53:32 -0700391 return ret;
392 }
393
394 pr_devinit(KERN_INFO PFX "initialized: timeout=%d sec (nowayout=%d)\n",
395 timeout, nowayout);
396
397 return 0;
398}
399
400/**
401 * bfin_wdt_remove - Initialize module
402 *
Wim Van Sebroeck168b5252009-12-26 19:13:00 +0000403 * Unregisters the misc device. Actual device
Mike Frysinger93539b12008-03-27 11:53:32 -0700404 * deinitialization is handled by bfin_wdt_close().
405 */
406static int __devexit bfin_wdt_remove(struct platform_device *pdev)
407{
408 misc_deregister(&bfin_wdt_miscdev);
Mike Frysinger93539b12008-03-27 11:53:32 -0700409 return 0;
410}
411
Wim Van Sebroeck168b5252009-12-26 19:13:00 +0000412/**
413 * bfin_wdt_shutdown - Soft Shutdown Handler
414 *
415 * Handles the soft shutdown event.
416 */
417static void bfin_wdt_shutdown(struct platform_device *pdev)
418{
419 stampit();
420
421 bfin_wdt_stop();
422}
423
Mike Frysinger93539b12008-03-27 11:53:32 -0700424static struct platform_device *bfin_wdt_device;
425
426static struct platform_driver bfin_wdt_driver = {
427 .probe = bfin_wdt_probe,
428 .remove = __devexit_p(bfin_wdt_remove),
Wim Van Sebroeck168b5252009-12-26 19:13:00 +0000429 .shutdown = bfin_wdt_shutdown,
Mike Frysinger93539b12008-03-27 11:53:32 -0700430 .suspend = bfin_wdt_suspend,
431 .resume = bfin_wdt_resume,
432 .driver = {
433 .name = WATCHDOG_NAME,
434 .owner = THIS_MODULE,
435 },
436};
437
438/**
Bryan Wu1e6d3202007-07-15 02:50:02 +0800439 * bfin_wdt_init - Initialize module
440 *
Mike Frysinger93539b12008-03-27 11:53:32 -0700441 * Checks the module params and registers the platform device & driver.
442 * Real work is in the platform probe function.
Bryan Wu1e6d3202007-07-15 02:50:02 +0800443 */
444static int __init bfin_wdt_init(void)
445{
446 int ret;
447
448 stampit();
449
450 /* Check that the timeout value is within range */
451 if (bfin_wdt_set_timeout(timeout))
452 return -EINVAL;
453
454 /* Since this is an on-chip device and needs no board-specific
455 * resources, we'll handle all the platform device stuff here.
456 */
Mike Frysinger93539b12008-03-27 11:53:32 -0700457 ret = platform_driver_register(&bfin_wdt_driver);
Bryan Wu1e6d3202007-07-15 02:50:02 +0800458 if (ret) {
Mike Frysinger93539b12008-03-27 11:53:32 -0700459 pr_init(KERN_ERR PFX "unable to register driver\n");
Bryan Wu1e6d3202007-07-15 02:50:02 +0800460 return ret;
461 }
462
Wim Van Sebroecka77dba72009-04-14 20:20:07 +0000463 bfin_wdt_device = platform_device_register_simple(WATCHDOG_NAME,
464 -1, NULL, 0);
Mike Frysinger93539b12008-03-27 11:53:32 -0700465 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);
Bryan Wu1e6d3202007-07-15 02:50:02 +0800469 }
470
Bryan Wu1e6d3202007-07-15 02:50:02 +0800471 return 0;
472}
473
474/**
475 * bfin_wdt_exit - Deinitialize module
476 *
Mike Frysinger93539b12008-03-27 11:53:32 -0700477 * Back out the platform device & driver steps. Real work is in the
478 * platform remove function.
Bryan Wu1e6d3202007-07-15 02:50:02 +0800479 */
480static void __exit bfin_wdt_exit(void)
481{
Mike Frysinger93539b12008-03-27 11:53:32 -0700482 platform_device_unregister(bfin_wdt_device);
483 platform_driver_unregister(&bfin_wdt_driver);
Bryan Wu1e6d3202007-07-15 02:50:02 +0800484}
485
486module_init(bfin_wdt_init);
487module_exit(bfin_wdt_exit);
488
489MODULE_AUTHOR("Michele d'Amico, Mike Frysinger <vapier@gentoo.org>");
490MODULE_DESCRIPTION("Blackfin Watchdog Device Driver");
491MODULE_LICENSE("GPL");
492MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
493
494module_param(timeout, uint, 0);
Alan Cox9a5f50d2008-05-19 14:06:30 +0100495MODULE_PARM_DESC(timeout,
496 "Watchdog timeout in seconds. (1<=timeout<=((2^32)/SCLK), default="
497 __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
Bryan Wu1e6d3202007-07-15 02:50:02 +0800498
499module_param(nowayout, int, 0);
Alan Cox9a5f50d2008-05-19 14:06:30 +0100500MODULE_PARM_DESC(nowayout,
501 "Watchdog cannot be stopped once started (default="
502 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");