c7dc4f7c988c4059bf9c81845b1ac559f397c8df
[openocd.git] / src / target / adi_v5_jtag.c
1 /***************************************************************************
2 * Copyright (C) 2006 by Magnus Lundin
3 * lundin@mlu.mine.nu
4 *
5 * Copyright (C) 2008 by Spencer Oliver
6 * spen@spen-soft.co.uk
7 *
8 * Copyright (C) 2009 by Oyvind Harboe
9 * oyvind.harboe@zylin.com
10 *
11 * Copyright (C) 2009-2010 by David Brownell
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ***************************************************************************/
26
27 /**
28 * @file
29 * This file implements JTAG transport support for cores implementing
30 the ARM Debug Interface version 5 (ADIv5).
31 */
32
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 #include <helper/list.h>
41
42 /*#define DEBUG_WAIT*/
43
44 /* JTAG instructions/registers for JTAG-DP and SWJ-DP */
45 #define JTAG_DP_ABORT 0x8
46 #define JTAG_DP_DPACC 0xA
47 #define JTAG_DP_APACC 0xB
48 #define JTAG_DP_IDCODE 0xE
49
50 /* three-bit ACK values for DPACC and APACC reads */
51 #define JTAG_ACK_OK_FAULT 0x2
52 #define JTAG_ACK_WAIT 0x1
53
54 static int jtag_ap_q_abort(struct adiv5_dap *dap, uint8_t *ack);
55
56 #ifdef DEBUG_WAIT
57 static const char *dap_reg_name(int instr, int reg_addr)
58 {
59 char *reg_name = "UNK";
60
61 if (instr == JTAG_DP_DPACC) {
62 switch (reg_addr) {
63 case DP_ABORT:
64 reg_name = "ABORT";
65 break;
66 case DP_CTRL_STAT:
67 reg_name = "CTRL/STAT";
68 break;
69 case DP_SELECT:
70 reg_name = "SELECT";
71 break;
72 case DP_RDBUFF:
73 reg_name = "RDBUFF";
74 break;
75 case DP_WCR:
76 reg_name = "WCR";
77 break;
78 default:
79 reg_name = "UNK";
80 break;
81 }
82 }
83
84 if (instr == JTAG_DP_APACC) {
85 switch (reg_addr) {
86 case MEM_AP_REG_CSW:
87 reg_name = "CSW";
88 break;
89 case MEM_AP_REG_TAR:
90 reg_name = "TAR";
91 break;
92 case MEM_AP_REG_DRW:
93 reg_name = "DRW";
94 break;
95 case MEM_AP_REG_BD0:
96 reg_name = "BD0";
97 break;
98 case MEM_AP_REG_BD1:
99 reg_name = "BD1";
100 break;
101 case MEM_AP_REG_BD2:
102 reg_name = "BD2";
103 break;
104 case MEM_AP_REG_BD3:
105 reg_name = "BD3";
106 break;
107 case MEM_AP_REG_CFG:
108 reg_name = "CFG";
109 break;
110 case MEM_AP_REG_BASE:
111 reg_name = "BASE";
112 break;
113 case AP_REG_IDR:
114 reg_name = "IDR";
115 break;
116 default:
117 reg_name = "UNK";
118 break;
119 }
120 }
121
122 return reg_name;
123 }
124 #endif
125
126 struct dap_cmd {
127 struct list_head lh;
128 uint8_t instr;
129 uint8_t reg_addr;
130 uint8_t RnW;
131 uint8_t *invalue;
132 uint8_t ack;
133 uint32_t memaccess_tck;
134 uint32_t dp_select;
135
136 struct scan_field fields[2];
137 uint8_t out_addr_buf;
138 uint8_t invalue_buf[4];
139 uint8_t outvalue_buf[4];
140 };
141
142 static void log_dap_cmd(const char *header, struct dap_cmd *el)
143 {
144 #ifdef DEBUG_WAIT
145 LOG_DEBUG("%s: %2s %6s %5s 0x%08x 0x%08x %2s", header,
146 el->instr == JTAG_DP_APACC ? "AP" : "DP",
147 dap_reg_name(el->instr, el->reg_addr),
148 el->RnW == DPAP_READ ? "READ" : "WRITE",
149 buf_get_u32(el->outvalue_buf, 0, 32),
150 buf_get_u32(el->invalue, 0, 32),
151 el->ack == JTAG_ACK_OK_FAULT ? "OK" :
152 (el->ack == JTAG_ACK_WAIT ? "WAIT" : "INVAL"));
153 #endif
154 }
155
156 static struct dap_cmd *dap_cmd_new(uint8_t instr,
157 uint8_t reg_addr, uint8_t RnW,
158 uint8_t *outvalue, uint8_t *invalue,
159 uint32_t memaccess_tck)
160 {
161 struct dap_cmd *cmd;
162
163 cmd = (struct dap_cmd *)calloc(1, sizeof(struct dap_cmd));
164 if (cmd != NULL) {
165 INIT_LIST_HEAD(&cmd->lh);
166 cmd->instr = instr;
167 cmd->reg_addr = reg_addr;
168 cmd->RnW = RnW;
169 if (outvalue != NULL)
170 memcpy(cmd->outvalue_buf, outvalue, 4);
171 cmd->invalue = (invalue != NULL) ? invalue : cmd->invalue_buf;
172 cmd->memaccess_tck = memaccess_tck;
173 }
174
175 return cmd;
176 }
177
178 static void flush_journal(struct list_head *lh)
179 {
180 struct dap_cmd *el, *tmp;
181
182 list_for_each_entry_safe(el, tmp, lh, lh) {
183 list_del(&el->lh);
184 free(el);
185 }
186 }
187
188 /***************************************************************************
189 *
190 * DPACC and APACC scanchain access through JTAG-DP (or SWJ-DP)
191 *
192 ***************************************************************************/
193
194 static int adi_jtag_dp_scan_cmd(struct adiv5_dap *dap, struct dap_cmd *cmd, uint8_t *ack)
195 {
196 struct jtag_tap *tap = dap->tap;
197 int retval;
198
199 retval = arm_jtag_set_instr(tap, cmd->instr, NULL, TAP_IDLE);
200 if (retval != ERROR_OK)
201 return retval;
202
203 /* Scan out a read or write operation using some DP or AP register.
204 * For APACC access with any sticky error flag set, this is discarded.
205 */
206 cmd->fields[0].num_bits = 3;
207 buf_set_u32(&cmd->out_addr_buf, 0, 3, ((cmd->reg_addr >> 1) & 0x6) | (cmd->RnW & 0x1));
208 cmd->fields[0].out_value = &cmd->out_addr_buf;
209 cmd->fields[0].in_value = (ack != NULL) ? ack : &cmd->ack;
210
211 /* NOTE: if we receive JTAG_ACK_WAIT, the previous operation did not
212 * complete; data we write is discarded, data we read is unpredictable.
213 * When overrun detect is active, STICKYORUN is set.
214 */
215
216 cmd->fields[1].num_bits = 32;
217 cmd->fields[1].out_value = cmd->outvalue_buf;
218 cmd->fields[1].in_value = cmd->invalue;
219
220 jtag_add_dr_scan(tap, 2, cmd->fields, TAP_IDLE);
221
222 /* Add specified number of tck clocks after starting memory bus
223 * access, giving the hardware time to complete the access.
224 * They provide more time for the (MEM) AP to complete the read ...
225 * See "Minimum Response Time" for JTAG-DP, in the ADIv5 spec.
226 */
227 if (cmd->instr == JTAG_DP_APACC) {
228 if (((cmd->reg_addr == MEM_AP_REG_DRW)
229 || ((cmd->reg_addr & 0xF0) == MEM_AP_REG_BD0))
230 && (cmd->memaccess_tck != 0))
231 jtag_add_runtest(cmd->memaccess_tck, TAP_IDLE);
232 }
233
234 return ERROR_OK;
235 }
236
237 static int adi_jtag_dp_scan_cmd_sync(struct adiv5_dap *dap, struct dap_cmd *cmd, uint8_t *ack)
238 {
239 int retval;
240
241 retval = adi_jtag_dp_scan_cmd(dap, cmd, ack);
242 if (retval != ERROR_OK)
243 return retval;
244
245 return jtag_execute_queue();
246 }
247
248 /**
249 * Scan DPACC or APACC using target ordered uint8_t buffers. No endianness
250 * conversions are performed. See section 4.4.3 of the ADIv5 spec, which
251 * discusses operations which access these registers.
252 *
253 * Note that only one scan is performed. If RnW is set, a separate scan
254 * will be needed to collect the data which was read; the "invalue" collects
255 * the posted result of a preceding operation, not the current one.
256 *
257 * @param dap the DAP
258 * @param instr JTAG_DP_APACC (AP access) or JTAG_DP_DPACC (DP access)
259 * @param reg_addr two significant bits; A[3:2]; for APACC access, the
260 * SELECT register has more addressing bits.
261 * @param RnW false iff outvalue will be written to the DP or AP
262 * @param outvalue points to a 32-bit (little-endian) integer
263 * @param invalue NULL, or points to a 32-bit (little-endian) integer
264 * @param ack points to where the three bit JTAG_ACK_* code will be stored
265 * @param memaccess_tck number of idle cycles to add after AP access
266 */
267
268 static int adi_jtag_dp_scan(struct adiv5_dap *dap,
269 uint8_t instr, uint8_t reg_addr, uint8_t RnW,
270 uint8_t *outvalue, uint8_t *invalue,
271 uint32_t memaccess_tck, uint8_t *ack)
272 {
273 struct dap_cmd *cmd;
274 int retval;
275
276 cmd = dap_cmd_new(instr, reg_addr, RnW, outvalue, invalue, memaccess_tck);
277 if (cmd != NULL)
278 cmd->dp_select = dap->select;
279 else
280 return ERROR_JTAG_DEVICE_ERROR;
281
282 retval = adi_jtag_dp_scan_cmd(dap, cmd, ack);
283 if (retval == ERROR_OK)
284 list_add_tail(&cmd->lh, &dap->cmd_journal);
285
286 return retval;
287 }
288
289 /**
290 * Scan DPACC or APACC out and in from host ordered uint32_t buffers.
291 * This is exactly like adi_jtag_dp_scan(), except that endianness
292 * conversions are performed (so the types of invalue and outvalue
293 * must be different).
294 */
295 static int adi_jtag_dp_scan_u32(struct adiv5_dap *dap,
296 uint8_t instr, uint8_t reg_addr, uint8_t RnW,
297 uint32_t outvalue, uint32_t *invalue,
298 uint32_t memaccess_tck, uint8_t *ack)
299 {
300 uint8_t out_value_buf[4];
301 int retval;
302
303 buf_set_u32(out_value_buf, 0, 32, outvalue);
304
305 retval = adi_jtag_dp_scan(dap, instr, reg_addr, RnW,
306 out_value_buf, (uint8_t *)invalue, memaccess_tck, ack);
307 if (retval != ERROR_OK)
308 return retval;
309
310 if (invalue)
311 jtag_add_callback(arm_le_to_h_u32,
312 (jtag_callback_data_t) invalue);
313
314 return retval;
315 }
316
317 static int adi_jtag_finish_read(struct adiv5_dap *dap)
318 {
319 int retval = ERROR_OK;
320
321 if (dap->last_read != NULL) {
322 retval = adi_jtag_dp_scan_u32(dap, JTAG_DP_DPACC,
323 DP_RDBUFF, DPAP_READ, 0, dap->last_read, 0, NULL);
324 dap->last_read = NULL;
325 }
326
327 return retval;
328 }
329
330 static int adi_jtag_scan_inout_check_u32(struct adiv5_dap *dap,
331 uint8_t instr, uint8_t reg_addr, uint8_t RnW,
332 uint32_t outvalue, uint32_t *invalue, uint32_t memaccess_tck)
333 {
334 int retval;
335
336 /* Issue the read or write */
337 retval = adi_jtag_dp_scan_u32(dap, instr, reg_addr,
338 RnW, outvalue, NULL, memaccess_tck, NULL);
339 if (retval != ERROR_OK)
340 return retval;
341
342 /* For reads, collect posted value; RDBUFF has no other effect.
343 * Assumes read gets acked with OK/FAULT, and CTRL_STAT says "OK".
344 */
345 if ((RnW == DPAP_READ) && (invalue != NULL)) {
346 retval = adi_jtag_dp_scan_u32(dap, JTAG_DP_DPACC,
347 DP_RDBUFF, DPAP_READ, 0, invalue, 0, NULL);
348 if (retval != ERROR_OK)
349 return retval;
350 }
351
352 return jtag_execute_queue();
353 }
354
355 static int jtagdp_overrun_check(struct adiv5_dap *dap)
356 {
357 int retval;
358 struct dap_cmd *el, *tmp, *prev = NULL;
359 int found_wait = 0;
360 int64_t time_now;
361 LIST_HEAD(replay_list);
362
363 /* make sure all queued transactions are complete */
364 retval = jtag_execute_queue();
365 if (retval != ERROR_OK)
366 goto done;
367
368 /* skip all completed transactions up to the first WAIT */
369 list_for_each_entry(el, &dap->cmd_journal, lh) {
370 if (el->ack == JTAG_ACK_OK_FAULT) {
371 log_dap_cmd("LOG", el);
372 } else if (el->ack == JTAG_ACK_WAIT) {
373 found_wait = 1;
374 break;
375 } else {
376 LOG_ERROR("Invalid ACK (%1x) in DAP response", el->ack);
377 log_dap_cmd("ERR", el);
378 retval = ERROR_JTAG_DEVICE_ERROR;
379 goto done;
380 }
381 }
382
383 /*
384 * If we found a stalled transaction and a previous transaction
385 * exists, check if it's a READ access.
386 */
387 if (found_wait && el != list_first_entry(&dap->cmd_journal, struct dap_cmd, lh)) {
388 prev = list_entry(el->lh.prev, struct dap_cmd, lh);
389 if (prev->RnW == DPAP_READ) {
390 log_dap_cmd("PND", prev);
391 /* search for the next OK transaction, it contains
392 * the result of the previous READ */
393 tmp = el;
394 list_for_each_entry_from(tmp, &dap->cmd_journal, lh) {
395 if (tmp->ack == JTAG_ACK_OK_FAULT) {
396 /* recover the read value */
397 log_dap_cmd("FND", tmp);
398 if (el->invalue != el->invalue_buf) {
399 uint32_t invalue = le_to_h_u32(tmp->invalue);
400 memcpy(el->invalue, &invalue, sizeof(uint32_t));
401 }
402 prev = NULL;
403 break;
404 }
405 }
406
407 if (prev != NULL) {
408 log_dap_cmd("LST", el);
409
410 /*
411 * At this point we're sure that no previous
412 * transaction completed and the DAP/AP is still
413 * in busy state. We know that the next "OK" scan
414 * will return the READ result we need to recover.
415 * To complete the READ, we just keep polling RDBUFF
416 * until the WAIT condition clears
417 */
418 tmp = dap_cmd_new(JTAG_DP_DPACC,
419 DP_RDBUFF, DPAP_READ, NULL, NULL, 0);
420 if (tmp == NULL) {
421 retval = ERROR_JTAG_DEVICE_ERROR;
422 goto done;
423 }
424 /* synchronously retry the command until it succeeds */
425 time_now = timeval_ms();
426 do {
427 retval = adi_jtag_dp_scan_cmd_sync(dap, tmp, NULL);
428 if (retval != ERROR_OK)
429 break;
430 if (tmp->ack == JTAG_ACK_OK_FAULT) {
431 log_dap_cmd("FND", tmp);
432 if (el->invalue != el->invalue_buf) {
433 uint32_t invalue = le_to_h_u32(tmp->invalue);
434 memcpy(el->invalue, &invalue, sizeof(uint32_t));
435 }
436 break;
437 }
438 if (tmp->ack != JTAG_ACK_WAIT) {
439 LOG_ERROR("Invalid ACK (%1x) in DAP response", tmp->ack);
440 log_dap_cmd("ERR", tmp);
441 retval = ERROR_JTAG_DEVICE_ERROR;
442 break;
443 }
444
445 } while (timeval_ms() - time_now < 1000);
446
447 if (retval == ERROR_OK) {
448 /* timeout happened */
449 if (tmp->ack != JTAG_ACK_OK_FAULT) {
450 LOG_ERROR("Timeout during WAIT recovery");
451 dap->select = DP_SELECT_INVALID;
452 jtag_ap_q_abort(dap, NULL);
453 /* clear the sticky overrun condition */
454 adi_jtag_scan_inout_check_u32(dap, JTAG_DP_DPACC,
455 DP_CTRL_STAT, DPAP_WRITE,
456 dap->dp_ctrl_stat | SSTICKYORUN, NULL, 0);
457 retval = ERROR_JTAG_DEVICE_ERROR;
458 }
459 }
460
461 /* we're done with this command, release it */
462 free(tmp);
463
464 if (retval != ERROR_OK)
465 goto done;
466
467 }
468 /* make el->invalue point to the default invalue
469 * so that we can safely retry it without clobbering
470 * the result we just recovered */
471 el->invalue = el->invalue_buf;
472 }
473 }
474
475 /* move all remaining transactions over to the replay list */
476 list_for_each_entry_safe_from(el, tmp, &dap->cmd_journal, lh) {
477 log_dap_cmd("REP", el);
478 list_move_tail(&el->lh, &replay_list);
479 }
480
481 /* we're done with the journal, flush it */
482 flush_journal(&dap->cmd_journal);
483
484 /* check for overrun condition in the last batch of transactions */
485 if (found_wait) {
486 LOG_INFO("DAP transaction stalled (WAIT) - slowing down");
487 /* clear the sticky overrun condition */
488 retval = adi_jtag_scan_inout_check_u32(dap, JTAG_DP_DPACC,
489 DP_CTRL_STAT, DPAP_WRITE,
490 dap->dp_ctrl_stat | SSTICKYORUN, NULL, 0);
491 if (retval != ERROR_OK)
492 goto done;
493
494 /* restore SELECT register first */
495 if (!list_empty(&replay_list)) {
496 el = list_first_entry(&replay_list, struct dap_cmd, lh);
497 tmp = dap_cmd_new(JTAG_DP_DPACC,
498 DP_SELECT, DPAP_WRITE, (uint8_t *)&el->dp_select, NULL, 0);
499 if (tmp == NULL) {
500 retval = ERROR_JTAG_DEVICE_ERROR;
501 goto done;
502 }
503 list_add(&tmp->lh, &replay_list);
504
505 dap->select = DP_SELECT_INVALID;
506 }
507
508 list_for_each_entry_safe(el, tmp, &replay_list, lh) {
509 time_now = timeval_ms();
510 do {
511 retval = adi_jtag_dp_scan_cmd_sync(dap, el, NULL);
512 if (retval != ERROR_OK)
513 break;
514 log_dap_cmd("REC", el);
515 if (el->ack == JTAG_ACK_OK_FAULT) {
516 if (el->invalue != el->invalue_buf) {
517 uint32_t invalue = le_to_h_u32(el->invalue);
518 memcpy(el->invalue, &invalue, sizeof(uint32_t));
519 }
520 break;
521 }
522 if (el->ack != JTAG_ACK_WAIT) {
523 LOG_ERROR("Invalid ACK (%1x) in DAP response", el->ack);
524 log_dap_cmd("ERR", el);
525 retval = ERROR_JTAG_DEVICE_ERROR;
526 break;
527 }
528 } while (timeval_ms() - time_now < 1000);
529
530 if (retval == ERROR_OK) {
531 if (el->ack != JTAG_ACK_OK_FAULT) {
532 LOG_ERROR("Timeout during WAIT recovery");
533 dap->select = DP_SELECT_INVALID;
534 jtag_ap_q_abort(dap, NULL);
535 /* clear the sticky overrun condition */
536 adi_jtag_scan_inout_check_u32(dap, JTAG_DP_DPACC,
537 DP_CTRL_STAT, DPAP_WRITE,
538 dap->dp_ctrl_stat | SSTICKYORUN, NULL, 0);
539 retval = ERROR_JTAG_DEVICE_ERROR;
540 break;
541 }
542 } else
543 break;
544 }
545 }
546
547 done:
548 flush_journal(&replay_list);
549 flush_journal(&dap->cmd_journal);
550 return retval;
551 }
552
553 static int jtagdp_transaction_endcheck(struct adiv5_dap *dap)
554 {
555 int retval;
556 uint32_t ctrlstat;
557
558 /* too expensive to call keep_alive() here */
559
560 /* Post CTRL/STAT read; discard any previous posted read value
561 * but collect its ACK status.
562 */
563 retval = adi_jtag_scan_inout_check_u32(dap, JTAG_DP_DPACC,
564 DP_CTRL_STAT, DPAP_READ, 0, &ctrlstat, 0);
565 if (retval != ERROR_OK)
566 goto done;
567
568 /* REVISIT also STICKYCMP, for pushed comparisons (nyet used) */
569
570 /* Check for STICKYERR */
571 if (ctrlstat & SSTICKYERR) {
572 LOG_DEBUG("jtag-dp: CTRL/STAT 0x%" PRIx32, ctrlstat);
573 /* Check power to debug regions */
574 if ((ctrlstat & (CDBGPWRUPREQ | CDBGPWRUPACK | CSYSPWRUPREQ | CSYSPWRUPACK)) !=
575 (CDBGPWRUPREQ | CDBGPWRUPACK | CSYSPWRUPREQ | CSYSPWRUPACK)) {
576 LOG_ERROR("Debug regions are unpowered, an unexpected reset might have happened");
577 }
578
579 if (ctrlstat & SSTICKYERR)
580 LOG_ERROR("JTAG-DP STICKY ERROR");
581 if (ctrlstat & SSTICKYORUN)
582 LOG_DEBUG("JTAG-DP STICKY OVERRUN");
583
584 /* Clear Sticky Error Bits */
585 retval = adi_jtag_scan_inout_check_u32(dap, JTAG_DP_DPACC,
586 DP_CTRL_STAT, DPAP_WRITE,
587 dap->dp_ctrl_stat | SSTICKYERR, NULL, 0);
588 if (retval != ERROR_OK)
589 goto done;
590
591 retval = ERROR_JTAG_DEVICE_ERROR;
592 }
593
594 done:
595 flush_journal(&dap->cmd_journal);
596 return retval;
597 }
598
599 /*--------------------------------------------------------------------------*/
600
601 static int jtag_dp_q_read(struct adiv5_dap *dap, unsigned reg,
602 uint32_t *data)
603 {
604 int retval = adi_jtag_dp_scan_u32(dap, JTAG_DP_DPACC, reg,
605 DPAP_READ, 0, dap->last_read, 0, NULL);
606 dap->last_read = data;
607 return retval;
608 }
609
610 static int jtag_dp_q_write(struct adiv5_dap *dap, unsigned reg,
611 uint32_t data)
612 {
613 int retval = adi_jtag_dp_scan_u32(dap, JTAG_DP_DPACC,
614 reg, DPAP_WRITE, data, dap->last_read, 0, NULL);
615 dap->last_read = NULL;
616 return retval;
617 }
618
619 /** Select the AP register bank matching bits 7:4 of reg. */
620 static int jtag_ap_q_bankselect(struct adiv5_ap *ap, unsigned reg)
621 {
622 struct adiv5_dap *dap = ap->dap;
623 uint32_t sel = ((uint32_t)ap->ap_num << 24) | (reg & 0x000000F0);
624
625 if (sel == dap->select)
626 return ERROR_OK;
627
628 dap->select = sel;
629
630 return jtag_dp_q_write(dap, DP_SELECT, sel);
631 }
632
633 static int jtag_ap_q_read(struct adiv5_ap *ap, unsigned reg,
634 uint32_t *data)
635 {
636 int retval = jtag_ap_q_bankselect(ap, reg);
637 if (retval != ERROR_OK)
638 return retval;
639
640 retval = adi_jtag_dp_scan_u32(ap->dap, JTAG_DP_APACC, reg,
641 DPAP_READ, 0, ap->dap->last_read, ap->memaccess_tck, NULL);
642 ap->dap->last_read = data;
643
644 return retval;
645 }
646
647 static int jtag_ap_q_write(struct adiv5_ap *ap, unsigned reg,
648 uint32_t data)
649 {
650 int retval = jtag_ap_q_bankselect(ap, reg);
651 if (retval != ERROR_OK)
652 return retval;
653
654 retval = adi_jtag_dp_scan_u32(ap->dap, JTAG_DP_APACC, reg,
655 DPAP_WRITE, data, ap->dap->last_read, ap->memaccess_tck, NULL);
656 ap->dap->last_read = NULL;
657 return retval;
658 }
659
660 static int jtag_ap_q_abort(struct adiv5_dap *dap, uint8_t *ack)
661 {
662 /* for JTAG, this is the only valid ABORT register operation */
663 int retval = adi_jtag_dp_scan_u32(dap, JTAG_DP_ABORT,
664 0, DPAP_WRITE, 1, NULL, 0, NULL);
665 if (retval != ERROR_OK)
666 return retval;
667
668 return jtag_execute_queue();
669 }
670
671 static int jtag_dp_run(struct adiv5_dap *dap)
672 {
673 int retval;
674 int retval2 = ERROR_OK;
675
676 retval = adi_jtag_finish_read(dap);
677 if (retval != ERROR_OK)
678 goto done;
679 retval2 = jtagdp_overrun_check(dap);
680 retval = jtagdp_transaction_endcheck(dap);
681
682 done:
683 return (retval2 != ERROR_OK) ? retval2 : retval;
684 }
685
686 static int jtag_dp_sync(struct adiv5_dap *dap)
687 {
688 return jtagdp_overrun_check(dap);
689 }
690
691 /* FIXME don't export ... just initialize as
692 * part of DAP setup
693 */
694 const struct dap_ops jtag_dp_ops = {
695 .queue_dp_read = jtag_dp_q_read,
696 .queue_dp_write = jtag_dp_q_write,
697 .queue_ap_read = jtag_ap_q_read,
698 .queue_ap_write = jtag_ap_q_write,
699 .queue_ap_abort = jtag_ap_q_abort,
700 .run = jtag_dp_run,
701 .sync = jtag_dp_sync,
702 };
703
704
705 static const uint8_t swd2jtag_bitseq[] = {
706 /* More than 50 TCK/SWCLK cycles with TMS/SWDIO high,
707 * putting both JTAG and SWD logic into reset state.
708 */
709 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
710 /* Switching equence disables SWD and enables JTAG
711 * NOTE: bits in the DP's IDCODE can expose the need for
712 * the old/deprecated sequence (0xae 0xde).
713 */
714 0x3c, 0xe7,
715 /* At least 50 TCK/SWCLK cycles with TMS/SWDIO high,
716 * putting both JTAG and SWD logic into reset state.
717 * NOTE: some docs say "at least 5".
718 */
719 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
720 };
721
722 /** Put the debug link into JTAG mode, if the target supports it.
723 * The link's initial mode may be either SWD or JTAG.
724 *
725 * @param target Enters JTAG mode (if possible).
726 *
727 * Note that targets implemented with SW-DP do not support JTAG, and
728 * that some targets which could otherwise support it may have been
729 * configured to disable JTAG signaling
730 *
731 * @return ERROR_OK or else a fault code.
732 */
733 int dap_to_jtag(struct target *target)
734 {
735 int retval;
736
737 LOG_DEBUG("Enter JTAG mode");
738
739 /* REVISIT it's nasty to need to make calls to a "jtag"
740 * subsystem if the link isn't in JTAG mode...
741 */
742
743 retval = jtag_add_tms_seq(8 * sizeof(swd2jtag_bitseq),
744 swd2jtag_bitseq, TAP_RESET);
745 if (retval == ERROR_OK)
746 retval = jtag_execute_queue();
747
748 /* REVISIT set up the DAP's ops vector for JTAG mode. */
749
750 return retval;
751 }