Skip to content

Commit ded5f59

Browse files
committed
target/riscv: Fix some timeout check order
Reorder the timeout check and conditional judgment to ensure that timeout check is not wasted.
1 parent a68695b commit ded5f59

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

src/target/riscv/riscv-013.c

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1887,19 +1887,21 @@ static int reset_dm(struct target *target)
18871887

18881888
const time_t start = time(NULL);
18891889
LOG_TARGET_DEBUG(target, "Waiting for the DM to acknowledge reset.");
1890-
do {
1890+
while (1) {
18911891
result = dm_read(target, &dmcontrol, DM_DMCONTROL);
18921892
if (result != ERROR_OK)
18931893
return result;
1894-
1894+
if (!get_field32(dmcontrol, DM_DMCONTROL_DMACTIVE)) {
1895+
LOG_TARGET_DEBUG(target, "The DM has just become deactivated (dmcontrol.dmactive: 1 -> 0).");
1896+
break;
1897+
}
18951898
if (time(NULL) - start > riscv_get_command_timeout_sec()) {
18961899
LOG_TARGET_ERROR(target, "DM didn't acknowledge reset in %d s. "
18971900
"Increase the timeout with 'riscv set_command_timeout_sec'.",
18981901
riscv_get_command_timeout_sec());
18991902
return ERROR_TIMEOUT_REACHED;
19001903
}
1901-
} while (get_field32(dmcontrol, DM_DMCONTROL_DMACTIVE));
1902-
LOG_TARGET_DEBUG(target, "DM reset initiated.");
1904+
}
19031905
}
19041906
/* TODO: Move the code above into `deactivate_dm()` function
19051907
* (a logical counterpart to activate_dm()). */
@@ -2999,10 +3001,20 @@ static int deassert_reset(struct target *target)
29993001
RISCV_DELAY_BASE);
30003002
time_t start = time(NULL);
30013003
LOG_TARGET_DEBUG(target, "Waiting for hart to come out of reset.");
3002-
do {
3004+
while (1) {
30033005
result = dmstatus_read(target, &dmstatus, true);
30043006
if (result != ERROR_OK)
30053007
return result;
3008+
/* Certain debug modules, like the one in GD32VF103
3009+
* MCUs, violate the specification's requirement that
3010+
* each hart is in "exactly one of four states" and,
3011+
* during reset, report harts as both unavailable and
3012+
* halted/running. To work around this, we check for
3013+
* the absence of the unavailable state rather than
3014+
* the presence of any other state. */
3015+
if (!get_field(dmstatus, DM_DMSTATUS_ALLUNAVAIL) ||
3016+
get_field(dmstatus, DM_DMSTATUS_ALLHAVERESET))
3017+
break;
30063018

30073019
if (time(NULL) - start > riscv_get_command_timeout_sec()) {
30083020
LOG_TARGET_ERROR(target, "Hart didn't leave reset in %ds; "
@@ -3013,15 +3025,7 @@ static int deassert_reset(struct target *target)
30133025
get_field(dmstatus, DM_DMSTATUS_ALLHAVERESET) ? "true" : "false");
30143026
return ERROR_TIMEOUT_REACHED;
30153027
}
3016-
/* Certain debug modules, like the one in GD32VF103
3017-
* MCUs, violate the specification's requirement that
3018-
* each hart is in "exactly one of four states" and,
3019-
* during reset, report harts as both unavailable and
3020-
* halted/running. To work around this, we check for
3021-
* the absence of the unavailable state rather than
3022-
* the presence of any other state. */
3023-
} while (get_field(dmstatus, DM_DMSTATUS_ALLUNAVAIL) &&
3024-
!get_field(dmstatus, DM_DMSTATUS_ALLHAVERESET));
3028+
}
30253029

30263030
riscv_scan_set_delay(&info->learned_delays, RISCV_DELAY_BASE,
30273031
orig_base_delay);

0 commit comments

Comments
 (0)