virtex2.c 13 KB

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