]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/watchdog/bfin_wdt.c
[WATCHDOG] change reboot_notifier to platform-shutdown method.
[net-next-2.6.git] / drivers / watchdog / bfin_wdt.c
CommitLineData
1e6d320f
BW
1/*
2 * Blackfin On-Chip Watchdog Driver
1e6d320f
BW
3 *
4 * Originally based on softdog.c
3dae93ec 5 * Copyright 2006-2010 Analog Devices Inc.
1e6d320f 6 * Copyright 2006-2007 Michele d'Amico
29fa0586 7 * Copyright 1996 Alan Cox <alan@lxorguk.ukuu.org.uk>
1e6d320f
BW
8 *
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>
1e6d320f
BW
22#include <linux/init.h>
23#include <linux/interrupt.h>
9a5f50d3 24#include <linux/uaccess.h>
089ab079 25#include <asm/blackfin.h>
1e6d320f 26
a77dba7e
WVS
27#define stamp(fmt, args...) \
28 pr_debug("%s:%i: " fmt "\n", __func__, __LINE__, ## args)
1e6d320f 29#define stampit() stamp("here i am")
a77dba7e
WVS
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); })
1e6d320f
BW
36
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;
42747d71 75static const struct watchdog_info bfin_wdt_info;
1e6d320f
BW
76static unsigned long open_check;
77static char expect_close;
bf6350a3 78static DEFINE_SPINLOCK(bfin_wdt_spinlock);
1e6d320f
BW
79
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{
3dae93ec 137 u32 cnt, max_t, sclk;
1e6d320f
BW
138 unsigned long flags;
139
3dae93ec
MF
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);
1e6d320f 144
3dae93ec 145 if (t > max_t) {
1e6d320f
BW
146 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);
9a5f50d3
AC
155 if (run)
156 bfin_wdt_start();
1e6d320f
BW
157 }
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
9a5f50d3 199 if (expect_close == 42)
1e6d320f 200 bfin_wdt_stop();
9a5f50d3
AC
201 else {
202 printk(KERN_CRIT PFX
203 "Unexpected close, not stopping watchdog!\n");
1e6d320f
BW
204 bfin_wdt_keepalive();
205 }
1e6d320f
BW
206 expect_close = 0;
207 clear_bit(0, &open_check);
1e6d320f
BW
208 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,
9a5f50d3 221 size_t len, loff_t *ppos)
1e6d320f
BW
222{
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
1e6d320f
BW
248 * @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 */
9a5f50d3
AC
255static long bfin_wdt_ioctl(struct file *file,
256 unsigned int cmd, unsigned long arg)
1e6d320f
BW
257{
258 void __user *argp = (void __user *)arg;
259 int __user *p = argp;
260
261 stampit();
262
263 switch (cmd) {
9a5f50d3
AC
264 case WDIOC_GETSUPPORT:
265 if (copy_to_user(argp, &bfin_wdt_info, sizeof(bfin_wdt_info)))
266 return -EFAULT;
267 else
1e6d320f 268 return 0;
9a5f50d3
AC
269 case WDIOC_GETSTATUS:
270 case WDIOC_GETBOOTSTATUS:
271 return put_user(!!(_bfin_swrst & SWRST_RESET_WDOG), p);
9a5f50d3
AC
272 case WDIOC_SETOPTIONS: {
273 unsigned long flags;
274 int options, ret = -EINVAL;
275
276 if (get_user(options, p))
277 return -EFAULT;
278
279 spin_lock_irqsave(&bfin_wdt_spinlock, flags);
280 if (options & WDIOS_DISABLECARD) {
281 bfin_wdt_stop();
282 ret = 0;
1e6d320f 283 }
9a5f50d3
AC
284 if (options & WDIOS_ENABLECARD) {
285 bfin_wdt_start();
286 ret = 0;
1e6d320f 287 }
9a5f50d3
AC
288 spin_unlock_irqrestore(&bfin_wdt_spinlock, flags);
289 return ret;
290 }
0c06090c
WVS
291 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);
9a5f50d3
AC
305 default:
306 return -ENOTTY;
1e6d320f
BW
307 }
308}
309
1e6d320f
BW
310#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
b47a166e 354static const struct file_operations bfin_wdt_fops = {
9a5f50d3
AC
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,
1e6d320f
BW
361};
362
363static struct miscdevice bfin_wdt_miscdev = {
364 .minor = WATCHDOG_MINOR,
365 .name = "watchdog",
366 .fops = &bfin_wdt_fops,
367};
368
42747d71 369static const struct watchdog_info bfin_wdt_info = {
1e6d320f
BW
370 .identity = "Blackfin Watchdog",
371 .options = WDIOF_SETTIMEOUT |
9a5f50d3
AC
372 WDIOF_KEEPALIVEPING |
373 WDIOF_MAGICCLOSE,
1e6d320f
BW
374};
375
1e6d320f 376/**
93539b19 377 * bfin_wdt_probe - Initialize module
1e6d320f 378 *
168b5251 379 * Registers the misc device. Actual device
1e6d320f
BW
380 * initialization is handled by bfin_wdt_open().
381 */
93539b19
MF
382static int __devinit bfin_wdt_probe(struct platform_device *pdev)
383{
384 int ret;
385
93539b19
MF
386 ret = misc_register(&bfin_wdt_miscdev);
387 if (ret) {
9a5f50d3
AC
388 pr_devinit(KERN_ERR PFX
389 "cannot register miscdev on minor=%d (err=%d)\n",
390 WATCHDOG_MINOR, ret);
93539b19
MF
391 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 *
168b5251 403 * Unregisters the misc device. Actual device
93539b19
MF
404 * 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);
93539b19
MF
409 return 0;
410}
411
168b5251
WVS
412/**
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
93539b19
MF
424static 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),
168b5251 429 .shutdown = bfin_wdt_shutdown,
93539b19
MF
430 .suspend = bfin_wdt_suspend,
431 .resume = bfin_wdt_resume,
432 .driver = {
433 .name = WATCHDOG_NAME,
434 .owner = THIS_MODULE,
435 },
436};
437
438/**
439 * bfin_wdt_init - Initialize module
440 *
441 * Checks the module params and registers the platform device & driver.
442 * Real work is in the platform probe function.
443 */
1e6d320f
BW
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 */
93539b19 457 ret = platform_driver_register(&bfin_wdt_driver);
1e6d320f 458 if (ret) {
93539b19 459 pr_init(KERN_ERR PFX "unable to register driver\n");
1e6d320f
BW
460 return ret;
461 }
462
a77dba7e
WVS
463 bfin_wdt_device = platform_device_register_simple(WATCHDOG_NAME,
464 -1, NULL, 0);
93539b19
MF
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);
1e6d320f
BW
469 }
470
1e6d320f
BW
471 return 0;
472}
473
474/**
475 * bfin_wdt_exit - Deinitialize module
476 *
93539b19
MF
477 * Back out the platform device & driver steps. Real work is in the
478 * platform remove function.
1e6d320f
BW
479 */
480static void __exit bfin_wdt_exit(void)
481{
93539b19
MF
482 platform_device_unregister(bfin_wdt_device);
483 platform_driver_unregister(&bfin_wdt_driver);
1e6d320f
BW
484}
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);
9a5f50d3
AC
495MODULE_PARM_DESC(timeout,
496 "Watchdog timeout in seconds. (1<=timeout<=((2^32)/SCLK), default="
497 __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
1e6d320f
BW
498
499module_param(nowayout, int, 0);
9a5f50d3
AC
500MODULE_PARM_DESC(nowayout,
501 "Watchdog cannot be stopped once started (default="
502 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");