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.
 
 
 
 
 
 

794 lines
21 KiB

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