2 * Driver for USB-JTAG, Altera USB-Blaster II and compatibles
4 * Copyright (C) 2013 Franck Jullien franck.jullien@gmail.com
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include <jtag/interface.h>
25 #include <jtag/commands.h>
26 #include "helper/system.h"
27 #include <libusb_helper.h>
28 #include <target/image.h>
30 #include "ublast_access.h"
32 #define USBBLASTER_CTRL_READ_REV 0x94
33 #define USBBLASTER_CTRL_LOAD_FIRM 0xA0
34 #define USBBLASTER_EPOUT 4
35 #define USBBLASTER_EPIN 8
37 #define EZUSB_CPUCS 0xe600
40 /** Maximum size of a single firmware section. Entire EZ-USB code space = 16kB */
41 #define SECTION_BUFFERSIZE 16384
43 static int ublast2_libusb_read(struct ublast_lowlevel *low, uint8_t *buf,
44 unsigned size, uint32_t *bytes_read)
48 ret = jtag_libusb_bulk_read(low->libusb_dev,
59 static int ublast2_libusb_write(struct ublast_lowlevel *low, uint8_t *buf,
60 int size, uint32_t *bytes_written)
64 ret = jtag_libusb_bulk_write(low->libusb_dev,
76 static int ublast2_write_firmware_section(struct libusb_device_handle *libusb_dev,
77 struct image *firmware_image, int section_index)
80 uint8_t data[SECTION_BUFFERSIZE];
81 uint8_t *data_ptr = data;
84 uint16_t size = (uint16_t)firmware_image->sections[section_index].size;
85 uint16_t addr = (uint16_t)firmware_image->sections[section_index].base_address;
87 LOG_DEBUG("section %02i at addr 0x%04x (size 0x%04x)", section_index, addr,
90 /* Copy section contents to local buffer */
91 int ret = image_read_section(firmware_image, section_index, 0, size, data,
94 if ((ret != ERROR_OK) || (size_read != size)) {
95 /* Propagating the return code would return '0' (misleadingly indicating
96 * successful execution of the function) if only the size check fails. */
100 uint16_t bytes_remaining = size;
102 /* Send section data in chunks of up to 64 bytes to ULINK */
103 while (bytes_remaining > 0) {
104 if (bytes_remaining > 64)
107 chunk_size = bytes_remaining;
109 jtag_libusb_control_transfer(libusb_dev,
110 LIBUSB_REQUEST_TYPE_VENDOR |
112 USBBLASTER_CTRL_LOAD_FIRM,
119 bytes_remaining -= chunk_size;
121 data_ptr += chunk_size;
127 static int load_usb_blaster_firmware(struct libusb_device_handle *libusb_dev,
128 struct ublast_lowlevel *low)
130 struct image ublast2_firmware_image;
132 if (!low->firmware_path) {
133 LOG_ERROR("No firmware path specified");
137 if (libusb_claim_interface(libusb_dev, 0)) {
138 LOG_ERROR("unable to claim interface");
139 return ERROR_JTAG_INIT_FAILED;
142 ublast2_firmware_image.base_address = 0;
143 ublast2_firmware_image.base_address_set = false;
145 int ret = image_open(&ublast2_firmware_image, low->firmware_path, "ihex");
146 if (ret != ERROR_OK) {
147 LOG_ERROR("Could not load firmware image");
148 goto error_release_usb;
151 /** A host loader program must write 0x01 to the CPUCS register
152 * to put the CPU into RESET, load all or part of the EZUSB
153 * RAM with firmware, then reload the CPUCS register
154 * with ‘0’ to take the CPU out of RESET. The CPUCS register
155 * (at 0xE600) is the only EZ-USB register that can be written
156 * using the Firmware Download command.
159 char value = CPU_RESET;
160 jtag_libusb_control_transfer(libusb_dev,
161 LIBUSB_REQUEST_TYPE_VENDOR |
163 USBBLASTER_CTRL_LOAD_FIRM,
170 /* Download all sections in the image to ULINK */
171 for (unsigned int i = 0; i < ublast2_firmware_image.num_sections; i++) {
172 ret = ublast2_write_firmware_section(libusb_dev,
173 &ublast2_firmware_image, i);
174 if (ret != ERROR_OK) {
175 LOG_ERROR("Error while downloading the firmware");
176 goto error_close_firmware;
181 jtag_libusb_control_transfer(libusb_dev,
182 LIBUSB_REQUEST_TYPE_VENDOR |
184 USBBLASTER_CTRL_LOAD_FIRM,
191 error_close_firmware:
192 image_close(&ublast2_firmware_image);
196 * Release claimed interface. Most probably it is already disconnected
197 * and re-enumerated as new devices after firmware upload, so we do
198 * not need to care about errors.
200 libusb_release_interface(libusb_dev, 0);
205 static int ublast2_libusb_init(struct ublast_lowlevel *low)
207 const uint16_t vids[] = { low->ublast_vid_uninit, 0 };
208 const uint16_t pids[] = { low->ublast_pid_uninit, 0 };
209 struct libusb_device_handle *temp;
210 bool renumeration = false;
213 if (jtag_libusb_open(vids, pids, NULL, &temp, NULL) == ERROR_OK) {
214 LOG_INFO("Altera USB-Blaster II (uninitialized) found");
215 LOG_INFO("Loading firmware...");
216 ret = load_usb_blaster_firmware(temp, low);
217 jtag_libusb_close(temp);
223 const uint16_t vids_renum[] = { low->ublast_vid, 0 };
224 const uint16_t pids_renum[] = { low->ublast_pid, 0 };
226 if (renumeration == false) {
227 if (jtag_libusb_open(vids_renum, pids_renum, NULL,
228 &low->libusb_dev, NULL) != ERROR_OK) {
229 LOG_ERROR("Altera USB-Blaster II not found");
234 while (jtag_libusb_open(vids_renum, pids_renum, NULL,
235 &low->libusb_dev, NULL) != ERROR_OK && retry--) {
237 LOG_INFO("Waiting for reenumerate...");
241 LOG_ERROR("Altera USB-Blaster II not found");
246 if (libusb_claim_interface(low->libusb_dev, 0)) {
247 LOG_ERROR("unable to claim interface");
248 jtag_libusb_close(low->libusb_dev);
249 return ERROR_JTAG_INIT_FAILED;
253 jtag_libusb_control_transfer(low->libusb_dev,
254 LIBUSB_REQUEST_TYPE_VENDOR |
256 USBBLASTER_CTRL_READ_REV,
263 LOG_INFO("Altera USB-Blaster II found (Firm. rev. = %s)", buffer);
268 static int ublast2_libusb_quit(struct ublast_lowlevel *low)
270 if (libusb_release_interface(low->libusb_dev, 0))
271 LOG_ERROR("usb release interface failed");
273 jtag_libusb_close(low->libusb_dev);
277 static struct ublast_lowlevel low = {
278 .open = ublast2_libusb_init,
279 .close = ublast2_libusb_quit,
280 .read = ublast2_libusb_read,
281 .write = ublast2_libusb_write,
282 .flags = COPY_TDO_BUFFER,
285 struct ublast_lowlevel *ublast2_register_libusb(void)