OpenOCD
kinetis.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2011 by Mathias Kuester *
5  * kesmtp@freenet.de *
6  * *
7  * Copyright (C) 2011 sleep(5) ltd *
8  * tomas@sleepfive.com *
9  * *
10  * Copyright (C) 2012 by Christopher D. Kilgour *
11  * techie at whiterocker.com *
12  * *
13  * Copyright (C) 2013 Nemui Trinomius *
14  * nemuisan_kawausogasuki@live.jp *
15  * *
16  * Copyright (C) 2015 Tomas Vanek *
17  * vanekt@fbl.cz *
18  ***************************************************************************/
19 
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 
24 #include "jtag/interface.h"
25 #include "imp.h"
26 #include <helper/binarybuffer.h>
27 #include <helper/time_support.h>
28 #include <target/target_type.h>
29 #include <target/algorithm.h>
30 #include <target/arm_adi_v5.h>
31 #include <target/armv7m.h>
32 #include <target/cortex_m.h>
33 
34 /*
35  * Implementation Notes
36  *
37  * The persistent memories in the Kinetis chip families K10 through
38  * K70 are all manipulated with the Flash Memory Module. Some
39  * variants call this module the FTFE, others call it the FTFL. To
40  * indicate that both are considered here, we use FTFX.
41  *
42  * Within the module, according to the chip variant, the persistent
43  * memory is divided into what Freescale terms Program Flash, FlexNVM,
44  * and FlexRAM. All chip variants have Program Flash. Some chip
45  * variants also have FlexNVM and FlexRAM, which always appear
46  * together.
47  *
48  * A given Kinetis chip may have 1, 2 or 4 blocks of flash. Here we map
49  * each block to a separate bank. Each block size varies by chip and
50  * may be determined by the read-only SIM_FCFG1 register. The sector
51  * size within each bank/block varies by chip, and may be 1, 2 or 4k.
52  * The sector size may be different for flash and FlexNVM.
53  *
54  * The first half of the flash (1 or 2 blocks) is always Program Flash
55  * and always starts at address 0x00000000. The "PFLSH" flag, bit 23
56  * of the read-only SIM_FCFG2 register, determines whether the second
57  * half of the flash is also Program Flash or FlexNVM+FlexRAM. When
58  * PFLSH is set, the second from the first half. When PFLSH is clear,
59  * the second half of flash is FlexNVM and always starts at address
60  * 0x10000000. FlexRAM, which is also present when PFLSH is clear,
61  * always starts at address 0x14000000.
62  *
63  * The Flash Memory Module provides a register set where flash
64  * commands are loaded to perform flash operations like erase and
65  * program. Different commands are available depending on whether
66  * Program Flash or FlexNVM/FlexRAM is being manipulated. Although
67  * the commands used are quite consistent between flash blocks, the
68  * parameters they accept differ according to the flash sector size.
69  *
70  */
71 
72 /* Addresses */
73 #define FCF_ADDRESS 0x00000400
74 #define FCF_FPROT 0x8
75 #define FCF_FSEC 0xc
76 #define FCF_FOPT 0xd
77 #define FCF_FDPROT 0xf
78 #define FCF_SIZE 0x10
79 
80 #define FLEXRAM 0x14000000
81 
82 #define MSCM_OCMDR0 0x40001400
83 #define MSCM_OCMDR1 0x40001404
84 #define FMC_PFB01CR 0x4001f004
85 #define FTFX_FSTAT 0x40020000
86 #define FTFX_FCNFG 0x40020001
87 #define FTFX_FCCOB3 0x40020004
88 #define FTFX_FPROT3 0x40020010
89 #define FTFX_FDPROT 0x40020017
90 #define SIM_BASE 0x40047000
91 #define SIM_BASE_KL28 0x40074000
92 #define SIM_COPC 0x40048100
93  /* SIM_COPC does not exist on devices with changed SIM_BASE */
94 #define WDOG_BASE 0x40052000
95 #define WDOG32_KE1X 0x40052000
96 #define WDOG32_KL28 0x40076000
97 #define SMC_PMCTRL 0x4007E001
98 #define SMC_PMSTAT 0x4007E003
99 #define SMC32_PMCTRL 0x4007E00C
100 #define SMC32_PMSTAT 0x4007E014
101 #define PMC_REGSC 0x4007D002
102 #define MC_PMCTRL 0x4007E003
103 #define MCM_PLACR 0xF000300C
104 
105 /* Offsets */
106 #define SIM_SOPT1_OFFSET 0x0000
107 #define SIM_SDID_OFFSET 0x1024
108 #define SIM_FCFG1_OFFSET 0x104c
109 #define SIM_FCFG2_OFFSET 0x1050
110 
111 #define WDOG_STCTRLH_OFFSET 0
112 #define WDOG32_CS_OFFSET 0
113 
114 /* Values */
115 #define PM_STAT_RUN 0x01
116 #define PM_STAT_VLPR 0x04
117 #define PM_CTRL_RUNM_RUN 0x00
118 
119 /* Commands */
120 #define FTFX_CMD_BLOCKSTAT 0x00
121 #define FTFX_CMD_SECTSTAT 0x01
122 #define FTFX_CMD_LWORDPROG 0x06
123 #define FTFX_CMD_SECTERASE 0x09
124 #define FTFX_CMD_SECTWRITE 0x0b
125 #define FTFX_CMD_MASSERASE 0x44
126 #define FTFX_CMD_PGMPART 0x80
127 #define FTFX_CMD_SETFLEXRAM 0x81
128 
129 /* The older Kinetis K series uses the following SDID layout :
130  * Bit 31-16 : 0
131  * Bit 15-12 : REVID
132  * Bit 11-7 : DIEID
133  * Bit 6-4 : FAMID
134  * Bit 3-0 : PINID
135  *
136  * The newer Kinetis series uses the following SDID layout :
137  * Bit 31-28 : FAMID
138  * Bit 27-24 : SUBFAMID
139  * Bit 23-20 : SERIESID
140  * Bit 19-16 : SRAMSIZE
141  * Bit 15-12 : REVID
142  * Bit 6-4 : Reserved (0)
143  * Bit 3-0 : PINID
144  *
145  * We assume that if bits 31-16 are 0 then it's an older
146  * K-series MCU.
147  */
148 
149 #define KINETIS_SOPT1_RAMSIZE_MASK 0x0000F000
150 #define KINETIS_SOPT1_RAMSIZE_K24FN1M 0x0000B000
151 
152 #define KINETIS_SDID_K_SERIES_MASK 0x0000FFFF
153 
154 #define KINETIS_SDID_DIEID_MASK 0x00000F80
155 
156 #define KINETIS_SDID_DIEID_K22FN128 0x00000680 /* smaller pflash with FTFA */
157 #define KINETIS_SDID_DIEID_K22FN256 0x00000A80
158 #define KINETIS_SDID_DIEID_K22FN512 0x00000E80
159 #define KINETIS_SDID_DIEID_K24FN256 0x00000700
160 
161 #define KINETIS_SDID_DIEID_K24FN1M 0x00000300 /* Detect Errata 7534 */
162 
163 /* We can't rely solely on the FAMID field to determine the MCU
164  * type since some FAMID values identify multiple MCUs with
165  * different flash sector sizes (K20 and K22 for instance).
166  * Therefore we combine it with the DIEID bits which may possibly
167  * break if Freescale bumps the DIEID for a particular MCU. */
168 #define KINETIS_K_SDID_TYPE_MASK 0x00000FF0
169 #define KINETIS_K_SDID_K10_M50 0x00000000
170 #define KINETIS_K_SDID_K10_M72 0x00000080
171 #define KINETIS_K_SDID_K10_M100 0x00000100
172 #define KINETIS_K_SDID_K10_M120 0x00000180
173 #define KINETIS_K_SDID_K11 0x00000220
174 #define KINETIS_K_SDID_K12 0x00000200
175 #define KINETIS_K_SDID_K20_M50 0x00000010
176 #define KINETIS_K_SDID_K20_M72 0x00000090
177 #define KINETIS_K_SDID_K20_M100 0x00000110
178 #define KINETIS_K_SDID_K20_M120 0x00000190
179 #define KINETIS_K_SDID_K21_M50 0x00000230
180 #define KINETIS_K_SDID_K21_M120 0x00000330
181 #define KINETIS_K_SDID_K22_M50 0x00000210
182 #define KINETIS_K_SDID_K22_M120 0x00000310
183 #define KINETIS_K_SDID_K30_M72 0x000000A0
184 #define KINETIS_K_SDID_K30_M100 0x00000120
185 #define KINETIS_K_SDID_K40_M72 0x000000B0
186 #define KINETIS_K_SDID_K40_M100 0x00000130
187 #define KINETIS_K_SDID_K50_M72 0x000000E0
188 #define KINETIS_K_SDID_K51_M72 0x000000F0
189 #define KINETIS_K_SDID_K53 0x00000170
190 #define KINETIS_K_SDID_K60_M100 0x00000140
191 #define KINETIS_K_SDID_K60_M150 0x000001C0
192 #define KINETIS_K_SDID_K70_M150 0x000001D0
193 
194 #define KINETIS_K_REVID_MASK 0x0000F000
195 #define KINETIS_K_REVID_SHIFT 12
196 
197 #define KINETIS_SDID_SERIESID_MASK 0x00F00000
198 #define KINETIS_SDID_SERIESID_K 0x00000000
199 #define KINETIS_SDID_SERIESID_KL 0x00100000
200 #define KINETIS_SDID_SERIESID_KE 0x00200000
201 #define KINETIS_SDID_SERIESID_KW 0x00500000
202 #define KINETIS_SDID_SERIESID_KV 0x00600000
203 
204 #define KINETIS_SDID_SUBFAMID_SHIFT 24
205 #define KINETIS_SDID_SUBFAMID_MASK 0x0F000000
206 #define KINETIS_SDID_SUBFAMID_KX0 0x00000000
207 #define KINETIS_SDID_SUBFAMID_KX1 0x01000000
208 #define KINETIS_SDID_SUBFAMID_KX2 0x02000000
209 #define KINETIS_SDID_SUBFAMID_KX3 0x03000000
210 #define KINETIS_SDID_SUBFAMID_KX4 0x04000000
211 #define KINETIS_SDID_SUBFAMID_KX5 0x05000000
212 #define KINETIS_SDID_SUBFAMID_KX6 0x06000000
213 #define KINETIS_SDID_SUBFAMID_KX7 0x07000000
214 #define KINETIS_SDID_SUBFAMID_KX8 0x08000000
215 
216 #define KINETIS_SDID_FAMILYID_SHIFT 28
217 #define KINETIS_SDID_FAMILYID_MASK 0xF0000000
218 #define KINETIS_SDID_FAMILYID_K0X 0x00000000
219 #define KINETIS_SDID_FAMILYID_K1X 0x10000000
220 #define KINETIS_SDID_FAMILYID_K2X 0x20000000
221 #define KINETIS_SDID_FAMILYID_K3X 0x30000000
222 #define KINETIS_SDID_FAMILYID_K4X 0x40000000
223 #define KINETIS_SDID_FAMILYID_K5X 0x50000000
224 #define KINETIS_SDID_FAMILYID_K6X 0x60000000
225 #define KINETIS_SDID_FAMILYID_K7X 0x70000000
226 #define KINETIS_SDID_FAMILYID_K8X 0x80000000
227 #define KINETIS_SDID_FAMILYID_KL8X 0x90000000
228 
229 /* The field originally named DIEID has new name/meaning on KE1x */
230 #define KINETIS_SDID_PROJECTID_MASK KINETIS_SDID_DIEID_MASK
231 #define KINETIS_SDID_PROJECTID_KE1XF 0x00000080
232 #define KINETIS_SDID_PROJECTID_KE1XZ 0x00000100
233 
234 /* The S32K series uses a different, incompatible SDID layout :
235  * Bit 31-28 : GENERATION
236  * Bit 27-24 : SUBSERIES
237  * Bit 23-20 : DERIVATE
238  * Bit 19-16 : RAMSIZE
239  * Bit 15-12 : REVID
240  * Bit 11-8 : PACKAGE
241  * Bit 7-0 : FEATURES
242  */
243 
244 #define KINETIS_SDID_S32K_SERIES_MASK 0xFF000000 /* GENERATION + SUBSERIES */
245 #define KINETIS_SDID_S32K_SERIES_K11X 0x11000000
246 #define KINETIS_SDID_S32K_SERIES_K14X 0x14000000
247 
248 #define KINETIS_SDID_S32K_DERIVATE_MASK 0x00F00000
249 #define KINETIS_SDID_S32K_DERIVATE_KXX2 0x00200000
250 #define KINETIS_SDID_S32K_DERIVATE_KXX3 0x00300000
251 #define KINETIS_SDID_S32K_DERIVATE_KXX4 0x00400000
252 #define KINETIS_SDID_S32K_DERIVATE_KXX5 0x00500000
253 #define KINETIS_SDID_S32K_DERIVATE_KXX6 0x00600000
254 #define KINETIS_SDID_S32K_DERIVATE_KXX8 0x00800000
255 
258  bool probed;
259  unsigned int bank_number; /* bank number in particular chip */
260  struct flash_bank *bank;
261 
262  uint32_t sector_size;
263  uint32_t protection_size;
264  uint32_t prog_base; /* base address for FTFx operations */
265  /* usually same as bank->base for pflash, differs for FlexNVM */
266  uint32_t protection_block; /* number of first protection block in this bank */
267 
268  enum {
269  FC_AUTO = 0,
274 };
275 
276 #define KINETIS_MAX_BANKS 4u
277 
281 };
282 
283 struct kinetis_chip {
284  struct target *target;
285  bool probed;
286 
287  uint32_t sim_sdid;
288  uint32_t sim_fcfg1;
289  uint32_t sim_fcfg2;
292 
295  unsigned int max_flash_prog_size;
296 
297  uint32_t pflash_base;
298  uint32_t pflash_size;
299  uint32_t nvm_base;
300  uint32_t nvm_size; /* whole FlexNVM */
301  uint32_t dflash_size; /* accessible rest of FlexNVM if EEPROM backup uses part of FlexNVM */
302 
303  uint32_t progr_accel_ram;
304  uint32_t sim_base;
305 
307 
308  enum {
311  FS_PROGRAM_PHRASE = 4, /* Unsupported */
312 
315  FS_ECC = 0x100,
317 
318  enum {
320  KINETIS_CACHE_K, /* invalidate using FMC->PFB0CR/PFB01CR */
321  KINETIS_CACHE_L, /* invalidate using MCM->PLACR */
322  KINETIS_CACHE_MSCM, /* devices like KE1xF, invalidate MSCM->OCMDR0 */
323  KINETIS_CACHE_MSCM2, /* devices like S32K, invalidate MSCM->OCMDR0 and MSCM->OCMDR1 */
325 
326  enum {
333 
334  enum {
339 
340  char name[40];
341 
342  unsigned int num_banks;
344 };
345 
346 struct kinetis_type {
347  uint32_t sdid;
348  char *name;
349 };
350 
351 static const struct kinetis_type kinetis_types_old[] = {
352  { KINETIS_K_SDID_K10_M50, "MK10D%s5" },
353  { KINETIS_K_SDID_K10_M72, "MK10D%s7" },
354  { KINETIS_K_SDID_K10_M100, "MK10D%s10" },
355  { KINETIS_K_SDID_K10_M120, "MK10F%s12" },
356  { KINETIS_K_SDID_K11, "MK11D%s5" },
357  { KINETIS_K_SDID_K12, "MK12D%s5" },
358 
359  { KINETIS_K_SDID_K20_M50, "MK20D%s5" },
360  { KINETIS_K_SDID_K20_M72, "MK20D%s7" },
361  { KINETIS_K_SDID_K20_M100, "MK20D%s10" },
362  { KINETIS_K_SDID_K20_M120, "MK20F%s12" },
363  { KINETIS_K_SDID_K21_M50, "MK21D%s5" },
364  { KINETIS_K_SDID_K21_M120, "MK21F%s12" },
365  { KINETIS_K_SDID_K22_M50, "MK22D%s5" },
366  { KINETIS_K_SDID_K22_M120, "MK22F%s12" },
367 
368  { KINETIS_K_SDID_K30_M72, "MK30D%s7" },
369  { KINETIS_K_SDID_K30_M100, "MK30D%s10" },
370 
371  { KINETIS_K_SDID_K40_M72, "MK40D%s7" },
372  { KINETIS_K_SDID_K40_M100, "MK40D%s10" },
373 
374  { KINETIS_K_SDID_K50_M72, "MK50D%s7" },
375  { KINETIS_K_SDID_K51_M72, "MK51D%s7" },
376  { KINETIS_K_SDID_K53, "MK53D%s10" },
377 
378  { KINETIS_K_SDID_K60_M100, "MK60D%s10" },
379  { KINETIS_K_SDID_K60_M150, "MK60F%s15" },
380 
381  { KINETIS_K_SDID_K70_M150, "MK70F%s15" },
382 };
383 
384 
385 #define MDM_AP 1
386 
387 #define MDM_REG_STAT 0x00
388 #define MDM_REG_CTRL 0x04
389 #define MDM_REG_ID 0xfc
390 
391 #define MDM_STAT_FMEACK (1<<0)
392 #define MDM_STAT_FREADY (1<<1)
393 #define MDM_STAT_SYSSEC (1<<2)
394 #define MDM_STAT_SYSRES (1<<3)
395 #define MDM_STAT_FMEEN (1<<5)
396 #define MDM_STAT_BACKDOOREN (1<<6)
397 #define MDM_STAT_LPEN (1<<7)
398 #define MDM_STAT_VLPEN (1<<8)
399 #define MDM_STAT_LLSMODEXIT (1<<9)
400 #define MDM_STAT_VLLSXMODEXIT (1<<10)
401 #define MDM_STAT_CORE_HALTED (1<<16)
402 #define MDM_STAT_CORE_SLEEPDEEP (1<<17)
403 #define MDM_STAT_CORESLEEPING (1<<18)
404 
405 #define MDM_CTRL_FMEIP (1<<0)
406 #define MDM_CTRL_DBG_DIS (1<<1)
407 #define MDM_CTRL_DBG_REQ (1<<2)
408 #define MDM_CTRL_SYS_RES_REQ (1<<3)
409 #define MDM_CTRL_CORE_HOLD_RES (1<<4)
410 #define MDM_CTRL_VLLSX_DBG_REQ (1<<5)
411 #define MDM_CTRL_VLLSX_DBG_ACK (1<<6)
412 #define MDM_CTRL_VLLSX_STAT_ACK (1<<7)
413 
414 #define MDM_ACCESS_TIMEOUT 500 /* msec */
415 
416 
417 static bool allow_fcf_writes;
418 static uint8_t fcf_fopt = 0xff;
419 static bool create_banks;
420 
421 
422 const struct flash_driver kinetis_flash;
423 static int kinetis_write_inner(struct flash_bank *bank, const uint8_t *buffer,
424  uint32_t offset, uint32_t count);
425 static int kinetis_probe_chip(struct kinetis_chip *k_chip);
426 static int kinetis_probe_chip_s32k(struct kinetis_chip *k_chip);
427 static int kinetis_auto_probe(struct flash_bank *bank);
428 
429 
430 static int kinetis_mdm_write_register(struct adiv5_dap *dap, unsigned int reg, uint32_t value)
431 {
432  LOG_DEBUG("MDM_REG[0x%02x] <- %08" PRIX32, reg, value);
433 
434  struct adiv5_ap *ap = dap_get_ap(dap, MDM_AP);
435  if (!ap) {
436  LOG_DEBUG("MDM: failed to get AP");
437  return ERROR_FAIL;
438  }
439 
440  int retval = dap_queue_ap_write(ap, reg, value);
441  if (retval != ERROR_OK) {
442  LOG_DEBUG("MDM: failed to queue a write request");
443  dap_put_ap(ap);
444  return retval;
445  }
446 
447  retval = dap_run(dap);
448  dap_put_ap(ap);
449  if (retval != ERROR_OK) {
450  LOG_DEBUG("MDM: dap_run failed");
451  return retval;
452  }
453 
454 
455  return ERROR_OK;
456 }
457 
458 static int kinetis_mdm_read_register(struct adiv5_dap *dap, unsigned int reg, uint32_t *result)
459 {
460  struct adiv5_ap *ap = dap_get_ap(dap, MDM_AP);
461  if (!ap) {
462  LOG_DEBUG("MDM: failed to get AP");
463  return ERROR_FAIL;
464  }
465 
466  int retval = dap_queue_ap_read(ap, reg, result);
467  if (retval != ERROR_OK) {
468  LOG_DEBUG("MDM: failed to queue a read request");
469  dap_put_ap(ap);
470  return retval;
471  }
472 
473  retval = dap_run(dap);
474  dap_put_ap(ap);
475  if (retval != ERROR_OK) {
476  LOG_DEBUG("MDM: dap_run failed");
477  return retval;
478  }
479 
480  LOG_DEBUG("MDM_REG[0x%02x]: %08" PRIX32, reg, *result);
481  return ERROR_OK;
482 }
483 
484 static int kinetis_mdm_poll_register(struct adiv5_dap *dap, unsigned int reg,
485  uint32_t mask, uint32_t value, uint32_t timeout_ms)
486 {
487  uint32_t val;
488  int retval;
489  int64_t ms_timeout = timeval_ms() + timeout_ms;
490 
491  do {
492  retval = kinetis_mdm_read_register(dap, reg, &val);
493  if (retval != ERROR_OK || (val & mask) == value)
494  return retval;
495 
496  alive_sleep(1);
497  } while (timeval_ms() < ms_timeout);
498 
499  LOG_DEBUG("MDM: polling timed out");
500  return ERROR_FAIL;
501 }
502 
503 /*
504  * This command can be used to break a watchdog reset loop when
505  * connecting to an unsecured target. Unlike other commands, halt will
506  * automatically retry as it does not know how far into the boot process
507  * it is when the command is called.
508  */
509 COMMAND_HANDLER(kinetis_mdm_halt)
510 {
512  struct cortex_m_common *cortex_m = target_to_cm(target);
513  struct adiv5_dap *dap = cortex_m->armv7m.arm.dap;
514  int retval;
515  int tries = 0;
516  uint32_t stat;
517  int64_t ms_timeout = timeval_ms() + MDM_ACCESS_TIMEOUT;
518 
519  if (!dap) {
520  LOG_ERROR("Cannot perform halt with a high-level adapter");
521  return ERROR_FAIL;
522  }
523 
524  while (true) {
525  tries++;
526 
528 
529  alive_sleep(1);
530 
531  retval = kinetis_mdm_read_register(dap, MDM_REG_STAT, &stat);
532  if (retval != ERROR_OK) {
533  LOG_DEBUG("MDM: failed to read MDM_REG_STAT");
534  continue;
535  }
536 
537  /* Repeat setting MDM_CTRL_CORE_HOLD_RES until system is out of
538  * reset with flash ready and without security
539  */
542  break;
543 
544  if (timeval_ms() >= ms_timeout) {
545  LOG_ERROR("MDM: halt timed out");
546  return ERROR_FAIL;
547  }
548  }
549 
550  LOG_DEBUG("MDM: halt succeeded after %d attempts.", tries);
551 
553  /* enable polling in case kinetis_check_flash_security_status disabled it */
554  jtag_poll_set_enabled(true);
555 
556  alive_sleep(100);
557 
558  target->reset_halt = true;
560 
561  retval = kinetis_mdm_write_register(dap, MDM_REG_CTRL, 0);
562  if (retval != ERROR_OK) {
563  LOG_ERROR("MDM: failed to clear MDM_REG_CTRL");
564  return retval;
565  }
566 
568 
569  return ERROR_OK;
570 }
571 
572 COMMAND_HANDLER(kinetis_mdm_reset)
573 {
575  struct cortex_m_common *cortex_m = target_to_cm(target);
576  struct adiv5_dap *dap = cortex_m->armv7m.arm.dap;
577  int retval;
578 
579  if (!dap) {
580  LOG_ERROR("Cannot perform reset with a high-level adapter");
581  return ERROR_FAIL;
582  }
583 
585  if (retval != ERROR_OK) {
586  LOG_ERROR("MDM: failed to write MDM_REG_CTRL");
587  return retval;
588  }
589 
591  if (retval != ERROR_OK) {
592  LOG_ERROR("MDM: failed to assert reset");
593  return retval;
594  }
595 
596  retval = kinetis_mdm_write_register(dap, MDM_REG_CTRL, 0);
597  if (retval != ERROR_OK) {
598  LOG_ERROR("MDM: failed to clear MDM_REG_CTRL");
599  return retval;
600  }
601 
602  return ERROR_OK;
603 }
604 
605 /*
606  * This function implements the procedure to mass erase the flash via
607  * SWD/JTAG on Kinetis K and L series of devices as it is described in
608  * AN4835 "Production Flash Programming Best Practices for Kinetis K-
609  * and L-series MCUs" Section 4.2.1. To prevent a watchdog reset loop,
610  * the core remains halted after this function completes as suggested
611  * by the application note.
612  */
613 COMMAND_HANDLER(kinetis_mdm_mass_erase)
614 {
616  struct cortex_m_common *cortex_m = target_to_cm(target);
617  struct adiv5_dap *dap = cortex_m->armv7m.arm.dap;
618 
619  if (!dap) {
620  LOG_ERROR("Cannot perform mass erase with a high-level adapter");
621  return ERROR_FAIL;
622  }
623 
624  int retval;
625 
626  /*
627  * ... Power on the processor, or if power has already been
628  * applied, assert the RESET pin to reset the processor. For
629  * devices that do not have a RESET pin, write the System
630  * Reset Request bit in the MDM-AP control register after
631  * establishing communication...
632  */
633 
634  /* assert SRST if configured */
635  bool has_srst = jtag_get_reset_config() & RESET_HAS_SRST;
636  if (has_srst)
638 
640  if (retval != ERROR_OK && !has_srst) {
641  LOG_ERROR("MDM: failed to assert reset");
642  goto deassert_reset_and_exit;
643  }
644 
645  /*
646  * ... Read the MDM-AP status register repeatedly and wait for
647  * stable conditions suitable for mass erase:
648  * - mass erase is enabled
649  * - flash is ready
650  * - reset is finished
651  *
652  * Mass erase is started as soon as all conditions are met in 32
653  * subsequent status reads.
654  *
655  * In case of not stable conditions (RESET/WDOG loop in secured device)
656  * the user is asked for manual pressing of RESET button
657  * as a last resort.
658  */
659  int cnt_mass_erase_disabled = 0;
660  int cnt_ready = 0;
661  int64_t ms_start = timeval_ms();
662  bool man_reset_requested = false;
663 
664  do {
665  uint32_t stat = 0;
666  int64_t ms_elapsed = timeval_ms() - ms_start;
667 
668  if (!man_reset_requested && ms_elapsed > 100) {
669  LOG_INFO("MDM: Press RESET button now if possible.");
670  man_reset_requested = true;
671  }
672 
673  if (ms_elapsed > 3000) {
674  LOG_ERROR("MDM: waiting for mass erase conditions timed out.");
675  LOG_INFO("Mass erase of a secured MCU is not possible without hardware reset.");
676  LOG_INFO("Connect SRST, use 'reset_config srst_only' and retry.");
677  goto deassert_reset_and_exit;
678  }
679  retval = kinetis_mdm_read_register(dap, MDM_REG_STAT, &stat);
680  if (retval != ERROR_OK) {
681  cnt_ready = 0;
682  continue;
683  }
684 
685  if (!(stat & MDM_STAT_FMEEN)) {
686  cnt_ready = 0;
687  cnt_mass_erase_disabled++;
688  if (cnt_mass_erase_disabled > 10) {
689  LOG_ERROR("MDM: mass erase is disabled");
690  goto deassert_reset_and_exit;
691  }
692  continue;
693  }
694 
695  if ((stat & (MDM_STAT_FREADY | MDM_STAT_SYSRES)) == MDM_STAT_FREADY)
696  cnt_ready++;
697  else
698  cnt_ready = 0;
699 
700  } while (cnt_ready < 32);
701 
702  /*
703  * ... Write the MDM-AP control register to set the Flash Mass
704  * Erase in Progress bit. This will start the mass erase
705  * process...
706  */
708  if (retval != ERROR_OK) {
709  LOG_ERROR("MDM: failed to start mass erase");
710  goto deassert_reset_and_exit;
711  }
712 
713  /*
714  * ... Read the MDM-AP control register until the Flash Mass
715  * Erase in Progress bit clears...
716  * Data sheed defines erase time <3.6 sec/512kB flash block.
717  * The biggest device has 4 pflash blocks => timeout 16 sec.
718  */
719  retval = kinetis_mdm_poll_register(dap, MDM_REG_CTRL, MDM_CTRL_FMEIP, 0, 16000);
720  if (retval != ERROR_OK) {
721  LOG_ERROR("MDM: mass erase timeout");
722  goto deassert_reset_and_exit;
723  }
724 
726  /* enable polling in case kinetis_check_flash_security_status disabled it */
727  jtag_poll_set_enabled(true);
728 
729  alive_sleep(100);
730 
731  target->reset_halt = true;
733 
734  /*
735  * ... Negate the RESET signal or clear the System Reset Request
736  * bit in the MDM-AP control register.
737  */
738  retval = kinetis_mdm_write_register(dap, MDM_REG_CTRL, 0);
739  if (retval != ERROR_OK)
740  LOG_ERROR("MDM: failed to clear MDM_REG_CTRL");
741 
743 
744  return retval;
745 
746 deassert_reset_and_exit:
748  if (has_srst)
750  return retval;
751 }
752 
753 static const uint32_t kinetis_known_mdm_ids[] = {
754  0x001C0000, /* Kinetis-K Series */
755  0x001C0020, /* Kinetis-L/M/V/E Series */
756  0x001C0030, /* Kinetis with a Cortex-M7, in time of writing KV58 */
757 };
758 
759 /*
760  * This function implements the procedure to connect to
761  * SWD/JTAG on Kinetis K and L series of devices as it is described in
762  * AN4835 "Production Flash Programming Best Practices for Kinetis K-
763  * and L-series MCUs" Section 4.1.1
764  */
765 COMMAND_HANDLER(kinetis_check_flash_security_status)
766 {
768  struct cortex_m_common *cortex_m = target_to_cm(target);
769  struct adiv5_dap *dap = cortex_m->armv7m.arm.dap;
770 
771  if (!dap) {
772  LOG_WARNING("Cannot check flash security status with a high-level adapter");
773  return ERROR_OK;
774  }
775 
776  if (!dap->ops)
777  return ERROR_OK; /* too early to check, in JTAG mode ops may not be initialised */
778 
779  uint32_t val;
780  int retval;
781 
782  /*
783  * ... The MDM-AP ID register can be read to verify that the
784  * connection is working correctly...
785  */
786  retval = kinetis_mdm_read_register(dap, MDM_REG_ID, &val);
787  if (retval != ERROR_OK) {
788  LOG_ERROR("MDM: failed to read ID register");
789  return ERROR_OK;
790  }
791 
792  if (val == 0)
793  return ERROR_OK; /* dap not yet initialised */
794 
795  bool found = false;
796  for (size_t i = 0; i < ARRAY_SIZE(kinetis_known_mdm_ids); i++) {
797  if (val == kinetis_known_mdm_ids[i]) {
798  found = true;
799  break;
800  }
801  }
802 
803  if (!found)
804  LOG_WARNING("MDM: unknown ID %08" PRIX32, val);
805 
806  /*
807  * ... Read the System Security bit to determine if security is enabled.
808  * If System Security = 0, then proceed. If System Security = 1, then
809  * communication with the internals of the processor, including the
810  * flash, will not be possible without issuing a mass erase command or
811  * unsecuring the part through other means (backdoor key unlock)...
812  */
813  retval = kinetis_mdm_read_register(dap, MDM_REG_STAT, &val);
814  if (retval != ERROR_OK) {
815  LOG_ERROR("MDM: failed to read MDM_REG_STAT");
816  return ERROR_OK;
817  }
818 
819  /*
820  * System Security bit is also active for short time during reset.
821  * If a MCU has blank flash and runs in RESET/WDOG loop,
822  * System Security bit is active most of time!
823  * We should observe Flash Ready bit and read status several times
824  * to avoid false detection of secured MCU
825  */
826  int secured_score = 0, flash_not_ready_score = 0;
827 
828  if ((val & (MDM_STAT_SYSSEC | MDM_STAT_FREADY)) != MDM_STAT_FREADY) {
829  uint32_t stats[32];
830  struct adiv5_ap *ap = dap_get_ap(dap, MDM_AP);
831  if (!ap) {
832  LOG_ERROR("MDM: failed to get AP");
833  return ERROR_OK;
834  }
835 
836  for (unsigned int i = 0; i < 32; i++) {
837  stats[i] = MDM_STAT_FREADY;
838  dap_queue_ap_read(ap, MDM_REG_STAT, &stats[i]);
839  }
840  retval = dap_run(dap);
841  dap_put_ap(ap);
842  if (retval != ERROR_OK) {
843  LOG_DEBUG("MDM: dap_run failed when validating secured state");
844  return ERROR_OK;
845  }
846  for (unsigned int i = 0; i < 32; i++) {
847  if (stats[i] & MDM_STAT_SYSSEC)
848  secured_score++;
849  if (!(stats[i] & MDM_STAT_FREADY))
850  flash_not_ready_score++;
851  }
852  }
853 
854  if (flash_not_ready_score <= 8 && secured_score > 24) {
855  jtag_poll_set_enabled(false);
856 
857  LOG_WARNING("*********** ATTENTION! ATTENTION! ATTENTION! ATTENTION! **********");
858  LOG_WARNING("**** ****");
859  LOG_WARNING("**** Your Kinetis MCU is in secured state, which means that, ****");
860  LOG_WARNING("**** with exception for very basic communication, JTAG/SWD ****");
861  LOG_WARNING("**** interface will NOT work. In order to restore its ****");
862  LOG_WARNING("**** functionality please issue 'kinetis mdm mass_erase' ****");
863  LOG_WARNING("**** command, power cycle the MCU and restart OpenOCD. ****");
864  LOG_WARNING("**** ****");
865  LOG_WARNING("*********** ATTENTION! ATTENTION! ATTENTION! ATTENTION! **********");
866 
867  } else if (flash_not_ready_score > 24) {
868  jtag_poll_set_enabled(false);
869  LOG_WARNING("**** Your Kinetis MCU is probably locked-up in RESET/WDOG loop. ****");
870  LOG_WARNING("**** Common reason is a blank flash (at least a reset vector). ****");
871  LOG_WARNING("**** Issue 'kinetis mdm halt' command or if SRST is connected ****");
872  LOG_WARNING("**** and configured, use 'reset halt' ****");
873  LOG_WARNING("**** If MCU cannot be halted, it is likely secured and running ****");
874  LOG_WARNING("**** in RESET/WDOG loop. Issue 'kinetis mdm mass_erase' ****");
875 
876  } else {
877  LOG_INFO("MDM: Chip is unsecured. Continuing.");
878  jtag_poll_set_enabled(true);
879  }
880 
881  return ERROR_OK;
882 }
883 
884 
886 {
887  struct flash_bank *bank_iter;
888  struct kinetis_flash_bank *k_bank;
889 
890  /* iterate over all kinetis banks */
891  for (bank_iter = flash_bank_list(); bank_iter; bank_iter = bank_iter->next) {
892  if (bank_iter->driver != &kinetis_flash
893  || bank_iter->target != target)
894  continue;
895 
896  k_bank = bank_iter->driver_priv;
897  if (!k_bank)
898  continue;
899 
900  if (k_bank->k_chip)
901  return k_bank->k_chip;
902  }
903  return NULL;
904 }
905 
906 static uint8_t kinetis_fcfg1_depart(enum kinetis_chip_type chip_type, uint32_t sim_fcfg1)
907 {
908  unsigned int shift = chip_type == KINETIS_CT_S32K ? 12 : 8;
909 
910  return (sim_fcfg1 >> shift) & 0x0f;
911 }
912 
913 static int kinetis_chip_options(struct kinetis_chip *k_chip, int argc, const char *argv[])
914 {
915  for (int i = 0; i < argc; i++) {
916  if (strcmp(argv[i], "-sim-base") == 0) {
917  if (i + 1 < argc)
918  k_chip->sim_base = strtoul(argv[++i], NULL, 0);
919  } else if (strcmp(argv[i], "-s32k") == 0) {
921  } else
922  LOG_ERROR("Unsupported flash bank option %s", argv[i]);
923  }
924  return ERROR_OK;
925 }
926 
927 FLASH_BANK_COMMAND_HANDLER(kinetis_flash_bank_command)
928 {
929  struct target *target = bank->target;
930  struct kinetis_chip *k_chip;
931  struct kinetis_flash_bank *k_bank;
932  int retval;
933 
934  if (CMD_ARGC < 6)
936 
937  LOG_INFO("add flash_bank kinetis %s", bank->name);
938 
940 
941  if (!k_chip) {
942  k_chip = calloc(1, sizeof(struct kinetis_chip));
943  if (!k_chip) {
944  LOG_ERROR("No memory");
945  return ERROR_FAIL;
946  }
947 
948  k_chip->target = target;
949 
950  /* only the first defined bank can define chip options */
951  retval = kinetis_chip_options(k_chip, CMD_ARGC - 6, CMD_ARGV + 6);
952  if (retval != ERROR_OK)
953  return retval;
954  }
955 
957  LOG_ERROR("Only %u Kinetis flash banks are supported", KINETIS_MAX_BANKS);
958  return ERROR_FAIL;
959  }
960 
961  bank->driver_priv = k_bank = &(k_chip->banks[k_chip->num_banks]);
962  k_bank->k_chip = k_chip;
963  k_bank->bank_number = k_chip->num_banks;
964  k_bank->bank = bank;
965  k_chip->num_banks++;
966 
967  return ERROR_OK;
968 }
969 
970 
972 {
973  struct kinetis_flash_bank *k_bank = bank->driver_priv;
974  if (!k_bank)
975  return;
976 
977  struct kinetis_chip *k_chip = k_bank->k_chip;
978  if (!k_chip)
979  return;
980 
981  k_chip->num_banks--;
982  if (k_chip->num_banks == 0)
983  free(k_chip);
984 }
985 
986 
987 static int kinetis_create_missing_banks(struct kinetis_chip *k_chip)
988 {
989  unsigned int num_blocks;
990  struct kinetis_flash_bank *k_bank;
991  struct flash_bank *bank;
992  char base_name[69], name[87], num[11];
993  char *class, *p;
994 
995  num_blocks = k_chip->num_pflash_blocks + k_chip->num_nvm_blocks;
996  if (num_blocks > KINETIS_MAX_BANKS) {
997  LOG_ERROR("Only %u Kinetis flash banks are supported", KINETIS_MAX_BANKS);
998  return ERROR_FAIL;
999  }
1000 
1001  bank = k_chip->banks[0].bank;
1002  if (bank && bank->name) {
1003  strncpy(base_name, bank->name, sizeof(base_name) - 1);
1004  base_name[sizeof(base_name) - 1] = '\0';
1005  p = strstr(base_name, ".pflash");
1006  if (p) {
1007  *p = '\0';
1008  if (k_chip->num_pflash_blocks > 1) {
1009  /* rename first bank if numbering is needed */
1010  snprintf(name, sizeof(name), "%s.pflash0", base_name);
1011  free(bank->name);
1012  bank->name = strdup(name);
1013  }
1014  }
1015  } else {
1016  strncpy(base_name, target_name(k_chip->target), sizeof(base_name) - 1);
1017  base_name[sizeof(base_name) - 1] = '\0';
1018  p = strstr(base_name, ".cpu");
1019  if (p)
1020  *p = '\0';
1021  }
1022 
1023  for (unsigned int bank_idx = 1; bank_idx < num_blocks; bank_idx++) {
1024  k_bank = &(k_chip->banks[bank_idx]);
1025  bank = k_bank->bank;
1026 
1027  if (bank)
1028  continue;
1029 
1030  num[0] = '\0';
1031 
1032  if (bank_idx < k_chip->num_pflash_blocks) {
1033  class = "pflash";
1034  if (k_chip->num_pflash_blocks > 1)
1035  snprintf(num, sizeof(num), "%u", bank_idx);
1036  } else {
1037  class = "flexnvm";
1038  if (k_chip->num_nvm_blocks > 1)
1039  snprintf(num, sizeof(num), "%u",
1040  bank_idx - k_chip->num_pflash_blocks);
1041  }
1042 
1043  bank = calloc(1, sizeof(struct flash_bank));
1044  if (!bank)
1045  return ERROR_FAIL;
1046 
1047  bank->target = k_chip->target;
1048  bank->driver = &kinetis_flash;
1049  bank->default_padded_value = bank->erased_value = 0xff;
1050  bank->minimal_write_gap = FLASH_WRITE_GAP_SECTOR;
1051 
1052  snprintf(name, sizeof(name), "%s.%s%s",
1053  base_name, class, num);
1054  bank->name = strdup(name);
1055 
1056  bank->driver_priv = k_bank = &(k_chip->banks[k_chip->num_banks]);
1057  k_bank->k_chip = k_chip;
1058  k_bank->bank_number = bank_idx;
1059  k_bank->bank = bank;
1060  if (k_chip->num_banks <= bank_idx)
1061  k_chip->num_banks = bank_idx + 1;
1062 
1064  }
1065  return ERROR_OK;
1066 }
1067 
1068 
1069 static int kinetis_disable_wdog_algo(struct target *target, size_t code_size, const uint8_t *code, uint32_t wdog_base)
1070 {
1071  struct working_area *wdog_algorithm;
1072  struct armv7m_algorithm armv7m_info;
1073  struct reg_param reg_params[1];
1074  int retval;
1075 
1076  if (target->state != TARGET_HALTED) {
1077  LOG_ERROR("Target not halted");
1078  return ERROR_TARGET_NOT_HALTED;
1079  }
1080 
1081  retval = target_alloc_working_area(target, code_size, &wdog_algorithm);
1082  if (retval != ERROR_OK)
1083  return retval;
1084 
1085  retval = target_write_buffer(target, wdog_algorithm->address,
1086  code_size, code);
1087  if (retval == ERROR_OK) {
1088  armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
1089  armv7m_info.core_mode = ARM_MODE_THREAD;
1090 
1091  init_reg_param(&reg_params[0], "r0", 32, PARAM_OUT);
1092  buf_set_u32(reg_params[0].value, 0, 32, wdog_base);
1093 
1094  retval = target_run_algorithm(target, 0, NULL, 1, reg_params,
1095  wdog_algorithm->address,
1096  wdog_algorithm->address + code_size - 2,
1097  500, &armv7m_info);
1098 
1099  destroy_reg_param(&reg_params[0]);
1100 
1101  if (retval != ERROR_OK)
1102  LOG_ERROR("Error executing Kinetis WDOG unlock algorithm");
1103  }
1104 
1105  target_free_working_area(target, wdog_algorithm);
1106 
1107  return retval;
1108 }
1109 
1110 /* Disable the watchdog on Kinetis devices
1111  * Standard Kx WDOG peripheral checks timing and therefore requires to run algo.
1112  */
1114 {
1115  const uint32_t wdog_base = WDOG_BASE;
1116  uint16_t wdog;
1117  int retval;
1118 
1119  static const uint8_t kinetis_unlock_wdog_code[] = {
1120 #include "../../../contrib/loaders/watchdog/armv7m_kinetis_wdog.inc"
1121  };
1122 
1123  retval = target_read_u16(target, wdog_base + WDOG_STCTRLH_OFFSET, &wdog);
1124  if (retval != ERROR_OK)
1125  return retval;
1126 
1127  if ((wdog & 0x1) == 0) {
1128  /* watchdog already disabled */
1129  return ERROR_OK;
1130  }
1131  LOG_INFO("Disabling Kinetis watchdog (initial WDOG_STCTRLH = 0x%04" PRIx16 ")", wdog);
1132 
1133  retval = kinetis_disable_wdog_algo(target, sizeof(kinetis_unlock_wdog_code), kinetis_unlock_wdog_code, wdog_base);
1134  if (retval != ERROR_OK)
1135  return retval;
1136 
1137  retval = target_read_u16(target, wdog_base + WDOG_STCTRLH_OFFSET, &wdog);
1138  if (retval != ERROR_OK)
1139  return retval;
1140 
1141  LOG_INFO("WDOG_STCTRLH = 0x%04" PRIx16, wdog);
1142  return (wdog & 0x1) ? ERROR_FAIL : ERROR_OK;
1143 }
1144 
1145 static int kinetis_disable_wdog32(struct target *target, uint32_t wdog_base)
1146 {
1147  uint32_t wdog_cs;
1148  int retval;
1149 
1150  static const uint8_t kinetis_unlock_wdog_code[] = {
1151 #include "../../../contrib/loaders/watchdog/armv7m_kinetis_wdog32.inc"
1152  };
1153 
1154  retval = target_read_u32(target, wdog_base + WDOG32_CS_OFFSET, &wdog_cs);
1155  if (retval != ERROR_OK)
1156  return retval;
1157 
1158  if ((wdog_cs & 0x80) == 0)
1159  return ERROR_OK; /* watchdog already disabled */
1160 
1161  LOG_INFO("Disabling Kinetis watchdog (initial WDOG_CS 0x%08" PRIx32 ")", wdog_cs);
1162 
1163  retval = kinetis_disable_wdog_algo(target, sizeof(kinetis_unlock_wdog_code), kinetis_unlock_wdog_code, wdog_base);
1164  if (retval != ERROR_OK)
1165  return retval;
1166 
1167  retval = target_read_u32(target, wdog_base + WDOG32_CS_OFFSET, &wdog_cs);
1168  if (retval != ERROR_OK)
1169  return retval;
1170 
1171  if ((wdog_cs & 0x80) == 0)
1172  return ERROR_OK; /* watchdog disabled successfully */
1173 
1174  LOG_ERROR("Cannot disable Kinetis watchdog (WDOG_CS 0x%08" PRIx32 "), issue 'reset init'", wdog_cs);
1175  return ERROR_FAIL;
1176 }
1177 
1178 static int kinetis_disable_wdog(struct kinetis_chip *k_chip)
1179 {
1180  struct target *target = k_chip->target;
1181  uint8_t sim_copc;
1182  int retval;
1183 
1184  if (!k_chip->probed) {
1185  switch (k_chip->chip_type) {
1186  case KINETIS_CT_S32K:
1187  retval = kinetis_probe_chip_s32k(k_chip);
1188  break;
1189  default:
1190  retval = kinetis_probe_chip(k_chip);
1191  }
1192  if (retval != ERROR_OK)
1193  return retval;
1194  }
1195 
1196  switch (k_chip->watchdog_type) {
1197  case KINETIS_WDOG_K:
1199 
1200  case KINETIS_WDOG_COP:
1201  retval = target_read_u8(target, SIM_COPC, &sim_copc);
1202  if (retval != ERROR_OK)
1203  return retval;
1204 
1205  if ((sim_copc & 0xc) == 0)
1206  return ERROR_OK; /* watchdog already disabled */
1207 
1208  LOG_INFO("Disabling Kinetis watchdog (initial SIM_COPC 0x%02" PRIx8 ")", sim_copc);
1209  retval = target_write_u8(target, SIM_COPC, sim_copc & ~0xc);
1210  if (retval != ERROR_OK)
1211  return retval;
1212 
1213  retval = target_read_u8(target, SIM_COPC, &sim_copc);
1214  if (retval != ERROR_OK)
1215  return retval;
1216 
1217  if ((sim_copc & 0xc) == 0)
1218  return ERROR_OK; /* watchdog disabled successfully */
1219 
1220  LOG_ERROR("Cannot disable Kinetis watchdog (SIM_COPC 0x%02" PRIx8 "), issue 'reset init'", sim_copc);
1221  return ERROR_FAIL;
1222 
1223  case KINETIS_WDOG32_KE1X:
1225 
1226  case KINETIS_WDOG32_KL28:
1228 
1229  default:
1230  return ERROR_OK;
1231  }
1232 }
1233 
1234 COMMAND_HANDLER(kinetis_disable_wdog_handler)
1235 {
1237  struct kinetis_chip *k_chip = kinetis_get_chip(target);
1238 
1239  if (!k_chip)
1240  return ERROR_FAIL;
1241 
1242  if (CMD_ARGC > 0)
1244 
1245  return kinetis_disable_wdog(k_chip);
1246 }
1247 
1248 
1249 static int kinetis_ftfx_decode_error(uint8_t fstat)
1250 {
1251  if (fstat & 0x20) {
1252  LOG_ERROR("Flash operation failed, illegal command");
1254 
1255  } else if (fstat & 0x10)
1256  LOG_ERROR("Flash operation failed, protection violated");
1257 
1258  else if (fstat & 0x40)
1259  LOG_ERROR("Flash operation failed, read collision");
1260 
1261  else if (fstat & 0x80)
1262  return ERROR_OK;
1263 
1264  else
1265  LOG_ERROR("Flash operation timed out");
1266 
1268 }
1269 
1271 {
1272  /* reset error flags */
1273  return target_write_u8(target, FTFX_FSTAT, 0x70);
1274 }
1275 
1276 
1278 {
1279  int result;
1280  uint8_t fstat;
1281 
1282  /* wait until busy */
1283  for (unsigned int i = 0; i < 50; i++) {
1284  result = target_read_u8(target, FTFX_FSTAT, &fstat);
1285  if (result != ERROR_OK)
1286  return result;
1287 
1288  if (fstat & 0x80)
1289  break;
1290  }
1291 
1292  if ((fstat & 0x80) == 0) {
1293  LOG_ERROR("Flash controller is busy");
1295  }
1296  if (fstat != 0x80) {
1297  /* reset error flags */
1298  result = kinetis_ftfx_clear_error(target);
1299  }
1300  return result;
1301 }
1302 
1303 /* Kinetis Program-LongWord Microcodes */
1304 static const uint8_t kinetis_flash_write_code[] = {
1305 #include "../../../contrib/loaders/flash/kinetis/kinetis_flash.inc"
1306 };
1307 
1308 /* Program LongWord Block Write */
1309 static int kinetis_write_block(struct flash_bank *bank, const uint8_t *buffer,
1310  uint32_t offset, uint32_t wcount)
1311 {
1312  struct target *target = bank->target;
1313  uint32_t buffer_size;
1314  struct working_area *write_algorithm;
1315  struct working_area *source;
1316  struct kinetis_flash_bank *k_bank = bank->driver_priv;
1317  uint32_t address = k_bank->prog_base + offset;
1318  uint32_t end_address;
1319  struct reg_param reg_params[5];
1320  struct armv7m_algorithm armv7m_info;
1321  int retval;
1322  uint8_t fstat;
1323 
1324  /* allocate working area with flash programming code */
1326  &write_algorithm) != ERROR_OK) {
1327  LOG_WARNING("no working area available, can't do block memory writes");
1329  }
1330 
1331  retval = target_write_buffer(target, write_algorithm->address,
1333  if (retval != ERROR_OK)
1334  return retval;
1335 
1336  /* memory buffer, size *must* be multiple of word */
1337  buffer_size = target_get_working_area_avail(target) & ~(sizeof(uint32_t) - 1);
1338  if (buffer_size < 256) {
1339  LOG_WARNING("large enough working area not available, can't do block memory writes");
1341  } else if (buffer_size > 16384) {
1342  /* probably won't benefit from more than 16k ... */
1343  buffer_size = 16384;
1344  }
1345 
1347  LOG_ERROR("allocating working area failed");
1349  }
1350 
1351  armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
1352  armv7m_info.core_mode = ARM_MODE_THREAD;
1353 
1354  init_reg_param(&reg_params[0], "r0", 32, PARAM_IN_OUT); /* address */
1355  init_reg_param(&reg_params[1], "r1", 32, PARAM_OUT); /* word count */
1356  init_reg_param(&reg_params[2], "r2", 32, PARAM_OUT);
1357  init_reg_param(&reg_params[3], "r3", 32, PARAM_OUT);
1358  init_reg_param(&reg_params[4], "r4", 32, PARAM_OUT);
1359 
1360  buf_set_u32(reg_params[0].value, 0, 32, address);
1361  buf_set_u32(reg_params[1].value, 0, 32, wcount);
1362  buf_set_u32(reg_params[2].value, 0, 32, source->address);
1363  buf_set_u32(reg_params[3].value, 0, 32, source->address + source->size);
1364  buf_set_u32(reg_params[4].value, 0, 32, FTFX_FSTAT);
1365 
1366  retval = target_run_flash_async_algorithm(target, buffer, wcount, 4,
1367  0, NULL,
1368  5, reg_params,
1369  source->address, source->size,
1370  write_algorithm->address, 0,
1371  &armv7m_info);
1372 
1373  if (retval == ERROR_FLASH_OPERATION_FAILED) {
1374  end_address = buf_get_u32(reg_params[0].value, 0, 32);
1375 
1376  LOG_ERROR("Error writing flash at %08" PRIx32, end_address);
1377 
1378  retval = target_read_u8(target, FTFX_FSTAT, &fstat);
1379  if (retval == ERROR_OK) {
1380  retval = kinetis_ftfx_decode_error(fstat);
1381 
1382  /* reset error flags */
1384  }
1385  } else if (retval != ERROR_OK)
1386  LOG_ERROR("Error executing kinetis Flash programming algorithm");
1387 
1389  target_free_working_area(target, write_algorithm);
1390 
1391  destroy_reg_param(&reg_params[0]);
1392  destroy_reg_param(&reg_params[1]);
1393  destroy_reg_param(&reg_params[2]);
1394  destroy_reg_param(&reg_params[3]);
1395  destroy_reg_param(&reg_params[4]);
1396 
1397  return retval;
1398 }
1399 
1400 static int kinetis_protect(struct flash_bank *bank, int set, unsigned int first,
1401  unsigned int last)
1402 {
1403  if (allow_fcf_writes) {
1404  LOG_ERROR("Protection setting is possible with 'kinetis fcf_source protection' only!");
1405  return ERROR_FAIL;
1406  }
1407 
1408  if (!bank->prot_blocks || bank->num_prot_blocks == 0) {
1409  LOG_ERROR("No protection possible for current bank!");
1410  return ERROR_FLASH_BANK_INVALID;
1411  }
1412 
1413  for (unsigned int i = first; i < bank->num_prot_blocks && i <= last; i++)
1414  bank->prot_blocks[i].is_protected = set;
1415 
1416  LOG_INFO("Protection bits will be written at the next FCF sector erase or write.");
1417  LOG_INFO("Do not issue 'flash info' command until protection is written,");
1418  LOG_INFO("doing so would re-read protection status from MCU.");
1419 
1420  return ERROR_OK;
1421 }
1422 
1424 {
1425  struct kinetis_flash_bank *k_bank = bank->driver_priv;
1426  int result;
1427  int b;
1428  uint32_t fprot;
1429 
1430  if (k_bank->flash_class == FC_PFLASH) {
1431 
1432  /* read protection register */
1433  result = target_read_u32(bank->target, FTFX_FPROT3, &fprot);
1434  if (result != ERROR_OK)
1435  return result;
1436 
1437  /* Every bit protects 1/32 of the full flash (not necessarily just this bank) */
1438 
1439  } else if (k_bank->flash_class == FC_FLEX_NVM) {
1440  uint8_t fdprot;
1441 
1442  /* read protection register */
1443  result = target_read_u8(bank->target, FTFX_FDPROT, &fdprot);
1444  if (result != ERROR_OK)
1445  return result;
1446 
1447  fprot = fdprot;
1448 
1449  } else {
1450  LOG_ERROR("Protection checks for FlexRAM not supported");
1451  return ERROR_FLASH_BANK_INVALID;
1452  }
1453 
1454  b = k_bank->protection_block;
1455  for (unsigned int i = 0; i < bank->num_prot_blocks; i++) {
1456  if ((fprot >> b) & 1)
1457  bank->prot_blocks[i].is_protected = 0;
1458  else
1459  bank->prot_blocks[i].is_protected = 1;
1460 
1461  b++;
1462  }
1463 
1464  return ERROR_OK;
1465 }
1466 
1467 
1468 static int kinetis_fill_fcf(struct flash_bank *bank, uint8_t *fcf)
1469 {
1470  uint32_t fprot = 0xffffffff;
1471  uint8_t fsec = 0xfe; /* set MCU unsecure */
1472  uint8_t fdprot = 0xff;
1473  unsigned int num_blocks;
1474  uint32_t pflash_bit;
1475  uint8_t dflash_bit;
1476  struct flash_bank *bank_iter;
1477  struct kinetis_flash_bank *k_bank = bank->driver_priv;
1478  struct kinetis_chip *k_chip = k_bank->k_chip;
1479 
1480  memset(fcf, 0xff, FCF_SIZE);
1481 
1482  pflash_bit = 1;
1483  dflash_bit = 1;
1484 
1485  /* iterate over all kinetis banks */
1486  /* current bank is bank 0, it contains FCF */
1487  num_blocks = k_chip->num_pflash_blocks + k_chip->num_nvm_blocks;
1488  for (unsigned int bank_idx = 0; bank_idx < num_blocks; bank_idx++) {
1489  k_bank = &(k_chip->banks[bank_idx]);
1490  bank_iter = k_bank->bank;
1491 
1492  if (!bank_iter) {
1493  LOG_WARNING("Missing bank %u configuration, FCF protection flags may be incomplete", bank_idx);
1494  continue;
1495  }
1496 
1497  kinetis_auto_probe(bank_iter);
1498 
1499  if (bank_iter->num_prot_blocks == 0) {
1500  if (k_bank->flash_class == FC_PFLASH) {
1501  LOG_ERROR("BUG: PFLASH bank %u has no protection blocks",
1502  bank_idx);
1503  } else {
1504  LOG_DEBUG("skipping FLEX_NVM bank %u with no prot blocks (EE bkp only)",
1505  bank_idx);
1506  }
1507  continue;
1508  }
1509 
1510  if (!bank_iter->prot_blocks) {
1511  LOG_ERROR("BUG: bank %u has NULL protection blocks array",
1512  bank_idx);
1513  continue;
1514  }
1515 
1516  if (k_bank->flash_class == FC_PFLASH) {
1517  for (unsigned int i = 0; i < bank_iter->num_prot_blocks; i++) {
1518  if (bank_iter->prot_blocks[i].is_protected == 1)
1519  fprot &= ~pflash_bit;
1520 
1521  pflash_bit <<= 1;
1522  }
1523 
1524  } else if (k_bank->flash_class == FC_FLEX_NVM) {
1525  for (unsigned int i = 0; i < bank_iter->num_prot_blocks; i++) {
1526  if (bank_iter->prot_blocks[i].is_protected == 1)
1527  fdprot &= ~dflash_bit;
1528 
1529  dflash_bit <<= 1;
1530  }
1531 
1532  }
1533  }
1534 
1535  target_buffer_set_u32(bank->target, fcf + FCF_FPROT, fprot);
1536  fcf[FCF_FSEC] = fsec;
1537  fcf[FCF_FOPT] = fcf_fopt;
1538  fcf[FCF_FDPROT] = fdprot;
1539  return ERROR_OK;
1540 }
1541 
1542 static int kinetis_ftfx_command(struct target *target, uint8_t fcmd, uint32_t faddr,
1543  uint8_t fccob4, uint8_t fccob5, uint8_t fccob6, uint8_t fccob7,
1544  uint8_t fccob8, uint8_t fccob9, uint8_t fccoba, uint8_t fccobb,
1545  uint8_t *ftfx_fstat)
1546 {
1547  uint8_t command[12] = {faddr & 0xff, (faddr >> 8) & 0xff, (faddr >> 16) & 0xff, fcmd,
1548  fccob7, fccob6, fccob5, fccob4,
1549  fccobb, fccoba, fccob9, fccob8};
1550  int result;
1551  uint8_t fstat;
1552  int64_t ms_timeout = timeval_ms() + 250;
1553 
1554  result = target_write_memory(target, FTFX_FCCOB3, 4, 3, command);
1555  if (result != ERROR_OK)
1556  return result;
1557 
1558  /* start command */
1559  result = target_write_u8(target, FTFX_FSTAT, 0x80);
1560  if (result != ERROR_OK)
1561  return result;
1562 
1563  /* wait for done */
1564  do {
1565  result = target_read_u8(target, FTFX_FSTAT, &fstat);
1566 
1567  if (result != ERROR_OK)
1568  return result;
1569 
1570  if (fstat & 0x80)
1571  break;
1572 
1573  } while (timeval_ms() < ms_timeout);
1574 
1575  if (ftfx_fstat)
1576  *ftfx_fstat = fstat;
1577 
1578  if ((fstat & 0xf0) != 0x80) {
1579  LOG_DEBUG("ftfx command failed FSTAT: %02X FCCOB: %02X%02X%02X%02X %02X%02X%02X%02X %02X%02X%02X%02X",
1580  fstat, command[3], command[2], command[1], command[0],
1581  command[7], command[6], command[5], command[4],
1582  command[11], command[10], command[9], command[8]);
1583 
1584  return kinetis_ftfx_decode_error(fstat);
1585  }
1586 
1587  return ERROR_OK;
1588 }
1589 
1590 
1591 static int kinetis_read_pmstat(struct kinetis_chip *k_chip, uint8_t *pmstat)
1592 {
1593  int result;
1594  uint32_t stat32;
1595  struct target *target = k_chip->target;
1596 
1597  switch (k_chip->sysmodectrlr_type) {
1598  case KINETIS_SMC:
1599  return target_read_u8(target, SMC_PMSTAT, pmstat);
1600 
1601  case KINETIS_SMC32:
1602  result = target_read_u32(target, SMC32_PMSTAT, &stat32);
1603  if (result == ERROR_OK)
1604  *pmstat = stat32 & 0xff;
1605  return result;
1606 
1607  case KINETIS_MC:
1608  /* emulate SMC by reading PMC_REGSC bit 3 (VLPRS) */
1609  result = target_read_u8(target, PMC_REGSC, pmstat);
1610  if (result == ERROR_OK) {
1611  if (*pmstat & 0x08)
1612  *pmstat = PM_STAT_VLPR;
1613  else
1614  *pmstat = PM_STAT_RUN;
1615  }
1616  return result;
1617  }
1618  return ERROR_FAIL;
1619 }
1620 
1621 static int kinetis_check_run_mode(struct kinetis_chip *k_chip)
1622 {
1623  int result;
1624  uint8_t pmstat;
1625  struct target *target;
1626 
1627  if (!k_chip) {
1628  LOG_ERROR("Chip not probed.");
1629  return ERROR_FAIL;
1630  }
1631  target = k_chip->target;
1632 
1633  if (target->state != TARGET_HALTED) {
1634  LOG_ERROR("Target not halted");
1635  return ERROR_TARGET_NOT_HALTED;
1636  }
1637 
1638  result = kinetis_read_pmstat(k_chip, &pmstat);
1639  if (result != ERROR_OK)
1640  return result;
1641 
1642  if (pmstat == PM_STAT_RUN)
1643  return ERROR_OK;
1644 
1645  if (pmstat == PM_STAT_VLPR) {
1646  /* It is safe to switch from VLPR to RUN mode without changing clock */
1647  LOG_INFO("Switching from VLPR to RUN mode.");
1648 
1649  switch (k_chip->sysmodectrlr_type) {
1650  case KINETIS_SMC:
1652  break;
1653 
1654  case KINETIS_SMC32:
1656  break;
1657 
1658  case KINETIS_MC:
1660  break;
1661  }
1662  if (result != ERROR_OK)
1663  return result;
1664 
1665  for (unsigned int i = 100; i > 0; i--) {
1666  result = kinetis_read_pmstat(k_chip, &pmstat);
1667  if (result != ERROR_OK)
1668  return result;
1669 
1670  if (pmstat == PM_STAT_RUN)
1671  return ERROR_OK;
1672  }
1673  }
1674 
1675  LOG_ERROR("Flash operation not possible in current run mode: SMC_PMSTAT: 0x%x", pmstat);
1676  LOG_ERROR("Issue a 'reset init' command.");
1677  return ERROR_TARGET_NOT_HALTED;
1678 }
1679 
1680 
1682 {
1683  struct target *target = k_chip->target;
1684 
1685  switch (k_chip->cache_type) {
1686  case KINETIS_CACHE_K:
1687  target_write_u8(target, FMC_PFB01CR + 2, 0xf0);
1688  /* Set CINV_WAY bits - request invalidate of all cache ways */
1689  /* FMC_PFB0CR has same address and CINV_WAY bits as FMC_PFB01CR */
1690  break;
1691 
1692  case KINETIS_CACHE_L:
1693  target_write_u8(target, MCM_PLACR + 1, 0x04);
1694  /* set bit CFCC - Clear Flash Controller Cache */
1695  break;
1696 
1697  case KINETIS_CACHE_MSCM:
1699  /* disable data prefetch and flash speculate */
1700  break;
1701 
1702  case KINETIS_CACHE_MSCM2:
1705  /* disable data prefetch and flash speculate */
1706  break;
1707 
1708  default:
1709  break;
1710  }
1711 }
1712 
1713 
1714 static int kinetis_erase(struct flash_bank *bank, unsigned int first,
1715  unsigned int last)
1716 {
1717  int result;
1718  struct kinetis_flash_bank *k_bank = bank->driver_priv;
1719  struct kinetis_chip *k_chip = k_bank->k_chip;
1720 
1721  result = kinetis_check_run_mode(k_chip);
1722  if (result != ERROR_OK)
1723  return result;
1724 
1725  /* reset error flags */
1726  result = kinetis_ftfx_prepare(bank->target);
1727  if (result != ERROR_OK)
1728  return result;
1729 
1730  if ((first > bank->num_sectors) || (last > bank->num_sectors))
1732 
1733  /*
1734  * FIXME: TODO: use the 'Erase Flash Block' command if the
1735  * requested erase is PFlash or NVM and encompasses the entire
1736  * block. Should be quicker.
1737  */
1738  for (unsigned int i = first; i <= last; i++) {
1739  /* set command and sector address */
1740  result = kinetis_ftfx_command(bank->target, FTFX_CMD_SECTERASE, k_bank->prog_base + bank->sectors[i].offset,
1741  0, 0, 0, 0, 0, 0, 0, 0, NULL);
1742 
1743  if (result != ERROR_OK) {
1744  LOG_WARNING("erase sector %u failed", i);
1746  }
1747 
1748  if (k_bank->prog_base == 0
1749  && bank->sectors[i].offset <= FCF_ADDRESS
1750  && bank->sectors[i].offset + bank->sectors[i].size > FCF_ADDRESS + FCF_SIZE) {
1751  if (allow_fcf_writes) {
1752  LOG_WARNING("Flash Configuration Field erased, DO NOT reset or power off the device");
1753  LOG_WARNING("until correct FCF is programmed or MCU gets security lock.");
1754  } else {
1755  uint8_t fcf_buffer[FCF_SIZE];
1756 
1757  kinetis_fill_fcf(bank, fcf_buffer);
1758  result = kinetis_write_inner(bank, fcf_buffer, FCF_ADDRESS, FCF_SIZE);
1759  if (result != ERROR_OK)
1760  LOG_WARNING("Flash Configuration Field write failed");
1761  else
1762  LOG_DEBUG("Generated FCF written");
1763  }
1764  }
1765  }
1766 
1768 
1769  return ERROR_OK;
1770 }
1771 
1773 {
1774  int result;
1775  uint8_t ftfx_fcnfg;
1776 
1777  /* check if ram ready */
1778  result = target_read_u8(target, FTFX_FCNFG, &ftfx_fcnfg);
1779  if (result != ERROR_OK)
1780  return result;
1781 
1782  if (ftfx_fcnfg & (1 << 1))
1783  return ERROR_OK; /* ram ready */
1784 
1785  /* make flex ram available */
1786  result = kinetis_ftfx_command(target, FTFX_CMD_SETFLEXRAM, 0x00ff0000,
1787  0, 0, 0, 0, 0, 0, 0, 0, NULL);
1788  if (result != ERROR_OK)
1790 
1791  /* check again */
1792  result = target_read_u8(target, FTFX_FCNFG, &ftfx_fcnfg);
1793  if (result != ERROR_OK)
1794  return result;
1795 
1796  if (ftfx_fcnfg & (1 << 1))
1797  return ERROR_OK; /* ram ready */
1798 
1800 }
1801 
1802 
1803 static int kinetis_write_sections(struct flash_bank *bank, const uint8_t *buffer,
1804  uint32_t offset, uint32_t count)
1805 {
1806  int result = ERROR_OK;
1807  struct kinetis_flash_bank *k_bank = bank->driver_priv;
1808  struct kinetis_chip *k_chip = k_bank->k_chip;
1809  uint8_t *buffer_aligned = NULL;
1810  /*
1811  * Kinetis uses different terms for the granularity of
1812  * sector writes, e.g. "phrase" or "128 bits". We use
1813  * the generic term "chunk". The largest possible
1814  * Kinetis "chunk" is 16 bytes (128 bits).
1815  */
1816  uint32_t prog_section_chunk_bytes = k_bank->sector_size >> 8;
1817  uint32_t prog_size_bytes = k_chip->max_flash_prog_size;
1818 
1819  while (count > 0) {
1820  uint32_t size = prog_size_bytes - offset % prog_size_bytes;
1821  uint32_t align_begin = offset % prog_section_chunk_bytes;
1822  uint32_t align_end;
1823  uint32_t size_aligned;
1824  uint16_t chunk_count;
1825  uint8_t ftfx_fstat;
1826 
1827  if (size > count)
1828  size = count;
1829 
1830  align_end = (align_begin + size) % prog_section_chunk_bytes;
1831  if (align_end)
1832  align_end = prog_section_chunk_bytes - align_end;
1833 
1834  size_aligned = align_begin + size + align_end;
1835  chunk_count = size_aligned / prog_section_chunk_bytes;
1836 
1837  if (size != size_aligned) {
1838  /* aligned section: the first, the last or the only */
1839  if (!buffer_aligned)
1840  buffer_aligned = malloc(prog_size_bytes);
1841 
1842  memset(buffer_aligned, 0xff, size_aligned);
1843  memcpy(buffer_aligned + align_begin, buffer, size);
1844 
1845  result = target_write_memory(bank->target, k_chip->progr_accel_ram,
1846  4, size_aligned / 4, buffer_aligned);
1847 
1848  LOG_DEBUG("section @ " TARGET_ADDR_FMT " aligned begin %" PRIu32
1849  ", end %" PRIu32,
1850  bank->base + offset, align_begin, align_end);
1851  } else
1852  result = target_write_memory(bank->target, k_chip->progr_accel_ram,
1853  4, size_aligned / 4, buffer);
1854 
1855  LOG_DEBUG("write section @ " TARGET_ADDR_FMT " with length %" PRIu32
1856  " bytes",
1857  bank->base + offset, size);
1858 
1859  if (result != ERROR_OK) {
1860  LOG_ERROR("target_write_memory failed");
1861  break;
1862  }
1863 
1864  /* execute section-write command */
1865  result = kinetis_ftfx_command(bank->target, FTFX_CMD_SECTWRITE,
1866  k_bank->prog_base + offset - align_begin,
1867  chunk_count>>8, chunk_count, 0, 0,
1868  0, 0, 0, 0, &ftfx_fstat);
1869 
1870  if (result != ERROR_OK) {
1871  LOG_ERROR("Error writing section at " TARGET_ADDR_FMT,
1872  bank->base + offset);
1873  break;
1874  }
1875 
1876  if (ftfx_fstat & 0x01) {
1877  LOG_ERROR("Flash write error at " TARGET_ADDR_FMT,
1878  bank->base + offset);
1879  if (k_bank->prog_base == 0 && offset == FCF_ADDRESS + FCF_SIZE
1880  && (k_chip->flash_support & FS_WIDTH_256BIT)) {
1881  LOG_ERROR("Flash write immediately after the end of Flash Config Field shows error");
1882  LOG_ERROR("because the flash memory is 256 bits wide (data were written correctly).");
1883  LOG_ERROR("Either change the linker script to add a gap of 16 bytes after FCF");
1884  LOG_ERROR("or set 'kinetis fcf_source write'");
1885  }
1886  }
1887 
1888  buffer += size;
1889  offset += size;
1890  count -= size;
1891 
1892  keep_alive();
1893  }
1894 
1895  free(buffer_aligned);
1896  return result;
1897 }
1898 
1899 
1900 static int kinetis_write_inner(struct flash_bank *bank, const uint8_t *buffer,
1901  uint32_t offset, uint32_t count)
1902 {
1903  int result;
1904  bool fallback = false;
1905  struct kinetis_flash_bank *k_bank = bank->driver_priv;
1906  struct kinetis_chip *k_chip = k_bank->k_chip;
1907 
1908  if (!(k_chip->flash_support & FS_PROGRAM_SECTOR)) {
1909  /* fallback to longword write */
1910  fallback = true;
1911  LOG_INFO("This device supports Program Longword execution only.");
1912  } else {
1913  result = kinetis_make_ram_ready(bank->target);
1914  if (result != ERROR_OK) {
1915  fallback = true;
1916  LOG_WARNING("FlexRAM not ready, fallback to slow longword write.");
1917  }
1918  }
1919 
1920  LOG_DEBUG("flash write @ " TARGET_ADDR_FMT, bank->base + offset);
1921 
1922  if (!fallback) {
1923  /* program section command */
1925  } else if (k_chip->flash_support & FS_PROGRAM_LONGWORD) {
1926  /* program longword command, not supported in FTFE */
1927  uint8_t *new_buffer = NULL;
1928 
1929  /* check word alignment */
1930  if (offset & 0x3) {
1931  LOG_ERROR("offset 0x%" PRIx32 " breaks the required alignment", offset);
1933  }
1934 
1935  if (count & 0x3) {
1936  uint32_t old_count = count;
1937  count = (old_count | 3) + 1;
1938  new_buffer = malloc(count);
1939  if (!new_buffer) {
1940  LOG_ERROR("odd number of bytes to write and no memory "
1941  "for padding buffer");
1942  return ERROR_FAIL;
1943  }
1944  LOG_INFO("odd number of bytes to write (%" PRIu32 "), extending to %" PRIu32 " "
1945  "and padding with 0xff", old_count, count);
1946  memset(new_buffer + old_count, 0xff, count - old_count);
1947  buffer = memcpy(new_buffer, buffer, old_count);
1948  }
1949 
1950  uint32_t words_remaining = count / 4;
1951 
1952  kinetis_disable_wdog(k_chip);
1953 
1954  /* try using a block write */
1955  result = kinetis_write_block(bank, buffer, offset, words_remaining);
1956 
1957  if (result == ERROR_TARGET_RESOURCE_NOT_AVAILABLE) {
1958  /* if block write failed (no sufficient working area),
1959  * we use normal (slow) single word accesses */
1960  LOG_WARNING("couldn't use block writes, falling back to single "
1961  "memory accesses");
1962 
1963  while (words_remaining) {
1964  uint8_t ftfx_fstat;
1965 
1966  LOG_DEBUG("write longword @ %08" PRIx32, (uint32_t)(bank->base + offset));
1967 
1968  result = kinetis_ftfx_command(bank->target, FTFX_CMD_LWORDPROG, k_bank->prog_base + offset,
1969  buffer[3], buffer[2], buffer[1], buffer[0],
1970  0, 0, 0, 0, &ftfx_fstat);
1971 
1972  if (result != ERROR_OK) {
1973  LOG_ERROR("Error writing longword at " TARGET_ADDR_FMT,
1974  bank->base + offset);
1975  break;
1976  }
1977 
1978  if (ftfx_fstat & 0x01)
1979  LOG_ERROR("Flash write error at " TARGET_ADDR_FMT,
1980  bank->base + offset);
1981 
1982  buffer += 4;
1983  offset += 4;
1984  words_remaining--;
1985 
1986  keep_alive();
1987  }
1988  }
1989  free(new_buffer);
1990  } else {
1991  LOG_ERROR("Flash write strategy not implemented");
1993  }
1994 
1996  return result;
1997 }
1998 
1999 
2000 static int kinetis_write(struct flash_bank *bank, const uint8_t *buffer,
2001  uint32_t offset, uint32_t count)
2002 {
2003  int result;
2004  bool set_fcf = false;
2005  bool fcf_in_data_valid = false;
2006  bool fcf_differs = false;
2007  int sect = 0;
2008  struct kinetis_flash_bank *k_bank = bank->driver_priv;
2009  struct kinetis_chip *k_chip = k_bank->k_chip;
2010  uint8_t fcf_buffer[FCF_SIZE];
2011  uint8_t fcf_current[FCF_SIZE];
2012  uint8_t fcf_in_data[FCF_SIZE];
2013 
2014  result = kinetis_check_run_mode(k_chip);
2015  if (result != ERROR_OK)
2016  return result;
2017 
2018  /* reset error flags */
2019  result = kinetis_ftfx_prepare(bank->target);
2020  if (result != ERROR_OK)
2021  return result;
2022 
2023  if (k_bank->prog_base == 0 && !allow_fcf_writes) {
2024  if (bank->sectors[1].offset <= FCF_ADDRESS)
2025  sect = 1; /* 1kb sector, FCF in 2nd sector */
2026 
2027  if (offset < bank->sectors[sect].offset + bank->sectors[sect].size
2028  && offset + count > bank->sectors[sect].offset)
2029  set_fcf = true; /* write to any part of sector with FCF */
2030  }
2031 
2032  if (set_fcf) {
2033  kinetis_fill_fcf(bank, fcf_buffer);
2034 
2035  fcf_in_data_valid = offset <= FCF_ADDRESS
2036  && offset + count >= FCF_ADDRESS + FCF_SIZE;
2037  if (fcf_in_data_valid) {
2038  memcpy(fcf_in_data, buffer + FCF_ADDRESS - offset, FCF_SIZE);
2039  if (memcmp(fcf_in_data, fcf_buffer, 8)) {
2040  fcf_differs = true;
2041  LOG_INFO("Setting of backdoor key is not supported in mode 'kinetis fcf_source protection'.");
2042  }
2043  if (memcmp(fcf_in_data + FCF_FPROT, fcf_buffer + FCF_FPROT, 4)) {
2044  fcf_differs = true;
2045  LOG_INFO("Flash protection requested in the programmed file differs from current setting.");
2046  }
2047  if (fcf_in_data[FCF_FDPROT] != fcf_buffer[FCF_FDPROT]) {
2048  fcf_differs = true;
2049  LOG_INFO("Data flash protection requested in the programmed file differs from current setting.");
2050  }
2051  if ((fcf_in_data[FCF_FSEC] & 3) != 2) {
2052  fcf_in_data_valid = false;
2053  LOG_INFO("Device security requested in the programmed file! Write denied.");
2054  } else if (fcf_in_data[FCF_FSEC] != fcf_buffer[FCF_FSEC]) {
2055  fcf_differs = true;
2056  LOG_INFO("Strange unsecure mode 0x%02" PRIx8
2057  " requested in the programmed file, set FSEC = 0x%02" PRIx8
2058  " in the startup code!",
2059  fcf_in_data[FCF_FSEC], fcf_buffer[FCF_FSEC]);
2060  }
2061  if (fcf_in_data[FCF_FOPT] != fcf_buffer[FCF_FOPT]) {
2062  fcf_differs = true;
2063  LOG_INFO("FOPT requested in the programmed file differs from current setting, set 'kinetis fopt 0x%02"
2064  PRIx8 "'.", fcf_in_data[FCF_FOPT]);
2065  }
2066 
2067  /* If the device has ECC flash, then we cannot re-program FCF */
2068  if (fcf_differs) {
2069  if (k_chip->flash_support & FS_ECC) {
2070  fcf_in_data_valid = false;
2071  LOG_INFO("Cannot re-program FCF. Expect verify errors at FCF (0x400-0x40f).");
2072  } else {
2073  LOG_INFO("Trying to re-program FCF.");
2074  if (!(k_chip->flash_support & FS_PROGRAM_LONGWORD))
2075  LOG_INFO("Flash re-programming may fail on this device!");
2076  }
2077  }
2078  }
2079  }
2080 
2081  if (set_fcf && !fcf_in_data_valid) {
2082  if (offset < FCF_ADDRESS) {
2083  /* write part preceding FCF */
2085  if (result != ERROR_OK)
2086  return result;
2087  }
2088 
2089  result = target_read_memory(bank->target, bank->base + FCF_ADDRESS, 4, FCF_SIZE / 4, fcf_current);
2090  if (result == ERROR_OK && memcmp(fcf_current, fcf_buffer, FCF_SIZE) == 0)
2091  set_fcf = false;
2092 
2093  if (set_fcf) {
2094  /* write FCF if differs from flash - eliminate multiple writes */
2095  result = kinetis_write_inner(bank, fcf_buffer, FCF_ADDRESS, FCF_SIZE);
2096  if (result != ERROR_OK)
2097  return result;
2098  }
2099 
2100  LOG_WARNING("Flash Configuration Field written.");
2101  LOG_WARNING("Reset or power off the device to make settings effective.");
2102 
2103  if (offset + count > FCF_ADDRESS + FCF_SIZE) {
2104  uint32_t delta = FCF_ADDRESS + FCF_SIZE - offset;
2105  /* write part after FCF */
2106  result = kinetis_write_inner(bank, buffer + delta, FCF_ADDRESS + FCF_SIZE, count - delta);
2107  }
2108  return result;
2109 
2110  } else {
2111  /* no FCF fiddling, normal write */
2113  }
2114 }
2115 
2116 
2117 static int kinetis_probe_chip_s32k(struct kinetis_chip *k_chip)
2118 {
2119  int result;
2120  uint8_t fcfg1_eesize, fcfg1_depart;
2121  uint32_t ee_size = 0;
2122  uint32_t pflash_size_k, nvm_size_k, dflash_size_k;
2123  unsigned int generation = 0, subseries = 0, derivate = 0;
2124 
2125  struct target *target = k_chip->target;
2126  k_chip->probed = false;
2127  k_chip->pflash_sector_size = 0;
2128  k_chip->pflash_base = 0;
2129  k_chip->nvm_base = 0x10000000;
2130  k_chip->progr_accel_ram = FLEXRAM;
2131  k_chip->flash_support = FS_PROGRAM_PHRASE | FS_PROGRAM_SECTOR;
2132  k_chip->watchdog_type = KINETIS_WDOG32_KE1X;
2133  k_chip->sysmodectrlr_type = KINETIS_SMC32;
2134 
2135  if (k_chip->sim_base == 0)
2136  k_chip->sim_base = SIM_BASE;
2137 
2138  result = target_read_u32(target, k_chip->sim_base + SIM_SDID_OFFSET, &k_chip->sim_sdid);
2139  if (result != ERROR_OK)
2140  return result;
2141 
2142  generation = (k_chip->sim_sdid) >> 28 & 0x0f;
2143  subseries = (k_chip->sim_sdid) >> 24 & 0x0f;
2144  derivate = (k_chip->sim_sdid) >> 20 & 0x0f;
2145 
2146  switch (k_chip->sim_sdid & KINETIS_SDID_S32K_SERIES_MASK) {
2148  k_chip->cache_type = KINETIS_CACHE_L;
2149  k_chip->num_pflash_blocks = 1;
2150  k_chip->num_nvm_blocks = 1;
2151  /* Non-interleaved */
2152  k_chip->max_flash_prog_size = 512;
2153 
2154  switch (k_chip->sim_sdid & KINETIS_SDID_S32K_DERIVATE_MASK) {
2156  /* S32K116 CPU 48Mhz Flash 128KB RAM 17KB+2KB */
2157  /* Non-Interleaved */
2158  k_chip->pflash_size = 128 << 10;
2159  k_chip->pflash_sector_size = 2 << 10;
2160  /* Non-Interleaved */
2161  k_chip->nvm_size = 32 << 10;
2162  k_chip->nvm_sector_size = 2 << 10;
2163  break;
2165  /* S32K118 CPU 80Mhz Flash 256KB+32KB RAM 32KB+4KB */
2166  /* Non-Interleaved */
2167  k_chip->pflash_size = 256 << 10;
2168  k_chip->pflash_sector_size = 2 << 10;
2169  /* Non-Interleaved */
2170  k_chip->nvm_size = 32 << 10;
2171  k_chip->nvm_sector_size = 2 << 10;
2172  break;
2173  }
2174  break;
2175 
2177  k_chip->cache_type = KINETIS_CACHE_MSCM2;
2178  k_chip->num_pflash_blocks = 1;
2179  k_chip->num_nvm_blocks = 1;
2180  /* Non-interleaved */
2181  k_chip->max_flash_prog_size = 512;
2182  switch (k_chip->sim_sdid & KINETIS_SDID_S32K_DERIVATE_MASK) {
2185  /* S32K142/S32K142W CPU 80Mhz Flash 256KB+64KB RAM 32KB+4KB */
2186  /* Non-Interleaved */
2187  k_chip->pflash_size = 256 << 10;
2188  k_chip->pflash_sector_size = 2 << 10;
2189  /* Non-Interleaved */
2190  k_chip->nvm_size = 64 << 10;
2191  k_chip->nvm_sector_size = 2 << 10;
2192  break;
2195  /* S32K144/S32K144W CPU 80Mhz Flash 512KB+64KB RAM 64KB+4KB */
2196  /* Interleaved */
2197  k_chip->pflash_size = 512 << 10;
2198  k_chip->pflash_sector_size = 4 << 10;
2199  /* Non-Interleaved */
2200  k_chip->nvm_size = 64 << 10;
2201  k_chip->nvm_sector_size = 2 << 10;
2202  break;
2204  /* S32K146 CPU 80Mhz Flash 1024KB+64KB RAM 128KB+4KB */
2205  /* Interleaved */
2206  k_chip->pflash_size = 1024 << 10;
2207  k_chip->pflash_sector_size = 4 << 10;
2208  k_chip->num_pflash_blocks = 2;
2209  /* Non-Interleaved */
2210  k_chip->nvm_size = 64 << 10;
2211  k_chip->nvm_sector_size = 2 << 10;
2212  break;
2214  /* S32K148 CPU 80Mhz Flash 1536KB+512KB RAM 256KB+4KB */
2215  /* Interleaved */
2216  k_chip->pflash_size = 1536 << 10;
2217  k_chip->pflash_sector_size = 4 << 10;
2218  k_chip->num_pflash_blocks = 3;
2219  /* Interleaved */
2220  k_chip->nvm_size = 512 << 10;
2221  k_chip->nvm_sector_size = 4 << 10;
2222  /* Interleaved */
2223  k_chip->max_flash_prog_size = 1 << 10;
2224  break;
2225  }
2226  break;
2227 
2228  default:
2229  LOG_ERROR("Unsupported S32K1xx-series");
2230  }
2231 
2232  if (k_chip->pflash_sector_size == 0) {
2233  LOG_ERROR("MCU is unsupported, SDID 0x%08" PRIx32, k_chip->sim_sdid);
2235  }
2236 
2237  result = target_read_u32(target, k_chip->sim_base + SIM_FCFG1_OFFSET, &k_chip->sim_fcfg1);
2238  if (result != ERROR_OK)
2239  return result;
2240  k_chip->sim_fcfg2 = 0; /* S32K1xx does not implement FCFG2 register. */
2241 
2242  fcfg1_depart = (k_chip->sim_fcfg1 >> 12) & 0x0f;
2243  fcfg1_eesize = (k_chip->sim_fcfg1 >> 16) & 0x0f;
2244  if (fcfg1_eesize <= 9)
2245  ee_size = (16 << (10 - fcfg1_eesize));
2246  if (fcfg1_depart == 0xf) {
2247  /* Default value. On most S32K1xx, this means FlexNVM is not partitioned. But on S32K148, this
2248  can be valid (and mean 512KB DFlash / 0KB EEPROM backup) _if_ a PGMPART has been previously
2249  launched. We can't distinguish this, so there we treat it as a valid value. */
2250  if (generation == 1 && subseries == 4 && derivate == 8) {
2251  LOG_WARNING("DEPART 0b1111 on S32K148: assuming 512 KiB DFlash. This is only valid if a PGMPART"
2252  " has been previously launched. Use DEPART 0b0000 to avoid this warning.");
2253  k_chip->dflash_size = k_chip->nvm_size;
2254  } else {
2255  k_chip->dflash_size = 0;
2256  }
2257  } else if ((fcfg1_depart & 0x8) == 0) {
2258  /* Binary 0xxx values encode the amount reserved for EEPROM emulation. */
2259  if (fcfg1_depart)
2260  k_chip->dflash_size = k_chip->nvm_size - (4096 << fcfg1_depart);
2261  else
2262  k_chip->dflash_size = k_chip->nvm_size;
2263  } else {
2264  /* Binary 1xxx valued encode the DFlash size. */
2265  if (fcfg1_depart & 0x7)
2266  k_chip->dflash_size = 4096 << (fcfg1_depart & 0x7);
2267  else
2268  k_chip->dflash_size = 0;
2269  }
2270 
2271  snprintf(k_chip->name, sizeof(k_chip->name), "S32K%u%u%u",
2272  generation, subseries, derivate);
2273 
2274  pflash_size_k = k_chip->pflash_size / 1024;
2275  dflash_size_k = k_chip->dflash_size / 1024;
2276 
2277  LOG_INFO("%s detected: %u flash blocks", k_chip->name, k_chip->num_pflash_blocks + k_chip->num_nvm_blocks);
2278  LOG_INFO("%u PFlash banks: %" PRIu32 " KiB total", k_chip->num_pflash_blocks, pflash_size_k);
2279 
2280  nvm_size_k = k_chip->nvm_size / 1024;
2281 
2282  if (k_chip->num_nvm_blocks) {
2283  LOG_INFO("%u FlexNVM banks: %" PRIu32 " KiB total, %" PRIu32 " KiB available as data flash, %"
2284  PRIu32 " bytes FlexRAM",
2285  k_chip->num_nvm_blocks, nvm_size_k, dflash_size_k, ee_size);
2286  }
2287 
2288  k_chip->probed = true;
2289 
2290  if (create_banks)
2292 
2293  return ERROR_OK;
2294 }
2295 
2296 
2297 static int kinetis_probe_chip(struct kinetis_chip *k_chip)
2298 {
2299  int result;
2300  uint8_t fcfg1_nvmsize, fcfg1_pfsize, fcfg1_eesize, fcfg1_depart;
2301  uint8_t fcfg2_pflsh;
2302  uint32_t ee_size = 0;
2303  uint32_t pflash_size_k, nvm_size_k, dflash_size_k;
2304  uint32_t pflash_size_m;
2305  unsigned int num_blocks = 0;
2306  unsigned int maxaddr_shift = 13;
2307  struct target *target = k_chip->target;
2308 
2309  unsigned int familyid = 0, subfamid = 0;
2310  unsigned int cpu_mhz = 120;
2311  bool use_nvm_marking = false;
2312  char flash_marking[12], nvm_marking[2];
2313  char name[40];
2314 
2315  k_chip->probed = false;
2316  k_chip->pflash_sector_size = 0;
2317  k_chip->pflash_base = 0;
2318  k_chip->nvm_base = 0x10000000;
2319  k_chip->progr_accel_ram = FLEXRAM;
2320 
2321  name[0] = '\0';
2322 
2323  if (k_chip->sim_base)
2324  result = target_read_u32(target, k_chip->sim_base + SIM_SDID_OFFSET, &k_chip->sim_sdid);
2325  else {
2326  result = target_read_u32(target, SIM_BASE + SIM_SDID_OFFSET, &k_chip->sim_sdid);
2327  if (result == ERROR_OK)
2328  k_chip->sim_base = SIM_BASE;
2329  else {
2331  if (result == ERROR_OK)
2332  k_chip->sim_base = SIM_BASE_KL28;
2333  }
2334  }
2335  if (result != ERROR_OK)
2336  return result;
2337 
2338  if ((k_chip->sim_sdid & (~KINETIS_SDID_K_SERIES_MASK)) == 0) {
2339  /* older K-series MCU */
2340  uint32_t mcu_type = k_chip->sim_sdid & KINETIS_K_SDID_TYPE_MASK;
2341  k_chip->cache_type = KINETIS_CACHE_K;
2342  k_chip->watchdog_type = KINETIS_WDOG_K;
2343 
2344  switch (mcu_type) {
2347  /* 1kB sectors */
2348  k_chip->pflash_sector_size = 1<<10;
2349  k_chip->nvm_sector_size = 1<<10;
2350  num_blocks = 2;
2351  k_chip->flash_support = FS_PROGRAM_LONGWORD | FS_PROGRAM_SECTOR;
2352  break;
2360  /* 2kB sectors, 1kB FlexNVM sectors */
2361  k_chip->pflash_sector_size = 2<<10;
2362  k_chip->nvm_sector_size = 1<<10;
2363  num_blocks = 2;
2364  k_chip->flash_support = FS_PROGRAM_LONGWORD | FS_PROGRAM_SECTOR;
2365  k_chip->max_flash_prog_size = 1<<10;
2366  break;
2369  case KINETIS_K_SDID_K11:
2370  case KINETIS_K_SDID_K12:
2374  case KINETIS_K_SDID_K53:
2376  /* 2kB sectors */
2377  k_chip->pflash_sector_size = 2<<10;
2378  k_chip->nvm_sector_size = 2<<10;
2379  num_blocks = 2;
2380  k_chip->flash_support = FS_PROGRAM_LONGWORD | FS_PROGRAM_SECTOR;
2381  break;
2384  /* 4kB sectors (MK21FN1M0, MK21FX512, MK22FN1M0, MK22FX512) */
2385  k_chip->pflash_sector_size = 4<<10;
2386  k_chip->max_flash_prog_size = 1<<10;
2387  k_chip->nvm_sector_size = 4<<10;
2388  num_blocks = 2;
2389  k_chip->flash_support = FS_PROGRAM_PHRASE | FS_PROGRAM_SECTOR;
2390  break;
2395  /* 4kB sectors */
2396  k_chip->pflash_sector_size = 4<<10;
2397  k_chip->nvm_sector_size = 4<<10;
2398  num_blocks = 4;
2399  k_chip->flash_support = FS_PROGRAM_PHRASE | FS_PROGRAM_SECTOR;
2400  break;
2401  default:
2402  LOG_ERROR("Unsupported K-family FAMID");
2403  }
2404 
2405  for (size_t idx = 0; idx < ARRAY_SIZE(kinetis_types_old); idx++) {
2406  if (kinetis_types_old[idx].sdid == mcu_type) {
2407  strcpy(name, kinetis_types_old[idx].name);
2408  use_nvm_marking = true;
2409  break;
2410  }
2411  }
2412 
2413  /* first revision of some devices has no SMC */
2414  switch (mcu_type) {
2420  {
2421  uint32_t revid = (k_chip->sim_sdid & KINETIS_K_REVID_MASK) >> KINETIS_K_REVID_SHIFT;
2422  /* highest bit set corresponds to rev 2.x */
2423  if (revid <= 7) {
2424  k_chip->sysmodectrlr_type = KINETIS_MC;
2425  strcat(name, " Rev 1.x");
2426  }
2427  }
2428  break;
2429  }
2430 
2431  } else {
2432  /* Newer K-series or KL series MCU */
2435 
2436  switch (k_chip->sim_sdid & KINETIS_SDID_SERIESID_MASK) {
2438  use_nvm_marking = true;
2439  k_chip->cache_type = KINETIS_CACHE_K;
2440  k_chip->watchdog_type = KINETIS_WDOG_K;
2441 
2444  /* K02FN64, K02FN128: FTFA, 2kB sectors */
2445  k_chip->pflash_sector_size = 2<<10;
2446  num_blocks = 1;
2447  k_chip->flash_support = FS_PROGRAM_LONGWORD;
2448  cpu_mhz = 100;
2449  break;
2450 
2452  /* MK24FN1M reports as K22, this should detect it (according to errata note 1N83J) */
2453  uint32_t sopt1;
2454  result = target_read_u32(target, k_chip->sim_base + SIM_SOPT1_OFFSET, &sopt1);
2455  if (result != ERROR_OK)
2456  return result;
2457 
2460  /* MK24FN1M */
2461  k_chip->pflash_sector_size = 4<<10;
2462  num_blocks = 2;
2463  k_chip->flash_support = FS_PROGRAM_PHRASE | FS_PROGRAM_SECTOR;
2464  k_chip->max_flash_prog_size = 1<<10;
2465  subfamid = 4; /* errata 1N83J fix */
2466  break;
2467  }
2471  /* K22 with new-style SDID - smaller pflash with FTFA, 2kB sectors */
2472  k_chip->pflash_sector_size = 2<<10;
2473  /* autodetect 1 or 2 blocks */
2474  k_chip->flash_support = FS_PROGRAM_LONGWORD;
2475  break;
2476  }
2477  LOG_ERROR("Unsupported Kinetis K22 DIEID");
2478  break;
2479  }
2481  k_chip->pflash_sector_size = 4<<10;
2483  /* K24FN256 - smaller pflash with FTFA */
2484  num_blocks = 1;
2485  k_chip->flash_support = FS_PROGRAM_LONGWORD;
2486  break;
2487  }
2488  /* K24FN1M without errata 7534 */
2489  num_blocks = 2;
2490  k_chip->flash_support = FS_PROGRAM_PHRASE | FS_PROGRAM_SECTOR;
2491  k_chip->max_flash_prog_size = 1<<10;
2492  break;
2493 
2494  case KINETIS_SDID_FAMILYID_K6X | KINETIS_SDID_SUBFAMID_KX1: /* errata 7534 - should be K63 */
2495  case KINETIS_SDID_FAMILYID_K6X | KINETIS_SDID_SUBFAMID_KX2: /* errata 7534 - should be K64 */
2496  subfamid += 2; /* errata 7534 fix */
2497  /* fallthrough */
2499  /* K63FN1M0 */
2501  /* K64FN1M0, K64FX512 */
2502  k_chip->pflash_sector_size = 4<<10;
2503  k_chip->nvm_sector_size = 4<<10;
2504  k_chip->max_flash_prog_size = 1<<10;
2505  num_blocks = 2;
2506  k_chip->flash_support = FS_PROGRAM_PHRASE | FS_PROGRAM_SECTOR;
2507  break;
2508 
2510  /* K26FN2M0 */
2512  /* K66FN2M0, K66FX1M0 */
2513  k_chip->pflash_sector_size = 4<<10;
2514  k_chip->nvm_sector_size = 4<<10;
2515  k_chip->max_flash_prog_size = 1<<10;
2516  num_blocks = 4;
2517  k_chip->flash_support = FS_PROGRAM_PHRASE | FS_PROGRAM_SECTOR | FS_ECC;
2518  cpu_mhz = 180;
2519  break;
2520 
2522  /* K27FN2M0 */
2524  /* K28FN2M0 */
2525  k_chip->pflash_sector_size = 4<<10;
2526  k_chip->max_flash_prog_size = 1<<10;
2527  num_blocks = 4;
2528  k_chip->flash_support = FS_PROGRAM_PHRASE | FS_PROGRAM_SECTOR | FS_ECC;
2529  cpu_mhz = 150;
2530  break;
2531 
2535  /* K80FN256, K81FN256, K82FN256 */
2536  k_chip->pflash_sector_size = 4<<10;
2537  num_blocks = 1;
2538  k_chip->flash_support = FS_PROGRAM_LONGWORD | FS_NO_CMD_BLOCKSTAT;
2539  cpu_mhz = 150;
2540  break;
2541 
2544  /* KL81Z128, KL82Z128 */
2545  k_chip->pflash_sector_size = 2<<10;
2546  num_blocks = 1;
2547  k_chip->flash_support = FS_PROGRAM_LONGWORD | FS_NO_CMD_BLOCKSTAT;
2548  k_chip->cache_type = KINETIS_CACHE_L;
2549 
2550  use_nvm_marking = false;
2551  snprintf(name, sizeof(name), "MKL8%uZ%%s7",
2552  subfamid);
2553  break;
2554 
2555  default:
2556  LOG_ERROR("Unsupported Kinetis FAMILYID SUBFAMID");
2557  }
2558 
2559  if (name[0] == '\0')
2560  snprintf(name, sizeof(name), "MK%u%uF%%s%u",
2561  familyid, subfamid, cpu_mhz / 10);
2562  break;
2563 
2565  /* KL-series */
2566  k_chip->pflash_sector_size = 1<<10;
2567  k_chip->nvm_sector_size = 1<<10;
2568  /* autodetect 1 or 2 blocks */
2569  k_chip->flash_support = FS_PROGRAM_LONGWORD;
2570  k_chip->cache_type = KINETIS_CACHE_L;
2571  k_chip->watchdog_type = KINETIS_WDOG_COP;
2572 
2573  cpu_mhz = 48;
2577  subfamid = 7;
2578  break;
2579 
2581  cpu_mhz = 72;
2582  k_chip->pflash_sector_size = 2<<10;
2583  num_blocks = 2;
2584  k_chip->watchdog_type = KINETIS_WDOG32_KL28;
2585  k_chip->sysmodectrlr_type = KINETIS_SMC32;
2586  break;
2587  }
2588 
2589  snprintf(name, sizeof(name), "MKL%u%uZ%%s%u",
2590  familyid, subfamid, cpu_mhz / 10);
2591  break;
2592 
2594  /* Newer KW-series (all KW series except KW2xD, KW01Z) */
2595  cpu_mhz = 48;
2598  /* KW40Z */
2600  /* KW30Z */
2602  /* KW20Z */
2603  /* FTFA, 1kB sectors */
2604  k_chip->pflash_sector_size = 1<<10;
2605  k_chip->nvm_sector_size = 1<<10;
2606  /* autodetect 1 or 2 blocks */
2607  k_chip->flash_support = FS_PROGRAM_LONGWORD;
2608  k_chip->cache_type = KINETIS_CACHE_L;
2609  k_chip->watchdog_type = KINETIS_WDOG_COP;
2610  break;
2612  /* KW41Z */
2614  /* KW31Z */
2616  /* KW21Z */
2617  /* FTFA, 2kB sectors */
2618  k_chip->pflash_sector_size = 2<<10;
2619  k_chip->nvm_sector_size = 2<<10;
2620  /* autodetect 1 or 2 blocks */
2621  k_chip->flash_support = FS_PROGRAM_LONGWORD;
2622  k_chip->cache_type = KINETIS_CACHE_L;
2623  k_chip->watchdog_type = KINETIS_WDOG_COP;
2624  break;
2625  default:
2626  LOG_ERROR("Unsupported KW FAMILYID SUBFAMID");
2627  }
2628  snprintf(name, sizeof(name), "MKW%u%uZ%%s%u",
2629  familyid, subfamid, cpu_mhz / 10);
2630  break;
2631 
2633  /* KV-series */
2634  k_chip->watchdog_type = KINETIS_WDOG_K;
2637  /* KV10: FTFA, 1kB sectors */
2638  k_chip->pflash_sector_size = 1<<10;
2639  num_blocks = 1;
2640  k_chip->flash_support = FS_PROGRAM_LONGWORD;
2641  k_chip->cache_type = KINETIS_CACHE_L;
2642  strcpy(name, "MKV10Z%s7");
2643  break;
2644 
2646  /* KV11: FTFA, 2kB sectors */
2647  k_chip->pflash_sector_size = 2<<10;
2648  num_blocks = 1;
2649  k_chip->flash_support = FS_PROGRAM_LONGWORD;
2650  k_chip->cache_type = KINETIS_CACHE_L;
2651  strcpy(name, "MKV11Z%s7");
2652  break;
2653 
2655  /* KV30: FTFA, 2kB sectors, 1 block */
2657  /* KV31: FTFA, 2kB sectors, 2 blocks */
2658  k_chip->pflash_sector_size = 2<<10;
2659  /* autodetect 1 or 2 blocks */
2660  k_chip->flash_support = FS_PROGRAM_LONGWORD;
2661  k_chip->cache_type = KINETIS_CACHE_K;
2662  break;
2663 
2667  /* KV4x: FTFA, 4kB sectors */
2668  k_chip->pflash_sector_size = 4<<10;
2669  num_blocks = 1;
2670  k_chip->flash_support = FS_PROGRAM_LONGWORD;
2671  k_chip->cache_type = KINETIS_CACHE_K;
2672  cpu_mhz = 168;
2673  break;
2674 
2677  /* KV5x: FTFE, 8kB sectors */
2678  k_chip->pflash_sector_size = 8<<10;
2679  k_chip->max_flash_prog_size = 1<<10;
2680  num_blocks = 1;
2681  maxaddr_shift = 14;
2682  k_chip->flash_support = FS_PROGRAM_PHRASE | FS_PROGRAM_SECTOR | FS_WIDTH_256BIT | FS_ECC;
2683  k_chip->pflash_base = 0x10000000;
2684  k_chip->progr_accel_ram = 0x18000000;
2685  cpu_mhz = 240;
2686  break;
2687 
2688  default:
2689  LOG_ERROR("Unsupported KV FAMILYID SUBFAMID");
2690  }
2691 
2692  if (name[0] == '\0')
2693  snprintf(name, sizeof(name), "MKV%u%uF%%s%u",
2694  familyid, subfamid, cpu_mhz / 10);
2695  break;
2696 
2698  /* KE1x-series */
2699  k_chip->watchdog_type = KINETIS_WDOG32_KE1X;
2700  switch (k_chip->sim_sdid &
2704  /* KE1xZ: FTFE, 2kB sectors */
2705  k_chip->pflash_sector_size = 2<<10;
2706  k_chip->nvm_sector_size = 2<<10;
2707  k_chip->max_flash_prog_size = 1<<9;
2708  num_blocks = 2;
2709  k_chip->flash_support = FS_PROGRAM_PHRASE | FS_PROGRAM_SECTOR;
2710  k_chip->cache_type = KINETIS_CACHE_L;
2711 
2712  cpu_mhz = 72;
2713  snprintf(name, sizeof(name), "MKE%u%uZ%%s%u",
2714  familyid, subfamid, cpu_mhz / 10);
2715  break;
2716 
2720  /* KE1xF: FTFE, 4kB sectors */
2721  k_chip->pflash_sector_size = 4<<10;
2722  k_chip->nvm_sector_size = 2<<10;
2723  k_chip->max_flash_prog_size = 1<<10;
2724  num_blocks = 2;
2725  k_chip->flash_support = FS_PROGRAM_PHRASE | FS_PROGRAM_SECTOR;
2726  k_chip->cache_type = KINETIS_CACHE_MSCM;
2727 
2728  cpu_mhz = 168;
2729  snprintf(name, sizeof(name), "MKE%u%uF%%s%u",
2730  familyid, subfamid, cpu_mhz / 10);
2731  break;
2732 
2733  default:
2734  LOG_ERROR("Unsupported KE FAMILYID SUBFAMID");
2735  }
2736  break;
2737 
2738  default:
2739  LOG_ERROR("Unsupported K-series");
2740  }
2741  }
2742 
2743  if (k_chip->pflash_sector_size == 0) {
2744  LOG_ERROR("MCU is unsupported, SDID 0x%08" PRIx32, k_chip->sim_sdid);
2746  }
2747 
2748  result = target_read_u32(target, k_chip->sim_base + SIM_FCFG1_OFFSET, &k_chip->sim_fcfg1);
2749  if (result != ERROR_OK)
2750  return result;
2751 
2752  result = target_read_u32(target, k_chip->sim_base + SIM_FCFG2_OFFSET, &k_chip->sim_fcfg2);
2753  if (result != ERROR_OK)
2754  return result;
2755 
2756  LOG_DEBUG("SDID: 0x%08" PRIX32 " FCFG1: 0x%08" PRIX32 " FCFG2: 0x%08" PRIX32, k_chip->sim_sdid,
2757  k_chip->sim_fcfg1, k_chip->sim_fcfg2);
2758 
2759  fcfg1_nvmsize = (uint8_t)((k_chip->sim_fcfg1 >> 28) & 0x0f);
2760  fcfg1_pfsize = (uint8_t)((k_chip->sim_fcfg1 >> 24) & 0x0f);
2761  fcfg1_eesize = (uint8_t)((k_chip->sim_fcfg1 >> 16) & 0x0f);
2762  fcfg1_depart = (uint8_t)((k_chip->sim_fcfg1 >> 8) & 0x0f);
2763 
2764  fcfg2_pflsh = (uint8_t)((k_chip->sim_fcfg2 >> 23) & 0x01);
2765  k_chip->fcfg2_maxaddr0_shifted = ((k_chip->sim_fcfg2 >> 24) & 0x7f) << maxaddr_shift;
2766  k_chip->fcfg2_maxaddr1_shifted = ((k_chip->sim_fcfg2 >> 16) & 0x7f) << maxaddr_shift;
2767 
2768  if (num_blocks == 0)
2769  num_blocks = k_chip->fcfg2_maxaddr1_shifted ? 2 : 1;
2770  else if (k_chip->fcfg2_maxaddr1_shifted == 0 && num_blocks >= 2 && fcfg2_pflsh) {
2771  /* fcfg2_maxaddr1 may be zero due to partitioning whole NVM as EEPROM backup
2772  * Do not adjust block count in this case! */
2773  num_blocks = 1;
2774  LOG_WARNING("MAXADDR1 is zero, number of flash banks adjusted to 1");
2775  } else if (k_chip->fcfg2_maxaddr1_shifted != 0 && num_blocks == 1) {
2776  num_blocks = 2;
2777  LOG_WARNING("MAXADDR1 is non zero, number of flash banks adjusted to 2");
2778  }
2779 
2780  /* when the PFLSH bit is set, there is no FlexNVM/FlexRAM */
2781  if (!fcfg2_pflsh) {
2782  switch (fcfg1_nvmsize) {
2783  case 0x03:
2784  case 0x05:
2785  case 0x07:
2786  case 0x09:
2787  case 0x0b:
2788  k_chip->nvm_size = 1 << (14 + (fcfg1_nvmsize >> 1));
2789  break;
2790  case 0x0f:
2791  if (k_chip->pflash_sector_size >= 4<<10)
2792  k_chip->nvm_size = 512<<10;
2793  else
2794  /* K20_100 */
2795  k_chip->nvm_size = 256<<10;
2796  break;
2797  default:
2798  k_chip->nvm_size = 0;
2799  break;
2800  }
2801 
2802  switch (fcfg1_eesize) {
2803  case 0x00:
2804  case 0x01:
2805  case 0x02:
2806  case 0x03:
2807  case 0x04:
2808  case 0x05:
2809  case 0x06:
2810  case 0x07:
2811  case 0x08:
2812  case 0x09:
2813  ee_size = (16 << (10 - fcfg1_eesize));
2814  break;
2815  default:
2816  ee_size = 0;
2817  break;
2818  }
2819 
2820  switch (fcfg1_depart) {
2821  case 0x01:
2822  case 0x02:
2823  case 0x03:
2824  case 0x04:
2825  case 0x05:
2826  case 0x06:
2827  k_chip->dflash_size = k_chip->nvm_size - (4096 << fcfg1_depart);
2828  break;
2829  case 0x07:
2830  case 0x08:
2831  k_chip->dflash_size = 0;
2832  break;
2833  case 0x09:
2834  case 0x0a:
2835  case 0x0b:
2836  case 0x0c:
2837  case 0x0d:
2838  k_chip->dflash_size = 4096 << (fcfg1_depart & 0x7);
2839  break;
2840  default:
2841  k_chip->dflash_size = k_chip->nvm_size;
2842  break;
2843  }
2844  }
2845 
2846  switch (fcfg1_pfsize) {
2847  case 0x00:
2848  k_chip->pflash_size = 8192;
2849  break;
2850  case 0x01:
2851  case 0x03:
2852  case 0x05:
2853  case 0x07:
2854  case 0x09:
2855  case 0x0b:
2856  case 0x0d:
2857  k_chip->pflash_size = 1 << (14 + (fcfg1_pfsize >> 1));
2858  break;
2859  case 0x0f:
2860  /* a peculiar case: Freescale states different sizes for 0xf
2861  * KL03P24M48SF0RM 32 KB .... duplicate of code 0x3
2862  * K02P64M100SFARM 128 KB ... duplicate of code 0x7
2863  * K22P121M120SF8RM 256 KB ... duplicate of code 0x9
2864  * K22P121M120SF7RM 512 KB ... duplicate of code 0xb
2865  * K22P100M120SF5RM 1024 KB ... duplicate of code 0xd
2866  * K26P169M180SF5RM 2048 KB ... the only unique value
2867  * fcfg2_maxaddr0 seems to be the only clue to pflash_size
2868  * Checking fcfg2_maxaddr0 in bank probe is pointless then
2869  */
2870  if (fcfg2_pflsh)
2871  k_chip->pflash_size = k_chip->fcfg2_maxaddr0_shifted * num_blocks;
2872  else
2873  k_chip->pflash_size = k_chip->fcfg2_maxaddr0_shifted * num_blocks / 2;
2874  if (k_chip->pflash_size != 2048<<10)
2875  LOG_WARNING("SIM_FCFG1 PFSIZE = 0xf: please check if pflash is %" PRIu32 " KB", k_chip->pflash_size>>10);
2876 
2877  break;
2878  default:
2879  k_chip->pflash_size = 0;
2880  break;
2881  }
2882 
2883  if (k_chip->flash_support & FS_PROGRAM_SECTOR && k_chip->max_flash_prog_size == 0) {
2884  k_chip->max_flash_prog_size = k_chip->pflash_sector_size;
2885  /* Program section size is equal to sector size by default */
2886  }
2887 
2888  if (fcfg2_pflsh) {
2889  k_chip->num_pflash_blocks = num_blocks;
2890  k_chip->num_nvm_blocks = 0;
2891  } else {
2892  k_chip->num_pflash_blocks = (num_blocks + 1) / 2;
2893  k_chip->num_nvm_blocks = num_blocks - k_chip->num_pflash_blocks;
2894  }
2895 
2896  if (use_nvm_marking) {
2897  nvm_marking[0] = k_chip->num_nvm_blocks ? 'X' : 'N';
2898  nvm_marking[1] = '\0';
2899  } else
2900  nvm_marking[0] = '\0';
2901 
2902  pflash_size_k = k_chip->pflash_size / 1024;
2903  pflash_size_m = pflash_size_k / 1024;
2904  if (pflash_size_m)
2905  snprintf(flash_marking, sizeof(flash_marking), "%s%" PRIu32 "M0xxx", nvm_marking, pflash_size_m);
2906  else
2907  snprintf(flash_marking, sizeof(flash_marking), "%s%" PRIu32 "xxx", nvm_marking, pflash_size_k);
2908 
2909  snprintf(k_chip->name, sizeof(k_chip->name), name, flash_marking);
2910  LOG_INFO("Kinetis %s detected: %u flash blocks", k_chip->name, num_blocks);
2911  LOG_INFO("%u PFlash banks: %" PRIu32 " KiB total", k_chip->num_pflash_blocks, pflash_size_k);
2912  if (k_chip->num_nvm_blocks) {
2913  nvm_size_k = k_chip->nvm_size / 1024;
2914  dflash_size_k = k_chip->dflash_size / 1024;
2915  LOG_INFO("%u FlexNVM banks: %" PRIu32 " KiB total, %" PRIu32 " KiB available as data flash, %"
2916  PRIu32 " bytes FlexRAM", k_chip->num_nvm_blocks, nvm_size_k, dflash_size_k, ee_size);
2917  }
2918 
2919  k_chip->probed = true;
2920 
2921  if (create_banks)
2923 
2924  return ERROR_OK;
2925 }
2926 
2927 static int kinetis_probe(struct flash_bank *bank)
2928 {
2929  int result;
2930  uint8_t fcfg2_maxaddr0, fcfg2_pflsh, fcfg2_maxaddr1;
2931  unsigned int num_blocks, first_nvm_bank;
2932  uint32_t size_k;
2933  struct kinetis_flash_bank *k_bank = bank->driver_priv;
2934  struct kinetis_chip *k_chip;
2935 
2936  assert(k_bank);
2937  k_chip = k_bank->k_chip;
2938 
2939  k_bank->probed = false;
2940 
2941  if (!k_chip->probed) {
2942  switch (k_chip->chip_type) {
2943  case KINETIS_CT_S32K:
2944  result = kinetis_probe_chip_s32k(k_chip);
2945  break;
2946  default:
2947  result = kinetis_probe_chip(k_chip);
2948  }
2949  if (result != ERROR_OK)
2950  return result;
2951  }
2952 
2953  num_blocks = k_chip->num_pflash_blocks + k_chip->num_nvm_blocks;
2954  first_nvm_bank = k_chip->num_pflash_blocks;
2955 
2956  if (k_bank->bank_number < k_chip->num_pflash_blocks) {
2957  /* pflash, banks start at address zero */
2958  k_bank->flash_class = FC_PFLASH;
2959  bank->size = (k_chip->pflash_size / k_chip->num_pflash_blocks);
2960  bank->base = k_chip->pflash_base + bank->size * k_bank->bank_number;
2961  k_bank->prog_base = 0x00000000 + bank->size * k_bank->bank_number;
2962  k_bank->sector_size = k_chip->pflash_sector_size;
2963  /* pflash is divided into 32 protection areas for
2964  * parts with more than 32K of PFlash. For parts with
2965  * less the protection unit is set to 1024 bytes */
2966  k_bank->protection_size = MAX(k_chip->pflash_size / 32, 1024);
2967  bank->num_prot_blocks = bank->size / k_bank->protection_size;
2968  k_bank->protection_block = bank->num_prot_blocks * k_bank->bank_number;
2969 
2970  size_k = bank->size / 1024;
2971  LOG_DEBUG("Kinetis bank %u: %" PRIu32 "k PFlash, FTFx base 0x%08" PRIx32 ", sect %" PRIu32,
2972  k_bank->bank_number, size_k, k_bank->prog_base, k_bank->sector_size);
2973 
2974  } else if (k_bank->bank_number < num_blocks) {
2975  /* nvm, banks start at address 0x10000000 */
2976  unsigned int nvm_ord = k_bank->bank_number - first_nvm_bank;
2977  uint32_t limit;
2978 
2979  k_bank->flash_class = FC_FLEX_NVM;
2980  bank->size = k_chip->nvm_size / k_chip->num_nvm_blocks;
2981  bank->base = k_chip->nvm_base + bank->size * nvm_ord;
2982  k_bank->prog_base = 0x00800000 + bank->size * nvm_ord;
2983  k_bank->sector_size = k_chip->nvm_sector_size;
2984  if (k_chip->dflash_size == 0) {
2985  k_bank->protection_size = 0;
2986  } else {
2987  int i;
2988  for (i = k_chip->dflash_size; ~i & 1; i >>= 1)
2989  ;
2990  if (i == 1)
2991  k_bank->protection_size = k_chip->dflash_size / 8; /* data flash size = 2^^n */
2992  else
2993  k_bank->protection_size = k_chip->nvm_size / 8; /* TODO: verify on SF1, not documented in RM */
2994  }
2995  bank->num_prot_blocks = 8 / k_chip->num_nvm_blocks;
2996  k_bank->protection_block = bank->num_prot_blocks * nvm_ord;
2997 
2998  /* EEPROM backup part of FlexNVM is not accessible, use dflash_size as a limit */
2999  if (k_chip->dflash_size > bank->size * nvm_ord)
3000  limit = k_chip->dflash_size - bank->size * nvm_ord;
3001  else
3002  limit = 0;
3003 
3004  if (bank->size > limit) {
3005  bank->size = limit;
3006  LOG_DEBUG("FlexNVM bank %u limited to 0x%08" PRIx32 " due to active EEPROM backup",
3007  k_bank->bank_number, limit);
3008  }
3009 
3010  size_k = bank->size / 1024;
3011  LOG_DEBUG("Kinetis bank %u: %" PRIu32 "k FlexNVM, FTFx base 0x%08" PRIx32 ", sect %" PRIu32,
3012  k_bank->bank_number, size_k, k_bank->prog_base, k_bank->sector_size);
3013 
3014  } else {
3015  LOG_ERROR("Cannot determine parameters for bank %u, only %u banks on device",
3016  k_bank->bank_number, num_blocks);
3017  return ERROR_FLASH_BANK_INVALID;
3018  }
3019 
3020  /* S32K1xx does not implement FCFG2 register. Skip checks. */
3021  if (k_chip->chip_type != KINETIS_CT_S32K) {
3022  fcfg2_pflsh = (uint8_t)((k_chip->sim_fcfg2 >> 23) & 0x01);
3023  fcfg2_maxaddr0 = (uint8_t)((k_chip->sim_fcfg2 >> 24) & 0x7f);
3024  fcfg2_maxaddr1 = (uint8_t)((k_chip->sim_fcfg2 >> 16) & 0x7f);
3025 
3026  if (k_bank->bank_number == 0 && k_chip->fcfg2_maxaddr0_shifted != bank->size)
3027  LOG_WARNING("MAXADDR0 0x%02" PRIx8 " check failed,"
3028  " please report to OpenOCD mailing list", fcfg2_maxaddr0);
3029 
3030  if (fcfg2_pflsh) {
3031  if (k_bank->bank_number == 1 && k_chip->fcfg2_maxaddr1_shifted != bank->size)
3032  LOG_WARNING("MAXADDR1 0x%02" PRIx8 " check failed,"
3033  " please report to OpenOCD mailing list", fcfg2_maxaddr1);
3034  } else {
3035  if (k_bank->bank_number == first_nvm_bank
3036  && k_chip->fcfg2_maxaddr1_shifted != k_chip->dflash_size)
3037  LOG_WARNING("FlexNVM MAXADDR1 0x%02" PRIx8 " check failed,"
3038  " please report to OpenOCD mailing list", fcfg2_maxaddr1);
3039  }
3040  }
3041 
3042  free(bank->sectors);
3043  bank->sectors = NULL;
3044 
3045  free(bank->prot_blocks);
3046  bank->prot_blocks = NULL;
3047 
3048  if (k_bank->sector_size == 0) {
3049  LOG_ERROR("Unknown sector size for bank %u", bank->bank_number);
3050  return ERROR_FLASH_BANK_INVALID;
3051  }
3052 
3053  bank->num_sectors = bank->size / k_bank->sector_size;
3054 
3055  if (bank->num_sectors > 0) {
3056  /* FlexNVM bank can be used for EEPROM backup therefore zero sized */
3057  bank->sectors = alloc_block_array(0, k_bank->sector_size, bank->num_sectors);
3058  if (!bank->sectors)
3059  return ERROR_FAIL;
3060 
3061  bank->prot_blocks = alloc_block_array(0, k_bank->protection_size, bank->num_prot_blocks);
3062  if (!bank->prot_blocks)
3063  return ERROR_FAIL;
3064 
3065  } else {
3066  bank->num_prot_blocks = 0;
3067  }
3068 
3069  k_bank->probed = true;
3070 
3071  return ERROR_OK;
3072 }
3073 
3075 {
3076  struct kinetis_flash_bank *k_bank = bank->driver_priv;
3077 
3078  if (k_bank && k_bank->probed)
3079  return ERROR_OK;
3080 
3081  return kinetis_probe(bank);
3082 }
3083 
3084 static int kinetis_info(struct flash_bank *bank, struct command_invocation *cmd)
3085 {
3086  const char *bank_class_names[] = {
3087  "(ANY)", "PFlash", "FlexNVM", "FlexRAM"
3088  };
3089 
3090  struct kinetis_flash_bank *k_bank = bank->driver_priv;
3091  struct kinetis_chip *k_chip = k_bank->k_chip;
3092  uint32_t size_k = bank->size / 1024;
3093 
3095  "%s %s: %" PRIu32 "k %s bank %s at " TARGET_ADDR_FMT,
3096  bank->driver->name, k_chip->name,
3097  size_k, bank_class_names[k_bank->flash_class],
3098  bank->name, bank->base);
3099 
3100  return ERROR_OK;
3101 }
3102 
3104 {
3105  struct kinetis_flash_bank *k_bank = bank->driver_priv;
3106  struct kinetis_chip *k_chip = k_bank->k_chip;
3107  int result;
3108 
3109  /* surprisingly blank check does not work in VLPR and HSRUN modes */
3110  result = kinetis_check_run_mode(k_chip);
3111  if (result != ERROR_OK)
3112  return result;
3113 
3114  /* reset error flags */
3115  result = kinetis_ftfx_prepare(bank->target);
3116  if (result != ERROR_OK)
3117  return result;
3118 
3119  if (k_bank->flash_class == FC_PFLASH || k_bank->flash_class == FC_FLEX_NVM) {
3120  bool block_dirty = true;
3121  bool use_block_cmd = !(k_chip->flash_support & FS_NO_CMD_BLOCKSTAT);
3122  uint8_t ftfx_fstat;
3123 
3124  if (use_block_cmd && k_bank->flash_class == FC_FLEX_NVM) {
3125  uint8_t fcfg1_depart = kinetis_fcfg1_depart(k_chip->chip_type, k_chip->sim_fcfg1);
3126  /* block operation cannot be used on FlexNVM when EEPROM backup partition is set */
3127  if (fcfg1_depart != 0xf && fcfg1_depart != 0)
3128  use_block_cmd = false;
3129  }
3130 
3131  if (use_block_cmd) {
3132  /* check if whole bank is blank */
3133  result = kinetis_ftfx_command(bank->target, FTFX_CMD_BLOCKSTAT, k_bank->prog_base,
3134  0, 0, 0, 0, 0, 0, 0, 0, &ftfx_fstat);
3135 
3136  if (result != ERROR_OK)
3137  kinetis_ftfx_clear_error(bank->target);
3138  else if ((ftfx_fstat & 0x01) == 0)
3139  block_dirty = false;
3140  }
3141 
3142  if (block_dirty) {
3143  /* the whole bank is not erased, check sector-by-sector */
3144  for (unsigned int i = 0; i < bank->num_sectors; i++) {
3145  /* normal margin */
3146  result = kinetis_ftfx_command(bank->target, FTFX_CMD_SECTSTAT,
3147  k_bank->prog_base + bank->sectors[i].offset,
3148  1, 0, 0, 0, 0, 0, 0, 0, &ftfx_fstat);
3149 
3150  if (result == ERROR_OK) {
3151  bank->sectors[i].is_erased = !(ftfx_fstat & 0x01);
3152  } else {
3153  LOG_DEBUG("Ignoring error on PFlash sector blank-check");
3154  kinetis_ftfx_clear_error(bank->target);
3155  bank->sectors[i].is_erased = -1;
3156  }
3157  }
3158  } else {
3159  /* the whole bank is erased, update all sectors */
3160  for (unsigned int i = 0; i < bank->num_sectors; i++)
3161  bank->sectors[i].is_erased = 1;
3162  }
3163  } else {
3164  LOG_WARNING("kinetis_blank_check not supported yet for FlexRAM");
3166  }
3167 
3168  return ERROR_OK;
3169 }
3170 
3171 
3172 COMMAND_HANDLER(kinetis_nvm_partition)
3173 {
3174  int result;
3175  unsigned int bank_idx;
3176  unsigned int num_blocks, first_nvm_bank;
3177  unsigned long par, log2 = 0, ee1 = 0, ee2 = 0;
3178  enum { SHOW_INFO, DF_SIZE, EEBKP_SIZE } sz_type = SHOW_INFO;
3179  bool enable;
3180  uint8_t load_flex_ram = 1;
3181  uint8_t ee_size_code = 0x3f;
3182  uint8_t flex_nvm_partition_code = 0;
3183  uint8_t ee_split = 3;
3185  struct kinetis_chip *k_chip;
3186  uint32_t sim_fcfg1;
3187 
3188  k_chip = kinetis_get_chip(target);
3189  if (!k_chip) {
3190  command_print(CMD, "[%s] is not a Kinetis chip", target_name(target));
3191  return ERROR_FAIL;
3192  }
3193 
3194  if (CMD_ARGC >= 2) {
3195  if (strcmp(CMD_ARGV[0], "dataflash") == 0)
3196  sz_type = DF_SIZE;
3197  else if (strcmp(CMD_ARGV[0], "eebkp") == 0)
3198  sz_type = EEBKP_SIZE;
3199 
3200  COMMAND_PARSE_NUMBER(ulong, CMD_ARGV[1], par);
3201  while (par >> (log2 + 3))
3202  log2++;
3203  }
3204  switch (sz_type) {
3205  case SHOW_INFO:
3206  result = target_read_u32(target, k_chip->sim_base + SIM_FCFG1_OFFSET, &sim_fcfg1);
3207  if (result != ERROR_OK)
3208  return result;
3209 
3210  flex_nvm_partition_code = kinetis_fcfg1_depart(k_chip->chip_type, sim_fcfg1);
3211  switch (flex_nvm_partition_code) {
3212  case 0:
3213  command_print(CMD, "No EEPROM backup, data flash only");
3214  break;
3215  case 1:
3216  case 2:
3217  case 3:
3218  case 4:
3219  case 5:
3220  case 6:
3221  command_print(CMD, "EEPROM backup %d KB", 4 << flex_nvm_partition_code);
3222  break;
3223  case 8:
3224  command_print(CMD, "No data flash, EEPROM backup only");
3225  break;
3226  case 0x9:
3227  case 0xA:
3228  case 0xB:
3229  case 0xC:
3230  case 0xD:
3231  case 0xE:
3232  command_print(CMD, "data flash %d KB", 4 << (flex_nvm_partition_code & 7));
3233  break;
3234  case 0xf:
3235  command_print(CMD, "No EEPROM backup, data flash only (DEPART not set)");
3236  break;
3237  default:
3238  command_print(CMD, "Unsupported EEPROM backup size code 0x%02" PRIx8, flex_nvm_partition_code);
3239  }
3240  return ERROR_OK;
3241 
3242  case DF_SIZE:
3243  flex_nvm_partition_code = 0x8 | log2;
3244  break;
3245 
3246  case EEBKP_SIZE:
3247  flex_nvm_partition_code = log2;
3248  break;
3249  }
3250 
3251  if (CMD_ARGC == 3) {
3252  unsigned long eex;
3253  COMMAND_PARSE_NUMBER(ulong, CMD_ARGV[2], eex);
3254  ee1 = ee2 = eex / 2;
3255  } else if (CMD_ARGC >= 4) {
3256  COMMAND_PARSE_NUMBER(ulong, CMD_ARGV[2], ee1);
3257  COMMAND_PARSE_NUMBER(ulong, CMD_ARGV[3], ee2);
3258  }
3259 
3260  enable = ee1 + ee2 > 0;
3261  if (enable) {
3262  for (log2 = 2; ; log2++) {
3263  if (ee1 + ee2 == (16u << 10) >> log2)
3264  break;
3265  if (ee1 + ee2 > (16u << 10) >> log2 || log2 >= 9) {
3266  LOG_ERROR("Unsupported EEPROM size");
3268  }
3269  }
3270 
3271  if (k_chip->chip_type == KINETIS_CT_S32K && ee1 != ee2) {
3272  LOG_ERROR("S32K1xx EEPROM cannot be split into two blocks");
3274  }
3275 
3276  if (ee1 * 3 == ee2)
3277  ee_split = 1;
3278  else if (ee1 * 7 == ee2)
3279  ee_split = 0;
3280  else if (ee1 != ee2) {
3281  LOG_ERROR("Unsupported EEPROM sizes ratio");
3283  }
3284 
3285  ee_size_code = log2 | ee_split << 4;
3286  }
3287 
3288  if (CMD_ARGC >= 5)
3289  COMMAND_PARSE_ON_OFF(CMD_ARGV[4], enable);
3290  if (enable)
3291  load_flex_ram = 0;
3292 
3293  /* S32K1xx has no EEPROM split and rejects the command if split bits are set */
3294  if (k_chip->chip_type == KINETIS_CT_S32K)
3295  ee_size_code &= 0x0f;
3296 
3297  LOG_INFO("DEPART 0x%" PRIx8 ", EEPROM size code 0x%" PRIx8,
3298  flex_nvm_partition_code, ee_size_code);
3299 
3300  result = kinetis_check_run_mode(k_chip);
3301  if (result != ERROR_OK)
3302  return result;
3303 
3304  /* reset error flags */
3305  result = kinetis_ftfx_prepare(target);
3306  if (result != ERROR_OK)
3307  return result;
3308 
3309  result = kinetis_ftfx_command(target, FTFX_CMD_PGMPART, load_flex_ram,
3310  ee_size_code, flex_nvm_partition_code, 0, 0,
3311  0, 0, 0, 0, NULL);
3312  if (result != ERROR_OK)
3313  return result;
3314 
3315  command_print(CMD, "FlexNVM partition set. Please reset MCU.");
3316 
3317  first_nvm_bank = k_chip->num_pflash_blocks;
3318  num_blocks = k_chip->num_pflash_blocks + k_chip->num_nvm_blocks;
3319  for (bank_idx = first_nvm_bank; bank_idx < num_blocks; bank_idx++)
3320  k_chip->banks[bank_idx].probed = false; /* re-probe before next use */
3321  k_chip->probed = false;
3322 
3323  command_print(CMD, "FlexNVM banks will be re-probed to set new data flash size.");
3324  return ERROR_OK;
3325 }
3326 
3327 COMMAND_HANDLER(kinetis_fcf_source_handler)
3328 {
3329  if (CMD_ARGC > 1)
3331 
3332  if (CMD_ARGC == 1) {
3333  if (strcmp(CMD_ARGV[0], "write") == 0)
3334  allow_fcf_writes = true;
3335  else if (strcmp(CMD_ARGV[0], "protection") == 0)
3336  allow_fcf_writes = false;
3337  else
3339  }
3340 
3341  if (allow_fcf_writes) {
3342  command_print(CMD, "Arbitrary Flash Configuration Field writes enabled.");
3343  command_print(CMD, "Protection info writes to FCF disabled.");
3344  LOG_WARNING("BEWARE: incorrect flash configuration may permanently lock the device.");
3345  } else {
3346  command_print(CMD, "Protection info writes to Flash Configuration Field enabled.");
3347  command_print(CMD, "Arbitrary FCF writes disabled. Mode safe from unwanted locking of the device.");
3348  }
3349 
3350  return ERROR_OK;
3351 }
3352 
3353 COMMAND_HANDLER(kinetis_fopt_handler)
3354 {
3355  if (CMD_ARGC > 1)
3357 
3358  if (CMD_ARGC == 1) {
3360  } else {
3361  command_print(CMD, "FCF_FOPT 0x%02" PRIx8, fcf_fopt);
3362  }
3363 
3364  return ERROR_OK;
3365 }
3366 
3367 COMMAND_HANDLER(kinetis_create_banks_handler)
3368 {
3369  if (CMD_ARGC > 0)
3371 
3372  create_banks = true;
3373 
3374  return ERROR_OK;
3375 }
3376 
3377 
3379  {
3380  .name = "check_security",
3381  .mode = COMMAND_EXEC,
3382  .help = "Check status of device security lock",
3383  .usage = "",
3384  .handler = kinetis_check_flash_security_status,
3385  },
3386  {
3387  .name = "halt",
3388  .mode = COMMAND_EXEC,
3389  .help = "Issue a halt via the MDM-AP",
3390  .usage = "",
3391  .handler = kinetis_mdm_halt,
3392  },
3393  {
3394  .name = "mass_erase",
3395  .mode = COMMAND_EXEC,
3396  .help = "Issue a complete flash erase via the MDM-AP",
3397  .usage = "",
3398  .handler = kinetis_mdm_mass_erase,
3399  },
3400  {
3401  .name = "reset",
3402  .mode = COMMAND_EXEC,
3403  .help = "Issue a reset via the MDM-AP",
3404  .usage = "",
3405  .handler = kinetis_mdm_reset,
3406  },
3408 };
3409 
3410 static const struct command_registration kinetis_exec_command_handlers[] = {
3411  {
3412  .name = "mdm",
3413  .mode = COMMAND_ANY,
3414  .help = "MDM-AP command group",
3415  .usage = "",
3417  },
3418  {
3419  .name = "disable_wdog",
3420  .mode = COMMAND_EXEC,
3421  .help = "Disable the watchdog timer",
3422  .usage = "",
3423  .handler = kinetis_disable_wdog_handler,
3424  },
3425  {
3426  .name = "nvm_partition",
3427  .mode = COMMAND_EXEC,
3428  .help = "Show/set data flash or EEPROM backup size in kilobytes,"
3429  " set two EEPROM sizes in bytes and FlexRAM loading during reset",
3430  .usage = "('info'|'dataflash' size|'eebkp' size) [eesize1 eesize2] ['on'|'off']",
3431  .handler = kinetis_nvm_partition,
3432  },
3433  {
3434  .name = "fcf_source",
3435  .mode = COMMAND_EXEC,
3436  .help = "Use protection as a source for Flash Configuration Field or allow writing arbitrary values to the FCF"
3437  " Mode 'protection' is safe from unwanted locking of the device.",
3438  .usage = "['protection'|'write']",
3439  .handler = kinetis_fcf_source_handler,
3440  },
3441  {
3442  .name = "fopt",
3443  .mode = COMMAND_EXEC,
3444  .help = "FCF_FOPT value source in 'kinetis fcf_source protection' mode",
3445  .usage = "[num]",
3446  .handler = kinetis_fopt_handler,
3447  },
3448  {
3449  .name = "create_banks",
3450  .mode = COMMAND_CONFIG,
3451  .help = "Driver creates additional banks if device with two/four flash blocks is probed",
3452  .handler = kinetis_create_banks_handler,
3453  .usage = "",
3454  },
3456 };
3457 
3458 static const struct command_registration kinetis_command_handler[] = {
3459  {
3460  .name = "kinetis",
3461  .mode = COMMAND_ANY,
3462  .help = "Kinetis flash controller commands",
3463  .usage = "",
3465  },
3467 };
3468 
3469 
3470 
3471 const struct flash_driver kinetis_flash = {
3472  .name = "kinetis",
3473  .commands = kinetis_command_handler,
3474  .flash_bank_command = kinetis_flash_bank_command,
3475  .erase = kinetis_erase,
3476  .protect = kinetis_protect,
3477  .write = kinetis_write,
3478  .read = default_flash_read,
3479  .probe = kinetis_probe,
3480  .auto_probe = kinetis_auto_probe,
3481  .erase_check = kinetis_blank_check,
3482  .protect_check = kinetis_protect_check,
3483  .info = kinetis_info,
3484  .free_driver_priv = kinetis_free_driver_priv,
3485 };
void init_reg_param(struct reg_param *param, const char *reg_name, uint32_t size, enum param_direction direction)
Definition: algorithm.c:29
void destroy_reg_param(struct reg_param *param)
Definition: algorithm.c:38
@ PARAM_OUT
Definition: algorithm.h:16
@ PARAM_IN_OUT
Definition: algorithm.h:17
const char * name
Definition: am13e230x.c:167
@ ARM_MODE_THREAD
Definition: arm.h:94
struct adiv5_ap * dap_get_ap(struct adiv5_dap *dap, uint64_t ap_num)
Definition: arm_adi_v5.c:1222
int dap_put_ap(struct adiv5_ap *ap)
Definition: arm_adi_v5.c:1242
This defines formats and data structures used to talk to ADIv5 entities.
static int dap_queue_ap_read(struct adiv5_ap *ap, unsigned int reg, uint32_t *data)
Queue an AP register read.
Definition: arm_adi_v5.h:590
static int dap_queue_ap_write(struct adiv5_ap *ap, unsigned int reg, uint32_t data)
Queue an AP register write.
Definition: arm_adi_v5.h:610
static int dap_run(struct adiv5_dap *dap)
Perform all queued DAP operations, and clear any errors posted in the CTRL_STAT register when they ar...
Definition: arm_adi_v5.h:648
#define ARMV7M_COMMON_MAGIC
Definition: armv7m.h:229
Support functions to access arbitrary bits in a byte array.
static uint32_t buf_get_u32(const uint8_t *_buffer, unsigned int first, unsigned int num)
Retrieves num bits from _buffer, starting at the first bit, returning the bits in a 32-bit word.
Definition: binarybuffer.h:104
static void buf_set_u32(uint8_t *_buffer, unsigned int first, unsigned int num, uint32_t value)
Sets num bits in _buffer, starting at the first bit, using the bits in value.
Definition: binarybuffer.h:34
void command_print_sameline(struct command_invocation *cmd, const char *format,...)
Definition: command.c: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 CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:161
#define COMMAND_PARSE_ON_OFF(in, out)
parses an on/off command argument
Definition: command.h:533
#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 CMD_CTX
Use this macro to access the context of the command being handled, rather than accessing the variable...
Definition: command.h:151
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:256
#define ERROR_COMMAND_ARGUMENT_INVALID
Definition: command.h:407
@ COMMAND_CONFIG
Definition: command.h:41
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
static struct cortex_m_common * target_to_cm(struct target *target)
Definition: cortex_m.h:347
uint64_t buffer
Pointer to data buffer to send over SPI.
Definition: dw-spi-helper.h:0
uint32_t size
Size of dw_spi_transaction::buffer.
Definition: dw-spi-helper.h:4
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_OPERATION_FAILED
Definition: flash/common.h:30
#define ERROR_FLASH_DST_BREAKS_ALIGNMENT
Definition: flash/common.h:32
struct flash_sector * alloc_block_array(uint32_t offset, uint32_t size, unsigned int num_blocks)
Allocate and fill an array of sectors or protection blocks.
void flash_bank_add(struct flash_bank *bank)
Adds a new NOR bank to the global list of banks.
int default_flash_read(struct flash_bank *bank, uint8_t *buffer, uint32_t offset, uint32_t count)
Provides default read implementation for flash memory.
struct flash_bank * flash_bank_list(void)
void jtag_poll_set_enabled(bool value)
Assign flag reporting whether JTAG polling is disallowed.
Definition: jtag/core.c:166
int adapter_deassert_reset(void)
Definition: jtag/core.c:1895
enum reset_types jtag_get_reset_config(void)
Definition: jtag/core.c:1730
int adapter_assert_reset(void)
Definition: jtag/core.c:1875
@ RESET_HAS_SRST
Definition: jtag.h:218
#define KINETIS_SOPT1_RAMSIZE_K24FN1M
Definition: kinetis.c:150
#define KINETIS_K_SDID_K60_M100
Definition: kinetis.c:190
#define KINETIS_SDID_DIEID_K22FN256
Definition: kinetis.c:157
#define PM_STAT_RUN
Definition: kinetis.c:115
#define KINETIS_SDID_SERIESID_MASK
Definition: kinetis.c:197
static int kinetis_probe_chip_s32k(struct kinetis_chip *k_chip)
Definition: kinetis.c:2117
static int kinetis_protect_check(struct flash_bank *bank)
Definition: kinetis.c:1423
#define KINETIS_K_SDID_K22_M50
Definition: kinetis.c:181
#define WDOG_BASE
Definition: kinetis.c:94
#define WDOG32_KE1X
Definition: kinetis.c:95
#define KINETIS_SDID_PROJECTID_KE1XF
Definition: kinetis.c:231
#define KINETIS_SDID_FAMILYID_SHIFT
Definition: kinetis.c:216
#define FTFX_CMD_SECTERASE
Definition: kinetis.c:123
static int kinetis_create_missing_banks(struct kinetis_chip *k_chip)
Definition: kinetis.c:987
#define PM_CTRL_RUNM_RUN
Definition: kinetis.c:117
#define FCF_FPROT
Definition: kinetis.c:74
#define SIM_FCFG1_OFFSET
Definition: kinetis.c:108
#define KINETIS_SDID_SUBFAMID_MASK
Definition: kinetis.c:205
static int kinetis_info(struct flash_bank *bank, struct command_invocation *cmd)
Definition: kinetis.c:3084
#define KINETIS_SDID_SERIESID_KL
Definition: kinetis.c:199
#define MDM_AP
Definition: kinetis.c:385
#define KINETIS_SDID_S32K_SERIES_MASK
Definition: kinetis.c:244
static int kinetis_blank_check(struct flash_bank *bank)
Definition: kinetis.c:3103
#define KINETIS_K_SDID_K21_M120
Definition: kinetis.c:180
#define SIM_SDID_OFFSET
Definition: kinetis.c:107
#define FCF_SIZE
Definition: kinetis.c:78
#define MDM_STAT_SYSSEC
Definition: kinetis.c:393
static bool create_banks
Definition: kinetis.c:419
#define KINETIS_K_SDID_K30_M100
Definition: kinetis.c:184
#define KINETIS_K_SDID_K12
Definition: kinetis.c:174
#define KINETIS_K_SDID_K40_M72
Definition: kinetis.c:185
static int kinetis_write_block(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t wcount)
Definition: kinetis.c:1309
#define FTFX_CMD_SETFLEXRAM
Definition: kinetis.c:127
static int kinetis_probe(struct flash_bank *bank)
Definition: kinetis.c:2927
#define KINETIS_SDID_FAMILYID_K1X
Definition: kinetis.c:219
#define KINETIS_SDID_SUBFAMID_KX6
Definition: kinetis.c:212
#define KINETIS_MAX_BANKS
Definition: kinetis.c:276
#define MDM_CTRL_CORE_HOLD_RES
Definition: kinetis.c:409
static int kinetis_chip_options(struct kinetis_chip *k_chip, int argc, const char *argv[])
Definition: kinetis.c:913
#define SMC32_PMSTAT
Definition: kinetis.c:100
#define KINETIS_SDID_S32K_SERIES_K14X
Definition: kinetis.c:246
#define FTFX_FCNFG
Definition: kinetis.c:86
static int kinetis_disable_wdog32(struct target *target, uint32_t wdog_base)
Definition: kinetis.c:1145
#define KINETIS_SDID_FAMILYID_K3X
Definition: kinetis.c:221
#define KINETIS_K_SDID_K60_M150
Definition: kinetis.c:191
#define MDM_STAT_FMEEN
Definition: kinetis.c:395
#define SMC_PMCTRL
Definition: kinetis.c:97
#define MDM_REG_ID
Definition: kinetis.c:389
#define KINETIS_SDID_DIEID_K24FN1M
Definition: kinetis.c:161
#define KINETIS_SDID_FAMILYID_MASK
Definition: kinetis.c:217
#define KINETIS_K_REVID_MASK
Definition: kinetis.c:194
#define KINETIS_K_SDID_K20_M100
Definition: kinetis.c:177
static int kinetis_protect(struct flash_bank *bank, int set, unsigned int first, unsigned int last)
Definition: kinetis.c:1400
#define WDOG32_KL28
Definition: kinetis.c:96
#define KINETIS_K_SDID_K22_M120
Definition: kinetis.c:182
#define KINETIS_SDID_PROJECTID_MASK
Definition: kinetis.c:230
static const uint8_t kinetis_flash_write_code[]
Definition: kinetis.c:1304
static const struct kinetis_type kinetis_types_old[]
Definition: kinetis.c:351
#define KINETIS_SDID_FAMILYID_KL8X
Definition: kinetis.c:227
#define KINETIS_SDID_FAMILYID_K5X
Definition: kinetis.c:223
COMMAND_HANDLER(kinetis_mdm_halt)
Definition: kinetis.c:509
static int kinetis_write(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
Definition: kinetis.c:2000
static int kinetis_fill_fcf(struct flash_bank *bank, uint8_t *fcf)
Definition: kinetis.c:1468
#define KINETIS_K_SDID_K40_M100
Definition: kinetis.c:186
#define SMC_PMSTAT
Definition: kinetis.c:98
#define KINETIS_SDID_SERIESID_K
Definition: kinetis.c:198
#define KINETIS_SDID_SERIESID_KV
Definition: kinetis.c:202
#define FCF_ADDRESS
Definition: kinetis.c:73
#define KINETIS_SDID_S32K_DERIVATE_KXX8
Definition: kinetis.c:254
static const struct command_registration kinetis_exec_command_handlers[]
Definition: kinetis.c:3410
#define KINETIS_SDID_SUBFAMID_KX2
Definition: kinetis.c:208
static uint8_t fcf_fopt
Definition: kinetis.c:418
#define FTFX_CMD_BLOCKSTAT
Definition: kinetis.c:120
#define KINETIS_K_SDID_TYPE_MASK
Definition: kinetis.c:168
#define KINETIS_K_SDID_K70_M150
Definition: kinetis.c:192
static int kinetis_make_ram_ready(struct target *target)
Definition: kinetis.c:1772
#define KINETIS_SDID_FAMILYID_K4X
Definition: kinetis.c:222
#define KINETIS_K_SDID_K53
Definition: kinetis.c:189
static int kinetis_probe_chip(struct kinetis_chip *k_chip)
Definition: kinetis.c:2297
#define KINETIS_SDID_DIEID_MASK
Definition: kinetis.c:154
#define KINETIS_K_REVID_SHIFT
Definition: kinetis.c:195
#define KINETIS_SDID_PROJECTID_KE1XZ
Definition: kinetis.c:232
static int kinetis_disable_wdog_kx(struct target *target)
Definition: kinetis.c:1113
static int kinetis_ftfx_clear_error(struct target *target)
Definition: kinetis.c:1270
#define KINETIS_SDID_S32K_DERIVATE_KXX5
Definition: kinetis.c:252
#define KINETIS_SDID_SERIESID_KW
Definition: kinetis.c:201
#define FCF_FOPT
Definition: kinetis.c:76
static int kinetis_check_run_mode(struct kinetis_chip *k_chip)
Definition: kinetis.c:1621
#define KINETIS_SDID_K_SERIES_MASK
Definition: kinetis.c:152
#define KINETIS_SDID_SUBFAMID_KX8
Definition: kinetis.c:214
#define KINETIS_SDID_S32K_DERIVATE_KXX4
Definition: kinetis.c:251
#define SIM_FCFG2_OFFSET
Definition: kinetis.c:109
#define KINETIS_SDID_S32K_DERIVATE_KXX3
Definition: kinetis.c:250
#define KINETIS_K_SDID_K10_M100
Definition: kinetis.c:171
#define KINETIS_SDID_SUBFAMID_KX7
Definition: kinetis.c:213
#define PM_STAT_VLPR
Definition: kinetis.c:116
static int kinetis_write_inner(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
Definition: kinetis.c:1900
#define KINETIS_SDID_FAMILYID_K8X
Definition: kinetis.c:226
static int kinetis_auto_probe(struct flash_bank *bank)
Definition: kinetis.c:3074
#define KINETIS_K_SDID_K20_M120
Definition: kinetis.c:178
#define KINETIS_SDID_SUBFAMID_SHIFT
Definition: kinetis.c:204
#define KINETIS_SDID_SUBFAMID_KX0
Definition: kinetis.c:206
#define MDM_REG_STAT
Definition: kinetis.c:387
static int kinetis_ftfx_command(struct target *target, uint8_t fcmd, uint32_t faddr, uint8_t fccob4, uint8_t fccob5, uint8_t fccob6, uint8_t fccob7, uint8_t fccob8, uint8_t fccob9, uint8_t fccoba, uint8_t fccobb, uint8_t *ftfx_fstat)
Definition: kinetis.c:1542
#define FMC_PFB01CR
Definition: kinetis.c:84
#define MDM_CTRL_FMEIP
Definition: kinetis.c:405
#define MSCM_OCMDR0
Definition: kinetis.c:82
#define MCM_PLACR
Definition: kinetis.c:103
#define SIM_BASE_KL28
Definition: kinetis.c:91
#define KINETIS_SDID_SUBFAMID_KX3
Definition: kinetis.c:209
#define KINETIS_K_SDID_K10_M120
Definition: kinetis.c:172
#define KINETIS_SDID_FAMILYID_K0X
Definition: kinetis.c:218
#define SIM_COPC
Definition: kinetis.c:92
static void kinetis_invalidate_flash_cache(struct kinetis_chip *k_chip)
Definition: kinetis.c:1681
#define KINETIS_SDID_FAMILYID_K2X
Definition: kinetis.c:220
static int kinetis_write_sections(struct flash_bank *bank, const uint8_t *buffer, uint32_t offset, uint32_t count)
Definition: kinetis.c:1803
static uint8_t kinetis_fcfg1_depart(enum kinetis_chip_type chip_type, uint32_t sim_fcfg1)
Definition: kinetis.c:906
const struct flash_driver kinetis_flash
Definition: kinetis.c:422
#define KINETIS_K_SDID_K30_M72
Definition: kinetis.c:183
static const uint32_t kinetis_known_mdm_ids[]
Definition: kinetis.c:753
#define KINETIS_SDID_SUBFAMID_KX5
Definition: kinetis.c:211
FLASH_BANK_COMMAND_HANDLER(kinetis_flash_bank_command)
Definition: kinetis.c:927
static int kinetis_mdm_read_register(struct adiv5_dap *dap, unsigned int reg, uint32_t *result)
Definition: kinetis.c:458
static void kinetis_free_driver_priv(struct flash_bank *bank)
Definition: kinetis.c:971
#define SIM_SOPT1_OFFSET
Definition: kinetis.c:106
#define KINETIS_SDID_SUBFAMID_KX1
Definition: kinetis.c:207
#define KINETIS_K_SDID_K11
Definition: kinetis.c:173
#define KINETIS_K_SDID_K20_M72
Definition: kinetis.c:176
#define KINETIS_SDID_S32K_SERIES_K11X
Definition: kinetis.c:245
static int kinetis_ftfx_prepare(struct target *target)
Definition: kinetis.c:1277
#define KINETIS_SDID_DIEID_K22FN128
Definition: kinetis.c:156
#define KINETIS_K_SDID_K10_M50
Definition: kinetis.c:169
#define FTFX_CMD_SECTSTAT
Definition: kinetis.c:121
#define SMC32_PMCTRL
Definition: kinetis.c:99
#define FTFX_CMD_PGMPART
Definition: kinetis.c:126
#define KINETIS_SDID_S32K_DERIVATE_MASK
Definition: kinetis.c:248
static bool allow_fcf_writes
Definition: kinetis.c:417
static struct kinetis_chip * kinetis_get_chip(struct target *target)
Definition: kinetis.c:885
#define FTFX_FDPROT
Definition: kinetis.c:89
static int kinetis_mdm_write_register(struct adiv5_dap *dap, unsigned int reg, uint32_t value)
Definition: kinetis.c:430
#define FTFX_CMD_LWORDPROG
Definition: kinetis.c:122
#define SIM_BASE
Definition: kinetis.c:90
#define FTFX_FPROT3
Definition: kinetis.c:88
#define KINETIS_K_SDID_K10_M72
Definition: kinetis.c:170
#define FCF_FSEC
Definition: kinetis.c:75
#define FLEXRAM
Definition: kinetis.c:80
static int kinetis_disable_wdog(struct kinetis_chip *k_chip)
Definition: kinetis.c:1178
#define KINETIS_SDID_DIEID_K22FN512
Definition: kinetis.c:158
#define KINETIS_SDID_S32K_DERIVATE_KXX2
Definition: kinetis.c:249
#define KINETIS_SDID_DIEID_K24FN256
Definition: kinetis.c:159
#define PMC_REGSC
Definition: kinetis.c:101
#define KINETIS_SDID_SERIESID_KE
Definition: kinetis.c:200
#define MDM_ACCESS_TIMEOUT
Definition: kinetis.c:414
#define MDM_STAT_FREADY
Definition: kinetis.c:392
static int kinetis_mdm_poll_register(struct adiv5_dap *dap, unsigned int reg, uint32_t mask, uint32_t value, uint32_t timeout_ms)
Definition: kinetis.c:484
#define FTFX_FCCOB3
Definition: kinetis.c:87
#define MDM_REG_CTRL
Definition: kinetis.c:388
#define MDM_STAT_SYSRES
Definition: kinetis.c:394
static int kinetis_erase(struct flash_bank *bank, unsigned int first, unsigned int last)
Definition: kinetis.c:1714
#define KINETIS_K_SDID_K51_M72
Definition: kinetis.c:188
kinetis_chip_type
Definition: kinetis.c:278
@ KINETIS_CT_S32K
Definition: kinetis.c:280
@ KINETIS_CT_KINETIS
Definition: kinetis.c:279
#define WDOG32_CS_OFFSET
Definition: kinetis.c:112
static const struct command_registration kinetis_command_handler[]
Definition: kinetis.c:3458
static int kinetis_read_pmstat(struct kinetis_chip *k_chip, uint8_t *pmstat)
Definition: kinetis.c:1591
#define KINETIS_SOPT1_RAMSIZE_MASK
Definition: kinetis.c:149
static int kinetis_disable_wdog_algo(struct target *target, size_t code_size, const uint8_t *code, uint32_t wdog_base)
Definition: kinetis.c:1069
#define KINETIS_SDID_FAMILYID_K6X
Definition: kinetis.c:224
static int kinetis_ftfx_decode_error(uint8_t fstat)
Definition: kinetis.c:1249
#define KINETIS_K_SDID_K50_M72
Definition: kinetis.c:187
#define FTFX_FSTAT
Definition: kinetis.c:85
#define KINETIS_SDID_S32K_DERIVATE_KXX6
Definition: kinetis.c:253
#define FTFX_CMD_SECTWRITE
Definition: kinetis.c:124
#define FCF_FDPROT
Definition: kinetis.c:77
#define MSCM_OCMDR1
Definition: kinetis.c:83
#define KINETIS_K_SDID_K21_M50
Definition: kinetis.c:179
static const struct command_registration kinetis_security_command_handlers[]
Definition: kinetis.c:3378
#define KINETIS_K_SDID_K20_M50
Definition: kinetis.c:175
#define MDM_CTRL_SYS_RES_REQ
Definition: kinetis.c:408
#define WDOG_STCTRLH_OFFSET
Definition: kinetis.c:111
#define MC_PMCTRL
Definition: kinetis.c:102
#define KINETIS_SDID_SUBFAMID_KX4
Definition: kinetis.c:210
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
#define FLASH_WRITE_GAP_SECTOR
Definition: nor/core.h:63
uint8_t mask
Definition: parport.c:70
#define MAX(a, b)
Definition: replacements.h:25
struct target * target
Definition: rtt/rtt.c:26
struct rtt_source source
Definition: rtt/rtt.c:23
This represents an ARM Debug Interface (v5) Access Port (AP).
Definition: arm_adi_v5.h:250
struct adiv5_dap * dap
DAP this AP belongs to.
Definition: arm_adi_v5.h:254
This represents an ARM Debug Interface (v5) Debug Access Port (DAP).
Definition: arm_adi_v5.h:348
const struct dap_ops * ops
Definition: arm_adi_v5.h:349
struct adiv5_dap * dap
For targets conforming to ARM Debug Interface v5, this handle references the Debug Access Port (DAP) ...
Definition: arm.h:258
unsigned int common_magic
Definition: armv7m.h:306
enum arm_mode core_mode
Definition: armv7m.h:308
struct arm arm
Definition: armv7m.h:234
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
struct armv7m_common armv7m
Definition: cortex_m.h:275
Provides details of a flash bank, available either on-chip or through a major interface.
Definition: nor/core.h:75
unsigned int num_prot_blocks
The number of protection blocks in this bank.
Definition: nor/core.h:137
const struct flash_driver * driver
Driver for this bank.
Definition: nor/core.h:80
void * driver_priv
Private driver storage pointer.
Definition: nor/core.h:81
struct flash_sector * prot_blocks
Array of protection blocks, allocated and initialized by the flash driver.
Definition: nor/core.h:139
struct flash_bank * next
The next flash bank on this chip.
Definition: nor/core.h:141
struct target * target
Target to which this bank belongs.
Definition: nor/core.h:78
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
int is_protected
Indication of protection status: 0 = unprotected/unlocked, 1 = protected/locked, other = unknown.
Definition: nor/core.h:55
uint32_t nvm_base
Definition: kinetis.c:299
enum kinetis_chip::@10 watchdog_type
uint32_t sim_sdid
Definition: kinetis.c:287
unsigned int nvm_sector_size
Definition: kinetis.c:294
struct kinetis_flash_bank banks[KINETIS_MAX_BANKS]
Definition: kinetis.c:343
char name[40]
Definition: kinetis.c:340
uint32_t pflash_size
Definition: kinetis.c:298
uint32_t sim_fcfg2
Definition: kinetis.c:289
uint32_t sim_fcfg1
Definition: kinetis.c:288
uint32_t nvm_size
Definition: kinetis.c:300
struct target * target
Definition: kinetis.c:284
enum kinetis_chip_type chip_type
Definition: kinetis.c:306
@ KINETIS_SMC32
Definition: kinetis.c:336
unsigned int num_nvm_blocks
Definition: kinetis.c:293
enum kinetis_chip::@8 flash_support
unsigned int pflash_sector_size
Definition: kinetis.c:294
enum kinetis_chip::@9 cache_type
uint32_t sim_base
Definition: kinetis.c:304
@ FS_PROGRAM_PHRASE
Definition: kinetis.c:311
@ FS_PROGRAM_LONGWORD
Definition: kinetis.c:310
@ FS_WIDTH_256BIT
Definition: kinetis.c:314
@ FS_NO_CMD_BLOCKSTAT
Definition: kinetis.c:313
@ FS_PROGRAM_SECTOR
Definition: kinetis.c:309
enum kinetis_chip::@11 sysmodectrlr_type
@ KINETIS_WDOG_K
Definition: kinetis.c:328
@ KINETIS_WDOG32_KL28
Definition: kinetis.c:331
@ KINETIS_WDOG_NONE
Definition: kinetis.c:327
@ KINETIS_WDOG32_KE1X
Definition: kinetis.c:330
@ KINETIS_WDOG_COP
Definition: kinetis.c:329
uint32_t progr_accel_ram
Definition: kinetis.c:303
unsigned int max_flash_prog_size
Definition: kinetis.c:295
bool probed
Definition: kinetis.c:285
unsigned int num_pflash_blocks
Definition: kinetis.c:293
uint32_t dflash_size
Definition: kinetis.c:301
@ KINETIS_CACHE_K
Definition: kinetis.c:320
@ KINETIS_CACHE_L
Definition: kinetis.c:321
@ KINETIS_CACHE_MSCM
Definition: kinetis.c:322
@ KINETIS_CACHE_MSCM2
Definition: kinetis.c:323
@ KINETIS_CACHE_NONE
Definition: kinetis.c:319
unsigned int num_banks
Definition: kinetis.c:342
uint32_t fcfg2_maxaddr0_shifted
Definition: kinetis.c:290
uint32_t pflash_base
Definition: kinetis.c:297
uint32_t fcfg2_maxaddr1_shifted
Definition: kinetis.c:291
uint32_t sector_size
Definition: kinetis.c:262
uint32_t prog_base
Definition: kinetis.c:264
uint32_t protection_size
Definition: kinetis.c:263
struct flash_bank * bank
Definition: kinetis.c:260
enum kinetis_flash_bank::@7 flash_class
struct kinetis_chip * k_chip
Definition: kinetis.c:257
unsigned int bank_number
Definition: kinetis.c:259
uint32_t protection_block
Definition: kinetis.c:266
char * name
Definition: kinetis.c:348
uint32_t sdid
Definition: kinetis.c:347
uint8_t * value
Definition: algorithm.h:30
Definition: register.h:111
int(* deassert_reset)(struct target *target)
The implementation is responsible for polling the target such that target->state reflects the state c...
Definition: target_type.h:77
int(* assert_reset)(struct target *target)
Definition: target_type.h:65
Definition: target.h:119
enum target_state state
Definition: target.h:167
struct target_type * type
Definition: target.h:120
bool reset_halt
Definition: target.h:154
target_addr_t address
Definition: target.h:89
void target_buffer_set_u32(struct target *target, uint8_t *buffer, uint32_t value)
Definition: target.c:363
int target_write_buffer(struct target *target, target_addr_t address, uint32_t size, const uint8_t *buffer)
Definition: target.c:2410
int target_write_u8(struct target *target, target_addr_t address, uint8_t value)
Definition: target.c:2710
int target_read_u8(struct target *target, target_addr_t address, uint8_t *value)
Definition: target.c:2642
int target_run_algorithm(struct target *target, int num_mem_params, struct mem_param *mem_params, int num_reg_params, struct reg_param *reg_param, target_addr_t entry_point, target_addr_t exit_point, unsigned int timeout_ms, void *arch_info)
Downloads a target-specific native code algorithm to the target, and executes it.
Definition: target.c:787
int target_write_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, const uint8_t *buffer)
Write count items of size bytes to the memory of target at the address given.
Definition: target.c:1289
uint32_t target_get_working_area_avail(struct target *target)
Definition: target.c:2220
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_poll(struct target *target)
Definition: target.c:488
int target_free_working_area(struct target *target, struct working_area *area)
Free a working area.
Definition: target.c:2174
int target_read_u16(struct target *target, target_addr_t address, uint16_t *value)
Definition: target.c:2622
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
int target_read_memory(struct target *target, target_addr_t address, uint32_t size, uint32_t count, uint8_t *buffer)
Read count items of size bytes from the memory of target at the address given.
Definition: target.c:1261
struct target * get_current_target(struct command_context *cmd_ctx)
Definition: target.c:469
#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
int64_t timeval_ms(void)
#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 cmd
Definition: vdebug.c:1
uint8_t offset[4]
Definition: vdebug.c:9
uint8_t count[4]
Definition: vdebug.c:22