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-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *	Watchdog for the 7101 PMU version found in the ALi M1535 chipsets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/miscdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/watchdog.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/ioport.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/notifier.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/reboot.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/pci.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #define WATCHDOG_NAME "ALi_M1535"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #define WATCHDOG_TIMEOUT 60	/* 60 sec default timeout */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) /* internal variables */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) static unsigned long ali_is_open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) static char ali_expect_release;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) static struct pci_dev *ali_pci;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) static u32 ali_timeout_bits;		/* stores the computed timeout */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) static DEFINE_SPINLOCK(ali_lock);	/* Guards the hardware */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) /* module parameters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) static int timeout = WATCHDOG_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) module_param(timeout, int, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) MODULE_PARM_DESC(timeout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 		"Watchdog timeout in seconds. (0 < timeout < 18000, default="
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 				__MODULE_STRING(WATCHDOG_TIMEOUT) ")");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) static bool nowayout = WATCHDOG_NOWAYOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) module_param(nowayout, bool, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) MODULE_PARM_DESC(nowayout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 		"Watchdog cannot be stopped once started (default="
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 				__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  *	ali_start	-	start watchdog countdown
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  *	Starts the timer running providing the timer has a counter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  *	configuration set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) static void ali_start(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	u32 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	spin_lock(&ali_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	pci_read_config_dword(ali_pci, 0xCC, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	val &= ~0x3F;	/* Mask count */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	val |= (1 << 25) | ali_timeout_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	pci_write_config_dword(ali_pci, 0xCC, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	spin_unlock(&ali_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67)  *	ali_stop	-	stop the timer countdown
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69)  *	Stop the ALi watchdog countdown
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) static void ali_stop(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	u32 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	spin_lock(&ali_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	pci_read_config_dword(ali_pci, 0xCC, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	val &= ~0x3F;		/* Mask count to zero (disabled) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	val &= ~(1 << 25);	/* and for safety mask the reset enable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	pci_write_config_dword(ali_pci, 0xCC, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	spin_unlock(&ali_lock);
^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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87)  *	ali_keepalive	-	send a keepalive to the watchdog
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89)  *	Send a keepalive to the timer (actually we restart the timer).
^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) static void ali_keepalive(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	ali_start();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98)  *	ali_settimer	-	compute the timer reload value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99)  *	@t: time in seconds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)  *	Computes the timeout values needed
^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) static int ali_settimer(int t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	if (t < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	else if (t < 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		ali_timeout_bits = t|(1 << 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	else if (t < 3600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		ali_timeout_bits = (t / 60)|(1 << 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	else if (t < 18000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		ali_timeout_bits = (t / 300)|(1 << 6)|(1 << 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	timeout = t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)  *	/dev/watchdog handling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)  *	ali_write	-	writes to ALi watchdog
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)  *	@file: file from VFS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)  *	@data: user address of data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)  *	@len: length of data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)  *	@ppos: pointer to the file offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)  *	Handle a write to the ALi watchdog. Writing to the file pings
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)  *	the watchdog and resets it. Writing the magic 'V' sequence allows
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)  *	the next close to turn off the watchdog.
^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) static ssize_t ali_write(struct file *file, const char __user *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 						size_t len, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	/* See if we got the magic character 'V' and reload the timer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	if (len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		if (!nowayout) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 			size_t i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 			/* note: just in case someone wrote the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 			   magic character five months ago... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 			ali_expect_release = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 			/* scan to see whether or not we got
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 			   the magic character */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 			for (i = 0; i != len; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 				char c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 				if (get_user(c, data + i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 					return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 				if (c == 'V')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 					ali_expect_release = 42;
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		/* someone wrote to us, we should reload the timer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		ali_start();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)  *	ali_ioctl	-	handle watchdog ioctls
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)  *	@file: VFS file pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)  *	@cmd: ioctl number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)  *	@arg: arguments to the ioctl
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)  *	Handle the watchdog ioctls supported by the ALi driver. Really
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)  *	we want an extension to enable irq ack monitoring and the like
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) static long ali_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	void __user *argp = (void __user *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	int __user *p = argp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	static const struct watchdog_info ident = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		.options =		WDIOF_KEEPALIVEPING |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 					WDIOF_SETTIMEOUT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 					WDIOF_MAGICCLOSE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		.firmware_version =	0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		.identity =		"ALi M1535 WatchDog Timer",
^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) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	case WDIOC_GETSUPPORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		return copy_to_user(argp, &ident, sizeof(ident)) ? -EFAULT : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	case WDIOC_GETSTATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	case WDIOC_GETBOOTSTATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		return put_user(0, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	case WDIOC_SETOPTIONS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		int new_options, retval = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		if (get_user(new_options, p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		if (new_options & WDIOS_DISABLECARD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 			ali_stop();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 			retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		if (new_options & WDIOS_ENABLECARD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 			ali_start();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 			retval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	case WDIOC_KEEPALIVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		ali_keepalive();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	case WDIOC_SETTIMEOUT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	{
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		int new_timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		if (get_user(new_timeout, p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		if (ali_settimer(new_timeout))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 		ali_keepalive();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	case WDIOC_GETTIMEOUT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		return put_user(timeout, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		return -ENOTTY;
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)  *	ali_open	-	handle open of ali watchdog
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)  *	@inode: inode from VFS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)  *	@file: file from VFS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)  *	Open the ALi watchdog device. Ensure only one person opens it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)  *	at a time. Also start the watchdog running.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) static int ali_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	/* /dev/watchdog can only be opened once */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	if (test_and_set_bit(0, &ali_is_open))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	/* Activate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	ali_start();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	return stream_open(inode, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^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)  *	ali_release	-	close an ALi watchdog
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)  *	@inode: inode from VFS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)  *	@file: file from VFS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)  *	Close the ALi watchdog device. Actual shutdown of the timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)  *	only occurs if the magic sequence has been set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) static int ali_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	 *      Shut off the timer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	if (ali_expect_release == 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		ali_stop();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		pr_crit("Unexpected close, not stopping watchdog!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		ali_keepalive();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	clear_bit(0, &ali_is_open);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	ali_expect_release = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)  *	ali_notify_sys	-	System down notifier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)  *	Notifier for system down
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)  */
^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) static int ali_notify_sys(struct notifier_block *this,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 					unsigned long code, void *unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	if (code == SYS_DOWN || code == SYS_HALT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		ali_stop();		/* Turn the WDT off */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	return NOTIFY_DONE;
^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)  *	Data for PCI driver interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)  *	This data only exists for exporting the supported
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)  *	PCI ids via MODULE_DEVICE_TABLE.  We do not actually
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)  *	register a pci_driver, because someone else might one day
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)  *	want to register another driver on the same PCI id.
^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 const struct pci_device_id ali_pci_tbl[] __used = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	{ PCI_VENDOR_ID_AL, 0x1533, PCI_ANY_ID, PCI_ANY_ID,},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	{ PCI_VENDOR_ID_AL, 0x1535, PCI_ANY_ID, PCI_ANY_ID,},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	{ 0, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) MODULE_DEVICE_TABLE(pci, ali_pci_tbl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)  *	ali_find_watchdog	-	find a 1535 and 7101
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)  *	Scans the PCI hardware for a 1535 series bridge and matching 7101
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)  *	watchdog device. This may be overtight but it is better to be safe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) static int __init ali_find_watchdog(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	struct pci_dev *pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	u32 wdog;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	/* Check for a 1533/1535 series bridge */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	pdev = pci_get_device(PCI_VENDOR_ID_AL, 0x1535, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	if (pdev == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		pdev = pci_get_device(PCI_VENDOR_ID_AL, 0x1533, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	if (pdev == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	pci_dev_put(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	/* Check for the a 7101 PMU */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	pdev = pci_get_device(PCI_VENDOR_ID_AL, 0x7101, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	if (pdev == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	if (pci_enable_device(pdev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		pci_dev_put(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	ali_pci = pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	 *	Initialize the timer bits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	pci_read_config_dword(pdev, 0xCC, &wdog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	/* Timer bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	wdog &= ~0x3F;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	/* Issued events */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	wdog &= ~((1 << 27)|(1 << 26)|(1 << 25)|(1 << 24));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	/* No monitor bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	wdog &= ~((1 << 16)|(1 << 13)|(1 << 12)|(1 << 11)|(1 << 10)|(1 << 9));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	pci_write_config_dword(pdev, 0xCC, wdog);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)  *	Kernel Interfaces
^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) static const struct file_operations ali_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	.owner		=	THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	.llseek		=	no_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	.write		=	ali_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	.unlocked_ioctl =	ali_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	.compat_ioctl	= 	compat_ptr_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	.open		=	ali_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	.release	=	ali_release,
^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 struct miscdevice ali_miscdev = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	.minor =	WATCHDOG_MINOR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	.name =		"watchdog",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	.fops =		&ali_fops,
^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) static struct notifier_block ali_notifier = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	.notifier_call =	ali_notify_sys,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)  *	watchdog_init	-	module initialiser
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)  *	Scan for a suitable watchdog and if so initialize it. Return an error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)  *	if we cannot, the error causes the module to unload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) static int __init watchdog_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	/* Check whether or not the hardware watchdog is there */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	if (ali_find_watchdog() != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	/* Check that the timeout value is within it's range;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	   if not reset to the default */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	if (timeout < 1 || timeout >= 18000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		timeout = WATCHDOG_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 		pr_info("timeout value must be 0 < timeout < 18000, using %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 			timeout);
^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) 	/* Calculate the watchdog's timeout */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	ali_settimer(timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	ret = register_reboot_notifier(&ali_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	if (ret != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 		pr_err("cannot register reboot notifier (err=%d)\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	ret = misc_register(&ali_miscdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	if (ret != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 		pr_err("cannot register miscdev on minor=%d (err=%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 		       WATCHDOG_MINOR, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 		goto unreg_reboot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	pr_info("initialized. timeout=%d sec (nowayout=%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 		timeout, nowayout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) unreg_reboot:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	unregister_reboot_notifier(&ali_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)  *	watchdog_exit	-	module de-initialiser
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432)  *	Called while unloading a successfully installed watchdog module.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) static void __exit watchdog_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	/* Stop the timer before we leave */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	ali_stop();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	/* Deregister */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	misc_deregister(&ali_miscdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	unregister_reboot_notifier(&ali_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	pci_dev_put(ali_pci);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) module_init(watchdog_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) module_exit(watchdog_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) MODULE_AUTHOR("Alan Cox");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) MODULE_DESCRIPTION("ALi M1535 PMU Watchdog Timer driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) MODULE_LICENSE("GPL");