jtag.c 7.0 KB

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