Skip to content

Commit

Permalink
jtag/drivers: add USB-Blaster II
Browse files Browse the repository at this point in the history
This patchs adds a new access driver for the existing
USB-Blaster interface driver.

This interface (as it is build-in on the sockit development
board) is composed of a Cypress EZ-USB plus a CPLD.

The Cypress chip as an embedded 8051 microcontroller.
When it's powered up, the firmware is downloaded to the
chip then the device is disconnected and reconnected with
the new firmware.

The USB-Blaster II protocol is almost identicial to
the old one. The only difference is that you need to
send a 0x5F before read TDO back. This command
seems to copy TDO buffer datas to the endpoint buffer.

Driver will be auto enabled if libusb-1.0 is detected.

Change-Id: I562a720a68cb4dcabeab791947d5d38776cb70fa
Signed-off-by: Franck Jullien <[email protected]>
Reviewed-on: http://openocd.zylin.com/1791
Tested-by: jenkins
Reviewed-by: Robert Jarzmik <[email protected]>
Reviewed-by: Spencer Oliver <[email protected]>
  • Loading branch information
fjullien authored and ntfreak committed Jan 20, 2014
1 parent e18c958 commit 87e91f4
Show file tree
Hide file tree
Showing 7 changed files with 358 additions and 15 deletions.
5 changes: 3 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ m4_define([USB1_ADAPTERS],
[[[ftdi], [MPSSE mode of FTDI based devices], [FTDI]],
[[stlink], [ST-Link JTAG Programmer], [HLADAPTER_STLINK]],
[[ti_icdi], [TI ICDI JTAG Programmer], [HLADAPTER_ICDI]],
[[ulink], [Keil ULINK JTAG Programmer], [ULINK]]])
[[ulink], [Keil ULINK JTAG Programmer], [ULINK]],
[[usb_blaster_2], [Altera USB-Blaster II Compatible], [USB_BLASTER_2]]])

