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 <libusb_common.h>
27 #include <target/image.h>
29 #include "ublast_access.h"
31 #define USBBLASTER_CTRL_READ_REV 0x94
32 #define USBBLASTER_CTRL_LOAD_FIRM 0xA0
33 #define USBBLASTER_EPOUT 4
34 #define USBBLASTER_EPIN 8
36 #define EZUSB_CPUCS 0xe600
39 /** Maximum size of a single firmware section. Entire EZ-USB code space = 16kB */
40 #define SECTION_BUFFERSIZE 16384
42 static int ublast2_libusb_read(struct ublast_lowlevel
*low
, uint8_t *buf
,
43 unsigned size
, uint32_t *bytes_read
)
47 ret
= jtag_libusb_bulk_read(low
->libusb_dev
,
58 static int ublast2_libusb_write(struct ublast_lowlevel
*low
, uint8_t *buf
,
59 int size
, uint32_t *bytes_written
)
63 ret
= jtag_libusb_bulk_write(low
->libusb_dev
,
75 static int ublast2_write_firmware_section(struct libusb_device_handle
*libusb_dev
,
76 struct image
*firmware_image
, int section_index
)
79 uint8_t data
[SECTION_BUFFERSIZE
];
80 uint8_t *data_ptr
= data
;
83 uint16_t size
= (uint16_t)firmware_image
->sections
[section_index
].size
;
84 uint16_t addr
= (uint16_t)firmware_image
->sections
[section_index
].base_address
;
86 LOG_DEBUG("section %02i at addr 0x%04x (size 0x%04x)", section_index
, addr
,
89 /* Copy section contents to local buffer */
90 int ret
= image_read_section(firmware_image
, section_index
, 0, size
, data
,
93 if ((ret
!= ERROR_OK
) || (size_read
!= size
)) {
94 /* Propagating the return code would return '0' (misleadingly indicating
95 * successful execution of the function) if only the size check fails. */
99 uint16_t bytes_remaining
= size
;
101 /* Send section data in chunks of up to 64 bytes to ULINK */
102 while (bytes_remaining
> 0) {
103 if (bytes_remaining
> 64)
106 chunk_size
= bytes_remaining
;
108 jtag_libusb_control_transfer(libusb_dev
,
109 LIBUSB_REQUEST_TYPE_VENDOR
| \
111 USBBLASTER_CTRL_LOAD_FIRM
,
118 bytes_remaining
-= chunk_size
;
120 data_ptr
+= chunk_size
;
126 static int load_usb_blaster_firmware(struct libusb_device_handle
*libusb_dev
,
127 struct ublast_lowlevel
*low
)
129 struct image ublast2_firmware_image
;
131 if (!low
->firmware_path
) {
132 LOG_ERROR("No firmware path specified");
136 ublast2_firmware_image
.base_address
= 0;
137 ublast2_firmware_image
.base_address_set
= 0;
139 int ret
= image_open(&ublast2_firmware_image
, low
->firmware_path
, "ihex");
140 if (ret
!= ERROR_OK
) {
141 LOG_ERROR("Could not load firmware image");
145 /** A host loader program must write 0x01 to the CPUCS register
146 * to put the CPU into RESET, load all or part of the EZUSB
147 * RAM with firmware, then reload the CPUCS register
148 * with ‘0’ to take the CPU out of RESET. The CPUCS register
149 * (at 0xE600) is the only EZ-USB register that can be written
150 * using the Firmware Download command.
153 char value
= CPU_RESET
;
154 jtag_libusb_control_transfer(libusb_dev
,
155 LIBUSB_REQUEST_TYPE_VENDOR
| \
157 USBBLASTER_CTRL_LOAD_FIRM
,
164 /* Download all sections in the image to ULINK */
165 for (int i
= 0; i
< ublast2_firmware_image
.num_sections
; i
++) {
166 ret
= ublast2_write_firmware_section(libusb_dev
,
167 &ublast2_firmware_image
, i
);
168 if (ret
!= ERROR_OK
) {
169 LOG_ERROR("Error while downloading the firmware");
175 jtag_libusb_control_transfer(libusb_dev
,
176 LIBUSB_REQUEST_TYPE_VENDOR
| \
178 USBBLASTER_CTRL_LOAD_FIRM
,
185 image_close(&ublast2_firmware_image
);
190 static int ublast2_libusb_init(struct ublast_lowlevel
*low
)
192 const uint16_t vids
[] = { low
->ublast_vid_uninit
, 0 };
193 const uint16_t pids
[] = { low
->ublast_pid_uninit
, 0 };
194 struct libusb_device_handle
*temp
;
195 bool renumeration
= false;
198 if (jtag_libusb_open(vids
, pids
, NULL
, &temp
) == ERROR_OK
) {
199 LOG_INFO("Altera USB-Blaster II (uninitialized) found");
200 LOG_INFO("Loading firmware...");
201 ret
= load_usb_blaster_firmware(temp
, low
);
202 jtag_libusb_close(temp
);
208 const uint16_t vids_renum
[] = { low
->ublast_vid
, 0 };
209 const uint16_t pids_renum
[] = { low
->ublast_pid
, 0 };
211 if (renumeration
== false) {
212 if (jtag_libusb_open(vids_renum
, pids_renum
, NULL
, &low
->libusb_dev
) != ERROR_OK
) {
213 LOG_ERROR("Altera USB-Blaster II not found");
218 while (jtag_libusb_open(vids_renum
, pids_renum
, NULL
, &low
->libusb_dev
) != ERROR_OK
&& retry
--) {
220 LOG_INFO("Waiting for renumerate...");
224 LOG_ERROR("Altera USB-Blaster II not found");
230 jtag_libusb_control_transfer(low
->libusb_dev
,
231 LIBUSB_REQUEST_TYPE_VENDOR
| \
233 USBBLASTER_CTRL_READ_REV
,
240 LOG_INFO("Altera USB-Blaster II found (Firm. rev. = %s)", buffer
);
245 static int ublast2_libusb_quit(struct ublast_lowlevel
*low
)
247 jtag_libusb_close(low
->libusb_dev
);
251 static struct ublast_lowlevel low
= {
252 .open
= ublast2_libusb_init
,
253 .close
= ublast2_libusb_quit
,
254 .read
= ublast2_libusb_read
,
255 .write
= ublast2_libusb_write
,
256 .flags
= COPY_TDO_BUFFER
,
259 struct ublast_lowlevel
*ublast2_register_libusb(void)
Linking to existing account procedure
If you already have an account and want to add another login method
you
MUST first sign in with your existing account and
then change URL to read
https://review.openocd.org/login/?link
to get to this page again but this time it'll work for linking. Thank you.
SSH host keys fingerprints
1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=.. |
|+o.. . |
|*.o . . |
|+B . . . |
|Bo. = o S |
|Oo.+ + = |
|oB=.* = . o |
| =+=.+ + E |
|. .=o . o |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)