comments and debug code
[fw/openocd] / src / target / arm7_9_common.c
index 9161aa677026c93a0363ad3fd261c43a0c0c66f2..3fe8efa95ee11f0b9d857f224cfb4b5be0aba2ea 100644 (file)
@@ -66,7 +66,6 @@ int handle_arm7_9_fast_memory_access_command(struct command_context_s *cmd_ctx,
 int handle_arm7_9_dcc_downloads_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
 int handle_arm7_9_etm_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
 
-
 static int arm7_9_clear_watchpoints(arm7_9_common_t *arm7_9)
 {
        embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], 0x0);
@@ -79,6 +78,26 @@ static int arm7_9_clear_watchpoints(arm7_9_common_t *arm7_9)
        return jtag_execute_queue();
 }
 
+static void arm7_9_assign_wp(arm7_9_common_t *arm7_9, breakpoint_t *breakpoint)
+{
+       if (!arm7_9->wp0_used)
+       {
+               arm7_9->wp0_used = 1;
+               breakpoint->set = 1;
+               arm7_9->wp_available--;
+       }
+       else if (!arm7_9->wp1_used)
+       {
+               arm7_9->wp1_used = 1;
+               breakpoint->set = 2;
+               arm7_9->wp_available--;
+       }
+       else
+       {
+               LOG_ERROR("BUG: no hardware comparator available");
+       }
+}
+
 /* set up embedded ice registers */
 static int arm7_9_set_software_breakpoints(arm7_9_common_t *arm7_9)
 {
@@ -143,7 +162,6 @@ int arm7_9_setup(target_t *target)
        return arm7_9_clear_watchpoints(arm7_9);
 }
 
-
 int arm7_9_get_arch_pointers(target_t *target, armv4_5_common_t **armv4_5_p, arm7_9_common_t **arm7_9_p)
 {
        armv4_5_common_t *armv4_5 = target->arch_info;
@@ -184,6 +202,13 @@ int arm7_9_set_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
        {
                /* either an ARM (4 byte) or Thumb (2 byte) breakpoint */
                u32 mask = (breakpoint->length == 4) ? 0x3u : 0x1u;
+
+               /* reassign a hw breakpoint */
+               if (breakpoint->set==0)
+               {
+                       arm7_9_assign_wp(arm7_9, breakpoint);
+               }
+
                if (breakpoint->set==1)
                {
                        embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_ADDR_VALUE], breakpoint->address);
@@ -231,7 +256,7 @@ int arm7_9_set_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
                                return retval;
                        }
 
-                       if ((retval = target->type->read_memory(target, breakpoint->address, 4, 1, (u8 *)&verify)) != ERROR_OK)
+                       if ((retval = target_read_u32(target, breakpoint->address, &verify)) != ERROR_OK)
                        {
                                return retval;
                        }
@@ -255,7 +280,7 @@ int arm7_9_set_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
                                return retval;
                        }
 
-                       if ((retval = target->type->read_memory(target, breakpoint->address, 2, 1, (u8 *)&verify)) != ERROR_OK)
+                       if ((retval = target_read_u16(target, breakpoint->address, &verify)) != ERROR_OK)
                        {
                                return retval;
                        }
@@ -269,7 +294,6 @@ int arm7_9_set_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
        }
 
        return retval;
-
 }
 
 int arm7_9_unset_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
@@ -291,11 +315,13 @@ int arm7_9_unset_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
                {
                        embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W0_CONTROL_VALUE], 0x0);
                        arm7_9->wp0_used = 0;
+                       arm7_9->wp_available++;
                }
                else if (breakpoint->set == 2)
                {
                        embeddedice_set_reg(&arm7_9->eice_cache->reg_list[EICE_W1_CONTROL_VALUE], 0x0);
                        arm7_9->wp1_used = 0;
+                       arm7_9->wp_available++;
                }
                retval = jtag_execute_queue();
                breakpoint->set = 0;