m4_define([USB_ADAPTERS],
[[[jlink], [Segger J-Link JTAG Programmer], [JLINK]],
Expand Down Expand Up @@ -1161,7 +1162,7 @@ AM_CONDITIONAL([FT2232_DRIVER], [test $build_ft2232_ftd2xx = yes -o $build_ft223
AM_CONDITIONAL([USB_BLASTER_LIBFTDI], [test $build_usb_blaster_libftdi = yes])
AM_CONDITIONAL([USB_BLASTER_FTD2XX], [test $build_usb_blaster_ftd2xx = yes])
AM_CONDITIONAL([JTAG_VPI], [test $build_jtag_vpi = yes -o $build_jtag_vpi = yes])
AM_CONDITIONAL([USB_BLASTER_DRIVER], [test $build_usb_blaster_ftd2xx = yes -o $build_usb_blaster_libftdi = yes])
AM_CONDITIONAL([USB_BLASTER_DRIVER], [test $build_usb_blaster_ftd2xx = yes -o $build_usb_blaster_libftdi = yes -o $use_libusb1 = yes])
AM_CONDITIONAL([AMTJTAGACCEL], [test $build_amtjtagaccel = yes])
AM_CONDITIONAL([GW16012], [test $build_gw16012 = yes])
AM_CONDITIONAL([PRESTO_LIBFTDI], [test $build_presto_libftdi = yes])
Expand Down
6 changes: 6 additions & 0 deletions src/jtag/drivers/usb_blaster/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
include $(top_srcdir)/common.mk

AM_CPPFLAGS += -I$(top_srcdir)/src/jtag/drivers $(LIBUSB1_CFLAGS)

noinst_LTLIBRARIES = libocdusbblaster.la
libocdusbblaster_la_SOURCES = $(USB_BLASTER_SRC)

Expand All @@ -13,6 +15,10 @@ if USB_BLASTER_FTD2XX
USB_BLASTER_SRC += ublast_access_ftd2xx.c
endif

if USB_BLASTER_2
USB_BLASTER_SRC += ublast2_access_libusb.c
endif

noinst_HEADERS = ublast_access.h

MAINTAINERCLEANFILES = $(srcdir)/Makefile.in
253 changes: 253 additions & 0 deletions src/jtag/drivers/usb_blaster/ublast2_access_libusb.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
/*
* Driver for USB-JTAG, Altera USB-Blaster II and compatibles
*
* Copyright (C) 2013 Franck Jullien [email protected]
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <jtag/interface.h>
#include <jtag/commands.h>
#include <libusb_common.h>
#include <target/image.h>

#include "ublast_access.h"

#define USBBLASTER_CTRL_READ_REV 0x94
#define USBBLASTER_CTRL_LOAD_FIRM 0xA0
#define USBBLASTER_EPOUT 4
#define USBBLASTER_EPIN 8

#define EZUSB_CPUCS 0xe600
#define CPU_RESET 1

/** Maximum size of a single firmware section. Entire EZ-USB code space = 16kB */
#define SECTION_BUFFERSIZE 16384

static int ublast2_libusb_read(struct ublast_lowlevel *low, uint8_t *buf,
unsigned size, uint32_t *bytes_read)
{
*bytes_read = jtag_libusb_bulk_read(low->libusb_dev,
USBBLASTER_EPIN | \
LIBUSB_ENDPOINT_IN,
(char *)buf,
size,
100);
return ERROR_OK;
}

static int ublast2_libusb_write(struct ublast_lowlevel *low, uint8_t *buf,
int size, uint32_t *bytes_written)
{
*bytes_written = jtag_libusb_bulk_write(low->libusb_dev,
USBBLASTER_EPOUT | \
LIBUSB_ENDPOINT_OUT,
(char *)buf,
size,
100);
return ERROR_OK;
}

static int ublast2_write_firmware_section(struct jtag_libusb_device_handle *libusb_dev,
struct image *firmware_image, int section_index)
{
uint16_t chunk_size;
uint8_t data[SECTION_BUFFERSIZE];
uint8_t *data_ptr = data;
size_t size_read;

uint16_t size = (uint16_t)firmware_image->sections[section_index].size;
uint16_t addr = (uint16_t)firmware_image->sections[section_index].base_address;

LOG_DEBUG("section %02i at addr 0x%04x (size 0x%04x)", section_index, addr,
size);

if (data == NULL)
return ERROR_FAIL;

/* Copy section contents to local buffer */
int ret = image_read_section(firmware_image, section_index, 0, size, data,
&size_read);

if ((ret != ERROR_OK) || (size_read != size)) {
/* Propagating the return code would return '0' (misleadingly indicating
* successful execution of the function) if only the size check fails. */
return ERROR_FAIL;
}

uint16_t bytes_remaining = size;

/* Send section data in chunks of up to 64 bytes to ULINK */
while (bytes_remaining > 0) {
if (bytes_remaining > 64)
chunk_size = 64;
else
chunk_size = bytes_remaining;

jtag_libusb_control_transfer(libusb_dev,
LIBUSB_REQUEST_TYPE_VENDOR | \
LIBUSB_ENDPOINT_OUT,
USBBLASTER_CTRL_LOAD_FIRM,
addr,
0,
(char *)data_ptr,
chunk_size,
100);

bytes_remaining -= chunk_size;
addr += chunk_size;
data_ptr += chunk_size;
}

return ERROR_OK;
}

static int load_usb_blaster_firmware(struct jtag_libusb_device_handle *libusb_dev,
struct ublast_lowlevel *low)
{
struct image ublast2_firmware_image;

if (!low->firmware_path) {
LOG_ERROR("No firmware path specified");
return ERROR_FAIL;
}

ublast2_firmware_image.base_address = 0;
ublast2_firmware_image.base_address_set = 0;

int ret = image_open(&ublast2_firmware_image, low->firmware_path, "ihex");
if (ret != ERROR_OK) {
LOG_ERROR("Could not load firmware image");
return ret;
}

/** A host loader program must write 0x01 to the CPUCS register
* to put the CPU into RESET, load all or part of the EZUSB
* RAM with firmware, then reload the CPUCS register
* with ‘0’ to take the CPU out of RESET. The CPUCS register
* (at 0xE600) is the only EZ-USB register that can be written
* using the Firmware Download command.
*/

char value = CPU_RESET;
jtag_libusb_control_transfer(libusb_dev,
LIBUSB_REQUEST_TYPE_VENDOR | \
LIBUSB_ENDPOINT_OUT,
USBBLASTER_CTRL_LOAD_FIRM,
EZUSB_CPUCS,
0,
&value,
1,
100);

/* Download all sections in the image to ULINK */
for (int i = 0; i < ublast2_firmware_image.num_sections; i++) {
ret = ublast2_write_firmware_section(libusb_dev,
&ublast2_firmware_image, i);
if (ret != ERROR_OK) {
LOG_ERROR("Error while downloading the firmware");
return ret;
}
}

value = !CPU_RESET;
jtag_libusb_control_transfer(libusb_dev,
LIBUSB_REQUEST_TYPE_VENDOR | \
LIBUSB_ENDPOINT_OUT,
USBBLASTER_CTRL_LOAD_FIRM,
EZUSB_CPUCS,
0,
&value,
1,
100);

image_close(&ublast2_firmware_image);

return ERROR_OK;
}

static int ublast2_libusb_init(struct ublast_lowlevel *low)
{
const uint16_t vids[] = { low->ublast_vid_uninit, 0 };
const uint16_t pids[] = { low->ublast_pid_uninit, 0 };
struct jtag_libusb_device_handle *temp;
bool renumeration = false;
int ret;

if (jtag_libusb_open(vids, pids, &temp) == ERROR_OK) {
LOG_INFO("Altera USB-Blaster II (uninitialized) found");
LOG_INFO("Loading firmware...");
ret = load_usb_blaster_firmware(temp, low);
jtag_libusb_close(temp);
if (ret != ERROR_OK)
return ret;
renumeration = true;
}

const uint16_t vids_renum[] = { low->ublast_vid, 0 };
const uint16_t pids_renum[] = { low->ublast_pid, 0 };

if (renumeration == false) {
if (jtag_libusb_open(vids_renum, pids_renum, &low->libusb_dev) != ERROR_OK) {
LOG_ERROR("Altera USB-Blaster II not found");
return ERROR_FAIL;
}
} else {
int retry = 10;
while (jtag_libusb_open(vids_renum, pids_renum, &low->libusb_dev) != ERROR_OK && retry--) {
usleep(1000000);
LOG_INFO("Waiting for renumerate...");
}

if (!retry) {
LOG_ERROR("Altera USB-Blaster II not found");
return ERROR_FAIL;
}
}

char buffer[5];
jtag_libusb_control_transfer(low->libusb_dev,
LIBUSB_REQUEST_TYPE_VENDOR | \
LIBUSB_ENDPOINT_IN,
USBBLASTER_CTRL_READ_REV,
0,
0,
buffer,
5,
100);

LOG_INFO("Altera USB-Blaster II found (Firm. rev. = %s)", buffer);

return ERROR_OK;
}

static int ublast2_libusb_quit(struct ublast_lowlevel *low)
{
jtag_libusb_close(low->libusb_dev);
return ERROR_OK;
};

static struct ublast_lowlevel low = {
.open = ublast2_libusb_init,
.close = ublast2_libusb_quit,
.read = ublast2_libusb_read,
.write = ublast2_libusb_write,
.flags = COPY_TDO_BUFFER,
};

struct ublast_lowlevel *ublast2_register_libusb(void)
{
return &low;
}
16 changes: 15 additions & 1 deletion src/jtag/drivers/usb_blaster/ublast_access.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Inspired from original code from Kolja Waschk's USB-JTAG project
* (http://www.ixo.de/info/usb_jtag/), and from openocd project.
*
* Copyright (C) 2013 Franck Jullien [email protected]
* Copyright (C) 2012 Robert Jarzmik [email protected]
* Copyright (C) 2011 Ali Lown [email protected]
* Copyright (C) 2009 Catalin Patulea [email protected]
Expand All @@ -21,10 +22,19 @@
*
*/

#include <libusb_common.h>

/* Low level flags */
#define COPY_TDO_BUFFER (1 << 0)

struct ublast_lowlevel {
uint16_t ublast_vid;
uint16_t ublast_pid;
uint16_t ublast_vid_uninit;
uint16_t ublast_pid_uninit;
char *ublast_device_desc;
struct jtag_libusb_device_handle *libusb_dev;
char *firmware_path;

int (*write)(struct ublast_lowlevel *low, uint8_t *buf, int size,
uint32_t *bytes_written);
Expand All @@ -35,18 +45,22 @@ struct ublast_lowlevel {
int (*speed)(struct ublast_lowlevel *low, int speed);

void *priv;
int flags;
};

/**
* ublast_register_ftdi - get a lowlevel USB Blaster driver
* ublast_register_ftd2xx - get a lowlevel USB Blaster driver
* ublast2_register_libusb - get a lowlevel USB Blaster II driver
*
* Get a lowlevel USB-Blaster driver. In the current implementation, there are 2
* Get a lowlevel USB-Blaster driver. In the current implementation, there are 3
* possible lowlevel drivers :
* - one based on libftdi from ftdichip.com
* - one based on libftdxx, the free alternative
* - one based on libusb, specific to the USB-Blaster II
*
* Returns the lowlevel driver structure.
*/
extern struct ublast_lowlevel *ublast_register_ftdi(void);
extern struct ublast_lowlevel *ublast_register_ftd2xx(void);
extern struct ublast_lowlevel *ublast2_register_libusb(void);
Loading

0 comments on commit 87e91f4

Please sign in to comment.