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