OpenOCD
rp2xxx.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 #ifdef HAVE_CONFIG_H
4 #include "config.h"
5 #endif
6 
7 #include "imp.h"
8 #include <helper/binarybuffer.h>
9 #include <target/algorithm.h>
10 #include <target/armv7m.h>
11 #include "spi.h"
12 #include "sfdp.h"
13 #include <target/cortex_m.h>
14 
15 /* this is 'M' 'u', 1 (version) */
16 #define BOOTROM_RP2040_MAGIC 0x01754d
17 /* this is 'M' 'u', 2 (version) */
18 #define BOOTROM_RP2350_MAGIC 0x02754d
19 #define BOOTROM_MAGIC_MASK 0xffffff
20 #define BOOTROM_MAGIC_ADDR 0x00000010
21 
22 #define MAKE_TAG(a, b) (((b)<<8) | a)
23 #define FUNC_FLASH_EXIT_XIP MAKE_TAG('E', 'X')
24 #define FUNC_CONNECT_INTERNAL_FLASH MAKE_TAG('I', 'F')
25 #define FUNC_FLASH_RANGE_ERASE MAKE_TAG('R', 'E')
26 #define FUNC_FLASH_RANGE_PROGRAM MAKE_TAG('R', 'P')
27 #define FUNC_FLASH_FLUSH_CACHE MAKE_TAG('F', 'C')
28 #define FUNC_FLASH_ENTER_CMD_XIP MAKE_TAG('C', 'X')
29 #define FUNC_BOOTROM_STATE_RESET MAKE_TAG('S', 'R')
30 #define FUNC_BOOTROM_SET_STACK MAKE_TAG('S', 'S')
31 #define FUNC_FLASH_RESET_ADDRESS_TRANS MAKE_TAG('R', 'A')
32 
33 /* ROM table flags for RP2350 A1 ROM onwards */
34 #define RT_FLAG_FUNC_RISCV 0x01
35 #define RT_FLAG_FUNC_ARM_SEC 0x04
36 #define RT_FLAG_FUNC_ARM_NONSEC 0x10
37 #define RT_FLAG_DATA 0x40
38 
39 // these form a bit set
40 #define BOOTROM_STATE_RESET_CURRENT_CORE 0x01
41 #define BOOTROM_STATE_RESET_OTHER_CORE 0x02
42 #define BOOTROM_STATE_RESET_GLOBAL_STATE 0x04
43 
44 #define ACCESSCTRL_LOCK_OFFSET 0x40060000u
45 #define ACCESSCTRL_LOCK_DEBUG_BITS 0x00000008u
46 #define ACCESSCTRL_CFGRESET_OFFSET 0x40060008u
47 #define ACCESSCTRL_WRITE_PASSWORD 0xacce0000u
48 
49 #define RP2040_SSI_DR0 0x18000060
50 #define RP2040_QSPI_CTRL 0x4001800c
51 
52 #define RP2040_QSPI_CTRL_OUTOVER_MASK (3ul << 8)
53 #define RP2040_QSPI_CTRL_OUTOVER_LOW (2ul << 8)
54 #define RP2040_QSPI_CTRL_OUTOVER_HIGH (3ul << 8)
55 
56 #define RP2350_QMI_DIRECT_CSR 0x400d0000
57 #define RP2350_QMI_DIRECT_TX 0x400d0004
58 #define RP2350_QMI_DIRECT_RX 0x400d0008
59 
60 #define RP2350_QMI_DIRECT_CSR_EN BIT(0)
61 #define RP2350_QMI_DIRECT_CSR_ASSERT_CS0N BIT(2)
62 #define RP2350_QMI_DIRECT_TX_NOPUSH BIT(20)
63 #define RP2350_QMI_DIRECT_TX_OE BIT(19)
64 
65 #define RP2XXX_SYSINFO_CHIP_ID 0x40000000
66 #define RP2XXX_CHIP_ID_PART_MANUFACTURER(id) ((id) & 0x0fffffff)
67 #define RP2XXX_CHIP_ID_MANUFACTURER 0x493
68 #define RP2XXX_MK_PART(part) (((part) << 12) | (RP2XXX_CHIP_ID_MANUFACTURER << 1) | 1)
69 #define RP2040_CHIP_ID_PART 0x0002
70 #define IS_RP2040(id) (RP2XXX_CHIP_ID_PART_MANUFACTURER(id) == RP2XXX_MK_PART(RP2040_CHIP_ID_PART))
71 #define RP2350_CHIP_ID_PART 0x0004
72 #define IS_RP2350(id) (RP2XXX_CHIP_ID_PART_MANUFACTURER(id) == RP2XXX_MK_PART(RP2350_CHIP_ID_PART))
73 #define RP2XXX_CHIP_ID_REVISION(id) ((id) >> 28)
74 
75 #define RP2XXX_MAX_ALGO_STACK_USAGE 1024
76 #define RP2XXX_MAX_RAM_ALGO_SIZE 1024
77 
78 #define RP2XXX_ROM_API_FIXED_FLASH_PAGE 256
79 #define RP2XXX_ROM_API_FIXED_FLASH_SECTOR 4096
80 
81 // Calling bootrom functions on Arm RP2350 requires the redundancy
82 // coprocessor (RCP) to be initialised. Usually this is done first thing by
83 // the bootrom, but the debugger may skip this, e.g. by resetting the cores
84 // and then running a NO_FLASH binary, or by reset-halting the cores before
85 // flash programming.
86 //
87 // The first case can be handled by a stub in the binary itself to initialise
88 // the RCP with dummy values if the bootrom has not already initialised it.
89 // (Note this case is only reachable via the debugger.) The second case
90 // requires the debugger itself to initialise the RCP, using this stub code:
91 
92 static const int rcp_init_code_bkpt_offset = 24;
93 static const uint8_t rcp_init_code[] = {
94  // Just enable the RCP which is fine if it already was (we assume no other
95  // co-processors are enabled at this point to save space)
96  0x06, 0x48, // ldr r0, = PPB_BASE + M33_CPACR_OFFSET
97  0x5f, 0xf4, 0x40, 0x41, // movs r1, #M33_CPACR_CP7_BITS
98  0x01, 0x60, // str r1, [r0]
99  // Only initialize canary seeds if they haven't been (as to do so twice is a fault)
100  0x30, 0xee, 0x10, 0xf7, // mrc p7, #1, r15, c0, c0, #0
101  0x04, 0xd4, // bmi 1f
102  // Todo should we use something random here and pass it into the algorithm?
103  0x40, 0xec, 0x80, 0x07, // mcrr p7, #8, r0, r0, c0
104  0x40, 0xec, 0x81, 0x07, // mcrr p7, #8, r0, r0, c1
105  // Let other core know
106  0x40, 0xbf, // sev
107  // 1:
108  0x00, 0xbe, // bkpt (end of algorithm)
109  0x00, 0x00, // pad
110  0x88, 0xed, 0x00, 0xe0 // PPB_BASE + M33_CPACR_OFFSET
111 };
112 
113 // An algorithm stub that can be concatenated with a null-terminated list of
114 // (PC, SP, r0-r3) records to perform a batch of ROM calls under a single
115 // OpenOCD algorithm call, to save on algorithm overhead:
116 #define ROM_CALL_BATCH_ALGO_SIZE_BYTES 32
119 // <_start>:
120  0x07, 0xa7, // add r7, pc, #28 ; (adr r7, 20 <_args>)
121 // <_do_next>:
122  0x10, 0xcf, // ldmia r7!, {r4}
123  0x00, 0x2c, // cmp r4, #0
124  0x0a, 0xd0, // beq.n 1e <_done>
125  0x20, 0xcf, // ldmia r7!, {r5}
126  0xad, 0x46, // mov sp, r5
127  0x0f, 0xcf, // ldmia r7!, {r0, r1, r2, r3}
128  0xa0, 0x47, // blx r4
129  0xf7, 0xe7, // b.n 2 <_do_next>
130  0xc0, 0x46, // nop
131  0xc0, 0x46, // nop
132  0xc0, 0x46, // nop
133  0xc0, 0x46, // nop
134  0xc0, 0x46, // nop
135  0xc0, 0x46, // nop
136 // <_done>:
137  0x00, 0xbe, // bkpt 0x0000
138 // <_args>:
139 };
140 
141 // The same as rom_call_batch_algo_armv6m, but clearing stack limits before setting stack:
143 // <_start>:
144  0x07, 0xa7, // add r7, pc, #28 ; (adr r7, 20 <_args>)
145  0x00, 0x20, // movs r0, #0
146  0x80, 0xf3, 0x0a, 0x88, // msr MSPLIM, r0
147  0x80, 0xf3, 0x0b, 0x88, // msr PSPLIM, r0
148 // <_do_next>:
149  0x10, 0xcf, // ldmia r7!, {r4}
150  0x00, 0x2c, // cmp r4, #0
151  0x05, 0xd0, // beq.n 1e <_done>
152  0x20, 0xcf, // ldmia r7!, {r5}
153  0xad, 0x46, // mov sp, r5
154  0x0f, 0xcf, // ldmia r7!, {r0, r1, r2, r3}
155  0xa0, 0x47, // blx r4
156  0xf7, 0xe7, // b.n c <_do_next>
157  0xc0, 0x46, // nop
158 // <_done>:
159  0x00, 0xbe, // bkpt 0x0000
160 // <_args>:
161 };
162 
163 // The same as rom_call_batch_algo_armv6m, but placing arguments in a0-a3 on RISC-V:
165 // <_start>:
166  0x97, 0x04, 0x00, 0x00, // auipc s1,0
167  0x93, 0x84, 0x04, 0x02, // add s1,s1,32 # 20 <_args>
168 // <_do_next>:
169  0x98, 0x40, // lw a4,0(s1)
170  0x11, 0xcb, // beqz a4,1e <_done>
171  0x03, 0xa1, 0x44, 0x00, // lw sp,4(s1)
172  0x88, 0x44, // lw a0,8(s1)
173  0xcc, 0x44, // lw a1,12(s1)
174  0x90, 0x48, // lw a2,16(s1)
175  0xd4, 0x48, // lw a3,20(s1)
176  0xe1, 0x04, // add s1,s1,24
177  0x02, 0x97, // jalr a4
178  0xf5, 0xb7, // j 8 <_do_next>
179 // <_done>:
180  0x02, 0x90, // ebreak
181 // <_args>:
182 };
183 
185  uint32_t pc;
186  uint32_t sp;
187  uint32_t args[4];
188 };
189 
191  bool probed; /* flag indicating successful flash probe */
192  uint32_t id; /* cached SYSINFO CHIP_ID */
193  struct working_area *stack; /* stack used by Boot ROM calls */
194  /* static code scratchpad used for RAM algorithms -- allocated in advance
195  so that higher-level calls can just grab all remaining workarea: */
197  /* function jump table populated by rp2xxx_flash_probe() */
207 
208  char dev_name[20];
210  struct flash_device spi_dev; /* detected model of SPI flash */
212 };
213 
214 #ifndef LOG_ROM_SYMBOL_DEBUG
215 #define LOG_ROM_SYMBOL_DEBUG LOG_DEBUG
216 #endif
217 
218 static int rp2040_lookup_rom_symbol(struct target *target, uint16_t tag, uint16_t flags, uint16_t *symbol_out)
219 {
220  LOG_ROM_SYMBOL_DEBUG("Looking up ROM symbol '%c%c' in RP2040 table", tag & 0xff, (tag >> 8) & 0xff);
221  if (flags != RT_FLAG_FUNC_ARM_SEC && flags != RT_FLAG_DATA) {
222  /* Note RT flags do not exist on RP2040, so just sanity check that we
223  are asked for a type of thing that actually exists in the ROM table */
224  LOG_ERROR("Only data and Secure Arm functions can be looked up in RP2040 ROM table");
225  return ERROR_FAIL;
226  }
227 
228  uint16_t ptr_to_entry;
229  unsigned int offset_magic_to_table_ptr = flags == RT_FLAG_DATA ? 6 : 4;
230  int err = target_read_u16(target, BOOTROM_MAGIC_ADDR + offset_magic_to_table_ptr, &ptr_to_entry);
231  if (err != ERROR_OK)
232  return err;
233 
234  uint16_t entry_tag;
235  do {
236  err = target_read_u16(target, ptr_to_entry, &entry_tag);
237  if (err != ERROR_OK)
238  return err;
239 
240  if (entry_tag == tag) {
241  /* 16 bit symbol is next */
242  err = target_read_u16(target, ptr_to_entry + 2, symbol_out);
243  if (err != ERROR_OK)
244  return err;
245 
246  LOG_ROM_SYMBOL_DEBUG(" -> found: 0x%04" PRIx16, *symbol_out);
247  return ERROR_OK;
248  }
249  ptr_to_entry += 4;
250  } while (entry_tag);
251  *symbol_out = 0;
252  return ERROR_FAIL;
253 }
254 
255 static int rp2350_a0_lookup_symbol(struct target *target, uint16_t tag, uint16_t flags, uint16_t *symbol_out)
256 {
257  LOG_ROM_SYMBOL_DEBUG("Looking up ROM symbol '%c%c' in RP2350 A0 table", tag & 0xff, (tag >> 8) & 0xff);
258 
259  /* RP2350 A0 table format is the same as RP2040 except with 16 bits of
260  flags after each 16-bit pointer. We ignore the flags, as each symbol
261  only has one datum associated with it. */
262 
263  uint32_t magic_ptr = BOOTROM_MAGIC_ADDR;
264  if (flags == RT_FLAG_FUNC_RISCV) {
265  /* RP2350 A0 used split function tables for Arm/RISC-V -- not used on
266  any other device or any other version of this device. There is a
267  well-known RISC-V table at the top of ROM, matching the well-known
268  Arm table at the bottom of ROM. */
269  magic_ptr = 0x7decu;
270  } else if (flags != RT_FLAG_FUNC_ARM_SEC) {
271  LOG_WARNING("Ignoring non-default flags for RP2350 A0 lookup, hope you like Secure Arm functions");
272  }
273 
274  uint16_t ptr_to_entry;
275  const unsigned int offset_magic_to_table_ptr = 4;
276  int err = target_read_u16(target, magic_ptr + offset_magic_to_table_ptr, &ptr_to_entry);
277  if (err != ERROR_OK)
278  return err;
279 
280  uint16_t entry_tag;
281  do {
282  err = target_read_u16(target, ptr_to_entry, &entry_tag);
283  if (err != ERROR_OK)
284  return err;
285 
286  if (entry_tag == tag) {
287  err = target_read_u16(target, ptr_to_entry + 2, symbol_out);
288  if (err != ERROR_OK)
289  return err;
290 
291  LOG_ROM_SYMBOL_DEBUG(" -> found: 0x%04" PRIx16, *symbol_out);
292  return ERROR_OK;
293  }
294  ptr_to_entry += 6;
295  } while (entry_tag);
296  *symbol_out = 0;
297  return ERROR_FAIL;
298 }
299 
300 static int rp2350_lookup_rom_symbol(struct target *target, uint32_t ptr_to_entry,
301  uint16_t tag, uint16_t flags, uint16_t *symbol_out)
302 {
303  LOG_ROM_SYMBOL_DEBUG("Looking up ROM symbol '%c%c' in RP2350 A1 table", tag & 0xff, (tag >> 8) & 0xff);
304 
305  /* On RP2350 A1, Each entry has a flag bitmap identifying the type of its
306  contents. The entry contains one halfword of data for each set flag
307  bit. There may be both Arm and RISC-V entries under the same tag, or
308  separate Arm Secure/NonSecure entries (or all three, why not). */
309 
310  while (true) {
311  uint16_t entry_tag, entry_flags;
312 
313  int err = target_read_u16(target, ptr_to_entry, &entry_tag);
314  if (err != ERROR_OK)
315  return err;
316 
317  if (entry_tag == 0) {
318  *symbol_out = 0;
319  return ERROR_FAIL;
320  }
321  ptr_to_entry += 2;
322 
323  err = target_read_u16(target, ptr_to_entry, &entry_flags);
324  if (err != ERROR_OK)
325  return err;
326 
327  ptr_to_entry += 2;
328 
329  uint16_t matching_flags = flags & entry_flags;
330 
331  if (tag == entry_tag && matching_flags != 0) {
332  /* This is our entry, seek to the correct data item and return it. */
333  bool is_riscv_func = matching_flags & RT_FLAG_FUNC_RISCV;
334  while (!(matching_flags & 1)) {
335  if (entry_flags & 1)
336  ptr_to_entry += 2;
337 
338  matching_flags >>= 1;
339  entry_flags >>= 1;
340  }
341  if (is_riscv_func) {
342  /* For RISC-V, the table entry itself is the entry point -- trick
343  to make shared function implementations smaller */
344  *symbol_out = ptr_to_entry;
345  return ERROR_OK;
346  }
347  err = target_read_u16(target, ptr_to_entry, symbol_out);
348  if (err != ERROR_OK)
349  return err;
350 
351  LOG_ROM_SYMBOL_DEBUG(" -> found: 0x%04" PRIx16, *symbol_out);
352  return ERROR_OK;
353  }
354  /* Skip past this entry */
355  while (entry_flags) {
356  if (entry_flags & 1)
357  ptr_to_entry += 2;
358 
359  entry_flags >>= 1;
360  }
361  }
362 }
363 
364 static int rp2xxx_lookup_rom_symbol(struct target *target, uint16_t tag, uint16_t flags, uint16_t *symbol_out)
365 {
366  uint32_t magic;
367  int err = target_read_u32(target, BOOTROM_MAGIC_ADDR, &magic);
368  if (err != ERROR_OK)
369  return err;
370 
371  /* Ignore version */
372  magic &= BOOTROM_MAGIC_MASK;
373 
374  if (magic == BOOTROM_RP2350_MAGIC) {
375  /* Distinguish old-style RP2350 ROM table (A0, and earlier A1 builds)
376  based on position of table -- a high address means it is shared with
377  RISC-V, i.e. new-style. */
378  uint16_t table_ptr;
379  err = target_read_u16(target, BOOTROM_MAGIC_ADDR + 4, &table_ptr);
380  if (err != ERROR_OK)
381  return err;
382  if (table_ptr < 0x7c00)
383  return rp2350_a0_lookup_symbol(target, tag, flags, symbol_out);
384  else
385  return rp2350_lookup_rom_symbol(target, table_ptr, tag, flags, symbol_out);
386 
387  } else if (magic == BOOTROM_RP2040_MAGIC) {
388  return rp2040_lookup_rom_symbol(target, tag, flags, symbol_out);
389  }
390  LOG_ERROR("RP2040/RP2350 BOOT ROM not found");
391  return ERROR_FAIL;
392 }
393 
395 {
396  uint16_t symtype_func = is_arm(target_to_arm(target))
398  int err;
400  symtype_func, &priv->jump_flash_exit_xip);
401  if (err != ERROR_OK) {
402  LOG_ERROR("Function FUNC_FLASH_EXIT_XIP not found in RP2xxx ROM");
403  return err;
404  }
405 
407  symtype_func, &priv->jump_connect_internal_flash);
408  if (err != ERROR_OK) {
409  LOG_ERROR("Function FUNC_CONNECT_INTERNAL_FLASH not found in RP2xxx ROM");
410  return err;
411  }
412 
413  err = rp2xxx_lookup_rom_symbol(target, FUNC_FLASH_RANGE_ERASE, symtype_func, &priv->jump_flash_range_erase);
414  if (err != ERROR_OK) {
415  LOG_ERROR("Function FUNC_FLASH_RANGE_ERASE not found in RP2xxx ROM");
416  return err;
417  }
418 
419  err = rp2xxx_lookup_rom_symbol(target, FUNC_FLASH_RANGE_PROGRAM, symtype_func, &priv->jump_flash_range_program);
420  if (err != ERROR_OK) {
421  LOG_ERROR("Function FUNC_FLASH_RANGE_PROGRAM not found in RP2xxx ROM");
422  return err;
423  }
424 
425  err = rp2xxx_lookup_rom_symbol(target, FUNC_FLASH_FLUSH_CACHE, symtype_func, &priv->jump_flush_cache);
426  if (err != ERROR_OK) {
427  LOG_ERROR("Function FUNC_FLASH_FLUSH_CACHE not found in RP2xxx ROM");
428  return err;
429  }
430 
431  err = rp2xxx_lookup_rom_symbol(target, FUNC_FLASH_ENTER_CMD_XIP, symtype_func, &priv->jump_enter_cmd_xip);
432  if (err != ERROR_OK) {
433  LOG_ERROR("Function FUNC_FLASH_ENTER_CMD_XIP not found in RP2xxx ROM");
434  return err;
435  }
436 
437  // From this point are optional functions which do not exist on e.g. RP2040
438  // or pre-production RP2350 ROM versions:
439  if (IS_RP2040(priv->id)) {
440  priv->jump_bootrom_reset_state = 0;
441  priv->jump_flash_reset_address_trans = 0;
442  return ERROR_OK;
443  }
444 
445  err = rp2xxx_lookup_rom_symbol(target, FUNC_BOOTROM_STATE_RESET, symtype_func, &priv->jump_bootrom_reset_state);
446  if (err != ERROR_OK) {
447  priv->jump_bootrom_reset_state = 0;
448  LOG_WARNING("Function FUNC_BOOTROM_STATE_RESET not found in RP2xxx ROM. (probably an RP2350 A0)");
449  }
450 
451  if (!is_arm(target_to_arm(target))) {
453  &priv->jump_bootrom_set_varm_stack);
454  if (err != ERROR_OK) {
455  priv->jump_bootrom_set_varm_stack = 0;
456  LOG_WARNING("Function FUNC_BOOTROM_SET_STACK not found in RP2xxx ROM. (probably an RP2350 A0)");
457  }
458  }
459 
461  symtype_func, &priv->jump_flash_reset_address_trans);
462  if (err != ERROR_OK) {
463  priv->jump_flash_reset_address_trans = 0;
464  LOG_WARNING("Function FUNC_FLASH_RESET_ADDRESS_TRANS not found in RP2xxx ROM. (probably an RP2350 A0)");
465  }
466  return ERROR_OK;
467 }
468 
469 // Call a list of PC + SP + r0-r3 function call tuples with a single OpenOCD
470 // algorithm invocation, to amortise the algorithm overhead over multiple calls:
472  struct rp2xxx_rom_call_batch_record *calls, unsigned int n_calls)
473 {
474  // Note + sizeof(uint32_t) is for the null terminator
475  unsigned int batch_size = ROM_CALL_BATCH_ALGO_SIZE_BYTES
476  + n_calls * sizeof(struct rp2xxx_rom_call_batch_record)
477  + sizeof(uint32_t);
478 
479  if (!priv->ram_algo_space) {
480  LOG_ERROR("No RAM code space allocated for ROM call");
482  }
483  if (priv->ram_algo_space->size < batch_size) {
484  LOG_ERROR("RAM code space too small for call batch size of %u", n_calls);
485  return ERROR_BUF_TOO_SMALL;
486  }
487 
488  LOG_TARGET_DEBUG(target, "Calling batch of %u ROM functions:", n_calls);
489  for (unsigned int i = 0; i < n_calls; ++i) {
490  LOG_DEBUG(" func @ %" PRIx32, calls[i].pc);
491  LOG_DEBUG(" sp = %" PRIx32, calls[i].sp);
492  for (unsigned int j = 0; j < 4; ++j)
493  LOG_DEBUG(" a%u = %" PRIx32, j, calls[i].args[j]);
494  }
495 
496  if (n_calls <= 0) {
497  LOG_DEBUG("Returning early from call of 0 ROM functions");
498  return ERROR_OK;
499  }
500 
501  const uint8_t *algo_code;
502  if (is_arm(target_to_arm(target))) {
503  if (target_to_arm(target)->arch == ARM_ARCH_V8M) {
504  LOG_DEBUG("Using algo: rp2xxx_rom_call_batch_algo_armv8m");
506  } else {
507  LOG_DEBUG("Using algo: rp2xxx_rom_call_batch_algo_armv6m");
509  }
510  } else {
511  LOG_DEBUG("Using algo: rp2xxx_rom_call_batch_algo_riscv");
513  }
514 
515  uint8_t *batch_bf = malloc(batch_size);
516  if (!batch_bf) {
517  LOG_ERROR("No memory for batch buffer");
518  return ERROR_FAIL;
519  }
520  memcpy(batch_bf, algo_code, ROM_CALL_BATCH_ALGO_SIZE_BYTES);
521  unsigned int words = n_calls * sizeof(struct rp2xxx_rom_call_batch_record)
522  / sizeof(uint32_t);
524  &batch_bf[ROM_CALL_BATCH_ALGO_SIZE_BYTES], words,
525  (const uint32_t *)calls);
526  /* Null terminator */
527  target_buffer_set_u32(target, &batch_bf[batch_size - sizeof(uint32_t)], 0);
528 
529  int err = target_write_buffer(target,
530  priv->ram_algo_space->address,
531  batch_size,
532  batch_bf
533  );
534  free(batch_bf);
535 
536  if (err != ERROR_OK) {
537  LOG_ERROR("Failed to write ROM batch algorithm to RAM code space");
538  return err;
539  }
540 
541  // Call into the ROM batch algorithm -- this will in turn call each ROM
542  // call specified by the batch records.
543  target_addr_t algo_start_addr = priv->ram_algo_space->address;
544  target_addr_t algo_end_addr = priv->ram_algo_space->address + rp2xxx_rom_call_batch_algo_bkpt_offset;
545  unsigned int algo_timeout_ms = 3000;
546  if (is_arm(target_to_arm(target))) {
547  struct armv7m_algorithm alg_info;
549  alg_info.core_mode = ARM_MODE_THREAD;
551  0, NULL, /* No memory arguments */
552  0, NULL, /* No register arguments */
553  algo_start_addr, algo_end_addr,
554  algo_timeout_ms,
555  &alg_info
556  );
557  } else {
558  // Presumed RISC-V -- there is no RISCV_COMMON_MAGIC on older OpenOCD
560  0, NULL, /* No memory arguments */
561  0, NULL, /* No register arguments */
562  algo_start_addr, algo_end_addr,
563  algo_timeout_ms,
564  NULL /* Currently no RISC-V-specific algorithm info */
565  );
566  }
567  if (err != ERROR_OK) {
568  LOG_ERROR("Failed to call ROM function batch");
569  /* This case is hit when loading new ROM images on FPGA, but can also be hit on real
570  hardware if you swap two devices with different ROM versions without restarting OpenOCD: */
571  LOG_ROM_SYMBOL_DEBUG("Repopulating ROM address cache after failed ROM call");
572  /* We ignore the error on this next call because we have already failed, this is just
573  recovery for the next attempt. */
575  return err;
576  }
577  return ERROR_OK;
578 }
579 
580 // Call a single ROM function, using the default algorithm stack.
582  uint16_t func_offset, uint32_t argdata[], unsigned int n_args)
583 {
584  assert(n_args <= 4); /* only allow register arguments -- capped at just 4 on Arm */
585 
586  if (!priv->stack) {
587  LOG_ERROR("no stack for flash programming code");
589  }
590  target_addr_t stacktop = priv->stack->address + priv->stack->size;
591 
592  struct rp2xxx_rom_call_batch_record call = {
593  .pc = func_offset,
594  .sp = stacktop
595  };
596  for (unsigned int i = 0; i < n_args; ++i)
597  call.args[i] = argdata[i];
598 
599  return rp2xxx_call_rom_func_batch(target, priv, &call, 1);
600 }
601 
603 {
604  // Attempt to reset ACCESSCTRL, in case Secure access to SRAM has been
605  // blocked, which will stop us from loading/running algorithms such as RCP
606  // init. (Also ROM, QMI regs are needed later)
607  uint32_t accessctrl_lock_reg;
608  if (target_read_u32(target, ACCESSCTRL_LOCK_OFFSET, &accessctrl_lock_reg) != ERROR_OK) {
609  LOG_ERROR("Failed to read ACCESSCTRL lock register");
610  // Failed to read an APB register which should always be readable from
611  // any security/privilege level. Something fundamental is wrong. E.g.:
612  //
613  // - The debugger is attempting to perform Secure bus accesses on a
614  // system where Secure debug has been disabled
615  // - clk_sys or busfabric clock are stopped (try doing a rescue reset)
616  return ERROR_FAIL;
617  }
618  if (accessctrl_lock_reg & ACCESSCTRL_LOCK_DEBUG_BITS) {
619  LOG_ERROR("ACCESSCTRL is locked, so can't reset permissions. Following steps might fail");
620  } else {
621  LOG_DEBUG("Reset ACCESSCTRL permissions via CFGRESET");
623  }
624  return ERROR_OK;
625 }
626 
628 {
629  // Flash algorithms (and the RCP init stub called by this function) must
630  // run in the Secure state, so flip the state now before attempting to
631  // execute any code on the core.
632  int retval;
633  uint32_t dscsr;
634  retval = target_read_u32(target, DCB_DSCSR, &dscsr);
635  if (retval != ERROR_OK) {
636  LOG_ERROR("RP2350 init ARM core: DSCSR read failed");
637  return retval;
638  }
639 
640  LOG_DEBUG("DSCSR: 0x%08" PRIx32, dscsr);
641  if (!(dscsr & DSCSR_CDS)) {
642  LOG_DEBUG("Setting Current Domain Secure in DSCSR");
643  retval = target_write_u32(target, DCB_DSCSR, (dscsr & ~DSCSR_CDSKEY) | DSCSR_CDS);
644  if (retval != ERROR_OK) {
645  LOG_ERROR("RP2350 init ARM core: DSCSR read failed");
646  return retval;
647  }
648  }
649 
650  if (!priv->stack) {
651  LOG_ERROR("No stack for flash programming code");
653  }
654 
655  if (!priv->ram_algo_space || priv->ram_algo_space->size < sizeof(rcp_init_code)) {
656  LOG_ERROR("No algorithm space for rcp_init code");
657  return ERROR_BUF_TOO_SMALL;
658  }
659 
660  int err = target_write_memory(target,
661  priv->ram_algo_space->address,
662  1,
663  sizeof(rcp_init_code),
665  );
666  if (err != ERROR_OK) {
667  LOG_ERROR("Failed to load rcp_init algorithm into RAM");
668  return ERROR_FAIL;
669  }
670 
671  LOG_TARGET_DEBUG(target, "Calling rcp_init, code at " TARGET_ADDR_FMT,
672  priv->ram_algo_space->address);
673 
674  /* Actually call the function */
675  struct armv7m_algorithm alg_info;
677  alg_info.core_mode = ARM_MODE_THREAD;
679  0, NULL, /* No memory arguments */
680  0, NULL, /* No register arguments */
681  priv->ram_algo_space->address,
682  priv->ram_algo_space->address + rcp_init_code_bkpt_offset,
683  1000, /* 1s timeout */
684  &alg_info
685  );
686  if (err != ERROR_OK) {
687  LOG_ERROR("Failed to invoke rcp_init");
688  return err;
689  }
690 
691  return err;
692 }
693 
695 {
696  int err = ERROR_OK;
697 
698  if (!priv->stack) {
699  /* target_alloc_working_area always allocates multiples of 4 bytes, so no worry about alignment */
701  if (err != ERROR_OK) {
702  LOG_ERROR("Could not allocate stack for flash programming code -- insufficient space");
704  }
705  }
706 
707  if (!priv->ram_algo_space) {
709  if (err != ERROR_OK) {
710  LOG_ERROR("Could not allocate RAM code space for ROM calls -- insufficient space");
712  }
713  }
714 
715  if (IS_RP2350(priv->id)) {
717  if (err != ERROR_OK) {
718  LOG_ERROR("Failed to init ACCESSCTRL before ROM call");
719  return err;
720  }
721  if (is_arm(target_to_arm(target))) {
723  if (err != ERROR_OK) {
724  LOG_ERROR("Failed to init Arm core 0 before ROM call");
725  return err;
726  }
727  }
728  uint32_t reset_args[1] = {
730  };
731  if (!priv->jump_bootrom_reset_state) {
732  LOG_WARNING("RP2350 flash: no bootrom_reset_method");
733  } else {
734  /* This is mainly required to clear varmulet_enclosing_cpu pointers on RISC-V, in case
735  an Arm -> RISC-V call has been interrupted (these pointers are used to handle
736  reentrant calls to the ROM emulator) */
737  LOG_DEBUG("Clearing core 0 ROM state");
738  err = rp2xxx_call_rom_func(target, priv, priv->jump_bootrom_reset_state,
739  reset_args, ARRAY_SIZE(reset_args));
740  if (err != ERROR_OK) {
741  LOG_ERROR("RP2350 flash: failed to call reset core state");
742  return err;
743  }
744  }
745  if (!is_arm(target_to_arm(target)) && priv->jump_bootrom_set_varm_stack) {
746  /* Pass {0, 0} to set_varmulet_user_stack() to enable automatic emulation of Arm APIs
747  using the ROM's default stacks. Usually the bootrom does this before exiting to user
748  code, but it needs to be done manually when the USB bootloader has been interrupted. */
749  LOG_DEBUG("Enabling default Arm emulator stacks for RISC-V ROM calls");
750  struct working_area *set_stack_mem_args;
751  err = target_alloc_working_area(target, 2 * sizeof(uint32_t), &set_stack_mem_args);
752  if (err != ERROR_OK) {
753  LOG_ERROR("Failed to allocate memory for arguments to set_varmulet_user_stack()");
754  return err;
755  }
756 
757  err = target_write_u32(target, set_stack_mem_args->address, 0);
758  if (err == ERROR_OK)
759  err = target_write_u32(target, set_stack_mem_args->address + 4, 0);
760 
761  if (err != ERROR_OK) {
762  LOG_ERROR("Failed to initialise memory arguments for set_varmulet_user_stack()");
763  target_free_working_area(target, set_stack_mem_args);
764  return err;
765  }
766 
767  uint32_t set_stack_register_args[1] = {
768  set_stack_mem_args->address
769  };
770  err = rp2xxx_call_rom_func(target, priv, priv->jump_bootrom_set_varm_stack,
771  set_stack_register_args, ARRAY_SIZE(set_stack_register_args));
772  target_free_working_area(target, set_stack_mem_args);
773  if (err != ERROR_OK) {
774  LOG_ERROR("Failed to initialise Arm emulation stacks for RISC-V");
775  return err;
776  }
777  }
778  }
779 
780  LOG_DEBUG("Connecting flash IOs and issuing XIP exit sequence to flash");
781  struct rp2xxx_rom_call_batch_record calls[2] = {
782  {
783  .pc = priv->jump_connect_internal_flash,
784  .sp = priv->stack->address + priv->stack->size
785  },
786  {
787  .pc = priv->jump_flash_exit_xip,
788  .sp = priv->stack->address + priv->stack->size
789  }
790  };
791  err = rp2xxx_call_rom_func_batch(target, priv, calls, 2);
792  if (err != ERROR_OK) {
793  LOG_ERROR("RP2xxx flash: failed to exit flash XIP mode");
794  return err;
795  }
796 
797  return ERROR_OK;
798 }
799 
801 {
802  // Flash content has changed. We can now do a bit of poking to make
803  // the new flash contents visible to us via memory-mapped (XIP) interface
804  // in the 0x1... memory region.
805 
806  LOG_DEBUG("Flushing flash cache after write behind");
807 
808  struct rp2xxx_rom_call_batch_record finishing_calls[2] = {
809  {
810  .pc = priv->jump_flush_cache,
811  .sp = priv->stack->address + priv->stack->size
812  },
813  {
814  .sp = priv->stack->address + priv->stack->size
815  },
816  };
817 
818  int num_finishing_calls = 1;
819  // Note on RP2350 it's not *required* to call flash_enter_cmd_xip, since
820  // the ROM leaves flash XIPable by default in between direct-mode
821  // accesses
822  if (IS_RP2040(priv->id)) {
823  finishing_calls[num_finishing_calls++].pc = priv->jump_enter_cmd_xip;
824  } else if (priv->jump_flash_reset_address_trans) {
825  // Note flash_reset_address_trans function does not exist on older devices
826  finishing_calls[num_finishing_calls++].pc = priv->jump_flash_reset_address_trans;
827  }
828 
829  int retval = rp2xxx_call_rom_func_batch(target, priv, finishing_calls, num_finishing_calls);
830  if (retval != ERROR_OK)
831  LOG_ERROR("RP2xxx: failed to flush flash cache/restore XIP");
832 
833  return retval;
834 }
835 
837 {
838  /* OpenOCD is prone to trashing work-area allocations on target state
839  transitions, which leaves us with stale work area pointers in our
840  driver state. Best to clean up our allocations manually after
841  completing each flash call, so we know to make fresh ones next time. */
842  LOG_DEBUG("Cleaning up after flash operations");
843  if (priv->stack) {
845  priv->stack = 0;
846  }
847  if (priv->ram_algo_space) {
848  target_free_working_area(target, priv->ram_algo_space);
849  priv->ram_algo_space = 0;
850  }
851 }
852 
853 static int rp2xxx_flash_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
854 {
855  LOG_DEBUG("Writing %" PRIu32 " bytes starting at 0x%" PRIx32, count, offset);
856 
857  struct rp2xxx_flash_bank *priv = bank->driver_priv;
858  struct target *target = bank->target;
859 
860  if (target->state != TARGET_HALTED) {
861  LOG_ERROR("Target not halted");
863  }
864 
865  struct working_area *bounce = NULL;
866 
867  int err = setup_for_raw_flash_cmd(target, priv);
868  if (err != ERROR_OK)
869  goto cleanup_and_return;
870 
872  /* We try to allocate working area rounded down to device page size,
873  * al least 1 page, at most the write data size */
874  unsigned int chunk_size = MIN(MAX(avail_pages, 1) * RP2XXX_ROM_API_FIXED_FLASH_PAGE, count);
875  err = target_alloc_working_area(target, chunk_size, &bounce);
876  if (err != ERROR_OK) {
877  LOG_ERROR("Could not allocate bounce buffer for flash programming. Can't continue");
878  goto cleanup_and_return;
879  }
880 
881  LOG_DEBUG("Allocated flash bounce buffer @" TARGET_ADDR_FMT, bounce->address);
882 
883  while (count > 0) {
884  uint32_t write_size = count > chunk_size ? chunk_size : count;
885  LOG_DEBUG("Writing %" PRIu32 " bytes to offset 0x%" PRIx32,
886  write_size, offset);
887  err = target_write_buffer(target, bounce->address, write_size, buffer);
888  if (err != ERROR_OK) {
889  LOG_ERROR("Could not load data into target bounce buffer");
890  break;
891  }
892  uint32_t args[3] = {
893  offset, /* addr */
894  bounce->address, /* data */
895  write_size /* count */
896  };
897  err = rp2xxx_call_rom_func(target, priv, priv->jump_flash_range_program,
898  args, ARRAY_SIZE(args));
899  keep_alive();
900  if (err != ERROR_OK) {
901  LOG_ERROR("Failed to invoke flash programming code on target");
902  break;
903  }
904 
905  buffer += write_size;
906  offset += write_size;
907  count -= write_size;
908  }
909 
910 cleanup_and_return:
912 
913  /* Don't propagate error or user gets fooled the flash write failed */
915 
917  return err;
918 }
919 
920 static int rp2xxx_flash_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
921 {
922  struct rp2xxx_flash_bank *priv = bank->driver_priv;
923  struct target *target = bank->target;
924 
925  if (target->state != TARGET_HALTED) {
926  LOG_ERROR("Target not halted");
928  }
929 
930  uint32_t offset_start = bank->sectors[first].offset;
931  uint32_t offset_last = bank->sectors[last].offset + bank->sectors[last].size;
932  uint32_t length = offset_last - offset_start;
933  LOG_DEBUG("erase %" PRIu32 " bytes starting at 0x%" PRIx32,
934  length, offset_start);
935 
936  int err = setup_for_raw_flash_cmd(target, priv);
937  if (err != ERROR_OK)
938  goto cleanup_and_return;
939 
940  uint32_t offset_next = offset_start;
941 
942  /* Break erase into multiple calls to avoid timeout on large erase. Choose 128k chunk which has
943  fairly low ROM call overhead and empirically seems to avoid the default keep_alive() limit
944  as well as our ROM call timeout. */
945  const uint32_t erase_chunk_size = 128 * 1024;
946 
947  /* Promote log level for long erases to provide feedback */
948  bool requires_loud_prints = offset_last - offset_next >= 2 * erase_chunk_size;
949  enum log_levels chunk_log_level = requires_loud_prints ? LOG_LVL_INFO : LOG_LVL_DEBUG;
950 
951  while (offset_next < offset_last) {
952  uint32_t remaining = offset_last - offset_next;
953  uint32_t call_size = remaining < erase_chunk_size ? remaining : erase_chunk_size;
954  /* Shorten the first call of a large erase if necessary to align subsequent calls */
955  if (offset_next % erase_chunk_size != 0 && call_size == erase_chunk_size)
956  call_size = erase_chunk_size - offset_next % erase_chunk_size;
957 
958  LOG_CUSTOM_LEVEL(chunk_log_level,
959  " Erase chunk: 0x%08" PRIx32 " -> 0x%08" PRIx32,
960  offset_next,
961  offset_next + call_size - 1
962  );
963 
964  /* This ROM function uses the optimal mixture of 4k 20h and 64k D8h erases, without
965  over-erase. This is why we force the flash_bank sector size attribute to 4k even if
966  OpenOCD prefers to give the block size instead. */
967  uint32_t args[4] = {
968  offset_next,
969  call_size,
970  65536, /* block_size */
971  0xd8 /* block_cmd */
972  };
973 
974  err = rp2xxx_call_rom_func(target, priv, priv->jump_flash_range_erase, args, ARRAY_SIZE(args));
975  keep_alive();
976 
977  if (err != ERROR_OK)
978  break;
979 
980  offset_next += call_size;
981  }
982 
983 
984 cleanup_and_return:
985  /* Don't propagate error or user gets fooled the flash erase failed */
987 
989  return err;
990 }
991 
992 /* -----------------------------------------------------------------------------
993  Driver probing etc */
994 
995 
996 static int rp2040_ssel_active(struct target *target, bool active)
997 {
999  uint32_t val;
1000 
1001  int err = target_read_u32(target, RP2040_QSPI_CTRL, &val);
1002  if (err != ERROR_OK)
1003  return err;
1004 
1005  val = (val & ~RP2040_QSPI_CTRL_OUTOVER_MASK) | state;
1006 
1008  if (err != ERROR_OK)
1009  return err;
1010 
1011  return ERROR_OK;
1012 }
1013 
1014 static int rp2040_spi_tx_rx(struct target *target,
1015  const uint8_t *tx, unsigned int tx_len,
1016  unsigned int dummy_len,
1017  uint8_t *rx, unsigned int rx_len)
1018 {
1019  int retval, retval2;
1020 
1021  retval = rp2040_ssel_active(target, true);
1022  if (retval != ERROR_OK) {
1023  LOG_ERROR("QSPI select failed");
1024  goto deselect;
1025  }
1026 
1027  unsigned int tx_cnt = 0;
1028  unsigned int rx_cnt = 0;
1029  unsigned int xfer_len = tx_len + dummy_len + rx_len;
1030  while (rx_cnt < xfer_len) {
1031  int in_flight = tx_cnt - rx_cnt;
1032  if (tx_cnt < xfer_len && in_flight < 14) {
1033  uint32_t dr = tx_cnt < tx_len ? tx[tx_cnt] : 0;
1035  if (retval != ERROR_OK)
1036  break;
1037 
1038  tx_cnt++;
1039  continue;
1040  }
1041  uint32_t dr;
1042  retval = target_read_u32(target, RP2040_SSI_DR0, &dr);
1043  if (retval != ERROR_OK)
1044  break;
1045 
1046  if (rx_cnt >= tx_len + dummy_len)
1047  rx[rx_cnt - tx_len - dummy_len] = (uint8_t)dr;
1048 
1049  rx_cnt++;
1050  }
1051 
1052 deselect:
1053  retval2 = rp2040_ssel_active(target, false);
1054 
1055  if (retval != ERROR_OK) {
1056  LOG_ERROR("QSPI Tx/Rx failed");
1057  return retval;
1058  }
1059  if (retval2 != ERROR_OK)
1060  LOG_ERROR("QSPI deselect failed");
1061 
1062  return retval2;
1063 }
1064 
1065 static int rp2350_spi_tx_rx(struct target *target,
1066  const uint8_t *tx, unsigned int tx_len,
1067  unsigned int dummy_len,
1068  uint8_t *rx, unsigned int rx_len)
1069 {
1070  uint32_t direct_csr;
1071  int retval = target_read_u32(target, RP2350_QMI_DIRECT_CSR, &direct_csr);
1072  if (retval != ERROR_OK) {
1073  LOG_ERROR("QMI DIRECT_CSR read failed");
1074  return retval;
1075  }
1077  retval = target_write_u32(target, RP2350_QMI_DIRECT_CSR, direct_csr);
1078  if (retval != ERROR_OK) {
1079  LOG_ERROR("QMI DIRECT mode enable failed");
1080  goto deselect;
1081  }
1082 
1083  unsigned int tx_cnt = 0;
1084  unsigned int rx_cnt = 0;
1085  unsigned int xfer_len = tx_len + dummy_len + rx_len;
1086  while (tx_cnt < xfer_len || rx_cnt < rx_len) {
1087  int in_flight = tx_cnt - tx_len - dummy_len - rx_cnt;
1088  if (tx_cnt < xfer_len && in_flight < 4) {
1089  uint32_t tx_cmd;
1090  if (tx_cnt < tx_len)
1091  tx_cmd = tx[tx_cnt] | RP2350_QMI_DIRECT_TX_NOPUSH | RP2350_QMI_DIRECT_TX_OE;
1092  else if (tx_cnt < tx_len + dummy_len)
1093  tx_cmd = RP2350_QMI_DIRECT_TX_NOPUSH;
1094  else
1095  tx_cmd = 0;
1096 
1097  retval = target_write_u32(target, RP2350_QMI_DIRECT_TX, tx_cmd);
1098  if (retval != ERROR_OK)
1099  break;
1100 
1101  tx_cnt++;
1102  continue;
1103  }
1104  if (rx_cnt < rx_len) {
1105  uint32_t dr;
1107  if (retval != ERROR_OK)
1108  break;
1109 
1110  rx[rx_cnt] = (uint8_t)dr;
1111  rx_cnt++;
1112  }
1113  }
1114 
1115 deselect:
1117  int retval2 = target_write_u32(target, RP2350_QMI_DIRECT_CSR, direct_csr);
1118 
1119  if (retval != ERROR_OK) {
1120  LOG_ERROR("QSPI Tx/Rx failed");
1121  return retval;
1122  }
1123  if (retval2 != ERROR_OK)
1124  LOG_ERROR("QMI DIRECT mode disable failed");
1125 
1126  return retval2;
1127 }
1128 
1129 static int rp2xxx_spi_tx_rx(struct flash_bank *bank,
1130  const uint8_t *tx, unsigned int tx_len,
1131  unsigned int dummy_len,
1132  uint8_t *rx, unsigned int rx_len)
1133 {
1134  struct rp2xxx_flash_bank *priv = bank->driver_priv;
1135  struct target *target = bank->target;
1136 
1137  if (IS_RP2040(priv->id))
1138  return rp2040_spi_tx_rx(target, tx, tx_len, dummy_len, rx, rx_len);
1139  else if (IS_RP2350(priv->id))
1140  return rp2350_spi_tx_rx(target, tx, tx_len, dummy_len, rx, rx_len);
1141  else
1142  return ERROR_FAIL;
1143 }
1144 
1145 static int rp2xxx_read_sfdp_block(struct flash_bank *bank, uint32_t addr,
1146  unsigned int words, uint32_t *buffer)
1147 {
1148  struct rp2xxx_flash_bank *priv = bank->driver_priv;
1149 
1150  uint8_t cmd[4] = { SPIFLASH_READ_SFDP };
1151  uint8_t data[4 * words + priv->sfdp_dummy_detect];
1152 
1153  h_u24_to_be(&cmd[1], addr);
1154 
1155  int retval = rp2xxx_spi_tx_rx(bank, cmd, sizeof(cmd), priv->sfdp_dummy,
1156  data, 4 * words + priv->sfdp_dummy_detect);
1157  if (retval != ERROR_OK)
1158  return retval;
1159 
1160  if (priv->sfdp_dummy_detect) {
1161  for (unsigned int i = 0; i < priv->sfdp_dummy_detect; i++)
1162  if (le_to_h_u32(&data[i]) == SFDP_MAGIC) {
1163  priv->sfdp_dummy_detect = 0;
1164  priv->sfdp_dummy = i;
1165  break;
1166  }
1167  for (unsigned int i = 0; i < words; i++)
1168  buffer[i] = le_to_h_u32(&data[4 * i + priv->sfdp_dummy]);
1169  } else {
1170  for (unsigned int i = 0; i < words; i++)
1171  buffer[i] = le_to_h_u32(&data[4 * i]);
1172  }
1173  return retval;
1174 }
1175 
1177 {
1178  struct rp2xxx_flash_bank *priv = bank->driver_priv;
1179  struct target *target = bank->target;
1180 
1181  int retval = target_read_u32(target, RP2XXX_SYSINFO_CHIP_ID, &priv->id);
1182  if (retval != ERROR_OK) {
1183  LOG_ERROR("SYSINFO CHIP_ID read failed");
1184  return retval;
1185  }
1186  if (!IS_RP2040(priv->id) && !IS_RP2350(priv->id)) {
1187  LOG_ERROR("Unknown SYSINFO CHIP_ID 0x%08" PRIx32, priv->id);
1188  return ERROR_FLASH_BANK_INVALID;
1189  }
1190 
1192  if (retval != ERROR_OK)
1193  return retval;
1194 
1195  /* the Boot ROM flash_range_program() routine requires page alignment */
1196  bank->write_start_alignment = RP2XXX_ROM_API_FIXED_FLASH_PAGE;
1197  bank->write_end_alignment = RP2XXX_ROM_API_FIXED_FLASH_PAGE;
1198 
1199  uint32_t flash_id = 0;
1200  if (priv->size_override) {
1201  priv->spi_dev.name = "size override";
1202  LOG_DEBUG("SPI flash autodetection disabled, using configured size");
1203  } else {
1204  if (target->state != TARGET_HALTED) {
1205  LOG_ERROR("Target not halted");
1206  return ERROR_TARGET_NOT_HALTED;
1207  }
1208 
1209  bank->size = 0;
1210 
1212  /* ignore error, flash size detection could work anyway */
1213 
1214  const uint8_t cmd[] = { SPIFLASH_READ_ID };
1215  uint8_t data[3];
1216  retval = rp2xxx_spi_tx_rx(bank, cmd, sizeof(cmd), 0, data, sizeof(data));
1217  if (retval == ERROR_OK) {
1218  flash_id = le_to_h_u24(data);
1219 
1220  /* search for a SPI flash Device ID match */
1221  for (const struct flash_device *p = flash_devices; p->name ; p++) {
1222  if (p->device_id == flash_id) {
1223  priv->spi_dev = *p;
1224  bank->size = p->size_in_bytes;
1225  break;
1226  }
1227  }
1228  }
1229 
1230  if (bank->size == 0) {
1231  priv->sfdp_dummy_detect = 8;
1232  priv->sfdp_dummy = 0;
1233  retval = spi_sfdp(bank, &priv->spi_dev, &rp2xxx_read_sfdp_block);
1234  if (retval == ERROR_OK)
1235  bank->size = priv->spi_dev.size_in_bytes;
1236  }
1237 
1239  }
1240 
1241  snprintf(priv->dev_name, sizeof(priv->dev_name), "%s rev %u",
1242  IS_RP2350(priv->id) ? "RP2350" : "RP2040",
1244 
1245  if (bank->size == 0) {
1246  LOG_ERROR("%s, QSPI Flash id = 0x%06" PRIx32 " not recognised",
1247  priv->dev_name, flash_id);
1248  return ERROR_FLASH_BANK_INVALID;
1249  }
1250 
1251  bank->num_sectors = bank->size / RP2XXX_ROM_API_FIXED_FLASH_SECTOR;
1252 
1253  if (priv->size_override) {
1254  LOG_INFO("%s, QSPI Flash size override = %u KiB in %u sectors",
1255  priv->dev_name, bank->size / 1024, bank->num_sectors);
1256  } else {
1257  LOG_INFO("%s, QSPI Flash %s id = 0x%06" PRIx32 " size = %u KiB in %u sectors",
1258  priv->dev_name, priv->spi_dev.name, flash_id,
1259  bank->size / 1024, bank->num_sectors);
1260  }
1261 
1262  free(bank->sectors);
1263  bank->sectors = alloc_block_array(0, RP2XXX_ROM_API_FIXED_FLASH_SECTOR, bank->num_sectors);
1264  if (!bank->sectors)
1265  return ERROR_FAIL;
1266 
1267  priv->probed = true;
1268 
1269  return ERROR_OK;
1270 }
1271 
1273 {
1274  struct rp2xxx_flash_bank *priv = bank->driver_priv;
1275 
1276  if (priv->probed)
1277  return ERROR_OK;
1278 
1279  return rp2xxx_flash_probe(bank);
1280 }
1281 
1282 /* -----------------------------------------------------------------------------
1283  Driver boilerplate */
1284 
1285 FLASH_BANK_COMMAND_HANDLER(rp2xxx_flash_bank_command)
1286 {
1287  struct rp2xxx_flash_bank *priv;
1288  priv = calloc(1, sizeof(struct rp2xxx_flash_bank));
1289  priv->size_override = bank->size != 0;
1290 
1291  /* Set up driver_priv */
1292  bank->driver_priv = priv;
1293 
1294  return ERROR_OK;
1295 }
1296 
1297 
1298 COMMAND_HANDLER(rp2xxx_rom_api_call_handler)
1299 {
1300  if (CMD_ARGC < 1)
1302 
1303  struct target *target = get_current_target(CMD->ctx);
1304 
1305  struct flash_bank *bank;
1306  for (bank = flash_bank_list(); bank; bank = bank->next) {
1307  if (bank->driver != &rp2xxx_flash)
1308  continue;
1309 
1310  if (bank->target == target)
1311  break;
1312  }
1313 
1314  if (!bank) {
1315  command_print(CMD, "[%s] No associated RP2xxx flash bank found",
1316  target_name(target));
1317  return ERROR_FAIL;
1318  }
1319 
1320  int retval = rp2xxx_flash_auto_probe(bank);
1321  if (retval != ERROR_OK) {
1322  command_print(CMD, "auto_probe failed");
1323  return retval;
1324  }
1325 
1326  uint16_t tag = MAKE_TAG(CMD_ARGV[0][0], CMD_ARGV[0][1]);
1327 
1328  uint32_t args[4] = { 0 };
1329  for (unsigned int i = 0; i + 1 < CMD_ARGC && i < ARRAY_SIZE(args); i++)
1330  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[i + 1], args[i]);
1331 
1332  if (target->state != TARGET_HALTED) {
1333  command_print(CMD, "Target not halted");
1334  return ERROR_TARGET_NOT_HALTED;
1335  }
1336 
1337  struct rp2xxx_flash_bank *priv = bank->driver_priv;
1338  retval = setup_for_raw_flash_cmd(target, priv);
1339  if (retval != ERROR_OK)
1340  goto cleanup_and_return;
1341 
1342  uint16_t symtype_func = is_arm(target_to_arm(target))
1344 
1345  uint16_t fc;
1346  retval = rp2xxx_lookup_rom_symbol(target, tag, symtype_func, &fc);
1347  if (retval != ERROR_OK) {
1348  command_print(CMD, "Function %.2s not found in RP2xxx ROM",
1349  CMD_ARGV[0]);
1350  goto cleanup_and_return;
1351  }
1352 
1353  /* command_print() output gets lost if the command is called
1354  * in an event handler, use LOG_INFO instead */
1355  LOG_INFO("RP2xxx ROM API function %.2s @ %04" PRIx16, CMD_ARGV[0], fc);
1356 
1357  retval = rp2xxx_call_rom_func(target, priv, fc, args, ARRAY_SIZE(args));
1358  if (retval != ERROR_OK)
1359  command_print(CMD, "RP2xxx ROM API call failed");
1360 
1361 cleanup_and_return:
1363  return retval;
1364 }
1365 
1366 COMMAND_HANDLER(rp2xxx_switch_target_handler)
1367 {
1368  if (CMD_ARGC != 2)
1370 
1371  struct target *old_target = get_target(CMD_ARGV[0]);
1372  if (!old_target) {
1373  command_print(CMD, "Unrecognised old target %s", CMD_ARGV[0]);
1375  }
1376 
1377  struct target *new_target = get_target(CMD_ARGV[1]);
1378  if (!new_target) {
1379  command_print(CMD, "Unrecognised new target %s", CMD_ARGV[1]);
1381  }
1382 
1383  struct flash_bank *bank;
1384  for (bank = flash_bank_list(); bank; bank = bank->next) {
1385  if (bank->driver == &rp2xxx_flash) {
1386  if (bank->target == old_target) {
1387  bank->target = new_target;
1388  struct rp2xxx_flash_bank *priv = bank->driver_priv;
1389  priv->probed = false;
1390  return ERROR_OK;
1391  } else if (bank->target == new_target) {
1392  return ERROR_OK;
1393  }
1394  }
1395  }
1396 
1397  command_print(CMD, "Neither old nor new target %s found in flash bank list",
1398  CMD_ARGV[0]);
1399  return ERROR_FAIL;
1400 }
1401 
1402 static const struct command_registration rp2xxx_exec_command_handlers[] = {
1403  {
1404  .name = "rom_api_call",
1405  .mode = COMMAND_EXEC,
1406  .help = "arbitrary ROM API call",
1407  .usage = "fc [p0 [p1 [p2 [p3]]]]",
1408  .handler = rp2xxx_rom_api_call_handler,
1409  },
1410  {
1411  .name = "_switch_target",
1412  .mode = COMMAND_EXEC,
1413  .help = "internal use",
1414  .usage = "old_target new_target",
1415  .handler = rp2xxx_switch_target_handler,
1416  },
1418 };
1419 
1420 static const struct command_registration rp2xxx_command_handler[] = {
1421  {
1422  .name = "rp2xxx",
1423  .mode = COMMAND_ANY,
1424  .help = "rp2xxx flash controller commands",
1425  .usage = "",
1427  },
1429 };
1430 
1431 const struct flash_driver rp2xxx_flash = {
1432  .name = "rp2xxx",
1433  .commands = rp2xxx_command_handler,
1434  .flash_bank_command = rp2xxx_flash_bank_command,
1435  .erase = rp2xxx_flash_erase,
1436  .write = rp2xxx_flash_write,
1437  .read = default_flash_read,
1438  .probe = rp2xxx_flash_probe,
1439  .auto_probe = rp2xxx_flash_auto_probe,
1440  .erase_check = default_flash_blank_check,
1441  .free_driver_priv = default_flash_free_driver_priv
1442 };
@ ARM_ARCH_V8M
Definition: arm.h:58
static bool is_arm(struct arm *arm)
Definition: arm.h:267
@ ARM_MODE_THREAD
Definition: arm.h:94
static struct arm * target_to_arm(const struct target *target)
Convert target handle to generic ARM target state handle.
Definition: arm.h:261
#define ARMV7M_COMMON_MAGIC
Definition: armv7m.h:224
Support functions to access arbitrary bits in a byte array.
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:376
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:141
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:156
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:402
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:151
#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:442
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:253
#define ERROR_COMMAND_ARGUMENT_INVALID
Definition: command.h:404
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
#define DSCSR_CDS
Definition: cortex_m.h:154
#define DCB_DSCSR
Definition: cortex_m.h:83
#define DSCSR_CDSKEY
Definition: cortex_m.h:153
uint64_t buffer
Pointer to data buffer to send over SPI.
Definition: dw-spi-helper.h:0
uint8_t bank
Definition: esirisc.c:135
static struct esp_usb_jtag * priv
Definition: esp_usb_jtag.c:219
uint8_t length
Definition: esp_usb_jtag.c:1
#define ERROR_FLASH_BANK_INVALID
Definition: flash/common.h:28
struct flash_sector * alloc_block_array(uint32_t offset, uint32_t size, unsigned int num_blocks)
Allocate and fill an array of sectors or protection blocks.
int default_flash_blank_check(struct flash_bank *bank)
Provides default erased-bank check handling.
int default_flash_read(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
Provides default read implementation for flash memory.
void default_flash_free_driver_priv(struct flash_bank *bank)
Deallocates bank->driver_priv.
struct flash_bank * flash_bank_list(void)
uint32_t dr
Definition: imx_gpio.c:0
void keep_alive(void)
Definition: log.c:426
#define LOG_CUSTOM_LEVEL(level, expr ...)
Definition: log.h:118
#define LOG_WARNING(expr ...)
Definition: log.h:130
#define ERROR_FAIL
Definition: log.h:174
#define ERROR_BUF_TOO_SMALL
Definition: log.h:170
#define LOG_TARGET_DEBUG(target, fmt_str,...)
Definition: log.h:150
#define LOG_ERROR(expr ...)
Definition: log.h:133
#define LOG_INFO(expr ...)
Definition: log.h:127
#define LOG_DEBUG(expr ...)
Definition: log.h:110
#define ERROR_OK
Definition: log.h:168
log_levels
Definition: log.h:40
@ LOG_LVL_INFO
Definition: log.h:46
@ LOG_LVL_DEBUG
Definition: log.h:47
#define sp
Definition: mips32.c:222
#define MIN(a, b)
Definition: replacements.h:22
#define MAX(a, b)
Definition: replacements.h:25
#define RP2350_QMI_DIRECT_CSR
Definition: rp2xxx.c:56
#define RP2040_QSPI_CTRL_OUTOVER_LOW
Definition: rp2xxx.c:53
static const uint8_t rp2xxx_rom_call_batch_algo_riscv[ROM_CALL_BATCH_ALGO_SIZE_BYTES]
Definition: rp2xxx.c:164
#define RP2350_QMI_DIRECT_RX
Definition: rp2xxx.c:58
#define LOG_ROM_SYMBOL_DEBUG
Definition: rp2xxx.c:215
#define RP2350_QMI_DIRECT_TX
Definition: rp2xxx.c:57
#define FUNC_FLASH_RANGE_ERASE
Definition: rp2xxx.c:25
static int rp2350_init_arm_core0(struct target *target, struct rp2xxx_flash_bank *priv)
Definition: rp2xxx.c:627
static int rp2350_a0_lookup_symbol(struct target *target, uint16_t tag, uint16_t flags, uint16_t *symbol_out)
Definition: rp2xxx.c:255
static const struct command_registration rp2xxx_command_handler[]
Definition: rp2xxx.c:1420
static const int rp2xxx_rom_call_batch_algo_bkpt_offset
Definition: rp2xxx.c:117
#define BOOTROM_RP2350_MAGIC
Definition: rp2xxx.c:18
static int rp2xxx_call_rom_func(struct target *target, struct rp2xxx_flash_bank *priv, uint16_t func_offset, uint32_t argdata[], unsigned int n_args)
Definition: rp2xxx.c:581
#define RP2040_QSPI_CTRL_OUTOVER_MASK
Definition: rp2xxx.c:52
#define RT_FLAG_FUNC_RISCV
Definition: rp2xxx.c:34
#define MAKE_TAG(a, b)
Definition: rp2xxx.c:22
#define FUNC_FLASH_FLUSH_CACHE
Definition: rp2xxx.c:27
static const uint8_t rcp_init_code[]
Definition: rp2xxx.c:93
static int rp2xxx_flash_probe(struct flash_bank *bank)
Definition: rp2xxx.c:1176
static int rp2xxx_invalidate_cache_restore_xip(struct target *target, struct rp2xxx_flash_bank *priv)
Definition: rp2xxx.c:800
#define RP2040_QSPI_CTRL_OUTOVER_HIGH
Definition: rp2xxx.c:54
const struct flash_driver rp2xxx_flash
Definition: rp2xxx.c:1431
#define RP2040_SSI_DR0
Definition: rp2xxx.c:49
#define RT_FLAG_DATA
Definition: rp2xxx.c:37
#define FUNC_FLASH_RESET_ADDRESS_TRANS
Definition: rp2xxx.c:31
#define RP2350_QMI_DIRECT_CSR_EN
Definition: rp2xxx.c:60
#define ROM_CALL_BATCH_ALGO_SIZE_BYTES
Definition: rp2xxx.c:116
#define FUNC_FLASH_EXIT_XIP
Definition: rp2xxx.c:23
static const struct command_registration rp2xxx_exec_command_handlers[]
Definition: rp2xxx.c:1402
#define RP2040_QSPI_CTRL
Definition: rp2xxx.c:50
COMMAND_HANDLER(rp2xxx_rom_api_call_handler)
Definition: rp2xxx.c:1298
#define ACCESSCTRL_WRITE_PASSWORD
Definition: rp2xxx.c:47
#define FUNC_BOOTROM_SET_STACK
Definition: rp2xxx.c:30
#define RP2XXX_CHIP_ID_REVISION(id)
Definition: rp2xxx.c:73
#define BOOTROM_STATE_RESET_CURRENT_CORE
Definition: rp2xxx.c:40
static int rp2350_spi_tx_rx(struct target *target, const uint8_t *tx, unsigned int tx_len, unsigned int dummy_len, uint8_t *rx, unsigned int rx_len)
Definition: rp2xxx.c:1065
static int rp2040_spi_tx_rx(struct target *target, const uint8_t *tx, unsigned int tx_len, unsigned int dummy_len, uint8_t *rx, unsigned int rx_len)
Definition: rp2xxx.c:1014
static void cleanup_after_raw_flash_cmd(struct target *target, struct rp2xxx_flash_bank *priv)
Definition: rp2xxx.c:836
static int rp2xxx_spi_tx_rx(struct flash_bank *bank, const uint8_t *tx, unsigned int tx_len, unsigned int dummy_len, uint8_t *rx, unsigned int rx_len)
Definition: rp2xxx.c:1129
#define ACCESSCTRL_LOCK_DEBUG_BITS
Definition: rp2xxx.c:45
#define RT_FLAG_FUNC_ARM_SEC
Definition: rp2xxx.c:35
static int rp2xxx_call_rom_func_batch(struct target *target, struct rp2xxx_flash_bank *priv, struct rp2xxx_rom_call_batch_record *calls, unsigned int n_calls)
Definition: rp2xxx.c:471
#define FUNC_BOOTROM_STATE_RESET
Definition: rp2xxx.c:29
#define RP2XXX_MAX_RAM_ALGO_SIZE
Definition: rp2xxx.c:76
FLASH_BANK_COMMAND_HANDLER(rp2xxx_flash_bank_command)
Definition: rp2xxx.c:1285
#define ACCESSCTRL_CFGRESET_OFFSET
Definition: rp2xxx.c:46
#define ACCESSCTRL_LOCK_OFFSET
Definition: rp2xxx.c:44
#define FUNC_CONNECT_INTERNAL_FLASH
Definition: rp2xxx.c:24
#define RP2XXX_ROM_API_FIXED_FLASH_PAGE
Definition: rp2xxx.c:78
#define IS_RP2040(id)
Definition: rp2xxx.c:70
static int rp2xxx_flash_auto_probe(struct flash_bank *bank)
Definition: rp2xxx.c:1272
static int rp2xxx_populate_rom_pointer_cache(struct target *target, struct rp2xxx_flash_bank *priv)
Definition: rp2xxx.c:394
#define RP2350_QMI_DIRECT_TX_OE
Definition: rp2xxx.c:63
#define BOOTROM_MAGIC_ADDR
Definition: rp2xxx.c:20
static int rp2xxx_flash_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
Definition: rp2xxx.c:853
static const uint8_t rp2xxx_rom_call_batch_algo_armv6m[ROM_CALL_BATCH_ALGO_SIZE_BYTES]
Definition: rp2xxx.c:118
#define RP2XXX_ROM_API_FIXED_FLASH_SECTOR
Definition: rp2xxx.c:79
static int rp2350_lookup_rom_symbol(struct target *target, uint32_t ptr_to_entry, uint16_t tag, uint16_t flags, uint16_t *symbol_out)
Definition: rp2xxx.c:300
#define IS_RP2350(id)
Definition: rp2xxx.c:72
static int rp2350_init_accessctrl(struct target *target)
Definition: rp2xxx.c:602
#define RP2XXX_MAX_ALGO_STACK_USAGE
Definition: rp2xxx.c:75
#define BOOTROM_MAGIC_MASK
Definition: rp2xxx.c:19
#define FUNC_FLASH_RANGE_PROGRAM
Definition: rp2xxx.c:26
#define BOOTROM_RP2040_MAGIC
Definition: rp2xxx.c:16
static const int rcp_init_code_bkpt_offset
Definition: rp2xxx.c:92
#define RP2350_QMI_DIRECT_TX_NOPUSH
Definition: rp2xxx.c:62
static int rp2040_ssel_active(struct target *target, bool active)
Definition: rp2xxx.c:996
static int rp2xxx_read_sfdp_block(struct flash_bank *bank, uint32_t addr, unsigned int words, uint32_t *buffer)
Definition: rp2xxx.c:1145
#define RP2XXX_SYSINFO_CHIP_ID
Definition: rp2xxx.c:65
#define RP2350_QMI_DIRECT_CSR_ASSERT_CS0N
Definition: rp2xxx.c:61
static int rp2040_lookup_rom_symbol(struct target *target, uint16_t tag, uint16_t flags, uint16_t *symbol_out)
Definition: rp2xxx.c:218
static int setup_for_raw_flash_cmd(struct target *target, struct rp2xxx_flash_bank *priv)
Definition: rp2xxx.c:694
static int rp2xxx_flash_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
Definition: rp2xxx.c:920
static int rp2xxx_lookup_rom_symbol(struct target *target, uint16_t tag, uint16_t flags, uint16_t *symbol_out)
Definition: rp2xxx.c:364
static const uint8_t rp2xxx_rom_call_batch_algo_armv8m[ROM_CALL_BATCH_ALGO_SIZE_BYTES]
Definition: rp2xxx.c:142
#define FUNC_FLASH_ENTER_CMD_XIP
Definition: rp2xxx.c:28
target_addr_t addr
Start address to search for the control block.
Definition: rtt/rtt.c:28
int spi_sfdp(struct flash_bank *bank, struct flash_device *dev, read_sfdp_block_t read_sfdp_block)
Definition: sfdp.c:60
#define SFDP_MAGIC
Definition: sfdp.h:10
const struct flash_device flash_devices[]
Definition: spi.c:24
#define SPIFLASH_READ_SFDP
Definition: spi.h:80
#define SPIFLASH_READ_ID
Definition: spi.h:72
unsigned int common_magic
Definition: armv7m.h:299
enum arm_mode core_mode
Definition: armv7m.h:301
const char * name
Definition: command.h:235
Provides details of a flash bank, available either on-chip or through a major interface.
Definition: nor/core.h:75
const char * name
Definition: spi.h:21
Provides the implementation-independent structure that defines all of the callbacks required by OpenO...
Definition: nor/driver.h:39
const char * name
Gives a human-readable name of this flash driver, This field is used to select and initialize the dri...
Definition: nor/driver.h:44
uint16_t jump_connect_internal_flash
Definition: rp2xxx.c:199
uint16_t jump_flash_reset_address_trans
Definition: rp2xxx.c:203
uint16_t jump_flash_range_erase
Definition: rp2xxx.c:200
uint16_t jump_bootrom_reset_state
Definition: rp2xxx.c:205
uint16_t jump_enter_cmd_xip
Definition: rp2xxx.c:204
struct working_area * stack
Definition: rp2xxx.c:193
unsigned int sfdp_dummy_detect
Definition: rp2xxx.c:211
uint32_t id
Definition: rp2xxx.c:192
struct working_area * ram_algo_space
Definition: rp2xxx.c:196
uint16_t jump_flush_cache
Definition: rp2xxx.c:202
uint16_t jump_flash_exit_xip
Definition: rp2xxx.c:198
struct flash_device spi_dev
Definition: rp2xxx.c:210
uint16_t jump_bootrom_set_varm_stack
Definition: rp2xxx.c:206
bool size_override
Definition: rp2xxx.c:209
unsigned int sfdp_dummy
Definition: rp2xxx.c:211
uint16_t jump_flash_range_program
Definition: rp2xxx.c:201
char dev_name[20]
Definition: rp2xxx.c:208
Definition: target.h:116
enum target_state state
Definition: target.h:157
target_addr_t address
Definition: target.h:86
struct target * get_target(const char *id)
Definition: target.c:441
void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
Definition: target.c:360
int target_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
Definition: target.c:2350
int target_run_algorithm(struct target *target, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_param, target_addr_t entry_point, target_addr_t exit_point, unsigned int timeout_ms, void *arch_info)
Downloads a target-specific native code algorithm to the target, and executes it.
Definition: target.c:782
int target_write_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
Write count items of size bytes to the memory of target at the address given.
Definition: target.c:1274
uint32_t target_get_working_area_avail(struct target *target)
Definition: target.c:2173
int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
Definition: target.c:2069
int target_write_u32(struct target *target, target_addr_t address, uint32_t value)
Definition: target.c:2649
int target_free_working_area(struct target *target, struct working_area *area)
Free a working area.
Definition: target.c:2127
int target_read_u16(struct target *target, target_addr_t address, uint16_t *value)
Definition: target.c:2582
int target_read_u32(struct target *target, target_addr_t address, uint32_t *value)
Definition: target.c:2558
struct target * get_current_target(struct command_context *cmd_ctx)
Definition: target.c:466
void target_buffer_set_u32_array(struct target *target, uint8_t *buffer, uint32_t count, const uint32_t *srcbuf)
Definition: target.c:425
#define ERROR_TARGET_NOT_HALTED
Definition: target.h:783
static const char * target_name(const struct target *target)
Returns the instance-specific name of the specified target.
Definition: target.h:233
@ TARGET_HALTED
Definition: target.h:56
#define ERROR_TARGET_RESOURCE_NOT_AVAILABLE
Definition: target.h:787
static uint32_t le_to_h_u24(const uint8_t *buf)
Definition: types.h:117
#define TARGET_ADDR_FMT
Definition: types.h:342
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.
Definition: types.h:57
uint64_t target_addr_t
Definition: types.h:335
static void h_u24_to_be(uint8_t *buf, unsigned int val)
Definition: types.h:201
static uint32_t le_to_h_u32(const uint8_t *buf)
Definition: types.h:112
#define NULL
Definition: usb.h:16
uint8_t cmd
Definition: vdebug.c:1
uint8_t offset[4]
Definition: vdebug.c:9
uint8_t state[4]
Definition: vdebug.c:21
uint8_t count[4]
Definition: vdebug.c:22