1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28#include <common.h>
29#include <xilinx.h>
30#include <altera.h>
31#include <lattice.h>
32
33#if 0
34#define FPGA_DEBUG
35#endif
36
37
38#ifndef CONFIG_MAX_FPGA_DEVICES
39#define CONFIG_MAX_FPGA_DEVICES 5
40#endif
41
42
43#ifdef FPGA_DEBUG
44#define PRINTF(fmt,args...) printf (fmt ,##args)
45#else
46#define PRINTF(fmt,args...)
47#endif
48
49
50static int next_desc = FPGA_INVALID_DEVICE;
51static fpga_desc desc_table[CONFIG_MAX_FPGA_DEVICES];
52
53
54static __attribute__((__const__)) fpga_desc * __attribute__((__const__)) fpga_get_desc( int devnum );
55static __attribute__((__const__)) fpga_desc * __attribute__((__const__)) fpga_validate(int devnum, const void *buf,
56 size_t bsize, char *fn );
57static int fpga_dev_info( int devnum );
58
59
60
61
62
63
64
65static void fpga_no_sup( char *fn, char *msg )
66{
67 if ( fn && msg ) {
68 printf( "%s: No support for %s.\n", fn, msg);
69 } else if ( msg ) {
70 printf( "No support for %s.\n", msg);
71 } else {
72 printf( "No FPGA suport!\n");
73 }
74}
75
76
77
78
79
80static __attribute__((__const__)) fpga_desc * __attribute__((__const__)) fpga_get_desc( int devnum )
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
95
96
97static __attribute__((__const__)) fpga_desc * __attribute__((__const__)) fpga_validate(int devnum, const void *buf,
98 size_t bsize, char *fn )
99{
100 fpga_desc * desc = fpga_get_desc( devnum );
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 }
110 return desc;
111}
112
113
114
115
116
117static int fpga_dev_info( int devnum )
118{
119 int ret_val = FPGA_FAIL;
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:
128#if defined(CONFIG_FPGA_XILINX)
129 printf( "Xilinx Device\nDescriptor @ 0x%p\n", desc );
130 ret_val = xilinx_info( desc->devdesc );
131#else
132 fpga_no_sup( (char *)__FUNCTION__, "Xilinx devices" );
133#endif
134 break;
135 case fpga_altera:
136#if defined(CONFIG_FPGA_ALTERA)
137 printf( "Altera Device\nDescriptor @ 0x%p\n", desc );
138 ret_val = altera_info( desc->devdesc );
139#else
140 fpga_no_sup( (char *)__FUNCTION__, "Altera devices" );
141#endif
142 break;
143 case fpga_lattice:
144#if defined(CONFIG_FPGA_LATTICE)
145 printf("Lattice Device\nDescriptor @ 0x%p\n", desc);
146 ret_val = lattice_info(desc->devdesc);
147#else
148 fpga_no_sup( (char *)__FUNCTION__, "Lattice devices" );
149#endif
150 break;
151 default:
152 printf( "%s: Invalid or unsupported device type %d\n",
153 __FUNCTION__, desc->devtype );
154 }
155 } else {
156 printf( "%s: Invalid device number %d\n",
157 __FUNCTION__, devnum );
158 }
159
160 return ret_val;
161}
162
163
164
165
166
167
168void fpga_init(void)
169{
170 next_desc = 0;
171 memset( desc_table, 0, sizeof(desc_table));
172
173 PRINTF( "%s: CONFIG_FPGA = 0x%x\n", __FUNCTION__, CONFIG_FPGA );
174}
175
176
177
178
179int fpga_count( void )
180{
181 return next_desc;
182}
183
184
185
186
187int fpga_add( fpga_type devtype, void *desc )
188{
189 int devnum = FPGA_INVALID_DEVICE;
190
191 if ( next_desc < 0 ) {
192 printf( "%s: FPGA support not initialized!\n", __FUNCTION__ );
193 } else if (( devtype > fpga_min_type ) && ( devtype < fpga_undefined )) {
194 if ( desc ) {
195 if ( next_desc < CONFIG_MAX_FPGA_DEVICES ) {
196 devnum = next_desc;
197 desc_table[next_desc].devtype = devtype;
198 desc_table[next_desc++].devdesc = desc;
199 } else {
200 printf( "%s: Exceeded Max FPGA device count\n", __FUNCTION__ );
201 }
202 } else {
203 printf( "%s: NULL device descriptor\n", __FUNCTION__ );
204 }
205 } else {
206 printf( "%s: Unsupported FPGA type %d\n", __FUNCTION__, devtype );
207 }
208
209 return devnum;
210}
211
212
213
214
215int fpga_load(int devnum, const void *buf, size_t bsize)
216{
217 int ret_val = FPGA_FAIL;
218 fpga_desc * desc = fpga_validate( devnum, buf, bsize, (char *)__FUNCTION__ );
219
220 if ( desc ) {
221 switch ( desc->devtype ) {
222 case fpga_xilinx:
223#if defined(CONFIG_FPGA_XILINX)
224 ret_val = xilinx_load( desc->devdesc, buf, bsize );
225#else
226 fpga_no_sup( (char *)__FUNCTION__, "Xilinx devices" );
227#endif
228 break;
229 case fpga_altera:
230#if defined(CONFIG_FPGA_ALTERA)
231 ret_val = altera_load( desc->devdesc, buf, bsize );
232#else
233 fpga_no_sup( (char *)__FUNCTION__, "Altera devices" );
234#endif
235 break;
236 case fpga_lattice:
237#if defined(CONFIG_FPGA_LATTICE)
238 ret_val = lattice_load(desc->devdesc, buf, bsize);
239#else
240 fpga_no_sup( (char *)__FUNCTION__, "Lattice devices" );
241#endif
242 break;
243 default:
244 printf( "%s: Invalid or unsupported device type %d\n",
245 __FUNCTION__, desc->devtype );
246 }
247 }
248
249 return ret_val;
250}
251
252
253
254
255int fpga_dump(int devnum, const void *buf, size_t bsize)
256{
257 int ret_val = FPGA_FAIL;
258 fpga_desc * desc = fpga_validate( devnum, buf, bsize, (char *)__FUNCTION__ );
259
260 if ( desc ) {
261 switch ( desc->devtype ) {
262 case fpga_xilinx:
263#if defined(CONFIG_FPGA_XILINX)
264 ret_val = xilinx_dump( desc->devdesc, buf, bsize );
265#else
266 fpga_no_sup( (char *)__FUNCTION__, "Xilinx devices" );
267#endif
268 break;
269 case fpga_altera:
270#if defined(CONFIG_FPGA_ALTERA)
271 ret_val = altera_dump( desc->devdesc, buf, bsize );
272#else
273 fpga_no_sup( (char *)__FUNCTION__, "Altera devices" );
274#endif
275 break;
276 case fpga_lattice:
277#if defined(CONFIG_FPGA_LATTICE)
278 ret_val = lattice_dump(desc->devdesc, buf, bsize);
279#else
280 fpga_no_sup( (char *)__FUNCTION__, "Lattice devices" );
281#endif
282 break;
283 default:
284 printf( "%s: Invalid or unsupported device type %d\n",
285 __FUNCTION__, desc->devtype );
286 }
287 }
288
289 return ret_val;
290}
291
292
293
294
295
296
297int fpga_info( int devnum )
298{
299 if ( devnum == FPGA_INVALID_DEVICE ) {
300 if ( next_desc > 0 ) {
301 int dev;
302
303 for ( dev = 0; dev < next_desc; dev++ ) {
304 fpga_dev_info( dev );
305 }
306 return FPGA_SUCCESS;
307 } else {
308 printf( "%s: No FPGA devices available.\n", __FUNCTION__ );
309 return FPGA_FAIL;
310 }
311 }
312 else return fpga_dev_info( devnum );
313}
314
315
316