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.
 
 
 
 
 
 

368 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]) bitq_io(0, 0, 0);
  147. else if (tap_transitions[cur_state].high == cmd->path[i]) bitq_io(1, 0, 0);
  148. else {
  149. LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[cmd->path[i]]);
  150. exit(-1);
  151. }
  152. cur_state = cmd->path[i];
  153. }
  154. end_state = cur_state;
  155. }
  156. void bitq_runtest(int num_cycles)
  157. {
  158. int i;
  159. /* only do a state_move when we're not already in IDLE */
  160. if (cur_state != TAP_IDLE) bitq_state_move(TAP_IDLE);
  161. /* execute num_cycles */
  162. for (i = 0; i < num_cycles; i++)
  163. bitq_io(0, 0, 0);
  164. /* finish in end_state */
  165. if (cur_state != end_state) bitq_state_move(end_state);
  166. }
  167. void bitq_scan_field(scan_field_t *field, int pause)
  168. {
  169. int bit_cnt;
  170. int tdo_req;
  171. u8 *out_ptr;
  172. u8 out_mask;
  173. if ( field->in_value || field->in_handler) tdo_req=1;
  174. else tdo_req=0;
  175. if (field->out_value==NULL) {
  176. /* just send zeros and request data from TDO */
  177. for (bit_cnt=field->num_bits; bit_cnt>1; bit_cnt--)
  178. bitq_io(0, 0, tdo_req);
  179. bitq_io(pause, 0, tdo_req);
  180. }
  181. else {
  182. /* send data, and optionally request TDO */
  183. out_mask=0x01;
  184. out_ptr=field->out_value;
  185. for (bit_cnt=field->num_bits; bit_cnt>1; bit_cnt--) {
  186. bitq_io(0, ((*out_ptr)&out_mask)!=0, tdo_req);
  187. if (out_mask==0x80) {
  188. out_mask=0x01;
  189. out_ptr++;
  190. }
  191. else out_mask<<=1;
  192. }
  193. bitq_io(pause, ((*out_ptr)&out_mask)!=0, tdo_req);
  194. }
  195. if (pause) {
  196. bitq_io(0,0,0);
  197. if (cur_state==TAP_IRSHIFT) cur_state=TAP_IRPAUSE;
  198. else if (cur_state==TAP_DRSHIFT) cur_state=TAP_DRPAUSE;
  199. }
  200. }
  201. void bitq_scan(scan_command_t *cmd)
  202. {
  203. int i;
  204. if (cmd->ir_scan) bitq_state_move(TAP_IRSHIFT);
  205. else bitq_state_move(TAP_DRSHIFT);
  206. for (i=0; i < cmd->num_fields-1; i++)
  207. bitq_scan_field(&cmd->fields[i], 0);
  208. bitq_scan_field(&cmd->fields[i], 1);
  209. }
  210. int bitq_execute_queue(void)
  211. {
  212. jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
  213. bitq_in_state.cmd = jtag_command_queue;
  214. bitq_in_state.field_idx = 0;
  215. bitq_in_state.bit_pos = 0;
  216. bitq_in_state.status = ERROR_OK;
  217. while (cmd) {
  218. switch (cmd->type) {
  219. case JTAG_END_STATE:
  220. #ifdef _DEBUG_JTAG_IO_
  221. LOG_DEBUG("end_state: %i", cmd->cmd.end_state->end_state);
  222. #endif
  223. bitq_end_state(cmd->cmd.end_state->end_state);
  224. break;
  225. case JTAG_RESET:
  226. #ifdef _DEBUG_JTAG_IO_
  227. LOG_DEBUG("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  228. #endif
  229. if ((cmd->cmd.reset->trst == 1) || (cmd->cmd.reset->srst && (jtag_reset_config & RESET_SRST_PULLS_TRST)))
  230. {
  231. cur_state = TAP_RESET;
  232. }
  233. bitq_interface->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  234. if (bitq_interface->in_rdy()) bitq_in_proc();
  235. break;
  236. case JTAG_RUNTEST:
  237. #ifdef _DEBUG_JTAG_IO_
  238. LOG_DEBUG("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, cmd->cmd.runtest->end_state);
  239. #endif
  240. bitq_end_state(cmd->cmd.runtest->end_state);
  241. bitq_runtest(cmd->cmd.runtest->num_cycles);
  242. break;
  243. case JTAG_STATEMOVE:
  244. #ifdef _DEBUG_JTAG_IO_
  245. LOG_DEBUG("statemove end in %i", cmd->cmd.statemove->end_state);
  246. #endif
  247. bitq_end_state(cmd->cmd.statemove->end_state);
  248. bitq_state_move(end_state); /* uncoditional TAP move */
  249. break;
  250. case JTAG_PATHMOVE:
  251. #ifdef _DEBUG_JTAG_IO_
  252. LOG_DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
  253. #endif
  254. bitq_path_move(cmd->cmd.pathmove);
  255. break;
  256. case JTAG_SCAN:
  257. #ifdef _DEBUG_JTAG_IO_
  258. LOG_DEBUG("scan end in %i", cmd->cmd.scan->end_state);
  259. if (cmd->cmd.scan->ir_scan) LOG_DEBUG("scan ir");
  260. else LOG_DEBUG("scan dr");
  261. #endif
  262. bitq_end_state(cmd->cmd.scan->end_state);
  263. bitq_scan(cmd->cmd.scan);
  264. if (cur_state != end_state) bitq_state_move(end_state);
  265. break;
  266. case JTAG_SLEEP:
  267. #ifdef _DEBUG_JTAG_IO_
  268. LOG_DEBUG("sleep %i", cmd->cmd.sleep->us);
  269. #endif
  270. bitq_interface->sleep(cmd->cmd.sleep->us);
  271. if (bitq_interface->in_rdy()) bitq_in_proc();
  272. break;
  273. default:
  274. LOG_ERROR("BUG: unknown JTAG command type encountered");
  275. exit(-1);
  276. }
  277. cmd = cmd->next;
  278. }
  279. bitq_interface->flush();
  280. bitq_in_proc();
  281. if (bitq_in_state.cmd) {
  282. LOG_ERROR("missing data from bitq interface");
  283. return ERROR_JTAG_QUEUE_FAILED;
  284. }
  285. if (bitq_interface->in()>=0) {
  286. LOG_ERROR("extra data from bitq interface");
  287. return ERROR_JTAG_QUEUE_FAILED;
  288. }
  289. return bitq_in_state.status;
  290. }
  291. void bitq_cleanup(void)
  292. {
  293. if (bitq_in_buffer!=NULL)
  294. {
  295. free(bitq_in_buffer);
  296. bitq_in_buffer=NULL;
  297. }
  298. }