Skip to content

Commit

Permalink
do not assume DTM version unless dtmcontrol is read successfully
Browse files Browse the repository at this point in the history
Change-Id: I02906dbbe04778623a51774bc21fa45b55d12881
Signed-off-by: Parshintsev Anatoly <[email protected]>
  • Loading branch information
aap-sc committed Sep 28, 2023
1 parent ef3be96 commit d76ac4b
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 32 deletions.
19 changes: 11 additions & 8 deletions src/target/riscv/riscv-011.c
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ static uint16_t dram_address(unsigned int index)
return 0x40 + index - 0x10;
}

static uint32_t dtmcontrol_scan(struct target *target, uint32_t out)
static uint32_t dtmcontrol_scan(struct target *target, uint32_t out, uint32_t *in_ptr)
{
struct scan_field field;
uint8_t in_value[4];
Expand All @@ -300,7 +300,9 @@ static uint32_t dtmcontrol_scan(struct target *target, uint32_t out)
uint32_t in = buf_get_u32(field.in_value, 0, 32);
LOG_DEBUG("DTMCONTROL: 0x%x -> 0x%x", out, in);

return in;
if (in_ptr)
*in_ptr = in;
return ERROR_OK;
}

static uint32_t idcode_scan(struct target *target)
Expand Down Expand Up @@ -338,7 +340,7 @@ static void increase_dbus_busy_delay(struct target *target)
info->dtmcontrol_idle, info->dbus_busy_delay,
info->interrupt_high_delay);

dtmcontrol_scan(target, DTMCONTROL_DBUS_RESET);
dtmcontrol_scan(target, DTMCONTROL_DBUS_RESET, NULL /* discard value */);
}

static void increase_interrupt_high_delay(struct target *target)
Expand Down Expand Up @@ -1458,16 +1460,17 @@ static int step(struct target *target, int current, target_addr_t address,
static int examine(struct target *target)
{
/* Don't need to select dbus, since the first thing we do is read dtmcontrol. */
uint32_t dtmcontrol;
if (dtmcontrol_scan(target, 0, &dtmcontrol) != ERROR_OK || dtmcontrol == 0) {
LOG_ERROR("Could not scan dtmcontrol. Check JTAG connectivity/board power.");
return ERROR_FAIL;
}

uint32_t dtmcontrol = dtmcontrol_scan(target, 0);
LOG_DEBUG("dtmcontrol=0x%x", dtmcontrol);
LOG_DEBUG(" addrbits=%d", get_field(dtmcontrol, DTMCONTROL_ADDRBITS));
LOG_DEBUG(" version=%d", get_field(dtmcontrol, DTMCONTROL_VERSION));
LOG_DEBUG(" idle=%d", get_field(dtmcontrol, DTMCONTROL_IDLE));
if (dtmcontrol == 0) {
LOG_ERROR("dtmcontrol is 0. Check JTAG connectivity/board power.");
return ERROR_FAIL;
}

if (get_field(dtmcontrol, DTMCONTROL_VERSION) != 0) {
LOG_ERROR("Unsupported DTM version %d. (dtmcontrol=0x%x)",
get_field(dtmcontrol, DTMCONTROL_VERSION), dtmcontrol);
Expand Down
26 changes: 15 additions & 11 deletions src/target/riscv/riscv-013.c
Original file line number Diff line number Diff line change
Expand Up @@ -411,14 +411,14 @@ static void select_dmi(struct target *target)
jtag_add_ir_scan(target->tap, &select_dbus, TAP_IDLE);
}

static uint32_t dtmcontrol_scan(struct target *target, uint32_t out)
static uint32_t dtmcontrol_scan(struct target *target, uint32_t out, uint32_t *in_ptr)
{
struct scan_field field;
uint8_t in_value[4];
uint8_t out_value[4] = { 0 };

if (bscan_tunnel_ir_width != 0)
return dtmcontrol_scan_via_bscan(target, out);
return dtmcontrol_scan_via_bscan(target, out, in_ptr);

buf_set_u32(out_value, 0, 32, out);

Expand All @@ -441,7 +441,9 @@ static uint32_t dtmcontrol_scan(struct target *target, uint32_t out)
uint32_t in = buf_get_u32(field.in_value, 0, 32);
LOG_DEBUG("DTMCS: 0x%x -> 0x%x", out, in);

return in;
if (in_ptr)
*in_ptr = in;
return ERROR_OK;
}

static void increase_dmi_busy_delay(struct target *target)
Expand All @@ -452,7 +454,7 @@ static void increase_dmi_busy_delay(struct target *target)
info->dtmcs_idle, info->dmi_busy_delay,
info->ac_busy_delay);

dtmcontrol_scan(target, DTM_DTMCS_DMIRESET);
dtmcontrol_scan(target, DTM_DTMCS_DMIRESET, NULL /* discard result */);
}

