virtex2.c 11 KB

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