blob: 5c70b474d94b85f4b5846857a1d6b662b27d461a [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
stroese0621f6f2004-12-16 18:43:13 +00002 * (C) Copyright 2001-2004
wdenkc6097192002-11-03 00:24:07 +00003 * Matthias Fuchs, esd gmbh germany, matthias.fuchs@esd-electronics.com
4 * Stefan Roese, esd gmbh germany, stefan.roese@esd-electronics.com
5 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
wdenkc6097192002-11-03 00:24:07 +00007 */
8
9#include <common.h>
10#include <asm/processor.h>
Matthias Fuchsbb57ad42009-02-20 10:19:19 +010011#include <asm/io.h>
wdenkc6097192002-11-03 00:24:07 +000012#include <command.h>
13
14/* ------------------------------------------------------------------------- */
15
16#ifdef FPGA_DEBUG
17#define DBG(x...) printf(x)
18#else
19#define DBG(x...)
20#endif /* DEBUG */
21
22#define MAX_ONES 226
23
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020024#ifdef CONFIG_SYS_FPGA_PRG
25# define FPGA_PRG CONFIG_SYS_FPGA_PRG /* FPGA program pin (ppc output) */
26# define FPGA_CLK CONFIG_SYS_FPGA_CLK /* FPGA clk pin (ppc output) */
27# define FPGA_DATA CONFIG_SYS_FPGA_DATA /* FPGA data pin (ppc output) */
28# define FPGA_DONE CONFIG_SYS_FPGA_DONE /* FPGA done pin (ppc input) */
29# define FPGA_INIT CONFIG_SYS_FPGA_INIT /* FPGA init pin (ppc input) */
wdenkc6097192002-11-03 00:24:07 +000030#else
Wolfgang Denk2b3e7e62008-03-09 10:50:41 +010031# define FPGA_PRG 0x04000000 /* FPGA program pin (ppc output) */
32# define FPGA_CLK 0x02000000 /* FPGA clk pin (ppc output) */
33# define FPGA_DATA 0x01000000 /* FPGA data pin (ppc output) */
34# define FPGA_DONE 0x00800000 /* FPGA done pin (ppc input) */
35# define FPGA_INIT 0x00400000 /* FPGA init pin (ppc input) */
wdenkc6097192002-11-03 00:24:07 +000036#endif
37
Wolfgang Denk2b3e7e62008-03-09 10:50:41 +010038#define ERROR_FPGA_PRG_INIT_LOW -1 /* Timeout after PRG* asserted */
39#define ERROR_FPGA_PRG_INIT_HIGH -2 /* Timeout after PRG* deasserted */
40#define ERROR_FPGA_PRG_DONE -3 /* Timeout after programming */
wdenkc6097192002-11-03 00:24:07 +000041
stroese0621f6f2004-12-16 18:43:13 +000042#ifndef SET_FPGA
Matthias Fuchsbb57ad42009-02-20 10:19:19 +010043# define SET_FPGA(data) out_be32((void *)GPIO0_OR, data)
stroese0621f6f2004-12-16 18:43:13 +000044#endif
wdenkc6097192002-11-03 00:24:07 +000045
stroese0621f6f2004-12-16 18:43:13 +000046#ifdef FPGA_PROG_ACTIVE_HIGH
47# define FPGA_PRG_LOW FPGA_PRG
48# define FPGA_PRG_HIGH 0
49#else
50# define FPGA_PRG_LOW 0
51# define FPGA_PRG_HIGH FPGA_PRG
52#endif
53
54#define FPGA_CLK_LOW 0
55#define FPGA_CLK_HIGH FPGA_CLK
56
57#define FPGA_DATA_LOW 0
58#define FPGA_DATA_HIGH FPGA_DATA
59
60#define FPGA_WRITE_1 { \
61 SET_FPGA(FPGA_PRG_HIGH | FPGA_CLK_LOW | FPGA_DATA_HIGH); /* set clock to 0 */ \
62 SET_FPGA(FPGA_PRG_HIGH | FPGA_CLK_LOW | FPGA_DATA_HIGH); /* set data to 1 */ \
63 SET_FPGA(FPGA_PRG_HIGH | FPGA_CLK_HIGH | FPGA_DATA_HIGH); /* set clock to 1 */ \
Wolfgang Denk2b3e7e62008-03-09 10:50:41 +010064 SET_FPGA(FPGA_PRG_HIGH | FPGA_CLK_HIGH | FPGA_DATA_HIGH);} /* set data to 1 */
wdenkc6097192002-11-03 00:24:07 +000065
66#define FPGA_WRITE_0 { \
stroese0621f6f2004-12-16 18:43:13 +000067 SET_FPGA(FPGA_PRG_HIGH | FPGA_CLK_LOW | FPGA_DATA_HIGH); /* set clock to 0 */ \
68 SET_FPGA(FPGA_PRG_HIGH | FPGA_CLK_LOW | FPGA_DATA_LOW); /* set data to 0 */ \
69 SET_FPGA(FPGA_PRG_HIGH | FPGA_CLK_HIGH | FPGA_DATA_LOW); /* set clock to 1 */ \
Wolfgang Denk2b3e7e62008-03-09 10:50:41 +010070 SET_FPGA(FPGA_PRG_HIGH | FPGA_CLK_HIGH | FPGA_DATA_HIGH);} /* set data to 1 */
stroese0621f6f2004-12-16 18:43:13 +000071
72#ifndef FPGA_DONE_STATE
Matthias Fuchsbb57ad42009-02-20 10:19:19 +010073# define FPGA_DONE_STATE (in_be32((void *)GPIO0_IR) & FPGA_DONE)
stroese0621f6f2004-12-16 18:43:13 +000074#endif
75#ifndef FPGA_INIT_STATE
Matthias Fuchsbb57ad42009-02-20 10:19:19 +010076# define FPGA_INIT_STATE (in_be32((void *)GPIO0_IR) & FPGA_INIT)
stroese0621f6f2004-12-16 18:43:13 +000077#endif
wdenkc6097192002-11-03 00:24:07 +000078
79
Wolfgang Denk2b3e7e62008-03-09 10:50:41 +010080static int fpga_boot (const unsigned char *fpgadata, int size)
wdenkc6097192002-11-03 00:24:07 +000081{
Wolfgang Denk2b3e7e62008-03-09 10:50:41 +010082 int i, index, len;
83 int count;
84 unsigned char b;
wdenkc6097192002-11-03 00:24:07 +000085
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020086#ifdef CONFIG_SYS_FPGA_SPARTAN2
Wolfgang Denk2b3e7e62008-03-09 10:50:41 +010087 int j;
wdenkc6097192002-11-03 00:24:07 +000088#else
Wolfgang Denk2b3e7e62008-03-09 10:50:41 +010089 int bit;
wdenkc6097192002-11-03 00:24:07 +000090#endif
91
Wolfgang Denk2b3e7e62008-03-09 10:50:41 +010092 /* display infos on fpgaimage */
93 index = 15;
94 for (i = 0; i < 4; i++) {
95 len = fpgadata[index];
96 DBG ("FPGA: %s\n", &(fpgadata[index + 1]));
97 index += len + 3;
98 }
wdenkc6097192002-11-03 00:24:07 +000099
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200100#ifdef CONFIG_SYS_FPGA_SPARTAN2
Wolfgang Denk2b3e7e62008-03-09 10:50:41 +0100101 /* search for preamble 0xFFFFFFFF */
102 while (1) {
103 if ((fpgadata[index] == 0xff) && (fpgadata[index + 1] == 0xff)
104 && (fpgadata[index + 2] == 0xff)
105 && (fpgadata[index + 3] == 0xff))
106 break; /* preamble found */
107 else
108 index++;
109 }
110#else
111 /* search for preamble 0xFF2X */
112 for (index = 0; index < size - 1; index++) {
113 if ((fpgadata[index] == 0xff)
114 && ((fpgadata[index + 1] & 0xf0) == 0x30))
115 break;
116 }
117 index += 2;
118#endif
119
120 DBG ("FPGA: configdata starts at position 0x%x\n", index);
121 DBG ("FPGA: length of fpga-data %d\n", size - index);
122
123 /*
124 * Setup port pins for fpga programming
125 */
stroese0621f6f2004-12-16 18:43:13 +0000126#ifndef CONFIG_M5249
Matthias Fuchsbb57ad42009-02-20 10:19:19 +0100127 out_be32 ((void *)GPIO0_ODR, 0x00000000); /* no open drain pins */
128 /* setup for output */
129 out_be32 ((void *)GPIO0_TCR,
130 in_be32 ((void *)GPIO0_TCR) |
131 FPGA_PRG | FPGA_CLK | FPGA_DATA);
stroese0621f6f2004-12-16 18:43:13 +0000132#endif
Wolfgang Denk2b3e7e62008-03-09 10:50:41 +0100133 SET_FPGA (FPGA_PRG_HIGH | FPGA_CLK_HIGH | FPGA_DATA_HIGH); /* set pins to high */
wdenkc6097192002-11-03 00:24:07 +0000134
Wolfgang Denk2b3e7e62008-03-09 10:50:41 +0100135 DBG ("%s, ", (FPGA_DONE_STATE == 0) ? "NOT DONE" : "DONE");
136 DBG ("%s\n", (FPGA_INIT_STATE == 0) ? "NOT INIT" : "INIT");
wdenkc6097192002-11-03 00:24:07 +0000137
Wolfgang Denk2b3e7e62008-03-09 10:50:41 +0100138 /*
139 * Init fpga by asserting and deasserting PROGRAM*
140 */
141 SET_FPGA (FPGA_PRG_LOW | FPGA_CLK_HIGH | FPGA_DATA_HIGH); /* set prog active */
wdenkc6097192002-11-03 00:24:07 +0000142
Wolfgang Denk2b3e7e62008-03-09 10:50:41 +0100143 /* Wait for FPGA init line low */
144 count = 0;
145 while (FPGA_INIT_STATE) {
146 udelay (1000); /* wait 1ms */
147 /* Check for timeout - 100us max, so use 3ms */
148 if (count++ > 3) {
149 DBG ("FPGA: Booting failed!\n");
150 return ERROR_FPGA_PRG_INIT_LOW;
151 }
wdenk8bde7f72003-06-27 21:31:46 +0000152 }
wdenkc6097192002-11-03 00:24:07 +0000153
Wolfgang Denk2b3e7e62008-03-09 10:50:41 +0100154 DBG ("%s, ", (FPGA_DONE_STATE == 0) ? "NOT DONE" : "DONE");
155 DBG ("%s\n", (FPGA_INIT_STATE == 0) ? "NOT INIT" : "INIT");
wdenkc6097192002-11-03 00:24:07 +0000156
Wolfgang Denk2b3e7e62008-03-09 10:50:41 +0100157 /* deassert PROGRAM* */
158 SET_FPGA (FPGA_PRG_HIGH | FPGA_CLK_HIGH | FPGA_DATA_HIGH); /* set prog inactive */
wdenkc6097192002-11-03 00:24:07 +0000159
Wolfgang Denk2b3e7e62008-03-09 10:50:41 +0100160 /* Wait for FPGA end of init period . */
161 count = 0;
162 while (!(FPGA_INIT_STATE)) {
163 udelay (1000); /* wait 1ms */
164 /* Check for timeout */
165 if (count++ > 3) {
166 DBG ("FPGA: Booting failed!\n");
167 return ERROR_FPGA_PRG_INIT_HIGH;
168 }
wdenk8bde7f72003-06-27 21:31:46 +0000169 }
wdenkc6097192002-11-03 00:24:07 +0000170
Wolfgang Denk2b3e7e62008-03-09 10:50:41 +0100171 DBG ("%s, ", (FPGA_DONE_STATE == 0) ? "NOT DONE" : "DONE");
172 DBG ("%s\n", (FPGA_INIT_STATE == 0) ? "NOT INIT" : "INIT");
wdenkc6097192002-11-03 00:24:07 +0000173
Wolfgang Denk2b3e7e62008-03-09 10:50:41 +0100174 DBG ("write configuration data into fpga\n");
175 /* write configuration-data into fpga... */
wdenkc6097192002-11-03 00:24:07 +0000176
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200177#ifdef CONFIG_SYS_FPGA_SPARTAN2
Wolfgang Denk2b3e7e62008-03-09 10:50:41 +0100178 /*
179 * Load uncompressed image into fpga
180 */
181 for (i = index; i < size; i++) {
182 b = fpgadata[i];
183 for (j = 0; j < 8; j++) {
184 if ((b & 0x80) == 0x80) {
185 FPGA_WRITE_1;
186 } else {
187 FPGA_WRITE_0;
188 }
189 b <<= 1;
190 }
wdenk8bde7f72003-06-27 21:31:46 +0000191 }
wdenkc6097192002-11-03 00:24:07 +0000192#else
Wolfgang Denk2b3e7e62008-03-09 10:50:41 +0100193 /* send 0xff 0x20 */
194 FPGA_WRITE_1;
195 FPGA_WRITE_1;
196 FPGA_WRITE_1;
197 FPGA_WRITE_1;
198 FPGA_WRITE_1;
199 FPGA_WRITE_1;
200 FPGA_WRITE_1;
201 FPGA_WRITE_1;
202 FPGA_WRITE_0;
203 FPGA_WRITE_0;
204 FPGA_WRITE_1;
205 FPGA_WRITE_0;
206 FPGA_WRITE_0;
207 FPGA_WRITE_0;
208 FPGA_WRITE_0;
209 FPGA_WRITE_0;
wdenkc6097192002-11-03 00:24:07 +0000210
Wolfgang Denk2b3e7e62008-03-09 10:50:41 +0100211 /*
212 ** Bit_DeCompression
213 ** Code 1 .. maxOnes : n '1's followed by '0'
214 ** maxOnes + 1 .. maxOnes + 1 : n - 1 '1's no '0'
215 ** maxOnes + 2 .. 254 : n - (maxOnes + 2) '0's followed by '1'
216 ** 255 : '1'
217 */
wdenkc6097192002-11-03 00:24:07 +0000218
Wolfgang Denk2b3e7e62008-03-09 10:50:41 +0100219 for (i = index; i < size; i++) {
220 b = fpgadata[i];
221 if ((b >= 1) && (b <= MAX_ONES)) {
222 for (bit = 0; bit < b; bit++) {
223 FPGA_WRITE_1;
224 }
225 FPGA_WRITE_0;
226 } else if (b == (MAX_ONES + 1)) {
227 for (bit = 1; bit < b; bit++) {
228 FPGA_WRITE_1;
229 }
230 } else if ((b >= (MAX_ONES + 2)) && (b <= 254)) {
231 for (bit = 0; bit < (b - (MAX_ONES + 2)); bit++) {
232 FPGA_WRITE_0;
233 }
234 FPGA_WRITE_1;
235 } else if (b == 255) {
236 FPGA_WRITE_1;
237 }
wdenkc6097192002-11-03 00:24:07 +0000238 }
wdenkc6097192002-11-03 00:24:07 +0000239#endif
240
Wolfgang Denk2b3e7e62008-03-09 10:50:41 +0100241 DBG ("%s, ", (FPGA_DONE_STATE == 0) ? "NOT DONE" : "DONE");
242 DBG ("%s\n", (FPGA_INIT_STATE == 0) ? "NOT INIT" : "INIT");
wdenkc6097192002-11-03 00:24:07 +0000243
Wolfgang Denk2b3e7e62008-03-09 10:50:41 +0100244 /*
245 * Check if fpga's DONE signal - correctly booted ?
246 */
wdenkc6097192002-11-03 00:24:07 +0000247
Wolfgang Denk2b3e7e62008-03-09 10:50:41 +0100248 /* Wait for FPGA end of programming period . */
249 count = 0;
250 while (!(FPGA_DONE_STATE)) {
251 udelay (1000); /* wait 1ms */
252 /* Check for timeout */
253 if (count++ > 3) {
254 DBG ("FPGA: Booting failed!\n");
255 return ERROR_FPGA_PRG_DONE;
256 }
wdenk8bde7f72003-06-27 21:31:46 +0000257 }
wdenkc6097192002-11-03 00:24:07 +0000258
Wolfgang Denk2b3e7e62008-03-09 10:50:41 +0100259 DBG ("FPGA: Booting successful!\n");
260 return 0;
wdenkc6097192002-11-03 00:24:07 +0000261}