]> git.sur5r.net Git - openocd/blob - src/target/adi_v5_swd.c
arm_adi_v5: Remove unused is_swd flag
[openocd] / 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 static bool do_sync;
61
62 static void swd_finish_read(struct adiv5_dap *dap)
63 {
64         const struct swd_driver *swd = jtag_interface->swd;
65         if (dap->last_read != NULL) {
66                 swd->read_reg(swd_cmd(true, false, DP_RDBUFF), dap->last_read, 0);
67                 dap->last_read = NULL;
68         }
69 }
70
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);
75
76 static void swd_clear_sticky_errors(struct adiv5_dap *dap)
77 {
78         const struct swd_driver *swd = jtag_interface->swd;
79         assert(swd);
80
81         swd->write_reg(swd_cmd(false,  false, DP_ABORT),
82                 STKCMPCLR | STKERRCLR | WDERRCLR | ORUNERRCLR, 0);
83 }
84
85 static int swd_run_inner(struct adiv5_dap *dap)
86 {
87         const struct swd_driver *swd = jtag_interface->swd;
88         int retval;
89
90         retval = swd->run();
91
92         if (retval != ERROR_OK) {
93                 /* fault response */
94                 dap->do_reconnect = true;
95         }
96
97         return retval;
98 }
99
100 static int swd_connect(struct adiv5_dap *dap)
101 {
102         uint32_t idcode;
103         int status;
104
105         /* FIXME validate transport config ... is the
106          * configured DAP present (check IDCODE)?
107          * Is *only* one DAP configured?
108          *
109          * MUST READ IDCODE
110          */
111
112         /* Note, debugport_init() does setup too */
113         jtag_interface->swd->switch_seq(JTAG_TO_SWD);
114
115         dap->do_reconnect = false;
116
117         swd_queue_dp_read(dap, DP_IDCODE, &idcode);
118
119         /* force clear all sticky faults */
120         swd_clear_sticky_errors(dap);
121
122         status = swd_run_inner(dap);
123
124         if (status == ERROR_OK) {
125                 LOG_INFO("SWD IDCODE %#8.8" PRIx32, idcode);
126                 dap->do_reconnect = false;
127         } else
128                 dap->do_reconnect = true;
129
130         return status;
131 }
132
133 static inline int check_sync(struct adiv5_dap *dap)
134 {
135         return do_sync ? swd_run_inner(dap) : ERROR_OK;
136 }
137
138 static int swd_check_reconnect(struct adiv5_dap *dap)
139 {
140         if (dap->do_reconnect)
141                 return swd_connect(dap);
142
143         return ERROR_OK;
144 }
145
146 static int swd_queue_ap_abort(struct adiv5_dap *dap, uint8_t *ack)
147 {
148         const struct swd_driver *swd = jtag_interface->swd;
149         assert(swd);
150
151         swd->write_reg(swd_cmd(false,  false, DP_ABORT),
152                 DAPABORT | STKCMPCLR | STKERRCLR | WDERRCLR | ORUNERRCLR, 0);
153         return check_sync(dap);
154 }
155
156 /** Select the DP register bank matching bits 7:4 of reg. */
157 static void swd_queue_dp_bankselect(struct adiv5_dap *dap, unsigned reg)
158 {
159         uint32_t select_dp_bank = (reg & 0x000000F0) >> 4;
160
161         if (reg == DP_SELECT)
162                 return;
163
164         if (select_dp_bank == dap->dp_bank_value)
165                 return;
166
167         dap->dp_bank_value = select_dp_bank;
168         select_dp_bank |= dap->ap_current | dap->ap_bank_value;
169
170         swd_queue_dp_write(dap, DP_SELECT, select_dp_bank);
171 }
172
173 static int swd_queue_dp_read(struct adiv5_dap *dap, unsigned reg,
174                 uint32_t *data)
175 {
176         const struct swd_driver *swd = jtag_interface->swd;
177         assert(swd);
178
179         int retval = swd_check_reconnect(dap);
180         if (retval != ERROR_OK)
181                 return retval;
182
183         swd_queue_dp_bankselect(dap, reg);
184         swd->read_reg(swd_cmd(true,  false, reg), data, 0);
185
186         return check_sync(dap);
187 }
188
189 static int swd_queue_dp_write(struct adiv5_dap *dap, unsigned reg,
190                 uint32_t data)
191 {
192         const struct swd_driver *swd = jtag_interface->swd;
193         assert(swd);
194
195         int retval = swd_check_reconnect(dap);
196         if (retval != ERROR_OK)
197                 return retval;
198
199         swd_finish_read(dap);
200         swd_queue_dp_bankselect(dap, reg);
201         swd->write_reg(swd_cmd(false,  false, reg), data, 0);
202
203         return check_sync(dap);
204 }
205
206 /** Select the AP register bank matching bits 7:4 of reg. */
207 static void swd_queue_ap_bankselect(struct adiv5_dap *dap, unsigned reg)
208 {
209         uint32_t select_ap_bank = reg & 0x000000F0;
210
211         if (select_ap_bank == dap->ap_bank_value)
212                 return;
213
214         dap->ap_bank_value = select_ap_bank;
215         select_ap_bank |= dap->ap_current | dap->dp_bank_value;
216
217         swd_queue_dp_write(dap, DP_SELECT, select_ap_bank);
218 }
219
220 static int swd_queue_ap_read(struct adiv5_dap *dap, unsigned reg,
221                 uint32_t *data)
222 {
223         const struct swd_driver *swd = jtag_interface->swd;
224         assert(swd);
225
226         int retval = swd_check_reconnect(dap);
227         if (retval != ERROR_OK)
228                 return retval;
229
230         swd_queue_ap_bankselect(dap, reg);
231         swd->read_reg(swd_cmd(true,  true, reg), dap->last_read, dap->ap[dap_ap_get_select(dap)].memaccess_tck);
232         dap->last_read = data;
233
234         return check_sync(dap);
235 }
236
237 static int swd_queue_ap_write(struct adiv5_dap *dap, unsigned reg,
238                 uint32_t data)
239 {
240         const struct swd_driver *swd = jtag_interface->swd;
241         assert(swd);
242
243         int retval = swd_check_reconnect(dap);
244         if (retval != ERROR_OK)
245                 return retval;
246
247         swd_finish_read(dap);
248         swd_queue_ap_bankselect(dap, reg);
249         swd->write_reg(swd_cmd(false,  true, reg), data, dap->ap[dap_ap_get_select(dap)].memaccess_tck);
250
251         return check_sync(dap);
252 }
253
254 /** Executes all queued DAP operations. */
255 static int swd_run(struct adiv5_dap *dap)
256 {
257         swd_finish_read(dap);
258         return swd_run_inner(dap);
259 }
260
261 const struct dap_ops swd_dap_ops = {
262         .queue_dp_read = swd_queue_dp_read,
263         .queue_dp_write = swd_queue_dp_write,
264         .queue_ap_read = swd_queue_ap_read,
265         .queue_ap_write = swd_queue_ap_write,
266         .queue_ap_abort = swd_queue_ap_abort,
267         .run = swd_run,
268 };
269
270 /*
271  * This represents the bits which must be sent out on TMS/SWDIO to
272  * switch a DAP implemented using an SWJ-DP module into SWD mode.
273  * These bits are stored (and transmitted) LSB-first.
274  *
275  * See the DAP-Lite specification, section 2.2.5 for information
276  * about making the debug link select SWD or JTAG.  (Similar info
277  * is in a few other ARM documents.)
278  */
279 static const uint8_t jtag2swd_bitseq[] = {
280         /* More than 50 TCK/SWCLK cycles with TMS/SWDIO high,
281          * putting both JTAG and SWD logic into reset state.
282          */
283         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
284         /* Switching sequence enables SWD and disables JTAG
285          * NOTE: bits in the DP's IDCODE may expose the need for
286          * an old/obsolete/deprecated sequence (0xb6 0xed).
287          */
288         0x9e, 0xe7,
289         /* More than 50 TCK/SWCLK cycles with TMS/SWDIO high,
290          * putting both JTAG and SWD logic into reset state.
291          */
292         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
293 };
294
295 /**
296  * Put the debug link into SWD mode, if the target supports it.
297  * The link's initial mode may be either JTAG (for example,
298  * with SWJ-DP after reset) or SWD.
299  *
300  * @param target Enters SWD mode (if possible).
301  *
302  * Note that targets using the JTAG-DP do not support SWD, and that
303  * some targets which could otherwise support it may have have been
304  * configured to disable SWD signaling
305  *
306  * @return ERROR_OK or else a fault code.
307  */
308 int dap_to_swd(struct target *target)
309 {
310         struct arm *arm = target_to_arm(target);
311         int retval;
312
313         if (!arm->dap) {
314                 LOG_ERROR("SWD mode is not available");
315                 return ERROR_FAIL;
316         }
317
318         LOG_DEBUG("Enter SWD mode");
319
320         /* REVISIT it's ugly to need to make calls to a "jtag"
321          * subsystem if the link may not be in JTAG mode...
322          */
323
324         retval =  jtag_add_tms_seq(8 * sizeof(jtag2swd_bitseq),
325                         jtag2swd_bitseq, TAP_INVALID);
326         if (retval == ERROR_OK)
327                 retval = jtag_execute_queue();
328
329         /* set up the DAP's ops vector for SWD mode. */
330         arm->dap->ops = &swd_dap_ops;
331
332         return retval;
333 }
334
335 COMMAND_HANDLER(handle_swd_wcr)
336 {
337         int retval;
338         struct target *target = get_current_target(CMD_CTX);
339         struct arm *arm = target_to_arm(target);
340         struct adiv5_dap *dap = arm->dap;
341         uint32_t wcr;
342         unsigned trn, scale = 0;
343
344         switch (CMD_ARGC) {
345         /* no-args: just dump state */
346         case 0:
347                 /*retval = swd_queue_dp_read(dap, DP_WCR, &wcr); */
348                 retval = dap_queue_dp_read(dap, DP_WCR, &wcr);
349                 if (retval == ERROR_OK)
350                         dap->ops->run(dap);
351                 if (retval != ERROR_OK) {
352                         LOG_ERROR("can't read WCR?");
353                         return retval;
354                 }
355
356                 command_print(CMD_CTX,
357                         "turnaround=%" PRIu32 ", prescale=%" PRIu32,
358                         WCR_TO_TRN(wcr),
359                         WCR_TO_PRESCALE(wcr));
360         return ERROR_OK;
361
362         case 2:         /* TRN and prescale */
363                 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], scale);
364                 if (scale > 7) {
365                         LOG_ERROR("prescale %d is too big", scale);
366                         return ERROR_FAIL;
367                 }
368                 /* FALL THROUGH */
369
370         case 1:         /* TRN only */
371                 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], trn);
372                 if (trn < 1 || trn > 4) {
373                         LOG_ERROR("turnaround %d is invalid", trn);
374                         return ERROR_FAIL;
375                 }
376
377                 wcr = ((trn - 1) << 8) | scale;
378                 /* FIXME
379                  * write WCR ...
380                  * then, re-init adapter with new TRN
381                  */
382                 LOG_ERROR("can't yet modify WCR");
383                 return ERROR_FAIL;
384
385         default:        /* too many arguments */
386                 return ERROR_COMMAND_SYNTAX_ERROR;
387         }
388 }
389
390 static const struct command_registration swd_commands[] = {
391         {
392                 /*
393                  * Set up SWD and JTAG targets identically, unless/until
394                  * infrastructure improves ...  meanwhile, ignore all
395                  * JTAG-specific stuff like IR length for SWD.
396                  *
397                  * REVISIT can we verify "just one SWD DAP" here/early?
398                  */
399                 .name = "newdap",
400                 .jim_handler = jim_jtag_newtap,
401                 .mode = COMMAND_CONFIG,
402                 .help = "declare a new SWD DAP"
403         },
404         {
405                 .name = "wcr",
406                 .handler = handle_swd_wcr,
407                 .mode = COMMAND_ANY,
408                 .help = "display or update DAP's WCR register",
409                 .usage = "turnaround (1..4), prescale (0..7)",
410         },
411
412         /* REVISIT -- add a command for SWV trace on/off */
413         COMMAND_REGISTRATION_DONE
414 };
415
416 static const struct command_registration swd_handlers[] = {
417         {
418                 .name = "swd",
419                 .mode = COMMAND_ANY,
420                 .help = "SWD command group",
421                 .chain = swd_commands,
422         },
423         COMMAND_REGISTRATION_DONE
424 };
425
426 static int swd_select(struct command_context *ctx)
427 {
428         int retval;
429
430         retval = register_commands(ctx, NULL, swd_handlers);
431
432         if (retval != ERROR_OK)
433                 return retval;
434
435         const struct swd_driver *swd = jtag_interface->swd;
436
437          /* be sure driver is in SWD mode; start
438           * with hardware default TRN (1), it can be changed later
439           */
440         if (!swd || !swd->read_reg || !swd->write_reg || !swd->init) {
441                 LOG_DEBUG("no SWD driver?");
442                 return ERROR_FAIL;
443         }
444
445         retval = swd->init();
446         if (retval != ERROR_OK) {
447                 LOG_DEBUG("can't init SWD driver");
448                 return retval;
449         }
450
451         /* force DAP into SWD mode (not JTAG) */
452         /*retval = dap_to_swd(target);*/
453
454         if (ctx->current_target) {
455                 /* force DAP into SWD mode (not JTAG) */
456                 struct target *target = get_current_target(ctx);
457                 retval = dap_to_swd(target);
458         }
459
460         return retval;
461 }
462
463 static int swd_init(struct command_context *ctx)
464 {
465         struct target *target = get_current_target(ctx);
466         struct arm *arm = target_to_arm(target);
467         struct adiv5_dap *dap = arm->dap;
468         /* Force the DAP's ops vector for SWD mode.
469          * messy - is there a better way? */
470         arm->dap->ops = &swd_dap_ops;
471
472         return swd_connect(dap);
473 }
474
475 static struct transport swd_transport = {
476         .name = "swd",
477         .select = swd_select,
478         .init = swd_init,
479 };
480
481 static void swd_constructor(void) __attribute__((constructor));
482 static void swd_constructor(void)
483 {
484         transport_register(&swd_transport);
485 }
486
487 /** Returns true if the current debug session
488  * is using SWD as its transport.
489  */
490 bool transport_is_swd(void)
491 {
492         return get_current_transport() == &swd_transport;
493 }