OpenOCD
swd.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 /***************************************************************************
4  * Copyright (C) 2009-2010 by David Brownell *
5  ***************************************************************************/
6 
7 #ifndef OPENOCD_JTAG_SWD_H
8 #define OPENOCD_JTAG_SWD_H
9 
10 #include <helper/log.h>
11 #include <target/arm_adi_v5.h>
12 
13 /* Bits in SWD command packets, written from host to target
14  * first bit on the wire is START
15  */
16 #define SWD_CMD_START (1 << 0) /* always set */
17 #define SWD_CMD_APNDP (1 << 1) /* set only for AP access */
18 #define SWD_CMD_RNW (1 << 2) /* set only for read access */
19 #define SWD_CMD_A32 (3 << 3) /* bits A[3:2] of register addr */
20 #define SWD_CMD_PARITY (1 << 5) /* parity of APnDP|RnW|A32 */
21 #define SWD_CMD_STOP (0 << 6) /* always clear for synch SWD */
22 #define SWD_CMD_PARK (1 << 7) /* driven high by host */
23 /* followed by TRN, 3-bits of ACK, TRN */
24 
25 /*
26  * The SWD subsystem error codes
27  */
28 #define ERROR_SWD_FAIL (-400)
29 #define ERROR_SWD_FAULT (-401)
35 static inline uint8_t swd_cmd(bool is_read, bool is_ap, uint8_t regnum)
36 {
37  uint8_t cmd = (is_ap ? SWD_CMD_APNDP : 0)
38  | (is_read ? SWD_CMD_RNW : 0)
39  | ((regnum & 0xc) << 1);
40 
41  /* 8 cmd bits 4:1 may be set */
42  if (parity_u32(cmd))
44 
45  /* driver handles START, STOP, and TRN */
46 
47  return cmd;
48 }
49 
50 /* SWD_ACK_* bits are defined in <target/arm_adi_v5.h> */
51 
58 static inline bool swd_cmd_returns_ack(uint8_t cmd)
59 {
60  uint8_t base_cmd = cmd & (SWD_CMD_APNDP | SWD_CMD_RNW | SWD_CMD_A32);
61 
62  /* DPv2 does not reply to DP_TARGETSEL write cmd */
63  return base_cmd != swd_cmd(false, false, DP_TARGETSEL);
64 }
65 
72 static inline int swd_ack_to_error_code(uint8_t ack)
73 {
74  switch (ack) {
75  case SWD_ACK_OK:
76  return ERROR_OK;
77  case SWD_ACK_WAIT:
78  return ERROR_WAIT;
79  case SWD_ACK_FAULT:
80  return ERROR_SWD_FAULT;
81  default:
82  return ERROR_SWD_FAIL;
83  }
84 }
85 
86 /*
87  * The following sequences are updated to
88  * ARM(tm) Debug Interface v5 Architecture Specification ARM IHI 0031E
89  */
90 
98 static const uint8_t swd_seq_line_reset[] = {
99  /* At least 50 SWCLK cycles with SWDIO high */
100  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
101  /* At least 2 idle (low) cycles */
102  0x00,
103 };
104 static const unsigned swd_seq_line_reset_len = 64;
105 
115 static const uint8_t swd_seq_jtag_to_swd[] = {
116  /* At least 50 TCK/SWCLK cycles with TMS/SWDIO high */
117  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
118  /* Switching sequence from JTAG to SWD */
119  0x9e, 0xe7,
120  /* At least 50 TCK/SWCLK cycles with TMS/SWDIO high */
121  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
122  /* At least 2 idle (low) cycles */
123  0x00,
124 };
125 static const unsigned swd_seq_jtag_to_swd_len = 136;
126 
136 static const uint8_t swd_seq_swd_to_jtag[] = {
137  /* At least 50 TCK/SWCLK cycles with TMS/SWDIO high */
138  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
139  /* Switching sequence from SWD to JTAG */
140  0x3c, 0xe7,
141  /* At least 5 TCK/SWCLK cycles with TMS/SWDIO high */
142  0xff,
143 };
144 static const unsigned swd_seq_swd_to_jtag_len = 80;
145 
153 static const uint8_t swd_seq_swd_to_dormant[] = {
154  /* At least 50 SWCLK cycles with SWDIO high */
155  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
156  /* Switching sequence from SWD to dormant */
157  0xbc, 0xe3,
158 };
159 static const unsigned swd_seq_swd_to_dormant_len = 72;
160 
171 static const uint8_t swd_seq_dormant_to_swd[] = {
172  /* At least 8 SWCLK cycles with SWDIO high */
173  0xff,
174  /* Selection alert sequence */
175  0x92, 0xf3, 0x09, 0x62, 0x95, 0x2d, 0x85, 0x86,
176  0xe9, 0xaf, 0xdd, 0xe3, 0xa2, 0x0e, 0xbc, 0x19,
177  /*
178  * 4 SWCLK cycles with SWDIO low ...
179  * + SWD activation code 0x1a ...
180  * + at least 8 SWCLK cycles with SWDIO high
181  */
182  0xa0, /* ((0x00) & GENMASK(3, 0)) | ((0x1a << 4) & GENMASK(7, 4)) */
183  0xf1, /* ((0x1a >> 4) & GENMASK(3, 0)) | ((0xff << 4) & GENMASK(7, 4)) */
184  0xff,
185  /* At least 50 SWCLK cycles with SWDIO high */
186  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
187  /* At least 2 idle (low) cycles */
188  0x00,
189 };
190 static const unsigned swd_seq_dormant_to_swd_len = 224;
191 
199 static const uint8_t swd_seq_jtag_to_dormant[] = {
200  /* At least 5 TCK cycles with TMS high */
201  0xff,
202  /*
203  * Still one TCK cycle with TMS high followed by 31 bits JTAG-to-DS
204  * select sequence 0xba, 0xbb, 0xbb, 0x33,
205  */
206  0x75, /* ((0xff >> 7) & GENMASK(0, 0)) | ((0xba << 1) & GENMASK(7, 1)) */
207  0x77, /* ((0xba >> 7) & GENMASK(0, 0)) | ((0xbb << 1) & GENMASK(7, 1)) */
208  0x77, /* ((0xbb >> 7) & GENMASK(0, 0)) | ((0xbb << 1) & GENMASK(7, 1)) */
209  0x67, /* ((0xbb >> 7) & GENMASK(0, 0)) | ((0x33 << 1) & GENMASK(7, 1)) */
210 };
211 static const unsigned swd_seq_jtag_to_dormant_len = 40;
212 
230 static const uint8_t swd_seq_dormant_to_jtag[] = {
231  /* At least 8 TCK/SWCLK cycles with TMS/SWDIO high */
232  0xff,
233  /* Selection alert sequence */
234  0x92, 0xf3, 0x09, 0x62, 0x95, 0x2d, 0x85, 0x86,
235  0xe9, 0xaf, 0xdd, 0xe3, 0xa2, 0x0e, 0xbc, 0x19,
236  /*
237  * 4 TCK/SWCLK cycles with TMS/SWDIO low ...
238  * + 12 bits JTAG-serial activation code 0x00, 0x00
239  */
240  0x00, 0x00,
241  /* put the TAP in Run/Test Idle */
242  0x00,
243 };
244 static const unsigned swd_seq_dormant_to_jtag_len = 160;
245 
246 struct swd_driver {
255  int (*init)(void);
256 
264  int (*switch_seq)(enum swd_special_seq seq);
265 
274  void (*read_reg)(uint8_t cmd, uint32_t *value, uint32_t ap_delay_hint);
275 
284  void (*write_reg)(uint8_t cmd, uint32_t value, uint32_t ap_delay_hint);
285 
292  int (*run)(void);
293 
306  int *(*trace)(bool swo);
307 };
308 
309 int swd_init_reset(struct command_context *cmd_ctx);
310 
311 #endif /* OPENOCD_JTAG_SWD_H */
This defines formats and data structures used to talk to ADIv5 entities.
#define SWD_ACK_FAULT
Definition: arm_adi_v5.h:33
swd_special_seq
Definition: arm_adi_v5.h:236
#define DP_TARGETSEL
Definition: arm_adi_v5.h:59
#define SWD_ACK_WAIT
Definition: arm_adi_v5.h:32
#define SWD_ACK_OK
Definition: arm_adi_v5.h:31
#define ERROR_WAIT
Definition: log.h:171
#define ERROR_OK
Definition: log.h:164
int(* switch_seq)(enum swd_special_seq seq)
Queue a special SWDIO sequence.
Definition: swd.h:264
void(* read_reg)(uint8_t cmd, uint32_t *value, uint32_t ap_delay_hint)
Queued read of an AP or DP register.
Definition: swd.h:274
int(* init)(void)
Initialize the debug link so it can perform SWD operations.
Definition: swd.h:255
int(* run)(void)
Execute any queued transactions and collect the result.
Definition: swd.h:292
void(* write_reg)(uint8_t cmd, uint32_t value, uint32_t ap_delay_hint)
Queued write of an AP or DP register.
Definition: swd.h:284
static const unsigned swd_seq_swd_to_jtag_len
Definition: swd.h:144
static const unsigned swd_seq_jtag_to_swd_len
Definition: swd.h:125
static const uint8_t swd_seq_dormant_to_jtag[]
Dormant-to-JTAG sequence.
Definition: swd.h:230
#define SWD_CMD_A32
Definition: swd.h:19
static const uint8_t swd_seq_dormant_to_swd[]
Dormant-to-SWD sequence.
Definition: swd.h:171
static const uint8_t swd_seq_jtag_to_dormant[]
JTAG-to-dormant sequence.
Definition: swd.h:199
static const unsigned swd_seq_dormant_to_swd_len
Definition: swd.h:190
static bool swd_cmd_returns_ack(uint8_t cmd)
Test if we can rely on ACK returned by SWD command.
Definition: swd.h:58
static int swd_ack_to_error_code(uint8_t ack)
Convert SWD ACK value returned from DP to OpenOCD error code.
Definition: swd.h:72
static uint8_t swd_cmd(bool is_read, bool is_ap, uint8_t regnum)
Construct a "cmd" byte, in lSB bit order, which swd_driver.read_reg() and swd_driver....
Definition: swd.h:35
int swd_init_reset(struct command_context *cmd_ctx)
Definition: jtag/core.c:1583
#define ERROR_SWD_FAIL
Definition: swd.h:28
#define SWD_CMD_APNDP
Definition: swd.h:17
static const unsigned swd_seq_jtag_to_dormant_len
Definition: swd.h:211
#define SWD_CMD_RNW
Definition: swd.h:18
#define ERROR_SWD_FAULT
Definition: swd.h:29
static const unsigned swd_seq_dormant_to_jtag_len
Definition: swd.h:244
static const uint8_t swd_seq_line_reset[]
SWD Line reset.
Definition: swd.h:98
static const unsigned swd_seq_line_reset_len
Definition: swd.h:104
#define SWD_CMD_PARITY
Definition: swd.h:20
static const unsigned swd_seq_swd_to_dormant_len
Definition: swd.h:159
static const uint8_t swd_seq_jtag_to_swd[]
JTAG-to-SWD sequence.
Definition: swd.h:115
static const uint8_t swd_seq_swd_to_jtag[]
SWD-to-JTAG sequence.
Definition: swd.h:136
static const uint8_t swd_seq_swd_to_dormant[]
SWD-to-dormant sequence.
Definition: swd.h:153
static int parity_u32(uint32_t x)
Calculate the (even) parity of a 32-bit datum.
Definition: types.h:265
uint8_t cmd
Definition: vdebug.c:1