1 // SPDX-License-Identifier: GPL-2.0+
7 * Copyright (c) 2019 SED Systems, a division of Calian Ltd.
11 * Configuration support for Xilinx Virtex2 devices. Based
24 #define PRINTF(fmt, args...) printf(fmt, ##args)
26 #define PRINTF(fmt, args...)
30 * If the SelectMap interface can be overrun by the processor, define
31 * CONFIG_SYS_FPGA_CHECK_BUSY and/or CONFIG_FPGA_DELAY in the board
32 * configuration file and add board-specific support for checking BUSY status.
33 * By default, assume that the SelectMap interface cannot be overrun.
35 #ifndef CONFIG_SYS_FPGA_CHECK_BUSY
36 #undef CONFIG_SYS_FPGA_CHECK_BUSY
39 #ifndef CONFIG_FPGA_DELAY
40 #define CONFIG_FPGA_DELAY()
43 #ifndef CONFIG_SYS_FPGA_PROG_FEEDBACK
44 #define CONFIG_SYS_FPGA_PROG_FEEDBACK
48 * Don't allow config cycle to be interrupted
50 #ifndef CONFIG_SYS_FPGA_CHECK_CTRLC
51 #undef CONFIG_SYS_FPGA_CHECK_CTRLC
55 * Check for errors during configuration by default
57 #ifndef CONFIG_SYS_FPGA_CHECK_ERROR
58 #define CONFIG_SYS_FPGA_CHECK_ERROR
62 * The default timeout in mS for INIT_B to deassert after PROG_B has
63 * been deasserted. Per the latest Virtex II Handbook (page 347), the
64 * max time from PORG_B deassertion to INIT_B deassertion is 4uS per
65 * data frame for the XC2V8000. The XC2V8000 has 2860 data frames
66 * which yields 11.44 mS. So let's make it bigger in order to handle
67 * an XC2V1000, if anyone can ever get ahold of one.
69 #ifndef CONFIG_SYS_FPGA_WAIT_INIT
70 #define CONFIG_SYS_FPGA_WAIT_INIT CONFIG_SYS_HZ / 2 /* 500 ms */
74 * The default timeout for waiting for BUSY to deassert during configuration.
75 * This is normally not necessary since for most reasonable configuration
76 * clock frequencies (i.e. 66 MHz or less), BUSY monitoring is unnecessary.
78 #ifndef CONFIG_SYS_FPGA_WAIT_BUSY
79 #define CONFIG_SYS_FPGA_WAIT_BUSY CONFIG_SYS_HZ / 200 /* 5 ms*/
82 /* Default timeout for waiting for FPGA to enter operational mode after
83 * configuration data has been written.
85 #ifndef CONFIG_SYS_FPGA_WAIT_CONFIG
86 #define CONFIG_SYS_FPGA_WAIT_CONFIG CONFIG_SYS_HZ / 5 /* 200 ms */
89 static int virtex2_ssm_load(xilinx_desc *desc, const void *buf, size_t bsize);
90 static int virtex2_ssm_dump(xilinx_desc *desc, const void *buf, size_t bsize);
92 static int virtex2_ss_load(xilinx_desc *desc, const void *buf, size_t bsize);
93 static int virtex2_ss_dump(xilinx_desc *desc, const void *buf, size_t bsize);
95 static int virtex2_load(xilinx_desc *desc, const void *buf, size_t bsize,
96 bitstream_type bstype)
98 int ret_val = FPGA_FAIL;
100 switch (desc->iface) {
102 PRINTF("%s: Launching Slave Serial Load\n", __func__);
103 ret_val = virtex2_ss_load(desc, buf, bsize);
106 case slave_selectmap:
107 PRINTF("%s: Launching Slave Parallel Load\n", __func__);
108 ret_val = virtex2_ssm_load(desc, buf, bsize);
112 printf("%s: Unsupported interface type, %d\n",
113 __func__, desc->iface);
118 static int virtex2_dump(xilinx_desc *desc, const void *buf, size_t bsize)
120 int ret_val = FPGA_FAIL;
122 switch (desc->iface) {
124 PRINTF("%s: Launching Slave Serial Dump\n", __func__);
125 ret_val = virtex2_ss_dump(desc, buf, bsize);
129 PRINTF("%s: Launching Slave Parallel Dump\n", __func__);
130 ret_val = virtex2_ssm_dump(desc, buf, bsize);
134 printf("%s: Unsupported interface type, %d\n",
135 __func__, desc->iface);
140 static int virtex2_info(xilinx_desc *desc)
146 * Virtex-II Slave SelectMap or Serial configuration loader. Configuration
148 * 1. Set the FPGA's PROG_B line low.
149 * 2. Set the FPGA's PROG_B line high. Wait for INIT_B to go high.
150 * 3. Write data to the SelectMap port. If INIT_B goes low at any time
151 * this process, a configuration error (most likely CRC failure) has
152 * ocurred. At this point a status word may be read from the
153 * SelectMap interface to determine the source of the problem (You
154 * could, for instance, put this in your 'abort' function handler).
155 * 4. After all data has been written, test the state of the FPGA
156 * INIT_B and DONE lines. If both are high, configuration has
157 * succeeded. Congratulations!
159 static int virtex2_slave_pre(xilinx_virtex2_slave_fns *fn, int cookie)
163 PRINTF("%s:%d: Start with interface functions @ 0x%p\n",
164 __func__, __LINE__, fn);
167 printf("%s:%d: NULL Interface function table!\n",
172 /* Gotta split this one up (so the stack won't blow??) */
173 PRINTF("%s:%d: Function Table:\n"
181 &fn, fn, fn->pre, fn->pgm, fn->init, fn->err);
182 PRINTF(" clock 0x%p\n"
190 fn->clk, fn->cs, fn->wr, fn->rdata, fn->wdata,
191 fn->busy, fn->abort, fn->post);
193 #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
194 printf("Initializing FPGA Device %d...\n", cookie);
197 * Run the pre configuration function if there is one.
203 * Assert the program line. The minimum pulse width for
204 * Virtex II devices is 300 nS (Tprogram parameter in datasheet).
205 * There is no maximum value for the pulse width. Check to make
206 * sure that INIT_B goes low after assertion of PROG_B
208 (*fn->pgm)(true, true, cookie);
212 if (get_timer(ts) > CONFIG_SYS_FPGA_WAIT_INIT) {
213 printf("%s:%d: ** Timeout after %d ticks waiting for INIT to assert.\n",
214 __func__, __LINE__, CONFIG_SYS_FPGA_WAIT_INIT);
215 (*fn->abort)(cookie);
218 } while (!(*fn->init)(cookie));
220 (*fn->pgm)(false, true, cookie);
223 (*fn->clk)(true, true, cookie);
226 * Start a timer and wait for INIT_B to go high
231 if (get_timer(ts) > CONFIG_SYS_FPGA_WAIT_INIT) {
232 printf("%s:%d: ** Timeout after %d ticks waiting for INIT to deassert.\n",
233 __func__, __LINE__, CONFIG_SYS_FPGA_WAIT_INIT);
234 (*fn->abort)(cookie);
237 } while ((*fn->init)(cookie) && (*fn->busy)(cookie));
240 (*fn->wr)(true, true, cookie);
242 (*fn->cs)(true, true, cookie);
248 static int virtex2_slave_post(xilinx_virtex2_slave_fns *fn,
251 int ret_val = FPGA_SUCCESS;
256 * Finished writing the data; deassert FPGA CS_B and WRITE_B signals.
260 (*fn->cs)(false, true, cookie);
262 (*fn->wr)(false, true, cookie);
264 #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
269 * Check for successful configuration. FPGA INIT_B and DONE
270 * should both be high upon successful configuration. Continue pulsing
271 * clock with data set to all ones until DONE is asserted and for 8
272 * clock cycles afterwards.
276 if ((*fn->done)(cookie) == FPGA_SUCCESS &&
277 !((*fn->init)(cookie))) {
282 if (get_timer(ts) > CONFIG_SYS_FPGA_WAIT_CONFIG) {
283 printf("%s:%d: ** Timeout after %d ticks waiting for DONE to assert and INIT to deassert\n",
284 __func__, __LINE__, CONFIG_SYS_FPGA_WAIT_CONFIG);
285 (*fn->abort)(cookie);
290 unsigned char dummy = 0xff;
291 (*fn->wbulkdata)(&dummy, 1, true, cookie);
293 (*fn->wdata)(0xff, true, cookie);
295 (*fn->clk)(false, true, cookie);
297 (*fn->clk)(true, true, cookie);
301 if (ret_val == FPGA_SUCCESS) {
302 #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
303 printf("Initialization of FPGA device %d complete\n", cookie);
306 * Run the post configuration function if there is one.
311 #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
312 printf("** Initialization of FPGA device %d FAILED\n",
319 static int virtex2_ssm_load(xilinx_desc *desc, const void *buf, size_t bsize)
321 int ret_val = FPGA_FAIL;
322 xilinx_virtex2_slave_fns *fn = desc->iface_fns;
323 size_t bytecount = 0;
324 unsigned char *data = (unsigned char *)buf;
325 int cookie = desc->cookie;
327 ret_val = virtex2_slave_pre(fn, cookie);
328 if (ret_val != FPGA_SUCCESS)
332 * Load the data byte by byte
334 while (bytecount < bsize) {
335 #ifdef CONFIG_SYS_FPGA_CHECK_CTRLC
337 (*fn->abort)(cookie);
342 if ((*fn->done)(cookie) == FPGA_SUCCESS) {
343 PRINTF("%s:%d:done went active early, bytecount = %d\n",
344 __func__, __LINE__, bytecount);
348 #ifdef CONFIG_SYS_FPGA_CHECK_ERROR
349 if ((*fn->init)(cookie)) {
350 printf("\n%s:%d: ** Error: INIT asserted during configuration\n",
352 printf("%zu = buffer offset, %zu = buffer size\n",
354 (*fn->abort)(cookie);
359 (*fn->wdata)(data[bytecount++], true, cookie);
363 * Cycle the clock pin
365 (*fn->clk)(false, true, cookie);
367 (*fn->clk)(true, true, cookie);
369 #ifdef CONFIG_SYS_FPGA_CHECK_BUSY
371 while ((*fn->busy)(cookie)) {
372 if (get_timer(ts) > CONFIG_SYS_FPGA_WAIT_BUSY) {
373 printf("%s:%d: ** Timeout after %d ticks waiting for BUSY to deassert\n",
375 CONFIG_SYS_FPGA_WAIT_BUSY);
376 (*fn->abort)(cookie);
382 #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
383 if (bytecount % (bsize / 40) == 0)
388 return virtex2_slave_post(fn, cookie);
392 * Read the FPGA configuration data
394 static int virtex2_ssm_dump(xilinx_desc *desc, const void *buf, size_t bsize)
396 int ret_val = FPGA_FAIL;
397 xilinx_virtex2_slave_fns *fn = desc->iface_fns;
400 unsigned char *data = (unsigned char *)buf;
401 size_t bytecount = 0;
402 int cookie = desc->cookie;
404 printf("Starting Dump of FPGA Device %d...\n", cookie);
406 (*fn->cs)(true, true, cookie);
407 (*fn->clk)(true, true, cookie);
409 while (bytecount < bsize) {
410 #ifdef CONFIG_SYS_FPGA_CHECK_CTRLC
412 (*fn->abort)(cookie);
417 * Cycle the clock and read the data
419 (*fn->clk)(false, true, cookie);
420 (*fn->clk)(true, true, cookie);
421 (*fn->rdata)(&data[bytecount++], cookie);
422 #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
423 if (bytecount % (bsize / 40) == 0)
429 * Deassert CS_B and cycle the clock to deselect the device.
431 (*fn->cs)(false, false, cookie);
432 (*fn->clk)(false, true, cookie);
433 (*fn->clk)(true, true, cookie);
435 #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
440 printf("%s:%d: NULL Interface function table!\n",
446 static int virtex2_ss_load(xilinx_desc *desc, const void *buf, size_t bsize)
448 int ret_val = FPGA_FAIL;
449 xilinx_virtex2_slave_fns *fn = desc->iface_fns;
450 unsigned char *data = (unsigned char *)buf;
451 int cookie = desc->cookie;
453 ret_val = virtex2_slave_pre(fn, cookie);
454 if (ret_val != FPGA_SUCCESS)
458 /* Load the data in a single chunk */
459 (*fn->wbulkdata)(data, bsize, true, cookie);
461 size_t bytecount = 0;
464 * Load the data bit by bit
466 while (bytecount < bsize) {
467 unsigned char curr_data = data[bytecount++];
470 #ifdef CONFIG_SYS_FPGA_CHECK_CTRLC
472 (*fn->abort) (cookie);
477 if ((*fn->done)(cookie) == FPGA_SUCCESS) {
478 PRINTF("%s:%d:done went active early, bytecount = %d\n",
479 __func__, __LINE__, bytecount);
483 #ifdef CONFIG_SYS_FPGA_CHECK_ERROR
484 if ((*fn->init)(cookie)) {
485 printf("\n%s:%d: ** Error: INIT asserted during configuration\n",
487 printf("%zu = buffer offset, %zu = buffer size\n",
489 (*fn->abort)(cookie);
494 for (bit = 7; bit >= 0; --bit) {
495 unsigned char curr_bit = (curr_data >> bit) & 1;
496 (*fn->wdata)(curr_bit, true, cookie);
498 (*fn->clk)(false, true, cookie);
500 (*fn->clk)(true, true, cookie);
503 /* Slave serial never uses a busy pin */
505 #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
506 if (bytecount % (bsize / 40) == 0)
512 return virtex2_slave_post(fn, cookie);
515 static int virtex2_ss_dump(xilinx_desc *desc, const void *buf, size_t bsize)
517 printf("%s: Slave Serial Dumping is unsupported\n", __func__);
521 /* vim: set ts=4 tw=78: */
523 struct xilinx_fpga_op virtex2_op = {
524 .load = virtex2_load,
525 .dump = virtex2_dump,
526 .info = virtex2_info,