zynqpl.c 9.3 KB

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