Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2003 |
| 4 | * Steven Scholz, imc Measurement & Control, steven.scholz@imc-berlin.de |
| 5 | * |
| 6 | * (C) Copyright 2002 |
| 7 | * Rich Ireland, Enterasys Networks, rireland@enterasys.com. |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Alexander Dahl | 7178d0e | 2022-10-07 14:20:00 +0200 | [diff] [blame] | 10 | #define LOG_CATEGORY UCLASS_FPGA |
| 11 | |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 12 | #include <config.h> /* core U-Boot definitions */ |
Simon Glass | a73bda4 | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 13 | #include <console.h> |
Alexander Dahl | 7178d0e | 2022-10-07 14:20:00 +0200 | [diff] [blame] | 14 | #include <log.h> |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 15 | #include <ACEX1K.h> /* ACEX device family */ |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 16 | #include <linux/delay.h> |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 17 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 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). |
Tom Rini | 88d86ec | 2022-12-04 10:03:57 -0500 | [diff] [blame] | 20 | * If there is a need to operate slower, define CFG_FPGA_DELAY in |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 21 | * the board config file to slow things down. |
| 22 | */ |
Tom Rini | 88d86ec | 2022-12-04 10:03:57 -0500 | [diff] [blame] | 23 | #ifndef CFG_FPGA_DELAY |
| 24 | #define CFG_FPGA_DELAY() |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 25 | #endif |
| 26 | |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 27 | #ifndef CFG_SYS_FPGA_WAIT |
| 28 | #define CFG_SYS_FPGA_WAIT CONFIG_SYS_HZ/10 /* 100 ms */ |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 29 | #endif |
| 30 | |
Wolfgang Denk | 74f9b38 | 2011-07-30 13:33:49 +0000 | [diff] [blame] | 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); */ |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 34 | |
| 35 | /* ------------------------------------------------------------------------- */ |
| 36 | /* ACEX1K Generic Implementation */ |
Wolfgang Denk | 74f9b38 | 2011-07-30 13:33:49 +0000 | [diff] [blame] | 37 | int ACEX1K_load(Altera_desc *desc, const void *buf, size_t bsize) |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 38 | { |
| 39 | int ret_val = FPGA_FAIL; |
| 40 | |
| 41 | switch (desc->iface) { |
| 42 | case passive_serial: |
Alexander Dahl | 7178d0e | 2022-10-07 14:20:00 +0200 | [diff] [blame] | 43 | log_debug("Launching Passive Serial Loader\n"); |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 44 | ret_val = ACEX1K_ps_load (desc, buf, bsize); |
| 45 | break; |
| 46 | |
| 47 | /* Add new interface types here */ |
| 48 | |
| 49 | default: |
| 50 | printf ("%s: Unsupported interface type, %d\n", |
| 51 | __FUNCTION__, desc->iface); |
| 52 | } |
| 53 | |
| 54 | return ret_val; |
| 55 | } |
| 56 | |
Wolfgang Denk | 74f9b38 | 2011-07-30 13:33:49 +0000 | [diff] [blame] | 57 | int ACEX1K_dump(Altera_desc *desc, const void *buf, size_t bsize) |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 58 | { |
| 59 | int ret_val = FPGA_FAIL; |
| 60 | |
| 61 | switch (desc->iface) { |
| 62 | case passive_serial: |
Alexander Dahl | 7178d0e | 2022-10-07 14:20:00 +0200 | [diff] [blame] | 63 | log_debug("Launching Passive Serial Dump\n"); |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 64 | ret_val = ACEX1K_ps_dump (desc, buf, bsize); |
| 65 | break; |
| 66 | |
| 67 | /* Add new interface types here */ |
| 68 | |
| 69 | default: |
| 70 | printf ("%s: Unsupported interface type, %d\n", |
| 71 | __FUNCTION__, desc->iface); |
| 72 | } |
| 73 | |
| 74 | return ret_val; |
| 75 | } |
| 76 | |
| 77 | int ACEX1K_info( Altera_desc *desc ) |
| 78 | { |
| 79 | return FPGA_SUCCESS; |
| 80 | } |
| 81 | |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 82 | /* ------------------------------------------------------------------------- */ |
| 83 | /* ACEX1K Passive Serial Generic Implementation */ |
| 84 | |
Wolfgang Denk | 74f9b38 | 2011-07-30 13:33:49 +0000 | [diff] [blame] | 85 | static int ACEX1K_ps_load(Altera_desc *desc, const void *buf, size_t bsize) |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 86 | { |
| 87 | int ret_val = FPGA_FAIL; /* assume the worst */ |
| 88 | Altera_ACEX1K_Passive_Serial_fns *fn = desc->iface_fns; |
| 89 | int i; |
| 90 | |
Alexander Dahl | 7178d0e | 2022-10-07 14:20:00 +0200 | [diff] [blame] | 91 | log_debug("start with interface functions @ 0x%p\n", fn); |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 92 | |
| 93 | if (fn) { |
| 94 | size_t bytecount = 0; |
| 95 | unsigned char *data = (unsigned char *) buf; |
| 96 | int cookie = desc->cookie; /* make a local copy */ |
| 97 | unsigned long ts; /* timestamp */ |
| 98 | |
Alexander Dahl | 7178d0e | 2022-10-07 14:20:00 +0200 | [diff] [blame] | 99 | log_debug("Function Table:\n" |
| 100 | "ptr:\t0x%p\n" |
| 101 | "struct: 0x%p\n" |
| 102 | "config:\t0x%p\n" |
| 103 | "status:\t0x%p\n" |
| 104 | "clk:\t0x%p\n" |
| 105 | "data:\t0x%p\n" |
| 106 | "done:\t0x%p\n\n", |
| 107 | &fn, fn, fn->config, fn->status, |
| 108 | fn->clk, fn->data, fn->done); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 109 | #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK |
wdenk | d729d30 | 2004-02-27 00:07:27 +0000 | [diff] [blame] | 110 | printf ("Loading FPGA Device %d...", cookie); |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 111 | #endif |
| 112 | |
| 113 | /* |
| 114 | * Run the pre configuration function if there is one. |
| 115 | */ |
| 116 | if (*fn->pre) { |
| 117 | (*fn->pre) (cookie); |
| 118 | } |
| 119 | |
| 120 | /* Establish the initial state */ |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 121 | (*fn->config) (true, true, cookie); /* Assert nCONFIG */ |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 122 | |
| 123 | udelay(2); /* T_cfg > 2us */ |
| 124 | |
| 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); |
| 130 | return FPGA_FAIL; |
| 131 | } |
| 132 | |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 133 | (*fn->config) (false, true, cookie); /* Deassert nCONFIG */ |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 134 | udelay(2); /* T_cf2st1 < 4us */ |
| 135 | |
| 136 | /* Wait for nSTATUS to be released (i.e. deasserted) */ |
| 137 | ts = get_timer (0); /* get current time */ |
| 138 | do { |
Tom Rini | 88d86ec | 2022-12-04 10:03:57 -0500 | [diff] [blame] | 139 | CFG_FPGA_DELAY (); |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 140 | if (get_timer (ts) > CFG_SYS_FPGA_WAIT) { /* check the time */ |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 141 | puts ("** Timeout waiting for STATUS to go high.\n"); |
| 142 | (*fn->abort) (cookie); |
| 143 | return FPGA_FAIL; |
| 144 | } |
| 145 | (*fn->done) (cookie); |
| 146 | } while ((*fn->status) (cookie)); |
| 147 | |
| 148 | /* Get ready for the burn */ |
Tom Rini | 88d86ec | 2022-12-04 10:03:57 -0500 | [diff] [blame] | 149 | CFG_FPGA_DELAY (); |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 150 | |
| 151 | /* Load the data */ |
| 152 | while (bytecount < bsize) { |
| 153 | unsigned char val=0; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 154 | #ifdef CONFIG_SYS_FPGA_CHECK_CTRLC |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 155 | if (ctrlc ()) { |
| 156 | (*fn->abort) (cookie); |
| 157 | return FPGA_FAIL; |
| 158 | } |
| 159 | #endif |
| 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); |
| 166 | return (FPGA_FAIL); |
| 167 | } |
| 168 | #endif |
| 169 | val = data [bytecount ++ ]; |
| 170 | i = 8; |
| 171 | do { |
| 172 | /* Deassert the clock */ |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 173 | (*fn->clk) (false, true, cookie); |
Tom Rini | 88d86ec | 2022-12-04 10:03:57 -0500 | [diff] [blame] | 174 | CFG_FPGA_DELAY (); |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 175 | /* Write data */ |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 176 | (*fn->data) ((val & 0x01), true, cookie); |
Tom Rini | 88d86ec | 2022-12-04 10:03:57 -0500 | [diff] [blame] | 177 | CFG_FPGA_DELAY (); |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 178 | /* Assert the clock */ |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 179 | (*fn->clk) (true, true, cookie); |
Tom Rini | 88d86ec | 2022-12-04 10:03:57 -0500 | [diff] [blame] | 180 | CFG_FPGA_DELAY (); |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 181 | val >>= 1; |
| 182 | i --; |
| 183 | } while (i > 0); |
| 184 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 185 | #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 186 | if (bytecount % (bsize / 40) == 0) |
| 187 | putc ('.'); /* let them know we are alive */ |
| 188 | #endif |
| 189 | } |
| 190 | |
Tom Rini | 88d86ec | 2022-12-04 10:03:57 -0500 | [diff] [blame] | 191 | CFG_FPGA_DELAY (); |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 192 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 193 | #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK |
wdenk | d729d30 | 2004-02-27 00:07:27 +0000 | [diff] [blame] | 194 | putc (' '); /* terminate the dotted line */ |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 195 | #endif |
| 196 | |
| 197 | /* |
| 198 | * Checking FPGA's CONF_DONE signal - correctly booted ? |
| 199 | */ |
| 200 | |
| 201 | if ( ! (*fn->done) (cookie) ) { |
| 202 | puts ("** Booting failed! CONF_DONE is still deasserted.\n"); |
| 203 | (*fn->abort) (cookie); |
| 204 | return (FPGA_FAIL); |
| 205 | } |
| 206 | |
| 207 | /* |
| 208 | * "DCLK must be clocked an additional 10 times fpr ACEX 1K..." |
| 209 | */ |
| 210 | |
| 211 | for (i = 0; i < 12; i++) { |
Tom Rini | 88d86ec | 2022-12-04 10:03:57 -0500 | [diff] [blame] | 212 | CFG_FPGA_DELAY (); |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 213 | (*fn->clk) (true, true, cookie); /* Assert the clock pin */ |
Tom Rini | 88d86ec | 2022-12-04 10:03:57 -0500 | [diff] [blame] | 214 | CFG_FPGA_DELAY (); |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 215 | (*fn->clk) (false, true, cookie); /* Deassert the clock pin */ |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | ret_val = FPGA_SUCCESS; |
| 219 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 220 | #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 221 | if (ret_val == FPGA_SUCCESS) { |
| 222 | puts ("Done.\n"); |
| 223 | } |
| 224 | else { |
| 225 | puts ("Fail.\n"); |
| 226 | } |
| 227 | #endif |
| 228 | (*fn->post) (cookie); |
| 229 | |
| 230 | } else { |
| 231 | printf ("%s: NULL Interface function table!\n", __FUNCTION__); |
| 232 | } |
| 233 | |
| 234 | return ret_val; |
| 235 | } |
| 236 | |
Wolfgang Denk | 74f9b38 | 2011-07-30 13:33:49 +0000 | [diff] [blame] | 237 | static int ACEX1K_ps_dump(Altera_desc *desc, const void *buf, size_t bsize) |
wdenk | 9b7f384 | 2003-10-09 20:09:04 +0000 | [diff] [blame] | 238 | { |
| 239 | /* Readback is only available through the Slave Parallel and */ |
| 240 | /* boundary-scan interfaces. */ |
| 241 | printf ("%s: Passive Serial Dumping is unavailable\n", |
| 242 | __FUNCTION__); |
| 243 | return FPGA_FAIL; |
| 244 | } |