You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

897 lines
31 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2005 by Dominic Rath *
  3. * Dominic.Rath@gmx.de *
  4. * *
  5. * Copyright (C) 2007,2008 Øyvind Harboe *
  6. * oyvind.harboe@zylin.com *
  7. * *
  8. * This program is free software; you can redistribute it and/or modify *
  9. * it under the terms of the GNU General Public License as published by *
  10. * the Free Software Foundation; either version 2 of the License, or *
  11. * (at your option) any later version. *
  12. * *
  13. * This program is distributed in the hope that it will be useful, *
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  16. * GNU General Public License for more details. *
  17. * *
  18. * You should have received a copy of the GNU General Public License *
  19. * along with this program; if not, write to the *
  20. * Free Software Foundation, Inc., *
  21. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  22. ***************************************************************************/
  23. #ifndef JTAG_H
  24. #define JTAG_H
  25. #include "binarybuffer.h"
  26. #include "log.h"
  27. #ifdef _DEBUG_JTAG_IO_
  28. #define DEBUG_JTAG_IO(expr ...) LOG_DEBUG(expr)
  29. #else
  30. #define DEBUG_JTAG_IO(expr ...)
  31. #endif
  32. #ifndef DEBUG_JTAG_IOZ
  33. #define DEBUG_JTAG_IOZ 64
  34. #endif
  35. /*-----<Macros>--------------------------------------------------*/
  36. /** When given an array, compute its DIMension, i.e. number of elements in the array */
  37. #define DIM(x) (sizeof(x)/sizeof((x)[0]))
  38. /** Calculate the number of bytes required to hold @a n TAP scan bits */
  39. #define TAP_SCAN_BYTES(n) CEIL(n, 8)
  40. /*-----</Macros>-------------------------------------------------*/
  41. /*
  42. * Tap states from ARM7TDMI-S Technical reference manual.
  43. * Also, validated against several other ARM core technical manuals.
  44. *
  45. * N.B. tap_get_tms_path() was changed to reflect this corrected
  46. * numbering and ordering of the TAP states.
  47. *
  48. * DANGER!!!! some interfaces care about the actual numbers used
  49. * as they are handed off directly to hardware implementations.
  50. */
  51. typedef enum tap_state
  52. {
  53. #if BUILD_ECOSBOARD
  54. /* These are the old numbers. Leave as-is for now... */
  55. TAP_RESET = 0, TAP_IDLE = 8,
  56. TAP_DRSELECT = 1, TAP_DRCAPTURE = 2, TAP_DRSHIFT = 3, TAP_DREXIT1 = 4,
  57. TAP_DRPAUSE = 5, TAP_DREXIT2 = 6, TAP_DRUPDATE = 7,
  58. TAP_IRSELECT = 9, TAP_IRCAPTURE = 10, TAP_IRSHIFT = 11, TAP_IREXIT1 = 12,
  59. TAP_IRPAUSE = 13, TAP_IREXIT2 = 14, TAP_IRUPDATE = 15,
  60. TAP_NUM_STATES = 16, TAP_INVALID = -1,
  61. #else
  62. /* Proper ARM recommended numbers */
  63. TAP_DREXIT2 = 0x0,
  64. TAP_DREXIT1 = 0x1,
  65. TAP_DRSHIFT = 0x2,
  66. TAP_DRPAUSE = 0x3,
  67. TAP_IRSELECT = 0x4,
  68. TAP_DRUPDATE = 0x5,
  69. TAP_DRCAPTURE = 0x6,
  70. TAP_DRSELECT = 0x7,
  71. TAP_IREXIT2 = 0x8,
  72. TAP_IREXIT1 = 0x9,
  73. TAP_IRSHIFT = 0xa,
  74. TAP_IRPAUSE = 0xb,
  75. TAP_IDLE = 0xc,
  76. TAP_IRUPDATE = 0xd,
  77. TAP_IRCAPTURE = 0xe,
  78. TAP_RESET = 0x0f,
  79. TAP_NUM_STATES = 0x10,
  80. TAP_INVALID = -1,
  81. #endif
  82. } tap_state_t;
  83. typedef struct tap_transition_s
  84. {
  85. tap_state_t high;
  86. tap_state_t low;
  87. } tap_transition_t;
  88. //extern tap_transition_t tap_transitions[16]; /* describe the TAP state diagram */
  89. /*-----<Cable Helper API>-------------------------------------------*/
  90. /* The "Cable Helper API" is what the cable drivers can use to help implement
  91. * their "Cable API". So a Cable Helper API is a set of helper functions used by
  92. * cable drivers, and this is different from a Cable API. A "Cable API" is what
  93. * higher level code used to talk to a cable.
  94. */
  95. /** implementation of wrapper function tap_set_state() */
  96. void tap_set_state_impl(tap_state_t new_state);
  97. /**
  98. * Function tap_set_state
  99. * sets the state of a "state follower" which tracks the state of the TAPs connected to the
  100. * cable. The state follower is hopefully always in the same state as the actual
  101. * TAPs in the jtag chain, and will be so if there are no bugs in the tracking logic within that
  102. * cable driver. All the cable drivers call this function to indicate the state they think
  103. * the TAPs attached to their cables are in. Because this function can also log transitions,
  104. * it will be helpful to call this function with every transition that the TAPs being manipulated
  105. * are expected to traverse, not just end points of a multi-step state path.
  106. * @param new_state is the state we think the TAPs are currently in or are about to enter.
  107. */
  108. #if defined(_DEBUG_JTAG_IO_)
  109. #define tap_set_state(new_state) \
  110. do { \
  111. LOG_DEBUG( "tap_set_state(%s)", tap_state_name(new_state) ); \
  112. tap_set_state_impl(new_state); \
  113. } while (0)
  114. #else
  115. static inline void tap_set_state(tap_state_t new_state)
  116. {
  117. tap_set_state_impl(new_state);
  118. }
  119. #endif
  120. /**
  121. * Function tap_get_state
  122. * gets the state of the "state follower" which tracks the state of the TAPs connected to
  123. * the cable.
  124. * @see tap_set_state
  125. * @return tap_state_t - The state the TAPs are in now.
  126. */
  127. tap_state_t tap_get_state(void);
  128. /**
  129. * Function tap_set_end_state
  130. * sets the state of an "end state follower" which tracks the state that any cable driver
  131. * thinks will be the end (resultant) state of the current TAP SIR or SDR operation. At completion
  132. * of that TAP operation this value is copied into the state follower via tap_set_state().
  133. * @param new_end_state is that state the TAPs should enter at completion of a pending TAP operation.
  134. */
  135. void tap_set_end_state(tap_state_t new_end_state);
  136. /**
  137. * Function tap_get_end_state
  138. * @see tap_set_end_state
  139. * @return tap_state_t - The state the TAPs should be in at completion of the current TAP operation.
  140. */
  141. tap_state_t tap_get_end_state(void);
  142. /**
  143. * Function tap_get_tms_path
  144. * returns a 7 bit long "bit sequence" indicating what has to be done with TMS
  145. * during a sequence of seven TAP clock cycles in order to get from
  146. * state \a "from" to state \a "to".
  147. * @param from is the starting state
  148. * @param to is the resultant or final state
  149. * @return int - a 7 bit sequence, with the first bit in the sequence at bit 0.
  150. */
  151. int tap_get_tms_path(tap_state_t from, tap_state_t to);
  152. /**
  153. * Function int tap_get_tms_path_len
  154. * returns the total number of bits that represents a TMS path
  155. * transition as given by the function tap_get_tms_path().
  156. *
  157. * For at least one interface (JLink) it's not OK to simply "pad" TMS sequences
  158. * to fit a whole byte. (I suspect this is a general TAP problem within OOCD.)
  159. * Padding TMS causes all manner of instability that's not easily
  160. * discovered. Using this routine we can apply EXACTLY the state transitions
  161. * required to make something work - no more - no less.
  162. *
  163. * @param from is the starting state
  164. * @param to is the resultant or final state
  165. * @return int - the total number of bits in a transition.
  166. */
  167. int tap_get_tms_path_len(tap_state_t from, tap_state_t to);
  168. /**
  169. * Function tap_move_ndx
  170. * when given a stable state, returns an index from 0-5. The index corresponds to a
  171. * sequence of stable states which are given in this order: <p>
  172. * { TAP_RESET, TAP_IDLE, TAP_DRSHIFT, TAP_DRPAUSE, TAP_IRSHIFT, TAP_IRPAUSE }
  173. * <p>
  174. * This sequence corresponds to look up tables which are used in some of the
  175. * cable drivers.
  176. * @param astate is the stable state to find in the sequence. If a non stable
  177. * state is passed, this may cause the program to output an error message
  178. * and terminate.
  179. * @return int - the array (or sequence) index as described above
  180. */
  181. int tap_move_ndx(tap_state_t astate);
  182. /**
  183. * Function tap_is_state_stable
  184. * returns true if the \a astate is stable.
  185. */
  186. bool tap_is_state_stable(tap_state_t astate);
  187. /**
  188. * Function tap_state_transition
  189. * takes a current TAP state and returns the next state according to the tms value.
  190. * @param current_state is the state of a TAP currently.
  191. * @param tms is either zero or non-zero, just like a real TMS line in a jtag interface.
  192. * @return tap_state_t - the next state a TAP would enter.
  193. */
  194. tap_state_t tap_state_transition(tap_state_t current_state, bool tms);
  195. /**
  196. * Function tap_state_name
  197. * Returns a string suitable for display representing the JTAG tap_state
  198. */
  199. const char* tap_state_name(tap_state_t state);
  200. #ifdef _DEBUG_JTAG_IO_
  201. /**
  202. * @brief Prints verbose TAP state transitions for the given TMS/TDI buffers.
  203. * @param tms_buf must points to a buffer containing the TMS bitstream.
  204. * @param tdi_buf must points to a buffer containing the TDI bitstream.
  205. * @param tap_len must specify the length of the TMS/TDI bitstreams.
  206. * @param start_tap_state must specify the current TAP state.
  207. * @returns the final TAP state; pass as @a start_tap_state in following call.
  208. */
  209. tap_state_t jtag_debug_state_machine(const void *tms_buf, const void *tdi_buf,
  210. unsigned tap_len, tap_state_t start_tap_state);
  211. #else
  212. static inline tap_state_t jtag_debug_state_machine(const void *tms_buf,
  213. const void *tdi_buf, unsigned tap_len, tap_state_t start_tap_state)
  214. {
  215. return start_tap_state;
  216. }
  217. #endif // _DEBUG_JTAG_IO_
  218. /*-----</Cable Helper API>------------------------------------------*/
  219. extern tap_state_t cmd_queue_end_state; /* finish DR scans in dr_end_state */
  220. extern tap_state_t cmd_queue_cur_state; /* current TAP state */
  221. typedef void* error_handler_t; /* Later on we can delete error_handler_t, but keep it for now to make patches more readable */
  222. struct scan_field_s;
  223. typedef int (*in_handler_t)(u8* in_value, void* priv, struct scan_field_s* field);
  224. typedef struct scan_field_s
  225. {
  226. jtag_tap_t* tap; /* tap pointer this instruction refers to */
  227. int num_bits; /* number of bits this field specifies (up to 32) */
  228. u8* out_value; /* value to be scanned into the device */
  229. u8* in_value; /* pointer to a 32-bit memory location to take data scanned out */
  230. u8* check_value; /* Used together with jtag_add_dr_scan_check() to check data clocked
  231. in */
  232. u8* check_mask; /* mask to go with check_value */
  233. /* internal work space */
  234. int allocated; /* in_value has been allocated for the queue */
  235. int modified; /* did we modify the in_value? */
  236. u8 intmp[4]; /* temporary storage for checking synchronously */
  237. } scan_field_t;
  238. enum scan_type {
  239. /* IN: from device to host, OUT: from host to device */
  240. SCAN_IN = 1, SCAN_OUT = 2, SCAN_IO = 3
  241. };
  242. typedef struct scan_command_s
  243. {
  244. int ir_scan; /* instruction/not data scan */
  245. int num_fields; /* number of fields in *fields array */
  246. scan_field_t* fields; /* pointer to an array of data scan fields */
  247. tap_state_t end_state; /* TAP state in which JTAG commands should finish */
  248. } scan_command_t;
  249. typedef struct statemove_command_s
  250. {
  251. tap_state_t end_state; /* TAP state in which JTAG commands should finish */
  252. } statemove_command_t;
  253. typedef struct pathmove_command_s
  254. {
  255. int num_states; /* number of states in *path */
  256. tap_state_t* path; /* states that have to be passed */
  257. } pathmove_command_t;
  258. typedef struct runtest_command_s
  259. {
  260. int num_cycles; /* number of cycles that should be spent in Run-Test/Idle */
  261. tap_state_t end_state; /* TAP state in which JTAG commands should finish */
  262. } runtest_command_t;
  263. typedef struct stableclocks_command_s
  264. {
  265. int num_cycles; /* number of clock cycles that should be sent */
  266. } stableclocks_command_t;
  267. typedef struct reset_command_s
  268. {
  269. int trst; /* trst/srst 0: deassert, 1: assert, -1: don't change */
  270. int srst;
  271. } reset_command_t;
  272. typedef struct end_state_command_s
  273. {
  274. tap_state_t end_state; /* TAP state in which JTAG commands should finish */
  275. } end_state_command_t;
  276. typedef struct sleep_command_s
  277. {
  278. u32 us; /* number of microseconds to sleep */
  279. } sleep_command_t;
  280. typedef union jtag_command_container_u
  281. {
  282. scan_command_t* scan;
  283. statemove_command_t* statemove;
  284. pathmove_command_t* pathmove;
  285. runtest_command_t* runtest;
  286. stableclocks_command_t* stableclocks;
  287. reset_command_t* reset;
  288. end_state_command_t* end_state;
  289. sleep_command_t* sleep;
  290. } jtag_command_container_t;
  291. enum jtag_command_type {
  292. JTAG_SCAN = 1,
  293. JTAG_STATEMOVE = 2,
  294. JTAG_RUNTEST = 3,
  295. JTAG_RESET = 4,
  296. JTAG_END_STATE = 5,
  297. JTAG_PATHMOVE = 6,
  298. JTAG_SLEEP = 7,
  299. JTAG_STABLECLOCKS = 8
  300. };
  301. typedef struct jtag_command_s
  302. {
  303. jtag_command_container_t cmd;
  304. enum jtag_command_type type;
  305. struct jtag_command_s* next;
  306. } jtag_command_t;
  307. extern jtag_command_t* jtag_command_queue;
  308. /* forward declaration */
  309. typedef struct jtag_tap_event_action_s jtag_tap_event_action_t;
  310. /* this is really: typedef jtag_tap_t */
  311. /* But - the typedef is done in "types.h" */
  312. /* due to "forward decloration reasons" */
  313. struct jtag_tap_s
  314. {
  315. const char* chip;
  316. const char* tapname;
  317. const char* dotted_name;
  318. int abs_chain_position;
  319. int enabled;
  320. int ir_length; /* size of instruction register */
  321. u32 ir_capture_value;
  322. u8* expected; /* Capture-IR expected value */
  323. u32 ir_capture_mask;
  324. u8* expected_mask; /* Capture-IR expected mask */
  325. u32 idcode; /* device identification code */
  326. u32* expected_ids; /* Array of expected identification codes */
  327. u8 expected_ids_cnt; /* Number of expected identification codes */
  328. u8* cur_instr; /* current instruction */
  329. int bypass; /* bypass register selected */
  330. jtag_tap_event_action_t* event_action;
  331. jtag_tap_t* next_tap;
  332. };
  333. extern jtag_tap_t* jtag_AllTaps(void);
  334. extern jtag_tap_t* jtag_TapByPosition(int n);
  335. extern jtag_tap_t* jtag_TapByString(const char* dotted_name);
  336. extern jtag_tap_t* jtag_TapByJimObj(Jim_Interp* interp, Jim_Obj* obj);
  337. extern jtag_tap_t* jtag_TapByAbsPosition(int abs_position);
  338. extern int jtag_NumEnabledTaps(void);
  339. extern int jtag_NumTotalTaps(void);
  340. static __inline__ jtag_tap_t* jtag_NextEnabledTap(jtag_tap_t* p)
  341. {
  342. if (p == NULL)
  343. {
  344. /* start at the head of list */
  345. p = jtag_AllTaps();
  346. }
  347. else
  348. {
  349. /* start *after* this one */
  350. p = p->next_tap;
  351. }
  352. while (p)
  353. {
  354. if (p->enabled)
  355. {
  356. break;
  357. }
  358. else
  359. {
  360. p = p->next_tap;
  361. }
  362. }
  363. return p;
  364. }
  365. enum reset_line_mode {
  366. LINE_OPEN_DRAIN = 0x0,
  367. LINE_PUSH_PULL = 0x1,
  368. };
  369. typedef struct jtag_interface_s
  370. {
  371. char* name;
  372. /* queued command execution
  373. */
  374. int (*execute_queue)(void);
  375. /* interface initalization
  376. */
  377. int (*speed)(int speed);
  378. int (*register_commands)(struct command_context_s* cmd_ctx);
  379. int (*init)(void);
  380. int (*quit)(void);
  381. /* returns JTAG maxium speed for KHz. 0=RTCK. The function returns
  382. * a failure if it can't support the KHz/RTCK.
  383. *
  384. * WARNING!!!! if RTCK is *slow* then think carefully about
  385. * whether you actually want to support this in the driver.
  386. * Many target scripts are written to handle the absence of RTCK
  387. * and use a fallback kHz TCK.
  388. */
  389. int (*khz)(int khz, int* jtag_speed);
  390. /* returns the KHz for the provided JTAG speed. 0=RTCK. The function returns
  391. * a failure if it can't support the KHz/RTCK. */
  392. int (*speed_div)(int speed, int* khz);
  393. /* Read and clear the power dropout flag. Note that a power dropout
  394. * can be transitionary, easily much less than a ms.
  395. *
  396. * So to find out if the power is *currently* on, you must invoke
  397. * this method twice. Once to clear the power dropout flag and a
  398. * second time to read the current state.
  399. *
  400. * Currently the default implementation is never to detect power dropout.
  401. */
  402. int (*power_dropout)(int* power_dropout);
  403. /* Read and clear the srst asserted detection flag.
  404. *
  405. * NB!!!! like power_dropout this does *not* read the current
  406. * state. srst assertion is transitionary and *can* be much
  407. * less than 1ms.
  408. */
  409. int (*srst_asserted)(int* srst_asserted);
  410. } jtag_interface_t;
  411. enum jtag_event {
  412. JTAG_TRST_ASSERTED
  413. };
  414. extern char* jtag_event_strings[];
  415. enum jtag_tap_event {
  416. JTAG_TAP_EVENT_ENABLE,
  417. JTAG_TAP_EVENT_DISABLE
  418. };
  419. extern const Jim_Nvp nvp_jtag_tap_event[];
  420. struct jtag_tap_event_action_s
  421. {
  422. enum jtag_tap_event event;
  423. Jim_Obj* body;
  424. jtag_tap_event_action_t* next;
  425. };
  426. extern int jtag_trst;
  427. extern int jtag_srst;
  428. typedef struct jtag_event_callback_s
  429. {
  430. int (*callback)(enum jtag_event event, void* priv);
  431. void* priv;
  432. struct jtag_event_callback_s* next;
  433. } jtag_event_callback_t;
  434. extern jtag_event_callback_t* jtag_event_callbacks;
  435. extern jtag_interface_t* jtag; /* global pointer to configured JTAG interface */
  436. extern int jtag_speed;
  437. extern int jtag_speed_post_reset;
  438. enum reset_types {
  439. RESET_NONE = 0x0,
  440. RESET_HAS_TRST = 0x1,
  441. RESET_HAS_SRST = 0x2,
  442. RESET_TRST_AND_SRST = 0x3,
  443. RESET_SRST_PULLS_TRST = 0x4,
  444. RESET_TRST_PULLS_SRST = 0x8,
  445. RESET_TRST_OPEN_DRAIN = 0x10,
  446. RESET_SRST_PUSH_PULL = 0x20,
  447. };
  448. extern enum reset_types jtag_reset_config;
  449. /* initialize interface upon startup. A successful no-op
  450. * upon subsequent invocations
  451. */
  452. extern int jtag_interface_init(struct command_context_s* cmd_ctx);
  453. /* initialize JTAG chain using only a RESET reset. If init fails,
  454. * try reset + init.
  455. */
  456. extern int jtag_init(struct command_context_s* cmd_ctx);
  457. /* reset, then initialize JTAG chain */
  458. extern int jtag_init_reset(struct command_context_s* cmd_ctx);
  459. extern int jtag_register_commands(struct command_context_s* cmd_ctx);
  460. /* JTAG interface, can be implemented with a software or hardware fifo
  461. *
  462. * TAP_DRSHIFT and TAP_IRSHIFT are illegal end states. TAP_DRSHIFT/IRSHIFT as end states
  463. * can be emulated by using a larger scan.
  464. *
  465. * Code that is relatively insensitive to the path(as long
  466. * as it is JTAG compliant) taken through state machine can use
  467. * endstate for jtag_add_xxx_scan(). Otherwise the pause state must be
  468. * specified as end state and a subsequent jtag_add_pathmove() must
  469. * be issued.
  470. *
  471. */
  472. extern void jtag_add_ir_scan(int num_fields, scan_field_t* fields, tap_state_t endstate);
  473. /* same as jtag_add_ir_scan except no verify is performed */
  474. extern void jtag_add_ir_scan_noverify(int num_fields, scan_field_t *fields, tap_state_t state);
  475. extern int interface_jtag_add_ir_scan(int num_fields, scan_field_t* fields, tap_state_t endstate);
  476. extern void jtag_add_dr_scan(int num_fields, scan_field_t* fields, tap_state_t endstate);
  477. /* set in_value to point to 32 bits of memory to scan into. This function
  478. * is a way to handle the case of synchronous and asynchronous
  479. * JTAG queues.
  480. *
  481. * In the event of an asynchronous queue execution the queue buffer
  482. * allocation method is used, for the synchronous case the temporary 32 bits come
  483. * from the input field itself.
  484. */
  485. #ifndef HAVE_JTAG_MINIDRIVER_H
  486. extern void jtag_alloc_in_value32(scan_field_t *field);
  487. #else
  488. static __inline__ void jtag_alloc_in_value32(scan_field_t *field)
  489. {
  490. field->in_value=field->intmp;
  491. }
  492. #endif
  493. /* This version of jtag_add_dr_scan() uses the check_value/mask fields */
  494. extern void jtag_add_dr_scan_check(int num_fields, scan_field_t* fields, tap_state_t endstate);
  495. extern int interface_jtag_add_dr_scan(int num_fields, scan_field_t* fields, tap_state_t endstate);
  496. extern void jtag_add_plain_ir_scan(int num_fields, scan_field_t* fields, tap_state_t endstate);
  497. extern int interface_jtag_add_plain_ir_scan(int num_fields, scan_field_t* fields, tap_state_t endstate);
  498. extern void jtag_add_plain_dr_scan(int num_fields, scan_field_t* fields, tap_state_t endstate);
  499. extern int interface_jtag_add_plain_dr_scan(int num_fields, scan_field_t* fields, tap_state_t endstate);
  500. /* Simplest/typical callback - do some conversion on the data clocked in.
  501. * This callback is for such conversion that can not fail.
  502. * For conversion types or checks that can
  503. * fail, use the jtag_callback_t variant */
  504. typedef void (*jtag_callback1_t)(u8 *in);
  505. #ifndef HAVE_JTAG_MINIDRIVER_H
  506. /* A simpler version of jtag_add_callback4 */
  507. extern void jtag_add_callback(jtag_callback1_t, u8 *in);
  508. #else
  509. /* implemented by minidriver */
  510. #endif
  511. #ifdef __ECOS
  512. typedef CYG_ADDRWORD intptr_t;
  513. #endif
  514. /* This type can store an integer safely by a normal cast on 64 and
  515. * 32 bit systems. */
  516. typedef intptr_t jtag_callback_data_t;
  517. /* The generic callback mechanism.
  518. *
  519. * The callback is invoked with three arguments. The first argument is
  520. * the pointer to the data clocked in.
  521. */
  522. typedef int (*jtag_callback_t)(u8 *in, jtag_callback_data_t data1, jtag_callback_data_t data2, jtag_callback_data_t data3);
  523. /* This callback can be executed immediately the queue has been flushed. Note that
  524. * the JTAG queue can either be executed synchronously or asynchronously. Typically
  525. * for USB the queue is executed asynchronously. For low latency interfaces, the
  526. * queue may be executed synchronously.
  527. *
  528. * These callbacks are typically executed *after* the *entire* JTAG queue has been
  529. * executed for e.g. USB interfaces.
  530. *
  531. * The callbacks are guaranteeed to be invoked in the order that they were queued.
  532. *
  533. * The strange name is due to C's lack of overloading using function arguments
  534. *
  535. * The callback mechansim is very general and does not really make any assumptions
  536. * about what the callback does and what the arguments are.
  537. *
  538. * in - typically used to point to the data to operate on. More often than not
  539. * this will be the data clocked in during a shift operation
  540. *
  541. * data1 - an integer that is big enough to be used either as an 'int' or
  542. * cast to/from a pointer
  543. *
  544. * data2 - an integer that is big enough to be used either as an 'int' or
  545. * cast to/from a pointer
  546. *
  547. * Why stop at 'data2' for arguments? Somewhat historical reasons. This is
  548. * sufficient to implement the jtag_check_value_mask(), besides the
  549. * line is best drawn somewhere...
  550. *
  551. * If the execution of the queue fails before the callbacks, then the
  552. * callbacks may or may not be invoked depending on driver implementation.
  553. */
  554. #ifndef HAVE_JTAG_MINIDRIVER_H
  555. extern void jtag_add_callback4(jtag_callback_t, u8 *in, jtag_callback_data_t data1, jtag_callback_data_t data2, jtag_callback_data_t data3);
  556. #else
  557. /* implemented by minidriver */
  558. #endif
  559. /* run a TAP_RESET reset. End state is TAP_RESET, regardless
  560. * of start state.
  561. */
  562. extern void jtag_add_tlr(void);
  563. extern int interface_jtag_add_tlr(void);
  564. /* Application code *must* assume that interfaces will
  565. * implement transitions between states with different
  566. * paths and path lengths through the state diagram. The
  567. * path will vary across interface and also across versions
  568. * of the same interface over time. Even if the OpenOCD code
  569. * is unchanged, the actual path taken may vary over time
  570. * and versions of interface firmware or PCB revisions.
  571. *
  572. * Use jtag_add_pathmove() when specific transition sequences
  573. * are required.
  574. *
  575. * Do not use jtag_add_pathmove() unless you need to, but do use it
  576. * if you have to.
  577. *
  578. * DANGER! If the target is dependent upon a particular sequence
  579. * of transitions for things to work correctly(e.g. as a workaround
  580. * for an errata that contradicts the JTAG standard), then pathmove
  581. * must be used, even if some jtag interfaces happen to use the
  582. * desired path. Worse, the jtag interface used for testing a
  583. * particular implementation, could happen to use the "desired"
  584. * path when transitioning to/from end
  585. * state.
  586. *
  587. * A list of unambigious single clock state transitions, not
  588. * all drivers can support this, but it is required for e.g.
  589. * XScale and Xilinx support
  590. *
  591. * Note! TAP_RESET must not be used in the path!
  592. *
  593. * Note that the first on the list must be reachable
  594. * via a single transition from the current state.
  595. *
  596. * All drivers are required to implement jtag_add_pathmove().
  597. * However, if the pathmove sequence can not be precisely
  598. * executed, an interface_jtag_add_pathmove() or jtag_execute_queue()
  599. * must return an error. It is legal, but not recommended, that
  600. * a driver returns an error in all cases for a pathmove if it
  601. * can only implement a few transitions and therefore
  602. * a partial implementation of pathmove would have little practical
  603. * application.
  604. */
  605. extern void jtag_add_pathmove(int num_states, tap_state_t* path);
  606. extern int interface_jtag_add_pathmove(int num_states, tap_state_t* path);
  607. /* go to TAP_IDLE, if we're not already there and cycle
  608. * precisely num_cycles in the TAP_IDLE after which move
  609. * to the end state, if it is != TAP_IDLE
  610. *
  611. * nb! num_cycles can be 0, in which case the fn will navigate
  612. * to endstate via TAP_IDLE
  613. */
  614. extern void jtag_add_runtest(int num_cycles, tap_state_t endstate);
  615. extern int interface_jtag_add_runtest(int num_cycles, tap_state_t endstate);
  616. /* A reset of the TAP state machine can be requested.
  617. *
  618. * Whether tms or trst reset is used depends on the capabilities of
  619. * the target and jtag interface(reset_config command configures this).
  620. *
  621. * srst can driver a reset of the TAP state machine and vice
  622. * versa
  623. *
  624. * Application code may need to examine value of jtag_reset_config
  625. * to determine the proper codepath
  626. *
  627. * DANGER! Even though srst drives trst, trst might not be connected to
  628. * the interface, and it might actually be *harmful* to assert trst in this case.
  629. *
  630. * This is why combinations such as "reset_config srst_only srst_pulls_trst"
  631. * are supported.
  632. *
  633. * only req_tlr_or_trst and srst can have a transition for a
  634. * call as the effects of transitioning both at the "same time"
  635. * are undefined, but when srst_pulls_trst or vice versa,
  636. * then trst & srst *must* be asserted together.
  637. */
  638. extern void jtag_add_reset(int req_tlr_or_trst, int srst);
  639. /* this drives the actual srst and trst pins. srst will always be 0
  640. * if jtag_reset_config & RESET_SRST_PULLS_TRST != 0 and ditto for
  641. * trst.
  642. *
  643. * the higher level jtag_add_reset will invoke jtag_add_tlr() if
  644. * approperiate
  645. */
  646. extern int interface_jtag_add_reset(int trst, int srst);
  647. extern void jtag_add_end_state(tap_state_t endstate);
  648. extern int interface_jtag_add_end_state(tap_state_t endstate);
  649. extern void jtag_add_sleep(u32 us);
  650. extern int interface_jtag_add_sleep(u32 us);
  651. /**
  652. * Function jtag_add_stable_clocks
  653. * first checks that the state in which the clocks are to be issued is
  654. * stable, then queues up clock_count clocks for transmission.
  655. */
  656. void jtag_add_clocks(int num_cycles);
  657. int interface_jtag_add_clocks(int num_cycles);
  658. /*
  659. * For software FIFO implementations, the queued commands can be executed
  660. * during this call or earlier. A sw queue might decide to push out
  661. * some of the jtag_add_xxx() operations once the queue is "big enough".
  662. *
  663. * This fn will return an error code if any of the prior jtag_add_xxx()
  664. * calls caused a failure, e.g. check failure. Note that it does not
  665. * matter if the operation was executed *before* jtag_execute_queue(),
  666. * jtag_execute_queue() will still return an error code.
  667. *
  668. * All jtag_add_xxx() calls that have in_handler!=NULL will have been
  669. * executed when this fn returns, but if what has been queued only
  670. * clocks data out, without reading anything back, then JTAG could
  671. * be running *after* jtag_execute_queue() returns. The API does
  672. * not define a way to flush a hw FIFO that runs *after*
  673. * jtag_execute_queue() returns.
  674. *
  675. * jtag_add_xxx() commands can either be executed immediately or
  676. * at some time between the jtag_add_xxx() fn call and jtag_execute_queue().
  677. */
  678. extern int jtag_execute_queue(void);
  679. /* same as jtag_execute_queue() but does not clear the error flag */
  680. extern void jtag_execute_queue_noclear(void);
  681. /* this flag is set when an error occurs while executing the queue. cleared
  682. * by jtag_execute_queue()
  683. *
  684. * this flag can also be set from application code if some error happens
  685. * during processing that should be reported during jtag_execute_queue().
  686. */
  687. extern int jtag_error;
  688. static __inline__ void jtag_set_error(int error)
  689. {
  690. if ((error==ERROR_OK)||(jtag_error!=ERROR_OK))
  691. {
  692. /* keep first error */
  693. return;
  694. }
  695. jtag_error=error;
  696. }
  697. /* can be implemented by hw+sw */
  698. extern int interface_jtag_execute_queue(void);
  699. extern int jtag_power_dropout(int* dropout);
  700. extern int jtag_srst_asserted(int* srst_asserted);
  701. /* JTAG support functions */
  702. struct invalidstruct
  703. {
  704. };
  705. /* execute jtag queue and check value and use mask if mask is != NULL. invokes
  706. * jtag_set_error() with any error. */
  707. extern void jtag_check_value_mask(scan_field_t *field, u8 *value, u8 *mask);
  708. extern enum scan_type jtag_scan_type(scan_command_t* cmd);
  709. extern int jtag_scan_size(scan_command_t* cmd);
  710. extern int jtag_read_buffer(u8* buffer, scan_command_t* cmd);
  711. extern int jtag_build_buffer(scan_command_t* cmd, u8** buffer);
  712. extern void jtag_sleep(u32 us);
  713. extern int jtag_call_event_callbacks(enum jtag_event event);
  714. extern int jtag_register_event_callback(int (* callback)(enum jtag_event event, void* priv), void* priv);
  715. extern int jtag_verify_capture_ir;
  716. void jtag_tap_handle_event(jtag_tap_t* tap, enum jtag_tap_event e);
  717. /* error codes
  718. * JTAG subsystem uses codes between -100 and -199 */
  719. #define ERROR_JTAG_INIT_FAILED (-100)
  720. #define ERROR_JTAG_INVALID_INTERFACE (-101)
  721. #define ERROR_JTAG_NOT_IMPLEMENTED (-102)
  722. #define ERROR_JTAG_TRST_ASSERTED (-103)
  723. #define ERROR_JTAG_QUEUE_FAILED (-104)
  724. #define ERROR_JTAG_NOT_STABLE_STATE (-105)
  725. #define ERROR_JTAG_DEVICE_ERROR (-107)
  726. /* this allows JTAG devices to implement the entire jtag_xxx() layer in hw/sw */
  727. #ifdef HAVE_JTAG_MINIDRIVER_H
  728. /* Here a #define MINIDRIVER() and an inline version of hw fifo interface_jtag_add_dr_out can be defined */
  729. #include "jtag_minidriver.h"
  730. #define MINIDRIVER(a) notused ## a
  731. #else
  732. #define MINIDRIVER(a) a
  733. /* jtag_add_dr_out() is a faster version of jtag_add_dr_scan()
  734. *
  735. * Current or end_state can not be TAP_RESET. end_state can be TAP_INVALID
  736. *
  737. * num_bits[i] is the number of bits to clock out from value[i] LSB first.
  738. *
  739. * If the device is in bypass, then that is an error condition in
  740. * the caller code that is not detected by this fn, whereas jtag_add_dr_scan()
  741. * does detect it. Similarly if the device is not in bypass, data must
  742. * be passed to it.
  743. *
  744. * If anything fails, then jtag_error will be set and jtag_execute() will
  745. * return an error. There is no way to determine if there was a failure
  746. * during this function call.
  747. *
  748. * Note that this jtag_add_dr_out can be defined as an inline function.
  749. */
  750. extern void interface_jtag_add_dr_out(jtag_tap_t* tap, int num_fields, const int* num_bits, const u32* value,
  751. tap_state_t end_state);
  752. #endif
  753. static __inline__ void jtag_add_dr_out(jtag_tap_t* tap, int num_fields, const int* num_bits, const u32* value,
  754. tap_state_t end_state)
  755. {
  756. if (end_state != TAP_INVALID)
  757. cmd_queue_end_state = end_state;
  758. cmd_queue_cur_state = cmd_queue_end_state;
  759. interface_jtag_add_dr_out(tap, num_fields, num_bits, value, cmd_queue_end_state);
  760. }
  761. #endif /* JTAG_H */