axi.c 7.4 KB

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