OpenOCD
ublast2_access_libusb.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /*
4  * Driver for USB-JTAG, Altera USB-Blaster II and compatibles
5  *
6  * Copyright (C) 2013 Franck Jullien franck.jullien@gmail.com
7  *
8  */
9 
10 #ifdef HAVE_CONFIG_H
11 #include <config.h>
12 #endif
13 #include <jtag/interface.h>
14 #include <jtag/commands.h>
15 #include "helper/system.h"
16 #include <libusb_helper.h>
17 #include <target/image.h>
18 
19 #include "ublast_access.h"
20 
21 #define USBBLASTER_CTRL_READ_REV 0x94
22 #define USBBLASTER_CTRL_LOAD_FIRM 0xA0
23 #define USBBLASTER_EPOUT 4
24 #define USBBLASTER_EPIN 8
25 
26 #define EZUSB_CPUCS 0xe600
27 #define CPU_RESET 1
28 
30 #define SECTION_BUFFERSIZE 16384
31 
32 static int ublast2_libusb_read(struct ublast_lowlevel *low, uint8_t *buf,
33  unsigned size, uint32_t *bytes_read)
34 {
35  int ret, tmp = 0;
36 
39  LIBUSB_ENDPOINT_IN,
40  (char *)buf,
41  size,
42  100, &tmp);
43  *bytes_read = tmp;
44 
45  return ret;
46 }
47 
48 static int ublast2_libusb_write(struct ublast_lowlevel *low, uint8_t *buf,
49  int size, uint32_t *bytes_written)
50 {
51  int ret, tmp = 0;
52 
55  LIBUSB_ENDPOINT_OUT,
56  (char *)buf,
57  size,
58  100, &tmp);
59  *bytes_written = tmp;
60 
61  return ret;
62 
63 }
64 
65 static int ublast2_write_firmware_section(struct libusb_device_handle *libusb_dev,
66  struct image *firmware_image, int section_index)
67 {
68  uint16_t chunk_size;
69  uint8_t data[SECTION_BUFFERSIZE];
70  uint8_t *data_ptr = data;
71  size_t size_read;
72 
73  uint16_t size = (uint16_t)firmware_image->sections[section_index].size;
74  uint16_t addr = (uint16_t)firmware_image->sections[section_index].base_address;
75 
76  LOG_DEBUG("section %02i at addr 0x%04x (size 0x%04x)", section_index, addr,
77  size);
78 
79  /* Copy section contents to local buffer */
80  int ret = image_read_section(firmware_image, section_index, 0, size, data,
81  &size_read);
82 
83  if ((ret != ERROR_OK) || (size_read != size)) {
84  /* Propagating the return code would return '0' (misleadingly indicating
85  * successful execution of the function) if only the size check fails. */
86  return ERROR_FAIL;
87  }
88 
89  uint16_t bytes_remaining = size;
90 
91  /* Send section data in chunks of up to 64 bytes to ULINK */
92  while (bytes_remaining > 0) {
93  if (bytes_remaining > 64)
94  chunk_size = 64;
95  else
96  chunk_size = bytes_remaining;
97 
99  LIBUSB_REQUEST_TYPE_VENDOR |
100  LIBUSB_ENDPOINT_OUT,
102  addr,
103  0,
104  (char *)data_ptr,
105  chunk_size,
106  100);
107 
108  bytes_remaining -= chunk_size;
109  addr += chunk_size;
110  data_ptr += chunk_size;
111  }
112 
113  return ERROR_OK;
114 }
115 
116 static int load_usb_blaster_firmware(struct libusb_device_handle *libusb_dev,
117  struct ublast_lowlevel *low)
118 {
119  struct image ublast2_firmware_image;
120 
121  if (!low->firmware_path) {
122  LOG_ERROR("No firmware path specified");
123  return ERROR_FAIL;
124  }
125 
126  if (libusb_claim_interface(libusb_dev, 0)) {
127  LOG_ERROR("unable to claim interface");
128  return ERROR_JTAG_INIT_FAILED;
129  }
130 
131  ublast2_firmware_image.base_address = 0;
132  ublast2_firmware_image.base_address_set = false;
133 
134  int ret = image_open(&ublast2_firmware_image, low->firmware_path, "ihex");
135  if (ret != ERROR_OK) {
136  LOG_ERROR("Could not load firmware image");
137  goto error_release_usb;
138  }
139 
148  char value = CPU_RESET;
149  jtag_libusb_control_transfer(libusb_dev,
150  LIBUSB_REQUEST_TYPE_VENDOR |
151  LIBUSB_ENDPOINT_OUT,
153  EZUSB_CPUCS,
154  0,
155  &value,
156  1,
157  100);
158 
159  /* Download all sections in the image to ULINK */
160  for (unsigned int i = 0; i < ublast2_firmware_image.num_sections; i++) {
161  ret = ublast2_write_firmware_section(libusb_dev,
162  &ublast2_firmware_image, i);
163  if (ret != ERROR_OK) {
164  LOG_ERROR("Error while downloading the firmware");
165  goto error_close_firmware;
166  }
167  }
168 
169  value = !CPU_RESET;
170  jtag_libusb_control_transfer(libusb_dev,
171  LIBUSB_REQUEST_TYPE_VENDOR |
172  LIBUSB_ENDPOINT_OUT,
174  EZUSB_CPUCS,
175  0,
176  &value,
177  1,
178  100);
179 
180 error_close_firmware:
181  image_close(&ublast2_firmware_image);
182 
183 error_release_usb:
184  /*
185  * Release claimed interface. Most probably it is already disconnected
186  * and re-enumerated as new devices after firmware upload, so we do
187  * not need to care about errors.
188  */
189  libusb_release_interface(libusb_dev, 0);
190 
191  return ret;
192 }
193 
195 {
196  const uint16_t vids[] = { low->ublast_vid_uninit, 0 };
197  const uint16_t pids[] = { low->ublast_pid_uninit, 0 };
198  struct libusb_device_handle *temp;
199  bool renumeration = false;
200  int ret;
201 
202  if (jtag_libusb_open(vids, pids, &temp, NULL) == ERROR_OK) {
203  LOG_INFO("Altera USB-Blaster II (uninitialized) found");
204  LOG_INFO("Loading firmware...");
205  ret = load_usb_blaster_firmware(temp, low);
206  jtag_libusb_close(temp);
207  if (ret != ERROR_OK)
208  return ret;
209  renumeration = true;
210  }
211 
212  const uint16_t vids_renum[] = { low->ublast_vid, 0 };
213  const uint16_t pids_renum[] = { low->ublast_pid, 0 };
214 
215  if (renumeration == false) {
216  if (jtag_libusb_open(vids_renum, pids_renum, &low->libusb_dev, NULL) != ERROR_OK) {
217  LOG_ERROR("Altera USB-Blaster II not found");
218  return ERROR_FAIL;
219  }
220  } else {
221  int retry = 10;
222  while (jtag_libusb_open(vids_renum, pids_renum, &low->libusb_dev, NULL) != ERROR_OK && retry--) {
223  usleep(1000000);
224  LOG_INFO("Waiting for reenumerate...");
225  }
226 
227  if (!retry) {
228  LOG_ERROR("Altera USB-Blaster II not found");
229  return ERROR_FAIL;
230  }
231  }
232 
233  if (libusb_claim_interface(low->libusb_dev, 0)) {
234  LOG_ERROR("unable to claim interface");
236  return ERROR_JTAG_INIT_FAILED;
237  }
238 
239  char buffer[5];
241  LIBUSB_REQUEST_TYPE_VENDOR |
242  LIBUSB_ENDPOINT_IN,
244  0,
245  0,
246  buffer,
247  5,
248  100);
249 
250  LOG_INFO("Altera USB-Blaster II found (Firm. rev. = %s)", buffer);
251 
252  return ERROR_OK;
253 }
254 
256 {
257  if (libusb_release_interface(low->libusb_dev, 0))
258  LOG_ERROR("usb release interface failed");
259 
261  return ERROR_OK;
262 };
263 
264 static struct ublast_lowlevel low = {
266  .close = ublast2_libusb_quit,
267  .read = ublast2_libusb_read,
268  .write = ublast2_libusb_write,
269  .flags = COPY_TDO_BUFFER,
270 };
271 
273 {
274  return &low;
275 }
void image_close(struct image *image)
Definition: image.c:1197
int image_read_section(struct image *image, int section, target_addr_t offset, uint32_t size, uint8_t *buffer, size_t *size_read)
Definition: image.c:1065
int image_open(struct image *image, const char *url, const char *type_string)
Definition: image.c:949
#define ERROR_JTAG_INIT_FAILED
Definition: jtag.h:549
int jtag_libusb_bulk_write(struct libusb_device_handle *dev, int ep, char *bytes, int size, int timeout, int *transferred)
int jtag_libusb_control_transfer(struct libusb_device_handle *dev, uint8_t request_type, uint8_t request, uint16_t value, uint16_t index, char *bytes, uint16_t size, unsigned int timeout)
void jtag_libusb_close(struct libusb_device_handle *dev)
int jtag_libusb_bulk_read(struct libusb_device_handle *dev, int ep, char *bytes, int size, int timeout, int *transferred)
int jtag_libusb_open(const uint16_t vids[], const uint16_t pids[], struct libusb_device_handle **out, adapter_get_alternate_serial_fn adapter_get_alternate_serial)
#define ERROR_FAIL
Definition: log.h:161
#define LOG_ERROR(expr ...)
Definition: log.h:123
#define LOG_INFO(expr ...)
Definition: log.h:117
#define LOG_DEBUG(expr ...)
Definition: log.h:109
#define ERROR_OK
Definition: log.h:155
uint32_t addr
Definition: nuttx.c:65
size_t size
Size of the control block search area.
Definition: rtt/rtt.c:30
Definition: image.h:48
unsigned int num_sections
Definition: image.h:51
struct imagesection * sections
Definition: image.h:52
long long base_address
Definition: image.h:54
bool base_address_set
Definition: image.h:53
target_addr_t base_address
Definition: image.h:42
uint32_t size
Definition: image.h:43
uint16_t ublast_pid_uninit
Definition: ublast_access.h:27
int(* open)(struct ublast_lowlevel *low)
Definition: ublast_access.h:35
struct libusb_device_handle * libusb_dev
Definition: ublast_access.h:28
uint16_t ublast_vid
Definition: ublast_access.h:24
uint16_t ublast_vid_uninit
Definition: ublast_access.h:26
uint16_t ublast_pid
Definition: ublast_access.h:25
char * firmware_path
Definition: ublast_access.h:29
#define SECTION_BUFFERSIZE
Maximum size of a single firmware section.
static int ublast2_libusb_write(struct ublast_lowlevel *low, uint8_t *buf, int size, uint32_t *bytes_written)
#define USBBLASTER_CTRL_LOAD_FIRM
static int ublast2_write_firmware_section(struct libusb_device_handle *libusb_dev, struct image *firmware_image, int section_index)
static int ublast2_libusb_read(struct ublast_lowlevel *low, uint8_t *buf, unsigned size, uint32_t *bytes_read)
static int ublast2_libusb_quit(struct ublast_lowlevel *low)
static int ublast2_libusb_init(struct ublast_lowlevel *low)
struct ublast_lowlevel * ublast2_register_libusb(void)
#define USBBLASTER_EPIN
#define CPU_RESET
#define EZUSB_CPUCS
static struct ublast_lowlevel low
#define USBBLASTER_CTRL_READ_REV
#define USBBLASTER_EPOUT
static int load_usb_blaster_firmware(struct libusb_device_handle *libusb_dev, struct ublast_lowlevel *low)
#define COPY_TDO_BUFFER
Definition: ublast_access.h:21
#define NULL
Definition: usb.h:16