axi.c 7.4 KB

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