command: Remove the cmd_tbl_t typedef
[oweals/u-boot.git] / cmd / axi.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2016
4  * Dirk Eibach,  Guntermann & Drunck GmbH, dirk.eibach@gdsys.cc
5  *
6  * (C) Copyright 2017, 2018
7  * Mario Six,  Guntermann & Drunck GmbH, mario.six@gdsys.cc
8  *
9  * SPDX-License-Identifier:     GPL-2.0+
10  */
11
12 #include <common.h>
13 #include <axi.h>
14 #include <command.h>
15 #include <console.h>
16 #include <dm.h>
17
18 /* Currently selected AXI bus device */
19 static struct udevice *axi_cur_bus;
20 /* Transmission size from last command */
21 static uint dp_last_size;
22 /* Address from last command */
23 static uint dp_last_addr;
24 /* Number of bytes to display from last command; default = 64 */
25 static uint dp_last_length = 0x40;
26
27 /**
28  * show_bus() - Show devices on a single AXI bus
29  * @bus: The AXI bus device to printt information for
30  */
31 static void show_bus(struct udevice *bus)
32 {
33         struct udevice *dev;
34
35         printf("Bus %d:\t%s", bus->req_seq, bus->name);
36         if (device_active(bus))
37                 printf("  (active %d)", bus->seq);
38         printf("\n");
39         for (device_find_first_child(bus, &dev);
40              dev;
41              device_find_next_child(&dev))
42                 printf("  %s\n", dev->name);
43 }
44
45 /**
46  * axi_set_cur_bus() - Set the currently active AXI bus
47  * @busnum: The number of the bus (i.e. its sequence number) that should be
48  *          made active
49  *
50  * The operations supplied by this command operate only on the currently active
51  * bus.
52  *
53  * Return: 0 if OK, -ve on error
54  */
55 static int axi_set_cur_bus(unsigned int busnum)
56 {
57         struct udevice *bus;
58         struct udevice *dummy;
59         int ret;
60
61         /* Make sure that all sequence numbers are initialized */
62         for (uclass_first_device(UCLASS_AXI, &dummy);
63              dummy;
64              uclass_next_device(&dummy))
65                 ;
66
67         ret = uclass_get_device_by_seq(UCLASS_AXI, busnum, &bus);
68         if (ret) {
69                 debug("%s: No bus %d\n", __func__, busnum);
70                 return ret;
71         }
72         axi_cur_bus = bus;
73
74         return 0;
75 }
76
77 /**
78  * axi_get_cur_bus() - Retrieve the currently active AXI bus device
79  * @busp: Pointer to a struct udevice that receives the currently active bus
80  *        device
81  *
82  * Return: 0 if OK, -ve on error
83  */
84 static int axi_get_cur_bus(struct udevice **busp)
85 {
86         if (!axi_cur_bus) {
87                 puts("No AXI bus selected\n");
88                 return -ENODEV;
89         }
90         *busp = axi_cur_bus;
91
92         return 0;
93 }
94
95 /*
96  * Command handlers
97  */
98
99 static int do_axi_show_bus(struct cmd_tbl *cmdtp, int flag, int argc,
100                            char *const argv[])
101 {
102         struct udevice *dummy;
103
104         /* Make sure that all sequence numbers are initialized */
105         for (uclass_first_device(UCLASS_AXI, &dummy);
106              dummy;
107              uclass_next_device(&dummy))
108                 ;
109
110         if (argc == 1) {
111                 /* show all busses */
112                 struct udevice *bus;
113
114                 for (uclass_first_device(UCLASS_AXI, &bus);
115                      bus;
116                      uclass_next_device(&bus))
117                         show_bus(bus);
118         } else {
119                 int i;
120
121                 /* show specific bus */
122                 i = simple_strtoul(argv[1], NULL, 10);
123
124                 struct udevice *bus;
125                 int ret;
126
127                 ret = uclass_get_device_by_seq(UCLASS_AXI, i, &bus);
128                 if (ret) {
129                         printf("Invalid bus %d: err=%d\n", i, ret);
130                         return CMD_RET_FAILURE;
131                 }
132                 show_bus(bus);
133         }
134
135         return 0;
136 }
137
138 static int do_axi_bus_num(struct cmd_tbl *cmdtp, int flag, int argc,
139                           char *const argv[])
140 {
141         int ret = 0;
142         int bus_no;
143
144         if (argc == 1) {
145                 /* querying current setting */
146                 struct udevice *bus;
147
148                 if (!axi_get_cur_bus(&bus))
149                         bus_no = bus->seq;
150                 else
151                         bus_no = -1;
152
153                 printf("Current bus is %d\n", bus_no);
154         } else {
155                 bus_no = simple_strtoul(argv[1], NULL, 10);
156                 printf("Setting bus to %d\n", bus_no);
157
158                 ret = axi_set_cur_bus(bus_no);
159                 if (ret)
160                         printf("Failure changing bus number (%d)\n", ret);
161         }
162
163         return ret ? CMD_RET_FAILURE : 0;
164 }
165
166 static int do_axi_md(struct cmd_tbl *cmdtp, int flag, int argc,
167                      char *const argv[])
168 {
169         /* Print that many bytes per line */
170         const uint DISP_LINE_LEN = 16;
171         u8 linebuf[DISP_LINE_LEN];
172         unsigned int k;
173         ulong addr, length, size;
174         ulong nbytes;
175         enum axi_size_t axisize;
176         int unitsize;
177
178         /*
179          * We use the last specified parameters, unless new ones are
180          * entered.
181          */
182         size = dp_last_size;
183         addr = dp_last_addr;
184         length = dp_last_length;
185
186         if (argc < 3)
187                 return CMD_RET_USAGE;
188
189         if (!axi_cur_bus) {
190                 puts("No AXI bus selected\n");
191                 return CMD_RET_FAILURE;
192         }
193
194         if ((flag & CMD_FLAG_REPEAT) == 0) {
195                 size = simple_strtoul(argv[1], NULL, 10);
196
197                 /*
198                  * Address is specified since argc >= 3
199                  */
200                 addr = simple_strtoul(argv[2], NULL, 16);
201
202                 /*
203                  * If there's another parameter, it is the length to display;
204                  * length is the number of objects, not number of bytes
205                  */
206                 if (argc > 3)
207                         length = simple_strtoul(argv[3], NULL, 16);
208         }
209
210         switch (size) {
211         case 8:
212                 axisize = AXI_SIZE_8;
213                 unitsize = 1;
214                 break;
215         case 16:
216                 axisize = AXI_SIZE_16;
217                 unitsize = 2;
218                 break;
219         case 32:
220                 axisize = AXI_SIZE_32;
221                 unitsize = 4;
222                 break;
223         default:
224                 printf("Unknown read size '%lu'\n", size);
225                 return CMD_RET_USAGE;
226         };
227
228         nbytes = length * unitsize;
229         do {
230                 ulong linebytes = (nbytes > DISP_LINE_LEN) ?
231                                   DISP_LINE_LEN : nbytes;
232
233                 for (k = 0; k < linebytes / unitsize; ++k) {
234                         int ret = axi_read(axi_cur_bus, addr + k * unitsize,
235                                            linebuf + k * unitsize, axisize);
236
237                         if (!ret) /* Continue if axi_read was successful */
238                                 continue;
239
240                         if (ret == -ENOSYS)
241                                 printf("axi_read failed; read size not supported?\n");
242                         else
243                                 printf("axi_read failed: err = %d\n", ret);
244
245                         return CMD_RET_FAILURE;
246                 }
247                 print_buffer(addr, (void *)linebuf, unitsize,
248                              linebytes / unitsize,
249                              DISP_LINE_LEN / unitsize);
250
251                 nbytes -= max(linebytes, 1UL);
252                 addr += linebytes;
253
254                 if (ctrlc())
255                         break;
256         } while (nbytes > 0);
257
258         dp_last_size = size;
259         dp_last_addr = addr;
260         dp_last_length = length;
261
262         return 0;
263 }
264
265 static int do_axi_mw(struct cmd_tbl *cmdtp, int flag, int argc,
266                      char *const argv[])
267 {
268         u32 writeval;
269         ulong addr, count, size;
270         enum axi_size_t axisize;
271
272         if (argc <= 3 || argc >= 6)
273                 return CMD_RET_USAGE;
274
275         size = simple_strtoul(argv[1], NULL, 10);
276
277         switch (size) {
278         case 8:
279                 axisize = AXI_SIZE_8;
280                 break;
281         case 16:
282                 axisize = AXI_SIZE_16;
283                 break;
284         case 32:
285                 axisize = AXI_SIZE_32;
286                 break;
287         default:
288                 printf("Unknown write size '%lu'\n", size);
289                 return CMD_RET_USAGE;
290         };
291
292         /* Address is specified since argc > 4 */
293         addr = simple_strtoul(argv[2], NULL, 16);
294
295         /* Get the value to write */
296         writeval = simple_strtoul(argv[3], NULL, 16);
297
298         /* Count ? */
299         if (argc == 5)
300                 count = simple_strtoul(argv[4], NULL, 16);
301         else
302                 count = 1;
303
304         while (count-- > 0) {
305                 int ret = axi_write(axi_cur_bus, addr + count * sizeof(u32),
306                                     &writeval, axisize);
307
308                 if (ret) {
309                         printf("axi_write failed: err = %d\n", ret);
310                         return CMD_RET_FAILURE;
311                 }
312         }
313
314         return 0;
315 }
316
317 static struct cmd_tbl cmd_axi_sub[] = {
318         U_BOOT_CMD_MKENT(bus, 1, 1, do_axi_show_bus, "", ""),
319         U_BOOT_CMD_MKENT(dev, 1, 1, do_axi_bus_num, "", ""),
320         U_BOOT_CMD_MKENT(md, 4, 1, do_axi_md, "", ""),
321         U_BOOT_CMD_MKENT(mw, 5, 1, do_axi_mw, "", ""),
322 };
323
324 static int do_ihs_axi(struct cmd_tbl *cmdtp, int flag, int argc,
325                       char *const argv[])
326 {
327         struct cmd_tbl *c;
328
329         if (argc < 2)
330                 return CMD_RET_USAGE;
331
332         /* Strip off leading 'axi' command argument */
333         argc--;
334         argv++;
335
336         /* Hand off rest of command line to sub-commands */
337         c = find_cmd_tbl(argv[0], &cmd_axi_sub[0], ARRAY_SIZE(cmd_axi_sub));
338
339         if (c)
340                 return c->cmd(cmdtp, flag, argc, argv);
341         else
342                 return CMD_RET_USAGE;
343 }
344
345 static char axi_help_text[] =
346         "bus  - show AXI bus info\n"
347         "axi dev [bus] - show or set current AXI bus to bus number [bus]\n"
348         "axi md size addr [# of objects] - read from AXI device at address [addr] and data width [size] (one of 8, 16, 32)\n"
349         "axi mw size addr value [count] - write data [value] to AXI device at address [addr] and data width [size] (one of 8, 16, 32)\n";
350
351 U_BOOT_CMD(axi, 7, 1, do_ihs_axi,
352            "AXI sub-system",
353            axi_help_text
354 );