zynqpl.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * (C) Copyright 2012-2013, Xilinx, Michal Simek
  3. *
  4. * (C) Copyright 2012
  5. * Joe Hershberger <joe.hershberger@ni.com>
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <common.h>
  10. #include <asm/io.h>
  11. #include <zynqpl.h>
  12. #include <asm/arch/hardware.h>
  13. #include <asm/arch/sys_proto.h>
  14. #define DEVCFG_CTRL_PCFG_PROG_B 0x40000000
  15. #define DEVCFG_ISR_FATAL_ERROR_MASK 0x00740040
  16. #define DEVCFG_ISR_ERROR_FLAGS_MASK 0x00340840
  17. #define DEVCFG_ISR_RX_FIFO_OV 0x00040000
  18. #define DEVCFG_ISR_DMA_DONE 0x00002000
  19. #define DEVCFG_ISR_PCFG_DONE 0x00000004
  20. #define DEVCFG_STATUS_DMA_CMD_Q_F 0x80000000
  21. #define DEVCFG_STATUS_DMA_CMD_Q_E 0x40000000
  22. #define DEVCFG_STATUS_DMA_DONE_CNT_MASK 0x30000000
  23. #define DEVCFG_STATUS_PCFG_INIT 0x00000010
  24. #define DEVCFG_MCTRL_PCAP_LPBK 0x00000010
  25. #define DEVCFG_MCTRL_RFIFO_FLUSH 0x00000002
  26. #define DEVCFG_MCTRL_WFIFO_FLUSH 0x00000001
  27. #ifndef CONFIG_SYS_FPGA_WAIT
  28. #define CONFIG_SYS_FPGA_WAIT CONFIG_SYS_HZ/100 /* 10 ms */
  29. #endif
  30. #ifndef CONFIG_SYS_FPGA_PROG_TIME
  31. #define CONFIG_SYS_FPGA_PROG_TIME (CONFIG_SYS_HZ * 4) /* 4 s */
  32. #endif
  33. int zynq_info(Xilinx_desc *desc)
  34. {
  35. return FPGA_SUCCESS;
  36. }
  37. #define DUMMY_WORD 0xffffffff
  38. /* Xilinx binary format header */
  39. static const u32 bin_format[] = {
  40. DUMMY_WORD, /* Dummy words */
  41. DUMMY_WORD,
  42. DUMMY_WORD,
  43. DUMMY_WORD,
  44. DUMMY_WORD,
  45. DUMMY_WORD,
  46. DUMMY_WORD,
  47. DUMMY_WORD,
  48. 0x000000bb, /* Sync word */
  49. 0x11220044, /* Sync word */
  50. DUMMY_WORD,
  51. DUMMY_WORD,
  52. 0xaa995566, /* Sync word */
  53. };
  54. #define SWAP_NO 1
  55. #define SWAP_DONE 2
  56. /*
  57. * Load the whole word from unaligned buffer
  58. * Keep in your mind that it is byte loading on little-endian system
  59. */
  60. static u32 load_word(const void *buf, u32 swap)
  61. {
  62. u32 word = 0;
  63. u8 *bitc = (u8 *)buf;
  64. int p;
  65. if (swap == SWAP_NO) {
  66. for (p = 0; p < 4; p++) {
  67. word <<= 8;
  68. word |= bitc[p];
  69. }
  70. } else {
  71. for (p = 3; p >= 0; p--) {
  72. word <<= 8;
  73. word |= bitc[p];
  74. }
  75. }
  76. return word;
  77. }
  78. static u32 check_header(const void *buf)
  79. {
  80. u32 i, pattern;
  81. int swap = SWAP_NO;
  82. u32 *test = (u32 *)buf;
  83. debug("%s: Let's check bitstream header\n", __func__);
  84. /* Checking that passing bin is not a bitstream */
  85. for (i = 0; i < ARRAY_SIZE(bin_format); i++) {
  86. pattern = load_word(&test[i], swap);
  87. /*
  88. * Bitstreams in binary format are swapped
  89. * compare to regular bistream.
  90. * Do not swap dummy word but if swap is done assume
  91. * that parsing buffer is binary format
  92. */
  93. if ((__swab32(pattern) != DUMMY_WORD) &&
  94. (__swab32(pattern) == bin_format[i])) {
  95. pattern = __swab32(pattern);
  96. swap = SWAP_DONE;
  97. debug("%s: data swapped - let's swap\n", __func__);
  98. }
  99. debug("%s: %d/%x: pattern %x/%x bin_format\n", __func__, i,
  100. (u32)&test[i], pattern, bin_format[i]);
  101. if (pattern != bin_format[i]) {
  102. debug("%s: Bitstream is not recognized\n", __func__);
  103. return 0;
  104. }
  105. }
  106. debug("%s: Found bitstream header at %x %s swapinng\n", __func__,
  107. (u32)buf, swap == SWAP_NO ? "without" : "with");
  108. return swap;
  109. }
  110. static void *check_data(u8 *buf, size_t bsize, u32 *swap)
  111. {
  112. u32 word, p = 0; /* possition */
  113. /* Because buf doesn't need to be aligned let's read it by chars */
  114. for (p = 0; p < bsize; p++) {
  115. word = load_word(&buf[p], SWAP_NO);
  116. debug("%s: word %x %x/%x\n", __func__, word, p, (u32)&buf[p]);
  117. /* Find the first bitstream dummy word */
  118. if (word == DUMMY_WORD) {
  119. debug("%s: Found dummy word at position %x/%x\n",
  120. __func__, p, (u32)&buf[p]);
  121. *swap = check_header(&buf[p]);
  122. if (*swap) {
  123. /* FIXME add full bitstream checking here */
  124. return &buf[p];
  125. }
  126. }
  127. /* Loop can be huge - support CTRL + C */
  128. if (ctrlc())
  129. return 0;
  130. }
  131. return 0;
  132. }
  133. int zynq_load(Xilinx_desc *desc, const void *buf, size_t bsize)
  134. {
  135. unsigned long ts; /* Timestamp */
  136. u32 partialbit = 0;
  137. u32 i, control, isr_status, status, swap, diff;
  138. u32 *buf_start;
  139. /* Detect if we are going working with partial or full bitstream */
  140. if (bsize != desc->size) {
  141. printf("%s: Working with partial bitstream\n", __func__);
  142. partialbit = 1;
  143. }
  144. buf_start = check_data((u8 *)buf, bsize, &swap);
  145. if (!buf_start)
  146. return FPGA_FAIL;
  147. /* Check if data is postpone from start */
  148. diff = (u32)buf_start - (u32)buf;
  149. if (diff) {
  150. printf("%s: Bitstream is not validated yet (diff %x)\n",
  151. __func__, diff);
  152. return FPGA_FAIL;
  153. }
  154. if ((u32)buf_start & 0x3) {
  155. u32 *new_buf = (u32 *)((u32)buf & ~0x3);
  156. printf("%s: Align buffer at %x to %x(swap %d)\n", __func__,
  157. (u32)buf_start, (u32)new_buf, swap);
  158. for (i = 0; i < (bsize/4); i++)
  159. new_buf[i] = load_word(&buf_start[i], swap);
  160. swap = SWAP_DONE;
  161. buf = new_buf;
  162. } else if (swap != SWAP_DONE) {
  163. /* For bitstream which are aligned */
  164. u32 *new_buf = (u32 *)buf;
  165. printf("%s: Bitstream is not swapped(%d) - swap it\n", __func__,
  166. swap);
  167. for (i = 0; i < (bsize/4); i++)
  168. new_buf[i] = load_word(&buf_start[i], swap);
  169. swap = SWAP_DONE;
  170. }
  171. /* Clear loopback bit */
  172. clrbits_le32(&devcfg_base->mctrl, DEVCFG_MCTRL_PCAP_LPBK);
  173. if (!partialbit) {
  174. zynq_slcr_devcfg_disable();
  175. /* Setting PCFG_PROG_B signal to high */
  176. control = readl(&devcfg_base->ctrl);
  177. writel(control | DEVCFG_CTRL_PCFG_PROG_B, &devcfg_base->ctrl);
  178. /* Setting PCFG_PROG_B signal to low */
  179. writel(control & ~DEVCFG_CTRL_PCFG_PROG_B, &devcfg_base->ctrl);
  180. /* Polling the PCAP_INIT status for Reset */
  181. ts = get_timer(0);
  182. while (readl(&devcfg_base->status) & DEVCFG_STATUS_PCFG_INIT) {
  183. if (get_timer(ts) > CONFIG_SYS_FPGA_WAIT) {
  184. printf("%s: Timeout wait for INIT to clear\n",
  185. __func__);
  186. return FPGA_FAIL;
  187. }
  188. }
  189. /* Setting PCFG_PROG_B signal to high */
  190. writel(control | DEVCFG_CTRL_PCFG_PROG_B, &devcfg_base->ctrl);
  191. /* Polling the PCAP_INIT status for Set */
  192. ts = get_timer(0);
  193. while (!(readl(&devcfg_base->status) &
  194. DEVCFG_STATUS_PCFG_INIT)) {
  195. if (get_timer(ts) > CONFIG_SYS_FPGA_WAIT) {
  196. printf("%s: Timeout wait for INIT to set\n",
  197. __func__);
  198. return FPGA_FAIL;
  199. }
  200. }
  201. }
  202. isr_status = readl(&devcfg_base->int_sts);
  203. /* Clear it all, so if Boot ROM comes back, it can proceed */
  204. writel(0xFFFFFFFF, &devcfg_base->int_sts);
  205. if (isr_status & DEVCFG_ISR_FATAL_ERROR_MASK) {
  206. debug("%s: Fatal errors in PCAP 0x%X\n", __func__, isr_status);
  207. /* If RX FIFO overflow, need to flush RX FIFO first */
  208. if (isr_status & DEVCFG_ISR_RX_FIFO_OV) {
  209. writel(DEVCFG_MCTRL_RFIFO_FLUSH, &devcfg_base->mctrl);
  210. writel(0xFFFFFFFF, &devcfg_base->int_sts);
  211. }
  212. return FPGA_FAIL;
  213. }
  214. status = readl(&devcfg_base->status);
  215. debug("%s: Status = 0x%08X\n", __func__, status);
  216. if (status & DEVCFG_STATUS_DMA_CMD_Q_F) {
  217. debug("%s: Error: device busy\n", __func__);
  218. return FPGA_FAIL;
  219. }
  220. debug("%s: Device ready\n", __func__);
  221. if (!(status & DEVCFG_STATUS_DMA_CMD_Q_E)) {
  222. if (!(readl(&devcfg_base->int_sts) & DEVCFG_ISR_DMA_DONE)) {
  223. /* Error state, transfer cannot occur */
  224. debug("%s: ISR indicates error\n", __func__);
  225. return FPGA_FAIL;
  226. } else {
  227. /* Clear out the status */
  228. writel(DEVCFG_ISR_DMA_DONE, &devcfg_base->int_sts);
  229. }
  230. }
  231. if (status & DEVCFG_STATUS_DMA_DONE_CNT_MASK) {
  232. /* Clear the count of completed DMA transfers */
  233. writel(DEVCFG_STATUS_DMA_DONE_CNT_MASK, &devcfg_base->status);
  234. }
  235. debug("%s: Source = 0x%08X\n", __func__, (u32)buf);
  236. debug("%s: Size = %zu\n", __func__, bsize);
  237. /* Set up the transfer */
  238. writel((u32)buf | 1, &devcfg_base->dma_src_addr);
  239. writel(0xFFFFFFFF, &devcfg_base->dma_dst_addr);
  240. writel(bsize >> 2, &devcfg_base->dma_src_len);
  241. writel(0, &devcfg_base->dma_dst_len);
  242. isr_status = readl(&devcfg_base->int_sts);
  243. /* Polling the PCAP_INIT status for Set */
  244. ts = get_timer(0);
  245. while (!(isr_status & DEVCFG_ISR_DMA_DONE)) {
  246. if (isr_status & DEVCFG_ISR_ERROR_FLAGS_MASK) {
  247. debug("%s: Error: isr = 0x%08X\n", __func__,
  248. isr_status);
  249. debug("%s: Write count = 0x%08X\n", __func__,
  250. readl(&devcfg_base->write_count));
  251. debug("%s: Read count = 0x%08X\n", __func__,
  252. readl(&devcfg_base->read_count));
  253. return FPGA_FAIL;
  254. }
  255. if (get_timer(ts) > CONFIG_SYS_FPGA_PROG_TIME) {
  256. printf("%s: Timeout wait for DMA to complete\n",
  257. __func__);
  258. return FPGA_FAIL;
  259. }
  260. isr_status = readl(&devcfg_base->int_sts);
  261. }
  262. debug("%s: DMA transfer is done\n", __func__);
  263. /* Check FPGA configuration completion */
  264. ts = get_timer(0);
  265. while (!(isr_status & DEVCFG_ISR_PCFG_DONE)) {
  266. if (get_timer(ts) > CONFIG_SYS_FPGA_WAIT) {
  267. printf("%s: Timeout wait for FPGA to config\n",
  268. __func__);
  269. return FPGA_FAIL;
  270. }
  271. isr_status = readl(&devcfg_base->int_sts);
  272. }
  273. debug("%s: FPGA config done\n", __func__);
  274. /* Clear out the DMA status */
  275. writel(DEVCFG_ISR_DMA_DONE, &devcfg_base->int_sts);
  276. if (!partialbit)
  277. zynq_slcr_devcfg_enable();
  278. return FPGA_SUCCESS;
  279. }
  280. int zynq_dump(Xilinx_desc *desc, const void *buf, size_t bsize)
  281. {
  282. return FPGA_FAIL;
  283. }