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.
 
 
 
 
 
 

320 lines
9.0 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2005 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * Copyright (C) 2007,2008 Øyvind Harboe *
  6. * oyvind.harboe@zylin.com *
  7. * *
  8. * This program is free software; you can redistribute it and/or modify *
  9. * it under the terms of the GNU General Public License as published by *
  10. * the Free Software Foundation; either version 2 of the License, or *
  11. * (at your option) any later version. *
  12. * *
  13. * This program is distributed in the hope that it will be useful, *
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  16. * GNU General Public License for more details. *
  17. * *
  18. * You should have received a copy of the GNU General Public License *
  19. * along with this program; if not, write to the *
  20. * Free Software Foundation, Inc., *
  21. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  22. ***************************************************************************/
  23. #ifdef HAVE_CONFIG_H
  24. #include "config.h"
  25. #endif
  26. #include "bitbang.h"
  27. /* project specific includes */
  28. #include "log.h"
  29. #include "types.h"
  30. #include "jtag.h"
  31. #include "configuration.h"
  32. /* system includes */
  33. #include <string.h>
  34. #include <stdlib.h>
  35. #include <unistd.h>
  36. bitbang_interface_t *bitbang_interface;
  37. /* DANGER!!!! clock absolutely *MUST* be 0 in idle or reset won't work!
  38. *
  39. * Set this to 1 and str912 reset halt will fail.
  40. *
  41. * If someone can submit a patch with an explanation it will be greatly
  42. * appreciated, but as far as I can tell (ØH) DCLK is generated upon
  43. * clk=0 in TAP_IDLE. Good luck deducing that from the ARM documentation!
  44. * The ARM documentation uses the term "DCLK is asserted while in the TAP_IDLE
  45. * state". With hardware there is no such thing as *while* in a state. There
  46. * are only edges. So clk => 0 is in fact a very subtle state transition that
  47. * happens *while* in the TAP_IDLE state. "#&¤"#¤&"#&"#&
  48. *
  49. * For "reset halt" the last thing that happens before srst is asserted
  50. * is that the breakpoint is set up. If DCLK is not wiggled one last
  51. * time before the reset, then the breakpoint is not set up and
  52. * "reset halt" will fail to halt.
  53. *
  54. */
  55. #define CLOCK_IDLE() 0
  56. int bitbang_execute_queue(void);
  57. /* The bitbang driver leaves the TCK 0 when in idle */
  58. void bitbang_end_state(enum tap_state state)
  59. {
  60. if (tap_move_map[state] != -1)
  61. end_state = state;
  62. else
  63. {
  64. LOG_ERROR("BUG: %i is not a valid end state", state);
  65. exit(-1);
  66. }
  67. }
  68. void bitbang_state_move(void) {
  69. int i=0, tms=0;
  70. u8 tms_scan = TAP_MOVE(cur_state, end_state);
  71. for (i = 0; i < 7; i++)
  72. {
  73. tms = (tms_scan >> i) & 1;
  74. bitbang_interface->write(0, tms, 0);
  75. bitbang_interface->write(1, tms, 0);
  76. }
  77. bitbang_interface->write(CLOCK_IDLE(), tms, 0);
  78. cur_state = end_state;
  79. }
  80. void bitbang_path_move(pathmove_command_t *cmd)
  81. {
  82. int num_states = cmd->num_states;
  83. int state_count;
  84. int tms = 0;
  85. state_count = 0;
  86. while (num_states)
  87. {
  88. if (tap_transitions[cur_state].low == cmd->path[state_count])
  89. {
  90. tms = 0;
  91. }
  92. else if (tap_transitions[cur_state].high == cmd->path[state_count])
  93. {
  94. tms = 1;
  95. }
  96. else
  97. {
  98. LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[cmd->path[state_count]]);
  99. exit(-1);
  100. }
  101. bitbang_interface->write(0, tms, 0);
  102. bitbang_interface->write(1, tms, 0);
  103. cur_state = cmd->path[state_count];
  104. state_count++;
  105. num_states--;
  106. }
  107. bitbang_interface->write(CLOCK_IDLE(), tms, 0);
  108. end_state = cur_state;
  109. }
  110. void bitbang_runtest(int num_cycles)
  111. {
  112. int i;
  113. enum tap_state saved_end_state = end_state;
  114. /* only do a state_move when we're not already in IDLE */
  115. if (cur_state != TAP_IDLE)
  116. {
  117. bitbang_end_state(TAP_IDLE);
  118. bitbang_state_move();
  119. }
  120. /* execute num_cycles */
  121. for (i = 0; i < num_cycles; i++)
  122. {
  123. bitbang_interface->write(0, 0, 0);
  124. bitbang_interface->write(1, 0, 0);
  125. }
  126. bitbang_interface->write(CLOCK_IDLE(), 0, 0);
  127. /* finish in end_state */
  128. bitbang_end_state(saved_end_state);
  129. if (cur_state != end_state)
  130. bitbang_state_move();
  131. }
  132. void bitbang_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
  133. {
  134. enum tap_state saved_end_state = end_state;
  135. int bit_cnt;
  136. if (!((!ir_scan && (cur_state == TAP_DRSHIFT)) || (ir_scan && (cur_state == TAP_IRSHIFT))))
  137. {
  138. if (ir_scan)
  139. bitbang_end_state(TAP_IRSHIFT);
  140. else
  141. bitbang_end_state(TAP_DRSHIFT);
  142. bitbang_state_move();
  143. bitbang_end_state(saved_end_state);
  144. }
  145. for (bit_cnt = 0; bit_cnt < scan_size; bit_cnt++)
  146. {
  147. int val=0;
  148. int tms=(bit_cnt==scan_size-1) ? 1 : 0;
  149. int tdi;
  150. int bytec=bit_cnt/8;
  151. int bcval=1<<(bit_cnt % 8);
  152. /* if we're just reading the scan, but don't care about the output
  153. * default to outputting 'low', this also makes valgrind traces more readable,
  154. * as it removes the dependency on an uninitialised value
  155. */
  156. tdi=0;
  157. if ((type != SCAN_IN) && (buffer[bytec] & bcval))
  158. tdi=1;
  159. bitbang_interface->write(0, tms, tdi);
  160. if (type!=SCAN_OUT)
  161. val=bitbang_interface->read();
  162. bitbang_interface->write(1, tms, tdi);
  163. if (type != SCAN_OUT)
  164. {
  165. if (val)
  166. buffer[bytec] |= bcval;
  167. else
  168. buffer[bytec] &= ~bcval;
  169. }
  170. }
  171. /* TAP_DRSHIFT & TAP_IRSHIFT are illegal end states, so we always transition to the pause
  172. * state which is a legal stable state from which statemove will work.
  173. *
  174. * Exit1 -> Pause
  175. */
  176. bitbang_interface->write(0, 0, 0);
  177. bitbang_interface->write(1, 0, 0);
  178. bitbang_interface->write(CLOCK_IDLE(), 0, 0);
  179. if (ir_scan)
  180. cur_state = TAP_IRPAUSE;
  181. else
  182. cur_state = TAP_DRPAUSE;
  183. if (cur_state != end_state)
  184. bitbang_state_move();
  185. }
  186. int bitbang_execute_queue(void)
  187. {
  188. jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
  189. int scan_size;
  190. enum scan_type type;
  191. u8 *buffer;
  192. int retval;
  193. if (!bitbang_interface)
  194. {
  195. LOG_ERROR("BUG: Bitbang interface called, but not yet initialized");
  196. exit(-1);
  197. }
  198. /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
  199. * that wasn't handled by a caller-provided error handler
  200. */
  201. retval = ERROR_OK;
  202. if(bitbang_interface->blink)
  203. bitbang_interface->blink(1);
  204. while (cmd)
  205. {
  206. switch (cmd->type)
  207. {
  208. case JTAG_END_STATE:
  209. #ifdef _DEBUG_JTAG_IO_
  210. LOG_DEBUG("end_state: %i", cmd->cmd.end_state->end_state);
  211. #endif
  212. if (cmd->cmd.end_state->end_state != -1)
  213. bitbang_end_state(cmd->cmd.end_state->end_state);
  214. break;
  215. case JTAG_RESET:
  216. #ifdef _DEBUG_JTAG_IO_
  217. LOG_DEBUG("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  218. #endif
  219. if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_reset_config & RESET_SRST_PULLS_TRST)))
  220. {
  221. cur_state = TAP_RESET;
  222. }
  223. bitbang_interface->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  224. break;
  225. case JTAG_RUNTEST:
  226. #ifdef _DEBUG_JTAG_IO_
  227. LOG_DEBUG("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, cmd->cmd.runtest->end_state);
  228. #endif
  229. if (cmd->cmd.runtest->end_state != -1)
  230. bitbang_end_state(cmd->cmd.runtest->end_state);
  231. bitbang_runtest(cmd->cmd.runtest->num_cycles);
  232. break;
  233. case JTAG_STATEMOVE:
  234. #ifdef _DEBUG_JTAG_IO_
  235. LOG_DEBUG("statemove end in %i", cmd->cmd.statemove->end_state);
  236. #endif
  237. if (cmd->cmd.statemove->end_state != -1)
  238. bitbang_end_state(cmd->cmd.statemove->end_state);
  239. bitbang_state_move();
  240. break;
  241. case JTAG_PATHMOVE:
  242. #ifdef _DEBUG_JTAG_IO_
  243. LOG_DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
  244. #endif
  245. bitbang_path_move(cmd->cmd.pathmove);
  246. break;
  247. case JTAG_SCAN:
  248. #ifdef _DEBUG_JTAG_IO_
  249. LOG_DEBUG("%s scan end in %i", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", cmd->cmd.scan->end_state);
  250. #endif
  251. if (cmd->cmd.scan->end_state != -1)
  252. bitbang_end_state(cmd->cmd.scan->end_state);
  253. scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
  254. type = jtag_scan_type(cmd->cmd.scan);
  255. bitbang_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
  256. if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
  257. retval = ERROR_JTAG_QUEUE_FAILED;
  258. if (buffer)
  259. free(buffer);
  260. break;
  261. case JTAG_SLEEP:
  262. #ifdef _DEBUG_JTAG_IO_
  263. LOG_DEBUG("sleep %i", cmd->cmd.sleep->us);
  264. #endif
  265. jtag_sleep(cmd->cmd.sleep->us);
  266. break;
  267. default:
  268. LOG_ERROR("BUG: unknown JTAG command type encountered");
  269. exit(-1);
  270. }
  271. cmd = cmd->next;
  272. }
  273. if(bitbang_interface->blink)
  274. bitbang_interface->blink(0);
  275. return retval;
  276. }