Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Álvaro Fernández Rojas | 0da081e | 2017-05-16 18:29:13 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2017 Álvaro Fernández Rojas <noltari@gmail.com> |
Álvaro Fernández Rojas | 0da081e | 2017-05-16 18:29:13 +0200 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
Samuel Holland | 3a8713a | 2021-11-03 22:55:14 -0500 | [diff] [blame] | 8 | #include <dm/device-internal.h> |
Álvaro Fernández Rojas | 0da081e | 2017-05-16 18:29:13 +0200 | [diff] [blame] | 9 | #include <errno.h> |
Samuel Holland | 3a8713a | 2021-11-03 22:55:14 -0500 | [diff] [blame] | 10 | #include <malloc.h> |
Álvaro Fernández Rojas | 0da081e | 2017-05-16 18:29:13 +0200 | [diff] [blame] | 11 | #include <sysreset.h> |
| 12 | #include <wdt.h> |
| 13 | |
Samuel Holland | 90722ba | 2021-11-03 22:55:13 -0500 | [diff] [blame] | 14 | struct wdt_reboot_plat { |
Álvaro Fernández Rojas | 0da081e | 2017-05-16 18:29:13 +0200 | [diff] [blame] | 15 | struct udevice *wdt; |
| 16 | }; |
| 17 | |
| 18 | static int wdt_reboot_request(struct udevice *dev, enum sysreset_t type) |
| 19 | { |
Samuel Holland | 90722ba | 2021-11-03 22:55:13 -0500 | [diff] [blame] | 20 | struct wdt_reboot_plat *plat = dev_get_plat(dev); |
Álvaro Fernández Rojas | 0da081e | 2017-05-16 18:29:13 +0200 | [diff] [blame] | 21 | int ret; |
| 22 | |
Samuel Holland | 90722ba | 2021-11-03 22:55:13 -0500 | [diff] [blame] | 23 | ret = wdt_expire_now(plat->wdt, 0); |
Álvaro Fernández Rojas | 0da081e | 2017-05-16 18:29:13 +0200 | [diff] [blame] | 24 | if (ret) |
| 25 | return ret; |
| 26 | |
| 27 | return -EINPROGRESS; |
| 28 | } |
| 29 | |
| 30 | static struct sysreset_ops wdt_reboot_ops = { |
| 31 | .request = wdt_reboot_request, |
| 32 | }; |
| 33 | |
Samuel Holland | 90722ba | 2021-11-03 22:55:13 -0500 | [diff] [blame] | 34 | static int wdt_reboot_of_to_plat(struct udevice *dev) |
Álvaro Fernández Rojas | 0da081e | 2017-05-16 18:29:13 +0200 | [diff] [blame] | 35 | { |
Samuel Holland | 90722ba | 2021-11-03 22:55:13 -0500 | [diff] [blame] | 36 | struct wdt_reboot_plat *plat = dev_get_plat(dev); |
Álvaro Fernández Rojas | 0da081e | 2017-05-16 18:29:13 +0200 | [diff] [blame] | 37 | int err; |
| 38 | |
| 39 | err = uclass_get_device_by_phandle(UCLASS_WDT, dev, |
Samuel Holland | 90722ba | 2021-11-03 22:55:13 -0500 | [diff] [blame] | 40 | "wdt", &plat->wdt); |
Álvaro Fernández Rojas | 0da081e | 2017-05-16 18:29:13 +0200 | [diff] [blame] | 41 | if (err) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 42 | pr_err("unable to find wdt device\n"); |
Álvaro Fernández Rojas | 0da081e | 2017-05-16 18:29:13 +0200 | [diff] [blame] | 43 | return err; |
| 44 | } |
| 45 | |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | static const struct udevice_id wdt_reboot_ids[] = { |
| 50 | { .compatible = "wdt-reboot" }, |
| 51 | { /* sentinel */ } |
| 52 | }; |
| 53 | |
| 54 | U_BOOT_DRIVER(wdt_reboot) = { |
| 55 | .name = "wdt_reboot", |
| 56 | .id = UCLASS_SYSRESET, |
| 57 | .of_match = wdt_reboot_ids, |
Samuel Holland | 90722ba | 2021-11-03 22:55:13 -0500 | [diff] [blame] | 58 | .of_to_plat = wdt_reboot_of_to_plat, |
| 59 | .plat_auto = sizeof(struct wdt_reboot_plat), |
Álvaro Fernández Rojas | 0da081e | 2017-05-16 18:29:13 +0200 | [diff] [blame] | 60 | .ops = &wdt_reboot_ops, |
Álvaro Fernández Rojas | 0da081e | 2017-05-16 18:29:13 +0200 | [diff] [blame] | 61 | }; |
Samuel Holland | 3a8713a | 2021-11-03 22:55:14 -0500 | [diff] [blame] | 62 | |
| 63 | #if IS_ENABLED(CONFIG_SYSRESET_WATCHDOG_AUTO) |
| 64 | int sysreset_register_wdt(struct udevice *dev) |
| 65 | { |
| 66 | struct wdt_reboot_plat *plat = malloc(sizeof(*plat)); |
| 67 | int ret; |
| 68 | |
| 69 | if (!plat) |
| 70 | return -ENOMEM; |
| 71 | |
| 72 | plat->wdt = dev; |
| 73 | |
| 74 | ret = device_bind(dev, DM_DRIVER_GET(wdt_reboot), |
| 75 | dev->name, plat, ofnode_null(), NULL); |
| 76 | if (ret) { |
| 77 | free(plat); |
| 78 | return ret; |
| 79 | } |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | #endif |