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.
 
 
 
 
 
 

1074 lines
27 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2007 by Juergen Stuber <juergen@jstuber.net> *
  3. * based on Dominic Rath's and Benedikt Sauter's usbprog.c *
  4. * *
  5. * Copyright (C) 2008 by Spencer Oliver *
  6. * spen@spen-soft.co.uk *
  7. * *
  8. * This program is free software; you can redistribute it and/or modify *
  9. * it under the terms of the GNU General Public License as published by *
  10. * the Free Software Foundation; either version 2 of the License, or *
  11. * (at your option) any later version. *
  12. * *
  13. * This program is distributed in the hope that it will be useful, *
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  16. * GNU General Public License for more details. *
  17. * *
  18. * You should have received a copy of the GNU General Public License *
  19. * along with this program; if not, write to the *
  20. * Free Software Foundation, Inc., *
  21. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  22. ***************************************************************************/
  23. #ifdef HAVE_CONFIG_H
  24. #include "config.h"
  25. #endif
  26. #define INCLUDE_JTAG_INTERFACE_H
  27. #include "jtag.h"
  28. #include <usb.h>
  29. #define VID 0x1366
  30. #define PID 0x0101
  31. #define JLINK_WRITE_ENDPOINT 0x02
  32. #define JLINK_READ_ENDPOINT 0x81
  33. static unsigned int jlink_write_ep = JLINK_WRITE_ENDPOINT;
  34. static unsigned int jlink_read_ep = JLINK_READ_ENDPOINT;
  35. static unsigned int jlink_hw_jtag_version = 2;
  36. #define JLINK_USB_TIMEOUT 1000
  37. // See Section 1.3.2 of the Segger JLink USB protocol manual
  38. /* 2048 is the max value we can use here */
  39. //#define JLINK_TAP_BUFFER_SIZE 2048
  40. #define JLINK_TAP_BUFFER_SIZE 256
  41. //#define JLINK_TAP_BUFFER_SIZE 384
  42. #define JLINK_IN_BUFFER_SIZE 2048
  43. #define JLINK_OUT_BUFFER_SIZE 2*2048+4
  44. #define JLINK_EMU_RESULT_BUFFER_SIZE 64
  45. /* Global USB buffers */
  46. static u8 usb_in_buffer[JLINK_IN_BUFFER_SIZE];
  47. static u8 usb_out_buffer[JLINK_OUT_BUFFER_SIZE];
  48. static u8 usb_emu_result_buffer[JLINK_EMU_RESULT_BUFFER_SIZE];
  49. /* Constants for JLink command */
  50. #define EMU_CMD_VERSION 0x01
  51. #define EMU_CMD_SET_SPEED 0x05
  52. #define EMU_CMD_GET_STATE 0x07
  53. #define EMU_CMD_HW_CLOCK 0xc8
  54. #define EMU_CMD_HW_TMS0 0xc9
  55. #define EMU_CMD_HW_TMS1 0xca
  56. #define EMU_CMD_HW_JTAG2 0xce
  57. #define EMU_CMD_HW_JTAG3 0xcf
  58. #define EMU_CMD_GET_MAX_MEM_BLOCK 0xd4
  59. #define EMU_CMD_HW_RESET0 0xdc
  60. #define EMU_CMD_HW_RESET1 0xdd
  61. #define EMU_CMD_HW_TRST0 0xde
  62. #define EMU_CMD_HW_TRST1 0xdf
  63. #define EMU_CMD_GET_CAPS 0xe8
  64. #define EMU_CMD_GET_HW_VERSION 0xf0
  65. /* bits return from EMU_CMD_GET_CAPS */
  66. #define EMU_CAP_GET_HW_VERSION 1
  67. #define EMU_CAP_GET_MAX_BLOCK_SIZE 11
  68. /* max speed 12MHz v5.0 jlink */
  69. #define JLINK_MAX_SPEED 12000
  70. /* External interface functions */
  71. static int jlink_execute_queue(void);
  72. static int jlink_speed(int speed);
  73. static int jlink_speed_div(int speed, int* khz);
  74. static int jlink_khz(int khz, int *jtag_speed);
  75. static int jlink_register_commands(struct command_context_s *cmd_ctx);
  76. static int jlink_init(void);
  77. static int jlink_quit(void);
  78. /* CLI command handler functions */
  79. static int jlink_handle_jlink_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  80. static int jlink_handle_jlink_hw_jtag_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
  81. /* Queue command functions */
  82. static void jlink_end_state(tap_state_t state);
  83. static void jlink_state_move(void);
  84. static void jlink_path_move(int num_states, tap_state_t *path);
  85. static void jlink_runtest(int num_cycles);
  86. static void jlink_scan(bool ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command);
  87. static void jlink_reset(int trst, int srst);
  88. static void jlink_simple_command(u8 command);
  89. static int jlink_get_status(void);
  90. /* J-Link tap buffer functions */
  91. static void jlink_tap_init(void);
  92. static int jlink_tap_execute(void);
  93. static void jlink_tap_ensure_space(int scans, int bits);
  94. static void jlink_tap_append_step(int tms, int tdi);
  95. static void jlink_tap_append_scan(int length, u8 *buffer, scan_command_t *command);
  96. /* Jlink lowlevel functions */
  97. typedef struct jlink_jtag
  98. {
  99. struct usb_dev_handle* usb_handle;
  100. } jlink_jtag_t;
  101. static jlink_jtag_t *jlink_usb_open(void);
  102. static void jlink_usb_close(jlink_jtag_t *jlink_jtag);
  103. static int jlink_usb_message(jlink_jtag_t *jlink_jtag, int out_length, int in_length);
  104. static int jlink_usb_write(jlink_jtag_t *jlink_jtag, int out_length);
  105. static int jlink_usb_read(jlink_jtag_t *jlink_jtag, int expected_size);
  106. static int jlink_usb_read_emu_result(jlink_jtag_t *jlink_jtag);
  107. /* helper functions */
  108. static int jlink_get_version_info(void);
  109. #ifdef _DEBUG_USB_COMMS_
  110. static void jlink_debug_buffer(u8 *buffer, int length);
  111. #endif
  112. static enum tap_state jlink_last_state = TAP_RESET;
  113. static jlink_jtag_t* jlink_jtag_handle;
  114. /***************************************************************************/
  115. /* External interface implementation */
  116. jtag_interface_t jlink_interface =
  117. {
  118. .name = "jlink",
  119. .execute_queue = jlink_execute_queue,
  120. .speed = jlink_speed,
  121. .speed_div = jlink_speed_div,
  122. .khz = jlink_khz,
  123. .register_commands = jlink_register_commands,
  124. .init = jlink_init,
  125. .quit = jlink_quit
  126. };
  127. static void jlink_execute_runtest(jtag_command_t *cmd)
  128. {
  129. DEBUG_JTAG_IO("runtest %i cycles, end in %i",
  130. cmd->cmd.runtest->num_cycles,
  131. cmd->cmd.runtest->end_state);
  132. if (cmd->cmd.runtest->end_state != TAP_INVALID)
  133. jlink_end_state(cmd->cmd.runtest->end_state);
  134. jlink_runtest(cmd->cmd.runtest->num_cycles);
  135. }
  136. static void jlink_execute_statemove(jtag_command_t *cmd)
  137. {
  138. DEBUG_JTAG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
  139. if (cmd->cmd.statemove->end_state != TAP_INVALID)
  140. {
  141. jlink_end_state(cmd->cmd.statemove->end_state);
  142. }
  143. jlink_state_move();
  144. }
  145. static void jlink_execute_pathmove(jtag_command_t *cmd)
  146. {
  147. DEBUG_JTAG_IO("pathmove: %i states, end in %i",
  148. cmd->cmd.pathmove->num_states,
  149. cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
  150. jlink_path_move(cmd->cmd.pathmove->num_states,
  151. cmd->cmd.pathmove->path);
  152. }
  153. static void jlink_execute_scan(jtag_command_t *cmd)
  154. {
  155. int scan_size;
  156. enum scan_type type;
  157. u8 *buffer;
  158. DEBUG_JTAG_IO("scan end in %s", tap_state_name(cmd->cmd.scan->end_state));
  159. if (cmd->cmd.scan->end_state != TAP_INVALID)
  160. jlink_end_state(cmd->cmd.scan->end_state);
  161. scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
  162. DEBUG_JTAG_IO("scan input, length = %d", scan_size);
  163. #ifdef _DEBUG_USB_COMMS_
  164. jlink_debug_buffer(buffer, (scan_size + 7) / 8);
  165. #endif
  166. type = jtag_scan_type(cmd->cmd.scan);
  167. jlink_scan(cmd->cmd.scan->ir_scan,
  168. type, buffer, scan_size, cmd->cmd.scan);
  169. }
  170. static void jlink_execute_reset(jtag_command_t *cmd)
  171. {
  172. DEBUG_JTAG_IO("reset trst: %i srst %i",
  173. cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  174. jlink_tap_execute();
  175. jlink_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  176. jlink_tap_execute();
  177. }
  178. static void jlink_execute_sleep(jtag_command_t *cmd)
  179. {
  180. DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
  181. jlink_tap_execute();
  182. jtag_sleep(cmd->cmd.sleep->us);
  183. }
  184. static void jlink_execute_command(jtag_command_t *cmd)
  185. {
  186. switch (cmd->type)
  187. {
  188. case JTAG_RUNTEST: jlink_execute_runtest(cmd); break;
  189. case JTAG_STATEMOVE: jlink_execute_statemove(cmd); break;
  190. case JTAG_PATHMOVE: jlink_execute_pathmove(cmd); break;
  191. case JTAG_SCAN: jlink_execute_scan(cmd); break;
  192. case JTAG_RESET: jlink_execute_reset(cmd); break;
  193. case JTAG_SLEEP: jlink_execute_sleep(cmd); break;
  194. default:
  195. LOG_ERROR("BUG: unknown JTAG command type encountered");
  196. exit(-1);
  197. }
  198. }
  199. static int jlink_execute_queue(void)
  200. {
  201. jtag_command_t *cmd = jtag_command_queue;
  202. while (cmd != NULL)
  203. {
  204. jlink_execute_command(cmd);
  205. cmd = cmd->next;
  206. }
  207. return jlink_tap_execute();
  208. }
  209. /* Sets speed in kHz. */
  210. static int jlink_speed(int speed)
  211. {
  212. int result;
  213. if (speed > JLINK_MAX_SPEED)
  214. {
  215. LOG_INFO("Ignoring speed request: %dkHz exceeds %dkHz maximum",
  216. speed, JLINK_MAX_SPEED);
  217. return ERROR_OK;
  218. }
  219. /* check for RTCK setting */
  220. if (speed == 0)
  221. speed = -1;
  222. usb_out_buffer[0] = EMU_CMD_SET_SPEED;
  223. usb_out_buffer[1] = (speed >> 0) & 0xff;
  224. usb_out_buffer[2] = (speed >> 8) & 0xff;
  225. result = jlink_usb_write(jlink_jtag_handle, 3);
  226. if (result != 3)
  227. {
  228. LOG_ERROR("J-Link setting speed failed (%d)", result);
  229. return ERROR_JTAG_DEVICE_ERROR;
  230. }
  231. return ERROR_OK;
  232. }
  233. static int jlink_speed_div(int speed, int* khz)
  234. {
  235. *khz = speed;
  236. return ERROR_OK;
  237. }
  238. static int jlink_khz(int khz, int *jtag_speed)
  239. {
  240. *jtag_speed = khz;
  241. return ERROR_OK;
  242. }
  243. static int jlink_register_commands(struct command_context_s *cmd_ctx)
  244. {
  245. register_command(cmd_ctx, NULL, "jlink_info",
  246. &jlink_handle_jlink_info_command, COMMAND_EXEC,
  247. "query jlink info");
  248. register_command(cmd_ctx, NULL, "jlink_hw_jtag",
  249. &jlink_handle_jlink_hw_jtag_command, COMMAND_EXEC,
  250. "set/get jlink hw jtag command version [2|3]");
  251. return ERROR_OK;
  252. }
  253. static int jlink_init(void)
  254. {
  255. int check_cnt;
  256. jlink_jtag_handle = jlink_usb_open();
  257. if (jlink_jtag_handle == 0)
  258. {
  259. LOG_ERROR("Cannot find jlink Interface! Please check connection and permissions.");
  260. return ERROR_JTAG_INIT_FAILED;
  261. }
  262. jlink_hw_jtag_version = 2;
  263. check_cnt = 0;
  264. while (check_cnt < 3)
  265. {
  266. if (jlink_get_version_info() == ERROR_OK)
  267. {
  268. /* attempt to get status */
  269. jlink_get_status();
  270. break;
  271. }
  272. check_cnt++;
  273. }
  274. if (check_cnt == 3)
  275. {
  276. LOG_INFO("J-Link initial read failed, don't worry");
  277. }
  278. LOG_INFO("J-Link JTAG Interface ready");
  279. jlink_reset(0, 0);
  280. jtag_sleep(3000);
  281. jlink_tap_init();
  282. jlink_speed(jtag_speed);
  283. return ERROR_OK;
  284. }
  285. static int jlink_quit(void)
  286. {
  287. jlink_usb_close(jlink_jtag_handle);
  288. return ERROR_OK;
  289. }
  290. /***************************************************************************/
  291. /* Queue command implementations */
  292. static void jlink_end_state(tap_state_t state)
  293. {
  294. if (tap_is_state_stable(state))
  295. {
  296. tap_set_end_state(state);
  297. }
  298. else
  299. {
  300. LOG_ERROR("BUG: %i is not a valid end state", state);
  301. exit(-1);
  302. }
  303. }
  304. /* Goes to the end state. */
  305. static void jlink_state_move(void)
  306. {
  307. int i;
  308. int tms = 0;
  309. u8 tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
  310. u8 tms_scan_bits = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
  311. for (i = 0; i < tms_scan_bits; i++)
  312. {
  313. tms = (tms_scan >> i) & 1;
  314. jlink_tap_append_step(tms, 0);
  315. }
  316. tap_set_state(tap_get_end_state());
  317. }
  318. static void jlink_path_move(int num_states, tap_state_t *path)
  319. {
  320. int i;
  321. for (i = 0; i < num_states; i++)
  322. {
  323. if (path[i] == tap_state_transition(tap_get_state(), false))
  324. {
  325. jlink_tap_append_step(0, 0);
  326. }
  327. else if (path[i] == tap_state_transition(tap_get_state(), true))
  328. {
  329. jlink_tap_append_step(1, 0);
  330. }
  331. else
  332. {
  333. LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path[i]));
  334. exit(-1);
  335. }
  336. tap_set_state(path[i]);
  337. }
  338. tap_set_end_state(tap_get_state());
  339. }
  340. static void jlink_runtest(int num_cycles)
  341. {
  342. int i;
  343. tap_state_t saved_end_state = tap_get_end_state();
  344. jlink_tap_ensure_space(1,num_cycles + 16);
  345. /* only do a state_move when we're not already in IDLE */
  346. if (tap_get_state() != TAP_IDLE)
  347. {
  348. jlink_end_state(TAP_IDLE);
  349. jlink_state_move();
  350. // num_cycles--;
  351. }
  352. /* execute num_cycles */
  353. for (i = 0; i < num_cycles; i++)
  354. {
  355. jlink_tap_append_step(0, 0);
  356. }
  357. /* finish in end_state */
  358. jlink_end_state(saved_end_state);
  359. if (tap_get_state() != tap_get_end_state())
  360. {
  361. jlink_state_move();
  362. }
  363. }
  364. static void jlink_scan(bool ir_scan, enum scan_type type, u8 *buffer, int scan_size, scan_command_t *command)
  365. {
  366. tap_state_t saved_end_state;
  367. jlink_tap_ensure_space(1, scan_size + 16);
  368. saved_end_state = tap_get_end_state();
  369. /* Move to appropriate scan state */
  370. jlink_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
  371. /* Only move if we're not already there */
  372. if (tap_get_state() != tap_get_end_state())
  373. jlink_state_move();
  374. jlink_end_state(saved_end_state);
  375. /* Scan */
  376. jlink_tap_append_scan(scan_size, buffer, command);
  377. /* We are in Exit1, go to Pause */
  378. jlink_tap_append_step(0, 0);
  379. tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
  380. if (tap_get_state() != tap_get_end_state())
  381. {
  382. jlink_state_move();
  383. }
  384. }
  385. static void jlink_reset(int trst, int srst)
  386. {
  387. LOG_DEBUG("trst: %i, srst: %i", trst, srst);
  388. /* Signals are active low */
  389. if (srst == 0)
  390. {
  391. jlink_simple_command(EMU_CMD_HW_RESET1);
  392. }
  393. if (srst == 1)
  394. {
  395. jlink_simple_command(EMU_CMD_HW_RESET0);
  396. }
  397. if (trst == 1)
  398. {
  399. jlink_simple_command(EMU_CMD_HW_TRST0);
  400. }
  401. if (trst == 0)
  402. {
  403. jlink_simple_command(EMU_CMD_HW_TRST1);
  404. jtag_sleep(5000);
  405. jlink_end_state(TAP_RESET);
  406. jlink_state_move();
  407. }
  408. }
  409. static void jlink_simple_command(u8 command)
  410. {
  411. int result;
  412. DEBUG_JTAG_IO("0x%02x", command);
  413. usb_out_buffer[0] = command;
  414. result = jlink_usb_write(jlink_jtag_handle, 1);
  415. if (result != 1)
  416. {
  417. LOG_ERROR("J-Link command 0x%02x failed (%d)", command, result);
  418. }
  419. }
  420. static int jlink_get_status(void)
  421. {
  422. int result;
  423. jlink_simple_command(EMU_CMD_GET_STATE);
  424. result = jlink_usb_read(jlink_jtag_handle, 8);
  425. if (result != 8)
  426. {
  427. LOG_ERROR("J-Link command EMU_CMD_GET_STATE failed (%d)\n", result);
  428. return ERROR_JTAG_DEVICE_ERROR;
  429. }
  430. int vref = usb_in_buffer[0] + (usb_in_buffer[1] << 8);
  431. LOG_INFO("Vref = %d.%d TCK = %d TDI = %d TDO = %d TMS = %d SRST = %d TRST = %d\n", \
  432. vref / 1000, vref % 1000, \
  433. usb_in_buffer[2], usb_in_buffer[3], usb_in_buffer[4], \
  434. usb_in_buffer[5], usb_in_buffer[6], usb_in_buffer[7]);
  435. if (vref < 1500)
  436. LOG_ERROR("Vref too low. Check Target Power\n");
  437. return ERROR_OK;
  438. }
  439. static int jlink_get_version_info(void)
  440. {
  441. int result;
  442. int len;
  443. u32 jlink_caps, jlink_max_size;
  444. /* query hardware version */
  445. jlink_simple_command(EMU_CMD_VERSION);
  446. result = jlink_usb_read(jlink_jtag_handle, 2);
  447. if (2 != result)
  448. {
  449. LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)\n", result);
  450. return ERROR_JTAG_DEVICE_ERROR;
  451. }
  452. len = buf_get_u32(usb_in_buffer, 0, 16);
  453. if (len > JLINK_IN_BUFFER_SIZE)
  454. {
  455. LOG_ERROR("J-Link command EMU_CMD_VERSION impossible return length 0x%0x", len);
  456. len = JLINK_IN_BUFFER_SIZE;
  457. }
  458. result = jlink_usb_read(jlink_jtag_handle, len);
  459. if (result != len)
  460. {
  461. LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)\n", result);
  462. return ERROR_JTAG_DEVICE_ERROR;
  463. }
  464. usb_in_buffer[result] = 0;
  465. LOG_INFO("%s", (char *)usb_in_buffer);
  466. /* query hardware capabilities */
  467. jlink_simple_command(EMU_CMD_GET_CAPS);
  468. result = jlink_usb_read(jlink_jtag_handle, 4);
  469. if (4 != result)
  470. {
  471. LOG_ERROR("J-Link command EMU_CMD_GET_CAPS failed (%d)\n", result);
  472. return ERROR_JTAG_DEVICE_ERROR;
  473. }
  474. jlink_caps = buf_get_u32(usb_in_buffer, 0, 32);
  475. LOG_INFO("JLink caps 0x%x", jlink_caps);
  476. if (jlink_caps & (1 << EMU_CAP_GET_HW_VERSION))
  477. {
  478. /* query hardware version */
  479. jlink_simple_command(EMU_CMD_GET_HW_VERSION);
  480. result = jlink_usb_read(jlink_jtag_handle, 4);
  481. if (4 != result)
  482. {
  483. LOG_ERROR("J-Link command EMU_CMD_GET_HW_VERSION failed (%d)\n", result);
  484. return ERROR_JTAG_DEVICE_ERROR;
  485. }
  486. u32 jlink_hw_version = buf_get_u32(usb_in_buffer, 0, 32);
  487. u32 major_revision = (jlink_hw_version / 10000) % 100;
  488. if (major_revision >= 5)
  489. jlink_hw_jtag_version = 3;
  490. LOG_INFO("JLink hw version %i", jlink_hw_version);
  491. }
  492. if (jlink_caps & (1 << EMU_CAP_GET_MAX_BLOCK_SIZE))
  493. {
  494. /* query hardware maximum memory block */
  495. jlink_simple_command(EMU_CMD_GET_MAX_MEM_BLOCK);
  496. result = jlink_usb_read(jlink_jtag_handle, 4);
  497. if (4 != result)
  498. {
  499. LOG_ERROR("J-Link command EMU_CMD_GET_MAX_MEM_BLOCK failed (%d)\n", result);
  500. return ERROR_JTAG_DEVICE_ERROR;
  501. }
  502. jlink_max_size = buf_get_u32(usb_in_buffer, 0, 32);
  503. LOG_INFO("JLink max mem block %i", jlink_max_size);
  504. }
  505. return ERROR_OK;
  506. }
  507. static int jlink_handle_jlink_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  508. {
  509. if (jlink_get_version_info() == ERROR_OK)
  510. {
  511. /* attempt to get status */
  512. jlink_get_status();
  513. }
  514. return ERROR_OK;
  515. }
  516. static int jlink_handle_jlink_hw_jtag_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
  517. {
  518. switch (argc) {
  519. case 0:
  520. command_print(cmd_ctx, "jlink hw jtag %i", jlink_hw_jtag_version);
  521. break;
  522. case 1: {
  523. int request_version = atoi(args[0]);
  524. switch (request_version) {
  525. case 2: case 3:
  526. jlink_hw_jtag_version = request_version;
  527. break;
  528. default:
  529. return ERROR_COMMAND_SYNTAX_ERROR;
  530. }
  531. break;
  532. }
  533. default:
  534. return ERROR_COMMAND_SYNTAX_ERROR;
  535. }
  536. return ERROR_OK;
  537. }
  538. /***************************************************************************/
  539. /* J-Link tap functions */
  540. static unsigned tap_length=0;
  541. static u8 tms_buffer[JLINK_TAP_BUFFER_SIZE];
  542. static u8 tdi_buffer[JLINK_TAP_BUFFER_SIZE];
  543. static u8 tdo_buffer[JLINK_TAP_BUFFER_SIZE];
  544. typedef struct
  545. {
  546. int first; /* First bit position in tdo_buffer to read */
  547. int length; /* Number of bits to read */
  548. scan_command_t *command; /* Corresponding scan command */
  549. u8 *buffer;
  550. } pending_scan_result_t;
  551. #define MAX_PENDING_SCAN_RESULTS 256
  552. static int pending_scan_results_length;
  553. static pending_scan_result_t pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
  554. static void jlink_tap_init(void)
  555. {
  556. tap_length = 0;
  557. pending_scan_results_length = 0;
  558. }
  559. static void jlink_tap_ensure_space(int scans, int bits)
  560. {
  561. int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
  562. int available_bits = JLINK_TAP_BUFFER_SIZE * 8 - tap_length - 32;
  563. if (scans > available_scans || bits > available_bits)
  564. {
  565. jlink_tap_execute();
  566. }
  567. }
  568. static void jlink_tap_append_step(int tms, int tdi)
  569. {
  570. int index = tap_length / 8;
  571. if (index >= JLINK_TAP_BUFFER_SIZE)
  572. {
  573. LOG_ERROR("jlink_tap_append_step: overflow");
  574. *(u32 *)0xFFFFFFFF = 0;
  575. exit(-1);
  576. }
  577. int bit_index = tap_length % 8;
  578. u8 bit = 1 << bit_index;
  579. // we do not pad TMS, so be sure to initialize all bits
  580. if (0 == bit_index)
  581. {
  582. tms_buffer[index] = tdi_buffer[index] = 0;
  583. }
  584. if (tms)
  585. tms_buffer[index] |= bit;
  586. else
  587. tms_buffer[index] &= ~bit;
  588. if (tdi)
  589. tdi_buffer[index] |= bit;
  590. else
  591. tdi_buffer[index] &= ~bit;
  592. tap_length++;
  593. }
  594. static void jlink_tap_append_scan(int length, u8 *buffer, scan_command_t *command)
  595. {
  596. pending_scan_result_t *pending_scan_result =
  597. &pending_scan_results_buffer[pending_scan_results_length];
  598. int i;
  599. pending_scan_result->first = tap_length;
  600. pending_scan_result->length = length;
  601. pending_scan_result->command = command;
  602. pending_scan_result->buffer = buffer;
  603. for (i = 0; i < length; i++)
  604. {
  605. int tms = (i < (length - 1)) ? 0 : 1;
  606. int tdi = (buffer[i / 8] & (1 << (i % 8)))!=0;
  607. jlink_tap_append_step(tms, tdi);
  608. }
  609. pending_scan_results_length++;
  610. }
  611. /* Pad and send a tap sequence to the device, and receive the answer.
  612. * For the purpose of padding we assume that we are in idle or pause state. */
  613. static int jlink_tap_execute(void)
  614. {
  615. int byte_length;
  616. int i;
  617. int result;
  618. if (!tap_length)
  619. return ERROR_OK;
  620. /* JLink returns an extra NULL in packet when size of in message is a multiple of 64, creates problems with usb comms */
  621. /* WARNING This will interfere with tap state counting */
  622. while ((TAP_SCAN_BYTES(tap_length)%64)==0)
  623. {
  624. jlink_tap_append_step((tap_get_state() == TAP_RESET)?1:0, 0);
  625. }
  626. // number of full bytes (plus one if some would be left over)
  627. byte_length = TAP_SCAN_BYTES(tap_length);
  628. bool use_jtag3 = jlink_hw_jtag_version >= 3;
  629. usb_out_buffer[0] = use_jtag3 ? EMU_CMD_HW_JTAG3 : EMU_CMD_HW_JTAG2;
  630. usb_out_buffer[1] = 0;
  631. usb_out_buffer[2] = (tap_length >> 0) & 0xff;
  632. usb_out_buffer[3] = (tap_length >> 8) & 0xff;
  633. memcpy(usb_out_buffer + 4, tms_buffer, byte_length);
  634. memcpy(usb_out_buffer + 4 + byte_length, tdi_buffer, byte_length);
  635. jlink_last_state = jtag_debug_state_machine(tms_buffer, tdi_buffer,
  636. tap_length, jlink_last_state);
  637. result = jlink_usb_message(jlink_jtag_handle, 4 + 2 * byte_length, byte_length);
  638. if (result != byte_length)
  639. {
  640. LOG_ERROR("jlink_tap_execute, wrong result %d (expected %d)", result, byte_length);
  641. jlink_tap_init();
  642. return ERROR_JTAG_QUEUE_FAILED;
  643. }
  644. memcpy(tdo_buffer, usb_in_buffer, byte_length);
  645. for (i = 0; i < pending_scan_results_length; i++)
  646. {
  647. pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[i];
  648. u8 *buffer = pending_scan_result->buffer;
  649. int length = pending_scan_result->length;
  650. int first = pending_scan_result->first;
  651. scan_command_t *command = pending_scan_result->command;
  652. /* Copy to buffer */
  653. buf_set_buf(tdo_buffer, first, buffer, 0, length);
  654. DEBUG_JTAG_IO("pending scan result, length = %d", length);
  655. #ifdef _DEBUG_USB_COMMS_
  656. jlink_debug_buffer(buffer, TAP_SCAN_BYTES(length));
  657. #endif
  658. if (jtag_read_buffer(buffer, command) != ERROR_OK)
  659. {
  660. jlink_tap_init();
  661. return ERROR_JTAG_QUEUE_FAILED;
  662. }
  663. if (pending_scan_result->buffer != NULL)
  664. {
  665. free(pending_scan_result->buffer);
  666. }
  667. }
  668. jlink_tap_init();
  669. return ERROR_OK;
  670. }
  671. /*****************************************************************************/
  672. /* JLink USB low-level functions */
  673. static jlink_jtag_t* jlink_usb_open()
  674. {
  675. struct usb_bus *busses;
  676. struct usb_bus *bus;
  677. struct usb_device *dev;
  678. jlink_jtag_t *result;
  679. result = (jlink_jtag_t*) malloc(sizeof(jlink_jtag_t));
  680. usb_init();
  681. usb_find_busses();
  682. usb_find_devices();
  683. busses = usb_get_busses();
  684. /* find jlink_jtag device in usb bus */
  685. for (bus = busses; bus; bus = bus->next)
  686. {
  687. for (dev = bus->devices; dev; dev = dev->next)
  688. {
  689. if ((dev->descriptor.idVendor == VID) && (dev->descriptor.idProduct == PID))
  690. {
  691. result->usb_handle = usb_open(dev);
  692. /* usb_set_configuration required under win32 */
  693. usb_set_configuration(result->usb_handle, dev->config[0].bConfigurationValue);
  694. usb_claim_interface(result->usb_handle, 0);
  695. #if 0
  696. /*
  697. * This makes problems under Mac OS X. And is not needed
  698. * under Windows. Hopefully this will not break a linux build
  699. */
  700. usb_set_altinterface(result->usb_handle, 0);
  701. #endif
  702. struct usb_interface *iface = dev->config->interface;
  703. struct usb_interface_descriptor *desc = iface->altsetting;
  704. for (int i = 0; i < desc->bNumEndpoints; i++)
  705. {
  706. u8 epnum = desc->endpoint[i].bEndpointAddress;
  707. bool is_input = epnum & 0x80;
  708. LOG_DEBUG("usb ep %s %02x", is_input ? "in" : "out", epnum);
  709. if (is_input)
  710. jlink_read_ep = epnum;
  711. else
  712. jlink_write_ep = epnum;
  713. }
  714. return result;
  715. }
  716. }
  717. }
  718. free(result);
  719. return NULL;
  720. }
  721. static void jlink_usb_close(jlink_jtag_t *jlink_jtag)
  722. {
  723. usb_close(jlink_jtag->usb_handle);
  724. free(jlink_jtag);
  725. }
  726. /* Send a message and receive the reply. */
  727. static int jlink_usb_message(jlink_jtag_t *jlink_jtag, int out_length, int in_length)
  728. {
  729. int result;
  730. result = jlink_usb_write(jlink_jtag, out_length);
  731. if (result != out_length)
  732. {
  733. LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)",
  734. out_length, result);
  735. return ERROR_JTAG_DEVICE_ERROR;
  736. }
  737. result = jlink_usb_read(jlink_jtag, in_length);
  738. if ((result != in_length) && (result != (in_length + 1)))
  739. {
  740. LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)",
  741. in_length, result);
  742. return ERROR_JTAG_DEVICE_ERROR;
  743. }
  744. if (jlink_hw_jtag_version < 3)
  745. return result;
  746. int result2 = ERROR_OK;
  747. if (result == in_length)
  748. {
  749. /* Must read the result from the EMU too */
  750. result2 = jlink_usb_read_emu_result(jlink_jtag);
  751. if (1 != result2)
  752. {
  753. LOG_ERROR("jlink_usb_read_emu_result retried requested=1, result=%d, in_length=%i", result2,in_length);
  754. /* Try again once, should only happen if (in_length%64==0) */
  755. result2 = jlink_usb_read_emu_result(jlink_jtag);
  756. if (1 != result2)
  757. {
  758. LOG_ERROR("jlink_usb_read_emu_result failed "
  759. "(requested=1, result=%d)", result2);
  760. return ERROR_JTAG_DEVICE_ERROR;
  761. }
  762. }
  763. /* Check the result itself */
  764. result2 = usb_emu_result_buffer[0];
  765. }
  766. else
  767. {
  768. /* Save the result, then remove it from return value */
  769. result2 = usb_in_buffer[result--];
  770. }
  771. if (result2)
  772. {
  773. LOG_ERROR("jlink_usb_message failed with result=%d)", result2);
  774. return ERROR_JTAG_DEVICE_ERROR;
  775. }
  776. return result;
  777. }
  778. /* calls the given usb_bulk_* function, allowing for the data to trickle in with some timeouts */
  779. static int usb_bulk_with_retries(
  780. int (*f)(usb_dev_handle *, int, char *, int, int),
  781. usb_dev_handle *dev, int ep,
  782. char *bytes, int size, int timeout)
  783. {
  784. int tries = 3, count = 0;
  785. while (tries && (count < size))
  786. {
  787. int result = f(dev, ep, bytes + count, size - count, timeout);
  788. if (result > 0)
  789. count += result;
  790. else if ((-ETIMEDOUT != result) || !--tries)
  791. return result;
  792. }
  793. return count;
  794. }
  795. static int wrap_usb_bulk_write(usb_dev_handle *dev, int ep,
  796. char *buff, int size, int timeout)
  797. {
  798. /* usb_bulk_write() takes const char *buff */
  799. return usb_bulk_write(dev, ep, buff, size, timeout);
  800. }
  801. static inline int usb_bulk_write_ex(usb_dev_handle *dev, int ep,
  802. char *bytes, int size, int timeout)
  803. {
  804. return usb_bulk_with_retries(&wrap_usb_bulk_write,
  805. dev, ep, bytes, size, timeout);
  806. }
  807. static inline int usb_bulk_read_ex(usb_dev_handle *dev, int ep,
  808. char *bytes, int size, int timeout)
  809. {
  810. return usb_bulk_with_retries(&usb_bulk_read,
  811. dev, ep, bytes, size, timeout);
  812. }
  813. /* Write data from out_buffer to USB. */
  814. static int jlink_usb_write(jlink_jtag_t *jlink_jtag, int out_length)
  815. {
  816. int result;
  817. if (out_length > JLINK_OUT_BUFFER_SIZE)
  818. {
  819. LOG_ERROR("jlink_jtag_write illegal out_length=%d (max=%d)", out_length, JLINK_OUT_BUFFER_SIZE);
  820. return -1;
  821. }
  822. result = usb_bulk_write_ex(jlink_jtag->usb_handle, jlink_write_ep,
  823. (char *)usb_out_buffer, out_length, JLINK_USB_TIMEOUT);
  824. DEBUG_JTAG_IO("jlink_usb_write, out_length = %d, result = %d", out_length, result);
  825. #ifdef _DEBUG_USB_COMMS_
  826. jlink_debug_buffer(usb_out_buffer, out_length);
  827. #endif
  828. return result;
  829. }
  830. /* Read data from USB into in_buffer. */
  831. static int jlink_usb_read(jlink_jtag_t *jlink_jtag, int expected_size)
  832. {
  833. int result = usb_bulk_read_ex(jlink_jtag->usb_handle, jlink_read_ep,
  834. (char *)usb_in_buffer, expected_size, JLINK_USB_TIMEOUT);
  835. DEBUG_JTAG_IO("jlink_usb_read, result = %d", result);
  836. #ifdef _DEBUG_USB_COMMS_
  837. jlink_debug_buffer(usb_in_buffer, result);
  838. #endif
  839. return result;
  840. }
  841. /* Read the result from the previous EMU cmd into result_buffer. */
  842. static int jlink_usb_read_emu_result(jlink_jtag_t *jlink_jtag)
  843. {
  844. int result = usb_bulk_read_ex(jlink_jtag->usb_handle, jlink_read_ep,
  845. (char *)usb_emu_result_buffer, 1 /* JLINK_EMU_RESULT_BUFFER_SIZE */,
  846. JLINK_USB_TIMEOUT);
  847. DEBUG_JTAG_IO("jlink_usb_read_result, result = %d", result);
  848. #ifdef _DEBUG_USB_COMMS_
  849. jlink_debug_buffer(usb_emu_result_buffer, result);
  850. #endif
  851. return result;
  852. }
  853. #ifdef _DEBUG_USB_COMMS_
  854. #define BYTES_PER_LINE 16
  855. static void jlink_debug_buffer(u8 *buffer, int length)
  856. {
  857. char line[81];
  858. char s[4];
  859. int i;
  860. int j;
  861. for (i = 0; i < length; i += BYTES_PER_LINE)
  862. {
  863. snprintf(line, 5, "%04x", i);
  864. for (j = i; j < i + BYTES_PER_LINE && j < length; j++)
  865. {
  866. snprintf(s, 4, " %02x", buffer[j]);
  867. strcat(line, s);
  868. }
  869. LOG_DEBUG("%s", line);
  870. }
  871. }
  872. #endif