1 // SPDX-License-Identifier: GPL-2.0-or-later
3 /***************************************************************************
4 * Copyright (C) 2006 by Magnus Lundin
7 * Copyright (C) 2008 by Spencer Oliver
10 * Copyright (C) 2009 by Oyvind Harboe
11 * oyvind.harboe@zylin.com
13 * Copyright (C) 2009-2010 by David Brownell
15 * Copyright (C) 2020-2021, Ampere Computing LLC *
16 ***************************************************************************/
20 * This file implements JTAG transport support for cores implementing
21 the ARM Debug Interface version 5 (ADIv5) and version 6 (ADIv6).
29 #include "arm_adi_v5.h"
30 #include <helper/time_support.h>
31 #include <helper/list.h>
34 /*#define DEBUG_WAIT*/
36 /* JTAG instructions/registers for JTAG-DP and SWJ-DP */
37 #define JTAG_DP_ABORT 0xF8
38 #define JTAG_DP_DPACC 0xFA
39 #define JTAG_DP_APACC 0xFB
40 #define JTAG_DP_IDCODE 0xFE
42 /* three-bit ACK values for DPACC and APACC reads */
43 #define JTAG_ACK_WAIT 0x1 /* ADIv5 and ADIv6 */
44 #define JTAG_ACK_OK_FAULT 0x2 /* ADIv5 */
45 #define JTAG_ACK_FAULT 0x2 /* ADIv6 */
46 #define JTAG_ACK_OK 0x4 /* ADIV6 */
48 static int jtag_ap_q_abort(struct adiv5_dap
*dap
, uint8_t *ack
);
51 static const char *dap_reg_name(struct adiv5_dap
*dap
, uint8_t instr
, uint16_t reg_addr
)
53 char *reg_name
= "UNK";
55 if (instr
== JTAG_DP_DPACC
) {
61 reg_name
= "CTRL/STAT";
78 if (instr
== JTAG_DP_APACC
) {
79 if (reg_addr
== MEM_AP_REG_CSW(dap
))
81 else if (reg_addr
== MEM_AP_REG_TAR(dap
))
83 else if (reg_addr
== MEM_AP_REG_TAR64(dap
))
85 else if (reg_addr
== MEM_AP_REG_DRW(dap
))
87 else if (reg_addr
== MEM_AP_REG_BD0(dap
))
89 else if (reg_addr
== MEM_AP_REG_BD1(dap
))
91 else if (reg_addr
== MEM_AP_REG_BD2(dap
))
93 else if (reg_addr
== MEM_AP_REG_BD3(dap
))
95 else if (reg_addr
== MEM_AP_REG_CFG(dap
))
97 else if (reg_addr
== MEM_AP_REG_BASE(dap
))
99 else if (reg_addr
== MEM_AP_REG_BASE64(dap
))
101 else if (reg_addr
== AP_REG_IDR(dap
))
118 uint32_t memaccess_tck
;
121 struct scan_field fields
[2];
122 uint8_t out_addr_buf
;
123 uint8_t invalue_buf
[4];
124 uint8_t outvalue_buf
[4];
127 #define MAX_DAP_COMMAND_NUM 65536
129 struct dap_cmd_pool
{
134 static void log_dap_cmd(struct adiv5_dap
*dap
, const char *header
, struct dap_cmd
*el
)
139 case JTAG_ACK_WAIT
: /* ADIv5 and ADIv6 */
142 case JTAG_ACK_OK_FAULT
: /* ADIv5, same value as JTAG_ACK_FAULT */
143 /* case JTAG_ACK_FAULT: */ /* ADIv6 */
149 case JTAG_ACK_OK
: /* ADIv6 */
159 LOG_DEBUG("%s: %2s %6s %5s 0x%08x 0x%08x %2s", header
,
160 el
->instr
== JTAG_DP_APACC
? "AP" : "DP",
161 dap_reg_name(dap
, el
->instr
, el
->reg_addr
),
162 el
->rnw
== DPAP_READ
? "READ" : "WRITE",
163 buf_get_u32(el
->outvalue_buf
, 0, 32),
164 buf_get_u32(el
->invalue
, 0, 32),
169 static int jtag_limit_queue_size(struct adiv5_dap
*dap
)
171 if (dap
->cmd_pool_size
< MAX_DAP_COMMAND_NUM
)
177 static struct dap_cmd
*dap_cmd_new(struct adiv5_dap
*dap
, uint8_t instr
,
178 uint16_t reg_addr
, uint8_t rnw
,
179 uint8_t *outvalue
, uint8_t *invalue
,
180 uint32_t memaccess_tck
)
183 struct dap_cmd_pool
*pool
= NULL
;
185 if (list_empty(&dap
->cmd_pool
)) {
186 pool
= calloc(1, sizeof(struct dap_cmd_pool
));
190 pool
= list_first_entry(&dap
->cmd_pool
, struct dap_cmd_pool
, lh
);
194 INIT_LIST_HEAD(&pool
->lh
);
195 dap
->cmd_pool_size
++;
197 struct dap_cmd
*cmd
= &pool
->cmd
;
198 INIT_LIST_HEAD(&cmd
->lh
);
200 cmd
->reg_addr
= reg_addr
;
203 memcpy(cmd
->outvalue_buf
, outvalue
, 4);
204 cmd
->invalue
= (invalue
) ? invalue
: cmd
->invalue_buf
;
205 cmd
->memaccess_tck
= memaccess_tck
;
210 static void dap_cmd_release(struct adiv5_dap
*dap
, struct dap_cmd
*cmd
)
212 struct dap_cmd_pool
*pool
= container_of(cmd
, struct dap_cmd_pool
, cmd
);
213 if (dap
->cmd_pool_size
> MAX_DAP_COMMAND_NUM
)
216 list_add(&pool
->lh
, &dap
->cmd_pool
);
218 dap
->cmd_pool_size
--;
221 static void flush_journal(struct adiv5_dap
*dap
, struct list_head
*lh
)
223 struct dap_cmd
*el
, *tmp
;
225 list_for_each_entry_safe(el
, tmp
, lh
, lh
) {
227 dap_cmd_release(dap
, el
);
231 static void jtag_quit(struct adiv5_dap
*dap
)
233 struct dap_cmd_pool
*el
, *tmp
;
234 struct list_head
*lh
= &dap
->cmd_pool
;
236 list_for_each_entry_safe(el
, tmp
, lh
, lh
) {
242 /***************************************************************************
244 * DPACC and APACC scanchain access through JTAG-DP (or SWJ-DP)
246 ***************************************************************************/
248 static int adi_jtag_dp_scan_cmd(struct adiv5_dap
*dap
, struct dap_cmd
*cmd
, uint8_t *ack
)
250 struct jtag_tap
*tap
= dap
->tap
;
253 retval
= arm_jtag_set_instr(tap
, cmd
->instr
, NULL
, TAP_IDLE
);
254 if (retval
!= ERROR_OK
)
257 /* Scan out a read or write operation using some DP or AP register.
258 * For APACC access with any sticky error flag set, this is discarded.
260 cmd
->fields
[0].num_bits
= 3;
261 buf_set_u32(&cmd
->out_addr_buf
, 0, 3, ((cmd
->reg_addr
>> 1) & 0x6) | (cmd
->rnw
& 0x1));
262 cmd
->fields
[0].out_value
= &cmd
->out_addr_buf
;
263 cmd
->fields
[0].in_value
= (ack
) ? ack
: &cmd
->ack
;
265 /* NOTE: if we receive JTAG_ACK_WAIT, the previous operation did not
266 * complete; data we write is discarded, data we read is unpredictable.
267 * When overrun detect is active, STICKYORUN is set.
270 cmd
->fields
[1].num_bits
= 32;
271 cmd
->fields
[1].out_value
= cmd
->outvalue_buf
;
272 cmd
->fields
[1].in_value
= cmd
->invalue
;
274 jtag_add_dr_scan(tap
, 2, cmd
->fields
, TAP_IDLE
);
276 /* Add specified number of tck clocks after starting AP register
277 * access or memory bus access, giving the hardware time to complete
279 * They provide more time for the (MEM) AP to complete the read ...
280 * See "Minimum Response Time" for JTAG-DP, in the ADIv5/ADIv6 spec.
282 if (cmd
->instr
== JTAG_DP_APACC
&& cmd
->memaccess_tck
!= 0)
283 jtag_add_runtest(cmd
->memaccess_tck
, TAP_IDLE
);
288 static int adi_jtag_dp_scan_cmd_sync(struct adiv5_dap
*dap
, struct dap_cmd
*cmd
, uint8_t *ack
)
292 retval
= adi_jtag_dp_scan_cmd(dap
, cmd
, ack
);
293 if (retval
!= ERROR_OK
)
296 return jtag_execute_queue();
300 * Scan DPACC or APACC using target ordered uint8_t buffers. No endianness
301 * conversions are performed. See section 4.4.3 of the ADIv5/ADIv6 spec, which
302 * discusses operations which access these registers.
304 * Note that only one scan is performed. If rnw is set, a separate scan
305 * will be needed to collect the data which was read; the "invalue" collects
306 * the posted result of a preceding operation, not the current one.
309 * @param instr JTAG_DP_APACC (AP access) or JTAG_DP_DPACC (DP access)
310 * @param reg_addr two significant bits; A[3:2]; for APACC access, the
311 * SELECT register has more addressing bits.
312 * @param rnw false iff outvalue will be written to the DP or AP
313 * @param outvalue points to a 32-bit (little-endian) integer
314 * @param invalue NULL, or points to a 32-bit (little-endian) integer
315 * @param ack points to where the three bit JTAG_ACK_* code will be stored
316 * @param memaccess_tck number of idle cycles to add after AP access
319 static int adi_jtag_dp_scan(struct adiv5_dap
*dap
,
320 uint8_t instr
, uint16_t reg_addr
, uint8_t rnw
,
321 uint8_t *outvalue
, uint8_t *invalue
,
322 uint32_t memaccess_tck
, uint8_t *ack
)
327 cmd
= dap_cmd_new(dap
, instr
, reg_addr
, rnw
, outvalue
, invalue
, memaccess_tck
);
329 cmd
->dp_select
= dap
->select
;
331 return ERROR_JTAG_DEVICE_ERROR
;
333 retval
= adi_jtag_dp_scan_cmd(dap
, cmd
, ack
);
334 if (retval
== ERROR_OK
)
335 list_add_tail(&cmd
->lh
, &dap
->cmd_journal
);
341 * Scan DPACC or APACC out and in from host ordered uint32_t buffers.
342 * This is exactly like adi_jtag_dp_scan(), except that endianness
343 * conversions are performed (so the types of invalue and outvalue
344 * must be different).
346 static int adi_jtag_dp_scan_u32(struct adiv5_dap
*dap
,
347 uint8_t instr
, uint16_t reg_addr
, uint8_t rnw
,
348 uint32_t outvalue
, uint32_t *invalue
,
349 uint32_t memaccess_tck
, uint8_t *ack
)
351 uint8_t out_value_buf
[4];
353 uint64_t sel
= (reg_addr
>> 4) & DP_SELECT_DPBANK
;
355 /* No need to change SELECT or RDBUFF as they are not banked */
356 if (instr
== JTAG_DP_DPACC
&& reg_addr
!= DP_SELECT
&& reg_addr
!= DP_RDBUFF
357 && (!dap
->select_valid
|| sel
!= (dap
->select
& DP_SELECT_DPBANK
))) {
358 /* Use the AP part of dap->select regardless of dap->select_valid:
359 * if !dap->select_valid
360 * dap->select contains a speculative value likely going to be used
361 * in the following swd_queue_ap_bankselect() */
362 sel
|= dap
->select
& SELECT_AP_MASK
;
364 LOG_DEBUG_IO("DP BANK SELECT: %" PRIx32
, (uint32_t)sel
);
366 buf_set_u32(out_value_buf
, 0, 32, (uint32_t)sel
);
368 retval
= adi_jtag_dp_scan(dap
, JTAG_DP_DPACC
,
369 DP_SELECT
, DPAP_WRITE
, out_value_buf
, NULL
, 0, NULL
);
370 if (retval
!= ERROR_OK
)
374 dap
->select_valid
= true;
376 buf_set_u32(out_value_buf
, 0, 32, outvalue
);
378 retval
= adi_jtag_dp_scan(dap
, instr
, reg_addr
, rnw
,
379 out_value_buf
, (uint8_t *)invalue
, memaccess_tck
, ack
);
380 if (retval
!= ERROR_OK
)
384 jtag_add_callback(arm_le_to_h_u32
,
385 (jtag_callback_data_t
) invalue
);
390 static int adi_jtag_finish_read(struct adiv5_dap
*dap
)
392 int retval
= ERROR_OK
;
394 if (dap
->last_read
) {
395 retval
= adi_jtag_dp_scan_u32(dap
, JTAG_DP_DPACC
,
396 DP_RDBUFF
, DPAP_READ
, 0, dap
->last_read
, 0, NULL
);
397 dap
->last_read
= NULL
;
403 static int adi_jtag_scan_inout_check_u32(struct adiv5_dap
*dap
,
404 uint8_t instr
, uint16_t reg_addr
, uint8_t rnw
,
405 uint32_t outvalue
, uint32_t *invalue
, uint32_t memaccess_tck
)
409 /* Issue the read or write */
410 retval
= adi_jtag_dp_scan_u32(dap
, instr
, reg_addr
,
411 rnw
, outvalue
, NULL
, memaccess_tck
, NULL
);
412 if (retval
!= ERROR_OK
)
415 /* For reads, collect posted value; RDBUFF has no other effect.
416 * Assumes read gets acked with OK/FAULT, and CTRL_STAT says "OK".
418 if ((rnw
== DPAP_READ
) && (invalue
)) {
419 retval
= adi_jtag_dp_scan_u32(dap
, JTAG_DP_DPACC
,
420 DP_RDBUFF
, DPAP_READ
, 0, invalue
, 0, NULL
);
421 if (retval
!= ERROR_OK
)
425 return jtag_execute_queue();
428 static int jtagdp_overrun_check(struct adiv5_dap
*dap
)
431 struct dap_cmd
*el
, *tmp
, *prev
= NULL
;
434 LIST_HEAD(replay_list
);
436 /* make sure all queued transactions are complete */
437 retval
= jtag_execute_queue();
438 if (retval
!= ERROR_OK
)
441 /* skip all completed transactions up to the first WAIT */
442 list_for_each_entry(el
, &dap
->cmd_journal
, lh
) {
444 * JTAG_ACK_OK_FAULT (ADIv5) and JTAG_ACK_FAULT (ADIv6) are equal so
445 * the following statement is checking to see if an acknowledgment of
446 * OK or FAULT is generated for ADIv5 or ADIv6
448 if (el
->ack
== JTAG_ACK_OK_FAULT
|| (is_adiv6(dap
) && el
->ack
== JTAG_ACK_OK
)) {
449 log_dap_cmd(dap
, "LOG", el
);
450 } else if (el
->ack
== JTAG_ACK_WAIT
) {
454 LOG_ERROR("Invalid ACK (%1x) in DAP response", el
->ack
);
455 log_dap_cmd(dap
, "ERR", el
);
456 retval
= ERROR_JTAG_DEVICE_ERROR
;
462 * If we found a stalled transaction and a previous transaction
463 * exists, check if it's a READ access.
465 if (found_wait
&& el
!= list_first_entry(&dap
->cmd_journal
, struct dap_cmd
, lh
)) {
466 prev
= list_entry(el
->lh
.prev
, struct dap_cmd
, lh
);
467 if (prev
->rnw
== DPAP_READ
) {
468 log_dap_cmd(dap
, "PND", prev
);
469 /* search for the next OK transaction, it contains
470 * the result of the previous READ */
472 list_for_each_entry_from(tmp
, &dap
->cmd_journal
, lh
) {
473 /* The following check covers OK and FAULT ACKs for both ADIv5 and ADIv6 */
474 if (tmp
->ack
== JTAG_ACK_OK_FAULT
|| (is_adiv6(dap
) && tmp
->ack
== JTAG_ACK_OK
)) {
475 /* recover the read value */
476 log_dap_cmd(dap
, "FND", tmp
);
477 if (el
->invalue
!= el
->invalue_buf
) {
478 uint32_t invalue
= le_to_h_u32(tmp
->invalue
);
479 memcpy(el
->invalue
, &invalue
, sizeof(uint32_t));
487 log_dap_cmd(dap
, "LST", el
);
490 * At this point we're sure that no previous
491 * transaction completed and the DAP/AP is still
492 * in busy state. We know that the next "OK" scan
493 * will return the READ result we need to recover.
494 * To complete the READ, we just keep polling RDBUFF
495 * until the WAIT condition clears
497 tmp
= dap_cmd_new(dap
, JTAG_DP_DPACC
,
498 DP_RDBUFF
, DPAP_READ
, NULL
, NULL
, 0);
500 retval
= ERROR_JTAG_DEVICE_ERROR
;
503 /* synchronously retry the command until it succeeds */
504 time_now
= timeval_ms();
506 retval
= adi_jtag_dp_scan_cmd_sync(dap
, tmp
, NULL
);
507 if (retval
!= ERROR_OK
)
509 /* The following check covers OK and FAULT ACKs for both ADIv5 and ADIv6 */
510 if (tmp
->ack
== JTAG_ACK_OK_FAULT
|| (is_adiv6(dap
) && tmp
->ack
== JTAG_ACK_OK
)) {
511 log_dap_cmd(dap
, "FND", tmp
);
512 if (el
->invalue
!= el
->invalue_buf
) {
513 uint32_t invalue
= le_to_h_u32(tmp
->invalue
);
514 memcpy(el
->invalue
, &invalue
, sizeof(uint32_t));
518 if (tmp
->ack
!= JTAG_ACK_WAIT
) {
519 LOG_ERROR("Invalid ACK (%1x) in DAP response", tmp
->ack
);
520 log_dap_cmd(dap
, "ERR", tmp
);
521 retval
= ERROR_JTAG_DEVICE_ERROR
;
525 } while (timeval_ms() - time_now
< 1000);
527 if (retval
== ERROR_OK
) {
528 /* timeout happened */
529 if (tmp
->ack
== JTAG_ACK_WAIT
) {
530 LOG_ERROR("Timeout during WAIT recovery");
531 dap
->select_valid
= false;
532 dap
->select1_valid
= false;
533 /* Keep dap->select unchanged, the same AP and AP bank
534 * is likely going to be used further */
535 jtag_ap_q_abort(dap
, NULL
);
536 /* clear the sticky overrun condition */
537 adi_jtag_scan_inout_check_u32(dap
, JTAG_DP_DPACC
,
538 DP_CTRL_STAT
, DPAP_WRITE
,
539 dap
->dp_ctrl_stat
| SSTICKYORUN
, NULL
, 0);
540 retval
= ERROR_JTAG_DEVICE_ERROR
;
544 /* we're done with this command, release it */
545 dap_cmd_release(dap
, tmp
);
547 if (retval
!= ERROR_OK
)
551 /* make el->invalue point to the default invalue
552 * so that we can safely retry it without clobbering
553 * the result we just recovered */
554 el
->invalue
= el
->invalue_buf
;
558 /* move all remaining transactions over to the replay list */
559 list_for_each_entry_safe_from(el
, tmp
, &dap
->cmd_journal
, lh
) {
560 log_dap_cmd(dap
, "REP", el
);
561 list_move_tail(&el
->lh
, &replay_list
);
564 /* we're done with the journal, flush it */
565 flush_journal(dap
, &dap
->cmd_journal
);
567 /* check for overrun condition in the last batch of transactions */
569 LOG_INFO("DAP transaction stalled (WAIT) - slowing down and resending");
570 /* clear the sticky overrun condition */
571 retval
= adi_jtag_scan_inout_check_u32(dap
, JTAG_DP_DPACC
,
572 DP_CTRL_STAT
, DPAP_WRITE
,
573 dap
->dp_ctrl_stat
| SSTICKYORUN
, NULL
, 0);
574 if (retval
!= ERROR_OK
)
577 /* restore SELECT register first */
578 if (!list_empty(&replay_list
)) {
579 el
= list_first_entry(&replay_list
, struct dap_cmd
, lh
);
581 uint8_t out_value_buf
[4];
582 buf_set_u32(out_value_buf
, 0, 32, (uint32_t)(el
->dp_select
));
584 tmp
= dap_cmd_new(dap
, JTAG_DP_DPACC
,
585 DP_SELECT
, DPAP_WRITE
, out_value_buf
, NULL
, 0);
587 retval
= ERROR_JTAG_DEVICE_ERROR
;
590 list_add(&tmp
->lh
, &replay_list
);
592 /* TODO: ADIv6 DP SELECT1 handling */
594 dap
->select_valid
= false;
597 list_for_each_entry_safe(el
, tmp
, &replay_list
, lh
) {
598 time_now
= timeval_ms();
600 retval
= adi_jtag_dp_scan_cmd_sync(dap
, el
, NULL
);
601 if (retval
!= ERROR_OK
)
603 log_dap_cmd(dap
, "REC", el
);
604 if (el
->ack
== JTAG_ACK_OK_FAULT
|| (is_adiv6(dap
) && el
->ack
== JTAG_ACK_OK
)) {
605 if (el
->invalue
!= el
->invalue_buf
) {
606 uint32_t invalue
= le_to_h_u32(el
->invalue
);
607 memcpy(el
->invalue
, &invalue
, sizeof(uint32_t));
611 if (el
->ack
!= JTAG_ACK_WAIT
) {
612 LOG_ERROR("Invalid ACK (%1x) in DAP response", el
->ack
);
613 log_dap_cmd(dap
, "ERR", el
);
614 retval
= ERROR_JTAG_DEVICE_ERROR
;
617 LOG_DEBUG("DAP transaction stalled during replay (WAIT) - resending");
618 /* clear the sticky overrun condition */
619 retval
= adi_jtag_scan_inout_check_u32(dap
, JTAG_DP_DPACC
,
620 DP_CTRL_STAT
, DPAP_WRITE
,
621 dap
->dp_ctrl_stat
| SSTICKYORUN
, NULL
, 0);
622 if (retval
!= ERROR_OK
)
624 } while (timeval_ms() - time_now
< 1000);
626 if (retval
== ERROR_OK
) {
627 if (el
->ack
== JTAG_ACK_WAIT
) {
628 LOG_ERROR("Timeout during WAIT recovery");
629 dap
->select_valid
= false;
630 dap
->select1_valid
= false;
631 /* Keep dap->select unchanged, the same AP and AP bank
632 * is likely going to be used further */
633 jtag_ap_q_abort(dap
, NULL
);
634 /* clear the sticky overrun condition */
635 adi_jtag_scan_inout_check_u32(dap
, JTAG_DP_DPACC
,
636 DP_CTRL_STAT
, DPAP_WRITE
,
637 dap
->dp_ctrl_stat
| SSTICKYORUN
, NULL
, 0);
638 retval
= ERROR_JTAG_DEVICE_ERROR
;
647 flush_journal(dap
, &replay_list
);
648 flush_journal(dap
, &dap
->cmd_journal
);
652 static int jtagdp_transaction_endcheck(struct adiv5_dap
*dap
)
655 uint32_t ctrlstat
, pwrmask
;
657 /* too expensive to call keep_alive() here */
659 /* Post CTRL/STAT read; discard any previous posted read value
660 * but collect its ACK status.
662 retval
= adi_jtag_scan_inout_check_u32(dap
, JTAG_DP_DPACC
,
663 DP_CTRL_STAT
, DPAP_READ
, 0, &ctrlstat
, 0);
664 if (retval
!= ERROR_OK
)
667 /* REVISIT also STICKYCMP, for pushed comparisons (nyet used) */
669 /* Check for STICKYERR */
670 if (ctrlstat
& SSTICKYERR
) {
671 LOG_DEBUG("jtag-dp: CTRL/STAT 0x%" PRIx32
, ctrlstat
);
672 /* Check power to debug regions */
673 pwrmask
= CDBGPWRUPREQ
| CDBGPWRUPACK
| CSYSPWRUPREQ
;
674 if (!dap
->ignore_syspwrupack
)
675 pwrmask
|= CSYSPWRUPACK
;
676 if ((ctrlstat
& pwrmask
) != pwrmask
) {
677 LOG_ERROR("Debug regions are unpowered, an unexpected reset might have happened");
678 dap
->do_reconnect
= true;
681 if (ctrlstat
& SSTICKYERR
)
682 LOG_ERROR("JTAG-DP STICKY ERROR");
683 if (ctrlstat
& SSTICKYORUN
)
684 LOG_DEBUG("JTAG-DP STICKY OVERRUN");
686 /* Clear Sticky Error and Sticky Overrun Bits */
687 retval
= adi_jtag_scan_inout_check_u32(dap
, JTAG_DP_DPACC
,
688 DP_CTRL_STAT
, DPAP_WRITE
,
689 dap
->dp_ctrl_stat
| SSTICKYERR
| SSTICKYORUN
, NULL
, 0);
690 if (retval
!= ERROR_OK
)
693 retval
= ERROR_JTAG_DEVICE_ERROR
;
697 flush_journal(dap
, &dap
->cmd_journal
);
701 /*--------------------------------------------------------------------------*/
703 static int jtag_connect(struct adiv5_dap
*dap
)
705 dap
->do_reconnect
= false;
706 return dap_dp_init(dap
);
709 static int jtag_check_reconnect(struct adiv5_dap
*dap
)
711 if (dap
->do_reconnect
)
712 return jtag_connect(dap
);
717 static int jtag_send_sequence(struct adiv5_dap
*dap
, enum swd_special_seq seq
)
723 retval
= jtag_add_tms_seq(swd_seq_jtag_to_swd_len
,
724 swd_seq_jtag_to_swd
, TAP_INVALID
);
727 retval
= jtag_add_tms_seq(swd_seq_swd_to_jtag_len
,
728 swd_seq_swd_to_jtag
, TAP_RESET
);
731 LOG_ERROR("Sequence %d not supported", seq
);
734 if (retval
== ERROR_OK
)
735 retval
= jtag_execute_queue();
739 static int jtag_dp_q_read(struct adiv5_dap
*dap
, unsigned reg
,
742 int retval
= jtag_limit_queue_size(dap
);
743 if (retval
!= ERROR_OK
)
746 retval
= adi_jtag_dp_scan_u32(dap
, JTAG_DP_DPACC
, reg
,
747 DPAP_READ
, 0, dap
->last_read
, 0, NULL
);
748 dap
->last_read
= data
;
752 static int jtag_dp_q_write(struct adiv5_dap
*dap
, unsigned reg
,
755 int retval
= jtag_limit_queue_size(dap
);
756 if (retval
!= ERROR_OK
)
759 retval
= adi_jtag_dp_scan_u32(dap
, JTAG_DP_DPACC
,
760 reg
, DPAP_WRITE
, data
, dap
->last_read
, 0, NULL
);
761 dap
->last_read
= NULL
;
765 /** Select the AP register bank */
766 static int jtag_ap_q_bankselect(struct adiv5_ap
*ap
, unsigned reg
)
769 struct adiv5_dap
*dap
= ap
->dap
;
773 sel
= ap
->ap_num
| (reg
& 0x00000FF0);
775 sel
= (ap
->ap_num
<< 24) | (reg
& ADIV5_DP_SELECT_APBANK
);
777 uint64_t sel_diff
= (sel
^ dap
->select
) & SELECT_AP_MASK
;
779 bool set_select
= !dap
->select_valid
|| (sel_diff
& 0xffffffffull
);
780 bool set_select1
= is_adiv6(dap
) && dap
->asize
> 32
781 && (!dap
->select1_valid
782 || sel_diff
& (0xffffffffull
<< 32));
784 if (set_select
&& set_select1
) {
785 /* Prepare DP bank for DP_SELECT1 now to save one write */
786 sel
|= (DP_SELECT1
>> 4) & DP_SELECT_DPBANK
;
788 /* Use the DP part of dap->select regardless of dap->select_valid:
789 * if !dap->select_valid
790 * dap->select contains a speculative value likely going to be used
791 * in the following swd_queue_dp_bankselect().
792 * Moreover dap->select_valid should never be false here as a DP bank
793 * is always selected before selecting an AP bank */
794 sel
|= dap
->select
& DP_SELECT_DPBANK
;
798 LOG_DEBUG_IO("AP BANK SELECT: %" PRIx32
, (uint32_t)sel
);
800 retval
= jtag_dp_q_write(dap
, DP_SELECT
, (uint32_t)sel
);
801 if (retval
!= ERROR_OK
) {
802 dap
->select_valid
= false;
808 LOG_DEBUG_IO("AP BANK SELECT1: %" PRIx32
, (uint32_t)(sel
>> 32));
810 retval
= jtag_dp_q_write(dap
, DP_SELECT1
, (uint32_t)(sel
>> 32));
811 if (retval
!= ERROR_OK
) {
812 dap
->select1_valid
= false;
821 static int jtag_ap_q_read(struct adiv5_ap
*ap
, unsigned reg
,
824 int retval
= jtag_limit_queue_size(ap
->dap
);
825 if (retval
!= ERROR_OK
)
828 retval
= jtag_check_reconnect(ap
->dap
);
829 if (retval
!= ERROR_OK
)
832 retval
= jtag_ap_q_bankselect(ap
, reg
);
833 if (retval
!= ERROR_OK
)
836 retval
= adi_jtag_dp_scan_u32(ap
->dap
, JTAG_DP_APACC
, reg
,
837 DPAP_READ
, 0, ap
->dap
->last_read
, ap
->memaccess_tck
, NULL
);
838 ap
->dap
->last_read
= data
;
843 static int jtag_ap_q_write(struct adiv5_ap
*ap
, unsigned reg
,
846 int retval
= jtag_limit_queue_size(ap
->dap
);
847 if (retval
!= ERROR_OK
)
850 retval
= jtag_check_reconnect(ap
->dap
);
851 if (retval
!= ERROR_OK
)
854 retval
= jtag_ap_q_bankselect(ap
, reg
);
855 if (retval
!= ERROR_OK
)
858 retval
= adi_jtag_dp_scan_u32(ap
->dap
, JTAG_DP_APACC
, reg
,
859 DPAP_WRITE
, data
, ap
->dap
->last_read
, ap
->memaccess_tck
, NULL
);
860 ap
->dap
->last_read
= NULL
;
864 static int jtag_ap_q_abort(struct adiv5_dap
*dap
, uint8_t *ack
)
866 /* for JTAG, this is the only valid ABORT register operation */
867 int retval
= adi_jtag_dp_scan_u32(dap
, JTAG_DP_ABORT
,
868 0, DPAP_WRITE
, 1, NULL
, 0, NULL
);
869 if (retval
!= ERROR_OK
)
872 return jtag_execute_queue();
875 static int jtag_dp_run(struct adiv5_dap
*dap
)
878 int retval2
= ERROR_OK
;
880 retval
= adi_jtag_finish_read(dap
);
881 if (retval
!= ERROR_OK
)
883 retval2
= jtagdp_overrun_check(dap
);
884 retval
= jtagdp_transaction_endcheck(dap
);
887 return (retval2
!= ERROR_OK
) ? retval2
: retval
;
890 static int jtag_dp_sync(struct adiv5_dap
*dap
)
892 return jtagdp_overrun_check(dap
);
895 /* FIXME don't export ... just initialize as
898 const struct dap_ops jtag_dp_ops
= {
899 .connect
= jtag_connect
,
900 .send_sequence
= jtag_send_sequence
,
901 .queue_dp_read
= jtag_dp_q_read
,
902 .queue_dp_write
= jtag_dp_q_write
,
903 .queue_ap_read
= jtag_ap_q_read
,
904 .queue_ap_write
= jtag_ap_q_write
,
905 .queue_ap_abort
= jtag_ap_q_abort
,
907 .sync
= jtag_dp_sync
,