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.
 
 
 
 
 
 

976 lines
24 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2009-2010 by Simon Qian <SimonQian@SimonQian.com> *
  3. * *
  4. * This program is free software; you can redistribute it and/or modify *
  5. * it under the terms of the GNU General Public License as published by *
  6. * the Free Software Foundation; either version 2 of the License, or *
  7. * (at your option) any later version. *
  8. * *
  9. * This program is distributed in the hope that it will be useful, *
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  12. * GNU General Public License for more details. *
  13. * *
  14. * You should have received a copy of the GNU General Public License *
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  16. ***************************************************************************/
  17. /* Versaloon is a programming tool for multiple MCUs.
  18. * It's distributed under GPLv3.
  19. * You can find it at http://www.Versaloon.com/.
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include "config.h"
  23. #endif
  24. #include <jtag/interface.h>
  25. #include <jtag/commands.h>
  26. #include <jtag/swd.h>
  27. #include <libusb.h>
  28. #include "versaloon/versaloon_include.h"
  29. #include "versaloon/versaloon.h"
  30. static int vsllink_tms_offset;
  31. struct pending_scan_result {
  32. int src_offset;
  33. int dest_offset;
  34. int length; /* Number of bits to read */
  35. struct scan_command *command; /* Corresponding scan command */
  36. uint8_t *ack;
  37. uint8_t *buffer;
  38. bool last; /* indicate the last scan pending */
  39. };
  40. #define MAX_PENDING_SCAN_RESULTS 256
  41. static int pending_scan_results_length;
  42. static struct pending_scan_result
  43. pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
  44. /* Queue command functions */
  45. static void vsllink_end_state(tap_state_t state);
  46. static void vsllink_state_move(void);
  47. static void vsllink_path_move(int num_states, tap_state_t *path);
  48. static void vsllink_tms(int num_bits, const uint8_t *bits);
  49. static void vsllink_runtest(int num_cycles);
  50. static void vsllink_stableclocks(int num_cycles, int tms);
  51. static void vsllink_scan(bool ir_scan, enum scan_type type,
  52. uint8_t *buffer, int scan_size, struct scan_command *command);
  53. static void vsllink_reset(int trst, int srst);
  54. /* VSLLink tap buffer functions */
  55. static void vsllink_tap_append_step(int tms, int tdi);
  56. static void vsllink_tap_init(void);
  57. static int vsllink_tap_execute(void);
  58. static void vsllink_tap_ensure_pending(int scans);
  59. static void vsllink_tap_append_scan(int length, uint8_t *buffer,
  60. struct scan_command *command);
  61. /* VSLLink SWD functions */
  62. static int_least32_t vsllink_swd_frequency(int_least32_t hz);
  63. static int vsllink_swd_switch_seq(enum swd_special_seq seq);
  64. /* VSLLink lowlevel functions */
  65. struct vsllink {
  66. struct libusb_context *libusb_ctx;
  67. struct libusb_device_handle *usb_device_handle;
  68. };
  69. static int vsllink_usb_open(struct vsllink *vsllink);
  70. static void vsllink_usb_close(struct vsllink *vsllink);
  71. #if defined _DEBUG_JTAG_IO_
  72. static void vsllink_debug_buffer(uint8_t *buffer, int length);
  73. #endif
  74. static int tap_length;
  75. static int tap_buffer_size;
  76. static uint8_t *tms_buffer;
  77. static uint8_t *tdi_buffer;
  78. static uint8_t *tdo_buffer;
  79. static bool swd_mode;
  80. static struct vsllink *vsllink_handle;
  81. static int vsllink_execute_queue(void)
  82. {
  83. struct jtag_command *cmd = jtag_command_queue;
  84. int scan_size;
  85. enum scan_type type;
  86. uint8_t *buffer;
  87. DEBUG_JTAG_IO("-------------------------------------"
  88. " vsllink "
  89. "-------------------------------------");
  90. while (cmd != NULL) {
  91. switch (cmd->type) {
  92. case JTAG_RUNTEST:
  93. DEBUG_JTAG_IO("runtest %i cycles, end in %s",
  94. cmd->cmd.runtest->num_cycles,
  95. tap_state_name(cmd->cmd.runtest->end_state));
  96. vsllink_end_state(cmd->cmd.runtest->end_state);
  97. vsllink_runtest(cmd->cmd.runtest->num_cycles);
  98. break;
  99. case JTAG_TLR_RESET:
  100. DEBUG_JTAG_IO("statemove end in %s",
  101. tap_state_name(cmd->cmd.statemove->end_state));
  102. vsllink_end_state(cmd->cmd.statemove->end_state);
  103. vsllink_state_move();
  104. break;
  105. case JTAG_PATHMOVE:
  106. DEBUG_JTAG_IO("pathmove: %i states, end in %s",
  107. cmd->cmd.pathmove->num_states,
  108. tap_state_name(cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]));
  109. vsllink_path_move(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path);
  110. break;
  111. case JTAG_SCAN:
  112. DEBUG_JTAG_IO("JTAG Scan...");
  113. vsllink_end_state(cmd->cmd.scan->end_state);
  114. scan_size = jtag_build_buffer(
  115. cmd->cmd.scan, &buffer);
  116. if (cmd->cmd.scan->ir_scan)
  117. DEBUG_JTAG_IO(
  118. "JTAG Scan write IR(%d bits), "
  119. "end in %s:",
  120. scan_size,
  121. tap_state_name(cmd->cmd.scan->end_state));
  122. else
  123. DEBUG_JTAG_IO(
  124. "JTAG Scan write DR(%d bits), "
  125. "end in %s:",
  126. scan_size,
  127. tap_state_name(cmd->cmd.scan->end_state));
  128. #ifdef _DEBUG_JTAG_IO_
  129. vsllink_debug_buffer(buffer,
  130. DIV_ROUND_UP(scan_size, 8));
  131. #endif
  132. type = jtag_scan_type(cmd->cmd.scan);
  133. vsllink_scan(cmd->cmd.scan->ir_scan,
  134. type, buffer, scan_size,
  135. cmd->cmd.scan);
  136. break;
  137. case JTAG_RESET:
  138. DEBUG_JTAG_IO("reset trst: %i srst %i",
  139. cmd->cmd.reset->trst,
  140. cmd->cmd.reset->srst);
  141. vsllink_tap_execute();
  142. if (cmd->cmd.reset->trst == 1)
  143. tap_set_state(TAP_RESET);
  144. vsllink_reset(cmd->cmd.reset->trst,
  145. cmd->cmd.reset->srst);
  146. break;
  147. case JTAG_SLEEP:
  148. DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
  149. vsllink_tap_execute();
  150. jtag_sleep(cmd->cmd.sleep->us);
  151. break;
  152. case JTAG_STABLECLOCKS:
  153. DEBUG_JTAG_IO("add %d clocks",
  154. cmd->cmd.stableclocks->num_cycles);
  155. switch (tap_get_state()) {
  156. case TAP_RESET:
  157. /* tms must be '1' to stay
  158. * n TAP_RESET mode
  159. */
  160. scan_size = 1;
  161. break;
  162. case TAP_DRSHIFT:
  163. case TAP_IDLE:
  164. case TAP_DRPAUSE:
  165. case TAP_IRSHIFT:
  166. case TAP_IRPAUSE:
  167. /* else, tms should be '0' */
  168. scan_size = 0;
  169. break;
  170. /* above stable states are OK */
  171. default:
  172. LOG_ERROR("jtag_add_clocks() "
  173. "in non-stable state \"%s\"",
  174. tap_state_name(tap_get_state())
  175. );
  176. exit(-1);
  177. }
  178. vsllink_stableclocks(cmd->cmd.stableclocks->num_cycles, scan_size);
  179. break;
  180. case JTAG_TMS:
  181. DEBUG_JTAG_IO("add %d jtag tms",
  182. cmd->cmd.tms->num_bits);
  183. vsllink_tms(cmd->cmd.tms->num_bits, cmd->cmd.tms->bits);
  184. break;
  185. default:
  186. LOG_ERROR("BUG: unknown JTAG command type "
  187. "encountered: %d", cmd->type);
  188. exit(-1);
  189. }
  190. cmd = cmd->next;
  191. }
  192. return vsllink_tap_execute();
  193. }
  194. static int vsllink_speed(int speed)
  195. {
  196. if (swd_mode) {
  197. vsllink_swd_frequency(speed * 1000);
  198. return ERROR_OK;
  199. }
  200. versaloon_interface.adaptors.jtag_raw.config(0, (uint16_t)speed);
  201. return versaloon_interface.adaptors.peripheral_commit();
  202. }
  203. static int vsllink_khz(int khz, int *jtag_speed)
  204. {
  205. *jtag_speed = khz;
  206. return ERROR_OK;
  207. }
  208. static int vsllink_speed_div(int jtag_speed, int *khz)
  209. {
  210. *khz = jtag_speed;
  211. return ERROR_OK;
  212. }
  213. static void vsllink_free_buffer(void)
  214. {
  215. if (tdi_buffer != NULL) {
  216. free(tdi_buffer);
  217. tdi_buffer = NULL;
  218. }
  219. if (tdo_buffer != NULL) {
  220. free(tdo_buffer);
  221. tdo_buffer = NULL;
  222. }
  223. if (tms_buffer != NULL) {
  224. free(tms_buffer);
  225. tms_buffer = NULL;
  226. }
  227. }
  228. static int vsllink_quit(void)
  229. {
  230. versaloon_interface.adaptors.gpio.config(0, GPIO_SRST | GPIO_TRST,
  231. 0, 0, GPIO_SRST | GPIO_TRST);
  232. versaloon_interface.adaptors.gpio.fini(0);
  233. if (swd_mode)
  234. versaloon_interface.adaptors.swd.fini(0);
  235. else
  236. versaloon_interface.adaptors.jtag_raw.fini(0);
  237. versaloon_interface.adaptors.peripheral_commit();
  238. versaloon_interface.fini();
  239. vsllink_free_buffer();
  240. vsllink_usb_close(vsllink_handle);
  241. free(vsllink_handle);
  242. return ERROR_OK;
  243. }
  244. static int vsllink_interface_init(void)
  245. {
  246. vsllink_handle = malloc(sizeof(struct vsllink));
  247. if (NULL == vsllink_handle) {
  248. LOG_ERROR("unable to allocate memory");
  249. return ERROR_FAIL;
  250. }
  251. libusb_init(&vsllink_handle->libusb_ctx);
  252. if (ERROR_OK != vsllink_usb_open(vsllink_handle)) {
  253. LOG_ERROR("Can't find USB JTAG Interface!" \
  254. "Please check connection and permissions.");
  255. return ERROR_JTAG_INIT_FAILED;
  256. }
  257. LOG_DEBUG("vsllink found on %04X:%04X",
  258. versaloon_interface.usb_setting.vid,
  259. versaloon_interface.usb_setting.pid);
  260. versaloon_usb_device_handle = vsllink_handle->usb_device_handle;
  261. if (ERROR_OK != versaloon_interface.init())
  262. return ERROR_FAIL;
  263. if (versaloon_interface.usb_setting.buf_size < 32) {
  264. versaloon_interface.fini();
  265. return ERROR_FAIL;
  266. }
  267. return ERROR_OK;
  268. }
  269. static int vsllink_init(void)
  270. {
  271. int retval = vsllink_interface_init();
  272. if (ERROR_OK != retval)
  273. return retval;
  274. versaloon_interface.adaptors.gpio.init(0);
  275. versaloon_interface.adaptors.gpio.config(0, GPIO_SRST, 0, GPIO_SRST,
  276. GPIO_SRST);
  277. versaloon_interface.adaptors.delay.delayms(100);
  278. versaloon_interface.adaptors.peripheral_commit();
  279. if (swd_mode) {
  280. versaloon_interface.adaptors.gpio.config(0, GPIO_TRST, 0,
  281. GPIO_TRST, GPIO_TRST);
  282. versaloon_interface.adaptors.swd.init(0);
  283. vsllink_swd_frequency(jtag_get_speed_khz() * 1000);
  284. vsllink_swd_switch_seq(JTAG_TO_SWD);
  285. } else {
  286. /* malloc buffer size for tap */
  287. tap_buffer_size = versaloon_interface.usb_setting.buf_size / 2 - 32;
  288. vsllink_free_buffer();
  289. tdi_buffer = malloc(tap_buffer_size);
  290. tdo_buffer = malloc(tap_buffer_size);
  291. tms_buffer = malloc(tap_buffer_size);
  292. if ((NULL == tdi_buffer) || (NULL == tdo_buffer) || (NULL == tms_buffer)) {
  293. vsllink_quit();
  294. return ERROR_FAIL;
  295. }
  296. versaloon_interface.adaptors.jtag_raw.init(0);
  297. versaloon_interface.adaptors.jtag_raw.config(0, jtag_get_speed_khz());
  298. versaloon_interface.adaptors.gpio.config(0, GPIO_SRST | GPIO_TRST,
  299. GPIO_TRST, GPIO_SRST, GPIO_SRST);
  300. }
  301. if (ERROR_OK != versaloon_interface.adaptors.peripheral_commit())
  302. return ERROR_FAIL;
  303. vsllink_reset(0, 0);
  304. vsllink_tap_init();
  305. return ERROR_OK;
  306. }
  307. /**************************************************************************
  308. * Queue command implementations */
  309. static void vsllink_end_state(tap_state_t state)
  310. {
  311. if (tap_is_state_stable(state))
  312. tap_set_end_state(state);
  313. else {
  314. LOG_ERROR("BUG: %i is not a valid end state", state);
  315. exit(-1);
  316. }
  317. }
  318. /* Goes to the end state. */
  319. static void vsllink_state_move(void)
  320. {
  321. int i;
  322. uint8_t tms_scan = tap_get_tms_path(tap_get_state(),
  323. tap_get_end_state());
  324. uint8_t tms_scan_bits = tap_get_tms_path_len(tap_get_state(),
  325. tap_get_end_state());
  326. for (i = 0; i < tms_scan_bits; i++)
  327. vsllink_tap_append_step((tms_scan >> i) & 1, 0);
  328. tap_set_state(tap_get_end_state());
  329. }
  330. static void vsllink_path_move(int num_states, tap_state_t *path)
  331. {
  332. for (int i = 0; i < num_states; i++) {
  333. if (path[i] == tap_state_transition(tap_get_state(), false))
  334. vsllink_tap_append_step(0, 0);
  335. else if (path[i] == tap_state_transition(tap_get_state(), true))
  336. vsllink_tap_append_step(1, 0);
  337. else {
  338. LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
  339. tap_state_name(tap_get_state()),
  340. tap_state_name(path[i]));
  341. exit(-1);
  342. }
  343. tap_set_state(path[i]);
  344. }
  345. tap_set_end_state(tap_get_state());
  346. }
  347. static void vsllink_tms(int num_bits, const uint8_t *bits)
  348. {
  349. for (int i = 0; i < num_bits; i++)
  350. vsllink_tap_append_step((bits[i / 8] >> (i % 8)) & 1, 0);
  351. }
  352. static void vsllink_stableclocks(int num_cycles, int tms)
  353. {
  354. while (num_cycles > 0) {
  355. vsllink_tap_append_step(tms, 0);
  356. num_cycles--;
  357. }
  358. }
  359. static void vsllink_runtest(int num_cycles)
  360. {
  361. tap_state_t saved_end_state = tap_get_end_state();
  362. if (tap_get_state() != TAP_IDLE) {
  363. /* enter IDLE state */
  364. vsllink_end_state(TAP_IDLE);
  365. vsllink_state_move();
  366. }
  367. vsllink_stableclocks(num_cycles, 0);
  368. /* post-process */
  369. /* set end_state */
  370. vsllink_end_state(saved_end_state);
  371. if (tap_get_end_state() != tap_get_end_state())
  372. vsllink_state_move();
  373. }
  374. static void vsllink_scan(bool ir_scan, enum scan_type type, uint8_t *buffer,
  375. int scan_size, struct scan_command *command)
  376. {
  377. tap_state_t saved_end_state;
  378. saved_end_state = tap_get_end_state();
  379. /* Move to appropriate scan state */
  380. vsllink_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
  381. if (tap_get_state() != tap_get_end_state())
  382. vsllink_state_move();
  383. vsllink_end_state(saved_end_state);
  384. /* Scan */
  385. vsllink_tap_append_scan(scan_size, buffer, command);
  386. /* Goto Pause and record position to insert tms:0 */
  387. vsllink_tap_append_step(0, 0);
  388. vsllink_tms_offset = tap_length;
  389. tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
  390. if (tap_get_state() != tap_get_end_state())
  391. vsllink_state_move();
  392. }
  393. static void vsllink_reset(int trst, int srst)
  394. {
  395. LOG_DEBUG("trst: %i, srst: %i", trst, srst);
  396. if (!srst)
  397. versaloon_interface.adaptors.gpio.config(0, GPIO_SRST, 0, GPIO_SRST, GPIO_SRST);
  398. else
  399. versaloon_interface.adaptors.gpio.config(0, GPIO_SRST, GPIO_SRST, 0, 0);
  400. if (!swd_mode) {
  401. if (!trst)
  402. versaloon_interface.adaptors.gpio.out(0, GPIO_TRST, GPIO_TRST);
  403. else
  404. versaloon_interface.adaptors.gpio.out(0, GPIO_TRST, 0);
  405. }
  406. versaloon_interface.adaptors.peripheral_commit();
  407. }
  408. COMMAND_HANDLER(vsllink_handle_usb_vid_command)
  409. {
  410. if (CMD_ARGC != 1)
  411. return ERROR_COMMAND_SYNTAX_ERROR;
  412. COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0],
  413. versaloon_interface.usb_setting.vid);
  414. return ERROR_OK;
  415. }
  416. COMMAND_HANDLER(vsllink_handle_usb_pid_command)
  417. {
  418. if (CMD_ARGC != 1)
  419. return ERROR_COMMAND_SYNTAX_ERROR;
  420. COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0],
  421. versaloon_interface.usb_setting.pid);
  422. return ERROR_OK;
  423. }
  424. COMMAND_HANDLER(vsllink_handle_usb_serial_command)
  425. {
  426. if (CMD_ARGC > 1)
  427. return ERROR_COMMAND_SYNTAX_ERROR;
  428. free(versaloon_interface.usb_setting.serialstring);
  429. if (CMD_ARGC == 1)
  430. versaloon_interface.usb_setting.serialstring = strdup(CMD_ARGV[0]);
  431. else
  432. versaloon_interface.usb_setting.serialstring = NULL;
  433. return ERROR_OK;
  434. }
  435. COMMAND_HANDLER(vsllink_handle_usb_bulkin_command)
  436. {
  437. if (CMD_ARGC != 1)
  438. return ERROR_COMMAND_SYNTAX_ERROR;
  439. COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0],
  440. versaloon_interface.usb_setting.ep_in);
  441. versaloon_interface.usb_setting.ep_in |= 0x80;
  442. return ERROR_OK;
  443. }
  444. COMMAND_HANDLER(vsllink_handle_usb_bulkout_command)
  445. {
  446. if (CMD_ARGC != 1)
  447. return ERROR_COMMAND_SYNTAX_ERROR;
  448. COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0],
  449. versaloon_interface.usb_setting.ep_out);
  450. versaloon_interface.usb_setting.ep_out &= ~0x80;
  451. return ERROR_OK;
  452. }
  453. COMMAND_HANDLER(vsllink_handle_usb_interface_command)
  454. {
  455. if (CMD_ARGC != 1)
  456. return ERROR_COMMAND_SYNTAX_ERROR;
  457. COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0],
  458. versaloon_interface.usb_setting.interface);
  459. return ERROR_OK;
  460. }
  461. /**************************************************************************
  462. * VSLLink tap functions */
  463. static void vsllink_tap_init(void)
  464. {
  465. tap_length = 0;
  466. pending_scan_results_length = 0;
  467. vsllink_tms_offset = 0;
  468. }
  469. static void vsllink_tap_ensure_pending(int scans)
  470. {
  471. int available_scans =
  472. MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
  473. if (scans > available_scans)
  474. vsllink_tap_execute();
  475. }
  476. static void vsllink_tap_append_step(int tms, int tdi)
  477. {
  478. int index_var = tap_length / 8;
  479. int bit_index = tap_length % 8;
  480. uint8_t bit = 1 << bit_index;
  481. if (tms)
  482. tms_buffer[index_var] |= bit;
  483. else
  484. tms_buffer[index_var] &= ~bit;
  485. if (tdi)
  486. tdi_buffer[index_var] |= bit;
  487. else
  488. tdi_buffer[index_var] &= ~bit;
  489. tap_length++;
  490. if (tap_buffer_size * 8 <= tap_length)
  491. vsllink_tap_execute();
  492. }
  493. static void vsllink_tap_append_scan(int length, uint8_t *buffer,
  494. struct scan_command *command)
  495. {
  496. struct pending_scan_result *pending_scan_result;
  497. int len_tmp, len_all, i;
  498. len_all = 0;
  499. while (len_all < length) {
  500. vsllink_tap_ensure_pending(1);
  501. pending_scan_result =
  502. &pending_scan_results_buffer[
  503. pending_scan_results_length];
  504. if ((length - len_all) > (tap_buffer_size * 8 - tap_length)) {
  505. /* Use all memory available
  506. vsllink_tap_append_step will commit automatically */
  507. len_tmp = tap_buffer_size * 8 - tap_length;
  508. pending_scan_result->last = false;
  509. } else {
  510. len_tmp = length - len_all;
  511. pending_scan_result->last = true;
  512. }
  513. pending_scan_result->src_offset = tap_length;
  514. pending_scan_result->dest_offset = len_all;
  515. pending_scan_result->length = len_tmp;
  516. pending_scan_result->command = command;
  517. pending_scan_result->buffer = buffer;
  518. pending_scan_results_length++;
  519. for (i = 0; i < len_tmp; i++) {
  520. vsllink_tap_append_step(((len_all + i) < length-1
  521. ? 0 : 1),
  522. (buffer[(len_all + i)/8]
  523. >> ((len_all + i)%8)) & 1);
  524. }
  525. len_all += len_tmp;
  526. }
  527. }
  528. static int vsllink_jtag_execute(void)
  529. {
  530. int i;
  531. int result;
  532. if (tap_length <= 0)
  533. return ERROR_OK;
  534. versaloon_interface.adaptors.jtag_raw.execute(0, tdi_buffer, tms_buffer,
  535. tdo_buffer, tap_length);
  536. result = versaloon_interface.adaptors.peripheral_commit();
  537. if (result == ERROR_OK) {
  538. for (i = 0; i < pending_scan_results_length; i++) {
  539. struct pending_scan_result *pending_scan_result =
  540. &pending_scan_results_buffer[i];
  541. uint8_t *buffer = pending_scan_result->buffer;
  542. int length = pending_scan_result->length;
  543. int src_first = pending_scan_result->src_offset;
  544. int dest_first = pending_scan_result->dest_offset;
  545. bool last = pending_scan_result->last;
  546. struct scan_command *command;
  547. command = pending_scan_result->command;
  548. buf_set_buf(tdo_buffer, src_first, buffer, dest_first, length);
  549. #ifdef _DEBUG_JTAG_IO_
  550. DEBUG_JTAG_IO(
  551. "JTAG scan read(%d bits, from src %d bits to dest %d bits):",
  552. length, src_first, dest_first);
  553. vsllink_debug_buffer(buffer + dest_first / 8,
  554. DIV_ROUND_UP(length, 7));
  555. #endif
  556. if (last) {
  557. if (jtag_read_buffer(buffer, command)
  558. != ERROR_OK) {
  559. vsllink_tap_init();
  560. return ERROR_JTAG_QUEUE_FAILED;
  561. }
  562. if (pending_scan_result->buffer != NULL)
  563. free(pending_scan_result->buffer);
  564. }
  565. }
  566. } else {
  567. LOG_ERROR("vsllink_jtag_execute failure");
  568. return ERROR_JTAG_QUEUE_FAILED;
  569. }
  570. vsllink_tap_init();
  571. return ERROR_OK;
  572. }
  573. static int vsllink_tap_execute(void)
  574. {
  575. if (swd_mode)
  576. return ERROR_OK;
  577. return vsllink_jtag_execute();
  578. }
  579. static int vsllink_swd_init(void)
  580. {
  581. LOG_INFO("VSLLink SWD mode enabled");
  582. swd_mode = true;
  583. return ERROR_OK;
  584. }
  585. static int_least32_t vsllink_swd_frequency(int_least32_t hz)
  586. {
  587. const int_least32_t delay2hz[] = {
  588. 1850000, 235000, 130000, 102000, 85000, 72000
  589. };
  590. if (hz > 0) {
  591. uint16_t delay = UINT16_MAX;
  592. for (uint16_t i = 0; i < ARRAY_SIZE(delay2hz); i++) {
  593. if (hz >= delay2hz[i]) {
  594. hz = delay2hz[i];
  595. delay = i;
  596. break;
  597. }
  598. }
  599. if (delay == UINT16_MAX)
  600. delay = (500000 / hz) - 1;
  601. /* Calculate retry count after a WAIT response. This will give
  602. * a retry timeout at about ~250 ms. 54 is the number of bits
  603. * found in a transaction. */
  604. uint16_t retry_count = 250 * hz / 1000 / 54;
  605. LOG_DEBUG("SWD delay: %d, retry count: %d", delay, retry_count);
  606. versaloon_interface.adaptors.swd.config(0, 2, retry_count, delay);
  607. }
  608. return hz;
  609. }
  610. static int vsllink_swd_switch_seq(enum swd_special_seq seq)
  611. {
  612. switch (seq) {
  613. case LINE_RESET:
  614. LOG_DEBUG("SWD line reset");
  615. versaloon_interface.adaptors.swd.seqout(0, swd_seq_line_reset,
  616. swd_seq_line_reset_len);
  617. break;
  618. case JTAG_TO_SWD:
  619. LOG_DEBUG("JTAG-to-SWD");
  620. versaloon_interface.adaptors.swd.seqout(0, swd_seq_jtag_to_swd,
  621. swd_seq_jtag_to_swd_len);
  622. break;
  623. case SWD_TO_JTAG:
  624. LOG_DEBUG("SWD-to-JTAG");
  625. versaloon_interface.adaptors.swd.seqout(0, swd_seq_swd_to_jtag,
  626. swd_seq_swd_to_jtag_len);
  627. break;
  628. default:
  629. LOG_ERROR("Sequence %d not supported", seq);
  630. return ERROR_FAIL;
  631. }
  632. return ERROR_OK;
  633. }
  634. static void vsllink_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
  635. {
  636. versaloon_interface.adaptors.swd.transact(0, cmd, value, NULL);
  637. }
  638. static void vsllink_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
  639. {
  640. versaloon_interface.adaptors.swd.transact(0, cmd, &value, NULL);
  641. }
  642. static int vsllink_swd_run_queue(void)
  643. {
  644. return versaloon_interface.adaptors.peripheral_commit();
  645. }
  646. /****************************************************************************
  647. * VSLLink USB low-level functions */
  648. static int vsllink_check_usb_strings(
  649. struct libusb_device_handle *usb_device_handle,
  650. struct libusb_device_descriptor *usb_desc)
  651. {
  652. char desc_string[256];
  653. int retval;
  654. if (NULL != versaloon_interface.usb_setting.serialstring) {
  655. retval = libusb_get_string_descriptor_ascii(usb_device_handle,
  656. usb_desc->iSerialNumber, (unsigned char *)desc_string,
  657. sizeof(desc_string));
  658. if (retval < 0)
  659. return ERROR_FAIL;
  660. if (strncmp(desc_string, versaloon_interface.usb_setting.serialstring,
  661. sizeof(desc_string)))
  662. return ERROR_FAIL;
  663. }
  664. retval = libusb_get_string_descriptor_ascii(usb_device_handle,
  665. usb_desc->iProduct, (unsigned char *)desc_string,
  666. sizeof(desc_string));
  667. if (retval < 0)
  668. return ERROR_FAIL;
  669. if (strstr(desc_string, "Versaloon") == NULL)
  670. return ERROR_FAIL;
  671. return ERROR_OK;
  672. }
  673. static int vsllink_usb_open(struct vsllink *vsllink)
  674. {
  675. ssize_t num_devices, i;
  676. libusb_device **usb_devices;
  677. struct libusb_device_descriptor usb_desc;
  678. struct libusb_device_handle *usb_device_handle;
  679. int retval;
  680. num_devices = libusb_get_device_list(vsllink->libusb_ctx, &usb_devices);
  681. if (num_devices <= 0)
  682. return ERROR_FAIL;
  683. for (i = 0; i < num_devices; i++) {
  684. libusb_device *device = usb_devices[i];
  685. retval = libusb_get_device_descriptor(device, &usb_desc);
  686. if (retval != 0)
  687. continue;
  688. if (usb_desc.idVendor != versaloon_interface.usb_setting.vid ||
  689. usb_desc.idProduct != versaloon_interface.usb_setting.pid)
  690. continue;
  691. retval = libusb_open(device, &usb_device_handle);
  692. if (retval != 0)
  693. continue;
  694. retval = vsllink_check_usb_strings(usb_device_handle, &usb_desc);
  695. if (ERROR_OK == retval)
  696. break;
  697. libusb_close(usb_device_handle);
  698. }
  699. libusb_free_device_list(usb_devices, 1);
  700. if (i == num_devices)
  701. return ERROR_FAIL;
  702. retval = libusb_claim_interface(usb_device_handle,
  703. versaloon_interface.usb_setting.interface);
  704. if (retval != 0) {
  705. LOG_ERROR("unable to claim interface");
  706. libusb_close(usb_device_handle);
  707. return ERROR_FAIL;
  708. }
  709. vsllink->usb_device_handle = usb_device_handle;
  710. return ERROR_OK;
  711. }
  712. static void vsllink_usb_close(struct vsllink *vsllink)
  713. {
  714. libusb_release_interface(vsllink->usb_device_handle,
  715. versaloon_interface.usb_setting.interface);
  716. libusb_close(vsllink->usb_device_handle);
  717. }
  718. #define BYTES_PER_LINE 16
  719. #if defined _DEBUG_JTAG_IO_
  720. static void vsllink_debug_buffer(uint8_t *buffer, int length)
  721. {
  722. char line[81];
  723. char s[4];
  724. int i;
  725. int j;
  726. for (i = 0; i < length; i += BYTES_PER_LINE) {
  727. snprintf(line, 5, "%04x", i);
  728. for (j = i; j < i + BYTES_PER_LINE && j < length; j++) {
  729. snprintf(s, 4, " %02x", buffer[j]);
  730. strcat(line, s);
  731. }
  732. LOG_DEBUG("%s", line);
  733. }
  734. }
  735. #endif /* _DEBUG_JTAG_IO_ */
  736. static const struct command_registration vsllink_command_handlers[] = {
  737. {
  738. .name = "vsllink_usb_vid",
  739. .handler = &vsllink_handle_usb_vid_command,
  740. .mode = COMMAND_CONFIG,
  741. },
  742. {
  743. .name = "vsllink_usb_pid",
  744. .handler = &vsllink_handle_usb_pid_command,
  745. .mode = COMMAND_CONFIG,
  746. },
  747. {
  748. .name = "vsllink_usb_serial",
  749. .handler = &vsllink_handle_usb_serial_command,
  750. .mode = COMMAND_CONFIG,
  751. },
  752. {
  753. .name = "vsllink_usb_bulkin",
  754. .handler = &vsllink_handle_usb_bulkin_command,
  755. .mode = COMMAND_CONFIG,
  756. },
  757. {
  758. .name = "vsllink_usb_bulkout",
  759. .handler = &vsllink_handle_usb_bulkout_command,
  760. .mode = COMMAND_CONFIG,
  761. },
  762. {
  763. .name = "vsllink_usb_interface",
  764. .handler = &vsllink_handle_usb_interface_command,
  765. .mode = COMMAND_CONFIG,
  766. },
  767. COMMAND_REGISTRATION_DONE
  768. };
  769. static const char * const vsllink_transports[] = {"jtag", "swd", NULL};
  770. static const struct swd_driver vsllink_swd_driver = {
  771. .init = vsllink_swd_init,
  772. .frequency = vsllink_swd_frequency,
  773. .switch_seq = vsllink_swd_switch_seq,
  774. .read_reg = vsllink_swd_read_reg,
  775. .write_reg = vsllink_swd_write_reg,
  776. .run = vsllink_swd_run_queue,
  777. };
  778. struct jtag_interface vsllink_interface = {
  779. .name = "vsllink",
  780. .supported = DEBUG_CAP_TMS_SEQ,
  781. .commands = vsllink_command_handlers,
  782. .transports = vsllink_transports,
  783. .swd = &vsllink_swd_driver,
  784. .init = vsllink_init,
  785. .quit = vsllink_quit,
  786. .khz = vsllink_khz,
  787. .speed = vsllink_speed,
  788. .speed_div = vsllink_speed_div,
  789. .execute_queue = vsllink_execute_queue,
  790. };