zynqpl.c 12 KB

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