drivers: USB Blaster II: close file and release USB device if firmware handling failed
[fw/openocd] / 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  *   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/>.
18  *
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24 #include <jtag/interface.h>
25 #include <jtag/commands.h>
26 #include <libusb_helper.h>
27 #include <target/image.h>
28
29 #include "ublast_access.h"
30
31 #define USBBLASTER_CTRL_READ_REV        0x94
32 #define USBBLASTER_CTRL_LOAD_FIRM       0xA0
33 #define USBBLASTER_EPOUT                4
34 #define USBBLASTER_EPIN                 8
35
36 #define EZUSB_CPUCS                     0xe600
37 #define CPU_RESET                       1
38
39 /** Maximum size of a single firmware section. Entire EZ-USB code space = 16kB */
40 #define SECTION_BUFFERSIZE              16384
41
42 static int ublast2_libusb_read(struct ublast_lowlevel *low, uint8_t *buf,
43                               unsigned size, uint32_t *bytes_read)
44 {
45         int ret, tmp = 0;
46
47         ret = jtag_libusb_bulk_read(low->libusb_dev,
48                                             USBBLASTER_EPIN |
49                                             LIBUSB_ENDPOINT_IN,
50                                             (char *)buf,
51                                             size,
52                                             100, &tmp);
53         *bytes_read = tmp;
54
55         return ret;
56 }
57
58 static int ublast2_libusb_write(struct ublast_lowlevel *low, uint8_t *buf,
59                                int size, uint32_t *bytes_written)
60 {
61         int ret, tmp = 0;
62
63         ret = jtag_libusb_bulk_write(low->libusb_dev,
64                                                 USBBLASTER_EPOUT |
65                                                 LIBUSB_ENDPOINT_OUT,
66                                                 (char *)buf,
67                                                 size,
68                                                 100, &tmp);
69         *bytes_written = tmp;
70
71         return ret;
72
73 }
74
75 static int ublast2_write_firmware_section(struct libusb_device_handle *libusb_dev,
76                                    struct image *firmware_image, int section_index)
77 {
78         uint16_t chunk_size;
79         uint8_t data[SECTION_BUFFERSIZE];
80         uint8_t *data_ptr = data;
81         size_t size_read;
82
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;
85
86         LOG_DEBUG("section %02i at addr 0x%04x (size 0x%04x)", section_index, addr,
87                 size);
88
89         /* Copy section contents to local buffer */
90         int ret = image_read_section(firmware_image, section_index, 0, size, data,
91                         &size_read);
92
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. */
96                 return ERROR_FAIL;
97         }
98
99         uint16_t bytes_remaining = size;
100
101         /* Send section data in chunks of up to 64 bytes to ULINK */
102         while (bytes_remaining > 0) {
103                 if (bytes_remaining > 64)
104                         chunk_size = 64;
105                 else
106                         chunk_size = bytes_remaining;
107
108                 jtag_libusb_control_transfer(libusb_dev,
109                                              LIBUSB_REQUEST_TYPE_VENDOR |
110                                              LIBUSB_ENDPOINT_OUT,
111                                              USBBLASTER_CTRL_LOAD_FIRM,
112                                              addr,
113                                              0,
114                                              (char *)data_ptr,
115                                              chunk_size,
116                                              100);
117
118                 bytes_remaining -= chunk_size;
119                 addr += chunk_size;
120                 data_ptr += chunk_size;
121         }
122
123         return ERROR_OK;
124 }
125
126 static int load_usb_blaster_firmware(struct libusb_device_handle *libusb_dev,
127                                      struct ublast_lowlevel *low)
128 {
129         struct image ublast2_firmware_image;
130
131         if (!low->firmware_path) {
132                 LOG_ERROR("No firmware path specified");
133                 return ERROR_FAIL;
134         }
135
136         if (libusb_claim_interface(libusb_dev, 0)) {
137                 LOG_ERROR("unable to claim interface");
138                 return ERROR_JTAG_INIT_FAILED;
139         }
140
141         ublast2_firmware_image.base_address = 0;
142         ublast2_firmware_image.base_address_set = false;
143
144         int ret = image_open(&ublast2_firmware_image, low->firmware_path, "ihex");
145         if (ret != ERROR_OK) {
146                 LOG_ERROR("Could not load firmware image");
147                 goto error_release_usb;
148         }
149
150         /** A host loader program must write 0x01 to the CPUCS register
151          * to put the CPU into RESET, load all or part of the EZUSB
152          * RAM with firmware, then reload the CPUCS register
153          * with ‘0’ to take the CPU out of RESET. The CPUCS register
154          * (at 0xE600) is the only EZ-USB register that can be written
155          * using the Firmware Download command.
156          */
157
158         char value = CPU_RESET;
159         jtag_libusb_control_transfer(libusb_dev,
160                                      LIBUSB_REQUEST_TYPE_VENDOR |
161                                      LIBUSB_ENDPOINT_OUT,
162                                      USBBLASTER_CTRL_LOAD_FIRM,
163                                      EZUSB_CPUCS,
164                                      0,
165                                      &value,
166                                      1,
167                                      100);
168
169         /* Download all sections in the image to ULINK */
170         for (unsigned int i = 0; i < ublast2_firmware_image.num_sections; i++) {
171                 ret = ublast2_write_firmware_section(libusb_dev,
172                                                      &ublast2_firmware_image, i);
173                 if (ret != ERROR_OK) {
174                         LOG_ERROR("Error while downloading the firmware");
175                         goto error_close_firmware;
176                 }
177         }
178
179         value = !CPU_RESET;
180         jtag_libusb_control_transfer(libusb_dev,
181                                      LIBUSB_REQUEST_TYPE_VENDOR |
182                                      LIBUSB_ENDPOINT_OUT,
183                                      USBBLASTER_CTRL_LOAD_FIRM,
184                                      EZUSB_CPUCS,
185                                      0,
186                                      &value,
187                                      1,
188                                      100);
189
190 error_close_firmware:
191         image_close(&ublast2_firmware_image);
192
193 error_release_usb:
194         /*
195          * Release claimed interface. Most probably it is already disconnected
196          * and re-enumerated as new devices after firmware upload, so we do
197          * not need to care about errors.
198          */
199         libusb_release_interface(libusb_dev, 0);
200
201         return ret;
202 }
203
204 static int ublast2_libusb_init(struct ublast_lowlevel *low)
205 {
206         const uint16_t vids[] = { low->ublast_vid_uninit, 0 };
207         const uint16_t pids[] = { low->ublast_pid_uninit, 0 };
208         struct libusb_device_handle *temp;
209         bool renumeration = false;
210         int ret;
211
212         if (jtag_libusb_open(vids, pids, NULL, &temp, NULL) == ERROR_OK) {
213                 LOG_INFO("Altera USB-Blaster II (uninitialized) found");
214                 LOG_INFO("Loading firmware...");
215                 ret = load_usb_blaster_firmware(temp, low);
216                 jtag_libusb_close(temp);
217                 if (ret != ERROR_OK)
218                         return ret;
219                 renumeration = true;
220         }
221
222         const uint16_t vids_renum[] = { low->ublast_vid, 0 };
223         const uint16_t pids_renum[] = { low->ublast_pid, 0 };
224
225         if (renumeration == false) {
226                 if (jtag_libusb_open(vids_renum, pids_renum, NULL,
227                                 &low->libusb_dev, NULL) != ERROR_OK) {
228                         LOG_ERROR("Altera USB-Blaster II not found");
229                         return ERROR_FAIL;
230                 }
231         } else {
232                 int retry = 10;
233                 while (jtag_libusb_open(vids_renum, pids_renum, NULL,
234                                 &low->libusb_dev, NULL) != ERROR_OK && retry--) {
235                         usleep(1000000);
236                         LOG_INFO("Waiting for reenumerate...");
237                 }
238
239                 if (!retry) {
240                         LOG_ERROR("Altera USB-Blaster II not found");
241                         return ERROR_FAIL;
242                 }
243         }
244
245         if (libusb_claim_interface(low->libusb_dev, 0)) {
246                 LOG_ERROR("unable to claim interface");
247                 jtag_libusb_close(low->libusb_dev);
248                 return ERROR_JTAG_INIT_FAILED;
249         }
250
251         char buffer[5];
252         jtag_libusb_control_transfer(low->libusb_dev,
253                                      LIBUSB_REQUEST_TYPE_VENDOR |
254                                      LIBUSB_ENDPOINT_IN,
255                                      USBBLASTER_CTRL_READ_REV,
256                                      0,
257                                      0,
258                                      buffer,
259                                      5,
260                                      100);
261
262         LOG_INFO("Altera USB-Blaster II found (Firm. rev. = %s)", buffer);
263
264         return ERROR_OK;
265 }
266
267 static int ublast2_libusb_quit(struct ublast_lowlevel *low)
268 {
269         if (libusb_release_interface(low->libusb_dev, 0))
270                 LOG_ERROR("usb release interface failed");
271
272         jtag_libusb_close(low->libusb_dev);
273         return ERROR_OK;
274 };
275
276 static struct ublast_lowlevel low = {
277         .open = ublast2_libusb_init,
278         .close = ublast2_libusb_quit,
279         .read = ublast2_libusb_read,
280         .write = ublast2_libusb_write,
281         .flags = COPY_TDO_BUFFER,
282 };
283
284 struct ublast_lowlevel *ublast2_register_libusb(void)
285 {
286         return &low;
287 }