OpenOCD
cklink.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * T-Head CK-Link JTAG adapter driver. *
5  * *
6  * Supports the CK-Link Lite V2 wire protocol used by T-Head probes and *
7  * Bouffalo Lab BL616/BL702 CK-Link clones. USB bulk with a framed *
8  * command protocol reverse-engineered from T-Head's DebugServer. *
9  * *
10  * Copyright (C) 2026 by William Markezana *
11  * <william.markezana@gmail.com> *
12  * *
13  ***************************************************************************/
14 
15 #ifdef HAVE_CONFIG_H
16 #include "config.h"
17 #endif
18 
19 #include <assert.h>
20 
21 #include <jtag/adapter.h>
22 #include <jtag/interface.h>
23 #include <jtag/commands.h>
24 #include <helper/binarybuffer.h>
25 #include <helper/bits.h>
26 #include <helper/time_support.h>
27 #include "libusb_helper.h"
28 
29 // USB identification. Defaults can be overridden via `adapter usb vid_pid`.
30 #define CKLINK_VID_BOUFFALO 0x42bf
31 #define CKLINK_VID_THEAD 0x32bf
32 #define CKLINK_PID_LITE_V2 0xb210
33 
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
39 
40 // Wire-protocol framing bytes
41 #define CKLINK_FRAME_START 0x68
42 #define CKLINK_FRAME_END 0x16
43 
44 // Response frame layout: start + status + payload + checksum + end
45 #define CKLINK_FRAME_OVERHEAD 4
46 #define CKLINK_RESP_PAYLOAD_OFFSET 2
47 
48 // Opcodes
49 #define CKLINK_OP_SELFREG_WRITE 0x06
50 #define CKLINK_OP_SELFREG_READ 0x87
51 #define CKLINK_OP_JTAG_BATCH 0x88
52 
53 // Self-register indices
54 #define CKLINK_SR_CSR 0 // clock divider + CDI mode
55 #define CKLINK_SR_MTCR_WAIT 1 // target-comm wait cycles
56 #define CKLINK_SR_JTAG_CONFIG 8 // JTAG config word
57 #define CKLINK_SELFREG_BYTES 4
58 
59 /*
60  * Init values replayed from a captured DebugServer session.
61  * sr0 (CSR): byte 0 = clock divider, byte 3 = CDI mode (0x60=5-wire JTAG,
62  * 0x61=2-wire cJTAG). The 5->2->5 mode toggle at end of init acts as a
63  * TAP reset: the wire driver re-initializes on each switch.
64  * sr1 MTCR_WAIT = 1000 cycles. sr8 JTAG_CONFIG: opaque, known-good.
65  */
66 #define CKLINK_SR0_SETUP_CLK_BYTE 0x17 // ~2000 kHz
67 #define CKLINK_SR0_RUN_CLK_BYTE 0x12 // ~2526 kHz, DebugServer default
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
72 
73 // freq_kHz = 48000 / (N + 1); cap at DebugServer-validated 0x12.
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
77 
78 #define CKLINK_CSR(clk, mode) (((uint32_t)(mode) << 24) | (clk))
79 
80 // Batch opcode encodes IR/DR bit-counts in single bytes.
81 #define CKLINK_MAX_SCAN_BITS 255
82 #define CKLINK_MAX_SCAN_BYTES 32 // DIV_ROUND_UP(255, 8)
83 
84 // Synthesized IR when a DR scan arrives without a cached IR.
85 #define CKLINK_PLACEHOLDER_IR_BITS 5
86 #define CKLINK_PLACEHOLDER_IR_VALUE 0x01
87 
88 struct cklink_ctx {
89  struct libusb_device_handle *usb_dev;
92  uint8_t csr_clk;
94  unsigned int ir_bits;
96  uint8_t *scan_tdi;
97  uint8_t *scan_tdo;
98  size_t scan_buf_size;
101 };
102 
103 static struct cklink_ctx *cklink_handle;
104 
105 // Built-in VID/PID list, used when `adapter usb vid_pid` is not set.
106 static const uint16_t cklink_default_vids[] = {
109  0,
110 };
111 static const uint16_t cklink_default_pids[] = {
114  0,
115 };
116 
117 // Sum-mod-256.
118 static uint8_t cklink_checksum(const uint8_t *buf, unsigned int len)
119 {
120  uint8_t sum = 0;
121  for (unsigned int i = 0; i < len; i++)
122  sum += buf[i];
123  return sum;
124 }
125 
126 /* Send a framed command; NULL rxlen means fire-and-forget (selfreg writes
127  * are not acknowledged). @p opcode selects the response-checksum rule. */
128 static int cklink_usb_xfer(struct cklink_ctx *ck, uint8_t opcode,
129  unsigned int txlen, unsigned int *rxlen)
130 {
131  int actual = 0;
133  (char *)ck->tx_buf, txlen,
134  CKLINK_USB_TIMEOUT_MS, &actual);
135  if (ret != ERROR_OK || (unsigned int)actual != txlen) {
136  LOG_ERROR("CK-Link bulk write failed (ret=%d, actual=%d/%d)",
137  ret, actual, txlen);
138  return ERROR_FAIL;
139  }
140 
141  if (!rxlen)
142  return ERROR_OK;
143 
145  (char *)ck->rx_buf, CKLINK_USB_BUF_SIZE,
146  CKLINK_USB_TIMEOUT_MS, &actual);
147  if (ret != ERROR_OK || actual < (int)CKLINK_FRAME_OVERHEAD) {
148  LOG_ERROR("CK-Link bulk read failed (ret=%d, actual=%d)",
149  ret, actual);
150  return ERROR_FAIL;
151  }
152  if (ck->rx_buf[0] != CKLINK_FRAME_START) {
153  LOG_ERROR("CK-Link bad response start byte 0x%02x",
154  ck->rx_buf[0]);
155  return ERROR_FAIL;
156  }
157  if (ck->rx_buf[actual - 1] != CKLINK_FRAME_END) {
158  LOG_ERROR("CK-Link bad response end byte 0x%02x",
159  ck->rx_buf[actual - 1]);
160  return ERROR_FAIL;
161  }
162  /*
163  * RX checksum is opcode-specific: selfreg reads sum all payload;
164  * single-entry JTAG batch sums all except the last byte of dr_tdo.
165  */
166  unsigned int csum_end = actual - ((opcode == CKLINK_OP_JTAG_BATCH) ? 3 : 2);
167  uint8_t computed = cklink_checksum(ck->rx_buf, csum_end);
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);
171  return ERROR_FAIL;
172  }
173  // Status byte semantics undocumented; log non-zero for traces.
174  if (ck->rx_buf[1] != 0) {
175  LOG_DEBUG("CK-Link op=0x%02x returned status=0x%02x",
176  opcode, ck->rx_buf[1]);
177  }
178  *rxlen = (unsigned int)actual;
179  return ERROR_OK;
180 }
181 
182 static unsigned int cklink_frame_start(struct cklink_ctx *ck, uint8_t opcode)
183 {
184  ck->tx_buf[0] = CKLINK_FRAME_START;
185  ck->tx_buf[1] = opcode;
186  return 2;
187 }
188 
189 static unsigned int cklink_frame_end(struct cklink_ctx *ck, unsigned int pos)
190 {
191  assert(pos + 2 <= CKLINK_USB_BUF_SIZE);
192  ck->tx_buf[pos] = cklink_checksum(ck->tx_buf, pos);
193  ck->tx_buf[pos + 1] = CKLINK_FRAME_END;
194  return pos + 2;
195 }
196 
197 static int cklink_selfreg_write(struct cklink_ctx *ck, uint8_t reg,
198  const uint8_t *value)
199 {
200  unsigned int pos = cklink_frame_start(ck, CKLINK_OP_SELFREG_WRITE);
201  ck->tx_buf[pos++] = reg;
202  memcpy(&ck->tx_buf[pos], value, CKLINK_SELFREG_BYTES);
203  pos += CKLINK_SELFREG_BYTES;
204  pos = cklink_frame_end(ck, pos);
205  return cklink_usb_xfer(ck, CKLINK_OP_SELFREG_WRITE, pos, NULL);
206 }
207 
208 static int cklink_selfreg_read(struct cklink_ctx *ck, uint8_t reg, uint8_t *value)
209 {
210  unsigned int pos = cklink_frame_start(ck, CKLINK_OP_SELFREG_READ);
211  ck->tx_buf[pos++] = reg;
212  pos = cklink_frame_end(ck, pos);
213 
214  unsigned int rxlen = 0;
215  int ret = cklink_usb_xfer(ck, CKLINK_OP_SELFREG_READ, pos, &rxlen);
216  if (ret != ERROR_OK)
217  return ret;
218 
219  const unsigned int expected = CKLINK_FRAME_OVERHEAD + CKLINK_SELFREG_BYTES;
220  if (rxlen < expected) {
221  LOG_ERROR("CK-Link selfreg read response too short (%u < %u)",
222  rxlen, expected);
223  return ERROR_FAIL;
224  }
225  memcpy(value, &ck->rx_buf[CKLINK_RESP_PAYLOAD_OFFSET],
227  return ERROR_OK;
228 }
229 
230 /* Single-entry batch scan. Scans > 255 bits are clamped with a one-time
231  * warning: chunking across entries breaks Shift-DR continuity, and
232  * erroring out here breaks OpenOCD's chain probe's -expected-id recovery. */
233 static int cklink_jtag_scan(struct cklink_ctx *ck,
234  unsigned int ir_bits, const uint8_t *ir_tdi,
235  uint8_t *ir_tdo,
236  unsigned int dr_bits, const uint8_t *dr_tdi,
237  uint8_t *dr_tdo)
238 {
240  && !ck->long_scan_warned) {
241  LOG_WARNING("CK-Link: scan exceeds %u-bit per-entry limit (ir=%u dr=%u); clamping",
242  CKLINK_MAX_SCAN_BITS, ir_bits, dr_bits);
243  ck->long_scan_warned = true;
244  }
247  if (dr_bits > CKLINK_MAX_SCAN_BITS)
248  dr_bits = CKLINK_MAX_SCAN_BITS;
249 
250  const unsigned int ir_bytes = DIV_ROUND_UP(ir_bits, 8);
251  const unsigned int dr_bytes = DIV_ROUND_UP(dr_bits, 8);
252 
253  unsigned int pos = cklink_frame_start(ck, CKLINK_OP_JTAG_BATCH);
254  ck->tx_buf[pos++] = 0; // entry_count - 1
255  ck->tx_buf[pos++] = (uint8_t)ir_bits;
256  if (ir_tdi)
257  memcpy(&ck->tx_buf[pos], ir_tdi, ir_bytes);
258  else
259  memset(&ck->tx_buf[pos], 0, ir_bytes);
260  pos += ir_bytes;
261  ck->tx_buf[pos++] = (uint8_t)dr_bits;
262  if (dr_tdi)
263  memcpy(&ck->tx_buf[pos], dr_tdi, dr_bytes);
264  else
265  memset(&ck->tx_buf[pos], 0, dr_bytes);
266  pos += dr_bytes;
267  pos = cklink_frame_end(ck, pos);
268 
269  unsigned int rxlen = 0;
270  int ret = cklink_usb_xfer(ck, CKLINK_OP_JTAG_BATCH, pos, &rxlen);
271  if (ret != ERROR_OK)
272  return ret;
273 
274  // Response: start + status + ir_tdo + dr_echo + dr_tdo + csum + end.
275  const unsigned int expected =
276  CKLINK_FRAME_OVERHEAD + ir_bytes + 1 + dr_bytes;
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);
280  return ERROR_FAIL;
281  }
282 
283  if (ir_tdo && ir_bytes)
284  memcpy(ir_tdo, &ck->rx_buf[CKLINK_RESP_PAYLOAD_OFFSET], ir_bytes);
285 
286  const unsigned int dr_tdo_offset =
287  CKLINK_RESP_PAYLOAD_OFFSET + ir_bytes + 1;
288  if (dr_tdo)
289  memcpy(dr_tdo, &ck->rx_buf[dr_tdo_offset], dr_bytes);
290 
291  return ERROR_OK;
292 }
293 
294 // Shift num_bits of idle cycles, chunked to the 255-bit protocol limit.
295 static int cklink_idle_cycles(struct cklink_ctx *ck, unsigned int num_bits)
296 {
297  static const uint8_t zeros[CKLINK_MAX_SCAN_BYTES] = { 0 };
298 
299  while (num_bits > 0) {
300  unsigned int chunk = num_bits > CKLINK_MAX_SCAN_BITS
301  ? CKLINK_MAX_SCAN_BITS : num_bits;
302  int ret = cklink_jtag_scan(ck, 0, NULL, NULL,
303  chunk, zeros, NULL);
304  if (ret != ERROR_OK)
305  return ret;
306  num_bits -= chunk;
307  }
308  return ERROR_OK;
309 }
310 
311 /* Flush a pending IR with a 1-bit dummy DR so its in_value gets populated
312  * with real captured IR TDO. Called at end of execute_queue so Capture-IR
313  * validation sees real hardware data. */
314 static int cklink_flush_pending_ir(struct cklink_ctx *ck)
315 {
316  if (!ck || !ck->pending_ir_cmd)
317  return ERROR_OK;
318 
319  uint8_t ir_tdo[CKLINK_MAX_SCAN_BYTES] = { 0 };
320  uint8_t dummy_dr = 0;
321  int ret = cklink_jtag_scan(ck, ck->ir_bits, ck->ir_value, ir_tdo,
322  1, &dummy_dr, NULL);
323  if (ret != ERROR_OK)
324  return ret;
325 
326  struct scan_command *scan = ck->pending_ir_cmd->cmd.scan;
327  unsigned int off = 0;
328  for (unsigned int i = 0; i < scan->num_fields; i++) {
329  struct scan_field *f = &scan->fields[i];
330  if (f->in_value) {
331  buf_set_buf(ir_tdo, off, f->in_value, 0,
332  f->num_bits);
333  }
334  off += f->num_bits;
335  }
336  ck->pending_ir_cmd = NULL;
337  // ir_bits/ir_value stay valid: hardware IR is still what we cached.
338  return ERROR_OK;
339 }
340 
341 static int cklink_execute_scan(struct cklink_ctx *ck, struct jtag_command *cmd)
342 {
343  struct scan_command *scan = cmd->cmd.scan;
344 
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;
348 
349  if (total_bits == 0)
350  return ERROR_OK;
351 
352  const unsigned int total_bytes = DIV_ROUND_UP(total_bits, 8);
353  if (total_bytes > ck->scan_buf_size) {
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",
358  total_bits);
359  // Partial success is fine; freed in cklink_quit.
360  if (ntdi)
361  ck->scan_tdi = ntdi;
362  if (ntdo)
363  ck->scan_tdo = ntdo;
364  return ERROR_FAIL;
365  }
366  ck->scan_tdi = ntdi;
367  ck->scan_tdo = ntdo;
368  ck->scan_buf_size = total_bytes;
369  }
370  memset(ck->scan_tdi, 0, total_bytes);
371  memset(ck->scan_tdo, 0, total_bytes);
372 
373  unsigned int bit_offset = 0;
374  for (unsigned int i = 0; i < scan->num_fields; i++) {
375  struct scan_field *field = &scan->fields[i];
376  if (field->out_value) {
377  buf_set_buf(field->out_value, 0, ck->scan_tdi,
378  bit_offset, field->num_bits);
379  }
380  bit_offset += field->num_bits;
381  }
382 
383  if (scan->ir_scan) {
384  /*
385  * Defer IR until paired with a DR (batch needs both).
386  * Back-to-back IR drops the first: mid-queue flush would
387  * cycle Capture-DR/Update-DR on the previous IR's register
388  * (e.g. DMI), triggering spurious transactions.
389  */
390  if (total_bytes > sizeof(ck->ir_value)) {
391  LOG_ERROR("CK-Link: IR scan of %u bits exceeds cache (%zu bytes)",
392  total_bits, sizeof(ck->ir_value));
394  }
395  if (ck->pending_ir_cmd) {
396  if (!ck->dropped_ir_warned) {
397  LOG_WARNING("CK-Link: back-to-back IR scan dropped pending IR; multi-TAP chains are not supported");
398  ck->dropped_ir_warned = true;
399  } else {
400  LOG_DEBUG("CK-Link: dropping pending IR (%u bits) before new IR scan",
401  ck->ir_bits);
402  }
403  }
404  memcpy(ck->ir_value, ck->scan_tdi, total_bytes);
405  ck->ir_bits = total_bits;
406  ck->pending_ir_cmd = cmd;
407  return ERROR_OK;
408  }
409 
410  // No cached IR yet: inject IDCODE placeholder so chain detection works.
411  const uint8_t placeholder_ir = CKLINK_PLACEHOLDER_IR_VALUE;
412  const unsigned int eff_ir_bits = ck->ir_bits ? ck->ir_bits
414  const uint8_t *eff_ir_value = ck->ir_bits ? ck->ir_value : &placeholder_ir;
415  if (!ck->ir_bits)
416  LOG_DEBUG("CK-Link: DR scan with no cached IR; injecting IDCODE placeholder");
417 
418  uint8_t ir_tdo_buf[CKLINK_MAX_SCAN_BYTES] = { 0 };
419  int ret = cklink_jtag_scan(ck, eff_ir_bits, eff_ir_value, ir_tdo_buf,
420  total_bits, ck->scan_tdi, ck->scan_tdo);
421  if (ret != ERROR_OK)
422  return ret;
423 
424  // Scatter captured IR TDO back into the deferred IR command's fields.
425  if (ck->pending_ir_cmd) {
426  struct scan_command *ir_scan = ck->pending_ir_cmd->cmd.scan;
427  unsigned int ir_off = 0;
428  for (unsigned int i = 0; i < ir_scan->num_fields; i++) {
429  struct scan_field *f = &ir_scan->fields[i];
430  if (f->in_value) {
431  buf_set_buf(ir_tdo_buf, ir_off,
432  f->in_value, 0, f->num_bits);
433  }
434  ir_off += f->num_bits;
435  }
436  ck->pending_ir_cmd = NULL;
437  }
438 
439  /*
440  * For clamped scans, fill un-shifted TDO with caller's TDI (models
441  * "no more TAPs past the limit" - a bare wire). Gives OpenOCD's
442  * bypass chain-length probe the expected end-of-chain marker.
443  */
444  if (total_bits > CKLINK_MAX_SCAN_BITS) {
447  total_bits - CKLINK_MAX_SCAN_BITS);
448  }
449 
450  bit_offset = 0;
451  for (unsigned int i = 0; i < scan->num_fields; i++) {
452  struct scan_field *field = &scan->fields[i];
453  if (field->in_value) {
455  field->in_value, 0, field->num_bits);
456  }
457  bit_offset += field->num_bits;
458  }
459 
460  return ERROR_OK;
461 }
462 
463 static int cklink_write_csr(struct cklink_ctx *ck, uint8_t clk, uint8_t mode)
464 {
465  uint8_t buf[CKLINK_SELFREG_BYTES];
466  h_u32_to_le(buf, CKLINK_CSR(clk, mode));
467  return cklink_selfreg_write(ck, CKLINK_SR_CSR, buf);
468 }
469 
470 // khz -> sr0 byte 0 divider, rounded up so we never exceed the request.
471 static uint8_t cklink_khz_to_div(int khz)
472 {
473  if (khz <= 0)
474  return CKLINK_CLK_DIV_MAX;
475  int n = ((CKLINK_CLK_SOURCE_KHZ + khz - 1) / khz) - 1;
476  if (n < CKLINK_CLK_DIV_MIN)
477  n = CKLINK_CLK_DIV_MIN;
478  if (n > CKLINK_CLK_DIV_MAX)
479  n = CKLINK_CLK_DIV_MAX;
480  return (uint8_t)n;
481 }
482 
483 static int cklink_execute_tlr_reset(struct cklink_ctx *ck)
484 {
485  // Probe-side reset via 5->2->5 mode toggle; target TAP reset not guaranteed.
486  int ret;
487 
489  if (ret != ERROR_OK)
490  return ret;
492  if (ret != ERROR_OK)
493  return ret;
495  if (ret != ERROR_OK)
496  return ret;
497 
498  ck->ir_bits = 0;
499  memset(ck->ir_value, 0, sizeof(ck->ir_value));
500  ck->pending_ir_cmd = NULL;
502  return ERROR_OK;
503 }
504 
505 static int cklink_execute_runtest(struct cklink_ctx *ck, struct jtag_command *cmd)
506 {
507  return cklink_idle_cycles(ck, cmd->cmd.runtest->num_cycles);
508 }
509 
511 {
512  // TMS=0 shifts only hold state in RTI; other stable states would advance.
513  return cklink_idle_cycles(ck, cmd->cmd.stableclocks->num_cycles);
514 }
515 
517 {
518  // No TMS-sequence opcode: honoring JTAG_TMS is impossible.
519  LOG_ERROR("CK-Link: JTAG_TMS (%u bits) is not supported by this probe",
520  cmd->cmd.tms->num_bits);
522 }
523 
524 static int cklink_execute_reset(struct cklink_ctx *ck, struct jtag_command *cmd)
525 {
526  // Only TRST is meaningful here: we have no separate SRST control.
527  if (cmd->cmd.reset->trst)
528  return cklink_execute_tlr_reset(ck);
529  return ERROR_OK;
530 }
531 
532 static int cklink_execute_queue(struct jtag_command *cmd_queue)
533 {
534  struct cklink_ctx *ck = cklink_handle;
535  if (!ck)
536  return ERROR_JTAG_INIT_FAILED;
537 
538  for (struct jtag_command *cmd = cmd_queue; cmd; cmd = cmd->next) {
539  int ret;
540 
541  switch (cmd->type) {
542  case JTAG_SCAN:
543  ret = cklink_execute_scan(ck, cmd);
544  break;
545  case JTAG_TLR_RESET:
546  ret = cklink_execute_tlr_reset(ck);
547  break;
548  case JTAG_RUNTEST:
549  ret = cklink_execute_runtest(ck, cmd);
550  break;
551  case JTAG_RESET:
552  ret = cklink_execute_reset(ck, cmd);
553  break;
554  case JTAG_PATHMOVE:
555  // No TMS-sequence opcode: honoring PATHMOVE is impossible.
556  LOG_ERROR("CK-Link: JTAG_PATHMOVE is not supported by this probe");
558  break;
559  case JTAG_STABLECLOCKS:
560  ret = cklink_execute_stableclocks(ck, cmd);
561  break;
562  case JTAG_TMS:
563  ret = cklink_execute_tms(cmd);
564  break;
565  case JTAG_SLEEP:
566  jtag_sleep(cmd->cmd.sleep->us);
567  ret = ERROR_OK;
568  break;
569  default:
570  LOG_ERROR("CK-Link: unsupported JTAG command %d",
571  cmd->type);
573  }
574  if (ret != ERROR_OK)
575  return ret;
576  }
577  // Flush so any un-paired IR's in_value is populated before OpenOCD reads.
578  return cklink_flush_pending_ir(ck);
579 }
580 
581 static int cklink_probe_init(struct cklink_ctx *ck)
582 {
583  uint8_t sr1[CKLINK_SELFREG_BYTES];
584  uint8_t sr8[CKLINK_SELFREG_BYTES];
585  uint8_t readback[CKLINK_SELFREG_BYTES];
586  int ret;
587 
590 
591  // Configure at slow clock for cold-boot signal-integrity margin.
594  if (ret != ERROR_OK)
595  return ret;
597  if (ret != ERROR_OK)
598  return ret;
600  if (ret != ERROR_OK)
601  return ret;
604  if (ret != ERROR_OK)
605  return ret;
606 
607  ret = cklink_selfreg_read(ck, CKLINK_SR_CSR, readback);
608  if (ret != ERROR_OK)
609  return ret;
610  if (readback[3] != CKLINK_SR0_MODE_JTAG) {
611  LOG_WARNING("CK-Link: sr0 mode byte is 0x%02x, expected 0x%02x (5-wire JTAG)",
612  readback[3], CKLINK_SR0_MODE_JTAG);
613  }
615 
616  // Pre-load IR = IDCODE; 1-bit DR is the minimum the batch opcode accepts.
617  const uint8_t idcode_ir = CKLINK_PLACEHOLDER_IR_VALUE;
618  const uint8_t dummy_dr = 0;
619  return cklink_jtag_scan(ck, CKLINK_PLACEHOLDER_IR_BITS, &idcode_ir, NULL,
620  1, &dummy_dr, NULL);
621 }
622 
623 static int cklink_init(void)
624 {
625  struct cklink_ctx *ck = calloc(1, sizeof(*ck));
626  if (!ck) {
627  LOG_ERROR("CK-Link: out of memory");
628  return ERROR_JTAG_INIT_FAILED;
629  }
630 
631  const uint16_t *vids = adapter_usb_get_vids();
632  const uint16_t *pids = adapter_usb_get_pids();
633  if (!vids[0] || !pids[0]) {
634  vids = cklink_default_vids;
635  pids = cklink_default_pids;
636  }
637 
638  const char *serial = adapter_get_required_serial();
639  if (jtag_libusb_open(vids, pids, serial,
640  &ck->usb_dev, NULL) != ERROR_OK) {
641  LOG_ERROR("CK-Link probe not found");
642  free(ck);
643  return ERROR_JTAG_INIT_FAILED;
644  }
645 
646  libusb_set_auto_detach_kernel_driver(ck->usb_dev, 1);
647 
648  int claim = libusb_claim_interface(ck->usb_dev, CKLINK_USB_INTERFACE);
649  if (claim != 0) {
650  LOG_ERROR("CK-Link: failed to claim interface %d: %s",
651  CKLINK_USB_INTERFACE, libusb_error_name(claim));
653  free(ck);
654  return ERROR_JTAG_INIT_FAILED;
655  }
656 
657  if (cklink_probe_init(ck) != ERROR_OK) {
658  LOG_ERROR("CK-Link: probe initialization failed");
659  libusb_release_interface(ck->usb_dev, CKLINK_USB_INTERFACE);
661  free(ck);
662  return ERROR_JTAG_INIT_FAILED;
663  }
664 
665  cklink_handle = ck;
667  LOG_INFO("CK-Link Lite V2 initialized (5-wire JTAG)");
668  return ERROR_OK;
669 }
670 
671 static int cklink_quit(void)
672 {
673  if (!cklink_handle)
674  return ERROR_OK;
675 
676  libusb_release_interface(cklink_handle->usb_dev,
679  free(cklink_handle->scan_tdi);
680  free(cklink_handle->scan_tdo);
681  free(cklink_handle);
683  return ERROR_OK;
684 }
685 
686 static int cklink_speed(int speed)
687 {
688  struct cklink_ctx *ck = cklink_handle;
689  if (!ck)
690  return ERROR_OK; // called before init
691 
692  uint8_t div = cklink_khz_to_div(speed);
693  if (div == ck->csr_clk)
694  return ERROR_OK;
695  int ret = cklink_write_csr(ck, div, CKLINK_SR0_MODE_JTAG);
696  if (ret != ERROR_OK)
697  return ret;
698  ck->csr_clk = div;
699  int actual = CKLINK_CLK_SOURCE_KHZ / (div + 1);
700  LOG_DEBUG("CK-Link: adapter speed: requested %d kHz, programmed %d kHz (div=0x%02x)",
701  speed, actual, div);
702  return ERROR_OK;
703 }
704 
705 static int cklink_khz(int khz, int *jtag_speed)
706 {
707  if (khz == 0) {
708  LOG_ERROR("CK-Link: adaptive clocking (RTCK) is not supported");
710  }
711  uint8_t div = cklink_khz_to_div(khz);
712  int achievable = CKLINK_CLK_SOURCE_KHZ / (div + 1);
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;
719  }
720  }
721  *jtag_speed = achievable;
722  return ERROR_OK;
723 }
724 
725 static int cklink_speed_div(int speed, int *khz)
726 {
727  *khz = speed;
728  return ERROR_OK;
729 }
730 
731 static struct jtag_interface cklink_jtag_interface = {
733 };
734 
736  .name = "cklink",
737  .transport_ids = TRANSPORT_JTAG,
738  .transport_preferred_id = TRANSPORT_JTAG,
739 
740  .init = cklink_init,
741  .quit = cklink_quit,
742  .speed = cklink_speed,
743  .khz = cklink_khz,
744  .speed_div = cklink_speed_div,
745 
746  .jtag_ops = &cklink_jtag_interface,
747 };
const char * adapter_get_required_serial(void)
Retrieves the serial number set with command 'adapter serial'.
Definition: adapter.c:309
const uint16_t * adapter_usb_get_pids(void)
Definition: adapter.c:340
const uint16_t * adapter_usb_get_vids(void)
Definition: adapter.c:335
char * serial
Definition: adapter.c:49
enum arm_mode mode
Definition: armv4_5.c:281
void * buf_set_buf(const void *_src, unsigned int src_start, void *_dst, unsigned int dst_start, unsigned int len)
Definition: binarybuffer.c:120
Support functions to access arbitrary bits in a byte array.
@ JTAG_TLR_RESET
Definition: commands.h:137
@ JTAG_SCAN
Definition: commands.h:129
@ JTAG_PATHMOVE
Definition: commands.h:140
@ JTAG_STABLECLOCKS
Definition: commands.h:142
@ JTAG_RUNTEST
Definition: commands.h:138
@ JTAG_SLEEP
Definition: commands.h:141
@ JTAG_RESET
Definition: commands.h:139
@ JTAG_TMS
Definition: commands.h:143
#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...
Definition: interface.h:50
void jtag_sleep(uint32_t us)
Definition: jtag/core.c:1070
@ TAP_RESET
Definition: jtag.h:56
#define ERROR_JTAG_QUEUE_FAILED
Definition: jtag.h:556
#define ERROR_JTAG_INIT_FAILED
Definition: jtag.h:552
#define ERROR_JTAG_NOT_IMPLEMENTED
Definition: jtag.h:554
static struct scan_blk scan
Definition: lakemont.c:60
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 ...)
Definition: log.h:144
#define ERROR_FAIL
Definition: log.h:188
#define LOG_ERROR(expr ...)
Definition: log.h:147
#define LOG_INFO(expr ...)
Definition: log.h:141
#define LOG_DEBUG(expr ...)
Definition: log.h:124
#define ERROR_OK
Definition: log.h:182
const unsigned char bit_offset
Definition: mspm0.c:628
Represents a driver for a debugging interface.
Definition: interface.h:208
const char *const name
The name of the interface driver.
Definition: interface.h:210
union jtag_command_container cmd
Definition: commands.h:147
Represents a driver for a debugging interface.
Definition: interface.h:183
int(* execute_queue)(struct jtag_command *cmd_queue)
Execute commands in the supplied queue.
Definition: interface.h:196
Definition: register.h:111
The scan_command provide a means of encapsulating a set of scan_field structures that should be scann...
Definition: commands.h:35
bool ir_scan
instruction/not data scan
Definition: commands.h:37
struct scan_field * fields
pointer to an array of data scan fields
Definition: commands.h:41
This structure defines a single scan field in the scan.
Definition: jtag.h:87
uint8_t * in_value
A pointer to a 32-bit memory location for data scanned out.
Definition: jtag.h:93
const uint8_t * out_value
A pointer to value to be scanned into the device.
Definition: jtag.h:91
unsigned int num_bits
The number of bits this field specifies.
Definition: jtag.h:89
#define TRANSPORT_JTAG
Definition: transport.h:19
static void h_u32_to_le(uint8_t *buf, uint32_t val)
Definition: types.h:178
#define DIV_ROUND_UP(m, n)
Rounds m up to the nearest multiple of n using division.
Definition: types.h:79
struct scan_command * scan
Definition: commands.h:113
#define NULL
Definition: usb.h:16
uint8_t cmd
Definition: vdebug.c:1