axi.c 7.4 KB

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