+/**
+ * Perform bidirectional JTAG SCAN operation at maximum TCK frequency.
+ *
+ * Data stored in EP2 OUT buffer is shifted into the JTAG chain via TDI, TDO
+ * data is sampled and stored in the EP2 IN buffer.
+ * The TAP-FSM state is alyways left in the PAUSE-DR/PAUSE-IR state.
+ *
+ * Maximum achievable TCK frequency is 78 kHz for ULINK clocked at 24 MHz.
+ *
+ * @param out_offset offset in OUT2BUF where payload data starts
+ */
+void jtag_slow_scan_io(u8 out_offset, u8 in_offset)
+{
+ u8 scan_size_bytes, bits_last_byte;
+ u8 tms_count_start, tms_count_end;
+ u8 tms_sequence_start, tms_sequence_end;
+ u8 tdi_data, tdo_data, i, j, k;
+
+ u8 outb_buffer;
+
+ /* Get parameters from OUT2BUF */
+ scan_size_bytes = OUT2BUF[out_offset];
+ bits_last_byte = OUT2BUF[out_offset + 1];
+ tms_count_start = (OUT2BUF[out_offset + 2] >> 4) & 0x0F;
+ tms_count_end = OUT2BUF[out_offset + 2] & 0x0F;
+ tms_sequence_start = OUT2BUF[out_offset + 3];
+ tms_sequence_end = OUT2BUF[out_offset + 4];
+
+ if (tms_count_start > 0) {
+ jtag_slow_clock_tms(tms_count_start, tms_sequence_start);
+ }
+
+ outb_buffer = OUTB & ~(PIN_TCK | PIN_TMS);
+
+ /* Shift all bytes except the last byte */
+ for (i = 0; i < scan_size_bytes - 1; i++) {
+ tdi_data = OUT2BUF[i + out_offset + 5];
+ tdo_data = 0;
+
+ for (j = 0; j < 8; j++) {
+ if (tdi_data & 0x01) {
+ outb_buffer |= PIN_TDI;
+ }
+ else {
+ outb_buffer &= ~PIN_TDI;
+ }
+
+ OUTB = outb_buffer; /* TDI and TCK change here */
+ for (k = 0; k < delay_scan_io; k++);
+ tdi_data = tdi_data >> 1;
+
+ OUTB = (outb_buffer | PIN_TCK);
+ for (k = 0; k < delay_scan_io; k++);
+ tdo_data = tdo_data >> 1;
+
+ if (GET_TDO()) {
+ tdo_data |= 0x80;
+ }
+ }
+
+ /* Copy TDO data to IN2BUF */
+ IN2BUF[i + in_offset] = tdo_data;
+ }
+
+ tdi_data = OUT2BUF[i + out_offset + 5];
+ tdo_data = 0;
+
+ /* Shift the last byte */
+ for (j = 0; j < bits_last_byte; j++) {
+ if (tdi_data & 0x01) {
+ outb_buffer |= PIN_TDI;
+ }
+ else {
+ outb_buffer &= ~PIN_TDI;
+ }
+
+ /* Assert TMS signal if requested and this is the last bit */
+ if ((j == bits_last_byte - 1) && (tms_count_end > 0)) {
+ outb_buffer |= PIN_TMS;
+ tms_count_end--;
+ tms_sequence_end = tms_sequence_end >> 1;
+ }
+
+ OUTB = outb_buffer; /* TDI and TCK change here */
+ for (k = 0; k < delay_scan_io; k++);
+ tdi_data = tdi_data >> 1;
+
+ OUTB = (outb_buffer | PIN_TCK);
+ for (k = 0; k < delay_scan_io; k++);
+ tdo_data = tdo_data >> 1;
+
+ if (GET_TDO()) {
+ tdo_data |= 0x80;
+ }
+ }
+ tdo_data = tdo_data >> (8 - bits_last_byte);
+
+ /* Copy TDO data to IN2BUF */
+ IN2BUF[i + in_offset] = tdo_data;
+
+ /* Move to correct end state */
+ if (tms_count_end > 0) {
+ jtag_slow_clock_tms(tms_count_end, tms_sequence_end);
+ }
+}
+