zynqpl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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 <fs.h>
  12. #include <zynqpl.h>
  13. #include <linux/sizes.h>
  14. #include <asm/arch/hardware.h>
  15. #include <asm/arch/sys_proto.h>
  16. #define DEVCFG_CTRL_PCFG_PROG_B 0x40000000
  17. #define DEVCFG_ISR_FATAL_ERROR_MASK 0x00740040
  18. #define DEVCFG_ISR_ERROR_FLAGS_MASK 0x00340840
  19. #define DEVCFG_ISR_RX_FIFO_OV 0x00040000
  20. #define DEVCFG_ISR_DMA_DONE 0x00002000
  21. #define DEVCFG_ISR_PCFG_DONE 0x00000004
  22. #define DEVCFG_STATUS_DMA_CMD_Q_F 0x80000000
  23. #define DEVCFG_STATUS_DMA_CMD_Q_E 0x40000000
  24. #define DEVCFG_STATUS_DMA_DONE_CNT_MASK 0x30000000
  25. #define DEVCFG_STATUS_PCFG_INIT 0x00000010
  26. #define DEVCFG_MCTRL_PCAP_LPBK 0x00000010
  27. #define DEVCFG_MCTRL_RFIFO_FLUSH 0x00000002
  28. #define DEVCFG_MCTRL_WFIFO_FLUSH 0x00000001
  29. #ifndef CONFIG_SYS_FPGA_WAIT
  30. #define CONFIG_SYS_FPGA_WAIT CONFIG_SYS_HZ/100 /* 10 ms */
  31. #endif
  32. #ifndef CONFIG_SYS_FPGA_PROG_TIME
  33. #define CONFIG_SYS_FPGA_PROG_TIME (CONFIG_SYS_HZ * 4) /* 4 s */
  34. #endif
  35. static int zynq_info(xilinx_desc *desc)
  36. {
  37. return FPGA_SUCCESS;
  38. }
  39. #define DUMMY_WORD 0xffffffff
  40. /* Xilinx binary format header */
  41. static const u32 bin_format[] = {
  42. DUMMY_WORD, /* Dummy words */
  43. DUMMY_WORD,
  44. DUMMY_WORD,
  45. DUMMY_WORD,
  46. DUMMY_WORD,
  47. DUMMY_WORD,
  48. DUMMY_WORD,
  49. DUMMY_WORD,
  50. 0x000000bb, /* Sync word */
  51. 0x11220044, /* Sync word */
  52. DUMMY_WORD,
  53. DUMMY_WORD,
  54. 0xaa995566, /* Sync word */
  55. };
  56. #define SWAP_NO 1
  57. #define SWAP_DONE 2
  58. /*
  59. * Load the whole word from unaligned buffer
  60. * Keep in your mind that it is byte loading on little-endian system
  61. */
  62. static u32 load_word(const void *buf, u32 swap)
  63. {
  64. u32 word = 0;
  65. u8 *bitc = (u8 *)buf;
  66. int p;
  67. if (swap == SWAP_NO) {
  68. for (p = 0; p < 4; p++) {
  69. word <<= 8;
  70. word |= bitc[p];
  71. }
  72. } else {
  73. for (p = 3; p >= 0; p--) {
  74. word <<= 8;
  75. word |= bitc[p];
  76. }
  77. }
  78. return word;
  79. }
  80. static u32 check_header(const void *buf)
  81. {
  82. u32 i, pattern;
  83. int swap = SWAP_NO;
  84. u32 *test = (u32 *)buf;
  85. debug("%s: Let's check bitstream header\n", __func__);
  86. /* Checking that passing bin is not a bitstream */
  87. for (i = 0; i < ARRAY_SIZE(bin_format); i++) {
  88. pattern = load_word(&test[i], swap);
  89. /*
  90. * Bitstreams in binary format are swapped
  91. * compare to regular bistream.
  92. * Do not swap dummy word but if swap is done assume
  93. * that parsing buffer is binary format
  94. */
  95. if ((__swab32(pattern) != DUMMY_WORD) &&
  96. (__swab32(pattern) == bin_format[i])) {
  97. pattern = __swab32(pattern);
  98. swap = SWAP_DONE;
  99. debug("%s: data swapped - let's swap\n", __func__);
  100. }
  101. debug("%s: %d/%x: pattern %x/%x bin_format\n", __func__, i,
  102. (u32)&test[i], pattern, bin_format[i]);
  103. if (pattern != bin_format[i]) {
  104. debug("%s: Bitstream is not recognized\n", __func__);
  105. return 0;
  106. }
  107. }
  108. debug("%s: Found bitstream header at %x %s swapinng\n", __func__,
  109. (u32)buf, swap == SWAP_NO ? "without" : "with");
  110. return swap;
  111. }
  112. static void *check_data(u8 *buf, size_t bsize, u32 *swap)
  113. {
  114. u32 word, p = 0; /* possition */
  115. /* Because buf doesn't need to be aligned let's read it by chars */
  116. for (p = 0; p < bsize; p++) {
  117. word = load_word(&buf[p], SWAP_NO);
  118. debug("%s: word %x %x/%x\n", __func__, word, p, (u32)&buf[p]);
  119. /* Find the first bitstream dummy word */
  120. if (word == DUMMY_WORD) {
  121. debug("%s: Found dummy word at position %x/%x\n",
  122. __func__, p, (u32)&buf[p]);
  123. *swap = check_header(&buf[p]);
  124. if (*swap) {
  125. /* FIXME add full bitstream checking here */
  126. return &buf[p];
  127. }
  128. }
  129. /* Loop can be huge - support CTRL + C */
  130. if (ctrlc())
  131. return NULL;
  132. }
  133. return NULL;
  134. }
  135. static int zynq_dma_transfer(u32 srcbuf, u32 srclen, u32 dstbuf, u32 dstlen)
  136. {
  137. unsigned long ts;
  138. u32 isr_status;
  139. /* Set up the transfer */
  140. writel((u32)srcbuf, &devcfg_base->dma_src_addr);
  141. writel(dstbuf, &devcfg_base->dma_dst_addr);
  142. writel(srclen, &devcfg_base->dma_src_len);
  143. writel(dstlen, &devcfg_base->dma_dst_len);
  144. isr_status = readl(&devcfg_base->int_sts);
  145. /* Polling the PCAP_INIT status for Set */
  146. ts = get_timer(0);
  147. while (!(isr_status & DEVCFG_ISR_DMA_DONE)) {
  148. if (isr_status & DEVCFG_ISR_ERROR_FLAGS_MASK) {
  149. debug("%s: Error: isr = 0x%08X\n", __func__,
  150. isr_status);
  151. debug("%s: Write count = 0x%08X\n", __func__,
  152. readl(&devcfg_base->write_count));
  153. debug("%s: Read count = 0x%08X\n", __func__,
  154. readl(&devcfg_base->read_count));
  155. return FPGA_FAIL;
  156. }
  157. if (get_timer(ts) > CONFIG_SYS_FPGA_PROG_TIME) {
  158. printf("%s: Timeout wait for DMA to complete\n",
  159. __func__);
  160. return FPGA_FAIL;
  161. }
  162. isr_status = readl(&devcfg_base->int_sts);
  163. }
  164. debug("%s: DMA transfer is done\n", __func__);
  165. /* Clear out the DMA status */
  166. writel(DEVCFG_ISR_DMA_DONE, &devcfg_base->int_sts);
  167. return FPGA_SUCCESS;
  168. }
  169. static int zynq_dma_xfer_init(bitstream_type bstype)
  170. {
  171. u32 status, control, isr_status;
  172. unsigned long ts;
  173. /* Clear loopback bit */
  174. clrbits_le32(&devcfg_base->mctrl, DEVCFG_MCTRL_PCAP_LPBK);
  175. if (bstype != BIT_PARTIAL) {
  176. zynq_slcr_devcfg_disable();
  177. /* Setting PCFG_PROG_B signal to high */
  178. control = readl(&devcfg_base->ctrl);
  179. writel(control | DEVCFG_CTRL_PCFG_PROG_B, &devcfg_base->ctrl);
  180. /* Setting PCFG_PROG_B signal to low */
  181. writel(control & ~DEVCFG_CTRL_PCFG_PROG_B, &devcfg_base->ctrl);
  182. /* Polling the PCAP_INIT status for Reset */
  183. ts = get_timer(0);
  184. while (readl(&devcfg_base->status) & DEVCFG_STATUS_PCFG_INIT) {
  185. if (get_timer(ts) > CONFIG_SYS_FPGA_WAIT) {
  186. printf("%s: Timeout wait for INIT to clear\n",
  187. __func__);
  188. return FPGA_FAIL;
  189. }
  190. }
  191. /* Setting PCFG_PROG_B signal to high */
  192. writel(control | DEVCFG_CTRL_PCFG_PROG_B, &devcfg_base->ctrl);
  193. /* Polling the PCAP_INIT status for Set */
  194. ts = get_timer(0);
  195. while (!(readl(&devcfg_base->status) &
  196. DEVCFG_STATUS_PCFG_INIT)) {
  197. if (get_timer(ts) > CONFIG_SYS_FPGA_WAIT) {
  198. printf("%s: Timeout wait for INIT to set\n",
  199. __func__);
  200. return FPGA_FAIL;
  201. }
  202. }
  203. }
  204. isr_status = readl(&devcfg_base->int_sts);
  205. /* Clear it all, so if Boot ROM comes back, it can proceed */
  206. writel(0xFFFFFFFF, &devcfg_base->int_sts);
  207. if (isr_status & DEVCFG_ISR_FATAL_ERROR_MASK) {
  208. debug("%s: Fatal errors in PCAP 0x%X\n", __func__, isr_status);
  209. /* If RX FIFO overflow, need to flush RX FIFO first */
  210. if (isr_status & DEVCFG_ISR_RX_FIFO_OV) {
  211. writel(DEVCFG_MCTRL_RFIFO_FLUSH, &devcfg_base->mctrl);
  212. writel(0xFFFFFFFF, &devcfg_base->int_sts);
  213. }
  214. return FPGA_FAIL;
  215. }
  216. status = readl(&devcfg_base->status);
  217. debug("%s: Status = 0x%08X\n", __func__, status);
  218. if (status & DEVCFG_STATUS_DMA_CMD_Q_F) {
  219. debug("%s: Error: device busy\n", __func__);
  220. return FPGA_FAIL;
  221. }
  222. debug("%s: Device ready\n", __func__);
  223. if (!(status & DEVCFG_STATUS_DMA_CMD_Q_E)) {
  224. if (!(readl(&devcfg_base->int_sts) & DEVCFG_ISR_DMA_DONE)) {
  225. /* Error state, transfer cannot occur */
  226. debug("%s: ISR indicates error\n", __func__);
  227. return FPGA_FAIL;
  228. } else {
  229. /* Clear out the status */
  230. writel(DEVCFG_ISR_DMA_DONE, &devcfg_base->int_sts);
  231. }
  232. }
  233. if (status & DEVCFG_STATUS_DMA_DONE_CNT_MASK) {
  234. /* Clear the count of completed DMA transfers */
  235. writel(DEVCFG_STATUS_DMA_DONE_CNT_MASK, &devcfg_base->status);
  236. }
  237. return FPGA_SUCCESS;
  238. }
  239. static u32 *zynq_align_dma_buffer(u32 *buf, u32 len, u32 swap)
  240. {
  241. u32 *new_buf;
  242. u32 i;
  243. if ((u32)buf != ALIGN((u32)buf, ARCH_DMA_MINALIGN)) {
  244. new_buf = (u32 *)ALIGN((u32)buf, ARCH_DMA_MINALIGN);
  245. /*
  246. * This might be dangerous but permits to flash if
  247. * ARCH_DMA_MINALIGN is greater than header size
  248. */
  249. if (new_buf > buf) {
  250. debug("%s: Aligned buffer is after buffer start\n",
  251. __func__);
  252. new_buf -= ARCH_DMA_MINALIGN;
  253. }
  254. printf("%s: Align buffer at %x to %x(swap %d)\n", __func__,
  255. (u32)buf, (u32)new_buf, swap);
  256. for (i = 0; i < (len/4); i++)
  257. new_buf[i] = load_word(&buf[i], swap);
  258. buf = new_buf;
  259. } else if (swap != SWAP_DONE) {
  260. /* For bitstream which are aligned */
  261. u32 *new_buf = (u32 *)buf;
  262. printf("%s: Bitstream is not swapped(%d) - swap it\n", __func__,
  263. swap);
  264. for (i = 0; i < (len/4); i++)
  265. new_buf[i] = load_word(&buf[i], swap);
  266. }
  267. return buf;
  268. }
  269. static int zynq_validate_bitstream(xilinx_desc *desc, const void *buf,
  270. size_t bsize, u32 blocksize, u32 *swap,
  271. bitstream_type *bstype)
  272. {
  273. u32 *buf_start;
  274. u32 diff;
  275. buf_start = check_data((u8 *)buf, blocksize, swap);
  276. if (!buf_start)
  277. return FPGA_FAIL;
  278. /* Check if data is postpone from start */
  279. diff = (u32)buf_start - (u32)buf;
  280. if (diff) {
  281. printf("%s: Bitstream is not validated yet (diff %x)\n",
  282. __func__, diff);
  283. return FPGA_FAIL;
  284. }
  285. if ((u32)buf < SZ_1M) {
  286. printf("%s: Bitstream has to be placed up to 1MB (%x)\n",
  287. __func__, (u32)buf);
  288. return FPGA_FAIL;
  289. }
  290. if (zynq_dma_xfer_init(*bstype))
  291. return FPGA_FAIL;
  292. return 0;
  293. }
  294. static int zynq_load(xilinx_desc *desc, const void *buf, size_t bsize,
  295. bitstream_type bstype)
  296. {
  297. unsigned long ts; /* Timestamp */
  298. u32 isr_status, swap;
  299. /*
  300. * send bsize inplace of blocksize as it was not a bitstream
  301. * in chunks
  302. */
  303. if (zynq_validate_bitstream(desc, buf, bsize, bsize, &swap,
  304. &bstype))
  305. return FPGA_FAIL;
  306. buf = zynq_align_dma_buffer((u32 *)buf, bsize, swap);
  307. debug("%s: Source = 0x%08X\n", __func__, (u32)buf);
  308. debug("%s: Size = %zu\n", __func__, bsize);
  309. /* flush(clean & invalidate) d-cache range buf */
  310. flush_dcache_range((u32)buf, (u32)buf +
  311. roundup(bsize, ARCH_DMA_MINALIGN));
  312. if (zynq_dma_transfer((u32)buf | 1, bsize >> 2, 0xffffffff, 0))
  313. return FPGA_FAIL;
  314. isr_status = readl(&devcfg_base->int_sts);
  315. /* Check FPGA configuration completion */
  316. ts = get_timer(0);
  317. while (!(isr_status & DEVCFG_ISR_PCFG_DONE)) {
  318. if (get_timer(ts) > CONFIG_SYS_FPGA_WAIT) {
  319. printf("%s: Timeout wait for FPGA to config\n",
  320. __func__);
  321. return FPGA_FAIL;
  322. }
  323. isr_status = readl(&devcfg_base->int_sts);
  324. }
  325. debug("%s: FPGA config done\n", __func__);
  326. if (bstype != BIT_PARTIAL)
  327. zynq_slcr_devcfg_enable();
  328. return FPGA_SUCCESS;
  329. }
  330. #if defined(CONFIG_CMD_FPGA_LOADFS)
  331. static int zynq_loadfs(xilinx_desc *desc, const void *buf, size_t bsize,
  332. fpga_fs_info *fsinfo)
  333. {
  334. unsigned long ts; /* Timestamp */
  335. u32 isr_status, swap;
  336. u32 partialbit = 0;
  337. loff_t blocksize, actread;
  338. loff_t pos = 0;
  339. int fstype;
  340. char *interface, *dev_part, *filename;
  341. blocksize = fsinfo->blocksize;
  342. interface = fsinfo->interface;
  343. dev_part = fsinfo->dev_part;
  344. filename = fsinfo->filename;
  345. fstype = fsinfo->fstype;
  346. if (fs_set_blk_dev(interface, dev_part, fstype))
  347. return FPGA_FAIL;
  348. if (fs_read(filename, (u32) buf, pos, blocksize, &actread) < 0)
  349. return FPGA_FAIL;
  350. if (zynq_validate_bitstream(desc, buf, bsize, blocksize, &swap,
  351. &partialbit))
  352. return FPGA_FAIL;
  353. dcache_disable();
  354. do {
  355. buf = zynq_align_dma_buffer((u32 *)buf, blocksize, swap);
  356. if (zynq_dma_transfer((u32)buf | 1, blocksize >> 2,
  357. 0xffffffff, 0))
  358. return FPGA_FAIL;
  359. bsize -= blocksize;
  360. pos += blocksize;
  361. if (fs_set_blk_dev(interface, dev_part, fstype))
  362. return FPGA_FAIL;
  363. if (bsize > blocksize) {
  364. if (fs_read(filename, (u32) buf, pos, blocksize, &actread) < 0)
  365. return FPGA_FAIL;
  366. } else {
  367. if (fs_read(filename, (u32) buf, pos, bsize, &actread) < 0)
  368. return FPGA_FAIL;
  369. }
  370. } while (bsize > blocksize);
  371. buf = zynq_align_dma_buffer((u32 *)buf, blocksize, swap);
  372. if (zynq_dma_transfer((u32)buf | 1, bsize >> 2, 0xffffffff, 0))
  373. return FPGA_FAIL;
  374. dcache_enable();
  375. isr_status = readl(&devcfg_base->int_sts);
  376. /* Check FPGA configuration completion */
  377. ts = get_timer(0);
  378. while (!(isr_status & DEVCFG_ISR_PCFG_DONE)) {
  379. if (get_timer(ts) > CONFIG_SYS_FPGA_WAIT) {
  380. printf("%s: Timeout wait for FPGA to config\n",
  381. __func__);
  382. return FPGA_FAIL;
  383. }
  384. isr_status = readl(&devcfg_base->int_sts);
  385. }
  386. debug("%s: FPGA config done\n", __func__);
  387. if (!partialbit)
  388. zynq_slcr_devcfg_enable();
  389. return FPGA_SUCCESS;
  390. }
  391. #endif
  392. static int zynq_dump(xilinx_desc *desc, const void *buf, size_t bsize)
  393. {
  394. return FPGA_FAIL;
  395. }
  396. struct xilinx_fpga_op zynq_op = {
  397. .load = zynq_load,
  398. #if defined(CONFIG_CMD_FPGA_LOADFS)
  399. .loadfs = zynq_loadfs,
  400. #endif
  401. .dump = zynq_dump,
  402. .info = zynq_info,
  403. };