2 * (C) Copyright 2001-2003
6 * SPDX-License-Identifier: GPL-2.0+
8 /* The DEBUG define must be before common to enable debugging */
11 #include <asm/processor.h>
14 /* ------------------------------------------------------------------------- */
19 #define PD(bit) (1 << (15 - (bit)))
20 # define FPGA_INIT PD(11) /* FPGA init pin (ppc input) */
21 # define FPGA_PRG PD(12) /* FPGA program pin (ppc output) */
22 # define FPGA_CLK PD(13) /* FPGA clk pin (ppc output) */
23 # define FPGA_DATA PD(14) /* FPGA data pin (ppc output) */
24 # define FPGA_DONE PD(15) /* FPGA done pin (ppc input) */
27 /* DDR 0 - input, 1 - output */
28 #define FPGA_INIT_PDDIR FPGA_PRG | FPGA_CLK | FPGA_DATA /* just set outputs */
31 #define SET_FPGA(data) immr->im_ioport.iop_pddat = (data)
32 #define GET_FPGA immr->im_ioport.iop_pddat
34 #define FPGA_WRITE_1 { \
35 SET_FPGA(FPGA_PRG | FPGA_DATA); /* set clock to 0 */ \
36 SET_FPGA(FPGA_PRG | FPGA_DATA); /* set data to 1 */ \
37 SET_FPGA(FPGA_PRG | FPGA_CLK | FPGA_DATA); /* set clock to 1 */ \
38 SET_FPGA(FPGA_PRG | FPGA_CLK | FPGA_DATA);} /* set data to 1 */
40 #define FPGA_WRITE_0 { \
41 SET_FPGA(FPGA_PRG | FPGA_DATA); /* set clock to 0 */ \
42 SET_FPGA(FPGA_PRG); /* set data to 0 */ \
43 SET_FPGA(FPGA_PRG | FPGA_CLK); /* set clock to 1 */ \
44 SET_FPGA(FPGA_PRG | FPGA_CLK | FPGA_DATA);} /* set data to 1 */
47 int fpga_boot (unsigned char *fpgadata, int size)
49 volatile immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
53 #ifdef CONFIG_SYS_FPGA_SPARTAN2
61 debug ("fpga_boot: fpgadata = %p, size = %d\n", fpgadata, size);
63 /* display infos on fpgaimage */
66 for (i = 0; i < 4; i++) {
67 len = fpgadata[index];
68 printf (" %s", &(fpgadata[index + 1]));
76 #ifdef CONFIG_SYS_FPGA_SPARTAN2
77 /* search for preamble 0xFFFFFFFF */
79 if ((fpgadata[index] == 0xff) && (fpgadata[index + 1] == 0xff)
80 && (fpgadata[index + 2] == 0xff)
81 && (fpgadata[index + 3] == 0xff))
82 break; /* preamble found */
87 /* search for preamble 0xFF2X */
88 for (index = 0; index < size - 1; index++) {
89 if ((fpgadata[index] == 0xff)
90 && ((fpgadata[index + 1] & 0xf0) == 0x30))
96 debug ("FPGA: configdata starts at position 0x%x\n", index);
97 debug ("FPGA: length of fpga-data %d\n", size - index);
100 * Setup port pins for fpga programming
102 immr->im_ioport.iop_pddir = FPGA_INIT_PDDIR;
104 debug ("%s, ", ((GET_FPGA & FPGA_DONE) == 0) ? "NOT DONE" : "DONE");
105 debug ("%s\n", ((GET_FPGA & FPGA_INIT) == 0) ? "NOT INIT" : "INIT");
108 * Init fpga by asserting and deasserting PROGRAM*
110 SET_FPGA (FPGA_CLK | FPGA_DATA);
112 /* Wait for FPGA init line low */
114 while (GET_FPGA & FPGA_INIT) {
115 udelay (1000); /* wait 1ms */
116 /* Check for timeout - 100us max, so use 3ms */
118 debug ("FPGA: Booting failed!\n");
119 return ERROR_FPGA_PRG_INIT_LOW;
123 debug ("%s, ", ((GET_FPGA & FPGA_DONE) == 0) ? "NOT DONE" : "DONE");
124 debug ("%s\n", ((GET_FPGA & FPGA_INIT) == 0) ? "NOT INIT" : "INIT");
126 /* deassert PROGRAM* */
127 SET_FPGA (FPGA_PRG | FPGA_CLK | FPGA_DATA);
129 /* Wait for FPGA end of init period . */
131 while (!(GET_FPGA & FPGA_INIT)) {
132 udelay (1000); /* wait 1ms */
133 /* Check for timeout */
135 debug ("FPGA: Booting failed!\n");
136 return ERROR_FPGA_PRG_INIT_HIGH;
140 debug ("%s, ", ((GET_FPGA & FPGA_DONE) == 0) ? "NOT DONE" : "DONE");
141 debug ("%s\n", ((GET_FPGA & FPGA_INIT) == 0) ? "NOT INIT" : "INIT");
143 debug ("write configuration data into fpga\n");
144 /* write configuration-data into fpga... */
146 #ifdef CONFIG_SYS_FPGA_SPARTAN2
148 * Load uncompressed image into fpga
150 for (i = index; i < size; i++) {
151 #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
153 printf ("%6d out of %6d\r", i, size); /* let them know we are alive */
157 for (j = 0; j < 8; j++) {
158 if ((data & 0x80) == 0x80) {
166 /* add some 0xff to the end of the file */
167 for (i = 0; i < 8; i++) {
169 for (j = 0; j < 8; j++) {
170 if ((data & 0x80) == 0x80) {
199 ** Code 1 .. maxOnes : n '1's followed by '0'
200 ** maxOnes + 1 .. maxOnes + 1 : n - 1 '1's no '0'
201 ** maxOnes + 2 .. 254 : n - (maxOnes + 2) '0's followed by '1'
205 for (i = index; i < size; i++) {
207 if ((b >= 1) && (b <= MAX_ONES)) {
208 for (bit = 0; bit < b; bit++) {
212 } else if (b == (MAX_ONES + 1)) {
213 for (bit = 1; bit < b; bit++) {
216 } else if ((b >= (MAX_ONES + 2)) && (b <= 254)) {
217 for (bit = 0; bit < (b - (MAX_ONES + 2)); bit++) {
221 } else if (b == 255) {
227 debug ("%s, ", ((GET_FPGA & FPGA_DONE) == 0) ? "NOT DONE" : "DONE");
228 debug ("%s\n", ((GET_FPGA & FPGA_INIT) == 0) ? "NOT INIT" : "INIT");
231 * Check if fpga's DONE signal - correctly booted ?
234 /* Wait for FPGA end of programming period . */
236 while (!(GET_FPGA & FPGA_DONE)) {
237 udelay (1000); /* wait 1ms */
238 /* Check for timeout */
240 debug ("FPGA: Booting failed!\n");
241 return ERROR_FPGA_PRG_DONE;
245 debug ("FPGA: Booting successful!\n");