ACEX1K.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. /* 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 CONFIG_SYS_FPGA_WAIT
  44. #define CONFIG_SYS_FPGA_WAIT CONFIG_SYS_HZ/10 /* 100 ms */
  45. #endif
  46. static int ACEX1K_ps_load( Altera_desc *desc, void *buf, size_t bsize );
  47. static int ACEX1K_ps_dump( Altera_desc *desc, void *buf, size_t bsize );
  48. /* static int ACEX1K_ps_info( Altera_desc *desc ); */
  49. /* ------------------------------------------------------------------------- */
  50. /* ACEX1K Generic Implementation */
  51. int ACEX1K_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 = ACEX1K_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 ACEX1K_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 = ACEX1K_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 ACEX1K_info( Altera_desc *desc )
  82. {
  83. return FPGA_SUCCESS;
  84. }
  85. /* ------------------------------------------------------------------------- */
  86. /* ACEX1K Passive Serial Generic Implementation */
  87. static int ACEX1K_ps_load (Altera_desc * desc, void *buf, size_t bsize)
  88. {
  89. int ret_val = FPGA_FAIL; /* assume the worst */
  90. Altera_ACEX1K_Passive_Serial_fns *fn = desc->iface_fns;
  91. int i;
  92. PRINTF ("%s: start with interface functions @ 0x%p\n",
  93. __FUNCTION__, fn);
  94. if (fn) {
  95. size_t bytecount = 0;
  96. unsigned char *data = (unsigned char *) buf;
  97. int cookie = desc->cookie; /* make a local copy */
  98. unsigned long ts; /* timestamp */
  99. PRINTF ("%s: Function Table:\n"
  100. "ptr:\t0x%p\n"
  101. "struct: 0x%p\n"
  102. "config:\t0x%p\n"
  103. "status:\t0x%p\n"
  104. "clk:\t0x%p\n"
  105. "data:\t0x%p\n"
  106. "done:\t0x%p\n\n",
  107. __FUNCTION__, &fn, fn, fn->config, fn->status,
  108. fn->clk, fn->data, fn->done);
  109. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  110. printf ("Loading FPGA Device %d...", 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->config) (TRUE, TRUE, cookie); /* Assert nCONFIG */
  120. udelay(2); /* T_cfg > 2us */
  121. /* nSTATUS should be asserted now */
  122. (*fn->done) (cookie);
  123. if ( !(*fn->status) (cookie) ) {
  124. puts ("** nSTATUS is not asserted.\n");
  125. (*fn->abort) (cookie);
  126. return FPGA_FAIL;
  127. }
  128. (*fn->config) (FALSE, TRUE, cookie); /* Deassert nCONFIG */
  129. udelay(2); /* T_cf2st1 < 4us */
  130. /* Wait for nSTATUS to be released (i.e. deasserted) */
  131. ts = get_timer (0); /* get current time */
  132. do {
  133. CONFIG_FPGA_DELAY ();
  134. if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT) { /* check the time */
  135. puts ("** Timeout waiting for STATUS to go high.\n");
  136. (*fn->abort) (cookie);
  137. return FPGA_FAIL;
  138. }
  139. (*fn->done) (cookie);
  140. } while ((*fn->status) (cookie));
  141. /* Get ready for the burn */
  142. CONFIG_FPGA_DELAY ();
  143. /* Load the data */
  144. while (bytecount < bsize) {
  145. unsigned char val=0;
  146. #ifdef CONFIG_SYS_FPGA_CHECK_CTRLC
  147. if (ctrlc ()) {
  148. (*fn->abort) (cookie);
  149. return FPGA_FAIL;
  150. }
  151. #endif
  152. /* Altera detects an error if INIT goes low (active)
  153. while DONE is low (inactive) */
  154. #if 0 /* not yet implemented */
  155. if ((*fn->done) (cookie) == 0 && (*fn->init) (cookie)) {
  156. puts ("** CRC error during FPGA load.\n");
  157. (*fn->abort) (cookie);
  158. return (FPGA_FAIL);
  159. }
  160. #endif
  161. val = data [bytecount ++ ];
  162. i = 8;
  163. do {
  164. /* Deassert the clock */
  165. (*fn->clk) (FALSE, TRUE, cookie);
  166. CONFIG_FPGA_DELAY ();
  167. /* Write data */
  168. (*fn->data) ( (val & 0x01), TRUE, cookie);
  169. CONFIG_FPGA_DELAY ();
  170. /* Assert the clock */
  171. (*fn->clk) (TRUE, TRUE, cookie);
  172. CONFIG_FPGA_DELAY ();
  173. val >>= 1;
  174. i --;
  175. } while (i > 0);
  176. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  177. if (bytecount % (bsize / 40) == 0)
  178. putc ('.'); /* let them know we are alive */
  179. #endif
  180. }
  181. CONFIG_FPGA_DELAY ();
  182. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  183. putc (' '); /* terminate the dotted line */
  184. #endif
  185. /*
  186. * Checking FPGA's CONF_DONE signal - correctly booted ?
  187. */
  188. if ( ! (*fn->done) (cookie) ) {
  189. puts ("** Booting failed! CONF_DONE is still deasserted.\n");
  190. (*fn->abort) (cookie);
  191. return (FPGA_FAIL);
  192. }
  193. /*
  194. * "DCLK must be clocked an additional 10 times fpr ACEX 1K..."
  195. */
  196. for (i = 0; i < 12; i++) {
  197. CONFIG_FPGA_DELAY ();
  198. (*fn->clk) (TRUE, TRUE, cookie); /* Assert the clock pin */
  199. CONFIG_FPGA_DELAY ();
  200. (*fn->clk) (FALSE, TRUE, cookie); /* Deassert the clock pin */
  201. }
  202. ret_val = FPGA_SUCCESS;
  203. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  204. if (ret_val == FPGA_SUCCESS) {
  205. puts ("Done.\n");
  206. }
  207. else {
  208. puts ("Fail.\n");
  209. }
  210. #endif
  211. (*fn->post) (cookie);
  212. } else {
  213. printf ("%s: NULL Interface function table!\n", __FUNCTION__);
  214. }
  215. return ret_val;
  216. }
  217. static int ACEX1K_ps_dump (Altera_desc * desc, void *buf, size_t bsize)
  218. {
  219. /* Readback is only available through the Slave Parallel and */
  220. /* boundary-scan interfaces. */
  221. printf ("%s: Passive Serial Dumping is unavailable\n",
  222. __FUNCTION__);
  223. return FPGA_FAIL;
  224. }