ACEX1K.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. * (C) Copyright 2003
  3. * Steven Scholz, imc Measurement & Control, steven.scholz@imc-berlin.de
  4. *
  5. * (C) Copyright 2002
  6. * Rich Ireland, Enterasys Networks, rireland@enterasys.com.
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. *
  26. */
  27. #include <common.h> /* core U-Boot definitions */
  28. #include <ACEX1K.h> /* ACEX device family */
  29. #if (CONFIG_FPGA & (CFG_ALTERA | CFG_ACEX1K))
  30. /* Define FPGA_DEBUG to get debug printf's */
  31. /* #define FPGA_DEBUG */
  32. #ifdef FPGA_DEBUG
  33. #define PRINTF(fmt,args...) printf (fmt ,##args)
  34. #else
  35. #define PRINTF(fmt,args...)
  36. #endif
  37. #undef CFG_FPGA_CHECK_BUSY
  38. #define CFG_FPGA_PROG_FEEDBACK
  39. /* Note: The assumption is that we cannot possibly run fast enough to
  40. * overrun the device (the Slave Parallel mode can free run at 50MHz).
  41. * If there is a need to operate slower, define CONFIG_FPGA_DELAY in
  42. * the board config file to slow things down.
  43. */
  44. #ifndef CONFIG_FPGA_DELAY
  45. #define CONFIG_FPGA_DELAY()
  46. #endif
  47. #ifndef CFG_FPGA_WAIT
  48. #define CFG_FPGA_WAIT 100
  49. #endif
  50. static int ACEX1K_ps_load( Altera_desc *desc, void *buf, size_t bsize );
  51. static int ACEX1K_ps_dump( Altera_desc *desc, void *buf, size_t bsize );
  52. /* static int ACEX1K_ps_info( Altera_desc *desc ); */
  53. static int ACEX1K_ps_reloc( Altera_desc *desc, ulong reloc_offset );
  54. /* ------------------------------------------------------------------------- */
  55. /* ACEX1K Generic Implementation */
  56. int ACEX1K_load (Altera_desc * desc, void *buf, size_t bsize)
  57. {
  58. int ret_val = FPGA_FAIL;
  59. switch (desc->iface) {
  60. case passive_serial:
  61. PRINTF ("%s: Launching Passive Serial Loader\n", __FUNCTION__);
  62. ret_val = ACEX1K_ps_load (desc, buf, bsize);
  63. break;
  64. /* Add new interface types here */
  65. default:
  66. printf ("%s: Unsupported interface type, %d\n",
  67. __FUNCTION__, desc->iface);
  68. }
  69. return ret_val;
  70. }
  71. int ACEX1K_dump (Altera_desc * desc, void *buf, size_t bsize)
  72. {
  73. int ret_val = FPGA_FAIL;
  74. switch (desc->iface) {
  75. case passive_serial:
  76. PRINTF ("%s: Launching Passive Serial Dump\n", __FUNCTION__);
  77. ret_val = ACEX1K_ps_dump (desc, buf, bsize);
  78. break;
  79. /* Add new interface types here */
  80. default:
  81. printf ("%s: Unsupported interface type, %d\n",
  82. __FUNCTION__, desc->iface);
  83. }
  84. return ret_val;
  85. }
  86. int ACEX1K_info( Altera_desc *desc )
  87. {
  88. return FPGA_SUCCESS;
  89. }
  90. int ACEX1K_reloc (Altera_desc * desc, ulong reloc_offset)
  91. {
  92. int ret_val = FPGA_FAIL; /* assume a failure */
  93. if (desc->family != Altera_ACEX1K) {
  94. printf ("%s: Unsupported family type, %d\n",
  95. __FUNCTION__, desc->family);
  96. return FPGA_FAIL;
  97. } else
  98. switch (desc->iface) {
  99. case passive_serial:
  100. ret_val = ACEX1K_ps_reloc (desc, reloc_offset);
  101. break;
  102. /* Add new interface types here */
  103. default:
  104. printf ("%s: Unsupported interface type, %d\n",
  105. __FUNCTION__, desc->iface);
  106. }
  107. return ret_val;
  108. }
  109. /* ------------------------------------------------------------------------- */
  110. /* ACEX1K Passive Serial Generic Implementation */
  111. static int ACEX1K_ps_load (Altera_desc * desc, void *buf, size_t bsize)
  112. {
  113. int ret_val = FPGA_FAIL; /* assume the worst */
  114. Altera_ACEX1K_Passive_Serial_fns *fn = desc->iface_fns;
  115. int i;
  116. PRINTF ("%s: start with interface functions @ 0x%p\n",
  117. __FUNCTION__, fn);
  118. if (fn) {
  119. size_t bytecount = 0;
  120. unsigned char *data = (unsigned char *) buf;
  121. int cookie = desc->cookie; /* make a local copy */
  122. unsigned long ts; /* timestamp */
  123. PRINTF ("%s: Function Table:\n"
  124. "ptr:\t0x%p\n"
  125. "struct: 0x%p\n"
  126. "config:\t0x%p\n"
  127. "status:\t0x%p\n"
  128. "clk:\t0x%p\n"
  129. "data:\t0x%p\n"
  130. "done:\t0x%p\n\n",
  131. __FUNCTION__, &fn, fn, fn->config, fn->status,
  132. fn->clk, fn->data, fn->done);
  133. #ifdef CFG_FPGA_PROG_FEEDBACK
  134. printf ("Loading FPGA Device %d (@ %ld)...\n", cookie, ts);
  135. #endif
  136. /*
  137. * Run the pre configuration function if there is one.
  138. */
  139. if (*fn->pre) {
  140. (*fn->pre) (cookie);
  141. }
  142. /* Establish the initial state */
  143. (*fn->config) (TRUE, TRUE, cookie); /* Assert nCONFIG */
  144. udelay(2); /* T_cfg > 2us */
  145. /* nSTATUS should be asserted now */
  146. (*fn->done) (cookie);
  147. if ( !(*fn->status) (cookie) ) {
  148. puts ("** nSTATUS is not asserted.\n");
  149. (*fn->abort) (cookie);
  150. return FPGA_FAIL;
  151. }
  152. (*fn->config) (FALSE, TRUE, cookie); /* Deassert nCONFIG */
  153. udelay(2); /* T_cf2st1 < 4us */
  154. /* Wait for nSTATUS to be released (i.e. deasserted) */
  155. ts = get_timer (0); /* get current time */
  156. do {
  157. CONFIG_FPGA_DELAY ();
  158. if (get_timer (ts) > CFG_FPGA_WAIT) { /* check the time */
  159. puts ("** Timeout waiting for STATUS to go high.\n");
  160. (*fn->abort) (cookie);
  161. return FPGA_FAIL;
  162. }
  163. (*fn->done) (cookie);
  164. } while ((*fn->status) (cookie));
  165. /* Get ready for the burn */
  166. CONFIG_FPGA_DELAY ();
  167. /* Load the data */
  168. while (bytecount < bsize) {
  169. unsigned char val=0;
  170. #ifdef CFG_FPGA_CHECK_CTRLC
  171. if (ctrlc ()) {
  172. (*fn->abort) (cookie);
  173. return FPGA_FAIL;
  174. }
  175. #endif
  176. /* Altera detects an error if INIT goes low (active)
  177. while DONE is low (inactive) */
  178. #if 0 /* not yet implemented */
  179. if ((*fn->done) (cookie) == 0 && (*fn->init) (cookie)) {
  180. puts ("** CRC error during FPGA load.\n");
  181. (*fn->abort) (cookie);
  182. return (FPGA_FAIL);
  183. }
  184. #endif
  185. val = data [bytecount ++ ];
  186. i = 8;
  187. do {
  188. /* Deassert the clock */
  189. (*fn->clk) (FALSE, TRUE, cookie);
  190. CONFIG_FPGA_DELAY ();
  191. /* Write data */
  192. (*fn->data) ( (val & 0x01), TRUE, cookie);
  193. CONFIG_FPGA_DELAY ();
  194. /* Assert the clock */
  195. (*fn->clk) (TRUE, TRUE, cookie);
  196. CONFIG_FPGA_DELAY ();
  197. val >>= 1;
  198. i --;
  199. } while (i > 0);
  200. #ifdef CFG_FPGA_PROG_FEEDBACK
  201. if (bytecount % (bsize / 40) == 0)
  202. putc ('.'); /* let them know we are alive */
  203. #endif
  204. }
  205. CONFIG_FPGA_DELAY ();
  206. #ifdef CFG_FPGA_PROG_FEEDBACK
  207. putc ('\n'); /* terminate the dotted line */
  208. #endif
  209. /*
  210. * Checking FPGA's CONF_DONE signal - correctly booted ?
  211. */
  212. if ( ! (*fn->done) (cookie) ) {
  213. puts ("** Booting failed! CONF_DONE is still deasserted.\n");
  214. (*fn->abort) (cookie);
  215. return (FPGA_FAIL);
  216. }
  217. /*
  218. * "DCLK must be clocked an additional 10 times fpr ACEX 1K..."
  219. */
  220. for (i = 0; i < 12; i++) {
  221. CONFIG_FPGA_DELAY ();
  222. (*fn->clk) (TRUE, TRUE, cookie); /* Assert the clock pin */
  223. CONFIG_FPGA_DELAY ();
  224. (*fn->clk) (FALSE, TRUE, cookie); /* Deassert the clock pin */
  225. }
  226. ret_val = FPGA_SUCCESS;
  227. #ifdef CFG_FPGA_PROG_FEEDBACK
  228. if (ret_val == FPGA_SUCCESS) {
  229. puts ("Done.\n");
  230. }
  231. else {
  232. puts ("Fail.\n");
  233. }
  234. #endif
  235. (*fn->post) (cookie);
  236. } else {
  237. printf ("%s: NULL Interface function table!\n", __FUNCTION__);
  238. }
  239. return ret_val;
  240. }
  241. static int ACEX1K_ps_dump (Altera_desc * desc, void *buf, size_t bsize)
  242. {
  243. /* Readback is only available through the Slave Parallel and */
  244. /* boundary-scan interfaces. */
  245. printf ("%s: Passive Serial Dumping is unavailable\n",
  246. __FUNCTION__);
  247. return FPGA_FAIL;
  248. }
  249. static int ACEX1K_ps_reloc (Altera_desc * desc, ulong reloc_offset)
  250. {
  251. int ret_val = FPGA_FAIL; /* assume the worst */
  252. Altera_ACEX1K_Passive_Serial_fns *fn_r, *fn =
  253. (Altera_ACEX1K_Passive_Serial_fns *) (desc->iface_fns);
  254. if (fn) {
  255. ulong addr;
  256. /* Get the relocated table address */
  257. addr = (ulong) fn + reloc_offset;
  258. fn_r = (Altera_ACEX1K_Passive_Serial_fns *) addr;
  259. if (!fn_r->relocated) {
  260. if (memcmp (fn_r, fn,
  261. sizeof (Altera_ACEX1K_Passive_Serial_fns))
  262. == 0) {
  263. /* good copy of the table, fix the descriptor pointer */
  264. desc->iface_fns = fn_r;
  265. } else {
  266. PRINTF ("%s: Invalid function table at 0x%p\n",
  267. __FUNCTION__, fn_r);
  268. return FPGA_FAIL;
  269. }
  270. PRINTF ("%s: Relocating descriptor at 0x%p\n", __FUNCTION__,
  271. desc);
  272. addr = (ulong) (fn->pre) + reloc_offset;
  273. fn_r->pre = (Altera_pre_fn) addr;
  274. addr = (ulong) (fn->config) + reloc_offset;
  275. fn_r->config = (Altera_config_fn) addr;
  276. addr = (ulong) (fn->status) + reloc_offset;
  277. fn_r->status = (Altera_status_fn) addr;
  278. addr = (ulong) (fn->done) + reloc_offset;
  279. fn_r->done = (Altera_done_fn) addr;
  280. addr = (ulong) (fn->clk) + reloc_offset;
  281. fn_r->clk = (Altera_clk_fn) addr;
  282. addr = (ulong) (fn->data) + reloc_offset;
  283. fn_r->data = (Altera_data_fn) addr;
  284. addr = (ulong) (fn->abort) + reloc_offset;
  285. fn_r->abort = (Altera_abort_fn) addr;
  286. addr = (ulong) (fn->post) + reloc_offset;
  287. fn_r->post = (Altera_post_fn) addr;
  288. fn_r->relocated = TRUE;
  289. } else {
  290. /* this table has already been moved */
  291. /* XXX - should check to see if the descriptor is correct */
  292. desc->iface_fns = fn_r;
  293. }
  294. ret_val = FPGA_SUCCESS;
  295. } else {
  296. printf ("%s: NULL Interface function table!\n", __FUNCTION__);
  297. }
  298. return ret_val;
  299. }
  300. #endif /* (CONFIG_FPGA & (CFG_ALTERA | CFG_ACEX1K)) */