Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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