wdenk | e221174 | 2002-11-02 23:30:20 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002 |
| 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 | * Generic FPGA support |
| 27 | */ |
| 28 | #include <common.h> /* core U-Boot definitions */ |
| 29 | #include <xilinx.h> /* xilinx specific definitions */ |
| 30 | #include <altera.h> /* altera specific definitions */ |
Stefano Babic | 3b8ac46 | 2010-06-29 11:47:48 +0200 | [diff] [blame^] | 31 | #include <lattice.h> |
wdenk | e221174 | 2002-11-02 23:30:20 +0000 | [diff] [blame] | 32 | |
wdenk | e221174 | 2002-11-02 23:30:20 +0000 | [diff] [blame] | 33 | #if 0 |
| 34 | #define FPGA_DEBUG /* define FPGA_DEBUG to get debug messages */ |
| 35 | #endif |
| 36 | |
| 37 | /* Local definitions */ |
| 38 | #ifndef CONFIG_MAX_FPGA_DEVICES |
| 39 | #define CONFIG_MAX_FPGA_DEVICES 5 |
| 40 | #endif |
| 41 | |
| 42 | /* Enable/Disable debug console messages */ |
| 43 | #ifdef FPGA_DEBUG |
| 44 | #define PRINTF(fmt,args...) printf (fmt ,##args) |
| 45 | #else |
| 46 | #define PRINTF(fmt,args...) |
| 47 | #endif |
| 48 | |
| 49 | /* Local static data */ |
wdenk | e221174 | 2002-11-02 23:30:20 +0000 | [diff] [blame] | 50 | static int next_desc = FPGA_INVALID_DEVICE; |
| 51 | static fpga_desc desc_table[CONFIG_MAX_FPGA_DEVICES]; |
| 52 | |
| 53 | /* Local static functions */ |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 54 | static __attribute__((__const__)) fpga_desc * __attribute__((__const__)) fpga_get_desc( int devnum ); |
| 55 | static __attribute__((__const__)) fpga_desc * __attribute__((__const__)) fpga_validate( int devnum, void *buf, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 56 | size_t bsize, char *fn ); |
wdenk | e221174 | 2002-11-02 23:30:20 +0000 | [diff] [blame] | 57 | static int fpga_dev_info( int devnum ); |
| 58 | |
| 59 | |
| 60 | /* ------------------------------------------------------------------------- */ |
| 61 | |
| 62 | /* fpga_no_sup |
| 63 | * 'no support' message function |
| 64 | */ |
| 65 | static void fpga_no_sup( char *fn, char *msg ) |
| 66 | { |
| 67 | if ( fn && msg ) { |
Matthias Fuchs | 0133502 | 2007-12-27 17:12:34 +0100 | [diff] [blame] | 68 | printf( "%s: No support for %s.\n", fn, msg); |
wdenk | e221174 | 2002-11-02 23:30:20 +0000 | [diff] [blame] | 69 | } else if ( msg ) { |
Matthias Fuchs | 0133502 | 2007-12-27 17:12:34 +0100 | [diff] [blame] | 70 | printf( "No support for %s.\n", msg); |
wdenk | e221174 | 2002-11-02 23:30:20 +0000 | [diff] [blame] | 71 | } else { |
Matthias Fuchs | 0133502 | 2007-12-27 17:12:34 +0100 | [diff] [blame] | 72 | printf( "No FPGA suport!\n"); |
wdenk | e221174 | 2002-11-02 23:30:20 +0000 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
| 76 | |
| 77 | /* fpga_get_desc |
| 78 | * map a device number to a descriptor |
| 79 | */ |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 80 | static __attribute__((__const__)) fpga_desc * __attribute__((__const__)) fpga_get_desc( int devnum ) |
wdenk | e221174 | 2002-11-02 23:30:20 +0000 | [diff] [blame] | 81 | { |
| 82 | fpga_desc *desc = (fpga_desc * )NULL; |
| 83 | |
| 84 | if (( devnum >= 0 ) && (devnum < next_desc )) { |
| 85 | desc = &desc_table[devnum]; |
| 86 | PRINTF( "%s: found fpga descriptor #%d @ 0x%p\n", |
| 87 | __FUNCTION__, devnum, desc ); |
| 88 | } |
| 89 | |
| 90 | return desc; |
| 91 | } |
| 92 | |
| 93 | |
| 94 | /* fpga_validate |
| 95 | * generic parameter checking code |
| 96 | */ |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 97 | static __attribute__((__const__)) fpga_desc * __attribute__((__const__)) fpga_validate( int devnum, void *buf, |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 98 | size_t bsize, char *fn ) |
wdenk | e221174 | 2002-11-02 23:30:20 +0000 | [diff] [blame] | 99 | { |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 100 | fpga_desc * desc = fpga_get_desc( devnum ); |
wdenk | e221174 | 2002-11-02 23:30:20 +0000 | [diff] [blame] | 101 | |
| 102 | if ( !desc ) { |
| 103 | printf( "%s: Invalid device number %d\n", fn, devnum ); |
| 104 | } |
| 105 | |
| 106 | if ( !buf ) { |
| 107 | printf( "%s: Null buffer.\n", fn ); |
| 108 | return (fpga_desc * const)NULL; |
| 109 | } |
wdenk | e221174 | 2002-11-02 23:30:20 +0000 | [diff] [blame] | 110 | return desc; |
| 111 | } |
| 112 | |
| 113 | |
| 114 | /* fpga_dev_info |
| 115 | * generic multiplexing code |
| 116 | */ |
| 117 | static int fpga_dev_info( int devnum ) |
| 118 | { |
| 119 | int ret_val = FPGA_FAIL; /* assume failure */ |
| 120 | const fpga_desc * const desc = fpga_get_desc( devnum ); |
| 121 | |
| 122 | if ( desc ) { |
| 123 | PRINTF( "%s: Device Descriptor @ 0x%p\n", |
| 124 | __FUNCTION__, desc->devdesc ); |
| 125 | |
| 126 | switch ( desc->devtype ) { |
| 127 | case fpga_xilinx: |
Matthias Fuchs | 0133502 | 2007-12-27 17:12:34 +0100 | [diff] [blame] | 128 | #if defined(CONFIG_FPGA_XILINX) |
wdenk | e221174 | 2002-11-02 23:30:20 +0000 | [diff] [blame] | 129 | printf( "Xilinx Device\nDescriptor @ 0x%p\n", desc ); |
| 130 | ret_val = xilinx_info( desc->devdesc ); |
| 131 | #else |
Stefan Roese | 64cd52e | 2006-09-18 10:48:03 +0200 | [diff] [blame] | 132 | fpga_no_sup( (char *)__FUNCTION__, "Xilinx devices" ); |
wdenk | e221174 | 2002-11-02 23:30:20 +0000 | [diff] [blame] | 133 | #endif |
| 134 | break; |
| 135 | case fpga_altera: |
Matthias Fuchs | 0133502 | 2007-12-27 17:12:34 +0100 | [diff] [blame] | 136 | #if defined(CONFIG_FPGA_ALTERA) |
wdenk | e221174 | 2002-11-02 23:30:20 +0000 | [diff] [blame] | 137 | printf( "Altera Device\nDescriptor @ 0x%p\n", desc ); |
| 138 | ret_val = altera_info( desc->devdesc ); |
| 139 | #else |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 140 | fpga_no_sup( (char *)__FUNCTION__, "Altera devices" ); |
wdenk | e221174 | 2002-11-02 23:30:20 +0000 | [diff] [blame] | 141 | #endif |
| 142 | break; |
Stefano Babic | 3b8ac46 | 2010-06-29 11:47:48 +0200 | [diff] [blame^] | 143 | case fpga_lattice: |
| 144 | printf("Lattice Device\nDescriptor @ 0x%p\n", desc); |
| 145 | ret_val = lattice_info(desc->devdesc); |
| 146 | break; |
wdenk | e221174 | 2002-11-02 23:30:20 +0000 | [diff] [blame] | 147 | default: |
| 148 | printf( "%s: Invalid or unsupported device type %d\n", |
| 149 | __FUNCTION__, desc->devtype ); |
| 150 | } |
| 151 | } else { |
| 152 | printf( "%s: Invalid device number %d\n", |
| 153 | __FUNCTION__, devnum ); |
| 154 | } |
| 155 | |
| 156 | return ret_val; |
| 157 | } |
| 158 | |
| 159 | |
wdenk | e221174 | 2002-11-02 23:30:20 +0000 | [diff] [blame] | 160 | /* ------------------------------------------------------------------------- */ |
| 161 | /* fgpa_init is usually called from misc_init_r() and MUST be called |
| 162 | * before any of the other fpga functions are used. |
| 163 | */ |
Peter Tyser | 6385b28 | 2009-09-21 11:20:32 -0500 | [diff] [blame] | 164 | void fpga_init(void) |
wdenk | e221174 | 2002-11-02 23:30:20 +0000 | [diff] [blame] | 165 | { |
wdenk | e221174 | 2002-11-02 23:30:20 +0000 | [diff] [blame] | 166 | next_desc = 0; |
| 167 | memset( desc_table, 0, sizeof(desc_table)); |
| 168 | |
| 169 | PRINTF( "%s: CONFIG_FPGA = 0x%x\n", __FUNCTION__, CONFIG_FPGA ); |
wdenk | e221174 | 2002-11-02 23:30:20 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /* fpga_count |
| 173 | * Basic interface function to get the current number of devices available. |
| 174 | */ |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 175 | int fpga_count( void ) |
wdenk | e221174 | 2002-11-02 23:30:20 +0000 | [diff] [blame] | 176 | { |
| 177 | return next_desc; |
| 178 | } |
| 179 | |
| 180 | /* fpga_add |
Peter Tyser | 6385b28 | 2009-09-21 11:20:32 -0500 | [diff] [blame] | 181 | * Add the device descriptor to the device table. |
wdenk | e221174 | 2002-11-02 23:30:20 +0000 | [diff] [blame] | 182 | */ |
| 183 | int fpga_add( fpga_type devtype, void *desc ) |
| 184 | { |
| 185 | int devnum = FPGA_INVALID_DEVICE; |
| 186 | |
| 187 | if ( next_desc < 0 ) { |
| 188 | printf( "%s: FPGA support not initialized!\n", __FUNCTION__ ); |
| 189 | } else if (( devtype > fpga_min_type ) && ( devtype < fpga_undefined )) { |
| 190 | if ( desc ) { |
| 191 | if ( next_desc < CONFIG_MAX_FPGA_DEVICES ) { |
Peter Tyser | 6385b28 | 2009-09-21 11:20:32 -0500 | [diff] [blame] | 192 | devnum = next_desc; |
| 193 | desc_table[next_desc].devtype = devtype; |
| 194 | desc_table[next_desc++].devdesc = desc; |
wdenk | e221174 | 2002-11-02 23:30:20 +0000 | [diff] [blame] | 195 | } else { |
| 196 | printf( "%s: Exceeded Max FPGA device count\n", __FUNCTION__ ); |
| 197 | } |
| 198 | } else { |
| 199 | printf( "%s: NULL device descriptor\n", __FUNCTION__ ); |
| 200 | } |
| 201 | } else { |
| 202 | printf( "%s: Unsupported FPGA type %d\n", __FUNCTION__, devtype ); |
| 203 | } |
| 204 | |
| 205 | return devnum; |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | * Generic multiplexing code |
| 210 | */ |
| 211 | int fpga_load( int devnum, void *buf, size_t bsize ) |
| 212 | { |
| 213 | int ret_val = FPGA_FAIL; /* assume failure */ |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 214 | fpga_desc * desc = fpga_validate( devnum, buf, bsize, (char *)__FUNCTION__ ); |
wdenk | e221174 | 2002-11-02 23:30:20 +0000 | [diff] [blame] | 215 | |
| 216 | if ( desc ) { |
| 217 | switch ( desc->devtype ) { |
| 218 | case fpga_xilinx: |
Matthias Fuchs | 0133502 | 2007-12-27 17:12:34 +0100 | [diff] [blame] | 219 | #if defined(CONFIG_FPGA_XILINX) |
wdenk | e221174 | 2002-11-02 23:30:20 +0000 | [diff] [blame] | 220 | ret_val = xilinx_load( desc->devdesc, buf, bsize ); |
| 221 | #else |
Stefan Roese | 64cd52e | 2006-09-18 10:48:03 +0200 | [diff] [blame] | 222 | fpga_no_sup( (char *)__FUNCTION__, "Xilinx devices" ); |
wdenk | e221174 | 2002-11-02 23:30:20 +0000 | [diff] [blame] | 223 | #endif |
| 224 | break; |
| 225 | case fpga_altera: |
Matthias Fuchs | 0133502 | 2007-12-27 17:12:34 +0100 | [diff] [blame] | 226 | #if defined(CONFIG_FPGA_ALTERA) |
wdenk | e221174 | 2002-11-02 23:30:20 +0000 | [diff] [blame] | 227 | ret_val = altera_load( desc->devdesc, buf, bsize ); |
| 228 | #else |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 229 | fpga_no_sup( (char *)__FUNCTION__, "Altera devices" ); |
wdenk | e221174 | 2002-11-02 23:30:20 +0000 | [diff] [blame] | 230 | #endif |
| 231 | break; |
Stefano Babic | 3b8ac46 | 2010-06-29 11:47:48 +0200 | [diff] [blame^] | 232 | case fpga_lattice: |
| 233 | ret_val = lattice_load(desc->devdesc, buf, bsize); |
| 234 | break; |
wdenk | e221174 | 2002-11-02 23:30:20 +0000 | [diff] [blame] | 235 | default: |
| 236 | printf( "%s: Invalid or unsupported device type %d\n", |
| 237 | __FUNCTION__, desc->devtype ); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | return ret_val; |
| 242 | } |
| 243 | |
| 244 | /* fpga_dump |
| 245 | * generic multiplexing code |
| 246 | */ |
| 247 | int fpga_dump( int devnum, void *buf, size_t bsize ) |
| 248 | { |
| 249 | int ret_val = FPGA_FAIL; /* assume failure */ |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 250 | fpga_desc * desc = fpga_validate( devnum, buf, bsize, (char *)__FUNCTION__ ); |
wdenk | e221174 | 2002-11-02 23:30:20 +0000 | [diff] [blame] | 251 | |
| 252 | if ( desc ) { |
| 253 | switch ( desc->devtype ) { |
| 254 | case fpga_xilinx: |
Matthias Fuchs | 0133502 | 2007-12-27 17:12:34 +0100 | [diff] [blame] | 255 | #if defined(CONFIG_FPGA_XILINX) |
wdenk | e221174 | 2002-11-02 23:30:20 +0000 | [diff] [blame] | 256 | ret_val = xilinx_dump( desc->devdesc, buf, bsize ); |
| 257 | #else |
Stefan Roese | 64cd52e | 2006-09-18 10:48:03 +0200 | [diff] [blame] | 258 | fpga_no_sup( (char *)__FUNCTION__, "Xilinx devices" ); |
wdenk | e221174 | 2002-11-02 23:30:20 +0000 | [diff] [blame] | 259 | #endif |
| 260 | break; |
| 261 | case fpga_altera: |
Matthias Fuchs | 0133502 | 2007-12-27 17:12:34 +0100 | [diff] [blame] | 262 | #if defined(CONFIG_FPGA_ALTERA) |
wdenk | e221174 | 2002-11-02 23:30:20 +0000 | [diff] [blame] | 263 | ret_val = altera_dump( desc->devdesc, buf, bsize ); |
| 264 | #else |
Wolfgang Denk | 77ddac9 | 2005-10-13 16:45:02 +0200 | [diff] [blame] | 265 | fpga_no_sup( (char *)__FUNCTION__, "Altera devices" ); |
wdenk | e221174 | 2002-11-02 23:30:20 +0000 | [diff] [blame] | 266 | #endif |
| 267 | break; |
Stefano Babic | 3b8ac46 | 2010-06-29 11:47:48 +0200 | [diff] [blame^] | 268 | case fpga_lattice: |
| 269 | ret_val = lattice_dump(desc->devdesc, buf, bsize); |
| 270 | break; |
wdenk | e221174 | 2002-11-02 23:30:20 +0000 | [diff] [blame] | 271 | default: |
| 272 | printf( "%s: Invalid or unsupported device type %d\n", |
| 273 | __FUNCTION__, desc->devtype ); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | return ret_val; |
| 278 | } |
| 279 | |
| 280 | |
| 281 | /* fpga_info |
| 282 | * front end to fpga_dev_info. If devnum is invalid, report on all |
| 283 | * available devices. |
| 284 | */ |
| 285 | int fpga_info( int devnum ) |
| 286 | { |
| 287 | if ( devnum == FPGA_INVALID_DEVICE ) { |
| 288 | if ( next_desc > 0 ) { |
| 289 | int dev; |
| 290 | |
| 291 | for ( dev = 0; dev < next_desc; dev++ ) { |
| 292 | fpga_dev_info( dev ); |
| 293 | } |
| 294 | return FPGA_SUCCESS; |
| 295 | } else { |
| 296 | printf( "%s: No FPGA devices available.\n", __FUNCTION__ ); |
| 297 | return FPGA_FAIL; |
| 298 | } |
| 299 | } |
| 300 | else return fpga_dev_info( devnum ); |
| 301 | } |
| 302 | |
| 303 | /* ------------------------------------------------------------------------- */ |