OpenOCD
eud.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
6 
7 #include <assert.h>
8 #include <inttypes.h>
9 #include <stdint.h>
10 
11 #include <helper/binarybuffer.h>
12 #include <helper/bits.h>
13 #include <helper/bitfield.h>
14 #include <helper/log.h>
15 #include <jtag/adapter.h>
16 #include <jtag/interface.h>
17 #include <jtag/swd.h>
18 #include <libusb.h>
19 #include <target/arm_adi_v5.h>
20 
21 #include "libusb_helper.h"
22 
23 #define EUD_VID 0x05c6
24 #define EUD_CTL_PID 0x9501
25 #define EUD_SWD_PID 0x9504
26 #define EUD_INTERFACE 0
27 #define EUD_EP_OUT 0x02
28 #define EUD_EP_IN 0x81
29 #define EUD_USB_TIMEOUT_MS 6000
30 /*
31  * The SWD function exposes 32-byte hardware FIFOs in both directions.
32  * The hardware processes commands in order until any of the following:
33  * a) it encounters an undefined opcode
34  * b) cur_cmd == EUD_NOP/FLUSH, after which it ignores the rest of the buffer
35  * c) until it processes everything
36  */
37 #define EUD_SWD_BUFFER_SIZE 32
38 /*
39  * Every queue flush ends with a STATUS opcode; budget for a FLUSH opcode
40  * too, even though eud_swd_flush_queue() omits it when the IN FIFO ends up
41  * exactly full.
42  */
43 #define EUD_SWD_QUEUE_TRAILER_SIZE 2
44 #define EUD_SWD_STATUS_SIZE 4
45 #define EUD_SWD_DITMS_OUT_SIZE 5
46 
47 #define EUD_CTL_CMD_CTLOUT_SET 7
48 #define EUD_CTL_CMD_CTLOUT_CLR 8
49 
50 #define EUD_CTL_RCTLR_SRST_N BIT(0)
51 #define EUD_CTL_SWD_PERIPH_EN BIT(2)
52 #define EUD_CTL_JTAG_PERIPH_EN BIT(4)
53 #define EUD_CTL_VDDMIN_TCK_EN BIT(6)
54 #define EUD_CTL_GPIO_JTAG_SEL BIT(7)
55 #define EUD_CTL_DAP_MUX_SEL BIT(10)
56 #define EUD_CTL_ACC_TDO_SEL BIT(17)
57 #define EUD_CTL_DAP_TDO_SEL BIT(18)
58 #define EUD_CTL_MTAP_TDO_SEL BIT(19)
59 #define EUD_CTL_DAP_SWDO_SEL BIT(20)
60 
61 #define EUD_CTL_SWD_ON_CLEAR (EUD_CTL_JTAG_PERIPH_EN | \
62  EUD_CTL_GPIO_JTAG_SEL | \
63  EUD_CTL_ACC_TDO_SEL | \
64  EUD_CTL_DAP_TDO_SEL | \
65  EUD_CTL_MTAP_TDO_SEL)
66 #define EUD_CTL_SWD_ON_SET (EUD_CTL_RCTLR_SRST_N | \
67  EUD_CTL_SWD_PERIPH_EN | \
68  EUD_CTL_VDDMIN_TCK_EN | \
69  EUD_CTL_DAP_MUX_SEL | \
70  EUD_CTL_DAP_SWDO_SEL)
71 #define EUD_SWD_BB_RCTLR_SRST_N BIT(2)
72 #define EUD_SWD_BB_GPIO_SRST_N BIT(4)
73 #define EUD_SWD_BB_GPIO_TRST_N BIT(5)
74 #define EUD_SWD_BB_DAP_TRST_N BIT(6)
75 #define EUD_SWD_BB_RESET_DEFAULT (EUD_SWD_BB_RCTLR_SRST_N | \
76  EUD_SWD_BB_GPIO_SRST_N | \
77  EUD_SWD_BB_GPIO_TRST_N | \
78  EUD_SWD_BB_DAP_TRST_N)
79 
80 #define EUD_SWD_CMD_FLUSH 1
81 #define EUD_SWD_CMD_FREQ 2
82 #define EUD_SWD_CMD_DELAY 3
83  // Delay between commands = (N+1) / swd_clk_freq
84 #define SWD_CMD_DELAY_TIME GENMASK(7, 0)
85 #define SWD_CMD_DELAY_RESERVED GENMASK(31, 8)
86 #define EUD_SWD_CMD_BITBANG 4
87 #define EUD_SWD_CMD_DITMS 5
88 #define SWD_CMD_DITMS_DATA GENMASK(15, 0)
89 #define SWD_CMD_DITMS_DATA_BITS 16
90 #define SWD_CMD_DITMS_COUNT GENMASK(31, 16)
91 #define EUD_SWD_DITMS_MAX_BITS (FIELD_GET(SWD_CMD_DITMS_COUNT, SWD_CMD_DITMS_COUNT) + 1)
92 #define EUD_SWD_CMD_TIMING 6
93  // The number of retries the SWD Peripheral performs due to a WAIT response
94  // from the DAP
95 #define SWD_CMD_TIMING_RETRY_COUNT GENMASK(15, 0)
96  // The number of SWD clock cycles allowed for the turn-around time minus 1
97 #define SWD_CMD_TIMING_TURNAROUND_TIME GENMASK(17, 16)
98  // If retry_count == 0 and SWD R/W is delayed by ACK.WAIT, wait 35 cycles
99  // before issuing another SWD R/W
100 #define SWD_CMD_TIMING_WAIT_35_IF_ERR BIT(18)
101 #define SWD_CMD_TIMING_RESERVED GENMASK(31, 19)
102 #define EUD_SWD_CMD_STATUS 7
103  // Reading the status clears cmd_cntr and the error flags, so every queue
104  // flush needs a status of its own
105  // The ack[2:0] the DAP returned for the last command processed
106 #define SWD_CMD_STATUS_ACK GENMASK(2, 0)
107  // Set once the SWD Peripheral has exhausted its ACK.WAIT retries
108 #define SWD_CMD_STATUS_WAIT_TIMEOUT BIT(3)
109  // Set when a DAP response does not match the parity bit it came with
110 #define SWD_CMD_STATUS_PARITY_ERR BIT(4)
111  // The number of SWD R/W commands the peripheral got through. An error flag
112  // stops it on the offending command, but an ack that is merely not OK does
113  // not, so it bounds a failure rather than pinpointing it
114 #define SWD_CMD_STATUS_CMD_CNTR GENMASK(31, 16)
115 #define EUD_SWD_CMD_PERIPH_RST 8
116 
117 struct eud_device {
118  libusb_device_handle *handle;
119  uint16_t pid;
120  bool claimed;
121 };
122 
124  uint8_t cmd;
125  uint32_t value;
126  uint32_t *read_value;
127  uint32_t ap_delay_clk;
128 };
129 
130 static libusb_context *eud_libusb_ctx;
131 static struct eud_device eud_ctl;
132 static struct eud_device eud_swd;
133 /*
134  * Every transfer costs at least one OUT byte, so a queue can never hold more
135  * transfers than the FIFO is bytes wide.
136  */
138 static unsigned int eud_queue_len;
139 static unsigned int eud_queue_out_len;
140 static unsigned int eud_queue_in_len;
141 static int eud_speed_index = 0xa;
142 
143 static const unsigned int eud_speed_khz[] = {
144  120000,
145  80000,
146  60000,
147  40000,
148  30000,
149  15000,
150  7500,
151  3750,
152  1875,
153  938,
154  469,
155  234,
156  117,
157 };
158 
159 static void eud_close(struct eud_device *dev)
160 {
161  if (!dev->handle)
162  return;
163 
164  if (dev->claimed)
165  libusb_release_interface(dev->handle, EUD_INTERFACE);
166 
167  libusb_close(dev->handle);
168  dev->handle = NULL;
169  dev->pid = 0;
170  dev->claimed = false;
171 }
172 
173 static int eud_libusb_reinit(void)
174 {
175  if (eud_libusb_ctx)
176  libusb_exit(eud_libusb_ctx);
177 
178  int ret = libusb_init(&eud_libusb_ctx);
179  if (ret != LIBUSB_SUCCESS) {
180  LOG_ERROR("libusb_init() failed: %s", libusb_error_name(ret));
181  return ERROR_FAIL;
182  }
183 
184  return ERROR_OK;
185 }
186 
187 static int eud_open_inner(uint16_t pid, struct eud_device *dev, bool quiet)
188 {
189  dev->handle = libusb_open_device_with_vid_pid(eud_libusb_ctx, EUD_VID, pid);
190  if (!dev->handle) {
191  if (!quiet)
192  LOG_ERROR("unable to open EUD USB function %04x:%04x", EUD_VID, pid);
193  return ERROR_FAIL;
194  }
195 
196  int ret = libusb_claim_interface(dev->handle, EUD_INTERFACE);
197  if (ret != LIBUSB_SUCCESS) {
198  if (!quiet)
199  LOG_ERROR("libusb_claim_interface() failed: %s", libusb_error_name(ret));
200  eud_close(dev);
201  return jtag_libusb_error(ret);
202  }
203 
204  dev->pid = pid;
205  dev->claimed = true;
206  return ERROR_OK;
207 }
208 
209 static int eud_open(uint16_t pid, struct eud_device *dev)
210 {
211  return eud_open_inner(pid, dev, false);
212 }
213 
214 static int eud_bulk_write(struct eud_device *dev, const uint8_t *buf, int len)
215 {
216  LOG_DEBUG_IO("EUD pid 0x%04x write len %d opcode 0x%02x", dev->pid, len, buf[0]);
217  int transferred;
218  int ret = libusb_bulk_transfer(dev->handle, EUD_EP_OUT, (unsigned char *)buf,
219  len, &transferred, EUD_USB_TIMEOUT_MS);
220  if (ret != LIBUSB_SUCCESS) {
221  LOG_ERROR("EUD bulk write failed: %s", libusb_error_name(ret));
222  return jtag_libusb_error(ret);
223  }
224 
225  if (transferred != len) {
226  LOG_ERROR("short EUD bulk write: %d of %d bytes", transferred, len);
227  return ERROR_FAIL;
228  }
229 
230  return ERROR_OK;
231 }
232 
233 static int eud_bulk_read(struct eud_device *dev, uint8_t *buf, int len)
234 {
235  int transferred;
236  int ret = libusb_bulk_transfer(dev->handle, EUD_EP_IN, buf, len,
237  &transferred, EUD_USB_TIMEOUT_MS);
238  if (ret != LIBUSB_SUCCESS) {
239  LOG_ERROR("EUD bulk read failed: %s", libusb_error_name(ret));
240  return jtag_libusb_error(ret);
241  }
242 
243  if (transferred != len) {
244  LOG_ERROR("short EUD bulk read: %d of %d bytes", transferred, len);
245  return ERROR_FAIL;
246  }
247 
248  return ERROR_OK;
249 }
250 
251 static int eud_op(struct eud_device *dev, uint8_t opcode, uint32_t payload)
252 {
253  uint8_t buf[5];
254 
255  buf[0] = opcode;
256  h_u32_to_le(&buf[1], payload);
257 
258  return eud_bulk_write(dev, buf, sizeof(buf));
259 }
260 
261 static int eud_op_no_payload(struct eud_device *dev, uint8_t opcode)
262 {
263  return eud_bulk_write(dev, &opcode, sizeof(opcode));
264 }
265 
266 static int eud_ctl_enable_swd(void)
267 {
268  int retval = eud_open(EUD_CTL_PID, &eud_ctl);
269  if (retval != ERROR_OK)
270  return retval;
271 
273  if (retval != ERROR_OK)
274  goto out;
275 
277 
278 out:
279  eud_close(&eud_ctl);
280  return retval;
281 }
282 
284 {
285  int retval = eud_ctl_enable_swd();
286  if (retval != ERROR_OK)
287  return retval;
288 
289  for (unsigned int i = 0; i < 5000; i++) {
290  alive_sleep(1);
291 
292  retval = eud_libusb_reinit();
293  if (retval != ERROR_OK)
294  return retval;
295 
296  retval = eud_open_inner(EUD_SWD_PID, &eud_swd, true);
297  if (retval == ERROR_OK)
298  return ERROR_OK;
299  }
300 
301  LOG_ERROR("unable to open EUD USB function %04x:%04x", EUD_VID, EUD_SWD_PID);
302  return ERROR_FAIL;
303 }
304 
305 static int eud_reopen_swd(void)
306 {
307  eud_close(&eud_swd);
308  return eud_open_swd_after_enable();
309 }
310 
311 static int eud_swd_set_speed_index(int speed_index)
312 {
313  if (speed_index < 0 || speed_index >= (int)ARRAY_SIZE(eud_speed_khz)) {
314  LOG_ERROR("EUD speed index %d is out of range", speed_index);
315  return ERROR_FAIL;
316  }
317 
318  int retval = eud_op(&eud_swd, EUD_SWD_CMD_FREQ, speed_index);
319  if (retval != ERROR_OK)
320  return retval;
321 
322  eud_speed_index = speed_index;
323  return ERROR_OK;
324 }
325 
326 static int eud_swd_init_defaults(void)
327 {
329  if (retval != ERROR_OK)
330  return retval;
331 
333  if (retval != ERROR_OK)
334  return retval;
335 
336  retval = eud_op(&eud_swd, EUD_SWD_CMD_DELAY,
338  if (retval != ERROR_OK)
339  return retval;
340 
343 }
344 
345 static int eud_swd_bitbang(uint32_t payload)
346 {
347  uint8_t out[6];
348  uint8_t in[4];
349 
350  out[0] = EUD_SWD_CMD_BITBANG;
351  h_u32_to_le(&out[1], payload);
352  out[5] = EUD_SWD_CMD_FLUSH;
353 
354  int retval = eud_bulk_write(&eud_swd, out, sizeof(out));
355  if (retval != ERROR_OK)
356  return retval;
357 
358  return eud_bulk_read(&eud_swd, in, sizeof(in));
359 }
360 
361 static int eud_ditms_u16(uint16_t tms, uint16_t count)
362 {
363  int retval = eud_op(&eud_swd, EUD_SWD_CMD_DITMS,
366  if (retval == ERROR_OK)
367  alive_sleep(1);
368  return retval;
369 }
370 
371 static int eud_swd_prepare(void)
372 {
373  return eud_swd_init_defaults();
374 }
375 
376 static int eud_ditms_sequence(const uint8_t *seq, unsigned int bit_len)
377 {
378  int retval = ERROR_OK;
379  unsigned int bit_pos = 0;
380 
381  // EUD can issue (N + 1) bits with a single CMD_DITMS
382  // However, for N > 15 (i.e. 'send more than 16 bits') it repeats the payload[15]
383  // value (N - 16) times
384  while (bit_pos < bit_len) {
385  unsigned int cmd_len = bit_len - bit_pos;
386  if (cmd_len > SWD_CMD_DITMS_DATA_BITS) {
387  bool repeated_bit = buf_get_u32(seq, bit_pos + SWD_CMD_DITMS_DATA_BITS - 1, 1);
388 
389  cmd_len = SWD_CMD_DITMS_DATA_BITS;
390  while (cmd_len < EUD_SWD_DITMS_MAX_BITS && bit_pos + cmd_len < bit_len) {
391  if (buf_get_u32(seq, bit_pos + cmd_len, 1) != repeated_bit)
392  break;
393 
394  cmd_len++;
395  }
396  }
397 
398  unsigned int payload_bits = MIN(cmd_len, SWD_CMD_DITMS_DATA_BITS);
399  uint16_t tms = buf_get_u32(seq, bit_pos, payload_bits);
400 
401  retval = eud_ditms_u16(tms, cmd_len - 1);
402  if (retval != ERROR_OK)
403  break;
404 
405  bit_pos += cmd_len;
406  }
407 
408  return retval;
409 }
410 
412 {
413  switch (seq) {
414  case LINE_RESET:
415  LOG_DEBUG("SWD line reset");
417  case JTAG_TO_SWD:
418  LOG_DEBUG("JTAG-to-SWD");
420  case JTAG_TO_DORMANT:
421  LOG_DEBUG("JTAG-to-DORMANT");
423  case SWD_TO_JTAG:
424  LOG_DEBUG("SWD-to-JTAG ignored");
425  return ERROR_OK;
426  case SWD_TO_DORMANT:
427  LOG_DEBUG("SWD-to-DORMANT");
429  case DORMANT_TO_SWD:
430  LOG_DEBUG("DORMANT-to-SWD");
432  case DORMANT_TO_JTAG:
433  LOG_DEBUG("DORMANT-to-JTAG");
435  default:
436  LOG_ERROR("Sequence %d not supported", seq);
437  return ERROR_FAIL;
438  }
439 }
440 
441 static int eud_swd_init(void)
442 {
443  if (!eud_swd.handle)
444  return ERROR_OK;
445 
446  int retval = eud_swd_prepare();
447  if (retval == ERROR_OK)
448  return ERROR_OK;
449 
450  retval = eud_reopen_swd();
451  if (retval != ERROR_OK)
452  return retval;
453 
454  return eud_swd_prepare();
455 }
456 
457 static int eud_swd_flush_queue(uint32_t min_idle_clk);
458 
459 static unsigned int eud_swd_transfer_out_size(const struct eud_swd_transfer *transfer)
460 {
461  unsigned int size = transfer->cmd & SWD_CMD_RNW ? 1 : 5;
462 
463  if (transfer->ap_delay_clk)
465 
466  return size;
467 }
468 
469 static unsigned int eud_swd_transfer_in_size(const struct eud_swd_transfer *transfer)
470 {
471  return transfer->cmd & SWD_CMD_RNW ? 4 : 0;
472 }
473 
474 static int eud_swd_queue_transfer(uint8_t cmd, uint32_t value,
475  uint32_t *read_value, uint32_t ap_delay_clk)
476 {
478  LOG_ERROR("EUD SWD AP delay of %" PRIu32 " clocks is too large", ap_delay_clk);
479  return ERROR_FAIL;
480  }
481 
482  struct eud_swd_transfer transfer = {
483  .cmd = cmd,
484  .value = value,
485  .read_value = read_value,
486  .ap_delay_clk = ap_delay_clk,
487  };
488  unsigned int transfer_out = eud_swd_transfer_out_size(&transfer);
489  unsigned int transfer_in = eud_swd_transfer_in_size(&transfer);
490  bool returns_ack = swd_cmd_returns_ack(cmd);
491  /* Reserve a DITMS for trailing idle clocks if this transfer has no AP delay. */
492  unsigned int last_pad = ap_delay_clk ? 0 : EUD_SWD_DITMS_OUT_SIZE;
493 
494  /*
495  * Run whatever is already queued if this transfer would not fit in
496  * the hardware FIFOs alongside it, or if it is unacknowledged: a
497  * DP_TARGETSEL write is answered with silence, so it must be the
498  * only transfer in the queue, see eud_swd_flush_queue().
499  */
500  if (eud_queue_len &&
501  (!returns_ack ||
502  eud_queue_out_len + transfer_out + last_pad + EUD_SWD_QUEUE_TRAILER_SIZE > EUD_SWD_BUFFER_SIZE ||
504  int retval = eud_swd_flush_queue(0);
505  if (retval != ERROR_OK)
506  return retval;
507  }
508 
509  eud_queue[eud_queue_len++] = transfer;
510  eud_queue_out_len += transfer_out;
511  eud_queue_in_len += transfer_in;
512 
513  /*
514  * If a TARGETSEL write happens to be the last transaction,
515  * run_queue() can't pad it after this flush. Add the 8 idle clock
516  * cycle wait to handle the pessimistic case.
517  */
518  if (!returns_ack)
519  return eud_swd_flush_queue(8);
520 
521  return ERROR_OK;
522 }
523 
524 static int eud_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
525 {
526  assert(cmd & SWD_CMD_RNW);
528 }
529 
530 static int eud_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
531 {
532  assert(!(cmd & SWD_CMD_RNW));
534 }
535 
536 static int eud_swd_flush_queue(uint32_t min_idle_clk)
537 {
538  uint8_t out[EUD_SWD_BUFFER_SIZE];
539  uint8_t in[EUD_SWD_BUFFER_SIZE];
540  unsigned int out_len = 0;
541  unsigned int in_len = 0;
542  unsigned int count = eud_queue_len;
543 
544  for (unsigned int i = 0; i < count; i++) {
545  const struct eud_swd_transfer *transfer = &eud_queue[i];
546  uint8_t eud_cmd = (transfer->cmd | SWD_CMD_START | SWD_CMD_PARK) & ~SWD_CMD_STOP;
547  /* Use the last transfer's AP delay for any required trailing idle clocks. */
548  uint32_t delay_clk = transfer->ap_delay_clk;
549 
550  if (i == count - 1 && delay_clk < min_idle_clk)
551  delay_clk = min_idle_clk;
552 
553  LOG_DEBUG_IO("%s %s reg %x %" PRIx32,
554  transfer->cmd & SWD_CMD_APNDP ? "AP" : "DP",
555  transfer->cmd & SWD_CMD_RNW ? "read" : "write",
556  (transfer->cmd & SWD_CMD_A32) >> 1,
557  transfer->value);
558 
559  out[out_len++] = eud_cmd;
560  if (transfer->cmd & SWD_CMD_RNW) {
561  in_len += 4;
562  } else {
563  h_u32_to_le(&out[out_len], transfer->value);
564  out_len += 4;
565  }
566 
567  if (delay_clk) {
568  out[out_len++] = EUD_SWD_CMD_DITMS;
569  h_u32_to_le(&out[out_len],
570  FIELD_PREP(SWD_CMD_DITMS_COUNT, delay_clk - 1));
571  out_len += 4;
572  }
573  }
574 
575  out[out_len++] = EUD_SWD_CMD_STATUS;
576  in_len += EUD_SWD_STATUS_SIZE;
577 
578  /*
579  * The hardware auto-flushes once the IN FIFO is exactly full, so an
580  * explicit FLUSH is only needed when the queue leaves it partially
581  * filled.
582  */
583  if (in_len != EUD_SWD_BUFFER_SIZE)
584  out[out_len++] = EUD_SWD_CMD_FLUSH;
585 
586  assert(out_len <= sizeof(out));
587  assert(in_len <= sizeof(in));
588 
589  LOG_DEBUG_IO("Flushing EUD SWD queue of %u transfers (%u bytes out, %u bytes in)",
590  count, out_len, in_len);
591 
592  int retval = eud_bulk_write(&eud_swd, out, out_len);
593  if (retval != ERROR_OK)
594  goto out;
595 
596  retval = eud_bulk_read(&eud_swd, in, in_len);
597  if (retval != ERROR_OK)
598  goto out;
599 
600  unsigned int in_pos = 0;
601  for (unsigned int i = 0; i < count; i++) {
602  const struct eud_swd_transfer *transfer = &eud_queue[i];
603  if (transfer->cmd & SWD_CMD_RNW) {
604  uint32_t value = le_to_h_u32(&in[in_pos]);
605  in_pos += 4;
606  if (transfer->read_value)
607  *transfer->read_value = value;
608  LOG_DEBUG_IO("read result: %" PRIx32, value);
609  }
610  }
611 
612  uint32_t status = le_to_h_u32(&in[in_pos]);
613 
614  /*
615  * A DP_TARGETSEL write is answered with silence, so whatever the
616  * peripheral sampled during the ack phase is meaningless. Such a
617  * transfer gets a queue of its own precisely so that its status can be
618  * dropped without hiding the transfers around it.
619  */
620  if (!swd_cmd_returns_ack(eud_queue[0].cmd)) {
621  assert(count == 1);
622  retval = ERROR_OK;
623  goto out;
624  }
625 
626  /*
627  * The ack belongs to the last command the peripheral processed and a
628  * sticky DAP error makes every command after the first bad one fail
629  * too, so a failure can only be attributed to the queue, not to a
630  * transfer within it. Report how far the queue got instead.
631  */
632  uint32_t processed = FIELD_GET(SWD_CMD_STATUS_CMD_CNTR, status);
633  uint8_t ack = FIELD_GET(SWD_CMD_STATUS_ACK, status);
634 
636  LOG_ERROR("SWD parity error after %" PRIu32 " of %u transfers (status 0x%08" PRIx32 ")",
637  processed, count, status);
638  retval = ERROR_SWD_FAIL;
639  goto out;
640  }
641 
643  LOG_DEBUG("SWD out of ACK.WAIT retries after %" PRIu32 " of %u transfers (status 0x%08" PRIx32 ")",
644  processed, count, status);
645  retval = ERROR_WAIT;
646  goto out;
647  }
648 
649  if (ack != SWD_ACK_OK) {
650  LOG_DEBUG("SWD ack=%u after %" PRIu32 " of %u transfers (status 0x%08" PRIx32 ")",
651  ack, processed, count, status);
652  retval = swd_ack_to_error_code(ack);
653  goto out;
654  }
655 
656  retval = ERROR_OK;
657 
658 out:
659  eud_queue_len = 0;
660  eud_queue_out_len = 0;
661  eud_queue_in_len = 0;
662  return retval;
663 }
664 
665 static int eud_swd_run_queue(void)
666 {
667  if (!eud_queue_len)
668  return ERROR_OK;
669 
670  /*
671  * ADI requires a transaction to be followed by another transaction or at
672  * least 8 idle cycles so that data is clocked through the AP.
673  */
674  return eud_swd_flush_queue(8);
675 }
676 
677 static int eud_init(void)
678 {
679  eud_queue_len = 0;
680  eud_queue_out_len = 0;
681  eud_queue_in_len = 0;
682 
683  int ret = libusb_init(&eud_libusb_ctx);
684  if (ret != LIBUSB_SUCCESS) {
685  LOG_ERROR("libusb_init() failed: %s", libusb_error_name(ret));
686  return ERROR_FAIL;
687  }
688 
689  int retval = eud_open_inner(EUD_SWD_PID, &eud_swd, true);
690  if (retval != ERROR_OK) {
691  retval = eud_open_swd_after_enable();
692  if (retval != ERROR_OK)
693  goto fail;
694  }
695 
696  retval = eud_swd_prepare();
697  if (retval != ERROR_OK) {
698  retval = eud_reopen_swd();
699  if (retval != ERROR_OK)
700  goto fail;
701 
702  retval = eud_swd_prepare();
703  if (retval != ERROR_OK)
704  goto fail;
705  }
706 
707  return ERROR_OK;
708 
709 fail:
710  eud_close(&eud_swd);
711  eud_close(&eud_ctl);
712  libusb_exit(eud_libusb_ctx);
714  return retval;
715 }
716 
717 static int eud_quit(void)
718 {
719  eud_close(&eud_swd);
720  eud_close(&eud_ctl);
721 
722  if (eud_libusb_ctx) {
723  libusb_exit(eud_libusb_ctx);
725  }
726 
727  return ERROR_OK;
728 }
729 
730 static int eud_reset(int trst, int srst)
731 {
732  uint32_t value = EUD_SWD_BB_RESET_DEFAULT;
733 
734  if (srst)
736  if (trst)
738 
739  return eud_swd_bitbang(value);
740 }
741 
742 static int eud_speed(int speed)
743 {
744  if (speed < 0 || speed >= (int)ARRAY_SIZE(eud_speed_khz)) {
745  LOG_ERROR("EUD speed index %d is out of range", speed);
746  return ERROR_FAIL;
747  }
748 
749  eud_speed_index = speed;
750 
751  if (!eud_swd.handle)
752  return ERROR_OK;
753 
754  return eud_swd_set_speed_index(speed);
755 }
756 
757 static int eud_khz(int khz, int *jtag_speed)
758 {
759  int selected = ARRAY_SIZE(eud_speed_khz) - 1;
760 
761  for (unsigned int i = 0; i < ARRAY_SIZE(eud_speed_khz); i++) {
762  if ((unsigned int)khz >= eud_speed_khz[i]) {
763  selected = i;
764  break;
765  }
766  }
767 
768  *jtag_speed = selected;
769  return ERROR_OK;
770 }
771 
772 static int eud_speed_div(int speed, int *khz)
773 {
774  if (speed < 0 || speed >= (int)ARRAY_SIZE(eud_speed_khz))
775  return ERROR_FAIL;
776 
777  *khz = eud_speed_khz[speed];
778  return ERROR_OK;
779 }
780 
781 static const struct swd_driver eud_swd_driver = {
782  .init = eud_swd_init,
783  .switch_seq = eud_swd_switch_seq,
784  .read_reg = eud_swd_read_reg,
785  .write_reg = eud_swd_write_reg,
786  .run = eud_swd_run_queue,
787 };
788 
790  .name = "eud",
791  .transport_ids = TRANSPORT_SWD,
792  .transport_preferred_id = TRANSPORT_SWD,
793 
794  .init = eud_init,
795  .quit = eud_quit,
796  .reset = eud_reset,
797  .speed = eud_speed,
798  .khz = eud_khz,
799  .speed_div = eud_speed_div,
800 
801  .swd_ops = &eud_swd_driver,
802 };
This defines formats and data structures used to talk to ADIv5 entities.
swd_special_seq
Definition: arm_adi_v5.h:236
@ DORMANT_TO_JTAG
Definition: arm_adi_v5.h:243
@ JTAG_TO_SWD
Definition: arm_adi_v5.h:238
@ DORMANT_TO_SWD
Definition: arm_adi_v5.h:242
@ LINE_RESET
Definition: arm_adi_v5.h:237
@ JTAG_TO_DORMANT
Definition: arm_adi_v5.h:239
@ SWD_TO_DORMANT
Definition: arm_adi_v5.h:241
@ SWD_TO_JTAG
Definition: arm_adi_v5.h:240
#define SWD_ACK_OK
Definition: arm_adi_v5.h:31
Support functions to access arbitrary bits in a byte array.
static uint32_t buf_get_u32(const uint8_t *_buffer, unsigned int first, unsigned int num)
Retrieves num bits from _buffer, starting at the first bit, returning the bits in a 32-bit word.
Definition: binarybuffer.h:104
#define FIELD_GET(_mask, _value)
FIELD_GET(_mask, _value) - Extract a value from a bitfield @_mask: Bitfield mask @_value: Bitfield va...
Definition: bitfield.h:57
#define FIELD_PREP(_mask, _value)
FIELD_PREP(_mask, _value) - Prepare a value for insertion into a bitfield @_mask: Bitfield mask @_val...
Definition: bitfield.h:44
uint32_t size
Size of dw_spi_transaction::buffer.
Definition: dw-spi-helper.h:4
static const struct swd_driver eud_swd_driver
Definition: eud.c:781
static int eud_swd_init_defaults(void)
Definition: eud.c:326
static unsigned int eud_queue_len
Definition: eud.c:138
static int eud_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
Definition: eud.c:530
#define EUD_CTL_CMD_CTLOUT_CLR
Definition: eud.c:48
#define EUD_SWD_QUEUE_TRAILER_SIZE
Definition: eud.c:43
#define SWD_CMD_STATUS_CMD_CNTR
Definition: eud.c:114
#define SWD_CMD_TIMING_RETRY_COUNT
Definition: eud.c:95
#define EUD_INTERFACE
Definition: eud.c:26
static int eud_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
Definition: eud.c:524
static unsigned int eud_swd_transfer_out_size(const struct eud_swd_transfer *transfer)
Definition: eud.c:459
#define EUD_SWD_CMD_DELAY
Definition: eud.c:82
static int eud_swd_flush_queue(uint32_t min_idle_clk)
Definition: eud.c:536
static int eud_reopen_swd(void)
Definition: eud.c:305
#define EUD_CTL_SWD_ON_CLEAR
Definition: eud.c:61
#define EUD_CTL_CMD_CTLOUT_SET
Definition: eud.c:47
#define SWD_CMD_STATUS_PARITY_ERR
Definition: eud.c:110
#define EUD_SWD_CMD_PERIPH_RST
Definition: eud.c:115
static int eud_open_swd_after_enable(void)
Definition: eud.c:283
static int eud_swd_switch_seq(enum swd_special_seq seq)
Definition: eud.c:411
#define EUD_SWD_PID
Definition: eud.c:25
static int eud_speed_div(int speed, int *khz)
Definition: eud.c:772
static unsigned int eud_queue_out_len
Definition: eud.c:139
#define SWD_CMD_STATUS_WAIT_TIMEOUT
Definition: eud.c:108
static int eud_bulk_read(struct eud_device *dev, uint8_t *buf, int len)
Definition: eud.c:233
#define EUD_SWD_CMD_STATUS
Definition: eud.c:102
static int eud_libusb_reinit(void)
Definition: eud.c:173
static int eud_ctl_enable_swd(void)
Definition: eud.c:266
static const unsigned int eud_speed_khz[]
Definition: eud.c:143
static unsigned int eud_swd_transfer_in_size(const struct eud_swd_transfer *transfer)
Definition: eud.c:469
static unsigned int eud_queue_in_len
Definition: eud.c:140
static int eud_open_inner(uint16_t pid, struct eud_device *dev, bool quiet)
Definition: eud.c:187
#define SWD_CMD_DITMS_DATA
Definition: eud.c:88
#define EUD_SWD_BUFFER_SIZE
Definition: eud.c:37
#define EUD_SWD_CMD_FREQ
Definition: eud.c:81
#define EUD_EP_IN
Definition: eud.c:28
#define SWD_CMD_DITMS_DATA_BITS
Definition: eud.c:89
#define SWD_CMD_DELAY_TIME
Definition: eud.c:84
static int eud_ditms_u16(uint16_t tms, uint16_t count)
Definition: eud.c:361
#define SWD_CMD_STATUS_ACK
Definition: eud.c:106
#define EUD_SWD_BB_GPIO_SRST_N
Definition: eud.c:72
#define EUD_CTL_PID
Definition: eud.c:24
static struct eud_device eud_ctl
Definition: eud.c:131
static int eud_swd_set_speed_index(int speed_index)
Definition: eud.c:311
#define EUD_USB_TIMEOUT_MS
Definition: eud.c:29
static int eud_open(uint16_t pid, struct eud_device *dev)
Definition: eud.c:209
static struct eud_device eud_swd
Definition: eud.c:132
static int eud_khz(int khz, int *jtag_speed)
Definition: eud.c:757
#define SWD_CMD_DITMS_COUNT
Definition: eud.c:90
static int eud_swd_bitbang(uint32_t payload)
Definition: eud.c:345
static int eud_swd_init(void)
Definition: eud.c:441
#define EUD_EP_OUT
Definition: eud.c:27
static int eud_reset(int trst, int srst)
Definition: eud.c:730
#define EUD_SWD_CMD_BITBANG
Definition: eud.c:86
#define EUD_SWD_CMD_DITMS
Definition: eud.c:87
#define EUD_SWD_BB_RCTLR_SRST_N
Definition: eud.c:71
static libusb_context * eud_libusb_ctx
Definition: eud.c:130
static struct eud_swd_transfer eud_queue[EUD_SWD_BUFFER_SIZE]
Definition: eud.c:137
static int eud_ditms_sequence(const uint8_t *seq, unsigned int bit_len)
Definition: eud.c:376
static int eud_speed_index
Definition: eud.c:141
static int eud_op_no_payload(struct eud_device *dev, uint8_t opcode)
Definition: eud.c:261
#define EUD_SWD_BB_DAP_TRST_N
Definition: eud.c:74
#define EUD_SWD_STATUS_SIZE
Definition: eud.c:44
static int eud_swd_queue_transfer(uint8_t cmd, uint32_t value, uint32_t *read_value, uint32_t ap_delay_clk)
Definition: eud.c:474
#define EUD_SWD_CMD_FLUSH
Definition: eud.c:80
static int eud_op(struct eud_device *dev, uint8_t opcode, uint32_t payload)
Definition: eud.c:251
struct adapter_driver eud_adapter_driver
Definition: eud.c:789
#define EUD_VID
Definition: eud.c:23
static int eud_quit(void)
Definition: eud.c:717
static int eud_bulk_write(struct eud_device *dev, const uint8_t *buf, int len)
Definition: eud.c:214
#define EUD_SWD_DITMS_OUT_SIZE
Definition: eud.c:45
static int eud_swd_prepare(void)
Definition: eud.c:371
#define EUD_CTL_SWD_ON_SET
Definition: eud.c:66
static void eud_close(struct eud_device *dev)
Definition: eud.c:159
static int eud_swd_run_queue(void)
Definition: eud.c:665
#define EUD_SWD_CMD_TIMING
Definition: eud.c:92
static int eud_init(void)
Definition: eud.c:677
#define EUD_SWD_DITMS_MAX_BITS
Definition: eud.c:91
static int eud_speed(int speed)
Definition: eud.c:742
#define EUD_SWD_BB_RESET_DEFAULT
Definition: eud.c:75
int jtag_libusb_error(int err)
Definition: libusb_helper.c:28
void alive_sleep(uint64_t ms)
Definition: log.c:478
#define ERROR_WAIT
Definition: log.h:189
#define LOG_DEBUG_IO(expr ...)
Definition: log.h:116
#define ERROR_FAIL
Definition: log.h:188
#define LOG_ERROR(expr ...)
Definition: log.h:147
#define LOG_DEBUG(expr ...)
Definition: log.h:124
#define ERROR_OK
Definition: log.h:182
#define MIN(a, b)
Definition: replacements.h:22
Represents a driver for a debugging interface.
Definition: interface.h:219
const char *const name
The name of the interface driver.
Definition: interface.h:221
libusb_device_handle * handle
Definition: eud.c:118
uint16_t pid
Definition: eud.c:119
bool claimed
Definition: eud.c:120
uint32_t value
Definition: eud.c:125
uint32_t * read_value
Definition: eud.c:126
uint8_t cmd
Definition: eud.c:124
uint32_t ap_delay_clk
Definition: eud.c:127
int(* init)(void)
Initialize the debug link so it can perform SWD operations.
Definition: swd.h:255
static const unsigned int swd_seq_dormant_to_swd_len
Definition: swd.h:190
static const uint8_t swd_seq_dormant_to_jtag[]
Dormant-to-JTAG sequence.
Definition: swd.h:230
#define SWD_CMD_A32
Definition: swd.h:19
static const uint8_t swd_seq_dormant_to_swd[]
Dormant-to-SWD sequence.
Definition: swd.h:171
static const uint8_t swd_seq_jtag_to_dormant[]
JTAG-to-dormant sequence.
Definition: swd.h:199
static bool swd_cmd_returns_ack(uint8_t cmd)
Test if we can rely on ACK returned by SWD command.
Definition: swd.h:58
#define SWD_CMD_PARK
Definition: swd.h:22
static int swd_ack_to_error_code(uint8_t ack)
Convert SWD ACK value returned from DP to OpenOCD error code.
Definition: swd.h:72
static const unsigned int swd_seq_jtag_to_swd_len
Definition: swd.h:125
#define ERROR_SWD_FAIL
Definition: swd.h:28
static const unsigned int swd_seq_line_reset_len
Definition: swd.h:104
static const unsigned int swd_seq_dormant_to_jtag_len
Definition: swd.h:244
#define SWD_CMD_APNDP
Definition: swd.h:17
static const unsigned int swd_seq_swd_to_dormant_len
Definition: swd.h:159
#define SWD_CMD_START
Definition: swd.h:16
#define SWD_CMD_RNW
Definition: swd.h:18
static const uint8_t swd_seq_line_reset[]
SWD Line reset.
Definition: swd.h:98
#define SWD_CMD_STOP
Definition: swd.h:21
static const uint8_t swd_seq_jtag_to_swd[]
JTAG-to-SWD sequence.
Definition: swd.h:115
static const unsigned int swd_seq_jtag_to_dormant_len
Definition: swd.h:211
static const uint8_t swd_seq_swd_to_dormant[]
SWD-to-dormant sequence.
Definition: swd.h:153
#define TRANSPORT_SWD
Definition: transport.h:20
static void h_u32_to_le(uint8_t *buf, uint32_t val)
Definition: types.h:178
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.
Definition: types.h:57
static uint32_t le_to_h_u32(const uint8_t *buf)
Definition: types.h:112
#define NULL
Definition: usb.h:16
uint8_t status[4]
Definition: vdebug.c:17
uint8_t cmd
Definition: vdebug.c:1
uint8_t count[4]
Definition: vdebug.c:22