30 #undef AUTOMATIC_BUSY_LED
33 #undef USE_HARDWARE_SHIFTER_FOR_TMS
35 #define INTERFACE_NAME "RLink"
37 #define USB_IDVENDOR (0x138e)
38 #define USB_IDPRODUCT (0x9000)
40 #define USB_EP1OUT_ADDR (0x01)
41 #define USB_EP1OUT_SIZE (16)
42 #define USB_EP1IN_ADDR (USB_EP1OUT_ADDR | 0x80)
43 #define USB_EP1IN_SIZE (USB_EP1OUT_SIZE)
45 #define USB_EP2OUT_ADDR (0x02)
46 #define USB_EP2OUT_SIZE (64)
47 #define USB_EP2IN_ADDR (USB_EP2OUT_ADDR | 0x80)
48 #define USB_EP2IN_SIZE (USB_EP2OUT_SIZE)
49 #define USB_EP2BANK_SIZE (512)
51 #define DTC_STATUS_POLL_BYTE (ST7_USB_BUF_EP0OUT + 0xff)
53 #define ST7_PD_NBUSY_LED ST7_PD0
54 #define ST7_PD_NRUN_LED ST7_PD1
56 #define ST7_PD_VPP_SEL ST7_PD6
58 #define ST7_PD_VPP_SHDN ST7_PD7
61 #define ST7_PE_ADAPTER_SENSE_IN ST7_PE3
62 #define ST7_PE_ADAPTER_SENSE_OUT ST7_PE4
65 #define ST7_PA_IO1 ST7_PA1
66 #define ST7_PA_IO2 ST7_PA2
67 #define ST7_PA_IO4 ST7_PA4
68 #define ST7_PA_IO8 ST7_PA6
69 #define ST7_PA_IO10 ST7_PA7
70 #define ST7_PB_IO5 ST7_PB5
71 #define ST7_PC_IO9 ST7_PC1
72 #define ST7_PC_IO3 ST7_PC2
73 #define ST7_PC_IO7 ST7_PC3
74 #define ST7_PE_IO6 ST7_PE5
77 #define ST7_PA_RTCK ST7_PA_IO0
78 #define ST7_PA_NTRST ST7_PA_IO1
79 #define ST7_PC_TDI ST7_PC_IO3
80 #define ST7_PA_DBGRQ ST7_PA_IO4
81 #define ST7_PB_NSRST ST7_PB_IO5
82 #define ST7_PE_TMS ST7_PE_IO6
83 #define ST7_PC_TCK ST7_PC_IO7
84 #define ST7_PC_TDO ST7_PC_IO9
85 #define ST7_PA_DBGACK ST7_PA_IO10
87 static struct libusb_device_handle *
hdev;
97 uint8_t *usb_buffer_p;
102 if (
length >
sizeof(usb_buffer))
103 length =
sizeof(usb_buffer);
105 usb_buffer_p = usb_buffer;
109 *usb_buffer_p++ = va_arg(ap,
int);
116 sizeof(usb_buffer) - (usb_buffer_p - usb_buffer)
122 (
char *)usb_buffer,
sizeof(usb_buffer),
133 static ssize_t ep1_memory_read(
134 struct libusb_device_handle *hdev_param, uint16_t
addr,
147 sizeof(usb_buffer) - 4
154 if (remain >
sizeof(usb_buffer))
155 length =
sizeof(usb_buffer);
159 usb_buffer[1] =
addr >> 8;
160 usb_buffer[2] =
addr;
165 (
char *)usb_buffer,
sizeof(usb_buffer),
170 if (usb_ret !=
ERROR_OK || transferred < (
int)
sizeof(usb_buffer))
207 if (remain > (
sizeof(usb_buffer) - 4))
208 length = (
sizeof(usb_buffer) - 4);
212 usb_buffer[1] =
addr >> 8;
213 usb_buffer[2] =
addr;
223 sizeof(usb_buffer) - 4 -
length
230 (
char *)usb_buffer,
sizeof(usb_buffer),
235 if (usb_ret !=
ERROR_OK || transferred < (
int)
sizeof(usb_buffer))
249 static ssize_t ep1_memory_writel(
struct libusb_device_handle *hdev_param, uint16_t
addr,
265 *buffer_p++ = va_arg(ap,
int);
273 #define DTCLOAD_COMMENT (0)
274 #define DTCLOAD_ENTRY (1)
275 #define DTCLOAD_LOAD (2)
276 #define DTCLOAD_RUN (3)
277 #define DTCLOAD_LUT_START (4)
278 #define DTCLOAD_LUT (5)
280 #define DTC_LOAD_BUFFER ST7_USB_BUF_EP2UIDO
295 struct header *header;
296 uint8_t lut_start = 0xc0;
309 if (
length <
sizeof(*header)) {
314 header = (
struct header *)
buffer;
315 buffer +=
sizeof(*header);
316 length -=
sizeof(*header);
318 if (
length < (
size_t)header->length + 1) {
323 switch (header->type) {
329 if (!strncmp(
"download", (
char *)
buffer + 1, 8))
336 header->length + 1,
buffer);
368 LOG_ERROR(
"Invalid DTC image record type: 0x%02x", header->type);
373 buffer += (header->length + 1);
374 length -= (header->length + 1);
446 struct libusb_device_handle *hdev_param,
447 uint8_t *command_buffer,
448 int command_buffer_size,
449 uint8_t *reply_buffer,
450 int reply_buffer_size
458 LOG_DEBUG(
"%d/%d", command_buffer_size, reply_buffer_size);
494 if (dtc_status & 0x01)
498 LOG_ERROR(
"too many retries waiting for DTC status");
499 return LIBUSB_ERROR_TIMEOUT;
504 if (reply_buffer && reply_buffer_size) {
508 (
char *)reply_buffer, reply_buffer_size,
513 if (usb_err !=
ERROR_OK || transferred < reply_buffer_size) {
514 LOG_ERROR(
"Read of endpoint 2 returned %d, expected %d",
515 usb_err, reply_buffer_size
617 uint8_t *dtc_p, *tdo_p;
618 uint8_t dtc_mask, tdo_mask;
634 reply_buffer,
sizeof(reply_buffer)
637 LOG_ERROR(
"dtc_run_download: %s", libusb_error_name(usb_err));
643 dtc_p = reply_buffer;
664 dtc_mask = 1 << (8 - 1);
671 if (*dtc_p & dtc_mask)
679 dtc_mask = 1 << (8 - 1);
697 dtc_mask = 1 << (8 - 1);
699 dtc_mask = 1 << (bit_cnt - 1);
730 rq_next = rq_p->
next;
782 if ((
bits >= 8) || !i) {
783 byte_param <<= (8 -
bits);
788 #ifdef USE_HARDWARE_SHIFTER_FOR_TMS
796 #ifdef USE_HARDWARE_SHIFTER_FOR_TMS
848 for (i = 0; i < tms_count; i++) {
849 tms = (tms_scan >> i) & 1;
858 unsigned int num_states =
cmd->num_states;
869 LOG_ERROR(
"BUG: %s -> %s isn't a valid TAP transition",
896 for (
unsigned int i = 0; i < num_cycles; i++)
921 LOG_ERROR(
"%s", libusb_error_name(usb_err));
931 if (usb_err !=
ERROR_OK || transferred < 1) {
932 LOG_ERROR(
"%s", libusb_error_name(usb_err));
957 LOG_ERROR(
"%s", libusb_error_name(usb_err));
967 if (usb_err !=
ERROR_OK || transferred < 1) {
968 LOG_ERROR(
"%s", libusb_error_name(usb_err));
988 LOG_ERROR(
"%s", libusb_error_name(usb_err));
998 if (usb_err !=
ERROR_OK || transferred < 1) {
999 LOG_ERROR(
"%s", libusb_error_name(usb_err));
1005 uint8_t *
buffer,
int scan_size)
1016 uint8_t tdi_mask, *tdi_p;
1019 if (scan_size < 1) {
1020 LOG_ERROR(
"scan_size cannot be less than 1 bit");
1024 ir_scan =
cmd->cmd.scan->ir_scan;
1039 printf(
"scan_size = %d, type = 0x%x\n", scan_size,
type);
1045 buffer[scan_size / 8] &= ((1 << ((scan_size - 1) % 8) + 1) - 1);
1047 printf(
"before scan:");
1048 for (i = 0; i < (scan_size + 7) / 8; i++)
1049 printf(
" %02x",
buffer[i]);
1055 byte_bits = (int)(scan_size - 1) / 8 * 8;
1057 extra_bits = (scan_size - 1) - byte_bits;
1072 dtc_mask = 1 << (extra_bits - 1);
1074 while (extra_bits--) {
1075 if (*tdi_p & tdi_mask)
1081 if (tdi_mask == 0) {
1099 chunk_bits = byte_bits;
1101 if (chunk_bits > (16 * 8))
1102 chunk_bits = (16 * 8);
1119 byte_bits -= chunk_bits;
1124 LOG_ERROR(
"enqueuing DTC reply entry: %s", strerror(errno));
1127 dtc_queue.reply_index += (chunk_bits + 7) / 8;
1129 tdi_bit_offset += chunk_bits;
1133 chunk_bytes = chunk_bits / 8;
1150 dtc_mask = 1 << (8 - 1);
1152 while (chunk_bits--) {
1153 if (*tdi_p & tdi_mask)
1157 if (dtc_mask == 0) {
1160 dtc_mask = 1 << (8 - 1);
1164 if (tdi_mask == 0) {
1181 LOG_ERROR(
"enqueuing DTC reply entry: %s", strerror(errno));
1187 tdi_bit_offset += extra_bits;
1198 dtc_mask = 1 << (8 - 1);
1200 while (extra_bits--) {
1201 if (*tdi_p & tdi_mask)
1207 if (tdi_mask == 0) {
1230 LOG_ERROR(
"enqueuing DTC reply entry: %s", strerror(errno));
1255 int retval, tmp_retval;
1260 #ifndef AUTOMATIC_BUSY_LED
1269 switch (
cmd->type) {
1284 switch (
cmd->type) {
1287 cmd->cmd.reset->trst,
1288 cmd->cmd.reset->srst);
1289 if (
cmd->cmd.reset->trst == 1 ||
1290 (
cmd->cmd.reset->srst &&
1297 cmd->cmd.runtest->num_cycles,
1298 cmd->cmd.runtest->end_state);
1299 if (
cmd->cmd.runtest->end_state != -1)
1305 if (
cmd->cmd.statemove->end_state != -1)
1311 cmd->cmd.pathmove->num_states,
1312 cmd->cmd.pathmove->path[
cmd->cmd.pathmove->num_states - 1]);
1317 (
cmd->cmd.scan->ir_scan) ?
"IR" :
"DR",
1318 cmd->cmd.scan->end_state);
1319 if (
cmd->cmd.scan->end_state != -1)
1331 LOG_ERROR(
"BUG: unknown JTAG command type encountered");
1342 retval = tmp_retval;
1344 #ifndef AUTOMATIC_BUSY_LED
1372 "An error occurred while trying to load DTC code for speed \"%d\".",
1379 LOG_ERROR(
"starting DTC: %s", libusb_error_name(ret));
1387 LOG_ERROR(
"%d is not a supported speed", speed);
1402 LOG_ERROR(
"%d is not a supported speed", speed);
1438 struct libusb_device_descriptor descriptor;
1439 struct libusb_device *usb_dev = libusb_get_device(
hdev);
1440 int r = libusb_get_device_descriptor(usb_dev, &descriptor);
1442 LOG_ERROR(
"error %d getting device descriptor", r);
1446 if (descriptor.bNumConfigurations > 1) {
1447 LOG_ERROR(
"Whoops! NumConfigurations is not 1, don't know what to do...");
1450 struct libusb_config_descriptor *
config;
1451 libusb_get_config_descriptor(usb_dev, 0, &
config);
1452 if (
config->bNumInterfaces > 1) {
1453 LOG_ERROR(
"Whoops! NumInterfaces is not 1, don't know what to do...");
1460 libusb_set_configuration(
hdev,
config->bConfigurationValue);
1464 i = libusb_claim_interface(
hdev, 0);
1465 if (i != LIBUSB_SUCCESS) {
1466 LOG_ERROR(
"usb_claim_interface: %s", libusb_error_name(i));
1467 j = libusb_detach_kernel_driver(
hdev, 0);
1468 if (j != LIBUSB_SUCCESS)
1469 LOG_ERROR(
"detach kernel driver: %s", libusb_error_name(j));
1474 }
while (--retries);
1476 if (i != LIBUSB_SUCCESS) {
1480 if (libusb_set_interface_alt_setting(
hdev, 0, 0) != LIBUSB_SUCCESS) {
1494 for (i = 0; i < 5; i++) {
1500 LOG_ERROR(
"USB write error: %s", libusb_error_name(j));
1505 (
char *)reply_buffer,
sizeof(reply_buffer),
1509 if (j != LIBUSB_ERROR_TIMEOUT)
1513 if (j !=
ERROR_OK || transferred != (
int)
sizeof(reply_buffer)) {
1514 LOG_ERROR(
"USB read error: %s", libusb_error_name(j));
1522 if ((reply_buffer[0] != 0) || (reply_buffer[1] != 0) || (reply_buffer[2] != 3))
1524 "The rlink device is not of the version that the developers have played with. It may or may not work.");
1549 (
char *)reply_buffer, 1,
1574 (
char *)reply_buffer, 1,
1623 #ifdef AUTOMATIC_BUSY_LED
1652 libusb_release_interface(
hdev, 0);
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,.
uint32_t size
Size of dw_spi_transaction::buffer.
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 tap_set_state(new_state)
This function sets the state of a "state follower" which tracks the state of the TAPs connected to th...
static struct device_config config
void jtag_sleep(uint32_t us)
enum reset_types jtag_get_reset_config(void)
tap_state
Defines JTAG Test Access Port states.
#define ERROR_JTAG_QUEUE_FAILED
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_bulk_read(struct libusb_device_handle *dev, int ep, char *bytes, int size, int timeout, int *transferred)
#define LIBUSB_TIMEOUT_MS
#define LOG_DEBUG_IO(expr ...)
#define LOG_WARNING(expr ...)
#define LOG_ERROR(expr ...)
#define LOG_DEBUG(expr ...)
uint8_t bits[QN908X_FLASH_MAX_BLOCKS *QN908X_FLASH_PAGES_PER_BLOCK/8]
static int dtc_load_from_buffer(struct libusb_device_handle *hdev_param, const uint8_t *buffer, size_t length)
static int dtc_queue_run(void)
static struct dtc_reply_queue_entry * dtc_queue_enqueue_reply(enum scan_type type, uint8_t *buffer, int size, int offset, int length, struct jtag_command *cmd)
static int dtc_queue_init(void)
static struct libusb_device_handle * hdev
static int tap_state_queue_append(uint8_t tms)
#define ST7_PE_ADAPTER_SENSE_IN
static int rlink_khz(int khz, int *speed)
static ssize_t ep1_memory_write(struct libusb_device_handle *hdev_param, uint16_t addr, size_t length, uint8_t const *buffer)
static struct jtag_interface rlink_interface
static int dtc_run_download(struct libusb_device_handle *hdev_param, uint8_t *command_buffer, int command_buffer_size, uint8_t *reply_buffer, int reply_buffer_size)
static void rlink_path_move(struct pathmove_command *cmd)
static int rlink_init(void)
#define ST7_PE_ADAPTER_SENSE_OUT
static int ep1_generic_commandl(struct libusb_device_handle *hdev_param, size_t length,...)
static struct @34 tap_state_queue
static void rlink_end_state(enum tap_state state)
uint8_t cmd_buffer[USB_EP2BANK_SIZE]
static int dtc_start_download(void)
static int rlink_scan(struct jtag_command *cmd, enum scan_type type, uint8_t *buffer, int scan_size)
static int rlink_speed(int speed)
#define DTCLOAD_LUT_START
struct dtc_reply_queue_entry * rq_head
static uint8_t dtc_entry_download
static int tap_state_queue_run(void)
static int rlink_quit(void)
static struct @33 dtc_queue
static int tap_state_queue_init(void)
struct adapter_driver rlink_adapter_driver
static int rlink_execute_queue(struct jtag_command *cmd_queue)
struct dtc_reply_queue_entry * rq_tail
static void rlink_state_move(void)
static void rlink_runtest(unsigned int num_cycles)
static int dtc_queue_run_if_full(int reserved_cmd, int reserved_reply)
static void rlink_reset(int trst, int srst)
static int rlink_speed_div(int speed, int *khz)
#define DTC_STATUS_POLL_BYTE
const size_t rlink_speed_table_size
#define DTC_CMD_SHIFT_TMS_TDI_BIT_PAIR(tms, tdi, tdo)
#define DTC_CMD_SHIFT_TMS_BITS(bits)
#define DTC_CMD_SHIFT_TDO_BYTES(bytes)
#define DTC_CMD_SHIFT_TDIO_BYTES(bytes)
#define DTC_CMD_SHIFT_TMS_BYTES(bytes)
#define DTC_CMD_SHIFT_TDI_BYTES(bytes)
#define DTC_CMD_SHIFT_TDIO_BITS(bits)
#define EP1_CMD_MEMORY_READ
#define EP1_CMD_LEDUE_BUSY
#define EP1_CMD_SET_PORTD_LEDS
#define EP1_CMD_DTC_GET_CACHED_STATUS
#define EP1_CMD_SET_PORTD_VPP
#define EP1_CMD_SET_UPLOAD
#define EP1_CMD_LEDUE_NONE
#define EP1_CMD_MEMORY_WRITE
#define EP1_CMD_SET_DOWNLOAD
#define EP1_CMD_GET_FWREV
#define ST7_EP2TXR_DTOG_TX
#define ST7_USB_BUF_EP0OUT
#define ST7_EP2TXR_STAT_VALID
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.
struct dtc_reply_queue_entry::@35 scan
struct dtc_reply_queue_entry * next
struct jtag_command * cmd
union jtag_command_container cmd
Represents a driver for a debugging interface.
int(* execute_queue)(struct jtag_command *cmd_queue)
Execute commands in the supplied queue.
struct scan_command * scan