1 // SPDX-License-Identifier: GPL-2.0+
10 #define LOG_CATEGORY UCLASS_FPGA
12 #include <config.h> /* core U-Boot definitions */
15 #include <ACEX1K.h> /* ACEX device family */
16 #include <linux/delay.h>
18 /* Note: The assumption is that we cannot possibly run fast enough to
19 * overrun the device (the Slave Parallel mode can free run at 50MHz).
20 * If there is a need to operate slower, define CFG_FPGA_DELAY in
21 * the board config file to slow things down.
23 #ifndef CFG_FPGA_DELAY
24 #define CFG_FPGA_DELAY()
27 #ifndef CFG_SYS_FPGA_WAIT
28 #define CFG_SYS_FPGA_WAIT CONFIG_SYS_HZ/10 /* 100 ms */
31 static int ACEX1K_ps_load(Altera_desc *desc, const void *buf, size_t bsize);
32 static int ACEX1K_ps_dump(Altera_desc *desc, const void *buf, size_t bsize);
33 /* static int ACEX1K_ps_info(Altera_desc *desc); */
35 /* ------------------------------------------------------------------------- */
36 /* ACEX1K Generic Implementation */
37 int ACEX1K_load(Altera_desc *desc, const void *buf, size_t bsize)
39 int ret_val = FPGA_FAIL;
41 switch (desc->iface) {
43 log_debug("Launching Passive Serial Loader\n");
44 ret_val = ACEX1K_ps_load (desc, buf, bsize);
47 /* Add new interface types here */
50 printf ("%s: Unsupported interface type, %d\n",
51 __FUNCTION__, desc->iface);
57 int ACEX1K_dump(Altera_desc *desc, const void *buf, size_t bsize)
59 int ret_val = FPGA_FAIL;
61 switch (desc->iface) {
63 log_debug("Launching Passive Serial Dump\n");
64 ret_val = ACEX1K_ps_dump (desc, buf, bsize);
67 /* Add new interface types here */
70 printf ("%s: Unsupported interface type, %d\n",
71 __FUNCTION__, desc->iface);
77 int ACEX1K_info( Altera_desc *desc )
82 /* ------------------------------------------------------------------------- */
83 /* ACEX1K Passive Serial Generic Implementation */
85 static int ACEX1K_ps_load(Altera_desc *desc, const void *buf, size_t bsize)
87 int ret_val = FPGA_FAIL; /* assume the worst */
88 Altera_ACEX1K_Passive_Serial_fns *fn = desc->iface_fns;
91 log_debug("start with interface functions @ 0x%p\n", fn);
95 unsigned char *data = (unsigned char *) buf;
96 int cookie = desc->cookie; /* make a local copy */
97 unsigned long ts; /* timestamp */
99 log_debug("Function Table:\n"
107 &fn, fn, fn->config, fn->status,
108 fn->clk, fn->data, fn->done);
109 #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
110 printf ("Loading FPGA Device %d...", cookie);
114 * Run the pre configuration function if there is one.
120 /* Establish the initial state */
121 (*fn->config) (true, true, cookie); /* Assert nCONFIG */
123 udelay(2); /* T_cfg > 2us */
125 /* nSTATUS should be asserted now */
126 (*fn->done) (cookie);
127 if ( !(*fn->status) (cookie) ) {
128 puts ("** nSTATUS is not asserted.\n");
129 (*fn->abort) (cookie);
133 (*fn->config) (false, true, cookie); /* Deassert nCONFIG */
134 udelay(2); /* T_cf2st1 < 4us */
136 /* Wait for nSTATUS to be released (i.e. deasserted) */
137 ts = get_timer (0); /* get current time */
140 if (get_timer (ts) > CFG_SYS_FPGA_WAIT) { /* check the time */
141 puts ("** Timeout waiting for STATUS to go high.\n");
142 (*fn->abort) (cookie);
145 (*fn->done) (cookie);
146 } while ((*fn->status) (cookie));
148 /* Get ready for the burn */
152 while (bytecount < bsize) {
154 #ifdef CONFIG_SYS_FPGA_CHECK_CTRLC
156 (*fn->abort) (cookie);
160 /* Altera detects an error if INIT goes low (active)
161 while DONE is low (inactive) */
162 #if 0 /* not yet implemented */
163 if ((*fn->done) (cookie) == 0 && (*fn->init) (cookie)) {
164 puts ("** CRC error during FPGA load.\n");
165 (*fn->abort) (cookie);
169 val = data [bytecount ++ ];
172 /* Deassert the clock */
173 (*fn->clk) (false, true, cookie);
176 (*fn->data) ((val & 0x01), true, cookie);
178 /* Assert the clock */
179 (*fn->clk) (true, true, cookie);
185 #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
186 if (bytecount % (bsize / 40) == 0)
187 putc ('.'); /* let them know we are alive */
193 #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
194 putc (' '); /* terminate the dotted line */
198 * Checking FPGA's CONF_DONE signal - correctly booted ?
201 if ( ! (*fn->done) (cookie) ) {
202 puts ("** Booting failed! CONF_DONE is still deasserted.\n");
203 (*fn->abort) (cookie);
208 * "DCLK must be clocked an additional 10 times fpr ACEX 1K..."
211 for (i = 0; i < 12; i++) {
213 (*fn->clk) (true, true, cookie); /* Assert the clock pin */
215 (*fn->clk) (false, true, cookie); /* Deassert the clock pin */
218 ret_val = FPGA_SUCCESS;
220 #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
221 if (ret_val == FPGA_SUCCESS) {
228 (*fn->post) (cookie);
231 printf ("%s: NULL Interface function table!\n", __FUNCTION__);
237 static int ACEX1K_ps_dump(Altera_desc *desc, const void *buf, size_t bsize)
239 /* Readback is only available through the Slave Parallel and */
240 /* boundary-scan interfaces. */
241 printf ("%s: Passive Serial Dumping is unavailable\n",