ACEX1K.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2003
  4. * Steven Scholz, imc Measurement & Control, steven.scholz@imc-berlin.de
  5. *
  6. * (C) Copyright 2002
  7. * Rich Ireland, Enterasys Networks, rireland@enterasys.com.
  8. */
  9. #define LOG_CATEGORY UCLASS_FPGA
  10. #include <common.h> /* core U-Boot definitions */
  11. #include <console.h>
  12. #include <log.h>
  13. #include <ACEX1K.h> /* ACEX device family */
  14. #include <linux/delay.h>
  15. /* Note: The assumption is that we cannot possibly run fast enough to
  16. * overrun the device (the Slave Parallel mode can free run at 50MHz).
  17. * If there is a need to operate slower, define CFG_FPGA_DELAY in
  18. * the board config file to slow things down.
  19. */
  20. #ifndef CFG_FPGA_DELAY
  21. #define CFG_FPGA_DELAY()
  22. #endif
  23. #ifndef CFG_SYS_FPGA_WAIT
  24. #define CFG_SYS_FPGA_WAIT CONFIG_SYS_HZ/10 /* 100 ms */
  25. #endif
  26. static int ACEX1K_ps_load(Altera_desc *desc, const void *buf, size_t bsize);
  27. static int ACEX1K_ps_dump(Altera_desc *desc, const void *buf, size_t bsize);
  28. /* static int ACEX1K_ps_info(Altera_desc *desc); */
  29. /* ------------------------------------------------------------------------- */
  30. /* ACEX1K Generic Implementation */
  31. int ACEX1K_load(Altera_desc *desc, const void *buf, size_t bsize)
  32. {
  33. int ret_val = FPGA_FAIL;
  34. switch (desc->iface) {
  35. case passive_serial:
  36. log_debug("Launching Passive Serial Loader\n");
  37. ret_val = ACEX1K_ps_load (desc, buf, bsize);
  38. break;
  39. /* Add new interface types here */
  40. default:
  41. printf ("%s: Unsupported interface type, %d\n",
  42. __FUNCTION__, desc->iface);
  43. }
  44. return ret_val;
  45. }
  46. int ACEX1K_dump(Altera_desc *desc, const void *buf, size_t bsize)
  47. {
  48. int ret_val = FPGA_FAIL;
  49. switch (desc->iface) {
  50. case passive_serial:
  51. log_debug("Launching Passive Serial Dump\n");
  52. ret_val = ACEX1K_ps_dump (desc, buf, bsize);
  53. break;
  54. /* Add new interface types here */
  55. default:
  56. printf ("%s: Unsupported interface type, %d\n",
  57. __FUNCTION__, desc->iface);
  58. }
  59. return ret_val;
  60. }
  61. int ACEX1K_info( Altera_desc *desc )
  62. {
  63. return FPGA_SUCCESS;
  64. }
  65. /* ------------------------------------------------------------------------- */
  66. /* ACEX1K Passive Serial Generic Implementation */
  67. static int ACEX1K_ps_load(Altera_desc *desc, const void *buf, size_t bsize)
  68. {
  69. int ret_val = FPGA_FAIL; /* assume the worst */
  70. Altera_ACEX1K_Passive_Serial_fns *fn = desc->iface_fns;
  71. int i;
  72. log_debug("start with interface functions @ 0x%p\n", fn);
  73. if (fn) {
  74. size_t bytecount = 0;
  75. unsigned char *data = (unsigned char *) buf;
  76. int cookie = desc->cookie; /* make a local copy */
  77. unsigned long ts; /* timestamp */
  78. log_debug("Function Table:\n"
  79. "ptr:\t0x%p\n"
  80. "struct: 0x%p\n"
  81. "config:\t0x%p\n"
  82. "status:\t0x%p\n"
  83. "clk:\t0x%p\n"
  84. "data:\t0x%p\n"
  85. "done:\t0x%p\n\n",
  86. &fn, fn, fn->config, fn->status,
  87. fn->clk, fn->data, fn->done);
  88. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  89. printf ("Loading FPGA Device %d...", cookie);
  90. #endif
  91. /*
  92. * Run the pre configuration function if there is one.
  93. */
  94. if (*fn->pre) {
  95. (*fn->pre) (cookie);
  96. }
  97. /* Establish the initial state */
  98. (*fn->config) (true, true, cookie); /* Assert nCONFIG */
  99. udelay(2); /* T_cfg > 2us */
  100. /* nSTATUS should be asserted now */
  101. (*fn->done) (cookie);
  102. if ( !(*fn->status) (cookie) ) {
  103. puts ("** nSTATUS is not asserted.\n");
  104. (*fn->abort) (cookie);
  105. return FPGA_FAIL;
  106. }
  107. (*fn->config) (false, true, cookie); /* Deassert nCONFIG */
  108. udelay(2); /* T_cf2st1 < 4us */
  109. /* Wait for nSTATUS to be released (i.e. deasserted) */
  110. ts = get_timer (0); /* get current time */
  111. do {
  112. CFG_FPGA_DELAY ();
  113. if (get_timer (ts) > CFG_SYS_FPGA_WAIT) { /* check the time */
  114. puts ("** Timeout waiting for STATUS to go high.\n");
  115. (*fn->abort) (cookie);
  116. return FPGA_FAIL;
  117. }
  118. (*fn->done) (cookie);
  119. } while ((*fn->status) (cookie));
  120. /* Get ready for the burn */
  121. CFG_FPGA_DELAY ();
  122. /* Load the data */
  123. while (bytecount < bsize) {
  124. unsigned char val=0;
  125. #ifdef CONFIG_SYS_FPGA_CHECK_CTRLC
  126. if (ctrlc ()) {
  127. (*fn->abort) (cookie);
  128. return FPGA_FAIL;
  129. }
  130. #endif
  131. /* Altera detects an error if INIT goes low (active)
  132. while DONE is low (inactive) */
  133. #if 0 /* not yet implemented */
  134. if ((*fn->done) (cookie) == 0 && (*fn->init) (cookie)) {
  135. puts ("** CRC error during FPGA load.\n");
  136. (*fn->abort) (cookie);
  137. return (FPGA_FAIL);
  138. }
  139. #endif
  140. val = data [bytecount ++ ];
  141. i = 8;
  142. do {
  143. /* Deassert the clock */
  144. (*fn->clk) (false, true, cookie);
  145. CFG_FPGA_DELAY ();
  146. /* Write data */
  147. (*fn->data) ((val & 0x01), true, cookie);
  148. CFG_FPGA_DELAY ();
  149. /* Assert the clock */
  150. (*fn->clk) (true, true, cookie);
  151. CFG_FPGA_DELAY ();
  152. val >>= 1;
  153. i --;
  154. } while (i > 0);
  155. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  156. if (bytecount % (bsize / 40) == 0)
  157. putc ('.'); /* let them know we are alive */
  158. #endif
  159. }
  160. CFG_FPGA_DELAY ();
  161. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  162. putc (' '); /* terminate the dotted line */
  163. #endif
  164. /*
  165. * Checking FPGA's CONF_DONE signal - correctly booted ?
  166. */
  167. if ( ! (*fn->done) (cookie) ) {
  168. puts ("** Booting failed! CONF_DONE is still deasserted.\n");
  169. (*fn->abort) (cookie);
  170. return (FPGA_FAIL);
  171. }
  172. /*
  173. * "DCLK must be clocked an additional 10 times fpr ACEX 1K..."
  174. */
  175. for (i = 0; i < 12; i++) {
  176. CFG_FPGA_DELAY ();
  177. (*fn->clk) (true, true, cookie); /* Assert the clock pin */
  178. CFG_FPGA_DELAY ();
  179. (*fn->clk) (false, true, cookie); /* Deassert the clock pin */
  180. }
  181. ret_val = FPGA_SUCCESS;
  182. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  183. if (ret_val == FPGA_SUCCESS) {
  184. puts ("Done.\n");
  185. }
  186. else {
  187. puts ("Fail.\n");
  188. }
  189. #endif
  190. (*fn->post) (cookie);
  191. } else {
  192. printf ("%s: NULL Interface function table!\n", __FUNCTION__);
  193. }
  194. return ret_val;
  195. }
  196. static int ACEX1K_ps_dump(Altera_desc *desc, const void *buf, size_t bsize)
  197. {
  198. /* Readback is only available through the Slave Parallel and */
  199. /* boundary-scan interfaces. */
  200. printf ("%s: Passive Serial Dumping is unavailable\n",
  201. __FUNCTION__);
  202. return FPGA_FAIL;
  203. }