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