xref: /OK3568_Linux_fs/u-boot/drivers/fpga/spartan2.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * (C) Copyright 2002
3*4882a593Smuzhiyun  * Rich Ireland, Enterasys Networks, rireland@enterasys.com.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <common.h>		/* core U-Boot definitions */
9*4882a593Smuzhiyun #include <spartan2.h>		/* Spartan-II device family */
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun /* Define FPGA_DEBUG to get debug printf's */
12*4882a593Smuzhiyun #ifdef	FPGA_DEBUG
13*4882a593Smuzhiyun #define PRINTF(fmt,args...)	printf (fmt ,##args)
14*4882a593Smuzhiyun #else
15*4882a593Smuzhiyun #define PRINTF(fmt,args...)
16*4882a593Smuzhiyun #endif
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #undef CONFIG_SYS_FPGA_CHECK_BUSY
19*4882a593Smuzhiyun #undef CONFIG_SYS_FPGA_PROG_FEEDBACK
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun /* Note: The assumption is that we cannot possibly run fast enough to
22*4882a593Smuzhiyun  * overrun the device (the Slave Parallel mode can free run at 50MHz).
23*4882a593Smuzhiyun  * If there is a need to operate slower, define CONFIG_FPGA_DELAY in
24*4882a593Smuzhiyun  * the board config file to slow things down.
25*4882a593Smuzhiyun  */
26*4882a593Smuzhiyun #ifndef CONFIG_FPGA_DELAY
27*4882a593Smuzhiyun #define CONFIG_FPGA_DELAY()
28*4882a593Smuzhiyun #endif
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun #ifndef CONFIG_SYS_FPGA_WAIT
31*4882a593Smuzhiyun #define CONFIG_SYS_FPGA_WAIT CONFIG_SYS_HZ/100	/* 10 ms */
32*4882a593Smuzhiyun #endif
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun static int spartan2_sp_load(xilinx_desc *desc, const void *buf, size_t bsize);
35*4882a593Smuzhiyun static int spartan2_sp_dump(xilinx_desc *desc, const void *buf, size_t bsize);
36*4882a593Smuzhiyun /* static int spartan2_sp_info(xilinx_desc *desc ); */
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun static int spartan2_ss_load(xilinx_desc *desc, const void *buf, size_t bsize);
39*4882a593Smuzhiyun static int spartan2_ss_dump(xilinx_desc *desc, const void *buf, size_t bsize);
40*4882a593Smuzhiyun /* static int spartan2_ss_info(xilinx_desc *desc ); */
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun /* ------------------------------------------------------------------------- */
43*4882a593Smuzhiyun /* Spartan-II Generic Implementation */
spartan2_load(xilinx_desc * desc,const void * buf,size_t bsize,bitstream_type bstype)44*4882a593Smuzhiyun static int spartan2_load(xilinx_desc *desc, const void *buf, size_t bsize,
45*4882a593Smuzhiyun 			 bitstream_type bstype)
46*4882a593Smuzhiyun {
47*4882a593Smuzhiyun 	int ret_val = FPGA_FAIL;
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	switch (desc->iface) {
50*4882a593Smuzhiyun 	case slave_serial:
51*4882a593Smuzhiyun 		PRINTF ("%s: Launching Slave Serial Load\n", __FUNCTION__);
52*4882a593Smuzhiyun 		ret_val = spartan2_ss_load(desc, buf, bsize);
53*4882a593Smuzhiyun 		break;
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun 	case slave_parallel:
56*4882a593Smuzhiyun 		PRINTF ("%s: Launching Slave Parallel Load\n", __FUNCTION__);
57*4882a593Smuzhiyun 		ret_val = spartan2_sp_load(desc, buf, bsize);
58*4882a593Smuzhiyun 		break;
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	default:
61*4882a593Smuzhiyun 		printf ("%s: Unsupported interface type, %d\n",
62*4882a593Smuzhiyun 				__FUNCTION__, desc->iface);
63*4882a593Smuzhiyun 	}
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun 	return ret_val;
66*4882a593Smuzhiyun }
67*4882a593Smuzhiyun 
spartan2_dump(xilinx_desc * desc,const void * buf,size_t bsize)68*4882a593Smuzhiyun static int spartan2_dump(xilinx_desc *desc, const void *buf, size_t bsize)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun 	int ret_val = FPGA_FAIL;
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 	switch (desc->iface) {
73*4882a593Smuzhiyun 	case slave_serial:
74*4882a593Smuzhiyun 		PRINTF ("%s: Launching Slave Serial Dump\n", __FUNCTION__);
75*4882a593Smuzhiyun 		ret_val = spartan2_ss_dump(desc, buf, bsize);
76*4882a593Smuzhiyun 		break;
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 	case slave_parallel:
79*4882a593Smuzhiyun 		PRINTF ("%s: Launching Slave Parallel Dump\n", __FUNCTION__);
80*4882a593Smuzhiyun 		ret_val = spartan2_sp_dump(desc, buf, bsize);
81*4882a593Smuzhiyun 		break;
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 	default:
84*4882a593Smuzhiyun 		printf ("%s: Unsupported interface type, %d\n",
85*4882a593Smuzhiyun 				__FUNCTION__, desc->iface);
86*4882a593Smuzhiyun 	}
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun 	return ret_val;
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun 
spartan2_info(xilinx_desc * desc)91*4882a593Smuzhiyun static int spartan2_info(xilinx_desc *desc)
92*4882a593Smuzhiyun {
93*4882a593Smuzhiyun 	return FPGA_SUCCESS;
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun /* ------------------------------------------------------------------------- */
98*4882a593Smuzhiyun /* Spartan-II Slave Parallel Generic Implementation */
99*4882a593Smuzhiyun 
spartan2_sp_load(xilinx_desc * desc,const void * buf,size_t bsize)100*4882a593Smuzhiyun static int spartan2_sp_load(xilinx_desc *desc, const void *buf, size_t bsize)
101*4882a593Smuzhiyun {
102*4882a593Smuzhiyun 	int ret_val = FPGA_FAIL;	/* assume the worst */
103*4882a593Smuzhiyun 	xilinx_spartan2_slave_parallel_fns *fn = desc->iface_fns;
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 	PRINTF ("%s: start with interface functions @ 0x%p\n",
106*4882a593Smuzhiyun 			__FUNCTION__, fn);
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 	if (fn) {
109*4882a593Smuzhiyun 		size_t bytecount = 0;
110*4882a593Smuzhiyun 		unsigned char *data = (unsigned char *) buf;
111*4882a593Smuzhiyun 		int cookie = desc->cookie;	/* make a local copy */
112*4882a593Smuzhiyun 		unsigned long ts;		/* timestamp */
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun 		PRINTF ("%s: Function Table:\n"
115*4882a593Smuzhiyun 				"ptr:\t0x%p\n"
116*4882a593Smuzhiyun 				"struct: 0x%p\n"
117*4882a593Smuzhiyun 				"pre: 0x%p\n"
118*4882a593Smuzhiyun 				"pgm:\t0x%p\n"
119*4882a593Smuzhiyun 				"init:\t0x%p\n"
120*4882a593Smuzhiyun 				"err:\t0x%p\n"
121*4882a593Smuzhiyun 				"clk:\t0x%p\n"
122*4882a593Smuzhiyun 				"cs:\t0x%p\n"
123*4882a593Smuzhiyun 				"wr:\t0x%p\n"
124*4882a593Smuzhiyun 				"read data:\t0x%p\n"
125*4882a593Smuzhiyun 				"write data:\t0x%p\n"
126*4882a593Smuzhiyun 				"busy:\t0x%p\n"
127*4882a593Smuzhiyun 				"abort:\t0x%p\n",
128*4882a593Smuzhiyun 				"post:\t0x%p\n\n",
129*4882a593Smuzhiyun 				__FUNCTION__, &fn, fn, fn->pre, fn->pgm, fn->init, fn->err,
130*4882a593Smuzhiyun 				fn->clk, fn->cs, fn->wr, fn->rdata, fn->wdata, fn->busy,
131*4882a593Smuzhiyun 				fn->abort, fn->post);
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun 		/*
134*4882a593Smuzhiyun 		 * This code is designed to emulate the "Express Style"
135*4882a593Smuzhiyun 		 * Continuous Data Loading in Slave Parallel Mode for
136*4882a593Smuzhiyun 		 * the Spartan-II Family.
137*4882a593Smuzhiyun 		 */
138*4882a593Smuzhiyun #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
139*4882a593Smuzhiyun 		printf ("Loading FPGA Device %d...\n", cookie);
140*4882a593Smuzhiyun #endif
141*4882a593Smuzhiyun 		/*
142*4882a593Smuzhiyun 		 * Run the pre configuration function if there is one.
143*4882a593Smuzhiyun 		 */
144*4882a593Smuzhiyun 		if (*fn->pre) {
145*4882a593Smuzhiyun 			(*fn->pre) (cookie);
146*4882a593Smuzhiyun 		}
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 		/* Establish the initial state */
149*4882a593Smuzhiyun 		(*fn->pgm) (true, true, cookie);	/* Assert the program, commit */
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 		/* Get ready for the burn */
152*4882a593Smuzhiyun 		CONFIG_FPGA_DELAY ();
153*4882a593Smuzhiyun 		(*fn->pgm) (false, true, cookie);	/* Deassert the program, commit */
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 		ts = get_timer (0);		/* get current time */
156*4882a593Smuzhiyun 		/* Now wait for INIT and BUSY to go high */
157*4882a593Smuzhiyun 		do {
158*4882a593Smuzhiyun 			CONFIG_FPGA_DELAY ();
159*4882a593Smuzhiyun 			if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT) {	/* check the time */
160*4882a593Smuzhiyun 				puts ("** Timeout waiting for INIT to clear.\n");
161*4882a593Smuzhiyun 				(*fn->abort) (cookie);	/* abort the burn */
162*4882a593Smuzhiyun 				return FPGA_FAIL;
163*4882a593Smuzhiyun 			}
164*4882a593Smuzhiyun 		} while ((*fn->init) (cookie) && (*fn->busy) (cookie));
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 		(*fn->wr) (true, true, cookie); /* Assert write, commit */
167*4882a593Smuzhiyun 		(*fn->cs) (true, true, cookie); /* Assert chip select, commit */
168*4882a593Smuzhiyun 		(*fn->clk) (true, true, cookie);	/* Assert the clock pin */
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 		/* Load the data */
171*4882a593Smuzhiyun 		while (bytecount < bsize) {
172*4882a593Smuzhiyun 			/* XXX - do we check for an Ctrl-C press in here ??? */
173*4882a593Smuzhiyun 			/* XXX - Check the error bit? */
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 			(*fn->wdata) (data[bytecount++], true, cookie); /* write the data */
176*4882a593Smuzhiyun 			CONFIG_FPGA_DELAY ();
177*4882a593Smuzhiyun 			(*fn->clk) (false, true, cookie);	/* Deassert the clock pin */
178*4882a593Smuzhiyun 			CONFIG_FPGA_DELAY ();
179*4882a593Smuzhiyun 			(*fn->clk) (true, true, cookie);	/* Assert the clock pin */
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun #ifdef CONFIG_SYS_FPGA_CHECK_BUSY
182*4882a593Smuzhiyun 			ts = get_timer (0);	/* get current time */
183*4882a593Smuzhiyun 			while ((*fn->busy) (cookie)) {
184*4882a593Smuzhiyun 				/* XXX - we should have a check in here somewhere to
185*4882a593Smuzhiyun 				 * make sure we aren't busy forever... */
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun 				CONFIG_FPGA_DELAY ();
188*4882a593Smuzhiyun 				(*fn->clk) (false, true, cookie);	/* Deassert the clock pin */
189*4882a593Smuzhiyun 				CONFIG_FPGA_DELAY ();
190*4882a593Smuzhiyun 				(*fn->clk) (true, true, cookie);	/* Assert the clock pin */
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun 				if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT) {	/* check the time */
193*4882a593Smuzhiyun 					puts ("** Timeout waiting for BUSY to clear.\n");
194*4882a593Smuzhiyun 					(*fn->abort) (cookie);	/* abort the burn */
195*4882a593Smuzhiyun 					return FPGA_FAIL;
196*4882a593Smuzhiyun 				}
197*4882a593Smuzhiyun 			}
198*4882a593Smuzhiyun #endif
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
201*4882a593Smuzhiyun 			if (bytecount % (bsize / 40) == 0)
202*4882a593Smuzhiyun 				putc ('.');		/* let them know we are alive */
203*4882a593Smuzhiyun #endif
204*4882a593Smuzhiyun 		}
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 		CONFIG_FPGA_DELAY ();
207*4882a593Smuzhiyun 		(*fn->cs) (false, true, cookie);	/* Deassert the chip select */
208*4882a593Smuzhiyun 		(*fn->wr) (false, true, cookie);	/* Deassert the write pin */
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
211*4882a593Smuzhiyun 		putc ('\n');			/* terminate the dotted line */
212*4882a593Smuzhiyun #endif
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 		/* now check for done signal */
215*4882a593Smuzhiyun 		ts = get_timer (0);		/* get current time */
216*4882a593Smuzhiyun 		ret_val = FPGA_SUCCESS;
217*4882a593Smuzhiyun 		while ((*fn->done) (cookie) == FPGA_FAIL) {
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun 			CONFIG_FPGA_DELAY ();
220*4882a593Smuzhiyun 			(*fn->clk) (false, true, cookie);	/* Deassert the clock pin */
221*4882a593Smuzhiyun 			CONFIG_FPGA_DELAY ();
222*4882a593Smuzhiyun 			(*fn->clk) (true, true, cookie);	/* Assert the clock pin */
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 			if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT) {	/* check the time */
225*4882a593Smuzhiyun 				puts ("** Timeout waiting for DONE to clear.\n");
226*4882a593Smuzhiyun 				(*fn->abort) (cookie);	/* abort the burn */
227*4882a593Smuzhiyun 				ret_val = FPGA_FAIL;
228*4882a593Smuzhiyun 				break;
229*4882a593Smuzhiyun 			}
230*4882a593Smuzhiyun 		}
231*4882a593Smuzhiyun 
232*4882a593Smuzhiyun 		/*
233*4882a593Smuzhiyun 		 * Run the post configuration function if there is one.
234*4882a593Smuzhiyun 		 */
235*4882a593Smuzhiyun 		if (*fn->post)
236*4882a593Smuzhiyun 			(*fn->post) (cookie);
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
239*4882a593Smuzhiyun 		if (ret_val == FPGA_SUCCESS)
240*4882a593Smuzhiyun 			puts ("Done.\n");
241*4882a593Smuzhiyun 		else
242*4882a593Smuzhiyun 			puts ("Fail.\n");
243*4882a593Smuzhiyun #endif
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun 	} else {
246*4882a593Smuzhiyun 		printf ("%s: NULL Interface function table!\n", __FUNCTION__);
247*4882a593Smuzhiyun 	}
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun 	return ret_val;
250*4882a593Smuzhiyun }
251*4882a593Smuzhiyun 
spartan2_sp_dump(xilinx_desc * desc,const void * buf,size_t bsize)252*4882a593Smuzhiyun static int spartan2_sp_dump(xilinx_desc *desc, const void *buf, size_t bsize)
253*4882a593Smuzhiyun {
254*4882a593Smuzhiyun 	int ret_val = FPGA_FAIL;	/* assume the worst */
255*4882a593Smuzhiyun 	xilinx_spartan2_slave_parallel_fns *fn = desc->iface_fns;
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 	if (fn) {
258*4882a593Smuzhiyun 		unsigned char *data = (unsigned char *) buf;
259*4882a593Smuzhiyun 		size_t bytecount = 0;
260*4882a593Smuzhiyun 		int cookie = desc->cookie;	/* make a local copy */
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 		printf ("Starting Dump of FPGA Device %d...\n", cookie);
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun 		(*fn->cs) (true, true, cookie); /* Assert chip select, commit */
265*4882a593Smuzhiyun 		(*fn->clk) (true, true, cookie);	/* Assert the clock pin */
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun 		/* dump the data */
268*4882a593Smuzhiyun 		while (bytecount < bsize) {
269*4882a593Smuzhiyun 			/* XXX - do we check for an Ctrl-C press in here ??? */
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun 			(*fn->clk) (false, true, cookie);	/* Deassert the clock pin */
272*4882a593Smuzhiyun 			(*fn->clk) (true, true, cookie);	/* Assert the clock pin */
273*4882a593Smuzhiyun 			(*fn->rdata) (&(data[bytecount++]), cookie);	/* read the data */
274*4882a593Smuzhiyun #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
275*4882a593Smuzhiyun 			if (bytecount % (bsize / 40) == 0)
276*4882a593Smuzhiyun 				putc ('.');		/* let them know we are alive */
277*4882a593Smuzhiyun #endif
278*4882a593Smuzhiyun 		}
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 		(*fn->cs) (false, false, cookie);	/* Deassert the chip select */
281*4882a593Smuzhiyun 		(*fn->clk) (false, true, cookie);	/* Deassert the clock pin */
282*4882a593Smuzhiyun 		(*fn->clk) (true, true, cookie);	/* Assert the clock pin */
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
285*4882a593Smuzhiyun 		putc ('\n');			/* terminate the dotted line */
286*4882a593Smuzhiyun #endif
287*4882a593Smuzhiyun 		puts ("Done.\n");
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun 		/* XXX - checksum the data? */
290*4882a593Smuzhiyun 	} else {
291*4882a593Smuzhiyun 		printf ("%s: NULL Interface function table!\n", __FUNCTION__);
292*4882a593Smuzhiyun 	}
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 	return ret_val;
295*4882a593Smuzhiyun }
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun /* ------------------------------------------------------------------------- */
299*4882a593Smuzhiyun 
spartan2_ss_load(xilinx_desc * desc,const void * buf,size_t bsize)300*4882a593Smuzhiyun static int spartan2_ss_load(xilinx_desc *desc, const void *buf, size_t bsize)
301*4882a593Smuzhiyun {
302*4882a593Smuzhiyun 	int ret_val = FPGA_FAIL;	/* assume the worst */
303*4882a593Smuzhiyun 	xilinx_spartan2_slave_serial_fns *fn = desc->iface_fns;
304*4882a593Smuzhiyun 	int i;
305*4882a593Smuzhiyun 	unsigned char val;
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun 	PRINTF ("%s: start with interface functions @ 0x%p\n",
308*4882a593Smuzhiyun 			__FUNCTION__, fn);
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun 	if (fn) {
311*4882a593Smuzhiyun 		size_t bytecount = 0;
312*4882a593Smuzhiyun 		unsigned char *data = (unsigned char *) buf;
313*4882a593Smuzhiyun 		int cookie = desc->cookie;	/* make a local copy */
314*4882a593Smuzhiyun 		unsigned long ts;		/* timestamp */
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun 		PRINTF ("%s: Function Table:\n"
317*4882a593Smuzhiyun 				"ptr:\t0x%p\n"
318*4882a593Smuzhiyun 				"struct: 0x%p\n"
319*4882a593Smuzhiyun 				"pgm:\t0x%p\n"
320*4882a593Smuzhiyun 				"init:\t0x%p\n"
321*4882a593Smuzhiyun 				"clk:\t0x%p\n"
322*4882a593Smuzhiyun 				"wr:\t0x%p\n"
323*4882a593Smuzhiyun 				"done:\t0x%p\n\n",
324*4882a593Smuzhiyun 				__FUNCTION__, &fn, fn, fn->pgm, fn->init,
325*4882a593Smuzhiyun 				fn->clk, fn->wr, fn->done);
326*4882a593Smuzhiyun #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
327*4882a593Smuzhiyun 		printf ("Loading FPGA Device %d...\n", cookie);
328*4882a593Smuzhiyun #endif
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun 		/*
331*4882a593Smuzhiyun 		 * Run the pre configuration function if there is one.
332*4882a593Smuzhiyun 		 */
333*4882a593Smuzhiyun 		if (*fn->pre) {
334*4882a593Smuzhiyun 			(*fn->pre) (cookie);
335*4882a593Smuzhiyun 		}
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun 		/* Establish the initial state */
338*4882a593Smuzhiyun 		(*fn->pgm) (true, true, cookie);	/* Assert the program, commit */
339*4882a593Smuzhiyun 
340*4882a593Smuzhiyun 		/* Wait for INIT state (init low)                            */
341*4882a593Smuzhiyun 		ts = get_timer (0);		/* get current time */
342*4882a593Smuzhiyun 		do {
343*4882a593Smuzhiyun 			CONFIG_FPGA_DELAY ();
344*4882a593Smuzhiyun 			if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT) {	/* check the time */
345*4882a593Smuzhiyun 				puts ("** Timeout waiting for INIT to start.\n");
346*4882a593Smuzhiyun 				return FPGA_FAIL;
347*4882a593Smuzhiyun 			}
348*4882a593Smuzhiyun 		} while (!(*fn->init) (cookie));
349*4882a593Smuzhiyun 
350*4882a593Smuzhiyun 		/* Get ready for the burn */
351*4882a593Smuzhiyun 		CONFIG_FPGA_DELAY ();
352*4882a593Smuzhiyun 		(*fn->pgm) (false, true, cookie);	/* Deassert the program, commit */
353*4882a593Smuzhiyun 
354*4882a593Smuzhiyun 		ts = get_timer (0);		/* get current time */
355*4882a593Smuzhiyun 		/* Now wait for INIT to go high */
356*4882a593Smuzhiyun 		do {
357*4882a593Smuzhiyun 			CONFIG_FPGA_DELAY ();
358*4882a593Smuzhiyun 			if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT) {	/* check the time */
359*4882a593Smuzhiyun 				puts ("** Timeout waiting for INIT to clear.\n");
360*4882a593Smuzhiyun 				return FPGA_FAIL;
361*4882a593Smuzhiyun 			}
362*4882a593Smuzhiyun 		} while ((*fn->init) (cookie));
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun 		/* Load the data */
365*4882a593Smuzhiyun 		while (bytecount < bsize) {
366*4882a593Smuzhiyun 
367*4882a593Smuzhiyun 			/* Xilinx detects an error if INIT goes low (active)
368*4882a593Smuzhiyun 			   while DONE is low (inactive) */
369*4882a593Smuzhiyun 			if ((*fn->done) (cookie) == 0 && (*fn->init) (cookie)) {
370*4882a593Smuzhiyun 				puts ("** CRC error during FPGA load.\n");
371*4882a593Smuzhiyun 				return (FPGA_FAIL);
372*4882a593Smuzhiyun 			}
373*4882a593Smuzhiyun 			val = data [bytecount ++];
374*4882a593Smuzhiyun 			i = 8;
375*4882a593Smuzhiyun 			do {
376*4882a593Smuzhiyun 				/* Deassert the clock */
377*4882a593Smuzhiyun 				(*fn->clk) (false, true, cookie);
378*4882a593Smuzhiyun 				CONFIG_FPGA_DELAY ();
379*4882a593Smuzhiyun 				/* Write data */
380*4882a593Smuzhiyun 				(*fn->wr) ((val & 0x80), true, cookie);
381*4882a593Smuzhiyun 				CONFIG_FPGA_DELAY ();
382*4882a593Smuzhiyun 				/* Assert the clock */
383*4882a593Smuzhiyun 				(*fn->clk) (true, true, cookie);
384*4882a593Smuzhiyun 				CONFIG_FPGA_DELAY ();
385*4882a593Smuzhiyun 				val <<= 1;
386*4882a593Smuzhiyun 				i --;
387*4882a593Smuzhiyun 			} while (i > 0);
388*4882a593Smuzhiyun 
389*4882a593Smuzhiyun #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
390*4882a593Smuzhiyun 			if (bytecount % (bsize / 40) == 0)
391*4882a593Smuzhiyun 				putc ('.');		/* let them know we are alive */
392*4882a593Smuzhiyun #endif
393*4882a593Smuzhiyun 		}
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun 		CONFIG_FPGA_DELAY ();
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
398*4882a593Smuzhiyun 		putc ('\n');			/* terminate the dotted line */
399*4882a593Smuzhiyun #endif
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun 		/* now check for done signal */
402*4882a593Smuzhiyun 		ts = get_timer (0);		/* get current time */
403*4882a593Smuzhiyun 		ret_val = FPGA_SUCCESS;
404*4882a593Smuzhiyun 		(*fn->wr) (true, true, cookie);
405*4882a593Smuzhiyun 
406*4882a593Smuzhiyun 		while (! (*fn->done) (cookie)) {
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun 			CONFIG_FPGA_DELAY ();
409*4882a593Smuzhiyun 			(*fn->clk) (false, true, cookie);	/* Deassert the clock pin */
410*4882a593Smuzhiyun 			CONFIG_FPGA_DELAY ();
411*4882a593Smuzhiyun 			(*fn->clk) (true, true, cookie);	/* Assert the clock pin */
412*4882a593Smuzhiyun 
413*4882a593Smuzhiyun 			putc ('*');
414*4882a593Smuzhiyun 
415*4882a593Smuzhiyun 			if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT) {	/* check the time */
416*4882a593Smuzhiyun 				puts ("** Timeout waiting for DONE to clear.\n");
417*4882a593Smuzhiyun 				ret_val = FPGA_FAIL;
418*4882a593Smuzhiyun 				break;
419*4882a593Smuzhiyun 			}
420*4882a593Smuzhiyun 		}
421*4882a593Smuzhiyun 		putc ('\n');			/* terminate the dotted line */
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun 		/*
424*4882a593Smuzhiyun 		 * Run the post configuration function if there is one.
425*4882a593Smuzhiyun 		 */
426*4882a593Smuzhiyun 		if (*fn->post)
427*4882a593Smuzhiyun 			(*fn->post) (cookie);
428*4882a593Smuzhiyun 
429*4882a593Smuzhiyun #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
430*4882a593Smuzhiyun 		if (ret_val == FPGA_SUCCESS)
431*4882a593Smuzhiyun 			puts ("Done.\n");
432*4882a593Smuzhiyun 		else
433*4882a593Smuzhiyun 			puts ("Fail.\n");
434*4882a593Smuzhiyun #endif
435*4882a593Smuzhiyun 
436*4882a593Smuzhiyun 	} else {
437*4882a593Smuzhiyun 		printf ("%s: NULL Interface function table!\n", __FUNCTION__);
438*4882a593Smuzhiyun 	}
439*4882a593Smuzhiyun 
440*4882a593Smuzhiyun 	return ret_val;
441*4882a593Smuzhiyun }
442*4882a593Smuzhiyun 
spartan2_ss_dump(xilinx_desc * desc,const void * buf,size_t bsize)443*4882a593Smuzhiyun static int spartan2_ss_dump(xilinx_desc *desc, const void *buf, size_t bsize)
444*4882a593Smuzhiyun {
445*4882a593Smuzhiyun 	/* Readback is only available through the Slave Parallel and         */
446*4882a593Smuzhiyun 	/* boundary-scan interfaces.                                         */
447*4882a593Smuzhiyun 	printf ("%s: Slave Serial Dumping is unavailable\n",
448*4882a593Smuzhiyun 			__FUNCTION__);
449*4882a593Smuzhiyun 	return FPGA_FAIL;
450*4882a593Smuzhiyun }
451*4882a593Smuzhiyun 
452*4882a593Smuzhiyun struct xilinx_fpga_op spartan2_op = {
453*4882a593Smuzhiyun 	.load = spartan2_load,
454*4882a593Smuzhiyun 	.dump = spartan2_dump,
455*4882a593Smuzhiyun 	.info = spartan2_info,
456*4882a593Smuzhiyun };
457