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.
 
 
 
 
 
 

356 lines
10 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. /**
  37. * Function bitbang_stableclocks
  38. * issues a number of clock cycles while staying in a stable state.
  39. * Because the TMS value required to stay in the RESET state is a 1, whereas
  40. * the TMS value required to stay in any of the other stable states is a 0,
  41. * this function checks the current stable state to decide on the value of TMS
  42. * to use.
  43. */
  44. static void bitbang_stableclocks(int num_cycles);
  45. bitbang_interface_t *bitbang_interface;
  46. /* DANGER!!!! clock absolutely *MUST* be 0 in idle or reset won't work!
  47. *
  48. * Set this to 1 and str912 reset halt will fail.
  49. *
  50. * If someone can submit a patch with an explanation it will be greatly
  51. * appreciated, but as far as I can tell (ØH) DCLK is generated upon
  52. * clk=0 in TAP_IDLE. Good luck deducing that from the ARM documentation!
  53. * The ARM documentation uses the term "DCLK is asserted while in the TAP_IDLE
  54. * state". With hardware there is no such thing as *while* in a state. There
  55. * are only edges. So clk => 0 is in fact a very subtle state transition that
  56. * happens *while* in the TAP_IDLE state. "#&¤"#¤&"#&"#&
  57. *
  58. * For "reset halt" the last thing that happens before srst is asserted
  59. * is that the breakpoint is set up. If DCLK is not wiggled one last
  60. * time before the reset, then the breakpoint is not set up and
  61. * "reset halt" will fail to halt.
  62. *
  63. */
  64. #define CLOCK_IDLE() 0
  65. int bitbang_execute_queue(void);
  66. /* The bitbang driver leaves the TCK 0 when in idle */
  67. void bitbang_end_state(tap_state_t state)
  68. {
  69. if (tap_is_state_stable(state))
  70. tap_set_end_state(state);
  71. else
  72. {
  73. LOG_ERROR("BUG: %i is not a valid end state", state);
  74. exit(-1);
  75. }
  76. }
  77. void bitbang_state_move(void)
  78. {
  79. int i=0, tms=0;
  80. u8 tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
  81. for (i = 0; i < 7; i++)
  82. {
  83. tms = (tms_scan >> i) & 1;
  84. bitbang_interface->write(0, tms, 0);
  85. bitbang_interface->write(1, tms, 0);
  86. }
  87. bitbang_interface->write(CLOCK_IDLE(), tms, 0);
  88. tap_set_state(tap_get_end_state());
  89. }
  90. void bitbang_path_move(pathmove_command_t *cmd)
  91. {
  92. int num_states = cmd->num_states;
  93. int state_count;
  94. int tms = 0;
  95. state_count = 0;
  96. while (num_states)
  97. {
  98. if (tap_state_transition(tap_get_state(), FALSE) == cmd->path[state_count])
  99. {
  100. tms = 0;
  101. }
  102. else if (tap_state_transition(tap_get_state(), TRUE) == cmd->path[state_count])
  103. {
  104. tms = 1;
  105. }
  106. else
  107. {
  108. LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(cmd->path[state_count]));
  109. exit(-1);
  110. }
  111. bitbang_interface->write(0, tms, 0);
  112. bitbang_interface->write(1, tms, 0);
  113. tap_set_state(cmd->path[state_count]);
  114. state_count++;
  115. num_states--;
  116. }
  117. bitbang_interface->write(CLOCK_IDLE(), tms, 0);
  118. tap_set_end_state(tap_get_state());
  119. }
  120. void bitbang_runtest(int num_cycles)
  121. {
  122. int i;
  123. tap_state_t saved_end_state = tap_get_end_state();
  124. /* only do a state_move when we're not already in IDLE */
  125. if (tap_get_state() != TAP_IDLE)
  126. {
  127. bitbang_end_state(TAP_IDLE);
  128. bitbang_state_move();
  129. }
  130. /* execute num_cycles */
  131. for (i = 0; i < num_cycles; i++)
  132. {
  133. bitbang_interface->write(0, 0, 0);
  134. bitbang_interface->write(1, 0, 0);
  135. }
  136. bitbang_interface->write(CLOCK_IDLE(), 0, 0);
  137. /* finish in end_state */
  138. bitbang_end_state(saved_end_state);
  139. if (tap_get_state() != tap_get_end_state())
  140. bitbang_state_move();
  141. }
  142. static void bitbang_stableclocks(int num_cycles)
  143. {
  144. int tms = (tap_get_state() == TAP_RESET ? 1 : 0);
  145. int i;
  146. /* send num_cycles clocks onto the cable */
  147. for (i = 0; i < num_cycles; i++)
  148. {
  149. bitbang_interface->write(1, tms, 0);
  150. bitbang_interface->write(0, tms, 0);
  151. }
  152. }
  153. void bitbang_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
  154. {
  155. tap_state_t saved_end_state = tap_get_end_state();
  156. int bit_cnt;
  157. if (!((!ir_scan && (tap_get_state() == TAP_DRSHIFT)) || (ir_scan && (tap_get_state() == TAP_IRSHIFT))))
  158. {
  159. if (ir_scan)
  160. bitbang_end_state(TAP_IRSHIFT);
  161. else
  162. bitbang_end_state(TAP_DRSHIFT);
  163. bitbang_state_move();
  164. bitbang_end_state(saved_end_state);
  165. }
  166. for (bit_cnt = 0; bit_cnt < scan_size; bit_cnt++)
  167. {
  168. int val=0;
  169. int tms=(bit_cnt==scan_size-1) ? 1 : 0;
  170. int tdi;
  171. int bytec=bit_cnt/8;
  172. int bcval=1<<(bit_cnt % 8);
  173. /* if we're just reading the scan, but don't care about the output
  174. * default to outputting 'low', this also makes valgrind traces more readable,
  175. * as it removes the dependency on an uninitialised value
  176. */
  177. tdi=0;
  178. if ((type != SCAN_IN) && (buffer[bytec] & bcval))
  179. tdi=1;
  180. bitbang_interface->write(0, tms, tdi);
  181. if (type!=SCAN_OUT)
  182. val=bitbang_interface->read();
  183. bitbang_interface->write(1, tms, tdi);
  184. if (type != SCAN_OUT)
  185. {
  186. if (val)
  187. buffer[bytec] |= bcval;
  188. else
  189. buffer[bytec] &= ~bcval;
  190. }
  191. }
  192. /* TAP_DRSHIFT & TAP_IRSHIFT are illegal end states, so we always transition to the pause
  193. * state which is a legal stable state from which statemove will work.
  194. *
  195. * Exit1 -> Pause
  196. */
  197. bitbang_interface->write(0, 0, 0);
  198. bitbang_interface->write(1, 0, 0);
  199. bitbang_interface->write(CLOCK_IDLE(), 0, 0);
  200. if (ir_scan)
  201. tap_set_state(TAP_IRPAUSE);
  202. else
  203. tap_set_state(TAP_DRPAUSE);
  204. if (tap_get_state() != tap_get_end_state())
  205. bitbang_state_move();
  206. }
  207. int bitbang_execute_queue(void)
  208. {
  209. jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
  210. int scan_size;
  211. enum scan_type type;
  212. u8 *buffer;
  213. int retval;
  214. if (!bitbang_interface)
  215. {
  216. LOG_ERROR("BUG: Bitbang interface called, but not yet initialized");
  217. exit(-1);
  218. }
  219. /* return ERROR_OK, unless a jtag_read_buffer returns a failed check
  220. * that wasn't handled by a caller-provided error handler
  221. */
  222. retval = ERROR_OK;
  223. if(bitbang_interface->blink)
  224. bitbang_interface->blink(1);
  225. while (cmd)
  226. {
  227. switch (cmd->type)
  228. {
  229. case JTAG_END_STATE:
  230. #ifdef _DEBUG_JTAG_IO_
  231. LOG_DEBUG("end_state: %s", tap_state_name(cmd->cmd.end_state->end_state) );
  232. #endif
  233. if (cmd->cmd.end_state->end_state != -1)
  234. bitbang_end_state(cmd->cmd.end_state->end_state);
  235. break;
  236. case JTAG_RESET:
  237. #ifdef _DEBUG_JTAG_IO_
  238. LOG_DEBUG("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  239. #endif
  240. if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_reset_config & RESET_SRST_PULLS_TRST)))
  241. {
  242. tap_set_state(TAP_RESET);
  243. }
  244. bitbang_interface->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  245. break;
  246. case JTAG_RUNTEST:
  247. #ifdef _DEBUG_JTAG_IO_
  248. LOG_DEBUG("runtest %i cycles, end in %s", cmd->cmd.runtest->num_cycles, tap_state_name(cmd->cmd.runtest->end_state) );
  249. #endif
  250. if (cmd->cmd.runtest->end_state != -1)
  251. bitbang_end_state(cmd->cmd.runtest->end_state);
  252. bitbang_runtest(cmd->cmd.runtest->num_cycles);
  253. break;
  254. case JTAG_STABLECLOCKS:
  255. /* this is only allowed while in a stable state. A check for a stable
  256. * state was done in jtag_add_clocks()
  257. */
  258. bitbang_stableclocks(cmd->cmd.stableclocks->num_cycles);
  259. break;
  260. case JTAG_STATEMOVE:
  261. #ifdef _DEBUG_JTAG_IO_
  262. LOG_DEBUG("statemove end in %s", tap_state_name(cmd->cmd.statemove->end_state));
  263. #endif
  264. if (cmd->cmd.statemove->end_state != -1)
  265. bitbang_end_state(cmd->cmd.statemove->end_state);
  266. bitbang_state_move();
  267. break;
  268. case JTAG_PATHMOVE:
  269. #ifdef _DEBUG_JTAG_IO_
  270. LOG_DEBUG("pathmove: %i states, end in %s", cmd->cmd.pathmove->num_states,
  271. tap_state_name(cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]));
  272. #endif
  273. bitbang_path_move(cmd->cmd.pathmove);
  274. break;
  275. case JTAG_SCAN:
  276. #ifdef _DEBUG_JTAG_IO_
  277. LOG_DEBUG("%s scan end in %s", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", tap_state_name(cmd->cmd.scan->end_state) );
  278. #endif
  279. if (cmd->cmd.scan->end_state != -1)
  280. bitbang_end_state(cmd->cmd.scan->end_state);
  281. scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
  282. type = jtag_scan_type(cmd->cmd.scan);
  283. bitbang_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
  284. if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
  285. retval = ERROR_JTAG_QUEUE_FAILED;
  286. if (buffer)
  287. free(buffer);
  288. break;
  289. case JTAG_SLEEP:
  290. #ifdef _DEBUG_JTAG_IO_
  291. LOG_DEBUG("sleep %i", cmd->cmd.sleep->us);
  292. #endif
  293. jtag_sleep(cmd->cmd.sleep->us);
  294. break;
  295. default:
  296. LOG_ERROR("BUG: unknown JTAG command type encountered");
  297. exit(-1);
  298. }
  299. cmd = cmd->next;
  300. }
  301. if(bitbang_interface->blink)
  302. bitbang_interface->blink(0);
  303. return retval;
  304. }