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
69 int xsvf_register_commands(struct command_context_s *cmd_ctx)
71 register_command(cmd_ctx, NULL, "xsvf", handle_xsvf_command,
72 COMMAND_EXEC, "run xsvf <file>");
77 int xsvf_read_buffer(int num_bits, int fd, u8* buf)
81 for (num_bytes = (num_bits + 7) / 8; num_bytes > 0; num_bytes--)
83 if (read(fd, buf + num_bytes - 1, 1) < 0)
84 return ERROR_XSVF_EOF;
90 int xsvf_read_xstates(int fd, enum tap_state *path, int max_path, int *path_len)
95 while ((read(fd, &c, 1) > 0) && (c == 0x12))
97 if (*path_len > max_path)
99 WARNING("XSTATE path longer than max_path");
102 if (read(fd, &uc, 1) < 0)
104 return ERROR_XSVF_EOF;
106 path[(*path_len)++] = xsvf_to_tap[uc];
109 lseek(fd, -1, SEEK_CUR);
114 int handle_xsvf_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
118 unsigned char uc, uc2;
124 int tdo_mismatch = 0;
126 int runtest_requires_tck = 0;
128 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 */
132 command_print(cmd_ctx, "usage: xsvf <device#|plain> <file> <variant>");
136 if (strcmp(args[0], "plain") != 0)
138 device = strtoul(args[0], NULL, 0);
141 if ((xsvf_fd = open(args[1], O_RDONLY)) < 0)
143 command_print(cmd_ctx, "file %s not found", args[0]);
147 if ((argc > 2) && (strcmp(args[2], "virt2") == 0))
149 runtest_requires_tck = 1;
152 while (read(xsvf_fd, &c, 1) > 0)
156 case 0x00: /* XCOMPLETE */
158 if (jtag_execute_queue() != ERROR_OK)
164 case 0x01: /* XTDOMASK */
166 if (dr_in_mask && (xsvf_read_buffer(xsdrsize, xsvf_fd, dr_in_mask) != ERROR_OK))
169 case 0x02: /* XSIR */
171 if (read(xsvf_fd, &c, 1) < 0)
175 u8 *ir_buf = malloc((c + 7) / 8);
176 if (xsvf_read_buffer(c, xsvf_fd, ir_buf) != ERROR_OK)
181 field.device = device;
183 field.out_value = ir_buf;
184 field.out_mask = NULL;
185 field.in_value = NULL;
186 field.in_check_value = NULL;
187 field.in_check_mask = NULL;
188 field.in_handler = NULL;
189 field.in_handler_priv = NULL;
191 jtag_add_plain_ir_scan(1, &field, TAP_PI);
193 jtag_add_ir_scan(1, &field, TAP_PI);
194 if (jtag_execute_queue() != ERROR_OK)
202 if (runtest_requires_tck)
203 jtag_add_runtest(xruntest, xsvf_to_tap[xendir]);
206 jtag_add_statemove(TAP_RTI);
207 jtag_add_sleep(xruntest);
208 jtag_add_statemove(xsvf_to_tap[xendir]);
211 else if (xendir != 0xd) /* Pause-IR */
212 jtag_add_statemove(xsvf_to_tap[xendir]);
217 case 0x03: /* XSDR */
219 if (xsvf_read_buffer(xsdrsize, xsvf_fd, dr_out_buf) != ERROR_OK)
224 field.device = device;
225 field.num_bits = xsdrsize;
226 field.out_value = dr_out_buf;
227 field.out_mask = NULL;
228 field.in_value = NULL;
229 jtag_set_check_value(&field, dr_in_buf, dr_in_mask, NULL);
231 jtag_add_plain_dr_scan(1, &field, TAP_PD);
233 jtag_add_dr_scan(1, &field, TAP_PD);
234 if (jtag_execute_queue() != ERROR_OK)
241 if (runtest_requires_tck)
242 jtag_add_runtest(xruntest, xsvf_to_tap[xenddr]);
245 jtag_add_statemove(TAP_RTI);
246 jtag_add_sleep(xruntest);
247 jtag_add_statemove(xsvf_to_tap[xenddr]);
250 else if (xendir != 0x6) /* Pause-DR */
251 jtag_add_statemove(xsvf_to_tap[xenddr]);
254 case 0x04: /* XRUNTEST */
256 if (read(xsvf_fd, buf4, 4) < 0)
260 xruntest = be_to_h_u32(buf4);
263 case 0x07: /* XREPEAT */
265 if (read(xsvf_fd, &c, 1) < 0)
272 case 0x08: /* XSDRSIZE */
274 if (read(xsvf_fd, buf4, 4) < 0)
278 xsdrsize = be_to_h_u32(buf4);
282 dr_out_buf = malloc((xsdrsize + 7) / 8);
283 dr_in_buf = malloc((xsdrsize + 7) / 8);
284 dr_in_mask = malloc((xsdrsize + 7) / 8);
287 case 0x09: /* XSDRTDO */
289 if (xsvf_read_buffer(xsdrsize, xsvf_fd, dr_out_buf) != ERROR_OK)
293 if (xsvf_read_buffer(xsdrsize, xsvf_fd, dr_in_buf) != ERROR_OK)
298 field.device = device;
299 field.num_bits = xsdrsize;
300 field.out_value = dr_out_buf;
301 field.out_mask = NULL;
302 field.in_value = NULL;
303 jtag_set_check_value(&field, dr_in_buf, dr_in_mask, NULL);
305 jtag_add_plain_dr_scan(1, &field, TAP_PD);
307 jtag_add_dr_scan(1, &field, TAP_PD);
308 if (jtag_execute_queue() != ERROR_OK)
315 if (runtest_requires_tck)
316 jtag_add_runtest(xruntest, xsvf_to_tap[xenddr]);
319 jtag_add_statemove(TAP_RTI);
320 jtag_add_sleep(xruntest);
321 jtag_add_statemove(xsvf_to_tap[xenddr]);
324 else if (xendir != 0x6) /* Pause-DR */
325 jtag_add_statemove(xsvf_to_tap[xenddr]);
329 case 0x0a: /* XSETDRMASKS */
330 ERROR("unsupported XSETSDRMASKS\n");
333 case 0x0b: /* XSDRINC */
334 ERROR("unsupported XSDRINC\n");
337 case 0x0c: /* XSDRB */
340 case 0x0d: /* XSDRC */
343 case 0x0e: /* XSDRE */
346 case 0x0f: /* XSDRTDOB */
349 case 0x10: /* XSDRTDOB */
352 case 0x11: /* XSDRTDOB */
355 case 0x12: /* XSTATE */
357 if (read(xsvf_fd, &uc, 1) < 0)
361 enum tap_state *path = calloc(XSTATE_MAX_PATH, 4);
363 path[0] = xsvf_to_tap[uc];
364 if (xsvf_read_xstates(xsvf_fd, path, XSTATE_MAX_PATH, &path_len) != ERROR_OK)
368 jtag_add_pathmove(path_len, path);
373 case 0x13: /* XENDIR */
375 if (read(xsvf_fd, &c, 1) < 0)
385 ERROR("unknown XENDIR endstate");
390 case 0x14: /* XENDDR */
392 if (read(xsvf_fd, &c, 1) < 0)
402 ERROR("unknown XENDDR endstate");
407 case 0x15: /* XSIR2 */
409 if (read(xsvf_fd, buf2, 2) < 0)
414 us = be_to_h_u16(buf2);
415 ir_buf = malloc((us + 7) / 8);
416 if (xsvf_read_buffer(us, xsvf_fd, ir_buf) != ERROR_OK)
421 field.device = device;
423 field.out_value = ir_buf;
424 field.out_mask = NULL;
425 field.in_value = NULL;
426 field.in_check_value = NULL;
427 field.in_check_mask = NULL;
428 field.in_handler = NULL;
429 field.in_handler_priv = NULL;
431 jtag_add_plain_ir_scan(1, &field, xsvf_to_tap[xendir]);
433 jtag_add_ir_scan(1, &field, xsvf_to_tap[xendir]);
438 case 0x16: /* XCOMMENT */
441 if (read(xsvf_fd, &c, 1) < 0)
448 case 0x17: /* XWAIT */
450 if ((read(xsvf_fd, &uc, 1) < 0) || (read(xsvf_fd, &uc2, 1) < 0) || (read(xsvf_fd, buf4, 4) < 0))
454 jtag_add_statemove(xsvf_to_tap[uc]);
455 ui = be_to_h_u32(buf4);
457 jtag_add_statemove(xsvf_to_tap[uc2]);
461 ERROR("unknown xsvf command (0x%2.2x)\n", c);
465 if (do_abort || unsupported || tdo_mismatch)
471 command_print(cmd_ctx, "TDO mismatch, aborting");
477 command_print(cmd_ctx, "unsupported xsvf command encountered, aborting");
483 command_print(cmd_ctx, "premature end detected, aborting");
498 command_print(cmd_ctx, "XSVF file programmed successfully");