OpenOCD
stm32l4x.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2015 by Uwe Bonnes *
5  * bon@elektron.ikp.physik.tu-darmstadt.de *
6  * *
7  * Copyright (C) 2019 by Tarek Bochkati for STMicroelectronics *
8  * tarek.bouchkati@gmail.com *
9  ***************************************************************************/
10 
11 #ifdef HAVE_CONFIG_H
12 #include "config.h"
13 #endif
14 
15 #include "imp.h"
16 #include <helper/align.h>
17 #include <helper/binarybuffer.h>
18 #include <helper/bits.h>
19 #include <target/algorithm.h>
20 #include <target/arm_adi_v5.h>
21 #include <target/cortex_m.h>
22 #include "stm32l4x.h"
23 
24 /* STM32L4xxx series for reference.
25  *
26  * RM0351 (STM32L4x5/STM32L4x6)
27  * http://www.st.com/resource/en/reference_manual/dm00083560.pdf
28  *
29  * RM0394 (STM32L43x/44x/45x/46x)
30  * http://www.st.com/resource/en/reference_manual/dm00151940.pdf
31  *
32  * RM0432 (STM32L4R/4Sxx)
33  * http://www.st.com/resource/en/reference_manual/dm00310109.pdf
34  *
35  * STM32L476RG Datasheet (for erase timing)
36  * http://www.st.com/resource/en/datasheet/stm32l476rg.pdf
37  *
38  * The RM0351 devices have normally two banks, but on 512 and 256 kiB devices
39  * an option byte is available to map all sectors to the first bank.
40  * Both STM32 banks are treated as one OpenOCD bank, as other STM32 devices
41  * handlers do!
42  *
43  * RM0394 devices have a single bank only.
44  *
45  * RM0432 devices have single and dual bank operating modes.
46  * - for STM32L4R/Sxx the FLASH size is 2Mbyte or 1Mbyte.
47  * - for STM32L4P/Q5x the FLASH size is 1Mbyte or 512Kbyte.
48  * Bank page (sector) size is 4Kbyte (dual mode) or 8Kbyte (single mode).
49  *
50  * Bank mode is controlled by two different bits in option bytes register.
51  * - for STM32L4R/Sxx
52  * In 2M FLASH devices bit 22 (DBANK) controls Dual Bank mode.
53  * In 1M FLASH devices bit 21 (DB1M) controls Dual Bank mode.
54  * - for STM32L4P5/Q5x
55  * In 1M FLASH devices bit 22 (DBANK) controls Dual Bank mode.
56  * In 512K FLASH devices bit 21 (DB512K) controls Dual Bank mode.
57  */
58 
59 /* STM32WBxxx series for reference.
60  *
61  * RM0493 (STM32WBA52x)
62  * http://www.st.com/resource/en/reference_manual/dm00821869.pdf
63  *
64  * RM0434 (STM32WB55/WB35x)
65  * http://www.st.com/resource/en/reference_manual/dm00318631.pdf
66  *
67  * RM0471 (STM32WB50/WB30x)
68  * http://www.st.com/resource/en/reference_manual/dm00622834.pdf
69  *
70  * RM0473 (STM32WB15x)
71  * http://www.st.com/resource/en/reference_manual/dm00649196.pdf
72  *
73  * RM0478 (STM32WB10x)
74  * http://www.st.com/resource/en/reference_manual/dm00689203.pdf
75  */
76 
77 /* STM32WLxxx series for reference.
78  *
79  * RM0461 (STM32WLEx)
80  * http://www.st.com/resource/en/reference_manual/dm00530369.pdf
81  *
82  * RM0453 (STM32WL5x)
83  * http://www.st.com/resource/en/reference_manual/dm00451556.pdf
84  */
85 
86 /* STM32C0xxx series for reference.
87  *
88  * RM0490 (STM32C0x1)
89  * http://www.st.com/resource/en/reference_manual/dm00781702.pdf
90  */
91 
92 /* STM32G0xxx series for reference.
93  *
94  * RM0444 (STM32G0x1)
95  * http://www.st.com/resource/en/reference_manual/dm00371828.pdf
96  *
97  * RM0454 (STM32G0x0)
98  * http://www.st.com/resource/en/reference_manual/dm00463896.pdf
99  */
100 
101 /* STM32G4xxx series for reference.
102  *
103  * RM0440 (STM32G43x/44x/47x/48x/49x/4Ax)
104  * http://www.st.com/resource/en/reference_manual/dm00355726.pdf
105  *
106  * Cat. 2 devices have single bank only, page size is 2kByte.
107  *
108  * Cat. 3 devices have single and dual bank operating modes,
109  * Page size is 2kByte (dual mode) or 4kByte (single mode).
110  *
111  * Bank mode is controlled by bit 22 (DBANK) in option bytes register.
112  * Both banks are treated as a single OpenOCD bank.
113  *
114  * Cat. 4 devices have single bank only, page size is 2kByte.
115  */
116 
117 /* STM32L5xxx series for reference.
118  *
119  * RM0428 (STM32L552xx/STM32L562xx)
120  * http://www.st.com/resource/en/reference_manual/dm00346336.pdf
121  */
122 
123 /* STM32U0xxx series for reference.
124  *
125  * RM0503 (STM32U0xx)
126  * https://www.st.com/resource/en/reference_manual/rm0503-stm32u0-series-advanced-armbased-32bit-mcus-stmicroelectronics.pdf
127  */
128 
129 /* STM32U5xxx series for reference.
130  *
131  * RM0456 (STM32U5xx)
132  * http://www.st.com/resource/en/reference_manual/dm00477635.pdf
133  */
134 
135 /* Erase time can be as high as 25ms, 10x this and assume it's toast... */
136 
137 #define FLASH_ERASE_TIMEOUT 250
138 #define FLASH_WRITE_TIMEOUT 50
139 
140 
141 /* relevant STM32L4 flags ****************************************************/
142 #define F_NONE 0
143 /* this flag indicates if the device flash is with dual bank architecture */
144 #define F_HAS_DUAL_BANK BIT(0)
145 /* this flags is used for dual bank devices only, it indicates if the
146  * 4 WRPxx are usable if the device is configured in single-bank mode */
147 #define F_USE_ALL_WRPXX BIT(1)
148 /* this flag indicates if the device embeds a TrustZone security feature */
149 #define F_HAS_TZ BIT(2)
150 /* this flag indicates if the device has the same flash registers as STM32L5 */
151 #define F_HAS_L5_FLASH_REGS BIT(3)
152 /* this flag indicates that programming should be done in quad-word
153  * the default programming word size is double-word */
154 #define F_QUAD_WORD_PROG BIT(4)
155 /* end of STM32L4 flags ******************************************************/
156 
157 
164  /* for some devices like STM32WL5x, the CPU2 have a dedicated C2CR register w/o LOCKs,
165  * so it uses the C2CR for flash operations and CR for checking locks and locking */
166  STM32_FLASH_CR_WLK_INDEX, /* FLASH_CR_WITH_LOCK */
173 };
174 
176  RDP_LEVEL_0 = 0xAA,
177  RDP_LEVEL_0_5 = 0x55, /* for devices with TrustZone enabled */
178  RDP_LEVEL_1 = 0x00,
179  RDP_LEVEL_2 = 0xCC
180 };
181 
183  [STM32_FLASH_ACR_INDEX] = 0x000,
184  [STM32_FLASH_KEYR_INDEX] = 0x008,
185  [STM32_FLASH_OPTKEYR_INDEX] = 0x00C,
186  [STM32_FLASH_SR_INDEX] = 0x010,
187  [STM32_FLASH_CR_INDEX] = 0x014,
188  [STM32_FLASH_OPTR_INDEX] = 0x020,
189  [STM32_FLASH_WRP1AR_INDEX] = 0x02C,
190  [STM32_FLASH_WRP1BR_INDEX] = 0x030,
191  [STM32_FLASH_WRP2AR_INDEX] = 0x04C,
192  [STM32_FLASH_WRP2BR_INDEX] = 0x050,
193 };
194 
196  [STM32_FLASH_ACR_INDEX] = 0x000,
197  [STM32_FLASH_KEYR_INDEX] = 0x008,
198  [STM32_FLASH_OPTKEYR_INDEX] = 0x010,
199  [STM32_FLASH_SR_INDEX] = 0x060,
200  [STM32_FLASH_CR_INDEX] = 0x064,
201  [STM32_FLASH_CR_WLK_INDEX] = 0x014,
202  [STM32_FLASH_OPTR_INDEX] = 0x020,
203  [STM32_FLASH_WRP1AR_INDEX] = 0x02C,
204  [STM32_FLASH_WRP1BR_INDEX] = 0x030,
205 };
206 
208  [STM32_FLASH_ACR_INDEX] = 0x000,
209  [STM32_FLASH_KEYR_INDEX] = 0x008, /* NSKEYR */
210  [STM32_FLASH_OPTKEYR_INDEX] = 0x010,
211  [STM32_FLASH_SR_INDEX] = 0x020, /* NSSR */
212  [STM32_FLASH_CR_INDEX] = 0x028, /* NSCR */
213  [STM32_FLASH_OPTR_INDEX] = 0x040,
214  [STM32_FLASH_WRP1AR_INDEX] = 0x058,
215  [STM32_FLASH_WRP1BR_INDEX] = 0x05C,
216  [STM32_FLASH_WRP2AR_INDEX] = 0x068,
217  [STM32_FLASH_WRP2BR_INDEX] = 0x06C,
218 };
219 
221  [STM32_FLASH_ACR_INDEX] = 0x000,
222  [STM32_FLASH_KEYR_INDEX] = 0x00C, /* SECKEYR */
223  [STM32_FLASH_OPTKEYR_INDEX] = 0x010,
224  [STM32_FLASH_SR_INDEX] = 0x024, /* SECSR */
225  [STM32_FLASH_CR_INDEX] = 0x02C, /* SECCR */
226  [STM32_FLASH_OPTR_INDEX] = 0x040,
227  [STM32_FLASH_WRP1AR_INDEX] = 0x058,
228  [STM32_FLASH_WRP1BR_INDEX] = 0x05C,
229  [STM32_FLASH_WRP2AR_INDEX] = 0x068,
230  [STM32_FLASH_WRP2BR_INDEX] = 0x06C,
231 };
232 
233 struct stm32l4_rev {
234  const uint16_t rev;
235  const char *str;
236 };
237 
239  uint16_t id;
240  const char *device_str;
241  const struct stm32l4_rev *revs;
242  const size_t num_revs;
243  const uint16_t max_flash_size_kb;
244  const uint32_t flags; /* one bit per feature, see STM32L4 flags: macros F_XXX */
245  const uint32_t flash_regs_base;
246  const uint32_t fsize_addr;
247  const uint32_t otp_base;
248  const uint32_t otp_size;
249 };
250 
252  bool probed;
253  uint32_t idcode;
254  unsigned int bank1_sectors;
257  uint32_t user_bank_size;
258  uint32_t data_width;
259  uint32_t cr_bker_mask;
260  uint32_t sr_bsy_mask;
261  uint32_t wrpxxr_mask;
263  uint32_t flash_regs_base;
264  const uint32_t *flash_regs;
266  enum stm32l4_rdp rdp;
267  bool tzen;
268  uint32_t optr;
269 };
270 
275 };
276 
277 struct stm32l4_wrp {
279  uint32_t value;
280  bool used;
281  int first;
282  int last;
283  int offset;
284 };
285 
286 /* human readable list of families this drivers supports (sorted alphabetically) */
287 static const char *device_families = "STM32C0/G0/G4/L4/L4+/L5/U0/U5/WB/WL";
288 
289 static const struct stm32l4_rev stm32l47_l48xx_revs[] = {
290  { 0x1000, "1" }, { 0x1001, "2" }, { 0x1003, "3" }, { 0x1007, "4" }
291 };
292 
293 static const struct stm32l4_rev stm32l43_l44xx_revs[] = {
294  { 0x1000, "A" }, { 0x1001, "Z" }, { 0x2001, "Y" },
295 };
296 
297 
298 static const struct stm32l4_rev stm32c01xx_revs[] = {
299  { 0x1000, "A" }, { 0x1001, "Z" },
300 };
301 
302 static const struct stm32l4_rev stm32c03xx_revs[] = {
303  { 0x1000, "A" }, { 0x1001, "Z" },
304 };
305 
306 static const struct stm32l4_rev stm32c071xx_revs[] = {
307  { 0x1001, "Z" },
308 };
309 
310 static const struct stm32l4_rev stm32g05_g06xx_revs[] = {
311  { 0x1000, "A" },
312 };
313 
314 static const struct stm32l4_rev stm32_g07_g08xx_revs[] = {
315  { 0x1000, "A/Z" } /* A and Z, no typo in RM! */, { 0x2000, "B" },
316 };
317 
318 static const struct stm32l4_rev stm32l49_l4axx_revs[] = {
319  { 0x1000, "A" }, { 0x2000, "B" },
320 };
321 
322 static const struct stm32l4_rev stm32l45_l46xx_revs[] = {
323  { 0x1000, "A" }, { 0x1001, "Z" }, { 0x2001, "Y" },
324 };
325 
326 static const struct stm32l4_rev stm32l41_l42xx_revs[] = {
327  { 0x1000, "A" }, { 0x1001, "Z" }, { 0x2001, "Y" },
328 };
329 
330 static const struct stm32l4_rev stm32g03_g04xx_revs[] = {
331  { 0x1000, "A" }, { 0x1001, "Z" }, { 0x2000, "B" },
332 };
333 
334 static const struct stm32l4_rev stm32g0b_g0cxx_revs[] = {
335  { 0x1000, "A" },
336 };
337 
338 static const struct stm32l4_rev stm32u0xx_revs[] = {
339  { 0x1000, "A" },
340 };
341 
342 static const struct stm32l4_rev stm32g43_g44xx_revs[] = {
343  { 0x1000, "A" }, { 0x2000, "B" }, { 0x2001, "Z" },
344 };
345 
346 static const struct stm32l4_rev stm32g47_g48xx_revs[] = {
347  { 0x1000, "A" }, { 0x2000, "B" }, { 0x2001, "Z" },
348 };
349 
350 static const struct stm32l4_rev stm32l4r_l4sxx_revs[] = {
351  { 0x1000, "A" }, { 0x1001, "Z" }, { 0x1003, "Y" }, { 0x100F, "W" },
352  { 0x101F, "V" },
353 };
354 
355 static const struct stm32l4_rev stm32l4p_l4qxx_revs[] = {
356  { 0x1001, "Z" },
357 };
358 
359 static const struct stm32l4_rev stm32l55_l56xx_revs[] = {
360  { 0x1000, "A" }, { 0x2000, "B" }, { 0x2001, "Z" },
361 };
362 
363 static const struct stm32l4_rev stm32g49_g4axx_revs[] = {
364  { 0x1000, "A" },
365 };
366 
367 static const struct stm32l4_rev stm32u53_u54xx_revs[] = {
368  { 0x1000, "A" }, { 0x1001, "Z" },
369 };
370 
371 static const struct stm32l4_rev stm32u57_u58xx_revs[] = {
372  { 0x1000, "A" }, { 0x1001, "Z" }, { 0x1003, "Y" }, { 0x2000, "B" },
373  { 0x2001, "X" }, { 0x3000, "C" }, { 0x3001, "W" },
374 };
375 
376 static const struct stm32l4_rev stm32u59_u5axx_revs[] = {
377  { 0x3001, "X" },
378 };
379 
380 static const struct stm32l4_rev stm32wba5x_revs[] = {
381  { 0x1000, "A" },
382 };
383 
384 static const struct stm32l4_rev stm32wb1xx_revs[] = {
385  { 0x1000, "A" }, { 0x2000, "B" },
386 };
387 
388 static const struct stm32l4_rev stm32wb5xx_revs[] = {
389  { 0x2001, "2.1" },
390 };
391 
392 static const struct stm32l4_rev stm32wb3xx_revs[] = {
393  { 0x1000, "A" },
394 };
395 
396 static const struct stm32l4_rev stm32wle_wl5xx_revs[] = {
397  { 0x1000, "1.0" },
398 };
399 
400 static const struct stm32l4_part_info stm32l4_parts[] = {
401  {
403  .revs = stm32l47_l48xx_revs,
404  .num_revs = ARRAY_SIZE(stm32l47_l48xx_revs),
405  .device_str = "STM32L47/L48xx",
406  .max_flash_size_kb = 1024,
407  .flags = F_HAS_DUAL_BANK,
408  .flash_regs_base = 0x40022000,
409  .fsize_addr = 0x1FFF75E0,
410  .otp_base = 0x1FFF7000,
411  .otp_size = 1024,
412  },
413  {
414  .id = DEVID_STM32L43_L44XX,
415  .revs = stm32l43_l44xx_revs,
416  .num_revs = ARRAY_SIZE(stm32l43_l44xx_revs),
417  .device_str = "STM32L43/L44xx",
418  .max_flash_size_kb = 256,
419  .flags = F_NONE,
420  .flash_regs_base = 0x40022000,
421  .fsize_addr = 0x1FFF75E0,
422  .otp_base = 0x1FFF7000,
423  .otp_size = 1024,
424  },
425  {
426  .id = DEVID_STM32C01XX,
427  .revs = stm32c01xx_revs,
428  .num_revs = ARRAY_SIZE(stm32c01xx_revs),
429  .device_str = "STM32C01xx",
430  .max_flash_size_kb = 32,
431  .flags = F_NONE,
432  .flash_regs_base = 0x40022000,
433  .fsize_addr = 0x1FFF75A0,
434  .otp_base = 0x1FFF7000,
435  .otp_size = 1024,
436  },
437  {
438  .id = DEVID_STM32C03XX,
439  .revs = stm32c03xx_revs,
440  .num_revs = ARRAY_SIZE(stm32c03xx_revs),
441  .device_str = "STM32C03xx",
442  .max_flash_size_kb = 32,
443  .flags = F_NONE,
444  .flash_regs_base = 0x40022000,
445  .fsize_addr = 0x1FFF75A0,
446  .otp_base = 0x1FFF7000,
447  .otp_size = 1024,
448  },
449  {
450  .id = DEVID_STM32C071XX,
451  .revs = stm32c071xx_revs,
452  .num_revs = ARRAY_SIZE(stm32c071xx_revs),
453  .device_str = "STM32C071xx",
454  .max_flash_size_kb = 128,
455  .flags = F_NONE,
456  .flash_regs_base = 0x40022000,
457  .fsize_addr = 0x1FFF75A0,
458  .otp_base = 0x1FFF7000,
459  .otp_size = 1024,
460  },
461  {
462  .id = DEVID_STM32U53_U54XX,
463  .revs = stm32u53_u54xx_revs,
464  .num_revs = ARRAY_SIZE(stm32u53_u54xx_revs),
465  .device_str = "STM32U535/U545",
466  .max_flash_size_kb = 512,
468  .flash_regs_base = 0x40022000,
469  .fsize_addr = 0x0BFA07A0,
470  .otp_base = 0x0BFA0000,
471  .otp_size = 512,
472  },
473  {
474  .id = DEVID_STM32G05_G06XX,
475  .revs = stm32g05_g06xx_revs,
476  .num_revs = ARRAY_SIZE(stm32g05_g06xx_revs),
477  .device_str = "STM32G05/G06xx",
478  .max_flash_size_kb = 64,
479  .flags = F_NONE,
480  .flash_regs_base = 0x40022000,
481  .fsize_addr = 0x1FFF75E0,
482  .otp_base = 0x1FFF7000,
483  .otp_size = 1024,
484  },
485  {
486  .id = DEVID_STM32G07_G08XX,
487  .revs = stm32_g07_g08xx_revs,
488  .num_revs = ARRAY_SIZE(stm32_g07_g08xx_revs),
489  .device_str = "STM32G07/G08xx",
490  .max_flash_size_kb = 128,
491  .flags = F_NONE,
492  .flash_regs_base = 0x40022000,
493  .fsize_addr = 0x1FFF75E0,
494  .otp_base = 0x1FFF7000,
495  .otp_size = 1024,
496  },
497  {
498  .id = DEVID_STM32L49_L4AXX,
499  .revs = stm32l49_l4axx_revs,
500  .num_revs = ARRAY_SIZE(stm32l49_l4axx_revs),
501  .device_str = "STM32L49/L4Axx",
502  .max_flash_size_kb = 1024,
503  .flags = F_HAS_DUAL_BANK,
504  .flash_regs_base = 0x40022000,
505  .fsize_addr = 0x1FFF75E0,
506  .otp_base = 0x1FFF7000,
507  .otp_size = 1024,
508  },
509  {
510  .id = DEVID_STM32L45_L46XX,
511  .revs = stm32l45_l46xx_revs,
512  .num_revs = ARRAY_SIZE(stm32l45_l46xx_revs),
513  .device_str = "STM32L45/L46xx",
514  .max_flash_size_kb = 512,
515  .flags = F_NONE,
516  .flash_regs_base = 0x40022000,
517  .fsize_addr = 0x1FFF75E0,
518  .otp_base = 0x1FFF7000,
519  .otp_size = 1024,
520  },
521  {
522  .id = DEVID_STM32L41_L42XX,
523  .revs = stm32l41_l42xx_revs,
524  .num_revs = ARRAY_SIZE(stm32l41_l42xx_revs),
525  .device_str = "STM32L41/L42xx",
526  .max_flash_size_kb = 128,
527  .flags = F_NONE,
528  .flash_regs_base = 0x40022000,
529  .fsize_addr = 0x1FFF75E0,
530  .otp_base = 0x1FFF7000,
531  .otp_size = 1024,
532  },
533  {
534  .id = DEVID_STM32G03_G04XX,
535  .revs = stm32g03_g04xx_revs,
536  .num_revs = ARRAY_SIZE(stm32g03_g04xx_revs),
537  .device_str = "STM32G03x/G04xx",
538  .max_flash_size_kb = 64,
539  .flags = F_NONE,
540  .flash_regs_base = 0x40022000,
541  .fsize_addr = 0x1FFF75E0,
542  .otp_base = 0x1FFF7000,
543  .otp_size = 1024,
544  },
545  {
546  .id = DEVID_STM32G0B_G0CXX,
547  .revs = stm32g0b_g0cxx_revs,
548  .num_revs = ARRAY_SIZE(stm32g0b_g0cxx_revs),
549  .device_str = "STM32G0B/G0Cx",
550  .max_flash_size_kb = 512,
551  .flags = F_HAS_DUAL_BANK,
552  .flash_regs_base = 0x40022000,
553  .fsize_addr = 0x1FFF75E0,
554  .otp_base = 0x1FFF7000,
555  .otp_size = 1024,
556  },
557  {
558  .id = DEVID_STM32G43_G44XX,
559  .revs = stm32g43_g44xx_revs,
560  .num_revs = ARRAY_SIZE(stm32g43_g44xx_revs),
561  .device_str = "STM32G43/G44xx",
562  .max_flash_size_kb = 128,
563  .flags = F_NONE,
564  .flash_regs_base = 0x40022000,
565  .fsize_addr = 0x1FFF75E0,
566  .otp_base = 0x1FFF7000,
567  .otp_size = 1024,
568  },
569  {
570  .id = DEVID_STM32G47_G48XX,
571  .revs = stm32g47_g48xx_revs,
572  .num_revs = ARRAY_SIZE(stm32g47_g48xx_revs),
573  .device_str = "STM32G47/G48xx",
574  .max_flash_size_kb = 512,
575  .flags = F_HAS_DUAL_BANK | F_USE_ALL_WRPXX,
576  .flash_regs_base = 0x40022000,
577  .fsize_addr = 0x1FFF75E0,
578  .otp_base = 0x1FFF7000,
579  .otp_size = 1024,
580  },
581  {
582  .id = DEVID_STM32L4R_L4SXX,
583  .revs = stm32l4r_l4sxx_revs,
584  .num_revs = ARRAY_SIZE(stm32l4r_l4sxx_revs),
585  .device_str = "STM32L4R/L4Sxx",
586  .max_flash_size_kb = 2048,
587  .flags = F_HAS_DUAL_BANK | F_USE_ALL_WRPXX,
588  .flash_regs_base = 0x40022000,
589  .fsize_addr = 0x1FFF75E0,
590  .otp_base = 0x1FFF7000,
591  .otp_size = 1024,
592  },
593  {
594  .id = DEVID_STM32L4P_L4QXX,
595  .revs = stm32l4p_l4qxx_revs,
596  .num_revs = ARRAY_SIZE(stm32l4p_l4qxx_revs),
597  .device_str = "STM32L4P/L4Qxx",
598  .max_flash_size_kb = 1024,
599  .flags = F_HAS_DUAL_BANK | F_USE_ALL_WRPXX,
600  .flash_regs_base = 0x40022000,
601  .fsize_addr = 0x1FFF75E0,
602  .otp_base = 0x1FFF7000,
603  .otp_size = 1024,
604  },
605  {
606  .id = DEVID_STM32L55_L56XX,
607  .revs = stm32l55_l56xx_revs,
608  .num_revs = ARRAY_SIZE(stm32l55_l56xx_revs),
609  .device_str = "STM32L55/L56xx",
610  .max_flash_size_kb = 512,
612  .flash_regs_base = 0x40022000,
613  .fsize_addr = 0x0BFA05E0,
614  .otp_base = 0x0BFA0000,
615  .otp_size = 512,
616  },
617  {
618  .id = DEVID_STM32G49_G4AXX,
619  .revs = stm32g49_g4axx_revs,
620  .num_revs = ARRAY_SIZE(stm32g49_g4axx_revs),
621  .device_str = "STM32G49/G4Axx",
622  .max_flash_size_kb = 512,
623  .flags = F_NONE,
624  .flash_regs_base = 0x40022000,
625  .fsize_addr = 0x1FFF75E0,
626  .otp_base = 0x1FFF7000,
627  .otp_size = 1024,
628  },
629  {
630  .id = DEVID_STM32U031XX,
631  .revs = stm32u0xx_revs,
632  .num_revs = ARRAY_SIZE(stm32u0xx_revs),
633  .device_str = "STM32U031xx",
634  .max_flash_size_kb = 64,
635  .flags = F_NONE,
636  .flash_regs_base = 0x40022000,
637  .fsize_addr = 0x1FFF3EA0,
638  .otp_base = 0x1FFF6800,
639  .otp_size = 1024,
640  },
641  {
643  .revs = stm32u0xx_revs,
644  .num_revs = ARRAY_SIZE(stm32u0xx_revs),
645  .device_str = "STM32U073/U083xx",
646  .max_flash_size_kb = 256,
647  .flags = F_NONE,
648  .flash_regs_base = 0x40022000,
649  .fsize_addr = 0x1FFF6EA0,
650  .otp_base = 0x1FFF6800,
651  .otp_size = 1024,
652  },
653  {
654  .id = DEVID_STM32U59_U5AXX,
655  .revs = stm32u59_u5axx_revs,
656  .num_revs = ARRAY_SIZE(stm32u59_u5axx_revs),
657  .device_str = "STM32U59/U5Axx",
658  .max_flash_size_kb = 4096,
660  .flash_regs_base = 0x40022000,
661  .fsize_addr = 0x0BFA07A0,
662  .otp_base = 0x0BFA0000,
663  .otp_size = 512,
664  },
665  {
666  .id = DEVID_STM32U57_U58XX,
667  .revs = stm32u57_u58xx_revs,
668  .num_revs = ARRAY_SIZE(stm32u57_u58xx_revs),
669  .device_str = "STM32U57/U58xx",
670  .max_flash_size_kb = 2048,
672  .flash_regs_base = 0x40022000,
673  .fsize_addr = 0x0BFA07A0,
674  .otp_base = 0x0BFA0000,
675  .otp_size = 512,
676  },
677  {
678  .id = DEVID_STM32WBA5X,
679  .revs = stm32wba5x_revs,
680  .num_revs = ARRAY_SIZE(stm32wba5x_revs),
681  .device_str = "STM32WBA5x",
682  .max_flash_size_kb = 1024,
684  .flash_regs_base = 0x40022000,
685  .fsize_addr = 0x0FF907A0,
686  .otp_base = 0x0FF90000,
687  .otp_size = 512,
688  },
689  {
690  .id = DEVID_STM32WB1XX,
691  .revs = stm32wb1xx_revs,
692  .num_revs = ARRAY_SIZE(stm32wb1xx_revs),
693  .device_str = "STM32WB1x",
694  .max_flash_size_kb = 320,
695  .flags = F_NONE,
696  .flash_regs_base = 0x58004000,
697  .fsize_addr = 0x1FFF75E0,
698  .otp_base = 0x1FFF7000,
699  .otp_size = 1024,
700  },
701  {
702  .id = DEVID_STM32WB5XX,
703  .revs = stm32wb5xx_revs,
704  .num_revs = ARRAY_SIZE(stm32wb5xx_revs),
705  .device_str = "STM32WB5x",
706  .max_flash_size_kb = 1024,
707  .flags = F_NONE,
708  .flash_regs_base = 0x58004000,
709  .fsize_addr = 0x1FFF75E0,
710  .otp_base = 0x1FFF7000,
711  .otp_size = 1024,
712  },
713  {
714  .id = DEVID_STM32WB3XX,
715  .revs = stm32wb3xx_revs,
716  .num_revs = ARRAY_SIZE(stm32wb3xx_revs),
717  .device_str = "STM32WB3x",
718  .max_flash_size_kb = 512,
719  .flags = F_NONE,
720  .flash_regs_base = 0x58004000,
721  .fsize_addr = 0x1FFF75E0,
722  .otp_base = 0x1FFF7000,
723  .otp_size = 1024,
724  },
725  {
726  .id = DEVID_STM32WLE_WL5XX,
727  .revs = stm32wle_wl5xx_revs,
728  .num_revs = ARRAY_SIZE(stm32wle_wl5xx_revs),
729  .device_str = "STM32WLE/WL5x",
730  .max_flash_size_kb = 256,
731  .flags = F_NONE,
732  .flash_regs_base = 0x58004000,
733  .fsize_addr = 0x1FFF75E0,
734  .otp_base = 0x1FFF7000,
735  .otp_size = 1024,
736  },
737 };
738 
739 /* flash bank stm32l4x <base> <size> 0 0 <target#> */
740 FLASH_BANK_COMMAND_HANDLER(stm32l4_flash_bank_command)
741 {
742  struct stm32l4_flash_bank *stm32l4_info;
743 
744  if (CMD_ARGC < 6)
746 
747  /* fix-up bank base address: 0 is used for normal flash memory */
748  if (bank->base == 0)
749  bank->base = STM32_FLASH_BANK_BASE;
750 
751  stm32l4_info = calloc(1, sizeof(struct stm32l4_flash_bank));
752  if (!stm32l4_info)
753  return ERROR_FAIL; /* Checkme: What better error to use?*/
754  bank->driver_priv = stm32l4_info;
755 
756  stm32l4_info->probed = false;
757  stm32l4_info->otp_enabled = false;
758  stm32l4_info->user_bank_size = bank->size;
759 
760  return ERROR_OK;
761 }
762 
763 /* bitmap helper extension */
764 struct range {
765  unsigned int start;
766  unsigned int end;
767 };
768 
769 static void bitmap_to_ranges(unsigned long *bitmap, unsigned int nbits,
770  struct range *ranges, unsigned int *ranges_count)
771 {
772  *ranges_count = 0;
773  bool last_bit = 0, cur_bit;
774  for (unsigned int i = 0; i < nbits; i++) {
775  cur_bit = test_bit(i, bitmap);
776 
777  if (cur_bit && !last_bit) {
778  (*ranges_count)++;
779  ranges[*ranges_count - 1].start = i;
780  ranges[*ranges_count - 1].end = i;
781  } else if (cur_bit && last_bit) {
782  /* update (increment) the end this range */
783  ranges[*ranges_count - 1].end = i;
784  }
785 
786  last_bit = cur_bit;
787  }
788 }
789 
790 static inline int range_print_one(struct range *range, char *str)
791 {
792  if (range->start == range->end)
793  return sprintf(str, "[%d]", range->start);
794 
795  return sprintf(str, "[%d,%d]", range->start, range->end);
796 }
797 
798 static char *range_print_alloc(struct range *ranges, unsigned int ranges_count)
799 {
800  /* each range will be printed like the following: [start,end]
801  * start and end, both are unsigned int, an unsigned int takes 10 characters max
802  * plus 3 characters for '[', ',' and ']'
803  * thus means each range can take maximum 23 character
804  * after each range we add a ' ' as separator and finally we need the '\0'
805  * if the ranges_count is zero we reserve one char for '\0' to return an empty string */
806  char *str = calloc(1, ranges_count * (24 * sizeof(char)) + 1);
807  char *ptr = str;
808 
809  for (unsigned int i = 0; i < ranges_count; i++) {
810  ptr += range_print_one(&(ranges[i]), ptr);
811 
812  if (i < ranges_count - 1)
813  *(ptr++) = ' ';
814  }
815 
816  return str;
817 }
818 
819 /* end of bitmap helper extension */
820 
821 static inline bool stm32l4_is_otp(struct flash_bank *bank)
822 {
823  struct stm32l4_flash_bank *stm32l4_info = bank->driver_priv;
824  return bank->base == stm32l4_info->part_info->otp_base;
825 }
826 
827 static int stm32l4_otp_enable(struct flash_bank *bank, bool enable)
828 {
829  struct stm32l4_flash_bank *stm32l4_info = bank->driver_priv;
830 
831  if (!stm32l4_is_otp(bank))
832  return ERROR_FAIL;
833 
834  char *op_str = enable ? "enabled" : "disabled";
835 
836  LOG_INFO("OTP memory (bank #%d) is %s%s for write commands",
837  bank->bank_number,
838  stm32l4_info->otp_enabled == enable ? "already " : "",
839  op_str);
840 
841  stm32l4_info->otp_enabled = enable;
842 
843  return ERROR_OK;
844 }
845 
846 static inline bool stm32l4_otp_is_enabled(struct flash_bank *bank)
847 {
848  struct stm32l4_flash_bank *stm32l4_info = bank->driver_priv;
849  return stm32l4_info->otp_enabled;
850 }
851 
853 {
854  struct stm32l4_flash_bank *stm32l4_info = bank->driver_priv;
855 
856  bool tzen = false;
857 
858  if (stm32l4_info->part_info->flags & F_HAS_TZ)
859  tzen = (stm32l4_info->optr & FLASH_TZEN) != 0;
860 
861  uint32_t rdp = stm32l4_info->optr & FLASH_RDP_MASK;
862 
863  /* for devices without TrustZone:
864  * RDP level 0 and 2 values are to 0xAA and 0xCC
865  * Any other value corresponds to RDP level 1
866  * for devices with TrusZone:
867  * RDP level 0 and 2 values are 0xAA and 0xCC
868  * RDP level 0.5 value is 0x55 only if TZEN = 1
869  * Any other value corresponds to RDP level 1, including 0x55 if TZEN = 0
870  */
871 
872  if (rdp != RDP_LEVEL_0 && rdp != RDP_LEVEL_2) {
873  if (!tzen || (tzen && rdp != RDP_LEVEL_0_5))
874  rdp = RDP_LEVEL_1;
875  }
876 
877  stm32l4_info->tzen = tzen;
878  stm32l4_info->rdp = rdp;
879 }
880 
881 static inline uint32_t stm32l4_get_flash_reg(struct flash_bank *bank, uint32_t reg_offset)
882 {
883  struct stm32l4_flash_bank *stm32l4_info = bank->driver_priv;
884  return stm32l4_info->flash_regs_base + reg_offset;
885 }
886 
887 static inline uint32_t stm32l4_get_flash_reg_by_index(struct flash_bank *bank,
888  enum stm32l4_flash_reg_index reg_index)
889 {
890  struct stm32l4_flash_bank *stm32l4_info = bank->driver_priv;
891  return stm32l4_get_flash_reg(bank, stm32l4_info->flash_regs[reg_index]);
892 }
893 
894 static inline int stm32l4_read_flash_reg(struct flash_bank *bank, uint32_t reg_offset, uint32_t *value)
895 {
896  return target_read_u32(bank->target, stm32l4_get_flash_reg(bank, reg_offset), value);
897 }
898 
900  enum stm32l4_flash_reg_index reg_index, uint32_t *value)
901 {
902  struct stm32l4_flash_bank *stm32l4_info = bank->driver_priv;
903  return stm32l4_read_flash_reg(bank, stm32l4_info->flash_regs[reg_index], value);
904 }
905 
906 static inline int stm32l4_write_flash_reg(struct flash_bank *bank, uint32_t reg_offset, uint32_t value)
907 {
908  return target_write_u32(bank->target, stm32l4_get_flash_reg(bank, reg_offset), value);
909 }
910 
912  enum stm32l4_flash_reg_index reg_index, uint32_t value)
913 {
914  struct stm32l4_flash_bank *stm32l4_info = bank->driver_priv;
915  return stm32l4_write_flash_reg(bank, stm32l4_info->flash_regs[reg_index], value);
916 }
917 
919 {
920  struct stm32l4_flash_bank *stm32l4_info = bank->driver_priv;
921  uint32_t status;
922  int retval = ERROR_OK;
923 
924  /* wait for busy to clear */
925  for (;;) {
927  if (retval != ERROR_OK)
928  return retval;
929  LOG_DEBUG("status: 0x%" PRIx32 "", status);
930  if ((status & stm32l4_info->sr_bsy_mask) == 0)
931  break;
932  if (timeout-- <= 0) {
933  LOG_ERROR("timed out waiting for flash");
934  return ERROR_FAIL;
935  }
936  alive_sleep(1);
937  }
938 
939  if (status & FLASH_WRPERR) {
940  LOG_ERROR("stm32x device protected");
941  retval = ERROR_FAIL;
942  }
943 
944  /* Clear but report errors */
945  if (status & FLASH_ERROR) {
946  if (retval == ERROR_OK)
947  retval = ERROR_FAIL;
948  /* If this operation fails, we ignore it and report the original
949  * retval
950  */
952  }
953 
954  return retval;
955 }
956 
958 static int stm32l4_set_secbb(struct flash_bank *bank, uint32_t value)
959 {
960  /* This function should be used only with device with TrustZone, do just a security check */
961  struct stm32l4_flash_bank *stm32l4_info = bank->driver_priv;
962  assert(stm32l4_info->part_info->flags & F_HAS_TZ);
963 
964  /* based on RM0438 Rev6 for STM32L5x devices:
965  * to modify a page block-based security attribution, it is recommended to
966  * 1- check that no flash operation is ongoing on the related page
967  * 2- add ISB instruction after modifying the page security attribute in SECBBxRy
968  * this step is not need in case of JTAG direct access
969  */
971  if (retval != ERROR_OK)
972  return retval;
973 
974  /* write SECBBxRy registers */
975  LOG_DEBUG("setting secure block-based areas registers (SECBBxRy) to 0x%08x", value);
976 
977  const uint8_t secbb_regs[] = {
978  FLASH_SECBB1(1), FLASH_SECBB1(2), FLASH_SECBB1(3), FLASH_SECBB1(4), /* bank 1 SECBB register offsets */
979  FLASH_SECBB2(1), FLASH_SECBB2(2), FLASH_SECBB2(3), FLASH_SECBB2(4) /* bank 2 SECBB register offsets */
980  };
981 
982 
983  unsigned int num_secbb_regs = ARRAY_SIZE(secbb_regs);
984 
985  /* in single bank mode, it's useless to modify FLASH_SECBB2Rx registers
986  * then consider only the first half of secbb_regs
987  */
988  if (!stm32l4_info->dual_bank_mode)
989  num_secbb_regs /= 2;
990 
991  for (unsigned int i = 0; i < num_secbb_regs; i++) {
992  retval = stm32l4_write_flash_reg(bank, secbb_regs[i], value);
993  if (retval != ERROR_OK)
994  return retval;
995  }
996 
997  return ERROR_OK;
998 }
999 
1001 {
1002  struct stm32l4_flash_bank *stm32l4_info = bank->driver_priv;
1003  return (stm32l4_info->flash_regs[STM32_FLASH_CR_WLK_INDEX]) ?
1005 }
1006 
1008 {
1009  const uint32_t flash_cr_index = stm32l4_get_flash_cr_with_lock_index(bank);
1010  uint32_t ctrl;
1011 
1012  /* first check if not already unlocked
1013  * otherwise writing on STM32_FLASH_KEYR will fail
1014  */
1015  int retval = stm32l4_read_flash_reg_by_index(bank, flash_cr_index, &ctrl);
1016  if (retval != ERROR_OK)
1017  return retval;
1018 
1019  if ((ctrl & FLASH_LOCK) == 0)
1020  return ERROR_OK;
1021 
1022  /* unlock flash registers */
1024  if (retval != ERROR_OK)
1025  return retval;
1026 
1028  if (retval != ERROR_OK)
1029  return retval;
1030 
1031  retval = stm32l4_read_flash_reg_by_index(bank, flash_cr_index, &ctrl);
1032  if (retval != ERROR_OK)
1033  return retval;
1034 
1035  if (ctrl & FLASH_LOCK) {
1036  LOG_ERROR("flash not unlocked STM32_FLASH_CR: %" PRIx32, ctrl);
1037  return ERROR_TARGET_FAILURE;
1038  }
1039 
1040  return ERROR_OK;
1041 }
1042 
1044 {
1045  const uint32_t flash_cr_index = stm32l4_get_flash_cr_with_lock_index(bank);
1046  uint32_t ctrl;
1047 
1048  int retval = stm32l4_read_flash_reg_by_index(bank, flash_cr_index, &ctrl);
1049  if (retval != ERROR_OK)
1050  return retval;
1051 
1052  if ((ctrl & FLASH_OPTLOCK) == 0)
1053  return ERROR_OK;
1054 
1055  /* unlock option registers */
1057  if (retval != ERROR_OK)
1058  return retval;
1059 
1061  if (retval != ERROR_OK)
1062  return retval;
1063 
1064  retval = stm32l4_read_flash_reg_by_index(bank, flash_cr_index, &ctrl);
1065  if (retval != ERROR_OK)
1066  return retval;
1067 
1068  if (ctrl & FLASH_OPTLOCK) {
1069  LOG_ERROR("options not unlocked STM32_FLASH_CR: %" PRIx32, ctrl);
1070  return ERROR_TARGET_FAILURE;
1071  }
1072 
1073  return ERROR_OK;
1074 }
1075 
1077 {
1078  int retval, retval2;
1079 
1080  retval = stm32l4_unlock_reg(bank);
1081  if (retval != ERROR_OK)
1082  goto err_lock;
1083 
1084  retval = stm32l4_unlock_option_reg(bank);
1085  if (retval != ERROR_OK)
1086  goto err_lock;
1087 
1088  /* Set OBL_LAUNCH bit in CR -> system reset and option bytes reload,
1089  * but the RMs explicitly do *NOT* list this as power-on reset cause, and:
1090  * "Note: If the read protection is set while the debugger is still
1091  * connected through JTAG/SWD, apply a POR (power-on reset) instead of a system reset."
1092  */
1093 
1094  /* "Setting OBL_LAUNCH generates a reset so the option byte loading is performed under system reset" */
1095  /* Due to this reset ST-Link reports an SWD_DP_ERROR, despite the write was successful,
1096  * then just ignore the returned value */
1098 
1099  /* Need to re-probe after change */
1100  struct stm32l4_flash_bank *stm32l4_info = bank->driver_priv;
1101  stm32l4_info->probed = false;
1102 
1103 err_lock:
1106 
1107  if (retval != ERROR_OK)
1108  return retval;
1109 
1110  return retval2;
1111 }
1112 
1113 static int stm32l4_write_option(struct flash_bank *bank, uint32_t reg_offset,
1114  uint32_t value, uint32_t mask)
1115 {
1116  struct stm32l4_flash_bank *stm32l4_info = bank->driver_priv;
1117  uint32_t optiondata;
1118  int retval, retval2;
1119 
1120  retval = stm32l4_read_flash_reg(bank, reg_offset, &optiondata);
1121  if (retval != ERROR_OK)
1122  return retval;
1123 
1124  /* for STM32L5 and similar devices, use always non-secure
1125  * registers for option bytes programming */
1126  const uint32_t *saved_flash_regs = stm32l4_info->flash_regs;
1127  if (stm32l4_info->part_info->flags & F_HAS_L5_FLASH_REGS)
1128  stm32l4_info->flash_regs = stm32l5_ns_flash_regs;
1129 
1130  retval = stm32l4_unlock_reg(bank);
1131  if (retval != ERROR_OK)
1132  goto err_lock;
1133 
1134  retval = stm32l4_unlock_option_reg(bank);
1135  if (retval != ERROR_OK)
1136  goto err_lock;
1137 
1138  optiondata = (optiondata & ~mask) | (value & mask);
1139 
1140  retval = stm32l4_write_flash_reg(bank, reg_offset, optiondata);
1141  if (retval != ERROR_OK)
1142  goto err_lock;
1143 
1145  if (retval != ERROR_OK)
1146  goto err_lock;
1147 
1149 
1150 err_lock:
1153  stm32l4_info->flash_regs = saved_flash_regs;
1154 
1155  if (retval != ERROR_OK)
1156  return retval;
1157 
1158  return retval2;
1159 }
1160 
1161 static int stm32l4_get_one_wrpxy(struct flash_bank *bank, struct stm32l4_wrp *wrpxy,
1162  enum stm32l4_flash_reg_index reg_idx, int offset)
1163 {
1164  struct stm32l4_flash_bank *stm32l4_info = bank->driver_priv;
1165  int ret;
1166 
1167  wrpxy->reg_idx = reg_idx;
1168  wrpxy->offset = offset;
1169 
1170  ret = stm32l4_read_flash_reg_by_index(bank, wrpxy->reg_idx , &wrpxy->value);
1171  if (ret != ERROR_OK)
1172  return ret;
1173 
1174  wrpxy->first = (wrpxy->value & stm32l4_info->wrpxxr_mask) + wrpxy->offset;
1175  wrpxy->last = ((wrpxy->value >> 16) & stm32l4_info->wrpxxr_mask) + wrpxy->offset;
1176  wrpxy->used = wrpxy->first <= wrpxy->last;
1177 
1178  return ERROR_OK;
1179 }
1180 
1181 static int stm32l4_get_all_wrpxy(struct flash_bank *bank, enum stm32_bank_id dev_bank_id,
1182  struct stm32l4_wrp *wrpxy, unsigned int *n_wrp)
1183 {
1184  struct stm32l4_flash_bank *stm32l4_info = bank->driver_priv;
1185  int ret;
1186 
1187  *n_wrp = 0;
1188 
1189  /* for single bank devices there is 2 WRP regions.
1190  * for dual bank devices there is 2 WRP regions per bank,
1191  * if configured as single bank only 2 WRP are usable
1192  * except for STM32L4R/S/P/Q, G4 cat3, L5 ... all 4 WRP are usable
1193  * note: this should be revised, if a device will have the SWAP banks option
1194  */
1195 
1196  int wrp2y_sectors_offset = -1; /* -1 : unused */
1197 
1198  /* if bank_id is BANK1 or ALL_BANKS */
1199  if (dev_bank_id != STM32_BANK2) {
1200  /* get FLASH_WRP1AR */
1201  ret = stm32l4_get_one_wrpxy(bank, &wrpxy[(*n_wrp)++], STM32_FLASH_WRP1AR_INDEX, 0);
1202  if (ret != ERROR_OK)
1203  return ret;
1204 
1205  /* get WRP1BR */
1206  ret = stm32l4_get_one_wrpxy(bank, &wrpxy[(*n_wrp)++], STM32_FLASH_WRP1BR_INDEX, 0);
1207  if (ret != ERROR_OK)
1208  return ret;
1209 
1210  /* for some devices (like STM32L4R/S) in single-bank mode, the 4 WRPxx are usable */
1211  if ((stm32l4_info->part_info->flags & F_USE_ALL_WRPXX) && !stm32l4_info->dual_bank_mode)
1212  wrp2y_sectors_offset = 0;
1213  }
1214 
1215  /* if bank_id is BANK2 or ALL_BANKS */
1216  if (dev_bank_id != STM32_BANK1 && stm32l4_info->dual_bank_mode)
1217  wrp2y_sectors_offset = stm32l4_info->bank1_sectors;
1218 
1219  if (wrp2y_sectors_offset >= 0) {
1220  /* get WRP2AR */
1221  ret = stm32l4_get_one_wrpxy(bank, &wrpxy[(*n_wrp)++], STM32_FLASH_WRP2AR_INDEX, wrp2y_sectors_offset);
1222  if (ret != ERROR_OK)
1223  return ret;
1224 
1225  /* get WRP2BR */
1226  ret = stm32l4_get_one_wrpxy(bank, &wrpxy[(*n_wrp)++], STM32_FLASH_WRP2BR_INDEX, wrp2y_sectors_offset);
1227  if (ret != ERROR_OK)
1228  return ret;
1229  }
1230 
1231  return ERROR_OK;
1232 }
1233 
1234 static int stm32l4_write_one_wrpxy(struct flash_bank *bank, struct stm32l4_wrp *wrpxy)
1235 {
1236  struct stm32l4_flash_bank *stm32l4_info = bank->driver_priv;
1237 
1238  int wrp_start = wrpxy->first - wrpxy->offset;
1239  int wrp_end = wrpxy->last - wrpxy->offset;
1240 
1241  uint32_t wrp_value = (wrp_start & stm32l4_info->wrpxxr_mask) | ((wrp_end & stm32l4_info->wrpxxr_mask) << 16);
1242 
1243  return stm32l4_write_option(bank, stm32l4_info->flash_regs[wrpxy->reg_idx], wrp_value, 0xffffffff);
1244 }
1245 
1246 static int stm32l4_write_all_wrpxy(struct flash_bank *bank, struct stm32l4_wrp *wrpxy, unsigned int n_wrp)
1247 {
1248  int ret;
1249 
1250  for (unsigned int i = 0; i < n_wrp; i++) {
1251  ret = stm32l4_write_one_wrpxy(bank, &wrpxy[i]);
1252  if (ret != ERROR_OK)
1253  return ret;
1254  }
1255 
1256  return ERROR_OK;
1257 }
1258 
1260 {
1261  unsigned int n_wrp;
1262  struct stm32l4_wrp wrpxy[4];
1263 
1264  int ret = stm32l4_get_all_wrpxy(bank, STM32_ALL_BANKS, wrpxy, &n_wrp);
1265  if (ret != ERROR_OK)
1266  return ret;
1267 
1268  /* initialize all sectors as unprotected */
1269  for (unsigned int i = 0; i < bank->num_sectors; i++)
1270  bank->sectors[i].is_protected = 0;
1271 
1272  /* now check WRPxy and mark the protected sectors */
1273  for (unsigned int i = 0; i < n_wrp; i++) {
1274  if (wrpxy[i].used) {
1275  for (int s = wrpxy[i].first; s <= wrpxy[i].last; s++)
1276  bank->sectors[s].is_protected = 1;
1277  }
1278  }
1279 
1280  return ERROR_OK;
1281 }
1282 
1283 static int stm32l4_erase(struct flash_bank *bank, unsigned int first,
1284  unsigned int last)
1285 {
1286  struct stm32l4_flash_bank *stm32l4_info = bank->driver_priv;
1287  int retval, retval2;
1288 
1289  assert((first <= last) && (last < bank->num_sectors));
1290 
1291  if (stm32l4_is_otp(bank)) {
1292  LOG_ERROR("cannot erase OTP memory");
1294  }
1295 
1296  if (bank->target->state != TARGET_HALTED) {
1297  LOG_ERROR("Target not halted");
1298  return ERROR_TARGET_NOT_HALTED;
1299  }
1300 
1301  if (stm32l4_info->tzen && (stm32l4_info->rdp == RDP_LEVEL_0)) {
1302  /* set all FLASH pages as secure */
1304  if (retval != ERROR_OK) {
1305  /* restore all FLASH pages as non-secure */
1306  stm32l4_set_secbb(bank, FLASH_SECBB_NON_SECURE); /* ignore the return value */
1307  return retval;
1308  }
1309  }
1310 
1311  retval = stm32l4_unlock_reg(bank);
1312  if (retval != ERROR_OK)
1313  goto err_lock;
1314 
1315  /*
1316  Sector Erase
1317  To erase a sector, follow the procedure below:
1318  1. Check that no Flash memory operation is ongoing by
1319  checking the BSY bit in the FLASH_SR register
1320  2. Set the PER bit and select the page and bank
1321  you wish to erase in the FLASH_CR register
1322  3. Set the STRT bit in the FLASH_CR register
1323  4. Wait for the BSY bit to be cleared
1324  */
1325 
1326  for (unsigned int i = first; i <= last; i++) {
1327  uint32_t erase_flags;
1328  erase_flags = FLASH_PER | FLASH_STRT;
1329 
1330  if (i >= stm32l4_info->bank1_sectors) {
1331  uint8_t snb;
1332  snb = i - stm32l4_info->bank1_sectors;
1333  erase_flags |= snb << FLASH_PAGE_SHIFT | stm32l4_info->cr_bker_mask;
1334  } else
1335  erase_flags |= i << FLASH_PAGE_SHIFT;
1337  if (retval != ERROR_OK)
1338  break;
1339 
1341  if (retval != ERROR_OK)
1342  break;
1343  }
1344 
1345 err_lock:
1347 
1348  if (stm32l4_info->tzen && (stm32l4_info->rdp == RDP_LEVEL_0)) {
1349  /* restore all FLASH pages as non-secure */
1351  if (retval3 != ERROR_OK)
1352  return retval3;
1353  }
1354 
1355  if (retval != ERROR_OK)
1356  return retval;
1357 
1358  return retval2;
1359 }
1360 
1361 static int stm32l4_protect_same_bank(struct flash_bank *bank, enum stm32_bank_id bank_id, int set,
1362  unsigned int first, unsigned int last)
1363 {
1364  unsigned int i;
1365 
1366  /* check if the desired protection is already configured */
1367  for (i = first; i <= last; i++) {
1368  if (bank->sectors[i].is_protected != set)
1369  break;
1370  else if (i == last) {
1371  LOG_INFO("The specified sectors are already %s", set ? "protected" : "unprotected");
1372  return ERROR_OK;
1373  }
1374  }
1375 
1376  /* all sectors from first to last (or part of them) could have different
1377  * protection other than the requested */
1378  unsigned int n_wrp;
1379  struct stm32l4_wrp wrpxy[4];
1380 
1381  int ret = stm32l4_get_all_wrpxy(bank, bank_id, wrpxy, &n_wrp);
1382  if (ret != ERROR_OK)
1383  return ret;
1384 
1385  /* use bitmap and range helpers to optimize the WRP usage */
1386  DECLARE_BITMAP(pages, bank->num_sectors);
1387  bitmap_zero(pages, bank->num_sectors);
1388 
1389  for (i = 0; i < n_wrp; i++) {
1390  if (wrpxy[i].used) {
1391  for (int p = wrpxy[i].first; p <= wrpxy[i].last; p++)
1392  set_bit(p, pages);
1393  }
1394  }
1395 
1396  /* we have at most 'n_wrp' WRP areas
1397  * add one range if the user is trying to protect a fifth range */
1398  struct range ranges[n_wrp + 1];
1399  unsigned int ranges_count = 0;
1400 
1401  bitmap_to_ranges(pages, bank->num_sectors, ranges, &ranges_count);
1402 
1403  /* pretty-print the currently protected ranges */
1404  if (ranges_count > 0) {
1405  char *ranges_str = range_print_alloc(ranges, ranges_count);
1406  LOG_DEBUG("current protected areas: %s", ranges_str);
1407  free(ranges_str);
1408  } else
1409  LOG_DEBUG("current protected areas: none");
1410 
1411  if (set) { /* flash protect */
1412  for (i = first; i <= last; i++)
1413  set_bit(i, pages);
1414  } else { /* flash unprotect */
1415  for (i = first; i <= last; i++)
1416  clear_bit(i, pages);
1417  }
1418 
1419  /* check the ranges_count after the user request */
1420  bitmap_to_ranges(pages, bank->num_sectors, ranges, &ranges_count);
1421 
1422  /* pretty-print the requested areas for protection */
1423  if (ranges_count > 0) {
1424  char *ranges_str = range_print_alloc(ranges, ranges_count);
1425  LOG_DEBUG("requested areas for protection: %s", ranges_str);
1426  free(ranges_str);
1427  } else
1428  LOG_DEBUG("requested areas for protection: none");
1429 
1430  if (ranges_count > n_wrp) {
1431  LOG_ERROR("cannot set the requested protection "
1432  "(only %u write protection areas are available)" , n_wrp);
1433  return ERROR_FAIL;
1434  }
1435 
1436  /* re-init all WRPxy as disabled (first > last)*/
1437  for (i = 0; i < n_wrp; i++) {
1438  wrpxy[i].first = wrpxy[i].offset + 1;
1439  wrpxy[i].last = wrpxy[i].offset;
1440  }
1441 
1442  /* then configure WRPxy areas */
1443  for (i = 0; i < ranges_count; i++) {
1444  wrpxy[i].first = ranges[i].start;
1445  wrpxy[i].last = ranges[i].end;
1446  }
1447 
1448  /* finally write WRPxy registers */
1449  return stm32l4_write_all_wrpxy(bank, wrpxy, n_wrp);
1450 }
1451 
1452 static int stm32l4_protect(struct flash_bank *bank, int set, unsigned int first, unsigned int last)
1453 {
1454  struct target *target = bank->target;
1455  struct stm32l4_flash_bank *stm32l4_info = bank->driver_priv;
1456 
1457  if (stm32l4_is_otp(bank)) {
1458  LOG_ERROR("cannot protect/unprotect OTP memory");
1460  }
1461 
1462  if (target->state != TARGET_HALTED) {
1463  LOG_ERROR("Target not halted");
1464  return ERROR_TARGET_NOT_HALTED;
1465  }
1466 
1467  /* refresh the sectors' protection */
1468  int ret = stm32l4_protect_check(bank);
1469  if (ret != ERROR_OK)
1470  return ret;
1471 
1472  /* the requested sectors could be located into bank1 and/or bank2 */
1473  if (last < stm32l4_info->bank1_sectors) {
1474  return stm32l4_protect_same_bank(bank, STM32_BANK1, set, first, last);
1475  } else if (first >= stm32l4_info->bank1_sectors) {
1476  return stm32l4_protect_same_bank(bank, STM32_BANK2, set, first, last);
1477  } else {
1478  ret = stm32l4_protect_same_bank(bank, STM32_BANK1, set, first, stm32l4_info->bank1_sectors - 1);
1479  if (ret != ERROR_OK)
1480  return ret;
1481 
1482  return stm32l4_protect_same_bank(bank, STM32_BANK2, set, stm32l4_info->bank1_sectors, last);
1483  }
1484 }
1485 
1486 /* count is the size divided by stm32l4_info->data_width */
1487 static int stm32l4_write_block(struct flash_bank *bank, const uint8_t *buffer,
1488  uint32_t offset, uint32_t count)
1489 {
1490  struct target *target = bank->target;
1491  struct stm32l4_flash_bank *stm32l4_info = bank->driver_priv;
1492  struct working_area *write_algorithm;
1493  struct working_area *source;
1494  uint32_t address = bank->base + offset;
1495  struct reg_param reg_params[5];
1496  struct armv7m_algorithm armv7m_info;
1497  int retval = ERROR_OK;
1498 
1499  static const uint8_t stm32l4_flash_write_code[] = {
1500 #include "../../../contrib/loaders/flash/stm32/stm32l4x.inc"
1501  };
1502 
1503  if (target_alloc_working_area(target, sizeof(stm32l4_flash_write_code),
1504  &write_algorithm) != ERROR_OK) {
1505  LOG_WARNING("no working area available, can't do block memory writes");
1507  }
1508 
1509  retval = target_write_buffer(target, write_algorithm->address,
1510  sizeof(stm32l4_flash_write_code),
1511  stm32l4_flash_write_code);
1512  if (retval != ERROR_OK) {
1513  target_free_working_area(target, write_algorithm);
1514  return retval;
1515  }
1516 
1517  /* data_width should be multiple of double-word */
1518  assert(stm32l4_info->data_width % 8 == 0);
1519  const size_t extra_size = sizeof(struct stm32l4_work_area);
1520  uint32_t buffer_size = target_get_working_area_avail(target) - extra_size;
1521  /* buffer_size should be multiple of stm32l4_info->data_width */
1522  buffer_size &= ~(stm32l4_info->data_width - 1);
1523 
1524  if (buffer_size < 256) {
1525  LOG_WARNING("large enough working area not available, can't do block memory writes");
1526  target_free_working_area(target, write_algorithm);
1528  } else if (buffer_size > 16384) {
1529  /* probably won't benefit from more than 16k ... */
1530  buffer_size = 16384;
1531  }
1532 
1533  if (target_alloc_working_area_try(target, buffer_size + extra_size, &source) != ERROR_OK) {
1534  LOG_ERROR("allocating working area failed");
1536  }
1537 
1538  armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
1539  armv7m_info.core_mode = ARM_MODE_THREAD;
1540 
1541  /* contrib/loaders/flash/stm32/stm32l4x.c:write() arguments */
1542  init_reg_param(&reg_params[0], "r0", 32, PARAM_IN_OUT); /* stm32l4_work_area ptr , status (out) */
1543  init_reg_param(&reg_params[1], "r1", 32, PARAM_OUT); /* buffer end */
1544  init_reg_param(&reg_params[2], "r2", 32, PARAM_OUT); /* target address */
1545  init_reg_param(&reg_params[3], "r3", 32, PARAM_OUT); /* count (of stm32l4_info->data_width) */
1546 
1547  buf_set_u32(reg_params[0].value, 0, 32, source->address);
1548  buf_set_u32(reg_params[1].value, 0, 32, source->address + source->size);
1549  buf_set_u32(reg_params[2].value, 0, 32, address);
1550  buf_set_u32(reg_params[3].value, 0, 32, count);
1551 
1552  /* write algo stack pointer */
1553  init_reg_param(&reg_params[4], "sp", 32, PARAM_OUT);
1554  buf_set_u32(reg_params[4].value, 0, 32, source->address +
1555  offsetof(struct stm32l4_work_area, stack) + LDR_STACK_SIZE);
1556 
1557  struct stm32l4_loader_params loader_extra_params;
1558 
1559  target_buffer_set_u32(target, (uint8_t *) &loader_extra_params.flash_sr_addr,
1561  target_buffer_set_u32(target, (uint8_t *) &loader_extra_params.flash_cr_addr,
1563  target_buffer_set_u32(target, (uint8_t *) &loader_extra_params.flash_word_size,
1564  stm32l4_info->data_width);
1565  target_buffer_set_u32(target, (uint8_t *) &loader_extra_params.flash_sr_bsy_mask,
1566  stm32l4_info->sr_bsy_mask);
1567 
1568  retval = target_write_buffer(target, source->address, sizeof(loader_extra_params),
1569  (uint8_t *) &loader_extra_params);
1570  if (retval != ERROR_OK)
1571  return retval;
1572 
1574  0, NULL,
1575  ARRAY_SIZE(reg_params), reg_params,
1576  source->address + offsetof(struct stm32l4_work_area, fifo),
1577  source->size - offsetof(struct stm32l4_work_area, fifo),
1578  write_algorithm->address, 0,
1579  &armv7m_info);
1580 
1581  if (retval == ERROR_FLASH_OPERATION_FAILED) {
1582  LOG_ERROR("error executing stm32l4 flash write algorithm");
1583 
1584  uint32_t error;
1586  error &= FLASH_ERROR;
1587 
1588  if (error & FLASH_WRPERR)
1589  LOG_ERROR("flash memory write protected");
1590 
1591  if (error != 0) {
1592  LOG_ERROR("flash write failed = %08" PRIx32, error);
1593  /* Clear but report errors */
1595  retval = ERROR_FAIL;
1596  }
1597  }
1598 
1600  target_free_working_area(target, write_algorithm);
1601 
1602  destroy_reg_param(&reg_params[0]);
1603  destroy_reg_param(&reg_params[1]);
1604  destroy_reg_param(&reg_params[2]);
1605  destroy_reg_param(&reg_params[3]);
1606  destroy_reg_param(&reg_params[4]);
1607 
1608  return retval;
1609 }
1610 
1611 /* count is the size divided by stm32l4_info->data_width */
1612 static int stm32l4_write_block_without_loader(struct flash_bank *bank, const uint8_t *buffer,
1613  uint32_t offset, uint32_t count)
1614 {
1615  struct stm32l4_flash_bank *stm32l4_info = bank->driver_priv;
1616  struct target *target = bank->target;
1617  uint32_t address = bank->base + offset;
1618  int retval = ERROR_OK;
1619 
1620  /* wait for BSY bit */
1622  if (retval != ERROR_OK)
1623  return retval;
1624 
1625  /* set PG in FLASH_CR */
1627  if (retval != ERROR_OK)
1628  return retval;
1629 
1630 
1631  /* write directly to flash memory */
1632  const uint8_t *src = buffer;
1633  const uint32_t data_width_in_words = stm32l4_info->data_width / 4;
1634  while (count--) {
1635  retval = target_write_memory(target, address, 4, data_width_in_words, src);
1636  if (retval != ERROR_OK)
1637  return retval;
1638 
1639  /* wait for BSY bit */
1641  if (retval != ERROR_OK)
1642  return retval;
1643 
1644  src += stm32l4_info->data_width;
1645  address += stm32l4_info->data_width;
1646  }
1647 
1648  /* reset PG in FLASH_CR */
1650  if (retval != ERROR_OK)
1651  return retval;
1652 
1653  return retval;
1654 }
1655 
1656 static int stm32l4_write(struct flash_bank *bank, const uint8_t *buffer,
1657  uint32_t offset, uint32_t count)
1658 {
1659  struct stm32l4_flash_bank *stm32l4_info = bank->driver_priv;
1660  int retval = ERROR_OK, retval2;
1661 
1663  LOG_ERROR("OTP memory is disabled for write commands");
1664  return ERROR_FAIL;
1665  }
1666 
1667  if (bank->target->state != TARGET_HALTED) {
1668  LOG_ERROR("Target not halted");
1669  return ERROR_TARGET_NOT_HALTED;
1670  }
1671 
1672  /* ensure that stm32l4_info->data_width is 'at least' a multiple of dword */
1673  assert(stm32l4_info->data_width % 8 == 0);
1674 
1675  /* The flash write must be aligned to the 'stm32l4_info->data_width' boundary.
1676  * The flash infrastructure ensures it, do just a security check */
1677  assert(offset % stm32l4_info->data_width == 0);
1678  assert(count % stm32l4_info->data_width == 0);
1679 
1680  /* STM32G4xxx Cat. 3 devices may have gaps between banks, check whether
1681  * data to be written does not go into a gap:
1682  * suppose buffer is fully contained in bank from sector 0 to sector
1683  * num->sectors - 1 and sectors are ordered according to offset
1684  */
1685  struct flash_sector *head = &bank->sectors[0];
1686  struct flash_sector *tail = &bank->sectors[bank->num_sectors - 1];
1687 
1688  while ((head < tail) && (offset >= (head + 1)->offset)) {
1689  /* buffer does not intersect head nor gap behind head */
1690  head++;
1691  }
1692 
1693  while ((head < tail) && (offset + count <= (tail - 1)->offset + (tail - 1)->size)) {
1694  /* buffer does not intersect tail nor gap before tail */
1695  --tail;
1696  }
1697 
1698  LOG_DEBUG("data: 0x%08" PRIx32 " - 0x%08" PRIx32 ", sectors: 0x%08" PRIx32 " - 0x%08" PRIx32,
1699  offset, offset + count - 1, head->offset, tail->offset + tail->size - 1);
1700 
1701  /* Now check that there is no gap from head to tail, this should work
1702  * even for multiple or non-symmetric gaps
1703  */
1704  while (head < tail) {
1705  if (head->offset + head->size != (head + 1)->offset) {
1706  LOG_ERROR("write into gap from " TARGET_ADDR_FMT " to " TARGET_ADDR_FMT,
1707  bank->base + head->offset + head->size,
1708  bank->base + (head + 1)->offset - 1);
1709  retval = ERROR_FLASH_DST_OUT_OF_BANK;
1710  }
1711  head++;
1712  }
1713 
1714  if (retval != ERROR_OK)
1715  return retval;
1716 
1717  if (stm32l4_info->tzen && (stm32l4_info->rdp == RDP_LEVEL_0)) {
1718  /* set all FLASH pages as secure */
1720  if (retval != ERROR_OK) {
1721  /* restore all FLASH pages as non-secure */
1722  stm32l4_set_secbb(bank, FLASH_SECBB_NON_SECURE); /* ignore the return value */
1723  return retval;
1724  }
1725  }
1726 
1727  retval = stm32l4_unlock_reg(bank);
1728  if (retval != ERROR_OK)
1729  goto err_lock;
1730 
1731 
1732  /* For TrustZone enabled devices, when TZEN is set and RDP level is 0.5,
1733  * the debug is possible only in non-secure state.
1734  * Thus means the flashloader will run in non-secure mode,
1735  * and the workarea need to be in non-secure RAM */
1736  if (stm32l4_info->tzen && (stm32l4_info->rdp == RDP_LEVEL_0_5))
1737  LOG_WARNING("RDP = 0x55, the work-area should be in non-secure RAM (check SAU partitioning)");
1738 
1739  /* first try to write using the loader, for better performance */
1741  count / stm32l4_info->data_width);
1742 
1743  /* if resources are not available write without a loader */
1744  if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE) {
1745  LOG_WARNING("falling back to programming without a flash loader (slower)");
1747  count / stm32l4_info->data_width);
1748  }
1749 
1750 err_lock:
1752 
1753  if (stm32l4_info->tzen && (stm32l4_info->rdp == RDP_LEVEL_0)) {
1754  /* restore all FLASH pages as non-secure */
1756  if (retval3 != ERROR_OK)
1757  return retval3;
1758  }
1759 
1760  if (retval != ERROR_OK) {
1761  LOG_ERROR("block write failed");
1762  return retval;
1763  }
1764  return retval2;
1765 }
1766 
1767 static int stm32l4_read_idcode(struct flash_bank *bank, uint32_t *id)
1768 {
1769  int retval = ERROR_OK;
1770  struct target *target = bank->target;
1771 
1772  /* try reading possible IDCODE registers, in the following order */
1773  uint32_t dbgmcu_idcode[] = {DBGMCU_IDCODE_L4_G4, DBGMCU_IDCODE_G0, DBGMCU_IDCODE_L5};
1774 
1775  for (unsigned int i = 0; i < ARRAY_SIZE(dbgmcu_idcode); i++) {
1776  retval = target_read_u32(target, dbgmcu_idcode[i], id);
1777  if ((retval == ERROR_OK) && ((*id & 0xfff) != 0) && ((*id & 0xfff) != 0xfff))
1778  return ERROR_OK;
1779  }
1780 
1781  /* Workaround for STM32WL5x devices:
1782  * DBGMCU_IDCODE cannot be read using CPU1 (Cortex-M0+) at AP1,
1783  * to solve this read the UID64 (IEEE 64-bit unique device ID register) */
1784 
1785  struct armv7m_common *armv7m = target_to_armv7m_safe(target);
1786  if (!armv7m) {
1787  LOG_ERROR("Flash requires Cortex-M target");
1788  return ERROR_TARGET_INVALID;
1789  }
1790 
1791  /* CPU2 (Cortex-M0+) is supported only with non-hla adapters because it is on AP1.
1792  * Using HLA adapters armv7m.debug_ap is null, and checking ap_num triggers a segfault */
1794  armv7m->debug_ap && armv7m->debug_ap->ap_num == 1) {
1795  uint32_t uid64_ids;
1796 
1797  /* UID64 is contains
1798  * - Bits 63:32 : DEVNUM (unique device number, different for each individual device)
1799  * - Bits 31:08 : STID (company ID) = 0x0080E1
1800  * - Bits 07:00 : DEVID (device ID) = 0x15
1801  *
1802  * read only the fixed values {STID,DEVID} from UID64_IDS to identify the device as STM32WLx
1803  */
1804  retval = target_read_u32(target, UID64_IDS, &uid64_ids);
1805  if (retval == ERROR_OK && uid64_ids == UID64_IDS_STM32WL) {
1806  /* force the DEV_ID to DEVID_STM32WLE_WL5XX and the REV_ID to unknown */
1807  *id = DEVID_STM32WLE_WL5XX;
1808  return ERROR_OK;
1809  }
1810  }
1811 
1812  LOG_ERROR("can't get the device id");
1813  return (retval == ERROR_OK) ? ERROR_FAIL : retval;
1814 }
1815 
1816 static const char *get_stm32l4_rev_str(struct flash_bank *bank)
1817 {
1818  struct stm32l4_flash_bank *stm32l4_info = bank->driver_priv;
1819  const struct stm32l4_part_info *part_info = stm32l4_info->part_info;
1820  assert(part_info);
1821 
1822  const uint16_t rev_id = stm32l4_info->idcode >> 16;
1823  for (unsigned int i = 0; i < part_info->num_revs; i++) {
1824  if (rev_id == part_info->revs[i].rev)
1825  return part_info->revs[i].str;
1826  }
1827  return "'unknown'";
1828 }
1829 
1830 static const char *get_stm32l4_bank_type_str(struct flash_bank *bank)
1831 {
1832  struct stm32l4_flash_bank *stm32l4_info = bank->driver_priv;
1833  assert(stm32l4_info->part_info);
1834  return stm32l4_is_otp(bank) ? "OTP" :
1835  stm32l4_info->dual_bank_mode ? "Flash dual" :
1836  "Flash single";
1837 }
1838 
1839 static int stm32l4_probe(struct flash_bank *bank)
1840 {
1841  struct target *target = bank->target;
1842  struct stm32l4_flash_bank *stm32l4_info = bank->driver_priv;
1843  const struct stm32l4_part_info *part_info;
1844  uint16_t flash_size_kb = 0xffff;
1845 
1846  if (!target_was_examined(target)) {
1847  LOG_ERROR("Target not examined yet");
1849  }
1850 
1851  struct armv7m_common *armv7m = target_to_armv7m_safe(target);
1852  if (!armv7m) {
1853  LOG_ERROR("Flash requires Cortex-M target");
1854  return ERROR_TARGET_INVALID;
1855  }
1856 
1857  stm32l4_info->probed = false;
1858 
1859  /* read stm32 device id registers */
1860  int retval = stm32l4_read_idcode(bank, &stm32l4_info->idcode);
1861  if (retval != ERROR_OK)
1862  return retval;
1863 
1864  const uint32_t device_id = stm32l4_info->idcode & 0xFFF;
1865 
1866  for (unsigned int n = 0; n < ARRAY_SIZE(stm32l4_parts); n++) {
1867  if (device_id == stm32l4_parts[n].id) {
1868  stm32l4_info->part_info = &stm32l4_parts[n];
1869  break;
1870  }
1871  }
1872 
1873  if (!stm32l4_info->part_info) {
1874  LOG_WARNING("Cannot identify target as an %s family device.", device_families);
1875  return ERROR_FAIL;
1876  }
1877 
1878  part_info = stm32l4_info->part_info;
1879  const char *rev_str = get_stm32l4_rev_str(bank);
1880  const uint16_t rev_id = stm32l4_info->idcode >> 16;
1881 
1882  LOG_INFO("device idcode = 0x%08" PRIx32 " (%s - Rev %s : 0x%04x)",
1883  stm32l4_info->idcode, part_info->device_str, rev_str, rev_id);
1884 
1885  stm32l4_info->flash_regs_base = stm32l4_info->part_info->flash_regs_base;
1886  stm32l4_info->data_width = (part_info->flags & F_QUAD_WORD_PROG) ? 16 : 8;
1887  stm32l4_info->cr_bker_mask = FLASH_BKER;
1888  stm32l4_info->sr_bsy_mask = FLASH_BSY;
1889 
1890  /* Set flash write alignment boundaries.
1891  * Ask the flash infrastructure to ensure required alignment */
1892  bank->write_start_alignment = stm32l4_info->data_width;
1893  bank->write_end_alignment = stm32l4_info->data_width;
1894 
1895  /* Initialize the flash registers layout */
1896  if (part_info->flags & F_HAS_L5_FLASH_REGS)
1897  stm32l4_info->flash_regs = stm32l5_ns_flash_regs;
1898  else
1899  stm32l4_info->flash_regs = stm32l4_flash_regs;
1900 
1901  /* read flash option register */
1903  if (retval != ERROR_OK)
1904  return retval;
1905 
1907 
1908  /* for devices with TrustZone, use flash secure registers when TZEN=1 and RDP is LEVEL_0 */
1909  if (stm32l4_info->tzen && (stm32l4_info->rdp == RDP_LEVEL_0)) {
1910  if (part_info->flags & F_HAS_L5_FLASH_REGS) {
1911  stm32l4_info->flash_regs_base |= STM32L5_REGS_SEC_OFFSET;
1912  stm32l4_info->flash_regs = stm32l5_s_flash_regs;
1913  } else {
1914  LOG_ERROR("BUG: device supported incomplete");
1915  return ERROR_NOT_IMPLEMENTED;
1916  }
1917  }
1918 
1919  if (part_info->flags & F_HAS_TZ)
1920  LOG_INFO("TZEN = %d : TrustZone %s by option bytes",
1921  stm32l4_info->tzen,
1922  stm32l4_info->tzen ? "enabled" : "disabled");
1923 
1924  LOG_INFO("RDP level %s (0x%02X)",
1925  stm32l4_info->rdp == RDP_LEVEL_0 ? "0" : stm32l4_info->rdp == RDP_LEVEL_0_5 ? "0.5" : "1",
1926  stm32l4_info->rdp);
1927 
1928  if (stm32l4_is_otp(bank)) {
1929  bank->size = part_info->otp_size;
1930 
1931  LOG_INFO("OTP size is %d bytes, base address is " TARGET_ADDR_FMT, bank->size, bank->base);
1932 
1933  /* OTP memory is considered as one sector */
1934  free(bank->sectors);
1935  bank->num_sectors = 1;
1936  bank->sectors = alloc_block_array(0, part_info->otp_size, 1);
1937 
1938  if (!bank->sectors) {
1939  LOG_ERROR("failed to allocate bank sectors");
1940  return ERROR_FAIL;
1941  }
1942 
1943  stm32l4_info->probed = true;
1944  return ERROR_OK;
1945  } else if (bank->base != STM32_FLASH_BANK_BASE && bank->base != STM32_FLASH_S_BANK_BASE) {
1946  LOG_ERROR("invalid bank base address");
1947  return ERROR_FAIL;
1948  }
1949 
1950  /* get flash size from target. */
1951  retval = target_read_u16(target, part_info->fsize_addr, &flash_size_kb);
1952 
1953  /* failed reading flash size or flash size invalid (early silicon),
1954  * default to max target family */
1955  if (retval != ERROR_OK || flash_size_kb == 0xffff || flash_size_kb == 0
1956  || flash_size_kb > part_info->max_flash_size_kb) {
1957  LOG_WARNING("STM32 flash size failed, probe inaccurate - assuming %dk flash",
1958  part_info->max_flash_size_kb);
1959  flash_size_kb = part_info->max_flash_size_kb;
1960  }
1961 
1962  /* if the user sets the size manually then ignore the probed value
1963  * this allows us to work around devices that have a invalid flash size register value */
1964  if (stm32l4_info->user_bank_size) {
1965  LOG_WARNING("overriding size register by configured bank size - MAY CAUSE TROUBLE");
1966  flash_size_kb = stm32l4_info->user_bank_size / 1024;
1967  }
1968 
1969  LOG_INFO("flash size = %d KiB", flash_size_kb);
1970 
1971  /* did we assign a flash size? */
1972  assert((flash_size_kb != 0xffff) && flash_size_kb);
1973 
1974  const bool is_max_flash_size = flash_size_kb == stm32l4_info->part_info->max_flash_size_kb;
1975 
1976  stm32l4_info->bank1_sectors = 0;
1977  stm32l4_info->hole_sectors = 0;
1978 
1979  int num_pages = 0;
1980  int page_size_kb = 0;
1981 
1982  stm32l4_info->dual_bank_mode = false;
1983 
1984  switch (device_id) {
1985  case DEVID_STM32L47_L48XX:
1986  case DEVID_STM32L49_L4AXX:
1987  /* if flash size is max (1M) the device is always dual bank
1988  * STM32L47/L48xx: has variants with 512K
1989  * STM32L49/L4Axx: has variants with 512 and 256
1990  * for these variants:
1991  * if DUAL_BANK = 0 -> single bank
1992  * else -> dual bank without gap
1993  * note: the page size is invariant
1994  */
1995  page_size_kb = 2;
1996  num_pages = flash_size_kb / page_size_kb;
1997  stm32l4_info->bank1_sectors = num_pages;
1998 
1999  /* check DUAL_BANK option bit if the flash is less than 1M */
2000  if (is_max_flash_size || (stm32l4_info->optr & FLASH_L4_DUAL_BANK)) {
2001  stm32l4_info->dual_bank_mode = true;
2002  stm32l4_info->bank1_sectors = num_pages / 2;
2003  }
2004  break;
2005  case DEVID_STM32L43_L44XX:
2006  case DEVID_STM32C01XX:
2007  case DEVID_STM32C03XX:
2008  case DEVID_STM32C071XX:
2009  case DEVID_STM32G05_G06XX:
2010  case DEVID_STM32G07_G08XX:
2011  case DEVID_STM32U031XX:
2013  case DEVID_STM32L45_L46XX:
2014  case DEVID_STM32L41_L42XX:
2015  case DEVID_STM32G03_G04XX:
2016  case DEVID_STM32G43_G44XX:
2017  case DEVID_STM32G49_G4AXX:
2018  case DEVID_STM32WB1XX:
2019  /* single bank flash */
2020  page_size_kb = 2;
2021  num_pages = flash_size_kb / page_size_kb;
2022  stm32l4_info->bank1_sectors = num_pages;
2023  break;
2024  case DEVID_STM32G0B_G0CXX:
2025  /* single/dual bank depending on DUAL_BANK option bit */
2026  page_size_kb = 2;
2027  num_pages = flash_size_kb / page_size_kb;
2028  stm32l4_info->bank1_sectors = num_pages;
2029  stm32l4_info->cr_bker_mask = FLASH_BKER_G0;
2030 
2031  /* check DUAL_BANK bit */
2032  if (stm32l4_info->optr & FLASH_G0_DUAL_BANK) {
2033  stm32l4_info->sr_bsy_mask = FLASH_BSY | FLASH_BSY2;
2034  stm32l4_info->dual_bank_mode = true;
2035  stm32l4_info->bank1_sectors = num_pages / 2;
2036  }
2037  break;
2038  case DEVID_STM32G47_G48XX:
2039  /* STM32G47/8 can be single/dual bank:
2040  * if DUAL_BANK = 0 -> single bank
2041  * else -> dual bank WITH gap
2042  */
2043  page_size_kb = 4;
2044  num_pages = flash_size_kb / page_size_kb;
2045  stm32l4_info->bank1_sectors = num_pages;
2046  if (stm32l4_info->optr & FLASH_G4_DUAL_BANK) {
2047  stm32l4_info->dual_bank_mode = true;
2048  page_size_kb = 2;
2049  num_pages = flash_size_kb / page_size_kb;
2050  stm32l4_info->bank1_sectors = num_pages / 2;
2051 
2052  /* for devices with trimmed flash, there is a gap between both banks */
2053  stm32l4_info->hole_sectors =
2054  (part_info->max_flash_size_kb - flash_size_kb) / (2 * page_size_kb);
2055  }
2056  break;
2057  case DEVID_STM32L4R_L4SXX:
2058  case DEVID_STM32L4P_L4QXX:
2059  /* STM32L4R/S can be single/dual bank:
2060  * if size = 2M check DBANK bit
2061  * if size = 1M check DB1M bit
2062  * STM32L4P/Q can be single/dual bank
2063  * if size = 1M check DBANK bit
2064  * if size = 512K check DB512K bit (same as DB1M bit)
2065  */
2066  page_size_kb = 8;
2067  num_pages = flash_size_kb / page_size_kb;
2068  stm32l4_info->bank1_sectors = num_pages;
2069  if ((is_max_flash_size && (stm32l4_info->optr & FLASH_L4R_DBANK)) ||
2070  (!is_max_flash_size && (stm32l4_info->optr & FLASH_LRR_DB1M))) {
2071  stm32l4_info->dual_bank_mode = true;
2072  page_size_kb = 4;
2073  num_pages = flash_size_kb / page_size_kb;
2074  stm32l4_info->bank1_sectors = num_pages / 2;
2075  }
2076  break;
2077  case DEVID_STM32L55_L56XX:
2078  /* STM32L55/L56xx can be single/dual bank:
2079  * if size = 512K check DBANK bit
2080  * if size = 256K check DB256K bit
2081  *
2082  * default page size is 4kb, if DBANK = 1, the page size is 2kb.
2083  */
2084 
2085  page_size_kb = (stm32l4_info->optr & FLASH_L5_DBANK) ? 2 : 4;
2086  num_pages = flash_size_kb / page_size_kb;
2087  stm32l4_info->bank1_sectors = num_pages;
2088 
2089  if ((is_max_flash_size && (stm32l4_info->optr & FLASH_L5_DBANK)) ||
2090  (!is_max_flash_size && (stm32l4_info->optr & FLASH_L5_DB256))) {
2091  stm32l4_info->dual_bank_mode = true;
2092  stm32l4_info->bank1_sectors = num_pages / 2;
2093  }
2094  break;
2095  case DEVID_STM32U53_U54XX:
2096  case DEVID_STM32U57_U58XX:
2097  case DEVID_STM32U59_U5AXX:
2098  /* according to RM0456 Rev 4, Chapter 7.3.1 and 7.9.13
2099  * U53x/U54x have 512K max flash size:
2100  * 512K variants are always in DUAL BANK mode
2101  * 256K and 128K variants can be in DUAL BANK mode if FLASH_OPTR:DUALBANK is set
2102  * U57x/U58x have 2M max flash size:
2103  * 2M variants are always in DUAL BANK mode
2104  * 1M variants can be in DUAL BANK mode if FLASH_OPTR:DUALBANK is set
2105  * U59x/U5Ax have 4M max flash size:
2106  * 4M variants are always in DUAL BANK mode
2107  * 2M variants can be in DUAL BANK mode if FLASH_OPTR:DUALBANK is set
2108  * Note: flash banks are always contiguous
2109  */
2110 
2111  page_size_kb = 8;
2112  num_pages = flash_size_kb / page_size_kb;
2113  stm32l4_info->bank1_sectors = num_pages;
2114  if (is_max_flash_size || (stm32l4_info->optr & FLASH_U5_DUALBANK)) {
2115  stm32l4_info->dual_bank_mode = true;
2116  stm32l4_info->bank1_sectors = num_pages / 2;
2117  }
2118  break;
2119  case DEVID_STM32WBA5X:
2120  /* single bank flash */
2121  page_size_kb = 8;
2122  num_pages = flash_size_kb / page_size_kb;
2123  stm32l4_info->bank1_sectors = num_pages;
2124  break;
2125  case DEVID_STM32WB5XX:
2126  case DEVID_STM32WB3XX:
2127  /* single bank flash */
2128  page_size_kb = 4;
2129  num_pages = flash_size_kb / page_size_kb;
2130  stm32l4_info->bank1_sectors = num_pages;
2131  break;
2132  case DEVID_STM32WLE_WL5XX:
2133  /* single bank flash */
2134  page_size_kb = 2;
2135  num_pages = flash_size_kb / page_size_kb;
2136  stm32l4_info->bank1_sectors = num_pages;
2137 
2138  /* CPU2 (Cortex-M0+) is supported only with non-hla adapters because it is on AP1.
2139  * Using HLA adapters armv7m->debug_ap is null, and checking ap_num triggers a segfault */
2140  if (armv7m->debug_ap && armv7m->debug_ap->ap_num == 1)
2141  stm32l4_info->flash_regs = stm32wl_cpu2_flash_regs;
2142  break;
2143  default:
2144  LOG_ERROR("unsupported device");
2145  return ERROR_FAIL;
2146  }
2147 
2148  /* ensure that at least there is 1 flash sector / page */
2149  if (num_pages == 0) {
2150  if (stm32l4_info->user_bank_size)
2151  LOG_ERROR("The specified flash size is less than page size");
2152 
2153  LOG_ERROR("Flash pages count cannot be zero");
2154  return ERROR_FAIL;
2155  }
2156 
2157  LOG_INFO("flash mode : %s-bank", stm32l4_info->dual_bank_mode ? "dual" : "single");
2158 
2159  const int gap_size_kb = stm32l4_info->hole_sectors * page_size_kb;
2160 
2161  if (gap_size_kb != 0) {
2162  LOG_INFO("gap detected from 0x%08x to 0x%08x",
2163  STM32_FLASH_BANK_BASE + stm32l4_info->bank1_sectors
2164  * page_size_kb * 1024,
2165  STM32_FLASH_BANK_BASE + (stm32l4_info->bank1_sectors
2166  * page_size_kb + gap_size_kb) * 1024 - 1);
2167  }
2168 
2169  /* number of significant bits in WRPxxR differs per device,
2170  * always right adjusted, on some devices non-implemented
2171  * bits read as '0', on others as '1' ...
2172  * notably G4 Cat. 2 implement only 6 bits, contradicting the RM
2173  */
2174 
2175  /* use *max_flash_size* instead of actual size as the trimmed versions
2176  * certainly use the same number of bits
2177  */
2178  uint32_t max_pages = stm32l4_info->part_info->max_flash_size_kb / page_size_kb;
2179 
2180  /* in dual bank mode number of pages is doubled, but extra bit is bank selection */
2181  stm32l4_info->wrpxxr_mask = ((max_pages >> (stm32l4_info->dual_bank_mode ? 1 : 0)) - 1);
2182  assert((stm32l4_info->wrpxxr_mask & 0xFFFF0000) == 0);
2183  LOG_DEBUG("WRPxxR mask 0x%04" PRIx16, (uint16_t)stm32l4_info->wrpxxr_mask);
2184 
2185  free(bank->sectors);
2186 
2187  bank->size = (flash_size_kb + gap_size_kb) * 1024;
2188  bank->num_sectors = num_pages;
2189  bank->sectors = malloc(sizeof(struct flash_sector) * bank->num_sectors);
2190  if (!bank->sectors) {
2191  LOG_ERROR("failed to allocate bank sectors");
2192  return ERROR_FAIL;
2193  }
2194 
2195  for (unsigned int i = 0; i < bank->num_sectors; i++) {
2196  bank->sectors[i].offset = i * page_size_kb * 1024;
2197  /* in dual bank configuration, if there is a gap between banks
2198  * we fix up the sector offset to consider this gap */
2199  if (i >= stm32l4_info->bank1_sectors && stm32l4_info->hole_sectors)
2200  bank->sectors[i].offset += gap_size_kb * 1024;
2201  bank->sectors[i].size = page_size_kb * 1024;
2202  bank->sectors[i].is_erased = -1;
2203  bank->sectors[i].is_protected = 1;
2204  }
2205 
2206  stm32l4_info->probed = true;
2207  return ERROR_OK;
2208 }
2209 
2211 {
2212  struct stm32l4_flash_bank *stm32l4_info = bank->driver_priv;
2213  if (stm32l4_info->probed) {
2214  uint32_t optr_cur;
2215 
2216  /* save flash_regs_base */
2217  uint32_t saved_flash_regs_base = stm32l4_info->flash_regs_base;
2218 
2219  /* for devices with TrustZone, use NS flash registers to read OPTR */
2220  if (stm32l4_info->part_info->flags & F_HAS_L5_FLASH_REGS)
2221  stm32l4_info->flash_regs_base &= ~STM32L5_REGS_SEC_OFFSET;
2222 
2223  /* read flash option register and re-probe if optr value is changed */
2225 
2226  /* restore saved flash_regs_base */
2227  stm32l4_info->flash_regs_base = saved_flash_regs_base;
2228 
2229  if (retval != ERROR_OK)
2230  return retval;
2231 
2232  if (stm32l4_info->optr == optr_cur)
2233  return ERROR_OK;
2234  }
2235 
2236  return stm32l4_probe(bank);
2237 }
2238 
2240 {
2241  struct stm32l4_flash_bank *stm32l4_info = bank->driver_priv;
2242  const struct stm32l4_part_info *part_info = stm32l4_info->part_info;
2243 
2244  if (part_info) {
2245  const uint16_t rev_id = stm32l4_info->idcode >> 16;
2246  command_print_sameline(cmd, "%s - Rev %s : 0x%04x", part_info->device_str,
2247  get_stm32l4_rev_str(bank), rev_id);
2248  if (stm32l4_info->probed)
2250  } else {
2251  command_print_sameline(cmd, "Cannot identify target as an %s device", device_families);
2252  }
2253 
2254  return ERROR_OK;
2255 }
2256 
2258 {
2259  int retval, retval2;
2260  struct target *target = bank->target;
2261  struct stm32l4_flash_bank *stm32l4_info = bank->driver_priv;
2262 
2263  if (stm32l4_is_otp(bank)) {
2264  LOG_ERROR("cannot erase OTP memory");
2266  }
2267 
2268  uint32_t action = FLASH_MER1;
2269 
2270  if (stm32l4_info->part_info->flags & F_HAS_DUAL_BANK)
2271  action |= FLASH_MER2;
2272 
2273  if (target->state != TARGET_HALTED) {
2274  LOG_ERROR("Target not halted");
2275  return ERROR_TARGET_NOT_HALTED;
2276  }
2277 
2278  if (stm32l4_info->tzen && (stm32l4_info->rdp == RDP_LEVEL_0)) {
2279  /* set all FLASH pages as secure */
2281  if (retval != ERROR_OK) {
2282  /* restore all FLASH pages as non-secure */
2283  stm32l4_set_secbb(bank, FLASH_SECBB_NON_SECURE); /* ignore the return value */
2284  return retval;
2285  }
2286  }
2287 
2288  retval = stm32l4_unlock_reg(bank);
2289  if (retval != ERROR_OK)
2290  goto err_lock;
2291 
2292  /* mass erase flash memory */
2294  if (retval != ERROR_OK)
2295  goto err_lock;
2296 
2298  if (retval != ERROR_OK)
2299  goto err_lock;
2300 
2302  if (retval != ERROR_OK)
2303  goto err_lock;
2304 
2306 
2307 err_lock:
2309 
2310  if (stm32l4_info->tzen && (stm32l4_info->rdp == RDP_LEVEL_0)) {
2311  /* restore all FLASH pages as non-secure */
2313  if (retval3 != ERROR_OK)
2314  return retval3;
2315  }
2316 
2317  if (retval != ERROR_OK)
2318  return retval;
2319 
2320  return retval2;
2321 }
2322 
2323 COMMAND_HANDLER(stm32l4_handle_mass_erase_command)
2324 {
2325  if (CMD_ARGC != 1)
2327 
2328  struct flash_bank *bank;
2329  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
2330  if (retval != ERROR_OK)
2331  return retval;
2332 
2333  retval = stm32l4_mass_erase(bank);
2334  if (retval == ERROR_OK)
2335  command_print(CMD, "stm32l4x mass erase complete");
2336  else
2337  command_print(CMD, "stm32l4x mass erase failed");
2338 
2339  return retval;
2340 }
2341 
2342 COMMAND_HANDLER(stm32l4_handle_option_read_command)
2343 {
2344  if (CMD_ARGC != 2)
2346 
2347  struct flash_bank *bank;
2348  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
2349  if (retval != ERROR_OK)
2350  return retval;
2351 
2352  uint32_t reg_offset, reg_addr;
2353  uint32_t value = 0;
2354 
2355  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], reg_offset);
2356  reg_addr = stm32l4_get_flash_reg(bank, reg_offset);
2357 
2358  retval = stm32l4_read_flash_reg(bank, reg_offset, &value);
2359  if (retval != ERROR_OK)
2360  return retval;
2361 
2362  command_print(CMD, "Option Register: <0x%" PRIx32 "> = 0x%" PRIx32 "", reg_addr, value);
2363 
2364  return retval;
2365 }
2366 
2367 COMMAND_HANDLER(stm32l4_handle_option_write_command)
2368 {
2369  if (CMD_ARGC != 3 && CMD_ARGC != 4)
2371 
2372  struct flash_bank *bank;
2373  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
2374  if (retval != ERROR_OK)
2375  return retval;
2376 
2377  uint32_t reg_offset;
2378  uint32_t value = 0;
2379  uint32_t mask = 0xFFFFFFFF;
2380 
2381  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], reg_offset);
2382  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], value);
2383 
2384  if (CMD_ARGC > 3)
2386 
2387  command_print(CMD, "%s Option written.\n"
2388  "INFO: a reset or power cycle is required "
2389  "for the new settings to take effect.", bank->driver->name);
2390 
2391  retval = stm32l4_write_option(bank, reg_offset, value, mask);
2392  return retval;
2393 }
2394 
2395 COMMAND_HANDLER(stm32l4_handle_trustzone_command)
2396 {
2397  if (CMD_ARGC < 1 || CMD_ARGC > 2)
2399 
2400  struct flash_bank *bank;
2401  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
2402  if (retval != ERROR_OK)
2403  return retval;
2404 
2405  struct stm32l4_flash_bank *stm32l4_info = bank->driver_priv;
2406  if (!(stm32l4_info->part_info->flags & F_HAS_TZ)) {
2407  LOG_ERROR("This device does not have a TrustZone");
2408  return ERROR_FAIL;
2409  }
2410 
2412  if (retval != ERROR_OK)
2413  return retval;
2414 
2416 
2417  if (CMD_ARGC == 1) {
2418  /* only display the TZEN value */
2419  LOG_INFO("Global TrustZone Security is %s", stm32l4_info->tzen ? "enabled" : "disabled");
2420  return ERROR_OK;
2421  }
2422 
2423  bool new_tzen;
2424  COMMAND_PARSE_ENABLE(CMD_ARGV[1], new_tzen);
2425 
2426  if (new_tzen == stm32l4_info->tzen) {
2427  LOG_INFO("The requested TZEN is already programmed");
2428  return ERROR_OK;
2429  }
2430 
2431  if (new_tzen) {
2432  if (stm32l4_info->rdp != RDP_LEVEL_0) {
2433  LOG_ERROR("TZEN can be set only when RDP level is 0");
2434  return ERROR_FAIL;
2435  }
2438  } else {
2439  /* Deactivation of TZEN (from 1 to 0) is only possible when the RDP is
2440  * changing to level 0 (from level 1 to level 0 or from level 0.5 to level 0). */
2441  if (stm32l4_info->rdp != RDP_LEVEL_1 && stm32l4_info->rdp != RDP_LEVEL_0_5) {
2442  LOG_ERROR("Deactivation of TZEN is only possible when the RDP is changing to level 0");
2443  return ERROR_FAIL;
2444  }
2445 
2448  }
2449 
2450  if (retval != ERROR_OK)
2451  return retval;
2452 
2454 }
2455 
2456 COMMAND_HANDLER(stm32l4_handle_option_load_command)
2457 {
2458  if (CMD_ARGC != 1)
2460 
2461  struct flash_bank *bank;
2462  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
2463  if (retval != ERROR_OK)
2464  return retval;
2465 
2466  retval = stm32l4_perform_obl_launch(bank);
2467  if (retval != ERROR_OK) {
2468  command_print(CMD, "stm32l4x option load failed");
2469  return retval;
2470  }
2471 
2472 
2473  command_print(CMD, "stm32l4x option load completed. Power-on reset might be required");
2474 
2475  return ERROR_OK;
2476 }
2477 
2478 COMMAND_HANDLER(stm32l4_handle_lock_command)
2479 {
2480  struct target *target = NULL;
2481 
2482  if (CMD_ARGC != 1)
2484 
2485  struct flash_bank *bank;
2486  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
2487  if (retval != ERROR_OK)
2488  return retval;
2489 
2490  if (stm32l4_is_otp(bank)) {
2491  LOG_ERROR("cannot lock/unlock OTP memory");
2493  }
2494 
2495  target = bank->target;
2496 
2497  if (target->state != TARGET_HALTED) {
2498  LOG_ERROR("Target not halted");
2499  return ERROR_TARGET_NOT_HALTED;
2500  }
2501 
2502  /* set readout protection level 1 by erasing the RDP option byte */
2503  struct stm32l4_flash_bank *stm32l4_info = bank->driver_priv;
2506  command_print(CMD, "%s failed to lock device", bank->driver->name);
2507  return ERROR_OK;
2508  }
2509 
2510  return ERROR_OK;
2511 }
2512 
2513 COMMAND_HANDLER(stm32l4_handle_unlock_command)
2514 {
2515  struct target *target = NULL;
2516 
2517  if (CMD_ARGC != 1)
2519 
2520  struct flash_bank *bank;
2521  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
2522  if (retval != ERROR_OK)
2523  return retval;
2524 
2525  if (stm32l4_is_otp(bank)) {
2526  LOG_ERROR("cannot lock/unlock OTP memory");
2528  }
2529 
2530  target = bank->target;
2531 
2532  if (target->state != TARGET_HALTED) {
2533  LOG_ERROR("Target not halted");
2534  return ERROR_TARGET_NOT_HALTED;
2535  }
2536 
2537  struct stm32l4_flash_bank *stm32l4_info = bank->driver_priv;
2540  command_print(CMD, "%s failed to unlock device", bank->driver->name);
2541  return ERROR_OK;
2542  }
2543 
2544  return ERROR_OK;
2545 }
2546 
2547 COMMAND_HANDLER(stm32l4_handle_wrp_info_command)
2548 {
2549  if (CMD_ARGC < 1 || CMD_ARGC > 2)
2551 
2552  struct flash_bank *bank;
2553  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
2554  if (retval != ERROR_OK)
2555  return retval;
2556 
2557  if (stm32l4_is_otp(bank)) {
2558  LOG_ERROR("OTP memory does not have write protection areas");
2560  }
2561 
2562  struct stm32l4_flash_bank *stm32l4_info = bank->driver_priv;
2563  enum stm32_bank_id dev_bank_id = STM32_ALL_BANKS;
2564  if (CMD_ARGC == 2) {
2565  if (strcmp(CMD_ARGV[1], "bank1") == 0)
2566  dev_bank_id = STM32_BANK1;
2567  else if (strcmp(CMD_ARGV[1], "bank2") == 0)
2568  dev_bank_id = STM32_BANK2;
2569  else
2571  }
2572 
2573  if (dev_bank_id == STM32_BANK2) {
2574  if (!(stm32l4_info->part_info->flags & F_HAS_DUAL_BANK)) {
2575  LOG_ERROR("this device has no second bank");
2576  return ERROR_FAIL;
2577  } else if (!stm32l4_info->dual_bank_mode) {
2578  LOG_ERROR("this device is configured in single bank mode");
2579  return ERROR_FAIL;
2580  }
2581  }
2582 
2583  int ret;
2584  unsigned int n_wrp, i;
2585  struct stm32l4_wrp wrpxy[4];
2586 
2587  ret = stm32l4_get_all_wrpxy(bank, dev_bank_id, wrpxy, &n_wrp);
2588  if (ret != ERROR_OK)
2589  return ret;
2590 
2591  /* use bitmap and range helpers to better describe protected areas */
2592  DECLARE_BITMAP(pages, bank->num_sectors);
2593  bitmap_zero(pages, bank->num_sectors);
2594 
2595  for (i = 0; i < n_wrp; i++) {
2596  if (wrpxy[i].used) {
2597  for (int p = wrpxy[i].first; p <= wrpxy[i].last; p++)
2598  set_bit(p, pages);
2599  }
2600  }
2601 
2602  /* we have at most 'n_wrp' WRP areas */
2603  struct range ranges[n_wrp];
2604  unsigned int ranges_count = 0;
2605 
2606  bitmap_to_ranges(pages, bank->num_sectors, ranges, &ranges_count);
2607 
2608  if (ranges_count > 0) {
2609  /* pretty-print the protected ranges */
2610  char *ranges_str = range_print_alloc(ranges, ranges_count);
2611  command_print(CMD, "protected areas: %s", ranges_str);
2612  free(ranges_str);
2613  } else
2614  command_print(CMD, "no protected areas");
2615 
2616  return ERROR_OK;
2617 }
2618 
2619 COMMAND_HANDLER(stm32l4_handle_otp_command)
2620 {
2621  if (CMD_ARGC != 2)
2623 
2624  struct flash_bank *bank;
2625  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
2626  if (retval != ERROR_OK)
2627  return retval;
2628 
2629  if (!stm32l4_is_otp(bank)) {
2630  command_print(CMD, "the specified bank is not an OTP memory");
2631  return ERROR_FAIL;
2632  }
2633  if (strcmp(CMD_ARGV[1], "enable") == 0)
2634  stm32l4_otp_enable(bank, true);
2635  else if (strcmp(CMD_ARGV[1], "disable") == 0)
2636  stm32l4_otp_enable(bank, false);
2637  else if (strcmp(CMD_ARGV[1], "show") == 0)
2638  command_print(CMD, "OTP memory bank #%d is %s for write commands.",
2639  bank->bank_number, stm32l4_otp_is_enabled(bank) ? "enabled" : "disabled");
2640  else
2642 
2643  return ERROR_OK;
2644 }
2645 
2646 static const struct command_registration stm32l4_exec_command_handlers[] = {
2647  {
2648  .name = "lock",
2649  .handler = stm32l4_handle_lock_command,
2650  .mode = COMMAND_EXEC,
2651  .usage = "bank_id",
2652  .help = "Lock entire flash device.",
2653  },
2654  {
2655  .name = "unlock",
2656  .handler = stm32l4_handle_unlock_command,
2657  .mode = COMMAND_EXEC,
2658  .usage = "bank_id",
2659  .help = "Unlock entire protected flash device.",
2660  },
2661  {
2662  .name = "mass_erase",
2663  .handler = stm32l4_handle_mass_erase_command,
2664  .mode = COMMAND_EXEC,
2665  .usage = "bank_id",
2666  .help = "Erase entire flash device.",
2667  },
2668  {
2669  .name = "option_read",
2670  .handler = stm32l4_handle_option_read_command,
2671  .mode = COMMAND_EXEC,
2672  .usage = "bank_id reg_offset",
2673  .help = "Read & Display device option bytes.",
2674  },
2675  {
2676  .name = "option_write",
2677  .handler = stm32l4_handle_option_write_command,
2678  .mode = COMMAND_EXEC,
2679  .usage = "bank_id reg_offset value [mask]",
2680  .help = "Write device option bit fields with provided value.",
2681  },
2682  {
2683  .name = "trustzone",
2684  .handler = stm32l4_handle_trustzone_command,
2685  .mode = COMMAND_EXEC,
2686  .usage = "<bank_id> [enable|disable]",
2687  .help = "Configure TrustZone security",
2688  },
2689  {
2690  .name = "wrp_info",
2691  .handler = stm32l4_handle_wrp_info_command,
2692  .mode = COMMAND_EXEC,
2693  .usage = "bank_id [bank1|bank2]",
2694  .help = "list the protected areas using WRP",
2695  },
2696  {
2697  .name = "option_load",
2698  .handler = stm32l4_handle_option_load_command,
2699  .mode = COMMAND_EXEC,
2700  .usage = "bank_id",
2701  .help = "Force re-load of device options (will cause device reset).",
2702  },
2703  {
2704  .name = "otp",
2705  .handler = stm32l4_handle_otp_command,
2706  .mode = COMMAND_EXEC,
2707  .usage = "<bank_id> <enable|disable|show>",
2708  .help = "OTP (One Time Programmable) memory write enable/disable",
2709  },
2711 };
2712 
2713 static const struct command_registration stm32l4_command_handlers[] = {
2714  {
2715  .name = "stm32l4x",
2716  .mode = COMMAND_ANY,
2717  .help = "stm32l4x flash command group",
2718  .usage = "",
2720  },
2722 };
2723 
2724 const struct flash_driver stm32l4x_flash = {
2725  .name = "stm32l4x",
2726  .commands = stm32l4_command_handlers,
2727  .flash_bank_command = stm32l4_flash_bank_command,
2728  .erase = stm32l4_erase,
2729  .protect = stm32l4_protect,
2730  .write = stm32l4_write,
2731  .read = default_flash_read,
2732  .probe = stm32l4_probe,
2733  .auto_probe = stm32l4_auto_probe,
2734  .erase_check = default_flash_blank_check,
2735  .protect_check = stm32l4_protect_check,
2736  .info = get_stm32l4_info,
2737  .free_driver_priv = default_flash_free_driver_priv,
2738 };
void init_reg_param(struct reg_param *param, char *reg_name, uint32_t size, enum param_direction direction)
Definition: algorithm.c:29
void destroy_reg_param(struct reg_param *param)
Definition: algorithm.c:37
@ PARAM_OUT
Definition: algorithm.h:16
@ PARAM_IN_OUT
Definition: algorithm.h:17
@ ARM_MODE_THREAD
Definition: arm.h:94
This defines formats and data structures used to talk to ADIv5 entities.
static struct armv7m_common * target_to_armv7m_safe(struct target *target)
Definition: armv7m.h:274
#define ARMV7M_COMMON_MAGIC
Definition: armv7m.h:220
Support functions to access arbitrary bits in a byte array.
static void buf_set_u32(uint8_t *_buffer, unsigned int first, unsigned int num, uint32_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
Definition: binarybuffer.h:34
static int test_bit(unsigned int nr, const volatile unsigned long *addr)
test_bit - Determine whether a bit is set
Definition: bits.h:73
static void bitmap_zero(unsigned long *dst, unsigned int nbits)
bitmap_zero - Clears all the bits in memory
Definition: bits.h:36
static void set_bit(unsigned int nr, volatile unsigned long *addr)
set_bit - Set a bit in memory
Definition: bits.h:60
static void clear_bit(unsigned int nr, volatile unsigned long *addr)
clear_bit - Clear a bit in memory
Definition: bits.h:47
#define DECLARE_BITMAP(name, bits)
Definition: bits.h:29
void command_print_sameline(struct command_invocation *cmd, const char *format,...)
Definition: command.c:420
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:443
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:141
#define CALL_COMMAND_HANDLER(name, extra ...)
Use this to macro to call a command helper (or a nested handler).
Definition: command.h:118
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:156
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:402
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:151
#define COMMAND_PARSE_ENABLE(in, out)
parses an enable/disable command argument
Definition: command.h:533
#define COMMAND_PARSE_NUMBER(type, in, out)
parses the string in into out as a type, or prints a command error and passes the error code to the c...
Definition: command.h:442
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:253
#define ERROR_COMMAND_ARGUMENT_INVALID
Definition: command.h:404
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
static enum cortex_m_impl_part cortex_m_get_impl_part(struct target *target)
Definition: cortex_m.h:319
@ CORTEX_M0P_PARTNO
Definition: cortex_m.h:54
#define FLASH_PG
Definition: em357.c:44
#define KEY2
Definition: em357.c:68
#define FLASH_PER
Definition: em357.c:45
#define FLASH_BSY
Definition: em357.c:55
#define FLASH_LOCK
Definition: em357.c:50
#define FLASH_STRT
Definition: em357.c:49
#define KEY1
Definition: em357.c:67
uint8_t bank
Definition: esirisc.c:135
int mask
Definition: esirisc.c:1739
#define ERROR_FLASH_OPER_UNSUPPORTED
Definition: flash/common.h:36
#define ERROR_FLASH_OPERATION_FAILED
Definition: flash/common.h:30
#define ERROR_FLASH_DST_OUT_OF_BANK
Definition: flash/common.h:31
struct flash_sector * alloc_block_array(uint32_t offset, uint32_t size, unsigned int num_blocks)
Allocate and fill an array of sectors or protection blocks.
int default_flash_blank_check(struct flash_bank *bank)
Provides default erased-bank check handling.
int default_flash_read(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
Provides default read implementation for flash memory.
void default_flash_free_driver_priv(struct flash_bank *bank)
Deallocates bank->driver_priv.
void alive_sleep(uint64_t ms)
Definition: log.c:456
#define ERROR_NOT_IMPLEMENTED
Definition: log.h:177
#define LOG_WARNING(expr ...)
Definition: log.h:129
#define ERROR_FAIL
Definition: log.h:173
#define LOG_ERROR(expr ...)
Definition: log.h:132
#define LOG_INFO(expr ...)
Definition: log.h:126
#define LOG_DEBUG(expr ...)
Definition: log.h:109
#define ERROR_OK
Definition: log.h:167
#define FLASH_ERROR
Definition: msp432.h:72
struct rtt_control ctrl
Control block.
Definition: rtt/rtt.c:25
size_t size
Size of the control block search area.
Definition: rtt/rtt.c:30
struct rtt_source source
Definition: rtt/rtt.c:23
#define FLASH_OBL_LAUNCH
Definition: stm32f1x.c:72
#define FLASH_WRPERR
Definition: stm32f2x.c:157
#define OPTKEY2
Definition: stm32f2x.c:178
#define FLASH_MER1
Definition: stm32f2x.c:142
#define OPTKEY1
Definition: stm32f2x.c:177
stm32l4_rdp
Definition: stm32l4x.c:175
@ RDP_LEVEL_1
Definition: stm32l4x.c:178
@ RDP_LEVEL_2
Definition: stm32l4x.c:179
@ RDP_LEVEL_0_5
Definition: stm32l4x.c:177
@ RDP_LEVEL_0
Definition: stm32l4x.c:176
static const struct stm32l4_rev stm32g05_g06xx_revs[]
Definition: stm32l4x.c:310
static const struct stm32l4_rev stm32c071xx_revs[]
Definition: stm32l4x.c:306
static int stm32l4_get_all_wrpxy(struct flash_bank *bank, enum stm32_bank_id dev_bank_id, struct stm32l4_wrp *wrpxy, unsigned int *n_wrp)
Definition: stm32l4x.c:1181
static int stm32l4_protect_check(struct flash_bank *bank)
Definition: stm32l4x.c:1259
#define FLASH_ERASE_TIMEOUT
Definition: stm32l4x.c:137
static int stm32l4_get_one_wrpxy(struct flash_bank *bank, struct stm32l4_wrp *wrpxy, enum stm32l4_flash_reg_index reg_idx, int offset)
Definition: stm32l4x.c:1161
static const struct stm32l4_rev stm32l43_l44xx_revs[]
Definition: stm32l4x.c:293
static const struct stm32l4_rev stm32u53_u54xx_revs[]
Definition: stm32l4x.c:367
static const struct stm32l4_rev stm32u59_u5axx_revs[]
Definition: stm32l4x.c:376
#define F_QUAD_WORD_PROG
Definition: stm32l4x.c:154
static const struct command_registration stm32l4_exec_command_handlers[]
Definition: stm32l4x.c:2646
#define F_NONE
Definition: stm32l4x.c:142
static int stm32l4_write_flash_reg(struct flash_bank *bank, uint32_t reg_offset, uint32_t value)
Definition: stm32l4x.c:906
static const uint32_t stm32l4_flash_regs[STM32_FLASH_REG_INDEX_NUM]
Definition: stm32l4x.c:182
#define F_USE_ALL_WRPXX
Definition: stm32l4x.c:147
stm32l4_flash_reg_index
Definition: stm32l4x.c:158
@ STM32_FLASH_SR_INDEX
Definition: stm32l4x.c:162
@ STM32_FLASH_CR_WLK_INDEX
Definition: stm32l4x.c:166
@ STM32_FLASH_ACR_INDEX
Definition: stm32l4x.c:159
@ STM32_FLASH_OPTR_INDEX
Definition: stm32l4x.c:167
@ STM32_FLASH_WRP1AR_INDEX
Definition: stm32l4x.c:168
@ STM32_FLASH_CR_INDEX
Definition: stm32l4x.c:163
@ STM32_FLASH_WRP2BR_INDEX
Definition: stm32l4x.c:171
@ STM32_FLASH_OPTKEYR_INDEX
Definition: stm32l4x.c:161
@ STM32_FLASH_WRP1BR_INDEX
Definition: stm32l4x.c:169
@ STM32_FLASH_KEYR_INDEX
Definition: stm32l4x.c:160
@ STM32_FLASH_WRP2AR_INDEX
Definition: stm32l4x.c:170
@ STM32_FLASH_REG_INDEX_NUM
Definition: stm32l4x.c:172
#define F_HAS_TZ
Definition: stm32l4x.c:149
static int stm32l4_perform_obl_launch(struct flash_bank *bank)
Definition: stm32l4x.c:1076
static const struct stm32l4_part_info stm32l4_parts[]
Definition: stm32l4x.c:400
static char * range_print_alloc(struct range *ranges, unsigned int ranges_count)
Definition: stm32l4x.c:798
static int stm32l4_read_idcode(struct flash_bank *bank, uint32_t *id)
Definition: stm32l4x.c:1767
static const struct stm32l4_rev stm32g47_g48xx_revs[]
Definition: stm32l4x.c:346
static int stm32l4_read_flash_reg(struct flash_bank *bank, uint32_t reg_offset, uint32_t *value)
Definition: stm32l4x.c:894
static bool stm32l4_otp_is_enabled(struct flash_bank *bank)
Definition: stm32l4x.c:846
#define F_HAS_L5_FLASH_REGS
Definition: stm32l4x.c:151
static const char * get_stm32l4_bank_type_str(struct flash_bank *bank)
Definition: stm32l4x.c:1830
static bool stm32l4_is_otp(struct flash_bank *bank)
Definition: stm32l4x.c:821
static int stm32l4_get_flash_cr_with_lock_index(struct flash_bank *bank)
Definition: stm32l4x.c:1000
static int range_print_one(struct range *range, char *str)
Definition: stm32l4x.c:790
static const struct stm32l4_rev stm32g03_g04xx_revs[]
Definition: stm32l4x.c:330
static const struct stm32l4_rev stm32c01xx_revs[]
Definition: stm32l4x.c:298
#define F_HAS_DUAL_BANK
Definition: stm32l4x.c:144
static const char * device_families
Definition: stm32l4x.c:287
stm32_bank_id
Definition: stm32l4x.c:271
@ STM32_BANK1
Definition: stm32l4x.c:272
@ STM32_BANK2
Definition: stm32l4x.c:273
@ STM32_ALL_BANKS
Definition: stm32l4x.c:274
static const struct stm32l4_rev stm32u57_u58xx_revs[]
Definition: stm32l4x.c:371
static const struct command_registration stm32l4_command_handlers[]
Definition: stm32l4x.c:2713
static const struct stm32l4_rev stm32l45_l46xx_revs[]
Definition: stm32l4x.c:322
static int stm32l4_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
Definition: stm32l4x.c:1283
static const struct stm32l4_rev stm32l4p_l4qxx_revs[]
Definition: stm32l4x.c:355
static const struct stm32l4_rev stm32l41_l42xx_revs[]
Definition: stm32l4x.c:326
static int stm32l4_otp_enable(struct flash_bank *bank, bool enable)
Definition: stm32l4x.c:827
static const struct stm32l4_rev stm32l47_l48xx_revs[]
Definition: stm32l4x.c:289
static const struct stm32l4_rev stm32l4r_l4sxx_revs[]
Definition: stm32l4x.c:350
static int stm32l4_write_option(struct flash_bank *bank, uint32_t reg_offset, uint32_t value, uint32_t mask)
Definition: stm32l4x.c:1113
static const struct stm32l4_rev stm32l55_l56xx_revs[]
Definition: stm32l4x.c:359
static const struct stm32l4_rev stm32wb1xx_revs[]
Definition: stm32l4x.c:384
static int stm32l4_wait_status_busy(struct flash_bank *bank, int timeout)
Definition: stm32l4x.c:918
static const struct stm32l4_rev stm32wba5x_revs[]
Definition: stm32l4x.c:380
static int stm32l4_mass_erase(struct flash_bank *bank)
Definition: stm32l4x.c:2257
static const char * get_stm32l4_rev_str(struct flash_bank *bank)
Definition: stm32l4x.c:1816
static int get_stm32l4_info(struct flash_bank *bank, struct command_invocation *cmd)
Definition: stm32l4x.c:2239
COMMAND_HANDLER(stm32l4_handle_mass_erase_command)
Definition: stm32l4x.c:2323
static const struct stm32l4_rev stm32_g07_g08xx_revs[]
Definition: stm32l4x.c:314
static const struct stm32l4_rev stm32g43_g44xx_revs[]
Definition: stm32l4x.c:342
static const struct stm32l4_rev stm32c03xx_revs[]
Definition: stm32l4x.c:302
static uint32_t stm32l4_get_flash_reg(struct flash_bank *bank, uint32_t reg_offset)
Definition: stm32l4x.c:881
static const struct stm32l4_rev stm32wle_wl5xx_revs[]
Definition: stm32l4x.c:396
static int stm32l4_protect_same_bank(struct flash_bank *bank, enum stm32_bank_id bank_id, int set, unsigned int first, unsigned int last)
Definition: stm32l4x.c:1361
FLASH_BANK_COMMAND_HANDLER(stm32l4_flash_bank_command)
Definition: stm32l4x.c:740
static int stm32l4_set_secbb(struct flash_bank *bank, uint32_t value)
set all FLASH_SECBB registers to the same value
Definition: stm32l4x.c:958
static int stm32l4_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
Definition: stm32l4x.c:1656
static const struct stm32l4_rev stm32g49_g4axx_revs[]
Definition: stm32l4x.c:363
const struct flash_driver stm32l4x_flash
Definition: stm32l4x.c:2724
static int stm32l4_probe(struct flash_bank *bank)
Definition: stm32l4x.c:1839
static const uint32_t stm32l5_s_flash_regs[STM32_FLASH_REG_INDEX_NUM]
Definition: stm32l4x.c:220
static int stm32l4_write_block_without_loader(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
Definition: stm32l4x.c:1612
static int stm32l4_read_flash_reg_by_index(struct flash_bank *bank, enum stm32l4_flash_reg_index reg_index, uint32_t *value)
Definition: stm32l4x.c:899
static const struct stm32l4_rev stm32u0xx_revs[]
Definition: stm32l4x.c:338
static int stm32l4_unlock_option_reg(struct flash_bank *bank)
Definition: stm32l4x.c:1043
static const uint32_t stm32wl_cpu2_flash_regs[STM32_FLASH_REG_INDEX_NUM]
Definition: stm32l4x.c:195
static int stm32l4_unlock_reg(struct flash_bank *bank)
Definition: stm32l4x.c:1007
static int stm32l4_write_all_wrpxy(struct flash_bank *bank, struct stm32l4_wrp *wrpxy, unsigned int n_wrp)
Definition: stm32l4x.c:1246
static void stm32l4_sync_rdp_tzen(struct flash_bank *bank)
Definition: stm32l4x.c:852
static const struct stm32l4_rev stm32wb3xx_revs[]
Definition: stm32l4x.c:392
#define FLASH_WRITE_TIMEOUT
Definition: stm32l4x.c:138
static const struct stm32l4_rev stm32wb5xx_revs[]
Definition: stm32l4x.c:388
static void bitmap_to_ranges(unsigned long *bitmap, unsigned int nbits, struct range *ranges, unsigned int *ranges_count)
Definition: stm32l4x.c:769
static const uint32_t stm32l5_ns_flash_regs[STM32_FLASH_REG_INDEX_NUM]
Definition: stm32l4x.c:207
static int stm32l4_write_block(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
Definition: stm32l4x.c:1487
static int stm32l4_write_flash_reg_by_index(struct flash_bank *bank, enum stm32l4_flash_reg_index reg_index, uint32_t value)
Definition: stm32l4x.c:911
static int stm32l4_protect(struct flash_bank *bank, int set, unsigned int first, unsigned int last)
Definition: stm32l4x.c:1452
static uint32_t stm32l4_get_flash_reg_by_index(struct flash_bank *bank, enum stm32l4_flash_reg_index reg_index)
Definition: stm32l4x.c:887
static int stm32l4_write_one_wrpxy(struct flash_bank *bank, struct stm32l4_wrp *wrpxy)
Definition: stm32l4x.c:1234
static const struct stm32l4_rev stm32l49_l4axx_revs[]
Definition: stm32l4x.c:318
static int stm32l4_auto_probe(struct flash_bank *bank)
Definition: stm32l4x.c:2210
static const struct stm32l4_rev stm32g0b_g0cxx_revs[]
Definition: stm32l4x.c:334
#define DEVID_STM32G03_G04XX
Definition: stm32l4x.h:99
#define DEVID_STM32L55_L56XX
Definition: stm32l4x.h:105
#define DBGMCU_IDCODE_G0
Definition: stm32l4x.h:80
#define DEVID_STM32G0B_G0CXX
Definition: stm32l4x.h:100
#define FLASH_L5_DB256
Definition: stm32l4x.h:68
#define FLASH_SECBB_SECURE
Definition: stm32l4x.h:76
#define DBGMCU_IDCODE_L5
Definition: stm32l4x.h:82
#define FLASH_G0_DUAL_BANK
Definition: stm32l4x.h:62
#define STM32_FLASH_S_BANK_BASE
Definition: stm32l4x.h:119
#define DEVID_STM32G49_G4AXX
Definition: stm32l4x.h:106
#define DEVID_STM32WBA5X
Definition: stm32l4x.h:110
#define DEVID_STM32WB1XX
Definition: stm32l4x.h:112
#define DEVID_STM32C03XX
Definition: stm32l4x.h:91
#define FLASH_OPTSTRT
Definition: stm32l4x.h:30
#define FLASH_SECBB2(X)
Definition: stm32l4x.h:74
#define DEVID_STM32U53_U54XX
Definition: stm32l4x.h:92
#define DEVID_STM32L4R_L4SXX
Definition: stm32l4x.h:103
#define UID64_IDS
Definition: stm32l4x.h:84
#define FLASH_L5_DBANK
Definition: stm32l4x.h:67
#define FLASH_L4_DUAL_BANK
Definition: stm32l4x.h:64
#define FLASH_PAGE_SHIFT
Definition: stm32l4x.h:25
#define DEVID_STM32G47_G48XX
Definition: stm32l4x.h:102
#define FLASH_U5_DUALBANK
Definition: stm32l4x.h:69
#define STM32_FLASH_BANK_BASE
Definition: stm32l4x.h:118
#define FLASH_G4_DUAL_BANK
Definition: stm32l4x.h:63
#define DEVID_STM32L47_L48XX
Definition: stm32l4x.h:88
#define DEVID_STM32G07_G08XX
Definition: stm32l4x.h:95
#define DEVID_STM32C071XX
Definition: stm32l4x.h:111
#define DEVID_STM32WB5XX
Definition: stm32l4x.h:113
#define DEVID_STM32U59_U5AXX
Definition: stm32l4x.h:107
#define DEVID_STM32L49_L4AXX
Definition: stm32l4x.h:96
#define DEVID_STM32L43_L44XX
Definition: stm32l4x.h:89
#define FLASH_RDP_MASK
Definition: stm32l4x.h:61
#define DEVID_STM32L41_L42XX
Definition: stm32l4x.h:98
#define DEVID_STM32U073_U083XX
Definition: stm32l4x.h:109
#define DEVID_STM32WLE_WL5XX
Definition: stm32l4x.h:115
#define FLASH_L4R_DBANK
Definition: stm32l4x.h:65
#define DEVID_STM32G05_G06XX
Definition: stm32l4x.h:93
#define LDR_STACK_SIZE
Definition: stm32l4x.h:125
#define DEVID_STM32U031XX
Definition: stm32l4x.h:94
#define FLASH_OPTLOCK
Definition: stm32l4x.h:34
#define DEVID_STM32WB3XX
Definition: stm32l4x.h:114
#define FLASH_TZEN
Definition: stm32l4x.h:70
#define DEVID_STM32G43_G44XX
Definition: stm32l4x.h:101
#define UID64_IDS_STM32WL
Definition: stm32l4x.h:85
#define FLASH_BSY2
Definition: stm32l4x.h:39
#define FLASH_SECBB_NON_SECURE
Definition: stm32l4x.h:77
#define STM32L5_REGS_SEC_OFFSET
Definition: stm32l4x.h:122
#define FLASH_SECBB1(X)
Definition: stm32l4x.h:73
#define FLASH_BKER_G0
Definition: stm32l4x.h:27
#define DEVID_STM32U57_U58XX
Definition: stm32l4x.h:108
#define DBGMCU_IDCODE_L4_G4
Definition: stm32l4x.h:81
#define DEVID_STM32L4P_L4QXX
Definition: stm32l4x.h:104
#define FLASH_LRR_DB1M
Definition: stm32l4x.h:66
#define FLASH_BKER
Definition: stm32l4x.h:26
#define DEVID_STM32C01XX
Definition: stm32l4x.h:90
#define DEVID_STM32L45_L46XX
Definition: stm32l4x.h:97
#define FLASH_MER2
Definition: stm32l4x.h:28
uint64_t ap_num
ADIv5: Number of this AP (0~255) ADIv6: Base address of this AP (4k aligned) TODO: to be more coheren...
Definition: arm_adi_v5.h:261
unsigned int common_magic
Definition: armv7m.h:295
enum arm_mode core_mode
Definition: armv7m.h:297
struct adiv5_ap * debug_ap
Definition: armv7m.h:230
When run_command is called, a new instance will be created on the stack, filled with the proper value...
Definition: command.h:76
const char * name
Definition: command.h:235
Provides details of a flash bank, available either on-chip or through a major interface.
Definition: nor/core.h:75
Provides the implementation-independent structure that defines all of the callbacks required by OpenO...
Definition: nor/driver.h:39
const char * name
Gives a human-readable name of this flash driver, This field is used to select and initialize the dri...
Definition: nor/driver.h:44
Describes the geometry and status of a single flash sector within a flash bank.
Definition: nor/core.h:28
uint32_t offset
Bus offset from start of the flash chip (in bytes).
Definition: nor/core.h:30
uint32_t size
Number of bytes in this flash sector.
Definition: nor/core.h:32
unsigned int start
Definition: stm32l4x.c:765
unsigned int end
Definition: stm32l4x.c:766
unsigned int bank1_sectors
Definition: stm32l4x.c:254
uint32_t wrpxxr_mask
Definition: stm32l4x.c:261
uint32_t user_bank_size
Definition: stm32l4x.c:257
const uint32_t * flash_regs
Definition: stm32l4x.c:264
uint32_t flash_regs_base
Definition: stm32l4x.c:263
enum stm32l4_rdp rdp
Definition: stm32l4x.c:266
uint32_t idcode
Definition: stm32l4x.c:253
uint32_t cr_bker_mask
Definition: stm32l4x.c:259
uint32_t data_width
Definition: stm32l4x.c:258
const struct stm32l4_part_info * part_info
Definition: stm32l4x.c:262
uint32_t sr_bsy_mask
Definition: stm32l4x.c:260
const uint32_t otp_size
Definition: stm32l4x.c:248
const struct stm32l4_rev * revs
Definition: stm32l4x.c:241
const char * device_str
Definition: stm32l4x.c:240
const uint32_t fsize_addr
Definition: stm32l4x.c:246
const uint32_t flash_regs_base
Definition: stm32l4x.c:245
const uint32_t flags
Definition: stm32l4x.c:244
const uint32_t otp_base
Definition: stm32l4x.c:247
const size_t num_revs
Definition: stm32l4x.c:242
const uint16_t max_flash_size_kb
Definition: stm32l4x.c:243
const char * str
Definition: stm32l4x.c:235
const uint16_t rev
Definition: stm32l4x.c:234
uint8_t stack[LDR_STACK_SIZE]
Definition: stm32l4x.h:134
bool used
Definition: stm32l4x.c:280
enum stm32l4_flash_reg_index reg_idx
Definition: stm32l4x.c:278
int offset
Definition: stm32l4x.c:283
uint32_t value
Definition: stm32l4x.c:279
Definition: target.h:116
enum target_state state
Definition: target.h:157
Definition: psoc6.c:83
target_addr_t address
Definition: target.h:86
void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
Definition: target.c:352
int target_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
Definition: target.c:2342
int target_write_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
Write count items of size bytes to the memory of target at the address given.
Definition: target.c:1265
uint32_t target_get_working_area_avail(struct target *target)
Definition: target.c:2164
int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
Definition: target.c:2060
int target_write_u32(struct target *target, target_addr_t address, uint32_t value)
Definition: target.c:2641
int target_free_working_area(struct target *target, struct working_area *area)
Free a working area.
Definition: target.c:2118
int target_alloc_working_area_try(struct target *target, uint32_t size, struct working_area **area)
Definition: target.c:1966
int target_read_u16(struct target *target, target_addr_t address, uint16_t *value)
Definition: target.c:2574
int target_run_flash_async_algorithm(struct target *target, const uint8_t *buffer, uint32_t count, int block_size, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_params, uint32_t buffer_start, uint32_t buffer_size, uint32_t entry_point, uint32_t exit_point, void *arch_info)
Streams data to a circular buffer on target intended for consumption by code running asynchronously o...
Definition: target.c:930
int target_read_u32(struct target *target, target_addr_t address, uint32_t *value)
Definition: target.c:2550
#define ERROR_TARGET_NOT_HALTED
Definition: target.h:790
static bool target_was_examined(const struct target *target)
Definition: target.h:436
#define ERROR_TARGET_INVALID
Definition: target.h:787
@ TARGET_HALTED
Definition: target.h:56
#define ERROR_TARGET_NOT_EXAMINED
Definition: target.h:797
#define ERROR_TARGET_RESOURCE_NOT_AVAILABLE
Definition: target.h:794
#define ERROR_TARGET_FAILURE
Definition: target.h:791
#define TARGET_ADDR_FMT
Definition: types.h:342
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.
Definition: types.h:57
#define NULL
Definition: usb.h:16
uint8_t status[4]
Definition: vdebug.c:17
uint8_t cmd
Definition: vdebug.c:1
uint8_t offset[4]
Definition: vdebug.c:9
uint8_t count[4]
Definition: vdebug.c:22