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.
 
 
 
 
 
 

389 lines
12 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. #include <sys/time.h>
  34. #include <time.h>
  35. bitq_interface_t *bitq_interface; /* low level bit queue interface */
  36. bitq_state_t bitq_in_state; /* state of input queue */
  37. u8 *bitq_in_buffer; /* buffer dynamically reallocated as needed */
  38. unsigned long bitq_in_bufsize=32; /* min. buffer size */
  39. /*
  40. * input queue processing does not use jtag_read_buffer() to avoid unnecessary overhead
  41. * also the buffer for incomming data is reallocated only if necessary
  42. * no parameters, makes use of stored state information
  43. */
  44. void bitq_in_proc(void)
  45. {
  46. /* static information preserved between calls to increase performance */
  47. static u8 *in_buff; /* pointer to buffer for scanned data */
  48. static int in_idx; /* index of byte being scanned */
  49. static u8 in_mask; /* mask of next bit to be scanned */
  50. scan_field_t *field;
  51. int tdo;
  52. int result;
  53. /* loop through the queue */
  54. while (bitq_in_state.cmd) {
  55. /* only JTAG_SCAN command may return data */
  56. if (bitq_in_state.cmd->type==JTAG_SCAN) {
  57. /* loop through the fields */
  58. while (bitq_in_state.field_idx<bitq_in_state.cmd->cmd.scan->num_fields) {
  59. field=&bitq_in_state.cmd->cmd.scan->fields[bitq_in_state.field_idx];
  60. if (field->in_check_value || field->in_value || field->in_handler) {
  61. if (bitq_in_state.bit_pos==0) {
  62. /* initialize field scanning */
  63. in_mask=0x01;
  64. in_idx=0;
  65. if (field->in_value) in_buff=field->in_value;
  66. else {
  67. /* buffer reallocation needed? */
  68. if (field->num_bits>bitq_in_bufsize*8) {
  69. /* buffer previously allocated? */
  70. if (bitq_in_buffer!=NULL) {
  71. /* free it */
  72. free(bitq_in_buffer);
  73. bitq_in_buffer=NULL;
  74. }
  75. /* double the buffer size until it fits */
  76. while (field->num_bits>bitq_in_bufsize*8) bitq_in_bufsize*=2;
  77. }
  78. /* if necessary, allocate buffer and check for malloc error */
  79. if (bitq_in_buffer==NULL && (bitq_in_buffer=malloc(bitq_in_bufsize))==NULL) {
  80. 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. if ((tdo=bitq_interface->in())<0) {
  89. #ifdef _DEBUG_JTAG_IO_
  90. DEBUG("bitq in EOF");
  91. #endif
  92. return;
  93. }
  94. if (in_mask==0x01) in_buff[in_idx]=0;
  95. if (tdo) in_buff[in_idx]|=in_mask;
  96. if (in_mask==0x80) {
  97. in_mask=0x01;
  98. in_idx++;
  99. }
  100. else in_mask<<=1;
  101. bitq_in_state.bit_pos++;
  102. }
  103. if (field->in_check_value) {
  104. /* match scanned in value */
  105. for (in_idx=0; in_idx*8<field->num_bits; in_idx++) {
  106. if (field->in_check_mask) in_mask=field->in_check_mask[in_idx];
  107. else in_mask=0xff;
  108. if (field->num_bits-in_idx*8<8) in_mask>>=8-(field->num_bits-in_idx*8);
  109. if (field->in_check_value[in_idx]&in_mask!=in_buff[in_idx]&in_mask) {
  110. char *captured_char = buf_to_str(in_buff, (field->num_bits > 64) ? 64 : field->num_bits, 16);
  111. char *in_check_value_char = buf_to_str(field->in_check_value, (field->num_bits > 64) ? 64 : field->num_bits, 16);
  112. char *in_check_mask_char = buf_to_str(field->in_check_mask, (field->num_bits > 64) ? 64 : field->num_bits, 16);
  113. /* TODO: error reporting */
  114. WARNING("value captured during scan didn't pass the requested check: captured: 0x%s check_value: 0x%s check_mask: 0x%s", captured_char, in_check_value_char, in_check_mask_char);
  115. bitq_in_state.status=ERROR_JTAG_QUEUE_FAILED;
  116. free(captured_char);
  117. free(in_check_value_char);
  118. free(in_check_mask_char);
  119. break; /* leave the comparison loop upon first mismatch */
  120. }
  121. }
  122. }
  123. if (field->in_handler && bitq_in_state.status==ERROR_OK) {
  124. bitq_in_state.status=(*field->in_handler)(in_buff, field->in_handler_priv);
  125. }
  126. }
  127. bitq_in_state.field_idx++; /* advance to next field */
  128. bitq_in_state.bit_pos=0; /* start next field from the first bit */
  129. }
  130. }
  131. bitq_in_state.cmd=bitq_in_state.cmd->next; /* advance to next command */
  132. bitq_in_state.field_idx=0; /* preselect first field */
  133. }
  134. }
  135. void bitq_io(int tms, int tdi, int tdo_req)
  136. {
  137. bitq_interface->out(tms, tdi, tdo_req);
  138. /* check and process the input queue */
  139. if (bitq_interface->in_rdy()) bitq_in_proc();
  140. }
  141. void bitq_end_state(enum tap_state state)
  142. {
  143. if (state==-1) return;
  144. if (tap_move_map[state]==-1) {
  145. ERROR("BUG: %i is not a valid end state", state);
  146. exit(-1);
  147. }
  148. end_state = state;
  149. }
  150. void bitq_state_move(enum tap_state new_state)
  151. {
  152. int i=0;
  153. u8 tms_scan;
  154. if (tap_move_map[cur_state]==-1 || tap_move_map[new_state]==-1) {
  155. ERROR("TAP move from or to unstable state");
  156. exit(-1);
  157. }
  158. tms_scan=TAP_MOVE(cur_state, new_state);
  159. for (i=0; i<7; i++) {
  160. bitq_io(tms_scan&1, 0, 0);
  161. tms_scan>>=1;
  162. }
  163. cur_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. if (tap_transitions[cur_state].low == cmd->path[i]) bitq_io(0, 0, 0);
  170. else if (tap_transitions[cur_state].high == cmd->path[i]) bitq_io(1, 0, 0);
  171. else {
  172. ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[cmd->path[i]]);
  173. exit(-1);
  174. }
  175. cur_state = cmd->path[i];
  176. }
  177. end_state = cur_state;
  178. }
  179. void bitq_runtest(int num_cycles)
  180. {
  181. int i;
  182. /* only do a state_move when we're not already in RTI */
  183. if (cur_state != TAP_RTI) bitq_state_move(TAP_RTI);
  184. /* execute num_cycles */
  185. for (i = 0; i < num_cycles; i++)
  186. bitq_io(0, 0, 0);
  187. /* finish in end_state */
  188. if (cur_state != end_state) bitq_state_move(end_state);
  189. }
  190. void bitq_scan_field(scan_field_t *field, int pause)
  191. {
  192. int bit_cnt;
  193. int tdo_req;
  194. u8 *out_ptr;
  195. u8 out_mask;
  196. if (field->in_check_value || field->in_value || field->in_handler) tdo_req=1;
  197. else tdo_req=0;
  198. if (field->out_value==NULL) {
  199. /* just send zeros and request data from TDO */
  200. for (bit_cnt=field->num_bits; bit_cnt>1; bit_cnt--)
  201. bitq_io(0, 0, tdo_req);
  202. bitq_io(pause, 0, tdo_req);
  203. }
  204. else {
  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. bitq_io(0, ((*out_ptr)&out_mask)!=0, tdo_req);
  210. if (out_mask==0x80) {
  211. out_mask=0x01;
  212. out_ptr++;
  213. }
  214. else out_mask<<=1;
  215. }
  216. bitq_io(pause, ((*out_ptr)&out_mask)!=0, tdo_req);
  217. }
  218. if (pause) {
  219. bitq_io(0,0,0);
  220. if (cur_state==TAP_SI) cur_state=TAP_PI;
  221. else if (cur_state==TAP_SD) cur_state=TAP_PD;
  222. }
  223. }
  224. void bitq_scan(scan_command_t *cmd)
  225. {
  226. int i;
  227. if (cmd->ir_scan) bitq_state_move(TAP_SI);
  228. else bitq_state_move(TAP_SD);
  229. for (i=0; i < cmd->num_fields-1; i++)
  230. bitq_scan_field(&cmd->fields[i], 0);
  231. bitq_scan_field(&cmd->fields[i], 1);
  232. }
  233. int bitq_execute_queue(void)
  234. {
  235. jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
  236. bitq_in_state.cmd = jtag_command_queue;
  237. bitq_in_state.field_idx = 0;
  238. bitq_in_state.bit_pos = 0;
  239. bitq_in_state.status = ERROR_OK;
  240. while (cmd) {
  241. switch (cmd->type) {
  242. case JTAG_END_STATE:
  243. #ifdef _DEBUG_JTAG_IO_
  244. DEBUG("end_state: %i", cmd->cmd.end_state->end_state);
  245. #endif
  246. bitq_end_state(cmd->cmd.end_state->end_state);
  247. break;
  248. case JTAG_RESET:
  249. #ifdef _DEBUG_JTAG_IO_
  250. DEBUG("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  251. #endif
  252. bitq_interface->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  253. if (bitq_interface->in_rdy()) bitq_in_proc();
  254. break;
  255. case JTAG_RUNTEST:
  256. #ifdef _DEBUG_JTAG_IO_
  257. DEBUG("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, cmd->cmd.runtest->end_state);
  258. #endif
  259. bitq_end_state(cmd->cmd.runtest->end_state);
  260. bitq_runtest(cmd->cmd.runtest->num_cycles);
  261. break;
  262. case JTAG_STATEMOVE:
  263. #ifdef _DEBUG_JTAG_IO_
  264. DEBUG("statemove end in %i", cmd->cmd.statemove->end_state);
  265. #endif
  266. bitq_end_state(cmd->cmd.statemove->end_state);
  267. bitq_state_move(end_state); /* uncoditional TAP move */
  268. break;
  269. case JTAG_PATHMOVE:
  270. #ifdef _DEBUG_JTAG_IO_
  271. DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
  272. #endif
  273. bitq_path_move(cmd->cmd.pathmove);
  274. break;
  275. case JTAG_SCAN:
  276. #ifdef _DEBUG_JTAG_IO_
  277. DEBUG("scan end in %i", cmd->cmd.scan->end_state);
  278. if (cmd->cmd.scan->ir_scan) DEBUG("scan ir");
  279. else DEBUG("scan dr");
  280. #endif
  281. bitq_end_state(cmd->cmd.scan->end_state);
  282. bitq_scan(cmd->cmd.scan);
  283. if (cur_state != end_state) bitq_state_move(end_state);
  284. break;
  285. case JTAG_SLEEP:
  286. #ifdef _DEBUG_JTAG_IO_
  287. DEBUG("sleep %i", cmd->cmd.sleep->us);
  288. #endif
  289. bitq_interface->sleep(cmd->cmd.sleep->us);
  290. if (bitq_interface->in_rdy()) bitq_in_proc();
  291. break;
  292. default:
  293. ERROR("BUG: unknown JTAG command type encountered");
  294. exit(-1);
  295. }
  296. cmd = cmd->next;
  297. }
  298. bitq_interface->flush();
  299. bitq_in_proc();
  300. if (bitq_in_state.cmd) {
  301. ERROR("missing data from bitq interface");
  302. return ERROR_JTAG_QUEUE_FAILED;
  303. }
  304. if (bitq_interface->in()>=0) {
  305. ERROR("extra data from bitq interface");
  306. return ERROR_JTAG_QUEUE_FAILED;
  307. }
  308. return bitq_in_state.status;
  309. }
  310. void bitq_cleanup(void)
  311. {
  312. if (bitq_in_buffer!=NULL)
  313. {
  314. free(bitq_in_buffer);
  315. bitq_in_buffer=NULL;
  316. }
  317. }