/**
Expand Down Expand Up @@ -597,7 +599,7 @@ static int dmi_op_timeout(struct target *target, uint32_t *data_in,
} else if (status == DMI_STATUS_SUCCESS) {
break;
} else {
dtmcontrol_scan(target, DTM_DTMCS_DMIRESET);
dtmcontrol_scan(target, DTM_DTMCS_DMIRESET, NULL /* discard result */);
break;
}
if (time(NULL) - start > timeout_sec)
Expand Down Expand Up @@ -632,7 +634,7 @@ static int dmi_op_timeout(struct target *target, uint32_t *data_in,
"Failed DMI %s (NOP) at 0x%x; status=%d", op_name, address,
status);
}
dtmcontrol_scan(target, DTM_DTMCS_DMIRESET);
dtmcontrol_scan(target, DTM_DTMCS_DMIRESET, NULL /* discard result */);
return ERROR_FAIL;
}
if (time(NULL) - start > timeout_sec)
Expand Down Expand Up @@ -1844,17 +1846,19 @@ static int examine(struct target *target)
/* Don't need to select dbus, since the first thing we do is read dtmcontrol. */
LOG_TARGET_DEBUG(target, "dbgbase=0x%x", target->dbgbase);

uint32_t dtmcontrol = dtmcontrol_scan(target, 0);
uint32_t dtmcontrol;
if (dtmcontrol_scan(target, 0, &dtmcontrol) != ERROR_OK || dtmcontrol == 0) {
LOG_TARGET_ERROR(target, "could not scan dtmcontrol. Check JTAG connectivity/board power.");
return ERROR_FAIL;
}

LOG_TARGET_DEBUG(target, "dtmcontrol=0x%x", dtmcontrol);
LOG_TARGET_DEBUG(target, " dmireset=%d", get_field(dtmcontrol, DTM_DTMCS_DMIRESET));
LOG_TARGET_DEBUG(target, " idle=%d", get_field(dtmcontrol, DTM_DTMCS_IDLE));
LOG_TARGET_DEBUG(target, " dmistat=%d", get_field(dtmcontrol, DTM_DTMCS_DMISTAT));
LOG_TARGET_DEBUG(target, " abits=%d", get_field(dtmcontrol, DTM_DTMCS_ABITS));
LOG_TARGET_DEBUG(target, " version=%d", get_field(dtmcontrol, DTM_DTMCS_VERSION));
if (dtmcontrol == 0) {
LOG_TARGET_ERROR(target, "dtmcontrol is 0. Check JTAG connectivity/board power.");
return ERROR_FAIL;
}

if (get_field(dtmcontrol, DTM_DTMCS_VERSION) != 1) {
LOG_TARGET_ERROR(target, "Unsupported DTM version %d. (dtmcontrol=0x%x)",
get_field(dtmcontrol, DTM_DTMCS_VERSION), dtmcontrol);
Expand Down
58 changes: 46 additions & 12 deletions src/target/riscv/riscv.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ void select_dmi_via_bscan(struct target *target)
bscan_tunnel_nested_tap_select_dmi, TAP_IDLE);
}

uint32_t dtmcontrol_scan_via_bscan(struct target *target, uint32_t out)
uint32_t dtmcontrol_scan_via_bscan(struct target *target, uint32_t out, uint32_t *in_ptr)
{
/* On BSCAN TAP: Select IR=USER4, issue tunneled IR scan via BSCAN TAP's DR */
uint8_t tunneled_ir_width[4] = {bscan_tunnel_ir_width};
Expand Down Expand Up @@ -365,18 +365,19 @@ uint32_t dtmcontrol_scan_via_bscan(struct target *target, uint32_t out)
uint32_t in = buf_get_u32(in_value, 1, 32);
LOG_DEBUG("DTMCS: 0x%x -> 0x%x", out, in);

return in;
if (in_ptr)
*in_ptr = in;
return ERROR_OK;
}

