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