source: sandbox/JamPlayerUSB/jbijtag.c@ 186

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

remove unused variables and debug printouts

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