2  *      IT8712F "Smart Guardian" Watchdog support
 
   4  *      Copyright (c) 2006-2007 Jorge Boncompte - DTI2 <jorge@dti2.net>
 
   6  *      Based on info and code taken from:
 
   8  *      drivers/char/watchdog/scx200_wdt.c
 
  10  *      IT8712F EC-LPC I/O Preliminary Specification 0.9.2.pdf
 
  12  *      This program is free software; you can redistribute it and/or
 
  13  *      modify it under the terms of the GNU General Public License as
 
  14  *      published by the Free Software Foundation; either version 2 of the
 
  15  *      License, or (at your option) any later version.
 
  17  *      The author(s) of this software shall not be held liable for damages
 
  18  *      of any nature resulting due to the use of this software. This
 
  19  *      software is provided AS-IS with no warranties.
 
  22 #include <linux/module.h>
 
  23 #include <linux/moduleparam.h>
 
  24 #include <linux/init.h>
 
  25 #include <linux/miscdevice.h>
 
  26 #include <linux/watchdog.h>
 
  27 #include <linux/notifier.h>
 
  28 #include <linux/reboot.h>
 
  30 #include <linux/pci.h>
 
  31 #include <linux/spinlock.h>
 
  33 #include <asm/uaccess.h>
 
  36 #define NAME "it8712f_wdt"
 
  38 MODULE_AUTHOR("Jorge Boncompte - DTI2 <jorge@dti2.net>");
 
  39 MODULE_DESCRIPTION("IT8712F Watchdog Driver");
 
  40 MODULE_LICENSE("GPL");
 
  41 MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
 
  43 static int margin = 60;         /* in seconds */
 
  44 module_param(margin, int, 0);
 
  45 MODULE_PARM_DESC(margin, "Watchdog margin in seconds");
 
  47 static int nowayout = WATCHDOG_NOWAYOUT;
 
  48 module_param(nowayout, int, 0);
 
  49 MODULE_PARM_DESC(nowayout, "Disable watchdog shutdown on close");
 
  51 static struct semaphore it8712f_wdt_sem;
 
  52 static unsigned expect_close;
 
  53 static spinlock_t io_lock;
 
  55 /* Dog Food address - We use the game port address */
 
  56 static unsigned short address;
 
  58 #define REG             0x2e    /* The register to read/write */
 
  59 #define VAL             0x2f    /* The value to read/write */
 
  61 #define LDN             0x07    /* Register: Logical device select */
 
  62 #define DEVID           0x20    /* Register: Device ID */
 
  63 #define DEVREV          0x22    /* Register: Device Revision */
 
  64 #define ACT_REG         0x30    /* LDN Register: Activation */
 
  65 #define BASE_REG        0x60    /* LDN Register: Base address */
 
  67 #define IT8712F_DEVID   0x8712
 
  69 #define LDN_GPIO        0x07    /* GPIO and Watch Dog Timer */
 
  70 #define LDN_GAME        0x09    /* Game Port */
 
  72 #define WDT_CONTROL     0x71    /* WDT Register: Control */
 
  73 #define WDT_CONFIG      0x72    /* WDT Register: Configuration */
 
  74 #define WDT_TIMEOUT     0x73    /* WDT Register: Timeout Value */
 
  76 #define WDT_RESET_GAME  0x10
 
  77 #define WDT_RESET_KBD   0x20
 
  78 #define WDT_RESET_MOUSE 0x40
 
  79 #define WDT_RESET_CIR   0x80
 
  81 #define WDT_UNIT_SEC    0x80    /* If 0 in MINUTES */
 
  83 #define WDT_OUT_PWROK   0x10
 
  84 #define WDT_OUT_KRST    0x40
 
  94 superio_outb(int val, int reg)
 
 112 superio_select(int ldn)
 
 133         spin_unlock(&io_lock);
 
 137 it8712f_wdt_ping(void)
 
 143 it8712f_wdt_update_margin(void)
 
 145         int config = WDT_OUT_KRST | WDT_OUT_PWROK;
 
 147         printk(KERN_INFO NAME ": timer margin %d seconds\n", margin);
 
 149         /* The timeout register only has 8bits wide */
 
 151                 config |= WDT_UNIT_SEC; /* else UNIT are MINUTES */
 
 152         superio_outb(config, WDT_CONFIG);
 
 154         superio_outb((margin > 255) ? (margin / 60) : margin, WDT_TIMEOUT);
 
 158 it8712f_wdt_enable(void)
 
 160         printk(KERN_DEBUG NAME ": enabling watchdog timer\n");
 
 162         superio_select(LDN_GPIO);
 
 164         superio_outb(WDT_RESET_GAME, WDT_CONTROL);
 
 166         it8712f_wdt_update_margin();
 
 174 it8712f_wdt_disable(void)
 
 176         printk(KERN_DEBUG NAME ": disabling watchdog timer\n");
 
 179         superio_select(LDN_GPIO);
 
 181         superio_outb(0, WDT_CONFIG);
 
 182         superio_outb(0, WDT_CONTROL);
 
 183         superio_outb(0, WDT_TIMEOUT);
 
 189 it8712f_wdt_notify(struct notifier_block *this,
 
 190                     unsigned long code, void *unused)
 
 192         if (code == SYS_HALT || code == SYS_POWER_OFF)
 
 194                         it8712f_wdt_disable();
 
 199 static struct notifier_block it8712f_wdt_notifier = {
 
 200         .notifier_call = it8712f_wdt_notify,
 
 204 it8712f_wdt_write(struct file *file, const char __user *data,
 
 205         size_t len, loff_t *ppos)
 
 207         /* check for a magic close character */
 
 214                 for (i = 0; i < len; ++i) {
 
 216                         if (get_user(c, data+i))
 
 227 it8712f_wdt_ioctl(struct inode *inode, struct file *file,
 
 228         unsigned int cmd, unsigned long arg)
 
 230         void __user *argp = (void __user *)arg;
 
 231         int __user *p = argp;
 
 232         static struct watchdog_info ident = {
 
 233                 .identity = "IT8712F Watchdog",
 
 234                 .firmware_version = 1,
 
 235                 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING,
 
 242         case WDIOC_GETSUPPORT:
 
 243                 if (copy_to_user(argp, &ident, sizeof(ident)))
 
 246         case WDIOC_GETSTATUS:
 
 247         case WDIOC_GETBOOTSTATUS:
 
 248                 return put_user(0, p);
 
 249         case WDIOC_KEEPALIVE:
 
 252         case WDIOC_SETTIMEOUT:
 
 253                 if (get_user(new_margin, p))
 
 259                 superio_select(LDN_GPIO);
 
 261                 it8712f_wdt_update_margin();
 
 265         case WDIOC_GETTIMEOUT:
 
 266                 if (put_user(margin, p))
 
 273 it8712f_wdt_open(struct inode *inode, struct file *file)
 
 275         /* only allow one at a time */
 
 276         if (down_trylock(&it8712f_wdt_sem))
 
 278         it8712f_wdt_enable();
 
 280         return nonseekable_open(inode, file);
 
 284 it8712f_wdt_release(struct inode *inode, struct file *file)
 
 286         if (expect_close != 42) {
 
 287                 printk(KERN_WARNING NAME
 
 288                         ": watchdog device closed unexpectedly, will not"
 
 289                         " disable the watchdog timer\n");
 
 290         } else if (!nowayout) {
 
 291                 it8712f_wdt_disable();
 
 294         up(&it8712f_wdt_sem);
 
 299 static const struct file_operations it8712f_wdt_fops = {
 
 300         .owner = THIS_MODULE,
 
 302         .write = it8712f_wdt_write,
 
 303         .ioctl = it8712f_wdt_ioctl,
 
 304         .open = it8712f_wdt_open,
 
 305         .release = it8712f_wdt_release,
 
 308 static struct miscdevice it8712f_wdt_miscdev = {
 
 309         .minor = WATCHDOG_MINOR,
 
 311         .fops = &it8712f_wdt_fops,
 
 315 it8712f_wdt_find(unsigned short *address)
 
 321         chip_type = superio_inw(DEVID);
 
 322         if (chip_type != IT8712F_DEVID)
 
 325         superio_select(LDN_GAME);
 
 326         superio_outb(1, ACT_REG);
 
 327         if (!(superio_inb(ACT_REG) & 0x01)) {
 
 328                 printk(KERN_ERR NAME ": Device not activated, skipping\n");
 
 332         *address = superio_inw(BASE_REG);
 
 334                 printk(KERN_ERR NAME ": Base address not set, skipping\n");
 
 339         printk(KERN_DEBUG NAME ": Found IT%04xF chip revision %d - "
 
 340                 "using DogFood address 0x%x\n",
 
 341                 chip_type, superio_inb(DEVREV) & 0x0f, *address);
 
 349 it8712f_wdt_init(void)
 
 353         spin_lock_init(&io_lock);
 
 355         if (it8712f_wdt_find(&address))
 
 358         if (!request_region(address, 1, "IT8712F Watchdog")) {
 
 359                 printk(KERN_WARNING NAME ": watchdog I/O region busy\n");
 
 363         it8712f_wdt_disable();
 
 365         sema_init(&it8712f_wdt_sem, 1);
 
 367         err = register_reboot_notifier(&it8712f_wdt_notifier);
 
 369                 printk(KERN_ERR NAME ": unable to register reboot notifier\n");
 
 373         err = misc_register(&it8712f_wdt_miscdev);
 
 376                         ": cannot register miscdev on minor=%d (err=%d)\n",
 
 377                         WATCHDOG_MINOR, err);
 
 385         unregister_reboot_notifier(&it8712f_wdt_notifier);
 
 387         release_region(address, 1);
 
 392 it8712f_wdt_exit(void)
 
 394         misc_deregister(&it8712f_wdt_miscdev);
 
 395         unregister_reboot_notifier(&it8712f_wdt_notifier);
 
 396         release_region(address, 1);
 
 399 module_init(it8712f_wdt_init);
 
 400 module_exit(it8712f_wdt_exit);