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.
 
 
 
 
 
 

868 lines
21 KiB

  1. // vim:ts=4 sw=4:
  2. /***************************************************************************
  3. * Copyright (C) 2009 by Dimitar Dimitrov <dinuxbg@gmail.com> *
  4. * based on Dominic Rath's and Benedikt Sauter's usbprog.c *
  5. * *
  6. * This program is free software; you can redistribute it and/or modify *
  7. * it under the terms of the GNU General Public License as published by *
  8. * the Free Software Foundation; either version 2 of the License, or *
  9. * (at your option) any later version. *
  10. * *
  11. * This program is distributed in the hope that it will be useful, *
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  14. * GNU General Public License for more details. *
  15. * *
  16. * You should have received a copy of the GNU General Public License *
  17. * along with this program; if not, write to the *
  18. * Free Software Foundation, Inc., *
  19. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  20. ***************************************************************************/
  21. #ifdef HAVE_CONFIG_H
  22. #include "config.h"
  23. #endif
  24. #define INCLUDE_JTAG_INTERFACE_H
  25. #include "jtag.h"
  26. #include <usb.h>
  27. #define USB_VID 0x15ba
  28. #define USB_PID 0x001e
  29. #define ARMJTAGEW_EPT_BULK_OUT 0x01u
  30. #define ARMJTAGEW_EPT_BULK_IN 0x82u
  31. #define ARMJTAGEW_USB_TIMEOUT 2000
  32. #define ARMJTAGEW_IN_BUFFER_SIZE (4*1024)
  33. #define ARMJTAGEW_OUT_BUFFER_SIZE (4*1024)
  34. /* USB command request codes. */
  35. #define CMD_GET_VERSION 0x00
  36. #define CMD_SELECT_DPIMPL 0x10
  37. #define CMD_SET_TCK_FREQUENCY 0x11
  38. #define CMD_GET_TCK_FREQUENCY 0x12
  39. #define CMD_MEASURE_MAX_TCK_FREQ 0x15
  40. #define CMD_MEASURE_RTCK_RESPONSE 0x16
  41. #define CMD_TAP_SHIFT 0x17
  42. #define CMD_SET_TAPHW_STATE 0x20
  43. #define CMD_GET_TAPHW_STATE 0x21
  44. #define CMD_TGPWR_SETUP 0x22
  45. /* Global USB buffers */
  46. static u8 usb_in_buffer[ARMJTAGEW_IN_BUFFER_SIZE];
  47. static u8 usb_out_buffer[ARMJTAGEW_OUT_BUFFER_SIZE];
  48. /* External interface functions */
  49. static int armjtagew_execute_queue(void);
  50. static int armjtagew_speed(int speed);
  51. static int armjtagew_khz(int khz, int *jtag_speed);
  52. static int armjtagew_register_commands(struct command_context_s *cmd_ctx);
  53. static int armjtagew_init(void);
  54. static int armjtagew_quit(void);
  55. /* CLI command handler functions */
  56. static int armjtagew_handle_armjtagew_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  57. /* Queue command functions */
  58. static void armjtagew_end_state(tap_state_t state);
  59. static void armjtagew_state_move(void);
  60. static void armjtagew_path_move(int num_states, tap_state_t *path);
  61. static void armjtagew_runtest(int num_cycles);
  62. static void armjtagew_scan(bool ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command);
  63. static void armjtagew_reset(int trst, int srst);
  64. //static void armjtagew_simple_command(u8 command);
  65. static int armjtagew_get_status(void);
  66. /* tap buffer functions */
  67. static void armjtagew_tap_init(void);
  68. static int armjtagew_tap_execute(void);
  69. static void armjtagew_tap_ensure_space(int scans, int bits);
  70. static void armjtagew_tap_append_step(int tms, int tdi);
  71. static void armjtagew_tap_append_scan(int length, u8 *buffer, scan_command_t *command);
  72. /* ARM-JTAG-EW lowlevel functions */
  73. typedef struct armjtagew_jtag
  74. {
  75. struct usb_dev_handle* usb_handle;
  76. } armjtagew_jtag_t;
  77. static armjtagew_jtag_t *armjtagew_usb_open(void);
  78. static void armjtagew_usb_close(armjtagew_jtag_t *armjtagew_jtag);
  79. static int armjtagew_usb_message(armjtagew_jtag_t *armjtagew_jtag, int out_length, int in_length);
  80. static int armjtagew_usb_write(armjtagew_jtag_t *armjtagew_jtag, int out_length);
  81. static int armjtagew_usb_read(armjtagew_jtag_t *armjtagew_jtag, int exp_in_length);
  82. /* helper functions */
  83. static int armjtagew_get_version_info(void);
  84. #ifdef _DEBUG_USB_COMMS_
  85. static void armjtagew_debug_buffer(u8 *buffer, int length);
  86. #endif
  87. static armjtagew_jtag_t* armjtagew_jtag_handle;
  88. /***************************************************************************/
  89. /* External interface implementation */
  90. jtag_interface_t armjtagew_interface =
  91. {
  92. .name = "arm-jtag-ew",
  93. .execute_queue = armjtagew_execute_queue,
  94. .speed = armjtagew_speed,
  95. .khz = armjtagew_khz,
  96. .register_commands = armjtagew_register_commands,
  97. .init = armjtagew_init,
  98. .quit = armjtagew_quit
  99. };
  100. static int armjtagew_execute_queue(void)
  101. {
  102. jtag_command_t *cmd = jtag_command_queue;
  103. int scan_size;
  104. enum scan_type type;
  105. u8 *buffer;
  106. while (cmd != NULL)
  107. {
  108. switch (cmd->type)
  109. {
  110. case JTAG_RUNTEST:
  111. DEBUG_JTAG_IO( "runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, \
  112. cmd->cmd.runtest->end_state);
  113. armjtagew_end_state(cmd->cmd.runtest->end_state);
  114. armjtagew_runtest(cmd->cmd.runtest->num_cycles);
  115. break;
  116. case JTAG_STATEMOVE:
  117. DEBUG_JTAG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
  118. armjtagew_end_state(cmd->cmd.statemove->end_state);
  119. armjtagew_state_move();
  120. break;
  121. case JTAG_PATHMOVE:
  122. DEBUG_JTAG_IO("pathmove: %i states, end in %i", \
  123. cmd->cmd.pathmove->num_states, \
  124. cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
  125. armjtagew_path_move(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path);
  126. break;
  127. case JTAG_SCAN:
  128. DEBUG_JTAG_IO("scan end in %i", cmd->cmd.scan->end_state);
  129. armjtagew_end_state(cmd->cmd.scan->end_state);
  130. scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
  131. DEBUG_JTAG_IO("scan input, length = %d", scan_size);
  132. #ifdef _DEBUG_USB_COMMS_
  133. armjtagew_debug_buffer(buffer, (scan_size + 7) / 8);
  134. #endif
  135. type = jtag_scan_type(cmd->cmd.scan);
  136. armjtagew_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size, cmd->cmd.scan);
  137. break;
  138. case JTAG_RESET:
  139. DEBUG_JTAG_IO("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  140. armjtagew_tap_execute();
  141. if (cmd->cmd.reset->trst == 1)
  142. {
  143. tap_set_state(TAP_RESET);
  144. }
  145. armjtagew_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  146. break;
  147. case JTAG_SLEEP:
  148. DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
  149. armjtagew_tap_execute();
  150. jtag_sleep(cmd->cmd.sleep->us);
  151. break;
  152. default:
  153. LOG_ERROR("BUG: unknown JTAG command type encountered");
  154. exit(-1);
  155. }
  156. cmd = cmd->next;
  157. }
  158. return armjtagew_tap_execute();
  159. }
  160. /* Sets speed in kHz. */
  161. static int armjtagew_speed(int speed)
  162. {
  163. int result;
  164. int speed_real;
  165. usb_out_buffer[0] = CMD_SET_TCK_FREQUENCY;
  166. buf_set_u32(usb_out_buffer+1, 0, 32, speed);
  167. result = armjtagew_usb_message(armjtagew_jtag_handle, 4, 4);
  168. if (result < 0)
  169. {
  170. LOG_ERROR("ARM-JTAG-EW setting speed failed (%d)", result);
  171. return ERROR_JTAG_DEVICE_ERROR;
  172. }
  173. usb_out_buffer[0] = CMD_GET_TCK_FREQUENCY;
  174. result = armjtagew_usb_message(armjtagew_jtag_handle, 1, 4);
  175. speed_real = (int)buf_get_u32(usb_in_buffer,0,32);
  176. if(result < 0)
  177. {
  178. LOG_ERROR("ARM-JTAG-EW getting speed failed (%d)", result);
  179. return ERROR_JTAG_DEVICE_ERROR;
  180. }
  181. else
  182. {
  183. LOG_INFO("Requested speed %dkHz, emulator reported %dkHz.", speed, speed_real);
  184. }
  185. return ERROR_OK;
  186. }
  187. static int armjtagew_khz(int khz, int *jtag_speed)
  188. {
  189. *jtag_speed = khz;
  190. return ERROR_OK;
  191. }
  192. static int armjtagew_register_commands(struct command_context_s *cmd_ctx)
  193. {
  194. register_command(cmd_ctx, NULL, "armjtagew_info", armjtagew_handle_armjtagew_info_command, COMMAND_EXEC,
  195. "query armjtagew info");
  196. return ERROR_OK;
  197. }
  198. static int armjtagew_init(void)
  199. {
  200. int check_cnt;
  201. armjtagew_jtag_handle = armjtagew_usb_open();
  202. if (armjtagew_jtag_handle == 0)
  203. {
  204. LOG_ERROR("Cannot find ARM-JTAG-EW Interface! Please check connection and permissions.");
  205. return ERROR_JTAG_INIT_FAILED;
  206. }
  207. check_cnt = 0;
  208. while (check_cnt < 3)
  209. {
  210. if (armjtagew_get_version_info() == ERROR_OK)
  211. {
  212. /* attempt to get status */
  213. armjtagew_get_status();
  214. break;
  215. }
  216. check_cnt++;
  217. }
  218. if (check_cnt == 3)
  219. {
  220. LOG_INFO("ARM-JTAG-EW initial read failed, don't worry");
  221. }
  222. LOG_INFO("ARM-JTAG-EW JTAG Interface ready");
  223. armjtagew_reset(0, 0);
  224. armjtagew_tap_init();
  225. return ERROR_OK;
  226. }
  227. static int armjtagew_quit(void)
  228. {
  229. armjtagew_usb_close(armjtagew_jtag_handle);
  230. return ERROR_OK;
  231. }
  232. /***************************************************************************/
  233. /* Queue command implementations */
  234. static void armjtagew_end_state(tap_state_t state)
  235. {
  236. if (tap_is_state_stable(state))
  237. {
  238. tap_set_end_state(state);
  239. }
  240. else
  241. {
  242. LOG_ERROR("BUG: %i is not a valid end state", state);
  243. exit(-1);
  244. }
  245. }
  246. /* Goes to the end state. */
  247. static void armjtagew_state_move(void)
  248. {
  249. int i;
  250. int tms = 0;
  251. u8 tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
  252. int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
  253. for (i = 0; i < tms_count; i++)
  254. {
  255. tms = (tms_scan >> i) & 1;
  256. armjtagew_tap_append_step(tms, 0);
  257. }
  258. tap_set_state(tap_get_end_state());
  259. }
  260. static void armjtagew_path_move(int num_states, tap_state_t *path)
  261. {
  262. int i;
  263. for (i = 0; i < num_states; i++)
  264. {
  265. /*
  266. * TODO: The ARM-JTAG-EW hardware delays TDI with 3 TCK cycles when in RTCK mode.
  267. * Either handle that here, or update the documentation with examples
  268. * how to fix that in the configuration files.
  269. */
  270. if (path[i] == tap_state_transition(tap_get_state(), false))
  271. {
  272. armjtagew_tap_append_step(0, 0);
  273. }
  274. else if (path[i] == tap_state_transition(tap_get_state(), true))
  275. {
  276. armjtagew_tap_append_step(1, 0);
  277. }
  278. else
  279. {
  280. LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path[i]));
  281. exit(-1);
  282. }
  283. tap_set_state(path[i]);
  284. }
  285. tap_set_end_state(tap_get_state());
  286. }
  287. static void armjtagew_runtest(int num_cycles)
  288. {
  289. int i;
  290. tap_state_t saved_end_state = tap_get_end_state();
  291. /* only do a state_move when we're not already in IDLE */
  292. if (tap_get_state() != TAP_IDLE)
  293. {
  294. armjtagew_end_state(TAP_IDLE);
  295. armjtagew_state_move();
  296. }
  297. /* execute num_cycles */
  298. for (i = 0; i < num_cycles; i++)
  299. {
  300. armjtagew_tap_append_step(0, 0);
  301. }
  302. /* finish in end_state */
  303. armjtagew_end_state(saved_end_state);
  304. if (tap_get_state() != tap_get_end_state())
  305. {
  306. armjtagew_state_move();
  307. }
  308. }
  309. static void armjtagew_scan(bool ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command)
  310. {
  311. tap_state_t saved_end_state;
  312. armjtagew_tap_ensure_space(1, scan_size + 8);
  313. saved_end_state = tap_get_end_state();
  314. /* Move to appropriate scan state */
  315. armjtagew_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
  316. armjtagew_state_move();
  317. armjtagew_end_state(saved_end_state);
  318. /* Scan */
  319. armjtagew_tap_append_scan(scan_size, buffer, command);
  320. /* We are in Exit1, go to Pause */
  321. armjtagew_tap_append_step(0, 0);
  322. tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
  323. if (tap_get_state() != tap_get_end_state())
  324. {
  325. armjtagew_state_move();
  326. }
  327. }
  328. static void armjtagew_reset(int trst, int srst)
  329. {
  330. const u8 trst_mask = (1u<<5);
  331. const u8 srst_mask = (1u<<6);
  332. u8 val = 0;
  333. u8 outp_en = 0;
  334. u8 change_mask = 0;
  335. int result;
  336. LOG_DEBUG("trst: %i, srst: %i", trst, srst);
  337. if (srst == 0)
  338. {
  339. val |= srst_mask;
  340. outp_en &= ~srst_mask; /* tristate */
  341. change_mask |= srst_mask;
  342. }
  343. else if (srst == 1)
  344. {
  345. val &= ~srst_mask;
  346. outp_en |= srst_mask;
  347. change_mask |= srst_mask;
  348. }
  349. if (trst == 0)
  350. {
  351. val |= trst_mask;
  352. outp_en &= ~trst_mask; /* tristate */
  353. change_mask |= trst_mask;
  354. }
  355. else if (trst == 1)
  356. {
  357. val &= ~trst_mask;
  358. outp_en |= trst_mask;
  359. change_mask |= trst_mask;
  360. }
  361. usb_out_buffer[0] = CMD_SET_TAPHW_STATE;
  362. usb_out_buffer[1] = val;
  363. usb_out_buffer[2] = outp_en;
  364. usb_out_buffer[3] = change_mask;
  365. result = armjtagew_usb_write(armjtagew_jtag_handle, 4);
  366. if (result != 4)
  367. {
  368. LOG_ERROR("ARM-JTAG-EW TRST/SRST pin set failed failed (%d)", result);
  369. }
  370. }
  371. static int armjtagew_get_status(void)
  372. {
  373. int result;
  374. usb_out_buffer[0] = CMD_GET_TAPHW_STATE;
  375. result = armjtagew_usb_message(armjtagew_jtag_handle, 1, 12);
  376. if (result == 0)
  377. {
  378. unsigned int u_tg = buf_get_u32(usb_in_buffer, 0, 16);
  379. LOG_INFO("U_tg = %d mV, U_aux = %d mV, U_tgpwr = %d mV, I_tgpwr = %d mA, D1 = %d, Target power %s %s\n", \
  380. buf_get_u32(usb_in_buffer + 0, 0, 16), \
  381. buf_get_u32(usb_in_buffer + 2, 0, 16), \
  382. buf_get_u32(usb_in_buffer + 4, 0, 16), \
  383. buf_get_u32(usb_in_buffer + 6, 0, 16), \
  384. usb_in_buffer[9], \
  385. usb_in_buffer[11] ? "OVERCURRENT" : "OK", \
  386. usb_in_buffer[10] ? "enabled" : "disabled");
  387. if (u_tg < 1500)
  388. {
  389. LOG_ERROR("Vref too low. Check Target Power\n");
  390. }
  391. }
  392. else
  393. {
  394. LOG_ERROR("ARM-JTAG-EW command CMD_GET_TAPHW_STATE failed (%d)\n", result);
  395. }
  396. return ERROR_OK;
  397. }
  398. static int armjtagew_get_version_info(void)
  399. {
  400. int result;
  401. char sn[16];
  402. char auxinfo[257];
  403. /* query hardware version */
  404. usb_out_buffer[0] = CMD_GET_VERSION;
  405. result = armjtagew_usb_message(armjtagew_jtag_handle, 1, 4+15+256);
  406. if (result != 0)
  407. {
  408. LOG_ERROR("ARM-JTAG-EW command CMD_GET_VERSION failed (%d)\n", result);
  409. return ERROR_JTAG_DEVICE_ERROR;
  410. }
  411. memcpy(sn, usb_in_buffer+4, 15);
  412. sn[15] = '\0';
  413. memcpy(auxinfo, usb_in_buffer+4+15, 256);
  414. auxinfo[256] = '\0';
  415. LOG_INFO("ARM-JTAG-EW firmware version %d.%d, hardware revision %c, SN=%s, Additional info: %s", \
  416. usb_in_buffer[1], usb_in_buffer[0], \
  417. isgraph(usb_in_buffer[2]) ? usb_in_buffer[2] : 'X', \
  418. sn, auxinfo);
  419. return ERROR_OK;
  420. }
  421. static int armjtagew_handle_armjtagew_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  422. {
  423. if (armjtagew_get_version_info() == ERROR_OK)
  424. {
  425. /* attempt to get status */
  426. armjtagew_get_status();
  427. }
  428. return ERROR_OK;
  429. }
  430. /***************************************************************************/
  431. /* ARM-JTAG-EW tap functions */
  432. /* 2048 is the max value we can use here */
  433. #define ARMJTAGEW_TAP_BUFFER_SIZE 2048
  434. static int tap_length;
  435. static u8 tms_buffer[ARMJTAGEW_TAP_BUFFER_SIZE];
  436. static u8 tdi_buffer[ARMJTAGEW_TAP_BUFFER_SIZE];
  437. static u8 tdo_buffer[ARMJTAGEW_TAP_BUFFER_SIZE];
  438. typedef struct
  439. {
  440. int first; /* First bit position in tdo_buffer to read */
  441. int length; /* Number of bits to read */
  442. scan_command_t *command; /* Corresponding scan command */
  443. u8 *buffer;
  444. } pending_scan_result_t;
  445. #define MAX_PENDING_SCAN_RESULTS 256
  446. static int pending_scan_results_length;
  447. static pending_scan_result_t pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
  448. static int last_tms;
  449. static void armjtagew_tap_init(void)
  450. {
  451. tap_length = 0;
  452. pending_scan_results_length = 0;
  453. }
  454. static void armjtagew_tap_ensure_space(int scans, int bits)
  455. {
  456. int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
  457. int available_bits = ARMJTAGEW_TAP_BUFFER_SIZE * 8 - tap_length;
  458. if (scans > available_scans || bits > available_bits)
  459. {
  460. armjtagew_tap_execute();
  461. }
  462. }
  463. static void armjtagew_tap_append_step(int tms, int tdi)
  464. {
  465. last_tms = tms;
  466. int index = tap_length / 8;
  467. if (index < ARMJTAGEW_TAP_BUFFER_SIZE)
  468. {
  469. int bit_index = tap_length % 8;
  470. u8 bit = 1 << bit_index;
  471. if (tms)
  472. {
  473. tms_buffer[index] |= bit;
  474. }
  475. else
  476. {
  477. tms_buffer[index] &= ~bit;
  478. }
  479. if (tdi)
  480. {
  481. tdi_buffer[index] |= bit;
  482. }
  483. else
  484. {
  485. tdi_buffer[index] &= ~bit;
  486. }
  487. tap_length++;
  488. }
  489. else
  490. {
  491. LOG_ERROR("armjtagew_tap_append_step, overflow");
  492. }
  493. }
  494. void armjtagew_tap_append_scan(int length, u8 *buffer, scan_command_t *command)
  495. {
  496. pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[pending_scan_results_length];
  497. int i;
  498. pending_scan_result->first = tap_length;
  499. pending_scan_result->length = length;
  500. pending_scan_result->command = command;
  501. pending_scan_result->buffer = buffer;
  502. for (i = 0; i < length; i++)
  503. {
  504. armjtagew_tap_append_step((i < length-1 ? 0 : 1), (buffer[i/8] >> (i%8)) & 1);
  505. }
  506. pending_scan_results_length++;
  507. }
  508. /* Pad and send a tap sequence to the device, and receive the answer.
  509. * For the purpose of padding we assume that we are in idle or pause state. */
  510. static int armjtagew_tap_execute(void)
  511. {
  512. int byte_length;
  513. int tms_offset;
  514. int tdi_offset;
  515. int i;
  516. int result;
  517. if (tap_length > 0)
  518. {
  519. /* Pad last byte so that tap_length is divisible by 8 */
  520. while (tap_length % 8 != 0)
  521. {
  522. /* More of the last TMS value keeps us in the same state,
  523. * analogous to free-running JTAG interfaces. */
  524. armjtagew_tap_append_step(last_tms, 0);
  525. }
  526. byte_length = tap_length / 8;
  527. usb_out_buffer[0] = CMD_TAP_SHIFT;
  528. buf_set_u32(usb_out_buffer+1, 0, 16, byte_length);
  529. tms_offset = 3;
  530. for (i = 0; i < byte_length; i++)
  531. {
  532. usb_out_buffer[tms_offset + i] = flip_u32(tms_buffer[i],8);
  533. }
  534. tdi_offset = tms_offset + byte_length;
  535. for (i = 0; i < byte_length; i++)
  536. {
  537. usb_out_buffer[tdi_offset + i] = flip_u32(tdi_buffer[i],8);
  538. }
  539. result = armjtagew_usb_message(armjtagew_jtag_handle, 3 + 2 * byte_length, byte_length + 4);
  540. if (result == 0)
  541. {
  542. int stat;
  543. stat = (int)buf_get_u32(usb_in_buffer + byte_length, 0, 32);
  544. if(stat) {
  545. LOG_ERROR("armjtagew_tap_execute, emulator returned error code %d for a CMD_TAP_SHIFT command", stat);
  546. return ERROR_JTAG_QUEUE_FAILED;
  547. }
  548. for (i = 0; i < byte_length; i++)
  549. {
  550. tdo_buffer[i] = flip_u32(usb_in_buffer[i],8);
  551. }
  552. for (i = 0; i < pending_scan_results_length; i++)
  553. {
  554. pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[i];
  555. u8 *buffer = pending_scan_result->buffer;
  556. int length = pending_scan_result->length;
  557. int first = pending_scan_result->first;
  558. scan_command_t *command = pending_scan_result->command;
  559. /* Copy to buffer */
  560. buf_set_buf(tdo_buffer, first, buffer, 0, length);
  561. DEBUG_JTAG_IO("pending scan result, length = %d", length);
  562. #ifdef _DEBUG_USB_COMMS_
  563. armjtagew_debug_buffer(buffer, byte_length);
  564. #endif
  565. if (jtag_read_buffer(buffer, command) != ERROR_OK)
  566. {
  567. armjtagew_tap_init();
  568. return ERROR_JTAG_QUEUE_FAILED;
  569. }
  570. if (pending_scan_result->buffer != NULL)
  571. {
  572. free(pending_scan_result->buffer);
  573. }
  574. }
  575. }
  576. else
  577. {
  578. LOG_ERROR("armjtagew_tap_execute, wrong result %d, expected %d", result, byte_length);
  579. return ERROR_JTAG_QUEUE_FAILED;
  580. }
  581. armjtagew_tap_init();
  582. }
  583. return ERROR_OK;
  584. }
  585. /*****************************************************************************/
  586. /* JLink USB low-level functions */
  587. static armjtagew_jtag_t* armjtagew_usb_open()
  588. {
  589. struct usb_bus *busses;
  590. struct usb_bus *bus;
  591. struct usb_device *dev;
  592. armjtagew_jtag_t *result;
  593. result = (armjtagew_jtag_t*) malloc(sizeof(armjtagew_jtag_t));
  594. usb_init();
  595. usb_find_busses();
  596. usb_find_devices();
  597. busses = usb_get_busses();
  598. /* find armjtagew_jtag device in usb bus */
  599. for (bus = busses; bus; bus = bus->next)
  600. {
  601. for (dev = bus->devices; dev; dev = dev->next)
  602. {
  603. if ((dev->descriptor.idVendor == USB_VID) && (dev->descriptor.idProduct == USB_PID))
  604. {
  605. result->usb_handle = usb_open(dev);
  606. #if 0
  607. /* usb_set_configuration required under win32 */
  608. usb_set_configuration(result->usb_handle, dev->config[0].bConfigurationValue);
  609. #endif
  610. usb_claim_interface(result->usb_handle, 0);
  611. #if 0
  612. /*
  613. * This makes problems under Mac OS X. And is not needed
  614. * under Windows. Hopefully this will not break a linux build
  615. */
  616. usb_set_altinterface(result->usb_handle, 0);
  617. #endif
  618. return result;
  619. }
  620. }
  621. }
  622. free(result);
  623. return NULL;
  624. }
  625. static void armjtagew_usb_close(armjtagew_jtag_t *armjtagew_jtag)
  626. {
  627. usb_close(armjtagew_jtag->usb_handle);
  628. free(armjtagew_jtag);
  629. }
  630. /* Send a message and receive the reply. */
  631. static int armjtagew_usb_message(armjtagew_jtag_t *armjtagew_jtag, int out_length, int in_length)
  632. {
  633. int result;
  634. result = armjtagew_usb_write(armjtagew_jtag, out_length);
  635. if (result == out_length)
  636. {
  637. result = armjtagew_usb_read(armjtagew_jtag, in_length);
  638. if (result != in_length)
  639. {
  640. LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)", in_length, result);
  641. return -1;
  642. }
  643. }
  644. else
  645. {
  646. LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)", out_length, result);
  647. return -1;
  648. }
  649. return 0;
  650. }
  651. /* Write data from out_buffer to USB. */
  652. static int armjtagew_usb_write(armjtagew_jtag_t *armjtagew_jtag, int out_length)
  653. {
  654. int result;
  655. if (out_length > ARMJTAGEW_OUT_BUFFER_SIZE)
  656. {
  657. LOG_ERROR("armjtagew_jtag_write illegal out_length=%d (max=%d)", out_length, ARMJTAGEW_OUT_BUFFER_SIZE);
  658. return -1;
  659. }
  660. result = usb_bulk_write(armjtagew_jtag->usb_handle, ARMJTAGEW_EPT_BULK_OUT, \
  661. (char*)usb_out_buffer, out_length, ARMJTAGEW_USB_TIMEOUT);
  662. DEBUG_JTAG_IO("armjtagew_usb_write, out_length = %d, result = %d", out_length, result);
  663. #ifdef _DEBUG_USB_COMMS_
  664. armjtagew_debug_buffer(usb_out_buffer, out_length);
  665. #endif
  666. return result;
  667. }
  668. /* Read data from USB into in_buffer. */
  669. static int armjtagew_usb_read(armjtagew_jtag_t *armjtagew_jtag, int exp_in_length)
  670. {
  671. int result = usb_bulk_read(armjtagew_jtag->usb_handle, ARMJTAGEW_EPT_BULK_IN, \
  672. (char*)usb_in_buffer, exp_in_length, ARMJTAGEW_USB_TIMEOUT);
  673. DEBUG_JTAG_IO("armjtagew_usb_read, result = %d", result);
  674. #ifdef _DEBUG_USB_COMMS_
  675. armjtagew_debug_buffer(usb_in_buffer, result);
  676. #endif
  677. return result;
  678. }
  679. #ifdef _DEBUG_USB_COMMS_
  680. #define BYTES_PER_LINE 16
  681. static void armjtagew_debug_buffer(u8 *buffer, int length)
  682. {
  683. char line[81];
  684. char s[4];
  685. int i;
  686. int j;
  687. for (i = 0; i < length; i += BYTES_PER_LINE)
  688. {
  689. snprintf(line, 5, "%04x", i);
  690. for (j = i; j < i + BYTES_PER_LINE && j < length; j++)
  691. {
  692. snprintf(s, 4, " %02x", buffer[j]);
  693. strcat(line, s);
  694. }
  695. LOG_DEBUG("%s", line);
  696. }
  697. }
  698. #endif