Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | 5d3207d | 2002-08-21 22:08:56 +0000 | [diff] [blame] | 2 | /* |
Michal Simek | d5dae85 | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 3 | * (C) Copyright 2012-2013, Xilinx, Michal Simek |
| 4 | * |
wdenk | 5d3207d | 2002-08-21 22:08:56 +0000 | [diff] [blame] | 5 | * (C) Copyright 2002 |
| 6 | * Rich Ireland, Enterasys Networks, rireland@enterasys.com. |
| 7 | * Keith Outwater, keith_outwater@mvis.com |
wdenk | 5d3207d | 2002-08-21 22:08:56 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * Xilinx FPGA support |
| 12 | */ |
| 13 | |
| 14 | #include <common.h> |
Michal Simek | 6631db4 | 2013-04-26 15:04:48 +0200 | [diff] [blame] | 15 | #include <fpga.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 16 | #include <log.h> |
wdenk | 5d3207d | 2002-08-21 22:08:56 +0000 | [diff] [blame] | 17 | #include <virtex2.h> |
| 18 | #include <spartan2.h> |
Wolfgang Denk | 875c789 | 2005-09-25 16:44:21 +0200 | [diff] [blame] | 19 | #include <spartan3.h> |
Michal Simek | d5dae85 | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 20 | #include <zynqpl.h> |
wdenk | 5d3207d | 2002-08-21 22:08:56 +0000 | [diff] [blame] | 21 | |
wdenk | 5d3207d | 2002-08-21 22:08:56 +0000 | [diff] [blame] | 22 | /* Local Static Functions */ |
Michal Simek | f8c1be9 | 2014-03-13 12:49:21 +0100 | [diff] [blame] | 23 | static int xilinx_validate(xilinx_desc *desc, char *fn); |
wdenk | 5d3207d | 2002-08-21 22:08:56 +0000 | [diff] [blame] | 24 | |
| 25 | /* ------------------------------------------------------------------------- */ |
| 26 | |
Goldschmidt Simon | 8b93a92 | 2017-11-10 14:17:41 +0000 | [diff] [blame] | 27 | int fpga_is_partial_data(int devnum, size_t img_len) |
| 28 | { |
| 29 | const fpga_desc * const desc = fpga_get_desc(devnum); |
| 30 | xilinx_desc *desc_xilinx = desc->devdesc; |
| 31 | |
| 32 | /* Check datasize against FPGA size */ |
| 33 | if (img_len >= desc_xilinx->size) |
| 34 | return 0; |
| 35 | |
| 36 | /* datasize is smaller, must be partial data */ |
| 37 | return 1; |
| 38 | } |
| 39 | |
Michal Simek | 7a78bd2 | 2014-05-02 14:09:30 +0200 | [diff] [blame] | 40 | int fpga_loadbitstream(int devnum, char *fpgadata, size_t size, |
| 41 | bitstream_type bstype) |
Michal Simek | 52c2064 | 2013-04-26 13:12:07 +0200 | [diff] [blame] | 42 | { |
| 43 | unsigned int length; |
| 44 | unsigned int swapsize; |
Michal Simek | 52c2064 | 2013-04-26 13:12:07 +0200 | [diff] [blame] | 45 | unsigned char *dataptr; |
| 46 | unsigned int i; |
Michal Simek | 6631db4 | 2013-04-26 15:04:48 +0200 | [diff] [blame] | 47 | const fpga_desc *desc; |
Michal Simek | f8c1be9 | 2014-03-13 12:49:21 +0100 | [diff] [blame] | 48 | xilinx_desc *xdesc; |
Michal Simek | 52c2064 | 2013-04-26 13:12:07 +0200 | [diff] [blame] | 49 | |
| 50 | dataptr = (unsigned char *)fpgadata; |
Michal Simek | 6631db4 | 2013-04-26 15:04:48 +0200 | [diff] [blame] | 51 | /* Find out fpga_description */ |
| 52 | desc = fpga_validate(devnum, dataptr, 0, (char *)__func__); |
| 53 | /* Assign xilinx device description */ |
| 54 | xdesc = desc->devdesc; |
Michal Simek | 52c2064 | 2013-04-26 13:12:07 +0200 | [diff] [blame] | 55 | |
| 56 | /* skip the first bytes of the bitsteam, their meaning is unknown */ |
| 57 | length = (*dataptr << 8) + *(dataptr + 1); |
| 58 | dataptr += 2; |
| 59 | dataptr += length; |
| 60 | |
| 61 | /* get design name (identifier, length, string) */ |
| 62 | length = (*dataptr << 8) + *(dataptr + 1); |
| 63 | dataptr += 2; |
| 64 | if (*dataptr++ != 0x61) { |
| 65 | debug("%s: Design name id not recognized in bitstream\n", |
| 66 | __func__); |
| 67 | return FPGA_FAIL; |
| 68 | } |
| 69 | |
| 70 | length = (*dataptr << 8) + *(dataptr + 1); |
| 71 | dataptr += 2; |
Siva Durga Prasad Paladugu | d863909 | 2017-03-02 18:50:11 +0530 | [diff] [blame] | 72 | printf(" design filename = \"%s\"\n", dataptr); |
| 73 | dataptr += length; |
Michal Simek | 52c2064 | 2013-04-26 13:12:07 +0200 | [diff] [blame] | 74 | |
| 75 | /* get part number (identifier, length, string) */ |
| 76 | if (*dataptr++ != 0x62) { |
| 77 | printf("%s: Part number id not recognized in bitstream\n", |
| 78 | __func__); |
| 79 | return FPGA_FAIL; |
| 80 | } |
| 81 | |
| 82 | length = (*dataptr << 8) + *(dataptr + 1); |
| 83 | dataptr += 2; |
Michal Simek | 6631db4 | 2013-04-26 15:04:48 +0200 | [diff] [blame] | 84 | |
| 85 | if (xdesc->name) { |
Siva Durga Prasad Paladugu | d863909 | 2017-03-02 18:50:11 +0530 | [diff] [blame] | 86 | i = (ulong)strstr((char *)dataptr, xdesc->name); |
Siva Durga Prasad Paladugu | f721326 | 2016-01-11 12:30:41 +0530 | [diff] [blame] | 87 | if (!i) { |
Michal Simek | 6631db4 | 2013-04-26 15:04:48 +0200 | [diff] [blame] | 88 | printf("%s: Wrong bitstream ID for this device\n", |
| 89 | __func__); |
| 90 | printf("%s: Bitstream ID %s, current device ID %d/%s\n", |
Siva Durga Prasad Paladugu | d863909 | 2017-03-02 18:50:11 +0530 | [diff] [blame] | 91 | __func__, dataptr, devnum, xdesc->name); |
Michal Simek | 6631db4 | 2013-04-26 15:04:48 +0200 | [diff] [blame] | 92 | return FPGA_FAIL; |
| 93 | } |
| 94 | } else { |
Michal Simek | f8c1be9 | 2014-03-13 12:49:21 +0100 | [diff] [blame] | 95 | printf("%s: Please fill correct device ID to xilinx_desc\n", |
Michal Simek | 6631db4 | 2013-04-26 15:04:48 +0200 | [diff] [blame] | 96 | __func__); |
| 97 | } |
Siva Durga Prasad Paladugu | d863909 | 2017-03-02 18:50:11 +0530 | [diff] [blame] | 98 | printf(" part number = \"%s\"\n", dataptr); |
| 99 | dataptr += length; |
Michal Simek | 52c2064 | 2013-04-26 13:12:07 +0200 | [diff] [blame] | 100 | |
| 101 | /* get date (identifier, length, string) */ |
| 102 | if (*dataptr++ != 0x63) { |
| 103 | printf("%s: Date identifier not recognized in bitstream\n", |
| 104 | __func__); |
| 105 | return FPGA_FAIL; |
| 106 | } |
| 107 | |
| 108 | length = (*dataptr << 8) + *(dataptr+1); |
| 109 | dataptr += 2; |
Siva Durga Prasad Paladugu | d863909 | 2017-03-02 18:50:11 +0530 | [diff] [blame] | 110 | printf(" date = \"%s\"\n", dataptr); |
| 111 | dataptr += length; |
Michal Simek | 52c2064 | 2013-04-26 13:12:07 +0200 | [diff] [blame] | 112 | |
| 113 | /* get time (identifier, length, string) */ |
| 114 | if (*dataptr++ != 0x64) { |
| 115 | printf("%s: Time identifier not recognized in bitstream\n", |
| 116 | __func__); |
| 117 | return FPGA_FAIL; |
| 118 | } |
| 119 | |
| 120 | length = (*dataptr << 8) + *(dataptr+1); |
| 121 | dataptr += 2; |
Siva Durga Prasad Paladugu | d863909 | 2017-03-02 18:50:11 +0530 | [diff] [blame] | 122 | printf(" time = \"%s\"\n", dataptr); |
| 123 | dataptr += length; |
Michal Simek | 52c2064 | 2013-04-26 13:12:07 +0200 | [diff] [blame] | 124 | |
| 125 | /* get fpga data length (identifier, length) */ |
| 126 | if (*dataptr++ != 0x65) { |
| 127 | printf("%s: Data length id not recognized in bitstream\n", |
| 128 | __func__); |
| 129 | return FPGA_FAIL; |
| 130 | } |
| 131 | swapsize = ((unsigned int) *dataptr << 24) + |
| 132 | ((unsigned int) *(dataptr + 1) << 16) + |
| 133 | ((unsigned int) *(dataptr + 2) << 8) + |
| 134 | ((unsigned int) *(dataptr + 3)); |
| 135 | dataptr += 4; |
| 136 | printf(" bytes in bitstream = %d\n", swapsize); |
| 137 | |
Oleksandr Suvorov | 282eed5 | 2022-07-22 17:16:07 +0300 | [diff] [blame] | 138 | return fpga_load(devnum, dataptr, swapsize, bstype, 0); |
Michal Simek | 52c2064 | 2013-04-26 13:12:07 +0200 | [diff] [blame] | 139 | } |
| 140 | |
Michal Simek | 7a78bd2 | 2014-05-02 14:09:30 +0200 | [diff] [blame] | 141 | int xilinx_load(xilinx_desc *desc, const void *buf, size_t bsize, |
Oleksandr Suvorov | 7a9a0df | 2022-07-22 17:16:06 +0300 | [diff] [blame] | 142 | bitstream_type bstype, int flags) |
wdenk | 5d3207d | 2002-08-21 22:08:56 +0000 | [diff] [blame] | 143 | { |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 144 | if (!xilinx_validate (desc, (char *)__FUNCTION__)) { |
wdenk | 5d3207d | 2002-08-21 22:08:56 +0000 | [diff] [blame] | 145 | printf ("%s: Invalid device descriptor\n", __FUNCTION__); |
Michal Simek | 14cfc4f | 2014-03-13 13:07:57 +0100 | [diff] [blame] | 146 | return FPGA_FAIL; |
| 147 | } |
wdenk | 5d3207d | 2002-08-21 22:08:56 +0000 | [diff] [blame] | 148 | |
Michal Simek | 6cd68c8 | 2014-07-16 10:31:21 +0200 | [diff] [blame] | 149 | if (!desc->operations || !desc->operations->load) { |
| 150 | printf("%s: Missing load operation\n", __func__); |
| 151 | return FPGA_FAIL; |
| 152 | } |
| 153 | |
Oleksandr Suvorov | 3e78481 | 2022-07-22 17:16:10 +0300 | [diff] [blame] | 154 | return desc->operations->load(desc, buf, bsize, bstype, flags); |
wdenk | 5d3207d | 2002-08-21 22:08:56 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Siva Durga Prasad Paladugu | 1a89766 | 2014-03-14 16:35:37 +0530 | [diff] [blame] | 157 | #if defined(CONFIG_CMD_FPGA_LOADFS) |
| 158 | int xilinx_loadfs(xilinx_desc *desc, const void *buf, size_t bsize, |
| 159 | fpga_fs_info *fpga_fsinfo) |
| 160 | { |
| 161 | if (!xilinx_validate(desc, (char *)__func__)) { |
| 162 | printf("%s: Invalid device descriptor\n", __func__); |
| 163 | return FPGA_FAIL; |
| 164 | } |
| 165 | |
Michal Simek | 6cd68c8 | 2014-07-16 10:31:21 +0200 | [diff] [blame] | 166 | if (!desc->operations || !desc->operations->loadfs) { |
| 167 | printf("%s: Missing loadfs operation\n", __func__); |
Siva Durga Prasad Paladugu | 1a89766 | 2014-03-14 16:35:37 +0530 | [diff] [blame] | 168 | return FPGA_FAIL; |
Michal Simek | 6cd68c8 | 2014-07-16 10:31:21 +0200 | [diff] [blame] | 169 | } |
Siva Durga Prasad Paladugu | 1a89766 | 2014-03-14 16:35:37 +0530 | [diff] [blame] | 170 | |
| 171 | return desc->operations->loadfs(desc, buf, bsize, fpga_fsinfo); |
| 172 | } |
| 173 | #endif |
| 174 | |
Oleksandr Suvorov | fb2b885 | 2022-07-22 17:16:02 +0300 | [diff] [blame] | 175 | #if CONFIG_IS_ENABLED(FPGA_LOAD_SECURE) |
Siva Durga Prasad Paladugu | a18d09e | 2018-05-31 15:10:23 +0530 | [diff] [blame] | 176 | int xilinx_loads(xilinx_desc *desc, const void *buf, size_t bsize, |
| 177 | struct fpga_secure_info *fpga_sec_info) |
| 178 | { |
| 179 | if (!xilinx_validate(desc, (char *)__func__)) { |
| 180 | printf("%s: Invalid device descriptor\n", __func__); |
| 181 | return FPGA_FAIL; |
| 182 | } |
| 183 | |
| 184 | if (!desc->operations || !desc->operations->loads) { |
| 185 | printf("%s: Missing loads operation\n", __func__); |
| 186 | return FPGA_FAIL; |
| 187 | } |
| 188 | |
| 189 | return desc->operations->loads(desc, buf, bsize, fpga_sec_info); |
| 190 | } |
| 191 | #endif |
| 192 | |
Michal Simek | f8c1be9 | 2014-03-13 12:49:21 +0100 | [diff] [blame] | 193 | int xilinx_dump(xilinx_desc *desc, const void *buf, size_t bsize) |
wdenk | 5d3207d | 2002-08-21 22:08:56 +0000 | [diff] [blame] | 194 | { |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 195 | if (!xilinx_validate (desc, (char *)__FUNCTION__)) { |
wdenk | 5d3207d | 2002-08-21 22:08:56 +0000 | [diff] [blame] | 196 | printf ("%s: Invalid device descriptor\n", __FUNCTION__); |
Michal Simek | 14cfc4f | 2014-03-13 13:07:57 +0100 | [diff] [blame] | 197 | return FPGA_FAIL; |
| 198 | } |
wdenk | 5d3207d | 2002-08-21 22:08:56 +0000 | [diff] [blame] | 199 | |
Michal Simek | 6cd68c8 | 2014-07-16 10:31:21 +0200 | [diff] [blame] | 200 | if (!desc->operations || !desc->operations->dump) { |
| 201 | printf("%s: Missing dump operation\n", __func__); |
| 202 | return FPGA_FAIL; |
| 203 | } |
| 204 | |
Michal Simek | 14cfc4f | 2014-03-13 13:07:57 +0100 | [diff] [blame] | 205 | return desc->operations->dump(desc, buf, bsize); |
wdenk | 5d3207d | 2002-08-21 22:08:56 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Michal Simek | f8c1be9 | 2014-03-13 12:49:21 +0100 | [diff] [blame] | 208 | int xilinx_info(xilinx_desc *desc) |
wdenk | 5d3207d | 2002-08-21 22:08:56 +0000 | [diff] [blame] | 209 | { |
| 210 | int ret_val = FPGA_FAIL; |
| 211 | |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 212 | if (xilinx_validate (desc, (char *)__FUNCTION__)) { |
wdenk | 5d3207d | 2002-08-21 22:08:56 +0000 | [diff] [blame] | 213 | printf ("Family: \t"); |
| 214 | switch (desc->family) { |
Michal Simek | b625b9a | 2014-03-13 11:23:43 +0100 | [diff] [blame] | 215 | case xilinx_spartan2: |
wdenk | 5d3207d | 2002-08-21 22:08:56 +0000 | [diff] [blame] | 216 | printf ("Spartan-II\n"); |
| 217 | break; |
Michal Simek | 2a6e386 | 2014-03-13 11:28:42 +0100 | [diff] [blame] | 218 | case xilinx_spartan3: |
Wolfgang Denk | 875c789 | 2005-09-25 16:44:21 +0200 | [diff] [blame] | 219 | printf ("Spartan-III\n"); |
| 220 | break; |
Michal Simek | d9071ce | 2014-03-13 11:33:36 +0100 | [diff] [blame] | 221 | case xilinx_virtex2: |
wdenk | 5d3207d | 2002-08-21 22:08:56 +0000 | [diff] [blame] | 222 | printf ("Virtex-II\n"); |
| 223 | break; |
Michal Simek | d5dae85 | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 224 | case xilinx_zynq: |
| 225 | printf("Zynq PL\n"); |
| 226 | break; |
Siva Durga Prasad Paladugu | 6b24501 | 2016-01-13 16:25:37 +0530 | [diff] [blame] | 227 | case xilinx_zynqmp: |
| 228 | printf("ZynqMP PL\n"); |
| 229 | break; |
Siva Durga Prasad Paladugu | 26e054c | 2019-08-05 15:54:59 +0530 | [diff] [blame] | 230 | case xilinx_versal: |
| 231 | printf("Versal PL\n"); |
| 232 | break; |
| 233 | /* Add new family types here */ |
wdenk | 5d3207d | 2002-08-21 22:08:56 +0000 | [diff] [blame] | 234 | default: |
| 235 | printf ("Unknown family type, %d\n", desc->family); |
| 236 | } |
| 237 | |
| 238 | printf ("Interface type:\t"); |
| 239 | switch (desc->iface) { |
| 240 | case slave_serial: |
| 241 | printf ("Slave Serial\n"); |
| 242 | break; |
| 243 | case master_serial: /* Not used */ |
| 244 | printf ("Master Serial\n"); |
| 245 | break; |
| 246 | case slave_parallel: |
| 247 | printf ("Slave Parallel\n"); |
| 248 | break; |
| 249 | case jtag_mode: /* Not used */ |
| 250 | printf ("JTAG Mode\n"); |
| 251 | break; |
| 252 | case slave_selectmap: |
| 253 | printf ("Slave SelectMap Mode\n"); |
| 254 | break; |
| 255 | case master_selectmap: |
| 256 | printf ("Master SelectMap Mode\n"); |
| 257 | break; |
Michal Simek | d5dae85 | 2013-04-22 15:43:02 +0200 | [diff] [blame] | 258 | case devcfg: |
| 259 | printf("Device configuration interface (Zynq)\n"); |
| 260 | break; |
Siva Durga Prasad Paladugu | 6b24501 | 2016-01-13 16:25:37 +0530 | [diff] [blame] | 261 | case csu_dma: |
| 262 | printf("csu_dma configuration interface (ZynqMP)\n"); |
| 263 | break; |
Siva Durga Prasad Paladugu | 26e054c | 2019-08-05 15:54:59 +0530 | [diff] [blame] | 264 | case cfi: |
| 265 | printf("CFI configuration interface (Versal)\n"); |
| 266 | break; |
wdenk | 5d3207d | 2002-08-21 22:08:56 +0000 | [diff] [blame] | 267 | /* Add new interface types here */ |
| 268 | default: |
| 269 | printf ("Unsupported interface type, %d\n", desc->iface); |
| 270 | } |
| 271 | |
Simon Glass | ddc9437 | 2014-06-07 22:07:58 -0600 | [diff] [blame] | 272 | printf("Device Size: \t%zd bytes\n" |
| 273 | "Cookie: \t0x%x (%d)\n", |
| 274 | desc->size, desc->cookie, desc->cookie); |
Michal Simek | 6631db4 | 2013-04-26 15:04:48 +0200 | [diff] [blame] | 275 | if (desc->name) |
| 276 | printf("Device name: \t%s\n", desc->name); |
wdenk | 5d3207d | 2002-08-21 22:08:56 +0000 | [diff] [blame] | 277 | |
Michal Simek | e136eae | 2014-07-16 10:36:42 +0200 | [diff] [blame] | 278 | if (desc->iface_fns) |
wdenk | 5d3207d | 2002-08-21 22:08:56 +0000 | [diff] [blame] | 279 | printf ("Device Function Table @ 0x%p\n", desc->iface_fns); |
Michal Simek | e136eae | 2014-07-16 10:36:42 +0200 | [diff] [blame] | 280 | else |
wdenk | 5d3207d | 2002-08-21 22:08:56 +0000 | [diff] [blame] | 281 | printf ("No Device Function Table.\n"); |
| 282 | |
Michal Simek | e136eae | 2014-07-16 10:36:42 +0200 | [diff] [blame] | 283 | if (desc->operations && desc->operations->info) |
| 284 | desc->operations->info(desc); |
| 285 | |
wdenk | 5d3207d | 2002-08-21 22:08:56 +0000 | [diff] [blame] | 286 | ret_val = FPGA_SUCCESS; |
| 287 | } else { |
| 288 | printf ("%s: Invalid device descriptor\n", __FUNCTION__); |
| 289 | } |
| 290 | |
| 291 | return ret_val; |
| 292 | } |
| 293 | |
wdenk | 5d3207d | 2002-08-21 22:08:56 +0000 | [diff] [blame] | 294 | /* ------------------------------------------------------------------------- */ |
| 295 | |
Michal Simek | f8c1be9 | 2014-03-13 12:49:21 +0100 | [diff] [blame] | 296 | static int xilinx_validate(xilinx_desc *desc, char *fn) |
wdenk | 5d3207d | 2002-08-21 22:08:56 +0000 | [diff] [blame] | 297 | { |
York Sun | 472d546 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 298 | int ret_val = false; |
wdenk | 5d3207d | 2002-08-21 22:08:56 +0000 | [diff] [blame] | 299 | |
| 300 | if (desc) { |
| 301 | if ((desc->family > min_xilinx_type) && |
| 302 | (desc->family < max_xilinx_type)) { |
| 303 | if ((desc->iface > min_xilinx_iface_type) && |
| 304 | (desc->iface < max_xilinx_iface_type)) { |
| 305 | if (desc->size) { |
York Sun | 472d546 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 306 | ret_val = true; |
wdenk | 5d3207d | 2002-08-21 22:08:56 +0000 | [diff] [blame] | 307 | } else |
| 308 | printf ("%s: NULL part size\n", fn); |
| 309 | } else |
| 310 | printf ("%s: Invalid Interface type, %d\n", |
| 311 | fn, desc->iface); |
| 312 | } else |
| 313 | printf ("%s: Invalid family type, %d\n", fn, desc->family); |
| 314 | } else |
| 315 | printf ("%s: NULL descriptor!\n", fn); |
| 316 | |
| 317 | return ret_val; |
| 318 | } |