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)  *   Copyright (C) 2010-2012 Hans de Goede <hdegoede@redhat.com>           *
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/acpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/watchdog.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include "sch56xx-common.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) /* Insmod parameters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) static int nowayout = WATCHDOG_NOWAYOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) module_param(nowayout, int, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	__MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #define SIO_SCH56XX_LD_EM	0x0C	/* Embedded uController Logical Dev */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #define SIO_UNLOCK_KEY		0x55	/* Key to enable Super-I/O */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #define SIO_LOCK_KEY		0xAA	/* Key to disable Super-I/O */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define SIO_REG_LDSEL		0x07	/* Logical device select */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define SIO_REG_DEVID		0x20	/* Device ID */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #define SIO_REG_ENABLE		0x30	/* Logical device enable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define SIO_REG_ADDR		0x66	/* Logical device address (2 bytes) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define SIO_SCH5627_ID		0xC6	/* Chipset ID */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define SIO_SCH5636_ID		0xC7	/* Chipset ID */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #define REGION_LENGTH		10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #define SCH56XX_CMD_READ	0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #define SCH56XX_CMD_WRITE	0x03
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) /* Watchdog registers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) #define SCH56XX_REG_WDOG_PRESET		0x58B
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) #define SCH56XX_REG_WDOG_CONTROL	0x58C
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) #define SCH56XX_WDOG_TIME_BASE_SEC	0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) #define SCH56XX_REG_WDOG_OUTPUT_ENABLE	0x58E
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) #define SCH56XX_WDOG_OUTPUT_ENABLE	0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) struct sch56xx_watchdog_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	u16 addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	struct mutex *io_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	struct watchdog_info wdinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	struct watchdog_device wddev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	u8 watchdog_preset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	u8 watchdog_control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	u8 watchdog_output_enable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) static struct platform_device *sch56xx_pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) /* Super I/O functions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) static inline int superio_inb(int base, int reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	outb(reg, base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	return inb(base + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) static inline int superio_enter(int base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	/* Don't step on other drivers' I/O space by accident */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	if (!request_muxed_region(base, 2, "sch56xx")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		pr_err("I/O address 0x%04x already in use\n", base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	outb(SIO_UNLOCK_KEY, base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) static inline void superio_select(int base, int ld)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	outb(SIO_REG_LDSEL, base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	outb(ld, base + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) static inline void superio_exit(int base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	outb(SIO_LOCK_KEY, base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	release_region(base, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) static int sch56xx_send_cmd(u16 addr, u8 cmd, u16 reg, u8 v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	u8 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	 * According to SMSC for the commands we use the maximum time for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	 * the EM to respond is 15 ms, but testing shows in practice it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	 * responds within 15-32 reads, so we first busy poll, and if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	 * that fails sleep a bit and try again until we are way past
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	 * the 15 ms maximum response time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	const int max_busy_polls = 64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	const int max_lazy_polls = 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	/* (Optional) Write-Clear the EC to Host Mailbox Register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	val = inb(addr + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	outb(val, addr + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	/* Set Mailbox Address Pointer to first location in Region 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	outb(0x00, addr + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	outb(0x80, addr + 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	/* Write Request Packet Header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	outb(cmd, addr + 4); /* VREG Access Type read:0x02 write:0x03 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	outb(0x01, addr + 5); /* # of Entries: 1 Byte (8-bit) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	outb(0x04, addr + 2); /* Mailbox AP to first data entry loc. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	/* Write Value field */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	if (cmd == SCH56XX_CMD_WRITE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		outb(v, addr + 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	/* Write Address field */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	outb(reg & 0xff, addr + 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	outb(reg >> 8, addr + 7);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	/* Execute the Random Access Command */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	outb(0x01, addr); /* Write 01h to the Host-to-EC register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	/* EM Interface Polling "Algorithm" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	for (i = 0; i < max_busy_polls + max_lazy_polls; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		if (i >= max_busy_polls)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 			msleep(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		/* Read Interrupt source Register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		val = inb(addr + 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		/* Write Clear the interrupt source bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		if (val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 			outb(val, addr + 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		/* Command Completed ? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		if (val & 0x01)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	if (i == max_busy_polls + max_lazy_polls) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		pr_err("Max retries exceeded reading virtual register 0x%04hx (%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		       reg, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	 * According to SMSC we may need to retry this, but sofar I've always
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	 * seen this succeed in 1 try.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	for (i = 0; i < max_busy_polls; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		/* Read EC-to-Host Register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		val = inb(addr + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		/* Command Completed ? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		if (val == 0x01)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		if (i == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 			pr_warn("EC reports: 0x%02x reading virtual register 0x%04hx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 				(unsigned int)val, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	if (i == max_busy_polls) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		pr_err("Max retries exceeded reading virtual register 0x%04hx (%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		       reg, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	 * According to the SMSC app note we should now do:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	 * Set Mailbox Address Pointer to first location in Region 1 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	 * outb(0x00, addr + 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	 * outb(0x80, addr + 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	 * But if we do that things don't work, so let's not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	/* Read Value field */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	if (cmd == SCH56XX_CMD_READ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		return inb(addr + 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	return 0;
^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) int sch56xx_read_virtual_reg(u16 addr, u16 reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	return sch56xx_send_cmd(addr, SCH56XX_CMD_READ, reg, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) EXPORT_SYMBOL(sch56xx_read_virtual_reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) int sch56xx_write_virtual_reg(u16 addr, u16 reg, u8 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	return sch56xx_send_cmd(addr, SCH56XX_CMD_WRITE, reg, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) EXPORT_SYMBOL(sch56xx_write_virtual_reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) int sch56xx_read_virtual_reg16(u16 addr, u16 reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	int lsb, msb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	/* Read LSB first, this will cause the matching MSB to be latched */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	lsb = sch56xx_read_virtual_reg(addr, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	if (lsb < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 		return lsb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	msb = sch56xx_read_virtual_reg(addr, reg + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	if (msb < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		return msb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	return lsb | (msb << 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) EXPORT_SYMBOL(sch56xx_read_virtual_reg16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) int sch56xx_read_virtual_reg12(u16 addr, u16 msb_reg, u16 lsn_reg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 			       int high_nibble)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	int msb, lsn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	/* Read MSB first, this will cause the matching LSN to be latched */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	msb = sch56xx_read_virtual_reg(addr, msb_reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	if (msb < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 		return msb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	lsn = sch56xx_read_virtual_reg(addr, lsn_reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	if (lsn < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		return lsn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	if (high_nibble)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		return (msb << 4) | (lsn >> 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		return (msb << 4) | (lsn & 0x0f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) EXPORT_SYMBOL(sch56xx_read_virtual_reg12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)  * Watchdog routines
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) static int watchdog_set_timeout(struct watchdog_device *wddev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 				unsigned int timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	struct sch56xx_watchdog_data *data = watchdog_get_drvdata(wddev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	unsigned int resolution;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	u8 control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	/* 1 second or 60 second resolution? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	if (timeout <= 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		resolution = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		resolution = 60;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	if (timeout < resolution || timeout > (resolution * 255))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	if (resolution == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		control = data->watchdog_control | SCH56XX_WDOG_TIME_BASE_SEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		control = data->watchdog_control & ~SCH56XX_WDOG_TIME_BASE_SEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	if (data->watchdog_control != control) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		mutex_lock(data->io_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		ret = sch56xx_write_virtual_reg(data->addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 						SCH56XX_REG_WDOG_CONTROL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 						control);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		mutex_unlock(data->io_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		data->watchdog_control = control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	 * Remember new timeout value, but do not write as that (re)starts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	 * the watchdog countdown.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	data->watchdog_preset = DIV_ROUND_UP(timeout, resolution);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	wddev->timeout = data->watchdog_preset * resolution;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	return 0;
^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) static int watchdog_start(struct watchdog_device *wddev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	struct sch56xx_watchdog_data *data = watchdog_get_drvdata(wddev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	u8 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	 * The sch56xx's watchdog cannot really be started / stopped
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	 * it is always running, but we can avoid the timer expiring
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	 * from causing a system reset by clearing the output enable bit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	 * The sch56xx's watchdog will set the watchdog event bit, bit 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	 * of the second interrupt source register (at base-address + 9),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	 * when the timer expires.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	 * This will only cause a system reset if the 0-1 flank happens when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	 * output enable is true. Setting output enable after the flank will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	 * not cause a reset, nor will the timer expiring a second time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	 * This means we must clear the watchdog event bit in case it is set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	 * The timer may still be running (after a recent watchdog_stop) and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	 * mere milliseconds away from expiring, so the timer must be reset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	 * first!
^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) 	mutex_lock(data->io_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	/* 1. Reset the watchdog countdown counter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	ret = sch56xx_write_virtual_reg(data->addr, SCH56XX_REG_WDOG_PRESET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 					data->watchdog_preset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		goto leave;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	/* 2. Enable output */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	val = data->watchdog_output_enable | SCH56XX_WDOG_OUTPUT_ENABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	ret = sch56xx_write_virtual_reg(data->addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 					SCH56XX_REG_WDOG_OUTPUT_ENABLE, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		goto leave;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	data->watchdog_output_enable = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	/* 3. Clear the watchdog event bit if set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	val = inb(data->addr + 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	if (val & 0x01)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		outb(0x01, data->addr + 9);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) leave:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	mutex_unlock(data->io_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) static int watchdog_trigger(struct watchdog_device *wddev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	struct sch56xx_watchdog_data *data = watchdog_get_drvdata(wddev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	/* Reset the watchdog countdown counter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	mutex_lock(data->io_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	ret = sch56xx_write_virtual_reg(data->addr, SCH56XX_REG_WDOG_PRESET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 					data->watchdog_preset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	mutex_unlock(data->io_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) static int watchdog_stop(struct watchdog_device *wddev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	struct sch56xx_watchdog_data *data = watchdog_get_drvdata(wddev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	u8 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	val = data->watchdog_output_enable & ~SCH56XX_WDOG_OUTPUT_ENABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	mutex_lock(data->io_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	ret = sch56xx_write_virtual_reg(data->addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 					SCH56XX_REG_WDOG_OUTPUT_ENABLE, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	mutex_unlock(data->io_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	data->watchdog_output_enable = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) static const struct watchdog_ops watchdog_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	.owner		= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	.start		= watchdog_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	.stop		= watchdog_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	.ping		= watchdog_trigger,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	.set_timeout	= watchdog_set_timeout,
^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) struct sch56xx_watchdog_data *sch56xx_watchdog_register(struct device *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	u16 addr, u32 revision, struct mutex *io_lock, int check_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	struct sch56xx_watchdog_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	int err, control, output_enable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	/* Cache the watchdog registers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	mutex_lock(io_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	control =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 		sch56xx_read_virtual_reg(addr, SCH56XX_REG_WDOG_CONTROL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	output_enable =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		sch56xx_read_virtual_reg(addr, SCH56XX_REG_WDOG_OUTPUT_ENABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	mutex_unlock(io_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	if (control < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	if (output_enable < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 	if (check_enabled && !(output_enable & SCH56XX_WDOG_OUTPUT_ENABLE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		pr_warn("Watchdog not enabled by BIOS, not registering\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	data = kzalloc(sizeof(struct sch56xx_watchdog_data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	data->addr = addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	data->io_lock = io_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	strlcpy(data->wdinfo.identity, "sch56xx watchdog",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 		sizeof(data->wdinfo.identity));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	data->wdinfo.firmware_version = revision;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	data->wdinfo.options = WDIOF_KEEPALIVEPING | WDIOF_SETTIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	if (!nowayout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 		data->wdinfo.options |= WDIOF_MAGICCLOSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	data->wddev.info = &data->wdinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	data->wddev.ops = &watchdog_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	data->wddev.parent = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	data->wddev.timeout = 60;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	data->wddev.min_timeout = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	data->wddev.max_timeout = 255 * 60;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	if (nowayout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 		set_bit(WDOG_NO_WAY_OUT, &data->wddev.status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	if (output_enable & SCH56XX_WDOG_OUTPUT_ENABLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 		set_bit(WDOG_HW_RUNNING, &data->wddev.status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	/* Since the watchdog uses a downcounter there is no register to read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	   the BIOS set timeout from (if any was set at all) ->
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	   Choose a preset which will give us a 1 minute timeout */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	if (control & SCH56XX_WDOG_TIME_BASE_SEC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 		data->watchdog_preset = 60; /* seconds */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 		data->watchdog_preset = 1; /* minute */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	data->watchdog_control = control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	data->watchdog_output_enable = output_enable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	watchdog_set_drvdata(&data->wddev, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	err = watchdog_register_device(&data->wddev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 		pr_err("Registering watchdog chardev: %d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 		kfree(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 		return NULL;
^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) 	return data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) EXPORT_SYMBOL(sch56xx_watchdog_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) void sch56xx_watchdog_unregister(struct sch56xx_watchdog_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	watchdog_unregister_device(&data->wddev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	kfree(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) EXPORT_SYMBOL(sch56xx_watchdog_unregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)  * platform dev find, add and remove functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) static int __init sch56xx_find(int sioaddr, const char **name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	u8 devid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	unsigned short address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	err = superio_enter(sioaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	devid = superio_inb(sioaddr, SIO_REG_DEVID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	switch (devid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 	case SIO_SCH5627_ID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 		*name = "sch5627";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	case SIO_SCH5636_ID:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 		*name = "sch5636";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 		pr_debug("Unsupported device id: 0x%02x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 			 (unsigned int)devid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 		err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	superio_select(sioaddr, SIO_SCH56XX_LD_EM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	if (!(superio_inb(sioaddr, SIO_REG_ENABLE) & 0x01)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 		pr_warn("Device not activated\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 		err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	}
^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) 	 * Warning the order of the low / high byte is the other way around
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	 * as on most other superio devices!!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	address = superio_inb(sioaddr, SIO_REG_ADDR) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 		   superio_inb(sioaddr, SIO_REG_ADDR + 1) << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	if (address == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 		pr_warn("Base address not set\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 		err = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 		goto exit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	err = address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) exit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	superio_exit(sioaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	return err;
^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 __init sch56xx_device_add(int address, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	struct resource res = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 		.start	= address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 		.end	= address + REGION_LENGTH - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 		.flags	= IORESOURCE_IO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	sch56xx_pdev = platform_device_alloc(name, address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	if (!sch56xx_pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	res.name = sch56xx_pdev->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	err = acpi_check_resource_conflict(&res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 		goto exit_device_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	err = platform_device_add_resources(sch56xx_pdev, &res, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 		pr_err("Device resource addition failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 		goto exit_device_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	err = platform_device_add(sch56xx_pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 		pr_err("Device addition failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 		goto exit_device_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) exit_device_put:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	platform_device_put(sch56xx_pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) static int __init sch56xx_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	int address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	const char *name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	address = sch56xx_find(0x4e, &name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 	if (address < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 		address = sch56xx_find(0x2e, &name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 	if (address < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 		return address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	return sch56xx_device_add(address, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) static void __exit sch56xx_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 	platform_device_unregister(sch56xx_pdev);
^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_DESCRIPTION("SMSC SCH56xx Hardware Monitoring Common Code");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) module_init(sch56xx_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) module_exit(sch56xx_exit);