Fixed tools/env utilities
[u-boot-openmoko/mini2440.git] / common / spartan3.c
blobd329e70cf736a5550cd7a1ee50b6aa0b52c14f90
1 /*
2 * (C) Copyright 2002
3 * Rich Ireland, Enterasys Networks, rireland@enterasys.com.
5 * See file CREDITS for list of people who contributed to this
6 * project.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
26 * Configuration support for Xilinx Spartan3 devices. Based
27 * on spartan2.c (Rich Ireland, rireland@enterasys.com).
30 #include <common.h> /* core U-Boot definitions */
31 #include <spartan3.h> /* Spartan-II device family */
33 #if defined(CONFIG_FPGA) && defined(CONFIG_FPGA_SPARTAN3)
35 /* Define FPGA_DEBUG to get debug printf's */
36 #ifdef FPGA_DEBUG
37 #define PRINTF(fmt,args...) printf (fmt ,##args)
38 #else
39 #define PRINTF(fmt,args...)
40 #endif
42 #undef CFG_FPGA_CHECK_BUSY
43 #undef CFG_FPGA_PROG_FEEDBACK
45 /* Note: The assumption is that we cannot possibly run fast enough to
46 * overrun the device (the Slave Parallel mode can free run at 50MHz).
47 * If there is a need to operate slower, define CONFIG_FPGA_DELAY in
48 * the board config file to slow things down.
50 #ifndef CONFIG_FPGA_DELAY
51 #define CONFIG_FPGA_DELAY()
52 #endif
54 #ifndef CFG_FPGA_WAIT
55 #define CFG_FPGA_WAIT CFG_HZ/100 /* 10 ms */
56 #endif
58 static int Spartan3_sp_load( Xilinx_desc *desc, void *buf, size_t bsize );
59 static int Spartan3_sp_dump( Xilinx_desc *desc, void *buf, size_t bsize );
60 /* static int Spartan3_sp_info( Xilinx_desc *desc ); */
61 static int Spartan3_sp_reloc( Xilinx_desc *desc, ulong reloc_offset );
63 static int Spartan3_ss_load( Xilinx_desc *desc, void *buf, size_t bsize );
64 static int Spartan3_ss_dump( Xilinx_desc *desc, void *buf, size_t bsize );
65 /* static int Spartan3_ss_info( Xilinx_desc *desc ); */
66 static int Spartan3_ss_reloc( Xilinx_desc *desc, ulong reloc_offset );
68 /* ------------------------------------------------------------------------- */
69 /* Spartan-II Generic Implementation */
70 int Spartan3_load (Xilinx_desc * desc, void *buf, size_t bsize)
72 int ret_val = FPGA_FAIL;
74 switch (desc->iface) {
75 case slave_serial:
76 PRINTF ("%s: Launching Slave Serial Load\n", __FUNCTION__);
77 ret_val = Spartan3_ss_load (desc, buf, bsize);
78 break;
80 case slave_parallel:
81 PRINTF ("%s: Launching Slave Parallel Load\n", __FUNCTION__);
82 ret_val = Spartan3_sp_load (desc, buf, bsize);
83 break;
85 default:
86 printf ("%s: Unsupported interface type, %d\n",
87 __FUNCTION__, desc->iface);
90 return ret_val;
93 int Spartan3_dump (Xilinx_desc * desc, void *buf, size_t bsize)
95 int ret_val = FPGA_FAIL;
97 switch (desc->iface) {
98 case slave_serial:
99 PRINTF ("%s: Launching Slave Serial Dump\n", __FUNCTION__);
100 ret_val = Spartan3_ss_dump (desc, buf, bsize);
101 break;
103 case slave_parallel:
104 PRINTF ("%s: Launching Slave Parallel Dump\n", __FUNCTION__);
105 ret_val = Spartan3_sp_dump (desc, buf, bsize);
106 break;
108 default:
109 printf ("%s: Unsupported interface type, %d\n",
110 __FUNCTION__, desc->iface);
113 return ret_val;
116 int Spartan3_info( Xilinx_desc *desc )
118 return FPGA_SUCCESS;
122 int Spartan3_reloc (Xilinx_desc * desc, ulong reloc_offset)
124 int ret_val = FPGA_FAIL; /* assume a failure */
126 if (desc->family != Xilinx_Spartan3) {
127 printf ("%s: Unsupported family type, %d\n",
128 __FUNCTION__, desc->family);
129 return FPGA_FAIL;
130 } else
131 switch (desc->iface) {
132 case slave_serial:
133 ret_val = Spartan3_ss_reloc (desc, reloc_offset);
134 break;
136 case slave_parallel:
137 ret_val = Spartan3_sp_reloc (desc, reloc_offset);
138 break;
140 default:
141 printf ("%s: Unsupported interface type, %d\n",
142 __FUNCTION__, desc->iface);
145 return ret_val;
149 /* ------------------------------------------------------------------------- */
150 /* Spartan-II Slave Parallel Generic Implementation */
152 static int Spartan3_sp_load (Xilinx_desc * desc, void *buf, size_t bsize)
154 int ret_val = FPGA_FAIL; /* assume the worst */
155 Xilinx_Spartan3_Slave_Parallel_fns *fn = desc->iface_fns;
157 PRINTF ("%s: start with interface functions @ 0x%p\n",
158 __FUNCTION__, fn);
160 if (fn) {
161 size_t bytecount = 0;
162 unsigned char *data = (unsigned char *) buf;
163 int cookie = desc->cookie; /* make a local copy */
164 unsigned long ts; /* timestamp */
166 PRINTF ("%s: Function Table:\n"
167 "ptr:\t0x%p\n"
168 "struct: 0x%p\n"
169 "pre: 0x%p\n"
170 "pgm:\t0x%p\n"
171 "init:\t0x%p\n"
172 "err:\t0x%p\n"
173 "clk:\t0x%p\n"
174 "cs:\t0x%p\n"
175 "wr:\t0x%p\n"
176 "read data:\t0x%p\n"
177 "write data:\t0x%p\n"
178 "busy:\t0x%p\n"
179 "abort:\t0x%p\n",
180 "post:\t0x%p\n\n",
181 __FUNCTION__, &fn, fn, fn->pre, fn->pgm, fn->init, fn->err,
182 fn->clk, fn->cs, fn->wr, fn->rdata, fn->wdata, fn->busy,
183 fn->abort, fn->post);
186 * This code is designed to emulate the "Express Style"
187 * Continuous Data Loading in Slave Parallel Mode for
188 * the Spartan-II Family.
190 #ifdef CFG_FPGA_PROG_FEEDBACK
191 printf ("Loading FPGA Device %d...\n", cookie);
192 #endif
194 * Run the pre configuration function if there is one.
196 if (*fn->pre) {
197 (*fn->pre) (cookie);
200 /* Establish the initial state */
201 (*fn->pgm) (TRUE, TRUE, cookie); /* Assert the program, commit */
203 /* Get ready for the burn */
204 CONFIG_FPGA_DELAY ();
205 (*fn->pgm) (FALSE, TRUE, cookie); /* Deassert the program, commit */
207 ts = get_timer (0); /* get current time */
208 /* Now wait for INIT and BUSY to go high */
209 do {
210 CONFIG_FPGA_DELAY ();
211 if (get_timer (ts) > CFG_FPGA_WAIT) { /* check the time */
212 puts ("** Timeout waiting for INIT to clear.\n");
213 (*fn->abort) (cookie); /* abort the burn */
214 return FPGA_FAIL;
216 } while ((*fn->init) (cookie) && (*fn->busy) (cookie));
218 (*fn->wr) (TRUE, TRUE, cookie); /* Assert write, commit */
219 (*fn->cs) (TRUE, TRUE, cookie); /* Assert chip select, commit */
220 (*fn->clk) (TRUE, TRUE, cookie); /* Assert the clock pin */
222 /* Load the data */
223 while (bytecount < bsize) {
224 /* XXX - do we check for an Ctrl-C press in here ??? */
225 /* XXX - Check the error bit? */
227 (*fn->wdata) (data[bytecount++], TRUE, cookie); /* write the data */
228 CONFIG_FPGA_DELAY ();
229 (*fn->clk) (FALSE, TRUE, cookie); /* Deassert the clock pin */
230 CONFIG_FPGA_DELAY ();
231 (*fn->clk) (TRUE, TRUE, cookie); /* Assert the clock pin */
233 #ifdef CFG_FPGA_CHECK_BUSY
234 ts = get_timer (0); /* get current time */
235 while ((*fn->busy) (cookie)) {
236 /* XXX - we should have a check in here somewhere to
237 * make sure we aren't busy forever... */
239 CONFIG_FPGA_DELAY ();
240 (*fn->clk) (FALSE, TRUE, cookie); /* Deassert the clock pin */
241 CONFIG_FPGA_DELAY ();
242 (*fn->clk) (TRUE, TRUE, cookie); /* Assert the clock pin */
244 if (get_timer (ts) > CFG_FPGA_WAIT) { /* check the time */
245 puts ("** Timeout waiting for BUSY to clear.\n");
246 (*fn->abort) (cookie); /* abort the burn */
247 return FPGA_FAIL;
250 #endif
252 #ifdef CFG_FPGA_PROG_FEEDBACK
253 if (bytecount % (bsize / 40) == 0)
254 putc ('.'); /* let them know we are alive */
255 #endif
258 CONFIG_FPGA_DELAY ();
259 (*fn->cs) (FALSE, TRUE, cookie); /* Deassert the chip select */
260 (*fn->wr) (FALSE, TRUE, cookie); /* Deassert the write pin */
262 #ifdef CFG_FPGA_PROG_FEEDBACK
263 putc ('\n'); /* terminate the dotted line */
264 #endif
266 /* now check for done signal */
267 ts = get_timer (0); /* get current time */
268 ret_val = FPGA_SUCCESS;
269 while ((*fn->done) (cookie) == FPGA_FAIL) {
270 /* XXX - we should have a check in here somewhere to
271 * make sure we aren't busy forever... */
273 CONFIG_FPGA_DELAY ();
274 (*fn->clk) (FALSE, TRUE, cookie); /* Deassert the clock pin */
275 CONFIG_FPGA_DELAY ();
276 (*fn->clk) (TRUE, TRUE, cookie); /* Assert the clock pin */
278 if (get_timer (ts) > CFG_FPGA_WAIT) { /* check the time */
279 puts ("** Timeout waiting for DONE to clear.\n");
280 (*fn->abort) (cookie); /* abort the burn */
281 ret_val = FPGA_FAIL;
282 break;
286 if (ret_val == FPGA_SUCCESS) {
287 #ifdef CFG_FPGA_PROG_FEEDBACK
288 puts ("Done.\n");
289 #endif
292 * Run the post configuration function if there is one.
294 if (*fn->post) {
295 (*fn->post) (cookie);
298 else {
299 #ifdef CFG_FPGA_PROG_FEEDBACK
300 puts ("Fail.\n");
301 #endif
304 } else {
305 printf ("%s: NULL Interface function table!\n", __FUNCTION__);
308 return ret_val;
311 static int Spartan3_sp_dump (Xilinx_desc * desc, void *buf, size_t bsize)
313 int ret_val = FPGA_FAIL; /* assume the worst */
314 Xilinx_Spartan3_Slave_Parallel_fns *fn = desc->iface_fns;
316 if (fn) {
317 unsigned char *data = (unsigned char *) buf;
318 size_t bytecount = 0;
319 int cookie = desc->cookie; /* make a local copy */
321 printf ("Starting Dump of FPGA Device %d...\n", cookie);
323 (*fn->cs) (TRUE, TRUE, cookie); /* Assert chip select, commit */
324 (*fn->clk) (TRUE, TRUE, cookie); /* Assert the clock pin */
326 /* dump the data */
327 while (bytecount < bsize) {
328 /* XXX - do we check for an Ctrl-C press in here ??? */
330 (*fn->clk) (FALSE, TRUE, cookie); /* Deassert the clock pin */
331 (*fn->clk) (TRUE, TRUE, cookie); /* Assert the clock pin */
332 (*fn->rdata) (&(data[bytecount++]), cookie); /* read the data */
333 #ifdef CFG_FPGA_PROG_FEEDBACK
334 if (bytecount % (bsize / 40) == 0)
335 putc ('.'); /* let them know we are alive */
336 #endif
339 (*fn->cs) (FALSE, FALSE, cookie); /* Deassert the chip select */
340 (*fn->clk) (FALSE, TRUE, cookie); /* Deassert the clock pin */
341 (*fn->clk) (TRUE, TRUE, cookie); /* Assert the clock pin */
343 #ifdef CFG_FPGA_PROG_FEEDBACK
344 putc ('\n'); /* terminate the dotted line */
345 #endif
346 puts ("Done.\n");
348 /* XXX - checksum the data? */
349 } else {
350 printf ("%s: NULL Interface function table!\n", __FUNCTION__);
353 return ret_val;
357 static int Spartan3_sp_reloc (Xilinx_desc * desc, ulong reloc_offset)
359 int ret_val = FPGA_FAIL; /* assume the worst */
360 Xilinx_Spartan3_Slave_Parallel_fns *fn_r, *fn =
361 (Xilinx_Spartan3_Slave_Parallel_fns *) (desc->iface_fns);
363 if (fn) {
364 ulong addr;
366 /* Get the relocated table address */
367 addr = (ulong) fn + reloc_offset;
368 fn_r = (Xilinx_Spartan3_Slave_Parallel_fns *) addr;
370 if (!fn_r->relocated) {
372 if (memcmp (fn_r, fn,
373 sizeof (Xilinx_Spartan3_Slave_Parallel_fns))
374 == 0) {
375 /* good copy of the table, fix the descriptor pointer */
376 desc->iface_fns = fn_r;
377 } else {
378 PRINTF ("%s: Invalid function table at 0x%p\n",
379 __FUNCTION__, fn_r);
380 return FPGA_FAIL;
383 PRINTF ("%s: Relocating descriptor at 0x%p\n", __FUNCTION__,
384 desc);
386 addr = (ulong) (fn->pre) + reloc_offset;
387 fn_r->pre = (Xilinx_pre_fn) addr;
389 addr = (ulong) (fn->pgm) + reloc_offset;
390 fn_r->pgm = (Xilinx_pgm_fn) addr;
392 addr = (ulong) (fn->init) + reloc_offset;
393 fn_r->init = (Xilinx_init_fn) addr;
395 addr = (ulong) (fn->done) + reloc_offset;
396 fn_r->done = (Xilinx_done_fn) addr;
398 addr = (ulong) (fn->clk) + reloc_offset;
399 fn_r->clk = (Xilinx_clk_fn) addr;
401 addr = (ulong) (fn->err) + reloc_offset;
402 fn_r->err = (Xilinx_err_fn) addr;
404 addr = (ulong) (fn->cs) + reloc_offset;
405 fn_r->cs = (Xilinx_cs_fn) addr;
407 addr = (ulong) (fn->wr) + reloc_offset;
408 fn_r->wr = (Xilinx_wr_fn) addr;
410 addr = (ulong) (fn->rdata) + reloc_offset;
411 fn_r->rdata = (Xilinx_rdata_fn) addr;
413 addr = (ulong) (fn->wdata) + reloc_offset;
414 fn_r->wdata = (Xilinx_wdata_fn) addr;
416 addr = (ulong) (fn->busy) + reloc_offset;
417 fn_r->busy = (Xilinx_busy_fn) addr;
419 addr = (ulong) (fn->abort) + reloc_offset;
420 fn_r->abort = (Xilinx_abort_fn) addr;
422 addr = (ulong) (fn->post) + reloc_offset;
423 fn_r->post = (Xilinx_post_fn) addr;
425 fn_r->relocated = TRUE;
427 } else {
428 /* this table has already been moved */
429 /* XXX - should check to see if the descriptor is correct */
430 desc->iface_fns = fn_r;
433 ret_val = FPGA_SUCCESS;
434 } else {
435 printf ("%s: NULL Interface function table!\n", __FUNCTION__);
438 return ret_val;
442 /* ------------------------------------------------------------------------- */
444 static int Spartan3_ss_load (Xilinx_desc * desc, void *buf, size_t bsize)
446 int ret_val = FPGA_FAIL; /* assume the worst */
447 Xilinx_Spartan3_Slave_Serial_fns *fn = desc->iface_fns;
448 int i;
449 unsigned char val;
451 PRINTF ("%s: start with interface functions @ 0x%p\n",
452 __FUNCTION__, fn);
454 if (fn) {
455 size_t bytecount = 0;
456 unsigned char *data = (unsigned char *) buf;
457 int cookie = desc->cookie; /* make a local copy */
458 unsigned long ts; /* timestamp */
460 PRINTF ("%s: Function Table:\n"
461 "ptr:\t0x%p\n"
462 "struct: 0x%p\n"
463 "pgm:\t0x%p\n"
464 "init:\t0x%p\n"
465 "clk:\t0x%p\n"
466 "wr:\t0x%p\n"
467 "done:\t0x%p\n\n",
468 __FUNCTION__, &fn, fn, fn->pgm, fn->init,
469 fn->clk, fn->wr, fn->done);
470 #ifdef CFG_FPGA_PROG_FEEDBACK
471 printf ("Loading FPGA Device %d...\n", cookie);
472 #endif
475 * Run the pre configuration function if there is one.
477 if (*fn->pre) {
478 (*fn->pre) (cookie);
481 /* Establish the initial state */
482 (*fn->pgm) (TRUE, TRUE, cookie); /* Assert the program, commit */
484 /* Wait for INIT state (init low) */
485 ts = get_timer (0); /* get current time */
486 do {
487 CONFIG_FPGA_DELAY ();
488 if (get_timer (ts) > CFG_FPGA_WAIT) { /* check the time */
489 puts ("** Timeout waiting for INIT to start.\n");
490 return FPGA_FAIL;
492 } while (!(*fn->init) (cookie));
494 /* Get ready for the burn */
495 CONFIG_FPGA_DELAY ();
496 (*fn->pgm) (FALSE, TRUE, cookie); /* Deassert the program, commit */
498 ts = get_timer (0); /* get current time */
499 /* Now wait for INIT to go high */
500 do {
501 CONFIG_FPGA_DELAY ();
502 if (get_timer (ts) > CFG_FPGA_WAIT) { /* check the time */
503 puts ("** Timeout waiting for INIT to clear.\n");
504 return FPGA_FAIL;
506 } while ((*fn->init) (cookie));
508 /* Load the data */
509 while (bytecount < bsize) {
511 /* Xilinx detects an error if INIT goes low (active)
512 while DONE is low (inactive) */
513 if ((*fn->done) (cookie) == 0 && (*fn->init) (cookie)) {
514 puts ("** CRC error during FPGA load.\n");
515 return (FPGA_FAIL);
517 val = data [bytecount ++];
518 i = 8;
519 do {
520 /* Deassert the clock */
521 (*fn->clk) (FALSE, TRUE, cookie);
522 CONFIG_FPGA_DELAY ();
523 /* Write data */
524 (*fn->wr) ((val & 0x80), TRUE, cookie);
525 CONFIG_FPGA_DELAY ();
526 /* Assert the clock */
527 (*fn->clk) (TRUE, TRUE, cookie);
528 CONFIG_FPGA_DELAY ();
529 val <<= 1;
530 i --;
531 } while (i > 0);
533 #ifdef CFG_FPGA_PROG_FEEDBACK
534 if (bytecount % (bsize / 40) == 0)
535 putc ('.'); /* let them know we are alive */
536 #endif
539 CONFIG_FPGA_DELAY ();
541 #ifdef CFG_FPGA_PROG_FEEDBACK
542 putc ('\n'); /* terminate the dotted line */
543 #endif
545 /* now check for done signal */
546 ts = get_timer (0); /* get current time */
547 ret_val = FPGA_SUCCESS;
548 (*fn->wr) (TRUE, TRUE, cookie);
550 while (! (*fn->done) (cookie)) {
551 /* XXX - we should have a check in here somewhere to
552 * make sure we aren't busy forever... */
554 CONFIG_FPGA_DELAY ();
555 (*fn->clk) (FALSE, TRUE, cookie); /* Deassert the clock pin */
556 CONFIG_FPGA_DELAY ();
557 (*fn->clk) (TRUE, TRUE, cookie); /* Assert the clock pin */
559 putc ('*');
561 if (get_timer (ts) > CFG_FPGA_WAIT) { /* check the time */
562 puts ("** Timeout waiting for DONE to clear.\n");
563 ret_val = FPGA_FAIL;
564 break;
567 putc ('\n'); /* terminate the dotted line */
570 * Run the post configuration function if there is one.
572 if (*fn->post) {
573 (*fn->post) (cookie);
576 #ifdef CFG_FPGA_PROG_FEEDBACK
577 if (ret_val == FPGA_SUCCESS) {
578 puts ("Done.\n");
580 else {
581 puts ("Fail.\n");
583 #endif
585 } else {
586 printf ("%s: NULL Interface function table!\n", __FUNCTION__);
589 return ret_val;
592 static int Spartan3_ss_dump (Xilinx_desc * desc, void *buf, size_t bsize)
594 /* Readback is only available through the Slave Parallel and */
595 /* boundary-scan interfaces. */
596 printf ("%s: Slave Serial Dumping is unavailable\n",
597 __FUNCTION__);
598 return FPGA_FAIL;
601 static int Spartan3_ss_reloc (Xilinx_desc * desc, ulong reloc_offset)
603 int ret_val = FPGA_FAIL; /* assume the worst */
604 Xilinx_Spartan3_Slave_Serial_fns *fn_r, *fn =
605 (Xilinx_Spartan3_Slave_Serial_fns *) (desc->iface_fns);
607 if (fn) {
608 ulong addr;
610 /* Get the relocated table address */
611 addr = (ulong) fn + reloc_offset;
612 fn_r = (Xilinx_Spartan3_Slave_Serial_fns *) addr;
614 if (!fn_r->relocated) {
616 if (memcmp (fn_r, fn,
617 sizeof (Xilinx_Spartan3_Slave_Serial_fns))
618 == 0) {
619 /* good copy of the table, fix the descriptor pointer */
620 desc->iface_fns = fn_r;
621 } else {
622 PRINTF ("%s: Invalid function table at 0x%p\n",
623 __FUNCTION__, fn_r);
624 return FPGA_FAIL;
627 PRINTF ("%s: Relocating descriptor at 0x%p\n", __FUNCTION__,
628 desc);
630 if (fn->pre) {
631 addr = (ulong) (fn->pre) + reloc_offset;
632 fn_r->pre = (Xilinx_pre_fn) addr;
635 addr = (ulong) (fn->pgm) + reloc_offset;
636 fn_r->pgm = (Xilinx_pgm_fn) addr;
638 addr = (ulong) (fn->init) + reloc_offset;
639 fn_r->init = (Xilinx_init_fn) addr;
641 addr = (ulong) (fn->done) + reloc_offset;
642 fn_r->done = (Xilinx_done_fn) addr;
644 addr = (ulong) (fn->clk) + reloc_offset;
645 fn_r->clk = (Xilinx_clk_fn) addr;
647 addr = (ulong) (fn->wr) + reloc_offset;
648 fn_r->wr = (Xilinx_wr_fn) addr;
650 if (fn->post) {
651 addr = (ulong) (fn->post) + reloc_offset;
652 fn_r->post = (Xilinx_post_fn) addr;
655 fn_r->relocated = TRUE;
657 } else {
658 /* this table has already been moved */
659 /* XXX - should check to see if the descriptor is correct */
660 desc->iface_fns = fn_r;
663 ret_val = FPGA_SUCCESS;
664 } else {
665 printf ("%s: NULL Interface function table!\n", __FUNCTION__);
668 return ret_val;
672 #endif