virtex2.c 15 KB

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