OpenOCD
max32xxx.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2016 by Maxim Integrated *
5  * Copyright (C) 2025-2026 Analog Devices, Inc. *
6  ***************************************************************************/
7 
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11 
12 #include "imp.h"
13 #include "max32xxx_options.h"
14 #include <helper/binarybuffer.h>
15 #include <target/algorithm.h>
16 #include <target/armv7m.h>
17 #include <target/target.h>
18 #include <target/target_type.h>
19 
20 // Register addresses
21 #define FLC_ADDR 0x00000000
22 #define FLC_CLKDIV 0x00000004
23 #define FLC_CN 0x00000008
24 #define FLC_PR1E_ADDR 0x0000000C
25 #define FLC_PR2S_ADDR 0x00000010
26 #define FLC_PR2E_ADDR 0x00000014
27 #define FLC_PR3S_ADDR 0x00000018
28 #define FLC_PR3E_ADDR 0x0000001C
29 #define FLC_MD 0x00000020
30 #define FLC_INT 0x00000024
31 #define FLC_DATA0 0x00000030
32 #define FLC_DATA1 0x00000034
33 #define FLC_DATA2 0x00000038
34 #define FLC_DATA3 0x0000003C
35 #define FLC_BL_CTRL 0x00000170
36 #define FLC_PROT 0x00000300
37 
38 #define ARM_PID_REG 0xE00FFFE0
39 #define MAX326XX_ID_REG 0x40000838
40 
41 // Register settings
42 #define FLC_INT_AF 0x00000002
43 
44 #define FLC_CN_UNLOCK_MASK 0xF0000000
45 #define FLC_CN_UNLOCK_VALUE 0x20000000
46 
47 #define FLC_CN_PEND 0x01000000
48 #define FLC_CN_ERASE_CODE_MASK 0x0000FF00
49 #define FLC_CN_ERASE_CODE_PGE 0x00005500
50 #define FLC_CN_ERASE_CODE_ME 0x0000AA00
51 #define FLC_CN_32BIT 0x00000010
52 #define FLC_CN_PGE 0x00000004
53 #define FLC_CN_ME 0x00000002
54 #define FLC_CN_WR 0x00000001
55 #define FLC_CN_PGE 0x00000004
56 #define FLC_CN_ME 0x00000002
57 #define FLC_CN_WR 0x00000001
58 
59 #define FLC_BL_CTRL_23 0x00020000
60 #define FLC_BL_CTRL_IFREN 0x00000001
61 
62 #define MASK_FLASH_BUSY (0x048800E0 & ~0x04000000)
63 #define MASK_DISABLE_INTS (0xFFFFFCFC)
64 #define MASK_FLASH_UNLOCKED (0xF588FFEF & ~0x04000000)
65 #define MASK_FLASH_LOCK (0xF588FFEF & ~0x04000000)
66 #define MASK_FLASH_ERASE (0xF588FFEF & ~0x04000000)
67 #define MASK_FLASH_ERASED (0xF48800EB & ~0x04000000)
68 #define MASK_ACCESS_VIOLATIONS (0xFFFFFCFC)
69 #define MASK_FLASH_WRITE (0xF588FFEF & ~0x04000000)
70 #define MASK_WRITE_ALIGNED (0xF588FFEF & ~0x04000000)
71 #define MASK_WRITE_COMPLETE (0xF488FFEE & ~0x04000000)
72 #define MASK_WRITE_BURST (0xF588FFEF & ~0x04000000)
73 #define MASK_BURST_COMPLETE (0xF488FFEE & ~0x04000000)
74 #define MASK_WRITE_REMAINING (0xF588FFEF & ~0x04000000)
75 #define MASK_REMAINING_COMPLETE (0xF488FFEE & ~0x04000000)
76 #define MASK_MASS_ERASE (0xF588FFEF & ~0x04000000)
77 #define MASK_ERASE_COMPLETE (0xF48800ED & ~0x04000000)
78 
79 #define ARM_PID_DEFAULT_CM3 0x0000B4C3
80 #define ARM_PID_DEFAULT_CM4 0x0000B4C4
81 #define MAX326XX_ID 0x0000004D
82 
83 static int max32xxx_mass_erase(struct flash_bank *bank);
84 
86  bool probed;
87  bool max326xx;
88  unsigned int flash_size;
89  unsigned int flc_base;
90  unsigned int sector_size;
91  unsigned int clkdiv_value;
92  unsigned int int_state;
93  unsigned int options;
94 };
95 
96 static const uint8_t write_code_arm[] = {
97 #include "../../../contrib/loaders/flash/max32xxx/max32xxx_write_arm.inc"
98 };
99 
100 FLASH_BANK_COMMAND_HANDLER(max32xxx_flash_bank_command)
101 {
102  struct max32xxx_flash_bank *info;
103 
104  if (CMD_ARGC != 10) {
105  LOG_ERROR("incorrect flash bank max32xxx configuration: <base> <size> 0 0 <target> <FLC base> <sector size> <clkdiv> <options>");
107  }
108 
109  info = calloc(1, sizeof(struct max32xxx_flash_bank));
110  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], info->flash_size);
111  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[6], info->flc_base);
112  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[7], info->sector_size);
113  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[8], info->clkdiv_value);
114  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[9], info->options);
115 
116  info->int_state = 0;
117  bank->driver_priv = info;
118  return ERROR_OK;
119 }
120 
121 static int get_info(struct flash_bank *bank, struct command_invocation *cmd)
122 {
123  struct max32xxx_flash_bank *info = bank->driver_priv;
124 
125  if (!info->probed)
127 
128  command_print_sameline(cmd, "\nMaxim Integrated max32xxx flash driver\n");
129  return ERROR_OK;
130 }
131 
132 static bool max32xxx_flash_busy(uint32_t flash_cn)
133 {
134  if (flash_cn & (FLC_CN_PGE | FLC_CN_ME | FLC_CN_WR))
135  return true;
136 
137  return false;
138 }
139 
141 {
142  struct target *target = bank->target;
143  struct max32xxx_flash_bank *info = bank->driver_priv;
144  uint32_t flash_cn;
145  uint32_t bootloader;
146 
147  // Check if the flash controller is busy
148  target_read_u32(target, info->flc_base + FLC_CN, &flash_cn);
149  if (max32xxx_flash_busy(flash_cn))
150  return ERROR_FLASH_BUSY;
151 
152  // Refresh flash controller timing
153  target_write_u32(target, info->flc_base + FLC_CLKDIV, info->clkdiv_value);
154 
155  // Clear and disable flash programming interrupts
156  target_read_u32(target, info->flc_base + FLC_INT, &info->int_state);
157  target_write_u32(target, info->flc_base + FLC_INT, 0);
158 
159  /* Clear the lower bit in the bootloader configuration register in case flash page 0 has
160  * been replaced */
161  if (target_read_u32(target, info->flc_base + FLC_BL_CTRL, &bootloader) != ERROR_OK) {
162  LOG_ERROR("Read failure on FLC_BL_CTRL");
163  return ERROR_FAIL;
164  }
165  if (bootloader & FLC_BL_CTRL_23) {
166  LOG_WARNING("FLC_BL_CTRL indicates BL mode 2 or mode 3.");
167  if (bootloader & FLC_BL_CTRL_IFREN) {
168  LOG_WARNING("Flash page 0 swapped out, attempting to swap back in for programming");
169  bootloader &= ~(FLC_BL_CTRL_IFREN);
170  if (target_write_u32(target, info->flc_base + FLC_BL_CTRL,
171  bootloader) != ERROR_OK) {
172  LOG_ERROR("Write failure on FLC_BL_CTRL");
173  return ERROR_FAIL;
174  }
175  if (target_read_u32(target, info->flc_base + FLC_BL_CTRL,
176  &bootloader) != ERROR_OK) {
177  LOG_ERROR("Read failure on FLC_BL_CTRL");
178  return ERROR_FAIL;
179  }
180  if (bootloader & FLC_BL_CTRL_IFREN)
181  LOG_ERROR("Unable to swap flash page 0 back in. Writes to page 0 will fail.");
182  }
183  }
184 
185  // Unlock flash
186  flash_cn &= ~(FLC_CN_UNLOCK_MASK);
187  flash_cn |= FLC_CN_UNLOCK_VALUE;
188  target_write_u32(target, info->flc_base + FLC_CN, flash_cn);
189 
190  // Confirm flash is unlocked
191  target_read_u32(target, info->flc_base + FLC_CN, &flash_cn);
192  if ((flash_cn & FLC_CN_UNLOCK_VALUE) != FLC_CN_UNLOCK_VALUE)
193  return ERROR_FAIL;
194 
195  return ERROR_OK;
196 }
197 
199 {
200  struct target *target = bank->target;
201  struct max32xxx_flash_bank *info = bank->driver_priv;
202  uint32_t flash_cn;
203 
204  // Restore flash programming interrupts
205  target_write_u32(target, info->flc_base + FLC_INT, info->int_state);
206 
207  // Lock flash
208  target_read_u32(target, info->flc_base + FLC_CN, &flash_cn);
209  flash_cn &= ~(FLC_CN_UNLOCK_MASK);
210  target_write_u32(target, info->flc_base + FLC_CN, flash_cn);
211  return ERROR_OK;
212 }
213 
215 {
216  struct max32xxx_flash_bank *info = bank->driver_priv;
217  struct target *target = bank->target;
218  uint32_t temp_reg;
219 
220  if (!info->probed)
222 
223  if (!info->max326xx) {
224  for (unsigned int i = 0; i < bank->num_sectors; i++)
225  bank->sectors[i].is_protected = -1;
226 
228  }
229 
230  // Check the protection
231  for (unsigned int i = 0; i < bank->num_sectors; i++) {
232  if (i % 32 == 0)
233  target_read_u32(target, info->flc_base + FLC_PROT + ((i / 32) * 4), &temp_reg);
234 
235  if (temp_reg & (0x1 << i % 32))
236  bank->sectors[i].is_protected = 1;
237  else
238  bank->sectors[i].is_protected = 0;
239  }
240  return ERROR_OK;
241 }
242 
243 static int max32xxx_erase(struct flash_bank *bank, unsigned int first,
244  unsigned int last)
245 {
246  uint32_t flash_cn, flash_int;
247  struct max32xxx_flash_bank *info = bank->driver_priv;
248  struct target *target = bank->target;
249  int retval;
250  int retry;
251 
252  if (bank->target->state != TARGET_HALTED) {
253  LOG_ERROR("Target not halted");
255  }
256 
257  if (!info->probed)
259 
260  if (last < first || last >= bank->num_sectors)
262 
263  if (first == 0 && last == (bank->num_sectors - 1))
264  return max32xxx_mass_erase(bank);
265 
266  // Prepare to issue flash operation
267  retval = max32xxx_flash_op_pre(bank);
268 
269  if (retval != ERROR_OK)
270  return retval;
271 
272  int erased = 0;
273  for (unsigned int banknr = first; banknr <= last; banknr++) {
274  // Check the protection
275  if (bank->sectors[banknr].is_protected == 1) {
276  LOG_WARNING("Flash sector %u is protected", banknr);
277  continue;
278  } else {
279  erased = 1;
280  }
281 
282  // Address is first word in page
283  target_write_u32(target, info->flc_base + FLC_ADDR, banknr * info->sector_size);
284 
285  // Write page erase code
286  target_read_u32(target, info->flc_base + FLC_CN, &flash_cn);
287  flash_cn |= FLC_CN_ERASE_CODE_PGE;
288  target_write_u32(target, info->flc_base + FLC_CN, flash_cn);
289 
290  // Issue page erase command
291  flash_cn |= FLC_CN_PGE;
292  target_write_u32(target, info->flc_base + FLC_CN, flash_cn);
293 
294  // Wait until erase complete
295  retry = 1000;
296  do {
297  target_read_u32(target, info->flc_base + FLC_CN, &flash_cn);
298  } while ((--retry > 0) && max32xxx_flash_busy(flash_cn));
299 
300  if (retry <= 0) {
301  LOG_ERROR("Timed out waiting for flash page erase @ 0x%08" PRIx32,
302  (banknr * info->sector_size));
304  }
305 
306  // Check access violations
307  target_read_u32(target, info->flc_base + FLC_INT, &flash_int);
308  if (flash_int & FLC_INT_AF) {
309  LOG_ERROR("Error erasing flash page %i", banknr);
310  target_write_u32(target, info->flc_base + FLC_INT, 0);
313  }
314  }
315 
316  if (!erased) {
317  LOG_ERROR("All pages protected %u to %u", first, last);
319  return ERROR_FAIL;
320  }
321 
323  return ERROR_FAIL;
324 
325  return ERROR_OK;
326 }
327 
328 static int max32xxx_protect(struct flash_bank *bank, int set,
329  unsigned int first, unsigned int last)
330 {
331  struct max32xxx_flash_bank *info = bank->driver_priv;
332  struct target *target = bank->target;
333  uint32_t temp_reg;
334 
335  if (bank->target->state != TARGET_HALTED) {
336  LOG_ERROR("Target not halted");
338  }
339 
340  if (!info->probed)
342 
343  if (!info->max326xx)
345 
346  if (last < first || last >= bank->num_sectors)
348 
349  // Setup the protection on the pages given
350  for (unsigned int page = first; page <= last; page++) {
351  if (set) {
352  // Set the write/erase bit for this page
353  target_read_u32(target, info->flc_base + FLC_PROT + (page / 32), &temp_reg);
354  temp_reg |= (0x1 << page % 32);
355  target_write_u32(target, info->flc_base + FLC_PROT + (page / 32), temp_reg);
356  bank->sectors[page].is_protected = 1;
357  } else {
358  // Clear the write/erase bit for this page
359  target_read_u32(target, info->flc_base + FLC_PROT + (page / 32), &temp_reg);
360  temp_reg &= ~(0x1 << page % 32);
361  target_write_u32(target, info->flc_base + FLC_PROT + (page / 32), temp_reg);
362  bank->sectors[page].is_protected = 0;
363  }
364  }
365 
366  return ERROR_OK;
367 }
368 
369 static int max32xxx_write_block(struct flash_bank *bank, const uint8_t *buffer,
370  uint32_t offset, uint32_t len)
371 {
372  struct max32xxx_flash_bank *info = bank->driver_priv;
373  struct target *target = bank->target;
374  const char *target_type_name = (const char *)target->type->name;
375  uint32_t buffer_size = 16384;
376  struct working_area *source;
377  struct working_area *write_algorithm;
378  struct reg_param reg_params[5];
379  struct mem_param mem_param[2];
380  struct armv7m_algorithm armv7m_info;
381  int retval = ERROR_OK;
382  // power of two, and multiple of word size
383  static const unsigned int buf_min = 128;
384  uint8_t *write_code;
385  int write_code_size;
386 
387 
388  if (strcmp(target_type_name, "cortex_m") == 0) {
389  write_code = (uint8_t *)write_code_arm;
390  write_code_size = sizeof(write_code_arm);
391  } else {
393  }
394 
395  LOG_DEBUG("bank=%p buffer=%p offset=%08" PRIx32 " len=%08" PRIx32 "",
396  bank, buffer, offset, len);
397 
398  // flash write code
399  if (target_alloc_working_area(target, write_code_size, &write_algorithm) != ERROR_OK) {
400  LOG_DEBUG("no working area for block memory writes");
402  }
403 
404  // memory buffer
406  buffer_size /= 2;
407 
408  if (buffer_size <= buf_min) {
409  target_free_working_area(target, write_algorithm);
411  }
412 
413  LOG_DEBUG("retry target_alloc_working_area(%s, size=%" PRIu32 ")",
415  }
416 
417  target_write_buffer(target, write_algorithm->address, write_code_size,
418  write_code);
419 
420  armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
421  armv7m_info.core_mode = ARM_MODE_THREAD;
422 
423  // TODO: a0-a3 for RISCV
424 
425  if (strcmp(target_type_name, "cortex_m") == 0) {
426  init_reg_param(&reg_params[0], "r0", 32, PARAM_OUT);
427  init_reg_param(&reg_params[1], "r1", 32, PARAM_OUT);
428  init_reg_param(&reg_params[2], "r2", 32, PARAM_OUT);
429  init_reg_param(&reg_params[3], "r3", 32, PARAM_OUT);
430  } else {
431  init_reg_param(&reg_params[0], "a0", 32, PARAM_OUT);
432  init_reg_param(&reg_params[1], "a1", 32, PARAM_OUT);
433  init_reg_param(&reg_params[2], "a2", 32, PARAM_OUT);
434  init_reg_param(&reg_params[3], "a3", 32, PARAM_OUT);
435  }
436  init_reg_param(&reg_params[4], "sp", 32, PARAM_OUT);
437 
438  buf_set_u32(reg_params[0].value, 0, 32, source->address);
439  buf_set_u32(reg_params[1].value, 0, 32, source->address + source->size);
440  buf_set_u32(reg_params[2].value, 0, 32, len);
441  buf_set_u32(reg_params[3].value, 0, 32, offset);
442  buf_set_u32(reg_params[4].value, 0, 32, source->address + source->size);
443 
444  // mem_params for options
445  init_mem_param(&mem_param[0], source->address + (source->size - 8 - 128), 4, PARAM_OUT);
446  init_mem_param(&mem_param[1], source->address + (source->size - 4 - 128), 4, PARAM_OUT);
447  buf_set_u32(mem_param[0].value, 0, 32, info->options);
448  buf_set_u32(mem_param[1].value, 0, 32, info->flc_base);
449 
450  // leave room for stack, 32-bit options and encryption buffer
452  buffer,
453  len,
454  1,
455  2,
456  mem_param,
457  5,
458  reg_params,
459  source->address,
460  (source->size - 8 - 256),
461  write_algorithm->address,
462  0,
463  &armv7m_info);
464 
465  if (retval == ERROR_FLASH_OPERATION_FAILED)
466  LOG_ERROR("error %d executing max32xxx flash write algorithm", retval);
467 
468  target_free_working_area(target, write_algorithm);
470  destroy_reg_param(&reg_params[0]);
471  destroy_reg_param(&reg_params[1]);
472  destroy_reg_param(&reg_params[2]);
473  destroy_reg_param(&reg_params[3]);
474  destroy_reg_param(&reg_params[4]);
475  return retval;
476 }
477 
478 static int max32xxx_write(struct flash_bank *bank, const uint8_t *buffer,
479  uint32_t offset, uint32_t count)
480 {
481  struct max32xxx_flash_bank *info = bank->driver_priv;
482  struct target *target = bank->target;
483  uint32_t flash_cn, flash_int;
484  uint32_t address = offset;
485  uint32_t remaining = count;
486  int retval;
487  int retry;
488 
489  if (bank->target->state != TARGET_HALTED) {
490  LOG_ERROR("Target not halted");
492  }
493 
494  LOG_DEBUG("bank=%p buffer=%p offset=%08" PRIx32 " count=%08" PRIx32 "",
495  bank, buffer, offset, count);
496 
497  if (!info->probed)
499 
500  if ((info->options & OPTIONS_128) == 0) {
501  if (offset & 0x3) {
502  LOG_ERROR("offset size must be 32-bit aligned");
504  }
505  } else {
506  if (offset & 0xF) {
507  LOG_ERROR("offset size must be 128-bit aligned");
509  }
510  }
511 
512  if (offset + count > bank->size)
514 
515  // Prepare to issue flash operation
516  retval = max32xxx_flash_op_pre(bank);
517 
518  if (retval != ERROR_OK) {
520  return retval;
521  }
522 
523  if (remaining >= 16) {
524  // try using a block write
525 
526  retval = max32xxx_write_block(bank, buffer, offset, remaining);
527  if (retval != ERROR_OK) {
528  if (retval == ERROR_TARGET_RESOURCE_NOT_AVAILABLE) {
529  if (info->options & OPTIONS_ENC) {
530  LOG_ERROR("Must use algorithm in working area for encryption");
532  }
533  LOG_DEBUG("writing flash word-at-a-time");
534  } else {
537  }
538  } else {
539  // all words_remaining have been written
540  buffer += remaining;
541  address += remaining;
542  remaining -= remaining;
543  }
544  }
545 
546  if (((info->options & OPTIONS_128) == 0) && remaining >= 4) {
547  // write in 32-bit units
548  target_read_u32(target, info->flc_base + FLC_CN, &flash_cn);
549  flash_cn |= FLC_CN_32BIT;
550  target_write_u32(target, info->flc_base + FLC_CN, flash_cn);
551 
552  while (remaining >= 4) {
554  target_write_buffer(target, info->flc_base + FLC_DATA0, 4, buffer);
555  flash_cn |= FLC_CN_WR;
556  target_write_u32(target, info->flc_base + FLC_CN, flash_cn);
557  // Wait until flash operation is complete
558  retry = 10;
559 
560  do {
561  target_read_u32(target, info->flc_base + FLC_CN, &flash_cn);
562  } while ((--retry > 0) && max32xxx_flash_busy(flash_cn));
563 
564  if (retry <= 0) {
565  LOG_ERROR("Timed out waiting for flash write @ 0x%08" PRIx32,
566  address);
569  }
570 
571  buffer += 4;
572  address += 4;
573  remaining -= 4;
574  }
575  }
576 
577  if ((info->options & OPTIONS_128) && remaining >= 16) {
578  // write in 128-bit units
579  target_read_u32(target, info->flc_base + FLC_CN, &flash_cn);
580  flash_cn &= ~(FLC_CN_32BIT);
581  target_write_u32(target, info->flc_base + FLC_CN, flash_cn);
582 
583  while (remaining >= 16) {
585 
586  if ((address & 0xFFF) == 0)
587  LOG_DEBUG("Writing @ 0x%08" PRIx32, address);
588 
589  target_write_buffer(target, info->flc_base + FLC_DATA0, 16, buffer);
590  flash_cn |= FLC_CN_WR;
591  target_write_u32(target, info->flc_base + FLC_CN, flash_cn);
592  // Wait until flash operation is complete
593  retry = 10;
594 
595  do {
596  target_read_u32(target, info->flc_base + FLC_CN, &flash_cn);
597  } while ((--retry > 0) && max32xxx_flash_busy(flash_cn));
598 
599  if (retry <= 0) {
600  LOG_ERROR("Timed out waiting for flash write @ 0x%08" PRIx32,
601  address);
604  }
605 
606  buffer += 16;
607  address += 16;
608  remaining -= 16;
609  }
610  }
611 
612  if (((info->options & OPTIONS_128) == 0) && remaining > 0) {
613  // write remaining bytes in a 32-bit unit
614  target_read_u32(target, info->flc_base + FLC_CN, &flash_cn);
615  flash_cn |= FLC_CN_32BIT;
616  target_write_u32(target, info->flc_base + FLC_CN, flash_cn);
617 
618  uint8_t last_word[4] = {0xFF, 0xFF, 0xFF, 0xFF};
619  uint32_t i = 0;
620 
621  while (remaining > 0) {
622  last_word[i++] = *buffer;
623  buffer++;
624  remaining--;
625  }
626 
628  target_write_buffer(target, info->flc_base + FLC_DATA0, 4, last_word);
629  flash_cn |= FLC_CN_WR;
630  target_write_u32(target, info->flc_base + FLC_CN, flash_cn);
631 
632  // Wait until flash operation is complete
633  retry = 10;
634 
635  do {
636  target_read_u32(target, info->flc_base + FLC_CN, &flash_cn);
637  } while ((--retry > 0) && max32xxx_flash_busy(flash_cn));
638 
639  if (retry <= 0) {
640  LOG_ERROR("Timed out waiting for flash write @ 0x%08" PRIx32, address);
643  }
644  }
645 
646  if ((info->options & OPTIONS_128) && remaining > 0) {
647  // write remaining bytes in a 128-bit unit
648  if (target_read_u32(target, info->flc_base + FLC_CN, &flash_cn) != ERROR_OK) {
650  return ERROR_FAIL;
651  }
652 
653  target_read_u32(target, info->flc_base + FLC_CN, &flash_cn);
654  flash_cn &= ~(FLC_CN_32BIT);
655  target_write_u32(target, info->flc_base + FLC_CN, flash_cn);
656 
657  uint8_t last_words[16] = {0xFF, 0xFF, 0xFF, 0xFF,
658  0xFF, 0xFF, 0xFF, 0xFF,
659  0xFF, 0xFF, 0xFF, 0xFF,
660  0xFF, 0xFF, 0xFF, 0xFF};
661 
662  uint32_t i = 0;
663 
664  while (remaining > 0) {
665  last_words[i++] = *buffer;
666  buffer++;
667  remaining--;
668  }
669 
671  target_write_buffer(target, info->flc_base + FLC_DATA0, 4, last_words);
672  target_write_buffer(target, info->flc_base + FLC_DATA0 + 4, 4, last_words + 4);
673  target_write_buffer(target, info->flc_base + FLC_DATA0 + 8, 4, last_words + 8);
674  target_write_buffer(target, info->flc_base + FLC_DATA0 + 12, 4, last_words + 12);
675  flash_cn |= FLC_CN_WR;
676  target_write_u32(target, info->flc_base + FLC_CN, flash_cn);
677 
678  // Wait until flash operation is complete
679  retry = 10;
680  do {
681  if (target_read_u32(target, info->flc_base + FLC_CN,
682  &flash_cn) != ERROR_OK) {
684  return ERROR_FAIL;
685  }
686  } while ((--retry > 0) && (flash_cn & FLC_CN_PEND));
687 
688  if (retry <= 0) {
689  LOG_ERROR("Timed out waiting for flash write @ 0x%08" PRIx32, address);
692  }
693  }
694 
695  // Check access violations
696  target_read_u32(target, info->flc_base + FLC_INT, &flash_int);
697  if (flash_int & FLC_INT_AF) {
698  LOG_ERROR("Flash Error writing 0x%" PRIx32 " bytes at 0x%08" PRIx32, count, offset);
701  }
702 
704  return ERROR_FAIL;
705 
706  return ERROR_OK;
707 }
708 
709 static int max32xxx_probe(struct flash_bank *bank)
710 {
711  struct max32xxx_flash_bank *info = bank->driver_priv;
712  struct target *target = bank->target;
713  uint32_t arm_id[2];
714  uint16_t arm_pid;
715 
716  free(bank->sectors);
717 
718  // provide this for the benefit of the NOR flash framework
719  bank->size = info->flash_size;
720  bank->num_sectors = info->flash_size / info->sector_size;
721  bank->sectors = calloc(bank->num_sectors, sizeof(struct flash_sector));
722 
723  for (unsigned int i = 0; i < bank->num_sectors; i++) {
724  bank->sectors[i].offset = i * info->sector_size;
725  bank->sectors[i].size = info->sector_size;
726  bank->sectors[i].is_erased = -1;
727  bank->sectors[i].is_protected = -1;
728  }
729 
730  // Probe to determine if this part is in the max326xx family
731  info->max326xx = false;
732  target_read_u32(target, ARM_PID_REG, &arm_id[0]);
733  target_read_u32(target, ARM_PID_REG + 4, &arm_id[1]);
734  arm_pid = (arm_id[1] << 8) + arm_id[0];
735  LOG_DEBUG("arm_pid = 0x%x", arm_pid);
736 
737  if (arm_pid == ARM_PID_DEFAULT_CM3 || arm_pid == ARM_PID_DEFAULT_CM4) {
738  uint32_t max326xx_id;
739  target_read_u32(target, MAX326XX_ID_REG, &max326xx_id);
740  LOG_DEBUG("max326xx_id = 0x%" PRIx32, max326xx_id);
741  max326xx_id = ((max326xx_id & 0xFF000000) >> 24);
742  if (max326xx_id == MAX326XX_ID)
743  info->max326xx = true;
744  }
745  LOG_DEBUG("info->max326xx = %d", info->max326xx);
746 
747  // Initialize the protection bits for each flash page
749  LOG_WARNING("Flash protection not supported on this device");
750 
751  info->probed = true;
752  return ERROR_OK;
753 }
754 
756 {
757  struct target *target = NULL;
758  struct max32xxx_flash_bank *info = NULL;
759  uint32_t flash_cn, flash_int;
760  int retval;
761  int retry;
762  info = bank->driver_priv;
763  target = bank->target;
764 
765  if (target->state != TARGET_HALTED) {
766  LOG_ERROR("Target not halted");
768  }
769 
770  if (!info->probed)
772 
773  bool protected = true;
774  for (unsigned int i = 0; i < bank->num_sectors; i++) {
775  if (bank->sectors[i].is_protected == 1)
776  LOG_WARNING("Flash sector %u is protected", i);
777  else
778  protected = false;
779  }
780 
781  if (protected) {
782  LOG_ERROR("All pages protected");
783  return ERROR_FAIL;
784  }
785 
786  // Prepare to issue flash operation
787  retval = max32xxx_flash_op_pre(bank);
788 
789  if (retval != ERROR_OK)
790  return retval;
791 
792  // Write mass erase code
793  target_read_u32(target, info->flc_base + FLC_CN, &flash_cn);
794  flash_cn |= FLC_CN_ERASE_CODE_ME;
795  target_write_u32(target, info->flc_base + FLC_CN, flash_cn);
796 
797  // Issue mass erase command
798  flash_cn |= FLC_CN_ME;
799  target_write_u32(target, info->flc_base + FLC_CN, flash_cn);
800 
801  // Wait until erase complete
802  retry = 1000;
803  do {
804  target_read_u32(target, info->flc_base + FLC_CN, &flash_cn);
805  } while ((--retry > 0) && max32xxx_flash_busy(flash_cn));
806 
807  if (retry <= 0) {
808  LOG_ERROR("Timed out waiting for flash mass erase");
810  }
811 
812  // Check access violations
813  target_read_u32(target, info->flc_base + FLC_INT, &flash_int);
814  if (flash_int & FLC_INT_AF) {
815  LOG_ERROR("Error mass erasing");
816  target_write_u32(target, info->flc_base + FLC_INT, 0);
818  }
819 
821  return ERROR_FAIL;
822 
823  return ERROR_OK;
824 }
825 
826 COMMAND_HANDLER(max32xxx_handle_mass_erase_command)
827 {
828  struct flash_bank *bank;
829  int retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
830 
831  if (CMD_ARGC < 1) {
832  command_print(CMD, "max32xxx mass_erase <bank>");
833  return ERROR_OK;
834  }
835 
836  if (retval != ERROR_OK)
837  return retval;
838 
840  command_print(CMD, "max32xxx mass erase complete");
841  else
842  command_print(CMD, "max32xxx mass erase failed");
843 
844  return ERROR_OK;
845 }
846 
847 COMMAND_HANDLER(max32xxx_handle_protection_set_command)
848 {
849  struct flash_bank *bank;
850  int retval;
851  struct max32xxx_flash_bank *info;
852  uint32_t addr, len;
853 
854  if (CMD_ARGC != 3) {
855  command_print(CMD, "max32xxx protection_set <bank> <addr> <size>");
856  return ERROR_OK;
857  }
858 
859  retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
860  if (retval != ERROR_OK)
861  return retval;
862  info = bank->driver_priv;
863 
864  // Convert the range to the page numbers
865  if (sscanf(CMD_ARGV[1], "0x%" SCNx32, &addr) != 1) {
866  LOG_WARNING("Error parsing address");
867  command_print(CMD, "max32xxx protection_set <bank> <addr> <size>");
868  return ERROR_FAIL;
869  }
870  // Mask off the top portion on the address
871  addr = (addr & 0x0FFFFFFF);
872 
873  if (sscanf(CMD_ARGV[2], "0x%" SCNx32, &len) != 1) {
874  LOG_WARNING("Error parsing length");
875  command_print(CMD, "max32xxx protection_set <bank> <addr> <size>");
876  return ERROR_FAIL;
877  }
878 
879  // Check the address is in the range of the flash
880  if ((addr + len) >= info->flash_size)
882 
883  if (len == 0)
884  return ERROR_OK;
885 
886  // Convert the address and length to the page boundaries
887  addr = addr - (addr % info->sector_size);
888  if (len % info->sector_size)
889  len = len + info->sector_size - (len % info->sector_size);
890 
891  // Convert the address and length to page numbers
892  addr = (addr / info->sector_size);
893  len = addr + (len / info->sector_size) - 1;
894 
895  if (max32xxx_protect(bank, 1, addr, len) == ERROR_OK)
896  command_print(CMD, "max32xxx protection set complete");
897  else
898  command_print(CMD, "max32xxx protection set failed");
899 
900  return ERROR_OK;
901 }
902 
903 COMMAND_HANDLER(max32xxx_handle_protection_clr_command)
904 {
905  struct flash_bank *bank;
906  int retval;
907  struct max32xxx_flash_bank *info;
908  uint32_t addr, len;
909 
910  if (CMD_ARGC != 3) {
911  command_print(CMD, "max32xxx protection_clr <bank> <addr> <size>");
912  return ERROR_OK;
913  }
914 
915  retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
916  if (retval != ERROR_OK)
917  return retval;
918  info = bank->driver_priv;
919 
920  // Convert the range to the page numbers
921  if (sscanf(CMD_ARGV[1], "0x%" SCNx32, &addr) != 1) {
922  LOG_WARNING("Error parsing address");
923  command_print(CMD, "max32xxx protection_clr <bank> <addr> <size>");
924  return ERROR_FAIL;
925  }
926  // Mask off the top portion on the address
927  addr = (addr & 0x0FFFFFFF);
928 
929  if (sscanf(CMD_ARGV[2], "0x%" SCNx32, &len) != 1) {
930  LOG_WARNING("Error parsing length");
931  command_print(CMD, "max32xxx protection_clr <bank> <addr> <size>");
932  return ERROR_FAIL;
933  }
934 
935  // Check the address is in the range of the flash
936  if ((addr + len) >= info->flash_size)
938 
939  if (len == 0)
940  return ERROR_OK;
941 
942  // Convert the address and length to the page boundaries
943  addr = addr - (addr % info->sector_size);
944  if (len % info->sector_size)
945  len = len + info->sector_size - (len % info->sector_size);
946 
947  // Convert the address and length to page numbers
948  addr = (addr / info->sector_size);
949  len = addr + (len / info->sector_size) - 1;
950 
951  if (max32xxx_protect(bank, 0, addr, len) == ERROR_OK)
952  command_print(CMD, "max32xxx protection clear complete");
953  else
954  command_print(CMD, "max32xxx protection clear failed");
955 
956  return ERROR_OK;
957 }
958 
959 COMMAND_HANDLER(max32xxx_handle_protection_check_command)
960 {
961  struct flash_bank *bank;
962  int retval;
963  struct max32xxx_flash_bank *info;
964 
965  if (CMD_ARGC < 1) {
966  command_print(CMD, "max32xxx protection_check <bank>");
967  return ERROR_OK;
968  }
969 
970  retval = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
971  if (retval != ERROR_OK)
972  return retval;
973  info = bank->driver_priv;
974 
975  // Update the protection array
976  retval = max32xxx_protect_check(bank);
977  if (retval != ERROR_OK) {
978  LOG_WARNING("Error updating the protection array");
979  return retval;
980  }
981 
982  LOG_WARNING("s:<sector number> a:<address> p:<protection bit>");
983  for (unsigned int i = 0; i < bank->num_sectors; i += 4) {
984  LOG_WARNING("s:%03d a:0x%06x p:%d | s:%03d a:0x%06x p:%d | s:%03d a:0x%06x p:%d | s:%03d a:0x%06x p:%d",
985  (i + 0), (i + 0) * info->sector_size, bank->sectors[(i + 0)].is_protected,
986  (i + 1), (i + 1) * info->sector_size, bank->sectors[(i + 1)].is_protected,
987  (i + 2), (i + 2) * info->sector_size, bank->sectors[(i + 2)].is_protected,
988  (i + 3), (i + 3) * info->sector_size, bank->sectors[(i + 3)].is_protected);
989  }
990 
991  return ERROR_OK;
992 }
993 
995  {
996  .name = "mass_erase",
997  .handler = max32xxx_handle_mass_erase_command,
998  .mode = COMMAND_EXEC,
999  .usage = "bank_id",
1000  .help = "mass erase flash",
1001  },
1002  {
1003  .name = "protection_set",
1004  .handler = max32xxx_handle_protection_set_command,
1005  .mode = COMMAND_EXEC,
1006  .usage = "bank_id addr size",
1007  .help = "set flash protection for address range",
1008  },
1009  {
1010  .name = "protection_clr",
1011  .handler = max32xxx_handle_protection_clr_command,
1012  .mode = COMMAND_EXEC,
1013  .usage = "bank_id addr size",
1014  .help = "clear flash protection for address range",
1015  },
1016  {
1017  .name = "protection_check",
1018  .handler = max32xxx_handle_protection_check_command,
1019  .mode = COMMAND_EXEC,
1020  .usage = "bank_id",
1021  .help = "check flash protection",
1022  },
1024 };
1025 
1026 static const struct command_registration max32xxx_command_handlers[] = {
1027  {
1028  .name = "max32xxx",
1029  .mode = COMMAND_EXEC,
1030  .help = "max32xxx flash command group",
1032  .usage = "",
1033  },
1035 };
1036 
1037 const struct flash_driver max32xxx_flash = {
1038  .name = "max32xxx",
1039  .commands = max32xxx_command_handlers,
1040  .flash_bank_command = max32xxx_flash_bank_command,
1041  .erase = max32xxx_erase,
1042  .protect = max32xxx_protect,
1043  .write = max32xxx_write,
1044  .read = default_flash_read,
1045  .probe = max32xxx_probe,
1046  .auto_probe = max32xxx_probe,
1047  .erase_check = default_flash_blank_check,
1048  .protect_check = max32xxx_protect_check,
1049  .info = get_info,
1050  .free_driver_priv = default_flash_free_driver_priv,
1051 };
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
void init_mem_param(struct mem_param *param, uint32_t address, uint32_t size, enum param_direction direction)
Definition: algorithm.c:15
@ PARAM_OUT
Definition: algorithm.h:16
@ 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 void buf_set_u32(uint8_t *_buffer, unsigned int first, unsigned int num, uint32_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
Definition: binarybuffer.h:34
void command_print_sameline(struct command_invocation *cmd, const char *format,...)
Definition: command.c:378
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:389
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:146
#define CALL_COMMAND_HANDLER(name, extra ...)
Use this to macro to call a command helper (or a nested handler).
Definition: command.h:123
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:161
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:156
#define COMMAND_PARSE_NUMBER(type, in, out)
parses the string in into out as a type, or prints a command error and passes the error code to the c...
Definition: command.h:445
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:256
@ 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 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_OPER_UNSUPPORTED
Definition: flash/common.h:36
#define ERROR_FLASH_BANK_INVALID
Definition: flash/common.h:28
#define ERROR_FLASH_SECTOR_INVALID
Definition: flash/common.h:29
#define ERROR_FLASH_BANK_NOT_PROBED
Definition: flash/common.h:35
#define ERROR_FLASH_OPERATION_FAILED
Definition: flash/common.h:30
#define ERROR_FLASH_DST_BREAKS_ALIGNMENT
Definition: flash/common.h:32
#define ERROR_FLASH_BUSY
Definition: flash/common.h:33
#define ERROR_FLASH_DST_OUT_OF_BANK
Definition: flash/common.h:31
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.
#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_DEBUG(expr ...)
Definition: log.h:124
#define ERROR_OK
Definition: log.h:182
#define FLC_CLKDIV
Definition: max32xxx.c:22
#define FLC_CN_32BIT
Definition: max32xxx.c:51
static int max32xxx_flash_op_post(struct flash_bank *bank)
Definition: max32xxx.c:198
static int max32xxx_write_block(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t len)
Definition: max32xxx.c:369
static int max32xxx_probe(struct flash_bank *bank)
Definition: max32xxx.c:709
#define FLC_INT_AF
Definition: max32xxx.c:42
static int max32xxx_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
Definition: max32xxx.c:478
static const struct command_registration max32xxx_exec_command_handlers[]
Definition: max32xxx.c:994
#define FLC_CN_WR
Definition: max32xxx.c:57
#define FLC_CN_ME
Definition: max32xxx.c:56
static const struct command_registration max32xxx_command_handlers[]
Definition: max32xxx.c:1026
#define FLC_ADDR
Definition: max32xxx.c:21
#define MAX326XX_ID_REG
Definition: max32xxx.c:39
#define FLC_CN_UNLOCK_MASK
Definition: max32xxx.c:44
#define FLC_BL_CTRL
Definition: max32xxx.c:35
#define FLC_CN_PEND
Definition: max32xxx.c:47
static const uint8_t write_code_arm[]
Definition: max32xxx.c:96
#define MAX326XX_ID
Definition: max32xxx.c:81
#define FLC_CN
Definition: max32xxx.c:23
static int max32xxx_protect(struct flash_bank *bank, int set, unsigned int first, unsigned int last)
Definition: max32xxx.c:328
#define FLC_DATA0
Definition: max32xxx.c:31
static int max32xxx_mass_erase(struct flash_bank *bank)
Definition: max32xxx.c:755
#define FLC_CN_ERASE_CODE_ME
Definition: max32xxx.c:50
#define FLC_CN_PGE
Definition: max32xxx.c:55
#define FLC_BL_CTRL_23
Definition: max32xxx.c:59
static int max32xxx_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
Definition: max32xxx.c:243
static bool max32xxx_flash_busy(uint32_t flash_cn)
Definition: max32xxx.c:132
COMMAND_HANDLER(max32xxx_handle_mass_erase_command)
Definition: max32xxx.c:826
#define ARM_PID_DEFAULT_CM3
Definition: max32xxx.c:79
#define ARM_PID_DEFAULT_CM4
Definition: max32xxx.c:80
#define ARM_PID_REG
Definition: max32xxx.c:38
const struct flash_driver max32xxx_flash
Definition: max32xxx.c:1037
static int get_info(struct flash_bank *bank, struct command_invocation *cmd)
Definition: max32xxx.c:121
#define FLC_CN_UNLOCK_VALUE
Definition: max32xxx.c:45
#define FLC_CN_ERASE_CODE_PGE
Definition: max32xxx.c:49
static int max32xxx_flash_op_pre(struct flash_bank *bank)
Definition: max32xxx.c:140
static int max32xxx_protect_check(struct flash_bank *bank)
Definition: max32xxx.c:214
#define FLC_PROT
Definition: max32xxx.c:36
FLASH_BANK_COMMAND_HANDLER(max32xxx_flash_bank_command)
Definition: max32xxx.c:100
#define FLC_BL_CTRL_IFREN
Definition: max32xxx.c:60
#define FLC_INT
Definition: max32xxx.c:30
#define OPTIONS_128
#define OPTIONS_ENC
static const uint8_t write_code[]
Definition: max32xxx_qspi.c:76
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
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
const char * usage
a string listing the options and arguments, required or optional
Definition: command.h:244
Provides details of a flash bank, available either on-chip or through a major interface.
Definition: nor/core.h:75
Provides the implementation-independent structure that defines all of the callbacks required by OpenO...
Definition: nor/driver.h:39
const char * name
Gives a human-readable name of this flash driver, This field is used to select and initialize the dri...
Definition: nor/driver.h:44
Describes the geometry and status of a single flash sector within a flash bank.
Definition: nor/core.h:28
unsigned int options
Definition: max32xxx.c:93
unsigned int int_state
Definition: max32xxx.c:92
unsigned int clkdiv_value
Definition: max32xxx.c:91
unsigned int flc_base
Definition: max32xxx.c:89
unsigned int flash_size
Definition: max32xxx.c:88
unsigned int sector_size
Definition: max32xxx.c:90
const char * name
Name of this type of target.
Definition: target_type.h:32
Definition: target.h:119
enum target_state state
Definition: target.h:167
struct target_type * type
Definition: target.h:120
int target_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
Definition: target.c:2410
int target_alloc_working_area(struct target *target, uint32_t size, struct working_area **area)
Definition: target.c:2118
int target_write_u32(struct target *target, target_addr_t address, uint32_t value)
Definition: target.c:2676
int target_free_working_area(struct target *target, struct working_area *area)
Free a working area.
Definition: target.c:2174
int target_alloc_working_area_try(struct target *target, uint32_t size, struct working_area **area)
Definition: target.c:2018
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:946
int target_read_u32(struct target *target, target_addr_t address, uint32_t *value)
Definition: target.c:2602
const char * target_type_name(const struct target *target)
Get the target type name.
Definition: target.c:750
#define ERROR_TARGET_NOT_HALTED
Definition: target.h:818
static const char * target_name(const struct target *target)
Returns the instance-specific name of the specified target.
Definition: target.h:247
@ TARGET_HALTED
Definition: target.h:58
#define ERROR_TARGET_RESOURCE_NOT_AVAILABLE
Definition: target.h:822
static struct ublast_lowlevel_priv info
#define NULL
Definition: usb.h:16
uint8_t cmd
Definition: vdebug.c:1
uint8_t offset[4]
Definition: vdebug.c:9
uint8_t count[4]
Definition: vdebug.c:22