@@ -370,25 +396,9 @@ int arm7_9_add_breakpoint(struct target_s *target, breakpoint_t *breakpoint)
 
        if (breakpoint->type == BKPT_HARD)
        {
-               arm7_9->wp_available--;
-
-               if (!arm7_9->wp0_used)
-               {
-                       arm7_9->wp0_used = 1;
-                       breakpoint->set = 1;
-               }
-               else if (!arm7_9->wp1_used)
-               {
-                       arm7_9->wp1_used = 1;
-                       breakpoint->set = 2;
-               }
-               else
-               {
-                       LOG_ERROR("BUG: no hardware comparator available");
-               }
+               arm7_9_assign_wp(arm7_9, breakpoint);
        }
 
-
        arm7_9->breakpoint_count++;
 
        return arm7_9_set_breakpoint(target, breakpoint);
@@ -571,9 +581,6 @@ int arm7_9_remove_watchpoint(struct target_s *target, watchpoint_t *watchpoint)
        return ERROR_OK;
 }
 
-
-
-
 int arm7_9_execute_sys_speed(struct target_s *target)
 {
        int retval;
@@ -584,7 +591,7 @@ int arm7_9_execute_sys_speed(struct target_s *target)
        reg_t *dbg_stat = &arm7_9->eice_cache->reg_list[EICE_DBG_STAT];
 
        /* set RESTART instruction */
-       jtag_add_end_state(TAP_RTI);
+       jtag_add_end_state(TAP_IDLE);
        if (arm7_9->need_bypass_before_restart) {
                arm7_9->need_bypass_before_restart = 0;
                arm_jtag_set_instr(jtag_info, 0xf, NULL);
@@ -630,7 +637,7 @@ int arm7_9_execute_fast_sys_speed(struct target_s *target)
        reg_t *dbg_stat = &arm7_9->eice_cache->reg_list[EICE_DBG_STAT];
 
        /* set RESTART instruction */
-       jtag_add_end_state(TAP_RTI);
+       jtag_add_end_state(TAP_IDLE);
        if (arm7_9->need_bypass_before_restart) {
                arm7_9->need_bypass_before_restart = 0;
                arm_jtag_set_instr(jtag_info, 0xf, NULL);
@@ -642,9 +649,9 @@ int arm7_9_execute_fast_sys_speed(struct target_s *target)
                /* check for DBGACK and SYSCOMP set (others don't care) */
 
                /* NB! These are constants that must be available until after next jtag_execute() and
-                  we evaluate the values upon first execution in lieu of setting up these constants
-                  during early setup.
-               */
+                * we evaluate the values upon first execution in lieu of setting up these constants
+                * during early setup.
+                * */
                buf_set_u32(check_value, 0, 32, 0x9);
                buf_set_u32(check_mask, 0, 32, 0x9);
                set=1;
@@ -662,7 +669,8 @@ int arm7_9_target_request_data(target_t *target, u32 size, u8 *buffer)
        arm7_9_common_t *arm7_9 = armv4_5->arch_info;
        arm_jtag_t *jtag_info = &arm7_9->jtag_info;
        u32 *data;
-       int i, retval = ERROR_OK;
+       int retval = ERROR_OK;
+       u32 i;
 
        data = malloc(size * (sizeof(u32)));
 
@@ -689,7 +697,6 @@ int arm7_9_handle_target_request(void *priv)
        arm_jtag_t *jtag_info = &arm7_9->jtag_info;
        reg_t *dcc_control = &arm7_9->eice_cache->reg_list[EICE_COMMS_CTRL];
 
-
        if (!target->dbg_msg_enabled)
                return ERROR_OK;
 
@@ -857,28 +864,25 @@ int arm7_9_assert_reset(target_t *target)
                jtag_add_reset(0, 1);
        }
 
-
        target->state = TARGET_RESET;
        jtag_add_sleep(50000);
 
        armv4_5_invalidate_core_regs(target);
 
-    if ((target->reset_halt)&&((jtag_reset_config & RESET_SRST_PULLS_TRST)==0))
+       if ((target->reset_halt)&&((jtag_reset_config & RESET_SRST_PULLS_TRST)==0))
        {
                /* debug entry was already prepared in arm7_9_assert_reset() */
                target->debug_reason = DBG_REASON_DBGRQ;
        }
 
        return ERROR_OK;
-
 }
 
 int arm7_9_deassert_reset(target_t *target)
 {
        int retval=ERROR_OK;
        LOG_DEBUG("target->state: %s",
-                 Jim_Nvp_value2name_simple( nvp_target_state,target->state)->name);
-
+               Jim_Nvp_value2name_simple( nvp_target_state,target->state)->name);
 
        /* deassert reset lines */
        jtag_add_reset(0, 0);
@@ -1492,14 +1496,14 @@ int arm7_9_restart_core(struct target_s *target)
        arm_jtag_t *jtag_info = &arm7_9->jtag_info;
 
        /* set RESTART instruction */
-       jtag_add_end_state(TAP_RTI);
+       jtag_add_end_state(TAP_IDLE);
        if (arm7_9->need_bypass_before_restart) {
                arm7_9->need_bypass_before_restart = 0;
                arm_jtag_set_instr(jtag_info, 0xf, NULL);
        }
        arm_jtag_set_instr(jtag_info, 0x4, NULL);
 
-       jtag_add_runtest(1, TAP_RTI);
+       jtag_add_runtest(1, TAP_IDLE);
        return jtag_execute_queue();
 }
 
