@@ -534,16 +534,48 @@ 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 , "Activating the DM with DMI base address (dbgbase) = 0x%x" , dm_base_addr );
543
+ if (dmi_write (target , DM_DMCONTROL + dm_base_addr , DM_DMCONTROL_DMACTIVE ) != ERROR_OK )
544
+ return ERROR_FAIL ;
545
+
546
+ LOG_TARGET_DEBUG (target , "Waiting for the DM to become active." );
547
+ do {
548
+ if (dmi_read (target , & dmcontrol , DM_DMCONTROL + dm_base_addr ) != ERROR_OK )
549
+ return ERROR_FAIL ;
550
+
551
+ if (time (NULL ) - start > riscv_get_command_timeout_sec ()) {
552
+ LOG_TARGET_ERROR (target , "Debug Module (at address dbgbase=0x%" PRIx32 ") did not become active in %d s. "
553
+ "Increase the timeout with 'riscv set_command_timeout_sec'." ,
554
+ dm_base_addr , riscv_get_command_timeout_sec ());
555
+ return ERROR_TIMEOUT_REACHED ;
556
+ }
557
+ } while (!get_field32 (dmcontrol , DM_DMCONTROL_DMACTIVE ));
558
+ LOG_TARGET_DEBUG (target , "DM has become active." );
559
+ return ERROR_OK ;
560
+ }
561
+
562
+ static int check_dbgbase_exists (struct target * target )
538
563
{
539
564
uint32_t next_dm = 0 ;
540
565
unsigned int count = 1 ;
566
+ uint32_t dmcontrol = 0 ;
541
567
542
568
LOG_TARGET_DEBUG (target , "Searching for DM with DMI base address (dbgbase) = 0x%x" , target -> dbgbase );
543
569
while (1 ) {
544
570
uint32_t current_dm = next_dm ;
545
571
if (current_dm == target -> dbgbase )
546
- return true;
572
+ return ERROR_OK ;
573
+
574
+ if (dmi_read (target , & dmcontrol , DM_DMCONTROL + current_dm ) != ERROR_OK )
575
+ break ;
576
+ if (!get_field32 (dmcontrol , DM_DMCONTROL_DMACTIVE ) && wait_for_dm_activation (target , current_dm ) != ERROR_OK )
577
+ break ;
578
+
547
579
if (dmi_read (target , & next_dm , DM_NEXTDM + current_dm ) != ERROR_OK )
548
580
break ;
549
581
LOG_TARGET_DEBUG (target , "dm @ 0x%x --> nextdm=0x%x" , current_dm , next_dm );
@@ -558,7 +590,7 @@ static bool check_dbgbase_exists(struct target *target)
558
590
break ;
559
591
}
560
592
}
561
- return false ;
593
+ return ERROR_FAIL ;
562
594
}
563
595
564
596
static int dmstatus_read (struct target * target , uint32_t * dmstatus ,
@@ -1869,26 +1901,11 @@ static int reset_dm(struct target *target)
1869
1901
LOG_TARGET_DEBUG (target , "DM reset initiated." );
1870
1902
}
1871
1903
1872
- LOG_TARGET_DEBUG (target , "Activating the DM." );
1873
- result = dm_write (target , DM_DMCONTROL , DM_DMCONTROL_DMACTIVE );
1904
+ LOG_TARGET_DEBUG (target , "Waiting for the DM to come out of reset ." );
1905
+ result = wait_for_dm_activation (target , dm -> base );
1874
1906
if (result != ERROR_OK )
1875
1907
return result ;
1876
1908
1877
- const time_t start = time (NULL );
1878
- LOG_TARGET_DEBUG (target , "Waiting for the DM to come out of reset." );
1879
- do {
1880
- result = dm_read (target , & dmcontrol , DM_DMCONTROL );
1881
- if (result != ERROR_OK )
1882
- return result ;
1883
-
1884
- if (time (NULL ) - start > riscv_get_command_timeout_sec ()) {
1885
- LOG_TARGET_ERROR (target , "Debug Module did not become active in %d s. "
1886
- "Increase the timeout with 'riscv set_command_timeout_sec'." ,
1887
- riscv_get_command_timeout_sec ());
1888
- return ERROR_TIMEOUT_REACHED ;
1889
- }
1890
- } while (!get_field32 (dmcontrol , DM_DMCONTROL_DMACTIVE ));
1891
-
1892
1909
LOG_TARGET_DEBUG (target , "DM successfully reset." );
1893
1910
dm -> was_reset = true;
1894
1911
return ERROR_OK ;
@@ -2043,7 +2060,7 @@ static int examine(struct target *target)
2043
2060
info -> abits , RISCV013_DTMCS_ABITS_MIN );
2044
2061
}
2045
2062
2046
- if (! check_dbgbase_exists (target )) {
2063
+ if (check_dbgbase_exists (target ) != ERROR_OK ) {
2047
2064
LOG_TARGET_ERROR (target , "Could not find debug module with DMI base address (dbgbase) = 0x%x" , target -> dbgbase );
2048
2065
return ERROR_FAIL ;
2049
2066
}
0 commit comments