nds32: add new target type nds32_v2, nds32_v3, nds32_v3m
[openocd.git] / src / target / nds32_v3_common.c
1 /***************************************************************************
2 * Copyright (C) 2013 Andes Technology *
3 * Hsiangkai Wang <hkwang@andestech.com> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
19 ***************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "breakpoints.h"
26 #include "nds32_reg.h"
27 #include "nds32_disassembler.h"
28 #include "nds32.h"
29 #include "nds32_aice.h"
30 #include "nds32_v3_common.h"
31
32 static struct nds32_v3_common_callback *v3_common_callback;
33
34 static int nds32_v3_register_mapping(struct nds32 *nds32, int reg_no)
35 {
36 if (reg_no == PC)
37 return IR11;
38
39 return reg_no;
40 }
41
42 static int nds32_v3_get_debug_reason(struct nds32 *nds32, uint32_t *reason)
43 {
44 uint32_t edmsw;
45 struct aice_port_s *aice = target_to_aice(nds32->target);
46 aice_read_debug_reg(aice, NDS_EDM_SR_EDMSW, &edmsw);
47
48 *reason = (edmsw >> 12) & 0x0F;
49
50 return ERROR_OK;
51 }
52
53 /**
54 * Save processor state. This is called after a HALT instruction
55 * succeeds, and on other occasions the processor enters debug mode
56 * (breakpoint, watchpoint, etc).
57 */
58 static int nds32_v3_debug_entry(struct nds32 *nds32, bool enable_watchpoint)
59 {
60 LOG_DEBUG("nds32_v3_debug_entry");
61
62 jtag_poll_set_enabled(false);
63
64 enum target_state backup_state = nds32->target->state;
65 nds32->target->state = TARGET_HALTED;
66
67 if (nds32->init_arch_info_after_halted == false) {
68 /* init architecture info according to config registers */
69 CHECK_RETVAL(nds32_config(nds32));
70
71 nds32->init_arch_info_after_halted = true;
72 }
73
74 /* REVISIT entire cache should already be invalid !!! */
75 register_cache_invalidate(nds32->core_cache);
76
77 /* deactivate all hardware breakpoints */
78 CHECK_RETVAL(v3_common_callback->deactivate_hardware_breakpoint(nds32->target));
79
80 if (enable_watchpoint)
81 CHECK_RETVAL(v3_common_callback->deactivate_hardware_watchpoint(nds32->target));
82
83 if (ERROR_OK != nds32_examine_debug_reason(nds32)) {
84 nds32->target->state = backup_state;
85
86 /* re-activate all hardware breakpoints & watchpoints */
87 CHECK_RETVAL(v3_common_callback->activate_hardware_breakpoint(nds32->target));
88
89 if (enable_watchpoint)
90 CHECK_RETVAL(v3_common_callback->activate_hardware_watchpoint(nds32->target));
91
92 jtag_poll_set_enabled(true);
93
94 return ERROR_FAIL;
95 }
96
97 /* Save registers. */
98 nds32_full_context(nds32);
99
100 /* check interrupt level */
101 v3_common_callback->check_interrupt_stack(nds32);
102
103 return ERROR_OK;
104 }
105
106 /**
107 * Restore processor state.
108 */
109 static int nds32_v3_leave_debug_state(struct nds32 *nds32, bool enable_watchpoint)
110 {
111 LOG_DEBUG("nds32_v3_leave_debug_state");
112
113 struct target *target = nds32->target;
114
115 /* activate all hardware breakpoints */
116 CHECK_RETVAL(v3_common_callback->activate_hardware_breakpoint(target));
117
118 if (enable_watchpoint) {
119 /* activate all watchpoints */
120 CHECK_RETVAL(v3_common_callback->activate_hardware_watchpoint(target));
121 }
122
123 /* restore interrupt stack */
124 v3_common_callback->restore_interrupt_stack(nds32);
125
126 /* REVISIT once we start caring about MMU and cache state,
127 * address it here ...
128 */
129
130 /* restore PSW, PC, and R0 ... after flushing any modified
131 * registers.
132 */
133 CHECK_RETVAL(nds32_restore_context(target));
134
135 /* enable polling */
136 jtag_poll_set_enabled(true);
137
138 return ERROR_OK;
139 }
140
141 static int nds32_v3_get_exception_address(struct nds32 *nds32,
142 uint32_t *address, uint32_t reason)
143 {
144 LOG_DEBUG("nds32_v3_get_exception_address");
145
146 struct aice_port_s *aice = target_to_aice(nds32->target);
147 struct target *target = nds32->target;
148 uint32_t edmsw;
149 uint32_t edm_cfg;
150 uint32_t match_bits;
151 uint32_t match_count;
152 int32_t i;
153 static int32_t number_of_hard_break;
154
155 if (number_of_hard_break == 0) {
156 aice_read_debug_reg(aice, NDS_EDM_SR_EDM_CFG, &edm_cfg);
157 number_of_hard_break = (edm_cfg & 0x7) + 1;
158 }
159
160 aice_read_debug_reg(aice, NDS_EDM_SR_EDMSW, &edmsw);
161 /* clear matching bits (write-one-clear) */
162 aice_write_debug_reg(aice, NDS_EDM_SR_EDMSW, edmsw);
163 match_bits = (edmsw >> 4) & 0xFF;
164 match_count = 0;
165 for (i = 0 ; i < number_of_hard_break ; i++) {
166 if (match_bits & (1 << i)) {
167 aice_read_debug_reg(aice, NDS_EDM_SR_BPA0 + i, address);
168 match_count++;
169 }
170 }
171
172 if (match_count > 1) { /* multiple hits */
173 *address = 0;
174 return ERROR_OK;
175 } else if (match_count == 1) {
176 uint32_t val_pc;
177 uint32_t opcode;
178 struct nds32_instruction instruction;
179 struct watchpoint *wp;
180 bool hit;
181
182 nds32_get_mapped_reg(nds32, PC, &val_pc);
183
184 if ((NDS32_DEBUG_DATA_ADDR_WATCHPOINT_NEXT_PRECISE == reason) ||
185 (NDS32_DEBUG_DATA_VALUE_WATCHPOINT_NEXT_PRECISE == reason)) {
186 if (edmsw & 0x4) /* check EDMSW.IS_16BIT */
187 val_pc -= 2;
188 else
189 val_pc -= 4;
190 }
191
192 nds32_read_opcode(nds32, val_pc, &opcode);
193 nds32_evaluate_opcode(nds32, opcode, val_pc, &instruction);
194
195 LOG_DEBUG("PC: 0x%08x, access start: 0x%08x, end: 0x%08x", val_pc,
196 instruction.access_start, instruction.access_end);
197
198 /* check if multiple hits in the access range */
199 uint32_t in_range_watch_count = 0;
200 for (wp = target->watchpoints; wp; wp = wp->next) {
201 if ((instruction.access_start <= wp->address) &&
202 (wp->address < instruction.access_end))
203 in_range_watch_count++;
204 }
205 if (in_range_watch_count > 1) {
206 /* Hit LSMW instruction. */
207 *address = 0;
208 return ERROR_OK;
209 }
210
211 /* dispel false match */
212 hit = false;
213 for (wp = target->watchpoints; wp; wp = wp->next) {
214 if (((*address ^ wp->address) & (~wp->mask)) == 0) {
215 uint32_t watch_start;
216 uint32_t watch_end;
217
218 watch_start = wp->address;
219 watch_end = wp->address + wp->length;
220
221 if ((watch_end <= instruction.access_start) ||
222 (instruction.access_end <= watch_start))
223 continue;
224
225 hit = true;
226 break;
227 }
228 }
229
230 if (hit)
231 return ERROR_OK;
232 else
233 return ERROR_FAIL;
234 } else if (match_count == 0) {
235 /* global stop is precise exception */
236 if ((NDS32_DEBUG_LOAD_STORE_GLOBAL_STOP == reason) && nds32->global_stop) {
237 /* parse instruction to get correct access address */
238 uint32_t val_pc;
239 uint32_t opcode;
240 struct nds32_instruction instruction;
241
242 nds32_get_mapped_reg(nds32, PC, &val_pc);
243 nds32_read_opcode(nds32, val_pc, &opcode);
244 nds32_evaluate_opcode(nds32, opcode, val_pc, &instruction);
245
246 *address = instruction.access_start;
247
248 return ERROR_OK;
249 }
250 }
251
252 *address = 0xFFFFFFFF;
253 return ERROR_FAIL;
254 }
255
256 void nds32_v3_common_register_callback(struct nds32_v3_common_callback *callback)
257 {
258 v3_common_callback = callback;
259 }
260
261 /** target_type functions: */
262 /* target request support */
263 int nds32_v3_target_request_data(struct target *target,
264 uint32_t size, uint8_t *buffer)
265 {
266 /* AndesCore could use DTR register to communicate with OpenOCD
267 * to output messages
268 * Target data will be put in buffer
269 * The format of DTR is as follow
270 * DTR[31:16] => length, DTR[15:8] => size, DTR[7:0] => target_req_cmd
271 * target_req_cmd has three possible values:
272 * TARGET_REQ_TRACEMSG
273 * TARGET_REQ_DEBUGMSG
274 * TARGET_REQ_DEBUGCHAR
275 * if size == 0, target will call target_asciimsg(),
276 * else call target_hexmsg()
277 */
278 LOG_WARNING("Not implemented: %s", __func__);
279
280 return ERROR_OK;
281 }
282
283 int nds32_v3_soft_reset_halt(struct target *target)
284 {
285 struct aice_port_s *aice = target_to_aice(target);
286 return aice_assert_srst(aice, AICE_RESET_HOLD);
287 }
288
289 int nds32_v3_checksum_memory(struct target *target,
290 uint32_t address, uint32_t count, uint32_t *checksum)
291 {
292 LOG_WARNING("Not implemented: %s", __func__);
293
294 return ERROR_FAIL;
295 }
296
297 int nds32_v3_target_create_common(struct target *target, struct nds32 *nds32)
298 {
299 nds32->register_map = nds32_v3_register_mapping;
300 nds32->get_debug_reason = nds32_v3_get_debug_reason;
301 nds32->enter_debug_state = nds32_v3_debug_entry;
302 nds32->leave_debug_state = nds32_v3_leave_debug_state;
303 nds32->get_watched_address = nds32_v3_get_exception_address;
304
305 /* Init target->arch_info in nds32_init_arch_info().
306 * After this, user could use target_to_nds32() to get nds32 object */
307 nds32_init_arch_info(target, nds32);
308
309 return ERROR_OK;
310 }
311
312 int nds32_v3_run_algorithm(struct target *target,
313 int num_mem_params,
314 struct mem_param *mem_params,
315 int num_reg_params,
316 struct reg_param *reg_params,
317 uint32_t entry_point,
318 uint32_t exit_point,
319 int timeout_ms,
320 void *arch_info)
321 {
322 LOG_WARNING("Not implemented: %s", __func__);
323
324 return ERROR_FAIL;
325 }
326
327 int nds32_v3_read_buffer(struct target *target, uint32_t address,
328 uint32_t size, uint8_t *buffer)
329 {
330 struct nds32 *nds32 = target_to_nds32(target);
331 struct nds32_memory *memory = &(nds32->memory);
332
333 if ((NDS_MEMORY_ACC_CPU == memory->access_channel) &&
334 (target->state != TARGET_HALTED)) {
335 LOG_WARNING("target was not halted");
336 return ERROR_TARGET_NOT_HALTED;
337 }
338
339 uint32_t physical_address;
340 /* BUG: If access range crosses multiple pages, the translation will not correct
341 * for second page or so. */
342
343 /* When DEX is set to one, hardware will enforce the following behavior without
344 * modifying the corresponding control bits in PSW.
345 *
346 * Disable all interrupts
347 * Become superuser mode
348 * Turn off IT/DT
349 * Use MMU_CFG.DE as the data access endian
350 * Use MMU_CFG.DRDE as the device register access endian if MMU_CTL.DREE is asserted
351 * Disable audio special features
352 * Disable inline function call
353 *
354 * Because hardware will turn off IT/DT by default, it MUST translate virtual address
355 * to physical address.
356 */
357 if (ERROR_OK == target->type->virt2phys(target, address, &physical_address))
358 address = physical_address;
359 else
360 return ERROR_FAIL;
361
362 return nds32_read_buffer(target, address, size, buffer);
363 }
364
365 int nds32_v3_write_buffer(struct target *target, uint32_t address,
366 uint32_t size, const uint8_t *buffer)
367 {
368 struct nds32 *nds32 = target_to_nds32(target);
369 struct nds32_memory *memory = &(nds32->memory);
370
371 if ((NDS_MEMORY_ACC_CPU == memory->access_channel) &&
372 (target->state != TARGET_HALTED)) {
373 LOG_WARNING("target was not halted");
374 return ERROR_TARGET_NOT_HALTED;
375 }
376
377 uint32_t physical_address;
378 /* BUG: If access range crosses multiple pages, the translation will not correct
379 * for second page or so. */
380
381 /* When DEX is set to one, hardware will enforce the following behavior without
382 * modifying the corresponding control bits in PSW.
383 *
384 * Disable all interrupts
385 * Become superuser mode
386 * Turn off IT/DT
387 * Use MMU_CFG.DE as the data access endian
388 * Use MMU_CFG.DRDE as the device register access endian if MMU_CTL.DREE is asserted
389 * Disable audio special features
390 * Disable inline function call
391 *
392 * Because hardware will turn off IT/DT by default, it MUST translate virtual address
393 * to physical address.
394 */
395 if (ERROR_OK == target->type->virt2phys(target, address, &physical_address))
396 address = physical_address;
397 else
398 return ERROR_FAIL;
399
400 return nds32_write_buffer(target, address, size, buffer);
401 }
402
403 int nds32_v3_read_memory(struct target *target, uint32_t address,
404 uint32_t size, uint32_t count, uint8_t *buffer)
405 {
406 struct nds32 *nds32 = target_to_nds32(target);
407 struct nds32_memory *memory = &(nds32->memory);
408
409 if ((NDS_MEMORY_ACC_CPU == memory->access_channel) &&
410 (target->state != TARGET_HALTED)) {
411 LOG_WARNING("target was not halted");
412 return ERROR_TARGET_NOT_HALTED;
413 }
414
415 uint32_t physical_address;
416 /* BUG: If access range crosses multiple pages, the translation will not correct
417 * for second page or so. */
418
419 /* When DEX is set to one, hardware will enforce the following behavior without
420 * modifying the corresponding control bits in PSW.
421 *
422 * Disable all interrupts
423 * Become superuser mode
424 * Turn off IT/DT
425 * Use MMU_CFG.DE as the data access endian
426 * Use MMU_CFG.DRDE as the device register access endian if MMU_CTL.DREE is asserted
427 * Disable audio special features
428 * Disable inline function call
429 *
430 * Because hardware will turn off IT/DT by default, it MUST translate virtual address
431 * to physical address.
432 */
433 if (ERROR_OK == target->type->virt2phys(target, address, &physical_address))
434 address = physical_address;
435 else
436 return ERROR_FAIL;
437
438 int result;
439
440 result = nds32_read_memory(target, address, size, count, buffer);
441
442 return result;
443 }
444
445 int nds32_v3_write_memory(struct target *target, uint32_t address,
446 uint32_t size, uint32_t count, const uint8_t *buffer)
447 {
448 struct nds32 *nds32 = target_to_nds32(target);
449 struct nds32_memory *memory = &(nds32->memory);
450
451 if ((NDS_MEMORY_ACC_CPU == memory->access_channel) &&
452 (target->state != TARGET_HALTED)) {
453 LOG_WARNING("target was not halted");
454 return ERROR_TARGET_NOT_HALTED;
455 }
456
457 uint32_t physical_address;
458 /* BUG: If access range crosses multiple pages, the translation will not correct
459 * for second page or so. */
460
461 /* When DEX is set to one, hardware will enforce the following behavior without
462 * modifying the corresponding control bits in PSW.
463 *
464 * Disable all interrupts
465 * Become superuser mode
466 * Turn off IT/DT
467 * Use MMU_CFG.DE as the data access endian
468 * Use MMU_CFG.DRDE as the device register access endian if MMU_CTL.DREE is asserted
469 * Disable audio special features
470 * Disable inline function call
471 *
472 * Because hardware will turn off IT/DT by default, it MUST translate virtual address
473 * to physical address.
474 */
475 if (ERROR_OK == target->type->virt2phys(target, address, &physical_address))
476 address = physical_address;
477 else
478 return ERROR_FAIL;
479
480 return nds32_write_memory(target, address, size, count, buffer);
481 }
482
483 int nds32_v3_init_target(struct command_context *cmd_ctx,
484 struct target *target)
485 {
486 /* Initialize anything we can set up without talking to the target */
487 struct nds32 *nds32 = target_to_nds32(target);
488
489 nds32_init(nds32);
490
491 return ERROR_OK;
492 }

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)