]> Git Repo - J-u-boot.git/blame - drivers/fpga/virtex2.c
global: Move remaining CONFIG_*SRIO_* to CFG_*
[J-u-boot.git] / drivers / fpga / virtex2.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
5d3207da
WD
2/*
3 * (C) Copyright 2002
4 * Rich Ireland, Enterasys Networks, [email protected].
5 * Keith Outwater, [email protected]
175dccd7
RH
6 *
7 * Copyright (c) 2019 SED Systems, a division of Calian Ltd.
5d3207da
WD
8 */
9
10/*
11 * Configuration support for Xilinx Virtex2 devices. Based
12 * on spartan2.c (Rich Ireland, [email protected]).
13 */
14
63c46e02
AD
15#define LOG_CATEGORY UCLASS_FPGA
16
5d3207da 17#include <common.h>
24b852a7 18#include <console.h>
63c46e02 19#include <log.h>
5d3207da 20#include <virtex2.h>
c05ed00a 21#include <linux/delay.h>
5d3207da 22
5d3207da
WD
23/*
24 * If the SelectMap interface can be overrun by the processor, define
fa57af05
RH
25 * CONFIG_SYS_FPGA_CHECK_BUSY and/or CONFIG_FPGA_DELAY in the board
26 * configuration file and add board-specific support for checking BUSY status.
27 * By default, assume that the SelectMap interface cannot be overrun.
5d3207da 28 */
6d0f6bcf
JCPV
29#ifndef CONFIG_SYS_FPGA_CHECK_BUSY
30#undef CONFIG_SYS_FPGA_CHECK_BUSY
5d3207da
WD
31#endif
32
33#ifndef CONFIG_FPGA_DELAY
34#define CONFIG_FPGA_DELAY()
35#endif
36
5d3207da
WD
37/*
38 * Check for errors during configuration by default
39 */
6d0f6bcf
JCPV
40#ifndef CONFIG_SYS_FPGA_CHECK_ERROR
41#define CONFIG_SYS_FPGA_CHECK_ERROR
5d3207da
WD
42#endif
43
44/*
45 * The default timeout in mS for INIT_B to deassert after PROG_B has
46 * been deasserted. Per the latest Virtex II Handbook (page 347), the
47 * max time from PORG_B deassertion to INIT_B deassertion is 4uS per
48 * data frame for the XC2V8000. The XC2V8000 has 2860 data frames
49 * which yields 11.44 mS. So let's make it bigger in order to handle
50 * an XC2V1000, if anyone can ever get ahold of one.
51 */
6d0f6bcf 52#ifndef CONFIG_SYS_FPGA_WAIT_INIT
fa57af05 53#define CONFIG_SYS_FPGA_WAIT_INIT CONFIG_SYS_HZ / 2 /* 500 ms */
5d3207da
WD
54#endif
55
56/*
57 * The default timeout for waiting for BUSY to deassert during configuration.
58 * This is normally not necessary since for most reasonable configuration
59 * clock frequencies (i.e. 66 MHz or less), BUSY monitoring is unnecessary.
60 */
6d0f6bcf 61#ifndef CONFIG_SYS_FPGA_WAIT_BUSY
fa57af05 62#define CONFIG_SYS_FPGA_WAIT_BUSY CONFIG_SYS_HZ / 200 /* 5 ms*/
5d3207da
WD
63#endif
64
65/* Default timeout for waiting for FPGA to enter operational mode after
66 * configuration data has been written.
67 */
6d0f6bcf 68#ifndef CONFIG_SYS_FPGA_WAIT_CONFIG
fa57af05 69#define CONFIG_SYS_FPGA_WAIT_CONFIG CONFIG_SYS_HZ / 5 /* 200 ms */
5d3207da
WD
70#endif
71
f8c1be98
MS
72static int virtex2_ssm_load(xilinx_desc *desc, const void *buf, size_t bsize);
73static int virtex2_ssm_dump(xilinx_desc *desc, const void *buf, size_t bsize);
5d3207da 74
f8c1be98
MS
75static int virtex2_ss_load(xilinx_desc *desc, const void *buf, size_t bsize);
76static int virtex2_ss_dump(xilinx_desc *desc, const void *buf, size_t bsize);
5d3207da 77
7a78bd26 78static int virtex2_load(xilinx_desc *desc, const void *buf, size_t bsize,
3e78481d 79 bitstream_type bstype, int flags)
5d3207da
WD
80{
81 int ret_val = FPGA_FAIL;
82
83 switch (desc->iface) {
84 case slave_serial:
63c46e02 85 log_debug("Launching Slave Serial Load\n");
d9071ce0 86 ret_val = virtex2_ss_load(desc, buf, bsize);
5d3207da
WD
87 break;
88
89 case slave_selectmap:
63c46e02 90 log_debug("Launching Slave Parallel Load\n");
d9071ce0 91 ret_val = virtex2_ssm_load(desc, buf, bsize);
5d3207da
WD
92 break;
93
94 default:
fa57af05
RH
95 printf("%s: Unsupported interface type, %d\n",
96 __func__, desc->iface);
5d3207da
WD
97 }
98 return ret_val;
99}
100
14cfc4f3 101static int virtex2_dump(xilinx_desc *desc, const void *buf, size_t bsize)
5d3207da
WD
102{
103 int ret_val = FPGA_FAIL;
104
105 switch (desc->iface) {
106 case slave_serial:
63c46e02 107 log_debug("Launching Slave Serial Dump\n");
d9071ce0 108 ret_val = virtex2_ss_dump(desc, buf, bsize);
5d3207da
WD
109 break;
110
111 case slave_parallel:
63c46e02 112 log_debug("Launching Slave Parallel Dump\n");
d9071ce0 113 ret_val = virtex2_ssm_dump(desc, buf, bsize);
5d3207da
WD
114 break;
115
116 default:
fa57af05
RH
117 printf("%s: Unsupported interface type, %d\n",
118 __func__, desc->iface);
5d3207da
WD
119 }
120 return ret_val;
121}
122
14cfc4f3 123static int virtex2_info(xilinx_desc *desc)
5d3207da
WD
124{
125 return FPGA_SUCCESS;
126}
127
5d3207da 128/*
175dccd7
RH
129 * Virtex-II Slave SelectMap or Serial configuration loader. Configuration
130 * is as follows:
5d3207da
WD
131 * 1. Set the FPGA's PROG_B line low.
132 * 2. Set the FPGA's PROG_B line high. Wait for INIT_B to go high.
133 * 3. Write data to the SelectMap port. If INIT_B goes low at any time
134 * this process, a configuration error (most likely CRC failure) has
135 * ocurred. At this point a status word may be read from the
136 * SelectMap interface to determine the source of the problem (You
9a9200b4 137 * could, for instance, put this in your 'abort' function handler).
5d3207da
WD
138 * 4. After all data has been written, test the state of the FPGA
139 * INIT_B and DONE lines. If both are high, configuration has
140 * succeeded. Congratulations!
141 */
175dccd7 142static int virtex2_slave_pre(xilinx_virtex2_slave_fns *fn, int cookie)
5d3207da 143{
3372081c 144 unsigned long ts;
5d3207da 145
63c46e02 146 log_debug("Start with interface functions @ 0x%p\n", fn);
5d3207da 147
3372081c
RH
148 if (!fn) {
149 printf("%s:%d: NULL Interface function table!\n",
150 __func__, __LINE__);
151 return FPGA_FAIL;
152 }
153
154 /* Gotta split this one up (so the stack won't blow??) */
63c46e02
AD
155 log_debug("Function Table:\n"
156 " base 0x%p\n"
157 " struct 0x%p\n"
158 " pre 0x%p\n"
159 " prog 0x%p\n"
160 " init 0x%p\n"
161 " error 0x%p\n",
162 &fn, fn, fn->pre, fn->pgm, fn->init, fn->err);
163 log_debug(" clock 0x%p\n"
164 " cs 0x%p\n"
165 " write 0x%p\n"
166 " rdata 0x%p\n"
167 " wdata 0x%p\n"
168 " busy 0x%p\n"
169 " abort 0x%p\n"
170 " post 0x%p\n\n",
171 fn->clk, fn->cs, fn->wr, fn->rdata, fn->wdata,
172 fn->busy, fn->abort, fn->post);
5d3207da 173
6d0f6bcf 174#ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
3372081c 175 printf("Initializing FPGA Device %d...\n", cookie);
5d3207da 176#endif
3372081c
RH
177 /*
178 * Run the pre configuration function if there is one.
179 */
180 if (*fn->pre)
181 (*fn->pre)(cookie);
182
183 /*
184 * Assert the program line. The minimum pulse width for
185 * Virtex II devices is 300 nS (Tprogram parameter in datasheet).
186 * There is no maximum value for the pulse width. Check to make
187 * sure that INIT_B goes low after assertion of PROG_B
188 */
189 (*fn->pgm)(true, true, cookie);
190 udelay(10);
191 ts = get_timer(0);
192 do {
193 if (get_timer(ts) > CONFIG_SYS_FPGA_WAIT_INIT) {
194 printf("%s:%d: ** Timeout after %d ticks waiting for INIT to assert.\n",
195 __func__, __LINE__, CONFIG_SYS_FPGA_WAIT_INIT);
196 (*fn->abort)(cookie);
197 return FPGA_FAIL;
198 }
199 } while (!(*fn->init)(cookie));
5d3207da 200
3372081c
RH
201 (*fn->pgm)(false, true, cookie);
202 CONFIG_FPGA_DELAY();
203 if (fn->clk)
fa57af05 204 (*fn->clk)(true, true, cookie);
5d3207da 205
3372081c
RH
206 /*
207 * Start a timer and wait for INIT_B to go high
208 */
209 ts = get_timer(0);
210 do {
211 CONFIG_FPGA_DELAY();
212 if (get_timer(ts) > CONFIG_SYS_FPGA_WAIT_INIT) {
213 printf("%s:%d: ** Timeout after %d ticks waiting for INIT to deassert.\n",
214 __func__, __LINE__, CONFIG_SYS_FPGA_WAIT_INIT);
215 (*fn->abort)(cookie);
216 return FPGA_FAIL;
217 }
218 } while ((*fn->init)(cookie) && (*fn->busy)(cookie));
5d3207da 219
3372081c 220 if (fn->wr)
fa57af05 221 (*fn->wr)(true, true, cookie);
3372081c 222 if (fn->cs)
fa57af05 223 (*fn->cs)(true, true, cookie);
5d3207da 224
3372081c
RH
225 mdelay(10);
226 return FPGA_SUCCESS;
227}
9a9200b4 228
175dccd7 229static int virtex2_slave_post(xilinx_virtex2_slave_fns *fn,
3372081c
RH
230 int cookie)
231{
232 int ret_val = FPGA_SUCCESS;
a0549f73 233 int num_done = 0;
3372081c
RH
234 unsigned long ts;
235
236 /*
237 * Finished writing the data; deassert FPGA CS_B and WRITE_B signals.
238 */
239 CONFIG_FPGA_DELAY();
240 if (fn->cs)
241 (*fn->cs)(false, true, cookie);
242 if (fn->wr)
243 (*fn->wr)(false, true, cookie);
9a9200b4 244
3372081c
RH
245#ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
246 putc('\n');
5d3207da 247#endif
9a9200b4 248
3372081c
RH
249 /*
250 * Check for successful configuration. FPGA INIT_B and DONE
a0549f73
RH
251 * should both be high upon successful configuration. Continue pulsing
252 * clock with data set to all ones until DONE is asserted and for 8
253 * clock cycles afterwards.
3372081c
RH
254 */
255 ts = get_timer(0);
a0549f73
RH
256 while (true) {
257 if ((*fn->done)(cookie) == FPGA_SUCCESS &&
258 !((*fn->init)(cookie))) {
259 if (num_done++ >= 8)
260 break;
261 }
262
3372081c
RH
263 if (get_timer(ts) > CONFIG_SYS_FPGA_WAIT_CONFIG) {
264 printf("%s:%d: ** Timeout after %d ticks waiting for DONE to assert and INIT to deassert\n",
265 __func__, __LINE__, CONFIG_SYS_FPGA_WAIT_CONFIG);
266 (*fn->abort)(cookie);
267 ret_val = FPGA_FAIL;
268 break;
269 }
175dccd7
RH
270 if (fn->wbulkdata) {
271 unsigned char dummy = 0xff;
272 (*fn->wbulkdata)(&dummy, 1, true, cookie);
273 } else {
274 (*fn->wdata)(0xff, true, cookie);
275 CONFIG_FPGA_DELAY();
276 (*fn->clk)(false, true, cookie);
277 CONFIG_FPGA_DELAY();
278 (*fn->clk)(true, true, cookie);
279 }
3372081c 280 }
5d3207da 281
3372081c
RH
282 if (ret_val == FPGA_SUCCESS) {
283#ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
284 printf("Initialization of FPGA device %d complete\n", cookie);
5d3207da 285#endif
3372081c
RH
286 /*
287 * Run the post configuration function if there is one.
288 */
289 if (*fn->post)
290 (*fn->post)(cookie);
291 } else {
6d0f6bcf 292#ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
3372081c
RH
293 printf("** Initialization of FPGA device %d FAILED\n",
294 cookie);
5d3207da 295#endif
3372081c
RH
296 }
297 return ret_val;
298}
299
300static int virtex2_ssm_load(xilinx_desc *desc, const void *buf, size_t bsize)
301{
302 int ret_val = FPGA_FAIL;
175dccd7 303 xilinx_virtex2_slave_fns *fn = desc->iface_fns;
3372081c
RH
304 size_t bytecount = 0;
305 unsigned char *data = (unsigned char *)buf;
306 int cookie = desc->cookie;
307
308 ret_val = virtex2_slave_pre(fn, cookie);
309 if (ret_val != FPGA_SUCCESS)
310 return ret_val;
311
312 /*
313 * Load the data byte by byte
314 */
315 while (bytecount < bsize) {
316#ifdef CONFIG_SYS_FPGA_CHECK_CTRLC
317 if (ctrlc()) {
318 (*fn->abort)(cookie);
319 return FPGA_FAIL;
5d3207da 320 }
3372081c 321#endif
5d3207da 322
3372081c 323 if ((*fn->done)(cookie) == FPGA_SUCCESS) {
63c46e02
AD
324 log_debug("done went active early, bytecount = %zu\n",
325 bytecount);
3372081c
RH
326 break;
327 }
5d3207da 328
3372081c
RH
329#ifdef CONFIG_SYS_FPGA_CHECK_ERROR
330 if ((*fn->init)(cookie)) {
331 printf("\n%s:%d: ** Error: INIT asserted during configuration\n",
332 __func__, __LINE__);
333 printf("%zu = buffer offset, %zu = buffer size\n",
334 bytecount, bsize);
335 (*fn->abort)(cookie);
336 return FPGA_FAIL;
337 }
5d3207da
WD
338#endif
339
3372081c
RH
340 (*fn->wdata)(data[bytecount++], true, cookie);
341 CONFIG_FPGA_DELAY();
342
5d3207da 343 /*
3372081c 344 * Cycle the clock pin
5d3207da 345 */
3372081c
RH
346 (*fn->clk)(false, true, cookie);
347 CONFIG_FPGA_DELAY();
348 (*fn->clk)(true, true, cookie);
349
350#ifdef CONFIG_SYS_FPGA_CHECK_BUSY
fa57af05 351 ts = get_timer(0);
3372081c
RH
352 while ((*fn->busy)(cookie)) {
353 if (get_timer(ts) > CONFIG_SYS_FPGA_WAIT_BUSY) {
354 printf("%s:%d: ** Timeout after %d ticks waiting for BUSY to deassert\n",
fa57af05 355 __func__, __LINE__,
3372081c 356 CONFIG_SYS_FPGA_WAIT_BUSY);
fa57af05 357 (*fn->abort)(cookie);
3372081c 358 return FPGA_FAIL;
5d3207da
WD
359 }
360 }
5d3207da 361#endif
3372081c 362
6d0f6bcf 363#ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
3372081c
RH
364 if (bytecount % (bsize / 40) == 0)
365 putc('.');
5d3207da 366#endif
5d3207da 367 }
3372081c
RH
368
369 return virtex2_slave_post(fn, cookie);
5d3207da
WD
370}
371
372/*
373 * Read the FPGA configuration data
374 */
f8c1be98 375static int virtex2_ssm_dump(xilinx_desc *desc, const void *buf, size_t bsize)
5d3207da
WD
376{
377 int ret_val = FPGA_FAIL;
175dccd7 378 xilinx_virtex2_slave_fns *fn = desc->iface_fns;
5d3207da
WD
379
380 if (fn) {
fa57af05 381 unsigned char *data = (unsigned char *)buf;
5d3207da
WD
382 size_t bytecount = 0;
383 int cookie = desc->cookie;
384
fa57af05 385 printf("Starting Dump of FPGA Device %d...\n", cookie);
5d3207da 386
fa57af05
RH
387 (*fn->cs)(true, true, cookie);
388 (*fn->clk)(true, true, cookie);
5d3207da
WD
389
390 while (bytecount < bsize) {
6d0f6bcf 391#ifdef CONFIG_SYS_FPGA_CHECK_CTRLC
fa57af05
RH
392 if (ctrlc()) {
393 (*fn->abort)(cookie);
5d3207da
WD
394 return FPGA_FAIL;
395 }
396#endif
397 /*
398 * Cycle the clock and read the data
399 */
fa57af05
RH
400 (*fn->clk)(false, true, cookie);
401 (*fn->clk)(true, true, cookie);
402 (*fn->rdata)(&data[bytecount++], cookie);
6d0f6bcf 403#ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
5d3207da 404 if (bytecount % (bsize / 40) == 0)
fa57af05 405 putc('.');
5d3207da
WD
406#endif
407 }
408
409 /*
410 * Deassert CS_B and cycle the clock to deselect the device.
411 */
fa57af05
RH
412 (*fn->cs)(false, false, cookie);
413 (*fn->clk)(false, true, cookie);
414 (*fn->clk)(true, true, cookie);
5d3207da 415
6d0f6bcf 416#ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
fa57af05 417 putc('\n');
5d3207da 418#endif
fa57af05 419 puts("Done.\n");
5d3207da 420 } else {
fa57af05
RH
421 printf("%s:%d: NULL Interface function table!\n",
422 __func__, __LINE__);
5d3207da
WD
423 }
424 return ret_val;
425}
426
f8c1be98 427static int virtex2_ss_load(xilinx_desc *desc, const void *buf, size_t bsize)
5d3207da 428{
175dccd7
RH
429 int ret_val = FPGA_FAIL;
430 xilinx_virtex2_slave_fns *fn = desc->iface_fns;
431 unsigned char *data = (unsigned char *)buf;
432 int cookie = desc->cookie;
433
434 ret_val = virtex2_slave_pre(fn, cookie);
435 if (ret_val != FPGA_SUCCESS)
436 return ret_val;
437
438 if (fn->wbulkdata) {
439 /* Load the data in a single chunk */
440 (*fn->wbulkdata)(data, bsize, true, cookie);
441 } else {
442 size_t bytecount = 0;
443
444 /*
445 * Load the data bit by bit
446 */
447 while (bytecount < bsize) {
448 unsigned char curr_data = data[bytecount++];
449 int bit;
450
451#ifdef CONFIG_SYS_FPGA_CHECK_CTRLC
452 if (ctrlc()) {
453 (*fn->abort) (cookie);
454 return FPGA_FAIL;
455 }
456#endif
457
458 if ((*fn->done)(cookie) == FPGA_SUCCESS) {
63c46e02
AD
459 log_debug("done went active early, bytecount = %zu\n",
460 bytecount);
175dccd7
RH
461 break;
462 }
463
464#ifdef CONFIG_SYS_FPGA_CHECK_ERROR
465 if ((*fn->init)(cookie)) {
466 printf("\n%s:%d: ** Error: INIT asserted during configuration\n",
467 __func__, __LINE__);
468 printf("%zu = buffer offset, %zu = buffer size\n",
469 bytecount, bsize);
470 (*fn->abort)(cookie);
471 return FPGA_FAIL;
472 }
473#endif
474
475 for (bit = 7; bit >= 0; --bit) {
476 unsigned char curr_bit = (curr_data >> bit) & 1;
477 (*fn->wdata)(curr_bit, true, cookie);
478 CONFIG_FPGA_DELAY();
479 (*fn->clk)(false, true, cookie);
480 CONFIG_FPGA_DELAY();
481 (*fn->clk)(true, true, cookie);
482 }
483
484 /* Slave serial never uses a busy pin */
485
486#ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
487 if (bytecount % (bsize / 40) == 0)
488 putc('.');
489#endif
490 }
491 }
492
493 return virtex2_slave_post(fn, cookie);
5d3207da
WD
494}
495
f8c1be98 496static int virtex2_ss_dump(xilinx_desc *desc, const void *buf, size_t bsize)
5d3207da 497{
fa57af05 498 printf("%s: Slave Serial Dumping is unsupported\n", __func__);
5d3207da
WD
499 return FPGA_FAIL;
500}
501
5d3207da 502/* vim: set ts=4 tw=78: */
14cfc4f3
MS
503
504struct xilinx_fpga_op virtex2_op = {
505 .load = virtex2_load,
506 .dump = virtex2_dump,
507 .info = virtex2_info,
508};
This page took 0.61244 seconds and 4 git commands to generate.