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.
 
 
 
 
 
 

1646 lines
40 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. * Copyright (C) 2011 by Jean-Christophe PLAGNIOL-VIILARD *
  9. * plagnioj@jcrosoft.com *
  10. * *
  11. * This program is free software; you can redistribute it and/or modify *
  12. * it under the terms of the GNU General Public License as published by *
  13. * the Free Software Foundation; either version 2 of the License, or *
  14. * (at your option) any later version. *
  15. * *
  16. * This program is distributed in the hope that it will be useful, *
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  19. * GNU General Public License for more details. *
  20. * *
  21. * You should have received a copy of the GNU General Public License *
  22. * along with this program; if not, write to the *
  23. * Free Software Foundation, Inc., *
  24. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  25. ***************************************************************************/
  26. #ifdef HAVE_CONFIG_H
  27. #include "config.h"
  28. #endif
  29. #include <jtag/interface.h>
  30. #include <jtag/commands.h>
  31. #include "libusb_common.h"
  32. /* See Segger's public documentation:
  33. * Reference manual for J-Link USB Protocol
  34. * Document RM08001-R6 Date: June 16, 2009
  35. * (Or newer, with some SWD information).
  36. http://www.segger.com/cms/admin/uploads/productDocs/RM08001_JLinkUSBProtocol.pdf
  37. */
  38. /*
  39. * The default pid of the segger is 0x0101
  40. * But when you change the USB Address it will also
  41. *
  42. * pid = ( usb_address > 0x4) ? 0x0101 : (0x101 + usb_address)
  43. */
  44. #define VID 0x1366, 0x1366, 0x1366, 0x1366
  45. #define PID 0x0101, 0x0102, 0x0103, 0x0104
  46. #define JLINK_WRITE_ENDPOINT 0x02
  47. #define JLINK_READ_ENDPOINT 0x81
  48. static unsigned int jlink_write_ep = JLINK_WRITE_ENDPOINT;
  49. static unsigned int jlink_read_ep = JLINK_READ_ENDPOINT;
  50. static unsigned int jlink_hw_jtag_version = 2;
  51. #define JLINK_USB_TIMEOUT 1000
  52. // See Section 3.3.2 of the Segger JLink USB protocol manual
  53. /* 2048 is the max value we can use here */
  54. #define JLINK_TAP_BUFFER_SIZE 2048
  55. //#define JLINK_TAP_BUFFER_SIZE 256
  56. //#define JLINK_TAP_BUFFER_SIZE 384
  57. #define JLINK_IN_BUFFER_SIZE 2048
  58. #define JLINK_OUT_BUFFER_SIZE 2*2048 + 4
  59. #define JLINK_EMU_RESULT_BUFFER_SIZE 64
  60. /* Global USB buffers */
  61. static uint8_t usb_in_buffer[JLINK_IN_BUFFER_SIZE];
  62. static uint8_t usb_out_buffer[JLINK_OUT_BUFFER_SIZE];
  63. static uint8_t usb_emu_result_buffer[JLINK_EMU_RESULT_BUFFER_SIZE];
  64. /* Constants for JLink command */
  65. #define EMU_CMD_VERSION 0x01
  66. #define EMU_CMD_SET_SPEED 0x05
  67. #define EMU_CMD_GET_STATE 0x07
  68. #define EMU_CMD_HW_CLOCK 0xc8
  69. #define EMU_CMD_HW_TMS0 0xc9
  70. #define EMU_CMD_HW_TMS1 0xca
  71. #define EMU_CMD_HW_JTAG2 0xce
  72. #define EMU_CMD_HW_JTAG3 0xcf
  73. #define EMU_CMD_GET_MAX_MEM_BLOCK 0xd4
  74. #define EMU_CMD_HW_RESET0 0xdc
  75. #define EMU_CMD_HW_RESET1 0xdd
  76. #define EMU_CMD_HW_TRST0 0xde
  77. #define EMU_CMD_HW_TRST1 0xdf
  78. #define EMU_CMD_GET_CAPS 0xe8
  79. #define EMU_CMD_GET_HW_VERSION 0xf0
  80. #define EMU_CMD_READ_CONFIG 0xf2
  81. #define EMU_CMD_WRITE_CONFIG 0xf3
  82. /* bits return from EMU_CMD_GET_CAPS */
  83. #define EMU_CAP_RESERVED_1 0
  84. #define EMU_CAP_GET_HW_VERSION 1
  85. #define EMU_CAP_WRITE_DCC 2
  86. #define EMU_CAP_ADAPTIVE_CLOCKING 3
  87. #define EMU_CAP_READ_CONFIG 4
  88. #define EMU_CAP_WRITE_CONFIG 5
  89. #define EMU_CAP_TRACE 6
  90. #define EMU_CAP_WRITE_MEM 7
  91. #define EMU_CAP_READ_MEM 8
  92. #define EMU_CAP_SPEED_INFO 9
  93. #define EMU_CAP_EXEC_CODE 10
  94. #define EMU_CAP_GET_MAX_BLOCK_SIZE 11
  95. #define EMU_CAP_GET_HW_INFO 12
  96. #define EMU_CAP_SET_KS_POWER 13
  97. #define EMU_CAP_RESET_STOP_TIMED 14
  98. #define EMU_CAP_RESERVED_2 15
  99. #define EMU_CAP_MEASURE_RTCK_REACT 16
  100. #define EMU_CAP_SELECT_IF 17
  101. #define EMU_CAP_RW_MEM_ARM79 18
  102. #define EMU_CAP_GET_COUNTERS 19
  103. #define EMU_CAP_READ_DCC 20
  104. #define EMU_CAP_GET_CPU_CAPS 21
  105. #define EMU_CAP_EXEC_CPU_CMD 22
  106. #define EMU_CAP_SWO 23
  107. #define EMU_CAP_WRITE_DCC_EX 24
  108. #define EMU_CAP_UPDATE_FIRMWARE_EX 25
  109. #define EMU_CAP_FILE_IO 26
  110. #define EMU_CAP_REGISTER 27
  111. #define EMU_CAP_INDICATORS 28
  112. #define EMU_CAP_TEST_NET_SPEED 29
  113. #define EMU_CAP_RAWTRACE 30
  114. #define EMU_CAP_RESERVED_3 31
  115. static char *jlink_cap_str[] = {
  116. "Always 1.",
  117. "Supports command EMU_CMD_GET_HARDWARE_VERSION",
  118. "Supports command EMU_CMD_WRITE_DCC",
  119. "Supports adaptive clocking",
  120. "Supports command EMU_CMD_READ_CONFIG",
  121. "Supports command EMU_CMD_WRITE_CONFIG",
  122. "Supports trace commands",
  123. "Supports command EMU_CMD_WRITE_MEM",
  124. "Supports command EMU_CMD_READ_MEM",
  125. "Supports command EMU_CMD_GET_SPEED",
  126. "Supports command EMU_CMD_CODE_...",
  127. "Supports command EMU_CMD_GET_MAX_BLOCK_SIZE",
  128. "Supports command EMU_CMD_GET_HW_INFO",
  129. "Supports command EMU_CMD_SET_KS_POWER",
  130. "Supports command EMU_CMD_HW_RELEASE_RESET_STOP_TIMED",
  131. "Reserved",
  132. "Supports command EMU_CMD_MEASURE_RTCK_REACT",
  133. "Supports command EMU_CMD_HW_SELECT_IF",
  134. "Supports command EMU_CMD_READ/WRITE_MEM_ARM79",
  135. "Supports command EMU_CMD_GET_COUNTERS",
  136. "Supports command EMU_CMD_READ_DCC",
  137. "Supports command EMU_CMD_GET_CPU_CAPS",
  138. "Supports command EMU_CMD_EXEC_CPU_CMD",
  139. "Supports command EMU_CMD_SWO",
  140. "Supports command EMU_CMD_WRITE_DCC_EX",
  141. "Supports command EMU_CMD_UPDATE_FIRMWARE_EX",
  142. "Supports command EMU_CMD_FILE_IO",
  143. "Supports command EMU_CMD_REGISTER",
  144. "Supports command EMU_CMD_INDICATORS",
  145. "Supports command EMU_CMD_TEST_NET_SPEED",
  146. "Supports command EMU_CMD_RAWTRACE",
  147. "Reserved",
  148. };
  149. /* max speed 12MHz v5.0 jlink */
  150. #define JLINK_MAX_SPEED 12000
  151. /* J-Link hardware versions */
  152. #define JLINK_HW_TYPE_JLINK 0
  153. #define JLINK_HW_TYPE_JTRACE 1
  154. #define JLINK_HW_TYPE_FLASHER 2
  155. #define JLINK_HW_TYPE_JLINK_PRO 3
  156. #define JLINK_HW_TYPE_MAX 4
  157. static char *jlink_hw_type_str[] = {
  158. "J-Link",
  159. "J-Trace",
  160. "Flasher",
  161. "J-Link Pro",
  162. };
  163. /* Queue command functions */
  164. static void jlink_end_state(tap_state_t state);
  165. static void jlink_state_move(void);
  166. static void jlink_path_move(int num_states, tap_state_t *path);
  167. static void jlink_runtest(int num_cycles);
  168. static void jlink_scan(bool ir_scan, enum scan_type type, uint8_t *buffer,
  169. int scan_size, struct scan_command *command);
  170. static void jlink_reset(int trst, int srst);
  171. static void jlink_simple_command(uint8_t command);
  172. static int jlink_get_status(void);
  173. /* J-Link tap buffer functions */
  174. static void jlink_tap_init(void);
  175. static int jlink_tap_execute(void);
  176. static void jlink_tap_ensure_space(int scans, int bits);
  177. static void jlink_tap_append_step(int tms, int tdi);
  178. static void jlink_tap_append_scan(int length, uint8_t *buffer,
  179. struct scan_command *command);
  180. /* Jlink lowlevel functions */
  181. struct jlink {
  182. struct jtag_libusb_device_handle *usb_handle;
  183. };
  184. static struct jlink *jlink_usb_open(void);
  185. static void jlink_usb_close(struct jlink *jlink);
  186. static int jlink_usb_message(struct jlink *jlink, int out_length, int in_length);
  187. static int jlink_usb_write(struct jlink *jlink, int out_length);
  188. static int jlink_usb_read(struct jlink *jlink, int expected_size);
  189. static int jlink_usb_read_emu_result(struct jlink *jlink);
  190. /* helper functions */
  191. static int jlink_get_version_info(void);
  192. #ifdef _DEBUG_USB_COMMS_
  193. static void jlink_debug_buffer(uint8_t *buffer, int length);
  194. #else
  195. static inline void jlink_debug_buffer(uint8_t *buffer, int length)
  196. {
  197. }
  198. #endif
  199. static enum tap_state jlink_last_state = TAP_RESET;
  200. static struct jlink* jlink_handle;
  201. /* pid could be specified at runtime */
  202. static uint16_t vids[] = { VID, 0 };
  203. static uint16_t pids[] = { PID, 0 };
  204. static uint32_t jlink_caps;
  205. static uint32_t jlink_hw_type;
  206. /* 256 byte non-volatile memory */
  207. struct jlink_config {
  208. uint8_t usb_address;
  209. /* 0ffset 0x01 to 0x03 */
  210. uint8_t reserved_1[3];
  211. uint32_t kickstart_power_on_jtag_pin_19;
  212. /* 0ffset 0x08 to 0x1f */
  213. uint8_t reserved_2[24];
  214. /* IP only for J-Link Pro */
  215. uint8_t ip_address[4];
  216. uint8_t subnet_mask[4];
  217. /* 0ffset 0x28 to 0x2f */
  218. uint8_t reserved_3[8];
  219. uint8_t mac_address[6];
  220. /* 0ffset 0x36 to 0xff */
  221. uint8_t reserved_4[202];
  222. } __attribute__ ((packed));
  223. struct jlink_config jlink_cfg;
  224. /***************************************************************************/
  225. /* External interface implementation */
  226. static void jlink_execute_runtest(struct jtag_command *cmd)
  227. {
  228. DEBUG_JTAG_IO("runtest %i cycles, end in %i",
  229. cmd->cmd.runtest->num_cycles,
  230. cmd->cmd.runtest->end_state);
  231. jlink_end_state(cmd->cmd.runtest->end_state);
  232. jlink_runtest(cmd->cmd.runtest->num_cycles);
  233. }
  234. static void jlink_execute_statemove(struct jtag_command *cmd)
  235. {
  236. DEBUG_JTAG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
  237. jlink_end_state(cmd->cmd.statemove->end_state);
  238. jlink_state_move();
  239. }
  240. static void jlink_execute_pathmove(struct jtag_command *cmd)
  241. {
  242. DEBUG_JTAG_IO("pathmove: %i states, end in %i",
  243. cmd->cmd.pathmove->num_states,
  244. cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
  245. jlink_path_move(cmd->cmd.pathmove->num_states,
  246. cmd->cmd.pathmove->path);
  247. }
  248. static void jlink_execute_scan(struct jtag_command *cmd)
  249. {
  250. int scan_size;
  251. enum scan_type type;
  252. uint8_t *buffer;
  253. DEBUG_JTAG_IO("scan end in %s", tap_state_name(cmd->cmd.scan->end_state));
  254. jlink_end_state(cmd->cmd.scan->end_state);
  255. scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
  256. DEBUG_JTAG_IO("scan input, length = %d", scan_size);
  257. jlink_debug_buffer(buffer, (scan_size + 7) / 8);
  258. type = jtag_scan_type(cmd->cmd.scan);
  259. jlink_scan(cmd->cmd.scan->ir_scan,
  260. type, buffer, scan_size, cmd->cmd.scan);
  261. }
  262. static void jlink_execute_reset(struct jtag_command *cmd)
  263. {
  264. DEBUG_JTAG_IO("reset trst: %i srst %i",
  265. cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  266. jlink_tap_execute();
  267. jlink_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  268. jlink_tap_execute();
  269. }
  270. static void jlink_execute_sleep(struct jtag_command *cmd)
  271. {
  272. DEBUG_JTAG_IO("sleep %" PRIi32 "", cmd->cmd.sleep->us);
  273. jlink_tap_execute();
  274. jtag_sleep(cmd->cmd.sleep->us);
  275. }
  276. static void jlink_execute_command(struct jtag_command *cmd)
  277. {
  278. switch (cmd->type)
  279. {
  280. case JTAG_RUNTEST: jlink_execute_runtest(cmd); break;
  281. case JTAG_TLR_RESET: jlink_execute_statemove(cmd); break;
  282. case JTAG_PATHMOVE: jlink_execute_pathmove(cmd); break;
  283. case JTAG_SCAN: jlink_execute_scan(cmd); break;
  284. case JTAG_RESET: jlink_execute_reset(cmd); break;
  285. case JTAG_SLEEP: jlink_execute_sleep(cmd); break;
  286. default:
  287. LOG_ERROR("BUG: unknown JTAG command type encountered");
  288. exit(-1);
  289. }
  290. }
  291. static int jlink_execute_queue(void)
  292. {
  293. struct jtag_command *cmd = jtag_command_queue;
  294. while (cmd != NULL)
  295. {
  296. jlink_execute_command(cmd);
  297. cmd = cmd->next;
  298. }
  299. return jlink_tap_execute();
  300. }
  301. /* Sets speed in kHz. */
  302. static int jlink_speed(int speed)
  303. {
  304. int result;
  305. if (speed > JLINK_MAX_SPEED)
  306. {
  307. LOG_INFO("reduce speed request: %dkHz to %dkHz maximum",
  308. speed, JLINK_MAX_SPEED);
  309. speed = JLINK_MAX_SPEED;
  310. }
  311. /* check for RTCK setting */
  312. if (speed == 0)
  313. speed = -1;
  314. usb_out_buffer[0] = EMU_CMD_SET_SPEED;
  315. usb_out_buffer[1] = (speed >> 0) & 0xff;
  316. usb_out_buffer[2] = (speed >> 8) & 0xff;
  317. result = jlink_usb_write(jlink_handle, 3);
  318. if (result != 3)
  319. {
  320. LOG_ERROR("J-Link setting speed failed (%d)", result);
  321. return ERROR_JTAG_DEVICE_ERROR;
  322. }
  323. return ERROR_OK;
  324. }
  325. static int jlink_speed_div(int speed, int* khz)
  326. {
  327. *khz = speed;
  328. return ERROR_OK;
  329. }
  330. static int jlink_khz(int khz, int *jtag_speed)
  331. {
  332. *jtag_speed = khz;
  333. return ERROR_OK;
  334. }
  335. static int jlink_init(void)
  336. {
  337. int i;
  338. jlink_handle = jlink_usb_open();
  339. if (jlink_handle == 0)
  340. {
  341. LOG_ERROR("Cannot find jlink Interface! Please check "
  342. "connection and permissions.");
  343. return ERROR_JTAG_INIT_FAILED;
  344. }
  345. /*
  346. * The next three instructions were added after discovering a problem
  347. * while using an oscilloscope.
  348. * For the V8 SAM-ICE dongle (and likely other j-link device variants),
  349. * the reset line to the target microprocessor was found to cycle only
  350. * intermittently during emulator startup (even after encountering the
  351. * downstream reset instruction later in the code).
  352. * This was found to create two issues:
  353. * 1) In general it is a bad practice to not reset a CPU to a known
  354. * state when starting an emulator and
  355. * 2) something critical happens inside the dongle when it does the
  356. * first read following a new USB session.
  357. * Keeping the processor in reset during the first read collecting
  358. * version information seems to prevent errant
  359. * "J-Link command EMU_CMD_VERSION failed" issues.
  360. */
  361. LOG_INFO("J-Link initialization started / target CPU reset initiated");
  362. jlink_simple_command(EMU_CMD_HW_TRST0);
  363. jlink_simple_command(EMU_CMD_HW_RESET0);
  364. usleep(1000);
  365. jlink_hw_jtag_version = 2;
  366. if (jlink_get_version_info() == ERROR_OK)
  367. {
  368. /* attempt to get status */
  369. jlink_get_status();
  370. }
  371. LOG_INFO("J-Link JTAG Interface ready");
  372. jlink_reset(0, 0);
  373. jtag_sleep(3000);
  374. jlink_tap_init();
  375. /* v5/6 jlink seems to have an issue if the first tap move
  376. * is not divisible by 8, so we send a TLR on first power up */
  377. for (i = 0; i < 8; i++) {
  378. jlink_tap_append_step(1, 0);
  379. }
  380. jlink_tap_execute();
  381. return ERROR_OK;
  382. }
  383. static int jlink_quit(void)
  384. {
  385. jlink_usb_close(jlink_handle);
  386. return ERROR_OK;
  387. }
  388. /***************************************************************************/
  389. /* Queue command implementations */
  390. static void jlink_end_state(tap_state_t state)
  391. {
  392. if (tap_is_state_stable(state))
  393. {
  394. tap_set_end_state(state);
  395. }
  396. else
  397. {
  398. LOG_ERROR("BUG: %i is not a valid end state", state);
  399. exit(-1);
  400. }
  401. }
  402. /* Goes to the end state. */
  403. static void jlink_state_move(void)
  404. {
  405. int i;
  406. int tms = 0;
  407. uint8_t tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
  408. uint8_t tms_scan_bits = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
  409. for (i = 0; i < tms_scan_bits; i++)
  410. {
  411. tms = (tms_scan >> i) & 1;
  412. jlink_tap_append_step(tms, 0);
  413. }
  414. tap_set_state(tap_get_end_state());
  415. }
  416. static void jlink_path_move(int num_states, tap_state_t *path)
  417. {
  418. int i;
  419. for (i = 0; i < num_states; i++)
  420. {
  421. if (path[i] == tap_state_transition(tap_get_state(), false))
  422. {
  423. jlink_tap_append_step(0, 0);
  424. }
  425. else if (path[i] == tap_state_transition(tap_get_state(), true))
  426. {
  427. jlink_tap_append_step(1, 0);
  428. }
  429. else
  430. {
  431. LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
  432. tap_state_name(tap_get_state()), tap_state_name(path[i]));
  433. exit(-1);
  434. }
  435. tap_set_state(path[i]);
  436. }
  437. tap_set_end_state(tap_get_state());
  438. }
  439. static void jlink_runtest(int num_cycles)
  440. {
  441. int i;
  442. tap_state_t saved_end_state = tap_get_end_state();
  443. jlink_tap_ensure_space(1,num_cycles + 16);
  444. /* only do a state_move when we're not already in IDLE */
  445. if (tap_get_state() != TAP_IDLE)
  446. {
  447. jlink_end_state(TAP_IDLE);
  448. jlink_state_move();
  449. // num_cycles--;
  450. }
  451. /* execute num_cycles */
  452. for (i = 0; i < num_cycles; i++)
  453. {
  454. jlink_tap_append_step(0, 0);
  455. }
  456. /* finish in end_state */
  457. jlink_end_state(saved_end_state);
  458. if (tap_get_state() != tap_get_end_state())
  459. {
  460. jlink_state_move();
  461. }
  462. }
  463. static void jlink_scan(bool ir_scan, enum scan_type type, uint8_t *buffer,
  464. int scan_size, struct scan_command *command)
  465. {
  466. tap_state_t saved_end_state;
  467. jlink_tap_ensure_space(1, scan_size + 16);
  468. saved_end_state = tap_get_end_state();
  469. /* Move to appropriate scan state */
  470. jlink_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
  471. /* Only move if we're not already there */
  472. if (tap_get_state() != tap_get_end_state())
  473. jlink_state_move();
  474. jlink_end_state(saved_end_state);
  475. /* Scan */
  476. jlink_tap_append_scan(scan_size, buffer, command);
  477. /* We are in Exit1, go to Pause */
  478. jlink_tap_append_step(0, 0);
  479. tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
  480. if (tap_get_state() != tap_get_end_state())
  481. {
  482. jlink_state_move();
  483. }
  484. }
  485. static void jlink_reset(int trst, int srst)
  486. {
  487. LOG_DEBUG("trst: %i, srst: %i", trst, srst);
  488. /* Signals are active low */
  489. if (srst == 0)
  490. {
  491. jlink_simple_command(EMU_CMD_HW_RESET1);
  492. }
  493. if (srst == 1)
  494. {
  495. jlink_simple_command(EMU_CMD_HW_RESET0);
  496. }
  497. if (trst == 1)
  498. {
  499. jlink_simple_command(EMU_CMD_HW_TRST0);
  500. }
  501. if (trst == 0)
  502. {
  503. jlink_simple_command(EMU_CMD_HW_TRST1);
  504. }
  505. }
  506. static void jlink_simple_command(uint8_t command)
  507. {
  508. int result;
  509. DEBUG_JTAG_IO("0x%02x", command);
  510. usb_out_buffer[0] = command;
  511. result = jlink_usb_write(jlink_handle, 1);
  512. if (result != 1)
  513. {
  514. LOG_ERROR("J-Link command 0x%02x failed (%d)", command, result);
  515. }
  516. }
  517. static int jlink_get_status(void)
  518. {
  519. int result;
  520. jlink_simple_command(EMU_CMD_GET_STATE);
  521. result = jlink_usb_read(jlink_handle, 8);
  522. if (result != 8)
  523. {
  524. LOG_ERROR("J-Link command EMU_CMD_GET_STATE failed (%d)", result);
  525. return ERROR_JTAG_DEVICE_ERROR;
  526. }
  527. int vref = usb_in_buffer[0] + (usb_in_buffer[1] << 8);
  528. LOG_INFO("Vref = %d.%d TCK = %d TDI = %d TDO = %d TMS = %d SRST = %d TRST = %d", \
  529. vref / 1000, vref % 1000, \
  530. usb_in_buffer[2], usb_in_buffer[3], usb_in_buffer[4], \
  531. usb_in_buffer[5], usb_in_buffer[6], usb_in_buffer[7]);
  532. if (vref < 1500)
  533. LOG_ERROR("Vref too low. Check Target Power");
  534. return ERROR_OK;
  535. }
  536. #define jlink_dump_printf(context, expr ...) \
  537. do { \
  538. if (context) \
  539. command_print(context, expr); \
  540. else \
  541. LOG_INFO(expr); \
  542. } while(0);
  543. static void jlink_caps_dump(struct command_context *ctx)
  544. {
  545. int i;
  546. jlink_dump_printf(ctx, "J-Link Capabilities");
  547. for (i = 1; i < 31; i++)
  548. if (jlink_caps & (1 << i))
  549. jlink_dump_printf(ctx, "%s", jlink_cap_str[i]);
  550. }
  551. static void jlink_config_usb_address_dump(struct command_context *ctx, struct jlink_config *cfg)
  552. {
  553. if (!cfg)
  554. return;
  555. jlink_dump_printf(ctx, "USB-Address: 0x%x", cfg->usb_address);
  556. }
  557. static void jlink_config_kickstart_dump(struct command_context *ctx, struct jlink_config *cfg)
  558. {
  559. if (!cfg)
  560. return;
  561. jlink_dump_printf(ctx, "Kickstart power on JTAG-pin 19: 0x%x",
  562. cfg->kickstart_power_on_jtag_pin_19);
  563. }
  564. static void jlink_config_mac_address_dump(struct command_context *ctx, struct jlink_config *cfg)
  565. {
  566. if (!cfg)
  567. return;
  568. jlink_dump_printf(ctx, "MAC Address: %.02x:%.02x:%.02x:%.02x:%.02x:%.02x",
  569. cfg->mac_address[5], cfg->mac_address[4],
  570. cfg->mac_address[3], cfg->mac_address[2],
  571. cfg->mac_address[1], cfg->mac_address[0]);
  572. }
  573. static void jlink_config_ip_dump(struct command_context *ctx, struct jlink_config *cfg)
  574. {
  575. if (!cfg)
  576. return;
  577. jlink_dump_printf(ctx, "IP Address: %d.%d.%d.%d",
  578. cfg->ip_address[3], cfg->ip_address[2],
  579. cfg->ip_address[1], cfg->ip_address[0]);
  580. jlink_dump_printf(ctx, "Subnet Mask: %d.%d.%d.%d",
  581. cfg->subnet_mask[3], cfg->subnet_mask[2],
  582. cfg->subnet_mask[1], cfg->subnet_mask[0]);
  583. }
  584. static void jlink_config_dump(struct command_context *ctx, struct jlink_config *cfg)
  585. {
  586. if (!cfg)
  587. return;
  588. jlink_dump_printf(ctx, "J-Link configuration");
  589. jlink_config_usb_address_dump(ctx, cfg);
  590. jlink_config_kickstart_dump(ctx, cfg);
  591. if (jlink_hw_type == JLINK_HW_TYPE_JLINK_PRO)
  592. {
  593. jlink_config_ip_dump(ctx, cfg);
  594. jlink_config_mac_address_dump(ctx, cfg);
  595. }
  596. }
  597. static int jlink_get_config(struct jlink_config *cfg)
  598. {
  599. int result;
  600. int size = sizeof(struct jlink_config);
  601. jlink_simple_command(EMU_CMD_READ_CONFIG);
  602. result = jlink_usb_read(jlink_handle, size);
  603. if (size != result)
  604. {
  605. LOG_ERROR("jlink_usb_read failed (requested=%d, result=%d)", size, result);
  606. return ERROR_FAIL;
  607. }
  608. memcpy(cfg, usb_in_buffer, size);
  609. /*
  610. * Section 4.2.4 IN-transaction
  611. * read dummy 0-byte packet
  612. */
  613. jlink_usb_read(jlink_handle, 1);
  614. return ERROR_OK;
  615. }
  616. static int jlink_set_config(struct jlink_config *cfg)
  617. {
  618. int result;
  619. int size = sizeof(struct jlink_config);
  620. jlink_simple_command(EMU_CMD_WRITE_CONFIG);
  621. memcpy(usb_out_buffer, cfg, size);
  622. result = jlink_usb_write(jlink_handle, size);
  623. if (result != size)
  624. {
  625. LOG_ERROR("jlink_usb_write failed (requested=%d, result=%d)", 256, result);
  626. return ERROR_FAIL;
  627. }
  628. return ERROR_OK;
  629. }
  630. static int jlink_get_version_info(void)
  631. {
  632. int result;
  633. int len;
  634. uint32_t jlink_max_size;
  635. /* query hardware version */
  636. jlink_simple_command(EMU_CMD_VERSION);
  637. result = jlink_usb_read(jlink_handle, 2);
  638. if (2 != result)
  639. {
  640. LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)", result);
  641. return ERROR_JTAG_DEVICE_ERROR;
  642. }
  643. len = buf_get_u32(usb_in_buffer, 0, 16);
  644. if (len > JLINK_IN_BUFFER_SIZE)
  645. {
  646. LOG_ERROR("J-Link command EMU_CMD_VERSION impossible return length 0x%0x", len);
  647. len = JLINK_IN_BUFFER_SIZE;
  648. }
  649. result = jlink_usb_read(jlink_handle, len);
  650. if (result != len)
  651. {
  652. LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)", result);
  653. return ERROR_JTAG_DEVICE_ERROR;
  654. }
  655. usb_in_buffer[result] = 0;
  656. LOG_INFO("%s", (char *)usb_in_buffer);
  657. /* query hardware capabilities */
  658. jlink_simple_command(EMU_CMD_GET_CAPS);
  659. result = jlink_usb_read(jlink_handle, 4);
  660. if (4 != result)
  661. {
  662. LOG_ERROR("J-Link command EMU_CMD_GET_CAPS failed (%d)", result);
  663. return ERROR_JTAG_DEVICE_ERROR;
  664. }
  665. jlink_caps = buf_get_u32(usb_in_buffer, 0, 32);
  666. LOG_INFO("J-Link caps 0x%x", (unsigned)jlink_caps);
  667. if (jlink_caps & (1 << EMU_CAP_GET_HW_VERSION))
  668. {
  669. /* query hardware version */
  670. jlink_simple_command(EMU_CMD_GET_HW_VERSION);
  671. result = jlink_usb_read(jlink_handle, 4);
  672. if (4 != result)
  673. {
  674. LOG_ERROR("J-Link command EMU_CMD_GET_HW_VERSION failed (%d)", result);
  675. return ERROR_JTAG_DEVICE_ERROR;
  676. }
  677. uint32_t jlink_hw_version = buf_get_u32(usb_in_buffer, 0, 32);
  678. uint32_t major_revision = (jlink_hw_version / 10000) % 100;
  679. jlink_hw_type = (jlink_hw_version / 1000000) % 100;
  680. if (major_revision >= 5)
  681. jlink_hw_jtag_version = 3;
  682. LOG_INFO("J-Link hw version %i", (int)jlink_hw_version);
  683. if (jlink_hw_type >= JLINK_HW_TYPE_MAX)
  684. LOG_INFO("J-Link hw type uknown 0x%x", jlink_hw_type);
  685. else
  686. LOG_INFO("J-Link hw type %s", jlink_hw_type_str[jlink_hw_type]);
  687. }
  688. if (jlink_caps & (1 << EMU_CAP_GET_MAX_BLOCK_SIZE))
  689. {
  690. /* query hardware maximum memory block */
  691. jlink_simple_command(EMU_CMD_GET_MAX_MEM_BLOCK);
  692. result = jlink_usb_read(jlink_handle, 4);
  693. if (4 != result)
  694. {
  695. LOG_ERROR("J-Link command EMU_CMD_GET_MAX_MEM_BLOCK failed (%d)", result);
  696. return ERROR_JTAG_DEVICE_ERROR;
  697. }
  698. jlink_max_size = buf_get_u32(usb_in_buffer, 0, 32);
  699. LOG_INFO("J-Link max mem block %i", (int)jlink_max_size);
  700. }
  701. if (jlink_caps & (1 << EMU_CAP_READ_CONFIG))
  702. {
  703. if (jlink_get_config(&jlink_cfg) != ERROR_OK)
  704. return ERROR_JTAG_DEVICE_ERROR;
  705. jlink_config_dump(NULL, &jlink_cfg);
  706. }
  707. return ERROR_OK;
  708. }
  709. COMMAND_HANDLER(jlink_pid_command)
  710. {
  711. if (CMD_ARGC != 1)
  712. {
  713. LOG_ERROR("Need exactly one argument to jlink_pid");
  714. return ERROR_FAIL;
  715. }
  716. pids[0] = strtoul(CMD_ARGV[0], NULL, 16);
  717. pids[1] = 0;
  718. vids[1] = 0;
  719. return ERROR_OK;
  720. }
  721. COMMAND_HANDLER(jlink_handle_jlink_info_command)
  722. {
  723. if (jlink_get_version_info() == ERROR_OK)
  724. {
  725. /* attempt to get status */
  726. jlink_get_status();
  727. }
  728. return ERROR_OK;
  729. }
  730. COMMAND_HANDLER(jlink_handle_jlink_caps_command)
  731. {
  732. jlink_caps_dump(CMD_CTX);
  733. return ERROR_OK;
  734. }
  735. COMMAND_HANDLER(jlink_handle_jlink_hw_jtag_command)
  736. {
  737. switch (CMD_ARGC) {
  738. case 0:
  739. command_print(CMD_CTX, "J-Link hw jtag %i", jlink_hw_jtag_version);
  740. break;
  741. case 1: {
  742. int request_version = atoi(CMD_ARGV[0]);
  743. switch (request_version) {
  744. case 2: case 3:
  745. jlink_hw_jtag_version = request_version;
  746. break;
  747. default:
  748. return ERROR_COMMAND_SYNTAX_ERROR;
  749. }
  750. break;
  751. }
  752. default:
  753. return ERROR_COMMAND_SYNTAX_ERROR;
  754. }
  755. return ERROR_OK;
  756. }
  757. COMMAND_HANDLER(jlink_handle_jlink_kickstart_command)
  758. {
  759. uint32_t kickstart;
  760. if (CMD_ARGC < 1)
  761. {
  762. jlink_config_kickstart_dump(CMD_CTX, &jlink_cfg);
  763. return ERROR_OK;
  764. }
  765. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], kickstart);
  766. jlink_cfg.kickstart_power_on_jtag_pin_19 = kickstart;
  767. return ERROR_OK;
  768. }
  769. COMMAND_HANDLER(jlink_handle_jlink_mac_address_command)
  770. {
  771. uint8_t addr[6];
  772. int i;
  773. char *e;
  774. const char *str;
  775. if (CMD_ARGC < 1)
  776. {
  777. jlink_config_mac_address_dump(CMD_CTX, &jlink_cfg);
  778. return ERROR_OK;
  779. }
  780. str = CMD_ARGV[0];
  781. if ((strlen(str) != 17) || (str[2] != ':' || str[5] != ':' || str[8] != ':' ||
  782. str[11] != ':' || str[14] != ':'))
  783. {
  784. command_print(CMD_CTX, "ethaddr miss format ff:ff:ff:ff:ff:ff");
  785. return ERROR_COMMAND_SYNTAX_ERROR;
  786. }
  787. for (i = 5; i >= 0; i--)
  788. {
  789. addr[i] = strtoul(str, &e, 16);
  790. str = e + 1;
  791. }
  792. if (!(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5]))
  793. {
  794. command_print(CMD_CTX, "invalid it's zero mac_address");
  795. return ERROR_COMMAND_SYNTAX_ERROR;
  796. }
  797. if (!(0x01 & addr[0]))
  798. {
  799. command_print(CMD_CTX, "invalid it's a multicat mac_address");
  800. return ERROR_COMMAND_SYNTAX_ERROR;
  801. }
  802. memcpy(jlink_cfg.mac_address, addr, sizeof(addr));
  803. return ERROR_OK;
  804. }
  805. static int string_to_ip(const char *s, uint8_t *ip, int *pos)
  806. {
  807. uint8_t lip[4];
  808. char *e;
  809. const char *s_save = s;
  810. int i;
  811. if (!s)
  812. return -EINVAL;
  813. for (i = 0; i < 4; i++) {
  814. lip[i] = strtoul(s, &e, 10);
  815. if (*e != '.' && i != 3)
  816. return -EINVAL;
  817. s = e + 1;
  818. }
  819. *pos = e - s_save;
  820. memcpy(ip, lip, sizeof(lip));
  821. return ERROR_OK;
  822. }
  823. static void cpy_ip(uint8_t *dst, uint8_t *src)
  824. {
  825. int i, j;
  826. for (i = 0, j = 3; i < 4; i++, j--)
  827. dst[i] = src[j];
  828. }
  829. COMMAND_HANDLER(jlink_handle_jlink_ip_command)
  830. {
  831. uint32_t ip_address;
  832. uint32_t subnet_mask = 0;
  833. int i, len;
  834. int ret;
  835. uint8_t subnet_bits = 24;
  836. if (CMD_ARGC < 1)
  837. {
  838. jlink_config_ip_dump(CMD_CTX, &jlink_cfg);
  839. return ERROR_OK;
  840. }
  841. ret = string_to_ip(CMD_ARGV[0], (uint8_t*)&ip_address, &i);
  842. if (ret != ERROR_OK)
  843. return ret;
  844. len = strlen(CMD_ARGV[0]);
  845. /* check for this format A.B.C.D/E */
  846. if (i < len)
  847. {
  848. if (CMD_ARGV[0][i] != '/')
  849. return ERROR_COMMAND_SYNTAX_ERROR;
  850. COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0] + i + 1, subnet_bits);
  851. }
  852. else
  853. {
  854. if (CMD_ARGC > 1)
  855. {
  856. ret = string_to_ip(CMD_ARGV[1], (uint8_t*)&subnet_mask, &i);
  857. if (ret != ERROR_OK)
  858. return ret;
  859. }
  860. }
  861. if (!subnet_mask)
  862. subnet_mask = (uint32_t)(subnet_bits < 32 ?
  863. ((1ULL << subnet_bits) -1) : 0xffffffff);
  864. cpy_ip(jlink_cfg.ip_address, (uint8_t*)&ip_address);
  865. cpy_ip(jlink_cfg.subnet_mask, (uint8_t*)&subnet_mask);
  866. return ERROR_OK;
  867. }
  868. COMMAND_HANDLER(jlink_handle_jlink_reset_command)
  869. {
  870. memset(&jlink_cfg, 0xff, sizeof(jlink_cfg));
  871. return ERROR_OK;
  872. }
  873. COMMAND_HANDLER(jlink_handle_jlink_save_command)
  874. {
  875. if (!(jlink_caps & (1 << EMU_CAP_WRITE_CONFIG)))
  876. {
  877. command_print(CMD_CTX, "J-Link write emulator configuration not supported");
  878. return ERROR_OK;
  879. }
  880. command_print(CMD_CTX, "The J-Link need to be unpluged and repluged ta have the config effective");
  881. return jlink_set_config(&jlink_cfg);
  882. }
  883. COMMAND_HANDLER(jlink_handle_jlink_usb_address_command)
  884. {
  885. uint32_t address;
  886. if (CMD_ARGC < 1)
  887. {
  888. jlink_config_usb_address_dump(CMD_CTX, &jlink_cfg);
  889. return ERROR_OK;
  890. }
  891. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
  892. if (address > 0x3 && address != 0xff)
  893. {
  894. command_print(CMD_CTX, "USB Address must be between 0x00 and 0x03 or 0xff");
  895. return ERROR_COMMAND_SYNTAX_ERROR;
  896. }
  897. jlink_cfg.usb_address = address;
  898. return ERROR_OK;
  899. }
  900. COMMAND_HANDLER(jlink_handle_jlink_config_command)
  901. {
  902. struct jlink_config cfg;
  903. int ret = ERROR_OK;
  904. if (CMD_ARGC == 0)
  905. {
  906. if (!(jlink_caps & (1 << EMU_CAP_READ_CONFIG)))
  907. {
  908. command_print(CMD_CTX, "J-Link read emulator configuration not supported");
  909. goto exit;
  910. }
  911. ret = jlink_get_config(&cfg);
  912. if ( ret != ERROR_OK)
  913. command_print(CMD_CTX, "J-Link read emulator configuration failled");
  914. else
  915. jlink_config_dump(CMD_CTX, &jlink_cfg);
  916. }
  917. exit:
  918. return ret;
  919. }
  920. static const struct command_registration jlink_config_subcommand_handlers[] = {
  921. {
  922. .name = "kickstart",
  923. .handler = &jlink_handle_jlink_kickstart_command,
  924. .mode = COMMAND_EXEC,
  925. .help = "set Kickstart power on JTAG-pin 19.",
  926. .usage = "[val]",
  927. },
  928. {
  929. .name = "mac_address",
  930. .handler = &jlink_handle_jlink_mac_address_command,
  931. .mode = COMMAND_EXEC,
  932. .help = "set the MAC Address",
  933. .usage = "[ff:ff:ff:ff:ff:ff]",
  934. },
  935. {
  936. .name = "ip",
  937. .handler = &jlink_handle_jlink_ip_command,
  938. .mode = COMMAND_EXEC,
  939. .help = "set the ip address of the J-Link Pro, "
  940. "where A.B.C.D is the ip, "
  941. "E the bit of the subnet mask, "
  942. "F.G.H.I the subnet mask",
  943. .usage = "[A.B.C.D[/E] [F.G.H.I]]",
  944. },
  945. {
  946. .name = "reset",
  947. .handler = &jlink_handle_jlink_reset_command,
  948. .mode = COMMAND_EXEC,
  949. .help = "reset the current config",
  950. },
  951. {
  952. .name = "save",
  953. .handler = &jlink_handle_jlink_save_command,
  954. .mode = COMMAND_EXEC,
  955. .help = "save the current config",
  956. },
  957. {
  958. .name = "usb_address",
  959. .handler = &jlink_handle_jlink_usb_address_command,
  960. .mode = COMMAND_EXEC,
  961. .help = "set the USB-Address, "
  962. "This will change the product id",
  963. .usage = "[0x00 to 0x03 or 0xff]",
  964. },
  965. COMMAND_REGISTRATION_DONE
  966. };
  967. static const struct command_registration jlink_subcommand_handlers[] = {
  968. {
  969. .name = "caps",
  970. .handler = &jlink_handle_jlink_caps_command,
  971. .mode = COMMAND_EXEC,
  972. .help = "show jlink capabilities",
  973. },
  974. {
  975. .name = "info",
  976. .handler = &jlink_handle_jlink_info_command,
  977. .mode = COMMAND_EXEC,
  978. .help = "show jlink info",
  979. },
  980. {
  981. .name = "hw_jtag",
  982. .handler = &jlink_handle_jlink_hw_jtag_command,
  983. .mode = COMMAND_EXEC,
  984. .help = "access J-Link HW JTAG command version",
  985. .usage = "[2|3]",
  986. },
  987. {
  988. .name = "config",
  989. .handler = &jlink_handle_jlink_config_command,
  990. .mode = COMMAND_EXEC,
  991. .help = "access J-Link configuration, "
  992. "if no argument this will dump the config",
  993. .chain = jlink_config_subcommand_handlers,
  994. },
  995. {
  996. .name = "pid",
  997. .handler = &jlink_pid_command,
  998. .mode = COMMAND_CONFIG,
  999. .help = "set the pid of the interface we want to use",
  1000. },
  1001. COMMAND_REGISTRATION_DONE
  1002. };
  1003. static const struct command_registration jlink_command_handlers[] = {
  1004. {
  1005. .name = "jlink",
  1006. .mode = COMMAND_ANY,
  1007. .help = "perform jlink management",
  1008. .chain = jlink_subcommand_handlers,
  1009. },
  1010. COMMAND_REGISTRATION_DONE
  1011. };
  1012. struct jtag_interface jlink_interface = {
  1013. .name = "jlink",
  1014. .commands = jlink_command_handlers,
  1015. .execute_queue = jlink_execute_queue,
  1016. .speed = jlink_speed,
  1017. .speed_div = jlink_speed_div,
  1018. .khz = jlink_khz,
  1019. .init = jlink_init,
  1020. .quit = jlink_quit,
  1021. };
  1022. /***************************************************************************/
  1023. /* J-Link tap functions */
  1024. static unsigned tap_length = 0;
  1025. static uint8_t tms_buffer[JLINK_TAP_BUFFER_SIZE];
  1026. static uint8_t tdi_buffer[JLINK_TAP_BUFFER_SIZE];
  1027. static uint8_t tdo_buffer[JLINK_TAP_BUFFER_SIZE];
  1028. struct pending_scan_result {
  1029. int first; /* First bit position in tdo_buffer to read */
  1030. int length; /* Number of bits to read */
  1031. struct scan_command *command; /* Corresponding scan command */
  1032. uint8_t *buffer;
  1033. };
  1034. #define MAX_PENDING_SCAN_RESULTS 256
  1035. static int pending_scan_results_length;
  1036. static struct pending_scan_result pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
  1037. static void jlink_tap_init(void)
  1038. {
  1039. tap_length = 0;
  1040. pending_scan_results_length = 0;
  1041. }
  1042. static void jlink_tap_ensure_space(int scans, int bits)
  1043. {
  1044. int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
  1045. int available_bits = JLINK_TAP_BUFFER_SIZE * 8 - tap_length - 32;
  1046. if (scans > available_scans || bits > available_bits)
  1047. {
  1048. jlink_tap_execute();
  1049. }
  1050. }
  1051. static void jlink_tap_append_step(int tms, int tdi)
  1052. {
  1053. int index_var = tap_length / 8;
  1054. if (index_var >= JLINK_TAP_BUFFER_SIZE)
  1055. {
  1056. LOG_ERROR("jlink_tap_append_step: overflow");
  1057. *(uint32_t *)0xFFFFFFFF = 0;
  1058. exit(-1);
  1059. }
  1060. int bit_index = tap_length % 8;
  1061. uint8_t bit = 1 << bit_index;
  1062. // we do not pad TMS, so be sure to initialize all bits
  1063. if (0 == bit_index)
  1064. {
  1065. tms_buffer[index_var] = tdi_buffer[index_var] = 0;
  1066. }
  1067. if (tms)
  1068. tms_buffer[index_var] |= bit;
  1069. else
  1070. tms_buffer[index_var] &= ~bit;
  1071. if (tdi)
  1072. tdi_buffer[index_var] |= bit;
  1073. else
  1074. tdi_buffer[index_var] &= ~bit;
  1075. tap_length++;
  1076. }
  1077. static void jlink_tap_append_scan(int length, uint8_t *buffer,
  1078. struct scan_command *command)
  1079. {
  1080. struct pending_scan_result *pending_scan_result =
  1081. &pending_scan_results_buffer[pending_scan_results_length];
  1082. int i;
  1083. pending_scan_result->first = tap_length;
  1084. pending_scan_result->length = length;
  1085. pending_scan_result->command = command;
  1086. pending_scan_result->buffer = buffer;
  1087. for (i = 0; i < length; i++)
  1088. {
  1089. int tms = (i < (length - 1)) ? 0 : 1;
  1090. int tdi = (buffer[i / 8] & (1 << (i % 8))) != 0;
  1091. jlink_tap_append_step(tms, tdi);
  1092. }
  1093. pending_scan_results_length++;
  1094. }
  1095. /* Pad and send a tap sequence to the device, and receive the answer.
  1096. * For the purpose of padding we assume that we are in idle or pause state. */
  1097. static int jlink_tap_execute(void)
  1098. {
  1099. int byte_length;
  1100. int i;
  1101. int result;
  1102. if (!tap_length)
  1103. return ERROR_OK;
  1104. /* JLink returns an extra NULL in packet when size of incoming
  1105. * message is a multiple of 64, creates problems with USB comms.
  1106. * WARNING: This will interfere with tap state counting. */
  1107. while ((DIV_ROUND_UP(tap_length, 8) % 64) == 0)
  1108. {
  1109. jlink_tap_append_step((tap_get_state() == TAP_RESET) ? 1 : 0, 0);
  1110. }
  1111. // number of full bytes (plus one if some would be left over)
  1112. byte_length = DIV_ROUND_UP(tap_length, 8);
  1113. bool use_jtag3 = jlink_hw_jtag_version >= 3;
  1114. usb_out_buffer[0] = use_jtag3 ? EMU_CMD_HW_JTAG3 : EMU_CMD_HW_JTAG2;
  1115. usb_out_buffer[1] = 0;
  1116. usb_out_buffer[2] = (tap_length >> 0) & 0xff;
  1117. usb_out_buffer[3] = (tap_length >> 8) & 0xff;
  1118. memcpy(usb_out_buffer + 4, tms_buffer, byte_length);
  1119. memcpy(usb_out_buffer + 4 + byte_length, tdi_buffer, byte_length);
  1120. jlink_last_state = jtag_debug_state_machine(tms_buffer, tdi_buffer,
  1121. tap_length, jlink_last_state);
  1122. result = jlink_usb_message(jlink_handle, 4 + 2 * byte_length, byte_length);
  1123. if (result != byte_length)
  1124. {
  1125. LOG_ERROR("jlink_tap_execute, wrong result %d (expected %d)",
  1126. result, byte_length);
  1127. jlink_tap_init();
  1128. return ERROR_JTAG_QUEUE_FAILED;
  1129. }
  1130. memcpy(tdo_buffer, usb_in_buffer, byte_length);
  1131. for (i = 0; i < pending_scan_results_length; i++)
  1132. {
  1133. struct pending_scan_result *pending_scan_result = &pending_scan_results_buffer[i];
  1134. uint8_t *buffer = pending_scan_result->buffer;
  1135. int length = pending_scan_result->length;
  1136. int first = pending_scan_result->first;
  1137. struct scan_command *command = pending_scan_result->command;
  1138. /* Copy to buffer */
  1139. buf_set_buf(tdo_buffer, first, buffer, 0, length);
  1140. DEBUG_JTAG_IO("pending scan result, length = %d", length);
  1141. jlink_debug_buffer(buffer, DIV_ROUND_UP(length, 8));
  1142. if (jtag_read_buffer(buffer, command) != ERROR_OK)
  1143. {
  1144. jlink_tap_init();
  1145. return ERROR_JTAG_QUEUE_FAILED;
  1146. }
  1147. if (pending_scan_result->buffer != NULL)
  1148. {
  1149. free(pending_scan_result->buffer);
  1150. }
  1151. }
  1152. jlink_tap_init();
  1153. return ERROR_OK;
  1154. }
  1155. /*****************************************************************************/
  1156. /* JLink USB low-level functions */
  1157. static struct jlink* jlink_usb_open()
  1158. {
  1159. struct jtag_libusb_device_handle *devh;
  1160. if (jtag_libusb_open(vids, pids, &devh) != ERROR_OK)
  1161. return NULL;
  1162. /* BE ***VERY CAREFUL*** ABOUT MAKING CHANGES IN THIS
  1163. * AREA!!!!!!!!!!! The behavior of libusb is not completely
  1164. * consistent across Windows, Linux, and Mac OS X platforms.
  1165. * The actions taken in the following compiler conditionals may
  1166. * not agree with published documentation for libusb, but were
  1167. * found to be necessary through trials and tribulations. Even
  1168. * little tweaks can break one or more platforms, so if you do
  1169. * make changes test them carefully on all platforms before
  1170. * committing them!
  1171. */
  1172. #if IS_WIN32 == 0
  1173. jtag_libusb_reset_device(devh);
  1174. #if IS_DARWIN == 0
  1175. int timeout = 5;
  1176. /* reopen jlink after usb_reset
  1177. * on win32 this may take a second or two to re-enumerate */
  1178. int retval;
  1179. while ((retval = jtag_libusb_open(vids, pids, &devh)) != ERROR_OK)
  1180. {
  1181. usleep(1000);
  1182. timeout--;
  1183. if (!timeout) {
  1184. break;
  1185. }
  1186. }
  1187. if (ERROR_OK != retval)
  1188. return NULL;
  1189. #endif
  1190. #endif
  1191. /* usb_set_configuration required under win32 */
  1192. struct jtag_libusb_device *udev = jtag_libusb_get_device(devh);
  1193. jtag_libusb_set_configuration(devh, 0);
  1194. jtag_libusb_claim_interface(devh, 0);
  1195. #if 0
  1196. /*
  1197. * This makes problems under Mac OS X. And is not needed
  1198. * under Windows. Hopefully this will not break a linux build
  1199. */
  1200. usb_set_altinterface(result->usb_handle, 0);
  1201. #endif
  1202. jtag_libusb_get_endpoints(udev, &jlink_read_ep, &jlink_write_ep);
  1203. struct jlink *result = malloc(sizeof(struct jlink));
  1204. result->usb_handle = devh;
  1205. return result;
  1206. }
  1207. static void jlink_usb_close(struct jlink *jlink)
  1208. {
  1209. jtag_libusb_close(jlink->usb_handle);
  1210. free(jlink);
  1211. }
  1212. /* Send a message and receive the reply. */
  1213. static int jlink_usb_message(struct jlink *jlink, int out_length, int in_length)
  1214. {
  1215. int result;
  1216. result = jlink_usb_write(jlink, out_length);
  1217. if (result != out_length)
  1218. {
  1219. LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)",
  1220. out_length, result);
  1221. return ERROR_JTAG_DEVICE_ERROR;
  1222. }
  1223. result = jlink_usb_read(jlink, in_length);
  1224. if ((result != in_length) && (result != (in_length + 1)))
  1225. {
  1226. LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)",
  1227. in_length, result);
  1228. return ERROR_JTAG_DEVICE_ERROR;
  1229. }
  1230. if (jlink_hw_jtag_version < 3)
  1231. return result;
  1232. int result2 = ERROR_OK;
  1233. if (result == in_length)
  1234. {
  1235. /* Must read the result from the EMU too */
  1236. result2 = jlink_usb_read_emu_result(jlink);
  1237. if (1 != result2)
  1238. {
  1239. LOG_ERROR("jlink_usb_read_emu_result retried requested = 1, "
  1240. "result=%d, in_length=%i", result2, in_length);
  1241. /* Try again once, should only happen if (in_length%64 == 0) */
  1242. result2 = jlink_usb_read_emu_result(jlink);
  1243. if (1 != result2)
  1244. {
  1245. LOG_ERROR("jlink_usb_read_emu_result failed "
  1246. "(requested = 1, result=%d)", result2);
  1247. return ERROR_JTAG_DEVICE_ERROR;
  1248. }
  1249. }
  1250. /* Check the result itself */
  1251. result2 = usb_emu_result_buffer[0];
  1252. }
  1253. else
  1254. {
  1255. /* Save the result, then remove it from return value */
  1256. result2 = usb_in_buffer[result--];
  1257. }
  1258. if (result2)
  1259. {
  1260. LOG_ERROR("jlink_usb_message failed with result=%d)", result2);
  1261. return ERROR_JTAG_DEVICE_ERROR;
  1262. }
  1263. return result;
  1264. }
  1265. /* calls the given usb_bulk_* function, allowing for the data to
  1266. * trickle in with some timeouts */
  1267. static int usb_bulk_with_retries(
  1268. int (*f)(jtag_libusb_device_handle *, int, char *, int, int),
  1269. jtag_libusb_device_handle *dev, int ep,
  1270. char *bytes, int size, int timeout)
  1271. {
  1272. int tries = 3, count = 0;
  1273. while (tries && (count < size))
  1274. {
  1275. int result = f(dev, ep, bytes + count, size - count, timeout);
  1276. if (result > 0)
  1277. count += result;
  1278. else if ((-ETIMEDOUT != result) || !--tries)
  1279. return result;
  1280. }
  1281. return count;
  1282. }
  1283. static int wrap_usb_bulk_write(jtag_libusb_device_handle *dev, int ep,
  1284. char *buff, int size, int timeout)
  1285. {
  1286. /* usb_bulk_write() takes const char *buff */
  1287. return jtag_libusb_bulk_write(dev, ep, buff, size, timeout);
  1288. }
  1289. static inline int usb_bulk_write_ex(jtag_libusb_device_handle *dev, int ep,
  1290. char *bytes, int size, int timeout)
  1291. {
  1292. return usb_bulk_with_retries(&wrap_usb_bulk_write,
  1293. dev, ep, bytes, size, timeout);
  1294. }
  1295. static inline int usb_bulk_read_ex(jtag_libusb_device_handle *dev, int ep,
  1296. char *bytes, int size, int timeout)
  1297. {
  1298. return usb_bulk_with_retries(&jtag_libusb_bulk_read,
  1299. dev, ep, bytes, size, timeout);
  1300. }
  1301. /* Write data from out_buffer to USB. */
  1302. static int jlink_usb_write(struct jlink *jlink, int out_length)
  1303. {
  1304. int result;
  1305. if (out_length > JLINK_OUT_BUFFER_SIZE)
  1306. {
  1307. LOG_ERROR("jlink_write illegal out_length=%d (max=%d)",
  1308. out_length, JLINK_OUT_BUFFER_SIZE);
  1309. return -1;
  1310. }
  1311. result = usb_bulk_write_ex(jlink->usb_handle, jlink_write_ep,
  1312. (char *)usb_out_buffer, out_length, JLINK_USB_TIMEOUT);
  1313. DEBUG_JTAG_IO("jlink_usb_write, out_length = %d, result = %d",
  1314. out_length, result);
  1315. jlink_debug_buffer(usb_out_buffer, out_length);
  1316. return result;
  1317. }
  1318. /* Read data from USB into in_buffer. */
  1319. static int jlink_usb_read(struct jlink *jlink, int expected_size)
  1320. {
  1321. int result = usb_bulk_read_ex(jlink->usb_handle, jlink_read_ep,
  1322. (char *)usb_in_buffer, expected_size, JLINK_USB_TIMEOUT);
  1323. DEBUG_JTAG_IO("jlink_usb_read, result = %d", result);
  1324. jlink_debug_buffer(usb_in_buffer, result);
  1325. return result;
  1326. }
  1327. /* Read the result from the previous EMU cmd into result_buffer. */
  1328. static int jlink_usb_read_emu_result(struct jlink *jlink)
  1329. {
  1330. int result = usb_bulk_read_ex(jlink->usb_handle, jlink_read_ep,
  1331. (char *)usb_emu_result_buffer, 1 /* JLINK_EMU_RESULT_BUFFER_SIZE */,
  1332. JLINK_USB_TIMEOUT);
  1333. DEBUG_JTAG_IO("jlink_usb_read_result, result = %d", result);
  1334. jlink_debug_buffer(usb_emu_result_buffer, result);
  1335. return result;
  1336. }
  1337. #ifdef _DEBUG_USB_COMMS_
  1338. #define BYTES_PER_LINE 16
  1339. static void jlink_debug_buffer(uint8_t *buffer, int length)
  1340. {
  1341. char line[81];
  1342. char s[4];
  1343. int i;
  1344. int j;
  1345. for (i = 0; i < length; i += BYTES_PER_LINE)
  1346. {
  1347. snprintf(line, 5, "%04x", i);
  1348. for (j = i; j < i + BYTES_PER_LINE && j < length; j++)
  1349. {
  1350. snprintf(s, 4, " %02x", buffer[j]);
  1351. strcat(line, s);
  1352. }
  1353. LOG_DEBUG("%s", line);
  1354. }
  1355. }
  1356. #endif