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