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