jlink: Added hardware version number for JLink firmware on LPC-Link2
[openocd.git] / src / target / dsp563xx.c
blob1cb18cf11dcde5963f0f6110084bf4697e5f8e13
1 /***************************************************************************
2 * Copyright (C) 2009-2011 by Mathias Kuester *
3 * mkdorg@users.sourceforge.net *
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 ***************************************************************************/
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
25 #include <jim.h>
27 #include "target.h"
28 #include "breakpoints.h"
29 #include "target_type.h"
30 #include "algorithm.h"
31 #include "register.h"
32 #include "dsp563xx.h"
33 #include "dsp563xx_once.h"
35 #define ASM_REG_W_R0 0x60F400
36 #define ASM_REG_W_R1 0x61F400
37 #define ASM_REG_W_R2 0x62F400
38 #define ASM_REG_W_R3 0x63F400
39 #define ASM_REG_W_R4 0x64F400
40 #define ASM_REG_W_R5 0x65F400
41 #define ASM_REG_W_R6 0x66F400
42 #define ASM_REG_W_R7 0x67F400
44 #define ASM_REG_W_N0 0x70F400
45 #define ASM_REG_W_N1 0x71F400
46 #define ASM_REG_W_N2 0x72F400
47 #define ASM_REG_W_N3 0x73F400
48 #define ASM_REG_W_N4 0x74F400
49 #define ASM_REG_W_N5 0x75F400
50 #define ASM_REG_W_N6 0x76F400
51 #define ASM_REG_W_N7 0x77F400
53 #define ASM_REG_W_M0 0x05F420
54 #define ASM_REG_W_M1 0x05F421
55 #define ASM_REG_W_M2 0x05F422
56 #define ASM_REG_W_M3 0x05F423
57 #define ASM_REG_W_M4 0x05F424
58 #define ASM_REG_W_M5 0x05F425
59 #define ASM_REG_W_M6 0x05F426
60 #define ASM_REG_W_M7 0x05F427
62 #define ASM_REG_W_X0 0x44F400
63 #define ASM_REG_W_X1 0x45F400
65 #define ASM_REG_W_Y0 0x46F400
66 #define ASM_REG_W_Y1 0x47F400
68 #define ASM_REG_W_A0 0x50F400
69 #define ASM_REG_W_A1 0x54F400
70 #define ASM_REG_W_A2 0x52F400
72 #define ASM_REG_W_B0 0x51F400
73 #define ASM_REG_W_B1 0x55F400
74 #define ASM_REG_W_B2 0x53F400
76 #define ASM_REG_W_VBA 0x05F430
77 #define ASM_REG_W_OMR 0x05F43A
78 #define ASM_REG_W_EP 0x05F42A
79 #define ASM_REG_W_SC 0x05F431
80 #define ASM_REG_W_SZ 0x05F438
81 #define ASM_REG_W_SR 0x05F439
82 #define ASM_REG_W_SP 0x05F43B
83 #define ASM_REG_W_SSH 0x05F43C
84 #define ASM_REG_W_SSL 0x05F43D
85 #define ASM_REG_W_LA 0x05F43E
86 #define ASM_REG_W_LC 0x05F43F
87 #define ASM_REG_W_PC 0x000000
88 #define ASM_REG_W_IPRC 0xFFFFFF
89 #define ASM_REG_W_IPRP 0xFFFFFE
91 #define ASM_REG_W_BCR 0xFFFFFB
92 #define ASM_REG_W_DCR 0xFFFFFA
93 #define ASM_REG_W_AAR0 0xFFFFF9
94 #define ASM_REG_W_AAR1 0xFFFFF8
95 #define ASM_REG_W_AAR2 0xFFFFF7
96 #define ASM_REG_W_AAR3 0xFFFFF6
99 * OBCR Register bit definitions
101 #define OBCR_b0_and_b1 ((0x0) << 10)
102 #define OBCR_b0_or_b1 ((0x1) << 10)
103 #define OBCR_b1_after_b0 ((0x2) << 10)
104 #define OBCR_b0_after_b1 ((0x3) << 10)
106 #define OBCR_BP_DISABLED (0x0)
107 #define OBCR_BP_MEM_P (0x1)
108 #define OBCR_BP_MEM_X (0x2)
109 #define OBCR_BP_MEM_Y (0x3)
110 #define OBCR_BP_ON_READ ((0x2) << 0)
111 #define OBCR_BP_ON_WRITE ((0x1) << 0)
112 #define OBCR_BP_CC_NOT_EQUAL ((0x0) << 2)
113 #define OBCR_BP_CC_EQUAL ((0x1) << 2)
114 #define OBCR_BP_CC_LESS_THAN ((0x2) << 2)
115 #define OBCR_BP_CC_GREATER_THAN ((0x3) << 2)
117 #define OBCR_BP_0(x) ((x)<<2)
118 #define OBCR_BP_1(x) ((x)<<6)
121 enum once_reg_idx {
122 ONCE_REG_IDX_OSCR = 0,
123 ONCE_REG_IDX_OMBC = 1,
124 ONCE_REG_IDX_OBCR = 2,
125 ONCE_REG_IDX_OMLR0 = 3,
126 ONCE_REG_IDX_OMLR1 = 4,
127 ONCE_REG_IDX_OGDBR = 5,
128 ONCE_REG_IDX_OPDBR = 6,
129 ONCE_REG_IDX_OPILR = 7,
130 ONCE_REG_IDX_PDB = 8,
131 ONCE_REG_IDX_OTC = 9,
132 ONCE_REG_IDX_OPABFR = 10,
133 ONCE_REG_IDX_OPABDR = 11,
134 ONCE_REG_IDX_OPABEX = 12,
135 ONCE_REG_IDX_OPABF0 = 13,
136 ONCE_REG_IDX_OPABF1 = 14,
137 ONCE_REG_IDX_OPABF2 = 15,
138 ONCE_REG_IDX_OPABF3 = 16,
139 ONCE_REG_IDX_OPABF4 = 17,
140 ONCE_REG_IDX_OPABF5 = 18,
141 ONCE_REG_IDX_OPABF6 = 19,
142 ONCE_REG_IDX_OPABF7 = 20,
143 ONCE_REG_IDX_OPABF8 = 21,
144 ONCE_REG_IDX_OPABF9 = 22,
145 ONCE_REG_IDX_OPABF10 = 23,
146 ONCE_REG_IDX_OPABF11 = 24,
149 static struct once_reg once_regs[] = {
150 {ONCE_REG_IDX_OSCR, DSP563XX_ONCE_OSCR, 24, "OSCR", 0},
151 {ONCE_REG_IDX_OMBC, DSP563XX_ONCE_OMBC, 24, "OMBC", 0},
152 {ONCE_REG_IDX_OBCR, DSP563XX_ONCE_OBCR, 24, "OBCR", 0},
153 {ONCE_REG_IDX_OMLR0, DSP563XX_ONCE_OMLR0, 24, "OMLR0", 0},
154 {ONCE_REG_IDX_OMLR1, DSP563XX_ONCE_OMLR1, 24, "OMLR1", 0},
155 {ONCE_REG_IDX_OGDBR, DSP563XX_ONCE_OGDBR, 24, "OGDBR", 0},
156 {ONCE_REG_IDX_OPDBR, DSP563XX_ONCE_OPDBR, 24, "OPDBR", 0},
157 {ONCE_REG_IDX_OPILR, DSP563XX_ONCE_OPILR, 24, "OPILR", 0},
158 {ONCE_REG_IDX_PDB, DSP563XX_ONCE_PDBGOTO, 24, "PDB", 0},
159 {ONCE_REG_IDX_OTC, DSP563XX_ONCE_OTC, 24, "OTC", 0},
160 {ONCE_REG_IDX_OPABFR, DSP563XX_ONCE_OPABFR, 24, "OPABFR", 0},
161 {ONCE_REG_IDX_OPABDR, DSP563XX_ONCE_OPABDR, 24, "OPABDR", 0},
162 {ONCE_REG_IDX_OPABEX, DSP563XX_ONCE_OPABEX, 24, "OPABEX", 0},
163 {ONCE_REG_IDX_OPABF0, DSP563XX_ONCE_OPABF11, 25, "OPABF0", 0},
164 {ONCE_REG_IDX_OPABF1, DSP563XX_ONCE_OPABF11, 25, "OPABF1", 0},
165 {ONCE_REG_IDX_OPABF2, DSP563XX_ONCE_OPABF11, 25, "OPABF2", 0},
166 {ONCE_REG_IDX_OPABF3, DSP563XX_ONCE_OPABF11, 25, "OPABF3", 0},
167 {ONCE_REG_IDX_OPABF4, DSP563XX_ONCE_OPABF11, 25, "OPABF4", 0},
168 {ONCE_REG_IDX_OPABF5, DSP563XX_ONCE_OPABF11, 25, "OPABF5", 0},
169 {ONCE_REG_IDX_OPABF6, DSP563XX_ONCE_OPABF11, 25, "OPABF6", 0},
170 {ONCE_REG_IDX_OPABF7, DSP563XX_ONCE_OPABF11, 25, "OPABF7", 0},
171 {ONCE_REG_IDX_OPABF8, DSP563XX_ONCE_OPABF11, 25, "OPABF8", 0},
172 {ONCE_REG_IDX_OPABF9, DSP563XX_ONCE_OPABF11, 25, "OPABF9", 0},
173 {ONCE_REG_IDX_OPABF10, DSP563XX_ONCE_OPABF11, 25, "OPABF10", 0},
174 {ONCE_REG_IDX_OPABF11, DSP563XX_ONCE_OPABF11, 25, "OPABF11", 0},
175 /* {25,0x1f,24,"NRSEL",0}, */
178 enum dsp563xx_reg_idx {
179 DSP563XX_REG_IDX_R0 = 0,
180 DSP563XX_REG_IDX_R1 = 1,
181 DSP563XX_REG_IDX_R2 = 2,
182 DSP563XX_REG_IDX_R3 = 3,
183 DSP563XX_REG_IDX_R4 = 4,
184 DSP563XX_REG_IDX_R5 = 5,
185 DSP563XX_REG_IDX_R6 = 6,
186 DSP563XX_REG_IDX_R7 = 7,
187 DSP563XX_REG_IDX_N0 = 8,
188 DSP563XX_REG_IDX_N1 = 9,
189 DSP563XX_REG_IDX_N2 = 10,
190 DSP563XX_REG_IDX_N3 = 11,
191 DSP563XX_REG_IDX_N4 = 12,
192 DSP563XX_REG_IDX_N5 = 13,
193 DSP563XX_REG_IDX_N6 = 14,
194 DSP563XX_REG_IDX_N7 = 15,
195 DSP563XX_REG_IDX_M0 = 16,
196 DSP563XX_REG_IDX_M1 = 17,
197 DSP563XX_REG_IDX_M2 = 18,
198 DSP563XX_REG_IDX_M3 = 19,
199 DSP563XX_REG_IDX_M4 = 20,
200 DSP563XX_REG_IDX_M5 = 21,
201 DSP563XX_REG_IDX_M6 = 22,
202 DSP563XX_REG_IDX_M7 = 23,
203 DSP563XX_REG_IDX_X0 = 24,
204 DSP563XX_REG_IDX_X1 = 25,
205 DSP563XX_REG_IDX_Y0 = 26,
206 DSP563XX_REG_IDX_Y1 = 27,
207 DSP563XX_REG_IDX_A0 = 28,
208 DSP563XX_REG_IDX_A1 = 29,
209 DSP563XX_REG_IDX_A2 = 30,
210 DSP563XX_REG_IDX_B0 = 31,
211 DSP563XX_REG_IDX_B1 = 32,
212 DSP563XX_REG_IDX_B2 = 33,
213 DSP563XX_REG_IDX_SSH = 34,
214 DSP563XX_REG_IDX_SSL = 35,
215 DSP563XX_REG_IDX_SP = 36,
216 DSP563XX_REG_IDX_EP = 37,
217 DSP563XX_REG_IDX_SZ = 38,
218 DSP563XX_REG_IDX_SC = 39,
219 DSP563XX_REG_IDX_PC = 40,
220 DSP563XX_REG_IDX_SR = 41,
221 DSP563XX_REG_IDX_OMR = 42,
222 DSP563XX_REG_IDX_LA = 43,
223 DSP563XX_REG_IDX_LC = 44,
224 DSP563XX_REG_IDX_VBA = 45,
225 DSP563XX_REG_IDX_IPRC = 46,
226 DSP563XX_REG_IDX_IPRP = 47,
227 DSP563XX_REG_IDX_BCR = 48,
228 DSP563XX_REG_IDX_DCR = 49,
229 DSP563XX_REG_IDX_AAR0 = 50,
230 DSP563XX_REG_IDX_AAR1 = 51,
231 DSP563XX_REG_IDX_AAR2 = 52,
232 DSP563XX_REG_IDX_AAR3 = 53,
235 static const struct {
236 unsigned id;
237 const char *name;
238 unsigned bits;
239 /* effective addressing mode encoding */
240 uint8_t eame;
241 uint32_t instr_mask;
242 } dsp563xx_regs[] = {
243 /* *INDENT-OFF* */
244 /* address registers */
245 {DSP563XX_REG_IDX_R0, "r0", 24, 0x10, ASM_REG_W_R0},
246 {DSP563XX_REG_IDX_R1, "r1", 24, 0x11, ASM_REG_W_R1},
247 {DSP563XX_REG_IDX_R2, "r2", 24, 0x12, ASM_REG_W_R2},
248 {DSP563XX_REG_IDX_R3, "r3", 24, 0x13, ASM_REG_W_R3},
249 {DSP563XX_REG_IDX_R4, "r4", 24, 0x14, ASM_REG_W_R4},
250 {DSP563XX_REG_IDX_R5, "r5", 24, 0x15, ASM_REG_W_R5},
251 {DSP563XX_REG_IDX_R6, "r6", 24, 0x16, ASM_REG_W_R6},
252 {DSP563XX_REG_IDX_R7, "r7", 24, 0x17, ASM_REG_W_R7},
253 /* offset registers */
254 {DSP563XX_REG_IDX_N0, "n0", 24, 0x18, ASM_REG_W_N0},
255 {DSP563XX_REG_IDX_N1, "n1", 24, 0x19, ASM_REG_W_N1},
256 {DSP563XX_REG_IDX_N2, "n2", 24, 0x1a, ASM_REG_W_N2},
257 {DSP563XX_REG_IDX_N3, "n3", 24, 0x1b, ASM_REG_W_N3},
258 {DSP563XX_REG_IDX_N4, "n4", 24, 0x1c, ASM_REG_W_N4},
259 {DSP563XX_REG_IDX_N5, "n5", 24, 0x1d, ASM_REG_W_N5},
260 {DSP563XX_REG_IDX_N6, "n6", 24, 0x1e, ASM_REG_W_N6},
261 {DSP563XX_REG_IDX_N7, "n7", 24, 0x1f, ASM_REG_W_N7},
262 /* modifier registers */
263 {DSP563XX_REG_IDX_M0, "m0", 24, 0x20, ASM_REG_W_M0},
264 {DSP563XX_REG_IDX_M1, "m1", 24, 0x21, ASM_REG_W_M1},
265 {DSP563XX_REG_IDX_M2, "m2", 24, 0x22, ASM_REG_W_M2},
266 {DSP563XX_REG_IDX_M3, "m3", 24, 0x23, ASM_REG_W_M3},
267 {DSP563XX_REG_IDX_M4, "m4", 24, 0x24, ASM_REG_W_M4},
268 {DSP563XX_REG_IDX_M5, "m5", 24, 0x25, ASM_REG_W_M5},
269 {DSP563XX_REG_IDX_M6, "m6", 24, 0x26, ASM_REG_W_M6},
270 {DSP563XX_REG_IDX_M7, "m7", 24, 0x27, ASM_REG_W_M7},
271 /* data alu input register */
272 {DSP563XX_REG_IDX_X0, "x0", 24, 0x04, ASM_REG_W_X0},
273 {DSP563XX_REG_IDX_X1, "x1", 24, 0x05, ASM_REG_W_X1},
274 {DSP563XX_REG_IDX_Y0, "y0", 24, 0x06, ASM_REG_W_Y0},
275 {DSP563XX_REG_IDX_Y1, "y1", 24, 0x07, ASM_REG_W_Y1},
276 /* data alu accumulator register */
277 {DSP563XX_REG_IDX_A0, "a0", 24, 0x08, ASM_REG_W_A0},
278 {DSP563XX_REG_IDX_A1, "a1", 24, 0x0c, ASM_REG_W_A1},
279 {DSP563XX_REG_IDX_A2, "a2", 8, 0x0a, ASM_REG_W_A2},
280 {DSP563XX_REG_IDX_B0, "b0", 24, 0x09, ASM_REG_W_B0},
281 {DSP563XX_REG_IDX_B1, "b1", 24, 0x0d, ASM_REG_W_B1},
282 {DSP563XX_REG_IDX_B2, "b2", 8, 0x0b, ASM_REG_W_B2},
283 /* stack */
284 {DSP563XX_REG_IDX_SSH, "ssh", 24, 0x3c, ASM_REG_W_SSH},
285 {DSP563XX_REG_IDX_SSL, "ssl", 24, 0x3d, ASM_REG_W_SSL},
286 {DSP563XX_REG_IDX_SP, "sp", 24, 0x3b, ASM_REG_W_SP},
287 {DSP563XX_REG_IDX_EP, "ep", 24, 0x2a, ASM_REG_W_EP},
288 {DSP563XX_REG_IDX_SZ, "sz", 24, 0x38, ASM_REG_W_SZ},
289 {DSP563XX_REG_IDX_SC, "sc", 24, 0x31, ASM_REG_W_SC},
290 /* system */
291 {DSP563XX_REG_IDX_PC, "pc", 24, 0x00, ASM_REG_W_PC},
292 {DSP563XX_REG_IDX_SR, "sr", 24, 0x39, ASM_REG_W_SR},
293 {DSP563XX_REG_IDX_OMR, "omr", 24, 0x3a, ASM_REG_W_OMR},
294 {DSP563XX_REG_IDX_LA, "la", 24, 0x3e, ASM_REG_W_LA},
295 {DSP563XX_REG_IDX_LC, "lc", 24, 0x3f, ASM_REG_W_LC},
296 /* interrupt */
297 {DSP563XX_REG_IDX_VBA, "vba", 24, 0x30, ASM_REG_W_VBA},
298 {DSP563XX_REG_IDX_IPRC, "iprc", 24, 0x00, ASM_REG_W_IPRC},
299 {DSP563XX_REG_IDX_IPRP, "iprp", 24, 0x00, ASM_REG_W_IPRP},
300 /* port a */
301 {DSP563XX_REG_IDX_BCR, "bcr", 24, 0x00, ASM_REG_W_BCR},
302 {DSP563XX_REG_IDX_DCR, "dcr", 24, 0x00, ASM_REG_W_DCR},
303 {DSP563XX_REG_IDX_AAR0, "aar0", 24, 0x00, ASM_REG_W_AAR0},
304 {DSP563XX_REG_IDX_AAR1, "aar1", 24, 0x00, ASM_REG_W_AAR1},
305 {DSP563XX_REG_IDX_AAR2, "aar2", 24, 0x00, ASM_REG_W_AAR2},
306 {DSP563XX_REG_IDX_AAR3, "aar3", 24, 0x00, ASM_REG_W_AAR3},
307 /* *INDENT-ON* */
310 enum memory_type {
311 MEM_X = 0,
312 MEM_Y = 1,
313 MEM_P = 2,
314 MEM_L = 3,
317 enum watchpoint_condition {
318 EQUAL,
319 NOT_EQUAL,
320 GREATER,
321 LESS_THAN
324 #define INSTR_JUMP 0x0AF080
325 /* Effective Addressing Mode Encoding */
326 #define EAME_R0 0x10
327 /* instrcution encoder */
328 /* movep
329 * s - peripheral space X/Y (X=0,Y=1)
330 * w - write/read
331 * d - source/destination register
332 * p - IO short address
334 #define INSTR_MOVEP_REG_HIO(s, w, d, p) (0x084000 | \
335 ((s & 1) << 16) | ((w & 1) << 15) | ((d & 0x3f) << 8) | (p & 0x3f))
337 /* the gdb register list is send in this order */
338 static const uint8_t gdb_reg_list_idx[] = {
339 DSP563XX_REG_IDX_X1, DSP563XX_REG_IDX_X0, DSP563XX_REG_IDX_Y1, DSP563XX_REG_IDX_Y0,
340 DSP563XX_REG_IDX_A2, DSP563XX_REG_IDX_A1, DSP563XX_REG_IDX_A0, DSP563XX_REG_IDX_B2,
341 DSP563XX_REG_IDX_B1, DSP563XX_REG_IDX_B0, DSP563XX_REG_IDX_PC, DSP563XX_REG_IDX_SR,
342 DSP563XX_REG_IDX_OMR, DSP563XX_REG_IDX_LA, DSP563XX_REG_IDX_LC, DSP563XX_REG_IDX_SSH,
343 DSP563XX_REG_IDX_SSL, DSP563XX_REG_IDX_SP, DSP563XX_REG_IDX_EP, DSP563XX_REG_IDX_SZ,
344 DSP563XX_REG_IDX_SC, DSP563XX_REG_IDX_VBA, DSP563XX_REG_IDX_IPRC, DSP563XX_REG_IDX_IPRP,
345 DSP563XX_REG_IDX_BCR, DSP563XX_REG_IDX_DCR, DSP563XX_REG_IDX_AAR0, DSP563XX_REG_IDX_AAR1,
346 DSP563XX_REG_IDX_AAR2, DSP563XX_REG_IDX_AAR3, DSP563XX_REG_IDX_R0, DSP563XX_REG_IDX_R1,
347 DSP563XX_REG_IDX_R2, DSP563XX_REG_IDX_R3, DSP563XX_REG_IDX_R4, DSP563XX_REG_IDX_R5,
348 DSP563XX_REG_IDX_R6, DSP563XX_REG_IDX_R7, DSP563XX_REG_IDX_N0, DSP563XX_REG_IDX_N1,
349 DSP563XX_REG_IDX_N2, DSP563XX_REG_IDX_N3, DSP563XX_REG_IDX_N4, DSP563XX_REG_IDX_N5,
350 DSP563XX_REG_IDX_N6, DSP563XX_REG_IDX_N7, DSP563XX_REG_IDX_M0, DSP563XX_REG_IDX_M1,
351 DSP563XX_REG_IDX_M2, DSP563XX_REG_IDX_M3, DSP563XX_REG_IDX_M4, DSP563XX_REG_IDX_M5,
352 DSP563XX_REG_IDX_M6, DSP563XX_REG_IDX_M7,
355 static int dsp563xx_get_gdb_reg_list(struct target *target,
356 struct reg **reg_list[],
357 int *reg_list_size,
358 enum target_register_class reg_class)
360 int i;
361 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
363 if (target->state != TARGET_HALTED)
364 return ERROR_TARGET_NOT_HALTED;
366 *reg_list_size = DSP563XX_NUMCOREREGS;
367 *reg_list = malloc(sizeof(struct reg *) * (*reg_list_size));
369 if (!*reg_list)
370 return ERROR_COMMAND_SYNTAX_ERROR;
372 for (i = 0; i < DSP563XX_NUMCOREREGS; i++)
373 (*reg_list)[i] = &dsp563xx->core_cache->reg_list[gdb_reg_list_idx[i]];
375 return ERROR_OK;
379 static int dsp563xx_read_core_reg(struct target *target, int num)
381 uint32_t reg_value;
382 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
384 if ((num < 0) || (num >= DSP563XX_NUMCOREREGS))
385 return ERROR_COMMAND_SYNTAX_ERROR;
387 reg_value = dsp563xx->core_regs[num];
388 buf_set_u32(dsp563xx->core_cache->reg_list[num].value, 0, 32, reg_value);
389 dsp563xx->core_cache->reg_list[num].valid = 1;
390 dsp563xx->core_cache->reg_list[num].dirty = 0;
392 return ERROR_OK;
395 static int dsp563xx_write_core_reg(struct target *target, int num)
397 uint32_t reg_value;
398 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
400 if ((num < 0) || (num >= DSP563XX_NUMCOREREGS))
401 return ERROR_COMMAND_SYNTAX_ERROR;
403 reg_value = buf_get_u32(dsp563xx->core_cache->reg_list[num].value, 0, 32);
404 dsp563xx->core_regs[num] = reg_value;
405 dsp563xx->core_cache->reg_list[num].valid = 1;
406 dsp563xx->core_cache->reg_list[num].dirty = 0;
408 return ERROR_OK;
411 static int dsp563xx_get_core_reg(struct reg *reg)
413 struct dsp563xx_core_reg *dsp563xx_reg = reg->arch_info;
414 struct target *target = dsp563xx_reg->target;
415 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
417 LOG_DEBUG("%s", __func__);
419 if (target->state != TARGET_HALTED)
420 return ERROR_TARGET_NOT_HALTED;
422 return dsp563xx->read_core_reg(target, dsp563xx_reg->num);
425 static int dsp563xx_set_core_reg(struct reg *reg, uint8_t *buf)
427 LOG_DEBUG("%s", __func__);
429 struct dsp563xx_core_reg *dsp563xx_reg = reg->arch_info;
430 struct target *target = dsp563xx_reg->target;
431 uint32_t value = buf_get_u32(buf, 0, 32);
433 if (target->state != TARGET_HALTED)
434 return ERROR_TARGET_NOT_HALTED;
436 buf_set_u32(reg->value, 0, reg->size, value);
437 reg->dirty = 1;
438 reg->valid = 1;
440 return ERROR_OK;
443 static const struct reg_arch_type dsp563xx_reg_type = {
444 .get = dsp563xx_get_core_reg,
445 .set = dsp563xx_set_core_reg,
448 static void dsp563xx_build_reg_cache(struct target *target)
450 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
452 struct reg_cache **cache_p = register_get_last_cache_p(&target->reg_cache);
453 struct reg_cache *cache = malloc(sizeof(struct reg_cache));
454 struct reg *reg_list = calloc(DSP563XX_NUMCOREREGS, sizeof(struct reg));
455 struct dsp563xx_core_reg *arch_info = malloc(
456 sizeof(struct dsp563xx_core_reg) * DSP563XX_NUMCOREREGS);
457 int i;
459 /* Build the process context cache */
460 cache->name = "dsp563xx registers";
461 cache->next = NULL;
462 cache->reg_list = reg_list;
463 cache->num_regs = DSP563XX_NUMCOREREGS;
464 (*cache_p) = cache;
465 dsp563xx->core_cache = cache;
467 for (i = 0; i < DSP563XX_NUMCOREREGS; i++) {
468 arch_info[i].num = dsp563xx_regs[i].id;
469 arch_info[i].name = dsp563xx_regs[i].name;
470 arch_info[i].size = dsp563xx_regs[i].bits;
471 arch_info[i].eame = dsp563xx_regs[i].eame;
472 arch_info[i].instr_mask = dsp563xx_regs[i].instr_mask;
473 arch_info[i].target = target;
474 arch_info[i].dsp563xx_common = dsp563xx;
475 reg_list[i].name = dsp563xx_regs[i].name;
476 reg_list[i].size = 32; /* dsp563xx_regs[i].bits; */
477 reg_list[i].value = calloc(1, 4);
478 reg_list[i].dirty = 0;
479 reg_list[i].valid = 0;
480 reg_list[i].type = &dsp563xx_reg_type;
481 reg_list[i].arch_info = &arch_info[i];
485 static int dsp563xx_read_register(struct target *target, int num, int force);
486 static int dsp563xx_write_register(struct target *target, int num, int force);
488 static int dsp563xx_reg_read_high_io(struct target *target, uint32_t instr_mask, uint32_t *data)
490 int err;
491 uint32_t instr;
492 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
494 /* we use r0 to store temporary data */
495 if (!dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_R0].valid)
496 dsp563xx->read_core_reg(target, DSP563XX_REG_IDX_R0);
498 /* move source memory to r0 */
499 instr = INSTR_MOVEP_REG_HIO(MEM_X, 0, EAME_R0, instr_mask);
500 err = dsp563xx_once_execute_sw_ir(target->tap, 0, instr);
501 if (err != ERROR_OK)
502 return err;
503 /* move r0 to debug register */
504 instr = INSTR_MOVEP_REG_HIO(MEM_X, 1, EAME_R0, 0xfffffc);
505 err = dsp563xx_once_execute_sw_ir(target->tap, 1, instr);
506 if (err != ERROR_OK)
507 return err;
508 /* read debug register */
509 err = dsp563xx_once_reg_read(target->tap, 1, DSP563XX_ONCE_OGDBR, data);
510 if (err != ERROR_OK)
511 return err;
512 /* r0 is no longer valid on target */
513 dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_R0].dirty = 1;
515 return ERROR_OK;
518 static int dsp563xx_reg_write_high_io(struct target *target, uint32_t instr_mask, uint32_t data)
520 int err;
521 uint32_t instr;
522 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
524 /* we use r0 to store temporary data */
525 if (!dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_R0].valid)
526 dsp563xx->read_core_reg(target, DSP563XX_REG_IDX_R0);
528 /* move data to r0 */
529 err = dsp563xx_once_execute_dw_ir(target->tap, 0, 0x60F400, data);
530 if (err != ERROR_OK)
531 return err;
532 /* move r0 to destination memory */
533 instr = INSTR_MOVEP_REG_HIO(MEM_X, 1, EAME_R0, instr_mask);
534 err = dsp563xx_once_execute_sw_ir(target->tap, 1, instr);
535 if (err != ERROR_OK)
536 return err;
538 /* r0 is no longer valid on target */
539 dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_R0].dirty = 1;
541 return ERROR_OK;
544 static int dsp563xx_reg_read(struct target *target, uint32_t eame, uint32_t *data)
546 int err;
547 uint32_t instr;
549 instr = INSTR_MOVEP_REG_HIO(MEM_X, 1, eame, 0xfffffc);
550 err = dsp563xx_once_execute_sw_ir(target->tap, 0, instr);
551 if (err != ERROR_OK)
552 return err;
553 /* nop */
554 err = dsp563xx_once_execute_sw_ir(target->tap, 1, 0x000000);
555 if (err != ERROR_OK)
556 return err;
557 /* read debug register */
558 return dsp563xx_once_reg_read(target->tap, 1, DSP563XX_ONCE_OGDBR, data);
561 static int dsp563xx_reg_write(struct target *target, uint32_t instr_mask, uint32_t data)
563 int err;
565 err = dsp563xx_once_execute_dw_ir(target->tap, 0, instr_mask, data);
566 if (err != ERROR_OK)
567 return err;
568 /* nop */
569 return dsp563xx_once_execute_sw_ir(target->tap, 1, 0x000000);
572 static int dsp563xx_reg_pc_read(struct target *target)
574 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
576 /* pc was changed, nothing todo */
577 if (dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_PC].dirty)
578 return ERROR_OK;
580 /* conditional branch check */
581 if (once_regs[ONCE_REG_IDX_OPABDR].reg == once_regs[ONCE_REG_IDX_OPABEX].reg) {
582 if ((once_regs[ONCE_REG_IDX_OPABF11].reg & 1) == 0) {
583 LOG_DEBUG("%s conditional branch not supported yet (0x%" PRIx32 " 0x%" PRIx32 " 0x%" PRIx32 ")",
584 __func__,
585 (once_regs[ONCE_REG_IDX_OPABF11].reg >> 1),
586 once_regs[ONCE_REG_IDX_OPABDR].reg,
587 once_regs[ONCE_REG_IDX_OPABEX].reg);
589 /* TODO: use disassembly to set correct pc offset
590 * read 2 words from OPABF11 and disasm the instruction
592 dsp563xx->core_regs[DSP563XX_REG_IDX_PC] =
593 (once_regs[ONCE_REG_IDX_OPABF11].reg >> 1) & 0x00FFFFFF;
594 } else {
595 if (once_regs[ONCE_REG_IDX_OPABEX].reg ==
596 once_regs[ONCE_REG_IDX_OPABFR].reg)
597 dsp563xx->core_regs[DSP563XX_REG_IDX_PC] =
598 once_regs[ONCE_REG_IDX_OPABEX].reg;
599 else
600 dsp563xx->core_regs[DSP563XX_REG_IDX_PC] =
601 once_regs[ONCE_REG_IDX_OPABEX].reg - 1;
603 } else
604 dsp563xx->core_regs[DSP563XX_REG_IDX_PC] = once_regs[ONCE_REG_IDX_OPABEX].reg;
606 dsp563xx->read_core_reg(target, DSP563XX_REG_IDX_PC);
608 return ERROR_OK;
611 static int dsp563xx_reg_ssh_read(struct target *target)
613 int err;
614 uint32_t sp;
615 struct dsp563xx_core_reg *arch_info;
616 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
618 arch_info = dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_SSH].arch_info;
620 /* get a valid stack pointer */
621 err = dsp563xx_read_register(target, DSP563XX_REG_IDX_SP, 0);
622 if (err != ERROR_OK)
623 return err;
624 sp = dsp563xx->core_regs[DSP563XX_REG_IDX_SP];
625 err = dsp563xx_write_register(target, DSP563XX_REG_IDX_SP, 0);
626 if (err != ERROR_OK)
627 return err;
629 /* get a valid stack count */
630 err = dsp563xx_read_register(target, DSP563XX_REG_IDX_SC, 0);
631 if (err != ERROR_OK)
632 return err;
634 err = dsp563xx_write_register(target, DSP563XX_REG_IDX_SC, 0);
635 if (err != ERROR_OK)
636 return err;
638 /* get a valid extended pointer */
639 err = dsp563xx_read_register(target, DSP563XX_REG_IDX_EP, 0);
640 if (err != ERROR_OK)
641 return err;
643 err = dsp563xx_write_register(target, DSP563XX_REG_IDX_EP, 0);
644 if (err != ERROR_OK)
645 return err;
647 if (!sp)
648 sp = 0x00FFFFFF;
649 else {
650 err = dsp563xx_reg_read(target, arch_info->eame, &sp);
651 if (err != ERROR_OK)
652 return err;
654 err = dsp563xx_write_register(target, DSP563XX_REG_IDX_SC, 1);
655 if (err != ERROR_OK)
656 return err;
657 err = dsp563xx_write_register(target, DSP563XX_REG_IDX_SP, 1);
658 if (err != ERROR_OK)
659 return err;
660 err = dsp563xx_write_register(target, DSP563XX_REG_IDX_EP, 1);
661 if (err != ERROR_OK)
662 return err;
665 dsp563xx->core_regs[DSP563XX_REG_IDX_SSH] = sp;
666 dsp563xx->read_core_reg(target, DSP563XX_REG_IDX_SSH);
668 return ERROR_OK;
671 static int dsp563xx_reg_ssh_write(struct target *target)
673 int err;
674 uint32_t sp;
675 struct dsp563xx_core_reg *arch_info;
676 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
678 arch_info = dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_SSH].arch_info;
680 /* get a valid stack pointer */
681 err = dsp563xx_read_register(target, DSP563XX_REG_IDX_SP, 0);
682 if (err != ERROR_OK)
683 return err;
684 sp = dsp563xx->core_regs[DSP563XX_REG_IDX_SP];
686 if (sp) {
687 sp--;
688 /* write new stackpointer */
689 dsp563xx->core_regs[DSP563XX_REG_IDX_SP] = sp;
690 err = dsp563xx->read_core_reg(target, DSP563XX_REG_IDX_SP);
691 if (err != ERROR_OK)
692 return err;
693 err = dsp563xx_write_register(target, DSP563XX_REG_IDX_SP, 1);
694 if (err != ERROR_OK)
695 return err;
697 err = dsp563xx_reg_write(target, arch_info->instr_mask,
698 dsp563xx->core_regs[DSP563XX_REG_IDX_SSH]);
699 if (err != ERROR_OK)
700 return err;
702 err = dsp563xx_read_register(target, DSP563XX_REG_IDX_SP, 1);
703 if (err != ERROR_OK)
704 return err;
705 err = dsp563xx_read_register(target, DSP563XX_REG_IDX_SSH, 1);
706 if (err != ERROR_OK)
707 return err;
710 return ERROR_OK;
713 static int dsp563xx_reg_ssl_read(struct target *target)
715 int err;
716 uint32_t sp;
717 struct dsp563xx_core_reg *arch_info;
718 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
720 arch_info = dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_SSL].arch_info;
722 /* get a valid stack pointer */
723 err = dsp563xx_read_register(target, DSP563XX_REG_IDX_SP, 0);
724 if (err != ERROR_OK)
725 return err;
726 sp = dsp563xx->core_regs[DSP563XX_REG_IDX_SP];
728 if (!sp)
729 sp = 0x00FFFFFF;
730 else {
731 err = dsp563xx_reg_read(target, arch_info->eame, &sp);
732 if (err != ERROR_OK)
733 return err;
736 dsp563xx->core_regs[DSP563XX_REG_IDX_SSL] = sp;
737 dsp563xx->read_core_reg(target, DSP563XX_REG_IDX_SSL);
739 return ERROR_OK;
742 static int dsp563xx_read_register(struct target *target, int num, int force)
744 int err = ERROR_OK;
745 uint32_t data = 0;
746 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
747 struct dsp563xx_core_reg *arch_info;
749 if (force)
750 dsp563xx->core_cache->reg_list[num].valid = 0;
752 if (!dsp563xx->core_cache->reg_list[num].valid) {
753 arch_info = dsp563xx->core_cache->reg_list[num].arch_info;
755 switch (arch_info->num) {
756 case DSP563XX_REG_IDX_SSH:
757 err = dsp563xx_reg_ssh_read(target);
758 break;
759 case DSP563XX_REG_IDX_SSL:
760 err = dsp563xx_reg_ssl_read(target);
761 break;
762 case DSP563XX_REG_IDX_PC:
763 err = dsp563xx_reg_pc_read(target);
764 break;
765 case DSP563XX_REG_IDX_IPRC:
766 case DSP563XX_REG_IDX_IPRP:
767 case DSP563XX_REG_IDX_BCR:
768 case DSP563XX_REG_IDX_DCR:
769 case DSP563XX_REG_IDX_AAR0:
770 case DSP563XX_REG_IDX_AAR1:
771 case DSP563XX_REG_IDX_AAR2:
772 case DSP563XX_REG_IDX_AAR3:
773 err = dsp563xx_reg_read_high_io(target,
774 arch_info->instr_mask, &data);
775 if (err == ERROR_OK) {
776 dsp563xx->core_regs[num] = data;
777 dsp563xx->read_core_reg(target, num);
779 break;
780 default:
781 err = dsp563xx_reg_read(target, arch_info->eame, &data);
782 if (err == ERROR_OK) {
783 dsp563xx->core_regs[num] = data;
784 dsp563xx->read_core_reg(target, num);
786 break;
790 return err;
793 static int dsp563xx_write_register(struct target *target, int num, int force)
795 int err = ERROR_OK;
796 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
797 struct dsp563xx_core_reg *arch_info;
799 if (force)
800 dsp563xx->core_cache->reg_list[num].dirty = 1;
802 if (dsp563xx->core_cache->reg_list[num].dirty) {
803 arch_info = dsp563xx->core_cache->reg_list[num].arch_info;
805 dsp563xx->write_core_reg(target, num);
807 switch (arch_info->num) {
808 case DSP563XX_REG_IDX_SSH:
809 err = dsp563xx_reg_ssh_write(target);
810 break;
811 case DSP563XX_REG_IDX_PC:
812 /* pc is updated on resume, no need to write it here */
813 break;
814 case DSP563XX_REG_IDX_IPRC:
815 case DSP563XX_REG_IDX_IPRP:
816 case DSP563XX_REG_IDX_BCR:
817 case DSP563XX_REG_IDX_DCR:
818 case DSP563XX_REG_IDX_AAR0:
819 case DSP563XX_REG_IDX_AAR1:
820 case DSP563XX_REG_IDX_AAR2:
821 case DSP563XX_REG_IDX_AAR3:
822 err = dsp563xx_reg_write_high_io(target,
823 arch_info->instr_mask,
824 dsp563xx->core_regs[num]);
825 break;
826 default:
827 err = dsp563xx_reg_write(target,
828 arch_info->instr_mask,
829 dsp563xx->core_regs[num]);
831 if ((err == ERROR_OK) && (arch_info->num == DSP563XX_REG_IDX_SP)) {
832 dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_SSH].valid =
834 dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_SSL].valid =
838 break;
842 return err;
845 static int dsp563xx_save_context(struct target *target)
847 int i, err = ERROR_OK;
849 for (i = 0; i < DSP563XX_NUMCOREREGS; i++) {
850 err = dsp563xx_read_register(target, i, 0);
851 if (err != ERROR_OK)
852 break;
855 return err;
858 static int dsp563xx_restore_context(struct target *target)
860 int i, err = ERROR_OK;
862 for (i = 0; i < DSP563XX_NUMCOREREGS; i++) {
863 err = dsp563xx_write_register(target, i, 0);
864 if (err != ERROR_OK)
865 break;
868 return err;
871 static void dsp563xx_invalidate_x_context(struct target *target,
872 uint32_t addr_start,
873 uint32_t addr_end)
875 int i;
876 struct dsp563xx_core_reg *arch_info;
877 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
879 if (addr_start > ASM_REG_W_IPRC)
880 return;
881 if (addr_start < ASM_REG_W_AAR3)
882 return;
884 for (i = DSP563XX_REG_IDX_IPRC; i < DSP563XX_NUMCOREREGS; i++) {
885 arch_info = dsp563xx->core_cache->reg_list[i].arch_info;
887 if ((arch_info->instr_mask >= addr_start) &&
888 (arch_info->instr_mask <= addr_end)) {
889 dsp563xx->core_cache->reg_list[i].valid = 0;
890 dsp563xx->core_cache->reg_list[i].dirty = 0;
895 static int dsp563xx_target_create(struct target *target, Jim_Interp *interp)
897 struct dsp563xx_common *dsp563xx = calloc(1, sizeof(struct dsp563xx_common));
899 if (!dsp563xx)
900 return ERROR_COMMAND_SYNTAX_ERROR;
902 dsp563xx->jtag_info.tap = target->tap;
903 target->arch_info = dsp563xx;
904 dsp563xx->read_core_reg = dsp563xx_read_core_reg;
905 dsp563xx->write_core_reg = dsp563xx_write_core_reg;
907 return ERROR_OK;
910 static int dsp563xx_init_target(struct command_context *cmd_ctx, struct target *target)
912 LOG_DEBUG("%s", __func__);
914 dsp563xx_build_reg_cache(target);
915 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
917 dsp563xx->hardware_breakpoints_cleared = 0;
918 dsp563xx->hardware_breakpoint[0].used = BPU_NONE;
920 return ERROR_OK;
923 static int dsp563xx_examine(struct target *target)
925 uint32_t chip;
927 if (target->tap->hasidcode == false) {
928 LOG_ERROR("no IDCODE present on device");
929 return ERROR_COMMAND_SYNTAX_ERROR;
932 if (!target_was_examined(target)) {
933 target_set_examined(target);
935 /* examine core and chip derivate number */
936 chip = (target->tap->idcode>>12) & 0x3ff;
937 /* core number 0 means DSP563XX */
938 if (((chip>>5)&0x1f) == 0)
939 chip += 300;
941 LOG_INFO("DSP56%03" PRId32 " device found", chip);
943 /* Clear all breakpoints */
944 dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_OBCR, 0);
947 return ERROR_OK;
950 static int dsp563xx_arch_state(struct target *target)
952 LOG_DEBUG("%s", __func__);
953 return ERROR_OK;
956 #define DSP563XX_SR_SA (1<<17)
957 #define DSP563XX_SR_SC (1<<13)
959 static int dsp563xx_debug_once_init(struct target *target)
961 return dsp563xx_once_read_register(target->tap, 1, once_regs, DSP563XX_NUMONCEREGS);
964 static int dsp563xx_debug_init(struct target *target)
966 int err;
967 uint32_t sr;
968 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
969 struct dsp563xx_core_reg *arch_info;
971 err = dsp563xx_debug_once_init(target);
972 if (err != ERROR_OK)
973 return err;
975 arch_info = dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_SR].arch_info;
977 /* check 24bit mode */
978 err = dsp563xx_read_register(target, DSP563XX_REG_IDX_SR, 0);
979 if (err != ERROR_OK)
980 return err;
982 sr = dsp563xx->core_regs[DSP563XX_REG_IDX_SR];
984 if (sr & (DSP563XX_SR_SA | DSP563XX_SR_SC)) {
985 sr &= ~(DSP563XX_SR_SA | DSP563XX_SR_SC);
987 err = dsp563xx_once_execute_dw_ir(target->tap, 1, arch_info->instr_mask, sr);
988 if (err != ERROR_OK)
989 return err;
990 dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_SR].dirty = 1;
993 err = dsp563xx_read_register(target, DSP563XX_REG_IDX_N0, 0);
994 if (err != ERROR_OK)
995 return err;
996 err = dsp563xx_read_register(target, DSP563XX_REG_IDX_N1, 0);
997 if (err != ERROR_OK)
998 return err;
999 err = dsp563xx_read_register(target, DSP563XX_REG_IDX_M0, 0);
1000 if (err != ERROR_OK)
1001 return err;
1002 err = dsp563xx_read_register(target, DSP563XX_REG_IDX_M1, 0);
1003 if (err != ERROR_OK)
1004 return err;
1006 if (dsp563xx->core_regs[DSP563XX_REG_IDX_N0] != 0x000000) {
1007 arch_info = dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_N0].arch_info;
1008 err = dsp563xx_reg_write(target, arch_info->instr_mask, 0x000000);
1009 if (err != ERROR_OK)
1010 return err;
1012 dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_N0].dirty = 1;
1014 if (dsp563xx->core_regs[DSP563XX_REG_IDX_N1] != 0x000000) {
1015 arch_info = dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_N1].arch_info;
1016 err = dsp563xx_reg_write(target, arch_info->instr_mask, 0x000000);
1017 if (err != ERROR_OK)
1018 return err;
1020 dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_N1].dirty = 1;
1022 if (dsp563xx->core_regs[DSP563XX_REG_IDX_M0] != 0xffffff) {
1023 arch_info = dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_M0].arch_info;
1024 err = dsp563xx_reg_write(target, arch_info->instr_mask, 0xffffff);
1025 if (err != ERROR_OK)
1026 return err;
1028 dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_M0].dirty = 1;
1030 if (dsp563xx->core_regs[DSP563XX_REG_IDX_M1] != 0xffffff) {
1031 arch_info = dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_M1].arch_info;
1032 err = dsp563xx_reg_write(target, arch_info->instr_mask, 0xffffff);
1033 if (err != ERROR_OK)
1034 return err;
1036 dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_M1].dirty = 1;
1038 err = dsp563xx_save_context(target);
1039 if (err != ERROR_OK)
1040 return err;
1042 return ERROR_OK;
1045 static int dsp563xx_jtag_debug_request(struct target *target)
1047 return dsp563xx_once_request_debug(target->tap, target->state == TARGET_RESET);
1050 static int dsp563xx_poll(struct target *target)
1052 int err;
1053 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
1054 uint32_t once_status = 0;
1055 int state;
1057 state = dsp563xx_once_target_status(target->tap);
1059 if (state == TARGET_UNKNOWN) {
1060 target->state = state;
1061 LOG_ERROR("jtag status contains invalid mode value - communication failure");
1062 return ERROR_TARGET_FAILURE;
1065 err = dsp563xx_once_reg_read(target->tap, 1, DSP563XX_ONCE_OSCR, &once_status);
1066 if (err != ERROR_OK)
1067 return err;
1069 if ((once_status & DSP563XX_ONCE_OSCR_DEBUG_M) == DSP563XX_ONCE_OSCR_DEBUG_M) {
1070 if (target->state != TARGET_HALTED) {
1071 target->state = TARGET_HALTED;
1073 err = dsp563xx_debug_init(target);
1074 if (err != ERROR_OK)
1075 return err;
1077 if (once_status & (DSP563XX_ONCE_OSCR_MBO|DSP563XX_ONCE_OSCR_SWO))
1078 target_call_event_callbacks(target, TARGET_EVENT_DEBUG_HALTED);
1079 else
1080 target_call_event_callbacks(target, TARGET_EVENT_HALTED);
1082 LOG_DEBUG("target->state: %s (%" PRIx32 ")", target_state_name(target), once_status);
1083 LOG_INFO("halted: PC: 0x%" PRIx32, dsp563xx->core_regs[DSP563XX_REG_IDX_PC]);
1087 if (!dsp563xx->hardware_breakpoints_cleared) {
1088 err = dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_OBCR, 0);
1089 err = dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_OMLR0, 0);
1090 err = dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_OMLR1, 0);
1091 dsp563xx->hardware_breakpoints_cleared = 1;
1094 return ERROR_OK;
1097 static int dsp563xx_halt(struct target *target)
1099 int err;
1101 LOG_DEBUG("%s", __func__);
1103 if (target->state == TARGET_HALTED) {
1104 LOG_DEBUG("target was already halted");
1105 return ERROR_OK;
1108 if (target->state == TARGET_UNKNOWN)
1109 LOG_WARNING("target was in unknown state when halt was requested");
1111 err = dsp563xx_jtag_debug_request(target);
1112 if (err != ERROR_OK)
1113 return err;
1115 target->debug_reason = DBG_REASON_DBGRQ;
1117 return ERROR_OK;
1120 static int dsp563xx_resume(struct target *target,
1121 int current,
1122 uint32_t address,
1123 int handle_breakpoints,
1124 int debug_execution)
1126 int err;
1127 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
1129 /* check if pc was changed and resume want to execute the next address
1130 * if pc was changed from gdb or other interface we will
1131 * jump to this address and don't execute the next address
1132 * this will not affect the resume command with an address argument
1133 * because current is set to zero then
1135 if (current && dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_PC].dirty) {
1136 dsp563xx_write_core_reg(target, DSP563XX_REG_IDX_PC);
1137 address = dsp563xx->core_regs[DSP563XX_REG_IDX_PC];
1138 current = 0;
1141 LOG_DEBUG("%s %08X %08X", __func__, current, (unsigned) address);
1143 err = dsp563xx_restore_context(target);
1144 if (err != ERROR_OK)
1145 return err;
1146 register_cache_invalidate(dsp563xx->core_cache);
1148 if (current) {
1149 /* restore pipeline registers and go */
1150 err = dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_OPDBR,
1151 once_regs[ONCE_REG_IDX_OPILR].reg);
1152 if (err != ERROR_OK)
1153 return err;
1154 err = dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_OPDBR |
1155 DSP563XX_ONCE_OCR_EX | DSP563XX_ONCE_OCR_GO,
1156 once_regs[ONCE_REG_IDX_OPDBR].reg);
1157 if (err != ERROR_OK)
1158 return err;
1159 } else {
1160 /* set to go register and jump */
1161 err = dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_OPDBR, INSTR_JUMP);
1162 if (err != ERROR_OK)
1163 return err;
1164 err = dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_PDBGOTO |
1165 DSP563XX_ONCE_OCR_EX | DSP563XX_ONCE_OCR_GO, address);
1166 if (err != ERROR_OK)
1167 return err;
1170 target->state = TARGET_RUNNING;
1172 target_call_event_callbacks(target, TARGET_EVENT_DEBUG_RESUMED);
1174 return ERROR_OK;
1177 static int dsp563xx_step_ex(struct target *target,
1178 int current,
1179 uint32_t address,
1180 int handle_breakpoints,
1181 int steps)
1183 int err;
1184 uint32_t once_status;
1185 uint32_t dr_in, cnt;
1186 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
1188 if (target->state != TARGET_HALTED) {
1189 LOG_DEBUG("target was not halted");
1190 return ERROR_OK;
1193 /* check if pc was changed and step want to execute the next address
1194 * if pc was changed from gdb or other interface we will
1195 * jump to this address and don't execute the next address
1196 * this will not affect the step command with an address argument
1197 * because current is set to zero then
1199 if (current && dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_PC].dirty) {
1200 dsp563xx_write_core_reg(target, DSP563XX_REG_IDX_PC);
1201 address = dsp563xx->core_regs[DSP563XX_REG_IDX_PC];
1202 current = 0;
1205 LOG_DEBUG("%s %08X %08X", __func__, current, (unsigned) address);
1207 err = dsp563xx_jtag_debug_request(target);
1208 if (err != ERROR_OK)
1209 return err;
1210 err = dsp563xx_restore_context(target);
1211 if (err != ERROR_OK)
1212 return err;
1214 /* reset trace mode */
1215 err = dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_OSCR, 0x000000);
1216 if (err != ERROR_OK)
1217 return err;
1218 /* enable trace mode */
1219 err = dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_OSCR, DSP563XX_ONCE_OSCR_TME);
1220 if (err != ERROR_OK)
1221 return err;
1223 cnt = steps;
1225 /* on JUMP we need one extra cycle */
1226 if (!current)
1227 cnt++;
1229 /* load step counter with N-1 */
1230 err = dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_OTC, cnt);
1231 if (err != ERROR_OK)
1232 return err;
1234 if (current) {
1235 /* restore pipeline registers and go */
1236 err = dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_OPDBR,
1237 once_regs[ONCE_REG_IDX_OPILR].reg);
1238 if (err != ERROR_OK)
1239 return err;
1240 err = dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_OPDBR |
1241 DSP563XX_ONCE_OCR_EX | DSP563XX_ONCE_OCR_GO,
1242 once_regs[ONCE_REG_IDX_OPDBR].reg);
1243 if (err != ERROR_OK)
1244 return err;
1245 } else {
1246 /* set to go register and jump */
1247 err = dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_OPDBR, INSTR_JUMP);
1248 if (err != ERROR_OK)
1249 return err;
1250 err = dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_PDBGOTO |
1251 DSP563XX_ONCE_OCR_EX | DSP563XX_ONCE_OCR_GO,
1252 address);
1253 if (err != ERROR_OK)
1254 return err;
1257 while (1) {
1258 err = dsp563xx_once_reg_read(target->tap, 1, DSP563XX_ONCE_OSCR, &once_status);
1259 if (err != ERROR_OK)
1260 return err;
1262 if (once_status & DSP563XX_ONCE_OSCR_TO) {
1263 err = dsp563xx_once_reg_read(target->tap, 1, DSP563XX_ONCE_OPABFR, &dr_in);
1264 if (err != ERROR_OK)
1265 return err;
1266 LOG_DEBUG("fetch: %08X", (unsigned) dr_in&0x00ffffff);
1267 err = dsp563xx_once_reg_read(target->tap, 1, DSP563XX_ONCE_OPABDR, &dr_in);
1268 if (err != ERROR_OK)
1269 return err;
1270 LOG_DEBUG("decode: %08X", (unsigned) dr_in&0x00ffffff);
1271 err = dsp563xx_once_reg_read(target->tap, 1, DSP563XX_ONCE_OPABEX, &dr_in);
1272 if (err != ERROR_OK)
1273 return err;
1274 LOG_DEBUG("execute: %08X", (unsigned) dr_in&0x00ffffff);
1276 /* reset trace mode */
1277 err = dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_OSCR, 0x000000);
1278 if (err != ERROR_OK)
1279 return err;
1281 register_cache_invalidate(dsp563xx->core_cache);
1282 err = dsp563xx_debug_init(target);
1283 if (err != ERROR_OK)
1284 return err;
1286 break;
1290 return ERROR_OK;
1293 static int dsp563xx_step(struct target *target,
1294 int current,
1295 uint32_t address,
1296 int handle_breakpoints)
1298 int err;
1299 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
1301 if (target->state != TARGET_HALTED) {
1302 LOG_WARNING("target not halted");
1303 return ERROR_TARGET_NOT_HALTED;
1306 err = dsp563xx_step_ex(target, current, address, handle_breakpoints, 0);
1307 if (err != ERROR_OK)
1308 return err;
1310 target->debug_reason = DBG_REASON_SINGLESTEP;
1311 target_call_event_callbacks(target, TARGET_EVENT_HALTED);
1313 LOG_INFO("halted: PC: 0x%" PRIx32, dsp563xx->core_regs[DSP563XX_REG_IDX_PC]);
1315 return err;
1318 static int dsp563xx_assert_reset(struct target *target)
1320 int retval = 0;
1321 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
1322 enum reset_types jtag_reset_config = jtag_get_reset_config();
1324 if (jtag_reset_config & RESET_HAS_SRST) {
1325 /* default to asserting srst */
1326 if (jtag_reset_config & RESET_SRST_PULLS_TRST)
1327 jtag_add_reset(1, 1);
1328 else
1329 jtag_add_reset(0, 1);
1332 target->state = TARGET_RESET;
1333 jtag_add_sleep(5000);
1335 /* registers are now invalid */
1336 register_cache_invalidate(dsp563xx->core_cache);
1338 if (target->reset_halt) {
1339 retval = target_halt(target);
1340 if (retval != ERROR_OK)
1341 return retval;
1344 LOG_DEBUG("%s", __func__);
1345 return ERROR_OK;
1348 static int dsp563xx_deassert_reset(struct target *target)
1350 int err;
1352 /* deassert reset lines */
1353 jtag_add_reset(0, 0);
1355 err = dsp563xx_poll(target);
1356 if (err != ERROR_OK)
1357 return err;
1359 if (target->reset_halt) {
1360 if (target->state == TARGET_HALTED) {
1361 /* after a reset the cpu jmp to the
1362 * reset vector and need 2 cycles to fill
1363 * the cache (fetch,decode,excecute)
1365 err = dsp563xx_step_ex(target, 1, 0, 1, 1);
1366 if (err != ERROR_OK)
1367 return err;
1369 } else
1370 target->state = TARGET_RUNNING;
1372 LOG_DEBUG("%s", __func__);
1373 return ERROR_OK;
1376 static int dsp563xx_run_algorithm(struct target *target,
1377 int num_mem_params, struct mem_param *mem_params,
1378 int num_reg_params, struct reg_param *reg_params,
1379 uint32_t entry_point, uint32_t exit_point,
1380 int timeout_ms, void *arch_info)
1382 int i;
1383 int retval = ERROR_OK;
1384 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
1386 if (target->state != TARGET_HALTED) {
1387 LOG_WARNING("target not halted");
1388 return ERROR_TARGET_NOT_HALTED;
1391 for (i = 0; i < num_mem_params; i++) {
1392 retval = target_write_buffer(target, mem_params[i].address,
1393 mem_params[i].size, mem_params[i].value);
1394 if (retval != ERROR_OK)
1395 return retval;
1398 for (i = 0; i < num_reg_params; i++) {
1399 struct reg *reg = register_get_by_name(dsp563xx->core_cache,
1400 reg_params[i].reg_name,
1403 if (!reg) {
1404 LOG_ERROR("BUG: register '%s' not found", reg_params[i].reg_name);
1405 continue;
1408 if (reg->size != reg_params[i].size) {
1409 LOG_ERROR("BUG: register '%s' size doesn't match reg_params[i].size",
1410 reg_params[i].reg_name);
1411 continue;
1414 retval = dsp563xx_set_core_reg(reg, reg_params[i].value);
1415 if (retval != ERROR_OK)
1416 return retval;
1419 /* exec */
1420 retval = target_resume(target, 0, entry_point, 1, 1);
1421 if (retval != ERROR_OK)
1422 return retval;
1424 retval = target_wait_state(target, TARGET_HALTED, timeout_ms);
1425 if (retval != ERROR_OK)
1426 return retval;
1428 for (i = 0; i < num_mem_params; i++) {
1429 if (mem_params[i].direction != PARAM_OUT)
1430 retval = target_read_buffer(target,
1431 mem_params[i].address,
1432 mem_params[i].size,
1433 mem_params[i].value);
1434 if (retval != ERROR_OK)
1435 return retval;
1438 for (i = 0; i < num_reg_params; i++) {
1439 if (reg_params[i].direction != PARAM_OUT) {
1441 struct reg *reg = register_get_by_name(dsp563xx->core_cache,
1442 reg_params[i].reg_name,
1444 if (!reg) {
1445 LOG_ERROR("BUG: register '%s' not found", reg_params[i].reg_name);
1446 continue;
1449 if (reg->size != reg_params[i].size) {
1450 LOG_ERROR(
1451 "BUG: register '%s' size doesn't match reg_params[i].size",
1452 reg_params[i].reg_name);
1453 continue;
1456 buf_set_u32(reg_params[i].value, 0, 32, buf_get_u32(reg->value, 0, 32));
1460 return ERROR_OK;
1463 /* global command context from openocd.c */
1464 extern struct command_context *global_cmd_ctx;
1466 static int dsp563xx_get_default_memory(void)
1468 Jim_Interp *interp;
1469 Jim_Obj *memspace;
1470 char *c;
1472 if (!global_cmd_ctx)
1473 return MEM_P;
1475 interp = global_cmd_ctx->interp;
1477 if (!interp)
1478 return MEM_P;
1480 memspace = Jim_GetGlobalVariableStr(interp, "memspace", JIM_NONE);
1482 if (!memspace)
1483 return MEM_P;
1485 c = (char *)Jim_GetString(memspace, NULL);
1487 if (!c)
1488 return MEM_P;
1490 switch (c[0]) {
1491 case '1':
1492 return MEM_X;
1493 case '2':
1494 return MEM_Y;
1495 case '3':
1496 return MEM_L;
1497 default:
1498 break;
1501 return MEM_P;
1504 static int dsp563xx_read_memory_core(struct target *target,
1505 int mem_type,
1506 uint32_t address,
1507 uint32_t size,
1508 uint32_t count,
1509 uint8_t *buffer)
1511 int err;
1512 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
1513 uint32_t i, x;
1514 uint32_t data, move_cmd = 0;
1515 uint8_t *b;
1517 LOG_DEBUG(
1518 "memtype: %d address: 0x%8.8" PRIx32 ", size: 0x%8.8" PRIx32 ", count: 0x%8.8" PRIx32 "",
1519 mem_type,
1520 address,
1521 size,
1522 count);
1524 if (target->state != TARGET_HALTED) {
1525 LOG_WARNING("target not halted");
1526 return ERROR_TARGET_NOT_HALTED;
1529 switch (mem_type) {
1530 case MEM_X:
1531 /* TODO: mark effected queued registers */
1532 move_cmd = 0x61d800;
1533 break;
1534 case MEM_Y:
1535 move_cmd = 0x69d800;
1536 break;
1537 case MEM_P:
1538 move_cmd = 0x07d891;
1539 break;
1540 default:
1541 return ERROR_COMMAND_SYNTAX_ERROR;
1544 /* we use r0 to store temporary data */
1545 if (!dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_R0].valid)
1546 dsp563xx->read_core_reg(target, DSP563XX_REG_IDX_R0);
1547 /* we use r1 to store temporary data */
1548 if (!dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_R1].valid)
1549 dsp563xx->read_core_reg(target, DSP563XX_REG_IDX_R1);
1551 /* r0 is no longer valid on target */
1552 dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_R0].dirty = 1;
1553 /* r1 is no longer valid on target */
1554 dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_R1].dirty = 1;
1556 x = count;
1557 b = buffer;
1559 err = dsp563xx_once_execute_dw_ir(target->tap, 1, 0x60F400, address);
1560 if (err != ERROR_OK)
1561 return err;
1563 for (i = 0; i < x; i++) {
1564 err = dsp563xx_once_execute_sw_ir(target->tap, 0, move_cmd);
1565 if (err != ERROR_OK)
1566 return err;
1567 err = dsp563xx_once_execute_sw_ir(target->tap, 0, 0x08D13C);
1568 if (err != ERROR_OK)
1569 return err;
1570 err = dsp563xx_once_reg_read(target->tap, 0,
1571 DSP563XX_ONCE_OGDBR, (uint32_t *)(void *)b);
1572 if (err != ERROR_OK)
1573 return err;
1574 b += 4;
1577 /* flush the jtag queue */
1578 err = jtag_execute_queue();
1579 if (err != ERROR_OK)
1580 return err;
1582 /* walk over the buffer and fix target endianness */
1583 b = buffer;
1585 for (i = 0; i < x; i++) {
1586 data = buf_get_u32(b, 0, 32) & 0x00FFFFFF;
1587 /* LOG_DEBUG("R: %08X", *((uint32_t*)b)); */
1588 target_buffer_set_u32(target, b, data);
1589 b += 4;
1592 return ERROR_OK;
1595 static int dsp563xx_read_memory(struct target *target,
1596 int mem_type,
1597 uint32_t address,
1598 uint32_t size,
1599 uint32_t count,
1600 uint8_t *buffer)
1602 int err;
1603 uint32_t i, i1;
1604 uint8_t *buffer_y, *buffer_x;
1606 /* if size equals zero we are called from target read memory
1607 * and have to handle the parameter here */
1608 if ((size == 0) && (count != 0)) {
1609 size = count % 4;
1611 if (size)
1612 LOG_DEBUG("size is not aligned to 4 byte");
1614 count = (count - size) / 4;
1615 size = 4;
1618 /* we only support 4 byte aligned data */
1619 if ((size != 4) || (!count))
1620 return ERROR_COMMAND_SYNTAX_ERROR;
1622 if (mem_type != MEM_L)
1623 return dsp563xx_read_memory_core(target, mem_type, address, size, count, buffer);
1625 buffer_y = malloc(size * count);
1626 if (!buffer_y)
1627 return ERROR_COMMAND_SYNTAX_ERROR;
1629 buffer_x = malloc(size * count);
1630 if (!buffer_x) {
1631 free(buffer_y);
1632 return ERROR_COMMAND_SYNTAX_ERROR;
1635 err = dsp563xx_read_memory_core(target, MEM_Y, address, size, count / 2, buffer_y);
1637 if (err != ERROR_OK) {
1638 free(buffer_y);
1639 free(buffer_x);
1640 return err;
1643 err = dsp563xx_read_memory_core(target, MEM_X, address, size, count / 2, buffer_x);
1645 if (err != ERROR_OK) {
1646 free(buffer_y);
1647 free(buffer_x);
1648 return err;
1651 for (i = 0, i1 = 0; i < count; i += 2, i1++) {
1652 buf_set_u32(buffer + i*sizeof(uint32_t), 0, 32,
1653 buf_get_u32(buffer_y + i1 * sizeof(uint32_t), 0, 32));
1654 buf_set_u32(buffer + (i + 1) * sizeof(uint32_t), 0, 32,
1655 buf_get_u32(buffer_x + i1 * sizeof(uint32_t), 0, 32));
1658 free(buffer_y);
1659 free(buffer_x);
1661 return ERROR_OK;
1664 static int dsp563xx_read_memory_default(struct target *target,
1665 uint32_t address,
1666 uint32_t size,
1667 uint32_t count,
1668 uint8_t *buffer)
1671 return dsp563xx_read_memory(target,
1672 dsp563xx_get_default_memory(), address, size, count, buffer);
1675 static int dsp563xx_read_buffer_default(struct target *target,
1676 uint32_t address,
1677 uint32_t size,
1678 uint8_t *buffer)
1681 return dsp563xx_read_memory(target, dsp563xx_get_default_memory(), address, size, 0,
1682 buffer);
1685 static int dsp563xx_write_memory_core(struct target *target,
1686 int mem_type,
1687 uint32_t address,
1688 uint32_t size,
1689 uint32_t count,
1690 const uint8_t *buffer)
1692 int err;
1693 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
1694 uint32_t i, x;
1695 uint32_t data, move_cmd = 0;
1696 const uint8_t *b;
1698 LOG_DEBUG(
1699 "memtype: %d address: 0x%8.8" PRIx32 ", size: 0x%8.8" PRIx32 ", count: 0x%8.8" PRIx32 "",
1700 mem_type,
1701 address,
1702 size,
1703 count);
1705 if (target->state != TARGET_HALTED) {
1706 LOG_WARNING("target not halted");
1707 return ERROR_TARGET_NOT_HALTED;
1710 switch (mem_type) {
1711 case MEM_X:
1712 /* invalidate affected x registers */
1713 dsp563xx_invalidate_x_context(target, address, address + count - 1);
1714 move_cmd = 0x615800;
1715 break;
1716 case MEM_Y:
1717 move_cmd = 0x695800;
1718 break;
1719 case MEM_P:
1720 move_cmd = 0x075891;
1721 break;
1722 default:
1723 return ERROR_COMMAND_SYNTAX_ERROR;
1726 /* we use r0 to store temporary data */
1727 if (!dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_R0].valid)
1728 dsp563xx->read_core_reg(target, DSP563XX_REG_IDX_R0);
1729 /* we use r1 to store temporary data */
1730 if (!dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_R1].valid)
1731 dsp563xx->read_core_reg(target, DSP563XX_REG_IDX_R1);
1733 /* r0 is no longer valid on target */
1734 dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_R0].dirty = 1;
1735 /* r1 is no longer valid on target */
1736 dsp563xx->core_cache->reg_list[DSP563XX_REG_IDX_R1].dirty = 1;
1738 x = count;
1739 b = buffer;
1741 err = dsp563xx_once_execute_dw_ir(target->tap, 1, 0x60F400, address);
1742 if (err != ERROR_OK)
1743 return err;
1745 for (i = 0; i < x; i++) {
1746 data = target_buffer_get_u32(target, b);
1748 /* LOG_DEBUG("W: %08X", data); */
1750 data &= 0x00ffffff;
1752 err = dsp563xx_once_execute_dw_ir(target->tap, 0, 0x61F400, data);
1753 if (err != ERROR_OK)
1754 return err;
1755 err = dsp563xx_once_execute_sw_ir(target->tap, 0, move_cmd);
1756 if (err != ERROR_OK)
1757 return err;
1758 b += 4;
1761 /* flush the jtag queue */
1762 err = jtag_execute_queue();
1763 if (err != ERROR_OK)
1764 return err;
1766 return ERROR_OK;
1769 static int dsp563xx_write_memory(struct target *target,
1770 int mem_type,
1771 uint32_t address,
1772 uint32_t size,
1773 uint32_t count,
1774 const uint8_t *buffer)
1776 int err;
1777 uint32_t i, i1;
1778 uint8_t *buffer_y, *buffer_x;
1780 /* if size equals zero we are called from target write memory
1781 * and have to handle the parameter here */
1782 if ((size == 0) && (count != 0)) {
1783 size = count % 4;
1785 if (size)
1786 LOG_DEBUG("size is not aligned to 4 byte");
1788 count = (count - size) / 4;
1789 size = 4;
1792 /* we only support 4 byte aligned data */
1793 if ((size != 4) || (!count))
1794 return ERROR_COMMAND_SYNTAX_ERROR;
1796 if (mem_type != MEM_L)
1797 return dsp563xx_write_memory_core(target, mem_type, address, size, count, buffer);
1799 buffer_y = malloc(size * count);
1800 if (!buffer_y)
1801 return ERROR_COMMAND_SYNTAX_ERROR;
1803 buffer_x = malloc(size * count);
1804 if (!buffer_x) {
1805 free(buffer_y);
1806 return ERROR_COMMAND_SYNTAX_ERROR;
1809 for (i = 0, i1 = 0; i < count; i += 2, i1++) {
1810 buf_set_u32(buffer_y + i1 * sizeof(uint32_t), 0, 32,
1811 buf_get_u32(buffer + i * sizeof(uint32_t), 0, 32));
1812 buf_set_u32(buffer_x + i1 * sizeof(uint32_t), 0, 32,
1813 buf_get_u32(buffer + (i + 1) * sizeof(uint32_t), 0, 32));
1816 err = dsp563xx_write_memory_core(target, MEM_Y, address, size, count / 2, buffer_y);
1818 if (err != ERROR_OK) {
1819 free(buffer_y);
1820 free(buffer_x);
1821 return err;
1824 err = dsp563xx_write_memory_core(target, MEM_X, address, size, count / 2, buffer_x);
1826 if (err != ERROR_OK) {
1827 free(buffer_y);
1828 free(buffer_x);
1829 return err;
1832 free(buffer_y);
1833 free(buffer_x);
1835 return ERROR_OK;
1838 static int dsp563xx_write_memory_default(struct target *target,
1839 uint32_t address,
1840 uint32_t size,
1841 uint32_t count,
1842 const uint8_t *buffer)
1844 return dsp563xx_write_memory(target,
1845 dsp563xx_get_default_memory(), address, size, count, buffer);
1848 static int dsp563xx_write_buffer_default(struct target *target,
1849 uint32_t address,
1850 uint32_t size,
1851 const uint8_t *buffer)
1853 return dsp563xx_write_memory(target, dsp563xx_get_default_memory(), address, size, 0,
1854 buffer);
1858 * Exit with error here, because we support watchpoints over a custom command.
1859 * This is because the DSP has separate X,Y,P memspace which is not compatible to the
1860 * traditional watchpoint logic.
1862 static int dsp563xx_add_watchpoint(struct target *target, struct watchpoint *watchpoint)
1864 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1868 * @see dsp563xx_add_watchpoint
1870 static int dsp563xx_remove_watchpoint(struct target *target, struct watchpoint *watchpoint)
1872 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1875 static void handle_md_output(struct command_context *cmd_ctx,
1876 struct target *target,
1877 uint32_t address,
1878 unsigned size,
1879 unsigned count,
1880 const uint8_t *buffer)
1882 const unsigned line_bytecnt = 32;
1883 unsigned line_modulo = line_bytecnt / size;
1885 char output[line_bytecnt * 4 + 1];
1886 unsigned output_len = 0;
1888 const char *value_fmt;
1889 switch (size) {
1890 case 4:
1891 value_fmt = "%8.8x ";
1892 break;
1893 case 2:
1894 value_fmt = "%4.4x ";
1895 break;
1896 case 1:
1897 value_fmt = "%2.2x ";
1898 break;
1899 default:
1900 /* "can't happen", caller checked */
1901 LOG_ERROR("invalid memory read size: %u", size);
1902 return;
1905 for (unsigned i = 0; i < count; i++) {
1906 if (i % line_modulo == 0)
1907 output_len += snprintf(output + output_len,
1908 sizeof(output) - output_len,
1909 "0x%8.8x: ",
1910 (unsigned) (address + i));
1912 uint32_t value = 0;
1913 const uint8_t *value_ptr = buffer + i * size;
1914 switch (size) {
1915 case 4:
1916 value = target_buffer_get_u32(target, value_ptr);
1917 break;
1918 case 2:
1919 value = target_buffer_get_u16(target, value_ptr);
1920 break;
1921 case 1:
1922 value = *value_ptr;
1924 output_len += snprintf(output + output_len,
1925 sizeof(output) - output_len,
1926 value_fmt,
1927 value);
1929 if ((i % line_modulo == line_modulo - 1) || (i == count - 1)) {
1930 command_print(cmd_ctx, "%s", output);
1931 output_len = 0;
1936 static int dsp563xx_add_custom_watchpoint(struct target *target, uint32_t address, uint32_t memType,
1937 enum watchpoint_rw rw, enum watchpoint_condition cond)
1939 int err = ERROR_OK;
1940 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
1942 bool wasRunning = false;
1943 /* Only set breakpoint when halted */
1944 if (target->state != TARGET_HALTED) {
1945 dsp563xx_halt(target);
1946 wasRunning = true;
1949 if (dsp563xx->hardware_breakpoint[0].used) {
1950 LOG_ERROR("Cannot add watchpoint. Hardware resource already used.");
1951 err = ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1954 uint32_t obcr_value = 0;
1955 if (err == ERROR_OK) {
1956 obcr_value |= OBCR_b0_or_b1;
1957 switch (memType) {
1958 case MEM_X:
1959 obcr_value |= OBCR_BP_MEM_X;
1960 break;
1961 case MEM_Y:
1962 obcr_value |= OBCR_BP_MEM_Y;
1963 break;
1964 case MEM_P:
1965 obcr_value |= OBCR_BP_MEM_P;
1966 break;
1967 default:
1968 LOG_ERROR("Unknown memType parameter (%" PRIu32 ")", memType);
1969 err = ERROR_TARGET_INVALID;
1973 if (err == ERROR_OK) {
1974 switch (rw) {
1975 case WPT_READ:
1976 obcr_value |= OBCR_BP_0(OBCR_BP_ON_READ);
1977 break;
1978 case WPT_WRITE:
1979 obcr_value |= OBCR_BP_0(OBCR_BP_ON_WRITE);
1980 break;
1981 case WPT_ACCESS:
1982 obcr_value |= OBCR_BP_0(OBCR_BP_ON_READ|OBCR_BP_ON_WRITE);
1983 break;
1984 default:
1985 LOG_ERROR("Unsupported write mode (%d)", rw);
1986 err = ERROR_TARGET_INVALID;
1990 if (err == ERROR_OK) {
1991 switch (cond) {
1992 case EQUAL:
1993 obcr_value |= OBCR_BP_0(OBCR_BP_CC_EQUAL);
1994 break;
1995 case NOT_EQUAL:
1996 obcr_value |= OBCR_BP_0(OBCR_BP_CC_NOT_EQUAL);
1997 break;
1998 case LESS_THAN:
1999 obcr_value |= OBCR_BP_0(OBCR_BP_CC_LESS_THAN);
2000 break;
2001 case GREATER:
2002 obcr_value |= OBCR_BP_0(OBCR_BP_CC_GREATER_THAN);
2003 break;
2004 default:
2005 LOG_ERROR("Unsupported condition code (%d)", cond);
2006 err = ERROR_TARGET_INVALID;
2010 if (err == ERROR_OK)
2011 err = dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_OMLR0, address);
2013 if (err == ERROR_OK)
2014 err = dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_OMLR1, 0x0);
2016 if (err == ERROR_OK)
2017 err = dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_OBCR, obcr_value);
2019 if (err == ERROR_OK) {
2020 /* You should write the memory breakpoint counter to 0 */
2021 err = dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_OMBC, 0);
2024 if (err == ERROR_OK) {
2025 /* You should write the memory breakpoint counter to 0 */
2026 err = dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_OTC, 0);
2029 if (err == ERROR_OK)
2030 dsp563xx->hardware_breakpoint[0].used = BPU_WATCHPOINT;
2032 if (err == ERROR_OK && wasRunning) {
2033 /* Resume from current PC */
2034 err = dsp563xx_resume(target, 1, 0x0, 0, 0);
2037 return err;
2040 static int dsp563xx_remove_custom_watchpoint(struct target *target)
2042 int err = ERROR_OK;
2043 struct dsp563xx_common *dsp563xx = target_to_dsp563xx(target);
2045 if (dsp563xx->hardware_breakpoint[0].used != BPU_WATCHPOINT) {
2046 LOG_ERROR("Cannot remove watchpoint, as no watchpoint is currently configured!");
2047 err = ERROR_TARGET_INVALID;
2050 if (err == ERROR_OK) {
2051 /* Clear watchpoint by clearing OBCR. */
2052 err = dsp563xx_once_reg_write(target->tap, 1, DSP563XX_ONCE_OBCR, 0);
2055 if (err == ERROR_OK)
2056 dsp563xx->hardware_breakpoint[0].used = BPU_NONE;
2058 return err;
2061 COMMAND_HANDLER(dsp563xx_add_watchpoint_command)
2063 int err = ERROR_OK;
2064 struct target *target = get_current_target(CMD_CTX);
2066 uint32_t mem_type = 0;
2067 switch (CMD_NAME[2]) {
2068 case 'x':
2069 mem_type = MEM_X;
2070 break;
2071 case 'y':
2072 mem_type = MEM_Y;
2073 break;
2074 case 'p':
2075 mem_type = MEM_P;
2076 break;
2077 default:
2078 return ERROR_COMMAND_SYNTAX_ERROR;
2081 if (CMD_ARGC < 2)
2082 return ERROR_COMMAND_SYNTAX_ERROR;
2084 uint32_t address = 0;
2085 if (CMD_ARGC > 2)
2086 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], address);
2088 enum watchpoint_condition cond;
2089 switch (CMD_ARGV[0][0]) {
2090 case '>':
2091 cond = GREATER;
2092 break;
2093 case '<':
2094 cond = LESS_THAN;
2095 break;
2096 case '=':
2097 cond = EQUAL;
2098 break;
2099 case '!':
2100 cond = NOT_EQUAL;
2101 break;
2102 default:
2103 return ERROR_COMMAND_SYNTAX_ERROR;
2106 enum watchpoint_rw rw;
2107 switch (CMD_ARGV[1][0]) {
2108 case 'r':
2109 rw = WPT_READ;
2110 break;
2111 case 'w':
2112 rw = WPT_WRITE;
2113 break;
2114 case 'a':
2115 rw = WPT_ACCESS;
2116 break;
2117 default:
2118 return ERROR_COMMAND_SYNTAX_ERROR;
2121 err = dsp563xx_add_custom_watchpoint(target, address, mem_type, rw, cond);
2123 return err;
2126 /* Adding a breakpoint using the once breakpoint logic.
2127 * Note that this mechanism is a true hw breakpoint and is share between the watchpoint logic.
2128 * This means, you can only have one breakpoint/watchpoint at any time.
2130 static int dsp563xx_add_breakpoint(struct target *target, struct breakpoint *breakpoint)
2132 return dsp563xx_add_custom_watchpoint(target, breakpoint->address, MEM_P, WPT_READ, EQUAL);
2135 static int dsp563xx_remove_breakpoint(struct target *target, struct breakpoint *breakpoint)
2137 return dsp563xx_remove_custom_watchpoint(target);
2140 COMMAND_HANDLER(dsp563xx_remove_watchpoint_command)
2142 struct target *target = get_current_target(CMD_CTX);
2144 return dsp563xx_remove_custom_watchpoint(target);
2147 COMMAND_HANDLER(dsp563xx_mem_command)
2149 struct target *target = get_current_target(CMD_CTX);
2150 int err = ERROR_OK;
2151 int read_mem;
2152 uint32_t address = 0;
2153 uint32_t count = 1, i;
2154 uint32_t pattern = 0;
2155 uint32_t mem_type;
2156 uint8_t *buffer, *b;
2158 switch (CMD_NAME[1]) {
2159 case 'w':
2160 read_mem = 0;
2161 break;
2162 case 'd':
2163 read_mem = 1;
2164 break;
2165 default:
2166 return ERROR_COMMAND_SYNTAX_ERROR;
2169 switch (CMD_NAME[3]) {
2170 case 'x':
2171 mem_type = MEM_X;
2172 break;
2173 case 'y':
2174 mem_type = MEM_Y;
2175 break;
2176 case 'p':
2177 mem_type = MEM_P;
2178 break;
2179 default:
2180 return ERROR_COMMAND_SYNTAX_ERROR;
2183 if (CMD_ARGC > 0)
2184 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], address);
2186 if (read_mem == 0) {
2187 if (CMD_ARGC < 2)
2188 return ERROR_COMMAND_SYNTAX_ERROR;
2189 if (CMD_ARGC > 1)
2190 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], pattern);
2191 if (CMD_ARGC > 2)
2192 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], count);
2195 if (read_mem == 1) {
2196 if (CMD_ARGC < 1)
2197 return ERROR_COMMAND_SYNTAX_ERROR;
2198 if (CMD_ARGC > 1)
2199 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], count);
2202 buffer = calloc(count, sizeof(uint32_t));
2204 if (read_mem == 1) {
2205 err = dsp563xx_read_memory(target, mem_type, address, sizeof(uint32_t),
2206 count, buffer);
2207 if (err == ERROR_OK)
2208 handle_md_output(CMD_CTX, target, address, sizeof(uint32_t), count, buffer);
2210 } else {
2211 b = buffer;
2213 for (i = 0; i < count; i++) {
2214 target_buffer_set_u32(target, b, pattern);
2215 b += 4;
2218 err = dsp563xx_write_memory(target,
2219 mem_type,
2220 address,
2221 sizeof(uint32_t),
2222 count,
2223 buffer);
2226 free(buffer);
2228 return err;
2231 static const struct command_registration dsp563xx_command_handlers[] = {
2233 .name = "mwwx",
2234 .handler = dsp563xx_mem_command,
2235 .mode = COMMAND_EXEC,
2236 .help = "write x memory words",
2237 .usage = "address value [count]",
2240 .name = "mwwy",
2241 .handler = dsp563xx_mem_command,
2242 .mode = COMMAND_EXEC,
2243 .help = "write y memory words",
2244 .usage = "address value [count]",
2247 .name = "mwwp",
2248 .handler = dsp563xx_mem_command,
2249 .mode = COMMAND_EXEC,
2250 .help = "write p memory words",
2251 .usage = "address value [count]",
2254 .name = "mdwx",
2255 .handler = dsp563xx_mem_command,
2256 .mode = COMMAND_EXEC,
2257 .help = "display x memory words",
2258 .usage = "address [count]",
2261 .name = "mdwy",
2262 .handler = dsp563xx_mem_command,
2263 .mode = COMMAND_EXEC,
2264 .help = "display y memory words",
2265 .usage = "address [count]",
2268 .name = "mdwp",
2269 .handler = dsp563xx_mem_command,
2270 .mode = COMMAND_EXEC,
2271 .help = "display p memory words",
2272 .usage = "address [count]",
2275 * Watchpoint commands
2278 .name = "wpp",
2279 .handler = dsp563xx_add_watchpoint_command,
2280 .mode = COMMAND_EXEC,
2281 .help = "Create p memspace watchpoint",
2282 .usage = "(>|<|=|!) (r|w|a) address",
2285 .name = "wpx",
2286 .handler = dsp563xx_add_watchpoint_command,
2287 .mode = COMMAND_EXEC,
2288 .help = "Create x memspace watchpoint",
2289 .usage = "(>|<|=|!) (r|w|a) address",
2292 .name = "wpy",
2293 .handler = dsp563xx_add_watchpoint_command,
2294 .mode = COMMAND_EXEC,
2295 .help = "Create y memspace watchpoint",
2296 .usage = "(>|<|=|!) (r|w|a) address",
2299 .name = "rwpc",
2300 .handler = dsp563xx_remove_watchpoint_command,
2301 .mode = COMMAND_EXEC,
2302 .help = "remove watchpoint custom",
2303 .usage = " ",
2305 COMMAND_REGISTRATION_DONE
2308 /** Holds methods for DSP563XX targets. */
2309 struct target_type dsp563xx_target = {
2310 .name = "dsp563xx",
2312 .poll = dsp563xx_poll,
2313 .arch_state = dsp563xx_arch_state,
2315 .get_gdb_reg_list = dsp563xx_get_gdb_reg_list,
2317 .halt = dsp563xx_halt,
2318 .resume = dsp563xx_resume,
2319 .step = dsp563xx_step,
2321 .assert_reset = dsp563xx_assert_reset,
2322 .deassert_reset = dsp563xx_deassert_reset,
2324 .read_memory = dsp563xx_read_memory_default,
2325 .write_memory = dsp563xx_write_memory_default,
2327 .read_buffer = dsp563xx_read_buffer_default,
2328 .write_buffer = dsp563xx_write_buffer_default,
2330 .run_algorithm = dsp563xx_run_algorithm,
2332 .add_breakpoint = dsp563xx_add_breakpoint,
2333 .remove_breakpoint = dsp563xx_remove_breakpoint,
2334 .add_watchpoint = dsp563xx_add_watchpoint,
2335 .remove_watchpoint = dsp563xx_remove_watchpoint,
2337 .commands = dsp563xx_command_handlers,
2338 .target_create = dsp563xx_target_create,
2339 .init_target = dsp563xx_init_target,
2340 .examine = dsp563xx_examine,