aboutsummaryrefslogtreecommitdiff
path: root/cmd/wdt.c
blob: b9fdf7ad155fae50dbb52817f1d096f5221035b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// SPDX-License-Identifier: GPL-2.0+
/*
 * Watchdog commands
 *
 * Copyright (c) 2019 Michael Walle <michael@walle.cc>
 */

#include <common.h>
#include <command.h>
#include <dm.h>
#include <wdt.h>

static struct udevice *currdev;

static int do_wdt_list(struct cmd_tbl *cmdtp, int flag, int argc,
		       char *const argv[])
{
	struct udevice *dev;
	struct uclass *uc;
	int ret;

	ret = uclass_get(UCLASS_WDT, &uc);
	if (ret)
		return CMD_RET_FAILURE;

	uclass_foreach_dev(dev, uc)
		printf("%s (%s)\n", dev->name, dev->driver->name);

	return CMD_RET_SUCCESS;
}

static int do_wdt_dev(struct cmd_tbl *cmdtp, int flag, int argc,
		      char *const argv[])
{
	int ret;

	if (argc > 1) {
		ret = uclass_get_device_by_name(UCLASS_WDT, argv[1], &currdev);
		if (ret) {
			printf("Can't get the watchdog timer: %s\n", argv[1]);
			return CMD_RET_FAILURE;
		}
	} else {
		if (!currdev) {
			printf("No watchdog timer device set!\n");
			return CMD_RET_FAILURE;
		}
		printf("dev: %s\n", currdev->name);
	}

	return CMD_RET_SUCCESS;
}

static int check_currdev(void)
{
	if (!currdev) {
		printf("No device set, use 'wdt dev' first\n");
		return CMD_RET_FAILURE;
	}
	return 0;
}

static int do_wdt_start(struct cmd_tbl *cmdtp, int flag, int argc,
			char *const argv[])
{
	int ret;
	u64 timeout;
	ulong flags = 0;

	if (argc < 2)
		return CMD_RET_USAGE;

	ret = check_currdev();
	if (ret)
		return ret;

	timeout = simple_strtoull(argv[1], NULL, 0);
	if (argc > 2)
		flags = simple_strtoul(argv[2], NULL, 0);

	ret = wdt_start(currdev, timeout, flags);
	if (ret == -ENOSYS) {
		printf("Starting watchdog timer not supported.\n");
		return CMD_RET_FAILURE;
	} else if (ret) {
		printf("Starting watchdog timer failed (%d)\n", ret);
		return CMD_RET_FAILURE;
	}

	return CMD_RET_SUCCESS;
}

static int do_wdt_stop(struct cmd_tbl *cmdtp, int flag, int argc,
		       char *const argv[])
{
	int ret;

	ret = check_currdev();
	if (ret)
		return ret;

	ret = wdt_stop(currdev);
	if (ret == -ENOSYS) {
		printf("Stopping watchdog timer not supported.\n");
		return CMD_RET_FAILURE;
	} else if (ret) {
		printf("Stopping watchdog timer failed (%d)\n", ret);
		return CMD_RET_FAILURE;
	}

	return CMD_RET_SUCCESS;
}

static int do_wdt_reset(struct cmd_tbl *cmdtp, int flag, int argc,
			char *const argv[])
{
	int ret;

	ret = check_currdev();
	if (ret)
		return ret;

	ret = wdt_reset(currdev);
	if (ret == -ENOSYS) {
		printf("Resetting watchdog timer not supported.\n");
		return CMD_RET_FAILURE;
	} else if (ret) {
		printf("Resetting watchdog timer failed (%d)\n", ret);
		return CMD_RET_FAILURE;
	}

	return CMD_RET_SUCCESS;
}

static int do_wdt_expire(struct cmd_tbl *cmdtp, int flag, int argc,
			 char *const argv[])
{
	int ret;
	ulong flags = 0;

	ret = check_currdev();
	if (ret)
		return ret;

	if (argc > 1)
		flags = simple_strtoul(argv[1], NULL, 0);

	ret = wdt_expire_now(currdev, flags);
	if (ret == -ENOSYS) {
		printf("Expiring watchdog timer not supported.\n");
		return CMD_RET_FAILURE;
	} else if (ret) {
		printf("Expiring watchdog timer failed (%d)\n", ret);
		return CMD_RET_FAILURE;
	}

	return CMD_RET_SUCCESS;
}

U_BOOT_LONGHELP(wdt,
	"list - list watchdog devices\n"
	"wdt dev [<name>] - get/set current watchdog device\n"
	"wdt start <timeout ms> [flags] - start watchdog timer\n"
	"wdt stop - stop watchdog timer\n"
	"wdt reset - reset watchdog timer\n"
	"wdt expire [flags] - expire watchdog timer immediately\n");

U_BOOT_CMD_WITH_SUBCMDS(wdt, "Watchdog sub-system", wdt_help_text,
	U_BOOT_SUBCMD_MKENT(list, 1, 1, do_wdt_list),
	U_BOOT_SUBCMD_MKENT(dev, 2, 1, do_wdt_dev),
	U_BOOT_SUBCMD_MKENT(start, 3, 1, do_wdt_start),
	U_BOOT_SUBCMD_MKENT(stop, 1, 1, do_wdt_stop),
	U_BOOT_SUBCMD_MKENT(reset, 1, 1, do_wdt_reset),
	U_BOOT_SUBCMD_MKENT(expire, 2, 1, do_wdt_expire));