xref: /openbmc/u-boot/cmd/remoteproc.c (revision 6f443330)
183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
22e192b24SSimon Glass /*
32e192b24SSimon Glass  * (C) Copyright 2015
42e192b24SSimon Glass  * Texas Instruments Incorporated - http://www.ti.com/
52e192b24SSimon Glass  */
62e192b24SSimon Glass #include <common.h>
72e192b24SSimon Glass #include <command.h>
82e192b24SSimon Glass #include <dm.h>
92e192b24SSimon Glass #include <errno.h>
102e192b24SSimon Glass #include <malloc.h>
112e192b24SSimon Glass #include <remoteproc.h>
122e192b24SSimon Glass 
132e192b24SSimon Glass /**
142e192b24SSimon Glass  * print_remoteproc_list() - print all the remote processor devices
152e192b24SSimon Glass  *
162e192b24SSimon Glass  * Return: 0 if no error, else returns appropriate error value.
172e192b24SSimon Glass  */
print_remoteproc_list(void)182e192b24SSimon Glass static int print_remoteproc_list(void)
192e192b24SSimon Glass {
202e192b24SSimon Glass 	struct udevice *dev;
212e192b24SSimon Glass 	struct uclass *uc;
222e192b24SSimon Glass 	int ret;
232e192b24SSimon Glass 	char *type;
242e192b24SSimon Glass 
252e192b24SSimon Glass 	ret = uclass_get(UCLASS_REMOTEPROC, &uc);
262e192b24SSimon Glass 	if (ret) {
272e192b24SSimon Glass 		printf("Cannot find Remote processor class\n");
282e192b24SSimon Glass 		return ret;
292e192b24SSimon Glass 	}
302e192b24SSimon Glass 
312e192b24SSimon Glass 	uclass_foreach_dev(dev, uc) {
322e192b24SSimon Glass 		struct dm_rproc_uclass_pdata *uc_pdata;
332e192b24SSimon Glass 		const struct dm_rproc_ops *ops = rproc_get_ops(dev);
342e192b24SSimon Glass 
352e192b24SSimon Glass 		uc_pdata = dev_get_uclass_platdata(dev);
362e192b24SSimon Glass 
372e192b24SSimon Glass 		switch (uc_pdata->mem_type) {
382e192b24SSimon Glass 		case RPROC_INTERNAL_MEMORY_MAPPED:
392e192b24SSimon Glass 			type = "internal memory mapped";
402e192b24SSimon Glass 			break;
412e192b24SSimon Glass 		default:
422e192b24SSimon Glass 			type = "unknown";
432e192b24SSimon Glass 			break;
442e192b24SSimon Glass 		}
452e192b24SSimon Glass 		printf("%d - Name:'%s' type:'%s' supports: %s%s%s%s%s%s\n",
462e192b24SSimon Glass 		       dev->seq,
472e192b24SSimon Glass 		       uc_pdata->name,
482e192b24SSimon Glass 		       type,
492e192b24SSimon Glass 		       ops->load ? "load " : "",
502e192b24SSimon Glass 		       ops->start ? "start " : "",
512e192b24SSimon Glass 		       ops->stop ? "stop " : "",
522e192b24SSimon Glass 		       ops->reset ? "reset " : "",
532e192b24SSimon Glass 		       ops->is_running ? "is_running " : "",
542e192b24SSimon Glass 		       ops->ping ? "ping " : "");
552e192b24SSimon Glass 	}
562e192b24SSimon Glass 	return 0;
572e192b24SSimon Glass }
582e192b24SSimon Glass 
592e192b24SSimon Glass /**
602e192b24SSimon Glass  * do_rproc_init() - do basic initialization
612e192b24SSimon Glass  * @cmdtp:	unused
622e192b24SSimon Glass  * @flag:	unused
632e192b24SSimon Glass  * @argc:	unused
642e192b24SSimon Glass  * @argv:	unused
652e192b24SSimon Glass  *
662e192b24SSimon Glass  * Return: 0 if no error, else returns appropriate error value.
672e192b24SSimon Glass  */
do_rproc_init(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])682e192b24SSimon Glass static int do_rproc_init(cmd_tbl_t *cmdtp, int flag, int argc,
692e192b24SSimon Glass 			 char *const argv[])
702e192b24SSimon Glass {
712e192b24SSimon Glass 	if (rproc_is_initialized()) {
722e192b24SSimon Glass 		printf("\tRemote Processors are already initialized\n");
732e192b24SSimon Glass 	} else {
742e192b24SSimon Glass 		if (!rproc_init())
752e192b24SSimon Glass 			return 0;
762e192b24SSimon Glass 		printf("Few Remote Processors failed to be initalized\n");
772e192b24SSimon Glass 	}
782e192b24SSimon Glass 
792e192b24SSimon Glass 	return CMD_RET_FAILURE;
802e192b24SSimon Glass }
812e192b24SSimon Glass 
822e192b24SSimon Glass /**
832e192b24SSimon Glass  * do_remoteproc_list() - print list of remote proc devices.
842e192b24SSimon Glass  * @cmdtp:	unused
852e192b24SSimon Glass  * @flag:	unused
862e192b24SSimon Glass  * @argc:	unused
872e192b24SSimon Glass  * @argv:	unused
882e192b24SSimon Glass  *
892e192b24SSimon Glass  * Return: 0 if no error, else returns appropriate error value.
902e192b24SSimon Glass  */
do_remoteproc_list(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])912e192b24SSimon Glass static int do_remoteproc_list(cmd_tbl_t *cmdtp, int flag, int argc,
922e192b24SSimon Glass 			      char *const argv[])
932e192b24SSimon Glass {
942e192b24SSimon Glass 	if (!rproc_is_initialized()) {
952e192b24SSimon Glass 		printf("\t Remote Processors is not initialized\n");
962e192b24SSimon Glass 		return CMD_RET_USAGE;
972e192b24SSimon Glass 	}
982e192b24SSimon Glass 
992e192b24SSimon Glass 	if (print_remoteproc_list())
1002e192b24SSimon Glass 		return CMD_RET_FAILURE;
1012e192b24SSimon Glass 
1022e192b24SSimon Glass 	return 0;
1032e192b24SSimon Glass }
1042e192b24SSimon Glass 
1052e192b24SSimon Glass /**
1062e192b24SSimon Glass  * do_remoteproc_load() - Load a remote processor with binary image
1072e192b24SSimon Glass  * @cmdtp:	unused
1082e192b24SSimon Glass  * @flag:	unused
1092e192b24SSimon Glass  * @argc:	argument count for the load function
1102e192b24SSimon Glass  * @argv:	arguments for the load function
1112e192b24SSimon Glass  *
1122e192b24SSimon Glass  * Return: 0 if no error, else returns appropriate error value.
1132e192b24SSimon Glass  */
do_remoteproc_load(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])1142e192b24SSimon Glass static int do_remoteproc_load(cmd_tbl_t *cmdtp, int flag, int argc,
1152e192b24SSimon Glass 			      char *const argv[])
1162e192b24SSimon Glass {
1172e192b24SSimon Glass 	ulong addr, size;
1182e192b24SSimon Glass 	int id, ret;
1192e192b24SSimon Glass 
1202e192b24SSimon Glass 	if (argc != 4)
1212e192b24SSimon Glass 		return CMD_RET_USAGE;
1222e192b24SSimon Glass 
123*1a25d907SKeerthy 	id = (int)simple_strtoul(argv[1], NULL, 10);
1242e192b24SSimon Glass 	addr = simple_strtoul(argv[2], NULL, 16);
1252e192b24SSimon Glass 
1262e192b24SSimon Glass 	size = simple_strtoul(argv[3], NULL, 16);
1272e192b24SSimon Glass 
1282e192b24SSimon Glass 	if (!size) {
1292e192b24SSimon Glass 		printf("\t Expect some size??\n");
1302e192b24SSimon Glass 		return CMD_RET_USAGE;
1312e192b24SSimon Glass 	}
1322e192b24SSimon Glass 
1332e192b24SSimon Glass 	if (!rproc_is_initialized()) {
1342e192b24SSimon Glass 		printf("\tRemote Processors are not initialized\n");
1352e192b24SSimon Glass 		return CMD_RET_USAGE;
1362e192b24SSimon Glass 	}
1372e192b24SSimon Glass 
1382e192b24SSimon Glass 	ret = rproc_load(id, addr, size);
1392e192b24SSimon Glass 	printf("Load Remote Processor %d with data@addr=0x%08lx %lu bytes:%s\n",
1402e192b24SSimon Glass 	       id, addr, size, ret ? " Failed!" : " Success!");
1412e192b24SSimon Glass 
1422e192b24SSimon Glass 	return ret ? CMD_RET_FAILURE : 0;
1432e192b24SSimon Glass }
1442e192b24SSimon Glass 
1452e192b24SSimon Glass /**
1462e192b24SSimon Glass  * do_remoteproc_wrapper() - wrapper for various  rproc commands
1472e192b24SSimon Glass  * @cmdtp:	unused
1482e192b24SSimon Glass  * @flag:	unused
1492e192b24SSimon Glass  * @argc:	argument count for the rproc command
1502e192b24SSimon Glass  * @argv:	arguments for the rproc command
1512e192b24SSimon Glass  *
1522e192b24SSimon Glass  * Most of the commands just take id as a parameter andinvoke various
1532e192b24SSimon Glass  * helper routines in remote processor core. by using a set of
1542e192b24SSimon Glass  * common checks, we can reduce the amount of code used for this.
1552e192b24SSimon Glass  *
1562e192b24SSimon Glass  * Return: 0 if no error, else returns appropriate error value.
1572e192b24SSimon Glass  */
do_remoteproc_wrapper(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])1582e192b24SSimon Glass static int do_remoteproc_wrapper(cmd_tbl_t *cmdtp, int flag, int argc,
1592e192b24SSimon Glass 				 char *const argv[])
1602e192b24SSimon Glass {
1612e192b24SSimon Glass 	int id, ret = CMD_RET_USAGE;
1622e192b24SSimon Glass 
1632e192b24SSimon Glass 	if (argc != 2)
1642e192b24SSimon Glass 		return CMD_RET_USAGE;
1652e192b24SSimon Glass 
166*1a25d907SKeerthy 	id = (int)simple_strtoul(argv[1], NULL, 10);
1672e192b24SSimon Glass 
1682e192b24SSimon Glass 	if (!rproc_is_initialized()) {
1692e192b24SSimon Glass 		printf("\tRemote Processors are not initialized\n");
1702e192b24SSimon Glass 		return CMD_RET_USAGE;
1712e192b24SSimon Glass 	}
1722e192b24SSimon Glass 
1732e192b24SSimon Glass 	if (!strcmp(argv[0], "start")) {
1742e192b24SSimon Glass 		ret = rproc_start(id);
1752e192b24SSimon Glass 	} else if (!strcmp(argv[0], "stop")) {
1762e192b24SSimon Glass 		ret = rproc_stop(id);
1772e192b24SSimon Glass 	} else if (!strcmp(argv[0], "reset")) {
1782e192b24SSimon Glass 		ret = rproc_reset(id);
1792e192b24SSimon Glass 	} else if (!strcmp(argv[0], "is_running")) {
1802e192b24SSimon Glass 		ret = rproc_is_running(id);
1812e192b24SSimon Glass 		if (!ret) {
1822e192b24SSimon Glass 			printf("Remote processor is Running\n");
1832e192b24SSimon Glass 		} else if (ret == 1) {
1842e192b24SSimon Glass 			printf("Remote processor is NOT Running\n");
1852e192b24SSimon Glass 			ret = 0;
1862e192b24SSimon Glass 		}
1872e192b24SSimon Glass 		/* Else error.. */
1882e192b24SSimon Glass 	} else if (!strcmp(argv[0], "ping")) {
1892e192b24SSimon Glass 		ret = rproc_ping(id);
1902e192b24SSimon Glass 		if (!ret) {
1912e192b24SSimon Glass 			printf("Remote processor responds 'Pong'\n");
1922e192b24SSimon Glass 		} else if (ret == 1) {
1932e192b24SSimon Glass 			printf("No response from Remote processor\n");
1942e192b24SSimon Glass 			ret = 0;
1952e192b24SSimon Glass 		}
1962e192b24SSimon Glass 		/* Else error.. */
1972e192b24SSimon Glass 	}
1982e192b24SSimon Glass 
1992e192b24SSimon Glass 	if (ret < 0)
2002e192b24SSimon Glass 		printf("Operation Failed with error (%d)\n", ret);
2012e192b24SSimon Glass 
2022e192b24SSimon Glass 	return ret ? CMD_RET_FAILURE : 0;
2032e192b24SSimon Glass }
2042e192b24SSimon Glass 
2052e192b24SSimon Glass static cmd_tbl_t cmd_remoteproc_sub[] = {
2062e192b24SSimon Glass 	U_BOOT_CMD_MKENT(init, 0, 1, do_rproc_init,
2072e192b24SSimon Glass 			 "Enumerate and initialize all processors", ""),
2082e192b24SSimon Glass 	U_BOOT_CMD_MKENT(list, 0, 1, do_remoteproc_list,
2092e192b24SSimon Glass 			 "list remote processors", ""),
2102e192b24SSimon Glass 	U_BOOT_CMD_MKENT(load, 5, 1, do_remoteproc_load,
2112e192b24SSimon Glass 			 "Load remote processor with provided image",
2122e192b24SSimon Glass 			 "<id> [addr] [size]\n"
2132e192b24SSimon Glass 			 "- id: ID of the remote processor(see 'list' cmd)\n"
2142e192b24SSimon Glass 			 "- addr: Address in memory of the image to loadup\n"
2152e192b24SSimon Glass 			 "- size: Size of the image to loadup\n"),
2162e192b24SSimon Glass 	U_BOOT_CMD_MKENT(start, 1, 1, do_remoteproc_wrapper,
2172e192b24SSimon Glass 			 "Start remote processor",
2182e192b24SSimon Glass 			 "id - ID of the remote processor (see 'list' cmd)\n"),
2192e192b24SSimon Glass 	U_BOOT_CMD_MKENT(stop, 1, 1, do_remoteproc_wrapper,
2202e192b24SSimon Glass 			 "Stop remote processor",
2212e192b24SSimon Glass 			 "id - ID of the remote processor (see 'list' cmd)\n"),
2222e192b24SSimon Glass 	U_BOOT_CMD_MKENT(reset, 1, 1, do_remoteproc_wrapper,
2232e192b24SSimon Glass 			 "Reset remote processor",
2242e192b24SSimon Glass 			 "id - ID of the remote processor (see 'list' cmd)\n"),
2252e192b24SSimon Glass 	U_BOOT_CMD_MKENT(is_running, 1, 1, do_remoteproc_wrapper,
2262e192b24SSimon Glass 			 "Check to see if remote processor is running\n",
2272e192b24SSimon Glass 			 "id - ID of the remote processor (see 'list' cmd)\n"),
2282e192b24SSimon Glass 	U_BOOT_CMD_MKENT(ping, 1, 1, do_remoteproc_wrapper,
2292e192b24SSimon Glass 			 "Ping to communicate with remote processor\n",
2302e192b24SSimon Glass 			 "id - ID of the remote processor (see 'list' cmd)\n"),
2312e192b24SSimon Glass };
2322e192b24SSimon Glass 
2332e192b24SSimon Glass /**
2342e192b24SSimon Glass  * do_remoteproc() - (replace: short desc)
2352e192b24SSimon Glass  * @cmdtp:	unused
2362e192b24SSimon Glass  * @flag:	unused
2372e192b24SSimon Glass  * @argc:	argument count
2382e192b24SSimon Glass  * @argv:	argument list
2392e192b24SSimon Glass  *
2402e192b24SSimon Glass  * parses up the command table to invoke the correct command.
2412e192b24SSimon Glass  *
2422e192b24SSimon Glass  * Return: 0 if no error, else returns appropriate error value.
2432e192b24SSimon Glass  */
do_remoteproc(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])2442e192b24SSimon Glass static int do_remoteproc(cmd_tbl_t *cmdtp, int flag, int argc,
2452e192b24SSimon Glass 			 char *const argv[])
2462e192b24SSimon Glass {
2472e192b24SSimon Glass 	cmd_tbl_t *c = NULL;
2482e192b24SSimon Glass 
2492e192b24SSimon Glass 	/* Strip off leading 'rproc' command argument */
2502e192b24SSimon Glass 	argc--;
2512e192b24SSimon Glass 	argv++;
2522e192b24SSimon Glass 
2532e192b24SSimon Glass 	if (argc)
2542e192b24SSimon Glass 		c = find_cmd_tbl(argv[0], cmd_remoteproc_sub,
2552e192b24SSimon Glass 				 ARRAY_SIZE(cmd_remoteproc_sub));
2562e192b24SSimon Glass 	if (c)
2572e192b24SSimon Glass 		return c->cmd(cmdtp, flag, argc, argv);
2582e192b24SSimon Glass 
2592e192b24SSimon Glass 	return CMD_RET_USAGE;
2602e192b24SSimon Glass }
2612e192b24SSimon Glass 
2622e192b24SSimon Glass U_BOOT_CMD(rproc, 5, 1, do_remoteproc,
2632e192b24SSimon Glass 	   "Control operation of remote processors in an SoC",
2642e192b24SSimon Glass 	   " [init|list|load|start|stop|reset|is_running|ping]\n"
2652e192b24SSimon Glass 	   "\t\t Where:\n"
2662e192b24SSimon Glass 	   "\t\t[addr] is a memory address\n"
2672e192b24SSimon Glass 	   "\t\t<id> is a numerical identifier for the remote processor\n"
2682e192b24SSimon Glass 	   "\t\t     provided by 'list' command.\n"
2692e192b24SSimon Glass 	   "\t\tNote: Remote processors must be initalized prior to usage\n"
2702e192b24SSimon Glass 	   "\t\tNote: Services are dependent on the driver capability\n"
2712e192b24SSimon Glass 	   "\t\t      'list' command shows the capability of each device\n"
2722e192b24SSimon Glass 	   "\n\tSubcommands:\n"
2732e192b24SSimon Glass 	   "\tinit   - Enumerate and initalize the remote processors\n"
2742e192b24SSimon Glass 	   "\tlist   - list available remote processors\n"
2752e192b24SSimon Glass 	   "\tload <id> [addr] [size]- Load the remote processor with binary\n"
2762e192b24SSimon Glass 	   "\t		  image stored at address [addr] in memory\n"
2772e192b24SSimon Glass 	   "\tstart <id>	- Start the remote processor(must be loaded)\n"
2782e192b24SSimon Glass 	   "\tstop <id>	- Stop the remote processor\n"
2792e192b24SSimon Glass 	   "\treset <id>	- Reset the remote processor\n"
2802e192b24SSimon Glass 	   "\tis_running <id> - Reports if the remote processor is running\n"
2812e192b24SSimon Glass 	   "\tping <id>	- Ping the remote processor for communication\n");
282