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.
 
 
 
 
 
 

391 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. #include "bitq.h"
  24. #include "interface.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 uint8_t* 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 uint8_t* in_buff; /* pointer to buffer for scanned data */
  38. static int in_idx; /* index of byte being scanned */
  39. static uint8_t 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 (!tap_is_state_stable(state))
  128. {
  129. LOG_ERROR("BUG: %i is not a valid end state", state);
  130. exit(-1);
  131. }
  132. tap_set_end_state(state);
  133. }
  134. void bitq_state_move(tap_state_t new_state)
  135. {
  136. int i = 0;
  137. uint8_t tms_scan;
  138. if (!tap_is_state_stable(tap_get_state()) || !tap_is_state_stable(new_state))
  139. {
  140. LOG_ERROR("TAP move from or to unstable state");
  141. exit(-1);
  142. }
  143. tms_scan = tap_get_tms_path(tap_get_state(), new_state);
  144. int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
  145. for (i = 0; i<tms_count; i++)
  146. {
  147. bitq_io(tms_scan & 1, 0, 0);
  148. tms_scan >>= 1;
  149. }
  150. tap_set_state(new_state);
  151. }
  152. void bitq_path_move(pathmove_command_t* cmd)
  153. {
  154. int i;
  155. for (i = 0; i <= cmd->num_states; i++)
  156. {
  157. if (tap_state_transition(tap_get_state(), false) == cmd->path[i])
  158. bitq_io(0, 0, 0);
  159. else if (tap_state_transition(tap_get_state(), true) == cmd->path[i])
  160. bitq_io(1, 0, 0);
  161. else
  162. {
  163. LOG_ERROR( "BUG: %s -> %s isn't a valid TAP transition", tap_state_name(
  164. tap_get_state() ), tap_state_name(cmd->path[i]) );
  165. exit(-1);
  166. }
  167. tap_set_state(cmd->path[i]);
  168. }
  169. tap_set_end_state( tap_get_state() );
  170. }
  171. void bitq_runtest(int num_cycles)
  172. {
  173. int i;
  174. /* only do a state_move when we're not already in IDLE */
  175. if (tap_get_state() != TAP_IDLE)
  176. bitq_state_move(TAP_IDLE);
  177. /* execute num_cycles */
  178. for (i = 0; i < num_cycles; i++)
  179. bitq_io(0, 0, 0);
  180. /* finish in end_state */
  181. if ( tap_get_state() != tap_get_end_state() )
  182. bitq_state_move( tap_get_end_state() );
  183. }
  184. void bitq_scan_field(scan_field_t* field, int pause)
  185. {
  186. int bit_cnt;
  187. int tdo_req;
  188. uint8_t* out_ptr;
  189. uint8_t out_mask;
  190. if (field->in_value)
  191. tdo_req = 1;
  192. else
  193. tdo_req = 0;
  194. if (field->out_value==NULL)
  195. {
  196. /* just send zeros and request data from TDO */
  197. for (bit_cnt = field->num_bits; bit_cnt>1; bit_cnt--)
  198. bitq_io(0, 0, tdo_req);
  199. bitq_io(pause, 0, tdo_req);
  200. }
  201. else
  202. {
  203. /* send data, and optionally request TDO */
  204. out_mask = 0x01;
  205. out_ptr = field->out_value;
  206. for (bit_cnt = field->num_bits; bit_cnt>1; bit_cnt--)
  207. {
  208. bitq_io(0, ( (*out_ptr) & out_mask ) != 0, tdo_req);
  209. if (out_mask==0x80)
  210. {
  211. out_mask = 0x01;
  212. out_ptr++;
  213. }
  214. else
  215. out_mask <<= 1;
  216. }
  217. bitq_io(pause, ( (*out_ptr) & out_mask ) != 0, tdo_req);
  218. }
  219. if (pause)
  220. {
  221. bitq_io(0, 0, 0);
  222. if (tap_get_state()==TAP_IRSHIFT)
  223. tap_set_state(TAP_IRPAUSE);
  224. else if (tap_get_state()==TAP_DRSHIFT)
  225. tap_set_state(TAP_DRPAUSE);
  226. }
  227. }
  228. void bitq_scan(scan_command_t* cmd)
  229. {
  230. int i;
  231. if (cmd->ir_scan)
  232. bitq_state_move(TAP_IRSHIFT);
  233. else
  234. bitq_state_move(TAP_DRSHIFT);
  235. for (i = 0; i < cmd->num_fields - 1; i++)
  236. bitq_scan_field(&cmd->fields[i], 0);
  237. bitq_scan_field(&cmd->fields[i], 1);
  238. }
  239. int bitq_execute_queue(void)
  240. {
  241. jtag_command_t* cmd = jtag_command_queue; /* currently processed command */
  242. bitq_in_state.cmd = jtag_command_queue;
  243. bitq_in_state.field_idx = 0;
  244. bitq_in_state.bit_pos = 0;
  245. bitq_in_state.status = ERROR_OK;
  246. while (cmd)
  247. {
  248. switch (cmd->type)
  249. {
  250. case JTAG_RESET:
  251. #ifdef _DEBUG_JTAG_IO_
  252. LOG_DEBUG("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  253. #endif
  254. if ( (cmd->cmd.reset->trst == 1) || ( cmd->cmd.reset->srst && (jtag_get_reset_config() & RESET_SRST_PULLS_TRST) ) )
  255. {
  256. tap_set_state(TAP_RESET);
  257. }
  258. bitq_interface->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  259. if ( bitq_interface->in_rdy() )
  260. bitq_in_proc();
  261. break;
  262. case JTAG_RUNTEST:
  263. #ifdef _DEBUG_JTAG_IO_
  264. LOG_DEBUG("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, cmd->cmd.runtest->end_state);
  265. #endif
  266. bitq_end_state(cmd->cmd.runtest->end_state);
  267. bitq_runtest(cmd->cmd.runtest->num_cycles);
  268. break;
  269. case JTAG_STATEMOVE:
  270. #ifdef _DEBUG_JTAG_IO_
  271. LOG_DEBUG("statemove end in %i", cmd->cmd.statemove->end_state);
  272. #endif
  273. bitq_end_state(cmd->cmd.statemove->end_state);
  274. bitq_state_move( tap_get_end_state() ); /* uncoditional TAP move */
  275. break;
  276. case JTAG_PATHMOVE:
  277. #ifdef _DEBUG_JTAG_IO_
  278. LOG_DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states,
  279. cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
  280. #endif
  281. bitq_path_move(cmd->cmd.pathmove);
  282. break;
  283. case JTAG_SCAN:
  284. #ifdef _DEBUG_JTAG_IO_
  285. LOG_DEBUG("scan end in %i", cmd->cmd.scan->end_state);
  286. if (cmd->cmd.scan->ir_scan)
  287. LOG_DEBUG("scan ir");
  288. else
  289. LOG_DEBUG("scan dr");
  290. #endif
  291. bitq_end_state(cmd->cmd.scan->end_state);
  292. bitq_scan(cmd->cmd.scan);
  293. if ( tap_get_state() != tap_get_end_state() )
  294. bitq_state_move( tap_get_end_state() );
  295. break;
  296. case JTAG_SLEEP:
  297. #ifdef _DEBUG_JTAG_IO_
  298. LOG_DEBUG("sleep %i", cmd->cmd.sleep->us);
  299. #endif
  300. bitq_interface->sleep(cmd->cmd.sleep->us);
  301. if ( bitq_interface->in_rdy() )
  302. bitq_in_proc();
  303. break;
  304. default:
  305. LOG_ERROR("BUG: unknown JTAG command type encountered");
  306. exit(-1);
  307. }
  308. cmd = cmd->next;
  309. }
  310. bitq_interface->flush();
  311. bitq_in_proc();
  312. if (bitq_in_state.cmd)
  313. {
  314. LOG_ERROR("missing data from bitq interface");
  315. return ERROR_JTAG_QUEUE_FAILED;
  316. }
  317. if (bitq_interface->in() >= 0)
  318. {
  319. LOG_ERROR("extra data from bitq interface");
  320. return ERROR_JTAG_QUEUE_FAILED;
  321. }
  322. return bitq_in_state.status;
  323. }
  324. void bitq_cleanup(void)
  325. {
  326. if (bitq_in_buffer != NULL)
  327. {
  328. free(bitq_in_buffer);
  329. bitq_in_buffer = NULL;
  330. }
  331. }