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.
 
 
 
 
 
 

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