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.
 
 
 
 
 
 

525 lines
15 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2005 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * Copyright (C) 2007-2010 Øyvind Harboe *
  6. * oyvind.harboe@zylin.com *
  7. * *
  8. * Copyright (C) 2009 SoftPLC Corporation *
  9. * http://softplc.com *
  10. * dick@softplc.com *
  11. * *
  12. * Copyright (C) 2009 Zachary T Welch *
  13. * zw@superlucidity.net *
  14. * *
  15. * This program is free software; you can redistribute it and/or modify *
  16. * it under the terms of the GNU General Public License as published by *
  17. * the Free Software Foundation; either version 2 of the License, or *
  18. * (at your option) any later version. *
  19. * *
  20. * This program is distributed in the hope that it will be useful, *
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  23. * GNU General Public License for more details. *
  24. * *
  25. * You should have received a copy of the GNU General Public License *
  26. * along with this program; if not, write to the *
  27. * Free Software Foundation, Inc., *
  28. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  29. ***************************************************************************/
  30. #ifdef HAVE_CONFIG_H
  31. #include "config.h"
  32. #endif
  33. #include <jtag/jtag.h>
  34. #include <jtag/interface.h>
  35. #include <jtag/commands.h>
  36. #include <jtag/minidriver.h>
  37. #include <helper/command.h>
  38. struct jtag_callback_entry {
  39. struct jtag_callback_entry *next;
  40. jtag_callback_t callback;
  41. jtag_callback_data_t data0;
  42. jtag_callback_data_t data1;
  43. jtag_callback_data_t data2;
  44. jtag_callback_data_t data3;
  45. };
  46. static struct jtag_callback_entry *jtag_callback_queue_head;
  47. static struct jtag_callback_entry *jtag_callback_queue_tail;
  48. static void jtag_callback_queue_reset(void)
  49. {
  50. jtag_callback_queue_head = NULL;
  51. jtag_callback_queue_tail = NULL;
  52. }
  53. /**
  54. * Copy a struct scan_field for insertion into the queue.
  55. *
  56. * This allocates a new copy of out_value using cmd_queue_alloc.
  57. */
  58. static void cmd_queue_scan_field_clone(struct scan_field *dst, const struct scan_field *src)
  59. {
  60. dst->num_bits = src->num_bits;
  61. dst->out_value = buf_cpy(src->out_value, cmd_queue_alloc(DIV_ROUND_UP(src->num_bits, 8)), src->num_bits);
  62. dst->in_value = src->in_value;
  63. }
  64. /**
  65. * see jtag_add_ir_scan()
  66. *
  67. */
  68. int interface_jtag_add_ir_scan(struct jtag_tap *active,
  69. const struct scan_field *in_fields, tap_state_t state)
  70. {
  71. size_t num_taps = jtag_tap_count_enabled();
  72. struct jtag_command *cmd = cmd_queue_alloc(sizeof(struct jtag_command));
  73. struct scan_command *scan = cmd_queue_alloc(sizeof(struct scan_command));
  74. struct scan_field *out_fields = cmd_queue_alloc(num_taps * sizeof(struct scan_field));
  75. jtag_queue_command(cmd);
  76. cmd->type = JTAG_SCAN;
  77. cmd->cmd.scan = scan;
  78. scan->ir_scan = true;
  79. scan->num_fields = num_taps; /* one field per device */
  80. scan->fields = out_fields;
  81. scan->end_state = state;
  82. struct scan_field *field = out_fields; /* keep track where we insert data */
  83. /* loop over all enabled TAPs */
  84. for (struct jtag_tap *tap = jtag_tap_next_enabled(NULL); tap != NULL; tap = jtag_tap_next_enabled(tap)) {
  85. /* search the input field list for fields for the current TAP */
  86. if (tap == active) {
  87. /* if TAP is listed in input fields, copy the value */
  88. tap->bypass = 0;
  89. cmd_queue_scan_field_clone(field, in_fields);
  90. } else {
  91. /* if a TAP isn't listed in input fields, set it to BYPASS */
  92. tap->bypass = 1;
  93. field->num_bits = tap->ir_length;
  94. field->out_value = buf_set_ones(cmd_queue_alloc(DIV_ROUND_UP(tap->ir_length, 8)), tap->ir_length);
  95. field->in_value = NULL; /* do not collect input for tap's in bypass */
  96. }
  97. /* update device information */
  98. buf_cpy(field->out_value, tap->cur_instr, tap->ir_length);
  99. field++;
  100. }
  101. /* paranoia: jtag_tap_count_enabled() and jtag_tap_next_enabled() not in sync */
  102. assert(field == out_fields + num_taps);
  103. return ERROR_OK;
  104. }
  105. /**
  106. * see jtag_add_dr_scan()
  107. *
  108. */
  109. int interface_jtag_add_dr_scan(struct jtag_tap *active, int in_num_fields,
  110. const struct scan_field *in_fields, tap_state_t state)
  111. {
  112. /* count devices in bypass */
  113. size_t bypass_devices = 0;
  114. for (struct jtag_tap *tap = jtag_tap_next_enabled(NULL); tap != NULL; tap = jtag_tap_next_enabled(tap)) {
  115. if (tap->bypass)
  116. bypass_devices++;
  117. }
  118. struct jtag_command *cmd = cmd_queue_alloc(sizeof(struct jtag_command));
  119. struct scan_command *scan = cmd_queue_alloc(sizeof(struct scan_command));
  120. struct scan_field *out_fields = cmd_queue_alloc((in_num_fields + bypass_devices) * sizeof(struct scan_field));
  121. jtag_queue_command(cmd);
  122. cmd->type = JTAG_SCAN;
  123. cmd->cmd.scan = scan;
  124. scan->ir_scan = false;
  125. scan->num_fields = in_num_fields + bypass_devices;
  126. scan->fields = out_fields;
  127. scan->end_state = state;
  128. struct scan_field *field = out_fields; /* keep track where we insert data */
  129. /* loop over all enabled TAPs */
  130. for (struct jtag_tap *tap = jtag_tap_next_enabled(NULL); tap != NULL; tap = jtag_tap_next_enabled(tap)) {
  131. /* if TAP is not bypassed insert matching input fields */
  132. if (!tap->bypass) {
  133. assert(active == tap);
  134. #ifndef NDEBUG
  135. /* remember initial position for assert() */
  136. struct scan_field *start_field = field;
  137. #endif /* NDEBUG */
  138. for (int j = 0; j < in_num_fields; j++) {
  139. cmd_queue_scan_field_clone(field, in_fields + j);
  140. field++;
  141. }
  142. assert(field > start_field); /* must have at least one input field per not bypassed TAP */
  143. }
  144. /* if a TAP is bypassed, generated a dummy bit*/
  145. else {
  146. field->num_bits = 1;
  147. field->out_value = NULL;
  148. field->in_value = NULL;
  149. field++;
  150. }
  151. }
  152. assert(field == out_fields + scan->num_fields); /* no superfluous input fields permitted */
  153. return ERROR_OK;
  154. }
  155. /**
  156. * Generate a DR SCAN using the array of output values passed to the function
  157. *
  158. * This function assumes that the parameter target_tap specifies the one TAP
  159. * that is not bypassed. All other TAPs must be bypassed and the function will
  160. * generate a dummy 1bit field for them.
  161. *
  162. * For the target_tap a sequence of output-only fields will be generated where
  163. * each field has the size num_bits and the field's values are taken from
  164. * the array value.
  165. *
  166. * The bypass status of TAPs is set by jtag_add_ir_scan().
  167. *
  168. */
  169. void interface_jtag_add_dr_out(struct jtag_tap *target_tap,
  170. int in_num_fields,
  171. const int *num_bits,
  172. const uint32_t *value,
  173. tap_state_t end_state)
  174. {
  175. /* count devices in bypass */
  176. size_t bypass_devices = 0;
  177. for (struct jtag_tap *tap = jtag_tap_next_enabled(NULL); tap != NULL; tap = jtag_tap_next_enabled(tap)) {
  178. if (tap->bypass)
  179. bypass_devices++;
  180. }
  181. struct jtag_command *cmd = cmd_queue_alloc(sizeof(struct jtag_command));
  182. struct scan_command *scan = cmd_queue_alloc(sizeof(struct scan_command));
  183. struct scan_field *out_fields = cmd_queue_alloc((in_num_fields + bypass_devices) * sizeof(struct scan_field));
  184. jtag_queue_command(cmd);
  185. cmd->type = JTAG_SCAN;
  186. cmd->cmd.scan = scan;
  187. scan->ir_scan = false;
  188. scan->num_fields = in_num_fields + bypass_devices;
  189. scan->fields = out_fields;
  190. scan->end_state = end_state;
  191. bool target_tap_match = false;
  192. struct scan_field *field = out_fields; /* keep track where we insert data */
  193. /* loop over all enabled TAPs */
  194. for (struct jtag_tap *tap = jtag_tap_next_enabled(NULL); tap != NULL; tap = jtag_tap_next_enabled(tap)) {
  195. /* if TAP is not bypassed insert matching input fields */
  196. if (!tap->bypass) {
  197. assert(tap == target_tap); /* target_tap must match the one not bypassed TAP */
  198. target_tap_match = true;
  199. for (int j = 0; j < in_num_fields; j++) {
  200. uint8_t out_value[4];
  201. size_t scan_size = num_bits[j];
  202. buf_set_u32(out_value, 0, scan_size, value[j]);
  203. field->num_bits = scan_size;
  204. field->out_value = buf_cpy(out_value, cmd_queue_alloc(DIV_ROUND_UP(scan_size, 8)), scan_size);
  205. field->in_value = NULL;
  206. field++;
  207. }
  208. }
  209. /* if a TAP is bypassed, generated a dummy bit*/
  210. else {
  211. field->num_bits = 1;
  212. field->out_value = NULL;
  213. field->in_value = NULL;
  214. field++;
  215. }
  216. }
  217. assert(target_tap_match); /* target_tap should be enabled and not bypassed */
  218. }
  219. static int jtag_add_plain_scan(int num_bits, const uint8_t *out_bits,
  220. uint8_t *in_bits, tap_state_t state, bool ir_scan)
  221. {
  222. struct jtag_command *cmd = cmd_queue_alloc(sizeof(struct jtag_command));
  223. struct scan_command *scan = cmd_queue_alloc(sizeof(struct scan_command));
  224. struct scan_field *out_fields = cmd_queue_alloc(sizeof(struct scan_field));
  225. jtag_queue_command(cmd);
  226. cmd->type = JTAG_SCAN;
  227. cmd->cmd.scan = scan;
  228. scan->ir_scan = ir_scan;
  229. scan->num_fields = 1;
  230. scan->fields = out_fields;
  231. scan->end_state = state;
  232. out_fields->num_bits = num_bits;
  233. out_fields->out_value = buf_cpy(out_bits, cmd_queue_alloc(DIV_ROUND_UP(num_bits, 8)), num_bits);
  234. out_fields->in_value = in_bits;
  235. return ERROR_OK;
  236. }
  237. int interface_jtag_add_plain_dr_scan(int num_bits, const uint8_t *out_bits, uint8_t *in_bits, tap_state_t state)
  238. {
  239. return jtag_add_plain_scan(num_bits, out_bits, in_bits, state, false);
  240. }
  241. int interface_jtag_add_plain_ir_scan(int num_bits, const uint8_t *out_bits, uint8_t *in_bits, tap_state_t state)
  242. {
  243. return jtag_add_plain_scan(num_bits, out_bits, in_bits, state, true);
  244. }
  245. int interface_jtag_add_tlr(void)
  246. {
  247. tap_state_t state = TAP_RESET;
  248. /* allocate memory for a new list member */
  249. struct jtag_command *cmd = cmd_queue_alloc(sizeof(struct jtag_command));
  250. jtag_queue_command(cmd);
  251. cmd->type = JTAG_TLR_RESET;
  252. cmd->cmd.statemove = cmd_queue_alloc(sizeof(struct statemove_command));
  253. cmd->cmd.statemove->end_state = state;
  254. return ERROR_OK;
  255. }
  256. int interface_add_tms_seq(unsigned num_bits, const uint8_t *seq, enum tap_state state)
  257. {
  258. struct jtag_command *cmd;
  259. cmd = cmd_queue_alloc(sizeof(struct jtag_command));
  260. if (cmd == NULL)
  261. return ERROR_FAIL;
  262. cmd->type = JTAG_TMS;
  263. cmd->cmd.tms = cmd_queue_alloc(sizeof(*cmd->cmd.tms));
  264. if (!cmd->cmd.tms)
  265. return ERROR_FAIL;
  266. /* copy the bits; our caller doesn't guarantee they'll persist */
  267. cmd->cmd.tms->num_bits = num_bits;
  268. cmd->cmd.tms->bits = buf_cpy(seq,
  269. cmd_queue_alloc(DIV_ROUND_UP(num_bits, 8)), num_bits);
  270. if (!cmd->cmd.tms->bits)
  271. return ERROR_FAIL;
  272. jtag_queue_command(cmd);
  273. return ERROR_OK;
  274. }
  275. int interface_jtag_add_pathmove(int num_states, const tap_state_t *path)
  276. {
  277. /* allocate memory for a new list member */
  278. struct jtag_command *cmd = cmd_queue_alloc(sizeof(struct jtag_command));
  279. jtag_queue_command(cmd);
  280. cmd->type = JTAG_PATHMOVE;
  281. cmd->cmd.pathmove = cmd_queue_alloc(sizeof(struct pathmove_command));
  282. cmd->cmd.pathmove->num_states = num_states;
  283. cmd->cmd.pathmove->path = cmd_queue_alloc(sizeof(tap_state_t) * num_states);
  284. for (int i = 0; i < num_states; i++)
  285. cmd->cmd.pathmove->path[i] = path[i];
  286. return ERROR_OK;
  287. }
  288. int interface_jtag_add_runtest(int num_cycles, tap_state_t state)
  289. {
  290. /* allocate memory for a new list member */
  291. struct jtag_command *cmd = cmd_queue_alloc(sizeof(struct jtag_command));
  292. jtag_queue_command(cmd);
  293. cmd->type = JTAG_RUNTEST;
  294. cmd->cmd.runtest = cmd_queue_alloc(sizeof(struct runtest_command));
  295. cmd->cmd.runtest->num_cycles = num_cycles;
  296. cmd->cmd.runtest->end_state = state;
  297. return ERROR_OK;
  298. }
  299. int interface_jtag_add_clocks(int num_cycles)
  300. {
  301. /* allocate memory for a new list member */
  302. struct jtag_command *cmd = cmd_queue_alloc(sizeof(struct jtag_command));
  303. jtag_queue_command(cmd);
  304. cmd->type = JTAG_STABLECLOCKS;
  305. cmd->cmd.stableclocks = cmd_queue_alloc(sizeof(struct stableclocks_command));
  306. cmd->cmd.stableclocks->num_cycles = num_cycles;
  307. return ERROR_OK;
  308. }
  309. int interface_jtag_add_reset(int req_trst, int req_srst)
  310. {
  311. /* allocate memory for a new list member */
  312. struct jtag_command *cmd = cmd_queue_alloc(sizeof(struct jtag_command));
  313. jtag_queue_command(cmd);
  314. cmd->type = JTAG_RESET;
  315. cmd->cmd.reset = cmd_queue_alloc(sizeof(struct reset_command));
  316. cmd->cmd.reset->trst = req_trst;
  317. cmd->cmd.reset->srst = req_srst;
  318. return ERROR_OK;
  319. }
  320. int interface_jtag_add_sleep(uint32_t us)
  321. {
  322. /* allocate memory for a new list member */
  323. struct jtag_command *cmd = cmd_queue_alloc(sizeof(struct jtag_command));
  324. jtag_queue_command(cmd);
  325. cmd->type = JTAG_SLEEP;
  326. cmd->cmd.sleep = cmd_queue_alloc(sizeof(struct sleep_command));
  327. cmd->cmd.sleep->us = us;
  328. return ERROR_OK;
  329. }
  330. /* add callback to end of queue */
  331. void interface_jtag_add_callback4(jtag_callback_t callback,
  332. jtag_callback_data_t data0, jtag_callback_data_t data1,
  333. jtag_callback_data_t data2, jtag_callback_data_t data3)
  334. {
  335. struct jtag_callback_entry *entry = cmd_queue_alloc(sizeof(struct jtag_callback_entry));
  336. entry->next = NULL;
  337. entry->callback = callback;
  338. entry->data0 = data0;
  339. entry->data1 = data1;
  340. entry->data2 = data2;
  341. entry->data3 = data3;
  342. if (jtag_callback_queue_head == NULL) {
  343. jtag_callback_queue_head = entry;
  344. jtag_callback_queue_tail = entry;
  345. } else {
  346. jtag_callback_queue_tail->next = entry;
  347. jtag_callback_queue_tail = entry;
  348. }
  349. }
  350. int interface_jtag_execute_queue(void)
  351. {
  352. static int reentry;
  353. assert(reentry == 0);
  354. reentry++;
  355. int retval = default_interface_jtag_execute_queue();
  356. if (retval == ERROR_OK) {
  357. struct jtag_callback_entry *entry;
  358. for (entry = jtag_callback_queue_head; entry != NULL; entry = entry->next) {
  359. retval = entry->callback(entry->data0, entry->data1, entry->data2, entry->data3);
  360. if (retval != ERROR_OK)
  361. break;
  362. }
  363. }
  364. jtag_command_queue_reset();
  365. jtag_callback_queue_reset();
  366. reentry--;
  367. return retval;
  368. }
  369. static int jtag_convert_to_callback4(jtag_callback_data_t data0,
  370. jtag_callback_data_t data1, jtag_callback_data_t data2, jtag_callback_data_t data3)
  371. {
  372. ((jtag_callback1_t)data1)(data0);
  373. return ERROR_OK;
  374. }
  375. void interface_jtag_add_callback(jtag_callback1_t callback, jtag_callback_data_t data0)
  376. {
  377. jtag_add_callback4(jtag_convert_to_callback4, data0, (jtag_callback_data_t)callback, 0, 0);
  378. }
  379. /* A minidriver can use use an inline versions of this API level fn */
  380. void jtag_add_dr_out(struct jtag_tap *tap,
  381. int num_fields, const int *num_bits, const uint32_t *value,
  382. tap_state_t end_state)
  383. {
  384. assert(end_state != TAP_RESET);
  385. assert(end_state != TAP_INVALID);
  386. cmd_queue_cur_state = end_state;
  387. interface_jtag_add_dr_out(tap,
  388. num_fields, num_bits, value,
  389. end_state);
  390. }
  391. void jtag_add_callback(jtag_callback1_t f, jtag_callback_data_t data0)
  392. {
  393. interface_jtag_add_callback(f, data0);
  394. }
  395. void jtag_add_callback4(jtag_callback_t f, jtag_callback_data_t data0,
  396. jtag_callback_data_t data1, jtag_callback_data_t data2,
  397. jtag_callback_data_t data3)
  398. {
  399. interface_jtag_add_callback4(f, data0, data1, data2, data3);
  400. }