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.
 
 
 
 
 
 

358 lines
9.7 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. /* only JTAG_SCAN command may return data */
  53. if (bitq_in_state.cmd->type==JTAG_SCAN) {
  54. /* loop through the fields */
  55. while (bitq_in_state.field_idx<bitq_in_state.cmd->cmd.scan->num_fields) {
  56. field=&bitq_in_state.cmd->cmd.scan->fields[bitq_in_state.field_idx];
  57. if ( field->in_value || field->in_handler) {
  58. if (bitq_in_state.bit_pos==0) {
  59. /* initialize field scanning */
  60. in_mask=0x01;
  61. in_idx=0;
  62. if (field->in_value) in_buff=field->in_value;
  63. else {
  64. /* buffer reallocation needed? */
  65. if (field->num_bits>bitq_in_bufsize*8) {
  66. /* buffer previously allocated? */
  67. if (bitq_in_buffer!=NULL) {
  68. /* free it */
  69. free(bitq_in_buffer);
  70. bitq_in_buffer=NULL;
  71. }
  72. /* double the buffer size until it fits */
  73. while (field->num_bits>bitq_in_bufsize*8) bitq_in_bufsize*=2;
  74. }
  75. /* if necessary, allocate buffer and check for malloc error */
  76. if (bitq_in_buffer==NULL && (bitq_in_buffer=malloc(bitq_in_bufsize))==NULL) {
  77. LOG_ERROR("malloc error");
  78. exit(-1);
  79. }
  80. in_buff=(void *)bitq_in_buffer;
  81. }
  82. }
  83. /* field scanning */
  84. while (bitq_in_state.bit_pos<field->num_bits) {
  85. if ((tdo=bitq_interface->in())<0) {
  86. #ifdef _DEBUG_JTAG_IO_
  87. LOG_DEBUG("bitq in EOF");
  88. #endif
  89. return;
  90. }
  91. if (in_mask==0x01) in_buff[in_idx]=0;
  92. if (tdo) in_buff[in_idx]|=in_mask;
  93. if (in_mask==0x80) {
  94. in_mask=0x01;
  95. in_idx++;
  96. }
  97. else in_mask<<=1;
  98. bitq_in_state.bit_pos++;
  99. }
  100. if (field->in_handler && bitq_in_state.status==ERROR_OK) {
  101. bitq_in_state.status=(*field->in_handler)(in_buff, field->in_handler_priv, field);
  102. }
  103. }
  104. bitq_in_state.field_idx++; /* advance to next field */
  105. bitq_in_state.bit_pos=0; /* start next field from the first bit */
  106. }
  107. }
  108. bitq_in_state.cmd=bitq_in_state.cmd->next; /* advance to next command */
  109. bitq_in_state.field_idx=0; /* preselect first field */
  110. }
  111. }
  112. void bitq_io(int tms, int tdi, int tdo_req)
  113. {
  114. bitq_interface->out(tms, tdi, tdo_req);
  115. /* check and process the input queue */
  116. if (bitq_interface->in_rdy()) bitq_in_proc();
  117. }
  118. void bitq_end_state(enum tap_state state)
  119. {
  120. if (state==-1) return;
  121. if (tap_move_map[state]==-1) {
  122. LOG_ERROR("BUG: %i is not a valid end state", state);
  123. exit(-1);
  124. }
  125. end_state = state;
  126. }
  127. void bitq_state_move(enum tap_state new_state)
  128. {
  129. int i=0;
  130. u8 tms_scan;
  131. if (tap_move_map[cur_state]==-1 || tap_move_map[new_state]==-1) {
  132. LOG_ERROR("TAP move from or to unstable state");
  133. exit(-1);
  134. }
  135. tms_scan=TAP_MOVE(cur_state, new_state);
  136. for (i=0; i<7; i++) {
  137. bitq_io(tms_scan&1, 0, 0);
  138. tms_scan>>=1;
  139. }
  140. cur_state = new_state;
  141. }
  142. void bitq_path_move(pathmove_command_t *cmd)
  143. {
  144. int i;
  145. for (i=0; i<=cmd->num_states; i++) {
  146. if (tap_transitions[cur_state].low == cmd->path[i])
  147. bitq_io(0, 0, 0);
  148. else if (tap_transitions[cur_state].high == cmd->path[i])
  149. bitq_io(1, 0, 0);
  150. else {
  151. LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", jtag_state_name(cur_state), jtag_state_name(cmd->path[i]));
  152. exit(-1);
  153. }
  154. cur_state = cmd->path[i];
  155. }
  156. end_state = cur_state;
  157. }
  158. void bitq_runtest(int num_cycles)
  159. {
  160. int i;
  161. /* only do a state_move when we're not already in IDLE */
  162. if (cur_state != TAP_IDLE) bitq_state_move(TAP_IDLE);
  163. /* execute num_cycles */
  164. for (i = 0; i < num_cycles; i++)
  165. bitq_io(0, 0, 0);
  166. /* finish in end_state */
  167. if (cur_state != end_state) bitq_state_move(end_state);
  168. }
  169. void bitq_scan_field(scan_field_t *field, int pause)
  170. {
  171. int bit_cnt;
  172. int tdo_req;
  173. u8 *out_ptr;
  174. u8 out_mask;
  175. if ( field->in_value || field->in_handler) tdo_req=1;
  176. else tdo_req=0;
  177. if (field->out_value==NULL) {
  178. /* just send zeros and request data from TDO */
  179. for (bit_cnt=field->num_bits; bit_cnt>1; bit_cnt--)
  180. bitq_io(0, 0, tdo_req);
  181. bitq_io(pause, 0, tdo_req);
  182. }
  183. else {
  184. /* send data, and optionally request TDO */
  185. out_mask=0x01;
  186. out_ptr=field->out_value;
  187. for (bit_cnt=field->num_bits; bit_cnt>1; bit_cnt--) {
  188. bitq_io(0, ((*out_ptr)&out_mask)!=0, tdo_req);
  189. if (out_mask==0x80) {
  190. out_mask=0x01;
  191. out_ptr++;
  192. }
  193. else out_mask<<=1;
  194. }
  195. bitq_io(pause, ((*out_ptr)&out_mask)!=0, tdo_req);
  196. }
  197. if (pause) {
  198. bitq_io(0,0,0);
  199. if (cur_state==TAP_IRSHIFT) cur_state=TAP_IRPAUSE;
  200. else if (cur_state==TAP_DRSHIFT) cur_state=TAP_DRPAUSE;
  201. }
  202. }
  203. void bitq_scan(scan_command_t *cmd)
  204. {
  205. int i;
  206. if (cmd->ir_scan) bitq_state_move(TAP_IRSHIFT);
  207. else bitq_state_move(TAP_DRSHIFT);
  208. for (i=0; i < cmd->num_fields-1; i++)
  209. bitq_scan_field(&cmd->fields[i], 0);
  210. bitq_scan_field(&cmd->fields[i], 1);
  211. }
  212. int bitq_execute_queue(void)
  213. {
  214. jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
  215. bitq_in_state.cmd = jtag_command_queue;
  216. bitq_in_state.field_idx = 0;
  217. bitq_in_state.bit_pos = 0;
  218. bitq_in_state.status = ERROR_OK;
  219. while (cmd) {
  220. switch (cmd->type) {
  221. case JTAG_END_STATE:
  222. #ifdef _DEBUG_JTAG_IO_
  223. LOG_DEBUG("end_state: %i", cmd->cmd.end_state->end_state);
  224. #endif
  225. bitq_end_state(cmd->cmd.end_state->end_state);
  226. break;
  227. case JTAG_RESET:
  228. #ifdef _DEBUG_JTAG_IO_
  229. LOG_DEBUG("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  230. #endif
  231. if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_reset_config & RESET_SRST_PULLS_TRST)))
  232. {
  233. cur_state = TAP_RESET;
  234. }
  235. bitq_interface->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  236. if (bitq_interface->in_rdy()) bitq_in_proc();
  237. break;
  238. case JTAG_RUNTEST:
  239. #ifdef _DEBUG_JTAG_IO_
  240. LOG_DEBUG("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, cmd->cmd.runtest->end_state);
  241. #endif
  242. bitq_end_state(cmd->cmd.runtest->end_state);
  243. bitq_runtest(cmd->cmd.runtest->num_cycles);
  244. break;
  245. case JTAG_STATEMOVE:
  246. #ifdef _DEBUG_JTAG_IO_
  247. LOG_DEBUG("statemove end in %i", cmd->cmd.statemove->end_state);
  248. #endif
  249. bitq_end_state(cmd->cmd.statemove->end_state);
  250. bitq_state_move(end_state); /* uncoditional TAP move */
  251. break;
  252. case JTAG_PATHMOVE:
  253. #ifdef _DEBUG_JTAG_IO_
  254. LOG_DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
  255. #endif
  256. bitq_path_move(cmd->cmd.pathmove);
  257. break;
  258. case JTAG_SCAN:
  259. #ifdef _DEBUG_JTAG_IO_
  260. LOG_DEBUG("scan end in %i", cmd->cmd.scan->end_state);
  261. if (cmd->cmd.scan->ir_scan) LOG_DEBUG("scan ir");
  262. else LOG_DEBUG("scan dr");
  263. #endif
  264. bitq_end_state(cmd->cmd.scan->end_state);
  265. bitq_scan(cmd->cmd.scan);
  266. if (cur_state != end_state) bitq_state_move(end_state);
  267. break;
  268. case JTAG_SLEEP:
  269. #ifdef _DEBUG_JTAG_IO_
  270. LOG_DEBUG("sleep %i", cmd->cmd.sleep->us);
  271. #endif
  272. bitq_interface->sleep(cmd->cmd.sleep->us);
  273. if (bitq_interface->in_rdy()) bitq_in_proc();
  274. break;
  275. default:
  276. LOG_ERROR("BUG: unknown JTAG command type encountered");
  277. exit(-1);
  278. }
  279. cmd = cmd->next;
  280. }
  281. bitq_interface->flush();
  282. bitq_in_proc();
  283. if (bitq_in_state.cmd) {
  284. LOG_ERROR("missing data from bitq interface");
  285. return ERROR_JTAG_QUEUE_FAILED;
  286. }
  287. if (bitq_interface->in()>=0) {
  288. LOG_ERROR("extra data from bitq interface");
  289. return ERROR_JTAG_QUEUE_FAILED;
  290. }
  291. return bitq_in_state.status;
  292. }
  293. void bitq_cleanup(void)
  294. {
  295. if (bitq_in_buffer!=NULL)
  296. {
  297. free(bitq_in_buffer);
  298. bitq_in_buffer=NULL;
  299. }
  300. }