1 // SPDX-License-Identifier: GPL-2.0-only
3 * ACPI Hardware Watchdog (WDAT) driver.
5 * Copyright (C) 2016, Intel Corporation
6 * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
9 #include <linux/acpi.h>
10 #include <linux/ioport.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
14 #include <linux/watchdog.h>
16 #define MAX_WDAT_ACTIONS ACPI_WDAT_ACTION_RESERVED
19 * struct wdat_instruction - Single ACPI WDAT instruction
20 * @entry: Copy of the ACPI table instruction
21 * @reg: Register the instruction is accessing
22 * @node: Next instruction in action sequence
24 struct wdat_instruction {
25 struct acpi_wdat_entry entry;
27 struct list_head node;
31 * struct wdat_wdt - ACPI WDAT watchdog device
32 * @pdev: Parent platform device
33 * @wdd: Watchdog core device
34 * @period: How long is one watchdog period in ms
35 * @stopped_in_sleep: Is this watchdog stopped by the firmware in S1-S5
36 * @stopped: Was the watchdog stopped by the driver in suspend
37 * @instructions: An array of instruction lists indexed by an action number from
38 * the WDAT table. There can be %NULL entries for not implemented
42 struct platform_device *pdev;
43 struct watchdog_device wdd;
45 bool stopped_in_sleep;
47 struct list_head *instructions[MAX_WDAT_ACTIONS];
50 #define to_wdat_wdt(wdd) container_of(wdd, struct wdat_wdt, wdd)
52 static bool nowayout = WATCHDOG_NOWAYOUT;
53 module_param(nowayout, bool, 0);
54 MODULE_PARM_DESC(nowayout, "Watchdog cannot be stopped once started (default="
55 __MODULE_STRING(WATCHDOG_NOWAYOUT) ")");
57 #define WDAT_DEFAULT_TIMEOUT 30
59 static int timeout = WDAT_DEFAULT_TIMEOUT;
60 module_param(timeout, int, 0);
61 MODULE_PARM_DESC(timeout, "Watchdog timeout in seconds (default="
62 __MODULE_STRING(WDAT_DEFAULT_TIMEOUT) ")");
64 static int wdat_wdt_read(struct wdat_wdt *wdat,
65 const struct wdat_instruction *instr, u32 *value)
67 const struct acpi_generic_address *gas = &instr->entry.register_region;
69 switch (gas->access_width) {
71 *value = ioread8(instr->reg);
74 *value = ioread16(instr->reg);
77 *value = ioread32(instr->reg);
83 dev_dbg(&wdat->pdev->dev, "Read %#x from 0x%08llx\n", *value,
89 static int wdat_wdt_write(struct wdat_wdt *wdat,
90 const struct wdat_instruction *instr, u32 value)
92 const struct acpi_generic_address *gas = &instr->entry.register_region;
94 switch (gas->access_width) {
96 iowrite8((u8)value, instr->reg);
99 iowrite16((u16)value, instr->reg);
102 iowrite32(value, instr->reg);
108 dev_dbg(&wdat->pdev->dev, "Wrote %#x to 0x%08llx\n", value,
114 static int wdat_wdt_run_action(struct wdat_wdt *wdat, unsigned int action,
115 u32 param, u32 *retval)
117 struct wdat_instruction *instr;
119 if (action >= ARRAY_SIZE(wdat->instructions))
122 if (!wdat->instructions[action])
125 dev_dbg(&wdat->pdev->dev, "Running action %#x\n", action);
127 /* Run each instruction sequentially */
128 list_for_each_entry(instr, wdat->instructions[action], node) {
129 const struct acpi_wdat_entry *entry = &instr->entry;
130 const struct acpi_generic_address *gas;
131 u32 flags, value, mask, x, y;
135 gas = &entry->register_region;
137 preserve = entry->instruction & ACPI_WDAT_PRESERVE_REGISTER;
138 flags = entry->instruction & ~ACPI_WDAT_PRESERVE_REGISTER;
139 value = entry->value;
143 case ACPI_WDAT_READ_VALUE:
144 ret = wdat_wdt_read(wdat, instr, &x);
147 x >>= gas->bit_offset;
150 *retval = x == value;
153 case ACPI_WDAT_READ_COUNTDOWN:
154 ret = wdat_wdt_read(wdat, instr, &x);
157 x >>= gas->bit_offset;
163 case ACPI_WDAT_WRITE_VALUE:
165 x <<= gas->bit_offset;
167 ret = wdat_wdt_read(wdat, instr, &y);
170 y = y & ~(mask << gas->bit_offset);
173 ret = wdat_wdt_write(wdat, instr, x);
178 case ACPI_WDAT_WRITE_COUNTDOWN:
181 x <<= gas->bit_offset;
183 ret = wdat_wdt_read(wdat, instr, &y);
186 y = y & ~(mask << gas->bit_offset);
189 ret = wdat_wdt_write(wdat, instr, x);
195 dev_err(&wdat->pdev->dev, "Unknown instruction: %u\n",
204 static int wdat_wdt_enable_reboot(struct wdat_wdt *wdat)
209 * WDAT specification says that the watchdog is required to reboot
210 * the system when it fires. However, it also states that it is
211 * recommended to make it configurable through hardware register. We
212 * enable reboot now if it is configurable, just in case.
214 ret = wdat_wdt_run_action(wdat, ACPI_WDAT_SET_REBOOT, 0, NULL);
215 if (ret && ret != -EOPNOTSUPP) {
216 dev_err(&wdat->pdev->dev,
217 "Failed to enable reboot when watchdog triggers\n");
224 static void wdat_wdt_boot_status(struct wdat_wdt *wdat)
229 ret = wdat_wdt_run_action(wdat, ACPI_WDAT_GET_STATUS, 0, &boot_status);
230 if (ret && ret != -EOPNOTSUPP) {
231 dev_err(&wdat->pdev->dev, "Failed to read boot status\n");
236 wdat->wdd.bootstatus = WDIOF_CARDRESET;
238 /* Clear the boot status in case BIOS did not do it */
239 ret = wdat_wdt_run_action(wdat, ACPI_WDAT_SET_STATUS, 0, NULL);
240 if (ret && ret != -EOPNOTSUPP)
241 dev_err(&wdat->pdev->dev, "Failed to clear boot status\n");
244 static void wdat_wdt_set_running(struct wdat_wdt *wdat)
249 ret = wdat_wdt_run_action(wdat, ACPI_WDAT_GET_RUNNING_STATE, 0,
251 if (ret && ret != -EOPNOTSUPP)
252 dev_err(&wdat->pdev->dev, "Failed to read running state\n");
255 set_bit(WDOG_HW_RUNNING, &wdat->wdd.status);
258 static int wdat_wdt_start(struct watchdog_device *wdd)
260 return wdat_wdt_run_action(to_wdat_wdt(wdd),
261 ACPI_WDAT_SET_RUNNING_STATE, 0, NULL);
264 static int wdat_wdt_stop(struct watchdog_device *wdd)
266 return wdat_wdt_run_action(to_wdat_wdt(wdd),
267 ACPI_WDAT_SET_STOPPED_STATE, 0, NULL);
270 static int wdat_wdt_ping(struct watchdog_device *wdd)
272 return wdat_wdt_run_action(to_wdat_wdt(wdd), ACPI_WDAT_RESET, 0, NULL);
275 static int wdat_wdt_set_timeout(struct watchdog_device *wdd,
276 unsigned int timeout)
278 struct wdat_wdt *wdat = to_wdat_wdt(wdd);
279 unsigned int periods;
282 periods = timeout * 1000 / wdat->period;
283 ret = wdat_wdt_run_action(wdat, ACPI_WDAT_SET_COUNTDOWN, periods, NULL);
285 wdd->timeout = timeout;
289 static unsigned int wdat_wdt_get_timeleft(struct watchdog_device *wdd)
291 struct wdat_wdt *wdat = to_wdat_wdt(wdd);
294 wdat_wdt_run_action(wdat, ACPI_WDAT_GET_CURRENT_COUNTDOWN, 0, &periods);
295 return periods * wdat->period / 1000;
298 static const struct watchdog_info wdat_wdt_info = {
299 .options = WDIOF_SETTIMEOUT | WDIOF_KEEPALIVEPING | WDIOF_MAGICCLOSE,
300 .firmware_version = 0,
301 .identity = "wdat_wdt",
304 static struct watchdog_ops wdat_wdt_ops = {
305 .owner = THIS_MODULE,
306 .start = wdat_wdt_start,
307 .stop = wdat_wdt_stop,
308 .ping = wdat_wdt_ping,
309 .set_timeout = wdat_wdt_set_timeout,
312 static int wdat_wdt_probe(struct platform_device *pdev)
314 struct device *dev = &pdev->dev;
315 const struct acpi_wdat_entry *entries;
316 const struct acpi_table_wdat *tbl;
317 struct wdat_wdt *wdat;
318 struct resource *res;
323 status = acpi_get_table(ACPI_SIG_WDAT, 0,
324 (struct acpi_table_header **)&tbl);
325 if (ACPI_FAILURE(status))
328 wdat = devm_kzalloc(dev, sizeof(*wdat), GFP_KERNEL);
332 regs = devm_kcalloc(dev, pdev->num_resources, sizeof(*regs),
337 /* WDAT specification wants to have >= 1ms period */
338 if (tbl->timer_period < 1)
340 if (tbl->min_count > tbl->max_count)
343 wdat->period = tbl->timer_period;
344 wdat->wdd.min_timeout = DIV_ROUND_UP(wdat->period * tbl->min_count, 1000);
345 wdat->wdd.max_timeout = wdat->period * tbl->max_count / 1000;
346 wdat->stopped_in_sleep = tbl->flags & ACPI_WDAT_STOPPED;
347 wdat->wdd.info = &wdat_wdt_info;
348 wdat->wdd.ops = &wdat_wdt_ops;
351 /* Request and map all resources */
352 for (i = 0; i < pdev->num_resources; i++) {
355 res = &pdev->resource[i];
356 if (resource_type(res) == IORESOURCE_MEM) {
357 reg = devm_ioremap_resource(dev, res);
360 } else if (resource_type(res) == IORESOURCE_IO) {
361 reg = devm_ioport_map(dev, res->start, 1);
365 dev_err(dev, "Unsupported resource\n");
372 entries = (struct acpi_wdat_entry *)(tbl + 1);
373 for (i = 0; i < tbl->entries; i++) {
374 const struct acpi_generic_address *gas;
375 struct wdat_instruction *instr;
376 struct list_head *instructions;
381 action = entries[i].action;
382 if (action >= MAX_WDAT_ACTIONS) {
383 dev_dbg(dev, "Skipping unknown action: %u\n", action);
387 instr = devm_kzalloc(dev, sizeof(*instr), GFP_KERNEL);
391 INIT_LIST_HEAD(&instr->node);
392 instr->entry = entries[i];
394 gas = &entries[i].register_region;
396 memset(&r, 0, sizeof(r));
397 r.start = gas->address;
398 r.end = r.start + ACPI_ACCESS_BYTE_WIDTH(gas->access_width) - 1;
399 if (gas->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
400 r.flags = IORESOURCE_MEM;
401 } else if (gas->space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
402 r.flags = IORESOURCE_IO;
404 dev_dbg(dev, "Unsupported address space: %d\n",
409 /* Find the matching resource */
410 for (j = 0; j < pdev->num_resources; j++) {
411 res = &pdev->resource[j];
412 if (resource_contains(res, &r)) {
413 instr->reg = regs[j] + r.start - res->start;
419 dev_err(dev, "I/O resource not found\n");
423 instructions = wdat->instructions[action];
425 instructions = devm_kzalloc(dev,
426 sizeof(*instructions),
431 INIT_LIST_HEAD(instructions);
432 wdat->instructions[action] = instructions;
435 list_add_tail(&instr->node, instructions);
438 if (wdat->instructions[ACPI_WDAT_GET_CURRENT_COUNTDOWN])
439 wdat_wdt_ops.get_timeleft = wdat_wdt_get_timeleft;
441 wdat_wdt_boot_status(wdat);
442 wdat_wdt_set_running(wdat);
444 ret = wdat_wdt_enable_reboot(wdat);
448 platform_set_drvdata(pdev, wdat);
451 * Set initial timeout so that userspace has time to configure the
452 * watchdog properly after it has opened the device. In some cases
453 * the BIOS default is too short and causes immediate reboot.
455 if (watchdog_timeout_invalid(&wdat->wdd, timeout)) {
456 dev_warn(dev, "Invalid timeout %d given, using %d\n",
457 timeout, WDAT_DEFAULT_TIMEOUT);
458 timeout = WDAT_DEFAULT_TIMEOUT;
461 ret = wdat_wdt_set_timeout(&wdat->wdd, timeout);
465 watchdog_set_nowayout(&wdat->wdd, nowayout);
466 watchdog_stop_on_reboot(&wdat->wdd);
467 watchdog_stop_on_unregister(&wdat->wdd);
468 return devm_watchdog_register_device(dev, &wdat->wdd);
471 static int wdat_wdt_suspend_noirq(struct device *dev)
473 struct wdat_wdt *wdat = dev_get_drvdata(dev);
476 if (!watchdog_active(&wdat->wdd))
480 * We need to stop the watchdog if firmware is not doing it or if we
481 * are going suspend to idle (where firmware is not involved). If
482 * firmware is stopping the watchdog we kick it here one more time
483 * to give it some time.
485 wdat->stopped = false;
486 if (acpi_target_system_state() == ACPI_STATE_S0 ||
487 !wdat->stopped_in_sleep) {
488 ret = wdat_wdt_stop(&wdat->wdd);
490 wdat->stopped = true;
492 ret = wdat_wdt_ping(&wdat->wdd);
498 static int wdat_wdt_resume_noirq(struct device *dev)
500 struct wdat_wdt *wdat = dev_get_drvdata(dev);
503 if (!watchdog_active(&wdat->wdd))
506 if (!wdat->stopped) {
508 * Looks like the boot firmware reinitializes the watchdog
509 * before it hands off to the OS on resume from sleep so we
510 * stop and reprogram the watchdog here.
512 ret = wdat_wdt_stop(&wdat->wdd);
516 ret = wdat_wdt_set_timeout(&wdat->wdd, wdat->wdd.timeout);
520 ret = wdat_wdt_enable_reboot(wdat);
524 ret = wdat_wdt_ping(&wdat->wdd);
529 return wdat_wdt_start(&wdat->wdd);
532 static const struct dev_pm_ops wdat_wdt_pm_ops = {
533 NOIRQ_SYSTEM_SLEEP_PM_OPS(wdat_wdt_suspend_noirq, wdat_wdt_resume_noirq)
536 static struct platform_driver wdat_wdt_driver = {
537 .probe = wdat_wdt_probe,
540 .pm = pm_sleep_ptr(&wdat_wdt_pm_ops),
544 module_platform_driver(wdat_wdt_driver);
546 MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
547 MODULE_DESCRIPTION("ACPI Hardware Watchdog (WDAT) driver");
548 MODULE_LICENSE("GPL v2");
549 MODULE_ALIAS("platform:wdat_wdt");