OpenOCD
linuxspidev.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /* Copyright (C) 2020 by Lup Yuen Lee <luppy@appkaki.com>
4  * Copyright (C) 2024 by Richard Pasek <rpasek@google.com>
5  */
6 
12 /* Uncomment to log SPI exchanges (very verbose, slows things down a lot)
13  *
14  * A quick note on interpreting SPI exchange messages:
15  *
16  * This implementation works by performing SPI exchanges with MOSI and MISO
17  * tied together with a 1K resistor. This combined signal becomes SWDIO.
18  *
19  * Since we are performing SPI exchanges, (reading and writing at the same
20  * time) this means when the target isn't driving SWDIO, what is written by
21  * the host should also be read by the host.
22  *
23  * On SWD writes:
24  * The TX and RX data should match except for the ACK bits from the target
25  * swd write reg exchange: len=6
26  * tx_buf=C5 02 40 00 02 2C
27  * rx_buf=C5 42 40 00 02 2C
28  * ^
29  * |
30  * ACK from target
31  *
32  * On SWD reads:
33  * Only the command byte should match
34  * swd read reg exchange: len=6
35  * tx_buf=B1 00 00 00 00 00
36  * rx_buf=B1 40 20 00 00 F8
37  * ^^
38  * ||
39  * Command packet from host
40  *
41  */
42 // #define LOG_SPI_EXCHANGE
43 
44 #ifdef HAVE_CONFIG_H
45 #include "config.h"
46 #endif
47 
48 #include <stdint.h>
49 #include <unistd.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <fcntl.h>
53 #include <sys/ioctl.h>
54 #include <linux/spi/spidev.h>
55 #include <jtag/adapter.h>
56 #include <jtag/swd.h>
57 #include <jtag/interface.h>
58 #include <jtag/commands.h>
59 
60 #if BUILD_LINUXGPIOD
61 #include "linuxgpiod.h"
62 #endif
63 
64 // Number of bits per SPI exchange
65 #define SPI_BITS 8
66 
67 /* Some transactions such as the JTAG to SWD transitions take extra space.
68  * Ensure queue buffers are always large enough to guarantee that these
69  * transactions can occur */
70 #define MIN_QUEUE_BYTES 64
71 
72 // Time in uS after the last bit of transfer before deselecting the device
73 #define SPI_DESELECT_DELAY 0
74 
75 // Maximum number of SWD transactions to queue together in a SPI exchange
76 #define MAX_QUEUE_ENTRIES 64
77 
78 #define CMD_BITS 8
79 #define TURN_BITS 1
80 #define ACK_BITS 3
81 #define DATA_BITS 32
82 #define PARITY_BITS 1
83 
84 #define SWD_WR_BITS (CMD_BITS + TURN_BITS + ACK_BITS + TURN_BITS + DATA_BITS + PARITY_BITS)
85 #define SWD_RD_BITS (CMD_BITS + TURN_BITS + ACK_BITS + DATA_BITS + PARITY_BITS + TURN_BITS)
86 #define SWD_OP_BITS (MAX(SWD_WR_BITS, SWD_RD_BITS))
87 #define SWD_OP_BYTES (DIV_ROUND_UP(SWD_OP_BITS, SPI_BITS))
88 
89 #define AP_DELAY_CLOCKS 8
90 #define AP_DELAY_BYTES (DIV_ROUND_UP(AP_DELAY_CLOCKS, SPI_BITS))
91 
92 #define END_IDLE_CLOCKS 8
93 #define END_IDLE_BYTES (DIV_ROUND_UP(END_IDLE_CLOCKS, SPI_BITS))
94 
95 // File descriptor for SPI device
96 static int spi_fd = -1;
97 
98 // SPI Configuration
99 static char *spi_path;
100 static uint32_t spi_mode = SPI_MODE_3; // Note: SPI in LSB mode is not often supported. We'll flip LSB to MSB ourselves.
101 static uint32_t spi_speed;
102 
103 struct queue_info {
104  unsigned int buf_idx;
105  uint32_t *rx_ptr;
106 };
107 
108 static int queue_retval;
109 static unsigned int max_queue_entries;
110 static unsigned int queue_fill;
111 static unsigned int queue_buf_fill;
112 static unsigned int queue_buf_size;
113 static struct queue_info *queue_infos;
114 static uint8_t *queue_tx_buf;
115 static uint8_t *queue_rx_buf;
116 static uint8_t *tx_flip_buf;
117 
118 static int spidev_quit(void);
119 static int spidev_swd_switch_seq(enum swd_special_seq seq);
120 static int spidev_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk);
121 
122 static void spi_exchange(const uint8_t *tx_data, uint8_t *rx_data, unsigned int len)
123 {
124 #ifdef LOG_SPI_EXCHANGE
125  LOG_OUTPUT("exchange: len=%u\n", len);
126 #endif // LOG_SPI_EXCHANGE
127 
128  if (len == 0) {
129  LOG_DEBUG("exchange with no length");
130  return;
131  }
132 
133  if (!tx_data && !rx_data) {
134  LOG_DEBUG("exchange with no valid tx or rx pointers");
135  return;
136  }
137 
138  if (len > queue_buf_size) {
139  LOG_ERROR("exchange too large len=%u ", len);
140  return;
141  }
142 
143  if (tx_data) {
144  // Reverse LSB to MSB
145  for (unsigned int i = 0; i < len; i++)
146  tx_flip_buf[i] = flip_u32(tx_data[i], 8);
147 
148 #ifdef LOG_SPI_EXCHANGE
149  if (len != 0) {
150  LOG_OUTPUT(" tx_buf=");
151  for (unsigned int i = 0; i < len; i++)
152  LOG_OUTPUT("%.2" PRIx8 " ", tx_flip_buf[i]);
153  }
154  LOG_OUTPUT("\n");
155 #endif // LOG_SPI_EXCHANGE
156  }
157  // Transmit the MSB buffer to SPI device.
158  struct spi_ioc_transfer tr = {
159  /* The following must be cast to unsigned long to compile correctly on
160  * 32 and 64 bit machines (as is done in the spidev examples in Linux
161  * kernel code in tools/spi/).
162  */
163  .tx_buf = (unsigned long)(tx_data ? tx_flip_buf : NULL),
164  .rx_buf = (unsigned long)rx_data,
165  .len = len,
166  .delay_usecs = SPI_DESELECT_DELAY,
167  .speed_hz = spi_speed,
168  .bits_per_word = SPI_BITS,
169  };
170  int ret = ioctl(spi_fd, SPI_IOC_MESSAGE(1), &tr);
171 
172  if (ret < 1) {
173  LOG_ERROR("exchange failed");
174  return;
175  }
176 
177  if (rx_data) {
178 #ifdef LOG_SPI_EXCHANGE
179  if (len != 0) {
180  LOG_OUTPUT(" rx_buf=");
181  for (unsigned int i = 0; i < len; i++)
182  LOG_OUTPUT("%.2" PRIx8 " ", rx_data[i]);
183  }
184  LOG_OUTPUT("\n");
185 #endif // LOG_SPI_EXCHANGE
186 
187  // Reverse MSB to LSB
188  for (unsigned int i = 0; i < len; i++)
189  rx_data[i] = flip_u32(rx_data[i], 8);
190  }
191 }
192 
193 static int spidev_speed(int speed)
194 {
195  uint32_t tmp_speed = speed;
196 
197  int ret = ioctl(spi_fd, SPI_IOC_WR_MAX_SPEED_HZ, &tmp_speed);
198  if (ret == -1) {
199  LOG_ERROR("Failed to set SPI %d speed", speed);
200  return ERROR_FAIL;
201  }
202 
203  spi_speed = speed;
204 
205  return ERROR_OK;
206 }
207 
208 static int spidev_speed_div(int speed, int *khz)
209 {
210  *khz = speed / 1000;
211  return ERROR_OK;
212 }
213 
214 static int spidev_khz(int khz, int *jtag_speed)
215 {
216  if (khz == 0) {
217  LOG_DEBUG("RCLK not supported");
218  return ERROR_FAIL;
219  }
220 
221  *jtag_speed = khz * 1000;
222  return ERROR_OK;
223 }
224 
225 static void spidev_free_queue(void)
226 {
227  max_queue_entries = 0;
228  queue_buf_size = 0;
229 
230  free(queue_infos);
231  queue_infos = NULL;
232 
233  free(queue_tx_buf);
234  queue_tx_buf = NULL;
235 
236  free(queue_rx_buf);
237  queue_rx_buf = NULL;
238 
239  free(tx_flip_buf);
240  tx_flip_buf = NULL;
241 }
242 
243 static void spidev_clear_queue(void)
244 {
245  queue_fill = 0;
246  queue_buf_fill = 0;
247 
248  memset(queue_infos, 0, sizeof(struct queue_info) * max_queue_entries);
249  memset(queue_tx_buf, 0, queue_buf_size);
250  memset(queue_rx_buf, 0, queue_buf_size);
251  memset(tx_flip_buf, 0, queue_buf_size);
252 }
253 
254 static int spidev_alloc_queue(unsigned int new_queue_entries)
255 {
256  if (queue_fill || queue_buf_fill) {
257  LOG_ERROR("Can't realloc queue when queue is in use");
258  return ERROR_FAIL;
259  }
260 
261  unsigned int new_queue_buf_size =
262  (new_queue_entries * (SWD_OP_BYTES + AP_DELAY_BYTES)) + END_IDLE_BYTES;
263  if (new_queue_buf_size < MIN_QUEUE_BYTES) {
264  LOG_INFO("Increasing queue buffer size from %u to minimum %u bytes",
265  new_queue_buf_size, MIN_QUEUE_BYTES);
266  new_queue_buf_size = MIN_QUEUE_BYTES;
267  }
268 
269  queue_infos = realloc(queue_infos, sizeof(struct queue_info) * new_queue_entries);
270  if (!queue_infos)
271  goto realloc_fail;
272 
273  queue_tx_buf = realloc(queue_tx_buf, new_queue_buf_size);
274  if (!queue_tx_buf)
275  goto realloc_fail;
276 
277  queue_rx_buf = realloc(queue_rx_buf, new_queue_buf_size);
278  if (!queue_rx_buf)
279  goto realloc_fail;
280 
281  tx_flip_buf = realloc(tx_flip_buf, new_queue_buf_size);
282  if (!tx_flip_buf)
283  goto realloc_fail;
284 
285  max_queue_entries = new_queue_entries;
286  queue_buf_size = new_queue_buf_size;
287 
289 
290  LOG_DEBUG("Set queue entries to %u (buffers %u bytes)", max_queue_entries, queue_buf_size);
291 
292  return ERROR_OK;
293 
294 realloc_fail:
296 
297  LOG_ERROR("Couldn't allocate queue. Out of memory.");
298  return ERROR_FAIL;
299 }
300 
301 static int spidev_init(void)
302 {
303  LOG_INFO("SPI SWD driver");
304 
305  if (spi_fd >= 0)
306  return ERROR_OK;
307 
308  if (!spi_path) {
309  LOG_ERROR("Path to spidev not set");
310  return ERROR_JTAG_INIT_FAILED;
311  }
312 
313  int ret;
314 #if BUILD_LINUXGPIOD
316  if (ret != ERROR_OK)
317  goto out_error;
318 #endif
319 
320  // Open SPI device.
321  spi_fd = open(spi_path, O_RDWR);
322  if (spi_fd < 0) {
323  LOG_ERROR("Failed to open SPI port at %s", spi_path);
324  goto out_error;
325  }
326 
327  // Set SPI mode.
328 #ifdef SPI_IOC_WR_MODE32
329  ret = ioctl(spi_fd, SPI_IOC_WR_MODE32, &spi_mode);
330 #else
331  // Linux pre 3.15 does not support MODE32, use 8-bit ioctl
332  if (spi_mode & ~0xff) {
333  LOG_ERROR("SPI mode 0x%" PRIx32 ", system permits 8 bits only", spi_mode);
334  goto out_error;
335  }
336 
337  uint8_t mode = (uint8_t)spi_mode;
338  ret = ioctl(spi_fd, SPI_IOC_WR_MODE, &mode);
339 #endif
340  if (ret == -1) {
341  LOG_ERROR("Failed to set SPI mode 0x%" PRIx32, spi_mode);
342  goto out_error;
343  }
344 
345  // Set SPI bits per word.
346  uint32_t spi_bits = SPI_BITS;
347  ret = ioctl(spi_fd, SPI_IOC_WR_BITS_PER_WORD, &spi_bits);
348  if (ret == -1) {
349  LOG_ERROR("Failed to set SPI %" PRIu8 " bits per transfer", spi_bits);
350  goto out_error;
351  }
352 
353  LOG_INFO("Opened SPI device at %s in mode 0x%" PRIx32 " with %" PRIu8 " bits ",
354  spi_path, spi_mode, spi_bits);
355 
356  if (max_queue_entries == 0) {
358  if (ret != ERROR_OK)
359  goto out_error;
360  }
361 
362  return ERROR_OK;
363 
364 out_error:
365  spidev_quit();
366 
367  return ERROR_JTAG_INIT_FAILED;
368 }
369 
370 static int spidev_quit(void)
371 {
373 
374 #if BUILD_LINUXGPIOD
375  linuxgpiod_quit();
376 #endif
377 
378  if (spi_fd < 0)
379  return ERROR_OK;
380 
381  close(spi_fd);
382  spi_fd = -1;
383 
384  free(spi_path);
385  spi_path = NULL;
386 
387  return ERROR_OK;
388 }
389 
390 static int spidev_swd_init(void)
391 {
392  LOG_DEBUG("spidev_swd_init");
393  return ERROR_OK;
394 }
395 
396 static int spidev_swd_execute_queue(unsigned int end_idle_bytes)
397 {
398  LOG_DEBUG_IO("Executing %u queued transactions", queue_fill);
399 
400  if (queue_retval != ERROR_OK) {
401  LOG_DEBUG_IO("Skipping due to previous errors: %d", queue_retval);
402  goto skip;
403  }
404 
405  /* A transaction must be followed by another transaction or at least 8 idle
406  * cycles to ensure that data is clocked through the AP. Since the tx
407  * buffer is zeroed after each queue run, every byte added to the buffer
408  * fill will add on an additional 8 idle cycles.
409  */
410  queue_buf_fill += end_idle_bytes;
411 
413 
414  for (unsigned int queue_idx = 0; queue_idx < queue_fill; queue_idx++) {
415  unsigned int buf_idx = queue_infos[queue_idx].buf_idx;
416  uint8_t *tx_ptr = &queue_tx_buf[buf_idx];
417  uint8_t *rx_ptr = &queue_rx_buf[buf_idx];
418  uint8_t cmd = buf_get_u32(tx_ptr, 0, CMD_BITS);
419  bool read = cmd & SWD_CMD_RNW ? true : false;
420  int ack = buf_get_u32(rx_ptr, CMD_BITS + TURN_BITS, ACK_BITS);
421  uint32_t data = read ?
424 
425  // Devices do not reply to DP_TARGETSEL write cmd, ignore received ack
426  bool check_ack = swd_cmd_returns_ack(cmd);
427 
428  LOG_CUSTOM_LEVEL((check_ack && ack != SWD_ACK_OK) ? LOG_LVL_DEBUG : LOG_LVL_DEBUG_IO,
429  "%s%s %s %s reg %X = %08" PRIx32,
430  check_ack ? "" : "ack ignored ",
431  ack == SWD_ACK_OK ? "OK" :
432  ack == SWD_ACK_WAIT ? "WAIT" :
433  ack == SWD_ACK_FAULT ? "FAULT" : "JUNK",
434  cmd & SWD_CMD_APNDP ? "AP" : "DP",
435  read ? "read" : "write",
436  (cmd & SWD_CMD_A32) >> 1,
437  data);
438 
439  if (ack != SWD_ACK_OK && check_ack) {
441  goto skip;
442 
443  } else if (read) {
445 
446  if (parity != parity_u32(data)) {
447  LOG_ERROR("SWD Read data parity mismatch");
449  goto skip;
450  }
451 
452  if (queue_infos[queue_idx].rx_ptr)
453  *queue_infos[queue_idx].rx_ptr = data;
454  }
455  }
456 
457 skip:
459 
460  int retval = queue_retval;
462 
463  return retval;
464 }
465 
466 static int spidev_swd_run_queue(void)
467 {
468  /* Since we are unsure if another SWD transaction will follow the
469  * transactions we are just about to execute, we need to add on 8 idle
470  * cycles.
471  */
473 }
474 
475 static void spidev_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data, uint32_t ap_delay_clk)
476 {
477  unsigned int swd_op_bytes = DIV_ROUND_UP(SWD_OP_BITS + ap_delay_clk, SPI_BITS);
478 
479  if (queue_fill >= max_queue_entries ||
480  queue_buf_fill + swd_op_bytes + END_IDLE_BYTES > queue_buf_size) {
481  /* Not enough room in the queue. Run the queue. No idle bytes are
482  * needed because we are going to execute transactions right after
483  * the queue is emptied.
484  */
486  }
487 
488  if (queue_retval != ERROR_OK)
489  return;
490 
491  uint8_t *tx_ptr = &queue_tx_buf[queue_buf_fill];
492 
494 
495  buf_set_u32(tx_ptr, 0, CMD_BITS, cmd);
496 
497  if (cmd & SWD_CMD_RNW) {
498  // Queue a read transaction
500  } else {
501  // Queue a write transaction
502  buf_set_u32(tx_ptr,
504  buf_set_u32(tx_ptr,
506  }
507 
509 
510  /* Add idle cycles after AP accesses to avoid WAIT. Buffer is already
511  * zeroed so we just need to advance the pointer to add idle cycles.
512  */
513  queue_buf_fill += swd_op_bytes;
514 
515  queue_fill++;
516 }
517 
518 static int spidev_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
519 {
520  assert(cmd & SWD_CMD_RNW);
521  spidev_swd_queue_cmd(cmd, value, 0, ap_delay_clk);
522 
523  return ERROR_OK; /* TODO: return error instead of queuing it */
524 }
525 
526 static int spidev_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
527 {
528  assert(!(cmd & SWD_CMD_RNW));
529  spidev_swd_queue_cmd(cmd, NULL, value, ap_delay_clk);
530 
531  return ERROR_OK; /* TODO: return error instead of queuing it */
532 }
533 
535 {
536  switch (seq) {
537  case LINE_RESET:
538  LOG_DEBUG_IO("SWD line reset");
540  break;
541  case JTAG_TO_SWD:
542  LOG_DEBUG("JTAG-to-SWD");
544  break;
545  case JTAG_TO_DORMANT:
546  LOG_DEBUG("JTAG-to-DORMANT");
548  break;
549  case SWD_TO_JTAG:
550  LOG_DEBUG("SWD-to-JTAG");
552  break;
553  case SWD_TO_DORMANT:
554  LOG_DEBUG("SWD-to-DORMANT");
556  break;
557  case DORMANT_TO_SWD:
558  LOG_DEBUG("DORMANT-to-SWD");
560  break;
561  case DORMANT_TO_JTAG:
562  LOG_DEBUG("DORMANT-to-JTAG");
564  break;
565  default:
566  LOG_ERROR("Sequence %d not supported", seq);
567  return ERROR_FAIL;
568  }
569 
570  return ERROR_OK;
571 }
572 
573 COMMAND_HANDLER(spidev_handle_path_command)
574 {
575  if (CMD_ARGC != 1)
577 
578  free(spi_path);
579  spi_path = strdup(CMD_ARGV[0]);
580  if (!spi_path) {
581  LOG_ERROR("Out of memory");
582  return ERROR_FAIL;
583  }
584 
585  return ERROR_OK;
586 }
587 
588 COMMAND_HANDLER(spidev_handle_mode_command)
589 {
590  if (CMD_ARGC != 1)
592 
594  return ERROR_OK;
595 }
596 
597 COMMAND_HANDLER(spidev_handle_queue_entries_command)
598 {
599  uint32_t new_queue_entries;
600 
601  if (CMD_ARGC != 1)
603 
604  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], new_queue_entries);
605 
606  return spidev_alloc_queue(new_queue_entries);
607 }
608 
609 const struct swd_driver spidev_swd = {
611  .switch_seq = spidev_swd_switch_seq,
612  .read_reg = spidev_swd_read_reg,
613  .write_reg = spidev_swd_write_reg,
614  .run = spidev_swd_run_queue,
615 };
616 
617 static const struct command_registration spidev_subcommand_handlers[] = {
618  {
619  .name = "path",
620  .handler = &spidev_handle_path_command,
621  .mode = COMMAND_CONFIG,
622  .help = "set the path to the spidev device",
623  .usage = "path_to_spidev",
624  },
625  {
626  .name = "mode",
627  .handler = &spidev_handle_mode_command,
628  .mode = COMMAND_CONFIG,
629  .help = "set the mode of the spi port with optional bit flags (default=3)",
630  .usage = "mode",
631  },
632  {
633  .name = "queue_entries",
634  .handler = &spidev_handle_queue_entries_command,
635  .mode = COMMAND_CONFIG,
636  .help = "set the queue entry size (default=64)",
637  .usage = "queue_entries",
638  },
640 };
641 
642 static const struct command_registration spidev_command_handlers[] = {
643  {
644  .name = "spidev",
645  .mode = COMMAND_ANY,
646  .help = "perform spidev management",
648  .usage = "",
649  },
651 };
652 
654  .name = "linuxspidev",
655  .transport_ids = TRANSPORT_SWD,
656  .transport_preferred_id = TRANSPORT_SWD,
657  .commands = spidev_command_handlers,
658 
659  .init = spidev_init,
660  .quit = spidev_quit,
661  .speed = spidev_speed,
662  .khz = spidev_khz,
663  .speed_div = spidev_speed_div,
664 #if BUILD_LINUXGPIOD
665  .reset = linuxgpiod_reset,
666 #endif
667 
668  .swd_ops = &spidev_swd,
669 };
@ ADAPTER_GPIO_IDX_SRST
Definition: adapter.h:61
#define SWD_ACK_FAULT
Definition: arm_adi_v5.h:33
swd_special_seq
Definition: arm_adi_v5.h:236
@ DORMANT_TO_JTAG
Definition: arm_adi_v5.h:243
@ JTAG_TO_SWD
Definition: arm_adi_v5.h:238
@ DORMANT_TO_SWD
Definition: arm_adi_v5.h:242
@ LINE_RESET
Definition: arm_adi_v5.h:237
@ JTAG_TO_DORMANT
Definition: arm_adi_v5.h:239
@ SWD_TO_DORMANT
Definition: arm_adi_v5.h:241
@ SWD_TO_JTAG
Definition: arm_adi_v5.h:240
#define SWD_ACK_WAIT
Definition: arm_adi_v5.h:32
#define SWD_ACK_OK
Definition: arm_adi_v5.h:31
enum arm_mode mode
Definition: armv4_5.c:280
uint32_t flip_u32(uint32_t value, unsigned int num)
Inverts the ordering of bits inside a 32-bit word (e.g.
Definition: binarybuffer.c:165
static uint32_t buf_get_u32(const uint8_t *_buffer, unsigned int first, unsigned int num)
Retrieves num bits from _buffer, starting at the first bit, returning the bits in a 32-bit word.
Definition: binarybuffer.h:104
static void buf_set_u32(uint8_t *_buffer, unsigned int first, unsigned int num, uint32_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
Definition: binarybuffer.h:34
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:161
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:405
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:156
#define COMMAND_PARSE_NUMBER(type, in, out)
parses the string in into out as a type, or prints a command error and passes the error code to the c...
Definition: command.h:445
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:256
@ COMMAND_CONFIG
Definition: command.h:41
@ COMMAND_ANY
Definition: command.h:42
#define ERROR_JTAG_INIT_FAILED
Definition: jtag.h:552
int linuxgpiod_reset(int trst, int srst)
Definition: linuxgpiod.c:517
int linuxgpiod_helper_get_line(enum adapter_gpio_config_index idx)
Definition: linuxgpiod.c:593
int linuxgpiod_quit(void)
Definition: linuxgpiod.c:584
static const struct command_registration spidev_subcommand_handlers[]
Definition: linuxspidev.c:617
static int spi_fd
Definition: linuxspidev.c:96
#define SWD_OP_BITS
Definition: linuxspidev.c:86
#define END_IDLE_BYTES
Definition: linuxspidev.c:93
static void spidev_free_queue(void)
Definition: linuxspidev.c:225
static void spidev_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data, uint32_t ap_delay_clk)
Definition: linuxspidev.c:475
#define TURN_BITS
Definition: linuxspidev.c:79
static uint32_t spi_mode
Definition: linuxspidev.c:100
static int spidev_swd_init(void)
Definition: linuxspidev.c:390
#define ACK_BITS
Definition: linuxspidev.c:80
static int spidev_alloc_queue(unsigned int new_queue_entries)
Definition: linuxspidev.c:254
static uint8_t * queue_tx_buf
Definition: linuxspidev.c:114
static int spidev_swd_execute_queue(unsigned int end_idle_bytes)
Definition: linuxspidev.c:396
#define DATA_BITS
Definition: linuxspidev.c:81
const struct swd_driver spidev_swd
Definition: linuxspidev.c:609
#define SWD_OP_BYTES
Definition: linuxspidev.c:87
static uint8_t * queue_rx_buf
Definition: linuxspidev.c:115
static void spidev_clear_queue(void)
Definition: linuxspidev.c:243
static int spidev_quit(void)
Definition: linuxspidev.c:370
#define MIN_QUEUE_BYTES
Definition: linuxspidev.c:70
COMMAND_HANDLER(spidev_handle_path_command)
Definition: linuxspidev.c:573
static int queue_retval
Definition: linuxspidev.c:108
struct adapter_driver linuxspidev_adapter_driver
Definition: linuxspidev.c:653
#define AP_DELAY_BYTES
Definition: linuxspidev.c:90
static struct queue_info * queue_infos
Definition: linuxspidev.c:113
#define SPI_BITS
Definition: linuxspidev.c:65
static int spidev_khz(int khz, int *jtag_speed)
Definition: linuxspidev.c:214
static char * spi_path
Definition: linuxspidev.c:99
static unsigned int max_queue_entries
Definition: linuxspidev.c:109
#define MAX_QUEUE_ENTRIES
Definition: linuxspidev.c:76
static int spidev_init(void)
Definition: linuxspidev.c:301
static uint32_t spi_speed
Definition: linuxspidev.c:101
static int spidev_speed_div(int speed, int *khz)
Definition: linuxspidev.c:208
#define SPI_DESELECT_DELAY
Definition: linuxspidev.c:73
static uint8_t * tx_flip_buf
Definition: linuxspidev.c:116
static unsigned int queue_buf_fill
Definition: linuxspidev.c:111
static const struct command_registration spidev_command_handlers[]
Definition: linuxspidev.c:642
static int spidev_swd_switch_seq(enum swd_special_seq seq)
Definition: linuxspidev.c:534
static int spidev_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
Definition: linuxspidev.c:518
static unsigned int queue_fill
Definition: linuxspidev.c:110
static int spidev_speed(int speed)
Definition: linuxspidev.c:193
static void spi_exchange(const uint8_t *tx_data, uint8_t *rx_data, unsigned int len)
Definition: linuxspidev.c:122
#define PARITY_BITS
Definition: linuxspidev.c:82
static unsigned int queue_buf_size
Definition: linuxspidev.c:112
#define CMD_BITS
Definition: linuxspidev.c:78
static int spidev_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
Definition: linuxspidev.c:526
static int spidev_swd_run_queue(void)
Definition: linuxspidev.c:466
#define LOG_CUSTOM_LEVEL(level, expr ...)
Definition: log.h:132
#define LOG_OUTPUT(expr ...)
Definition: log.h:156
#define LOG_DEBUG_IO(expr ...)
Definition: log.h:116
#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
@ LOG_LVL_DEBUG
Definition: log.h:55
@ LOG_LVL_DEBUG_IO
Definition: log.h:56
Represents a driver for a debugging interface.
Definition: interface.h:219
const char *const name
The name of the interface driver.
Definition: interface.h:221
const char * name
Definition: command.h:239
const char * usage
a string listing the options and arguments, required or optional
Definition: command.h:244
unsigned int buf_idx
Definition: linuxspidev.c:104
uint32_t * rx_ptr
Definition: linuxspidev.c:105
int(* init)(void)
Initialize the debug link so it can perform SWD operations.
Definition: swd.h:255
static const unsigned int swd_seq_dormant_to_swd_len
Definition: swd.h:190
static const uint8_t swd_seq_dormant_to_jtag[]
Dormant-to-JTAG sequence.
Definition: swd.h:230
#define SWD_CMD_A32
Definition: swd.h:19
static const uint8_t swd_seq_dormant_to_swd[]
Dormant-to-SWD sequence.
Definition: swd.h:171
static const uint8_t swd_seq_jtag_to_dormant[]
JTAG-to-dormant sequence.
Definition: swd.h:199
static bool swd_cmd_returns_ack(uint8_t cmd)
Test if we can rely on ACK returned by SWD command.
Definition: swd.h:58
#define SWD_CMD_PARK
Definition: swd.h:22
static int swd_ack_to_error_code(uint8_t ack)
Convert SWD ACK value returned from DP to OpenOCD error code.
Definition: swd.h:72
static const unsigned int swd_seq_jtag_to_swd_len
Definition: swd.h:125
static const unsigned int swd_seq_line_reset_len
Definition: swd.h:104
static const unsigned int swd_seq_dormant_to_jtag_len
Definition: swd.h:244
#define SWD_CMD_APNDP
Definition: swd.h:17
static const unsigned int swd_seq_swd_to_dormant_len
Definition: swd.h:159
#define SWD_CMD_START
Definition: swd.h:16
#define SWD_CMD_RNW
Definition: swd.h:18
static const uint8_t swd_seq_line_reset[]
SWD Line reset.
Definition: swd.h:98
static const uint8_t swd_seq_jtag_to_swd[]
JTAG-to-SWD sequence.
Definition: swd.h:115
static const uint8_t swd_seq_swd_to_jtag[]
SWD-to-JTAG sequence.
Definition: swd.h:136
static const unsigned int swd_seq_swd_to_jtag_len
Definition: swd.h:144
static const unsigned int swd_seq_jtag_to_dormant_len
Definition: swd.h:211
static const uint8_t swd_seq_swd_to_dormant[]
SWD-to-dormant sequence.
Definition: swd.h:153
#define true
Definition: system.h:59
#define TRANSPORT_SWD
Definition: transport.h:20
#define DIV_ROUND_UP(m, n)
Rounds m up to the nearest multiple of n using division.
Definition: types.h:79
static int parity_u32(uint32_t x)
Calculate the (even) parity of a 32-bit datum.
Definition: types.h:265
#define NULL
Definition: usb.h:16
uint8_t cmd
Definition: vdebug.c:1
static unsigned int parity(unsigned int v)
Definition: xscale.c:619