1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2015
4  * Texas Instruments Incorporated - https://www.ti.com/
5  */
6 #include <command.h>
7 #include <dm.h>
8 #include <errno.h>
9 #include <malloc.h>
10 #include <remoteproc.h>
11 
12 /**
13  * print_remoteproc_list() - print all the remote processor devices
14  *
15  * Return: 0 if no error, else returns appropriate error value.
16  */
print_remoteproc_list(void)17 static int print_remoteproc_list(void)
18 {
19 	struct udevice *dev;
20 	struct uclass *uc;
21 	int ret;
22 	char *type;
23 
24 	ret = uclass_get(UCLASS_REMOTEPROC, &uc);
25 	if (ret) {
26 		printf("Cannot find Remote processor class\n");
27 		return ret;
28 	}
29 
30 	uclass_foreach_dev(dev, uc) {
31 		struct dm_rproc_uclass_pdata *uc_pdata;
32 		const struct dm_rproc_ops *ops = rproc_get_ops(dev);
33 
34 		uc_pdata = dev_get_uclass_plat(dev);
35 
36 		/* Do not print if rproc is not probed */
37 		if (!(dev_get_flags(dev) & DM_FLAG_ACTIVATED))
38 			continue;
39 
40 		switch (uc_pdata->mem_type) {
41 		case RPROC_INTERNAL_MEMORY_MAPPED:
42 			type = "internal memory mapped";
43 			break;
44 		default:
45 			type = "unknown";
46 			break;
47 		}
48 		printf("%d - Name:'%s' type:'%s' supports: %s%s%s%s%s%s\n",
49 		       dev_seq(dev),
50 		       uc_pdata->name,
51 		       type,
52 		       ops->load ? "load " : "",
53 		       ops->start ? "start " : "",
54 		       ops->stop ? "stop " : "",
55 		       ops->reset ? "reset " : "",
56 		       ops->is_running ? "is_running " : "",
57 		       ops->ping ? "ping " : "");
58 	}
59 	return 0;
60 }
61 
62 /**
63  * do_rproc_init() - do basic initialization
64  * @cmdtp:	unused
65  * @flag:	unused
66  * @argc:	unused
67  * @argv:	unused
68  *
69  * Return: 0 if no error, else returns appropriate error value.
70  */
do_rproc_init(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])71 static int do_rproc_init(struct cmd_tbl *cmdtp, int flag, int argc,
72 			 char *const argv[])
73 {
74 	int id;
75 
76 	if (rproc_is_initialized()) {
77 		printf("\tRemote Processors are already initialized\n");
78 		return CMD_RET_FAILURE;
79 	}
80 
81 	if (argc == 1) {
82 		if (!rproc_init())
83 			return 0;
84 		printf("Few Remote Processors failed to be initialized\n");
85 	} else if (argc == 2) {
86 		id = (int)dectoul(argv[1], NULL);
87 		if (!rproc_dev_init(id))
88 			return 0;
89 		printf("Remote Processor %d failed to be initialized\n", id);
90 	}
91 
92 	return CMD_RET_FAILURE;
93 }
94 
95 /**
96  * do_remoteproc_list() - print list of remote proc devices.
97  * @cmdtp:	unused
98  * @flag:	unused
99  * @argc:	unused
100  * @argv:	unused
101  *
102  * Return: 0 if no error, else returns appropriate error value.
103  */
do_remoteproc_list(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])104 static int do_remoteproc_list(struct cmd_tbl *cmdtp, int flag, int argc,
105 			      char *const argv[])
106 {
107 	if (print_remoteproc_list())
108 		return CMD_RET_FAILURE;
109 
110 	return 0;
111 }
112 
113 /**
114  * do_remoteproc_load() - Load a remote processor with binary image
115  * @cmdtp:	unused
116  * @flag:	unused
117  * @argc:	argument count for the load function
118  * @argv:	arguments for the load function
119  *
120  * Return: 0 if no error, else returns appropriate error value.
121  */
do_remoteproc_load(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])122 static int do_remoteproc_load(struct cmd_tbl *cmdtp, int flag, int argc,
123 			      char *const argv[])
124 {
125 	ulong addr, size;
126 	int id, ret;
127 
128 	if (argc != 4)
129 		return CMD_RET_USAGE;
130 
131 	id = (int)dectoul(argv[1], NULL);
132 	addr = hextoul(argv[2], NULL);
133 
134 	size = hextoul(argv[3], NULL);
135 
136 	if (!size) {
137 		printf("\t Expect some size??\n");
138 		return CMD_RET_USAGE;
139 	}
140 
141 	ret = rproc_load(id, addr, size);
142 	printf("Load Remote Processor %d with data@addr=0x%08lx %lu bytes:%s\n",
143 	       id, addr, size, ret ? " Failed!" : " Success!");
144 
145 	return ret ? CMD_RET_FAILURE : 0;
146 }
147 
148 /**
149  * do_remoteproc_wrapper() - wrapper for various  rproc commands
150  * @cmdtp:	unused
151  * @flag:	unused
152  * @argc:	argument count for the rproc command
153  * @argv:	arguments for the rproc command
154  *
155  * Most of the commands just take id as a parameter andinvoke various
156  * helper routines in remote processor core. by using a set of
157  * common checks, we can reduce the amount of code used for this.
158  *
159  * Return: 0 if no error, else returns appropriate error value.
160  */
do_remoteproc_wrapper(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])161 static int do_remoteproc_wrapper(struct cmd_tbl *cmdtp, int flag, int argc,
162 				 char *const argv[])
163 {
164 	int id, ret = CMD_RET_USAGE;
165 
166 	if (argc != 2)
167 		return CMD_RET_USAGE;
168 
169 	id = (int)dectoul(argv[1], NULL);
170 
171 	if (!strcmp(argv[0], "start")) {
172 		ret = rproc_start(id);
173 	} else if (!strcmp(argv[0], "stop")) {
174 		ret = rproc_stop(id);
175 	} else if (!strcmp(argv[0], "reset")) {
176 		ret = rproc_reset(id);
177 	} else if (!strcmp(argv[0], "is_running")) {
178 		ret = rproc_is_running(id);
179 		if (!ret) {
180 			printf("Remote processor is Running\n");
181 		} else if (ret == 1) {
182 			printf("Remote processor is NOT Running\n");
183 			ret = 0;
184 		}
185 		/* Else error.. */
186 	} else if (!strcmp(argv[0], "ping")) {
187 		ret = rproc_ping(id);
188 		if (!ret) {
189 			printf("Remote processor responds 'Pong'\n");
190 		} else if (ret == 1) {
191 			printf("No response from Remote processor\n");
192 			ret = 0;
193 		}
194 		/* Else error.. */
195 	}
196 
197 	if (ret < 0)
198 		printf("Operation Failed with error (%d)\n", ret);
199 
200 	return ret ? CMD_RET_FAILURE : 0;
201 }
202 
203 static struct cmd_tbl cmd_remoteproc_sub[] = {
204 	U_BOOT_CMD_MKENT(init, 1, 1, do_rproc_init,
205 			 "Enumerate and initialize the remote processor(s)",
206 			 "id - ID of the remote processor\n"
207 			 "If id is not passed, initialize all the remote processors"),
208 	U_BOOT_CMD_MKENT(list, 0, 1, do_remoteproc_list,
209 			 "list remote processors", ""),
210 	U_BOOT_CMD_MKENT(load, 5, 1, do_remoteproc_load,
211 			 "Load remote processor with provided image",
212 			 "<id> [addr] [size]\n"
213 			 "- id: ID of the remote processor(see 'list' cmd)\n"
214 			 "- addr: Address in memory of the image to loadup\n"
215 			 "- size: Size of the image to loadup\n"),
216 	U_BOOT_CMD_MKENT(start, 1, 1, do_remoteproc_wrapper,
217 			 "Start remote processor",
218 			 "id - ID of the remote processor (see 'list' cmd)\n"),
219 	U_BOOT_CMD_MKENT(stop, 1, 1, do_remoteproc_wrapper,
220 			 "Stop remote processor",
221 			 "id - ID of the remote processor (see 'list' cmd)\n"),
222 	U_BOOT_CMD_MKENT(reset, 1, 1, do_remoteproc_wrapper,
223 			 "Reset remote processor",
224 			 "id - ID of the remote processor (see 'list' cmd)\n"),
225 	U_BOOT_CMD_MKENT(is_running, 1, 1, do_remoteproc_wrapper,
226 			 "Check to see if remote processor is running\n",
227 			 "id - ID of the remote processor (see 'list' cmd)\n"),
228 	U_BOOT_CMD_MKENT(ping, 1, 1, do_remoteproc_wrapper,
229 			 "Ping to communicate with remote processor\n",
230 			 "id - ID of the remote processor (see 'list' cmd)\n"),
231 };
232 
233 /**
234  * do_remoteproc() - (replace: short desc)
235  * @cmdtp:	unused
236  * @flag:	unused
237  * @argc:	argument count
238  * @argv:	argument list
239  *
240  * parses up the command table to invoke the correct command.
241  *
242  * Return: 0 if no error, else returns appropriate error value.
243  */
do_remoteproc(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])244 static int do_remoteproc(struct cmd_tbl *cmdtp, int flag, int argc,
245 			 char *const argv[])
246 {
247 	struct cmd_tbl *c = NULL;
248 
249 	/* Strip off leading 'rproc' command argument */
250 	argc--;
251 	argv++;
252 
253 	if (argc)
254 		c = find_cmd_tbl(argv[0], cmd_remoteproc_sub,
255 				 ARRAY_SIZE(cmd_remoteproc_sub));
256 	if (c)
257 		return c->cmd(cmdtp, flag, argc, argv);
258 
259 	return CMD_RET_USAGE;
260 }
261 
262 U_BOOT_CMD(rproc, 5, 1, do_remoteproc,
263 	   "Control operation of remote processors in an SoC",
264 	   " [init|list|load|start|stop|reset|is_running|ping]\n"
265 	   "\t\t Where:\n"
266 	   "\t\t[addr] is a memory address\n"
267 	   "\t\t<id> is a numerical identifier for the remote processor\n"
268 	   "\t\t     provided by 'list' command.\n"
269 	   "\t\tNote: Remote processors must be initalized prior to usage\n"
270 	   "\t\tNote: Services are dependent on the driver capability\n"
271 	   "\t\t      'list' command shows the capability of each device\n"
272 	   "\n\tSubcommands:\n"
273 	   "\tinit <id> - Enumerate and initalize the remote processor.\n"
274 	   "\t		  if id is not passed, initialize all the remote prcessors\n"
275 	   "\tlist   - list available remote processors\n"
276 	   "\tload <id> [addr] [size]- Load the remote processor with binary\n"
277 	   "\t		  image stored at address [addr] in memory\n"
278 	   "\tstart <id>	- Start the remote processor(must be loaded)\n"
279 	   "\tstop <id>	- Stop the remote processor\n"
280 	   "\treset <id>	- Reset the remote processor\n"
281 	   "\tis_running <id> - Reports if the remote processor is running\n"
282 	   "\tping <id>	- Ping the remote processor for communication\n");
283