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