fpgajtag.c 5.8 KB

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