1 /***************************************************************************
2 * Copyright (C) 2011-2013 by Martin Schmoelzer *
3 * <martin.schmoelzer@student.tuwien.ac.at> *
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. *
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. *
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 ***************************************************************************/
24 #include <jtag/interface.h>
25 #include <jtag/commands.h>
26 #include <target/image.h>
28 #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
34 /** USB Product ID of ULINK device in unconfigured state (no firmware loaded
35 * yet) or with OpenULINK firmware. */
36 #define ULINK_PID 0x2710
38 /** Address of EZ-USB CPU Control & Status register. This register can be
39 * written by issuing a Control EP0 vendor request. */
40 #define CPUCS_REG 0x7F92
42 /** USB Control EP0 bRequest: "Firmware Load". */
43 #define REQUEST_FIRMWARE_LOAD 0xA0
45 /** Value to write into CPUCS to put EZ-USB into reset. */
46 #define CPU_RESET 0x01
48 /** Value to write into CPUCS to put EZ-USB out of reset. */
49 #define CPU_START 0x00
51 /** Base address of firmware in EZ-USB code space. */
52 #define FIRMWARE_ADDR 0x0000
54 /** USB interface number */
55 #define USB_INTERFACE 0
57 /** libusb timeout in ms */
58 #define USB_TIMEOUT 5000
60 /** Delay (in microseconds) to wait while EZ-USB performs ReNumeration. */
61 #define ULINK_RENUMERATION_DELAY 1500000
63 /** Default location of OpenULINK firmware image. */
64 #define ULINK_FIRMWARE_FILE PKGDATADIR "/OpenULINK/ulink_firmware.hex"
66 /** Maximum size of a single firmware section. Entire EZ-USB code space = 8kB */
67 #define SECTION_BUFFERSIZE 8192
69 /** Tuning of OpenOCD SCAN commands split into multiple OpenULINK commands. */
70 #define SPLIT_SCAN_THRESHOLD 10
72 /** ULINK hardware type */
74 /** Original ULINK adapter, based on Cypress EZ-USB (AN2131):
75 * Full JTAG support, no SWD support. */
78 /** Newer ULINK adapter, based on NXP LPC2148. Currently unsupported. */
81 /** Newer ULINK adapter, based on EZ-USB FX2 + FPGA. Currently unsupported. */
84 /** Newer ULINK adapter, possibly based on ULINK 2. Currently unsupported. */
88 enum ulink_payload_direction
{
89 PAYLOAD_DIRECTION_OUT
,
93 enum ulink_delay_type
{
102 * OpenULINK command (OpenULINK command queue element).
104 * For the OUT direction payload, things are quite easy: Payload is stored
105 * in a rather small array (up to 63 bytes), the payload is always allocated
106 * by the function generating the command and freed by ulink_clear_queue().
108 * For the IN direction payload, things get a little bit more complicated:
109 * The maximum IN payload size for a single command is 64 bytes. Assume that
110 * a single OpenOCD command needs to scan 256 bytes. This results in the
111 * generation of four OpenULINK commands. The function generating these
112 * commands shall allocate an uint8_t[256] array. Each command's #payload_in
113 * pointer shall point to the corresponding offset where IN data shall be
114 * placed, while #payload_in_start shall point to the first element of the 256
116 * - first command: #payload_in_start + 0
117 * - second command: #payload_in_start + 64
118 * - third command: #payload_in_start + 128
119 * - fourth command: #payload_in_start + 192
121 * The last command sets #needs_postprocessing to true.
124 uint8_t id
; /**< ULINK command ID */
126 uint8_t *payload_out
; /**< OUT direction payload data */
127 uint8_t payload_out_size
; /**< OUT direction payload size for this command */
129 uint8_t *payload_in_start
; /**< Pointer to first element of IN payload array */
130 uint8_t *payload_in
; /**< Pointer where IN payload shall be stored */
131 uint8_t payload_in_size
; /**< IN direction payload size for this command */
133 /** Indicates if this command needs post-processing */
134 bool needs_postprocessing
;
136 /** Indicates if ulink_clear_queue() should free payload_in_start */
137 bool free_payload_in_start
;
139 /** Pointer to corresponding OpenOCD command for post-processing */
140 struct jtag_command
*cmd_origin
;
142 struct ulink_cmd
*next
; /**< Pointer to next command (linked list) */
145 /** Describes one driver instance */
147 struct libusb_context
*libusb_ctx
;
148 struct libusb_device_handle
*usb_device_handle
;
149 enum ulink_type type
;
151 int delay_scan_in
; /**< Delay value for SCAN_IN commands */
152 int delay_scan_out
; /**< Delay value for SCAN_OUT commands */
153 int delay_scan_io
; /**< Delay value for SCAN_IO commands */
154 int delay_clock_tck
; /**< Delay value for CLOCK_TMS commands */
155 int delay_clock_tms
; /**< Delay value for CLOCK_TCK commands */
157 int commands_in_queue
; /**< Number of commands in queue */
158 struct ulink_cmd
*queue_start
; /**< Pointer to first command in queue */
159 struct ulink_cmd
*queue_end
; /**< Pointer to last command in queue */
162 /**************************** Function Prototypes *****************************/
164 /* USB helper functions */
165 int ulink_usb_open(struct ulink
**device
);
166 int ulink_usb_close(struct ulink
**device
);
168 /* ULINK MCU (Cypress EZ-USB) specific functions */
169 int ulink_cpu_reset(struct ulink
*device
, unsigned char reset_bit
);
170 int ulink_load_firmware_and_renumerate(struct ulink
**device
, const char *filename
,
172 int ulink_load_firmware(struct ulink
*device
, const char *filename
);
173 int ulink_write_firmware_section(struct ulink
*device
,
174 struct image
*firmware_image
, int section_index
);
176 /* Generic helper functions */
177 void ulink_print_signal_states(uint8_t input_signals
, uint8_t output_signals
);
179 /* OpenULINK command generation helper functions */
180 int ulink_allocate_payload(struct ulink_cmd
*ulink_cmd
, int size
,
181 enum ulink_payload_direction direction
);
183 /* OpenULINK command queue helper functions */
184 int ulink_get_queue_size(struct ulink
*device
,
185 enum ulink_payload_direction direction
);
186 void ulink_clear_queue(struct ulink
*device
);
187 int ulink_append_queue(struct ulink
*device
, struct ulink_cmd
*ulink_cmd
);
188 int ulink_execute_queued_commands(struct ulink
*device
, int timeout
);
190 static void ulink_print_queue(struct ulink
*device
);
192 int ulink_append_scan_cmd(struct ulink
*device
,
193 enum scan_type scan_type
,
198 uint8_t tms_count_start
,
199 uint8_t tms_sequence_start
,
200 uint8_t tms_count_end
,
201 uint8_t tms_sequence_end
,
202 struct jtag_command
*origin
,
204 int ulink_append_clock_tms_cmd(struct ulink
*device
, uint8_t count
,
206 int ulink_append_clock_tck_cmd(struct ulink
*device
, uint16_t count
);
207 int ulink_append_get_signals_cmd(struct ulink
*device
);
208 int ulink_append_set_signals_cmd(struct ulink
*device
, uint8_t low
,
210 int ulink_append_sleep_cmd(struct ulink
*device
, uint32_t us
);
211 int ulink_append_configure_tck_cmd(struct ulink
*device
,
217 int ulink_append_led_cmd(struct ulink
*device
, uint8_t led_state
);
218 int ulink_append_test_cmd(struct ulink
*device
);
220 /* OpenULINK TCK frequency helper functions */
221 int ulink_calculate_delay(enum ulink_delay_type type
, long f
, int *delay
);
223 /* Interface between OpenULINK and OpenOCD */
224 static void ulink_set_end_state(tap_state_t endstate
);
225 int ulink_queue_statemove(struct ulink
*device
);
227 int ulink_queue_scan(struct ulink
*device
, struct jtag_command
*cmd
);
228 int ulink_queue_tlr_reset(struct ulink
*device
, struct jtag_command
*cmd
);
229 int ulink_queue_runtest(struct ulink
*device
, struct jtag_command
*cmd
);
230 int ulink_queue_reset(struct ulink
*device
, struct jtag_command
*cmd
);
231 int ulink_queue_pathmove(struct ulink
*device
, struct jtag_command
*cmd
);
232 int ulink_queue_sleep(struct ulink
*device
, struct jtag_command
*cmd
);
233 int ulink_queue_stableclocks(struct ulink
*device
, struct jtag_command
*cmd
);
235 int ulink_post_process_scan(struct ulink_cmd
*ulink_cmd
);
236 int ulink_post_process_queue(struct ulink
*device
);
238 /* JTAG driver functions (registered in struct jtag_interface) */
239 static int ulink_execute_queue(void);
240 static int ulink_khz(int khz
, int *jtag_speed
);
241 static int ulink_speed(int speed
);
242 static int ulink_speed_div(int speed
, int *khz
);
243 static int ulink_init(void);
244 static int ulink_quit(void);
246 /****************************** Global Variables ******************************/
248 struct ulink
*ulink_handle
;
250 /**************************** USB helper functions ****************************/
253 * Opens the ULINK device and claims its USB interface.
255 * Currently, only the original ULINK is supported
257 * @param device pointer to struct ulink identifying ULINK driver instance.
258 * @return on success: ERROR_OK
259 * @return on failure: ERROR_FAIL
261 int ulink_usb_open(struct ulink
**device
)
263 ssize_t num_devices
, i
;
265 libusb_device
**usb_devices
;
266 struct libusb_device_descriptor usb_desc
;
267 struct libusb_device_handle
*usb_device_handle
;
269 num_devices
= libusb_get_device_list((*device
)->libusb_ctx
, &usb_devices
);
271 if (num_devices
<= 0)
275 for (i
= 0; i
< num_devices
; i
++) {
276 if (libusb_get_device_descriptor(usb_devices
[i
], &usb_desc
) != 0)
278 else if (usb_desc
.idVendor
== ULINK_VID
&& usb_desc
.idProduct
== ULINK_PID
) {
287 if (libusb_open(usb_devices
[i
], &usb_device_handle
) != 0)
289 libusb_free_device_list(usb_devices
, 1);
291 if (libusb_claim_interface(usb_device_handle
, 0) != 0)
294 (*device
)->usb_device_handle
= usb_device_handle
;
295 (*device
)->type
= ULINK_1
;
301 * Releases the ULINK interface and closes the USB device handle.
303 * @param device pointer to struct ulink identifying ULINK driver instance.
304 * @return on success: ERROR_OK
305 * @return on failure: ERROR_FAIL
307 int ulink_usb_close(struct ulink
**device
)
309 if (libusb_release_interface((*device
)->usb_device_handle
, 0) != 0)
312 libusb_close((*device
)->usb_device_handle
);
314 (*device
)->usb_device_handle
= NULL
;
319 /******************* ULINK CPU (EZ-USB) specific functions ********************/
322 * Writes '0' or '1' to the CPUCS register, putting the EZ-USB CPU into reset
325 * @param device pointer to struct ulink identifying ULINK driver instance.
326 * @param reset_bit 0 to put CPU into reset, 1 to put CPU out of reset.
327 * @return on success: ERROR_OK
328 * @return on failure: ERROR_FAIL
330 int ulink_cpu_reset(struct ulink
*device
, unsigned char reset_bit
)
334 ret
= libusb_control_transfer(device
->usb_device_handle
,
335 (LIBUSB_ENDPOINT_OUT
| LIBUSB_REQUEST_TYPE_VENDOR
| LIBUSB_RECIPIENT_DEVICE
),
336 REQUEST_FIRMWARE_LOAD
, CPUCS_REG
, 0, &reset_bit
, 1, USB_TIMEOUT
);
338 /* usb_control_msg() returns the number of bytes transferred during the
339 * DATA stage of the control transfer - must be exactly 1 in this case! */
346 * Puts the ULINK's EZ-USB microcontroller into reset state, downloads
347 * the firmware image, resumes the microcontroller and re-enumerates
350 * @param device pointer to struct ulink identifying ULINK driver instance.
351 * The usb_handle member will be modified during re-enumeration.
352 * @param filename path to the Intel HEX file containing the firmware image.
353 * @param delay the delay to wait for the device to re-enumerate.
354 * @return on success: ERROR_OK
355 * @return on failure: ERROR_FAIL
357 int ulink_load_firmware_and_renumerate(struct ulink
**device
,
358 const char *filename
, uint32_t delay
)
362 /* Basic process: After downloading the firmware, the ULINK will disconnect
363 * itself and re-connect after a short amount of time so we have to close
364 * the handle and re-enumerate USB devices */
366 ret
= ulink_load_firmware(*device
, filename
);
370 ret
= ulink_usb_close(device
);
376 ret
= ulink_usb_open(device
);
384 * Downloads a firmware image to the ULINK's EZ-USB microcontroller
387 * @param device pointer to struct ulink identifying ULINK driver instance.
388 * @param filename an absolute or relative path to the Intel HEX file
389 * containing the firmware image.
390 * @return on success: ERROR_OK
391 * @return on failure: ERROR_FAIL
393 int ulink_load_firmware(struct ulink
*device
, const char *filename
)
395 struct image ulink_firmware_image
;
398 ret
= ulink_cpu_reset(device
, CPU_RESET
);
399 if (ret
!= ERROR_OK
) {
400 LOG_ERROR("Could not halt ULINK CPU");
404 ulink_firmware_image
.base_address
= 0;
405 ulink_firmware_image
.base_address_set
= 0;
407 ret
= image_open(&ulink_firmware_image
, filename
, "ihex");
408 if (ret
!= ERROR_OK
) {
409 LOG_ERROR("Could not load firmware image");
413 /* Download all sections in the image to ULINK */
414 for (i
= 0; i
< ulink_firmware_image
.num_sections
; i
++) {
415 ret
= ulink_write_firmware_section(device
, &ulink_firmware_image
, i
);
420 image_close(&ulink_firmware_image
);
422 ret
= ulink_cpu_reset(device
, CPU_START
);
423 if (ret
!= ERROR_OK
) {
424 LOG_ERROR("Could not restart ULINK CPU");
432 * Send one contiguous firmware section to the ULINK's EZ-USB microcontroller
435 * @param device pointer to struct ulink identifying ULINK driver instance.
436 * @param firmware_image pointer to the firmware image that contains the section
437 * which should be sent to the ULINK's EZ-USB microcontroller.
438 * @param section_index index of the section within the firmware image.
439 * @return on success: ERROR_OK
440 * @return on failure: ERROR_FAIL
442 int ulink_write_firmware_section(struct ulink
*device
,
443 struct image
*firmware_image
, int section_index
)
445 uint16_t addr
, size
, bytes_remaining
, chunk_size
;
446 uint8_t data
[SECTION_BUFFERSIZE
];
447 uint8_t *data_ptr
= data
;
451 size
= (uint16_t)firmware_image
->sections
[section_index
].size
;
452 addr
= (uint16_t)firmware_image
->sections
[section_index
].base_address
;
454 LOG_DEBUG("section %02i at addr 0x%04x (size 0x%04x)", section_index
, addr
,
457 /* Copy section contents to local buffer */
458 ret
= image_read_section(firmware_image
, section_index
, 0, size
, data
,
461 if ((ret
!= ERROR_OK
) || (size_read
!= size
)) {
462 /* Propagating the return code would return '0' (misleadingly indicating
463 * successful execution of the function) if only the size check fails. */
467 bytes_remaining
= size
;
469 /* Send section data in chunks of up to 64 bytes to ULINK */
470 while (bytes_remaining
> 0) {
471 if (bytes_remaining
> 64)
474 chunk_size
= bytes_remaining
;
476 ret
= libusb_control_transfer(device
->usb_device_handle
,
477 (LIBUSB_ENDPOINT_OUT
| LIBUSB_REQUEST_TYPE_VENDOR
| LIBUSB_RECIPIENT_DEVICE
),
478 REQUEST_FIRMWARE_LOAD
, addr
, FIRMWARE_ADDR
, (unsigned char *)data_ptr
,
479 chunk_size
, USB_TIMEOUT
);
481 if (ret
!= (int)chunk_size
) {
482 /* Abort if libusb sent less data than requested */
486 bytes_remaining
-= chunk_size
;
488 data_ptr
+= chunk_size
;
494 /************************** Generic helper functions **************************/
497 * Print state of interesting signals via LOG_INFO().
499 * @param input_signals input signal states as returned by CMD_GET_SIGNALS
500 * @param output_signals output signal states as returned by CMD_GET_SIGNALS
502 void ulink_print_signal_states(uint8_t input_signals
, uint8_t output_signals
)
504 LOG_INFO("ULINK signal states: TDI: %i, TDO: %i, TMS: %i, TCK: %i, TRST: %i,"
506 (output_signals
& SIGNAL_TDI ?
1 : 0),
507 (input_signals
& SIGNAL_TDO ?
1 : 0),
508 (output_signals
& SIGNAL_TMS ?
1 : 0),
509 (output_signals
& SIGNAL_TCK ?
1 : 0),
510 (output_signals
& SIGNAL_TRST ?
0 : 1), /* Inverted by hardware */
511 (output_signals
& SIGNAL_RESET ?
0 : 1)); /* Inverted by hardware */
514 /**************** OpenULINK command generation helper functions ***************/
517 * Allocate and initialize space in memory for OpenULINK command payload.
519 * @param ulink_cmd pointer to command whose payload should be allocated.
520 * @param size the amount of memory to allocate (bytes).
521 * @param direction which payload to allocate.
522 * @return on success: ERROR_OK
523 * @return on failure: ERROR_FAIL
525 int ulink_allocate_payload(struct ulink_cmd
*ulink_cmd
, int size
,
526 enum ulink_payload_direction direction
)
530 payload
= calloc(size
, sizeof(uint8_t));
532 if (payload
== NULL
) {
533 LOG_ERROR("Could not allocate OpenULINK command payload: out of memory");
538 case PAYLOAD_DIRECTION_OUT
:
539 if (ulink_cmd
->payload_out
!= NULL
) {
540 LOG_ERROR("BUG: Duplicate payload allocation for OpenULINK command");
544 ulink_cmd
->payload_out
= payload
;
545 ulink_cmd
->payload_out_size
= size
;
548 case PAYLOAD_DIRECTION_IN
:
549 if (ulink_cmd
->payload_in_start
!= NULL
) {
550 LOG_ERROR("BUG: Duplicate payload allocation for OpenULINK command");
554 ulink_cmd
->payload_in_start
= payload
;
555 ulink_cmd
->payload_in
= payload
;
556 ulink_cmd
->payload_in_size
= size
;
558 /* By default, free payload_in_start in ulink_clear_queue(). Commands
559 * that do not want this behavior (e. g. split scans) must turn it off
561 ulink_cmd
->free_payload_in_start
= true;
569 /****************** OpenULINK command queue helper functions ******************/
572 * Get the current number of bytes in the queue, including command IDs.
574 * @param device pointer to struct ulink identifying ULINK driver instance.
575 * @param direction the transfer direction for which to get byte count.
576 * @return the number of bytes currently stored in the queue for the specified
579 int ulink_get_queue_size(struct ulink
*device
,
580 enum ulink_payload_direction direction
)
582 struct ulink_cmd
*current
= device
->queue_start
;
585 while (current
!= NULL
) {
587 case PAYLOAD_DIRECTION_OUT
:
588 sum
+= current
->payload_out_size
+ 1; /* + 1 byte for Command ID */
590 case PAYLOAD_DIRECTION_IN
:
591 sum
+= current
->payload_in_size
;
595 current
= current
->next
;
602 * Clear the OpenULINK command queue.
604 * @param device pointer to struct ulink identifying ULINK driver instance.
605 * @return on success: ERROR_OK
606 * @return on failure: ERROR_FAIL
608 void ulink_clear_queue(struct ulink
*device
)
610 struct ulink_cmd
*current
= device
->queue_start
;
611 struct ulink_cmd
*next
= NULL
;
613 while (current
!= NULL
) {
614 /* Save pointer to next element */
615 next
= current
->next
;
617 /* Free payloads: OUT payload can be freed immediately */
618 free(current
->payload_out
);
619 current
->payload_out
= NULL
;
621 /* IN payload MUST be freed ONLY if no other commands use the
622 * payload_in_start buffer */
623 if (current
->free_payload_in_start
== true) {
624 free(current
->payload_in_start
);
625 current
->payload_in_start
= NULL
;
626 current
->payload_in
= NULL
;
629 /* Free queue element */
632 /* Proceed with next element */
636 device
->commands_in_queue
= 0;
637 device
->queue_start
= NULL
;
638 device
->queue_end
= NULL
;
642 * Add a command to the OpenULINK command queue.
644 * @param device pointer to struct ulink identifying ULINK driver instance.
645 * @param ulink_cmd pointer to command that shall be appended to the OpenULINK
647 * @return on success: ERROR_OK
648 * @return on failure: ERROR_FAIL
650 int ulink_append_queue(struct ulink
*device
, struct ulink_cmd
*ulink_cmd
)
652 int newsize_out
, newsize_in
;
655 newsize_out
= ulink_get_queue_size(device
, PAYLOAD_DIRECTION_OUT
) + 1
656 + ulink_cmd
->payload_out_size
;
658 newsize_in
= ulink_get_queue_size(device
, PAYLOAD_DIRECTION_IN
)
659 + ulink_cmd
->payload_in_size
;
661 /* Check if the current command can be appended to the queue */
662 if ((newsize_out
> 64) || (newsize_in
> 64)) {
663 /* New command does not fit. Execute all commands in queue before starting
664 * new queue with the current command as first entry. */
665 ret
= ulink_execute_queued_commands(device
, USB_TIMEOUT
);
669 ret
= ulink_post_process_queue(device
);
673 ulink_clear_queue(device
);
676 if (device
->queue_start
== NULL
) {
677 /* Queue was empty */
678 device
->commands_in_queue
= 1;
680 device
->queue_start
= ulink_cmd
;
681 device
->queue_end
= ulink_cmd
;
683 /* There are already commands in the queue */
684 device
->commands_in_queue
++;
686 device
->queue_end
->next
= ulink_cmd
;
687 device
->queue_end
= ulink_cmd
;
694 * Sends all queued OpenULINK commands to the ULINK for execution.
696 * @param device pointer to struct ulink identifying ULINK driver instance.
697 * @return on success: ERROR_OK
698 * @return on failure: ERROR_FAIL
700 int ulink_execute_queued_commands(struct ulink
*device
, int timeout
)
702 struct ulink_cmd
*current
;
703 int ret
, i
, index_out
, index_in
, count_out
, count_in
, transferred
;
706 if (LOG_LEVEL_IS(LOG_LVL_DEBUG_IO
))
707 ulink_print_queue(device
);
713 for (current
= device
->queue_start
; current
; current
= current
->next
) {
714 /* Add command to packet */
715 buffer
[index_out
] = current
->id
;
719 for (i
= 0; i
< current
->payload_out_size
; i
++)
720 buffer
[index_out
+ i
] = current
->payload_out
[i
];
721 index_out
+= current
->payload_out_size
;
722 count_in
+= current
->payload_in_size
;
723 count_out
+= current
->payload_out_size
;
726 /* Send packet to ULINK */
727 ret
= libusb_bulk_transfer(device
->usb_device_handle
, (2 | LIBUSB_ENDPOINT_OUT
),
728 (unsigned char *)buffer
, count_out
, &transferred
, timeout
);
731 if (transferred
!= count_out
)
734 /* Wait for response if commands contain IN payload data */
736 ret
= libusb_bulk_transfer(device
->usb_device_handle
, (2 | LIBUSB_ENDPOINT_IN
),
737 (unsigned char *)buffer
, 64, &transferred
, timeout
);
740 if (transferred
!= count_in
)
743 /* Write back IN payload data */
745 for (current
= device
->queue_start
; current
; current
= current
->next
) {
746 for (i
= 0; i
< current
->payload_in_size
; i
++) {
747 current
->payload_in
[i
] = buffer
[index_in
];
757 * Convert an OpenULINK command ID (\a id) to a human-readable string.
759 * @param id the OpenULINK command ID.
760 * @return the corresponding human-readable string.
762 static const char *ulink_cmd_id_string(uint8_t id
)
766 return "CMD_SCAN_IN";
768 case CMD_SLOW_SCAN_IN
:
769 return "CMD_SLOW_SCAN_IN";
772 return "CMD_SCAN_OUT";
774 case CMD_SLOW_SCAN_OUT
:
775 return "CMD_SLOW_SCAN_OUT";
778 return "CMD_SCAN_IO";
780 case CMD_SLOW_SCAN_IO
:
781 return "CMD_SLOW_SCAN_IO";
784 return "CMD_CLOCK_TMS";
786 case CMD_SLOW_CLOCK_TMS
:
787 return "CMD_SLOW_CLOCK_TMS";
790 return "CMD_CLOCK_TCK";
792 case CMD_SLOW_CLOCK_TCK
:
793 return "CMD_SLOW_CLOCK_TCK";
796 return "CMD_SLEEP_US";
799 return "CMD_SLEEP_MS";
801 case CMD_GET_SIGNALS
:
802 return "CMD_GET_SIGNALS";
804 case CMD_SET_SIGNALS
:
805 return "CMD_SET_SIGNALS";
807 case CMD_CONFIGURE_TCK_FREQ
:
808 return "CMD_CONFIGURE_TCK_FREQ";
811 return "CMD_SET_LEDS";
817 return "CMD_UNKNOWN";
823 * Print one OpenULINK command to stdout.
825 * @param ulink_cmd pointer to OpenULINK command.
827 static void ulink_print_command(struct ulink_cmd
*ulink_cmd
)
831 printf(" %-22s | OUT size = %i, bytes = 0x",
832 ulink_cmd_id_string(ulink_cmd
->id
), ulink_cmd
->payload_out_size
);
834 for (i
= 0; i
< ulink_cmd
->payload_out_size
; i
++)
835 printf("%02X ", ulink_cmd
->payload_out
[i
]);
836 printf("\n | IN size = %i\n",
837 ulink_cmd
->payload_in_size
);
841 * Print the OpenULINK command queue to stdout.
843 * @param device pointer to struct ulink identifying ULINK driver instance.
845 static void ulink_print_queue(struct ulink
*device
)
847 struct ulink_cmd
*current
;
849 printf("OpenULINK command queue:\n");
851 for (current
= device
->queue_start
; current
; current
= current
->next
)
852 ulink_print_command(current
);
858 * Creates and appends a JTAG scan command to the OpenULINK command queue.
859 * A JTAG scan consists of three steps:
860 * - Move to the desired SHIFT state, depending on scan type (IR/DR scan).
861 * - Shift TDI data into the JTAG chain, optionally reading the TDO pin.
862 * - Move to the desired end state.
864 * @param device pointer to struct ulink identifying ULINK driver instance.
865 * @param scan_type the type of the scan (IN, OUT, IO (bidirectional)).
866 * @param scan_size_bits number of bits to shift into the JTAG chain.
867 * @param tdi pointer to array containing TDI data.
868 * @param tdo_start pointer to first element of array where TDO data shall be
869 * stored. See #ulink_cmd for details.
870 * @param tdo pointer to array where TDO data shall be stored
871 * @param tms_count_start number of TMS state transitions to perform BEFORE
872 * shifting data into the JTAG chain.
873 * @param tms_sequence_start sequence of TMS state transitions that will be
874 * performed BEFORE shifting data into the JTAG chain.
875 * @param tms_count_end number of TMS state transitions to perform AFTER
876 * shifting data into the JTAG chain.
877 * @param tms_sequence_end sequence of TMS state transitions that will be
878 * performed AFTER shifting data into the JTAG chain.
879 * @param origin pointer to OpenOCD command that generated this scan command.
880 * @param postprocess whether this command needs to be post-processed after
882 * @return on success: ERROR_OK
883 * @return on failure: ERROR_FAIL
885 int ulink_append_scan_cmd(struct ulink
*device
, enum scan_type scan_type
,
886 int scan_size_bits
, uint8_t *tdi
, uint8_t *tdo_start
, uint8_t *tdo
,
887 uint8_t tms_count_start
, uint8_t tms_sequence_start
, uint8_t tms_count_end
,
888 uint8_t tms_sequence_end
, struct jtag_command
*origin
, bool postprocess
)
890 struct ulink_cmd
*cmd
= calloc(1, sizeof(struct ulink_cmd
));
891 int ret
, i
, scan_size_bytes
;
892 uint8_t bits_last_byte
;
897 /* Check size of command. USB buffer can hold 64 bytes, 1 byte is command ID,
898 * 5 bytes are setup data -> 58 remaining payload bytes for TDI data */
899 if (scan_size_bits
> (58 * 8)) {
900 LOG_ERROR("BUG: Tried to create CMD_SCAN_IO OpenULINK command with too"
906 scan_size_bytes
= DIV_ROUND_UP(scan_size_bits
, 8);
908 bits_last_byte
= scan_size_bits
% 8;
909 if (bits_last_byte
== 0)
912 /* Allocate out_payload depending on scan type */
915 if (device
->delay_scan_in
< 0)
916 cmd
->id
= CMD_SCAN_IN
;
918 cmd
->id
= CMD_SLOW_SCAN_IN
;
919 ret
= ulink_allocate_payload(cmd
, 5, PAYLOAD_DIRECTION_OUT
);
922 if (device
->delay_scan_out
< 0)
923 cmd
->id
= CMD_SCAN_OUT
;
925 cmd
->id
= CMD_SLOW_SCAN_OUT
;
926 ret
= ulink_allocate_payload(cmd
, scan_size_bytes
+ 5, PAYLOAD_DIRECTION_OUT
);
929 if (device
->delay_scan_io
< 0)
930 cmd
->id
= CMD_SCAN_IO
;
932 cmd
->id
= CMD_SLOW_SCAN_IO
;
933 ret
= ulink_allocate_payload(cmd
, scan_size_bytes
+ 5, PAYLOAD_DIRECTION_OUT
);
936 LOG_ERROR("BUG: ulink_append_scan_cmd() encountered an unknown scan type");
941 if (ret
!= ERROR_OK
) {
946 /* Build payload_out that is common to all scan types */
947 cmd
->payload_out
[0] = scan_size_bytes
& 0xFF;
948 cmd
->payload_out
[1] = bits_last_byte
& 0xFF;
949 cmd
->payload_out
[2] = ((tms_count_start
& 0x0F) << 4) | (tms_count_end
& 0x0F);
950 cmd
->payload_out
[3] = tms_sequence_start
;
951 cmd
->payload_out
[4] = tms_sequence_end
;
953 /* Setup payload_out for types with OUT transfer */
954 if ((scan_type
== SCAN_OUT
) || (scan_type
== SCAN_IO
)) {
955 for (i
= 0; i
< scan_size_bytes
; i
++)
956 cmd
->payload_out
[i
+ 5] = tdi
[i
];
959 /* Setup payload_in pointers for types with IN transfer */
960 if ((scan_type
== SCAN_IN
) || (scan_type
== SCAN_IO
)) {
961 cmd
->payload_in_start
= tdo_start
;
962 cmd
->payload_in
= tdo
;
963 cmd
->payload_in_size
= scan_size_bytes
;
966 cmd
->needs_postprocessing
= postprocess
;
967 cmd
->cmd_origin
= origin
;
969 /* For scan commands, we free payload_in_start only when the command is
970 * the last in a series of split commands or a stand-alone command */
971 cmd
->free_payload_in_start
= postprocess
;
973 return ulink_append_queue(device
, cmd
);
977 * Perform TAP state transitions
979 * @param device pointer to struct ulink identifying ULINK driver instance.
980 * @param count defines the number of TCK clock cycles generated (up to 8).
981 * @param sequence defines the TMS pin levels for each state transition. The
982 * Least-Significant Bit is read first.
983 * @return on success: ERROR_OK
984 * @return on failure: ERROR_FAIL
986 int ulink_append_clock_tms_cmd(struct ulink
*device
, uint8_t count
,
989 struct ulink_cmd
*cmd
= calloc(1, sizeof(struct ulink_cmd
));
995 if (device
->delay_clock_tms
< 0)
996 cmd
->id
= CMD_CLOCK_TMS
;
998 cmd
->id
= CMD_SLOW_CLOCK_TMS
;
1000 /* CMD_CLOCK_TMS has two OUT payload bytes and zero IN payload bytes */
1001 ret
= ulink_allocate_payload(cmd
, 2, PAYLOAD_DIRECTION_OUT
);
1002 if (ret
!= ERROR_OK
) {
1007 cmd
->payload_out
[0] = count
;
1008 cmd
->payload_out
[1] = sequence
;
1010 return ulink_append_queue(device
, cmd
);
1014 * Generate a defined amount of TCK clock cycles
1016 * All other JTAG signals are left unchanged.
1018 * @param device pointer to struct ulink identifying ULINK driver instance.
1019 * @param count the number of TCK clock cycles to generate.
1020 * @return on success: ERROR_OK
1021 * @return on failure: ERROR_FAIL
1023 int ulink_append_clock_tck_cmd(struct ulink
*device
, uint16_t count
)
1025 struct ulink_cmd
*cmd
= calloc(1, sizeof(struct ulink_cmd
));
1031 if (device
->delay_clock_tck
< 0)
1032 cmd
->id
= CMD_CLOCK_TCK
;
1034 cmd
->id
= CMD_SLOW_CLOCK_TCK
;
1036 /* CMD_CLOCK_TCK has two OUT payload bytes and zero IN payload bytes */
1037 ret
= ulink_allocate_payload(cmd
, 2, PAYLOAD_DIRECTION_OUT
);
1038 if (ret
!= ERROR_OK
) {
1043 cmd
->payload_out
[0] = count
& 0xff;
1044 cmd
->payload_out
[1] = (count
>> 8) & 0xff;
1046 return ulink_append_queue(device
, cmd
);
1050 * Read JTAG signals.
1052 * @param device pointer to struct ulink identifying ULINK driver instance.
1053 * @return on success: ERROR_OK
1054 * @return on failure: ERROR_FAIL
1056 int ulink_append_get_signals_cmd(struct ulink
*device
)
1058 struct ulink_cmd
*cmd
= calloc(1, sizeof(struct ulink_cmd
));
1064 cmd
->id
= CMD_GET_SIGNALS
;
1065 cmd
->needs_postprocessing
= true;
1067 /* CMD_GET_SIGNALS has two IN payload bytes */
1068 ret
= ulink_allocate_payload(cmd
, 2, PAYLOAD_DIRECTION_IN
);
1070 if (ret
!= ERROR_OK
) {
1075 return ulink_append_queue(device
, cmd
);
1079 * Arbitrarily set JTAG output signals.
1081 * @param device pointer to struct ulink identifying ULINK driver instance.
1082 * @param low defines which signals will be de-asserted. Each bit corresponds
1091 * @param high defines which signals will be asserted.
1092 * @return on success: ERROR_OK
1093 * @return on failure: ERROR_FAIL
1095 int ulink_append_set_signals_cmd(struct ulink
*device
, uint8_t low
,
1098 struct ulink_cmd
*cmd
= calloc(1, sizeof(struct ulink_cmd
));
1104 cmd
->id
= CMD_SET_SIGNALS
;
1106 /* CMD_SET_SIGNALS has two OUT payload bytes and zero IN payload bytes */
1107 ret
= ulink_allocate_payload(cmd
, 2, PAYLOAD_DIRECTION_OUT
);
1109 if (ret
!= ERROR_OK
) {
1114 cmd
->payload_out
[0] = low
;
1115 cmd
->payload_out
[1] = high
;
1117 return ulink_append_queue(device
, cmd
);
1121 * Sleep for a pre-defined number of microseconds
1123 * @param device pointer to struct ulink identifying ULINK driver instance.
1124 * @param us the number microseconds to sleep.
1125 * @return on success: ERROR_OK
1126 * @return on failure: ERROR_FAIL
1128 int ulink_append_sleep_cmd(struct ulink
*device
, uint32_t us
)
1130 struct ulink_cmd
*cmd
= calloc(1, sizeof(struct ulink_cmd
));
1136 cmd
->id
= CMD_SLEEP_US
;
1138 /* CMD_SLEEP_US has two OUT payload bytes and zero IN payload bytes */
1139 ret
= ulink_allocate_payload(cmd
, 2, PAYLOAD_DIRECTION_OUT
);
1141 if (ret
!= ERROR_OK
) {
1146 cmd
->payload_out
[0] = us
& 0x00ff;
1147 cmd
->payload_out
[1] = (us
>> 8) & 0x00ff;
1149 return ulink_append_queue(device
, cmd
);
1153 * Set TCK delay counters
1155 * @param device pointer to struct ulink identifying ULINK driver instance.
1156 * @param delay_scan_in delay count top value in jtag_slow_scan_in() function.
1157 * @param delay_scan_out delay count top value in jtag_slow_scan_out() function.
1158 * @param delay_scan_io delay count top value in jtag_slow_scan_io() function.
1159 * @param delay_tck delay count top value in jtag_clock_tck() function.
1160 * @param delay_tms delay count top value in jtag_slow_clock_tms() function.
1161 * @return on success: ERROR_OK
1162 * @return on failure: ERROR_FAIL
1164 int ulink_append_configure_tck_cmd(struct ulink
*device
, int delay_scan_in
,
1165 int delay_scan_out
, int delay_scan_io
, int delay_tck
, int delay_tms
)
1167 struct ulink_cmd
*cmd
= calloc(1, sizeof(struct ulink_cmd
));
1173 cmd
->id
= CMD_CONFIGURE_TCK_FREQ
;
1175 /* CMD_CONFIGURE_TCK_FREQ has five OUT payload bytes and zero
1176 * IN payload bytes */
1177 ret
= ulink_allocate_payload(cmd
, 5, PAYLOAD_DIRECTION_OUT
);
1178 if (ret
!= ERROR_OK
) {
1183 if (delay_scan_in
< 0)
1184 cmd
->payload_out
[0] = 0;
1186 cmd
->payload_out
[0] = (uint8_t)delay_scan_in
;
1188 if (delay_scan_out
< 0)
1189 cmd
->payload_out
[1] = 0;
1191 cmd
->payload_out
[1] = (uint8_t)delay_scan_out
;
1193 if (delay_scan_io
< 0)
1194 cmd
->payload_out
[2] = 0;
1196 cmd
->payload_out
[2] = (uint8_t)delay_scan_io
;
1199 cmd
->payload_out
[3] = 0;
1201 cmd
->payload_out
[3] = (uint8_t)delay_tck
;
1204 cmd
->payload_out
[4] = 0;
1206 cmd
->payload_out
[4] = (uint8_t)delay_tms
;
1208 return ulink_append_queue(device
, cmd
);
1212 * Turn on/off ULINK LEDs.
1214 * @param device pointer to struct ulink identifying ULINK driver instance.
1215 * @param led_state which LED(s) to turn on or off. The following bits
1216 * influence the LEDS:
1217 * - Bit 0: Turn COM LED on
1218 * - Bit 1: Turn RUN LED on
1219 * - Bit 2: Turn COM LED off
1220 * - Bit 3: Turn RUN LED off
1221 * If both the on-bit and the off-bit for the same LED is set, the LED is
1223 * @return on success: ERROR_OK
1224 * @return on failure: ERROR_FAIL
1226 int ulink_append_led_cmd(struct ulink
*device
, uint8_t led_state
)
1228 struct ulink_cmd
*cmd
= calloc(1, sizeof(struct ulink_cmd
));
1234 cmd
->id
= CMD_SET_LEDS
;
1236 /* CMD_SET_LEDS has one OUT payload byte and zero IN payload bytes */
1237 ret
= ulink_allocate_payload(cmd
, 1, PAYLOAD_DIRECTION_OUT
);
1238 if (ret
!= ERROR_OK
) {
1243 cmd
->payload_out
[0] = led_state
;
1245 return ulink_append_queue(device
, cmd
);
1249 * Test command. Used to check if the ULINK device is ready to accept new
1252 * @param device pointer to struct ulink identifying ULINK driver instance.
1253 * @return on success: ERROR_OK
1254 * @return on failure: ERROR_FAIL
1256 int ulink_append_test_cmd(struct ulink
*device
)
1258 struct ulink_cmd
*cmd
= calloc(1, sizeof(struct ulink_cmd
));
1266 /* CMD_TEST has one OUT payload byte and zero IN payload bytes */
1267 ret
= ulink_allocate_payload(cmd
, 1, PAYLOAD_DIRECTION_OUT
);
1268 if (ret
!= ERROR_OK
) {
1273 cmd
->payload_out
[0] = 0xAA;
1275 return ulink_append_queue(device
, cmd
);
1278 /****************** OpenULINK TCK frequency helper functions ******************/
1281 * Calculate delay values for a given TCK frequency.
1283 * The OpenULINK firmware uses five different speed values for different
1284 * commands. These speed values are calculated in these functions.
1286 * The five different commands which support variable TCK frequency are
1287 * implemented twice in the firmware:
1288 * 1. Maximum possible frequency without any artificial delay
1289 * 2. Variable frequency with artificial linear delay loop
1291 * To set the ULINK to maximum frequency, it is only neccessary to use the
1292 * corresponding command IDs. To set the ULINK to a lower frequency, the
1293 * delay loop top values have to be calculated first. Then, a
1294 * CMD_CONFIGURE_TCK_FREQ command needs to be sent to the ULINK device.
1296 * The delay values are described by linear equations:
1298 * (t = period, k = constant, x = delay value, d = constant)
1300 * Thus, the delay can be calculated as in the following equation:
1303 * The constants in these equations have been determined and validated by
1304 * measuring the frequency resulting from different delay values.
1306 * @param type for which command to calculate the delay value.
1307 * @param f TCK frequency for which to calculate the delay value in Hz.
1308 * @param delay where to store resulting delay value.
1309 * @return on success: ERROR_OK
1310 * @return on failure: ERROR_FAIL
1312 int ulink_calculate_delay(enum ulink_delay_type type
, long f
, int *delay
)
1316 /* Calculate period of requested TCK frequency */
1317 t
= 1.0 / (float)(f
);
1320 case DELAY_CLOCK_TCK
:
1321 x
= (t
- (float)(6E-6)) / (float)(4E-6);
1323 case DELAY_CLOCK_TMS
:
1324 x
= (t
- (float)(8.5E-6)) / (float)(4E-6);
1327 x
= (t
- (float)(8.8308E-6)) / (float)(4E-6);
1329 case DELAY_SCAN_OUT
:
1330 x
= (t
- (float)(1.0527E-5)) / (float)(4E-6);
1333 x
= (t
- (float)(1.3132E-5)) / (float)(4E-6);
1340 /* Check if the delay value is negative. This happens when a frequency is
1341 * requested that is too high for the delay loop implementation. In this
1342 * case, set delay value to zero. */
1346 /* We need to convert the exact delay value to an integer. Therefore, we
1347 * round the exact value UP to ensure that the resulting frequency is NOT
1348 * higher than the requested frequency. */
1351 /* Check if the value is within limits */
1355 *delay
= (int)x_ceil
;
1361 * Calculate frequency for a given delay value.
1363 * Similar to the #ulink_calculate_delay function, this function calculates the
1364 * TCK frequency for a given delay value by using linear equations of the form:
1366 * (t = period, k = constant, x = delay value, d = constant)
1368 * @param type for which command to calculate the delay value.
1369 * @param delay delay value for which to calculate the resulting TCK frequency.
1370 * @return the resulting TCK frequency
1372 static long ulink_calculate_frequency(enum ulink_delay_type type
, int delay
)
1380 case DELAY_CLOCK_TCK
:
1382 t
= (float)(2.666E-6);
1384 t
= (float)(4E-6) * (float)(delay
) + (float)(6E-6);
1386 case DELAY_CLOCK_TMS
:
1388 t
= (float)(5.666E-6);
1390 t
= (float)(4E-6) * (float)(delay
) + (float)(8.5E-6);
1394 t
= (float)(5.5E-6);
1396 t
= (float)(4E-6) * (float)(delay
) + (float)(8.8308E-6);
1398 case DELAY_SCAN_OUT
:
1400 t
= (float)(7.0E-6);
1402 t
= (float)(4E-6) * (float)(delay
) + (float)(1.0527E-5);
1406 t
= (float)(9.926E-6);
1408 t
= (float)(4E-6) * (float)(delay
) + (float)(1.3132E-5);
1415 return roundf(f_float
);
1418 /******************* Interface between OpenULINK and OpenOCD ******************/
1421 * Sets the end state follower (see interface.h) if \a endstate is a stable
1424 * @param endstate the state the end state follower should be set to.
1426 static void ulink_set_end_state(tap_state_t endstate
)
1428 if (tap_is_state_stable(endstate
))
1429 tap_set_end_state(endstate
);
1431 LOG_ERROR("BUG: %s is not a valid end state", tap_state_name(endstate
));
1437 * Move from the current TAP state to the current TAP end state.
1439 * @param device pointer to struct ulink identifying ULINK driver instance.
1440 * @return on success: ERROR_OK
1441 * @return on failure: ERROR_FAIL
1443 int ulink_queue_statemove(struct ulink
*device
)
1445 uint8_t tms_sequence
, tms_count
;
1448 if (tap_get_state() == tap_get_end_state()) {
1449 /* Do nothing if we are already there */
1453 tms_sequence
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
1454 tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1456 ret
= ulink_append_clock_tms_cmd(device
, tms_count
, tms_sequence
);
1458 if (ret
== ERROR_OK
)
1459 tap_set_state(tap_get_end_state());
1465 * Perform a scan operation on a JTAG register.
1467 * @param device pointer to struct ulink identifying ULINK driver instance.
1468 * @param cmd pointer to the command that shall be executed.
1469 * @return on success: ERROR_OK
1470 * @return on failure: ERROR_FAIL
1472 int ulink_queue_scan(struct ulink
*device
, struct jtag_command
*cmd
)
1474 uint32_t scan_size_bits
, scan_size_bytes
, bits_last_scan
;
1475 uint32_t scans_max_payload
, bytecount
;
1476 uint8_t *tdi_buffer_start
= NULL
, *tdi_buffer
= NULL
;
1477 uint8_t *tdo_buffer_start
= NULL
, *tdo_buffer
= NULL
;
1479 uint8_t first_tms_count
, first_tms_sequence
;
1480 uint8_t last_tms_count
, last_tms_sequence
;
1482 uint8_t tms_count_pause
, tms_sequence_pause
;
1483 uint8_t tms_count_resume
, tms_sequence_resume
;
1485 uint8_t tms_count_start
, tms_sequence_start
;
1486 uint8_t tms_count_end
, tms_sequence_end
;
1488 enum scan_type type
;
1491 /* Determine scan size */
1492 scan_size_bits
= jtag_scan_size(cmd
->cmd
.scan
);
1493 scan_size_bytes
= DIV_ROUND_UP(scan_size_bits
, 8);
1495 /* Determine scan type (IN/OUT/IO) */
1496 type
= jtag_scan_type(cmd
->cmd
.scan
);
1498 /* Determine number of scan commands with maximum payload */
1499 scans_max_payload
= scan_size_bytes
/ 58;
1501 /* Determine size of last shift command */
1502 bits_last_scan
= scan_size_bits
- (scans_max_payload
* 58 * 8);
1504 /* Allocate TDO buffer if required */
1505 if ((type
== SCAN_IN
) || (type
== SCAN_IO
)) {
1506 tdo_buffer_start
= calloc(sizeof(uint8_t), scan_size_bytes
);
1508 if (tdo_buffer_start
== NULL
)
1511 tdo_buffer
= tdo_buffer_start
;
1514 /* Fill TDI buffer if required */
1515 if ((type
== SCAN_OUT
) || (type
== SCAN_IO
)) {
1516 jtag_build_buffer(cmd
->cmd
.scan
, &tdi_buffer_start
);
1517 tdi_buffer
= tdi_buffer_start
;
1520 /* Get TAP state transitions */
1521 if (cmd
->cmd
.scan
->ir_scan
) {
1522 ulink_set_end_state(TAP_IRSHIFT
);
1523 first_tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1524 first_tms_sequence
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
1526 tap_set_state(TAP_IRSHIFT
);
1527 tap_set_end_state(cmd
->cmd
.scan
->end_state
);
1528 last_tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1529 last_tms_sequence
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
1531 /* TAP state transitions for split scans */
1532 tms_count_pause
= tap_get_tms_path_len(TAP_IRSHIFT
, TAP_IRPAUSE
);
1533 tms_sequence_pause
= tap_get_tms_path(TAP_IRSHIFT
, TAP_IRPAUSE
);
1534 tms_count_resume
= tap_get_tms_path_len(TAP_IRPAUSE
, TAP_IRSHIFT
);
1535 tms_sequence_resume
= tap_get_tms_path(TAP_IRPAUSE
, TAP_IRSHIFT
);
1537 ulink_set_end_state(TAP_DRSHIFT
);
1538 first_tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1539 first_tms_sequence
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
1541 tap_set_state(TAP_DRSHIFT
);
1542 tap_set_end_state(cmd
->cmd
.scan
->end_state
);
1543 last_tms_count
= tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1544 last_tms_sequence
= tap_get_tms_path(tap_get_state(), tap_get_end_state());
1546 /* TAP state transitions for split scans */
1547 tms_count_pause
= tap_get_tms_path_len(TAP_DRSHIFT
, TAP_DRPAUSE
);
1548 tms_sequence_pause
= tap_get_tms_path(TAP_DRSHIFT
, TAP_DRPAUSE
);
1549 tms_count_resume
= tap_get_tms_path_len(TAP_DRPAUSE
, TAP_DRSHIFT
);
1550 tms_sequence_resume
= tap_get_tms_path(TAP_DRPAUSE
, TAP_DRSHIFT
);
1553 /* Generate scan commands */
1554 bytecount
= scan_size_bytes
;
1555 while (bytecount
> 0) {
1556 if (bytecount
== scan_size_bytes
) {
1557 /* This is the first scan */
1558 tms_count_start
= first_tms_count
;
1559 tms_sequence_start
= first_tms_sequence
;
1561 /* Resume from previous scan */
1562 tms_count_start
= tms_count_resume
;
1563 tms_sequence_start
= tms_sequence_resume
;
1566 if (bytecount
> 58) { /* Full scan, at least one scan will follow */
1567 tms_count_end
= tms_count_pause
;
1568 tms_sequence_end
= tms_sequence_pause
;
1570 ret
= ulink_append_scan_cmd(device
,
1585 /* Update TDI and TDO buffer pointers */
1586 if (tdi_buffer_start
!= NULL
)
1588 if (tdo_buffer_start
!= NULL
)
1590 } else if (bytecount
== 58) { /* Full scan, no further scans */
1591 tms_count_end
= last_tms_count
;
1592 tms_sequence_end
= last_tms_sequence
;
1594 ret
= ulink_append_scan_cmd(device
,
1608 } else {/* Scan with less than maximum payload, no further scans */
1609 tms_count_end
= last_tms_count
;
1610 tms_sequence_end
= last_tms_sequence
;
1612 ret
= ulink_append_scan_cmd(device
,
1628 if (ret
!= ERROR_OK
) {
1629 free(tdi_buffer_start
);
1634 free(tdi_buffer_start
);
1636 /* Set current state to the end state requested by the command */
1637 tap_set_state(cmd
->cmd
.scan
->end_state
);
1643 * Move the TAP into the Test Logic Reset state.
1645 * @param device pointer to struct ulink identifying ULINK driver instance.
1646 * @param cmd pointer to the command that shall be executed.
1647 * @return on success: ERROR_OK
1648 * @return on failure: ERROR_FAIL
1650 int ulink_queue_tlr_reset(struct ulink
*device
, struct jtag_command
*cmd
)
1654 ret
= ulink_append_clock_tms_cmd(device
, 5, 0xff);
1656 if (ret
== ERROR_OK
)
1657 tap_set_state(TAP_RESET
);
1665 * Generate TCK clock cycles while remaining
1666 * in the Run-Test/Idle state.
1668 * @param device pointer to struct ulink identifying ULINK driver instance.
1669 * @param cmd pointer to the command that shall be executed.
1670 * @return on success: ERROR_OK
1671 * @return on failure: ERROR_FAIL
1673 int ulink_queue_runtest(struct ulink
*device
, struct jtag_command
*cmd
)
1677 /* Only perform statemove if the TAP currently isn't in the TAP_IDLE state */
1678 if (tap_get_state() != TAP_IDLE
) {
1679 ulink_set_end_state(TAP_IDLE
);
1680 ulink_queue_statemove(device
);
1683 /* Generate the clock cycles */
1684 ret
= ulink_append_clock_tck_cmd(device
, cmd
->cmd
.runtest
->num_cycles
);
1685 if (ret
!= ERROR_OK
)
1688 /* Move to end state specified in command */
1689 if (cmd
->cmd
.runtest
->end_state
!= tap_get_state()) {
1690 tap_set_end_state(cmd
->cmd
.runtest
->end_state
);
1691 ulink_queue_statemove(device
);
1698 * Execute a JTAG_RESET command
1700 * @param cmd pointer to the command that shall be executed.
1701 * @return on success: ERROR_OK
1702 * @return on failure: ERROR_FAIL
1704 int ulink_queue_reset(struct ulink
*device
, struct jtag_command
*cmd
)
1706 uint8_t low
= 0, high
= 0;
1708 if (cmd
->cmd
.reset
->trst
) {
1709 tap_set_state(TAP_RESET
);
1710 high
|= SIGNAL_TRST
;
1714 if (cmd
->cmd
.reset
->srst
)
1715 high
|= SIGNAL_RESET
;
1717 low
|= SIGNAL_RESET
;
1719 return ulink_append_set_signals_cmd(device
, low
, high
);
1723 * Move to one TAP state or several states in succession.
1725 * @param device pointer to struct ulink identifying ULINK driver instance.
1726 * @param cmd pointer to the command that shall be executed.
1727 * @return on success: ERROR_OK
1728 * @return on failure: ERROR_FAIL
1730 int ulink_queue_pathmove(struct ulink
*device
, struct jtag_command
*cmd
)
1732 int ret
, i
, num_states
, batch_size
, state_count
;
1734 uint8_t tms_sequence
;
1736 num_states
= cmd
->cmd
.pathmove
->num_states
;
1737 path
= cmd
->cmd
.pathmove
->path
;
1740 while (num_states
> 0) {
1743 /* Determine batch size */
1744 if (num_states
>= 8)
1747 batch_size
= num_states
;
1749 for (i
= 0; i
< batch_size
; i
++) {
1750 if (tap_state_transition(tap_get_state(), false) == path
[state_count
]) {
1751 /* Append '0' transition: clear bit 'i' in tms_sequence */
1752 buf_set_u32(&tms_sequence
, i
, 1, 0x0);
1753 } else if (tap_state_transition(tap_get_state(), true)
1754 == path
[state_count
]) {
1755 /* Append '1' transition: set bit 'i' in tms_sequence */
1756 buf_set_u32(&tms_sequence
, i
, 1, 0x1);
1758 /* Invalid state transition */
1759 LOG_ERROR("BUG: %s -> %s isn't a valid TAP state transition",
1760 tap_state_name(tap_get_state()),
1761 tap_state_name(path
[state_count
]));
1765 tap_set_state(path
[state_count
]);
1770 /* Append CLOCK_TMS command to OpenULINK command queue */
1772 "pathmove batch: count = %i, sequence = 0x%x", batch_size
, tms_sequence
);
1773 ret
= ulink_append_clock_tms_cmd(ulink_handle
, batch_size
, tms_sequence
);
1774 if (ret
!= ERROR_OK
)
1782 * Sleep for a specific amount of time.
1784 * @param device pointer to struct ulink identifying ULINK driver instance.
1785 * @param cmd pointer to the command that shall be executed.
1786 * @return on success: ERROR_OK
1787 * @return on failure: ERROR_FAIL
1789 int ulink_queue_sleep(struct ulink
*device
, struct jtag_command
*cmd
)
1791 /* IMPORTANT! Due to the time offset in command execution introduced by
1792 * command queueing, this needs to be implemented in the ULINK device */
1793 return ulink_append_sleep_cmd(device
, cmd
->cmd
.sleep
->us
);
1797 * Generate TCK cycles while remaining in a stable state.
1799 * @param device pointer to struct ulink identifying ULINK driver instance.
1800 * @param cmd pointer to the command that shall be executed.
1802 int ulink_queue_stableclocks(struct ulink
*device
, struct jtag_command
*cmd
)
1805 unsigned num_cycles
;
1807 if (!tap_is_state_stable(tap_get_state())) {
1808 LOG_ERROR("JTAG_STABLECLOCKS: state not stable");
1812 num_cycles
= cmd
->cmd
.stableclocks
->num_cycles
;
1814 /* TMS stays either high (Test Logic Reset state) or low (all other states) */
1815 if (tap_get_state() == TAP_RESET
)
1816 ret
= ulink_append_set_signals_cmd(device
, 0, SIGNAL_TMS
);
1818 ret
= ulink_append_set_signals_cmd(device
, SIGNAL_TMS
, 0);
1820 if (ret
!= ERROR_OK
)
1823 while (num_cycles
> 0) {
1824 if (num_cycles
> 0xFFFF) {
1825 /* OpenULINK CMD_CLOCK_TCK can generate up to 0xFFFF (uint16_t) cycles */
1826 ret
= ulink_append_clock_tck_cmd(device
, 0xFFFF);
1827 num_cycles
-= 0xFFFF;
1829 ret
= ulink_append_clock_tck_cmd(device
, num_cycles
);
1833 if (ret
!= ERROR_OK
)
1841 * Post-process JTAG_SCAN command
1843 * @param ulink_cmd pointer to OpenULINK command that shall be processed.
1844 * @return on success: ERROR_OK
1845 * @return on failure: ERROR_FAIL
1847 int ulink_post_process_scan(struct ulink_cmd
*ulink_cmd
)
1849 struct jtag_command
*cmd
= ulink_cmd
->cmd_origin
;
1852 switch (jtag_scan_type(cmd
->cmd
.scan
)) {
1855 ret
= jtag_read_buffer(ulink_cmd
->payload_in_start
, cmd
->cmd
.scan
);
1858 /* Nothing to do for OUT scans */
1862 LOG_ERROR("BUG: ulink_post_process_scan() encountered an unknown"
1872 * Perform post-processing of commands after OpenULINK queue has been executed.
1874 * @param device pointer to struct ulink identifying ULINK driver instance.
1875 * @return on success: ERROR_OK
1876 * @return on failure: ERROR_FAIL
1878 int ulink_post_process_queue(struct ulink
*device
)
1880 struct ulink_cmd
*current
;
1881 struct jtag_command
*openocd_cmd
;
1884 current
= device
->queue_start
;
1886 while (current
!= NULL
) {
1887 openocd_cmd
= current
->cmd_origin
;
1889 /* Check if a corresponding OpenOCD command is stored for this
1890 * OpenULINK command */
1891 if ((current
->needs_postprocessing
== true) && (openocd_cmd
!= NULL
)) {
1892 switch (openocd_cmd
->type
) {
1894 ret
= ulink_post_process_scan(current
);
1896 case JTAG_TLR_RESET
:
1901 case JTAG_STABLECLOCKS
:
1902 /* Nothing to do for these commands */
1907 LOG_ERROR("BUG: ulink_post_process_queue() encountered unknown JTAG "
1912 if (ret
!= ERROR_OK
)
1916 current
= current
->next
;
1922 /**************************** JTAG driver functions ***************************/
1925 * Executes the JTAG Command Queue.
1927 * This is done in three stages: First, all OpenOCD commands are processed into
1928 * queued OpenULINK commands. Next, the OpenULINK command queue is sent to the
1929 * ULINK device and data received from the ULINK device is cached. Finally,
1930 * the post-processing function writes back data to the corresponding OpenOCD
1933 * @return on success: ERROR_OK
1934 * @return on failure: ERROR_FAIL
1936 static int ulink_execute_queue(void)
1938 struct jtag_command
*cmd
= jtag_command_queue
;
1942 switch (cmd
->type
) {
1944 ret
= ulink_queue_scan(ulink_handle
, cmd
);
1946 case JTAG_TLR_RESET
:
1947 ret
= ulink_queue_tlr_reset(ulink_handle
, cmd
);
1950 ret
= ulink_queue_runtest(ulink_handle
, cmd
);
1953 ret
= ulink_queue_reset(ulink_handle
, cmd
);
1956 ret
= ulink_queue_pathmove(ulink_handle
, cmd
);
1959 ret
= ulink_queue_sleep(ulink_handle
, cmd
);
1961 case JTAG_STABLECLOCKS
:
1962 ret
= ulink_queue_stableclocks(ulink_handle
, cmd
);
1966 LOG_ERROR("BUG: encountered unknown JTAG command type");
1970 if (ret
!= ERROR_OK
)
1976 if (ulink_handle
->commands_in_queue
> 0) {
1977 ret
= ulink_execute_queued_commands(ulink_handle
, USB_TIMEOUT
);
1978 if (ret
!= ERROR_OK
)
1981 ret
= ulink_post_process_queue(ulink_handle
);
1982 if (ret
!= ERROR_OK
)
1985 ulink_clear_queue(ulink_handle
);
1992 * Set the TCK frequency of the ULINK adapter.
1994 * @param khz desired JTAG TCK frequency.
1995 * @param jtag_speed where to store corresponding adapter-specific speed value.
1996 * @return on success: ERROR_OK
1997 * @return on failure: ERROR_FAIL
1999 static int ulink_khz(int khz
, int *jtag_speed
)
2004 LOG_ERROR("RCLK not supported");
2008 /* CLOCK_TCK commands are decoupled from others. Therefore, the frequency
2009 * setting can be done independently from all other commands. */
2011 ulink_handle
->delay_clock_tck
= -1;
2013 ret
= ulink_calculate_delay(DELAY_CLOCK_TCK
, khz
* 1000,
2014 &ulink_handle
->delay_clock_tck
);
2015 if (ret
!= ERROR_OK
)
2019 /* SCAN_{IN,OUT,IO} commands invoke CLOCK_TMS commands. Therefore, if the
2020 * requested frequency goes below the maximum frequency for SLOW_CLOCK_TMS
2021 * commands, all SCAN commands MUST also use the variable frequency
2022 * implementation! */
2024 ulink_handle
->delay_clock_tms
= -1;
2025 ulink_handle
->delay_scan_in
= -1;
2026 ulink_handle
->delay_scan_out
= -1;
2027 ulink_handle
->delay_scan_io
= -1;
2029 ret
= ulink_calculate_delay(DELAY_CLOCK_TMS
, khz
* 1000,
2030 &ulink_handle
->delay_clock_tms
);
2031 if (ret
!= ERROR_OK
)
2034 ret
= ulink_calculate_delay(DELAY_SCAN_IN
, khz
* 1000,
2035 &ulink_handle
->delay_scan_in
);
2036 if (ret
!= ERROR_OK
)
2039 ret
= ulink_calculate_delay(DELAY_SCAN_OUT
, khz
* 1000,
2040 &ulink_handle
->delay_scan_out
);
2041 if (ret
!= ERROR_OK
)
2044 ret
= ulink_calculate_delay(DELAY_SCAN_IO
, khz
* 1000,
2045 &ulink_handle
->delay_scan_io
);
2046 if (ret
!= ERROR_OK
)
2050 LOG_DEBUG_IO("ULINK TCK setup: delay_tck = %i (%li Hz),",
2051 ulink_handle
->delay_clock_tck
,
2052 ulink_calculate_frequency(DELAY_CLOCK_TCK
, ulink_handle
->delay_clock_tck
));
2053 LOG_DEBUG_IO(" delay_tms = %i (%li Hz),",
2054 ulink_handle
->delay_clock_tms
,
2055 ulink_calculate_frequency(DELAY_CLOCK_TMS
, ulink_handle
->delay_clock_tms
));
2056 LOG_DEBUG_IO(" delay_scan_in = %i (%li Hz),",
2057 ulink_handle
->delay_scan_in
,
2058 ulink_calculate_frequency(DELAY_SCAN_IN
, ulink_handle
->delay_scan_in
));
2059 LOG_DEBUG_IO(" delay_scan_out = %i (%li Hz),",
2060 ulink_handle
->delay_scan_out
,
2061 ulink_calculate_frequency(DELAY_SCAN_OUT
, ulink_handle
->delay_scan_out
));
2062 LOG_DEBUG_IO(" delay_scan_io = %i (%li Hz),",
2063 ulink_handle
->delay_scan_io
,
2064 ulink_calculate_frequency(DELAY_SCAN_IO
, ulink_handle
->delay_scan_io
));
2066 /* Configure the ULINK device with the new delay values */
2067 ret
= ulink_append_configure_tck_cmd(ulink_handle
,
2068 ulink_handle
->delay_scan_in
,
2069 ulink_handle
->delay_scan_out
,
2070 ulink_handle
->delay_scan_io
,
2071 ulink_handle
->delay_clock_tck
,
2072 ulink_handle
->delay_clock_tms
);
2074 if (ret
!= ERROR_OK
)
2083 * Set the TCK frequency of the ULINK adapter.
2085 * Because of the way the TCK frequency is set up in the OpenULINK firmware,
2086 * there are five different speed settings. To simplify things, the
2087 * adapter-specific speed setting value is identical to the TCK frequency in
2090 * @param speed desired adapter-specific speed value.
2091 * @return on success: ERROR_OK
2092 * @return on failure: ERROR_FAIL
2094 static int ulink_speed(int speed
)
2098 return ulink_khz(speed
, &dummy
);
2102 * Convert adapter-specific speed value to corresponding TCK frequency in kHz.
2104 * Because of the way the TCK frequency is set up in the OpenULINK firmware,
2105 * there are five different speed settings. To simplify things, the
2106 * adapter-specific speed setting value is identical to the TCK frequency in
2109 * @param speed adapter-specific speed value.
2110 * @param khz where to store corresponding TCK frequency in kHz.
2111 * @return on success: ERROR_OK
2112 * @return on failure: ERROR_FAIL
2114 static int ulink_speed_div(int speed
, int *khz
)
2122 * Initiates the firmware download to the ULINK adapter and prepares
2125 * @return on success: ERROR_OK
2126 * @return on failure: ERROR_FAIL
2128 static int ulink_init(void)
2130 int ret
, transferred
;
2131 char str_manufacturer
[20];
2132 bool download_firmware
= false;
2133 unsigned char *dummy
;
2134 uint8_t input_signals
, output_signals
;
2136 ulink_handle
= calloc(1, sizeof(struct ulink
));
2137 if (ulink_handle
== NULL
)
2140 libusb_init(&ulink_handle
->libusb_ctx
);
2142 ret
= ulink_usb_open(&ulink_handle
);
2143 if (ret
!= ERROR_OK
) {
2144 LOG_ERROR("Could not open ULINK device");
2146 ulink_handle
= NULL
;
2150 /* Get String Descriptor to determine if firmware needs to be loaded */
2151 ret
= libusb_get_string_descriptor_ascii(ulink_handle
->usb_device_handle
, 1, (unsigned char *)str_manufacturer
, 20);
2153 /* Could not get descriptor -> Unconfigured or original Keil firmware */
2154 download_firmware
= true;
2156 /* We got a String Descriptor, check if it is the correct one */
2157 if (strncmp(str_manufacturer
, "OpenULINK", 9) != 0)
2158 download_firmware
= true;
2161 if (download_firmware
== true) {
2162 LOG_INFO("Loading OpenULINK firmware. This is reversible by power-cycling"
2164 ret
= ulink_load_firmware_and_renumerate(&ulink_handle
,
2165 ULINK_FIRMWARE_FILE
, ULINK_RENUMERATION_DELAY
);
2166 if (ret
!= ERROR_OK
) {
2167 LOG_ERROR("Could not download firmware and re-numerate ULINK");
2169 ulink_handle
= NULL
;
2173 LOG_INFO("ULINK device is already running OpenULINK firmware");
2175 /* Initialize OpenULINK command queue */
2176 ulink_clear_queue(ulink_handle
);
2178 /* Issue one test command with short timeout */
2179 ret
= ulink_append_test_cmd(ulink_handle
);
2180 if (ret
!= ERROR_OK
)
2183 ret
= ulink_execute_queued_commands(ulink_handle
, 200);
2184 if (ret
!= ERROR_OK
) {
2185 /* Sending test command failed. The ULINK device may be forever waiting for
2186 * the host to fetch an USB Bulk IN packet (e. g. OpenOCD crashed or was
2187 * shut down by the user via Ctrl-C. Try to retrieve this Bulk IN packet. */
2188 dummy
= calloc(64, sizeof(uint8_t));
2190 ret
= libusb_bulk_transfer(ulink_handle
->usb_device_handle
, (2 | LIBUSB_ENDPOINT_IN
),
2191 dummy
, 64, &transferred
, 200);
2195 if (ret
!= 0 || transferred
== 0) {
2196 /* Bulk IN transfer failed -> unrecoverable error condition */
2197 LOG_ERROR("Cannot communicate with ULINK device. Disconnect ULINK from "
2198 "the USB port and re-connect, then re-run OpenOCD");
2200 ulink_handle
= NULL
;
2203 #ifdef _DEBUG_USB_COMMS_
2205 /* Successfully received Bulk IN packet -> continue */
2206 LOG_INFO("Recovered from lost Bulk IN packet");
2210 ulink_clear_queue(ulink_handle
);
2212 ulink_append_get_signals_cmd(ulink_handle
);
2213 ulink_execute_queued_commands(ulink_handle
, 200);
2215 /* Post-process the single CMD_GET_SIGNALS command */
2216 input_signals
= ulink_handle
->queue_start
->payload_in
[0];
2217 output_signals
= ulink_handle
->queue_start
->payload_in
[1];
2219 ulink_print_signal_states(input_signals
, output_signals
);
2221 ulink_clear_queue(ulink_handle
);
2227 * Closes the USB handle for the ULINK device.
2229 * @return on success: ERROR_OK
2230 * @return on failure: ERROR_FAIL
2232 static int ulink_quit(void)
2236 ret
= ulink_usb_close(&ulink_handle
);
2243 * Set a custom path to ULINK firmware image and force downloading to ULINK.
2245 COMMAND_HANDLER(ulink_download_firmware_handler
)
2250 return ERROR_COMMAND_SYNTAX_ERROR
;
2253 LOG_INFO("Downloading ULINK firmware image %s", CMD_ARGV
[0]);
2255 /* Download firmware image in CMD_ARGV[0] */
2256 ret
= ulink_load_firmware_and_renumerate(&ulink_handle
, CMD_ARGV
[0],
2257 ULINK_RENUMERATION_DELAY
);
2262 /*************************** Command Registration **************************/
2264 static const struct command_registration ulink_command_handlers
[] = {
2266 .name
= "ulink_download_firmware",
2267 .handler
= &ulink_download_firmware_handler
,
2268 .mode
= COMMAND_EXEC
,
2269 .help
= "download firmware image to ULINK device",
2270 .usage
= "path/to/ulink_firmware.hex",
2272 COMMAND_REGISTRATION_DONE
,
2275 struct jtag_interface ulink_interface
= {
2278 .commands
= ulink_command_handlers
,
2279 .transports
= jtag_only
,
2281 .execute_queue
= ulink_execute_queue
,
2283 .speed
= ulink_speed
,
2284 .speed_div
= ulink_speed_div
,