Skip to content

Commit a198879

Browse files
committed
riscv_debug: Turn on DM before trying to read version
According to the RISC-V debug specification, DM must be turned on before accessing DM registers (like reading version of DM). This fixes attaching to the target.
1 parent efcbe07 commit a198879

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/target/riscv_debug.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,30 @@ void riscv_dmi_init(riscv_dmi_s *const dmi)
304304
do {
305305
/* Read out the DM's status register */
306306
uint32_t dm_status = 0;
307+
308+
/* Turn on DM before trying to read version */
309+
if (!riscv_dmi_write(dmi, base_addr + RV_DM_CONTROL, RV_DM_CTRL_ACTIVE)) {
310+
DEBUG_ERROR("error turning on DM!\n");
311+
return;
312+
}
313+
314+
/* After changing the value of dm_active, the debugger must poll dmcontrol
315+
* until dm_active has taken the requested value */
316+
bool dm_active = false;
317+
uint32_t dm_control = 0;
318+
while (!dm_active) {
319+
if (!riscv_dmi_read(dmi, base_addr + RV_DM_CONTROL, &dm_control)) {
320+
DEBUG_ERROR("error turning on DM!\n");
321+
return;
322+
}
323+
dm_active = dm_control & 1;
324+
uint8_t counter = 0;
325+
if (++counter >= 100) {
326+
DEBUG_ERROR("Timeout while trying to turn on DM\n");
327+
return;
328+
}
329+
}
330+
307331
if (!riscv_dmi_read(dmi, base_addr + RV_DM_STATUS, &dm_status)) {
308332
/* If we fail to read the status register, abort */
309333
break;

0 commit comments

Comments
 (0)