cyclon2.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * (C) Copyright 2006
  3. * Heiko Schocher, hs@denx.de
  4. * Based on ACE1XK.c
  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. #include <common.h> /* core U-Boot definitions */
  26. #include <altera.h>
  27. #include <ACEX1K.h> /* ACEX device family */
  28. #if (CONFIG_FPGA & (CFG_ALTERA | CFG_CYCLON2))
  29. /* Define FPGA_DEBUG to get debug printf's */
  30. #ifdef FPGA_DEBUG
  31. #define PRINTF(fmt,args...) printf (fmt ,##args)
  32. #else
  33. #define PRINTF(fmt,args...)
  34. #endif
  35. /* Note: The assumption is that we cannot possibly run fast enough to
  36. * overrun the device (the Slave Parallel mode can free run at 50MHz).
  37. * If there is a need to operate slower, define CONFIG_FPGA_DELAY in
  38. * the board config file to slow things down.
  39. */
  40. #ifndef CONFIG_FPGA_DELAY
  41. #define CONFIG_FPGA_DELAY()
  42. #endif
  43. #ifndef CFG_FPGA_WAIT
  44. #define CFG_FPGA_WAIT CFG_HZ/10 /* 100 ms */
  45. #endif
  46. static int CYC2_ps_load( Altera_desc *desc, void *buf, size_t bsize );
  47. static int CYC2_ps_dump( Altera_desc *desc, void *buf, size_t bsize );
  48. /* static int CYC2_ps_info( Altera_desc *desc ); */
  49. static int CYC2_ps_reloc( Altera_desc *desc, ulong reloc_offset );
  50. /* ------------------------------------------------------------------------- */
  51. /* CYCLON2 Generic Implementation */
  52. int CYC2_load (Altera_desc * desc, void *buf, size_t bsize)
  53. {
  54. int ret_val = FPGA_FAIL;
  55. switch (desc->iface) {
  56. case passive_serial:
  57. PRINTF ("%s: Launching Passive Serial Loader\n", __FUNCTION__);
  58. ret_val = CYC2_ps_load (desc, buf, bsize);
  59. break;
  60. /* Add new interface types here */
  61. default:
  62. printf ("%s: Unsupported interface type, %d\n",
  63. __FUNCTION__, desc->iface);
  64. }
  65. return ret_val;
  66. }
  67. int CYC2_dump (Altera_desc * desc, void *buf, size_t bsize)
  68. {
  69. int ret_val = FPGA_FAIL;
  70. switch (desc->iface) {
  71. case passive_serial:
  72. PRINTF ("%s: Launching Passive Serial Dump\n", __FUNCTION__);
  73. ret_val = CYC2_ps_dump (desc, buf, bsize);
  74. break;
  75. /* Add new interface types here */
  76. default:
  77. printf ("%s: Unsupported interface type, %d\n",
  78. __FUNCTION__, desc->iface);
  79. }
  80. return ret_val;
  81. }
  82. int CYC2_info( Altera_desc *desc )
  83. {
  84. return FPGA_SUCCESS;
  85. }
  86. int CYC2_reloc (Altera_desc * desc, ulong reloc_offset)
  87. {
  88. int ret_val = FPGA_FAIL; /* assume a failure */
  89. if (desc->family != Altera_CYC2) {
  90. printf ("%s: Unsupported family type, %d\n",
  91. __FUNCTION__, desc->family);
  92. return FPGA_FAIL;
  93. } else
  94. switch (desc->iface) {
  95. case passive_serial:
  96. ret_val = CYC2_ps_reloc (desc, reloc_offset);
  97. break;
  98. /* Add new interface types here */
  99. default:
  100. printf ("%s: Unsupported interface type, %d\n",
  101. __FUNCTION__, desc->iface);
  102. }
  103. return ret_val;
  104. }
  105. /* ------------------------------------------------------------------------- */
  106. /* CYCLON2 Passive Serial Generic Implementation */
  107. static int CYC2_ps_load (Altera_desc * desc, void *buf, size_t bsize)
  108. {
  109. int ret_val = FPGA_FAIL; /* assume the worst */
  110. Altera_CYC2_Passive_Serial_fns *fn = desc->iface_fns;
  111. int ret = 0;
  112. PRINTF ("%s: start with interface functions @ 0x%p\n",
  113. __FUNCTION__, fn);
  114. if (fn) {
  115. int cookie = desc->cookie; /* make a local copy */
  116. unsigned long ts; /* timestamp */
  117. PRINTF ("%s: Function Table:\n"
  118. "ptr:\t0x%p\n"
  119. "struct: 0x%p\n"
  120. "config:\t0x%p\n"
  121. "status:\t0x%p\n"
  122. "write:\t0x%p\n"
  123. "done:\t0x%p\n\n",
  124. __FUNCTION__, &fn, fn, fn->config, fn->status,
  125. fn->write, fn->done);
  126. #ifdef CFG_FPGA_PROG_FEEDBACK
  127. printf ("Loading FPGA Device %d...", cookie);
  128. #endif
  129. /*
  130. * Run the pre configuration function if there is one.
  131. */
  132. if (*fn->pre) {
  133. (*fn->pre) (cookie);
  134. }
  135. /* Establish the initial state */
  136. (*fn->config) (TRUE, TRUE, cookie); /* Assert nCONFIG */
  137. udelay(2); /* T_cfg > 2us */
  138. /* Wait for nSTATUS to be asserted */
  139. ts = get_timer (0); /* get current time */
  140. do {
  141. CONFIG_FPGA_DELAY ();
  142. if (get_timer (ts) > CFG_FPGA_WAIT) { /* check the time */
  143. puts ("** Timeout waiting for STATUS to go high.\n");
  144. (*fn->abort) (cookie);
  145. return FPGA_FAIL;
  146. }
  147. } while (!(*fn->status) (cookie));
  148. /* Get ready for the burn */
  149. CONFIG_FPGA_DELAY ();
  150. ret = (*fn->write) (buf, bsize, TRUE, cookie);
  151. if (ret) {
  152. puts ("** Write failed.\n");
  153. (*fn->abort) (cookie);
  154. return FPGA_FAIL;
  155. }
  156. #ifdef CFG_FPGA_PROG_FEEDBACK
  157. puts(" OK? ...");
  158. #endif
  159. CONFIG_FPGA_DELAY ();
  160. #ifdef CFG_FPGA_PROG_FEEDBACK
  161. putc (' '); /* terminate the dotted line */
  162. #endif
  163. /*
  164. * Checking FPGA's CONF_DONE signal - correctly booted ?
  165. */
  166. if ( ! (*fn->done) (cookie) ) {
  167. puts ("** Booting failed! CONF_DONE is still deasserted.\n");
  168. (*fn->abort) (cookie);
  169. return (FPGA_FAIL);
  170. }
  171. #ifdef CFG_FPGA_PROG_FEEDBACK
  172. puts(" OK\n");
  173. #endif
  174. ret_val = FPGA_SUCCESS;
  175. #ifdef CFG_FPGA_PROG_FEEDBACK
  176. if (ret_val == FPGA_SUCCESS) {
  177. puts ("Done.\n");
  178. }
  179. else {
  180. puts ("Fail.\n");
  181. }
  182. #endif
  183. (*fn->post) (cookie);
  184. } else {
  185. printf ("%s: NULL Interface function table!\n", __FUNCTION__);
  186. }
  187. return ret_val;
  188. }
  189. static int CYC2_ps_dump (Altera_desc * desc, void *buf, size_t bsize)
  190. {
  191. /* Readback is only available through the Slave Parallel and */
  192. /* boundary-scan interfaces. */
  193. printf ("%s: Passive Serial Dumping is unavailable\n",
  194. __FUNCTION__);
  195. return FPGA_FAIL;
  196. }
  197. static int CYC2_ps_reloc (Altera_desc * desc, ulong reloc_offset)
  198. {
  199. int ret_val = FPGA_FAIL; /* assume the worst */
  200. Altera_CYC2_Passive_Serial_fns *fn_r, *fn =
  201. (Altera_CYC2_Passive_Serial_fns *) (desc->iface_fns);
  202. if (fn) {
  203. ulong addr;
  204. /* Get the relocated table address */
  205. addr = (ulong) fn + reloc_offset;
  206. fn_r = (Altera_CYC2_Passive_Serial_fns *) addr;
  207. if (!fn_r->relocated) {
  208. if (memcmp (fn_r, fn,
  209. sizeof (Altera_CYC2_Passive_Serial_fns))
  210. == 0) {
  211. /* good copy of the table, fix the descriptor pointer */
  212. desc->iface_fns = fn_r;
  213. } else {
  214. PRINTF ("%s: Invalid function table at 0x%p\n",
  215. __FUNCTION__, fn_r);
  216. return FPGA_FAIL;
  217. }
  218. PRINTF ("%s: Relocating descriptor at 0x%p\n", __FUNCTION__,
  219. desc);
  220. addr = (ulong) (fn->pre) + reloc_offset;
  221. fn_r->pre = (Altera_pre_fn) addr;
  222. addr = (ulong) (fn->config) + reloc_offset;
  223. fn_r->config = (Altera_config_fn) addr;
  224. addr = (ulong) (fn->status) + reloc_offset;
  225. fn_r->status = (Altera_status_fn) addr;
  226. addr = (ulong) (fn->done) + reloc_offset;
  227. fn_r->done = (Altera_done_fn) addr;
  228. addr = (ulong) (fn->write) + reloc_offset;
  229. fn_r->write = (Altera_write_fn) addr;
  230. addr = (ulong) (fn->abort) + reloc_offset;
  231. fn_r->abort = (Altera_abort_fn) addr;
  232. addr = (ulong) (fn->post) + reloc_offset;
  233. fn_r->post = (Altera_post_fn) addr;
  234. fn_r->relocated = TRUE;
  235. } else {
  236. /* this table has already been moved */
  237. /* XXX - should check to see if the descriptor is correct */
  238. desc->iface_fns = fn_r;
  239. }
  240. ret_val = FPGA_SUCCESS;
  241. } else {
  242. printf ("%s: NULL Interface function table!\n", __FUNCTION__);
  243. }
  244. return ret_val;
  245. }
  246. #endif /* (CONFIG_FPGA & (CFG_ALTERA | CFG_CYCLON2)) */