linux/drivers/watchdog/pc87413_wdt.c
<<
>>
Prefs
   1/*
   2 *      NS pc87413-wdt Watchdog Timer driver for Linux 2.6.x.x
   3 *
   4 *      This code is based on wdt.c with original copyright.
   5 *
   6 *      (C) Copyright 2006 Sven Anders, <anders@anduras.de>
   7 *                     and Marcus Junker, <junker@anduras.de>
   8 *
   9 *      This program is free software; you can redistribute it and/or
  10 *      modify it under the terms of the GNU General Public License
  11 *      as published by the Free Software Foundation; either version
  12 *      2 of the License, or (at your option) any later version.
  13 *
  14 *      Neither Sven Anders, Marcus Junker nor ANDURAS AG
  15 *      admit liability nor provide warranty for any of this software.
  16 *      This material is provided "AS-IS" and at no charge.
  17 *
  18 *      Release 1.1
  19 */
  20
  21#include <linux/module.h>
  22#include <linux/types.h>
  23#include <linux/miscdevice.h>
  24#include <linux/watchdog.h>
  25#include <linux/ioport.h>
  26#include <linux/delay.h>
  27#include <linux/notifier.h>
  28#include <linux/fs.h>
  29#include <linux/reboot.h>
  30#include <linux/init.h>
  31#include <linux/spinlock.h>
  32#include <linux/moduleparam.h>
  33#include <linux/io.h>
  34#include <linux/uaccess.h>
  35
  36#include <asm/system.h>
  37
  38/* #define DEBUG 1 */
  39
  40#define DEFAULT_TIMEOUT     1           /* 1 minute */
  41#define MAX_TIMEOUT         255
  42
  43#define VERSION             "1.1"
  44#define MODNAME             "pc87413 WDT"
  45#define PFX                 MODNAME ": "
  46#define DPFX                MODNAME " - DEBUG: "
  47
  48#define WDT_INDEX_IO_PORT   (io+0)      /* I/O port base (index register) */
  49#define WDT_DATA_IO_PORT    (WDT_INDEX_IO_PORT+1)
  50#define SWC_LDN             0x04
  51#define SIOCFG2             0x22        /* Serial IO register */
  52#define WDCTL               0x10        /* Watchdog-Timer-Controll-Register */
  53#define WDTO                0x11        /* Watchdog timeout register */
  54#define WDCFG               0x12        /* Watchdog config register */
  55
  56#define IO_DEFAULT      0x2E            /* Address used on Portwell Boards */
  57
  58static int io = IO_DEFAULT;
  59
  60static int timeout = DEFAULT_TIMEOUT;   /* timeout value */
  61static unsigned long timer_enabled;     /* is the timer enabled? */
  62
  63static char expect_close;               /* is the close expected? */
  64
  65static DEFINE_SPINLOCK(io_lock);        /* to guard us from io races */
  66
  67static int nowayout = WATCHDOG_NOWAYOUT;
  68
  69/* -- Low level function ----------------------------------------*/
  70
  71/* Select pins for Watchdog output */
  72
  73static inline void pc87413_select_wdt_out(void)
  74{
  75        unsigned int cr_data = 0;
  76
  77        /* Step 1: Select multiple pin,pin55,as WDT output */
  78
  79        outb_p(SIOCFG2, WDT_INDEX_IO_PORT);
  80
  81        cr_data = inb(WDT_DATA_IO_PORT);
  82
  83        cr_data |= 0x80; /* Set Bit7 to 1*/
  84        outb_p(SIOCFG2, WDT_INDEX_IO_PORT);
  85
  86        outb_p(cr_data, WDT_DATA_IO_PORT);
  87
  88#ifdef DEBUG
  89        printk(KERN_INFO DPFX
  90                "Select multiple pin,pin55,as WDT output: Bit7 to 1: %d\n",
  91                                                                cr_data);
  92#endif
  93}
  94
  95/* Enable SWC functions */
  96
  97static inline void pc87413_enable_swc(void)
  98{
  99        unsigned int cr_data = 0;
 100
 101        /* Step 2: Enable SWC functions */
 102
 103        outb_p(0x07, WDT_INDEX_IO_PORT);        /* Point SWC_LDN (LDN=4) */
 104        outb_p(SWC_LDN, WDT_DATA_IO_PORT);
 105
 106        outb_p(0x30, WDT_INDEX_IO_PORT);        /* Read Index 0x30 First */
 107        cr_data = inb(WDT_DATA_IO_PORT);
 108        cr_data |= 0x01;                        /* Set Bit0 to 1 */
 109        outb_p(0x30, WDT_INDEX_IO_PORT);
 110        outb_p(cr_data, WDT_DATA_IO_PORT);      /* Index0x30_bit0P1 */
 111
 112#ifdef DEBUG
 113        printk(KERN_INFO DPFX "pc87413 - Enable SWC functions\n");
 114#endif
 115}
 116
 117/* Read SWC I/O base address */
 118
 119static inline unsigned int pc87413_get_swc_base(void)
 120{
 121        unsigned int  swc_base_addr = 0;
 122        unsigned char addr_l, addr_h = 0;
 123
 124        /* Step 3: Read SWC I/O Base Address */
 125
 126        outb_p(0x60, WDT_INDEX_IO_PORT);        /* Read Index 0x60 */
 127        addr_h = inb(WDT_DATA_IO_PORT);
 128
 129        outb_p(0x61, WDT_INDEX_IO_PORT);        /* Read Index 0x61 */
 130
 131        addr_l = inb(WDT_DATA_IO_PORT);
 132
 133        swc_base_addr = (addr_h << 8) + addr_l;
 134#ifdef DEBUG
 135        printk(KERN_INFO DPFX
 136                "Read SWC I/O Base Address: low %d, high %d, res %d\n",
 137                                                addr_l, addr_h, swc_base_addr);
 138#endif
 139        return swc_base_addr;
 140}
 141
 142/* Select Bank 3 of SWC */
 143
 144static inline void pc87413_swc_bank3(unsigned int swc_base_addr)
 145{
 146        /* Step 4: Select Bank3 of SWC */
 147        outb_p(inb(swc_base_addr + 0x0f) | 0x03, swc_base_addr + 0x0f);
 148#ifdef DEBUG
 149        printk(KERN_INFO DPFX "Select Bank3 of SWC\n");
 150#endif
 151}
 152
 153/* Set watchdog timeout to x minutes */
 154
 155static inline void pc87413_programm_wdto(unsigned int swc_base_addr,
 156                                         char pc87413_time)
 157{
 158        /* Step 5: Programm WDTO, Twd. */
 159        outb_p(pc87413_time, swc_base_addr + WDTO);
 160#ifdef DEBUG
 161        printk(KERN_INFO DPFX "Set WDTO to %d minutes\n", pc87413_time);
 162#endif
 163}
 164
 165/* Enable WDEN */
 166
 167static inline void pc87413_enable_wden(unsigned int swc_base_addr)
 168{
 169        /* Step 6: Enable WDEN */
 170        outb_p(inb(swc_base_addr + WDCTL) | 0x01, swc_base_addr + WDCTL);
 171#ifdef DEBUG
 172        printk(KERN_INFO DPFX "Enable WDEN\n");
 173#endif
 174}
 175
 176/* Enable SW_WD_TREN */
 177static inline void pc87413_enable_sw_wd_tren(unsigned int swc_base_addr)
 178{
 179        /* Enable SW_WD_TREN */
 180        outb_p(inb(swc_base_addr + WDCFG) | 0x80, swc_base_addr + WDCFG);
 181#ifdef DEBUG
 182        printk(KERN_INFO DPFX "Enable SW_WD_TREN\n");
 183#endif
 184}
 185
 186/* Disable SW_WD_TREN */
 187
 188static inline void pc87413_disable_sw_wd_tren(unsigned int swc_base_addr)
 189{
 190        /* Disable SW_WD_TREN */
 191        outb_p(inb(swc_base_addr + WDCFG) & 0x7f, swc_base_addr + WDCFG);
 192#ifdef DEBUG
 193        printk(KERN_INFO DPFX "pc87413 - Disable SW_WD_TREN\n");
 194#endif
 195}
 196
 197/* Enable SW_WD_TRG */
 198
 199static inline void pc87413_enable_sw_wd_trg(unsigned int swc_base_addr)
 200{
 201        /* Enable SW_WD_TRG */
 202        outb_p(inb(swc_base_addr + WDCTL) | 0x80, swc_base_addr + WDCTL);
 203#ifdef DEBUG
 204        printk(KERN_INFO DPFX "pc87413 - Enable SW_WD_TRG\n");
 205#endif
 206}
 207
 208/* Disable SW_WD_TRG */
 209
 210static inline void pc87413_disable_sw_wd_trg(unsigned int swc_base_addr)
 211{
 212        /* Disable SW_WD_TRG */
 213        outb_p(inb(swc_base_addr + WDCTL) & 0x7f, swc_base_addr + WDCTL);
 214#ifdef DEBUG
 215        printk(KERN_INFO DPFX "Disable SW_WD_TRG\n");
 216#endif
 217}
 218
 219/* -- Higher level functions ------------------------------------*/
 220
 221/* Enable the watchdog */
 222
 223static void pc87413_enable(void)
 224{
 225        unsigned int swc_base_addr;
 226
 227        spin_lock(&io_lock);
 228
 229        pc87413_select_wdt_out();
 230        pc87413_enable_swc();
 231        swc_base_addr = pc87413_get_swc_base();
 232        pc87413_swc_bank3(swc_base_addr);
 233        pc87413_programm_wdto(swc_base_addr, timeout);
 234        pc87413_enable_wden(swc_base_addr);
 235        pc87413_enable_sw_wd_tren(swc_base_addr);
 236        pc87413_enable_sw_wd_trg(swc_base_addr);
 237
 238        spin_unlock(&io_lock);
 239}
 240
 241/* Disable the watchdog */
 242
 243static void pc87413_disable(void)
 244{
 245        unsigned int swc_base_addr;
 246
 247        spin_lock(&io_lock);
 248
 249        pc87413_select_wdt_out();
 250        pc87413_enable_swc();
 251        swc_base_addr = pc87413_get_swc_base();
 252        pc87413_swc_bank3(swc_base_addr);
 253        pc87413_disable_sw_wd_tren(swc_base_addr);
 254        pc87413_disable_sw_wd_trg(swc_base_addr);
 255        pc87413_programm_wdto(swc_base_addr, 0);
 256
 257        spin_unlock(&io_lock);
 258}
 259
 260/* Refresh the watchdog */
 261
 262static void pc87413_refresh(void)
 263{
 264        unsigned int swc_base_addr;
 265
 266        spin_lock(&io_lock);
 267
 268        pc87413_select_wdt_out();
 269        pc87413_enable_swc();
 270        swc_base_addr = pc87413_get_swc_base();
 271        pc87413_swc_bank3(swc_base_addr);
 272        pc87413_disable_sw_wd_tren(swc_base_addr);
 273        pc87413_disable_sw_wd_trg(swc_base_addr);
 274        pc87413_programm_wdto(swc_base_addr, timeout);
 275        pc87413_enable_wden(swc_base_addr);
 276        pc87413_enable_sw_wd_tren(swc_base_addr);
 277        pc87413_enable_sw_wd_trg(swc_base_addr);
 278
 279        spin_unlock(&io_lock);
 280}
 281
 282/* -- File operations -------------------------------------------*/
 283
 284/**
 285 *      pc87413_open:
 286 *      @inode: inode of device
 287 *      @file: file handle to device
 288 *
 289 */
 290
 291static int pc87413_open(struct inode *inode, struct file *file)
 292{
 293        /* /dev/watchdog can only be opened once */
 294
 295        if (test_and_set_bit(0, &timer_enabled))
 296                return -EBUSY;
 297
 298        if (nowayout)
 299                __module_get(THIS_MODULE);
 300
 301        /* Reload and activate timer */
 302        pc87413_refresh();
 303
 304        printk(KERN_INFO MODNAME
 305                "Watchdog enabled. Timeout set to %d minute(s).\n", timeout);
 306
 307        return nonseekable_open(inode, file);
 308}
 309
 310/**
 311 *      pc87413_release:
 312 *      @inode: inode to board
 313 *      @file: file handle to board
 314 *
 315 *      The watchdog has a configurable API. There is a religious dispute
 316 *      between people who want their watchdog to be able to shut down and
 317 *      those who want to be sure if the watchdog manager dies the machine
 318 *      reboots. In the former case we disable the counters, in the latter
 319 *      case you have to open it again very soon.
 320 */
 321
 322static int pc87413_release(struct inode *inode, struct file *file)
 323{
 324        /* Shut off the timer. */
 325
 326        if (expect_close == 42) {
 327                pc87413_disable();
 328                printk(KERN_INFO MODNAME
 329                                "Watchdog disabled, sleeping again...\n");
 330        } else {
 331                printk(KERN_CRIT MODNAME
 332                                "Unexpected close, not stopping watchdog!\n");
 333                pc87413_refresh();
 334        }
 335        clear_bit(0, &timer_enabled);
 336        expect_close = 0;
 337        return 0;
 338}
 339
 340/**
 341 *      pc87413_status:
 342 *
 343 *      return, if the watchdog is enabled (timeout is set...)
 344 */
 345
 346
 347static int pc87413_status(void)
 348{
 349          return 0; /* currently not supported */
 350}
 351
 352/**
 353 *      pc87413_write:
 354 *      @file: file handle to the watchdog
 355 *      @data: data buffer to write
 356 *      @len: length in bytes
 357 *      @ppos: pointer to the position to write. No seeks allowed
 358 *
 359 *      A write to a watchdog device is defined as a keepalive signal. Any
 360 *      write of data will do, as we we don't define content meaning.
 361 */
 362
 363static ssize_t pc87413_write(struct file *file, const char __user *data,
 364                             size_t len, loff_t *ppos)
 365{
 366        /* See if we got the magic character 'V' and reload the timer */
 367        if (len) {
 368                if (!nowayout) {
 369                        size_t i;
 370
 371                        /* reset expect flag */
 372                        expect_close = 0;
 373
 374                        /* scan to see whether or not we got the
 375                           magic character */
 376                        for (i = 0; i != len; i++) {
 377                                char c;
 378                                if (get_user(c, data + i))
 379                                        return -EFAULT;
 380                                if (c == 'V')
 381                                        expect_close = 42;
 382                        }
 383                }
 384
 385                /* someone wrote to us, we should reload the timer */
 386                pc87413_refresh();
 387        }
 388        return len;
 389}
 390
 391/**
 392 *      pc87413_ioctl:
 393 *      @file: file handle to the device
 394 *      @cmd: watchdog command
 395 *      @arg: argument pointer
 396 *
 397 *      The watchdog API defines a common set of functions for all watchdogs
 398 *      according to their available features. We only actually usefully support
 399 *      querying capabilities and current status.
 400 */
 401
 402static long pc87413_ioctl(struct file *file, unsigned int cmd,
 403                                                unsigned long arg)
 404{
 405        int new_timeout;
 406
 407        union {
 408                struct watchdog_info __user *ident;
 409                int __user *i;
 410        } uarg;
 411
 412        static const struct watchdog_info ident = {
 413                .options          = WDIOF_KEEPALIVEPING |
 414                                    WDIOF_SETTIMEOUT |
 415                                    WDIOF_MAGICCLOSE,
 416                .firmware_version = 1,
 417                .identity         = "PC87413(HF/F) watchdog",
 418        };
 419
 420        uarg.i = (int __user *)arg;
 421
 422        switch (cmd) {
 423        case WDIOC_GETSUPPORT:
 424                return copy_to_user(uarg.ident, &ident,
 425                                        sizeof(ident)) ? -EFAULT : 0;
 426        case WDIOC_GETSTATUS:
 427                return put_user(pc87413_status(), uarg.i);
 428        case WDIOC_GETBOOTSTATUS:
 429                return put_user(0, uarg.i);
 430        case WDIOC_SETOPTIONS:
 431        {
 432                int options, retval = -EINVAL;
 433                if (get_user(options, uarg.i))
 434                        return -EFAULT;
 435                if (options & WDIOS_DISABLECARD) {
 436                        pc87413_disable();
 437                        retval = 0;
 438                }
 439                if (options & WDIOS_ENABLECARD) {
 440                        pc87413_enable();
 441                        retval = 0;
 442                }
 443                return retval;
 444        }
 445        case WDIOC_KEEPALIVE:
 446                pc87413_refresh();
 447#ifdef DEBUG
 448                printk(KERN_INFO DPFX "keepalive\n");
 449#endif
 450                return 0;
 451        case WDIOC_SETTIMEOUT:
 452                if (get_user(new_timeout, uarg.i))
 453                        return -EFAULT;
 454                /* the API states this is given in secs */
 455                new_timeout /= 60;
 456                if (new_timeout < 0 || new_timeout > MAX_TIMEOUT)
 457                        return -EINVAL;
 458                timeout = new_timeout;
 459                pc87413_refresh();
 460                /* fall through and return the new timeout... */
 461        case WDIOC_GETTIMEOUT:
 462                new_timeout = timeout * 60;
 463                return put_user(new_timeout, uarg.i);
 464        default:
 465                return -ENOTTY;
 466        }
 467}
 468
 469/* -- Notifier funtions -----------------------------------------*/
 470
 471/**
 472 *      notify_sys:
 473 *      @this: our notifier block
 474 *      @code: the event being reported
 475 *      @unused: unused
 476 *
 477 *      Our notifier is called on system shutdowns. We want to turn the card
 478 *      off at reboot otherwise the machine will reboot again during memory
 479 *      test or worse yet during the following fsck. This would suck, in fact
 480 *      trust me - if it happens it does suck.
 481 */
 482
 483static int pc87413_notify_sys(struct notifier_block *this,
 484                              unsigned long code,
 485                              void *unused)
 486{
 487        if (code == SYS_DOWN || code == SYS_HALT)
 488                /* Turn the card off */
 489                pc87413_disable();
 490        return NOTIFY_DONE;
 491}
 492
 493/* -- Module's structures ---------------------------------------*/
 494
 495static const struct file_operations pc87413_fops = {
 496        .owner          = THIS_MODULE,
 497        .llseek         = no_llseek,
 498        .write          = pc87413_write,
 499        .unlocked_ioctl = pc87413_ioctl,
 500        .open           = pc87413_open,
 501        .release        = pc87413_release,
 502};
 503
 504static struct notifier_block pc87413_notifier = {
 505        .notifier_call  = pc87413_notify_sys,
 506};
 507
 508static struct miscdevice pc87413_miscdev = {
 509        .minor          = WATCHDOG_MINOR,
 510        .name           = "watchdog",
 511        .fops           = &pc87413_fops,
 512};
 513
 514/* -- Module init functions -------------------------------------*/
 515
 516/**
 517 *      pc87413_init: module's "constructor"
 518 *
 519 *      Set up the WDT watchdog board. All we have to do is grab the
 520 *      resources we require and bitch if anyone beat us to them.
 521 *      The open() function will actually kick the board off.
 522 */
 523
 524static int __init pc87413_init(void)
 525{
 526        int ret;
 527
 528        printk(KERN_INFO PFX "Version " VERSION " at io 0x%X\n",
 529                                                        WDT_INDEX_IO_PORT);
 530
 531        /* request_region(io, 2, "pc87413"); */
 532
 533        ret = register_reboot_notifier(&pc87413_notifier);
 534        if (ret != 0) {
 535                printk(KERN_ERR PFX
 536                        "cannot register reboot notifier (err=%d)\n", ret);
 537        }
 538
 539        ret = misc_register(&pc87413_miscdev);
 540        if (ret != 0) {
 541                printk(KERN_ERR PFX
 542                        "cannot register miscdev on minor=%d (err=%d)\n",
 543                        WATCHDOG_MINOR, ret);
 544                unregister_reboot_notifier(&pc87413_notifier);
 545                return ret;
 546        }
 547        printk(KERN_INFO PFX "initialized. timeout=%d min \n", timeout);
 548        pc87413_enable();
 549        return 0;
 550}
 551
 552/**
 553 *      pc87413_exit: module's "destructor"
 554 *
 555 *      Unload the watchdog. You cannot do this with any file handles open.
 556 *      If your watchdog is set to continue ticking on close and you unload
 557 *      it, well it keeps ticking. We won't get the interrupt but the board
 558 *      will not touch PC memory so all is fine. You just have to load a new
 559 *      module in 60 seconds or reboot.
 560 */
 561
 562static void __exit pc87413_exit(void)
 563{
 564        /* Stop the timer before we leave */
 565        if (!nowayout) {
 566                pc87413_disable();
 567                printk(KERN_INFO MODNAME "Watchdog disabled.\n");
 568        }
 569
 570        misc_deregister(&pc87413_miscdev);
 571        unregister_reboot_notifier(&pc87413_notifier);
 572        /* release_region(io, 2); */
 573
 574        printk(KERN_INFO MODNAME " watchdog component driver removed.\n");
 575}
 576
 577module_init(pc87413_init);
 578module_exit(pc87413_exit);
 579
 580MODULE_AUTHOR("Sven Anders <anders@anduras.de>, "
 581                "Marcus Junker <junker@anduras.de>,");
 582MODULE_DESCRIPTION("PC87413 WDT driver");
 583MODULE_LICENSE("GPL");
 584
 585MODULE_ALIAS_MISCDEV(WATCHDOG_MINOR);
 586
 587module_param(io, int, 0);
 588MODULE_PARM_DESC(io, MODNAME " I/O port (default: "
 589                                        __MODULE_STRING(IO_DEFAULT) ").");
 590
 591module_param(timeout, int, 0);
 592MODULE_PARM_DESC(timeout,
 593                "Watchdog timeout in minutes (default="
 594                                __MODULE_STRING(DEFAULT_TIMEOUT) ").");
 595
 596module_param(nowayout, int, 0);
 597MODULE_PARM_DESC(nowayout,
 598                "Watchdog cannot be stopped once started (default="
 599                                __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
 600
 601