Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * IB700 Single Board Computer WDT driver |
| 3 | * |
| 4 | * (c) Copyright 2001 Charles Howes <chowes@vsol.net> |
| 5 | * |
Wim Van Sebroeck | c9d7710 | 2007-01-27 22:07:03 +0100 | [diff] [blame] | 6 | * Based on advantechwdt.c which is based on acquirewdt.c which |
| 7 | * is based on wdt.c. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * |
| 9 | * (c) Copyright 2000-2001 Marek Michalkiewicz <marekm@linux.org.pl> |
| 10 | * |
| 11 | * Based on acquirewdt.c which is based on wdt.c. |
| 12 | * Original copyright messages: |
| 13 | * |
| 14 | * (c) Copyright 1996 Alan Cox <alan@redhat.com>, All Rights Reserved. |
| 15 | * http://www.redhat.com |
| 16 | * |
| 17 | * This program is free software; you can redistribute it and/or |
| 18 | * modify it under the terms of the GNU General Public License |
| 19 | * as published by the Free Software Foundation; either version |
| 20 | * 2 of the License, or (at your option) any later version. |
| 21 | * |
| 22 | * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide |
| 23 | * warranty for any of this software. This material is provided |
| 24 | * "AS-IS" and at no charge. |
| 25 | * |
| 26 | * (c) Copyright 1995 Alan Cox <alan@redhat.com> |
| 27 | * |
Wim Van Sebroeck | c9d7710 | 2007-01-27 22:07:03 +0100 | [diff] [blame] | 28 | * 14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com> |
| 29 | * Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT |
| 30 | * Added timeout module option to override default |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | * |
| 32 | */ |
| 33 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <linux/module.h> |
| 35 | #include <linux/types.h> |
| 36 | #include <linux/miscdevice.h> |
| 37 | #include <linux/watchdog.h> |
| 38 | #include <linux/ioport.h> |
| 39 | #include <linux/notifier.h> |
| 40 | #include <linux/fs.h> |
| 41 | #include <linux/reboot.h> |
| 42 | #include <linux/init.h> |
| 43 | #include <linux/spinlock.h> |
| 44 | #include <linux/moduleparam.h> |
| 45 | |
| 46 | #include <asm/io.h> |
| 47 | #include <asm/uaccess.h> |
| 48 | #include <asm/system.h> |
| 49 | |
| 50 | static unsigned long ibwdt_is_open; |
| 51 | static spinlock_t ibwdt_lock; |
| 52 | static char expect_close; |
| 53 | |
| 54 | #define PFX "ib700wdt: " |
| 55 | |
| 56 | /* |
| 57 | * |
| 58 | * Watchdog Timer Configuration |
| 59 | * |
| 60 | * The function of the watchdog timer is to reset the system |
| 61 | * automatically and is defined at I/O port 0443H. To enable the |
| 62 | * watchdog timer and allow the system to reset, write I/O port 0443H. |
| 63 | * To disable the timer, write I/O port 0441H for the system to stop the |
| 64 | * watchdog function. The timer has a tolerance of 20% for its |
| 65 | * intervals. |
| 66 | * |
| 67 | * The following describes how the timer should be programmed. |
| 68 | * |
| 69 | * Enabling Watchdog: |
| 70 | * MOV AX,000FH (Choose the values from 0 to F) |
| 71 | * MOV DX,0443H |
| 72 | * OUT DX,AX |
| 73 | * |
| 74 | * Disabling Watchdog: |
| 75 | * MOV AX,000FH (Any value is fine.) |
| 76 | * MOV DX,0441H |
| 77 | * OUT DX,AX |
| 78 | * |
| 79 | * Watchdog timer control table: |
| 80 | * Level Value Time/sec | Level Value Time/sec |
| 81 | * 1 F 0 | 9 7 16 |
| 82 | * 2 E 2 | 10 6 18 |
| 83 | * 3 D 4 | 11 5 20 |
| 84 | * 4 C 6 | 12 4 22 |
| 85 | * 5 B 8 | 13 3 24 |
| 86 | * 6 A 10 | 14 2 26 |
| 87 | * 7 9 12 | 15 1 28 |
| 88 | * 8 8 14 | 16 0 30 |
| 89 | * |
| 90 | */ |
| 91 | |
| 92 | static int wd_times[] = { |
| 93 | 30, /* 0x0 */ |
| 94 | 28, /* 0x1 */ |
| 95 | 26, /* 0x2 */ |
| 96 | 24, /* 0x3 */ |
| 97 | 22, /* 0x4 */ |
| 98 | 20, /* 0x5 */ |
| 99 | 18, /* 0x6 */ |
| 100 | 16, /* 0x7 */ |
| 101 | 14, /* 0x8 */ |
| 102 | 12, /* 0x9 */ |
| 103 | 10, /* 0xA */ |
| 104 | 8, /* 0xB */ |
| 105 | 6, /* 0xC */ |
| 106 | 4, /* 0xD */ |
| 107 | 2, /* 0xE */ |
| 108 | 0, /* 0xF */ |
| 109 | }; |
| 110 | |
| 111 | #define WDT_STOP 0x441 |
| 112 | #define WDT_START 0x443 |
| 113 | |
| 114 | /* Default timeout */ |
| 115 | #define WD_TIMO 0 /* 30 seconds +/- 20%, from table */ |
| 116 | |
| 117 | static int wd_margin = WD_TIMO; |
| 118 | |
Andrey Panin | 4bfdf37 | 2005-07-27 11:43:58 -0700 | [diff] [blame] | 119 | static int nowayout = WATCHDOG_NOWAYOUT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | module_param(nowayout, int, 0); |
Wim Van Sebroeck | bffda5c | 2007-01-27 20:54:24 +0100 | [diff] [blame] | 121 | MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
| 123 | |
| 124 | /* |
Wim Van Sebroeck | 35d55c9 | 2007-01-27 21:50:53 +0100 | [diff] [blame] | 125 | * Watchdog Operations |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | */ |
| 127 | |
| 128 | static void |
| 129 | ibwdt_ping(void) |
| 130 | { |
Wim Van Sebroeck | e42162a | 2007-01-27 22:12:54 +0100 | [diff] [blame^] | 131 | spin_lock(&ibwdt_lock); |
| 132 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | /* Write a watchdog value */ |
| 134 | outb_p(wd_margin, WDT_START); |
Wim Van Sebroeck | e42162a | 2007-01-27 22:12:54 +0100 | [diff] [blame^] | 135 | |
| 136 | spin_unlock(&ibwdt_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Wim Van Sebroeck | 35d55c9 | 2007-01-27 21:50:53 +0100 | [diff] [blame] | 139 | static void |
| 140 | ibwdt_disable(void) |
| 141 | { |
Wim Van Sebroeck | e42162a | 2007-01-27 22:12:54 +0100 | [diff] [blame^] | 142 | spin_lock(&ibwdt_lock); |
Wim Van Sebroeck | 35d55c9 | 2007-01-27 21:50:53 +0100 | [diff] [blame] | 143 | outb_p(0, WDT_STOP); |
Wim Van Sebroeck | e42162a | 2007-01-27 22:12:54 +0100 | [diff] [blame^] | 144 | spin_unlock(&ibwdt_lock); |
Wim Van Sebroeck | 35d55c9 | 2007-01-27 21:50:53 +0100 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | static int |
| 148 | ibwdt_set_heartbeat(int t) |
| 149 | { |
| 150 | int i; |
| 151 | |
| 152 | if ((t < 0) || (t > 30)) |
| 153 | return -EINVAL; |
| 154 | |
| 155 | for (i = 0x0F; i > -1; i--) |
| 156 | if (wd_times[i] > t) |
| 157 | break; |
| 158 | wd_margin = i; |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | * /dev/watchdog handling |
| 164 | */ |
| 165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | static ssize_t |
| 167 | ibwdt_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) |
| 168 | { |
| 169 | if (count) { |
| 170 | if (!nowayout) { |
| 171 | size_t i; |
| 172 | |
| 173 | /* In case it was set long ago */ |
| 174 | expect_close = 0; |
| 175 | |
| 176 | for (i = 0; i != count; i++) { |
| 177 | char c; |
| 178 | if (get_user(c, buf + i)) |
| 179 | return -EFAULT; |
| 180 | if (c == 'V') |
| 181 | expect_close = 42; |
| 182 | } |
| 183 | } |
| 184 | ibwdt_ping(); |
| 185 | } |
| 186 | return count; |
| 187 | } |
| 188 | |
| 189 | static int |
| 190 | ibwdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd, |
| 191 | unsigned long arg) |
| 192 | { |
Wim Van Sebroeck | 35d55c9 | 2007-01-27 21:50:53 +0100 | [diff] [blame] | 193 | int new_margin; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | void __user *argp = (void __user *)arg; |
| 195 | int __user *p = argp; |
| 196 | |
| 197 | static struct watchdog_info ident = { |
| 198 | .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | WDIOF_MAGICCLOSE, |
| 199 | .firmware_version = 1, |
| 200 | .identity = "IB700 WDT", |
| 201 | }; |
| 202 | |
| 203 | switch (cmd) { |
| 204 | case WDIOC_GETSUPPORT: |
| 205 | if (copy_to_user(argp, &ident, sizeof(ident))) |
| 206 | return -EFAULT; |
| 207 | break; |
| 208 | |
| 209 | case WDIOC_GETSTATUS: |
Wim Van Sebroeck | c9d7710 | 2007-01-27 22:07:03 +0100 | [diff] [blame] | 210 | case WDIOC_GETBOOTSTATUS: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | return put_user(0, p); |
| 212 | |
| 213 | case WDIOC_KEEPALIVE: |
| 214 | ibwdt_ping(); |
| 215 | break; |
| 216 | |
| 217 | case WDIOC_SETTIMEOUT: |
| 218 | if (get_user(new_margin, p)) |
| 219 | return -EFAULT; |
Wim Van Sebroeck | 35d55c9 | 2007-01-27 21:50:53 +0100 | [diff] [blame] | 220 | if (ibwdt_set_heartbeat(new_margin)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | ibwdt_ping(); |
| 223 | /* Fall */ |
| 224 | |
| 225 | case WDIOC_GETTIMEOUT: |
| 226 | return put_user(wd_times[wd_margin], p); |
Wim Van Sebroeck | e42162a | 2007-01-27 22:12:54 +0100 | [diff] [blame^] | 227 | |
| 228 | case WDIOC_SETOPTIONS: |
| 229 | { |
| 230 | int options, retval = -EINVAL; |
| 231 | |
| 232 | if (get_user(options, p)) |
| 233 | return -EFAULT; |
| 234 | |
| 235 | if (options & WDIOS_DISABLECARD) { |
| 236 | ibwdt_disable(); |
| 237 | retval = 0; |
| 238 | } |
| 239 | |
| 240 | if (options & WDIOS_ENABLECARD) { |
| 241 | ibwdt_ping(); |
| 242 | retval = 0; |
| 243 | } |
| 244 | |
| 245 | return retval; |
| 246 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | |
| 248 | default: |
Samuel Tardieu | 795b89d | 2006-09-09 17:34:31 +0200 | [diff] [blame] | 249 | return -ENOTTY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | } |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | static int |
| 255 | ibwdt_open(struct inode *inode, struct file *file) |
| 256 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | if (test_and_set_bit(0, &ibwdt_is_open)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | return -EBUSY; |
| 259 | } |
| 260 | if (nowayout) |
| 261 | __module_get(THIS_MODULE); |
| 262 | |
| 263 | /* Activate */ |
| 264 | ibwdt_ping(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | return nonseekable_open(inode, file); |
| 266 | } |
| 267 | |
| 268 | static int |
| 269 | ibwdt_close(struct inode *inode, struct file *file) |
| 270 | { |
Wim Van Sebroeck | c9d7710 | 2007-01-27 22:07:03 +0100 | [diff] [blame] | 271 | if (expect_close == 42) { |
Wim Van Sebroeck | 35d55c9 | 2007-01-27 21:50:53 +0100 | [diff] [blame] | 272 | ibwdt_disable(); |
Wim Van Sebroeck | c9d7710 | 2007-01-27 22:07:03 +0100 | [diff] [blame] | 273 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | printk(KERN_CRIT PFX "WDT device closed unexpectedly. WDT will not stop!\n"); |
Wim Van Sebroeck | c9d7710 | 2007-01-27 22:07:03 +0100 | [diff] [blame] | 275 | ibwdt_ping(); |
| 276 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | clear_bit(0, &ibwdt_is_open); |
| 278 | expect_close = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | /* |
| 283 | * Notifier for system down |
| 284 | */ |
| 285 | |
| 286 | static int |
| 287 | ibwdt_notify_sys(struct notifier_block *this, unsigned long code, |
| 288 | void *unused) |
| 289 | { |
| 290 | if (code == SYS_DOWN || code == SYS_HALT) { |
| 291 | /* Turn the WDT off */ |
Wim Van Sebroeck | 35d55c9 | 2007-01-27 21:50:53 +0100 | [diff] [blame] | 292 | ibwdt_disable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | } |
| 294 | return NOTIFY_DONE; |
| 295 | } |
| 296 | |
| 297 | /* |
| 298 | * Kernel Interfaces |
| 299 | */ |
| 300 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 301 | static const struct file_operations ibwdt_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | .owner = THIS_MODULE, |
| 303 | .llseek = no_llseek, |
| 304 | .write = ibwdt_write, |
| 305 | .ioctl = ibwdt_ioctl, |
| 306 | .open = ibwdt_open, |
| 307 | .release = ibwdt_close, |
| 308 | }; |
| 309 | |
| 310 | static struct miscdevice ibwdt_miscdev = { |
| 311 | .minor = WATCHDOG_MINOR, |
| 312 | .name = "watchdog", |
| 313 | .fops = &ibwdt_fops, |
| 314 | }; |
| 315 | |
| 316 | /* |
| 317 | * The WDT needs to learn about soft shutdowns in order to |
| 318 | * turn the timebomb registers off. |
| 319 | */ |
| 320 | |
| 321 | static struct notifier_block ibwdt_notifier = { |
| 322 | .notifier_call = ibwdt_notify_sys, |
| 323 | }; |
| 324 | |
Wim Van Sebroeck | f6e4803 | 2007-01-27 21:58:08 +0100 | [diff] [blame] | 325 | /* |
| 326 | * Init & exit routines |
| 327 | */ |
| 328 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | static int __init ibwdt_init(void) |
| 330 | { |
| 331 | int res; |
| 332 | |
| 333 | printk(KERN_INFO PFX "WDT driver for IB700 single board computer initialising.\n"); |
| 334 | |
| 335 | spin_lock_init(&ibwdt_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | |
| 337 | #if WDT_START != WDT_STOP |
| 338 | if (!request_region(WDT_STOP, 1, "IB700 WDT")) { |
| 339 | printk (KERN_ERR PFX "STOP method I/O %X is not available.\n", WDT_STOP); |
| 340 | res = -EIO; |
| 341 | goto out_nostopreg; |
| 342 | } |
| 343 | #endif |
| 344 | |
| 345 | if (!request_region(WDT_START, 1, "IB700 WDT")) { |
| 346 | printk (KERN_ERR PFX "START method I/O %X is not available.\n", WDT_START); |
| 347 | res = -EIO; |
| 348 | goto out_nostartreg; |
| 349 | } |
Wim Van Sebroeck | f6e4803 | 2007-01-27 21:58:08 +0100 | [diff] [blame] | 350 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | res = register_reboot_notifier(&ibwdt_notifier); |
| 352 | if (res) { |
| 353 | printk (KERN_ERR PFX "Failed to register reboot notifier.\n"); |
| 354 | goto out_noreboot; |
| 355 | } |
Wim Van Sebroeck | f6e4803 | 2007-01-27 21:58:08 +0100 | [diff] [blame] | 356 | |
| 357 | res = misc_register(&ibwdt_miscdev); |
| 358 | if (res) { |
| 359 | printk (KERN_ERR PFX "failed to register misc device\n"); |
| 360 | goto out_nomisc; |
| 361 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | return 0; |
| 363 | |
Wim Van Sebroeck | f6e4803 | 2007-01-27 21:58:08 +0100 | [diff] [blame] | 364 | out_nomisc: |
| 365 | unregister_reboot_notifier(&ibwdt_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | out_noreboot: |
| 367 | release_region(WDT_START, 1); |
| 368 | out_nostartreg: |
| 369 | #if WDT_START != WDT_STOP |
| 370 | release_region(WDT_STOP, 1); |
| 371 | #endif |
| 372 | out_nostopreg: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | return res; |
| 374 | } |
| 375 | |
| 376 | static void __exit |
| 377 | ibwdt_exit(void) |
| 378 | { |
| 379 | misc_deregister(&ibwdt_miscdev); |
| 380 | unregister_reboot_notifier(&ibwdt_notifier); |
Wim Van Sebroeck | f6e4803 | 2007-01-27 21:58:08 +0100 | [diff] [blame] | 381 | release_region(WDT_START,1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | #if WDT_START != WDT_STOP |
| 383 | release_region(WDT_STOP,1); |
| 384 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | module_init(ibwdt_init); |
| 388 | module_exit(ibwdt_exit); |
| 389 | |
| 390 | MODULE_AUTHOR("Charles Howes <chowes@vsol.net>"); |
| 391 | MODULE_DESCRIPTION("IB700 SBC watchdog driver"); |
| 392 | MODULE_LICENSE("GPL"); |
| 393 | MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); |
| 394 | |
| 395 | /* end of ib700wdt.c */ |