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