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
26
27
28#include <common.h>
29#include <ACEX1K.h>
30
31
32#ifdef FPGA_DEBUG
33#define PRINTF(fmt,args...) printf (fmt ,##args)
34#else
35#define PRINTF(fmt,args...)
36#endif
37
38
39
40
41
42
43#ifndef CONFIG_FPGA_DELAY
44#define CONFIG_FPGA_DELAY()
45#endif
46
47#ifndef CONFIG_SYS_FPGA_WAIT
48#define CONFIG_SYS_FPGA_WAIT CONFIG_SYS_HZ/10
49#endif
50
51static int ACEX1K_ps_load( Altera_desc *desc, void *buf, size_t bsize );
52static int ACEX1K_ps_dump( Altera_desc *desc, void *buf, size_t bsize );
53
54static int ACEX1K_ps_reloc( Altera_desc *desc, ulong reloc_offset );
55
56
57
58int ACEX1K_load (Altera_desc * desc, void *buf, size_t bsize)
59{
60 int ret_val = FPGA_FAIL;
61
62 switch (desc->iface) {
63 case passive_serial:
64 PRINTF ("%s: Launching Passive Serial Loader\n", __FUNCTION__);
65 ret_val = ACEX1K_ps_load (desc, buf, bsize);
66 break;
67
68
69
70 default:
71 printf ("%s: Unsupported interface type, %d\n",
72 __FUNCTION__, desc->iface);
73 }
74
75 return ret_val;
76}
77
78int ACEX1K_dump (Altera_desc * desc, void *buf, size_t bsize)
79{
80 int ret_val = FPGA_FAIL;
81
82 switch (desc->iface) {
83 case passive_serial:
84 PRINTF ("%s: Launching Passive Serial Dump\n", __FUNCTION__);
85 ret_val = ACEX1K_ps_dump (desc, buf, bsize);
86 break;
87
88
89
90 default:
91 printf ("%s: Unsupported interface type, %d\n",
92 __FUNCTION__, desc->iface);
93 }
94
95 return ret_val;
96}
97
98int ACEX1K_info( Altera_desc *desc )
99{
100 return FPGA_SUCCESS;
101}
102
103
104int ACEX1K_reloc (Altera_desc * desc, ulong reloc_offset)
105{
106 int ret_val = FPGA_FAIL;
107
108 if (desc->family != Altera_ACEX1K) {
109 printf ("%s: Unsupported family type, %d\n",
110 __FUNCTION__, desc->family);
111 return FPGA_FAIL;
112 } else
113 switch (desc->iface) {
114 case passive_serial:
115 ret_val = ACEX1K_ps_reloc (desc, reloc_offset);
116 break;
117
118
119
120 default:
121 printf ("%s: Unsupported interface type, %d\n",
122 __FUNCTION__, desc->iface);
123 }
124
125 return ret_val;
126}
127
128
129
130
131
132static int ACEX1K_ps_load (Altera_desc * desc, void *buf, size_t bsize)
133{
134 int ret_val = FPGA_FAIL;
135 Altera_ACEX1K_Passive_Serial_fns *fn = desc->iface_fns;
136 int i;
137
138 PRINTF ("%s: start with interface functions @ 0x%p\n",
139 __FUNCTION__, fn);
140
141 if (fn) {
142 size_t bytecount = 0;
143 unsigned char *data = (unsigned char *) buf;
144 int cookie = desc->cookie;
145 unsigned long ts;
146
147 PRINTF ("%s: Function Table:\n"
148 "ptr:\t0x%p\n"
149 "struct: 0x%p\n"
150 "config:\t0x%p\n"
151 "status:\t0x%p\n"
152 "clk:\t0x%p\n"
153 "data:\t0x%p\n"
154 "done:\t0x%p\n\n",
155 __FUNCTION__, &fn, fn, fn->config, fn->status,
156 fn->clk, fn->data, fn->done);
157#ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
158 printf ("Loading FPGA Device %d...", cookie);
159#endif
160
161
162
163
164 if (*fn->pre) {
165 (*fn->pre) (cookie);
166 }
167
168
169 (*fn->config) (TRUE, TRUE, cookie);
170
171 udelay(2);
172
173
174 (*fn->done) (cookie);
175 if ( !(*fn->status) (cookie) ) {
176 puts ("** nSTATUS is not asserted.\n");
177 (*fn->abort) (cookie);
178 return FPGA_FAIL;
179 }
180
181 (*fn->config) (FALSE, TRUE, cookie);
182 udelay(2);
183
184
185 ts = get_timer (0);
186 do {
187 CONFIG_FPGA_DELAY ();
188 if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT) {
189 puts ("** Timeout waiting for STATUS to go high.\n");
190 (*fn->abort) (cookie);
191 return FPGA_FAIL;
192 }
193 (*fn->done) (cookie);
194 } while ((*fn->status) (cookie));
195
196
197 CONFIG_FPGA_DELAY ();
198
199
200 while (bytecount < bsize) {
201 unsigned char val=0;
202#ifdef CONFIG_SYS_FPGA_CHECK_CTRLC
203 if (ctrlc ()) {
204 (*fn->abort) (cookie);
205 return FPGA_FAIL;
206 }
207#endif
208
209
210#if 0
211 if ((*fn->done) (cookie) == 0 && (*fn->init) (cookie)) {
212 puts ("** CRC error during FPGA load.\n");
213 (*fn->abort) (cookie);
214 return (FPGA_FAIL);
215 }
216#endif
217 val = data [bytecount ++ ];
218 i = 8;
219 do {
220
221 (*fn->clk) (FALSE, TRUE, cookie);
222 CONFIG_FPGA_DELAY ();
223
224 (*fn->data) ( (val & 0x01), TRUE, cookie);
225 CONFIG_FPGA_DELAY ();
226
227 (*fn->clk) (TRUE, TRUE, cookie);
228 CONFIG_FPGA_DELAY ();
229 val >>= 1;
230 i --;
231 } while (i > 0);
232
233#ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
234 if (bytecount % (bsize / 40) == 0)
235 putc ('.');
236#endif
237 }
238
239 CONFIG_FPGA_DELAY ();
240
241#ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
242 putc (' ');
243#endif
244
245
246
247
248
249 if ( ! (*fn->done) (cookie) ) {
250 puts ("** Booting failed! CONF_DONE is still deasserted.\n");
251 (*fn->abort) (cookie);
252 return (FPGA_FAIL);
253 }
254
255
256
257
258
259 for (i = 0; i < 12; i++) {
260 CONFIG_FPGA_DELAY ();
261 (*fn->clk) (TRUE, TRUE, cookie);
262 CONFIG_FPGA_DELAY ();
263 (*fn->clk) (FALSE, TRUE, cookie);
264 }
265
266 ret_val = FPGA_SUCCESS;
267
268#ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
269 if (ret_val == FPGA_SUCCESS) {
270 puts ("Done.\n");
271 }
272 else {
273 puts ("Fail.\n");
274 }
275#endif
276 (*fn->post) (cookie);
277
278 } else {
279 printf ("%s: NULL Interface function table!\n", __FUNCTION__);
280 }
281
282 return ret_val;
283}
284
285static int ACEX1K_ps_dump (Altera_desc * desc, void *buf, size_t bsize)
286{
287
288
289 printf ("%s: Passive Serial Dumping is unavailable\n",
290 __FUNCTION__);
291 return FPGA_FAIL;
292}
293
294static int ACEX1K_ps_reloc (Altera_desc * desc, ulong reloc_offset)
295{
296 int ret_val = FPGA_FAIL;
297 Altera_ACEX1K_Passive_Serial_fns *fn_r, *fn =
298 (Altera_ACEX1K_Passive_Serial_fns *) (desc->iface_fns);
299
300 if (fn) {
301 ulong addr;
302
303
304 addr = (ulong) fn + reloc_offset;
305 fn_r = (Altera_ACEX1K_Passive_Serial_fns *) addr;
306
307 if (!fn_r->relocated) {
308
309 if (memcmp (fn_r, fn,
310 sizeof (Altera_ACEX1K_Passive_Serial_fns))
311 == 0) {
312
313 desc->iface_fns = fn_r;
314 } else {
315 PRINTF ("%s: Invalid function table at 0x%p\n",
316 __FUNCTION__, fn_r);
317 return FPGA_FAIL;
318 }
319
320 PRINTF ("%s: Relocating descriptor at 0x%p\n", __FUNCTION__,
321 desc);
322
323 addr = (ulong) (fn->pre) + reloc_offset;
324 fn_r->pre = (Altera_pre_fn) addr;
325
326 addr = (ulong) (fn->config) + reloc_offset;
327 fn_r->config = (Altera_config_fn) addr;
328
329 addr = (ulong) (fn->status) + reloc_offset;
330 fn_r->status = (Altera_status_fn) addr;
331
332 addr = (ulong) (fn->done) + reloc_offset;
333 fn_r->done = (Altera_done_fn) addr;
334
335 addr = (ulong) (fn->clk) + reloc_offset;
336 fn_r->clk = (Altera_clk_fn) addr;
337
338 addr = (ulong) (fn->data) + reloc_offset;
339 fn_r->data = (Altera_data_fn) addr;
340
341 addr = (ulong) (fn->abort) + reloc_offset;
342 fn_r->abort = (Altera_abort_fn) addr;
343
344 addr = (ulong) (fn->post) + reloc_offset;
345 fn_r->post = (Altera_post_fn) addr;
346
347 fn_r->relocated = TRUE;
348
349 } else {
350
351
352 desc->iface_fns = fn_r;
353 }
354
355 ret_val = FPGA_SUCCESS;
356 } else {
357 printf ("%s: NULL Interface function table!\n", __FUNCTION__);
358 }
359
360 return ret_val;
361
362}
363