@@ -534,16 +534,50 @@ static int dm_write(struct target *target, uint32_t address, uint32_t value)
534
534
return dmi_write (target , riscv013_get_dmi_address (target , address ), value );
535
535
}
536
536
537
- static bool check_dbgbase_exists (struct target * target )
537
+ static int wait_for_dm_activation (struct target * target , uint32_t dm_base_addr )
538
+ {
539
+ uint32_t dmcontrol = 0 ;
540
+ const time_t start = time (NULL );
541
+
542
+ LOG_TARGET_DEBUG (target , "Waiting for the DM to become active." );
543
+ do {
544
+ if (dmi_read (target , & dmcontrol , DM_DMCONTROL + dm_base_addr ) != ERROR_OK )
545
+ return ERROR_FAIL ;
546
+
547
+ if (time (NULL ) - start > riscv_get_command_timeout_sec ()) {
548
+ LOG_TARGET_ERROR (target , "Debug Module (at address dbgbase=0x%" PRIx32 ") did not become active in %d s. "
549
+ "Increase the timeout with 'riscv set_command_timeout_sec'." ,
550
+ dm_base_addr , riscv_get_command_timeout_sec ());
551
+ return ERROR_TIMEOUT_REACHED ;
552
+ }
553
+ } while (!get_field32 (dmcontrol , DM_DMCONTROL_DMACTIVE ));
554
+ LOG_TARGET_DEBUG (target , "DM has become active." );
555
+ return ERROR_OK ;
556
+ }
557
+
558
+ static int check_dbgbase_exists (struct target * target )
538
559
{
539
560
uint32_t next_dm = 0 ;
540
561
unsigned int count = 1 ;
562
+ uint32_t dmcontrol = 0 ;
541
563
542
564
LOG_TARGET_DEBUG (target , "Searching for DM with DMI base address (dbgbase) = 0x%x" , target -> dbgbase );
543
565
while (1 ) {
544
566
uint32_t current_dm = next_dm ;
545
567
if (current_dm == target -> dbgbase )
546
- return true;
568
+ return ERROR_OK ;
569
+
570
+ if (dmi_read (target , & dmcontrol , DM_DMCONTROL + current_dm ) != ERROR_OK )
571
+ break ;
572
+ if (!get_field32 (dmcontrol , DM_DMCONTROL_DMACTIVE )) {
573
+ LOG_TARGET_DEBUG (target , "Activating the DM with DMI base address (dbgbase) = 0x%x" , current_dm );
574
+ if (dmi_write (target , DM_DMCONTROL + current_dm , DM_DMCONTROL_DMACTIVE ) != ERROR_OK )
575
+ break ;
576
+
577
+ if (wait_for_dm_activation (target , current_dm ) != ERROR_OK )
578
+ break ;
579
+ }
580
+
547
581
if (dmi_read (target , & next_dm , DM_NEXTDM + current_dm ) != ERROR_OK )
548
582
break ;
549
583
LOG_TARGET_DEBUG (target , "dm @ 0x%x --> nextdm=0x%x" , current_dm , next_dm );
@@ -558,7 +592,7 @@ static bool check_dbgbase_exists(struct target *target)
558
592
break ;
559
593
}
560
594
}
561
- return false ;
595
+ return ERROR_FAIL ;
562
596
}
563
597
564
598
static int dmstatus_read (struct target * target , uint32_t * dmstatus ,
@@ -1852,21 +1886,9 @@ static int reset_dm(struct target *target)
1852
1886
if (result != ERROR_OK )
1853
1887
return result ;
1854
1888
1855
- const time_t start = time (NULL );
1856
- LOG_TARGET_DEBUG (target , "Waiting for the DM to acknowledge reset." );
1857
- do {
1858
- result = dm_read (target , & dmcontrol , DM_DMCONTROL );
1859
- if (result != ERROR_OK )
1860
- return result ;
1861
-
1862
- if (time (NULL ) - start > riscv_get_command_timeout_sec ()) {
1863
- LOG_TARGET_ERROR (target , "DM didn't acknowledge reset in %d s. "
1864
- "Increase the timeout with 'riscv set_command_timeout_sec'." ,
1865
- riscv_get_command_timeout_sec ());
1866
- return ERROR_TIMEOUT_REACHED ;
1867
- }
1868
- } while (get_field32 (dmcontrol , DM_DMCONTROL_DMACTIVE ));
1869
- LOG_TARGET_DEBUG (target , "DM reset initiated." );
1889
+ result = wait_for_dm_activation (target , dm -> base );
1890
+ if (result != ERROR_OK )
1891
+ return result ;
1870
1892
}
1871
1893
1872
1894
LOG_TARGET_DEBUG (target , "Activating the DM." );
@@ -2043,7 +2065,7 @@ static int examine(struct target *target)
2043
2065
info -> abits , RISCV013_DTMCS_ABITS_MIN );
2044
2066
}
2045
2067
2046
- if (! check_dbgbase_exists (target )) {
2068
+ if (check_dbgbase_exists (target ) != ERROR_OK ) {
2047
2069
LOG_TARGET_ERROR (target , "Could not find debug module with DMI base address (dbgbase) = 0x%x" , target -> dbgbase );
2048
2070
return ERROR_FAIL ;
2049
2071
}
0 commit comments