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+
19 /* Currently selected AXI bus device */
20 static struct udevice *axi_cur_bus;
21 /* Transmission size from last command */
22 static uint dp_last_size;
23 /* Address from last command */
24 static uint dp_last_addr;
25 /* Number of bytes to display from last command; default = 64 */
26 static uint dp_last_length = 0x40;
29 * show_bus() - Show devices on a single AXI bus
30 * @bus: The AXI bus device to printt information for
32 static void show_bus(struct udevice *bus)
36 printf("Bus %d:\t%s", dev_seq(bus), bus->name);
37 if (device_active(bus))
40 for (device_find_first_child(bus, &dev);
42 device_find_next_child(&dev))
43 printf(" %s\n", dev->name);
47 * axi_set_cur_bus() - Set the currently active AXI bus
48 * @busnum: The number of the bus (i.e. its sequence number) that should be
51 * The operations supplied by this command operate only on the currently active
54 * Return: 0 if OK, -ve on error
56 static int axi_set_cur_bus(unsigned int busnum)
59 struct udevice *dummy;
62 /* Make sure that all sequence numbers are initialized */
63 for (uclass_first_device(UCLASS_AXI, &dummy);
65 uclass_next_device(&dummy))
68 ret = uclass_get_device_by_seq(UCLASS_AXI, busnum, &bus);
70 debug("%s: No bus %d\n", __func__, busnum);
79 * axi_get_cur_bus() - Retrieve the currently active AXI bus device
80 * @busp: Pointer to a struct udevice that receives the currently active bus
83 * Return: 0 if OK, -ve on error
85 static int axi_get_cur_bus(struct udevice **busp)
88 puts("No AXI bus selected\n");
100 static int do_axi_show_bus(struct cmd_tbl *cmdtp, int flag, int argc,
103 struct udevice *dummy;
105 /* Make sure that all sequence numbers are initialized */
106 for (uclass_first_device(UCLASS_AXI, &dummy);
108 uclass_next_device(&dummy))
112 /* show all busses */
115 for (uclass_first_device(UCLASS_AXI, &bus);
117 uclass_next_device(&bus))
122 /* show specific bus */
123 i = dectoul(argv[1], NULL);
128 ret = uclass_get_device_by_seq(UCLASS_AXI, i, &bus);
130 printf("Invalid bus %d: err=%d\n", i, ret);
131 return CMD_RET_FAILURE;
139 static int do_axi_bus_num(struct cmd_tbl *cmdtp, int flag, int argc,
146 /* querying current setting */
149 if (!axi_get_cur_bus(&bus))
150 bus_no = dev_seq(bus);
154 printf("Current bus is %d\n", bus_no);
156 bus_no = dectoul(argv[1], NULL);
157 printf("Setting bus to %d\n", bus_no);
159 ret = axi_set_cur_bus(bus_no);
161 printf("Failure changing bus number (%d)\n", ret);
164 return ret ? CMD_RET_FAILURE : 0;
167 static int do_axi_md(struct cmd_tbl *cmdtp, int flag, int argc,
170 /* Print that many bytes per line */
171 const uint DISP_LINE_LEN = 16;
172 u8 linebuf[DISP_LINE_LEN];
174 ulong addr, length, size;
176 enum axi_size_t axisize;
180 * We use the last specified parameters, unless new ones are
185 length = dp_last_length;
188 return CMD_RET_USAGE;
191 puts("No AXI bus selected\n");
192 return CMD_RET_FAILURE;
195 if ((flag & CMD_FLAG_REPEAT) == 0) {
196 size = dectoul(argv[1], NULL);
199 * Address is specified since argc >= 3
201 addr = hextoul(argv[2], NULL);
204 * If there's another parameter, it is the length to display;
205 * length is the number of objects, not number of bytes
208 length = hextoul(argv[3], NULL);
213 axisize = AXI_SIZE_8;
217 axisize = AXI_SIZE_16;
221 axisize = AXI_SIZE_32;
225 printf("Unknown read size '%lu'\n", size);
226 return CMD_RET_USAGE;
229 nbytes = length * unitsize;
231 ulong linebytes = (nbytes > DISP_LINE_LEN) ?
232 DISP_LINE_LEN : nbytes;
234 for (k = 0; k < linebytes / unitsize; ++k) {
235 int ret = axi_read(axi_cur_bus, addr + k * unitsize,
236 linebuf + k * unitsize, axisize);
238 if (!ret) /* Continue if axi_read was successful */
242 printf("axi_read failed; read size not supported?\n");
244 printf("axi_read failed: err = %d\n", ret);
246 return CMD_RET_FAILURE;
248 print_buffer(addr, (void *)linebuf, unitsize,
249 linebytes / unitsize,
250 DISP_LINE_LEN / unitsize);
252 nbytes -= max(linebytes, 1UL);
257 } while (nbytes > 0);
261 dp_last_length = length;
266 static int do_axi_mw(struct cmd_tbl *cmdtp, int flag, int argc,
270 ulong addr, count, size;
271 enum axi_size_t axisize;
273 if (argc <= 3 || argc >= 6)
274 return CMD_RET_USAGE;
276 size = dectoul(argv[1], NULL);
280 axisize = AXI_SIZE_8;
283 axisize = AXI_SIZE_16;
286 axisize = AXI_SIZE_32;
289 printf("Unknown write size '%lu'\n", size);
290 return CMD_RET_USAGE;
293 /* Address is specified since argc > 4 */
294 addr = hextoul(argv[2], NULL);
296 /* Get the value to write */
297 writeval = hextoul(argv[3], NULL);
301 count = hextoul(argv[4], NULL);
305 while (count-- > 0) {
306 int ret = axi_write(axi_cur_bus, addr + count * sizeof(u32),
310 printf("axi_write failed: err = %d\n", ret);
311 return CMD_RET_FAILURE;
318 static struct cmd_tbl cmd_axi_sub[] = {
319 U_BOOT_CMD_MKENT(bus, 1, 1, do_axi_show_bus, "", ""),
320 U_BOOT_CMD_MKENT(dev, 1, 1, do_axi_bus_num, "", ""),
321 U_BOOT_CMD_MKENT(md, 4, 1, do_axi_md, "", ""),
322 U_BOOT_CMD_MKENT(mw, 5, 1, do_axi_mw, "", ""),
325 static int do_ihs_axi(struct cmd_tbl *cmdtp, int flag, int argc,
331 return CMD_RET_USAGE;
333 /* Strip off leading 'axi' command argument */
337 /* Hand off rest of command line to sub-commands */
338 c = find_cmd_tbl(argv[0], &cmd_axi_sub[0], ARRAY_SIZE(cmd_axi_sub));
341 return c->cmd(cmdtp, flag, argc, argv);
343 return CMD_RET_USAGE;
346 static char axi_help_text[] =
347 "bus - show AXI bus info\n"
348 "axi dev [bus] - show or set current AXI bus to bus number [bus]\n"
349 "axi md size addr [# of objects] - read from AXI device at address [addr] and data width [size] (one of 8, 16, 32)\n"
350 "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";
352 U_BOOT_CMD(axi, 7, 1, do_ihs_axi,