virtex2.c 15 KB

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