- val = buf_get_u32(ir_test, chain_pos, 2);
- /* Only fail this check if we have IDCODE for this device */
- if ((val != 0x1)&&(tap->hasidcode))
- {
- char *cbuf = buf_to_str(ir_test, total_ir_length, 16);
- LOG_ERROR("Could not validate JTAG scan chain, IR mismatch, scan returned 0x%s. tap=%s pos=%d expected 0x1 got %0x", cbuf, jtag_tap_name(tap), chain_pos, val);
- free(cbuf);
- free(ir_test);
- return ERROR_JTAG_INIT_FAILED;
+ /* If we're autoprobing, guess IR lengths. They must be at
+ * least two bits. Guessing will fail if (a) any TAP does
+ * not conform to the JTAG spec; or (b) when the upper bits
+ * captured from some conforming TAP are nonzero.
+ *
+ * REVISIT alternative approach: escape to some tcl code
+ * which could provide more knowledge, based on IDCODE; and
+ * only guess when that has no success.
+ */
+ if (tap->ir_length == 0) {
+ tap->ir_length = 2;
+ while ((val = buf_get_u32(ir_test, chain_pos,
+ tap->ir_length + 1)) == 1) {
+ tap->ir_length++;
+ }
+ LOG_WARNING("AUTO %s - use \"... -irlen %d\"",
+ jtag_tap_name(tap), tap->ir_length);
+ }
+
+ /* Validate the two LSBs, which must be 01 per JTAG spec.
+ *
+ * Or ... more bits could be provided by TAP declaration.
+ * Plus, some taps (notably in i.MX series chips) violate
+ * this part of the JTAG spec, so their capture mask/value
+ * attributes might disable this test.
+ */
+ val = buf_get_u32(ir_test, chain_pos, tap->ir_length);
+ if ((val & tap->ir_capture_mask) != tap->ir_capture_value) {
+ LOG_ERROR("%s: IR capture error; saw 0x%0*x not 0x%0*x",
+ jtag_tap_name(tap),
+ (tap->ir_length + 7) / tap->ir_length,
+ val,
+ (tap->ir_length + 7) / tap->ir_length,
+ (unsigned) tap->ir_capture_value);
+
+ retval = ERROR_JTAG_INIT_FAILED;
+ goto done;