fpgajtag.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #define MODULE "fpga"
  2. #include "common.h"
  3. #include "jtag.h"
  4. #include "fpga.h"
  5. #include "spz.h"
  6. /*
  7. * See:
  8. * https://github.com/RichardPlunkett/jrunner-beaglebone/blob/master/jb_jtag.c
  9. * and the Cyclone III (!) handbook, volume 1, table 9-20, page 9-63
  10. */
  11. enum JTAG_IR {
  12. JI_EXTEST = 0x000,
  13. JI_PULSE_NCONFIG = 0x001,
  14. JI_PROGRAM = 0x002,
  15. JI_STARTUP = 0x003,
  16. JI_CHECK_STATUS = 0x004,
  17. JI_SAMPLE = 0x005,
  18. JI_IDCODE = 0x006,
  19. JI_USERCODE = 0x007,
  20. JI_CONFIG_IO = 0x00d,
  21. JI_CLAMP = 0x00a,
  22. JI_HIGHZ = 0x00b,
  23. JI_EXTEST2 = 0x00f, /* Stratix II, Cyclone II */
  24. JI_KEY_CLR_VREG = 0x029,
  25. JI_KEY_PROG_VOL = 0x1ad,
  26. JI_EN_ACTIVE_CLK = 0x1ee,
  27. JI_FACTORY = 0x281,
  28. JI_ACTIVE_ENGAGE = 0x2b0,
  29. JI_ACTIVE_DISENGAGE = 0x2d0,
  30. JI_DIS_ACTIVE_CLK = 0x2ee,
  31. JI_BYPASS = 0x3ff
  32. };
  33. #define FPGA_IR_LEN 10
  34. /* Copied from the SVF file */
  35. #define JTAG_FPGA_LEADIN_BITS (22*8)
  36. /*
  37. * The check status chain seems to match the I/O chain, with in order
  38. * {output, control, input}; the chain represents the pads in
  39. * *reverse* order with bits [2:0] corresponding to pad 363 (D3) and
  40. * [1079:1077] to pad 0; pads 33-36 are the JTAG pins and are not
  41. * included in the chain.
  42. */
  43. #define JTAG_FPGA_CHECK_STATUS_BITS 1080
  44. #define PAD_TO_BIT(p,b) (((359 - ((p) - 4*((p) > 36)))*3)+(b))
  45. #define JTAG_FPGA_CONF_DONE_BIT PAD_TO_BIT(227, 1)
  46. #define JTAG_FPGA_HZ 6000000
  47. #define JTAG_FPGA_MS ((JTAG_FPGA_HZ+999)/1000)
  48. #define JTAG_FPGA_US ((JTAG_FPGA_HZ+999999)/1000000)
  49. #define PIN_FPGA_TDI 16
  50. #define PIN_FPGA_TDO 17
  51. #define PIN_FPGA_TMS 14
  52. #define PIN_FPGA_TCK 18
  53. #define PIN_FPGA_nCE 26
  54. static const struct jtag_config jtag_config_fpga = {
  55. .hz = JTAG_FPGA_HZ,
  56. .pin_tdi = PIN_FPGA_TDI,
  57. .pin_tdo = PIN_FPGA_TDO,
  58. .pin_tms = PIN_FPGA_TMS,
  59. .pin_tck = PIN_FPGA_TCK,
  60. .be = false
  61. };
  62. static bool test_bit(const uint32_t *buf, unsigned int bit)
  63. {
  64. return (buf[bit >> 5] >> (bit & 31)) & 1;
  65. }
  66. static int fpga_finish(int err)
  67. {
  68. tap_goto_state(TAP_RUN_TEST_IDLE);
  69. /* Park IR at bypass, wait 1 ms */
  70. tap_set_ir(JI_BYPASS, FPGA_IR_LEN);
  71. tap_run_test_idle(JTAG_FPGA_MS);
  72. /* Reset?! */
  73. jtag_disable(NULL);
  74. return err;
  75. }
  76. static uint32_t tap_get_idcode(void)
  77. {
  78. uint32_t idcode;
  79. tap_set_ir(JI_IDCODE, FPGA_IR_LEN);
  80. tap_goto_state(TAP_SHIFT_DR);
  81. jtag_io(32, JIO_TMS, NULL, &idcode);
  82. tap_goto_state(TAP_RUN_TEST_IDLE);
  83. return idcode;
  84. }
  85. /*
  86. * See the Cyclone IV handbook, volume 1, table 8-17, page 8-59
  87. * for the programming flow.
  88. */
  89. int fpga_program_spz(spz_stream *spz)
  90. {
  91. int err = 0;
  92. uint32_t idcode;
  93. uint32_t check_status_buf[(JTAG_FPGA_CHECK_STATUS_BITS+31) >> 5];
  94. /* Configure JTAG to access the FPGA */
  95. jtag_enable(&jtag_config_fpga);
  96. int idcode_loops = 4;
  97. while (idcode_loops--) {
  98. idcode = tap_get_idcode();
  99. if (idcode == spz->header.addr)
  100. break;
  101. MSG("invalid IDCODE %08X expected %08X, %s\n",
  102. idcode, spz->header.addr,
  103. idcode_loops ? "attempting reset..." : "giving up");
  104. if (!idcode_loops) {
  105. MSG("check for JTAG cable connected, or power cycle board\n");
  106. err = FWUPDATE_ERR_FPGA_MISMATCH;
  107. goto fail;
  108. }
  109. tap_reset();
  110. jtag_delay(1000);
  111. tap_goto_state(TAP_SHIFT_DR);
  112. jtag_io(32, JIO_TMS, NULL, &idcode);
  113. MSG("IDCODE after reset %08X\n", idcode);
  114. }
  115. MSG("IDCODE %08X is valid\n", idcode);
  116. /* Disengage programming hardware if active */
  117. tap_set_ir(JI_ACTIVE_DISENGAGE, FPGA_IR_LEN);
  118. tap_run_test_idle(16);
  119. tap_set_ir(JI_PROGRAM, FPGA_IR_LEN);
  120. tap_run_test_idle(16);
  121. jtag_delay(100);
  122. tap_run_test_idle(8192);
  123. /* Leadin: shift in a number of 1s */
  124. tap_goto_state(TAP_SHIFT_DR);
  125. jtag_io(JTAG_FPGA_LEADIN_BITS, JIO_TDI, NULL, NULL);
  126. /* The actual data */
  127. err = jtag_shift_spz(spz, 0);
  128. /* 32 bits of 0 terminates the transaction */
  129. jtag_io(32, JIO_TMS, NULL, NULL);
  130. tap_goto_state(TAP_RUN_TEST_IDLE);
  131. /* Check status */
  132. int check_status_loops = 10;
  133. while (1) {
  134. tap_set_ir(JI_CHECK_STATUS, FPGA_IR_LEN);
  135. tap_run_test_idle(5*JTAG_FPGA_US);
  136. tap_goto_state(TAP_SHIFT_DR);
  137. jtag_io(JTAG_FPGA_CHECK_STATUS_BITS, JIO_TMS, NULL, check_status_buf);
  138. tap_goto_state(TAP_RUN_TEST_IDLE);
  139. if (!test_bit(check_status_buf, JTAG_FPGA_CONF_DONE_BIT)) {
  140. check_status_loops--;
  141. MSG("not ready to start... %s\n",
  142. check_status_loops ? "waiting" : "giving up");
  143. if (!check_status_loops) {
  144. err = FWUPDATE_ERR_FPGA_FAILED;
  145. goto fail;
  146. }
  147. jtag_delay(10000); /* 10 ms */
  148. } else {
  149. MSG("ready to start\n");
  150. break;
  151. }
  152. }
  153. /* Go to user mode */
  154. tap_set_ir(JI_STARTUP, FPGA_IR_LEN);
  155. tap_run_test_idle((4096*JTAG_FPGA_MS)/1000+512);
  156. /* Common finish */
  157. fail:
  158. return fpga_finish(err);
  159. }
  160. //
  161. // Board 2.1 has IO26 connected to nCE on the FPGA; this pin has
  162. // a pulldown but can be raised high by external JTAG. We don't
  163. // want the external pulldown to fight an internal pullup, but
  164. // also don't want the pin to float on the 1.0 and 2.0 board revisions
  165. // where it is NC.
  166. //
  167. // XXX: Actually try to detect board revision 2.1...
  168. //
  169. // YYY: IO26 is CS# för PSRAM! This is invalid usage... probably will need
  170. // a rework on the 2.1 board!
  171. //
  172. void fpga_enable_nce(void)
  173. {
  174. #if 0
  175. pinMode(PIN_FPGA_nCE, INPUT_PULLDOWN);
  176. delayMicroseconds(100); /* Just in case */
  177. #endif
  178. }
  179. int fpga_reset(void)
  180. {
  181. int err = 0;
  182. printf("[FPGA] Resetting FPGA via JTAG\n");
  183. fpga_enable_nce();
  184. jtag_enable(&jtag_config_fpga);
  185. tap_run_test_idle(JTAG_FPGA_MS);
  186. /* Make sure to enable loader (not supposed to be needed...) */
  187. tap_set_ir(JI_ACTIVE_ENGAGE, FPGA_IR_LEN);
  188. tap_run_test_idle(16);
  189. /* Pulse nCONFIG via JTAG */
  190. tap_set_ir(JI_PULSE_NCONFIG, FPGA_IR_LEN);
  191. tap_run_test_idle(JTAG_FPGA_MS);
  192. /* Common finish */
  193. fail:
  194. return fpga_finish(err);
  195. }