OpenOCD
stm32h7x.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2017 by STMicroelectronics *
5  ***************************************************************************/
6 #ifdef HAVE_CONFIG_H
7 #include "config.h"
8 #endif
9 
10 #include "imp.h"
11 #include <helper/binarybuffer.h>
12 #include <helper/bits.h>
13 #include <target/algorithm.h>
14 #include <target/cortex_m.h>
15 
16 /* Erase time can be as high as 1000ms, 10x this and it's toast... */
17 #define FLASH_ERASE_TIMEOUT 10000
18 #define FLASH_WRITE_TIMEOUT 5
19 #define MASS_ERASE_TIMEOUT 30000
20 
21 static const uint8_t stm32h7_flash_write_code[] = {
22 #include "../../../contrib/loaders/flash/stm32/stm32h7x.inc"
23 };
24 
25 static const uint8_t stm32h7rs_flash_write_code[] = {
26 #include "../../../contrib/loaders/flash/stm32/stm32h7rx.inc"
27 };
28 
45 };
46 
47 /* RM 433 */
48 /* Same Flash registers for both banks, */
49 /* access depends on Flash Base address */
51  [STM32_FLASH_ACR_INDEX] = 0x00,
52  [STM32_FLASH_KEYR_INDEX] = 0x04,
54  [STM32_FLASH_SR_INDEX] = 0x10,
55  [STM32_FLASH_CR_INDEX] = 0x0C,
57  [STM32_FLASH_OPTCR_INDEX] = 0x18,
60  [STM32_FLASH_OPTCCR_INDEX] = 0x24,
63 };
64 
66  [STM32_FLASH_ACR_INDEX] = 0x00,
67  [STM32_FLASH_KEYR_INDEX] = 0x04,
68  [STM32_FLASH_OPTKEYR_INDEX] = 0x100,
69  [STM32_FLASH_SR_INDEX] = 0x14,
70  [STM32_FLASH_CR_INDEX] = 0x10,
72  [STM32_FLASH_OPTCR_INDEX] = 0x104,
73  [STM32_FLASH_OPTSR_INDEX] = 0x10C,
74  [STM32_FLASH_ISR_INDEX] = 0x24,
75 };
76 
77 /* FLASH_CR register bits */
78 #define FLASH_LOCK BIT(0)
79 #define FLASH_PG BIT(1)
80 #define FLASH_SER BIT(2)
81 #define FLASH_BER BIT(3)
82 #define FLASH_PSIZE_8 (0 << 4)
83 #define FLASH_PSIZE_16 (1 << 4)
84 #define FLASH_PSIZE_32 (2 << 4)
85 #define FLASH_PSIZE_64 (3 << 4)
86 #define FLASH_FW BIT(6)
87 #define FLASH_START BIT(7)
88 
89 /* FLASH_ISR register bits for H7RS */
90 #define FLASH_CRCRDERRF BIT(28) /* CRC read error flag */
91 #define FLASH_CRCENDF BIT(27) /* CRC end flag */
92 #define FLASH_DBECCERRF BIT(26) /* ECC double error flag */
93 #define FLASH_SNECCERRF BIT(25) /* ECC single error flag */
94 #define FLASH_RDSERRF BIT(24) /* Read security error flag */
95 #define FLASH_INCERRF BIT(21) /* Inconsistency error flag */
96 #define FLASH_OBLERRF BIT(20) /* Option byte loading error flag */
97 #define FLASH_STRBERRF BIT(19) /* Strobe error flag */
98 #define FLASH_PGSERRF BIT(18) /* Programming sequence error flag */
99 #define FLASH_WRPERRF BIT(17) /* Write protection error flag */
100 #define FLASH_EOPF BIT(16) /* End-of-program flag */
101 
102 /* FLASH_SR register bits */
103 #define FLASH_BSY BIT(0) /* Operation in progress */
104 #define FLASH_QW BIT(2) /* Operation queue in progress */
105 #define FLASH_WRPERR BIT(17) /* Write protection error */
106 #define FLASH_PGSERR BIT(18) /* Programming sequence error */
107 #define FLASH_STRBERR BIT(19) /* Strobe error */
108 #define FLASH_INCERR BIT(21) /* Inconsistency error */
109 #define FLASH_OPERR BIT(22) /* Operation error */
110 #define FLASH_RDPERR BIT(23) /* Read Protection error */
111 #define FLASH_RDSERR BIT(24) /* Secure Protection error */
112 #define FLASH_SNECCERR BIT(25) /* Single ECC error */
113 #define FLASH_DBECCERR BIT(26) /* Double ECC error */
114 
115 #define FLASH_ERROR (FLASH_WRPERR | FLASH_PGSERR | FLASH_STRBERR | FLASH_INCERR | FLASH_OPERR | \
116  FLASH_RDPERR | FLASH_RDSERR | FLASH_SNECCERR | FLASH_DBECCERR)
117 /* Possible errors for H7RS */
118 #define FLASH_ERROR_H7RS (FLASH_CRCRDERRF | FLASH_CRCENDF | FLASH_DBECCERRF | FLASH_SNECCERRF | FLASH_RDSERRF | \
119  FLASH_INCERRF | FLASH_OBLERRF | FLASH_STRBERRF | FLASH_PGSERRF | FLASH_WRPERRF | FLASH_EOPF)
120 
121 /* FLASH_OPTCR register bits */
122 #define OPT_LOCK BIT(0)
123 #define OPT_START BIT(1)
124 
125 /* FLASH_OPTSR register bits */
126 #define OPT_BSY BIT(0)
127 #define OPT_RDP_POS 8
128 #define OPT_RDP_MASK (0xff << OPT_RDP_POS)
129 #define OPT_OPTCHANGEERR BIT(30)
130 
131 /* FLASH_OPTCCR register bits */
132 #define OPT_CLR_OPTCHANGEERR BIT(30)
133 
134 /* register unlock keys */
135 #define KEY1 0x45670123
136 #define KEY2 0xCDEF89AB
137 
138 /* option register unlock key */
139 #define OPTKEY1 0x08192A3B
140 #define OPTKEY2 0x4C5D6E7F
141 
142 #define DBGMCU_IDCODE_REGISTER 0x5C001000
143 #define FLASH_BANK0_ADDRESS 0x08000000
144 #define FLASH_BANK1_ADDRESS 0x08100000
145 #define FLASH_REG_BASE_B0 0x52002000
146 #define FLASH_REG_BASE_B1 0x52002100
147 
148 /* Supported device IDs */
149 #define DEVID_STM32H74_H75XX 0x450
150 #define DEVID_STM32H7A_H7BXX 0x480
151 #define DEVID_STM32H72_H73XX 0x483
152 #define DEVID_STM32H7R_H7SXX 0x485
153 
154 struct stm32h7_rev {
155  uint16_t rev;
156  const char *str;
157 };
158 
159 /* stm32h7_part_info permits the store each device information and specificities.
160  * the default unit is byte unless the suffix '_kb' is used. */
161 
163  uint16_t id;
164  const char *device_str;
165  const struct stm32h7_rev *revs;
166  size_t num_revs;
167  unsigned int page_size_kb;
168  unsigned int block_size; /* flash write word size in bytes */
171  uint16_t max_bank_size_kb; /* Used when has_dual_bank is true */
172  uint32_t fsize_addr; /* Location of FSIZE register */
173  uint32_t wps_group_size; /* write protection group sectors' count */
174  uint32_t wps_mask;
175  /* function to compute flash_cr register values */
176  uint32_t (*compute_flash_cr)(uint32_t cmd, int snb);
177  int (*get_flash_error_status)(struct flash_bank *bank, uint32_t *status);
178  uint32_t flash_error;
179  const uint8_t *write_code;
181 };
182 
184  bool probed;
185  uint32_t idcode;
186  uint32_t user_bank_size;
187  uint32_t flash_regs_base; /* Address of flash reg controller */
188  const uint32_t *flash_regs;
190 };
191 
193  OPT_RDP_L0 = 0xaa,
194  OPT_RDP_L1 = 0x00,
195  OPT_RDP_L2 = 0xcc
196 };
197 
198 static const struct stm32h7_rev stm32h74_h75xx_revs[] = {
199  { 0x1000, "A" }, { 0x1001, "Z" }, { 0x1003, "Y" }, { 0x2001, "X" }, { 0x2003, "V" },
200 };
201 
202 static const struct stm32h7_rev stm32h7a_h7bxx_revs[] = {
203  { 0x1000, "A"},
204 };
205 
206 static const struct stm32h7_rev stm32h72_h73xx_revs[] = {
207  { 0x1000, "A" }, { 0x1001, "Z" },
208 };
209 
210 static const struct stm32h7_rev stm32h7r_h7sxx_revs[] = {
211  { 0x1000, "A" }, { 0x2000, "B" },
212 };
213 
214 static uint32_t stm32h74_h75xx_compute_flash_cr(uint32_t cmd, int snb)
215 {
216  return cmd | (snb << 8);
217 }
218 
219 static uint32_t stm32h7a_h7b_h7r_h7sxx_compute_flash_cr(uint32_t cmd, int snb)
220 {
221  /* save FW and START bits, to be right shifted by 2 bits later */
222  const uint32_t tmp = cmd & (FLASH_FW | FLASH_START);
223 
224  /* mask parallelism (ignored), FW and START bits */
226 
227  return cmd | (tmp >> 2) | (snb << 6);
228 }
229 
230 static int stm32h7_get_flash_status(struct flash_bank *bank, uint32_t *status);
231 static int stm32h7rs_get_flash_status(struct flash_bank *bank, uint32_t *status);
232 
233 static const struct stm32h7_part_info stm32h7_parts[] = {
234  {
236  .revs = stm32h74_h75xx_revs,
237  .num_revs = ARRAY_SIZE(stm32h74_h75xx_revs),
238  .device_str = "STM32H74x/75x",
239  .page_size_kb = 128,
240  .block_size = 32,
241  .max_flash_size_kb = 2048,
242  .max_bank_size_kb = 1024,
243  .has_dual_bank = true,
244  .fsize_addr = 0x1FF1E880,
245  .wps_group_size = 1,
246  .wps_mask = 0xFF,
247  .compute_flash_cr = stm32h74_h75xx_compute_flash_cr,
248  .get_flash_error_status = stm32h7_get_flash_status,
249  .flash_error = FLASH_ERROR,
250  .write_code = stm32h7_flash_write_code,
251  .write_code_size = sizeof(stm32h7_flash_write_code),
252  },
253  {
254  .id = DEVID_STM32H7A_H7BXX,
255  .revs = stm32h7a_h7bxx_revs,
256  .num_revs = ARRAY_SIZE(stm32h7a_h7bxx_revs),
257  .device_str = "STM32H7Ax/7Bx",
258  .page_size_kb = 8,
259  .block_size = 16,
260  .max_flash_size_kb = 2048,
261  .max_bank_size_kb = 1024,
262  .has_dual_bank = true,
263  .fsize_addr = 0x08FFF80C,
264  .wps_group_size = 4,
265  .wps_mask = 0xFFFFFFFF,
266  .compute_flash_cr = stm32h7a_h7b_h7r_h7sxx_compute_flash_cr,
267  .get_flash_error_status = stm32h7_get_flash_status,
268  .flash_error = FLASH_ERROR,
269  .write_code = stm32h7_flash_write_code,
270  .write_code_size = sizeof(stm32h7_flash_write_code),
271  },
272  {
273  .id = DEVID_STM32H72_H73XX,
274  .revs = stm32h72_h73xx_revs,
275  .num_revs = ARRAY_SIZE(stm32h72_h73xx_revs),
276  .device_str = "STM32H72x/73x",
277  .page_size_kb = 128,
278  .block_size = 32,
279  .max_flash_size_kb = 1024,
280  .max_bank_size_kb = 1024,
281  .has_dual_bank = false,
282  .fsize_addr = 0x1FF1E880,
283  .wps_group_size = 1,
284  .wps_mask = 0xFF,
285  .compute_flash_cr = stm32h74_h75xx_compute_flash_cr,
286  .get_flash_error_status = stm32h7_get_flash_status,
287  .flash_error = FLASH_ERROR,
288  .write_code = stm32h7_flash_write_code,
289  .write_code_size = sizeof(stm32h7_flash_write_code),
290  },
291  {
292  .id = DEVID_STM32H7R_H7SXX,
293  .revs = stm32h7r_h7sxx_revs,
294  .num_revs = ARRAY_SIZE(stm32h7r_h7sxx_revs),
295  .device_str = "STM32H7Rx/7Sx",
296  .page_size_kb = 8,
297  .block_size = 16,
298  .max_flash_size_kb = 64,
299  .max_bank_size_kb = 64,
300  .has_dual_bank = false,
301  .fsize_addr = 0x08FFF80C,
302  .wps_group_size = 1,
303  .wps_mask = 0xFF,
304  .compute_flash_cr = stm32h7a_h7b_h7r_h7sxx_compute_flash_cr,
305  .get_flash_error_status = stm32h7rs_get_flash_status,
306  .flash_error = FLASH_ERROR_H7RS,
307  .write_code = stm32h7rs_flash_write_code,
308  .write_code_size = sizeof(stm32h7rs_flash_write_code),
309  },
310 };
311 
312 /* flash bank stm32x <base> <size> 0 0 <target#> */
313 
314 FLASH_BANK_COMMAND_HANDLER(stm32h7_flash_bank_command)
315 {
316  struct stm32h7_flash_bank *stm32h7_info;
317 
318  if (CMD_ARGC < 6)
320 
321  stm32h7_info = malloc(sizeof(struct stm32h7_flash_bank));
322  bank->driver_priv = stm32h7_info;
323 
324  stm32h7_info->probed = false;
325  stm32h7_info->user_bank_size = bank->size;
326 
327  return ERROR_OK;
328 }
329 
330 static inline uint32_t stm32h7_get_flash_reg(struct flash_bank *bank, uint32_t reg_offset)
331 {
332  struct stm32h7_flash_bank *stm32h7_info = bank->driver_priv;
333  return reg_offset + stm32h7_info->flash_regs_base;
334 }
335 
336 static int stm32h7_read_flash_reg(struct flash_bank *bank, uint32_t reg_offset, uint32_t *value)
337 {
338  uint32_t reg_addr = stm32h7_get_flash_reg(bank, reg_offset);
339  int retval = target_read_u32(bank->target, reg_addr, value);
340 
341  if (retval != ERROR_OK)
342  LOG_ERROR("error while reading from address 0x%" PRIx32, reg_addr);
343 
344  return retval;
345 }
346 
348  enum stm32h7_flash_reg_index reg_index, uint32_t *value)
349 {
350  struct stm32h7_flash_bank *stm32h7_info = bank->driver_priv;
351  return stm32h7_read_flash_reg(bank, stm32h7_info->flash_regs[reg_index], value);
352 }
353 
354 static int stm32h7_write_flash_reg(struct flash_bank *bank, uint32_t reg_offset, uint32_t value)
355 {
356  uint32_t reg_addr = stm32h7_get_flash_reg(bank, reg_offset);
357  int retval = target_write_u32(bank->target, reg_addr, value);
358 
359  if (retval != ERROR_OK)
360  LOG_ERROR("error while writing to address 0x%" PRIx32, reg_addr);
361 
362  return retval;
363 }
364 
366  enum stm32h7_flash_reg_index reg_index, uint32_t value)
367 {
368  struct stm32h7_flash_bank *stm32h7_info = bank->driver_priv;
369  return stm32h7_write_flash_reg(bank, stm32h7_info->flash_regs[reg_index], value);
370 }
371 
372 static int stm32h7_get_flash_status(struct flash_bank *bank, uint32_t *status)
373 {
375 }
376 
377 static int stm32h7rs_get_flash_status(struct flash_bank *bank, uint32_t *status)
378 {
380 }
381 
383 {
384  uint32_t status;
385  int retval;
386  struct stm32h7_flash_bank *stm32h7_info = bank->driver_priv;
387 
388  /* wait for flash operations completion */
389  for (;;) {
391  if (retval != ERROR_OK)
392  return retval;
393 
394  if ((status & FLASH_QW) == 0)
395  break;
396 
397  if (timeout-- <= 0) {
398  LOG_ERROR("wait_flash_op_queue, time out expired, status: 0x%" PRIx32, status);
399  return ERROR_FAIL;
400  }
401  alive_sleep(1);
402  }
403 
404  if (status & FLASH_WRPERR) {
405  LOG_ERROR("wait_flash_op_queue, write protection error");
406  retval = ERROR_FAIL;
407  }
408 
409  /* Clear error + EOP flags but report errors */
410  if (status & stm32h7_info->part_info->flash_error) {
411  if (retval == ERROR_OK)
412  retval = ERROR_FAIL;
413  /* If this operation fails, we ignore it and report the original retval */
415  }
416  return retval;
417 }
418 
419 static int stm32h7_unlock_reg(struct flash_bank *bank)
420 {
421  uint32_t ctrl;
422 
423  /* first check if not already unlocked
424  * otherwise writing on FLASH_KEYR will fail
425  */
427  if (retval != ERROR_OK)
428  return retval;
429 
430  if ((ctrl & FLASH_LOCK) == 0)
431  return ERROR_OK;
432 
433  /* unlock flash registers for bank */
435  if (retval != ERROR_OK)
436  return retval;
437 
439  if (retval != ERROR_OK)
440  return retval;
441 
443  if (retval != ERROR_OK)
444  return retval;
445 
446  if (ctrl & FLASH_LOCK) {
447  LOG_ERROR("flash not unlocked STM32_FLASH_CRx: 0x%" PRIx32, ctrl);
448  return ERROR_TARGET_FAILURE;
449  }
450  return ERROR_OK;
451 }
452 
454 {
455  uint32_t ctrl;
456 
458  if (retval != ERROR_OK)
459  return retval;
460 
461  if ((ctrl & OPT_LOCK) == 0)
462  return ERROR_OK;
463 
464  /* unlock option registers */
466  if (retval != ERROR_OK)
467  return retval;
468 
470  if (retval != ERROR_OK)
471  return retval;
472 
474  if (retval != ERROR_OK)
475  return retval;
476 
477  if (ctrl & OPT_LOCK) {
478  LOG_ERROR("options not unlocked STM32_FLASH_OPTCR: 0x%" PRIx32, ctrl);
479  return ERROR_TARGET_FAILURE;
480  }
481 
482  return ERROR_OK;
483 }
484 
485 static inline int stm32h7_lock_reg(struct flash_bank *bank)
486 {
488 }
489 
490 static inline int stm32h7_lock_option_reg(struct flash_bank *bank)
491 {
493 }
494 
495 static int stm32h7_write_option(struct flash_bank *bank, uint32_t reg_offset, uint32_t value)
496 {
497  int retval, retval2;
498 
499  /* unlock option bytes for modification */
501  if (retval != ERROR_OK)
502  goto flash_options_lock;
503 
504  /* write option bytes */
505  retval = stm32h7_write_flash_reg_by_index(bank, reg_offset, value);
506  if (retval != ERROR_OK)
507  goto flash_options_lock;
508 
509  /* Remove OPT error flag before programming */
511  if (retval != ERROR_OK)
512  goto flash_options_lock;
513 
514  /* start programming cycle */
516  if (retval != ERROR_OK)
517  goto flash_options_lock;
518 
519  /* wait for completion */
521  uint32_t status;
522  for (;;) {
524  if (retval != ERROR_OK) {
525  LOG_ERROR("stm32h7_options_program: failed to read FLASH_OPTSR_CUR");
526  goto flash_options_lock;
527  }
528  if ((status & OPT_BSY) == 0)
529  break;
530 
531  if (timeout-- <= 0) {
532  LOG_ERROR("waiting for OBL launch, time out expired, OPTSR: 0x%" PRIx32, status);
533  retval = ERROR_FAIL;
534  goto flash_options_lock;
535  }
536  alive_sleep(1);
537  }
538 
539  /* check for failure */
540  if (status & OPT_OPTCHANGEERR) {
541  LOG_ERROR("error changing option bytes (OPTCHANGEERR=1)");
543  }
544 
545 flash_options_lock:
546  retval2 = stm32h7_lock_option_reg(bank);
547  if (retval2 != ERROR_OK)
548  LOG_ERROR("error during the lock of flash options");
549 
550  return (retval == ERROR_OK) ? retval2 : retval;
551 }
552 
553 static int stm32h7_modify_option(struct flash_bank *bank, uint32_t reg_offset, uint32_t value, uint32_t mask)
554 {
555  uint32_t data;
556 
557  int retval = stm32h7_read_flash_reg_by_index(bank, reg_offset, &data);
558  if (retval != ERROR_OK)
559  return retval;
560 
561  data = (data & ~mask) | (value & mask);
562 
563  return stm32h7_write_option(bank, reg_offset, data);
564 }
565 
567 {
568  uint32_t protection;
569 
570  /* read 'write protection' settings */
572  if (retval != ERROR_OK) {
573  LOG_DEBUG("unable to read WPSN_CUR register");
574  return retval;
575  }
576 
577  for (unsigned int i = 0; i < bank->num_prot_blocks; i++)
578  bank->prot_blocks[i].is_protected = protection & (1 << i) ? 0 : 1;
579 
580  return ERROR_OK;
581 }
582 
583 static int stm32h7_erase(struct flash_bank *bank, unsigned int first,
584  unsigned int last)
585 {
586  struct stm32h7_flash_bank *stm32h7_info = bank->driver_priv;
587  int retval, retval2;
588 
589  assert(first < bank->num_sectors);
590  assert(last < bank->num_sectors);
591 
592  if (bank->target->state != TARGET_HALTED)
594 
595  retval = stm32h7_unlock_reg(bank);
596  if (retval != ERROR_OK)
597  goto flash_lock;
598 
599  /*
600  Sector Erase
601  To erase a sector, follow the procedure below:
602  1. Check that no Flash memory operation is ongoing by checking the QW bit in the
603  FLASH_SR register
604  2. Set the SER bit and select the sector
605  you wish to erase (SNB) in the FLASH_CR register
606  3. Set the STRT bit in the FLASH_CR register
607  4. Wait for flash operations completion
608  */
609  for (unsigned int i = first; i <= last; i++) {
610  LOG_DEBUG("erase sector %u", i);
612  stm32h7_info->part_info->compute_flash_cr(FLASH_SER | FLASH_PSIZE_64, i));
613  if (retval != ERROR_OK) {
614  LOG_ERROR("Error erase sector %u", i);
615  goto flash_lock;
616  }
619  if (retval != ERROR_OK) {
620  LOG_ERROR("Error erase sector %u", i);
621  goto flash_lock;
622  }
624 
625  if (retval != ERROR_OK) {
626  LOG_ERROR("erase time-out or operation error sector %u", i);
627  goto flash_lock;
628  }
629  }
630 
631 flash_lock:
632  retval2 = stm32h7_lock_reg(bank);
633  if (retval2 != ERROR_OK)
634  LOG_ERROR("error during the lock of flash");
635 
636  return (retval == ERROR_OK) ? retval2 : retval;
637 }
638 
639 static int stm32h7_protect(struct flash_bank *bank, int set, unsigned int first,
640  unsigned int last)
641 {
642  struct target *target = bank->target;
643  struct stm32h7_flash_bank *stm32h7_info = bank->driver_priv;
644  uint32_t protection;
645 
646  if (target->state != TARGET_HALTED) {
647  LOG_ERROR("Target not halted");
649  }
650 
651  /* read 'write protection' settings */
653  if (retval != ERROR_OK) {
654  LOG_DEBUG("unable to read WPSN_CUR register");
655  return retval;
656  }
657 
658  for (unsigned int i = first; i <= last; i++) {
659  if (set)
660  protection &= ~(1 << i);
661  else
662  protection |= (1 << i);
663  }
664 
665  /* apply WRPSN mask */
666  protection &= stm32h7_info->part_info->wps_mask;
667 
668  LOG_DEBUG("stm32h7_protect, option_bytes written WPSN 0x%" PRIx32, protection);
669 
670  /* apply new option value */
672 }
673 
674 static int stm32h7_write_block(struct flash_bank *bank, const uint8_t *buffer,
675  uint32_t offset, uint32_t count)
676 {
677  struct target *target = bank->target;
678  struct stm32h7_flash_bank *stm32h7_info = bank->driver_priv;
679  /*
680  * If the size of the data part of the buffer is not a multiple of .block_size, we get
681  * "corrupted fifo read" pointer in target_run_flash_async_algorithm()
682  */
683  uint32_t data_size = 512 * stm32h7_info->part_info->block_size;
684  uint32_t buffer_size = 8 + data_size;
685  struct working_area *write_algorithm;
686  struct working_area *source;
687  uint32_t address = bank->base + offset;
688  struct reg_param reg_params[6];
689  struct armv7m_algorithm armv7m_info;
690  int retval = ERROR_OK;
691 
693  &write_algorithm) != ERROR_OK) {
694  LOG_WARNING("no working area available, can't do block memory writes");
696  }
697 
698  retval = target_write_buffer(target, write_algorithm->address,
699  stm32h7_info->part_info->write_code_size,
700  stm32h7_info->part_info->write_code);
701  if (retval != ERROR_OK) {
702  target_free_working_area(target, write_algorithm);
703  return retval;
704  }
705 
706 
707  /* memory buffer */
709  data_size /= 2;
710  buffer_size = 8 + data_size;
711  if (data_size <= 256) {
712  /* we already allocated the writing code, but failed to get a
713  * buffer, free the algorithm */
714  target_free_working_area(target, write_algorithm);
715 
716  LOG_WARNING("no large enough working area available, can't do block memory writes");
718  }
719  }
720 
721  LOG_DEBUG("target_alloc_working_area_try : buffer_size -> 0x%" PRIx32, buffer_size);
722 
723  armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
724  armv7m_info.core_mode = ARM_MODE_THREAD;
725 
726  init_reg_param(&reg_params[0], "r0", 32, PARAM_IN_OUT); /* buffer start, status (out) */
727  init_reg_param(&reg_params[1], "r1", 32, PARAM_OUT); /* buffer end */
728  init_reg_param(&reg_params[2], "r2", 32, PARAM_OUT); /* target address */
729  init_reg_param(&reg_params[3], "r3", 32, PARAM_OUT); /* count of words (word size = .block_size (bytes) */
730  init_reg_param(&reg_params[4], "r4", 32, PARAM_OUT); /* word size in bytes */
731  init_reg_param(&reg_params[5], "r5", 32, PARAM_OUT); /* flash reg base */
732 
733  buf_set_u32(reg_params[0].value, 0, 32, source->address);
734  buf_set_u32(reg_params[1].value, 0, 32, source->address + source->size);
735  buf_set_u32(reg_params[2].value, 0, 32, address);
736  buf_set_u32(reg_params[3].value, 0, 32, count);
737  buf_set_u32(reg_params[4].value, 0, 32, stm32h7_info->part_info->block_size);
738  buf_set_u32(reg_params[5].value, 0, 32, stm32h7_info->flash_regs_base);
739 
741  buffer,
742  count,
743  stm32h7_info->part_info->block_size,
744  0, NULL,
745  ARRAY_SIZE(reg_params), reg_params,
746  source->address, source->size,
747  write_algorithm->address, 0,
748  &armv7m_info);
749 
750  if (retval == ERROR_FLASH_OPERATION_FAILED) {
751  LOG_ERROR("error executing stm32h7 flash write algorithm");
752 
753  uint32_t flash_sr = buf_get_u32(reg_params[0].value, 0, 32);
754 
755  if (flash_sr & FLASH_WRPERR)
756  LOG_ERROR("flash memory write protected");
757 
758  if ((flash_sr & stm32h7_info->part_info->flash_error) != 0) {
759  LOG_ERROR("flash write failed, status = 0x%08" PRIx32, flash_sr);
760  /* Clear error + EOP flags but report errors */
762  retval = ERROR_FAIL;
763  }
764  }
765 
767  target_free_working_area(target, write_algorithm);
768 
769  destroy_reg_param(&reg_params[0]);
770  destroy_reg_param(&reg_params[1]);
771  destroy_reg_param(&reg_params[2]);
772  destroy_reg_param(&reg_params[3]);
773  destroy_reg_param(&reg_params[4]);
774  destroy_reg_param(&reg_params[5]);
775  return retval;
776 }
777 
778 static int stm32h7_write(struct flash_bank *bank, const uint8_t *buffer,
779  uint32_t offset, uint32_t count)
780 {
781  struct target *target = bank->target;
782  struct stm32h7_flash_bank *stm32h7_info = bank->driver_priv;
783  uint32_t address = bank->base + offset;
784  int retval, retval2;
785 
786  if (bank->target->state != TARGET_HALTED) {
787  LOG_ERROR("Target not halted");
789  }
790 
791  /* should be enforced via bank->write_start_alignment */
792  assert(!(offset % stm32h7_info->part_info->block_size));
793 
794  /* should be enforced via bank->write_end_alignment */
795  assert(!(count % stm32h7_info->part_info->block_size));
796 
797  retval = stm32h7_unlock_reg(bank);
798  if (retval != ERROR_OK)
799  goto flash_lock;
800 
801  uint32_t blocks_remaining = count / stm32h7_info->part_info->block_size;
802 
803  /* multiple words (n * .block_size) to be programmed in block */
804  if (blocks_remaining) {
805  retval = stm32h7_write_block(bank, buffer, offset, blocks_remaining);
806  if (retval != ERROR_OK) {
807  if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE) {
808  /* if block write failed (no sufficient working area),
809  * we use normal (slow) dword accesses */
810  LOG_WARNING("couldn't use block writes, falling back to single memory accesses");
811  }
812  } else {
813  buffer += blocks_remaining * stm32h7_info->part_info->block_size;
814  address += blocks_remaining * stm32h7_info->part_info->block_size;
815  blocks_remaining = 0;
816  }
817  if ((retval != ERROR_OK) && (retval != ERROR_TARGET_RESOURCE_NOT_AVAILABLE))
818  goto flash_lock;
819  }
820 
821  /*
822  Standard programming
823  The Flash memory programming sequence is as follows:
824  1. Check that no main Flash memory operation is ongoing by checking the QW bit in the
825  FLASH_SR register.
826  2. Set the PG bit in the FLASH_CR register
827  3. 8 x Word access (or Force Write FW)
828  4. Wait for flash operations completion
829  */
830  while (blocks_remaining > 0) {
832  stm32h7_info->part_info->compute_flash_cr(FLASH_PG | FLASH_PSIZE_64, 0));
833  if (retval != ERROR_OK)
834  goto flash_lock;
835 
836  retval = target_write_buffer(target, address, stm32h7_info->part_info->block_size, buffer);
837  if (retval != ERROR_OK)
838  goto flash_lock;
839 
841  if (retval != ERROR_OK)
842  goto flash_lock;
843 
844  buffer += stm32h7_info->part_info->block_size;
845  address += stm32h7_info->part_info->block_size;
846  blocks_remaining--;
847  }
848 
849 flash_lock:
850  retval2 = stm32h7_lock_reg(bank);
851  if (retval2 != ERROR_OK)
852  LOG_ERROR("error during the lock of flash");
853 
854  return (retval == ERROR_OK) ? retval2 : retval;
855 }
856 
857 static int stm32h7_read_id_code(struct flash_bank *bank, uint32_t *id)
858 {
859  /* read stm32 device id register */
860  int retval = target_read_u32(bank->target, DBGMCU_IDCODE_REGISTER, id);
861  if (retval != ERROR_OK)
862  return retval;
863  return ERROR_OK;
864 }
865 
866 static int stm32h7_probe(struct flash_bank *bank)
867 {
868  struct target *target = bank->target;
869  struct stm32h7_flash_bank *stm32h7_info = bank->driver_priv;
870  uint16_t flash_size_in_kb;
871  uint32_t device_id;
872 
873  stm32h7_info->probed = false;
874  stm32h7_info->part_info = NULL;
875 
876  if (!target_was_examined(target)) {
877  LOG_ERROR("Target not examined yet");
879  }
880 
881  int retval = stm32h7_read_id_code(bank, &stm32h7_info->idcode);
882  if (retval != ERROR_OK)
883  return retval;
884 
885  LOG_DEBUG("device id = 0x%08" PRIx32, stm32h7_info->idcode);
886 
887  device_id = stm32h7_info->idcode & 0xfff;
888  if (device_id == DEVID_STM32H7R_H7SXX) {
889  stm32h7_info->flash_regs = stm32h7rs_flash_regs;
890  } else {
891  stm32h7_info->flash_regs = stm32h7_flash_regs;
892  }
893 
894  for (unsigned int n = 0; n < ARRAY_SIZE(stm32h7_parts); n++) {
895  if (device_id == stm32h7_parts[n].id)
896  stm32h7_info->part_info = &stm32h7_parts[n];
897  }
898  if (!stm32h7_info->part_info) {
899  LOG_WARNING("Cannot identify target as a STM32H7xx family.");
900  return ERROR_FAIL;
901  } else {
902  LOG_INFO("Device: %s", stm32h7_info->part_info->device_str);
903  }
904 
905  /* update the address of controller */
906  if (bank->base == FLASH_BANK0_ADDRESS)
907  stm32h7_info->flash_regs_base = FLASH_REG_BASE_B0;
908  else if (bank->base == FLASH_BANK1_ADDRESS)
909  stm32h7_info->flash_regs_base = FLASH_REG_BASE_B1;
910  else {
911  LOG_WARNING("Flash register base not defined for bank %u", bank->bank_number);
912  return ERROR_FAIL;
913  }
914  LOG_DEBUG("flash_regs_base: 0x%" PRIx32, stm32h7_info->flash_regs_base);
915 
916  /* get flash size from target */
917  /* STM32H74x/H75x, the second core (Cortex-M4) cannot read the flash size */
918  retval = ERROR_FAIL;
919  if (device_id == DEVID_STM32H74_H75XX
921  LOG_WARNING("%s cannot read the flash size register", target_name(target));
922  else
923  retval = target_read_u16(target, stm32h7_info->part_info->fsize_addr, &flash_size_in_kb);
924 
925  if (retval != ERROR_OK) {
926  /* read error when device has invalid value, set max flash size */
927  flash_size_in_kb = stm32h7_info->part_info->max_flash_size_kb;
928  LOG_INFO("assuming %" PRIu16 "k flash", flash_size_in_kb);
929  } else
930  LOG_INFO("flash size probed value %" PRIu16 "k", flash_size_in_kb);
931 
932  /* setup bank size */
933  const uint32_t bank1_base = FLASH_BANK0_ADDRESS;
934  const uint32_t bank2_base = bank1_base + stm32h7_info->part_info->max_bank_size_kb * 1024;
935  bool has_dual_bank = stm32h7_info->part_info->has_dual_bank;
936 
937  switch (device_id) {
940  /* For STM32H74x/75x and STM32H7Ax/Bx
941  * - STM32H7xxxI devices contains dual bank, 1 Mbyte each
942  * - STM32H7xxxG devices contains dual bank, 512 Kbyte each
943  * - STM32H7xxxB devices contains single bank, 128 Kbyte
944  * - the second bank starts always from 0x08100000
945  */
946  if (flash_size_in_kb == 128)
947  has_dual_bank = false;
948  else
949  /* flash size is 2M or 1M */
950  flash_size_in_kb /= 2;
951  break;
954  break;
955  default:
956  LOG_ERROR("unsupported device");
957  return ERROR_FAIL;
958  }
959 
960  if (has_dual_bank) {
961  LOG_INFO("STM32H7 flash has dual banks");
962  if (bank->base != bank1_base && bank->base != bank2_base) {
963  LOG_ERROR("STM32H7 flash bank base address config is incorrect. "
964  TARGET_ADDR_FMT " but should rather be 0x%" PRIx32 " or 0x%" PRIx32,
965  bank->base, bank1_base, bank2_base);
966  return ERROR_FAIL;
967  }
968  } else {
969  LOG_INFO("STM32H7 flash has a single bank");
970  if (bank->base == bank2_base) {
971  LOG_ERROR("this device has a single bank only");
972  return ERROR_FAIL;
973  } else if (bank->base != bank1_base) {
974  LOG_ERROR("STM32H7 flash bank base address config is incorrect. "
975  TARGET_ADDR_FMT " but should be 0x%" PRIx32,
976  bank->base, bank1_base);
977  return ERROR_FAIL;
978  }
979  }
980 
981  LOG_INFO("Bank (%u) size is %" PRIu16 " kb, base address is " TARGET_ADDR_FMT,
982  bank->bank_number, flash_size_in_kb, bank->base);
983 
984  /* if the user sets the size manually then ignore the probed value
985  * this allows us to work around devices that have an invalid flash size register value */
986  if (stm32h7_info->user_bank_size) {
987  LOG_INFO("ignoring flash probed value, using configured bank size");
988  flash_size_in_kb = stm32h7_info->user_bank_size / 1024;
989  } else if (flash_size_in_kb == 0xffff) {
990  /* die flash size */
991  flash_size_in_kb = stm32h7_info->part_info->max_flash_size_kb;
992  }
993 
994  /* did we assign flash size? */
995  assert(flash_size_in_kb != 0xffff);
996  bank->size = flash_size_in_kb * 1024;
997  bank->write_start_alignment = stm32h7_info->part_info->block_size;
998  bank->write_end_alignment = stm32h7_info->part_info->block_size;
999 
1000  /* setup sectors */
1001  bank->num_sectors = flash_size_in_kb / stm32h7_info->part_info->page_size_kb;
1002  assert(bank->num_sectors > 0);
1003 
1004  free(bank->sectors);
1005 
1006  bank->sectors = alloc_block_array(0, stm32h7_info->part_info->page_size_kb * 1024,
1007  bank->num_sectors);
1008 
1009  if (!bank->sectors) {
1010  LOG_ERROR("failed to allocate bank sectors");
1011  return ERROR_FAIL;
1012  }
1013 
1014  /* setup protection blocks */
1015  const uint32_t wpsn = stm32h7_info->part_info->wps_group_size;
1016  assert(bank->num_sectors % wpsn == 0);
1017 
1018  bank->num_prot_blocks = bank->num_sectors / wpsn;
1019  assert(bank->num_prot_blocks > 0);
1020 
1021  free(bank->prot_blocks);
1022 
1023  bank->prot_blocks = alloc_block_array(0, stm32h7_info->part_info->page_size_kb * wpsn * 1024,
1024  bank->num_prot_blocks);
1025 
1026  if (!bank->prot_blocks) {
1027  LOG_ERROR("failed to allocate bank prot_block");
1028  return ERROR_FAIL;
1029  }
1030 
1031  stm32h7_info->probed = true;
1032  return ERROR_OK;
1033 }
1034 
1036 {
1037  struct stm32h7_flash_bank *stm32h7_info = bank->driver_priv;
1038 
1039  if (stm32h7_info->probed)
1040  return ERROR_OK;
1041 
1042  return stm32h7_probe(bank);
1043 }
1044 
1045 /* This method must return a string displaying information about the bank */
1047 {
1048  struct stm32h7_flash_bank *stm32h7_info = bank->driver_priv;
1049  const struct stm32h7_part_info *info = stm32h7_info->part_info;
1050 
1051  if (!stm32h7_info->probed) {
1052  int retval = stm32h7_probe(bank);
1053  if (retval != ERROR_OK) {
1054  command_print_sameline(cmd, "Unable to find bank information.");
1055  return retval;
1056  }
1057  }
1058 
1059  if (info) {
1060  const char *rev_str = NULL;
1061  uint16_t rev_id = stm32h7_info->idcode >> 16;
1062 
1063  for (unsigned int i = 0; i < info->num_revs; i++)
1064  if (rev_id == info->revs[i].rev)
1065  rev_str = info->revs[i].str;
1066 
1067  if (rev_str) {
1068  command_print_sameline(cmd, "%s - Rev: %s",
1069  stm32h7_info->part_info->device_str, rev_str);
1070  } else {
1072  "%s - Rev: unknown (0x%04" PRIx16 ")",
1073  stm32h7_info->part_info->device_str, rev_id);
1074  }
1075  } else {
1076  command_print_sameline(cmd, "Cannot identify target as a STM32H7x");
1077  return ERROR_FAIL;
1078  }
1079  return ERROR_OK;
1080 }
1081 
1082 static int stm32h7_set_rdp(struct flash_bank *bank, enum stm32h7_opt_rdp new_rdp)
1083 {
1084  struct target *target = bank->target;
1085  uint32_t optsr, cur_rdp;
1086  int retval;
1087 
1088  if (target->state != TARGET_HALTED) {
1089  LOG_ERROR("Target not halted");
1090  return ERROR_TARGET_NOT_HALTED;
1091  }
1092 
1094 
1095  if (retval != ERROR_OK) {
1096  LOG_DEBUG("unable to read FLASH_OPTSR_PRG register");
1097  return retval;
1098  }
1099 
1100  /* get current RDP, and check if there is a change */
1101  cur_rdp = (optsr & OPT_RDP_MASK) >> OPT_RDP_POS;
1102  if (new_rdp == cur_rdp) {
1103  LOG_INFO("the requested RDP value is already programmed");
1104  return ERROR_OK;
1105  }
1106 
1107  switch (new_rdp) {
1108  case OPT_RDP_L0:
1109  LOG_WARNING("unlocking the entire flash device");
1110  break;
1111  case OPT_RDP_L1:
1112  LOG_WARNING("locking the entire flash device");
1113  break;
1114  case OPT_RDP_L2:
1115  LOG_WARNING("locking the entire flash device, irreversible");
1116  break;
1117  }
1118 
1119  /* apply new RDP */
1120  optsr = (optsr & ~OPT_RDP_MASK) | (new_rdp << OPT_RDP_POS);
1121 
1122  /* apply new option value */
1124 }
1125 
1126 COMMAND_HANDLER(stm32h7_handle_lock_command)
1127 {
1128  if (CMD_ARGC < 1)
1130 
1131  struct flash_bank *bank;
1132  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
1133  if (retval != ERROR_OK)
1134  return retval;
1135 
1136  retval = stm32h7_set_rdp(bank, OPT_RDP_L1);
1137 
1138  if (retval != ERROR_OK)
1139  command_print(CMD, "%s failed to lock device", bank->driver->name);
1140  else
1141  command_print(CMD, "%s locked", bank->driver->name);
1142 
1143  return retval;
1144 }
1145 
1146 COMMAND_HANDLER(stm32h7_handle_unlock_command)
1147 {
1148  if (CMD_ARGC < 1)
1150 
1151  struct flash_bank *bank;
1152  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
1153  if (retval != ERROR_OK)
1154  return retval;
1155 
1156  retval = stm32h7_set_rdp(bank, OPT_RDP_L0);
1157 
1158  if (retval != ERROR_OK)
1159  command_print(CMD, "%s failed to unlock device", bank->driver->name);
1160  else
1161  command_print(CMD, "%s unlocked", bank->driver->name);
1162 
1163  return retval;
1164 }
1165 
1167 {
1168  int retval, retval2;
1169  struct target *target = bank->target;
1170  struct stm32h7_flash_bank *stm32h7_info = bank->driver_priv;
1171 
1172  if (target->state != TARGET_HALTED) {
1173  LOG_ERROR("Target not halted");
1174  return ERROR_TARGET_NOT_HALTED;
1175  }
1176 
1177  retval = stm32h7_unlock_reg(bank);
1178  if (retval != ERROR_OK)
1179  goto flash_lock;
1180 
1181  /* mass erase flash memory bank */
1183  stm32h7_info->part_info->compute_flash_cr(FLASH_BER | FLASH_PSIZE_64, 0));
1184  if (retval != ERROR_OK)
1185  goto flash_lock;
1186 
1189  if (retval != ERROR_OK)
1190  goto flash_lock;
1191 
1193  if (retval != ERROR_OK)
1194  goto flash_lock;
1195 
1196 flash_lock:
1197  retval2 = stm32h7_lock_reg(bank);
1198  if (retval2 != ERROR_OK)
1199  LOG_ERROR("error during the lock of flash");
1200 
1201  return (retval == ERROR_OK) ? retval2 : retval;
1202 }
1203 
1204 COMMAND_HANDLER(stm32h7_handle_mass_erase_command)
1205 {
1206  if (CMD_ARGC != 1)
1208 
1209  struct flash_bank *bank;
1210  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
1211  if (retval != ERROR_OK)
1212  return retval;
1213 
1214  retval = stm32h7_mass_erase(bank);
1215  if (retval == ERROR_OK)
1216  command_print(CMD, "stm32h7x mass erase complete");
1217  else
1218  command_print(CMD, "stm32h7x mass erase failed");
1219 
1220  return retval;
1221 }
1222 
1223 COMMAND_HANDLER(stm32h7_handle_option_read_command)
1224 {
1225  if (CMD_ARGC != 2)
1227 
1228  struct flash_bank *bank;
1229  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
1230  if (retval != ERROR_OK)
1231  return retval;
1232 
1233  uint32_t reg_offset, value;
1234 
1235  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], reg_offset);
1236  retval = stm32h7_read_flash_reg_by_index(bank, reg_offset, &value);
1237  if (retval != ERROR_OK)
1238  return retval;
1239 
1240  command_print(CMD, "Option Register: <0x%" PRIx32 "> = 0x%" PRIx32,
1241  stm32h7_get_flash_reg(bank, reg_offset), value);
1242 
1243  return retval;
1244 }
1245 
1246 COMMAND_HANDLER(stm32h7_handle_option_write_command)
1247 {
1248  if (CMD_ARGC != 3 && CMD_ARGC != 4)
1250 
1251  struct flash_bank *bank;
1252  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
1253  if (retval != ERROR_OK)
1254  return retval;
1255 
1256  uint32_t reg_offset, value, mask = 0xffffffff;
1257 
1258  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], reg_offset);
1259  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], value);
1260  if (CMD_ARGC > 3)
1262 
1263  return stm32h7_modify_option(bank, reg_offset, value, mask);
1264 }
1265 
1266 static const struct command_registration stm32h7_exec_command_handlers[] = {
1267  {
1268  .name = "lock",
1269  .handler = stm32h7_handle_lock_command,
1270  .mode = COMMAND_EXEC,
1271  .usage = "bank_id",
1272  .help = "Lock entire flash device.",
1273  },
1274  {
1275  .name = "unlock",
1276  .handler = stm32h7_handle_unlock_command,
1277  .mode = COMMAND_EXEC,
1278  .usage = "bank_id",
1279  .help = "Unlock entire protected flash device.",
1280  },
1281  {
1282  .name = "mass_erase",
1283  .handler = stm32h7_handle_mass_erase_command,
1284  .mode = COMMAND_EXEC,
1285  .usage = "bank_id",
1286  .help = "Erase entire flash device.",
1287  },
1288  {
1289  .name = "option_read",
1290  .handler = stm32h7_handle_option_read_command,
1291  .mode = COMMAND_EXEC,
1292  .usage = "bank_id reg_offset",
1293  .help = "Read and display device option bytes.",
1294  },
1295  {
1296  .name = "option_write",
1297  .handler = stm32h7_handle_option_write_command,
1298  .mode = COMMAND_EXEC,
1299  .usage = "bank_id reg_offset value [mask]",
1300  .help = "Write device option bit fields with provided value.",
1301  },
1303 };
1304 
1305 static const struct command_registration stm32h7_command_handlers[] = {
1306  {
1307  .name = "stm32h7x",
1308  .mode = COMMAND_ANY,
1309  .help = "stm32h7x flash command group",
1310  .usage = "",
1312  },
1314 };
1315 
1316 const struct flash_driver stm32h7x_flash = {
1317  .name = "stm32h7x",
1318  .commands = stm32h7_command_handlers,
1319  .flash_bank_command = stm32h7_flash_bank_command,
1320  .erase = stm32h7_erase,
1321  .protect = stm32h7_protect,
1322  .write = stm32h7_write,
1323  .read = default_flash_read,
1324  .probe = stm32h7_probe,
1325  .auto_probe = stm32h7_auto_probe,
1326  .erase_check = default_flash_blank_check,
1327  .protect_check = stm32h7_protect_check,
1328  .info = stm32h7_get_info,
1329  .free_driver_priv = default_flash_free_driver_priv,
1330 };
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
@ PARAM_IN_OUT
Definition: algorithm.h:17
@ ARM_MODE_THREAD
Definition: arm.h:94
#define ARMV7M_COMMON_MAGIC
Definition: armv7m.h:229
Support functions to access arbitrary bits in a byte array.
static uint32_t buf_get_u32(const uint8_t *_buffer, unsigned int first, unsigned int num)
Retrieves num bits from _buffer, starting at the first bit, returning the bits in a 32-bit word.
Definition: binarybuffer.h:104
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:348
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:371
#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:400
#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_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:440
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:251
@ 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:378
@ CORTEX_M4_PARTNO
Definition: cortex_m.h:58
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
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:470
#define LOG_WARNING(expr ...)
Definition: log.h:131
#define ERROR_FAIL
Definition: log.h:175
#define LOG_ERROR(expr ...)
Definition: log.h:134
#define LOG_INFO(expr ...)
Definition: log.h:128
#define LOG_DEBUG(expr ...)
Definition: log.h:111
#define ERROR_OK
Definition: log.h:169
uint8_t mask
Definition: parport.c:70
uint8_t protection
Definition: qn908x.c:1
struct rtt_control ctrl
Control block.
Definition: rtt/rtt.c:25
struct rtt_source source
Definition: rtt/rtt.c:23
#define MASS_ERASE_TIMEOUT
Definition: stm32h7x.c:19
static int stm32h7_protect_check(struct flash_bank *bank)
Definition: stm32h7x.c:566
static const struct stm32h7_part_info stm32h7_parts[]
Definition: stm32h7x.c:233
#define FLASH_ERASE_TIMEOUT
Definition: stm32h7x.c:17
#define OPT_BSY
Definition: stm32h7x.c:126
#define DEVID_STM32H74_H75XX
Definition: stm32h7x.c:149
#define OPT_CLR_OPTCHANGEERR
Definition: stm32h7x.c:132
static const struct stm32h7_rev stm32h74_h75xx_revs[]
Definition: stm32h7x.c:198
#define DEVID_STM32H72_H73XX
Definition: stm32h7x.c:151
#define FLASH_PG
Definition: stm32h7x.c:79
#define KEY2
Definition: stm32h7x.c:136
#define OPT_LOCK
Definition: stm32h7x.c:122
#define DBGMCU_IDCODE_REGISTER
Definition: stm32h7x.c:142
static const struct stm32h7_rev stm32h72_h73xx_revs[]
Definition: stm32h7x.c:206
#define FLASH_FW
Definition: stm32h7x.c:86
static int stm32h7_wait_flash_op_queue(struct flash_bank *bank, int timeout)
Definition: stm32h7x.c:382
static int stm32h7_probe(struct flash_bank *bank)
Definition: stm32h7x.c:866
#define OPT_START
Definition: stm32h7x.c:123
#define FLASH_BANK0_ADDRESS
Definition: stm32h7x.c:143
static const struct command_registration stm32h7_command_handlers[]
Definition: stm32h7x.c:1305
stm32h7_opt_rdp
Definition: stm32h7x.c:192
@ OPT_RDP_L2
Definition: stm32h7x.c:195
@ OPT_RDP_L0
Definition: stm32h7x.c:193
@ OPT_RDP_L1
Definition: stm32h7x.c:194
stm32h7_flash_reg_index
Definition: stm32h7x.c:29
@ STM32_FLASH_SR_INDEX
Definition: stm32h7x.c:33
@ STM32_FLASH_OPTCR_INDEX
Definition: stm32h7x.c:36
@ STM32_FLASH_OPTSR_CUR_INDEX
Definition: stm32h7x.c:38
@ STM32_FLASH_OPTSR_PRG_INDEX
Definition: stm32h7x.c:39
@ STM32_FLASH_ACR_INDEX
Definition: stm32h7x.c:30
@ STM32_FLASH_OPTCCR_INDEX
Definition: stm32h7x.c:40
@ STM32_FLASH_WPSN_PRG_INDEX
Definition: stm32h7x.c:42
@ STM32_FLASH_CR_INDEX
Definition: stm32h7x.c:34
@ STM32_FLASH_OPTKEYR_INDEX
Definition: stm32h7x.c:32
@ STM32_FLASH_OPTSR_INDEX
Definition: stm32h7x.c:37
@ STM32_FLASH_KEYR_INDEX
Definition: stm32h7x.c:31
@ STM32_FLASH_ICR_CCR_INDEX
Definition: stm32h7x.c:35
@ STM32_FLASH_WPSN_CUR_INDEX
Definition: stm32h7x.c:41
@ STM32_FLASH_ISR_INDEX
Definition: stm32h7x.c:43
@ STM32_FLASH_REG_INDEX_NUM
Definition: stm32h7x.c:44
static int stm32h7_get_info(struct flash_bank *bank, struct command_invocation *cmd)
Definition: stm32h7x.c:1046
#define FLASH_START
Definition: stm32h7x.c:87
static int stm32h7_modify_option(struct flash_bank *bank, uint32_t reg_offset, uint32_t value, uint32_t mask)
Definition: stm32h7x.c:553
#define OPT_RDP_POS
Definition: stm32h7x.c:127
#define FLASH_QW
Definition: stm32h7x.c:104
static int stm32h7_protect(struct flash_bank *bank, int set, unsigned int first, unsigned int last)
Definition: stm32h7x.c:639
static int stm32h7rs_get_flash_status(struct flash_bank *bank, uint32_t *status)
Definition: stm32h7x.c:377
static int stm32h7_write_option(struct flash_bank *bank, uint32_t reg_offset, uint32_t value)
Definition: stm32h7x.c:495
#define FLASH_BER
Definition: stm32h7x.c:81
#define FLASH_SER
Definition: stm32h7x.c:80
#define FLASH_WRPERR
Definition: stm32h7x.c:105
#define FLASH_REG_BASE_B0
Definition: stm32h7x.c:145
#define FLASH_ERROR
Definition: stm32h7x.c:115
#define OPTKEY2
Definition: stm32h7x.c:140
static const uint8_t stm32h7_flash_write_code[]
Definition: stm32h7x.c:21
static uint32_t stm32h7a_h7b_h7r_h7sxx_compute_flash_cr(uint32_t cmd, int snb)
Definition: stm32h7x.c:219
static int stm32h7_lock_option_reg(struct flash_bank *bank)
Definition: stm32h7x.c:490
#define FLASH_ERROR_H7RS
Definition: stm32h7x.c:118
static int stm32h7_unlock_option_reg(struct flash_bank *bank)
Definition: stm32h7x.c:453
static int stm32h7_read_flash_reg_by_index(struct flash_bank *bank, enum stm32h7_flash_reg_index reg_index, uint32_t *value)
Definition: stm32h7x.c:347
#define FLASH_LOCK
Definition: stm32h7x.c:78
static int stm32h7_set_rdp(struct flash_bank *bank, enum stm32h7_opt_rdp new_rdp)
Definition: stm32h7x.c:1082
#define FLASH_PSIZE_64
Definition: stm32h7x.c:85
static const uint32_t stm32h7_flash_regs[STM32_FLASH_REG_INDEX_NUM]
Definition: stm32h7x.c:50
#define DEVID_STM32H7R_H7SXX
Definition: stm32h7x.c:152
static int stm32h7_write_block(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
Definition: stm32h7x.c:674
#define OPT_OPTCHANGEERR
Definition: stm32h7x.c:129
static const uint32_t stm32h7rs_flash_regs[STM32_FLASH_REG_INDEX_NUM]
Definition: stm32h7x.c:65
#define OPT_RDP_MASK
Definition: stm32h7x.c:128
static uint32_t stm32h74_h75xx_compute_flash_cr(uint32_t cmd, int snb)
Definition: stm32h7x.c:214
FLASH_BANK_COMMAND_HANDLER(stm32h7_flash_bank_command)
Definition: stm32h7x.c:314
#define DEVID_STM32H7A_H7BXX
Definition: stm32h7x.c:150
static const struct stm32h7_rev stm32h7a_h7bxx_revs[]
Definition: stm32h7x.c:202
static const uint8_t stm32h7rs_flash_write_code[]
Definition: stm32h7x.c:25
static int stm32h7_write_flash_reg_by_index(struct flash_bank *bank, enum stm32h7_flash_reg_index reg_index, uint32_t value)
Definition: stm32h7x.c:365
static int stm32h7_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
Definition: stm32h7x.c:778
static int stm32h7_read_id_code(struct flash_bank *bank, uint32_t *id)
Definition: stm32h7x.c:857
static const struct command_registration stm32h7_exec_command_handlers[]
Definition: stm32h7x.c:1266
static int stm32h7_unlock_reg(struct flash_bank *bank)
Definition: stm32h7x.c:419
static int stm32h7_mass_erase(struct flash_bank *bank)
Definition: stm32h7x.c:1166
static int stm32h7_read_flash_reg(struct flash_bank *bank, uint32_t reg_offset, uint32_t *value)
Definition: stm32h7x.c:336
static const struct stm32h7_rev stm32h7r_h7sxx_revs[]
Definition: stm32h7x.c:210
const struct flash_driver stm32h7x_flash
Definition: stm32h7x.c:1316
COMMAND_HANDLER(stm32h7_handle_lock_command)
Definition: stm32h7x.c:1126
static uint32_t stm32h7_get_flash_reg(struct flash_bank *bank, uint32_t reg_offset)
Definition: stm32h7x.c:330
#define OPTKEY1
Definition: stm32h7x.c:139
static int stm32h7_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
Definition: stm32h7x.c:583
static int stm32h7_get_flash_status(struct flash_bank *bank, uint32_t *status)
Definition: stm32h7x.c:372
static int stm32h7_write_flash_reg(struct flash_bank *bank, uint32_t reg_offset, uint32_t value)
Definition: stm32h7x.c:354
#define FLASH_BANK1_ADDRESS
Definition: stm32h7x.c:144
#define FLASH_WRITE_TIMEOUT
Definition: stm32h7x.c:18
#define FLASH_REG_BASE_B1
Definition: stm32h7x.c:146
#define KEY1
Definition: stm32h7x.c:135
static int stm32h7_lock_reg(struct flash_bank *bank)
Definition: stm32h7x.c:485
static int stm32h7_auto_probe(struct flash_bank *bank)
Definition: stm32h7x.c:1035
unsigned int common_magic
Definition: armv7m.h:306
enum arm_mode core_mode
Definition: armv7m.h:308
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:234
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
uint32_t user_bank_size
Definition: stm32h7x.c:186
const struct stm32h7_part_info * part_info
Definition: stm32h7x.c:189
const uint32_t * flash_regs
Definition: stm32h7x.c:188
uint32_t idcode
Definition: stm32h7x.c:185
uint32_t flash_regs_base
Definition: stm32h7x.c:187
uint32_t fsize_addr
Definition: stm32h7x.c:172
unsigned int block_size
Definition: stm32h7x.c:168
const uint8_t * write_code
Definition: stm32h7x.c:179
int(* get_flash_error_status)(struct flash_bank *bank, uint32_t *status)
Definition: stm32h7x.c:177
const struct stm32h7_rev * revs
Definition: stm32h7x.c:165
uint32_t flash_error
Definition: stm32h7x.c:178
uint32_t wps_mask
Definition: stm32h7x.c:174
unsigned int page_size_kb
Definition: stm32h7x.c:167
uint32_t(* compute_flash_cr)(uint32_t cmd, int snb)
Definition: stm32h7x.c:176
uint16_t max_bank_size_kb
Definition: stm32h7x.c:171
const char * device_str
Definition: stm32h7x.c:164
uint16_t max_flash_size_kb
Definition: stm32h7x.c:169
uint32_t wps_group_size
Definition: stm32h7x.c:173
size_t write_code_size
Definition: stm32h7x.c:180
uint16_t rev
Definition: stm32h7x.c:155
const char * str
Definition: stm32h7x.c:156
Definition: target.h:119
enum target_state state
Definition: target.h:160
Definition: psoc6.c:83
target_addr_t address
Definition: target.h:89
int target_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
Definition: target.c:2351
int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
Definition: target.c:2070
int target_write_u32(struct target *target, target_addr_t address, uint32_t value)
Definition: target.c:2650
int target_free_working_area(struct target *target, struct working_area *area)
Free a working area.
Definition: target.c:2128
int target_alloc_working_area_try(struct target *target, uint32_t size, struct working_area **area)
Definition: target.c:1976
int target_read_u16(struct target *target, target_addr_t address, uint16_t *value)
Definition: target.c:2583
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:940
int target_read_u32(struct target *target, target_addr_t address, uint32_t *value)
Definition: target.c:2559
#define ERROR_TARGET_NOT_HALTED
Definition: target.h:786
static bool target_was_examined(const struct target *target)
Definition: target.h:432
static const char * target_name(const struct target *target)
Returns the instance-specific name of the specified target.
Definition: target.h:236
@ TARGET_HALTED
Definition: target.h:58
#define ERROR_TARGET_NOT_EXAMINED
Definition: target.h:793
#define ERROR_TARGET_RESOURCE_NOT_AVAILABLE
Definition: target.h:790
#define ERROR_TARGET_FAILURE
Definition: target.h:787
#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