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