jtag.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. // NOTE: The order of tms and tdi here are inverted from LPC1343CodeBase
  19. bool jtag_clock(struct jtag_port *jp, bool tms, bool tdi, bool *tdo)
  20. {
  21. unsigned char buf[3];
  22. memset(buf, (*jp->state & jp->ignored)
  23. | (tms ? jp->tms : 0)
  24. | (tdi ? jp->tdi : 0), sizeof(buf));
  25. buf[2] =
  26. buf[1] |= jp->tck;
  27. if (ft232r_write_all(jp->ftdi, buf, sizeof(buf)) != sizeof(buf))
  28. return false;
  29. *jp->state = buf[2];
  30. if (jp->async) {
  31. if (unlikely(tdo))
  32. applog(LOG_WARNING, "jtag_clock: request for tdo in async mode not possible");
  33. return true;
  34. }
  35. if (jp->bufread < 100 && !tdo) {
  36. // By deferring unnecessary reads, we can avoid some USB latency
  37. jp->bufread += sizeof(buf);
  38. return true;
  39. }
  40. #if 0 /* untested */
  41. else if (!tdo) {
  42. if (ft232r_purge_buffers(jp->ftdi, FTDI_PURGE_BOTH)) {
  43. jp->bufread = 0;
  44. return true;
  45. }
  46. }
  47. #endif
  48. uint8_t rbufsz = jp->bufread + sizeof(buf);
  49. jp->bufread = 0;
  50. unsigned char rbuf[rbufsz];
  51. if (ft232r_read_all(jp->ftdi, rbuf, rbufsz) != rbufsz)
  52. return false;
  53. if (tdo)
  54. *tdo = (rbuf[rbufsz-1] & jp->tdo);
  55. //applog(LOG_ERR, "%p tms=%d tdi=%d tdo=%d", jp, (int)tms, (int)tdi, (int)(bool)(buf[2]&jp->tdo));
  56. return true;
  57. }
  58. static bool jtag_rw_bit(struct jtag_port *jp, void *buf, uint8_t mask, bool tms, bool do_read)
  59. {
  60. uint8_t *byte = buf;
  61. bool tdo;
  62. if (!jtag_clock(jp, tms, byte[0] & mask, do_read ? &tdo : NULL))
  63. return false;
  64. if (do_read) {
  65. if (tdo)
  66. byte[0] |= mask;
  67. else
  68. byte[0] &= ~mask;
  69. }
  70. return true;
  71. }
  72. // Expects to start at the Capture step, to handle 0-length gracefully
  73. bool _jtag_llrw(struct jtag_port *jp, void *buf, size_t bitlength, bool do_read, int stage)
  74. {
  75. uint8_t *data = buf;
  76. int i, j;
  77. div_t d;
  78. if (!bitlength)
  79. return jtag_clock(jp, true, false, NULL);
  80. if (stage & 1)
  81. if (!jtag_clock(jp, false, false, NULL))
  82. return false;
  83. d = div(bitlength - 1, 8);
  84. for (i = 0; i < d.quot; ++i) {
  85. for (j = 0x80; j; j /= 2) {
  86. if (!jtag_rw_bit(jp, &data[i], j, false, do_read))
  87. return false;
  88. }
  89. }
  90. for (j = 0; j < d.rem; ++j)
  91. if (!jtag_rw_bit(jp, &data[i], 0x80 >> j, false, do_read))
  92. return false;
  93. if (stage & 2) {
  94. if (!jtag_rw_bit(jp, &data[i], 0x80 >> j, true, do_read))
  95. return false;
  96. if (!jtag_clock(jp, true, false, NULL)) // Update
  97. return false;
  98. }
  99. else
  100. if (!jtag_rw_bit(jp, &data[i], 0x80 >> j, false, do_read))
  101. return false;
  102. return true;
  103. }
  104. bool jtag_reset(struct jtag_port *jp)
  105. {
  106. for (int i = 0; i < 5; ++i)
  107. if (!jtag_clock(jp, true, false, NULL))
  108. return false;
  109. return jtag_clock(jp, false, false, NULL);
  110. }
  111. // Returns -1 for failure, -2 for unknown, or zero and higher for number of devices
  112. ssize_t jtag_detect(struct jtag_port *jp)
  113. {
  114. // TODO: detect more than 1 device
  115. int i;
  116. bool tdo;
  117. if (!(1
  118. && jtag_write(jp, JTAG_REG_IR, "\xff", 8)
  119. && jtag_clock(jp, true , false, NULL) // Select DR
  120. && jtag_clock(jp, false, false, NULL) // Capture DR
  121. && jtag_clock(jp, false, false, NULL) // Shift DR
  122. ))
  123. return -1;
  124. for (i = 0; i < 4; ++i)
  125. if (!jtag_clock(jp, false, false, NULL))
  126. return -1;
  127. if (!jtag_clock(jp, false, false, &tdo))
  128. return -1;
  129. if (tdo)
  130. return -1;
  131. for (i = 0; i < 4; ++i)
  132. {
  133. if (!jtag_clock(jp, false, true, &tdo))
  134. return -1;
  135. if (tdo)
  136. break;
  137. }
  138. if (!jtag_reset(jp))
  139. return -1;
  140. return i < 2 ? i : -2;
  141. }
  142. bool _jtag_rw(struct jtag_port *jp, enum jtagreg r, void *buf, size_t bitlength, bool do_read, int stage)
  143. {
  144. if (!jtag_clock(jp, true, false, NULL)) // Select DR
  145. return false;
  146. if (r == JTAG_REG_IR)
  147. if (!jtag_clock(jp, true, false, NULL)) // Select IR
  148. return false;
  149. if (!jtag_clock(jp, false, false, NULL)) // Capture
  150. return false;
  151. return _jtag_llrw(jp, buf, bitlength, do_read, stage); // Exit1
  152. }
  153. bool jtag_run(struct jtag_port *jp)
  154. {
  155. return jtag_clock(jp, false, false, NULL);
  156. }