OpenOCD
stm32lx.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2005 by Dominic Rath *
5  * Dominic.Rath@gmx.de *
6  * *
7  * Copyright (C) 2008 by Spencer Oliver *
8  * spen@spen-soft.co.uk *
9  * *
10  * Copyright (C) 2011 by Clement Burin des Roziers *
11  * clement.burin-des-roziers@hikob.com *
12  ***************************************************************************/
13 
14 #ifdef HAVE_CONFIG_H
15 #include "config.h"
16 #endif
17 
18 #include "imp.h"
19 #include <helper/binarybuffer.h>
20 #include <helper/bits.h>
21 #include <helper/align.h>
22 #include <target/algorithm.h>
23 #include <target/armv7m.h>
24 #include <target/cortex_m.h>
25 
26 /* stm32lx flash register locations */
27 
28 #define FLASH_ACR 0x00
29 #define FLASH_PECR 0x04
30 #define FLASH_PDKEYR 0x08
31 #define FLASH_PEKEYR 0x0C
32 #define FLASH_PRGKEYR 0x10
33 #define FLASH_OPTKEYR 0x14
34 #define FLASH_SR 0x18
35 #define FLASH_OBR 0x1C
36 #define FLASH_WRPR 0x20
37 
38 /* FLASH_ACR bites */
39 #define FLASH_ACR__LATENCY BIT(0)
40 #define FLASH_ACR__PRFTEN BIT(1)
41 #define FLASH_ACR__ACC64 BIT(2)
42 #define FLASH_ACR__SLEEP_PD BIT(3)
43 #define FLASH_ACR__RUN_PD BIT(4)
44 
45 /* FLASH_PECR bits */
46 #define FLASH_PECR__PELOCK BIT(0)
47 #define FLASH_PECR__PRGLOCK BIT(1)
48 #define FLASH_PECR__OPTLOCK BIT(2)
49 #define FLASH_PECR__PROG BIT(3)
50 #define FLASH_PECR__DATA BIT(4)
51 #define FLASH_PECR__FTDW BIT(8)
52 #define FLASH_PECR__ERASE BIT(9)
53 #define FLASH_PECR__FPRG BIT(10)
54 #define FLASH_PECR__EOPIE BIT(16)
55 #define FLASH_PECR__ERRIE BIT(17)
56 #define FLASH_PECR__OBL_LAUNCH BIT(18)
57 
58 /* FLASH_SR bits */
59 #define FLASH_SR__BSY BIT(0)
60 #define FLASH_SR__EOP BIT(1)
61 #define FLASH_SR__ENDHV BIT(2)
62 #define FLASH_SR__READY BIT(3)
63 #define FLASH_SR__WRPERR BIT(8)
64 #define FLASH_SR__PGAERR BIT(9)
65 #define FLASH_SR__SIZERR BIT(10)
66 #define FLASH_SR__OPTVERR BIT(11)
67 
68 /* Unlock keys */
69 #define PEKEY1 0x89ABCDEF
70 #define PEKEY2 0x02030405
71 #define PRGKEY1 0x8C9DAEBF
72 #define PRGKEY2 0x13141516
73 #define OPTKEY1 0xFBEAD9C8
74 #define OPTKEY2 0x24252627
75 
76 /* other registers */
77 #define DBGMCU_IDCODE 0xE0042000
78 #define DBGMCU_IDCODE_L0 0x40015800
79 
80 /* Constants */
81 #define FLASH_SECTOR_SIZE 4096
82 #define FLASH_BANK0_ADDRESS 0x08000000
83 
84 /* option bytes */
85 #define OPTION_BYTES_ADDRESS 0x1FF80000
86 
87 #define OPTION_BYTE_0_PR1 0x0000
88 #define OPTION_BYTE_0_PR0 0x00AA
89 
91 static int stm32lx_lock_program_memory(struct flash_bank *bank);
93 static int stm32lx_erase_sector(struct flash_bank *bank, int sector);
94 static int stm32lx_wait_until_bsy_clear(struct flash_bank *bank);
95 static int stm32lx_obl_launch(struct flash_bank *bank);
96 static int stm32lx_lock(struct flash_bank *bank);
97 static int stm32lx_unlock(struct flash_bank *bank);
98 static int stm32lx_mass_erase(struct flash_bank *bank);
100 static int stm32lx_write_option(struct flash_bank *bank, uint32_t reg_offset,
101  uint16_t value, uint16_t mask);
102 static int stm32lx_update_part_info(struct flash_bank *bank, uint16_t flash_size_in_kb);
103 
104 struct stm32lx_rev {
105  uint16_t rev;
106  const char *str;
107 };
108 
110  uint16_t id;
111  const char *device_str;
112  const struct stm32lx_rev *revs;
113  size_t num_revs;
114  unsigned int page_size;
115  unsigned int pages_per_sector;
117  uint16_t first_bank_size_kb; /* used when has_dual_banks is true */
119 
120  uint32_t flash_base; /* Flash controller registers location */
121  uint32_t fsize_base; /* Location of FSIZE register */
122 };
123 
125  bool probed;
126  uint32_t idcode;
127  uint32_t user_bank_size;
128  uint32_t flash_base;
129 
131 };
132 
133 static const struct stm32lx_rev stm32_416_revs[] = {
134  { 0x1000, "A" }, { 0x1008, "Y" }, { 0x1038, "W" }, { 0x1078, "V" },
135 };
136 static const struct stm32lx_rev stm32_417_revs[] = {
137  { 0x1000, "A" }, { 0x1008, "Z" }, { 0x1018, "Y" }, { 0x1038, "X" }
138 };
139 static const struct stm32lx_rev stm32_425_revs[] = {
140  { 0x1000, "A" }, { 0x2000, "B" }, { 0x2008, "Y" }, { 0x2018, "1, X" },
141 };
142 static const struct stm32lx_rev stm32_427_revs[] = {
143  { 0x1000, "A" }, { 0x1018, "Y" }, { 0x1038, "X" }, { 0x10f8, "V" },
144 };
145 static const struct stm32lx_rev stm32_429_revs[] = {
146  { 0x1000, "A" }, { 0x1018, "Z" },
147 };
148 static const struct stm32lx_rev stm32_436_revs[] = {
149  { 0x1000, "A" }, { 0x1008, "Z" }, { 0x1018, "Y" }, { 0x1038, "X" },
150 };
151 static const struct stm32lx_rev stm32_437_revs[] = {
152  { 0x1000, "A" },
153 };
154 static const struct stm32lx_rev stm32_447_revs[] = {
155  { 0x1000, "A" }, { 0x2000, "B" }, { 0x2008, "Z" },
156 };
157 static const struct stm32lx_rev stm32_457_revs[] = {
158  { 0x1000, "A" }, { 0x1008, "Z" },
159 };
160 
161 static const struct stm32lx_part_info stm32lx_parts[] = {
162  {
163  .id = 0x416,
164  .revs = stm32_416_revs,
165  .num_revs = ARRAY_SIZE(stm32_416_revs),
166  .device_str = "STM32L1xx (Cat.1 - Low/Medium Density)",
167  .page_size = 256,
168  .pages_per_sector = 16,
169  .max_flash_size_kb = 128,
170  .has_dual_banks = false,
171  .flash_base = 0x40023C00,
172  .fsize_base = 0x1FF8004C,
173  },
174  {
175  .id = 0x417,
176  .revs = stm32_417_revs,
177  .num_revs = ARRAY_SIZE(stm32_417_revs),
178  .device_str = "STM32L0xx (Cat. 3)",
179  .page_size = 128,
180  .pages_per_sector = 32,
181  .max_flash_size_kb = 64,
182  .has_dual_banks = false,
183  .flash_base = 0x40022000,
184  .fsize_base = 0x1FF8007C,
185  },
186  {
187  .id = 0x425,
188  .revs = stm32_425_revs,
189  .num_revs = ARRAY_SIZE(stm32_425_revs),
190  .device_str = "STM32L0xx (Cat. 2)",
191  .page_size = 128,
192  .pages_per_sector = 32,
193  .max_flash_size_kb = 32,
194  .has_dual_banks = false,
195  .flash_base = 0x40022000,
196  .fsize_base = 0x1FF8007C,
197  },
198  {
199  .id = 0x427,
200  .revs = stm32_427_revs,
201  .num_revs = ARRAY_SIZE(stm32_427_revs),
202  .device_str = "STM32L1xx (Cat.3 - Medium+ Density)",
203  .page_size = 256,
204  .pages_per_sector = 16,
205  .max_flash_size_kb = 256,
206  .has_dual_banks = false,
207  .flash_base = 0x40023C00,
208  .fsize_base = 0x1FF800CC,
209  },
210  {
211  .id = 0x429,
212  .revs = stm32_429_revs,
213  .num_revs = ARRAY_SIZE(stm32_429_revs),
214  .device_str = "STM32L1xx (Cat.2)",
215  .page_size = 256,
216  .pages_per_sector = 16,
217  .max_flash_size_kb = 128,
218  .has_dual_banks = false,
219  .flash_base = 0x40023C00,
220  .fsize_base = 0x1FF8004C,
221  },
222  {
223  .id = 0x436,
224  .revs = stm32_436_revs,
225  .num_revs = ARRAY_SIZE(stm32_436_revs),
226  .device_str = "STM32L1xx (Cat.4/Cat.3 - Medium+/High Density)",
227  .page_size = 256,
228  .pages_per_sector = 16,
229  .max_flash_size_kb = 384,
230  .first_bank_size_kb = 192,
231  .has_dual_banks = true,
232  .flash_base = 0x40023C00,
233  .fsize_base = 0x1FF800CC,
234  },
235  {
236  .id = 0x437,
237  .revs = stm32_437_revs,
238  .num_revs = ARRAY_SIZE(stm32_437_revs),
239  .device_str = "STM32L1xx (Cat.5/Cat.6)",
240  .page_size = 256,
241  .pages_per_sector = 16,
242  .max_flash_size_kb = 512,
243  .first_bank_size_kb = 0, /* determined in runtime */
244  .has_dual_banks = true,
245  .flash_base = 0x40023C00,
246  .fsize_base = 0x1FF800CC,
247  },
248  {
249  .id = 0x447,
250  .revs = stm32_447_revs,
251  .num_revs = ARRAY_SIZE(stm32_447_revs),
252  .device_str = "STM32L0xx (Cat.5)",
253  .page_size = 128,
254  .pages_per_sector = 32,
255  .max_flash_size_kb = 192,
256  .first_bank_size_kb = 0, /* determined in runtime */
257  .has_dual_banks = false, /* determined in runtime */
258  .flash_base = 0x40022000,
259  .fsize_base = 0x1FF8007C,
260  },
261  {
262  .id = 0x457,
263  .revs = stm32_457_revs,
264  .num_revs = ARRAY_SIZE(stm32_457_revs),
265  .device_str = "STM32L0xx (Cat.1)",
266  .page_size = 128,
267  .pages_per_sector = 32,
268  .max_flash_size_kb = 16,
269  .has_dual_banks = false,
270  .flash_base = 0x40022000,
271  .fsize_base = 0x1FF8007C,
272  },
273 };
274 
275 /* flash bank stm32lx <base> <size> 0 0 <target#>
276  */
277 FLASH_BANK_COMMAND_HANDLER(stm32lx_flash_bank_command)
278 {
279  struct stm32lx_flash_bank *stm32lx_info;
280  if (CMD_ARGC < 6)
282 
283  /* Create the bank structure */
284  stm32lx_info = calloc(1, sizeof(*stm32lx_info));
285 
286  /* Check allocation */
287  if (!stm32lx_info) {
288  LOG_ERROR("failed to allocate bank structure");
289  return ERROR_FAIL;
290  }
291 
292  bank->driver_priv = stm32lx_info;
293 
294  stm32lx_info->probed = false;
295  stm32lx_info->user_bank_size = bank->size;
296 
297  /* the stm32l erased value is 0x00 */
298  bank->default_padded_value = bank->erased_value = 0x00;
299 
300  return ERROR_OK;
301 }
302 
303 COMMAND_HANDLER(stm32lx_handle_mass_erase_command)
304 {
305  if (CMD_ARGC < 1)
307 
308  struct flash_bank *bank;
309  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
310  if (retval != ERROR_OK)
311  return retval;
312 
313  retval = stm32lx_mass_erase(bank);
314  if (retval == ERROR_OK)
315  command_print(CMD, "stm32lx mass erase complete");
316  else
317  command_print(CMD, "stm32lx mass erase failed");
318 
319  return retval;
320 }
321 
322 COMMAND_HANDLER(stm32lx_handle_lock_command)
323 {
324  if (CMD_ARGC < 1)
326 
327  struct flash_bank *bank;
328  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
329  if (retval != ERROR_OK)
330  return retval;
331 
332  retval = stm32lx_lock(bank);
333 
334  if (retval == ERROR_OK)
335  command_print(CMD, "STM32Lx locked, takes effect after power cycle.");
336  else
337  command_print(CMD, "STM32Lx lock failed");
338 
339  return retval;
340 }
341 
342 COMMAND_HANDLER(stm32lx_handle_unlock_command)
343 {
344  if (CMD_ARGC < 1)
346 
347  struct flash_bank *bank;
348  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
349  if (retval != ERROR_OK)
350  return retval;
351 
352  retval = stm32lx_unlock(bank);
353 
354  if (retval == ERROR_OK)
355  command_print(CMD, "STM32Lx unlocked, takes effect after power cycle.");
356  else
357  command_print(CMD, "STM32Lx unlock failed");
358 
359  return retval;
360 }
361 
362 COMMAND_HANDLER(stm32lx_handle_option_load_command)
363 {
364  if (CMD_ARGC != 1)
366 
367  struct flash_bank *bank;
368  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
369 
370  if (retval != ERROR_OK)
371  return retval;
372 
373  retval = stm32lx_obl_launch(bank);
374  if (retval != ERROR_OK) {
375  command_print(CMD, "failed to load option bytes");
376  return retval;
377  }
378 
379  return ERROR_OK;
380 }
381 
382 COMMAND_HANDLER(stm32lx_handle_option_read_command)
383 {
384  if (CMD_ARGC != 2)
386 
387  struct flash_bank *bank;
388  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
389 
390  if (retval != ERROR_OK)
391  return retval;
392 
393  uint32_t reg_offset;
394 
395  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], reg_offset);
396 
397  if (!IS_ALIGNED(reg_offset, 4)) {
398  command_print(CMD, "register offset must be word aligned");
400  }
401 
402  uint16_t value;
403  retval = target_read_u16(bank->target, OPTION_BYTES_ADDRESS + reg_offset,
404  &value);
405 
406  if (retval != ERROR_OK) {
407  command_print(CMD, "failed to read option bytes");
408  return retval;
409  }
410 
411  command_print(CMD, "0x%" PRIx16 "", value);
412 
413  return ERROR_OK;
414 }
415 
416 COMMAND_HANDLER(stm32lx_handle_option_write_command)
417 {
418  if (CMD_ARGC != 3 && CMD_ARGC != 4)
420 
421  struct flash_bank *bank;
422  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
423 
424  if (retval != ERROR_OK)
425  return retval;
426 
427  uint32_t reg_offset;
428  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], reg_offset);
429 
430  uint16_t value;
431  COMMAND_PARSE_NUMBER(u16, CMD_ARGV[2], value);
432 
433  if (!IS_ALIGNED(reg_offset, 4)) {
434  command_print(CMD, "register offset must be word aligned");
436  }
437 
438  uint16_t mask = 0xffff;
439 
440  if (CMD_ARGC > 3)
442 
443  retval = stm32lx_write_option(bank, reg_offset, value, mask);
444  if (retval != ERROR_OK) {
445  command_print(CMD, "failed to write option bytes");
446  return retval;
447  }
448 
449  return ERROR_OK;
450 }
451 
453 {
454  int retval;
455  struct target *target = bank->target;
456  struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
457 
458  uint32_t wrpr;
459 
460  /*
461  * Read the WRPR word, and check each bit (corresponding to each
462  * flash sector
463  */
464  retval = target_read_u32(target, stm32lx_info->flash_base + FLASH_WRPR,
465  &wrpr);
466  if (retval != ERROR_OK)
467  return retval;
468 
469  for (unsigned int i = 0; i < bank->num_sectors; i++) {
470  if (wrpr & (1 << i))
471  bank->sectors[i].is_protected = 1;
472  else
473  bank->sectors[i].is_protected = 0;
474  }
475  return ERROR_OK;
476 }
477 
478 static int stm32lx_erase(struct flash_bank *bank, unsigned int first,
479  unsigned int last)
480 {
481  int retval;
482 
483  /*
484  * It could be possible to do a mass erase if all sectors must be
485  * erased, but it is not implemented yet.
486  */
487 
488  if (bank->target->state != TARGET_HALTED) {
489  LOG_ERROR("Target not halted");
491  }
492 
493  /*
494  * Loop over the selected sectors and erase them
495  */
496  for (unsigned int i = first; i <= last; i++) {
497  retval = stm32lx_erase_sector(bank, i);
498  if (retval != ERROR_OK)
499  return retval;
500  bank->sectors[i].is_erased = 1;
501  }
502  return ERROR_OK;
503 }
504 
505 static int stm32lx_write_half_pages(struct flash_bank *bank, const uint8_t *buffer,
506  uint32_t offset, uint32_t count)
507 {
508  struct target *target = bank->target;
509  struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
510 
511  uint32_t hp_nb = stm32lx_info->part_info.page_size / 2;
512  uint32_t buffer_size = (16384 / hp_nb) * hp_nb; /* must be multiple of hp_nb */
513  struct working_area *write_algorithm;
514  struct working_area *source;
515  uint32_t address = bank->base + offset;
516 
517  struct reg_param reg_params[5];
518  struct armv7m_algorithm armv7m_info;
519 
520  int retval = ERROR_OK;
521 
522  static const uint8_t stm32lx_flash_write_code[] = {
523 #include "../../../contrib/loaders/flash/stm32/stm32lx.inc"
524  };
525 
526  /* Make sure we're performing a half-page aligned write. */
527  if (offset % hp_nb) {
528  LOG_ERROR("The offset must be %" PRIu32 "B-aligned but it is %" PRIi32 "B)", hp_nb, offset);
529  return ERROR_FAIL;
530  }
531  if (count % hp_nb) {
532  LOG_ERROR("The byte count must be %" PRIu32 "B-aligned but count is %" PRIu32 "B)", hp_nb, count);
533  return ERROR_FAIL;
534  }
535 
536  /* flash write code */
537  if (target_alloc_working_area(target, sizeof(stm32lx_flash_write_code),
538  &write_algorithm) != ERROR_OK) {
539  LOG_DEBUG("no working area for block memory writes");
541  }
542 
543  /* Write the flashing code */
544  retval = target_write_buffer(target,
545  write_algorithm->address,
546  sizeof(stm32lx_flash_write_code),
547  stm32lx_flash_write_code);
548  if (retval != ERROR_OK) {
549  target_free_working_area(target, write_algorithm);
550  return retval;
551  }
552 
553  /* Allocate half pages memory */
555  if (buffer_size > 1024)
556  buffer_size -= 1024;
557  else
558  buffer_size /= 2;
559 
560  if (buffer_size <= stm32lx_info->part_info.page_size) {
561  /* we already allocated the writing code, but failed to get a
562  * buffer, free the algorithm */
563  target_free_working_area(target, write_algorithm);
564 
565  LOG_WARNING("no large enough working area available, can't do block memory writes");
567  } else {
568  /* Make sure we're still asking for an integral number of half-pages */
569  buffer_size -= buffer_size % hp_nb;
570  }
571  }
572 
573  armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
574  armv7m_info.core_mode = ARM_MODE_THREAD;
575  init_reg_param(&reg_params[0], "r0", 32, PARAM_OUT);
576  init_reg_param(&reg_params[1], "r1", 32, PARAM_OUT);
577  init_reg_param(&reg_params[2], "r2", 32, PARAM_OUT);
578  init_reg_param(&reg_params[3], "r3", 32, PARAM_OUT);
579  init_reg_param(&reg_params[4], "r4", 32, PARAM_OUT);
580 
581  /* Enable half-page write */
583  if (retval != ERROR_OK) {
585  target_free_working_area(target, write_algorithm);
586 
587  destroy_reg_param(&reg_params[0]);
588  destroy_reg_param(&reg_params[1]);
589  destroy_reg_param(&reg_params[2]);
590  destroy_reg_param(&reg_params[3]);
591  destroy_reg_param(&reg_params[4]);
592  return retval;
593  }
594 
595  struct armv7m_common *armv7m = target_to_armv7m(target);
596  if (!armv7m) {
597 
598  /* something is very wrong if armv7m is NULL */
599  LOG_ERROR("unable to get armv7m target");
600  return retval;
601  }
602 
603  /* save any DEMCR flags and configure target to catch any Hard Faults */
604  uint32_t demcr_save = armv7m->demcr;
605  armv7m->demcr = VC_HARDERR;
606 
607  /* Loop while there are bytes to write */
608  while (count > 0) {
609  uint32_t this_count;
610  this_count = (count > buffer_size) ? buffer_size : count;
611 
612  /* Write the next half pages */
613  retval = target_write_buffer(target, source->address, this_count, buffer);
614  if (retval != ERROR_OK)
615  break;
616 
617  /* 4: Store useful information in the registers */
618  /* the destination address of the copy (R0) */
619  buf_set_u32(reg_params[0].value, 0, 32, address);
620  /* The source address of the copy (R1) */
621  buf_set_u32(reg_params[1].value, 0, 32, source->address);
622  /* The number of half pages to copy (R2) */
623  buf_set_u32(reg_params[2].value, 0, 32, this_count / hp_nb);
624  /* The size in byes of a half page (R3) */
625  buf_set_u32(reg_params[3].value, 0, 32, hp_nb);
626  /* The flash base address (R4) */
627  buf_set_u32(reg_params[4].value, 0, 32, stm32lx_info->flash_base);
628 
629  /* 5: Execute the bunch of code */
630  retval = target_run_algorithm(target, 0, NULL,
631  ARRAY_SIZE(reg_params), reg_params,
632  write_algorithm->address, 0, 10000, &armv7m_info);
633  if (retval != ERROR_OK)
634  break;
635 
636  /* check for Hard Fault */
637  if (armv7m->exception_number == 3)
638  break;
639 
640  /* 6: Wait while busy */
642  if (retval != ERROR_OK)
643  break;
644 
645  buffer += this_count;
646  address += this_count;
647  count -= this_count;
648  }
649 
650  /* restore previous flags */
651  armv7m->demcr = demcr_save;
652 
653  if (armv7m->exception_number == 3) {
654 
655  /* the stm32l15x devices seem to have an issue when blank.
656  * if a ram loader is executed on a blank device it will
657  * Hard Fault, this issue does not happen for a already programmed device.
658  * A related issue is described in the stm32l151xx errata (Doc ID 17721 Rev 6 - 2.1.3).
659  * The workaround of handling the Hard Fault exception does work, but makes the
660  * loader more complicated, as a compromise we manually write the pages, programming time
661  * is reduced by 50% using this slower method.
662  */
663 
664  LOG_WARNING("Couldn't use loader, falling back to page memory writes");
665 
666  while (count > 0) {
667  uint32_t this_count;
668  this_count = (count > hp_nb) ? hp_nb : count;
669 
670  /* Write the next half pages */
671  retval = target_write_buffer(target, address, this_count, buffer);
672  if (retval != ERROR_OK)
673  break;
674 
675  /* Wait while busy */
677  if (retval != ERROR_OK)
678  break;
679 
680  buffer += this_count;
681  address += this_count;
682  count -= this_count;
683  }
684  }
685 
686  if (retval == ERROR_OK)
688 
690  target_free_working_area(target, write_algorithm);
691 
692  destroy_reg_param(&reg_params[0]);
693  destroy_reg_param(&reg_params[1]);
694  destroy_reg_param(&reg_params[2]);
695  destroy_reg_param(&reg_params[3]);
696  destroy_reg_param(&reg_params[4]);
697 
698  return retval;
699 }
700 
701 static int stm32lx_write(struct flash_bank *bank, const uint8_t *buffer,
702  uint32_t offset, uint32_t count)
703 {
704  struct target *target = bank->target;
705  struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
706 
707  uint32_t hp_nb = stm32lx_info->part_info.page_size / 2;
708  uint32_t halfpages_number;
709  uint32_t bytes_remaining = 0;
710  uint32_t address = bank->base + offset;
711  uint32_t bytes_written = 0;
712  int retval, retval2;
713 
714  if (bank->target->state != TARGET_HALTED) {
715  LOG_ERROR("Target not halted");
717  }
718 
719  if (offset & 0x3) {
720  LOG_ERROR("offset 0x%" PRIx32 " breaks required 4-byte alignment", offset);
722  }
723 
725  if (retval != ERROR_OK)
726  return retval;
727 
728  /* first we need to write any unaligned head bytes up to
729  * the next 128 byte page */
730 
731  if (offset % hp_nb)
732  bytes_remaining = MIN(count, hp_nb - (offset % hp_nb));
733 
734  while (bytes_remaining > 0) {
735  uint8_t value[4] = {0xff, 0xff, 0xff, 0xff};
736 
737  /* copy remaining bytes into the write buffer */
738  uint32_t bytes_to_write = MIN(4, bytes_remaining);
739  memcpy(value, buffer + bytes_written, bytes_to_write);
740 
741  retval = target_write_buffer(target, address, 4, value);
742  if (retval != ERROR_OK)
743  goto reset_pg_and_lock;
744 
745  bytes_written += bytes_to_write;
746  bytes_remaining -= bytes_to_write;
747  address += 4;
748 
750  if (retval != ERROR_OK)
751  goto reset_pg_and_lock;
752  }
753 
754  offset += bytes_written;
755  count -= bytes_written;
756 
757  /* this should always pass this check here */
758  assert((offset % hp_nb) == 0);
759 
760  /* calculate half pages */
761  halfpages_number = count / hp_nb;
762 
763  if (halfpages_number) {
764  retval = stm32lx_write_half_pages(bank, buffer + bytes_written, offset, hp_nb * halfpages_number);
765  if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE) {
766  /* attempt slow memory writes */
767  LOG_WARNING("couldn't use block writes, falling back to single memory accesses");
768  halfpages_number = 0;
769  } else {
770  if (retval != ERROR_OK)
771  return ERROR_FAIL;
772  }
773  }
774 
775  /* write any remaining bytes */
776  uint32_t page_bytes_written = hp_nb * halfpages_number;
777  bytes_written += page_bytes_written;
778  address += page_bytes_written;
779  bytes_remaining = count - page_bytes_written;
780 
782  if (retval != ERROR_OK)
783  return retval;
784 
785  while (bytes_remaining > 0) {
786  uint8_t value[4] = {0xff, 0xff, 0xff, 0xff};
787 
788  /* copy remaining bytes into the write buffer */
789  uint32_t bytes_to_write = MIN(4, bytes_remaining);
790  memcpy(value, buffer + bytes_written, bytes_to_write);
791 
792  retval = target_write_buffer(target, address, 4, value);
793  if (retval != ERROR_OK)
794  goto reset_pg_and_lock;
795 
796  bytes_written += bytes_to_write;
797  bytes_remaining -= bytes_to_write;
798  address += 4;
799 
801  if (retval != ERROR_OK)
802  goto reset_pg_and_lock;
803  }
804 
805 reset_pg_and_lock:
807  if (retval == ERROR_OK)
808  retval = retval2;
809 
810  return retval;
811 }
812 
813 static int stm32lx_read_id_code(struct target *target, uint32_t *id)
814 {
815  struct armv7m_common *armv7m = target_to_armv7m(target);
816  int retval;
817  if (armv7m->arm.arch == ARM_ARCH_V6M)
818  retval = target_read_u32(target, DBGMCU_IDCODE_L0, id);
819  else
820  /* read stm32 device id register */
821  retval = target_read_u32(target, DBGMCU_IDCODE, id);
822  return retval;
823 }
824 
825 static int stm32lx_probe(struct flash_bank *bank)
826 {
827  struct target *target = bank->target;
828  struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
829  uint16_t flash_size_in_kb;
830  uint32_t device_id;
831  uint32_t base_address = FLASH_BANK0_ADDRESS;
832  uint32_t second_bank_base;
833  unsigned int n;
834 
835  stm32lx_info->probed = false;
836 
837  int retval = stm32lx_read_id_code(bank->target, &device_id);
838  if (retval != ERROR_OK)
839  return retval;
840 
841  stm32lx_info->idcode = device_id;
842 
843  LOG_DEBUG("device id = 0x%08" PRIx32, device_id);
844 
845  for (n = 0; n < ARRAY_SIZE(stm32lx_parts); n++) {
846  if ((device_id & 0xfff) == stm32lx_parts[n].id) {
847  stm32lx_info->part_info = stm32lx_parts[n];
848  break;
849  }
850  }
851 
852  if (n == ARRAY_SIZE(stm32lx_parts)) {
853  LOG_ERROR("Cannot identify target as an STM32 L0 or L1 family device.");
854  return ERROR_FAIL;
855  } else {
856  LOG_INFO("Device: %s", stm32lx_info->part_info.device_str);
857  }
858 
859  stm32lx_info->flash_base = stm32lx_info->part_info.flash_base;
860 
861  /* Get the flash size from target. */
862  retval = target_read_u16(target, stm32lx_info->part_info.fsize_base,
863  &flash_size_in_kb);
864 
865  /* 0x436 devices report their flash size as a 0 or 1 code indicating 384K
866  * or 256K, respectively. Please see RM0038 r8 or newer and refer to
867  * section 30.1.1. */
868  if (retval == ERROR_OK && (device_id & 0xfff) == 0x436) {
869  if (flash_size_in_kb == 0)
870  flash_size_in_kb = 384;
871  else if (flash_size_in_kb == 1)
872  flash_size_in_kb = 256;
873  }
874 
875  /* 0x429 devices only use the lowest 8 bits of the flash size register */
876  if (retval == ERROR_OK && (device_id & 0xfff) == 0x429) {
877  flash_size_in_kb &= 0xff;
878  }
879 
880  /* Failed reading flash size or flash size invalid (early silicon),
881  * default to max target family */
882  if (retval != ERROR_OK || flash_size_in_kb == 0xffff || flash_size_in_kb == 0) {
883  LOG_WARNING("STM32L flash size failed, probe inaccurate - assuming %dk flash",
884  stm32lx_info->part_info.max_flash_size_kb);
885  flash_size_in_kb = stm32lx_info->part_info.max_flash_size_kb;
886  } else if (flash_size_in_kb > stm32lx_info->part_info.max_flash_size_kb) {
887  LOG_WARNING("STM32L probed flash size assumed incorrect since FLASH_SIZE=%dk > %dk, - assuming %dk flash",
888  flash_size_in_kb, stm32lx_info->part_info.max_flash_size_kb,
889  stm32lx_info->part_info.max_flash_size_kb);
890  flash_size_in_kb = stm32lx_info->part_info.max_flash_size_kb;
891  }
892 
893  /* Overwrite default dual-bank configuration */
894  retval = stm32lx_update_part_info(bank, flash_size_in_kb);
895  if (retval != ERROR_OK)
896  return ERROR_FAIL;
897 
898  if (stm32lx_info->part_info.has_dual_banks) {
899  /* Use the configured base address to determine if this is the first or second flash bank.
900  * Verify that the base address is reasonably correct and determine the flash bank size
901  */
902  second_bank_base = base_address +
903  stm32lx_info->part_info.first_bank_size_kb * 1024;
904  if (bank->base == second_bank_base || !bank->base) {
905  /* This is the second bank */
906  base_address = second_bank_base;
907  flash_size_in_kb = flash_size_in_kb -
908  stm32lx_info->part_info.first_bank_size_kb;
909  } else if (bank->base == base_address) {
910  /* This is the first bank */
911  flash_size_in_kb = stm32lx_info->part_info.first_bank_size_kb;
912  } else {
913  LOG_WARNING("STM32L flash bank base address config is incorrect. "
914  TARGET_ADDR_FMT " but should rather be 0x%" PRIx32
915  " or 0x%" PRIx32,
916  bank->base, base_address, second_bank_base);
917  return ERROR_FAIL;
918  }
919  LOG_INFO("STM32L flash has dual banks. Bank (%u) size is %dkb, base address is 0x%" PRIx32,
920  bank->bank_number, flash_size_in_kb, base_address);
921  } else {
922  LOG_INFO("STM32L flash size is %dkb, base address is 0x%" PRIx32, flash_size_in_kb, base_address);
923  }
924 
925  /* if the user sets the size manually then ignore the probed value
926  * this allows us to work around devices that have a invalid flash size register value */
927  if (stm32lx_info->user_bank_size) {
928  flash_size_in_kb = stm32lx_info->user_bank_size / 1024;
929  LOG_INFO("ignoring flash probed value, using configured bank size: %dkbytes", flash_size_in_kb);
930  }
931 
932  /* calculate numbers of sectors (4kB per sector) */
933  unsigned int num_sectors = (flash_size_in_kb * 1024) / FLASH_SECTOR_SIZE;
934 
935  free(bank->sectors);
936 
937  bank->size = flash_size_in_kb * 1024;
938  bank->base = base_address;
939  bank->num_sectors = num_sectors;
940  bank->sectors = malloc(sizeof(struct flash_sector) * num_sectors);
941  if (!bank->sectors) {
942  LOG_ERROR("failed to allocate bank sectors");
943  return ERROR_FAIL;
944  }
945 
946  for (unsigned int i = 0; i < num_sectors; i++) {
947  bank->sectors[i].offset = i * FLASH_SECTOR_SIZE;
948  bank->sectors[i].size = FLASH_SECTOR_SIZE;
949  bank->sectors[i].is_erased = -1;
950  bank->sectors[i].is_protected = -1;
951  }
952 
953  stm32lx_info->probed = true;
954 
955  return ERROR_OK;
956 }
957 
958 static int stm32lx_auto_probe(struct flash_bank *bank)
959 {
960  struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
961 
962  if (stm32lx_info->probed)
963  return ERROR_OK;
964 
965  return stm32lx_probe(bank);
966 }
967 
968 /* This method must return a string displaying information about the bank */
970 {
971  struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
972  const struct stm32lx_part_info *info = &stm32lx_info->part_info;
973  uint16_t rev_id = stm32lx_info->idcode >> 16;
974  const char *rev_str = NULL;
975 
976  if (!stm32lx_info->probed) {
977  int retval = stm32lx_probe(bank);
978  if (retval != ERROR_OK) {
979  command_print_sameline(cmd, "Unable to find bank information.");
980  return retval;
981  }
982  }
983 
984  for (unsigned int i = 0; i < info->num_revs; i++)
985  if (rev_id == info->revs[i].rev)
986  rev_str = info->revs[i].str;
987 
988  if (rev_str) {
989  command_print_sameline(cmd, "%s - Rev: %s", info->device_str, rev_str);
990  } else {
991  command_print_sameline(cmd, "%s - Rev: unknown (0x%04x)", info->device_str, rev_id);
992  }
993 
994  return ERROR_OK;
995 }
996 
997 static const struct command_registration stm32lx_exec_command_handlers[] = {
998  {
999  .name = "mass_erase",
1000  .handler = stm32lx_handle_mass_erase_command,
1001  .mode = COMMAND_EXEC,
1002  .usage = "bank_id",
1003  .help = "Erase entire flash device. including available EEPROM",
1004  },
1005  {
1006  .name = "lock",
1007  .handler = stm32lx_handle_lock_command,
1008  .mode = COMMAND_EXEC,
1009  .usage = "bank_id",
1010  .help = "Increase the readout protection to Level 1.",
1011  },
1012  {
1013  .name = "unlock",
1014  .handler = stm32lx_handle_unlock_command,
1015  .mode = COMMAND_EXEC,
1016  .usage = "bank_id",
1017  .help = "Lower the readout protection from Level 1 to 0.",
1018  },
1019  {
1020  .name = "option_load",
1021  .handler = stm32lx_handle_option_load_command,
1022  .mode = COMMAND_EXEC,
1023  .usage = "bank_id",
1024  .help = "Force re-load of device options (will cause device reset).",
1025  },
1026  {
1027  .name = "option_read",
1028  .handler = stm32lx_handle_option_read_command,
1029  .mode = COMMAND_EXEC,
1030  .usage = "bank_id reg_offset",
1031  .help = "Read & Display device option bytes.",
1032  },
1033  {
1034  .name = "option_write",
1035  .handler = stm32lx_handle_option_write_command,
1036  .mode = COMMAND_EXEC,
1037  .usage = "bank_id reg_offset value [mask]",
1038  .help = "Write device option bit fields with provided value.",
1039  },
1041 };
1042 
1043 static const struct command_registration stm32lx_command_handlers[] = {
1044  {
1045  .name = "stm32lx",
1046  .mode = COMMAND_ANY,
1047  .help = "stm32lx flash command group",
1048  .usage = "",
1050  },
1052 };
1053 
1054 const struct flash_driver stm32lx_flash = {
1055  .name = "stm32lx",
1056  .commands = stm32lx_command_handlers,
1057  .flash_bank_command = stm32lx_flash_bank_command,
1058  .erase = stm32lx_erase,
1059  .write = stm32lx_write,
1060  .read = default_flash_read,
1061  .probe = stm32lx_probe,
1062  .auto_probe = stm32lx_auto_probe,
1063  .erase_check = default_flash_blank_check,
1064  .protect_check = stm32lx_protect_check,
1065  .info = stm32lx_get_info,
1066  .free_driver_priv = default_flash_free_driver_priv,
1067 };
1068 
1069 /* Static methods implementation */
1071 {
1072  struct target *target = bank->target;
1073  struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
1074  int retval;
1075  uint32_t reg32;
1076 
1077  /*
1078  * Unlocking the program memory is done by unlocking the PECR,
1079  * then by writing the 2 PRGKEY to the PRGKEYR register
1080  */
1081 
1082  /* check flash is not already unlocked */
1083  retval = target_read_u32(target, stm32lx_info->flash_base + FLASH_PECR,
1084  &reg32);
1085  if (retval != ERROR_OK)
1086  return retval;
1087 
1088  if ((reg32 & FLASH_PECR__PRGLOCK) == 0)
1089  return ERROR_OK;
1090 
1091  /* To unlock the PECR write the 2 PEKEY to the PEKEYR register */
1092  retval = target_write_u32(target, stm32lx_info->flash_base + FLASH_PEKEYR,
1093  PEKEY1);
1094  if (retval != ERROR_OK)
1095  return retval;
1096 
1097  retval = target_write_u32(target, stm32lx_info->flash_base + FLASH_PEKEYR,
1098  PEKEY2);
1099  if (retval != ERROR_OK)
1100  return retval;
1101 
1102  /* Make sure it worked */
1103  retval = target_read_u32(target, stm32lx_info->flash_base + FLASH_PECR,
1104  &reg32);
1105  if (retval != ERROR_OK)
1106  return retval;
1107 
1108  if (reg32 & FLASH_PECR__PELOCK) {
1109  LOG_ERROR("PELOCK is not cleared :(");
1111  }
1112 
1113  retval = target_write_u32(target, stm32lx_info->flash_base + FLASH_PRGKEYR,
1114  PRGKEY1);
1115  if (retval != ERROR_OK)
1116  return retval;
1117  retval = target_write_u32(target, stm32lx_info->flash_base + FLASH_PRGKEYR,
1118  PRGKEY2);
1119  if (retval != ERROR_OK)
1120  return retval;
1121 
1122  /* Make sure it worked */
1123  retval = target_read_u32(target, stm32lx_info->flash_base + FLASH_PECR,
1124  &reg32);
1125  if (retval != ERROR_OK)
1126  return retval;
1127 
1128  if (reg32 & FLASH_PECR__PRGLOCK) {
1129  LOG_ERROR("PRGLOCK is not cleared :(");
1131  }
1132 
1133  return ERROR_OK;
1134 }
1135 
1137 {
1138  struct target *target = bank->target;
1139  struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
1140  int retval;
1141  uint32_t reg32;
1142 
1147  if (retval != ERROR_OK)
1148  return retval;
1149 
1150  retval = target_read_u32(target, stm32lx_info->flash_base + FLASH_PECR,
1151  &reg32);
1152  if (retval != ERROR_OK)
1153  return retval;
1154 
1155  reg32 |= FLASH_PECR__FPRG;
1156  retval = target_write_u32(target, stm32lx_info->flash_base + FLASH_PECR,
1157  reg32);
1158  if (retval != ERROR_OK)
1159  return retval;
1160 
1161  retval = target_read_u32(target, stm32lx_info->flash_base + FLASH_PECR,
1162  &reg32);
1163  if (retval != ERROR_OK)
1164  return retval;
1165 
1166  reg32 |= FLASH_PECR__PROG;
1167  return target_write_u32(target, stm32lx_info->flash_base + FLASH_PECR,
1168  reg32);
1169 }
1170 
1172 {
1173  struct target *target = bank->target;
1174  struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
1175  int retval;
1176  uint32_t reg32;
1177 
1178  /* To lock the program memory, simply set the lock bit and lock PECR */
1179 
1180  retval = target_read_u32(target, stm32lx_info->flash_base + FLASH_PECR,
1181  &reg32);
1182  if (retval != ERROR_OK)
1183  return retval;
1184 
1185  reg32 |= FLASH_PECR__PRGLOCK;
1186  retval = target_write_u32(target, stm32lx_info->flash_base + FLASH_PECR,
1187  reg32);
1188  if (retval != ERROR_OK)
1189  return retval;
1190 
1191  retval = target_read_u32(target, stm32lx_info->flash_base + FLASH_PECR,
1192  &reg32);
1193  if (retval != ERROR_OK)
1194  return retval;
1195 
1196  reg32 |= FLASH_PECR__PELOCK;
1197  retval = target_write_u32(target, stm32lx_info->flash_base + FLASH_PECR,
1198  reg32);
1199  if (retval != ERROR_OK)
1200  return retval;
1201 
1202  return ERROR_OK;
1203 }
1204 
1205 static int stm32lx_erase_sector(struct flash_bank *bank, int sector)
1206 {
1207  struct target *target = bank->target;
1208  struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
1209  int retval;
1210  uint32_t reg32;
1211 
1212  /*
1213  * To erase a sector (i.e. stm32lx_info->part_info.pages_per_sector pages),
1214  * first unlock the memory, loop over the pages of this sector
1215  * and write 0x0 to its first word.
1216  */
1217 
1219  if (retval != ERROR_OK)
1220  return retval;
1221 
1222  for (int page = 0; page < (int)stm32lx_info->part_info.pages_per_sector;
1223  page++) {
1225  retval = target_write_u32(target,
1226  stm32lx_info->flash_base + FLASH_PECR, reg32);
1227  if (retval != ERROR_OK)
1228  return retval;
1229 
1231  if (retval != ERROR_OK)
1232  return retval;
1233 
1234  uint32_t addr = bank->base + bank->sectors[sector].offset + (page
1235  * stm32lx_info->part_info.page_size);
1236  retval = target_write_u32(target, addr, 0x0);
1237  if (retval != ERROR_OK)
1238  return retval;
1239 
1241  if (retval != ERROR_OK)
1242  return retval;
1243  }
1244 
1246  if (retval != ERROR_OK)
1247  return retval;
1248 
1249  return ERROR_OK;
1250 }
1251 
1252 static inline int stm32lx_get_flash_status(struct flash_bank *bank, uint32_t *status)
1253 {
1254  struct target *target = bank->target;
1255  struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
1256 
1257  return target_read_u32(target, stm32lx_info->flash_base + FLASH_SR, status);
1258 }
1259 
1261 {
1263 }
1264 
1266 {
1267  struct target *target = bank->target;
1268  struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
1269  int retval;
1270  uint32_t reg32;
1271 
1272  /*
1273  * Unlocking the options bytes is done by unlocking the PECR,
1274  * then by writing the 2 FLASH_PEKEYR to the FLASH_OPTKEYR register
1275  */
1276 
1277  /* check flash is not already unlocked */
1278  retval = target_read_u32(target, stm32lx_info->flash_base + FLASH_PECR, &reg32);
1279  if (retval != ERROR_OK)
1280  return retval;
1281 
1282  if ((reg32 & FLASH_PECR__OPTLOCK) == 0)
1283  return ERROR_OK;
1284 
1285  if ((reg32 & FLASH_PECR__PELOCK) != 0) {
1286 
1287  retval = target_write_u32(target, stm32lx_info->flash_base + FLASH_PEKEYR, PEKEY1);
1288  if (retval != ERROR_OK)
1289  return retval;
1290 
1291  retval = target_write_u32(target, stm32lx_info->flash_base + FLASH_PEKEYR, PEKEY2);
1292  if (retval != ERROR_OK)
1293  return retval;
1294  }
1295 
1296  /* To unlock the PECR write the 2 OPTKEY to the FLASH_OPTKEYR register */
1297  retval = target_write_u32(target, stm32lx_info->flash_base + FLASH_OPTKEYR, OPTKEY1);
1298  if (retval != ERROR_OK)
1299  return retval;
1300 
1301  retval = target_write_u32(target, stm32lx_info->flash_base + FLASH_OPTKEYR, OPTKEY2);
1302  if (retval != ERROR_OK)
1303  return retval;
1304 
1305  return ERROR_OK;
1306 }
1307 
1309 {
1310  struct target *target = bank->target;
1311  struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
1312  uint32_t status;
1313  int retval = ERROR_OK;
1314 
1315  /* wait for busy to clear */
1316  for (;;) {
1317  retval = stm32lx_get_flash_status(bank, &status);
1318  if (retval != ERROR_OK)
1319  return retval;
1320 
1321  LOG_DEBUG("status: 0x%" PRIx32, status);
1322  if ((status & FLASH_SR__BSY) == 0)
1323  break;
1324 
1325  if (timeout-- <= 0) {
1326  LOG_ERROR("timed out waiting for flash");
1327  return ERROR_FAIL;
1328  }
1329  alive_sleep(1);
1330  }
1331 
1332  if (status & FLASH_SR__WRPERR) {
1333  LOG_ERROR("access denied / write protected");
1334  retval = ERROR_FAIL;
1335  }
1336 
1337  if (status & FLASH_SR__PGAERR) {
1338  LOG_ERROR("invalid program address");
1339  retval = ERROR_FAIL;
1340  }
1341 
1342  /* Clear but report errors */
1343  if (status & FLASH_SR__OPTVERR) {
1344  /* If this operation fails, we ignore it and report the original retval */
1346  }
1347 
1348  return retval;
1349 }
1350 
1352 {
1353  struct target *target = bank->target;
1354  struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
1355 
1356  int retval = stm32lx_unlock_options_bytes(bank);
1357  if (retval != ERROR_OK)
1358  return retval;
1359 
1360  /* This will fail as the target gets immediately rebooted */
1361  target_write_u32(target, stm32lx_info->flash_base + FLASH_PECR,
1363 
1364  size_t tries = 10;
1365  do {
1367  retval = target_poll(target);
1368  } while (--tries > 0 &&
1369  (retval != ERROR_OK || target->state != TARGET_HALTED));
1370 
1371  return tries ? ERROR_OK : ERROR_FAIL;
1372 }
1373 
1374 static int stm32lx_write_option(struct flash_bank *bank, uint32_t reg_offset,
1375  uint16_t value, uint16_t mask)
1376 {
1377  struct target *target = bank->target;
1378 
1379  if (target->state != TARGET_HALTED) {
1380  LOG_ERROR("Target not halted");
1381  return ERROR_TARGET_NOT_HALTED;
1382  }
1383 
1384  uint32_t option_data = 0;
1385 
1386  /*
1387  * We only read the current option bytes if the mask requires it. This is
1388  * important as the option bytes on the STM32L1 series cannot be read when
1389  * the device is locked.
1390  */
1391  if (mask != 0xffff) {
1392  int retval = target_read_u32(target, OPTION_BYTES_ADDRESS + reg_offset,
1393  &option_data);
1394 
1395  if (retval != ERROR_OK)
1396  return retval;
1397  }
1398 
1399  int retval = stm32lx_unlock_options_bytes(bank);
1400  if (retval != ERROR_OK)
1401  return retval;
1402 
1403  option_data = (option_data & ~mask) | (value & mask);
1404  // Calculate the complementary value for both option bytes.
1405  option_data = (~(option_data & 0xffff)) << 16 | (option_data & 0xffff);
1406 
1407  retval = target_write_u32(target, OPTION_BYTES_ADDRESS + reg_offset,
1408  option_data);
1409  if (retval != ERROR_OK)
1410  return retval;
1411 
1412  retval = stm32lx_wait_until_bsy_clear_timeout(bank, 30000);
1413  if (retval != ERROR_OK)
1414  return retval;
1415 
1416  return ERROR_OK;
1417 }
1418 
1419 
1420 static int stm32lx_lock(struct flash_bank *bank)
1421 {
1422  /*
1423  * Set the RDP protection level to 1.
1424  * Use the mask 0xffff to avoid reading the option bytes, see comment in
1425  * stm32lx_write_option().
1426  */
1427  return stm32lx_write_option(bank, 0x00, OPTION_BYTE_0_PR1, 0xffff);
1428 }
1429 
1430 static int stm32lx_unlock(struct flash_bank *bank)
1431 {
1432  /*
1433  * Set the RDP protection level to 0.
1434  * Use the mask 0xffff to avoid reading the option bytes, see comment in
1435  * stm32lx_write_option().
1436  */
1437  return stm32lx_write_option(bank, 0x00, OPTION_BYTE_0_PR0, 0xffff);
1438 }
1439 
1441 {
1442  int retval;
1443  struct target *target = bank->target;
1444  struct stm32lx_flash_bank *stm32lx_info = NULL;
1445  uint32_t reg32;
1446 
1447  if (target->state != TARGET_HALTED) {
1448  LOG_ERROR("Target not halted");
1449  return ERROR_TARGET_NOT_HALTED;
1450  }
1451 
1452  stm32lx_info = bank->driver_priv;
1453 
1454  retval = stm32lx_lock(bank);
1455  if (retval != ERROR_OK)
1456  return retval;
1457 
1458  retval = stm32lx_obl_launch(bank);
1459  if (retval != ERROR_OK)
1460  return retval;
1461 
1462  retval = stm32lx_unlock(bank);
1463  if (retval != ERROR_OK)
1464  return retval;
1465 
1466  retval = stm32lx_obl_launch(bank);
1467  if (retval != ERROR_OK)
1468  return retval;
1469 
1470  retval = target_read_u32(target, stm32lx_info->flash_base + FLASH_PECR, &reg32);
1471  if (retval != ERROR_OK)
1472  return retval;
1473 
1474  retval = target_write_u32(target, stm32lx_info->flash_base + FLASH_PECR, reg32 | FLASH_PECR__OPTLOCK);
1475  if (retval != ERROR_OK)
1476  return retval;
1477 
1478  return ERROR_OK;
1479 }
1480 
1481 static int stm32lx_update_part_info(struct flash_bank *bank, uint16_t flash_size_in_kb)
1482 {
1483  struct stm32lx_flash_bank *stm32lx_info = bank->driver_priv;
1484 
1485  switch (stm32lx_info->part_info.id) {
1486  case 0x447: /* STM32L0xx (Cat.5) devices */
1487  if (flash_size_in_kb == 192 || flash_size_in_kb == 128) {
1488  stm32lx_info->part_info.first_bank_size_kb = flash_size_in_kb / 2;
1489  stm32lx_info->part_info.has_dual_banks = true;
1490  }
1491  break;
1492  case 0x437: /* STM32L1xx (Cat.5/Cat.6) */
1493  stm32lx_info->part_info.first_bank_size_kb = flash_size_in_kb / 2;
1494  break;
1495  }
1496 
1497  return ERROR_OK;
1498 }
void init_reg_param(struct reg_param *param, const 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:38
@ PARAM_OUT
Definition: algorithm.h:16
#define IS_ALIGNED(x, a)
Definition: align.h:22
@ ARM_ARCH_V6M
Definition: arm.h:56
@ ARM_MODE_THREAD
Definition: arm.h:94
static struct armv7m_common * target_to_armv7m(struct target *target)
Definition: armv7m.h:273
#define ARMV7M_COMMON_MAGIC
Definition: armv7m.h:229
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
void command_print_sameline(struct command_invocation *cmd, const char *format,...)
Definition: command.c:378
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:389
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:146
#define CALL_COMMAND_HANDLER(name, extra ...)
Use this to macro to call a command helper (or a nested handler).
Definition: command.h:123
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:161
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:405
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:156
#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:445
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:256
#define ERROR_COMMAND_ARGUMENT_INVALID
Definition: command.h:407
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
#define VC_HARDERR
Definition: cortex_m.h:189
uint64_t buffer
Pointer to data buffer to send over SPI.
Definition: dw-spi-helper.h:0
uint32_t buffer_size
Size of dw_spi_program::buffer.
Definition: dw-spi-helper.h:5
uint32_t address
Starting address. Sector aligned.
Definition: dw-spi-helper.h:0
uint8_t bank
Definition: esirisc.c:135
#define ERROR_FLASH_OPERATION_FAILED
Definition: flash/common.h:30
#define ERROR_FLASH_DST_BREAKS_ALIGNMENT
Definition: flash/common.h:32
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:478
#define LOG_WARNING(expr ...)
Definition: log.h:144
#define ERROR_FAIL
Definition: log.h:188
#define LOG_ERROR(expr ...)
Definition: log.h:147
#define LOG_INFO(expr ...)
Definition: log.h:141
#define LOG_DEBUG(expr ...)
Definition: log.h:124
#define ERROR_OK
Definition: log.h:182
uint8_t mask
Definition: parport.c:70
target_addr_t addr
Start address to search for the control block.
Definition: rtt/rtt.c:28
struct rtt_source source
Definition: rtt/rtt.c:23
#define FLASH_SR__OPTVERR
Definition: stm32lx.c:66
static const struct stm32lx_rev stm32_425_revs[]
Definition: stm32lx.c:139
static int stm32lx_protect_check(struct flash_bank *bank)
Definition: stm32lx.c:452
static int stm32lx_lock_program_memory(struct flash_bank *bank)
Definition: stm32lx.c:1171
#define FLASH_SR__PGAERR
Definition: stm32lx.c:64
static const struct stm32lx_rev stm32_447_revs[]
Definition: stm32lx.c:154
static const struct stm32lx_rev stm32_436_revs[]
Definition: stm32lx.c:148
static const struct stm32lx_rev stm32_437_revs[]
Definition: stm32lx.c:151
static int stm32lx_write_option(struct flash_bank *bank, uint32_t reg_offset, uint16_t value, uint16_t mask)
Definition: stm32lx.c:1374
#define DBGMCU_IDCODE_L0
Definition: stm32lx.c:78
#define OPTION_BYTES_ADDRESS
Definition: stm32lx.c:85
static int stm32lx_update_part_info(struct flash_bank *bank, uint16_t flash_size_in_kb)
Definition: stm32lx.c:1481
#define FLASH_SR
Definition: stm32lx.c:34
#define FLASH_WRPR
Definition: stm32lx.c:36
#define FLASH_PECR
Definition: stm32lx.c:29
static int stm32lx_auto_probe(struct flash_bank *bank)
Definition: stm32lx.c:958
#define OPTION_BYTE_0_PR0
Definition: stm32lx.c:88
#define FLASH_PEKEYR
Definition: stm32lx.c:31
#define PRGKEY2
Definition: stm32lx.c:72
#define FLASH_PECR__OPTLOCK
Definition: stm32lx.c:48
#define FLASH_PECR__ERASE
Definition: stm32lx.c:52
static int stm32lx_read_id_code(struct target *target, uint32_t *id)
Definition: stm32lx.c:813
#define FLASH_BANK0_ADDRESS
Definition: stm32lx.c:82
static const struct stm32lx_rev stm32_416_revs[]
Definition: stm32lx.c:133
static int stm32lx_unlock_program_memory(struct flash_bank *bank)
Definition: stm32lx.c:1070
static int stm32lx_probe(struct flash_bank *bank)
Definition: stm32lx.c:825
static const struct stm32lx_rev stm32_457_revs[]
Definition: stm32lx.c:157
static int stm32lx_erase_sector(struct flash_bank *bank, int sector)
Definition: stm32lx.c:1205
static int stm32lx_mass_erase(struct flash_bank *bank)
Definition: stm32lx.c:1440
static const struct command_registration stm32lx_exec_command_handlers[]
Definition: stm32lx.c:997
static const struct stm32lx_rev stm32_427_revs[]
Definition: stm32lx.c:142
static int stm32lx_write_half_pages(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
Definition: stm32lx.c:505
static const struct stm32lx_rev stm32_429_revs[]
Definition: stm32lx.c:145
#define FLASH_PECR__OBL_LAUNCH
Definition: stm32lx.c:56
#define FLASH_SR__WRPERR
Definition: stm32lx.c:63
#define FLASH_PECR__PROG
Definition: stm32lx.c:49
static const struct command_registration stm32lx_command_handlers[]
Definition: stm32lx.c:1043
#define OPTKEY2
Definition: stm32lx.c:74
#define FLASH_PRGKEYR
Definition: stm32lx.c:32
FLASH_BANK_COMMAND_HANDLER(stm32lx_flash_bank_command)
Definition: stm32lx.c:277
static int stm32lx_wait_until_bsy_clear(struct flash_bank *bank)
Definition: stm32lx.c:1260
static const struct stm32lx_rev stm32_417_revs[]
Definition: stm32lx.c:136
static int stm32lx_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
Definition: stm32lx.c:478
static const struct stm32lx_part_info stm32lx_parts[]
Definition: stm32lx.c:161
static int stm32lx_unlock(struct flash_bank *bank)
Definition: stm32lx.c:1430
#define FLASH_PECR__PELOCK
Definition: stm32lx.c:46
static int stm32lx_get_flash_status(struct flash_bank *bank, uint32_t *status)
Definition: stm32lx.c:1252
#define PEKEY2
Definition: stm32lx.c:70
#define FLASH_PECR__FPRG
Definition: stm32lx.c:53
#define OPTION_BYTE_0_PR1
Definition: stm32lx.c:87
#define PRGKEY1
Definition: stm32lx.c:71
#define FLASH_OPTKEYR
Definition: stm32lx.c:33
#define DBGMCU_IDCODE
Definition: stm32lx.c:77
const struct flash_driver stm32lx_flash
Definition: stm32lx.c:1054
static int stm32lx_obl_launch(struct flash_bank *bank)
Definition: stm32lx.c:1351
#define PEKEY1
Definition: stm32lx.c:69
COMMAND_HANDLER(stm32lx_handle_mass_erase_command)
Definition: stm32lx.c:303
#define OPTKEY1
Definition: stm32lx.c:73
#define FLASH_SR__BSY
Definition: stm32lx.c:59
static int stm32lx_unlock_options_bytes(struct flash_bank *bank)
Definition: stm32lx.c:1265
static int stm32lx_wait_until_bsy_clear_timeout(struct flash_bank *bank, int timeout)
Definition: stm32lx.c:1308
#define FLASH_SECTOR_SIZE
Definition: stm32lx.c:81
static int stm32lx_enable_write_half_page(struct flash_bank *bank)
Definition: stm32lx.c:1136
static int stm32lx_lock(struct flash_bank *bank)
Definition: stm32lx.c:1420
#define FLASH_PECR__PRGLOCK
Definition: stm32lx.c:47
static int stm32lx_get_info(struct flash_bank *bank, struct command_invocation *cmd)
Definition: stm32lx.c:969
static int stm32lx_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
Definition: stm32lx.c:701
enum arm_arch arch
ARM architecture version.
Definition: arm.h:203
unsigned int common_magic
Definition: armv7m.h:306
enum arm_mode core_mode
Definition: armv7m.h:308
int exception_number
Definition: armv7m.h:236
struct arm arm
Definition: armv7m.h:234
uint32_t demcr
Definition: armv7m.h:242
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:239
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 idcode
Definition: stm32lx.c:126
uint32_t flash_base
Definition: stm32lx.c:128
uint32_t user_bank_size
Definition: stm32lx.c:127
struct stm32lx_part_info part_info
Definition: stm32lx.c:130
const char * device_str
Definition: stm32lx.c:111
uint32_t fsize_base
Definition: stm32lx.c:121
uint16_t first_bank_size_kb
Definition: stm32lx.c:117
unsigned int pages_per_sector
Definition: stm32lx.c:115
const struct stm32lx_rev * revs
Definition: stm32lx.c:112
unsigned int page_size
Definition: stm32lx.c:114
uint16_t max_flash_size_kb
Definition: stm32lx.c:116
bool has_dual_banks
Definition: stm32lx.c:118
size_t num_revs
Definition: stm32lx.c:113
uint16_t id
Definition: stm32lx.c:110
uint32_t flash_base
Definition: stm32lx.c:120
const char * str
Definition: stm32lx.c:106
uint16_t rev
Definition: stm32lx.c:105
Definition: target.h:119
enum target_state state
Definition: target.h:167
Definition: psoc6.c:83
target_addr_t address
Definition: target.h:89
int target_halt(struct target *target)
Definition: target.c:518
int target_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
Definition: target.c:2423
int target_run_algorithm(struct target *target, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_param, target_addr_t entry_point, target_addr_t exit_point, unsigned int timeout_ms, void *arch_info)
Downloads a target-specific native code algorithm to the target, and executes it.
Definition: target.c:787
int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
Definition: target.c:2131
int target_write_u32(struct target *target, target_addr_t address, uint32_t value)
Definition: target.c:2689
int target_poll(struct target *target)
Definition: target.c:488
int target_free_working_area(struct target *target, struct working_area *area)
Free a working area.
Definition: target.c:2187
int target_alloc_working_area_try(struct target *target, uint32_t size, struct working_area **area)
Definition: target.c:2031
int target_read_u16(struct target *target, target_addr_t address, uint16_t *value)
Definition: target.c:2635
int target_read_u32(struct target *target, target_addr_t address, uint32_t *value)
Definition: target.c:2615
#define ERROR_TARGET_NOT_HALTED
Definition: target.h:818
@ TARGET_HALTED
Definition: target.h:58
#define ERROR_TARGET_RESOURCE_NOT_AVAILABLE
Definition: target.h:822
#define TARGET_ADDR_FMT
Definition: types.h:286
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.
Definition: types.h:57
static struct ublast_lowlevel_priv info
#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