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.
 
 
 
 
 
 

2317 lines
66 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2011-2013 by Martin Schmoelzer *
  3. * <martin.schmoelzer@student.tuwien.ac.at> *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  17. ***************************************************************************/
  18. #ifdef HAVE_CONFIG_H
  19. #include "config.h"
  20. #endif
  21. #include <math.h>
  22. #include <jtag/interface.h>
  23. #include <jtag/commands.h>
  24. #include <target/image.h>
  25. #include <libusb.h>
  26. #include "OpenULINK/include/msgtypes.h"
  27. /** USB Vendor ID of ULINK device in unconfigured state (no firmware loaded
  28. * yet) or with OpenULINK firmware. */
  29. #define ULINK_VID 0xC251
  30. /** USB Product ID of ULINK device in unconfigured state (no firmware loaded
  31. * yet) or with OpenULINK firmware. */
  32. #define ULINK_PID 0x2710
  33. /** Address of EZ-USB CPU Control & Status register. This register can be
  34. * written by issuing a Control EP0 vendor request. */
  35. #define CPUCS_REG 0x7F92
  36. /** USB Control EP0 bRequest: "Firmware Load". */
  37. #define REQUEST_FIRMWARE_LOAD 0xA0
  38. /** Value to write into CPUCS to put EZ-USB into reset. */
  39. #define CPU_RESET 0x01
  40. /** Value to write into CPUCS to put EZ-USB out of reset. */
  41. #define CPU_START 0x00
  42. /** Base address of firmware in EZ-USB code space. */
  43. #define FIRMWARE_ADDR 0x0000
  44. /** USB interface number */
  45. #define USB_INTERFACE 0
  46. /** libusb timeout in ms */
  47. #define USB_TIMEOUT 5000
  48. /** Delay (in microseconds) to wait while EZ-USB performs ReNumeration. */
  49. #define ULINK_RENUMERATION_DELAY 1500000
  50. /** Default location of OpenULINK firmware image. */
  51. #define ULINK_FIRMWARE_FILE PKGDATADIR "/OpenULINK/ulink_firmware.hex"
  52. /** Maximum size of a single firmware section. Entire EZ-USB code space = 8kB */
  53. #define SECTION_BUFFERSIZE 8192
  54. /** Tuning of OpenOCD SCAN commands split into multiple OpenULINK commands. */
  55. #define SPLIT_SCAN_THRESHOLD 10
  56. /** ULINK hardware type */
  57. enum ulink_type {
  58. /** Original ULINK adapter, based on Cypress EZ-USB (AN2131):
  59. * Full JTAG support, no SWD support. */
  60. ULINK_1,
  61. /** Newer ULINK adapter, based on NXP LPC2148. Currently unsupported. */
  62. ULINK_2,
  63. /** Newer ULINK adapter, based on EZ-USB FX2 + FPGA. Currently unsupported. */
  64. ULINK_PRO,
  65. /** Newer ULINK adapter, possibly based on ULINK 2. Currently unsupported. */
  66. ULINK_ME
  67. };
  68. enum ulink_payload_direction {
  69. PAYLOAD_DIRECTION_OUT,
  70. PAYLOAD_DIRECTION_IN
  71. };
  72. enum ulink_delay_type {
  73. DELAY_CLOCK_TCK,
  74. DELAY_CLOCK_TMS,
  75. DELAY_SCAN_IN,
  76. DELAY_SCAN_OUT,
  77. DELAY_SCAN_IO
  78. };
  79. /**
  80. * OpenULINK command (OpenULINK command queue element).
  81. *
  82. * For the OUT direction payload, things are quite easy: Payload is stored
  83. * in a rather small array (up to 63 bytes), the payload is always allocated
  84. * by the function generating the command and freed by ulink_clear_queue().
  85. *
  86. * For the IN direction payload, things get a little bit more complicated:
  87. * The maximum IN payload size for a single command is 64 bytes. Assume that
  88. * a single OpenOCD command needs to scan 256 bytes. This results in the
  89. * generation of four OpenULINK commands. The function generating these
  90. * commands shall allocate an uint8_t[256] array. Each command's #payload_in
  91. * pointer shall point to the corresponding offset where IN data shall be
  92. * placed, while #payload_in_start shall point to the first element of the 256
  93. * byte array.
  94. * - first command: #payload_in_start + 0
  95. * - second command: #payload_in_start + 64
  96. * - third command: #payload_in_start + 128
  97. * - fourth command: #payload_in_start + 192
  98. *
  99. * The last command sets #needs_postprocessing to true.
  100. */
  101. struct ulink_cmd {
  102. uint8_t id; /**< ULINK command ID */
  103. uint8_t *payload_out; /**< OUT direction payload data */
  104. uint8_t payload_out_size; /**< OUT direction payload size for this command */
  105. uint8_t *payload_in_start; /**< Pointer to first element of IN payload array */
  106. uint8_t *payload_in; /**< Pointer where IN payload shall be stored */
  107. uint8_t payload_in_size; /**< IN direction payload size for this command */
  108. /** Indicates if this command needs post-processing */
  109. bool needs_postprocessing;
  110. /** Indicates if ulink_clear_queue() should free payload_in_start */
  111. bool free_payload_in_start;
  112. /** Pointer to corresponding OpenOCD command for post-processing */
  113. struct jtag_command *cmd_origin;
  114. struct ulink_cmd *next; /**< Pointer to next command (linked list) */
  115. };
  116. /** Describes one driver instance */
  117. struct ulink {
  118. struct libusb_context *libusb_ctx;
  119. struct libusb_device_handle *usb_device_handle;
  120. enum ulink_type type;
  121. int delay_scan_in; /**< Delay value for SCAN_IN commands */
  122. int delay_scan_out; /**< Delay value for SCAN_OUT commands */
  123. int delay_scan_io; /**< Delay value for SCAN_IO commands */
  124. int delay_clock_tck; /**< Delay value for CLOCK_TMS commands */
  125. int delay_clock_tms; /**< Delay value for CLOCK_TCK commands */
  126. int commands_in_queue; /**< Number of commands in queue */
  127. struct ulink_cmd *queue_start; /**< Pointer to first command in queue */
  128. struct ulink_cmd *queue_end; /**< Pointer to last command in queue */
  129. };
  130. /**************************** Function Prototypes *****************************/
  131. /* USB helper functions */
  132. int ulink_usb_open(struct ulink **device);
  133. int ulink_usb_close(struct ulink **device);
  134. /* ULINK MCU (Cypress EZ-USB) specific functions */
  135. int ulink_cpu_reset(struct ulink *device, unsigned char reset_bit);
  136. int ulink_load_firmware_and_renumerate(struct ulink **device, const char *filename,
  137. uint32_t delay);
  138. int ulink_load_firmware(struct ulink *device, const char *filename);
  139. int ulink_write_firmware_section(struct ulink *device,
  140. struct image *firmware_image, int section_index);
  141. /* Generic helper functions */
  142. void ulink_print_signal_states(uint8_t input_signals, uint8_t output_signals);
  143. /* OpenULINK command generation helper functions */
  144. int ulink_allocate_payload(struct ulink_cmd *ulink_cmd, int size,
  145. enum ulink_payload_direction direction);
  146. /* OpenULINK command queue helper functions */
  147. int ulink_get_queue_size(struct ulink *device,
  148. enum ulink_payload_direction direction);
  149. void ulink_clear_queue(struct ulink *device);
  150. int ulink_append_queue(struct ulink *device, struct ulink_cmd *ulink_cmd);
  151. int ulink_execute_queued_commands(struct ulink *device, int timeout);
  152. #ifdef _DEBUG_JTAG_IO_
  153. const char *ulink_cmd_id_string(uint8_t id);
  154. void ulink_print_command(struct ulink_cmd *ulink_cmd);
  155. void ulink_print_queue(struct ulink *device);
  156. static int ulink_calculate_frequency(enum ulink_delay_type type, int delay, long *f);
  157. #endif
  158. int ulink_append_scan_cmd(struct ulink *device,
  159. enum scan_type scan_type,
  160. int scan_size_bits,
  161. uint8_t *tdi,
  162. uint8_t *tdo_start,
  163. uint8_t *tdo,
  164. uint8_t tms_count_start,
  165. uint8_t tms_sequence_start,
  166. uint8_t tms_count_end,
  167. uint8_t tms_sequence_end,
  168. struct jtag_command *origin,
  169. bool postprocess);
  170. int ulink_append_clock_tms_cmd(struct ulink *device, uint8_t count,
  171. uint8_t sequence);
  172. int ulink_append_clock_tck_cmd(struct ulink *device, uint16_t count);
  173. int ulink_append_get_signals_cmd(struct ulink *device);
  174. int ulink_append_set_signals_cmd(struct ulink *device, uint8_t low,
  175. uint8_t high);
  176. int ulink_append_sleep_cmd(struct ulink *device, uint32_t us);
  177. int ulink_append_configure_tck_cmd(struct ulink *device,
  178. int delay_scan_in,
  179. int delay_scan_out,
  180. int delay_scan_io,
  181. int delay_tck,
  182. int delay_tms);
  183. int ulink_append_led_cmd(struct ulink *device, uint8_t led_state);
  184. int ulink_append_test_cmd(struct ulink *device);
  185. /* OpenULINK TCK frequency helper functions */
  186. int ulink_calculate_delay(enum ulink_delay_type type, long f, int *delay);
  187. /* Interface between OpenULINK and OpenOCD */
  188. static void ulink_set_end_state(tap_state_t endstate);
  189. int ulink_queue_statemove(struct ulink *device);
  190. int ulink_queue_scan(struct ulink *device, struct jtag_command *cmd);
  191. int ulink_queue_tlr_reset(struct ulink *device, struct jtag_command *cmd);
  192. int ulink_queue_runtest(struct ulink *device, struct jtag_command *cmd);
  193. int ulink_queue_reset(struct ulink *device, struct jtag_command *cmd);
  194. int ulink_queue_pathmove(struct ulink *device, struct jtag_command *cmd);
  195. int ulink_queue_sleep(struct ulink *device, struct jtag_command *cmd);
  196. int ulink_queue_stableclocks(struct ulink *device, struct jtag_command *cmd);
  197. int ulink_post_process_scan(struct ulink_cmd *ulink_cmd);
  198. int ulink_post_process_queue(struct ulink *device);
  199. /* JTAG driver functions (registered in struct jtag_interface) */
  200. static int ulink_execute_queue(void);
  201. static int ulink_khz(int khz, int *jtag_speed);
  202. static int ulink_speed(int speed);
  203. static int ulink_speed_div(int speed, int *khz);
  204. static int ulink_init(void);
  205. static int ulink_quit(void);
  206. /****************************** Global Variables ******************************/
  207. struct ulink *ulink_handle;
  208. /**************************** USB helper functions ****************************/
  209. /**
  210. * Opens the ULINK device and claims its USB interface.
  211. *
  212. * Currently, only the original ULINK is supported
  213. *
  214. * @param device pointer to struct ulink identifying ULINK driver instance.
  215. * @return on success: ERROR_OK
  216. * @return on failure: ERROR_FAIL
  217. */
  218. int ulink_usb_open(struct ulink **device)
  219. {
  220. ssize_t num_devices, i;
  221. bool found;
  222. libusb_device **usb_devices;
  223. struct libusb_device_descriptor usb_desc;
  224. struct libusb_device_handle *usb_device_handle;
  225. num_devices = libusb_get_device_list((*device)->libusb_ctx, &usb_devices);
  226. if (num_devices <= 0)
  227. return ERROR_FAIL;
  228. found = false;
  229. for (i = 0; i < num_devices; i++) {
  230. if (libusb_get_device_descriptor(usb_devices[i], &usb_desc) != 0)
  231. continue;
  232. else if (usb_desc.idVendor == ULINK_VID && usb_desc.idProduct == ULINK_PID) {
  233. found = true;
  234. break;
  235. }
  236. }
  237. if (!found)
  238. return ERROR_FAIL;
  239. if (libusb_open(usb_devices[i], &usb_device_handle) != 0)
  240. return ERROR_FAIL;
  241. libusb_free_device_list(usb_devices, 1);
  242. if (libusb_claim_interface(usb_device_handle, 0) != 0)
  243. return ERROR_FAIL;
  244. (*device)->usb_device_handle = usb_device_handle;
  245. (*device)->type = ULINK_1;
  246. return ERROR_OK;
  247. }
  248. /**
  249. * Releases the ULINK interface and closes the USB device handle.
  250. *
  251. * @param device pointer to struct ulink identifying ULINK driver instance.
  252. * @return on success: ERROR_OK
  253. * @return on failure: ERROR_FAIL
  254. */
  255. int ulink_usb_close(struct ulink **device)
  256. {
  257. if (libusb_release_interface((*device)->usb_device_handle, 0) != 0)
  258. return ERROR_FAIL;
  259. libusb_close((*device)->usb_device_handle);
  260. (*device)->usb_device_handle = NULL;
  261. return ERROR_OK;
  262. }
  263. /******************* ULINK CPU (EZ-USB) specific functions ********************/
  264. /**
  265. * Writes '0' or '1' to the CPUCS register, putting the EZ-USB CPU into reset
  266. * or out of reset.
  267. *
  268. * @param device pointer to struct ulink identifying ULINK driver instance.
  269. * @param reset_bit 0 to put CPU into reset, 1 to put CPU out of reset.
  270. * @return on success: ERROR_OK
  271. * @return on failure: ERROR_FAIL
  272. */
  273. int ulink_cpu_reset(struct ulink *device, unsigned char reset_bit)
  274. {
  275. int ret;
  276. ret = libusb_control_transfer(device->usb_device_handle,
  277. (LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE),
  278. REQUEST_FIRMWARE_LOAD, CPUCS_REG, 0, &reset_bit, 1, USB_TIMEOUT);
  279. /* usb_control_msg() returns the number of bytes transferred during the
  280. * DATA stage of the control transfer - must be exactly 1 in this case! */
  281. if (ret != 1)
  282. return ERROR_FAIL;
  283. return ERROR_OK;
  284. }
  285. /**
  286. * Puts the ULINK's EZ-USB microcontroller into reset state, downloads
  287. * the firmware image, resumes the microcontroller and re-enumerates
  288. * USB devices.
  289. *
  290. * @param device pointer to struct ulink identifying ULINK driver instance.
  291. * The usb_handle member will be modified during re-enumeration.
  292. * @param filename path to the Intel HEX file containing the firmware image.
  293. * @param delay the delay to wait for the device to re-enumerate.
  294. * @return on success: ERROR_OK
  295. * @return on failure: ERROR_FAIL
  296. */
  297. int ulink_load_firmware_and_renumerate(struct ulink **device,
  298. const char *filename, uint32_t delay)
  299. {
  300. int ret;
  301. /* Basic process: After downloading the firmware, the ULINK will disconnect
  302. * itself and re-connect after a short amount of time so we have to close
  303. * the handle and re-enumerate USB devices */
  304. ret = ulink_load_firmware(*device, filename);
  305. if (ret != ERROR_OK)
  306. return ret;
  307. ret = ulink_usb_close(device);
  308. if (ret != ERROR_OK)
  309. return ret;
  310. usleep(delay);
  311. ret = ulink_usb_open(device);
  312. if (ret != ERROR_OK)
  313. return ret;
  314. return ERROR_OK;
  315. }
  316. /**
  317. * Downloads a firmware image to the ULINK's EZ-USB microcontroller
  318. * over the USB bus.
  319. *
  320. * @param device pointer to struct ulink identifying ULINK driver instance.
  321. * @param filename an absolute or relative path to the Intel HEX file
  322. * containing the firmware image.
  323. * @return on success: ERROR_OK
  324. * @return on failure: ERROR_FAIL
  325. */
  326. int ulink_load_firmware(struct ulink *device, const char *filename)
  327. {
  328. struct image ulink_firmware_image;
  329. int ret, i;
  330. ret = ulink_cpu_reset(device, CPU_RESET);
  331. if (ret != ERROR_OK) {
  332. LOG_ERROR("Could not halt ULINK CPU");
  333. return ret;
  334. }
  335. ulink_firmware_image.base_address = 0;
  336. ulink_firmware_image.base_address_set = 0;
  337. ret = image_open(&ulink_firmware_image, filename, "ihex");
  338. if (ret != ERROR_OK) {
  339. LOG_ERROR("Could not load firmware image");
  340. return ret;
  341. }
  342. /* Download all sections in the image to ULINK */
  343. for (i = 0; i < ulink_firmware_image.num_sections; i++) {
  344. ret = ulink_write_firmware_section(device, &ulink_firmware_image, i);
  345. if (ret != ERROR_OK)
  346. return ret;
  347. }
  348. image_close(&ulink_firmware_image);
  349. ret = ulink_cpu_reset(device, CPU_START);
  350. if (ret != ERROR_OK) {
  351. LOG_ERROR("Could not restart ULINK CPU");
  352. return ret;
  353. }
  354. return ERROR_OK;
  355. }
  356. /**
  357. * Send one contiguous firmware section to the ULINK's EZ-USB microcontroller
  358. * over the USB bus.
  359. *
  360. * @param device pointer to struct ulink identifying ULINK driver instance.
  361. * @param firmware_image pointer to the firmware image that contains the section
  362. * which should be sent to the ULINK's EZ-USB microcontroller.
  363. * @param section_index index of the section within the firmware image.
  364. * @return on success: ERROR_OK
  365. * @return on failure: ERROR_FAIL
  366. */
  367. int ulink_write_firmware_section(struct ulink *device,
  368. struct image *firmware_image, int section_index)
  369. {
  370. uint16_t addr, size, bytes_remaining, chunk_size;
  371. uint8_t data[SECTION_BUFFERSIZE];
  372. uint8_t *data_ptr = data;
  373. size_t size_read;
  374. int ret;
  375. size = (uint16_t)firmware_image->sections[section_index].size;
  376. addr = (uint16_t)firmware_image->sections[section_index].base_address;
  377. LOG_DEBUG("section %02i at addr 0x%04x (size 0x%04x)", section_index, addr,
  378. size);
  379. /* Copy section contents to local buffer */
  380. ret = image_read_section(firmware_image, section_index, 0, size, data,
  381. &size_read);
  382. if ((ret != ERROR_OK) || (size_read != size)) {
  383. /* Propagating the return code would return '0' (misleadingly indicating
  384. * successful execution of the function) if only the size check fails. */
  385. return ERROR_FAIL;
  386. }
  387. bytes_remaining = size;
  388. /* Send section data in chunks of up to 64 bytes to ULINK */
  389. while (bytes_remaining > 0) {
  390. if (bytes_remaining > 64)
  391. chunk_size = 64;
  392. else
  393. chunk_size = bytes_remaining;
  394. ret = libusb_control_transfer(device->usb_device_handle,
  395. (LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE),
  396. REQUEST_FIRMWARE_LOAD, addr, FIRMWARE_ADDR, (unsigned char *)data_ptr,
  397. chunk_size, USB_TIMEOUT);
  398. if (ret != (int)chunk_size) {
  399. /* Abort if libusb sent less data than requested */
  400. return ERROR_FAIL;
  401. }
  402. bytes_remaining -= chunk_size;
  403. addr += chunk_size;
  404. data_ptr += chunk_size;
  405. }
  406. return ERROR_OK;
  407. }
  408. /************************** Generic helper functions **************************/
  409. /**
  410. * Print state of interesting signals via LOG_INFO().
  411. *
  412. * @param input_signals input signal states as returned by CMD_GET_SIGNALS
  413. * @param output_signals output signal states as returned by CMD_GET_SIGNALS
  414. */
  415. void ulink_print_signal_states(uint8_t input_signals, uint8_t output_signals)
  416. {
  417. LOG_INFO("ULINK signal states: TDI: %i, TDO: %i, TMS: %i, TCK: %i, TRST: %i,"
  418. " SRST: %i",
  419. (output_signals & SIGNAL_TDI ? 1 : 0),
  420. (input_signals & SIGNAL_TDO ? 1 : 0),
  421. (output_signals & SIGNAL_TMS ? 1 : 0),
  422. (output_signals & SIGNAL_TCK ? 1 : 0),
  423. (output_signals & SIGNAL_TRST ? 0 : 1), /* Inverted by hardware */
  424. (output_signals & SIGNAL_RESET ? 0 : 1)); /* Inverted by hardware */
  425. }
  426. /**************** OpenULINK command generation helper functions ***************/
  427. /**
  428. * Allocate and initialize space in memory for OpenULINK command payload.
  429. *
  430. * @param ulink_cmd pointer to command whose payload should be allocated.
  431. * @param size the amount of memory to allocate (bytes).
  432. * @param direction which payload to allocate.
  433. * @return on success: ERROR_OK
  434. * @return on failure: ERROR_FAIL
  435. */
  436. int ulink_allocate_payload(struct ulink_cmd *ulink_cmd, int size,
  437. enum ulink_payload_direction direction)
  438. {
  439. uint8_t *payload;
  440. payload = calloc(size, sizeof(uint8_t));
  441. if (payload == NULL) {
  442. LOG_ERROR("Could not allocate OpenULINK command payload: out of memory");
  443. return ERROR_FAIL;
  444. }
  445. switch (direction) {
  446. case PAYLOAD_DIRECTION_OUT:
  447. if (ulink_cmd->payload_out != NULL) {
  448. LOG_ERROR("BUG: Duplicate payload allocation for OpenULINK command");
  449. free(payload);
  450. return ERROR_FAIL;
  451. } else {
  452. ulink_cmd->payload_out = payload;
  453. ulink_cmd->payload_out_size = size;
  454. }
  455. break;
  456. case PAYLOAD_DIRECTION_IN:
  457. if (ulink_cmd->payload_in_start != NULL) {
  458. LOG_ERROR("BUG: Duplicate payload allocation for OpenULINK command");
  459. free(payload);
  460. return ERROR_FAIL;
  461. } else {
  462. ulink_cmd->payload_in_start = payload;
  463. ulink_cmd->payload_in = payload;
  464. ulink_cmd->payload_in_size = size;
  465. /* By default, free payload_in_start in ulink_clear_queue(). Commands
  466. * that do not want this behavior (e. g. split scans) must turn it off
  467. * separately! */
  468. ulink_cmd->free_payload_in_start = true;
  469. }
  470. break;
  471. }
  472. return ERROR_OK;
  473. }
  474. /****************** OpenULINK command queue helper functions ******************/
  475. /**
  476. * Get the current number of bytes in the queue, including command IDs.
  477. *
  478. * @param device pointer to struct ulink identifying ULINK driver instance.
  479. * @param direction the transfer direction for which to get byte count.
  480. * @return the number of bytes currently stored in the queue for the specified
  481. * direction.
  482. */
  483. int ulink_get_queue_size(struct ulink *device,
  484. enum ulink_payload_direction direction)
  485. {
  486. struct ulink_cmd *current = device->queue_start;
  487. int sum = 0;
  488. while (current != NULL) {
  489. switch (direction) {
  490. case PAYLOAD_DIRECTION_OUT:
  491. sum += current->payload_out_size + 1; /* + 1 byte for Command ID */
  492. break;
  493. case PAYLOAD_DIRECTION_IN:
  494. sum += current->payload_in_size;
  495. break;
  496. }
  497. current = current->next;
  498. }
  499. return sum;
  500. }
  501. /**
  502. * Clear the OpenULINK command queue.
  503. *
  504. * @param device pointer to struct ulink identifying ULINK driver instance.
  505. * @return on success: ERROR_OK
  506. * @return on failure: ERROR_FAIL
  507. */
  508. void ulink_clear_queue(struct ulink *device)
  509. {
  510. struct ulink_cmd *current = device->queue_start;
  511. struct ulink_cmd *next = NULL;
  512. while (current != NULL) {
  513. /* Save pointer to next element */
  514. next = current->next;
  515. /* Free payloads: OUT payload can be freed immediately */
  516. free(current->payload_out);
  517. current->payload_out = NULL;
  518. /* IN payload MUST be freed ONLY if no other commands use the
  519. * payload_in_start buffer */
  520. if (current->free_payload_in_start == true) {
  521. free(current->payload_in_start);
  522. current->payload_in_start = NULL;
  523. current->payload_in = NULL;
  524. }
  525. /* Free queue element */
  526. free(current);
  527. /* Proceed with next element */
  528. current = next;
  529. }
  530. device->commands_in_queue = 0;
  531. device->queue_start = NULL;
  532. device->queue_end = NULL;
  533. }
  534. /**
  535. * Add a command to the OpenULINK command queue.
  536. *
  537. * @param device pointer to struct ulink identifying ULINK driver instance.
  538. * @param ulink_cmd pointer to command that shall be appended to the OpenULINK
  539. * command queue.
  540. * @return on success: ERROR_OK
  541. * @return on failure: ERROR_FAIL
  542. */
  543. int ulink_append_queue(struct ulink *device, struct ulink_cmd *ulink_cmd)
  544. {
  545. int newsize_out, newsize_in;
  546. int ret;
  547. newsize_out = ulink_get_queue_size(device, PAYLOAD_DIRECTION_OUT) + 1
  548. + ulink_cmd->payload_out_size;
  549. newsize_in = ulink_get_queue_size(device, PAYLOAD_DIRECTION_IN)
  550. + ulink_cmd->payload_in_size;
  551. /* Check if the current command can be appended to the queue */
  552. if ((newsize_out > 64) || (newsize_in > 64)) {
  553. /* New command does not fit. Execute all commands in queue before starting
  554. * new queue with the current command as first entry. */
  555. ret = ulink_execute_queued_commands(device, USB_TIMEOUT);
  556. if (ret != ERROR_OK)
  557. return ret;
  558. ret = ulink_post_process_queue(device);
  559. if (ret != ERROR_OK)
  560. return ret;
  561. ulink_clear_queue(device);
  562. }
  563. if (device->queue_start == NULL) {
  564. /* Queue was empty */
  565. device->commands_in_queue = 1;
  566. device->queue_start = ulink_cmd;
  567. device->queue_end = ulink_cmd;
  568. } else {
  569. /* There are already commands in the queue */
  570. device->commands_in_queue++;
  571. device->queue_end->next = ulink_cmd;
  572. device->queue_end = ulink_cmd;
  573. }
  574. return ERROR_OK;
  575. }
  576. /**
  577. * Sends all queued OpenULINK commands to the ULINK for execution.
  578. *
  579. * @param device pointer to struct ulink identifying ULINK driver instance.
  580. * @return on success: ERROR_OK
  581. * @return on failure: ERROR_FAIL
  582. */
  583. int ulink_execute_queued_commands(struct ulink *device, int timeout)
  584. {
  585. struct ulink_cmd *current;
  586. int ret, i, index_out, index_in, count_out, count_in, transferred;
  587. uint8_t buffer[64];
  588. #ifdef _DEBUG_JTAG_IO_
  589. ulink_print_queue(device);
  590. #endif
  591. index_out = 0;
  592. count_out = 0;
  593. count_in = 0;
  594. for (current = device->queue_start; current; current = current->next) {
  595. /* Add command to packet */
  596. buffer[index_out] = current->id;
  597. index_out++;
  598. count_out++;
  599. for (i = 0; i < current->payload_out_size; i++)
  600. buffer[index_out + i] = current->payload_out[i];
  601. index_out += current->payload_out_size;
  602. count_in += current->payload_in_size;
  603. count_out += current->payload_out_size;
  604. }
  605. /* Send packet to ULINK */
  606. ret = libusb_bulk_transfer(device->usb_device_handle, (2 | LIBUSB_ENDPOINT_OUT),
  607. (unsigned char *)buffer, count_out, &transferred, timeout);
  608. if (ret != 0)
  609. return ERROR_FAIL;
  610. if (transferred != count_out)
  611. return ERROR_FAIL;
  612. /* Wait for response if commands contain IN payload data */
  613. if (count_in > 0) {
  614. ret = libusb_bulk_transfer(device->usb_device_handle, (2 | LIBUSB_ENDPOINT_IN),
  615. (unsigned char *)buffer, 64, &transferred, timeout);
  616. if (ret != 0)
  617. return ERROR_FAIL;
  618. if (transferred != count_in)
  619. return ERROR_FAIL;
  620. /* Write back IN payload data */
  621. index_in = 0;
  622. for (current = device->queue_start; current; current = current->next) {
  623. for (i = 0; i < current->payload_in_size; i++) {
  624. current->payload_in[i] = buffer[index_in];
  625. index_in++;
  626. }
  627. }
  628. }
  629. return ERROR_OK;
  630. }
  631. #ifdef _DEBUG_JTAG_IO_
  632. /**
  633. * Convert an OpenULINK command ID (\a id) to a human-readable string.
  634. *
  635. * @param id the OpenULINK command ID.
  636. * @return the corresponding human-readable string.
  637. */
  638. const char *ulink_cmd_id_string(uint8_t id)
  639. {
  640. switch (id) {
  641. case CMD_SCAN_IN:
  642. return "CMD_SCAN_IN";
  643. break;
  644. case CMD_SLOW_SCAN_IN:
  645. return "CMD_SLOW_SCAN_IN";
  646. break;
  647. case CMD_SCAN_OUT:
  648. return "CMD_SCAN_OUT";
  649. break;
  650. case CMD_SLOW_SCAN_OUT:
  651. return "CMD_SLOW_SCAN_OUT";
  652. break;
  653. case CMD_SCAN_IO:
  654. return "CMD_SCAN_IO";
  655. break;
  656. case CMD_SLOW_SCAN_IO:
  657. return "CMD_SLOW_SCAN_IO";
  658. break;
  659. case CMD_CLOCK_TMS:
  660. return "CMD_CLOCK_TMS";
  661. break;
  662. case CMD_SLOW_CLOCK_TMS:
  663. return "CMD_SLOW_CLOCK_TMS";
  664. break;
  665. case CMD_CLOCK_TCK:
  666. return "CMD_CLOCK_TCK";
  667. break;
  668. case CMD_SLOW_CLOCK_TCK:
  669. return "CMD_SLOW_CLOCK_TCK";
  670. break;
  671. case CMD_SLEEP_US:
  672. return "CMD_SLEEP_US";
  673. break;
  674. case CMD_SLEEP_MS:
  675. return "CMD_SLEEP_MS";
  676. break;
  677. case CMD_GET_SIGNALS:
  678. return "CMD_GET_SIGNALS";
  679. break;
  680. case CMD_SET_SIGNALS:
  681. return "CMD_SET_SIGNALS";
  682. break;
  683. case CMD_CONFIGURE_TCK_FREQ:
  684. return "CMD_CONFIGURE_TCK_FREQ";
  685. break;
  686. case CMD_SET_LEDS:
  687. return "CMD_SET_LEDS";
  688. break;
  689. case CMD_TEST:
  690. return "CMD_TEST";
  691. break;
  692. default:
  693. return "CMD_UNKNOWN";
  694. break;
  695. }
  696. }
  697. /**
  698. * Print one OpenULINK command to stdout.
  699. *
  700. * @param ulink_cmd pointer to OpenULINK command.
  701. */
  702. void ulink_print_command(struct ulink_cmd *ulink_cmd)
  703. {
  704. int i;
  705. printf(" %-22s | OUT size = %i, bytes = 0x",
  706. ulink_cmd_id_string(ulink_cmd->id), ulink_cmd->payload_out_size);
  707. for (i = 0; i < ulink_cmd->payload_out_size; i++)
  708. printf("%02X ", ulink_cmd->payload_out[i]);
  709. printf("\n | IN size = %i\n",
  710. ulink_cmd->payload_in_size);
  711. }
  712. /**
  713. * Print the OpenULINK command queue to stdout.
  714. *
  715. * @param device pointer to struct ulink identifying ULINK driver instance.
  716. */
  717. void ulink_print_queue(struct ulink *device)
  718. {
  719. struct ulink_cmd *current;
  720. printf("OpenULINK command queue:\n");
  721. for (current = device->queue_start; current; current = current->next)
  722. ulink_print_command(current);
  723. }
  724. #endif /* _DEBUG_JTAG_IO_ */
  725. /**
  726. * Perform JTAG scan
  727. *
  728. * Creates and appends a JTAG scan command to the OpenULINK command queue.
  729. * A JTAG scan consists of three steps:
  730. * - Move to the desired SHIFT state, depending on scan type (IR/DR scan).
  731. * - Shift TDI data into the JTAG chain, optionally reading the TDO pin.
  732. * - Move to the desired end state.
  733. *
  734. * @param device pointer to struct ulink identifying ULINK driver instance.
  735. * @param scan_type the type of the scan (IN, OUT, IO (bidirectional)).
  736. * @param scan_size_bits number of bits to shift into the JTAG chain.
  737. * @param tdi pointer to array containing TDI data.
  738. * @param tdo_start pointer to first element of array where TDO data shall be
  739. * stored. See #ulink_cmd for details.
  740. * @param tdo pointer to array where TDO data shall be stored
  741. * @param tms_count_start number of TMS state transitions to perform BEFORE
  742. * shifting data into the JTAG chain.
  743. * @param tms_sequence_start sequence of TMS state transitions that will be
  744. * performed BEFORE shifting data into the JTAG chain.
  745. * @param tms_count_end number of TMS state transitions to perform AFTER
  746. * shifting data into the JTAG chain.
  747. * @param tms_sequence_end sequence of TMS state transitions that will be
  748. * performed AFTER shifting data into the JTAG chain.
  749. * @param origin pointer to OpenOCD command that generated this scan command.
  750. * @param postprocess whether this command needs to be post-processed after
  751. * execution.
  752. * @return on success: ERROR_OK
  753. * @return on failure: ERROR_FAIL
  754. */
  755. int ulink_append_scan_cmd(struct ulink *device, enum scan_type scan_type,
  756. int scan_size_bits, uint8_t *tdi, uint8_t *tdo_start, uint8_t *tdo,
  757. uint8_t tms_count_start, uint8_t tms_sequence_start, uint8_t tms_count_end,
  758. uint8_t tms_sequence_end, struct jtag_command *origin, bool postprocess)
  759. {
  760. struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd));
  761. int ret, i, scan_size_bytes;
  762. uint8_t bits_last_byte;
  763. if (cmd == NULL)
  764. return ERROR_FAIL;
  765. /* Check size of command. USB buffer can hold 64 bytes, 1 byte is command ID,
  766. * 5 bytes are setup data -> 58 remaining payload bytes for TDI data */
  767. if (scan_size_bits > (58 * 8)) {
  768. LOG_ERROR("BUG: Tried to create CMD_SCAN_IO OpenULINK command with too"
  769. " large payload");
  770. free(cmd);
  771. return ERROR_FAIL;
  772. }
  773. scan_size_bytes = DIV_ROUND_UP(scan_size_bits, 8);
  774. bits_last_byte = scan_size_bits % 8;
  775. if (bits_last_byte == 0)
  776. bits_last_byte = 8;
  777. /* Allocate out_payload depending on scan type */
  778. switch (scan_type) {
  779. case SCAN_IN:
  780. if (device->delay_scan_in < 0)
  781. cmd->id = CMD_SCAN_IN;
  782. else
  783. cmd->id = CMD_SLOW_SCAN_IN;
  784. ret = ulink_allocate_payload(cmd, 5, PAYLOAD_DIRECTION_OUT);
  785. break;
  786. case SCAN_OUT:
  787. if (device->delay_scan_out < 0)
  788. cmd->id = CMD_SCAN_OUT;
  789. else
  790. cmd->id = CMD_SLOW_SCAN_OUT;
  791. ret = ulink_allocate_payload(cmd, scan_size_bytes + 5, PAYLOAD_DIRECTION_OUT);
  792. break;
  793. case SCAN_IO:
  794. if (device->delay_scan_io < 0)
  795. cmd->id = CMD_SCAN_IO;
  796. else
  797. cmd->id = CMD_SLOW_SCAN_IO;
  798. ret = ulink_allocate_payload(cmd, scan_size_bytes + 5, PAYLOAD_DIRECTION_OUT);
  799. break;
  800. default:
  801. LOG_ERROR("BUG: ulink_append_scan_cmd() encountered an unknown scan type");
  802. ret = ERROR_FAIL;
  803. break;
  804. }
  805. if (ret != ERROR_OK) {
  806. free(cmd);
  807. return ret;
  808. }
  809. /* Build payload_out that is common to all scan types */
  810. cmd->payload_out[0] = scan_size_bytes & 0xFF;
  811. cmd->payload_out[1] = bits_last_byte & 0xFF;
  812. cmd->payload_out[2] = ((tms_count_start & 0x0F) << 4) | (tms_count_end & 0x0F);
  813. cmd->payload_out[3] = tms_sequence_start;
  814. cmd->payload_out[4] = tms_sequence_end;
  815. /* Setup payload_out for types with OUT transfer */
  816. if ((scan_type == SCAN_OUT) || (scan_type == SCAN_IO)) {
  817. for (i = 0; i < scan_size_bytes; i++)
  818. cmd->payload_out[i + 5] = tdi[i];
  819. }
  820. /* Setup payload_in pointers for types with IN transfer */
  821. if ((scan_type == SCAN_IN) || (scan_type == SCAN_IO)) {
  822. cmd->payload_in_start = tdo_start;
  823. cmd->payload_in = tdo;
  824. cmd->payload_in_size = scan_size_bytes;
  825. }
  826. cmd->needs_postprocessing = postprocess;
  827. cmd->cmd_origin = origin;
  828. /* For scan commands, we free payload_in_start only when the command is
  829. * the last in a series of split commands or a stand-alone command */
  830. cmd->free_payload_in_start = postprocess;
  831. return ulink_append_queue(device, cmd);
  832. }
  833. /**
  834. * Perform TAP state transitions
  835. *
  836. * @param device pointer to struct ulink identifying ULINK driver instance.
  837. * @param count defines the number of TCK clock cycles generated (up to 8).
  838. * @param sequence defines the TMS pin levels for each state transition. The
  839. * Least-Significant Bit is read first.
  840. * @return on success: ERROR_OK
  841. * @return on failure: ERROR_FAIL
  842. */
  843. int ulink_append_clock_tms_cmd(struct ulink *device, uint8_t count,
  844. uint8_t sequence)
  845. {
  846. struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd));
  847. int ret;
  848. if (cmd == NULL)
  849. return ERROR_FAIL;
  850. if (device->delay_clock_tms < 0)
  851. cmd->id = CMD_CLOCK_TMS;
  852. else
  853. cmd->id = CMD_SLOW_CLOCK_TMS;
  854. /* CMD_CLOCK_TMS has two OUT payload bytes and zero IN payload bytes */
  855. ret = ulink_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_OUT);
  856. if (ret != ERROR_OK) {
  857. free(cmd);
  858. return ret;
  859. }
  860. cmd->payload_out[0] = count;
  861. cmd->payload_out[1] = sequence;
  862. return ulink_append_queue(device, cmd);
  863. }
  864. /**
  865. * Generate a defined amount of TCK clock cycles
  866. *
  867. * All other JTAG signals are left unchanged.
  868. *
  869. * @param device pointer to struct ulink identifying ULINK driver instance.
  870. * @param count the number of TCK clock cycles to generate.
  871. * @return on success: ERROR_OK
  872. * @return on failure: ERROR_FAIL
  873. */
  874. int ulink_append_clock_tck_cmd(struct ulink *device, uint16_t count)
  875. {
  876. struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd));
  877. int ret;
  878. if (cmd == NULL)
  879. return ERROR_FAIL;
  880. if (device->delay_clock_tck < 0)
  881. cmd->id = CMD_CLOCK_TCK;
  882. else
  883. cmd->id = CMD_SLOW_CLOCK_TCK;
  884. /* CMD_CLOCK_TCK has two OUT payload bytes and zero IN payload bytes */
  885. ret = ulink_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_OUT);
  886. if (ret != ERROR_OK) {
  887. free(cmd);
  888. return ret;
  889. }
  890. cmd->payload_out[0] = count & 0xff;
  891. cmd->payload_out[1] = (count >> 8) & 0xff;
  892. return ulink_append_queue(device, cmd);
  893. }
  894. /**
  895. * Read JTAG signals.
  896. *
  897. * @param device pointer to struct ulink identifying ULINK driver instance.
  898. * @return on success: ERROR_OK
  899. * @return on failure: ERROR_FAIL
  900. */
  901. int ulink_append_get_signals_cmd(struct ulink *device)
  902. {
  903. struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd));
  904. int ret;
  905. if (cmd == NULL)
  906. return ERROR_FAIL;
  907. cmd->id = CMD_GET_SIGNALS;
  908. cmd->needs_postprocessing = true;
  909. /* CMD_GET_SIGNALS has two IN payload bytes */
  910. ret = ulink_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_IN);
  911. if (ret != ERROR_OK) {
  912. free(cmd);
  913. return ret;
  914. }
  915. return ulink_append_queue(device, cmd);
  916. }
  917. /**
  918. * Arbitrarily set JTAG output signals.
  919. *
  920. * @param device pointer to struct ulink identifying ULINK driver instance.
  921. * @param low defines which signals will be de-asserted. Each bit corresponds
  922. * to a JTAG signal:
  923. * - SIGNAL_TDI
  924. * - SIGNAL_TMS
  925. * - SIGNAL_TCK
  926. * - SIGNAL_TRST
  927. * - SIGNAL_BRKIN
  928. * - SIGNAL_RESET
  929. * - SIGNAL_OCDSE
  930. * @param high defines which signals will be asserted.
  931. * @return on success: ERROR_OK
  932. * @return on failure: ERROR_FAIL
  933. */
  934. int ulink_append_set_signals_cmd(struct ulink *device, uint8_t low,
  935. uint8_t high)
  936. {
  937. struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd));
  938. int ret;
  939. if (cmd == NULL)
  940. return ERROR_FAIL;
  941. cmd->id = CMD_SET_SIGNALS;
  942. /* CMD_SET_SIGNALS has two OUT payload bytes and zero IN payload bytes */
  943. ret = ulink_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_OUT);
  944. if (ret != ERROR_OK) {
  945. free(cmd);
  946. return ret;
  947. }
  948. cmd->payload_out[0] = low;
  949. cmd->payload_out[1] = high;
  950. return ulink_append_queue(device, cmd);
  951. }
  952. /**
  953. * Sleep for a pre-defined number of microseconds
  954. *
  955. * @param device pointer to struct ulink identifying ULINK driver instance.
  956. * @param us the number microseconds to sleep.
  957. * @return on success: ERROR_OK
  958. * @return on failure: ERROR_FAIL
  959. */
  960. int ulink_append_sleep_cmd(struct ulink *device, uint32_t us)
  961. {
  962. struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd));
  963. int ret;
  964. if (cmd == NULL)
  965. return ERROR_FAIL;
  966. cmd->id = CMD_SLEEP_US;
  967. /* CMD_SLEEP_US has two OUT payload bytes and zero IN payload bytes */
  968. ret = ulink_allocate_payload(cmd, 2, PAYLOAD_DIRECTION_OUT);
  969. if (ret != ERROR_OK) {
  970. free(cmd);
  971. return ret;
  972. }
  973. cmd->payload_out[0] = us & 0x00ff;
  974. cmd->payload_out[1] = (us >> 8) & 0x00ff;
  975. return ulink_append_queue(device, cmd);
  976. }
  977. /**
  978. * Set TCK delay counters
  979. *
  980. * @param device pointer to struct ulink identifying ULINK driver instance.
  981. * @param delay_scan_in delay count top value in jtag_slow_scan_in() function.
  982. * @param delay_scan_out delay count top value in jtag_slow_scan_out() function.
  983. * @param delay_scan_io delay count top value in jtag_slow_scan_io() function.
  984. * @param delay_tck delay count top value in jtag_clock_tck() function.
  985. * @param delay_tms delay count top value in jtag_slow_clock_tms() function.
  986. * @return on success: ERROR_OK
  987. * @return on failure: ERROR_FAIL
  988. */
  989. int ulink_append_configure_tck_cmd(struct ulink *device, int delay_scan_in,
  990. int delay_scan_out, int delay_scan_io, int delay_tck, int delay_tms)
  991. {
  992. struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd));
  993. int ret;
  994. if (cmd == NULL)
  995. return ERROR_FAIL;
  996. cmd->id = CMD_CONFIGURE_TCK_FREQ;
  997. /* CMD_CONFIGURE_TCK_FREQ has five OUT payload bytes and zero
  998. * IN payload bytes */
  999. ret = ulink_allocate_payload(cmd, 5, PAYLOAD_DIRECTION_OUT);
  1000. if (ret != ERROR_OK) {
  1001. free(cmd);
  1002. return ret;
  1003. }
  1004. if (delay_scan_in < 0)
  1005. cmd->payload_out[0] = 0;
  1006. else
  1007. cmd->payload_out[0] = (uint8_t)delay_scan_in;
  1008. if (delay_scan_out < 0)
  1009. cmd->payload_out[1] = 0;
  1010. else
  1011. cmd->payload_out[1] = (uint8_t)delay_scan_out;
  1012. if (delay_scan_io < 0)
  1013. cmd->payload_out[2] = 0;
  1014. else
  1015. cmd->payload_out[2] = (uint8_t)delay_scan_io;
  1016. if (delay_tck < 0)
  1017. cmd->payload_out[3] = 0;
  1018. else
  1019. cmd->payload_out[3] = (uint8_t)delay_tck;
  1020. if (delay_tms < 0)
  1021. cmd->payload_out[4] = 0;
  1022. else
  1023. cmd->payload_out[4] = (uint8_t)delay_tms;
  1024. return ulink_append_queue(device, cmd);
  1025. }
  1026. /**
  1027. * Turn on/off ULINK LEDs.
  1028. *
  1029. * @param device pointer to struct ulink identifying ULINK driver instance.
  1030. * @param led_state which LED(s) to turn on or off. The following bits
  1031. * influence the LEDS:
  1032. * - Bit 0: Turn COM LED on
  1033. * - Bit 1: Turn RUN LED on
  1034. * - Bit 2: Turn COM LED off
  1035. * - Bit 3: Turn RUN LED off
  1036. * If both the on-bit and the off-bit for the same LED is set, the LED is
  1037. * turned off.
  1038. * @return on success: ERROR_OK
  1039. * @return on failure: ERROR_FAIL
  1040. */
  1041. int ulink_append_led_cmd(struct ulink *device, uint8_t led_state)
  1042. {
  1043. struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd));
  1044. int ret;
  1045. if (cmd == NULL)
  1046. return ERROR_FAIL;
  1047. cmd->id = CMD_SET_LEDS;
  1048. /* CMD_SET_LEDS has one OUT payload byte and zero IN payload bytes */
  1049. ret = ulink_allocate_payload(cmd, 1, PAYLOAD_DIRECTION_OUT);
  1050. if (ret != ERROR_OK) {
  1051. free(cmd);
  1052. return ret;
  1053. }
  1054. cmd->payload_out[0] = led_state;
  1055. return ulink_append_queue(device, cmd);
  1056. }
  1057. /**
  1058. * Test command. Used to check if the ULINK device is ready to accept new
  1059. * commands.
  1060. *
  1061. * @param device pointer to struct ulink identifying ULINK driver instance.
  1062. * @return on success: ERROR_OK
  1063. * @return on failure: ERROR_FAIL
  1064. */
  1065. int ulink_append_test_cmd(struct ulink *device)
  1066. {
  1067. struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd));
  1068. int ret;
  1069. if (cmd == NULL)
  1070. return ERROR_FAIL;
  1071. cmd->id = CMD_TEST;
  1072. /* CMD_TEST has one OUT payload byte and zero IN payload bytes */
  1073. ret = ulink_allocate_payload(cmd, 1, PAYLOAD_DIRECTION_OUT);
  1074. if (ret != ERROR_OK) {
  1075. free(cmd);
  1076. return ret;
  1077. }
  1078. cmd->payload_out[0] = 0xAA;
  1079. return ulink_append_queue(device, cmd);
  1080. }
  1081. /****************** OpenULINK TCK frequency helper functions ******************/
  1082. /**
  1083. * Calculate delay values for a given TCK frequency.
  1084. *
  1085. * The OpenULINK firmware uses five different speed values for different
  1086. * commands. These speed values are calculated in these functions.
  1087. *
  1088. * The five different commands which support variable TCK frequency are
  1089. * implemented twice in the firmware:
  1090. * 1. Maximum possible frequency without any artificial delay
  1091. * 2. Variable frequency with artificial linear delay loop
  1092. *
  1093. * To set the ULINK to maximum frequency, it is only neccessary to use the
  1094. * corresponding command IDs. To set the ULINK to a lower frequency, the
  1095. * delay loop top values have to be calculated first. Then, a
  1096. * CMD_CONFIGURE_TCK_FREQ command needs to be sent to the ULINK device.
  1097. *
  1098. * The delay values are described by linear equations:
  1099. * t = k * x + d
  1100. * (t = period, k = constant, x = delay value, d = constant)
  1101. *
  1102. * Thus, the delay can be calculated as in the following equation:
  1103. * x = (t - d) / k
  1104. *
  1105. * The constants in these equations have been determined and validated by
  1106. * measuring the frequency resulting from different delay values.
  1107. *
  1108. * @param type for which command to calculate the delay value.
  1109. * @param f TCK frequency for which to calculate the delay value in Hz.
  1110. * @param delay where to store resulting delay value.
  1111. * @return on success: ERROR_OK
  1112. * @return on failure: ERROR_FAIL
  1113. */
  1114. int ulink_calculate_delay(enum ulink_delay_type type, long f, int *delay)
  1115. {
  1116. float t, x, x_ceil;
  1117. /* Calculate period of requested TCK frequency */
  1118. t = 1.0 / (float)(f);
  1119. switch (type) {
  1120. case DELAY_CLOCK_TCK:
  1121. x = (t - (float)(6E-6)) / (float)(4E-6);
  1122. break;
  1123. case DELAY_CLOCK_TMS:
  1124. x = (t - (float)(8.5E-6)) / (float)(4E-6);
  1125. break;
  1126. case DELAY_SCAN_IN:
  1127. x = (t - (float)(8.8308E-6)) / (float)(4E-6);
  1128. break;
  1129. case DELAY_SCAN_OUT:
  1130. x = (t - (float)(1.0527E-5)) / (float)(4E-6);
  1131. break;
  1132. case DELAY_SCAN_IO:
  1133. x = (t - (float)(1.3132E-5)) / (float)(4E-6);
  1134. break;
  1135. default:
  1136. return ERROR_FAIL;
  1137. break;
  1138. }
  1139. /* Check if the delay value is negative. This happens when a frequency is
  1140. * requested that is too high for the delay loop implementation. In this
  1141. * case, set delay value to zero. */
  1142. if (x < 0)
  1143. x = 0;
  1144. /* We need to convert the exact delay value to an integer. Therefore, we
  1145. * round the exact value UP to ensure that the resulting frequency is NOT
  1146. * higher than the requested frequency. */
  1147. x_ceil = ceilf(x);
  1148. /* Check if the value is within limits */
  1149. if (x_ceil > 255)
  1150. return ERROR_FAIL;
  1151. *delay = (int)x_ceil;
  1152. return ERROR_OK;
  1153. }
  1154. #ifdef _DEBUG_JTAG_IO_
  1155. /**
  1156. * Calculate frequency for a given delay value.
  1157. *
  1158. * Similar to the #ulink_calculate_delay function, this function calculates the
  1159. * TCK frequency for a given delay value by using linear equations of the form:
  1160. * t = k * x + d
  1161. * (t = period, k = constant, x = delay value, d = constant)
  1162. *
  1163. * @param type for which command to calculate the delay value.
  1164. * @param delay delay value for which to calculate the resulting TCK frequency.
  1165. * @param f where to store the resulting TCK frequency.
  1166. * @return on success: ERROR_OK
  1167. * @return on failure: ERROR_FAIL
  1168. */
  1169. static int ulink_calculate_frequency(enum ulink_delay_type type, int delay, long *f)
  1170. {
  1171. float t, f_float, f_rounded;
  1172. if (delay > 255)
  1173. return ERROR_FAIL;
  1174. switch (type) {
  1175. case DELAY_CLOCK_TCK:
  1176. if (delay < 0)
  1177. t = (float)(2.666E-6);
  1178. else
  1179. t = (float)(4E-6) * (float)(delay) + (float)(6E-6);
  1180. break;
  1181. case DELAY_CLOCK_TMS:
  1182. if (delay < 0)
  1183. t = (float)(5.666E-6);
  1184. else
  1185. t = (float)(4E-6) * (float)(delay) + (float)(8.5E-6);
  1186. break;
  1187. case DELAY_SCAN_IN:
  1188. if (delay < 0)
  1189. t = (float)(5.5E-6);
  1190. else
  1191. t = (float)(4E-6) * (float)(delay) + (float)(8.8308E-6);
  1192. break;
  1193. case DELAY_SCAN_OUT:
  1194. if (delay < 0)
  1195. t = (float)(7.0E-6);
  1196. else
  1197. t = (float)(4E-6) * (float)(delay) + (float)(1.0527E-5);
  1198. break;
  1199. case DELAY_SCAN_IO:
  1200. if (delay < 0)
  1201. t = (float)(9.926E-6);
  1202. else
  1203. t = (float)(4E-6) * (float)(delay) + (float)(1.3132E-5);
  1204. break;
  1205. default:
  1206. return ERROR_FAIL;
  1207. break;
  1208. }
  1209. f_float = 1.0 / t;
  1210. f_rounded = roundf(f_float);
  1211. *f = (long)f_rounded;
  1212. return ERROR_OK;
  1213. }
  1214. #endif
  1215. /******************* Interface between OpenULINK and OpenOCD ******************/
  1216. /**
  1217. * Sets the end state follower (see interface.h) if \a endstate is a stable
  1218. * state.
  1219. *
  1220. * @param endstate the state the end state follower should be set to.
  1221. */
  1222. static void ulink_set_end_state(tap_state_t endstate)
  1223. {
  1224. if (tap_is_state_stable(endstate))
  1225. tap_set_end_state(endstate);
  1226. else {
  1227. LOG_ERROR("BUG: %s is not a valid end state", tap_state_name(endstate));
  1228. exit(EXIT_FAILURE);
  1229. }
  1230. }
  1231. /**
  1232. * Move from the current TAP state to the current TAP end state.
  1233. *
  1234. * @param device pointer to struct ulink identifying ULINK driver instance.
  1235. * @return on success: ERROR_OK
  1236. * @return on failure: ERROR_FAIL
  1237. */
  1238. int ulink_queue_statemove(struct ulink *device)
  1239. {
  1240. uint8_t tms_sequence, tms_count;
  1241. int ret;
  1242. if (tap_get_state() == tap_get_end_state()) {
  1243. /* Do nothing if we are already there */
  1244. return ERROR_OK;
  1245. }
  1246. tms_sequence = tap_get_tms_path(tap_get_state(), tap_get_end_state());
  1247. tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
  1248. ret = ulink_append_clock_tms_cmd(device, tms_count, tms_sequence);
  1249. if (ret == ERROR_OK)
  1250. tap_set_state(tap_get_end_state());
  1251. return ret;
  1252. }
  1253. /**
  1254. * Perform a scan operation on a JTAG register.
  1255. *
  1256. * @param device pointer to struct ulink identifying ULINK driver instance.
  1257. * @param cmd pointer to the command that shall be executed.
  1258. * @return on success: ERROR_OK
  1259. * @return on failure: ERROR_FAIL
  1260. */
  1261. int ulink_queue_scan(struct ulink *device, struct jtag_command *cmd)
  1262. {
  1263. uint32_t scan_size_bits, scan_size_bytes, bits_last_scan;
  1264. uint32_t scans_max_payload, bytecount;
  1265. uint8_t *tdi_buffer_start = NULL, *tdi_buffer = NULL;
  1266. uint8_t *tdo_buffer_start = NULL, *tdo_buffer = NULL;
  1267. uint8_t first_tms_count, first_tms_sequence;
  1268. uint8_t last_tms_count, last_tms_sequence;
  1269. uint8_t tms_count_pause, tms_sequence_pause;
  1270. uint8_t tms_count_resume, tms_sequence_resume;
  1271. uint8_t tms_count_start, tms_sequence_start;
  1272. uint8_t tms_count_end, tms_sequence_end;
  1273. enum scan_type type;
  1274. int ret;
  1275. /* Determine scan size */
  1276. scan_size_bits = jtag_scan_size(cmd->cmd.scan);
  1277. scan_size_bytes = DIV_ROUND_UP(scan_size_bits, 8);
  1278. /* Determine scan type (IN/OUT/IO) */
  1279. type = jtag_scan_type(cmd->cmd.scan);
  1280. /* Determine number of scan commands with maximum payload */
  1281. scans_max_payload = scan_size_bytes / 58;
  1282. /* Determine size of last shift command */
  1283. bits_last_scan = scan_size_bits - (scans_max_payload * 58 * 8);
  1284. /* Allocate TDO buffer if required */
  1285. if ((type == SCAN_IN) || (type == SCAN_IO)) {
  1286. tdo_buffer_start = calloc(sizeof(uint8_t), scan_size_bytes);
  1287. if (tdo_buffer_start == NULL)
  1288. return ERROR_FAIL;
  1289. tdo_buffer = tdo_buffer_start;
  1290. }
  1291. /* Fill TDI buffer if required */
  1292. if ((type == SCAN_OUT) || (type == SCAN_IO)) {
  1293. jtag_build_buffer(cmd->cmd.scan, &tdi_buffer_start);
  1294. tdi_buffer = tdi_buffer_start;
  1295. }
  1296. /* Get TAP state transitions */
  1297. if (cmd->cmd.scan->ir_scan) {
  1298. ulink_set_end_state(TAP_IRSHIFT);
  1299. first_tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
  1300. first_tms_sequence = tap_get_tms_path(tap_get_state(), tap_get_end_state());
  1301. tap_set_state(TAP_IRSHIFT);
  1302. tap_set_end_state(cmd->cmd.scan->end_state);
  1303. last_tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
  1304. last_tms_sequence = tap_get_tms_path(tap_get_state(), tap_get_end_state());
  1305. /* TAP state transitions for split scans */
  1306. tms_count_pause = tap_get_tms_path_len(TAP_IRSHIFT, TAP_IRPAUSE);
  1307. tms_sequence_pause = tap_get_tms_path(TAP_IRSHIFT, TAP_IRPAUSE);
  1308. tms_count_resume = tap_get_tms_path_len(TAP_IRPAUSE, TAP_IRSHIFT);
  1309. tms_sequence_resume = tap_get_tms_path(TAP_IRPAUSE, TAP_IRSHIFT);
  1310. } else {
  1311. ulink_set_end_state(TAP_DRSHIFT);
  1312. first_tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
  1313. first_tms_sequence = tap_get_tms_path(tap_get_state(), tap_get_end_state());
  1314. tap_set_state(TAP_DRSHIFT);
  1315. tap_set_end_state(cmd->cmd.scan->end_state);
  1316. last_tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
  1317. last_tms_sequence = tap_get_tms_path(tap_get_state(), tap_get_end_state());
  1318. /* TAP state transitions for split scans */
  1319. tms_count_pause = tap_get_tms_path_len(TAP_DRSHIFT, TAP_DRPAUSE);
  1320. tms_sequence_pause = tap_get_tms_path(TAP_DRSHIFT, TAP_DRPAUSE);
  1321. tms_count_resume = tap_get_tms_path_len(TAP_DRPAUSE, TAP_DRSHIFT);
  1322. tms_sequence_resume = tap_get_tms_path(TAP_DRPAUSE, TAP_DRSHIFT);
  1323. }
  1324. /* Generate scan commands */
  1325. bytecount = scan_size_bytes;
  1326. while (bytecount > 0) {
  1327. if (bytecount == scan_size_bytes) {
  1328. /* This is the first scan */
  1329. tms_count_start = first_tms_count;
  1330. tms_sequence_start = first_tms_sequence;
  1331. } else {
  1332. /* Resume from previous scan */
  1333. tms_count_start = tms_count_resume;
  1334. tms_sequence_start = tms_sequence_resume;
  1335. }
  1336. if (bytecount > 58) { /* Full scan, at least one scan will follow */
  1337. tms_count_end = tms_count_pause;
  1338. tms_sequence_end = tms_sequence_pause;
  1339. ret = ulink_append_scan_cmd(device,
  1340. type,
  1341. 58 * 8,
  1342. tdi_buffer,
  1343. tdo_buffer_start,
  1344. tdo_buffer,
  1345. tms_count_start,
  1346. tms_sequence_start,
  1347. tms_count_end,
  1348. tms_sequence_end,
  1349. cmd,
  1350. false);
  1351. bytecount -= 58;
  1352. /* Update TDI and TDO buffer pointers */
  1353. if (tdi_buffer_start != NULL)
  1354. tdi_buffer += 58;
  1355. if (tdo_buffer_start != NULL)
  1356. tdo_buffer += 58;
  1357. } else if (bytecount == 58) { /* Full scan, no further scans */
  1358. tms_count_end = last_tms_count;
  1359. tms_sequence_end = last_tms_sequence;
  1360. ret = ulink_append_scan_cmd(device,
  1361. type,
  1362. 58 * 8,
  1363. tdi_buffer,
  1364. tdo_buffer_start,
  1365. tdo_buffer,
  1366. tms_count_start,
  1367. tms_sequence_start,
  1368. tms_count_end,
  1369. tms_sequence_end,
  1370. cmd,
  1371. true);
  1372. bytecount = 0;
  1373. } else {/* Scan with less than maximum payload, no further scans */
  1374. tms_count_end = last_tms_count;
  1375. tms_sequence_end = last_tms_sequence;
  1376. ret = ulink_append_scan_cmd(device,
  1377. type,
  1378. bits_last_scan,
  1379. tdi_buffer,
  1380. tdo_buffer_start,
  1381. tdo_buffer,
  1382. tms_count_start,
  1383. tms_sequence_start,
  1384. tms_count_end,
  1385. tms_sequence_end,
  1386. cmd,
  1387. true);
  1388. bytecount = 0;
  1389. }
  1390. if (ret != ERROR_OK) {
  1391. free(tdi_buffer_start);
  1392. return ret;
  1393. }
  1394. }
  1395. free(tdi_buffer_start);
  1396. /* Set current state to the end state requested by the command */
  1397. tap_set_state(cmd->cmd.scan->end_state);
  1398. return ERROR_OK;
  1399. }
  1400. /**
  1401. * Move the TAP into the Test Logic Reset state.
  1402. *
  1403. * @param device pointer to struct ulink identifying ULINK driver instance.
  1404. * @param cmd pointer to the command that shall be executed.
  1405. * @return on success: ERROR_OK
  1406. * @return on failure: ERROR_FAIL
  1407. */
  1408. int ulink_queue_tlr_reset(struct ulink *device, struct jtag_command *cmd)
  1409. {
  1410. int ret;
  1411. ret = ulink_append_clock_tms_cmd(device, 5, 0xff);
  1412. if (ret == ERROR_OK)
  1413. tap_set_state(TAP_RESET);
  1414. return ret;
  1415. }
  1416. /**
  1417. * Run Test.
  1418. *
  1419. * Generate TCK clock cycles while remaining
  1420. * in the Run-Test/Idle state.
  1421. *
  1422. * @param device pointer to struct ulink identifying ULINK driver instance.
  1423. * @param cmd pointer to the command that shall be executed.
  1424. * @return on success: ERROR_OK
  1425. * @return on failure: ERROR_FAIL
  1426. */
  1427. int ulink_queue_runtest(struct ulink *device, struct jtag_command *cmd)
  1428. {
  1429. int ret;
  1430. /* Only perform statemove if the TAP currently isn't in the TAP_IDLE state */
  1431. if (tap_get_state() != TAP_IDLE) {
  1432. ulink_set_end_state(TAP_IDLE);
  1433. ulink_queue_statemove(device);
  1434. }
  1435. /* Generate the clock cycles */
  1436. ret = ulink_append_clock_tck_cmd(device, cmd->cmd.runtest->num_cycles);
  1437. if (ret != ERROR_OK)
  1438. return ret;
  1439. /* Move to end state specified in command */
  1440. if (cmd->cmd.runtest->end_state != tap_get_state()) {
  1441. tap_set_end_state(cmd->cmd.runtest->end_state);
  1442. ulink_queue_statemove(device);
  1443. }
  1444. return ERROR_OK;
  1445. }
  1446. /**
  1447. * Execute a JTAG_RESET command
  1448. *
  1449. * @param cmd pointer to the command that shall be executed.
  1450. * @return on success: ERROR_OK
  1451. * @return on failure: ERROR_FAIL
  1452. */
  1453. int ulink_queue_reset(struct ulink *device, struct jtag_command *cmd)
  1454. {
  1455. uint8_t low = 0, high = 0;
  1456. if (cmd->cmd.reset->trst) {
  1457. tap_set_state(TAP_RESET);
  1458. high |= SIGNAL_TRST;
  1459. } else
  1460. low |= SIGNAL_TRST;
  1461. if (cmd->cmd.reset->srst)
  1462. high |= SIGNAL_RESET;
  1463. else
  1464. low |= SIGNAL_RESET;
  1465. return ulink_append_set_signals_cmd(device, low, high);
  1466. }
  1467. /**
  1468. * Move to one TAP state or several states in succession.
  1469. *
  1470. * @param device pointer to struct ulink identifying ULINK driver instance.
  1471. * @param cmd pointer to the command that shall be executed.
  1472. * @return on success: ERROR_OK
  1473. * @return on failure: ERROR_FAIL
  1474. */
  1475. int ulink_queue_pathmove(struct ulink *device, struct jtag_command *cmd)
  1476. {
  1477. int ret, i, num_states, batch_size, state_count;
  1478. tap_state_t *path;
  1479. uint8_t tms_sequence;
  1480. num_states = cmd->cmd.pathmove->num_states;
  1481. path = cmd->cmd.pathmove->path;
  1482. state_count = 0;
  1483. while (num_states > 0) {
  1484. tms_sequence = 0;
  1485. /* Determine batch size */
  1486. if (num_states >= 8)
  1487. batch_size = 8;
  1488. else
  1489. batch_size = num_states;
  1490. for (i = 0; i < batch_size; i++) {
  1491. if (tap_state_transition(tap_get_state(), false) == path[state_count]) {
  1492. /* Append '0' transition: clear bit 'i' in tms_sequence */
  1493. buf_set_u32(&tms_sequence, i, 1, 0x0);
  1494. } else if (tap_state_transition(tap_get_state(), true)
  1495. == path[state_count]) {
  1496. /* Append '1' transition: set bit 'i' in tms_sequence */
  1497. buf_set_u32(&tms_sequence, i, 1, 0x1);
  1498. } else {
  1499. /* Invalid state transition */
  1500. LOG_ERROR("BUG: %s -> %s isn't a valid TAP state transition",
  1501. tap_state_name(tap_get_state()),
  1502. tap_state_name(path[state_count]));
  1503. return ERROR_FAIL;
  1504. }
  1505. tap_set_state(path[state_count]);
  1506. state_count++;
  1507. num_states--;
  1508. }
  1509. /* Append CLOCK_TMS command to OpenULINK command queue */
  1510. LOG_INFO(
  1511. "pathmove batch: count = %i, sequence = 0x%x", batch_size, tms_sequence);
  1512. ret = ulink_append_clock_tms_cmd(ulink_handle, batch_size, tms_sequence);
  1513. if (ret != ERROR_OK)
  1514. return ret;
  1515. }
  1516. return ERROR_OK;
  1517. }
  1518. /**
  1519. * Sleep for a specific amount of time.
  1520. *
  1521. * @param device pointer to struct ulink identifying ULINK driver instance.
  1522. * @param cmd pointer to the command that shall be executed.
  1523. * @return on success: ERROR_OK
  1524. * @return on failure: ERROR_FAIL
  1525. */
  1526. int ulink_queue_sleep(struct ulink *device, struct jtag_command *cmd)
  1527. {
  1528. /* IMPORTANT! Due to the time offset in command execution introduced by
  1529. * command queueing, this needs to be implemented in the ULINK device */
  1530. return ulink_append_sleep_cmd(device, cmd->cmd.sleep->us);
  1531. }
  1532. /**
  1533. * Generate TCK cycles while remaining in a stable state.
  1534. *
  1535. * @param device pointer to struct ulink identifying ULINK driver instance.
  1536. * @param cmd pointer to the command that shall be executed.
  1537. */
  1538. int ulink_queue_stableclocks(struct ulink *device, struct jtag_command *cmd)
  1539. {
  1540. int ret;
  1541. unsigned num_cycles;
  1542. if (!tap_is_state_stable(tap_get_state())) {
  1543. LOG_ERROR("JTAG_STABLECLOCKS: state not stable");
  1544. return ERROR_FAIL;
  1545. }
  1546. num_cycles = cmd->cmd.stableclocks->num_cycles;
  1547. /* TMS stays either high (Test Logic Reset state) or low (all other states) */
  1548. if (tap_get_state() == TAP_RESET)
  1549. ret = ulink_append_set_signals_cmd(device, 0, SIGNAL_TMS);
  1550. else
  1551. ret = ulink_append_set_signals_cmd(device, SIGNAL_TMS, 0);
  1552. if (ret != ERROR_OK)
  1553. return ret;
  1554. while (num_cycles > 0) {
  1555. if (num_cycles > 0xFFFF) {
  1556. /* OpenULINK CMD_CLOCK_TCK can generate up to 0xFFFF (uint16_t) cycles */
  1557. ret = ulink_append_clock_tck_cmd(device, 0xFFFF);
  1558. num_cycles -= 0xFFFF;
  1559. } else {
  1560. ret = ulink_append_clock_tck_cmd(device, num_cycles);
  1561. num_cycles = 0;
  1562. }
  1563. if (ret != ERROR_OK)
  1564. return ret;
  1565. }
  1566. return ERROR_OK;
  1567. }
  1568. /**
  1569. * Post-process JTAG_SCAN command
  1570. *
  1571. * @param ulink_cmd pointer to OpenULINK command that shall be processed.
  1572. * @return on success: ERROR_OK
  1573. * @return on failure: ERROR_FAIL
  1574. */
  1575. int ulink_post_process_scan(struct ulink_cmd *ulink_cmd)
  1576. {
  1577. struct jtag_command *cmd = ulink_cmd->cmd_origin;
  1578. int ret;
  1579. switch (jtag_scan_type(cmd->cmd.scan)) {
  1580. case SCAN_IN:
  1581. case SCAN_IO:
  1582. ret = jtag_read_buffer(ulink_cmd->payload_in_start, cmd->cmd.scan);
  1583. break;
  1584. case SCAN_OUT:
  1585. /* Nothing to do for OUT scans */
  1586. ret = ERROR_OK;
  1587. break;
  1588. default:
  1589. LOG_ERROR("BUG: ulink_post_process_scan() encountered an unknown"
  1590. " JTAG scan type");
  1591. ret = ERROR_FAIL;
  1592. break;
  1593. }
  1594. return ret;
  1595. }
  1596. /**
  1597. * Perform post-processing of commands after OpenULINK queue has been executed.
  1598. *
  1599. * @param device pointer to struct ulink identifying ULINK driver instance.
  1600. * @return on success: ERROR_OK
  1601. * @return on failure: ERROR_FAIL
  1602. */
  1603. int ulink_post_process_queue(struct ulink *device)
  1604. {
  1605. struct ulink_cmd *current;
  1606. struct jtag_command *openocd_cmd;
  1607. int ret;
  1608. current = device->queue_start;
  1609. while (current != NULL) {
  1610. openocd_cmd = current->cmd_origin;
  1611. /* Check if a corresponding OpenOCD command is stored for this
  1612. * OpenULINK command */
  1613. if ((current->needs_postprocessing == true) && (openocd_cmd != NULL)) {
  1614. switch (openocd_cmd->type) {
  1615. case JTAG_SCAN:
  1616. ret = ulink_post_process_scan(current);
  1617. break;
  1618. case JTAG_TLR_RESET:
  1619. case JTAG_RUNTEST:
  1620. case JTAG_RESET:
  1621. case JTAG_PATHMOVE:
  1622. case JTAG_SLEEP:
  1623. case JTAG_STABLECLOCKS:
  1624. /* Nothing to do for these commands */
  1625. ret = ERROR_OK;
  1626. break;
  1627. default:
  1628. ret = ERROR_FAIL;
  1629. LOG_ERROR("BUG: ulink_post_process_queue() encountered unknown JTAG "
  1630. "command type");
  1631. break;
  1632. }
  1633. if (ret != ERROR_OK)
  1634. return ret;
  1635. }
  1636. current = current->next;
  1637. }
  1638. return ERROR_OK;
  1639. }
  1640. /**************************** JTAG driver functions ***************************/
  1641. /**
  1642. * Executes the JTAG Command Queue.
  1643. *
  1644. * This is done in three stages: First, all OpenOCD commands are processed into
  1645. * queued OpenULINK commands. Next, the OpenULINK command queue is sent to the
  1646. * ULINK device and data received from the ULINK device is cached. Finally,
  1647. * the post-processing function writes back data to the corresponding OpenOCD
  1648. * commands.
  1649. *
  1650. * @return on success: ERROR_OK
  1651. * @return on failure: ERROR_FAIL
  1652. */
  1653. static int ulink_execute_queue(void)
  1654. {
  1655. struct jtag_command *cmd = jtag_command_queue;
  1656. int ret;
  1657. while (cmd) {
  1658. switch (cmd->type) {
  1659. case JTAG_SCAN:
  1660. ret = ulink_queue_scan(ulink_handle, cmd);
  1661. break;
  1662. case JTAG_TLR_RESET:
  1663. ret = ulink_queue_tlr_reset(ulink_handle, cmd);
  1664. break;
  1665. case JTAG_RUNTEST:
  1666. ret = ulink_queue_runtest(ulink_handle, cmd);
  1667. break;
  1668. case JTAG_RESET:
  1669. ret = ulink_queue_reset(ulink_handle, cmd);
  1670. break;
  1671. case JTAG_PATHMOVE:
  1672. ret = ulink_queue_pathmove(ulink_handle, cmd);
  1673. break;
  1674. case JTAG_SLEEP:
  1675. ret = ulink_queue_sleep(ulink_handle, cmd);
  1676. break;
  1677. case JTAG_STABLECLOCKS:
  1678. ret = ulink_queue_stableclocks(ulink_handle, cmd);
  1679. break;
  1680. default:
  1681. ret = ERROR_FAIL;
  1682. LOG_ERROR("BUG: encountered unknown JTAG command type");
  1683. break;
  1684. }
  1685. if (ret != ERROR_OK)
  1686. return ret;
  1687. cmd = cmd->next;
  1688. }
  1689. if (ulink_handle->commands_in_queue > 0) {
  1690. ret = ulink_execute_queued_commands(ulink_handle, USB_TIMEOUT);
  1691. if (ret != ERROR_OK)
  1692. return ret;
  1693. ret = ulink_post_process_queue(ulink_handle);
  1694. if (ret != ERROR_OK)
  1695. return ret;
  1696. ulink_clear_queue(ulink_handle);
  1697. }
  1698. return ERROR_OK;
  1699. }
  1700. /**
  1701. * Set the TCK frequency of the ULINK adapter.
  1702. *
  1703. * @param khz desired JTAG TCK frequency.
  1704. * @param jtag_speed where to store corresponding adapter-specific speed value.
  1705. * @return on success: ERROR_OK
  1706. * @return on failure: ERROR_FAIL
  1707. */
  1708. static int ulink_khz(int khz, int *jtag_speed)
  1709. {
  1710. int ret;
  1711. if (khz == 0) {
  1712. LOG_ERROR("RCLK not supported");
  1713. return ERROR_FAIL;
  1714. }
  1715. /* CLOCK_TCK commands are decoupled from others. Therefore, the frequency
  1716. * setting can be done independently from all other commands. */
  1717. if (khz >= 375)
  1718. ulink_handle->delay_clock_tck = -1;
  1719. else {
  1720. ret = ulink_calculate_delay(DELAY_CLOCK_TCK, khz * 1000,
  1721. &ulink_handle->delay_clock_tck);
  1722. if (ret != ERROR_OK)
  1723. return ret;
  1724. }
  1725. /* SCAN_{IN,OUT,IO} commands invoke CLOCK_TMS commands. Therefore, if the
  1726. * requested frequency goes below the maximum frequency for SLOW_CLOCK_TMS
  1727. * commands, all SCAN commands MUST also use the variable frequency
  1728. * implementation! */
  1729. if (khz >= 176) {
  1730. ulink_handle->delay_clock_tms = -1;
  1731. ulink_handle->delay_scan_in = -1;
  1732. ulink_handle->delay_scan_out = -1;
  1733. ulink_handle->delay_scan_io = -1;
  1734. } else {
  1735. ret = ulink_calculate_delay(DELAY_CLOCK_TMS, khz * 1000,
  1736. &ulink_handle->delay_clock_tms);
  1737. if (ret != ERROR_OK)
  1738. return ret;
  1739. ret = ulink_calculate_delay(DELAY_SCAN_IN, khz * 1000,
  1740. &ulink_handle->delay_scan_in);
  1741. if (ret != ERROR_OK)
  1742. return ret;
  1743. ret = ulink_calculate_delay(DELAY_SCAN_OUT, khz * 1000,
  1744. &ulink_handle->delay_scan_out);
  1745. if (ret != ERROR_OK)
  1746. return ret;
  1747. ret = ulink_calculate_delay(DELAY_SCAN_IO, khz * 1000,
  1748. &ulink_handle->delay_scan_io);
  1749. if (ret != ERROR_OK)
  1750. return ret;
  1751. }
  1752. #ifdef _DEBUG_JTAG_IO_
  1753. long f_tck, f_tms, f_scan_in, f_scan_out, f_scan_io;
  1754. ulink_calculate_frequency(DELAY_CLOCK_TCK, ulink_handle->delay_clock_tck,
  1755. &f_tck);
  1756. ulink_calculate_frequency(DELAY_CLOCK_TMS, ulink_handle->delay_clock_tms,
  1757. &f_tms);
  1758. ulink_calculate_frequency(DELAY_SCAN_IN, ulink_handle->delay_scan_in,
  1759. &f_scan_in);
  1760. ulink_calculate_frequency(DELAY_SCAN_OUT, ulink_handle->delay_scan_out,
  1761. &f_scan_out);
  1762. ulink_calculate_frequency(DELAY_SCAN_IO, ulink_handle->delay_scan_io,
  1763. &f_scan_io);
  1764. DEBUG_JTAG_IO("ULINK TCK setup: delay_tck = %i (%li Hz),",
  1765. ulink_handle->delay_clock_tck, f_tck);
  1766. DEBUG_JTAG_IO(" delay_tms = %i (%li Hz),",
  1767. ulink_handle->delay_clock_tms, f_tms);
  1768. DEBUG_JTAG_IO(" delay_scan_in = %i (%li Hz),",
  1769. ulink_handle->delay_scan_in, f_scan_in);
  1770. DEBUG_JTAG_IO(" delay_scan_out = %i (%li Hz),",
  1771. ulink_handle->delay_scan_out, f_scan_out);
  1772. DEBUG_JTAG_IO(" delay_scan_io = %i (%li Hz),",
  1773. ulink_handle->delay_scan_io, f_scan_io);
  1774. #endif
  1775. /* Configure the ULINK device with the new delay values */
  1776. ret = ulink_append_configure_tck_cmd(ulink_handle,
  1777. ulink_handle->delay_scan_in,
  1778. ulink_handle->delay_scan_out,
  1779. ulink_handle->delay_scan_io,
  1780. ulink_handle->delay_clock_tck,
  1781. ulink_handle->delay_clock_tms);
  1782. if (ret != ERROR_OK)
  1783. return ret;
  1784. *jtag_speed = khz;
  1785. return ERROR_OK;
  1786. }
  1787. /**
  1788. * Set the TCK frequency of the ULINK adapter.
  1789. *
  1790. * Because of the way the TCK frequency is set up in the OpenULINK firmware,
  1791. * there are five different speed settings. To simplify things, the
  1792. * adapter-specific speed setting value is identical to the TCK frequency in
  1793. * khz.
  1794. *
  1795. * @param speed desired adapter-specific speed value.
  1796. * @return on success: ERROR_OK
  1797. * @return on failure: ERROR_FAIL
  1798. */
  1799. static int ulink_speed(int speed)
  1800. {
  1801. int dummy;
  1802. return ulink_khz(speed, &dummy);
  1803. }
  1804. /**
  1805. * Convert adapter-specific speed value to corresponding TCK frequency in kHz.
  1806. *
  1807. * Because of the way the TCK frequency is set up in the OpenULINK firmware,
  1808. * there are five different speed settings. To simplify things, the
  1809. * adapter-specific speed setting value is identical to the TCK frequency in
  1810. * khz.
  1811. *
  1812. * @param speed adapter-specific speed value.
  1813. * @param khz where to store corresponding TCK frequency in kHz.
  1814. * @return on success: ERROR_OK
  1815. * @return on failure: ERROR_FAIL
  1816. */
  1817. static int ulink_speed_div(int speed, int *khz)
  1818. {
  1819. *khz = speed;
  1820. return ERROR_OK;
  1821. }
  1822. /**
  1823. * Initiates the firmware download to the ULINK adapter and prepares
  1824. * the USB handle.
  1825. *
  1826. * @return on success: ERROR_OK
  1827. * @return on failure: ERROR_FAIL
  1828. */
  1829. static int ulink_init(void)
  1830. {
  1831. int ret, transferred;
  1832. char str_manufacturer[20];
  1833. bool download_firmware = false;
  1834. unsigned char *dummy;
  1835. uint8_t input_signals, output_signals;
  1836. ulink_handle = calloc(1, sizeof(struct ulink));
  1837. if (ulink_handle == NULL)
  1838. return ERROR_FAIL;
  1839. libusb_init(&ulink_handle->libusb_ctx);
  1840. ret = ulink_usb_open(&ulink_handle);
  1841. if (ret != ERROR_OK) {
  1842. LOG_ERROR("Could not open ULINK device");
  1843. free(ulink_handle);
  1844. ulink_handle = NULL;
  1845. return ret;
  1846. }
  1847. /* Get String Descriptor to determine if firmware needs to be loaded */
  1848. ret = libusb_get_string_descriptor_ascii(ulink_handle->usb_device_handle, 1, (unsigned char *)str_manufacturer, 20);
  1849. if (ret < 0) {
  1850. /* Could not get descriptor -> Unconfigured or original Keil firmware */
  1851. download_firmware = true;
  1852. } else {
  1853. /* We got a String Descriptor, check if it is the correct one */
  1854. if (strncmp(str_manufacturer, "OpenULINK", 9) != 0)
  1855. download_firmware = true;
  1856. }
  1857. if (download_firmware == true) {
  1858. LOG_INFO("Loading OpenULINK firmware. This is reversible by power-cycling"
  1859. " ULINK device.");
  1860. ret = ulink_load_firmware_and_renumerate(&ulink_handle,
  1861. ULINK_FIRMWARE_FILE, ULINK_RENUMERATION_DELAY);
  1862. if (ret != ERROR_OK) {
  1863. LOG_ERROR("Could not download firmware and re-numerate ULINK");
  1864. free(ulink_handle);
  1865. ulink_handle = NULL;
  1866. return ret;
  1867. }
  1868. } else
  1869. LOG_INFO("ULINK device is already running OpenULINK firmware");
  1870. /* Initialize OpenULINK command queue */
  1871. ulink_clear_queue(ulink_handle);
  1872. /* Issue one test command with short timeout */
  1873. ret = ulink_append_test_cmd(ulink_handle);
  1874. if (ret != ERROR_OK)
  1875. return ret;
  1876. ret = ulink_execute_queued_commands(ulink_handle, 200);
  1877. if (ret != ERROR_OK) {
  1878. /* Sending test command failed. The ULINK device may be forever waiting for
  1879. * the host to fetch an USB Bulk IN packet (e. g. OpenOCD crashed or was
  1880. * shut down by the user via Ctrl-C. Try to retrieve this Bulk IN packet. */
  1881. dummy = calloc(64, sizeof(uint8_t));
  1882. ret = libusb_bulk_transfer(ulink_handle->usb_device_handle, (2 | LIBUSB_ENDPOINT_IN),
  1883. dummy, 64, &transferred, 200);
  1884. free(dummy);
  1885. if (ret != 0 || transferred == 0) {
  1886. /* Bulk IN transfer failed -> unrecoverable error condition */
  1887. LOG_ERROR("Cannot communicate with ULINK device. Disconnect ULINK from "
  1888. "the USB port and re-connect, then re-run OpenOCD");
  1889. free(ulink_handle);
  1890. ulink_handle = NULL;
  1891. return ERROR_FAIL;
  1892. }
  1893. #ifdef _DEBUG_USB_COMMS_
  1894. else {
  1895. /* Successfully received Bulk IN packet -> continue */
  1896. LOG_INFO("Recovered from lost Bulk IN packet");
  1897. }
  1898. #endif
  1899. }
  1900. ulink_clear_queue(ulink_handle);
  1901. ulink_append_get_signals_cmd(ulink_handle);
  1902. ulink_execute_queued_commands(ulink_handle, 200);
  1903. /* Post-process the single CMD_GET_SIGNALS command */
  1904. input_signals = ulink_handle->queue_start->payload_in[0];
  1905. output_signals = ulink_handle->queue_start->payload_in[1];
  1906. ulink_print_signal_states(input_signals, output_signals);
  1907. ulink_clear_queue(ulink_handle);
  1908. return ERROR_OK;
  1909. }
  1910. /**
  1911. * Closes the USB handle for the ULINK device.
  1912. *
  1913. * @return on success: ERROR_OK
  1914. * @return on failure: ERROR_FAIL
  1915. */
  1916. static int ulink_quit(void)
  1917. {
  1918. int ret;
  1919. ret = ulink_usb_close(&ulink_handle);
  1920. free(ulink_handle);
  1921. return ret;
  1922. }
  1923. /**
  1924. * Set a custom path to ULINK firmware image and force downloading to ULINK.
  1925. */
  1926. COMMAND_HANDLER(ulink_download_firmware_handler)
  1927. {
  1928. int ret;
  1929. if (CMD_ARGC != 1)
  1930. return ERROR_COMMAND_SYNTAX_ERROR;
  1931. LOG_INFO("Downloading ULINK firmware image %s", CMD_ARGV[0]);
  1932. /* Download firmware image in CMD_ARGV[0] */
  1933. ret = ulink_load_firmware_and_renumerate(&ulink_handle, CMD_ARGV[0],
  1934. ULINK_RENUMERATION_DELAY);
  1935. return ret;
  1936. }
  1937. /*************************** Command Registration **************************/
  1938. static const struct command_registration ulink_command_handlers[] = {
  1939. {
  1940. .name = "ulink_download_firmware",
  1941. .handler = &ulink_download_firmware_handler,
  1942. .mode = COMMAND_EXEC,
  1943. .help = "download firmware image to ULINK device",
  1944. .usage = "path/to/ulink_firmware.hex",
  1945. },
  1946. COMMAND_REGISTRATION_DONE,
  1947. };
  1948. struct jtag_interface ulink_interface = {
  1949. .name = "ulink",
  1950. .commands = ulink_command_handlers,
  1951. .transports = jtag_only,
  1952. .execute_queue = ulink_execute_queue,
  1953. .khz = ulink_khz,
  1954. .speed = ulink_speed,
  1955. .speed_div = ulink_speed_div,
  1956. .init = ulink_init,
  1957. .quit = ulink_quit
  1958. };