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+
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;
28 * show_bus() - Show devices on a single AXI bus
29 * @bus: The AXI bus device to printt information for
31 static void show_bus(struct udevice *bus)
35 printf("Bus %d:\t%s", bus->req_seq, bus->name);
36 if (device_active(bus))
37 printf(" (active %d)", bus->seq);
39 for (device_find_first_child(bus, &dev);
41 device_find_next_child(&dev))
42 printf(" %s\n", dev->name);
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
50 * The operations supplied by this command operate only on the currently active
53 * Return: 0 if OK, -ve on error
55 static int axi_set_cur_bus(unsigned int busnum)
58 struct udevice *dummy;
61 /* Make sure that all sequence numbers are initialized */
62 for (uclass_first_device(UCLASS_AXI, &dummy);
64 uclass_next_device(&dummy))
67 ret = uclass_get_device_by_seq(UCLASS_AXI, busnum, &bus);
69 debug("%s: No bus %d\n", __func__, busnum);
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
82 * Return: 0 if OK, -ve on error
84 static int axi_get_cur_bus(struct udevice **busp)
87 puts("No AXI bus selected\n");
99 static int do_axi_show_bus(struct cmd_tbl *cmdtp, int flag, int argc,
102 struct udevice *dummy;
104 /* Make sure that all sequence numbers are initialized */
105 for (uclass_first_device(UCLASS_AXI, &dummy);
107 uclass_next_device(&dummy))
111 /* show all busses */
114 for (uclass_first_device(UCLASS_AXI, &bus);
116 uclass_next_device(&bus))
121 /* show specific bus */
122 i = simple_strtoul(argv[1], NULL, 10);
127 ret = uclass_get_device_by_seq(UCLASS_AXI, i, &bus);
129 printf("Invalid bus %d: err=%d\n", i, ret);
130 return CMD_RET_FAILURE;
138 static int do_axi_bus_num(struct cmd_tbl *cmdtp, int flag, int argc,
145 /* querying current setting */
148 if (!axi_get_cur_bus(&bus))
153 printf("Current bus is %d\n", bus_no);
155 bus_no = simple_strtoul(argv[1], NULL, 10);
156 printf("Setting bus to %d\n", bus_no);
158 ret = axi_set_cur_bus(bus_no);
160 printf("Failure changing bus number (%d)\n", ret);
163 return ret ? CMD_RET_FAILURE : 0;
166 static int do_axi_md(struct cmd_tbl *cmdtp, int flag, int argc,
169 /* Print that many bytes per line */
170 const uint DISP_LINE_LEN = 16;
171 u8 linebuf[DISP_LINE_LEN];
173 ulong addr, length, size;
175 enum axi_size_t axisize;
179 * We use the last specified parameters, unless new ones are
184 length = dp_last_length;
187 return CMD_RET_USAGE;
190 puts("No AXI bus selected\n");
191 return CMD_RET_FAILURE;
194 if ((flag & CMD_FLAG_REPEAT) == 0) {
195 size = simple_strtoul(argv[1], NULL, 10);
198 * Address is specified since argc >= 3
200 addr = simple_strtoul(argv[2], NULL, 16);
203 * If there's another parameter, it is the length to display;
204 * length is the number of objects, not number of bytes
207 length = simple_strtoul(argv[3], NULL, 16);
212 axisize = AXI_SIZE_8;
216 axisize = AXI_SIZE_16;
220 axisize = AXI_SIZE_32;
224 printf("Unknown read size '%lu'\n", size);
225 return CMD_RET_USAGE;
228 nbytes = length * unitsize;
230 ulong linebytes = (nbytes > DISP_LINE_LEN) ?
231 DISP_LINE_LEN : nbytes;
233 for (k = 0; k < linebytes / unitsize; ++k) {
234 int ret = axi_read(axi_cur_bus, addr + k * unitsize,
235 linebuf + k * unitsize, axisize);
237 if (!ret) /* Continue if axi_read was successful */
241 printf("axi_read failed; read size not supported?\n");
243 printf("axi_read failed: err = %d\n", ret);
245 return CMD_RET_FAILURE;
247 print_buffer(addr, (void *)linebuf, unitsize,
248 linebytes / unitsize,
249 DISP_LINE_LEN / unitsize);
251 nbytes -= max(linebytes, 1UL);
256 } while (nbytes > 0);
260 dp_last_length = length;
265 static int do_axi_mw(struct cmd_tbl *cmdtp, int flag, int argc,
269 ulong addr, count, size;
270 enum axi_size_t axisize;
272 if (argc <= 3 || argc >= 6)
273 return CMD_RET_USAGE;
275 size = simple_strtoul(argv[1], NULL, 10);
279 axisize = AXI_SIZE_8;
282 axisize = AXI_SIZE_16;
285 axisize = AXI_SIZE_32;
288 printf("Unknown write size '%lu'\n", size);
289 return CMD_RET_USAGE;
292 /* Address is specified since argc > 4 */
293 addr = simple_strtoul(argv[2], NULL, 16);
295 /* Get the value to write */
296 writeval = simple_strtoul(argv[3], NULL, 16);
300 count = simple_strtoul(argv[4], NULL, 16);
304 while (count-- > 0) {
305 int ret = axi_write(axi_cur_bus, addr + count * sizeof(u32),
309 printf("axi_write failed: err = %d\n", ret);
310 return CMD_RET_FAILURE;
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, "", ""),
324 static int do_ihs_axi(struct cmd_tbl *cmdtp, int flag, int argc,
330 return CMD_RET_USAGE;
332 /* Strip off leading 'axi' command argument */
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));
340 return c->cmd(cmdtp, flag, argc, argv);
342 return CMD_RET_USAGE;
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";
351 U_BOOT_CMD(axi, 7, 1, do_ihs_axi,