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.
 
 
 
 
 
 

2278 lines
65 KiB

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