cyclon2.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2006
  4. * Heiko Schocher, hs@denx.de
  5. * Based on ACE1XK.c
  6. */
  7. #include <common.h> /* core U-Boot definitions */
  8. #include <altera.h>
  9. #include <ACEX1K.h> /* ACEX device family */
  10. /* Define FPGA_DEBUG to get debug printf's */
  11. #ifdef FPGA_DEBUG
  12. #define PRINTF(fmt,args...) printf (fmt ,##args)
  13. #else
  14. #define PRINTF(fmt,args...)
  15. #endif
  16. /* Note: The assumption is that we cannot possibly run fast enough to
  17. * overrun the device (the Slave Parallel mode can free run at 50MHz).
  18. * If there is a need to operate slower, define CONFIG_FPGA_DELAY in
  19. * the board config file to slow things down.
  20. */
  21. #ifndef CONFIG_FPGA_DELAY
  22. #define CONFIG_FPGA_DELAY()
  23. #endif
  24. #ifndef CONFIG_SYS_FPGA_WAIT
  25. #define CONFIG_SYS_FPGA_WAIT CONFIG_SYS_HZ/10 /* 100 ms */
  26. #endif
  27. static int CYC2_ps_load(Altera_desc *desc, const void *buf, size_t bsize);
  28. static int CYC2_ps_dump(Altera_desc *desc, const void *buf, size_t bsize);
  29. /* static int CYC2_ps_info( Altera_desc *desc ); */
  30. /* ------------------------------------------------------------------------- */
  31. /* CYCLON2 Generic Implementation */
  32. int CYC2_load(Altera_desc *desc, const void *buf, size_t bsize)
  33. {
  34. int ret_val = FPGA_FAIL;
  35. switch (desc->iface) {
  36. case passive_serial:
  37. PRINTF ("%s: Launching Passive Serial Loader\n", __FUNCTION__);
  38. ret_val = CYC2_ps_load (desc, buf, bsize);
  39. break;
  40. case fast_passive_parallel:
  41. /* Fast Passive Parallel (FPP) and PS only differ in what is
  42. * done in the write() callback. Use the existing PS load
  43. * function for FPP, too.
  44. */
  45. PRINTF ("%s: Launching Fast Passive Parallel Loader\n",
  46. __FUNCTION__);
  47. ret_val = CYC2_ps_load(desc, buf, bsize);
  48. break;
  49. /* Add new interface types here */
  50. default:
  51. printf ("%s: Unsupported interface type, %d\n",
  52. __FUNCTION__, desc->iface);
  53. }
  54. return ret_val;
  55. }
  56. int CYC2_dump(Altera_desc *desc, const void *buf, size_t bsize)
  57. {
  58. int ret_val = FPGA_FAIL;
  59. switch (desc->iface) {
  60. case passive_serial:
  61. PRINTF ("%s: Launching Passive Serial Dump\n", __FUNCTION__);
  62. ret_val = CYC2_ps_dump (desc, buf, bsize);
  63. break;
  64. /* Add new interface types here */
  65. default:
  66. printf ("%s: Unsupported interface type, %d\n",
  67. __FUNCTION__, desc->iface);
  68. }
  69. return ret_val;
  70. }
  71. int CYC2_info( Altera_desc *desc )
  72. {
  73. return FPGA_SUCCESS;
  74. }
  75. /* ------------------------------------------------------------------------- */
  76. /* CYCLON2 Passive Serial Generic Implementation */
  77. static int CYC2_ps_load(Altera_desc *desc, const void *buf, size_t bsize)
  78. {
  79. int ret_val = FPGA_FAIL; /* assume the worst */
  80. Altera_CYC2_Passive_Serial_fns *fn = desc->iface_fns;
  81. int ret = 0;
  82. PRINTF ("%s: start with interface functions @ 0x%p\n",
  83. __FUNCTION__, fn);
  84. if (fn) {
  85. int cookie = desc->cookie; /* make a local copy */
  86. unsigned long ts; /* timestamp */
  87. PRINTF ("%s: Function Table:\n"
  88. "ptr:\t0x%p\n"
  89. "struct: 0x%p\n"
  90. "config:\t0x%p\n"
  91. "status:\t0x%p\n"
  92. "write:\t0x%p\n"
  93. "done:\t0x%p\n\n",
  94. __FUNCTION__, &fn, fn, fn->config, fn->status,
  95. fn->write, fn->done);
  96. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  97. printf ("Loading FPGA Device %d...", cookie);
  98. #endif
  99. /*
  100. * Run the pre configuration function if there is one.
  101. */
  102. if (*fn->pre) {
  103. (*fn->pre) (cookie);
  104. }
  105. /* Establish the initial state */
  106. (*fn->config) (false, true, cookie); /* De-assert nCONFIG */
  107. udelay(100);
  108. (*fn->config) (true, true, cookie); /* Assert nCONFIG */
  109. udelay(2); /* T_cfg > 2us */
  110. /* Wait for nSTATUS to be asserted */
  111. ts = get_timer (0); /* get current time */
  112. do {
  113. CONFIG_FPGA_DELAY ();
  114. if (get_timer (ts) > CONFIG_SYS_FPGA_WAIT) { /* check the time */
  115. puts ("** Timeout waiting for STATUS to go high.\n");
  116. (*fn->abort) (cookie);
  117. return FPGA_FAIL;
  118. }
  119. } while (!(*fn->status) (cookie));
  120. /* Get ready for the burn */
  121. CONFIG_FPGA_DELAY ();
  122. ret = (*fn->write) (buf, bsize, true, cookie);
  123. if (ret) {
  124. puts ("** Write failed.\n");
  125. (*fn->abort) (cookie);
  126. return FPGA_FAIL;
  127. }
  128. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  129. puts(" OK? ...");
  130. #endif
  131. CONFIG_FPGA_DELAY ();
  132. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  133. putc (' '); /* terminate the dotted line */
  134. #endif
  135. /*
  136. * Checking FPGA's CONF_DONE signal - correctly booted ?
  137. */
  138. if ( ! (*fn->done) (cookie) ) {
  139. puts ("** Booting failed! CONF_DONE is still deasserted.\n");
  140. (*fn->abort) (cookie);
  141. return (FPGA_FAIL);
  142. }
  143. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  144. puts(" OK\n");
  145. #endif
  146. ret_val = FPGA_SUCCESS;
  147. #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
  148. if (ret_val == FPGA_SUCCESS) {
  149. puts ("Done.\n");
  150. }
  151. else {
  152. puts ("Fail.\n");
  153. }
  154. #endif
  155. (*fn->post) (cookie);
  156. } else {
  157. printf ("%s: NULL Interface function table!\n", __FUNCTION__);
  158. }
  159. return ret_val;
  160. }
  161. static int CYC2_ps_dump(Altera_desc *desc, const void *buf, size_t bsize)
  162. {
  163. /* Readback is only available through the Slave Parallel and */
  164. /* boundary-scan interfaces. */
  165. printf ("%s: Passive Serial Dumping is unavailable\n",
  166. __FUNCTION__);
  167. return FPGA_FAIL;
  168. }