spartan3.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*
  2. * (C) Copyright 2002
  3. * Rich Ireland, Enterasys Networks, rireland@enterasys.com.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. *
  23. */
  24. /*
  25. * Configuration support for Xilinx Spartan3 devices. Based
  26. * on spartan2.c (Rich Ireland, rireland@enterasys.com).
  27. */
  28. #include <common.h> /* core U-Boot definitions */
  29. #include <spartan3.h> /* Spartan-II device family */
  30. /* Define FPGA_DEBUG to get debug printf's */
  31. #ifdef FPGA_DEBUG
  32. #define PRINTF(fmt,args...) printf (fmt ,##args)
  33. #else
  34. #define PRINTF(fmt,args...)
  35. #endif
  36. #undef CONFIG_SYS_FPGA_CHECK_BUSY
  37. #undef CONFIG_SYS_FPGA_PROG_FEEDBACK
  38. /* Note: The assumption is that we cannot possibly run fast enough to
  39. * overrun the device (the Slave Parallel mode can free run at 50MHz).
  40. * If there is a need to operate slower, define CONFIG_FPGA_DELAY in
  41. * the board config file to slow things down.
  42. */
  43. #ifndef CONFIG_FPGA_DELAY
  44. #define CONFIG_FPGA_DELAY()
  45. #endif
  46. #ifndef CONFIG_SYS_FPGA_WAIT
  47. #define CONFIG_SYS_FPGA_WAIT CONFIG_SYS_HZ/100 /* 10 ms */
  48. #endif
  49. static int Spartan3_sp_load( Xilinx_desc *desc, void *buf, size_t bsize );
  50. static int Spartan3_sp_dump( Xilinx_desc *desc, void *buf, size_t bsize );
  51. /* static int Spartan3_sp_info( Xilinx_desc *desc ); */
  52. static int Spartan3_ss_load( Xilinx_desc *desc, void *buf, size_t bsize );
  53. static int Spartan3_ss_dump( Xilinx_desc *desc, void *buf, size_t bsize );
  54. /* static int Spartan3_ss_info( Xilinx_desc *desc ); */
  55. /* ------------------------------------------------------------------------- */
  56. /* Spartan-II Generic Implementation */
  57. int Spartan3_load (Xilinx_desc * desc, void *buf, size_t bsize)
  58. {
  59. int ret_val = FPGA_FAIL;
  60. switch (desc->iface) {
  61. case slave_serial:
  62. PRINTF ("%s: Launching Slave Serial Load\n", __FUNCTION__);
  63. ret_val = Spartan3_ss_load (desc, buf, bsize);
  64. break;
  65. case slave_parallel:
  66. PRINTF ("%s: Launching Slave Parallel Load\n", __FUNCTION__);
  67. ret_val = Spartan3_sp_load (desc, buf, bsize);
  68. break;
  69. default:
  70. printf ("%s: Unsupported interface type, %d\n",
  71. __FUNCTION__, desc->iface);
  72. }
  73. return ret_val;
  74. }
  75. int Spartan3_dump (Xilinx_desc * desc, void *buf, size_t bsize)
  76. {
  77. int ret_val = FPGA_FAIL;
  78. switch (desc->iface) {
  79. case slave_serial:
  80. PRINTF ("%s: Launching Slave Serial Dump\n", __FUNCTION__);
  81. ret_val = Spartan3_ss_dump (desc, buf, bsize);
  82. break;
  83. case slave_parallel:
  84. PRINTF ("%s: Launching Slave Parallel Dump\n", __FUNCTION__);
  85. ret_val = Spartan3_sp_dump (desc, buf, bsize);
  86. break;
  87. default:
  88. printf ("%s: Unsupported interface type, %d\n",
  89. __FUNCTION__, desc->iface);
  90. }
  91. return ret_val;
  92. }
  93. int Spartan3_info( Xilinx_desc *desc )
  94. {
  95. return FPGA_SUCCESS;
  96. }
  97. /* ------------------------------------------------------------------------- */
  98. /* Spartan-II Slave Parallel Generic Implementation */
  99. static int Spartan3_sp_load (Xilinx_desc * desc, void *buf, size_t bsize)
  100. {
  101. int ret_val = FPGA_FAIL; /* assume the worst */
  102. Xilinx_Spartan3_Slave_Parallel_fns *fn = desc->iface_fns;
  103. PRINTF ("%s: start with interface functions @ 0x%p\n",
  104. __FUNCTION__, fn);
  105. if (fn) {
  106. size_t bytecount = 0;
  107. unsigned char *data = (unsigned char *) buf;
  108. int cookie = desc->cookie; /* make a local copy */
  109. unsigned long ts; /* timestamp */
  110. PRINTF ("%s: Function Table:\n"
  111. "ptr:\t0x%p\n"
  112. "struct: 0x%p\n"
  113. "pre: 0x%p\n"
  114. "pgm:\t0x%p\n"
  115. "init:\t0x%p\n"
  116. "err:\t0x%p\n"
  117. "clk:\t0x%p\n"
  118. "cs:\t0x%p\n"
  119. "wr:\t0x%p\n"
  120. "read data:\t0x%p\n"
  121. "write data:\t0x%p\n"
  122. "busy:\t0x%p\n"
  123. "abort:\t0x%p\n",
  124. "post:\t0x%p\n\n",
  125. __FUNCTION__, &fn, fn, fn->pre, fn->pgm, fn->init, fn->err,
  126. fn->clk, fn->cs, fn->wr, fn->rdata, fn->wdata, fn->busy,
  127. fn->abort, fn->post);
  128. /*
  129. * This code is designed to emulate the "Express Style"
  130. * Continuous Data Loading in Slave Parallel Mode for
  131. * the Spartan-II Family.
  132. */
  133. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  134. printf ("Loading FPGA Device %d...\n", cookie);
  135. #endif
  136. /*
  137. * Run the pre configuration function if there is one.
  138. */
  139. if (*fn->pre) {
  140. (*fn->pre) (cookie);
  141. }
  142. /* Establish the initial state */
  143. (*fn->pgm) (TRUE, TRUE, cookie); /* Assert the program, commit */
  144. /* Get ready for the burn */
  145. CONFIG_FPGA_DELAY ();
  146. (*fn->pgm) (FALSE, TRUE, cookie); /* Deassert the program, commit */
  147. ts = get_timer (0); /* get current time */
  148. /* Now wait for INIT and BUSY to go high */
  149. do {
  150. CONFIG_FPGA_DELAY ();
  151. if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT) { /* check the time */
  152. puts ("** Timeout waiting for INIT to clear.\n");
  153. (*fn->abort) (cookie); /* abort the burn */
  154. return FPGA_FAIL;
  155. }
  156. } while ((*fn->init) (cookie) && (*fn->busy) (cookie));
  157. (*fn->wr) (TRUE, TRUE, cookie); /* Assert write, commit */
  158. (*fn->cs) (TRUE, TRUE, cookie); /* Assert chip select, commit */
  159. (*fn->clk) (TRUE, TRUE, cookie); /* Assert the clock pin */
  160. /* Load the data */
  161. while (bytecount < bsize) {
  162. /* XXX - do we check for an Ctrl-C press in here ??? */
  163. /* XXX - Check the error bit? */
  164. (*fn->wdata) (data[bytecount++], TRUE, cookie); /* write the data */
  165. CONFIG_FPGA_DELAY ();
  166. (*fn->clk) (FALSE, TRUE, cookie); /* Deassert the clock pin */
  167. CONFIG_FPGA_DELAY ();
  168. (*fn->clk) (TRUE, TRUE, cookie); /* Assert the clock pin */
  169. #ifdef CONFIG_SYS_FPGA_CHECK_BUSY
  170. ts = get_timer (0); /* get current time */
  171. while ((*fn->busy) (cookie)) {
  172. /* XXX - we should have a check in here somewhere to
  173. * make sure we aren't busy forever... */
  174. CONFIG_FPGA_DELAY ();
  175. (*fn->clk) (FALSE, TRUE, cookie); /* Deassert the clock pin */
  176. CONFIG_FPGA_DELAY ();
  177. (*fn->clk) (TRUE, TRUE, cookie); /* Assert the clock pin */
  178. if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT) { /* check the time */
  179. puts ("** Timeout waiting for BUSY to clear.\n");
  180. (*fn->abort) (cookie); /* abort the burn */
  181. return FPGA_FAIL;
  182. }
  183. }
  184. #endif
  185. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  186. if (bytecount % (bsize / 40) == 0)
  187. putc ('.'); /* let them know we are alive */
  188. #endif
  189. }
  190. CONFIG_FPGA_DELAY ();
  191. (*fn->cs) (FALSE, TRUE, cookie); /* Deassert the chip select */
  192. (*fn->wr) (FALSE, TRUE, cookie); /* Deassert the write pin */
  193. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  194. putc ('\n'); /* terminate the dotted line */
  195. #endif
  196. /* now check for done signal */
  197. ts = get_timer (0); /* get current time */
  198. ret_val = FPGA_SUCCESS;
  199. while ((*fn->done) (cookie) == FPGA_FAIL) {
  200. /* XXX - we should have a check in here somewhere to
  201. * make sure we aren't busy forever... */
  202. CONFIG_FPGA_DELAY ();
  203. (*fn->clk) (FALSE, TRUE, cookie); /* Deassert the clock pin */
  204. CONFIG_FPGA_DELAY ();
  205. (*fn->clk) (TRUE, TRUE, cookie); /* Assert the clock pin */
  206. if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT) { /* check the time */
  207. puts ("** Timeout waiting for DONE to clear.\n");
  208. (*fn->abort) (cookie); /* abort the burn */
  209. ret_val = FPGA_FAIL;
  210. break;
  211. }
  212. }
  213. /*
  214. * Run the post configuration function if there is one.
  215. */
  216. if (*fn->post)
  217. (*fn->post) (cookie);
  218. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  219. if (ret_val == FPGA_SUCCESS)
  220. puts ("Done.\n");
  221. else
  222. puts ("Fail.\n");
  223. #endif
  224. } else {
  225. printf ("%s: NULL Interface function table!\n", __FUNCTION__);
  226. }
  227. return ret_val;
  228. }
  229. static int Spartan3_sp_dump (Xilinx_desc * desc, void *buf, size_t bsize)
  230. {
  231. int ret_val = FPGA_FAIL; /* assume the worst */
  232. Xilinx_Spartan3_Slave_Parallel_fns *fn = desc->iface_fns;
  233. if (fn) {
  234. unsigned char *data = (unsigned char *) buf;
  235. size_t bytecount = 0;
  236. int cookie = desc->cookie; /* make a local copy */
  237. printf ("Starting Dump of FPGA Device %d...\n", cookie);
  238. (*fn->cs) (TRUE, TRUE, cookie); /* Assert chip select, commit */
  239. (*fn->clk) (TRUE, TRUE, cookie); /* Assert the clock pin */
  240. /* dump the data */
  241. while (bytecount < bsize) {
  242. /* XXX - do we check for an Ctrl-C press in here ??? */
  243. (*fn->clk) (FALSE, TRUE, cookie); /* Deassert the clock pin */
  244. (*fn->clk) (TRUE, TRUE, cookie); /* Assert the clock pin */
  245. (*fn->rdata) (&(data[bytecount++]), cookie); /* read the data */
  246. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  247. if (bytecount % (bsize / 40) == 0)
  248. putc ('.'); /* let them know we are alive */
  249. #endif
  250. }
  251. (*fn->cs) (FALSE, FALSE, cookie); /* Deassert the chip select */
  252. (*fn->clk) (FALSE, TRUE, cookie); /* Deassert the clock pin */
  253. (*fn->clk) (TRUE, TRUE, cookie); /* Assert the clock pin */
  254. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  255. putc ('\n'); /* terminate the dotted line */
  256. #endif
  257. puts ("Done.\n");
  258. /* XXX - checksum the data? */
  259. } else {
  260. printf ("%s: NULL Interface function table!\n", __FUNCTION__);
  261. }
  262. return ret_val;
  263. }
  264. /* ------------------------------------------------------------------------- */
  265. static int Spartan3_ss_load (Xilinx_desc * desc, void *buf, size_t bsize)
  266. {
  267. int ret_val = FPGA_FAIL; /* assume the worst */
  268. Xilinx_Spartan3_Slave_Serial_fns *fn = desc->iface_fns;
  269. int i;
  270. unsigned char val;
  271. PRINTF ("%s: start with interface functions @ 0x%p\n",
  272. __FUNCTION__, fn);
  273. if (fn) {
  274. size_t bytecount = 0;
  275. unsigned char *data = (unsigned char *) buf;
  276. int cookie = desc->cookie; /* make a local copy */
  277. unsigned long ts; /* timestamp */
  278. PRINTF ("%s: Function Table:\n"
  279. "ptr:\t0x%p\n"
  280. "struct: 0x%p\n"
  281. "pgm:\t0x%p\n"
  282. "init:\t0x%p\n"
  283. "clk:\t0x%p\n"
  284. "wr:\t0x%p\n"
  285. "done:\t0x%p\n\n",
  286. __FUNCTION__, &fn, fn, fn->pgm, fn->init,
  287. fn->clk, fn->wr, fn->done);
  288. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  289. printf ("Loading FPGA Device %d...\n", cookie);
  290. #endif
  291. /*
  292. * Run the pre configuration function if there is one.
  293. */
  294. if (*fn->pre) {
  295. (*fn->pre) (cookie);
  296. }
  297. /* Establish the initial state */
  298. (*fn->pgm) (TRUE, TRUE, cookie); /* Assert the program, commit */
  299. /* Wait for INIT state (init low) */
  300. ts = get_timer (0); /* get current time */
  301. do {
  302. CONFIG_FPGA_DELAY ();
  303. if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT) { /* check the time */
  304. puts ("** Timeout waiting for INIT to start.\n");
  305. return FPGA_FAIL;
  306. }
  307. } while (!(*fn->init) (cookie));
  308. /* Get ready for the burn */
  309. CONFIG_FPGA_DELAY ();
  310. (*fn->pgm) (FALSE, TRUE, cookie); /* Deassert the program, commit */
  311. ts = get_timer (0); /* get current time */
  312. /* Now wait for INIT to go high */
  313. do {
  314. CONFIG_FPGA_DELAY ();
  315. if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT) { /* check the time */
  316. puts ("** Timeout waiting for INIT to clear.\n");
  317. return FPGA_FAIL;
  318. }
  319. } while ((*fn->init) (cookie));
  320. /* Load the data */
  321. if(*fn->bwr)
  322. (*fn->bwr) (data, bsize, TRUE, cookie);
  323. else {
  324. while (bytecount < bsize) {
  325. /* Xilinx detects an error if INIT goes low (active)
  326. while DONE is low (inactive) */
  327. if ((*fn->done) (cookie) == 0 && (*fn->init) (cookie)) {
  328. puts ("** CRC error during FPGA load.\n");
  329. return (FPGA_FAIL);
  330. }
  331. val = data [bytecount ++];
  332. i = 8;
  333. do {
  334. /* Deassert the clock */
  335. (*fn->clk) (FALSE, TRUE, cookie);
  336. CONFIG_FPGA_DELAY ();
  337. /* Write data */
  338. (*fn->wr) ((val & 0x80), TRUE, cookie);
  339. CONFIG_FPGA_DELAY ();
  340. /* Assert the clock */
  341. (*fn->clk) (TRUE, TRUE, cookie);
  342. CONFIG_FPGA_DELAY ();
  343. val <<= 1;
  344. i --;
  345. } while (i > 0);
  346. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  347. if (bytecount % (bsize / 40) == 0)
  348. putc ('.'); /* let them know we are alive */
  349. #endif
  350. }
  351. }
  352. CONFIG_FPGA_DELAY ();
  353. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  354. putc ('\n'); /* terminate the dotted line */
  355. #endif
  356. /* now check for done signal */
  357. ts = get_timer (0); /* get current time */
  358. ret_val = FPGA_SUCCESS;
  359. (*fn->wr) (TRUE, TRUE, cookie);
  360. while (! (*fn->done) (cookie)) {
  361. /* XXX - we should have a check in here somewhere to
  362. * make sure we aren't busy forever... */
  363. CONFIG_FPGA_DELAY ();
  364. (*fn->clk) (FALSE, TRUE, cookie); /* Deassert the clock pin */
  365. CONFIG_FPGA_DELAY ();
  366. (*fn->clk) (TRUE, TRUE, cookie); /* Assert the clock pin */
  367. putc ('*');
  368. if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT) { /* check the time */
  369. puts ("** Timeout waiting for DONE to clear.\n");
  370. ret_val = FPGA_FAIL;
  371. break;
  372. }
  373. }
  374. putc ('\n'); /* terminate the dotted line */
  375. /*
  376. * Run the post configuration function if there is one.
  377. */
  378. if (*fn->post)
  379. (*fn->post) (cookie);
  380. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  381. if (ret_val == FPGA_SUCCESS)
  382. puts ("Done.\n");
  383. else
  384. puts ("Fail.\n");
  385. #endif
  386. } else {
  387. printf ("%s: NULL Interface function table!\n", __FUNCTION__);
  388. }
  389. return ret_val;
  390. }
  391. static int Spartan3_ss_dump (Xilinx_desc * desc, void *buf, size_t bsize)
  392. {
  393. /* Readback is only available through the Slave Parallel and */
  394. /* boundary-scan interfaces. */
  395. printf ("%s: Slave Serial Dumping is unavailable\n",
  396. __FUNCTION__);
  397. return FPGA_FAIL;
  398. }