diff options
Diffstat (limited to 'drivers/char/watchdog/sc520_wdt.c')
-rw-r--r-- | drivers/char/watchdog/sc520_wdt.c | 447 |
1 files changed, 447 insertions, 0 deletions
diff --git a/drivers/char/watchdog/sc520_wdt.c b/drivers/char/watchdog/sc520_wdt.c new file mode 100644 index 000000000000..f6d143e1900d --- /dev/null +++ b/drivers/char/watchdog/sc520_wdt.c | |||
@@ -0,0 +1,447 @@ | |||
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 | |||
67 | #include <asm/io.h> | ||
68 | #include <asm/uaccess.h> | ||
69 | #include <asm/system.h> | ||
70 | |||
71 | #define OUR_NAME "sc520_wdt" | ||
72 | #define PFX OUR_NAME ": " | ||
73 | |||
74 | /* | ||
75 | * The AMD Elan SC520 timeout value is 492us times a power of 2 (0-7) | ||
76 | * | ||
77 | * 0: 492us 2: 1.01s 4: 4.03s 6: 16.22s | ||
78 | * 1: 503ms 3: 2.01s 5: 8.05s 7: 32.21s | ||
79 | * | ||
80 | * We will program the SC520 watchdog for a timeout of 2.01s. | ||
81 | * If we reset the watchdog every ~250ms we should be safe. | ||
82 | */ | ||
83 | |||
84 | #define WDT_INTERVAL (HZ/4+1) | ||
85 | |||
86 | /* | ||
87 | * We must not require too good response from the userspace daemon. | ||
88 | * Here we require the userspace daemon to send us a heartbeat | ||
89 | * char to /dev/watchdog every 30 seconds. | ||
90 | */ | ||
91 | |||
92 | #define WATCHDOG_TIMEOUT 30 /* 30 sec default timeout */ | ||
93 | static int timeout = WATCHDOG_TIMEOUT; /* in seconds, will be multiplied by HZ to get seconds to wait for a ping */ | ||
94 | module_param(timeout, int, 0); | ||
95 | MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds. (1<=timeout<=3600, default=" __MODULE_STRING(WATCHDOG_TIMEOUT) ")"); | ||
96 | |||
97 | #ifdef CONFIG_WATCHDOG_NOWAYOUT | ||
98 | static int nowayout = 1; | ||
99 | #else | ||
100 | static int nowayout = 0; | ||
101 | #endif | ||
102 | |||
103 | module_param(nowayout, int, 0); | ||
104 | MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=CONFIG_WATCHDOG_NOWAYOUT)"); | ||
105 | |||
106 | /* | ||
107 | * AMD Elan SC520 - Watchdog Timer Registers | ||
108 | */ | ||
109 | #define MMCR_BASE 0xfffef000 /* The default base address */ | ||
110 | #define OFFS_WDTMRCTL 0xCB0 /* Watchdog Timer Control Register */ | ||
111 | |||
112 | /* WDT Control Register bit definitions */ | ||
113 | #define WDT_EXP_SEL_01 0x0001 /* [01] Time-out = 496 us (with 33 Mhz clk). */ | ||
114 | #define WDT_EXP_SEL_02 0x0002 /* [02] Time-out = 508 ms (with 33 Mhz clk). */ | ||
115 | #define WDT_EXP_SEL_03 0x0004 /* [03] Time-out = 1.02 s (with 33 Mhz clk). */ | ||
116 | #define WDT_EXP_SEL_04 0x0008 /* [04] Time-out = 2.03 s (with 33 Mhz clk). */ | ||
117 | #define WDT_EXP_SEL_05 0x0010 /* [05] Time-out = 4.07 s (with 33 Mhz clk). */ | ||
118 | #define WDT_EXP_SEL_06 0x0020 /* [06] Time-out = 8.13 s (with 33 Mhz clk). */ | ||
119 | #define WDT_EXP_SEL_07 0x0040 /* [07] Time-out = 16.27s (with 33 Mhz clk). */ | ||
120 | #define WDT_EXP_SEL_08 0x0080 /* [08] Time-out = 32.54s (with 33 Mhz clk). */ | ||
121 | #define WDT_IRQ_FLG 0x1000 /* [12] Interrupt Request Flag */ | ||
122 | #define WDT_WRST_ENB 0x4000 /* [14] Watchdog Timer Reset Enable */ | ||
123 | #define WDT_ENB 0x8000 /* [15] Watchdog Timer Enable */ | ||
124 | |||
125 | static __u16 __iomem *wdtmrctl; | ||
126 | |||
127 | static void wdt_timer_ping(unsigned long); | ||
128 | static struct timer_list timer; | ||
129 | static unsigned long next_heartbeat; | ||
130 | static unsigned long wdt_is_open; | ||
131 | static char wdt_expect_close; | ||
132 | static spinlock_t wdt_spinlock; | ||
133 | |||
134 | /* | ||
135 | * Whack the dog | ||
136 | */ | ||
137 | |||
138 | static void wdt_timer_ping(unsigned long data) | ||
139 | { | ||
140 | /* If we got a heartbeat pulse within the WDT_US_INTERVAL | ||
141 | * we agree to ping the WDT | ||
142 | */ | ||
143 | if(time_before(jiffies, next_heartbeat)) | ||
144 | { | ||
145 | /* Ping the WDT */ | ||
146 | spin_lock(&wdt_spinlock); | ||
147 | writew(0xAAAA, wdtmrctl); | ||
148 | writew(0x5555, wdtmrctl); | ||
149 | spin_unlock(&wdt_spinlock); | ||
150 | |||
151 | /* Re-set the timer interval */ | ||
152 | timer.expires = jiffies + WDT_INTERVAL; | ||
153 | add_timer(&timer); | ||
154 | } else { | ||
155 | printk(KERN_WARNING PFX "Heartbeat lost! Will not ping the watchdog\n"); | ||
156 | } | ||
157 | } | ||
158 | |||
159 | /* | ||
160 | * Utility routines | ||
161 | */ | ||
162 | |||
163 | static void wdt_config(int writeval) | ||
164 | { | ||
165 | __u16 dummy; | ||
166 | unsigned long flags; | ||
167 | |||
168 | /* buy some time (ping) */ | ||
169 | spin_lock_irqsave(&wdt_spinlock, flags); | ||
170 | dummy=readw(wdtmrctl); /* ensure write synchronization */ | ||
171 | writew(0xAAAA, wdtmrctl); | ||
172 | writew(0x5555, wdtmrctl); | ||
173 | /* unlock WDT = make WDT configuration register writable one time */ | ||
174 | writew(0x3333, wdtmrctl); | ||
175 | writew(0xCCCC, wdtmrctl); | ||
176 | /* write WDT configuration register */ | ||
177 | writew(writeval, wdtmrctl); | ||
178 | spin_unlock_irqrestore(&wdt_spinlock, flags); | ||
179 | } | ||
180 | |||
181 | static int wdt_startup(void) | ||
182 | { | ||
183 | next_heartbeat = jiffies + (timeout * HZ); | ||
184 | |||
185 | /* Start the timer */ | ||
186 | timer.expires = jiffies + WDT_INTERVAL; | ||
187 | add_timer(&timer); | ||
188 | |||
189 | /* Start the watchdog */ | ||
190 | wdt_config(WDT_ENB | WDT_WRST_ENB | WDT_EXP_SEL_04); | ||
191 | |||
192 | printk(KERN_INFO PFX "Watchdog timer is now enabled.\n"); | ||
193 | return 0; | ||
194 | } | ||
195 | |||
196 | static int wdt_turnoff(void) | ||
197 | { | ||
198 | /* Stop the timer */ | ||
199 | del_timer(&timer); | ||
200 | |||
201 | /* Stop the watchdog */ | ||
202 | wdt_config(0); | ||
203 | |||
204 | printk(KERN_INFO PFX "Watchdog timer is now disabled...\n"); | ||
205 | return 0; | ||
206 | } | ||
207 | |||
208 | static int wdt_keepalive(void) | ||
209 | { | ||
210 | /* user land ping */ | ||
211 | next_heartbeat = jiffies + (timeout * HZ); | ||
212 | return 0; | ||
213 | } | ||
214 | |||
215 | static int wdt_set_heartbeat(int t) | ||
216 | { | ||
217 | if ((t < 1) || (t > 3600)) /* arbitrary upper limit */ | ||
218 | return -EINVAL; | ||
219 | |||
220 | timeout = t; | ||
221 | return 0; | ||
222 | } | ||
223 | |||
224 | /* | ||
225 | * /dev/watchdog handling | ||
226 | */ | ||
227 | |||
228 | static ssize_t fop_write(struct file * file, const char __user * buf, size_t count, loff_t * ppos) | ||
229 | { | ||
230 | /* See if we got the magic character 'V' and reload the timer */ | ||
231 | if(count) { | ||
232 | if (!nowayout) { | ||
233 | size_t ofs; | ||
234 | |||
235 | /* note: just in case someone wrote the magic character | ||
236 | * five months ago... */ | ||
237 | wdt_expect_close = 0; | ||
238 | |||
239 | /* now scan */ | ||
240 | for(ofs = 0; ofs != count; ofs++) { | ||
241 | char c; | ||
242 | if (get_user(c, buf + ofs)) | ||
243 | return -EFAULT; | ||
244 | if(c == 'V') | ||
245 | wdt_expect_close = 42; | ||
246 | } | ||
247 | } | ||
248 | |||
249 | /* Well, anyhow someone wrote to us, we should return that favour */ | ||
250 | wdt_keepalive(); | ||
251 | } | ||
252 | return count; | ||
253 | } | ||
254 | |||
255 | static int fop_open(struct inode * inode, struct file * file) | ||
256 | { | ||
257 | nonseekable_open(inode, file); | ||
258 | |||
259 | /* Just in case we're already talking to someone... */ | ||
260 | if(test_and_set_bit(0, &wdt_is_open)) | ||
261 | return -EBUSY; | ||
262 | if (nowayout) | ||
263 | __module_get(THIS_MODULE); | ||
264 | |||
265 | /* Good, fire up the show */ | ||
266 | wdt_startup(); | ||
267 | return 0; | ||
268 | } | ||
269 | |||
270 | static int fop_close(struct inode * inode, struct file * file) | ||
271 | { | ||
272 | if(wdt_expect_close == 42) { | ||
273 | wdt_turnoff(); | ||
274 | } else { | ||
275 | printk(KERN_CRIT PFX "Unexpected close, not stopping watchdog!\n"); | ||
276 | wdt_keepalive(); | ||
277 | } | ||
278 | clear_bit(0, &wdt_is_open); | ||
279 | wdt_expect_close = 0; | ||
280 | return 0; | ||
281 | } | ||
282 | |||
283 | static int fop_ioctl(struct inode *inode, struct file *file, unsigned int cmd, | ||
284 | unsigned long arg) | ||
285 | { | ||
286 | void __user *argp = (void __user *)arg; | ||
287 | int __user *p = argp; | ||
288 | static struct watchdog_info ident = { | ||
289 | .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE, | ||
290 | .firmware_version = 1, | ||
291 | .identity = "SC520", | ||
292 | }; | ||
293 | |||
294 | switch(cmd) | ||
295 | { | ||
296 | default: | ||
297 | return -ENOIOCTLCMD; | ||
298 | case WDIOC_GETSUPPORT: | ||
299 | return copy_to_user(argp, &ident, sizeof(ident))?-EFAULT:0; | ||
300 | case WDIOC_GETSTATUS: | ||
301 | case WDIOC_GETBOOTSTATUS: | ||
302 | return put_user(0, p); | ||
303 | case WDIOC_KEEPALIVE: | ||
304 | wdt_keepalive(); | ||
305 | return 0; | ||
306 | case WDIOC_SETOPTIONS: | ||
307 | { | ||
308 | int new_options, retval = -EINVAL; | ||
309 | |||
310 | if(get_user(new_options, p)) | ||
311 | return -EFAULT; | ||
312 | |||
313 | if(new_options & WDIOS_DISABLECARD) { | ||
314 | wdt_turnoff(); | ||
315 | retval = 0; | ||
316 | } | ||
317 | |||
318 | if(new_options & WDIOS_ENABLECARD) { | ||
319 | wdt_startup(); | ||
320 | retval = 0; | ||
321 | } | ||
322 | |||
323 | return retval; | ||
324 | } | ||
325 | case WDIOC_SETTIMEOUT: | ||
326 | { | ||
327 | int new_timeout; | ||
328 | |||
329 | if(get_user(new_timeout, p)) | ||
330 | return -EFAULT; | ||
331 | |||
332 | if(wdt_set_heartbeat(new_timeout)) | ||
333 | return -EINVAL; | ||
334 | |||
335 | wdt_keepalive(); | ||
336 | /* Fall through */ | ||
337 | } | ||
338 | case WDIOC_GETTIMEOUT: | ||
339 | return put_user(timeout, p); | ||
340 | } | ||
341 | } | ||
342 | |||
343 | static struct file_operations wdt_fops = { | ||
344 | .owner = THIS_MODULE, | ||
345 | .llseek = no_llseek, | ||
346 | .write = fop_write, | ||
347 | .open = fop_open, | ||
348 | .release = fop_close, | ||
349 | .ioctl = fop_ioctl, | ||
350 | }; | ||
351 | |||
352 | static struct miscdevice wdt_miscdev = { | ||
353 | .minor = WATCHDOG_MINOR, | ||
354 | .name = "watchdog", | ||
355 | .fops = &wdt_fops, | ||
356 | }; | ||
357 | |||
358 | /* | ||
359 | * Notifier for system down | ||
360 | */ | ||
361 | |||
362 | static int wdt_notify_sys(struct notifier_block *this, unsigned long code, | ||
363 | void *unused) | ||
364 | { | ||
365 | if(code==SYS_DOWN || code==SYS_HALT) | ||
366 | wdt_turnoff(); | ||
367 | return NOTIFY_DONE; | ||
368 | } | ||
369 | |||
370 | /* | ||
371 | * The WDT needs to learn about soft shutdowns in order to | ||
372 | * turn the timebomb registers off. | ||
373 | */ | ||
374 | |||
375 | static struct notifier_block wdt_notifier = { | ||
376 | .notifier_call = wdt_notify_sys, | ||
377 | }; | ||
378 | |||
379 | static void __exit sc520_wdt_unload(void) | ||
380 | { | ||
381 | if (!nowayout) | ||
382 | wdt_turnoff(); | ||
383 | |||
384 | /* Deregister */ | ||
385 | misc_deregister(&wdt_miscdev); | ||
386 | unregister_reboot_notifier(&wdt_notifier); | ||
387 | iounmap(wdtmrctl); | ||
388 | } | ||
389 | |||
390 | static int __init sc520_wdt_init(void) | ||
391 | { | ||
392 | int rc = -EBUSY; | ||
393 | |||
394 | spin_lock_init(&wdt_spinlock); | ||
395 | |||
396 | init_timer(&timer); | ||
397 | timer.function = wdt_timer_ping; | ||
398 | timer.data = 0; | ||
399 | |||
400 | /* Check that the timeout value is within it's range ; if not reset to the default */ | ||
401 | if (wdt_set_heartbeat(timeout)) { | ||
402 | wdt_set_heartbeat(WATCHDOG_TIMEOUT); | ||
403 | printk(KERN_INFO PFX "timeout value must be 1<=timeout<=3600, using %d\n", | ||
404 | WATCHDOG_TIMEOUT); | ||
405 | } | ||
406 | |||
407 | wdtmrctl = ioremap((unsigned long)(MMCR_BASE + OFFS_WDTMRCTL), 2); | ||
408 | if (!wdtmrctl) { | ||
409 | printk(KERN_ERR PFX "Unable to remap memory\n"); | ||
410 | rc = -ENOMEM; | ||
411 | goto err_out_region2; | ||
412 | } | ||
413 | |||
414 | rc = register_reboot_notifier(&wdt_notifier); | ||
415 | if (rc) { | ||
416 | printk(KERN_ERR PFX "cannot register reboot notifier (err=%d)\n", | ||
417 | rc); | ||
418 | goto err_out_ioremap; | ||
419 | } | ||
420 | |||
421 | rc = misc_register(&wdt_miscdev); | ||
422 | if (rc) { | ||
423 | printk(KERN_ERR PFX "cannot register miscdev on minor=%d (err=%d)\n", | ||
424 | WATCHDOG_MINOR, rc); | ||
425 | goto err_out_notifier; | ||
426 | } | ||
427 | |||
428 | printk(KERN_INFO PFX "WDT driver for SC520 initialised. timeout=%d sec (nowayout=%d)\n", | ||
429 | timeout,nowayout); | ||
430 | |||
431 | return 0; | ||
432 | |||
433 | err_out_notifier: | ||
434 | unregister_reboot_notifier(&wdt_notifier); | ||
435 | err_out_ioremap: | ||
436 | iounmap(wdtmrctl); | ||
437 | err_out_region2: | ||
438 | return rc; | ||
439 | } | ||
440 | |||
441 | module_init(sc520_wdt_init); | ||
442 | module_exit(sc520_wdt_unload); | ||
443 | |||
444 | MODULE_AUTHOR("Scott and Bill Jennings"); | ||
445 | MODULE_DESCRIPTION("Driver for watchdog timer in AMD \"Elan\" SC520 uProcessor"); | ||
446 | MODULE_LICENSE("GPL"); | ||
447 | MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); | ||