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.
 
 
 
 
 
 

322 lines
9.3 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_RTI. Good luck deducing that from the ARM documentation!
  44. * The ARM documentation uses the term "DCLK is asserted while in the TAP_RTI
  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_RTI 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 RTI */
  115. if (cur_state != TAP_RTI)
  116. {
  117. bitbang_end_state(TAP_RTI);
  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_SD)) || (ir_scan && (cur_state == TAP_SI))))
  137. {
  138. if (ir_scan)
  139. bitbang_end_state(TAP_SI);
  140. else
  141. bitbang_end_state(TAP_SD);
  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. /* if we're just reading the scan, but don't care about the output
  148. * default to outputting 'low', this also makes valgrind traces more readable,
  149. * as it removes the dependency on an uninitialised value
  150. */
  151. if ((type != SCAN_IN) && ((buffer[bit_cnt/8] >> (bit_cnt % 8)) & 0x1))
  152. {
  153. bitbang_interface->write(0, (bit_cnt==scan_size-1) ? 1 : 0, 1);
  154. bitbang_interface->write(1, (bit_cnt==scan_size-1) ? 1 : 0, 1);
  155. } else {
  156. bitbang_interface->write(0, (bit_cnt==scan_size-1) ? 1 : 0, 0);
  157. bitbang_interface->write(1, (bit_cnt==scan_size-1) ? 1 : 0, 0);
  158. }
  159. if (type != SCAN_OUT)
  160. {
  161. /*
  162. TDO should be sampled on the rising edge, and will change
  163. on the falling edge.
  164. Because there is no way to read the signal exactly at the rising edge,
  165. read after the rising edge.
  166. This is plain IEEE 1149 JTAG - nothing specific to the OpenOCD or its JTAG
  167. API.
  168. */
  169. if (bitbang_interface->read())
  170. buffer[(bit_cnt)/8] |= 1 << ((bit_cnt) % 8);
  171. else
  172. buffer[(bit_cnt)/8] &= ~(1 << ((bit_cnt) % 8));
  173. }
  174. }
  175. /* TAP_SD & TAP_SI are illegal end states, so we always transition to the pause
  176. * state which is a legal stable state from which statemove will work.
  177. *
  178. * Exit1 -> Pause
  179. */
  180. bitbang_interface->write(0, 0, 0);
  181. bitbang_interface->write(1, 0, 0);
  182. bitbang_interface->write(CLOCK_IDLE(), 0, 0);
  183. if (ir_scan)
  184. cur_state = TAP_PI;
  185. else
  186. cur_state = TAP_PD;
  187. if (cur_state != end_state)
  188. bitbang_state_move();
  189. }
  190. int bitbang_execute_queue(void)
  191. {
  192. jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
  193. int scan_size;
  194. enum scan_type type;
  195. u8 *buffer;
  196. int retval;
  197. if (!bitbang_interface)
  198. {
  199. LOG_ERROR("BUG: Bitbang interface called, but not yet initialized");
  200. exit(-1);
  201. }
  202. /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
  203. * that wasn't handled by a caller-provided error handler
  204. */
  205. retval = ERROR_OK;
  206. if(bitbang_interface->blink)
  207. bitbang_interface->blink(1);
  208. while (cmd)
  209. {
  210. switch (cmd->type)
  211. {
  212. case JTAG_END_STATE:
  213. #ifdef _DEBUG_JTAG_IO_
  214. LOG_DEBUG("end_state: %i", cmd->cmd.end_state->end_state);
  215. #endif
  216. if (cmd->cmd.end_state->end_state != -1)
  217. bitbang_end_state(cmd->cmd.end_state->end_state);
  218. break;
  219. case JTAG_RESET:
  220. #ifdef _DEBUG_JTAG_IO_
  221. LOG_DEBUG("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  222. #endif
  223. if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_reset_config & RESET_SRST_PULLS_TRST)))
  224. {
  225. cur_state = TAP_TLR;
  226. }
  227. bitbang_interface->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  228. break;
  229. case JTAG_RUNTEST:
  230. #ifdef _DEBUG_JTAG_IO_
  231. LOG_DEBUG("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, cmd->cmd.runtest->end_state);
  232. #endif
  233. if (cmd->cmd.runtest->end_state != -1)
  234. bitbang_end_state(cmd->cmd.runtest->end_state);
  235. bitbang_runtest(cmd->cmd.runtest->num_cycles);
  236. break;
  237. case JTAG_STATEMOVE:
  238. #ifdef _DEBUG_JTAG_IO_
  239. LOG_DEBUG("statemove end in %i", cmd->cmd.statemove->end_state);
  240. #endif
  241. if (cmd->cmd.statemove->end_state != -1)
  242. bitbang_end_state(cmd->cmd.statemove->end_state);
  243. bitbang_state_move();
  244. break;
  245. case JTAG_PATHMOVE:
  246. #ifdef _DEBUG_JTAG_IO_
  247. LOG_DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
  248. #endif
  249. bitbang_path_move(cmd->cmd.pathmove);
  250. break;
  251. case JTAG_SCAN:
  252. #ifdef _DEBUG_JTAG_IO_
  253. LOG_DEBUG("%s scan end in %i", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", cmd->cmd.scan->end_state);
  254. #endif
  255. if (cmd->cmd.scan->end_state != -1)
  256. bitbang_end_state(cmd->cmd.scan->end_state);
  257. scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
  258. type = jtag_scan_type(cmd->cmd.scan);
  259. bitbang_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
  260. if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
  261. retval = ERROR_JTAG_QUEUE_FAILED;
  262. if (buffer)
  263. free(buffer);
  264. break;
  265. case JTAG_SLEEP:
  266. #ifdef _DEBUG_JTAG_IO_
  267. LOG_DEBUG("sleep %i", cmd->cmd.sleep->us);
  268. #endif
  269. jtag_sleep(cmd->cmd.sleep->us);
  270. break;
  271. default:
  272. LOG_ERROR("BUG: unknown JTAG command type encountered");
  273. exit(-1);
  274. }
  275. cmd = cmd->next;
  276. }
  277. if(bitbang_interface->blink)
  278. bitbang_interface->blink(0);
  279. return retval;
  280. }