@@ -54,7 +54,8 @@ struct scan_field select_idcode = {
5454};
5555
5656static bscan_tunnel_type_t bscan_tunnel_type ;
57- int bscan_tunnel_ir_width ; /* if zero, then tunneling is not present/active */
57+ #define BSCAN_TUNNEL_IR_WIDTH_NBITS 7
58+ uint8_t bscan_tunnel_ir_width ; /* if zero, then tunneling is not present/active */
5859static int bscan_tunnel_ir_id ; /* IR ID of the JTAG TAP to access the tunnel. Valid when not 0 */
5960
6061static const uint8_t bscan_zero [4 ] = {0 };
@@ -67,7 +68,6 @@ static struct scan_field select_user4 = {
6768};
6869
6970
70- static uint8_t bscan_tunneled_ir_width [4 ] = {5 }; /* overridden by assignment in riscv_init_target */
7171static struct scan_field _bscan_tunnel_data_register_select_dmi [] = {
7272 {
7373 .num_bits = 3 ,
@@ -80,8 +80,8 @@ static struct scan_field _bscan_tunnel_data_register_select_dmi[] = {
8080 .in_value = NULL ,
8181 },
8282 {
83- .num_bits = 7 ,
84- .out_value = bscan_tunneled_ir_width ,
83+ .num_bits = BSCAN_TUNNEL_IR_WIDTH_NBITS ,
84+ .out_value = & bscan_tunnel_ir_width ,
8585 .in_value = NULL ,
8686 },
8787 {
@@ -98,8 +98,8 @@ static struct scan_field _bscan_tunnel_nested_tap_select_dmi[] = {
9898 .in_value = NULL ,
9999 },
100100 {
101- .num_bits = 7 ,
102- .out_value = bscan_tunneled_ir_width ,
101+ .num_bits = BSCAN_TUNNEL_IR_WIDTH_NBITS ,
102+ .out_value = & bscan_tunnel_ir_width ,
103103 .in_value = NULL ,
104104 },
105105 {
@@ -300,7 +300,6 @@ void select_dmi_via_bscan(struct target *target)
300300int dtmcontrol_scan_via_bscan (struct target * target , uint32_t out , uint32_t * in_ptr )
301301{
302302 /* On BSCAN TAP: Select IR=USER4, issue tunneled IR scan via BSCAN TAP's DR */
303- uint8_t tunneled_ir_width [4 ] = {bscan_tunnel_ir_width };
304303 uint8_t tunneled_dr_width [4 ] = {32 };
305304 uint8_t out_value [5 ] = {0 };
306305 uint8_t in_value [5 ] = {0 };
@@ -316,8 +315,8 @@ int dtmcontrol_scan_via_bscan(struct target *target, uint32_t out, uint32_t *in_
316315 tunneled_ir [1 ].num_bits = bscan_tunnel_ir_width ;
317316 tunneled_ir [1 ].out_value = ir_dtmcontrol ;
318317 tunneled_ir [1 ].in_value = NULL ;
319- tunneled_ir [2 ].num_bits = 7 ;
320- tunneled_ir [2 ].out_value = tunneled_ir_width ;
318+ tunneled_ir [2 ].num_bits = BSCAN_TUNNEL_IR_WIDTH_NBITS ;
319+ tunneled_ir [2 ].out_value = & bscan_tunnel_ir_width ;
321320 tunneled_ir [2 ].in_value = NULL ;
322321 tunneled_ir [3 ].num_bits = 1 ;
323322 tunneled_ir [3 ].out_value = bscan_zero ;
@@ -329,7 +328,7 @@ int dtmcontrol_scan_via_bscan(struct target *target, uint32_t out, uint32_t *in_
329328 tunneled_dr [1 ].num_bits = 32 + 1 ;
330329 tunneled_dr [1 ].out_value = out_value ;
331330 tunneled_dr [1 ].in_value = in_value ;
332- tunneled_dr [2 ].num_bits = 7 ;
331+ tunneled_dr [2 ].num_bits = BSCAN_TUNNEL_IR_WIDTH_NBITS ;
333332 tunneled_dr [2 ].out_value = tunneled_dr_width ;
334333 tunneled_dr [2 ].in_value = NULL ;
335334 tunneled_dr [3 ].num_bits = 1 ;
@@ -343,8 +342,8 @@ int dtmcontrol_scan_via_bscan(struct target *target, uint32_t out, uint32_t *in_
343342 tunneled_ir [2 ].num_bits = bscan_tunnel_ir_width ;
344343 tunneled_ir [2 ].out_value = ir_dtmcontrol ;
345344 tunneled_ir [1 ].in_value = NULL ;
346- tunneled_ir [1 ].num_bits = 7 ;
347- tunneled_ir [1 ].out_value = tunneled_ir_width ;
345+ tunneled_ir [1 ].num_bits = BSCAN_TUNNEL_IR_WIDTH_NBITS ;
346+ tunneled_ir [1 ].out_value = & bscan_tunnel_ir_width ;
348347 tunneled_ir [2 ].in_value = NULL ;
349348 tunneled_ir [0 ].num_bits = 1 ;
350349 tunneled_ir [0 ].out_value = bscan_zero ;
@@ -477,7 +476,6 @@ static int riscv_init_target(struct command_context *cmd_ctx,
477476 }
478477 h_u32_to_le (ir_user4 , ir_user4_raw );
479478 select_user4 .num_bits = target -> tap -> ir_length ;
480- bscan_tunneled_ir_width [0 ] = bscan_tunnel_ir_width ;
481479 if (bscan_tunnel_type == BSCAN_TUNNEL_DATA_REGISTER )
482480 bscan_tunnel_data_register_select_dmi [1 ].num_bits = bscan_tunnel_ir_width ;
483481 else /* BSCAN_TUNNEL_NESTED_TAP */
@@ -4386,18 +4384,23 @@ COMMAND_HANDLER(riscv_resume_order)
43864384
43874385COMMAND_HANDLER (riscv_use_bscan_tunnel )
43884386{
4389- int irwidth = 0 ;
4387+ uint8_t irwidth = 0 ;
43904388 int tunnel_type = BSCAN_TUNNEL_NESTED_TAP ;
43914389
4392- if (CMD_ARGC > 2 ) {
4393- LOG_ERROR ("Command takes at most two arguments" );
4390+ if (CMD_ARGC < 1 || CMD_ARGC > 2 )
43944391 return ERROR_COMMAND_SYNTAX_ERROR ;
4395- } else if (CMD_ARGC == 1 ) {
4396- COMMAND_PARSE_NUMBER (int , CMD_ARGV [0 ], irwidth );
4397- } else if (CMD_ARGC == 2 ) {
4398- COMMAND_PARSE_NUMBER (int , CMD_ARGV [0 ], irwidth );
4399- COMMAND_PARSE_NUMBER (int , CMD_ARGV [1 ], tunnel_type );
4392+
4393+ if (CMD_ARGC >= 1 ) {
4394+ COMMAND_PARSE_NUMBER (u8 , CMD_ARGV [0 ], irwidth );
4395+ assert (BSCAN_TUNNEL_IR_WIDTH_NBITS < 8 );
4396+ if (irwidth >= (uint8_t )1 << BSCAN_TUNNEL_IR_WIDTH_NBITS ) {
4397+ command_print (CMD , "'value' does not fit into %d bits." ,
4398+ BSCAN_TUNNEL_IR_WIDTH_NBITS );
4399+ return ERROR_COMMAND_ARGUMENT_OVERFLOW ;
4400+ }
44004401 }
4402+ if (CMD_ARGC == 2 )
4403+ COMMAND_PARSE_NUMBER (int , CMD_ARGV [1 ], tunnel_type );
44014404 if (tunnel_type == BSCAN_TUNNEL_NESTED_TAP )
44024405 LOG_INFO ("Nested Tap based Bscan Tunnel Selected" );
44034406 else if (tunnel_type == BSCAN_TUNNEL_DATA_REGISTER )
@@ -5199,18 +5202,14 @@ static const struct command_registration riscv_exec_command_handlers[] = {
51995202 {
52005203 .name = "use_bscan_tunnel" ,
52015204 .handler = riscv_use_bscan_tunnel ,
5202- .mode = COMMAND_ANY ,
5205+ .mode = COMMAND_CONFIG ,
52035206 .usage = "value [type]" ,
5204- .help = "Enable or disable use of a BSCAN tunnel to reach DM. Supply "
5205- "the width of the DM transport TAP's instruction register to "
5206- "enable. Supply a value of 0 to disable. Pass A second argument "
5207- "(optional) to indicate Bscan Tunnel Type {0:(default) NESTED_TAP , "
5208- "1: DATA_REGISTER}"
5207+ .help = "Enable or disable use of a BSCAN tunnel to reach DM."
52095208 },
52105209 {
52115210 .name = "set_bscan_tunnel_ir" ,
52125211 .handler = riscv_set_bscan_tunnel_ir ,
5213- .mode = COMMAND_ANY ,
5212+ .mode = COMMAND_CONFIG ,
52145213 .usage = "value" ,
52155214 .help = "Specify the JTAG TAP IR used to access the bscan tunnel. "
52165215 "By default it is 0x23 << (ir_length - 6), which map some "
0 commit comments