Skip to content

Commit 73d5899

Browse files
committed
target/riscv: make sure target is halted when reset_halt is set
- some MCU will need certain period of time to be halted after ndmreset is issued, so in deassert_reset, it needs to make sure MCU is halted before clearing DM_DMCONTROL_HALTREQ. Change-Id: I6d7ef7b9b33aff65cb996968fb28cd62e3e1fb16 Signed-off-by: Ryan QIAN <[email protected]>
1 parent 88fe568 commit 73d5899

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/target/riscv/riscv-013.c

+29
Original file line numberDiff line numberDiff line change
@@ -2928,18 +2928,47 @@ static int deassert_reset(struct target *target)
29282928
control = 0;
29292929
control = set_field(control, DM_DMCONTROL_DMACTIVE, 1);
29302930
control = set_field(control, DM_DMCONTROL_ACKHAVERESET, 1);
2931+
/* Ack reset with DM_DMCONTROL_HALTREQ for those MCUs which need a
2932+
* period of time to be halted after reset is released */
2933+
control = set_field(control, DM_DMCONTROL_HALTREQ, target->reset_halt ? 1 : 0);
29312934
control = set_dmcontrol_hartsel(control, info->index);
29322935
result = dm_write(target, DM_DMCONTROL, control);
29332936
if (result != ERROR_OK)
29342937
return result;
29352938

29362939
if (target->reset_halt) {
2940+
/* Wait for all harts to halt for those MCUs mentioned above */
2941+
start = time(NULL);
2942+
do {
2943+
result = dmstatus_read(target, &dmstatus, true);
2944+
if (result != ERROR_OK)
2945+
return result;
2946+
2947+
if (time(NULL) - start > riscv_get_command_timeout_sec()) {
2948+
LOG_TARGET_ERROR(target, "Hart didn't halt after reset in %ds; "
2949+
"dmstatus=0x%x (anyhalted=%s, allhalted=%s); "
2950+
"Increase the timeout with riscv set_command_timeout_sec.",
2951+
riscv_get_command_timeout_sec(), dmstatus,
2952+
get_field(dmstatus, DM_DMSTATUS_ANYHALTED) ? "true" : "false",
2953+
get_field(dmstatus, DM_DMSTATUS_ALLHALTED) ? "true" : "false");
2954+
return ERROR_TIMEOUT_REACHED;
2955+
}
2956+
} while (!get_field(dmstatus, DM_DMSTATUS_ALLHALTED));
29372957
target->state = TARGET_HALTED;
29382958
target->debug_reason = DBG_REASON_DBGRQ;
29392959
} else {
29402960
target->state = TARGET_RUNNING;
29412961
target->debug_reason = DBG_REASON_NOTHALTED;
29422962
}
2963+
2964+
/* clear DM_DMCONTROL_HALTREQ */
2965+
control = 0;
2966+
control = set_field(control, DM_DMCONTROL_DMACTIVE, 1);
2967+
control = set_dmcontrol_hartsel(control, info->index);
2968+
result = dm_write(target, DM_DMCONTROL, control);
2969+
if (result != ERROR_OK)
2970+
return result;
2971+
29432972
info->dcsr_ebreak_is_set = dcsr_ebreak_config_equals_reset_value(target);
29442973
return ERROR_OK;
29452974
}

0 commit comments

Comments
 (0)