jtag/hla_interface: avoid segfault with adapters that do not have configurable speed
[openocd.git] / src / target / adi_v5_swd.c
blob1827c59dbffc31278266bf98142a603dcceecf6e
1 /***************************************************************************
3 * Copyright (C) 2010 by David Brownell
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.
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.
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 ***************************************************************************/
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.
29 * Single-DAP support only.
31 * for details, see "ARM IHI 0031A"
32 * ARM Debug Interface v5 Architecture Specification
33 * especially section 5.3 for SWD protocol
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).
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.
45 #ifdef HAVE_CONFIG_H
46 #include "config.h"
47 #endif
49 #include "arm.h"
50 #include "arm_adi_v5.h"
51 #include <helper/time_support.h>
53 #include <transport/transport.h>
54 #include <jtag/interface.h>
56 #include <jtag/swd.h>
58 /* YUK! - but this is currently a global.... */
59 extern struct jtag_interface *jtag_interface;
60 static bool do_sync;
62 static void swd_finish_read(struct adiv5_dap *dap)
64 const struct swd_driver *swd = jtag_interface->swd;
65 if (dap->last_read != NULL) {
66 swd->read_reg(dap, swd_cmd(true, false, DP_RDBUFF), dap->last_read);
67 dap->last_read = NULL;
71 static int swd_queue_dp_write(struct adiv5_dap *dap, unsigned reg,
72 uint32_t data);
73 static int swd_queue_dp_read(struct adiv5_dap *dap, unsigned reg,
74 uint32_t *data);
76 static void swd_clear_sticky_errors(struct adiv5_dap *dap)
78 const struct swd_driver *swd = jtag_interface->swd;
79 assert(swd);
81 swd->write_reg(dap, swd_cmd(false, false, DP_ABORT),
82 STKCMPCLR | STKERRCLR | WDERRCLR | ORUNERRCLR);
85 static int swd_run_inner(struct adiv5_dap *dap)
87 const struct swd_driver *swd = jtag_interface->swd;
88 int retval;
90 retval = swd->run(dap);
92 if (retval != ERROR_OK) {
93 /* fault response */
94 dap->do_reconnect = true;
97 return retval;
100 static int swd_connect(struct adiv5_dap *dap)
102 uint32_t idcode;
103 int status;
105 /* FIXME validate transport config ... is the
106 * configured DAP present (check IDCODE)?
107 * Is *only* one DAP configured?
109 * MUST READ IDCODE
112 /* Note, debugport_init() does setup too */
113 jtag_interface->swd->switch_seq(dap, JTAG_TO_SWD);
115 swd_queue_dp_read(dap, DP_IDCODE, &idcode);
117 /* force clear all sticky faults */
118 swd_clear_sticky_errors(dap);
120 status = swd_run_inner(dap);
122 if (status == ERROR_OK) {
123 LOG_INFO("SWD IDCODE %#8.8" PRIx32, idcode);
124 dap->do_reconnect = false;
127 return status;
130 static inline int check_sync(struct adiv5_dap *dap)
132 return do_sync ? swd_run_inner(dap) : ERROR_OK;
135 static int swd_queue_ap_abort(struct adiv5_dap *dap, uint8_t *ack)
137 const struct swd_driver *swd = jtag_interface->swd;
138 assert(swd);
140 swd->write_reg(dap, swd_cmd(false, false, DP_ABORT),
141 DAPABORT | STKCMPCLR | STKERRCLR | WDERRCLR | ORUNERRCLR);
142 return check_sync(dap);
145 /** Select the DP register bank matching bits 7:4 of reg. */
146 static void swd_queue_dp_bankselect(struct adiv5_dap *dap, unsigned reg)
148 uint32_t select_dp_bank = (reg & 0x000000F0) >> 4;
150 if (reg == DP_SELECT)
151 return;
153 if (select_dp_bank == dap->dp_bank_value)
154 return;
156 dap->dp_bank_value = select_dp_bank;
157 select_dp_bank |= dap->ap_current | dap->ap_bank_value;
159 swd_queue_dp_write(dap, DP_SELECT, select_dp_bank);
162 static int swd_queue_dp_read(struct adiv5_dap *dap, unsigned reg,
163 uint32_t *data)
165 const struct swd_driver *swd = jtag_interface->swd;
166 assert(swd);
168 swd_queue_dp_bankselect(dap, reg);
169 swd->read_reg(dap, swd_cmd(true, false, reg), data);
171 return check_sync(dap);
174 static int swd_queue_dp_write(struct adiv5_dap *dap, unsigned reg,
175 uint32_t data)
177 const struct swd_driver *swd = jtag_interface->swd;
178 assert(swd);
180 swd_finish_read(dap);
181 swd_queue_dp_bankselect(dap, reg);
182 swd->write_reg(dap, swd_cmd(false, false, reg), data);
184 return check_sync(dap);
187 /** Select the AP register bank matching bits 7:4 of reg. */
188 static void swd_queue_ap_bankselect(struct adiv5_dap *dap, unsigned reg)
190 uint32_t select_ap_bank = reg & 0x000000F0;
192 if (select_ap_bank == dap->ap_bank_value)
193 return;
195 dap->ap_bank_value = select_ap_bank;
196 select_ap_bank |= dap->ap_current | dap->dp_bank_value;
198 swd_queue_dp_write(dap, DP_SELECT, select_ap_bank);
201 static int swd_queue_ap_read(struct adiv5_dap *dap, unsigned reg,
202 uint32_t *data)
204 const struct swd_driver *swd = jtag_interface->swd;
205 assert(swd);
207 if (dap->do_reconnect) {
208 int retval = swd_connect(dap);
209 if (retval != ERROR_OK)
210 return retval;
213 swd_queue_ap_bankselect(dap, reg);
214 swd->read_reg(dap, swd_cmd(true, true, reg), dap->last_read);
215 dap->last_read = data;
217 return check_sync(dap);
220 static int swd_queue_ap_write(struct adiv5_dap *dap, unsigned reg,
221 uint32_t data)
223 const struct swd_driver *swd = jtag_interface->swd;
224 assert(swd);
226 swd_finish_read(dap);
227 swd_queue_ap_bankselect(dap, reg);
228 swd->write_reg(dap, swd_cmd(false, true, reg), data);
230 return check_sync(dap);
233 /** Executes all queued DAP operations. */
234 static int swd_run(struct adiv5_dap *dap)
236 swd_finish_read(dap);
237 return swd_run_inner(dap);
240 const struct dap_ops swd_dap_ops = {
241 .is_swd = true,
243 .queue_dp_read = swd_queue_dp_read,
244 .queue_dp_write = swd_queue_dp_write,
245 .queue_ap_read = swd_queue_ap_read,
246 .queue_ap_write = swd_queue_ap_write,
247 .queue_ap_abort = swd_queue_ap_abort,
248 .run = swd_run,
252 * This represents the bits which must be sent out on TMS/SWDIO to
253 * switch a DAP implemented using an SWJ-DP module into SWD mode.
254 * These bits are stored (and transmitted) LSB-first.
256 * See the DAP-Lite specification, section 2.2.5 for information
257 * about making the debug link select SWD or JTAG. (Similar info
258 * is in a few other ARM documents.)
260 static const uint8_t jtag2swd_bitseq[] = {
261 /* More than 50 TCK/SWCLK cycles with TMS/SWDIO high,
262 * putting both JTAG and SWD logic into reset state.
264 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
265 /* Switching sequence enables SWD and disables JTAG
266 * NOTE: bits in the DP's IDCODE may expose the need for
267 * an old/obsolete/deprecated sequence (0xb6 0xed).
269 0x9e, 0xe7,
270 /* More than 50 TCK/SWCLK cycles with TMS/SWDIO high,
271 * putting both JTAG and SWD logic into reset state.
273 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
277 * Put the debug link into SWD mode, if the target supports it.
278 * The link's initial mode may be either JTAG (for example,
279 * with SWJ-DP after reset) or SWD.
281 * @param target Enters SWD mode (if possible).
283 * Note that targets using the JTAG-DP do not support SWD, and that
284 * some targets which could otherwise support it may have have been
285 * configured to disable SWD signaling
287 * @return ERROR_OK or else a fault code.
289 int dap_to_swd(struct target *target)
291 struct arm *arm = target_to_arm(target);
292 int retval;
294 if (!arm->dap) {
295 LOG_ERROR("SWD mode is not available");
296 return ERROR_FAIL;
299 LOG_DEBUG("Enter SWD mode");
301 /* REVISIT it's ugly to need to make calls to a "jtag"
302 * subsystem if the link may not be in JTAG mode...
305 retval = jtag_add_tms_seq(8 * sizeof(jtag2swd_bitseq),
306 jtag2swd_bitseq, TAP_INVALID);
307 if (retval == ERROR_OK)
308 retval = jtag_execute_queue();
310 /* set up the DAP's ops vector for SWD mode. */
311 arm->dap->ops = &swd_dap_ops;
313 return retval;
316 COMMAND_HANDLER(handle_swd_wcr)
318 int retval;
319 struct target *target = get_current_target(CMD_CTX);
320 struct arm *arm = target_to_arm(target);
321 struct adiv5_dap *dap = arm->dap;
322 uint32_t wcr;
323 unsigned trn, scale = 0;
325 switch (CMD_ARGC) {
326 /* no-args: just dump state */
327 case 0:
328 /*retval = swd_queue_dp_read(dap, DP_WCR, &wcr); */
329 retval = dap_queue_dp_read(dap, DP_WCR, &wcr);
330 if (retval == ERROR_OK)
331 dap->ops->run(dap);
332 if (retval != ERROR_OK) {
333 LOG_ERROR("can't read WCR?");
334 return retval;
337 command_print(CMD_CTX,
338 "turnaround=%" PRIu32 ", prescale=%" PRIu32,
339 WCR_TO_TRN(wcr),
340 WCR_TO_PRESCALE(wcr));
341 return ERROR_OK;
343 case 2: /* TRN and prescale */
344 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], scale);
345 if (scale > 7) {
346 LOG_ERROR("prescale %d is too big", scale);
347 return ERROR_FAIL;
349 /* FALL THROUGH */
351 case 1: /* TRN only */
352 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], trn);
353 if (trn < 1 || trn > 4) {
354 LOG_ERROR("turnaround %d is invalid", trn);
355 return ERROR_FAIL;
358 wcr = ((trn - 1) << 8) | scale;
359 /* FIXME
360 * write WCR ...
361 * then, re-init adapter with new TRN
363 LOG_ERROR("can't yet modify WCR");
364 return ERROR_FAIL;
366 default: /* too many arguments */
367 return ERROR_COMMAND_SYNTAX_ERROR;
371 static const struct command_registration swd_commands[] = {
374 * Set up SWD and JTAG targets identically, unless/until
375 * infrastructure improves ... meanwhile, ignore all
376 * JTAG-specific stuff like IR length for SWD.
378 * REVISIT can we verify "just one SWD DAP" here/early?
380 .name = "newdap",
381 .jim_handler = jim_jtag_newtap,
382 .mode = COMMAND_CONFIG,
383 .help = "declare a new SWD DAP"
386 .name = "wcr",
387 .handler = handle_swd_wcr,
388 .mode = COMMAND_ANY,
389 .help = "display or update DAP's WCR register",
390 .usage = "turnaround (1..4), prescale (0..7)",
393 /* REVISIT -- add a command for SWV trace on/off */
394 COMMAND_REGISTRATION_DONE
397 static const struct command_registration swd_handlers[] = {
399 .name = "swd",
400 .mode = COMMAND_ANY,
401 .help = "SWD command group",
402 .chain = swd_commands,
404 COMMAND_REGISTRATION_DONE
407 static int swd_select(struct command_context *ctx)
409 int retval;
411 retval = register_commands(ctx, NULL, swd_handlers);
413 if (retval != ERROR_OK)
414 return retval;
416 const struct swd_driver *swd = jtag_interface->swd;
418 /* be sure driver is in SWD mode; start
419 * with hardware default TRN (1), it can be changed later
421 if (!swd || !swd->read_reg || !swd->write_reg || !swd->init) {
422 LOG_DEBUG("no SWD driver?");
423 return ERROR_FAIL;
426 retval = swd->init();
427 if (retval != ERROR_OK) {
428 LOG_DEBUG("can't init SWD driver");
429 return retval;
432 /* force DAP into SWD mode (not JTAG) */
433 /*retval = dap_to_swd(target);*/
435 if (ctx->current_target) {
436 /* force DAP into SWD mode (not JTAG) */
437 struct target *target = get_current_target(ctx);
438 retval = dap_to_swd(target);
441 return retval;
444 static int swd_init(struct command_context *ctx)
446 struct target *target = get_current_target(ctx);
447 struct arm *arm = target_to_arm(target);
448 struct adiv5_dap *dap = arm->dap;
449 /* Force the DAP's ops vector for SWD mode.
450 * messy - is there a better way? */
451 arm->dap->ops = &swd_dap_ops;
453 return swd_connect(dap);
456 static struct transport swd_transport = {
457 .name = "swd",
458 .select = swd_select,
459 .init = swd_init,
462 static void swd_constructor(void) __attribute__((constructor));
463 static void swd_constructor(void)
465 transport_register(&swd_transport);
468 /** Returns true if the current debug session
469 * is using SWD as its transport.
471 bool transport_is_swd(void)
473 return get_current_transport() == &swd_transport;