zynqpl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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_CTRL_PCAP_RATE_EN_MASK 0x02000000
  19. #define DEVCFG_ISR_FATAL_ERROR_MASK 0x00740040
  20. #define DEVCFG_ISR_ERROR_FLAGS_MASK 0x00340840
  21. #define DEVCFG_ISR_RX_FIFO_OV 0x00040000
  22. #define DEVCFG_ISR_DMA_DONE 0x00002000
  23. #define DEVCFG_ISR_PCFG_DONE 0x00000004
  24. #define DEVCFG_STATUS_DMA_CMD_Q_F 0x80000000
  25. #define DEVCFG_STATUS_DMA_CMD_Q_E 0x40000000
  26. #define DEVCFG_STATUS_DMA_DONE_CNT_MASK 0x30000000
  27. #define DEVCFG_STATUS_PCFG_INIT 0x00000010
  28. #define DEVCFG_MCTRL_PCAP_LPBK 0x00000010
  29. #define DEVCFG_MCTRL_RFIFO_FLUSH 0x00000002
  30. #define DEVCFG_MCTRL_WFIFO_FLUSH 0x00000001
  31. #ifndef CONFIG_SYS_FPGA_WAIT
  32. #define CONFIG_SYS_FPGA_WAIT CONFIG_SYS_HZ/100 /* 10 ms */
  33. #endif
  34. #ifndef CONFIG_SYS_FPGA_PROG_TIME
  35. #define CONFIG_SYS_FPGA_PROG_TIME (CONFIG_SYS_HZ * 4) /* 4 s */
  36. #endif
  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 NULL;
  130. }
  131. return NULL;
  132. }
  133. static int zynq_dma_transfer(u32 srcbuf, u32 srclen, u32 dstbuf, u32 dstlen)
  134. {
  135. unsigned long ts;
  136. u32 isr_status;
  137. /* Set up the transfer */
  138. writel((u32)srcbuf, &devcfg_base->dma_src_addr);
  139. writel(dstbuf, &devcfg_base->dma_dst_addr);
  140. writel(srclen, &devcfg_base->dma_src_len);
  141. writel(dstlen, &devcfg_base->dma_dst_len);
  142. isr_status = readl(&devcfg_base->int_sts);
  143. /* Polling the PCAP_INIT status for Set */
  144. ts = get_timer(0);
  145. while (!(isr_status & DEVCFG_ISR_DMA_DONE)) {
  146. if (isr_status & DEVCFG_ISR_ERROR_FLAGS_MASK) {
  147. debug("%s: Error: isr = 0x%08X\n", __func__,
  148. isr_status);
  149. debug("%s: Write count = 0x%08X\n", __func__,
  150. readl(&devcfg_base->write_count));
  151. debug("%s: Read count = 0x%08X\n", __func__,
  152. readl(&devcfg_base->read_count));
  153. return FPGA_FAIL;
  154. }
  155. if (get_timer(ts) > CONFIG_SYS_FPGA_PROG_TIME) {
  156. printf("%s: Timeout wait for DMA to complete\n",
  157. __func__);
  158. return FPGA_FAIL;
  159. }
  160. isr_status = readl(&devcfg_base->int_sts);
  161. }
  162. debug("%s: DMA transfer is done\n", __func__);
  163. /* Clear out the DMA status */
  164. writel(DEVCFG_ISR_DMA_DONE, &devcfg_base->int_sts);
  165. return FPGA_SUCCESS;
  166. }
  167. static int zynq_dma_xfer_init(bitstream_type bstype)
  168. {
  169. u32 status, control, isr_status;
  170. unsigned long ts;
  171. /* Clear loopback bit */
  172. clrbits_le32(&devcfg_base->mctrl, DEVCFG_MCTRL_PCAP_LPBK);
  173. if (bstype != BIT_PARTIAL) {
  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. /*
  179. * Delay is required if AES efuse is selected as
  180. * key source.
  181. */
  182. if (control & DEVCFG_CTRL_PCFG_AES_EFUSE_MASK)
  183. mdelay(5);
  184. /* Setting PCFG_PROG_B signal to low */
  185. writel(control & ~DEVCFG_CTRL_PCFG_PROG_B, &devcfg_base->ctrl);
  186. /*
  187. * Delay is required if AES efuse is selected as
  188. * key source.
  189. */
  190. if (control & DEVCFG_CTRL_PCFG_AES_EFUSE_MASK)
  191. mdelay(5);
  192. /* Polling the PCAP_INIT status for Reset */
  193. ts = get_timer(0);
  194. while (readl(&devcfg_base->status) & DEVCFG_STATUS_PCFG_INIT) {
  195. if (get_timer(ts) > CONFIG_SYS_FPGA_WAIT) {
  196. printf("%s: Timeout wait for INIT to clear\n",
  197. __func__);
  198. return FPGA_FAIL;
  199. }
  200. }
  201. /* Setting PCFG_PROG_B signal to high */
  202. writel(control | DEVCFG_CTRL_PCFG_PROG_B, &devcfg_base->ctrl);
  203. /* Polling the PCAP_INIT status for Set */
  204. ts = get_timer(0);
  205. while (!(readl(&devcfg_base->status) &
  206. DEVCFG_STATUS_PCFG_INIT)) {
  207. if (get_timer(ts) > CONFIG_SYS_FPGA_WAIT) {
  208. printf("%s: Timeout wait for INIT to set\n",
  209. __func__);
  210. return FPGA_FAIL;
  211. }
  212. }
  213. }
  214. isr_status = readl(&devcfg_base->int_sts);
  215. /* Clear it all, so if Boot ROM comes back, it can proceed */
  216. writel(0xFFFFFFFF, &devcfg_base->int_sts);
  217. if (isr_status & DEVCFG_ISR_FATAL_ERROR_MASK) {
  218. debug("%s: Fatal errors in PCAP 0x%X\n", __func__, isr_status);
  219. /* If RX FIFO overflow, need to flush RX FIFO first */
  220. if (isr_status & DEVCFG_ISR_RX_FIFO_OV) {
  221. writel(DEVCFG_MCTRL_RFIFO_FLUSH, &devcfg_base->mctrl);
  222. writel(0xFFFFFFFF, &devcfg_base->int_sts);
  223. }
  224. return FPGA_FAIL;
  225. }
  226. status = readl(&devcfg_base->status);
  227. debug("%s: Status = 0x%08X\n", __func__, status);
  228. if (status & DEVCFG_STATUS_DMA_CMD_Q_F) {
  229. debug("%s: Error: device busy\n", __func__);
  230. return FPGA_FAIL;
  231. }
  232. debug("%s: Device ready\n", __func__);
  233. if (!(status & DEVCFG_STATUS_DMA_CMD_Q_E)) {
  234. if (!(readl(&devcfg_base->int_sts) & DEVCFG_ISR_DMA_DONE)) {
  235. /* Error state, transfer cannot occur */
  236. debug("%s: ISR indicates error\n", __func__);
  237. return FPGA_FAIL;
  238. } else {
  239. /* Clear out the status */
  240. writel(DEVCFG_ISR_DMA_DONE, &devcfg_base->int_sts);
  241. }
  242. }
  243. if (status & DEVCFG_STATUS_DMA_DONE_CNT_MASK) {
  244. /* Clear the count of completed DMA transfers */
  245. writel(DEVCFG_STATUS_DMA_DONE_CNT_MASK, &devcfg_base->status);
  246. }
  247. return FPGA_SUCCESS;
  248. }
  249. static u32 *zynq_align_dma_buffer(u32 *buf, u32 len, u32 swap)
  250. {
  251. u32 *new_buf;
  252. u32 i;
  253. if ((u32)buf != ALIGN((u32)buf, ARCH_DMA_MINALIGN)) {
  254. new_buf = (u32 *)ALIGN((u32)buf, ARCH_DMA_MINALIGN);
  255. /*
  256. * This might be dangerous but permits to flash if
  257. * ARCH_DMA_MINALIGN is greater than header size
  258. */
  259. if (new_buf > buf) {
  260. debug("%s: Aligned buffer is after buffer start\n",
  261. __func__);
  262. new_buf -= ARCH_DMA_MINALIGN;
  263. }
  264. printf("%s: Align buffer at %x to %x(swap %d)\n", __func__,
  265. (u32)buf, (u32)new_buf, swap);
  266. for (i = 0; i < (len/4); i++)
  267. new_buf[i] = load_word(&buf[i], swap);
  268. buf = new_buf;
  269. } else if (swap != SWAP_DONE) {
  270. /* For bitstream which are aligned */
  271. u32 *new_buf = (u32 *)buf;
  272. printf("%s: Bitstream is not swapped(%d) - swap it\n", __func__,
  273. swap);
  274. for (i = 0; i < (len/4); i++)
  275. new_buf[i] = load_word(&buf[i], swap);
  276. }
  277. return buf;
  278. }
  279. static int zynq_validate_bitstream(xilinx_desc *desc, const void *buf,
  280. size_t bsize, u32 blocksize, u32 *swap,
  281. bitstream_type *bstype)
  282. {
  283. u32 *buf_start;
  284. u32 diff;
  285. buf_start = check_data((u8 *)buf, blocksize, swap);
  286. if (!buf_start)
  287. return FPGA_FAIL;
  288. /* Check if data is postpone from start */
  289. diff = (u32)buf_start - (u32)buf;
  290. if (diff) {
  291. printf("%s: Bitstream is not validated yet (diff %x)\n",
  292. __func__, diff);
  293. return FPGA_FAIL;
  294. }
  295. if ((u32)buf < SZ_1M) {
  296. printf("%s: Bitstream has to be placed up to 1MB (%x)\n",
  297. __func__, (u32)buf);
  298. return FPGA_FAIL;
  299. }
  300. if (zynq_dma_xfer_init(*bstype))
  301. return FPGA_FAIL;
  302. return 0;
  303. }
  304. static int zynq_load(xilinx_desc *desc, const void *buf, size_t bsize,
  305. bitstream_type bstype)
  306. {
  307. unsigned long ts; /* Timestamp */
  308. u32 isr_status, swap;
  309. /*
  310. * send bsize inplace of blocksize as it was not a bitstream
  311. * in chunks
  312. */
  313. if (zynq_validate_bitstream(desc, buf, bsize, bsize, &swap,
  314. &bstype))
  315. return FPGA_FAIL;
  316. buf = zynq_align_dma_buffer((u32 *)buf, bsize, swap);
  317. debug("%s: Source = 0x%08X\n", __func__, (u32)buf);
  318. debug("%s: Size = %zu\n", __func__, bsize);
  319. /* flush(clean & invalidate) d-cache range buf */
  320. flush_dcache_range((u32)buf, (u32)buf +
  321. roundup(bsize, ARCH_DMA_MINALIGN));
  322. if (zynq_dma_transfer((u32)buf | 1, bsize >> 2, 0xffffffff, 0))
  323. return FPGA_FAIL;
  324. isr_status = readl(&devcfg_base->int_sts);
  325. /* Check FPGA configuration completion */
  326. ts = get_timer(0);
  327. while (!(isr_status & DEVCFG_ISR_PCFG_DONE)) {
  328. if (get_timer(ts) > CONFIG_SYS_FPGA_WAIT) {
  329. printf("%s: Timeout wait for FPGA to config\n",
  330. __func__);
  331. return FPGA_FAIL;
  332. }
  333. isr_status = readl(&devcfg_base->int_sts);
  334. }
  335. debug("%s: FPGA config done\n", __func__);
  336. if (bstype != BIT_PARTIAL)
  337. zynq_slcr_devcfg_enable();
  338. return FPGA_SUCCESS;
  339. }
  340. #if defined(CONFIG_CMD_FPGA_LOADFS)
  341. static int zynq_loadfs(xilinx_desc *desc, const void *buf, size_t bsize,
  342. fpga_fs_info *fsinfo)
  343. {
  344. unsigned long ts; /* Timestamp */
  345. u32 isr_status, swap;
  346. u32 partialbit = 0;
  347. loff_t blocksize, actread;
  348. loff_t pos = 0;
  349. int fstype;
  350. char *interface, *dev_part, *filename;
  351. blocksize = fsinfo->blocksize;
  352. interface = fsinfo->interface;
  353. dev_part = fsinfo->dev_part;
  354. filename = fsinfo->filename;
  355. fstype = fsinfo->fstype;
  356. if (fs_set_blk_dev(interface, dev_part, fstype))
  357. return FPGA_FAIL;
  358. if (fs_read(filename, (u32) buf, pos, blocksize, &actread) < 0)
  359. return FPGA_FAIL;
  360. if (zynq_validate_bitstream(desc, buf, bsize, blocksize, &swap,
  361. &partialbit))
  362. return FPGA_FAIL;
  363. dcache_disable();
  364. do {
  365. buf = zynq_align_dma_buffer((u32 *)buf, blocksize, swap);
  366. if (zynq_dma_transfer((u32)buf | 1, blocksize >> 2,
  367. 0xffffffff, 0))
  368. return FPGA_FAIL;
  369. bsize -= blocksize;
  370. pos += blocksize;
  371. if (fs_set_blk_dev(interface, dev_part, fstype))
  372. return FPGA_FAIL;
  373. if (bsize > blocksize) {
  374. if (fs_read(filename, (u32) buf, pos, blocksize, &actread) < 0)
  375. return FPGA_FAIL;
  376. } else {
  377. if (fs_read(filename, (u32) buf, pos, bsize, &actread) < 0)
  378. return FPGA_FAIL;
  379. }
  380. } while (bsize > blocksize);
  381. buf = zynq_align_dma_buffer((u32 *)buf, blocksize, swap);
  382. if (zynq_dma_transfer((u32)buf | 1, bsize >> 2, 0xffffffff, 0))
  383. return FPGA_FAIL;
  384. dcache_enable();
  385. isr_status = readl(&devcfg_base->int_sts);
  386. /* Check FPGA configuration completion */
  387. ts = get_timer(0);
  388. while (!(isr_status & DEVCFG_ISR_PCFG_DONE)) {
  389. if (get_timer(ts) > CONFIG_SYS_FPGA_WAIT) {
  390. printf("%s: Timeout wait for FPGA to config\n",
  391. __func__);
  392. return FPGA_FAIL;
  393. }
  394. isr_status = readl(&devcfg_base->int_sts);
  395. }
  396. debug("%s: FPGA config done\n", __func__);
  397. if (!partialbit)
  398. zynq_slcr_devcfg_enable();
  399. return FPGA_SUCCESS;
  400. }
  401. #endif
  402. struct xilinx_fpga_op zynq_op = {
  403. .load = zynq_load,
  404. #if defined(CONFIG_CMD_FPGA_LOADFS)
  405. .loadfs = zynq_loadfs,
  406. #endif
  407. };
  408. #ifdef CONFIG_CMD_ZYNQ_AES
  409. /*
  410. * Load the encrypted image from src addr and decrypt the image and
  411. * place it back the decrypted image into dstaddr.
  412. */
  413. int zynq_decrypt_load(u32 srcaddr, u32 srclen, u32 dstaddr, u32 dstlen)
  414. {
  415. if (srcaddr < SZ_1M || dstaddr < SZ_1M) {
  416. printf("%s: src and dst addr should be > 1M\n",
  417. __func__);
  418. return FPGA_FAIL;
  419. }
  420. if (zynq_dma_xfer_init(BIT_NONE)) {
  421. printf("%s: zynq_dma_xfer_init FAIL\n", __func__);
  422. return FPGA_FAIL;
  423. }
  424. writel((readl(&devcfg_base->ctrl) | DEVCFG_CTRL_PCAP_RATE_EN_MASK),
  425. &devcfg_base->ctrl);
  426. debug("%s: Source = 0x%08X\n", __func__, (u32)srcaddr);
  427. debug("%s: Size = %zu\n", __func__, srclen);
  428. /* flush(clean & invalidate) d-cache range buf */
  429. flush_dcache_range((u32)srcaddr, (u32)srcaddr +
  430. roundup(srclen << 2, ARCH_DMA_MINALIGN));
  431. /*
  432. * Flush destination address range only if image is not
  433. * bitstream.
  434. */
  435. flush_dcache_range((u32)dstaddr, (u32)dstaddr +
  436. roundup(dstlen << 2, ARCH_DMA_MINALIGN));
  437. if (zynq_dma_transfer(srcaddr | 1, srclen, dstaddr | 1, dstlen))
  438. return FPGA_FAIL;
  439. writel((readl(&devcfg_base->ctrl) & ~DEVCFG_CTRL_PCAP_RATE_EN_MASK),
  440. &devcfg_base->ctrl);
  441. return FPGA_SUCCESS;
  442. }
  443. #endif