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