ACEX1K.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. static int ACEX1K_ps_reloc( Altera_desc *desc, ulong reloc_offset );
  50. /* ------------------------------------------------------------------------- */
  51. /* ACEX1K Generic Implementation */
  52. int ACEX1K_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 = ACEX1K_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 ACEX1K_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 = ACEX1K_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 ACEX1K_info( Altera_desc *desc )
  83. {
  84. return FPGA_SUCCESS;
  85. }
  86. int ACEX1K_reloc (Altera_desc * desc, ulong reloc_offset)
  87. {
  88. int ret_val = FPGA_FAIL; /* assume a failure */
  89. if (desc->family != Altera_ACEX1K) {
  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 = ACEX1K_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. /* ACEX1K Passive Serial Generic Implementation */
  107. static int ACEX1K_ps_load (Altera_desc * desc, void *buf, size_t bsize)
  108. {
  109. int ret_val = FPGA_FAIL; /* assume the worst */
  110. Altera_ACEX1K_Passive_Serial_fns *fn = desc->iface_fns;
  111. int i;
  112. PRINTF ("%s: start with interface functions @ 0x%p\n",
  113. __FUNCTION__, fn);
  114. if (fn) {
  115. size_t bytecount = 0;
  116. unsigned char *data = (unsigned char *) buf;
  117. int cookie = desc->cookie; /* make a local copy */
  118. unsigned long ts; /* timestamp */
  119. PRINTF ("%s: Function Table:\n"
  120. "ptr:\t0x%p\n"
  121. "struct: 0x%p\n"
  122. "config:\t0x%p\n"
  123. "status:\t0x%p\n"
  124. "clk:\t0x%p\n"
  125. "data:\t0x%p\n"
  126. "done:\t0x%p\n\n",
  127. __FUNCTION__, &fn, fn, fn->config, fn->status,
  128. fn->clk, fn->data, fn->done);
  129. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  130. printf ("Loading FPGA Device %d...", cookie);
  131. #endif
  132. /*
  133. * Run the pre configuration function if there is one.
  134. */
  135. if (*fn->pre) {
  136. (*fn->pre) (cookie);
  137. }
  138. /* Establish the initial state */
  139. (*fn->config) (TRUE, TRUE, cookie); /* Assert nCONFIG */
  140. udelay(2); /* T_cfg > 2us */
  141. /* nSTATUS should be asserted now */
  142. (*fn->done) (cookie);
  143. if ( !(*fn->status) (cookie) ) {
  144. puts ("** nSTATUS is not asserted.\n");
  145. (*fn->abort) (cookie);
  146. return FPGA_FAIL;
  147. }
  148. (*fn->config) (FALSE, TRUE, cookie); /* Deassert nCONFIG */
  149. udelay(2); /* T_cf2st1 < 4us */
  150. /* Wait for nSTATUS to be released (i.e. deasserted) */
  151. ts = get_timer (0); /* get current time */
  152. do {
  153. CONFIG_FPGA_DELAY ();
  154. if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT) { /* check the time */
  155. puts ("** Timeout waiting for STATUS to go high.\n");
  156. (*fn->abort) (cookie);
  157. return FPGA_FAIL;
  158. }
  159. (*fn->done) (cookie);
  160. } while ((*fn->status) (cookie));
  161. /* Get ready for the burn */
  162. CONFIG_FPGA_DELAY ();
  163. /* Load the data */
  164. while (bytecount < bsize) {
  165. unsigned char val=0;
  166. #ifdef CONFIG_SYS_FPGA_CHECK_CTRLC
  167. if (ctrlc ()) {
  168. (*fn->abort) (cookie);
  169. return FPGA_FAIL;
  170. }
  171. #endif
  172. /* Altera detects an error if INIT goes low (active)
  173. while DONE is low (inactive) */
  174. #if 0 /* not yet implemented */
  175. if ((*fn->done) (cookie) == 0 && (*fn->init) (cookie)) {
  176. puts ("** CRC error during FPGA load.\n");
  177. (*fn->abort) (cookie);
  178. return (FPGA_FAIL);
  179. }
  180. #endif
  181. val = data [bytecount ++ ];
  182. i = 8;
  183. do {
  184. /* Deassert the clock */
  185. (*fn->clk) (FALSE, TRUE, cookie);
  186. CONFIG_FPGA_DELAY ();
  187. /* Write data */
  188. (*fn->data) ( (val & 0x01), TRUE, cookie);
  189. CONFIG_FPGA_DELAY ();
  190. /* Assert the clock */
  191. (*fn->clk) (TRUE, TRUE, cookie);
  192. CONFIG_FPGA_DELAY ();
  193. val >>= 1;
  194. i --;
  195. } while (i > 0);
  196. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  197. if (bytecount % (bsize / 40) == 0)
  198. putc ('.'); /* let them know we are alive */
  199. #endif
  200. }
  201. CONFIG_FPGA_DELAY ();
  202. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  203. putc (' '); /* terminate the dotted line */
  204. #endif
  205. /*
  206. * Checking FPGA's CONF_DONE signal - correctly booted ?
  207. */
  208. if ( ! (*fn->done) (cookie) ) {
  209. puts ("** Booting failed! CONF_DONE is still deasserted.\n");
  210. (*fn->abort) (cookie);
  211. return (FPGA_FAIL);
  212. }
  213. /*
  214. * "DCLK must be clocked an additional 10 times fpr ACEX 1K..."
  215. */
  216. for (i = 0; i < 12; i++) {
  217. CONFIG_FPGA_DELAY ();
  218. (*fn->clk) (TRUE, TRUE, cookie); /* Assert the clock pin */
  219. CONFIG_FPGA_DELAY ();
  220. (*fn->clk) (FALSE, TRUE, cookie); /* Deassert the clock pin */
  221. }
  222. ret_val = FPGA_SUCCESS;
  223. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  224. if (ret_val == FPGA_SUCCESS) {
  225. puts ("Done.\n");
  226. }
  227. else {
  228. puts ("Fail.\n");
  229. }
  230. #endif
  231. (*fn->post) (cookie);
  232. } else {
  233. printf ("%s: NULL Interface function table!\n", __FUNCTION__);
  234. }
  235. return ret_val;
  236. }
  237. static int ACEX1K_ps_dump (Altera_desc * desc, void *buf, size_t bsize)
  238. {
  239. /* Readback is only available through the Slave Parallel and */
  240. /* boundary-scan interfaces. */
  241. printf ("%s: Passive Serial Dumping is unavailable\n",
  242. __FUNCTION__);
  243. return FPGA_FAIL;
  244. }
  245. static int ACEX1K_ps_reloc (Altera_desc * desc, ulong reloc_offset)
  246. {
  247. int ret_val = FPGA_FAIL; /* assume the worst */
  248. Altera_ACEX1K_Passive_Serial_fns *fn_r, *fn =
  249. (Altera_ACEX1K_Passive_Serial_fns *) (desc->iface_fns);
  250. if (fn) {
  251. ulong addr;
  252. /* Get the relocated table address */
  253. addr = (ulong) fn + reloc_offset;
  254. fn_r = (Altera_ACEX1K_Passive_Serial_fns *) addr;
  255. if (!fn_r->relocated) {
  256. if (memcmp (fn_r, fn,
  257. sizeof (Altera_ACEX1K_Passive_Serial_fns))
  258. == 0) {
  259. /* good copy of the table, fix the descriptor pointer */
  260. desc->iface_fns = fn_r;
  261. } else {
  262. PRINTF ("%s: Invalid function table at 0x%p\n",
  263. __FUNCTION__, fn_r);
  264. return FPGA_FAIL;
  265. }
  266. PRINTF ("%s: Relocating descriptor at 0x%p\n", __FUNCTION__,
  267. desc);
  268. addr = (ulong) (fn->pre) + reloc_offset;
  269. fn_r->pre = (Altera_pre_fn) addr;
  270. addr = (ulong) (fn->config) + reloc_offset;
  271. fn_r->config = (Altera_config_fn) addr;
  272. addr = (ulong) (fn->status) + reloc_offset;
  273. fn_r->status = (Altera_status_fn) addr;
  274. addr = (ulong) (fn->done) + reloc_offset;
  275. fn_r->done = (Altera_done_fn) addr;
  276. addr = (ulong) (fn->clk) + reloc_offset;
  277. fn_r->clk = (Altera_clk_fn) addr;
  278. addr = (ulong) (fn->data) + reloc_offset;
  279. fn_r->data = (Altera_data_fn) addr;
  280. addr = (ulong) (fn->abort) + reloc_offset;
  281. fn_r->abort = (Altera_abort_fn) addr;
  282. addr = (ulong) (fn->post) + reloc_offset;
  283. fn_r->post = (Altera_post_fn) addr;
  284. fn_r->relocated = TRUE;
  285. } else {
  286. /* this table has already been moved */
  287. /* XXX - should check to see if the descriptor is correct */
  288. desc->iface_fns = fn_r;
  289. }
  290. ret_val = FPGA_SUCCESS;
  291. } else {
  292. printf ("%s: NULL Interface function table!\n", __FUNCTION__);
  293. }
  294. return ret_val;
  295. }