23 #define WATCHDOG_REFRESH_VALUE 0x6e524635
32 #define NRF5_FICR_REG(offset) (NRF51_52_FICR_BASE + (offset))
47 #define NRF5_UICR_REG(offset) (NRF51_52_UICR_BASE + (offset))
144 #define NRF51_DEVICE_DEF(id, pt, var, bcode, fsize) \
149 .build_code = bcode, \
150 .flash_size_kb = (fsize), \
151 .features = NRF5_FEATURE_SERIES_51, \
263 .info_variant = 0x104,
264 .info_package = 0x108,
273 .nvmc_base = 0x4001E000,
275 .watchdog_refresh_addr = 0x40010600,
286 .info_variant = 0x210,
287 .info_package = 0x214,
302 .info_variant = 0x148,
303 .info_package = 0x214,
320 .nvmc_base = 0x50039000,
322 .watchdog_refresh_addr = 0x50018600,
337 .nvmc_base = 0x41080000,
339 .watchdog_refresh_addr = 0x41080000,
347 struct nrf5_bank *nbank =
bank->driver_priv;
348 return nbank->probed;
353 struct nrf5_bank *nbank =
bank->driver_priv;
361 return nbank == &chip->
bank[1];
378 int timeout_ms = 340;
390 LOG_ERROR(
"Error waiting NVMC_READY: generic flash write/erase error (check protection etc...)");
394 if (ready == 0x00000001)
399 }
while ((
timeval_ms()-ts_start) < timeout_ms);
401 LOG_DEBUG(
"Timed out waiting for NVMC_READY");
413 LOG_ERROR(
"Failed to enable erase operation");
423 LOG_ERROR(
"Erase enable did not complete");
436 LOG_ERROR(
"Failed to enable write operation");
446 LOG_ERROR(
"Write enable did not complete");
459 LOG_ERROR(
"Failed to disable write/erase operation");
468 LOG_ERROR(
"Read only enable did not complete");
479 struct nrf5_bank *nbank =
bank->driver_priv;
485 LOG_ERROR(
"Couldn't read code region 0 size[FICR]");
489 if (clenr0 == 0xFFFFFFFF) {
493 LOG_ERROR(
"Couldn't read code region 0 size[UICR]");
498 for (
unsigned int i = 0; i <
bank->num_sectors; i++)
499 bank->sectors[i].is_protected =
500 clenr0 != 0xFFFFFFFF &&
bank->sectors[i].offset < clenr0;
508 struct nrf5_bank *nbank =
bank->driver_priv;
511 static uint32_t nrf5_bprot_offsets[4] = { 0x600, 0x604, 0x610, 0x614 };
512 uint32_t bprot_reg = 0;
515 for (
unsigned int i = 0; i <
bank->num_sectors; i++) {
516 unsigned int bit = i % 32;
518 unsigned int n_reg = i / 32;
526 bank->sectors[i].is_protected = (bprot_reg & (1 <<
bit)) ? 1 : 0;
533 struct nrf5_bank *nbank =
bank->driver_priv;
546 LOG_WARNING(
"Flash protection of this nRF device is not supported");
555 uint32_t clenr0, ppfc;
557 struct nrf5_bank *nbank =
bank->driver_priv;
561 LOG_ERROR(
"Code region 0 must start at the beginning of the bank");
568 LOG_ERROR(
"Couldn't read PPFC register");
572 if ((ppfc & 0xFF) == 0x00) {
573 LOG_ERROR(
"Code region 0 size was pre-programmed at the factory, can't change flash protection settings");
580 LOG_ERROR(
"Couldn't read code region 0 size from UICR");
584 if (!set || clenr0 != 0xFFFFFFFF) {
585 LOG_ERROR(
"You need to perform chip erase before changing the protection settings");
593 clenr0 =
bank->sectors[last].offset +
bank->sectors[last].size;
602 LOG_INFO(
"A reset or power cycle is required for the new protection settings to take effect.");
604 LOG_ERROR(
"Couldn't write code region 0 size to UICR");
615 struct nrf5_bank *nbank =
bank->driver_priv;
620 LOG_ERROR(
"UICR page does not support protection");
632 LOG_ERROR(
"Flash protection setting is not supported on this nRF5 device");
645 if (isalnum(b[0]) && isalnum(b[1]) && isalnum(b[2]) &&
646 (isalnum(b[3]) || b[3] == 0)) {
669 res = snprintf(buf, buf_size,
"nRF%s-%s(build code: %s)",
678 res = snprintf(buf, buf_size,
"nRF%" PRIx32
"-%s",
681 res = snprintf(buf, buf_size,
"nRF%" PRIx32
"-%s%.2s(build code: %s)",
684 variant, &variant[2]);
687 res = snprintf(buf, buf_size,
"nRF51xxx (HWID 0x%04" PRIx16
")", chip->
hwid);
691 if (res <= 0 || (
unsigned int)res >= buf_size) {
692 LOG_ERROR(
"BUG: buffer problem in %s", __func__);
700 struct nrf5_bank *nbank =
bank->driver_priv;
703 char chip_type_str[256];
721 LOG_DEBUG(
"Couldn't read FICR INFO.PART register");
755 LOG_DEBUG(
"FICR INFO likely not implemented. Invalid PART value 0x%08"
774 LOG_DEBUG(
"Invalid FICR INFO PART value 0x%08"
810 uint32_t numramblock;
813 LOG_DEBUG(
"Couldn't read FICR NUMRAMBLOCK register");
817 if (numramblock < 1 || numramblock > 4) {
818 LOG_DEBUG(
"FICR NUMRAMBLOCK strange value %" PRIx32, numramblock);
822 for (
unsigned int i = 0; i < numramblock; i++) {
823 uint32_t sizeramblock;
826 LOG_DEBUG(
"Couldn't read FICR NUMRAMBLOCK register");
829 if (sizeramblock < 1024 || sizeramblock > 65536)
830 LOG_DEBUG(
"FICR SIZERAMBLOCK strange value %" PRIx32, sizeramblock);
832 *ram_size += sizeramblock;
841 struct nrf5_bank *nbank =
bank->driver_priv;
849 switch (
bank->base) {
916 LOG_DEBUG(
"Couldn't read some of FICR INFO registers");
918 LOG_ERROR(
"Couldn't read some of FICR INFO registers");
929 LOG_ERROR(
"Couldn't read FICR CONFIGID register");
933 LOG_DEBUG(
"Couldn't read FICR CONFIGID register, using FICR INFO");
950 LOG_WARNING(
"HWID 0x%04" PRIx32
" mismatch: FICR INFO.PART %"
968 LOG_ERROR(
"Couldn't read code page size");
977 LOG_ERROR(
"Couldn't read code memory size");
981 char chip_type_str[256];
987 LOG_INFO(
"%s%s %ukB Flash, %ukB RAM",
988 device_is_unknown ?
"Unknown device: " :
"",
999 struct nrf5_bank *nbank =
bank->driver_priv;
1006 LOG_WARNING(
"Chip's reported Flash capacity does not match expected one");
1008 LOG_WARNING(
"Chip's reported Flash capacity does not match FICR INFO.FLASH");
1021 bank->num_sectors = 1;
1028 bank->sectors[0].is_protected = 0;
1078 LOG_ERROR(
"Couldn't read PPFC register");
1082 if ((ppfc & 0xFF) == 0xFF) {
1089 LOG_ERROR(
"The chip was not pre-programmed with SoftDevice stack and UICR cannot be erased separately. Please issue mass erase before trying to write to this region");
1132 uint32_t buffer_size = 8192;
1139 static const uint8_t nrf5_flash_write_code[] = {
1140 #include "../../../contrib/loaders/flash/nrf5/nrf5.inc"
1143 LOG_DEBUG(
"Writing buffer to flash address=0x%"PRIx32
" bytes=0x%"PRIx32, address, bytes);
1144 assert(bytes % 4 == 0);
1149 LOG_WARNING(
"no working area available, falling back to slow memory writes");
1151 for (; bytes > 0; bytes -= 4) {
1168 sizeof(nrf5_flash_write_code),
1169 nrf5_flash_write_code);
1176 buffer_size &= ~3UL;
1177 if (buffer_size <= 256) {
1181 LOG_WARNING(
"No large enough working area available, can't do block memory writes");
1207 write_algorithm->
address, write_algorithm->
address +
sizeof(nrf5_flash_write_code) - 2,
1233 struct nrf5_bank *nbank =
bank->driver_priv;
1237 assert(
count % 4 == 0);
1253 for (
unsigned int sector = 0; sector <
bank->num_sectors; sector++) {
1261 LOG_ERROR(
"Write refused, sector %d is protected", sector);
1279 LOG_ERROR(
"Failed to write to nrf5 flash");
1293 struct nrf5_bank *nbank =
bank->driver_priv;
1314 for (
unsigned int s = first; s <= last; s++) {
1317 &&
bank->sectors[s].is_protected == 1) {
1318 LOG_ERROR(
"Flash sector %d is protected", s);
1325 LOG_ERROR(
"Error erasing sector %d", s);
1337 struct nrf5_bank *nbank =
bank->driver_priv;
1374 struct nrf5_bank *nbank =
NULL;
1377 LOG_WARNING(
"Flash driver 'nrf51' is deprecated! Use 'nrf5' instead.");
1379 switch (
bank->base) {
1394 chip = calloc(1,
sizeof(*chip));
1401 switch (
bank->base) {
1404 nbank = &chip->
bank[0];
1409 nbank = &chip->
bank[1];
1416 nbank->probed =
false;
1417 bank->driver_priv = nbank;
1418 bank->write_start_alignment =
bank->write_end_alignment = 4;
1438 struct nrf5_bank *nbank =
bank->driver_priv;
1446 LOG_ERROR(
"Couldn't read PPFC register");
1450 if ((ppfc & 0xFF) == 0x00) {
1451 LOG_ERROR(
"Code region 0 size was pre-programmed at the factory, "
1452 "mass erase command won't work.");
1469 LOG_ERROR(
"Mass erase did not complete");
1477 LOG_INFO(
"A reset or power cycle is required if the flash was protected before.");
1486 .
name =
"mass_erase",
1487 .handler = nrf5_handle_mass_erase_command,
1489 .help =
"Erase all flash contents of the chip.",
1499 .help =
"nrf5 flash command group",
1506 .help =
"nrf51 flash command group",
1516 .flash_bank_command = nrf5_flash_bank_command,
1534 .flash_bank_command = nrf5_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 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.
void command_print_sameline(struct command_invocation *cmd, const char *format,...)
#define CMD_CTX
Use this macro to access the context of the command being handled, rather than accessing the variable...
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
#define ERROR_FLASH_OPER_UNSUPPORTED
#define ERROR_FLASH_BANK_INVALID
#define ERROR_FLASH_PROTECTED
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.
int get_flash_bank_by_addr(struct target *target, target_addr_t addr, bool check, struct flash_bank **result_bank)
Returns the flash bank located at a specified address.
struct flash_bank * flash_bank_list(void)
void alive_sleep(uint64_t ms)
#define LOG_WARNING(expr ...)
#define LOG_ERROR(expr ...)
#define LOG_INFO(expr ...)
#define LOG_DEBUG(expr ...)
static bool nrf5_info_variant_to_str(uint32_t variant, char *bf, bool swap)
static const struct nrf5_ficr_map nrf91new_ficr_offsets
static bool nrf5_bank_is_uicr(const struct nrf5_bank *nbank)
static const struct nrf5_ficr_map nrf51_52_ficr_offsets
static const char * nrf5_decode_info_package(uint32_t package)
static int nrf5_get_chip_type_str(const struct nrf5_info *chip, char *buf, unsigned int buf_size)
static int nrf5_protect(struct flash_bank *bank, int set, unsigned int first, unsigned int last)
static const struct nrf5_ficr_map nrf53_91_ficr_offsets
COMMAND_HANDLER(nrf5_handle_mass_erase_command)
#define NRF5_FICR_REG(offset)
static const struct command_registration nrf5_exec_command_handlers[]
const struct flash_driver nrf5_flash
static const struct nrf5_map nrf53net_map
static const struct nrf5_device_package nrf52_packages_table[]
static int nrf5_read_ficr_more_info(struct nrf5_info *chip)
static int nrf5_wait_for_nvmc(struct nrf5_info *chip)
static const struct nrf5_map nrf53app_91_map
static int nrf5_setup_bank(struct flash_bank *bank)
static int nrf5_auto_probe(struct flash_bank *bank)
static int nrf51_get_ram_size(struct target *target, uint32_t *ram_size)
static int nrf51_protect_check_clenr0(struct flash_bank *bank)
static const struct nrf5_map nrf51_52_map
@ NRF5_FEATURE_ERASE_BY_FLASH_WR
static bool nrf5_chip_is_probed(const struct flash_bank *bank)
static int nrf5_probe(struct flash_bank *bank)
static int nrf52_protect_check_bprot(struct flash_bank *bank)
#define WATCHDOG_REFRESH_VALUE
static const struct command_registration nrf5_command_handlers[]
static int nrf5_protect_check(struct flash_bank *bank)
static int nrf5_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
static int nrf5_probe_chip(struct flash_bank *bank)
static int nrf5_nvmc_read_only(struct nrf5_info *chip)
static int nrf51_52_partno_check(struct nrf5_info *chip)
static int nrf5_erase_page(struct flash_bank *bank, struct nrf5_info *chip, struct flash_sector *sector)
static int nrf5_nvmc_write_enable(struct nrf5_info *chip)
static struct nrf5_info * nrf5_get_chip(struct target *target)
static int nrf5_info(struct flash_bank *bank, struct command_invocation *cmd)
static int nrf5_read_ficr_info_part(struct nrf5_info *chip, const struct nrf5_map *map, const struct nrf5_ficr_map *ficr_offsets)
static int nrf5_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
#define NRF51_DEVICE_DEF(id, pt, var, bcode, fsize)
static void nrf5_free_driver_priv(struct flash_bank *bank)
static int nrf5_ll_flash_write(struct nrf5_info *chip, uint32_t address, const uint8_t *buffer, uint32_t bytes)
static int nrf53_91_partno_check(struct nrf5_info *chip)
static int nrf5_nvmc_read_u32(struct nrf5_info *chip, uint32_t reg_offset, uint32_t *value)
static int nrf5_nvmc_erase_enable(struct nrf5_info *chip)
FLASH_BANK_COMMAND_HANDLER(nrf5_flash_bank_command)
static bool nrf5_bank_is_probed(const struct flash_bank *bank)
#define NRF5_UICR_REG(offset)
static int nrf5_nvmc_write_u32(struct nrf5_info *chip, uint32_t reg_offset, uint32_t value)
static int nrf51_protect_clenr0(struct flash_bank *bank, int set, unsigned int first, unsigned int last)
static const struct nrf5_device_spec nrf5_known_devices_table[]
const struct flash_driver nrf5_flash nrf51_flash
@ NRF51_FICR_SIZERAMBLOCK3
@ NRF51_FICR_SIZERAMBLOCK0
@ NRF51_FICR_SIZERAMBLOCK2
@ NRF51_FICR_SIZERAMBLOCK1
static uint32_t bit(uint32_t value, unsigned int b)
unsigned int common_magic
When run_command is called, a new instance will be created on the stack, filled with the proper value...
Provides details of a flash bank, available either on-chip or through a major interface.
const struct flash_driver * driver
Driver for this bank.
void * driver_priv
Private driver storage pointer.
struct flash_bank * next
The next flash bank on this chip.
struct target * target
Target to which this bank belongs.
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...
Describes the geometry and status of a single flash sector within a flash bank.
int is_erased
Indication of erasure status: 0 = not erased, 1 = erased, other = unknown.
uint32_t offset
Bus offset from start of the flash chip (in bytes).
int is_protected
Indication of protection status: 0 = unprotected/unlocked, 1 = protected/locked, other = unknown.
uint32_t size
Number of bytes in this flash sector.
unsigned int flash_size_kb
enum nrf5_features features
struct nrf52_ficr_info ficr_info
uint32_t flash_num_sectors
struct nrf5_info::nrf5_bank bank[2]
const struct nrf5_ficr_map * ficr_offsets
const struct nrf5_map * map
const struct nrf5_device_spec * spec
enum nrf5_features features
uint32_t watchdog_refresh_addr
int target_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
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.
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_flash_async_algorithm(struct target *target, const uint8_t *buffer, uint32_t count, int block_size, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_params, uint32_t buffer_start, uint32_t buffer_size, uint32_t entry_point, uint32_t exit_point, void *arch_info)
Streams data to a circular buffer on target intended for consumption by code running asynchronously o...
int target_read_u32(struct target *target, target_addr_t address, uint32_t *value)
struct target * get_current_target(struct command_context *cmd_ctx)
#define ERROR_TARGET_NOT_HALTED
#define ERROR_TARGET_RESOURCE_NOT_AVAILABLE
static void h_u32_to_be(uint8_t *buf, uint32_t val)
static void h_u32_to_le(uint8_t *buf, uint32_t val)
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.