Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Stefan Roese | 5f1cf2d | 2006-08-15 14:15:51 +0200 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2006 |
| 4 | * Heiko Schocher, hs@denx.de |
| 5 | * Based on ACE1XK.c |
Stefan Roese | 5f1cf2d | 2006-08-15 14:15:51 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
Alexander Dahl | e543b1b | 2022-10-07 14:19:59 +0200 | [diff] [blame] | 8 | #define LOG_CATEGORY UCLASS_FPGA |
| 9 | |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 10 | #include <config.h> /* core U-Boot definitions */ |
Alexander Dahl | e543b1b | 2022-10-07 14:19:59 +0200 | [diff] [blame] | 11 | #include <log.h> |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 12 | #include <time.h> |
Stefan Roese | 5f1cf2d | 2006-08-15 14:15:51 +0200 | [diff] [blame] | 13 | #include <altera.h> |
| 14 | #include <ACEX1K.h> /* ACEX device family */ |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 15 | #include <linux/delay.h> |
Stefan Roese | 5f1cf2d | 2006-08-15 14:15:51 +0200 | [diff] [blame] | 16 | |
Stefan Roese | 5f1cf2d | 2006-08-15 14:15:51 +0200 | [diff] [blame] | 17 | /* Note: The assumption is that we cannot possibly run fast enough to |
| 18 | * overrun the device (the Slave Parallel mode can free run at 50MHz). |
Tom Rini | 88d86ec | 2022-12-04 10:03:57 -0500 | [diff] [blame] | 19 | * If there is a need to operate slower, define CFG_FPGA_DELAY in |
Stefan Roese | 5f1cf2d | 2006-08-15 14:15:51 +0200 | [diff] [blame] | 20 | * the board config file to slow things down. |
| 21 | */ |
Tom Rini | 88d86ec | 2022-12-04 10:03:57 -0500 | [diff] [blame] | 22 | #ifndef CFG_FPGA_DELAY |
| 23 | #define CFG_FPGA_DELAY() |
Stefan Roese | 5f1cf2d | 2006-08-15 14:15:51 +0200 | [diff] [blame] | 24 | #endif |
| 25 | |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 26 | #ifndef CFG_SYS_FPGA_WAIT |
| 27 | #define CFG_SYS_FPGA_WAIT CONFIG_SYS_HZ / 10 /* 100 ms */ |
Stefan Roese | 5f1cf2d | 2006-08-15 14:15:51 +0200 | [diff] [blame] | 28 | #endif |
| 29 | |
Wolfgang Denk | 74f9b38 | 2011-07-30 13:33:49 +0000 | [diff] [blame] | 30 | static int CYC2_ps_load(Altera_desc *desc, const void *buf, size_t bsize); |
| 31 | static int CYC2_ps_dump(Altera_desc *desc, const void *buf, size_t bsize); |
Stefan Roese | 5f1cf2d | 2006-08-15 14:15:51 +0200 | [diff] [blame] | 32 | /* static int CYC2_ps_info( Altera_desc *desc ); */ |
Stefan Roese | 5f1cf2d | 2006-08-15 14:15:51 +0200 | [diff] [blame] | 33 | |
| 34 | /* ------------------------------------------------------------------------- */ |
| 35 | /* CYCLON2 Generic Implementation */ |
Wolfgang Denk | 74f9b38 | 2011-07-30 13:33:49 +0000 | [diff] [blame] | 36 | int CYC2_load(Altera_desc *desc, const void *buf, size_t bsize) |
Stefan Roese | 5f1cf2d | 2006-08-15 14:15:51 +0200 | [diff] [blame] | 37 | { |
| 38 | int ret_val = FPGA_FAIL; |
| 39 | |
| 40 | switch (desc->iface) { |
| 41 | case passive_serial: |
Alexander Dahl | e543b1b | 2022-10-07 14:19:59 +0200 | [diff] [blame] | 42 | log_debug("Launching Passive Serial Loader\n"); |
Alexander Dahl | 246bc02 | 2019-06-28 14:41:21 +0200 | [diff] [blame] | 43 | ret_val = CYC2_ps_load(desc, buf, bsize); |
Stefan Roese | 5f1cf2d | 2006-08-15 14:15:51 +0200 | [diff] [blame] | 44 | break; |
| 45 | |
Michael Jones | d846bb5 | 2011-07-14 23:09:41 +0000 | [diff] [blame] | 46 | case fast_passive_parallel: |
| 47 | /* Fast Passive Parallel (FPP) and PS only differ in what is |
| 48 | * done in the write() callback. Use the existing PS load |
| 49 | * function for FPP, too. |
| 50 | */ |
Alexander Dahl | e543b1b | 2022-10-07 14:19:59 +0200 | [diff] [blame] | 51 | log_debug("Launching Fast Passive Parallel Loader\n"); |
Michael Jones | d846bb5 | 2011-07-14 23:09:41 +0000 | [diff] [blame] | 52 | ret_val = CYC2_ps_load(desc, buf, bsize); |
| 53 | break; |
| 54 | |
Stefan Roese | 5f1cf2d | 2006-08-15 14:15:51 +0200 | [diff] [blame] | 55 | /* Add new interface types here */ |
| 56 | |
| 57 | default: |
Alexander Dahl | 246bc02 | 2019-06-28 14:41:21 +0200 | [diff] [blame] | 58 | printf("%s: Unsupported interface type, %d\n", |
| 59 | __func__, desc->iface); |
Stefan Roese | 5f1cf2d | 2006-08-15 14:15:51 +0200 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | return ret_val; |
| 63 | } |
| 64 | |
Wolfgang Denk | 74f9b38 | 2011-07-30 13:33:49 +0000 | [diff] [blame] | 65 | int CYC2_dump(Altera_desc *desc, const void *buf, size_t bsize) |
Stefan Roese | 5f1cf2d | 2006-08-15 14:15:51 +0200 | [diff] [blame] | 66 | { |
| 67 | int ret_val = FPGA_FAIL; |
| 68 | |
| 69 | switch (desc->iface) { |
| 70 | case passive_serial: |
Alexander Dahl | e543b1b | 2022-10-07 14:19:59 +0200 | [diff] [blame] | 71 | log_debug("Launching Passive Serial Dump\n"); |
Alexander Dahl | 246bc02 | 2019-06-28 14:41:21 +0200 | [diff] [blame] | 72 | ret_val = CYC2_ps_dump(desc, buf, bsize); |
Stefan Roese | 5f1cf2d | 2006-08-15 14:15:51 +0200 | [diff] [blame] | 73 | break; |
| 74 | |
| 75 | /* Add new interface types here */ |
| 76 | |
| 77 | default: |
Alexander Dahl | 246bc02 | 2019-06-28 14:41:21 +0200 | [diff] [blame] | 78 | printf("%s: Unsupported interface type, %d\n", |
| 79 | __func__, desc->iface); |
Stefan Roese | 5f1cf2d | 2006-08-15 14:15:51 +0200 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | return ret_val; |
| 83 | } |
| 84 | |
Alexander Dahl | 246bc02 | 2019-06-28 14:41:21 +0200 | [diff] [blame] | 85 | int CYC2_info(Altera_desc *desc) |
Stefan Roese | 5f1cf2d | 2006-08-15 14:15:51 +0200 | [diff] [blame] | 86 | { |
| 87 | return FPGA_SUCCESS; |
| 88 | } |
| 89 | |
Stefan Roese | 5f1cf2d | 2006-08-15 14:15:51 +0200 | [diff] [blame] | 90 | /* ------------------------------------------------------------------------- */ |
Alexander Dahl | 246bc02 | 2019-06-28 14:41:21 +0200 | [diff] [blame] | 91 | /* CYCLON2 Passive Serial Generic Implementation */ |
Wolfgang Denk | 74f9b38 | 2011-07-30 13:33:49 +0000 | [diff] [blame] | 92 | static int CYC2_ps_load(Altera_desc *desc, const void *buf, size_t bsize) |
Stefan Roese | 5f1cf2d | 2006-08-15 14:15:51 +0200 | [diff] [blame] | 93 | { |
| 94 | int ret_val = FPGA_FAIL; /* assume the worst */ |
| 95 | Altera_CYC2_Passive_Serial_fns *fn = desc->iface_fns; |
| 96 | int ret = 0; |
| 97 | |
Alexander Dahl | e543b1b | 2022-10-07 14:19:59 +0200 | [diff] [blame] | 98 | log_debug("start with interface functions @ 0x%p\n", fn); |
Stefan Roese | 5f1cf2d | 2006-08-15 14:15:51 +0200 | [diff] [blame] | 99 | |
| 100 | if (fn) { |
| 101 | int cookie = desc->cookie; /* make a local copy */ |
| 102 | unsigned long ts; /* timestamp */ |
| 103 | |
Alexander Dahl | e543b1b | 2022-10-07 14:19:59 +0200 | [diff] [blame] | 104 | log_debug("Function Table:\n" |
| 105 | "ptr:\t0x%p\n" |
| 106 | "struct: 0x%p\n" |
| 107 | "config:\t0x%p\n" |
| 108 | "status:\t0x%p\n" |
| 109 | "write:\t0x%p\n" |
| 110 | "done:\t0x%p\n\n", |
| 111 | &fn, fn, fn->config, fn->status, |
| 112 | fn->write, fn->done); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 113 | #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK |
Alexander Dahl | 246bc02 | 2019-06-28 14:41:21 +0200 | [diff] [blame] | 114 | printf("Loading FPGA Device %d...", cookie); |
Stefan Roese | 5f1cf2d | 2006-08-15 14:15:51 +0200 | [diff] [blame] | 115 | #endif |
| 116 | |
| 117 | /* |
| 118 | * Run the pre configuration function if there is one. |
| 119 | */ |
Alexander Dahl | 246bc02 | 2019-06-28 14:41:21 +0200 | [diff] [blame] | 120 | if (*fn->pre) |
Stefan Roese | 5f1cf2d | 2006-08-15 14:15:51 +0200 | [diff] [blame] | 121 | (*fn->pre) (cookie); |
Stefan Roese | 5f1cf2d | 2006-08-15 14:15:51 +0200 | [diff] [blame] | 122 | |
| 123 | /* Establish the initial state */ |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 124 | (*fn->config) (false, true, cookie); /* De-assert nCONFIG */ |
Stephan Gatzka | 67f3291 | 2012-10-22 23:11:41 +0000 | [diff] [blame] | 125 | udelay(100); |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 126 | (*fn->config) (true, true, cookie); /* Assert nCONFIG */ |
Stefan Roese | 5f1cf2d | 2006-08-15 14:15:51 +0200 | [diff] [blame] | 127 | |
| 128 | udelay(2); /* T_cfg > 2us */ |
| 129 | |
| 130 | /* Wait for nSTATUS to be asserted */ |
Alexander Dahl | 246bc02 | 2019-06-28 14:41:21 +0200 | [diff] [blame] | 131 | ts = get_timer(0); /* get current time */ |
Stefan Roese | 5f1cf2d | 2006-08-15 14:15:51 +0200 | [diff] [blame] | 132 | do { |
Tom Rini | 88d86ec | 2022-12-04 10:03:57 -0500 | [diff] [blame] | 133 | CFG_FPGA_DELAY(); |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 134 | if (get_timer(ts) > CFG_SYS_FPGA_WAIT) { |
Alexander Dahl | 246bc02 | 2019-06-28 14:41:21 +0200 | [diff] [blame] | 135 | /* check the time */ |
| 136 | puts("** Timeout waiting for STATUS to go high.\n"); |
Stefan Roese | 5f1cf2d | 2006-08-15 14:15:51 +0200 | [diff] [blame] | 137 | (*fn->abort) (cookie); |
| 138 | return FPGA_FAIL; |
| 139 | } |
| 140 | } while (!(*fn->status) (cookie)); |
| 141 | |
| 142 | /* Get ready for the burn */ |
Tom Rini | 88d86ec | 2022-12-04 10:03:57 -0500 | [diff] [blame] | 143 | CFG_FPGA_DELAY(); |
Stefan Roese | 5f1cf2d | 2006-08-15 14:15:51 +0200 | [diff] [blame] | 144 | |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 145 | ret = (*fn->write) (buf, bsize, true, cookie); |
Stefan Roese | 5f1cf2d | 2006-08-15 14:15:51 +0200 | [diff] [blame] | 146 | if (ret) { |
Alexander Dahl | 246bc02 | 2019-06-28 14:41:21 +0200 | [diff] [blame] | 147 | puts("** Write failed.\n"); |
Stefan Roese | 5f1cf2d | 2006-08-15 14:15:51 +0200 | [diff] [blame] | 148 | (*fn->abort) (cookie); |
| 149 | return FPGA_FAIL; |
| 150 | } |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 151 | #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK |
Stefan Roese | 5f1cf2d | 2006-08-15 14:15:51 +0200 | [diff] [blame] | 152 | puts(" OK? ..."); |
| 153 | #endif |
| 154 | |
Tom Rini | 88d86ec | 2022-12-04 10:03:57 -0500 | [diff] [blame] | 155 | CFG_FPGA_DELAY(); |
Stefan Roese | 5f1cf2d | 2006-08-15 14:15:51 +0200 | [diff] [blame] | 156 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 157 | #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK |
Alexander Dahl | 246bc02 | 2019-06-28 14:41:21 +0200 | [diff] [blame] | 158 | putc(' '); /* terminate the dotted line */ |
Stefan Roese | 5f1cf2d | 2006-08-15 14:15:51 +0200 | [diff] [blame] | 159 | #endif |
| 160 | |
Alexander Dahl | a8da71c | 2019-06-28 14:41:22 +0200 | [diff] [blame] | 161 | /* |
| 162 | * Checking FPGA's CONF_DONE signal - correctly booted ? |
| 163 | */ |
Stefan Roese | 5f1cf2d | 2006-08-15 14:15:51 +0200 | [diff] [blame] | 164 | |
Alexander Dahl | a8da71c | 2019-06-28 14:41:22 +0200 | [diff] [blame] | 165 | if (!(*fn->done) (cookie)) { |
| 166 | puts("** Booting failed! CONF_DONE is still deasserted.\n"); |
| 167 | (*fn->abort) (cookie); |
| 168 | return FPGA_FAIL; |
| 169 | } |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 170 | #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK |
Alexander Dahl | a8da71c | 2019-06-28 14:41:22 +0200 | [diff] [blame] | 171 | puts(" OK\n"); |
Stefan Roese | 5f1cf2d | 2006-08-15 14:15:51 +0200 | [diff] [blame] | 172 | #endif |
| 173 | |
Alexander Dahl | a8da71c | 2019-06-28 14:41:22 +0200 | [diff] [blame] | 174 | ret_val = FPGA_SUCCESS; |
Stefan Roese | 5f1cf2d | 2006-08-15 14:15:51 +0200 | [diff] [blame] | 175 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 176 | #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK |
Alexander Dahl | a8da71c | 2019-06-28 14:41:22 +0200 | [diff] [blame] | 177 | if (ret_val == FPGA_SUCCESS) |
| 178 | puts("Done.\n"); |
| 179 | else |
| 180 | puts("Fail.\n"); |
Stefan Roese | 5f1cf2d | 2006-08-15 14:15:51 +0200 | [diff] [blame] | 181 | #endif |
Stefan Roese | 5f1cf2d | 2006-08-15 14:15:51 +0200 | [diff] [blame] | 182 | |
Alexander Dahl | d52678c | 2019-06-28 14:41:23 +0200 | [diff] [blame] | 183 | /* |
| 184 | * Run the post configuration function if there is one. |
| 185 | */ |
| 186 | if (*fn->post) |
| 187 | (*fn->post) (cookie); |
Stefan Roese | 5f1cf2d | 2006-08-15 14:15:51 +0200 | [diff] [blame] | 188 | } else { |
Alexander Dahl | 246bc02 | 2019-06-28 14:41:21 +0200 | [diff] [blame] | 189 | printf("%s: NULL Interface function table!\n", __func__); |
Stefan Roese | 5f1cf2d | 2006-08-15 14:15:51 +0200 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | return ret_val; |
| 193 | } |
| 194 | |
Wolfgang Denk | 74f9b38 | 2011-07-30 13:33:49 +0000 | [diff] [blame] | 195 | static int CYC2_ps_dump(Altera_desc *desc, const void *buf, size_t bsize) |
Stefan Roese | 5f1cf2d | 2006-08-15 14:15:51 +0200 | [diff] [blame] | 196 | { |
| 197 | /* Readback is only available through the Slave Parallel and */ |
| 198 | /* boundary-scan interfaces. */ |
Alexander Dahl | 246bc02 | 2019-06-28 14:41:21 +0200 | [diff] [blame] | 199 | printf("%s: Passive Serial Dumping is unavailable\n", __func__); |
Stefan Roese | 5f1cf2d | 2006-08-15 14:15:51 +0200 | [diff] [blame] | 200 | return FPGA_FAIL; |
| 201 | } |