OpenOCD
xmc4xxx.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /**************************************************************************
4 * Copyright (C) 2015 Jeff Ciesielski <jeffciesielski@gmail.com> *
5 ***************************************************************************/
6 
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10 
11 #include "imp.h"
12 #include <helper/binarybuffer.h>
13 #include <target/algorithm.h>
14 #include <target/armv7m.h>
15 
16 /* Maximum number of sectors */
17 #define MAX_XMC_SECTORS 12
18 
19 /* System control unit registers */
20 #define SCU_REG_BASE 0x50004000
21 
22 #define SCU_ID_CHIP 0x04
23 
24 /* Base of the non-cached flash memory */
25 #define PFLASH_BASE 0x0C000000
26 
27 /* User configuration block offsets */
28 #define UCB0_BASE 0x00000000
29 #define UCB1_BASE 0x00000400
30 #define UCB2_BASE 0x00000800
31 
32 /* Flash register base */
33 #define FLASH_REG_BASE 0x58000000
34 
35 /* PMU ID Registers */
36 #define FLASH_REG_PMU_ID (FLASH_REG_BASE | 0x0508)
37 
38 /* PMU Fields */
39 #define PMU_MOD_REV_MASK 0xFF
40 #define PMU_MOD_TYPE_MASK 0xFF00
41 #define PMU_MOD_NO_MASK 0xFFFF0000
42 
43 /* Prefetch Config */
44 #define FLASH_REG_PREF_PCON (FLASH_REG_BASE | 0x4000)
45 
46 /* Prefetch Fields */
47 #define PCON_IBYP (1 << 0)
48 #define PCON_IINV (1 << 1)
49 
50 /* Flash ID Register */
51 #define FLASH_REG_FLASH0_ID (FLASH_REG_BASE | 0x2008)
52 
53 /* Flash Status Register */
54 #define FLASH_REG_FLASH0_FSR (FLASH_REG_BASE | 0x2010)
55 
56 #define FSR_PBUSY (0)
57 #define FSR_FABUSY (1)
58 #define FSR_PROG (4)
59 #define FSR_ERASE (5)
60 #define FSR_PFPAGE (6)
61 #define FSR_PFOPER (8)
62 #define FSR_SQER (10)
63 #define FSR_PROER (11)
64 #define FSR_PFSBER (12)
65 #define FSR_PFDBER (14)
66 #define FSR_PROIN (16)
67 #define FSR_RPROIN (18)
68 #define FSR_RPRODIS (19)
69 #define FSR_WPROIN0 (21)
70 #define FSR_WPROIN1 (22)
71 #define FSR_WPROIN2 (23)
72 #define FSR_WPRODIS0 (25)
73 #define FSR_WPRODIS1 (26)
74 #define FSR_SLM (28)
75 #define FSR_VER (31)
76 
77 #define FSR_PBUSY_MASK (0x01 << FSR_PBUSY)
78 #define FSR_FABUSY_MASK (0x01 << FSR_FABUSY)
79 #define FSR_PROG_MASK (0x01 << FSR_PROG)
80 #define FSR_ERASE_MASK (0x01 << FSR_ERASE)
81 #define FSR_PFPAGE_MASK (0x01 << FSR_PFPAGE)
82 #define FSR_PFOPER_MASK (0x01 << FSR_PFOPER)
83 #define FSR_SQER_MASK (0x01 << FSR_SQER)
84 #define FSR_PROER_MASK (0x01 << FSR_PROER)
85 #define FSR_PFSBER_MASK (0x01 << FSR_PFSBER)
86 #define FSR_PFDBER_MASK (0x01 << FSR_PFDBER)
87 #define FSR_PROIN_MASK (0x01 << FSR_PROIN)
88 #define FSR_RPROIN_MASK (0x01 << FSR_RPROIN)
89 #define FSR_RPRODIS_MASK (0x01 << FSR_RPRODIS)
90 #define FSR_WPROIN0_MASK (0x01 << FSR_WPROIN0)
91 #define FSR_WPROIN1_MASK (0x01 << FSR_WPROIN1)
92 #define FSR_WPROIN2_MASK (0x01 << FSR_WPROIN2)
93 #define FSR_WPRODIS0_MASK (0x01 << FSR_WPRODIS0)
94 #define FSR_WPRODIS1_MASK (0x01 << FSR_WPRODIS1)
95 #define FSR_SLM_MASK (0x01 << FSR_SLM)
96 #define FSR_VER_MASK (0x01 << FSR_VER)
97 
98 /* Flash Config Register */
99 #define FLASH_REG_FLASH0_FCON (FLASH_REG_BASE | 0x2014)
100 
101 #define FCON_WSPFLASH (0)
102 #define FCON_WSECPF (4)
103 #define FCON_IDLE (13)
104 #define FCON_ESLDIS (14)
105 #define FCON_SLEEP (15)
106 #define FCON_RPA (16)
107 #define FCON_DCF (17)
108 #define FCON_DDF (18)
109 #define FCON_VOPERM (24)
110 #define FCON_SQERM (25)
111 #define FCON_PROERM (26)
112 #define FCON_PFSBERM (27)
113 #define FCON_PFDBERM (29)
114 #define FCON_EOBM (31)
115 
116 #define FCON_WSPFLASH_MASK (0x0f << FCON_WSPFLASH)
117 #define FCON_WSECPF_MASK (0x01 << FCON_WSECPF)
118 #define FCON_IDLE_MASK (0x01 << FCON_IDLE)
119 #define FCON_ESLDIS_MASK (0x01 << FCON_ESLDIS)
120 #define FCON_SLEEP_MASK (0x01 << FCON_SLEEP)
121 #define FCON_RPA_MASK (0x01 << FCON_RPA)
122 #define FCON_DCF_MASK (0x01 << FCON_DCF)
123 #define FCON_DDF_MASK (0x01 << FCON_DDF)
124 #define FCON_VOPERM_MASK (0x01 << FCON_VOPERM)
125 #define FCON_SQERM_MASK (0x01 << FCON_SQERM)
126 #define FCON_PROERM_MASK (0x01 << FCON_PROERM)
127 #define FCON_PFSBERM_MASK (0x01 << FCON_PFSBERM)
128 #define FCON_PFDBERM_MASK (0x01 << FCON_PFDBERM)
129 #define FCON_EOBM_MASK (0x01 << FCON_EOBM)
130 
131 /* Flash Margin Control Register */
132 #define FLASH_REG_FLASH0_MARP (FLASH_REG_BASE | 0x2018)
133 
134 #define MARP_MARGIN (0)
135 #define MARP_TRAPDIS (15)
136 
137 #define MARP_MARGIN_MASK (0x0f << MARP_MARGIN)
138 #define MARP_TRAPDIS_MASK (0x01 << MARP_TRAPDIS)
139 
140 /* Flash Protection Registers */
141 #define FLASH_REG_FLASH0_PROCON0 (FLASH_REG_BASE | 0x2020)
142 #define FLASH_REG_FLASH0_PROCON1 (FLASH_REG_BASE | 0x2024)
143 #define FLASH_REG_FLASH0_PROCON2 (FLASH_REG_BASE | 0x2028)
144 
145 #define PROCON_S0L (0)
146 #define PROCON_S1L (1)
147 #define PROCON_S2L (2)
148 #define PROCON_S3L (3)
149 #define PROCON_S4L (4)
150 #define PROCON_S5L (5)
151 #define PROCON_S6L (6)
152 #define PROCON_S7L (7)
153 #define PROCON_S8L (8)
154 #define PROCON_S9L (9)
155 #define PROCON_S10_S11L (10)
156 #define PROCON_RPRO (15)
157 
158 #define PROCON_S0L_MASK (0x01 << PROCON_S0L)
159 #define PROCON_S1L_MASK (0x01 << PROCON_S1L)
160 #define PROCON_S2L_MASK (0x01 << PROCON_S2L)
161 #define PROCON_S3L_MASK (0x01 << PROCON_S3L)
162 #define PROCON_S4L_MASK (0x01 << PROCON_S4L)
163 #define PROCON_S5L_MASK (0x01 << PROCON_S5L)
164 #define PROCON_S6L_MASK (0x01 << PROCON_S6L)
165 #define PROCON_S7L_MASK (0x01 << PROCON_S7L)
166 #define PROCON_S8L_MASK (0x01 << PROCON_S8L)
167 #define PROCON_S9L_MASK (0x01 << PROCON_S9L)
168 #define PROCON_S10_S11L_MASK (0x01 << PROCON_S10_S11L)
169 #define PROCON_RPRO_MASK (0x01 << PROCON_RPRO)
170 
171 #define FLASH_PROTECT_CONFIRMATION_CODE 0x8AFE15C3
172 
173 /* Flash controller configuration values */
174 #define FLASH_ID_XMC4500 0xA2
175 #define FLASH_ID_XMC4300_XMC4700_4800 0x92
176 #define FLASH_ID_XMC4100_4200 0x9C
177 #define FLASH_ID_XMC4400 0x9F
178 
179 /* Timeouts */
180 #define FLASH_OP_TIMEOUT 5000
181 
182 /* Flash commands (write/erase/protect) are performed using special
183  * command sequences that are written to magic addresses in the flash controller */
184 /* Command sequence addresses. See reference manual, section 8: Flash Command Sequences */
185 #define FLASH_CMD_ERASE_1 0x0C005554
186 #define FLASH_CMD_ERASE_2 0x0C00AAA8
187 #define FLASH_CMD_ERASE_3 FLASH_CMD_ERASE_1
188 #define FLASH_CMD_ERASE_4 FLASH_CMD_ERASE_1
189 #define FLASH_CMD_ERASE_5 FLASH_CMD_ERASE_2
190 /* ERASE_6 is the sector base address */
191 
192 #define FLASH_CMD_CLEAR_STATUS FLASH_CMD_ERASE_1
193 
194 #define FLASH_CMD_ENTER_PAGEMODE FLASH_CMD_ERASE_1
195 
196 #define FLASH_CMD_LOAD_PAGE_1 0x0C0055F0
197 #define FLASH_CMD_LOAD_PAGE_2 0x0C0055F4
198 
199 #define FLASH_CMD_WRITE_PAGE_1 FLASH_CMD_ERASE_1
200 #define FLASH_CMD_WRITE_PAGE_2 FLASH_CMD_ERASE_2
201 #define FLASH_CMD_WRITE_PAGE_3 FLASH_CMD_ERASE_1
202 /* WRITE_PAGE_4 is the page base address */
203 
204 #define FLASH_CMD_TEMP_UNPROT_1 FLASH_CMD_ERASE_1
205 #define FLASH_CMD_TEMP_UNPROT_2 FLASH_CMD_ERASE_2
206 #define FLASH_CMD_TEMP_UNPROT_3 0x0C00553C
207 #define FLASH_CMD_TEMP_UNPROT_4 FLASH_CMD_ERASE_2
208 #define FLASH_CMD_TEMP_UNPROT_5 FLASH_CMD_ERASE_2
209 #define FLASH_CMD_TEMP_UNPROT_6 0x0C005558
210 
212  bool probed;
213 
214  /* We need the flash controller ID to choose the sector layout */
215  uint32_t fcon_id;
216 
217  /* Passwords used for protection operations */
218  uint32_t pw1;
219  uint32_t pw2;
220  bool pw_set;
221 
222  /* Protection flags */
224 
226 };
227 
229  uint32_t address;
230  uint32_t magic;
231 };
232 
233 /* Sector capacities. See section 8 of xmc4x00_rm */
234 static const unsigned int sector_capacity_9[9] = {
235  16, 16, 16, 16, 16, 16, 16, 16, 128
236 };
237 
238 static const unsigned int sector_capacity_10[10] = {
239  16, 16, 16, 16, 16, 16, 16, 16, 128, 256
240 };
241 
242 static const unsigned int sector_capacity_12[12] = {
243  16, 16, 16, 16, 16, 16, 16, 16, 128, 256, 256, 256
244 };
245 
246 static const unsigned int sector_capacity_16[16] = {
247  16, 16, 16, 16, 16, 16, 16, 16, 128, 256, 256, 256, 256, 256, 256, 256
248 };
249 
251  struct xmc4xxx_command_seq *seq,
252  int seq_len)
253 {
254  int res = ERROR_OK;
255 
256  for (int i = 0; i < seq_len; i++) {
257  res = target_write_u32(bank->target, seq[i].address,
258  seq[i].magic);
259  if (res != ERROR_OK)
260  return res;
261  }
262 
263  return ERROR_OK;
264 }
265 
267 {
268  const unsigned int *capacity = NULL;
269 
270  /* At this point, we know which flash controller ID we're
271  * talking to and simply need to fill out the bank structure accordingly */
272  LOG_DEBUG("%u sectors", bank->num_sectors);
273 
274  switch (bank->num_sectors) {
275  case 9:
276  capacity = sector_capacity_9;
277  break;
278  case 10:
279  capacity = sector_capacity_10;
280  break;
281  case 12:
282  capacity = sector_capacity_12;
283  break;
284  case 16:
285  capacity = sector_capacity_16;
286  break;
287  default:
288  LOG_ERROR("Unexpected number of sectors, %u\n",
289  bank->num_sectors);
290  return ERROR_FAIL;
291  }
292 
293  /* This looks like a bank that we understand, now we know the
294  * corresponding sector capacities and we can add those up into the
295  * bank size. */
296  uint32_t total_offset = 0;
297  bank->sectors = calloc(bank->num_sectors,
298  sizeof(struct flash_sector));
299  for (unsigned int i = 0; i < bank->num_sectors; i++) {
300  bank->sectors[i].size = capacity[i] * 1024;
301  bank->sectors[i].offset = total_offset;
302  bank->sectors[i].is_erased = -1;
303  bank->sectors[i].is_protected = -1;
304 
305  bank->size += bank->sectors[i].size;
306  LOG_DEBUG("\t%d: %uk", i, capacity[i]);
307  total_offset += bank->sectors[i].size;
308  }
309 
310  /* This part doesn't follow the typical standard of 0xff
311  * being the erased value.*/
312  bank->default_padded_value = bank->erased_value = 0x00;
313 
314  return ERROR_OK;
315 }
316 
317 static int xmc4xxx_probe(struct flash_bank *bank)
318 {
319  int res;
320  uint32_t devid, config;
321  struct xmc4xxx_flash_bank *fb = bank->driver_priv;
322  uint8_t flash_id;
323 
324  if (fb->probed)
325  return ERROR_OK;
326 
327  /* It's not possible for the DAP to access the OTP locations needed for
328  * probing the part info and Flash geometry so we require that the target
329  * be halted before proceeding. */
330  if (bank->target->state != TARGET_HALTED) {
331  LOG_WARNING("Cannot communicate... target not halted.");
333  }
334 
335  /* The SCU registers contain the ID of the chip */
336  res = target_read_u32(bank->target, SCU_REG_BASE + SCU_ID_CHIP, &devid);
337  if (res != ERROR_OK) {
338  LOG_ERROR("Cannot read device identification register.");
339  return res;
340  }
341 
342  /* Make sure this is a XMC4000 family device */
343  if ((devid & 0xF0000) != 0x40000 && devid != 0) {
344  LOG_ERROR("Platform ID doesn't match XMC4xxx: 0x%08" PRIx32, devid);
345  return ERROR_FAIL;
346  }
347 
348  LOG_DEBUG("Found XMC4xxx with devid: 0x%08" PRIx32, devid);
349 
350  /* Now sanity-check the Flash controller itself. */
352  &config);
353  if (res != ERROR_OK) {
354  LOG_ERROR("Cannot read Flash bank configuration.");
355  return res;
356  }
357  flash_id = (config & 0xff0000) >> 16;
358 
359  /* The Flash configuration register is our only means of
360  * determining the sector layout. We need to make sure that
361  * we understand the type of controller we're dealing with */
362  switch (flash_id) {
364  bank->num_sectors = 9;
365  LOG_DEBUG("XMC4xxx: XMC4100/4200 detected.");
366  break;
367  case FLASH_ID_XMC4400:
368  bank->num_sectors = 10;
369  LOG_DEBUG("XMC4xxx: XMC4400 detected.");
370  break;
371  case FLASH_ID_XMC4500:
372  bank->num_sectors = 12;
373  LOG_DEBUG("XMC4xxx: XMC4500 detected.");
374  break;
376  bank->num_sectors = 16;
377  LOG_DEBUG("XMC4xxx: XMC4700/4800 detected.");
378  break;
379  default:
380  LOG_ERROR("XMC4xxx: Unexpected flash ID. got %02" PRIx8,
381  flash_id);
382  return ERROR_FAIL;
383  }
384 
385  /* Retrieve information about the particular bank we're probing and fill in
386  * the bank structure accordingly. */
388  if (res == ERROR_OK) {
389  /* We're done */
390  fb->probed = true;
391  } else {
392  LOG_ERROR("Unable to load bank information.");
393  return ERROR_FAIL;
394  }
395 
396  return ERROR_OK;
397 }
398 
400  unsigned int sector, uint32_t *ret_addr)
401 {
402  /* Make sure we understand this sector */
403  if (sector > bank->num_sectors)
404  return ERROR_FAIL;
405 
406  *ret_addr = bank->base + bank->sectors[sector].offset;
407 
408  return ERROR_OK;
409 
410 }
411 
413 {
414  int res;
415  /* TODO: Do we need to check for sequence error? */
416  LOG_INFO("Clearing flash status");
418  0xF5);
419  if (res != ERROR_OK) {
420  LOG_ERROR("Unable to write erase command sequence");
421  return res;
422  }
423 
424  return ERROR_OK;
425 }
426 
427 static int xmc4xxx_get_flash_status(struct flash_bank *bank, uint32_t *status)
428 {
429  int res;
430 
432 
433  if (res != ERROR_OK)
434  LOG_ERROR("Cannot read flash status register.");
435 
436  return res;
437 }
438 
440 {
441  int res;
442  uint32_t status;
443 
445  if (res != ERROR_OK)
446  return res;
447 
448  /* While the flash controller is busy, wait */
449  while (status & FSR_PBUSY_MASK) {
451  if (res != ERROR_OK)
452  return res;
453 
454  if (timeout-- <= 0) {
455  LOG_ERROR("Timed out waiting for flash");
456  return ERROR_FAIL;
457  }
458  alive_sleep(1);
459  keep_alive();
460  }
461 
462  if (status & FSR_PROER_MASK) {
463  LOG_ERROR("XMC4xxx flash protected");
464  res = ERROR_FAIL;
465  }
466 
467  return res;
468 }
469 
470 static int xmc4xxx_erase_sector(struct flash_bank *bank, uint32_t address,
471  bool user_config)
472 {
473  int res;
474  uint32_t status;
475 
476  /* See reference manual table 8.4: Command Sequences for Flash Control */
477  struct xmc4xxx_command_seq erase_cmd_seq[6] = {
478  {FLASH_CMD_ERASE_1, 0xAA},
479  {FLASH_CMD_ERASE_2, 0x55},
480  {FLASH_CMD_ERASE_3, 0x80},
481  {FLASH_CMD_ERASE_4, 0xAA},
482  {FLASH_CMD_ERASE_5, 0x55},
483  {0xFF, 0xFF} /* Needs filled in */
484  };
485 
486  /* We need to fill in the base address of the sector we'll be
487  * erasing, as well as the magic code that determines whether
488  * this is a standard flash sector or a user configuration block */
489 
490  erase_cmd_seq[5].address = address;
491  if (user_config) {
492  /* Removing flash protection requires the addition of
493  * the base address */
494  erase_cmd_seq[5].address += bank->base;
495  erase_cmd_seq[5].magic = 0xC0;
496  } else {
497  erase_cmd_seq[5].magic = 0x30;
498  }
499 
500  res = xmc4xxx_write_command_sequence(bank, erase_cmd_seq,
501  ARRAY_SIZE(erase_cmd_seq));
502  if (res != ERROR_OK)
503  return res;
504 
505  /* Read the flash status register */
507  if (res != ERROR_OK) {
508  LOG_ERROR("Cannot read flash status register.");
509  return res;
510  }
511 
512  /* Check for a sequence error */
513  if (status & FSR_SQER_MASK) {
514  LOG_ERROR("Error with flash erase sequence");
515  return ERROR_FAIL;
516  }
517 
518  /* Make sure a flash erase was triggered */
519  if (!(status & FSR_ERASE_MASK)) {
520  LOG_ERROR("Flash failed to erase");
521  return ERROR_FAIL;
522  }
523 
524  /* Now we must wait for the erase operation to end */
526 }
527 
528 static int xmc4xxx_erase(struct flash_bank *bank, unsigned int first,
529  unsigned int last)
530 {
531  struct xmc4xxx_flash_bank *fb = bank->driver_priv;
532  int res;
533 
534  if (bank->target->state != TARGET_HALTED) {
535  LOG_ERROR("Unable to erase, target is not halted");
537  }
538 
539  if (!fb->probed) {
540  res = xmc4xxx_probe(bank);
541  if (res != ERROR_OK)
542  return res;
543  }
544 
545  uint32_t tmp_addr;
546  /* Loop through the sectors and erase each one */
547  for (unsigned int i = first; i <= last; i++) {
548  res = xmc4xxx_get_sector_start_addr(bank, i, &tmp_addr);
549  if (res != ERROR_OK) {
550  LOG_ERROR("Invalid sector %u", i);
551  return res;
552  }
553 
554  LOG_DEBUG("Erasing sector %u @ 0x%08"PRIx32, i, tmp_addr);
555 
556  res = xmc4xxx_erase_sector(bank, tmp_addr, false);
557  if (res != ERROR_OK) {
558  LOG_ERROR("Unable to write erase command sequence");
559  goto clear_status_and_exit;
560  }
561 
562  /* Now we must wait for the erase operation to end */
564 
565  if (res != ERROR_OK)
566  goto clear_status_and_exit;
567  }
568 
569 clear_status_and_exit:
571 
572 }
573 
575 {
576  int res;
577  uint32_t status;
578 
579  res = target_write_u32(bank->target, FLASH_CMD_ENTER_PAGEMODE, 0x50);
580  if (res != ERROR_OK) {
581  LOG_ERROR("Unable to write enter page mode command");
582  return ERROR_FAIL;
583  }
584 
586 
587  if (res != ERROR_OK)
588  return res;
589 
590  /* Make sure we're in page mode */
591  if (!(status & FSR_PFPAGE_MASK)) {
592  LOG_ERROR("Unable to enter page mode");
593  return ERROR_FAIL;
594  }
595 
596  /* Make sure we didn't encounter a sequence error */
597  if (status & FSR_SQER_MASK) {
598  LOG_ERROR("Sequence error while entering page mode");
599  return ERROR_FAIL;
600  }
601 
602  return res;
603 }
604 
605 static int xmc4xxx_write_page(struct flash_bank *bank, const uint8_t *pg_buf,
606  uint32_t offset, bool user_config)
607 {
608  int res;
609  uint32_t status;
610 
611  /* Base of the flash write command */
612  struct xmc4xxx_command_seq write_cmd_seq[4] = {
613  {FLASH_CMD_WRITE_PAGE_1, 0xAA},
614  {FLASH_CMD_WRITE_PAGE_2, 0x55},
615  {FLASH_CMD_WRITE_PAGE_3, 0xFF}, /* Needs filled in */
616  {0xFF, 0xFF} /* Needs filled in */
617  };
618 
619  /* The command sequence differs depending on whether this is
620  * being written to standard flash or the user configuration
621  * area */
622  if (user_config)
623  write_cmd_seq[2].magic = 0xC0;
624  else
625  write_cmd_seq[2].magic = 0xA0;
626 
627  /* Finally, we need to add the address that this page will be
628  * written to */
629  write_cmd_seq[3].address = bank->base + offset;
630  write_cmd_seq[3].magic = 0xAA;
631 
632 
633  /* Flash pages are written 256 bytes at a time. For each 256
634  * byte chunk, we need to:
635  * 1. Enter page mode. This activates the flash write buffer
636  * 2. Load the page buffer with data (2x 32 bit words at a time)
637  * 3. Burn the page buffer into its intended location
638  * If the starting offset is not on a 256 byte boundary, we
639  * will need to pad the beginning of the write buffer
640  * accordingly. Likewise, if the last page does not fill the
641  * buffer, we should pad it to avoid leftover data from being
642  * written to flash
643  */
645  if (res != ERROR_OK)
646  return res;
647 
648  /* Copy the data into the page buffer*/
649  for (int i = 0; i < 256; i += 8) {
650  uint32_t w_lo = target_buffer_get_u32(bank->target, &pg_buf[i]);
651  uint32_t w_hi = target_buffer_get_u32(bank->target, &pg_buf[i + 4]);
652  LOG_DEBUG("WLO: %08"PRIx32, w_lo);
653  LOG_DEBUG("WHI: %08"PRIx32, w_hi);
654 
655  /* Data is loaded 2x 32 bit words at a time */
656  res = target_write_u32(bank->target, FLASH_CMD_LOAD_PAGE_1, w_lo);
657  if (res != ERROR_OK)
658  return res;
659 
660  res = target_write_u32(bank->target, FLASH_CMD_LOAD_PAGE_2, w_hi);
661  if (res != ERROR_OK)
662  return res;
663 
664  /* Check for an error */
666  if (res != ERROR_OK)
667  return res;
668 
669  if (status & FSR_SQER_MASK) {
670  LOG_ERROR("Error loading page buffer");
671  return ERROR_FAIL;
672  }
673  }
674 
675  /* The page buffer is now full, time to commit it to flash */
676 
677  res = xmc4xxx_write_command_sequence(bank, write_cmd_seq, ARRAY_SIZE(write_cmd_seq));
678  if (res != ERROR_OK) {
679  LOG_ERROR("Unable to enter write command sequence");
680  return res;
681  }
682 
683  /* Read the flash status register */
685  if (res != ERROR_OK)
686  return res;
687 
688  /* Check for a sequence error */
689  if (status & FSR_SQER_MASK) {
690  LOG_ERROR("Error with flash write sequence");
691  return ERROR_FAIL;
692  }
693 
694  /* Make sure a flash write was triggered */
695  if (!(status & FSR_PROG_MASK)) {
696  LOG_ERROR("Failed to write flash page");
697  return ERROR_FAIL;
698  }
699 
700  /* Wait for the write operation to end */
702  if (res != ERROR_OK)
703  return res;
704 
705  /* TODO: Verify that page was written without error */
706  return res;
707 }
708 
709 static int xmc4xxx_write(struct flash_bank *bank, const uint8_t *buffer,
710  uint32_t offset, uint32_t count)
711 {
712  struct xmc4xxx_flash_bank *fb = bank->driver_priv;
713  int res = ERROR_OK;
714 
715  if (bank->target->state != TARGET_HALTED) {
716  LOG_ERROR("Unable to erase, target is not halted");
718  }
719 
720  if (!fb->probed) {
721  res = xmc4xxx_probe(bank);
722  if (res != ERROR_OK)
723  return res;
724  }
725 
726  /* Make sure we won't run off the end of the flash bank */
727  if ((offset + count) > (bank->size)) {
728  LOG_ERROR("Attempting to write past the end of flash");
729  return ERROR_FAIL;
730  }
731 
732 
733  /* Attempt to write the passed in buffer to flash */
734  /* Pages are written 256 bytes at a time, we need to handle
735  * scenarios where padding is required at the beginning and
736  * end of a page */
737  while (count) {
738  /* page working area */
739  uint8_t tmp_buf[256] = {0};
740 
741  /* Amount of data we'll be writing to this page */
742  int remaining;
743  int end_pad;
744 
745  remaining = MIN(count, sizeof(tmp_buf));
746  end_pad = sizeof(tmp_buf) - remaining;
747 
748  /* Make sure we're starting on a page boundary */
749  int start_pad = offset % 256;
750  if (start_pad) {
751  LOG_INFO("Write does not start on a 256 byte boundary. "
752  "Padding by %d bytes", start_pad);
753  memset(tmp_buf, 0xff, start_pad);
754  /* Subtract the amount of start offset from
755  * the amount of data we'll need to write */
756  remaining -= start_pad;
757  }
758 
759  /* Remove the amount we'll be writing from the total count */
760  count -= remaining;
761 
762  /* Now copy in the remaining data */
763  memcpy(&tmp_buf[start_pad], buffer, remaining);
764 
765  if (end_pad) {
766  LOG_INFO("Padding end of page @" TARGET_ADDR_FMT " by %d bytes",
767  bank->base + offset, end_pad);
768  memset(&tmp_buf[256 - end_pad], 0xff, end_pad);
769  }
770 
771  /* Now commit this page to flash, if there was start
772  * padding, we should subtract that from the target offset */
773  res = xmc4xxx_write_page(bank, tmp_buf, (offset - start_pad), false);
774  if (res != ERROR_OK) {
775  LOG_ERROR("Unable to write flash page");
776  goto abort_write_and_exit;
777  }
778 
779  /* Advance the buffer pointer */
780  buffer += remaining;
781 
782  /* Advance the offset */
783  offset += remaining;
784  }
785 
786 abort_write_and_exit:
788  return res;
789 
790 }
791 
793 {
794  struct xmc4xxx_flash_bank *fb = bank->driver_priv;
795  uint32_t scu_idcode;
796 
797  if (bank->target->state != TARGET_HALTED) {
798  LOG_WARNING("Cannot communicate... target not halted.");
800  }
801 
802  /* The SCU registers contain the ID of the chip */
803  int res = target_read_u32(bank->target, SCU_REG_BASE + SCU_ID_CHIP, &scu_idcode);
804  if (res != ERROR_OK) {
805  LOG_ERROR("Cannot read device identification register.");
806  return res;
807  }
808 
809  uint16_t dev_id = (scu_idcode & 0xfff0) >> 4;
810  uint16_t rev_id = scu_idcode & 0xf;
811  const char *dev_str;
812  const char *rev_str = NULL;
813 
814  switch (dev_id) {
815  case 0x100:
816  dev_str = "XMC4100";
817 
818  switch (rev_id) {
819  case 0x1:
820  rev_str = "AA";
821  break;
822  case 0x2:
823  rev_str = "AB";
824  break;
825  }
826  break;
827  case 0x200:
828  dev_str = "XMC4200";
829 
830  switch (rev_id) {
831  case 0x1:
832  rev_str = "AA";
833  break;
834  case 0x2:
835  rev_str = "AB";
836  break;
837  }
838  break;
839  case 0x300:
840  dev_str = "XMC4300";
841 
842  switch (rev_id) {
843  case 0x1:
844  rev_str = "AA";
845  }
846  break;
847  case 0x400:
848  dev_str = "XMC4400";
849 
850  switch (rev_id) {
851  case 0x1:
852  rev_str = "AA";
853  break;
854  case 0x2:
855  rev_str = "AB";
856  break;
857  }
858  break;
859  case 0:
860  /* XMC4500 EES AA13 with date codes before GE212
861  * had zero SCU_IDCHIP
862  */
863  dev_str = "XMC4500 EES";
864  rev_str = "AA13";
865  break;
866  case 0x500:
867  dev_str = "XMC4500";
868 
869  switch (rev_id) {
870  case 0x2:
871  rev_str = "AA";
872  break;
873  case 0x3:
874  rev_str = "AB";
875  break;
876  case 0x4:
877  rev_str = "AC";
878  break;
879  }
880  break;
881  case 0x700:
882  dev_str = "XMC4700";
883 
884  switch (rev_id) {
885  case 0x1:
886  rev_str = "EES-AA";
887  break;
888  }
889  break;
890  case 0x800:
891  dev_str = "XMC4800";
892 
893  switch (rev_id) {
894  case 0x1:
895  rev_str = "EES-AA";
896  break;
897  }
898  break;
899 
900  default:
901  command_print_sameline(cmd, "Cannot identify target as an XMC4xxx. SCU_ID: %"PRIx32 "\n", scu_idcode);
902  return ERROR_OK;
903  }
904 
905  /* String to declare protection data held in the private driver */
906  char prot_str[512] = {0};
907  if (fb->read_protected)
908  snprintf(prot_str, sizeof(prot_str), "\nFlash is read protected");
909 
910  bool otp_enabled = false;
911  for (unsigned int i = 0; i < bank->num_sectors; i++)
912  if (fb->write_prot_otp[i])
913  otp_enabled = true;
914 
915  /* If OTP Write protection is enabled (User 2), list each
916  * sector that has it enabled */
917  char otp_str[14];
918  if (otp_enabled) {
919  strcat(prot_str, "\nOTP Protection is enabled for sectors:\n");
920  for (unsigned int i = 0; i < bank->num_sectors; i++) {
921  if (fb->write_prot_otp[i]) {
922  snprintf(otp_str, sizeof(otp_str), "- %d\n", i);
923  strncat(prot_str, otp_str, sizeof(prot_str) - strlen(prot_str) - 1);
924  }
925  }
926  }
927 
928  if (rev_str)
929  command_print_sameline(cmd, "%s - Rev: %s%s", dev_str, rev_str, prot_str);
930  else
931  command_print_sameline(cmd, "%s - Rev: unknown (0x%01x)%s", dev_str, rev_id, prot_str);
932 
933  return ERROR_OK;
934 }
935 
936 static int xmc4xxx_temp_unprotect(struct flash_bank *bank, int user_level)
937 {
938  struct xmc4xxx_flash_bank *fb;
939  int res = ERROR_OK;
940  uint32_t status = 0;
941 
942  struct xmc4xxx_command_seq temp_unprot_seq[6] = {
943  {FLASH_CMD_TEMP_UNPROT_1, 0xAA},
944  {FLASH_CMD_TEMP_UNPROT_2, 0x55},
945  {FLASH_CMD_TEMP_UNPROT_3, 0xFF}, /* Needs filled in */
946  {FLASH_CMD_TEMP_UNPROT_4, 0xFF}, /* Needs filled in */
947  {FLASH_CMD_TEMP_UNPROT_5, 0xFF}, /* Needs filled in */
949  };
950 
951  if (user_level < 0 || user_level > 2) {
952  LOG_ERROR("Invalid user level, must be 0-2");
953  return ERROR_FAIL;
954  }
955 
956  fb = bank->driver_priv;
957 
958  /* Fill in the user level and passwords */
959  temp_unprot_seq[2].magic = user_level;
960  temp_unprot_seq[3].magic = fb->pw1;
961  temp_unprot_seq[4].magic = fb->pw2;
962 
963  res = xmc4xxx_write_command_sequence(bank, temp_unprot_seq,
964  ARRAY_SIZE(temp_unprot_seq));
965  if (res != ERROR_OK) {
966  LOG_ERROR("Unable to write temp unprotect sequence");
967  return res;
968  }
969 
971  if (res != ERROR_OK)
972  return res;
973 
974  if (status & FSR_WPRODIS0) {
975  LOG_INFO("Flash is temporarily unprotected");
976  } else {
977  LOG_INFO("Unable to disable flash protection");
978  res = ERROR_FAIL;
979  }
980 
981 
982  return res;
983 }
984 
985 static int xmc4xxx_flash_unprotect(struct flash_bank *bank, int32_t level)
986 {
987  uint32_t addr;
988  int res;
989 
990  switch (level) {
991  case 0:
992  addr = UCB0_BASE;
993  break;
994  case 1:
995  addr = UCB1_BASE;
996  break;
997  default:
998  LOG_ERROR("Invalid user level. Must be 0-1");
999  return ERROR_FAIL;
1000  }
1001 
1002  res = xmc4xxx_erase_sector(bank, addr, true);
1003 
1004  if (res != ERROR_OK)
1005  LOG_ERROR("Error erasing user configuration block");
1006 
1007  return res;
1008 }
1009 
1010 /* Reference: "XMC4500 Flash Protection.pptx" app note */
1011 static int xmc4xxx_flash_protect(struct flash_bank *bank, int level, bool read_protect,
1012  unsigned int first, unsigned int last)
1013 {
1014  /* User configuration block buffers */
1015  uint8_t ucp0_buf[8 * sizeof(uint32_t)] = {0};
1016  uint32_t ucb_base = 0;
1017  uint32_t procon = 0;
1018  int res = ERROR_OK;
1019  uint32_t status = 0;
1020  bool proin = false;
1021 
1022  struct xmc4xxx_flash_bank *fb = bank->driver_priv;
1023 
1024  /* Read protect only works for user 0, make sure we don't try
1025  * to do something silly */
1026  if (level != 0 && read_protect) {
1027  LOG_ERROR("Read protection is for user level 0 only!");
1028  return ERROR_FAIL;
1029  }
1030 
1031  /* Check to see if protection is already installed for the
1032  * specified user level. If it is, the user configuration
1033  * block will need to be erased before we can continue */
1034 
1035  /* Grab the flash status register*/
1037  if (res != ERROR_OK)
1038  return res;
1039 
1040  switch (level) {
1041  case 0:
1043  proin = true;
1044  break;
1045  case 1:
1046  if (status & FSR_WPROIN1_MASK)
1047  proin = true;
1048  break;
1049  case 2:
1050  if (status & FSR_WPROIN2_MASK)
1051  proin = true;
1052  break;
1053  }
1054 
1055  if (proin) {
1056  LOG_ERROR("Flash protection is installed for user %d"
1057  " and must be removed before continuing", level);
1058  return ERROR_FAIL;
1059  }
1060 
1061  /* If this device has 12 flash sectors, protection for
1062  * sectors 10 & 11 are handled jointly. If we are trying to
1063  * write all sectors, we should decrement
1064  * last to ensure we don't write to a register bit that
1065  * doesn't exist*/
1066  if ((bank->num_sectors == 12) && (last == 12))
1067  last--;
1068 
1069  /* We need to fill out the procon register representation
1070  * that we will be writing to the device */
1071  for (unsigned int i = first; i <= last; i++)
1072  procon |= 1 << i;
1073 
1074  /* If read protection is requested, set the appropriate bit
1075  * (we checked that this is allowed above) */
1076  if (read_protect)
1077  procon |= PROCON_RPRO_MASK;
1078 
1079  LOG_DEBUG("Setting flash protection with procon:");
1080  LOG_DEBUG("PROCON: %"PRIx32, procon);
1081 
1082  /* First we need to copy in the procon register to the buffer
1083  * we're going to attempt to write. This is written twice */
1084  target_buffer_set_u32(bank->target, &ucp0_buf[0 * 4], procon);
1085  target_buffer_set_u32(bank->target, &ucp0_buf[2 * 4], procon);
1086 
1087  /* Now we must copy in both flash passwords. As with the
1088  * procon data, this must be written twice (4 total words
1089  * worth of data) */
1090  target_buffer_set_u32(bank->target, &ucp0_buf[4 * 4], fb->pw1);
1091  target_buffer_set_u32(bank->target, &ucp0_buf[5 * 4], fb->pw2);
1092  target_buffer_set_u32(bank->target, &ucp0_buf[6 * 4], fb->pw1);
1093  target_buffer_set_u32(bank->target, &ucp0_buf[7 * 4], fb->pw2);
1094 
1095  /* Finally, (if requested) we copy in the confirmation
1096  * code so that the protection is permanent and will
1097  * require a password to undo. */
1098  target_buffer_set_u32(bank->target, &ucp0_buf[0 * 4], FLASH_PROTECT_CONFIRMATION_CODE);
1099  target_buffer_set_u32(bank->target, &ucp0_buf[2 * 4], FLASH_PROTECT_CONFIRMATION_CODE);
1100 
1101  /* Now that the data is copied into place, we must write
1102  * these pages into flash */
1103 
1104  /* The user configuration block base depends on what level of
1105  * protection we're trying to install, select the proper one */
1106  switch (level) {
1107  case 0:
1108  ucb_base = UCB0_BASE;
1109  break;
1110  case 1:
1111  ucb_base = UCB1_BASE;
1112  break;
1113  case 2:
1114  ucb_base = UCB2_BASE;
1115  break;
1116  }
1117 
1118  /* Write the user config pages */
1119  res = xmc4xxx_write_page(bank, ucp0_buf, ucb_base, true);
1120  if (res != ERROR_OK) {
1121  LOG_ERROR("Error writing user configuration block 0");
1122  return res;
1123  }
1124 
1125  return ERROR_OK;
1126 }
1127 
1128 static int xmc4xxx_protect(struct flash_bank *bank, int set, unsigned int first,
1129  unsigned int last)
1130 {
1131  struct xmc4xxx_flash_bank *fb = bank->driver_priv;
1132 
1133  /* Check for flash passwords */
1134  if (!fb->pw_set) {
1135  LOG_ERROR("Flash passwords not set, use xmc4xxx flash_password to set them");
1136  return ERROR_FAIL;
1137  }
1138 
1139  /* We want to clear flash protection temporarily*/
1140  if (set == 0) {
1141  LOG_WARNING("Flash protection will be temporarily disabled"
1142  " for all pages (User 0 only)!");
1143  return xmc4xxx_temp_unprotect(bank, 0);
1144  }
1145 
1146  /* Install write protection for user 0 on the specified pages */
1147  return xmc4xxx_flash_protect(bank, 0, false, first, last);
1148 }
1149 
1151 {
1152  int ret;
1153  uint32_t protection[3] = {0};
1154  struct xmc4xxx_flash_bank *fb = bank->driver_priv;
1155 
1157  if (ret != ERROR_OK) {
1158  LOG_ERROR("Unable to read flash User0 protection register");
1159  return ret;
1160  }
1161 
1163  if (ret != ERROR_OK) {
1164  LOG_ERROR("Unable to read flash User1 protection register");
1165  return ret;
1166  }
1167 
1169  if (ret != ERROR_OK) {
1170  LOG_ERROR("Unable to read flash User2 protection register");
1171  return ret;
1172  }
1173 
1174  unsigned int sectors = bank->num_sectors;
1175 
1176  /* On devices with 12 sectors, sectors 10 & 11 are protected
1177  * together instead of individually */
1178  if (sectors == 12)
1179  sectors--;
1180 
1181  /* Clear the protection status */
1182  for (unsigned int i = 0; i < bank->num_sectors; i++) {
1183  bank->sectors[i].is_protected = 0;
1184  fb->write_prot_otp[i] = false;
1185  }
1186  fb->read_protected = false;
1187 
1188  /* The xmc4xxx series supports 3 levels of user protection
1189  * (User0, User1 (low priority), and User 2(OTP), we need to
1190  * check all 3 */
1191  for (unsigned int i = 0; i < ARRAY_SIZE(protection); i++) {
1192 
1193  /* Check for write protection on every available
1194  * sector */
1195  for (unsigned int j = 0; j < sectors; j++) {
1196  int set = (protection[i] & (1 << j)) ? 1 : 0;
1197  bank->sectors[j].is_protected |= set;
1198 
1199  /* Handle sector 11 */
1200  if (j == 10)
1201  bank->sectors[j + 1].is_protected |= set;
1202 
1203  /* User 2 indicates this protection is
1204  * permanent, make note in the private driver structure */
1205  if (i == 2 && set) {
1206  fb->write_prot_otp[j] = true;
1207 
1208  /* Handle sector 11 */
1209  if (j == 10)
1210  fb->write_prot_otp[j + 1] = true;
1211  }
1212 
1213  }
1214  }
1215 
1216  /* XMC4xxx also supports read protection, make a note
1217  * in the private driver structure */
1218  if (protection[0] & PROCON_RPRO_MASK)
1219  fb->read_protected = true;
1220 
1221  return ERROR_OK;
1222 }
1223 
1224 FLASH_BANK_COMMAND_HANDLER(xmc4xxx_flash_bank_command)
1225 {
1226  bank->driver_priv = malloc(sizeof(struct xmc4xxx_flash_bank));
1227 
1228  if (!bank->driver_priv)
1230 
1231  (void)memset(bank->driver_priv, 0, sizeof(struct xmc4xxx_flash_bank));
1232 
1233  return ERROR_OK;
1234 }
1235 
1236 COMMAND_HANDLER(xmc4xxx_handle_flash_password_command)
1237 {
1238  int res;
1239  struct flash_bank *bank;
1240 
1241  if (CMD_ARGC < 3)
1243 
1244  res = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
1245  if (res != ERROR_OK)
1246  return res;
1247 
1248  struct xmc4xxx_flash_bank *fb = bank->driver_priv;
1249 
1250  errno = 0;
1251 
1252  /* We skip over the flash bank */
1253  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], fb->pw1);
1254 
1255  if (errno)
1257 
1258  COMMAND_PARSE_NUMBER(u32, CMD_ARGV[2], fb->pw2);
1259 
1260  if (errno)
1262 
1263  fb->pw_set = true;
1264 
1265  command_print(CMD, "XMC4xxx flash passwords set to:\n");
1266  command_print(CMD, "-0x%08"PRIx32"\n", fb->pw1);
1267  command_print(CMD, "-0x%08"PRIx32"\n", fb->pw2);
1268  return ERROR_OK;
1269 }
1270 
1271 COMMAND_HANDLER(xmc4xxx_handle_flash_unprotect_command)
1272 {
1273  struct flash_bank *bank;
1274  int res;
1275  int32_t level;
1276 
1277  if (CMD_ARGC < 2)
1279 
1280  res = CALL_COMMAND_HANDLER(flash_command_get_bank, 0, &bank);
1281  if (res != ERROR_OK)
1282  return res;
1283 
1284  COMMAND_PARSE_NUMBER(s32, CMD_ARGV[1], level);
1285 
1286  return xmc4xxx_flash_unprotect(bank, level);
1287 }
1288 
1289 static const struct command_registration xmc4xxx_exec_command_handlers[] = {
1290  {
1291  .name = "flash_password",
1292  .handler = xmc4xxx_handle_flash_password_command,
1293  .mode = COMMAND_EXEC,
1294  .usage = "bank_id password1 password2",
1295  .help = "Set the flash passwords used for protect operations. "
1296  "Passwords should be in standard hex form (0x00000000). "
1297  "(You must call this before any other protect commands) "
1298  "NOTE: The xmc4xxx's UCB area only allows for FOUR cycles. "
1299  "Please use protection carefully!",
1300  },
1301  {
1302  .name = "flash_unprotect",
1303  .handler = xmc4xxx_handle_flash_unprotect_command,
1304  .mode = COMMAND_EXEC,
1305  .usage = "bank_id user_level[0-1]",
1306  .help = "Permanently Removes flash protection (read and write) "
1307  "for the specified user level",
1308  },
1310 };
1311 
1312 static const struct command_registration xmc4xxx_command_handlers[] = {
1313  {
1314  .name = "xmc4xxx",
1315  .mode = COMMAND_ANY,
1316  .help = "xmc4xxx flash command group",
1317  .usage = "",
1319  },
1321 };
1322 
1323 const struct flash_driver xmc4xxx_flash = {
1324  .name = "xmc4xxx",
1325  .commands = xmc4xxx_command_handlers,
1326  .flash_bank_command = xmc4xxx_flash_bank_command,
1327  .erase = xmc4xxx_erase,
1328  .write = xmc4xxx_write,
1329  .read = default_flash_read,
1330  .probe = xmc4xxx_probe,
1331  .auto_probe = xmc4xxx_probe,
1332  .erase_check = default_flash_blank_check,
1333  .info = xmc4xxx_get_info_command,
1334  .protect_check = xmc4xxx_protect_check,
1335  .protect = xmc4xxx_protect,
1336  .free_driver_priv = default_flash_free_driver_priv,
1337 };
Support functions to access arbitrary bits in a byte array.
void command_print_sameline(struct command_invocation *cmd, const char *format,...)
Definition: command.c:378
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:389
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:146
#define CALL_COMMAND_HANDLER(name, extra ...)
Use this to macro to call a command helper (or a nested handler).
Definition: command.h:123
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:161
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:405
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:156
#define COMMAND_PARSE_NUMBER(type, in, out)
parses the string in into out as a type, or prints a command error and passes the error code to the c...
Definition: command.h:445
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:256
@ 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 address
Starting address. Sector aligned.
Definition: dw-spi-helper.h:0
uint8_t bank
Definition: esirisc.c:135
#define ERROR_FLASH_OPERATION_FAILED
Definition: flash/common.h:30
int default_flash_blank_check(struct flash_bank *bank)
Provides default erased-bank check handling.
int default_flash_read(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
Provides default read implementation for flash memory.
void default_flash_free_driver_priv(struct flash_bank *bank)
Deallocates bank->driver_priv.
void alive_sleep(uint64_t ms)
Definition: log.c:478
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 protection
Definition: qn908x.c:1
#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
When run_command is called, a new instance will be created on the stack, filled with the proper value...
Definition: command.h:76
const char * name
Definition: command.h:239
Provides details of a flash bank, available either on-chip or through a major interface.
Definition: nor/core.h:75
Provides the implementation-independent structure that defines all of the callbacks required by OpenO...
Definition: nor/driver.h:39
const char * name
Gives a human-readable name of this flash driver, This field is used to select and initialize the dri...
Definition: nor/driver.h:44
Describes the geometry and status of a single flash sector within a flash bank.
Definition: nor/core.h:28
Definition: psoc6.c:83
uint32_t address
Definition: xmc4xxx.c:229
uint32_t magic
Definition: xmc4xxx.c:230
bool write_prot_otp[MAX_XMC_SECTORS]
Definition: xmc4xxx.c:225
uint32_t pw2
Definition: xmc4xxx.c:219
uint32_t fcon_id
Definition: xmc4xxx.c:215
uint32_t pw1
Definition: xmc4xxx.c:218
void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
Definition: target.c:362
int target_write_u32(struct target *target, target_addr_t address, uint32_t value)
Definition: target.c:2635
int target_read_u32(struct target *target, target_addr_t address, uint32_t *value)
Definition: target.c:2561
uint32_t target_buffer_get_u32(struct target *target, const uint8_t *buffer)
Definition: target.c:326
#define ERROR_TARGET_NOT_HALTED
Definition: target.h:817
@ TARGET_HALTED
Definition: target.h:58
#define TARGET_ADDR_FMT
Definition: types.h:286
#define ARRAY_SIZE(x)
Compute the number of elements of a variable length array.
Definition: types.h:57
#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
static int xmc4xxx_temp_unprotect(struct flash_bank *bank, int user_level)
Definition: xmc4xxx.c:936
#define MAX_XMC_SECTORS
Definition: xmc4xxx.c:17
#define FLASH_CMD_WRITE_PAGE_2
Definition: xmc4xxx.c:200
static const unsigned int sector_capacity_12[12]
Definition: xmc4xxx.c:242
#define SCU_ID_CHIP
Definition: xmc4xxx.c:22
#define FSR_PBUSY_MASK
Definition: xmc4xxx.c:77
static int xmc4xxx_wait_status_busy(struct flash_bank *bank, int timeout)
Definition: xmc4xxx.c:439
#define FLASH_CMD_ERASE_5
Definition: xmc4xxx.c:189
#define PROCON_RPRO_MASK
Definition: xmc4xxx.c:169
#define FLASH_ID_XMC4100_4200
Definition: xmc4xxx.c:176
#define FLASH_CMD_LOAD_PAGE_2
Definition: xmc4xxx.c:197
static const unsigned int sector_capacity_9[9]
Definition: xmc4xxx.c:234
static const unsigned int sector_capacity_10[10]
Definition: xmc4xxx.c:238
#define FLASH_CMD_ENTER_PAGEMODE
Definition: xmc4xxx.c:194
static int xmc4xxx_load_bank_layout(struct flash_bank *bank)
Definition: xmc4xxx.c:266
static int xmc4xxx_write_page(struct flash_bank *bank, const uint8_t *pg_buf, uint32_t offset, bool user_config)
Definition: xmc4xxx.c:605
#define FSR_WPRODIS0
Definition: xmc4xxx.c:72
#define FLASH_CMD_ERASE_1
Definition: xmc4xxx.c:185
static int xmc4xxx_clear_flash_status(struct flash_bank *bank)
Definition: xmc4xxx.c:412
#define FLASH_CMD_WRITE_PAGE_1
Definition: xmc4xxx.c:199
#define FLASH_ID_XMC4500
Definition: xmc4xxx.c:174
#define FLASH_CMD_TEMP_UNPROT_1
Definition: xmc4xxx.c:204
#define FSR_WPROIN0_MASK
Definition: xmc4xxx.c:90
#define FLASH_REG_FLASH0_PROCON2
Definition: xmc4xxx.c:143
static const struct command_registration xmc4xxx_command_handlers[]
Definition: xmc4xxx.c:1312
#define UCB1_BASE
Definition: xmc4xxx.c:29
#define FSR_PROER_MASK
Definition: xmc4xxx.c:84
static int xmc4xxx_write_command_sequence(struct flash_bank *bank, struct xmc4xxx_command_seq *seq, int seq_len)
Definition: xmc4xxx.c:250
#define UCB0_BASE
Definition: xmc4xxx.c:28
static int xmc4xxx_probe(struct flash_bank *bank)
Definition: xmc4xxx.c:317
#define FLASH_CMD_CLEAR_STATUS
Definition: xmc4xxx.c:192
#define FLASH_REG_FLASH0_PROCON1
Definition: xmc4xxx.c:142
#define FLASH_CMD_ERASE_4
Definition: xmc4xxx.c:188
static int xmc4xxx_protect_check(struct flash_bank *bank)
Definition: xmc4xxx.c:1150
static int xmc4xxx_get_info_command(struct flash_bank *bank, struct command_invocation *cmd)
Definition: xmc4xxx.c:792
static const struct command_registration xmc4xxx_exec_command_handlers[]
Definition: xmc4xxx.c:1289
static int xmc4xxx_get_flash_status(struct flash_bank *bank, uint32_t *status)
Definition: xmc4xxx.c:427
#define SCU_REG_BASE
Definition: xmc4xxx.c:20
static int xmc4xxx_erase_sector(struct flash_bank *bank, uint32_t address, bool user_config)
Definition: xmc4xxx.c:470
static const unsigned int sector_capacity_16[16]
Definition: xmc4xxx.c:246
#define UCB2_BASE
Definition: xmc4xxx.c:30
#define FLASH_CMD_TEMP_UNPROT_3
Definition: xmc4xxx.c:206
#define FSR_ERASE_MASK
Definition: xmc4xxx.c:80
#define FLASH_CMD_TEMP_UNPROT_5
Definition: xmc4xxx.c:208
#define FSR_WPROIN2_MASK
Definition: xmc4xxx.c:92
COMMAND_HANDLER(xmc4xxx_handle_flash_password_command)
Definition: xmc4xxx.c:1236
static int xmc4xxx_protect(struct flash_bank *bank, int set, unsigned int first, unsigned int last)
Definition: xmc4xxx.c:1128
const struct flash_driver xmc4xxx_flash
Definition: xmc4xxx.c:1323
#define FLASH_CMD_ERASE_2
Definition: xmc4xxx.c:186
#define FLASH_ID_XMC4400
Definition: xmc4xxx.c:177
#define FLASH_ID_XMC4300_XMC4700_4800
Definition: xmc4xxx.c:175
static int xmc4xxx_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
Definition: xmc4xxx.c:528
#define FSR_RPROIN_MASK
Definition: xmc4xxx.c:88
#define FSR_PFPAGE_MASK
Definition: xmc4xxx.c:81
#define FLASH_REG_FLASH0_ID
Definition: xmc4xxx.c:51
static int xmc4xxx_flash_unprotect(struct flash_bank *bank, int32_t level)
Definition: xmc4xxx.c:985
#define FLASH_REG_FLASH0_FSR
Definition: xmc4xxx.c:54
#define FLASH_CMD_TEMP_UNPROT_2
Definition: xmc4xxx.c:205
#define FLASH_CMD_WRITE_PAGE_3
Definition: xmc4xxx.c:201
#define FSR_WPROIN1_MASK
Definition: xmc4xxx.c:91
FLASH_BANK_COMMAND_HANDLER(xmc4xxx_flash_bank_command)
Definition: xmc4xxx.c:1224
#define FSR_SQER_MASK
Definition: xmc4xxx.c:83
#define FLASH_CMD_TEMP_UNPROT_6
Definition: xmc4xxx.c:209
#define FLASH_CMD_LOAD_PAGE_1
Definition: xmc4xxx.c:196
#define FLASH_CMD_TEMP_UNPROT_4
Definition: xmc4xxx.c:207
static int xmc4xxx_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
Definition: xmc4xxx.c:709
#define FLASH_CMD_ERASE_3
Definition: xmc4xxx.c:187
static int xmc4xxx_get_sector_start_addr(struct flash_bank *bank, unsigned int sector, uint32_t *ret_addr)
Definition: xmc4xxx.c:399
static int xmc4xxx_flash_protect(struct flash_bank *bank, int level, bool read_protect, unsigned int first, unsigned int last)
Definition: xmc4xxx.c:1011
#define FLASH_REG_FLASH0_PROCON0
Definition: xmc4xxx.c:141
static int xmc4xxx_enter_page_mode(struct flash_bank *bank)
Definition: xmc4xxx.c:574
#define FSR_PROG_MASK
Definition: xmc4xxx.c:79
#define FLASH_OP_TIMEOUT
Definition: xmc4xxx.c:180
#define FLASH_PROTECT_CONFIRMATION_CODE
Definition: xmc4xxx.c:171