e500416b6e10587ffcb6c0fa544a7a4e60a586b4
[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, write to the
25 * Free Software Foundation, Inc.,
26 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 ***************************************************************************/
28
29 /**
30 * @file
31 * This file implements JTAG transport support for cores implementing
32 the ARM Debug Interface version 5 (ADIv5).
33 */
34
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38
39 #include "arm.h"
40 #include "arm_adi_v5.h"
41 #include <helper/time_support.h>
42
43 /* JTAG instructions/registers for JTAG-DP and SWJ-DP */
44 #define JTAG_DP_ABORT 0x8
45 #define JTAG_DP_DPACC 0xA
46 #define JTAG_DP_APACC 0xB
47 #define JTAG_DP_IDCODE 0xE
48
49 /* three-bit ACK values for DPACC and APACC reads */
50 #define JTAG_ACK_OK_FAULT 0x2
51 #define JTAG_ACK_WAIT 0x1
52
53 /***************************************************************************
54 *
55 * DPACC and APACC scanchain access through JTAG-DP (or SWJ-DP)
56 *
57 ***************************************************************************/
58
59 /**
60 * Scan DPACC or APACC using target ordered uint8_t buffers. No endianness
61 * conversions are performed. See section 4.4.3 of the ADIv5 spec, which
62 * discusses operations which access these registers.
63 *
64 * Note that only one scan is performed. If RnW is set, a separate scan
65 * will be needed to collect the data which was read; the "invalue" collects
66 * the posted result of a preceding operation, not the current one.
67 *
68 * @param dap the DAP
69 * @param instr JTAG_DP_APACC (AP access) or JTAG_DP_DPACC (DP access)
70 * @param reg_addr two significant bits; A[3:2]; for APACC access, the
71 * SELECT register has more addressing bits.
72 * @param RnW false iff outvalue will be written to the DP or AP
73 * @param outvalue points to a 32-bit (little-endian) integer
74 * @param invalue NULL, or points to a 32-bit (little-endian) integer
75 * @param ack points to where the three bit JTAG_ACK_* code will be stored
76 */
77
78 static int adi_jtag_dp_scan(struct adiv5_dap *dap,
79 uint8_t instr, uint8_t reg_addr, uint8_t RnW,
80 uint8_t *outvalue, uint8_t *invalue, uint8_t *ack)
81 {
82 struct arm_jtag *jtag_info = dap->jtag_info;
83 struct scan_field fields[2];
84 uint8_t out_addr_buf;
85 int retval;
86
87 retval = arm_jtag_set_instr(jtag_info, instr, NULL, TAP_IDLE);
88 if (retval != ERROR_OK)
89 return retval;
90
91 /* Scan out a read or write operation using some DP or AP register.
92 * For APACC access with any sticky error flag set, this is discarded.
93 */
94 fields[0].num_bits = 3;
95 buf_set_u32(&out_addr_buf, 0, 3, ((reg_addr >> 1) & 0x6) | (RnW & 0x1));
96 fields[0].out_value = &out_addr_buf;
97 fields[0].in_value = ack;
98
99 /* NOTE: if we receive JTAG_ACK_WAIT, the previous operation did not
100 * complete; data we write is discarded, data we read is unpredictable.
101 * When overrun detect is active, STICKYORUN is set.
102 */
103
104 fields[1].num_bits = 32;
105 fields[1].out_value = outvalue;
106 fields[1].in_value = invalue;
107
108 jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
109
110 /* Add specified number of tck clocks after starting memory bus
111 * access, giving the hardware time to complete the access.
112 * They provide more time for the (MEM) AP to complete the read ...
113 * See "Minimum Response Time" for JTAG-DP, in the ADIv5 spec.
114 */
115 if ((instr == JTAG_DP_APACC)
116 && ((reg_addr == AP_REG_DRW)
117 || ((reg_addr & 0xF0) == AP_REG_BD0))
118 && (dap->memaccess_tck != 0))
119 jtag_add_runtest(dap->memaccess_tck,
120 TAP_IDLE);
121
122 return ERROR_OK;
123 }
124
125 /**
126 * Scan DPACC or APACC out and in from host ordered uint32_t buffers.
127 * This is exactly like adi_jtag_dp_scan(), except that endianness
128 * conversions are performed (so the types of invalue and outvalue
129 * must be different).
130 */
131 static int adi_jtag_dp_scan_u32(struct adiv5_dap *dap,
132 uint8_t instr, uint8_t reg_addr, uint8_t RnW,
133 uint32_t outvalue, uint32_t *invalue, uint8_t *ack)
134 {
135 uint8_t out_value_buf[4];
136 int retval;
137
138 buf_set_u32(out_value_buf, 0, 32, outvalue);
139
140 retval = adi_jtag_dp_scan(dap, instr, reg_addr, RnW,
141 out_value_buf, (uint8_t *)invalue, ack);
142 if (retval != ERROR_OK)
143 return retval;
144
145 if (invalue)
146 jtag_add_callback(arm_le_to_h_u32,
147 (jtag_callback_data_t) invalue);
148
149 return retval;
150 }
151
152 /**
153 * Utility to write AP registers.
154 */
155 static inline int adi_jtag_ap_write_check(struct adiv5_dap *dap,
156 uint8_t reg_addr, uint8_t *outvalue)
157 {
158 return adi_jtag_dp_scan(dap, JTAG_DP_APACC, reg_addr, DPAP_WRITE,
159 outvalue, NULL, NULL);
160 }
161
162 static int adi_jtag_scan_inout_check_u32(struct adiv5_dap *dap,
163 uint8_t instr, uint8_t reg_addr, uint8_t RnW,
164 uint32_t outvalue, uint32_t *invalue)
165 {
166 int retval;
167
168 /* Issue the read or write */
169 retval = adi_jtag_dp_scan_u32(dap, instr, reg_addr,
170 RnW, outvalue, NULL, NULL);
171 if (retval != ERROR_OK)
172 return retval;
173
174 /* For reads, collect posted value; RDBUFF has no other effect.
175 * Assumes read gets acked with OK/FAULT, and CTRL_STAT says "OK".
176 */
177 if ((RnW == DPAP_READ) && (invalue != NULL))
178 retval = adi_jtag_dp_scan_u32(dap, JTAG_DP_DPACC,
179 DP_RDBUFF, DPAP_READ, 0, invalue, &dap->ack);
180 return retval;
181 }
182
183 static int jtagdp_transaction_endcheck(struct adiv5_dap *dap)
184 {
185 int retval;
186 uint32_t ctrlstat;
187
188 /* too expensive to call keep_alive() here */
189
190 /* Here be dragons!
191 *
192 * It is easy to be in a JTAG clock range where the target
193 * is not operating in a stable fashion. This happens
194 * for a few reasons:
195 *
196 * - the user may construct a simple test case to try to see
197 * if a higher JTAG clock works to eke out more performance.
198 * This simple case may pass, but more complex situations can
199 * fail.
200 *
201 * - The mostly works JTAG clock rate and the complete failure
202 * JTAG clock rate may be as much as 2-4x apart. This seems
203 * to be especially true on RC oscillator driven parts.
204 *
205 * So: even if calling adi_jtag_scan_inout_check_u32() multiple
206 * times here seems to "make things better here", it is just
207 * hiding problems with too high a JTAG clock.
208 *
209 * Note that even if some parts have RCLK/RTCK, that doesn't
210 * mean that RCLK/RTCK is the *correct* rate to run the JTAG
211 * interface at, i.e. RCLK/RTCK rates can be "too high", especially
212 * before the RC oscillator phase is not yet complete.
213 */
214
215 /* Post CTRL/STAT read; discard any previous posted read value
216 * but collect its ACK status.
217 */
218 retval = adi_jtag_scan_inout_check_u32(dap, JTAG_DP_DPACC,
219 DP_CTRL_STAT, DPAP_READ, 0, &ctrlstat);
220 if (retval != ERROR_OK)
221 return retval;
222 retval = jtag_execute_queue();
223 if (retval != ERROR_OK)
224 return retval;
225
226 dap->ack = dap->ack & 0x7;
227
228 /* common code path avoids calling timeval_ms() */
229 if (dap->ack != JTAG_ACK_OK_FAULT) {
230 long long then = timeval_ms();
231
232 while (dap->ack != JTAG_ACK_OK_FAULT) {
233 if (dap->ack == JTAG_ACK_WAIT) {
234 if ((timeval_ms()-then) > 1000) {
235 /* NOTE: this would be a good spot
236 * to use JTAG_DP_ABORT.
237 */
238 LOG_WARNING("Timeout (1000ms) waiting "
239 "for ACK=OK/FAULT "
240 "in JTAG-DP transaction");
241 return ERROR_JTAG_DEVICE_ERROR;
242 }
243 } else {
244 LOG_WARNING("Invalid ACK %#x "
245 "in JTAG-DP transaction",
246 dap->ack);
247 return ERROR_JTAG_DEVICE_ERROR;
248 }
249
250 retval = adi_jtag_scan_inout_check_u32(dap, JTAG_DP_DPACC,
251 DP_CTRL_STAT, DPAP_READ, 0, &ctrlstat);
252 if (retval != ERROR_OK)
253 return retval;
254 retval = jtag_execute_queue();
255 if (retval != ERROR_OK)
256 return retval;
257 dap->ack = dap->ack & 0x7;
258 }
259 }
260
261 /* REVISIT also STICKYCMP, for pushed comparisons (nyet used) */
262
263 /* Check for STICKYERR and STICKYORUN */
264 if (ctrlstat & (SSTICKYORUN | SSTICKYERR)) {
265 LOG_DEBUG("jtag-dp: CTRL/STAT error, 0x%" PRIx32, ctrlstat);
266 /* Check power to debug regions */
267 if ((ctrlstat & 0xf0000000) != 0xf0000000) {
268 retval = ahbap_debugport_init(dap);
269 if (retval != ERROR_OK)
270 return retval;
271 } else {
272 uint32_t mem_ap_csw, mem_ap_tar;
273
274 /* Maybe print information about last intended
275 * MEM-AP access; but not if autoincrementing.
276 * *Real* CSW and TAR values are always shown.
277 */
278 if (dap->ap_tar_value != (uint32_t) -1)
279 LOG_DEBUG("MEM-AP Cached values: "
280 "ap_bank 0x%" PRIx32
281 ", ap_csw 0x%" PRIx32
282 ", ap_tar 0x%" PRIx32,
283 dap->ap_bank_value,
284 dap->ap_csw_value,
285 dap->ap_tar_value);
286
287 if (ctrlstat & SSTICKYORUN)
288 LOG_ERROR("JTAG-DP OVERRUN - check clock, "
289 "memaccess, or reduce jtag speed");
290
291 if (ctrlstat & SSTICKYERR)
292 LOG_ERROR("JTAG-DP STICKY ERROR");
293
294 /* Clear Sticky Error Bits */
295 retval = adi_jtag_scan_inout_check_u32(dap, JTAG_DP_DPACC,
296 DP_CTRL_STAT, DPAP_WRITE,
297 dap->dp_ctrl_stat | SSTICKYORUN
298 | SSTICKYERR, NULL);
299 if (retval != ERROR_OK)
300 return retval;
301 retval = adi_jtag_scan_inout_check_u32(dap, JTAG_DP_DPACC,
302 DP_CTRL_STAT, DPAP_READ, 0, &ctrlstat);
303 if (retval != ERROR_OK)
304 return retval;
305 retval = jtag_execute_queue();
306 if (retval != ERROR_OK)
307 return retval;
308
309 LOG_DEBUG("jtag-dp: CTRL/STAT 0x%" PRIx32, ctrlstat);
310
311 retval = dap_queue_ap_read(dap,
312 AP_REG_CSW, &mem_ap_csw);
313 if (retval != ERROR_OK)
314 return retval;
315
316 retval = dap_queue_ap_read(dap,
317 AP_REG_TAR, &mem_ap_tar);
318 if (retval != ERROR_OK)
319 return retval;
320
321 retval = jtag_execute_queue();
322 if (retval != ERROR_OK)
323 return retval;
324 LOG_ERROR("MEM_AP_CSW 0x%" PRIx32 ", MEM_AP_TAR 0x%"
325 PRIx32, mem_ap_csw, mem_ap_tar);
326
327 }
328 retval = jtag_execute_queue();
329 if (retval != ERROR_OK)
330 return retval;
331 return ERROR_JTAG_DEVICE_ERROR;
332 }
333
334 return ERROR_OK;
335 }
336
337 /*--------------------------------------------------------------------------*/
338
339 static int jtag_idcode_q_read(struct adiv5_dap *dap,
340 uint8_t *ack, uint32_t *data)
341 {
342 struct arm_jtag *jtag_info = dap->jtag_info;
343 int retval;
344 struct scan_field fields[1];
345
346 /* This is a standard JTAG operation -- no DAP tweakage */
347 retval = arm_jtag_set_instr(jtag_info, JTAG_DP_IDCODE, NULL, TAP_IDLE);
348 if (retval != ERROR_OK)
349 return retval;
350
351 fields[0].num_bits = 32;
352 fields[0].out_value = NULL;
353 fields[0].in_value = (void *) data;
354
355 jtag_add_dr_scan(jtag_info->tap, 1, fields, TAP_IDLE);
356
357 jtag_add_callback(arm_le_to_h_u32,
358 (jtag_callback_data_t) data);
359
360 return ERROR_OK;
361 }
362
363 static int jtag_dp_q_read(struct adiv5_dap *dap, unsigned reg,
364 uint32_t *data)
365 {
366 return adi_jtag_scan_inout_check_u32(dap, JTAG_DP_DPACC,
367 reg, DPAP_READ, 0, data);
368 }
369
370 static int jtag_dp_q_write(struct adiv5_dap *dap, unsigned reg,
371 uint32_t data)
372 {
373 return adi_jtag_scan_inout_check_u32(dap, JTAG_DP_DPACC,
374 reg, DPAP_WRITE, data, NULL);
375 }
376
377 /** Select the AP register bank matching bits 7:4 of reg. */
378 static int jtag_ap_q_bankselect(struct adiv5_dap *dap, unsigned reg)
379 {
380 uint32_t select_ap_bank = reg & 0x000000F0;
381
382 if (select_ap_bank == dap->ap_bank_value)
383 return ERROR_OK;
384 dap->ap_bank_value = select_ap_bank;
385
386 select_ap_bank |= dap->ap_current;
387
388 return jtag_dp_q_write(dap, DP_SELECT, select_ap_bank);
389 }
390
391 static int jtag_ap_q_read(struct adiv5_dap *dap, unsigned reg,
392 uint32_t *data)
393 {
394 int retval = jtag_ap_q_bankselect(dap, reg);
395
396 if (retval != ERROR_OK)
397 return retval;
398
399 return adi_jtag_scan_inout_check_u32(dap, JTAG_DP_APACC, reg,
400 DPAP_READ, 0, data);
401 }
402
403 static int jtag_ap_q_write(struct adiv5_dap *dap, unsigned reg,
404 uint32_t data)
405 {
406 uint8_t out_value_buf[4];
407
408 int retval = jtag_ap_q_bankselect(dap, reg);
409 if (retval != ERROR_OK)
410 return retval;
411
412 buf_set_u32(out_value_buf, 0, 32, data);
413
414 return adi_jtag_ap_write_check(dap, reg, out_value_buf);
415 }
416
417 static int jtag_ap_q_read_block(struct adiv5_dap *dap, unsigned reg,
418 uint32_t blocksize, uint8_t *buffer)
419 {
420 uint32_t readcount;
421 int retval = ERROR_OK;
422
423 /* Scan out first read */
424 retval = adi_jtag_dp_scan(dap, JTAG_DP_APACC, reg,
425 DPAP_READ, 0, NULL, NULL);
426 if (retval != ERROR_OK)
427 return retval;
428
429 for (readcount = 0; readcount < blocksize - 1; readcount++) {
430 /* Scan out next read; scan in posted value for the
431 * previous one. Assumes read is acked "OK/FAULT",
432 * and CTRL_STAT says that meant "OK".
433 */
434 retval = adi_jtag_dp_scan(dap, JTAG_DP_APACC, reg,
435 DPAP_READ, 0, buffer + 4 * readcount,
436 &dap->ack);
437 if (retval != ERROR_OK)
438 return retval;
439 }
440
441 /* Scan in last posted value; RDBUFF has no other effect,
442 * assuming ack is OK/FAULT and CTRL_STAT says "OK".
443 */
444 retval = adi_jtag_dp_scan(dap, JTAG_DP_DPACC, DP_RDBUFF,
445 DPAP_READ, 0, buffer + 4 * readcount,
446 &dap->ack);
447
448 return retval;
449 }
450
451 static int jtag_ap_q_abort(struct adiv5_dap *dap, uint8_t *ack)
452 {
453 /* for JTAG, this is the only valid ABORT register operation */
454 return adi_jtag_dp_scan_u32(dap, JTAG_DP_ABORT,
455 0, DPAP_WRITE, 1, NULL, ack);
456 }
457
458 static int jtag_dp_run(struct adiv5_dap *dap)
459 {
460 return jtagdp_transaction_endcheck(dap);
461 }
462
463 /* FIXME don't export ... just initialize as
464 * part of DAP setup
465 */
466 const struct dap_ops jtag_dp_ops = {
467 .queue_idcode_read = jtag_idcode_q_read,
468 .queue_dp_read = jtag_dp_q_read,
469 .queue_dp_write = jtag_dp_q_write,
470 .queue_ap_read = jtag_ap_q_read,
471 .queue_ap_write = jtag_ap_q_write,
472 .queue_ap_read_block = jtag_ap_q_read_block,
473 .queue_ap_abort = jtag_ap_q_abort,
474 .run = jtag_dp_run,
475 };
476
477
478 static const uint8_t swd2jtag_bitseq[] = {
479 /* More than 50 TCK/SWCLK cycles with TMS/SWDIO high,
480 * putting both JTAG and SWD logic into reset state.
481 */
482 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
483 /* Switching equence disables SWD and enables JTAG
484 * NOTE: bits in the DP's IDCODE can expose the need for
485 * the old/deprecated sequence (0xae 0xde).
486 */
487 0x3c, 0xe7,
488 /* At least 50 TCK/SWCLK cycles with TMS/SWDIO high,
489 * putting both JTAG and SWD logic into reset state.
490 * NOTE: some docs say "at least 5".
491 */
492 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
493 };
494
495 /** Put the debug link into JTAG mode, if the target supports it.
496 * The link's initial mode may be either SWD or JTAG.
497 *
498 * @param target Enters JTAG mode (if possible).
499 *
500 * Note that targets implemented with SW-DP do not support JTAG, and
501 * that some targets which could otherwise support it may have been
502 * configured to disable JTAG signaling
503 *
504 * @return ERROR_OK or else a fault code.
505 */
506 int dap_to_jtag(struct target *target)
507 {
508 int retval;
509
510 LOG_DEBUG("Enter JTAG mode");
511
512 /* REVISIT it's nasty to need to make calls to a "jtag"
513 * subsystem if the link isn't in JTAG mode...
514 */
515
516 retval = jtag_add_tms_seq(8 * sizeof(swd2jtag_bitseq),
517 swd2jtag_bitseq, TAP_RESET);
518 if (retval == ERROR_OK)
519 retval = jtag_execute_queue();
520
521 /* REVISIT set up the DAP's ops vector for JTAG mode. */
522
523 return retval;
524 }

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)