17 #define BOOTROM_MAGIC 0x01754d
18 #define BOOTROM_MAGIC_ADDR 0x00000010
25 #define MAKE_TAG(a, b) (((b)<<8) | a)
26 #define FUNC_DEBUG_TRAMPOLINE MAKE_TAG('D', 'T')
27 #define FUNC_DEBUG_TRAMPOLINE_END MAKE_TAG('D', 'E')
28 #define FUNC_FLASH_EXIT_XIP MAKE_TAG('E', 'X')
29 #define FUNC_CONNECT_INTERNAL_FLASH MAKE_TAG('I', 'F')
30 #define FUNC_FLASH_RANGE_ERASE MAKE_TAG('R', 'E')
31 #define FUNC_FLASH_RANGE_PROGRAM MAKE_TAG('R', 'P')
32 #define FUNC_FLASH_FLUSH_CACHE MAKE_TAG('F', 'C')
33 #define FUNC_FLASH_ENTER_CMD_XIP MAKE_TAG('C', 'X')
63 LOG_ERROR(
"Incorrect RP2040 BOOT ROM version");
80 if (entry_tag == tag) {
90 uint16_t func_offset, uint32_t argdata[],
unsigned int n_args,
int timeout_ms)
92 char *regnames[4] = {
"r0",
"r1",
"r2",
"r3" };
97 LOG_ERROR(
"no stack for flash programming code");
107 for (
unsigned int i = 0; i < n_args; ++i) {
113 buf_set_u32(args[n_args].value, 0, 32, func_offset);
116 buf_set_u32(args[n_args + 1].value, 0, 32, stacktop);
117 unsigned int n_reg_params = n_args + 2;
119 for (
unsigned int i = 0; i < n_reg_params; ++i)
129 priv->jump_debug_trampoline,
priv->jump_debug_trampoline_end,
134 for (
unsigned int i = 0; i < n_reg_params; ++i)
138 LOG_ERROR(
"Failed to invoke ROM function @0x%" PRIx16, func_offset);
157 LOG_DEBUG(
"Flushing flash cache after write behind");
160 LOG_ERROR(
"Failed to flush flash cache");
164 LOG_DEBUG(
"Configuring SSI for execute-in-place");
167 LOG_ERROR(
"Failed to set SSI to XIP mode");
186 const int STACK_SIZE = 256;
189 LOG_ERROR(
"Could not allocate stack for flash programming code");
196 LOG_ERROR(
"Failed to connect internal flash");
200 LOG_DEBUG(
"Kicking flash out of XIP mode");
203 LOG_ERROR(
"Failed to exit flash XIP mode");
232 unsigned int chunk_size =
MIN(
MAX(avail_pages, 1) *
priv->dev->pagesize,
count);
235 LOG_ERROR(
"Could not allocate bounce buffer for flash programming. Can't continue");
242 uint32_t write_size =
count > chunk_size ? chunk_size :
count;
243 LOG_DEBUG(
"Writing %d bytes to offset 0x%" PRIx32, write_size,
offset);
246 LOG_ERROR(
"Could not load data into target bounce buffer");
257 LOG_ERROR(
"Failed to invoke flash programming code on target");
284 uint32_t start_addr =
bank->sectors[first].offset;
285 uint32_t
length =
bank->sectors[last].offset +
bank->sectors[last].size - start_addr;
286 LOG_DEBUG(
"RP2040 erase %d bytes starting at 0x%" PRIx32,
length, start_addr);
292 LOG_DEBUG(
"Remote call flash_range_erase");
295 bank->sectors[first].offset,
296 bank->sectors[last].offset +
bank->sectors[last].size -
bank->sectors[first].offset,
297 priv->dev->sectorsize,
311 int timeout_ms = 2000 * (last - first) + 1000;
327 const uint32_t qspi_ctrl_outover_low = 2UL << 8;
328 const uint32_t qspi_ctrl_outover_high = 3UL << 8;
329 uint32_t
state = (active) ? qspi_ctrl_outover_low : qspi_ctrl_outover_high;
336 val = (val & ~qspi_ctrl_outover_high) |
state;
347 uint32_t device_id = 0;
362 device_id |= (
status & 0xFF) << 24;
366 *devid = device_id >> 8;
387 LOG_ERROR(
"Debug trampoline not found in RP2040 ROM.");
390 priv->jump_debug_trampoline &= ~1u;
394 LOG_ERROR(
"Debug trampoline end not found in RP2040 ROM.");
397 priv->jump_debug_trampoline_end &= ~1u;
401 LOG_ERROR(
"Function FUNC_FLASH_EXIT_XIP not found in RP2040 ROM.");
407 LOG_ERROR(
"Function FUNC_CONNECT_INTERNAL_FLASH not found in RP2040 ROM.");
413 LOG_ERROR(
"Function FUNC_FLASH_RANGE_ERASE not found in RP2040 ROM.");
419 LOG_ERROR(
"Function FUNC_FLASH_RANGE_PROGRAM not found in RP2040 ROM.");
425 LOG_ERROR(
"Function FUNC_FLASH_FLUSH_CACHE not found in RP2040 ROM.");
431 LOG_ERROR(
"Function FUNC_FLASH_ENTER_CMD_XIP not found in RP2040 ROM.");
437 uint32_t device_id = 0;
449 if (p->device_id == device_id) {
455 LOG_ERROR(
"Unknown flash device (ID 0x%08" PRIx32
")", device_id);
459 LOG_INFO(
"Found flash device \'%s\' (ID 0x%08" PRIx32
")",
460 priv->dev->name,
priv->dev->device_id);
463 bank->write_start_alignment =
priv->dev->pagesize;
464 bank->write_end_alignment =
priv->dev->pagesize;
466 bank->size =
priv->dev->size_in_bytes;
493 free(
bank->driver_priv);
504 priv->probed =
false;
513 .
name =
"rp2040_flash",
514 .flash_bank_command = rp2040_flash_bank_command,
void init_reg_param(struct reg_param *param, char *reg_name, uint32_t size, enum param_direction direction)
void destroy_reg_param(struct reg_param *param)
#define ARMV7M_COMMON_MAGIC
Support functions to access arbitrary bits in a byte array.
static uint32_t buf_get_u32(const uint8_t *_buffer, unsigned first, unsigned num)
Retrieves num bits from _buffer, starting at the first bit, returning the bits in a 32-bit word.
static void buf_set_u32(uint8_t *_buffer, unsigned first, unsigned num, uint32_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
static struct esp_usb_jtag * priv
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.
#define LOG_TARGET_DEBUG(target, fmt_str,...)
#define LOG_ERROR(expr ...)
#define LOG_INFO(expr ...)
#define LOG_DEBUG(expr ...)
#define FUNC_FLASH_RANGE_ERASE
static int rp2040_flash_auto_probe(struct flash_bank *bank)
static int rp2040_flash_probe(struct flash_bank *bank)
#define FUNC_FLASH_FLUSH_CACHE
#define FUNC_DEBUG_TRAMPOLINE_END
static int rp2040_finalize_stack_free(struct flash_bank *bank)
#define FUNC_FLASH_EXIT_XIP
static int rp2040_flash_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
static int rp2040_spi_read_flash_id(struct target *target, uint32_t *devid)
#define FUNC_CONNECT_INTERNAL_FLASH
FLASH_BANK_COMMAND_HANDLER(rp2040_flash_bank_command)
static int rp2040_stack_grab_and_prep(struct flash_bank *bank)
#define BOOTROM_MAGIC_ADDR
static int rp2040_flash_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
static void rp2040_flash_free_driver_priv(struct flash_bank *bank)
static uint32_t rp2040_lookup_symbol(struct target *target, uint32_t tag, uint16_t *symbol)
#define FUNC_FLASH_RANGE_PROGRAM
#define FUNC_DEBUG_TRAMPOLINE
static int rp2040_ssel_active(struct target *target, bool active)
struct flash_driver rp2040_flash
static int rp2040_call_rom_func(struct target *target, struct rp2040_flash_bank *priv, uint16_t func_offset, uint32_t argdata[], unsigned int n_args, int timeout_ms)
#define FUNC_FLASH_ENTER_CMD_XIP
const struct flash_device flash_devices[]
unsigned int common_magic
Provides details of a flash bank, available either on-chip or through a major interface.
Provides the implementation-independent structure that defines all of the callbacks required by OpenO...
const char * name
Gives a human-readable name of this flash driver, This field is used to select and initialize the dri...
uint16_t jump_debug_trampoline_end
uint16_t jump_flush_cache
const struct flash_device * dev
uint16_t jump_flash_range_program
uint16_t jump_flash_range_erase
struct working_area * stack
uint16_t jump_flash_exit_xip
uint16_t jump_connect_internal_flash
uint16_t jump_enter_cmd_xip
uint16_t jump_debug_trampoline
int target_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
uint32_t target_get_working_area_avail(struct target *target)
int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
int target_write_u32(struct target *target, target_addr_t address, uint32_t value)
int target_free_working_area(struct target *target, struct working_area *area)
Free a working area.
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, int timeout_ms, void *arch_info)
Downloads a target-specific native code algorithm to the target, and executes it.
int target_read_u16(struct target *target, target_addr_t address, uint16_t *value)
int target_read_u32(struct target *target, target_addr_t address, uint32_t *value)
#define ERROR_TARGET_NOT_HALTED
#define ERROR_TARGET_RESOURCE_NOT_AVAILABLE
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.