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 * 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. *
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. *
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 ***************************************************************************/
35 #include <sys/types.h>
43 #define XSTATE_MAX_PATH (12)
45 int handle_xsvf_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
49 u8 *dr_out_buf; /* from host to device (TDI) */
50 u8 *dr_in_buf; /* from device to host (TDO) */
54 int xruntest = 0; /* number of TCK cycles / microseconds */
55 int xrepeat = 0x20; /* number of XC9500 retries */
60 enum tap_state xsvf_to_tap[] =
63 TAP_SDS, TAP_CD, TAP_SD, TAP_E1D, TAP_PD, TAP_E2D, TAP_UD,
64 TAP_SIS, TAP_CI, TAP_SI, TAP_E1I, TAP_PI, TAP_E2I, TAP_UI,
69 0x0, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x1, 0x9, 0xa, 0xb, 0xc, 0xe, 0xf
73 /* xsvf has it's own definition of a statemove. This needs
74 * to be handled according to the specs, which has nothing
75 * to do with the JTAG spec or OpenOCD as such.
77 * Implemented via jtag_add_pathmove().
79 void xsvf_add_statemove(enum tap_state state)
81 enum tap_state moves[7]; /* max # of transitions */
83 enum tap_state curstate = cmd_queue_cur_state;
84 u8 move = TAP_MOVE(cmd_queue_cur_state, state);
86 if ((state != TAP_TLR) && (state == cmd_queue_cur_state))
91 int j = (move >> i) & 1;
94 curstate = tap_transitions[curstate].high;
97 curstate = tap_transitions[curstate].low;
102 jtag_add_pathmove(7, moves);
105 int xsvf_register_commands(struct command_context_s *cmd_ctx)
107 register_command(cmd_ctx, NULL, "xsvf", handle_xsvf_command,
108 COMMAND_EXEC, "run xsvf <file>");
113 int xsvf_read_buffer(int num_bits, int fd, u8* buf)
117 for (num_bytes = (num_bits + 7) / 8; num_bytes > 0; num_bytes--)
119 if (read(fd, buf + num_bytes - 1, 1) < 0)
120 return ERROR_XSVF_EOF;
126 int xsvf_read_xstates(int fd, enum tap_state *path, int max_path, int *path_len)
131 while ((read(fd, &c, 1) > 0) && (c == 0x12))
133 if (*path_len > max_path)
135 LOG_WARNING("XSTATE path longer than max_path");
138 if (read(fd, &uc, 1) < 0)
140 return ERROR_XSVF_EOF;
142 path[(*path_len)++] = xsvf_to_tap[uc];
145 lseek(fd, -1, SEEK_CUR);
150 int handle_xsvf_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
154 unsigned char uc, uc2;
160 int tdo_mismatch = 0;
162 int runtest_requires_tck = 0;
164 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 */
168 command_print(cmd_ctx, "usage: xsvf <device#|plain> <file> <variant>");
172 if (strcmp(args[0], "plain") != 0)
174 device = strtoul(args[0], NULL, 0);
177 if ((xsvf_fd = open(args[1], O_RDONLY)) < 0)
179 command_print(cmd_ctx, "file %s not found", args[0]);
183 if ((argc > 2) && (strcmp(args[2], "virt2") == 0))
185 runtest_requires_tck = 1;
188 while (read(xsvf_fd, &c, 1) > 0)
192 case 0x00: /* XCOMPLETE */
193 LOG_DEBUG("XCOMPLETE");
194 if (jtag_execute_queue() != ERROR_OK)
200 case 0x01: /* XTDOMASK */
201 LOG_DEBUG("XTDOMASK");
202 if (dr_in_mask && (xsvf_read_buffer(xsdrsize, xsvf_fd, dr_in_mask) != ERROR_OK))
205 case 0x02: /* XSIR */
207 if (read(xsvf_fd, &c, 1) < 0)
211 u8 *ir_buf = malloc((c + 7) / 8);
212 if (xsvf_read_buffer(c, xsvf_fd, ir_buf) != ERROR_OK)
217 field.device = device;
219 field.out_value = ir_buf;
220 field.out_mask = NULL;
221 field.in_value = NULL;
222 field.in_check_value = NULL;
223 field.in_check_mask = NULL;
224 field.in_handler = NULL;
225 field.in_handler_priv = NULL;
227 jtag_add_plain_ir_scan(1, &field, TAP_PI);
229 jtag_add_ir_scan(1, &field, TAP_PI);
230 if (jtag_execute_queue() != ERROR_OK)
238 if (runtest_requires_tck)
239 jtag_add_runtest(xruntest, xsvf_to_tap[xendir]);
242 xsvf_add_statemove(TAP_RTI);
243 jtag_add_sleep(xruntest);
244 xsvf_add_statemove(xsvf_to_tap[xendir]);
247 else if (xendir != 0xd) /* Pause-IR */
248 xsvf_add_statemove(xsvf_to_tap[xendir]);
253 case 0x03: /* XSDR */
255 if (xsvf_read_buffer(xsdrsize, xsvf_fd, dr_out_buf) != ERROR_OK)
260 field.device = device;
261 field.num_bits = xsdrsize;
262 field.out_value = dr_out_buf;
263 field.out_mask = NULL;
264 field.in_value = NULL;
265 jtag_set_check_value(&field, dr_in_buf, dr_in_mask, NULL);
267 jtag_add_plain_dr_scan(1, &field, TAP_PD);
269 jtag_add_dr_scan(1, &field, TAP_PD);
270 if (jtag_execute_queue() != ERROR_OK)
277 if (runtest_requires_tck)
278 jtag_add_runtest(xruntest, xsvf_to_tap[xenddr]);
281 xsvf_add_statemove(TAP_RTI);
282 jtag_add_sleep(xruntest);
283 xsvf_add_statemove(xsvf_to_tap[xenddr]);
286 else if (xendir != 0x6) /* Pause-DR */
287 xsvf_add_statemove(xsvf_to_tap[xenddr]);
290 case 0x04: /* XRUNTEST */
291 LOG_DEBUG("XRUNTEST");
292 if (read(xsvf_fd, buf4, 4) < 0)
296 xruntest = be_to_h_u32(buf4);
299 case 0x07: /* XREPEAT */
300 LOG_DEBUG("XREPEAT");
301 if (read(xsvf_fd, &c, 1) < 0)
308 case 0x08: /* XSDRSIZE */
309 LOG_DEBUG("XSDRSIZE");
310 if (read(xsvf_fd, buf4, 4) < 0)
314 xsdrsize = be_to_h_u32(buf4);
318 dr_out_buf = malloc((xsdrsize + 7) / 8);
319 dr_in_buf = malloc((xsdrsize + 7) / 8);
320 dr_in_mask = malloc((xsdrsize + 7) / 8);
323 case 0x09: /* XSDRTDO */
324 LOG_DEBUG("XSDRTDO");
325 if (xsvf_read_buffer(xsdrsize, xsvf_fd, dr_out_buf) != ERROR_OK)
329 if (xsvf_read_buffer(xsdrsize, xsvf_fd, dr_in_buf) != ERROR_OK)
334 field.device = device;
335 field.num_bits = xsdrsize;
336 field.out_value = dr_out_buf;
337 field.out_mask = NULL;
338 field.in_value = NULL;
339 jtag_set_check_value(&field, dr_in_buf, dr_in_mask, NULL);
341 jtag_add_plain_dr_scan(1, &field, TAP_PD);
343 jtag_add_dr_scan(1, &field, TAP_PD);
344 if (jtag_execute_queue() != ERROR_OK)
351 if (runtest_requires_tck)
352 jtag_add_runtest(xruntest, xsvf_to_tap[xenddr]);
355 xsvf_add_statemove(TAP_RTI);
356 jtag_add_sleep(xruntest);
357 xsvf_add_statemove(xsvf_to_tap[xenddr]);
360 else if (xendir != 0x6) /* Pause-DR */
361 xsvf_add_statemove(xsvf_to_tap[xenddr]);
365 case 0x0a: /* XSETDRMASKS */
366 LOG_ERROR("unsupported XSETSDRMASKS\n");
369 case 0x0b: /* XSDRINC */
370 LOG_ERROR("unsupported XSDRINC\n");
373 case 0x0c: /* XSDRB */
376 case 0x0d: /* XSDRC */
379 case 0x0e: /* XSDRE */
382 case 0x0f: /* XSDRTDOB */
385 case 0x10: /* XSDRTDOB */
388 case 0x11: /* XSDRTDOB */
391 case 0x12: /* XSTATE */
393 if (read(xsvf_fd, &uc, 1) < 0)
397 enum tap_state *path = calloc(XSTATE_MAX_PATH, 4);
399 path[0] = xsvf_to_tap[uc];
400 if (xsvf_read_xstates(xsvf_fd, path, XSTATE_MAX_PATH, &path_len) != ERROR_OK)
404 jtag_add_pathmove(path_len, path);
409 case 0x13: /* XENDIR */
411 if (read(xsvf_fd, &c, 1) < 0)
421 LOG_ERROR("unknown XENDIR endstate");
426 case 0x14: /* XENDDR */
428 if (read(xsvf_fd, &c, 1) < 0)
438 LOG_ERROR("unknown XENDDR endstate");
443 case 0x15: /* XSIR2 */
445 if (read(xsvf_fd, buf2, 2) < 0)
450 us = be_to_h_u16(buf2);
451 ir_buf = malloc((us + 7) / 8);
452 if (xsvf_read_buffer(us, xsvf_fd, ir_buf) != ERROR_OK)
457 field.device = device;
459 field.out_value = ir_buf;
460 field.out_mask = NULL;
461 field.in_value = NULL;
462 field.in_check_value = NULL;
463 field.in_check_mask = NULL;
464 field.in_handler = NULL;
465 field.in_handler_priv = NULL;
467 jtag_add_plain_ir_scan(1, &field, xsvf_to_tap[xendir]);
469 jtag_add_ir_scan(1, &field, xsvf_to_tap[xendir]);
474 case 0x16: /* XCOMMENT */
477 if (read(xsvf_fd, &c, 1) < 0)
484 case 0x17: /* XWAIT */
486 if ((read(xsvf_fd, &uc, 1) < 0) || (read(xsvf_fd, &uc2, 1) < 0) || (read(xsvf_fd, buf4, 4) < 0))
490 xsvf_add_statemove(xsvf_to_tap[uc]);
491 ui = be_to_h_u32(buf4);
493 xsvf_add_statemove(xsvf_to_tap[uc2]);
497 LOG_ERROR("unknown xsvf command (0x%2.2x)\n", c);
501 if (do_abort || unsupported || tdo_mismatch)
507 command_print(cmd_ctx, "TDO mismatch, aborting");
513 command_print(cmd_ctx, "unsupported xsvf command encountered, aborting");
519 command_print(cmd_ctx, "premature end detected, aborting");
534 command_print(cmd_ctx, "XSVF file programmed successfully");