zynqmppl.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * (C) Copyright 2015 - 2016, Xilinx, Inc,
  3. * Michal Simek <michal.simek@xilinx.com>
  4. * Siva Durga Prasad <siva.durga.paladugu@xilinx.com>
  5. *
  6. * SPDX-License-Identifier: GPL-2.0
  7. */
  8. #include <console.h>
  9. #include <common.h>
  10. #include <zynqmppl.h>
  11. #include <linux/sizes.h>
  12. #define DUMMY_WORD 0xffffffff
  13. /* Xilinx binary format header */
  14. static const u32 bin_format[] = {
  15. DUMMY_WORD, /* Dummy words */
  16. DUMMY_WORD,
  17. DUMMY_WORD,
  18. DUMMY_WORD,
  19. DUMMY_WORD,
  20. DUMMY_WORD,
  21. DUMMY_WORD,
  22. DUMMY_WORD,
  23. DUMMY_WORD,
  24. DUMMY_WORD,
  25. DUMMY_WORD,
  26. DUMMY_WORD,
  27. DUMMY_WORD,
  28. DUMMY_WORD,
  29. DUMMY_WORD,
  30. DUMMY_WORD,
  31. 0x000000bb, /* Sync word */
  32. 0x11220044, /* Sync word */
  33. DUMMY_WORD,
  34. DUMMY_WORD,
  35. 0xaa995566, /* Sync word */
  36. };
  37. #define SWAP_NO 1
  38. #define SWAP_DONE 2
  39. /*
  40. * Load the whole word from unaligned buffer
  41. * Keep in your mind that it is byte loading on little-endian system
  42. */
  43. static u32 load_word(const void *buf, u32 swap)
  44. {
  45. u32 word = 0;
  46. u8 *bitc = (u8 *)buf;
  47. int p;
  48. if (swap == SWAP_NO) {
  49. for (p = 0; p < 4; p++) {
  50. word <<= 8;
  51. word |= bitc[p];
  52. }
  53. } else {
  54. for (p = 3; p >= 0; p--) {
  55. word <<= 8;
  56. word |= bitc[p];
  57. }
  58. }
  59. return word;
  60. }
  61. static u32 check_header(const void *buf)
  62. {
  63. u32 i, pattern;
  64. int swap = SWAP_NO;
  65. u32 *test = (u32 *)buf;
  66. debug("%s: Let's check bitstream header\n", __func__);
  67. /* Checking that passing bin is not a bitstream */
  68. for (i = 0; i < ARRAY_SIZE(bin_format); i++) {
  69. pattern = load_word(&test[i], swap);
  70. /*
  71. * Bitstreams in binary format are swapped
  72. * compare to regular bistream.
  73. * Do not swap dummy word but if swap is done assume
  74. * that parsing buffer is binary format
  75. */
  76. if ((__swab32(pattern) != DUMMY_WORD) &&
  77. (__swab32(pattern) == bin_format[i])) {
  78. swap = SWAP_DONE;
  79. debug("%s: data swapped - let's swap\n", __func__);
  80. }
  81. debug("%s: %d/%px: pattern %x/%x bin_format\n", __func__, i,
  82. &test[i], pattern, bin_format[i]);
  83. }
  84. debug("%s: Found bitstream header at %px %s swapinng\n", __func__,
  85. buf, swap == SWAP_NO ? "without" : "with");
  86. return swap;
  87. }
  88. static void *check_data(u8 *buf, size_t bsize, u32 *swap)
  89. {
  90. u32 word, p = 0; /* possition */
  91. /* Because buf doesn't need to be aligned let's read it by chars */
  92. for (p = 0; p < bsize; p++) {
  93. word = load_word(&buf[p], SWAP_NO);
  94. debug("%s: word %x %x/%px\n", __func__, word, p, &buf[p]);
  95. /* Find the first bitstream dummy word */
  96. if (word == DUMMY_WORD) {
  97. debug("%s: Found dummy word at position %x/%px\n",
  98. __func__, p, &buf[p]);
  99. *swap = check_header(&buf[p]);
  100. if (*swap) {
  101. /* FIXME add full bitstream checking here */
  102. return &buf[p];
  103. }
  104. }
  105. /* Loop can be huge - support CTRL + C */
  106. if (ctrlc())
  107. return NULL;
  108. }
  109. return NULL;
  110. }
  111. static ulong zynqmp_align_dma_buffer(u32 *buf, u32 len, u32 swap)
  112. {
  113. u32 *new_buf;
  114. u32 i;
  115. if ((ulong)buf != ALIGN((ulong)buf, ARCH_DMA_MINALIGN)) {
  116. new_buf = (u32 *)ALIGN((ulong)buf, ARCH_DMA_MINALIGN);
  117. /*
  118. * This might be dangerous but permits to flash if
  119. * ARCH_DMA_MINALIGN is greater than header size
  120. */
  121. if (new_buf > (u32 *)buf) {
  122. debug("%s: Aligned buffer is after buffer start\n",
  123. __func__);
  124. new_buf -= ARCH_DMA_MINALIGN;
  125. }
  126. printf("%s: Align buffer at %px to %px(swap %d)\n", __func__,
  127. buf, new_buf, swap);
  128. for (i = 0; i < (len/4); i++)
  129. new_buf[i] = load_word(&buf[i], swap);
  130. buf = new_buf;
  131. } else if (swap != SWAP_DONE) {
  132. /* For bitstream which are aligned */
  133. u32 *new_buf = (u32 *)buf;
  134. printf("%s: Bitstream is not swapped(%d) - swap it\n", __func__,
  135. swap);
  136. for (i = 0; i < (len/4); i++)
  137. new_buf[i] = load_word(&buf[i], swap);
  138. }
  139. return (ulong)buf;
  140. }
  141. static int zynqmp_validate_bitstream(xilinx_desc *desc, const void *buf,
  142. size_t bsize, u32 blocksize, u32 *swap)
  143. {
  144. ulong *buf_start;
  145. ulong diff;
  146. buf_start = check_data((u8 *)buf, blocksize, swap);
  147. if (!buf_start)
  148. return FPGA_FAIL;
  149. /* Check if data is postpone from start */
  150. diff = (ulong)buf_start - (ulong)buf;
  151. if (diff) {
  152. printf("%s: Bitstream is not validated yet (diff %lx)\n",
  153. __func__, diff);
  154. return FPGA_FAIL;
  155. }
  156. if ((ulong)buf < SZ_1M) {
  157. printf("%s: Bitstream has to be placed up to 1MB (%px)\n",
  158. __func__, buf);
  159. return FPGA_FAIL;
  160. }
  161. return 0;
  162. }
  163. static int invoke_smc(ulong id, ulong reg0, ulong reg1, ulong reg2)
  164. {
  165. struct pt_regs regs;
  166. regs.regs[0] = id;
  167. regs.regs[1] = reg0;
  168. regs.regs[2] = reg1;
  169. regs.regs[3] = reg2;
  170. smc_call(&regs);
  171. return regs.regs[0];
  172. }
  173. static int zynqmp_load(xilinx_desc *desc, const void *buf, size_t bsize,
  174. bitstream_type bstype)
  175. {
  176. u32 swap;
  177. ulong bin_buf, flags;
  178. int ret;
  179. if (zynqmp_validate_bitstream(desc, buf, bsize, bsize, &swap))
  180. return FPGA_FAIL;
  181. bin_buf = zynqmp_align_dma_buffer((u32 *)buf, bsize, swap);
  182. debug("%s called!\n", __func__);
  183. flush_dcache_range(bin_buf, bin_buf + bsize);
  184. if (bsize % 4)
  185. bsize = bsize / 4 + 1;
  186. else
  187. bsize = bsize / 4;
  188. flags = (u32)bsize | ((u64)bstype << 32);
  189. ret = invoke_smc(ZYNQMP_SIP_SVC_PM_FPGA_LOAD, bin_buf, flags, 0);
  190. if (ret)
  191. debug("PL FPGA LOAD fail\n");
  192. return ret;
  193. }
  194. struct xilinx_fpga_op zynqmp_op = {
  195. .load = zynqmp_load,
  196. };