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.
27 #include <jtag/interface.h>
28 #include <jtag/commands.h>
30 #include "ublast_access.h"
33 static struct ftdi_context
*ublast_getftdic(struct ublast_lowlevel
*low
)
38 static int ublast_ftdi_read(struct ublast_lowlevel
*low
, uint8_t *buf
,
39 unsigned size
, uint32_t *bytes_read
)
43 struct ftdi_context
*ftdic
= ublast_getftdic(low
);
46 while ((*bytes_read
< size
) && timeout
--) {
47 retval
= ftdi_read_data(ftdic
, buf
+ *bytes_read
,
51 LOG_ERROR("ftdi_read_data: %s",
52 ftdi_get_error_string(ftdic
));
53 return ERROR_JTAG_DEVICE_ERROR
;
55 *bytes_read
+= retval
;
60 static int ublast_ftdi_write(struct ublast_lowlevel
*low
, uint8_t *buf
, int size
,
61 uint32_t *bytes_written
)
64 struct ftdi_context
*ftdic
= ublast_getftdic(low
);
66 retval
= ftdi_write_data(ftdic
, buf
, size
);
69 LOG_ERROR("ftdi_write_data: %s",
70 ftdi_get_error_string(ftdic
));
71 return ERROR_JTAG_DEVICE_ERROR
;
73 *bytes_written
= retval
;
77 static int ublast_ftdi_init(struct ublast_lowlevel
*low
)
79 uint8_t latency_timer
;
80 struct ftdi_context
*ftdic
= ublast_getftdic(low
);
82 LOG_INFO("usb blaster interface using libftdi");
83 if (ftdi_init(ftdic
) < 0)
84 return ERROR_JTAG_INIT_FAILED
;
86 /* context, vendor id, product id */
87 if (ftdi_usb_open(ftdic
, low
->ublast_vid
, low
->ublast_pid
) < 0) {
88 LOG_ERROR("unable to open ftdi device: %s", ftdic
->error_str
);
89 return ERROR_JTAG_INIT_FAILED
;
92 if (ftdi_usb_reset(ftdic
) < 0) {
93 LOG_ERROR("unable to reset ftdi device");
94 return ERROR_JTAG_INIT_FAILED
;
97 if (ftdi_set_latency_timer(ftdic
, 2) < 0) {
98 LOG_ERROR("unable to set latency timer");
99 return ERROR_JTAG_INIT_FAILED
;
102 if (ftdi_get_latency_timer(ftdic
, &latency_timer
) < 0)
103 LOG_ERROR("unable to get latency timer");
105 LOG_DEBUG("current latency timer: %u", latency_timer
);
107 ftdi_disable_bitbang(ftdic
);
111 static int ublast_ftdi_quit(struct ublast_lowlevel
*low
)
113 struct ftdi_context
*ftdic
= ublast_getftdic(low
);
115 ftdi_usb_close(ftdic
);
120 static struct ublast_lowlevel_priv
{
121 struct ftdi_context ftdic
;
124 static struct ublast_lowlevel low
= {
125 .open
= ublast_ftdi_init
,
126 .close
= ublast_ftdi_quit
,
127 .read
= ublast_ftdi_read
,
128 .write
= ublast_ftdi_write
,
132 struct ublast_lowlevel
*ublast_register_ftdi(void)