blob: d1ef58579a3718d05016fb8017c9095e97ac0308 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * AMD Elan SC520 processor Watchdog Timer driver
3 *
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +00004 * Based on acquirewdt.c by Alan Cox,
5 * and sbc60xxwdt.c by Jakob Oestergaard <jakob@unthought.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +000014 * the hope that it may be useful for others.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 *
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
Joe Perches27c766a2012-02-15 15:06:19 -080055#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#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>
Tim Schmielau4e57b682005-10-30 15:03:48 -080068#include <linux/jiffies.h>
Alan Coxff948062008-05-19 14:08:44 +010069#include <linux/io.h>
70#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <asm/system.h>
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074/*
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 */
Alan Coxff948062008-05-19 14:08:44 +010093/* in seconds, will be multiplied by HZ to get seconds to wait for a ping */
94static int timeout = WATCHDOG_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095module_param(timeout, int, 0);
Alan Coxff948062008-05-19 14:08:44 +010096MODULE_PARM_DESC(timeout,
97 "Watchdog timeout in seconds. (1 <= timeout <= 3600, default="
98 __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Wim Van Sebroeck86a1e182012-03-05 16:51:11 +0100100static bool nowayout = WATCHDOG_NOWAYOUT;
101module_param(nowayout, bool, 0);
Alan Coxff948062008-05-19 14:08:44 +0100102MODULE_PARM_DESC(nowayout,
103 "Watchdog cannot be stopped once started (default="
104 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
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
125static __u16 __iomem *wdtmrctl;
126
127static void wdt_timer_ping(unsigned long);
Jiri Slaby82eb7c52007-02-08 18:39:36 +0100128static DEFINE_TIMER(timer, wdt_timer_ping, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129static unsigned long next_heartbeat;
130static unsigned long wdt_is_open;
131static char wdt_expect_close;
Alexey Dobriyanc7dfd0c2007-11-01 16:27:08 -0700132static DEFINE_SPINLOCK(wdt_spinlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134/*
135 * Whack the dog
136 */
137
138static 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 */
Alan Coxff948062008-05-19 14:08:44 +0100143 if (time_before(jiffies, next_heartbeat)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 /* Ping the WDT */
145 spin_lock(&wdt_spinlock);
146 writew(0xAAAA, wdtmrctl);
147 writew(0x5555, wdtmrctl);
148 spin_unlock(&wdt_spinlock);
149
150 /* Re-set the timer interval */
Jiri Slaby82eb7c52007-02-08 18:39:36 +0100151 mod_timer(&timer, jiffies + WDT_INTERVAL);
Alan Coxff948062008-05-19 14:08:44 +0100152 } else
Joe Perches27c766a2012-02-15 15:06:19 -0800153 pr_warn("Heartbeat lost! Will not ping the watchdog\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
156/*
157 * Utility routines
158 */
159
160static void wdt_config(int writeval)
161{
162 __u16 dummy;
163 unsigned long flags;
164
165 /* buy some time (ping) */
166 spin_lock_irqsave(&wdt_spinlock, flags);
Alan Coxff948062008-05-19 14:08:44 +0100167 dummy = readw(wdtmrctl); /* ensure write synchronization */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 writew(0xAAAA, wdtmrctl);
169 writew(0x5555, wdtmrctl);
170 /* unlock WDT = make WDT configuration register writable one time */
171 writew(0x3333, wdtmrctl);
172 writew(0xCCCC, wdtmrctl);
173 /* write WDT configuration register */
174 writew(writeval, wdtmrctl);
175 spin_unlock_irqrestore(&wdt_spinlock, flags);
176}
177
178static int wdt_startup(void)
179{
180 next_heartbeat = jiffies + (timeout * HZ);
181
182 /* Start the timer */
Jiri Slaby82eb7c52007-02-08 18:39:36 +0100183 mod_timer(&timer, jiffies + WDT_INTERVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 /* Start the watchdog */
186 wdt_config(WDT_ENB | WDT_WRST_ENB | WDT_EXP_SEL_04);
187
Joe Perches27c766a2012-02-15 15:06:19 -0800188 pr_info("Watchdog timer is now enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 return 0;
190}
191
192static int wdt_turnoff(void)
193{
194 /* Stop the timer */
195 del_timer(&timer);
196
197 /* Stop the watchdog */
198 wdt_config(0);
199
Joe Perches27c766a2012-02-15 15:06:19 -0800200 pr_info("Watchdog timer is now disabled...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 return 0;
202}
203
204static int wdt_keepalive(void)
205{
206 /* user land ping */
207 next_heartbeat = jiffies + (timeout * HZ);
208 return 0;
209}
210
211static 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
Alan Coxff948062008-05-19 14:08:44 +0100224static ssize_t fop_write(struct file *file, const char __user *buf,
225 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 /* See if we got the magic character 'V' and reload the timer */
Alan Coxff948062008-05-19 14:08:44 +0100228 if (count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 if (!nowayout) {
230 size_t ofs;
231
232 /* note: just in case someone wrote the magic character
233 * five months ago... */
234 wdt_expect_close = 0;
235
236 /* now scan */
Alan Coxff948062008-05-19 14:08:44 +0100237 for (ofs = 0; ofs != count; ofs++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 char c;
239 if (get_user(c, buf + ofs))
240 return -EFAULT;
Alan Coxff948062008-05-19 14:08:44 +0100241 if (c == 'V')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 wdt_expect_close = 42;
243 }
244 }
245
Alan Coxff948062008-05-19 14:08:44 +0100246 /* Well, anyhow someone wrote to us, we should
247 return that favour */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 wdt_keepalive();
249 }
250 return count;
251}
252
Alan Coxff948062008-05-19 14:08:44 +0100253static int fop_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 /* Just in case we're already talking to someone... */
Alan Coxff948062008-05-19 14:08:44 +0100256 if (test_and_set_bit(0, &wdt_is_open))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 return -EBUSY;
258 if (nowayout)
259 __module_get(THIS_MODULE);
260
261 /* Good, fire up the show */
262 wdt_startup();
Wim Van Sebroeck6abe78b2007-07-24 21:38:37 +0000263 return nonseekable_open(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
Alan Coxff948062008-05-19 14:08:44 +0100266static int fop_close(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Alan Coxff948062008-05-19 14:08:44 +0100268 if (wdt_expect_close == 42)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 wdt_turnoff();
Alan Coxff948062008-05-19 14:08:44 +0100270 else {
Joe Perches27c766a2012-02-15 15:06:19 -0800271 pr_crit("Unexpected close, not stopping watchdog!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 wdt_keepalive();
273 }
274 clear_bit(0, &wdt_is_open);
275 wdt_expect_close = 0;
276 return 0;
277}
278
Alan Cox9b748ed2008-05-19 14:08:49 +0100279static long fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
281 void __user *argp = (void __user *)arg;
282 int __user *p = argp;
Alan Coxff948062008-05-19 14:08:44 +0100283 static const struct watchdog_info ident = {
284 .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT
285 | WDIOF_MAGICCLOSE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 .firmware_version = 1,
287 .identity = "SC520",
288 };
289
Wim Van Sebroeck5eb82492008-07-17 18:08:47 +0000290 switch (cmd) {
Alan Coxff948062008-05-19 14:08:44 +0100291 case WDIOC_GETSUPPORT:
292 return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
293 case WDIOC_GETSTATUS:
294 case WDIOC_GETBOOTSTATUS:
295 return put_user(0, p);
Alan Coxff948062008-05-19 14:08:44 +0100296 case WDIOC_SETOPTIONS:
297 {
298 int new_options, retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Alan Coxff948062008-05-19 14:08:44 +0100300 if (get_user(new_options, p))
301 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Alan Coxff948062008-05-19 14:08:44 +0100303 if (new_options & WDIOS_DISABLECARD) {
304 wdt_turnoff();
305 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Alan Coxff948062008-05-19 14:08:44 +0100308 if (new_options & WDIOS_ENABLECARD) {
309 wdt_startup();
310 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
Alan Coxff948062008-05-19 14:08:44 +0100312
313 return retval;
314 }
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000315 case WDIOC_KEEPALIVE:
316 wdt_keepalive();
317 return 0;
Alan Coxff948062008-05-19 14:08:44 +0100318 case WDIOC_SETTIMEOUT:
319 {
320 int new_timeout;
321
322 if (get_user(new_timeout, p))
323 return -EFAULT;
324
325 if (wdt_set_heartbeat(new_timeout))
326 return -EINVAL;
327
328 wdt_keepalive();
329 /* Fall through */
330 }
331 case WDIOC_GETTIMEOUT:
332 return put_user(timeout, p);
Wim Van Sebroeck0c060902008-07-18 11:41:17 +0000333 default:
334 return -ENOTTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
336}
337
Arjan van de Ven62322d22006-07-03 00:24:21 -0700338static const struct file_operations wdt_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 .owner = THIS_MODULE,
340 .llseek = no_llseek,
341 .write = fop_write,
342 .open = fop_open,
343 .release = fop_close,
Alan Cox9b748ed2008-05-19 14:08:49 +0100344 .unlocked_ioctl = fop_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345};
346
347static struct miscdevice wdt_miscdev = {
348 .minor = WATCHDOG_MINOR,
349 .name = "watchdog",
350 .fops = &wdt_fops,
351};
352
353/*
354 * Notifier for system down
355 */
356
357static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
358 void *unused)
359{
Alan Coxff948062008-05-19 14:08:44 +0100360 if (code == SYS_DOWN || code == SYS_HALT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 wdt_turnoff();
362 return NOTIFY_DONE;
363}
364
365/*
366 * The WDT needs to learn about soft shutdowns in order to
367 * turn the timebomb registers off.
368 */
369
370static struct notifier_block wdt_notifier = {
371 .notifier_call = wdt_notify_sys,
372};
373
374static void __exit sc520_wdt_unload(void)
375{
376 if (!nowayout)
377 wdt_turnoff();
378
379 /* Deregister */
380 misc_deregister(&wdt_miscdev);
381 unregister_reboot_notifier(&wdt_notifier);
382 iounmap(wdtmrctl);
383}
384
385static int __init sc520_wdt_init(void)
386{
387 int rc = -EBUSY;
388
Alan Coxff948062008-05-19 14:08:44 +0100389 /* Check that the timeout value is within it's range ;
390 if not reset to the default */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (wdt_set_heartbeat(timeout)) {
392 wdt_set_heartbeat(WATCHDOG_TIMEOUT);
Joe Perches27c766a2012-02-15 15:06:19 -0800393 pr_info("timeout value must be 1 <= timeout <= 3600, using %d\n",
394 WATCHDOG_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
396
Sean Youngcef153a2011-08-23 22:30:09 +0100397 wdtmrctl = ioremap(MMCR_BASE + OFFS_WDTMRCTL, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 if (!wdtmrctl) {
Joe Perches27c766a2012-02-15 15:06:19 -0800399 pr_err("Unable to remap memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 rc = -ENOMEM;
401 goto err_out_region2;
402 }
403
404 rc = register_reboot_notifier(&wdt_notifier);
405 if (rc) {
Joe Perches27c766a2012-02-15 15:06:19 -0800406 pr_err("cannot register reboot notifier (err=%d)\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 goto err_out_ioremap;
408 }
409
410 rc = misc_register(&wdt_miscdev);
411 if (rc) {
Joe Perches27c766a2012-02-15 15:06:19 -0800412 pr_err("cannot register miscdev on minor=%d (err=%d)\n",
413 WATCHDOG_MINOR, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 goto err_out_notifier;
415 }
416
Joe Perches27c766a2012-02-15 15:06:19 -0800417 pr_info("WDT driver for SC520 initialised. timeout=%d sec (nowayout=%d)\n",
418 timeout, nowayout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 return 0;
421
422err_out_notifier:
423 unregister_reboot_notifier(&wdt_notifier);
424err_out_ioremap:
425 iounmap(wdtmrctl);
426err_out_region2:
427 return rc;
428}
429
430module_init(sc520_wdt_init);
431module_exit(sc520_wdt_unload);
432
433MODULE_AUTHOR("Scott and Bill Jennings");
Wim Van Sebroeck143a2e52009-03-18 08:35:09 +0000434MODULE_DESCRIPTION(
435 "Driver for watchdog timer in AMD \"Elan\" SC520 uProcessor");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436MODULE_LICENSE("GPL");
437MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);