OpenOCD
adi_v5_swd.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  *
5  * Copyright (C) 2010 by David Brownell
6  ***************************************************************************/
7 
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36 
37 #include "arm.h"
38 #include "arm_adi_v5.h"
39 #include <helper/time_support.h>
40 
41 #include <transport/transport.h>
42 #include <jtag/adapter.h>
43 #include <jtag/interface.h>
44 
45 #include <jtag/swd.h>
46 
47 /* for debug, set do_sync to true to force synchronous transfers */
48 static bool do_sync;
49 
51 
53 
57 };
58 
59 static int swd_queue_dp_write_inner(struct adiv5_dap *dap, unsigned int reg,
60  uint32_t data);
61 
62 
63 static int swd_send_sequence(struct adiv5_dap *dap, enum swd_special_seq seq)
64 {
65  const struct swd_driver *swd = adiv5_dap_swd_driver(dap);
66  assert(swd);
67 
68  return swd->switch_seq(seq);
69 }
70 
71 static int swd_finish_read(struct adiv5_dap *dap)
72 {
73  const struct swd_driver *swd = adiv5_dap_swd_driver(dap);
74  if (!dap->last_read)
75  return ERROR_OK;
76 
77  int retval = swd->read_reg(swd_cmd(true, false, DP_RDBUFF),
78  dap->last_read, 0);
79  dap->last_read = NULL;
80  return retval;
81 }
82 
83 static int swd_clear_sticky_errors(struct adiv5_dap *dap)
84 {
85  const struct swd_driver *swd = adiv5_dap_swd_driver(dap);
86  assert(swd);
87 
88  LOG_DEBUG_IO("DP ABORT: clear sticky errors");
89  return swd->write_reg(swd_cmd(false, false, DP_ABORT),
91 }
92 
93 static int swd_run_inner(struct adiv5_dap *dap)
94 {
95  const struct swd_driver *swd = adiv5_dap_swd_driver(dap);
96 
97  return swd->run();
98 }
99 
100 static inline int check_sync(struct adiv5_dap *dap)
101 {
102  return do_sync ? swd_run_inner(dap) : ERROR_OK;
103 }
104 
106 static int swd_queue_dp_bankselect(struct adiv5_dap *dap, unsigned int reg)
107 {
108  /* Only register address 0 (ADIv6 only) and 4 are banked. */
109  if (is_adiv6(dap) ? (reg & 0xf) > 4 : (reg & 0xf) != 4)
110  return ERROR_OK;
111 
112  uint32_t sel = (reg >> 4) & DP_SELECT_DPBANK;
113 
114  /* ADIv6 ensures DPBANKSEL = 0 after line reset */
115  if ((dap->select_valid || (is_adiv6(dap) && dap->select_dpbanksel_valid))
116  && (sel == (dap->select & DP_SELECT_DPBANK)))
117  return ERROR_OK;
118 
119  /* Use the AP part of dap->select regardless of dap->select_valid:
120  * if !dap->select_valid
121  * dap->select contains a speculative value likely going to be used
122  * in the following swd_queue_ap_bankselect() */
123  sel |= (uint32_t)(dap->select & SELECT_AP_MASK);
124 
125  LOG_DEBUG_IO("DP BANK SELECT: %" PRIx32, sel);
126 
127  /* dap->select cache gets updated in the following call */
128  return swd_queue_dp_write_inner(dap, DP_SELECT, sel);
129 }
130 
131 static int swd_queue_dp_read_inner(struct adiv5_dap *dap, unsigned int reg,
132  uint32_t *data)
133 {
134  const struct swd_driver *swd = adiv5_dap_swd_driver(dap);
135  assert(swd);
136 
137  int retval = swd_queue_dp_bankselect(dap, reg);
138  if (retval != ERROR_OK)
139  return retval;
140 
141  retval = swd->read_reg(swd_cmd(true, false, reg), data, 0);
142  if (retval != ERROR_OK)
143  return retval;
144 
145  return check_sync(dap);
146 }
147 
148 static int swd_queue_dp_write_inner(struct adiv5_dap *dap, unsigned int reg,
149  uint32_t data)
150 {
151  const struct swd_driver *swd = adiv5_dap_swd_driver(dap);
152  assert(swd);
153 
154  int retval = swd_finish_read(dap);
155  if (retval != ERROR_OK)
156  return retval;
157 
158 
159  if (reg == DP_SELECT) {
160  dap->select = data | (dap->select & (0xffffffffull << 32));
161 
162  retval = swd->write_reg(swd_cmd(false, false, reg), data, 0);
163  if (retval != ERROR_OK)
164  return retval;
165 
166  retval = check_sync(dap);
167  dap->select_valid = (retval == ERROR_OK);
169 
170  return retval;
171  }
172 
173  if (reg == DP_SELECT1)
174  dap->select = ((uint64_t)data << 32) | (dap->select & 0xffffffffull);
175 
176  /* DP_ABORT write is not banked.
177  * Prevent writing DP_SELECT before as it would fail on locked up DP */
178  if (reg != DP_ABORT)
179  retval = swd_queue_dp_bankselect(dap, reg);
180 
181  if (retval == ERROR_OK) {
182  retval = swd->write_reg(swd_cmd(false, false, reg), data, 0);
183  if (retval != ERROR_OK)
184  return retval;
185 
186  retval = check_sync(dap);
187  }
188 
189  if (reg == DP_SELECT1)
190  dap->select1_valid = (retval == ERROR_OK);
191 
192  return retval;
193 }
194 
195 
196 static inline int swd_check_dpidr_in_loop(uint32_t dpidr)
197 {
198  /* Without this check and with SWDIO pulled down a false DP detection
199  * is possible on an unconnected SWD or when target does not respond:
200  * The gradual drop of voltage at not driven SWDIO signal imposed
201  * by discharging pins and wiring capacity can be misinterpreted
202  * by the adapter as a valid read result:
203  * ACK 100, RDATA 00000000000000000000000000000000, PARITY 0 */
204  if ((dpidr & BIT(0)) == 0) {
205  /* The helper is used in the loop waiting for successful connection.
206  * Log to debug only, the error could be temporary */
207  LOG_DEBUG("Connecting DP: bad IDR value 0x%08" PRIx32
208  ", bit 0 should read-as-one", dpidr);
209  return ERROR_FAIL;
210  }
211  return ERROR_OK;
212 }
213 
214 static int swd_check_dpidr_final(struct adiv5_dap *dap, uint32_t dpidr)
215 {
216  unsigned int dp_ver = (dpidr & DP_DPIDR_VERSION_MASK) >> DP_DPIDR_VERSION_SHIFT;
217 
218  LOG_INFO("SWD DPIDR 0x%08" PRIx32 " DPv%u", dpidr, dp_ver);
219 
220  if (is_adiv6(dap)) {
221  if (dp_ver < 3) {
222  LOG_ERROR("Connecting DP: ADIv6 requires DPv3");
223  return ERROR_FAIL;
224  }
225  } else {
226  if (dp_ver > 2) {
227  LOG_ERROR("Connecting DP: DPv3 requires dap create -adiv6 option");
228  return ERROR_FAIL;
229  }
230  }
231  return ERROR_OK;
232 }
233 
234 static int swd_multidrop_select_inner(struct adiv5_dap *dap, uint32_t *dpidr_ptr,
235  uint32_t *dlpidr_ptr, uint32_t dp_abort)
236 {
237  int retval;
238  uint32_t dpidr, dlpidr;
239 
240  assert(dap_is_multidrop(dap));
241 
242  /* Send JTAG_TO_DORMANT and DORMANT_TO_SWD just once
243  * and then use shorter LINE_RESET until communication fails */
247  } else {
249  }
250 
251  /*
252  * Zero dap->select and set dap->select_dpbanksel_valid
253  * to skip the write to DP_SELECT before DPIDR read, avoiding
254  * the protocol error.
255  * Clear the other validity flags because the rest of the DP
256  * SELECT and SELECT1 registers is unknown after line reset.
257  */
258  dap->select = 0;
259  dap->select_dpbanksel_valid = true;
260  dap->select_valid = false;
261  dap->select1_valid = false;
262 
264  if (retval != ERROR_OK)
265  return retval;
266 
267  retval = swd_queue_dp_read_inner(dap, DP_DPIDR, &dpidr);
268  if (retval != ERROR_OK)
269  return retval;
270 
271  /* Ideally just clear ORUN flag which is set by reset */
272  if (dp_abort & DAPABORT)
273  LOG_WARNING("Connecting too long, issuing DAPABORT");
274  else if (dp_abort & STKERRCLR)
275  LOG_DEBUG_IO("DP ABORT: clear sticky errors");
276 
277  retval = swd_queue_dp_write_inner(dap, DP_ABORT, dp_abort);
278  if (retval != ERROR_OK)
279  return retval;
280 
281  if (dp_abort & DAPABORT) {
282  /* Run the queue after DAPABORT */
283  retval = swd_run_inner(dap);
284  if (retval != ERROR_OK)
285  return retval;
286  }
287 
288  retval = swd_queue_dp_read_inner(dap, DP_DLPIDR, &dlpidr);
289  if (retval != ERROR_OK)
290  return retval;
291 
292  retval = swd_run_inner(dap);
293  if (retval != ERROR_OK)
294  return retval;
295 
296  retval = swd_check_dpidr_in_loop(dpidr);
297  if (retval != ERROR_OK)
298  return retval;
299 
300  if ((dpidr & DP_DPIDR_VERSION_MASK) < (2UL << DP_DPIDR_VERSION_SHIFT)) {
301  LOG_INFO("Read DPIDR 0x%08" PRIx32
302  " has version < 2. A non multidrop capable device connected?",
303  dpidr);
304  return ERROR_FAIL;
305  }
306 
307  /* TODO: check TARGETID if DLIPDR is same for more than one DP */
308  uint32_t expected_dlpidr = DP_DLPIDR_PROTVSN |
310  if (dlpidr != expected_dlpidr) {
311  LOG_INFO("Read incorrect DLPIDR 0x%08" PRIx32
312  " (possibly CTRL/STAT value)",
313  dlpidr);
314  return ERROR_FAIL;
315  }
316 
317  LOG_DEBUG_IO("Selected DP_TARGETSEL 0x%08" PRIx32, dap->multidrop_targetsel);
320 
321  if (dpidr_ptr)
322  *dpidr_ptr = dpidr;
323 
324  if (dlpidr_ptr)
325  *dlpidr_ptr = dlpidr;
326 
327  return retval;
328 }
329 
330 static int swd_multidrop_select(struct adiv5_dap *dap)
331 {
332  if (!dap_is_multidrop(dap))
333  return ERROR_OK;
334 
335  if (swd_multidrop_selected_dap == dap)
336  return ERROR_OK;
337 
338  int retval = ERROR_OK;
339  for (unsigned int retry = 0; ; retry++) {
341  if (retval == ERROR_OK)
342  break;
343 
345  if (retry > 3) {
346  LOG_ERROR("Failed to select multidrop %s", adiv5_dap_name(dap));
347  dap->do_reconnect = true;
348  return retval;
349  }
350 
351  LOG_DEBUG("Failed to select multidrop %s, retrying...",
352  adiv5_dap_name(dap));
353  }
354 
355  dap->do_reconnect = false;
356  return retval;
357 }
358 
359 static int swd_connect_multidrop(struct adiv5_dap *dap)
360 {
361  int retval;
362  uint32_t dpidr = 0xdeadbeef;
363  uint32_t dlpidr = 0xdeadbeef;
364  int64_t timeout = timeval_ms() + 500;
365  bool timeout_reached = false;
366  bool send_dapabort = false;
367  bool switch_to_dormant = false;
368 
369  for (unsigned int retry_after_timeout = 0; ; ) {
370  /* Do not make any assumptions about SWD state in case of reconnect */
371  if (dap->do_reconnect)
373 
374  /* Clear link state, including the SELECT cache. */
375  dap->do_reconnect = false;
378 
379  uint32_t dp_abort = STKCMPCLR | STKERRCLR | WDERRCLR | ORUNERRCLR;
380  if (send_dapabort)
381  dp_abort |= DAPABORT;
382 
383  retval = swd_multidrop_select_inner(dap, &dpidr, &dlpidr, dp_abort);
384  if (retval == ERROR_OK)
385  break;
386 
387  /* Wait gracefully until timeout. Then try more force: DAPABORT */
388  timeout_reached = timeval_ms() > timeout;
389  if (timeout_reached) {
390  retry_after_timeout++;
391  if (retry_after_timeout > 6)
392  break;
393 
394  /* Alternate DAPABORT and DORMANT methods to revive stalled DP */
395  send_dapabort = retry_after_timeout & 1;
396  switch_to_dormant = !send_dapabort;
397  }
398 
399  alive_sleep((retval == ERROR_WAIT && !timeout_reached) ? 10 : 1);
400 
402 
403  if (switch_to_dormant)
405  }
406 
407  if (retval != ERROR_OK) {
409  LOG_ERROR("Failed to connect multidrop %s", adiv5_dap_name(dap));
410  return retval;
411  }
412 
414 
415  retval = swd_check_dpidr_final(dap, dpidr);
416  LOG_INFO("multidrop DLPIDR 0x%08" PRIx32, dlpidr);
417 
418  return retval;
419 }
420 
421 static int swd_connect_single(struct adiv5_dap *dap)
422 {
423  int dpidr_read_retval = ERROR_FAIL;
424  int retval = ERROR_FAIL;
425  int64_t timeout = timeval_ms() + 500;
426  bool timeout_reached = false;
427  bool send_dapabort = false;
428  bool switch_to_dormant = false;
429  uint32_t dpidr = 0xdeadbeef;
430  bool dpidr_was_read = false;
431 
432  for (unsigned int retry_after_timeout = 0; ; ) {
433  if (dpidr_read_retval != ERROR_OK) {
434  if (dap->switch_through_dormant) {
435  if (switch_to_dormant) {
437  switch_to_dormant = false;
438  } else {
440  }
442  } else {
444  }
445 
446  /* Clear link state, including the SELECT cache. */
447  dap->do_reconnect = false;
449 
450  /* The sequences to enter in SWD (JTAG_TO_SWD and DORMANT_TO_SWD) end
451  * with a SWD line reset sequence (50 clk with SWDIO high).
452  * From ARM IHI 0031F ADIv5.2 and ARM IHI 0074C ADIv6.0,
453  * chapter B4.3.3 "Connection and line reset sequence":
454  * - DPv3 (ADIv6) only: line reset sets DP_SELECT_DPBANK to zero;
455  * - read of DP_DPIDR takes the connection out of reset;
456  * - write of DP_TARGETSEL keeps the connection in reset;
457  * - other accesses return protocol error (SWDIO not driven by target).
458  *
459  * dap_invalidate_cache() sets dap->select to zero and all validity
460  * flags to invalid. Set dap->select_dpbanksel_valid only
461  * to skip the write to DP_SELECT, avoiding the protocol error.
462  * Read DP_DPIDR to get out of reset.
463  */
464  dap->select_dpbanksel_valid = true;
465 
466  retval = swd_queue_dp_read_inner(dap, DP_DPIDR, &dpidr);
467  if (retval == ERROR_OK)
468  retval = swd_run_inner(dap);
469 
470  if (retval == ERROR_OK) {
471  LOG_DEBUG("DP IDR 0x%08" PRIx32, dpidr);
472  retval = swd_check_dpidr_in_loop(dpidr);
473  }
474 
475  dpidr_read_retval = retval;
476  if (retval == ERROR_OK) {
477  dpidr_was_read = true;
478  } else {
480  }
481  }
482 
483  if (dpidr_read_retval == ERROR_OK) {
484  /* Overrun error has been set by line reset seq if overrun detection
485  * is active. Clear it before write to DP SELECT */
486 
487  if (send_dapabort) {
488  LOG_WARNING("Too long SWD WAIT, issuing DAPABORT");
489  /* DAPABORT needs some number of clock cycles to have an effect.
490  * If a wait sensitive operation immediately follows,
491  * it may return WAIT. Run the queue to be safe */
492  retval = swd_queue_dp_write_inner(dap, DP_ABORT,
494  if (retval == ERROR_OK)
495  retval = swd_run_inner(dap);
496 
497  } else {
498  retval = swd_clear_sticky_errors(dap);
499  }
500 
501  /* CMSIS-DAP adapter can return ERROR_WAIT here in contradiction
502  * to ADI rules. WAIT comes from RDBUFF cmd, added by the adapter
503  * to get a delayed result (which is not applicable after write
504  * to DP ABORT). Fortunately it makes no difference whether we got
505  * WAIT earlier here or later from DP SELECT write. */
506 
507  /* Write DP SELECT regardless of the cached value.
508  * ADIv5 needs it before access to banked reg 4 (CTRL/STAT etc..)
509  * ADIv6 ensures DPBANKSEL field is initialized to zero
510  * Write DP SELECT anyway as the operation detects stalled DP. */
511 
512  /* We should be aware the overrun detection in DP has not been set
513  * and could be left from a previous operation in any state.
514  * Writing the zero data and zero parity is safe because in the case
515  * of WAIT or FAULT response the adapter expecting ORUNDETECT = 1
516  * sends the data whereas the DP with ORUNDETECT = 0 looks for
517  * the start bit of a next cmd (see IHI0031F, B4.2.5 and B4.2.6)
518  * Zero data prevents DP from interpreting them as a false cmd. */
519 
520  if (retval == ERROR_OK) {
521  uint32_t sel = 0;
522  LOG_DEBUG_IO("DP BANK SELECT: %" PRIx32, sel);
523 
524  /* dap->select cache gets updated in the following call */
525  retval = swd_queue_dp_write_inner(dap, DP_SELECT, sel);
526  }
527 
528  /* The overrun detection has not been enabled in DP.
529  * Running the queue between DP SELECT write and the following
530  * banked register (CTRL/STAT) access detects failed write
531  * in time to prevent misplaced access to other banks. */
532  if (retval == ERROR_OK)
533  retval = swd_run_inner(dap);
534 
535  if (retval == ERROR_OK)
536  break;
537  }
538 
539  /* Wait gracefully until timeout. Then try more force: DAPABORT */
540  timeout_reached = timeval_ms() > timeout;
541  if (timeout_reached) {
542  retry_after_timeout++;
543  if (retry_after_timeout > 6)
544  break;
545  }
546 
547  alive_sleep((retval == ERROR_WAIT && !timeout_reached) ? 10 : 1);
548 
549  if (dpidr_read_retval == ERROR_OK) {
550  /* ERROR_WAIT means the DP is stalled by a not finished previous
551  * SWD operation - retry without re-sending sequences during
552  * graceful wait */
553  if (retval != ERROR_WAIT) {
554  /* Force SWD sequence and IDR read */
555  dpidr_read_retval = ERROR_FAIL;
556  } else if (timeout_reached) {
557  /* Alternate DAPABORT and DORMANT methods to revive stalled DP */
558  send_dapabort = retry_after_timeout & 1;
559  switch_to_dormant = !send_dapabort;
560  if (switch_to_dormant)
561  dap->switch_through_dormant = true;
562 
563  /* Force SWD sequence and IDR read */
564  dpidr_read_retval = ERROR_FAIL;
565  }
566  }
567  }
568 
569  if (retval == ERROR_OK)
570  return swd_check_dpidr_final(dap, dpidr);
571 
572  if (dpidr_was_read)
573  LOG_ERROR("Error connecting DP: cannot write to ABORT / SELECT");
574  else
575  LOG_ERROR("Error connecting DP: cannot read IDR");
576 
577  return retval;
578 }
579 
580 static int swd_pre_connect(struct adiv5_dap *dap)
581 {
583 
584  return ERROR_OK;
585 }
586 
587 static int swd_connect(struct adiv5_dap *dap)
588 {
589  int status;
590 
591  /* FIXME validate transport config ... is the
592  * configured DAP present (check IDCODE)?
593  */
594 
595  /* Check if we should reset srst already when connecting, but not if reconnecting. */
596  if (!dap->do_reconnect) {
598 
602  else
603  LOG_WARNING("\'srst_nogate\' reset_config option is required");
604  }
605  }
606 
607  if (dap_is_multidrop(dap))
609  else
610  status = swd_connect_single(dap);
611 
612  if (status == ERROR_OK)
613  status = dap_dp_init(dap);
614 
615  return status;
616 }
617 
618 static int swd_error_recovery(struct adiv5_dap *dap, int err,
619  enum swd_recovery_mode mode)
620 {
621  /* TODO: do not reconnect on all errors */
622  if (err != ERROR_OK) {
623  dap->do_reconnect = true;
624  LOG_DEBUG_IO("Reconnect on next SWD op");
625  }
626  return err;
627 }
628 
629 static int swd_check_reconnect(struct adiv5_dap *dap)
630 {
631  if (dap->do_reconnect)
632  return swd_connect(dap);
633 
634  return ERROR_OK;
635 }
636 
637 static int swd_queue_ap_abort(struct adiv5_dap *dap, uint8_t *ack)
638 {
639  const struct swd_driver *swd = adiv5_dap_swd_driver(dap);
640  assert(swd);
641 
642  /* TODO: Send DAPABORT in swd_multidrop_select_inner()
643  * in the case the multidrop dap is not selected?
644  * swd_queue_ap_abort() is not currently used anyway...
645  */
646  int retval = swd_multidrop_select(dap);
647  if (retval != ERROR_OK)
648  return retval;
649 
650  retval = swd->write_reg(swd_cmd(false, false, DP_ABORT),
652  if (retval != ERROR_OK)
653  return retval;
654 
655  return check_sync(dap);
656 }
657 
658 static int swd_queue_dp_read(struct adiv5_dap *dap, unsigned int reg,
659  uint32_t *data)
660 {
661  int retval = swd_check_reconnect(dap);
662  if (retval != ERROR_OK)
663  return retval;
664 
665  retval = swd_multidrop_select(dap);
666  if (retval != ERROR_OK)
667  return retval;
668 
669  retval = swd_queue_dp_read_inner(dap, reg, data);
670  return swd_error_recovery(dap, retval, SWD_RECOVERY_QUEUE);
671 }
672 
673 static int swd_queue_dp_write(struct adiv5_dap *dap, unsigned int reg,
674  uint32_t data)
675 {
676  int retval = swd_check_reconnect(dap);
677  if (retval != ERROR_OK)
678  return retval;
679 
680  retval = swd_multidrop_select(dap);
681  if (retval != ERROR_OK)
682  return retval;
683 
684  retval = swd_queue_dp_write_inner(dap, reg, data);
685  return swd_error_recovery(dap, retval, SWD_RECOVERY_QUEUE);
686 }
687 
689 static int swd_queue_ap_bankselect(struct adiv5_ap *ap, unsigned int reg)
690 {
691  int retval;
692  struct adiv5_dap *dap = ap->dap;
693  uint64_t sel;
694 
695  if (is_adiv6(dap))
696  sel = ap->ap_num | (reg & 0x00000FF0);
697  else
698  sel = (ap->ap_num << 24) | (reg & ADIV5_DP_SELECT_APBANK);
699 
700  uint64_t sel_diff = (sel ^ dap->select) & SELECT_AP_MASK;
701 
702  bool set_select = !dap->select_valid || (sel_diff & 0xffffffffull);
703  bool set_select1 = is_adiv6(dap) && dap->asize > 32
704  && (!dap->select1_valid
705  || sel_diff & (0xffffffffull << 32));
706 
707  if (set_select && set_select1) {
708  /* Prepare DP bank for DP_SELECT1 now to save one write */
709  sel |= (DP_SELECT1 & 0x000000f0) >> 4;
710  } else {
711  /* Use the DP part of dap->select regardless of dap->select_valid:
712  * if !dap->select_valid
713  * dap->select contains a speculative value likely going to be used
714  * in the following swd_queue_dp_bankselect().
715  * Moreover dap->select_valid should never be false here as a DP bank
716  * is always selected before selecting an AP bank */
717  sel |= dap->select & DP_SELECT_DPBANK;
718  }
719 
720  if (set_select) {
721  LOG_DEBUG_IO("AP BANK SELECT: %" PRIx32, (uint32_t)sel);
722 
723  retval = swd_queue_dp_write(dap, DP_SELECT, (uint32_t)sel);
724  retval = swd_error_recovery(dap, retval, SWD_RECOVERY_QUEUE);
725  if (retval != ERROR_OK)
726  return retval;
727  }
728 
729  if (set_select1) {
730  LOG_DEBUG_IO("AP BANK SELECT1: %" PRIx32, (uint32_t)(sel >> 32));
731 
732  retval = swd_queue_dp_write(dap, DP_SELECT1, (uint32_t)(sel >> 32));
733  retval = swd_error_recovery(dap, retval, SWD_RECOVERY_QUEUE);
734  if (retval != ERROR_OK)
735  return retval;
736  }
737 
738  return ERROR_OK;
739 }
740 
741 static int swd_queue_ap_read(struct adiv5_ap *ap, unsigned int reg,
742  uint32_t *data)
743 {
744  struct adiv5_dap *dap = ap->dap;
745  const struct swd_driver *swd = adiv5_dap_swd_driver(dap);
746  assert(swd);
747 
748  int retval = swd_check_reconnect(dap);
749  if (retval != ERROR_OK)
750  return retval;
751 
752  retval = swd_multidrop_select(dap);
753  if (retval != ERROR_OK)
754  return retval;
755 
756  retval = swd_queue_ap_bankselect(ap, reg);
757  if (retval != ERROR_OK)
758  return retval;
759 
760  retval = swd->read_reg(swd_cmd(true, true, reg),
761  dap->last_read, ap->memaccess_tck);
762  retval = swd_error_recovery(dap, retval, SWD_RECOVERY_QUEUE);
763  if (retval != ERROR_OK)
764  return retval;
765 
766  dap->last_read = data;
767 
768  return check_sync(dap);
769 }
770 
771 static int swd_queue_ap_write(struct adiv5_ap *ap, unsigned int reg,
772  uint32_t data)
773 {
774  struct adiv5_dap *dap = ap->dap;
775  const struct swd_driver *swd = adiv5_dap_swd_driver(dap);
776  assert(swd);
777 
778  int retval = swd_check_reconnect(dap);
779  if (retval != ERROR_OK)
780  return retval;
781 
782  retval = swd_multidrop_select(dap);
783  if (retval != ERROR_OK)
784  return retval;
785 
786  retval = swd_finish_read(dap);
787  retval = swd_error_recovery(dap, retval, SWD_RECOVERY_QUEUE);
788  if (retval != ERROR_OK)
789  return retval;
790 
791  retval = swd_queue_ap_bankselect(ap, reg);
792  if (retval != ERROR_OK)
793  return retval;
794 
795  retval = swd->write_reg(swd_cmd(false, true, reg), data, ap->memaccess_tck);
796  retval = swd_error_recovery(dap, retval, SWD_RECOVERY_QUEUE);
797  if (retval != ERROR_OK)
798  return retval;
799 
800  return check_sync(dap);
801 }
802 
804 static int swd_run(struct adiv5_dap *dap)
805 {
806  int retval = swd_multidrop_select(dap);
807  if (retval != ERROR_OK)
808  return retval;
809 
810  retval = swd_finish_read(dap);
811  retval = swd_error_recovery(dap, retval, SWD_RECOVERY_QUEUE);
812  if (retval != ERROR_OK)
813  return retval;
814 
815  retval = swd_run_inner(dap);
816  return swd_error_recovery(dap, retval, SWD_RECOVERY_RUN);
817 }
818 
820 static void swd_quit(struct adiv5_dap *dap)
821 {
822  const struct swd_driver *swd = adiv5_dap_swd_driver(dap);
823  static bool done;
824 
825  /* There is no difference if the sequence is sent at the last
826  * or the first swd_quit() call, send it just once */
827  if (done)
828  return;
829 
830  done = true;
831  if (dap_is_multidrop(dap)) {
832  /* Emit the switch seq to dormant state regardless the state mirrored
833  * in swd_multidrop_in_swd_state. Doing so ensures robust operation
834  * in the case the variable is out of sync.
835  * Sending SWD_TO_DORMANT makes no change if the DP is already dormant. */
838  /* Revisit!
839  * Leaving DPs in dormant state was tested and offers some safety
840  * against DPs mismatch in case of unintentional use of non-multidrop SWD.
841  * To put SWJ-DPs to power-on state issue
842  * swd->switch_seq(DORMANT_TO_JTAG);
843  */
844  } else {
845  if (dap->switch_through_dormant) {
848  } else {
849  swd->switch_seq(SWD_TO_JTAG);
850  }
851  }
852 
853  /* flush the queue to shift out the sequence before exit */
854  swd->run();
855 }
856 
857 const struct dap_ops swd_dap_ops = {
859  .connect = swd_connect,
860  .send_sequence = swd_send_sequence,
861  .queue_dp_read = swd_queue_dp_read,
862  .queue_dp_write = swd_queue_dp_write,
863  .queue_ap_read = swd_queue_ap_read,
864  .queue_ap_write = swd_queue_ap_write,
865  .queue_ap_abort = swd_queue_ap_abort,
866  .run = swd_run,
867  .quit = swd_quit,
868 };
869 
870 static const struct command_registration swd_commands[] = {
871  {
872  /*
873  * Set up SWD and JTAG targets identically, unless/until
874  * infrastructure improves ... meanwhile, ignore all
875  * JTAG-specific stuff like IR length for SWD.
876  *
877  * REVISIT can we verify "just one SWD DAP" here/early?
878  */
879  .name = "newdap",
880  .handler = handle_jtag_newtap,
881  .mode = COMMAND_CONFIG,
882  .help = "declare a new SWD DAP",
883  .usage = "basename dap_type ['-irlen' count] "
884  "['-enable'|'-disable'] "
885  "['-expected-id' number] "
886  "['-ignore-version'] "
887  "['-ignore-bypass'] "
888  "['-ircapture' number] "
889  "['-ir-bypass' number] "
890  "['-mask' number]",
891  },
893 };
894 
895 static const struct command_registration swd_handlers[] = {
896  {
897  .name = "swd",
898  .mode = COMMAND_ANY,
899  .help = "SWD command group",
900  .chain = swd_commands,
901  .usage = "",
902  },
904 };
905 
906 static int swd_select(struct command_context *ctx)
907 {
908  const struct swd_driver *swd = adapter_driver->swd_ops;
909  int retval;
910 
911  retval = register_commands(ctx, NULL, swd_handlers);
912  if (retval != ERROR_OK)
913  return retval;
914 
915  /* be sure driver is in SWD mode; start
916  * with hardware default TRN (1), it can be changed later
917  */
918  if (!swd || !swd->read_reg || !swd->write_reg || !swd->init) {
919  LOG_DEBUG("no SWD driver?");
920  return ERROR_FAIL;
921  }
922 
923  retval = swd->init();
924  if (retval != ERROR_OK) {
925  LOG_DEBUG("can't init SWD driver");
926  return retval;
927  }
928 
929  return retval;
930 }
931 
932 static int swd_init(struct command_context *ctx)
933 {
934  /* nothing done here, SWD is initialized
935  * together with the DAP */
936  return ERROR_OK;
937 }
938 
939 static struct transport swd_transport = {
940  .id = TRANSPORT_SWD,
941  .select = swd_select,
942  .init = swd_init,
943 };
944 
945 static void swd_constructor(void) __attribute__((constructor));
946 static void swd_constructor(void)
947 {
949 }
950 
955 {
957 }
static int swd_multidrop_select(struct adiv5_dap *dap)
Definition: adi_v5_swd.c:330
const struct dap_ops swd_dap_ops
Definition: adi_v5_swd.c:857
static int swd_check_dpidr_final(struct adiv5_dap *dap, uint32_t dpidr)
Definition: adi_v5_swd.c:214
static int swd_connect_single(struct adiv5_dap *dap)
Definition: adi_v5_swd.c:421
static int swd_queue_ap_abort(struct adiv5_dap *dap, uint8_t *ack)
Definition: adi_v5_swd.c:637
static const struct command_registration swd_commands[]
Definition: adi_v5_swd.c:870
static int swd_finish_read(struct adiv5_dap *dap)
Definition: adi_v5_swd.c:71
static int swd_queue_dp_write_inner(struct adiv5_dap *dap, unsigned int reg, uint32_t data)
Definition: adi_v5_swd.c:148
static int check_sync(struct adiv5_dap *dap)
Definition: adi_v5_swd.c:100
static int swd_queue_ap_read(struct adiv5_ap *ap, unsigned int reg, uint32_t *data)
Definition: adi_v5_swd.c:741
static int swd_queue_dp_write(struct adiv5_dap *dap, unsigned int reg, uint32_t data)
Definition: adi_v5_swd.c:673
swd_recovery_mode
Definition: adi_v5_swd.c:54
@ SWD_RECOVERY_RUN
Definition: adi_v5_swd.c:56
@ SWD_RECOVERY_QUEUE
Definition: adi_v5_swd.c:55
static int swd_run_inner(struct adiv5_dap *dap)
Definition: adi_v5_swd.c:93
static int swd_connect(struct adiv5_dap *dap)
Definition: adi_v5_swd.c:587
static int swd_error_recovery(struct adiv5_dap *dap, int err, enum swd_recovery_mode mode)
Definition: adi_v5_swd.c:618
static int swd_pre_connect(struct adiv5_dap *dap)
Definition: adi_v5_swd.c:580
static int swd_connect_multidrop(struct adiv5_dap *dap)
Definition: adi_v5_swd.c:359
static int swd_init(struct command_context *ctx)
Definition: adi_v5_swd.c:932
static int swd_run(struct adiv5_dap *dap)
Executes all queued DAP operations.
Definition: adi_v5_swd.c:804
static int swd_select(struct command_context *ctx)
Definition: adi_v5_swd.c:906
static int swd_queue_ap_bankselect(struct adiv5_ap *ap, unsigned int reg)
Select the AP register bank.
Definition: adi_v5_swd.c:689
static int swd_queue_dp_bankselect(struct adiv5_dap *dap, unsigned int reg)
Select the DP register bank.
Definition: adi_v5_swd.c:106
static int swd_queue_dp_read_inner(struct adiv5_dap *dap, unsigned int reg, uint32_t *data)
Definition: adi_v5_swd.c:131
static int swd_clear_sticky_errors(struct adiv5_dap *dap)
Definition: adi_v5_swd.c:83
static struct transport swd_transport
Definition: adi_v5_swd.c:939
static bool swd_multidrop_in_swd_state
Definition: adi_v5_swd.c:52
bool transport_is_swd(void)
Returns true if the current debug session is using SWD as its transport.
Definition: adi_v5_swd.c:954
static bool do_sync
Definition: adi_v5_swd.c:48
static int swd_queue_ap_write(struct adiv5_ap *ap, unsigned int reg, uint32_t data)
Definition: adi_v5_swd.c:771
static int swd_check_reconnect(struct adiv5_dap *dap)
Definition: adi_v5_swd.c:629
static int swd_send_sequence(struct adiv5_dap *dap, enum swd_special_seq seq)
Definition: adi_v5_swd.c:63
static void swd_quit(struct adiv5_dap *dap)
Put the SWJ-DP back to JTAG mode.
Definition: adi_v5_swd.c:820
static int swd_queue_dp_read(struct adiv5_dap *dap, unsigned int reg, uint32_t *data)
Definition: adi_v5_swd.c:658
static const struct command_registration swd_handlers[]
Definition: adi_v5_swd.c:895
static int swd_multidrop_select_inner(struct adiv5_dap *dap, uint32_t *dpidr_ptr, uint32_t *dlpidr_ptr, uint32_t dp_abort)
Definition: adi_v5_swd.c:234
static int swd_check_dpidr_in_loop(uint32_t dpidr)
Definition: adi_v5_swd.c:196
static struct adiv5_dap * swd_multidrop_selected_dap
Definition: adi_v5_swd.c:50
static void swd_constructor(void)
Definition: adi_v5_swd.c:945
Holds the interface to ARM cores.
int dap_dp_init(struct adiv5_dap *dap)
Initialize a DAP.
Definition: arm_adi_v5.c:787
void dap_invalidate_cache(struct adiv5_dap *dap)
Invalidate cached DP select and cached TAR and CSW of all APs.
Definition: arm_adi_v5.c:764
This defines formats and data structures used to talk to ADIv5 entities.
#define DP_ABORT
Definition: arm_adi_v5.h:46
#define WDERRCLR
Definition: arm_adi_v5.h:71
#define DP_SELECT
Definition: arm_adi_v5.h:57
const struct swd_driver * adiv5_dap_swd_driver(struct adiv5_dap *self)
Definition: arm_dap.c:59
#define DP_TARGETSEL_INSTANCEID_MASK
Definition: arm_adi_v5.h:114
#define DP_SELECT1
Definition: arm_adi_v5.h:55
#define SELECT_AP_MASK
Definition: arm_adi_v5.h:107
#define STKERRCLR
Definition: arm_adi_v5.h:70
#define DP_DPIDR
Definition: arm_adi_v5.h:45
#define DP_RDBUFF
Definition: arm_adi_v5.h:58
#define DP_DLPIDR
Definition: arm_adi_v5.h:53
#define ORUNERRCLR
Definition: arm_adi_v5.h:72
#define DP_DLPIDR_PROTVSN
Definition: arm_adi_v5.h:98
#define STKCMPCLR
Definition: arm_adi_v5.h:69
#define DAPABORT
Definition: arm_adi_v5.h:68
#define DP_DPIDR_VERSION_MASK
Definition: arm_adi_v5.h:65
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
const char * adiv5_dap_name(struct adiv5_dap *self)
Definition: arm_dap.c:53
static bool is_adiv6(const struct adiv5_dap *dap)
Check if DAP is ADIv6.
Definition: arm_adi_v5.h:523
#define DP_TARGETSEL
Definition: arm_adi_v5.h:59
#define DP_DPIDR_VERSION_SHIFT
Definition: arm_adi_v5.h:64
static bool dap_is_multidrop(struct adiv5_dap *dap)
Check if SWD multidrop configuration is valid.
Definition: arm_adi_v5.h:767
#define ADIV5_DP_SELECT_APBANK
Definition: arm_adi_v5.h:101
#define DP_SELECT_DPBANK
Definition: arm_adi_v5.h:102
enum arm_mode mode
Definition: armv4_5.c:280
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:256
static int register_commands(struct command_context *cmd_ctx, const char *cmd_prefix, const struct command_registration *cmds)
Register one or more commands in the specified context, as children of parent (or top-level commends,...
Definition: command.h:277
@ COMMAND_CONFIG
Definition: command.h:41
@ COMMAND_ANY
Definition: command.h:42
static enum reset_types jtag_reset_config
Definition: jtag/core.c:89
enum reset_types jtag_get_reset_config(void)
Definition: jtag/core.c:1730
int adapter_assert_reset(void)
Definition: jtag/core.c:1875
reset_types
Definition: jtag.h:215
@ RESET_SRST_NO_GATING
Definition: jtag.h:224
@ RESET_CNCT_UNDER_SRST
Definition: jtag.h:225
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 LOG_WARNING(expr ...)
Definition: log.h:144
#define ERROR_FAIL
Definition: log.h:188
#define LOG_ERROR(expr ...)
Definition: log.h:147
#define LOG_INFO(expr ...)
Definition: log.h:141
#define LOG_DEBUG(expr ...)
Definition: log.h:124
#define ERROR_OK
Definition: log.h:182
struct qn908x_flash_bank __attribute__
Definition: armv8.c:1059
#define BIT(nr)
Definition: stm32l4x.h:18
Represents a driver for a debugging interface.
Definition: interface.h:219
const struct swd_driver * swd_ops
Low-level SWD APIs.
Definition: interface.h:365
This represents an ARM Debug Interface (v5) Access Port (AP).
Definition: arm_adi_v5.h:250
uint64_t ap_num
ADIv5: Number of this AP (0~255) ADIv6: Base address of this AP (4k aligned) TODO: to be more coheren...
Definition: arm_adi_v5.h:261
struct adiv5_dap * dap
DAP this AP belongs to.
Definition: arm_adi_v5.h:254
uint32_t memaccess_tck
Configures how many extra tck clocks are added after starting a MEM-AP access before we try to read i...
Definition: arm_adi_v5.h:306
This represents an ARM Debug Interface (v5) Debug Access Port (DAP).
Definition: arm_adi_v5.h:348
bool select_valid
Validity of DP SELECT cache.
Definition: arm_adi_v5.h:372
bool select1_valid
Definition: arm_adi_v5.h:373
struct adiv5_ap ap[DP_APSEL_MAX+1]
Definition: arm_adi_v5.h:364
bool select_dpbanksel_valid
Partial DPBANKSEL validity for SWD only.
Definition: arm_adi_v5.h:383
bool do_reconnect
Signals that an attempt to reestablish communication afresh should be performed before the next acces...
Definition: arm_adi_v5.h:414
uint32_t * last_read
Holds the pointer to the destination word for the last queued read, for use with posted AP read seque...
Definition: arm_adi_v5.h:392
bool switch_through_dormant
Record if enter in SWD required passing through DORMANT.
Definition: arm_adi_v5.h:430
uint32_t multidrop_targetsel
Value to select DP in SWD multidrop mode or DP_TARGETSEL_INVALID.
Definition: arm_adi_v5.h:421
uint64_t select
Cache for DP SELECT and SELECT1 (ADIv6) register.
Definition: arm_adi_v5.h:370
unsigned int asize
Definition: arm_adi_v5.h:436
const char * name
Definition: command.h:239
const char * usage
a string listing the options and arguments, required or optional
Definition: command.h:244
Transport-neutral representation of queued DAP transactions, supporting both JTAG and SWD transports.
Definition: arm_adi_v5.h:446
int(* pre_connect_init)(struct adiv5_dap *dap)
Optional; called once on the first enabled dap before connecting.
Definition: arm_adi_v5.h:448
Definition: register.h:111
int(* switch_seq)(enum swd_special_seq seq)
Queue a special SWDIO sequence.
Definition: swd.h:264
int(* read_reg)(uint8_t cmd, uint32_t *value, uint32_t ap_delay_hint)
Queued read of an AP or DP register.
Definition: swd.h:276
int(* init)(void)
Initialize the debug link so it can perform SWD operations.
Definition: swd.h:255
int(* run)(void)
Execute any queued transactions and collect the result.
Definition: swd.h:296
int(* write_reg)(uint8_t cmd, uint32_t value, uint32_t ap_delay_hint)
Queued write of an AP or DP register.
Definition: swd.h:288
Definition: psoc6.c:83
Wrapper for transport lifecycle operations.
Definition: transport.h:55
unsigned int id
Each transport has a unique ID, used to select it from among the alternatives.
Definition: transport.h:60
static uint8_t swd_cmd(bool is_read, bool is_ap, uint8_t regnum)
Construct a "cmd" byte, in lSB bit order, which swd_driver.read_reg() and swd_driver....
Definition: swd.h:35
int64_t timeval_ms(void)
struct transport * get_current_transport(void)
Returns the transport currently being used by this debug or programming session.
Definition: transport.c:252
int transport_register(struct transport *new_transport)
Registers a transport.
Definition: transport.c:211
#define TRANSPORT_SWD
Definition: transport.h:20
#define NULL
Definition: usb.h:16
uint8_t status[4]
Definition: vdebug.c:17