virtex2.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /*
  2. * (C) Copyright 2002
  3. * Rich Ireland, Enterasys Networks, rireland@enterasys.com.
  4. * Keith Outwater, keith_outwater@mvis.com
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. *
  24. */
  25. /*
  26. * Configuration support for Xilinx Virtex2 devices. Based
  27. * on spartan2.c (Rich Ireland, rireland@enterasys.com).
  28. */
  29. #include <common.h>
  30. #include <virtex2.h>
  31. #if (CONFIG_FPGA & (CFG_XILINX | CFG_VIRTEX2))
  32. #if 0
  33. #define FPGA_DEBUG
  34. #endif
  35. #ifdef FPGA_DEBUG
  36. #define PRINTF(fmt,args...) printf (fmt ,##args)
  37. #else
  38. #define PRINTF(fmt,args...)
  39. #endif
  40. /*
  41. * If the SelectMap interface can be overrun by the processor, define
  42. * CFG_FPGA_CHECK_BUSY and/or CONFIG_FPGA_DELAY in the board configuration
  43. * file and add board-specific support for checking BUSY status. By default,
  44. * assume that the SelectMap interface cannot be overrun.
  45. */
  46. #ifndef CFG_FPGA_CHECK_BUSY
  47. #undef CFG_FPGA_CHECK_BUSY
  48. #endif
  49. #ifndef CONFIG_FPGA_DELAY
  50. #define CONFIG_FPGA_DELAY()
  51. #endif
  52. #ifndef CFG_FPGA_PROG_FEEDBACK
  53. #define CFG_FPGA_PROG_FEEDBACK
  54. #endif
  55. /*
  56. * Don't allow config cycle to be interrupted
  57. */
  58. #ifndef CFG_FPGA_CHECK_CTRLC
  59. #undef CFG_FPGA_CHECK_CTRLC
  60. #endif
  61. /*
  62. * Check for errors during configuration by default
  63. */
  64. #ifndef CFG_FPGA_CHECK_ERROR
  65. #define CFG_FPGA_CHECK_ERROR
  66. #endif
  67. /*
  68. * The default timeout in mS for INIT_B to deassert after PROG_B has
  69. * been deasserted. Per the latest Virtex II Handbook (page 347), the
  70. * max time from PORG_B deassertion to INIT_B deassertion is 4uS per
  71. * data frame for the XC2V8000. The XC2V8000 has 2860 data frames
  72. * which yields 11.44 mS. So let's make it bigger in order to handle
  73. * an XC2V1000, if anyone can ever get ahold of one.
  74. */
  75. #ifndef CFG_FPGA_WAIT_INIT
  76. #define CFG_FPGA_WAIT_INIT CFG_HZ/2 /* 500 ms */
  77. #endif
  78. /*
  79. * The default timeout for waiting for BUSY to deassert during configuration.
  80. * This is normally not necessary since for most reasonable configuration
  81. * clock frequencies (i.e. 66 MHz or less), BUSY monitoring is unnecessary.
  82. */
  83. #ifndef CFG_FPGA_WAIT_BUSY
  84. #define CFG_FPGA_WAIT_BUSY CFG_HZ/200 /* 5 ms*/
  85. #endif
  86. /* Default timeout for waiting for FPGA to enter operational mode after
  87. * configuration data has been written.
  88. */
  89. #ifndef CFG_FPGA_WAIT_CONFIG
  90. #define CFG_FPGA_WAIT_CONFIG CFG_HZ/5 /* 200 ms */
  91. #endif
  92. static int Virtex2_ssm_load (Xilinx_desc * desc, void *buf, size_t bsize);
  93. static int Virtex2_ssm_dump (Xilinx_desc * desc, void *buf, size_t bsize);
  94. static int Virtex2_ssm_reloc (Xilinx_desc * desc, ulong reloc_offset);
  95. static int Virtex2_ss_load (Xilinx_desc * desc, void *buf, size_t bsize);
  96. static int Virtex2_ss_dump (Xilinx_desc * desc, void *buf, size_t bsize);
  97. static int Virtex2_ss_reloc (Xilinx_desc * desc, ulong reloc_offset);
  98. int Virtex2_load (Xilinx_desc * desc, 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 Load\n", __FUNCTION__);
  104. ret_val = Virtex2_ss_load (desc, buf, bsize);
  105. break;
  106. case slave_selectmap:
  107. PRINTF ("%s: Launching Slave Parallel Load\n", __FUNCTION__);
  108. ret_val = Virtex2_ssm_load (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. int Virtex2_dump (Xilinx_desc * desc, void *buf, size_t bsize)
  117. {
  118. int ret_val = FPGA_FAIL;
  119. switch (desc->iface) {
  120. case slave_serial:
  121. PRINTF ("%s: Launching Slave Serial Dump\n", __FUNCTION__);
  122. ret_val = Virtex2_ss_dump (desc, buf, bsize);
  123. break;
  124. case slave_parallel:
  125. PRINTF ("%s: Launching Slave Parallel Dump\n", __FUNCTION__);
  126. ret_val = Virtex2_ssm_dump (desc, buf, bsize);
  127. break;
  128. default:
  129. printf ("%s: Unsupported interface type, %d\n",
  130. __FUNCTION__, desc->iface);
  131. }
  132. return ret_val;
  133. }
  134. int Virtex2_info (Xilinx_desc * desc)
  135. {
  136. return FPGA_SUCCESS;
  137. }
  138. int Virtex2_reloc (Xilinx_desc * desc, ulong reloc_offset)
  139. {
  140. int ret_val = FPGA_FAIL;
  141. if (desc->family != Xilinx_Virtex2) {
  142. printf ("%s: Unsupported family type, %d\n",
  143. __FUNCTION__, desc->family);
  144. return FPGA_FAIL;
  145. } else
  146. switch (desc->iface) {
  147. case slave_serial:
  148. ret_val = Virtex2_ss_reloc (desc, reloc_offset);
  149. break;
  150. case slave_selectmap:
  151. ret_val = Virtex2_ssm_reloc (desc, reloc_offset);
  152. break;
  153. default:
  154. printf ("%s: Unsupported interface type, %d\n",
  155. __FUNCTION__, desc->iface);
  156. }
  157. return ret_val;
  158. }
  159. /*
  160. * Virtex-II Slave SelectMap configuration loader. Configuration via
  161. * SelectMap is as follows:
  162. * 1. Set the FPGA's PROG_B line low.
  163. * 2. Set the FPGA's PROG_B line high. Wait for INIT_B to go high.
  164. * 3. Write data to the SelectMap port. If INIT_B goes low at any time
  165. * this process, a configuration error (most likely CRC failure) has
  166. * ocurred. At this point a status word may be read from the
  167. * SelectMap interface to determine the source of the problem (You
  168. * could, for instance, put this in your 'abort' function handler).
  169. * 4. After all data has been written, test the state of the FPGA
  170. * INIT_B and DONE lines. If both are high, configuration has
  171. * succeeded. Congratulations!
  172. */
  173. static int Virtex2_ssm_load (Xilinx_desc * desc, void *buf, size_t bsize)
  174. {
  175. int ret_val = FPGA_FAIL;
  176. Xilinx_Virtex2_Slave_SelectMap_fns *fn = desc->iface_fns;
  177. PRINTF ("%s:%d: Start with interface functions @ 0x%p\n",
  178. __FUNCTION__, __LINE__, fn);
  179. if (fn) {
  180. size_t bytecount = 0;
  181. unsigned char *data = (unsigned char *) buf;
  182. int cookie = desc->cookie;
  183. unsigned long ts;
  184. /* Gotta split this one up (so the stack won't blow??) */
  185. PRINTF ("%s:%d: Function Table:\n"
  186. " base 0x%p\n"
  187. " struct 0x%p\n"
  188. " pre 0x%p\n"
  189. " prog 0x%p\n"
  190. " init 0x%p\n"
  191. " error 0x%p\n",
  192. __FUNCTION__, __LINE__,
  193. &fn, fn, fn->pre, fn->pgm, fn->init, fn->err);
  194. PRINTF (" clock 0x%p\n"
  195. " cs 0x%p\n"
  196. " write 0x%p\n"
  197. " rdata 0x%p\n"
  198. " wdata 0x%p\n"
  199. " busy 0x%p\n"
  200. " abort 0x%p\n"
  201. " post 0x%p\n\n",
  202. fn->clk, fn->cs, fn->wr, fn->rdata, fn->wdata,
  203. fn->busy, fn->abort, fn->post);
  204. #ifdef CFG_FPGA_PROG_FEEDBACK
  205. printf ("Initializing FPGA Device %d...\n", cookie);
  206. #endif
  207. /*
  208. * Run the pre configuration function if there is one.
  209. */
  210. if (*fn->pre) {
  211. (*fn->pre) (cookie);
  212. }
  213. /*
  214. * Assert the program line. The minimum pulse width for
  215. * Virtex II devices is 300 nS (Tprogram parameter in datasheet).
  216. * There is no maximum value for the pulse width. Check to make
  217. * sure that INIT_B goes low after assertion of PROG_B
  218. */
  219. (*fn->pgm) (TRUE, TRUE, cookie);
  220. udelay (10);
  221. ts = get_timer (0);
  222. do {
  223. if (get_timer (ts) > CFG_FPGA_WAIT_INIT) {
  224. printf ("%s:%d: ** Timeout after %d ticks waiting for INIT"
  225. " to assert.\n", __FUNCTION__, __LINE__,
  226. CFG_FPGA_WAIT_INIT);
  227. (*fn->abort) (cookie);
  228. return FPGA_FAIL;
  229. }
  230. } while (!(*fn->init) (cookie));
  231. (*fn->pgm) (FALSE, TRUE, cookie);
  232. CONFIG_FPGA_DELAY ();
  233. (*fn->clk) (TRUE, TRUE, cookie);
  234. /*
  235. * Start a timer and wait for INIT_B to go high
  236. */
  237. ts = get_timer (0);
  238. do {
  239. CONFIG_FPGA_DELAY ();
  240. if (get_timer (ts) > CFG_FPGA_WAIT_INIT) {
  241. printf ("%s:%d: ** Timeout after %d ticks waiting for INIT"
  242. " to deassert.\n", __FUNCTION__, __LINE__,
  243. CFG_FPGA_WAIT_INIT);
  244. (*fn->abort) (cookie);
  245. return FPGA_FAIL;
  246. }
  247. } while ((*fn->init) (cookie) && (*fn->busy) (cookie));
  248. (*fn->wr) (TRUE, TRUE, cookie);
  249. (*fn->cs) (TRUE, TRUE, cookie);
  250. udelay (10000);
  251. /*
  252. * Load the data byte by byte
  253. */
  254. while (bytecount < bsize) {
  255. #ifdef CFG_FPGA_CHECK_CTRLC
  256. if (ctrlc ()) {
  257. (*fn->abort) (cookie);
  258. return FPGA_FAIL;
  259. }
  260. #endif
  261. if ((*fn->done) (cookie) == FPGA_SUCCESS) {
  262. PRINTF ("%s:%d:done went active early, bytecount = %d\n",
  263. __FUNCTION__, __LINE__, bytecount);
  264. break;
  265. }
  266. #ifdef CFG_FPGA_CHECK_ERROR
  267. if ((*fn->init) (cookie)) {
  268. printf ("\n%s:%d: ** Error: INIT asserted during"
  269. " configuration\n", __FUNCTION__, __LINE__);
  270. printf ("%d = buffer offset, %d = buffer size\n",
  271. bytecount, bsize);
  272. (*fn->abort) (cookie);
  273. return FPGA_FAIL;
  274. }
  275. #endif
  276. (*fn->wdata) (data[bytecount++], TRUE, cookie);
  277. CONFIG_FPGA_DELAY ();
  278. /*
  279. * Cycle the clock pin
  280. */
  281. (*fn->clk) (FALSE, TRUE, cookie);
  282. CONFIG_FPGA_DELAY ();
  283. (*fn->clk) (TRUE, TRUE, cookie);
  284. #ifdef CFG_FPGA_CHECK_BUSY
  285. ts = get_timer (0);
  286. while ((*fn->busy) (cookie)) {
  287. if (get_timer (ts) > CFG_FPGA_WAIT_BUSY) {
  288. printf ("%s:%d: ** Timeout after %d ticks waiting for"
  289. " BUSY to deassert\n",
  290. __FUNCTION__, __LINE__, CFG_FPGA_WAIT_BUSY);
  291. (*fn->abort) (cookie);
  292. return FPGA_FAIL;
  293. }
  294. }
  295. #endif
  296. #ifdef CFG_FPGA_PROG_FEEDBACK
  297. if (bytecount % (bsize / 40) == 0)
  298. putc ('.');
  299. #endif
  300. }
  301. /*
  302. * Finished writing the data; deassert FPGA CS_B and WRITE_B signals.
  303. */
  304. CONFIG_FPGA_DELAY ();
  305. (*fn->cs) (FALSE, TRUE, cookie);
  306. (*fn->wr) (FALSE, TRUE, cookie);
  307. #ifdef CFG_FPGA_PROG_FEEDBACK
  308. putc ('\n');
  309. #endif
  310. /*
  311. * Check for successful configuration. FPGA INIT_B and DONE should
  312. * both be high upon successful configuration.
  313. */
  314. ts = get_timer (0);
  315. ret_val = FPGA_SUCCESS;
  316. while (((*fn->done) (cookie) == FPGA_FAIL) || (*fn->init) (cookie)) {
  317. if (get_timer (ts) > CFG_FPGA_WAIT_CONFIG) {
  318. printf ("%s:%d: ** Timeout after %d ticks waiting for DONE to"
  319. "assert and INIT to deassert\n",
  320. __FUNCTION__, __LINE__, CFG_FPGA_WAIT_CONFIG);
  321. (*fn->abort) (cookie);
  322. ret_val = FPGA_FAIL;
  323. break;
  324. }
  325. }
  326. if (ret_val == FPGA_SUCCESS) {
  327. #ifdef CFG_FPGA_PROG_FEEDBACK
  328. printf ("Initialization of FPGA device %d complete\n", cookie);
  329. #endif
  330. /*
  331. * Run the post configuration function if there is one.
  332. */
  333. if (*fn->post) {
  334. (*fn->post) (cookie);
  335. }
  336. } else {
  337. #ifdef CFG_FPGA_PROG_FEEDBACK
  338. printf ("** Initialization of FPGA device %d FAILED\n",
  339. cookie);
  340. #endif
  341. }
  342. } else {
  343. printf ("%s:%d: NULL Interface function table!\n",
  344. __FUNCTION__, __LINE__);
  345. }
  346. return ret_val;
  347. }
  348. /*
  349. * Read the FPGA configuration data
  350. */
  351. static int Virtex2_ssm_dump (Xilinx_desc * desc, void *buf, size_t bsize)
  352. {
  353. int ret_val = FPGA_FAIL;
  354. Xilinx_Virtex2_Slave_SelectMap_fns *fn = desc->iface_fns;
  355. if (fn) {
  356. unsigned char *data = (unsigned char *) buf;
  357. size_t bytecount = 0;
  358. int cookie = desc->cookie;
  359. printf ("Starting Dump of FPGA Device %d...\n", cookie);
  360. (*fn->cs) (TRUE, TRUE, cookie);
  361. (*fn->clk) (TRUE, TRUE, cookie);
  362. while (bytecount < bsize) {
  363. #ifdef CFG_FPGA_CHECK_CTRLC
  364. if (ctrlc ()) {
  365. (*fn->abort) (cookie);
  366. return FPGA_FAIL;
  367. }
  368. #endif
  369. /*
  370. * Cycle the clock and read the data
  371. */
  372. (*fn->clk) (FALSE, TRUE, cookie);
  373. (*fn->clk) (TRUE, TRUE, cookie);
  374. (*fn->rdata) (&(data[bytecount++]), cookie);
  375. #ifdef CFG_FPGA_PROG_FEEDBACK
  376. if (bytecount % (bsize / 40) == 0)
  377. putc ('.');
  378. #endif
  379. }
  380. /*
  381. * Deassert CS_B and cycle the clock to deselect the device.
  382. */
  383. (*fn->cs) (FALSE, FALSE, cookie);
  384. (*fn->clk) (FALSE, TRUE, cookie);
  385. (*fn->clk) (TRUE, TRUE, cookie);
  386. #ifdef CFG_FPGA_PROG_FEEDBACK
  387. putc ('\n');
  388. #endif
  389. puts ("Done.\n");
  390. } else {
  391. printf ("%s:%d: NULL Interface function table!\n",
  392. __FUNCTION__, __LINE__);
  393. }
  394. return ret_val;
  395. }
  396. /*
  397. * Relocate the addresses in the function table from FLASH (or ROM,
  398. * or whatever) to RAM.
  399. */
  400. static int Virtex2_ssm_reloc (Xilinx_desc * desc, ulong reloc_offset)
  401. {
  402. ulong addr;
  403. int ret_val = FPGA_FAIL;
  404. Xilinx_Virtex2_Slave_SelectMap_fns *fn_r, *fn =
  405. (Xilinx_Virtex2_Slave_SelectMap_fns *) (desc->iface_fns);
  406. if (fn) {
  407. /*
  408. * Get the relocated table address
  409. */
  410. addr = (ulong) fn + reloc_offset;
  411. fn_r = (Xilinx_Virtex2_Slave_SelectMap_fns *) addr;
  412. /*
  413. * Check to see if the table has already been relocated. If not, do
  414. * a sanity check to make sure there is a faithful copy of the
  415. * FLASH based function table in RAM, then adjust the table.
  416. */
  417. if (!fn_r->relocated) {
  418. if (memcmp
  419. (fn_r, fn, sizeof (Xilinx_Virtex2_Slave_SelectMap_fns))
  420. == 0) {
  421. desc->iface_fns = fn_r;
  422. } else {
  423. PRINTF ("%s:%d: Invalid function table at 0x%p\n",
  424. __FUNCTION__, __LINE__, fn_r);
  425. return FPGA_FAIL;
  426. }
  427. PRINTF ("%s:%d: Relocating descriptor at 0x%p\n",
  428. __FUNCTION__, __LINE__, desc);
  429. addr = (ulong) (fn->pre) + reloc_offset;
  430. fn_r->pre = (Xilinx_pre_fn) addr;
  431. addr = (ulong) (fn->pgm) + reloc_offset;
  432. fn_r->pgm = (Xilinx_pgm_fn) addr;
  433. addr = (ulong) (fn->init) + reloc_offset;
  434. fn_r->init = (Xilinx_init_fn) addr;
  435. addr = (ulong) (fn->done) + reloc_offset;
  436. fn_r->done = (Xilinx_done_fn) addr;
  437. addr = (ulong) (fn->err) + reloc_offset;
  438. fn_r->err = (Xilinx_err_fn) addr;
  439. addr = (ulong) (fn->clk) + reloc_offset;
  440. fn_r->clk = (Xilinx_clk_fn) addr;
  441. addr = (ulong) (fn->cs) + reloc_offset;
  442. fn_r->cs = (Xilinx_cs_fn) addr;
  443. addr = (ulong) (fn->wr) + reloc_offset;
  444. fn_r->wr = (Xilinx_wr_fn) addr;
  445. addr = (ulong) (fn->rdata) + reloc_offset;
  446. fn_r->rdata = (Xilinx_rdata_fn) addr;
  447. addr = (ulong) (fn->wdata) + reloc_offset;
  448. fn_r->wdata = (Xilinx_wdata_fn) addr;
  449. addr = (ulong) (fn->busy) + reloc_offset;
  450. fn_r->busy = (Xilinx_busy_fn) addr;
  451. addr = (ulong) (fn->abort) + reloc_offset;
  452. fn_r->abort = (Xilinx_abort_fn) addr;
  453. addr = (ulong) (fn->post) + reloc_offset;
  454. fn_r->post = (Xilinx_post_fn) addr;
  455. fn_r->relocated = TRUE;
  456. } else {
  457. printf ("%s:%d: Function table @0x%p has already been relocated\n", __FUNCTION__, __LINE__, fn_r);
  458. desc->iface_fns = fn_r;
  459. }
  460. ret_val = FPGA_SUCCESS;
  461. } else {
  462. printf ("%s: NULL Interface function table!\n", __FUNCTION__);
  463. }
  464. return ret_val;
  465. }
  466. static int Virtex2_ss_load (Xilinx_desc * desc, void *buf, size_t bsize)
  467. {
  468. printf ("%s: Slave Serial Loading is unsupported\n", __FUNCTION__);
  469. return FPGA_FAIL;
  470. }
  471. static int Virtex2_ss_dump (Xilinx_desc * desc, void *buf, size_t bsize)
  472. {
  473. printf ("%s: Slave Serial Dumping is unsupported\n", __FUNCTION__);
  474. return FPGA_FAIL;
  475. }
  476. static int Virtex2_ss_reloc (Xilinx_desc * desc, ulong reloc_offset)
  477. {
  478. int ret_val = FPGA_FAIL;
  479. Xilinx_Virtex2_Slave_Serial_fns *fn =
  480. (Xilinx_Virtex2_Slave_Serial_fns *) (desc->iface_fns);
  481. if (fn) {
  482. printf ("%s:%d: Slave Serial Loading is unsupported\n",
  483. __FUNCTION__, __LINE__);
  484. } else {
  485. printf ("%s:%d: NULL Interface function table!\n",
  486. __FUNCTION__, __LINE__);
  487. }
  488. return ret_val;
  489. }
  490. #endif
  491. /* vim: set ts=4 tw=78: */