source: sandbox/JamPlayerUSB/jbijtag.c@ 109

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

First working version

File size: 32.9 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, 0);
521 }
522
523 /*
524 * Now step to Run Test / Idle
525 */
526 jbi_jtag_io(TMS_LOW, TDI_LOW, IGNORE_TDO, 0);
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, 0);
565 }
566 else if (state == RESET)
567 {
568 jbi_jtag_io(TMS_HIGH, TDI_LOW, IGNORE_TDO, 0);
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, 0);
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 flag = (cycles % 8 != 0 || tms) ? 0 : 1;
644
645 for (count = 0L; count < cycles; count++)
646 {
647 jbi_jtag_io(tms, TDI_LOW, IGNORE_TDO, flag);
648 }
649 }
650
651 return (status);
652}
653
654/****************************************************************************/
655/* */
656
657JBI_RETURN_TYPE jbi_do_wait_microseconds
658(
659 long microseconds,
660 JBIE_JTAG_STATE wait_state
661)
662
663/* */
664/* Description: Causes JTAG hardware to sit in the specified stable */
665/* state for the specified duration of real time. If */
666/* no JTAG operations have been performed yet, then only */
667/* a delay is performed. This permits the WAIT USECS */
668/* statement to be used in VECTOR programs without causing */
669/* any JTAG operations. */
670/* */
671/* Returns: JBIC_SUCCESS for success, else appropriate error code */
672/* */
673/****************************************************************************/
674{
675 JBI_RETURN_TYPE status = JBIC_SUCCESS;
676
677 if ((jbi_jtag_state != JBI_ILLEGAL_JTAG_STATE) &&
678 (jbi_jtag_state != wait_state))
679 {
680 status = jbi_goto_jtag_state(wait_state);
681 }
682
683 if (status == JBIC_SUCCESS)
684 {
685 /*
686 * Wait for specified time interval
687 */
688 jbi_delay(microseconds);
689 }
690
691 return (status);
692}
693
694/****************************************************************************/
695/* */
696
697void jbi_jtag_concatenate_data
698(
699 unsigned char *buffer,
700 unsigned char *preamble_data,
701 unsigned int preamble_count,
702 unsigned char *target_data,
703 unsigned long start_index,
704 unsigned int target_count,
705 unsigned char *postamble_data,
706 unsigned int postamble_count
707)
708
709/* */
710/* Description: Copies preamble data, target data, and postamble data */
711/* into one buffer for IR or DR scans. */
712/* */
713/* Returns: nothing */
714/* */
715/****************************************************************************/
716{
717 unsigned long i;
718 unsigned long j;
719 unsigned long k;
720
721 for (i = 0L; i < preamble_count; ++i)
722 {
723 if (preamble_data[i >> 3L] & (1L << (i & 7L)))
724 {
725 buffer[i >> 3L] |= (1L << (i & 7L));
726 }
727 else
728 {
729 buffer[i >> 3L] &= ~(unsigned int) (1L << (i & 7L));
730 }
731 }
732
733 j = start_index;
734 k = preamble_count + target_count;
735 for (; i < k; ++i, ++j)
736 {
737 if (target_data[j >> 3L] & (1L << (j & 7L)))
738 {
739 buffer[i >> 3L] |= (1L << (i & 7L));
740 }
741 else
742 {
743 buffer[i >> 3L] &= ~(unsigned int) (1L << (i & 7L));
744 }
745 }
746
747 j = 0L;
748 k = preamble_count + target_count + postamble_count;
749 for (; i < k; ++i, ++j)
750 {
751 if (postamble_data[j >> 3L] & (1L << (j & 7L)))
752 {
753 buffer[i >> 3L] |= (1L << (i & 7L));
754 }
755 else
756 {
757 buffer[i >> 3L] &= ~(unsigned int) (1L << (i & 7L));
758 }
759 }
760}
761
762int jbi_jtag_drscan
763(
764 int start_state,
765 int count,
766 unsigned char *tdi,
767 unsigned char *tdo
768)
769{
770 int i = 0;
771 int tdo_bit = 0;
772 int status = 1;
773
774 /*
775 * First go to DRSHIFT state
776 */
777 switch (start_state)
778 {
779 case 0: /* IDLE */
780 jbi_jtag_io(1, 0, 0, 0); /* DRSELECT */
781 jbi_jtag_io(0, 0, 0, 0); /* DRCAPTURE */
782 jbi_jtag_io(0, 0, 0, 0); /* DRSHIFT */
783 break;
784
785 case 1: /* DRPAUSE */
786 jbi_jtag_io(1, 0, 0, 0); /* DREXIT2 */
787 jbi_jtag_io(1, 0, 0, 0); /* DRUPDATE */
788 jbi_jtag_io(1, 0, 0, 0); /* DRSELECT */
789 jbi_jtag_io(0, 0, 0, 0); /* DRCAPTURE */
790 jbi_jtag_io(0, 0, 0, 0); /* DRSHIFT */
791 break;
792
793 case 2: /* IRPAUSE */
794 jbi_jtag_io(1, 0, 0, 0); /* IREXIT2 */
795 jbi_jtag_io(1, 0, 0, 0); /* IRUPDATE */
796 jbi_jtag_io(1, 0, 0, 0); /* DRSELECT */
797 jbi_jtag_io(0, 0, 0, 0); /* DRCAPTURE */
798 jbi_jtag_io(0, 0, 0, 0); /* DRSHIFT */
799 break;
800
801 default:
802 status = 0;
803 }
804
805 if (status)
806 {
807 /* loop in the SHIFT-DR state */
808 for (i = 0; i < count; i++)
809 {
810 tdo_bit = jbi_jtag_io(
811 (i == count - 1),
812 tdi[i >> 3] & (1 << (i & 7)),
813 (tdo != NULL), 1);
814
815 if (tdo != NULL)
816 {
817 if (tdo_bit)
818 {
819 tdo[i >> 3] |= (1 << (i & 7));
820 }
821 else
822 {
823 tdo[i >> 3] &= ~(unsigned int) (1 << (i & 7));
824 }
825 }
826 }
827
828 jbi_jtag_io(0, 0, 0, 0); /* DRPAUSE */
829 }
830
831 return (status);
832}
833
834int jbi_jtag_irscan
835(
836 int start_state,
837 int count,
838 unsigned char *tdi,
839 unsigned char *tdo
840)
841{
842 int i = 0;
843 int tdo_bit = 0;
844 int status = 1;
845
846 /*
847 * First go to IRSHIFT state
848 */
849 switch (start_state)
850 {
851 case 0: /* IDLE */
852 jbi_jtag_io(1, 0, 0, 0); /* DRSELECT */
853 jbi_jtag_io(1, 0, 0, 0); /* IRSELECT */
854 jbi_jtag_io(0, 0, 0, 0); /* IRCAPTURE */
855 jbi_jtag_io(0, 0, 0, 0); /* IRSHIFT */
856 break;
857
858 case 1: /* DRPAUSE */
859 jbi_jtag_io(1, 0, 0, 0); /* DREXIT2 */
860 jbi_jtag_io(1, 0, 0, 0); /* DRUPDATE */
861 jbi_jtag_io(1, 0, 0, 0); /* DRSELECT */
862 jbi_jtag_io(1, 0, 0, 0); /* IRSELECT */
863 jbi_jtag_io(0, 0, 0, 0); /* IRCAPTURE */
864 jbi_jtag_io(0, 0, 0, 0); /* IRSHIFT */
865 break;
866
867 case 2: /* IRPAUSE */
868 jbi_jtag_io(1, 0, 0, 0); /* IREXIT2 */
869 jbi_jtag_io(1, 0, 0, 0); /* IRUPDATE */
870 jbi_jtag_io(1, 0, 0, 0); /* DRSELECT */
871 jbi_jtag_io(1, 0, 0, 0); /* IRSELECT */
872 jbi_jtag_io(0, 0, 0, 0); /* IRCAPTURE */
873 jbi_jtag_io(0, 0, 0, 0); /* IRSHIFT */
874 break;
875
876 default:
877 status = 0;
878 }
879
880 if (status)
881 {
882 /* loop in the SHIFT-IR state */
883 for (i = 0; i < count; i++)
884 {
885 tdo_bit = jbi_jtag_io(
886 (i == count - 1),
887 tdi[i >> 3] & (1 << (i & 7)),
888 (tdo != NULL), 0);
889
890 if (tdo != NULL)
891 {
892 if (tdo_bit)
893 {
894 tdo[i >> 3] |= (1 << (i & 7));
895 }
896 else
897 {
898 tdo[i >> 3] &= ~(unsigned int) (1 << (i & 7));
899 }
900 }
901 }
902
903 jbi_jtag_io(0, 0, 0, 0); /* IRPAUSE */
904 }
905
906 return (status);
907}
908
909/****************************************************************************/
910/* */
911
912void jbi_jtag_extract_target_data
913(
914 unsigned char *buffer,
915 unsigned char *target_data,
916 unsigned int start_index,
917 unsigned int preamble_count,
918 unsigned int target_count
919)
920
921/* */
922/* Description: Copies target data from scan buffer, filtering out */
923/* preamble and postamble data. */
924/* */
925/* Returns: nothing */
926/* */
927/****************************************************************************/
928{
929 unsigned int i;
930 unsigned int j;
931 unsigned int k;
932
933 j = preamble_count;
934 k = start_index + target_count;
935 for (i = start_index; i < k; ++i, ++j)
936 {
937 if (buffer[j >> 3] & (1 << (j & 7)))
938 {
939 target_data[i >> 3] |= (1 << (i & 7));
940 }
941 else
942 {
943 target_data[i >> 3] &= ~(unsigned int) (1 << (i & 7));
944 }
945 }
946}
947
948/****************************************************************************/
949/* */
950
951JBI_RETURN_TYPE jbi_do_irscan
952(
953 unsigned int count,
954 unsigned char *tdi_data,
955 unsigned int start_index
956)
957
958/* */
959/* Description: Shifts data into instruction register */
960/* */
961/* Returns: JBIC_SUCCESS for success, else appropriate error code */
962/* */
963/****************************************************************************/
964{
965 int start_code = 0;
966 unsigned int alloc_chars = 0;
967 unsigned int shift_count = jbi_ir_preamble + count + jbi_ir_postamble;
968 JBI_RETURN_TYPE status = JBIC_SUCCESS;
969 JBIE_JTAG_STATE start_state = JBI_ILLEGAL_JTAG_STATE;
970
971 switch (jbi_jtag_state)
972 {
973 case JBI_ILLEGAL_JTAG_STATE:
974 case RESET:
975 case IDLE:
976 start_code = 0;
977 start_state = IDLE;
978 break;
979
980 case DRSELECT:
981 case DRCAPTURE:
982 case DRSHIFT:
983 case DREXIT1:
984 case DRPAUSE:
985 case DREXIT2:
986 case DRUPDATE:
987 start_code = 1;
988 start_state = DRPAUSE;
989 break;
990
991 case IRSELECT:
992 case IRCAPTURE:
993 case IRSHIFT:
994 case IREXIT1:
995 case IRPAUSE:
996 case IREXIT2:
997 case IRUPDATE:
998 start_code = 2;
999 start_state = IRPAUSE;
1000 break;
1001
1002 default:
1003 status = JBIC_INTERNAL_ERROR;
1004 break;
1005 }
1006
1007 if (status == JBIC_SUCCESS)
1008 {
1009 if (jbi_jtag_state != start_state)
1010 {
1011 status = jbi_goto_jtag_state(start_state);
1012 }
1013 }
1014
1015 if (status == JBIC_SUCCESS)
1016 {
1017 if (jbi_workspace != NULL)
1018 {
1019 if (shift_count > JBIC_MAX_JTAG_IR_LENGTH)
1020 {
1021 status = JBIC_OUT_OF_MEMORY;
1022 }
1023 }
1024 else if (shift_count > jbi_ir_length)
1025 {
1026 alloc_chars = (shift_count + 7) >> 3;
1027 jbi_free(jbi_ir_buffer);
1028 jbi_ir_buffer = (unsigned char *) jbi_malloc(alloc_chars);
1029
1030 if (jbi_ir_buffer == NULL)
1031 {
1032 status = JBIC_OUT_OF_MEMORY;
1033 }
1034 else
1035 {
1036 jbi_ir_length = alloc_chars * 8;
1037 }
1038 }
1039 }
1040
1041 if (status == JBIC_SUCCESS)
1042 {
1043 /*
1044 * Copy preamble data, IR data, and postamble data into a buffer
1045 */
1046 jbi_jtag_concatenate_data
1047 (
1048 jbi_ir_buffer,
1049 jbi_ir_preamble_data,
1050 jbi_ir_preamble,
1051 tdi_data,
1052 start_index,
1053 count,
1054 jbi_ir_postamble_data,
1055 jbi_ir_postamble
1056 );
1057
1058 /*
1059 * Do the IRSCAN
1060 */
1061 jbi_jtag_irscan
1062 (
1063 start_code,
1064 shift_count,
1065 jbi_ir_buffer,
1066 NULL
1067 );
1068
1069 /* jbi_jtag_irscan() always ends in IRPAUSE state */
1070 jbi_jtag_state = IRPAUSE;
1071 }
1072
1073 if (status == JBIC_SUCCESS)
1074 {
1075 if (jbi_irstop_state != IRPAUSE)
1076 {
1077 status = jbi_goto_jtag_state(jbi_irstop_state);
1078 }
1079 }
1080
1081 return (status);
1082}
1083
1084/****************************************************************************/
1085/* */
1086
1087JBI_RETURN_TYPE jbi_swap_ir
1088(
1089 unsigned int count,
1090 unsigned char *in_data,
1091 unsigned int in_index,
1092 unsigned char *out_data,
1093 unsigned int out_index
1094)
1095
1096/* */
1097/* Description: Shifts data into instruction register, capturing output */
1098/* data */
1099/* */
1100/* Returns: JBIC_SUCCESS for success, else appropriate error code */
1101/* */
1102/****************************************************************************/
1103{
1104 int start_code = 0;
1105 unsigned int alloc_chars = 0;
1106 unsigned int shift_count = jbi_ir_preamble + count + jbi_ir_postamble;
1107 JBI_RETURN_TYPE status = JBIC_SUCCESS;
1108 JBIE_JTAG_STATE start_state = JBI_ILLEGAL_JTAG_STATE;
1109
1110 switch (jbi_jtag_state)
1111 {
1112 case JBI_ILLEGAL_JTAG_STATE:
1113 case RESET:
1114 case IDLE:
1115 start_code = 0;
1116 start_state = IDLE;
1117 break;
1118
1119 case DRSELECT:
1120 case DRCAPTURE:
1121 case DRSHIFT:
1122 case DREXIT1:
1123 case DRPAUSE:
1124 case DREXIT2:
1125 case DRUPDATE:
1126 start_code = 1;
1127 start_state = DRPAUSE;
1128 break;
1129
1130 case IRSELECT:
1131 case IRCAPTURE:
1132 case IRSHIFT:
1133 case IREXIT1:
1134 case IRPAUSE:
1135 case IREXIT2:
1136 case IRUPDATE:
1137 start_code = 2;
1138 start_state = IRPAUSE;
1139 break;
1140
1141 default:
1142 status = JBIC_INTERNAL_ERROR;
1143 break;
1144 }
1145
1146 if (status == JBIC_SUCCESS)
1147 {
1148 if (jbi_jtag_state != start_state)
1149 {
1150 status = jbi_goto_jtag_state(start_state);
1151 }
1152 }
1153
1154 if (status == JBIC_SUCCESS)
1155 {
1156 if (jbi_workspace != NULL)
1157 {
1158 if (shift_count > JBIC_MAX_JTAG_IR_LENGTH)
1159 {
1160 status = JBIC_OUT_OF_MEMORY;
1161 }
1162 }
1163 else if (shift_count > jbi_ir_length)
1164 {
1165 alloc_chars = (shift_count + 7) >> 3;
1166 jbi_free(jbi_ir_buffer);
1167 jbi_ir_buffer = (unsigned char *) jbi_malloc(alloc_chars);
1168
1169 if (jbi_ir_buffer == NULL)
1170 {
1171 status = JBIC_OUT_OF_MEMORY;
1172 }
1173 else
1174 {
1175 jbi_ir_length = alloc_chars * 8;
1176 }
1177 }
1178 }
1179
1180 if (status == JBIC_SUCCESS)
1181 {
1182 /*
1183 * Copy preamble data, IR data, and postamble data into a buffer
1184 */
1185 jbi_jtag_concatenate_data
1186 (
1187 jbi_ir_buffer,
1188 jbi_ir_preamble_data,
1189 jbi_ir_preamble,
1190 in_data,
1191 in_index,
1192 count,
1193 jbi_ir_postamble_data,
1194 jbi_ir_postamble
1195 );
1196
1197 /*
1198 * Do the IRSCAN
1199 */
1200 jbi_jtag_irscan
1201 (
1202 start_code,
1203 shift_count,
1204 jbi_ir_buffer,
1205 jbi_ir_buffer
1206 );
1207
1208 /* jbi_jtag_irscan() always ends in IRPAUSE state */
1209 jbi_jtag_state = IRPAUSE;
1210 }
1211
1212 if (status == JBIC_SUCCESS)
1213 {
1214 if (jbi_irstop_state != IRPAUSE)
1215 {
1216 status = jbi_goto_jtag_state(jbi_irstop_state);
1217 }
1218 }
1219
1220 if (status == JBIC_SUCCESS)
1221 {
1222 /*
1223 * Now extract the returned data from the buffer
1224 */
1225 jbi_jtag_extract_target_data
1226 (
1227 jbi_ir_buffer,
1228 out_data,
1229 out_index,
1230 jbi_ir_preamble,
1231 count
1232 );
1233 }
1234
1235 return (status);
1236}
1237
1238/****************************************************************************/
1239/* */
1240
1241JBI_RETURN_TYPE jbi_do_drscan
1242(
1243 unsigned int count,
1244 unsigned char *tdi_data,
1245 unsigned long start_index
1246)
1247
1248/* */
1249/* Description: Shifts data into data register (ignoring output data) */
1250/* */
1251/* Returns: JBIC_SUCCESS for success, else appropriate error code */
1252/* */
1253/****************************************************************************/
1254{
1255 int start_code = 0;
1256 unsigned int alloc_chars = 0;
1257 unsigned int shift_count = jbi_dr_preamble + count + jbi_dr_postamble;
1258 JBI_RETURN_TYPE status = JBIC_SUCCESS;
1259 JBIE_JTAG_STATE start_state = JBI_ILLEGAL_JTAG_STATE;
1260
1261 switch (jbi_jtag_state)
1262 {
1263 case JBI_ILLEGAL_JTAG_STATE:
1264 case RESET:
1265 case IDLE:
1266 start_code = 0;
1267 start_state = IDLE;
1268 break;
1269
1270 case DRSELECT:
1271 case DRCAPTURE:
1272 case DRSHIFT:
1273 case DREXIT1:
1274 case DRPAUSE:
1275 case DREXIT2:
1276 case DRUPDATE:
1277 start_code = 1;
1278 start_state = DRPAUSE;
1279 break;
1280
1281 case IRSELECT:
1282 case IRCAPTURE:
1283 case IRSHIFT:
1284 case IREXIT1:
1285 case IRPAUSE:
1286 case IREXIT2:
1287 case IRUPDATE:
1288 start_code = 2;
1289 start_state = IRPAUSE;
1290 break;
1291
1292 default:
1293 status = JBIC_INTERNAL_ERROR;
1294 break;
1295 }
1296
1297 if (status == JBIC_SUCCESS)
1298 {
1299 if (jbi_jtag_state != start_state)
1300 {
1301 status = jbi_goto_jtag_state(start_state);
1302 }
1303 }
1304
1305 if (status == JBIC_SUCCESS)
1306 {
1307 if (jbi_workspace != NULL)
1308 {
1309 if (shift_count > JBIC_MAX_JTAG_DR_LENGTH)
1310 {
1311 status = JBIC_OUT_OF_MEMORY;
1312 }
1313 }
1314 else if (shift_count > jbi_dr_length)
1315 {
1316 alloc_chars = (shift_count + 7) >> 3;
1317 jbi_free(jbi_dr_buffer);
1318 jbi_dr_buffer = (unsigned char *) jbi_malloc(alloc_chars);
1319
1320 if (jbi_dr_buffer == NULL)
1321 {
1322 status = JBIC_OUT_OF_MEMORY;
1323 }
1324 else
1325 {
1326 jbi_dr_length = alloc_chars * 8;
1327 }
1328 }
1329 }
1330
1331 if (status == JBIC_SUCCESS)
1332 {
1333 /*
1334 * Copy preamble data, DR data, and postamble data into a buffer
1335 */
1336 jbi_jtag_concatenate_data
1337 (
1338 jbi_dr_buffer,
1339 jbi_dr_preamble_data,
1340 jbi_dr_preamble,
1341 tdi_data,
1342 start_index,
1343 count,
1344 jbi_dr_postamble_data,
1345 jbi_dr_postamble
1346 );
1347
1348 /*
1349 * Do the DRSCAN
1350 */
1351 jbi_jtag_drscan
1352 (
1353 start_code,
1354 shift_count,
1355 jbi_dr_buffer,
1356 NULL
1357 );
1358
1359 /* jbi_jtag_drscan() always ends in DRPAUSE state */
1360 jbi_jtag_state = DRPAUSE;
1361 }
1362
1363 if (status == JBIC_SUCCESS)
1364 {
1365 if (jbi_drstop_state != DRPAUSE)
1366 {
1367 status = jbi_goto_jtag_state(jbi_drstop_state);
1368 }
1369 }
1370
1371 return (status);
1372}
1373
1374/****************************************************************************/
1375/* */
1376
1377JBI_RETURN_TYPE jbi_swap_dr
1378(
1379 unsigned int count,
1380 unsigned char *in_data,
1381 unsigned long in_index,
1382 unsigned char *out_data,
1383 unsigned int out_index
1384)
1385
1386/* */
1387/* Description: Shifts data into data register, capturing output data */
1388/* */
1389/* Returns: JBIC_SUCCESS for success, else appropriate error code */
1390/* */
1391/****************************************************************************/
1392{
1393 int start_code = 0;
1394 unsigned int alloc_chars = 0;
1395 unsigned int shift_count = jbi_dr_preamble + count + jbi_dr_postamble;
1396 JBI_RETURN_TYPE status = JBIC_SUCCESS;
1397 JBIE_JTAG_STATE start_state = JBI_ILLEGAL_JTAG_STATE;
1398
1399 switch (jbi_jtag_state)
1400 {
1401 case JBI_ILLEGAL_JTAG_STATE:
1402 case RESET:
1403 case IDLE:
1404 start_code = 0;
1405 start_state = IDLE;
1406 break;
1407
1408 case DRSELECT:
1409 case DRCAPTURE:
1410 case DRSHIFT:
1411 case DREXIT1:
1412 case DRPAUSE:
1413 case DREXIT2:
1414 case DRUPDATE:
1415 start_code = 1;
1416 start_state = DRPAUSE;
1417 break;
1418
1419 case IRSELECT:
1420 case IRCAPTURE:
1421 case IRSHIFT:
1422 case IREXIT1:
1423 case IRPAUSE:
1424 case IREXIT2:
1425 case IRUPDATE:
1426 start_code = 2;
1427 start_state = IRPAUSE;
1428 break;
1429
1430 default:
1431 status = JBIC_INTERNAL_ERROR;
1432 break;
1433 }
1434
1435 if (status == JBIC_SUCCESS)
1436 {
1437 if (jbi_jtag_state != start_state)
1438 {
1439 status = jbi_goto_jtag_state(start_state);
1440 }
1441 }
1442
1443 if (status == JBIC_SUCCESS)
1444 {
1445 if (jbi_workspace != NULL)
1446 {
1447 if (shift_count > JBIC_MAX_JTAG_DR_LENGTH)
1448 {
1449 status = JBIC_OUT_OF_MEMORY;
1450 }
1451 }
1452 else if (shift_count > jbi_dr_length)
1453 {
1454 alloc_chars = (shift_count + 7) >> 3;
1455 jbi_free(jbi_dr_buffer);
1456 jbi_dr_buffer = (unsigned char *) jbi_malloc(alloc_chars);
1457
1458 if (jbi_dr_buffer == NULL)
1459 {
1460 status = JBIC_OUT_OF_MEMORY;
1461 }
1462 else
1463 {
1464 jbi_dr_length = alloc_chars * 8;
1465 }
1466 }
1467 }
1468
1469 if (status == JBIC_SUCCESS)
1470 {
1471 /*
1472 * Copy preamble data, DR data, and postamble data into a buffer
1473 */
1474 jbi_jtag_concatenate_data
1475 (
1476 jbi_dr_buffer,
1477 jbi_dr_preamble_data,
1478 jbi_dr_preamble,
1479 in_data,
1480 in_index,
1481 count,
1482 jbi_dr_postamble_data,
1483 jbi_dr_postamble
1484 );
1485
1486 /*
1487 * Do the DRSCAN
1488 */
1489 jbi_jtag_drscan
1490 (
1491 start_code,
1492 shift_count,
1493 jbi_dr_buffer,
1494 jbi_dr_buffer
1495 );
1496
1497 /* jbi_jtag_drscan() always ends in DRPAUSE state */
1498 jbi_jtag_state = DRPAUSE;
1499 }
1500
1501 if (status == JBIC_SUCCESS)
1502 {
1503 if (jbi_drstop_state != DRPAUSE)
1504 {
1505 status = jbi_goto_jtag_state(jbi_drstop_state);
1506 }
1507 }
1508
1509 if (status == JBIC_SUCCESS)
1510 {
1511 /*
1512 * Now extract the returned data from the buffer
1513 */
1514 jbi_jtag_extract_target_data
1515 (
1516 jbi_dr_buffer,
1517 out_data,
1518 out_index,
1519 jbi_dr_preamble,
1520 count
1521 );
1522 }
1523
1524 return (status);
1525}
1526
1527/****************************************************************************/
1528/* */
1529
1530void jbi_free_jtag_padding_buffers(int reset_jtag)
1531
1532/* */
1533/* Description: Frees memory allocated for JTAG IR and DR buffers */
1534/* */
1535/* Returns: nothing */
1536/* */
1537/****************************************************************************/
1538{
1539 /*
1540 * If the JTAG interface was used, reset it to TLR
1541 */
1542 if (reset_jtag && (jbi_jtag_state != JBI_ILLEGAL_JTAG_STATE))
1543 {
1544 jbi_jtag_reset_idle();
1545 }
1546
1547 if (jbi_workspace == NULL)
1548 {
1549 if (jbi_dr_preamble_data != NULL)
1550 {
1551 jbi_free(jbi_dr_preamble_data);
1552 jbi_dr_preamble_data = NULL;
1553 }
1554
1555 if (jbi_dr_postamble_data != NULL)
1556 {
1557 jbi_free(jbi_dr_postamble_data);
1558 jbi_dr_postamble_data = NULL;
1559 }
1560
1561 if (jbi_dr_buffer != NULL)
1562 {
1563 jbi_free(jbi_dr_buffer);
1564 jbi_dr_buffer = NULL;
1565 }
1566
1567 if (jbi_ir_preamble_data != NULL)
1568 {
1569 jbi_free(jbi_ir_preamble_data);
1570 jbi_ir_preamble_data = NULL;
1571 }
1572
1573 if (jbi_ir_postamble_data != NULL)
1574 {
1575 jbi_free(jbi_ir_postamble_data);
1576 jbi_ir_postamble_data = NULL;
1577 }
1578
1579 if (jbi_ir_buffer != NULL)
1580 {
1581 jbi_free(jbi_ir_buffer);
1582 jbi_ir_buffer = NULL;
1583 }
1584 }
1585}
Note: See TracBrowser for help on using the repository browser.