fpgajtag.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. static const struct jtag_config jtag_config_fpga = {
  50. .hz = JTAG_FPGA_HZ,
  51. .pin_tdi = 16,
  52. .pin_tdo = 17,
  53. .pin_tms = 14,
  54. .pin_tck = 18,
  55. .be = false
  56. };
  57. static bool test_bit(const uint32_t *buf, unsigned int bit)
  58. {
  59. return (buf[bit >> 5] >> (bit & 31)) & 1;
  60. }
  61. static void fpga_finish(void)
  62. {
  63. tap_goto_state(TAP_RUN_TEST_IDLE);
  64. /* Park IR at bypass, wait 1 ms */
  65. tap_set_ir(JI_BYPASS, FPGA_IR_LEN);
  66. tap_run_test_idle(JTAG_FPGA_MS);
  67. /* Reset?! */
  68. jtag_disable(NULL);
  69. }
  70. static uint32_t tap_get_idcode(void)
  71. {
  72. uint32_t idcode;
  73. tap_set_ir(JI_IDCODE, FPGA_IR_LEN);
  74. tap_goto_state(TAP_SHIFT_DR);
  75. jtag_io(32, JIO_TMS, NULL, &idcode);
  76. tap_goto_state(TAP_RUN_TEST_IDLE);
  77. return idcode;
  78. }
  79. /*
  80. * See the Cyclone IV handbook, volume 1, table 8-17, page 8-59
  81. * for the programming flow.
  82. */
  83. int fpga_program_spz(spz_stream *spz)
  84. {
  85. int err = 0;
  86. uint32_t idcode;
  87. uint32_t check_status_buf[(JTAG_FPGA_CHECK_STATUS_BITS+31) >> 5];
  88. /* Configure JTAG to access the FPGA */
  89. jtag_enable(&jtag_config_fpga);
  90. int idcode_loops = 4;
  91. while (idcode_loops--) {
  92. idcode = tap_get_idcode();
  93. if (idcode == spz->header.addr)
  94. break;
  95. MSG("invalid IDCODE %08X expected %08X, %s\n",
  96. idcode, spz->header.addr,
  97. idcode_loops ? "attempting reset..." : "giving up");
  98. if (!idcode_loops) {
  99. MSG("check for JTAG cable connected, or power cycle board\n");
  100. err = FWUPDATE_ERR_FPGA_MISMATCH;
  101. goto fail;
  102. }
  103. tap_reset();
  104. jtag_delay(1000);
  105. tap_goto_state(TAP_SHIFT_DR);
  106. jtag_io(32, JIO_TMS, NULL, &idcode);
  107. MSG("IDCODE after reset %08X\n", idcode);
  108. }
  109. MSG("IDCODE %08X is valid\n", idcode);
  110. /* Disengage programming hardware if active */
  111. tap_set_ir(JI_ACTIVE_DISENGAGE, FPGA_IR_LEN);
  112. tap_run_test_idle(16);
  113. tap_set_ir(JI_PROGRAM, FPGA_IR_LEN);
  114. tap_run_test_idle(16);
  115. jtag_delay(100);
  116. tap_run_test_idle(8192);
  117. /* Leadin: shift in a number of 1s */
  118. tap_goto_state(TAP_SHIFT_DR);
  119. jtag_io(JTAG_FPGA_LEADIN_BITS, JIO_TDI, NULL, NULL);
  120. /* The actual data */
  121. err = jtag_shift_spz(spz, 0);
  122. /* 32 bits of 0 terminates the transaction */
  123. jtag_io(32, JIO_TMS, NULL, NULL);
  124. tap_goto_state(TAP_RUN_TEST_IDLE);
  125. /* Check status */
  126. int check_status_loops = 10;
  127. while (1) {
  128. tap_set_ir(JI_CHECK_STATUS, FPGA_IR_LEN);
  129. tap_run_test_idle(5*JTAG_FPGA_US);
  130. tap_goto_state(TAP_SHIFT_DR);
  131. jtag_io(JTAG_FPGA_CHECK_STATUS_BITS, JIO_TMS, NULL, check_status_buf);
  132. tap_goto_state(TAP_RUN_TEST_IDLE);
  133. if (!test_bit(check_status_buf, JTAG_FPGA_CONF_DONE_BIT)) {
  134. check_status_loops--;
  135. MSG("not ready to start... %s\n",
  136. check_status_loops ? "waiting" : "giving up");
  137. if (!check_status_loops) {
  138. err = FWUPDATE_ERR_FPGA_FAILED;
  139. goto fail;
  140. }
  141. jtag_delay(10000); /* 10 ms */
  142. } else {
  143. MSG("ready to start\n");
  144. break;
  145. }
  146. }
  147. /* Go to user mode */
  148. tap_set_ir(JI_STARTUP, FPGA_IR_LEN);
  149. tap_run_test_idle((4096*JTAG_FPGA_MS)/1000+512);
  150. /* Common finish */
  151. fail:
  152. fpga_finish();
  153. return err;
  154. }
  155. int fpga_reset(void)
  156. {
  157. int err = 0;
  158. jtag_enable(&jtag_config_fpga);
  159. tap_run_test_idle(JTAG_FPGA_MS);
  160. /* Make sure to enable loader (not supposed to be needed...) */
  161. tap_set_ir(JI_ACTIVE_ENGAGE, FPGA_IR_LEN);
  162. tap_run_test_idle(16);
  163. /* Pulse nCONFIG via JTAG */
  164. tap_set_ir(JI_PULSE_NCONFIG, FPGA_IR_LEN);
  165. tap_run_test_idle(JTAG_FPGA_MS);
  166. /* Common finish */
  167. fpga_finish();
  168. return err;
  169. }