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.
 
 
 
 
 
 

1567 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. case JTAG_RUNTEST:
  280. jlink_execute_runtest(cmd);
  281. break;
  282. case JTAG_TLR_RESET:
  283. jlink_execute_statemove(cmd);
  284. break;
  285. case JTAG_PATHMOVE:
  286. jlink_execute_pathmove(cmd);
  287. break;
  288. case JTAG_SCAN:
  289. jlink_execute_scan(cmd);
  290. break;
  291. case JTAG_RESET:
  292. jlink_execute_reset(cmd);
  293. break;
  294. case JTAG_SLEEP:
  295. jlink_execute_sleep(cmd);
  296. break;
  297. default:
  298. LOG_ERROR("BUG: unknown JTAG command type encountered");
  299. exit(-1);
  300. }
  301. }
  302. static int jlink_execute_queue(void)
  303. {
  304. struct jtag_command *cmd = jtag_command_queue;
  305. while (cmd != NULL) {
  306. jlink_execute_command(cmd);
  307. cmd = cmd->next;
  308. }
  309. return jlink_tap_execute();
  310. }
  311. /* Sets speed in kHz. */
  312. static int jlink_speed(int speed)
  313. {
  314. int result;
  315. if (speed > JLINK_MAX_SPEED) {
  316. LOG_INFO("reduce speed request: %dkHz to %dkHz maximum",
  317. speed, JLINK_MAX_SPEED);
  318. speed = JLINK_MAX_SPEED;
  319. }
  320. /* check for RTCK setting */
  321. if (speed == 0)
  322. speed = -1;
  323. usb_out_buffer[0] = EMU_CMD_SET_SPEED;
  324. usb_out_buffer[1] = (speed >> 0) & 0xff;
  325. usb_out_buffer[2] = (speed >> 8) & 0xff;
  326. result = jlink_usb_write(jlink_handle, 3);
  327. if (result != 3) {
  328. LOG_ERROR("J-Link setting speed failed (%d)", result);
  329. return ERROR_JTAG_DEVICE_ERROR;
  330. }
  331. return ERROR_OK;
  332. }
  333. static int jlink_speed_div(int speed, int *khz)
  334. {
  335. *khz = speed;
  336. return ERROR_OK;
  337. }
  338. static int jlink_khz(int khz, int *jtag_speed)
  339. {
  340. *jtag_speed = khz;
  341. return ERROR_OK;
  342. }
  343. static int jlink_init(void)
  344. {
  345. int i;
  346. jlink_handle = jlink_usb_open();
  347. if (jlink_handle == 0) {
  348. LOG_ERROR("Cannot find jlink Interface! Please check "
  349. "connection and permissions.");
  350. return ERROR_JTAG_INIT_FAILED;
  351. }
  352. /*
  353. * The next three instructions were added after discovering a problem
  354. * while using an oscilloscope.
  355. * For the V8 SAM-ICE dongle (and likely other j-link device variants),
  356. * the reset line to the target microprocessor was found to cycle only
  357. * intermittently during emulator startup (even after encountering the
  358. * downstream reset instruction later in the code).
  359. * This was found to create two issues:
  360. * 1) In general it is a bad practice to not reset a CPU to a known
  361. * state when starting an emulator and
  362. * 2) something critical happens inside the dongle when it does the
  363. * first read following a new USB session.
  364. * Keeping the processor in reset during the first read collecting
  365. * version information seems to prevent errant
  366. * "J-Link command EMU_CMD_VERSION failed" issues.
  367. */
  368. LOG_INFO("J-Link initialization started / target CPU reset initiated");
  369. jlink_simple_command(EMU_CMD_HW_TRST0);
  370. jlink_simple_command(EMU_CMD_HW_RESET0);
  371. usleep(1000);
  372. jlink_hw_jtag_version = 2;
  373. if (jlink_get_version_info() == ERROR_OK) {
  374. /* attempt to get status */
  375. jlink_get_status();
  376. }
  377. LOG_INFO("J-Link JTAG Interface ready");
  378. jlink_reset(0, 0);
  379. jtag_sleep(3000);
  380. jlink_tap_init();
  381. /* v5/6 jlink seems to have an issue if the first tap move
  382. * is not divisible by 8, so we send a TLR on first power up */
  383. for (i = 0; i < 8; i++)
  384. jlink_tap_append_step(1, 0);
  385. jlink_tap_execute();
  386. return ERROR_OK;
  387. }
  388. static int jlink_quit(void)
  389. {
  390. jlink_usb_close(jlink_handle);
  391. return ERROR_OK;
  392. }
  393. /***************************************************************************/
  394. /* Queue command implementations */
  395. static void jlink_end_state(tap_state_t state)
  396. {
  397. if (tap_is_state_stable(state))
  398. tap_set_end_state(state);
  399. else {
  400. LOG_ERROR("BUG: %i is not a valid end state", state);
  401. exit(-1);
  402. }
  403. }
  404. /* Goes to the end state. */
  405. static void jlink_state_move(void)
  406. {
  407. int i;
  408. int tms = 0;
  409. uint8_t tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
  410. uint8_t tms_scan_bits = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
  411. for (i = 0; i < tms_scan_bits; i++) {
  412. tms = (tms_scan >> i) & 1;
  413. jlink_tap_append_step(tms, 0);
  414. }
  415. tap_set_state(tap_get_end_state());
  416. }
  417. static void jlink_path_move(int num_states, tap_state_t *path)
  418. {
  419. int i;
  420. for (i = 0; i < num_states; i++) {
  421. if (path[i] == tap_state_transition(tap_get_state(), false))
  422. jlink_tap_append_step(0, 0);
  423. else if (path[i] == tap_state_transition(tap_get_state(), true))
  424. jlink_tap_append_step(1, 0);
  425. else {
  426. LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
  427. tap_state_name(tap_get_state()), tap_state_name(path[i]));
  428. exit(-1);
  429. }
  430. tap_set_state(path[i]);
  431. }
  432. tap_set_end_state(tap_get_state());
  433. }
  434. static void jlink_runtest(int num_cycles)
  435. {
  436. int i;
  437. tap_state_t saved_end_state = tap_get_end_state();
  438. jlink_tap_ensure_space(1, num_cycles + 16);
  439. /* only do a state_move when we're not already in IDLE */
  440. if (tap_get_state() != TAP_IDLE) {
  441. jlink_end_state(TAP_IDLE);
  442. jlink_state_move();
  443. /* num_cycles--; */
  444. }
  445. /* execute num_cycles */
  446. for (i = 0; i < num_cycles; i++)
  447. jlink_tap_append_step(0, 0);
  448. /* finish in end_state */
  449. jlink_end_state(saved_end_state);
  450. if (tap_get_state() != tap_get_end_state())
  451. jlink_state_move();
  452. }
  453. static void jlink_scan(bool ir_scan, enum scan_type type, uint8_t *buffer,
  454. int scan_size, struct scan_command *command)
  455. {
  456. tap_state_t saved_end_state;
  457. jlink_tap_ensure_space(1, scan_size + 16);
  458. saved_end_state = tap_get_end_state();
  459. /* Move to appropriate scan state */
  460. jlink_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
  461. /* Only move if we're not already there */
  462. if (tap_get_state() != tap_get_end_state())
  463. jlink_state_move();
  464. jlink_end_state(saved_end_state);
  465. /* Scan */
  466. jlink_tap_append_scan(scan_size, buffer, command);
  467. /* We are in Exit1, go to Pause */
  468. jlink_tap_append_step(0, 0);
  469. tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
  470. if (tap_get_state() != tap_get_end_state())
  471. jlink_state_move();
  472. }
  473. static void jlink_reset(int trst, int srst)
  474. {
  475. LOG_DEBUG("trst: %i, srst: %i", trst, srst);
  476. /* Signals are active low */
  477. if (srst == 0)
  478. jlink_simple_command(EMU_CMD_HW_RESET1);
  479. if (srst == 1)
  480. jlink_simple_command(EMU_CMD_HW_RESET0);
  481. if (trst == 1)
  482. jlink_simple_command(EMU_CMD_HW_TRST0);
  483. if (trst == 0)
  484. jlink_simple_command(EMU_CMD_HW_TRST1);
  485. }
  486. static void jlink_simple_command(uint8_t command)
  487. {
  488. int result;
  489. DEBUG_JTAG_IO("0x%02x", command);
  490. usb_out_buffer[0] = command;
  491. result = jlink_usb_write(jlink_handle, 1);
  492. if (result != 1)
  493. LOG_ERROR("J-Link command 0x%02x failed (%d)", command, result);
  494. }
  495. static int jlink_get_status(void)
  496. {
  497. int result;
  498. jlink_simple_command(EMU_CMD_GET_STATE);
  499. result = jlink_usb_read(jlink_handle, 8);
  500. if (result != 8) {
  501. LOG_ERROR("J-Link command EMU_CMD_GET_STATE failed (%d)", result);
  502. return ERROR_JTAG_DEVICE_ERROR;
  503. }
  504. int vref = usb_in_buffer[0] + (usb_in_buffer[1] << 8);
  505. LOG_INFO("Vref = %d.%d TCK = %d TDI = %d TDO = %d TMS = %d SRST = %d TRST = %d", \
  506. vref / 1000, vref % 1000, \
  507. usb_in_buffer[2], usb_in_buffer[3], usb_in_buffer[4], \
  508. usb_in_buffer[5], usb_in_buffer[6], usb_in_buffer[7]);
  509. if (vref < 1500)
  510. LOG_ERROR("Vref too low. Check Target Power");
  511. return ERROR_OK;
  512. }
  513. #define jlink_dump_printf(context, expr ...) \
  514. do { \
  515. if (context) \
  516. command_print(context, expr); \
  517. else \
  518. LOG_INFO(expr); \
  519. } while (0);
  520. static void jlink_caps_dump(struct command_context *ctx)
  521. {
  522. int i;
  523. jlink_dump_printf(ctx, "J-Link Capabilities");
  524. for (i = 1; i < 31; i++)
  525. if (jlink_caps & (1 << i))
  526. jlink_dump_printf(ctx, "%s", jlink_cap_str[i]);
  527. }
  528. static void jlink_config_usb_address_dump(struct command_context *ctx, struct jlink_config *cfg)
  529. {
  530. if (!cfg)
  531. return;
  532. jlink_dump_printf(ctx, "USB-Address: 0x%x", cfg->usb_address);
  533. }
  534. static void jlink_config_kickstart_dump(struct command_context *ctx, struct jlink_config *cfg)
  535. {
  536. if (!cfg)
  537. return;
  538. jlink_dump_printf(ctx, "Kickstart power on JTAG-pin 19: 0x%x",
  539. cfg->kickstart_power_on_jtag_pin_19);
  540. }
  541. static void jlink_config_mac_address_dump(struct command_context *ctx, struct jlink_config *cfg)
  542. {
  543. if (!cfg)
  544. return;
  545. jlink_dump_printf(ctx, "MAC Address: %.02x:%.02x:%.02x:%.02x:%.02x:%.02x",
  546. cfg->mac_address[5], cfg->mac_address[4],
  547. cfg->mac_address[3], cfg->mac_address[2],
  548. cfg->mac_address[1], cfg->mac_address[0]);
  549. }
  550. static void jlink_config_ip_dump(struct command_context *ctx, struct jlink_config *cfg)
  551. {
  552. if (!cfg)
  553. return;
  554. jlink_dump_printf(ctx, "IP Address: %d.%d.%d.%d",
  555. cfg->ip_address[3], cfg->ip_address[2],
  556. cfg->ip_address[1], cfg->ip_address[0]);
  557. jlink_dump_printf(ctx, "Subnet Mask: %d.%d.%d.%d",
  558. cfg->subnet_mask[3], cfg->subnet_mask[2],
  559. cfg->subnet_mask[1], cfg->subnet_mask[0]);
  560. }
  561. static void jlink_config_dump(struct command_context *ctx, struct jlink_config *cfg)
  562. {
  563. if (!cfg)
  564. return;
  565. jlink_dump_printf(ctx, "J-Link configuration");
  566. jlink_config_usb_address_dump(ctx, cfg);
  567. jlink_config_kickstart_dump(ctx, cfg);
  568. if (jlink_hw_type == JLINK_HW_TYPE_JLINK_PRO) {
  569. jlink_config_ip_dump(ctx, cfg);
  570. jlink_config_mac_address_dump(ctx, cfg);
  571. }
  572. }
  573. static int jlink_get_config(struct jlink_config *cfg)
  574. {
  575. int result;
  576. int size = sizeof(struct jlink_config);
  577. jlink_simple_command(EMU_CMD_READ_CONFIG);
  578. result = jlink_usb_read(jlink_handle, size);
  579. if (size != result) {
  580. LOG_ERROR("jlink_usb_read failed (requested=%d, result=%d)", size, result);
  581. return ERROR_FAIL;
  582. }
  583. memcpy(cfg, usb_in_buffer, size);
  584. /*
  585. * Section 4.2.4 IN-transaction
  586. * read dummy 0-byte packet
  587. */
  588. jlink_usb_read(jlink_handle, 1);
  589. return ERROR_OK;
  590. }
  591. static int jlink_set_config(struct jlink_config *cfg)
  592. {
  593. int result;
  594. int size = sizeof(struct jlink_config);
  595. jlink_simple_command(EMU_CMD_WRITE_CONFIG);
  596. memcpy(usb_out_buffer, cfg, size);
  597. result = jlink_usb_write(jlink_handle, size);
  598. if (result != size) {
  599. LOG_ERROR("jlink_usb_write failed (requested=%d, result=%d)", 256, result);
  600. return ERROR_FAIL;
  601. }
  602. return ERROR_OK;
  603. }
  604. static int jlink_get_version_info(void)
  605. {
  606. int result;
  607. int len;
  608. uint32_t jlink_max_size;
  609. /* query hardware version */
  610. jlink_simple_command(EMU_CMD_VERSION);
  611. result = jlink_usb_read(jlink_handle, 2);
  612. if (2 != result) {
  613. LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)", result);
  614. return ERROR_JTAG_DEVICE_ERROR;
  615. }
  616. len = buf_get_u32(usb_in_buffer, 0, 16);
  617. if (len > JLINK_IN_BUFFER_SIZE) {
  618. LOG_ERROR("J-Link command EMU_CMD_VERSION impossible return length 0x%0x", len);
  619. len = JLINK_IN_BUFFER_SIZE;
  620. }
  621. result = jlink_usb_read(jlink_handle, len);
  622. if (result != len) {
  623. LOG_ERROR("J-Link command EMU_CMD_VERSION failed (%d)", result);
  624. return ERROR_JTAG_DEVICE_ERROR;
  625. }
  626. usb_in_buffer[result] = 0;
  627. LOG_INFO("%s", (char *)usb_in_buffer);
  628. /* query hardware capabilities */
  629. jlink_simple_command(EMU_CMD_GET_CAPS);
  630. result = jlink_usb_read(jlink_handle, 4);
  631. if (4 != result) {
  632. LOG_ERROR("J-Link command EMU_CMD_GET_CAPS failed (%d)", result);
  633. return ERROR_JTAG_DEVICE_ERROR;
  634. }
  635. jlink_caps = buf_get_u32(usb_in_buffer, 0, 32);
  636. LOG_INFO("J-Link caps 0x%x", (unsigned)jlink_caps);
  637. if (jlink_caps & (1 << EMU_CAP_GET_HW_VERSION)) {
  638. /* query hardware version */
  639. jlink_simple_command(EMU_CMD_GET_HW_VERSION);
  640. result = jlink_usb_read(jlink_handle, 4);
  641. if (4 != result) {
  642. LOG_ERROR("J-Link command EMU_CMD_GET_HW_VERSION failed (%d)", result);
  643. return ERROR_JTAG_DEVICE_ERROR;
  644. }
  645. uint32_t jlink_hw_version = buf_get_u32(usb_in_buffer, 0, 32);
  646. uint32_t major_revision = (jlink_hw_version / 10000) % 100;
  647. jlink_hw_type = (jlink_hw_version / 1000000) % 100;
  648. if (major_revision >= 5)
  649. jlink_hw_jtag_version = 3;
  650. LOG_INFO("J-Link hw version %i", (int)jlink_hw_version);
  651. if (jlink_hw_type >= JLINK_HW_TYPE_MAX)
  652. LOG_INFO("J-Link hw type uknown 0x%x", jlink_hw_type);
  653. else
  654. LOG_INFO("J-Link hw type %s", jlink_hw_type_str[jlink_hw_type]);
  655. }
  656. if (jlink_caps & (1 << EMU_CAP_GET_MAX_BLOCK_SIZE)) {
  657. /* query hardware maximum memory block */
  658. jlink_simple_command(EMU_CMD_GET_MAX_MEM_BLOCK);
  659. result = jlink_usb_read(jlink_handle, 4);
  660. if (4 != result) {
  661. LOG_ERROR("J-Link command EMU_CMD_GET_MAX_MEM_BLOCK failed (%d)", result);
  662. return ERROR_JTAG_DEVICE_ERROR;
  663. }
  664. jlink_max_size = buf_get_u32(usb_in_buffer, 0, 32);
  665. LOG_INFO("J-Link max mem block %i", (int)jlink_max_size);
  666. }
  667. if (jlink_caps & (1 << EMU_CAP_READ_CONFIG)) {
  668. if (jlink_get_config(&jlink_cfg) != ERROR_OK)
  669. return ERROR_JTAG_DEVICE_ERROR;
  670. jlink_config_dump(NULL, &jlink_cfg);
  671. }
  672. return ERROR_OK;
  673. }
  674. COMMAND_HANDLER(jlink_pid_command)
  675. {
  676. if (CMD_ARGC != 1) {
  677. LOG_ERROR("Need exactly one argument to jlink_pid");
  678. return ERROR_FAIL;
  679. }
  680. pids[0] = strtoul(CMD_ARGV[0], NULL, 16);
  681. pids[1] = 0;
  682. vids[1] = 0;
  683. return ERROR_OK;
  684. }
  685. COMMAND_HANDLER(jlink_handle_jlink_info_command)
  686. {
  687. if (jlink_get_version_info() == ERROR_OK) {
  688. /* attempt to get status */
  689. jlink_get_status();
  690. }
  691. return ERROR_OK;
  692. }
  693. COMMAND_HANDLER(jlink_handle_jlink_caps_command)
  694. {
  695. jlink_caps_dump(CMD_CTX);
  696. return ERROR_OK;
  697. }
  698. COMMAND_HANDLER(jlink_handle_jlink_hw_jtag_command)
  699. {
  700. switch (CMD_ARGC) {
  701. case 0:
  702. command_print(CMD_CTX, "J-Link hw jtag %i", jlink_hw_jtag_version);
  703. break;
  704. case 1: {
  705. int request_version = atoi(CMD_ARGV[0]);
  706. switch (request_version) {
  707. case 2:
  708. case 3:
  709. jlink_hw_jtag_version = request_version;
  710. break;
  711. default:
  712. return ERROR_COMMAND_SYNTAX_ERROR;
  713. }
  714. break;
  715. }
  716. default:
  717. return ERROR_COMMAND_SYNTAX_ERROR;
  718. }
  719. return ERROR_OK;
  720. }
  721. COMMAND_HANDLER(jlink_handle_jlink_kickstart_command)
  722. {
  723. uint32_t kickstart;
  724. if (CMD_ARGC < 1) {
  725. jlink_config_kickstart_dump(CMD_CTX, &jlink_cfg);
  726. return ERROR_OK;
  727. }
  728. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], kickstart);
  729. jlink_cfg.kickstart_power_on_jtag_pin_19 = kickstart;
  730. return ERROR_OK;
  731. }
  732. COMMAND_HANDLER(jlink_handle_jlink_mac_address_command)
  733. {
  734. uint8_t addr[6];
  735. int i;
  736. char *e;
  737. const char *str;
  738. if (CMD_ARGC < 1) {
  739. jlink_config_mac_address_dump(CMD_CTX, &jlink_cfg);
  740. return ERROR_OK;
  741. }
  742. str = CMD_ARGV[0];
  743. if ((strlen(str) != 17) || (str[2] != ':' || str[5] != ':' || str[8] != ':' ||
  744. str[11] != ':' || str[14] != ':')) {
  745. command_print(CMD_CTX, "ethaddr miss format ff:ff:ff:ff:ff:ff");
  746. return ERROR_COMMAND_SYNTAX_ERROR;
  747. }
  748. for (i = 5; i >= 0; i--) {
  749. addr[i] = strtoul(str, &e, 16);
  750. str = e + 1;
  751. }
  752. if (!(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5])) {
  753. command_print(CMD_CTX, "invalid it's zero mac_address");
  754. return ERROR_COMMAND_SYNTAX_ERROR;
  755. }
  756. if (!(0x01 & addr[0])) {
  757. command_print(CMD_CTX, "invalid it's a multicat mac_address");
  758. return ERROR_COMMAND_SYNTAX_ERROR;
  759. }
  760. memcpy(jlink_cfg.mac_address, addr, sizeof(addr));
  761. return ERROR_OK;
  762. }
  763. static int string_to_ip(const char *s, uint8_t *ip, int *pos)
  764. {
  765. uint8_t lip[4];
  766. char *e;
  767. const char *s_save = s;
  768. int i;
  769. if (!s)
  770. return -EINVAL;
  771. for (i = 0; i < 4; i++) {
  772. lip[i] = strtoul(s, &e, 10);
  773. if (*e != '.' && i != 3)
  774. return -EINVAL;
  775. s = e + 1;
  776. }
  777. *pos = e - s_save;
  778. memcpy(ip, lip, sizeof(lip));
  779. return ERROR_OK;
  780. }
  781. static void cpy_ip(uint8_t *dst, uint8_t *src)
  782. {
  783. int i, j;
  784. for (i = 0, j = 3; i < 4; i++, j--)
  785. dst[i] = src[j];
  786. }
  787. COMMAND_HANDLER(jlink_handle_jlink_ip_command)
  788. {
  789. uint32_t ip_address;
  790. uint32_t subnet_mask = 0;
  791. int i, len;
  792. int ret;
  793. uint8_t subnet_bits = 24;
  794. if (CMD_ARGC < 1) {
  795. jlink_config_ip_dump(CMD_CTX, &jlink_cfg);
  796. return ERROR_OK;
  797. }
  798. ret = string_to_ip(CMD_ARGV[0], (uint8_t *)&ip_address, &i);
  799. if (ret != ERROR_OK)
  800. return ret;
  801. len = strlen(CMD_ARGV[0]);
  802. /* check for this format A.B.C.D/E */
  803. if (i < len) {
  804. if (CMD_ARGV[0][i] != '/')
  805. return ERROR_COMMAND_SYNTAX_ERROR;
  806. COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0] + i + 1, subnet_bits);
  807. } else {
  808. if (CMD_ARGC > 1) {
  809. ret = string_to_ip(CMD_ARGV[1], (uint8_t *)&subnet_mask, &i);
  810. if (ret != ERROR_OK)
  811. return ret;
  812. }
  813. }
  814. if (!subnet_mask)
  815. subnet_mask = (uint32_t)(subnet_bits < 32 ?
  816. ((1ULL << subnet_bits) - 1) : 0xffffffff);
  817. cpy_ip(jlink_cfg.ip_address, (uint8_t *)&ip_address);
  818. cpy_ip(jlink_cfg.subnet_mask, (uint8_t *)&subnet_mask);
  819. return ERROR_OK;
  820. }
  821. COMMAND_HANDLER(jlink_handle_jlink_reset_command)
  822. {
  823. memset(&jlink_cfg, 0xff, sizeof(jlink_cfg));
  824. return ERROR_OK;
  825. }
  826. COMMAND_HANDLER(jlink_handle_jlink_save_command)
  827. {
  828. if (!(jlink_caps & (1 << EMU_CAP_WRITE_CONFIG))) {
  829. command_print(CMD_CTX, "J-Link write emulator configuration not supported");
  830. return ERROR_OK;
  831. }
  832. command_print(CMD_CTX, "The J-Link need to be unpluged and repluged ta have the config effective");
  833. return jlink_set_config(&jlink_cfg);
  834. }
  835. COMMAND_HANDLER(jlink_handle_jlink_usb_address_command)
  836. {
  837. uint32_t address;
  838. if (CMD_ARGC < 1) {
  839. jlink_config_usb_address_dump(CMD_CTX, &jlink_cfg);
  840. return ERROR_OK;
  841. }
  842. COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
  843. if (address > 0x3 && address != 0xff) {
  844. command_print(CMD_CTX, "USB Address must be between 0x00 and 0x03 or 0xff");
  845. return ERROR_COMMAND_SYNTAX_ERROR;
  846. }
  847. jlink_cfg.usb_address = address;
  848. return ERROR_OK;
  849. }
  850. COMMAND_HANDLER(jlink_handle_jlink_config_command)
  851. {
  852. struct jlink_config cfg;
  853. int ret = ERROR_OK;
  854. if (CMD_ARGC == 0) {
  855. if (!(jlink_caps & (1 << EMU_CAP_READ_CONFIG))) {
  856. command_print(CMD_CTX, "J-Link read emulator configuration not supported");
  857. goto exit;
  858. }
  859. ret = jlink_get_config(&cfg);
  860. if (ret != ERROR_OK)
  861. command_print(CMD_CTX, "J-Link read emulator configuration failled");
  862. else
  863. jlink_config_dump(CMD_CTX, &jlink_cfg);
  864. }
  865. exit:
  866. return ret;
  867. }
  868. static const struct command_registration jlink_config_subcommand_handlers[] = {
  869. {
  870. .name = "kickstart",
  871. .handler = &jlink_handle_jlink_kickstart_command,
  872. .mode = COMMAND_EXEC,
  873. .help = "set Kickstart power on JTAG-pin 19.",
  874. .usage = "[val]",
  875. },
  876. {
  877. .name = "mac_address",
  878. .handler = &jlink_handle_jlink_mac_address_command,
  879. .mode = COMMAND_EXEC,
  880. .help = "set the MAC Address",
  881. .usage = "[ff:ff:ff:ff:ff:ff]",
  882. },
  883. {
  884. .name = "ip",
  885. .handler = &jlink_handle_jlink_ip_command,
  886. .mode = COMMAND_EXEC,
  887. .help = "set the ip address of the J-Link Pro, "
  888. "where A.B.C.D is the ip, "
  889. "E the bit of the subnet mask, "
  890. "F.G.H.I the subnet mask",
  891. .usage = "[A.B.C.D[/E] [F.G.H.I]]",
  892. },
  893. {
  894. .name = "reset",
  895. .handler = &jlink_handle_jlink_reset_command,
  896. .mode = COMMAND_EXEC,
  897. .help = "reset the current config",
  898. },
  899. {
  900. .name = "save",
  901. .handler = &jlink_handle_jlink_save_command,
  902. .mode = COMMAND_EXEC,
  903. .help = "save the current config",
  904. },
  905. {
  906. .name = "usb_address",
  907. .handler = &jlink_handle_jlink_usb_address_command,
  908. .mode = COMMAND_EXEC,
  909. .help = "set the USB-Address, "
  910. "This will change the product id",
  911. .usage = "[0x00 to 0x03 or 0xff]",
  912. },
  913. COMMAND_REGISTRATION_DONE
  914. };
  915. static const struct command_registration jlink_subcommand_handlers[] = {
  916. {
  917. .name = "caps",
  918. .handler = &jlink_handle_jlink_caps_command,
  919. .mode = COMMAND_EXEC,
  920. .help = "show jlink capabilities",
  921. },
  922. {
  923. .name = "info",
  924. .handler = &jlink_handle_jlink_info_command,
  925. .mode = COMMAND_EXEC,
  926. .help = "show jlink info",
  927. },
  928. {
  929. .name = "hw_jtag",
  930. .handler = &jlink_handle_jlink_hw_jtag_command,
  931. .mode = COMMAND_EXEC,
  932. .help = "access J-Link HW JTAG command version",
  933. .usage = "[2|3]",
  934. },
  935. {
  936. .name = "config",
  937. .handler = &jlink_handle_jlink_config_command,
  938. .mode = COMMAND_EXEC,
  939. .help = "access J-Link configuration, "
  940. "if no argument this will dump the config",
  941. .chain = jlink_config_subcommand_handlers,
  942. },
  943. {
  944. .name = "pid",
  945. .handler = &jlink_pid_command,
  946. .mode = COMMAND_CONFIG,
  947. .help = "set the pid of the interface we want to use",
  948. },
  949. COMMAND_REGISTRATION_DONE
  950. };
  951. static const struct command_registration jlink_command_handlers[] = {
  952. {
  953. .name = "jlink",
  954. .mode = COMMAND_ANY,
  955. .help = "perform jlink management",
  956. .chain = jlink_subcommand_handlers,
  957. },
  958. COMMAND_REGISTRATION_DONE
  959. };
  960. struct jtag_interface jlink_interface = {
  961. .name = "jlink",
  962. .commands = jlink_command_handlers,
  963. .execute_queue = jlink_execute_queue,
  964. .speed = jlink_speed,
  965. .speed_div = jlink_speed_div,
  966. .khz = jlink_khz,
  967. .init = jlink_init,
  968. .quit = jlink_quit,
  969. };
  970. /***************************************************************************/
  971. /* J-Link tap functions */
  972. static unsigned tap_length;
  973. static uint8_t tms_buffer[JLINK_TAP_BUFFER_SIZE];
  974. static uint8_t tdi_buffer[JLINK_TAP_BUFFER_SIZE];
  975. static uint8_t tdo_buffer[JLINK_TAP_BUFFER_SIZE];
  976. struct pending_scan_result {
  977. int first; /* First bit position in tdo_buffer to read */
  978. int length; /* Number of bits to read */
  979. struct scan_command *command; /* Corresponding scan command */
  980. uint8_t *buffer;
  981. };
  982. #define MAX_PENDING_SCAN_RESULTS 256
  983. static int pending_scan_results_length;
  984. static struct pending_scan_result pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
  985. static void jlink_tap_init(void)
  986. {
  987. tap_length = 0;
  988. pending_scan_results_length = 0;
  989. }
  990. static void jlink_tap_ensure_space(int scans, int bits)
  991. {
  992. int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
  993. int available_bits = JLINK_TAP_BUFFER_SIZE * 8 - tap_length - 32;
  994. if (scans > available_scans || bits > available_bits)
  995. jlink_tap_execute();
  996. }
  997. static void jlink_tap_append_step(int tms, int tdi)
  998. {
  999. int index_var = tap_length / 8;
  1000. if (index_var >= JLINK_TAP_BUFFER_SIZE) {
  1001. LOG_ERROR("jlink_tap_append_step: overflow");
  1002. *(uint32_t *)0xFFFFFFFF = 0;
  1003. exit(-1);
  1004. }
  1005. int bit_index = tap_length % 8;
  1006. uint8_t bit = 1 << bit_index;
  1007. /* we do not pad TMS, so be sure to initialize all bits */
  1008. if (0 == bit_index)
  1009. tms_buffer[index_var] = tdi_buffer[index_var] = 0;
  1010. if (tms)
  1011. tms_buffer[index_var] |= bit;
  1012. else
  1013. tms_buffer[index_var] &= ~bit;
  1014. if (tdi)
  1015. tdi_buffer[index_var] |= bit;
  1016. else
  1017. tdi_buffer[index_var] &= ~bit;
  1018. tap_length++;
  1019. }
  1020. static void jlink_tap_append_scan(int length, uint8_t *buffer,
  1021. struct scan_command *command)
  1022. {
  1023. struct pending_scan_result *pending_scan_result =
  1024. &pending_scan_results_buffer[pending_scan_results_length];
  1025. int i;
  1026. pending_scan_result->first = tap_length;
  1027. pending_scan_result->length = length;
  1028. pending_scan_result->command = command;
  1029. pending_scan_result->buffer = buffer;
  1030. for (i = 0; i < length; i++) {
  1031. int tms = (i < (length - 1)) ? 0 : 1;
  1032. int tdi = (buffer[i / 8] & (1 << (i % 8))) != 0;
  1033. jlink_tap_append_step(tms, tdi);
  1034. }
  1035. pending_scan_results_length++;
  1036. }
  1037. /* Pad and send a tap sequence to the device, and receive the answer.
  1038. * For the purpose of padding we assume that we are in idle or pause state. */
  1039. static int jlink_tap_execute(void)
  1040. {
  1041. int byte_length;
  1042. int i;
  1043. int result;
  1044. if (!tap_length)
  1045. return ERROR_OK;
  1046. /* JLink returns an extra NULL in packet when size of incoming
  1047. * message is a multiple of 64, creates problems with USB comms.
  1048. * WARNING: This will interfere with tap state counting. */
  1049. while ((DIV_ROUND_UP(tap_length, 8) % 64) == 0)
  1050. jlink_tap_append_step((tap_get_state() == TAP_RESET) ? 1 : 0, 0);
  1051. /* number of full bytes (plus one if some would be left over) */
  1052. byte_length = DIV_ROUND_UP(tap_length, 8);
  1053. bool use_jtag3 = jlink_hw_jtag_version >= 3;
  1054. usb_out_buffer[0] = use_jtag3 ? EMU_CMD_HW_JTAG3 : EMU_CMD_HW_JTAG2;
  1055. usb_out_buffer[1] = 0;
  1056. usb_out_buffer[2] = (tap_length >> 0) & 0xff;
  1057. usb_out_buffer[3] = (tap_length >> 8) & 0xff;
  1058. memcpy(usb_out_buffer + 4, tms_buffer, byte_length);
  1059. memcpy(usb_out_buffer + 4 + byte_length, tdi_buffer, byte_length);
  1060. jlink_last_state = jtag_debug_state_machine(tms_buffer, tdi_buffer,
  1061. tap_length, jlink_last_state);
  1062. result = jlink_usb_message(jlink_handle, 4 + 2 * byte_length, byte_length);
  1063. if (result != byte_length) {
  1064. LOG_ERROR("jlink_tap_execute, wrong result %d (expected %d)",
  1065. result, byte_length);
  1066. jlink_tap_init();
  1067. return ERROR_JTAG_QUEUE_FAILED;
  1068. }
  1069. memcpy(tdo_buffer, usb_in_buffer, byte_length);
  1070. for (i = 0; i < pending_scan_results_length; i++) {
  1071. struct pending_scan_result *pending_scan_result = &pending_scan_results_buffer[i];
  1072. uint8_t *buffer = pending_scan_result->buffer;
  1073. int length = pending_scan_result->length;
  1074. int first = pending_scan_result->first;
  1075. struct scan_command *command = pending_scan_result->command;
  1076. /* Copy to buffer */
  1077. buf_set_buf(tdo_buffer, first, buffer, 0, length);
  1078. DEBUG_JTAG_IO("pending scan result, length = %d", length);
  1079. jlink_debug_buffer(buffer, DIV_ROUND_UP(length, 8));
  1080. if (jtag_read_buffer(buffer, command) != ERROR_OK) {
  1081. jlink_tap_init();
  1082. return ERROR_JTAG_QUEUE_FAILED;
  1083. }
  1084. if (pending_scan_result->buffer != NULL)
  1085. free(pending_scan_result->buffer);
  1086. }
  1087. jlink_tap_init();
  1088. return ERROR_OK;
  1089. }
  1090. /*****************************************************************************/
  1091. /* JLink USB low-level functions */
  1092. static struct jlink *jlink_usb_open()
  1093. {
  1094. struct jtag_libusb_device_handle *devh;
  1095. if (jtag_libusb_open(vids, pids, &devh) != ERROR_OK)
  1096. return NULL;
  1097. /* BE ***VERY CAREFUL*** ABOUT MAKING CHANGES IN THIS
  1098. * AREA!!!!!!!!!!! The behavior of libusb is not completely
  1099. * consistent across Windows, Linux, and Mac OS X platforms.
  1100. * The actions taken in the following compiler conditionals may
  1101. * not agree with published documentation for libusb, but were
  1102. * found to be necessary through trials and tribulations. Even
  1103. * little tweaks can break one or more platforms, so if you do
  1104. * make changes test them carefully on all platforms before
  1105. * committing them!
  1106. */
  1107. #if IS_WIN32 == 0
  1108. jtag_libusb_reset_device(devh);
  1109. #if IS_DARWIN == 0
  1110. int timeout = 5;
  1111. /* reopen jlink after usb_reset
  1112. * on win32 this may take a second or two to re-enumerate */
  1113. int retval;
  1114. while ((retval = jtag_libusb_open(vids, pids, &devh)) != ERROR_OK) {
  1115. usleep(1000);
  1116. timeout--;
  1117. if (!timeout)
  1118. break;
  1119. }
  1120. if (ERROR_OK != retval)
  1121. return NULL;
  1122. #endif
  1123. #endif
  1124. /* usb_set_configuration required under win32 */
  1125. struct jtag_libusb_device *udev = jtag_libusb_get_device(devh);
  1126. jtag_libusb_set_configuration(devh, 0);
  1127. jtag_libusb_claim_interface(devh, 0);
  1128. #if 0
  1129. /*
  1130. * This makes problems under Mac OS X. And is not needed
  1131. * under Windows. Hopefully this will not break a linux build
  1132. */
  1133. usb_set_altinterface(result->usb_handle, 0);
  1134. #endif
  1135. jtag_libusb_get_endpoints(udev, &jlink_read_ep, &jlink_write_ep);
  1136. struct jlink *result = malloc(sizeof(struct jlink));
  1137. result->usb_handle = devh;
  1138. return result;
  1139. }
  1140. static void jlink_usb_close(struct jlink *jlink)
  1141. {
  1142. jtag_libusb_close(jlink->usb_handle);
  1143. free(jlink);
  1144. }
  1145. /* Send a message and receive the reply. */
  1146. static int jlink_usb_message(struct jlink *jlink, int out_length, int in_length)
  1147. {
  1148. int result;
  1149. result = jlink_usb_write(jlink, out_length);
  1150. if (result != out_length) {
  1151. LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)",
  1152. out_length, result);
  1153. return ERROR_JTAG_DEVICE_ERROR;
  1154. }
  1155. result = jlink_usb_read(jlink, in_length);
  1156. if ((result != in_length) && (result != (in_length + 1))) {
  1157. LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)",
  1158. in_length, result);
  1159. return ERROR_JTAG_DEVICE_ERROR;
  1160. }
  1161. if (jlink_hw_jtag_version < 3)
  1162. return result;
  1163. int result2 = ERROR_OK;
  1164. if (result == in_length) {
  1165. /* Must read the result from the EMU too */
  1166. result2 = jlink_usb_read_emu_result(jlink);
  1167. if (1 != result2) {
  1168. LOG_ERROR("jlink_usb_read_emu_result retried requested = 1, "
  1169. "result=%d, in_length=%i", result2, in_length);
  1170. /* Try again once, should only happen if (in_length%64 == 0) */
  1171. result2 = jlink_usb_read_emu_result(jlink);
  1172. if (1 != result2) {
  1173. LOG_ERROR("jlink_usb_read_emu_result failed "
  1174. "(requested = 1, result=%d)", result2);
  1175. return ERROR_JTAG_DEVICE_ERROR;
  1176. }
  1177. }
  1178. /* Check the result itself */
  1179. result2 = usb_emu_result_buffer[0];
  1180. } else {
  1181. /* Save the result, then remove it from return value */
  1182. result2 = usb_in_buffer[result--];
  1183. }
  1184. if (result2) {
  1185. LOG_ERROR("jlink_usb_message failed with result=%d)", result2);
  1186. return ERROR_JTAG_DEVICE_ERROR;
  1187. }
  1188. return result;
  1189. }
  1190. /* calls the given usb_bulk_* function, allowing for the data to
  1191. * trickle in with some timeouts */
  1192. static int usb_bulk_with_retries(
  1193. int (*f)(jtag_libusb_device_handle *, int, char *, int, int),
  1194. jtag_libusb_device_handle *dev, int ep,
  1195. char *bytes, int size, int timeout)
  1196. {
  1197. int tries = 3, count = 0;
  1198. while (tries && (count < size)) {
  1199. int result = f(dev, ep, bytes + count, size - count, timeout);
  1200. if (result > 0)
  1201. count += result;
  1202. else if ((-ETIMEDOUT != result) || !--tries)
  1203. return result;
  1204. }
  1205. return count;
  1206. }
  1207. static int wrap_usb_bulk_write(jtag_libusb_device_handle *dev, int ep,
  1208. char *buff, int size, int timeout)
  1209. {
  1210. /* usb_bulk_write() takes const char *buff */
  1211. return jtag_libusb_bulk_write(dev, ep, buff, size, timeout);
  1212. }
  1213. static inline int usb_bulk_write_ex(jtag_libusb_device_handle *dev, int ep,
  1214. char *bytes, int size, int timeout)
  1215. {
  1216. return usb_bulk_with_retries(&wrap_usb_bulk_write,
  1217. dev, ep, bytes, size, timeout);
  1218. }
  1219. static inline int usb_bulk_read_ex(jtag_libusb_device_handle *dev, int ep,
  1220. char *bytes, int size, int timeout)
  1221. {
  1222. return usb_bulk_with_retries(&jtag_libusb_bulk_read,
  1223. dev, ep, bytes, size, timeout);
  1224. }
  1225. /* Write data from out_buffer to USB. */
  1226. static int jlink_usb_write(struct jlink *jlink, int out_length)
  1227. {
  1228. int result;
  1229. if (out_length > JLINK_OUT_BUFFER_SIZE) {
  1230. LOG_ERROR("jlink_write illegal out_length=%d (max=%d)",
  1231. out_length, JLINK_OUT_BUFFER_SIZE);
  1232. return -1;
  1233. }
  1234. result = usb_bulk_write_ex(jlink->usb_handle, jlink_write_ep,
  1235. (char *)usb_out_buffer, out_length, JLINK_USB_TIMEOUT);
  1236. DEBUG_JTAG_IO("jlink_usb_write, out_length = %d, result = %d",
  1237. out_length, result);
  1238. jlink_debug_buffer(usb_out_buffer, out_length);
  1239. return result;
  1240. }
  1241. /* Read data from USB into in_buffer. */
  1242. static int jlink_usb_read(struct jlink *jlink, int expected_size)
  1243. {
  1244. int result = usb_bulk_read_ex(jlink->usb_handle, jlink_read_ep,
  1245. (char *)usb_in_buffer, expected_size, JLINK_USB_TIMEOUT);
  1246. DEBUG_JTAG_IO("jlink_usb_read, result = %d", result);
  1247. jlink_debug_buffer(usb_in_buffer, result);
  1248. return result;
  1249. }
  1250. /* Read the result from the previous EMU cmd into result_buffer. */
  1251. static int jlink_usb_read_emu_result(struct jlink *jlink)
  1252. {
  1253. int result = usb_bulk_read_ex(jlink->usb_handle, jlink_read_ep,
  1254. (char *)usb_emu_result_buffer, 1 /* JLINK_EMU_RESULT_BUFFER_SIZE */,
  1255. JLINK_USB_TIMEOUT);
  1256. DEBUG_JTAG_IO("jlink_usb_read_result, result = %d", result);
  1257. jlink_debug_buffer(usb_emu_result_buffer, result);
  1258. return result;
  1259. }
  1260. #ifdef _DEBUG_USB_COMMS_
  1261. #define BYTES_PER_LINE 16
  1262. static void jlink_debug_buffer(uint8_t *buffer, int length)
  1263. {
  1264. char line[81];
  1265. char s[4];
  1266. int i;
  1267. int j;
  1268. for (i = 0; i < length; i += BYTES_PER_LINE) {
  1269. snprintf(line, 5, "%04x", i);
  1270. for (j = i; j < i + BYTES_PER_LINE && j < length; j++) {
  1271. snprintf(s, 4, " %02x", buffer[j]);
  1272. strcat(line, s);
  1273. }
  1274. LOG_DEBUG("%s", line);
  1275. }
  1276. }
  1277. #endif