spartan3.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /*
  2. * (C) Copyright 2002
  3. * Rich Ireland, Enterasys Networks, rireland@enterasys.com.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. /*
  8. * Configuration support for Xilinx Spartan3 devices. Based
  9. * on spartan2.c (Rich Ireland, rireland@enterasys.com).
  10. */
  11. #include <common.h> /* core U-Boot definitions */
  12. #include <spartan3.h> /* Spartan-II device family */
  13. /* Define FPGA_DEBUG to get debug printf's */
  14. #ifdef FPGA_DEBUG
  15. #define PRINTF(fmt,args...) printf (fmt ,##args)
  16. #else
  17. #define PRINTF(fmt,args...)
  18. #endif
  19. #undef CONFIG_SYS_FPGA_CHECK_BUSY
  20. /* Note: The assumption is that we cannot possibly run fast enough to
  21. * overrun the device (the Slave Parallel mode can free run at 50MHz).
  22. * If there is a need to operate slower, define CONFIG_FPGA_DELAY in
  23. * the board config file to slow things down.
  24. */
  25. #ifndef CONFIG_FPGA_DELAY
  26. #define CONFIG_FPGA_DELAY()
  27. #endif
  28. #ifndef CONFIG_SYS_FPGA_WAIT
  29. #define CONFIG_SYS_FPGA_WAIT CONFIG_SYS_HZ/100 /* 10 ms */
  30. #endif
  31. static int spartan3_sp_load(xilinx_desc *desc, const void *buf, size_t bsize);
  32. static int spartan3_sp_dump(xilinx_desc *desc, const void *buf, size_t bsize);
  33. /* static int spartan3_sp_info(xilinx_desc *desc ); */
  34. static int spartan3_ss_load(xilinx_desc *desc, const void *buf, size_t bsize);
  35. static int spartan3_ss_dump(xilinx_desc *desc, const void *buf, size_t bsize);
  36. /* static int spartan3_ss_info(xilinx_desc *desc); */
  37. /* ------------------------------------------------------------------------- */
  38. /* Spartan-II Generic Implementation */
  39. static int spartan3_load(xilinx_desc *desc, const void *buf, size_t bsize,
  40. bitstream_type bstype)
  41. {
  42. int ret_val = FPGA_FAIL;
  43. switch (desc->iface) {
  44. case slave_serial:
  45. PRINTF ("%s: Launching Slave Serial Load\n", __FUNCTION__);
  46. ret_val = spartan3_ss_load(desc, buf, bsize);
  47. break;
  48. case slave_parallel:
  49. PRINTF ("%s: Launching Slave Parallel Load\n", __FUNCTION__);
  50. ret_val = spartan3_sp_load(desc, buf, bsize);
  51. break;
  52. default:
  53. printf ("%s: Unsupported interface type, %d\n",
  54. __FUNCTION__, desc->iface);
  55. }
  56. return ret_val;
  57. }
  58. static int spartan3_dump(xilinx_desc *desc, const void *buf, size_t bsize)
  59. {
  60. int ret_val = FPGA_FAIL;
  61. switch (desc->iface) {
  62. case slave_serial:
  63. PRINTF ("%s: Launching Slave Serial Dump\n", __FUNCTION__);
  64. ret_val = spartan3_ss_dump(desc, buf, bsize);
  65. break;
  66. case slave_parallel:
  67. PRINTF ("%s: Launching Slave Parallel Dump\n", __FUNCTION__);
  68. ret_val = spartan3_sp_dump(desc, buf, bsize);
  69. break;
  70. default:
  71. printf ("%s: Unsupported interface type, %d\n",
  72. __FUNCTION__, desc->iface);
  73. }
  74. return ret_val;
  75. }
  76. static int spartan3_info(xilinx_desc *desc)
  77. {
  78. return FPGA_SUCCESS;
  79. }
  80. /* ------------------------------------------------------------------------- */
  81. /* Spartan-II Slave Parallel Generic Implementation */
  82. static int spartan3_sp_load(xilinx_desc *desc, const void *buf, size_t bsize)
  83. {
  84. int ret_val = FPGA_FAIL; /* assume the worst */
  85. xilinx_spartan3_slave_parallel_fns *fn = desc->iface_fns;
  86. PRINTF ("%s: start with interface functions @ 0x%p\n",
  87. __FUNCTION__, fn);
  88. if (fn) {
  89. size_t bytecount = 0;
  90. unsigned char *data = (unsigned char *) buf;
  91. int cookie = desc->cookie; /* make a local copy */
  92. unsigned long ts; /* timestamp */
  93. PRINTF ("%s: Function Table:\n"
  94. "ptr:\t0x%p\n"
  95. "struct: 0x%p\n"
  96. "pre: 0x%p\n"
  97. "pgm:\t0x%p\n"
  98. "init:\t0x%p\n"
  99. "err:\t0x%p\n"
  100. "clk:\t0x%p\n"
  101. "cs:\t0x%p\n"
  102. "wr:\t0x%p\n"
  103. "read data:\t0x%p\n"
  104. "write data:\t0x%p\n"
  105. "busy:\t0x%p\n"
  106. "abort:\t0x%p\n",
  107. "post:\t0x%p\n\n",
  108. __FUNCTION__, &fn, fn, fn->pre, fn->pgm, fn->init, fn->err,
  109. fn->clk, fn->cs, fn->wr, fn->rdata, fn->wdata, fn->busy,
  110. fn->abort, fn->post);
  111. /*
  112. * This code is designed to emulate the "Express Style"
  113. * Continuous Data Loading in Slave Parallel Mode for
  114. * the Spartan-II Family.
  115. */
  116. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  117. printf ("Loading FPGA Device %d...\n", cookie);
  118. #endif
  119. /*
  120. * Run the pre configuration function if there is one.
  121. */
  122. if (*fn->pre) {
  123. (*fn->pre) (cookie);
  124. }
  125. /* Establish the initial state */
  126. (*fn->pgm) (true, true, cookie); /* Assert the program, commit */
  127. /* Get ready for the burn */
  128. CONFIG_FPGA_DELAY ();
  129. (*fn->pgm) (false, true, cookie); /* Deassert the program, commit */
  130. ts = get_timer (0); /* get current time */
  131. /* Now wait for INIT and BUSY to go high */
  132. do {
  133. CONFIG_FPGA_DELAY ();
  134. if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT) { /* check the time */
  135. puts ("** Timeout waiting for INIT to clear.\n");
  136. (*fn->abort) (cookie); /* abort the burn */
  137. return FPGA_FAIL;
  138. }
  139. } while ((*fn->init) (cookie) && (*fn->busy) (cookie));
  140. (*fn->wr) (true, true, cookie); /* Assert write, commit */
  141. (*fn->cs) (true, true, cookie); /* Assert chip select, commit */
  142. (*fn->clk) (true, true, cookie); /* Assert the clock pin */
  143. /* Load the data */
  144. while (bytecount < bsize) {
  145. /* XXX - do we check for an Ctrl-C press in here ??? */
  146. /* XXX - Check the error bit? */
  147. (*fn->wdata) (data[bytecount++], true, cookie); /* write the data */
  148. CONFIG_FPGA_DELAY ();
  149. (*fn->clk) (false, true, cookie); /* Deassert the clock pin */
  150. CONFIG_FPGA_DELAY ();
  151. (*fn->clk) (true, true, cookie); /* Assert the clock pin */
  152. #ifdef CONFIG_SYS_FPGA_CHECK_BUSY
  153. ts = get_timer (0); /* get current time */
  154. while ((*fn->busy) (cookie)) {
  155. /* XXX - we should have a check in here somewhere to
  156. * make sure we aren't busy forever... */
  157. CONFIG_FPGA_DELAY ();
  158. (*fn->clk) (false, true, cookie); /* Deassert the clock pin */
  159. CONFIG_FPGA_DELAY ();
  160. (*fn->clk) (true, true, cookie); /* Assert the clock pin */
  161. if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT) { /* check the time */
  162. puts ("** Timeout waiting for BUSY to clear.\n");
  163. (*fn->abort) (cookie); /* abort the burn */
  164. return FPGA_FAIL;
  165. }
  166. }
  167. #endif
  168. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  169. if (bytecount % (bsize / 40) == 0)
  170. putc ('.'); /* let them know we are alive */
  171. #endif
  172. }
  173. CONFIG_FPGA_DELAY ();
  174. (*fn->cs) (false, true, cookie); /* Deassert the chip select */
  175. (*fn->wr) (false, true, cookie); /* Deassert the write pin */
  176. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  177. putc ('\n'); /* terminate the dotted line */
  178. #endif
  179. /* now check for done signal */
  180. ts = get_timer (0); /* get current time */
  181. ret_val = FPGA_SUCCESS;
  182. while ((*fn->done) (cookie) == FPGA_FAIL) {
  183. /* XXX - we should have a check in here somewhere to
  184. * make sure we aren't busy forever... */
  185. CONFIG_FPGA_DELAY ();
  186. (*fn->clk) (false, true, cookie); /* Deassert the clock pin */
  187. CONFIG_FPGA_DELAY ();
  188. (*fn->clk) (true, true, cookie); /* Assert the clock pin */
  189. if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT) { /* check the time */
  190. puts ("** Timeout waiting for DONE to clear.\n");
  191. (*fn->abort) (cookie); /* abort the burn */
  192. ret_val = FPGA_FAIL;
  193. break;
  194. }
  195. }
  196. /*
  197. * Run the post configuration function if there is one.
  198. */
  199. if (*fn->post)
  200. (*fn->post) (cookie);
  201. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  202. if (ret_val == FPGA_SUCCESS)
  203. puts ("Done.\n");
  204. else
  205. puts ("Fail.\n");
  206. #endif
  207. } else {
  208. printf ("%s: NULL Interface function table!\n", __FUNCTION__);
  209. }
  210. return ret_val;
  211. }
  212. static int spartan3_sp_dump(xilinx_desc *desc, const void *buf, size_t bsize)
  213. {
  214. int ret_val = FPGA_FAIL; /* assume the worst */
  215. xilinx_spartan3_slave_parallel_fns *fn = desc->iface_fns;
  216. if (fn) {
  217. unsigned char *data = (unsigned char *) buf;
  218. size_t bytecount = 0;
  219. int cookie = desc->cookie; /* make a local copy */
  220. printf ("Starting Dump of FPGA Device %d...\n", cookie);
  221. (*fn->cs) (true, true, cookie); /* Assert chip select, commit */
  222. (*fn->clk) (true, true, cookie); /* Assert the clock pin */
  223. /* dump the data */
  224. while (bytecount < bsize) {
  225. /* XXX - do we check for an Ctrl-C press in here ??? */
  226. (*fn->clk) (false, true, cookie); /* Deassert the clock pin */
  227. (*fn->clk) (true, true, cookie); /* Assert the clock pin */
  228. (*fn->rdata) (&(data[bytecount++]), cookie); /* read the data */
  229. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  230. if (bytecount % (bsize / 40) == 0)
  231. putc ('.'); /* let them know we are alive */
  232. #endif
  233. }
  234. (*fn->cs) (false, false, cookie); /* Deassert the chip select */
  235. (*fn->clk) (false, true, cookie); /* Deassert the clock pin */
  236. (*fn->clk) (true, true, cookie); /* Assert the clock pin */
  237. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  238. putc ('\n'); /* terminate the dotted line */
  239. #endif
  240. puts ("Done.\n");
  241. /* XXX - checksum the data? */
  242. } else {
  243. printf ("%s: NULL Interface function table!\n", __FUNCTION__);
  244. }
  245. return ret_val;
  246. }
  247. /* ------------------------------------------------------------------------- */
  248. static int spartan3_ss_load(xilinx_desc *desc, const void *buf, size_t bsize)
  249. {
  250. int ret_val = FPGA_FAIL; /* assume the worst */
  251. xilinx_spartan3_slave_serial_fns *fn = desc->iface_fns;
  252. int i;
  253. unsigned char val;
  254. PRINTF ("%s: start with interface functions @ 0x%p\n",
  255. __FUNCTION__, fn);
  256. if (fn) {
  257. size_t bytecount = 0;
  258. unsigned char *data = (unsigned char *) buf;
  259. int cookie = desc->cookie; /* make a local copy */
  260. unsigned long ts; /* timestamp */
  261. PRINTF ("%s: Function Table:\n"
  262. "ptr:\t0x%p\n"
  263. "struct: 0x%p\n"
  264. "pgm:\t0x%p\n"
  265. "init:\t0x%p\n"
  266. "clk:\t0x%p\n"
  267. "wr:\t0x%p\n"
  268. "done:\t0x%p\n\n",
  269. __FUNCTION__, &fn, fn, fn->pgm, fn->init,
  270. fn->clk, fn->wr, fn->done);
  271. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  272. printf ("Loading FPGA Device %d...\n", cookie);
  273. #endif
  274. /*
  275. * Run the pre configuration function if there is one.
  276. */
  277. if (*fn->pre) {
  278. (*fn->pre) (cookie);
  279. }
  280. /* Establish the initial state */
  281. (*fn->pgm) (true, true, cookie); /* Assert the program, commit */
  282. /* Wait for INIT state (init low) */
  283. ts = get_timer (0); /* get current time */
  284. do {
  285. CONFIG_FPGA_DELAY ();
  286. if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT) { /* check the time */
  287. puts ("** Timeout waiting for INIT to start.\n");
  288. if (*fn->abort)
  289. (*fn->abort) (cookie);
  290. return FPGA_FAIL;
  291. }
  292. } while (!(*fn->init) (cookie));
  293. /* Get ready for the burn */
  294. CONFIG_FPGA_DELAY ();
  295. (*fn->pgm) (false, true, cookie); /* Deassert the program, commit */
  296. ts = get_timer (0); /* get current time */
  297. /* Now wait for INIT to go high */
  298. do {
  299. CONFIG_FPGA_DELAY ();
  300. if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT) { /* check the time */
  301. puts ("** Timeout waiting for INIT to clear.\n");
  302. if (*fn->abort)
  303. (*fn->abort) (cookie);
  304. return FPGA_FAIL;
  305. }
  306. } while ((*fn->init) (cookie));
  307. /* Load the data */
  308. if(*fn->bwr)
  309. (*fn->bwr) (data, bsize, true, cookie);
  310. else {
  311. while (bytecount < bsize) {
  312. /* Xilinx detects an error if INIT goes low (active)
  313. while DONE is low (inactive) */
  314. if ((*fn->done) (cookie) == 0 && (*fn->init) (cookie)) {
  315. puts ("** CRC error during FPGA load.\n");
  316. if (*fn->abort)
  317. (*fn->abort) (cookie);
  318. return (FPGA_FAIL);
  319. }
  320. val = data [bytecount ++];
  321. i = 8;
  322. do {
  323. /* Deassert the clock */
  324. (*fn->clk) (false, true, cookie);
  325. CONFIG_FPGA_DELAY ();
  326. /* Write data */
  327. (*fn->wr) ((val & 0x80), true, cookie);
  328. CONFIG_FPGA_DELAY ();
  329. /* Assert the clock */
  330. (*fn->clk) (true, true, cookie);
  331. CONFIG_FPGA_DELAY ();
  332. val <<= 1;
  333. i --;
  334. } while (i > 0);
  335. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  336. if (bytecount % (bsize / 40) == 0)
  337. putc ('.'); /* let them know we are alive */
  338. #endif
  339. }
  340. }
  341. CONFIG_FPGA_DELAY ();
  342. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  343. putc ('\n'); /* terminate the dotted line */
  344. #endif
  345. /* now check for done signal */
  346. ts = get_timer (0); /* get current time */
  347. ret_val = FPGA_SUCCESS;
  348. (*fn->wr) (true, true, cookie);
  349. while (! (*fn->done) (cookie)) {
  350. /* XXX - we should have a check in here somewhere to
  351. * make sure we aren't busy forever... */
  352. CONFIG_FPGA_DELAY ();
  353. (*fn->clk) (false, true, cookie); /* Deassert the clock pin */
  354. CONFIG_FPGA_DELAY ();
  355. (*fn->clk) (true, true, cookie); /* Assert the clock pin */
  356. putc ('*');
  357. if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT) { /* check the time */
  358. puts ("** Timeout waiting for DONE to clear.\n");
  359. ret_val = FPGA_FAIL;
  360. break;
  361. }
  362. }
  363. putc ('\n'); /* terminate the dotted line */
  364. /*
  365. * Run the post configuration function if there is one.
  366. */
  367. if (*fn->post)
  368. (*fn->post) (cookie);
  369. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  370. if (ret_val == FPGA_SUCCESS)
  371. puts ("Done.\n");
  372. else
  373. puts ("Fail.\n");
  374. #endif
  375. } else {
  376. printf ("%s: NULL Interface function table!\n", __FUNCTION__);
  377. }
  378. return ret_val;
  379. }
  380. static int spartan3_ss_dump(xilinx_desc *desc, const void *buf, size_t bsize)
  381. {
  382. /* Readback is only available through the Slave Parallel and */
  383. /* boundary-scan interfaces. */
  384. printf ("%s: Slave Serial Dumping is unavailable\n",
  385. __FUNCTION__);
  386. return FPGA_FAIL;
  387. }
  388. struct xilinx_fpga_op spartan3_op = {
  389. .load = spartan3_load,
  390. .dump = spartan3_dump,
  391. .info = spartan3_info,
  392. };