jtag.c 7.2 KB

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