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