52 #define CPUCS_REG 0xE600
55 #define REQUEST_FIRMWARE_LOAD 0xA0
58 #define CPU_RESET 0x01
61 #define CPU_START 0x00
64 #define FIRMWARE_ADDR 0x0000
67 #define ANGIE_RENUMERATION_DELAY_US 800000
70 #define ANGIE_FIRMWARE_FILE PKGDATADIR "/angie/angie_firmware.bin"
73 #define ANGIE_BITSTREAM_FILE PKGDATADIR "/angie/angie_bitstream.bit"
79 #define ANGIE_FW_SECTION_SIZE 16384
82 #define VR_CFGOPEN 0xB0
83 #define VR_DATAOUTOPEN 0xB2
85 #define ANGIE_VID 0x584E
86 #define ANGIE_NPROG_PID 0x424E
87 #define ANGIE_PROG_OOCD_PID 0x414F
88 #define ANGIE_PROG_NXB2_PID 0x4a55
95 #define NSYSRST_GPIO 6
97 #define ANGIE_XFER_BUFFER_TOTAL_SIZE (16 * 1024)
98 #define ANGIE_USB_BULK_SIZE 512
101 #define ANGIE_USB_TIMEOUT_MS 1000
176 for (
int bit_cnt = 0; bit_cnt < scan_size; bit_cnt++) {
178 int bytec = bit_cnt / 8;
180 int bcval = 1 << (bit_cnt % 8);
184 entry->
buffer[bytec] |= bcval;
186 entry->
buffer[bytec] &= ~bcval;
236 int sent_chunk_size = 0, bytes_received = 0;
244 LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
248 LOG_ERROR(
"Failed to send GPIF count to target");
268 if (sent_chunk_size == xfer_size && bytes_received == xfer_size) {
269 device->reply_buffer_len += xfer_size;
270 device->xfer_buffer_len -= xfer_size;
272 *bytes_sent += sent_chunk_size;
291 if (
device->xfer_buffer_len == 0)
294 int total_bytes_sent = 0;
295 device->reply_buffer_len = 0;
301 total_bytes_sent, &sent_chunk_size);
304 total_bytes_sent += sent_chunk_size;
305 }
while (
device->xfer_buffer_len > 0);
387 struct libusb_device_handle *usb_dev;
391 LOG_ERROR(
"Failed to open ANGIE USB interface");
411 if (libusb_release_interface(
device->usbdev, 0) != LIBUSB_SUCCESS) {
412 LOG_ERROR(
"Could not release interface 0");
434 LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
450 uint8_t *data,
size_t size)
452 int bytes_remaining =
size;
455 while (bytes_remaining > 0) {
459 if (bytes_remaining > 64)
462 chunk_size = bytes_remaining;
465 LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
472 if (transferred != chunk_size) {
477 bytes_remaining -= chunk_size;
496 struct image angie_firmware_image;
507 ret =
image_open(&angie_firmware_image, filename,
"bin");
509 LOG_ERROR(
"Could not load firmware image");
520 for (
unsigned int i = 0; i < angie_firmware_image.
num_sections; i++) {
525 LOG_DEBUG(
"section %02i at addr 0x%04x (size 0x%04" PRIx32
")",
529 size, data, &size_read);
532 if (size_read !=
size) {
539 LOG_ERROR(
"Could not write firmware section");
548 LOG_ERROR(
"Could not restart ANGIE CPU");
569 const char *filename,
610 const char *bitstream_file_path = filename;
611 FILE *bitstream_file =
NULL;
612 char *bitstream_data =
NULL;
616 bitstream_file = fopen(bitstream_file_path,
"rb");
617 if (!bitstream_file) {
618 LOG_ERROR(
"Failed to open bitstream file: %s\n", bitstream_file_path);
624 fseek(bitstream_file, 0, SEEK_END);
625 size_t bitstream_size = ftell(bitstream_file);
626 fseek(bitstream_file, 0, SEEK_SET);
629 bitstream_data = malloc(bitstream_size);
630 if (!bitstream_data) {
631 LOG_ERROR(
"Failed to allocate memory for bitstream data.");
637 if (fread(bitstream_data, 1, bitstream_size, bitstream_file) != bitstream_size) {
638 LOG_ERROR(
"Failed to read bitstream data.");
646 LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
647 VR_CFGOPEN, 0, 0, (
char *)gpifcnt,
sizeof(gpifcnt),
655 int actual_length = 0;
659 LOG_ERROR(
"Failed to send bitstream data: %s", libusb_strerror(ret));
663 LOG_INFO(
"Bitstream sent successfully.");
666 free(bitstream_data);
668 fclose(bitstream_file);
681 struct libusb_device_descriptor desc;
686 if (ret != LIBUSB_SUCCESS)
725 for (
int i = skip; i < tms_count; i++) {
726 tms = (tms_scan >> i) & 1;
761 cmd_size +=
count * 2 + 1;
777 cmd_size +=
count * 2 + 1;
813 int start_offset =
device->xfer_buffer_len;
818 for (
int i = 0; i < scan_size; i++) {
821 tms = (i == scan_size - 1) ? 1 : 0;
825 int bcval = 1 << (i % 8);
887 cmd_size +=
cmd->num_cycles * 2 + 1;
918 for (
unsigned int i = 0; i <
cmd->num_cycles; i++) {
953 unsigned int num_bits =
cmd->num_bits;
954 const uint8_t *
bits =
cmd->bits;
959 for (
unsigned int i = 0; i < num_bits; i++) {
960 tms = ((
bits[i / 8] >> (i % 8)) & 1);
987 if (
cmd->trst == 1 ||
993 else if (
cmd->trst == 0)
998 else if (
cmd->srst == 0)
1022 for (
unsigned int i = 0; i <
cmd->num_cycles; i++) {
1063 int num_states =
cmd->num_states;
1069 int state_count = 0;
1070 while (num_states) {
1076 LOG_ERROR(
"BUG: %s -> %s isn't a valid TAP transition",
1111 switch (
cmd->type) {
1115 return cmd->cmd.tms->num_bits + 2 + 1;
1121 return cmd->cmd.stableclocks->num_cycles * 2;
1124 cmd->cmd.statemove->end_state) * 2 + 1;
1126 return cmd->cmd.pathmove->num_states * 2 + 1;
1131 LOG_ERROR(
"BUG: unknown JTAG command type encountered");
1153 switch (
cmd->type) {
1210 LOG_ERROR(
"BUG: unknown JTAG command type encountered");
1264 LOG_INFO(
"Loading ANGIE firmware. This is reversible by power-cycling ANGIE device.");
1267 if (ret != LIBUSB_SUCCESS) {
1268 LOG_ERROR(
"Failed to claim interface 0");
1277 LOG_ERROR(
"Could not download firmware and re-numerate ANGIE");
1284 LOG_ERROR(
"Could not download bitstream");
1289 LOG_INFO(
"ANGIE device is already running ANGIE firmware");
1305 int baud = (divisor == 0) ? 3000000 :
1306 (divisor == 1) ? 2000000 :
1308 LOG_DEBUG(
"angie speed(%d) rate %d bits/sec", divisor, baud);
1330 }
else if (khz > 1700) {
1333 *divisor = (2 * 3000 / khz + 1) / 2;
1334 if (*divisor > 0x3FFF)
1352 else if (divisor == 1)
1355 *khz = 30000 / divisor;
static int angie_usb_open(struct angie *device)
Open Angie USB interface.
#define ANGIE_XFER_BUFFER_TOTAL_SIZE
#define CPUCS_REG
Address of EZ-USB ANGIE CPU Control & Status register.
static int angie_init(void)
Angie initialization method.
static bool angie_is_firmware_needed(struct angie *device)
Check if Angie firmware must be updated.
#define ANGIE_FIRMWARE_FILE
Default location of ANGIE firmware image.
static int angie_jtag_execute_pathmove(struct angie *device, const struct pathmove_command *cmd)
Execute JTAG PATHMOVE command.
static int angie_jtag_scan_size(struct angie *device, const struct scan_command *cmd)
Return JTAG SCAN command size in bytes.
static int angie_buffer_flush_check(struct angie *device, size_t size)
Check if transfer buffer has enough remaining space for a given size.
static int angie_jtag_execute_scan(struct angie *device, const struct scan_command *cmd)
Execute JTAG SCAN command.
static int angie_state_move(struct angie *device, int skip)
Move TAP to given state.
static int angie_load_firmware(struct angie *device, const char *filename)
Downloads a firmware image to the ANGIE's EZ-USB microcontroller over the USB bus.
#define CPU_START
Value to write into CPUCS to put EZ-USB ANGIE out of reset.
#define ANGIE_USB_TIMEOUT_MS
USB timeout delay in milliseconds.
static void angie_read_queue_execute(struct read_queue *queue, struct angie *device)
Execute elements enqueued in the read queue list.
struct adapter_driver angie_adapter_driver
#define REQUEST_FIRMWARE_LOAD
USB Control EP0 bRequest: "Firmware Load".
static struct jtag_interface angie_interface
#define IN_EP
USB endpoints.
#define FIRMWARE_ADDR
Base address of firmware in EZ-USB ANGIE code space.
static int angie_buffer_flush_chunk(struct angie *device, int xfer_size, int offset, int *bytes_sent)
Flush a chunk of Angie's buffer.
static int angie_write_firmware_section(struct angie *device, uint16_t address, uint8_t *data, size_t size)
Send one contiguous firmware section to the ANGIE's EZ-USB microcontroller over the USB bus.
static int angie_jtag_execute_queue(struct jtag_command *cmd_queue)
Execute JTAG commands queue.
static void angie_read_queue_clean(struct read_queue *queue)
Clear the read queue list.
static int angie_buffer_flush(struct angie *device)
Flush Angie transfer buffer.
#define ANGIE_USB_BULK_SIZE
static void angie_read_queue_add(struct read_queue *queue, struct read_queue_entry *entry)
Add a single entry to the read queue.
static int angie_jtag_execute_reset(struct angie *device, const struct reset_command *cmd)
Execute JTAG RESET command Control /TRST and /SYSRST pins.
static int angie_jtag_execute_statemove(struct angie *device, const struct statemove_command *cmd)
Execute JTAG STATEMOVE command.
#define ANGIE_FW_SECTION_SIZE
Maximum size of a single firmware section.
struct angie * angie_handle
Angie device singleton.
static int angie_buffer_append(struct angie *device, int tck, int tms, int tdi)
Append a bit-bang JTAG value to the transfer buffer.
static size_t angie_cmd_size(struct angie *device, const struct jtag_command *cmd)
Process command size in bytes.
#define ANGIE_PROG_OOCD_PID
#define ANGIE_RENUMERATION_DELAY_US
Delay (in microseconds) to wait while EZ-USB performs ReNumeration.
static int angie_load_firmware_and_renumerate(struct angie *device, const char *filename, uint32_t delay_us)
Puts the ANGIE's EZ-USB microcontroller into reset state, downloads the firmware image,...
static int angie_buffer_append_simple(struct angie *device, uint8_t value)
Append a single byte value to the transfer buffer.
static int angie_speed(int divisor)
Angie set speed method.
static int angie_khz(int khz, int *divisor)
Angie set khz method.
static int angie_jtag_execute_stableclocks(struct angie *device, const struct stableclocks_command *cmd)
Execute JTAG STABLECLOCKS command Issues a number of clock cycles while staying in a stable state.
static int angie_jtag_execute_runtest(struct angie *device, const struct runtest_command *cmd)
Execute JTAG RUNTEST command.
static int angie_quit(void)
Angie quit method.
static int angie_usb_close(struct angie *device)
Releases the ANGIE interface and closes the USB device handle.
#define ANGIE_PROG_NXB2_PID
#define CPU_RESET
Value to write into CPUCS to put EZ-USB ANGIE into reset.
static int angie_cpu_reset(struct angie *device, char reset_bit)
Writes '0' or '1' to the CPUCS register, putting the EZ-USB CPU into reset or out of reset.
#define ANGIE_BITSTREAM_FILE
Default location of ANGIE firmware image.
static void angie_read_queue_init(struct read_queue *queue)
Init read queue list.
static int angie_jtag_execute_tms(struct angie *device, const struct tms_command *cmd)
Execute JTAG TMS command Clock a bunch of TMS transitions, to change the JTAG state machine.
#define VR_CFGOPEN
Vendor Requests.
static int angie_speed_div(int divisor, int *khz)
Angie set speed div.
static void angie_set_end_state(enum tap_state state)
Set TAP end state.
static int angie_jtag_runtest_size(struct angie *device, const struct runtest_command *cmd)
Return JTAG RUNTEST command size in bytes.
static int angie_load_bitstream(struct angie *device, const char *filename)
Downloads a bitstream file to the ANGIE's FPGA through the EZ-USB microcontroller over the USB bus.
static const struct device_t * device
unsigned int jtag_scan_size(const struct scan_command *cmd)
int jtag_build_buffer(const struct scan_command *cmd, uint8_t **buffer)
enum scan_type jtag_scan_type(const struct scan_command *cmd)
int jtag_read_buffer(uint8_t *buffer, const struct scan_command *cmd)
scan_type
The inferred type of a scan_command structure, indicating whether the command has the host scan in fr...
@ SCAN_IN
From device to host,.
@ SCAN_OUT
From host to device,.
void delay_us(uint16_t delay)
uint64_t buffer
Pointer to data buffer to send over SPI.
uint32_t size
Size of dw_spi_transaction::buffer.
uint32_t address
Starting address. Sector aligned.
void image_close(struct image *image)
int image_read_section(struct image *image, int section, target_addr_t offset, uint32_t size, uint8_t *buffer, size_t *size_read)
int image_open(struct image *image, const char *url, const char *type_string)
enum tap_state tap_get_end_state(void)
For more information,.
enum tap_state tap_state_transition(enum tap_state cur_state, bool tms)
Function tap_state_transition takes a current TAP state and returns the next state according to the t...
const char * tap_state_name(enum tap_state state)
Function tap_state_name Returns a string suitable for display representing the JTAG tap_state.
int tap_get_tms_path_len(enum tap_state from, enum tap_state to)
Function int tap_get_tms_path_len returns the total number of bits that represents a TMS path transit...
void tap_set_end_state(enum tap_state new_end_state)
This function sets the state of an "end state follower" which tracks the state that any cable driver ...
enum tap_state tap_get_state(void)
This function gets the state of the "state follower" which tracks the state of the TAPs connected to ...
bool tap_is_state_stable(enum tap_state astate)
Function tap_is_state_stable returns true if the astate is stable.
int tap_get_tms_path(enum tap_state from, enum tap_state to)
This function provides a "bit sequence" indicating what has to be done with TMS during a sequence of ...
#define DEBUG_CAP_TMS_SEQ
#define tap_set_state(new_state)
This function sets the state of a "state follower" which tracks the state of the TAPs connected to th...
void jtag_sleep(uint32_t us)
enum reset_types jtag_get_reset_config(void)
#define ERROR_JTAG_DEVICE_ERROR
tap_state
Defines JTAG Test Access Port states.
int jtag_libusb_open(const uint16_t vids[], const uint16_t pids[], const char *product, struct libusb_device_handle **out, adapter_get_alternate_serial_fn adapter_get_alternate_serial)
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, int *transferred)
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)
static void list_add_tail(struct list_head *new, struct list_head *head)
#define list_for_each_entry_safe(p, n, h, field)
static void list_del(struct list_head *entry)
static void INIT_LIST_HEAD(struct list_head *list)
#define LOG_DEBUG_IO(expr ...)
#define LOG_ERROR(expr ...)
#define LOG_INFO(expr ...)
#define LOG_DEBUG(expr ...)
uint8_t bits[QN908X_FLASH_MAX_BLOCKS *QN908X_FLASH_PAGES_PER_BLOCK/8]
target_addr_t addr
Start address to search for the control block.
Represents a driver for a debugging interface.
const char *const name
The name of the interface driver.
Angie device main context.
struct libusb_device_handle * usbdev
uint8_t reply_buffer[ANGIE_XFER_BUFFER_TOTAL_SIZE]
uint8_t xfer_buffer[ANGIE_XFER_BUFFER_TOTAL_SIZE]
struct read_queue read_queue
unsigned int num_sections
struct imagesection * sections
target_addr_t base_address
Represents a driver for a debugging interface.
unsigned int supported
Bit vector listing capabilities exposed by this driver.
Entry element used to forge a reply buffer for openocd JTAG core.
const struct scan_command * cmd
List of elements used in a multiple commands reply.
The scan_command provide a means of encapsulating a set of scan_field structures that should be scann...
Encapsulates a series of bits to be clocked out, affecting state and mode of the interface.
static void h_u32_to_be(uint8_t *buf, uint32_t val)