adi_v5_swd: Read RDBUFF once after a sequence of AP reads
[openocd.git] / src / target / adi_v5_swd.c
1 /***************************************************************************
2 *
3 * Copyright (C) 2010 by David Brownell
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the
17 * Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 ***************************************************************************/
20
21 /**
22 * @file
23 * Utilities to support ARM "Serial Wire Debug" (SWD), a low pin-count debug
24 * link protocol used in cases where JTAG is not wanted. This is coupled to
25 * recent versions of ARM's "CoreSight" debug framework. This specific code
26 * is a transport level interface, with "target/arm_adi_v5.[hc]" code
27 * understanding operation semantics, shared with the JTAG transport.
28 *
29 * Single-DAP support only.
30 *
31 * for details, see "ARM IHI 0031A"
32 * ARM Debug Interface v5 Architecture Specification
33 * especially section 5.3 for SWD protocol
34 *
35 * On many chips (most current Cortex-M3 parts) SWD is a run-time alternative
36 * to JTAG. Boards may support one or both. There are also SWD-only chips,
37 * (using SW-DP not SWJ-DP).
38 *
39 * Even boards that also support JTAG can benefit from SWD support, because
40 * usually there's no way to access the SWO trace view mechanism in JTAG mode.
41 * That is, trace access may require SWD support.
42 *
43 */
44
45 #ifdef HAVE_CONFIG_H
46 #include "config.h"
47 #endif
48
49 #include "arm.h"
50 #include "arm_adi_v5.h"
51 #include <helper/time_support.h>
52
53 #include <transport/transport.h>
54 #include <jtag/interface.h>
55
56 #include <jtag/swd.h>
57
58 /* YUK! - but this is currently a global.... */
59 extern struct jtag_interface *jtag_interface;
60
61 static int swd_finish_read(struct adiv5_dap *dap)
62 {
63 const struct swd_driver *swd = jtag_interface->swd;
64 int retval = ERROR_OK;
65 if (dap->last_read != NULL) {
66 retval = swd->read_reg(swd_cmd(true, false, DP_RDBUFF), dap->last_read);
67 dap->last_read = NULL;
68 }
69 return retval;
70 }
71
72 static int (swd_queue_dp_write)(struct adiv5_dap *dap, unsigned reg,
73 uint32_t data);
74
75 static int swd_queue_ap_abort(struct adiv5_dap *dap, uint8_t *ack)
76 {
77 const struct swd_driver *swd = jtag_interface->swd;
78 assert(swd);
79
80 return swd->write_reg(swd_cmd(false, false, DP_ABORT),
81 STKCMPCLR | STKERRCLR | WDERRCLR | ORUNERRCLR);
82 }
83
84 /** Select the DP register bank matching bits 7:4 of reg. */
85 static int swd_queue_dp_bankselect(struct adiv5_dap *dap, unsigned reg)
86 {
87 uint32_t select_dp_bank = (reg & 0x000000F0) >> 4;
88
89 if (reg == DP_SELECT)
90 return ERROR_OK;
91
92 if (select_dp_bank == dap->dp_bank_value)
93 return ERROR_OK;
94
95 dap->dp_bank_value = select_dp_bank;
96 select_dp_bank |= dap->ap_current | dap->ap_bank_value;
97
98 return swd_queue_dp_write(dap, DP_SELECT, select_dp_bank);
99 }
100
101 static int swd_queue_dp_read(struct adiv5_dap *dap, unsigned reg,
102 uint32_t *data)
103 {
104 int retval;
105 /* REVISIT status return vs ack ... */
106 const struct swd_driver *swd = jtag_interface->swd;
107 assert(swd);
108
109 retval = swd_queue_dp_bankselect(dap, reg);
110 if (retval != ERROR_OK)
111 return retval;
112
113 retval = swd->read_reg(swd_cmd(true, false, reg), data);
114
115 if (retval != ERROR_OK) {
116 /* fault response */
117 uint8_t ack = retval & 0xff;
118 swd_queue_ap_abort(dap, &ack);
119 }
120
121 return retval;
122 }
123
124 static int swd_queue_idcode_read(struct adiv5_dap *dap,
125 uint8_t *ack, uint32_t *data)
126 {
127 int status = swd_queue_dp_read(dap, DP_IDCODE, data);
128 if (status < 0)
129 return status;
130 *ack = status;
131 /* ?? */
132 return ERROR_OK;
133 }
134
135 static int (swd_queue_dp_write)(struct adiv5_dap *dap, unsigned reg,
136 uint32_t data)
137 {
138 int retval;
139 /* REVISIT status return vs ack ... */
140 const struct swd_driver *swd = jtag_interface->swd;
141 assert(swd);
142
143 retval = swd_finish_read(dap);
144 if (retval != ERROR_OK)
145 return retval;
146
147 retval = swd_queue_dp_bankselect(dap, reg);
148 if (retval != ERROR_OK)
149 return retval;
150
151 retval = swd->write_reg(swd_cmd(false, false, reg), data);
152
153 if (retval != ERROR_OK) {
154 /* fault response */
155 uint8_t ack = retval & 0xff;
156 swd_queue_ap_abort(dap, &ack);
157 }
158
159 return retval;
160 }
161
162 /** Select the AP register bank matching bits 7:4 of reg. */
163 static int swd_queue_ap_bankselect(struct adiv5_dap *dap, unsigned reg)
164 {
165 uint32_t select_ap_bank = reg & 0x000000F0;
166
167 if (select_ap_bank == dap->ap_bank_value)
168 return ERROR_OK;
169
170 dap->ap_bank_value = select_ap_bank;
171 select_ap_bank |= dap->ap_current | dap->dp_bank_value;
172
173 return swd_queue_dp_write(dap, DP_SELECT, select_ap_bank);
174 }
175
176 static int (swd_queue_ap_read)(struct adiv5_dap *dap, unsigned reg,
177 uint32_t *data)
178 {
179 /* REVISIT status return ... */
180 const struct swd_driver *swd = jtag_interface->swd;
181 assert(swd);
182
183 int retval = swd_queue_ap_bankselect(dap, reg);
184 if (retval != ERROR_OK)
185 return retval;
186
187 retval = swd->read_reg(swd_cmd(true, true, reg), dap->last_read);
188 dap->last_read = data;
189
190 if (retval != ERROR_OK) {
191 /* fault response */
192 uint8_t ack = retval & 0xff;
193 swd_queue_ap_abort(dap, &ack);
194 return retval;
195 }
196
197 return retval;
198 }
199
200 static int (swd_queue_ap_write)(struct adiv5_dap *dap, unsigned reg,
201 uint32_t data)
202 {
203 /* REVISIT status return ... */
204 const struct swd_driver *swd = jtag_interface->swd;
205 assert(swd);
206 int retval;
207
208 retval = swd_finish_read(dap);
209 if (retval != ERROR_OK)
210 return retval;
211
212 retval = swd_queue_ap_bankselect(dap, reg);
213 if (retval != ERROR_OK)
214 return retval;
215
216 retval = swd->write_reg(swd_cmd(false, true, reg), data);
217
218 if (retval != ERROR_OK) {
219 /* fault response */
220 uint8_t ack = retval & 0xff;
221 swd_queue_ap_abort(dap, &ack);
222 }
223
224 return retval;
225 }
226
227 /** Executes all queued DAP operations. */
228 static int swd_run(struct adiv5_dap *dap)
229 {
230 /* for now the SWD interface hard-wires a zero-size queue. */
231
232 int retval = swd_finish_read(dap);
233
234 /* FIXME but we still need to check and scrub
235 * any hardware errors ...
236 */
237 return retval;
238 }
239
240 const struct dap_ops swd_dap_ops = {
241 .is_swd = true,
242
243 .queue_idcode_read = swd_queue_idcode_read,
244 .queue_dp_read = swd_queue_dp_read,
245 .queue_dp_write = swd_queue_dp_write,
246 .queue_ap_read = swd_queue_ap_read,
247 .queue_ap_write = swd_queue_ap_write,
248 .queue_ap_abort = swd_queue_ap_abort,
249 .run = swd_run,
250 };
251
252 /*
253 * This represents the bits which must be sent out on TMS/SWDIO to
254 * switch a DAP implemented using an SWJ-DP module into SWD mode.
255 * These bits are stored (and transmitted) LSB-first.
256 *
257 * See the DAP-Lite specification, section 2.2.5 for information
258 * about making the debug link select SWD or JTAG. (Similar info
259 * is in a few other ARM documents.)
260 */
261 static const uint8_t jtag2swd_bitseq[] = {
262 /* More than 50 TCK/SWCLK cycles with TMS/SWDIO high,
263 * putting both JTAG and SWD logic into reset state.
264 */
265 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
266 /* Switching sequence enables SWD and disables JTAG
267 * NOTE: bits in the DP's IDCODE may expose the need for
268 * an old/obsolete/deprecated sequence (0xb6 0xed).
269 */
270 0x9e, 0xe7,
271 /* More than 50 TCK/SWCLK cycles with TMS/SWDIO high,
272 * putting both JTAG and SWD logic into reset state.
273 */
274 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
275 };
276
277 /**
278 * Put the debug link into SWD mode, if the target supports it.
279 * The link's initial mode may be either JTAG (for example,
280 * with SWJ-DP after reset) or SWD.
281 *
282 * @param target Enters SWD mode (if possible).
283 *
284 * Note that targets using the JTAG-DP do not support SWD, and that
285 * some targets which could otherwise support it may have have been
286 * configured to disable SWD signaling
287 *
288 * @return ERROR_OK or else a fault code.
289 */
290 int dap_to_swd(struct target *target)
291 {
292 struct arm *arm = target_to_arm(target);
293 int retval;
294
295 LOG_DEBUG("Enter SWD mode");
296
297 /* REVISIT it's ugly to need to make calls to a "jtag"
298 * subsystem if the link may not be in JTAG mode...
299 */
300
301 retval = jtag_add_tms_seq(8 * sizeof(jtag2swd_bitseq),
302 jtag2swd_bitseq, TAP_INVALID);
303 if (retval == ERROR_OK)
304 retval = jtag_execute_queue();
305
306 /* set up the DAP's ops vector for SWD mode. */
307 arm->dap->ops = &swd_dap_ops;
308
309 return retval;
310 }
311
312 COMMAND_HANDLER(handle_swd_wcr)
313 {
314 int retval;
315 struct target *target = get_current_target(CMD_CTX);
316 struct arm *arm = target_to_arm(target);
317 struct adiv5_dap *dap = arm->dap;
318 uint32_t wcr;
319 unsigned trn, scale = 0;
320
321 switch (CMD_ARGC) {
322 /* no-args: just dump state */
323 case 0:
324 /*retval = swd_queue_dp_read(dap, DP_WCR, &wcr); */
325 retval = dap_queue_dp_read(dap, DP_WCR, &wcr);
326 if (retval == ERROR_OK)
327 dap->ops->run(dap);
328 if (retval != ERROR_OK) {
329 LOG_ERROR("can't read WCR?");
330 return retval;
331 }
332
333 command_print(CMD_CTX,
334 "turnaround=%" PRIu32 ", prescale=%" PRIu32,
335 WCR_TO_TRN(wcr),
336 WCR_TO_PRESCALE(wcr));
337 return ERROR_OK;
338
339 case 2: /* TRN and prescale */
340 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], scale);
341 if (scale > 7) {
342 LOG_ERROR("prescale %d is too big", scale);
343 return ERROR_FAIL;
344 }
345 /* FALL THROUGH */
346
347 case 1: /* TRN only */
348 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], trn);
349 if (trn < 1 || trn > 4) {
350 LOG_ERROR("turnaround %d is invalid", trn);
351 return ERROR_FAIL;
352 }
353
354 wcr = ((trn - 1) << 8) | scale;
355 /* FIXME
356 * write WCR ...
357 * then, re-init adapter with new TRN
358 */
359 LOG_ERROR("can't yet modify WCR");
360 return ERROR_FAIL;
361
362 default: /* too many arguments */
363 return ERROR_COMMAND_SYNTAX_ERROR;
364 }
365 }
366
367 static const struct command_registration swd_commands[] = {
368 {
369 /*
370 * Set up SWD and JTAG targets identically, unless/until
371 * infrastructure improves ... meanwhile, ignore all
372 * JTAG-specific stuff like IR length for SWD.
373 *
374 * REVISIT can we verify "just one SWD DAP" here/early?
375 */
376 .name = "newdap",
377 .jim_handler = jim_jtag_newtap,
378 .mode = COMMAND_CONFIG,
379 .help = "declare a new SWD DAP"
380 },
381 {
382 .name = "wcr",
383 .handler = handle_swd_wcr,
384 .mode = COMMAND_ANY,
385 .help = "display or update DAP's WCR register",
386 .usage = "turnaround (1..4), prescale (0..7)",
387 },
388
389 /* REVISIT -- add a command for SWV trace on/off */
390 COMMAND_REGISTRATION_DONE
391 };
392
393 static const struct command_registration swd_handlers[] = {
394 {
395 .name = "swd",
396 .mode = COMMAND_ANY,
397 .help = "SWD command group",
398 .chain = swd_commands,
399 },
400 COMMAND_REGISTRATION_DONE
401 };
402
403 static int swd_select(struct command_context *ctx)
404 {
405 int retval;
406
407 retval = register_commands(ctx, NULL, swd_handlers);
408
409 if (retval != ERROR_OK)
410 return retval;
411
412 const struct swd_driver *swd = jtag_interface->swd;
413
414 /* be sure driver is in SWD mode; start
415 * with hardware default TRN (1), it can be changed later
416 */
417 if (!swd || !swd->read_reg || !swd->write_reg || !swd->init) {
418 LOG_DEBUG("no SWD driver?");
419 return ERROR_FAIL;
420 }
421
422 retval = swd->init(1);
423 if (retval != ERROR_OK) {
424 LOG_DEBUG("can't init SWD driver");
425 return retval;
426 }
427
428 /* force DAP into SWD mode (not JTAG) */
429 /*retval = dap_to_swd(target);*/
430
431 if (ctx->current_target) {
432 /* force DAP into SWD mode (not JTAG) */
433 struct target *target = get_current_target(ctx);
434 retval = dap_to_swd(target);
435 }
436
437 return retval;
438 }
439
440 static int swd_init(struct command_context *ctx)
441 {
442 struct target *target = get_current_target(ctx);
443 struct arm *arm = target_to_arm(target);
444 struct adiv5_dap *dap = arm->dap;
445 uint32_t idcode;
446 int status;
447
448 /* Force the DAP's ops vector for SWD mode.
449 * messy - is there a better way? */
450 arm->dap->ops = &swd_dap_ops;
451
452 /* FIXME validate transport config ... is the
453 * configured DAP present (check IDCODE)?
454 * Is *only* one DAP configured?
455 *
456 * MUST READ IDCODE
457 */
458
459 /* Note, debugport_init() does setup too */
460
461 uint8_t ack;
462
463 status = swd_queue_idcode_read(dap, &ack, &idcode);
464
465 if (status == ERROR_OK)
466 LOG_INFO("SWD IDCODE %#8.8" PRIx32, idcode);
467
468 /* force clear all sticky faults */
469 swd_queue_ap_abort(dap, &ack);
470
471 /* this is a workaround to get polling working */
472 jtag_add_reset(0, 0);
473
474 return status;
475 }
476
477 static struct transport swd_transport = {
478 .name = "swd",
479 .select = swd_select,
480 .init = swd_init,
481 };
482
483 static void swd_constructor(void) __attribute__((constructor));
484 static void swd_constructor(void)
485 {
486 transport_register(&swd_transport);
487 }
488
489 /** Returns true if the current debug session
490 * is using SWD as its transport.
491 */
492 bool transport_is_swd(void)
493 {
494 return get_current_transport() == &swd_transport;
495 }

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)