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