virtex2.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. * (C) Copyright 2002
  3. * Rich Ireland, Enterasys Networks, rireland@enterasys.com.
  4. * Keith Outwater, keith_outwater@mvis.com
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. /*
  9. * Configuration support for Xilinx Virtex2 devices. Based
  10. * on spartan2.c (Rich Ireland, rireland@enterasys.com).
  11. */
  12. #include <common.h>
  13. #include <console.h>
  14. #include <virtex2.h>
  15. #if 0
  16. #define FPGA_DEBUG
  17. #endif
  18. #ifdef FPGA_DEBUG
  19. #define PRINTF(fmt,args...) printf (fmt ,##args)
  20. #else
  21. #define PRINTF(fmt,args...)
  22. #endif
  23. /*
  24. * If the SelectMap interface can be overrun by the processor, define
  25. * CONFIG_SYS_FPGA_CHECK_BUSY and/or CONFIG_FPGA_DELAY in the board configuration
  26. * file and add board-specific support for checking BUSY status. By default,
  27. * assume that the SelectMap interface cannot be overrun.
  28. */
  29. #ifndef CONFIG_SYS_FPGA_CHECK_BUSY
  30. #undef CONFIG_SYS_FPGA_CHECK_BUSY
  31. #endif
  32. #ifndef CONFIG_FPGA_DELAY
  33. #define CONFIG_FPGA_DELAY()
  34. #endif
  35. #ifndef CONFIG_SYS_FPGA_PROG_FEEDBACK
  36. #define CONFIG_SYS_FPGA_PROG_FEEDBACK
  37. #endif
  38. /*
  39. * Don't allow config cycle to be interrupted
  40. */
  41. #ifndef CONFIG_SYS_FPGA_CHECK_CTRLC
  42. #undef CONFIG_SYS_FPGA_CHECK_CTRLC
  43. #endif
  44. /*
  45. * Check for errors during configuration by default
  46. */
  47. #ifndef CONFIG_SYS_FPGA_CHECK_ERROR
  48. #define CONFIG_SYS_FPGA_CHECK_ERROR
  49. #endif
  50. /*
  51. * The default timeout in mS for INIT_B to deassert after PROG_B has
  52. * been deasserted. Per the latest Virtex II Handbook (page 347), the
  53. * max time from PORG_B deassertion to INIT_B deassertion is 4uS per
  54. * data frame for the XC2V8000. The XC2V8000 has 2860 data frames
  55. * which yields 11.44 mS. So let's make it bigger in order to handle
  56. * an XC2V1000, if anyone can ever get ahold of one.
  57. */
  58. #ifndef CONFIG_SYS_FPGA_WAIT_INIT
  59. #define CONFIG_SYS_FPGA_WAIT_INIT CONFIG_SYS_HZ/2 /* 500 ms */
  60. #endif
  61. /*
  62. * The default timeout for waiting for BUSY to deassert during configuration.
  63. * This is normally not necessary since for most reasonable configuration
  64. * clock frequencies (i.e. 66 MHz or less), BUSY monitoring is unnecessary.
  65. */
  66. #ifndef CONFIG_SYS_FPGA_WAIT_BUSY
  67. #define CONFIG_SYS_FPGA_WAIT_BUSY CONFIG_SYS_HZ/200 /* 5 ms*/
  68. #endif
  69. /* Default timeout for waiting for FPGA to enter operational mode after
  70. * configuration data has been written.
  71. */
  72. #ifndef CONFIG_SYS_FPGA_WAIT_CONFIG
  73. #define CONFIG_SYS_FPGA_WAIT_CONFIG CONFIG_SYS_HZ/5 /* 200 ms */
  74. #endif
  75. static int virtex2_ssm_load(xilinx_desc *desc, const void *buf, size_t bsize);
  76. static int virtex2_ssm_dump(xilinx_desc *desc, const void *buf, size_t bsize);
  77. static int virtex2_ss_load(xilinx_desc *desc, const void *buf, size_t bsize);
  78. static int virtex2_ss_dump(xilinx_desc *desc, const void *buf, size_t bsize);
  79. static int virtex2_load(xilinx_desc *desc, const void *buf, size_t bsize,
  80. bitstream_type bstype)
  81. {
  82. int ret_val = FPGA_FAIL;
  83. switch (desc->iface) {
  84. case slave_serial:
  85. PRINTF ("%s: Launching Slave Serial Load\n", __FUNCTION__);
  86. ret_val = virtex2_ss_load(desc, buf, bsize);
  87. break;
  88. case slave_selectmap:
  89. PRINTF ("%s: Launching Slave Parallel Load\n", __FUNCTION__);
  90. ret_val = virtex2_ssm_load(desc, buf, bsize);
  91. break;
  92. default:
  93. printf ("%s: Unsupported interface type, %d\n",
  94. __FUNCTION__, desc->iface);
  95. }
  96. return ret_val;
  97. }
  98. static int virtex2_dump(xilinx_desc *desc, const void *buf, size_t bsize)
  99. {
  100. int ret_val = FPGA_FAIL;
  101. switch (desc->iface) {
  102. case slave_serial:
  103. PRINTF ("%s: Launching Slave Serial Dump\n", __FUNCTION__);
  104. ret_val = virtex2_ss_dump(desc, buf, bsize);
  105. break;
  106. case slave_parallel:
  107. PRINTF ("%s: Launching Slave Parallel Dump\n", __FUNCTION__);
  108. ret_val = virtex2_ssm_dump(desc, buf, bsize);
  109. break;
  110. default:
  111. printf ("%s: Unsupported interface type, %d\n",
  112. __FUNCTION__, desc->iface);
  113. }
  114. return ret_val;
  115. }
  116. static int virtex2_info(xilinx_desc *desc)
  117. {
  118. return FPGA_SUCCESS;
  119. }
  120. /*
  121. * Virtex-II Slave SelectMap configuration loader. Configuration via
  122. * SelectMap is as follows:
  123. * 1. Set the FPGA's PROG_B line low.
  124. * 2. Set the FPGA's PROG_B line high. Wait for INIT_B to go high.
  125. * 3. Write data to the SelectMap port. If INIT_B goes low at any time
  126. * this process, a configuration error (most likely CRC failure) has
  127. * ocurred. At this point a status word may be read from the
  128. * SelectMap interface to determine the source of the problem (You
  129. * could, for instance, put this in your 'abort' function handler).
  130. * 4. After all data has been written, test the state of the FPGA
  131. * INIT_B and DONE lines. If both are high, configuration has
  132. * succeeded. Congratulations!
  133. */
  134. static int virtex2_ssm_load(xilinx_desc *desc, const void *buf, size_t bsize)
  135. {
  136. int ret_val = FPGA_FAIL;
  137. xilinx_virtex2_slave_selectmap_fns *fn = desc->iface_fns;
  138. PRINTF ("%s:%d: Start with interface functions @ 0x%p\n",
  139. __FUNCTION__, __LINE__, fn);
  140. if (fn) {
  141. size_t bytecount = 0;
  142. unsigned char *data = (unsigned char *) buf;
  143. int cookie = desc->cookie;
  144. unsigned long ts;
  145. /* Gotta split this one up (so the stack won't blow??) */
  146. PRINTF ("%s:%d: Function Table:\n"
  147. " base 0x%p\n"
  148. " struct 0x%p\n"
  149. " pre 0x%p\n"
  150. " prog 0x%p\n"
  151. " init 0x%p\n"
  152. " error 0x%p\n",
  153. __FUNCTION__, __LINE__,
  154. &fn, fn, fn->pre, fn->pgm, fn->init, fn->err);
  155. PRINTF (" clock 0x%p\n"
  156. " cs 0x%p\n"
  157. " write 0x%p\n"
  158. " rdata 0x%p\n"
  159. " wdata 0x%p\n"
  160. " busy 0x%p\n"
  161. " abort 0x%p\n"
  162. " post 0x%p\n\n",
  163. fn->clk, fn->cs, fn->wr, fn->rdata, fn->wdata,
  164. fn->busy, fn->abort, fn->post);
  165. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  166. printf ("Initializing FPGA Device %d...\n", cookie);
  167. #endif
  168. /*
  169. * Run the pre configuration function if there is one.
  170. */
  171. if (*fn->pre) {
  172. (*fn->pre) (cookie);
  173. }
  174. /*
  175. * Assert the program line. The minimum pulse width for
  176. * Virtex II devices is 300 nS (Tprogram parameter in datasheet).
  177. * There is no maximum value for the pulse width. Check to make
  178. * sure that INIT_B goes low after assertion of PROG_B
  179. */
  180. (*fn->pgm) (true, true, cookie);
  181. udelay (10);
  182. ts = get_timer (0);
  183. do {
  184. if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT_INIT) {
  185. printf ("%s:%d: ** Timeout after %d ticks waiting for INIT"
  186. " to assert.\n", __FUNCTION__, __LINE__,
  187. CONFIG_SYS_FPGA_WAIT_INIT);
  188. (*fn->abort) (cookie);
  189. return FPGA_FAIL;
  190. }
  191. } while (!(*fn->init) (cookie));
  192. (*fn->pgm) (false, true, cookie);
  193. CONFIG_FPGA_DELAY ();
  194. (*fn->clk) (true, true, cookie);
  195. /*
  196. * Start a timer and wait for INIT_B to go high
  197. */
  198. ts = get_timer (0);
  199. do {
  200. CONFIG_FPGA_DELAY ();
  201. if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT_INIT) {
  202. printf ("%s:%d: ** Timeout after %d ticks waiting for INIT"
  203. " to deassert.\n", __FUNCTION__, __LINE__,
  204. CONFIG_SYS_FPGA_WAIT_INIT);
  205. (*fn->abort) (cookie);
  206. return FPGA_FAIL;
  207. }
  208. } while ((*fn->init) (cookie) && (*fn->busy) (cookie));
  209. (*fn->wr) (true, true, cookie);
  210. (*fn->cs) (true, true, cookie);
  211. udelay (10000);
  212. /*
  213. * Load the data byte by byte
  214. */
  215. while (bytecount < bsize) {
  216. #ifdef CONFIG_SYS_FPGA_CHECK_CTRLC
  217. if (ctrlc ()) {
  218. (*fn->abort) (cookie);
  219. return FPGA_FAIL;
  220. }
  221. #endif
  222. if ((*fn->done) (cookie) == FPGA_SUCCESS) {
  223. PRINTF ("%s:%d:done went active early, bytecount = %d\n",
  224. __FUNCTION__, __LINE__, bytecount);
  225. break;
  226. }
  227. #ifdef CONFIG_SYS_FPGA_CHECK_ERROR
  228. if ((*fn->init) (cookie)) {
  229. printf ("\n%s:%d: ** Error: INIT asserted during"
  230. " configuration\n", __FUNCTION__, __LINE__);
  231. printf ("%d = buffer offset, %d = buffer size\n",
  232. bytecount, bsize);
  233. (*fn->abort) (cookie);
  234. return FPGA_FAIL;
  235. }
  236. #endif
  237. (*fn->wdata) (data[bytecount++], true, cookie);
  238. CONFIG_FPGA_DELAY ();
  239. /*
  240. * Cycle the clock pin
  241. */
  242. (*fn->clk) (false, true, cookie);
  243. CONFIG_FPGA_DELAY ();
  244. (*fn->clk) (true, true, cookie);
  245. #ifdef CONFIG_SYS_FPGA_CHECK_BUSY
  246. ts = get_timer (0);
  247. while ((*fn->busy) (cookie)) {
  248. if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT_BUSY) {
  249. printf ("%s:%d: ** Timeout after %d ticks waiting for"
  250. " BUSY to deassert\n",
  251. __FUNCTION__, __LINE__, CONFIG_SYS_FPGA_WAIT_BUSY);
  252. (*fn->abort) (cookie);
  253. return FPGA_FAIL;
  254. }
  255. }
  256. #endif
  257. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  258. if (bytecount % (bsize / 40) == 0)
  259. putc ('.');
  260. #endif
  261. }
  262. /*
  263. * Finished writing the data; deassert FPGA CS_B and WRITE_B signals.
  264. */
  265. CONFIG_FPGA_DELAY ();
  266. (*fn->cs) (false, true, cookie);
  267. (*fn->wr) (false, true, cookie);
  268. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  269. putc ('\n');
  270. #endif
  271. /*
  272. * Check for successful configuration. FPGA INIT_B and DONE should
  273. * both be high upon successful configuration.
  274. */
  275. ts = get_timer (0);
  276. ret_val = FPGA_SUCCESS;
  277. while (((*fn->done) (cookie) == FPGA_FAIL) || (*fn->init) (cookie)) {
  278. if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT_CONFIG) {
  279. printf ("%s:%d: ** Timeout after %d ticks waiting for DONE to"
  280. "assert and INIT to deassert\n",
  281. __FUNCTION__, __LINE__, CONFIG_SYS_FPGA_WAIT_CONFIG);
  282. (*fn->abort) (cookie);
  283. ret_val = FPGA_FAIL;
  284. break;
  285. }
  286. }
  287. if (ret_val == FPGA_SUCCESS) {
  288. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  289. printf ("Initialization of FPGA device %d complete\n", cookie);
  290. #endif
  291. /*
  292. * Run the post configuration function if there is one.
  293. */
  294. if (*fn->post) {
  295. (*fn->post) (cookie);
  296. }
  297. } else {
  298. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  299. printf ("** Initialization of FPGA device %d FAILED\n",
  300. cookie);
  301. #endif
  302. }
  303. } else {
  304. printf ("%s:%d: NULL Interface function table!\n",
  305. __FUNCTION__, __LINE__);
  306. }
  307. return ret_val;
  308. }
  309. /*
  310. * Read the FPGA configuration data
  311. */
  312. static int virtex2_ssm_dump(xilinx_desc *desc, const void *buf, size_t bsize)
  313. {
  314. int ret_val = FPGA_FAIL;
  315. xilinx_virtex2_slave_selectmap_fns *fn = desc->iface_fns;
  316. if (fn) {
  317. unsigned char *data = (unsigned char *) buf;
  318. size_t bytecount = 0;
  319. int cookie = desc->cookie;
  320. printf ("Starting Dump of FPGA Device %d...\n", cookie);
  321. (*fn->cs) (true, true, cookie);
  322. (*fn->clk) (true, true, cookie);
  323. while (bytecount < bsize) {
  324. #ifdef CONFIG_SYS_FPGA_CHECK_CTRLC
  325. if (ctrlc ()) {
  326. (*fn->abort) (cookie);
  327. return FPGA_FAIL;
  328. }
  329. #endif
  330. /*
  331. * Cycle the clock and read the data
  332. */
  333. (*fn->clk) (false, true, cookie);
  334. (*fn->clk) (true, true, cookie);
  335. (*fn->rdata) (&(data[bytecount++]), cookie);
  336. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  337. if (bytecount % (bsize / 40) == 0)
  338. putc ('.');
  339. #endif
  340. }
  341. /*
  342. * Deassert CS_B and cycle the clock to deselect the device.
  343. */
  344. (*fn->cs) (false, false, cookie);
  345. (*fn->clk) (false, true, cookie);
  346. (*fn->clk) (true, true, cookie);
  347. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  348. putc ('\n');
  349. #endif
  350. puts ("Done.\n");
  351. } else {
  352. printf ("%s:%d: NULL Interface function table!\n",
  353. __FUNCTION__, __LINE__);
  354. }
  355. return ret_val;
  356. }
  357. static int virtex2_ss_load(xilinx_desc *desc, const void *buf, size_t bsize)
  358. {
  359. printf ("%s: Slave Serial Loading is unsupported\n", __FUNCTION__);
  360. return FPGA_FAIL;
  361. }
  362. static int virtex2_ss_dump(xilinx_desc *desc, const void *buf, size_t bsize)
  363. {
  364. printf ("%s: Slave Serial Dumping is unsupported\n", __FUNCTION__);
  365. return FPGA_FAIL;
  366. }
  367. /* vim: set ts=4 tw=78: */
  368. struct xilinx_fpga_op virtex2_op = {
  369. .load = virtex2_load,
  370. .dump = virtex2_dump,
  371. .info = virtex2_info,
  372. };