OpenOCD
ulink.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2011-2013 by Martin Schmoelzer *
5  * <martin.schmoelzer@student.tuwien.ac.at> *
6  ***************************************************************************/
7 
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11 
12 #include <math.h>
13 #include "helper/system.h"
14 #include <jtag/interface.h>
15 #include <jtag/commands.h>
16 #include <target/image.h>
17 #include <libusb.h>
18 #include "libusb_helper.h"
20 
23 #define ULINK_VID 0xC251
24 
27 #define ULINK_PID 0x2710
28 
31 #define CPUCS_REG 0x7F92
32 
34 #define REQUEST_FIRMWARE_LOAD 0xA0
35 
37 #define CPU_RESET 0x01
38 
40 #define CPU_START 0x00
41 
43 #define FIRMWARE_ADDR 0x0000
44 
46 #define USB_INTERFACE 0
47 
49 #define ULINK_RENUMERATION_DELAY 1500000
50 
52 #define ULINK_FIRMWARE_FILE PKGDATADIR "/OpenULINK/ulink_firmware.hex"
53 
55 #define SECTION_BUFFERSIZE 8192
56 
58 #define SPLIT_SCAN_THRESHOLD 10
59 
61 enum ulink_type {
65 
68 
71 
73  ULINK_ME
74 };
75 
79 };
80 
87 };
88 
111 struct ulink_cmd {
112  uint8_t id;
114  uint8_t *payload_out;
117  uint8_t *payload_in_start;
118  uint8_t *payload_in;
119  uint8_t payload_in_size;
123 
126 
129 
130  struct ulink_cmd *next;
131 };
132 
134 struct ulink {
135  struct libusb_context *libusb_ctx;
136  struct libusb_device_handle *usb_device_handle;
137  enum ulink_type type;
138 
139  unsigned int ep_in;
140  unsigned int ep_out;
151 };
152 
153 /**************************** Function Prototypes *****************************/
154 
155 /* USB helper functions */
156 static int ulink_usb_open(struct ulink **device);
157 static int ulink_usb_close(struct ulink **device);
158 
159 /* ULINK MCU (Cypress EZ-USB) specific functions */
160 static int ulink_cpu_reset(struct ulink *device, unsigned char reset_bit);
161 static int ulink_load_firmware_and_renumerate(struct ulink **device, const char *filename,
162  uint32_t delay);
163 static int ulink_load_firmware(struct ulink *device, const char *filename);
164 static int ulink_write_firmware_section(struct ulink *device,
165  struct image *firmware_image, int section_index);
166 
167 /* Generic helper functions */
168 static void ulink_print_signal_states(uint8_t input_signals, uint8_t output_signals);
169 
170 /* OpenULINK command generation helper functions */
171 static int ulink_allocate_payload(struct ulink_cmd *ulink_cmd, int size,
173 
174 /* OpenULINK command queue helper functions */
175 static int ulink_get_queue_size(struct ulink *device,
177 static void ulink_clear_queue(struct ulink *device);
178 static int ulink_append_queue(struct ulink *device, struct ulink_cmd *ulink_cmd);
179 static int ulink_execute_queued_commands(struct ulink *device, int timeout);
180 
181 static void ulink_print_queue(struct ulink *device);
182 
183 static int ulink_append_scan_cmd(struct ulink *device,
184  enum scan_type scan_type,
185  int scan_size_bits,
186  uint8_t *tdi,
187  uint8_t *tdo_start,
188  uint8_t *tdo,
189  uint8_t tms_count_start,
190  uint8_t tms_sequence_start,
191  uint8_t tms_count_end,
192  uint8_t tms_sequence_end,
193  struct jtag_command *origin,
194  bool postprocess);
195 static int ulink_append_clock_tms_cmd(struct ulink *device, uint8_t count,
196  uint8_t sequence);
197 static int ulink_append_clock_tck_cmd(struct ulink *device, uint16_t count);
198 static int ulink_append_get_signals_cmd(struct ulink *device);
199 static int ulink_append_set_signals_cmd(struct ulink *device, uint8_t low,
200  uint8_t high);
201 static int ulink_append_sleep_cmd(struct ulink *device, uint32_t us);
202 static int ulink_append_configure_tck_cmd(struct ulink *device,
203  int delay_scan_in,
204  int delay_scan_out,
205  int delay_scan_io,
206  int delay_tck,
207  int delay_tms);
208 static int __attribute__((unused)) ulink_append_led_cmd(struct ulink *device, uint8_t led_state);
209 static int ulink_append_test_cmd(struct ulink *device);
210 
211 /* OpenULINK TCK frequency helper functions */
212 static int ulink_calculate_delay(enum ulink_delay_type type, long f, int *delay);
213 
214 /* Interface between OpenULINK and OpenOCD */
215 static void ulink_set_end_state(enum tap_state endstate);
216 static int ulink_queue_statemove(struct ulink *device);
217 
218 static int ulink_queue_scan(struct ulink *device, struct jtag_command *cmd);
219 static int ulink_queue_tlr_reset(struct ulink *device, struct jtag_command *cmd);
220 static int ulink_queue_runtest(struct ulink *device, struct jtag_command *cmd);
221 static int ulink_queue_reset(struct ulink *device, struct jtag_command *cmd);
222 static int ulink_queue_pathmove(struct ulink *device, struct jtag_command *cmd);
223 static int ulink_queue_sleep(struct ulink *device, struct jtag_command *cmd);
224 static int ulink_queue_stableclocks(struct ulink *device, struct jtag_command *cmd);
225 
226 static int ulink_post_process_scan(struct ulink_cmd *ulink_cmd);
227 static int ulink_post_process_queue(struct ulink *device);
228 
229 /* adapter driver functions */
230 static int ulink_execute_queue(struct jtag_command *cmd_queue);
231 static int ulink_khz(int khz, int *jtag_speed);
232 static int ulink_speed(int speed);
233 static int ulink_speed_div(int speed, int *khz);
234 static int ulink_init(void);
235 static int ulink_quit(void);
236 
237 /****************************** Global Variables ******************************/
238 
239 static struct ulink *ulink_handle;
240 
241 /**************************** USB helper functions ****************************/
242 
252 static int ulink_usb_open(struct ulink **device)
253 {
254  ssize_t num_devices, i;
255  bool found;
256  struct libusb_device **usb_devices;
257  struct libusb_device_descriptor usb_desc;
258  struct libusb_device_handle *usb_device_handle;
259 
260  num_devices = libusb_get_device_list((*device)->libusb_ctx, &usb_devices);
261 
262  if (num_devices <= 0)
263  return ERROR_FAIL;
264 
265  found = false;
266  for (i = 0; i < num_devices; i++) {
267  if (libusb_get_device_descriptor(usb_devices[i], &usb_desc) != 0)
268  continue;
269  else if (usb_desc.idVendor == ULINK_VID && usb_desc.idProduct == ULINK_PID) {
270  found = true;
271  break;
272  }
273  }
274 
275  if (!found)
276  return ERROR_FAIL;
277 
278  if (libusb_open(usb_devices[i], &usb_device_handle) != 0)
279  return ERROR_FAIL;
280  libusb_free_device_list(usb_devices, 1);
281 
282  (*device)->usb_device_handle = usb_device_handle;
283  (*device)->type = ULINK_1;
284 
285  return ERROR_OK;
286 }
287 
295 static int ulink_usb_close(struct ulink **device)
296 {
297  if (libusb_release_interface((*device)->usb_device_handle, 0) != 0)
298  return ERROR_FAIL;
299 
300  libusb_close((*device)->usb_device_handle);
301 
302  (*device)->usb_device_handle = NULL;
303 
304  return ERROR_OK;
305 }
306 
307 /******************* ULINK CPU (EZ-USB) specific functions ********************/
308 
318 static int ulink_cpu_reset(struct ulink *device, unsigned char reset_bit)
319 {
320  int ret;
321 
322  ret = libusb_control_transfer(device->usb_device_handle,
323  (LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE),
325 
326  /* usb_control_msg() returns the number of bytes transferred during the
327  * DATA stage of the control transfer - must be exactly 1 in this case! */
328  if (ret != 1)
329  return ERROR_FAIL;
330  return ERROR_OK;
331 }
332 
346  const char *filename, uint32_t delay)
347 {
348  int ret;
349 
350  /* Basic process: After downloading the firmware, the ULINK will disconnect
351  * itself and re-connect after a short amount of time so we have to close
352  * the handle and re-enumerate USB devices */
353 
354  ret = ulink_load_firmware(*device, filename);
355  if (ret != ERROR_OK)
356  return ret;
357 
358  ret = ulink_usb_close(device);
359  if (ret != ERROR_OK)
360  return ret;
361 
362  usleep(delay);
363 
364  ret = ulink_usb_open(device);
365  if (ret != ERROR_OK)
366  return ret;
367 
368  return ERROR_OK;
369 }
370 
381 static int ulink_load_firmware(struct ulink *device, const char *filename)
382 {
383  struct image ulink_firmware_image;
384  int ret;
385 
387  if (ret != ERROR_OK) {
388  LOG_ERROR("Could not halt ULINK CPU");
389  return ret;
390  }
391 
392  ulink_firmware_image.base_address = 0;
393  ulink_firmware_image.base_address_set = false;
394 
395  ret = image_open(&ulink_firmware_image, filename, "ihex");
396  if (ret != ERROR_OK) {
397  LOG_ERROR("Could not load firmware image");
398  return ret;
399  }
400 
401  /* Download all sections in the image to ULINK */
402  for (unsigned int i = 0; i < ulink_firmware_image.num_sections; i++) {
403  ret = ulink_write_firmware_section(device, &ulink_firmware_image, i);
404  if (ret != ERROR_OK)
405  return ret;
406  }
407 
408  image_close(&ulink_firmware_image);
409 
411  if (ret != ERROR_OK) {
412  LOG_ERROR("Could not restart ULINK CPU");
413  return ret;
414  }
415 
416  return ERROR_OK;
417 }
418 
431  struct image *firmware_image, int section_index)
432 {
433  uint16_t addr, size, bytes_remaining, chunk_size;
434  uint8_t data[SECTION_BUFFERSIZE];
435  uint8_t *data_ptr = data;
436  size_t size_read;
437  int ret;
438 
439  size = (uint16_t)firmware_image->sections[section_index].size;
440  addr = (uint16_t)firmware_image->sections[section_index].base_address;
441 
442  LOG_DEBUG("section %02i at addr 0x%04x (size 0x%04x)", section_index, addr,
443  size);
444 
445  /* Copy section contents to local buffer */
446  ret = image_read_section(firmware_image, section_index, 0, size, data,
447  &size_read);
448 
449  if ((ret != ERROR_OK) || (size_read != size)) {
450  /* Propagating the return code would return '0' (misleadingly indicating
451  * successful execution of the function) if only the size check fails. */
452  return ERROR_FAIL;
453  }
454 
455  bytes_remaining = size;
456 
457  /* Send section data in chunks of up to 64 bytes to ULINK */
458  while (bytes_remaining > 0) {
459  if (bytes_remaining > 64)
460  chunk_size = 64;
461  else
462  chunk_size = bytes_remaining;
463 
464  ret = libusb_control_transfer(device->usb_device_handle,
465  (LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE),
466  REQUEST_FIRMWARE_LOAD, addr, FIRMWARE_ADDR, (unsigned char *)data_ptr,
467  chunk_size, LIBUSB_TIMEOUT_MS);
468 
469  if (ret != (int)chunk_size) {
470  /* Abort if libusb sent less data than requested */
471  return ERROR_FAIL;
472  }
473 
474  bytes_remaining -= chunk_size;
475  addr += chunk_size;
476  data_ptr += chunk_size;
477  }
478 
479  return ERROR_OK;
480 }
481 
482 /************************** Generic helper functions **************************/
483 
490 static void ulink_print_signal_states(uint8_t input_signals, uint8_t output_signals)
491 {
492  LOG_INFO("ULINK signal states: TDI: %i, TDO: %i, TMS: %i, TCK: %i, TRST: %i,"
493  " SRST: %i",
494  (output_signals & SIGNAL_TDI ? 1 : 0),
495  (input_signals & SIGNAL_TDO ? 1 : 0),
496  (output_signals & SIGNAL_TMS ? 1 : 0),
497  (output_signals & SIGNAL_TCK ? 1 : 0),
498  (output_signals & SIGNAL_TRST ? 0 : 1), /* Inverted by hardware */
499  (output_signals & SIGNAL_RESET ? 0 : 1)); /* Inverted by hardware */
500 }
501 
502 /**************** OpenULINK command generation helper functions ***************/
503 
515 {
516  uint8_t *payload;
517 
518  payload = calloc(size, sizeof(uint8_t));
519 
520  if (!payload) {
521  LOG_ERROR("Could not allocate OpenULINK command payload: out of memory");
522  return ERROR_FAIL;
523  }
524 
525  switch (direction) {
527  if (ulink_cmd->payload_out) {
528  LOG_ERROR("BUG: Duplicate payload allocation for OpenULINK command");
529  free(payload);
530  return ERROR_FAIL;
531  }
532  ulink_cmd->payload_out = payload;
534  break;
537  LOG_ERROR("BUG: Duplicate payload allocation for OpenULINK command");
538  free(payload);
539  return ERROR_FAIL;
540  }
541  ulink_cmd->payload_in_start = payload;
542  ulink_cmd->payload_in = payload;
544 
545  /* By default, free payload_in_start in ulink_clear_queue(). Commands
546  * that do not want this behavior (e.g. split scans) must turn it off
547  * separately! */
549  break;
550  }
551 
552  return ERROR_OK;
553 }
554 
555 /****************** OpenULINK command queue helper functions ******************/
556 
565 static int ulink_get_queue_size(struct ulink *device,
567 {
568  struct ulink_cmd *current = device->queue_start;
569  int sum = 0;
570 
571  while (current) {
572  switch (direction) {
574  sum += current->payload_out_size + 1; /* + 1 byte for Command ID */
575  break;
577  sum += current->payload_in_size;
578  break;
579  }
580 
581  current = current->next;
582  }
583 
584  return sum;
585 }
586 
592 static void ulink_clear_queue(struct ulink *device)
593 {
594  struct ulink_cmd *current = device->queue_start;
595  struct ulink_cmd *next = NULL;
596 
597  while (current) {
598  /* Save pointer to next element */
599  next = current->next;
600 
601  /* Free payloads: OUT payload can be freed immediately */
602  free(current->payload_out);
603  current->payload_out = NULL;
604 
605  /* IN payload MUST be freed ONLY if no other commands use the
606  * payload_in_start buffer */
607  if (current->free_payload_in_start) {
608  free(current->payload_in_start);
609  current->payload_in_start = NULL;
610  current->payload_in = NULL;
611  }
612 
613  /* Free queue element */
614  free(current);
615 
616  /* Proceed with next element */
617  current = next;
618  }
619 
620  device->commands_in_queue = 0;
621  device->queue_start = NULL;
622  device->queue_end = NULL;
623 }
624 
634 static int ulink_append_queue(struct ulink *device, struct ulink_cmd *ulink_cmd)
635 {
636  int newsize_out, newsize_in;
637  int ret = ERROR_OK;
638 
641 
644 
645  /* Check if the current command can be appended to the queue */
646  if ((newsize_out > 64) || (newsize_in > 64)) {
647  /* New command does not fit. Execute all commands in queue before starting
648  * new queue with the current command as first entry. */
650 
651  if (ret == ERROR_OK)
653 
654  if (ret == ERROR_OK)
656  }
657 
658  if (!device->queue_start) {
659  /* Queue was empty */
660  device->commands_in_queue = 1;
661 
662  device->queue_start = ulink_cmd;
663  device->queue_end = ulink_cmd;
664  } else {
665  /* There are already commands in the queue */
666  device->commands_in_queue++;
667 
668  device->queue_end->next = ulink_cmd;
669  device->queue_end = ulink_cmd;
670  }
671 
672  if (ret != ERROR_OK)
674 
675  return ret;
676 }
677 
687 {
688  struct ulink_cmd *current;
689  int ret, i, index_out, index_in, count_out, count_in, transferred;
690  uint8_t buffer[64];
691 
694 
695  index_out = 0;
696  count_out = 0;
697  count_in = 0;
698 
699  for (current = device->queue_start; current; current = current->next) {
700  /* Add command to packet */
701  buffer[index_out] = current->id;
702  index_out++;
703  count_out++;
704 
705  for (i = 0; i < current->payload_out_size; i++)
706  buffer[index_out + i] = current->payload_out[i];
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 = libusb_bulk_transfer(device->usb_device_handle, device->ep_out,
714  (unsigned char *)buffer, count_out, &transferred, timeout);
715  if (ret != 0)
716  return ERROR_FAIL;
717  if (transferred != count_out)
718  return ERROR_FAIL;
719 
720  /* Wait for response if commands contain IN payload data */
721  if (count_in > 0) {
722  ret = libusb_bulk_transfer(device->usb_device_handle, device->ep_in,
723  (unsigned char *)buffer, 64, &transferred, timeout);
724  if (ret != 0)
725  return ERROR_FAIL;
726  if (transferred != count_in)
727  return ERROR_FAIL;
728 
729  /* Write back IN payload data */
730  index_in = 0;
731  for (current = device->queue_start; current; current = current->next) {
732  for (i = 0; i < current->payload_in_size; i++) {
733  current->payload_in[i] = buffer[index_in];
734  index_in++;
735  }
736  }
737  }
738 
739  return ERROR_OK;
740 }
741 
748 static const char *ulink_cmd_id_string(uint8_t id)
749 {
750  switch (id) {
751  case CMD_SCAN_IN:
752  return "CMD_SCAN_IN";
753  case CMD_SLOW_SCAN_IN:
754  return "CMD_SLOW_SCAN_IN";
755  case CMD_SCAN_OUT:
756  return "CMD_SCAN_OUT";
757  case CMD_SLOW_SCAN_OUT:
758  return "CMD_SLOW_SCAN_OUT";
759  case CMD_SCAN_IO:
760  return "CMD_SCAN_IO";
761  case CMD_SLOW_SCAN_IO:
762  return "CMD_SLOW_SCAN_IO";
763  case CMD_CLOCK_TMS:
764  return "CMD_CLOCK_TMS";
765  case CMD_SLOW_CLOCK_TMS:
766  return "CMD_SLOW_CLOCK_TMS";
767  case CMD_CLOCK_TCK:
768  return "CMD_CLOCK_TCK";
769  case CMD_SLOW_CLOCK_TCK:
770  return "CMD_SLOW_CLOCK_TCK";
771  case CMD_SLEEP_US:
772  return "CMD_SLEEP_US";
773  case CMD_SLEEP_MS:
774  return "CMD_SLEEP_MS";
775  case CMD_GET_SIGNALS:
776  return "CMD_GET_SIGNALS";
777  case CMD_SET_SIGNALS:
778  return "CMD_SET_SIGNALS";
780  return "CMD_CONFIGURE_TCK_FREQ";
781  case CMD_SET_LEDS:
782  return "CMD_SET_LEDS";
783  case CMD_TEST:
784  return "CMD_TEST";
785  default:
786  return "CMD_UNKNOWN";
787  }
788 }
789 
796 {
797  int i;
798 
799  printf(" %-22s | OUT size = %i, bytes = 0x",
801 
802  for (i = 0; i < ulink_cmd->payload_out_size; i++)
803  printf("%02X ", ulink_cmd->payload_out[i]);
804  printf("\n | IN size = %i\n",
806 }
807 
813 static void ulink_print_queue(struct ulink *device)
814 {
815  struct ulink_cmd *current;
816 
817  printf("OpenULINK command queue:\n");
818 
819  for (current = device->queue_start; current; current = current->next)
820  ulink_print_command(current);
821 }
822 
854  int scan_size_bits, uint8_t *tdi, uint8_t *tdo_start, uint8_t *tdo,
855  uint8_t tms_count_start, uint8_t tms_sequence_start, uint8_t tms_count_end,
856  uint8_t tms_sequence_end, struct jtag_command *origin, bool postprocess)
857 {
858  struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd));
859  int ret, i, scan_size_bytes;
860  uint8_t bits_last_byte;
861 
862  if (!cmd)
863  return ERROR_FAIL;
864 
865  /* Check size of command. USB buffer can hold 64 bytes, 1 byte is command ID,
866  * 5 bytes are setup data -> 58 remaining payload bytes for TDI data */
867  if (scan_size_bits > (58 * 8)) {
868  LOG_ERROR("BUG: Tried to create CMD_SCAN_IO OpenULINK command with too"
869  " large payload");
870  free(cmd);
871  return ERROR_FAIL;
872  }
873 
874  scan_size_bytes = DIV_ROUND_UP(scan_size_bits, 8);
875 
876  bits_last_byte = scan_size_bits % 8;
877  if (bits_last_byte == 0)
878  bits_last_byte = 8;
879 
880  /* Allocate out_payload depending on scan type */
881  switch (scan_type) {
882  case SCAN_IN:
883  if (device->delay_scan_in < 0)
884  cmd->id = CMD_SCAN_IN;
885  else
886  cmd->id = CMD_SLOW_SCAN_IN;
888  break;
889  case SCAN_OUT:
890  if (device->delay_scan_out < 0)
891  cmd->id = CMD_SCAN_OUT;
892  else
893  cmd->id = CMD_SLOW_SCAN_OUT;
894  ret = ulink_allocate_payload(cmd, scan_size_bytes + 5, PAYLOAD_DIRECTION_OUT);
895  break;
896  case SCAN_IO:
897  if (device->delay_scan_io < 0)
898  cmd->id = CMD_SCAN_IO;
899  else
900  cmd->id = CMD_SLOW_SCAN_IO;
901  ret = ulink_allocate_payload(cmd, scan_size_bytes + 5, PAYLOAD_DIRECTION_OUT);
902  break;
903  default:
904  LOG_ERROR("BUG: %s() encountered an unknown scan type", __func__);
905  ret = ERROR_FAIL;
906  break;
907  }
908 
909  if (ret != ERROR_OK) {
910  free(cmd);
911  return ret;
912  }
913 
914  /* Build payload_out that is common to all scan types */
915  cmd->payload_out[0] = scan_size_bytes & 0xFF;
916  cmd->payload_out[1] = bits_last_byte & 0xFF;
917  cmd->payload_out[2] = ((tms_count_start & 0x0F) << 4) | (tms_count_end & 0x0F);
918  cmd->payload_out[3] = tms_sequence_start;
919  cmd->payload_out[4] = tms_sequence_end;
920 
921  /* Setup payload_out for types with OUT transfer */
922  if ((scan_type == SCAN_OUT) || (scan_type == SCAN_IO)) {
923  for (i = 0; i < scan_size_bytes; i++)
924  cmd->payload_out[i + 5] = tdi[i];
925  }
926 
927  /* Setup payload_in pointers for types with IN transfer */
928  if ((scan_type == SCAN_IN) || (scan_type == SCAN_IO)) {
929  cmd->payload_in_start = tdo_start;
930  cmd->payload_in = tdo;
931  cmd->payload_in_size = scan_size_bytes;
932  }
933 
934  cmd->needs_postprocessing = postprocess;
935  cmd->cmd_origin = origin;
936 
937  /* For scan commands, we free payload_in_start only when the command is
938  * the last in a series of split commands or a stand-alone command */
939  cmd->free_payload_in_start = postprocess;
940 
941  return ulink_append_queue(device, cmd);
942 }
943 
954 static int ulink_append_clock_tms_cmd(struct ulink *device, uint8_t count,
955  uint8_t sequence)
956 {
957  struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd));
958  int ret;
959 
960  if (!cmd)
961  return ERROR_FAIL;
962 
963  if (device->delay_clock_tms < 0)
964  cmd->id = CMD_CLOCK_TMS;
965  else
966  cmd->id = CMD_SLOW_CLOCK_TMS;
967 
968  /* CMD_CLOCK_TMS has two OUT payload bytes and zero IN payload bytes */
970  if (ret != ERROR_OK) {
971  free(cmd);
972  return ret;
973  }
974 
975  cmd->payload_out[0] = count;
976  cmd->payload_out[1] = sequence;
977 
978  return ulink_append_queue(device, cmd);
979 }
980 
991 static int ulink_append_clock_tck_cmd(struct ulink *device, uint16_t count)
992 {
993  struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd));
994  int ret;
995 
996  if (!cmd)
997  return ERROR_FAIL;
998 
999  if (device->delay_clock_tck < 0)
1000  cmd->id = CMD_CLOCK_TCK;
1001  else
1002  cmd->id = CMD_SLOW_CLOCK_TCK;
1003 
1004  /* CMD_CLOCK_TCK has two OUT payload bytes and zero IN payload bytes */
1006  if (ret != ERROR_OK) {
1007  free(cmd);
1008  return ret;
1009  }
1010 
1011  cmd->payload_out[0] = count & 0xff;
1012  cmd->payload_out[1] = (count >> 8) & 0xff;
1013 
1014  return ulink_append_queue(device, cmd);
1015 }
1016 
1025 {
1026  struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd));
1027  int ret;
1028 
1029  if (!cmd)
1030  return ERROR_FAIL;
1031 
1032  cmd->id = CMD_GET_SIGNALS;
1033  cmd->needs_postprocessing = true;
1034 
1035  /* CMD_GET_SIGNALS has two IN payload bytes */
1037 
1038  if (ret != ERROR_OK) {
1039  free(cmd);
1040  return ret;
1041  }
1042 
1043  return ulink_append_queue(device, cmd);
1044 }
1045 
1063 static int ulink_append_set_signals_cmd(struct ulink *device, uint8_t low,
1064  uint8_t high)
1065 {
1066  struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd));
1067  int ret;
1068 
1069  if (!cmd)
1070  return ERROR_FAIL;
1071 
1072  cmd->id = CMD_SET_SIGNALS;
1073 
1074  /* CMD_SET_SIGNALS has two OUT payload bytes and zero IN payload bytes */
1076 
1077  if (ret != ERROR_OK) {
1078  free(cmd);
1079  return ret;
1080  }
1081 
1082  cmd->payload_out[0] = low;
1083  cmd->payload_out[1] = high;
1084 
1085  return ulink_append_queue(device, cmd);
1086 }
1087 
1096 static int ulink_append_sleep_cmd(struct ulink *device, uint32_t us)
1097 {
1098  struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd));
1099  int ret;
1100 
1101  if (!cmd)
1102  return ERROR_FAIL;
1103 
1104  cmd->id = CMD_SLEEP_US;
1105 
1106  /* CMD_SLEEP_US has two OUT payload bytes and zero IN payload bytes */
1108 
1109  if (ret != ERROR_OK) {
1110  free(cmd);
1111  return ret;
1112  }
1113 
1114  cmd->payload_out[0] = us & 0x00ff;
1115  cmd->payload_out[1] = (us >> 8) & 0x00ff;
1116 
1117  return ulink_append_queue(device, cmd);
1118 }
1119 
1133  int delay_scan_out, int delay_scan_io, int delay_tck, int delay_tms)
1134 {
1135  struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd));
1136  int ret;
1137 
1138  if (!cmd)
1139  return ERROR_FAIL;
1140 
1142 
1143  /* CMD_CONFIGURE_TCK_FREQ has five OUT payload bytes and zero
1144  * IN payload bytes */
1146  if (ret != ERROR_OK) {
1147  free(cmd);
1148  return ret;
1149  }
1150 
1151  if (delay_scan_in < 0)
1152  cmd->payload_out[0] = 0;
1153  else
1154  cmd->payload_out[0] = (uint8_t)delay_scan_in;
1155 
1156  if (delay_scan_out < 0)
1157  cmd->payload_out[1] = 0;
1158  else
1159  cmd->payload_out[1] = (uint8_t)delay_scan_out;
1160 
1161  if (delay_scan_io < 0)
1162  cmd->payload_out[2] = 0;
1163  else
1164  cmd->payload_out[2] = (uint8_t)delay_scan_io;
1165 
1166  if (delay_tck < 0)
1167  cmd->payload_out[3] = 0;
1168  else
1169  cmd->payload_out[3] = (uint8_t)delay_tck;
1170 
1171  if (delay_tms < 0)
1172  cmd->payload_out[4] = 0;
1173  else
1174  cmd->payload_out[4] = (uint8_t)delay_tms;
1175 
1176  return ulink_append_queue(device, cmd);
1177 }
1178 
1194 static int ulink_append_led_cmd(struct ulink *device, uint8_t led_state)
1195 {
1196  struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd));
1197  int ret;
1198 
1199  if (!cmd)
1200  return ERROR_FAIL;
1201 
1202  cmd->id = CMD_SET_LEDS;
1203 
1204  /* CMD_SET_LEDS has one OUT payload byte and zero IN payload bytes */
1206  if (ret != ERROR_OK) {
1207  free(cmd);
1208  return ret;
1209  }
1210 
1211  cmd->payload_out[0] = led_state;
1212 
1213  return ulink_append_queue(device, cmd);
1214 }
1215 
1225 {
1226  struct ulink_cmd *cmd = calloc(1, sizeof(struct ulink_cmd));
1227  int ret;
1228 
1229  if (!cmd)
1230  return ERROR_FAIL;
1231 
1232  cmd->id = CMD_TEST;
1233 
1234  /* CMD_TEST has one OUT payload byte and zero IN payload bytes */
1236  if (ret != ERROR_OK) {
1237  free(cmd);
1238  return ret;
1239  }
1240 
1241  cmd->payload_out[0] = 0xAA;
1242 
1243  return ulink_append_queue(device, cmd);
1244 }
1245 
1246 /****************** OpenULINK TCK frequency helper functions ******************/
1247 
1280 static int ulink_calculate_delay(enum ulink_delay_type type, long f, int *delay)
1281 {
1282  float t, x, x_ceil;
1283 
1284  /* Calculate period of requested TCK frequency */
1285  t = 1.0 / (float)(f);
1286 
1287  switch (type) {
1288  case DELAY_CLOCK_TCK:
1289  x = (t - (float)(6E-6)) / (float)(4E-6);
1290  break;
1291  case DELAY_CLOCK_TMS:
1292  x = (t - (float)(8.5E-6)) / (float)(4E-6);
1293  break;
1294  case DELAY_SCAN_IN:
1295  x = (t - (float)(8.8308E-6)) / (float)(4E-6);
1296  break;
1297  case DELAY_SCAN_OUT:
1298  x = (t - (float)(1.0527E-5)) / (float)(4E-6);
1299  break;
1300  case DELAY_SCAN_IO:
1301  x = (t - (float)(1.3132E-5)) / (float)(4E-6);
1302  break;
1303  default:
1304  return ERROR_FAIL;
1305  break;
1306  }
1307 
1308  /* Check if the delay value is negative. This happens when a frequency is
1309  * requested that is too high for the delay loop implementation. In this
1310  * case, set delay value to zero. */
1311  if (x < 0)
1312  x = 0;
1313 
1314  /* We need to convert the exact delay value to an integer. Therefore, we
1315  * round the exact value UP to ensure that the resulting frequency is NOT
1316  * higher than the requested frequency. */
1317  x_ceil = ceilf(x);
1318 
1319  /* Check if the value is within limits */
1320  if (x_ceil > 255)
1321  return ERROR_FAIL;
1322 
1323  *delay = (int)x_ceil;
1324 
1325  return ERROR_OK;
1326 }
1327 
1341 {
1342  float t, f_float;
1343 
1344  if (delay > 255)
1345  return 0;
1346 
1347  switch (type) {
1348  case DELAY_CLOCK_TCK:
1349  if (delay < 0)
1350  t = (float)(2.666E-6);
1351  else
1352  t = (float)(4E-6) * (float)(delay) + (float)(6E-6);
1353  break;
1354  case DELAY_CLOCK_TMS:
1355  if (delay < 0)
1356  t = (float)(5.666E-6);
1357  else
1358  t = (float)(4E-6) * (float)(delay) + (float)(8.5E-6);
1359  break;
1360  case DELAY_SCAN_IN:
1361  if (delay < 0)
1362  t = (float)(5.5E-6);
1363  else
1364  t = (float)(4E-6) * (float)(delay) + (float)(8.8308E-6);
1365  break;
1366  case DELAY_SCAN_OUT:
1367  if (delay < 0)
1368  t = (float)(7.0E-6);
1369  else
1370  t = (float)(4E-6) * (float)(delay) + (float)(1.0527E-5);
1371  break;
1372  case DELAY_SCAN_IO:
1373  if (delay < 0)
1374  t = (float)(9.926E-6);
1375  else
1376  t = (float)(4E-6) * (float)(delay) + (float)(1.3132E-5);
1377  break;
1378  default:
1379  return 0;
1380  }
1381 
1382  f_float = 1.0 / t;
1383  return roundf(f_float);
1384 }
1385 
1386 /******************* Interface between OpenULINK and OpenOCD ******************/
1387 
1394 static void ulink_set_end_state(enum tap_state endstate)
1395 {
1396  if (tap_is_state_stable(endstate))
1397  tap_set_end_state(endstate);
1398  else {
1399  LOG_ERROR("BUG: %s is not a valid end state", tap_state_name(endstate));
1400  exit(EXIT_FAILURE);
1401  }
1402 }
1403 
1412 {
1413  uint8_t tms_sequence, tms_count;
1414  int ret;
1415 
1416  if (tap_get_state() == tap_get_end_state()) {
1417  /* Do nothing if we are already there */
1418  return ERROR_OK;
1419  }
1420 
1421  tms_sequence = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1423 
1424  ret = ulink_append_clock_tms_cmd(device, tms_count, tms_sequence);
1425 
1426  if (ret == ERROR_OK)
1428 
1429  return ret;
1430 }
1431 
1440 static int ulink_queue_scan(struct ulink *device, struct jtag_command *cmd)
1441 {
1442  uint32_t scan_size_bits, scan_size_bytes, bits_last_scan;
1443  uint32_t scans_max_payload, bytecount;
1444  uint8_t *tdi_buffer_start = NULL, *tdi_buffer = NULL;
1445  uint8_t *tdo_buffer_start = NULL, *tdo_buffer = NULL;
1446 
1447  uint8_t first_tms_count, first_tms_sequence;
1448  uint8_t last_tms_count, last_tms_sequence;
1449 
1450  uint8_t tms_count_pause, tms_sequence_pause;
1451  uint8_t tms_count_resume, tms_sequence_resume;
1452 
1453  uint8_t tms_count_start, tms_sequence_start;
1454  uint8_t tms_count_end, tms_sequence_end;
1455 
1456  enum scan_type type;
1457  int ret;
1458 
1459  /* Determine scan size */
1460  scan_size_bits = jtag_scan_size(cmd->cmd.scan);
1461  scan_size_bytes = DIV_ROUND_UP(scan_size_bits, 8);
1462 
1463  /* Determine scan type (IN/OUT/IO) */
1464  type = jtag_scan_type(cmd->cmd.scan);
1465 
1466  /* Determine number of scan commands with maximum payload */
1467  scans_max_payload = scan_size_bytes / 58;
1468 
1469  /* Determine size of last shift command */
1470  bits_last_scan = scan_size_bits - (scans_max_payload * 58 * 8);
1471 
1472  /* Allocate TDO buffer if required */
1473  if ((type == SCAN_IN) || (type == SCAN_IO)) {
1474  tdo_buffer_start = calloc(scan_size_bytes, sizeof(uint8_t));
1475 
1476  if (!tdo_buffer_start)
1477  return ERROR_FAIL;
1478 
1479  tdo_buffer = tdo_buffer_start;
1480  }
1481 
1482  /* Fill TDI buffer if required */
1483  if ((type == SCAN_OUT) || (type == SCAN_IO)) {
1484  jtag_build_buffer(cmd->cmd.scan, &tdi_buffer_start);
1485  tdi_buffer = tdi_buffer_start;
1486  }
1487 
1488  /* Get TAP state transitions */
1489  if (cmd->cmd.scan->ir_scan) {
1491  first_tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1492  first_tms_sequence = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1493 
1495  tap_set_end_state(cmd->cmd.scan->end_state);
1496  last_tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1497  last_tms_sequence = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1498 
1499  /* TAP state transitions for split scans */
1500  tms_count_pause = tap_get_tms_path_len(TAP_IRSHIFT, TAP_IRPAUSE);
1501  tms_sequence_pause = tap_get_tms_path(TAP_IRSHIFT, TAP_IRPAUSE);
1502  tms_count_resume = tap_get_tms_path_len(TAP_IRPAUSE, TAP_IRSHIFT);
1503  tms_sequence_resume = tap_get_tms_path(TAP_IRPAUSE, TAP_IRSHIFT);
1504  } else {
1506  first_tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1507  first_tms_sequence = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1508 
1510  tap_set_end_state(cmd->cmd.scan->end_state);
1511  last_tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
1512  last_tms_sequence = tap_get_tms_path(tap_get_state(), tap_get_end_state());
1513 
1514  /* TAP state transitions for split scans */
1515  tms_count_pause = tap_get_tms_path_len(TAP_DRSHIFT, TAP_DRPAUSE);
1516  tms_sequence_pause = tap_get_tms_path(TAP_DRSHIFT, TAP_DRPAUSE);
1517  tms_count_resume = tap_get_tms_path_len(TAP_DRPAUSE, TAP_DRSHIFT);
1518  tms_sequence_resume = tap_get_tms_path(TAP_DRPAUSE, TAP_DRSHIFT);
1519  }
1520 
1521  /* Generate scan commands */
1522  bytecount = scan_size_bytes;
1523  while (bytecount > 0) {
1524  if (bytecount == scan_size_bytes) {
1525  /* This is the first scan */
1526  tms_count_start = first_tms_count;
1527  tms_sequence_start = first_tms_sequence;
1528  } else {
1529  /* Resume from previous scan */
1530  tms_count_start = tms_count_resume;
1531  tms_sequence_start = tms_sequence_resume;
1532  }
1533 
1534  if (bytecount > 58) { /* Full scan, at least one scan will follow */
1535  tms_count_end = tms_count_pause;
1536  tms_sequence_end = tms_sequence_pause;
1537 
1539  type,
1540  58 * 8,
1541  tdi_buffer,
1542  tdo_buffer_start,
1543  tdo_buffer,
1544  tms_count_start,
1545  tms_sequence_start,
1546  tms_count_end,
1547  tms_sequence_end,
1548  cmd,
1549  false);
1550 
1551  bytecount -= 58;
1552 
1553  /* Update TDI and TDO buffer pointers */
1554  if (tdi_buffer_start)
1555  tdi_buffer += 58;
1556  if (tdo_buffer_start)
1557  tdo_buffer += 58;
1558  } else if (bytecount == 58) { /* Full scan, no further scans */
1559  tms_count_end = last_tms_count;
1560  tms_sequence_end = last_tms_sequence;
1561 
1563  type,
1564  58 * 8,
1565  tdi_buffer,
1566  tdo_buffer_start,
1567  tdo_buffer,
1568  tms_count_start,
1569  tms_sequence_start,
1570  tms_count_end,
1571  tms_sequence_end,
1572  cmd,
1573  true);
1574 
1575  bytecount = 0;
1576  } else {/* Scan with less than maximum payload, no further scans */
1577  tms_count_end = last_tms_count;
1578  tms_sequence_end = last_tms_sequence;
1579 
1581  type,
1582  bits_last_scan,
1583  tdi_buffer,
1584  tdo_buffer_start,
1585  tdo_buffer,
1586  tms_count_start,
1587  tms_sequence_start,
1588  tms_count_end,
1589  tms_sequence_end,
1590  cmd,
1591  true);
1592 
1593  bytecount = 0;
1594  }
1595 
1596  if (ret != ERROR_OK) {
1597  free(tdi_buffer_start);
1598  free(tdo_buffer_start);
1599  return ret;
1600  }
1601  }
1602 
1603  free(tdi_buffer_start);
1604 
1605  /* Set current state to the end state requested by the command */
1606  tap_set_state(cmd->cmd.scan->end_state);
1607 
1608  return ERROR_OK;
1609 }
1610 
1619 static int ulink_queue_tlr_reset(struct ulink *device, struct jtag_command *cmd)
1620 {
1621  int ret;
1622 
1623  ret = ulink_append_clock_tms_cmd(device, 5, 0xff);
1624 
1625  if (ret == ERROR_OK)
1627 
1628  return ret;
1629 }
1630 
1642 static int ulink_queue_runtest(struct ulink *device, struct jtag_command *cmd)
1643 {
1644  int ret;
1645 
1646  /* Only perform statemove if the TAP currently isn't in the TAP_IDLE state */
1647  if (tap_get_state() != TAP_IDLE) {
1650  }
1651 
1652  /* Generate the clock cycles */
1653  ret = ulink_append_clock_tck_cmd(device, cmd->cmd.runtest->num_cycles);
1654  if (ret != ERROR_OK)
1655  return ret;
1656 
1657  /* Move to end state specified in command */
1658  if (cmd->cmd.runtest->end_state != tap_get_state()) {
1659  tap_set_end_state(cmd->cmd.runtest->end_state);
1661  }
1662 
1663  return ERROR_OK;
1664 }
1665 
1674 static int ulink_queue_reset(struct ulink *device, struct jtag_command *cmd)
1675 {
1676  uint8_t low = 0, high = 0;
1677 
1678  if (cmd->cmd.reset->trst) {
1680  high |= SIGNAL_TRST;
1681  } else
1682  low |= SIGNAL_TRST;
1683 
1684  if (cmd->cmd.reset->srst)
1685  high |= SIGNAL_RESET;
1686  else
1687  low |= SIGNAL_RESET;
1688 
1689  return ulink_append_set_signals_cmd(device, low, high);
1690 }
1691 
1700 static int ulink_queue_pathmove(struct ulink *device, struct jtag_command *cmd)
1701 {
1702  int ret, state_count;
1703  enum tap_state *path;
1704  uint8_t tms_sequence;
1705 
1706  unsigned int num_states = cmd->cmd.pathmove->num_states;
1707  path = cmd->cmd.pathmove->path;
1708  state_count = 0;
1709 
1710  while (num_states > 0) {
1711  unsigned int batch_size;
1712 
1713  tms_sequence = 0;
1714 
1715  /* Determine batch size */
1716  if (num_states >= 8)
1717  batch_size = 8;
1718  else
1719  batch_size = num_states;
1720 
1721  for (unsigned int i = 0; i < batch_size; i++) {
1722  if (tap_state_transition(tap_get_state(), false) == path[state_count]) {
1723  /* Append '0' transition: clear bit 'i' in tms_sequence */
1724  buf_set_u32(&tms_sequence, i, 1, 0x0);
1725  } else if (tap_state_transition(tap_get_state(), true)
1726  == path[state_count]) {
1727  /* Append '1' transition: set bit 'i' in tms_sequence */
1728  buf_set_u32(&tms_sequence, i, 1, 0x1);
1729  } else {
1730  /* Invalid state transition */
1731  LOG_ERROR("BUG: %s -> %s isn't a valid TAP state transition",
1733  tap_state_name(path[state_count]));
1734  return ERROR_FAIL;
1735  }
1736 
1737  tap_set_state(path[state_count]);
1738  state_count++;
1739  num_states--;
1740  }
1741 
1742  /* Append CLOCK_TMS command to OpenULINK command queue */
1743  LOG_INFO(
1744  "pathmove batch: count = %i, sequence = 0x%x", batch_size, tms_sequence);
1745  ret = ulink_append_clock_tms_cmd(ulink_handle, batch_size, tms_sequence);
1746  if (ret != ERROR_OK)
1747  return ret;
1748  }
1749 
1750  return ERROR_OK;
1751 }
1752 
1761 static int ulink_queue_sleep(struct ulink *device, struct jtag_command *cmd)
1762 {
1763  /* IMPORTANT! Due to the time offset in command execution introduced by
1764  * command queueing, this needs to be implemented in the ULINK device */
1765  return ulink_append_sleep_cmd(device, cmd->cmd.sleep->us);
1766 }
1767 
1775 {
1776  int ret;
1777 
1779  LOG_ERROR("JTAG_STABLECLOCKS: state not stable");
1780  return ERROR_FAIL;
1781  }
1782 
1783  unsigned int num_cycles = cmd->cmd.stableclocks->num_cycles;
1784 
1785  /* TMS stays either high (Test Logic Reset state) or low (all other states) */
1786  if (tap_get_state() == TAP_RESET)
1788  else
1790 
1791  if (ret != ERROR_OK)
1792  return ret;
1793 
1794  while (num_cycles > 0) {
1795  if (num_cycles > 0xFFFF) {
1796  /* OpenULINK CMD_CLOCK_TCK can generate up to 0xFFFF (uint16_t) cycles */
1797  ret = ulink_append_clock_tck_cmd(device, 0xFFFF);
1798  num_cycles -= 0xFFFF;
1799  } else {
1800  ret = ulink_append_clock_tck_cmd(device, num_cycles);
1801  num_cycles = 0;
1802  }
1803 
1804  if (ret != ERROR_OK)
1805  return ret;
1806  }
1807 
1808  return ERROR_OK;
1809 }
1810 
1819 {
1820  struct jtag_command *cmd = ulink_cmd->cmd_origin;
1821  int ret;
1822 
1823  switch (jtag_scan_type(cmd->cmd.scan)) {
1824  case SCAN_IN:
1825  case SCAN_IO:
1826  ret = jtag_read_buffer(ulink_cmd->payload_in_start, cmd->cmd.scan);
1827  break;
1828  case SCAN_OUT:
1829  /* Nothing to do for OUT scans */
1830  ret = ERROR_OK;
1831  break;
1832  default:
1833  LOG_ERROR("BUG: %s() encountered an unknown JTAG scan type", __func__);
1834  ret = ERROR_FAIL;
1835  break;
1836  }
1837 
1838  return ret;
1839 }
1840 
1849 {
1850  struct ulink_cmd *current;
1851  struct jtag_command *openocd_cmd;
1852  int ret;
1853 
1854  current = device->queue_start;
1855 
1856  while (current) {
1857  openocd_cmd = current->cmd_origin;
1858 
1859  /* Check if a corresponding OpenOCD command is stored for this
1860  * OpenULINK command */
1861  if (current->needs_postprocessing && openocd_cmd) {
1862  switch (openocd_cmd->type) {
1863  case JTAG_SCAN:
1864  ret = ulink_post_process_scan(current);
1865  break;
1866  case JTAG_TLR_RESET:
1867  case JTAG_RUNTEST:
1868  case JTAG_RESET:
1869  case JTAG_PATHMOVE:
1870  case JTAG_SLEEP:
1871  case JTAG_STABLECLOCKS:
1872  /* Nothing to do for these commands */
1873  ret = ERROR_OK;
1874  break;
1875  default:
1876  ret = ERROR_FAIL;
1877  LOG_ERROR("BUG: %s() encountered unknown JTAG command type", __func__);
1878  break;
1879  }
1880 
1881  if (ret != ERROR_OK)
1882  return ret;
1883  }
1884 
1885  current = current->next;
1886  }
1887 
1888  return ERROR_OK;
1889 }
1890 
1891 /**************************** JTAG driver functions ***************************/
1892 
1905 static int ulink_execute_queue(struct jtag_command *cmd_queue)
1906 {
1907  struct jtag_command *cmd = cmd_queue;
1908  int ret;
1909 
1910  while (cmd) {
1911  switch (cmd->type) {
1912  case JTAG_SCAN:
1913  ret = ulink_queue_scan(ulink_handle, cmd);
1914  break;
1915  case JTAG_TLR_RESET:
1916  ret = ulink_queue_tlr_reset(ulink_handle, cmd);
1917  break;
1918  case JTAG_RUNTEST:
1919  ret = ulink_queue_runtest(ulink_handle, cmd);
1920  break;
1921  case JTAG_RESET:
1922  ret = ulink_queue_reset(ulink_handle, cmd);
1923  break;
1924  case JTAG_PATHMOVE:
1925  ret = ulink_queue_pathmove(ulink_handle, cmd);
1926  break;
1927  case JTAG_SLEEP:
1928  ret = ulink_queue_sleep(ulink_handle, cmd);
1929  break;
1930  case JTAG_STABLECLOCKS:
1931  ret = ulink_queue_stableclocks(ulink_handle, cmd);
1932  break;
1933  default:
1934  ret = ERROR_FAIL;
1935  LOG_ERROR("BUG: encountered unknown JTAG command type");
1936  break;
1937  }
1938 
1939  if (ret != ERROR_OK)
1940  return ret;
1941 
1942  cmd = cmd->next;
1943  }
1944 
1945  if (ulink_handle->commands_in_queue > 0) {
1946  ret = ulink_execute_queued_commands(ulink_handle, LIBUSB_TIMEOUT_MS);
1947  if (ret != ERROR_OK)
1948  return ret;
1949 
1950  ret = ulink_post_process_queue(ulink_handle);
1951  if (ret != ERROR_OK)
1952  return ret;
1953 
1954  ulink_clear_queue(ulink_handle);
1955  }
1956 
1957  return ERROR_OK;
1958 }
1959 
1968 static int ulink_khz(int khz, int *jtag_speed)
1969 {
1970  int ret;
1971 
1972  if (khz == 0) {
1973  LOG_ERROR("RCLK not supported");
1974  return ERROR_FAIL;
1975  }
1976 
1977  /* CLOCK_TCK commands are decoupled from others. Therefore, the frequency
1978  * setting can be done independently from all other commands. */
1979  if (khz >= 375)
1980  ulink_handle->delay_clock_tck = -1;
1981  else {
1982  ret = ulink_calculate_delay(DELAY_CLOCK_TCK, khz * 1000,
1983  &ulink_handle->delay_clock_tck);
1984  if (ret != ERROR_OK)
1985  return ret;
1986  }
1987 
1988  /* SCAN_{IN,OUT,IO} commands invoke CLOCK_TMS commands. Therefore, if the
1989  * requested frequency goes below the maximum frequency for SLOW_CLOCK_TMS
1990  * commands, all SCAN commands MUST also use the variable frequency
1991  * implementation! */
1992  if (khz >= 176) {
1993  ulink_handle->delay_clock_tms = -1;
1994  ulink_handle->delay_scan_in = -1;
1995  ulink_handle->delay_scan_out = -1;
1996  ulink_handle->delay_scan_io = -1;
1997  } else {
1998  ret = ulink_calculate_delay(DELAY_CLOCK_TMS, khz * 1000,
1999  &ulink_handle->delay_clock_tms);
2000  if (ret != ERROR_OK)
2001  return ret;
2002 
2003  ret = ulink_calculate_delay(DELAY_SCAN_IN, khz * 1000,
2004  &ulink_handle->delay_scan_in);
2005  if (ret != ERROR_OK)
2006  return ret;
2007 
2008  ret = ulink_calculate_delay(DELAY_SCAN_OUT, khz * 1000,
2009  &ulink_handle->delay_scan_out);
2010  if (ret != ERROR_OK)
2011  return ret;
2012 
2013  ret = ulink_calculate_delay(DELAY_SCAN_IO, khz * 1000,
2014  &ulink_handle->delay_scan_io);
2015  if (ret != ERROR_OK)
2016  return ret;
2017  }
2018 
2019  LOG_DEBUG_IO("ULINK TCK setup: delay_tck = %i (%li Hz),",
2020  ulink_handle->delay_clock_tck,
2022  LOG_DEBUG_IO(" delay_tms = %i (%li Hz),",
2023  ulink_handle->delay_clock_tms,
2025  LOG_DEBUG_IO(" delay_scan_in = %i (%li Hz),",
2026  ulink_handle->delay_scan_in,
2028  LOG_DEBUG_IO(" delay_scan_out = %i (%li Hz),",
2029  ulink_handle->delay_scan_out,
2031  LOG_DEBUG_IO(" delay_scan_io = %i (%li Hz),",
2032  ulink_handle->delay_scan_io,
2034 
2035  /* Configure the ULINK device with the new delay values */
2036  ret = ulink_append_configure_tck_cmd(ulink_handle,
2037  ulink_handle->delay_scan_in,
2038  ulink_handle->delay_scan_out,
2039  ulink_handle->delay_scan_io,
2040  ulink_handle->delay_clock_tck,
2041  ulink_handle->delay_clock_tms);
2042 
2043  if (ret != ERROR_OK)
2044  return ret;
2045 
2046  *jtag_speed = khz;
2047 
2048  return ERROR_OK;
2049 }
2050 
2063 static int ulink_speed(int speed)
2064 {
2065  int dummy;
2066 
2067  return ulink_khz(speed, &dummy);
2068 }
2069 
2083 static int ulink_speed_div(int speed, int *khz)
2084 {
2085  *khz = speed;
2086 
2087  return ERROR_OK;
2088 }
2089 
2097 static int ulink_init(void)
2098 {
2099  int ret, transferred;
2100  char str_manufacturer[20];
2101  bool download_firmware = false;
2102  unsigned char *dummy;
2103  uint8_t input_signals, output_signals;
2104 
2105  ulink_handle = calloc(1, sizeof(struct ulink));
2106  if (!ulink_handle)
2107  return ERROR_FAIL;
2108 
2109  libusb_init(&ulink_handle->libusb_ctx);
2110 
2111  ret = ulink_usb_open(&ulink_handle);
2112  if (ret != ERROR_OK) {
2113  LOG_ERROR("Could not open ULINK device");
2114  free(ulink_handle);
2115  ulink_handle = NULL;
2116  return ret;
2117  }
2118 
2119  /* Get String Descriptor to determine if firmware needs to be loaded */
2120  ret = libusb_get_string_descriptor_ascii(ulink_handle->usb_device_handle, 1, (unsigned char *)str_manufacturer, 20);
2121  if (ret < 0) {
2122  /* Could not get descriptor -> Unconfigured or original Keil firmware */
2123  download_firmware = true;
2124  } else {
2125  /* We got a String Descriptor, check if it is the correct one */
2126  if (strncmp(str_manufacturer, "OpenULINK", 9) != 0)
2127  download_firmware = true;
2128  }
2129 
2130  if (download_firmware) {
2131  LOG_INFO("Loading OpenULINK firmware. This is reversible by power-cycling"
2132  " ULINK device.");
2133  ret = ulink_load_firmware_and_renumerate(&ulink_handle,
2135  if (ret != ERROR_OK) {
2136  LOG_ERROR("Could not download firmware and re-numerate ULINK");
2137  free(ulink_handle);
2138  ulink_handle = NULL;
2139  return ret;
2140  }
2141  } else
2142  LOG_INFO("ULINK device is already running OpenULINK firmware");
2143 
2144  /* Get OpenULINK USB IN/OUT endpoints and claim the interface */
2145  ret = jtag_libusb_choose_interface(ulink_handle->usb_device_handle,
2146  &ulink_handle->ep_in, &ulink_handle->ep_out, -1, -1, -1, -1);
2147  if (ret != ERROR_OK)
2148  return ret;
2149 
2150  /* Initialize OpenULINK command queue */
2151  ulink_clear_queue(ulink_handle);
2152 
2153  /* Issue one test command with short timeout */
2154  ret = ulink_append_test_cmd(ulink_handle);
2155  if (ret != ERROR_OK)
2156  return ret;
2157 
2158  ret = ulink_execute_queued_commands(ulink_handle, 200);
2159  if (ret != ERROR_OK) {
2160  /* Sending test command failed. The ULINK device may be forever waiting for
2161  * the host to fetch an USB Bulk IN packet (e. g. OpenOCD crashed or was
2162  * shut down by the user via Ctrl-C. Try to retrieve this Bulk IN packet. */
2163  dummy = calloc(64, sizeof(uint8_t));
2164 
2165  ret = libusb_bulk_transfer(ulink_handle->usb_device_handle, ulink_handle->ep_in,
2166  dummy, 64, &transferred, 200);
2167 
2168  free(dummy);
2169 
2170  if (ret != 0 || transferred == 0) {
2171  /* Bulk IN transfer failed -> unrecoverable error condition */
2172  LOG_ERROR("Cannot communicate with ULINK device. Disconnect ULINK from "
2173  "the USB port and re-connect, then re-run OpenOCD");
2174  free(ulink_handle);
2175  ulink_handle = NULL;
2176  return ERROR_FAIL;
2177  }
2178 #ifdef _DEBUG_USB_COMMS_
2179  else {
2180  /* Successfully received Bulk IN packet -> continue */
2181  LOG_INFO("Recovered from lost Bulk IN packet");
2182  }
2183 #endif
2184  }
2185  ulink_clear_queue(ulink_handle);
2186 
2187  ret = ulink_append_get_signals_cmd(ulink_handle);
2188  if (ret == ERROR_OK)
2189  ret = ulink_execute_queued_commands(ulink_handle, 200);
2190 
2191  if (ret == ERROR_OK) {
2192  /* Post-process the single CMD_GET_SIGNALS command */
2193  input_signals = ulink_handle->queue_start->payload_in[0];
2194  output_signals = ulink_handle->queue_start->payload_in[1];
2195 
2196  ulink_print_signal_states(input_signals, output_signals);
2197  }
2198 
2199  ulink_clear_queue(ulink_handle);
2200 
2201  return ERROR_OK;
2202 }
2203 
2210 static int ulink_quit(void)
2211 {
2212  int ret;
2213 
2214  ret = ulink_usb_close(&ulink_handle);
2215  free(ulink_handle);
2216 
2217  return ret;
2218 }
2219 
2223 COMMAND_HANDLER(ulink_download_firmware_handler)
2224 {
2225  int ret;
2226 
2227  if (CMD_ARGC != 1)
2229 
2230 
2231  LOG_INFO("Downloading ULINK firmware image %s", CMD_ARGV[0]);
2232 
2233  /* Download firmware image in CMD_ARGV[0] */
2234  ret = ulink_load_firmware_and_renumerate(&ulink_handle, CMD_ARGV[0],
2236 
2237  return ret;
2238 }
2239 
2240 /*************************** Command Registration **************************/
2241 
2242 static const struct command_registration ulink_subcommand_handlers[] = {
2243  {
2244  .name = "download_firmware",
2245  .handler = &ulink_download_firmware_handler,
2246  .mode = COMMAND_EXEC,
2247  .help = "download firmware image to ULINK device",
2248  .usage = "path/to/ulink_firmware.hex",
2249  },
2251 };
2252 
2253 static const struct command_registration ulink_command_handlers[] = {
2254  {
2255  .name = "ulink",
2256  .mode = COMMAND_ANY,
2257  .help = "perform ulink management",
2258  .chain = ulink_subcommand_handlers,
2259  .usage = "",
2260  },
2262 };
2263 
2264 static struct jtag_interface ulink_interface = {
2266 };
2267 
2269  .name = "ulink",
2270  .transport_ids = TRANSPORT_JTAG,
2271  .transport_preferred_id = TRANSPORT_JTAG,
2272  .commands = ulink_command_handlers,
2273 
2274  .init = ulink_init,
2275  .quit = ulink_quit,
2276  .speed = ulink_speed,
2277  .khz = ulink_khz,
2278  .speed_div = ulink_speed_div,
2279 
2280  .jtag_ops = &ulink_interface,
2281 };
static uint8_t tdi_buffer[ARMJTAGEW_TAP_BUFFER_SIZE]
Definition: arm-jtag-ew.c:510
static uint8_t tdo_buffer[ARMJTAGEW_TAP_BUFFER_SIZE]
Definition: arm-jtag-ew.c:511
static const struct device_t * device
Definition: at91rm9200.c:94
static void buf_set_u32(uint8_t *_buffer, unsigned int first, unsigned int num, uint32_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
Definition: binarybuffer.h:34
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:156
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:400
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:151
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:251
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
unsigned int jtag_scan_size(const struct scan_command *cmd)
Definition: commands.c:181
int jtag_build_buffer(const struct scan_command *cmd, uint8_t **buffer)
Definition: commands.c:192
enum scan_type jtag_scan_type(const struct scan_command *cmd)
Definition: commands.c:167
int jtag_read_buffer(uint8_t *buffer, const struct scan_command *cmd)
Definition: commands.c:230
scan_type
The inferred type of a scan_command structure, indicating whether the command has the host scan in fr...
Definition: commands.h:22
@ SCAN_IN
From device to host,.
Definition: commands.h:24
@ SCAN_OUT
From host to device,.
Definition: commands.h:26
@ SCAN_IO
Full-duplex scan.
Definition: commands.h:28
@ JTAG_TLR_RESET
Definition: commands.h:137
@ JTAG_SCAN
Definition: commands.h:129
@ JTAG_PATHMOVE
Definition: commands.h:140
@ JTAG_STABLECLOCKS
Definition: commands.h:142
@ JTAG_RUNTEST
Definition: commands.h:138
@ JTAG_SLEEP
Definition: commands.h:141
@ JTAG_RESET
Definition: commands.h:139
uint64_t buffer
Pointer to data buffer to send over SPI.
Definition: dw-spi-helper.h:0
uint32_t size
Size of dw_spi_transaction::buffer.
Definition: dw-spi-helper.h:4
uint8_t type
Definition: esp_usb_jtag.c:0
static uint16_t direction
Definition: ftdi.c:120
void image_close(struct image *image)
Definition: image.c:1210
int image_read_section(struct image *image, int section, target_addr_t offset, uint32_t size, uint8_t *buffer, size_t *size_read)
Definition: image.c:1078
int image_open(struct image *image, const char *url, const char *type_string)
Definition: image.c:956
enum tap_state tap_get_end_state(void)
For more information,.
Definition: interface.c:56
enum tap_state tap_state_transition(enum tap_state cur_state, bool tms)
Function tap_state_transition takes a current TAP state and returns the next state according to the t...
Definition: interface.c:223
const char * tap_state_name(enum tap_state state)
Function tap_state_name Returns a string suitable for display representing the JTAG tap_state.
Definition: interface.c:344
int tap_get_tms_path_len(enum tap_state from, enum tap_state to)
Function int tap_get_tms_path_len returns the total number of bits that represents a TMS path transit...
Definition: interface.c:195
void tap_set_end_state(enum tap_state new_end_state)
This function sets the state of an "end state follower" which tracks the state that any cable driver ...
Definition: interface.c:48
enum tap_state tap_get_state(void)
This function gets the state of the "state follower" which tracks the state of the TAPs connected to ...
Definition: interface.c:37
bool tap_is_state_stable(enum tap_state astate)
Function tap_is_state_stable returns true if the astate is stable.
Definition: interface.c:200
int tap_get_tms_path(enum tap_state from, enum tap_state to)
This function provides a "bit sequence" indicating what has to be done with TMS during a sequence of ...
Definition: interface.c:190
#define tap_set_state(new_state)
This function sets the state of a "state follower" which tracks the state of the TAPs connected to th...
Definition: interface.h:50
uint8_t delay_scan_in
Delay value for SCAN_IN operations with less than maximum TCK frequency.
Definition: jtag.c:17
uint8_t delay_scan_out
Delay value for SCAN_OUT operations with less than maximum TCK frequency.
Definition: jtag.c:20
uint8_t delay_tck
Delay value for CLOCK_TCK operations with less than maximum frequency.
Definition: jtag.c:26
uint8_t delay_scan_io
Delay value for SCAN_IO operations with less than maximum TCK frequency.
Definition: jtag.c:23
uint8_t delay_tms
Delay value for CLOCK_TMS operations with less than maximum frequency.
Definition: jtag.c:29
tap_state
Defines JTAG Test Access Port states.
Definition: jtag.h:37
@ TAP_RESET
Definition: jtag.h:56
@ TAP_DRPAUSE
Definition: jtag.h:44
@ TAP_IRSHIFT
Definition: jtag.h:51
@ TAP_IDLE
Definition: jtag.h:53
@ TAP_DRSHIFT
Definition: jtag.h:43
@ TAP_IRPAUSE
Definition: jtag.h:52
int jtag_libusb_choose_interface(struct libusb_device_handle *devh, unsigned int *usb_read_ep, unsigned int *usb_write_ep, int bclass, int subclass, int protocol, int trans_type)
Find the first interface optionally matching class, subclass and protocol and claim it.
#define LIBUSB_TIMEOUT_MS
Definition: libusb_helper.h:26
#define LOG_DEBUG_IO(expr ...)
Definition: log.h:102
#define ERROR_FAIL
Definition: log.h:174
#define LOG_ERROR(expr ...)
Definition: log.h:133
#define LOG_LEVEL_IS(FOO)
Definition: log.h:100
#define LOG_INFO(expr ...)
Definition: log.h:127
#define LOG_DEBUG(expr ...)
Definition: log.h:110
#define ERROR_OK
Definition: log.h:168
@ LOG_LVL_DEBUG_IO
Definition: log.h:48
Definition of the commands supported by the OpenULINK firmware.
#define CMD_CONFIGURE_TCK_FREQ
Definition: msgtypes.h:153
#define SIGNAL_TDI
Definition: msgtypes.h:164
#define SIGNAL_TMS
Definition: msgtypes.h:165
#define CMD_SLOW_CLOCK_TCK
Definition: msgtypes.h:148
#define CMD_SET_LEDS
Definition: msgtypes.h:154
#define CMD_SLOW_SCAN_IN
Definition: msgtypes.h:138
#define SIGNAL_RESET
Definition: msgtypes.h:169
#define CMD_SLOW_SCAN_OUT
Definition: msgtypes.h:140
#define CMD_SLOW_SCAN_IO
Definition: msgtypes.h:142
#define CMD_SCAN_OUT
Definition: msgtypes.h:139
#define CMD_GET_SIGNALS
Definition: msgtypes.h:151
#define CMD_SLEEP_MS
Definition: msgtypes.h:150
#define SIGNAL_TCK
Definition: msgtypes.h:166
#define SIGNAL_TRST
Definition: msgtypes.h:167
#define CMD_SCAN_IN
Definition: msgtypes.h:137
#define CMD_TEST
Definition: msgtypes.h:155
#define CMD_CLOCK_TCK
Definition: msgtypes.h:147
#define CMD_CLOCK_TMS
Definition: msgtypes.h:145
#define CMD_SCAN_IO
Definition: msgtypes.h:141
#define CMD_SLOW_CLOCK_TMS
Definition: msgtypes.h:146
#define CMD_SET_SIGNALS
Definition: msgtypes.h:152
#define SIGNAL_TDO
Definition: msgtypes.h:158
#define CMD_SLEEP_US
Definition: msgtypes.h:149
target_addr_t addr
Start address to search for the control block.
Definition: rtt/rtt.c:28
Represents a driver for a debugging interface.
Definition: interface.h:208
const char *const name
The name of the interface driver.
Definition: interface.h:210
const char * name
Definition: command.h:234
const char * usage
a string listing the options and arguments, required or optional
Definition: command.h:239
Definition: image.h:48
unsigned int num_sections
Definition: image.h:51
struct imagesection * sections
Definition: image.h:52
long long base_address
Definition: image.h:54
bool base_address_set
Definition: image.h:53
target_addr_t base_address
Definition: image.h:42
uint32_t size
Definition: image.h:43
enum jtag_command_type type
Definition: commands.h:148
Represents a driver for a debugging interface.
Definition: interface.h:183
int(* execute_queue)(struct jtag_command *cmd_queue)
Execute commands in the supplied queue.
Definition: interface.h:196
Definition: osbdm.c:17
Definition: psoc6.c:83
#define TRANSPORT_JTAG
Definition: transport.h:19
#define DIV_ROUND_UP(m, n)
Rounds m up to the nearest multiple of n using division.
Definition: types.h:79
static struct ublast_lowlevel low
#define NULL
Definition: usb.h:16
uint8_t cmd
Definition: vdebug.c:1
uint8_t dummy[96]
Definition: vdebug.c:23
uint8_t count[4]
Definition: vdebug.c:22