spartan3.c 13 KB

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