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.
 
 
 
 
 
 

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