Merge branch '2024-11-22-assorted-fixes'
[u-boot.git] / cmd / wdt.c
blobc7a06cca181f9daafe28dfba46f935e0dd2c43f7
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Watchdog commands
5 * Copyright (c) 2019 Michael Walle <michael@walle.cc>
6 */
8 #include <command.h>
9 #include <dm.h>
10 #include <wdt.h>
12 static struct udevice *currdev;
14 static int do_wdt_list(struct cmd_tbl *cmdtp, int flag, int argc,
15 char *const argv[])
17 struct udevice *dev;
18 struct uclass *uc;
19 int ret;
21 ret = uclass_get(UCLASS_WDT, &uc);
22 if (ret)
23 return CMD_RET_FAILURE;
25 uclass_foreach_dev(dev, uc)
26 printf("%s (%s)\n", dev->name, dev->driver->name);
28 return CMD_RET_SUCCESS;
31 static int do_wdt_dev(struct cmd_tbl *cmdtp, int flag, int argc,
32 char *const argv[])
34 int ret;
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;
42 } else {
43 if (!currdev) {
44 printf("No watchdog timer device set!\n");
45 return CMD_RET_FAILURE;
47 printf("dev: %s\n", currdev->name);
50 return CMD_RET_SUCCESS;
53 static int check_currdev(void)
55 if (!currdev) {
56 printf("No device set, use 'wdt dev' first\n");
57 return CMD_RET_FAILURE;
59 return 0;
62 static int do_wdt_start(struct cmd_tbl *cmdtp, int flag, int argc,
63 char *const argv[])
65 int ret;
66 u64 timeout;
67 ulong flags = 0;
69 if (argc < 2)
70 return CMD_RET_USAGE;
72 ret = check_currdev();
73 if (ret)
74 return ret;
76 timeout = simple_strtoull(argv[1], NULL, 0);
77 if (argc > 2)
78 flags = simple_strtoul(argv[2], NULL, 0);
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;
89 return CMD_RET_SUCCESS;
92 static int do_wdt_stop(struct cmd_tbl *cmdtp, int flag, int argc,
93 char *const argv[])
95 int ret;
97 ret = check_currdev();
98 if (ret)
99 return ret;
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;
110 return CMD_RET_SUCCESS;
113 static int do_wdt_reset(struct cmd_tbl *cmdtp, int flag, int argc,
114 char *const argv[])
116 int ret;
118 ret = check_currdev();
119 if (ret)
120 return ret;
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;
131 return CMD_RET_SUCCESS;
134 static int do_wdt_expire(struct cmd_tbl *cmdtp, int flag, int argc,
135 char *const argv[])
137 int ret;
138 ulong flags = 0;
140 ret = check_currdev();
141 if (ret)
142 return ret;
144 if (argc > 1)
145 flags = simple_strtoul(argv[1], NULL, 0);
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;
156 return CMD_RET_SUCCESS;
159 U_BOOT_LONGHELP(wdt,
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"
165 "wdt expire [flags] - expire watchdog timer immediately\n");
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));