@@ -538,12 +538,39 @@ static bool check_dbgbase_exists(struct target *target)
538538{
539539 uint32_t next_dm = 0 ;
540540 unsigned int count = 1 ;
541+ uint32_t dmcontrol = 0 ;
541542
542543 LOG_TARGET_DEBUG (target , "Searching for DM with DMI base address (dbgbase) = 0x%x" , target -> dbgbase );
543544 while (1 ) {
544545 uint32_t current_dm = next_dm ;
545546 if (current_dm == target -> dbgbase )
546547 return true;
548+
549+ if (dmi_read (target , & dmcontrol , DM_DMCONTROL + current_dm ) != ERROR_OK )
550+ break ;
551+ if (!get_field32 (dmcontrol , DM_DMCONTROL_DMACTIVE )) {
552+ LOG_TARGET_DEBUG (target , "Activating the DM with DMI base address (dbgbase) = 0x%x" , current_dm );
553+ if (dmi_write (target , DM_DMCONTROL + current_dm , DM_DMCONTROL_DMACTIVE ) != ERROR_OK )
554+ break ;
555+
556+ const time_t start = time (NULL );
557+ LOG_TARGET_DEBUG (target , "Waiting for the DM to become active." );
558+ do {
559+ if (dmi_read (target , & dmcontrol , DM_DMCONTROL + current_dm ) != ERROR_OK )
560+ return false;
561+
562+ if (time (NULL ) - start > riscv_get_command_timeout_sec ()) {
563+ LOG_TARGET_ERROR (target , "Debug Module did not become active in %d s. "
564+ "Increase the timeout with 'riscv set_command_timeout_sec'." ,
565+ riscv_get_command_timeout_sec ());
566+ return ERROR_TIMEOUT_REACHED ;
567+ }
568+
569+
570+ } while (!get_field32 (dmcontrol , DM_DMCONTROL_DMACTIVE ));
571+ LOG_TARGET_DEBUG (target , "DM is active." );
572+ }
573+
547574 if (dmi_read (target , & next_dm , DM_NEXTDM + current_dm ) != ERROR_OK )
548575 break ;
549576 LOG_TARGET_DEBUG (target , "dm @ 0x%x --> nextdm=0x%x" , current_dm , next_dm );
0 commit comments