1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
8 * Copyright (C) 2008 Peter Hettkamp *
9 * peter.hettkamp@htp-tel.de *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program; if not, write to the *
23 * Free Software Foundation, Inc., *
24 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
25 ***************************************************************************/
38 #include <sys/types.h>
46 #define XSTATE_MAX_PATH (12)
48 int handle_xsvf_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
);
52 u8
*dr_out_buf
; /* from host to device (TDI) */
53 u8
*dr_in_buf
; /* from device to host (TDO) */
57 int xruntest
= 0; /* number of TCK cycles / microseconds */
58 int xrepeat
= 0x20; /* number of XC9500 retries */
63 enum tap_state xsvf_to_tap
[] =
66 TAP_DRSELECT
, TAP_DRCAPTURE
, TAP_DRSHIFT
, TAP_DREXIT1
, TAP_DRPAUSE
, TAP_DREXIT2
, TAP_DRUPDATE
,
67 TAP_IRSELECT
, TAP_IRCAPTURE
, TAP_IRSHIFT
, TAP_IREXIT1
, TAP_IRPAUSE
, TAP_IREXIT2
, TAP_IRUPDATE
,
72 0x0, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x1, 0x9, 0xa, 0xb, 0xc, 0xe, 0xf
76 /* xsvf has it's own definition of a statemove. This needs
77 * to be handled according to the specs, which has nothing
78 * to do with the JTAG spec or OpenOCD as such.
80 * Implemented via jtag_add_pathmove().
82 void xsvf_add_statemove(enum tap_state state
)
84 enum tap_state moves
[7]; /* max # of transitions */
86 enum tap_state curstate
= cmd_queue_cur_state
;
87 u8 move
= TAP_MOVE(cmd_queue_cur_state
, state
);
89 if ((state
!= TAP_RESET
) && (state
== cmd_queue_cur_state
))
99 int j
= (move
>> i
) & 1;
102 curstate
= tap_transitions
[curstate
].high
;
105 curstate
= tap_transitions
[curstate
].low
;
110 jtag_add_pathmove(7, moves
);
113 int xsvf_register_commands(struct command_context_s
*cmd_ctx
)
115 register_command(cmd_ctx
, NULL
, "xsvf", handle_xsvf_command
,
116 COMMAND_EXEC
, "run xsvf <file>");
121 int xsvf_read_buffer(int num_bits
, int fd
, u8
* buf
)
125 for (num_bytes
= (num_bits
+ 7) / 8; num_bytes
> 0; num_bytes
--)
127 if (read(fd
, buf
+ num_bytes
- 1, 1) < 0)
128 return ERROR_XSVF_EOF
;
134 int xsvf_read_xstates(int fd
, enum tap_state
*path
, int max_path
, int *path_len
)
139 while ((read(fd
, &c
, 1) > 0) && (c
== 0x12))
141 if (*path_len
> max_path
)
143 LOG_WARNING("XSTATE path longer than max_path");
146 if (read(fd
, &uc
, 1) < 0)
148 return ERROR_XSVF_EOF
;
150 path
[(*path_len
)++] = xsvf_to_tap
[uc
];
153 lseek(fd
, -1, SEEK_CUR
);
158 int handle_xsvf_command(struct command_context_s
*cmd_ctx
, char *cmd
, char **args
, int argc
)
162 unsigned char uc
, uc2
;
168 int tdo_mismatch
= 0;
170 int runtest_requires_tck
= 0;
172 jtag_tap_t
*tap
= NULL
;
173 /* use NULL to indicate a "plain" xsvf file which accounts for
174 additional devices in the scan chain, otherwise the device
175 that should be affected */
179 command_print(cmd_ctx
, "usage: xsvf <device#|plain> <file> <variant>");
183 if (strcmp(args
[0], "plain") != 0)
185 tap
= jtag_TapByString( args
[0] );
187 command_print( cmd_ctx
, "Tap: %s unknown", args
[0] );
192 if ((xsvf_fd
= open(args
[1], O_RDONLY
)) < 0)
194 command_print(cmd_ctx
, "file %s not found", args
[0]);
198 if ((argc
> 2) && (strcmp(args
[2], "virt2") == 0))
200 runtest_requires_tck
= 1;
203 while (read(xsvf_fd
, &c
, 1) > 0)
207 case 0x00: /* XCOMPLETE */
208 LOG_DEBUG("XCOMPLETE");
209 if (jtag_execute_queue() != ERROR_OK
)
215 case 0x01: /* XTDOMASK */
216 LOG_DEBUG("XTDOMASK");
217 if (dr_in_mask
&& (xsvf_read_buffer(xsdrsize
, xsvf_fd
, dr_in_mask
) != ERROR_OK
))
220 case 0x02: /* XSIR */
222 if (read(xsvf_fd
, &c
, 1) < 0)
226 u8
*ir_buf
= malloc((c
+ 7) / 8);
227 if (xsvf_read_buffer(c
, xsvf_fd
, ir_buf
) != ERROR_OK
)
234 field
.out_value
= ir_buf
;
235 field
.out_mask
= NULL
;
236 field
.in_value
= NULL
;
237 field
.in_check_value
= NULL
;
238 field
.in_check_mask
= NULL
;
239 field
.in_handler
= NULL
;
240 field
.in_handler_priv
= NULL
;
242 jtag_add_plain_ir_scan(1, &field
, TAP_IRPAUSE
);
244 jtag_add_ir_scan(1, &field
, TAP_IRPAUSE
);
245 if (jtag_execute_queue() != ERROR_OK
)
253 if (runtest_requires_tck
)
254 jtag_add_runtest(xruntest
, xsvf_to_tap
[xendir
]);
257 xsvf_add_statemove(TAP_IDLE
);
258 jtag_add_sleep(xruntest
);
259 xsvf_add_statemove(xsvf_to_tap
[xendir
]);
262 else if (xendir
!= 0xd) /* Pause-IR */
263 xsvf_add_statemove(xsvf_to_tap
[xendir
]);
268 case 0x03: /* XSDR */
270 if (xsvf_read_buffer(xsdrsize
, xsvf_fd
, dr_out_buf
) != ERROR_OK
)
276 field
.num_bits
= xsdrsize
;
277 field
.out_value
= dr_out_buf
;
278 field
.out_mask
= NULL
;
279 field
.in_value
= NULL
;
280 jtag_set_check_value(&field
, dr_in_buf
, dr_in_mask
, NULL
);
282 jtag_add_plain_dr_scan(1, &field
, TAP_DRPAUSE
);
284 jtag_add_dr_scan(1, &field
, TAP_DRPAUSE
);
285 if (jtag_execute_queue() != ERROR_OK
)
292 if (runtest_requires_tck
)
293 jtag_add_runtest(xruntest
, xsvf_to_tap
[xenddr
]);
296 xsvf_add_statemove(TAP_IDLE
);
297 jtag_add_sleep(xruntest
);
298 xsvf_add_statemove(xsvf_to_tap
[xenddr
]);
301 else if (xendir
!= 0x6) /* Pause-DR */
302 xsvf_add_statemove(xsvf_to_tap
[xenddr
]);
305 case 0x04: /* XRUNTEST */
306 LOG_DEBUG("XRUNTEST");
307 if (read(xsvf_fd
, buf4
, 4) < 0)
311 xruntest
= be_to_h_u32(buf4
);
314 case 0x07: /* XREPEAT */
315 LOG_DEBUG("XREPEAT");
316 if (read(xsvf_fd
, &c
, 1) < 0)
323 case 0x08: /* XSDRSIZE */
324 LOG_DEBUG("XSDRSIZE");
325 if (read(xsvf_fd
, buf4
, 4) < 0)
329 xsdrsize
= be_to_h_u32(buf4
);
333 dr_out_buf
= malloc((xsdrsize
+ 7) / 8);
334 dr_in_buf
= malloc((xsdrsize
+ 7) / 8);
335 dr_in_mask
= malloc((xsdrsize
+ 7) / 8);
338 case 0x09: /* XSDRTDO */
339 LOG_DEBUG("XSDRTDO");
340 if (xsvf_read_buffer(xsdrsize
, xsvf_fd
, dr_out_buf
) != ERROR_OK
)
344 if (xsvf_read_buffer(xsdrsize
, xsvf_fd
, dr_in_buf
) != ERROR_OK
)
350 field
.num_bits
= xsdrsize
;
351 field
.out_value
= dr_out_buf
;
352 field
.out_mask
= NULL
;
353 field
.in_value
= NULL
;
354 jtag_set_check_value(&field
, dr_in_buf
, dr_in_mask
, NULL
);
356 jtag_add_plain_dr_scan(1, &field
, TAP_DRPAUSE
);
358 jtag_add_dr_scan(1, &field
, TAP_DRPAUSE
);
359 if (jtag_execute_queue() != ERROR_OK
)
366 if (runtest_requires_tck
)
367 jtag_add_runtest(xruntest
, xsvf_to_tap
[xenddr
]);
370 xsvf_add_statemove(TAP_IDLE
);
371 jtag_add_sleep(xruntest
);
372 xsvf_add_statemove(xsvf_to_tap
[xenddr
]);
375 else if (xendir
!= 0x6) /* Pause-DR */
376 xsvf_add_statemove(xsvf_to_tap
[xenddr
]);
380 case 0x0a: /* XSETDRMASKS */
381 LOG_ERROR("unsupported XSETSDRMASKS\n");
384 case 0x0b: /* XSDRINC */
385 LOG_ERROR("unsupported XSDRINC\n");
388 case 0x0c: /* XSDRB */
391 case 0x0d: /* XSDRC */
394 case 0x0e: /* XSDRE */
397 case 0x0f: /* XSDRTDOB */
400 case 0x10: /* XSDRTDOB */
403 case 0x11: /* XSDRTDOB */
406 case 0x12: /* XSTATE */
408 if (read(xsvf_fd
, &uc
, 1) < 0)
412 enum tap_state
*path
= calloc(XSTATE_MAX_PATH
, 4);
414 path
[0] = xsvf_to_tap
[uc
];
415 if (xsvf_read_xstates(xsvf_fd
, path
, XSTATE_MAX_PATH
, &path_len
) != ERROR_OK
)
420 /* here the trick is that jtag_add_pathmove() must end in a stable
421 state, so we must only invoke jtag_add_tlr() when we absolutely
424 for(i
=0,lasti
=0;i
<path_len
;i
++)
426 if(path
[i
]==TAP_RESET
)
430 jtag_add_pathmove(i
-lasti
,path
+lasti
);
438 jtag_add_pathmove(i
-lasti
, path
+lasti
);
444 case 0x13: /* XENDIR */
446 if (read(xsvf_fd
, &c
, 1) < 0)
456 LOG_ERROR("unknown XENDIR endstate");
461 case 0x14: /* XENDDR */
463 if (read(xsvf_fd
, &c
, 1) < 0)
473 LOG_ERROR("unknown XENDDR endstate");
478 case 0x15: /* XSIR2 */
480 if (read(xsvf_fd
, buf2
, 2) < 0)
485 us
= be_to_h_u16(buf2
);
486 ir_buf
= malloc((us
+ 7) / 8);
487 if (xsvf_read_buffer(us
, xsvf_fd
, ir_buf
) != ERROR_OK
)
494 field
.out_value
= ir_buf
;
495 field
.out_mask
= NULL
;
496 field
.in_value
= NULL
;
497 field
.in_check_value
= NULL
;
498 field
.in_check_mask
= NULL
;
499 field
.in_handler
= NULL
;
500 field
.in_handler_priv
= NULL
;
502 jtag_add_plain_ir_scan(1, &field
, xsvf_to_tap
[xendir
]);
504 jtag_add_ir_scan(1, &field
, xsvf_to_tap
[xendir
]);
509 case 0x16: /* XCOMMENT */
512 if (read(xsvf_fd
, &c
, 1) < 0)
519 case 0x17: /* XWAIT */
521 if ((read(xsvf_fd
, &uc
, 1) < 0) || (read(xsvf_fd
, &uc2
, 1) < 0) || (read(xsvf_fd
, buf4
, 4) < 0))
525 xsvf_add_statemove(xsvf_to_tap
[uc
]);
526 ui
= be_to_h_u32(buf4
);
528 xsvf_add_statemove(xsvf_to_tap
[uc2
]);
532 LOG_ERROR("unknown xsvf command (0x%2.2x)\n", c
);
536 if (do_abort
|| unsupported
|| tdo_mismatch
)
542 command_print(cmd_ctx
, "TDO mismatch, aborting");
548 command_print(cmd_ctx
, "unsupported xsvf command encountered, aborting");
554 command_print(cmd_ctx
, "premature end detected, aborting");
569 command_print(cmd_ctx
, "XSVF file programmed successfully");