Index: linux/drivers/char/watchdog/Kconfig =================================================================== --- linux.orig/drivers/char/watchdog/Kconfig +++ linux/drivers/char/watchdog/Kconfig @@ -327,6 +327,21 @@ config ITCO_VENDOR_SUPPORT devices. At this moment we only have additional support for some SuperMicro Inc. motherboards. +config IT87_WDT + tristate "IT87xx Watchdog Timer" + depends on WATCHDOG && X86 + ---help--- + This is the driver for the hardware watchdog on the IT87xx chipsets + as used in Asus A8N-SLI motherboards (and likely others). This + watchdog simply watches your kernel to make sure it doesn't freeze, + and if it does, it reboots your computer after a certain amount of + time. + + To compile this driver as a module, choose M here: the + module will be called it87_wdt. + + Most people will say N. + config SC1200_WDT tristate "National Semiconductor PC87307/PC97307 (ala SC1200) Watchdog" depends on WATCHDOG && X86 Index: linux/drivers/char/watchdog/Makefile =================================================================== --- linux.orig/drivers/char/watchdog/Makefile +++ linux/drivers/char/watchdog/Makefile @@ -47,6 +47,7 @@ obj-$(CONFIG_IBMASR) += ibmasr.o obj-$(CONFIG_WAFER_WDT) += wafer5823wdt.o obj-$(CONFIG_I6300ESB_WDT) += i6300esb.o obj-$(CONFIG_ITCO_WDT) += iTCO_wdt.o iTCO_vendor_support.o +obj-$(CONFIG_IT87_WDT) += it87_wdt.o obj-$(CONFIG_SC1200_WDT) += sc1200wdt.o obj-$(CONFIG_SCx200_WDT) += scx200_wdt.o obj-$(CONFIG_PC87413_WDT) += pc87413_wdt.o Index: linux/drivers/char/watchdog/it87_wdt.c =================================================================== --- /dev/null +++ linux/drivers/char/watchdog/it87_wdt.c @@ -0,0 +1,399 @@ +/* + * it87xx WDT driver + * + * (c) Copyright 2007 Nicholas Kain + * + * Based on wdt.c::advantechwdt.c::w83627hf_wdt.c. + * Original copyright messages: + * + * (c) Copyright 2003 Pádraig Brady + * (c) Copyright 2000-2001 Marek Michalkiewicz + * (c) Copyright 1996 Alan Cox , All Rights Reserved. + * http://www.redhat.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version + * 2 of the License, or (at your option) any later version. + * + * Neither Alan Cox nor CymruNet Ltd. admit liability nor provide + * warranty for any of this software. This material is provided + * "AS-IS" and at no charge. + * + * (c) Copyright 1995 Alan Cox + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#define WATCHDOG_NAME "it87 WDT" +#define PFX WATCHDOG_NAME ": " +#define WATCHDOG_TIMEOUT 60 /* 60 sec default timeout */ + +static unsigned long wdt_is_open; +static char expect_close; +static spinlock_t io_lock; + +#define DEV 0x07 /* Register: Logical device select */ +#define GPIO 0x07 /* The device with the WDT control registers */ +#define DEVID 0x20 /* Register: Device ID */ +#define DEVREV 0x22 /* Register: Device Revision */ +#define WD_CTRL 0x71 /* Register: Watchdog Control */ +#define WD_CFG 0x72 /* Register: Watchdog Configuration */ +#define WD_TIMEOUT 0x73 /* Register: Watchdog Timeout */ +#define REG 0x2e /* The register to read/write */ +#define VAL 0x2f /* The value to read/write */ + +static int timeout = WATCHDOG_TIMEOUT; /* in seconds */ +module_param(timeout, int, 0); +MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds. 1<= timeout <=63, default=" __MODULE_STRING(WATCHDOG_TIMEOUT) "."); + +static int nowayout = WATCHDOG_NOWAYOUT; +module_param(nowayout, int, 0); +MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default=" __MODULE_STRING(WATCHDOG_NOWAYOUT) ")"); + +static int +it87_inb(int reg) +{ + outb(reg, REG); + return inb(VAL); +} + +static void +it87_outb(int reg, unsigned char val) +{ + outb(reg, REG); + outb(val, VAL); +} + +static int +it87_inw(int reg) +{ + int val; + + val = it87_inb(reg++); + val |= it87_inb(reg) >> 8; + return val; +} + +static void +it87_outw(int reg, unsigned short val) +{ + it87_outb(reg++, val & 0xff); + it87_outb(reg, (val & 0xff00) >> 8); +} + +static void +it87_enter(void) +{ + spin_lock(&io_lock); + outb(0x87, REG); + outb(0x01, REG); + outb(0x55, REG); + outb(0x55, REG); +} + +static void +it87_select(unsigned char ldn) +{ + outb(DEV, REG); + outb(ldn, VAL); +} + +static void +it87_exit(void) +{ + outb(0x02, REG); + outb(0x02, VAL); + spin_unlock(&io_lock); +} + +static void +it87_init(void) +{ + int t; + + it87_enter(); + it87_select(GPIO); + + /* If watchdog is already running, reset the timer. */ + t = it87_inw(WD_TIMEOUT); + if (t) { + printk(KERN_INFO PFX "already running. Resetting timeout to " + "%d sec\n", timeout); + it87_outw(WD_TIMEOUT, timeout); + } + + it87_outb(WD_CTRL, 0x00); /* Disable interrupts resetting watchdog */ + it87_outb(WD_CFG, 0x80); /* Timeout value is in seconds */ + it87_exit(); +} + +static void +wdt_ctrl(int timeout) +{ + it87_enter(); + it87_select(GPIO); + it87_outw(WD_TIMEOUT, timeout); + it87_exit(); +} + +static int +wdt_ping(void) +{ + wdt_ctrl(timeout); + return 0; +} + +static int +wdt_disable(void) +{ + wdt_ctrl(0); + return 0; +} + +static int +wdt_set_heartbeat(int t) +{ + if ((t < 1) || (t > 36635)) /* Odd, I know, but following data sheet */ + return -EINVAL; + + timeout = t; + return 0; +} + +static ssize_t +wdt_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) +{ + if (count) { + if (!nowayout) { + size_t i; + + expect_close = 0; + + for (i = 0; i != count; i++) { + char c; + if (get_user(c, buf+i)) + return -EFAULT; + if (c == 'V') + expect_close = 42; + } + } + wdt_ping(); + } + return count; +} + +static int +wdt_ioctl(struct inode *inode, struct file *file, unsigned int cmd, + unsigned long arg) +{ + void __user *argp = (void __user *)arg; + int __user *p = argp; + int new_timeout; + static struct watchdog_info ident = { + .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT | + WDIOF_MAGICCLOSE, + .firmware_version = 1, + .identity = "IT87 WDT", + }; + + switch (cmd) { + case WDIOC_GETSUPPORT: + if (copy_to_user(argp, &ident, sizeof(ident))) + return -EFAULT; + break; + + case WDIOC_GETSTATUS: + case WDIOC_GETBOOTSTATUS: + return put_user(0, p); + + case WDIOC_KEEPALIVE: + wdt_ping(); + break; + + case WDIOC_SETTIMEOUT: + if (get_user(new_timeout, p)) + return -EFAULT; + if (wdt_set_heartbeat(new_timeout)) + return -EINVAL; + wdt_ping(); + /* Fall */ + + case WDIOC_GETTIMEOUT: + return put_user(timeout, p); + + case WDIOC_SETOPTIONS: + { + int options, retval = -EINVAL; + + if (get_user(options, p)) + return -EFAULT; + + if (options & WDIOS_DISABLECARD) { + wdt_disable(); + retval = 0; + } + + if (options & WDIOS_ENABLECARD) { + wdt_ping(); + retval = 0; + } + + return retval; + } + + default: + return -ENOTTY; + } + return 0; +} + +static int +wdt_open(struct inode *inode, struct file *file) +{ + if (test_and_set_bit(0, &wdt_is_open)) + return -EBUSY; + + wdt_ping(); /* Activate */ + return nonseekable_open(inode, file); +} + +static int +wdt_close(struct inode *inode, struct file *file) +{ + if (expect_close == 42) { + wdt_disable(); + } else { + printk(KERN_CRIT PFX "Unexpected close, not stopping " + "watchdog!\n"); + wdt_ping(); + } + expect_close = 0; + clear_bit(0, &wdt_is_open); + return 0; +} + +/* + * Notifier for system down + */ + +static int +wdt_notify_sys(struct notifier_block *this, unsigned long code, + void *unused) +{ + if (code == SYS_DOWN || code == SYS_HALT) { + /* Turn the WDT off */ + wdt_disable(); + } + return NOTIFY_DONE; +} + +/* + * Kernel Interfaces + */ + +static const struct file_operations wdt_fops = { + .owner = THIS_MODULE, + .llseek = no_llseek, + .write = wdt_write, + .ioctl = wdt_ioctl, + .open = wdt_open, + .release = wdt_close, +}; + +static struct miscdevice wdt_miscdev = { + .minor = WATCHDOG_MINOR, + .name = "watchdog", + .fops = &wdt_fops, +}; + +/* + * The WDT needs to learn about soft shutdowns in order to + * turn the timebomb registers off. + */ + +static struct notifier_block wdt_notifier = { + .notifier_call = wdt_notify_sys, +}; + +static int __init +wdt_init(void) +{ + int ret; + + spin_lock_init(&io_lock); + + printk(KERN_INFO "WDT driver for the ITE87xx Super I/O chip " + "initialising.\n"); + + if (wdt_set_heartbeat(timeout)) { + wdt_set_heartbeat(WATCHDOG_TIMEOUT); + printk (KERN_INFO PFX "timeout value must be 1<=timeout<=63, " + "using %d\n", WATCHDOG_TIMEOUT); + } + + if (!request_region(REG, 1, WATCHDOG_NAME)) { + printk (KERN_ERR PFX "I/O address 0x%04x already in use\n", + REG); + ret = -EIO; + goto out; + } + + it87_init(); + + ret = register_reboot_notifier(&wdt_notifier); + if (ret) { + printk (KERN_ERR PFX "cannot register reboot notifier " + "(err=%d)\n", ret); + goto unreg_regions; + } + + ret = misc_register(&wdt_miscdev); + if (ret) { + printk (KERN_ERR PFX "cannot register miscdev on minor=%d " + "(err=%d)\n", WATCHDOG_MINOR, ret); + goto unreg_reboot; + } + + printk (KERN_INFO PFX "initialized. timeout=%d sec (nowayout=%d)\n", + timeout, nowayout); + +out: + return ret; +unreg_reboot: + unregister_reboot_notifier(&wdt_notifier); +unreg_regions: + release_region(REG, 1); + goto out; +} + +static void __exit +wdt_exit(void) +{ + misc_deregister(&wdt_miscdev); + unregister_reboot_notifier(&wdt_notifier); + release_region(REG, 1); +} + +module_init(wdt_init); +module_exit(wdt_exit); + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Nicholas Kain "); +MODULE_DESCRIPTION("it87 WDT driver"); +MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR); +