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

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)