^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * AMD Elan SC520 processor Watchdog Timer driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Based on acquirewdt.c by Alan Cox,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * and sbc60xxwdt.c by Jakob Oestergaard <jakob@unthought.net>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * The authors do NOT admit liability nor provide warranty for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * any of this software. This material is provided "AS-IS" in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * the hope that it may be useful for others.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * (c) Copyright 2001 Scott Jennings <linuxdrivers@oro.net>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * 9/27 - 2001 [Initial release]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * Additional fixes Alan Cox
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * - Fixed formatting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * - Removed debug printks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * - Fixed SMP built kernel deadlock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * - Switched to private locks not lock_kernel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * - Used ioremap/writew/readw
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * - Added NOWAYOUT support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * 4/12 - 2002 Changes by Rob Radez <rob@osinvestor.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * - Change comments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * - Eliminate fop_llseek
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * - Change CONFIG_WATCHDOG_NOWAYOUT semantics
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * - Add KERN_* tags to printks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * - fix possible wdt_is_open race
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * - Report proper capabilities in watchdog_info
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * - Add WDIOC_{GETSTATUS, GETBOOTSTATUS, SETTIMEOUT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * GETTIMEOUT, SETOPTIONS} ioctls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * 09/8 - 2003 Changes by Wim Van Sebroeck <wim@iguana.be>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * - cleanup of trailing spaces
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * - added extra printk's for startup problems
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * - use module_param
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * - made timeout (the emulated heartbeat) a module_param
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * - made the keepalive ping an internal subroutine
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * 3/27 - 2004 Changes by Sean Young <sean@mess.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * - set MMCR_BASE to 0xfffef000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * - CBAR does not need to be read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * - removed debugging printks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * This WDT driver is different from most other Linux WDT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * drivers in that the driver will ping the watchdog by itself,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * because this particular WDT has a very short timeout (1.6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * seconds) and it would be insane to count on any userspace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * daemon always getting scheduled within that time frame.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * This driver uses memory mapped IO, and spinlock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #include <linux/timer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #include <linux/miscdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) #include <linux/watchdog.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) #include <linux/ioport.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) #include <linux/notifier.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) #include <linux/reboot.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) * The AMD Elan SC520 timeout value is 492us times a power of 2 (0-7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * 0: 492us 2: 1.01s 4: 4.03s 6: 16.22s
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * 1: 503ms 3: 2.01s 5: 8.05s 7: 32.21s
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * We will program the SC520 watchdog for a timeout of 2.01s.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * If we reset the watchdog every ~250ms we should be safe.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) #define WDT_INTERVAL (HZ/4+1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * We must not require too good response from the userspace daemon.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * Here we require the userspace daemon to send us a heartbeat
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * char to /dev/watchdog every 30 seconds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) #define WATCHDOG_TIMEOUT 30 /* 30 sec default timeout */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) /* in seconds, will be multiplied by HZ to get seconds to wait for a ping */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) static int timeout = WATCHDOG_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) module_param(timeout, int, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) MODULE_PARM_DESC(timeout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) "Watchdog timeout in seconds. (1 <= timeout <= 3600, default="
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) __MODULE_STRING(WATCHDOG_TIMEOUT) ")");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) static bool nowayout = WATCHDOG_NOWAYOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) module_param(nowayout, bool, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) MODULE_PARM_DESC(nowayout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) "Watchdog cannot be stopped once started (default="
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * AMD Elan SC520 - Watchdog Timer Registers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) #define MMCR_BASE 0xfffef000 /* The default base address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) #define OFFS_WDTMRCTL 0xCB0 /* Watchdog Timer Control Register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) /* WDT Control Register bit definitions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) #define WDT_EXP_SEL_01 0x0001 /* [01] Time-out = 496 us (with 33 Mhz clk). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) #define WDT_EXP_SEL_02 0x0002 /* [02] Time-out = 508 ms (with 33 Mhz clk). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) #define WDT_EXP_SEL_03 0x0004 /* [03] Time-out = 1.02 s (with 33 Mhz clk). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) #define WDT_EXP_SEL_04 0x0008 /* [04] Time-out = 2.03 s (with 33 Mhz clk). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) #define WDT_EXP_SEL_05 0x0010 /* [05] Time-out = 4.07 s (with 33 Mhz clk). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) #define WDT_EXP_SEL_06 0x0020 /* [06] Time-out = 8.13 s (with 33 Mhz clk). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) #define WDT_EXP_SEL_07 0x0040 /* [07] Time-out = 16.27s (with 33 Mhz clk). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) #define WDT_EXP_SEL_08 0x0080 /* [08] Time-out = 32.54s (with 33 Mhz clk). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) #define WDT_IRQ_FLG 0x1000 /* [12] Interrupt Request Flag */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) #define WDT_WRST_ENB 0x4000 /* [14] Watchdog Timer Reset Enable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) #define WDT_ENB 0x8000 /* [15] Watchdog Timer Enable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static __u16 __iomem *wdtmrctl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static void wdt_timer_ping(struct timer_list *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) static DEFINE_TIMER(timer, wdt_timer_ping);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) static unsigned long next_heartbeat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static unsigned long wdt_is_open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) static char wdt_expect_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) static DEFINE_SPINLOCK(wdt_spinlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) * Whack the dog
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) static void wdt_timer_ping(struct timer_list *unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) /* If we got a heartbeat pulse within the WDT_US_INTERVAL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * we agree to ping the WDT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) if (time_before(jiffies, next_heartbeat)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) /* Ping the WDT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) spin_lock(&wdt_spinlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) writew(0xAAAA, wdtmrctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) writew(0x5555, wdtmrctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) spin_unlock(&wdt_spinlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) /* Re-set the timer interval */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) mod_timer(&timer, jiffies + WDT_INTERVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) pr_warn("Heartbeat lost! Will not ping the watchdog\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) * Utility routines
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) static void wdt_config(int writeval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) /* buy some time (ping) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) spin_lock_irqsave(&wdt_spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) readw(wdtmrctl); /* ensure write synchronization */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) writew(0xAAAA, wdtmrctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) writew(0x5555, wdtmrctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) /* unlock WDT = make WDT configuration register writable one time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) writew(0x3333, wdtmrctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) writew(0xCCCC, wdtmrctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) /* write WDT configuration register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) writew(writeval, wdtmrctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) spin_unlock_irqrestore(&wdt_spinlock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) static int wdt_startup(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) next_heartbeat = jiffies + (timeout * HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) /* Start the timer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) mod_timer(&timer, jiffies + WDT_INTERVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) /* Start the watchdog */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) wdt_config(WDT_ENB | WDT_WRST_ENB | WDT_EXP_SEL_04);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) pr_info("Watchdog timer is now enabled\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) static int wdt_turnoff(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) /* Stop the timer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) del_timer_sync(&timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) /* Stop the watchdog */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) wdt_config(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) pr_info("Watchdog timer is now disabled...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) static int wdt_keepalive(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) /* user land ping */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) next_heartbeat = jiffies + (timeout * HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) static int wdt_set_heartbeat(int t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) if ((t < 1) || (t > 3600)) /* arbitrary upper limit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) timeout = t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) * /dev/watchdog handling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) static ssize_t fop_write(struct file *file, const char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) /* See if we got the magic character 'V' and reload the timer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) if (count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) if (!nowayout) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) size_t ofs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) /* note: just in case someone wrote the magic character
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) * five months ago... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) wdt_expect_close = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) /* now scan */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) for (ofs = 0; ofs != count; ofs++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) char c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) if (get_user(c, buf + ofs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) if (c == 'V')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) wdt_expect_close = 42;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) /* Well, anyhow someone wrote to us, we should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) return that favour */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) wdt_keepalive();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) static int fop_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) /* Just in case we're already talking to someone... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) if (test_and_set_bit(0, &wdt_is_open))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) if (nowayout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) __module_get(THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) /* Good, fire up the show */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) wdt_startup();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) return stream_open(inode, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) static int fop_close(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) if (wdt_expect_close == 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) wdt_turnoff();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) pr_crit("Unexpected close, not stopping watchdog!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) wdt_keepalive();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) clear_bit(0, &wdt_is_open);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) wdt_expect_close = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) static long fop_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) void __user *argp = (void __user *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) int __user *p = argp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) static const struct watchdog_info ident = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) .options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) | WDIOF_MAGICCLOSE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) .firmware_version = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) .identity = "SC520",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) case WDIOC_GETSUPPORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) case WDIOC_GETSTATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) case WDIOC_GETBOOTSTATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) return put_user(0, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) case WDIOC_SETOPTIONS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) int new_options, retval = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) if (get_user(new_options, p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (new_options & WDIOS_DISABLECARD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) wdt_turnoff();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) if (new_options & WDIOS_ENABLECARD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) wdt_startup();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) case WDIOC_KEEPALIVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) wdt_keepalive();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) case WDIOC_SETTIMEOUT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) int new_timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) if (get_user(new_timeout, p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) if (wdt_set_heartbeat(new_timeout))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) wdt_keepalive();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) case WDIOC_GETTIMEOUT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) return put_user(timeout, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) return -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) static const struct file_operations wdt_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) .llseek = no_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) .write = fop_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) .open = fop_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) .release = fop_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) .unlocked_ioctl = fop_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) .compat_ioctl = compat_ptr_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) static struct miscdevice wdt_miscdev = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) .minor = WATCHDOG_MINOR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) .name = "watchdog",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) .fops = &wdt_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) * Notifier for system down
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) static int wdt_notify_sys(struct notifier_block *this, unsigned long code,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) void *unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) if (code == SYS_DOWN || code == SYS_HALT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) wdt_turnoff();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) * The WDT needs to learn about soft shutdowns in order to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) * turn the timebomb registers off.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) static struct notifier_block wdt_notifier = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) .notifier_call = wdt_notify_sys,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) static void __exit sc520_wdt_unload(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) if (!nowayout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) wdt_turnoff();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) /* Deregister */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) misc_deregister(&wdt_miscdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) unregister_reboot_notifier(&wdt_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) iounmap(wdtmrctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) static int __init sc520_wdt_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) int rc = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) /* Check that the timeout value is within it's range ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) if not reset to the default */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) if (wdt_set_heartbeat(timeout)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) wdt_set_heartbeat(WATCHDOG_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) pr_info("timeout value must be 1 <= timeout <= 3600, using %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) WATCHDOG_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) wdtmrctl = ioremap(MMCR_BASE + OFFS_WDTMRCTL, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) if (!wdtmrctl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) pr_err("Unable to remap memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) goto err_out_region2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) rc = register_reboot_notifier(&wdt_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) pr_err("cannot register reboot notifier (err=%d)\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) goto err_out_ioremap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) rc = misc_register(&wdt_miscdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) pr_err("cannot register miscdev on minor=%d (err=%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) WATCHDOG_MINOR, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) goto err_out_notifier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) pr_info("WDT driver for SC520 initialised. timeout=%d sec (nowayout=%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) timeout, nowayout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) err_out_notifier:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) unregister_reboot_notifier(&wdt_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) err_out_ioremap:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) iounmap(wdtmrctl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) err_out_region2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) module_init(sc520_wdt_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) module_exit(sc520_wdt_unload);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) MODULE_AUTHOR("Scott and Bill Jennings");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) MODULE_DESCRIPTION(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) "Driver for watchdog timer in AMD \"Elan\" SC520 uProcessor");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) MODULE_LICENSE("GPL");