hla: Make consistent parameter naming
[openocd.git] / src / target / nds32_cmd.c
blob8970fd7eec70c2911dc3bfbc55c1b337a6a7d1c5
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 ***************************************************************************/
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
25 #include <helper/command.h>
26 #include "nds32.h"
27 #include "nds32_aice.h"
28 #include "nds32_disassembler.h"
30 extern struct nds32_edm_operation nds32_edm_ops[NDS32_EDM_OPERATION_MAX_NUM];
31 extern uint32_t nds32_edm_ops_num;
33 static const char *const NDS_MEMORY_ACCESS_NAME[] = {
34 "BUS",
35 "CPU",
38 static const char *const NDS_MEMORY_SELECT_NAME[] = {
39 "AUTO",
40 "MEM",
41 "ILM",
42 "DLM",
45 COMMAND_HANDLER(handle_nds32_dssim_command)
47 struct target *target = get_current_target(CMD_CTX);
48 struct nds32 *nds32 = target_to_nds32(target);
50 if (!is_nds32(nds32)) {
51 command_print(CMD_CTX, "current target isn't an Andes core");
52 return ERROR_FAIL;
55 if (CMD_ARGC > 0) {
56 if (strcmp(CMD_ARGV[0], "on") == 0)
57 nds32->step_isr_enable = true;
58 if (strcmp(CMD_ARGV[0], "off") == 0)
59 nds32->step_isr_enable = false;
62 command_print(CMD_CTX, "%s: $INT_MASK.DSSIM: %d", target_name(target),
63 nds32->step_isr_enable);
65 return ERROR_OK;
68 COMMAND_HANDLER(handle_nds32_memory_access_command)
70 struct target *target = get_current_target(CMD_CTX);
71 struct nds32 *nds32 = target_to_nds32(target);
72 struct aice_port_s *aice = target_to_aice(target);
73 struct nds32_memory *memory = &(nds32->memory);
75 if (!is_nds32(nds32)) {
76 command_print(CMD_CTX, "current target isn't an Andes core");
77 return ERROR_FAIL;
80 if (CMD_ARGC > 0) {
81 if (strcmp(CMD_ARGV[0], "bus") == 0)
82 memory->access_channel = NDS_MEMORY_ACC_BUS;
83 else if (strcmp(CMD_ARGV[0], "cpu") == 0)
84 memory->access_channel = NDS_MEMORY_ACC_CPU;
85 else /* default access channel is NDS_MEMORY_ACC_CPU */
86 memory->access_channel = NDS_MEMORY_ACC_CPU;
88 LOG_DEBUG("memory access channel is changed to %s",
89 NDS_MEMORY_ACCESS_NAME[memory->access_channel]);
91 aice_memory_access(aice, memory->access_channel);
92 } else {
93 command_print(CMD_CTX, "%s: memory access channel: %s",
94 target_name(target),
95 NDS_MEMORY_ACCESS_NAME[memory->access_channel]);
98 return ERROR_OK;
101 COMMAND_HANDLER(handle_nds32_memory_mode_command)
103 struct target *target = get_current_target(CMD_CTX);
104 struct nds32 *nds32 = target_to_nds32(target);
105 struct aice_port_s *aice = target_to_aice(target);
107 if (!is_nds32(nds32)) {
108 command_print(CMD_CTX, "current target isn't an Andes core");
109 return ERROR_FAIL;
112 if (CMD_ARGC > 0) {
114 if (nds32->edm.access_control == false) {
115 command_print(CMD_CTX, "%s does not support ACC_CTL. "
116 "Set memory mode to MEMORY", target_name(target));
117 nds32->memory.mode = NDS_MEMORY_SELECT_MEM;
118 } else if (nds32->edm.direct_access_local_memory == false) {
119 command_print(CMD_CTX, "%s does not support direct access "
120 "local memory. Set memory mode to MEMORY",
121 target_name(target));
122 nds32->memory.mode = NDS_MEMORY_SELECT_MEM;
124 /* set to ACC_CTL */
125 aice_memory_mode(aice, nds32->memory.mode);
126 } else {
127 if (strcmp(CMD_ARGV[0], "auto") == 0) {
128 nds32->memory.mode = NDS_MEMORY_SELECT_AUTO;
129 } else if (strcmp(CMD_ARGV[0], "mem") == 0) {
130 nds32->memory.mode = NDS_MEMORY_SELECT_MEM;
131 } else if (strcmp(CMD_ARGV[0], "ilm") == 0) {
132 if (nds32->memory.ilm_base == 0)
133 command_print(CMD_CTX, "%s does not support ILM",
134 target_name(target));
135 else
136 nds32->memory.mode = NDS_MEMORY_SELECT_ILM;
137 } else if (strcmp(CMD_ARGV[0], "dlm") == 0) {
138 if (nds32->memory.dlm_base == 0)
139 command_print(CMD_CTX, "%s does not support DLM",
140 target_name(target));
141 else
142 nds32->memory.mode = NDS_MEMORY_SELECT_DLM;
145 /* set to ACC_CTL */
146 aice_memory_mode(aice, nds32->memory.mode);
150 command_print(CMD_CTX, "%s: memory mode: %s",
151 target_name(target),
152 NDS_MEMORY_SELECT_NAME[nds32->memory.mode]);
154 return ERROR_OK;
157 COMMAND_HANDLER(handle_nds32_cache_command)
159 struct target *target = get_current_target(CMD_CTX);
160 struct nds32 *nds32 = target_to_nds32(target);
161 struct aice_port_s *aice = target_to_aice(target);
162 struct nds32_cache *icache = &(nds32->memory.icache);
163 struct nds32_cache *dcache = &(nds32->memory.dcache);
164 int result;
166 if (!is_nds32(nds32)) {
167 command_print(CMD_CTX, "current target isn't an Andes core");
168 return ERROR_FAIL;
171 if (CMD_ARGC > 0) {
173 if (strcmp(CMD_ARGV[0], "invalidate") == 0) {
174 if ((dcache->line_size != 0) && (dcache->enable == true)) {
175 /* D$ write back */
176 result = aice_cache_ctl(aice, AICE_CACHE_CTL_L1D_WBALL, 0);
177 if (result != ERROR_OK) {
178 command_print(CMD_CTX, "%s: Write back data cache...failed",
179 target_name(target));
180 return result;
183 command_print(CMD_CTX, "%s: Write back data cache...done",
184 target_name(target));
186 /* D$ invalidate */
187 result = aice_cache_ctl(aice, AICE_CACHE_CTL_L1D_INVALALL, 0);
188 if (result != ERROR_OK) {
189 command_print(CMD_CTX, "%s: Invalidate data cache...failed",
190 target_name(target));
191 return result;
194 command_print(CMD_CTX, "%s: Invalidate data cache...done",
195 target_name(target));
196 } else {
197 if (dcache->line_size == 0)
198 command_print(CMD_CTX, "%s: No data cache",
199 target_name(target));
200 else
201 command_print(CMD_CTX, "%s: Data cache disabled",
202 target_name(target));
205 if ((icache->line_size != 0) && (icache->enable == true)) {
206 /* I$ invalidate */
207 result = aice_cache_ctl(aice, AICE_CACHE_CTL_L1I_INVALALL, 0);
208 if (result != ERROR_OK) {
209 command_print(CMD_CTX, "%s: Invalidate instruction cache...failed",
210 target_name(target));
211 return result;
214 command_print(CMD_CTX, "%s: Invalidate instruction cache...done",
215 target_name(target));
216 } else {
217 if (icache->line_size == 0)
218 command_print(CMD_CTX, "%s: No instruction cache",
219 target_name(target));
220 else
221 command_print(CMD_CTX, "%s: Instruction cache disabled",
222 target_name(target));
224 } else
225 command_print(CMD_CTX, "No valid parameter");
228 return ERROR_OK;
231 COMMAND_HANDLER(handle_nds32_icache_command)
233 struct target *target = get_current_target(CMD_CTX);
234 struct nds32 *nds32 = target_to_nds32(target);
235 struct aice_port_s *aice = target_to_aice(target);
236 struct nds32_cache *icache = &(nds32->memory.icache);
237 int result;
239 if (!is_nds32(nds32)) {
240 command_print(CMD_CTX, "current target isn't an Andes core");
241 return ERROR_FAIL;
244 if (CMD_ARGC > 0) {
246 if (icache->line_size == 0) {
247 command_print(CMD_CTX, "%s: No instruction cache",
248 target_name(target));
249 return ERROR_OK;
252 if (strcmp(CMD_ARGV[0], "invalidate") == 0) {
253 if (icache->enable == true) {
254 /* I$ invalidate */
255 result = aice_cache_ctl(aice, AICE_CACHE_CTL_L1I_INVALALL, 0);
256 if (result != ERROR_OK) {
257 command_print(CMD_CTX, "%s: Invalidate instruction cache...failed",
258 target_name(target));
259 return result;
262 command_print(CMD_CTX, "%s: Invalidate instruction cache...done",
263 target_name(target));
264 } else {
265 command_print(CMD_CTX, "%s: Instruction cache disabled",
266 target_name(target));
268 } else if (strcmp(CMD_ARGV[0], "enable") == 0) {
269 uint32_t value;
270 nds32_get_mapped_reg(nds32, IR8, &value);
271 nds32_set_mapped_reg(nds32, IR8, value | 0x1);
272 } else if (strcmp(CMD_ARGV[0], "disable") == 0) {
273 uint32_t value;
274 nds32_get_mapped_reg(nds32, IR8, &value);
275 nds32_set_mapped_reg(nds32, IR8, value & ~0x1);
276 } else if (strcmp(CMD_ARGV[0], "dump") == 0) {
277 /* TODO: dump cache content */
278 } else {
279 command_print(CMD_CTX, "%s: No valid parameter", target_name(target));
283 return ERROR_OK;
286 COMMAND_HANDLER(handle_nds32_dcache_command)
288 struct target *target = get_current_target(CMD_CTX);
289 struct nds32 *nds32 = target_to_nds32(target);
290 struct aice_port_s *aice = target_to_aice(target);
291 struct nds32_cache *dcache = &(nds32->memory.dcache);
292 int result;
294 if (!is_nds32(nds32)) {
295 command_print(CMD_CTX, "current target isn't an Andes core");
296 return ERROR_FAIL;
299 if (CMD_ARGC > 0) {
301 if (dcache->line_size == 0) {
302 command_print(CMD_CTX, "%s: No data cache", target_name(target));
303 return ERROR_OK;
306 if (strcmp(CMD_ARGV[0], "invalidate") == 0) {
307 if (dcache->enable == true) {
308 /* D$ write back */
309 result = aice_cache_ctl(aice, AICE_CACHE_CTL_L1D_WBALL, 0);
310 if (result != ERROR_OK) {
311 command_print(CMD_CTX, "%s: Write back data cache...failed",
312 target_name(target));
313 return result;
316 command_print(CMD_CTX, "%s: Write back data cache...done",
317 target_name(target));
319 /* D$ invalidate */
320 result = aice_cache_ctl(aice, AICE_CACHE_CTL_L1D_INVALALL, 0);
321 if (result != ERROR_OK) {
322 command_print(CMD_CTX, "%s: Invalidate data cache...failed",
323 target_name(target));
324 return result;
327 command_print(CMD_CTX, "%s: Invalidate data cache...done",
328 target_name(target));
329 } else {
330 command_print(CMD_CTX, "%s: Data cache disabled",
331 target_name(target));
333 } else if (strcmp(CMD_ARGV[0], "enable") == 0) {
334 uint32_t value;
335 nds32_get_mapped_reg(nds32, IR8, &value);
336 nds32_set_mapped_reg(nds32, IR8, value | 0x2);
337 } else if (strcmp(CMD_ARGV[0], "disable") == 0) {
338 uint32_t value;
339 nds32_get_mapped_reg(nds32, IR8, &value);
340 nds32_set_mapped_reg(nds32, IR8, value & ~0x2);
341 } else if (strcmp(CMD_ARGV[0], "dump") == 0) {
342 /* TODO: dump cache content */
343 } else {
344 command_print(CMD_CTX, "%s: No valid parameter", target_name(target));
348 return ERROR_OK;
351 COMMAND_HANDLER(handle_nds32_auto_break_command)
353 struct target *target = get_current_target(CMD_CTX);
354 struct nds32 *nds32 = target_to_nds32(target);
356 if (!is_nds32(nds32)) {
357 command_print(CMD_CTX, "current target isn't an Andes core");
358 return ERROR_FAIL;
361 if (CMD_ARGC > 0) {
362 if (strcmp(CMD_ARGV[0], "on") == 0)
363 nds32->auto_convert_hw_bp = true;
364 if (strcmp(CMD_ARGV[0], "off") == 0)
365 nds32->auto_convert_hw_bp = false;
368 if (nds32->auto_convert_hw_bp)
369 command_print(CMD_CTX, "%s: convert sw break to hw break on ROM: on",
370 target_name(target));
371 else
372 command_print(CMD_CTX, "%s: convert sw break to hw break on ROM: off",
373 target_name(target));
375 return ERROR_OK;
378 COMMAND_HANDLER(handle_nds32_virtual_hosting_command)
380 struct target *target = get_current_target(CMD_CTX);
381 struct nds32 *nds32 = target_to_nds32(target);
383 if (!is_nds32(nds32)) {
384 command_print(CMD_CTX, "current target isn't an Andes core");
385 return ERROR_FAIL;
388 if (CMD_ARGC > 0) {
389 if (strcmp(CMD_ARGV[0], "on") == 0)
390 nds32->virtual_hosting = true;
391 if (strcmp(CMD_ARGV[0], "off") == 0)
392 nds32->virtual_hosting = false;
395 if (nds32->virtual_hosting)
396 command_print(CMD_CTX, "%s: virtual hosting: on", target_name(target));
397 else
398 command_print(CMD_CTX, "%s: virtual hosting: off", target_name(target));
400 return ERROR_OK;
403 COMMAND_HANDLER(handle_nds32_global_stop_command)
405 struct target *target = get_current_target(CMD_CTX);
406 struct nds32 *nds32 = target_to_nds32(target);
408 if (!is_nds32(nds32)) {
409 command_print(CMD_CTX, "current target isn't an Andes core");
410 return ERROR_FAIL;
413 if (CMD_ARGC > 0) {
414 if (strcmp(CMD_ARGV[0], "on") == 0)
415 nds32->global_stop = true;
416 if (strcmp(CMD_ARGV[0], "off") == 0)
417 nds32->global_stop = false;
420 if (nds32->global_stop)
421 LOG_INFO("%s: global stop: on", target_name(target));
422 else
423 LOG_INFO("%s: global stop: off", target_name(target));
425 return ERROR_OK;
428 COMMAND_HANDLER(handle_nds32_soft_reset_halt_command)
430 struct target *target = get_current_target(CMD_CTX);
431 struct nds32 *nds32 = target_to_nds32(target);
433 if (!is_nds32(nds32)) {
434 command_print(CMD_CTX, "current target isn't an Andes core");
435 return ERROR_FAIL;
438 if (CMD_ARGC > 0) {
439 if (strcmp(CMD_ARGV[0], "on") == 0)
440 nds32->soft_reset_halt = true;
441 if (strcmp(CMD_ARGV[0], "off") == 0)
442 nds32->soft_reset_halt = false;
445 if (nds32->soft_reset_halt)
446 LOG_INFO("%s: soft-reset-halt: on", target_name(target));
447 else
448 LOG_INFO("%s: soft-reset-halt: off", target_name(target));
450 return ERROR_OK;
453 COMMAND_HANDLER(handle_nds32_boot_time_command)
455 struct target *target = get_current_target(CMD_CTX);
456 struct nds32 *nds32 = target_to_nds32(target);
458 if (!is_nds32(nds32)) {
459 command_print(CMD_CTX, "current target isn't an Andes core");
460 return ERROR_FAIL;
463 if (CMD_ARGC > 0)
464 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], nds32->boot_time);
466 return ERROR_OK;
469 COMMAND_HANDLER(handle_nds32_login_edm_passcode_command)
471 struct target *target = get_current_target(CMD_CTX);
472 struct nds32 *nds32 = target_to_nds32(target);
474 if (!is_nds32(nds32)) {
475 command_print(CMD_CTX, "current target isn't an Andes core");
476 return ERROR_FAIL;
479 nds32->edm_passcode = strdup(CMD_ARGV[0]);
481 return ERROR_OK;
484 COMMAND_HANDLER(handle_nds32_login_edm_operation_command)
486 struct target *target = get_current_target(CMD_CTX);
487 struct nds32 *nds32 = target_to_nds32(target);
489 if (!is_nds32(nds32)) {
490 command_print(CMD_CTX, "current target isn't an Andes core");
491 return ERROR_FAIL;
494 if (CMD_ARGC > 1) {
496 uint32_t misc_reg_no;
497 uint32_t data;
499 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], misc_reg_no);
500 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], data);
502 if (nds32_edm_ops_num >= NDS32_EDM_OPERATION_MAX_NUM)
503 return ERROR_FAIL;
505 /* Just save the operation. Execute it in nds32_login() */
506 nds32_edm_ops[nds32_edm_ops_num].reg_no = misc_reg_no;
507 nds32_edm_ops[nds32_edm_ops_num].value = data;
508 nds32_edm_ops_num++;
509 } else
510 return ERROR_FAIL;
512 return ERROR_OK;
515 COMMAND_HANDLER(handle_nds32_reset_halt_as_init_command)
517 struct target *target = get_current_target(CMD_CTX);
518 struct nds32 *nds32 = target_to_nds32(target);
520 if (!is_nds32(nds32)) {
521 command_print(CMD_CTX, "current target isn't an Andes core");
522 return ERROR_FAIL;
525 if (CMD_ARGC > 0) {
526 if (strcmp(CMD_ARGV[0], "on") == 0)
527 nds32->reset_halt_as_examine = true;
528 if (strcmp(CMD_ARGV[0], "off") == 0)
529 nds32->reset_halt_as_examine = false;
532 return ERROR_OK;
535 COMMAND_HANDLER(handle_nds32_keep_target_edm_ctl_command)
537 struct target *target = get_current_target(CMD_CTX);
538 struct nds32 *nds32 = target_to_nds32(target);
540 if (!is_nds32(nds32)) {
541 command_print(CMD_CTX, "current target isn't an Andes core");
542 return ERROR_FAIL;
545 if (CMD_ARGC > 0) {
546 if (strcmp(CMD_ARGV[0], "on") == 0)
547 nds32->keep_target_edm_ctl = true;
548 if (strcmp(CMD_ARGV[0], "off") == 0)
549 nds32->keep_target_edm_ctl = false;
552 return ERROR_OK;
555 COMMAND_HANDLER(handle_nds32_decode_command)
557 struct target *target = get_current_target(CMD_CTX);
558 struct nds32 *nds32 = target_to_nds32(target);
560 if (!is_nds32(nds32)) {
561 command_print(CMD_CTX, "current target isn't an Andes core");
562 return ERROR_FAIL;
565 if (CMD_ARGC > 1) {
567 uint32_t addr;
568 uint32_t insn_count;
569 uint32_t opcode;
570 uint32_t read_addr;
571 uint32_t i;
572 struct nds32_instruction instruction;
574 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
575 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], insn_count);
577 read_addr = addr;
578 i = 0;
579 while (i < insn_count) {
580 if (ERROR_OK != nds32_read_opcode(nds32, read_addr, &opcode))
581 return ERROR_FAIL;
582 if (ERROR_OK != nds32_evaluate_opcode(nds32, opcode,
583 read_addr, &instruction))
584 return ERROR_FAIL;
586 command_print(CMD_CTX, "%s", instruction.text);
588 read_addr += instruction.instruction_size;
589 i++;
591 } else if (CMD_ARGC == 1) {
593 uint32_t addr;
594 uint32_t opcode;
595 struct nds32_instruction instruction;
597 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], addr);
599 if (ERROR_OK != nds32_read_opcode(nds32, addr, &opcode))
600 return ERROR_FAIL;
601 if (ERROR_OK != nds32_evaluate_opcode(nds32, opcode, addr, &instruction))
602 return ERROR_FAIL;
604 command_print(CMD_CTX, "%s", instruction.text);
605 } else
606 return ERROR_FAIL;
608 return ERROR_OK;
611 COMMAND_HANDLER(handle_nds32_word_access_mem_command)
613 struct target *target = get_current_target(CMD_CTX);
614 struct nds32 *nds32 = target_to_nds32(target);
616 if (!is_nds32(nds32)) {
617 command_print(CMD_CTX, "current target isn't an Andes core");
618 return ERROR_FAIL;
621 if (CMD_ARGC > 0) {
622 if (strcmp(CMD_ARGV[0], "on") == 0)
623 nds32->word_access_mem = true;
624 if (strcmp(CMD_ARGV[0], "off") == 0)
625 nds32->word_access_mem = false;
628 return ERROR_OK;
631 COMMAND_HANDLER(handle_nds32_query_target_command)
633 struct target *target = get_current_target(CMD_CTX);
634 struct nds32 *nds32 = target_to_nds32(target);
636 if (!is_nds32(nds32)) {
637 command_print(CMD_CTX, "current target isn't an Andes core");
638 return ERROR_FAIL;
641 command_print(CMD_CTX, "OCD");
643 return ERROR_OK;
646 COMMAND_HANDLER(handle_nds32_query_endian_command)
648 struct target *target = get_current_target(CMD_CTX);
649 struct nds32 *nds32 = target_to_nds32(target);
651 if (!is_nds32(nds32)) {
652 command_print(CMD_CTX, "current target isn't an Andes core");
653 return ERROR_FAIL;
656 uint32_t value_psw;
657 nds32_get_mapped_reg(nds32, IR0, &value_psw);
659 if (value_psw & 0x20)
660 command_print(CMD_CTX, "%s: BE", target_name(target));
661 else
662 command_print(CMD_CTX, "%s: LE", target_name(target));
664 return ERROR_OK;
667 COMMAND_HANDLER(handle_nds32_query_cpuid_command)
669 struct target *target = get_current_target(CMD_CTX);
670 struct nds32 *nds32 = target_to_nds32(target);
672 if (!is_nds32(nds32)) {
673 command_print(CMD_CTX, "current target isn't an Andes core");
674 return ERROR_FAIL;
677 command_print(CMD_CTX, "CPUID: %s", target_name(target));
679 return ERROR_OK;
682 static int jim_nds32_bulk_write(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
684 const char *cmd_name = Jim_GetString(argv[0], NULL);
686 Jim_GetOptInfo goi;
687 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
689 if (goi.argc < 3) {
690 Jim_SetResultFormatted(goi.interp,
691 "usage: %s <address> <count> <data>", cmd_name);
692 return JIM_ERR;
695 int e;
696 jim_wide address;
697 e = Jim_GetOpt_Wide(&goi, &address);
698 if (e != JIM_OK)
699 return e;
701 jim_wide count;
702 e = Jim_GetOpt_Wide(&goi, &count);
703 if (e != JIM_OK)
704 return e;
706 uint32_t *data = malloc(count * sizeof(uint32_t));
707 jim_wide i;
708 for (i = 0; i < count; i++) {
709 jim_wide tmp;
710 e = Jim_GetOpt_Wide(&goi, &tmp);
711 if (e != JIM_OK)
712 return e;
713 data[i] = (uint32_t)tmp;
716 /* all args must be consumed */
717 if (goi.argc != 0)
718 return JIM_ERR;
720 struct target *target = Jim_CmdPrivData(goi.interp);
721 int result;
723 result = target_write_buffer(target, address, count * 4, (const uint8_t *)data);
725 free(data);
727 return result;
730 static int jim_nds32_multi_write(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
732 const char *cmd_name = Jim_GetString(argv[0], NULL);
734 Jim_GetOptInfo goi;
735 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
737 if (goi.argc < 3) {
738 Jim_SetResultFormatted(goi.interp,
739 "usage: %s # of pairs [<address> <data>]+", cmd_name);
740 return JIM_ERR;
743 int e;
744 jim_wide num_of_pairs;
745 e = Jim_GetOpt_Wide(&goi, &num_of_pairs);
746 if (e != JIM_OK)
747 return e;
749 struct target *target = Jim_CmdPrivData(goi.interp);
750 struct aice_port_s *aice = target_to_aice(target);
751 int result;
752 uint32_t address;
753 uint32_t data;
754 jim_wide i;
756 aice_set_command_mode(aice, AICE_COMMAND_MODE_PACK);
757 for (i = 0; i < num_of_pairs; i++) {
758 jim_wide tmp;
759 e = Jim_GetOpt_Wide(&goi, &tmp);
760 if (e != JIM_OK)
761 break;
762 address = (uint32_t)tmp;
764 e = Jim_GetOpt_Wide(&goi, &tmp);
765 if (e != JIM_OK)
766 break;
767 data = (uint32_t)tmp;
769 result = target_write_buffer(target, address, 4, (const uint8_t *)&data);
770 if (result != ERROR_OK)
771 break;
773 aice_set_command_mode(aice, AICE_COMMAND_MODE_NORMAL);
775 /* all args must be consumed */
776 if (goi.argc != 0)
777 return JIM_ERR;
779 return ERROR_OK;
782 static int jim_nds32_bulk_read(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
784 const char *cmd_name = Jim_GetString(argv[0], NULL);
786 Jim_GetOptInfo goi;
787 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
789 if (goi.argc < 2) {
790 Jim_SetResultFormatted(goi.interp,
791 "usage: %s <address> <count>", cmd_name);
792 return JIM_ERR;
795 int e;
796 jim_wide address;
797 e = Jim_GetOpt_Wide(&goi, &address);
798 if (e != JIM_OK)
799 return e;
801 jim_wide count;
802 e = Jim_GetOpt_Wide(&goi, &count);
803 if (e != JIM_OK)
804 return e;
806 /* all args must be consumed */
807 if (goi.argc != 0)
808 return JIM_ERR;
810 struct target *target = Jim_CmdPrivData(goi.interp);
811 uint32_t *data = malloc(count * sizeof(uint32_t));
812 int result;
813 result = target_read_buffer(target, address, count * 4, (uint8_t *)data);
814 char data_str[11];
816 jim_wide i;
817 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
818 for (i = 0; i < count; i++) {
819 sprintf(data_str, "0x%08" PRIx32 " ", data[i]);
820 Jim_AppendStrings(interp, Jim_GetResult(interp), data_str, NULL);
823 free(data);
825 return result;
828 static int jim_nds32_read_edm_sr(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
830 const char *cmd_name = Jim_GetString(argv[0], NULL);
832 Jim_GetOptInfo goi;
833 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
835 if (goi.argc < 1) {
836 Jim_SetResultFormatted(goi.interp,
837 "usage: %s <edm_sr_name>", cmd_name);
838 return JIM_ERR;
841 int e;
842 char *edm_sr_name;
843 int edm_sr_name_len;
844 e = Jim_GetOpt_String(&goi, &edm_sr_name, &edm_sr_name_len);
845 if (e != JIM_OK)
846 return e;
848 /* all args must be consumed */
849 if (goi.argc != 0)
850 return JIM_ERR;
852 uint32_t edm_sr_number;
853 uint32_t edm_sr_value;
854 if (strncmp(edm_sr_name, "edm_dtr", edm_sr_name_len) == 0)
855 edm_sr_number = NDS_EDM_SR_EDM_DTR;
856 else if (strncmp(edm_sr_name, "edmsw", edm_sr_name_len) == 0)
857 edm_sr_number = NDS_EDM_SR_EDMSW;
858 else
859 return ERROR_FAIL;
861 struct target *target = Jim_CmdPrivData(goi.interp);
862 struct aice_port_s *aice = target_to_aice(target);
863 char data_str[11];
865 aice_read_debug_reg(aice, edm_sr_number, &edm_sr_value);
867 sprintf(data_str, "0x%08" PRIx32, edm_sr_value);
868 Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
869 Jim_AppendStrings(interp, Jim_GetResult(interp), data_str, NULL);
871 return ERROR_OK;
874 static int jim_nds32_write_edm_sr(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
876 const char *cmd_name = Jim_GetString(argv[0], NULL);
878 Jim_GetOptInfo goi;
879 Jim_GetOpt_Setup(&goi, interp, argc - 1, argv + 1);
881 if (goi.argc < 2) {
882 Jim_SetResultFormatted(goi.interp,
883 "usage: %s <edm_sr_name> <value>", cmd_name);
884 return JIM_ERR;
887 int e;
888 char *edm_sr_name;
889 int edm_sr_name_len;
890 e = Jim_GetOpt_String(&goi, &edm_sr_name, &edm_sr_name_len);
891 if (e != JIM_OK)
892 return e;
894 jim_wide value;
895 e = Jim_GetOpt_Wide(&goi, &value);
896 if (e != JIM_OK)
897 return e;
899 /* all args must be consumed */
900 if (goi.argc != 0)
901 return JIM_ERR;
903 uint32_t edm_sr_number;
904 if (strncmp(edm_sr_name, "edm_dtr", edm_sr_name_len) == 0)
905 edm_sr_number = NDS_EDM_SR_EDM_DTR;
906 else
907 return ERROR_FAIL;
909 struct target *target = Jim_CmdPrivData(goi.interp);
910 struct aice_port_s *aice = target_to_aice(target);
912 aice_write_debug_reg(aice, edm_sr_number, value);
914 return ERROR_OK;
917 static const struct command_registration nds32_query_command_handlers[] = {
919 .name = "target",
920 .handler = handle_nds32_query_target_command,
921 .mode = COMMAND_EXEC,
922 .usage = "",
923 .help = "reply 'OCD' for gdb to identify server-side is OpenOCD",
926 .name = "endian",
927 .handler = handle_nds32_query_endian_command,
928 .mode = COMMAND_EXEC,
929 .usage = "",
930 .help = "query target endian",
933 .name = "cpuid",
934 .handler = handle_nds32_query_cpuid_command,
935 .mode = COMMAND_EXEC,
936 .usage = "",
937 .help = "query CPU ID",
940 COMMAND_REGISTRATION_DONE
943 static const struct command_registration nds32_exec_command_handlers[] = {
945 .name = "dssim",
946 .handler = handle_nds32_dssim_command,
947 .mode = COMMAND_EXEC,
948 .usage = "['on'|'off']",
949 .help = "display/change $INT_MASK.DSSIM status",
952 .name = "mem_access",
953 .handler = handle_nds32_memory_access_command,
954 .mode = COMMAND_EXEC,
955 .usage = "['bus'|'cpu']",
956 .help = "display/change memory access channel",
959 .name = "mem_mode",
960 .handler = handle_nds32_memory_mode_command,
961 .mode = COMMAND_EXEC,
962 .usage = "['auto'|'mem'|'ilm'|'dlm']",
963 .help = "display/change memory mode",
966 .name = "cache",
967 .handler = handle_nds32_cache_command,
968 .mode = COMMAND_EXEC,
969 .usage = "['invalidate']",
970 .help = "cache control",
973 .name = "icache",
974 .handler = handle_nds32_icache_command,
975 .mode = COMMAND_EXEC,
976 .usage = "['invalidate'|'enable'|'disable'|'dump']",
977 .help = "icache control",
980 .name = "dcache",
981 .handler = handle_nds32_dcache_command,
982 .mode = COMMAND_EXEC,
983 .usage = "['invalidate'|'enable'|'disable'|'dump']",
984 .help = "dcache control",
987 .name = "auto_break",
988 .handler = handle_nds32_auto_break_command,
989 .mode = COMMAND_EXEC,
990 .usage = "['on'|'off']",
991 .help = "convert software breakpoints to hardware breakpoints if needed",
994 .name = "virtual_hosting",
995 .handler = handle_nds32_virtual_hosting_command,
996 .mode = COMMAND_ANY,
997 .usage = "['on'|'off']",
998 .help = "turn on/off virtual hosting",
1001 .name = "global_stop",
1002 .handler = handle_nds32_global_stop_command,
1003 .mode = COMMAND_ANY,
1004 .usage = "['on'|'off']",
1005 .help = "turn on/off global stop. After turning on, every load/store" \
1006 "instructions will be stopped to check memory access.",
1009 .name = "soft_reset_halt",
1010 .handler = handle_nds32_soft_reset_halt_command,
1011 .mode = COMMAND_ANY,
1012 .usage = "['on'|'off']",
1013 .help = "as issuing rest-halt, to use soft-reset-halt or not." \
1014 "the feature is for backward-compatible.",
1017 .name = "boot_time",
1018 .handler = handle_nds32_boot_time_command,
1019 .mode = COMMAND_CONFIG,
1020 .usage = "milliseconds",
1021 .help = "set the period to wait after srst.",
1024 .name = "login_edm_passcode",
1025 .handler = handle_nds32_login_edm_passcode_command,
1026 .mode = COMMAND_CONFIG,
1027 .usage = "passcode",
1028 .help = "set EDM passcode for secure MCU debugging.",
1031 .name = "login_edm_operation",
1032 .handler = handle_nds32_login_edm_operation_command,
1033 .mode = COMMAND_CONFIG,
1034 .usage = "login_edm_operation misc_reg_no value",
1035 .help = "add EDM operations for secure MCU debugging.",
1038 .name = "reset_halt_as_init",
1039 .handler = handle_nds32_reset_halt_as_init_command,
1040 .mode = COMMAND_CONFIG,
1041 .usage = "['on'|'off']",
1042 .help = "reset halt as openocd init.",
1045 .name = "keep_target_edm_ctl",
1046 .handler = handle_nds32_keep_target_edm_ctl_command,
1047 .mode = COMMAND_CONFIG,
1048 .usage = "['on'|'off']",
1049 .help = "Backup/Restore target EDM_CTL register.",
1052 .name = "decode",
1053 .handler = handle_nds32_decode_command,
1054 .mode = COMMAND_EXEC,
1055 .usage = "address icount",
1056 .help = "decode instruction.",
1059 .name = "word_access_mem",
1060 .handler = handle_nds32_word_access_mem_command,
1061 .mode = COMMAND_ANY,
1062 .usage = "['on'|'off']",
1063 .help = "Always use word-aligned address to access memory.",
1066 .name = "bulk_write",
1067 .jim_handler = jim_nds32_bulk_write,
1068 .mode = COMMAND_EXEC,
1069 .help = "Write multiple 32-bit words to target memory",
1070 .usage = "address count data",
1073 .name = "multi_write",
1074 .jim_handler = jim_nds32_multi_write,
1075 .mode = COMMAND_EXEC,
1076 .help = "Write multiple addresses/words to target memory",
1077 .usage = "num_of_pairs [address data]+",
1080 .name = "bulk_read",
1081 .jim_handler = jim_nds32_bulk_read,
1082 .mode = COMMAND_EXEC,
1083 .help = "Read multiple 32-bit words from target memory",
1084 .usage = "address count",
1087 .name = "read_edmsr",
1088 .jim_handler = jim_nds32_read_edm_sr,
1089 .mode = COMMAND_EXEC,
1090 .help = "Read EDM system register",
1091 .usage = "['edmsw'|'edm_dtr']",
1094 .name = "write_edmsr",
1095 .jim_handler = jim_nds32_write_edm_sr,
1096 .mode = COMMAND_EXEC,
1097 .help = "Write EDM system register",
1098 .usage = "['edm_dtr'] value",
1101 .name = "query",
1102 .mode = COMMAND_EXEC,
1103 .help = "Andes query command group",
1104 .usage = "",
1105 .chain = nds32_query_command_handlers,
1108 COMMAND_REGISTRATION_DONE
1111 const struct command_registration nds32_command_handlers[] = {
1113 .name = "nds",
1114 .mode = COMMAND_ANY,
1115 .help = "Andes command group",
1116 .usage = "",
1117 .chain = nds32_exec_command_handlers,
1119 COMMAND_REGISTRATION_DONE