3 * Rich Ireland, Enterasys Networks, rireland@enterasys.com.
4 * Keith Outwater, keith_outwater@mvis.com
6 * See file CREDITS for list of people who contributed to this
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27 * Configuration support for Xilinx Virtex2 devices. Based
28 * on spartan2.c (Rich Ireland, rireland@enterasys.com).
34 #if (CONFIG_FPGA & (CFG_XILINX | CFG_VIRTEX2))
37 #define PRINTF(fmt,args...) printf (fmt ,##args)
39 #define PRINTF(fmt,args...)
43 * If the SelectMap interface can be overrun by the processor, define
44 * CFG_FPGA_CHECK_BUSY and/or CONFIG_FPGA_DELAY in the board configuration
45 * file and add board-specific support for checking BUSY status. By default,
46 * assume that the SelectMap interface cannot be overrun.
48 #ifndef CFG_FPGA_CHECK_BUSY
49 #undef CFG_FPGA_CHECK_BUSY
52 #ifndef CONFIG_FPGA_DELAY
53 #define CONFIG_FPGA_DELAY()
56 #ifndef CFG_FPGA_PROG_FEEDBACK
57 #define CFG_FPGA_PROG_FEEDBACK
61 * Don't allow config cycle to be interrupted
63 #ifndef CFG_FPGA_CHECK_CTRLC
64 #undef CFG_FPGA_CHECK_CTRLC
68 * Check for errors during configuration by default
70 #ifndef CFG_FPGA_CHECK_ERROR
71 #define CFG_FPGA_CHECK_ERROR
75 * The default timeout in mS for INIT_B to deassert after PROG_B has
76 * been deasserted. Per the latest Virtex II Handbook (page 347), the
77 * max time from PORG_B deassertion to INIT_B deassertion is 4uS per
78 * data frame for the XC2V8000. The XC2V8000 has 2860 data frames
79 * which yields 11.44 mS. So let's make it bigger in order to handle
80 * an XC2V1000, if anyone can ever get ahold of one.
82 #ifndef CFG_FPGA_WAIT_INIT
83 #define CFG_FPGA_WAIT_INIT 500 /* time in milliseconds */
87 * The default timeout for waiting for BUSY to deassert during configuration.
88 * This is normally not necessary since for most reasonable configuration
89 * clock frequencies (i.e. 66 MHz or less), BUSY monitoring is unnecessary.
91 #ifndef CFG_FPGA_WAIT_BUSY
92 #define CFG_FPGA_WAIT_BUSY 5 /* time in milliseconds */
95 /* Default timeout for waiting for FPGA to enter operational mode after
96 * configuration data has been written.
98 #ifndef CFG_FPGA_WAIT_CONFIG
99 #define CFG_FPGA_WAIT_CONFIG 200 /* time in milliseconds */
102 static int Virtex2_ssm_load (Xilinx_desc * desc, void *buf, size_t bsize);
103 static int Virtex2_ssm_dump (Xilinx_desc * desc, void *buf, size_t bsize);
104 static int Virtex2_ssm_reloc (Xilinx_desc * desc, ulong reloc_offset);
106 static int Virtex2_ss_load (Xilinx_desc * desc, void *buf, size_t bsize);
107 static int Virtex2_ss_dump (Xilinx_desc * desc, void *buf, size_t bsize);
108 static int Virtex2_ss_reloc (Xilinx_desc * desc, ulong reloc_offset);
110 int Virtex2_load (Xilinx_desc * desc, void *buf, size_t bsize)
112 int ret_val = FPGA_FAIL;
114 switch (desc->iface) {
116 PRINTF ("%s: Launching Slave Serial Load\n", __FUNCTION__);
117 ret_val = Virtex2_ss_load (desc, buf, bsize);
120 case slave_selectmap:
121 PRINTF ("%s: Launching Slave Parallel Load\n", __FUNCTION__);
122 ret_val = Virtex2_ssm_load (desc, buf, bsize);
126 printf ("%s: Unsupported interface type, %d\n",
127 __FUNCTION__, desc->iface);
132 int Virtex2_dump (Xilinx_desc * desc, void *buf, size_t bsize)
134 int ret_val = FPGA_FAIL;
136 switch (desc->iface) {
138 PRINTF ("%s: Launching Slave Serial Dump\n", __FUNCTION__);
139 ret_val = Virtex2_ss_dump (desc, buf, bsize);
143 PRINTF ("%s: Launching Slave Parallel Dump\n", __FUNCTION__);
144 ret_val = Virtex2_ssm_dump (desc, buf, bsize);
148 printf ("%s: Unsupported interface type, %d\n",
149 __FUNCTION__, desc->iface);
154 int Virtex2_info (Xilinx_desc * desc)
159 int Virtex2_reloc (Xilinx_desc * desc, ulong reloc_offset)
161 int ret_val = FPGA_FAIL;
163 if (desc->family != Xilinx_Virtex2) {
164 printf ("%s: Unsupported family type, %d\n",
165 __FUNCTION__, desc->family);
168 switch (desc->iface) {
170 ret_val = Virtex2_ss_reloc (desc, reloc_offset);
173 case slave_selectmap:
174 ret_val = Virtex2_ssm_reloc (desc, reloc_offset);
178 printf ("%s: Unsupported interface type, %d\n",
179 __FUNCTION__, desc->iface);
185 * Virtex-II Slave SelectMap configuration loader. Configuration via
186 * SelectMap is as follows:
187 * 1. Set the FPGA's PROG_B line low.
188 * 2. Set the FPGA's PROG_B line high. Wait for INIT_B to go high.
189 * 3. Write data to the SelectMap port. If INIT_B goes low at any time
190 * this process, a configuration error (most likely CRC failure) has
191 * ocurred. At this point a status word may be read from the
192 * SelectMap interface to determine the source of the problem (You
193 * could, for instance, put this in you 'abort' function handler).
194 * 4. After all data has been written, test the state of the FPGA
195 * INIT_B and DONE lines. If both are high, configuration has
196 * succeeded. Congratulations!
198 static int Virtex2_ssm_load (Xilinx_desc * desc, void *buf, size_t bsize)
200 int ret_val = FPGA_FAIL;
201 Xilinx_Virtex2_Slave_SelectMap_fns *fn = desc->iface_fns;
203 PRINTF ("%s:%d: Start with interface functions @ 0x%p\n",
204 __FUNCTION__, __LINE__, fn);
207 size_t bytecount = 0;
208 unsigned char *data = (unsigned char *) buf;
209 int cookie = desc->cookie;
212 /* Gotta split this one up (so the stack won't blow??) */
213 PRINTF ("%s:%d: Function Table:\n"
220 __FUNCTION__, __LINE__,
221 &fn, fn, fn->pre, fn->pgm, fn->init, fn->err);
222 PRINTF (" clock 0x%p\n"
230 fn->clk, fn->cs, fn->wr, fn->rdata, fn->wdata,
231 fn->busy, fn->abort, fn->post);
233 #ifdef CFG_FPGA_PROG_FEEDBACK
234 printf ("Initializing FPGA Device %d...\n", cookie);
237 * Run the pre configuration function if there is one.
244 * Assert the program line. The minimum pulse width for
245 * Virtex II devices is 300 nS (Tprogram parameter in datasheet).
246 * There is no maximum value for the pulse width. Check to make
247 * sure that INIT_B goes low after assertion of PROG_B
249 (*fn->pgm) (TRUE, TRUE, cookie);
253 if (get_timer (ts) > CFG_FPGA_WAIT_INIT) {
254 printf ("%s:%d: ** Timeout after %d mS waiting for INIT"
255 " to assert.\n", __FUNCTION__, __LINE__,
257 (*fn->abort) (cookie);
260 } while (!(*fn->init) (cookie));
262 (*fn->pgm) (FALSE, TRUE, cookie);
263 CONFIG_FPGA_DELAY ();
264 (*fn->clk) (TRUE, TRUE, cookie);
267 * Start a timer and wait for INIT_B to go high
271 CONFIG_FPGA_DELAY ();
272 if (get_timer (ts) > CFG_FPGA_WAIT_INIT) {
273 printf ("%s:%d: ** Timeout after %d mS waiting for INIT"
274 " to deassert.\n", __FUNCTION__, __LINE__,
276 (*fn->abort) (cookie);
279 } while ((*fn->init) (cookie) && (*fn->busy) (cookie));
281 (*fn->wr) (TRUE, TRUE, cookie);
282 (*fn->cs) (TRUE, TRUE, cookie);
287 * Load the data byte by byte
289 while (bytecount < bsize) {
290 #ifdef CFG_FPGA_CHECK_CTRLC
292 (*fn->abort) (cookie);
296 #ifdef CFG_FPGA_CHECK_ERROR
297 if ((*fn->init) (cookie)) {
298 printf ("%s:%d: ** Error: INIT asserted during"
299 " configuration\n", __FUNCTION__, __LINE__);
300 (*fn->abort) (cookie);
304 (*fn->wdata) (data[bytecount++], TRUE, cookie);
305 CONFIG_FPGA_DELAY ();
308 * Cycle the clock pin
310 (*fn->clk) (FALSE, TRUE, cookie);
311 CONFIG_FPGA_DELAY ();
312 (*fn->clk) (TRUE, TRUE, cookie);
314 #ifdef CFG_FPGA_CHECK_BUSY
316 while ((*fn->busy) (cookie)) {
317 if (get_timer (ts) > CFG_FPGA_WAIT_BUSY) {
318 printf ("%s:%d: ** Timeout after %d mS waiting for"
319 " BUSY to deassert\n",
320 __FUNCTION__, __LINE__, CFG_FPGA_WAIT_BUSY);
321 (*fn->abort) (cookie);
327 #ifdef CFG_FPGA_PROG_FEEDBACK
328 if (bytecount % (bsize / 40) == 0)
334 * Finished writing the data; deassert FPGA CS_B and WRITE_B signals.
336 CONFIG_FPGA_DELAY ();
337 (*fn->cs) (FALSE, TRUE, cookie);
338 (*fn->wr) (FALSE, TRUE, cookie);
340 #ifdef CFG_FPGA_PROG_FEEDBACK
345 * Check for successful configuration. FPGA INIT_B and DONE should
346 * both be high upon successful configuration.
349 ret_val = FPGA_SUCCESS;
350 while (((*fn->done) (cookie) == FPGA_FAIL) || (*fn->init) (cookie)) {
351 if (get_timer (ts) > CFG_FPGA_WAIT_CONFIG) {
352 printf ("%s:%d: ** Timeout after %d mS waiting for DONE to"
353 "assert and INIT to deassert\n",
354 __FUNCTION__, __LINE__, CFG_FPGA_WAIT_CONFIG);
355 (*fn->abort) (cookie);
361 if (ret_val == FPGA_SUCCESS) {
362 #ifdef CFG_FPGA_PROG_FEEDBACK
363 printf ("Initialization of FPGA device %d complete\n", cookie);
366 * Run the post configuration function if there is one.
369 (*fn->post) (cookie);
372 #ifdef CFG_FPGA_PROG_FEEDBACK
373 printf ("** Initialization of FPGA device %d FAILED\n",
378 printf ("%s:%d: NULL Interface function table!\n",
379 __FUNCTION__, __LINE__);
385 * Read the FPGA configuration data
387 static int Virtex2_ssm_dump (Xilinx_desc * desc, void *buf, size_t bsize)
389 int ret_val = FPGA_FAIL;
390 Xilinx_Virtex2_Slave_SelectMap_fns *fn = desc->iface_fns;
393 unsigned char *data = (unsigned char *) buf;
394 size_t bytecount = 0;
395 int cookie = desc->cookie;
397 printf ("Starting Dump of FPGA Device %d...\n", cookie);
399 (*fn->cs) (TRUE, TRUE, cookie);
400 (*fn->clk) (TRUE, TRUE, cookie);
402 while (bytecount < bsize) {
403 #ifdef CFG_FPGA_CHECK_CTRLC
405 (*fn->abort) (cookie);
410 * Cycle the clock and read the data
412 (*fn->clk) (FALSE, TRUE, cookie);
413 (*fn->clk) (TRUE, TRUE, cookie);
414 (*fn->rdata) (&(data[bytecount++]), cookie);
415 #ifdef CFG_FPGA_PROG_FEEDBACK
416 if (bytecount % (bsize / 40) == 0)
422 * Deassert CS_B and cycle the clock to deselect the device.
424 (*fn->cs) (FALSE, FALSE, cookie);
425 (*fn->clk) (FALSE, TRUE, cookie);
426 (*fn->clk) (TRUE, TRUE, cookie);
428 #ifdef CFG_FPGA_PROG_FEEDBACK
433 printf ("%s:%d: NULL Interface function table!\n",
434 __FUNCTION__, __LINE__);
440 * Relocate the addresses in the function table from FLASH (or ROM,
441 * or whatever) to RAM.
443 static int Virtex2_ssm_reloc (Xilinx_desc * desc, ulong reloc_offset)
446 int ret_val = FPGA_FAIL;
447 Xilinx_Virtex2_Slave_SelectMap_fns *fn_r, *fn =
448 (Xilinx_Virtex2_Slave_SelectMap_fns *) (desc->iface_fns);
452 * Get the relocated table address
454 addr = (ulong) fn + reloc_offset;
455 fn_r = (Xilinx_Virtex2_Slave_SelectMap_fns *) addr;
458 * Check to see if the table has already been relocated. If not, do
459 * a sanity check to make sure there is a faithful copy of the
460 * FLASH based function table in RAM, then adjust the table.
462 if (!fn_r->relocated) {
464 (fn_r, fn, sizeof (Xilinx_Virtex2_Slave_SelectMap_fns))
466 desc->iface_fns = fn_r;
468 PRINTF ("%s:%d: Invalid function table at 0x%p\n",
469 __FUNCTION__, __LINE__, fn_r);
473 PRINTF ("%s:%d: Relocating descriptor at 0x%p\n",
474 __FUNCTION__, __LINE__, desc);
476 addr = (ulong) (fn->pre) + reloc_offset;
477 fn_r->pre = (Xilinx_pre_fn) addr;
478 addr = (ulong) (fn->pgm) + reloc_offset;
479 fn_r->pgm = (Xilinx_pgm_fn) addr;
480 addr = (ulong) (fn->init) + reloc_offset;
481 fn_r->init = (Xilinx_init_fn) addr;
482 addr = (ulong) (fn->done) + reloc_offset;
483 fn_r->done = (Xilinx_done_fn) addr;
484 addr = (ulong) (fn->err) + reloc_offset;
485 fn_r->err = (Xilinx_err_fn) addr;
486 addr = (ulong) (fn->clk) + reloc_offset;
487 fn_r->clk = (Xilinx_clk_fn) addr;
488 addr = (ulong) (fn->cs) + reloc_offset;
489 fn_r->cs = (Xilinx_cs_fn) addr;
490 addr = (ulong) (fn->wr) + reloc_offset;
491 fn_r->wr = (Xilinx_wr_fn) addr;
492 addr = (ulong) (fn->rdata) + reloc_offset;
493 fn_r->rdata = (Xilinx_rdata_fn) addr;
494 addr = (ulong) (fn->wdata) + reloc_offset;
495 fn_r->wdata = (Xilinx_wdata_fn) addr;
496 addr = (ulong) (fn->busy) + reloc_offset;
497 fn_r->busy = (Xilinx_busy_fn) addr;
498 addr = (ulong) (fn->abort) + reloc_offset;
499 fn_r->abort = (Xilinx_abort_fn) addr;
500 addr = (ulong) (fn->post) + reloc_offset;
501 fn_r->post = (Xilinx_post_fn) addr;
502 fn_r->relocated = TRUE;
504 printf ("%s:%d: Function table @0x%p has already been relocated\n", __FUNCTION__, __LINE__, fn_r);
505 desc->iface_fns = fn_r;
507 ret_val = FPGA_SUCCESS;
509 printf ("%s: NULL Interface function table!\n", __FUNCTION__);
514 static int Virtex2_ss_load (Xilinx_desc * desc, void *buf, size_t bsize)
516 printf ("%s: Slave Serial Loading is unsupported\n", __FUNCTION__);
520 static int Virtex2_ss_dump (Xilinx_desc * desc, void *buf, size_t bsize)
522 printf ("%s: Slave Serial Dumping is unsupported\n", __FUNCTION__);
526 static int Virtex2_ss_reloc (Xilinx_desc * desc, ulong reloc_offset)
528 int ret_val = FPGA_FAIL;
529 Xilinx_Virtex2_Slave_Serial_fns *fn =
530 (Xilinx_Virtex2_Slave_Serial_fns *) (desc->iface_fns);
533 printf ("%s:%d: Slave Serial Loading is unsupported\n",
534 __FUNCTION__, __LINE__);
536 printf ("%s:%d: NULL Interface function table!\n",
537 __FUNCTION__, __LINE__);
543 /* vim: set ts=4 tw=78: */