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.
 
 
 
 
 
 

394 lines
9.8 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. #define INCLUDE_JTAG_INTERFACE_H
  24. #include "bitq.h"
  25. bitq_interface_t* bitq_interface; /* low level bit queue interface */
  26. static bitq_state_t bitq_in_state; /* state of input queue */
  27. static u8* bitq_in_buffer; /* buffer dynamically reallocated as needed */
  28. static int bitq_in_bufsize = 32; /* min. buffer size */
  29. /*
  30. * input queue processing does not use jtag_read_buffer() to avoid unnecessary overhead
  31. * also the buffer for incomming data is reallocated only if necessary
  32. * no parameters, makes use of stored state information
  33. */
  34. void bitq_in_proc(void)
  35. {
  36. /* static information preserved between calls to increase performance */
  37. static u8* in_buff; /* pointer to buffer for scanned data */
  38. static int in_idx; /* index of byte being scanned */
  39. static u8 in_mask; /* mask of next bit to be scanned */
  40. scan_field_t* field;
  41. int tdo;
  42. /* loop through the queue */
  43. while (bitq_in_state.cmd)
  44. {
  45. /* only JTAG_SCAN command may return data */
  46. if (bitq_in_state.cmd->type==JTAG_SCAN)
  47. {
  48. /* loop through the fields */
  49. while (bitq_in_state.field_idx<bitq_in_state.cmd->cmd.scan->num_fields)
  50. {
  51. field = &bitq_in_state.cmd->cmd.scan->fields[bitq_in_state.field_idx];
  52. if (field->in_value)
  53. {
  54. if (bitq_in_state.bit_pos==0)
  55. {
  56. /* initialize field scanning */
  57. in_mask = 0x01;
  58. in_idx = 0;
  59. if (field->in_value)
  60. in_buff = field->in_value;
  61. else
  62. {
  63. /* buffer reallocation needed? */
  64. if (field->num_bits>bitq_in_bufsize * 8)
  65. {
  66. /* buffer previously allocated? */
  67. if (bitq_in_buffer!=NULL)
  68. {
  69. /* free it */
  70. free(bitq_in_buffer);
  71. bitq_in_buffer = NULL;
  72. }
  73. /* double the buffer size until it fits */
  74. while (field->num_bits>bitq_in_bufsize * 8)
  75. bitq_in_bufsize *= 2;
  76. }
  77. /* if necessary, allocate buffer and check for malloc error */
  78. if (bitq_in_buffer==NULL && ( bitq_in_buffer = malloc(bitq_in_bufsize) )==NULL)
  79. {
  80. LOG_ERROR("malloc error");
  81. exit(-1);
  82. }
  83. in_buff = (void*) bitq_in_buffer;
  84. }
  85. }
  86. /* field scanning */
  87. while (bitq_in_state.bit_pos<field->num_bits)
  88. {
  89. if ( ( tdo = bitq_interface->in() )<0 )
  90. {
  91. #ifdef _DEBUG_JTAG_IO_
  92. LOG_DEBUG("bitq in EOF");
  93. #endif
  94. return;
  95. }
  96. if (in_mask==0x01)
  97. in_buff[in_idx] = 0;
  98. if (tdo)
  99. in_buff[in_idx] |= in_mask;
  100. if (in_mask==0x80)
  101. {
  102. in_mask = 0x01;
  103. in_idx++;
  104. }
  105. else
  106. in_mask <<= 1;
  107. bitq_in_state.bit_pos++;
  108. }
  109. }
  110. bitq_in_state.field_idx++; /* advance to next field */
  111. bitq_in_state.bit_pos = 0; /* start next field from the first bit */
  112. }
  113. }
  114. bitq_in_state.cmd = bitq_in_state.cmd->next; /* advance to next command */
  115. bitq_in_state.field_idx = 0; /* preselect first field */
  116. }
  117. }
  118. void bitq_io(int tms, int tdi, int tdo_req)
  119. {
  120. bitq_interface->out(tms, tdi, tdo_req);
  121. /* check and process the input queue */
  122. if ( bitq_interface->in_rdy() )
  123. bitq_in_proc();
  124. }
  125. void bitq_end_state(tap_state_t state)
  126. {
  127. if (state==TAP_INVALID)
  128. return;
  129. if (!tap_is_state_stable(state))
  130. {
  131. LOG_ERROR("BUG: %i is not a valid end state", state);
  132. exit(-1);
  133. }
  134. tap_set_end_state(state);
  135. }
  136. void bitq_state_move(tap_state_t new_state)
  137. {
  138. int i = 0;
  139. u8 tms_scan;
  140. if (!tap_is_state_stable(tap_get_state()) || !tap_is_state_stable(new_state))
  141. {
  142. LOG_ERROR("TAP move from or to unstable state");
  143. exit(-1);
  144. }
  145. tms_scan = tap_get_tms_path(tap_get_state(), new_state);
  146. int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
  147. for (i = 0; i<tms_count; i++)
  148. {
  149. bitq_io(tms_scan & 1, 0, 0);
  150. tms_scan >>= 1;
  151. }
  152. tap_set_state(new_state);
  153. }
  154. void bitq_path_move(pathmove_command_t* cmd)
  155. {
  156. int i;
  157. for (i = 0; i<=cmd->num_states; i++)
  158. {
  159. if (tap_state_transition(tap_get_state(), false) == cmd->path[i])
  160. bitq_io(0, 0, 0);
  161. else if (tap_state_transition(tap_get_state(), true) == cmd->path[i])
  162. bitq_io(1, 0, 0);
  163. else
  164. {
  165. LOG_ERROR( "BUG: %s -> %s isn't a valid TAP transition", tap_state_name(
  166. tap_get_state() ), tap_state_name(cmd->path[i]) );
  167. exit(-1);
  168. }
  169. tap_set_state(cmd->path[i]);
  170. }
  171. tap_set_end_state( tap_get_state() );
  172. }
  173. void bitq_runtest(int num_cycles)
  174. {
  175. int i;
  176. /* only do a state_move when we're not already in IDLE */
  177. if (tap_get_state() != TAP_IDLE)
  178. bitq_state_move(TAP_IDLE);
  179. /* execute num_cycles */
  180. for (i = 0; i < num_cycles; i++)
  181. bitq_io(0, 0, 0);
  182. /* finish in end_state */
  183. if ( tap_get_state() != tap_get_end_state() )
  184. bitq_state_move( tap_get_end_state() );
  185. }
  186. void bitq_scan_field(scan_field_t* field, int pause)
  187. {
  188. int bit_cnt;
  189. int tdo_req;
  190. u8* out_ptr;
  191. u8 out_mask;
  192. if (field->in_value)
  193. tdo_req = 1;
  194. else
  195. tdo_req = 0;
  196. if (field->out_value==NULL)
  197. {
  198. /* just send zeros and request data from TDO */
  199. for (bit_cnt = field->num_bits; bit_cnt>1; bit_cnt--)
  200. bitq_io(0, 0, tdo_req);
  201. bitq_io(pause, 0, tdo_req);
  202. }
  203. else
  204. {
  205. /* send data, and optionally request TDO */
  206. out_mask = 0x01;
  207. out_ptr = field->out_value;
  208. for (bit_cnt = field->num_bits; bit_cnt>1; bit_cnt--)
  209. {
  210. bitq_io(0, ( (*out_ptr) & out_mask )!=0, tdo_req);
  211. if (out_mask==0x80)
  212. {
  213. out_mask = 0x01;
  214. out_ptr++;
  215. }
  216. else
  217. out_mask <<= 1;
  218. }
  219. bitq_io(pause, ( (*out_ptr) & out_mask )!=0, tdo_req);
  220. }
  221. if (pause)
  222. {
  223. bitq_io(0, 0, 0);
  224. if (tap_get_state()==TAP_IRSHIFT)
  225. tap_set_state(TAP_IRPAUSE);
  226. else if (tap_get_state()==TAP_DRSHIFT)
  227. tap_set_state(TAP_DRPAUSE);
  228. }
  229. }
  230. void bitq_scan(scan_command_t* cmd)
  231. {
  232. int i;
  233. if (cmd->ir_scan)
  234. bitq_state_move(TAP_IRSHIFT);
  235. else
  236. bitq_state_move(TAP_DRSHIFT);
  237. for (i = 0; i < cmd->num_fields - 1; i++)
  238. bitq_scan_field(&cmd->fields[i], 0);
  239. bitq_scan_field(&cmd->fields[i], 1);
  240. }
  241. int bitq_execute_queue(void)
  242. {
  243. jtag_command_t* cmd = jtag_command_queue; /* currently processed command */
  244. bitq_in_state.cmd = jtag_command_queue;
  245. bitq_in_state.field_idx = 0;
  246. bitq_in_state.bit_pos = 0;
  247. bitq_in_state.status = ERROR_OK;
  248. while (cmd)
  249. {
  250. switch (cmd->type)
  251. {
  252. case JTAG_RESET:
  253. #ifdef _DEBUG_JTAG_IO_
  254. LOG_DEBUG("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  255. #endif
  256. if ( (cmd->cmd.reset->trst == 1) || ( cmd->cmd.reset->srst && (jtag_reset_config & RESET_SRST_PULLS_TRST) ) )
  257. {
  258. tap_set_state(TAP_RESET);
  259. }
  260. bitq_interface->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  261. if ( bitq_interface->in_rdy() )
  262. bitq_in_proc();
  263. break;
  264. case JTAG_RUNTEST:
  265. #ifdef _DEBUG_JTAG_IO_
  266. LOG_DEBUG("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, cmd->cmd.runtest->end_state);
  267. #endif
  268. bitq_end_state(cmd->cmd.runtest->end_state);
  269. bitq_runtest(cmd->cmd.runtest->num_cycles);
  270. break;
  271. case JTAG_STATEMOVE:
  272. #ifdef _DEBUG_JTAG_IO_
  273. LOG_DEBUG("statemove end in %i", cmd->cmd.statemove->end_state);
  274. #endif
  275. bitq_end_state(cmd->cmd.statemove->end_state);
  276. bitq_state_move( tap_get_end_state() ); /* uncoditional TAP move */
  277. break;
  278. case JTAG_PATHMOVE:
  279. #ifdef _DEBUG_JTAG_IO_
  280. LOG_DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states,
  281. cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
  282. #endif
  283. bitq_path_move(cmd->cmd.pathmove);
  284. break;
  285. case JTAG_SCAN:
  286. #ifdef _DEBUG_JTAG_IO_
  287. LOG_DEBUG("scan end in %i", cmd->cmd.scan->end_state);
  288. if (cmd->cmd.scan->ir_scan)
  289. LOG_DEBUG("scan ir");
  290. else
  291. LOG_DEBUG("scan dr");
  292. #endif
  293. bitq_end_state(cmd->cmd.scan->end_state);
  294. bitq_scan(cmd->cmd.scan);
  295. if ( tap_get_state() != tap_get_end_state() )
  296. bitq_state_move( tap_get_end_state() );
  297. break;
  298. case JTAG_SLEEP:
  299. #ifdef _DEBUG_JTAG_IO_
  300. LOG_DEBUG("sleep %i", cmd->cmd.sleep->us);
  301. #endif
  302. bitq_interface->sleep(cmd->cmd.sleep->us);
  303. if ( bitq_interface->in_rdy() )
  304. bitq_in_proc();
  305. break;
  306. default:
  307. LOG_ERROR("BUG: unknown JTAG command type encountered");
  308. exit(-1);
  309. }
  310. cmd = cmd->next;
  311. }
  312. bitq_interface->flush();
  313. bitq_in_proc();
  314. if (bitq_in_state.cmd)
  315. {
  316. LOG_ERROR("missing data from bitq interface");
  317. return ERROR_JTAG_QUEUE_FAILED;
  318. }
  319. if (bitq_interface->in()>=0)
  320. {
  321. LOG_ERROR("extra data from bitq interface");
  322. return ERROR_JTAG_QUEUE_FAILED;
  323. }
  324. return bitq_in_state.status;
  325. }
  326. void bitq_cleanup(void)
  327. {
  328. if (bitq_in_buffer!=NULL)
  329. {
  330. free(bitq_in_buffer);
  331. bitq_in_buffer = NULL;
  332. }
  333. }