jtag/drivers: remove useless checks causing build failure with clang 3.5.0
[openocd.git] / src / jtag / drivers / usb_blaster / ublast2_access_libusb.c
1 /*
2 * Driver for USB-JTAG, Altera USB-Blaster II and compatibles
3 *
4 * Copyright (C) 2013 Franck Jullien franck.jullien@gmail.com
5 *
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.
10 *
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.
15 *
16 */
17
18 #ifdef HAVE_CONFIG_H
19 #include <config.h>
20 #endif
21 #include <jtag/interface.h>
22 #include <jtag/commands.h>
23 #include <libusb_common.h>
24 #include <target/image.h>
25
26 #include "ublast_access.h"
27
28 #define USBBLASTER_CTRL_READ_REV 0x94
29 #define USBBLASTER_CTRL_LOAD_FIRM 0xA0
30 #define USBBLASTER_EPOUT 4
31 #define USBBLASTER_EPIN 8
32
33 #define EZUSB_CPUCS 0xe600
34 #define CPU_RESET 1
35
36 /** Maximum size of a single firmware section. Entire EZ-USB code space = 16kB */
37 #define SECTION_BUFFERSIZE 16384
38
39 static int ublast2_libusb_read(struct ublast_lowlevel *low, uint8_t *buf,
40 unsigned size, uint32_t *bytes_read)
41 {
42 *bytes_read = jtag_libusb_bulk_read(low->libusb_dev,
43 USBBLASTER_EPIN | \
44 LIBUSB_ENDPOINT_IN,
45 (char *)buf,
46 size,
47 100);
48 return ERROR_OK;
49 }
50
51 static int ublast2_libusb_write(struct ublast_lowlevel *low, uint8_t *buf,
52 int size, uint32_t *bytes_written)
53 {
54 *bytes_written = jtag_libusb_bulk_write(low->libusb_dev,
55 USBBLASTER_EPOUT | \
56 LIBUSB_ENDPOINT_OUT,
57 (char *)buf,
58 size,
59 100);
60 return ERROR_OK;
61 }
62
63 static int ublast2_write_firmware_section(struct jtag_libusb_device_handle *libusb_dev,
64 struct image *firmware_image, int section_index)
65 {
66 uint16_t chunk_size;
67 uint8_t data[SECTION_BUFFERSIZE];
68 uint8_t *data_ptr = data;
69 size_t size_read;
70
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;
73
74 LOG_DEBUG("section %02i at addr 0x%04x (size 0x%04x)", section_index, addr,
75 size);
76
77 /* Copy section contents to local buffer */
78 int ret = image_read_section(firmware_image, section_index, 0, size, data,
79 &size_read);
80
81 if ((ret != ERROR_OK) || (size_read != size)) {
82 /* Propagating the return code would return '0' (misleadingly indicating
83 * successful execution of the function) if only the size check fails. */
84 return ERROR_FAIL;
85 }
86
87 uint16_t bytes_remaining = size;
88
89 /* Send section data in chunks of up to 64 bytes to ULINK */
90 while (bytes_remaining > 0) {
91 if (bytes_remaining > 64)
92 chunk_size = 64;
93 else
94 chunk_size = bytes_remaining;
95
96 jtag_libusb_control_transfer(libusb_dev,
97 LIBUSB_REQUEST_TYPE_VENDOR | \
98 LIBUSB_ENDPOINT_OUT,
99 USBBLASTER_CTRL_LOAD_FIRM,
100 addr,
101 0,
102 (char *)data_ptr,
103 chunk_size,
104 100);
105
106 bytes_remaining -= chunk_size;
107 addr += chunk_size;
108 data_ptr += chunk_size;
109 }
110
111 return ERROR_OK;
112 }
113
114 static int load_usb_blaster_firmware(struct jtag_libusb_device_handle *libusb_dev,
115 struct ublast_lowlevel *low)
116 {
117 struct image ublast2_firmware_image;
118
119 if (!low->firmware_path) {
120 LOG_ERROR("No firmware path specified");
121 return ERROR_FAIL;
122 }
123
124 ublast2_firmware_image.base_address = 0;
125 ublast2_firmware_image.base_address_set = 0;
126
127 int ret = image_open(&ublast2_firmware_image, low->firmware_path, "ihex");
128 if (ret != ERROR_OK) {
129 LOG_ERROR("Could not load firmware image");
130 return ret;
131 }
132
133 /** A host loader program must write 0x01 to the CPUCS register
134 * to put the CPU into RESET, load all or part of the EZUSB
135 * RAM with firmware, then reload the CPUCS register
136 * with ‘0’ to take the CPU out of RESET. The CPUCS register
137 * (at 0xE600) is the only EZ-USB register that can be written
138 * using the Firmware Download command.
139 */
140
141 char value = CPU_RESET;
142 jtag_libusb_control_transfer(libusb_dev,
143 LIBUSB_REQUEST_TYPE_VENDOR | \
144 LIBUSB_ENDPOINT_OUT,
145 USBBLASTER_CTRL_LOAD_FIRM,
146 EZUSB_CPUCS,
147 0,
148 &value,
149 1,
150 100);
151
152 /* Download all sections in the image to ULINK */
153 for (int i = 0; i < ublast2_firmware_image.num_sections; i++) {
154 ret = ublast2_write_firmware_section(libusb_dev,
155 &ublast2_firmware_image, i);
156 if (ret != ERROR_OK) {
157 LOG_ERROR("Error while downloading the firmware");
158 return ret;
159 }
160 }
161
162 value = !CPU_RESET;
163 jtag_libusb_control_transfer(libusb_dev,
164 LIBUSB_REQUEST_TYPE_VENDOR | \
165 LIBUSB_ENDPOINT_OUT,
166 USBBLASTER_CTRL_LOAD_FIRM,
167 EZUSB_CPUCS,
168 0,
169 &value,
170 1,
171 100);
172
173 image_close(&ublast2_firmware_image);
174
175 return ERROR_OK;
176 }
177
178 static int ublast2_libusb_init(struct ublast_lowlevel *low)
179 {
180 const uint16_t vids[] = { low->ublast_vid_uninit, 0 };
181 const uint16_t pids[] = { low->ublast_pid_uninit, 0 };
182 struct jtag_libusb_device_handle *temp;
183 bool renumeration = false;
184 int ret;
185
186 if (jtag_libusb_open(vids, pids, NULL, &temp) == ERROR_OK) {
187 LOG_INFO("Altera USB-Blaster II (uninitialized) found");
188 LOG_INFO("Loading firmware...");
189 ret = load_usb_blaster_firmware(temp, low);
190 jtag_libusb_close(temp);
191 if (ret != ERROR_OK)
192 return ret;
193 renumeration = true;
194 }
195
196 const uint16_t vids_renum[] = { low->ublast_vid, 0 };
197 const uint16_t pids_renum[] = { low->ublast_pid, 0 };
198
199 if (renumeration == false) {
200 if (jtag_libusb_open(vids_renum, pids_renum, NULL, &low->libusb_dev) != ERROR_OK) {
201 LOG_ERROR("Altera USB-Blaster II not found");
202 return ERROR_FAIL;
203 }
204 } else {
205 int retry = 10;
206 while (jtag_libusb_open(vids_renum, pids_renum, NULL, &low->libusb_dev) != ERROR_OK && retry--) {
207 usleep(1000000);
208 LOG_INFO("Waiting for renumerate...");
209 }
210
211 if (!retry) {
212 LOG_ERROR("Altera USB-Blaster II not found");
213 return ERROR_FAIL;
214 }
215 }
216
217 char buffer[5];
218 jtag_libusb_control_transfer(low->libusb_dev,
219 LIBUSB_REQUEST_TYPE_VENDOR | \
220 LIBUSB_ENDPOINT_IN,
221 USBBLASTER_CTRL_READ_REV,
222 0,
223 0,
224 buffer,
225 5,
226 100);
227
228 LOG_INFO("Altera USB-Blaster II found (Firm. rev. = %s)", buffer);
229
230 return ERROR_OK;
231 }
232
233 static int ublast2_libusb_quit(struct ublast_lowlevel *low)
234 {
235 jtag_libusb_close(low->libusb_dev);
236 return ERROR_OK;
237 };
238
239 static struct ublast_lowlevel low = {
240 .open = ublast2_libusb_init,
241 .close = ublast2_libusb_quit,
242 .read = ublast2_libusb_read,
243 .write = ublast2_libusb_write,
244 .flags = COPY_TDO_BUFFER,
245 };
246
247 struct ublast_lowlevel *ublast2_register_libusb(void)
248 {
249 return &low;
250 }

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)