flash/nor/nrf5: drop nrf5_get_probed_chip_if_halted()
[openocd.git] / src / flash / nor / nrf5.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2
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 ***************************************************************************/
9
10 #ifdef HAVE_CONFIG_H
11 #include "config.h"
12 #endif
13
14 #include "imp.h"
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
21 /* Both those values are constant across the current spectrum ofr nRF5 devices */
22 #define WATCHDOG_REFRESH_REGISTER 0x40010600
23 #define WATCHDOG_REFRESH_VALUE 0x6e524635
24
25 enum {
26 NRF5_FLASH_BASE = 0x00000000,
27 };
28
29 enum nrf5_ficr_registers {
30 NRF5_FICR_BASE = 0x10000000, /* Factory Information Configuration Registers */
31
32 #define NRF5_FICR_REG(offset) (NRF5_FICR_BASE + offset)
33
34 NRF5_FICR_CODEPAGESIZE = NRF5_FICR_REG(0x010),
35 NRF5_FICR_CODESIZE = NRF5_FICR_REG(0x014),
36
37 NRF51_FICR_CLENR0 = NRF5_FICR_REG(0x028),
38 NRF51_FICR_PPFC = NRF5_FICR_REG(0x02C),
39 NRF51_FICR_NUMRAMBLOCK = NRF5_FICR_REG(0x034),
40 NRF51_FICR_SIZERAMBLOCK0 = NRF5_FICR_REG(0x038),
41 NRF51_FICR_SIZERAMBLOCK1 = NRF5_FICR_REG(0x03C),
42 NRF51_FICR_SIZERAMBLOCK2 = NRF5_FICR_REG(0x040),
43 NRF51_FICR_SIZERAMBLOCK3 = NRF5_FICR_REG(0x044),
44
45 /* CONFIGID is documented on nRF51 series only.
46 * On nRF52 is present but not documented */
47 NRF5_FICR_CONFIGID = NRF5_FICR_REG(0x05C),
48
49 NRF5_FICR_DEVICEID0 = NRF5_FICR_REG(0x060),
50 NRF5_FICR_DEVICEID1 = NRF5_FICR_REG(0x064),
51 NRF5_FICR_ER0 = NRF5_FICR_REG(0x080),
52 NRF5_FICR_ER1 = NRF5_FICR_REG(0x084),
53 NRF5_FICR_ER2 = NRF5_FICR_REG(0x088),
54 NRF5_FICR_ER3 = NRF5_FICR_REG(0x08C),
55 NRF5_FICR_IR0 = NRF5_FICR_REG(0x090),
56 NRF5_FICR_IR1 = NRF5_FICR_REG(0x094),
57 NRF5_FICR_IR2 = NRF5_FICR_REG(0x098),
58 NRF5_FICR_IR3 = NRF5_FICR_REG(0x09C),
59 NRF5_FICR_DEVICEADDRTYPE = NRF5_FICR_REG(0x0A0),
60 NRF5_FICR_DEVICEADDR0 = NRF5_FICR_REG(0x0A4),
61 NRF5_FICR_DEVICEADDR1 = NRF5_FICR_REG(0x0A8),
62
63 NRF51_FICR_OVERRIDEN = NRF5_FICR_REG(0x0AC),
64 NRF51_FICR_NRF_1MBIT0 = NRF5_FICR_REG(0x0B0),
65 NRF51_FICR_NRF_1MBIT1 = NRF5_FICR_REG(0x0B4),
66 NRF51_FICR_NRF_1MBIT2 = NRF5_FICR_REG(0x0B8),
67 NRF51_FICR_NRF_1MBIT3 = NRF5_FICR_REG(0x0BC),
68 NRF51_FICR_NRF_1MBIT4 = NRF5_FICR_REG(0x0C0),
69 NRF51_FICR_BLE_1MBIT0 = NRF5_FICR_REG(0x0EC),
70 NRF51_FICR_BLE_1MBIT1 = NRF5_FICR_REG(0x0F0),
71 NRF51_FICR_BLE_1MBIT2 = NRF5_FICR_REG(0x0F4),
72 NRF51_FICR_BLE_1MBIT3 = NRF5_FICR_REG(0x0F8),
73 NRF51_FICR_BLE_1MBIT4 = NRF5_FICR_REG(0x0FC),
74
75 /* Following registers are available on nRF52 and on nRF51 since rev 3 */
76 NRF5_FICR_INFO_PART = NRF5_FICR_REG(0x100),
77 NRF5_FICR_INFO_VARIANT = NRF5_FICR_REG(0x104),
78 NRF5_FICR_INFO_PACKAGE = NRF5_FICR_REG(0x108),
79 NRF5_FICR_INFO_RAM = NRF5_FICR_REG(0x10C),
80 NRF5_FICR_INFO_FLASH = NRF5_FICR_REG(0x110),
81 };
82
83 enum nrf5_uicr_registers {
84 NRF5_UICR_BASE = 0x10001000, /* User Information
85 * Configuration Registers */
86
87 #define NRF5_UICR_REG(offset) (NRF5_UICR_BASE + offset)
88
89 NRF51_UICR_CLENR0 = NRF5_UICR_REG(0x000),
90 NRF51_UICR_RBPCONF = NRF5_UICR_REG(0x004),
91 NRF51_UICR_XTALFREQ = NRF5_UICR_REG(0x008),
92 NRF51_UICR_FWID = NRF5_UICR_REG(0x010),
93 };
94
95 enum nrf5_nvmc_registers {
96 NRF5_NVMC_BASE = 0x4001E000, /* Non-Volatile Memory
97 * Controller Registers */
98
99 #define NRF5_NVMC_REG(offset) (NRF5_NVMC_BASE + offset)
100
101 NRF5_NVMC_READY = NRF5_NVMC_REG(0x400),
102 NRF5_NVMC_CONFIG = NRF5_NVMC_REG(0x504),
103 NRF5_NVMC_ERASEPAGE = NRF5_NVMC_REG(0x508),
104 NRF5_NVMC_ERASEALL = NRF5_NVMC_REG(0x50C),
105 NRF5_NVMC_ERASEUICR = NRF5_NVMC_REG(0x514),
106
107 NRF5_BPROT_BASE = 0x40000000,
108 };
109
110 enum nrf5_nvmc_config_bits {
111 NRF5_NVMC_CONFIG_REN = 0x00,
112 NRF5_NVMC_CONFIG_WEN = 0x01,
113 NRF5_NVMC_CONFIG_EEN = 0x02,
114
115 };
116
117 struct nrf52_ficr_info {
118 uint32_t part;
119 uint32_t variant;
120 uint32_t package;
121 uint32_t ram;
122 uint32_t flash;
123 };
124
125 enum nrf5_features {
126 NRF5_FEATURE_SERIES_51 = 1 << 0,
127 NRF5_FEATURE_SERIES_52 = 1 << 1,
128 NRF5_FEATURE_BPROT = 1 << 2,
129 NRF5_FEATURE_ACL_PROT = 1 << 3,
130 };
131
132 struct nrf5_device_spec {
133 uint16_t hwid;
134 const char *part;
135 const char *variant;
136 const char *build_code;
137 unsigned int flash_size_kb;
138 enum nrf5_features features;
139 };
140
141 struct nrf5_info {
142 unsigned int refcount;
143
144 struct nrf5_bank {
145 struct nrf5_info *chip;
146 bool probed;
147 } bank[2];
148 struct target *target;
149
150 /* chip identification stored in nrf5_probe() for use in nrf5_info() */
151 bool ficr_info_valid;
152 struct nrf52_ficr_info ficr_info;
153 const struct nrf5_device_spec *spec;
154 uint16_t hwid;
155 enum nrf5_features features;
156 unsigned int flash_size_kb;
157 unsigned int ram_size_kb;
158 };
159
160 #define NRF51_DEVICE_DEF(id, pt, var, bcode, fsize) \
161 { \
162 .hwid = (id), \
163 .part = pt, \
164 .variant = var, \
165 .build_code = bcode, \
166 .flash_size_kb = (fsize), \
167 .features = NRF5_FEATURE_SERIES_51, \
168 }
169
170 /*
171 * The table maps known HWIDs to the part numbers, variant
172 * build code and some other info. For nRF51 rev 1 and 2 devices
173 * this is the only way how to get the part number and variant.
174 *
175 * All tested nRF51 rev 3 devices have FICR INFO fields
176 * but the fields are not documented in RM so we keep HWIDs in
177 * this table.
178 *
179 * nRF52 and newer devices have FICR INFO documented, the autodetection
180 * can rely on it and HWIDs table is not used.
181 *
182 * The known devices table below is derived from the "nRF5x series
183 * compatibility matrix" documents.
184 *
185 * Up to date with Matrix v2.0, plus some additional HWIDs.
186 *
187 * The additional HWIDs apply where the build code in the matrix is
188 * shown as Gx0, Bx0, etc. In these cases the HWID in the matrix is
189 * for x==0, x!=0 means different (unspecified) HWIDs.
190 */
191 static const struct nrf5_device_spec nrf5_known_devices_table[] = {
192 /* nRF51822 Devices (IC rev 1). */
193 NRF51_DEVICE_DEF(0x001D, "51822", "QFAA", "CA/C0", 256),
194 NRF51_DEVICE_DEF(0x0026, "51822", "QFAB", "AA", 128),
195 NRF51_DEVICE_DEF(0x0027, "51822", "QFAB", "A0", 128),
196 NRF51_DEVICE_DEF(0x0020, "51822", "CEAA", "BA", 256),
197 NRF51_DEVICE_DEF(0x002F, "51822", "CEAA", "B0", 256),
198
199 /* Some early nRF51-DK (PCA10028) & nRF51-Dongle (PCA10031) boards
200 with built-in jlink seem to use engineering samples not listed
201 in the nRF51 Series Compatibility Matrix V1.0. */
202 NRF51_DEVICE_DEF(0x0071, "51822", "QFAC", "AB", 256),
203
204 /* nRF51822 Devices (IC rev 2). */
205 NRF51_DEVICE_DEF(0x002A, "51822", "QFAA", "FA0", 256),
206 NRF51_DEVICE_DEF(0x0044, "51822", "QFAA", "GC0", 256),
207 NRF51_DEVICE_DEF(0x003C, "51822", "QFAA", "G0", 256),
208 NRF51_DEVICE_DEF(0x0057, "51822", "QFAA", "G2", 256),
209 NRF51_DEVICE_DEF(0x0058, "51822", "QFAA", "G3", 256),
210 NRF51_DEVICE_DEF(0x004C, "51822", "QFAB", "B0", 128),
211 NRF51_DEVICE_DEF(0x0040, "51822", "CEAA", "CA0", 256),
212 NRF51_DEVICE_DEF(0x0047, "51822", "CEAA", "DA0", 256),
213 NRF51_DEVICE_DEF(0x004D, "51822", "CEAA", "D00", 256),
214
215 /* nRF51822 Devices (IC rev 3). */
216 NRF51_DEVICE_DEF(0x0072, "51822", "QFAA", "H0", 256),
217 NRF51_DEVICE_DEF(0x00D1, "51822", "QFAA", "H2", 256),
218 NRF51_DEVICE_DEF(0x007B, "51822", "QFAB", "C0", 128),
219 NRF51_DEVICE_DEF(0x0083, "51822", "QFAC", "A0", 256),
220 NRF51_DEVICE_DEF(0x0084, "51822", "QFAC", "A1", 256),
221 NRF51_DEVICE_DEF(0x007D, "51822", "CDAB", "A0", 128),
222 NRF51_DEVICE_DEF(0x0079, "51822", "CEAA", "E0", 256),
223 NRF51_DEVICE_DEF(0x0087, "51822", "CFAC", "A0", 256),
224 NRF51_DEVICE_DEF(0x008F, "51822", "QFAA", "H1", 256),
225
226 /* nRF51422 Devices (IC rev 1). */
227 NRF51_DEVICE_DEF(0x001E, "51422", "QFAA", "CA", 256),
228 NRF51_DEVICE_DEF(0x0024, "51422", "QFAA", "C0", 256),
229 NRF51_DEVICE_DEF(0x0031, "51422", "CEAA", "A0A", 256),
230
231 /* nRF51422 Devices (IC rev 2). */
232 NRF51_DEVICE_DEF(0x002D, "51422", "QFAA", "DAA", 256),
233 NRF51_DEVICE_DEF(0x002E, "51422", "QFAA", "E0", 256),
234 NRF51_DEVICE_DEF(0x0061, "51422", "QFAB", "A00", 128),
235 NRF51_DEVICE_DEF(0x0050, "51422", "CEAA", "B0", 256),
236
237 /* nRF51422 Devices (IC rev 3). */
238 NRF51_DEVICE_DEF(0x0073, "51422", "QFAA", "F0", 256),
239 NRF51_DEVICE_DEF(0x007C, "51422", "QFAB", "B0", 128),
240 NRF51_DEVICE_DEF(0x0085, "51422", "QFAC", "A0", 256),
241 NRF51_DEVICE_DEF(0x0086, "51422", "QFAC", "A1", 256),
242 NRF51_DEVICE_DEF(0x007E, "51422", "CDAB", "A0", 128),
243 NRF51_DEVICE_DEF(0x007A, "51422", "CEAA", "C0", 256),
244 NRF51_DEVICE_DEF(0x0088, "51422", "CFAC", "A0", 256),
245
246 /* The driver fully autodetects nRF52 series devices by FICR INFO,
247 * no need for nRF52xxx HWIDs in this table */
248 };
249
250 struct nrf5_device_package {
251 uint32_t package;
252 const char *code;
253 };
254
255 /* Newer devices have FICR INFO.PACKAGE.
256 * This table converts its value to two character code */
257 static const struct nrf5_device_package nrf52_packages_table[] = {
258 { 0x2000, "QF" },
259 { 0x2001, "CH" },
260 { 0x2002, "CI" },
261 { 0x2003, "QC" },
262 { 0x2004, "QI/CA" }, /* differs nRF52805, 810, 811: CA, nRF52833, 840: QI */
263 { 0x2005, "CK" },
264 { 0x2007, "QD" },
265 { 0x2008, "CJ" },
266 { 0x2009, "CF" },
267 };
268
269 const struct flash_driver nrf5_flash, nrf51_flash;
270
271 static bool nrf5_bank_is_probed(const struct flash_bank *bank)
272 {
273 struct nrf5_bank *nbank = bank->driver_priv;
274 assert(nbank);
275
276 return nbank->probed;
277 }
278
279 static int nrf5_wait_for_nvmc(struct nrf5_info *chip)
280 {
281 uint32_t ready;
282 int res;
283 int timeout_ms = 340;
284 int64_t ts_start = timeval_ms();
285
286 do {
287 res = target_read_u32(chip->target, NRF5_NVMC_READY, &ready);
288 if (res != ERROR_OK) {
289 LOG_ERROR("Error waiting NVMC_READY: generic flash write/erase error (check protection etc...)");
290 return res;
291 }
292
293 if (ready == 0x00000001)
294 return ERROR_OK;
295
296 keep_alive();
297
298 } while ((timeval_ms()-ts_start) < timeout_ms);
299
300 LOG_DEBUG("Timed out waiting for NVMC_READY");
301 return ERROR_FLASH_BUSY;
302 }
303
304 static int nrf5_nvmc_erase_enable(struct nrf5_info *chip)
305 {
306 int res;
307 res = target_write_u32(chip->target,
308 NRF5_NVMC_CONFIG,
309 NRF5_NVMC_CONFIG_EEN);
310
311 if (res != ERROR_OK) {
312 LOG_ERROR("Failed to enable erase operation");
313 return res;
314 }
315
316 /*
317 According to NVMC examples in Nordic SDK busy status must be
318 checked after writing to NVMC_CONFIG
319 */
320 res = nrf5_wait_for_nvmc(chip);
321 if (res != ERROR_OK)
322 LOG_ERROR("Erase enable did not complete");
323
324 return res;
325 }
326
327 static int nrf5_nvmc_write_enable(struct nrf5_info *chip)
328 {
329 int res;
330 res = target_write_u32(chip->target,
331 NRF5_NVMC_CONFIG,
332 NRF5_NVMC_CONFIG_WEN);
333
334 if (res != ERROR_OK) {
335 LOG_ERROR("Failed to enable write operation");
336 return res;
337 }
338
339 /*
340 According to NVMC examples in Nordic SDK busy status must be
341 checked after writing to NVMC_CONFIG
342 */
343 res = nrf5_wait_for_nvmc(chip);
344 if (res != ERROR_OK)
345 LOG_ERROR("Write enable did not complete");
346
347 return res;
348 }
349
350 static int nrf5_nvmc_read_only(struct nrf5_info *chip)
351 {
352 int res;
353 res = target_write_u32(chip->target,
354 NRF5_NVMC_CONFIG,
355 NRF5_NVMC_CONFIG_REN);
356
357 if (res != ERROR_OK) {
358 LOG_ERROR("Failed to enable read-only operation");
359 return res;
360 }
361 /*
362 According to NVMC examples in Nordic SDK busy status must be
363 checked after writing to NVMC_CONFIG
364 */
365 res = nrf5_wait_for_nvmc(chip);
366 if (res != ERROR_OK)
367 LOG_ERROR("Read only enable did not complete");
368
369 return res;
370 }
371
372 static int nrf5_nvmc_generic_erase(struct nrf5_info *chip,
373 uint32_t erase_register, uint32_t erase_value)
374 {
375 int res;
376
377 res = nrf5_nvmc_erase_enable(chip);
378 if (res != ERROR_OK)
379 goto error;
380
381 res = target_write_u32(chip->target,
382 erase_register,
383 erase_value);
384 if (res != ERROR_OK)
385 goto set_read_only;
386
387 res = nrf5_wait_for_nvmc(chip);
388 if (res != ERROR_OK)
389 goto set_read_only;
390
391 return nrf5_nvmc_read_only(chip);
392
393 set_read_only:
394 nrf5_nvmc_read_only(chip);
395 error:
396 LOG_ERROR("Failed to erase reg: 0x%08"PRIx32" val: 0x%08"PRIx32,
397 erase_register, erase_value);
398 return ERROR_FAIL;
399 }
400
401 static int nrf5_protect_check_clenr0(struct flash_bank *bank)
402 {
403 int res;
404 uint32_t clenr0;
405
406 struct nrf5_bank *nbank = bank->driver_priv;
407 assert(nbank);
408 struct nrf5_info *chip = nbank->chip;
409 assert(chip);
410
411 res = target_read_u32(chip->target, NRF51_FICR_CLENR0,
412 &clenr0);
413 if (res != ERROR_OK) {
414 LOG_ERROR("Couldn't read code region 0 size[FICR]");
415 return res;
416 }
417
418 if (clenr0 == 0xFFFFFFFF) {
419 res = target_read_u32(chip->target, NRF51_UICR_CLENR0,
420 &clenr0);
421 if (res != ERROR_OK) {
422 LOG_ERROR("Couldn't read code region 0 size[UICR]");
423 return res;
424 }
425 }
426
427 for (unsigned int i = 0; i < bank->num_sectors; i++)
428 bank->sectors[i].is_protected =
429 clenr0 != 0xFFFFFFFF && bank->sectors[i].offset < clenr0;
430
431 return ERROR_OK;
432 }
433
434 static int nrf5_protect_check_bprot(struct flash_bank *bank)
435 {
436 struct nrf5_bank *nbank = bank->driver_priv;
437 assert(nbank);
438 struct nrf5_info *chip = nbank->chip;
439 assert(chip);
440
441 static uint32_t nrf5_bprot_offsets[4] = { 0x600, 0x604, 0x610, 0x614 };
442 uint32_t bprot_reg = 0;
443 int res;
444
445 for (unsigned int i = 0; i < bank->num_sectors; i++) {
446 unsigned int bit = i % 32;
447 if (bit == 0) {
448 unsigned int n_reg = i / 32;
449 if (n_reg >= ARRAY_SIZE(nrf5_bprot_offsets))
450 break;
451
452 res = target_read_u32(chip->target, NRF5_BPROT_BASE + nrf5_bprot_offsets[n_reg], &bprot_reg);
453 if (res != ERROR_OK)
454 return res;
455 }
456 bank->sectors[i].is_protected = (bprot_reg & (1 << bit)) ? 1 : 0;
457 }
458 return ERROR_OK;
459 }
460
461 static int nrf5_protect_check(struct flash_bank *bank)
462 {
463 /* UICR cannot be write protected so just return early */
464 if (bank->base == NRF5_UICR_BASE)
465 return ERROR_OK;
466
467 struct nrf5_bank *nbank = bank->driver_priv;
468 assert(nbank);
469 struct nrf5_info *chip = nbank->chip;
470 assert(chip);
471
472 if (chip->features & NRF5_FEATURE_BPROT)
473 return nrf5_protect_check_bprot(bank);
474
475 if (chip->features & NRF5_FEATURE_SERIES_51)
476 return nrf5_protect_check_clenr0(bank);
477
478 LOG_WARNING("Flash protection of this nRF device is not supported");
479 return ERROR_FLASH_OPER_UNSUPPORTED;
480 }
481
482 static int nrf5_protect_clenr0(struct flash_bank *bank, int set, unsigned int first,
483 unsigned int last)
484 {
485 int res;
486 uint32_t clenr0, ppfc;
487
488 struct nrf5_bank *nbank = bank->driver_priv;
489 assert(nbank);
490 struct nrf5_info *chip = nbank->chip;
491 assert(chip);
492
493 if (first != 0) {
494 LOG_ERROR("Code region 0 must start at the beginning of the bank");
495 return ERROR_FAIL;
496 }
497
498 res = target_read_u32(chip->target, NRF51_FICR_PPFC,
499 &ppfc);
500 if (res != ERROR_OK) {
501 LOG_ERROR("Couldn't read PPFC register");
502 return res;
503 }
504
505 if ((ppfc & 0xFF) == 0x00) {
506 LOG_ERROR("Code region 0 size was pre-programmed at the factory, can't change flash protection settings");
507 return ERROR_FAIL;
508 }
509
510 res = target_read_u32(chip->target, NRF51_UICR_CLENR0,
511 &clenr0);
512 if (res != ERROR_OK) {
513 LOG_ERROR("Couldn't read code region 0 size from UICR");
514 return res;
515 }
516
517 if (!set || clenr0 != 0xFFFFFFFF) {
518 LOG_ERROR("You need to perform chip erase before changing the protection settings");
519 return ERROR_FAIL;
520 }
521
522 res = nrf5_nvmc_write_enable(chip);
523 if (res != ERROR_OK)
524 goto error;
525
526 clenr0 = bank->sectors[last].offset + bank->sectors[last].size;
527 res = target_write_u32(chip->target, NRF51_UICR_CLENR0, clenr0);
528
529 int res2 = nrf5_wait_for_nvmc(chip);
530
531 if (res == ERROR_OK)
532 res = res2;
533
534 if (res == ERROR_OK)
535 LOG_INFO("A reset or power cycle is required for the new protection settings to take effect.");
536 else
537 LOG_ERROR("Couldn't write code region 0 size to UICR");
538
539 error:
540 nrf5_nvmc_read_only(chip);
541
542 return res;
543 }
544
545 static int nrf5_protect(struct flash_bank *bank, int set, unsigned int first,
546 unsigned int last)
547 {
548 /* UICR cannot be write protected so just bail out early */
549 if (bank->base == NRF5_UICR_BASE) {
550 LOG_ERROR("UICR page does not support protection");
551 return ERROR_FLASH_OPER_UNSUPPORTED;
552 }
553
554 if (bank->target->state != TARGET_HALTED) {
555 LOG_ERROR("Target not halted");
556 return ERROR_TARGET_NOT_HALTED;
557 }
558
559 struct nrf5_bank *nbank = bank->driver_priv;
560 assert(nbank);
561 struct nrf5_info *chip = nbank->chip;
562 assert(chip);
563
564 if (chip->features & NRF5_FEATURE_SERIES_51)
565 return nrf5_protect_clenr0(bank, set, first, last);
566
567 LOG_ERROR("Flash protection setting is not supported on this nRF5 device");
568 return ERROR_FLASH_OPER_UNSUPPORTED;
569 }
570
571 static bool nrf5_info_variant_to_str(uint32_t variant, char *bf)
572 {
573 uint8_t b[4];
574
575 h_u32_to_be(b, variant);
576 if (isalnum(b[0]) && isalnum(b[1]) && isalnum(b[2]) && isalnum(b[3])) {
577 memcpy(bf, b, 4);
578 bf[4] = 0;
579 return true;
580 }
581
582 strcpy(bf, "xxxx");
583 return false;
584 }
585
586 static const char *nrf5_decode_info_package(uint32_t package)
587 {
588 for (size_t i = 0; i < ARRAY_SIZE(nrf52_packages_table); i++) {
589 if (nrf52_packages_table[i].package == package)
590 return nrf52_packages_table[i].code;
591 }
592 return "xx";
593 }
594
595 static int get_nrf5_chip_type_str(const struct nrf5_info *chip, char *buf, unsigned int buf_size)
596 {
597 int res;
598 if (chip->spec) {
599 res = snprintf(buf, buf_size, "nRF%s-%s(build code: %s)",
600 chip->spec->part, chip->spec->variant, chip->spec->build_code);
601 } else if (chip->ficr_info_valid) {
602 char variant[5];
603 nrf5_info_variant_to_str(chip->ficr_info.variant, variant);
604 res = snprintf(buf, buf_size, "nRF%" PRIx32 "-%s%.2s(build code: %s)",
605 chip->ficr_info.part,
606 nrf5_decode_info_package(chip->ficr_info.package),
607 variant, &variant[2]);
608 } else {
609 res = snprintf(buf, buf_size, "nRF51xxx (HWID 0x%04" PRIx16 ")", chip->hwid);
610 }
611
612 /* safety: */
613 if (res <= 0 || (unsigned int)res >= buf_size) {
614 LOG_ERROR("BUG: buffer problem in %s", __func__);
615 return ERROR_FAIL;
616 }
617 return ERROR_OK;
618 }
619
620 static int nrf5_info(struct flash_bank *bank, struct command_invocation *cmd)
621 {
622 struct nrf5_bank *nbank = bank->driver_priv;
623 assert(nbank);
624 struct nrf5_info *chip = nbank->chip;
625 assert(chip);
626
627 char chip_type_str[256];
628 if (get_nrf5_chip_type_str(chip, chip_type_str, sizeof(chip_type_str)) != ERROR_OK)
629 return ERROR_FAIL;
630
631 command_print_sameline(cmd, "%s %ukB Flash, %ukB RAM",
632 chip_type_str, chip->flash_size_kb, chip->ram_size_kb);
633 return ERROR_OK;
634 }
635
636 static int nrf5_read_ficr_info(struct nrf5_info *chip)
637 {
638 int res;
639 struct target *target = chip->target;
640
641 chip->ficr_info_valid = false;
642
643 res = target_read_u32(target, NRF5_FICR_INFO_PART, &chip->ficr_info.part);
644 if (res != ERROR_OK) {
645 LOG_DEBUG("Couldn't read FICR INFO.PART register");
646 return res;
647 }
648
649 uint32_t series = chip->ficr_info.part & 0xfffff000;
650 switch (series) {
651 case 0x51000:
652 chip->features = NRF5_FEATURE_SERIES_51;
653 break;
654
655 case 0x52000:
656 chip->features = NRF5_FEATURE_SERIES_52;
657
658 switch (chip->ficr_info.part) {
659 case 0x52805:
660 case 0x52810:
661 case 0x52811:
662 case 0x52832:
663 chip->features |= NRF5_FEATURE_BPROT;
664 break;
665
666 case 0x52820:
667 case 0x52833:
668 case 0x52840:
669 chip->features |= NRF5_FEATURE_ACL_PROT;
670 break;
671 }
672 break;
673
674 default:
675 LOG_DEBUG("FICR INFO likely not implemented. Invalid PART value 0x%08"
676 PRIx32, chip->ficr_info.part);
677 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
678 }
679
680 /* Now we know the device has FICR INFO filled by something relevant:
681 * Although it is not documented, the tested nRF51 rev 3 devices
682 * have FICR INFO.PART, RAM and FLASH of the same format as nRF52.
683 * VARIANT and PACKAGE coding is unknown for a nRF51 device.
684 * nRF52 devices have FICR INFO documented and always filled. */
685
686 res = target_read_u32(target, NRF5_FICR_INFO_VARIANT, &chip->ficr_info.variant);
687 if (res != ERROR_OK)
688 return res;
689
690 res = target_read_u32(target, NRF5_FICR_INFO_PACKAGE, &chip->ficr_info.package);
691 if (res != ERROR_OK)
692 return res;
693
694 res = target_read_u32(target, NRF5_FICR_INFO_RAM, &chip->ficr_info.ram);
695 if (res != ERROR_OK)
696 return res;
697
698 res = target_read_u32(target, NRF5_FICR_INFO_FLASH, &chip->ficr_info.flash);
699 if (res != ERROR_OK)
700 return res;
701
702 chip->ficr_info_valid = true;
703 return ERROR_OK;
704 }
705
706 static int nrf5_get_ram_size(struct target *target, uint32_t *ram_size)
707 {
708 int res;
709
710 *ram_size = 0;
711
712 uint32_t numramblock;
713 res = target_read_u32(target, NRF51_FICR_NUMRAMBLOCK, &numramblock);
714 if (res != ERROR_OK) {
715 LOG_DEBUG("Couldn't read FICR NUMRAMBLOCK register");
716 return res;
717 }
718
719 if (numramblock < 1 || numramblock > 4) {
720 LOG_DEBUG("FICR NUMRAMBLOCK strange value %" PRIx32, numramblock);
721 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
722 }
723
724 for (unsigned int i = 0; i < numramblock; i++) {
725 uint32_t sizeramblock;
726 res = target_read_u32(target, NRF51_FICR_SIZERAMBLOCK0 + sizeof(uint32_t)*i, &sizeramblock);
727 if (res != ERROR_OK) {
728 LOG_DEBUG("Couldn't read FICR NUMRAMBLOCK register");
729 return res;
730 }
731 if (sizeramblock < 1024 || sizeramblock > 65536)
732 LOG_DEBUG("FICR SIZERAMBLOCK strange value %" PRIx32, sizeramblock);
733 else
734 *ram_size += sizeramblock;
735 }
736 return res;
737 }
738
739 static int nrf5_probe(struct flash_bank *bank)
740 {
741 int res;
742
743 struct nrf5_bank *nbank = bank->driver_priv;
744 assert(nbank);
745 struct nrf5_info *chip = nbank->chip;
746 assert(chip);
747 struct target *target = chip->target;
748
749 uint32_t configid;
750 res = target_read_u32(target, NRF5_FICR_CONFIGID, &configid);
751 if (res != ERROR_OK) {
752 LOG_ERROR("Couldn't read CONFIGID register");
753 return res;
754 }
755
756 /* HWID is stored in the lower two bytes of the CONFIGID register */
757 chip->hwid = configid & 0xFFFF;
758
759 /* guess a nRF51 series if the device has no FICR INFO and we don't know HWID */
760 chip->features = NRF5_FEATURE_SERIES_51;
761
762 /* Don't bail out on error for the case that some old engineering
763 * sample has FICR INFO registers unreadable. We can proceed anyway. */
764 (void)nrf5_read_ficr_info(chip);
765
766 chip->spec = NULL;
767 for (size_t i = 0; i < ARRAY_SIZE(nrf5_known_devices_table); i++) {
768 if (chip->hwid == nrf5_known_devices_table[i].hwid) {
769 chip->spec = &nrf5_known_devices_table[i];
770 chip->features = chip->spec->features;
771 break;
772 }
773 }
774
775 if (chip->spec && chip->ficr_info_valid) {
776 /* check if HWID table gives the same part as FICR INFO */
777 if (chip->ficr_info.part != strtoul(chip->spec->part, NULL, 16))
778 LOG_WARNING("HWID 0x%04" PRIx32 " mismatch: FICR INFO.PART %"
779 PRIx32, chip->hwid, chip->ficr_info.part);
780 }
781
782 if (chip->ficr_info_valid) {
783 chip->ram_size_kb = chip->ficr_info.ram;
784 } else {
785 uint32_t ram_size;
786 nrf5_get_ram_size(target, &ram_size);
787 chip->ram_size_kb = ram_size / 1024;
788 }
789
790 /* The value stored in NRF5_FICR_CODEPAGESIZE is the number of bytes in one page of FLASH. */
791 uint32_t flash_page_size;
792 res = target_read_u32(chip->target, NRF5_FICR_CODEPAGESIZE,
793 &flash_page_size);
794 if (res != ERROR_OK) {
795 LOG_ERROR("Couldn't read code page size");
796 return res;
797 }
798
799 /* Note the register name is misleading,
800 * NRF5_FICR_CODESIZE is the number of pages in flash memory, not the number of bytes! */
801 uint32_t num_sectors;
802 res = target_read_u32(chip->target, NRF5_FICR_CODESIZE, &num_sectors);
803 if (res != ERROR_OK) {
804 LOG_ERROR("Couldn't read code memory size");
805 return res;
806 }
807
808 chip->flash_size_kb = num_sectors * flash_page_size / 1024;
809
810 if (!chip->bank[0].probed && !chip->bank[1].probed) {
811 char chip_type_str[256];
812 if (get_nrf5_chip_type_str(chip, chip_type_str, sizeof(chip_type_str)) != ERROR_OK)
813 return ERROR_FAIL;
814 const bool device_is_unknown = (!chip->spec && !chip->ficr_info_valid);
815 LOG_INFO("%s%s %ukB Flash, %ukB RAM",
816 device_is_unknown ? "Unknown device: " : "",
817 chip_type_str,
818 chip->flash_size_kb,
819 chip->ram_size_kb);
820 }
821
822 free(bank->sectors);
823
824 if (bank->base == NRF5_FLASH_BASE) {
825 /* Sanity check */
826 if (chip->spec && chip->flash_size_kb != chip->spec->flash_size_kb)
827 LOG_WARNING("Chip's reported Flash capacity does not match expected one");
828 if (chip->ficr_info_valid && chip->flash_size_kb != chip->ficr_info.flash)
829 LOG_WARNING("Chip's reported Flash capacity does not match FICR INFO.FLASH");
830
831 bank->num_sectors = num_sectors;
832 bank->size = num_sectors * flash_page_size;
833
834 bank->sectors = alloc_block_array(0, flash_page_size, num_sectors);
835 if (!bank->sectors)
836 return ERROR_FAIL;
837
838 chip->bank[0].probed = true;
839
840 } else {
841 bank->num_sectors = 1;
842 bank->size = flash_page_size;
843
844 bank->sectors = alloc_block_array(0, flash_page_size, num_sectors);
845 if (!bank->sectors)
846 return ERROR_FAIL;
847
848 bank->sectors[0].is_protected = 0;
849
850 chip->bank[1].probed = true;
851 }
852
853 return ERROR_OK;
854 }
855
856 static int nrf5_auto_probe(struct flash_bank *bank)
857 {
858 if (nrf5_bank_is_probed(bank))
859 return ERROR_OK;
860
861 return nrf5_probe(bank);
862 }
863
864 static int nrf5_erase_all(struct nrf5_info *chip)
865 {
866 LOG_DEBUG("Erasing all non-volatile memory");
867 return nrf5_nvmc_generic_erase(chip,
868 NRF5_NVMC_ERASEALL,
869 0x00000001);
870 }
871
872 static int nrf5_erase_page(struct flash_bank *bank,
873 struct nrf5_info *chip,
874 struct flash_sector *sector)
875 {
876 int res;
877
878 LOG_DEBUG("Erasing page at 0x%"PRIx32, sector->offset);
879
880 if (bank->base == NRF5_UICR_BASE) {
881 if (chip->features & NRF5_FEATURE_SERIES_51) {
882 uint32_t ppfc;
883 res = target_read_u32(chip->target, NRF51_FICR_PPFC,
884 &ppfc);
885 if (res != ERROR_OK) {
886 LOG_ERROR("Couldn't read PPFC register");
887 return res;
888 }
889
890 if ((ppfc & 0xFF) == 0xFF) {
891 /* We can't erase the UICR. Double-check to
892 see if it's already erased before complaining. */
893 default_flash_blank_check(bank);
894 if (sector->is_erased == 1)
895 return ERROR_OK;
896
897 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");
898 return ERROR_FAIL;
899 }
900 }
901
902 res = nrf5_nvmc_generic_erase(chip,
903 NRF5_NVMC_ERASEUICR,
904 0x00000001);
905
906
907 } else {
908 res = nrf5_nvmc_generic_erase(chip,
909 NRF5_NVMC_ERASEPAGE,
910 sector->offset);
911 }
912
913 return res;
914 }
915
916 /* Start a low level flash write for the specified region */
917 static int nrf5_ll_flash_write(struct nrf5_info *chip, uint32_t address, const uint8_t *buffer, uint32_t bytes)
918 {
919 struct target *target = chip->target;
920 uint32_t buffer_size = 8192;
921 struct working_area *write_algorithm;
922 struct working_area *source;
923 struct reg_param reg_params[6];
924 struct armv7m_algorithm armv7m_info;
925 int retval = ERROR_OK;
926
927 static const uint8_t nrf5_flash_write_code[] = {
928 #include "../../../contrib/loaders/flash/nrf5/nrf5.inc"
929 };
930
931 LOG_DEBUG("Writing buffer to flash address=0x%"PRIx32" bytes=0x%"PRIx32, address, bytes);
932 assert(bytes % 4 == 0);
933
934 /* allocate working area with flash programming code */
935 if (target_alloc_working_area(target, sizeof(nrf5_flash_write_code),
936 &write_algorithm) != ERROR_OK) {
937 LOG_WARNING("no working area available, falling back to slow memory writes");
938
939 for (; bytes > 0; bytes -= 4) {
940 retval = target_write_memory(target, address, 4, 1, buffer);
941 if (retval != ERROR_OK)
942 return retval;
943
944 retval = nrf5_wait_for_nvmc(chip);
945 if (retval != ERROR_OK)
946 return retval;
947
948 address += 4;
949 buffer += 4;
950 }
951
952 return ERROR_OK;
953 }
954
955 retval = target_write_buffer(target, write_algorithm->address,
956 sizeof(nrf5_flash_write_code),
957 nrf5_flash_write_code);
958 if (retval != ERROR_OK)
959 return retval;
960
961 /* memory buffer */
962 while (target_alloc_working_area(target, buffer_size, &source) != ERROR_OK) {
963 buffer_size /= 2;
964 buffer_size &= ~3UL; /* Make sure it's 4 byte aligned */
965 if (buffer_size <= 256) {
966 /* free working area, write algorithm already allocated */
967 target_free_working_area(target, write_algorithm);
968
969 LOG_WARNING("No large enough working area available, can't do block memory writes");
970 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
971 }
972 }
973
974 armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
975 armv7m_info.core_mode = ARM_MODE_THREAD;
976
977 init_reg_param(&reg_params[0], "r0", 32, PARAM_IN_OUT); /* byte count */
978 init_reg_param(&reg_params[1], "r1", 32, PARAM_OUT); /* buffer start */
979 init_reg_param(&reg_params[2], "r2", 32, PARAM_OUT); /* buffer end */
980 init_reg_param(&reg_params[3], "r3", 32, PARAM_IN_OUT); /* target address */
981 init_reg_param(&reg_params[4], "r6", 32, PARAM_OUT); /* watchdog refresh value */
982 init_reg_param(&reg_params[5], "r7", 32, PARAM_OUT); /* watchdog refresh register address */
983
984 buf_set_u32(reg_params[0].value, 0, 32, bytes);
985 buf_set_u32(reg_params[1].value, 0, 32, source->address);
986 buf_set_u32(reg_params[2].value, 0, 32, source->address + source->size);
987 buf_set_u32(reg_params[3].value, 0, 32, address);
988 buf_set_u32(reg_params[4].value, 0, 32, WATCHDOG_REFRESH_VALUE);
989 buf_set_u32(reg_params[5].value, 0, 32, WATCHDOG_REFRESH_REGISTER);
990
991 retval = target_run_flash_async_algorithm(target, buffer, bytes/4, 4,
992 0, NULL,
993 ARRAY_SIZE(reg_params), reg_params,
994 source->address, source->size,
995 write_algorithm->address, write_algorithm->address + sizeof(nrf5_flash_write_code) - 2,
996 &armv7m_info);
997
998 target_free_working_area(target, source);
999 target_free_working_area(target, write_algorithm);
1000
1001 destroy_reg_param(&reg_params[0]);
1002 destroy_reg_param(&reg_params[1]);
1003 destroy_reg_param(&reg_params[2]);
1004 destroy_reg_param(&reg_params[3]);
1005 destroy_reg_param(&reg_params[4]);
1006 destroy_reg_param(&reg_params[5]);
1007
1008 return retval;
1009 }
1010
1011 static int nrf5_write(struct flash_bank *bank, const uint8_t *buffer,
1012 uint32_t offset, uint32_t count)
1013 {
1014 int res;
1015
1016 if (bank->target->state != TARGET_HALTED) {
1017 LOG_ERROR("Target not halted");
1018 return ERROR_TARGET_NOT_HALTED;
1019 }
1020
1021 struct nrf5_bank *nbank = bank->driver_priv;
1022 assert(nbank);
1023 struct nrf5_info *chip = nbank->chip;
1024 assert(chip);
1025
1026 assert(offset % 4 == 0);
1027 assert(count % 4 == 0);
1028
1029 /* UICR CLENR0 based protection used on nRF51 is somewhat clumsy:
1030 * RM reads: Code running from code region 1 will not be able to write
1031 * to code region 0.
1032 * Unfortunately the flash loader running from RAM can write to both
1033 * code regions without any hint the protection is violated.
1034 *
1035 * Update protection state and check if any flash sector to be written
1036 * is protected. */
1037 if (chip->features & NRF5_FEATURE_SERIES_51) {
1038
1039 res = nrf5_protect_check_clenr0(bank);
1040 if (res != ERROR_OK)
1041 return res;
1042
1043 for (unsigned int sector = 0; sector < bank->num_sectors; sector++) {
1044 struct flash_sector *bs = &bank->sectors[sector];
1045
1046 /* Start offset in or before this sector? */
1047 /* End offset in or behind this sector? */
1048 if ((offset < (bs->offset + bs->size))
1049 && ((offset + count - 1) >= bs->offset)
1050 && bs->is_protected == 1) {
1051 LOG_ERROR("Write refused, sector %d is protected", sector);
1052 return ERROR_FLASH_PROTECTED;
1053 }
1054 }
1055 }
1056
1057 res = nrf5_nvmc_write_enable(chip);
1058 if (res != ERROR_OK)
1059 goto error;
1060
1061 res = nrf5_ll_flash_write(chip, bank->base + offset, buffer, count);
1062 if (res != ERROR_OK)
1063 goto error;
1064
1065 return nrf5_nvmc_read_only(chip);
1066
1067 error:
1068 nrf5_nvmc_read_only(chip);
1069 LOG_ERROR("Failed to write to nrf5 flash");
1070 return res;
1071 }
1072
1073 static int nrf5_erase(struct flash_bank *bank, unsigned int first,
1074 unsigned int last)
1075 {
1076 int res;
1077
1078 if (bank->target->state != TARGET_HALTED) {
1079 LOG_ERROR("Target not halted");
1080 return ERROR_TARGET_NOT_HALTED;
1081 }
1082
1083 struct nrf5_bank *nbank = bank->driver_priv;
1084 assert(nbank);
1085 struct nrf5_info *chip = nbank->chip;
1086 assert(chip);
1087
1088 /* UICR CLENR0 based protection used on nRF51 prevents erase
1089 * absolutely silently. NVMC has no flag to indicate the protection
1090 * was violated.
1091 *
1092 * Update protection state and check if any flash sector to be erased
1093 * is protected. */
1094 if (chip->features & NRF5_FEATURE_SERIES_51) {
1095
1096 res = nrf5_protect_check_clenr0(bank);
1097 if (res != ERROR_OK)
1098 return res;
1099 }
1100
1101 /* For each sector to be erased */
1102 for (unsigned int s = first; s <= last && res == ERROR_OK; s++) {
1103
1104 if (chip->features & NRF5_FEATURE_SERIES_51
1105 && bank->sectors[s].is_protected == 1) {
1106 LOG_ERROR("Flash sector %d is protected", s);
1107 return ERROR_FLASH_PROTECTED;
1108 }
1109
1110 res = nrf5_erase_page(bank, chip, &bank->sectors[s]);
1111 if (res != ERROR_OK) {
1112 LOG_ERROR("Error erasing sector %d", s);
1113 return res;
1114 }
1115 }
1116
1117 return ERROR_OK;
1118 }
1119
1120 static void nrf5_free_driver_priv(struct flash_bank *bank)
1121 {
1122 struct nrf5_bank *nbank = bank->driver_priv;
1123 assert(nbank);
1124 struct nrf5_info *chip = nbank->chip;
1125 if (!chip)
1126 return;
1127
1128 chip->refcount--;
1129 if (chip->refcount == 0) {
1130 free(chip);
1131 bank->driver_priv = NULL;
1132 }
1133 }
1134
1135 static struct nrf5_info *nrf5_get_chip(struct target *target)
1136 {
1137 struct flash_bank *bank_iter;
1138
1139 /* iterate over nrf5 banks of same target */
1140 for (bank_iter = flash_bank_list(); bank_iter; bank_iter = bank_iter->next) {
1141 if (bank_iter->driver != &nrf5_flash && bank_iter->driver != &nrf51_flash)
1142 continue;
1143
1144 if (bank_iter->target != target)
1145 continue;
1146
1147 struct nrf5_bank *nbank = bank_iter->driver_priv;
1148 if (!nbank)
1149 continue;
1150
1151 if (nbank->chip)
1152 return nbank->chip;
1153 }
1154 return NULL;
1155 }
1156
1157 FLASH_BANK_COMMAND_HANDLER(nrf5_flash_bank_command)
1158 {
1159 struct nrf5_info *chip;
1160 struct nrf5_bank *nbank = NULL;
1161
1162 if (bank->driver == &nrf51_flash)
1163 LOG_WARNING("Flash driver 'nrf51' is deprecated! Use 'nrf5' instead.");
1164
1165 switch (bank->base) {
1166 case NRF5_FLASH_BASE:
1167 case NRF5_UICR_BASE:
1168 break;
1169 default:
1170 LOG_ERROR("Invalid bank address " TARGET_ADDR_FMT, bank->base);
1171 return ERROR_FAIL;
1172 }
1173
1174 chip = nrf5_get_chip(bank->target);
1175 if (!chip) {
1176 /* Create a new chip */
1177 chip = calloc(1, sizeof(*chip));
1178 if (!chip)
1179 return ERROR_FAIL;
1180
1181 chip->target = bank->target;
1182 }
1183
1184 switch (bank->base) {
1185 case NRF5_FLASH_BASE:
1186 nbank = &chip->bank[0];
1187 break;
1188 case NRF5_UICR_BASE:
1189 nbank = &chip->bank[1];
1190 break;
1191 }
1192 assert(nbank);
1193
1194 chip->refcount++;
1195 nbank->chip = chip;
1196 nbank->probed = false;
1197 bank->driver_priv = nbank;
1198 bank->write_start_alignment = bank->write_end_alignment = 4;
1199
1200 return ERROR_OK;
1201 }
1202
1203 COMMAND_HANDLER(nrf5_handle_mass_erase_command)
1204 {
1205 int res;
1206 struct flash_bank *bank = NULL;
1207 struct target *target = get_current_target(CMD_CTX);
1208
1209 res = get_flash_bank_by_addr(target, NRF5_FLASH_BASE, true, &bank);
1210 if (res != ERROR_OK)
1211 return res;
1212
1213 assert(bank);
1214
1215 if (target->state != TARGET_HALTED) {
1216 LOG_ERROR("Target not halted");
1217 return ERROR_TARGET_NOT_HALTED;
1218 }
1219
1220 struct nrf5_bank *nbank = bank->driver_priv;
1221 assert(nbank);
1222 struct nrf5_info *chip = nbank->chip;
1223 assert(chip);
1224
1225 if (chip->features & NRF5_FEATURE_SERIES_51) {
1226 uint32_t ppfc;
1227 res = target_read_u32(target, NRF51_FICR_PPFC,
1228 &ppfc);
1229 if (res != ERROR_OK) {
1230 LOG_ERROR("Couldn't read PPFC register");
1231 return res;
1232 }
1233
1234 if ((ppfc & 0xFF) == 0x00) {
1235 LOG_ERROR("Code region 0 size was pre-programmed at the factory, "
1236 "mass erase command won't work.");
1237 return ERROR_FAIL;
1238 }
1239 }
1240
1241 res = nrf5_erase_all(chip);
1242 if (res == ERROR_OK) {
1243 LOG_INFO("Mass erase completed.");
1244 if (chip->features & NRF5_FEATURE_SERIES_51)
1245 LOG_INFO("A reset or power cycle is required if the flash was protected before.");
1246
1247 } else {
1248 LOG_ERROR("Failed to erase the chip");
1249 }
1250
1251 return res;
1252 }
1253
1254 COMMAND_HANDLER(nrf5_handle_info_command)
1255 {
1256 int res;
1257 struct flash_bank *bank = NULL;
1258 struct target *target = get_current_target(CMD_CTX);
1259
1260 res = get_flash_bank_by_addr(target, NRF5_FLASH_BASE, true, &bank);
1261 if (res != ERROR_OK)
1262 return res;
1263
1264 assert(bank);
1265
1266 struct nrf5_bank *nbank = bank->driver_priv;
1267 assert(nbank);
1268 struct nrf5_info *chip = nbank->chip;
1269 assert(chip);
1270
1271 static struct {
1272 const uint32_t address;
1273 uint32_t value;
1274 } ficr[] = {
1275 { .address = NRF5_FICR_CODEPAGESIZE },
1276 { .address = NRF5_FICR_CODESIZE },
1277 { .address = NRF51_FICR_CLENR0 },
1278 { .address = NRF51_FICR_PPFC },
1279 { .address = NRF51_FICR_NUMRAMBLOCK },
1280 { .address = NRF51_FICR_SIZERAMBLOCK0 },
1281 { .address = NRF51_FICR_SIZERAMBLOCK1 },
1282 { .address = NRF51_FICR_SIZERAMBLOCK2 },
1283 { .address = NRF51_FICR_SIZERAMBLOCK3 },
1284 { .address = NRF5_FICR_CONFIGID },
1285 { .address = NRF5_FICR_DEVICEID0 },
1286 { .address = NRF5_FICR_DEVICEID1 },
1287 { .address = NRF5_FICR_ER0 },
1288 { .address = NRF5_FICR_ER1 },
1289 { .address = NRF5_FICR_ER2 },
1290 { .address = NRF5_FICR_ER3 },
1291 { .address = NRF5_FICR_IR0 },
1292 { .address = NRF5_FICR_IR1 },
1293 { .address = NRF5_FICR_IR2 },
1294 { .address = NRF5_FICR_IR3 },
1295 { .address = NRF5_FICR_DEVICEADDRTYPE },
1296 { .address = NRF5_FICR_DEVICEADDR0 },
1297 { .address = NRF5_FICR_DEVICEADDR1 },
1298 { .address = NRF51_FICR_OVERRIDEN },
1299 { .address = NRF51_FICR_NRF_1MBIT0 },
1300 { .address = NRF51_FICR_NRF_1MBIT1 },
1301 { .address = NRF51_FICR_NRF_1MBIT2 },
1302 { .address = NRF51_FICR_NRF_1MBIT3 },
1303 { .address = NRF51_FICR_NRF_1MBIT4 },
1304 { .address = NRF51_FICR_BLE_1MBIT0 },
1305 { .address = NRF51_FICR_BLE_1MBIT1 },
1306 { .address = NRF51_FICR_BLE_1MBIT2 },
1307 { .address = NRF51_FICR_BLE_1MBIT3 },
1308 { .address = NRF51_FICR_BLE_1MBIT4 },
1309 }, uicr[] = {
1310 { .address = NRF51_UICR_CLENR0, },
1311 { .address = NRF51_UICR_RBPCONF },
1312 { .address = NRF51_UICR_XTALFREQ },
1313 { .address = NRF51_UICR_FWID },
1314 };
1315
1316 for (size_t i = 0; i < ARRAY_SIZE(ficr); i++) {
1317 res = target_read_u32(chip->target, ficr[i].address,
1318 &ficr[i].value);
1319 if (res != ERROR_OK) {
1320 LOG_ERROR("Couldn't read %" PRIx32, ficr[i].address);
1321 return res;
1322 }
1323 }
1324
1325 for (size_t i = 0; i < ARRAY_SIZE(uicr); i++) {
1326 res = target_read_u32(chip->target, uicr[i].address,
1327 &uicr[i].value);
1328 if (res != ERROR_OK) {
1329 LOG_ERROR("Couldn't read %" PRIx32, uicr[i].address);
1330 return res;
1331 }
1332 }
1333
1334 command_print(CMD,
1335 "\n[factory information control block]\n\n"
1336 "code page size: %"PRIu32"B\n"
1337 "code memory size: %"PRIu32"kB\n"
1338 "code region 0 size: %"PRIu32"kB\n"
1339 "pre-programmed code: %s\n"
1340 "number of ram blocks: %"PRIu32"\n"
1341 "ram block 0 size: %"PRIu32"B\n"
1342 "ram block 1 size: %"PRIu32"B\n"
1343 "ram block 2 size: %"PRIu32"B\n"
1344 "ram block 3 size: %"PRIu32 "B\n"
1345 "config id: %" PRIx32 "\n"
1346 "device id: 0x%"PRIx32"%08"PRIx32"\n"
1347 "encryption root: 0x%08"PRIx32"%08"PRIx32"%08"PRIx32"%08"PRIx32"\n"
1348 "identity root: 0x%08"PRIx32"%08"PRIx32"%08"PRIx32"%08"PRIx32"\n"
1349 "device address type: 0x%"PRIx32"\n"
1350 "device address: 0x%"PRIx32"%08"PRIx32"\n"
1351 "override enable: %"PRIx32"\n"
1352 "NRF_1MBIT values: %"PRIx32" %"PRIx32" %"PRIx32" %"PRIx32" %"PRIx32"\n"
1353 "BLE_1MBIT values: %"PRIx32" %"PRIx32" %"PRIx32" %"PRIx32" %"PRIx32"\n"
1354 "\n[user information control block]\n\n"
1355 "code region 0 size: %"PRIu32"kB\n"
1356 "read back protection configuration: %"PRIx32"\n"
1357 "reset value for XTALFREQ: %"PRIx32"\n"
1358 "firmware id: 0x%04"PRIx32,
1359 ficr[0].value,
1360 (ficr[1].value * ficr[0].value) / 1024,
1361 (ficr[2].value == 0xFFFFFFFF) ? 0 : ficr[2].value / 1024,
1362 ((ficr[3].value & 0xFF) == 0x00) ? "present" : "not present",
1363 ficr[4].value,
1364 ficr[5].value,
1365 (ficr[6].value == 0xFFFFFFFF) ? 0 : ficr[6].value,
1366 (ficr[7].value == 0xFFFFFFFF) ? 0 : ficr[7].value,
1367 (ficr[8].value == 0xFFFFFFFF) ? 0 : ficr[8].value,
1368 ficr[9].value,
1369 ficr[10].value, ficr[11].value,
1370 ficr[12].value, ficr[13].value, ficr[14].value, ficr[15].value,
1371 ficr[16].value, ficr[17].value, ficr[18].value, ficr[19].value,
1372 ficr[20].value,
1373 ficr[21].value, ficr[22].value,
1374 ficr[23].value,
1375 ficr[24].value, ficr[25].value, ficr[26].value, ficr[27].value, ficr[28].value,
1376 ficr[29].value, ficr[30].value, ficr[31].value, ficr[32].value, ficr[33].value,
1377 (uicr[0].value == 0xFFFFFFFF) ? 0 : uicr[0].value / 1024,
1378 uicr[1].value & 0xFFFF,
1379 uicr[2].value & 0xFF,
1380 uicr[3].value & 0xFFFF);
1381
1382 return ERROR_OK;
1383 }
1384
1385 static const struct command_registration nrf5_exec_command_handlers[] = {
1386 {
1387 .name = "mass_erase",
1388 .handler = nrf5_handle_mass_erase_command,
1389 .mode = COMMAND_EXEC,
1390 .help = "Erase all flash contents of the chip.",
1391 .usage = "",
1392 },
1393 {
1394 .name = "info",
1395 .handler = nrf5_handle_info_command,
1396 .mode = COMMAND_EXEC,
1397 .help = "Show FICR and UICR info.",
1398 .usage = "",
1399 },
1400 COMMAND_REGISTRATION_DONE
1401 };
1402
1403 static const struct command_registration nrf5_command_handlers[] = {
1404 {
1405 .name = "nrf5",
1406 .mode = COMMAND_ANY,
1407 .help = "nrf5 flash command group",
1408 .usage = "",
1409 .chain = nrf5_exec_command_handlers,
1410 },
1411 {
1412 .name = "nrf51",
1413 .mode = COMMAND_ANY,
1414 .help = "nrf51 flash command group",
1415 .usage = "",
1416 .chain = nrf5_exec_command_handlers,
1417 },
1418 COMMAND_REGISTRATION_DONE
1419 };
1420
1421 const struct flash_driver nrf5_flash = {
1422 .name = "nrf5",
1423 .commands = nrf5_command_handlers,
1424 .flash_bank_command = nrf5_flash_bank_command,
1425 .info = nrf5_info,
1426 .erase = nrf5_erase,
1427 .protect = nrf5_protect,
1428 .write = nrf5_write,
1429 .read = default_flash_read,
1430 .probe = nrf5_probe,
1431 .auto_probe = nrf5_auto_probe,
1432 .erase_check = default_flash_blank_check,
1433 .protect_check = nrf5_protect_check,
1434 .free_driver_priv = nrf5_free_driver_priv,
1435 };
1436
1437 /* We need to retain the flash-driver name as well as the commands
1438 * for backwards compatibility */
1439 const struct flash_driver nrf51_flash = {
1440 .name = "nrf51",
1441 .commands = nrf5_command_handlers,
1442 .flash_bank_command = nrf5_flash_bank_command,
1443 .info = nrf5_info,
1444 .erase = nrf5_erase,
1445 .protect = nrf5_protect,
1446 .write = nrf5_write,
1447 .read = default_flash_read,
1448 .probe = nrf5_probe,
1449 .auto_probe = nrf5_auto_probe,
1450 .erase_check = default_flash_blank_check,
1451 .protect_check = nrf5_protect_check,
1452 .free_driver_priv = nrf5_free_driver_priv,
1453 };

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)