machzwd.c revision 9ffc93f203c18a70623f21950f1dd473c9ec48cd
1/* 2 * MachZ ZF-Logic Watchdog Timer driver for Linux 3 * 4 * 5 * This program is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU General Public License 7 * as published by the Free Software Foundation; either version 8 * 2 of the License, or (at your option) any later version. 9 * 10 * The author does NOT admit liability nor provide warranty for 11 * any of this software. This material is provided "AS-IS" in 12 * the hope that it may be useful for others. 13 * 14 * Author: Fernando Fuganti <fuganti@conectiva.com.br> 15 * 16 * Based on sbc60xxwdt.c by Jakob Oestergaard 17 * 18 * 19 * We have two timers (wd#1, wd#2) driven by a 32 KHz clock with the 20 * following periods: 21 * wd#1 - 2 seconds; 22 * wd#2 - 7.2 ms; 23 * After the expiration of wd#1, it can generate a NMI, SCI, SMI, or 24 * a system RESET and it starts wd#2 that unconditionally will RESET 25 * the system when the counter reaches zero. 26 * 27 * 14-Dec-2001 Matt Domsch <Matt_Domsch@dell.com> 28 * Added nowayout module option to override CONFIG_WATCHDOG_NOWAYOUT 29 */ 30 31#include <linux/module.h> 32#include <linux/moduleparam.h> 33#include <linux/types.h> 34#include <linux/timer.h> 35#include <linux/jiffies.h> 36#include <linux/miscdevice.h> 37#include <linux/watchdog.h> 38#include <linux/fs.h> 39#include <linux/ioport.h> 40#include <linux/notifier.h> 41#include <linux/reboot.h> 42#include <linux/init.h> 43#include <linux/io.h> 44#include <linux/uaccess.h> 45 46 47/* ports */ 48#define ZF_IOBASE 0x218 49#define INDEX 0x218 50#define DATA_B 0x219 51#define DATA_W 0x21A 52#define DATA_D 0x21A 53 54/* indexes */ /* size */ 55#define ZFL_VERSION 0x02 /* 16 */ 56#define CONTROL 0x10 /* 16 */ 57#define STATUS 0x12 /* 8 */ 58#define COUNTER_1 0x0C /* 16 */ 59#define COUNTER_2 0x0E /* 8 */ 60#define PULSE_LEN 0x0F /* 8 */ 61 62/* controls */ 63#define ENABLE_WD1 0x0001 64#define ENABLE_WD2 0x0002 65#define RESET_WD1 0x0010 66#define RESET_WD2 0x0020 67#define GEN_SCI 0x0100 68#define GEN_NMI 0x0200 69#define GEN_SMI 0x0400 70#define GEN_RESET 0x0800 71 72 73/* utilities */ 74 75#define WD1 0 76#define WD2 1 77 78#define zf_writew(port, data) { outb(port, INDEX); outw(data, DATA_W); } 79#define zf_writeb(port, data) { outb(port, INDEX); outb(data, DATA_B); } 80#define zf_get_ZFL_version() zf_readw(ZFL_VERSION) 81 82 83static unsigned short zf_readw(unsigned char port) 84{ 85 outb(port, INDEX); 86 return inw(DATA_W); 87} 88 89 90MODULE_AUTHOR("Fernando Fuganti <fuganti@conectiva.com.br>"); 91MODULE_DESCRIPTION("MachZ ZF-Logic Watchdog driver"); 92MODULE_LICENSE("GPL"); 93MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); 94 95static int nowayout = WATCHDOG_NOWAYOUT; 96module_param(nowayout, int, 0); 97MODULE_PARM_DESC(nowayout, 98 "Watchdog cannot be stopped once started (default=" 99 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); 100 101#define PFX "machzwd" 102 103static const struct watchdog_info zf_info = { 104 .options = WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE, 105 .firmware_version = 1, 106 .identity = "ZF-Logic watchdog", 107}; 108 109 110/* 111 * action refers to action taken when watchdog resets 112 * 0 = GEN_RESET 113 * 1 = GEN_SMI 114 * 2 = GEN_NMI 115 * 3 = GEN_SCI 116 * defaults to GEN_RESET (0) 117 */ 118static int action; 119module_param(action, int, 0); 120MODULE_PARM_DESC(action, "after watchdog resets, generate: " 121 "0 = RESET(*) 1 = SMI 2 = NMI 3 = SCI"); 122 123static void zf_ping(unsigned long data); 124 125static int zf_action = GEN_RESET; 126static unsigned long zf_is_open; 127static char zf_expect_close; 128static DEFINE_SPINLOCK(zf_port_lock); 129static DEFINE_TIMER(zf_timer, zf_ping, 0, 0); 130static unsigned long next_heartbeat; 131 132 133/* timeout for user land heart beat (10 seconds) */ 134#define ZF_USER_TIMEO (HZ*10) 135 136/* timeout for hardware watchdog (~500ms) */ 137#define ZF_HW_TIMEO (HZ/2) 138 139/* number of ticks on WD#1 (driven by a 32KHz clock, 2s) */ 140#define ZF_CTIMEOUT 0xffff 141 142#ifndef ZF_DEBUG 143# define dprintk(format, args...) 144#else 145# define dprintk(format, args...) printk(KERN_DEBUG PFX \ 146 ":%s:%d: " format, __func__, __LINE__ , ## args) 147#endif 148 149 150static inline void zf_set_status(unsigned char new) 151{ 152 zf_writeb(STATUS, new); 153} 154 155 156/* CONTROL register functions */ 157 158static inline unsigned short zf_get_control(void) 159{ 160 return zf_readw(CONTROL); 161} 162 163static inline void zf_set_control(unsigned short new) 164{ 165 zf_writew(CONTROL, new); 166} 167 168 169/* WD#? counter functions */ 170/* 171 * Just set counter value 172 */ 173 174static inline void zf_set_timer(unsigned short new, unsigned char n) 175{ 176 switch (n) { 177 case WD1: 178 zf_writew(COUNTER_1, new); 179 case WD2: 180 zf_writeb(COUNTER_2, new > 0xff ? 0xff : new); 181 default: 182 return; 183 } 184} 185 186/* 187 * stop hardware timer 188 */ 189static void zf_timer_off(void) 190{ 191 unsigned int ctrl_reg = 0; 192 unsigned long flags; 193 194 /* stop internal ping */ 195 del_timer_sync(&zf_timer); 196 197 spin_lock_irqsave(&zf_port_lock, flags); 198 /* stop watchdog timer */ 199 ctrl_reg = zf_get_control(); 200 ctrl_reg |= (ENABLE_WD1|ENABLE_WD2); /* disable wd1 and wd2 */ 201 ctrl_reg &= ~(ENABLE_WD1|ENABLE_WD2); 202 zf_set_control(ctrl_reg); 203 spin_unlock_irqrestore(&zf_port_lock, flags); 204 205 printk(KERN_INFO PFX ": Watchdog timer is now disabled\n"); 206} 207 208 209/* 210 * start hardware timer 211 */ 212static void zf_timer_on(void) 213{ 214 unsigned int ctrl_reg = 0; 215 unsigned long flags; 216 217 spin_lock_irqsave(&zf_port_lock, flags); 218 219 zf_writeb(PULSE_LEN, 0xff); 220 221 zf_set_timer(ZF_CTIMEOUT, WD1); 222 223 /* user land ping */ 224 next_heartbeat = jiffies + ZF_USER_TIMEO; 225 226 /* start the timer for internal ping */ 227 mod_timer(&zf_timer, jiffies + ZF_HW_TIMEO); 228 229 /* start watchdog timer */ 230 ctrl_reg = zf_get_control(); 231 ctrl_reg |= (ENABLE_WD1|zf_action); 232 zf_set_control(ctrl_reg); 233 spin_unlock_irqrestore(&zf_port_lock, flags); 234 235 printk(KERN_INFO PFX ": Watchdog timer is now enabled\n"); 236} 237 238 239static void zf_ping(unsigned long data) 240{ 241 unsigned int ctrl_reg = 0; 242 unsigned long flags; 243 244 zf_writeb(COUNTER_2, 0xff); 245 246 if (time_before(jiffies, next_heartbeat)) { 247 dprintk("time_before: %ld\n", next_heartbeat - jiffies); 248 /* 249 * reset event is activated by transition from 0 to 1 on 250 * RESET_WD1 bit and we assume that it is already zero... 251 */ 252 253 spin_lock_irqsave(&zf_port_lock, flags); 254 ctrl_reg = zf_get_control(); 255 ctrl_reg |= RESET_WD1; 256 zf_set_control(ctrl_reg); 257 258 /* ...and nothing changes until here */ 259 ctrl_reg &= ~(RESET_WD1); 260 zf_set_control(ctrl_reg); 261 spin_unlock_irqrestore(&zf_port_lock, flags); 262 263 mod_timer(&zf_timer, jiffies + ZF_HW_TIMEO); 264 } else 265 printk(KERN_CRIT PFX ": I will reset your machine\n"); 266} 267 268static ssize_t zf_write(struct file *file, const char __user *buf, size_t count, 269 loff_t *ppos) 270{ 271 /* See if we got the magic character */ 272 if (count) { 273 /* 274 * no need to check for close confirmation 275 * no way to disable watchdog ;) 276 */ 277 if (!nowayout) { 278 size_t ofs; 279 /* 280 * note: just in case someone wrote the magic character 281 * five months ago... 282 */ 283 zf_expect_close = 0; 284 285 /* now scan */ 286 for (ofs = 0; ofs != count; ofs++) { 287 char c; 288 if (get_user(c, buf + ofs)) 289 return -EFAULT; 290 if (c == 'V') { 291 zf_expect_close = 42; 292 dprintk("zf_expect_close = 42\n"); 293 } 294 } 295 } 296 297 /* 298 * Well, anyhow someone wrote to us, 299 * we should return that favour 300 */ 301 next_heartbeat = jiffies + ZF_USER_TIMEO; 302 dprintk("user ping at %ld\n", jiffies); 303 } 304 return count; 305} 306 307static long zf_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 308{ 309 void __user *argp = (void __user *)arg; 310 int __user *p = argp; 311 switch (cmd) { 312 case WDIOC_GETSUPPORT: 313 if (copy_to_user(argp, &zf_info, sizeof(zf_info))) 314 return -EFAULT; 315 break; 316 case WDIOC_GETSTATUS: 317 case WDIOC_GETBOOTSTATUS: 318 return put_user(0, p); 319 case WDIOC_KEEPALIVE: 320 zf_ping(0); 321 break; 322 default: 323 return -ENOTTY; 324 } 325 return 0; 326} 327 328static int zf_open(struct inode *inode, struct file *file) 329{ 330 if (test_and_set_bit(0, &zf_is_open)) 331 return -EBUSY; 332 if (nowayout) 333 __module_get(THIS_MODULE); 334 zf_timer_on(); 335 return nonseekable_open(inode, file); 336} 337 338static int zf_close(struct inode *inode, struct file *file) 339{ 340 if (zf_expect_close == 42) 341 zf_timer_off(); 342 else { 343 del_timer(&zf_timer); 344 printk(KERN_ERR PFX ": device file closed unexpectedly. " 345 "Will not stop the WDT!\n"); 346 } 347 clear_bit(0, &zf_is_open); 348 zf_expect_close = 0; 349 return 0; 350} 351 352/* 353 * Notifier for system down 354 */ 355 356static int zf_notify_sys(struct notifier_block *this, unsigned long code, 357 void *unused) 358{ 359 if (code == SYS_DOWN || code == SYS_HALT) 360 zf_timer_off(); 361 return NOTIFY_DONE; 362} 363 364static const struct file_operations zf_fops = { 365 .owner = THIS_MODULE, 366 .llseek = no_llseek, 367 .write = zf_write, 368 .unlocked_ioctl = zf_ioctl, 369 .open = zf_open, 370 .release = zf_close, 371}; 372 373static struct miscdevice zf_miscdev = { 374 .minor = WATCHDOG_MINOR, 375 .name = "watchdog", 376 .fops = &zf_fops, 377}; 378 379 380/* 381 * The device needs to learn about soft shutdowns in order to 382 * turn the timebomb registers off. 383 */ 384static struct notifier_block zf_notifier = { 385 .notifier_call = zf_notify_sys, 386}; 387 388static void __init zf_show_action(int act) 389{ 390 static const char * const str[] = { "RESET", "SMI", "NMI", "SCI" }; 391 392 printk(KERN_INFO PFX ": Watchdog using action = %s\n", str[act]); 393} 394 395static int __init zf_init(void) 396{ 397 int ret; 398 399 printk(KERN_INFO PFX 400 ": MachZ ZF-Logic Watchdog driver initializing.\n"); 401 402 ret = zf_get_ZFL_version(); 403 if (!ret || ret == 0xffff) { 404 printk(KERN_WARNING PFX ": no ZF-Logic found\n"); 405 return -ENODEV; 406 } 407 408 if (action <= 3 && action >= 0) 409 zf_action = zf_action >> action; 410 else 411 action = 0; 412 413 zf_show_action(action); 414 415 if (!request_region(ZF_IOBASE, 3, "MachZ ZFL WDT")) { 416 printk(KERN_ERR "cannot reserve I/O ports at %d\n", 417 ZF_IOBASE); 418 ret = -EBUSY; 419 goto no_region; 420 } 421 422 ret = register_reboot_notifier(&zf_notifier); 423 if (ret) { 424 printk(KERN_ERR "can't register reboot notifier (err=%d)\n", 425 ret); 426 goto no_reboot; 427 } 428 429 ret = misc_register(&zf_miscdev); 430 if (ret) { 431 printk(KERN_ERR "can't misc_register on minor=%d\n", 432 WATCHDOG_MINOR); 433 goto no_misc; 434 } 435 436 zf_set_status(0); 437 zf_set_control(0); 438 439 return 0; 440 441no_misc: 442 unregister_reboot_notifier(&zf_notifier); 443no_reboot: 444 release_region(ZF_IOBASE, 3); 445no_region: 446 return ret; 447} 448 449 450static void __exit zf_exit(void) 451{ 452 zf_timer_off(); 453 454 misc_deregister(&zf_miscdev); 455 unregister_reboot_notifier(&zf_notifier); 456 release_region(ZF_IOBASE, 3); 457} 458 459module_init(zf_init); 460module_exit(zf_exit); 461