static uint32_t dtmcontrol_scan(struct target *target, uint32_t out)
static uint32_t dtmcontrol_scan(struct target *target, uint32_t out, uint32_t *in_ptr)
{
struct scan_field field;
uint8_t in_value[4];
uint8_t out_value[4] = { 0 };

if (bscan_tunnel_ir_width != 0)
return dtmcontrol_scan_via_bscan(target, out);

return dtmcontrol_scan_via_bscan(target, out, in_ptr);

buf_set_u32(out_value, 0, 32, out);

Expand All @@ -392,14 +393,16 @@ static uint32_t dtmcontrol_scan(struct target *target, uint32_t out)

int retval = jtag_execute_queue();
if (retval != ERROR_OK) {
LOG_ERROR("failed jtag scan: %d", retval);
LOG_TARGET_ERROR(target, "failed dtmcontrol scan, error code = %d", retval);
return retval;
}

uint32_t in = buf_get_u32(field.in_value, 0, 32);
LOG_DEBUG("DTMCONTROL: 0x%x -> 0x%x", out, in);

return in;
if (in_ptr)
*in_ptr = in;
return ERROR_OK;
}

static struct target_type *get_target_type(struct target *target)
Expand All @@ -411,11 +414,13 @@ static struct target_type *get_target_type(struct target *target)

RISCV_INFO(info);
switch (info->dtm_version) {
case 0:
case DTM_DTMCS_VERSION_0_11:
return &riscv011_target;
case 1:
case DTM_DTMCS_VERSION_1_0:
return &riscv013_target;
default:
// TODO: once we have proper support for non-examined targets
// we should have an assert here
LOG_TARGET_ERROR(target, "Unsupported DTM version: %d",
info->dtm_version);
return NULL;
Expand Down Expand Up @@ -482,6 +487,7 @@ static void riscv_free_registers(struct target *target)
free(target->reg_cache->reg_list);
}
free(target->reg_cache);
target->reg_cache = NULL;
}
}

Expand All @@ -491,6 +497,8 @@ static void riscv_deinit_target(struct target *target)

struct riscv_info *info = target->arch_info;
struct target_type *tt = get_target_type(target);
if (!tt)
LOG_TARGET_ERROR(target, "Could not identify target type.");

if (riscv_flush_registers(target) != ERROR_OK)
LOG_TARGET_ERROR(target, "Failed to flush registers. Ignoring this error.");
Expand Down Expand Up @@ -1520,6 +1528,8 @@ static int oldriscv_step(struct target *target, int current, uint32_t address,
int handle_breakpoints)
{
struct target_type *tt = get_target_type(target);
if (!tt)
return ERROR_FAIL;
return tt->step(target, current, address, handle_breakpoints);
}

Expand All @@ -1545,8 +1555,14 @@ static int riscv_examine(struct target *target)
/* Don't need to select dbus, since the first thing we do is read dtmcontrol. */

RISCV_INFO(info);
uint32_t dtmcontrol = dtmcontrol_scan(target, 0);
uint32_t dtmcontrol;
if (dtmcontrol_scan(target, 0, &dtmcontrol) != ERROR_OK || dtmcontrol == 0) {
LOG_TARGET_ERROR(target, "Could not read dtmcontrol. Check JTAG connectivity/board power.");
return ERROR_FAIL;
}
LOG_TARGET_DEBUG(target, "dtmcontrol=0x%x", dtmcontrol);
// FIXME: if examine fails we may need to restore the original dtm_version
// NOTE: maybe we should update dtm_version only when examination is successful?
info->dtm_version = get_field(dtmcontrol, DTMCONTROL_VERSION);
LOG_TARGET_DEBUG(target, "version=0x%x", info->dtm_version);

Expand All @@ -1564,6 +1580,8 @@ static int riscv_examine(struct target *target)
static int oldriscv_poll(struct target *target)
{
struct target_type *tt = get_target_type(target);
if (!tt)
return ERROR_FAIL;
return tt->poll(target);
}

