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