1 // SPDX-License-Identifier: GPL-2.0+
4 * Rich Ireland, Enterasys Networks, rireland@enterasys.com.
7 #define LOG_CATEGORY UCLASS_FPGA
9 #include <common.h> /* core U-Boot definitions */
11 #include <spartan2.h> /* Spartan-II device family */
13 #undef CONFIG_SYS_FPGA_CHECK_BUSY
15 /* Note: The assumption is that we cannot possibly run fast enough to
16 * overrun the device (the Slave Parallel mode can free run at 50MHz).
17 * If there is a need to operate slower, define CONFIG_FPGA_DELAY in
18 * the board config file to slow things down.
20 #ifndef CONFIG_FPGA_DELAY
21 #define CONFIG_FPGA_DELAY()
24 #ifndef CONFIG_SYS_FPGA_WAIT
25 #define CONFIG_SYS_FPGA_WAIT CONFIG_SYS_HZ/100 /* 10 ms */
28 static int spartan2_sp_load(xilinx_desc *desc, const void *buf, size_t bsize);
29 static int spartan2_sp_dump(xilinx_desc *desc, const void *buf, size_t bsize);
30 /* static int spartan2_sp_info(xilinx_desc *desc ); */
32 static int spartan2_ss_load(xilinx_desc *desc, const void *buf, size_t bsize);
33 static int spartan2_ss_dump(xilinx_desc *desc, const void *buf, size_t bsize);
34 /* static int spartan2_ss_info(xilinx_desc *desc ); */
36 /* ------------------------------------------------------------------------- */
37 /* Spartan-II Generic Implementation */
38 static int spartan2_load(xilinx_desc *desc, const void *buf, size_t bsize,
39 bitstream_type bstype, int flags)
41 int ret_val = FPGA_FAIL;
43 switch (desc->iface) {
45 log_debug("Launching Slave Serial Load\n");
46 ret_val = spartan2_ss_load(desc, buf, bsize);
50 log_debug("Launching Slave Parallel Load\n");
51 ret_val = spartan2_sp_load(desc, buf, bsize);
55 printf ("%s: Unsupported interface type, %d\n",
56 __FUNCTION__, desc->iface);
62 static int spartan2_dump(xilinx_desc *desc, const void *buf, size_t bsize)
64 int ret_val = FPGA_FAIL;
66 switch (desc->iface) {
68 log_debug("Launching Slave Serial Dump\n");
69 ret_val = spartan2_ss_dump(desc, buf, bsize);
73 log_debug("Launching Slave Parallel Dump\n");
74 ret_val = spartan2_sp_dump(desc, buf, bsize);
78 printf ("%s: Unsupported interface type, %d\n",
79 __FUNCTION__, desc->iface);
85 static int spartan2_info(xilinx_desc *desc)
91 /* ------------------------------------------------------------------------- */
92 /* Spartan-II Slave Parallel Generic Implementation */
94 static int spartan2_sp_load(xilinx_desc *desc, const void *buf, size_t bsize)
96 int ret_val = FPGA_FAIL; /* assume the worst */
97 xilinx_spartan2_slave_parallel_fns *fn = desc->iface_fns;
99 log_debug("start with interface functions @ 0x%p\n", fn);
102 size_t bytecount = 0;
103 unsigned char *data = (unsigned char *) buf;
104 int cookie = desc->cookie; /* make a local copy */
105 unsigned long ts; /* timestamp */
107 log_debug("Function Table:\n"
118 "write data:\t0x%p\n"
122 &fn, fn, fn->pre, fn->pgm, fn->init, fn->err,
123 fn->clk, fn->cs, fn->wr, fn->rdata, fn->wdata, fn->busy,
124 fn->abort, fn->post);
127 * This code is designed to emulate the "Express Style"
128 * Continuous Data Loading in Slave Parallel Mode for
129 * the Spartan-II Family.
131 #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
132 printf ("Loading FPGA Device %d...\n", cookie);
135 * Run the pre configuration function if there is one.
141 /* Establish the initial state */
142 (*fn->pgm) (true, true, cookie); /* Assert the program, commit */
144 /* Get ready for the burn */
145 CONFIG_FPGA_DELAY ();
146 (*fn->pgm) (false, true, cookie); /* Deassert the program, commit */
148 ts = get_timer (0); /* get current time */
149 /* Now wait for INIT and BUSY to go high */
151 CONFIG_FPGA_DELAY ();
152 if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT) { /* check the time */
153 puts ("** Timeout waiting for INIT to clear.\n");
154 (*fn->abort) (cookie); /* abort the burn */
157 } while ((*fn->init) (cookie) && (*fn->busy) (cookie));
159 (*fn->wr) (true, true, cookie); /* Assert write, commit */
160 (*fn->cs) (true, true, cookie); /* Assert chip select, commit */
161 (*fn->clk) (true, true, cookie); /* Assert the clock pin */
164 while (bytecount < bsize) {
165 /* XXX - do we check for an Ctrl-C press in here ??? */
166 /* XXX - Check the error bit? */
168 (*fn->wdata) (data[bytecount++], true, cookie); /* write the data */
169 CONFIG_FPGA_DELAY ();
170 (*fn->clk) (false, true, cookie); /* Deassert the clock pin */
171 CONFIG_FPGA_DELAY ();
172 (*fn->clk) (true, true, cookie); /* Assert the clock pin */
174 #ifdef CONFIG_SYS_FPGA_CHECK_BUSY
175 ts = get_timer (0); /* get current time */
176 while ((*fn->busy) (cookie)) {
177 /* XXX - we should have a check in here somewhere to
178 * make sure we aren't busy forever... */
180 CONFIG_FPGA_DELAY ();
181 (*fn->clk) (false, true, cookie); /* Deassert the clock pin */
182 CONFIG_FPGA_DELAY ();
183 (*fn->clk) (true, true, cookie); /* Assert the clock pin */
185 if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT) { /* check the time */
186 puts ("** Timeout waiting for BUSY to clear.\n");
187 (*fn->abort) (cookie); /* abort the burn */
193 #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
194 if (bytecount % (bsize / 40) == 0)
195 putc ('.'); /* let them know we are alive */
199 CONFIG_FPGA_DELAY ();
200 (*fn->cs) (false, true, cookie); /* Deassert the chip select */
201 (*fn->wr) (false, true, cookie); /* Deassert the write pin */
203 #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
204 putc ('\n'); /* terminate the dotted line */
207 /* now check for done signal */
208 ts = get_timer (0); /* get current time */
209 ret_val = FPGA_SUCCESS;
210 while ((*fn->done) (cookie) == FPGA_FAIL) {
212 CONFIG_FPGA_DELAY ();
213 (*fn->clk) (false, true, cookie); /* Deassert the clock pin */
214 CONFIG_FPGA_DELAY ();
215 (*fn->clk) (true, true, cookie); /* Assert the clock pin */
217 if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT) { /* check the time */
218 puts ("** Timeout waiting for DONE to clear.\n");
219 (*fn->abort) (cookie); /* abort the burn */
226 * Run the post configuration function if there is one.
229 (*fn->post) (cookie);
231 #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
232 if (ret_val == FPGA_SUCCESS)
239 printf ("%s: NULL Interface function table!\n", __FUNCTION__);
245 static int spartan2_sp_dump(xilinx_desc *desc, const void *buf, size_t bsize)
247 int ret_val = FPGA_FAIL; /* assume the worst */
248 xilinx_spartan2_slave_parallel_fns *fn = desc->iface_fns;
251 unsigned char *data = (unsigned char *) buf;
252 size_t bytecount = 0;
253 int cookie = desc->cookie; /* make a local copy */
255 printf ("Starting Dump of FPGA Device %d...\n", cookie);
257 (*fn->cs) (true, true, cookie); /* Assert chip select, commit */
258 (*fn->clk) (true, true, cookie); /* Assert the clock pin */
261 while (bytecount < bsize) {
262 /* XXX - do we check for an Ctrl-C press in here ??? */
264 (*fn->clk) (false, true, cookie); /* Deassert the clock pin */
265 (*fn->clk) (true, true, cookie); /* Assert the clock pin */
266 (*fn->rdata) (&(data[bytecount++]), cookie); /* read the data */
267 #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
268 if (bytecount % (bsize / 40) == 0)
269 putc ('.'); /* let them know we are alive */
273 (*fn->cs) (false, false, cookie); /* Deassert the chip select */
274 (*fn->clk) (false, true, cookie); /* Deassert the clock pin */
275 (*fn->clk) (true, true, cookie); /* Assert the clock pin */
277 #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
278 putc ('\n'); /* terminate the dotted line */
282 /* XXX - checksum the data? */
284 printf ("%s: NULL Interface function table!\n", __FUNCTION__);
291 /* ------------------------------------------------------------------------- */
293 static int spartan2_ss_load(xilinx_desc *desc, const void *buf, size_t bsize)
295 int ret_val = FPGA_FAIL; /* assume the worst */
296 xilinx_spartan2_slave_serial_fns *fn = desc->iface_fns;
300 log_debug("start with interface functions @ 0x%p\n", fn);
303 size_t bytecount = 0;
304 unsigned char *data = (unsigned char *) buf;
305 int cookie = desc->cookie; /* make a local copy */
306 unsigned long ts; /* timestamp */
308 log_debug("Function Table:\n"
316 &fn, fn, fn->pgm, fn->init,
317 fn->clk, fn->wr, fn->done);
318 #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
319 printf ("Loading FPGA Device %d...\n", cookie);
323 * Run the pre configuration function if there is one.
329 /* Establish the initial state */
330 (*fn->pgm) (true, true, cookie); /* Assert the program, commit */
332 /* Wait for INIT state (init low) */
333 ts = get_timer (0); /* get current time */
335 CONFIG_FPGA_DELAY ();
336 if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT) { /* check the time */
337 puts ("** Timeout waiting for INIT to start.\n");
340 } while (!(*fn->init) (cookie));
342 /* Get ready for the burn */
343 CONFIG_FPGA_DELAY ();
344 (*fn->pgm) (false, true, cookie); /* Deassert the program, commit */
346 ts = get_timer (0); /* get current time */
347 /* Now wait for INIT to go high */
349 CONFIG_FPGA_DELAY ();
350 if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT) { /* check the time */
351 puts ("** Timeout waiting for INIT to clear.\n");
354 } while ((*fn->init) (cookie));
357 while (bytecount < bsize) {
359 /* Xilinx detects an error if INIT goes low (active)
360 while DONE is low (inactive) */
361 if ((*fn->done) (cookie) == 0 && (*fn->init) (cookie)) {
362 puts ("** CRC error during FPGA load.\n");
365 val = data [bytecount ++];
368 /* Deassert the clock */
369 (*fn->clk) (false, true, cookie);
370 CONFIG_FPGA_DELAY ();
372 (*fn->wr) ((val & 0x80), true, cookie);
373 CONFIG_FPGA_DELAY ();
374 /* Assert the clock */
375 (*fn->clk) (true, true, cookie);
376 CONFIG_FPGA_DELAY ();
381 #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
382 if (bytecount % (bsize / 40) == 0)
383 putc ('.'); /* let them know we are alive */
387 CONFIG_FPGA_DELAY ();
389 #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
390 putc ('\n'); /* terminate the dotted line */
393 /* now check for done signal */
394 ts = get_timer (0); /* get current time */
395 ret_val = FPGA_SUCCESS;
396 (*fn->wr) (true, true, cookie);
398 while (! (*fn->done) (cookie)) {
400 CONFIG_FPGA_DELAY ();
401 (*fn->clk) (false, true, cookie); /* Deassert the clock pin */
402 CONFIG_FPGA_DELAY ();
403 (*fn->clk) (true, true, cookie); /* Assert the clock pin */
407 if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT) { /* check the time */
408 puts ("** Timeout waiting for DONE to clear.\n");
413 putc ('\n'); /* terminate the dotted line */
416 * Run the post configuration function if there is one.
419 (*fn->post) (cookie);
421 #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
422 if (ret_val == FPGA_SUCCESS)
429 printf ("%s: NULL Interface function table!\n", __FUNCTION__);
435 static int spartan2_ss_dump(xilinx_desc *desc, const void *buf, size_t bsize)
437 /* Readback is only available through the Slave Parallel and */
438 /* boundary-scan interfaces. */
439 printf ("%s: Slave Serial Dumping is unavailable\n",
444 struct xilinx_fpga_op spartan2_op = {
445 .load = spartan2_load,
446 .dump = spartan2_dump,
447 .info = spartan2_info,