30 #define CKLINK_VID_BOUFFALO 0x42bf
31 #define CKLINK_VID_THEAD 0x32bf
32 #define CKLINK_PID_LITE_V2 0xb210
34 #define CKLINK_USB_INTERFACE 0
35 #define CKLINK_EP_OUT 0x02
36 #define CKLINK_EP_IN 0x81
37 #define CKLINK_USB_TIMEOUT_MS 1000
38 #define CKLINK_USB_BUF_SIZE 2048
41 #define CKLINK_FRAME_START 0x68
42 #define CKLINK_FRAME_END 0x16
45 #define CKLINK_FRAME_OVERHEAD 4
46 #define CKLINK_RESP_PAYLOAD_OFFSET 2
49 #define CKLINK_OP_SELFREG_WRITE 0x06
50 #define CKLINK_OP_SELFREG_READ 0x87
51 #define CKLINK_OP_JTAG_BATCH 0x88
54 #define CKLINK_SR_CSR 0
55 #define CKLINK_SR_MTCR_WAIT 1
56 #define CKLINK_SR_JTAG_CONFIG 8
57 #define CKLINK_SELFREG_BYTES 4
66 #define CKLINK_SR0_SETUP_CLK_BYTE 0x17
67 #define CKLINK_SR0_RUN_CLK_BYTE 0x12
68 #define CKLINK_SR0_MODE_JTAG 0x60
69 #define CKLINK_SR0_MODE_CJTAG 0x61
70 #define CKLINK_SR1_INIT_VALUE 0x000003e8
71 #define CKLINK_SR8_INIT_VALUE 0x00622250
74 #define CKLINK_CLK_SOURCE_KHZ 48000
75 #define CKLINK_CLK_DIV_MIN CKLINK_SR0_RUN_CLK_BYTE
76 #define CKLINK_CLK_DIV_MAX 0xff
78 #define CKLINK_CSR(clk, mode) (((uint32_t)(mode) << 24) | (clk))
81 #define CKLINK_MAX_SCAN_BITS 255
82 #define CKLINK_MAX_SCAN_BYTES 32
85 #define CKLINK_PLACEHOLDER_IR_BITS 5
86 #define CKLINK_PLACEHOLDER_IR_VALUE 0x01
121 for (
unsigned int i = 0; i < len; i++)
129 unsigned int txlen,
unsigned int *rxlen)
133 (
char *)ck->
tx_buf, txlen,
135 if (ret !=
ERROR_OK || (
unsigned int)actual != txlen) {
136 LOG_ERROR(
"CK-Link bulk write failed (ret=%d, actual=%d/%d)",
148 LOG_ERROR(
"CK-Link bulk read failed (ret=%d, actual=%d)",
153 LOG_ERROR(
"CK-Link bad response start byte 0x%02x",
158 LOG_ERROR(
"CK-Link bad response end byte 0x%02x",
168 if (computed != ck->
rx_buf[actual - 2]) {
169 LOG_ERROR(
"CK-Link response checksum mismatch: op=0x%02x stated=0x%02x computed=0x%02x",
170 opcode, ck->
rx_buf[actual - 2], computed);
175 LOG_DEBUG(
"CK-Link op=0x%02x returned status=0x%02x",
178 *rxlen = (
unsigned int)actual;
198 const uint8_t *value)
214 unsigned int rxlen = 0;
220 if (rxlen < expected) {
221 LOG_ERROR(
"CK-Link selfreg read response too short (%u < %u)",
234 unsigned int ir_bits,
const uint8_t *ir_tdi,
236 unsigned int dr_bits,
const uint8_t *dr_tdi,
241 LOG_WARNING(
"CK-Link: scan exceeds %u-bit per-entry limit (ir=%u dr=%u); clamping",
257 memcpy(&ck->
tx_buf[pos], ir_tdi, ir_bytes);
259 memset(&ck->
tx_buf[pos], 0, ir_bytes);
261 ck->
tx_buf[pos++] = (uint8_t)dr_bits;
263 memcpy(&ck->
tx_buf[pos], dr_tdi, dr_bytes);
265 memset(&ck->
tx_buf[pos], 0, dr_bytes);
269 unsigned int rxlen = 0;
275 const unsigned int expected =
277 if (rxlen < expected) {
278 LOG_ERROR(
"CK-Link scan response truncated: got %u, need %u (ir=%u dr=%u)",
279 rxlen, expected,
ir_bits, dr_bits);
283 if (ir_tdo && ir_bytes)
286 const unsigned int dr_tdo_offset =
289 memcpy(dr_tdo, &ck->
rx_buf[dr_tdo_offset], dr_bytes);
299 while (num_bits > 0) {
320 uint8_t dummy_dr = 0;
327 unsigned int off = 0;
328 for (
unsigned int i = 0; i <
scan->num_fields; i++) {
345 unsigned int total_bits = 0;
346 for (
unsigned int i = 0; i <
scan->num_fields; i++)
347 total_bits +=
scan->fields[i].num_bits;
352 const unsigned int total_bytes =
DIV_ROUND_UP(total_bits, 8);
354 uint8_t *ntdi = realloc(ck->
scan_tdi, total_bytes);
355 uint8_t *ntdo = realloc(ck->
scan_tdo, total_bytes);
356 if (!ntdi || !ntdo) {
357 LOG_ERROR(
"CK-Link: out of memory for %u-bit scan",
370 memset(ck->
scan_tdi, 0, total_bytes);
371 memset(ck->
scan_tdo, 0, total_bytes);
374 for (
unsigned int i = 0; i <
scan->num_fields; i++) {
390 if (total_bytes >
sizeof(ck->
ir_value)) {
391 LOG_ERROR(
"CK-Link: IR scan of %u bits exceeds cache (%zu bytes)",
397 LOG_WARNING(
"CK-Link: back-to-back IR scan dropped pending IR; multi-TAP chains are not supported");
400 LOG_DEBUG(
"CK-Link: dropping pending IR (%u bits) before new IR scan",
414 const uint8_t *eff_ir_value = ck->
ir_bits ? ck->
ir_value : &placeholder_ir;
416 LOG_DEBUG(
"CK-Link: DR scan with no cached IR; injecting IDCODE placeholder");
427 unsigned int ir_off = 0;
428 for (
unsigned int i = 0; i <
ir_scan->num_fields; i++) {
451 for (
unsigned int i = 0; i <
scan->num_fields; i++) {
519 LOG_ERROR(
"CK-Link: JTAG_TMS (%u bits) is not supported by this probe",
520 cmd->cmd.tms->num_bits);
527 if (
cmd->cmd.reset->trst)
556 LOG_ERROR(
"CK-Link: JTAG_PATHMOVE is not supported by this probe");
570 LOG_ERROR(
"CK-Link: unsupported JTAG command %d",
611 LOG_WARNING(
"CK-Link: sr0 mode byte is 0x%02x, expected 0x%02x (5-wire JTAG)",
618 const uint8_t dummy_dr = 0;
625 struct cklink_ctx *ck = calloc(1,
sizeof(*ck));
633 if (!vids[0] || !pids[0]) {
646 libusb_set_auto_detach_kernel_driver(ck->
usb_dev, 1);
650 LOG_ERROR(
"CK-Link: failed to claim interface %d: %s",
658 LOG_ERROR(
"CK-Link: probe initialization failed");
667 LOG_INFO(
"CK-Link Lite V2 initialized (5-wire JTAG)");
700 LOG_DEBUG(
"CK-Link: adapter speed: requested %d kHz, programmed %d kHz (div=0x%02x)",
708 LOG_ERROR(
"CK-Link: adaptive clocking (RTCK) is not supported");
713 if (achievable != khz) {
714 static int last_clamp_khz;
715 if (khz != last_clamp_khz) {
716 LOG_WARNING(
"CK-Link: requested %d kHz clamped to %d kHz (div=0x%02x)",
717 khz, achievable, div);
718 last_clamp_khz = khz;
721 *jtag_speed = achievable;
const char * adapter_get_required_serial(void)
Retrieves the serial number set with command 'adapter serial'.
const uint16_t * adapter_usb_get_pids(void)
const uint16_t * adapter_usb_get_vids(void)
void * buf_set_buf(const void *_src, unsigned int src_start, void *_dst, unsigned int dst_start, unsigned int len)
Support functions to access arbitrary bits in a byte array.
#define CKLINK_USB_INTERFACE
static const uint16_t cklink_default_vids[]
#define CKLINK_SR0_MODE_CJTAG
#define CKLINK_SR1_INIT_VALUE
static uint8_t cklink_khz_to_div(int khz)
#define CKLINK_SR0_RUN_CLK_BYTE
static int cklink_idle_cycles(struct cklink_ctx *ck, unsigned int num_bits)
static int cklink_execute_runtest(struct cklink_ctx *ck, struct jtag_command *cmd)
#define CKLINK_OP_JTAG_BATCH
static int cklink_speed_div(int speed, int *khz)
#define CKLINK_PLACEHOLDER_IR_BITS
#define CKLINK_PID_LITE_V2
static int cklink_execute_tlr_reset(struct cklink_ctx *ck)
#define CKLINK_SR8_INIT_VALUE
#define CKLINK_USB_TIMEOUT_MS
#define CKLINK_CLK_DIV_MIN
static int cklink_execute_reset(struct cklink_ctx *ck, struct jtag_command *cmd)
#define CKLINK_CLK_SOURCE_KHZ
static int cklink_execute_scan(struct cklink_ctx *ck, struct jtag_command *cmd)
#define CKLINK_SR_MTCR_WAIT
#define CKLINK_VID_BOUFFALO
#define CKLINK_PLACEHOLDER_IR_VALUE
static int cklink_quit(void)
static int cklink_execute_stableclocks(struct cklink_ctx *ck, struct jtag_command *cmd)
#define CKLINK_USB_BUF_SIZE
#define CKLINK_OP_SELFREG_WRITE
static int cklink_flush_pending_ir(struct cklink_ctx *ck)
static int cklink_speed(int speed)
static int cklink_usb_xfer(struct cklink_ctx *ck, uint8_t opcode, unsigned int txlen, unsigned int *rxlen)
#define CKLINK_SR0_MODE_JTAG
static int cklink_init(void)
static int cklink_execute_tms(struct jtag_command *cmd)
#define CKLINK_FRAME_OVERHEAD
#define CKLINK_SELFREG_BYTES
#define CKLINK_CSR(clk, mode)
#define CKLINK_FRAME_START
static int cklink_selfreg_read(struct cklink_ctx *ck, uint8_t reg, uint8_t *value)
#define CKLINK_SR0_SETUP_CLK_BYTE
#define CKLINK_MAX_SCAN_BYTES
static int cklink_khz(int khz, int *jtag_speed)
static unsigned int cklink_frame_end(struct cklink_ctx *ck, unsigned int pos)
static int cklink_selfreg_write(struct cklink_ctx *ck, uint8_t reg, const uint8_t *value)
#define CKLINK_OP_SELFREG_READ
static struct jtag_interface cklink_jtag_interface
#define CKLINK_CLK_DIV_MAX
static int cklink_jtag_scan(struct cklink_ctx *ck, unsigned int ir_bits, const uint8_t *ir_tdi, uint8_t *ir_tdo, unsigned int dr_bits, const uint8_t *dr_tdi, uint8_t *dr_tdo)
static int cklink_probe_init(struct cklink_ctx *ck)
static const uint16_t cklink_default_pids[]
#define CKLINK_SR_JTAG_CONFIG
#define CKLINK_RESP_PAYLOAD_OFFSET
static int cklink_execute_queue(struct jtag_command *cmd_queue)
static uint8_t cklink_checksum(const uint8_t *buf, unsigned int len)
#define CKLINK_MAX_SCAN_BITS
static int cklink_write_csr(struct cklink_ctx *ck, uint8_t clk, uint8_t mode)
struct adapter_driver cklink_adapter_driver
static struct cklink_ctx * cklink_handle
static unsigned int cklink_frame_start(struct cklink_ctx *ck, uint8_t opcode)
#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)
#define ERROR_JTAG_QUEUE_FAILED
#define ERROR_JTAG_INIT_FAILED
#define ERROR_JTAG_NOT_IMPLEMENTED
static struct scan_blk scan
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)
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)
#define LOG_WARNING(expr ...)
#define LOG_ERROR(expr ...)
#define LOG_INFO(expr ...)
#define LOG_DEBUG(expr ...)
const unsigned char bit_offset
Represents a driver for a debugging interface.
const char *const name
The name of the interface driver.
uint8_t ir_value[CKLINK_MAX_SCAN_BYTES]
uint8_t rx_buf[CKLINK_USB_BUF_SIZE]
struct jtag_command * pending_ir_cmd
struct libusb_device_handle * usb_dev
uint8_t tx_buf[CKLINK_USB_BUF_SIZE]
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.
The scan_command provide a means of encapsulating a set of scan_field structures that should be scann...
bool ir_scan
instruction/not data scan
struct scan_field * fields
pointer to an array of data scan fields
This structure defines a single scan field in the scan.
uint8_t * in_value
A pointer to a 32-bit memory location for data scanned out.
const uint8_t * out_value
A pointer to value to be scanned into the device.
unsigned int num_bits
The number of bits this field specifies.
static void h_u32_to_le(uint8_t *buf, uint32_t val)
#define DIV_ROUND_UP(m, n)
Rounds m up to the nearest multiple of n using division.
struct scan_command * scan