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#include <common.h>
26#include <spartan2.h>
27
28
29#ifdef FPGA_DEBUG
30#define PRINTF(fmt,args...) printf (fmt ,##args)
31#else
32#define PRINTF(fmt,args...)
33#endif
34
35#undef CONFIG_SYS_FPGA_CHECK_BUSY
36#undef CONFIG_SYS_FPGA_PROG_FEEDBACK
37
38
39
40
41
42
43#ifndef CONFIG_FPGA_DELAY
44#define CONFIG_FPGA_DELAY()
45#endif
46
47#ifndef CONFIG_SYS_FPGA_WAIT
48#define CONFIG_SYS_FPGA_WAIT CONFIG_SYS_HZ/100
49#endif
50
51static int Spartan2_sp_load(Xilinx_desc *desc, const void *buf, size_t bsize);
52static int Spartan2_sp_dump(Xilinx_desc *desc, const void *buf, size_t bsize);
53
54
55static int Spartan2_ss_load(Xilinx_desc *desc, const void *buf, size_t bsize);
56static int Spartan2_ss_dump(Xilinx_desc *desc, const void *buf, size_t bsize);
57
58
59
60
61int Spartan2_load(Xilinx_desc *desc, const void *buf, size_t bsize)
62{
63 int ret_val = FPGA_FAIL;
64
65 switch (desc->iface) {
66 case slave_serial:
67 PRINTF ("%s: Launching Slave Serial Load\n", __FUNCTION__);
68 ret_val = Spartan2_ss_load (desc, buf, bsize);
69 break;
70
71 case slave_parallel:
72 PRINTF ("%s: Launching Slave Parallel Load\n", __FUNCTION__);
73 ret_val = Spartan2_sp_load (desc, buf, bsize);
74 break;
75
76 default:
77 printf ("%s: Unsupported interface type, %d\n",
78 __FUNCTION__, desc->iface);
79 }
80
81 return ret_val;
82}
83
84int Spartan2_dump(Xilinx_desc *desc, const void *buf, size_t bsize)
85{
86 int ret_val = FPGA_FAIL;
87
88 switch (desc->iface) {
89 case slave_serial:
90 PRINTF ("%s: Launching Slave Serial Dump\n", __FUNCTION__);
91 ret_val = Spartan2_ss_dump (desc, buf, bsize);
92 break;
93
94 case slave_parallel:
95 PRINTF ("%s: Launching Slave Parallel Dump\n", __FUNCTION__);
96 ret_val = Spartan2_sp_dump (desc, buf, bsize);
97 break;
98
99 default:
100 printf ("%s: Unsupported interface type, %d\n",
101 __FUNCTION__, desc->iface);
102 }
103
104 return ret_val;
105}
106
107int Spartan2_info( Xilinx_desc *desc )
108{
109 return FPGA_SUCCESS;
110}
111
112
113
114
115
116static int Spartan2_sp_load(Xilinx_desc *desc, const void *buf, size_t bsize)
117{
118 int ret_val = FPGA_FAIL;
119 Xilinx_Spartan2_Slave_Parallel_fns *fn = desc->iface_fns;
120
121 PRINTF ("%s: start with interface functions @ 0x%p\n",
122 __FUNCTION__, fn);
123
124 if (fn) {
125 size_t bytecount = 0;
126 unsigned char *data = (unsigned char *) buf;
127 int cookie = desc->cookie;
128 unsigned long ts;
129
130 PRINTF ("%s: Function Table:\n"
131 "ptr:\t0x%p\n"
132 "struct: 0x%p\n"
133 "pre: 0x%p\n"
134 "pgm:\t0x%p\n"
135 "init:\t0x%p\n"
136 "err:\t0x%p\n"
137 "clk:\t0x%p\n"
138 "cs:\t0x%p\n"
139 "wr:\t0x%p\n"
140 "read data:\t0x%p\n"
141 "write data:\t0x%p\n"
142 "busy:\t0x%p\n"
143 "abort:\t0x%p\n",
144 "post:\t0x%p\n\n",
145 __FUNCTION__, &fn, fn, fn->pre, fn->pgm, fn->init, fn->err,
146 fn->clk, fn->cs, fn->wr, fn->rdata, fn->wdata, fn->busy,
147 fn->abort, fn->post);
148
149
150
151
152
153
154#ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
155 printf ("Loading FPGA Device %d...\n", cookie);
156#endif
157
158
159
160 if (*fn->pre) {
161 (*fn->pre) (cookie);
162 }
163
164
165 (*fn->pgm) (TRUE, TRUE, cookie);
166
167
168 CONFIG_FPGA_DELAY ();
169 (*fn->pgm) (FALSE, TRUE, cookie);
170
171 ts = get_timer (0);
172
173 do {
174 CONFIG_FPGA_DELAY ();
175 if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT) {
176 puts ("** Timeout waiting for INIT to clear.\n");
177 (*fn->abort) (cookie);
178 return FPGA_FAIL;
179 }
180 } while ((*fn->init) (cookie) && (*fn->busy) (cookie));
181
182 (*fn->wr) (TRUE, TRUE, cookie);
183 (*fn->cs) (TRUE, TRUE, cookie);
184 (*fn->clk) (TRUE, TRUE, cookie);
185
186
187 while (bytecount < bsize) {
188
189
190
191 (*fn->wdata) (data[bytecount++], TRUE, cookie);
192 CONFIG_FPGA_DELAY ();
193 (*fn->clk) (FALSE, TRUE, cookie);
194 CONFIG_FPGA_DELAY ();
195 (*fn->clk) (TRUE, TRUE, cookie);
196
197#ifdef CONFIG_SYS_FPGA_CHECK_BUSY
198 ts = get_timer (0);
199 while ((*fn->busy) (cookie)) {
200
201
202
203 CONFIG_FPGA_DELAY ();
204 (*fn->clk) (FALSE, TRUE, cookie);
205 CONFIG_FPGA_DELAY ();
206 (*fn->clk) (TRUE, TRUE, cookie);
207
208 if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT) {
209 puts ("** Timeout waiting for BUSY to clear.\n");
210 (*fn->abort) (cookie);
211 return FPGA_FAIL;
212 }
213 }
214#endif
215
216#ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
217 if (bytecount % (bsize / 40) == 0)
218 putc ('.');
219#endif
220 }
221
222 CONFIG_FPGA_DELAY ();
223 (*fn->cs) (FALSE, TRUE, cookie);
224 (*fn->wr) (FALSE, TRUE, cookie);
225
226#ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
227 putc ('\n');
228#endif
229
230
231 ts = get_timer (0);
232 ret_val = FPGA_SUCCESS;
233 while ((*fn->done) (cookie) == FPGA_FAIL) {
234
235 CONFIG_FPGA_DELAY ();
236 (*fn->clk) (FALSE, TRUE, cookie);
237 CONFIG_FPGA_DELAY ();
238 (*fn->clk) (TRUE, TRUE, cookie);
239
240 if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT) {
241 puts ("** Timeout waiting for DONE to clear.\n");
242 (*fn->abort) (cookie);
243 ret_val = FPGA_FAIL;
244 break;
245 }
246 }
247
248
249
250
251 if (*fn->post)
252 (*fn->post) (cookie);
253
254#ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
255 if (ret_val == FPGA_SUCCESS)
256 puts ("Done.\n");
257 else
258 puts ("Fail.\n");
259#endif
260
261 } else {
262 printf ("%s: NULL Interface function table!\n", __FUNCTION__);
263 }
264
265 return ret_val;
266}
267
268static int Spartan2_sp_dump(Xilinx_desc *desc, const void *buf, size_t bsize)
269{
270 int ret_val = FPGA_FAIL;
271 Xilinx_Spartan2_Slave_Parallel_fns *fn = desc->iface_fns;
272
273 if (fn) {
274 unsigned char *data = (unsigned char *) buf;
275 size_t bytecount = 0;
276 int cookie = desc->cookie;
277
278 printf ("Starting Dump of FPGA Device %d...\n", cookie);
279
280 (*fn->cs) (TRUE, TRUE, cookie);
281 (*fn->clk) (TRUE, TRUE, cookie);
282
283
284 while (bytecount < bsize) {
285
286
287 (*fn->clk) (FALSE, TRUE, cookie);
288 (*fn->clk) (TRUE, TRUE, cookie);
289 (*fn->rdata) (&(data[bytecount++]), cookie);
290#ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
291 if (bytecount % (bsize / 40) == 0)
292 putc ('.');
293#endif
294 }
295
296 (*fn->cs) (FALSE, FALSE, cookie);
297 (*fn->clk) (FALSE, TRUE, cookie);
298 (*fn->clk) (TRUE, TRUE, cookie);
299
300#ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
301 putc ('\n');
302#endif
303 puts ("Done.\n");
304
305
306 } else {
307 printf ("%s: NULL Interface function table!\n", __FUNCTION__);
308 }
309
310 return ret_val;
311}
312
313
314
315
316static int Spartan2_ss_load(Xilinx_desc *desc, const void *buf, size_t bsize)
317{
318 int ret_val = FPGA_FAIL;
319 Xilinx_Spartan2_Slave_Serial_fns *fn = desc->iface_fns;
320 int i;
321 unsigned char val;
322
323 PRINTF ("%s: start with interface functions @ 0x%p\n",
324 __FUNCTION__, fn);
325
326 if (fn) {
327 size_t bytecount = 0;
328 unsigned char *data = (unsigned char *) buf;
329 int cookie = desc->cookie;
330 unsigned long ts;
331
332 PRINTF ("%s: Function Table:\n"
333 "ptr:\t0x%p\n"
334 "struct: 0x%p\n"
335 "pgm:\t0x%p\n"
336 "init:\t0x%p\n"
337 "clk:\t0x%p\n"
338 "wr:\t0x%p\n"
339 "done:\t0x%p\n\n",
340 __FUNCTION__, &fn, fn, fn->pgm, fn->init,
341 fn->clk, fn->wr, fn->done);
342#ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
343 printf ("Loading FPGA Device %d...\n", cookie);
344#endif
345
346
347
348
349 if (*fn->pre) {
350 (*fn->pre) (cookie);
351 }
352
353
354 (*fn->pgm) (TRUE, TRUE, cookie);
355
356
357 ts = get_timer (0);
358 do {
359 CONFIG_FPGA_DELAY ();
360 if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT) {
361 puts ("** Timeout waiting for INIT to start.\n");
362 return FPGA_FAIL;
363 }
364 } while (!(*fn->init) (cookie));
365
366
367 CONFIG_FPGA_DELAY ();
368 (*fn->pgm) (FALSE, TRUE, cookie);
369
370 ts = get_timer (0);
371
372 do {
373 CONFIG_FPGA_DELAY ();
374 if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT) {
375 puts ("** Timeout waiting for INIT to clear.\n");
376 return FPGA_FAIL;
377 }
378 } while ((*fn->init) (cookie));
379
380
381 while (bytecount < bsize) {
382
383
384
385 if ((*fn->done) (cookie) == 0 && (*fn->init) (cookie)) {
386 puts ("** CRC error during FPGA load.\n");
387 return (FPGA_FAIL);
388 }
389 val = data [bytecount ++];
390 i = 8;
391 do {
392
393 (*fn->clk) (FALSE, TRUE, cookie);
394 CONFIG_FPGA_DELAY ();
395
396 (*fn->wr) ((val & 0x80), TRUE, cookie);
397 CONFIG_FPGA_DELAY ();
398
399 (*fn->clk) (TRUE, TRUE, cookie);
400 CONFIG_FPGA_DELAY ();
401 val <<= 1;
402 i --;
403 } while (i > 0);
404
405#ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
406 if (bytecount % (bsize / 40) == 0)
407 putc ('.');
408#endif
409 }
410
411 CONFIG_FPGA_DELAY ();
412
413#ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
414 putc ('\n');
415#endif
416
417
418 ts = get_timer (0);
419 ret_val = FPGA_SUCCESS;
420 (*fn->wr) (TRUE, TRUE, cookie);
421
422 while (! (*fn->done) (cookie)) {
423
424 CONFIG_FPGA_DELAY ();
425 (*fn->clk) (FALSE, TRUE, cookie);
426 CONFIG_FPGA_DELAY ();
427 (*fn->clk) (TRUE, TRUE, cookie);
428
429 putc ('*');
430
431 if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT) {
432 puts ("** Timeout waiting for DONE to clear.\n");
433 ret_val = FPGA_FAIL;
434 break;
435 }
436 }
437 putc ('\n');
438
439
440
441
442 if (*fn->post)
443 (*fn->post) (cookie);
444
445#ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
446 if (ret_val == FPGA_SUCCESS)
447 puts ("Done.\n");
448 else
449 puts ("Fail.\n");
450#endif
451
452 } else {
453 printf ("%s: NULL Interface function table!\n", __FUNCTION__);
454 }
455
456 return ret_val;
457}
458
459static int Spartan2_ss_dump(Xilinx_desc *desc, const void *buf, size_t bsize)
460{
461
462
463 printf ("%s: Slave Serial Dumping is unavailable\n",
464 __FUNCTION__);
465 return FPGA_FAIL;
466}
467