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