wdenk | 4a9cbbe | 2002-08-27 09:48:53 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2000, 2001 |
| 3 | * Rich Ireland, Enterasys Networks, rireland@enterasys.com. |
| 4 | * |
| 5 | * See file CREDITS for list of people who contributed to this |
| 6 | * project. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of |
| 11 | * the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 21 | * MA 02111-1307 USA |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | /* |
| 26 | * FPGA support |
| 27 | */ |
| 28 | #include <common.h> |
| 29 | #include <command.h> |
wdenk | 4a9cbbe | 2002-08-27 09:48:53 +0000 | [diff] [blame] | 30 | #if (CONFIG_COMMANDS & CFG_CMD_NET) |
| 31 | #include <net.h> |
| 32 | #endif |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 33 | #include <fpga.h> |
wdenk | c3d2b4b | 2005-01-22 18:13:04 +0000 | [diff] [blame] | 34 | #include <malloc.h> |
wdenk | 4a9cbbe | 2002-08-27 09:48:53 +0000 | [diff] [blame] | 35 | |
| 36 | #if 0 |
| 37 | #define FPGA_DEBUG |
| 38 | #endif |
| 39 | |
| 40 | #ifdef FPGA_DEBUG |
| 41 | #define PRINTF(fmt,args...) printf (fmt ,##args) |
| 42 | #else |
| 43 | #define PRINTF(fmt,args...) |
| 44 | #endif |
| 45 | |
| 46 | #if defined (CONFIG_FPGA) && ( CONFIG_COMMANDS & CFG_CMD_FPGA ) |
| 47 | |
| 48 | /* Local functions */ |
wdenk | d4ca31c | 2004-01-02 14:00:00 +0000 | [diff] [blame] | 49 | static void fpga_usage (cmd_tbl_t * cmdtp); |
| 50 | static int fpga_get_op (char *opstr); |
wdenk | 4a9cbbe | 2002-08-27 09:48:53 +0000 | [diff] [blame] | 51 | |
| 52 | /* Local defines */ |
| 53 | #define FPGA_NONE -1 |
| 54 | #define FPGA_INFO 0 |
| 55 | #define FPGA_LOAD 1 |
wdenk | 30ce5ab | 2005-01-09 18:12:51 +0000 | [diff] [blame] | 56 | #define FPGA_LOADB 2 |
wdenk | 4a9cbbe | 2002-08-27 09:48:53 +0000 | [diff] [blame] | 57 | #define FPGA_DUMP 3 |
| 58 | |
wdenk | 30ce5ab | 2005-01-09 18:12:51 +0000 | [diff] [blame] | 59 | /* Convert bitstream data and load into the fpga */ |
| 60 | int fpga_loadbitstream(unsigned long dev, char* fpgadata, size_t size) |
| 61 | { |
| 62 | int length; |
| 63 | char* swapdata; |
| 64 | int swapsize; |
| 65 | char buffer[80]; |
| 66 | char *ptr; |
| 67 | char *dataptr; |
| 68 | int data; |
| 69 | int i; |
| 70 | int rc; |
| 71 | |
| 72 | dataptr = fpgadata; |
| 73 | |
| 74 | #if CFG_FPGA_XILINX |
| 75 | /* skip the first 13 bytes of the bitsteam, their meaning is unknown */ |
| 76 | dataptr+=13; |
| 77 | |
| 78 | /* get design name (identifier, length, string) */ |
| 79 | if (*dataptr++ != 0x61) { |
wdenk | c3d2b4b | 2005-01-22 18:13:04 +0000 | [diff] [blame] | 80 | PRINTF("fpga_loadbitstream: Design name identifier not recognized in bitstream.\n"); |
wdenk | 30ce5ab | 2005-01-09 18:12:51 +0000 | [diff] [blame] | 81 | return FPGA_FAIL; |
| 82 | } |
| 83 | |
wdenk | a562e1b | 2005-01-09 18:21:42 +0000 | [diff] [blame] | 84 | length = (*dataptr << 8) + *(dataptr+1); |
wdenk | 30ce5ab | 2005-01-09 18:12:51 +0000 | [diff] [blame] | 85 | dataptr+=2; |
| 86 | for(i=0;i<length;i++) |
| 87 | buffer[i]=*dataptr++; |
wdenk | a562e1b | 2005-01-09 18:21:42 +0000 | [diff] [blame] | 88 | |
wdenk | 30ce5ab | 2005-01-09 18:12:51 +0000 | [diff] [blame] | 89 | buffer[length-5]='\0'; /* remove filename extension */ |
wdenk | c3d2b4b | 2005-01-22 18:13:04 +0000 | [diff] [blame] | 90 | PRINTF("fpga_loadbitstream: design name = \"%s\".\n",buffer); |
wdenk | 30ce5ab | 2005-01-09 18:12:51 +0000 | [diff] [blame] | 91 | |
| 92 | /* get part number (identifier, length, string) */ |
| 93 | if (*dataptr++ != 0x62) { |
wdenk | c3d2b4b | 2005-01-22 18:13:04 +0000 | [diff] [blame] | 94 | printf("fpga_loadbitstream: Part number identifier not recognized in bitstream.\n"); |
wdenk | 30ce5ab | 2005-01-09 18:12:51 +0000 | [diff] [blame] | 95 | return FPGA_FAIL; |
| 96 | } |
wdenk | a562e1b | 2005-01-09 18:21:42 +0000 | [diff] [blame] | 97 | |
wdenk | 30ce5ab | 2005-01-09 18:12:51 +0000 | [diff] [blame] | 98 | length = (*dataptr << 8) + *(dataptr+1); dataptr+=2; |
wdenk | a562e1b | 2005-01-09 18:21:42 +0000 | [diff] [blame] | 99 | for(i=0;i<length;i++) |
wdenk | 30ce5ab | 2005-01-09 18:12:51 +0000 | [diff] [blame] | 100 | buffer[i]=*dataptr++; |
wdenk | c3d2b4b | 2005-01-22 18:13:04 +0000 | [diff] [blame] | 101 | PRINTF("fpga_loadbitstream: part number = \"%s\".\n",buffer); |
wdenk | a562e1b | 2005-01-09 18:21:42 +0000 | [diff] [blame] | 102 | |
wdenk | 30ce5ab | 2005-01-09 18:12:51 +0000 | [diff] [blame] | 103 | /* get date (identifier, length, string) */ |
| 104 | if (*dataptr++ != 0x63) { |
wdenk | c3d2b4b | 2005-01-22 18:13:04 +0000 | [diff] [blame] | 105 | printf("fpga_loadbitstream: Date identifier not recognized in bitstream.\n"); |
wdenk | 30ce5ab | 2005-01-09 18:12:51 +0000 | [diff] [blame] | 106 | return FPGA_FAIL; |
| 107 | } |
wdenk | a562e1b | 2005-01-09 18:21:42 +0000 | [diff] [blame] | 108 | |
wdenk | 30ce5ab | 2005-01-09 18:12:51 +0000 | [diff] [blame] | 109 | length = (*dataptr << 8) + *(dataptr+1); dataptr+=2; |
| 110 | for(i=0;i<length;i++) |
| 111 | buffer[i]=*dataptr++; |
wdenk | c3d2b4b | 2005-01-22 18:13:04 +0000 | [diff] [blame] | 112 | PRINTF("fpga_loadbitstream: date = \"%s\".\n",buffer); |
wdenk | 30ce5ab | 2005-01-09 18:12:51 +0000 | [diff] [blame] | 113 | |
| 114 | /* get time (identifier, length, string) */ |
| 115 | if (*dataptr++ != 0x64) { |
wdenk | c3d2b4b | 2005-01-22 18:13:04 +0000 | [diff] [blame] | 116 | printf("fpga_loadbitstream: Time identifier not recognized in bitstream.\n"); |
wdenk | 30ce5ab | 2005-01-09 18:12:51 +0000 | [diff] [blame] | 117 | return FPGA_FAIL; |
| 118 | } |
wdenk | a562e1b | 2005-01-09 18:21:42 +0000 | [diff] [blame] | 119 | |
wdenk | 30ce5ab | 2005-01-09 18:12:51 +0000 | [diff] [blame] | 120 | length = (*dataptr << 8) + *(dataptr+1); dataptr+=2; |
| 121 | for(i=0;i<length;i++) |
| 122 | buffer[i]=*dataptr++; |
wdenk | c3d2b4b | 2005-01-22 18:13:04 +0000 | [diff] [blame] | 123 | PRINTF("fpga_loadbitstream: time = \"%s\".\n",buffer); |
wdenk | a562e1b | 2005-01-09 18:21:42 +0000 | [diff] [blame] | 124 | |
wdenk | 30ce5ab | 2005-01-09 18:12:51 +0000 | [diff] [blame] | 125 | /* get fpga data length (identifier, length) */ |
| 126 | if (*dataptr++ != 0x65) { |
wdenk | c3d2b4b | 2005-01-22 18:13:04 +0000 | [diff] [blame] | 127 | printf("fpga_loadbitstream: Data length identifier not recognized in bitstream.\n"); |
wdenk | 30ce5ab | 2005-01-09 18:12:51 +0000 | [diff] [blame] | 128 | return FPGA_FAIL; |
| 129 | } |
| 130 | swapsize = ((long)*dataptr<<24) + ((long)*(dataptr+1)<<16) + ((long)*(dataptr+2)<<8) + (long)*(dataptr+3); |
| 131 | dataptr+=4; |
wdenk | c3d2b4b | 2005-01-22 18:13:04 +0000 | [diff] [blame] | 132 | PRINTF("fpga_loadbitstream: bytes in bitstream = %d.\n",swapsize); |
wdenk | a562e1b | 2005-01-09 18:21:42 +0000 | [diff] [blame] | 133 | |
wdenk | 30ce5ab | 2005-01-09 18:12:51 +0000 | [diff] [blame] | 134 | /* check consistency of length obtained */ |
| 135 | if (swapsize >= size) { |
wdenk | c3d2b4b | 2005-01-22 18:13:04 +0000 | [diff] [blame] | 136 | printf("fpga_loadbitstream: Could not find right length of data in bitstream.\n"); |
wdenk | 30ce5ab | 2005-01-09 18:12:51 +0000 | [diff] [blame] | 137 | return FPGA_FAIL; |
| 138 | } |
wdenk | a562e1b | 2005-01-09 18:21:42 +0000 | [diff] [blame] | 139 | |
wdenk | 30ce5ab | 2005-01-09 18:12:51 +0000 | [diff] [blame] | 140 | /* allocate memory */ |
| 141 | swapdata = (char *)malloc(swapsize); |
| 142 | if (swapdata == NULL) { |
wdenk | c3d2b4b | 2005-01-22 18:13:04 +0000 | [diff] [blame] | 143 | printf("fpga_loadbitstream: Could not allocate %d bytes memory !\n",swapsize); |
wdenk | 30ce5ab | 2005-01-09 18:12:51 +0000 | [diff] [blame] | 144 | return FPGA_FAIL; |
| 145 | } |
wdenk | a562e1b | 2005-01-09 18:21:42 +0000 | [diff] [blame] | 146 | |
wdenk | 30ce5ab | 2005-01-09 18:12:51 +0000 | [diff] [blame] | 147 | /* read data into memory and swap bits */ |
| 148 | ptr = swapdata; |
| 149 | for (i = 0; i < swapsize; i++) { |
| 150 | data = 0x00; |
| 151 | data |= (*dataptr & 0x01) << 7; |
| 152 | data |= (*dataptr & 0x02) << 5; |
| 153 | data |= (*dataptr & 0x04) << 3; |
| 154 | data |= (*dataptr & 0x08) << 1; |
| 155 | data |= (*dataptr & 0x10) >> 1; |
| 156 | data |= (*dataptr & 0x20) >> 3; |
| 157 | data |= (*dataptr & 0x40) >> 5; |
| 158 | data |= (*dataptr & 0x80) >> 7; |
| 159 | *ptr++ = data; |
| 160 | dataptr++; |
| 161 | } |
| 162 | |
| 163 | rc = fpga_load(dev, swapdata, swapsize); |
| 164 | free(swapdata); |
| 165 | return rc; |
| 166 | #else |
| 167 | printf("Bitstream support only for Xilinx devices.\n"); |
| 168 | return FPGA_FAIL; |
| 169 | #endif |
| 170 | } |
| 171 | |
wdenk | 4a9cbbe | 2002-08-27 09:48:53 +0000 | [diff] [blame] | 172 | /* ------------------------------------------------------------------------- */ |
| 173 | /* command form: |
| 174 | * fpga <op> <device number> <data addr> <datasize> |
| 175 | * where op is 'load', 'dump', or 'info' |
| 176 | * If there is no device number field, the fpga environment variable is used. |
| 177 | * If there is no data addr field, the fpgadata environment variable is used. |
| 178 | * The info command requires no data address field. |
| 179 | */ |
wdenk | d4ca31c | 2004-01-02 14:00:00 +0000 | [diff] [blame] | 180 | int do_fpga (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[]) |
wdenk | 4a9cbbe | 2002-08-27 09:48:53 +0000 | [diff] [blame] | 181 | { |
wdenk | d4ca31c | 2004-01-02 14:00:00 +0000 | [diff] [blame] | 182 | int op, dev = FPGA_INVALID_DEVICE; |
| 183 | size_t data_size = 0; |
| 184 | void *fpga_data = NULL; |
| 185 | char *devstr = getenv ("fpga"); |
| 186 | char *datastr = getenv ("fpgadata"); |
| 187 | int rc = FPGA_FAIL; |
wdenk | 4a9cbbe | 2002-08-27 09:48:53 +0000 | [diff] [blame] | 188 | |
wdenk | d4ca31c | 2004-01-02 14:00:00 +0000 | [diff] [blame] | 189 | if (devstr) |
| 190 | dev = (int) simple_strtoul (devstr, NULL, 16); |
| 191 | if (datastr) |
| 192 | fpga_data = (void *) simple_strtoul (datastr, NULL, 16); |
wdenk | 4a9cbbe | 2002-08-27 09:48:53 +0000 | [diff] [blame] | 193 | |
wdenk | d4ca31c | 2004-01-02 14:00:00 +0000 | [diff] [blame] | 194 | switch (argc) { |
| 195 | case 5: /* fpga <op> <dev> <data> <datasize> */ |
| 196 | data_size = simple_strtoul (argv[4], NULL, 16); |
| 197 | case 4: /* fpga <op> <dev> <data> */ |
| 198 | fpga_data = (void *) simple_strtoul (argv[3], NULL, 16); |
wdenk | c3d2b4b | 2005-01-22 18:13:04 +0000 | [diff] [blame] | 199 | PRINTF ("do_fpga: fpga_data = 0x%x\n", |
wdenk | d4ca31c | 2004-01-02 14:00:00 +0000 | [diff] [blame] | 200 | (uint) fpga_data); |
| 201 | case 3: /* fpga <op> <dev | data addr> */ |
| 202 | dev = (int) simple_strtoul (argv[2], NULL, 16); |
wdenk | c3d2b4b | 2005-01-22 18:13:04 +0000 | [diff] [blame] | 203 | PRINTF ("do_fpga: device = %d\n", dev); |
wdenk | d4ca31c | 2004-01-02 14:00:00 +0000 | [diff] [blame] | 204 | /* FIXME - this is a really weak test */ |
| 205 | if ((argc == 3) && (dev > fpga_count ())) { /* must be buffer ptr */ |
wdenk | c3d2b4b | 2005-01-22 18:13:04 +0000 | [diff] [blame] | 206 | PRINTF ("do_fpga: Assuming buffer pointer in arg 3\n"); |
wdenk | d4ca31c | 2004-01-02 14:00:00 +0000 | [diff] [blame] | 207 | fpga_data = (void *) dev; |
wdenk | c3d2b4b | 2005-01-22 18:13:04 +0000 | [diff] [blame] | 208 | PRINTF ("do_fpga: fpga_data = 0x%x\n", |
wdenk | d4ca31c | 2004-01-02 14:00:00 +0000 | [diff] [blame] | 209 | (uint) fpga_data); |
| 210 | dev = FPGA_INVALID_DEVICE; /* reset device num */ |
| 211 | } |
| 212 | case 2: /* fpga <op> */ |
| 213 | op = (int) fpga_get_op (argv[1]); |
| 214 | break; |
| 215 | default: |
wdenk | c3d2b4b | 2005-01-22 18:13:04 +0000 | [diff] [blame] | 216 | PRINTF ("do_fpga: Too many or too few args (%d)\n", |
wdenk | d4ca31c | 2004-01-02 14:00:00 +0000 | [diff] [blame] | 217 | argc); |
| 218 | op = FPGA_NONE; /* force usage display */ |
| 219 | break; |
| 220 | } |
wdenk | 4a9cbbe | 2002-08-27 09:48:53 +0000 | [diff] [blame] | 221 | |
wdenk | d4ca31c | 2004-01-02 14:00:00 +0000 | [diff] [blame] | 222 | switch (op) { |
| 223 | case FPGA_NONE: |
| 224 | fpga_usage (cmdtp); |
| 225 | break; |
wdenk | 4a9cbbe | 2002-08-27 09:48:53 +0000 | [diff] [blame] | 226 | |
wdenk | d4ca31c | 2004-01-02 14:00:00 +0000 | [diff] [blame] | 227 | case FPGA_INFO: |
| 228 | rc = fpga_info (dev); |
| 229 | break; |
wdenk | 4a9cbbe | 2002-08-27 09:48:53 +0000 | [diff] [blame] | 230 | |
wdenk | d4ca31c | 2004-01-02 14:00:00 +0000 | [diff] [blame] | 231 | case FPGA_LOAD: |
| 232 | rc = fpga_load (dev, fpga_data, data_size); |
| 233 | break; |
wdenk | 4a9cbbe | 2002-08-27 09:48:53 +0000 | [diff] [blame] | 234 | |
wdenk | 30ce5ab | 2005-01-09 18:12:51 +0000 | [diff] [blame] | 235 | case FPGA_LOADB: |
| 236 | rc = fpga_loadbitstream(dev, fpga_data, data_size); |
| 237 | break; |
| 238 | |
wdenk | d4ca31c | 2004-01-02 14:00:00 +0000 | [diff] [blame] | 239 | case FPGA_DUMP: |
| 240 | rc = fpga_dump (dev, fpga_data, data_size); |
| 241 | break; |
wdenk | 4a9cbbe | 2002-08-27 09:48:53 +0000 | [diff] [blame] | 242 | |
wdenk | d4ca31c | 2004-01-02 14:00:00 +0000 | [diff] [blame] | 243 | default: |
| 244 | printf ("Unknown operation.\n"); |
| 245 | fpga_usage (cmdtp); |
| 246 | break; |
| 247 | } |
| 248 | return (rc); |
wdenk | 4a9cbbe | 2002-08-27 09:48:53 +0000 | [diff] [blame] | 249 | } |
| 250 | |
wdenk | d4ca31c | 2004-01-02 14:00:00 +0000 | [diff] [blame] | 251 | static void fpga_usage (cmd_tbl_t * cmdtp) |
wdenk | 4a9cbbe | 2002-08-27 09:48:53 +0000 | [diff] [blame] | 252 | { |
wdenk | d4ca31c | 2004-01-02 14:00:00 +0000 | [diff] [blame] | 253 | printf ("Usage:\n%s\n", cmdtp->usage); |
wdenk | 4a9cbbe | 2002-08-27 09:48:53 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /* |
| 257 | * Map op to supported operations. We don't use a table since we |
| 258 | * would just have to relocate it from flash anyway. |
| 259 | */ |
wdenk | d4ca31c | 2004-01-02 14:00:00 +0000 | [diff] [blame] | 260 | static int fpga_get_op (char *opstr) |
wdenk | 4a9cbbe | 2002-08-27 09:48:53 +0000 | [diff] [blame] | 261 | { |
| 262 | int op = FPGA_NONE; |
| 263 | |
| 264 | if (!strcmp ("info", opstr)) { |
| 265 | op = FPGA_INFO; |
wdenk | 30ce5ab | 2005-01-09 18:12:51 +0000 | [diff] [blame] | 266 | } else if (!strcmp ("loadb", opstr)) { |
| 267 | op = FPGA_LOADB; |
wdenk | d4ca31c | 2004-01-02 14:00:00 +0000 | [diff] [blame] | 268 | } else if (!strcmp ("load", opstr)) { |
wdenk | 4a9cbbe | 2002-08-27 09:48:53 +0000 | [diff] [blame] | 269 | op = FPGA_LOAD; |
wdenk | d4ca31c | 2004-01-02 14:00:00 +0000 | [diff] [blame] | 270 | } else if (!strcmp ("dump", opstr)) { |
wdenk | 4a9cbbe | 2002-08-27 09:48:53 +0000 | [diff] [blame] | 271 | op = FPGA_DUMP; |
| 272 | } |
| 273 | |
wdenk | d4ca31c | 2004-01-02 14:00:00 +0000 | [diff] [blame] | 274 | if (op == FPGA_NONE) { |
wdenk | 4a9cbbe | 2002-08-27 09:48:53 +0000 | [diff] [blame] | 275 | printf ("Unknown fpga operation \"%s\"\n", opstr); |
| 276 | } |
| 277 | return op; |
| 278 | } |
| 279 | |
wdenk | d4ca31c | 2004-01-02 14:00:00 +0000 | [diff] [blame] | 280 | U_BOOT_CMD (fpga, 6, 1, do_fpga, |
wdenk | dd875c7 | 2004-01-03 21:24:46 +0000 | [diff] [blame] | 281 | "fpga - loadable FPGA image support\n", |
wdenk | d4ca31c | 2004-01-02 14:00:00 +0000 | [diff] [blame] | 282 | "fpga [operation type] [device number] [image address] [image size]\n" |
| 283 | "fpga operations:\n" |
| 284 | "\tinfo\tlist known device information.\n" |
| 285 | "\tload\tLoad device from memory buffer.\n" |
wdenk | 30ce5ab | 2005-01-09 18:12:51 +0000 | [diff] [blame] | 286 | "\tloadb\tLoad device from bitstream buffer (Xilinx devices only).\n" |
wdenk | d4ca31c | 2004-01-02 14:00:00 +0000 | [diff] [blame] | 287 | "\tdump\tLoad device to memory buffer.\n"); |
| 288 | #endif /* CONFIG_FPGA && CONFIG_COMMANDS & CFG_CMD_FPGA */ |