1 /***************************************************************************
2 * Copyright (C) 2013 by mike brown *
3 * mike@theshedworks.org.uk *
5 * Copyright (C) 2013 by Spencer Oliver *
6 * spen@spen-soft.co.uk *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
22 ***************************************************************************/
26 * Utilities to support ARM "CMSIS-DAP", The CoreSight Debug Access Port.
27 * This is coupled to recent versions of ARM's "CoreSight" debug framework.
28 * This specific code is a transport level interface, with
29 * "target/arm_adi_v5.[hc]" code understanding operation semantics,
30 * shared with the SWD & JTAG transports.
38 #include "arm_adi_v5.h"
39 #include <helper/time_support.h>
41 #include <transport/transport.h>
42 #include <jtag/interface.h>
46 #define CMSIS_CMD_DP (0 << 0) /* set only for AP access */
47 #define CMSIS_CMD_AP (1 << 0) /* set only for AP access */
48 #define CMSIS_CMD_READ (1 << 1) /* set only for read access */
49 #define CMSIS_CMD_WRITE (0 << 1) /* set only for read access */
50 #define CMSIS_CMD_A32(n) ((n)&0x0C) /* bits A[3:2] of register addr */
51 #define CMSIS_CMD_VAL_MATCH (1 << 4) /* Value Match */
52 #define CMSIS_CMD_MATCH_MSK (1 << 5) /* Match Mask */
54 /* YUK! - but this is currently a global.... */
55 extern struct jtag_interface
*jtag_interface
;
57 static int (cmsis_dap_queue_ap_abort
)(struct adiv5_dap
*dap
, uint8_t *ack
)
59 LOG_DEBUG("CMSIS-ADI: cmsis_dap_queue_ap_abort");
61 /* FIXME: implement this properly cmsis-dap has DAP_WriteABORT()
62 * for now just hack @ everything */
63 return jtag_interface
->swd
->write_reg(
64 (CMSIS_CMD_DP
| CMSIS_CMD_WRITE
| CMSIS_CMD_A32(DP_ABORT
)), 0x1e);
67 static int cmsis_dap_queue_dp_read(struct adiv5_dap
*dap
, unsigned reg
, uint32_t *data
)
69 LOG_DEBUG("CMSIS-ADI: cmsis_dap_queue_dp_read %d", reg
);
71 int retval
= jtag_interface
->swd
->read_reg(
72 (CMSIS_CMD_DP
| CMSIS_CMD_READ
| CMSIS_CMD_A32(reg
)), data
);
74 if (retval
!= ERROR_OK
) {
76 uint8_t ack
= retval
& 0xff;
77 cmsis_dap_queue_ap_abort(dap
, &ack
);
83 static int cmsis_dap_queue_idcode_read(struct adiv5_dap
*dap
, uint8_t *ack
, uint32_t *data
)
85 LOG_DEBUG("CMSIS-ADI: cmsis_dap_queue_idcode_read");
87 int retval
= cmsis_dap_queue_dp_read(dap
, DP_IDCODE
, data
);
88 if (retval
!= ERROR_OK
)
96 static int (cmsis_dap_queue_dp_write
)(struct adiv5_dap
*dap
, unsigned reg
, uint32_t data
)
98 LOG_DEBUG("CMSIS-ADI: cmsis_dap_queue_dp_write %d 0x%08" PRIx32
, reg
, data
);
100 /* setting the ORUNDETECT bit causes issues for some targets,
101 * disable until we find out why */
102 if (reg
== DP_CTRL_STAT
) {
103 LOG_DEBUG("disabling overrun detection");
104 data
&= ~CORUNDETECT
;
107 int retval
= jtag_interface
->swd
->write_reg(
108 (CMSIS_CMD_DP
| CMSIS_CMD_WRITE
| CMSIS_CMD_A32(reg
)), data
);
110 if (retval
!= ERROR_OK
) {
112 uint8_t ack
= retval
& 0xff;
113 cmsis_dap_queue_ap_abort(dap
, &ack
);
119 /** Select the AP register bank matching bits 7:4 of reg. */
120 static int cmsis_dap_ap_q_bankselect(struct adiv5_dap
*dap
, unsigned reg
)
122 uint32_t select_ap_bank
= reg
& 0x000000F0;
124 if (select_ap_bank
== dap
->ap_bank_value
)
127 dap
->ap_bank_value
= select_ap_bank
;
128 select_ap_bank
|= dap
->ap_current
;
130 return cmsis_dap_queue_dp_write(dap
, DP_SELECT
, select_ap_bank
);
133 static int (cmsis_dap_queue_ap_read
)(struct adiv5_dap
*dap
, unsigned reg
, uint32_t *data
)
135 int retval
= cmsis_dap_ap_q_bankselect(dap
, reg
);
136 if (retval
!= ERROR_OK
)
139 LOG_DEBUG("CMSIS-ADI: cmsis_dap_queue_ap_read %d", reg
);
141 retval
= jtag_interface
->swd
->read_reg(
142 (CMSIS_CMD_AP
| CMSIS_CMD_READ
| CMSIS_CMD_A32(reg
)), data
);
144 if (retval
!= ERROR_OK
) {
146 uint8_t ack
= retval
& 0xff;
147 cmsis_dap_queue_ap_abort(dap
, &ack
);
153 static int (cmsis_dap_queue_ap_write
)(struct adiv5_dap
*dap
, unsigned reg
, uint32_t data
)
157 /* TODO: CSW_DBGSWENABLE (bit31) causes issues for some targets
158 * disable until we find out why */
159 if (reg
== AP_REG_CSW
)
160 data
&= ~CSW_DBGSWENABLE
;
162 int retval
= cmsis_dap_ap_q_bankselect(dap
, reg
);
163 if (retval
!= ERROR_OK
)
166 LOG_DEBUG("CMSIS-ADI: cmsis_dap_queue_ap_write %d 0x%08" PRIx32
, reg
, data
);
168 retval
= jtag_interface
->swd
->write_reg(
169 (CMSIS_CMD_AP
| CMSIS_CMD_WRITE
| CMSIS_CMD_A32(reg
)), data
);
171 if (retval
!= ERROR_OK
) {
173 uint8_t ack
= retval
& 0xff;
174 cmsis_dap_queue_ap_abort(dap
, &ack
);
180 static int (cmsis_dap_queue_ap_read_block
)(struct adiv5_dap
*dap
, unsigned reg
,
181 uint32_t blocksize
, uint8_t *buffer
)
183 LOG_DEBUG("CMSIS-ADI: cmsis_dap_queue_ap_read_block 0x%08" PRIx32
, blocksize
);
185 int retval
= jtag_interface
->swd
->read_block(
186 (CMSIS_CMD_AP
| CMSIS_CMD_READ
| CMSIS_CMD_A32(AP_REG_DRW
)),
189 if (retval
!= ERROR_OK
) {
191 uint8_t ack
= retval
& 0xff;
192 cmsis_dap_queue_ap_abort(dap
, &ack
);
198 /** Executes all queued DAP operations. */
199 static int cmsis_dap_run(struct adiv5_dap
*dap
)
201 LOG_DEBUG("CMSIS-ADI: cmsis_dap_run");
202 /* FIXME: for now the CMSIS-DAP interface hard-wires a zero-size queue. */
207 const struct dap_ops cmsis_dap_ops
= {
209 .queue_idcode_read
= cmsis_dap_queue_idcode_read
,
210 .queue_dp_read
= cmsis_dap_queue_dp_read
,
211 .queue_dp_write
= cmsis_dap_queue_dp_write
,
212 .queue_ap_read
= cmsis_dap_queue_ap_read
,
213 .queue_ap_write
= cmsis_dap_queue_ap_write
,
214 .queue_ap_read_block
= cmsis_dap_queue_ap_read_block
,
215 .queue_ap_abort
= cmsis_dap_queue_ap_abort
,
216 .run
= cmsis_dap_run
,
219 static const struct command_registration cmsis_dap_commands
[] = {
222 * Set up SWD and JTAG targets identically, unless/until
223 * infrastructure improves ... meanwhile, ignore all
224 * JTAG-specific stuff like IR length for SWD.
226 * REVISIT can we verify "just one SWD DAP" here/early?
229 .jim_handler
= jim_jtag_newtap
,
230 .mode
= COMMAND_CONFIG
,
231 .help
= "declare a new CMSIS-DAP"
233 COMMAND_REGISTRATION_DONE
236 static const struct command_registration cmsis_dap_handlers
[] = {
240 .help
= "cmsis_dap command group",
241 .chain
= cmsis_dap_commands
,
243 COMMAND_REGISTRATION_DONE
246 static int cmsis_dap_select(struct command_context
*ctx
)
248 LOG_DEBUG("CMSIS-ADI: cmsis_dap_select");
250 int retval
= register_commands(ctx
, NULL
, cmsis_dap_handlers
);
252 if (retval
!= ERROR_OK
)
255 /* FIXME: This needs a real overhaul!! FIXME
256 * be sure driver is in SWD mode; start
257 * with hardware default TRN (1), it can be changed later
258 * we use a bogus 'swd' driver to implement cmsis-dap as it is quite similar */
260 const struct swd_driver
*swd
= jtag_interface
->swd
;
261 if (!swd
|| !swd
->read_reg
|| !swd
->write_reg
|| !swd
->init
) {
262 LOG_ERROR("no SWD driver?");
266 retval
= swd
->init(1);
267 if (retval
!= ERROR_OK
) {
268 LOG_ERROR("unable to init CMSIS-DAP driver");
275 static int cmsis_dap_init(struct command_context
*ctx
)
277 struct target
*target
= get_current_target(ctx
);
278 struct arm
*arm
= target_to_arm(target
);
279 struct adiv5_dap
*dap
= arm
->dap
;
283 LOG_DEBUG("CMSIS-ADI: cmsis_dap_init");
285 /* Force the DAP's ops vector for CMSIS-DAP mode.
286 * messy - is there a better way? */
287 arm
->dap
->ops
= &cmsis_dap_ops
;
289 /* FIXME validate transport config ... is the
290 * configured DAP present (check IDCODE)?
291 * Is *only* one DAP configured?
296 /* Note, debugport_init() does setup too */
299 const struct swd_driver
*swd
= jtag_interface
->swd
;
300 if (!swd
|| !swd
->read_reg
|| !swd
->write_reg
|| !swd
->init
) {
301 LOG_ERROR("no SWD driver?");
305 int retval
= swd
->init(1);
306 if (retval
!= ERROR_OK
) {
307 LOG_ERROR("unable to init CMSIS-DAP driver");
314 status
= cmsis_dap_queue_idcode_read(dap
, &ack
, &idcode
);
316 if (status
== ERROR_OK
)
317 LOG_INFO("IDCODE 0x%08" PRIx32
, idcode
);
319 /* force clear all sticky faults */
320 cmsis_dap_queue_ap_abort(dap
, &ack
);
322 /* this is a workaround to get polling working */
323 jtag_add_reset(0, 0);
328 static struct transport cmsis_dap_transport
= {
330 .select
= cmsis_dap_select
,
331 .init
= cmsis_dap_init
,
334 static void cmsis_dap_constructor(void) __attribute__((constructor
));
335 static void cmsis_dap_constructor(void)
337 transport_register(&cmsis_dap_transport
);
340 /** Returns true if the current debug session
341 * is using CMSIS-DAP as its transport.
343 bool transport_is_cmsis_dap(void)
345 return get_current_transport() == &cmsis_dap_transport
;
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)