]> git.sur5r.net Git - openocd/blob - src/target/adi_v5_swd.c
39f38572c541d07c3866a96ad34802289d65b681
[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  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 <jtag/transport.h>
54 #include <jtag/interface.h>
55
56 #include <jtag/swd.h>
57
58
59
60 static int swd_queue_dp_read(struct adiv5_dap *dap, unsigned reg,
61                 uint32_t *data)
62 {
63         // REVISIT status return vs ack ...
64         return swd->read_reg(swd_cmd(true,  false, reg), data);
65 }
66
67 static int swd_queue_idcode_read(struct adiv5_dap *dap,
68                 uint8_t *ack, uint32_t *data)
69 {
70         int status = swd_queue_dp_read(dap, DP_IDCODE, data);
71         if (status < 0)
72                 return status;
73         *ack = status;
74         // ??
75         return ERROR_OK;
76 }
77
78 static int (swd_queue_dp_write)(struct adiv5_dap *dap, unsigned reg,
79                 uint32_t data)
80 {
81         // REVISIT status return vs ack ...
82         return swd->write_reg(swd_cmd(false,  false, reg), data);
83 }
84
85
86 static int (swd_queue_ap_read)(struct adiv5_dap *dap, unsigned reg,
87                 uint32_t *data)
88 {
89         // REVISIT  APSEL ...
90         // REVISIT status return ...
91         return swd->read_reg(swd_cmd(true,  true, reg), data);
92 }
93
94 static int (swd_queue_ap_write)(struct adiv5_dap *dap, unsigned reg,
95                 uint32_t data)
96 {
97         // REVISIT  APSEL ...
98         // REVISIT status return ...
99         return swd->write_reg(swd_cmd(false,  true, reg), data);
100 }
101
102 static int (swd_queue_ap_abort)(struct adiv5_dap *dap, uint8_t *ack)
103 {
104         return ERROR_FAIL;
105 }
106
107 /** Executes all queued DAP operations. */
108 static int swd_run(struct adiv5_dap *dap)
109 {
110         /* for now the SWD interface hard-wires a zero-size queue.  */
111
112         /* FIXME but we still need to check and scrub
113          * any hardware errors ...
114          */
115         return ERROR_OK;
116 }
117
118 const struct dap_ops swd_dap_ops = {
119         .is_swd = true,
120
121         .queue_idcode_read = swd_queue_idcode_read,
122         .queue_dp_read = swd_queue_dp_read,
123         .queue_dp_write = swd_queue_dp_write,
124         .queue_ap_read = swd_queue_ap_read,
125         .queue_ap_write = swd_queue_ap_write,
126         .queue_ap_abort = swd_queue_ap_abort,
127         .run = swd_run,
128 };
129
130 /*
131  * This represents the bits which must be sent out on TMS/SWDIO to
132  * switch a DAP implemented using an SWJ-DP module into SWD mode.
133  * These bits are stored (and transmitted) LSB-first.
134  *
135  * See the DAP-Lite specification, section 2.2.5 for information
136  * about making the debug link select SWD or JTAG.  (Similar info
137  * is in a few other ARM documents.)
138  */
139 static const uint8_t jtag2swd_bitseq[] = {
140         /* More than 50 TCK/SWCLK cycles with TMS/SWDIO high,
141          * putting both JTAG and SWD logic into reset state.
142          */
143         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
144         /* Switching sequence enables SWD and disables JTAG
145          * NOTE: bits in the DP's IDCODE may expose the need for
146          * an old/obsolete/deprecated sequence (0xb6 0xed).
147          */
148         0x9e, 0xe7,
149         /* More than 50 TCK/SWCLK cycles with TMS/SWDIO high,
150          * putting both JTAG and SWD logic into reset state.
151          */
152         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
153 };
154
155 /**
156  * Put the debug link into SWD mode, if the target supports it.
157  * The link's initial mode may be either JTAG (for example,
158  * with SWJ-DP after reset) or SWD.
159  *
160  * @param target Enters SWD mode (if possible).
161  *
162  * Note that targets using the JTAG-DP do not support SWD, and that
163  * some targets which could otherwise support it may have have been
164  * configured to disable SWD signaling
165  *
166  * @return ERROR_OK or else a fault code.
167  */
168 int dap_to_swd(struct target *target)
169 {
170         struct arm *arm = target_to_arm(target);
171         int retval;
172
173         LOG_DEBUG("Enter SWD mode");
174
175         /* REVISIT it's ugly to need to make calls to a "jtag"
176          * subsystem if the link may not be in JTAG mode...
177          */
178
179         retval =  jtag_add_tms_seq(8 * sizeof(jtag2swd_bitseq),
180                         jtag2swd_bitseq, TAP_INVALID);
181         if (retval == ERROR_OK)
182                 retval = jtag_execute_queue();
183
184         /* set up the DAP's ops vector for SWD mode. */
185         arm->dap->ops = &swd_dap_ops;
186
187         return retval;
188 }
189
190
191
192 COMMAND_HANDLER(handle_swd_wcr)
193 {
194         int retval;
195         struct target *target = get_current_target(CMD_CTX);
196         struct arm *arm = target_to_arm(target);
197 struct adiv5_dap *dap = arm->dap;
198         uint32_t wcr;
199         unsigned trn, scale = 0;
200
201
202         switch (CMD_ARGC) {
203         /* no-args: just dump state */
204         case 0:
205                 //retval = swd_queue_dp_read(dap, DP_WCR, &wcr);
206                 retval = dap_queue_dp_read(dap, DP_WCR, &wcr);
207                 if (retval == ERROR_OK)
208                         dap->ops->run(dap);
209                 if (retval != ERROR_OK) {
210                         LOG_ERROR("can't read WCR?");
211                         return retval;
212                 }
213
214                 command_print(CMD_CTX,
215                         "turnaround=%d, prescale=%d",
216                         WCR_TO_TRN(wcr),
217                         WCR_TO_PRESCALE(wcr));
218         return ERROR_OK;
219
220         case 2:         /* TRN and prescale */
221                 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], scale);
222                 if (scale > 7) {
223                         LOG_ERROR("prescale %d is too big", scale);
224                         return ERROR_FAIL;
225                 }
226                 /* FALL THROUGH */
227
228         case 1:         /* TRN only */
229                 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], trn);
230                 if (trn < 1 || trn > 4) {
231                         LOG_ERROR("turnaround %d is invalid", trn);
232                         return ERROR_FAIL;
233                 }
234
235                 wcr = ((trn - 1) << 8) | scale;
236                 /* FIXME
237                  * write WCR ...
238                  * then, re-init adapter with new TRN
239                  */
240                 LOG_ERROR("can't yet modify WCR");
241                 return ERROR_FAIL;
242
243         default:        /* too many arguments */
244                 return ERROR_COMMAND_SYNTAX_ERROR;
245         }
246 }
247
248 static const struct command_registration swd_commands[] = {
249         {
250                 /*
251                  * Set up SWD and JTAG targets identically, unless/until
252                  * infrastructure improves ...  meanwhile, ignore all
253                  * JTAG-specific stuff like IR length for SWD.
254                  *
255                  * REVISIT can we verify "just one SWD DAP" here/early?
256                  */
257                 .name = "newdap",
258                 .jim_handler = jim_jtag_newtap,
259                 .mode = COMMAND_CONFIG,
260                 .help = "declare a new SWD DAP"
261         },
262         {
263                 .name = "wcr",
264                 .handler = handle_swd_wcr,
265                 .mode = COMMAND_ANY,
266                 .help = "display or update DAP's WCR register",
267                 .usage = "turnaround (1..4), prescale (0..7)",
268         },
269
270         /* REVISIT -- add a command for SWV trace on/off */
271         COMMAND_REGISTRATION_DONE
272 };
273
274 static const struct command_registration swd_handlers[] = {
275         {
276                 .name = "swd",
277                 .mode = COMMAND_ANY,
278                 .help = "SWD command group",
279                 .chain = swd_commands,
280         },
281         COMMAND_REGISTRATION_DONE
282 };
283
284 static int swd_select(struct command_context *ctx)
285 {
286         struct target *target = get_current_target(ctx);
287         int retval;
288
289         retval = register_commands(ctx, NULL, swd_handlers);
290
291         if (retval != ERROR_OK)
292                 return retval;
293
294          /* be sure driver is in SWD mode; start
295           * with hardware default TRN (1), it can be changed later
296           */
297         if (!swd || !swd->read_reg || !swd->write_reg || !swd->init) {
298                 LOG_DEBUG("no SWD driver?");
299                 return ERROR_FAIL;
300         }
301
302          retval = swd->init(1);
303         if (retval != ERROR_OK) {
304                 LOG_DEBUG("can't init SWD driver");
305                 return retval;
306         }
307
308         /* force DAP into SWD mode (not JTAG) */
309         retval = dap_to_swd(target);
310
311         return retval;
312 }
313
314 static int swd_init(struct command_context *ctx)
315 {
316         struct target *target = get_current_target(ctx);
317         struct arm *arm = target_to_arm(target);
318 struct adiv5_dap *dap = arm->dap;
319         uint32_t idcode;
320         int status;
321
322
323         /* FIXME validate transport config ... is the
324          * configured DAP present (check IDCODE)?
325          * Is *only* one DAP configured?
326          *
327          * MUST READ IDCODE
328          */
329
330  /* Note, debugport_init() does setup too */
331
332         uint8_t ack;
333
334         status = swd_queue_idcode_read(dap, &ack, &idcode);
335
336         if (status == ERROR_OK)
337                 LOG_INFO("SWD IDCODE %#8.8x", idcode);
338
339         return status;
340
341 }
342
343 static struct transport swd_transport = {
344         .name = "swd",
345         .select = swd_select,
346         .init = swd_init,
347 };
348
349 static void swd_constructor(void) __attribute__((constructor));
350 static void swd_constructor(void)
351 {
352         transport_register(&swd_transport);
353 }
354
355 /** Returns true if the current debug session
356  * is using SWD as its transport.
357  */
358 bool transport_is_swd(void)
359 {
360         return get_current_transport() == &swd_transport;
361 }