spartan2.c 12 KB

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