zynqpl.c 12 KB

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