jtag.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. if (jp->async) {
  30. if (unlikely(tdo))
  31. applog(LOG_WARNING, "jtag_clock: request for tdo in async mode not possible");
  32. return true;
  33. }
  34. if (ft232r_read_all(jp->ftdi, buf, sizeof(buf)) != sizeof(buf))
  35. return false;
  36. if (tdo)
  37. *tdo = (buf[2] & jp->tdo);
  38. *jp->state = buf[2];
  39. //applog(LOG_ERR, "%p tms=%d tdi=%d tdo=%d", jp, (int)tms, (int)tdi, (int)(bool)(buf[3]&jp->tdo));
  40. return true;
  41. }
  42. static bool jtag_rw_bit(struct jtag_port *jp, void *buf, uint8_t mask, bool tms, bool do_read)
  43. {
  44. uint8_t *byte = buf;
  45. bool tdo;
  46. if (!jtag_clock(jp, tms, byte[0] & mask, do_read ? &tdo : NULL))
  47. return false;
  48. if (do_read) {
  49. if (tdo)
  50. byte[0] |= mask;
  51. else
  52. byte[0] &= ~mask;
  53. }
  54. return true;
  55. }
  56. // Expects to start at the Capture step, to handle 0-length gracefully
  57. bool _jtag_llrw(struct jtag_port *jp, void *buf, size_t bitlength, bool do_read, int stage)
  58. {
  59. uint8_t *data = buf;
  60. int i, j;
  61. div_t d;
  62. if (!bitlength)
  63. return jtag_clock(jp, true, false, NULL);
  64. if (stage & 1)
  65. if (!jtag_clock(jp, false, false, NULL))
  66. return false;
  67. d = div(bitlength - 1, 8);
  68. for (i = 0; i < d.quot; ++i) {
  69. for (j = 0x80; j; j /= 2) {
  70. if (!jtag_rw_bit(jp, &data[i], j, false, do_read))
  71. return false;
  72. }
  73. }
  74. for (j = 0; j < d.rem; ++j)
  75. if (!jtag_rw_bit(jp, &data[i], 0x80 >> j, false, do_read))
  76. return false;
  77. if (stage & 2) {
  78. if (!jtag_rw_bit(jp, &data[i], 0x80 >> j, true, do_read))
  79. return false;
  80. if (!jtag_clock(jp, true, false, NULL)) // Update
  81. return false;
  82. }
  83. else
  84. if (!jtag_rw_bit(jp, &data[i], 0x80 >> j, false, do_read))
  85. return false;
  86. return true;
  87. }
  88. bool jtag_reset(struct jtag_port *jp)
  89. {
  90. for (int i = 0; i < 5; ++i)
  91. if (!jtag_clock(jp, true, false, NULL))
  92. return false;
  93. return jtag_clock(jp, false, false, NULL);
  94. }
  95. // Returns -1 for failure, -2 for unknown, or zero and higher for number of devices
  96. ssize_t jtag_detect(struct jtag_port *jp)
  97. {
  98. // TODO: detect more than 1 device
  99. int i;
  100. bool tdo;
  101. if (!(1
  102. && jtag_write(jp, JTAG_REG_IR, "\xff", 8)
  103. && jtag_clock(jp, true , false, NULL) // Select DR
  104. && jtag_clock(jp, false, false, NULL) // Capture DR
  105. && jtag_clock(jp, false, false, NULL) // Shift DR
  106. ))
  107. return -1;
  108. for (i = 0; i < 4; ++i)
  109. if (!jtag_clock(jp, false, false, NULL))
  110. return -1;
  111. if (!jtag_clock(jp, false, false, &tdo))
  112. return -1;
  113. if (tdo)
  114. return -1;
  115. for (i = 0; i < 4; ++i)
  116. {
  117. if (!jtag_clock(jp, false, true, &tdo))
  118. return -1;
  119. if (tdo)
  120. break;
  121. }
  122. if (!jtag_reset(jp))
  123. return -1;
  124. return i < 2 ? i : -2;
  125. }
  126. bool _jtag_rw(struct jtag_port *jp, enum jtagreg r, void *buf, size_t bitlength, bool do_read, int stage)
  127. {
  128. if (!jtag_clock(jp, true, false, NULL)) // Select DR
  129. return false;
  130. if (r == JTAG_REG_IR)
  131. if (!jtag_clock(jp, true, false, NULL)) // Select IR
  132. return false;
  133. if (!jtag_clock(jp, false, false, NULL)) // Capture
  134. return false;
  135. return _jtag_llrw(jp, buf, bitlength, do_read, stage); // Exit1
  136. }
  137. bool jtag_run(struct jtag_port *jp)
  138. {
  139. return jtag_clock(jp, false, false, NULL);
  140. }