| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | *	AMD Elan SC520 processor Watchdog Timer driver | 
|  | 3 | * | 
| Wim Van Sebroeck | 143a2e5 | 2009-03-18 08:35:09 +0000 | [diff] [blame] | 4 | *	Based on acquirewdt.c by Alan Cox, | 
|  | 5 | *	     and sbc60xxwdt.c by Jakob Oestergaard <jakob@unthought.net> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | 
| Wim Van Sebroeck | 143a2e5 | 2009-03-18 08:35:09 +0000 | [diff] [blame] | 14 | *	the hope that it may be useful for others. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> | 
| Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 66 | #include <linux/jiffies.h> | 
| Alan Cox | ff94806 | 2008-05-19 14:08:44 +0100 | [diff] [blame] | 67 | #include <linux/io.h> | 
|  | 68 | #include <linux/uaccess.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ | 
| Alan Cox | ff94806 | 2008-05-19 14:08:44 +0100 | [diff] [blame] | 94 | /* in seconds, will be multiplied by HZ to get seconds to wait for a ping */ | 
|  | 95 | static int timeout = WATCHDOG_TIMEOUT; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | module_param(timeout, int, 0); | 
| Alan Cox | ff94806 | 2008-05-19 14:08:44 +0100 | [diff] [blame] | 97 | MODULE_PARM_DESC(timeout, | 
|  | 98 | "Watchdog timeout in seconds. (1 <= timeout <= 3600, default=" | 
|  | 99 | __MODULE_STRING(WATCHDOG_TIMEOUT) ")"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 |  | 
| Andrey Panin | 4bfdf37 | 2005-07-27 11:43:58 -0700 | [diff] [blame] | 101 | static int nowayout = WATCHDOG_NOWAYOUT; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | module_param(nowayout, int, 0); | 
| Alan Cox | ff94806 | 2008-05-19 14:08:44 +0100 | [diff] [blame] | 103 | MODULE_PARM_DESC(nowayout, | 
|  | 104 | "Watchdog cannot be stopped once started (default=" | 
|  | 105 | __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 |  | 
|  | 107 | /* | 
|  | 108 | * AMD Elan SC520 - Watchdog Timer Registers | 
|  | 109 | */ | 
|  | 110 | #define MMCR_BASE	0xfffef000	/* The default base address */ | 
|  | 111 | #define OFFS_WDTMRCTL	0xCB0	/* Watchdog Timer Control Register */ | 
|  | 112 |  | 
|  | 113 | /* WDT Control Register bit definitions */ | 
|  | 114 | #define WDT_EXP_SEL_01	0x0001	/* [01] Time-out = 496 us (with 33 Mhz clk). */ | 
|  | 115 | #define WDT_EXP_SEL_02	0x0002	/* [02] Time-out = 508 ms (with 33 Mhz clk). */ | 
|  | 116 | #define WDT_EXP_SEL_03	0x0004	/* [03] Time-out = 1.02 s (with 33 Mhz clk). */ | 
|  | 117 | #define WDT_EXP_SEL_04	0x0008	/* [04] Time-out = 2.03 s (with 33 Mhz clk). */ | 
|  | 118 | #define WDT_EXP_SEL_05	0x0010	/* [05] Time-out = 4.07 s (with 33 Mhz clk). */ | 
|  | 119 | #define WDT_EXP_SEL_06	0x0020	/* [06] Time-out = 8.13 s (with 33 Mhz clk). */ | 
|  | 120 | #define WDT_EXP_SEL_07	0x0040	/* [07] Time-out = 16.27s (with 33 Mhz clk). */ | 
|  | 121 | #define WDT_EXP_SEL_08	0x0080	/* [08] Time-out = 32.54s (with 33 Mhz clk). */ | 
|  | 122 | #define WDT_IRQ_FLG	0x1000	/* [12] Interrupt Request Flag */ | 
|  | 123 | #define WDT_WRST_ENB	0x4000	/* [14] Watchdog Timer Reset Enable */ | 
|  | 124 | #define WDT_ENB		0x8000	/* [15] Watchdog Timer Enable */ | 
|  | 125 |  | 
|  | 126 | static __u16 __iomem *wdtmrctl; | 
|  | 127 |  | 
|  | 128 | static void wdt_timer_ping(unsigned long); | 
| Jiri Slaby | 82eb7c5 | 2007-02-08 18:39:36 +0100 | [diff] [blame] | 129 | static DEFINE_TIMER(timer, wdt_timer_ping, 0, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | static unsigned long next_heartbeat; | 
|  | 131 | static unsigned long wdt_is_open; | 
|  | 132 | static char wdt_expect_close; | 
| Alexey Dobriyan | c7dfd0c | 2007-11-01 16:27:08 -0700 | [diff] [blame] | 133 | static DEFINE_SPINLOCK(wdt_spinlock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 |  | 
|  | 135 | /* | 
|  | 136 | *	Whack the dog | 
|  | 137 | */ | 
|  | 138 |  | 
|  | 139 | static void wdt_timer_ping(unsigned long data) | 
|  | 140 | { | 
|  | 141 | /* If we got a heartbeat pulse within the WDT_US_INTERVAL | 
|  | 142 | * we agree to ping the WDT | 
|  | 143 | */ | 
| Alan Cox | ff94806 | 2008-05-19 14:08:44 +0100 | [diff] [blame] | 144 | if (time_before(jiffies, next_heartbeat)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ | 
| Jiri Slaby | 82eb7c5 | 2007-02-08 18:39:36 +0100 | [diff] [blame] | 152 | mod_timer(&timer, jiffies + WDT_INTERVAL); | 
| Alan Cox | ff94806 | 2008-05-19 14:08:44 +0100 | [diff] [blame] | 153 | } else | 
|  | 154 | printk(KERN_WARNING PFX | 
|  | 155 | "Heartbeat lost! Will not ping the watchdog\n"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | } | 
|  | 157 |  | 
|  | 158 | /* | 
|  | 159 | *	Utility routines | 
|  | 160 | */ | 
|  | 161 |  | 
|  | 162 | static void wdt_config(int writeval) | 
|  | 163 | { | 
|  | 164 | __u16 dummy; | 
|  | 165 | unsigned long flags; | 
|  | 166 |  | 
|  | 167 | /* buy some time (ping) */ | 
|  | 168 | spin_lock_irqsave(&wdt_spinlock, flags); | 
| Alan Cox | ff94806 | 2008-05-19 14:08:44 +0100 | [diff] [blame] | 169 | dummy = readw(wdtmrctl);	/* ensure write synchronization */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | writew(0xAAAA, wdtmrctl); | 
|  | 171 | writew(0x5555, wdtmrctl); | 
|  | 172 | /* unlock WDT = make WDT configuration register writable one time */ | 
|  | 173 | writew(0x3333, wdtmrctl); | 
|  | 174 | writew(0xCCCC, wdtmrctl); | 
|  | 175 | /* write WDT configuration register */ | 
|  | 176 | writew(writeval, wdtmrctl); | 
|  | 177 | spin_unlock_irqrestore(&wdt_spinlock, flags); | 
|  | 178 | } | 
|  | 179 |  | 
|  | 180 | static int wdt_startup(void) | 
|  | 181 | { | 
|  | 182 | next_heartbeat = jiffies + (timeout * HZ); | 
|  | 183 |  | 
|  | 184 | /* Start the timer */ | 
| Jiri Slaby | 82eb7c5 | 2007-02-08 18:39:36 +0100 | [diff] [blame] | 185 | mod_timer(&timer, jiffies + WDT_INTERVAL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 |  | 
|  | 187 | /* Start the watchdog */ | 
|  | 188 | wdt_config(WDT_ENB | WDT_WRST_ENB | WDT_EXP_SEL_04); | 
|  | 189 |  | 
|  | 190 | printk(KERN_INFO PFX "Watchdog timer is now enabled.\n"); | 
|  | 191 | return 0; | 
|  | 192 | } | 
|  | 193 |  | 
|  | 194 | static int wdt_turnoff(void) | 
|  | 195 | { | 
|  | 196 | /* Stop the timer */ | 
|  | 197 | del_timer(&timer); | 
|  | 198 |  | 
|  | 199 | /* Stop the watchdog */ | 
|  | 200 | wdt_config(0); | 
|  | 201 |  | 
|  | 202 | printk(KERN_INFO PFX "Watchdog timer is now disabled...\n"); | 
|  | 203 | return 0; | 
|  | 204 | } | 
|  | 205 |  | 
|  | 206 | static int wdt_keepalive(void) | 
|  | 207 | { | 
|  | 208 | /* user land ping */ | 
|  | 209 | next_heartbeat = jiffies + (timeout * HZ); | 
|  | 210 | return 0; | 
|  | 211 | } | 
|  | 212 |  | 
|  | 213 | static int wdt_set_heartbeat(int t) | 
|  | 214 | { | 
|  | 215 | if ((t < 1) || (t > 3600))	/* arbitrary upper limit */ | 
|  | 216 | return -EINVAL; | 
|  | 217 |  | 
|  | 218 | timeout = t; | 
|  | 219 | return 0; | 
|  | 220 | } | 
|  | 221 |  | 
|  | 222 | /* | 
|  | 223 | *	/dev/watchdog handling | 
|  | 224 | */ | 
|  | 225 |  | 
| Alan Cox | ff94806 | 2008-05-19 14:08:44 +0100 | [diff] [blame] | 226 | static ssize_t fop_write(struct file *file, const char __user *buf, | 
|  | 227 | size_t count, loff_t *ppos) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | { | 
|  | 229 | /* See if we got the magic character 'V' and reload the timer */ | 
| Alan Cox | ff94806 | 2008-05-19 14:08:44 +0100 | [diff] [blame] | 230 | if (count) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | if (!nowayout) { | 
|  | 232 | size_t ofs; | 
|  | 233 |  | 
|  | 234 | /* note: just in case someone wrote the magic character | 
|  | 235 | * five months ago... */ | 
|  | 236 | wdt_expect_close = 0; | 
|  | 237 |  | 
|  | 238 | /* now scan */ | 
| Alan Cox | ff94806 | 2008-05-19 14:08:44 +0100 | [diff] [blame] | 239 | for (ofs = 0; ofs != count; ofs++) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | char c; | 
|  | 241 | if (get_user(c, buf + ofs)) | 
|  | 242 | return -EFAULT; | 
| Alan Cox | ff94806 | 2008-05-19 14:08:44 +0100 | [diff] [blame] | 243 | if (c == 'V') | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | wdt_expect_close = 42; | 
|  | 245 | } | 
|  | 246 | } | 
|  | 247 |  | 
| Alan Cox | ff94806 | 2008-05-19 14:08:44 +0100 | [diff] [blame] | 248 | /* Well, anyhow someone wrote to us, we should | 
|  | 249 | return that favour */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | wdt_keepalive(); | 
|  | 251 | } | 
|  | 252 | return count; | 
|  | 253 | } | 
|  | 254 |  | 
| Alan Cox | ff94806 | 2008-05-19 14:08:44 +0100 | [diff] [blame] | 255 | static int fop_open(struct inode *inode, struct file *file) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | /* Just in case we're already talking to someone... */ | 
| Alan Cox | ff94806 | 2008-05-19 14:08:44 +0100 | [diff] [blame] | 258 | if (test_and_set_bit(0, &wdt_is_open)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | return -EBUSY; | 
|  | 260 | if (nowayout) | 
|  | 261 | __module_get(THIS_MODULE); | 
|  | 262 |  | 
|  | 263 | /* Good, fire up the show */ | 
|  | 264 | wdt_startup(); | 
| Wim Van Sebroeck | 6abe78b | 2007-07-24 21:38:37 +0000 | [diff] [blame] | 265 | return nonseekable_open(inode, file); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | } | 
|  | 267 |  | 
| Alan Cox | ff94806 | 2008-05-19 14:08:44 +0100 | [diff] [blame] | 268 | static int fop_close(struct inode *inode, struct file *file) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | { | 
| Alan Cox | ff94806 | 2008-05-19 14:08:44 +0100 | [diff] [blame] | 270 | if (wdt_expect_close == 42) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | wdt_turnoff(); | 
| Alan Cox | ff94806 | 2008-05-19 14:08:44 +0100 | [diff] [blame] | 272 | else { | 
|  | 273 | printk(KERN_CRIT PFX | 
|  | 274 | "Unexpected close, not stopping watchdog!\n"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | wdt_keepalive(); | 
|  | 276 | } | 
|  | 277 | clear_bit(0, &wdt_is_open); | 
|  | 278 | wdt_expect_close = 0; | 
|  | 279 | return 0; | 
|  | 280 | } | 
|  | 281 |  | 
| Alan Cox | 9b748ed | 2008-05-19 14:08:49 +0100 | [diff] [blame] | 282 | static long fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | { | 
|  | 284 | void __user *argp = (void __user *)arg; | 
|  | 285 | int __user *p = argp; | 
| Alan Cox | ff94806 | 2008-05-19 14:08:44 +0100 | [diff] [blame] | 286 | static const struct watchdog_info ident = { | 
|  | 287 | .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | 
|  | 288 | | WDIOF_MAGICCLOSE, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | .firmware_version = 1, | 
|  | 290 | .identity = "SC520", | 
|  | 291 | }; | 
|  | 292 |  | 
| Wim Van Sebroeck | 5eb8249 | 2008-07-17 18:08:47 +0000 | [diff] [blame] | 293 | switch (cmd) { | 
| Alan Cox | ff94806 | 2008-05-19 14:08:44 +0100 | [diff] [blame] | 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); | 
| Alan Cox | ff94806 | 2008-05-19 14:08:44 +0100 | [diff] [blame] | 299 | case WDIOC_SETOPTIONS: | 
|  | 300 | { | 
|  | 301 | int new_options, retval = -EINVAL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 |  | 
| Alan Cox | ff94806 | 2008-05-19 14:08:44 +0100 | [diff] [blame] | 303 | if (get_user(new_options, p)) | 
|  | 304 | return -EFAULT; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 |  | 
| Alan Cox | ff94806 | 2008-05-19 14:08:44 +0100 | [diff] [blame] | 306 | if (new_options & WDIOS_DISABLECARD) { | 
|  | 307 | wdt_turnoff(); | 
|  | 308 | retval = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 |  | 
| Alan Cox | ff94806 | 2008-05-19 14:08:44 +0100 | [diff] [blame] | 311 | if (new_options & WDIOS_ENABLECARD) { | 
|  | 312 | wdt_startup(); | 
|  | 313 | retval = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | } | 
| Alan Cox | ff94806 | 2008-05-19 14:08:44 +0100 | [diff] [blame] | 315 |  | 
|  | 316 | return retval; | 
|  | 317 | } | 
| Wim Van Sebroeck | 0c06090 | 2008-07-18 11:41:17 +0000 | [diff] [blame] | 318 | case WDIOC_KEEPALIVE: | 
|  | 319 | wdt_keepalive(); | 
|  | 320 | return 0; | 
| Alan Cox | ff94806 | 2008-05-19 14:08:44 +0100 | [diff] [blame] | 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); | 
| Wim Van Sebroeck | 0c06090 | 2008-07-18 11:41:17 +0000 | [diff] [blame] | 336 | default: | 
|  | 337 | return -ENOTTY; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | } | 
|  | 339 | } | 
|  | 340 |  | 
| Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 341 | static const struct file_operations wdt_fops = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | .owner		= THIS_MODULE, | 
|  | 343 | .llseek		= no_llseek, | 
|  | 344 | .write		= fop_write, | 
|  | 345 | .open		= fop_open, | 
|  | 346 | .release	= fop_close, | 
| Alan Cox | 9b748ed | 2008-05-19 14:08:49 +0100 | [diff] [blame] | 347 | .unlocked_ioctl	= fop_ioctl, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | }; | 
|  | 349 |  | 
|  | 350 | static struct miscdevice wdt_miscdev = { | 
|  | 351 | .minor	= WATCHDOG_MINOR, | 
|  | 352 | .name	= "watchdog", | 
|  | 353 | .fops	= &wdt_fops, | 
|  | 354 | }; | 
|  | 355 |  | 
|  | 356 | /* | 
|  | 357 | *	Notifier for system down | 
|  | 358 | */ | 
|  | 359 |  | 
|  | 360 | static int wdt_notify_sys(struct notifier_block *this, unsigned long code, | 
|  | 361 | void *unused) | 
|  | 362 | { | 
| Alan Cox | ff94806 | 2008-05-19 14:08:44 +0100 | [diff] [blame] | 363 | if (code == SYS_DOWN || code == SYS_HALT) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | wdt_turnoff(); | 
|  | 365 | return NOTIFY_DONE; | 
|  | 366 | } | 
|  | 367 |  | 
|  | 368 | /* | 
|  | 369 | *	The WDT needs to learn about soft shutdowns in order to | 
|  | 370 | *	turn the timebomb registers off. | 
|  | 371 | */ | 
|  | 372 |  | 
|  | 373 | static struct notifier_block wdt_notifier = { | 
|  | 374 | .notifier_call = wdt_notify_sys, | 
|  | 375 | }; | 
|  | 376 |  | 
|  | 377 | static void __exit sc520_wdt_unload(void) | 
|  | 378 | { | 
|  | 379 | if (!nowayout) | 
|  | 380 | wdt_turnoff(); | 
|  | 381 |  | 
|  | 382 | /* Deregister */ | 
|  | 383 | misc_deregister(&wdt_miscdev); | 
|  | 384 | unregister_reboot_notifier(&wdt_notifier); | 
|  | 385 | iounmap(wdtmrctl); | 
|  | 386 | } | 
|  | 387 |  | 
|  | 388 | static int __init sc520_wdt_init(void) | 
|  | 389 | { | 
|  | 390 | int rc = -EBUSY; | 
|  | 391 |  | 
| Alan Cox | ff94806 | 2008-05-19 14:08:44 +0100 | [diff] [blame] | 392 | /* Check that the timeout value is within it's range ; | 
|  | 393 | if not reset to the default */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | if (wdt_set_heartbeat(timeout)) { | 
|  | 395 | wdt_set_heartbeat(WATCHDOG_TIMEOUT); | 
| Alan Cox | ff94806 | 2008-05-19 14:08:44 +0100 | [diff] [blame] | 396 | printk(KERN_INFO PFX | 
|  | 397 | "timeout value must be 1 <= timeout <= 3600, using %d\n", | 
|  | 398 | WATCHDOG_TIMEOUT); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | } | 
|  | 400 |  | 
| Sean Young | cef153a | 2011-08-23 22:30:09 +0100 | [diff] [blame] | 401 | wdtmrctl = ioremap(MMCR_BASE + OFFS_WDTMRCTL, 2); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | if (!wdtmrctl) { | 
|  | 403 | printk(KERN_ERR PFX "Unable to remap memory\n"); | 
|  | 404 | rc = -ENOMEM; | 
|  | 405 | goto err_out_region2; | 
|  | 406 | } | 
|  | 407 |  | 
|  | 408 | rc = register_reboot_notifier(&wdt_notifier); | 
|  | 409 | if (rc) { | 
| Alan Cox | ff94806 | 2008-05-19 14:08:44 +0100 | [diff] [blame] | 410 | printk(KERN_ERR PFX | 
|  | 411 | "cannot register reboot notifier (err=%d)\n", rc); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | goto err_out_ioremap; | 
|  | 413 | } | 
|  | 414 |  | 
|  | 415 | rc = misc_register(&wdt_miscdev); | 
|  | 416 | if (rc) { | 
| Alan Cox | ff94806 | 2008-05-19 14:08:44 +0100 | [diff] [blame] | 417 | printk(KERN_ERR PFX | 
|  | 418 | "cannot register miscdev on minor=%d (err=%d)\n", | 
|  | 419 | WATCHDOG_MINOR, rc); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | goto err_out_notifier; | 
|  | 421 | } | 
|  | 422 |  | 
| Alan Cox | ff94806 | 2008-05-19 14:08:44 +0100 | [diff] [blame] | 423 | printk(KERN_INFO PFX | 
|  | 424 | "WDT driver for SC520 initialised. timeout=%d sec (nowayout=%d)\n", | 
|  | 425 | timeout, nowayout); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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"); | 
| Wim Van Sebroeck | 143a2e5 | 2009-03-18 08:35:09 +0000 | [diff] [blame] | 441 | MODULE_DESCRIPTION( | 
|  | 442 | "Driver for watchdog timer in AMD \"Elan\" SC520 uProcessor"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | MODULE_LICENSE("GPL"); | 
|  | 444 | MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); |