virtex2.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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 <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. int Virtex2_load(Xilinx_desc *desc, const void *buf, size_t bsize)
  79. {
  80. int ret_val = FPGA_FAIL;
  81. switch (desc->iface) {
  82. case slave_serial:
  83. PRINTF ("%s: Launching Slave Serial Load\n", __FUNCTION__);
  84. ret_val = Virtex2_ss_load (desc, buf, bsize);
  85. break;
  86. case slave_selectmap:
  87. PRINTF ("%s: Launching Slave Parallel Load\n", __FUNCTION__);
  88. ret_val = Virtex2_ssm_load (desc, buf, bsize);
  89. break;
  90. default:
  91. printf ("%s: Unsupported interface type, %d\n",
  92. __FUNCTION__, desc->iface);
  93. }
  94. return ret_val;
  95. }
  96. int Virtex2_dump(Xilinx_desc *desc, const void *buf, size_t bsize)
  97. {
  98. int ret_val = FPGA_FAIL;
  99. switch (desc->iface) {
  100. case slave_serial:
  101. PRINTF ("%s: Launching Slave Serial Dump\n", __FUNCTION__);
  102. ret_val = Virtex2_ss_dump (desc, buf, bsize);
  103. break;
  104. case slave_parallel:
  105. PRINTF ("%s: Launching Slave Parallel Dump\n", __FUNCTION__);
  106. ret_val = Virtex2_ssm_dump (desc, buf, bsize);
  107. break;
  108. default:
  109. printf ("%s: Unsupported interface type, %d\n",
  110. __FUNCTION__, desc->iface);
  111. }
  112. return ret_val;
  113. }
  114. int Virtex2_info (Xilinx_desc * desc)
  115. {
  116. return FPGA_SUCCESS;
  117. }
  118. /*
  119. * Virtex-II Slave SelectMap configuration loader. Configuration via
  120. * SelectMap is as follows:
  121. * 1. Set the FPGA's PROG_B line low.
  122. * 2. Set the FPGA's PROG_B line high. Wait for INIT_B to go high.
  123. * 3. Write data to the SelectMap port. If INIT_B goes low at any time
  124. * this process, a configuration error (most likely CRC failure) has
  125. * ocurred. At this point a status word may be read from the
  126. * SelectMap interface to determine the source of the problem (You
  127. * could, for instance, put this in your 'abort' function handler).
  128. * 4. After all data has been written, test the state of the FPGA
  129. * INIT_B and DONE lines. If both are high, configuration has
  130. * succeeded. Congratulations!
  131. */
  132. static int Virtex2_ssm_load(Xilinx_desc *desc, const void *buf, size_t bsize)
  133. {
  134. int ret_val = FPGA_FAIL;
  135. Xilinx_Virtex2_Slave_SelectMap_fns *fn = desc->iface_fns;
  136. PRINTF ("%s:%d: Start with interface functions @ 0x%p\n",
  137. __FUNCTION__, __LINE__, fn);
  138. if (fn) {
  139. size_t bytecount = 0;
  140. unsigned char *data = (unsigned char *) buf;
  141. int cookie = desc->cookie;
  142. unsigned long ts;
  143. /* Gotta split this one up (so the stack won't blow??) */
  144. PRINTF ("%s:%d: Function Table:\n"
  145. " base 0x%p\n"
  146. " struct 0x%p\n"
  147. " pre 0x%p\n"
  148. " prog 0x%p\n"
  149. " init 0x%p\n"
  150. " error 0x%p\n",
  151. __FUNCTION__, __LINE__,
  152. &fn, fn, fn->pre, fn->pgm, fn->init, fn->err);
  153. PRINTF (" clock 0x%p\n"
  154. " cs 0x%p\n"
  155. " write 0x%p\n"
  156. " rdata 0x%p\n"
  157. " wdata 0x%p\n"
  158. " busy 0x%p\n"
  159. " abort 0x%p\n"
  160. " post 0x%p\n\n",
  161. fn->clk, fn->cs, fn->wr, fn->rdata, fn->wdata,
  162. fn->busy, fn->abort, fn->post);
  163. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  164. printf ("Initializing FPGA Device %d...\n", cookie);
  165. #endif
  166. /*
  167. * Run the pre configuration function if there is one.
  168. */
  169. if (*fn->pre) {
  170. (*fn->pre) (cookie);
  171. }
  172. /*
  173. * Assert the program line. The minimum pulse width for
  174. * Virtex II devices is 300 nS (Tprogram parameter in datasheet).
  175. * There is no maximum value for the pulse width. Check to make
  176. * sure that INIT_B goes low after assertion of PROG_B
  177. */
  178. (*fn->pgm) (true, true, cookie);
  179. udelay (10);
  180. ts = get_timer (0);
  181. do {
  182. if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT_INIT) {
  183. printf ("%s:%d: ** Timeout after %d ticks waiting for INIT"
  184. " to assert.\n", __FUNCTION__, __LINE__,
  185. CONFIG_SYS_FPGA_WAIT_INIT);
  186. (*fn->abort) (cookie);
  187. return FPGA_FAIL;
  188. }
  189. } while (!(*fn->init) (cookie));
  190. (*fn->pgm) (false, true, cookie);
  191. CONFIG_FPGA_DELAY ();
  192. (*fn->clk) (true, true, cookie);
  193. /*
  194. * Start a timer and wait for INIT_B to go high
  195. */
  196. ts = get_timer (0);
  197. do {
  198. CONFIG_FPGA_DELAY ();
  199. if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT_INIT) {
  200. printf ("%s:%d: ** Timeout after %d ticks waiting for INIT"
  201. " to deassert.\n", __FUNCTION__, __LINE__,
  202. CONFIG_SYS_FPGA_WAIT_INIT);
  203. (*fn->abort) (cookie);
  204. return FPGA_FAIL;
  205. }
  206. } while ((*fn->init) (cookie) && (*fn->busy) (cookie));
  207. (*fn->wr) (true, true, cookie);
  208. (*fn->cs) (true, true, cookie);
  209. udelay (10000);
  210. /*
  211. * Load the data byte by byte
  212. */
  213. while (bytecount < bsize) {
  214. #ifdef CONFIG_SYS_FPGA_CHECK_CTRLC
  215. if (ctrlc ()) {
  216. (*fn->abort) (cookie);
  217. return FPGA_FAIL;
  218. }
  219. #endif
  220. if ((*fn->done) (cookie) == FPGA_SUCCESS) {
  221. PRINTF ("%s:%d:done went active early, bytecount = %d\n",
  222. __FUNCTION__, __LINE__, bytecount);
  223. break;
  224. }
  225. #ifdef CONFIG_SYS_FPGA_CHECK_ERROR
  226. if ((*fn->init) (cookie)) {
  227. printf ("\n%s:%d: ** Error: INIT asserted during"
  228. " configuration\n", __FUNCTION__, __LINE__);
  229. printf ("%d = buffer offset, %d = buffer size\n",
  230. bytecount, bsize);
  231. (*fn->abort) (cookie);
  232. return FPGA_FAIL;
  233. }
  234. #endif
  235. (*fn->wdata) (data[bytecount++], true, cookie);
  236. CONFIG_FPGA_DELAY ();
  237. /*
  238. * Cycle the clock pin
  239. */
  240. (*fn->clk) (false, true, cookie);
  241. CONFIG_FPGA_DELAY ();
  242. (*fn->clk) (true, true, cookie);
  243. #ifdef CONFIG_SYS_FPGA_CHECK_BUSY
  244. ts = get_timer (0);
  245. while ((*fn->busy) (cookie)) {
  246. if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT_BUSY) {
  247. printf ("%s:%d: ** Timeout after %d ticks waiting for"
  248. " BUSY to deassert\n",
  249. __FUNCTION__, __LINE__, CONFIG_SYS_FPGA_WAIT_BUSY);
  250. (*fn->abort) (cookie);
  251. return FPGA_FAIL;
  252. }
  253. }
  254. #endif
  255. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  256. if (bytecount % (bsize / 40) == 0)
  257. putc ('.');
  258. #endif
  259. }
  260. /*
  261. * Finished writing the data; deassert FPGA CS_B and WRITE_B signals.
  262. */
  263. CONFIG_FPGA_DELAY ();
  264. (*fn->cs) (false, true, cookie);
  265. (*fn->wr) (false, true, cookie);
  266. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  267. putc ('\n');
  268. #endif
  269. /*
  270. * Check for successful configuration. FPGA INIT_B and DONE should
  271. * both be high upon successful configuration.
  272. */
  273. ts = get_timer (0);
  274. ret_val = FPGA_SUCCESS;
  275. while (((*fn->done) (cookie) == FPGA_FAIL) || (*fn->init) (cookie)) {
  276. if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT_CONFIG) {
  277. printf ("%s:%d: ** Timeout after %d ticks waiting for DONE to"
  278. "assert and INIT to deassert\n",
  279. __FUNCTION__, __LINE__, CONFIG_SYS_FPGA_WAIT_CONFIG);
  280. (*fn->abort) (cookie);
  281. ret_val = FPGA_FAIL;
  282. break;
  283. }
  284. }
  285. if (ret_val == FPGA_SUCCESS) {
  286. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  287. printf ("Initialization of FPGA device %d complete\n", cookie);
  288. #endif
  289. /*
  290. * Run the post configuration function if there is one.
  291. */
  292. if (*fn->post) {
  293. (*fn->post) (cookie);
  294. }
  295. } else {
  296. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  297. printf ("** Initialization of FPGA device %d FAILED\n",
  298. cookie);
  299. #endif
  300. }
  301. } else {
  302. printf ("%s:%d: NULL Interface function table!\n",
  303. __FUNCTION__, __LINE__);
  304. }
  305. return ret_val;
  306. }
  307. /*
  308. * Read the FPGA configuration data
  309. */
  310. static int Virtex2_ssm_dump(Xilinx_desc *desc, const void *buf, size_t bsize)
  311. {
  312. int ret_val = FPGA_FAIL;
  313. Xilinx_Virtex2_Slave_SelectMap_fns *fn = desc->iface_fns;
  314. if (fn) {
  315. unsigned char *data = (unsigned char *) buf;
  316. size_t bytecount = 0;
  317. int cookie = desc->cookie;
  318. printf ("Starting Dump of FPGA Device %d...\n", cookie);
  319. (*fn->cs) (true, true, cookie);
  320. (*fn->clk) (true, true, cookie);
  321. while (bytecount < bsize) {
  322. #ifdef CONFIG_SYS_FPGA_CHECK_CTRLC
  323. if (ctrlc ()) {
  324. (*fn->abort) (cookie);
  325. return FPGA_FAIL;
  326. }
  327. #endif
  328. /*
  329. * Cycle the clock and read the data
  330. */
  331. (*fn->clk) (false, true, cookie);
  332. (*fn->clk) (true, true, cookie);
  333. (*fn->rdata) (&(data[bytecount++]), cookie);
  334. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  335. if (bytecount % (bsize / 40) == 0)
  336. putc ('.');
  337. #endif
  338. }
  339. /*
  340. * Deassert CS_B and cycle the clock to deselect the device.
  341. */
  342. (*fn->cs) (false, false, cookie);
  343. (*fn->clk) (false, true, cookie);
  344. (*fn->clk) (true, true, cookie);
  345. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  346. putc ('\n');
  347. #endif
  348. puts ("Done.\n");
  349. } else {
  350. printf ("%s:%d: NULL Interface function table!\n",
  351. __FUNCTION__, __LINE__);
  352. }
  353. return ret_val;
  354. }
  355. static int Virtex2_ss_load(Xilinx_desc *desc, const void *buf, size_t bsize)
  356. {
  357. printf ("%s: Slave Serial Loading is unsupported\n", __FUNCTION__);
  358. return FPGA_FAIL;
  359. }
  360. static int Virtex2_ss_dump(Xilinx_desc *desc, const void *buf, size_t bsize)
  361. {
  362. printf ("%s: Slave Serial Dumping is unsupported\n", __FUNCTION__);
  363. return FPGA_FAIL;
  364. }
  365. /* vim: set ts=4 tw=78: */