2 \author Travis Goodspeed <travis at radiantmachines.com>
13 //! Handles a monitor command.
14 void jtag_handle_fn(uint8_t const app,
19 unsigned char jtagid=0;
21 // define the jtag app's app_t
22 app_t const jtag_app = {
34 "\tThe JTAG app handles basic JTAG operations such as\n"
35 "\tresetting the TAP, resetting the target, detecting\n"
36 "\tthe instruction register width, shifting bits into\n"
37 "\tboth the instruction and data registers.\n"
41 //! Remembers what the current JTAG state is
42 enum eTAPState jtag_state = UNKNOWN;
44 //! Returns true if we're in any of the data register states
47 return (int)(jtag_state & (SELECT_DR_SCAN | CAPTURE_DR |
48 SHIFT_DR | EXIT1_DR | PAUSE_DR |
49 EXIT2_DR | UPDATE_DR));
52 //! Returns true if we're in any of the instruction register states
55 return (int)(jtag_state & (SELECT_IR_SCAN | CAPTURE_IR |
56 SHIFT_IR | EXIT1_IR | PAUSE_IR |
57 EXIT2_IR | UPDATE_IR));
60 //! Returns true if we're in run-test-idle state
61 int in_run_test_idle()
63 return (int)(jtag_state & RUN_TEST_IDLE);
67 int in_state(enum eTAPState state)
69 return (int)(jtag_state & state);
73 //! Reset the target device
74 void jtag_reset_target()
82 //! Clock the JTAG clock line
91 //! Goes through test-logic-reset and ends in run-test-idle
100 jtag_tcktock(); // now in Reset state
102 jtag_tcktock(); // now in Run-Test/Idle state
103 jtag_state = RUN_TEST_IDLE;
106 //! Set up the pins for JTAG mode.
114 jtag_state = UNKNOWN;
117 //! Stop JTAG, release pins
124 //! Get into Shift-IR or Shift-DR state
125 void jtag_shift_register()
127 // assumes we're in any state that can transition to Shift-IR or Shift-DR
128 if (!in_state(CAPTURE_DR | CAPTURE_IR | SHIFT_DR |
129 SHIFT_IR | EXIT2_DR | EXIT2_IR ))
131 debugstr("Invalid JTAG state");
140 jtag_state = SHIFT_DR;
142 jtag_state = SHIFT_IR;
145 //! Get into Capture-IR state
146 void jtag_capture_ir()
148 // assumes you're in Run-Test/Idle, Update-DR or Update-IR
149 if (!in_state(RUN_TEST_IDLE | UPDATE_DR | UPDATE_IR))
151 debugstr("Invalid JTAG state");
157 jtag_tcktock(); // Select-DR-Scan
158 jtag_tcktock(); // Select-IR-Scan
160 jtag_tcktock(); // Capture-IR
162 jtag_state = CAPTURE_IR;
165 //! Get into Capture-DR state
166 void jtag_capture_dr()
168 // assumes you're in Run-Test/Idle, Update-DR or Update-IR
169 if (!in_state(RUN_TEST_IDLE | UPDATE_DR | UPDATE_IR))
171 debugstr("Invalid JTAG state");
177 jtag_tcktock(); // Select-DR-Scan
179 jtag_tcktock(); // Capture-IR
181 jtag_state = CAPTURE_DR;
184 //! Gets back to run-test-idle without going through the test-logic-reset
185 void jtag_run_test_idle()
189 if (in_state(SELECT_DR_SCAN | SELECT_IR_SCAN))
193 jtag_state <<= 1; //CAPTURE_DR or CAPTURE_IR
196 if (in_state(CAPTURE_DR | CAPTURE_IR))
200 jtag_state <<= 2; //EXIT1_DR or EXIT1_IR
203 if (in_state(SHIFT_DR | SHIFT_IR))
207 jtag_state <<= 1; //EXIT1_DR or EXIT1_IR
210 if (in_state(EXIT1_DR | EXIT1_IR))
214 jtag_state <<=3; //UPDATE_DR or UPDATE_IR
217 if (in_state(PAUSE_DR | PAUSE_IR))
221 jtag_state <<= 1; // EXIT2_DR or EXIT2_IR
224 if (in_state(EXIT2_DR | EXIT2_IR))
228 jtag_state <<= 1; // UPDATE_DR or UPDATE_IR
231 if (in_state(UPDATE_DR | UPDATE_IR | TEST_LOGIC_RESET))
235 jtag_state = RUN_TEST_IDLE;
240 // NOTE: important: THIS MODULE REVOLVES AROUND RETURNING TO RUNTEST/IDLE, OR
241 // THE FUNCTIONAL EQUIVALENT
242 //! Shift N bits over TDI/TDO. May choose LSB or MSB, and select whether to
243 // terminate (TMS-high on last bit) and whether to return to RUNTEST/IDLE
244 // flags should be 0 for most uses.
245 // for the extreme case, flags should be (NOEND|NORETDLE|LSB)
246 // other edge cases can involve a combination of those three flags
248 // the max bit-size that can be be shifted is 32-bits.
249 // for longer shifts, use the NOEND flag (which infers NORETIDLE so the
250 // additional flag is unnecessary)
252 // NORETIDLE is used for special cases where (as with arm) the debug
253 // subsystem does not want to return to the RUN-TEST/IDLE state between
255 uint32_t jtag_trans_n(uint32_t word,
257 enum eTransFlags flags)
260 uint32_t high = (1L << (bitcount - 1));
261 uint32_t mask = high - 1;
263 if (!in_state(SHIFT_IR | SHIFT_DR))
265 debugstr("jtag_trans_n from invalid TAP state");
273 for (bit = bitcount; bit > 0; bit--)
275 /* write MOSI on trailing edge of previous clock */
286 if ((bit == 1) && !(flags & NOEND))
287 SETTMS; //TMS high on last bit to exit.
291 if ((bit == 1) && !(flags & NOEND))
292 jtag_state <<= 1; // Exit1-DR or Exit1-IR
294 /* read MISO on trailing edge */
303 for (bit = bitcount; bit > 0; bit--)
305 /* write MOSI on trailing edge of previous clock */
314 word = (word & mask) << 1;
316 if ((bit==1) && !(flags & NOEND))
317 SETTMS; //TMS high on last bit to exit.
321 if ((bit == 1) && !(flags & NOEND))
322 jtag_state <<= 1; // Exit1-DR or Exit1-IR
324 /* read MISO on trailing edge */
331 if (!(flags & NOEND))
336 jtag_state <<= 3; // Update-DR or Update-IR
339 if (!(flags & NORETIDLE))
344 jtag_state = RUN_TEST_IDLE;
351 //! Detects the width of the IR register
352 uint16_t jtag_detect_ir_width()
357 if (!in_run_test_idle())
359 debugstr("Not in run-test-idle state");
363 // get to shift-ir state
365 jtag_shift_register();
367 // first we shift in 1024 zeros
369 for(i = 0; i < 1024; i++)
374 // now we'll clock in ones until we see one
376 for(i = 0; i < 1024; i++)
384 // now get back to run-test-idle
385 jtag_run_test_idle();
390 //! Detects how many TAPs are in the JTAG chain
391 uint16_t jtag_detect_chain_length()
397 if (!in_run_test_idle())
399 debugstr("detect chain length must start from run-test-idle");
403 // The standard JTAG instruction for "bypass" mode is to set all 1's in the
404 // instruction register. When in "bypass" mode, the DR acts like a 1-bit
405 // shift regiser. So we can use that to detect how many TAPs are in the
408 // first get into shift IR mode
410 jtag_shift_register();
412 // then we flood the IR chain with all 1's to put all device's DRs
416 for (i = 0; i < 1024; i++)
419 SETTMS; // exit on last bit
422 jtag_state = EXIT1_IR;
427 jtag_state = UPDATE_IR;
429 // go to Shift-DR state
431 jtag_shift_register();
433 // flush the DR's with zeros
436 for (i = 0; i < 1024; i++)
441 // send 1's into the DRs until we see one come out the other side
443 for (i = 0; i < 1024; i++)
452 // now get back to run-test-idle
453 jtag_run_test_idle();
458 //! Gets device ID for specified chip in the chain
459 uint32_t jtag_get_device_id(int chip)
462 uint16_t chain_length;
468 // figure out how many devices are in the chain
469 chain_length = jtag_detect_chain_length();
471 if (chip >= chain_length)
473 debugstr("invalid part index");
477 // reset everything again because going through test-logic-reset forces
478 // all IR's to have their manufacturer specific instruction for IDCODE
479 // and loads the DR's with the chip's ID code.
482 // get into shift DR state
484 jtag_shift_register();
486 // read out the 32-bit ID codes for each device
489 for (i = 0; i < (chip + 1); i++)
491 id = jtag_trans_n(0xFFFFFFFF, 32, LSB | NOEND | NORETIDLE);
494 jtag_run_test_idle();
499 //! Shift 8 bits in/out of selected register
500 uint8_t jtag_trans_8(uint8_t in)
502 uint32_t out = jtag_trans_n((uint32_t)in, 8, MSB);
503 return (uint8_t)(0x000000FF & out);
506 //! Shift 16 bits in/out of selected register
507 uint16_t jtag_trans_16(uint16_t in)
509 uint32_t out = jtag_trans_n((uint32_t)in, 16, MSB);
510 return (uint16_t)(0x0000FFFF & out);
513 //! Shift 8 bits of the IR.
514 uint8_t jtag_ir_shift_8(uint8_t in)
516 if (!in_run_test_idle())
518 debugstr("Not in run-test-idle state");
522 // get intot the right state
524 jtag_shift_register();
527 return jtag_trans_8(in);
530 //! Shift 16 bits of the DR.
531 uint16_t jtag_dr_shift_16(uint16_t in)
534 if (!in_run_test_idle())
536 debugstr("Not in run-test-idle state");
540 // get intot the right state
542 jtag_shift_register();
544 // shift DR, then idle
545 return jtag_trans_16(in);
548 //! Handles a monitor command.
549 void jtag_handle_fn(uint8_t const app,
555 // START handled by specific JTAG
567 cmddata[0] = jtag_ir_shift_8(cmddata[0]);
572 cmddataword[0] = htons(jtag_dr_shift_16(ntohs(cmddataword[0])));
581 case JTAG_RESET_TARGET:
587 case JTAG_DETECT_IR_WIDTH:
589 cmddataword[0] = htons(jtag_detect_ir_width());
593 case JTAG_DETECT_CHAIN_LENGTH:
595 cmddataword[0] = htons(jtag_detect_chain_length());
599 case JTAG_GET_DEVICE_ID:
601 cmddatalong[0] = htonl(jtag_get_device_id(ntohs(cmddataword[0])));