spartan3.c 13 KB

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