jtag.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. #define FTDI_READ_BUFFER_SIZE 100
  20. static
  21. unsigned char jtag_clock_byte(struct jtag_port *jp, bool tms, bool tdi)
  22. {
  23. return (jp->a->state & jp->ignored)
  24. | (tms ? jp->tms : 0)
  25. | (tdi ? jp->tdi : 0);
  26. }
  27. // NOTE: The order of tms and tdi here are inverted from LPC1343CodeBase
  28. bool jtag_clock(struct jtag_port *jp, bool tms, bool tdi, bool *tdo)
  29. {
  30. unsigned char bufsz = tdo ? 3 : 2;
  31. unsigned char buf[3];
  32. memset(buf, jtag_clock_byte(jp, tms, tdi), sizeof(buf));
  33. buf[2] =
  34. buf[1] |= jp->tck;
  35. if (ft232r_write_all(jp->a->ftdi, buf, bufsz) != bufsz)
  36. return false;
  37. jp->a->state = buf[2];
  38. if (jp->a->async) {
  39. if (unlikely(tdo))
  40. applog(LOG_WARNING, "jtag_clock: request for tdo in async mode not possible");
  41. #ifdef DEBUG_JTAG_CLOCK
  42. applog(LOG_DEBUG, "%p %02x tms=%d tdi=%d tdo=?async", jp, (unsigned)buf[2], (int)tms, (int)tdi);
  43. #endif
  44. return true;
  45. }
  46. jp->a->bufread += bufsz;
  47. if (jp->a->bufread < FTDI_READ_BUFFER_SIZE - sizeof(buf) && !tdo) {
  48. // By deferring unnecessary reads, we can avoid some USB latency
  49. #ifdef DEBUG_JTAG_CLOCK
  50. applog(LOG_DEBUG, "%p %02x tms=%d tdi=%d tdo=?defer", jp, (unsigned)buf[2], (int)tms, (int)tdi);
  51. #endif
  52. return true;
  53. }
  54. #if 0 /* untested */
  55. else if (!tdo) {
  56. if (ft232r_purge_buffers(jp->a->ftdi, FTDI_PURGE_BOTH)) {
  57. jp->bufread = 0;
  58. #ifdef DEBUG_JTAG_CLOCK
  59. applog(LOG_DEBUG, "%p %02x tms=%d tdi=%d tdo=?purge", jp, (unsigned)buf[2], (int)tms, (int)tdi);
  60. #endif
  61. return true;
  62. }
  63. }
  64. #endif
  65. uint8_t rbufsz = jp->a->bufread;
  66. jp->a->bufread = 0;
  67. unsigned char rbuf[rbufsz];
  68. if (ft232r_read_all(jp->a->ftdi, rbuf, rbufsz) != rbufsz)
  69. return false;
  70. if (tdo) {
  71. *tdo = (rbuf[rbufsz-1] & jp->tdo);
  72. #ifdef DEBUG_JTAG_CLOCK
  73. char *x = bin2hex(rbuf, rbufsz);
  74. 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);
  75. free(x);
  76. } else {
  77. applog(LOG_DEBUG, "%p %02x tms=%d tdi=%d tdo=?ignore", jp, (unsigned)buf[2], (int)tms, (int)tdi);
  78. #endif
  79. }
  80. return true;
  81. }
  82. static bool jtag_rw_bit(struct jtag_port *jp, void *buf, uint8_t mask, bool tms, bool do_read)
  83. {
  84. uint8_t *byte = buf;
  85. bool tdo;
  86. if (!jtag_clock(jp, tms, byte[0] & mask, do_read ? &tdo : NULL))
  87. return false;
  88. if (do_read) {
  89. if (tdo)
  90. byte[0] |= mask;
  91. else
  92. byte[0] &= ~mask;
  93. }
  94. return true;
  95. }
  96. static inline
  97. bool getbit(void *data, uint32_t bitnum)
  98. {
  99. unsigned char *cdata = data;
  100. div_t d = div(bitnum, 8);
  101. unsigned char b = cdata[d.quot];
  102. return b & (1<<(7 - d.rem));
  103. }
  104. static inline
  105. void setbit(void *data, uint32_t bitnum, bool nv)
  106. {
  107. unsigned char *cdata = data;
  108. div_t d = div(bitnum, 8);
  109. unsigned char *p = &cdata[d.quot];
  110. unsigned char o = (1<<(7 - d.rem));
  111. if (nv)
  112. *p |= o;
  113. else
  114. *p &= ~o;
  115. }
  116. // Expects to start at the Capture step, to handle 0-length gracefully
  117. bool _jtag_llrw(struct jtag_port *jp, void *buf, size_t bitlength, bool do_read, int stage)
  118. {
  119. uint8_t *data = buf;
  120. if (!bitlength)
  121. return jtag_clock(jp, true, false, NULL);
  122. if (stage & 1)
  123. if (!jtag_clock(jp, false, false, NULL))
  124. return false;
  125. #ifndef DEBUG_JTAG_CLOCK
  126. // This alternate implementation is designed to minimize ft232r reads (which are slow)
  127. if (do_read) {
  128. unsigned char rbuf[FTDI_READ_BUFFER_SIZE];
  129. unsigned char wbuf[3];
  130. ssize_t rbufsz, bitspending = 0;
  131. size_t databitoff = 0, i;
  132. --bitlength;
  133. for (i = 0; i < bitlength; ++i) {
  134. wbuf[0] = jtag_clock_byte(jp, false, getbit(data, i));
  135. wbuf[1] = wbuf[0] | jp->tck;
  136. if (ft232r_write_all(jp->a->ftdi, wbuf, 2) != 2)
  137. return false;
  138. jp->a->bufread += 2;
  139. ++bitspending;
  140. if (jp->a->bufread > FTDI_READ_BUFFER_SIZE - 2) {
  141. // The next bit would overflow, so read now
  142. rbufsz = jp->a->bufread;
  143. if (ft232r_read_all(jp->a->ftdi, rbuf, rbufsz) != rbufsz)
  144. return false;
  145. for (ssize_t j = rbufsz - ((bitspending - 1) * 2); j < rbufsz; j += 2)
  146. setbit(data, databitoff++, (rbuf[j] & jp->tdo));
  147. bitspending = 1;
  148. jp->a->bufread = 0;
  149. }
  150. }
  151. // Last bit needs special treatment
  152. wbuf[0] = jtag_clock_byte(jp, (stage & 2), getbit(data, i));
  153. wbuf[2] = wbuf[1] = wbuf[0] | jp->tck;
  154. if (ft232r_write_all(jp->a->ftdi, wbuf, sizeof(wbuf)) != sizeof(wbuf))
  155. return false;
  156. rbufsz = jp->a->bufread + 3;
  157. if (ft232r_read_all(jp->a->ftdi, rbuf, rbufsz) != rbufsz)
  158. return false;
  159. for (ssize_t j = rbufsz - 1 - (bitspending * 2); j < rbufsz; j += 2)
  160. setbit(data, databitoff++, (rbuf[j] & jp->tdo));
  161. setbit(data, databitoff++, (rbuf[rbufsz - 1] & jp->tdo));
  162. jp->a->bufread = 0;
  163. if (stage & 2) {
  164. if (!jtag_clock(jp, true, false, NULL)) // Update
  165. return false;
  166. }
  167. return true;
  168. }
  169. #endif
  170. int i, j;
  171. div_t d;
  172. d = div(bitlength - 1, 8);
  173. for (i = 0; i < d.quot; ++i) {
  174. for (j = 0x80; j; j /= 2) {
  175. if (!jtag_rw_bit(jp, &data[i], j, false, do_read))
  176. return false;
  177. }
  178. }
  179. for (j = 0; j < d.rem; ++j)
  180. if (!jtag_rw_bit(jp, &data[i], 0x80 >> j, false, do_read))
  181. return false;
  182. if (stage & 2) {
  183. if (!jtag_rw_bit(jp, &data[i], 0x80 >> j, true, do_read))
  184. return false;
  185. if (!jtag_clock(jp, true, false, NULL)) // Update
  186. return false;
  187. }
  188. else
  189. if (!jtag_rw_bit(jp, &data[i], 0x80 >> j, false, do_read))
  190. return false;
  191. return true;
  192. }
  193. bool jtag_reset(struct jtag_port *jp)
  194. {
  195. for (int i = 0; i < 5; ++i)
  196. if (!jtag_clock(jp, true, false, NULL))
  197. return false;
  198. return jtag_clock(jp, false, false, NULL);
  199. }
  200. // Returns -1 for failure, -2 for unknown, or zero and higher for number of devices
  201. ssize_t jtag_detect(struct jtag_port *jp)
  202. {
  203. // TODO: detect more than 1 device
  204. int i;
  205. bool tdo;
  206. if (!(1
  207. && jtag_write(jp, JTAG_REG_IR, "\xff", 8)
  208. && jtag_clock(jp, true , false, NULL) // Select DR
  209. && jtag_clock(jp, false, false, NULL) // Capture DR
  210. && jtag_clock(jp, false, false, NULL) // Shift DR
  211. ))
  212. return -1;
  213. for (i = 0; i < 4; ++i)
  214. if (!jtag_clock(jp, false, false, NULL))
  215. return -1;
  216. if (!jtag_clock(jp, false, false, &tdo))
  217. return -1;
  218. if (tdo)
  219. return -1;
  220. for (i = 0; i < 4; ++i)
  221. {
  222. if (!jtag_clock(jp, false, true, &tdo))
  223. return -1;
  224. if (tdo)
  225. break;
  226. }
  227. if (!jtag_reset(jp))
  228. return -1;
  229. return i < 2 ? i : -2;
  230. }
  231. bool _jtag_rw(struct jtag_port *jp, enum jtagreg r, void *buf, size_t bitlength, bool do_read, int stage)
  232. {
  233. if (!jtag_clock(jp, true, false, NULL)) // Select DR
  234. return false;
  235. if (r == JTAG_REG_IR)
  236. if (!jtag_clock(jp, true, false, NULL)) // Select IR
  237. return false;
  238. if (!jtag_clock(jp, false, false, NULL)) // Capture
  239. return false;
  240. return _jtag_llrw(jp, buf, bitlength, do_read, stage); // Exit1
  241. }
  242. bool jtag_run(struct jtag_port *jp)
  243. {
  244. return jtag_clock(jp, false, false, NULL);
  245. }