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-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * ACPI Hardware Watchdog (WDAT) driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2016, Intel Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/acpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/ioport.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/pm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/watchdog.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #define MAX_WDAT_ACTIONS ACPI_WDAT_ACTION_RESERVED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * struct wdat_instruction - Single ACPI WDAT instruction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * @entry: Copy of the ACPI table instruction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * @reg: Register the instruction is accessing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * @node: Next instruction in action sequence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) struct wdat_instruction {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	struct acpi_wdat_entry entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	void __iomem *reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	struct list_head node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  * struct wdat_wdt - ACPI WDAT watchdog device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  * @pdev: Parent platform device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  * @wdd: Watchdog core device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  * @period: How long is one watchdog period in ms
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  * @stopped_in_sleep: Is this watchdog stopped by the firmware in S1-S5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  * @stopped: Was the watchdog stopped by the driver in suspend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  * @actions: An array of instruction lists indexed by an action number from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  *           the WDAT table. There can be %NULL entries for not implemented
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  *           actions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) struct wdat_wdt {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	struct platform_device *pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	struct watchdog_device wdd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	unsigned int period;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	bool stopped_in_sleep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	bool stopped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	struct list_head *instructions[MAX_WDAT_ACTIONS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) #define to_wdat_wdt(wdd) container_of(wdd, struct wdat_wdt, wdd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) static bool nowayout = WATCHDOG_NOWAYOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) module_param(nowayout, bool, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) #define WDAT_DEFAULT_TIMEOUT	30
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) static int timeout = WDAT_DEFAULT_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) module_param(timeout, int, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds (default="
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		 __MODULE_STRING(WDAT_DEFAULT_TIMEOUT) ")");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) static int wdat_wdt_read(struct wdat_wdt *wdat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	 const struct wdat_instruction *instr, u32 *value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	const struct acpi_generic_address *gas = &instr->entry.register_region;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	switch (gas->access_width) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		*value = ioread8(instr->reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	case 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		*value = ioread16(instr->reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	case 3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		*value = ioread32(instr->reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	dev_dbg(&wdat->pdev->dev, "Read %#x from 0x%08llx\n", *value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		gas->address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) static int wdat_wdt_write(struct wdat_wdt *wdat,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	const struct wdat_instruction *instr, u32 value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	const struct acpi_generic_address *gas = &instr->entry.register_region;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	switch (gas->access_width) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	case 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		iowrite8((u8)value, instr->reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	case 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		iowrite16((u16)value, instr->reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	case 3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		iowrite32(value, instr->reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	dev_dbg(&wdat->pdev->dev, "Wrote %#x to 0x%08llx\n", value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		gas->address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	return 0;
^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) static int wdat_wdt_run_action(struct wdat_wdt *wdat, unsigned int action,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 			       u32 param, u32 *retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	struct wdat_instruction *instr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	if (action >= ARRAY_SIZE(wdat->instructions))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	if (!wdat->instructions[action])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	dev_dbg(&wdat->pdev->dev, "Running action %#x\n", action);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	/* Run each instruction sequentially */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	list_for_each_entry(instr, wdat->instructions[action], node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		const struct acpi_wdat_entry *entry = &instr->entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		const struct acpi_generic_address *gas;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		u32 flags, value, mask, x, y;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		bool preserve;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		gas = &entry->register_region;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		preserve = entry->instruction & ACPI_WDAT_PRESERVE_REGISTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		flags = entry->instruction & ~ACPI_WDAT_PRESERVE_REGISTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		value = entry->value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		mask = entry->mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		switch (flags) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		case ACPI_WDAT_READ_VALUE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 			ret = wdat_wdt_read(wdat, instr, &x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 			x >>= gas->bit_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 			x &= mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 			if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 				*retval = x == value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		case ACPI_WDAT_READ_COUNTDOWN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 			ret = wdat_wdt_read(wdat, instr, &x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 			x >>= gas->bit_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 			x &= mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 			if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 				*retval = x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		case ACPI_WDAT_WRITE_VALUE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 			x = value & mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 			x <<= gas->bit_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 			if (preserve) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 				ret = wdat_wdt_read(wdat, instr, &y);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 				if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 					return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 				y = y & ~(mask << gas->bit_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 				x |= y;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 			ret = wdat_wdt_write(wdat, instr, x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		case ACPI_WDAT_WRITE_COUNTDOWN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 			x = param;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 			x &= mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 			x <<= gas->bit_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 			if (preserve) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 				ret = wdat_wdt_read(wdat, instr, &y);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 				if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 					return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 				y = y & ~(mask << gas->bit_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 				x |= y;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 			ret = wdat_wdt_write(wdat, instr, x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 				return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 			dev_err(&wdat->pdev->dev, "Unknown instruction: %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 				flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) static int wdat_wdt_enable_reboot(struct wdat_wdt *wdat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	 * WDAT specification says that the watchdog is required to reboot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	 * the system when it fires. However, it also states that it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	 * recommeded to make it configurable through hardware register. We
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	 * enable reboot now if it is configurable, just in case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	ret = wdat_wdt_run_action(wdat, ACPI_WDAT_SET_REBOOT, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	if (ret && ret != -EOPNOTSUPP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		dev_err(&wdat->pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 			"Failed to enable reboot when watchdog triggers\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) static void wdat_wdt_boot_status(struct wdat_wdt *wdat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	u32 boot_status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	ret = wdat_wdt_run_action(wdat, ACPI_WDAT_GET_STATUS, 0, &boot_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	if (ret && ret != -EOPNOTSUPP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		dev_err(&wdat->pdev->dev, "Failed to read boot status\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	if (boot_status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		wdat->wdd.bootstatus = WDIOF_CARDRESET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	/* Clear the boot status in case BIOS did not do it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	ret = wdat_wdt_run_action(wdat, ACPI_WDAT_SET_STATUS, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	if (ret && ret != -EOPNOTSUPP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		dev_err(&wdat->pdev->dev, "Failed to clear boot status\n");
^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) static void wdat_wdt_set_running(struct wdat_wdt *wdat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	u32 running = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	ret = wdat_wdt_run_action(wdat, ACPI_WDAT_GET_RUNNING_STATE, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 				  &running);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	if (ret && ret != -EOPNOTSUPP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		dev_err(&wdat->pdev->dev, "Failed to read running state\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	if (running)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		set_bit(WDOG_HW_RUNNING, &wdat->wdd.status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) static int wdat_wdt_start(struct watchdog_device *wdd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	return wdat_wdt_run_action(to_wdat_wdt(wdd),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 				   ACPI_WDAT_SET_RUNNING_STATE, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) static int wdat_wdt_stop(struct watchdog_device *wdd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	return wdat_wdt_run_action(to_wdat_wdt(wdd),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 				   ACPI_WDAT_SET_STOPPED_STATE, 0, NULL);
^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 wdat_wdt_ping(struct watchdog_device *wdd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	return wdat_wdt_run_action(to_wdat_wdt(wdd), ACPI_WDAT_RESET, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) static int wdat_wdt_set_timeout(struct watchdog_device *wdd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 				unsigned int timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	struct wdat_wdt *wdat = to_wdat_wdt(wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	unsigned int periods;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	periods = timeout * 1000 / wdat->period;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	ret = wdat_wdt_run_action(wdat, ACPI_WDAT_SET_COUNTDOWN, periods, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		wdd->timeout = timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	return ret;
^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 unsigned int wdat_wdt_get_timeleft(struct watchdog_device *wdd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	struct wdat_wdt *wdat = to_wdat_wdt(wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	u32 periods = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	wdat_wdt_run_action(wdat, ACPI_WDAT_GET_CURRENT_COUNTDOWN, 0, &periods);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	return periods * wdat->period / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) static const struct watchdog_info wdat_wdt_info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	.options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	.firmware_version = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	.identity = "wdat_wdt",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) static const struct watchdog_ops wdat_wdt_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	.owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	.start = wdat_wdt_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	.stop = wdat_wdt_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	.ping = wdat_wdt_ping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	.set_timeout = wdat_wdt_set_timeout,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	.get_timeleft = wdat_wdt_get_timeleft,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) static int wdat_wdt_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	struct device *dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	const struct acpi_wdat_entry *entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	const struct acpi_table_wdat *tbl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	struct wdat_wdt *wdat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	struct resource *res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	void __iomem **regs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	acpi_status status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	status = acpi_get_table(ACPI_SIG_WDAT, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 				(struct acpi_table_header **)&tbl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	if (ACPI_FAILURE(status))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	wdat = devm_kzalloc(dev, sizeof(*wdat), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	if (!wdat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	regs = devm_kcalloc(dev, pdev->num_resources, sizeof(*regs),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 			    GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	if (!regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	/* WDAT specification wants to have >= 1ms period */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	if (tbl->timer_period < 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	if (tbl->min_count > tbl->max_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	wdat->period = tbl->timer_period;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	wdat->wdd.min_hw_heartbeat_ms = wdat->period * tbl->min_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	wdat->wdd.max_hw_heartbeat_ms = wdat->period * tbl->max_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	wdat->stopped_in_sleep = tbl->flags & ACPI_WDAT_STOPPED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	wdat->wdd.info = &wdat_wdt_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	wdat->wdd.ops = &wdat_wdt_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	wdat->pdev = pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	/* Request and map all resources */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	for (i = 0; i < pdev->num_resources; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		void __iomem *reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 		res = &pdev->resource[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 		if (resource_type(res) == IORESOURCE_MEM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 			reg = devm_ioremap_resource(dev, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 			if (IS_ERR(reg))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 				return PTR_ERR(reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 		} else if (resource_type(res) == IORESOURCE_IO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 			reg = devm_ioport_map(dev, res->start, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 			if (!reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 				return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 			dev_err(dev, "Unsupported resource\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 			return -EINVAL;
^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) 		regs[i] = reg;
^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) 	entries = (struct acpi_wdat_entry *)(tbl + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	for (i = 0; i < tbl->entries; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		const struct acpi_generic_address *gas;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 		struct wdat_instruction *instr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		struct list_head *instructions;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 		unsigned int action;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		struct resource r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		int j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 		action = entries[i].action;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 		if (action >= MAX_WDAT_ACTIONS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 			dev_dbg(dev, "Skipping unknown action: %u\n", action);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 		instr = devm_kzalloc(dev, sizeof(*instr), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		if (!instr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		INIT_LIST_HEAD(&instr->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		instr->entry = entries[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 		gas = &entries[i].register_region;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		memset(&r, 0, sizeof(r));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		r.start = gas->address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 		r.end = r.start + ACPI_ACCESS_BYTE_WIDTH(gas->access_width) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		if (gas->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 			r.flags = IORESOURCE_MEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		} else if (gas->space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 			r.flags = IORESOURCE_IO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 			dev_dbg(dev, "Unsupported address space: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 				gas->space_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 		/* Find the matching resource */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 		for (j = 0; j < pdev->num_resources; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 			res = &pdev->resource[j];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 			if (resource_contains(res, &r)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 				instr->reg = regs[j] + r.start - res->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 			}
^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) 		if (!instr->reg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 			dev_err(dev, "I/O resource not found\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 		instructions = wdat->instructions[action];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 		if (!instructions) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 			instructions = devm_kzalloc(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 						    sizeof(*instructions),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 						    GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 			if (!instructions)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 				return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 			INIT_LIST_HEAD(instructions);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 			wdat->instructions[action] = instructions;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 		list_add_tail(&instr->node, instructions);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	wdat_wdt_boot_status(wdat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	wdat_wdt_set_running(wdat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	ret = wdat_wdt_enable_reboot(wdat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	platform_set_drvdata(pdev, wdat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	 * Set initial timeout so that userspace has time to configure the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	 * watchdog properly after it has opened the device. In some cases
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	 * the BIOS default is too short and causes immediate reboot.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	if (timeout * 1000 < wdat->wdd.min_hw_heartbeat_ms ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	    timeout * 1000 > wdat->wdd.max_hw_heartbeat_ms) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 		dev_warn(dev, "Invalid timeout %d given, using %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 			 timeout, WDAT_DEFAULT_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 		timeout = WDAT_DEFAULT_TIMEOUT;
^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) 	ret = wdat_wdt_set_timeout(&wdat->wdd, timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	watchdog_set_nowayout(&wdat->wdd, nowayout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	return devm_watchdog_register_device(dev, &wdat->wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) #ifdef CONFIG_PM_SLEEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) static int wdat_wdt_suspend_noirq(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	struct wdat_wdt *wdat = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	if (!watchdog_active(&wdat->wdd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	 * We need to stop the watchdog if firmare is not doing it or if we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	 * are going suspend to idle (where firmware is not involved). If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	 * firmware is stopping the watchdog we kick it here one more time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	 * to give it some time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	wdat->stopped = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	if (acpi_target_system_state() == ACPI_STATE_S0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	    !wdat->stopped_in_sleep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 		ret = wdat_wdt_stop(&wdat->wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 		if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 			wdat->stopped = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 		ret = wdat_wdt_ping(&wdat->wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	return ret;
^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) static int wdat_wdt_resume_noirq(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	struct wdat_wdt *wdat = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	if (!watchdog_active(&wdat->wdd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	if (!wdat->stopped) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 		 * Looks like the boot firmware reinitializes the watchdog
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 		 * before it hands off to the OS on resume from sleep so we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 		 * stop and reprogram the watchdog here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 		ret = wdat_wdt_stop(&wdat->wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 		ret = wdat_wdt_set_timeout(&wdat->wdd, wdat->wdd.timeout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 		ret = wdat_wdt_enable_reboot(wdat);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 		ret = wdat_wdt_ping(&wdat->wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	return wdat_wdt_start(&wdat->wdd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) static const struct dev_pm_ops wdat_wdt_pm_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(wdat_wdt_suspend_noirq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 				      wdat_wdt_resume_noirq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) static struct platform_driver wdat_wdt_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	.probe = wdat_wdt_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 		.name = "wdat_wdt",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 		.pm = &wdat_wdt_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	},
^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) module_platform_driver(wdat_wdt_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) MODULE_DESCRIPTION("ACPI Hardware Watchdog (WDAT) driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) MODULE_ALIAS("platform:wdat_wdt");