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