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
18 #include <linux/delay.h>
25 #define PRINTF(fmt, args...) printf(fmt, ##args)
27 #define PRINTF(fmt, args...)
31 * If the SelectMap interface can be overrun by the processor, define
32 * CONFIG_SYS_FPGA_CHECK_BUSY and/or CONFIG_FPGA_DELAY in the board
33 * configuration file and add board-specific support for checking BUSY status.
34 * By default, assume that the SelectMap interface cannot be overrun.
36 #ifndef CONFIG_SYS_FPGA_CHECK_BUSY
37 #undef CONFIG_SYS_FPGA_CHECK_BUSY
40 #ifndef CONFIG_FPGA_DELAY
41 #define CONFIG_FPGA_DELAY()
44 #ifndef CONFIG_SYS_FPGA_PROG_FEEDBACK
45 #define CONFIG_SYS_FPGA_PROG_FEEDBACK
49 * Don't allow config cycle to be interrupted
51 #ifndef CONFIG_SYS_FPGA_CHECK_CTRLC
52 #undef CONFIG_SYS_FPGA_CHECK_CTRLC
56 * Check for errors during configuration by default
58 #ifndef CONFIG_SYS_FPGA_CHECK_ERROR
59 #define CONFIG_SYS_FPGA_CHECK_ERROR
63 * The default timeout in mS for INIT_B to deassert after PROG_B has
64 * been deasserted. Per the latest Virtex II Handbook (page 347), the
65 * max time from PORG_B deassertion to INIT_B deassertion is 4uS per
66 * data frame for the XC2V8000. The XC2V8000 has 2860 data frames
67 * which yields 11.44 mS. So let's make it bigger in order to handle
68 * an XC2V1000, if anyone can ever get ahold of one.
70 #ifndef CONFIG_SYS_FPGA_WAIT_INIT
71 #define CONFIG_SYS_FPGA_WAIT_INIT CONFIG_SYS_HZ / 2 /* 500 ms */
75 * The default timeout for waiting for BUSY to deassert during configuration.
76 * This is normally not necessary since for most reasonable configuration
77 * clock frequencies (i.e. 66 MHz or less), BUSY monitoring is unnecessary.
79 #ifndef CONFIG_SYS_FPGA_WAIT_BUSY
80 #define CONFIG_SYS_FPGA_WAIT_BUSY CONFIG_SYS_HZ / 200 /* 5 ms*/
83 /* Default timeout for waiting for FPGA to enter operational mode after
84 * configuration data has been written.
86 #ifndef CONFIG_SYS_FPGA_WAIT_CONFIG
87 #define CONFIG_SYS_FPGA_WAIT_CONFIG CONFIG_SYS_HZ / 5 /* 200 ms */
90 static int virtex2_ssm_load(xilinx_desc *desc, const void *buf, size_t bsize);
91 static int virtex2_ssm_dump(xilinx_desc *desc, const void *buf, size_t bsize);
93 static int virtex2_ss_load(xilinx_desc *desc, const void *buf, size_t bsize);
94 static int virtex2_ss_dump(xilinx_desc *desc, const void *buf, size_t bsize);
96 static int virtex2_load(xilinx_desc *desc, const void *buf, size_t bsize,
97 bitstream_type bstype)
99 int ret_val = FPGA_FAIL;
101 switch (desc->iface) {
103 PRINTF("%s: Launching Slave Serial Load\n", __func__);
104 ret_val = virtex2_ss_load(desc, buf, bsize);
107 case slave_selectmap:
108 PRINTF("%s: Launching Slave Parallel Load\n", __func__);
109 ret_val = virtex2_ssm_load(desc, buf, bsize);
113 printf("%s: Unsupported interface type, %d\n",
114 __func__, desc->iface);
119 static int virtex2_dump(xilinx_desc *desc, const void *buf, size_t bsize)
121 int ret_val = FPGA_FAIL;
123 switch (desc->iface) {
125 PRINTF("%s: Launching Slave Serial Dump\n", __func__);
126 ret_val = virtex2_ss_dump(desc, buf, bsize);
130 PRINTF("%s: Launching Slave Parallel Dump\n", __func__);
131 ret_val = virtex2_ssm_dump(desc, buf, bsize);
135 printf("%s: Unsupported interface type, %d\n",
136 __func__, desc->iface);
141 static int virtex2_info(xilinx_desc *desc)
147 * Virtex-II Slave SelectMap or Serial configuration loader. Configuration
149 * 1. Set the FPGA's PROG_B line low.
150 * 2. Set the FPGA's PROG_B line high. Wait for INIT_B to go high.
151 * 3. Write data to the SelectMap port. If INIT_B goes low at any time
152 * this process, a configuration error (most likely CRC failure) has
153 * ocurred. At this point a status word may be read from the
154 * SelectMap interface to determine the source of the problem (You
155 * could, for instance, put this in your 'abort' function handler).
156 * 4. After all data has been written, test the state of the FPGA
157 * INIT_B and DONE lines. If both are high, configuration has
158 * succeeded. Congratulations!
160 static int virtex2_slave_pre(xilinx_virtex2_slave_fns *fn, int cookie)
164 PRINTF("%s:%d: Start with interface functions @ 0x%p\n",
165 __func__, __LINE__, fn);
168 printf("%s:%d: NULL Interface function table!\n",
173 /* Gotta split this one up (so the stack won't blow??) */
174 PRINTF("%s:%d: Function Table:\n"
182 &fn, fn, fn->pre, fn->pgm, fn->init, fn->err);
183 PRINTF(" clock 0x%p\n"
191 fn->clk, fn->cs, fn->wr, fn->rdata, fn->wdata,
192 fn->busy, fn->abort, fn->post);
194 #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
195 printf("Initializing FPGA Device %d...\n", cookie);
198 * Run the pre configuration function if there is one.
204 * Assert the program line. The minimum pulse width for
205 * Virtex II devices is 300 nS (Tprogram parameter in datasheet).
206 * There is no maximum value for the pulse width. Check to make
207 * sure that INIT_B goes low after assertion of PROG_B
209 (*fn->pgm)(true, true, cookie);
213 if (get_timer(ts) > CONFIG_SYS_FPGA_WAIT_INIT) {
214 printf("%s:%d: ** Timeout after %d ticks waiting for INIT to assert.\n",
215 __func__, __LINE__, CONFIG_SYS_FPGA_WAIT_INIT);
216 (*fn->abort)(cookie);
219 } while (!(*fn->init)(cookie));
221 (*fn->pgm)(false, true, cookie);
224 (*fn->clk)(true, true, cookie);
227 * Start a timer and wait for INIT_B to go high
232 if (get_timer(ts) > CONFIG_SYS_FPGA_WAIT_INIT) {
233 printf("%s:%d: ** Timeout after %d ticks waiting for INIT to deassert.\n",
234 __func__, __LINE__, CONFIG_SYS_FPGA_WAIT_INIT);
235 (*fn->abort)(cookie);
238 } while ((*fn->init)(cookie) && (*fn->busy)(cookie));
241 (*fn->wr)(true, true, cookie);
243 (*fn->cs)(true, true, cookie);
249 static int virtex2_slave_post(xilinx_virtex2_slave_fns *fn,
252 int ret_val = FPGA_SUCCESS;
257 * Finished writing the data; deassert FPGA CS_B and WRITE_B signals.
261 (*fn->cs)(false, true, cookie);
263 (*fn->wr)(false, true, cookie);
265 #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
270 * Check for successful configuration. FPGA INIT_B and DONE
271 * should both be high upon successful configuration. Continue pulsing
272 * clock with data set to all ones until DONE is asserted and for 8
273 * clock cycles afterwards.
277 if ((*fn->done)(cookie) == FPGA_SUCCESS &&
278 !((*fn->init)(cookie))) {
283 if (get_timer(ts) > CONFIG_SYS_FPGA_WAIT_CONFIG) {
284 printf("%s:%d: ** Timeout after %d ticks waiting for DONE to assert and INIT to deassert\n",
285 __func__, __LINE__, CONFIG_SYS_FPGA_WAIT_CONFIG);
286 (*fn->abort)(cookie);
291 unsigned char dummy = 0xff;
292 (*fn->wbulkdata)(&dummy, 1, true, cookie);
294 (*fn->wdata)(0xff, true, cookie);
296 (*fn->clk)(false, true, cookie);
298 (*fn->clk)(true, true, cookie);
302 if (ret_val == FPGA_SUCCESS) {
303 #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
304 printf("Initialization of FPGA device %d complete\n", cookie);
307 * Run the post configuration function if there is one.
312 #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
313 printf("** Initialization of FPGA device %d FAILED\n",
320 static int virtex2_ssm_load(xilinx_desc *desc, const void *buf, size_t bsize)
322 int ret_val = FPGA_FAIL;
323 xilinx_virtex2_slave_fns *fn = desc->iface_fns;
324 size_t bytecount = 0;
325 unsigned char *data = (unsigned char *)buf;
326 int cookie = desc->cookie;
328 ret_val = virtex2_slave_pre(fn, cookie);
329 if (ret_val != FPGA_SUCCESS)
333 * Load the data byte by byte
335 while (bytecount < bsize) {
336 #ifdef CONFIG_SYS_FPGA_CHECK_CTRLC
338 (*fn->abort)(cookie);
343 if ((*fn->done)(cookie) == FPGA_SUCCESS) {
344 PRINTF("%s:%d:done went active early, bytecount = %d\n",
345 __func__, __LINE__, bytecount);
349 #ifdef CONFIG_SYS_FPGA_CHECK_ERROR
350 if ((*fn->init)(cookie)) {
351 printf("\n%s:%d: ** Error: INIT asserted during configuration\n",
353 printf("%zu = buffer offset, %zu = buffer size\n",
355 (*fn->abort)(cookie);
360 (*fn->wdata)(data[bytecount++], true, cookie);
364 * Cycle the clock pin
366 (*fn->clk)(false, true, cookie);
368 (*fn->clk)(true, true, cookie);
370 #ifdef CONFIG_SYS_FPGA_CHECK_BUSY
372 while ((*fn->busy)(cookie)) {
373 if (get_timer(ts) > CONFIG_SYS_FPGA_WAIT_BUSY) {
374 printf("%s:%d: ** Timeout after %d ticks waiting for BUSY to deassert\n",
376 CONFIG_SYS_FPGA_WAIT_BUSY);
377 (*fn->abort)(cookie);
383 #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
384 if (bytecount % (bsize / 40) == 0)
389 return virtex2_slave_post(fn, cookie);
393 * Read the FPGA configuration data
395 static int virtex2_ssm_dump(xilinx_desc *desc, const void *buf, size_t bsize)
397 int ret_val = FPGA_FAIL;
398 xilinx_virtex2_slave_fns *fn = desc->iface_fns;
401 unsigned char *data = (unsigned char *)buf;
402 size_t bytecount = 0;
403 int cookie = desc->cookie;
405 printf("Starting Dump of FPGA Device %d...\n", cookie);
407 (*fn->cs)(true, true, cookie);
408 (*fn->clk)(true, true, cookie);
410 while (bytecount < bsize) {
411 #ifdef CONFIG_SYS_FPGA_CHECK_CTRLC
413 (*fn->abort)(cookie);
418 * Cycle the clock and read the data
420 (*fn->clk)(false, true, cookie);
421 (*fn->clk)(true, true, cookie);
422 (*fn->rdata)(&data[bytecount++], cookie);
423 #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
424 if (bytecount % (bsize / 40) == 0)
430 * Deassert CS_B and cycle the clock to deselect the device.
432 (*fn->cs)(false, false, cookie);
433 (*fn->clk)(false, true, cookie);
434 (*fn->clk)(true, true, cookie);
436 #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
441 printf("%s:%d: NULL Interface function table!\n",
447 static int virtex2_ss_load(xilinx_desc *desc, const void *buf, size_t bsize)
449 int ret_val = FPGA_FAIL;
450 xilinx_virtex2_slave_fns *fn = desc->iface_fns;
451 unsigned char *data = (unsigned char *)buf;
452 int cookie = desc->cookie;
454 ret_val = virtex2_slave_pre(fn, cookie);
455 if (ret_val != FPGA_SUCCESS)
459 /* Load the data in a single chunk */
460 (*fn->wbulkdata)(data, bsize, true, cookie);
462 size_t bytecount = 0;
465 * Load the data bit by bit
467 while (bytecount < bsize) {
468 unsigned char curr_data = data[bytecount++];
471 #ifdef CONFIG_SYS_FPGA_CHECK_CTRLC
473 (*fn->abort) (cookie);
478 if ((*fn->done)(cookie) == FPGA_SUCCESS) {
479 PRINTF("%s:%d:done went active early, bytecount = %d\n",
480 __func__, __LINE__, bytecount);
484 #ifdef CONFIG_SYS_FPGA_CHECK_ERROR
485 if ((*fn->init)(cookie)) {
486 printf("\n%s:%d: ** Error: INIT asserted during configuration\n",
488 printf("%zu = buffer offset, %zu = buffer size\n",
490 (*fn->abort)(cookie);
495 for (bit = 7; bit >= 0; --bit) {
496 unsigned char curr_bit = (curr_data >> bit) & 1;
497 (*fn->wdata)(curr_bit, true, cookie);
499 (*fn->clk)(false, true, cookie);
501 (*fn->clk)(true, true, cookie);
504 /* Slave serial never uses a busy pin */
506 #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
507 if (bytecount % (bsize / 40) == 0)
513 return virtex2_slave_post(fn, cookie);
516 static int virtex2_ss_dump(xilinx_desc *desc, const void *buf, size_t bsize)
518 printf("%s: Slave Serial Dumping is unsupported\n", __func__);
522 /* vim: set ts=4 tw=78: */
524 struct xilinx_fpga_op virtex2_op = {
525 .load = virtex2_load,
526 .dump = virtex2_dump,
527 .info = virtex2_info,