You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

316 lines
8.6 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2007 by Pavel Chromy *
  3. * chromy@asix.cz *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  17. ***************************************************************************/
  18. #ifdef HAVE_CONFIG_H
  19. #include "config.h"
  20. #endif
  21. #include <jtag/jtag.h>
  22. #include "bitq.h"
  23. #include <jtag/interface.h>
  24. struct bitq_interface *bitq_interface; /* low level bit queue interface */
  25. /* state of input queue */
  26. struct bitq_state {
  27. struct jtag_command *cmd; /* command currently processed */
  28. int field_idx; /* index of field currently being processed */
  29. int bit_pos; /* position of bit currently being processed */
  30. int status; /* processing status */
  31. };
  32. static struct bitq_state bitq_in_state;
  33. /*
  34. * input queue processing does not use jtag_read_buffer() to avoid unnecessary overhead
  35. * no parameters, makes use of stored state information
  36. */
  37. static void bitq_in_proc(void)
  38. {
  39. /* loop through the queue */
  40. while (bitq_in_state.cmd) {
  41. /* only JTAG_SCAN command may return data */
  42. if (bitq_in_state.cmd->type == JTAG_SCAN) {
  43. /* loop through the fields */
  44. while (bitq_in_state.field_idx < bitq_in_state.cmd->cmd.scan->num_fields) {
  45. struct scan_field *field;
  46. field = &bitq_in_state.cmd->cmd.scan->fields[bitq_in_state.field_idx];
  47. if (field->in_value) {
  48. /* field scanning */
  49. while (bitq_in_state.bit_pos < field->num_bits) {
  50. /* index of byte being scanned */
  51. int in_idx = bitq_in_state.bit_pos / 8;
  52. /* mask of next bit to be scanned */
  53. uint8_t in_mask = 1 << (bitq_in_state.bit_pos % 8);
  54. int tdo = bitq_interface->in();
  55. if (tdo < 0) {
  56. #ifdef _DEBUG_JTAG_IO_
  57. LOG_DEBUG("bitq in EOF");
  58. #endif
  59. return;
  60. }
  61. if (in_mask == 0x01)
  62. field->in_value[in_idx] = 0;
  63. if (tdo)
  64. field->in_value[in_idx] |= in_mask;
  65. bitq_in_state.bit_pos++;
  66. }
  67. }
  68. bitq_in_state.field_idx++; /* advance to next field */
  69. bitq_in_state.bit_pos = 0; /* start next field from the first bit */
  70. }
  71. }
  72. bitq_in_state.cmd = bitq_in_state.cmd->next; /* advance to next command */
  73. bitq_in_state.field_idx = 0; /* preselect first field */
  74. }
  75. }
  76. static void bitq_io(int tms, int tdi, int tdo_req)
  77. {
  78. bitq_interface->out(tms, tdi, tdo_req);
  79. /* check and process the input queue */
  80. if (bitq_interface->in_rdy())
  81. bitq_in_proc();
  82. }
  83. static void bitq_end_state(tap_state_t state)
  84. {
  85. if (!tap_is_state_stable(state)) {
  86. LOG_ERROR("BUG: %i is not a valid end state", state);
  87. exit(-1);
  88. }
  89. tap_set_end_state(state);
  90. }
  91. static void bitq_state_move(tap_state_t new_state)
  92. {
  93. int i = 0;
  94. uint8_t tms_scan;
  95. if (!tap_is_state_stable(tap_get_state()) || !tap_is_state_stable(new_state)) {
  96. LOG_ERROR("TAP move from or to unstable state");
  97. exit(-1);
  98. }
  99. tms_scan = tap_get_tms_path(tap_get_state(), new_state);
  100. int tms_count = tap_get_tms_path_len(tap_get_state(), new_state);
  101. for (i = 0; i < tms_count; i++) {
  102. bitq_io(tms_scan & 1, 0, 0);
  103. tms_scan >>= 1;
  104. }
  105. tap_set_state(new_state);
  106. }
  107. static void bitq_path_move(struct pathmove_command *cmd)
  108. {
  109. int i;
  110. for (i = 0; i <= cmd->num_states; i++) {
  111. if (tap_state_transition(tap_get_state(), false) == cmd->path[i])
  112. bitq_io(0, 0, 0);
  113. else if (tap_state_transition(tap_get_state(), true) == cmd->path[i])
  114. bitq_io(1, 0, 0);
  115. else {
  116. LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(
  117. tap_get_state()), tap_state_name(cmd->path[i]));
  118. exit(-1);
  119. }
  120. tap_set_state(cmd->path[i]);
  121. }
  122. tap_set_end_state(tap_get_state());
  123. }
  124. static void bitq_runtest(int num_cycles)
  125. {
  126. int i;
  127. /* only do a state_move when we're not already in IDLE */
  128. if (tap_get_state() != TAP_IDLE)
  129. bitq_state_move(TAP_IDLE);
  130. /* execute num_cycles */
  131. for (i = 0; i < num_cycles; i++)
  132. bitq_io(0, 0, 0);
  133. /* finish in end_state */
  134. if (tap_get_state() != tap_get_end_state())
  135. bitq_state_move(tap_get_end_state());
  136. }
  137. static void bitq_scan_field(struct scan_field *field, int do_pause)
  138. {
  139. int bit_cnt;
  140. int tdo_req;
  141. const uint8_t *out_ptr;
  142. uint8_t out_mask;
  143. if (field->in_value)
  144. tdo_req = 1;
  145. else
  146. tdo_req = 0;
  147. if (field->out_value == NULL) {
  148. /* just send zeros and request data from TDO */
  149. for (bit_cnt = field->num_bits; bit_cnt > 1; bit_cnt--)
  150. bitq_io(0, 0, tdo_req);
  151. bitq_io(do_pause, 0, tdo_req);
  152. } else {
  153. /* send data, and optionally request TDO */
  154. out_mask = 0x01;
  155. out_ptr = field->out_value;
  156. for (bit_cnt = field->num_bits; bit_cnt > 1; bit_cnt--) {
  157. bitq_io(0, ((*out_ptr) & out_mask) != 0, tdo_req);
  158. if (out_mask == 0x80) {
  159. out_mask = 0x01;
  160. out_ptr++;
  161. } else
  162. out_mask <<= 1;
  163. }
  164. bitq_io(do_pause, ((*out_ptr) & out_mask) != 0, tdo_req);
  165. }
  166. if (do_pause) {
  167. bitq_io(0, 0, 0);
  168. if (tap_get_state() == TAP_IRSHIFT)
  169. tap_set_state(TAP_IRPAUSE);
  170. else if (tap_get_state() == TAP_DRSHIFT)
  171. tap_set_state(TAP_DRPAUSE);
  172. }
  173. }
  174. static void bitq_scan(struct scan_command *cmd)
  175. {
  176. int i;
  177. if (cmd->ir_scan)
  178. bitq_state_move(TAP_IRSHIFT);
  179. else
  180. bitq_state_move(TAP_DRSHIFT);
  181. for (i = 0; i < cmd->num_fields - 1; i++)
  182. bitq_scan_field(&cmd->fields[i], 0);
  183. bitq_scan_field(&cmd->fields[i], 1);
  184. }
  185. int bitq_execute_queue(void)
  186. {
  187. struct jtag_command *cmd = jtag_command_queue; /* currently processed command */
  188. bitq_in_state.cmd = jtag_command_queue;
  189. bitq_in_state.field_idx = 0;
  190. bitq_in_state.bit_pos = 0;
  191. bitq_in_state.status = ERROR_OK;
  192. while (cmd) {
  193. switch (cmd->type) {
  194. case JTAG_RESET:
  195. #ifdef _DEBUG_JTAG_IO_
  196. LOG_DEBUG("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  197. #endif
  198. if ((cmd->cmd.reset->trst == 1) ||
  199. (cmd->cmd.reset->srst &&
  200. (jtag_get_reset_config() & RESET_SRST_PULLS_TRST)))
  201. tap_set_state(TAP_RESET);
  202. bitq_interface->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  203. if (bitq_interface->in_rdy())
  204. bitq_in_proc();
  205. break;
  206. case JTAG_RUNTEST:
  207. #ifdef _DEBUG_JTAG_IO_
  208. LOG_DEBUG("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, cmd->cmd.runtest->end_state);
  209. #endif
  210. bitq_end_state(cmd->cmd.runtest->end_state);
  211. bitq_runtest(cmd->cmd.runtest->num_cycles);
  212. break;
  213. case JTAG_TLR_RESET:
  214. #ifdef _DEBUG_JTAG_IO_
  215. LOG_DEBUG("statemove end in %i", cmd->cmd.statemove->end_state);
  216. #endif
  217. bitq_end_state(cmd->cmd.statemove->end_state);
  218. bitq_state_move(tap_get_end_state()); /* uncoditional TAP move */
  219. break;
  220. case JTAG_PATHMOVE:
  221. #ifdef _DEBUG_JTAG_IO_
  222. LOG_DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states,
  223. cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
  224. #endif
  225. bitq_path_move(cmd->cmd.pathmove);
  226. break;
  227. case JTAG_SCAN:
  228. #ifdef _DEBUG_JTAG_IO_
  229. LOG_DEBUG("scan end in %i", cmd->cmd.scan->end_state);
  230. if (cmd->cmd.scan->ir_scan)
  231. LOG_DEBUG("scan ir");
  232. else
  233. LOG_DEBUG("scan dr");
  234. #endif
  235. bitq_end_state(cmd->cmd.scan->end_state);
  236. bitq_scan(cmd->cmd.scan);
  237. if (tap_get_state() != tap_get_end_state())
  238. bitq_state_move(tap_get_end_state());
  239. break;
  240. case JTAG_SLEEP:
  241. #ifdef _DEBUG_JTAG_IO_
  242. LOG_DEBUG("sleep %i", cmd->cmd.sleep->us);
  243. #endif
  244. bitq_interface->sleep(cmd->cmd.sleep->us);
  245. if (bitq_interface->in_rdy())
  246. bitq_in_proc();
  247. break;
  248. default:
  249. LOG_ERROR("BUG: unknown JTAG command type encountered");
  250. exit(-1);
  251. }
  252. cmd = cmd->next;
  253. }
  254. bitq_interface->flush();
  255. bitq_in_proc();
  256. if (bitq_in_state.cmd) {
  257. LOG_ERROR("missing data from bitq interface");
  258. return ERROR_JTAG_QUEUE_FAILED;
  259. }
  260. if (bitq_interface->in() >= 0) {
  261. LOG_ERROR("extra data from bitq interface");
  262. return ERROR_JTAG_QUEUE_FAILED;
  263. }
  264. return bitq_in_state.status;
  265. }
  266. void bitq_cleanup(void)
  267. {
  268. }