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