jtag.c 7.1 KB

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