1 /***************************************************************************
2 * Copyright (C) 2004 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 ***************************************************************************/
29 #include "replacements.h"
31 /* project specific includes */
35 #include "configuration.h"
36 #include "time_support.h"
47 /* enable this to debug io latency
50 #define _DEBUG_USB_IO_
53 /* enable this to debug communication
56 #define _DEBUG_USB_COMMS_
59 /* enable this to work around ftd2xx deadlock
62 #define _FTD2XX_QUEUE_DELAY_
65 int ftd2xx_execute_queue(void);
67 int ftd2xx_speed(int speed);
68 int ftd2xx_register_commands(struct command_context_s *cmd_ctx);
69 int ftd2xx_init(void);
70 int ftd2xx_quit(void);
72 int ftd2xx_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
73 int ftd2xx_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
74 int ftd2xx_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
76 char *ftd2xx_device_desc = NULL;
77 char *ftd2xx_layout = NULL;
78 u16 ftd2xx_vid = 0x0403;
79 u16 ftd2xx_pid = 0x6010;
81 typedef struct ftd2xx_layout_s
85 void(*reset)(int trst, int srst);
88 int usbjtag_init(void);
89 int jtagkey_init(void);
90 void usbjtag_reset(int trst, int srst);
91 void jtagkey_reset(int trst, int srst);
93 ftd2xx_layout_t ftd2xx_layouts[] =
95 {"usbjtag", usbjtag_init, usbjtag_reset},
96 {"jtagkey", jtagkey_init, jtagkey_reset},
97 {"jtagkey_prototype_v1", jtagkey_init, jtagkey_reset},
101 static u8 nTRST, nTRSTnOE, nSRST, nSRSTnOE;
103 static ftd2xx_layout_t *layout;
104 static u8 low_output = 0x0;
105 static u8 low_direction = 0x0;
106 static u8 high_output = 0x0;
107 static u8 high_direction = 0x0;
108 static FT_HANDLE ftdih = NULL;
110 static u8 *ftd2xx_buffer = NULL;
111 static int ftd2xx_buffer_size = 0;
112 static int ftd2xx_read_pointer = 0;
113 static int ftd2xx_expect_read = 0;
114 #define FTD2XX_BUFFER_SIZE 131072
115 #define BUFFER_ADD ftd2xx_buffer[ftd2xx_buffer_size++]
116 #define BUFFER_READ ftd2xx_buffer[ftd2xx_read_pointer++]
118 jtag_interface_t ftd2xx_interface =
123 .execute_queue = ftd2xx_execute_queue,
125 .support_statemove = 1,
127 .speed = ftd2xx_speed,
128 .register_commands = ftd2xx_register_commands,
133 int ftd2xx_speed(int speed)
139 buf[0] = 0x86; /* command "set divisor" */
140 buf[1] = speed & 0xff; /* valueL (0=6MHz, 1=3MHz, 2=1.5MHz, ...*/
141 buf[2] = (speed >> 8) & 0xff; /* valueH */
143 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
144 if (((status = FT_Write(ftdih, buf, 3, &bytes_written)) != FT_OK) || (bytes_written != 3))
146 ERROR("couldn't write to ftdi device: %i", status);
153 int ftd2xx_register_commands(struct command_context_s *cmd_ctx)
155 register_command(cmd_ctx, NULL, "ftd2xx_device_desc", ftd2xx_handle_device_desc_command,
156 COMMAND_CONFIG, NULL);
157 register_command(cmd_ctx, NULL, "ftd2xx_layout", ftd2xx_handle_layout_command,
158 COMMAND_CONFIG, NULL);
159 register_command(cmd_ctx, NULL, "ftd2xx_vid_pid", ftd2xx_handle_vid_pid_command,
160 COMMAND_CONFIG, NULL);
164 void ftd2xx_end_state(state)
166 if (tap_move_map[state] != -1)
170 ERROR("BUG: %i is not a valid end state", state);
175 void ftd2xx_read_scan(enum scan_type type, u8* buffer, int scan_size)
177 int num_bytes = ((scan_size + 7) / 8);
178 int bits_left = scan_size;
181 while(num_bytes-- > 1)
183 buffer[cur_byte] = BUFFER_READ;
188 buffer[cur_byte] = 0x0;
192 buffer[cur_byte] = BUFFER_READ >> 1;
195 buffer[cur_byte] = (buffer[cur_byte] | ((BUFFER_READ & 0x02) << 6)) >> (8 - bits_left);
199 void ftd2xx_debug_dump_buffer(void)
205 for (i = 0; i < ftd2xx_buffer_size; i++)
207 line_p += snprintf(line_p, 256 - (line_p - line), "%2.2x ", ftd2xx_buffer[i]);
219 int ftd2xx_send_and_recv(jtag_command_t *first, jtag_command_t *last)
229 #ifdef _DEBUG_USB_IO_
230 struct timeval start, inter, inter2, end;
231 struct timeval d_inter, d_inter2, d_end;
234 #ifdef _DEBUG_USB_COMMS_
235 DEBUG("write buffer (size %i):", ftd2xx_buffer_size);
236 ftd2xx_debug_dump_buffer();
239 #ifdef _DEBUG_USB_IO_
240 gettimeofday(&start, NULL);
243 if ((status = FT_Write(ftdih, ftd2xx_buffer, ftd2xx_buffer_size, &bytes_written)) != FT_OK)
245 ERROR("couldn't write to ftdi device: %i", status);
249 #ifdef _DEBUG_USB_IO_
250 gettimeofday(&inter, NULL);
253 if (ftd2xx_expect_read)
256 ftd2xx_buffer_size = 0;
258 #ifdef _FTD2XX_QUEUE_DELAY_
260 while (inrxqueue < ftd2xx_expect_read)
262 FT_GetQueueStatus(ftdih, &inrxqueue);
263 if (inrxqueue >= ftd2xx_expect_read)
269 #ifdef _DEBUG_USB_IO_
270 gettimeofday(&inter2, NULL);
273 if ((status = FT_Read(ftdih, ftd2xx_buffer, ftd2xx_expect_read, &bytes_read)) != FT_OK)
275 ERROR("couldn't read from ftdi device: %i", status);
279 #ifdef _DEBUG_USB_IO_
280 gettimeofday(&end, NULL);
282 timeval_subtract(&d_inter, &inter, &start);
283 timeval_subtract(&d_inter2, &inter2, &start);
284 timeval_subtract(&d_end, &end, &start);
286 INFO("inter: %i.%i, inter2: %i.%i end: %i.%i", d_inter.tv_sec, d_inter.tv_usec, d_inter2.tv_sec, d_inter2.tv_usec, d_end.tv_sec, d_end.tv_usec);
290 ftd2xx_buffer_size = bytes_read;
292 if (ftd2xx_expect_read != ftd2xx_buffer_size)
294 ERROR("ftd2xx_expect_read (%i) != ftd2xx_buffer_size (%i) (%i retries)", ftd2xx_expect_read, ftd2xx_buffer_size, 100 - timeout);
295 ftd2xx_debug_dump_buffer();
300 #ifdef _DEBUG_USB_COMMS_
301 DEBUG("read buffer (%i retries): %i bytes", 100 - timeout, ftd2xx_buffer_size);
302 ftd2xx_debug_dump_buffer();
306 ftd2xx_expect_read = 0;
307 ftd2xx_read_pointer = 0;
315 type = jtag_scan_type(cmd->cmd.scan);
316 if (type != SCAN_OUT)
318 scan_size = jtag_scan_size(cmd->cmd.scan);
319 buffer = calloc(CEIL(scan_size, 8), 1);
320 ftd2xx_read_scan(type, buffer, scan_size);
321 jtag_read_buffer(buffer, cmd->cmd.scan);
331 ftd2xx_buffer_size = 0;
336 void ftd2xx_add_pathmove(pathmove_command_t *cmd)
338 int num_states = cmd->num_states;
348 /* command "Clock Data to TMS/CS Pin (no Read)" */
350 /* number of states remaining */
351 BUFFER_ADD = (num_states % 7) - 1;
353 while (num_states % 7)
355 if (tap_transitions[cur_state].low == cmd->path[state_count])
356 buf_set_u32(&tms_byte, bit_count++, 1, 0x0);
357 else if (tap_transitions[cur_state].high == cmd->path[state_count])
358 buf_set_u32(&tms_byte, bit_count++, 1, 0x1);
361 ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_strings[cur_state], tap_state_strings[cmd->path[state_count]]);
365 cur_state = cmd->path[state_count];
370 BUFFER_ADD = tms_byte;
373 end_state = cur_state;
376 void ftd2xx_add_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
378 int num_bytes = (scan_size + 7) / 8;
379 int bits_left = scan_size;
383 if ((!ir_scan && (cur_state != TAP_SD)) || (ir_scan && (cur_state != TAP_SI)))
385 /* command "Clock Data to TMS/CS Pin (no Read)" */
392 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SI);
397 BUFFER_ADD = TAP_MOVE(cur_state, TAP_SD);
400 //DEBUG("added TMS scan (no read)");
403 /* add command for complete bytes */
408 /* Clock Data Bytes In and Out LSB First */
410 //DEBUG("added TDI bytes (io %i)", num_bytes);
412 else if (type == SCAN_OUT)
414 /* Clock Data Bytes Out on -ve Clock Edge LSB First (no Read) */
416 //DEBUG("added TDI bytes (o)");
418 else if (type == SCAN_IN)
420 /* Clock Data Bytes In on +ve Clock Edge LSB First (no Write) */
422 //DEBUG("added TDI bytes (i %i)", num_bytes);
424 BUFFER_ADD = (num_bytes-2) & 0xff;
425 BUFFER_ADD = ((num_bytes-2) >> 8) & 0xff;
429 /* add complete bytes */
430 while(num_bytes-- > 1)
432 BUFFER_ADD = buffer[cur_byte];
439 bits_left -= 8 * (num_bytes - 1);
442 /* the most signifcant bit is scanned during TAP movement */
444 last_bit = (buffer[cur_byte] >> (bits_left - 1)) & 0x1;
448 /* process remaining bits but the last one */
453 /* Clock Data Bits In and Out LSB First */
455 //DEBUG("added TDI bits (io) %i", bits_left - 1);
457 else if (type == SCAN_OUT)
459 /* Clock Data Bits Out on -ve Clock Edge LSB First (no Read) */
461 //DEBUG("added TDI bits (o)");
463 else if (type == SCAN_IN)
465 /* Clock Data Bits In on +ve Clock Edge LSB First (no Write) */
467 //DEBUG("added TDI bits (i %i)", bits_left - 1);
469 BUFFER_ADD = bits_left - 2;
471 BUFFER_ADD = buffer[cur_byte];
474 /* move from Shift-IR/DR to end state */
475 if (type != SCAN_OUT)
477 /* Clock Data to TMS/CS Pin with Read */
479 //DEBUG("added TMS scan (read)");
483 /* Clock Data to TMS/CS Pin (no Read) */
485 //DEBUG("added TMS scan (no read)");
488 BUFFER_ADD = TAP_MOVE(cur_state, end_state) | (last_bit << 7);
489 cur_state = end_state;
493 int ftd2xx_predict_scan_out(int scan_size, enum scan_type type)
495 int predicted_size = 3;
497 if (cur_state != TAP_SD)
500 if (type == SCAN_IN) /* only from device to host */
503 predicted_size += (CEIL(scan_size, 8) > 1) ? 3 : 0;
504 /* remaining bits - 1 (up to 7) */
505 predicted_size += ((scan_size - 1) % 8) ? 2 : 0;
507 else /* host to device, or bidirectional */
510 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) + 3 - 1) : 0;
511 /* remaining bits -1 (up to 7) */
512 predicted_size += ((scan_size - 1) % 8) ? 3 : 0;
515 return predicted_size;
518 int ftd2xx_predict_scan_in(int scan_size, enum scan_type type)
520 int predicted_size = 0;
522 if (type != SCAN_OUT)
525 predicted_size += (CEIL(scan_size, 8) > 1) ? (CEIL(scan_size, 8) - 1) : 0;
526 /* remaining bits - 1 */
527 predicted_size += ((scan_size - 1) % 8) ? 1 : 0;
528 /* last bit (from TMS scan) */
532 //DEBUG("scan_size: %i, predicted_size: %i", scan_size, predicted_size);
534 return predicted_size;
537 void usbjtag_reset(int trst, int srst)
542 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
543 low_direction |= nTRSTnOE; /* switch to output pin (output is low) */
545 low_output &= ~nTRST; /* switch output low */
549 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
550 low_direction &= ~nTRSTnOE; /* switch to input pin (high-Z + internal and external pullup) */
552 low_output |= nTRST; /* switch output high */
557 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
558 low_output &= ~nSRST; /* switch output low */
560 low_direction |= nSRSTnOE; /* switch to output pin (output is low) */
564 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
565 low_output |= nSRST; /* switch output high */
567 low_direction &= ~nSRSTnOE; /* switch to input pin (high-Z) */
570 /* command "set data bits low byte" */
572 BUFFER_ADD = low_output;
573 BUFFER_ADD = low_direction;
577 void jtagkey_reset(int trst, int srst)
582 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
583 high_output &= ~nTRSTnOE;
585 high_output &= ~nTRST;
589 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
590 high_output |= nTRSTnOE;
592 high_output |= nTRST;
597 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
598 high_output &= ~nSRST;
600 high_output &= ~nSRSTnOE;
604 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
605 high_output |= nSRST;
607 high_output |= nSRSTnOE;
610 /* command "set data bits high byte" */
612 BUFFER_ADD = high_output;
613 BUFFER_ADD = high_direction;
614 DEBUG("trst: %i, srst: %i, high_output: 0x%2.2x, high_direction: 0x%2.2x", trst, srst, high_output, high_direction);
617 int ftd2xx_execute_queue()
619 jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
620 jtag_command_t *first_unsent = cmd; /* next command that has to be sent */
622 int scan_size; /* size of IR or DR scan */
625 int predicted_size = 0;
626 int require_send = 0;
628 ftd2xx_buffer_size = 0;
629 ftd2xx_expect_read = 0;
636 if (cmd->cmd.end_state->end_state != -1)
637 ftd2xx_end_state(cmd->cmd.end_state->end_state);
640 /* only send the maximum buffer size that FT2232C can handle */
642 if (ftd2xx_buffer_size + predicted_size + 1 > FTD2XX_BUFFER_SIZE)
644 ftd2xx_send_and_recv(first_unsent, cmd);
649 layout->reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
652 #ifdef _DEBUG_JTAG_IO_
653 DEBUG("trst: %i, srst: %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
657 /* only send the maximum buffer size that FT2232C can handle */
659 if (cur_state != TAP_RTI)
661 predicted_size += 3 * CEIL(cmd->cmd.runtest->num_cycles, 7);
662 if ((cmd->cmd.runtest->end_state != -1) && (cmd->cmd.runtest->end_state != TAP_RTI))
664 if ((cmd->cmd.runtest->end_state == -1) && (end_state != TAP_RTI))
666 if (ftd2xx_buffer_size + predicted_size + 1 > FTD2XX_BUFFER_SIZE)
668 ftd2xx_send_and_recv(first_unsent, cmd);
672 if (cur_state != TAP_RTI)
674 /* command "Clock Data to TMS/CS Pin (no Read)" */
679 BUFFER_ADD = TAP_MOVE(cur_state, TAP_RTI);
683 i = cmd->cmd.runtest->num_cycles;
686 /* command "Clock Data to TMS/CS Pin (no Read)" */
689 BUFFER_ADD = (i > 7) ? 6 : (i - 1);
693 i -= (i > 7) ? 7 : i;
694 //DEBUG("added TMS scan (no read)");
696 if (cmd->cmd.runtest->end_state != -1)
697 ftd2xx_end_state(cmd->cmd.runtest->end_state);
698 if (cur_state != end_state)
700 /* command "Clock Data to TMS/CS Pin (no Read)" */
705 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
706 cur_state = end_state;
707 //DEBUG("added TMS scan (no read)");
710 #ifdef _DEBUG_JTAG_IO_
711 DEBUG("runtest: %i, end in %i", cmd->cmd.runtest->num_cycles, end_state);
715 /* only send the maximum buffer size that FT2232C can handle */
717 if (ftd2xx_buffer_size + predicted_size + 1 > FTD2XX_BUFFER_SIZE)
719 ftd2xx_send_and_recv(first_unsent, cmd);
723 if (cmd->cmd.statemove->end_state != -1)
724 ftd2xx_end_state(cmd->cmd.statemove->end_state);
725 /* command "Clock Data to TMS/CS Pin (no Read)" */
730 BUFFER_ADD = TAP_MOVE(cur_state, end_state);
731 //DEBUG("added TMS scan (no read)");
732 cur_state = end_state;
734 #ifdef _DEBUG_JTAG_IO_
735 DEBUG("statemove: %i", end_state);
739 /* only send the maximum buffer size that FT2232C can handle */
740 predicted_size = 3 * CEIL(cmd->cmd.pathmove->num_states, 7);
741 if (ftd2xx_buffer_size + predicted_size + 1 > FTD2XX_BUFFER_SIZE)
743 ftd2xx_send_and_recv(first_unsent, cmd);
747 ftd2xx_add_pathmove(cmd->cmd.pathmove);
749 #ifdef _DEBUG_JTAG_IO_
750 DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
754 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
755 type = jtag_scan_type(cmd->cmd.scan);
756 predicted_size = ftd2xx_predict_scan_out(scan_size, type);
757 if (ftd2xx_buffer_size + predicted_size + 1 > FTD2XX_BUFFER_SIZE)
759 DEBUG("ftd2xx buffer size reached, sending queued commands (first_unsent: %x, cmd: %x)", first_unsent, cmd);
760 ftd2xx_send_and_recv(first_unsent, cmd);
764 ftd2xx_expect_read += ftd2xx_predict_scan_in(scan_size, type);
765 //DEBUG("new read size: %i", ftd2xx_expect_read);
766 if (cmd->cmd.scan->end_state != -1)
767 ftd2xx_end_state(cmd->cmd.scan->end_state);
768 ftd2xx_add_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
772 #ifdef _DEBUG_JTAG_IO_
773 DEBUG("%s scan, %i bit, end in %i", (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size, end_state);
777 ftd2xx_send_and_recv(first_unsent, cmd);
778 first_unsent = cmd->next;
779 jtag_sleep(cmd->cmd.sleep->us);
780 #ifdef _DEBUG_JTAG_IO_
781 DEBUG("sleep %i usec", cmd->cmd.sleep->us);
785 ERROR("BUG: unknown JTAG command type encountered");
791 if (require_send > 0)
792 ftd2xx_send_and_recv(first_unsent, cmd);
797 int ftd2xx_init(void)
805 ftd2xx_layout_t *cur_layout = ftd2xx_layouts;
807 if ((ftd2xx_layout == NULL) || (ftd2xx_layout[0] == 0))
809 ftd2xx_layout = "usbjtag";
810 WARNING("No ftd2xx layout specified, using default 'usbjtag'");
813 while (cur_layout->name)
815 if (strcmp(cur_layout->name, ftd2xx_layout) == 0)
825 ERROR("No matching layout found for %s", ftd2xx_layout);
826 return ERROR_JTAG_INIT_FAILED;
829 if (ftd2xx_device_desc == NULL)
831 WARNING("no ftd2xx device description specified, using default 'Dual RS232'");
832 ftd2xx_device_desc = "Dual RS232";
836 /* Add JTAGkey Vid/Pid to the linux driver */
837 if ((status = FT_SetVIDPID(ftd2xx_vid, ftd2xx_pid)) != FT_OK)
839 WARNING("couldn't add %4.4x:%4.4x", ftd2xx_vid, ftd2xx_pid);
843 if ((status = FT_OpenEx(ftd2xx_device_desc, FT_OPEN_BY_DESCRIPTION, &ftdih)) != FT_OK)
845 ERROR("unable to open ftdi device: %i", status);
846 status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
849 char **desc_array = malloc(sizeof(char*) * (num_devices + 1));
852 for (i = 0; i < num_devices; i++)
853 desc_array[i] = malloc(64);
854 desc_array[num_devices] = NULL;
856 status = FT_ListDevices(desc_array, &num_devices, FT_LIST_ALL | FT_OPEN_BY_DESCRIPTION);
860 ERROR("ListDevices: %d\n", num_devices);
861 for (i = 0; i < num_devices; i++)
862 ERROR("%i: %s", i, desc_array[i]);
865 for (i = 0; i < num_devices; i++)
871 printf("ListDevices: NONE\n");
873 return ERROR_JTAG_INIT_FAILED;
876 if ((status = FT_SetLatencyTimer(ftdih, 2)) != FT_OK)
878 ERROR("unable to set latency timer: %i", status);
879 return ERROR_JTAG_INIT_FAILED;
882 if ((status = FT_GetLatencyTimer(ftdih, &latency_timer)) != FT_OK)
884 ERROR("unable to get latency timer: %i", status);
885 return ERROR_JTAG_INIT_FAILED;
889 DEBUG("current latency timer: %i", latency_timer);
892 if ((status = FT_SetBitMode(ftdih, 0x0b, 2)) != FT_OK)
894 ERROR("unable to enable bit i/o mode: %i", status);
895 return ERROR_JTAG_INIT_FAILED;
898 ftd2xx_buffer_size = 0;
899 ftd2xx_buffer = malloc(FTD2XX_BUFFER_SIZE);
901 if (layout->init() != ERROR_OK)
902 return ERROR_JTAG_INIT_FAILED;
904 ftd2xx_speed(jtag_speed);
906 buf[0] = 0x85; /* Disconnect TDI/DO to TDO/DI for Loopback */
907 if (((status = FT_Write(ftdih, buf, 1, &bytes_written)) != FT_OK) || (bytes_written != 1))
909 ERROR("couldn't write to ftdi device: %i", status);
910 return ERROR_JTAG_INIT_FAILED;
913 if ((status = FT_Purge(ftdih, FT_PURGE_RX | FT_PURGE_TX)) != FT_OK)
915 ERROR("error purging ftd2xx device: %i", status);
916 return ERROR_JTAG_INIT_FAILED;
922 int usbjtag_init(void)
929 low_direction = 0x0b;
936 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
938 low_direction &= ~nTRSTnOE; /* nTRST input */
939 low_output &= ~nTRST; /* nTRST = 0 */
943 low_direction |= nTRSTnOE; /* nTRST output */
944 low_output |= nTRST; /* nTRST = 1 */
947 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
949 low_direction |= nSRSTnOE; /* nSRST output */
950 low_output |= nSRST; /* nSRST = 1 */
954 low_direction &= ~nSRSTnOE; /* nSRST input */
955 low_output &= ~nSRST; /* nSRST = 0 */
958 /* initialize low byte for jtag */
959 buf[0] = 0x80; /* command "set data bits low byte" */
960 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, xRST high) */
961 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in */
962 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
964 if (((FT_Write(ftdih, buf, 3, &bytes_written)) != FT_OK) || (bytes_written != 3))
966 ERROR("couldn't write to ftdi device: %i", status);
967 return ERROR_JTAG_INIT_FAILED;
973 int jtagkey_init(void)
980 low_direction = 0x1b;
982 /* initialize low byte for jtag */
983 buf[0] = 0x80; /* command "set data bits low byte" */
984 buf[1] = low_output; /* value (TMS=1,TCK=0, TDI=0, nOE=0) */
985 buf[2] = low_direction; /* dir (output=1), TCK/TDI/TMS=out, TDO=in, nOE=out */
986 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
988 if (((FT_Write(ftdih, buf, 3, &bytes_written)) != FT_OK) || (bytes_written != 3))
990 ERROR("couldn't write to ftdi device: %i", status);
991 return ERROR_JTAG_INIT_FAILED;
994 if (strcmp(layout->name, "jtagkey") == 0)
1001 else if (strcmp(layout->name, "jtagkey_prototype_v1") == 0)
1010 ERROR("BUG: jtagkey_init called for non jtagkey layout");
1015 high_direction = 0x0f;
1017 if (jtag_reset_config & RESET_TRST_OPEN_DRAIN)
1019 high_output |= nTRSTnOE;
1020 high_output &= ~nTRST;
1024 high_output &= ~nTRSTnOE;
1025 high_output |= nTRST;
1028 if (jtag_reset_config & RESET_SRST_PUSH_PULL)
1030 high_output &= ~nSRSTnOE;
1031 high_output |= nSRST;
1035 high_output |= nSRSTnOE;
1036 high_output &= ~nSRST;
1039 /* initialize high port */
1040 buf[0] = 0x82; /* command "set data bits low byte" */
1041 buf[1] = high_output; /* value */
1042 buf[2] = high_direction; /* all outputs (xRST and xRSTnOE) */
1043 DEBUG("%2.2x %2.2x %2.2x", buf[0], buf[1], buf[2]);
1045 if (((FT_Write(ftdih, buf, 3, &bytes_written)) != FT_OK) || (bytes_written != 3))
1047 ERROR("couldn't write to ftdi device: %i", status);
1048 return ERROR_JTAG_INIT_FAILED;
1054 int ftd2xx_quit(void)
1058 status = FT_Close(ftdih);
1060 free(ftd2xx_buffer);
1065 int ftd2xx_handle_device_desc_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1069 ftd2xx_device_desc = strdup(args[0]);
1073 ERROR("expected exactly one argument to ftd2xx_device_desc <description>");
1079 int ftd2xx_handle_layout_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1084 ftd2xx_layout = malloc(strlen(args[0]) + 1);
1085 strcpy(ftd2xx_layout, args[0]);
1090 int ftd2xx_handle_vid_pid_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
1094 ftd2xx_vid = strtol(args[0], NULL, 0);
1095 ftd2xx_pid = strtol(args[1], NULL, 0);
1099 WARNING("incomplete ftd2xx_vid_pid configuration directive");