ffcf312e604e8c02ce141cbed3f711e7c0469863
[openocd.git] / src / jtag / drivers / usb_blaster / ublast_access_ftd2xx.c
1 /*
2 * Driver for USB-JTAG, Altera USB-Blaster and compatibles
3 *
4 * Inspired from original code from Kolja Waschk's USB-JTAG project
5 * (http://www.ixo.de/info/usb_jtag/), and from openocd project.
6 *
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
11 *
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.
16 *
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.
21 *
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/>.
24 *
25 */
26
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
30 #include <jtag/interface.h>
31 #include <jtag/commands.h>
32
33 #include "ublast_access.h"
34
35 #include <ftd2xx.h>
36 #include "jtag/drivers/ftd2xx_common.h"
37
38 static FT_HANDLE *ublast_getftdih(struct ublast_lowlevel *low)
39 {
40 return low->priv;
41 }
42
43 static int ublast_ftd2xx_write(struct ublast_lowlevel *low, uint8_t *buf, int size,
44 uint32_t *bytes_written)
45 {
46 FT_STATUS status;
47 DWORD dw_bytes_written;
48 FT_HANDLE *ftdih = ublast_getftdih(low);
49
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;
55 }
56 *bytes_written = dw_bytes_written;
57 return ERROR_OK;
58 }
59
60 static int ublast_ftd2xx_read(struct ublast_lowlevel *low, uint8_t *buf,
61 unsigned size, uint32_t *bytes_read)
62 {
63 DWORD dw_bytes_read;
64 FT_STATUS status;
65 FT_HANDLE *ftdih = ublast_getftdih(low);
66
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;
72 }
73 *bytes_read = dw_bytes_read;
74 return ERROR_OK;
75 }
76
77 static int ublast_ftd2xx_init(struct ublast_lowlevel *low)
78 {
79 FT_STATUS status;
80 FT_HANDLE *ftdih = ublast_getftdih(low);
81 uint8_t latency_timer;
82
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";
89 }
90
91 #if IS_WIN32 == 0
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);
97 }
98 #endif
99 status = FT_OpenEx(low->ublast_device_desc, FT_OPEN_BY_DESCRIPTION,
100 ftdih);
101 if (status != FT_OK) {
102 DWORD num_devices;
103
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) {
108 char **desc_array =
109 malloc(sizeof(char *) * (num_devices + 1));
110 unsigned int i;
111
112 for (i = 0; i < num_devices; i++)
113 desc_array[i] = malloc(64);
114 desc_array[num_devices] = NULL;
115
116 status = FT_ListDevices(desc_array, &num_devices,
117 FT_LIST_ALL | FT_OPEN_BY_DESCRIPTION);
118
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]);
123 }
124
125 for (i = 0; i < num_devices; i++)
126 free(desc_array[i]);
127 free(desc_array);
128 } else {
129 printf("ListDevices: NONE\n");
130 }
131 return ERROR_JTAG_INIT_FAILED;
132 }
133
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;
139 }
140
141 status = FT_GetLatencyTimer(*ftdih, &latency_timer);
142 if (status != FT_OK)
143 LOG_ERROR("unable to get latency timer: %s",
144 ftd2xx_status_string(status));
145 else
146 LOG_DEBUG("current latency timer: %i", latency_timer);
147
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;
153 }
154 return ERROR_OK;
155 }
156
157 static int ublast_ftd2xx_quit(struct ublast_lowlevel *low)
158 {
159 FT_HANDLE *ftdih = ublast_getftdih(low);
160
161 FT_Close(*ftdih);
162 return ERROR_OK;
163 }
164
165 static struct ublast_lowlevel_priv {
166 FT_HANDLE ftdih;
167 } info;
168
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,
174 .priv = &info,
175 };
176
177 struct ublast_lowlevel *ublast_register_ftd2xx(void)
178 {
179 return &low;
180 }