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.
21 #include <jtag/interface.h>
22 #include <jtag/commands.h>
23 #include <libusb_common.h>
24 #include <target/image.h>
26 #include "ublast_access.h"
28 #define USBBLASTER_CTRL_READ_REV 0x94
29 #define USBBLASTER_CTRL_LOAD_FIRM 0xA0
30 #define USBBLASTER_EPOUT 4
31 #define USBBLASTER_EPIN 8
33 #define EZUSB_CPUCS 0xe600
36 /** Maximum size of a single firmware section. Entire EZ-USB code space = 16kB */
37 #define SECTION_BUFFERSIZE 16384
39 static int ublast2_libusb_read(struct ublast_lowlevel *low, uint8_t *buf,
40 unsigned size, uint32_t *bytes_read)
42 *bytes_read = jtag_libusb_bulk_read(low->libusb_dev,
51 static int ublast2_libusb_write(struct ublast_lowlevel *low, uint8_t *buf,
52 int size, uint32_t *bytes_written)
54 *bytes_written = jtag_libusb_bulk_write(low->libusb_dev,
63 static int ublast2_write_firmware_section(struct jtag_libusb_device_handle *libusb_dev,
64 struct image *firmware_image, int section_index)
67 uint8_t data[SECTION_BUFFERSIZE];
68 uint8_t *data_ptr = data;
71 uint16_t size = (uint16_t)firmware_image->sections[section_index].size;
72 uint16_t addr = (uint16_t)firmware_image->sections[section_index].base_address;
74 LOG_DEBUG("section %02i at addr 0x%04x (size 0x%04x)", section_index, addr,
80 /* Copy section contents to local buffer */
81 int ret = image_read_section(firmware_image, section_index, 0, size, data,
84 if ((ret != ERROR_OK) || (size_read != size)) {
85 /* Propagating the return code would return '0' (misleadingly indicating
86 * successful execution of the function) if only the size check fails. */
90 uint16_t bytes_remaining = size;
92 /* Send section data in chunks of up to 64 bytes to ULINK */
93 while (bytes_remaining > 0) {
94 if (bytes_remaining > 64)
97 chunk_size = bytes_remaining;
99 jtag_libusb_control_transfer(libusb_dev,
100 LIBUSB_REQUEST_TYPE_VENDOR | \
102 USBBLASTER_CTRL_LOAD_FIRM,
109 bytes_remaining -= chunk_size;
111 data_ptr += chunk_size;
117 static int load_usb_blaster_firmware(struct jtag_libusb_device_handle *libusb_dev,
118 struct ublast_lowlevel *low)
120 struct image ublast2_firmware_image;
122 if (!low->firmware_path) {
123 LOG_ERROR("No firmware path specified");
127 ublast2_firmware_image.base_address = 0;
128 ublast2_firmware_image.base_address_set = 0;
130 int ret = image_open(&ublast2_firmware_image, low->firmware_path, "ihex");
131 if (ret != ERROR_OK) {
132 LOG_ERROR("Could not load firmware image");
136 /** A host loader program must write 0x01 to the CPUCS register
137 * to put the CPU into RESET, load all or part of the EZUSB
138 * RAM with firmware, then reload the CPUCS register
139 * with ‘0’ to take the CPU out of RESET. The CPUCS register
140 * (at 0xE600) is the only EZ-USB register that can be written
141 * using the Firmware Download command.
144 char value = CPU_RESET;
145 jtag_libusb_control_transfer(libusb_dev,
146 LIBUSB_REQUEST_TYPE_VENDOR | \
148 USBBLASTER_CTRL_LOAD_FIRM,
155 /* Download all sections in the image to ULINK */
156 for (int i = 0; i < ublast2_firmware_image.num_sections; i++) {
157 ret = ublast2_write_firmware_section(libusb_dev,
158 &ublast2_firmware_image, i);
159 if (ret != ERROR_OK) {
160 LOG_ERROR("Error while downloading the firmware");
166 jtag_libusb_control_transfer(libusb_dev,
167 LIBUSB_REQUEST_TYPE_VENDOR | \
169 USBBLASTER_CTRL_LOAD_FIRM,
176 image_close(&ublast2_firmware_image);
181 static int ublast2_libusb_init(struct ublast_lowlevel *low)
183 const uint16_t vids[] = { low->ublast_vid_uninit, 0 };
184 const uint16_t pids[] = { low->ublast_pid_uninit, 0 };
185 struct jtag_libusb_device_handle *temp;
186 bool renumeration = false;
189 if (jtag_libusb_open(vids, pids, &temp) == ERROR_OK) {
190 LOG_INFO("Altera USB-Blaster II (uninitialized) found");
191 LOG_INFO("Loading firmware...");
192 ret = load_usb_blaster_firmware(temp, low);
193 jtag_libusb_close(temp);
199 const uint16_t vids_renum[] = { low->ublast_vid, 0 };
200 const uint16_t pids_renum[] = { low->ublast_pid, 0 };
202 if (renumeration == false) {
203 if (jtag_libusb_open(vids_renum, pids_renum, &low->libusb_dev) != ERROR_OK) {
204 LOG_ERROR("Altera USB-Blaster II not found");
209 while (jtag_libusb_open(vids_renum, pids_renum, &low->libusb_dev) != ERROR_OK && retry--) {
211 LOG_INFO("Waiting for renumerate...");
215 LOG_ERROR("Altera USB-Blaster II not found");
221 jtag_libusb_control_transfer(low->libusb_dev,
222 LIBUSB_REQUEST_TYPE_VENDOR | \
224 USBBLASTER_CTRL_READ_REV,
231 LOG_INFO("Altera USB-Blaster II found (Firm. rev. = %s)", buffer);
236 static int ublast2_libusb_quit(struct ublast_lowlevel *low)
238 jtag_libusb_close(low->libusb_dev);
242 static struct ublast_lowlevel low = {
243 .open = ublast2_libusb_init,
244 .close = ublast2_libusb_quit,
245 .read = ublast2_libusb_read,
246 .write = ublast2_libusb_write,
247 .flags = COPY_TDO_BUFFER,
250 struct ublast_lowlevel *ublast2_register_libusb(void)