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
32#if 0
33#define FPGA_DEBUG
34#endif
35
36
37#ifndef CONFIG_MAX_FPGA_DEVICES
38#define CONFIG_MAX_FPGA_DEVICES 5
39#endif
40
41
42#ifdef FPGA_DEBUG
43#define PRINTF(fmt,args...) printf (fmt ,##args)
44#else
45#define PRINTF(fmt,args...)
46#endif
47
48
49static int next_desc = FPGA_INVALID_DEVICE;
50static fpga_desc desc_table[CONFIG_MAX_FPGA_DEVICES];
51
52
53static __attribute__((__const__)) fpga_desc * __attribute__((__const__)) fpga_get_desc( int devnum );
54static __attribute__((__const__)) fpga_desc * __attribute__((__const__)) fpga_validate( int devnum, void *buf,
55 size_t bsize, char *fn );
56static int fpga_dev_info( int devnum );
57
58
59
60
61
62
63
64static void fpga_no_sup( char *fn, char *msg )
65{
66 if ( fn && msg ) {
67 printf( "%s: No support for %s.\n", fn, msg);
68 } else if ( msg ) {
69 printf( "No support for %s.\n", msg);
70 } else {
71 printf( "No FPGA suport!\n");
72 }
73}
74
75
76
77
78
79static __attribute__((__const__)) fpga_desc * __attribute__((__const__)) fpga_get_desc( int devnum )
80{
81 fpga_desc *desc = (fpga_desc * )NULL;
82
83 if (( devnum >= 0 ) && (devnum < next_desc )) {
84 desc = &desc_table[devnum];
85 PRINTF( "%s: found fpga descriptor #%d @ 0x%p\n",
86 __FUNCTION__, devnum, desc );
87 }
88
89 return desc;
90}
91
92
93
94
95
96static __attribute__((__const__)) fpga_desc * __attribute__((__const__)) fpga_validate( int devnum, void *buf,
97 size_t bsize, char *fn )
98{
99 fpga_desc * desc = fpga_get_desc( devnum );
100
101 if ( !desc ) {
102 printf( "%s: Invalid device number %d\n", fn, devnum );
103 }
104
105 if ( !buf ) {
106 printf( "%s: Null buffer.\n", fn );
107 return (fpga_desc * const)NULL;
108 }
109 return desc;
110}
111
112
113
114
115
116static int fpga_dev_info( int devnum )
117{
118 int ret_val = FPGA_FAIL;
119 const fpga_desc * const desc = fpga_get_desc( devnum );
120
121 if ( desc ) {
122 PRINTF( "%s: Device Descriptor @ 0x%p\n",
123 __FUNCTION__, desc->devdesc );
124
125 switch ( desc->devtype ) {
126 case fpga_xilinx:
127#if defined(CONFIG_FPGA_XILINX)
128 printf( "Xilinx Device\nDescriptor @ 0x%p\n", desc );
129 ret_val = xilinx_info( desc->devdesc );
130#else
131 fpga_no_sup( (char *)__FUNCTION__, "Xilinx devices" );
132#endif
133 break;
134 case fpga_altera:
135#if defined(CONFIG_FPGA_ALTERA)
136 printf( "Altera Device\nDescriptor @ 0x%p\n", desc );
137 ret_val = altera_info( desc->devdesc );
138#else
139 fpga_no_sup( (char *)__FUNCTION__, "Altera devices" );
140#endif
141 break;
142 default:
143 printf( "%s: Invalid or unsupported device type %d\n",
144 __FUNCTION__, desc->devtype );
145 }
146 } else {
147 printf( "%s: Invalid device number %d\n",
148 __FUNCTION__, devnum );
149 }
150
151 return ret_val;
152}
153
154
155
156
157
158
159void fpga_init(void)
160{
161 next_desc = 0;
162 memset( desc_table, 0, sizeof(desc_table));
163
164 PRINTF( "%s: CONFIG_FPGA = 0x%x\n", __FUNCTION__, CONFIG_FPGA );
165}
166
167
168
169
170int fpga_count( void )
171{
172 return next_desc;
173}
174
175
176
177
178int fpga_add( fpga_type devtype, void *desc )
179{
180 int devnum = FPGA_INVALID_DEVICE;
181
182 if ( next_desc < 0 ) {
183 printf( "%s: FPGA support not initialized!\n", __FUNCTION__ );
184 } else if (( devtype > fpga_min_type ) && ( devtype < fpga_undefined )) {
185 if ( desc ) {
186 if ( next_desc < CONFIG_MAX_FPGA_DEVICES ) {
187 devnum = next_desc;
188 desc_table[next_desc].devtype = devtype;
189 desc_table[next_desc++].devdesc = desc;
190 } else {
191 printf( "%s: Exceeded Max FPGA device count\n", __FUNCTION__ );
192 }
193 } else {
194 printf( "%s: NULL device descriptor\n", __FUNCTION__ );
195 }
196 } else {
197 printf( "%s: Unsupported FPGA type %d\n", __FUNCTION__, devtype );
198 }
199
200 return devnum;
201}
202
203
204
205
206int fpga_load( int devnum, void *buf, size_t bsize )
207{
208 int ret_val = FPGA_FAIL;
209 fpga_desc * desc = fpga_validate( devnum, buf, bsize, (char *)__FUNCTION__ );
210
211 if ( desc ) {
212 switch ( desc->devtype ) {
213 case fpga_xilinx:
214#if defined(CONFIG_FPGA_XILINX)
215 ret_val = xilinx_load( desc->devdesc, buf, bsize );
216#else
217 fpga_no_sup( (char *)__FUNCTION__, "Xilinx devices" );
218#endif
219 break;
220 case fpga_altera:
221#if defined(CONFIG_FPGA_ALTERA)
222 ret_val = altera_load( desc->devdesc, buf, bsize );
223#else
224 fpga_no_sup( (char *)__FUNCTION__, "Altera devices" );
225#endif
226 break;
227 default:
228 printf( "%s: Invalid or unsupported device type %d\n",
229 __FUNCTION__, desc->devtype );
230 }
231 }
232
233 return ret_val;
234}
235
236
237
238
239int fpga_dump( int devnum, void *buf, size_t bsize )
240{
241 int ret_val = FPGA_FAIL;
242 fpga_desc * desc = fpga_validate( devnum, buf, bsize, (char *)__FUNCTION__ );
243
244 if ( desc ) {
245 switch ( desc->devtype ) {
246 case fpga_xilinx:
247#if defined(CONFIG_FPGA_XILINX)
248 ret_val = xilinx_dump( desc->devdesc, buf, bsize );
249#else
250 fpga_no_sup( (char *)__FUNCTION__, "Xilinx devices" );
251#endif
252 break;
253 case fpga_altera:
254#if defined(CONFIG_FPGA_ALTERA)
255 ret_val = altera_dump( desc->devdesc, buf, bsize );
256#else
257 fpga_no_sup( (char *)__FUNCTION__, "Altera devices" );
258#endif
259 break;
260 default:
261 printf( "%s: Invalid or unsupported device type %d\n",
262 __FUNCTION__, desc->devtype );
263 }
264 }
265
266 return ret_val;
267}
268
269
270
271
272
273
274int fpga_info( int devnum )
275{
276 if ( devnum == FPGA_INVALID_DEVICE ) {
277 if ( next_desc > 0 ) {
278 int dev;
279
280 for ( dev = 0; dev < next_desc; dev++ ) {
281 fpga_dev_info( dev );
282 }
283 return FPGA_SUCCESS;
284 } else {
285 printf( "%s: No FPGA devices available.\n", __FUNCTION__ );
286 return FPGA_FAIL;
287 }
288 }
289 else return fpga_dev_info( devnum );
290}
291
292
293