^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* cpwd.c - driver implementation for hardware watchdog
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * timers found on Sun Microsystems CP1400 and CP1500 boards.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * This device supports both the generic Linux watchdog
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * interface and Solaris-compatible ioctls as best it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * able.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * NOTE: CP1400 systems appear to have a defective intr_mask
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * register on the PLD, preventing the disabling of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * timer interrupts. We use a timer to periodically
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * reset 'stopped' watchdogs on affected platforms.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * Copyright (c) 2000 Eric Brower (ebrower@usa.net)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * Copyright (C) 2008 David S. Miller <davem@davemloft.net>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/major.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/miscdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/ioport.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/timer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/compat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <linux/of_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #include <asm/irq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #include <asm/watchdog.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #define DRIVER_NAME "cpwd"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #define WD_OBPNAME "watchdog"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #define WD_BADMODEL "SUNW,501-5336"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #define WD_BTIMEOUT (jiffies + (HZ * 1000))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #define WD_BLIMIT 0xFFFF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #define WD0_MINOR 212
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #define WD1_MINOR 213
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #define WD2_MINOR 214
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) /* Internal driver definitions. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #define WD0_ID 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #define WD1_ID 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #define WD2_ID 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #define WD_NUMDEVS 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #define WD_INTR_OFF 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) #define WD_INTR_ON 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) #define WD_STAT_INIT 0x01 /* Watchdog timer is initialized */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) #define WD_STAT_BSTOP 0x02 /* Watchdog timer is brokenstopped */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) #define WD_STAT_SVCD 0x04 /* Watchdog interrupt occurred */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) /* Register value definitions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #define WD0_INTR_MASK 0x01 /* Watchdog device interrupt masks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) #define WD1_INTR_MASK 0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) #define WD2_INTR_MASK 0x04
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) #define WD_S_RUNNING 0x01 /* Watchdog device status running */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) #define WD_S_EXPIRED 0x02 /* Watchdog device status expired */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct cpwd {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) void __iomem *regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) spinlock_t lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) unsigned int irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) unsigned long timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) bool enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) bool reboot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) bool broken;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) bool initialized;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) struct miscdevice misc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) void __iomem *regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) u8 intr_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) u8 runstatus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) u16 timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) } devs[WD_NUMDEVS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) static DEFINE_MUTEX(cpwd_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) static struct cpwd *cpwd_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) /* Sun uses Altera PLD EPF8820ATC144-4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * providing three hardware watchdogs:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * 1) RIC - sends an interrupt when triggered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * 2) XIR - asserts XIR_B_RESET when triggered, resets CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * 3) POR - asserts POR_B_RESET when triggered, resets CPU, backplane, board
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) *** Timer register block definition (struct wd_timer_regblk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * dcntr and limit registers (halfword access):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * -------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * | 15 | ...| 1 | 0 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) * -------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) * |- counter val -|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) * -------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) * dcntr - Current 16-bit downcounter value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) * When downcounter reaches '0' watchdog expires.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) * Reading this register resets downcounter with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * 'limit' value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * limit - 16-bit countdown value in 1/10th second increments.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * Writing this register begins countdown with input value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * Reading from this register does not affect counter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * NOTES: After watchdog reset, dcntr and limit contain '1'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) * status register (byte access):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) * ---------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * | 7 | ... | 2 | 1 | 0 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) * --------------+------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) * |- UNUSED -| EXP | RUN |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) * ---------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) * status- Bit 0 - Watchdog is running
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * Bit 1 - Watchdog has expired
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) *** PLD register block definition (struct wd_pld_regblk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * intr_mask register (byte access):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * ---------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * | 7 | ... | 3 | 2 | 1 | 0 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * +-------------+------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * |- UNUSED -| WD3 | WD2 | WD1 |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) * ---------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * WD3 - 1 == Interrupt disabled for watchdog 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * WD2 - 1 == Interrupt disabled for watchdog 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * WD1 - 1 == Interrupt disabled for watchdog 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) * pld_status register (byte access):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * UNKNOWN, MAGICAL MYSTERY REGISTER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) #define WD_TIMER_REGSZ 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) #define WD0_OFF 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) #define WD1_OFF (WD_TIMER_REGSZ * 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) #define WD2_OFF (WD_TIMER_REGSZ * 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) #define PLD_OFF (WD_TIMER_REGSZ * 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) #define WD_DCNTR 0x00
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) #define WD_LIMIT 0x04
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) #define WD_STATUS 0x08
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) #define PLD_IMASK (PLD_OFF + 0x00)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) #define PLD_STATUS (PLD_OFF + 0x04)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) static struct timer_list cpwd_timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) static int wd0_timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) static int wd1_timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) static int wd2_timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) module_param(wd0_timeout, int, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) MODULE_PARM_DESC(wd0_timeout, "Default watchdog0 timeout in 1/10secs");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) module_param(wd1_timeout, int, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) MODULE_PARM_DESC(wd1_timeout, "Default watchdog1 timeout in 1/10secs");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) module_param(wd2_timeout, int, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) MODULE_PARM_DESC(wd2_timeout, "Default watchdog2 timeout in 1/10secs");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) MODULE_AUTHOR("Eric Brower <ebrower@usa.net>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) MODULE_DESCRIPTION("Hardware watchdog driver for Sun Microsystems CP1400/1500");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) MODULE_SUPPORTED_DEVICE("watchdog");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) static void cpwd_writew(u16 val, void __iomem *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) writew(cpu_to_le16(val), addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) static u16 cpwd_readw(void __iomem *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) u16 val = readw(addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) return le16_to_cpu(val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) static void cpwd_writeb(u8 val, void __iomem *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) writeb(val, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) static u8 cpwd_readb(void __iomem *addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) return readb(addr);
^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) /* Enable or disable watchdog interrupts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) * Because of the CP1400 defect this should only be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) * called during initialzation or by wd_[start|stop]timer()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) * index - sub-device index, or -1 for 'all'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) * enable - non-zero to enable interrupts, zero to disable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) static void cpwd_toggleintr(struct cpwd *p, int index, int enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) unsigned char curregs = cpwd_readb(p->regs + PLD_IMASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) unsigned char setregs =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) (index == -1) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) (WD0_INTR_MASK | WD1_INTR_MASK | WD2_INTR_MASK) :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) (p->devs[index].intr_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) if (enable == WD_INTR_ON)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) curregs &= ~setregs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) curregs |= setregs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) cpwd_writeb(curregs, p->regs + PLD_IMASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) /* Restarts timer with maximum limit value and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) * does not unset 'brokenstop' value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) static void cpwd_resetbrokentimer(struct cpwd *p, int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) cpwd_toggleintr(p, index, WD_INTR_ON);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) cpwd_writew(WD_BLIMIT, p->devs[index].regs + WD_LIMIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) /* Timer method called to reset stopped watchdogs--
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) * because of the PLD bug on CP1400, we cannot mask
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) * interrupts within the PLD so me must continually
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) * reset the timers ad infinitum.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) static void cpwd_brokentimer(struct timer_list *unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) struct cpwd *p = cpwd_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) int id, tripped = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) /* kill a running timer instance, in case we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) * were called directly instead of by kernel timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) if (timer_pending(&cpwd_timer))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) del_timer(&cpwd_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) for (id = 0; id < WD_NUMDEVS; id++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) if (p->devs[id].runstatus & WD_STAT_BSTOP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) ++tripped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) cpwd_resetbrokentimer(p, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) if (tripped) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) /* there is at least one timer brokenstopped-- reschedule */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) cpwd_timer.expires = WD_BTIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) add_timer(&cpwd_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) }
^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) /* Reset countdown timer with 'limit' value and continue countdown.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) * This will not start a stopped timer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) static void cpwd_pingtimer(struct cpwd *p, int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) if (cpwd_readb(p->devs[index].regs + WD_STATUS) & WD_S_RUNNING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) cpwd_readw(p->devs[index].regs + WD_DCNTR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) /* Stop a running watchdog timer-- the timer actually keeps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) * running, but the interrupt is masked so that no action is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) * taken upon expiration.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) static void cpwd_stoptimer(struct cpwd *p, int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) if (cpwd_readb(p->devs[index].regs + WD_STATUS) & WD_S_RUNNING) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) cpwd_toggleintr(p, index, WD_INTR_OFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) if (p->broken) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) p->devs[index].runstatus |= WD_STAT_BSTOP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) cpwd_brokentimer(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) }
^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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) /* Start a watchdog timer with the specified limit value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) * If the watchdog is running, it will be restarted with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) * the provided limit value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) * This function will enable interrupts on the specified
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) * watchdog.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) static void cpwd_starttimer(struct cpwd *p, int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) if (p->broken)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) p->devs[index].runstatus &= ~WD_STAT_BSTOP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) p->devs[index].runstatus &= ~WD_STAT_SVCD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) cpwd_writew(p->devs[index].timeout, p->devs[index].regs + WD_LIMIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) cpwd_toggleintr(p, index, WD_INTR_ON);
^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 int cpwd_getstatus(struct cpwd *p, int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) unsigned char stat = cpwd_readb(p->devs[index].regs + WD_STATUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) unsigned char intr = cpwd_readb(p->devs[index].regs + PLD_IMASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) unsigned char ret = WD_STOPPED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) /* determine STOPPED */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) if (!stat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) /* determine EXPIRED vs FREERUN vs RUNNING */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) else if (WD_S_EXPIRED & stat) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) ret = WD_EXPIRED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) } else if (WD_S_RUNNING & stat) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) if (intr & p->devs[index].intr_mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) ret = WD_FREERUN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) /* Fudge WD_EXPIRED status for defective CP1400--
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) * IF timer is running
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) * AND brokenstop is set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) * AND an interrupt has been serviced
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) * we are WD_EXPIRED.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) * IF timer is running
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) * AND brokenstop is set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) * AND no interrupt has been serviced
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) * we are WD_FREERUN.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) if (p->broken &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) (p->devs[index].runstatus & WD_STAT_BSTOP)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) if (p->devs[index].runstatus & WD_STAT_SVCD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) ret = WD_EXPIRED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) /* we could as well pretend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) * we are expired */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) ret = WD_FREERUN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) ret = WD_RUNNING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) /* determine SERVICED */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) if (p->devs[index].runstatus & WD_STAT_SVCD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) ret |= WD_SERVICED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) static irqreturn_t cpwd_interrupt(int irq, void *dev_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) struct cpwd *p = dev_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) /* Only WD0 will interrupt-- others are NMI and we won't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) * see them here....
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) spin_lock_irq(&p->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) cpwd_stoptimer(p, WD0_ID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) p->devs[WD0_ID].runstatus |= WD_STAT_SVCD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) spin_unlock_irq(&p->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) static int cpwd_open(struct inode *inode, struct file *f)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) struct cpwd *p = cpwd_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) mutex_lock(&cpwd_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) switch (iminor(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) case WD0_MINOR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) case WD1_MINOR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) case WD2_MINOR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) mutex_unlock(&cpwd_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) /* Register IRQ on first open of device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) if (!p->initialized) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) if (request_irq(p->irq, &cpwd_interrupt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) IRQF_SHARED, DRIVER_NAME, p)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) pr_err("Cannot register IRQ %d\n", p->irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) mutex_unlock(&cpwd_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) p->initialized = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) mutex_unlock(&cpwd_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) return stream_open(inode, f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) static int cpwd_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) static long cpwd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) static const struct watchdog_info info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) .options = WDIOF_SETTIMEOUT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) .firmware_version = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) .identity = DRIVER_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) void __user *argp = (void __user *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) int index = iminor(inode) - WD0_MINOR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) struct cpwd *p = cpwd_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) int setopt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) /* Generic Linux IOCTLs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) case WDIOC_GETSUPPORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) if (copy_to_user(argp, &info, sizeof(struct watchdog_info)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) case WDIOC_GETSTATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) case WDIOC_GETBOOTSTATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) if (put_user(0, (int __user *)argp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) case WDIOC_KEEPALIVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) cpwd_pingtimer(p, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) case WDIOC_SETOPTIONS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) if (copy_from_user(&setopt, argp, sizeof(unsigned int)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) if (setopt & WDIOS_DISABLECARD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) if (p->enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) cpwd_stoptimer(p, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) } else if (setopt & WDIOS_ENABLECARD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) cpwd_starttimer(p, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) /* Solaris-compatible IOCTLs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) case WIOCGSTAT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) setopt = cpwd_getstatus(p, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) if (copy_to_user(argp, &setopt, sizeof(unsigned int)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) case WIOCSTART:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) cpwd_starttimer(p, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) case WIOCSTOP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) if (p->enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) cpwd_stoptimer(p, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) static long cpwd_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) return cpwd_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) static ssize_t cpwd_write(struct file *file, const char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) struct cpwd *p = cpwd_device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) int index = iminor(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) if (count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) cpwd_pingtimer(p, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) static ssize_t cpwd_read(struct file *file, char __user *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) static const struct file_operations cpwd_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) .unlocked_ioctl = cpwd_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) .compat_ioctl = cpwd_compat_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) .open = cpwd_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) .write = cpwd_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) .read = cpwd_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) .release = cpwd_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) .llseek = no_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) static int cpwd_probe(struct platform_device *op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) struct device_node *options;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) const char *str_prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) const void *prop_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) int i, err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) struct cpwd *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) if (cpwd_device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) p = devm_kzalloc(&op->dev, sizeof(*p), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) if (!p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) p->irq = op->archdata.irqs[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) spin_lock_init(&p->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) p->regs = of_ioremap(&op->resource[0], 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 4 * WD_TIMER_REGSZ, DRIVER_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) if (!p->regs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) pr_err("Unable to map registers\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) options = of_find_node_by_path("/options");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) if (!options) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) pr_err("Unable to find /options node\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) goto out_iounmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) prop_val = of_get_property(options, "watchdog-enable?", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) p->enabled = (prop_val ? true : false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) prop_val = of_get_property(options, "watchdog-reboot?", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) p->reboot = (prop_val ? true : false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) str_prop = of_get_property(options, "watchdog-timeout", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) if (str_prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) p->timeout = simple_strtoul(str_prop, NULL, 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) of_node_put(options);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) /* CP1400s seem to have broken PLD implementations-- the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) * interrupt_mask register cannot be written, so no timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) * interrupts can be masked within the PLD.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) str_prop = of_get_property(op->dev.of_node, "model", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) p->broken = (str_prop && !strcmp(str_prop, WD_BADMODEL));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) if (!p->enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) cpwd_toggleintr(p, -1, WD_INTR_OFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) for (i = 0; i < WD_NUMDEVS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) static const char *cpwd_names[] = { "RIC", "XIR", "POR" };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) static int *parms[] = { &wd0_timeout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) &wd1_timeout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) &wd2_timeout };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) struct miscdevice *mp = &p->devs[i].misc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) mp->minor = WD0_MINOR + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) mp->name = cpwd_names[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) mp->fops = &cpwd_fops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) p->devs[i].regs = p->regs + (i * WD_TIMER_REGSZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) p->devs[i].intr_mask = (WD0_INTR_MASK << i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) p->devs[i].runstatus &= ~WD_STAT_BSTOP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) p->devs[i].runstatus |= WD_STAT_INIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) p->devs[i].timeout = p->timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) if (*parms[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) p->devs[i].timeout = *parms[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) err = misc_register(&p->devs[i].misc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) pr_err("Could not register misc device for dev %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) goto out_unregister;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) if (p->broken) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) timer_setup(&cpwd_timer, cpwd_brokentimer, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) cpwd_timer.expires = WD_BTIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) pr_info("PLD defect workaround enabled for model %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) WD_BADMODEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) platform_set_drvdata(op, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) cpwd_device = p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) out_unregister:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) for (i--; i >= 0; i--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) misc_deregister(&p->devs[i].misc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) out_iounmap:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) of_iounmap(&op->resource[0], p->regs, 4 * WD_TIMER_REGSZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) static int cpwd_remove(struct platform_device *op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) struct cpwd *p = platform_get_drvdata(op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) for (i = 0; i < WD_NUMDEVS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) misc_deregister(&p->devs[i].misc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) if (!p->enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) cpwd_stoptimer(p, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) if (p->devs[i].runstatus & WD_STAT_BSTOP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) cpwd_resetbrokentimer(p, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) if (p->broken)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) del_timer_sync(&cpwd_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) if (p->initialized)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) free_irq(p->irq, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) of_iounmap(&op->resource[0], p->regs, 4 * WD_TIMER_REGSZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) cpwd_device = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) static const struct of_device_id cpwd_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) .name = "watchdog",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) {},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) MODULE_DEVICE_TABLE(of, cpwd_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) static struct platform_driver cpwd_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) .name = DRIVER_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) .of_match_table = cpwd_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) .probe = cpwd_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) .remove = cpwd_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) module_platform_driver(cpwd_driver);