source: sandbox/JamPlayerUSB/jbijtag.c@ 110

Last change on this file since 110 was 110, checked in by demin, 14 years ago

improve jtag i/o timing and call usb blaster functions directly from jbitag.c

File size: 32.1 KB
Line 
1/****************************************************************************/
2/* */
3/* Module: jbijtag.c */
4/* */
5/* Copyright (C) Altera Corporation 1998-2001 */
6/* */
7/* Description: Contains JTAG interface functions */
8/* */
9/* Revisions: 2.2 updated state transition paths */
10/* 2.0 added multi-page scan code for 16-bit PORT */
11/* */
12/****************************************************************************/
13
14#include <stdio.h>
15
16#include "jbiexprt.h"
17#include "jbicomp.h"
18#include "jbijtag.h"
19
20char *jbi_workspace = NULL;
21long jbi_workspace_size = 0L;
22
23/****************************************************************************/
24/* */
25/* Enumerated Types */
26/* */
27/****************************************************************************/
28
29/* maximum JTAG IR and DR lengths (in bits) */
30#define JBIC_MAX_JTAG_IR_PREAMBLE 256
31#define JBIC_MAX_JTAG_IR_POSTAMBLE 256
32#define JBIC_MAX_JTAG_IR_LENGTH 512
33#define JBIC_MAX_JTAG_DR_PREAMBLE 1024
34#define JBIC_MAX_JTAG_DR_POSTAMBLE 1024
35#define JBIC_MAX_JTAG_DR_LENGTH 2048
36
37/*
38* Global variable to store the current JTAG state
39*/
40JBIE_JTAG_STATE jbi_jtag_state = JBI_ILLEGAL_JTAG_STATE;
41
42/*
43* Store current stop-state for DR and IR scan commands
44*/
45JBIE_JTAG_STATE jbi_drstop_state = IDLE;
46JBIE_JTAG_STATE jbi_irstop_state = IDLE;
47
48/*
49* Store current padding values
50*/
51unsigned int jbi_dr_preamble = 0;
52unsigned int jbi_dr_postamble = 0;
53unsigned int jbi_ir_preamble = 0;
54unsigned int jbi_ir_postamble = 0;
55unsigned int jbi_dr_length = 0;
56unsigned int jbi_ir_length = 0;
57unsigned char *jbi_dr_preamble_data = NULL;
58unsigned char *jbi_dr_postamble_data = NULL;
59unsigned char *jbi_ir_preamble_data = NULL;
60unsigned char *jbi_ir_postamble_data = NULL;
61unsigned char *jbi_dr_buffer = NULL;
62unsigned char *jbi_ir_buffer = NULL;
63
64/*
65* This structure shows, for each JTAG state, which state is reached after
66* a single TCK clock cycle with TMS high or TMS low, respectively. This
67* describes all possible state transitions in the JTAG state machine.
68*/
69struct JBIS_JTAG_MACHINE
70{
71 JBIE_JTAG_STATE tms_high;
72 JBIE_JTAG_STATE tms_low;
73} jbi_jtag_state_transitions[] =
74{
75/* RESET */ { RESET, IDLE },
76/* IDLE */ { DRSELECT, IDLE },
77/* DRSELECT */ { IRSELECT, DRCAPTURE },
78/* DRCAPTURE */ { DREXIT1, DRSHIFT },
79/* DRSHIFT */ { DREXIT1, DRSHIFT },
80/* DREXIT1 */ { DRUPDATE, DRPAUSE },
81/* DRPAUSE */ { DREXIT2, DRPAUSE },
82/* DREXIT2 */ { DRUPDATE, DRSHIFT },
83/* DRUPDATE */ { DRSELECT, IDLE },
84/* IRSELECT */ { RESET, IRCAPTURE },
85/* IRCAPTURE */ { IREXIT1, IRSHIFT },
86/* IRSHIFT */ { IREXIT1, IRSHIFT },
87/* IREXIT1 */ { IRUPDATE, IRPAUSE },
88/* IRPAUSE */ { IREXIT2, IRPAUSE },
89/* IREXIT2 */ { IRUPDATE, IRSHIFT },
90/* IRUPDATE */ { DRSELECT, IDLE }
91};
92
93/*
94* This table contains the TMS value to be used to take the NEXT STEP on
95* the path to the desired state. The array index is the current state,
96* and the bit position is the desired endstate. To find out which state
97* is used as the intermediate state, look up the TMS value in the
98* jbi_jtag_state_transitions[] table.
99*/
100unsigned short jbi_jtag_path_map[16] =
101{
102/* RST RTI SDRS CDR SDR E1DR PDR E2DR */
103 0x0001, 0xFFFD, 0xFE01, 0xFFE7, 0xFFEF, 0xFF0F, 0xFFBF, 0xFFFF,
104/* UDR SIRS CIR SIR E1IR PIR E2IR UIR */
105 0xFEFD, 0x0001, 0xF3FF, 0xF7FF, 0x87FF, 0xDFFF, 0xFFFF, 0x7FFD
106};
107
108/*
109* Flag bits for jbi_jtag_io() function
110*/
111#define TMS_HIGH 1
112#define TMS_LOW 0
113#define TDI_HIGH 1
114#define TDI_LOW 0
115#define READ_TDO 1
116#define IGNORE_TDO 0
117
118/****************************************************************************/
119/* */
120
121JBI_RETURN_TYPE jbi_init_jtag()
122
123/* */
124/****************************************************************************/
125{
126 /* initial JTAG state is unknown */
127 jbi_jtag_state = JBI_ILLEGAL_JTAG_STATE;
128
129 /* initialize global variables to default state */
130 jbi_drstop_state = IDLE;
131 jbi_irstop_state = IDLE;
132 jbi_dr_preamble = 0;
133 jbi_dr_postamble = 0;
134 jbi_ir_preamble = 0;
135 jbi_ir_postamble = 0;
136 jbi_dr_length = 0;
137 jbi_ir_length = 0;
138
139 if (jbi_workspace != NULL)
140 {
141 jbi_dr_preamble_data = (unsigned char *) jbi_workspace;
142 jbi_dr_postamble_data = &jbi_dr_preamble_data[JBIC_MAX_JTAG_DR_PREAMBLE / 8];
143 jbi_ir_preamble_data = &jbi_dr_postamble_data[JBIC_MAX_JTAG_DR_POSTAMBLE / 8];
144 jbi_ir_postamble_data = &jbi_ir_preamble_data[JBIC_MAX_JTAG_IR_PREAMBLE / 8];
145 jbi_dr_buffer = &jbi_ir_postamble_data[JBIC_MAX_JTAG_IR_POSTAMBLE / 8];
146 jbi_ir_buffer = &jbi_dr_buffer[JBIC_MAX_JTAG_DR_LENGTH / 8];
147 }
148 else
149 {
150 jbi_dr_preamble_data = NULL;
151 jbi_dr_postamble_data = NULL;
152 jbi_ir_preamble_data = NULL;
153 jbi_ir_postamble_data = NULL;
154 jbi_dr_buffer = NULL;
155 jbi_ir_buffer = NULL;
156 }
157
158 return (JBIC_SUCCESS);
159}
160
161/****************************************************************************/
162/* */
163
164JBI_RETURN_TYPE jbi_set_drstop_state
165(
166 JBIE_JTAG_STATE state
167)
168
169/* */
170/****************************************************************************/
171{
172 jbi_drstop_state = state;
173
174 return (JBIC_SUCCESS);
175}
176
177/****************************************************************************/
178/* */
179
180JBI_RETURN_TYPE jbi_set_irstop_state
181(
182 JBIE_JTAG_STATE state
183)
184
185/* */
186/****************************************************************************/
187{
188 jbi_irstop_state = state;
189
190 return (JBIC_SUCCESS);
191}
192
193/****************************************************************************/
194/* */
195
196JBI_RETURN_TYPE jbi_set_dr_preamble
197(
198 unsigned int count,
199 unsigned int start_index,
200 unsigned char *preamble_data
201)
202
203/* */
204/****************************************************************************/
205{
206 JBI_RETURN_TYPE status = JBIC_SUCCESS;
207 unsigned int i;
208 unsigned int j;
209
210 if (jbi_workspace != NULL)
211 {
212 if (count > JBIC_MAX_JTAG_DR_PREAMBLE)
213 {
214 status = JBIC_OUT_OF_MEMORY;
215 }
216 else
217 {
218 jbi_dr_preamble = count;
219 }
220 }
221 else
222 {
223 if (count > jbi_dr_preamble)
224 {
225 jbi_free(jbi_dr_preamble_data);
226 jbi_dr_preamble_data = (unsigned char *) jbi_malloc((count + 7) >> 3);
227
228 if (jbi_dr_preamble_data == NULL)
229 {
230 status = JBIC_OUT_OF_MEMORY;
231 }
232 else
233 {
234 jbi_dr_preamble = count;
235 }
236 }
237 else
238 {
239 jbi_dr_preamble = count;
240 }
241 }
242
243 if (status == JBIC_SUCCESS)
244 {
245 for (i = 0; i < count; ++i)
246 {
247 j = i + start_index;
248
249 if (preamble_data == NULL)
250 {
251 jbi_dr_preamble_data[i >> 3] |= (1 << (i & 7));
252 }
253 else
254 {
255 if (preamble_data[j >> 3] & (1 << (j & 7)))
256 {
257 jbi_dr_preamble_data[i >> 3] |= (1 << (i & 7));
258 }
259 else
260 {
261 jbi_dr_preamble_data[i >> 3] &=
262 ~(unsigned int) (1 << (i & 7));
263 }
264 }
265 }
266 }
267
268 return (status);
269}
270
271/****************************************************************************/
272/* */
273
274JBI_RETURN_TYPE jbi_set_ir_preamble
275(
276 unsigned int count,
277 unsigned int start_index,
278 unsigned char *preamble_data
279)
280
281/* */
282/****************************************************************************/
283{
284 JBI_RETURN_TYPE status = JBIC_SUCCESS;
285 unsigned int i;
286 unsigned int j;
287
288 if (jbi_workspace != NULL)
289 {
290 if (count > JBIC_MAX_JTAG_IR_PREAMBLE)
291 {
292 status = JBIC_OUT_OF_MEMORY;
293 }
294 else
295 {
296 jbi_ir_preamble = count;
297 }
298 }
299 else
300 {
301 if (count > jbi_ir_preamble)
302 {
303 jbi_free(jbi_ir_preamble_data);
304 jbi_ir_preamble_data = (unsigned char *) jbi_malloc((count + 7) >> 3);
305
306 if (jbi_ir_preamble_data == NULL)
307 {
308 status = JBIC_OUT_OF_MEMORY;
309 }
310 else
311 {
312 jbi_ir_preamble = count;
313 }
314 }
315 else
316 {
317 jbi_ir_preamble = count;
318 }
319 }
320
321 if (status == JBIC_SUCCESS)
322 {
323 for (i = 0; i < count; ++i)
324 {
325 j = i + start_index;
326
327 if (preamble_data == NULL)
328 {
329 jbi_ir_preamble_data[i >> 3] |= (1 << (i & 7));
330 }
331 else
332 {
333 if (preamble_data[j >> 3] & (1 << (j & 7)))
334 {
335 jbi_ir_preamble_data[i >> 3] |= (1 << (i & 7));
336 }
337 else
338 {
339 jbi_ir_preamble_data[i >> 3] &=
340 ~(unsigned int) (1 << (i & 7));
341 }
342 }
343 }
344 }
345
346 return (status);
347}
348
349/****************************************************************************/
350/* */
351
352JBI_RETURN_TYPE jbi_set_dr_postamble
353(
354 unsigned int count,
355 unsigned int start_index,
356 unsigned char *postamble_data
357)
358
359/* */
360/****************************************************************************/
361{
362 JBI_RETURN_TYPE status = JBIC_SUCCESS;
363 unsigned int i;
364 unsigned int j;
365
366 if (jbi_workspace != NULL)
367 {
368 if (count > JBIC_MAX_JTAG_DR_POSTAMBLE)
369 {
370 status = JBIC_OUT_OF_MEMORY;
371 }
372 else
373 {
374 jbi_dr_postamble = count;
375 }
376 }
377 else
378 {
379 if (count > jbi_dr_postamble)
380 {
381 jbi_free(jbi_dr_postamble_data);
382 jbi_dr_postamble_data = (unsigned char *) jbi_malloc((count + 7) >> 3);
383
384 if (jbi_dr_postamble_data == NULL)
385 {
386 status = JBIC_OUT_OF_MEMORY;
387 }
388 else
389 {
390 jbi_dr_postamble = count;
391 }
392 }
393 else
394 {
395 jbi_dr_postamble = count;
396 }
397 }
398
399 if (status == JBIC_SUCCESS)
400 {
401 for (i = 0; i < count; ++i)
402 {
403 j = i + start_index;
404
405 if (postamble_data == NULL)
406 {
407 jbi_dr_postamble_data[i >> 3] |= (1 << (i & 7));
408 }
409 else
410 {
411 if (postamble_data[j >> 3] & (1 << (j & 7)))
412 {
413 jbi_dr_postamble_data[i >> 3] |= (1 << (i & 7));
414 }
415 else
416 {
417 jbi_dr_postamble_data[i >> 3] &=
418 ~(unsigned int) (1 << (i & 7));
419 }
420 }
421 }
422 }
423
424 return (status);
425}
426
427/****************************************************************************/
428/* */
429
430JBI_RETURN_TYPE jbi_set_ir_postamble
431(
432 unsigned int count,
433 unsigned int start_index,
434 unsigned char *postamble_data
435)
436
437/* */
438/****************************************************************************/
439{
440 JBI_RETURN_TYPE status = JBIC_SUCCESS;
441 unsigned int i;
442 unsigned int j;
443
444 if (jbi_workspace != NULL)
445 {
446 if (count > JBIC_MAX_JTAG_IR_POSTAMBLE)
447 {
448 status = JBIC_OUT_OF_MEMORY;
449 }
450 else
451 {
452 jbi_ir_postamble = count;
453 }
454 }
455 else
456 {
457 if (count > jbi_ir_postamble)
458 {
459 jbi_free(jbi_ir_postamble_data);
460 jbi_ir_postamble_data = (unsigned char *) jbi_malloc((count + 7) >> 3);
461
462 if (jbi_ir_postamble_data == NULL)
463 {
464 status = JBIC_OUT_OF_MEMORY;
465 }
466 else
467 {
468 jbi_ir_postamble = count;
469 }
470 }
471 else
472 {
473 jbi_ir_postamble = count;
474 }
475 }
476
477 if (status == JBIC_SUCCESS)
478 {
479 for (i = 0; i < count; ++i)
480 {
481 j = i + start_index;
482
483 if (postamble_data == NULL)
484 {
485 jbi_ir_postamble_data[i >> 3] |= (1 << (i & 7));
486 }
487 else
488 {
489 if (postamble_data[j >> 3] & (1 << (j & 7)))
490 {
491 jbi_ir_postamble_data[i >> 3] |= (1 << (i & 7));
492 }
493 else
494 {
495 jbi_ir_postamble_data[i >> 3] &=
496 ~(unsigned int) (1 << (i & 7));
497 }
498 }
499 }
500 }
501
502 return (status);
503}
504
505/****************************************************************************/
506/* */
507
508void jbi_jtag_reset_idle(void)
509
510/* */
511/****************************************************************************/
512{
513 int i;
514
515 /*
516 * Go to Test Logic Reset (no matter what the starting state may be)
517 */
518 for (i = 0; i < 5; ++i)
519 {
520 jbi_jtag_io(TMS_HIGH, TDI_LOW, IGNORE_TDO);
521 }
522
523 /*
524 * Now step to Run Test / Idle
525 */
526 jbi_jtag_io(TMS_LOW, TDI_LOW, IGNORE_TDO);
527
528 jbi_jtag_state = IDLE;
529}
530
531/****************************************************************************/
532/* */
533
534JBI_RETURN_TYPE jbi_goto_jtag_state
535(
536 JBIE_JTAG_STATE state
537)
538
539/* */
540/****************************************************************************/
541{
542 int tms;
543 int count = 0;
544 JBI_RETURN_TYPE status = JBIC_SUCCESS;
545
546 if (jbi_jtag_state == JBI_ILLEGAL_JTAG_STATE)
547 {
548 /* initialize JTAG chain to known state */
549 jbi_jtag_reset_idle();
550 }
551
552 if (jbi_jtag_state == state)
553 {
554 /*
555 * We are already in the desired state. If it is a stable state,
556 * loop here. Otherwise do nothing (no clock cycles).
557 */
558 if ((state == IDLE) ||
559 (state == DRSHIFT) ||
560 (state == DRPAUSE) ||
561 (state == IRSHIFT) ||
562 (state == IRPAUSE))
563 {
564 jbi_jtag_io(TMS_LOW, TDI_LOW, IGNORE_TDO);
565 }
566 else if (state == RESET)
567 {
568 jbi_jtag_io(TMS_HIGH, TDI_LOW, IGNORE_TDO);
569 }
570 }
571 else
572 {
573 while ((jbi_jtag_state != state) && (count < 9))
574 {
575 /*
576 * Get TMS value to take a step toward desired state
577 */
578 tms = (jbi_jtag_path_map[jbi_jtag_state] & (1 << state)) ?
579 TMS_HIGH : TMS_LOW;
580
581 /*
582 * Take a step
583 */
584 jbi_jtag_io(tms, TDI_LOW, IGNORE_TDO);
585
586 if (tms)
587 {
588 jbi_jtag_state =
589 jbi_jtag_state_transitions[jbi_jtag_state].tms_high;
590 }
591 else
592 {
593 jbi_jtag_state =
594 jbi_jtag_state_transitions[jbi_jtag_state].tms_low;
595 }
596
597 ++count;
598 }
599 }
600
601 if (jbi_jtag_state != state)
602 {
603 status = JBIC_INTERNAL_ERROR;
604 }
605
606 return (status);
607}
608
609/****************************************************************************/
610/* */
611
612JBI_RETURN_TYPE jbi_do_wait_cycles
613(
614 long cycles,
615 JBIE_JTAG_STATE wait_state
616)
617
618/* */
619/* Description: Causes JTAG hardware to loop in the specified stable */
620/* state for the specified number of TCK clock cycles. */
621/* */
622/* Returns: JBIC_SUCCESS for success, else appropriate error code */
623/* */
624/****************************************************************************/
625{
626 int tms, flag;
627 long count;
628 JBI_RETURN_TYPE status = JBIC_SUCCESS;
629
630 if (jbi_jtag_state != wait_state)
631 {
632 status = jbi_goto_jtag_state(wait_state);
633 }
634
635 if (status == JBIC_SUCCESS)
636 {
637 /*
638 * Set TMS high to loop in RESET state
639 * Set TMS low to loop in any other stable state
640 */
641 tms = (wait_state == RESET) ? TMS_HIGH : TMS_LOW;
642
643 usb_blaster_wait(cycles, tms);
644 }
645
646 return (status);
647}
648
649/****************************************************************************/
650/* */
651
652JBI_RETURN_TYPE jbi_do_wait_microseconds
653(
654 long microseconds,
655 JBIE_JTAG_STATE wait_state
656)
657
658/* */
659/* Description: Causes JTAG hardware to sit in the specified stable */
660/* state for the specified duration of real time. If */
661/* no JTAG operations have been performed yet, then only */
662/* a delay is performed. This permits the WAIT USECS */
663/* statement to be used in VECTOR programs without causing */
664/* any JTAG operations. */
665/* */
666/* Returns: JBIC_SUCCESS for success, else appropriate error code */
667/* */
668/****************************************************************************/
669{
670 JBI_RETURN_TYPE status = JBIC_SUCCESS;
671
672 if ((jbi_jtag_state != JBI_ILLEGAL_JTAG_STATE) &&
673 (jbi_jtag_state != wait_state))
674 {
675 status = jbi_goto_jtag_state(wait_state);
676 }
677
678 if (status == JBIC_SUCCESS)
679 {
680 /*
681 * Wait for specified time interval
682 */
683 jbi_delay(microseconds);
684 }
685
686 return (status);
687}
688
689/****************************************************************************/
690/* */
691
692void jbi_jtag_concatenate_data
693(
694 unsigned char *buffer,
695 unsigned char *preamble_data,
696 unsigned int preamble_count,
697 unsigned char *target_data,
698 unsigned long start_index,
699 unsigned int target_count,
700 unsigned char *postamble_data,
701 unsigned int postamble_count
702)
703
704/* */
705/* Description: Copies preamble data, target data, and postamble data */
706/* into one buffer for IR or DR scans. */
707/* */
708/* Returns: nothing */
709/* */
710/****************************************************************************/
711{
712 unsigned long i;
713 unsigned long j;
714 unsigned long k;
715
716 for (i = 0L; i < preamble_count; ++i)
717 {
718 if (preamble_data[i >> 3L] & (1L << (i & 7L)))
719 {
720 buffer[i >> 3L] |= (1L << (i & 7L));
721 }
722 else
723 {
724 buffer[i >> 3L] &= ~(unsigned int) (1L << (i & 7L));
725 }
726 }
727
728 j = start_index;
729 k = preamble_count + target_count;
730 for (; i < k; ++i, ++j)
731 {
732 if (target_data[j >> 3L] & (1L << (j & 7L)))
733 {
734 buffer[i >> 3L] |= (1L << (i & 7L));
735 }
736 else
737 {
738 buffer[i >> 3L] &= ~(unsigned int) (1L << (i & 7L));
739 }
740 }
741
742 j = 0L;
743 k = preamble_count + target_count + postamble_count;
744 for (; i < k; ++i, ++j)
745 {
746 if (postamble_data[j >> 3L] & (1L << (j & 7L)))
747 {
748 buffer[i >> 3L] |= (1L << (i & 7L));
749 }
750 else
751 {
752 buffer[i >> 3L] &= ~(unsigned int) (1L << (i & 7L));
753 }
754 }
755}
756
757int jbi_jtag_drscan
758(
759 int start_state,
760 int count,
761 unsigned char *tdi,
762 unsigned char *tdo
763)
764{
765 int i = 0;
766 int tdo_bit = 0;
767 int status = 1;
768
769 /*
770 * First go to DRSHIFT state
771 */
772 switch (start_state)
773 {
774 case 0: /* IDLE */
775 jbi_jtag_io(1, 0, 0); /* DRSELECT */
776 jbi_jtag_io(0, 0, 0); /* DRCAPTURE */
777 jbi_jtag_io(0, 0, 0); /* DRSHIFT */
778 break;
779
780 case 1: /* DRPAUSE */
781 jbi_jtag_io(1, 0, 0); /* DREXIT2 */
782 jbi_jtag_io(1, 0, 0); /* DRUPDATE */
783 jbi_jtag_io(1, 0, 0); /* DRSELECT */
784 jbi_jtag_io(0, 0, 0); /* DRCAPTURE */
785 jbi_jtag_io(0, 0, 0); /* DRSHIFT */
786 break;
787
788 case 2: /* IRPAUSE */
789 jbi_jtag_io(1, 0, 0); /* IREXIT2 */
790 jbi_jtag_io(1, 0, 0); /* IRUPDATE */
791 jbi_jtag_io(1, 0, 0); /* DRSELECT */
792 jbi_jtag_io(0, 0, 0); /* DRCAPTURE */
793 jbi_jtag_io(0, 0, 0); /* DRSHIFT */
794 break;
795
796 default:
797 status = 0;
798 }
799
800 if (status)
801 {
802 /* loop in the SHIFT-DR state */
803 usb_blaster_scan(count, tdi, tdo);
804
805 jbi_jtag_io(0, 0, 0); /* DRPAUSE */
806 }
807
808 return (status);
809}
810
811int jbi_jtag_irscan
812(
813 int start_state,
814 int count,
815 unsigned char *tdi,
816 unsigned char *tdo
817)
818{
819 int i = 0;
820 int tdo_bit = 0;
821 int status = 1;
822
823 /*
824 * First go to IRSHIFT state
825 */
826 switch (start_state)
827 {
828 case 0: /* IDLE */
829 jbi_jtag_io(1, 0, 0); /* DRSELECT */
830 jbi_jtag_io(1, 0, 0); /* IRSELECT */
831 jbi_jtag_io(0, 0, 0); /* IRCAPTURE */
832 jbi_jtag_io(0, 0, 0); /* IRSHIFT */
833 break;
834
835 case 1: /* DRPAUSE */
836 jbi_jtag_io(1, 0, 0); /* DREXIT2 */
837 jbi_jtag_io(1, 0, 0); /* DRUPDATE */
838 jbi_jtag_io(1, 0, 0); /* DRSELECT */
839 jbi_jtag_io(1, 0, 0); /* IRSELECT */
840 jbi_jtag_io(0, 0, 0); /* IRCAPTURE */
841 jbi_jtag_io(0, 0, 0); /* IRSHIFT */
842 break;
843
844 case 2: /* IRPAUSE */
845 jbi_jtag_io(1, 0, 0); /* IREXIT2 */
846 jbi_jtag_io(1, 0, 0); /* IRUPDATE */
847 jbi_jtag_io(1, 0, 0); /* DRSELECT */
848 jbi_jtag_io(1, 0, 0); /* IRSELECT */
849 jbi_jtag_io(0, 0, 0); /* IRCAPTURE */
850 jbi_jtag_io(0, 0, 0); /* IRSHIFT */
851 break;
852
853 default:
854 status = 0;
855 }
856
857 if (status)
858 {
859 /* loop in the SHIFT-IR state */
860 usb_blaster_scan(count, tdi, tdo);
861
862 jbi_jtag_io(0, 0, 0); /* IRPAUSE */
863 }
864
865 return (status);
866}
867
868/****************************************************************************/
869/* */
870
871void jbi_jtag_extract_target_data
872(
873 unsigned char *buffer,
874 unsigned char *target_data,
875 unsigned int start_index,
876 unsigned int preamble_count,
877 unsigned int target_count
878)
879
880/* */
881/* Description: Copies target data from scan buffer, filtering out */
882/* preamble and postamble data. */
883/* */
884/* Returns: nothing */
885/* */
886/****************************************************************************/
887{
888 unsigned int i;
889 unsigned int j;
890 unsigned int k;
891
892 j = preamble_count;
893 k = start_index + target_count;
894 for (i = start_index; i < k; ++i, ++j)
895 {
896 if (buffer[j >> 3] & (1 << (j & 7)))
897 {
898 target_data[i >> 3] |= (1 << (i & 7));
899 }
900 else
901 {
902 target_data[i >> 3] &= ~(unsigned int) (1 << (i & 7));
903 }
904 }
905}
906
907/****************************************************************************/
908/* */
909
910JBI_RETURN_TYPE jbi_do_irscan
911(
912 unsigned int count,
913 unsigned char *tdi_data,
914 unsigned int start_index
915)
916
917/* */
918/* Description: Shifts data into instruction register */
919/* */
920/* Returns: JBIC_SUCCESS for success, else appropriate error code */
921/* */
922/****************************************************************************/
923{
924 int start_code = 0;
925 unsigned int alloc_chars = 0;
926 unsigned int shift_count = jbi_ir_preamble + count + jbi_ir_postamble;
927 JBI_RETURN_TYPE status = JBIC_SUCCESS;
928 JBIE_JTAG_STATE start_state = JBI_ILLEGAL_JTAG_STATE;
929
930 switch (jbi_jtag_state)
931 {
932 case JBI_ILLEGAL_JTAG_STATE:
933 case RESET:
934 case IDLE:
935 start_code = 0;
936 start_state = IDLE;
937 break;
938
939 case DRSELECT:
940 case DRCAPTURE:
941 case DRSHIFT:
942 case DREXIT1:
943 case DRPAUSE:
944 case DREXIT2:
945 case DRUPDATE:
946 start_code = 1;
947 start_state = DRPAUSE;
948 break;
949
950 case IRSELECT:
951 case IRCAPTURE:
952 case IRSHIFT:
953 case IREXIT1:
954 case IRPAUSE:
955 case IREXIT2:
956 case IRUPDATE:
957 start_code = 2;
958 start_state = IRPAUSE;
959 break;
960
961 default:
962 status = JBIC_INTERNAL_ERROR;
963 break;
964 }
965
966 if (status == JBIC_SUCCESS)
967 {
968 if (jbi_jtag_state != start_state)
969 {
970 status = jbi_goto_jtag_state(start_state);
971 }
972 }
973
974 if (status == JBIC_SUCCESS)
975 {
976 if (jbi_workspace != NULL)
977 {
978 if (shift_count > JBIC_MAX_JTAG_IR_LENGTH)
979 {
980 status = JBIC_OUT_OF_MEMORY;
981 }
982 }
983 else if (shift_count > jbi_ir_length)
984 {
985 alloc_chars = (shift_count + 7) >> 3;
986 jbi_free(jbi_ir_buffer);
987 jbi_ir_buffer = (unsigned char *) jbi_malloc(alloc_chars);
988
989 if (jbi_ir_buffer == NULL)
990 {
991 status = JBIC_OUT_OF_MEMORY;
992 }
993 else
994 {
995 jbi_ir_length = alloc_chars * 8;
996 }
997 }
998 }
999
1000 if (status == JBIC_SUCCESS)
1001 {
1002 /*
1003 * Copy preamble data, IR data, and postamble data into a buffer
1004 */
1005 jbi_jtag_concatenate_data
1006 (
1007 jbi_ir_buffer,
1008 jbi_ir_preamble_data,
1009 jbi_ir_preamble,
1010 tdi_data,
1011 start_index,
1012 count,
1013 jbi_ir_postamble_data,
1014 jbi_ir_postamble
1015 );
1016
1017 /*
1018 * Do the IRSCAN
1019 */
1020 jbi_jtag_irscan
1021 (
1022 start_code,
1023 shift_count,
1024 jbi_ir_buffer,
1025 NULL
1026 );
1027
1028 /* jbi_jtag_irscan() always ends in IRPAUSE state */
1029 jbi_jtag_state = IRPAUSE;
1030 }
1031
1032 if (status == JBIC_SUCCESS)
1033 {
1034 if (jbi_irstop_state != IRPAUSE)
1035 {
1036 status = jbi_goto_jtag_state(jbi_irstop_state);
1037 }
1038 }
1039
1040 return (status);
1041}
1042
1043/****************************************************************************/
1044/* */
1045
1046JBI_RETURN_TYPE jbi_swap_ir
1047(
1048 unsigned int count,
1049 unsigned char *in_data,
1050 unsigned int in_index,
1051 unsigned char *out_data,
1052 unsigned int out_index
1053)
1054
1055/* */
1056/* Description: Shifts data into instruction register, capturing output */
1057/* data */
1058/* */
1059/* Returns: JBIC_SUCCESS for success, else appropriate error code */
1060/* */
1061/****************************************************************************/
1062{
1063 int start_code = 0;
1064 unsigned int alloc_chars = 0;
1065 unsigned int shift_count = jbi_ir_preamble + count + jbi_ir_postamble;
1066 JBI_RETURN_TYPE status = JBIC_SUCCESS;
1067 JBIE_JTAG_STATE start_state = JBI_ILLEGAL_JTAG_STATE;
1068
1069 switch (jbi_jtag_state)
1070 {
1071 case JBI_ILLEGAL_JTAG_STATE:
1072 case RESET:
1073 case IDLE:
1074 start_code = 0;
1075 start_state = IDLE;
1076 break;
1077
1078 case DRSELECT:
1079 case DRCAPTURE:
1080 case DRSHIFT:
1081 case DREXIT1:
1082 case DRPAUSE:
1083 case DREXIT2:
1084 case DRUPDATE:
1085 start_code = 1;
1086 start_state = DRPAUSE;
1087 break;
1088
1089 case IRSELECT:
1090 case IRCAPTURE:
1091 case IRSHIFT:
1092 case IREXIT1:
1093 case IRPAUSE:
1094 case IREXIT2:
1095 case IRUPDATE:
1096 start_code = 2;
1097 start_state = IRPAUSE;
1098 break;
1099
1100 default:
1101 status = JBIC_INTERNAL_ERROR;
1102 break;
1103 }
1104
1105 if (status == JBIC_SUCCESS)
1106 {
1107 if (jbi_jtag_state != start_state)
1108 {
1109 status = jbi_goto_jtag_state(start_state);
1110 }
1111 }
1112
1113 if (status == JBIC_SUCCESS)
1114 {
1115 if (jbi_workspace != NULL)
1116 {
1117 if (shift_count > JBIC_MAX_JTAG_IR_LENGTH)
1118 {
1119 status = JBIC_OUT_OF_MEMORY;
1120 }
1121 }
1122 else if (shift_count > jbi_ir_length)
1123 {
1124 alloc_chars = (shift_count + 7) >> 3;
1125 jbi_free(jbi_ir_buffer);
1126 jbi_ir_buffer = (unsigned char *) jbi_malloc(alloc_chars);
1127
1128 if (jbi_ir_buffer == NULL)
1129 {
1130 status = JBIC_OUT_OF_MEMORY;
1131 }
1132 else
1133 {
1134 jbi_ir_length = alloc_chars * 8;
1135 }
1136 }
1137 }
1138
1139 if (status == JBIC_SUCCESS)
1140 {
1141 /*
1142 * Copy preamble data, IR data, and postamble data into a buffer
1143 */
1144 jbi_jtag_concatenate_data
1145 (
1146 jbi_ir_buffer,
1147 jbi_ir_preamble_data,
1148 jbi_ir_preamble,
1149 in_data,
1150 in_index,
1151 count,
1152 jbi_ir_postamble_data,
1153 jbi_ir_postamble
1154 );
1155
1156 /*
1157 * Do the IRSCAN
1158 */
1159 jbi_jtag_irscan
1160 (
1161 start_code,
1162 shift_count,
1163 jbi_ir_buffer,
1164 jbi_ir_buffer
1165 );
1166
1167 /* jbi_jtag_irscan() always ends in IRPAUSE state */
1168 jbi_jtag_state = IRPAUSE;
1169 }
1170
1171 if (status == JBIC_SUCCESS)
1172 {
1173 if (jbi_irstop_state != IRPAUSE)
1174 {
1175 status = jbi_goto_jtag_state(jbi_irstop_state);
1176 }
1177 }
1178
1179 if (status == JBIC_SUCCESS)
1180 {
1181 /*
1182 * Now extract the returned data from the buffer
1183 */
1184 jbi_jtag_extract_target_data
1185 (
1186 jbi_ir_buffer,
1187 out_data,
1188 out_index,
1189 jbi_ir_preamble,
1190 count
1191 );
1192 }
1193
1194 return (status);
1195}
1196
1197/****************************************************************************/
1198/* */
1199
1200JBI_RETURN_TYPE jbi_do_drscan
1201(
1202 unsigned int count,
1203 unsigned char *tdi_data,
1204 unsigned long start_index
1205)
1206
1207/* */
1208/* Description: Shifts data into data register (ignoring output data) */
1209/* */
1210/* Returns: JBIC_SUCCESS for success, else appropriate error code */
1211/* */
1212/****************************************************************************/
1213{
1214 int start_code = 0;
1215 unsigned int alloc_chars = 0;
1216 unsigned int shift_count = jbi_dr_preamble + count + jbi_dr_postamble;
1217 JBI_RETURN_TYPE status = JBIC_SUCCESS;
1218 JBIE_JTAG_STATE start_state = JBI_ILLEGAL_JTAG_STATE;
1219
1220 switch (jbi_jtag_state)
1221 {
1222 case JBI_ILLEGAL_JTAG_STATE:
1223 case RESET:
1224 case IDLE:
1225 start_code = 0;
1226 start_state = IDLE;
1227 break;
1228
1229 case DRSELECT:
1230 case DRCAPTURE:
1231 case DRSHIFT:
1232 case DREXIT1:
1233 case DRPAUSE:
1234 case DREXIT2:
1235 case DRUPDATE:
1236 start_code = 1;
1237 start_state = DRPAUSE;
1238 break;
1239
1240 case IRSELECT:
1241 case IRCAPTURE:
1242 case IRSHIFT:
1243 case IREXIT1:
1244 case IRPAUSE:
1245 case IREXIT2:
1246 case IRUPDATE:
1247 start_code = 2;
1248 start_state = IRPAUSE;
1249 break;
1250
1251 default:
1252 status = JBIC_INTERNAL_ERROR;
1253 break;
1254 }
1255
1256 if (status == JBIC_SUCCESS)
1257 {
1258 if (jbi_jtag_state != start_state)
1259 {
1260 status = jbi_goto_jtag_state(start_state);
1261 }
1262 }
1263
1264 if (status == JBIC_SUCCESS)
1265 {
1266 if (jbi_workspace != NULL)
1267 {
1268 if (shift_count > JBIC_MAX_JTAG_DR_LENGTH)
1269 {
1270 status = JBIC_OUT_OF_MEMORY;
1271 }
1272 }
1273 else if (shift_count > jbi_dr_length)
1274 {
1275 alloc_chars = (shift_count + 7) >> 3;
1276 jbi_free(jbi_dr_buffer);
1277 jbi_dr_buffer = (unsigned char *) jbi_malloc(alloc_chars);
1278
1279 if (jbi_dr_buffer == NULL)
1280 {
1281 status = JBIC_OUT_OF_MEMORY;
1282 }
1283 else
1284 {
1285 jbi_dr_length = alloc_chars * 8;
1286 }
1287 }
1288 }
1289
1290 if (status == JBIC_SUCCESS)
1291 {
1292 /*
1293 * Copy preamble data, DR data, and postamble data into a buffer
1294 */
1295 jbi_jtag_concatenate_data
1296 (
1297 jbi_dr_buffer,
1298 jbi_dr_preamble_data,
1299 jbi_dr_preamble,
1300 tdi_data,
1301 start_index,
1302 count,
1303 jbi_dr_postamble_data,
1304 jbi_dr_postamble
1305 );
1306
1307 /*
1308 * Do the DRSCAN
1309 */
1310 jbi_jtag_drscan
1311 (
1312 start_code,
1313 shift_count,
1314 jbi_dr_buffer,
1315 NULL
1316 );
1317
1318 /* jbi_jtag_drscan() always ends in DRPAUSE state */
1319 jbi_jtag_state = DRPAUSE;
1320 }
1321
1322 if (status == JBIC_SUCCESS)
1323 {
1324 if (jbi_drstop_state != DRPAUSE)
1325 {
1326 status = jbi_goto_jtag_state(jbi_drstop_state);
1327 }
1328 }
1329
1330 return (status);
1331}
1332
1333/****************************************************************************/
1334/* */
1335
1336JBI_RETURN_TYPE jbi_swap_dr
1337(
1338 unsigned int count,
1339 unsigned char *in_data,
1340 unsigned long in_index,
1341 unsigned char *out_data,
1342 unsigned int out_index
1343)
1344
1345/* */
1346/* Description: Shifts data into data register, capturing output data */
1347/* */
1348/* Returns: JBIC_SUCCESS for success, else appropriate error code */
1349/* */
1350/****************************************************************************/
1351{
1352 int start_code = 0;
1353 unsigned int alloc_chars = 0;
1354 unsigned int shift_count = jbi_dr_preamble + count + jbi_dr_postamble;
1355 JBI_RETURN_TYPE status = JBIC_SUCCESS;
1356 JBIE_JTAG_STATE start_state = JBI_ILLEGAL_JTAG_STATE;
1357
1358 switch (jbi_jtag_state)
1359 {
1360 case JBI_ILLEGAL_JTAG_STATE:
1361 case RESET:
1362 case IDLE:
1363 start_code = 0;
1364 start_state = IDLE;
1365 break;
1366
1367 case DRSELECT:
1368 case DRCAPTURE:
1369 case DRSHIFT:
1370 case DREXIT1:
1371 case DRPAUSE:
1372 case DREXIT2:
1373 case DRUPDATE:
1374 start_code = 1;
1375 start_state = DRPAUSE;
1376 break;
1377
1378 case IRSELECT:
1379 case IRCAPTURE:
1380 case IRSHIFT:
1381 case IREXIT1:
1382 case IRPAUSE:
1383 case IREXIT2:
1384 case IRUPDATE:
1385 start_code = 2;
1386 start_state = IRPAUSE;
1387 break;
1388
1389 default:
1390 status = JBIC_INTERNAL_ERROR;
1391 break;
1392 }
1393
1394 if (status == JBIC_SUCCESS)
1395 {
1396 if (jbi_jtag_state != start_state)
1397 {
1398 status = jbi_goto_jtag_state(start_state);
1399 }
1400 }
1401
1402 if (status == JBIC_SUCCESS)
1403 {
1404 if (jbi_workspace != NULL)
1405 {
1406 if (shift_count > JBIC_MAX_JTAG_DR_LENGTH)
1407 {
1408 status = JBIC_OUT_OF_MEMORY;
1409 }
1410 }
1411 else if (shift_count > jbi_dr_length)
1412 {
1413 alloc_chars = (shift_count + 7) >> 3;
1414 jbi_free(jbi_dr_buffer);
1415 jbi_dr_buffer = (unsigned char *) jbi_malloc(alloc_chars);
1416
1417 if (jbi_dr_buffer == NULL)
1418 {
1419 status = JBIC_OUT_OF_MEMORY;
1420 }
1421 else
1422 {
1423 jbi_dr_length = alloc_chars * 8;
1424 }
1425 }
1426 }
1427
1428 if (status == JBIC_SUCCESS)
1429 {
1430 /*
1431 * Copy preamble data, DR data, and postamble data into a buffer
1432 */
1433 jbi_jtag_concatenate_data
1434 (
1435 jbi_dr_buffer,
1436 jbi_dr_preamble_data,
1437 jbi_dr_preamble,
1438 in_data,
1439 in_index,
1440 count,
1441 jbi_dr_postamble_data,
1442 jbi_dr_postamble
1443 );
1444
1445 /*
1446 * Do the DRSCAN
1447 */
1448 jbi_jtag_drscan
1449 (
1450 start_code,
1451 shift_count,
1452 jbi_dr_buffer,
1453 jbi_dr_buffer
1454 );
1455
1456 /* jbi_jtag_drscan() always ends in DRPAUSE state */
1457 jbi_jtag_state = DRPAUSE;
1458 }
1459
1460 if (status == JBIC_SUCCESS)
1461 {
1462 if (jbi_drstop_state != DRPAUSE)
1463 {
1464 status = jbi_goto_jtag_state(jbi_drstop_state);
1465 }
1466 }
1467
1468 if (status == JBIC_SUCCESS)
1469 {
1470 /*
1471 * Now extract the returned data from the buffer
1472 */
1473 jbi_jtag_extract_target_data
1474 (
1475 jbi_dr_buffer,
1476 out_data,
1477 out_index,
1478 jbi_dr_preamble,
1479 count
1480 );
1481 }
1482
1483 return (status);
1484}
1485
1486/****************************************************************************/
1487/* */
1488
1489void jbi_free_jtag_padding_buffers(int reset_jtag)
1490
1491/* */
1492/* Description: Frees memory allocated for JTAG IR and DR buffers */
1493/* */
1494/* Returns: nothing */
1495/* */
1496/****************************************************************************/
1497{
1498 /*
1499 * If the JTAG interface was used, reset it to TLR
1500 */
1501 if (reset_jtag && (jbi_jtag_state != JBI_ILLEGAL_JTAG_STATE))
1502 {
1503 jbi_jtag_reset_idle();
1504 }
1505
1506 if (jbi_workspace == NULL)
1507 {
1508 if (jbi_dr_preamble_data != NULL)
1509 {
1510 jbi_free(jbi_dr_preamble_data);
1511 jbi_dr_preamble_data = NULL;
1512 }
1513
1514 if (jbi_dr_postamble_data != NULL)
1515 {
1516 jbi_free(jbi_dr_postamble_data);
1517 jbi_dr_postamble_data = NULL;
1518 }
1519
1520 if (jbi_dr_buffer != NULL)
1521 {
1522 jbi_free(jbi_dr_buffer);
1523 jbi_dr_buffer = NULL;
1524 }
1525
1526 if (jbi_ir_preamble_data != NULL)
1527 {
1528 jbi_free(jbi_ir_preamble_data);
1529 jbi_ir_preamble_data = NULL;
1530 }
1531
1532 if (jbi_ir_postamble_data != NULL)
1533 {
1534 jbi_free(jbi_ir_postamble_data);
1535 jbi_ir_postamble_data = NULL;
1536 }
1537
1538 if (jbi_ir_buffer != NULL)
1539 {
1540 jbi_free(jbi_ir_buffer);
1541 jbi_ir_buffer = NULL;
1542 }
1543 }
1544}
Note: See TracBrowser for help on using the repository browser.