1 // SPDX-License-Identifier: GPL-2.0+
4 * Dirk Eibach, Guntermann & Drunck GmbH, dirk.eibach@gdsys.cc
6 * (C) Copyright 2017, 2018
7 * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc
9 * SPDX-License-Identifier: GPL-2.0+
16 #include <display_options.h>
20 /* Currently selected AXI bus device */
21 static struct udevice *axi_cur_bus;
22 /* Transmission size from last command */
23 static uint dp_last_size;
24 /* Address from last command */
25 static uint dp_last_addr;
26 /* Number of bytes to display from last command; default = 64 */
27 static uint dp_last_length = 0x40;
30 * show_bus() - Show devices on a single AXI bus
31 * @bus: The AXI bus device to printt information for
33 static void show_bus(struct udevice *bus)
37 printf("Bus %d:\t%s", dev_seq(bus), bus->name);
38 if (device_active(bus))
41 for (device_find_first_child(bus, &dev);
43 device_find_next_child(&dev))
44 printf(" %s\n", dev->name);
48 * axi_set_cur_bus() - Set the currently active AXI bus
49 * @busnum: The number of the bus (i.e. its sequence number) that should be
52 * The operations supplied by this command operate only on the currently active
55 * Return: 0 if OK, -ve on error
57 static int axi_set_cur_bus(unsigned int busnum)
60 struct udevice *dummy;
63 /* Make sure that all sequence numbers are initialized */
64 for (uclass_first_device(UCLASS_AXI, &dummy);
66 uclass_next_device(&dummy))
69 ret = uclass_get_device_by_seq(UCLASS_AXI, busnum, &bus);
71 debug("%s: No bus %d\n", __func__, busnum);
80 * axi_get_cur_bus() - Retrieve the currently active AXI bus device
81 * @busp: Pointer to a struct udevice that receives the currently active bus
84 * Return: 0 if OK, -ve on error
86 static int axi_get_cur_bus(struct udevice **busp)
89 puts("No AXI bus selected\n");
101 static int do_axi_show_bus(struct cmd_tbl *cmdtp, int flag, int argc,
104 struct udevice *dummy;
106 /* Make sure that all sequence numbers are initialized */
107 for (uclass_first_device(UCLASS_AXI, &dummy);
109 uclass_next_device(&dummy))
113 /* show all busses */
116 for (uclass_first_device(UCLASS_AXI, &bus);
118 uclass_next_device(&bus))
123 /* show specific bus */
124 i = dectoul(argv[1], NULL);
129 ret = uclass_get_device_by_seq(UCLASS_AXI, i, &bus);
131 printf("Invalid bus %d: err=%d\n", i, ret);
132 return CMD_RET_FAILURE;
140 static int do_axi_bus_num(struct cmd_tbl *cmdtp, int flag, int argc,
147 /* querying current setting */
150 if (!axi_get_cur_bus(&bus))
151 bus_no = dev_seq(bus);
155 printf("Current bus is %d\n", bus_no);
157 bus_no = dectoul(argv[1], NULL);
158 printf("Setting bus to %d\n", bus_no);
160 ret = axi_set_cur_bus(bus_no);
162 printf("Failure changing bus number (%d)\n", ret);
165 return ret ? CMD_RET_FAILURE : 0;
168 static int do_axi_md(struct cmd_tbl *cmdtp, int flag, int argc,
171 /* Print that many bytes per line */
172 const uint DISP_LINE_LEN = 16;
173 u8 linebuf[DISP_LINE_LEN];
175 ulong addr, length, size;
177 enum axi_size_t axisize;
181 * We use the last specified parameters, unless new ones are
186 length = dp_last_length;
189 return CMD_RET_USAGE;
192 puts("No AXI bus selected\n");
193 return CMD_RET_FAILURE;
196 if ((flag & CMD_FLAG_REPEAT) == 0) {
197 size = dectoul(argv[1], NULL);
200 * Address is specified since argc >= 3
202 addr = hextoul(argv[2], NULL);
205 * If there's another parameter, it is the length to display;
206 * length is the number of objects, not number of bytes
209 length = hextoul(argv[3], NULL);
214 axisize = AXI_SIZE_8;
218 axisize = AXI_SIZE_16;
222 axisize = AXI_SIZE_32;
226 printf("Unknown read size '%lu'\n", size);
227 return CMD_RET_USAGE;
230 nbytes = length * unitsize;
232 ulong linebytes = (nbytes > DISP_LINE_LEN) ?
233 DISP_LINE_LEN : nbytes;
235 for (k = 0; k < linebytes / unitsize; ++k) {
236 int ret = axi_read(axi_cur_bus, addr + k * unitsize,
237 linebuf + k * unitsize, axisize);
239 if (!ret) /* Continue if axi_read was successful */
243 printf("axi_read failed; read size not supported?\n");
245 printf("axi_read failed: err = %d\n", ret);
247 return CMD_RET_FAILURE;
249 print_buffer(addr, (void *)linebuf, unitsize,
250 linebytes / unitsize,
251 DISP_LINE_LEN / unitsize);
253 nbytes -= max(linebytes, 1UL);
258 } while (nbytes > 0);
262 dp_last_length = length;
267 static int do_axi_mw(struct cmd_tbl *cmdtp, int flag, int argc,
271 ulong addr, count, size;
272 enum axi_size_t axisize;
274 if (argc <= 3 || argc >= 6)
275 return CMD_RET_USAGE;
277 size = dectoul(argv[1], NULL);
281 axisize = AXI_SIZE_8;
284 axisize = AXI_SIZE_16;
287 axisize = AXI_SIZE_32;
290 printf("Unknown write size '%lu'\n", size);
291 return CMD_RET_USAGE;
294 /* Address is specified since argc > 4 */
295 addr = hextoul(argv[2], NULL);
297 /* Get the value to write */
298 writeval = hextoul(argv[3], NULL);
302 count = hextoul(argv[4], NULL);
306 while (count-- > 0) {
307 int ret = axi_write(axi_cur_bus, addr + count * sizeof(u32),
311 printf("axi_write failed: err = %d\n", ret);
312 return CMD_RET_FAILURE;
319 static struct cmd_tbl cmd_axi_sub[] = {
320 U_BOOT_CMD_MKENT(bus, 1, 1, do_axi_show_bus, "", ""),
321 U_BOOT_CMD_MKENT(dev, 1, 1, do_axi_bus_num, "", ""),
322 U_BOOT_CMD_MKENT(md, 4, 1, do_axi_md, "", ""),
323 U_BOOT_CMD_MKENT(mw, 5, 1, do_axi_mw, "", ""),
326 static int do_ihs_axi(struct cmd_tbl *cmdtp, int flag, int argc,
332 return CMD_RET_USAGE;
334 /* Strip off leading 'axi' command argument */
338 /* Hand off rest of command line to sub-commands */
339 c = find_cmd_tbl(argv[0], &cmd_axi_sub[0], ARRAY_SIZE(cmd_axi_sub));
342 return c->cmd(cmdtp, flag, argc, argv);
344 return CMD_RET_USAGE;
347 static char axi_help_text[] =
348 "bus - show AXI bus info\n"
349 "axi dev [bus] - show or set current AXI bus to bus number [bus]\n"
350 "axi md size addr [# of objects] - read from AXI device at address [addr] and data width [size] (one of 8, 16, 32)\n"
351 "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";
353 U_BOOT_CMD(axi, 7, 1, do_ihs_axi,