jtag.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright 2012 Luke Dashjr
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 3 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. */
  9. // NOTE: This code is based on code Luke-Jr wrote originally for LPC1343CodeBase
  10. #include <stdbool.h>
  11. #include <stdint.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include "ft232r.h"
  15. #include "jtag.h"
  16. #include "logging.h"
  17. #include "miner.h"
  18. //#define DEBUG_JTAG_CLOCK
  19. // NOTE: The order of tms and tdi here are inverted from LPC1343CodeBase
  20. bool jtag_clock(struct jtag_port *jp, bool tms, bool tdi, bool *tdo)
  21. {
  22. unsigned char buf[3];
  23. memset(buf, (jp->a->state & jp->ignored)
  24. | (tms ? jp->tms : 0)
  25. | (tdi ? jp->tdi : 0), sizeof(buf));
  26. buf[2] =
  27. buf[1] |= jp->tck;
  28. if (ft232r_write_all(jp->a->ftdi, buf, sizeof(buf)) != sizeof(buf))
  29. return false;
  30. jp->a->state = buf[2];
  31. if (jp->a->async) {
  32. if (unlikely(tdo))
  33. applog(LOG_WARNING, "jtag_clock: request for tdo in async mode not possible");
  34. #ifdef DEBUG_JTAG_CLOCK
  35. applog(LOG_DEBUG, "%p %02x tms=%d tdi=%d tdo=?async", jp, (unsigned)buf[2], (int)tms, (int)tdi);
  36. #endif
  37. return true;
  38. }
  39. if (jp->a->bufread < 100 && !tdo) {
  40. // By deferring unnecessary reads, we can avoid some USB latency
  41. jp->a->bufread += sizeof(buf);
  42. #ifdef DEBUG_JTAG_CLOCK
  43. applog(LOG_DEBUG, "%p %02x tms=%d tdi=%d tdo=?defer", jp, (unsigned)buf[2], (int)tms, (int)tdi);
  44. #endif
  45. return true;
  46. }
  47. #if 0 /* untested */
  48. else if (!tdo) {
  49. if (ft232r_purge_buffers(jp->a->ftdi, FTDI_PURGE_BOTH)) {
  50. jp->bufread = 0;
  51. #ifdef DEBUG_JTAG_CLOCK
  52. applog(LOG_DEBUG, "%p %02x tms=%d tdi=%d tdo=?purge", jp, (unsigned)buf[2], (int)tms, (int)tdi);
  53. #endif
  54. return true;
  55. }
  56. }
  57. #endif
  58. uint8_t rbufsz = jp->a->bufread + sizeof(buf);
  59. jp->a->bufread = 0;
  60. unsigned char rbuf[rbufsz];
  61. if (ft232r_read_all(jp->a->ftdi, rbuf, rbufsz) != rbufsz)
  62. return false;
  63. if (tdo)
  64. *tdo = (rbuf[rbufsz-1] & jp->tdo);
  65. #ifdef DEBUG_JTAG_CLOCK
  66. char *x = bin2hex(rbuf, rbufsz);
  67. applog(LOG_DEBUG, "%p %02x tms=%d tdi=%d tdo=%d (%u:%s)", jp, (unsigned)rbuf[rbufsz-1], (int)tms, (int)tdi, (int)(bool)(rbuf[rbufsz-1] & jp->tdo), (unsigned)rbufsz, x);
  68. free(x);
  69. #endif
  70. return true;
  71. }
  72. static bool jtag_rw_bit(struct jtag_port *jp, void *buf, uint8_t mask, bool tms, bool do_read)
  73. {
  74. uint8_t *byte = buf;
  75. bool tdo;
  76. if (!jtag_clock(jp, tms, byte[0] & mask, do_read ? &tdo : NULL))
  77. return false;
  78. if (do_read) {
  79. if (tdo)
  80. byte[0] |= mask;
  81. else
  82. byte[0] &= ~mask;
  83. }
  84. return true;
  85. }
  86. // Expects to start at the Capture step, to handle 0-length gracefully
  87. bool _jtag_llrw(struct jtag_port *jp, void *buf, size_t bitlength, bool do_read, int stage)
  88. {
  89. uint8_t *data = buf;
  90. int i, j;
  91. div_t d;
  92. if (!bitlength)
  93. return jtag_clock(jp, true, false, NULL);
  94. if (stage & 1)
  95. if (!jtag_clock(jp, false, false, NULL))
  96. return false;
  97. d = div(bitlength - 1, 8);
  98. for (i = 0; i < d.quot; ++i) {
  99. for (j = 0x80; j; j /= 2) {
  100. if (!jtag_rw_bit(jp, &data[i], j, false, do_read))
  101. return false;
  102. }
  103. }
  104. for (j = 0; j < d.rem; ++j)
  105. if (!jtag_rw_bit(jp, &data[i], 0x80 >> j, false, do_read))
  106. return false;
  107. if (stage & 2) {
  108. if (!jtag_rw_bit(jp, &data[i], 0x80 >> j, true, do_read))
  109. return false;
  110. if (!jtag_clock(jp, true, false, NULL)) // Update
  111. return false;
  112. }
  113. else
  114. if (!jtag_rw_bit(jp, &data[i], 0x80 >> j, false, do_read))
  115. return false;
  116. return true;
  117. }
  118. bool jtag_reset(struct jtag_port *jp)
  119. {
  120. for (int i = 0; i < 5; ++i)
  121. if (!jtag_clock(jp, true, false, NULL))
  122. return false;
  123. return jtag_clock(jp, false, false, NULL);
  124. }
  125. // Returns -1 for failure, -2 for unknown, or zero and higher for number of devices
  126. ssize_t jtag_detect(struct jtag_port *jp)
  127. {
  128. // TODO: detect more than 1 device
  129. int i;
  130. bool tdo;
  131. if (!(1
  132. && jtag_write(jp, JTAG_REG_IR, "\xff", 8)
  133. && jtag_clock(jp, true , false, NULL) // Select DR
  134. && jtag_clock(jp, false, false, NULL) // Capture DR
  135. && jtag_clock(jp, false, false, NULL) // Shift DR
  136. ))
  137. return -1;
  138. for (i = 0; i < 4; ++i)
  139. if (!jtag_clock(jp, false, false, NULL))
  140. return -1;
  141. if (!jtag_clock(jp, false, false, &tdo))
  142. return -1;
  143. if (tdo)
  144. return -1;
  145. for (i = 0; i < 4; ++i)
  146. {
  147. if (!jtag_clock(jp, false, true, &tdo))
  148. return -1;
  149. if (tdo)
  150. break;
  151. }
  152. if (!jtag_reset(jp))
  153. return -1;
  154. return i < 2 ? i : -2;
  155. }
  156. bool _jtag_rw(struct jtag_port *jp, enum jtagreg r, void *buf, size_t bitlength, bool do_read, int stage)
  157. {
  158. if (!jtag_clock(jp, true, false, NULL)) // Select DR
  159. return false;
  160. if (r == JTAG_REG_IR)
  161. if (!jtag_clock(jp, true, false, NULL)) // Select IR
  162. return false;
  163. if (!jtag_clock(jp, false, false, NULL)) // Capture
  164. return false;
  165. return _jtag_llrw(jp, buf, bitlength, do_read, stage); // Exit1
  166. }
  167. bool jtag_run(struct jtag_port *jp)
  168. {
  169. return jtag_clock(jp, false, false, NULL);
  170. }