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