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.
 
 
 
 
 
 

2037 lines
50 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. * Copyright (C) 2015 by Marc Schink *
  12. * openocd-dev@marcschink.de *
  13. * *
  14. * Copyright (C) 2015 by Paul Fertser *
  15. * fercerpav@gmail.com *
  16. * *
  17. * This program is free software; you can redistribute it and/or modify *
  18. * it under the terms of the GNU General Public License as published by *
  19. * the Free Software Foundation; either version 2 of the License, or *
  20. * (at your option) any later version. *
  21. * *
  22. * This program is distributed in the hope that it will be useful, *
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  25. * GNU General Public License for more details. *
  26. * *
  27. * You should have received a copy of the GNU General Public License *
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  29. ***************************************************************************/
  30. #ifdef HAVE_CONFIG_H
  31. #include "config.h"
  32. #endif
  33. #include <stdint.h>
  34. #include <math.h>
  35. #include <jtag/interface.h>
  36. #include <jtag/swd.h>
  37. #include <jtag/commands.h>
  38. #include <libjaylink/libjaylink.h>
  39. static struct jaylink_context *jayctx;
  40. static struct jaylink_device_handle *devh;
  41. static struct jaylink_connection conn;
  42. static struct jaylink_connection connlist[JAYLINK_MAX_CONNECTIONS];
  43. static enum jaylink_jtag_version jtag_command_version;
  44. static uint8_t caps[JAYLINK_DEV_EXT_CAPS_SIZE];
  45. static uint32_t serial_number;
  46. static bool use_serial_number;
  47. static enum jaylink_usb_address usb_address;
  48. static bool use_usb_address;
  49. static enum jaylink_target_interface iface = JAYLINK_TIF_JTAG;
  50. static bool trace_enabled;
  51. #define JLINK_MAX_SPEED 12000
  52. #define JLINK_TAP_BUFFER_SIZE 2048
  53. static unsigned int swd_buffer_size = JLINK_TAP_BUFFER_SIZE;
  54. /* 256 byte non-volatile memory */
  55. struct device_config {
  56. uint8_t usb_address;
  57. /* 0ffset 0x01 to 0x03 */
  58. uint8_t reserved_1[3];
  59. uint32_t target_power;
  60. /* 0ffset 0x08 to 0x1f */
  61. uint8_t reserved_2[24];
  62. /* IP only for J-Link Pro */
  63. uint8_t ip_address[4];
  64. uint8_t subnet_mask[4];
  65. /* 0ffset 0x28 to 0x2f */
  66. uint8_t reserved_3[8];
  67. uint8_t mac_address[6];
  68. /* 0ffset 0x36 to 0xff */
  69. uint8_t reserved_4[202];
  70. } __attribute__ ((packed));
  71. static struct device_config config;
  72. static struct device_config tmp_config;
  73. /* Queue command functions */
  74. static void jlink_end_state(tap_state_t state);
  75. static void jlink_state_move(void);
  76. static void jlink_path_move(int num_states, tap_state_t *path);
  77. static void jlink_stableclocks(int num_cycles);
  78. static void jlink_runtest(int num_cycles);
  79. static void jlink_reset(int trst, int srst);
  80. static int jlink_swd_run_queue(void);
  81. static void jlink_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data, uint32_t ap_delay_clk);
  82. static int jlink_swd_switch_seq(enum swd_special_seq seq);
  83. /* J-Link tap buffer functions */
  84. static void jlink_tap_init(void);
  85. static int jlink_flush(void);
  86. /**
  87. * Queue data to go out and in, flushing the queue as many times as
  88. * necessary.
  89. *
  90. * @param out A pointer to TDI data, if NULL, old stale data will be used.
  91. * @param out_offset A bit offset for TDI data.
  92. * @param tms_out A pointer to TMS data, if NULL, zeroes will be emitted.
  93. * @param tms_offset A bit offset for TMS data.
  94. * @param in A pointer to store TDO data to, if NULL the data will be discarded.
  95. * @param in_offset A bit offset for TDO data.
  96. * @param length Amount of bits to transfer out and in.
  97. *
  98. * @retval This function doesn't return any value.
  99. */
  100. static void jlink_clock_data(const uint8_t *out, unsigned out_offset,
  101. const uint8_t *tms_out, unsigned tms_offset,
  102. uint8_t *in, unsigned in_offset,
  103. unsigned length);
  104. static enum tap_state jlink_last_state = TAP_RESET;
  105. static int queued_retval;
  106. /***************************************************************************/
  107. /* External interface implementation */
  108. static void jlink_execute_stableclocks(struct jtag_command *cmd)
  109. {
  110. DEBUG_JTAG_IO("stableclocks %i cycles", cmd->cmd.runtest->num_cycles);
  111. jlink_stableclocks(cmd->cmd.runtest->num_cycles);
  112. }
  113. static void jlink_execute_runtest(struct jtag_command *cmd)
  114. {
  115. DEBUG_JTAG_IO("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles,
  116. cmd->cmd.runtest->end_state);
  117. jlink_end_state(cmd->cmd.runtest->end_state);
  118. jlink_runtest(cmd->cmd.runtest->num_cycles);
  119. }
  120. static void jlink_execute_statemove(struct jtag_command *cmd)
  121. {
  122. DEBUG_JTAG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
  123. jlink_end_state(cmd->cmd.statemove->end_state);
  124. jlink_state_move();
  125. }
  126. static void jlink_execute_pathmove(struct jtag_command *cmd)
  127. {
  128. DEBUG_JTAG_IO("pathmove: %i states, end in %i",
  129. cmd->cmd.pathmove->num_states,
  130. cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
  131. jlink_path_move(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path);
  132. }
  133. static void jlink_execute_scan(struct jtag_command *cmd)
  134. {
  135. DEBUG_JTAG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN",
  136. jtag_scan_type(cmd->cmd.scan));
  137. /* Make sure there are no trailing fields with num_bits == 0, or the logic below will fail. */
  138. while (cmd->cmd.scan->num_fields > 0
  139. && cmd->cmd.scan->fields[cmd->cmd.scan->num_fields - 1].num_bits == 0) {
  140. cmd->cmd.scan->num_fields--;
  141. LOG_DEBUG("discarding trailing empty field");
  142. }
  143. if (cmd->cmd.scan->num_fields == 0) {
  144. LOG_DEBUG("empty scan, doing nothing");
  145. return;
  146. }
  147. if (cmd->cmd.scan->ir_scan) {
  148. if (tap_get_state() != TAP_IRSHIFT) {
  149. jlink_end_state(TAP_IRSHIFT);
  150. jlink_state_move();
  151. }
  152. } else {
  153. if (tap_get_state() != TAP_DRSHIFT) {
  154. jlink_end_state(TAP_DRSHIFT);
  155. jlink_state_move();
  156. }
  157. }
  158. jlink_end_state(cmd->cmd.scan->end_state);
  159. struct scan_field *field = cmd->cmd.scan->fields;
  160. unsigned scan_size = 0;
  161. for (int i = 0; i < cmd->cmd.scan->num_fields; i++, field++) {
  162. scan_size += field->num_bits;
  163. DEBUG_JTAG_IO("%s%s field %d/%d %d bits",
  164. field->in_value ? "in" : "",
  165. field->out_value ? "out" : "",
  166. i,
  167. cmd->cmd.scan->num_fields,
  168. field->num_bits);
  169. if (i == cmd->cmd.scan->num_fields - 1 && tap_get_state() != tap_get_end_state()) {
  170. /* Last field, and we're leaving IRSHIFT/DRSHIFT. Clock last bit during tap
  171. * movement. This last field can't have length zero, it was checked above. */
  172. jlink_clock_data(field->out_value,
  173. 0,
  174. NULL,
  175. 0,
  176. field->in_value,
  177. 0,
  178. field->num_bits - 1);
  179. uint8_t last_bit = 0;
  180. if (field->out_value)
  181. bit_copy(&last_bit, 0, field->out_value, field->num_bits - 1, 1);
  182. uint8_t tms_bits = 0x01;
  183. jlink_clock_data(&last_bit,
  184. 0,
  185. &tms_bits,
  186. 0,
  187. field->in_value,
  188. field->num_bits - 1,
  189. 1);
  190. tap_set_state(tap_state_transition(tap_get_state(), 1));
  191. jlink_clock_data(NULL,
  192. 0,
  193. &tms_bits,
  194. 1,
  195. NULL,
  196. 0,
  197. 1);
  198. tap_set_state(tap_state_transition(tap_get_state(), 0));
  199. } else
  200. jlink_clock_data(field->out_value,
  201. 0,
  202. NULL,
  203. 0,
  204. field->in_value,
  205. 0,
  206. field->num_bits);
  207. }
  208. if (tap_get_state() != tap_get_end_state()) {
  209. jlink_end_state(tap_get_end_state());
  210. jlink_state_move();
  211. }
  212. DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
  213. (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
  214. tap_state_name(tap_get_end_state()));
  215. }
  216. static void jlink_execute_reset(struct jtag_command *cmd)
  217. {
  218. DEBUG_JTAG_IO("reset trst: %i srst %i", cmd->cmd.reset->trst,
  219. cmd->cmd.reset->srst);
  220. jlink_flush();
  221. jlink_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  222. jlink_flush();
  223. }
  224. static void jlink_execute_sleep(struct jtag_command *cmd)
  225. {
  226. DEBUG_JTAG_IO("sleep %" PRIi32 "", cmd->cmd.sleep->us);
  227. jlink_flush();
  228. jtag_sleep(cmd->cmd.sleep->us);
  229. }
  230. static int jlink_execute_command(struct jtag_command *cmd)
  231. {
  232. switch (cmd->type) {
  233. case JTAG_STABLECLOCKS:
  234. jlink_execute_stableclocks(cmd);
  235. break;
  236. case JTAG_RUNTEST:
  237. jlink_execute_runtest(cmd);
  238. break;
  239. case JTAG_TLR_RESET:
  240. jlink_execute_statemove(cmd);
  241. break;
  242. case JTAG_PATHMOVE:
  243. jlink_execute_pathmove(cmd);
  244. break;
  245. case JTAG_SCAN:
  246. jlink_execute_scan(cmd);
  247. break;
  248. case JTAG_RESET:
  249. jlink_execute_reset(cmd);
  250. break;
  251. case JTAG_SLEEP:
  252. jlink_execute_sleep(cmd);
  253. break;
  254. default:
  255. LOG_ERROR("BUG: Unknown JTAG command type encountered.");
  256. return ERROR_JTAG_QUEUE_FAILED;
  257. }
  258. return ERROR_OK;
  259. }
  260. static int jlink_execute_queue(void)
  261. {
  262. int ret;
  263. struct jtag_command *cmd = jtag_command_queue;
  264. while (cmd != NULL) {
  265. ret = jlink_execute_command(cmd);
  266. if (ret != ERROR_OK)
  267. return ret;
  268. cmd = cmd->next;
  269. }
  270. return jlink_flush();
  271. }
  272. static int jlink_speed(int speed)
  273. {
  274. int ret;
  275. struct jaylink_speed tmp;
  276. int max_speed;
  277. if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_SPEEDS)) {
  278. ret = jaylink_get_speeds(devh, &tmp);
  279. if (ret != JAYLINK_OK) {
  280. LOG_ERROR("jaylink_get_speeds() failed: %s.",
  281. jaylink_strerror_name(ret));
  282. return ERROR_JTAG_DEVICE_ERROR;
  283. }
  284. tmp.freq /= 1000;
  285. max_speed = tmp.freq / tmp.div;
  286. } else {
  287. max_speed = JLINK_MAX_SPEED;
  288. }
  289. if (!speed) {
  290. if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_ADAPTIVE_CLOCKING)) {
  291. LOG_ERROR("Adaptive clocking is not supported by the device.");
  292. return ERROR_JTAG_NOT_IMPLEMENTED;
  293. }
  294. speed = JAYLINK_SPEED_ADAPTIVE_CLOCKING;
  295. } else if (speed > max_speed) {
  296. LOG_INFO("Reduced speed from %d kHz to %d kHz (maximum).", speed,
  297. max_speed);
  298. speed = max_speed;
  299. }
  300. ret = jaylink_set_speed(devh, speed);
  301. if (ret != JAYLINK_OK) {
  302. LOG_ERROR("jaylink_set_speed() failed: %s.",
  303. jaylink_strerror_name(ret));
  304. return ERROR_JTAG_DEVICE_ERROR;
  305. }
  306. return ERROR_OK;
  307. }
  308. static int jlink_speed_div(int speed, int *khz)
  309. {
  310. *khz = speed;
  311. return ERROR_OK;
  312. }
  313. static int jlink_khz(int khz, int *jtag_speed)
  314. {
  315. *jtag_speed = khz;
  316. return ERROR_OK;
  317. }
  318. static bool read_device_config(struct device_config *cfg)
  319. {
  320. int ret;
  321. ret = jaylink_read_raw_config(devh, (uint8_t *)cfg);
  322. if (ret != JAYLINK_OK) {
  323. LOG_ERROR("jaylink_read_raw_config() failed: %s.",
  324. jaylink_strerror_name(ret));
  325. return false;
  326. }
  327. if (cfg->usb_address == 0xff)
  328. cfg->usb_address = 0x00;
  329. if (cfg->target_power == 0xffffffff)
  330. cfg->target_power = 0;
  331. return true;
  332. }
  333. static int select_interface(void)
  334. {
  335. int ret;
  336. uint32_t interfaces;
  337. if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_SELECT_TIF)) {
  338. if (iface != JAYLINK_TIF_JTAG) {
  339. LOG_ERROR("Device supports JTAG transport only.");
  340. return ERROR_JTAG_INIT_FAILED;
  341. }
  342. return ERROR_OK;
  343. }
  344. ret = jaylink_get_available_interfaces(devh, &interfaces);
  345. if (ret != JAYLINK_OK) {
  346. LOG_ERROR("jaylink_get_available_interfaces() failed: %s.",
  347. jaylink_strerror_name(ret));
  348. return ERROR_JTAG_INIT_FAILED;
  349. }
  350. if (!(interfaces & (1 << iface))) {
  351. LOG_ERROR("Selected transport is not supported by the device.");
  352. return ERROR_JTAG_INIT_FAILED;
  353. }
  354. ret = jaylink_select_interface(devh, iface, NULL);
  355. if (ret < 0) {
  356. LOG_ERROR("jaylink_select_interface() failed: %s.",
  357. jaylink_strerror_name(ret));
  358. return ERROR_JTAG_INIT_FAILED;
  359. }
  360. return ERROR_OK;
  361. }
  362. static int jlink_register(void)
  363. {
  364. int ret;
  365. size_t i;
  366. bool handle_found;
  367. size_t count;
  368. if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_REGISTER))
  369. return ERROR_OK;
  370. ret = jaylink_register(devh, &conn, connlist, &count);
  371. if (ret != JAYLINK_OK) {
  372. LOG_ERROR("jaylink_register() failed: %s.",
  373. jaylink_strerror_name(ret));
  374. return ERROR_FAIL;
  375. }
  376. handle_found = false;
  377. for (i = 0; i < count; i++) {
  378. if (connlist[i].handle == conn.handle) {
  379. handle_found = true;
  380. break;
  381. }
  382. }
  383. if (!handle_found) {
  384. LOG_ERROR("Registration failed: maximum number of connections on the "
  385. "device reached.");
  386. return ERROR_FAIL;
  387. }
  388. return ERROR_OK;
  389. }
  390. /*
  391. * Adjust the SWD transaction buffer size depending on the free device internal
  392. * memory. This ensures that the SWD transactions sent to the device do not
  393. * exceed the internal memory of the device.
  394. */
  395. static bool adjust_swd_buffer_size(void)
  396. {
  397. int ret;
  398. uint32_t tmp;
  399. if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_FREE_MEMORY))
  400. return true;
  401. ret = jaylink_get_free_memory(devh, &tmp);
  402. if (ret != JAYLINK_OK) {
  403. LOG_ERROR("jaylink_get_free_memory() failed: %s.",
  404. jaylink_strerror_name(ret));
  405. return false;
  406. }
  407. if (tmp < 143) {
  408. LOG_ERROR("Not enough free device internal memory: %u bytes.", tmp);
  409. return false;
  410. }
  411. tmp = MIN(JLINK_TAP_BUFFER_SIZE, (tmp - 16) / 2);
  412. if (tmp != swd_buffer_size) {
  413. swd_buffer_size = tmp;
  414. LOG_DEBUG("Adjusted SWD transaction buffer size to %u bytes.",
  415. swd_buffer_size);
  416. }
  417. return true;
  418. }
  419. static int jaylink_log_handler(const struct jaylink_context *ctx,
  420. enum jaylink_log_level level, const char *format, va_list args,
  421. void *user_data)
  422. {
  423. enum log_levels tmp;
  424. switch (level) {
  425. case JAYLINK_LOG_LEVEL_ERROR:
  426. tmp = LOG_LVL_ERROR;
  427. break;
  428. case JAYLINK_LOG_LEVEL_WARNING:
  429. tmp = LOG_LVL_WARNING;
  430. break;
  431. /*
  432. * Forward info messages to the debug output because they are more verbose
  433. * than info messages of OpenOCD.
  434. */
  435. case JAYLINK_LOG_LEVEL_INFO:
  436. case JAYLINK_LOG_LEVEL_DEBUG:
  437. tmp = LOG_LVL_DEBUG;
  438. break;
  439. default:
  440. tmp = LOG_LVL_WARNING;
  441. }
  442. log_vprintf_lf(tmp, __FILE__, __LINE__, __func__, format, args);
  443. return 0;
  444. }
  445. static int jlink_init(void)
  446. {
  447. int ret;
  448. struct jaylink_device **devs;
  449. unsigned int i;
  450. bool found_device;
  451. uint32_t tmp;
  452. char *firmware_version;
  453. struct jaylink_hardware_version hwver;
  454. struct jaylink_hardware_status hwstatus;
  455. enum jaylink_usb_address address;
  456. size_t length;
  457. ret = jaylink_init(&jayctx);
  458. if (ret != JAYLINK_OK) {
  459. LOG_ERROR("jaylink_init() failed: %s.",
  460. jaylink_strerror_name(ret));
  461. return ERROR_JTAG_INIT_FAILED;
  462. }
  463. ret = jaylink_log_set_callback(jayctx, &jaylink_log_handler, NULL);
  464. if (ret != JAYLINK_OK) {
  465. LOG_ERROR("jaylink_log_set_callback() failed: %s.",
  466. jaylink_strerror_name(ret));
  467. jaylink_exit(jayctx);
  468. return ERROR_JTAG_INIT_FAILED;
  469. }
  470. ret = jaylink_discovery_scan(jayctx, 0);
  471. if (ret != JAYLINK_OK) {
  472. LOG_ERROR("jaylink_discovery_scan() failed: %s.",
  473. jaylink_strerror_name(ret));
  474. jaylink_exit(jayctx);
  475. return ERROR_JTAG_INIT_FAILED;
  476. }
  477. ret = jaylink_get_devices(jayctx, &devs, NULL);
  478. if (ret != JAYLINK_OK) {
  479. LOG_ERROR("jaylink_get_devices() failed: %s.",
  480. jaylink_strerror_name(ret));
  481. jaylink_exit(jayctx);
  482. return ERROR_JTAG_INIT_FAILED;
  483. }
  484. found_device = false;
  485. if (!use_serial_number && !use_usb_address)
  486. LOG_INFO("No device selected, using first device.");
  487. for (i = 0; devs[i]; i++) {
  488. if (use_serial_number) {
  489. ret = jaylink_device_get_serial_number(devs[i], &tmp);
  490. if (ret == JAYLINK_ERR_NOT_AVAILABLE) {
  491. continue;
  492. } else if (ret != JAYLINK_OK) {
  493. LOG_WARNING("jaylink_device_get_serial_number() failed: %s.",
  494. jaylink_strerror_name(ret));
  495. continue;
  496. }
  497. if (serial_number != tmp)
  498. continue;
  499. }
  500. if (use_usb_address) {
  501. ret = jaylink_device_get_usb_address(devs[i], &address);
  502. if (ret != JAYLINK_OK) {
  503. LOG_WARNING("jaylink_device_get_usb_address() failed: %s.",
  504. jaylink_strerror_name(ret));
  505. continue;
  506. }
  507. if (usb_address != address)
  508. continue;
  509. }
  510. ret = jaylink_open(devs[i], &devh);
  511. if (ret == JAYLINK_OK) {
  512. found_device = true;
  513. break;
  514. }
  515. LOG_ERROR("Failed to open device: %s.", jaylink_strerror_name(ret));
  516. }
  517. jaylink_free_devices(devs, true);
  518. if (!found_device) {
  519. LOG_ERROR("No J-Link device found.");
  520. jaylink_exit(jayctx);
  521. return ERROR_JTAG_INIT_FAILED;
  522. }
  523. /*
  524. * Be careful with changing the following initialization sequence because
  525. * some devices are known to be sensitive regarding the order.
  526. */
  527. ret = jaylink_get_firmware_version(devh, &firmware_version, &length);
  528. if (ret != JAYLINK_OK) {
  529. LOG_ERROR("jaylink_get_firmware_version() failed: %s.",
  530. jaylink_strerror_name(ret));
  531. jaylink_close(devh);
  532. jaylink_exit(jayctx);
  533. return ERROR_JTAG_INIT_FAILED;
  534. } else if (length > 0) {
  535. LOG_INFO("%s", firmware_version);
  536. free(firmware_version);
  537. } else {
  538. LOG_WARNING("Device responds empty firmware version string.");
  539. }
  540. memset(caps, 0, JAYLINK_DEV_EXT_CAPS_SIZE);
  541. ret = jaylink_get_caps(devh, caps);
  542. if (ret != JAYLINK_OK) {
  543. LOG_ERROR("jaylink_get_caps() failed: %s.", jaylink_strerror_name(ret));
  544. jaylink_close(devh);
  545. jaylink_exit(jayctx);
  546. return ERROR_JTAG_INIT_FAILED;
  547. }
  548. if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_EXT_CAPS)) {
  549. ret = jaylink_get_extended_caps(devh, caps);
  550. if (ret != JAYLINK_OK) {
  551. LOG_ERROR("jaylink_get_extended_caps() failed: %s.",
  552. jaylink_strerror_name(ret));
  553. jaylink_close(devh);
  554. jaylink_exit(jayctx);
  555. return ERROR_JTAG_INIT_FAILED;
  556. }
  557. }
  558. jtag_command_version = JAYLINK_JTAG_V2;
  559. if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_HW_VERSION)) {
  560. ret = jaylink_get_hardware_version(devh, &hwver);
  561. if (ret != JAYLINK_OK) {
  562. LOG_ERROR("Failed to retrieve hardware version: %s.",
  563. jaylink_strerror_name(ret));
  564. jaylink_close(devh);
  565. jaylink_exit(jayctx);
  566. return ERROR_JTAG_INIT_FAILED;
  567. }
  568. LOG_INFO("Hardware version: %u.%02u", hwver.major, hwver.minor);
  569. if (hwver.major >= 5)
  570. jtag_command_version = JAYLINK_JTAG_V3;
  571. }
  572. if (iface == JAYLINK_TIF_SWD) {
  573. /*
  574. * Adjust the SWD transaction buffer size in case there is already
  575. * allocated memory on the device. This happens for example if the
  576. * memory for SWO capturing is still allocated because the software
  577. * which used the device before has not been shut down properly.
  578. */
  579. if (!adjust_swd_buffer_size()) {
  580. jaylink_close(devh);
  581. jaylink_exit(jayctx);
  582. return ERROR_JTAG_INIT_FAILED;
  583. }
  584. }
  585. if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
  586. if (!read_device_config(&config)) {
  587. LOG_ERROR("Failed to read device configuration data.");
  588. jaylink_close(devh);
  589. jaylink_exit(jayctx);
  590. return ERROR_JTAG_INIT_FAILED;
  591. }
  592. memcpy(&tmp_config, &config, sizeof(struct device_config));
  593. }
  594. ret = jaylink_get_hardware_status(devh, &hwstatus);
  595. if (ret != JAYLINK_OK) {
  596. LOG_ERROR("jaylink_get_hardware_status() failed: %s.",
  597. jaylink_strerror_name(ret));
  598. jaylink_close(devh);
  599. jaylink_exit(jayctx);
  600. return ERROR_JTAG_INIT_FAILED;
  601. }
  602. LOG_INFO("VTarget = %u.%03u V", hwstatus.target_voltage / 1000,
  603. hwstatus.target_voltage % 1000);
  604. conn.handle = 0;
  605. conn.pid = 0;
  606. strcpy(conn.hid, "0.0.0.0");
  607. conn.iid = 0;
  608. conn.cid = 0;
  609. ret = jlink_register();
  610. if (ret != ERROR_OK) {
  611. jaylink_close(devh);
  612. jaylink_exit(jayctx);
  613. return ERROR_JTAG_INIT_FAILED;
  614. }
  615. ret = select_interface();
  616. if (ret != ERROR_OK) {
  617. jaylink_close(devh);
  618. jaylink_exit(jayctx);
  619. return ret;
  620. }
  621. jlink_reset(0, 0);
  622. jtag_sleep(3000);
  623. jlink_tap_init();
  624. jlink_speed(jtag_get_speed_khz());
  625. if (iface == JAYLINK_TIF_JTAG) {
  626. /*
  627. * J-Link devices with firmware version v5 and v6 seems to have an issue
  628. * if the first tap move is not divisible by 8, so we send a TLR on
  629. * first power up.
  630. */
  631. uint8_t tms = 0xff;
  632. jlink_clock_data(NULL, 0, &tms, 0, NULL, 0, 8);
  633. jlink_flush();
  634. }
  635. return ERROR_OK;
  636. }
  637. static int jlink_quit(void)
  638. {
  639. int ret;
  640. size_t count;
  641. if (trace_enabled) {
  642. ret = jaylink_swo_stop(devh);
  643. if (ret != JAYLINK_OK)
  644. LOG_ERROR("jaylink_swo_stop() failed: %s.",
  645. jaylink_strerror_name(ret));
  646. }
  647. if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_REGISTER)) {
  648. ret = jaylink_unregister(devh, &conn, connlist, &count);
  649. if (ret != JAYLINK_OK)
  650. LOG_ERROR("jaylink_unregister() failed: %s.",
  651. jaylink_strerror_name(ret));
  652. }
  653. jaylink_close(devh);
  654. jaylink_exit(jayctx);
  655. return ERROR_OK;
  656. }
  657. /***************************************************************************/
  658. /* Queue command implementations */
  659. static void jlink_end_state(tap_state_t state)
  660. {
  661. if (tap_is_state_stable(state))
  662. tap_set_end_state(state);
  663. else {
  664. LOG_ERROR("BUG: %i is not a valid end state", state);
  665. exit(-1);
  666. }
  667. }
  668. /* Goes to the end state. */
  669. static void jlink_state_move(void)
  670. {
  671. uint8_t tms_scan;
  672. uint8_t tms_scan_bits;
  673. tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
  674. tms_scan_bits = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
  675. jlink_clock_data(NULL, 0, &tms_scan, 0, NULL, 0, tms_scan_bits);
  676. tap_set_state(tap_get_end_state());
  677. }
  678. static void jlink_path_move(int num_states, tap_state_t *path)
  679. {
  680. int i;
  681. uint8_t tms = 0xff;
  682. for (i = 0; i < num_states; i++) {
  683. if (path[i] == tap_state_transition(tap_get_state(), false))
  684. jlink_clock_data(NULL, 0, NULL, 0, NULL, 0, 1);
  685. else if (path[i] == tap_state_transition(tap_get_state(), true))
  686. jlink_clock_data(NULL, 0, &tms, 0, NULL, 0, 1);
  687. else {
  688. LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition.",
  689. tap_state_name(tap_get_state()), tap_state_name(path[i]));
  690. exit(-1);
  691. }
  692. tap_set_state(path[i]);
  693. }
  694. tap_set_end_state(tap_get_state());
  695. }
  696. static void jlink_stableclocks(int num_cycles)
  697. {
  698. int i;
  699. uint8_t tms = tap_get_state() == TAP_RESET;
  700. /* Execute num_cycles. */
  701. for (i = 0; i < num_cycles; i++)
  702. jlink_clock_data(NULL, 0, &tms, 0, NULL, 0, 1);
  703. }
  704. static void jlink_runtest(int num_cycles)
  705. {
  706. tap_state_t saved_end_state = tap_get_end_state();
  707. /* Only do a state_move when we're not already in IDLE. */
  708. if (tap_get_state() != TAP_IDLE) {
  709. jlink_end_state(TAP_IDLE);
  710. jlink_state_move();
  711. /* num_cycles--; */
  712. }
  713. jlink_stableclocks(num_cycles);
  714. /* Finish in end_state. */
  715. jlink_end_state(saved_end_state);
  716. if (tap_get_state() != tap_get_end_state())
  717. jlink_state_move();
  718. }
  719. static void jlink_reset(int trst, int srst)
  720. {
  721. LOG_DEBUG("TRST: %i, SRST: %i.", trst, srst);
  722. /* Signals are active low. */
  723. if (srst == 0)
  724. jaylink_set_reset(devh);
  725. if (srst == 1)
  726. jaylink_clear_reset(devh);
  727. if (trst == 1)
  728. jaylink_jtag_clear_trst(devh);
  729. if (trst == 0)
  730. jaylink_jtag_set_trst(devh);
  731. }
  732. COMMAND_HANDLER(jlink_usb_command)
  733. {
  734. int tmp;
  735. if (CMD_ARGC != 1) {
  736. command_print(CMD_CTX, "Need exactly one argument for jlink usb.");
  737. return ERROR_COMMAND_SYNTAX_ERROR;
  738. }
  739. if (sscanf(CMD_ARGV[0], "%i", &tmp) != 1) {
  740. command_print(CMD_CTX, "Invalid USB address: %s.", CMD_ARGV[0]);
  741. return ERROR_FAIL;
  742. }
  743. if (tmp < JAYLINK_USB_ADDRESS_0 || tmp > JAYLINK_USB_ADDRESS_3) {
  744. command_print(CMD_CTX, "Invalid USB address: %s.", CMD_ARGV[0]);
  745. return ERROR_FAIL;
  746. }
  747. usb_address = tmp;
  748. use_serial_number = false;
  749. use_usb_address = true;
  750. return ERROR_OK;
  751. }
  752. COMMAND_HANDLER(jlink_serial_command)
  753. {
  754. int ret;
  755. if (CMD_ARGC != 1) {
  756. command_print(CMD_CTX, "Need exactly one argument for jlink serial.");
  757. return ERROR_COMMAND_SYNTAX_ERROR;
  758. }
  759. ret = jaylink_parse_serial_number(CMD_ARGV[0], &serial_number);
  760. if (ret == JAYLINK_ERR) {
  761. command_print(CMD_CTX, "Invalid serial number: %s.", CMD_ARGV[0]);
  762. return ERROR_FAIL;
  763. } else if (ret != JAYLINK_OK) {
  764. command_print(CMD_CTX, "jaylink_parse_serial_number() failed: %s.",
  765. jaylink_strerror_name(ret));
  766. return ERROR_FAIL;
  767. }
  768. use_serial_number = true;
  769. use_usb_address = false;
  770. return ERROR_OK;
  771. }
  772. COMMAND_HANDLER(jlink_handle_hwstatus_command)
  773. {
  774. int ret;
  775. struct jaylink_hardware_status status;
  776. ret = jaylink_get_hardware_status(devh, &status);
  777. if (ret != JAYLINK_OK) {
  778. command_print(CMD_CTX, "jaylink_get_hardware_status() failed: %s.",
  779. jaylink_strerror_name(ret));
  780. return ERROR_FAIL;
  781. }
  782. command_print(CMD_CTX, "VTarget = %u.%03u V",
  783. status.target_voltage / 1000, status.target_voltage % 1000);
  784. command_print(CMD_CTX, "TCK = %u TDI = %u TDO = %u TMS = %u SRST = %u "
  785. "TRST = %u", status.tck, status.tdi, status.tdo, status.tms,
  786. status.tres, status.trst);
  787. if (status.target_voltage < 1500)
  788. command_print(CMD_CTX, "Target voltage too low. Check target power.");
  789. return ERROR_OK;
  790. }
  791. COMMAND_HANDLER(jlink_handle_free_memory_command)
  792. {
  793. int ret;
  794. uint32_t tmp;
  795. if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_FREE_MEMORY)) {
  796. command_print(CMD_CTX, "Retrieval of free memory is not supported by "
  797. "the device.");
  798. return ERROR_OK;
  799. }
  800. ret = jaylink_get_free_memory(devh, &tmp);
  801. if (ret != JAYLINK_OK) {
  802. command_print(CMD_CTX, "jaylink_get_free_memory() failed: %s.",
  803. jaylink_strerror_name(ret));
  804. return ERROR_FAIL;
  805. }
  806. command_print(CMD_CTX, "Device has %u bytes of free memory.", tmp);
  807. return ERROR_OK;
  808. }
  809. COMMAND_HANDLER(jlink_handle_jlink_jtag_command)
  810. {
  811. int tmp;
  812. int version;
  813. if (!CMD_ARGC) {
  814. switch (jtag_command_version) {
  815. case JAYLINK_JTAG_V2:
  816. version = 2;
  817. break;
  818. case JAYLINK_JTAG_V3:
  819. version = 3;
  820. break;
  821. default:
  822. return ERROR_FAIL;
  823. }
  824. command_print(CMD_CTX, "JTAG command version: %i", version);
  825. } else if (CMD_ARGC == 1) {
  826. if (sscanf(CMD_ARGV[0], "%i", &tmp) != 1) {
  827. command_print(CMD_CTX, "Invalid argument: %s.", CMD_ARGV[0]);
  828. return ERROR_COMMAND_SYNTAX_ERROR;
  829. }
  830. switch (tmp) {
  831. case 2:
  832. jtag_command_version = JAYLINK_JTAG_V2;
  833. break;
  834. case 3:
  835. jtag_command_version = JAYLINK_JTAG_V3;
  836. break;
  837. default:
  838. command_print(CMD_CTX, "Invalid argument: %s.", CMD_ARGV[0]);
  839. return ERROR_COMMAND_SYNTAX_ERROR;
  840. }
  841. } else {
  842. command_print(CMD_CTX, "Need exactly one argument for jlink jtag.");
  843. return ERROR_COMMAND_SYNTAX_ERROR;
  844. }
  845. return ERROR_OK;
  846. }
  847. COMMAND_HANDLER(jlink_handle_target_power_command)
  848. {
  849. int ret;
  850. int enable;
  851. if (CMD_ARGC != 1) {
  852. command_print(CMD_CTX, "Need exactly one argument for jlink "
  853. "targetpower.");
  854. return ERROR_COMMAND_SYNTAX_ERROR;
  855. }
  856. if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_SET_TARGET_POWER)) {
  857. command_print(CMD_CTX, "Target power supply is not supported by the "
  858. "device.");
  859. return ERROR_OK;
  860. }
  861. if (!strcmp(CMD_ARGV[0], "on")) {
  862. enable = true;
  863. } else if (!strcmp(CMD_ARGV[0], "off")) {
  864. enable = false;
  865. } else {
  866. command_print(CMD_CTX, "Invalid argument: %s.", CMD_ARGV[0]);
  867. return ERROR_FAIL;
  868. }
  869. ret = jaylink_set_target_power(devh, enable);
  870. if (ret != JAYLINK_OK) {
  871. command_print(CMD_CTX, "jaylink_set_target_power() failed: %s.",
  872. jaylink_strerror_name(ret));
  873. return ERROR_FAIL;
  874. }
  875. return ERROR_OK;
  876. }
  877. static void show_config_usb_address(struct command_context *ctx)
  878. {
  879. if (config.usb_address != tmp_config.usb_address)
  880. command_print(ctx, "USB address: %u [%u]", config.usb_address,
  881. tmp_config.usb_address);
  882. else
  883. command_print(ctx, "USB address: %u", config.usb_address);
  884. }
  885. static void show_config_ip_address(struct command_context *ctx)
  886. {
  887. if (!memcmp(config.ip_address, tmp_config.ip_address, 4))
  888. command_print(ctx, "IP address: %d.%d.%d.%d",
  889. config.ip_address[3], config.ip_address[2],
  890. config.ip_address[1], config.ip_address[0]);
  891. else
  892. command_print(ctx, "IP address: %d.%d.%d.%d [%d.%d.%d.%d]",
  893. config.ip_address[3], config.ip_address[2],
  894. config.ip_address[1], config.ip_address[0],
  895. tmp_config.ip_address[3], tmp_config.ip_address[2],
  896. tmp_config.ip_address[1], tmp_config.ip_address[0]);
  897. if (!memcmp(config.subnet_mask, tmp_config.subnet_mask, 4))
  898. command_print(ctx, "Subnet mask: %d.%d.%d.%d",
  899. config.subnet_mask[3], config.subnet_mask[2],
  900. config.subnet_mask[1], config.subnet_mask[0]);
  901. else
  902. command_print(ctx, "Subnet mask: %d.%d.%d.%d [%d.%d.%d.%d]",
  903. config.subnet_mask[3], config.subnet_mask[2],
  904. config.subnet_mask[1], config.subnet_mask[0],
  905. tmp_config.subnet_mask[3], tmp_config.subnet_mask[2],
  906. tmp_config.subnet_mask[1], tmp_config.subnet_mask[0]);
  907. }
  908. static void show_config_mac_address(struct command_context *ctx)
  909. {
  910. if (!memcmp(config.mac_address, tmp_config.mac_address, 6))
  911. command_print(ctx, "MAC address: %.02x:%.02x:%.02x:%.02x:%.02x:%.02x",
  912. config.mac_address[5], config.mac_address[4],
  913. config.mac_address[3], config.mac_address[2],
  914. config.mac_address[1], config.mac_address[0]);
  915. else
  916. command_print(ctx, "MAC address: %.02x:%.02x:%.02x:%.02x:%.02x:%.02x "
  917. "[%.02x:%.02x:%.02x:%.02x:%.02x:%.02x]",
  918. config.mac_address[5], config.mac_address[4],
  919. config.mac_address[3], config.mac_address[2],
  920. config.mac_address[1], config.mac_address[0],
  921. tmp_config.mac_address[5], tmp_config.mac_address[4],
  922. tmp_config.mac_address[3], tmp_config.mac_address[2],
  923. tmp_config.mac_address[1], tmp_config.mac_address[0]);
  924. }
  925. static void show_config_target_power(struct command_context *ctx)
  926. {
  927. const char *target_power;
  928. const char *current_target_power;
  929. if (!config.target_power)
  930. target_power = "off";
  931. else
  932. target_power = "on";
  933. if (!tmp_config.target_power)
  934. current_target_power = "off";
  935. else
  936. current_target_power = "on";
  937. if (config.target_power != tmp_config.target_power)
  938. command_print(ctx, "Target power supply: %s [%s]", target_power,
  939. current_target_power);
  940. else
  941. command_print(ctx, "Target power supply: %s", target_power);
  942. }
  943. static void show_config(struct command_context *ctx)
  944. {
  945. command_print(ctx, "J-Link device configuration:");
  946. show_config_usb_address(ctx);
  947. if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_SET_TARGET_POWER))
  948. show_config_target_power(ctx);
  949. if (jaylink_has_cap(caps, JAYLINK_DEV_CAP_ETHERNET)) {
  950. show_config_ip_address(ctx);
  951. show_config_mac_address(ctx);
  952. }
  953. }
  954. static int poll_trace(uint8_t *buf, size_t *size)
  955. {
  956. int ret;
  957. uint32_t length;
  958. length = *size;
  959. ret = jaylink_swo_read(devh, buf, &length);
  960. if (ret != JAYLINK_OK) {
  961. LOG_ERROR("jaylink_swo_read() failed: %s.", jaylink_strerror_name(ret));
  962. return ERROR_FAIL;
  963. }
  964. *size = length;
  965. return ERROR_OK;
  966. }
  967. static uint32_t calculate_trace_buffer_size(void)
  968. {
  969. int ret;
  970. uint32_t tmp;
  971. if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_GET_FREE_MEMORY))
  972. return 0;
  973. ret = jaylink_get_free_memory(devh, &tmp);
  974. if (ret != JAYLINK_OK) {
  975. LOG_ERROR("jaylink_get_free_memory() failed: %s.",
  976. jaylink_strerror_name(ret));
  977. return ERROR_FAIL;
  978. }
  979. if (tmp > 0x3fff || tmp <= 0x600)
  980. tmp = tmp >> 1;
  981. else
  982. tmp = tmp - 0x400;
  983. return tmp & 0xffffff00;
  984. }
  985. static bool check_trace_freq(struct jaylink_swo_speed speed,
  986. uint32_t trace_freq)
  987. {
  988. double min;
  989. double deviation;
  990. uint32_t divider;
  991. min = fabs(1.0 - (speed.freq / ((double)trace_freq * speed.min_div)));
  992. for (divider = speed.min_div; divider < speed.max_div; divider++) {
  993. deviation = fabs(1.0 - (speed.freq / ((double)trace_freq * divider)));
  994. if (deviation < 0.03) {
  995. LOG_DEBUG("Found suitable frequency divider %u with deviation of "
  996. "%.02f %%.", divider, deviation);
  997. return true;
  998. }
  999. if (deviation < min)
  1000. min = deviation;
  1001. }
  1002. LOG_ERROR("Selected trace frequency is not supported by the device. "
  1003. "Please choose a different trace frequency.");
  1004. LOG_ERROR("Maximum permitted deviation is 3.00 %%, but only %.02f %% "
  1005. "could be achieved.", min * 100);
  1006. return false;
  1007. }
  1008. static int config_trace(bool enabled, enum tpio_pin_protocol pin_protocol,
  1009. uint32_t port_size, unsigned int *trace_freq)
  1010. {
  1011. int ret;
  1012. uint32_t buffer_size;
  1013. struct jaylink_swo_speed speed;
  1014. if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_SWO)) {
  1015. LOG_ERROR("Trace capturing is not supported by the device.");
  1016. return ERROR_FAIL;
  1017. }
  1018. if (pin_protocol != ASYNC_UART) {
  1019. LOG_ERROR("Selected pin protocol is not supported.");
  1020. return ERROR_FAIL;
  1021. }
  1022. trace_enabled = enabled;
  1023. ret = jaylink_swo_stop(devh);
  1024. if (ret != JAYLINK_OK) {
  1025. LOG_ERROR("jaylink_swo_stop() failed: %s.", jaylink_strerror_name(ret));
  1026. return ERROR_FAIL;
  1027. }
  1028. if (!enabled) {
  1029. /*
  1030. * Adjust the SWD transaction buffer size as stopping SWO capturing
  1031. * deallocates device internal memory.
  1032. */
  1033. if (!adjust_swd_buffer_size())
  1034. return ERROR_FAIL;
  1035. return ERROR_OK;
  1036. }
  1037. buffer_size = calculate_trace_buffer_size();
  1038. if (!buffer_size) {
  1039. LOG_ERROR("Not enough free device memory to start trace capturing.");
  1040. return ERROR_FAIL;
  1041. }
  1042. ret = jaylink_swo_get_speeds(devh, JAYLINK_SWO_MODE_UART, &speed);
  1043. if (ret != JAYLINK_OK) {
  1044. LOG_ERROR("jaylink_swo_get_speeds() failed: %s.",
  1045. jaylink_strerror_name(ret));
  1046. return ERROR_FAIL;
  1047. }
  1048. if (!*trace_freq)
  1049. *trace_freq = speed.freq / speed.min_div;
  1050. if (!check_trace_freq(speed, *trace_freq))
  1051. return ERROR_FAIL;
  1052. LOG_DEBUG("Using %u bytes device memory for trace capturing.", buffer_size);
  1053. ret = jaylink_swo_start(devh, JAYLINK_SWO_MODE_UART, *trace_freq,
  1054. buffer_size);
  1055. if (ret != JAYLINK_OK) {
  1056. LOG_ERROR("jaylink_start_swo() failed: %s.",
  1057. jaylink_strerror_name(ret));
  1058. return ERROR_FAIL;
  1059. }
  1060. /*
  1061. * Adjust the SWD transaction buffer size as starting SWO capturing
  1062. * allocates device internal memory.
  1063. */
  1064. if (!adjust_swd_buffer_size())
  1065. return ERROR_FAIL;
  1066. return ERROR_OK;
  1067. }
  1068. COMMAND_HANDLER(jlink_handle_config_usb_address_command)
  1069. {
  1070. uint8_t tmp;
  1071. if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
  1072. command_print(CMD_CTX, "Reading configuration is not supported by the "
  1073. "device.");
  1074. return ERROR_OK;
  1075. }
  1076. if (!CMD_ARGC) {
  1077. show_config_usb_address(CMD_CTX);
  1078. } else if (CMD_ARGC == 1) {
  1079. if (sscanf(CMD_ARGV[0], "%" SCNd8, &tmp) != 1) {
  1080. command_print(CMD_CTX, "Invalid USB address: %s.", CMD_ARGV[0]);
  1081. return ERROR_FAIL;
  1082. }
  1083. if (tmp > JAYLINK_USB_ADDRESS_3) {
  1084. command_print(CMD_CTX, "Invalid USB address: %u.", tmp);
  1085. return ERROR_FAIL;
  1086. }
  1087. tmp_config.usb_address = tmp;
  1088. } else {
  1089. command_print(CMD_CTX, "Need exactly one argument for jlink config "
  1090. "usb.");
  1091. return ERROR_COMMAND_SYNTAX_ERROR;
  1092. }
  1093. return ERROR_OK;
  1094. }
  1095. COMMAND_HANDLER(jlink_handle_config_target_power_command)
  1096. {
  1097. int enable;
  1098. if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
  1099. command_print(CMD_CTX, "Reading configuration is not supported by the "
  1100. "device.");
  1101. return ERROR_OK;
  1102. }
  1103. if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_SET_TARGET_POWER)) {
  1104. command_print(CMD_CTX, "Target power supply is not supported by the "
  1105. "device.");
  1106. return ERROR_OK;
  1107. }
  1108. if (!CMD_ARGC) {
  1109. show_config_target_power(CMD_CTX);
  1110. } else if (CMD_ARGC == 1) {
  1111. if (!strcmp(CMD_ARGV[0], "on")) {
  1112. enable = true;
  1113. } else if (!strcmp(CMD_ARGV[0], "off")) {
  1114. enable = false;
  1115. } else {
  1116. command_print(CMD_CTX, "Invalid argument: %s.", CMD_ARGV[0]);
  1117. return ERROR_FAIL;
  1118. }
  1119. tmp_config.target_power = enable;
  1120. } else {
  1121. command_print(CMD_CTX, "Need exactly one argument for jlink config "
  1122. "targetpower.");
  1123. return ERROR_COMMAND_SYNTAX_ERROR;
  1124. }
  1125. return ERROR_OK;
  1126. }
  1127. COMMAND_HANDLER(jlink_handle_config_mac_address_command)
  1128. {
  1129. uint8_t addr[6];
  1130. int i;
  1131. char *e;
  1132. const char *str;
  1133. if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
  1134. command_print(CMD_CTX, "Reading configuration is not supported by the "
  1135. "device.");
  1136. return ERROR_OK;
  1137. }
  1138. if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_ETHERNET)) {
  1139. command_print(CMD_CTX, "Ethernet connectivity is not supported by the "
  1140. "device.");
  1141. return ERROR_OK;
  1142. }
  1143. if (!CMD_ARGC) {
  1144. show_config_mac_address(CMD_CTX);
  1145. } else if (CMD_ARGC == 1) {
  1146. str = CMD_ARGV[0];
  1147. if ((strlen(str) != 17) || (str[2] != ':' || str[5] != ':' || \
  1148. str[8] != ':' || str[11] != ':' || str[14] != ':')) {
  1149. command_print(CMD_CTX, "Invalid MAC address format.");
  1150. return ERROR_COMMAND_SYNTAX_ERROR;
  1151. }
  1152. for (i = 5; i >= 0; i--) {
  1153. addr[i] = strtoul(str, &e, 16);
  1154. str = e + 1;
  1155. }
  1156. if (!(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5])) {
  1157. command_print(CMD_CTX, "Invalid MAC address: zero address.");
  1158. return ERROR_COMMAND_SYNTAX_ERROR;
  1159. }
  1160. if (!(0x01 & addr[0])) {
  1161. command_print(CMD_CTX, "Invalid MAC address: multicast address.");
  1162. return ERROR_COMMAND_SYNTAX_ERROR;
  1163. }
  1164. memcpy(tmp_config.mac_address, addr, sizeof(addr));
  1165. } else {
  1166. command_print(CMD_CTX, "Need exactly one argument for jlink config "
  1167. " mac.");
  1168. return ERROR_COMMAND_SYNTAX_ERROR;
  1169. }
  1170. return ERROR_OK;
  1171. }
  1172. static bool string_to_ip(const char *s, uint8_t *ip, int *pos)
  1173. {
  1174. uint8_t lip[4];
  1175. char *e;
  1176. const char *s_save = s;
  1177. int i;
  1178. if (!s)
  1179. return false;
  1180. for (i = 0; i < 4; i++) {
  1181. lip[i] = strtoul(s, &e, 10);
  1182. if (*e != '.' && i != 3)
  1183. return false;
  1184. s = e + 1;
  1185. }
  1186. *pos = e - s_save;
  1187. memcpy(ip, lip, sizeof(lip));
  1188. return true;
  1189. }
  1190. static void cpy_ip(uint8_t *dst, uint8_t *src)
  1191. {
  1192. int i, j;
  1193. for (i = 0, j = 3; i < 4; i++, j--)
  1194. dst[i] = src[j];
  1195. }
  1196. COMMAND_HANDLER(jlink_handle_config_ip_address_command)
  1197. {
  1198. uint8_t ip_address[4];
  1199. uint32_t subnet_mask = 0;
  1200. int i, len;
  1201. uint8_t subnet_bits = 24;
  1202. if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
  1203. command_print(CMD_CTX, "Reading configuration is not supported by the "
  1204. "device.");
  1205. return ERROR_OK;
  1206. }
  1207. if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_ETHERNET)) {
  1208. command_print(CMD_CTX, "Ethernet connectivity is not supported by the "
  1209. "device.");
  1210. return ERROR_OK;
  1211. }
  1212. if (!CMD_ARGC) {
  1213. show_config_ip_address(CMD_CTX);
  1214. } else {
  1215. if (!string_to_ip(CMD_ARGV[0], ip_address, &i))
  1216. return ERROR_COMMAND_SYNTAX_ERROR;
  1217. len = strlen(CMD_ARGV[0]);
  1218. /* Check for format A.B.C.D/E. */
  1219. if (i < len) {
  1220. if (CMD_ARGV[0][i] != '/')
  1221. return ERROR_COMMAND_SYNTAX_ERROR;
  1222. COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0] + i + 1, subnet_bits);
  1223. } else if (CMD_ARGC > 1) {
  1224. if (!string_to_ip(CMD_ARGV[1], (uint8_t *)&subnet_mask, &i))
  1225. return ERROR_COMMAND_SYNTAX_ERROR;
  1226. }
  1227. if (!subnet_mask)
  1228. subnet_mask = (uint32_t)(subnet_bits < 32 ?
  1229. ((1ULL << subnet_bits) - 1) : 0xffffffff);
  1230. cpy_ip(tmp_config.ip_address, ip_address);
  1231. cpy_ip(tmp_config.subnet_mask, (uint8_t *)&subnet_mask);
  1232. }
  1233. return ERROR_OK;
  1234. }
  1235. COMMAND_HANDLER(jlink_handle_config_reset_command)
  1236. {
  1237. if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG))
  1238. return ERROR_OK;
  1239. memcpy(&tmp_config, &config, sizeof(struct device_config));
  1240. return ERROR_OK;
  1241. }
  1242. COMMAND_HANDLER(jlink_handle_config_write_command)
  1243. {
  1244. int ret;
  1245. if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
  1246. command_print(CMD_CTX, "Reading configuration is not supported by the "
  1247. "device.");
  1248. return ERROR_OK;
  1249. }
  1250. if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_WRITE_CONFIG)) {
  1251. command_print(CMD_CTX, "Writing configuration is not supported by the "
  1252. "device.");
  1253. return ERROR_OK;
  1254. }
  1255. if (!memcmp(&config, &tmp_config, sizeof(struct device_config))) {
  1256. command_print(CMD_CTX, "Operation not performed due to no changes in "
  1257. "the configuration.");
  1258. return ERROR_OK;
  1259. }
  1260. ret = jaylink_write_raw_config(devh, (const uint8_t *)&tmp_config);
  1261. if (ret != JAYLINK_OK) {
  1262. LOG_ERROR("jaylink_write_raw_config() failed: %s.",
  1263. jaylink_strerror_name(ret));
  1264. return ERROR_FAIL;
  1265. }
  1266. if (!read_device_config(&config)) {
  1267. LOG_ERROR("Failed to read device configuration for verification.");
  1268. return ERROR_FAIL;
  1269. }
  1270. if (memcmp(&config, &tmp_config, sizeof(struct device_config))) {
  1271. LOG_ERROR("Verification of device configuration failed. Please check "
  1272. "your device.");
  1273. return ERROR_FAIL;
  1274. }
  1275. memcpy(&tmp_config, &config, sizeof(struct device_config));
  1276. command_print(CMD_CTX, "The new device configuration applies after power "
  1277. "cycling the J-Link device.");
  1278. return ERROR_OK;
  1279. }
  1280. COMMAND_HANDLER(jlink_handle_config_command)
  1281. {
  1282. if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
  1283. command_print(CMD_CTX, "Device doesn't support reading configuration.");
  1284. return ERROR_OK;
  1285. }
  1286. if (CMD_ARGC == 0)
  1287. show_config(CMD_CTX);
  1288. return ERROR_OK;
  1289. }
  1290. static const struct command_registration jlink_config_subcommand_handlers[] = {
  1291. {
  1292. .name = "usb",
  1293. .handler = &jlink_handle_config_usb_address_command,
  1294. .mode = COMMAND_EXEC,
  1295. .help = "set the USB address",
  1296. .usage = "[0-3]",
  1297. },
  1298. {
  1299. .name = "targetpower",
  1300. .handler = &jlink_handle_config_target_power_command,
  1301. .mode = COMMAND_EXEC,
  1302. .help = "set the target power supply",
  1303. .usage = "[on|off]"
  1304. },
  1305. {
  1306. .name = "mac",
  1307. .handler = &jlink_handle_config_mac_address_command,
  1308. .mode = COMMAND_EXEC,
  1309. .help = "set the MAC Address",
  1310. .usage = "[ff:ff:ff:ff:ff:ff]",
  1311. },
  1312. {
  1313. .name = "ip",
  1314. .handler = &jlink_handle_config_ip_address_command,
  1315. .mode = COMMAND_EXEC,
  1316. .help = "set the IP address, where A.B.C.D is the IP address, "
  1317. "E the bit of the subnet mask, F.G.H.I the subnet mask",
  1318. .usage = "[A.B.C.D[/E] [F.G.H.I]]",
  1319. },
  1320. {
  1321. .name = "reset",
  1322. .handler = &jlink_handle_config_reset_command,
  1323. .mode = COMMAND_EXEC,
  1324. .help = "undo configuration changes"
  1325. },
  1326. {
  1327. .name = "write",
  1328. .handler = &jlink_handle_config_write_command,
  1329. .mode = COMMAND_EXEC,
  1330. .help = "write configuration to the device"
  1331. },
  1332. COMMAND_REGISTRATION_DONE
  1333. };
  1334. static const struct command_registration jlink_subcommand_handlers[] = {
  1335. {
  1336. .name = "jtag",
  1337. .handler = &jlink_handle_jlink_jtag_command,
  1338. .mode = COMMAND_EXEC,
  1339. .help = "select the JTAG command version",
  1340. .usage = "[2|3]",
  1341. },
  1342. {
  1343. .name = "targetpower",
  1344. .handler = &jlink_handle_target_power_command,
  1345. .mode = COMMAND_EXEC,
  1346. .help = "set the target power supply",
  1347. .usage = "<on|off>"
  1348. },
  1349. {
  1350. .name = "freemem",
  1351. .handler = &jlink_handle_free_memory_command,
  1352. .mode = COMMAND_EXEC,
  1353. .help = "show free device memory"
  1354. },
  1355. {
  1356. .name = "hwstatus",
  1357. .handler = &jlink_handle_hwstatus_command,
  1358. .mode = COMMAND_EXEC,
  1359. .help = "show the hardware status"
  1360. },
  1361. {
  1362. .name = "usb",
  1363. .handler = &jlink_usb_command,
  1364. .mode = COMMAND_CONFIG,
  1365. .help = "set the USB address of the device that should be used",
  1366. .usage = "<0-3>"
  1367. },
  1368. {
  1369. .name = "serial",
  1370. .handler = &jlink_serial_command,
  1371. .mode = COMMAND_CONFIG,
  1372. .help = "set the serial number of the device that should be used",
  1373. .usage = "<serial number>"
  1374. },
  1375. {
  1376. .name = "config",
  1377. .handler = &jlink_handle_config_command,
  1378. .mode = COMMAND_EXEC,
  1379. .help = "access the device configuration. If no argument is given "
  1380. "this will show the device configuration",
  1381. .chain = jlink_config_subcommand_handlers,
  1382. },
  1383. COMMAND_REGISTRATION_DONE
  1384. };
  1385. static const struct command_registration jlink_command_handlers[] = {
  1386. {
  1387. .name = "jlink",
  1388. .mode = COMMAND_ANY,
  1389. .help = "perform jlink management",
  1390. .chain = jlink_subcommand_handlers,
  1391. },
  1392. COMMAND_REGISTRATION_DONE
  1393. };
  1394. static int jlink_swd_init(void)
  1395. {
  1396. iface = JAYLINK_TIF_SWD;
  1397. return ERROR_OK;
  1398. }
  1399. static void jlink_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
  1400. {
  1401. assert(!(cmd & SWD_CMD_RnW));
  1402. jlink_swd_queue_cmd(cmd, NULL, value, ap_delay_clk);
  1403. }
  1404. static void jlink_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
  1405. {
  1406. assert(cmd & SWD_CMD_RnW);
  1407. jlink_swd_queue_cmd(cmd, value, 0, ap_delay_clk);
  1408. }
  1409. static int_least32_t jlink_swd_frequency(int_least32_t hz)
  1410. {
  1411. if (hz > 0)
  1412. jlink_speed(hz / 1000);
  1413. return hz;
  1414. }
  1415. /***************************************************************************/
  1416. /* J-Link tap functions */
  1417. static unsigned tap_length;
  1418. /* In SWD mode use tms buffer for direction control */
  1419. static uint8_t tms_buffer[JLINK_TAP_BUFFER_SIZE];
  1420. static uint8_t tdi_buffer[JLINK_TAP_BUFFER_SIZE];
  1421. static uint8_t tdo_buffer[JLINK_TAP_BUFFER_SIZE];
  1422. struct pending_scan_result {
  1423. /** First bit position in tdo_buffer to read. */
  1424. unsigned first;
  1425. /** Number of bits to read. */
  1426. unsigned length;
  1427. /** Location to store the result */
  1428. void *buffer;
  1429. /** Offset in the destination buffer */
  1430. unsigned buffer_offset;
  1431. };
  1432. #define MAX_PENDING_SCAN_RESULTS 256
  1433. static int pending_scan_results_length;
  1434. static struct pending_scan_result pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
  1435. static void jlink_tap_init(void)
  1436. {
  1437. tap_length = 0;
  1438. pending_scan_results_length = 0;
  1439. memset(tms_buffer, 0, sizeof(tms_buffer));
  1440. memset(tdi_buffer, 0, sizeof(tdi_buffer));
  1441. }
  1442. static void jlink_clock_data(const uint8_t *out, unsigned out_offset,
  1443. const uint8_t *tms_out, unsigned tms_offset,
  1444. uint8_t *in, unsigned in_offset,
  1445. unsigned length)
  1446. {
  1447. do {
  1448. unsigned available_length = JLINK_TAP_BUFFER_SIZE - tap_length / 8;
  1449. if (!available_length ||
  1450. (in && pending_scan_results_length == MAX_PENDING_SCAN_RESULTS)) {
  1451. if (jlink_flush() != ERROR_OK)
  1452. return;
  1453. available_length = JLINK_TAP_BUFFER_SIZE;
  1454. }
  1455. struct pending_scan_result *pending_scan_result =
  1456. &pending_scan_results_buffer[pending_scan_results_length];
  1457. unsigned scan_length = length > available_length ?
  1458. available_length : length;
  1459. if (out)
  1460. buf_set_buf(out, out_offset, tdi_buffer, tap_length, scan_length);
  1461. if (tms_out)
  1462. buf_set_buf(tms_out, tms_offset, tms_buffer, tap_length, scan_length);
  1463. if (in) {
  1464. pending_scan_result->first = tap_length;
  1465. pending_scan_result->length = scan_length;
  1466. pending_scan_result->buffer = in;
  1467. pending_scan_result->buffer_offset = in_offset;
  1468. pending_scan_results_length++;
  1469. }
  1470. tap_length += scan_length;
  1471. out_offset += scan_length;
  1472. tms_offset += scan_length;
  1473. in_offset += scan_length;
  1474. length -= scan_length;
  1475. } while (length > 0);
  1476. }
  1477. static int jlink_flush(void)
  1478. {
  1479. int i;
  1480. int ret;
  1481. if (!tap_length)
  1482. return ERROR_OK;
  1483. jlink_last_state = jtag_debug_state_machine(tms_buffer, tdi_buffer,
  1484. tap_length, jlink_last_state);
  1485. ret = jaylink_jtag_io(devh, tms_buffer, tdi_buffer, tdo_buffer,
  1486. tap_length, jtag_command_version);
  1487. if (ret != JAYLINK_OK) {
  1488. LOG_ERROR("jaylink_jtag_io() failed: %s.", jaylink_strerror_name(ret));
  1489. jlink_tap_init();
  1490. return ERROR_JTAG_QUEUE_FAILED;
  1491. }
  1492. for (i = 0; i < pending_scan_results_length; i++) {
  1493. struct pending_scan_result *p = &pending_scan_results_buffer[i];
  1494. buf_set_buf(tdo_buffer, p->first, p->buffer,
  1495. p->buffer_offset, p->length);
  1496. DEBUG_JTAG_IO("Pending scan result, length = %d.", p->length);
  1497. }
  1498. jlink_tap_init();
  1499. return ERROR_OK;
  1500. }
  1501. static void fill_buffer(uint8_t *buf, uint32_t val, uint32_t len)
  1502. {
  1503. unsigned int tap_pos = tap_length;
  1504. while (len > 32) {
  1505. buf_set_u32(buf, tap_pos, 32, val);
  1506. len -= 32;
  1507. tap_pos += 32;
  1508. }
  1509. if (len)
  1510. buf_set_u32(buf, tap_pos, len, val);
  1511. }
  1512. static void jlink_queue_data_out(const uint8_t *data, uint32_t len)
  1513. {
  1514. const uint32_t dir_out = 0xffffffff;
  1515. if (data)
  1516. bit_copy(tdi_buffer, tap_length, data, 0, len);
  1517. else
  1518. fill_buffer(tdi_buffer, 0, len);
  1519. fill_buffer(tms_buffer, dir_out, len);
  1520. tap_length += len;
  1521. }
  1522. static void jlink_queue_data_in(uint32_t len)
  1523. {
  1524. const uint32_t dir_in = 0;
  1525. fill_buffer(tms_buffer, dir_in, len);
  1526. tap_length += len;
  1527. }
  1528. static int jlink_swd_switch_seq(enum swd_special_seq seq)
  1529. {
  1530. const uint8_t *s;
  1531. unsigned int s_len;
  1532. switch (seq) {
  1533. case LINE_RESET:
  1534. LOG_DEBUG("SWD line reset");
  1535. s = swd_seq_line_reset;
  1536. s_len = swd_seq_line_reset_len;
  1537. break;
  1538. case JTAG_TO_SWD:
  1539. LOG_DEBUG("JTAG-to-SWD");
  1540. s = swd_seq_jtag_to_swd;
  1541. s_len = swd_seq_jtag_to_swd_len;
  1542. break;
  1543. case SWD_TO_JTAG:
  1544. LOG_DEBUG("SWD-to-JTAG");
  1545. s = swd_seq_swd_to_jtag;
  1546. s_len = swd_seq_swd_to_jtag_len;
  1547. break;
  1548. default:
  1549. LOG_ERROR("Sequence %d not supported.", seq);
  1550. return ERROR_FAIL;
  1551. }
  1552. jlink_queue_data_out(s, s_len);
  1553. return ERROR_OK;
  1554. }
  1555. static int jlink_swd_run_queue(void)
  1556. {
  1557. int i;
  1558. int ret;
  1559. LOG_DEBUG("Executing %d queued transactions.", pending_scan_results_length);
  1560. if (queued_retval != ERROR_OK) {
  1561. LOG_DEBUG("Skipping due to previous errors: %d.", queued_retval);
  1562. goto skip;
  1563. }
  1564. /*
  1565. * A transaction must be followed by another transaction or at least 8 idle
  1566. * cycles to ensure that data is clocked through the AP.
  1567. */
  1568. jlink_queue_data_out(NULL, 8);
  1569. ret = jaylink_swd_io(devh, tms_buffer, tdi_buffer, tdo_buffer, tap_length);
  1570. if (ret != JAYLINK_OK) {
  1571. LOG_ERROR("jaylink_swd_io() failed: %s.", jaylink_strerror_name(ret));
  1572. goto skip;
  1573. }
  1574. for (i = 0; i < pending_scan_results_length; i++) {
  1575. int ack = buf_get_u32(tdo_buffer, pending_scan_results_buffer[i].first, 3);
  1576. if (ack != SWD_ACK_OK) {
  1577. LOG_DEBUG("SWD ack not OK: %d %s", ack,
  1578. ack == SWD_ACK_WAIT ? "WAIT" : ack == SWD_ACK_FAULT ? "FAULT" : "JUNK");
  1579. queued_retval = ack == SWD_ACK_WAIT ? ERROR_WAIT : ERROR_FAIL;
  1580. goto skip;
  1581. } else if (pending_scan_results_buffer[i].length) {
  1582. uint32_t data = buf_get_u32(tdo_buffer, 3 + pending_scan_results_buffer[i].first, 32);
  1583. int parity = buf_get_u32(tdo_buffer, 3 + 32 + pending_scan_results_buffer[i].first, 1);
  1584. if (parity != parity_u32(data)) {
  1585. LOG_ERROR("SWD: Read data parity mismatch.");
  1586. queued_retval = ERROR_FAIL;
  1587. goto skip;
  1588. }
  1589. if (pending_scan_results_buffer[i].buffer)
  1590. *(uint32_t *)pending_scan_results_buffer[i].buffer = data;
  1591. }
  1592. }
  1593. skip:
  1594. jlink_tap_init();
  1595. ret = queued_retval;
  1596. queued_retval = ERROR_OK;
  1597. return ret;
  1598. }
  1599. static void jlink_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data, uint32_t ap_delay_clk)
  1600. {
  1601. uint8_t data_parity_trn[DIV_ROUND_UP(32 + 1, 8)];
  1602. if (tap_length + 46 + 8 + ap_delay_clk >= sizeof(tdi_buffer) * 8 ||
  1603. pending_scan_results_length == MAX_PENDING_SCAN_RESULTS) {
  1604. /* Not enough room in the queue. Run the queue. */
  1605. queued_retval = jlink_swd_run_queue();
  1606. }
  1607. if (queued_retval != ERROR_OK)
  1608. return;
  1609. cmd |= SWD_CMD_START | SWD_CMD_PARK;
  1610. jlink_queue_data_out(&cmd, 8);
  1611. pending_scan_results_buffer[pending_scan_results_length].first = tap_length;
  1612. if (cmd & SWD_CMD_RnW) {
  1613. /* Queue a read transaction. */
  1614. pending_scan_results_buffer[pending_scan_results_length].length = 32;
  1615. pending_scan_results_buffer[pending_scan_results_length].buffer = dst;
  1616. jlink_queue_data_in(1 + 3 + 32 + 1 + 1);
  1617. } else {
  1618. /* Queue a write transaction. */
  1619. pending_scan_results_buffer[pending_scan_results_length].length = 0;
  1620. jlink_queue_data_in(1 + 3 + 1);
  1621. buf_set_u32(data_parity_trn, 0, 32, data);
  1622. buf_set_u32(data_parity_trn, 32, 1, parity_u32(data));
  1623. jlink_queue_data_out(data_parity_trn, 32 + 1);
  1624. }
  1625. pending_scan_results_length++;
  1626. /* Insert idle cycles after AP accesses to avoid WAIT. */
  1627. if (cmd & SWD_CMD_APnDP)
  1628. jlink_queue_data_out(NULL, ap_delay_clk);
  1629. }
  1630. static const struct swd_driver jlink_swd = {
  1631. .init = &jlink_swd_init,
  1632. .frequency = &jlink_swd_frequency,
  1633. .switch_seq = &jlink_swd_switch_seq,
  1634. .read_reg = &jlink_swd_read_reg,
  1635. .write_reg = &jlink_swd_write_reg,
  1636. .run = &jlink_swd_run_queue,
  1637. };
  1638. static const char * const jlink_transports[] = { "jtag", "swd", NULL };
  1639. struct jtag_interface jlink_interface = {
  1640. .name = "jlink",
  1641. .commands = jlink_command_handlers,
  1642. .transports = jlink_transports,
  1643. .swd = &jlink_swd,
  1644. .execute_queue = &jlink_execute_queue,
  1645. .speed = &jlink_speed,
  1646. .speed_div = &jlink_speed_div,
  1647. .khz = &jlink_khz,
  1648. .init = &jlink_init,
  1649. .quit = &jlink_quit,
  1650. .config_trace = &config_trace,
  1651. .poll_trace = &poll_trace,
  1652. };