1 // SPDX-License-Identifier: GPL-2.0-or-later
3 /***************************************************************************
4 * Copyright (C) 2013 Synapse Product Development *
5 * Andrey Smirnov <andrew.smironv@gmail.com> *
6 * Angus Gratton <gus@projectgus.com> *
7 * Erdem U. Altunyurt <spamjunkeater@gmail.com> *
8 ***************************************************************************/
15 #include <helper/binarybuffer.h>
16 #include <target/algorithm.h>
17 #include <target/armv7m.h>
18 #include <helper/types.h>
19 #include <helper/time_support.h>
20 #include <helper/bits.h>
22 /* The refresh code is constant across the current spectrum of nRF5 devices */
23 #define WATCHDOG_REFRESH_VALUE 0x6e524635
26 NRF5_FLASH_BASE
= 0x00000000,
29 enum nrf5_ficr_registers
{
30 NRF51_52_FICR_BASE
= 0x10000000, /* Factory Information Configuration Registers */
32 #define NRF5_FICR_REG(offset) (NRF51_52_FICR_BASE + (offset))
34 NRF51_FICR_CLENR0
= NRF5_FICR_REG(0x028),
35 NRF51_FICR_PPFC
= NRF5_FICR_REG(0x02C),
36 NRF51_FICR_NUMRAMBLOCK
= NRF5_FICR_REG(0x034),
37 NRF51_FICR_SIZERAMBLOCK0
= NRF5_FICR_REG(0x038),
38 NRF51_FICR_SIZERAMBLOCK1
= NRF5_FICR_REG(0x03C),
39 NRF51_FICR_SIZERAMBLOCK2
= NRF5_FICR_REG(0x040),
40 NRF51_FICR_SIZERAMBLOCK3
= NRF5_FICR_REG(0x044),
43 enum nrf5_uicr_registers
{
44 NRF51_52_UICR_BASE
= 0x10001000, /* User Information
45 * Configuration Registers */
47 #define NRF5_UICR_REG(offset) (NRF51_52_UICR_BASE + (offset))
49 NRF51_UICR_CLENR0
= NRF5_UICR_REG(0x000),
52 enum nrf5_nvmc_registers
{
53 NRF5_NVMC_READY
= 0x400,
54 NRF5_NVMC_CONFIG
= 0x504,
55 NRF5_NVMC_ERASEPAGE
= 0x508,
56 NRF5_NVMC_ERASEALL
= 0x50C,
57 NRF5_NVMC_ERASEUICR
= 0x514,
59 NRF5_BPROT_BASE
= 0x40000000,
62 enum nrf5_nvmc_config_bits
{
63 NRF5_NVMC_CONFIG_REN
= 0x00,
64 NRF5_NVMC_CONFIG_WEN
= 0x01,
65 NRF5_NVMC_CONFIG_EEN
= 0x02,
69 struct nrf52_ficr_info
{
78 NRF5_FEATURE_SERIES_51
= BIT(0),
79 NRF5_FEATURE_SERIES_52
= BIT(1),
80 NRF5_FEATURE_BPROT
= BIT(2),
81 NRF5_FEATURE_ACL_PROT
= BIT(3),
82 NRF5_FEATURE_SERIES_53
= BIT(4),
83 NRF5_FEATURE_SERIES_91
= BIT(5),
84 NRF5_FEATURE_ERASE_BY_FLASH_WR
= BIT(6),
87 struct nrf5_device_spec
{
91 const char *build_code
;
92 unsigned int flash_size_kb
;
93 enum nrf5_features features
;
96 /* FICR registers offsets */
97 struct nrf5_ficr_map
{
98 uint32_t codepagesize
;
102 uint32_t info_variant
;
103 uint32_t info_package
;
115 uint32_t watchdog_refresh_addr
;
119 unsigned int refcount
;
123 struct nrf5_info
*chip
;
127 struct target
*target
;
129 /* chip identification stored in nrf5_probe_chip()
130 * for use in nrf5_info() and nrf5_setup_bank() */
131 bool ficr_info_valid
;
132 struct nrf52_ficr_info ficr_info
;
133 const struct nrf5_device_spec
*spec
;
135 enum nrf5_features features
;
136 uint32_t flash_page_size
;
137 uint32_t flash_num_sectors
;
138 unsigned int ram_size_kb
;
140 const struct nrf5_map
*map
;
141 const struct nrf5_ficr_map
*ficr_offsets
;
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, \
155 * The table maps known HWIDs to the part numbers, variant
156 * build code and some other info. For nRF51 rev 1 and 2 devices
157 * this is the only way how to get the part number and variant.
159 * All tested nRF51 rev 3 devices have FICR INFO fields
160 * but the fields are not documented in RM so we keep HWIDs in
163 * nRF52 and newer devices have FICR INFO documented, the autodetection
164 * can rely on it and HWIDs table is not used.
166 * The known devices table below is derived from the "nRF5x series
167 * compatibility matrix" documents.
169 * Up to date with Matrix v2.0, plus some additional HWIDs.
171 * The additional HWIDs apply where the build code in the matrix is
172 * shown as Gx0, Bx0, etc. In these cases the HWID in the matrix is
173 * for x==0, x!=0 means different (unspecified) HWIDs.
175 static const struct nrf5_device_spec nrf5_known_devices_table
[] = {
176 /* nRF51822 Devices (IC rev 1). */
177 NRF51_DEVICE_DEF(0x001D, "51822", "QFAA", "CA/C0", 256),
178 NRF51_DEVICE_DEF(0x0026, "51822", "QFAB", "AA", 128),
179 NRF51_DEVICE_DEF(0x0027, "51822", "QFAB", "A0", 128),
180 NRF51_DEVICE_DEF(0x0020, "51822", "CEAA", "BA", 256),
181 NRF51_DEVICE_DEF(0x002F, "51822", "CEAA", "B0", 256),
183 /* Some early nRF51-DK (PCA10028) & nRF51-Dongle (PCA10031) boards
184 with built-in jlink seem to use engineering samples not listed
185 in the nRF51 Series Compatibility Matrix V1.0. */
186 NRF51_DEVICE_DEF(0x0071, "51822", "QFAC", "AB", 256),
188 /* nRF51822 Devices (IC rev 2). */
189 NRF51_DEVICE_DEF(0x002A, "51822", "QFAA", "FA0", 256),
190 NRF51_DEVICE_DEF(0x0044, "51822", "QFAA", "GC0", 256),
191 NRF51_DEVICE_DEF(0x003C, "51822", "QFAA", "G0", 256),
192 NRF51_DEVICE_DEF(0x0057, "51822", "QFAA", "G2", 256),
193 NRF51_DEVICE_DEF(0x0058, "51822", "QFAA", "G3", 256),
194 NRF51_DEVICE_DEF(0x004C, "51822", "QFAB", "B0", 128),
195 NRF51_DEVICE_DEF(0x0040, "51822", "CEAA", "CA0", 256),
196 NRF51_DEVICE_DEF(0x0047, "51822", "CEAA", "DA0", 256),
197 NRF51_DEVICE_DEF(0x004D, "51822", "CEAA", "D00", 256),
199 /* nRF51822 Devices (IC rev 3). */
200 NRF51_DEVICE_DEF(0x0072, "51822", "QFAA", "H0", 256),
201 NRF51_DEVICE_DEF(0x00D1, "51822", "QFAA", "H2", 256),
202 NRF51_DEVICE_DEF(0x007B, "51822", "QFAB", "C0", 128),
203 NRF51_DEVICE_DEF(0x0083, "51822", "QFAC", "A0", 256),
204 NRF51_DEVICE_DEF(0x0084, "51822", "QFAC", "A1", 256),
205 NRF51_DEVICE_DEF(0x007D, "51822", "CDAB", "A0", 128),
206 NRF51_DEVICE_DEF(0x0079, "51822", "CEAA", "E0", 256),
207 NRF51_DEVICE_DEF(0x0087, "51822", "CFAC", "A0", 256),
208 NRF51_DEVICE_DEF(0x008F, "51822", "QFAA", "H1", 256),
210 /* nRF51422 Devices (IC rev 1). */
211 NRF51_DEVICE_DEF(0x001E, "51422", "QFAA", "CA", 256),
212 NRF51_DEVICE_DEF(0x0024, "51422", "QFAA", "C0", 256),
213 NRF51_DEVICE_DEF(0x0031, "51422", "CEAA", "A0A", 256),
215 /* nRF51422 Devices (IC rev 2). */
216 NRF51_DEVICE_DEF(0x002D, "51422", "QFAA", "DAA", 256),
217 NRF51_DEVICE_DEF(0x002E, "51422", "QFAA", "E0", 256),
218 NRF51_DEVICE_DEF(0x0061, "51422", "QFAB", "A00", 128),
219 NRF51_DEVICE_DEF(0x0050, "51422", "CEAA", "B0", 256),
221 /* nRF51422 Devices (IC rev 3). */
222 NRF51_DEVICE_DEF(0x0073, "51422", "QFAA", "F0", 256),
223 NRF51_DEVICE_DEF(0x007C, "51422", "QFAB", "B0", 128),
224 NRF51_DEVICE_DEF(0x0085, "51422", "QFAC", "A0", 256),
225 NRF51_DEVICE_DEF(0x0086, "51422", "QFAC", "A1", 256),
226 NRF51_DEVICE_DEF(0x007E, "51422", "CDAB", "A0", 128),
227 NRF51_DEVICE_DEF(0x007A, "51422", "CEAA", "C0", 256),
228 NRF51_DEVICE_DEF(0x0088, "51422", "CFAC", "A0", 256),
230 /* The driver fully autodetects nRF52 series devices by FICR INFO,
231 * no need for nRF52xxx HWIDs in this table */
234 struct nrf5_device_package
{
239 /* Newer devices have FICR INFO.PACKAGE.
240 * This table converts its value to two character code */
241 static const struct nrf5_device_package nrf52_packages_table
[] = {
246 { 0x2004, "QI/CA" }, /* differs nRF52805, 810, 811: CA, nRF52833, 840: QI */
253 static const struct nrf5_ficr_map nrf51_52_ficr_offsets
= {
254 .codepagesize
= 0x10,
257 /* CONFIGID is documented on nRF51 series only.
258 * On nRF52 is present but not documented */
261 /* Following registers are available on nRF52 and on nRF51 since rev 3 */
263 .info_variant
= 0x104,
264 .info_package
= 0x108,
269 static const struct nrf5_map nrf51_52_map
= {
270 .flash_base
= NRF5_FLASH_BASE
,
271 .ficr_base
= NRF51_52_FICR_BASE
,
272 .uicr_base
= NRF51_52_UICR_BASE
,
273 .nvmc_base
= 0x4001E000,
275 .watchdog_refresh_addr
= 0x40010600,
279 /* Third generation devices (nRF53, nRF91) */
281 static const struct nrf5_ficr_map nrf53_91_ficr_offsets
= {
282 .codepagesize
= 0x220,
286 .info_variant
= 0x210,
287 .info_package
= 0x214,
292 /* Since nRF9160 Product Specification v2.1 there is
293 * a new UICR field SIPINFO, which should be preferred.
294 * The original INFO fields describe just a part of the chip
295 * (PARTNO=9120 at nRF9161)
297 static const struct nrf5_ficr_map nrf91new_ficr_offsets
= {
298 .codepagesize
= 0x220,
301 .info_part
= 0x140, /* SIPINFO.PARTNO */
302 .info_variant
= 0x148, /* SIPINFO.VARIANT */
303 .info_package
= 0x214,
309 NRF53APP_91_FICR_BASE
= 0x00FF0000,
310 NRF53APP_91_UICR_BASE
= 0x00FF8000,
311 NRF53NET_FLASH_BASE
= 0x01000000,
312 NRF53NET_FICR_BASE
= 0x01FF0000,
313 NRF53NET_UICR_BASE
= 0x01FF8000,
316 static const struct nrf5_map nrf53app_91_map
= {
317 .flash_base
= NRF5_FLASH_BASE
,
318 .ficr_base
= NRF53APP_91_FICR_BASE
,
319 .uicr_base
= NRF53APP_91_UICR_BASE
,
320 .nvmc_base
= 0x50039000,
322 .watchdog_refresh_addr
= 0x50018600,
326 * SoC consists of two Cortex-M33 cores:
327 * - application core with security extensions
329 * Each core has its own RAM, flash, FICR and UICR
330 * The flash driver probes and handles flash and UICR of one core
331 * independently of those dedicated to the other core.
333 static const struct nrf5_map nrf53net_map
= {
334 .flash_base
= NRF53NET_FLASH_BASE
,
335 .ficr_base
= NRF53NET_FICR_BASE
,
336 .uicr_base
= NRF53NET_UICR_BASE
,
337 .nvmc_base
= 0x41080000,
339 .watchdog_refresh_addr
= 0x41080000,
343 const struct flash_driver nrf5_flash
, nrf51_flash
;
345 static bool nrf5_bank_is_probed(const struct flash_bank
*bank
)
347 struct nrf5_bank
*nbank
= bank
->driver_priv
;
348 return nbank
->probed
;
351 static bool nrf5_chip_is_probed(const struct flash_bank
*bank
)
353 struct nrf5_bank
*nbank
= bank
->driver_priv
;
354 struct nrf5_info
*chip
= nbank
->chip
;
355 return chip
->chip_probed
;
358 static bool nrf5_bank_is_uicr(const struct nrf5_bank
*nbank
)
360 struct nrf5_info
*chip
= nbank
->chip
;
361 return nbank
== &chip
->bank
[1];
364 static int nrf5_nvmc_read_u32(struct nrf5_info
*chip
, uint32_t reg_offset
, uint32_t *value
)
366 return target_read_u32(chip
->target
, chip
->map
->nvmc_base
+ reg_offset
, value
);
369 static int nrf5_nvmc_write_u32(struct nrf5_info
*chip
, uint32_t reg_offset
, uint32_t value
)
371 return target_write_u32(chip
->target
, chip
->map
->nvmc_base
+ reg_offset
, value
);
374 static int nrf5_wait_for_nvmc(struct nrf5_info
*chip
)
378 int timeout_ms
= 340;
379 int64_t ts_start
= timeval_ms();
382 res
= nrf5_nvmc_read_u32(chip
, NRF5_NVMC_READY
, &ready
);
383 if (res
== ERROR_WAIT
) {
384 /* The adapter does not handle SWD WAIT properly,
385 * add some delay to reduce number of error messages */
389 if (res
!= ERROR_OK
) {
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");
402 return ERROR_FLASH_BUSY
;
405 static int nrf5_nvmc_erase_enable(struct nrf5_info
*chip
)
408 res
= nrf5_nvmc_write_u32(chip
,
410 NRF5_NVMC_CONFIG_EEN
);
412 if (res
!= ERROR_OK
) {
413 LOG_ERROR("Failed to enable erase operation");
418 According to NVMC examples in Nordic SDK busy status must be
419 checked after writing to NVMC_CONFIG
421 res
= nrf5_wait_for_nvmc(chip
);
423 LOG_ERROR("Erase enable did not complete");
428 static int nrf5_nvmc_write_enable(struct nrf5_info
*chip
)
431 res
= nrf5_nvmc_write_u32(chip
,
433 NRF5_NVMC_CONFIG_WEN
);
435 if (res
!= ERROR_OK
) {
436 LOG_ERROR("Failed to enable write operation");
441 According to NVMC examples in Nordic SDK busy status must be
442 checked after writing to NVMC_CONFIG
444 res
= nrf5_wait_for_nvmc(chip
);
446 LOG_ERROR("Write enable did not complete");
451 static int nrf5_nvmc_read_only(struct nrf5_info
*chip
)
454 res
= nrf5_nvmc_write_u32(chip
,
456 NRF5_NVMC_CONFIG_REN
);
458 if (res
!= ERROR_OK
) {
459 LOG_ERROR("Failed to disable write/erase operation");
463 According to NVMC examples in Nordic SDK busy status must be
464 checked after writing to NVMC_CONFIG
466 res
= nrf5_wait_for_nvmc(chip
);
468 LOG_ERROR("Read only enable did not complete");
473 /* nRF51 series only */
474 static int nrf51_protect_check_clenr0(struct flash_bank
*bank
)
479 struct nrf5_bank
*nbank
= bank
->driver_priv
;
480 struct nrf5_info
*chip
= nbank
->chip
;
482 res
= target_read_u32(chip
->target
, NRF51_FICR_CLENR0
,
484 if (res
!= ERROR_OK
) {
485 LOG_ERROR("Couldn't read code region 0 size[FICR]");
489 if (clenr0
== 0xFFFFFFFF) {
490 res
= target_read_u32(chip
->target
, NRF51_UICR_CLENR0
,
492 if (res
!= ERROR_OK
) {
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
;
505 /* nRF52 series only */
506 static int nrf52_protect_check_bprot(struct flash_bank
*bank
)
508 struct nrf5_bank
*nbank
= bank
->driver_priv
;
509 struct nrf5_info
*chip
= nbank
->chip
;
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;
519 if (n_reg
>= ARRAY_SIZE(nrf5_bprot_offsets
))
522 res
= target_read_u32(chip
->target
, NRF5_BPROT_BASE
+ nrf5_bprot_offsets
[n_reg
], &bprot_reg
);
526 bank
->sectors
[i
].is_protected
= (bprot_reg
& (1 << bit
)) ? 1 : 0;
531 static int nrf5_protect_check(struct flash_bank
*bank
)
533 struct nrf5_bank
*nbank
= bank
->driver_priv
;
534 struct nrf5_info
*chip
= nbank
->chip
;
536 /* UICR cannot be write protected so just return early */
537 if (nrf5_bank_is_uicr(nbank
))
540 if (chip
->features
& NRF5_FEATURE_BPROT
)
541 return nrf52_protect_check_bprot(bank
);
543 if (chip
->features
& NRF5_FEATURE_SERIES_51
)
544 return nrf51_protect_check_clenr0(bank
);
546 LOG_WARNING("Flash protection of this nRF device is not supported");
547 return ERROR_FLASH_OPER_UNSUPPORTED
;
550 /* nRF51 series only */
551 static int nrf51_protect_clenr0(struct flash_bank
*bank
, int set
, unsigned int first
,
555 uint32_t clenr0
, ppfc
;
557 struct nrf5_bank
*nbank
= bank
->driver_priv
;
558 struct nrf5_info
*chip
= nbank
->chip
;
561 LOG_ERROR("Code region 0 must start at the beginning of the bank");
565 res
= target_read_u32(chip
->target
, NRF51_FICR_PPFC
,
567 if (res
!= ERROR_OK
) {
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");
577 res
= target_read_u32(chip
->target
, NRF51_UICR_CLENR0
,
579 if (res
!= ERROR_OK
) {
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");
589 res
= nrf5_nvmc_write_enable(chip
);
593 clenr0
= bank
->sectors
[last
].offset
+ bank
->sectors
[last
].size
;
594 res
= target_write_u32(chip
->target
, NRF51_UICR_CLENR0
, clenr0
);
596 int res2
= nrf5_wait_for_nvmc(chip
);
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");
607 nrf5_nvmc_read_only(chip
);
612 static int nrf5_protect(struct flash_bank
*bank
, int set
, unsigned int first
,
615 struct nrf5_bank
*nbank
= bank
->driver_priv
;
616 struct nrf5_info
*chip
= nbank
->chip
;
618 /* UICR cannot be write protected so just bail out early */
619 if (nrf5_bank_is_uicr(nbank
)) {
620 LOG_ERROR("UICR page does not support protection");
621 return ERROR_FLASH_OPER_UNSUPPORTED
;
624 if (bank
->target
->state
!= TARGET_HALTED
) {
625 LOG_ERROR("Target not halted");
626 return ERROR_TARGET_NOT_HALTED
;
629 if (chip
->features
& NRF5_FEATURE_SERIES_51
)
630 return nrf51_protect_clenr0(bank
, set
, first
, last
);
632 LOG_ERROR("Flash protection setting is not supported on this nRF5 device");
633 return ERROR_FLASH_OPER_UNSUPPORTED
;
636 static bool nrf5_info_variant_to_str(uint32_t variant
, char *bf
, bool swap
)
641 h_u32_to_le(b
, variant
);
643 h_u32_to_be(b
, variant
);
645 if (isalnum(b
[0]) && isalnum(b
[1]) && isalnum(b
[2]) &&
646 (isalnum(b
[3]) || b
[3] == 0)) {
656 static const char *nrf5_decode_info_package(uint32_t package
)
658 for (size_t i
= 0; i
< ARRAY_SIZE(nrf52_packages_table
); i
++) {
659 if (nrf52_packages_table
[i
].package
== package
)
660 return nrf52_packages_table
[i
].code
;
665 static int nrf5_get_chip_type_str(const struct nrf5_info
*chip
, char *buf
, unsigned int buf_size
)
669 res
= snprintf(buf
, buf_size
, "nRF%s-%s(build code: %s)",
670 chip
->spec
->part
, chip
->spec
->variant
, chip
->spec
->build_code
);
671 } else if (chip
->ficr_info_valid
) {
674 nrf5_info_variant_to_str(chip
->ficr_info
.variant
, variant
,
675 chip
->features
& NRF5_FEATURE_SERIES_91
);
677 if (chip
->features
& (NRF5_FEATURE_SERIES_53
| NRF5_FEATURE_SERIES_91
)) {
678 res
= snprintf(buf
, buf_size
, "nRF%" PRIx32
"-%s",
679 chip
->ficr_info
.part
, variant
);
681 res
= snprintf(buf
, buf_size
, "nRF%" PRIx32
"-%s%.2s(build code: %s)",
682 chip
->ficr_info
.part
,
683 nrf5_decode_info_package(chip
->ficr_info
.package
),
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__
);
698 static int nrf5_info(struct flash_bank
*bank
, struct command_invocation
*cmd
)
700 struct nrf5_bank
*nbank
= bank
->driver_priv
;
701 struct nrf5_info
*chip
= nbank
->chip
;
703 char chip_type_str
[256];
704 if (nrf5_get_chip_type_str(chip
, chip_type_str
, sizeof(chip_type_str
)) != ERROR_OK
)
707 unsigned int flash_size_kb
= chip
->flash_num_sectors
* chip
->flash_page_size
/ 1024;
708 command_print_sameline(cmd
, "%s %ukB Flash, %ukB RAM",
709 chip_type_str
, flash_size_kb
, chip
->ram_size_kb
);
713 static int nrf5_read_ficr_info_part(struct nrf5_info
*chip
, const struct nrf5_map
*map
,
714 const struct nrf5_ficr_map
*ficr_offsets
)
716 struct target
*target
= chip
->target
;
717 uint32_t ficr_base
= map
->ficr_base
;
719 int res
= target_read_u32(target
, ficr_base
+ ficr_offsets
->info_part
, &chip
->ficr_info
.part
);
721 LOG_DEBUG("Couldn't read FICR INFO.PART register");
726 static int nrf51_52_partno_check(struct nrf5_info
*chip
)
729 uint32_t series
= chip
->ficr_info
.part
& 0xfffff000;
732 chip
->features
= NRF5_FEATURE_SERIES_51
;
736 chip
->features
= NRF5_FEATURE_SERIES_52
;
738 switch (chip
->ficr_info
.part
) {
743 chip
->features
|= NRF5_FEATURE_BPROT
;
749 chip
->features
|= NRF5_FEATURE_ACL_PROT
;
755 LOG_DEBUG("FICR INFO likely not implemented. Invalid PART value 0x%08"
756 PRIx32
, chip
->ficr_info
.part
);
757 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
761 static int nrf53_91_partno_check(struct nrf5_info
*chip
)
763 uint32_t series
= chip
->ficr_info
.part
& 0xffffff00;
766 chip
->features
= NRF5_FEATURE_SERIES_53
| NRF5_FEATURE_ERASE_BY_FLASH_WR
;
770 chip
->features
= NRF5_FEATURE_SERIES_91
| NRF5_FEATURE_ERASE_BY_FLASH_WR
;
774 LOG_DEBUG("Invalid FICR INFO PART value 0x%08"
775 PRIx32
, chip
->ficr_info
.part
);
776 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
780 static int nrf5_read_ficr_more_info(struct nrf5_info
*chip
)
783 struct target
*target
= chip
->target
;
784 const struct nrf5_ficr_map
*ficr_offsets
= chip
->ficr_offsets
;
785 uint32_t ficr_base
= chip
->map
->ficr_base
;
787 res
= target_read_u32(target
, ficr_base
+ ficr_offsets
->info_variant
, &chip
->ficr_info
.variant
);
791 res
= target_read_u32(target
, ficr_base
+ ficr_offsets
->info_package
, &chip
->ficr_info
.package
);
795 res
= target_read_u32(target
, ficr_base
+ ficr_offsets
->info_ram
, &chip
->ficr_info
.ram
);
799 res
= target_read_u32(target
, ficr_base
+ ficr_offsets
->info_flash
, &chip
->ficr_info
.flash
);
803 /* nRF51 series only */
804 static int nrf51_get_ram_size(struct target
*target
, uint32_t *ram_size
)
810 uint32_t numramblock
;
811 res
= target_read_u32(target
, NRF51_FICR_NUMRAMBLOCK
, &numramblock
);
812 if (res
!= ERROR_OK
) {
813 LOG_DEBUG("Couldn't read FICR NUMRAMBLOCK register");
817 if (numramblock
< 1 || numramblock
> 4) {
818 LOG_DEBUG("FICR NUMRAMBLOCK strange value %" PRIx32
, numramblock
);
819 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
822 for (unsigned int i
= 0; i
< numramblock
; i
++) {
823 uint32_t sizeramblock
;
824 res
= target_read_u32(target
, NRF51_FICR_SIZERAMBLOCK0
+ sizeof(uint32_t)*i
, &sizeramblock
);
825 if (res
!= ERROR_OK
) {
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
;
837 static int nrf5_probe_chip(struct flash_bank
*bank
)
839 int res
= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
841 struct nrf5_bank
*nbank
= bank
->driver_priv
;
842 struct nrf5_info
*chip
= nbank
->chip
;
843 struct target
*target
= chip
->target
;
846 chip
->ficr_info_valid
= false;
848 /* First try to detect nRF53/91 */
849 switch (bank
->base
) {
850 case NRF5_FLASH_BASE
:
851 case NRF53APP_91_UICR_BASE
:
852 res
= nrf5_read_ficr_info_part(chip
, &nrf53app_91_map
, &nrf91new_ficr_offsets
);
853 if (res
== ERROR_OK
) {
854 res
= nrf53_91_partno_check(chip
);
855 if (res
== ERROR_OK
) {
856 chip
->map
= &nrf53app_91_map
;
857 chip
->ficr_offsets
= &nrf91new_ficr_offsets
;
862 res
= nrf5_read_ficr_info_part(chip
, &nrf53app_91_map
, &nrf53_91_ficr_offsets
);
866 res
= nrf53_91_partno_check(chip
);
870 chip
->map
= &nrf53app_91_map
;
871 chip
->ficr_offsets
= &nrf53_91_ficr_offsets
;
874 case NRF53NET_FLASH_BASE
:
875 case NRF53NET_UICR_BASE
:
876 res
= nrf5_read_ficr_info_part(chip
, &nrf53net_map
, &nrf53_91_ficr_offsets
);
880 res
= nrf53_91_partno_check(chip
);
884 chip
->map
= &nrf53net_map
;
885 chip
->ficr_offsets
= &nrf53_91_ficr_offsets
;
892 /* If nRF53/91 is not detected, try nRF51/52 */
893 if (res
!= ERROR_OK
) {
894 /* Guess a nRF51 series if the device has no FICR INFO and we don't know HWID */
895 chip
->features
= NRF5_FEATURE_SERIES_51
;
896 chip
->map
= &nrf51_52_map
;
897 chip
->ficr_offsets
= &nrf51_52_ficr_offsets
;
899 /* Don't bail out on error for the case that some old engineering
900 * sample has FICR INFO registers unreadable. We can proceed anyway. */
901 res
= nrf5_read_ficr_info_part(chip
, chip
->map
, chip
->ficr_offsets
);
903 res
= nrf51_52_partno_check(chip
);
906 if (res
== ERROR_OK
) {
907 /* Now we know the device has FICR INFO filled by something relevant:
908 * Although it is not documented, the tested nRF51 rev 3 devices
909 * have FICR INFO.PART, RAM and FLASH of the same format as nRF52.
910 * VARIANT and PACKAGE coding is unknown for a nRF51 device.
911 * nRF52 devices have FICR INFO documented and always filled. */
912 res
= nrf5_read_ficr_more_info(chip
);
913 if (res
== ERROR_OK
) {
914 chip
->ficr_info_valid
= true;
915 } else if (chip
->features
& NRF5_FEATURE_SERIES_51
) {
916 LOG_DEBUG("Couldn't read some of FICR INFO registers");
918 LOG_ERROR("Couldn't read some of FICR INFO registers");
923 const struct nrf5_ficr_map
*ficr_offsets
= chip
->ficr_offsets
;
924 uint32_t ficr_base
= chip
->map
->ficr_base
;
925 uint32_t configid
= 0;
926 res
= target_read_u32(target
, ficr_base
+ ficr_offsets
->configid
, &configid
);
927 if (res
!= ERROR_OK
) {
928 if (chip
->features
& NRF5_FEATURE_SERIES_51
) {
929 LOG_ERROR("Couldn't read FICR CONFIGID register");
933 LOG_DEBUG("Couldn't read FICR CONFIGID register, using FICR INFO");
936 /* HWID is stored in the lower two bytes of the CONFIGID register */
937 chip
->hwid
= configid
& 0xFFFF;
939 for (size_t i
= 0; i
< ARRAY_SIZE(nrf5_known_devices_table
); i
++) {
940 if (chip
->hwid
== nrf5_known_devices_table
[i
].hwid
) {
941 chip
->spec
= &nrf5_known_devices_table
[i
];
942 chip
->features
= chip
->spec
->features
;
947 if (chip
->spec
&& chip
->ficr_info_valid
) {
948 /* check if HWID table gives the same part as FICR INFO */
949 if (chip
->ficr_info
.part
!= strtoul(chip
->spec
->part
, NULL
, 16))
950 LOG_WARNING("HWID 0x%04" PRIx32
" mismatch: FICR INFO.PART %"
951 PRIx32
, chip
->hwid
, chip
->ficr_info
.part
);
954 if (chip
->ficr_info_valid
) {
955 chip
->ram_size_kb
= chip
->ficr_info
.ram
;
956 } else if (chip
->features
& NRF5_FEATURE_SERIES_51
) {
958 nrf51_get_ram_size(target
, &ram_size
);
959 chip
->ram_size_kb
= ram_size
/ 1024;
961 chip
->ram_size_kb
= 0;
964 /* The value stored in FICR CODEPAGESIZE is the number of bytes in one page of FLASH. */
965 res
= target_read_u32(chip
->target
, ficr_base
+ ficr_offsets
->codepagesize
,
966 &chip
->flash_page_size
);
967 if (res
!= ERROR_OK
) {
968 LOG_ERROR("Couldn't read code page size");
972 /* Note the register name is misleading,
973 * FICR CODESIZE is the number of pages in flash memory, not the number of bytes! */
974 res
= target_read_u32(chip
->target
, ficr_base
+ ficr_offsets
->codesize
,
975 &chip
->flash_num_sectors
);
976 if (res
!= ERROR_OK
) {
977 LOG_ERROR("Couldn't read code memory size");
981 char chip_type_str
[256];
982 if (nrf5_get_chip_type_str(chip
, chip_type_str
, sizeof(chip_type_str
)) != ERROR_OK
)
985 unsigned int flash_size_kb
= chip
->flash_num_sectors
* chip
->flash_page_size
/ 1024;
986 const bool device_is_unknown
= (!chip
->spec
&& !chip
->ficr_info_valid
);
987 LOG_INFO("%s%s %ukB Flash, %ukB RAM",
988 device_is_unknown
? "Unknown device: " : "",
993 chip
->chip_probed
= true;
997 static int nrf5_setup_bank(struct flash_bank
*bank
)
999 struct nrf5_bank
*nbank
= bank
->driver_priv
;
1000 struct nrf5_info
*chip
= nbank
->chip
;
1002 if (bank
->base
== chip
->map
->flash_base
) {
1003 unsigned int flash_size_kb
= chip
->flash_num_sectors
* chip
->flash_page_size
/ 1024;
1005 if (chip
->spec
&& flash_size_kb
!= chip
->spec
->flash_size_kb
)
1006 LOG_WARNING("Chip's reported Flash capacity does not match expected one");
1007 if (chip
->ficr_info_valid
&& flash_size_kb
!= chip
->ficr_info
.flash
)
1008 LOG_WARNING("Chip's reported Flash capacity does not match FICR INFO.FLASH");
1010 bank
->num_sectors
= chip
->flash_num_sectors
;
1011 bank
->size
= chip
->flash_num_sectors
* chip
->flash_page_size
;
1013 bank
->sectors
= alloc_block_array(0, chip
->flash_page_size
, bank
->num_sectors
);
1017 chip
->bank
[0].probed
= true;
1019 } else if (bank
->base
== chip
->map
->uicr_base
) {
1021 bank
->num_sectors
= 1;
1022 bank
->size
= chip
->flash_page_size
;
1024 bank
->sectors
= alloc_block_array(0, chip
->flash_page_size
, bank
->num_sectors
);
1028 bank
->sectors
[0].is_protected
= 0;
1030 chip
->bank
[1].probed
= true;
1032 LOG_ERROR("Invalid nRF bank address " TARGET_ADDR_FMT
, bank
->base
);
1033 return ERROR_FLASH_BANK_INVALID
;
1039 static int nrf5_probe(struct flash_bank
*bank
)
1041 /* probe always reads actual info from the device */
1042 int res
= nrf5_probe_chip(bank
);
1043 if (res
!= ERROR_OK
)
1046 return nrf5_setup_bank(bank
);
1049 static int nrf5_auto_probe(struct flash_bank
*bank
)
1051 if (nrf5_bank_is_probed(bank
))
1054 if (!nrf5_chip_is_probed(bank
)) {
1055 int res
= nrf5_probe_chip(bank
);
1056 if (res
!= ERROR_OK
)
1060 return nrf5_setup_bank(bank
);
1064 static int nrf5_erase_page(struct flash_bank
*bank
,
1065 struct nrf5_info
*chip
,
1066 struct flash_sector
*sector
)
1070 LOG_DEBUG("Erasing page at 0x%"PRIx32
, sector
->offset
);
1072 if (bank
->base
== chip
->map
->uicr_base
) {
1073 if (chip
->features
& NRF5_FEATURE_SERIES_51
) {
1075 res
= target_read_u32(chip
->target
, NRF51_FICR_PPFC
,
1077 if (res
!= ERROR_OK
) {
1078 LOG_ERROR("Couldn't read PPFC register");
1082 if ((ppfc
& 0xFF) == 0xFF) {
1083 /* We can't erase the UICR. Double-check to
1084 see if it's already erased before complaining. */
1085 default_flash_blank_check(bank
);
1086 if (sector
->is_erased
== 1)
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");
1094 res
= nrf5_nvmc_write_u32(chip
, NRF5_NVMC_ERASEUICR
, 0x00000001);
1096 } else if (chip
->features
& NRF5_FEATURE_ERASE_BY_FLASH_WR
) {
1097 res
= target_write_u32(chip
->target
, bank
->base
+ sector
->offset
, 0xffffffff);
1098 /* nRF9160 errata [2] NVMC: CPU code execution from RAM halted during
1099 * flash page erase operation
1100 * https://infocenter.nordicsemi.com/index.jsp?topic=%2Ferrata_nRF9160_Rev1%2FERR%2FnRF9160%2FRev1%2Flatest%2Fanomaly_160_2.html
1101 * affects also erasing by debugger MEM-AP write:
1103 * Write to a flash address stalls the bus for 87 ms until
1104 * page erase finishes! This makes problems if the adapter does not
1105 * handle SWD WAIT properly or does not wait long enough.
1106 * Using a target algo would not help, AP gets unresponsive too.
1107 * Neither sending AP ABORT helps, the next AP access stalls again.
1108 * Simply wait long enough before accessing AP again...
1110 * The same errata was observed in nRF9161
1112 if (chip
->features
& NRF5_FEATURE_SERIES_91
)
1116 res
= nrf5_nvmc_write_u32(chip
, NRF5_NVMC_ERASEPAGE
, sector
->offset
);
1119 if (res
!= ERROR_OK
) {
1120 /* caller logs the error */
1124 res
= nrf5_wait_for_nvmc(chip
);
1128 /* Start a low level flash write for the specified region */
1129 static int nrf5_ll_flash_write(struct nrf5_info
*chip
, uint32_t address
, const uint8_t *buffer
, uint32_t bytes
)
1131 struct target
*target
= chip
->target
;
1132 uint32_t buffer_size
= 8192;
1133 struct working_area
*write_algorithm
;
1134 struct working_area
*source
;
1135 struct reg_param reg_params
[6];
1136 struct armv7m_algorithm armv7m_info
;
1137 int retval
= ERROR_OK
;
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);
1146 /* allocate working area with flash programming code */
1147 if (target_alloc_working_area(target
, sizeof(nrf5_flash_write_code
),
1148 &write_algorithm
) != ERROR_OK
) {
1149 LOG_WARNING("no working area available, falling back to slow memory writes");
1151 for (; bytes
> 0; bytes
-= 4) {
1152 retval
= target_write_memory(target
, address
, 4, 1, buffer
);
1153 if (retval
!= ERROR_OK
)
1156 retval
= nrf5_wait_for_nvmc(chip
);
1157 if (retval
!= ERROR_OK
)
1167 retval
= target_write_buffer(target
, write_algorithm
->address
,
1168 sizeof(nrf5_flash_write_code
),
1169 nrf5_flash_write_code
);
1170 if (retval
!= ERROR_OK
)
1174 while (target_alloc_working_area(target
, buffer_size
, &source
) != ERROR_OK
) {
1176 buffer_size
&= ~3UL; /* Make sure it's 4 byte aligned */
1177 if (buffer_size
<= 256) {
1178 /* free working area, write algorithm already allocated */
1179 target_free_working_area(target
, write_algorithm
);
1181 LOG_WARNING("No large enough working area available, can't do block memory writes");
1182 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
1186 armv7m_info
.common_magic
= ARMV7M_COMMON_MAGIC
;
1187 armv7m_info
.core_mode
= ARM_MODE_THREAD
;
1189 init_reg_param(®_params
[0], "r0", 32, PARAM_IN_OUT
); /* byte count */
1190 init_reg_param(®_params
[1], "r1", 32, PARAM_OUT
); /* buffer start */
1191 init_reg_param(®_params
[2], "r2", 32, PARAM_OUT
); /* buffer end */
1192 init_reg_param(®_params
[3], "r3", 32, PARAM_IN_OUT
); /* target address */
1193 init_reg_param(®_params
[4], "r6", 32, PARAM_OUT
); /* watchdog refresh value */
1194 init_reg_param(®_params
[5], "r7", 32, PARAM_OUT
); /* watchdog refresh register address */
1196 buf_set_u32(reg_params
[0].value
, 0, 32, bytes
);
1197 buf_set_u32(reg_params
[1].value
, 0, 32, source
->address
);
1198 buf_set_u32(reg_params
[2].value
, 0, 32, source
->address
+ source
->size
);
1199 buf_set_u32(reg_params
[3].value
, 0, 32, address
);
1200 buf_set_u32(reg_params
[4].value
, 0, 32, WATCHDOG_REFRESH_VALUE
);
1201 buf_set_u32(reg_params
[5].value
, 0, 32, chip
->map
->watchdog_refresh_addr
);
1203 retval
= target_run_flash_async_algorithm(target
, buffer
, bytes
/4, 4,
1205 ARRAY_SIZE(reg_params
), reg_params
,
1206 source
->address
, source
->size
,
1207 write_algorithm
->address
, write_algorithm
->address
+ sizeof(nrf5_flash_write_code
) - 2,
1210 target_free_working_area(target
, source
);
1211 target_free_working_area(target
, write_algorithm
);
1213 destroy_reg_param(®_params
[0]);
1214 destroy_reg_param(®_params
[1]);
1215 destroy_reg_param(®_params
[2]);
1216 destroy_reg_param(®_params
[3]);
1217 destroy_reg_param(®_params
[4]);
1218 destroy_reg_param(®_params
[5]);
1223 static int nrf5_write(struct flash_bank
*bank
, const uint8_t *buffer
,
1224 uint32_t offset
, uint32_t count
)
1228 if (bank
->target
->state
!= TARGET_HALTED
) {
1229 LOG_ERROR("Target not halted");
1230 return ERROR_TARGET_NOT_HALTED
;
1233 struct nrf5_bank
*nbank
= bank
->driver_priv
;
1234 struct nrf5_info
*chip
= nbank
->chip
;
1236 assert(offset
% 4 == 0);
1237 assert(count
% 4 == 0);
1239 /* UICR CLENR0 based protection used on nRF51 is somewhat clumsy:
1240 * RM reads: Code running from code region 1 will not be able to write
1242 * Unfortunately the flash loader running from RAM can write to both
1243 * code regions without any hint the protection is violated.
1245 * Update protection state and check if any flash sector to be written
1247 if (chip
->features
& NRF5_FEATURE_SERIES_51
) {
1249 res
= nrf51_protect_check_clenr0(bank
);
1250 if (res
!= ERROR_OK
)
1253 for (unsigned int sector
= 0; sector
< bank
->num_sectors
; sector
++) {
1254 struct flash_sector
*bs
= &bank
->sectors
[sector
];
1256 /* Start offset in or before this sector? */
1257 /* End offset in or behind this sector? */
1258 if ((offset
< (bs
->offset
+ bs
->size
))
1259 && ((offset
+ count
- 1) >= bs
->offset
)
1260 && bs
->is_protected
== 1) {
1261 LOG_ERROR("Write refused, sector %d is protected", sector
);
1262 return ERROR_FLASH_PROTECTED
;
1267 res
= nrf5_nvmc_write_enable(chip
);
1268 if (res
!= ERROR_OK
)
1271 res
= nrf5_ll_flash_write(chip
, bank
->base
+ offset
, buffer
, count
);
1272 if (res
!= ERROR_OK
)
1275 return nrf5_nvmc_read_only(chip
);
1278 nrf5_nvmc_read_only(chip
);
1279 LOG_ERROR("Failed to write to nrf5 flash");
1283 static int nrf5_erase(struct flash_bank
*bank
, unsigned int first
,
1288 if (bank
->target
->state
!= TARGET_HALTED
) {
1289 LOG_ERROR("Target not halted");
1290 return ERROR_TARGET_NOT_HALTED
;
1293 struct nrf5_bank
*nbank
= bank
->driver_priv
;
1294 struct nrf5_info
*chip
= nbank
->chip
;
1296 /* UICR CLENR0 based protection used on nRF51 prevents erase
1297 * absolutely silently. NVMC has no flag to indicate the protection
1300 * Update protection state and check if any flash sector to be erased
1302 if (chip
->features
& NRF5_FEATURE_SERIES_51
) {
1304 res
= nrf51_protect_check_clenr0(bank
);
1305 if (res
!= ERROR_OK
)
1309 res
= nrf5_nvmc_erase_enable(chip
);
1310 if (res
!= ERROR_OK
)
1313 /* For each sector to be erased */
1314 for (unsigned int s
= first
; s
<= last
; s
++) {
1316 if (chip
->features
& NRF5_FEATURE_SERIES_51
1317 && bank
->sectors
[s
].is_protected
== 1) {
1318 LOG_ERROR("Flash sector %d is protected", s
);
1319 res
= ERROR_FLASH_PROTECTED
;
1323 res
= nrf5_erase_page(bank
, chip
, &bank
->sectors
[s
]);
1324 if (res
!= ERROR_OK
) {
1325 LOG_ERROR("Error erasing sector %d", s
);
1331 nrf5_nvmc_read_only(chip
);
1335 static void nrf5_free_driver_priv(struct flash_bank
*bank
)
1337 struct nrf5_bank
*nbank
= bank
->driver_priv
;
1338 struct nrf5_info
*chip
= nbank
->chip
;
1343 if (chip
->refcount
== 0) {
1345 bank
->driver_priv
= NULL
;
1349 static struct nrf5_info
*nrf5_get_chip(struct target
*target
)
1351 struct flash_bank
*bank_iter
;
1353 /* iterate over nrf5 banks of same target */
1354 for (bank_iter
= flash_bank_list(); bank_iter
; bank_iter
= bank_iter
->next
) {
1355 if (bank_iter
->driver
!= &nrf5_flash
&& bank_iter
->driver
!= &nrf51_flash
)
1358 if (bank_iter
->target
!= target
)
1361 struct nrf5_bank
*nbank
= bank_iter
->driver_priv
;
1371 FLASH_BANK_COMMAND_HANDLER(nrf5_flash_bank_command
)
1373 struct nrf5_info
*chip
;
1374 struct nrf5_bank
*nbank
= NULL
;
1376 if (bank
->driver
== &nrf51_flash
)
1377 LOG_WARNING("Flash driver 'nrf51' is deprecated! Use 'nrf5' instead.");
1379 switch (bank
->base
) {
1380 case NRF5_FLASH_BASE
:
1381 case NRF53NET_FLASH_BASE
:
1382 case NRF51_52_UICR_BASE
:
1383 case NRF53APP_91_UICR_BASE
:
1384 case NRF53NET_UICR_BASE
:
1387 LOG_ERROR("Invalid nRF bank address " TARGET_ADDR_FMT
, bank
->base
);
1388 return ERROR_FLASH_BANK_INVALID
;
1391 chip
= nrf5_get_chip(bank
->target
);
1393 /* Create a new chip */
1394 chip
= calloc(1, sizeof(*chip
));
1398 chip
->target
= bank
->target
;
1401 switch (bank
->base
) {
1402 case NRF5_FLASH_BASE
:
1403 case NRF53NET_FLASH_BASE
:
1404 nbank
= &chip
->bank
[0];
1406 case NRF51_52_UICR_BASE
:
1407 case NRF53APP_91_UICR_BASE
:
1408 case NRF53NET_UICR_BASE
:
1409 nbank
= &chip
->bank
[1];
1416 nbank
->probed
= false;
1417 bank
->driver_priv
= nbank
;
1418 bank
->write_start_alignment
= bank
->write_end_alignment
= 4;
1423 COMMAND_HANDLER(nrf5_handle_mass_erase_command
)
1426 struct flash_bank
*bank
= NULL
;
1427 struct target
*target
= get_current_target(CMD_CTX
);
1429 res
= get_flash_bank_by_addr(target
, NRF5_FLASH_BASE
, true, &bank
);
1430 if (res
!= ERROR_OK
)
1433 if (target
->state
!= TARGET_HALTED
) {
1434 LOG_ERROR("Target not halted");
1435 return ERROR_TARGET_NOT_HALTED
;
1438 struct nrf5_bank
*nbank
= bank
->driver_priv
;
1439 struct nrf5_info
*chip
= nbank
->chip
;
1441 if (chip
->features
& NRF5_FEATURE_SERIES_51
) {
1443 res
= target_read_u32(target
, NRF51_FICR_PPFC
,
1445 if (res
!= ERROR_OK
) {
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.");
1457 res
= nrf5_nvmc_erase_enable(chip
);
1458 if (res
!= ERROR_OK
)
1461 res
= nrf5_nvmc_write_u32(chip
, NRF5_NVMC_ERASEALL
, 0x00000001);
1462 if (res
!= ERROR_OK
) {
1463 LOG_ERROR("Mass erase failed");
1467 res
= nrf5_wait_for_nvmc(chip
);
1468 if (res
!= ERROR_OK
)
1469 LOG_ERROR("Mass erase did not complete");
1472 nrf5_nvmc_read_only(chip
);
1474 if (res
== ERROR_OK
) {
1475 LOG_INFO("Mass erase completed.");
1476 if (chip
->features
& NRF5_FEATURE_SERIES_51
)
1477 LOG_INFO("A reset or power cycle is required if the flash was protected before.");
1484 static const struct command_registration nrf5_exec_command_handlers
[] = {
1486 .name
= "mass_erase",
1487 .handler
= nrf5_handle_mass_erase_command
,
1488 .mode
= COMMAND_EXEC
,
1489 .help
= "Erase all flash contents of the chip.",
1492 COMMAND_REGISTRATION_DONE
1495 static const struct command_registration nrf5_command_handlers
[] = {
1498 .mode
= COMMAND_ANY
,
1499 .help
= "nrf5 flash command group",
1501 .chain
= nrf5_exec_command_handlers
,
1505 .mode
= COMMAND_ANY
,
1506 .help
= "nrf51 flash command group",
1508 .chain
= nrf5_exec_command_handlers
,
1510 COMMAND_REGISTRATION_DONE
1513 const struct flash_driver nrf5_flash
= {
1515 .commands
= nrf5_command_handlers
,
1516 .flash_bank_command
= nrf5_flash_bank_command
,
1518 .erase
= nrf5_erase
,
1519 .protect
= nrf5_protect
,
1520 .write
= nrf5_write
,
1521 .read
= default_flash_read
,
1522 .probe
= nrf5_probe
,
1523 .auto_probe
= nrf5_auto_probe
,
1524 .erase_check
= default_flash_blank_check
,
1525 .protect_check
= nrf5_protect_check
,
1526 .free_driver_priv
= nrf5_free_driver_priv
,
1529 /* We need to retain the flash-driver name as well as the commands
1530 * for backwards compatibility */
1531 const struct flash_driver nrf51_flash
= {
1533 .commands
= nrf5_command_handlers
,
1534 .flash_bank_command
= nrf5_flash_bank_command
,
1536 .erase
= nrf5_erase
,
1537 .protect
= nrf5_protect
,
1538 .write
= nrf5_write
,
1539 .read
= default_flash_read
,
1540 .probe
= nrf5_probe
,
1541 .auto_probe
= nrf5_auto_probe
,
1542 .erase_check
= default_flash_blank_check
,
1543 .protect_check
= nrf5_protect_check
,
1544 .free_driver_priv
= nrf5_free_driver_priv
,