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.
 
 
 
 
 
 

265 lines
7.1 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2005 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  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 "bitbang.h"
  24. /* project specific includes */
  25. #include "log.h"
  26. #include "types.h"
  27. #include "jtag.h"
  28. #include "configuration.h"
  29. /* system includes */
  30. #include <string.h>
  31. #include <stdlib.h>
  32. #include <unistd.h>
  33. #include <sys/time.h>
  34. #include <time.h>
  35. bitbang_interface_t *bitbang_interface;
  36. int bitbang_execute_queue(void);
  37. void bitbang_end_state(enum tap_state state)
  38. {
  39. if (tap_move_map[state] != -1)
  40. end_state = state;
  41. else
  42. {
  43. ERROR("BUG: %i is not a valid end state", state);
  44. exit(-1);
  45. }
  46. }
  47. void bitbang_state_move(void) {
  48. int i=0, tms=0;
  49. u8 tms_scan = TAP_MOVE(cur_state, end_state);
  50. for (i = 0; i < 7; i++)
  51. {
  52. tms = (tms_scan >> i) & 1;
  53. bitbang_interface->write(0, tms, 0);
  54. bitbang_interface->write(1, tms, 0);
  55. }
  56. bitbang_interface->write(0, tms, 0);
  57. cur_state = end_state;
  58. }
  59. void bitbang_path_move(pathmove_command_t *cmd)
  60. {
  61. int num_states = cmd->num_states;
  62. int state_count;
  63. state_count = 0;
  64. while (num_states)
  65. {
  66. if (tap_transitions[cur_state].low == cmd->path[state_count])
  67. {
  68. bitbang_interface->write(0, 0, 0);
  69. bitbang_interface->write(1, 0, 0);
  70. }
  71. else if (tap_transitions[cur_state].high == cmd->path[state_count])
  72. {
  73. bitbang_interface->write(0, 1, 0);
  74. bitbang_interface->write(1, 1, 0);
  75. }
  76. else
  77. {
  78. ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[cmd->path[state_count]]);
  79. exit(-1);
  80. }
  81. cur_state = cmd->path[state_count];
  82. state_count++;
  83. num_states--;
  84. }
  85. end_state = cur_state;
  86. }
  87. void bitbang_runtest(int num_cycles)
  88. {
  89. int i;
  90. enum tap_state saved_end_state = end_state;
  91. /* only do a state_move when we're not already in RTI */
  92. if (cur_state != TAP_RTI)
  93. {
  94. bitbang_end_state(TAP_RTI);
  95. bitbang_state_move();
  96. }
  97. /* execute num_cycles */
  98. bitbang_interface->write(0, 0, 0);
  99. for (i = 0; i < num_cycles; i++)
  100. {
  101. bitbang_interface->write(1, 0, 0);
  102. bitbang_interface->write(0, 0, 0);
  103. }
  104. /* finish in end_state */
  105. bitbang_end_state(saved_end_state);
  106. if (cur_state != end_state)
  107. bitbang_state_move();
  108. }
  109. void bitbang_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
  110. {
  111. enum tap_state saved_end_state = end_state;
  112. int bit_cnt;
  113. if (ir_scan)
  114. bitbang_end_state(TAP_SI);
  115. else
  116. bitbang_end_state(TAP_SD);
  117. bitbang_state_move();
  118. bitbang_end_state(saved_end_state);
  119. for (bit_cnt = 0; bit_cnt < scan_size; bit_cnt++)
  120. {
  121. /* if we're just reading the scan, but don't care about the output
  122. * default to outputting 'low', this also makes valgrind traces more readable,
  123. * as it removes the dependency on an uninitialised value
  124. */
  125. if ((type != SCAN_IN) && ((buffer[bit_cnt/8] >> (bit_cnt % 8)) & 0x1)) {
  126. bitbang_interface->write(0, (bit_cnt==scan_size-1) ? 1 : 0, 1);
  127. bitbang_interface->write(1, (bit_cnt==scan_size-1) ? 1 : 0, 1);
  128. } else {
  129. bitbang_interface->write(0, (bit_cnt==scan_size-1) ? 1 : 0, 0);
  130. bitbang_interface->write(1, (bit_cnt==scan_size-1) ? 1 : 0, 0);
  131. }
  132. if (type != SCAN_OUT)
  133. {
  134. if (bitbang_interface->read())
  135. buffer[(bit_cnt)/8] |= 1 << ((bit_cnt) % 8);
  136. else
  137. buffer[(bit_cnt)/8] &= ~(1 << ((bit_cnt) % 8));
  138. }
  139. }
  140. /* Exit1 -> Pause */
  141. bitbang_interface->write(0, 0, 0);
  142. bitbang_interface->write(1, 0, 0);
  143. if (ir_scan)
  144. cur_state = TAP_PI;
  145. else
  146. cur_state = TAP_PD;
  147. if (cur_state != end_state)
  148. bitbang_state_move();
  149. }
  150. int bitbang_execute_queue(void)
  151. {
  152. jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
  153. int scan_size;
  154. enum scan_type type;
  155. u8 *buffer;
  156. if (!bitbang_interface)
  157. {
  158. ERROR("BUG: Bitbang interface called, but not yet initialized");
  159. exit(-1);
  160. }
  161. while (cmd)
  162. {
  163. switch (cmd->type)
  164. {
  165. case JTAG_END_STATE:
  166. #ifdef _DEBUG_JTAG_IO_
  167. DEBUG("end_state: %i", cmd->cmd.end_state->end_state);
  168. #endif
  169. if (cmd->cmd.end_state->end_state != -1)
  170. bitbang_end_state(cmd->cmd.end_state->end_state);
  171. break;
  172. case JTAG_RESET:
  173. #ifdef _DEBUG_JTAG_IO_
  174. DEBUG("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  175. #endif
  176. if (cmd->cmd.reset->trst == 1)
  177. {
  178. cur_state = TAP_TLR;
  179. }
  180. bitbang_interface->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  181. break;
  182. case JTAG_RUNTEST:
  183. #ifdef _DEBUG_JTAG_IO_
  184. DEBUG("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, cmd->cmd.runtest->end_state);
  185. #endif
  186. if (cmd->cmd.runtest->end_state != -1)
  187. bitbang_end_state(cmd->cmd.runtest->end_state);
  188. bitbang_runtest(cmd->cmd.runtest->num_cycles);
  189. break;
  190. case JTAG_STATEMOVE:
  191. #ifdef _DEBUG_JTAG_IO_
  192. DEBUG("statemove end in %i", cmd->cmd.statemove->end_state);
  193. #endif
  194. if (cmd->cmd.statemove->end_state != -1)
  195. bitbang_end_state(cmd->cmd.statemove->end_state);
  196. bitbang_state_move();
  197. break;
  198. case JTAG_PATHMOVE:
  199. #ifdef _DEBUG_JTAG_IO_
  200. DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
  201. #endif
  202. bitbang_path_move(cmd->cmd.pathmove);
  203. break;
  204. case JTAG_SCAN:
  205. #ifdef _DEBUG_JTAG_IO_
  206. DEBUG("scan end in %i", cmd->cmd.scan->end_state);
  207. #endif
  208. if (cmd->cmd.scan->end_state != -1)
  209. bitbang_end_state(cmd->cmd.scan->end_state);
  210. scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
  211. type = jtag_scan_type(cmd->cmd.scan);
  212. bitbang_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
  213. if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
  214. return ERROR_JTAG_QUEUE_FAILED;
  215. if (buffer)
  216. free(buffer);
  217. break;
  218. case JTAG_SLEEP:
  219. #ifdef _DEBUG_JTAG_IO_
  220. DEBUG("sleep %i", cmd->cmd.sleep->us);
  221. #endif
  222. jtag_sleep(cmd->cmd.sleep->us);
  223. break;
  224. default:
  225. ERROR("BUG: unknown JTAG command type encountered");
  226. exit(-1);
  227. }
  228. cmd = cmd->next;
  229. }
  230. return ERROR_OK;
  231. }