2 * Driver for USB-JTAG, Altera USB-Blaster and compatibles
4 * Inspired from original code from Kolja Waschk's USB-JTAG project
5 * (http://www.ixo.de/info/usb_jtag/), and from openocd project.
7 * Copyright (C) 2012 Robert Jarzmik robert.jarzmik@free.fr
8 * Copyright (C) 2011 Ali Lown ali@lown.me.uk
9 * Copyright (C) 2009 Catalin Patulea cat@vv.carleton.ca
10 * Copyright (C) 2006 Kolja Waschk usbjtag@ixo.de
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 #include <jtag/interface.h>
31 #include <jtag/commands.h>
33 #include "ublast_access.h"
36 #include "jtag/drivers/ftd2xx_common.h"
38 static FT_HANDLE
*ublast_getftdih(struct ublast_lowlevel
*low
)
43 static int ublast_ftd2xx_write(struct ublast_lowlevel
*low
, uint8_t *buf
, int size
,
44 uint32_t *bytes_written
)
47 DWORD dw_bytes_written
;
48 FT_HANDLE
*ftdih
= ublast_getftdih(low
);
50 status
= FT_Write(*ftdih
, buf
, size
, &dw_bytes_written
);
51 if (status
!= FT_OK
) {
52 *bytes_written
= dw_bytes_written
;
53 LOG_ERROR("FT_Write returned: %s", ftd2xx_status_string(status
));
54 return ERROR_JTAG_DEVICE_ERROR
;
56 *bytes_written
= dw_bytes_written
;
60 static int ublast_ftd2xx_read(struct ublast_lowlevel
*low
, uint8_t *buf
,
61 unsigned size
, uint32_t *bytes_read
)
65 FT_HANDLE
*ftdih
= ublast_getftdih(low
);
67 status
= FT_Read(*ftdih
, buf
, size
, &dw_bytes_read
);
68 if (status
!= FT_OK
) {
69 *bytes_read
= dw_bytes_read
;
70 LOG_ERROR("FT_Read returned: %s", ftd2xx_status_string(status
));
71 return ERROR_JTAG_DEVICE_ERROR
;
73 *bytes_read
= dw_bytes_read
;
77 static int ublast_ftd2xx_init(struct ublast_lowlevel
*low
)
80 FT_HANDLE
*ftdih
= ublast_getftdih(low
);
81 uint8_t latency_timer
;
83 LOG_INFO("usb blaster interface using FTD2XX");
84 /* Open by device description */
85 if (low
->ublast_device_desc
== NULL
) {
86 LOG_WARNING("no usb blaster device description specified, "
87 "using default 'USB-Blaster'");
88 low
->ublast_device_desc
= "USB-Blaster";
92 /* Add non-standard Vid/Pid to the linux driver */
93 status
= FT_SetVIDPID(low
->ublast_vid
, low
->ublast_pid
);
94 if (status
!= FT_OK
) {
95 LOG_WARNING("couldn't add %4.4x:%4.4x",
96 low
->ublast_vid
, low
->ublast_pid
);
99 status
= FT_OpenEx(low
->ublast_device_desc
, FT_OPEN_BY_DESCRIPTION
,
101 if (status
!= FT_OK
) {
104 LOG_ERROR("unable to open ftdi device: %s",
105 ftd2xx_status_string(status
));
106 status
= FT_ListDevices(&num_devices
, NULL
, FT_LIST_NUMBER_ONLY
);
107 if (status
== FT_OK
) {
109 malloc(sizeof(char *) * (num_devices
+ 1));
112 for (i
= 0; i
< num_devices
; i
++)
113 desc_array
[i
] = malloc(64);
114 desc_array
[num_devices
] = NULL
;
116 status
= FT_ListDevices(desc_array
, &num_devices
,
117 FT_LIST_ALL
| FT_OPEN_BY_DESCRIPTION
);
119 if (status
== FT_OK
) {
120 LOG_ERROR("ListDevices: %" PRIu32
, (uint32_t)num_devices
);
121 for (i
= 0; i
< num_devices
; i
++)
122 LOG_ERROR("%i: %s", i
, desc_array
[i
]);
125 for (i
= 0; i
< num_devices
; i
++)
129 printf("ListDevices: NONE\n");
131 return ERROR_JTAG_INIT_FAILED
;
134 status
= FT_SetLatencyTimer(*ftdih
, 2);
135 if (status
!= FT_OK
) {
136 LOG_ERROR("unable to set latency timer: %s",
137 ftd2xx_status_string(status
));
138 return ERROR_JTAG_INIT_FAILED
;
141 status
= FT_GetLatencyTimer(*ftdih
, &latency_timer
);
143 LOG_ERROR("unable to get latency timer: %s",
144 ftd2xx_status_string(status
));
146 LOG_DEBUG("current latency timer: %i", latency_timer
);
148 status
= FT_SetBitMode(*ftdih
, 0x00, 0);
149 if (status
!= FT_OK
) {
150 LOG_ERROR("unable to disable bit i/o mode: %s",
151 ftd2xx_status_string(status
));
152 return ERROR_JTAG_INIT_FAILED
;
157 static int ublast_ftd2xx_quit(struct ublast_lowlevel
*low
)
159 FT_HANDLE
*ftdih
= ublast_getftdih(low
);
165 static struct ublast_lowlevel_priv
{
169 static struct ublast_lowlevel low
= {
170 .open
= ublast_ftd2xx_init
,
171 .close
= ublast_ftd2xx_quit
,
172 .read
= ublast_ftd2xx_read
,
173 .write
= ublast_ftd2xx_write
,
177 struct ublast_lowlevel
*ublast_register_ftd2xx(void)