1 // SPDX-License-Identifier: GPL-2.0+
4 * Rich Ireland, Enterasys Networks, rireland@enterasys.com.
7 #include <common.h> /* core U-Boot definitions */
8 #include <spartan2.h> /* Spartan-II device family */
10 /* Define FPGA_DEBUG to get debug printf's */
12 #define PRINTF(fmt,args...) printf (fmt ,##args)
14 #define PRINTF(fmt,args...)
17 #undef CONFIG_SYS_FPGA_CHECK_BUSY
18 #undef CONFIG_SYS_FPGA_PROG_FEEDBACK
20 /* Note: The assumption is that we cannot possibly run fast enough to
21 * overrun the device (the Slave Parallel mode can free run at 50MHz).
22 * If there is a need to operate slower, define CONFIG_FPGA_DELAY in
23 * the board config file to slow things down.
25 #ifndef CONFIG_FPGA_DELAY
26 #define CONFIG_FPGA_DELAY()
29 #ifndef CONFIG_SYS_FPGA_WAIT
30 #define CONFIG_SYS_FPGA_WAIT CONFIG_SYS_HZ/100 /* 10 ms */
33 static int spartan2_sp_load(xilinx_desc *desc, const void *buf, size_t bsize);
34 static int spartan2_sp_dump(xilinx_desc *desc, const void *buf, size_t bsize);
35 /* static int spartan2_sp_info(xilinx_desc *desc ); */
37 static int spartan2_ss_load(xilinx_desc *desc, const void *buf, size_t bsize);
38 static int spartan2_ss_dump(xilinx_desc *desc, const void *buf, size_t bsize);
39 /* static int spartan2_ss_info(xilinx_desc *desc ); */
41 /* ------------------------------------------------------------------------- */
42 /* Spartan-II Generic Implementation */
43 static int spartan2_load(xilinx_desc *desc, const void *buf, size_t bsize,
44 bitstream_type bstype)
46 int ret_val = FPGA_FAIL;
48 switch (desc->iface) {
50 PRINTF ("%s: Launching Slave Serial Load\n", __FUNCTION__);
51 ret_val = spartan2_ss_load(desc, buf, bsize);
55 PRINTF ("%s: Launching Slave Parallel Load\n", __FUNCTION__);
56 ret_val = spartan2_sp_load(desc, buf, bsize);
60 printf ("%s: Unsupported interface type, %d\n",
61 __FUNCTION__, desc->iface);
67 static int spartan2_dump(xilinx_desc *desc, const void *buf, size_t bsize)
69 int ret_val = FPGA_FAIL;
71 switch (desc->iface) {
73 PRINTF ("%s: Launching Slave Serial Dump\n", __FUNCTION__);
74 ret_val = spartan2_ss_dump(desc, buf, bsize);
78 PRINTF ("%s: Launching Slave Parallel Dump\n", __FUNCTION__);
79 ret_val = spartan2_sp_dump(desc, buf, bsize);
83 printf ("%s: Unsupported interface type, %d\n",
84 __FUNCTION__, desc->iface);
90 static int spartan2_info(xilinx_desc *desc)
96 /* ------------------------------------------------------------------------- */
97 /* Spartan-II Slave Parallel Generic Implementation */
99 static int spartan2_sp_load(xilinx_desc *desc, const void *buf, size_t bsize)
101 int ret_val = FPGA_FAIL; /* assume the worst */
102 xilinx_spartan2_slave_parallel_fns *fn = desc->iface_fns;
104 PRINTF ("%s: start with interface functions @ 0x%p\n",
108 size_t bytecount = 0;
109 unsigned char *data = (unsigned char *) buf;
110 int cookie = desc->cookie; /* make a local copy */
111 unsigned long ts; /* timestamp */
113 PRINTF ("%s: Function Table:\n"
124 "write data:\t0x%p\n"
128 __FUNCTION__, &fn, fn, fn->pre, fn->pgm, fn->init, fn->err,
129 fn->clk, fn->cs, fn->wr, fn->rdata, fn->wdata, fn->busy,
130 fn->abort, fn->post);
133 * This code is designed to emulate the "Express Style"
134 * Continuous Data Loading in Slave Parallel Mode for
135 * the Spartan-II Family.
137 #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
138 printf ("Loading FPGA Device %d...\n", cookie);
141 * Run the pre configuration function if there is one.
147 /* Establish the initial state */
148 (*fn->pgm) (true, true, cookie); /* Assert the program, commit */
150 /* Get ready for the burn */
151 CONFIG_FPGA_DELAY ();
152 (*fn->pgm) (false, true, cookie); /* Deassert the program, commit */
154 ts = get_timer (0); /* get current time */
155 /* Now wait for INIT and BUSY to go high */
157 CONFIG_FPGA_DELAY ();
158 if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT) { /* check the time */
159 puts ("** Timeout waiting for INIT to clear.\n");
160 (*fn->abort) (cookie); /* abort the burn */
163 } while ((*fn->init) (cookie) && (*fn->busy) (cookie));
165 (*fn->wr) (true, true, cookie); /* Assert write, commit */
166 (*fn->cs) (true, true, cookie); /* Assert chip select, commit */
167 (*fn->clk) (true, true, cookie); /* Assert the clock pin */
170 while (bytecount < bsize) {
171 /* XXX - do we check for an Ctrl-C press in here ??? */
172 /* XXX - Check the error bit? */
174 (*fn->wdata) (data[bytecount++], true, cookie); /* write the data */
175 CONFIG_FPGA_DELAY ();
176 (*fn->clk) (false, true, cookie); /* Deassert the clock pin */
177 CONFIG_FPGA_DELAY ();
178 (*fn->clk) (true, true, cookie); /* Assert the clock pin */
180 #ifdef CONFIG_SYS_FPGA_CHECK_BUSY
181 ts = get_timer (0); /* get current time */
182 while ((*fn->busy) (cookie)) {
183 /* XXX - we should have a check in here somewhere to
184 * make sure we aren't busy forever... */
186 CONFIG_FPGA_DELAY ();
187 (*fn->clk) (false, true, cookie); /* Deassert the clock pin */
188 CONFIG_FPGA_DELAY ();
189 (*fn->clk) (true, true, cookie); /* Assert the clock pin */
191 if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT) { /* check the time */
192 puts ("** Timeout waiting for BUSY to clear.\n");
193 (*fn->abort) (cookie); /* abort the burn */
199 #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
200 if (bytecount % (bsize / 40) == 0)
201 putc ('.'); /* let them know we are alive */
205 CONFIG_FPGA_DELAY ();
206 (*fn->cs) (false, true, cookie); /* Deassert the chip select */
207 (*fn->wr) (false, true, cookie); /* Deassert the write pin */
209 #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
210 putc ('\n'); /* terminate the dotted line */
213 /* now check for done signal */
214 ts = get_timer (0); /* get current time */
215 ret_val = FPGA_SUCCESS;
216 while ((*fn->done) (cookie) == FPGA_FAIL) {
218 CONFIG_FPGA_DELAY ();
219 (*fn->clk) (false, true, cookie); /* Deassert the clock pin */
220 CONFIG_FPGA_DELAY ();
221 (*fn->clk) (true, true, cookie); /* Assert the clock pin */
223 if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT) { /* check the time */
224 puts ("** Timeout waiting for DONE to clear.\n");
225 (*fn->abort) (cookie); /* abort the burn */
232 * Run the post configuration function if there is one.
235 (*fn->post) (cookie);
237 #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
238 if (ret_val == FPGA_SUCCESS)
245 printf ("%s: NULL Interface function table!\n", __FUNCTION__);
251 static int spartan2_sp_dump(xilinx_desc *desc, const void *buf, size_t bsize)
253 int ret_val = FPGA_FAIL; /* assume the worst */
254 xilinx_spartan2_slave_parallel_fns *fn = desc->iface_fns;
257 unsigned char *data = (unsigned char *) buf;
258 size_t bytecount = 0;
259 int cookie = desc->cookie; /* make a local copy */
261 printf ("Starting Dump of FPGA Device %d...\n", cookie);
263 (*fn->cs) (true, true, cookie); /* Assert chip select, commit */
264 (*fn->clk) (true, true, cookie); /* Assert the clock pin */
267 while (bytecount < bsize) {
268 /* XXX - do we check for an Ctrl-C press in here ??? */
270 (*fn->clk) (false, true, cookie); /* Deassert the clock pin */
271 (*fn->clk) (true, true, cookie); /* Assert the clock pin */
272 (*fn->rdata) (&(data[bytecount++]), cookie); /* read the data */
273 #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
274 if (bytecount % (bsize / 40) == 0)
275 putc ('.'); /* let them know we are alive */
279 (*fn->cs) (false, false, cookie); /* Deassert the chip select */
280 (*fn->clk) (false, true, cookie); /* Deassert the clock pin */
281 (*fn->clk) (true, true, cookie); /* Assert the clock pin */
283 #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
284 putc ('\n'); /* terminate the dotted line */
288 /* XXX - checksum the data? */
290 printf ("%s: NULL Interface function table!\n", __FUNCTION__);
297 /* ------------------------------------------------------------------------- */
299 static int spartan2_ss_load(xilinx_desc *desc, const void *buf, size_t bsize)
301 int ret_val = FPGA_FAIL; /* assume the worst */
302 xilinx_spartan2_slave_serial_fns *fn = desc->iface_fns;
306 PRINTF ("%s: start with interface functions @ 0x%p\n",
310 size_t bytecount = 0;
311 unsigned char *data = (unsigned char *) buf;
312 int cookie = desc->cookie; /* make a local copy */
313 unsigned long ts; /* timestamp */
315 PRINTF ("%s: Function Table:\n"
323 __FUNCTION__, &fn, fn, fn->pgm, fn->init,
324 fn->clk, fn->wr, fn->done);
325 #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
326 printf ("Loading FPGA Device %d...\n", cookie);
330 * Run the pre configuration function if there is one.
336 /* Establish the initial state */
337 (*fn->pgm) (true, true, cookie); /* Assert the program, commit */
339 /* Wait for INIT state (init low) */
340 ts = get_timer (0); /* get current time */
342 CONFIG_FPGA_DELAY ();
343 if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT) { /* check the time */
344 puts ("** Timeout waiting for INIT to start.\n");
347 } while (!(*fn->init) (cookie));
349 /* Get ready for the burn */
350 CONFIG_FPGA_DELAY ();
351 (*fn->pgm) (false, true, cookie); /* Deassert the program, commit */
353 ts = get_timer (0); /* get current time */
354 /* Now wait for INIT to go high */
356 CONFIG_FPGA_DELAY ();
357 if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT) { /* check the time */
358 puts ("** Timeout waiting for INIT to clear.\n");
361 } while ((*fn->init) (cookie));
364 while (bytecount < bsize) {
366 /* Xilinx detects an error if INIT goes low (active)
367 while DONE is low (inactive) */
368 if ((*fn->done) (cookie) == 0 && (*fn->init) (cookie)) {
369 puts ("** CRC error during FPGA load.\n");
372 val = data [bytecount ++];
375 /* Deassert the clock */
376 (*fn->clk) (false, true, cookie);
377 CONFIG_FPGA_DELAY ();
379 (*fn->wr) ((val & 0x80), true, cookie);
380 CONFIG_FPGA_DELAY ();
381 /* Assert the clock */
382 (*fn->clk) (true, true, cookie);
383 CONFIG_FPGA_DELAY ();
388 #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
389 if (bytecount % (bsize / 40) == 0)
390 putc ('.'); /* let them know we are alive */
394 CONFIG_FPGA_DELAY ();
396 #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
397 putc ('\n'); /* terminate the dotted line */
400 /* now check for done signal */
401 ts = get_timer (0); /* get current time */
402 ret_val = FPGA_SUCCESS;
403 (*fn->wr) (true, true, cookie);
405 while (! (*fn->done) (cookie)) {
407 CONFIG_FPGA_DELAY ();
408 (*fn->clk) (false, true, cookie); /* Deassert the clock pin */
409 CONFIG_FPGA_DELAY ();
410 (*fn->clk) (true, true, cookie); /* Assert the clock pin */
414 if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT) { /* check the time */
415 puts ("** Timeout waiting for DONE to clear.\n");
420 putc ('\n'); /* terminate the dotted line */
423 * Run the post configuration function if there is one.
426 (*fn->post) (cookie);
428 #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
429 if (ret_val == FPGA_SUCCESS)
436 printf ("%s: NULL Interface function table!\n", __FUNCTION__);
442 static int spartan2_ss_dump(xilinx_desc *desc, const void *buf, size_t bsize)
444 /* Readback is only available through the Slave Parallel and */
445 /* boundary-scan interfaces. */
446 printf ("%s: Slave Serial Dumping is unavailable\n",
451 struct xilinx_fpga_op spartan2_op = {
452 .load = spartan2_load,
453 .dump = spartan2_dump,
454 .info = spartan2_info,