1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
32 #include <sys/types.h>
40 #define XSTATE_MAX_PATH (12)
42 int handle_xsvf_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
46 u8 *dr_out_buf; /* from host to device (TDI) */
47 u8 *dr_in_buf; /* from device to host (TDO) */
51 int xruntest = 0; /* number of TCK cycles / microseconds */
52 int xrepeat = 0x20; /* number of XC9500 retries */
57 enum tap_state xsvf_to_tap[] =
60 TAP_SDS, TAP_CD, TAP_SD, TAP_E1D, TAP_PD, TAP_E2D, TAP_UD,
61 TAP_SIS, TAP_CI, TAP_SI, TAP_E1I, TAP_PI, TAP_E2I, TAP_UI,
66 0x0, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x1, 0x9, 0xa, 0xb, 0xc, 0xe, 0xf
70 /* xsvf has it's own definition of a statemove. This needs
71 * to be handled according to the specs, which has nothing
72 * to do with the JTAG spec or OpenOCD as such.
74 * Implemented via jtag_add_pathmove().
76 void xsvf_add_statemove(enum tap_state state)
78 enum tap_state moves[7]; /* max # of transitions */
80 enum tap_state curstate=cmd_queue_cur_state;
81 u8 move=tap_move[cmd_queue_cur_state][state];
83 if ((state!=TAP_TLR)&&(state==cmd_queue_cur_state))
90 curstate=tap_transitions[curstate].high;
93 curstate=tap_transitions[curstate].low;
97 jtag_add_pathmove(7, moves);
100 int xsvf_register_commands(struct command_context_s *cmd_ctx)
102 register_command(cmd_ctx, NULL, "xsvf", handle_xsvf_command,
103 COMMAND_EXEC, "run xsvf <file>");
108 int xsvf_read_buffer(int num_bits, int fd, u8* buf)
112 for (num_bytes = (num_bits + 7) / 8; num_bytes > 0; num_bytes--)
114 if (read(fd, buf + num_bytes - 1, 1) < 0)
115 return ERROR_XSVF_EOF;
121 int xsvf_read_xstates(int fd, enum tap_state *path, int max_path, int *path_len)
126 while ((read(fd, &c, 1) > 0) && (c == 0x12))
128 if (*path_len > max_path)
130 LOG_WARNING("XSTATE path longer than max_path");
133 if (read(fd, &uc, 1) < 0)
135 return ERROR_XSVF_EOF;
137 path[(*path_len)++] = xsvf_to_tap[uc];
140 lseek(fd, -1, SEEK_CUR);
145 int handle_xsvf_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
149 unsigned char uc, uc2;
155 int tdo_mismatch = 0;
157 int runtest_requires_tck = 0;
159 int device = -1; /* use -1 to indicate a "plain" xsvf file which accounts for additional devices in the scan chain, otherwise the device that should be affected */
163 command_print(cmd_ctx, "usage: xsvf <device#|plain> <file> <variant>");
167 if (strcmp(args[0], "plain") != 0)
169 device = strtoul(args[0], NULL, 0);
172 if ((xsvf_fd = open(args[1], O_RDONLY)) < 0)
174 command_print(cmd_ctx, "file %s not found", args[0]);
178 if ((argc > 2) && (strcmp(args[2], "virt2") == 0))
180 runtest_requires_tck = 1;
183 while (read(xsvf_fd, &c, 1) > 0)
187 case 0x00: /* XCOMPLETE */
188 LOG_DEBUG("XCOMPLETE");
189 if (jtag_execute_queue() != ERROR_OK)
195 case 0x01: /* XTDOMASK */
196 LOG_DEBUG("XTDOMASK");
197 if (dr_in_mask && (xsvf_read_buffer(xsdrsize, xsvf_fd, dr_in_mask) != ERROR_OK))
200 case 0x02: /* XSIR */
202 if (read(xsvf_fd, &c, 1) < 0)
206 u8 *ir_buf = malloc((c + 7) / 8);
207 if (xsvf_read_buffer(c, xsvf_fd, ir_buf) != ERROR_OK)
212 field.device = device;
214 field.out_value = ir_buf;
215 field.out_mask = NULL;
216 field.in_value = NULL;
217 field.in_check_value = NULL;
218 field.in_check_mask = NULL;
219 field.in_handler = NULL;
220 field.in_handler_priv = NULL;
222 jtag_add_plain_ir_scan(1, &field, TAP_PI);
224 jtag_add_ir_scan(1, &field, TAP_PI);
225 if (jtag_execute_queue() != ERROR_OK)
233 if (runtest_requires_tck)
234 jtag_add_runtest(xruntest, xsvf_to_tap[xendir]);
237 xsvf_add_statemove(TAP_RTI);
238 jtag_add_sleep(xruntest);
239 xsvf_add_statemove(xsvf_to_tap[xendir]);
242 else if (xendir != 0xd) /* Pause-IR */
243 xsvf_add_statemove(xsvf_to_tap[xendir]);
248 case 0x03: /* XSDR */
250 if (xsvf_read_buffer(xsdrsize, xsvf_fd, dr_out_buf) != ERROR_OK)
255 field.device = device;
256 field.num_bits = xsdrsize;
257 field.out_value = dr_out_buf;
258 field.out_mask = NULL;
259 field.in_value = NULL;
260 jtag_set_check_value(&field, dr_in_buf, dr_in_mask, NULL);
262 jtag_add_plain_dr_scan(1, &field, TAP_PD);
264 jtag_add_dr_scan(1, &field, TAP_PD);
265 if (jtag_execute_queue() != ERROR_OK)
272 if (runtest_requires_tck)
273 jtag_add_runtest(xruntest, xsvf_to_tap[xenddr]);
276 xsvf_add_statemove(TAP_RTI);
277 jtag_add_sleep(xruntest);
278 xsvf_add_statemove(xsvf_to_tap[xenddr]);
281 else if (xendir != 0x6) /* Pause-DR */
282 xsvf_add_statemove(xsvf_to_tap[xenddr]);
285 case 0x04: /* XRUNTEST */
286 LOG_DEBUG("XRUNTEST");
287 if (read(xsvf_fd, buf4, 4) < 0)
291 xruntest = be_to_h_u32(buf4);
294 case 0x07: /* XREPEAT */
295 LOG_DEBUG("XREPEAT");
296 if (read(xsvf_fd, &c, 1) < 0)
303 case 0x08: /* XSDRSIZE */
304 LOG_DEBUG("XSDRSIZE");
305 if (read(xsvf_fd, buf4, 4) < 0)
309 xsdrsize = be_to_h_u32(buf4);
313 dr_out_buf = malloc((xsdrsize + 7) / 8);
314 dr_in_buf = malloc((xsdrsize + 7) / 8);
315 dr_in_mask = malloc((xsdrsize + 7) / 8);
318 case 0x09: /* XSDRTDO */
319 LOG_DEBUG("XSDRTDO");
320 if (xsvf_read_buffer(xsdrsize, xsvf_fd, dr_out_buf) != ERROR_OK)
324 if (xsvf_read_buffer(xsdrsize, xsvf_fd, dr_in_buf) != ERROR_OK)
329 field.device = device;
330 field.num_bits = xsdrsize;
331 field.out_value = dr_out_buf;
332 field.out_mask = NULL;
333 field.in_value = NULL;
334 jtag_set_check_value(&field, dr_in_buf, dr_in_mask, NULL);
336 jtag_add_plain_dr_scan(1, &field, TAP_PD);
338 jtag_add_dr_scan(1, &field, TAP_PD);
339 if (jtag_execute_queue() != ERROR_OK)
346 if (runtest_requires_tck)
347 jtag_add_runtest(xruntest, xsvf_to_tap[xenddr]);
350 xsvf_add_statemove(TAP_RTI);
351 jtag_add_sleep(xruntest);
352 xsvf_add_statemove(xsvf_to_tap[xenddr]);
355 else if (xendir != 0x6) /* Pause-DR */
356 xsvf_add_statemove(xsvf_to_tap[xenddr]);
360 case 0x0a: /* XSETDRMASKS */
361 LOG_ERROR("unsupported XSETSDRMASKS\n");
364 case 0x0b: /* XSDRINC */
365 LOG_ERROR("unsupported XSDRINC\n");
368 case 0x0c: /* XSDRB */
371 case 0x0d: /* XSDRC */
374 case 0x0e: /* XSDRE */
377 case 0x0f: /* XSDRTDOB */
380 case 0x10: /* XSDRTDOB */
383 case 0x11: /* XSDRTDOB */
386 case 0x12: /* XSTATE */
388 if (read(xsvf_fd, &uc, 1) < 0)
392 enum tap_state *path = calloc(XSTATE_MAX_PATH, 4);
394 path[0] = xsvf_to_tap[uc];
395 if (xsvf_read_xstates(xsvf_fd, path, XSTATE_MAX_PATH, &path_len) != ERROR_OK)
399 jtag_add_pathmove(path_len, path);
404 case 0x13: /* XENDIR */
406 if (read(xsvf_fd, &c, 1) < 0)
416 LOG_ERROR("unknown XENDIR endstate");
421 case 0x14: /* XENDDR */
423 if (read(xsvf_fd, &c, 1) < 0)
433 LOG_ERROR("unknown XENDDR endstate");
438 case 0x15: /* XSIR2 */
440 if (read(xsvf_fd, buf2, 2) < 0)
445 us = be_to_h_u16(buf2);
446 ir_buf = malloc((us + 7) / 8);
447 if (xsvf_read_buffer(us, xsvf_fd, ir_buf) != ERROR_OK)
452 field.device = device;
454 field.out_value = ir_buf;
455 field.out_mask = NULL;
456 field.in_value = NULL;
457 field.in_check_value = NULL;
458 field.in_check_mask = NULL;
459 field.in_handler = NULL;
460 field.in_handler_priv = NULL;
462 jtag_add_plain_ir_scan(1, &field, xsvf_to_tap[xendir]);
464 jtag_add_ir_scan(1, &field, xsvf_to_tap[xendir]);
469 case 0x16: /* XCOMMENT */
472 if (read(xsvf_fd, &c, 1) < 0)
479 case 0x17: /* XWAIT */
481 if ((read(xsvf_fd, &uc, 1) < 0) || (read(xsvf_fd, &uc2, 1) < 0) || (read(xsvf_fd, buf4, 4) < 0))
485 xsvf_add_statemove(xsvf_to_tap[uc]);
486 ui = be_to_h_u32(buf4);
488 xsvf_add_statemove(xsvf_to_tap[uc2]);
492 LOG_ERROR("unknown xsvf command (0x%2.2x)\n", c);
496 if (do_abort || unsupported || tdo_mismatch)
502 command_print(cmd_ctx, "TDO mismatch, aborting");
508 command_print(cmd_ctx, "unsupported xsvf command encountered, aborting");
514 command_print(cmd_ctx, "premature end detected, aborting");
529 command_print(cmd_ctx, "XSVF file programmed successfully");