arm: error propagation of arm_jtag_set_instr
[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
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 /***************************************************************************
55 *
56 * DPACC and APACC scanchain access through JTAG-DP (or SWJ-DP)
57 *
58 ***************************************************************************/
59
60 /**
61 * Scan DPACC or APACC using target ordered uint8_t buffers. No endianness
62 * conversions are performed. See section 4.4.3 of the ADIv5 spec, which
63 * discusses operations which access these registers.
64 *
65 * Note that only one scan is performed. If RnW is set, a separate scan
66 * will be needed to collect the data which was read; the "invalue" collects
67 * the posted result of a preceding operation, not the current one.
68 *
69 * @param dap the DAP
70 * @param instr JTAG_DP_APACC (AP access) or JTAG_DP_DPACC (DP access)
71 * @param reg_addr two significant bits; A[3:2]; for APACC access, the
72 * SELECT register has more addressing bits.
73 * @param RnW false iff outvalue will be written to the DP or AP
74 * @param outvalue points to a 32-bit (little-endian) integer
75 * @param invalue NULL, or points to a 32-bit (little-endian) integer
76 * @param ack points to where the three bit JTAG_ACK_* code will be stored
77 */
78
79 /* FIXME don't export ... this is a temporary workaround for the
80 * mem_ap_read_buf_u32() mess, until it's no longer JTAG-specific.
81 */
82 int adi_jtag_dp_scan(struct adiv5_dap *dap,
83 uint8_t instr, uint8_t reg_addr, uint8_t RnW,
84 uint8_t *outvalue, uint8_t *invalue, uint8_t *ack)
85 {
86 struct arm_jtag *jtag_info = dap->jtag_info;
87 struct scan_field fields[2];
88 uint8_t out_addr_buf;
89 int retval;
90
91 retval = arm_jtag_set_instr(jtag_info, instr, NULL, TAP_IDLE);
92 if (retval != ERROR_OK)
93 return retval;
94
95 /* Scan out a read or write operation using some DP or AP register.
96 * For APACC access with any sticky error flag set, this is discarded.
97 */
98 fields[0].num_bits = 3;
99 buf_set_u32(&out_addr_buf, 0, 3, ((reg_addr >> 1) & 0x6) | (RnW & 0x1));
100 fields[0].out_value = &out_addr_buf;
101 fields[0].in_value = ack;
102
103 /* NOTE: if we receive JTAG_ACK_WAIT, the previous operation did not
104 * complete; data we write is discarded, data we read is unpredictable.
105 * When overrun detect is active, STICKYORUN is set.
106 */
107
108 fields[1].num_bits = 32;
109 fields[1].out_value = outvalue;
110 fields[1].in_value = invalue;
111
112 jtag_add_dr_scan(jtag_info->tap, 2, fields, TAP_IDLE);
113
114 /* Add specified number of tck clocks after starting memory bus
115 * access, giving the hardware time to complete the access.
116 * They provide more time for the (MEM) AP to complete the read ...
117 * See "Minimum Response Time" for JTAG-DP, in the ADIv5 spec.
118 */
119 if ((instr == JTAG_DP_APACC)
120 && ((reg_addr == AP_REG_DRW)
121 || ((reg_addr & 0xF0) == AP_REG_BD0))
122 && (dap->memaccess_tck != 0))
123 jtag_add_runtest(dap->memaccess_tck,
124 TAP_IDLE);
125
126 return ERROR_OK;
127 }
128
129 /**
130 * Scan DPACC or APACC out and in from host ordered uint32_t buffers.
131 * This is exactly like adi_jtag_dp_scan(), except that endianness
132 * conversions are performed (so the types of invalue and outvalue
133 * must be different).
134 */
135 static int adi_jtag_dp_scan_u32(struct adiv5_dap *dap,
136 uint8_t instr, uint8_t reg_addr, uint8_t RnW,
137 uint32_t outvalue, uint32_t *invalue, uint8_t *ack)
138 {
139 uint8_t out_value_buf[4];
140 int retval;
141
142 buf_set_u32(out_value_buf, 0, 32, outvalue);
143
144 retval = adi_jtag_dp_scan(dap, instr, reg_addr, RnW,
145 out_value_buf, (uint8_t *)invalue, ack);
146 if (retval != ERROR_OK)
147 return retval;
148
149 if (invalue)
150 jtag_add_callback(arm_le_to_h_u32,
151 (jtag_callback_data_t) invalue);
152
153 return retval;
154 }
155
156 /**
157 * Utility to write AP registers.
158 */
159 static inline int adi_jtag_ap_write_check(struct adiv5_dap *dap,
160 uint8_t reg_addr, uint8_t *outvalue)
161 {
162 return adi_jtag_dp_scan(dap, JTAG_DP_APACC, reg_addr, DPAP_WRITE,
163 outvalue, NULL, NULL);
164 }
165
166 static int adi_jtag_scan_inout_check_u32(struct adiv5_dap *dap,
167 uint8_t instr, uint8_t reg_addr, uint8_t RnW,
168 uint32_t outvalue, uint32_t *invalue)
169 {
170 int retval;
171
172 /* Issue the read or write */
173 retval = adi_jtag_dp_scan_u32(dap, instr, reg_addr,
174 RnW, outvalue, NULL, NULL);
175 if (retval != ERROR_OK)
176 return retval;
177
178 /* For reads, collect posted value; RDBUFF has no other effect.
179 * Assumes read gets acked with OK/FAULT, and CTRL_STAT says "OK".
180 */
181 if ((RnW == DPAP_READ) && (invalue != NULL))
182 retval = adi_jtag_dp_scan_u32(dap, JTAG_DP_DPACC,
183 DP_RDBUFF, DPAP_READ, 0, invalue, &dap->ack);
184 return retval;
185 }
186
187 static int jtagdp_transaction_endcheck(struct adiv5_dap *dap)
188 {
189 int retval;
190 uint32_t ctrlstat;
191
192 /* too expensive to call keep_alive() here */
193
194 #if 0
195 /* Danger!!!! BROKEN!!!! */
196 adi_jtag_scan_inout_check_u32(dap, JTAG_DP_DPACC,
197 DP_CTRL_STAT, DPAP_READ, 0, &ctrlstat);
198 /* Danger!!!! BROKEN!!!! Why will jtag_execute_queue() fail here????
199 R956 introduced the check on return value here and now Michael Schwingen reports
200 that this code no longer works....
201
202 https://lists.berlios.de/pipermail/openocd-development/2008-September/003107.html
203 */
204 if ((retval = jtag_execute_queue()) != ERROR_OK)
205 {
206 LOG_ERROR("BUG: Why does this fail the first time????");
207 }
208 /* Why??? second time it works??? */
209 #endif
210
211 /* Post CTRL/STAT read; discard any previous posted read value
212 * but collect its ACK status.
213 */
214 adi_jtag_scan_inout_check_u32(dap, JTAG_DP_DPACC,
215 DP_CTRL_STAT, DPAP_READ, 0, &ctrlstat);
216 if ((retval = jtag_execute_queue()) != ERROR_OK)
217 return retval;
218
219 dap->ack = dap->ack & 0x7;
220
221 /* common code path avoids calling timeval_ms() */
222 if (dap->ack != JTAG_ACK_OK_FAULT)
223 {
224 long long then = timeval_ms();
225
226 while (dap->ack != JTAG_ACK_OK_FAULT)
227 {
228 if (dap->ack == JTAG_ACK_WAIT)
229 {
230 if ((timeval_ms()-then) > 1000)
231 {
232 /* NOTE: this would be a good spot
233 * to use JTAG_DP_ABORT.
234 */
235 LOG_WARNING("Timeout (1000ms) waiting "
236 "for ACK=OK/FAULT "
237 "in JTAG-DP transaction");
238 return ERROR_JTAG_DEVICE_ERROR;
239 }
240 }
241 else
242 {
243 LOG_WARNING("Invalid ACK %#x "
244 "in JTAG-DP transaction",
245 dap->ack);
246 return ERROR_JTAG_DEVICE_ERROR;
247 }
248
249 adi_jtag_scan_inout_check_u32(dap, JTAG_DP_DPACC,
250 DP_CTRL_STAT, DPAP_READ, 0, &ctrlstat);
251 if ((retval = dap_run(dap)) != ERROR_OK)
252 return retval;
253 dap->ack = dap->ack & 0x7;
254 }
255 }
256
257 /* REVISIT also STICKYCMP, for pushed comparisons (nyet used) */
258
259 /* Check for STICKYERR and STICKYORUN */
260 if (ctrlstat & (SSTICKYORUN | SSTICKYERR))
261 {
262 LOG_DEBUG("jtag-dp: CTRL/STAT error, 0x%" PRIx32, ctrlstat);
263 /* Check power to debug regions */
264 if ((ctrlstat & 0xf0000000) != 0xf0000000)
265 {
266 retval = ahbap_debugport_init(dap);
267 if (retval != ERROR_OK)
268 return retval;
269 }
270 else
271 {
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 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 adi_jtag_scan_inout_check_u32(dap, JTAG_DP_DPACC,
300 DP_CTRL_STAT, DPAP_READ, 0, &ctrlstat);
301 if ((retval = dap_run(dap)) != ERROR_OK)
302 return retval;
303
304 LOG_DEBUG("jtag-dp: CTRL/STAT 0x%" PRIx32, ctrlstat);
305
306 retval = dap_queue_ap_read(dap,
307 AP_REG_CSW, &mem_ap_csw);
308 if (retval != ERROR_OK)
309 return retval;
310
311 retval = dap_queue_ap_read(dap,
312 AP_REG_TAR, &mem_ap_tar);
313 if (retval != ERROR_OK)
314 return retval;
315
316 if ((retval = dap_run(dap)) != ERROR_OK)
317 return retval;
318 LOG_ERROR("MEM_AP_CSW 0x%" PRIx32 ", MEM_AP_TAR 0x%"
319 PRIx32, mem_ap_csw, mem_ap_tar);
320
321 }
322 if ((retval = dap_run(dap)) != ERROR_OK)
323 return retval;
324 return ERROR_JTAG_DEVICE_ERROR;
325 }
326
327 return ERROR_OK;
328 }
329
330 /*--------------------------------------------------------------------------*/
331
332 static int jtag_idcode_q_read(struct adiv5_dap *dap,
333 uint8_t *ack, uint32_t *data)
334 {
335 struct arm_jtag *jtag_info = dap->jtag_info;
336 int retval;
337 struct scan_field fields[1];
338
339 /* This is a standard JTAG operation -- no DAP tweakage */
340 retval = arm_jtag_set_instr(jtag_info, JTAG_DP_IDCODE, NULL, TAP_IDLE);
341 if (retval != ERROR_OK)
342 return retval;
343
344 fields[0].num_bits = 32;
345 fields[0].out_value = NULL;
346 fields[0].in_value = (void *) data;
347
348 jtag_add_dr_scan(jtag_info->tap, 1, fields, TAP_IDLE);
349
350 jtag_add_callback(arm_le_to_h_u32,
351 (jtag_callback_data_t) data);
352
353 return ERROR_OK;
354 }
355
356 static int jtag_dp_q_read(struct adiv5_dap *dap, unsigned reg,
357 uint32_t *data)
358 {
359 return adi_jtag_scan_inout_check_u32(dap, JTAG_DP_DPACC,
360 reg, DPAP_READ, 0, data);
361 }
362
363 static int jtag_dp_q_write(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_WRITE, data, NULL);
368 }
369
370 /** Select the AP register bank matching bits 7:4 of reg. */
371 static int jtag_ap_q_bankselect(struct adiv5_dap *dap, unsigned reg)
372 {
373 uint32_t select_ap_bank = reg & 0x000000F0;
374
375 if (select_ap_bank == dap->ap_bank_value)
376 return ERROR_OK;
377 dap->ap_bank_value = select_ap_bank;
378
379 select_ap_bank |= dap->apsel;
380
381 return jtag_dp_q_write(dap, DP_SELECT, select_ap_bank);
382 }
383
384 static int jtag_ap_q_read(struct adiv5_dap *dap, unsigned reg,
385 uint32_t *data)
386 {
387 int retval = jtag_ap_q_bankselect(dap, reg);
388
389 if (retval != ERROR_OK)
390 return retval;
391
392 return adi_jtag_scan_inout_check_u32(dap, JTAG_DP_APACC, reg,
393 DPAP_READ, 0, data);
394 }
395
396 static int jtag_ap_q_write(struct adiv5_dap *dap, unsigned reg,
397 uint32_t data)
398 {
399 uint8_t out_value_buf[4];
400
401 int retval = jtag_ap_q_bankselect(dap, reg);
402 if (retval != ERROR_OK)
403 return retval;
404
405 buf_set_u32(out_value_buf, 0, 32, data);
406
407 return adi_jtag_ap_write_check(dap, reg, out_value_buf);
408 }
409
410 static int jtag_ap_q_abort(struct adiv5_dap *dap, uint8_t *ack)
411 {
412 /* for JTAG, this is the only valid ABORT register operation */
413 return adi_jtag_dp_scan_u32(dap, JTAG_DP_ABORT,
414 0, DPAP_WRITE, 1, NULL, ack);
415 }
416
417 static int jtag_dp_run(struct adiv5_dap *dap)
418 {
419 return jtagdp_transaction_endcheck(dap);
420 }
421
422 /* FIXME don't export ... just initialize as
423 * part of DAP setup
424 */
425 const struct dap_ops jtag_dp_ops = {
426 .queue_idcode_read = jtag_idcode_q_read,
427 .queue_dp_read = jtag_dp_q_read,
428 .queue_dp_write = jtag_dp_q_write,
429 .queue_ap_read = jtag_ap_q_read,
430 .queue_ap_write = jtag_ap_q_write,
431 .queue_ap_abort = jtag_ap_q_abort,
432 .run = jtag_dp_run,
433 };
434
435
436 static const uint8_t swd2jtag_bitseq[] = {
437 /* More than 50 TCK/SWCLK cycles with TMS/SWDIO high,
438 * putting both JTAG and SWD logic into reset state.
439 */
440 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
441 /* Switching equence disables SWD and enables JTAG
442 * NOTE: bits in the DP's IDCODE can expose the need for
443 * the old/deprecated sequence (0xae 0xde).
444 */
445 0x3c, 0xe7,
446 /* At least 50 TCK/SWCLK cycles with TMS/SWDIO high,
447 * putting both JTAG and SWD logic into reset state.
448 * NOTE: some docs say "at least 5".
449 */
450 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
451 };
452
453 /** Put the debug link into JTAG mode, if the target supports it.
454 * The link's initial mode may be either SWD or JTAG.
455 *
456 * @param target Enters JTAG mode (if possible).
457 *
458 * Note that targets implemented with SW-DP do not support JTAG, and
459 * that some targets which could otherwise support it may have been
460 * configured to disable JTAG signaling
461 *
462 * @return ERROR_OK or else a fault code.
463 */
464 int dap_to_jtag(struct target *target)
465 {
466 int retval;
467
468 LOG_DEBUG("Enter JTAG mode");
469
470 /* REVISIT it's nasty to need to make calls to a "jtag"
471 * subsystem if the link isn't in JTAG mode...
472 */
473
474 retval = jtag_add_tms_seq(8 * sizeof(swd2jtag_bitseq),
475 swd2jtag_bitseq, TAP_RESET);
476 if (retval == ERROR_OK)
477 retval = jtag_execute_queue();
478
479 /* REVISIT set up the DAP's ops vector for JTAG mode. */
480
481 return retval;
482 }

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)