Expand Down Expand Up @@ -1697,6 +1715,8 @@ static int halt_go(struct target *target)
int result;
if (!r->get_hart_state) {
struct target_type *tt = get_target_type(target);
if (!tt)
return ERROR_FAIL;
result = tt->halt(target);
} else {
result = riscv_halt_go_all_harts(target);
Expand All @@ -1718,6 +1738,8 @@ int riscv_halt(struct target *target)

if (!r->get_hart_state) {
struct target_type *tt = get_target_type(target);
if (!tt)
return ERROR_FAIL;
return tt->halt(target);
}

Expand Down Expand Up @@ -1763,6 +1785,8 @@ static int riscv_assert_reset(struct target *target)
{
LOG_TARGET_DEBUG(target, "coreid: [%d]", target->coreid);
struct target_type *tt = get_target_type(target);
if (!tt)
return ERROR_FAIL;
riscv_invalidate_register_cache(target);
return tt->assert_reset(target);
}
Expand All @@ -1771,6 +1795,8 @@ static int riscv_deassert_reset(struct target *target)
{
LOG_TARGET_DEBUG(target, "coreid: [%d]", target->coreid);
struct target_type *tt = get_target_type(target);
if (!tt)
return ERROR_FAIL;
return tt->deassert_reset(target);
}

Expand Down Expand Up @@ -1903,6 +1929,8 @@ static int resume_go(struct target *target, int current,
int result;
if (!r->get_hart_state) {
struct target_type *tt = get_target_type(target);
if (!tt)
return ERROR_FAIL;
result = tt->resume(target, current, address, handle_breakpoints,
debug_execution);
} else {
Expand Down Expand Up @@ -2403,6 +2431,8 @@ static int riscv_write_phys_memory(struct target *target, target_addr_t phys_add
uint32_t size, uint32_t count, const uint8_t *buffer)
{
struct target_type *tt = get_target_type(target);
if (!tt)
return ERROR_FAIL;
return tt->write_memory(target, phys_address, size, count, buffer);
}

Expand All @@ -2419,6 +2449,8 @@ static int riscv_write_memory(struct target *target, target_addr_t address,
address = physical_addr;

struct target_type *tt = get_target_type(target);
if (!tt)
return ERROR_FAIL;
return tt->write_memory(target, address, size, count, buffer);
}

Expand Down Expand Up @@ -2496,6 +2528,8 @@ static int riscv_get_gdb_reg_list(struct target *target,
static int riscv_arch_state(struct target *target)
{
struct target_type *tt = get_target_type(target);
if (!tt)
return ERROR_FAIL;
return tt->arch_state(target);
}

Expand Down Expand Up @@ -4212,7 +4246,7 @@ COMMAND_HANDLER(riscv_exec_progbuf)
struct target *target = get_current_target(CMD_CTX);

RISCV_INFO(r);
if (r->dtm_version != 1) {
if (r->dtm_version != DTM_DTMCS_VERSION_1_0) {
LOG_TARGET_ERROR(target, "exec_progbuf: Program buffer is "
"only supported on v0.13 or v1.0 targets.");
return ERROR_FAIL;
Expand Down Expand Up @@ -4610,7 +4644,7 @@ static void riscv_info_init(struct target *target, struct riscv_info *r)

r->common_magic = RISCV_COMMON_MAGIC;

r->dtm_version = 1;
r->dtm_version = DTM_DTMCS_VERSION_UNKNOWN;
r->version_specific = NULL;

memset(r->trigger_unique_id, 0xff, sizeof(r->trigger_unique_id));
Expand Down
4 changes: 3 additions & 1 deletion src/target/riscv/riscv.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ typedef struct {
char *name;
} range_list_t;

#define DTM_DTMCS_VERSION_UNKNOWN ((uint32_t)-1)

struct riscv_info {
unsigned int common_magic;

Expand Down Expand Up @@ -356,7 +358,7 @@ extern uint32_t bscan_tunneled_select_dmi_num_fields;
typedef enum { BSCAN_TUNNEL_NESTED_TAP, BSCAN_TUNNEL_DATA_REGISTER } bscan_tunnel_type_t;
extern int bscan_tunnel_ir_width;

uint32_t dtmcontrol_scan_via_bscan(struct target *target, uint32_t out);
uint32_t dtmcontrol_scan_via_bscan(struct target *target, uint32_t out, uint32_t *in_ptr);
void select_dmi_via_bscan(struct target *target);

/*** OpenOCD Interface */
Expand Down

0 comments on commit d76ac4b

Please sign in to comment.