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.
 
 
 
 
 
 

1086 lines
28 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. #include "interface.h"
  27. #include <jtag/commands.h>
  28. #include "usb_common.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 uint8_t usb_in_buffer[JLINK_IN_BUFFER_SIZE];
  47. static uint8_t usb_out_buffer[JLINK_OUT_BUFFER_SIZE];
  48. static uint8_t 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. /* Queue command functions */
  71. static void jlink_end_state(tap_state_t state);
  72. static void jlink_state_move(void);
  73. static void jlink_path_move(int num_states, tap_state_t *path);
  74. static void jlink_runtest(int num_cycles);
  75. static void jlink_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size, struct scan_command *command);
  76. static void jlink_reset(int trst, int srst);
  77. static void jlink_simple_command(uint8_t command);
  78. static int jlink_get_status(void);
  79. /* J-Link tap buffer functions */
  80. static void jlink_tap_init(void);
  81. static int jlink_tap_execute(void);
  82. static void jlink_tap_ensure_space(int scans, int bits);
  83. static void jlink_tap_append_step(int tms, int tdi);
  84. static void jlink_tap_append_scan(int length, uint8_t *buffer, struct scan_command *command);
  85. /* Jlink lowlevel functions */
  86. struct jlink {
  87. struct usb_dev_handle* usb_handle;
  88. };
  89. static struct jlink *jlink_usb_open(void);
  90. static void jlink_usb_close(struct jlink *jlink);
  91. static int jlink_usb_message(struct jlink *jlink, int out_length, int in_length);
  92. static int jlink_usb_write(struct jlink *jlink, int out_length);
  93. static int jlink_usb_read(struct jlink *jlink, int expected_size);
  94. static int jlink_usb_read_emu_result(struct jlink *jlink);
  95. /* helper functions */
  96. static int jlink_get_version_info(void);
  97. #ifdef _DEBUG_USB_COMMS_
  98. static void jlink_debug_buffer(uint8_t *buffer, int length);
  99. #endif
  100. static enum tap_state jlink_last_state = TAP_RESET;
  101. static struct jlink* jlink_handle;
  102. /***************************************************************************/
  103. /* External interface implementation */
  104. static void jlink_execute_runtest(struct jtag_command *cmd)
  105. {
  106. DEBUG_JTAG_IO("runtest %i cycles, end in %i",
  107. cmd->cmd.runtest->num_cycles,
  108. cmd->cmd.runtest->end_state);
  109. jlink_end_state(cmd->cmd.runtest->end_state);
  110. jlink_runtest(cmd->cmd.runtest->num_cycles);
  111. }
  112. static void jlink_execute_statemove(struct jtag_command *cmd)
  113. {
  114. DEBUG_JTAG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
  115. jlink_end_state(cmd->cmd.statemove->end_state);
  116. jlink_state_move();
  117. }
  118. static void jlink_execute_pathmove(struct jtag_command *cmd)
  119. {
  120. DEBUG_JTAG_IO("pathmove: %i states, end in %i",
  121. cmd->cmd.pathmove->num_states,
  122. cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
  123. jlink_path_move(cmd->cmd.pathmove->num_states,
  124. cmd->cmd.pathmove->path);
  125. }
  126. static void jlink_execute_scan(struct jtag_command *cmd)
  127. {
  128. int scan_size;
  129. enum scan_type type;
  130. uint8_t *buffer;
  131. DEBUG_JTAG_IO("scan end in %s", tap_state_name(cmd->cmd.scan->end_state));
  132. jlink_end_state(cmd->cmd.scan->end_state);
  133. scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
  134. DEBUG_JTAG_IO("scan input, length = %d", scan_size);
  135. #ifdef _DEBUG_USB_COMMS_
  136. jlink_debug_buffer(buffer, (scan_size + 7) / 8);
  137. #endif
  138. type = jtag_scan_type(cmd->cmd.scan);
  139. jlink_scan(cmd->cmd.scan->ir_scan,
  140. type, buffer, scan_size, cmd->cmd.scan);
  141. }
  142. static void jlink_execute_reset(struct jtag_command *cmd)
  143. {
  144. DEBUG_JTAG_IO("reset trst: %i srst %i",
  145. cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  146. jlink_tap_execute();
  147. jlink_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  148. jlink_tap_execute();
  149. }
  150. static void jlink_execute_sleep(struct jtag_command *cmd)
  151. {
  152. DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
  153. jlink_tap_execute();
  154. jtag_sleep(cmd->cmd.sleep->us);
  155. }
  156. static void jlink_execute_command(struct jtag_command *cmd)
  157. {
  158. switch (cmd->type)
  159. {
  160. case JTAG_RUNTEST: jlink_execute_runtest(cmd); break;
  161. case JTAG_STATEMOVE: jlink_execute_statemove(cmd); break;
  162. case JTAG_PATHMOVE: jlink_execute_pathmove(cmd); break;
  163. case JTAG_SCAN: jlink_execute_scan(cmd); break;
  164. case JTAG_RESET: jlink_execute_reset(cmd); break;
  165. case JTAG_SLEEP: jlink_execute_sleep(cmd); break;
  166. default:
  167. LOG_ERROR("BUG: unknown JTAG command type encountered");
  168. exit(-1);
  169. }
  170. }
  171. static int jlink_execute_queue(void)
  172. {
  173. struct jtag_command *cmd = jtag_command_queue;
  174. while (cmd != NULL)
  175. {
  176. jlink_execute_command(cmd);
  177. cmd = cmd->next;
  178. }
  179. return jlink_tap_execute();
  180. }
  181. /* Sets speed in kHz. */
  182. static int jlink_speed(int speed)
  183. {
  184. int result;
  185. if (speed > JLINK_MAX_SPEED)
  186. {
  187. LOG_INFO("Ignoring speed request: %dkHz exceeds %dkHz maximum",
  188. speed, JLINK_MAX_SPEED);
  189. return ERROR_OK;
  190. }
  191. /* check for RTCK setting */
  192. if (speed == 0)
  193. speed = -1;
  194. usb_out_buffer[0] = EMU_CMD_SET_SPEED;
  195. usb_out_buffer[1] = (speed >> 0) & 0xff;
  196. usb_out_buffer[2] = (speed >> 8) & 0xff;
  197. result = jlink_usb_write(jlink_handle, 3);
  198. if (result != 3)
  199. {
  200. LOG_ERROR("J-Link setting speed failed (%d)", result);
  201. return ERROR_JTAG_DEVICE_ERROR;
  202. }
  203. return ERROR_OK;
  204. }
  205. static int jlink_speed_div(int speed, int* khz)
  206. {
  207. *khz = speed;
  208. return ERROR_OK;
  209. }
  210. static int jlink_khz(int khz, int *jtag_speed)
  211. {
  212. *jtag_speed = khz;
  213. return ERROR_OK;
  214. }
  215. static int jlink_init(void)
  216. {
  217. int i;
  218. jlink_handle = jlink_usb_open();
  219. if (jlink_handle == 0)
  220. {
  221. LOG_ERROR("Cannot find jlink Interface! Please check connection and permissions.");
  222. return ERROR_JTAG_INIT_FAILED;
  223. }
  224. /*
  225. * The next three instructions were added after discovering a problem while using an oscilloscope. For the V8
  226. * SAM-ICE dongle (and likely other j-link device variants), the reset line to the target microprocessor was found to
  227. * cycle only intermittently during emulator startup (even after encountering the downstream reset instruction later
  228. * in the code). This was found to create two issues: 1) In general it is a bad practice to not reset a CPU to a known
  229. * state when starting an emulator and 2) something critical happens inside the dongle when it does the first read
  230. * following a new USB session. Keeping the processor in reset during the first read collecting version information
  231. * seems to prevent errant "J-Link command EMU_CMD_VERSION failed" issues.
  232. */
  233. LOG_INFO("J-Link initialization started / target CPU reset initiated");
  234. jlink_simple_command(EMU_CMD_HW_TRST0);
  235. jlink_simple_command(EMU_CMD_HW_RESET0);
  236. usleep(1000);
  237. jlink_hw_jtag_version = 2;
  238. if (jlink_get_version_info() == ERROR_OK)
  239. {
  240. /* attempt to get status */
  241. jlink_get_status();
  242. }
  243. LOG_INFO("J-Link JTAG Interface ready");
  244. jlink_reset(0, 0);
  245. jtag_sleep(3000);
  246. jlink_tap_init();
  247. jlink_speed(jtag_get_speed());
  248. /* v5/6 jlink seems to have an issue if the first tap move
  249. * is not divisible by 8, so we send a TLR on first power up */
  250. for (i = 0; i < 8; i++) {
  251. jlink_tap_append_step(1, 0);
  252. }
  253. jlink_tap_execute();
  254. return ERROR_OK;
  255. }
  256. static int jlink_quit(void)
  257. {
  258. jlink_usb_close(jlink_handle);
  259. return ERROR_OK;
  260. }
  261. /***************************************************************************/
  262. /* Queue command implementations */
  263. static void jlink_end_state(tap_state_t state)
  264. {
  265. if (tap_is_state_stable(state))
  266. {
  267. tap_set_end_state(state);
  268. }
  269. else
  270. {
  271. LOG_ERROR("BUG: %i is not a valid end state", state);
  272. exit(-1);
  273. }
  274. }
  275. /* Goes to the end state. */
  276. static void jlink_state_move(void)
  277. {
  278. int i;
  279. int tms = 0;
  280. uint8_t tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
  281. uint8_t tms_scan_bits = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
  282. for (i = 0; i < tms_scan_bits; i++)
  283. {
  284. tms = (tms_scan >> i) & 1;
  285. jlink_tap_append_step(tms, 0);
  286. }
  287. tap_set_state(tap_get_end_state());
  288. }
  289. static void jlink_path_move(int num_states, tap_state_t *path)
  290. {
  291. int i;
  292. for (i = 0; i < num_states; i++)
  293. {
  294. if (path[i] == tap_state_transition(tap_get_state(), false))
  295. {
  296. jlink_tap_append_step(0, 0);
  297. }
  298. else if (path[i] == tap_state_transition(tap_get_state(), true))
  299. {
  300. jlink_tap_append_step(1, 0);
  301. }
  302. else
  303. {
  304. LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path[i]));
  305. exit(-1);
  306. }
  307. tap_set_state(path[i]);
  308. }
  309. tap_set_end_state(tap_get_state());
  310. }
  311. static void jlink_runtest(int num_cycles)
  312. {
  313. int i;
  314. tap_state_t saved_end_state = tap_get_end_state();
  315. jlink_tap_ensure_space(1,num_cycles + 16);
  316. /* only do a state_move when we're not already in IDLE */
  317. if (tap_get_state() != TAP_IDLE)
  318. {
  319. jlink_end_state(TAP_IDLE);
  320. jlink_state_move();
  321. // num_cycles--;
  322. }
  323. /* execute num_cycles */
  324. for (i = 0; i < num_cycles; i++)
  325. {
  326. jlink_tap_append_step(0, 0);
  327. }
  328. /* finish in end_state */
  329. jlink_end_state(saved_end_state);
  330. if (tap_get_state() != tap_get_end_state())
  331. {
  332. jlink_state_move();
  333. }
  334. }
  335. static void jlink_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size, struct scan_command *command)
  336. {
  337. tap_state_t saved_end_state;
  338. jlink_tap_ensure_space(1, scan_size + 16);
  339. saved_end_state = tap_get_end_state();
  340. /* Move to appropriate scan state */
  341. jlink_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
  342. /* Only move if we're not already there */
  343. if (tap_get_state() != tap_get_end_state())
  344. jlink_state_move();
  345. jlink_end_state(saved_end_state);
  346. /* Scan */
  347. jlink_tap_append_scan(scan_size, buffer, command);
  348. /* We are in Exit1, go to Pause */
  349. jlink_tap_append_step(0, 0);
  350. tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
  351. if (tap_get_state() != tap_get_end_state())
  352. {
  353. jlink_state_move();
  354. }
  355. }
  356. static void jlink_reset(int trst, int srst)
  357. {
  358. LOG_DEBUG("trst: %i, srst: %i", trst, srst);
  359. /* Signals are active low */
  360. if (srst == 0)
  361. {
  362. jlink_simple_command(EMU_CMD_HW_RESET1);
  363. }
  364. if (srst == 1)
  365. {
  366. jlink_simple_command(EMU_CMD_HW_RESET0);
  367. }
  368. if (trst == 1)
  369. {
  370. jlink_simple_command(EMU_CMD_HW_TRST0);
  371. }
  372. if (trst == 0)
  373. {
  374. jlink_simple_command(EMU_CMD_HW_TRST1);
  375. }
  376. }
  377. static void jlink_simple_command(uint8_t command)
  378. {
  379. int result;
  380. DEBUG_JTAG_IO("0x%02x", command);
  381. usb_out_buffer[0] = command;
  382. result = jlink_usb_write(jlink_handle, 1);
  383. if (result != 1)
  384. {
  385. LOG_ERROR("J-Link command 0x%02x failed (%d)", command, result);
  386. }
  387. }
  388. static int jlink_get_status(void)
  389. {
  390. int result;
  391. jlink_simple_command(EMU_CMD_GET_STATE);
  392. result = jlink_usb_read(jlink_handle, 8);
  393. if (result != 8)
  394. {
  395. LOG_ERROR("J-Link command EMU_CMD_GET_STATE failed (%d)\n", result);
  396. return ERROR_JTAG_DEVICE_ERROR;
  397. }
  398. int vref = usb_in_buffer[0] + (usb_in_buffer[1] << 8);
  399. LOG_INFO("Vref = %d.%d TCK = %d TDI = %d TDO = %d TMS = %d SRST = %d TRST = %d\n", \
  400. vref / 1000, vref % 1000, \
  401. usb_in_buffer[2], usb_in_buffer[3], usb_in_buffer[4], \
  402. usb_in_buffer[5], usb_in_buffer[6], usb_in_buffer[7]);
  403. if (vref < 1500)
  404. LOG_ERROR("Vref too low. Check Target Power\n");
  405. return ERROR_OK;
  406. }
  407. static int jlink_get_version_info(void)
  408. {
  409. int result;
  410. int len;
  411. uint32_t jlink_caps, jlink_max_size;
  412. /* query hardware version */
  413. jlink_simple_command(EMU_CMD_VERSION);
  414. result = jlink_usb_read(jlink_handle, 2);
  415. if (2 != result)
  416. {
  417. LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)\n", result);
  418. return ERROR_JTAG_DEVICE_ERROR;
  419. }
  420. len = buf_get_u32(usb_in_buffer, 0, 16);
  421. if (len > JLINK_IN_BUFFER_SIZE)
  422. {
  423. LOG_ERROR("J-Link command EMU_CMD_VERSION impossible return length 0x%0x", len);
  424. len = JLINK_IN_BUFFER_SIZE;
  425. }
  426. result = jlink_usb_read(jlink_handle, len);
  427. if (result != len)
  428. {
  429. LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)\n", result);
  430. return ERROR_JTAG_DEVICE_ERROR;
  431. }
  432. usb_in_buffer[result] = 0;
  433. LOG_INFO("%s", (char *)usb_in_buffer);
  434. /* query hardware capabilities */
  435. jlink_simple_command(EMU_CMD_GET_CAPS);
  436. result = jlink_usb_read(jlink_handle, 4);
  437. if (4 != result)
  438. {
  439. LOG_ERROR("J-Link command EMU_CMD_GET_CAPS failed (%d)\n", result);
  440. return ERROR_JTAG_DEVICE_ERROR;
  441. }
  442. jlink_caps = buf_get_u32(usb_in_buffer, 0, 32);
  443. LOG_INFO("JLink caps 0x%x", (unsigned)jlink_caps);
  444. if (jlink_caps & (1 << EMU_CAP_GET_HW_VERSION))
  445. {
  446. /* query hardware version */
  447. jlink_simple_command(EMU_CMD_GET_HW_VERSION);
  448. result = jlink_usb_read(jlink_handle, 4);
  449. if (4 != result)
  450. {
  451. LOG_ERROR("J-Link command EMU_CMD_GET_HW_VERSION failed (%d)\n", result);
  452. return ERROR_JTAG_DEVICE_ERROR;
  453. }
  454. uint32_t jlink_hw_version = buf_get_u32(usb_in_buffer, 0, 32);
  455. uint32_t major_revision = (jlink_hw_version / 10000) % 100;
  456. if (major_revision >= 5)
  457. jlink_hw_jtag_version = 3;
  458. LOG_INFO("JLink hw version %i", (int)jlink_hw_version);
  459. }
  460. if (jlink_caps & (1 << EMU_CAP_GET_MAX_BLOCK_SIZE))
  461. {
  462. /* query hardware maximum memory block */
  463. jlink_simple_command(EMU_CMD_GET_MAX_MEM_BLOCK);
  464. result = jlink_usb_read(jlink_handle, 4);
  465. if (4 != result)
  466. {
  467. LOG_ERROR("J-Link command EMU_CMD_GET_MAX_MEM_BLOCK failed (%d)\n", result);
  468. return ERROR_JTAG_DEVICE_ERROR;
  469. }
  470. jlink_max_size = buf_get_u32(usb_in_buffer, 0, 32);
  471. LOG_INFO("JLink max mem block %i", (int)jlink_max_size);
  472. }
  473. return ERROR_OK;
  474. }
  475. COMMAND_HANDLER(jlink_handle_jlink_info_command)
  476. {
  477. if (jlink_get_version_info() == ERROR_OK)
  478. {
  479. /* attempt to get status */
  480. jlink_get_status();
  481. }
  482. return ERROR_OK;
  483. }
  484. COMMAND_HANDLER(jlink_handle_jlink_hw_jtag_command)
  485. {
  486. switch (CMD_ARGC) {
  487. case 0:
  488. command_print(CMD_CTX, "jlink hw jtag %i", jlink_hw_jtag_version);
  489. break;
  490. case 1: {
  491. int request_version = atoi(CMD_ARGV[0]);
  492. switch (request_version) {
  493. case 2: case 3:
  494. jlink_hw_jtag_version = request_version;
  495. break;
  496. default:
  497. return ERROR_COMMAND_SYNTAX_ERROR;
  498. }
  499. break;
  500. }
  501. default:
  502. return ERROR_COMMAND_SYNTAX_ERROR;
  503. }
  504. return ERROR_OK;
  505. }
  506. static const struct command_registration jlink_command_handlers[] = {
  507. {
  508. .name = "jlink_info",
  509. .handler = &jlink_handle_jlink_info_command,
  510. .mode = COMMAND_EXEC,
  511. .help = "show jlink info",
  512. },
  513. {
  514. .name = "jlink_hw_jtag",
  515. .handler = &jlink_handle_jlink_hw_jtag_command,
  516. .mode = COMMAND_EXEC,
  517. .help = "access J-Link HW JTAG command version",
  518. .usage = "[2|3]",
  519. },
  520. COMMAND_REGISTRATION_DONE
  521. };
  522. struct jtag_interface jlink_interface = {
  523. .name = "jlink",
  524. .commands = jlink_command_handlers,
  525. .execute_queue = &jlink_execute_queue,
  526. .speed = &jlink_speed,
  527. .speed_div = &jlink_speed_div,
  528. .khz = &jlink_khz,
  529. .init = &jlink_init,
  530. .quit = &jlink_quit,
  531. };
  532. /***************************************************************************/
  533. /* J-Link tap functions */
  534. static unsigned tap_length = 0;
  535. static uint8_t tms_buffer[JLINK_TAP_BUFFER_SIZE];
  536. static uint8_t tdi_buffer[JLINK_TAP_BUFFER_SIZE];
  537. static uint8_t tdo_buffer[JLINK_TAP_BUFFER_SIZE];
  538. struct pending_scan_result {
  539. int first; /* First bit position in tdo_buffer to read */
  540. int length; /* Number of bits to read */
  541. struct scan_command *command; /* Corresponding scan command */
  542. uint8_t *buffer;
  543. };
  544. #define MAX_PENDING_SCAN_RESULTS 256
  545. static int pending_scan_results_length;
  546. static struct pending_scan_result pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
  547. static void jlink_tap_init(void)
  548. {
  549. tap_length = 0;
  550. pending_scan_results_length = 0;
  551. }
  552. static void jlink_tap_ensure_space(int scans, int bits)
  553. {
  554. int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
  555. int available_bits = JLINK_TAP_BUFFER_SIZE * 8 - tap_length - 32;
  556. if (scans > available_scans || bits > available_bits)
  557. {
  558. jlink_tap_execute();
  559. }
  560. }
  561. static void jlink_tap_append_step(int tms, int tdi)
  562. {
  563. int index = tap_length / 8;
  564. if (index >= JLINK_TAP_BUFFER_SIZE)
  565. {
  566. LOG_ERROR("jlink_tap_append_step: overflow");
  567. *(uint32_t *)0xFFFFFFFF = 0;
  568. exit(-1);
  569. }
  570. int bit_index = tap_length % 8;
  571. uint8_t bit = 1 << bit_index;
  572. // we do not pad TMS, so be sure to initialize all bits
  573. if (0 == bit_index)
  574. {
  575. tms_buffer[index] = tdi_buffer[index] = 0;
  576. }
  577. if (tms)
  578. tms_buffer[index] |= bit;
  579. else
  580. tms_buffer[index] &= ~bit;
  581. if (tdi)
  582. tdi_buffer[index] |= bit;
  583. else
  584. tdi_buffer[index] &= ~bit;
  585. tap_length++;
  586. }
  587. static void jlink_tap_append_scan(int length, uint8_t *buffer, struct scan_command *command)
  588. {
  589. struct pending_scan_result *pending_scan_result =
  590. &pending_scan_results_buffer[pending_scan_results_length];
  591. int i;
  592. pending_scan_result->first = tap_length;
  593. pending_scan_result->length = length;
  594. pending_scan_result->command = command;
  595. pending_scan_result->buffer = buffer;
  596. for (i = 0; i < length; i++)
  597. {
  598. int tms = (i < (length - 1)) ? 0 : 1;
  599. int tdi = (buffer[i / 8] & (1 << (i % 8))) != 0;
  600. jlink_tap_append_step(tms, tdi);
  601. }
  602. pending_scan_results_length++;
  603. }
  604. /* Pad and send a tap sequence to the device, and receive the answer.
  605. * For the purpose of padding we assume that we are in idle or pause state. */
  606. static int jlink_tap_execute(void)
  607. {
  608. int byte_length;
  609. int i;
  610. int result;
  611. if (!tap_length)
  612. return ERROR_OK;
  613. /* JLink returns an extra NULL in packet when size of incoming
  614. * message is a multiple of 64, creates problems with USB comms.
  615. * WARNING: This will interfere with tap state counting. */
  616. while ((DIV_ROUND_UP(tap_length, 8) % 64) == 0)
  617. {
  618. jlink_tap_append_step((tap_get_state() == TAP_RESET)?1:0, 0);
  619. }
  620. // number of full bytes (plus one if some would be left over)
  621. byte_length = DIV_ROUND_UP(tap_length, 8);
  622. bool use_jtag3 = jlink_hw_jtag_version >= 3;
  623. usb_out_buffer[0] = use_jtag3 ? EMU_CMD_HW_JTAG3 : EMU_CMD_HW_JTAG2;
  624. usb_out_buffer[1] = 0;
  625. usb_out_buffer[2] = (tap_length >> 0) & 0xff;
  626. usb_out_buffer[3] = (tap_length >> 8) & 0xff;
  627. memcpy(usb_out_buffer + 4, tms_buffer, byte_length);
  628. memcpy(usb_out_buffer + 4 + byte_length, tdi_buffer, byte_length);
  629. jlink_last_state = jtag_debug_state_machine(tms_buffer, tdi_buffer,
  630. tap_length, jlink_last_state);
  631. result = jlink_usb_message(jlink_handle, 4 + 2 * byte_length, byte_length);
  632. if (result != byte_length)
  633. {
  634. LOG_ERROR("jlink_tap_execute, wrong result %d (expected %d)", result, byte_length);
  635. jlink_tap_init();
  636. return ERROR_JTAG_QUEUE_FAILED;
  637. }
  638. memcpy(tdo_buffer, usb_in_buffer, byte_length);
  639. for (i = 0; i < pending_scan_results_length; i++)
  640. {
  641. struct pending_scan_result *pending_scan_result = &pending_scan_results_buffer[i];
  642. uint8_t *buffer = pending_scan_result->buffer;
  643. int length = pending_scan_result->length;
  644. int first = pending_scan_result->first;
  645. struct scan_command *command = pending_scan_result->command;
  646. /* Copy to buffer */
  647. buf_set_buf(tdo_buffer, first, buffer, 0, length);
  648. DEBUG_JTAG_IO("pending scan result, length = %d", length);
  649. #ifdef _DEBUG_USB_COMMS_
  650. jlink_debug_buffer(buffer, DIV_ROUND_UP(length, 8));
  651. #endif
  652. if (jtag_read_buffer(buffer, command) != ERROR_OK)
  653. {
  654. jlink_tap_init();
  655. return ERROR_JTAG_QUEUE_FAILED;
  656. }
  657. if (pending_scan_result->buffer != NULL)
  658. {
  659. free(pending_scan_result->buffer);
  660. }
  661. }
  662. jlink_tap_init();
  663. return ERROR_OK;
  664. }
  665. /*****************************************************************************/
  666. /* JLink USB low-level functions */
  667. static struct jlink* jlink_usb_open()
  668. {
  669. usb_init();
  670. const uint16_t vids[] = { VID, 0 };
  671. const uint16_t pids[] = { PID, 0 };
  672. struct usb_dev_handle *dev;
  673. if (jtag_usb_open(vids, pids, &dev) != ERROR_OK)
  674. return NULL;
  675. /* BE ***VERY CAREFUL*** ABOUT MAKING CHANGES IN THIS
  676. * AREA!!!!!!!!!!! The behavior of libusb is not completely
  677. * consistent across Windows, Linux, and Mac OS X platforms.
  678. * The actions taken in the following compiler conditionals may
  679. * not agree with published documentation for libusb, but were
  680. * found to be necessary through trials and tribulations. Even
  681. * little tweaks can break one or more platforms, so if you do
  682. * make changes test them carefully on all platforms before
  683. * committing them!
  684. */
  685. #if IS_WIN32 == 0
  686. usb_reset(dev);
  687. #if IS_DARWIN == 0
  688. int timeout = 5;
  689. /* reopen jlink after usb_reset
  690. * on win32 this may take a second or two to re-enumerate */
  691. int retval;
  692. while ((retval = jtag_usb_open(vids, pids, &dev)) != ERROR_OK)
  693. {
  694. usleep(1000);
  695. timeout--;
  696. if (!timeout) {
  697. break;
  698. }
  699. }
  700. if (ERROR_OK != retval)
  701. return NULL;
  702. #endif
  703. #endif
  704. /* usb_set_configuration required under win32 */
  705. struct usb_device *udev = usb_device(dev);
  706. usb_set_configuration(dev, udev->config[0].bConfigurationValue);
  707. usb_claim_interface(dev, 0);
  708. #if 0
  709. /*
  710. * This makes problems under Mac OS X. And is not needed
  711. * under Windows. Hopefully this will not break a linux build
  712. */
  713. usb_set_altinterface(result->usb_handle, 0);
  714. #endif
  715. struct usb_interface *iface = udev->config->interface;
  716. struct usb_interface_descriptor *desc = iface->altsetting;
  717. for (int i = 0; i < desc->bNumEndpoints; i++)
  718. {
  719. uint8_t epnum = desc->endpoint[i].bEndpointAddress;
  720. bool is_input = epnum & 0x80;
  721. LOG_DEBUG("usb ep %s %02x", is_input ? "in" : "out", epnum);
  722. if (is_input)
  723. jlink_read_ep = epnum;
  724. else
  725. jlink_write_ep = epnum;
  726. }
  727. struct jlink *result = malloc(sizeof(struct jlink));
  728. result->usb_handle = dev;
  729. return result;
  730. }
  731. static void jlink_usb_close(struct jlink *jlink)
  732. {
  733. usb_close(jlink->usb_handle);
  734. free(jlink);
  735. }
  736. /* Send a message and receive the reply. */
  737. static int jlink_usb_message(struct jlink *jlink, int out_length, int in_length)
  738. {
  739. int result;
  740. result = jlink_usb_write(jlink, out_length);
  741. if (result != out_length)
  742. {
  743. LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)",
  744. out_length, result);
  745. return ERROR_JTAG_DEVICE_ERROR;
  746. }
  747. result = jlink_usb_read(jlink, in_length);
  748. if ((result != in_length) && (result != (in_length + 1)))
  749. {
  750. LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)",
  751. in_length, result);
  752. return ERROR_JTAG_DEVICE_ERROR;
  753. }
  754. if (jlink_hw_jtag_version < 3)
  755. return result;
  756. int result2 = ERROR_OK;
  757. if (result == in_length)
  758. {
  759. /* Must read the result from the EMU too */
  760. result2 = jlink_usb_read_emu_result(jlink);
  761. if (1 != result2)
  762. {
  763. LOG_ERROR("jlink_usb_read_emu_result retried requested = 1, result=%d, in_length=%i", result2,in_length);
  764. /* Try again once, should only happen if (in_length%64 == 0) */
  765. result2 = jlink_usb_read_emu_result(jlink);
  766. if (1 != result2)
  767. {
  768. LOG_ERROR("jlink_usb_read_emu_result failed "
  769. "(requested = 1, result=%d)", result2);
  770. return ERROR_JTAG_DEVICE_ERROR;
  771. }
  772. }
  773. /* Check the result itself */
  774. result2 = usb_emu_result_buffer[0];
  775. }
  776. else
  777. {
  778. /* Save the result, then remove it from return value */
  779. result2 = usb_in_buffer[result--];
  780. }
  781. if (result2)
  782. {
  783. LOG_ERROR("jlink_usb_message failed with result=%d)", result2);
  784. return ERROR_JTAG_DEVICE_ERROR;
  785. }
  786. return result;
  787. }
  788. /* calls the given usb_bulk_* function, allowing for the data to trickle in with some timeouts */
  789. static int usb_bulk_with_retries(
  790. int (*f)(usb_dev_handle *, int, char *, int, int),
  791. usb_dev_handle *dev, int ep,
  792. char *bytes, int size, int timeout)
  793. {
  794. int tries = 3, count = 0;
  795. while (tries && (count < size))
  796. {
  797. int result = f(dev, ep, bytes + count, size - count, timeout);
  798. if (result > 0)
  799. count += result;
  800. else if ((-ETIMEDOUT != result) || !--tries)
  801. return result;
  802. }
  803. return count;
  804. }
  805. static int wrap_usb_bulk_write(usb_dev_handle *dev, int ep,
  806. char *buff, int size, int timeout)
  807. {
  808. /* usb_bulk_write() takes const char *buff */
  809. return usb_bulk_write(dev, ep, buff, size, timeout);
  810. }
  811. static inline int usb_bulk_write_ex(usb_dev_handle *dev, int ep,
  812. char *bytes, int size, int timeout)
  813. {
  814. return usb_bulk_with_retries(&wrap_usb_bulk_write,
  815. dev, ep, bytes, size, timeout);
  816. }
  817. static inline int usb_bulk_read_ex(usb_dev_handle *dev, int ep,
  818. char *bytes, int size, int timeout)
  819. {
  820. return usb_bulk_with_retries(&usb_bulk_read,
  821. dev, ep, bytes, size, timeout);
  822. }
  823. /* Write data from out_buffer to USB. */
  824. static int jlink_usb_write(struct jlink *jlink, int out_length)
  825. {
  826. int result;
  827. if (out_length > JLINK_OUT_BUFFER_SIZE)
  828. {
  829. LOG_ERROR("jlink_write illegal out_length=%d (max=%d)", out_length, JLINK_OUT_BUFFER_SIZE);
  830. return -1;
  831. }
  832. result = usb_bulk_write_ex(jlink->usb_handle, jlink_write_ep,
  833. (char *)usb_out_buffer, out_length, JLINK_USB_TIMEOUT);
  834. DEBUG_JTAG_IO("jlink_usb_write, out_length = %d, result = %d", out_length, result);
  835. #ifdef _DEBUG_USB_COMMS_
  836. jlink_debug_buffer(usb_out_buffer, out_length);
  837. #endif
  838. return result;
  839. }
  840. /* Read data from USB into in_buffer. */
  841. static int jlink_usb_read(struct jlink *jlink, int expected_size)
  842. {
  843. int result = usb_bulk_read_ex(jlink->usb_handle, jlink_read_ep,
  844. (char *)usb_in_buffer, expected_size, JLINK_USB_TIMEOUT);
  845. DEBUG_JTAG_IO("jlink_usb_read, result = %d", result);
  846. #ifdef _DEBUG_USB_COMMS_
  847. jlink_debug_buffer(usb_in_buffer, result);
  848. #endif
  849. return result;
  850. }
  851. /* Read the result from the previous EMU cmd into result_buffer. */
  852. static int jlink_usb_read_emu_result(struct jlink *jlink)
  853. {
  854. int result = usb_bulk_read_ex(jlink->usb_handle, jlink_read_ep,
  855. (char *)usb_emu_result_buffer, 1 /* JLINK_EMU_RESULT_BUFFER_SIZE */,
  856. JLINK_USB_TIMEOUT);
  857. DEBUG_JTAG_IO("jlink_usb_read_result, result = %d", result);
  858. #ifdef _DEBUG_USB_COMMS_
  859. jlink_debug_buffer(usb_emu_result_buffer, result);
  860. #endif
  861. return result;
  862. }
  863. #ifdef _DEBUG_USB_COMMS_
  864. #define BYTES_PER_LINE 16
  865. static void jlink_debug_buffer(uint8_t *buffer, int length)
  866. {
  867. char line[81];
  868. char s[4];
  869. int i;
  870. int j;
  871. for (i = 0; i < length; i += BYTES_PER_LINE)
  872. {
  873. snprintf(line, 5, "%04x", i);
  874. for (j = i; j < i + BYTES_PER_LINE && j < length; j++)
  875. {
  876. snprintf(s, 4, " %02x", buffer[j]);
  877. strcat(line, s);
  878. }
  879. LOG_DEBUG("%s", line);
  880. }
  881. }
  882. #endif