aboutsummaryrefslogtreecommitdiff
path: root/src/jtag/drivers/usb_blaster/ublast_access_ftd2xx.c
blob: 14351fbd439e1c38f7176a89e7c75985b4c6c0df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
 *   Driver for USB-JTAG, Altera USB-Blaster and compatibles
 *
 *   Inspired from original code from Kolja Waschk's USB-JTAG project
 *   (http://www.ixo.de/info/usb_jtag/), and from openocd project.
 *
 *   Copyright (C) 2012 Robert Jarzmik robert.jarzmik@free.fr
 *   Copyright (C) 2011 Ali Lown ali@lown.me.uk
 *   Copyright (C) 2009 Catalin Patulea cat@vv.carleton.ca
 *   Copyright (C) 2006 Kolja Waschk usbjtag@ixo.de
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <jtag/interface.h>
#include <jtag/commands.h>

#include "ublast_access.h"

#include <ftd2xx.h>
#include "jtag/drivers/ftd2xx_common.h"

static FT_HANDLE *ublast_getftdih(struct ublast_lowlevel *low)
{
	return low->priv;
}

static int ublast_ftd2xx_write(struct ublast_lowlevel *low, uint8_t *buf, int size,
			      uint32_t *bytes_written)
{
	FT_STATUS status;
	DWORD dw_bytes_written;
	FT_HANDLE *ftdih = ublast_getftdih(low);

	status = FT_Write(*ftdih, buf, size, &dw_bytes_written);
	if (status != FT_OK) {
		*bytes_written = dw_bytes_written;
		LOG_ERROR("FT_Write returned: %s", ftd2xx_status_string(status));
		return ERROR_JTAG_DEVICE_ERROR;
	}
	*bytes_written = dw_bytes_written;
	return ERROR_OK;
}

static int ublast_ftd2xx_read(struct ublast_lowlevel *low, uint8_t *buf,
			     unsigned size, uint32_t *bytes_read)
{
	DWORD dw_bytes_read;
	FT_STATUS status;
	FT_HANDLE *ftdih = ublast_getftdih(low);

	status = FT_Read(*ftdih, buf, size, &dw_bytes_read);
	if (status != FT_OK) {
		*bytes_read = dw_bytes_read;
		LOG_ERROR("FT_Read returned: %s", ftd2xx_status_string(status));
		return ERROR_JTAG_DEVICE_ERROR;
	}
	*bytes_read = dw_bytes_read;
	return ERROR_OK;
}

static int ublast_ftd2xx_init(struct ublast_lowlevel *low)
{
	FT_STATUS status;
	FT_HANDLE *ftdih = ublast_getftdih(low);
	uint8_t latency_timer;

	LOG_INFO("usb blaster interface using FTD2XX");
	/* Open by device description */
	if (low->ublast_device_desc == NULL) {
		LOG_WARNING("no usb blaster device description specified, "
			    "using default 'USB-Blaster'");
		low->ublast_device_desc = "USB-Blaster";
	}

#if IS_WIN32 == 0
	/* Add non-standard Vid/Pid to the linux driver */
	status = FT_SetVIDPID(low->ublast_vid, low->ublast_pid);
	if (status != FT_OK) {
		LOG_WARNING("couldn't add %4.4x:%4.4x",
			    low->ublast_vid, low->ublast_pid);
	}
#endif
	status = FT_OpenEx(low->ublast_device_desc, FT_OPEN_BY_DESCRIPTION,
			   ftdih);
	if (status != FT_OK) {
		DWORD num_devices;

		LOG_ERROR("unable to open ftdi device: %s",
			  ftd2xx_status_string(status));
		status = FT_ListDevices(&num_devices, NULL, FT_LIST_NUMBER_ONLY);
		if (status == FT_OK) {
			char **desc_array =
				malloc(sizeof(char *) * (num_devices + 1));
			unsigned int i;

			for (i = 0; i < num_devices; i++)
				desc_array[i] = malloc(64);
			desc_array[num_devices] = NULL;

			status = FT_ListDevices(desc_array, &num_devices,
						FT_LIST_ALL | FT_OPEN_BY_DESCRIPTION);

			if (status == FT_OK) {
				LOG_ERROR("ListDevices: %" PRIu32, (uint32_t)num_devices);
				for (i = 0; i < num_devices; i++)
					LOG_ERROR("%i: %s", i, desc_array[i]);
			}

			for (i = 0; i < num_devices; i++)
				free(desc_array[i]);
			free(desc_array);
		} else {
			printf("ListDevices: NONE\n");
		}
		return ERROR_JTAG_INIT_FAILED;
	}

	status = FT_SetLatencyTimer(*ftdih, 2);
	if (status != FT_OK) {
		LOG_ERROR("unable to set latency timer: %s",
				ftd2xx_status_string(status));
		return ERROR_JTAG_INIT_FAILED;
	}

	status = FT_GetLatencyTimer(*ftdih, &latency_timer);
	if (status != FT_OK)
		LOG_ERROR("unable to get latency timer: %s",
				ftd2xx_status_string(status));
	else
		LOG_DEBUG("current latency timer: %i", latency_timer);

	status = FT_SetBitMode(*ftdih, 0x00, 0);
	if (status != FT_OK) {
		LOG_ERROR("unable to disable bit i/o mode: %s",
				ftd2xx_status_string(status));
		return ERROR_JTAG_INIT_FAILED;
	}
	return ERROR_OK;
}

static int ublast_ftd2xx_quit(struct ublast_lowlevel *low)
{
	FT_HANDLE *ftdih = ublast_getftdih(low);

	FT_Close(*ftdih);
	return ERROR_OK;
}

static struct ublast_lowlevel_priv {
	FT_HANDLE ftdih;
} info;

static struct ublast_lowlevel low = {
	.open = ublast_ftd2xx_init,
	.close = ublast_ftd2xx_quit,
	.read = ublast_ftd2xx_read,
	.write = ublast_ftd2xx_write,
	.priv = &info,
};

struct ublast_lowlevel *ublast_register_ftd2xx(void)
{
	return &low;
}