1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#undef DEBUG
26#include <common.h>
27#include <asm/processor.h>
28#include <command.h>
29#include "fpga.h"
30
31
32#define MAX_ONES 226
33
34
35#define PD(bit) (1 << (15 - (bit)))
36# define FPGA_INIT PD(11)
37# define FPGA_PRG PD(12)
38# define FPGA_CLK PD(13)
39# define FPGA_DATA PD(14)
40# define FPGA_DONE PD(15)
41
42
43
44#define FPGA_INIT_PDDIR FPGA_PRG | FPGA_CLK | FPGA_DATA
45
46
47#define SET_FPGA(data) immr->im_ioport.iop_pddat = (data)
48#define GET_FPGA immr->im_ioport.iop_pddat
49
50#define FPGA_WRITE_1 { \
51 SET_FPGA(FPGA_PRG | FPGA_DATA); \
52 SET_FPGA(FPGA_PRG | FPGA_DATA); \
53 SET_FPGA(FPGA_PRG | FPGA_CLK | FPGA_DATA); \
54 SET_FPGA(FPGA_PRG | FPGA_CLK | FPGA_DATA);}
55
56#define FPGA_WRITE_0 { \
57 SET_FPGA(FPGA_PRG | FPGA_DATA); \
58 SET_FPGA(FPGA_PRG); \
59 SET_FPGA(FPGA_PRG | FPGA_CLK); \
60 SET_FPGA(FPGA_PRG | FPGA_CLK | FPGA_DATA);}
61
62
63int fpga_boot (unsigned char *fpgadata, int size)
64{
65 volatile immap_t *immr = (immap_t *) CONFIG_SYS_IMMR;
66 int i, index, len;
67 int count;
68
69#ifdef CONFIG_SYS_FPGA_SPARTAN2
70 int j;
71 unsigned char data;
72#else
73 unsigned char b;
74 int bit;
75#endif
76
77 debug ("fpga_boot: fpgadata = %p, size = %d\n", fpgadata, size);
78
79
80 printf ("FPGA:");
81 index = 15;
82 for (i = 0; i < 4; i++) {
83 len = fpgadata[index];
84 printf (" %s", &(fpgadata[index + 1]));
85 index += len + 3;
86 }
87 printf ("\n");
88
89
90 index = 0;
91
92#ifdef CONFIG_SYS_FPGA_SPARTAN2
93
94 while (1) {
95 if ((fpgadata[index] == 0xff) && (fpgadata[index + 1] == 0xff)
96 && (fpgadata[index + 2] == 0xff)
97 && (fpgadata[index + 3] == 0xff))
98 break;
99 else
100 index++;
101 }
102#else
103
104 for (index = 0; index < size - 1; index++) {
105 if ((fpgadata[index] == 0xff)
106 && ((fpgadata[index + 1] & 0xf0) == 0x30))
107 break;
108 }
109 index += 2;
110#endif
111
112 debug ("FPGA: configdata starts at position 0x%x\n", index);
113 debug ("FPGA: length of fpga-data %d\n", size - index);
114
115
116
117
118 immr->im_ioport.iop_pddir = FPGA_INIT_PDDIR;
119
120 debug ("%s, ", ((GET_FPGA & FPGA_DONE) == 0) ? "NOT DONE" : "DONE");
121 debug ("%s\n", ((GET_FPGA & FPGA_INIT) == 0) ? "NOT INIT" : "INIT");
122
123
124
125
126 SET_FPGA (FPGA_CLK | FPGA_DATA);
127
128
129 count = 0;
130 while (GET_FPGA & FPGA_INIT) {
131 udelay (1000);
132
133 if (count++ > 3) {
134 debug ("FPGA: Booting failed!\n");
135 return ERROR_FPGA_PRG_INIT_LOW;
136 }
137 }
138
139 debug ("%s, ", ((GET_FPGA & FPGA_DONE) == 0) ? "NOT DONE" : "DONE");
140 debug ("%s\n", ((GET_FPGA & FPGA_INIT) == 0) ? "NOT INIT" : "INIT");
141
142
143 SET_FPGA (FPGA_PRG | FPGA_CLK | FPGA_DATA);
144
145
146 count = 0;
147 while (!(GET_FPGA & FPGA_INIT)) {
148 udelay (1000);
149
150 if (count++ > 3) {
151 debug ("FPGA: Booting failed!\n");
152 return ERROR_FPGA_PRG_INIT_HIGH;
153 }
154 }
155
156 debug ("%s, ", ((GET_FPGA & FPGA_DONE) == 0) ? "NOT DONE" : "DONE");
157 debug ("%s\n", ((GET_FPGA & FPGA_INIT) == 0) ? "NOT INIT" : "INIT");
158
159 debug ("write configuration data into fpga\n");
160
161
162#ifdef CONFIG_SYS_FPGA_SPARTAN2
163
164
165
166 for (i = index; i < size; i++) {
167#ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
168 if ((i % 1024) == 0)
169 printf ("%6d out of %6d\r", i, size);
170#endif
171
172 data = fpgadata[i];
173 for (j = 0; j < 8; j++) {
174 if ((data & 0x80) == 0x80) {
175 FPGA_WRITE_1;
176 } else {
177 FPGA_WRITE_0;
178 }
179 data <<= 1;
180 }
181 }
182
183 for (i = 0; i < 8; i++) {
184 data = 0xff;
185 for (j = 0; j < 8; j++) {
186 if ((data & 0x80) == 0x80) {
187 FPGA_WRITE_1;
188 } else {
189 FPGA_WRITE_0;
190 }
191 data <<= 1;
192 }
193 }
194#else
195
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_1;
203 FPGA_WRITE_1;
204 FPGA_WRITE_0;
205 FPGA_WRITE_0;
206 FPGA_WRITE_1;
207 FPGA_WRITE_0;
208 FPGA_WRITE_0;
209 FPGA_WRITE_0;
210 FPGA_WRITE_0;
211 FPGA_WRITE_0;
212
213
214
215
216
217
218
219
220
221 for (i = index; i < size; i++) {
222 b = fpgadata[i];
223 if ((b >= 1) && (b <= MAX_ONES)) {
224 for (bit = 0; bit < b; bit++) {
225 FPGA_WRITE_1;
226 }
227 FPGA_WRITE_0;
228 } else if (b == (MAX_ONES + 1)) {
229 for (bit = 1; bit < b; bit++) {
230 FPGA_WRITE_1;
231 }
232 } else if ((b >= (MAX_ONES + 2)) && (b <= 254)) {
233 for (bit = 0; bit < (b - (MAX_ONES + 2)); bit++) {
234 FPGA_WRITE_0;
235 }
236 FPGA_WRITE_1;
237 } else if (b == 255) {
238 FPGA_WRITE_1;
239 }
240 }
241#endif
242 debug ("\n\n");
243 debug ("%s, ", ((GET_FPGA & FPGA_DONE) == 0) ? "NOT DONE" : "DONE");
244 debug ("%s\n", ((GET_FPGA & FPGA_INIT) == 0) ? "NOT INIT" : "INIT");
245
246
247
248
249
250
251 count = 0;
252 while (!(GET_FPGA & FPGA_DONE)) {
253 udelay (1000);
254
255 if (count++ > 3) {
256 debug ("FPGA: Booting failed!\n");
257 return ERROR_FPGA_PRG_DONE;
258 }
259 }
260
261 debug ("FPGA: Booting successful!\n");
262 return 0;
263}
264