ACEX1K.c 9.3 KB

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