Skip to content

Commit 7dad25b

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 ee94350 commit 7dad25b

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

src/target/riscv_debug.c

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,31 @@ void riscv_dmi_init(riscv_dmi_s *const dmi)
302302
/* The first DM is always at base address 0 */
303303
uint32_t base_addr = 0U;
304304
do {
305+
/* Turn on DM before trying to read version */
306+
if (!riscv_dmi_write(dmi, base_addr + RV_DM_CONTROL, RV_DM_CTRL_ACTIVE)) {
307+
DEBUG_ERROR("error turning on DM!\n");
308+
return;
309+
}
310+
311+
/* After changing the value of dm_active, the debugger must poll dmcontrol
312+
* until dm_active has taken the requested value */
313+
bool dm_active = false;
314+
uint32_t dm_control = 0U;
315+
uint8_t counter = 0U;
316+
while (!dm_active) {
317+
if (!riscv_dmi_read(dmi, base_addr + RV_DM_CONTROL, &dm_control)) {
318+
DEBUG_ERROR("error turning on DM!\n");
319+
return;
320+
}
321+
dm_active = dm_control & 1U;
322+
if (++counter >= 100U) {
323+
DEBUG_ERROR("Timeout while trying to turn on DM\n");
324+
return;
325+
}
326+
}
327+
305328
/* Read out the DM's status register */
306-
uint32_t dm_status = 0;
329+
uint32_t dm_status = 0U;
307330
if (!riscv_dmi_read(dmi, base_addr + RV_DM_STATUS, &dm_status)) {
308331
/* If we fail to read the status register, abort */
309332
break;

0 commit comments

Comments
 (0)