Michael Walle | 23964a8 | 2019-04-06 02:24:02 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Watchdog commands |
| 4 | * |
| 5 | * Copyright (c) 2019 Michael Walle <michael@walle.cc> |
| 6 | */ |
| 7 | |
Michael Walle | 23964a8 | 2019-04-06 02:24:02 +0200 | [diff] [blame] | 8 | #include <command.h> |
| 9 | #include <dm.h> |
| 10 | #include <wdt.h> |
| 11 | |
| 12 | static struct udevice *currdev; |
| 13 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 14 | static int do_wdt_list(struct cmd_tbl *cmdtp, int flag, int argc, |
Michael Walle | 23964a8 | 2019-04-06 02:24:02 +0200 | [diff] [blame] | 15 | char *const argv[]) |
| 16 | { |
| 17 | struct udevice *dev; |
| 18 | struct uclass *uc; |
| 19 | int ret; |
| 20 | |
| 21 | ret = uclass_get(UCLASS_WDT, &uc); |
| 22 | if (ret) |
| 23 | return CMD_RET_FAILURE; |
| 24 | |
| 25 | uclass_foreach_dev(dev, uc) |
| 26 | printf("%s (%s)\n", dev->name, dev->driver->name); |
| 27 | |
| 28 | return CMD_RET_SUCCESS; |
| 29 | } |
| 30 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 31 | static int do_wdt_dev(struct cmd_tbl *cmdtp, int flag, int argc, |
Michael Walle | 23964a8 | 2019-04-06 02:24:02 +0200 | [diff] [blame] | 32 | char *const argv[]) |
| 33 | { |
| 34 | int ret; |
| 35 | |
| 36 | if (argc > 1) { |
| 37 | ret = uclass_get_device_by_name(UCLASS_WDT, argv[1], &currdev); |
| 38 | if (ret) { |
| 39 | printf("Can't get the watchdog timer: %s\n", argv[1]); |
| 40 | return CMD_RET_FAILURE; |
| 41 | } |
| 42 | } else { |
| 43 | if (!currdev) { |
| 44 | printf("No watchdog timer device set!\n"); |
| 45 | return CMD_RET_FAILURE; |
| 46 | } |
| 47 | printf("dev: %s\n", currdev->name); |
| 48 | } |
| 49 | |
| 50 | return CMD_RET_SUCCESS; |
| 51 | } |
| 52 | |
| 53 | static int check_currdev(void) |
| 54 | { |
| 55 | if (!currdev) { |
| 56 | printf("No device set, use 'wdt dev' first\n"); |
| 57 | return CMD_RET_FAILURE; |
| 58 | } |
| 59 | return 0; |
| 60 | } |
| 61 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 62 | static int do_wdt_start(struct cmd_tbl *cmdtp, int flag, int argc, |
Michael Walle | 23964a8 | 2019-04-06 02:24:02 +0200 | [diff] [blame] | 63 | char *const argv[]) |
| 64 | { |
| 65 | int ret; |
| 66 | u64 timeout; |
| 67 | ulong flags = 0; |
| 68 | |
| 69 | if (argc < 2) |
| 70 | return CMD_RET_USAGE; |
| 71 | |
| 72 | ret = check_currdev(); |
| 73 | if (ret) |
| 74 | return ret; |
| 75 | |
| 76 | timeout = simple_strtoull(argv[1], NULL, 0); |
| 77 | if (argc > 2) |
| 78 | flags = simple_strtoul(argv[2], NULL, 0); |
| 79 | |
| 80 | ret = wdt_start(currdev, timeout, flags); |
| 81 | if (ret == -ENOSYS) { |
| 82 | printf("Starting watchdog timer not supported.\n"); |
| 83 | return CMD_RET_FAILURE; |
| 84 | } else if (ret) { |
| 85 | printf("Starting watchdog timer failed (%d)\n", ret); |
| 86 | return CMD_RET_FAILURE; |
| 87 | } |
| 88 | |
| 89 | return CMD_RET_SUCCESS; |
| 90 | } |
| 91 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 92 | static int do_wdt_stop(struct cmd_tbl *cmdtp, int flag, int argc, |
Michael Walle | 23964a8 | 2019-04-06 02:24:02 +0200 | [diff] [blame] | 93 | char *const argv[]) |
| 94 | { |
| 95 | int ret; |
| 96 | |
| 97 | ret = check_currdev(); |
| 98 | if (ret) |
| 99 | return ret; |
| 100 | |
| 101 | ret = wdt_stop(currdev); |
| 102 | if (ret == -ENOSYS) { |
| 103 | printf("Stopping watchdog timer not supported.\n"); |
| 104 | return CMD_RET_FAILURE; |
| 105 | } else if (ret) { |
| 106 | printf("Stopping watchdog timer failed (%d)\n", ret); |
| 107 | return CMD_RET_FAILURE; |
| 108 | } |
| 109 | |
| 110 | return CMD_RET_SUCCESS; |
| 111 | } |
| 112 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 113 | static int do_wdt_reset(struct cmd_tbl *cmdtp, int flag, int argc, |
Michael Walle | 23964a8 | 2019-04-06 02:24:02 +0200 | [diff] [blame] | 114 | char *const argv[]) |
| 115 | { |
| 116 | int ret; |
| 117 | |
| 118 | ret = check_currdev(); |
| 119 | if (ret) |
| 120 | return ret; |
| 121 | |
| 122 | ret = wdt_reset(currdev); |
| 123 | if (ret == -ENOSYS) { |
| 124 | printf("Resetting watchdog timer not supported.\n"); |
| 125 | return CMD_RET_FAILURE; |
| 126 | } else if (ret) { |
| 127 | printf("Resetting watchdog timer failed (%d)\n", ret); |
| 128 | return CMD_RET_FAILURE; |
| 129 | } |
| 130 | |
| 131 | return CMD_RET_SUCCESS; |
| 132 | } |
| 133 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 134 | static int do_wdt_expire(struct cmd_tbl *cmdtp, int flag, int argc, |
Michael Walle | 23964a8 | 2019-04-06 02:24:02 +0200 | [diff] [blame] | 135 | char *const argv[]) |
| 136 | { |
| 137 | int ret; |
| 138 | ulong flags = 0; |
| 139 | |
| 140 | ret = check_currdev(); |
| 141 | if (ret) |
| 142 | return ret; |
| 143 | |
| 144 | if (argc > 1) |
| 145 | flags = simple_strtoul(argv[1], NULL, 0); |
| 146 | |
| 147 | ret = wdt_expire_now(currdev, flags); |
| 148 | if (ret == -ENOSYS) { |
| 149 | printf("Expiring watchdog timer not supported.\n"); |
| 150 | return CMD_RET_FAILURE; |
| 151 | } else if (ret) { |
| 152 | printf("Expiring watchdog timer failed (%d)\n", ret); |
| 153 | return CMD_RET_FAILURE; |
| 154 | } |
| 155 | |
| 156 | return CMD_RET_SUCCESS; |
| 157 | } |
| 158 | |
Tom Rini | 03f146c | 2023-10-07 15:13:08 -0400 | [diff] [blame] | 159 | U_BOOT_LONGHELP(wdt, |
Michael Walle | 23964a8 | 2019-04-06 02:24:02 +0200 | [diff] [blame] | 160 | "list - list watchdog devices\n" |
| 161 | "wdt dev [<name>] - get/set current watchdog device\n" |
| 162 | "wdt start <timeout ms> [flags] - start watchdog timer\n" |
| 163 | "wdt stop - stop watchdog timer\n" |
| 164 | "wdt reset - reset watchdog timer\n" |
Tom Rini | 03f146c | 2023-10-07 15:13:08 -0400 | [diff] [blame] | 165 | "wdt expire [flags] - expire watchdog timer immediately\n"); |
Michael Walle | 23964a8 | 2019-04-06 02:24:02 +0200 | [diff] [blame] | 166 | |
| 167 | U_BOOT_CMD_WITH_SUBCMDS(wdt, "Watchdog sub-system", wdt_help_text, |
| 168 | U_BOOT_SUBCMD_MKENT(list, 1, 1, do_wdt_list), |
| 169 | U_BOOT_SUBCMD_MKENT(dev, 2, 1, do_wdt_dev), |
| 170 | U_BOOT_SUBCMD_MKENT(start, 3, 1, do_wdt_start), |
| 171 | U_BOOT_SUBCMD_MKENT(stop, 1, 1, do_wdt_stop), |
| 172 | U_BOOT_SUBCMD_MKENT(reset, 1, 1, do_wdt_reset), |
| 173 | U_BOOT_SUBCMD_MKENT(expire, 2, 1, do_wdt_expire)); |