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')
55 FLASH_ID(
"autodetect disabled", 0x03, 0x00, 0x02, 0xd8, 0xc7, 0, 0x100, 0x10000, 0);
67 LOG_ERROR(
"Incorrect RP2040 BOOT ROM version");
84 if (entry_tag == tag) {
94 uint16_t func_offset, uint32_t argdata[],
unsigned int n_args,
unsigned int timeout_ms)
96 char *regnames[4] = {
"r0",
"r1",
"r2",
"r3" };
101 LOG_ERROR(
"no stack for flash programming code");
111 for (
unsigned int i = 0; i < n_args; ++i) {
117 buf_set_u32(args[n_args].value, 0, 32, func_offset);
120 buf_set_u32(args[n_args + 1].value, 0, 32, stacktop);
121 unsigned int n_reg_params = n_args + 2;
123 for (
unsigned int i = 0; i < n_reg_params; ++i)
133 priv->jump_debug_trampoline,
priv->jump_debug_trampoline_end,
138 for (
unsigned int i = 0; i < n_reg_params; ++i)
142 LOG_ERROR(
"Failed to invoke ROM function @0x%" PRIx16, func_offset);
161 LOG_DEBUG(
"Flushing flash cache after write behind");
164 LOG_ERROR(
"Failed to flush flash cache");
168 LOG_DEBUG(
"Configuring SSI for execute-in-place");
171 LOG_ERROR(
"Failed to set SSI to XIP mode");
190 const int STACK_SIZE = 256;
193 LOG_ERROR(
"Could not allocate stack for flash programming code");
200 LOG_ERROR(
"Failed to connect internal flash");
204 LOG_DEBUG(
"Kicking flash out of XIP mode");
207 LOG_ERROR(
"Failed to exit flash XIP mode");
236 unsigned int chunk_size =
MIN(
MAX(avail_pages, 1) *
priv->dev->pagesize,
count);
239 LOG_ERROR(
"Could not allocate bounce buffer for flash programming. Can't continue");
246 uint32_t write_size =
count > chunk_size ? chunk_size :
count;
247 LOG_DEBUG(
"Writing %d bytes to offset 0x%" PRIx32, write_size,
offset);
250 LOG_ERROR(
"Could not load data into target bounce buffer");
261 LOG_ERROR(
"Failed to invoke flash programming code on target");
288 uint32_t start_addr =
bank->sectors[first].offset;
289 uint32_t
length =
bank->sectors[last].offset +
bank->sectors[last].size - start_addr;
290 LOG_DEBUG(
"RP2040 erase %d bytes starting at 0x%" PRIx32,
length, start_addr);
296 LOG_DEBUG(
"Remote call flash_range_erase");
299 bank->sectors[first].offset,
300 bank->sectors[last].offset +
bank->sectors[last].size -
bank->sectors[first].offset,
301 priv->dev->sectorsize,
315 unsigned int timeout_ms = 2000 * (last - first) + 1000;
331 const uint32_t qspi_ctrl_outover_low = 2UL << 8;
332 const uint32_t qspi_ctrl_outover_high = 3UL << 8;
333 uint32_t
state = (active) ? qspi_ctrl_outover_low : qspi_ctrl_outover_high;
340 val = (val & ~qspi_ctrl_outover_high) |
state;
351 uint32_t device_id = 0;
366 device_id |= (
status & 0xFF) << 24;
370 *devid = device_id >> 8;
391 LOG_ERROR(
"Debug trampoline not found in RP2040 ROM.");
394 priv->jump_debug_trampoline &= ~1u;
398 LOG_ERROR(
"Debug trampoline end not found in RP2040 ROM.");
401 priv->jump_debug_trampoline_end &= ~1u;
405 LOG_ERROR(
"Function FUNC_FLASH_EXIT_XIP not found in RP2040 ROM.");
411 LOG_ERROR(
"Function FUNC_CONNECT_INTERNAL_FLASH not found in RP2040 ROM.");
417 LOG_ERROR(
"Function FUNC_FLASH_RANGE_ERASE not found in RP2040 ROM.");
423 LOG_ERROR(
"Function FUNC_FLASH_RANGE_PROGRAM not found in RP2040 ROM.");
429 LOG_ERROR(
"Function FUNC_FLASH_FLUSH_CACHE not found in RP2040 ROM.");
435 LOG_ERROR(
"Function FUNC_FLASH_ENTER_CMD_XIP not found in RP2040 ROM.");
442 LOG_DEBUG(
"SPI flash autodetection disabled, using configured size");
448 uint32_t device_id = 0;
460 if (p->device_id == device_id) {
466 LOG_ERROR(
"Unknown flash device (ID 0x%08" PRIx32
")", device_id);
469 LOG_INFO(
"Found flash device '%s' (ID 0x%08" PRIx32
")",
470 priv->dev->name,
priv->dev->device_id);
472 bank->size =
priv->dev->size_in_bytes;
476 bank->write_start_alignment =
priv->dev->pagesize;
477 bank->write_end_alignment =
priv->dev->pagesize;
504 free(
bank->driver_priv);
515 priv->probed =
false;
524 .
name =
"rp2040_flash",
525 .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 int first, unsigned int 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 int first, unsigned int 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
const 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, unsigned int timeout_ms)
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 const struct flash_device rp2040_default_spi_device
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)
#define FUNC_FLASH_ENTER_CMD_XIP
const struct flash_device flash_devices[]
#define FLASH_ID(n, re, qr, pp, es, ces, id, psize, ssize, size)
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)
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.
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_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.