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