@@ -1527,7 +1531,6 @@ void arm7_9_enable_breakpoints(struct target_s *target)
        }
 }
 
-
 int arm7_9_resume(struct target_s *target, int current, u32 address, int handle_breakpoints, int debug_execution)
 {
        armv4_5_common_t *armv4_5 = target->arch_info;
@@ -1553,6 +1556,9 @@ int arm7_9_resume(struct target_s *target, int current, u32 address, int handle_
        if (!current)
                buf_set_u32(armv4_5->core_cache->reg_list[15].value, 0, 32, address);
 
+       u32 current_pc;
+       current_pc = buf_get_u32(armv4_5->core_cache->reg_list[15].value, 0, 32);
+
        /* the front-end may request us not to handle breakpoints */
        if (handle_breakpoints)
        {
@@ -1564,8 +1570,18 @@ int arm7_9_resume(struct target_s *target, int current, u32 address, int handle_
                                return retval;
                        }
 
+                       /* calculate PC of next instruction */
+                       u32 next_pc;
+                       if ((retval = arm_simulate_step(target, &next_pc)) != ERROR_OK)
+                       {
+                               u32 current_opcode;
+                               target_read_u32(target, current_pc, &current_opcode);
+                               LOG_ERROR("BUG: couldn't calculate PC of next instruction, current opcode was 0x%8.8x", current_opcode);
+                               return retval;
+                       }
+
                        LOG_DEBUG("enable single-step");
-                       arm7_9->enable_single_step(target);
+                       arm7_9->enable_single_step(target, next_pc);
 
                        target->debug_reason = DBG_REASON_SINGLESTEP;
 
@@ -1675,23 +1691,13 @@ int arm7_9_resume(struct target_s *target, int current, u32 address, int handle_
        return ERROR_OK;
 }
 
-void arm7_9_enable_eice_step(target_t *target)
+void arm7_9_enable_eice_step(target_t *target, u32 next_pc)
 {
        armv4_5_common_t *armv4_5 = target->arch_info;
        arm7_9_common_t *arm7_9 = armv4_5->arch_info;
-       int retval;
 
        u32 current_pc;
        current_pc = buf_get_u32(armv4_5->core_cache->reg_list[15].value, 0, 32);
-       
-       u32 next_pc;
-       if ((retval = arm_simulate_step(target, &next_pc)) != ERROR_OK)
-       {
-               u32 current_opcode;
-               target_read_u32(target, current_pc, &current_opcode);
-               LOG_ERROR("BUG: couldn't calculate PC of next instruction, current opcode was 0x%8.8x", current_opcode);
-               return;
-       }
 
        if(next_pc != current_pc)
        {
@@ -1756,6 +1762,9 @@ int arm7_9_step(struct target_s *target, int current, u32 address, int handle_br
        if (!current)
                buf_set_u32(armv4_5->core_cache->reg_list[15].value, 0, 32, address);
 
+       u32 current_pc;
+       current_pc = buf_get_u32(armv4_5->core_cache->reg_list[15].value, 0, 32);
+
        /* the front-end may request us not to handle breakpoints */
        if (handle_breakpoints)
                if ((breakpoint = breakpoint_find(target, buf_get_u32(armv4_5->core_cache->reg_list[15].value, 0, 32))))
@@ -1766,12 +1775,22 @@ int arm7_9_step(struct target_s *target, int current, u32 address, int handle_br
 
        target->debug_reason = DBG_REASON_SINGLESTEP;
 
+       /* calculate PC of next instruction */
+       u32 next_pc;
+       if ((retval = arm_simulate_step(target, &next_pc)) != ERROR_OK)
+       {
+               u32 current_opcode;
+               target_read_u32(target, current_pc, &current_opcode);
+               LOG_ERROR("BUG: couldn't calculate PC of next instruction, current opcode was 0x%8.8x", current_opcode);
+               return retval;
+       }
+
        if ((retval = arm7_9_restore_context(target)) != ERROR_OK)
        {
                return retval;
        }
 
-       arm7_9->enable_single_step(target);
+       arm7_9->enable_single_step(target, next_pc);
 
        if (armv4_5->core_state == ARMV4_5_STATE_ARM)
        {
@@ -1817,7 +1836,6 @@ int arm7_9_step(struct target_s *target, int current, u32 address, int handle_br
                }
 
        return err;
-
 }
 
 int arm7_9_read_core_reg(struct target_s *target, int num, enum armv4_5_mode mode)
@@ -1884,7 +1902,6 @@ int arm7_9_read_core_reg(struct target_s *target, int num, enum armv4_5_mode mod
        }
 
        return ERROR_OK;
-
 }
 
 int arm7_9_write_core_reg(struct target_s *target, int num, enum armv4_5_mode mode, u32 value)
@@ -1954,7 +1971,7 @@ int arm7_9_read_memory(struct target_s *target, u32 address, u32 size, u32 count
        arm7_9_common_t *arm7_9 = armv4_5->arch_info;
 
        u32 reg[16];
-       int num_accesses = 0;
+       u32 num_accesses = 0;
        int thisrun_accesses;
        int i;
        u32 cpsr;
@@ -2000,9 +2017,11 @@ int arm7_9_read_memory(struct target_s *target, u32 address, u32 size, u32 count
                                 * from a sufficiently high clock (32 kHz is usually too slow)
                                 */
                                if (arm7_9->fast_memory_access)
-                                       arm7_9_execute_fast_sys_speed(target);
+                                       retval = arm7_9_execute_fast_sys_speed(target);
                                else
-                                       arm7_9_execute_sys_speed(target);
+                                       retval = arm7_9_execute_sys_speed(target);
+                               if (retval != ERROR_OK)
+                                       return retval;
 
                                arm7_9->read_core_regs_target_buffer(target, reg_list, buffer, 4);
 
@@ -2129,7 +2148,7 @@ int arm7_9_write_memory(struct target_s *target, u32 address, u32 size, u32 coun
        reg_t *dbg_ctrl = &arm7_9->eice_cache->reg_list[EICE_DBG_CTRL];
 
        u32 reg[16];
-       int num_accesses = 0;
+       u32 num_accesses = 0;
        int thisrun_accesses;
        int i;
        u32 cpsr;
@@ -2308,7 +2327,6 @@ int arm7_9_write_memory(struct target_s *target, u32 address, u32 size, u32 coun
 static int dcc_count;
 static u8 *dcc_buffer;
 
-
 static int arm7_9_dcc_completion(struct target_s *target, u32 exit_point, int timeout_ms, void *arch_info)
 {
        int retval = ERROR_OK;
@@ -2324,16 +2342,16 @@ static int arm7_9_dcc_completion(struct target_s *target, u32 exit_point, int ti
        if (count>2)
        {
                /* Handle first & last using standard embeddedice_write_reg and the middle ones w/the
-                  core function repeated.
-                */
+                * core function repeated. */
                embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_COMMS_DATA], fast_target_buffer_get_u32(buffer, little));
                buffer+=4;
 
                embeddedice_reg_t *ice_reg = arm7_9->eice_cache->reg_list[EICE_COMMS_DATA].arch_info;
                u8 reg_addr = ice_reg->addr & 0x1f;
-               int chain_pos = ice_reg->jtag_info->chain_pos;
+               jtag_tap_t *tap;
+               tap = ice_reg->jtag_info->tap;
 
-               embeddedice_write_dcc(chain_pos, reg_addr, buffer, little, count-2);
+               embeddedice_write_dcc(tap, reg_addr, buffer, little, count-2);
                buffer += (count-2)*4;
 
                embeddedice_write_reg(&arm7_9->eice_cache->reg_list[EICE_COMMS_DATA], fast_target_buffer_get_u32(buffer, little));
@@ -2354,7 +2372,6 @@ static int arm7_9_dcc_completion(struct target_s *target, u32 exit_point, int ti
        return target_wait_state(target, TARGET_HALTED, 500);
 }
 
-
 static const u32 dcc_code[] =
 {
        /* MRC      TST         BNE         MRC         STR         B */
@@ -2363,7 +2380,6 @@ static const u32 dcc_code[] =
 
 int armv4_5_run_algorithm_inner(struct target_s *target, int num_mem_params, mem_param_t *mem_params, int num_reg_params, reg_param_t *reg_params, u32 entry_point, u32 exit_point, int timeout_ms, void *arch_info, int (*run_it)(struct target_s *target, u32 exit_point, int timeout_ms, void *arch_info));
 
-
 int arm7_9_bulk_write_memory(target_t *target, u32 address, u32 count, u8 *buffer)
 {
        int retval;
@@ -2410,8 +2426,6 @@ int arm7_9_bulk_write_memory(target_t *target, u32 address, u32 count, u8 *buffe
 
        buf_set_u32(reg_params[0].value, 0, 32, address);
 
-       //armv4_5_run_algorithm_inner(struct target_s *target, int num_mem_params, mem_param_t *mem_params,
-       // int num_reg_params, reg_param_t *reg_params, u32 entry_point, u32 exit_point, int timeout_ms, void *arch_info, int (*run_it)(struct target_s *target, u32 exit_point, int timeout_ms, void *arch_info))
        dcc_count=count;
        dcc_buffer=buffer;
        retval = armv4_5_run_algorithm_inner(target, 0, NULL, 1, reg_params,
@@ -2467,7 +2481,7 @@ int arm7_9_checksum_memory(struct target_s *target, u32 address, u32 count, u32*
                0x04C11DB7                              /* CRC32XOR:    .word 0x04C11DB7 */
        };
 
-       int i;
+       u32 i;
 
        if (target_alloc_working_area(target, sizeof(arm7_9_crc_code), &crc_algorithm) != ERROR_OK)
        {
@@ -2519,7 +2533,7 @@ int arm7_9_blank_check_memory(struct target_s *target, u32 address, u32 count, u
        reg_param_t reg_params[3];
        armv4_5_algorithm_t armv4_5_info;
        int retval;
-       int i;
+       u32 i;
 
        u32 erase_check_code[] =
        {
@@ -2592,8 +2606,6 @@ int arm7_9_register_commands(struct command_context_s *cmd_ctx)
 
        register_command(cmd_ctx, arm7_9_cmd, "dbgrq", handle_arm7_9_dbgrq_command,
                COMMAND_ANY, "use EmbeddedICE dbgrq instead of breakpoint for target halt requests <enable|disable>");
-       register_command(cmd_ctx, arm7_9_cmd, "fast_writes", handle_arm7_9_fast_memory_access_command,
-                COMMAND_ANY, "(deprecated, see: arm7_9 fast_memory_access)");
        register_command(cmd_ctx, arm7_9_cmd, "fast_memory_access", handle_arm7_9_fast_memory_access_command,
                 COMMAND_ANY, "use fast memory accesses instead of slower but potentially unsafe slow accesses <enable|disable>");
        register_command(cmd_ctx, arm7_9_cmd, "dcc_downloads", handle_arm7_9_dcc_downloads_command,
@@ -2724,10 +2736,8 @@ int handle_arm7_9_write_core_reg_command(struct command_context_s *cmd_ctx, char
        value = strtoul(args[2], NULL, 0);
 
        return arm7_9_write_core_reg(target, num, mode, value);
-
 }
 
-
 int handle_arm7_9_dbgrq_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
 {
        target_t *target = get_current_target(cmd_ctx);