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(cmd_tbl_t
*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(cmd_tbl_t
*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(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
168 /* Print that many bytes per line */
169 const uint DISP_LINE_LEN
= 16;
170 u8 linebuf
[DISP_LINE_LEN
];
172 ulong addr
, length
, size
;
174 enum axi_size_t axisize
;
178 * We use the last specified parameters, unless new ones are
183 length
= dp_last_length
;
186 return CMD_RET_USAGE
;
189 puts("No AXI bus selected\n");
190 return CMD_RET_FAILURE
;
193 if ((flag
& CMD_FLAG_REPEAT
) == 0) {
194 size
= simple_strtoul(argv
[1], NULL
, 10);
197 * Address is specified since argc >= 3
199 addr
= simple_strtoul(argv
[2], NULL
, 16);
202 * If there's another parameter, it is the length to display;
203 * length is the number of objects, not number of bytes
206 length
= simple_strtoul(argv
[3], NULL
, 16);
211 axisize
= AXI_SIZE_8
;
215 axisize
= AXI_SIZE_16
;
219 axisize
= AXI_SIZE_32
;
223 printf("Unknown read size '%lu'\n", size
);
224 return CMD_RET_USAGE
;
227 nbytes
= length
* unitsize
;
229 ulong linebytes
= (nbytes
> DISP_LINE_LEN
) ?
230 DISP_LINE_LEN
: nbytes
;
232 for (k
= 0; k
< linebytes
/ unitsize
; ++k
) {
233 int ret
= axi_read(axi_cur_bus
, addr
+ k
* unitsize
,
234 linebuf
+ k
* unitsize
, axisize
);
236 if (!ret
) /* Continue if axi_read was successful */
240 printf("axi_read failed; read size not supported?\n");
242 printf("axi_read failed: err = %d\n", ret
);
244 return CMD_RET_FAILURE
;
246 print_buffer(addr
, (void *)linebuf
, unitsize
,
247 linebytes
/ unitsize
,
248 DISP_LINE_LEN
/ unitsize
);
250 nbytes
-= max(linebytes
, 1UL);
255 } while (nbytes
> 0);
259 dp_last_length
= length
;
264 static int do_axi_mw(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
267 ulong addr
, count
, size
;
268 enum axi_size_t axisize
;
270 if (argc
<= 3 || argc
>= 6)
271 return CMD_RET_USAGE
;
273 size
= simple_strtoul(argv
[1], NULL
, 10);
277 axisize
= AXI_SIZE_8
;
280 axisize
= AXI_SIZE_16
;
283 axisize
= AXI_SIZE_32
;
286 printf("Unknown write size '%lu'\n", size
);
287 return CMD_RET_USAGE
;
290 /* Address is specified since argc > 4 */
291 addr
= simple_strtoul(argv
[2], NULL
, 16);
293 /* Get the value to write */
294 writeval
= simple_strtoul(argv
[3], NULL
, 16);
298 count
= simple_strtoul(argv
[4], NULL
, 16);
302 while (count
-- > 0) {
303 int ret
= axi_write(axi_cur_bus
, addr
+ count
* sizeof(u32
),
307 printf("axi_write failed: err = %d\n", ret
);
308 return CMD_RET_FAILURE
;
315 static cmd_tbl_t cmd_axi_sub
[] = {
316 U_BOOT_CMD_MKENT(bus
, 1, 1, do_axi_show_bus
, "", ""),
317 U_BOOT_CMD_MKENT(dev
, 1, 1, do_axi_bus_num
, "", ""),
318 U_BOOT_CMD_MKENT(md
, 4, 1, do_axi_md
, "", ""),
319 U_BOOT_CMD_MKENT(mw
, 5, 1, do_axi_mw
, "", ""),
322 static int do_ihs_axi(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
328 return CMD_RET_USAGE
;
330 /* Strip off leading 'axi' command argument */
334 /* Hand off rest of command line to sub-commands */
335 c
= find_cmd_tbl(argv
[0], &cmd_axi_sub
[0], ARRAY_SIZE(cmd_axi_sub
));
338 return c
->cmd(cmdtp
, flag
, argc
, argv
);
340 return CMD_RET_USAGE
;
343 static char axi_help_text
[] =
344 "bus - show AXI bus info\n"
345 "axi dev [bus] - show or set current AXI bus to bus number [bus]\n"
346 "axi md size addr [# of objects] - read from AXI device at address [addr] and data width [size] (one of 8, 16, 32)\n"
347 "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";
349 U_BOOT_CMD(axi
, 7, 1, do_ihs_axi
,