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