Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Wolfgang Wegner | 406471c | 2010-01-25 11:27:44 +0100 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2006 |
| 4 | * Wolfgang Wegner, ASTRO Strobel Kommunikationssysteme GmbH, |
| 5 | * w.wegner@astro-kom.de |
| 6 | * |
| 7 | * based on the files by |
| 8 | * Heiko Schocher, DENX Software Engineering, hs@denx.de |
| 9 | * and |
| 10 | * Rich Ireland, Enterasys Networks, rireland@enterasys.com. |
| 11 | * Keith Outwater, keith_outwater@mvis.com. |
Wolfgang Wegner | 406471c | 2010-01-25 11:27:44 +0100 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | /* Altera/Xilinx FPGA configuration support for the ASTRO "URMEL" board */ |
| 15 | |
Simon Glass | a73bda4 | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 16 | #include <console.h> |
Wolfgang Wegner | 406471c | 2010-01-25 11:27:44 +0100 | [diff] [blame] | 17 | #include <watchdog.h> |
| 18 | #include <altera.h> |
| 19 | #include <ACEX1K.h> |
| 20 | #include <spartan3.h> |
| 21 | #include <command.h> |
| 22 | #include <asm/immap_5329.h> |
| 23 | #include <asm/io.h> |
| 24 | #include "fpga.h" |
| 25 | |
Wolfgang Wegner | 406471c | 2010-01-25 11:27:44 +0100 | [diff] [blame] | 26 | int altera_pre_fn(int cookie) |
| 27 | { |
| 28 | gpio_t *gpiop = (gpio_t *)MMAP_GPIO; |
| 29 | unsigned char tmp_char; |
| 30 | unsigned short tmp_short; |
| 31 | |
| 32 | /* first, set the required pins to GPIO function */ |
| 33 | /* PAR_T0IN -> GPIO */ |
| 34 | tmp_char = readb(&gpiop->par_timer); |
| 35 | tmp_char &= 0xfc; |
| 36 | writeb(tmp_char, &gpiop->par_timer); |
| 37 | /* all QSPI pins -> GPIO */ |
| 38 | writew(0x0000, &gpiop->par_qspi); |
| 39 | /* U0RTS, U0CTS -> GPIO */ |
| 40 | tmp_short = __raw_readw(&gpiop->par_uart); |
| 41 | tmp_short &= 0xfff3; |
| 42 | __raw_writew(tmp_short, &gpiop->par_uart); |
| 43 | /* all PWM pins -> GPIO */ |
| 44 | writeb(0x00, &gpiop->par_pwm); |
| 45 | /* next, set data direction registers */ |
| 46 | writeb(0x01, &gpiop->pddr_timer); |
| 47 | writeb(0x25, &gpiop->pddr_qspi); |
| 48 | writeb(0x0c, &gpiop->pddr_uart); |
| 49 | writeb(0x04, &gpiop->pddr_pwm); |
| 50 | |
| 51 | /* ensure other SPI peripherals are deselected */ |
| 52 | writeb(0x08, &gpiop->ppd_uart); |
| 53 | writeb(0x38, &gpiop->ppd_qspi); |
| 54 | |
| 55 | /* CONFIG = 0 STATUS = 0 -> FPGA in reset state */ |
| 56 | writeb(0xFB, &gpiop->pclrr_uart); |
| 57 | /* enable Altera configuration by clearing QSPI_CS2 and DT0IN */ |
| 58 | writeb(0xFE, &gpiop->pclrr_timer); |
| 59 | writeb(0xDF, &gpiop->pclrr_qspi); |
| 60 | return FPGA_SUCCESS; |
| 61 | } |
| 62 | |
| 63 | /* Set the state of CONFIG Pin */ |
| 64 | int altera_config_fn(int assert_config, int flush, int cookie) |
| 65 | { |
| 66 | gpio_t *gpiop = (gpio_t *)MMAP_GPIO; |
| 67 | |
| 68 | if (assert_config) |
| 69 | writeb(0x04, &gpiop->ppd_uart); |
| 70 | else |
| 71 | writeb(0xFB, &gpiop->pclrr_uart); |
| 72 | return FPGA_SUCCESS; |
| 73 | } |
| 74 | |
| 75 | /* Returns the state of STATUS Pin */ |
| 76 | int altera_status_fn(int cookie) |
| 77 | { |
| 78 | gpio_t *gpiop = (gpio_t *)MMAP_GPIO; |
| 79 | |
| 80 | if (readb(&gpiop->ppd_pwm) & 0x08) |
| 81 | return FPGA_FAIL; |
| 82 | return FPGA_SUCCESS; |
| 83 | } |
| 84 | |
| 85 | /* Returns the state of CONF_DONE Pin */ |
| 86 | int altera_done_fn(int cookie) |
| 87 | { |
| 88 | gpio_t *gpiop = (gpio_t *)MMAP_GPIO; |
| 89 | |
| 90 | if (readb(&gpiop->ppd_pwm) & 0x20) |
| 91 | return FPGA_FAIL; |
| 92 | return FPGA_SUCCESS; |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | * writes the complete buffer to the FPGA |
| 97 | * writing the complete buffer in one function is much faster, |
| 98 | * then calling it for every bit |
| 99 | */ |
Simon Glass | 660031e | 2014-06-07 22:07:58 -0600 | [diff] [blame] | 100 | int altera_write_fn(const void *buf, size_t len, int flush, int cookie) |
Wolfgang Wegner | 406471c | 2010-01-25 11:27:44 +0100 | [diff] [blame] | 101 | { |
| 102 | size_t bytecount = 0; |
| 103 | gpio_t *gpiop = (gpio_t *)MMAP_GPIO; |
| 104 | unsigned char *data = (unsigned char *)buf; |
| 105 | unsigned char val = 0; |
| 106 | int i; |
| 107 | int len_40 = len / 40; |
| 108 | |
| 109 | while (bytecount < len) { |
| 110 | val = data[bytecount++]; |
| 111 | i = 8; |
| 112 | do { |
| 113 | writeb(0xFB, &gpiop->pclrr_qspi); |
| 114 | if (val & 0x01) |
| 115 | writeb(0x01, &gpiop->ppd_qspi); |
| 116 | else |
| 117 | writeb(0xFE, &gpiop->pclrr_qspi); |
| 118 | writeb(0x04, &gpiop->ppd_qspi); |
| 119 | val >>= 1; |
| 120 | i--; |
| 121 | } while (i > 0); |
| 122 | |
| 123 | if (bytecount % len_40 == 0) { |
| 124 | #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG) |
Stefan Roese | 80877fa | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 125 | schedule(); |
Wolfgang Wegner | 406471c | 2010-01-25 11:27:44 +0100 | [diff] [blame] | 126 | #endif |
| 127 | #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK |
| 128 | putc('.'); /* let them know we are alive */ |
| 129 | #endif |
| 130 | #ifdef CONFIG_SYS_FPGA_CHECK_CTRLC |
| 131 | if (ctrlc()) |
| 132 | return FPGA_FAIL; |
| 133 | #endif |
| 134 | } |
| 135 | } |
| 136 | return FPGA_SUCCESS; |
| 137 | } |
| 138 | |
| 139 | /* called, when programming is aborted */ |
| 140 | int altera_abort_fn(int cookie) |
| 141 | { |
| 142 | gpio_t *gpiop = (gpio_t *)MMAP_GPIO; |
| 143 | |
| 144 | writeb(0x20, &gpiop->ppd_qspi); |
| 145 | writeb(0x08, &gpiop->ppd_uart); |
| 146 | return FPGA_SUCCESS; |
| 147 | } |
| 148 | |
| 149 | /* called, when programming was succesful */ |
| 150 | int altera_post_fn(int cookie) |
| 151 | { |
| 152 | return altera_abort_fn(cookie); |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * Note that these are pointers to code that is in Flash. They will be |
| 157 | * relocated at runtime. |
| 158 | * FIXME: relocation not yet working for coldfire, see below! |
| 159 | */ |
| 160 | Altera_CYC2_Passive_Serial_fns altera_fns = { |
| 161 | altera_pre_fn, |
| 162 | altera_config_fn, |
| 163 | altera_status_fn, |
| 164 | altera_done_fn, |
| 165 | altera_write_fn, |
| 166 | altera_abort_fn, |
| 167 | altera_post_fn |
| 168 | }; |
| 169 | |
Tom Rini | c89eb64 | 2022-06-12 20:01:59 -0400 | [diff] [blame] | 170 | #define FPGA_COUNT 1 |
| 171 | Altera_desc altera_fpga[FPGA_COUNT] = { |
Wolfgang Wegner | 406471c | 2010-01-25 11:27:44 +0100 | [diff] [blame] | 172 | {Altera_CYC2, |
| 173 | passive_serial, |
| 174 | 85903, |
| 175 | (void *)&altera_fns, |
| 176 | NULL, |
| 177 | 0} |
| 178 | }; |
| 179 | |
| 180 | /* Initialize the fpga. Return 1 on success, 0 on failure. */ |
| 181 | int astro5373l_altera_load(void) |
| 182 | { |
| 183 | int i; |
| 184 | |
Tom Rini | c89eb64 | 2022-06-12 20:01:59 -0400 | [diff] [blame] | 185 | for (i = 0; i < FPGA_COUNT; i++) { |
Wolfgang Wegner | 406471c | 2010-01-25 11:27:44 +0100 | [diff] [blame] | 186 | /* |
| 187 | * I did not yet manage to get relocation work properly, |
| 188 | * so set stuff here instead of static initialisation: |
| 189 | */ |
| 190 | altera_fns.pre = altera_pre_fn; |
| 191 | altera_fns.config = altera_config_fn; |
| 192 | altera_fns.status = altera_status_fn; |
| 193 | altera_fns.done = altera_done_fn; |
| 194 | altera_fns.write = altera_write_fn; |
| 195 | altera_fns.abort = altera_abort_fn; |
| 196 | altera_fns.post = altera_post_fn; |
| 197 | altera_fpga[i].iface_fns = (void *)&altera_fns; |
| 198 | fpga_add(fpga_altera, &altera_fpga[i]); |
| 199 | } |
| 200 | return 1; |
| 201 | } |
| 202 | |
| 203 | /* Set the FPGA's PROG_B line to the specified level */ |
Michal Simek | b4079cf | 2014-03-13 12:58:20 +0100 | [diff] [blame] | 204 | int xilinx_pgm_config_fn(int assert, int flush, int cookie) |
Wolfgang Wegner | 406471c | 2010-01-25 11:27:44 +0100 | [diff] [blame] | 205 | { |
| 206 | gpio_t *gpiop = (gpio_t *)MMAP_GPIO; |
| 207 | |
| 208 | if (assert) |
| 209 | writeb(0xFB, &gpiop->pclrr_uart); |
| 210 | else |
| 211 | writeb(0x04, &gpiop->ppd_uart); |
| 212 | return assert; |
| 213 | } |
| 214 | |
| 215 | /* |
| 216 | * Test the state of the active-low FPGA INIT line. Return 1 on INIT |
| 217 | * asserted (low). |
| 218 | */ |
Michal Simek | b4079cf | 2014-03-13 12:58:20 +0100 | [diff] [blame] | 219 | int xilinx_init_config_fn(int cookie) |
Wolfgang Wegner | 406471c | 2010-01-25 11:27:44 +0100 | [diff] [blame] | 220 | { |
| 221 | gpio_t *gpiop = (gpio_t *)MMAP_GPIO; |
| 222 | |
| 223 | return (readb(&gpiop->ppd_pwm) & 0x08) == 0; |
| 224 | } |
| 225 | |
| 226 | /* Test the state of the active-high FPGA DONE pin */ |
Michal Simek | b4079cf | 2014-03-13 12:58:20 +0100 | [diff] [blame] | 227 | int xilinx_done_config_fn(int cookie) |
Wolfgang Wegner | 406471c | 2010-01-25 11:27:44 +0100 | [diff] [blame] | 228 | { |
| 229 | gpio_t *gpiop = (gpio_t *)MMAP_GPIO; |
| 230 | |
| 231 | return (readb(&gpiop->ppd_pwm) & 0x20) >> 5; |
| 232 | } |
| 233 | |
| 234 | /* Abort an FPGA operation */ |
Michal Simek | b4079cf | 2014-03-13 12:58:20 +0100 | [diff] [blame] | 235 | int xilinx_abort_config_fn(int cookie) |
Wolfgang Wegner | 406471c | 2010-01-25 11:27:44 +0100 | [diff] [blame] | 236 | { |
| 237 | gpio_t *gpiop = (gpio_t *)MMAP_GPIO; |
| 238 | /* ensure all SPI peripherals and FPGAs are deselected */ |
| 239 | writeb(0x08, &gpiop->ppd_uart); |
| 240 | writeb(0x01, &gpiop->ppd_timer); |
| 241 | writeb(0x38, &gpiop->ppd_qspi); |
| 242 | return FPGA_FAIL; |
| 243 | } |
| 244 | |
| 245 | /* |
| 246 | * FPGA pre-configuration function. Just make sure that |
| 247 | * FPGA reset is asserted to keep the FPGA from starting up after |
| 248 | * configuration. |
| 249 | */ |
| 250 | int xilinx_pre_config_fn(int cookie) |
| 251 | { |
| 252 | gpio_t *gpiop = (gpio_t *)MMAP_GPIO; |
| 253 | unsigned char tmp_char; |
| 254 | unsigned short tmp_short; |
| 255 | |
| 256 | /* first, set the required pins to GPIO function */ |
| 257 | /* PAR_T0IN -> GPIO */ |
| 258 | tmp_char = readb(&gpiop->par_timer); |
| 259 | tmp_char &= 0xfc; |
| 260 | writeb(tmp_char, &gpiop->par_timer); |
| 261 | /* all QSPI pins -> GPIO */ |
| 262 | writew(0x0000, &gpiop->par_qspi); |
| 263 | /* U0RTS, U0CTS -> GPIO */ |
| 264 | tmp_short = __raw_readw(&gpiop->par_uart); |
| 265 | tmp_short &= 0xfff3; |
| 266 | __raw_writew(tmp_short, &gpiop->par_uart); |
| 267 | /* all PWM pins -> GPIO */ |
| 268 | writeb(0x00, &gpiop->par_pwm); |
| 269 | /* next, set data direction registers */ |
| 270 | writeb(0x01, &gpiop->pddr_timer); |
| 271 | writeb(0x25, &gpiop->pddr_qspi); |
| 272 | writeb(0x0c, &gpiop->pddr_uart); |
| 273 | writeb(0x04, &gpiop->pddr_pwm); |
| 274 | |
| 275 | /* ensure other SPI peripherals are deselected */ |
| 276 | writeb(0x08, &gpiop->ppd_uart); |
| 277 | writeb(0x38, &gpiop->ppd_qspi); |
| 278 | writeb(0x01, &gpiop->ppd_timer); |
| 279 | |
| 280 | /* CONFIG = 0, STATUS = 0 -> FPGA in reset state */ |
| 281 | writeb(0xFB, &gpiop->pclrr_uart); |
| 282 | /* enable Xilinx configuration by clearing QSPI_CS2 and U0CTS */ |
| 283 | writeb(0xF7, &gpiop->pclrr_uart); |
| 284 | writeb(0xDF, &gpiop->pclrr_qspi); |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | /* |
| 289 | * FPGA post configuration function. Should perform a test if FPGA is running. |
| 290 | */ |
| 291 | int xilinx_post_config_fn(int cookie) |
| 292 | { |
| 293 | int rc = 0; |
| 294 | |
| 295 | /* |
| 296 | * no test yet |
| 297 | */ |
| 298 | return rc; |
| 299 | } |
| 300 | |
Michal Simek | b4079cf | 2014-03-13 12:58:20 +0100 | [diff] [blame] | 301 | int xilinx_clk_config_fn(int assert_clk, int flush, int cookie) |
Wolfgang Wegner | 406471c | 2010-01-25 11:27:44 +0100 | [diff] [blame] | 302 | { |
| 303 | gpio_t *gpiop = (gpio_t *)MMAP_GPIO; |
| 304 | |
| 305 | if (assert_clk) |
| 306 | writeb(0x04, &gpiop->ppd_qspi); |
| 307 | else |
| 308 | writeb(0xFB, &gpiop->pclrr_qspi); |
| 309 | return assert_clk; |
| 310 | } |
| 311 | |
Michal Simek | b4079cf | 2014-03-13 12:58:20 +0100 | [diff] [blame] | 312 | int xilinx_wr_config_fn(int assert_write, int flush, int cookie) |
Wolfgang Wegner | 406471c | 2010-01-25 11:27:44 +0100 | [diff] [blame] | 313 | { |
| 314 | gpio_t *gpiop = (gpio_t *)MMAP_GPIO; |
| 315 | |
| 316 | if (assert_write) |
| 317 | writeb(0x01, &gpiop->ppd_qspi); |
| 318 | else |
| 319 | writeb(0xFE, &gpiop->pclrr_qspi); |
| 320 | return assert_write; |
| 321 | } |
| 322 | |
Michal Simek | b4079cf | 2014-03-13 12:58:20 +0100 | [diff] [blame] | 323 | int xilinx_fastwr_config_fn(void *buf, size_t len, int flush, int cookie) |
Wolfgang Wegner | 406471c | 2010-01-25 11:27:44 +0100 | [diff] [blame] | 324 | { |
| 325 | size_t bytecount = 0; |
| 326 | gpio_t *gpiop = (gpio_t *)MMAP_GPIO; |
| 327 | unsigned char *data = (unsigned char *)buf; |
| 328 | unsigned char val = 0; |
| 329 | int i; |
| 330 | int len_40 = len / 40; |
| 331 | |
| 332 | for (bytecount = 0; bytecount < len; bytecount++) { |
| 333 | val = *(data++); |
| 334 | for (i = 8; i > 0; i--) { |
| 335 | writeb(0xFB, &gpiop->pclrr_qspi); |
| 336 | if (val & 0x80) |
| 337 | writeb(0x01, &gpiop->ppd_qspi); |
| 338 | else |
| 339 | writeb(0xFE, &gpiop->pclrr_qspi); |
| 340 | writeb(0x04, &gpiop->ppd_qspi); |
| 341 | val <<= 1; |
| 342 | } |
| 343 | if (bytecount % len_40 == 0) { |
| 344 | #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG) |
Stefan Roese | 80877fa | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 345 | schedule(); |
Wolfgang Wegner | 406471c | 2010-01-25 11:27:44 +0100 | [diff] [blame] | 346 | #endif |
| 347 | #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK |
| 348 | putc('.'); /* let them know we are alive */ |
| 349 | #endif |
| 350 | #ifdef CONFIG_SYS_FPGA_CHECK_CTRLC |
| 351 | if (ctrlc()) |
| 352 | return FPGA_FAIL; |
| 353 | #endif |
| 354 | } |
| 355 | } |
| 356 | return FPGA_SUCCESS; |
| 357 | } |
| 358 | |
| 359 | /* |
| 360 | * Note that these are pointers to code that is in Flash. They will be |
| 361 | * relocated at runtime. |
| 362 | * FIXME: relocation not yet working for coldfire, see below! |
| 363 | */ |
Michal Simek | 2091a0c | 2014-03-13 11:28:42 +0100 | [diff] [blame] | 364 | xilinx_spartan3_slave_serial_fns xilinx_fns = { |
Wolfgang Wegner | 406471c | 2010-01-25 11:27:44 +0100 | [diff] [blame] | 365 | xilinx_pre_config_fn, |
Michal Simek | b4079cf | 2014-03-13 12:58:20 +0100 | [diff] [blame] | 366 | xilinx_pgm_config_fn, |
| 367 | xilinx_clk_config_fn, |
| 368 | xilinx_init_config_fn, |
| 369 | xilinx_done_config_fn, |
| 370 | xilinx_wr_config_fn, |
Wolfgang Wegner | 406471c | 2010-01-25 11:27:44 +0100 | [diff] [blame] | 371 | 0, |
Michal Simek | b4079cf | 2014-03-13 12:58:20 +0100 | [diff] [blame] | 372 | xilinx_fastwr_config_fn |
Wolfgang Wegner | 406471c | 2010-01-25 11:27:44 +0100 | [diff] [blame] | 373 | }; |
| 374 | |
Tom Rini | c89eb64 | 2022-06-12 20:01:59 -0400 | [diff] [blame] | 375 | xilinx_desc xilinx_fpga[FPGA_COUNT] = { |
Michal Simek | 2091a0c | 2014-03-13 11:28:42 +0100 | [diff] [blame] | 376 | {xilinx_spartan3, |
Wolfgang Wegner | 406471c | 2010-01-25 11:27:44 +0100 | [diff] [blame] | 377 | slave_serial, |
| 378 | XILINX_XC3S4000_SIZE, |
| 379 | (void *)&xilinx_fns, |
Michal Simek | 75fafac | 2014-03-13 13:07:57 +0100 | [diff] [blame] | 380 | 0, |
| 381 | &spartan3_op} |
Wolfgang Wegner | 406471c | 2010-01-25 11:27:44 +0100 | [diff] [blame] | 382 | }; |
| 383 | |
| 384 | /* Initialize the fpga. Return 1 on success, 0 on failure. */ |
| 385 | int astro5373l_xilinx_load(void) |
| 386 | { |
| 387 | int i; |
| 388 | |
| 389 | fpga_init(); |
| 390 | |
Tom Rini | c89eb64 | 2022-06-12 20:01:59 -0400 | [diff] [blame] | 391 | for (i = 0; i < FPGA_COUNT; i++) { |
Wolfgang Wegner | 406471c | 2010-01-25 11:27:44 +0100 | [diff] [blame] | 392 | /* |
| 393 | * I did not yet manage to get relocation work properly, |
| 394 | * so set stuff here instead of static initialisation: |
| 395 | */ |
| 396 | xilinx_fns.pre = xilinx_pre_config_fn; |
Michal Simek | b4079cf | 2014-03-13 12:58:20 +0100 | [diff] [blame] | 397 | xilinx_fns.pgm = xilinx_pgm_config_fn; |
| 398 | xilinx_fns.clk = xilinx_clk_config_fn; |
| 399 | xilinx_fns.init = xilinx_init_config_fn; |
| 400 | xilinx_fns.done = xilinx_done_config_fn; |
| 401 | xilinx_fns.wr = xilinx_wr_config_fn; |
| 402 | xilinx_fns.bwr = xilinx_fastwr_config_fn; |
Wolfgang Wegner | 406471c | 2010-01-25 11:27:44 +0100 | [diff] [blame] | 403 | xilinx_fpga[i].iface_fns = (void *)&xilinx_fns; |
| 404 | fpga_add(fpga_xilinx, &xilinx_fpga[i]); |
| 405 | } |
| 406 | return 1; |
| 407 | } |