[PATCH] Fix SAK_work workqueue initialization.
[pandora-kernel.git] / drivers / char / watchdog / sc520_wdt.c
1 /*
2  *      AMD Elan SC520 processor Watchdog Timer driver
3  *
4  *      Based on acquirewdt.c by Alan Cox,
5  *           and sbc60xxwdt.c by Jakob Oestergaard <jakob@unthought.net>
6  *
7  *      This program is free software; you can redistribute it and/or
8  *      modify it under the terms of the GNU General Public License
9  *      as published by the Free Software Foundation; either version
10  *      2 of the License, or (at your option) any later version.
11  *
12  *      The authors do NOT admit liability nor provide warranty for
13  *      any of this software. This material is provided "AS-IS" in
14  *      the hope that it may be useful for others.
15  *
16  *      (c) Copyright 2001    Scott Jennings <linuxdrivers@oro.net>
17  *           9/27 - 2001      [Initial release]
18  *
19  *      Additional fixes Alan Cox
20  *      -       Fixed formatting
21  *      -       Removed debug printks
22  *      -       Fixed SMP built kernel deadlock
23  *      -       Switched to private locks not lock_kernel
24  *      -       Used ioremap/writew/readw
25  *      -       Added NOWAYOUT support
26  *      4/12 - 2002 Changes by Rob Radez <rob@osinvestor.com>
27  *      -       Change comments
28  *      -       Eliminate fop_llseek
29  *      -       Change CONFIG_WATCHDOG_NOWAYOUT semantics
30  *      -       Add KERN_* tags to printks
31  *      -       fix possible wdt_is_open race
32  *      -       Report proper capabilities in watchdog_info
33  *      -       Add WDIOC_{GETSTATUS, GETBOOTSTATUS, SETTIMEOUT,
34  *              GETTIMEOUT, SETOPTIONS} ioctls
35  *      09/8 - 2003 Changes by Wim Van Sebroeck <wim@iguana.be>
36  *      -       cleanup of trailing spaces
37  *      -       added extra printk's for startup problems
38  *      -       use module_param
39  *      -       made timeout (the emulated heartbeat) a module_param
40  *      -       made the keepalive ping an internal subroutine
41  *      3/27 - 2004 Changes by Sean Young <sean@mess.org>
42  *      -       set MMCR_BASE to 0xfffef000
43  *      -       CBAR does not need to be read
44  *      -       removed debugging printks
45  *
46  *  This WDT driver is different from most other Linux WDT
47  *  drivers in that the driver will ping the watchdog by itself,
48  *  because this particular WDT has a very short timeout (1.6
49  *  seconds) and it would be insane to count on any userspace
50  *  daemon always getting scheduled within that time frame.
51  *
52  *  This driver uses memory mapped IO, and spinlock.
53  */
54
55 #include <linux/module.h>
56 #include <linux/moduleparam.h>
57 #include <linux/types.h>
58 #include <linux/timer.h>
59 #include <linux/miscdevice.h>
60 #include <linux/watchdog.h>
61 #include <linux/fs.h>
62 #include <linux/ioport.h>
63 #include <linux/notifier.h>
64 #include <linux/reboot.h>
65 #include <linux/init.h>
66 #include <linux/jiffies.h>
67
68 #include <asm/io.h>
69 #include <asm/uaccess.h>
70 #include <asm/system.h>
71
72 #define OUR_NAME "sc520_wdt"
73 #define PFX OUR_NAME ": "
74
75 /*
76  * The AMD Elan SC520 timeout value is 492us times a power of 2 (0-7)
77  *
78  *   0: 492us    2: 1.01s    4: 4.03s   6: 16.22s
79  *   1: 503ms    3: 2.01s    5: 8.05s   7: 32.21s
80  *
81  * We will program the SC520 watchdog for a timeout of 2.01s.
82  * If we reset the watchdog every ~250ms we should be safe.
83  */
84
85 #define WDT_INTERVAL (HZ/4+1)
86
87 /*
88  * We must not require too good response from the userspace daemon.
89  * Here we require the userspace daemon to send us a heartbeat
90  * char to /dev/watchdog every 30 seconds.
91  */
92
93 #define WATCHDOG_TIMEOUT 30             /* 30 sec default timeout */
94 static int timeout = WATCHDOG_TIMEOUT;  /* in seconds, will be multiplied by HZ to get seconds to wait for a ping */
95 module_param(timeout, int, 0);
96 MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds. (1<=timeout<=3600, default=" __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
97
98 static int nowayout = WATCHDOG_NOWAYOUT;
99 module_param(nowayout, int, 0);
100 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)");
101
102 /*
103  * AMD Elan SC520 - Watchdog Timer Registers
104  */
105 #define MMCR_BASE       0xfffef000      /* The default base address */
106 #define OFFS_WDTMRCTL   0xCB0   /* Watchdog Timer Control Register */
107
108 /* WDT Control Register bit definitions */
109 #define WDT_EXP_SEL_01  0x0001  /* [01] Time-out = 496 us (with 33 Mhz clk). */
110 #define WDT_EXP_SEL_02  0x0002  /* [02] Time-out = 508 ms (with 33 Mhz clk). */
111 #define WDT_EXP_SEL_03  0x0004  /* [03] Time-out = 1.02 s (with 33 Mhz clk). */
112 #define WDT_EXP_SEL_04  0x0008  /* [04] Time-out = 2.03 s (with 33 Mhz clk). */
113 #define WDT_EXP_SEL_05  0x0010  /* [05] Time-out = 4.07 s (with 33 Mhz clk). */
114 #define WDT_EXP_SEL_06  0x0020  /* [06] Time-out = 8.13 s (with 33 Mhz clk). */
115 #define WDT_EXP_SEL_07  0x0040  /* [07] Time-out = 16.27s (with 33 Mhz clk). */
116 #define WDT_EXP_SEL_08  0x0080  /* [08] Time-out = 32.54s (with 33 Mhz clk). */
117 #define WDT_IRQ_FLG     0x1000  /* [12] Interrupt Request Flag */
118 #define WDT_WRST_ENB    0x4000  /* [14] Watchdog Timer Reset Enable */
119 #define WDT_ENB         0x8000  /* [15] Watchdog Timer Enable */
120
121 static __u16 __iomem *wdtmrctl;
122
123 static void wdt_timer_ping(unsigned long);
124 static struct timer_list timer;
125 static unsigned long next_heartbeat;
126 static unsigned long wdt_is_open;
127 static char wdt_expect_close;
128 static spinlock_t wdt_spinlock;
129
130 /*
131  *      Whack the dog
132  */
133
134 static void wdt_timer_ping(unsigned long data)
135 {
136         /* If we got a heartbeat pulse within the WDT_US_INTERVAL
137          * we agree to ping the WDT
138          */
139         if(time_before(jiffies, next_heartbeat))
140         {
141                 /* Ping the WDT */
142                 spin_lock(&wdt_spinlock);
143                 writew(0xAAAA, wdtmrctl);
144                 writew(0x5555, wdtmrctl);
145                 spin_unlock(&wdt_spinlock);
146
147                 /* Re-set the timer interval */
148                 timer.expires = jiffies + WDT_INTERVAL;
149                 add_timer(&timer);
150         } else {
151                 printk(KERN_WARNING PFX "Heartbeat lost! Will not ping the watchdog\n");
152         }
153 }
154
155 /*
156  *      Utility routines
157  */
158
159 static void wdt_config(int writeval)
160 {
161         __u16 dummy;
162         unsigned long flags;
163
164         /* buy some time (ping) */
165         spin_lock_irqsave(&wdt_spinlock, flags);
166         dummy=readw(wdtmrctl);  /* ensure write synchronization */
167         writew(0xAAAA, wdtmrctl);
168         writew(0x5555, wdtmrctl);
169         /* unlock WDT = make WDT configuration register writable one time */
170         writew(0x3333, wdtmrctl);
171         writew(0xCCCC, wdtmrctl);
172         /* write WDT configuration register */
173         writew(writeval, wdtmrctl);
174         spin_unlock_irqrestore(&wdt_spinlock, flags);
175 }
176
177 static int wdt_startup(void)
178 {
179         next_heartbeat = jiffies + (timeout * HZ);
180
181         /* Start the timer */
182         timer.expires = jiffies + WDT_INTERVAL;
183         add_timer(&timer);
184
185         /* Start the watchdog */
186         wdt_config(WDT_ENB | WDT_WRST_ENB | WDT_EXP_SEL_04);
187
188         printk(KERN_INFO PFX "Watchdog timer is now enabled.\n");
189         return 0;
190 }
191
192 static int wdt_turnoff(void)
193 {
194         /* Stop the timer */
195         del_timer(&timer);
196
197         /* Stop the watchdog */
198         wdt_config(0);
199
200         printk(KERN_INFO PFX "Watchdog timer is now disabled...\n");
201         return 0;
202 }
203
204 static int wdt_keepalive(void)
205 {
206         /* user land ping */
207         next_heartbeat = jiffies + (timeout * HZ);
208         return 0;
209 }
210
211 static int wdt_set_heartbeat(int t)
212 {
213         if ((t < 1) || (t > 3600))      /* arbitrary upper limit */
214                 return -EINVAL;
215
216         timeout = t;
217         return 0;
218 }
219
220 /*
221  *      /dev/watchdog handling
222  */
223
224 static ssize_t fop_write(struct file * file, const char __user * buf, size_t count, loff_t * ppos)
225 {
226         /* See if we got the magic character 'V' and reload the timer */
227         if(count) {
228                 if (!nowayout) {
229                         size_t ofs;
230
231                         /* note: just in case someone wrote the magic character
232                          * five months ago... */
233                         wdt_expect_close = 0;
234
235                         /* now scan */
236                         for(ofs = 0; ofs != count; ofs++) {
237                                 char c;
238                                 if (get_user(c, buf + ofs))
239                                         return -EFAULT;
240                                 if(c == 'V')
241                                         wdt_expect_close = 42;
242                         }
243                 }
244
245                 /* Well, anyhow someone wrote to us, we should return that favour */
246                 wdt_keepalive();
247         }
248         return count;
249 }
250
251 static int fop_open(struct inode * inode, struct file * file)
252 {
253         nonseekable_open(inode, file);
254
255         /* Just in case we're already talking to someone... */
256         if(test_and_set_bit(0, &wdt_is_open))
257                 return -EBUSY;
258         if (nowayout)
259                 __module_get(THIS_MODULE);
260
261         /* Good, fire up the show */
262         wdt_startup();
263         return 0;
264 }
265
266 static int fop_close(struct inode * inode, struct file * file)
267 {
268         if(wdt_expect_close == 42) {
269                 wdt_turnoff();
270         } else {
271                 printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n");
272                 wdt_keepalive();
273         }
274         clear_bit(0, &wdt_is_open);
275         wdt_expect_close = 0;
276         return 0;
277 }
278
279 static int fop_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
280         unsigned long arg)
281 {
282         void __user *argp = (void __user *)arg;
283         int __user *p = argp;
284         static struct watchdog_info ident = {
285                 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE,
286                 .firmware_version = 1,
287                 .identity = "SC520",
288         };
289
290         switch(cmd)
291         {
292                 default:
293                         return -ENOTTY;
294                 case WDIOC_GETSUPPORT:
295                         return copy_to_user(argp, &ident, sizeof(ident))?-EFAULT:0;
296                 case WDIOC_GETSTATUS:
297                 case WDIOC_GETBOOTSTATUS:
298                         return put_user(0, p);
299                 case WDIOC_KEEPALIVE:
300                         wdt_keepalive();
301                         return 0;
302                 case WDIOC_SETOPTIONS:
303                 {
304                         int new_options, retval = -EINVAL;
305
306                         if(get_user(new_options, p))
307                                 return -EFAULT;
308
309                         if(new_options & WDIOS_DISABLECARD) {
310                                 wdt_turnoff();
311                                 retval = 0;
312                         }
313
314                         if(new_options & WDIOS_ENABLECARD) {
315                                 wdt_startup();
316                                 retval = 0;
317                         }
318
319                         return retval;
320                 }
321                 case WDIOC_SETTIMEOUT:
322                 {
323                         int new_timeout;
324
325                         if(get_user(new_timeout, p))
326                                 return -EFAULT;
327
328                         if(wdt_set_heartbeat(new_timeout))
329                                 return -EINVAL;
330
331                         wdt_keepalive();
332                         /* Fall through */
333                 }
334                 case WDIOC_GETTIMEOUT:
335                         return put_user(timeout, p);
336         }
337 }
338
339 static const struct file_operations wdt_fops = {
340         .owner          = THIS_MODULE,
341         .llseek         = no_llseek,
342         .write          = fop_write,
343         .open           = fop_open,
344         .release        = fop_close,
345         .ioctl          = fop_ioctl,
346 };
347
348 static struct miscdevice wdt_miscdev = {
349         .minor  = WATCHDOG_MINOR,
350         .name   = "watchdog",
351         .fops   = &wdt_fops,
352 };
353
354 /*
355  *      Notifier for system down
356  */
357
358 static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
359         void *unused)
360 {
361         if(code==SYS_DOWN || code==SYS_HALT)
362                 wdt_turnoff();
363         return NOTIFY_DONE;
364 }
365
366 /*
367  *      The WDT needs to learn about soft shutdowns in order to
368  *      turn the timebomb registers off.
369  */
370
371 static struct notifier_block wdt_notifier = {
372         .notifier_call = wdt_notify_sys,
373 };
374
375 static void __exit sc520_wdt_unload(void)
376 {
377         if (!nowayout)
378                 wdt_turnoff();
379
380         /* Deregister */
381         misc_deregister(&wdt_miscdev);
382         unregister_reboot_notifier(&wdt_notifier);
383         iounmap(wdtmrctl);
384 }
385
386 static int __init sc520_wdt_init(void)
387 {
388         int rc = -EBUSY;
389
390         spin_lock_init(&wdt_spinlock);
391
392         init_timer(&timer);
393         timer.function = wdt_timer_ping;
394         timer.data = 0;
395
396         /* Check that the timeout value is within it's range ; if not reset to the default */
397         if (wdt_set_heartbeat(timeout)) {
398                 wdt_set_heartbeat(WATCHDOG_TIMEOUT);
399                 printk(KERN_INFO PFX "timeout value must be 1<=timeout<=3600, using %d\n",
400                         WATCHDOG_TIMEOUT);
401         }
402
403         wdtmrctl = ioremap((unsigned long)(MMCR_BASE + OFFS_WDTMRCTL), 2);
404         if (!wdtmrctl) {
405                 printk(KERN_ERR PFX "Unable to remap memory\n");
406                 rc = -ENOMEM;
407                 goto err_out_region2;
408         }
409
410         rc = register_reboot_notifier(&wdt_notifier);
411         if (rc) {
412                 printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n",
413                         rc);
414                 goto err_out_ioremap;
415         }
416
417         rc = misc_register(&wdt_miscdev);
418         if (rc) {
419                 printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n",
420                         WATCHDOG_MINOR, rc);
421                 goto err_out_notifier;
422         }
423
424         printk(KERN_INFO PFX "WDT driver for SC520 initialised. timeout=%d sec (nowayout=%d)\n",
425                 timeout,nowayout);
426
427         return 0;
428
429 err_out_notifier:
430         unregister_reboot_notifier(&wdt_notifier);
431 err_out_ioremap:
432         iounmap(wdtmrctl);
433 err_out_region2:
434         return rc;
435 }
436
437 module_init(sc520_wdt_init);
438 module_exit(sc520_wdt_unload);
439
440 MODULE_AUTHOR("Scott and Bill Jennings");
441 MODULE_DESCRIPTION("Driver for watchdog timer in AMD \"Elan\" SC520 uProcessor");
442 MODULE_LICENSE("GPL");
443 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);