OpenOCD
efm32.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 Andreas Fritiofson *
11  * andreas.fritiofson@gmail.com *
12  * *
13  * Copyright (C) 2013 by Roman Dmitrienko *
14  * me@iamroman.org *
15  * *
16  * Copyright (C) 2014 Nemui Trinomius *
17  * nemuisan_kawausogasuki@live.jp *
18  * *
19  * Copyright (C) 2021 Michael Teichgräber *
20  * mteichgraeber@gmx.de *
21  * *
22  * Copyright (C) 2021 Doug Brunner *
23  * doug.a.brunner@gmail.com *
24  * *
25  * Copyright (C) 2022 Mikrodust AB *
26  * henrik.persson@mikrodust.com *
27  * *
28  * Copyright (c) 2026 Silicon Laboratories Inc. *
29  * jerome.pouiller@silabs.com *
30  ***************************************************************************/
31 
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35 
36 #include "imp.h"
37 #include <helper/binarybuffer.h>
38 #include <helper/time_support.h>
39 #include <target/algorithm.h>
40 #include <target/armv7m.h>
41 #include <target/cortex_m.h>
42 
43 /* Datasheet specifies ~22ms for page erase on first chip generation. 100ms
44  * provides reasonable margin.
45  */
46 #define EFM32_FLASH_OPERATION_TIMEOUT 100
47 
48 #define EFM32_FLASH_BASE_V1 0x00000000
49 #define EFM32_FLASH_BASE_V2 0x08000000
50 
51 /* size in bytes, not words; must fit all Gecko devices */
52 #define LOCKWORDS_SZ 512
53 
54 #define EFM32_MSC_INFO_BASE 0x0fe00000
55 #define EFM32_MSC_USER_DATA (EFM32_MSC_INFO_BASE + 0x0000)
56 #define EFM32_MSC_LOCK_BITS (EFM32_MSC_INFO_BASE + 0x4000)
57 #define EFM32_MSC_LOCK_BITS_EXTRA (EFM32_MSC_INFO_BASE + 0x4200)
58 
62 #define EFM32_DI_PARTINFO_NUM_MASK 0x0000ffff
63 #define EFM32_DI_PARTINFO_FAMILY_MASK 0x00ff0000
64 #define EFM32_DI_PARTINFO_TYPE_MASK 0x3f000000
66 #define EFM32_DI_PARTINFO_PAGE_SZ_MASK 0x000000ff
67 #define EFM32_DI_PARTINFO_PAGE_SZ_UD_MASK 0x0000ff00
71 };
72 
73 #define EFM32_DI_PART_FAMILY (EFM32_MSC_INFO_BASE + 0x81fe)
74 
75 static const struct efm32_dev_info_addr efm32_dev_info_addr[] = {
76  [0] = {
77  .part_num = EFM32_MSC_INFO_BASE + 0x81fc,
78  .part_rev = EFM32_MSC_INFO_BASE + 0x81ff,
79  .part_info = 0x00000000, // Not used in Series 0/1
80  .page_size = EFM32_MSC_INFO_BASE + 0x81e7,
81  .flash_sz = EFM32_MSC_INFO_BASE + 0x81f8,
82  .ram_sz = EFM32_MSC_INFO_BASE + 0x81fa,
83  },
84  [1] = {
85  .part_num = EFM32_MSC_INFO_BASE + 0x81fc,
86  .part_rev = EFM32_MSC_INFO_BASE + 0x81ff,
87  .part_info = 0x0000000, // Not used in Series 0/1
88  .page_size = EFM32_MSC_INFO_BASE + 0x81e7,
89  .flash_sz = EFM32_MSC_INFO_BASE + 0x81f8,
90  .ram_sz = EFM32_MSC_INFO_BASE + 0x81fa,
91  },
92  [2] = {
93  .part_num = 0x00000000, // Not used in Series 2
94  .part_rev = EFM32_MSC_INFO_BASE + 0x8002,
95  .part_info = EFM32_MSC_INFO_BASE + 0x8004,
96  .page_size = EFM32_MSC_INFO_BASE + 0x8008,
97  .flash_sz = EFM32_MSC_INFO_BASE + 0x800c,
98  .ram_sz = EFM32_MSC_INFO_BASE + 0x800e,
99  },
100 };
101 
102 // Offsets relative to msc_regbase
104 #define EFM32_MSC_WRITECTRL_WREN_MASK 0x0001
106 
107 #define EFM32_MSC_WRITECMD_LADDRIM_MASK 0x0001
108 #define EFM32_MSC_WRITECMD_ERASEPAGE_MASK 0x0002
109 #define EFM32_MSC_WRITECMD_WRITEONCE_MASK 0x0008
114 
115 #define EFM32_MSC_STATUS_BUSY_MASK 0x0001
116 #define EFM32_MSC_STATUS_LOCKED_MASK 0x0002
117 #define EFM32_MSC_STATUS_INVADDR_MASK 0x0004
118 #define EFM32_MSC_STATUS_WDATAREADY_MASK 0x0008
120 
121 #define EFM32_MSC_LOCK_LOCKKEY 0x1b71
123 
124  const uint8_t *flash_write_code;
126 };
127 
128 // see contrib/loaders/flash/efm32.S for source
129 static const uint8_t efm32_flash_write_code_s0_s1[] = {
130 #include "../../../contrib/loaders/flash/silabs/silabs_s0_s1.inc"
131 };
132 
133 static const uint8_t efm32_flash_write_code_s2[] = {
134 #include "../../../contrib/loaders/flash/silabs/silabs_s2.inc"
135 };
136 
137 static const struct efm32_msc_offset efm32_msc_offset[] = {
138  [0] = {
139  .off_writectrl = 0x0008,
140  .off_writecmd = 0x000c,
141  .off_addrb = 0x0010,
142  .off_wdata = 0x0018,
143  .off_status = 0x001c,
144  .off_lock = 0x003c,
145  .off_userdatasize = 0x0000, // Does not exist in Series 0/1
146  .flash_write_code = efm32_flash_write_code_s0_s1,
147  .flash_write_code_len = sizeof(efm32_flash_write_code_s0_s1),
148  },
149  [1] = {
150  .off_writectrl = 0x0008,
151  .off_writecmd = 0x000c,
152  .off_addrb = 0x0010,
153  .off_wdata = 0x0018,
154  .off_status = 0x001c,
155  .off_lock = 0x0040,
156  .off_userdatasize = 0x0000, // Does not exist in Series 0/1
157  .flash_write_code = efm32_flash_write_code_s0_s1,
158  .flash_write_code_len = sizeof(efm32_flash_write_code_s0_s1),
159  },
160  [2] = {
161  .off_writectrl = 0x000c,
162  .off_writecmd = 0x0010,
163  .off_addrb = 0x0014,
164  .off_wdata = 0x0018,
165  .off_status = 0x001c,
166  .off_lock = 0x003c,
167  .off_userdatasize = 0x0034,
168  .flash_write_code = efm32_flash_write_code_s2,
169  .flash_write_code_len = sizeof(efm32_flash_write_code_s2),
170  },
171 };
172 
173 // Series 2 only
174 #define EFM32_CMU_REG_CLKEN1_SET 0x50009068
175 
181 };
182 
184 {
185  switch (base) {
186  case EFM32_FLASH_BASE_V1:
187  case EFM32_FLASH_BASE_V2:
188  return EFM32_BANK_INDEX_MAIN;
189  case EFM32_MSC_USER_DATA:
191  case EFM32_MSC_LOCK_BITS:
193  default:
194  return ERROR_FAIL;
195  }
196 }
197 
199  uint8_t part_id;
200  int series;
201  const char *name;
202  uint32_t msc_regbase;
203 
204  // Page size in bytes, or 0 to read from msc_di->page_size
206 };
207 
208 struct efm32_info {
212  uint16_t part_num; // Series 0/1 only
213  uint32_t part_info; // Series 2 only
214  uint8_t part_rev;
215  uint16_t flash_sz_kib;
216  uint16_t ram_sz_kib;
217  uint16_t page_size;
218  uint16_t page_size_ud;
219 };
220 
222  struct efm32_info info;
224  uint32_t lb_page[LOCKWORDS_SZ / 4];
225  uint32_t refcount;
226 };
227 
228 static const struct efm32_family_data efm32_families[] = {
229  { 16, 1, "EFR32MG1P Mighty", .msc_regbase = 0x400e0000 },
230  { 17, 1, "EFR32MG1B Mighty", .msc_regbase = 0x400e0000 },
231  { 18, 1, "EFR32MG1V Mighty", .msc_regbase = 0x400e0000 },
232  { 19, 1, "EFR32BG1P Blue", .msc_regbase = 0x400e0000 },
233  { 20, 1, "EFR32BG1B Blue", .msc_regbase = 0x400e0000 },
234  { 21, 1, "EFR32BG1V Blue", .msc_regbase = 0x400e0000 },
235  { 25, 1, "EFR32FG1P Flex", .msc_regbase = 0x400e0000 },
236  { 26, 1, "EFR32FG1B Flex", .msc_regbase = 0x400e0000 },
237  { 27, 1, "EFR32FG1V Flex", .msc_regbase = 0x400e0000 },
238  { 28, 1, "EFR32MG12P Mighty", .msc_regbase = 0x400e0000 },
239  { 29, 1, "EFR32MG12B Mighty", .msc_regbase = 0x400e0000 },
240  { 30, 1, "EFR32MG12V Mighty", .msc_regbase = 0x400e0000 },
241  { 31, 1, "EFR32BG12P Blue", .msc_regbase = 0x400e0000 },
242  { 32, 1, "EFR32BG12B Blue", .msc_regbase = 0x400e0000 },
243  { 33, 1, "EFR32BG12V Blue", .msc_regbase = 0x400e0000 },
244  { 37, 1, "EFR32FG12P Flex", .msc_regbase = 0x400e0000 },
245  { 38, 1, "EFR32FG12B Flex", .msc_regbase = 0x400e0000 },
246  { 39, 1, "EFR32FG12V Flex", .msc_regbase = 0x400e0000 },
247  { 40, 1, "EFR32MG13P Mighty", .msc_regbase = 0x400e0000 },
248  { 41, 1, "EFR32MG13B Mighty", .msc_regbase = 0x400e0000 },
249  { 42, 1, "EFR32MG13V Mighty", .msc_regbase = 0x400e0000 },
250  { 43, 1, "EFR32BG13P Blue", .msc_regbase = 0x400e0000 },
251  { 44, 1, "EFR32BG13B Blue", .msc_regbase = 0x400e0000 },
252  { 45, 1, "EFR32BG13V Blue", .msc_regbase = 0x400e0000 },
253  { 46, 1, "EFR32ZG13P Zen", .msc_regbase = 0x400e0000 },
254  { 49, 1, "EFR32FG13P Flex", .msc_regbase = 0x400e0000 },
255  { 50, 1, "EFR32FG13B Flex", .msc_regbase = 0x400e0000 },
256  { 51, 1, "EFR32FG13V Flex", .msc_regbase = 0x400e0000 },
257  { 52, 1, "EFR32MG14P Mighty", .msc_regbase = 0x400e0000 },
258  { 53, 1, "EFR32MG14B Mighty", .msc_regbase = 0x400e0000 },
259  { 54, 1, "EFR32MG14V Mighty", .msc_regbase = 0x400e0000 },
260  { 55, 1, "EFR32BG14P Blue", .msc_regbase = 0x400e0000 },
261  { 56, 1, "EFR32BG14B Blue", .msc_regbase = 0x400e0000 },
262  { 57, 1, "EFR32BG14V Blue", .msc_regbase = 0x400e0000 },
263  { 58, 1, "EFR32ZG14P Zen", .msc_regbase = 0x400e0000 },
264  { 61, 1, "EFR32FG14P Flex", .msc_regbase = 0x400e0000 },
265  { 62, 1, "EFR32FG14B Flex", .msc_regbase = 0x400e0000 },
266  { 63, 1, "EFR32FG14V Flex", .msc_regbase = 0x400e0000 },
267  { 71, 0, "EFM32G", .msc_regbase = 0x400c0000, .page_size = 512 },
268  { 72, 0, "EFM32GG Giant", .msc_regbase = 0x400c0000 },
269  { 73, 0, "EFM32TG Tiny", .msc_regbase = 0x400c0000, .page_size = 512 },
270  { 74, 0, "EFM32LG Leopard", .msc_regbase = 0x400c0000 },
271  { 75, 0, "EFM32WG Wonder", .msc_regbase = 0x400c0000 },
272  { 76, 0, "EFM32ZG Zero", .msc_regbase = 0x400c0000, .page_size = 1024 },
273  { 77, 0, "EFM32HG Happy", .msc_regbase = 0x400c0000, .page_size = 1024 },
274  { 81, 1, "EFM32PG1B Pearl", .msc_regbase = 0x400e0000 },
275  { 83, 1, "EFM32JG1B Jade", .msc_regbase = 0x400e0000 },
276  { 85, 1, "EFM32PG12B Pearl", .msc_regbase = 0x400e0000 },
277  { 87, 1, "EFM32JG12B Jade", .msc_regbase = 0x400e0000 },
278  { 89, 1, "EFM32PG13B Pearl", .msc_regbase = 0x400e0000 },
279  { 91, 1, "EFM32JG13B Jade", .msc_regbase = 0x400e0000 },
280  { 100, 1, "EFM32GG11B Giant", .msc_regbase = 0x40000000 },
281  { 103, 1, "EFM32TG11B Tiny", .msc_regbase = 0x40000000 },
282  { 106, 1, "EFM32GG12B Giant", .msc_regbase = 0x40000000 },
283  { 120, 0, "EZR32WG Wonder", .msc_regbase = 0x400c0000 },
284  { 121, 0, "EZR32LG Leopard", .msc_regbase = 0x400c0000 },
285  { 122, 0, "EZR32HG Happy", .msc_regbase = 0x400c0000, .page_size = 1024 },
286  { 128, 2, "EFR32/EFM32 Series-2", .msc_regbase = 0x50030000 },
287 };
288 
289 const struct flash_driver efm32_flash;
290 
291 static int efm32_priv_write(struct flash_bank *bank, const uint8_t *buffer,
292  uint32_t addr, uint32_t count);
293 
294 static int efm32_write_only_lockbits(struct flash_bank *bank);
295 
297  uint32_t *value)
298 {
299  struct efm32_flash_chip *efm32_info = bank->driver_priv;
300 
301  return target_read_u32(bank->target,
303  value);
304 }
305 
307  uint32_t value)
308 {
309  struct efm32_flash_chip *efm32_info = bank->driver_priv;
310 
311  return target_write_u32(bank->target,
313  value);
314 }
315 
316 static int efm32_read_info(struct flash_bank *bank)
317 {
318  struct efm32_flash_chip *efm32_info = bank->driver_priv;
319  struct efm32_info *efm32_mcu_info = &efm32_info->info;
320  int ret;
321 
322  memset(efm32_mcu_info, 0, sizeof(struct efm32_info));
323 
324  uint8_t val8;
325  ret = target_read_u8(bank->target, EFM32_DI_PART_FAMILY, &val8);
326  if (ret != ERROR_OK)
327  return ret;
328  for (size_t i = 0; i < ARRAY_SIZE(efm32_families); i++) {
329  if (efm32_families[i].part_id == val8)
330  efm32_mcu_info->family_data = &efm32_families[i];
331  }
332  if (!efm32_mcu_info->family_data) {
333  LOG_ERROR("Unknown MCU family %d", val8);
334  return ERROR_FAIL;
335  }
336 
337  efm32_mcu_info->di_addr = &efm32_dev_info_addr[efm32_mcu_info->family_data->series];
338  efm32_mcu_info->msc_offset = &efm32_msc_offset[efm32_mcu_info->family_data->series];
339 
340  if (efm32_mcu_info->family_data->series == 2) {
341  ret = target_read_u32(bank->target,
342  efm32_mcu_info->di_addr->part_info,
343  &efm32_mcu_info->part_info);
344  if (ret != ERROR_OK)
345  return ret;
346  } else {
347  ret = target_read_u16(bank->target,
348  efm32_mcu_info->di_addr->part_num,
349  &efm32_mcu_info->part_num);
350  if (ret != ERROR_OK)
351  return ret;
352  }
353 
354  ret = target_read_u8(bank->target,
355  efm32_mcu_info->di_addr->part_rev,
356  &efm32_mcu_info->part_rev);
357  if (ret != ERROR_OK)
358  return ret;
359 
360  ret = target_read_u16(bank->target,
361  efm32_mcu_info->di_addr->flash_sz,
362  &efm32_mcu_info->flash_sz_kib);
363  if (ret != ERROR_OK)
364  return ret;
365 
366  ret = target_read_u16(bank->target,
367  efm32_mcu_info->di_addr->ram_sz,
368  &efm32_mcu_info->ram_sz_kib);
369  if (ret != ERROR_OK)
370  return ret;
371 
372  efm32_mcu_info->page_size_ud = 0;
373  if (efm32_mcu_info->family_data->page_size != 0) {
374  efm32_mcu_info->page_size = efm32_mcu_info->family_data->page_size;
375  } else if ((efm32_mcu_info->family_data->part_id == 72 ||
376  efm32_mcu_info->family_data->part_id == 74) &&
377  efm32_mcu_info->part_rev < 18) {
378  /* EFM32 GG/LG errata: MEM_INFO_PAGE_SIZE is invalid for MCUs
379  * with part_rev < 18
380  */
381  if (efm32_mcu_info->flash_sz_kib < 512)
382  efm32_mcu_info->page_size = 2048;
383  else
384  efm32_mcu_info->page_size = 4096;
385  } else {
386  uint32_t val32;
387  ret = target_read_u32(bank->target,
388  efm32_mcu_info->di_addr->page_size,
389  &val32);
390  if (ret != ERROR_OK)
391  return ret;
392 
393  efm32_mcu_info->page_size = BIT(FIELD_GET(EFM32_DI_PARTINFO_PAGE_SZ_MASK, val32)) * 1024;
394  if (efm32_mcu_info->family_data->series == 2)
395  efm32_mcu_info->page_size_ud = FIELD_GET(EFM32_DI_PARTINFO_PAGE_SZ_UD_MASK, val32) * 1024;
396  }
397  if (!efm32_mcu_info->page_size_ud)
398  efm32_mcu_info->page_size_ud = efm32_mcu_info->page_size;
399 
400  if (efm32_mcu_info->page_size != 512 &&
401  efm32_mcu_info->page_size != 1024 &&
402  efm32_mcu_info->page_size != 2048 &&
403  efm32_mcu_info->page_size != 4096 &&
404  efm32_mcu_info->page_size != 8192) {
405  LOG_ERROR("Invalid page size %u", efm32_mcu_info->page_size);
406  return ERROR_FAIL;
407  }
408 
409  return ERROR_OK;
410 }
411 
412 /* flash bank efm32 <base> <size> 0 0 <target#> */
413 FLASH_BANK_COMMAND_HANDLER(efm32_flash_bank_command)
414 {
415  if (CMD_ARGC < 6)
417 
418  int bank_index = efm32_get_bank_index(bank->base);
419  if (bank_index < 0) {
420  LOG_ERROR("Flash bank with base address %" PRIx32 " is not supported",
421  (uint32_t)bank->base);
422  return ERROR_FAIL;
423  }
424 
425  /* look for an existing flash structure matching target */
426  struct efm32_flash_chip *efm32_info = NULL;
427  for (struct flash_bank *bank_iter = flash_bank_list();
428  bank_iter;
429  bank_iter = bank_iter->next) {
430  if (bank_iter->driver == &efm32_flash &&
431  bank_iter->target == bank->target &&
432  bank->driver_priv) {
433  efm32_info = bank->driver_priv;
434  break;
435  }
436  }
437 
438  if (!efm32_info) {
439  /* target not matched, make a new one */
440  efm32_info = calloc(1, sizeof(struct efm32_flash_chip));
441 
442  memset(efm32_info->lb_page, 0xff, LOCKWORDS_SZ);
443  }
444 
445  ++efm32_info->refcount;
446  bank->driver_priv = efm32_info;
447 
448  return ERROR_OK;
449 }
450 
456 {
457  struct efm32_flash_chip *efm32_info = bank->driver_priv;
458 
459  if (efm32_info) {
460  /* Use ref count to determine if it can be freed; scanning bank
461  * list doesn't work, because this function can be called after
462  * some banks in the list have been already destroyed.
463  */
464  --efm32_info->refcount;
465  if (efm32_info->refcount == 0) {
466  free(efm32_info);
467  bank->driver_priv = NULL;
468  }
469  }
470 }
471 
473 {
474  struct efm32_flash_chip *efm32_info = bank->driver_priv;
475 
476  if (efm32_info->info.family_data->series == 0 ||
477  efm32_info->info.family_data->series == 1)
478  return ERROR_OK;
479 
480  unsigned int s2_family = FIELD_GET(EFM32_DI_PARTINFO_FAMILY_MASK,
481  efm32_info->info.part_info);
482  uint32_t msc_clken;
483  switch (s2_family) {
484  case 21:
485  msc_clken = 0;
486  break;
487  case 22:
488  case 27:
489  case 29:
490  msc_clken = BIT(17);
491  break;
492  case 23:
493  case 24:
494  case 25:
495  case 26:
496  case 28:
497  msc_clken = BIT(16);
498  break;
499  default:
500  LOG_WARNING("Don't know EFR/EFM Gx family number, can't set MSC register. Use default values..");
501  msc_clken = BIT(16);
502  }
503  int ret = target_write_u32(bank->target,
505  msc_clken);
506  if (ret != ERROR_OK) {
507  LOG_ERROR("Failed to enable MSC clock");
508  return ret;
509  }
510 
511  return ERROR_OK;
512 }
513 
514 /* set or reset given bits in a register */
515 static int efm32_set_reg_bits(struct flash_bank *bank, uint32_t reg,
516  uint32_t bitmask, int set)
517 {
518  int ret = 0;
519  uint32_t reg_val = 0;
520 
521  ret = efm32_read_reg_u32(bank, reg, &reg_val);
522  if (ret != ERROR_OK)
523  return ret;
524 
525  if (set)
526  reg_val |= bitmask;
527  else
528  reg_val &= ~bitmask;
529 
530  return efm32_write_reg_u32(bank, reg, reg_val);
531 }
532 
533 static int efm32_set_wren(struct flash_bank *bank, int write_enable)
534 {
535  struct efm32_flash_chip *efm32_info = bank->driver_priv;
536 
537  return efm32_set_reg_bits(bank,
539  EFM32_MSC_WRITECTRL_WREN_MASK, write_enable);
540 }
541 
542 static int efm32_msc_lock(struct flash_bank *bank, int lock)
543 {
544  struct efm32_flash_chip *efm32_info = bank->driver_priv;
545 
546  return efm32_write_reg_u32(bank,
548  lock ? 0 : EFM32_MSC_LOCK_LOCKKEY);
549 }
550 
551 static int efm32_wait_status(struct flash_bank *bank, int timeout_ms,
552  uint32_t wait_mask, bool wait_for_set)
553 {
554  struct efm32_flash_chip *efm32_info = bank->driver_priv;
555  int64_t start_ms = timeval_ms();
556  uint32_t status = 0;
557 
558  while (1) {
559  int ret = efm32_read_reg_u32(bank,
561  &status);
562  if (ret != ERROR_OK)
563  return ret;
564 
565  LOG_DEBUG("status: 0x%" PRIx32, status);
566 
567  if (!(status & wait_mask) && !wait_for_set)
568  break;
569  if ((status & wait_mask) && wait_for_set)
570  break;
571 
572  if (timeval_ms() - start_ms > timeout_ms) {
573  LOG_ERROR("timed out waiting for MSC status");
574  return ERROR_FAIL;
575  }
576 
577  alive_sleep(1);
578  }
579 
580  return ERROR_OK;
581 }
582 
583 static int efm32_erase_page(struct flash_bank *bank, uint32_t addr)
584 {
585  /* this function DOES NOT set WREN; must be set already */
586  /* 1. write address to ADDRB
587  2. write LADDRIM
588  3. check status (INVADDR, LOCKED)
589  4. write ERASEPAGE
590  5. wait until !STATUS_BUSY
591  */
592  struct efm32_flash_chip *efm32_info = bank->driver_priv;
593  int ret;
594 
595  LOG_DEBUG("erasing flash page at 0x%08" PRIx32, addr);
596 
599  addr);
600  if (ret != ERROR_OK)
601  return ret;
602 
603  ret = efm32_set_reg_bits(bank,
606  if (ret != ERROR_OK)
607  return ret;
608 
609  uint32_t status;
610  ret = efm32_read_reg_u32(bank,
612  &status);
613  if (ret != ERROR_OK)
614  return ret;
615 
616  LOG_DEBUG("status 0x%" PRIx32, status);
617 
619  LOG_ERROR("Page is locked");
620  return ERROR_FAIL;
621  } else if (status & EFM32_MSC_STATUS_INVADDR_MASK) {
622  LOG_ERROR("Invalid address 0x%" PRIx32, addr);
623  return ERROR_FAIL;
624  }
625 
626  ret = efm32_set_reg_bits(bank,
629  if (ret != ERROR_OK)
630  return ret;
631 
634 }
635 
636 static int efm32_erase(struct flash_bank *bank, unsigned int first,
637  unsigned int last)
638 {
639  struct target *target = bank->target;
640  int ret = 0;
641 
642  if (target->state != TARGET_HALTED) {
643  LOG_ERROR("Target not halted");
645  }
646 
648  if (ret != ERROR_OK) {
649  LOG_ERROR("Failed to enable MSC clock");
650  return ret;
651  }
652 
653  efm32_msc_lock(bank, 0);
654  ret = efm32_set_wren(bank, 1);
655  if (ret != ERROR_OK) {
656  LOG_ERROR("Failed to enable MSC write");
657  return ret;
658  }
659 
660  for (unsigned int i = first; i <= last; i++) {
661  ret = efm32_erase_page(bank, bank->base + bank->sectors[i].offset);
662  if (ret != ERROR_OK)
663  LOG_ERROR("Failed to erase page %d", i);
664  }
665 
666  ret = efm32_set_wren(bank, 0);
667  efm32_msc_lock(bank, 1);
668  if (ret != ERROR_OK)
669  return ret;
670 
671  if (bank->base == EFM32_MSC_LOCK_BITS) {
673  if (ret != ERROR_OK)
674  LOG_ERROR("Failed to restore lockbits after erase");
675  }
676 
677  return ret;
678 }
679 
681 {
682  struct efm32_flash_chip *efm32_info = bank->driver_priv;
683  struct target *target = bank->target;
684  int data_size = 0;
685  uint32_t *ptr = NULL;
686  int ret = 0;
687 
688  assert(bank->num_sectors > 0);
689 
690  /* calculate the number of 32-bit words to read (one lock bit per sector) */
691  data_size = (bank->num_sectors + 31) / 32;
692 
693  ptr = efm32_info->lb_page;
694 
695  for (int i = 0; i < data_size; i++, ptr++) {
696  ret = target_read_u32(target, EFM32_MSC_LOCK_BITS + i * 4, ptr);
697  if (ret != ERROR_OK) {
698  LOG_ERROR("Failed to read PLW %d", i);
699  return ret;
700  }
701  }
702 
703  /* also, read ULW, DLW, MLW, ALW and CLW words */
704 
705  /* ULW, word 126 */
706  ptr = efm32_info->lb_page + 126;
707  ret = target_read_u32(target, EFM32_MSC_LOCK_BITS + 126 * 4, ptr);
708  if (ret != ERROR_OK) {
709  LOG_ERROR("Failed to read ULW");
710  return ret;
711  }
712 
713  /* DLW, word 127 */
714  ptr = efm32_info->lb_page + 127;
715  ret = target_read_u32(target, EFM32_MSC_LOCK_BITS + 127 * 4, ptr);
716  if (ret != ERROR_OK) {
717  LOG_ERROR("Failed to read DLW");
718  return ret;
719  }
720 
721  /* MLW, word 125, present in GG, LG, PG, JG, EFR32 */
722  ptr = efm32_info->lb_page + 125;
723  ret = target_read_u32(target, EFM32_MSC_LOCK_BITS + 125 * 4, ptr);
724  if (ret != ERROR_OK) {
725  LOG_ERROR("Failed to read MLW");
726  return ret;
727  }
728 
729  /* ALW, word 124, present in GG, LG, PG, JG, EFR32 */
730  ptr = efm32_info->lb_page + 124;
731  ret = target_read_u32(target, EFM32_MSC_LOCK_BITS + 124 * 4, ptr);
732  if (ret != ERROR_OK) {
733  LOG_ERROR("Failed to read ALW");
734  return ret;
735  }
736 
737  /* CLW1, word 123, present in EFR32 */
738  ptr = efm32_info->lb_page + 123;
739  ret = target_read_u32(target, EFM32_MSC_LOCK_BITS + 123 * 4, ptr);
740  if (ret != ERROR_OK) {
741  LOG_ERROR("Failed to read CLW1");
742  return ret;
743  }
744 
745  /* CLW0, word 122, present in GG, LG, PG, JG, EFR32 */
746  ptr = efm32_info->lb_page + 122;
747  ret = target_read_u32(target, EFM32_MSC_LOCK_BITS + 122 * 4, ptr);
748  if (ret != ERROR_OK) {
749  LOG_ERROR("Failed to read CLW0");
750  return ret;
751  }
752 
753  return ERROR_OK;
754 }
755 
757 {
758  struct efm32_flash_chip *efm32_info = bank->driver_priv;
759 
760  return efm32_priv_write(bank, (uint8_t *)efm32_info->lb_page,
762 }
763 
765 {
766  struct efm32_flash_chip *efm32_info = bank->driver_priv;
767 
768  /* Preserve any data written to the high portion of the lockbits page */
769  assert(efm32_info->info.page_size >= LOCKWORDS_SZ);
770 
771  uint32_t extra_bytes = efm32_info->info.page_size - LOCKWORDS_SZ;
772  uint8_t *extra_data = NULL;
773  int ret;
774  if (extra_bytes) {
775  extra_data = malloc(extra_bytes);
776  ret = target_read_buffer(bank->target,
778  extra_bytes, extra_data);
779  if (ret != ERROR_OK) {
780  LOG_ERROR("Failed to read extra contents of LB page");
781  free(extra_data);
782  return ret;
783  }
784  }
785 
787  if (ret != ERROR_OK) {
788  LOG_ERROR("Failed to erase LB page");
789  if (extra_data)
790  free(extra_data);
791  return ret;
792  }
793 
794  if (extra_data) {
795  ret = efm32_priv_write(bank, extra_data,
797  extra_bytes);
798  free(extra_data);
799  if (ret != ERROR_OK) {
800  LOG_ERROR("Failed to restore extra contents of LB page");
801  return ret;
802  }
803  }
804 
806 }
807 
808 static int efm32_get_page_lock(struct flash_bank *bank, size_t page)
809 {
810  struct efm32_flash_chip *efm32_info = bank->driver_priv;
811  uint32_t dw = 0;
812  uint32_t mask = 0;
813 
814  switch (bank->base) {
815  case EFM32_FLASH_BASE_V1:
816  case EFM32_FLASH_BASE_V2:
817  dw = efm32_info->lb_page[page >> 5];
818  mask = BIT(page & 0x1f);
819  break;
820  case EFM32_MSC_USER_DATA:
821  dw = efm32_info->lb_page[126];
822  mask = BIT(0);
823  break;
824  case EFM32_MSC_LOCK_BITS:
825  dw = efm32_info->lb_page[126];
826  mask = BIT(1);
827  break;
828  }
829 
830  return (dw & mask) ? 0 : 1;
831 }
832 
833 static int efm32_set_page_lock(struct flash_bank *bank, size_t page, int set)
834 {
835  struct efm32_flash_chip *efm32_info = bank->driver_priv;
836 
837  if (bank->base != EFM32_FLASH_BASE_V1 &&
838  bank->base != EFM32_FLASH_BASE_V2) {
839  LOG_ERROR("Locking user and lockbits pages is not supported yet");
840  return ERROR_FAIL;
841  }
842 
843  uint32_t *dw = &efm32_info->lb_page[page >> 5];
844  uint32_t mask = BIT(page & 0x1f);
845  if (!set)
846  *dw |= mask;
847  else
848  *dw &= ~mask;
849 
850  return ERROR_OK;
851 }
852 
853 static int efm32_protect(struct flash_bank *bank, int set, unsigned int first,
854  unsigned int last)
855 {
856  struct efm32_flash_chip *efm32_info = bank->driver_priv;
857  struct target *target = bank->target;
858  int ret = 0;
859 
860  if (efm32_info->info.family_data->series == 2)
862 
863  if (target->state != TARGET_HALTED) {
864  LOG_ERROR("Target not halted");
866  }
867 
868  ret = efm32_read_lock_data(bank);
869  if (ret != ERROR_OK) {
870  LOG_ERROR("Failed to read LB data");
871  return ret;
872  }
873 
874  for (unsigned int i = first; i <= last; i++) {
875  ret = efm32_set_page_lock(bank, i, set);
876  if (ret != ERROR_OK) {
877  LOG_ERROR("Failed to set lock on page %d", i);
878  return ret;
879  }
880  }
881 
883  if (ret != ERROR_OK) {
884  LOG_ERROR("Failed to write LB page");
885  return ret;
886  }
887 
888  return ERROR_OK;
889 }
890 
891 static int efm32_write_block(struct flash_bank *bank, const uint8_t *buf,
892  uint32_t address, uint32_t count)
893 {
894  struct efm32_flash_chip *efm32_info = bank->driver_priv;
895  struct target *target = bank->target;
896  struct working_area *write_algorithm, *source;
897  int ret;
898 
899  /* flash write code */
902  &write_algorithm);
903  if (ret) {
904  LOG_WARNING("no working area available, can't do block memory writes");
906  }
907  ret = target_write_buffer(target, write_algorithm->address,
910  if (ret)
911  return ret;
912 
913  /* memory buffer */
915  if (buffer_size <= 256) {
916  LOG_WARNING("no large enough working area available, can't do block memory writes");
918  }
919  buffer_size = MIN(buffer_size, 16 * 1024);
921  if (ret)
922  return ret;
923 
924  struct reg_param reg_params[5];
925  init_reg_param(&reg_params[0], "r0", 32, PARAM_IN_OUT); /* flash base (in), status (out) */
926  init_reg_param(&reg_params[1], "r1", 32, PARAM_OUT); /* count (word-32bit) */
927  init_reg_param(&reg_params[2], "r2", 32, PARAM_OUT); /* buffer start */
928  init_reg_param(&reg_params[3], "r3", 32, PARAM_OUT); /* buffer end */
929  init_reg_param(&reg_params[4], "r4", 32, PARAM_IN_OUT); /* target address */
930 
931  buf_set_u32(reg_params[0].value, 0, 32, efm32_info->info.family_data->msc_regbase);
932  buf_set_u32(reg_params[1].value, 0, 32, count);
933  buf_set_u32(reg_params[2].value, 0, 32, source->address);
934  buf_set_u32(reg_params[3].value, 0, 32, source->address + source->size);
935  buf_set_u32(reg_params[4].value, 0, 32, address);
936 
937  struct armv7m_algorithm armv7m_info = {
939  .core_mode = ARM_MODE_THREAD,
940  };
942  0, NULL,
943  5, reg_params,
944  source->address, source->size,
945  write_algorithm->address, 0,
946  &armv7m_info);
947 
948  if (ret == ERROR_FLASH_OPERATION_FAILED) {
949  LOG_ERROR("flash write failed at address 0x%" PRIx32,
950  buf_get_u32(reg_params[4].value, 0, 32));
951 
952  if (buf_get_u32(reg_params[0].value, 0, 32) &
954  LOG_ERROR("flash memory write protected");
955  }
956 
957  if (buf_get_u32(reg_params[0].value, 0, 32) &
959  LOG_ERROR("invalid flash memory write address");
960  }
961  }
962 
964  target_free_working_area(target, write_algorithm);
965 
966  destroy_reg_param(&reg_params[0]);
967  destroy_reg_param(&reg_params[1]);
968  destroy_reg_param(&reg_params[2]);
969  destroy_reg_param(&reg_params[3]);
970  destroy_reg_param(&reg_params[4]);
971 
972  return ret;
973 }
974 
975 static int efm32_write_word(struct flash_bank *bank, uint32_t addr,
976  uint32_t val)
977 {
978  /* this function DOES NOT set WREN; must be set already */
979  /* 1. write address to ADDRB
980  2. write LADDRIM
981  3. check status (INVADDR, LOCKED)
982  4. wait for WDATAREADY
983  5. write data to WDATA
984  6. write WRITECMD_WRITEONCE to WRITECMD
985  7. wait until !STATUS_BUSY
986  */
987 
988  /* FIXME: EFM32G ref states (7.3.2) that writes should be
989  * performed twice per dword */
990  struct efm32_flash_chip *efm32_info = bank->driver_priv;
991  int ret;
992 
993  /* if not called, GDB errors will be reported during large writes */
994  keep_alive();
995 
998  addr);
999  if (ret != ERROR_OK)
1000  return ret;
1001 
1002  ret = efm32_set_reg_bits(bank,
1005  if (ret != ERROR_OK)
1006  return ret;
1007 
1008  uint32_t status;
1009  ret = efm32_read_reg_u32(bank,
1011  &status);
1012  if (ret != ERROR_OK)
1013  return ret;
1014 
1015  LOG_DEBUG("status 0x%" PRIx32, status);
1016 
1018  LOG_ERROR("Page is locked");
1019  return ERROR_FAIL;
1020  } else if (status & EFM32_MSC_STATUS_INVADDR_MASK) {
1021  LOG_ERROR("Invalid address 0x%" PRIx32, addr);
1022  return ERROR_FAIL;
1023  }
1024 
1027  if (ret != ERROR_OK) {
1028  LOG_ERROR("Wait for WDATAREADY failed");
1029  return ret;
1030  }
1031 
1032  ret = efm32_write_reg_u32(bank,
1034  val);
1035  if (ret != ERROR_OK) {
1036  LOG_ERROR("WDATA write failed");
1037  return ret;
1038  }
1039 
1040  ret = efm32_write_reg_u32(bank,
1043  if (ret != ERROR_OK) {
1044  LOG_ERROR("WRITECMD write failed");
1045  return ret;
1046  }
1047 
1050  if (ret != ERROR_OK) {
1051  LOG_ERROR("Wait for BUSY failed");
1052  return ret;
1053  }
1054 
1055  return ERROR_OK;
1056 }
1057 
1058 static int efm32_priv_write(struct flash_bank *bank, const uint8_t *buffer,
1059  uint32_t addr, uint32_t count)
1060 {
1061  struct target *target = bank->target;
1062  uint8_t *new_buffer = NULL;
1063 
1064  if (target->state != TARGET_HALTED) {
1065  LOG_ERROR("Target not halted");
1066  return ERROR_TARGET_NOT_HALTED;
1067  }
1068 
1069  if (addr & 0x3) {
1070  LOG_ERROR("addr 0x%" PRIx32 " breaks required 4-byte alignment", addr);
1072  }
1073 
1074  if (count & 0x3) {
1075  uint32_t old_count = count;
1076  count = (old_count | 3) + 1;
1077  new_buffer = malloc(count);
1078  if (!new_buffer) {
1079  LOG_ERROR("odd number of bytes to write and no memory for padding buffer");
1080  return ERROR_FAIL;
1081  }
1082  LOG_INFO("odd number of bytes to write (%" PRIu32 "), extending to %"
1083  PRIu32 " and padding with 0xff", old_count, count);
1084  memset(new_buffer, 0xff, count);
1085  buffer = memcpy(new_buffer, buffer, old_count);
1086  }
1087 
1088  uint32_t words_remaining = count / 4;
1089  int retval, retval2;
1090 
1091  retval = efm32_msc_clock_enable(bank);
1092  if (retval != ERROR_OK)
1093  goto cleanup;
1094 
1095  /* unlock flash registers */
1096  efm32_msc_lock(bank, 0);
1097  retval = efm32_set_wren(bank, 1);
1098  if (retval != ERROR_OK)
1099  goto cleanup;
1100 
1101  /* try using a block write */
1102  retval = efm32_write_block(bank, buffer, addr, words_remaining);
1103 
1104  if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE) {
1105  /* if block write failed (no sufficient working area),
1106  * we use normal (slow) single word accesses */
1107  LOG_WARNING("couldn't use block writes, falling back to single memory accesses");
1108 
1109  while (words_remaining > 0) {
1110  uint32_t value;
1111  memcpy(&value, buffer, sizeof(uint32_t));
1112 
1113  retval = efm32_write_word(bank, addr, value);
1114  if (retval != ERROR_OK)
1115  goto reset_pg_and_lock;
1116 
1117  words_remaining--;
1118  buffer += 4;
1119  addr += 4;
1120  }
1121  }
1122 
1123 reset_pg_and_lock:
1124  retval2 = efm32_set_wren(bank, 0);
1125  efm32_msc_lock(bank, 1);
1126  if (retval == ERROR_OK)
1127  retval = retval2;
1128 
1129 cleanup:
1130  free(new_buffer);
1131  return retval;
1132 }
1133 
1134 static int efm32_write(struct flash_bank *bank, const uint8_t *buffer,
1135  uint32_t offset, uint32_t count)
1136 {
1137  if (bank->base == EFM32_MSC_LOCK_BITS && offset < LOCKWORDS_SZ) {
1138  LOG_ERROR("Cannot write to lock words");
1139  return ERROR_FAIL;
1140  }
1141  return efm32_priv_write(bank, buffer, bank->base + offset, count);
1142 }
1143 
1144 static char *efm32_get_str_identifier(struct efm32_info *efm32_mcu_info,
1145  char *buf, size_t len)
1146 {
1147  if (!efm32_mcu_info->part_info) {
1148  snprintf(buf, len, "%s Gecko, rev %" PRIu8,
1149  efm32_mcu_info->family_data->name,
1150  efm32_mcu_info->part_rev);
1151  return buf;
1152  }
1153  unsigned int dev_num = FIELD_GET(EFM32_DI_PARTINFO_NUM_MASK,
1154  efm32_mcu_info->part_info);
1155  unsigned int dev_family = FIELD_GET(EFM32_DI_PARTINFO_FAMILY_MASK,
1156  efm32_mcu_info->part_info);
1157  unsigned int dev_type = FIELD_GET(EFM32_DI_PARTINFO_TYPE_MASK,
1158  efm32_mcu_info->part_info);
1159 
1160  const char *types = "FMBZxP";
1161  if (dev_type > strlen(types)) {
1162  snprintf(buf, len, "Unknown MCU family %u", dev_type);
1163  return buf;
1164  }
1165 
1166  char dev_num_letter = 'A' + (dev_num / 1000);
1167  unsigned int dev_num_digits = dev_num % 1000;
1168 
1169  snprintf(buf, len, "%s%cG%u %c%03u, rev %" PRIu8,
1170  types[dev_type] == 'P' ? "EFM32" : "EFR32",
1171  types[dev_type],
1172  dev_family,
1173  dev_num_letter,
1174  dev_num_digits,
1175  efm32_mcu_info->part_rev);
1176  return buf;
1177 }
1178 
1179 static int efm32_probe(struct flash_bank *bank)
1180 {
1181  struct efm32_flash_chip *efm32_info = bank->driver_priv;
1182  struct efm32_info *efm32_mcu_info = &efm32_info->info;
1183  int bank_index = efm32_get_bank_index(bank->base);
1184  uint32_t base_address = EFM32_FLASH_BASE_V2;
1185  char strbuf[256];
1186  int ret;
1187 
1188  assert(bank_index >= 0);
1189 
1190  efm32_info->probed[bank_index] = false;
1191  memset(efm32_info->lb_page, 0xff, LOCKWORDS_SZ);
1192 
1193  ret = efm32_read_info(bank);
1194  if (ret != ERROR_OK)
1195  return ret;
1196 
1197  if (efm32_mcu_info->family_data->series == 0 ||
1198  efm32_mcu_info->family_data->series == 1 ||
1199  FIELD_GET(EFM32_DI_PARTINFO_FAMILY_MASK, efm32_mcu_info->part_info) == 21 ||
1200  FIELD_GET(EFM32_DI_PARTINFO_FAMILY_MASK, efm32_mcu_info->part_info) == 22)
1201  base_address = EFM32_FLASH_BASE_V1;
1202 
1203  LOG_INFO("detected part: %s",
1204  efm32_get_str_identifier(efm32_mcu_info, strbuf, sizeof(strbuf)));
1205  LOG_INFO("flash size = %d KiB", efm32_mcu_info->flash_sz_kib);
1206  LOG_INFO("flash page size = %d B", efm32_mcu_info->page_size);
1207 
1208  assert(efm32_mcu_info->page_size != 0);
1209 
1210  free(bank->sectors);
1211  bank->sectors = NULL;
1212 
1213  uint32_t page_size;
1214  if (bank->base == base_address) { /* main flash */
1215  page_size = efm32_mcu_info->page_size;
1216  bank->num_sectors = efm32_mcu_info->flash_sz_kib * 1024 / page_size;
1217  assert(bank->num_sectors > 0);
1218  } else if (efm32_info->info.family_data->series != 2) {
1219  page_size = efm32_mcu_info->page_size_ud;
1220  bank->num_sectors = 1;
1221  } else {
1223  if (ret != ERROR_OK)
1224  return ret;
1225 
1226  uint32_t userdatasize;
1227  ret = efm32_read_reg_u32(bank,
1229  &userdatasize);
1230  if (ret != ERROR_OK) {
1231  LOG_ERROR("Failed to read page size");
1232  return ret;
1233  }
1234  page_size = efm32_mcu_info->page_size_ud;
1235  bank->num_sectors = (userdatasize * 256) / page_size;
1236  }
1237  bank->size = bank->num_sectors * page_size;
1238  bank->sectors = alloc_block_array(0, page_size, bank->num_sectors);
1239  if (!bank->sectors)
1240  return ERROR_FAIL;
1241 
1242  efm32_info->probed[bank_index] = true;
1243 
1244  return ERROR_OK;
1245 }
1246 
1247 static int efm32_auto_probe(struct flash_bank *bank)
1248 {
1249  struct efm32_flash_chip *efm32_info = bank->driver_priv;
1250  int bank_index = efm32_get_bank_index(bank->base);
1251 
1252  assert(bank_index >= 0);
1253 
1254  if (efm32_info->probed[bank_index])
1255  return ERROR_OK;
1256  return efm32_probe(bank);
1257 }
1258 
1260 {
1261  struct efm32_flash_chip *efm32_info = bank->driver_priv;
1262  struct target *target = bank->target;
1263  int ret = 0;
1264 
1265  if (efm32_info->info.family_data->series == 2)
1267 
1268  if (target->state != TARGET_HALTED) {
1269  LOG_ERROR("Target not halted");
1270  return ERROR_TARGET_NOT_HALTED;
1271  }
1272 
1273  ret = efm32_read_lock_data(bank);
1274  if (ret != ERROR_OK) {
1275  LOG_ERROR("Failed to read LB data");
1276  return ret;
1277  }
1278 
1279  assert(bank->sectors);
1280 
1281  for (unsigned int i = 0; i < bank->num_sectors; i++)
1282  bank->sectors[i].is_protected = efm32_get_page_lock(bank, i);
1283 
1284  return ERROR_OK;
1285 }
1286 
1288 {
1289  struct efm32_flash_chip *efm32_info = bank->driver_priv;
1290  char strbuf[256];
1291  int ret;
1292 
1293  ret = efm32_read_info(bank);
1294  if (ret != ERROR_OK) {
1295  LOG_ERROR("Failed to read EFM32 info");
1296  return ret;
1297  }
1298 
1301  strbuf,
1302  sizeof(strbuf)));
1303  return ERROR_OK;
1304 }
1305 
1306 COMMAND_HANDLER(efm32_handle_debuglock_command)
1307 {
1308  if (CMD_ARGC < 1)
1310 
1311  struct flash_bank *bank;
1312  int ret = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
1313  if (ret != ERROR_OK)
1314  return ret;
1315 
1316  struct efm32_flash_chip *efm32_info = bank->driver_priv;
1317  struct target *target = bank->target;
1318 
1319  if (target->state != TARGET_HALTED) {
1320  LOG_ERROR("Target not halted");
1321  return ERROR_TARGET_NOT_HALTED;
1322  }
1323 
1324  uint32_t *ptr = efm32_info->lb_page + 127;
1325  *ptr = 0;
1326 
1328  if (ret != ERROR_OK) {
1329  LOG_ERROR("Failed to enable MSC clock");
1330  return ret;
1331  }
1332 
1333  ret = efm32_write_lock_data(bank);
1334  if (ret != ERROR_OK) {
1335  LOG_ERROR("Failed to write LB page");
1336  return ret;
1337  }
1338 
1339  command_print(CMD, "efm32 debug interface locked, reset the device to apply");
1340 
1341  return ERROR_OK;
1342 }
1343 
1344 static const struct command_registration efm32_exec_command_handlers[] = {
1345  {
1346  .name = "debuglock",
1347  .handler = efm32_handle_debuglock_command,
1348  .mode = COMMAND_EXEC,
1349  .usage = "bank_id",
1350  .help = "Lock the debug interface of the device.",
1351  },
1353 };
1354 
1355 static const struct command_registration efm32_command_handlers[] = {
1356  {
1357  .name = "efm32",
1358  .mode = COMMAND_ANY,
1359  .help = "Silicon Labs (EFM32 and EFR32) flash command group",
1360  .usage = "",
1361  .chain = efm32_exec_command_handlers,
1362  },
1364 };
1365 
1366 const struct flash_driver efm32_flash = {
1367  .name = "efm32",
1368  .commands = efm32_command_handlers,
1369  .flash_bank_command = efm32_flash_bank_command,
1370  .erase = efm32_erase,
1371  .protect = efm32_protect,
1372  .write = efm32_write,
1373  .read = default_flash_read,
1374  .probe = efm32_probe,
1375  .auto_probe = efm32_auto_probe,
1376  .erase_check = default_flash_blank_check,
1377  .protect_check = efm32_protect_check,
1378  .info = efm32_get_info,
1379  .free_driver_priv = efm32_free_driver_priv,
1380 };
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
#define FIELD_GET(_mask, _value)
FIELD_GET(_mask, _value) - Extract a value from a bitfield @_mask: Bitfield mask @_value: Bitfield va...
Definition: bitfield.h:57
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 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_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:256
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
uint64_t buffer
Pointer to data buffer to send over SPI.
Definition: dw-spi-helper.h:0
uint32_t page_size
Page size.
Definition: dw-spi-helper.h:3
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
#define EFM32_DI_PARTINFO_PAGE_SZ_MASK
Definition: efm32.c:66
#define EFM32_DI_PART_FAMILY
Definition: efm32.c:73
static const struct efm32_family_data efm32_families[]
Definition: efm32.c:228
static int efm32_get_bank_index(target_addr_t base)
Definition: efm32.c:183
static int efm32_set_reg_bits(struct flash_bank *bank, uint32_t reg, uint32_t bitmask, int set)
Definition: efm32.c:515
static int efm32_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
Definition: efm32.c:636
static int efm32_set_page_lock(struct flash_bank *bank, size_t page, int set)
Definition: efm32.c:833
static int efm32_protect_check(struct flash_bank *bank)
Definition: efm32.c:1259
#define EFM32_MSC_INFO_BASE
Definition: efm32.c:54
COMMAND_HANDLER(efm32_handle_debuglock_command)
Definition: efm32.c:1306
static int efm32_wait_status(struct flash_bank *bank, int timeout_ms, uint32_t wait_mask, bool wait_for_set)
Definition: efm32.c:551
#define EFM32_FLASH_OPERATION_TIMEOUT
Definition: efm32.c:46
static int efm32_get_page_lock(struct flash_bank *bank, size_t page)
Definition: efm32.c:808
#define EFM32_DI_PARTINFO_TYPE_MASK
Definition: efm32.c:64
#define EFM32_DI_PARTINFO_FAMILY_MASK
Definition: efm32.c:63
#define EFM32_MSC_LOCK_BITS
Definition: efm32.c:56
static int efm32_set_wren(struct flash_bank *bank, int write_enable)
Definition: efm32.c:533
static int efm32_write_word(struct flash_bank *bank, uint32_t addr, uint32_t val)
Definition: efm32.c:975
static int efm32_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
Definition: efm32.c:1134
static int efm32_write_lock_data(struct flash_bank *bank)
Definition: efm32.c:764
#define EFM32_CMU_REG_CLKEN1_SET
Definition: efm32.c:174
static int efm32_msc_clock_enable(struct flash_bank *bank)
Definition: efm32.c:472
static int efm32_write_block(struct flash_bank *bank, const uint8_t *buf, uint32_t address, uint32_t count)
Definition: efm32.c:891
#define EFM32_MSC_USER_DATA
Definition: efm32.c:55
#define EFM32_MSC_STATUS_WDATAREADY_MASK
Definition: efm32.c:118
#define EFM32_MSC_STATUS_BUSY_MASK
Definition: efm32.c:115
static int efm32_protect(struct flash_bank *bank, int set, unsigned int first, unsigned int last)
Definition: efm32.c:853
static int efm32_read_lock_data(struct flash_bank *bank)
Definition: efm32.c:680
static int efm32_probe(struct flash_bank *bank)
Definition: efm32.c:1179
#define EFM32_MSC_LOCK_BITS_EXTRA
Definition: efm32.c:57
#define EFM32_MSC_WRITECMD_LADDRIM_MASK
Definition: efm32.c:107
static int efm32_write_only_lockbits(struct flash_bank *bank)
Definition: efm32.c:756
#define EFM32_MSC_LOCK_LOCKKEY
Definition: efm32.c:121
static int efm32_priv_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t addr, uint32_t count)
Definition: efm32.c:1058
FLASH_BANK_COMMAND_HANDLER(efm32_flash_bank_command)
Definition: efm32.c:413
#define EFM32_MSC_WRITECMD_WRITEONCE_MASK
Definition: efm32.c:109
static char * efm32_get_str_identifier(struct efm32_info *efm32_mcu_info, char *buf, size_t len)
Definition: efm32.c:1144
#define EFM32_MSC_WRITECTRL_WREN_MASK
Definition: efm32.c:104
static int efm32_erase_page(struct flash_bank *bank, uint32_t addr)
Definition: efm32.c:583
const struct flash_driver efm32_flash
Definition: efm32.c:289
static int efm32_msc_lock(struct flash_bank *bank, int lock)
Definition: efm32.c:542
static int efm32_write_reg_u32(struct flash_bank *bank, target_addr_t offset, uint32_t value)
Definition: efm32.c:306
static int efm32_auto_probe(struct flash_bank *bank)
Definition: efm32.c:1247
#define EFM32_DI_PARTINFO_NUM_MASK
Definition: efm32.c:62
#define LOCKWORDS_SZ
Definition: efm32.c:52
static void efm32_free_driver_priv(struct flash_bank *bank)
Remove flash structure corresponding to this bank, if and only if it's not used by any others.
Definition: efm32.c:455
#define EFM32_DI_PARTINFO_PAGE_SZ_UD_MASK
Definition: efm32.c:67
#define EFM32_FLASH_BASE_V1
Definition: efm32.c:48
efm32_bank_index
Definition: efm32.c:176
@ EFM32_BANK_INDEX_USER_DATA
Definition: efm32.c:178
@ EFM32_N_BANKS
Definition: efm32.c:180
@ EFM32_BANK_INDEX_MAIN
Definition: efm32.c:177
@ EFM32_BANK_INDEX_LOCK_BITS
Definition: efm32.c:179
static int efm32_get_info(struct flash_bank *bank, struct command_invocation *cmd)
Definition: efm32.c:1287
#define EFM32_FLASH_BASE_V2
Definition: efm32.c:49
static const struct command_registration efm32_command_handlers[]
Definition: efm32.c:1355
static const uint8_t efm32_flash_write_code_s0_s1[]
Definition: efm32.c:129
static const struct command_registration efm32_exec_command_handlers[]
Definition: efm32.c:1344
static int efm32_read_reg_u32(struct flash_bank *bank, target_addr_t offset, uint32_t *value)
Definition: efm32.c:296
#define EFM32_MSC_STATUS_INVADDR_MASK
Definition: efm32.c:117
#define EFM32_MSC_STATUS_LOCKED_MASK
Definition: efm32.c:116
static const uint8_t efm32_flash_write_code_s2[]
Definition: efm32.c:133
static int efm32_read_info(struct flash_bank *bank)
Definition: efm32.c:316
#define EFM32_MSC_WRITECMD_ERASEPAGE_MASK
Definition: efm32.c:108
uint8_t bank
Definition: esirisc.c:135
#define ERROR_FLASH_OPER_UNSUPPORTED
Definition: flash/common.h:36
#define ERROR_FLASH_OPERATION_FAILED
Definition: flash/common.h:30
#define ERROR_FLASH_DST_BREAKS_ALIGNMENT
Definition: flash/common.h:32
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.
struct flash_bank * flash_bank_list(void)
void alive_sleep(uint64_t ms)
Definition: log.c:478
void keep_alive(void)
Definition: log.c:437
#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
#define MIN(a, b)
Definition: replacements.h:22
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 BIT(nr)
Definition: stm32l4x.h:18
unsigned int common_magic
Definition: armv7m.h:306
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
target_addr_t ram_sz
Definition: efm32.c:70
target_addr_t part_rev
Definition: efm32.c:61
target_addr_t part_info
Definition: efm32.c:65
target_addr_t part_num
Definition: efm32.c:60
target_addr_t flash_sz
Definition: efm32.c:69
target_addr_t page_size
Definition: efm32.c:68
const char * name
Definition: efm32.c:201
uint8_t part_id
Definition: efm32.c:199
uint32_t msc_regbase
Definition: efm32.c:202
struct efm32_info info
Definition: efm32.c:222
uint32_t refcount
Definition: efm32.c:225
uint32_t lb_page[LOCKWORDS_SZ/4]
Definition: efm32.c:224
bool probed[EFM32_N_BANKS]
Definition: efm32.c:223
uint16_t part_num
Definition: efm32.c:212
const struct efm32_dev_info_addr * di_addr
Definition: efm32.c:210
uint32_t part_info
Definition: efm32.c:213
uint16_t ram_sz_kib
Definition: efm32.c:216
uint16_t page_size_ud
Definition: efm32.c:218
uint16_t flash_sz_kib
Definition: efm32.c:215
uint16_t page_size
Definition: efm32.c:217
const struct efm32_family_data * family_data
Definition: efm32.c:209
uint8_t part_rev
Definition: efm32.c:214
const struct efm32_msc_offset * msc_offset
Definition: efm32.c:211
target_addr_t off_status
Definition: efm32.c:119
target_addr_t off_writecmd
Definition: efm32.c:110
target_addr_t off_userdatasize
Definition: efm32.c:113
target_addr_t off_lock
Definition: efm32.c:122
size_t flash_write_code_len
Definition: efm32.c:125
target_addr_t off_writectrl
Definition: efm32.c:105
target_addr_t off_addrb
Definition: efm32.c:111
const uint8_t * flash_write_code
Definition: efm32.c:124
target_addr_t off_wdata
Definition: efm32.c:112
Provides details of a flash bank, available either on-chip or through a major interface.
Definition: nor/core.h:75
struct flash_bank * next
The next flash bank on this chip.
Definition: nor/core.h:130
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
uint8_t * value
Definition: algorithm.h:30
Definition: register.h:111
Definition: target.h:119
enum target_state state
Definition: target.h:167
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:2368
int target_read_buffer(struct target *target, target_addr_t address, uint32_t size, uint8_t *buffer)
Definition: target.c:2433
int target_read_u8(struct target *target, target_addr_t address, uint8_t *value)
Definition: target.c:2600
uint32_t target_get_working_area_avail(struct target *target)
Definition: target.c:2191
int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
Definition: target.c:2087
int target_write_u32(struct target *target, target_addr_t address, uint32_t value)
Definition: target.c:2634
int target_free_working_area(struct target *target, struct working_area *area)
Free a working area.
Definition: target.c:2145
int target_read_u16(struct target *target, target_addr_t address, uint16_t *value)
Definition: target.c:2580
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:943
int target_read_u32(struct target *target, target_addr_t address, uint32_t *value)
Definition: target.c:2560
#define ERROR_TARGET_NOT_HALTED
Definition: target.h:814
@ TARGET_HALTED
Definition: target.h:58
#define ERROR_TARGET_RESOURCE_NOT_AVAILABLE
Definition: target.h:818
int64_t timeval_ms(void)
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.
Definition: types.h:57
uint64_t target_addr_t
Definition: types.h:279
#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