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 9 #include <console.h> 10 #include <common.h> 11 #include <zynqmppl.h> 12 #include <linux/sizes.h> 13 #include <asm/arch/sys_proto.h> 14 #include <memalign.h> 15 16 #define DUMMY_WORD 0xffffffff 17 18 /* Xilinx binary format header */ 19 static const u32 bin_format[] = { 20 DUMMY_WORD, /* Dummy words */ 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 DUMMY_WORD, 32 DUMMY_WORD, 33 DUMMY_WORD, 34 DUMMY_WORD, 35 DUMMY_WORD, 36 0x000000bb, /* Sync word */ 37 0x11220044, /* Sync word */ 38 DUMMY_WORD, 39 DUMMY_WORD, 40 0xaa995566, /* Sync word */ 41 }; 42 43 #define SWAP_NO 1 44 #define SWAP_DONE 2 45 46 /* 47 * Load the whole word from unaligned buffer 48 * Keep in your mind that it is byte loading on little-endian system 49 */ 50 static u32 load_word(const void *buf, u32 swap) 51 { 52 u32 word = 0; 53 u8 *bitc = (u8 *)buf; 54 int p; 55 56 if (swap == SWAP_NO) { 57 for (p = 0; p < 4; p++) { 58 word <<= 8; 59 word |= bitc[p]; 60 } 61 } else { 62 for (p = 3; p >= 0; p--) { 63 word <<= 8; 64 word |= bitc[p]; 65 } 66 } 67 68 return word; 69 } 70 71 static u32 check_header(const void *buf) 72 { 73 u32 i, pattern; 74 int swap = SWAP_NO; 75 u32 *test = (u32 *)buf; 76 77 debug("%s: Let's check bitstream header\n", __func__); 78 79 /* Checking that passing bin is not a bitstream */ 80 for (i = 0; i < ARRAY_SIZE(bin_format); i++) { 81 pattern = load_word(&test[i], swap); 82 83 /* 84 * Bitstreams in binary format are swapped 85 * compare to regular bistream. 86 * Do not swap dummy word but if swap is done assume 87 * that parsing buffer is binary format 88 */ 89 if ((__swab32(pattern) != DUMMY_WORD) && 90 (__swab32(pattern) == bin_format[i])) { 91 swap = SWAP_DONE; 92 debug("%s: data swapped - let's swap\n", __func__); 93 } 94 95 debug("%s: %d/%px: pattern %x/%x bin_format\n", __func__, i, 96 &test[i], pattern, bin_format[i]); 97 } 98 debug("%s: Found bitstream header at %px %s swapinng\n", __func__, 99 buf, swap == SWAP_NO ? "without" : "with"); 100 101 return swap; 102 } 103 104 static void *check_data(u8 *buf, size_t bsize, u32 *swap) 105 { 106 u32 word, p = 0; /* possition */ 107 108 /* Because buf doesn't need to be aligned let's read it by chars */ 109 for (p = 0; p < bsize; p++) { 110 word = load_word(&buf[p], SWAP_NO); 111 debug("%s: word %x %x/%px\n", __func__, word, p, &buf[p]); 112 113 /* Find the first bitstream dummy word */ 114 if (word == DUMMY_WORD) { 115 debug("%s: Found dummy word at position %x/%px\n", 116 __func__, p, &buf[p]); 117 *swap = check_header(&buf[p]); 118 if (*swap) { 119 /* FIXME add full bitstream checking here */ 120 return &buf[p]; 121 } 122 } 123 /* Loop can be huge - support CTRL + C */ 124 if (ctrlc()) 125 return NULL; 126 } 127 return NULL; 128 } 129 130 static ulong zynqmp_align_dma_buffer(u32 *buf, u32 len, u32 swap) 131 { 132 u32 *new_buf; 133 u32 i; 134 135 if ((ulong)buf != ALIGN((ulong)buf, ARCH_DMA_MINALIGN)) { 136 new_buf = (u32 *)ALIGN((ulong)buf, ARCH_DMA_MINALIGN); 137 138 /* 139 * This might be dangerous but permits to flash if 140 * ARCH_DMA_MINALIGN is greater than header size 141 */ 142 if (new_buf > (u32 *)buf) { 143 debug("%s: Aligned buffer is after buffer start\n", 144 __func__); 145 new_buf -= ARCH_DMA_MINALIGN; 146 } 147 printf("%s: Align buffer at %px to %px(swap %d)\n", __func__, 148 buf, new_buf, swap); 149 150 for (i = 0; i < (len/4); i++) 151 new_buf[i] = load_word(&buf[i], swap); 152 153 buf = new_buf; 154 } else if (swap != SWAP_DONE) { 155 /* For bitstream which are aligned */ 156 u32 *new_buf = (u32 *)buf; 157 158 printf("%s: Bitstream is not swapped(%d) - swap it\n", __func__, 159 swap); 160 161 for (i = 0; i < (len/4); i++) 162 new_buf[i] = load_word(&buf[i], swap); 163 } 164 165 return (ulong)buf; 166 } 167 168 static int zynqmp_validate_bitstream(xilinx_desc *desc, const void *buf, 169 size_t bsize, u32 blocksize, u32 *swap) 170 { 171 ulong *buf_start; 172 ulong diff; 173 174 buf_start = check_data((u8 *)buf, blocksize, swap); 175 176 if (!buf_start) 177 return FPGA_FAIL; 178 179 /* Check if data is postpone from start */ 180 diff = (ulong)buf_start - (ulong)buf; 181 if (diff) { 182 printf("%s: Bitstream is not validated yet (diff %lx)\n", 183 __func__, diff); 184 return FPGA_FAIL; 185 } 186 187 if ((ulong)buf < SZ_1M) { 188 printf("%s: Bitstream has to be placed up to 1MB (%px)\n", 189 __func__, buf); 190 return FPGA_FAIL; 191 } 192 193 return 0; 194 } 195 196 static int zynqmp_load(xilinx_desc *desc, const void *buf, size_t bsize, 197 bitstream_type bstype) 198 { 199 ALLOC_CACHE_ALIGN_BUFFER(u32, bsizeptr, 1); 200 u32 swap; 201 ulong bin_buf; 202 int ret; 203 u32 buf_lo, buf_hi; 204 u32 ret_payload[PAYLOAD_ARG_CNT]; 205 206 if (zynqmp_validate_bitstream(desc, buf, bsize, bsize, &swap)) 207 return FPGA_FAIL; 208 209 bin_buf = zynqmp_align_dma_buffer((u32 *)buf, bsize, swap); 210 bsizeptr = (u32 *)&bsize; 211 212 debug("%s called!\n", __func__); 213 flush_dcache_range(bin_buf, bin_buf + bsize); 214 flush_dcache_range((ulong)bsizeptr, (ulong)bsizeptr + sizeof(size_t)); 215 216 buf_lo = (u32)bin_buf; 217 buf_hi = upper_32_bits(bin_buf); 218 bstype |= BIT(ZYNQMP_FPGA_BIT_NS); 219 ret = invoke_smc(ZYNQMP_SIP_SVC_PM_FPGA_LOAD, buf_lo, buf_hi, 220 (u32)(uintptr_t)bsizeptr, bstype, ret_payload); 221 if (ret) 222 debug("PL FPGA LOAD fail\n"); 223 224 return ret; 225 } 226 227 static int zynqmp_pcap_info(xilinx_desc *desc) 228 { 229 int ret; 230 u32 ret_payload[PAYLOAD_ARG_CNT]; 231 232 ret = invoke_smc(ZYNQMP_SIP_SVC_PM_FPGA_STATUS, 0, 0, 0, 233 0, ret_payload); 234 if (!ret) 235 printf("PCAP status\t0x%x\n", ret_payload[1]); 236 237 return ret; 238 } 239 240 struct xilinx_fpga_op zynqmp_op = { 241 .load = zynqmp_load, 242 .info = zynqmp_pcap_info, 243 }; 244