1
2
3
4
5
6
7
13
15
16
17
18
19
20
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
72
73
74
75
76
77
78
79
80
90
91
92
93
94
95
96
97
98
99
100
104
105
106
107
108
109
110
111
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
152
153
154
155
156
157
161
162
163
167
168
169
170
171
172
173
174
175
176
177
178
184
185
186
187
188
189
190
191
192
196
197
198
199
203
204
205
206
210
211
212
213
214
215
216
217
218
219
220
221
225
226
227
228
229
230
231
232
233
236
237
238
242
243
244
245
246
247
248
249
253
258
259
260
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
287
288
289
290
291
292
293
294
295
296
297
298
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
388
389
390
394
395
396
397
401
402
403
404
408
409
410
414
415
416
420
421
422
426
427
428
432
433
434
438
439
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
464
465
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
507
508
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "imp.h"
#include <helper/binarybuffer.h>
#include <target/algorithm.h>
#include <target/armv7m.h>
#include "spi.h"
5 includes
/* ... */
#define BOOTROM_MAGIC 0x01754d
#define BOOTROM_MAGIC_ADDR 0x00000010
/* ... */
#define MAKE_TAG(a, b) (((b)<<8) | a)
#define FUNC_DEBUG_TRAMPOLINE MAKE_TAG('D', 'T')
#define FUNC_DEBUG_TRAMPOLINE_END MAKE_TAG('D', 'E')
#define FUNC_FLASH_EXIT_XIP MAKE_TAG('E', 'X')
#define FUNC_CONNECT_INTERNAL_FLASH MAKE_TAG('I', 'F')
#define FUNC_FLASH_RANGE_ERASE MAKE_TAG('R', 'E')
#define FUNC_FLASH_RANGE_PROGRAM MAKE_TAG('R', 'P')
#define FUNC_FLASH_FLUSH_CACHE MAKE_TAG('F', 'C')
#define FUNC_FLASH_ENTER_CMD_XIP MAKE_TAG('C', 'X')
11 defines
struct rp2040_flash_bank {
bool probed;
struct working_area *stack;
uint16_t jump_debug_trampoline;
uint16_t jump_debug_trampoline_end;
uint16_t jump_flash_exit_xip;
uint16_t jump_connect_internal_flash;
uint16_t jump_flash_range_erase;
uint16_t jump_flash_range_program;
uint16_t jump_flush_cache;
uint16_t jump_enter_cmd_xip;
const struct flash_device *dev;
...};
static const struct flash_device rp2040_default_spi_device =
FLASH_ID("autodetect disabled", 0x03, 0x00, 0x02, 0xd8, 0xc7, 0, 0x100, 0x10000, 0);
static uint32_t rp2040_lookup_symbol(struct target *target, uint32_t tag, uint16_t *symbol)
{
uint32_t magic;
int err = target_read_u32(target, BOOTROM_MAGIC_ADDR, &magic);
if (err != ERROR_OK)
return err;
magic &= 0xffffff;
if (magic != BOOTROM_MAGIC) {
if (!((magic ^ BOOTROM_MAGIC)&0xffff))
LOG_ERROR("Incorrect RP2040 BOOT ROM version");
else
LOG_ERROR("RP2040 BOOT ROM not found");
return ERROR_FAIL;
}if (magic != BOOTROM_MAGIC) { ... }
uint16_t table_entry;
err = target_read_u16(target, BOOTROM_MAGIC_ADDR + 4, &table_entry);
if (err != ERROR_OK)
return err;
uint16_t entry_tag;
do {
err = target_read_u16(target, table_entry, &entry_tag);
if (err != ERROR_OK)
return err;
if (entry_tag == tag) {
return target_read_u16(target, table_entry + 2, symbol);
}if (entry_tag == tag) { ... }
table_entry += 4;
...} while (entry_tag);
return ERROR_FAIL;
}{ ... }
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)
{
char *regnames[4] = { "r0", "r1", "r2", "r3" };
assert(n_args <= ARRAY_SIZE(regnames));
if (!priv->stack) {
LOG_ERROR("no stack for flash programming code");
return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
}if (!priv->stack) { ... }
target_addr_t stacktop = priv->stack->address + priv->stack->size;
LOG_TARGET_DEBUG(target, "Calling ROM func @0x%" PRIx16 " with %u arguments", func_offset, n_args);
struct reg_param args[ARRAY_SIZE(regnames) + 2];
struct armv7m_algorithm alg_info;
for (unsigned int i = 0; i < n_args; ++i) {
init_reg_param(&args[i], regnames[i], 32, PARAM_OUT);
buf_set_u32(args[i].value, 0, 32, argdata[i]);
}for (unsigned int i = 0; i < n_args; ++i) { ... }
init_reg_param(&args[n_args], "r7", 32, PARAM_OUT);
buf_set_u32(args[n_args].value, 0, 32, func_offset);
init_reg_param(&args[n_args + 1], "sp", 32, PARAM_OUT);
buf_set_u32(args[n_args + 1].value, 0, 32, stacktop);
unsigned int n_reg_params = n_args + 2;
for (unsigned int i = 0; i < n_reg_params; ++i)
LOG_DEBUG("Set %s = 0x%" PRIx32, args[i].reg_name, buf_get_u32(args[i].value, 0, 32));
alg_info.common_magic = ARMV7M_COMMON_MAGIC;
alg_info.core_mode = ARM_MODE_THREAD;
int err = target_run_algorithm(
target,
0, NULL,
n_reg_params, args,
priv->jump_debug_trampoline, priv->jump_debug_trampoline_end,
timeout_ms,
&alg_info
);
for (unsigned int i = 0; i < n_reg_params; ++i)
destroy_reg_param(&args[i]);
if (err != ERROR_OK)
LOG_ERROR("Failed to invoke ROM function @0x%" PRIx16, func_offset);
return err;
}{ ... }
/* ... */
static int rp2040_finalize_stack_free(struct flash_bank *bank)
{
struct rp2040_flash_bank *priv = bank->driver_priv;
struct target *target = bank->target;
/* ... */
LOG_DEBUG("Flushing flash cache after write behind");
int err = rp2040_call_rom_func(target, priv, priv->jump_flush_cache, NULL, 0, 1000);
if (err != ERROR_OK) {
LOG_ERROR("Failed to flush flash cache");
}if (err != ERROR_OK) { ... }
LOG_DEBUG("Configuring SSI for execute-in-place");
err = rp2040_call_rom_func(target, priv, priv->jump_enter_cmd_xip, NULL, 0, 1000);
if (err != ERROR_OK)
LOG_ERROR("Failed to set SSI to XIP mode");
target_free_working_area(target, priv->stack);
priv->stack = NULL;
return err;
}{ ... }
/* ... */
static int rp2040_stack_grab_and_prep(struct flash_bank *bank)
{
struct rp2040_flash_bank *priv = bank->driver_priv;
struct target *target = bank->target;
const int STACK_SIZE = 256;
int err = target_alloc_working_area(target, STACK_SIZE, &priv->stack);
if (err != ERROR_OK) {
LOG_ERROR("Could not allocate stack for flash programming code");
return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
}if (err != ERROR_OK) { ... }
LOG_DEBUG("Connecting internal flash");
err = rp2040_call_rom_func(target, priv, priv->jump_connect_internal_flash, NULL, 0, 1000);
if (err != ERROR_OK) {
LOG_ERROR("Failed to connect internal flash");
return err;
}if (err != ERROR_OK) { ... }
LOG_DEBUG("Kicking flash out of XIP mode");
err = rp2040_call_rom_func(target, priv, priv->jump_flash_exit_xip, NULL, 0, 1000);
if (err != ERROR_OK) {
LOG_ERROR("Failed to exit flash XIP mode");
return err;
}if (err != ERROR_OK) { ... }
return ERROR_OK;
}{ ... }
static int rp2040_flash_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
{
LOG_DEBUG("Writing %d bytes starting at 0x%" PRIx32, count, offset);
struct rp2040_flash_bank *priv = bank->driver_priv;
struct target *target = bank->target;
if (target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
struct working_area *bounce = NULL;
int err = rp2040_stack_grab_and_prep(bank);
if (err != ERROR_OK)
goto cleanup;
unsigned int avail_pages = target_get_working_area_avail(target) / priv->dev->pagesize;
/* ... */
unsigned int chunk_size = MIN(MAX(avail_pages, 1) * priv->dev->pagesize, count);
err = target_alloc_working_area(target, chunk_size, &bounce);
if (err != ERROR_OK) {
LOG_ERROR("Could not allocate bounce buffer for flash programming. Can't continue");
goto cleanup;
}if (err != ERROR_OK) { ... }
LOG_DEBUG("Allocated flash bounce buffer @" TARGET_ADDR_FMT, bounce->address);
while (count > 0) {
uint32_t write_size = count > chunk_size ? chunk_size : count;
LOG_DEBUG("Writing %d bytes to offset 0x%" PRIx32, write_size, offset);
err = target_write_buffer(target, bounce->address, write_size, buffer);
if (err != ERROR_OK) {
LOG_ERROR("Could not load data into target bounce buffer");
break;
}if (err != ERROR_OK) { ... }
uint32_t args[3] = {
offset,
bounce->address,
write_size
...};
err = rp2040_call_rom_func(target, priv, priv->jump_flash_range_program,
args, ARRAY_SIZE(args), 3000);
if (err != ERROR_OK) {
LOG_ERROR("Failed to invoke flash programming code on target");
break;
}if (err != ERROR_OK) { ... }
buffer += write_size;
offset += write_size;
count -= write_size;
}while (count > 0) { ... }
cleanup:
target_free_working_area(target, bounce);
rp2040_finalize_stack_free(bank);
return err;
}{ ... }
static int rp2040_flash_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
{
struct rp2040_flash_bank *priv = bank->driver_priv;
struct target *target = bank->target;
if (target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
uint32_t start_addr = bank->sectors[first].offset;
uint32_t length = bank->sectors[last].offset + bank->sectors[last].size - start_addr;
LOG_DEBUG("RP2040 erase %d bytes starting at 0x%" PRIx32, length, start_addr);
int err = rp2040_stack_grab_and_prep(bank);
if (err != ERROR_OK)
goto cleanup;
LOG_DEBUG("Remote call flash_range_erase");
uint32_t args[4] = {
bank->sectors[first].offset,
bank->sectors[last].offset + bank->sectors[last].size - bank->sectors[first].offset,
priv->dev->sectorsize,
priv->dev->erase_cmd
...};
/* ... */
unsigned int timeout_ms = 2000 * (last - first) + 1000;
err = rp2040_call_rom_func(target, priv, priv->jump_flash_range_erase,
args, ARRAY_SIZE(args), timeout_ms);
cleanup:
rp2040_finalize_stack_free(bank);
return err;
}{ ... }
/* ... */
static int rp2040_ssel_active(struct target *target, bool active)
{
const target_addr_t qspi_ctrl_addr = 0x4001800c;
const uint32_t qspi_ctrl_outover_low = 2UL << 8;
const uint32_t qspi_ctrl_outover_high = 3UL << 8;
uint32_t state = (active) ? qspi_ctrl_outover_low : qspi_ctrl_outover_high;
uint32_t val;
int err = target_read_u32(target, qspi_ctrl_addr, &val);
if (err != ERROR_OK)
return err;
val = (val & ~qspi_ctrl_outover_high) | state;
err = target_write_u32(target, qspi_ctrl_addr, val);
if (err != ERROR_OK)
return err;
return ERROR_OK;
}{ ... }
static int rp2040_spi_read_flash_id(struct target *target, uint32_t *devid)
{
uint32_t device_id = 0;
const target_addr_t ssi_dr0 = 0x18000060;
int err = rp2040_ssel_active(target, true);
for (int count = 0; (count < 4) && (err == ERROR_OK); count++)
err = target_write_u32(target, ssi_dr0, SPIFLASH_READ_ID);
for (int count = 0; (count < 4) && (err == ERROR_OK); count++) {
uint32_t status;
err = target_read_u32(target, ssi_dr0, &status);
device_id >>= 8;
device_id |= (status & 0xFF) << 24;
}for (int count = 0; (count < 4) && (err == ERROR_OK); count++) { ... }
if (err == ERROR_OK)
*devid = device_id >> 8;
int err2 = rp2040_ssel_active(target, false);
if (err2 != ERROR_OK)
LOG_ERROR("SSEL inactive failed");
return err;
}{ ... }
static int rp2040_flash_probe(struct flash_bank *bank)
{
struct rp2040_flash_bank *priv = bank->driver_priv;
struct target *target = bank->target;
if (target->state != TARGET_HALTED) {
LOG_ERROR("Target not halted");
return ERROR_TARGET_NOT_HALTED;
}if (target->state != TARGET_HALTED) { ... }
int err = rp2040_lookup_symbol(target, FUNC_DEBUG_TRAMPOLINE, &priv->jump_debug_trampoline);
if (err != ERROR_OK) {
LOG_ERROR("Debug trampoline not found in RP2040 ROM.");
return err;
}if (err != ERROR_OK) { ... }
priv->jump_debug_trampoline &= ~1u;
err = rp2040_lookup_symbol(target, FUNC_DEBUG_TRAMPOLINE_END, &priv->jump_debug_trampoline_end);
if (err != ERROR_OK) {
LOG_ERROR("Debug trampoline end not found in RP2040 ROM.");
return err;
}if (err != ERROR_OK) { ... }
priv->jump_debug_trampoline_end &= ~1u;
err = rp2040_lookup_symbol(target, FUNC_FLASH_EXIT_XIP, &priv->jump_flash_exit_xip);
if (err != ERROR_OK) {
LOG_ERROR("Function FUNC_FLASH_EXIT_XIP not found in RP2040 ROM.");
return err;
}if (err != ERROR_OK) { ... }
err = rp2040_lookup_symbol(target, FUNC_CONNECT_INTERNAL_FLASH, &priv->jump_connect_internal_flash);
if (err != ERROR_OK) {
LOG_ERROR("Function FUNC_CONNECT_INTERNAL_FLASH not found in RP2040 ROM.");
return err;
}if (err != ERROR_OK) { ... }
err = rp2040_lookup_symbol(target, FUNC_FLASH_RANGE_ERASE, &priv->jump_flash_range_erase);
if (err != ERROR_OK) {
LOG_ERROR("Function FUNC_FLASH_RANGE_ERASE not found in RP2040 ROM.");
return err;
}if (err != ERROR_OK) { ... }
err = rp2040_lookup_symbol(target, FUNC_FLASH_RANGE_PROGRAM, &priv->jump_flash_range_program);
if (err != ERROR_OK) {
LOG_ERROR("Function FUNC_FLASH_RANGE_PROGRAM not found in RP2040 ROM.");
return err;
}if (err != ERROR_OK) { ... }
err = rp2040_lookup_symbol(target, FUNC_FLASH_FLUSH_CACHE, &priv->jump_flush_cache);
if (err != ERROR_OK) {
LOG_ERROR("Function FUNC_FLASH_FLUSH_CACHE not found in RP2040 ROM.");
return err;
}if (err != ERROR_OK) { ... }
err = rp2040_lookup_symbol(target, FUNC_FLASH_ENTER_CMD_XIP, &priv->jump_enter_cmd_xip);
if (err != ERROR_OK) {
LOG_ERROR("Function FUNC_FLASH_ENTER_CMD_XIP not found in RP2040 ROM.");
return err;
}if (err != ERROR_OK) { ... }
if (bank->size) {
priv->dev = &rp2040_default_spi_device;
LOG_DEBUG("SPI flash autodetection disabled, using configured size");
}if (bank->size) { ... } else {
err = rp2040_stack_grab_and_prep(bank);
uint32_t device_id = 0;
if (err == ERROR_OK)
err = rp2040_spi_read_flash_id(target, &device_id);
rp2040_finalize_stack_free(bank);
if (err != ERROR_OK)
return err;
priv->dev = NULL;
for (const struct flash_device *p = flash_devices; p->name ; p++)
if (p->device_id == device_id) {
priv->dev = p;
break;
}if (p->device_id == device_id) { ... }
if (!priv->dev) {
LOG_ERROR("Unknown flash device (ID 0x%08" PRIx32 ")", device_id);
return ERROR_FAIL;
}if (!priv->dev) { ... }
LOG_INFO("Found flash device '%s' (ID 0x%08" PRIx32 ")",
priv->dev->name, priv->dev->device_id);
bank->size = priv->dev->size_in_bytes;
}else { ... }
bank->write_start_alignment = priv->dev->pagesize;
bank->write_end_alignment = priv->dev->pagesize;
bank->num_sectors = bank->size / priv->dev->sectorsize;
LOG_INFO("RP2040 B0 Flash Probe: %" PRIu32 " bytes @" TARGET_ADDR_FMT ", in %u sectors\n",
bank->size, bank->base, bank->num_sectors);
bank->sectors = alloc_block_array(0, priv->dev->sectorsize, bank->num_sectors);
if (!bank->sectors)
return ERROR_FAIL;
if (err == ERROR_OK)
priv->probed = true;
return err;
}{ ... }
static int rp2040_flash_auto_probe(struct flash_bank *bank)
{
struct rp2040_flash_bank *priv = bank->driver_priv;
if (priv->probed)
return ERROR_OK;
return rp2040_flash_probe(bank);
}{ ... }
static void rp2040_flash_free_driver_priv(struct flash_bank *bank)
{
free(bank->driver_priv);
bank->driver_priv = NULL;
}{ ... }
/* ... */
FLASH_BANK_COMMAND_HANDLER(rp2040_flash_bank_command)
{
struct rp2040_flash_bank *priv;
priv = malloc(sizeof(struct rp2040_flash_bank));
priv->probed = false;
bank->driver_priv = priv;
return ERROR_OK;
}{ ... }
const struct flash_driver rp2040_flash = {
.name = "rp2040_flash",
.flash_bank_command = rp2040_flash_bank_command,
.erase = rp2040_flash_erase,
.write = rp2040_flash_write,
.read = default_flash_read,
.probe = rp2040_flash_probe,
.auto_probe = rp2040_flash_auto_probe,
.erase_check = default_flash_blank_check,
.free_driver_priv = rp2040_flash_free_driver_priv
...};