cyclon2.c 7.6 KB

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