1 /***************************************************************************
2 * Copyright (C) 2011 by Mathias Kuester *
5 * Copyright (C) 2011 sleep(5) ltd *
6 * tomas@sleepfive.com *
8 * Copyright (C) 2012 by Christopher D. Kilgour *
9 * techie at whiterocker.com *
11 * Copyright (C) 2013 Nemui Trinomius *
12 * nemuisan_kawausogasuki@live.jp *
14 * Copyright (C) 2015 Tomas Vanek *
17 * This program is free software; you can redistribute it and/or modify *
18 * it under the terms of the GNU General Public License as published by *
19 * the Free Software Foundation; either version 2 of the License, or *
20 * (at your option) any later version. *
22 * This program is distributed in the hope that it will be useful, *
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
25 * GNU General Public License for more details. *
27 * You should have received a copy of the GNU General Public License *
28 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
29 ***************************************************************************/
35 #include "jtag/interface.h"
37 #include <helper/binarybuffer.h>
38 #include <helper/time_support.h>
39 #include <target/target_type.h>
40 #include <target/algorithm.h>
41 #include <target/armv7m.h>
42 #include <target/cortex_m.h>
45 * Implementation Notes
47 * The persistent memories in the Kinetis chip families K10 through
48 * K70 are all manipulated with the Flash Memory Module. Some
49 * variants call this module the FTFE, others call it the FTFL. To
50 * indicate that both are considered here, we use FTFX.
52 * Within the module, according to the chip variant, the persistent
53 * memory is divided into what Freescale terms Program Flash, FlexNVM,
54 * and FlexRAM. All chip variants have Program Flash. Some chip
55 * variants also have FlexNVM and FlexRAM, which always appear
58 * A given Kinetis chip may have 1, 2 or 4 blocks of flash. Here we map
59 * each block to a separate bank. Each block size varies by chip and
60 * may be determined by the read-only SIM_FCFG1 register. The sector
61 * size within each bank/block varies by chip, and may be 1, 2 or 4k.
62 * The sector size may be different for flash and FlexNVM.
64 * The first half of the flash (1 or 2 blocks) is always Program Flash
65 * and always starts at address 0x00000000. The "PFLSH" flag, bit 23
66 * of the read-only SIM_FCFG2 register, determines whether the second
67 * half of the flash is also Program Flash or FlexNVM+FlexRAM. When
68 * PFLSH is set, the second from the first half. When PFLSH is clear,
69 * the second half of flash is FlexNVM and always starts at address
70 * 0x10000000. FlexRAM, which is also present when PFLSH is clear,
71 * always starts at address 0x14000000.
73 * The Flash Memory Module provides a register set where flash
74 * commands are loaded to perform flash operations like erase and
75 * program. Different commands are available depending on whether
76 * Program Flash or FlexNVM/FlexRAM is being manipulated. Although
77 * the commands used are quite consistent between flash blocks, the
78 * parameters they accept differ according to the flash sector size.
83 #define FCF_ADDRESS 0x00000400
87 #define FCF_FDPROT 0xf
90 #define FLEXRAM 0x14000000
92 #define MSCM_OCMDR0 0x40001400
93 #define FMC_PFB01CR 0x4001f004
94 #define FTFx_FSTAT 0x40020000
95 #define FTFx_FCNFG 0x40020001
96 #define FTFx_FCCOB3 0x40020004
97 #define FTFx_FPROT3 0x40020010
98 #define FTFx_FDPROT 0x40020017
99 #define SIM_SDID 0x40048024
100 #define SIM_SOPT1 0x40047000
101 #define SIM_FCFG1 0x4004804c
102 #define SIM_FCFG2 0x40048050
103 #define WDOG_STCTRH 0x40052000
104 #define SMC_PMCTRL 0x4007E001
105 #define SMC_PMSTAT 0x4007E003
106 #define MCM_PLACR 0xF000300C
109 #define PM_STAT_RUN 0x01
110 #define PM_STAT_VLPR 0x04
111 #define PM_CTRL_RUNM_RUN 0x00
114 #define FTFx_CMD_BLOCKSTAT 0x00
115 #define FTFx_CMD_SECTSTAT 0x01
116 #define FTFx_CMD_LWORDPROG 0x06
117 #define FTFx_CMD_SECTERASE 0x09
118 #define FTFx_CMD_SECTWRITE 0x0b
119 #define FTFx_CMD_MASSERASE 0x44
120 #define FTFx_CMD_PGMPART 0x80
121 #define FTFx_CMD_SETFLEXRAM 0x81
123 /* The older Kinetis K series uses the following SDID layout :
130 * The newer Kinetis series uses the following SDID layout :
132 * Bit 27-24 : SUBFAMID
133 * Bit 23-20 : SERIESID
134 * Bit 19-16 : SRAMSIZE
136 * Bit 6-4 : Reserved (0)
139 * We assume that if bits 31-16 are 0 then it's an older
143 #define KINETIS_SOPT1_RAMSIZE_MASK 0x0000F000
144 #define KINETIS_SOPT1_RAMSIZE_K24FN1M 0x0000B000
146 #define KINETIS_SDID_K_SERIES_MASK 0x0000FFFF
148 #define KINETIS_SDID_DIEID_MASK 0x00000F80
150 #define KINETIS_SDID_DIEID_K22FN128 0x00000680 /* smaller pflash with FTFA */
151 #define KINETIS_SDID_DIEID_K22FN256 0x00000A80
152 #define KINETIS_SDID_DIEID_K22FN512 0x00000E80
153 #define KINETIS_SDID_DIEID_K24FN256 0x00000700
155 #define KINETIS_SDID_DIEID_K24FN1M 0x00000300 /* Detect Errata 7534 */
157 /* We can't rely solely on the FAMID field to determine the MCU
158 * type since some FAMID values identify multiple MCUs with
159 * different flash sector sizes (K20 and K22 for instance).
160 * Therefore we combine it with the DIEID bits which may possibly
161 * break if Freescale bumps the DIEID for a particular MCU. */
162 #define KINETIS_K_SDID_TYPE_MASK 0x00000FF0
163 #define KINETIS_K_SDID_K10_M50 0x00000000
164 #define KINETIS_K_SDID_K10_M72 0x00000080
165 #define KINETIS_K_SDID_K10_M100 0x00000100
166 #define KINETIS_K_SDID_K10_M120 0x00000180
167 #define KINETIS_K_SDID_K11 0x00000220
168 #define KINETIS_K_SDID_K12 0x00000200
169 #define KINETIS_K_SDID_K20_M50 0x00000010
170 #define KINETIS_K_SDID_K20_M72 0x00000090
171 #define KINETIS_K_SDID_K20_M100 0x00000110
172 #define KINETIS_K_SDID_K20_M120 0x00000190
173 #define KINETIS_K_SDID_K21_M50 0x00000230
174 #define KINETIS_K_SDID_K21_M120 0x00000330
175 #define KINETIS_K_SDID_K22_M50 0x00000210
176 #define KINETIS_K_SDID_K22_M120 0x00000310
177 #define KINETIS_K_SDID_K30_M72 0x000000A0
178 #define KINETIS_K_SDID_K30_M100 0x00000120
179 #define KINETIS_K_SDID_K40_M72 0x000000B0
180 #define KINETIS_K_SDID_K40_M100 0x00000130
181 #define KINETIS_K_SDID_K50_M72 0x000000E0
182 #define KINETIS_K_SDID_K51_M72 0x000000F0
183 #define KINETIS_K_SDID_K53 0x00000170
184 #define KINETIS_K_SDID_K60_M100 0x00000140
185 #define KINETIS_K_SDID_K60_M150 0x000001C0
186 #define KINETIS_K_SDID_K70_M150 0x000001D0
188 #define KINETIS_SDID_SERIESID_MASK 0x00F00000
189 #define KINETIS_SDID_SERIESID_K 0x00000000
190 #define KINETIS_SDID_SERIESID_KL 0x00100000
191 #define KINETIS_SDID_SERIESID_KE 0x00200000
192 #define KINETIS_SDID_SERIESID_KW 0x00500000
193 #define KINETIS_SDID_SERIESID_KV 0x00600000
195 #define KINETIS_SDID_SUBFAMID_SHIFT 24
196 #define KINETIS_SDID_SUBFAMID_MASK 0x0F000000
197 #define KINETIS_SDID_SUBFAMID_KX0 0x00000000
198 #define KINETIS_SDID_SUBFAMID_KX1 0x01000000
199 #define KINETIS_SDID_SUBFAMID_KX2 0x02000000
200 #define KINETIS_SDID_SUBFAMID_KX3 0x03000000
201 #define KINETIS_SDID_SUBFAMID_KX4 0x04000000
202 #define KINETIS_SDID_SUBFAMID_KX5 0x05000000
203 #define KINETIS_SDID_SUBFAMID_KX6 0x06000000
204 #define KINETIS_SDID_SUBFAMID_KX7 0x07000000
205 #define KINETIS_SDID_SUBFAMID_KX8 0x08000000
207 #define KINETIS_SDID_FAMILYID_SHIFT 28
208 #define KINETIS_SDID_FAMILYID_MASK 0xF0000000
209 #define KINETIS_SDID_FAMILYID_K0X 0x00000000
210 #define KINETIS_SDID_FAMILYID_K1X 0x10000000
211 #define KINETIS_SDID_FAMILYID_K2X 0x20000000
212 #define KINETIS_SDID_FAMILYID_K3X 0x30000000
213 #define KINETIS_SDID_FAMILYID_K4X 0x40000000
214 #define KINETIS_SDID_FAMILYID_K5X 0x50000000
215 #define KINETIS_SDID_FAMILYID_K6X 0x60000000
216 #define KINETIS_SDID_FAMILYID_K7X 0x70000000
217 #define KINETIS_SDID_FAMILYID_K8X 0x80000000
218 #define KINETIS_SDID_FAMILYID_KL8X 0x90000000
220 /* The field originally named DIEID has new name/meaning on KE1x */
221 #define KINETIS_SDID_PROJECTID_MASK KINETIS_SDID_DIEID_MASK
222 #define KINETIS_SDID_PROJECTID_KE1xF 0x00000080
223 #define KINETIS_SDID_PROJECTID_KE1xZ 0x00000100
225 struct kinetis_flash_bank {
226 struct kinetis_chip *k_chip;
228 unsigned bank_number; /* bank number in particular chip */
229 struct flash_bank *bank;
231 uint32_t sector_size;
232 uint32_t protection_size;
233 uint32_t prog_base; /* base address for FTFx operations */
234 /* usually same as bank->base for pflash, differs for FlexNVM */
235 uint32_t protection_block; /* number of first protection block in this bank */
245 #define KINETIS_MAX_BANKS 4u
247 struct kinetis_chip {
248 struct target *target;
254 uint32_t fcfg2_maxaddr0_shifted;
255 uint32_t fcfg2_maxaddr1_shifted;
257 unsigned num_pflash_blocks, num_nvm_blocks;
258 unsigned pflash_sector_size, nvm_sector_size;
259 unsigned max_flash_prog_size;
261 uint32_t pflash_base;
262 uint32_t pflash_size;
264 uint32_t nvm_size; /* whole FlexNVM */
265 uint32_t dflash_size; /* accessible rest of FlexNVM if EEPROM backup uses part of FlexNVM */
267 uint32_t progr_accel_ram;
270 FS_PROGRAM_SECTOR = 1,
271 FS_PROGRAM_LONGWORD = 2,
272 FS_PROGRAM_PHRASE = 4, /* Unsupported */
273 FS_INVALIDATE_CACHE_K = 8, /* using FMC->PFB0CR/PFB01CR */
274 FS_INVALIDATE_CACHE_L = 0x10, /* using MCM->PLACR */
275 FS_INVALIDATE_CACHE_MSCM = 0x20,
276 FS_NO_CMD_BLOCKSTAT = 0x40,
277 FS_WIDTH_256BIT = 0x80,
283 struct kinetis_flash_bank banks[KINETIS_MAX_BANKS];
286 struct kinetis_type {
291 static const struct kinetis_type kinetis_types_old[] = {
292 { KINETIS_K_SDID_K10_M50, "MK10D%s5" },
293 { KINETIS_K_SDID_K10_M72, "MK10D%s7" },
294 { KINETIS_K_SDID_K10_M100, "MK10D%s10" },
295 { KINETIS_K_SDID_K10_M120, "MK10F%s12" },
296 { KINETIS_K_SDID_K11, "MK11D%s5" },
297 { KINETIS_K_SDID_K12, "MK12D%s5" },
299 { KINETIS_K_SDID_K20_M50, "MK20D%s5" },
300 { KINETIS_K_SDID_K20_M72, "MK20D%s7" },
301 { KINETIS_K_SDID_K20_M100, "MK20D%s10" },
302 { KINETIS_K_SDID_K20_M120, "MK20F%s12" },
303 { KINETIS_K_SDID_K21_M50, "MK21D%s5" },
304 { KINETIS_K_SDID_K21_M120, "MK21F%s12" },
305 { KINETIS_K_SDID_K22_M50, "MK22D%s5" },
306 { KINETIS_K_SDID_K22_M120, "MK22F%s12" },
308 { KINETIS_K_SDID_K30_M72, "MK30D%s7" },
309 { KINETIS_K_SDID_K30_M100, "MK30D%s10" },
311 { KINETIS_K_SDID_K40_M72, "MK40D%s7" },
312 { KINETIS_K_SDID_K40_M100, "MK40D%s10" },
314 { KINETIS_K_SDID_K50_M72, "MK50D%s7" },
315 { KINETIS_K_SDID_K51_M72, "MK51D%s7" },
316 { KINETIS_K_SDID_K53, "MK53D%s10" },
318 { KINETIS_K_SDID_K60_M100, "MK60D%s10" },
319 { KINETIS_K_SDID_K60_M150, "MK60F%s15" },
321 { KINETIS_K_SDID_K70_M150, "MK70F%s15" },
327 #define MDM_REG_STAT 0x00
328 #define MDM_REG_CTRL 0x04
329 #define MDM_REG_ID 0xfc
331 #define MDM_STAT_FMEACK (1<<0)
332 #define MDM_STAT_FREADY (1<<1)
333 #define MDM_STAT_SYSSEC (1<<2)
334 #define MDM_STAT_SYSRES (1<<3)
335 #define MDM_STAT_FMEEN (1<<5)
336 #define MDM_STAT_BACKDOOREN (1<<6)
337 #define MDM_STAT_LPEN (1<<7)
338 #define MDM_STAT_VLPEN (1<<8)
339 #define MDM_STAT_LLSMODEXIT (1<<9)
340 #define MDM_STAT_VLLSXMODEXIT (1<<10)
341 #define MDM_STAT_CORE_HALTED (1<<16)
342 #define MDM_STAT_CORE_SLEEPDEEP (1<<17)
343 #define MDM_STAT_CORESLEEPING (1<<18)
345 #define MDM_CTRL_FMEIP (1<<0)
346 #define MDM_CTRL_DBG_DIS (1<<1)
347 #define MDM_CTRL_DBG_REQ (1<<2)
348 #define MDM_CTRL_SYS_RES_REQ (1<<3)
349 #define MDM_CTRL_CORE_HOLD_RES (1<<4)
350 #define MDM_CTRL_VLLSX_DBG_REQ (1<<5)
351 #define MDM_CTRL_VLLSX_DBG_ACK (1<<6)
352 #define MDM_CTRL_VLLSX_STAT_ACK (1<<7)
354 #define MDM_ACCESS_TIMEOUT 500 /* msec */
357 static bool allow_fcf_writes;
358 static uint8_t fcf_fopt = 0xff;
361 struct flash_driver kinetis_flash;
362 static int kinetis_write_inner(struct flash_bank *bank, const uint8_t *buffer,
363 uint32_t offset, uint32_t count);
364 static int kinetis_auto_probe(struct flash_bank *bank);
367 static int kinetis_mdm_write_register(struct adiv5_dap *dap, unsigned reg, uint32_t value)
370 LOG_DEBUG("MDM_REG[0x%02x] <- %08" PRIX32, reg, value);
372 retval = dap_queue_ap_write(dap_ap(dap, MDM_AP), reg, value);
373 if (retval != ERROR_OK) {
374 LOG_DEBUG("MDM: failed to queue a write request");
378 retval = dap_run(dap);
379 if (retval != ERROR_OK) {
380 LOG_DEBUG("MDM: dap_run failed");
388 static int kinetis_mdm_read_register(struct adiv5_dap *dap, unsigned reg, uint32_t *result)
392 retval = dap_queue_ap_read(dap_ap(dap, MDM_AP), reg, result);
393 if (retval != ERROR_OK) {
394 LOG_DEBUG("MDM: failed to queue a read request");
398 retval = dap_run(dap);
399 if (retval != ERROR_OK) {
400 LOG_DEBUG("MDM: dap_run failed");
404 LOG_DEBUG("MDM_REG[0x%02x]: %08" PRIX32, reg, *result);
408 static int kinetis_mdm_poll_register(struct adiv5_dap *dap, unsigned reg,
409 uint32_t mask, uint32_t value, uint32_t timeout_ms)
413 int64_t ms_timeout = timeval_ms() + timeout_ms;
416 retval = kinetis_mdm_read_register(dap, reg, &val);
417 if (retval != ERROR_OK || (val & mask) == value)
421 } while (timeval_ms() < ms_timeout);
423 LOG_DEBUG("MDM: polling timed out");
428 * This command can be used to break a watchdog reset loop when
429 * connecting to an unsecured target. Unlike other commands, halt will
430 * automatically retry as it does not know how far into the boot process
431 * it is when the command is called.
433 COMMAND_HANDLER(kinetis_mdm_halt)
435 struct target *target = get_current_target(CMD_CTX);
436 struct cortex_m_common *cortex_m = target_to_cm(target);
437 struct adiv5_dap *dap = cortex_m->armv7m.arm.dap;
441 int64_t ms_timeout = timeval_ms() + MDM_ACCESS_TIMEOUT;
444 LOG_ERROR("Cannot perform halt with a high-level adapter");
451 kinetis_mdm_write_register(dap, MDM_REG_CTRL, MDM_CTRL_CORE_HOLD_RES);
455 retval = kinetis_mdm_read_register(dap, MDM_REG_STAT, &stat);
456 if (retval != ERROR_OK) {
457 LOG_DEBUG("MDM: failed to read MDM_REG_STAT");
461 /* Repeat setting MDM_CTRL_CORE_HOLD_RES until system is out of
462 * reset with flash ready and without security
464 if ((stat & (MDM_STAT_FREADY | MDM_STAT_SYSSEC | MDM_STAT_SYSRES))
465 == (MDM_STAT_FREADY | MDM_STAT_SYSRES))
468 if (timeval_ms() >= ms_timeout) {
469 LOG_ERROR("MDM: halt timed out");
474 LOG_DEBUG("MDM: halt succeded after %d attempts.", tries);
477 /* enable polling in case kinetis_check_flash_security_status disabled it */
478 jtag_poll_set_enabled(true);
482 target->reset_halt = true;
483 target->type->assert_reset(target);
485 retval = kinetis_mdm_write_register(dap, MDM_REG_CTRL, 0);
486 if (retval != ERROR_OK) {
487 LOG_ERROR("MDM: failed to clear MDM_REG_CTRL");
491 target->type->deassert_reset(target);
496 COMMAND_HANDLER(kinetis_mdm_reset)
498 struct target *target = get_current_target(CMD_CTX);
499 struct cortex_m_common *cortex_m = target_to_cm(target);
500 struct adiv5_dap *dap = cortex_m->armv7m.arm.dap;
504 LOG_ERROR("Cannot perform reset with a high-level adapter");
508 retval = kinetis_mdm_write_register(dap, MDM_REG_CTRL, MDM_CTRL_SYS_RES_REQ);
509 if (retval != ERROR_OK) {
510 LOG_ERROR("MDM: failed to write MDM_REG_CTRL");
514 retval = kinetis_mdm_poll_register(dap, MDM_REG_STAT, MDM_STAT_SYSRES, 0, 500);
515 if (retval != ERROR_OK) {
516 LOG_ERROR("MDM: failed to assert reset");
520 retval = kinetis_mdm_write_register(dap, MDM_REG_CTRL, 0);
521 if (retval != ERROR_OK) {
522 LOG_ERROR("MDM: failed to clear MDM_REG_CTRL");
530 * This function implements the procedure to mass erase the flash via
531 * SWD/JTAG on Kinetis K and L series of devices as it is described in
532 * AN4835 "Production Flash Programming Best Practices for Kinetis K-
533 * and L-series MCUs" Section 4.2.1. To prevent a watchdog reset loop,
534 * the core remains halted after this function completes as suggested
535 * by the application note.
537 COMMAND_HANDLER(kinetis_mdm_mass_erase)
539 struct target *target = get_current_target(CMD_CTX);
540 struct cortex_m_common *cortex_m = target_to_cm(target);
541 struct adiv5_dap *dap = cortex_m->armv7m.arm.dap;
544 LOG_ERROR("Cannot perform mass erase with a high-level adapter");
551 * ... Power on the processor, or if power has already been
552 * applied, assert the RESET pin to reset the processor. For
553 * devices that do not have a RESET pin, write the System
554 * Reset Request bit in the MDM-AP control register after
555 * establishing communication...
558 /* assert SRST if configured */
559 bool has_srst = jtag_get_reset_config() & RESET_HAS_SRST;
561 adapter_assert_reset();
563 retval = kinetis_mdm_write_register(dap, MDM_REG_CTRL, MDM_CTRL_SYS_RES_REQ);
564 if (retval != ERROR_OK && !has_srst) {
565 LOG_ERROR("MDM: failed to assert reset");
566 goto deassert_reset_and_exit;
570 * ... Read the MDM-AP status register repeatedly and wait for
571 * stable conditions suitable for mass erase:
572 * - mass erase is enabled
574 * - reset is finished
576 * Mass erase is started as soon as all conditions are met in 32
577 * subsequent status reads.
579 * In case of not stable conditions (RESET/WDOG loop in secured device)
580 * the user is asked for manual pressing of RESET button
583 int cnt_mass_erase_disabled = 0;
585 int64_t ms_start = timeval_ms();
586 bool man_reset_requested = false;
590 int64_t ms_elapsed = timeval_ms() - ms_start;
592 if (!man_reset_requested && ms_elapsed > 100) {
593 LOG_INFO("MDM: Press RESET button now if possible.");
594 man_reset_requested = true;
597 if (ms_elapsed > 3000) {
598 LOG_ERROR("MDM: waiting for mass erase conditions timed out.");
599 LOG_INFO("Mass erase of a secured MCU is not possible without hardware reset.");
600 LOG_INFO("Connect SRST, use 'reset_config srst_only' and retry.");
601 goto deassert_reset_and_exit;
603 retval = kinetis_mdm_read_register(dap, MDM_REG_STAT, &stat);
604 if (retval != ERROR_OK) {
609 if (!(stat & MDM_STAT_FMEEN)) {
611 cnt_mass_erase_disabled++;
612 if (cnt_mass_erase_disabled > 10) {
613 LOG_ERROR("MDM: mass erase is disabled");
614 goto deassert_reset_and_exit;
619 if ((stat & (MDM_STAT_FREADY | MDM_STAT_SYSRES)) == MDM_STAT_FREADY)
624 } while (cnt_ready < 32);
627 * ... Write the MDM-AP control register to set the Flash Mass
628 * Erase in Progress bit. This will start the mass erase
631 retval = kinetis_mdm_write_register(dap, MDM_REG_CTRL, MDM_CTRL_SYS_RES_REQ | MDM_CTRL_FMEIP);
632 if (retval != ERROR_OK) {
633 LOG_ERROR("MDM: failed to start mass erase");
634 goto deassert_reset_and_exit;
638 * ... Read the MDM-AP control register until the Flash Mass
639 * Erase in Progress bit clears...
640 * Data sheed defines erase time <3.6 sec/512kB flash block.
641 * The biggest device has 4 pflash blocks => timeout 16 sec.
643 retval = kinetis_mdm_poll_register(dap, MDM_REG_CTRL, MDM_CTRL_FMEIP, 0, 16000);
644 if (retval != ERROR_OK) {
645 LOG_ERROR("MDM: mass erase timeout");
646 goto deassert_reset_and_exit;
650 /* enable polling in case kinetis_check_flash_security_status disabled it */
651 jtag_poll_set_enabled(true);
655 target->reset_halt = true;
656 target->type->assert_reset(target);
659 * ... Negate the RESET signal or clear the System Reset Request
660 * bit in the MDM-AP control register.
662 retval = kinetis_mdm_write_register(dap, MDM_REG_CTRL, 0);
663 if (retval != ERROR_OK)
664 LOG_ERROR("MDM: failed to clear MDM_REG_CTRL");
666 target->type->deassert_reset(target);
670 deassert_reset_and_exit:
671 kinetis_mdm_write_register(dap, MDM_REG_CTRL, 0);
673 adapter_deassert_reset();
677 static const uint32_t kinetis_known_mdm_ids[] = {
678 0x001C0000, /* Kinetis-K Series */
679 0x001C0020, /* Kinetis-L/M/V/E Series */
680 0x001C0030, /* Kinetis with a Cortex-M7, in time of writing KV58 */
684 * This function implements the procedure to connect to
685 * SWD/JTAG on Kinetis K and L series of devices as it is described in
686 * AN4835 "Production Flash Programming Best Practices for Kinetis K-
687 * and L-series MCUs" Section 4.1.1
689 COMMAND_HANDLER(kinetis_check_flash_security_status)
691 struct target *target = get_current_target(CMD_CTX);
692 struct cortex_m_common *cortex_m = target_to_cm(target);
693 struct adiv5_dap *dap = cortex_m->armv7m.arm.dap;
696 LOG_WARNING("Cannot check flash security status with a high-level adapter");
701 return ERROR_OK; /* too early to check, in JTAG mode ops may not be initialised */
707 * ... The MDM-AP ID register can be read to verify that the
708 * connection is working correctly...
710 retval = kinetis_mdm_read_register(dap, MDM_REG_ID, &val);
711 if (retval != ERROR_OK) {
712 LOG_ERROR("MDM: failed to read ID register");
717 return ERROR_OK; /* dap not yet initialised */
720 for (size_t i = 0; i < ARRAY_SIZE(kinetis_known_mdm_ids); i++) {
721 if (val == kinetis_known_mdm_ids[i]) {
728 LOG_WARNING("MDM: unknown ID %08" PRIX32, val);
731 * ... Read the System Security bit to determine if security is enabled.
732 * If System Security = 0, then proceed. If System Security = 1, then
733 * communication with the internals of the processor, including the
734 * flash, will not be possible without issuing a mass erase command or
735 * unsecuring the part through other means (backdoor key unlock)...
737 retval = kinetis_mdm_read_register(dap, MDM_REG_STAT, &val);
738 if (retval != ERROR_OK) {
739 LOG_ERROR("MDM: failed to read MDM_REG_STAT");
744 * System Security bit is also active for short time during reset.
745 * If a MCU has blank flash and runs in RESET/WDOG loop,
746 * System Security bit is active most of time!
747 * We should observe Flash Ready bit and read status several times
748 * to avoid false detection of secured MCU
750 int secured_score = 0, flash_not_ready_score = 0;
752 if ((val & (MDM_STAT_SYSSEC | MDM_STAT_FREADY)) != MDM_STAT_FREADY) {
756 for (i = 0; i < 32; i++) {
757 stats[i] = MDM_STAT_FREADY;
758 dap_queue_ap_read(dap_ap(dap, MDM_AP), MDM_REG_STAT, &stats[i]);
760 retval = dap_run(dap);
761 if (retval != ERROR_OK) {
762 LOG_DEBUG("MDM: dap_run failed when validating secured state");
765 for (i = 0; i < 32; i++) {
766 if (stats[i] & MDM_STAT_SYSSEC)
768 if (!(stats[i] & MDM_STAT_FREADY))
769 flash_not_ready_score++;
773 if (flash_not_ready_score <= 8 && secured_score > 24) {
774 jtag_poll_set_enabled(false);
776 LOG_WARNING("*********** ATTENTION! ATTENTION! ATTENTION! ATTENTION! **********");
777 LOG_WARNING("**** ****");
778 LOG_WARNING("**** Your Kinetis MCU is in secured state, which means that, ****");
779 LOG_WARNING("**** with exception for very basic communication, JTAG/SWD ****");
780 LOG_WARNING("**** interface will NOT work. In order to restore its ****");
781 LOG_WARNING("**** functionality please issue 'kinetis mdm mass_erase' ****");
782 LOG_WARNING("**** command, power cycle the MCU and restart OpenOCD. ****");
783 LOG_WARNING("**** ****");
784 LOG_WARNING("*********** ATTENTION! ATTENTION! ATTENTION! ATTENTION! **********");
786 } else if (flash_not_ready_score > 24) {
787 jtag_poll_set_enabled(false);
788 LOG_WARNING("**** Your Kinetis MCU is probably locked-up in RESET/WDOG loop. ****");
789 LOG_WARNING("**** Common reason is a blank flash (at least a reset vector). ****");
790 LOG_WARNING("**** Issue 'kinetis mdm halt' command or if SRST is connected ****");
791 LOG_WARNING("**** and configured, use 'reset halt' ****");
792 LOG_WARNING("**** If MCU cannot be halted, it is likely secured and running ****");
793 LOG_WARNING("**** in RESET/WDOG loop. Issue 'kinetis mdm mass_erase' ****");
796 LOG_INFO("MDM: Chip is unsecured. Continuing.");
797 jtag_poll_set_enabled(true);
804 static struct kinetis_chip *kinetis_get_chip(struct target *target)
806 struct flash_bank *bank_iter;
807 struct kinetis_flash_bank *k_bank;
809 /* iterate over all kinetis banks */
810 for (bank_iter = flash_bank_list(); bank_iter; bank_iter = bank_iter->next) {
811 if (bank_iter->driver != &kinetis_flash
812 || bank_iter->target != target)
815 k_bank = bank_iter->driver_priv;
820 return k_bank->k_chip;
825 FLASH_BANK_COMMAND_HANDLER(kinetis_flash_bank_command)
827 struct target *target = bank->target;
828 struct kinetis_chip *k_chip;
829 struct kinetis_flash_bank *k_bank;
832 return ERROR_COMMAND_SYNTAX_ERROR;
834 LOG_INFO("add flash_bank kinetis %s", bank->name);
836 k_chip = kinetis_get_chip(target);
838 if (k_chip == NULL) {
839 k_chip = calloc(sizeof(struct kinetis_chip), 1);
840 if (k_chip == NULL) {
841 LOG_ERROR("No memory");
845 k_chip->target = target;
848 if (k_chip->num_banks >= KINETIS_MAX_BANKS) {
849 LOG_ERROR("Only %u Kinetis flash banks are supported", KINETIS_MAX_BANKS);
853 bank->driver_priv = k_bank = &(k_chip->banks[k_chip->num_banks]);
854 k_bank->k_chip = k_chip;
855 k_bank->bank_number = k_chip->num_banks;
862 /* Disable the watchdog on Kinetis devices */
863 int kinetis_disable_wdog(struct target *target, uint32_t sim_sdid)
865 struct working_area *wdog_algorithm;
866 struct armv7m_algorithm armv7m_info;
870 static const uint8_t kinetis_unlock_wdog_code[] = {
871 #include "../../../contrib/loaders/watchdog/armv7m_kinetis_wdog.inc"
874 /* Decide whether the connected device needs watchdog disabling.
875 * Disable for all Kx and KVx devices, return if it is a KLx */
877 if ((sim_sdid & KINETIS_SDID_SERIESID_MASK) == KINETIS_SDID_SERIESID_KL)
880 /* The connected device requires watchdog disabling. */
881 retval = target_read_u16(target, WDOG_STCTRH, &wdog);
882 if (retval != ERROR_OK)
885 if ((wdog & 0x1) == 0) {
886 /* watchdog already disabled */
889 LOG_INFO("Disabling Kinetis watchdog (initial WDOG_STCTRLH = 0x%x)", wdog);
891 if (target->state != TARGET_HALTED) {
892 LOG_ERROR("Target not halted");
893 return ERROR_TARGET_NOT_HALTED;
896 retval = target_alloc_working_area(target, sizeof(kinetis_unlock_wdog_code), &wdog_algorithm);
897 if (retval != ERROR_OK)
900 retval = target_write_buffer(target, wdog_algorithm->address,
901 sizeof(kinetis_unlock_wdog_code), (uint8_t *)kinetis_unlock_wdog_code);
902 if (retval != ERROR_OK) {
903 target_free_working_area(target, wdog_algorithm);
907 armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
908 armv7m_info.core_mode = ARM_MODE_THREAD;
910 retval = target_run_algorithm(target, 0, NULL, 0, NULL, wdog_algorithm->address,
911 wdog_algorithm->address + (sizeof(kinetis_unlock_wdog_code) - 2),
912 10000, &armv7m_info);
914 if (retval != ERROR_OK)
915 LOG_ERROR("error executing kinetis wdog unlock algorithm");
917 retval = target_read_u16(target, WDOG_STCTRH, &wdog);
918 if (retval != ERROR_OK)
920 LOG_INFO("WDOG_STCTRLH = 0x%x", wdog);
922 target_free_working_area(target, wdog_algorithm);
927 COMMAND_HANDLER(kinetis_disable_wdog_handler)
931 struct target *target = get_current_target(CMD_CTX);
934 return ERROR_COMMAND_SYNTAX_ERROR;
936 result = target_read_u32(target, SIM_SDID, &sim_sdid);
937 if (result != ERROR_OK) {
938 LOG_ERROR("Failed to read SIMSDID");
942 result = kinetis_disable_wdog(target, sim_sdid);
947 static int kinetis_ftfx_decode_error(uint8_t fstat)
950 LOG_ERROR("Flash operation failed, illegal command");
951 return ERROR_FLASH_OPER_UNSUPPORTED;
953 } else if (fstat & 0x10)
954 LOG_ERROR("Flash operation failed, protection violated");
956 else if (fstat & 0x40)
957 LOG_ERROR("Flash operation failed, read collision");
959 else if (fstat & 0x80)
963 LOG_ERROR("Flash operation timed out");
965 return ERROR_FLASH_OPERATION_FAILED;
968 static int kinetis_ftfx_clear_error(struct target *target)
970 /* reset error flags */
971 return target_write_u8(target, FTFx_FSTAT, 0x70);
975 static int kinetis_ftfx_prepare(struct target *target)
980 /* wait until busy */
981 for (i = 0; i < 50; i++) {
982 result = target_read_u8(target, FTFx_FSTAT, &fstat);
983 if (result != ERROR_OK)
990 if ((fstat & 0x80) == 0) {
991 LOG_ERROR("Flash controller is busy");
992 return ERROR_FLASH_OPERATION_FAILED;
995 /* reset error flags */
996 result = kinetis_ftfx_clear_error(target);
1001 /* Kinetis Program-LongWord Microcodes */
1002 static const uint8_t kinetis_flash_write_code[] = {
1003 #include "../../../contrib/loaders/flash/kinetis/kinetis_flash.inc"
1006 /* Program LongWord Block Write */
1007 static int kinetis_write_block(struct flash_bank *bank, const uint8_t *buffer,
1008 uint32_t offset, uint32_t wcount)
1010 struct target *target = bank->target;
1011 uint32_t buffer_size = 2048; /* Default minimum value */
1012 struct working_area *write_algorithm;
1013 struct working_area *source;
1014 struct kinetis_flash_bank *k_bank = bank->driver_priv;
1015 uint32_t address = k_bank->prog_base + offset;
1016 uint32_t end_address;
1017 struct reg_param reg_params[5];
1018 struct armv7m_algorithm armv7m_info;
1022 /* Increase buffer_size if needed */
1023 if (buffer_size < (target->working_area_size/2))
1024 buffer_size = (target->working_area_size/2);
1026 /* allocate working area with flash programming code */
1027 if (target_alloc_working_area(target, sizeof(kinetis_flash_write_code),
1028 &write_algorithm) != ERROR_OK) {
1029 LOG_WARNING("no working area available, can't do block memory writes");
1030 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1033 retval = target_write_buffer(target, write_algorithm->address,
1034 sizeof(kinetis_flash_write_code), kinetis_flash_write_code);
1035 if (retval != ERROR_OK)
1039 while (target_alloc_working_area(target, buffer_size, &source) != ERROR_OK) {
1041 if (buffer_size <= 256) {
1042 /* free working area, write algorithm already allocated */
1043 target_free_working_area(target, write_algorithm);
1045 LOG_WARNING("No large enough working area available, can't do block memory writes");
1046 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE;
1050 armv7m_info.common_magic = ARMV7M_COMMON_MAGIC;
1051 armv7m_info.core_mode = ARM_MODE_THREAD;
1053 init_reg_param(®_params[0], "r0", 32, PARAM_IN_OUT); /* address */
1054 init_reg_param(®_params[1], "r1", 32, PARAM_OUT); /* word count */
1055 init_reg_param(®_params[2], "r2", 32, PARAM_OUT);
1056 init_reg_param(®_params[3], "r3", 32, PARAM_OUT);
1057 init_reg_param(®_params[4], "r4", 32, PARAM_OUT);
1059 buf_set_u32(reg_params[0].value, 0, 32, address);
1060 buf_set_u32(reg_params[1].value, 0, 32, wcount);
1061 buf_set_u32(reg_params[2].value, 0, 32, source->address);
1062 buf_set_u32(reg_params[3].value, 0, 32, source->address + source->size);
1063 buf_set_u32(reg_params[4].value, 0, 32, FTFx_FSTAT);
1065 retval = target_run_flash_async_algorithm(target, buffer, wcount, 4,
1068 source->address, source->size,
1069 write_algorithm->address, 0,
1072 if (retval == ERROR_FLASH_OPERATION_FAILED) {
1073 end_address = buf_get_u32(reg_params[0].value, 0, 32);
1075 LOG_ERROR("Error writing flash at %08" PRIx32, end_address);
1077 retval = target_read_u8(target, FTFx_FSTAT, &fstat);
1078 if (retval == ERROR_OK) {
1079 retval = kinetis_ftfx_decode_error(fstat);
1081 /* reset error flags */
1082 target_write_u8(target, FTFx_FSTAT, 0x70);
1084 } else if (retval != ERROR_OK)
1085 LOG_ERROR("Error executing kinetis Flash programming algorithm");
1087 target_free_working_area(target, source);
1088 target_free_working_area(target, write_algorithm);
1090 destroy_reg_param(®_params[0]);
1091 destroy_reg_param(®_params[1]);
1092 destroy_reg_param(®_params[2]);
1093 destroy_reg_param(®_params[3]);
1094 destroy_reg_param(®_params[4]);
1099 static int kinetis_protect(struct flash_bank *bank, int set, int first, int last)
1103 if (allow_fcf_writes) {
1104 LOG_ERROR("Protection setting is possible with 'kinetis fcf_source protection' only!");
1108 if (!bank->prot_blocks || bank->num_prot_blocks == 0) {
1109 LOG_ERROR("No protection possible for current bank!");
1110 return ERROR_FLASH_BANK_INVALID;
1113 for (i = first; i < bank->num_prot_blocks && i <= last; i++)
1114 bank->prot_blocks[i].is_protected = set;
1116 LOG_INFO("Protection bits will be written at the next FCF sector erase or write.");
1117 LOG_INFO("Do not issue 'flash info' command until protection is written,");
1118 LOG_INFO("doing so would re-read protection status from MCU.");
1123 static int kinetis_protect_check(struct flash_bank *bank)
1125 struct kinetis_flash_bank *k_bank = bank->driver_priv;
1130 if (k_bank->flash_class == FC_PFLASH) {
1132 /* read protection register */
1133 result = target_read_u32(bank->target, FTFx_FPROT3, &fprot);
1134 if (result != ERROR_OK)
1137 /* Every bit protects 1/32 of the full flash (not necessarily just this bank) */
1139 } else if (k_bank->flash_class == FC_FLEX_NVM) {
1142 /* read protection register */
1143 result = target_read_u8(bank->target, FTFx_FDPROT, &fdprot);
1144 if (result != ERROR_OK)
1150 LOG_ERROR("Protection checks for FlexRAM not supported");
1151 return ERROR_FLASH_BANK_INVALID;
1154 b = k_bank->protection_block;
1155 for (i = 0; i < bank->num_prot_blocks; i++) {
1156 if ((fprot >> b) & 1)
1157 bank->prot_blocks[i].is_protected = 0;
1159 bank->prot_blocks[i].is_protected = 1;
1168 static int kinetis_fill_fcf(struct flash_bank *bank, uint8_t *fcf)
1170 uint32_t fprot = 0xffffffff;
1171 uint8_t fsec = 0xfe; /* set MCU unsecure */
1172 uint8_t fdprot = 0xff;
1175 unsigned num_blocks;
1176 uint32_t pflash_bit;
1178 struct flash_bank *bank_iter;
1179 struct kinetis_flash_bank *k_bank = bank->driver_priv;
1180 struct kinetis_chip *k_chip = k_bank->k_chip;
1182 memset(fcf, 0xff, FCF_SIZE);
1187 /* iterate over all kinetis banks */
1188 /* current bank is bank 0, it contains FCF */
1189 num_blocks = k_chip->num_pflash_blocks + k_chip->num_nvm_blocks;
1190 for (bank_idx = 0; bank_idx < num_blocks; bank_idx++) {
1191 k_bank = &(k_chip->banks[bank_idx]);
1192 bank_iter = k_bank->bank;
1194 if (bank_iter == NULL) {
1195 LOG_WARNING("Missing bank %u configuration, FCF protection flags may be incomplette", bank_idx);
1199 kinetis_auto_probe(bank_iter);
1201 if (k_bank->flash_class == FC_PFLASH) {
1202 for (i = 0; i < bank_iter->num_prot_blocks; i++) {
1203 if (bank_iter->prot_blocks[i].is_protected == 1)
1204 fprot &= ~pflash_bit;
1209 } else if (k_bank->flash_class == FC_FLEX_NVM) {
1210 for (i = 0; i < bank_iter->num_prot_blocks; i++) {
1211 if (bank_iter->prot_blocks[i].is_protected == 1)
1212 fdprot &= ~dflash_bit;
1220 target_buffer_set_u32(bank->target, fcf + FCF_FPROT, fprot);
1221 fcf[FCF_FSEC] = fsec;
1222 fcf[FCF_FOPT] = fcf_fopt;
1223 fcf[FCF_FDPROT] = fdprot;
1227 static int kinetis_ftfx_command(struct target *target, uint8_t fcmd, uint32_t faddr,
1228 uint8_t fccob4, uint8_t fccob5, uint8_t fccob6, uint8_t fccob7,
1229 uint8_t fccob8, uint8_t fccob9, uint8_t fccoba, uint8_t fccobb,
1230 uint8_t *ftfx_fstat)
1232 uint8_t command[12] = {faddr & 0xff, (faddr >> 8) & 0xff, (faddr >> 16) & 0xff, fcmd,
1233 fccob7, fccob6, fccob5, fccob4,
1234 fccobb, fccoba, fccob9, fccob8};
1237 int64_t ms_timeout = timeval_ms() + 250;
1239 result = target_write_memory(target, FTFx_FCCOB3, 4, 3, command);
1240 if (result != ERROR_OK)
1244 result = target_write_u8(target, FTFx_FSTAT, 0x80);
1245 if (result != ERROR_OK)
1250 result = target_read_u8(target, FTFx_FSTAT, &fstat);
1252 if (result != ERROR_OK)
1258 } while (timeval_ms() < ms_timeout);
1261 *ftfx_fstat = fstat;
1263 if ((fstat & 0xf0) != 0x80) {
1264 LOG_DEBUG("ftfx command failed FSTAT: %02X FCCOB: %02X%02X%02X%02X %02X%02X%02X%02X %02X%02X%02X%02X",
1265 fstat, command[3], command[2], command[1], command[0],
1266 command[7], command[6], command[5], command[4],
1267 command[11], command[10], command[9], command[8]);
1269 return kinetis_ftfx_decode_error(fstat);
1276 static int kinetis_check_run_mode(struct target *target)
1279 uint8_t pmctrl, pmstat;
1281 if (target->state != TARGET_HALTED) {
1282 LOG_ERROR("Target not halted");
1283 return ERROR_TARGET_NOT_HALTED;
1286 result = target_read_u8(target, SMC_PMSTAT, &pmstat);
1287 if (result != ERROR_OK)
1290 if (pmstat == PM_STAT_RUN)
1293 if (pmstat == PM_STAT_VLPR) {
1294 /* It is safe to switch from VLPR to RUN mode without changing clock */
1295 LOG_INFO("Switching from VLPR to RUN mode.");
1296 pmctrl = PM_CTRL_RUNM_RUN;
1297 result = target_write_u8(target, SMC_PMCTRL, pmctrl);
1298 if (result != ERROR_OK)
1301 for (i = 100; i; i--) {
1302 result = target_read_u8(target, SMC_PMSTAT, &pmstat);
1303 if (result != ERROR_OK)
1306 if (pmstat == PM_STAT_RUN)
1311 LOG_ERROR("Flash operation not possible in current run mode: SMC_PMSTAT: 0x%x", pmstat);
1312 LOG_ERROR("Issue a 'reset init' command.");
1313 return ERROR_TARGET_NOT_HALTED;
1317 static void kinetis_invalidate_flash_cache(struct kinetis_chip *k_chip)
1319 struct target *target = k_chip->target;
1321 if (k_chip->flash_support & FS_INVALIDATE_CACHE_K)
1322 target_write_u8(target, FMC_PFB01CR + 2, 0xf0);
1323 /* Set CINV_WAY bits - request invalidate of all cache ways */
1324 /* FMC_PFB0CR has same address and CINV_WAY bits as FMC_PFB01CR */
1326 else if (k_chip->flash_support & FS_INVALIDATE_CACHE_L)
1327 target_write_u8(target, MCM_PLACR + 1, 0x04);
1328 /* set bit CFCC - Clear Flash Controller Cache */
1330 else if (k_chip->flash_support & FS_INVALIDATE_CACHE_MSCM)
1331 target_write_u32(target, MSCM_OCMDR0, 0x30);
1332 /* disable data prefetch and flash speculate */
1338 static int kinetis_erase(struct flash_bank *bank, int first, int last)
1341 struct kinetis_flash_bank *k_bank = bank->driver_priv;
1343 result = kinetis_check_run_mode(bank->target);
1344 if (result != ERROR_OK)
1347 /* reset error flags */
1348 result = kinetis_ftfx_prepare(bank->target);
1349 if (result != ERROR_OK)
1352 if ((first > bank->num_sectors) || (last > bank->num_sectors))
1353 return ERROR_FLASH_OPERATION_FAILED;
1356 * FIXME: TODO: use the 'Erase Flash Block' command if the
1357 * requested erase is PFlash or NVM and encompasses the entire
1358 * block. Should be quicker.
1360 for (i = first; i <= last; i++) {
1361 /* set command and sector address */
1362 result = kinetis_ftfx_command(bank->target, FTFx_CMD_SECTERASE, k_bank->prog_base + bank->sectors[i].offset,
1363 0, 0, 0, 0, 0, 0, 0, 0, NULL);
1365 if (result != ERROR_OK) {
1366 LOG_WARNING("erase sector %d failed", i);
1367 return ERROR_FLASH_OPERATION_FAILED;
1370 bank->sectors[i].is_erased = 1;
1372 if (k_bank->prog_base == 0
1373 && bank->sectors[i].offset <= FCF_ADDRESS
1374 && bank->sectors[i].offset + bank->sectors[i].size > FCF_ADDRESS + FCF_SIZE) {
1375 if (allow_fcf_writes) {
1376 LOG_WARNING("Flash Configuration Field erased, DO NOT reset or power off the device");
1377 LOG_WARNING("until correct FCF is programmed or MCU gets security lock.");
1379 uint8_t fcf_buffer[FCF_SIZE];
1381 kinetis_fill_fcf(bank, fcf_buffer);
1382 result = kinetis_write_inner(bank, fcf_buffer, FCF_ADDRESS, FCF_SIZE);
1383 if (result != ERROR_OK)
1384 LOG_WARNING("Flash Configuration Field write failed");
1385 bank->sectors[i].is_erased = 0;
1390 kinetis_invalidate_flash_cache(k_bank->k_chip);
1395 static int kinetis_make_ram_ready(struct target *target)
1400 /* check if ram ready */
1401 result = target_read_u8(target, FTFx_FCNFG, &ftfx_fcnfg);
1402 if (result != ERROR_OK)
1405 if (ftfx_fcnfg & (1 << 1))
1406 return ERROR_OK; /* ram ready */
1408 /* make flex ram available */
1409 result = kinetis_ftfx_command(target, FTFx_CMD_SETFLEXRAM, 0x00ff0000,
1410 0, 0, 0, 0, 0, 0, 0, 0, NULL);
1411 if (result != ERROR_OK)
1412 return ERROR_FLASH_OPERATION_FAILED;
1415 result = target_read_u8(target, FTFx_FCNFG, &ftfx_fcnfg);
1416 if (result != ERROR_OK)
1419 if (ftfx_fcnfg & (1 << 1))
1420 return ERROR_OK; /* ram ready */
1422 return ERROR_FLASH_OPERATION_FAILED;
1426 static int kinetis_write_sections(struct flash_bank *bank, const uint8_t *buffer,
1427 uint32_t offset, uint32_t count)
1429 int result = ERROR_OK;
1430 struct kinetis_flash_bank *k_bank = bank->driver_priv;
1431 struct kinetis_chip *k_chip = k_bank->k_chip;
1432 uint8_t *buffer_aligned = NULL;
1434 * Kinetis uses different terms for the granularity of
1435 * sector writes, e.g. "phrase" or "128 bits". We use
1436 * the generic term "chunk". The largest possible
1437 * Kinetis "chunk" is 16 bytes (128 bits).
1439 uint32_t prog_section_chunk_bytes = k_bank->sector_size >> 8;
1440 uint32_t prog_size_bytes = k_chip->max_flash_prog_size;
1443 uint32_t size = prog_size_bytes - offset % prog_size_bytes;
1444 uint32_t align_begin = offset % prog_section_chunk_bytes;
1446 uint32_t size_aligned;
1447 uint16_t chunk_count;
1453 align_end = (align_begin + size) % prog_section_chunk_bytes;
1455 align_end = prog_section_chunk_bytes - align_end;
1457 size_aligned = align_begin + size + align_end;
1458 chunk_count = size_aligned / prog_section_chunk_bytes;
1460 if (size != size_aligned) {
1461 /* aligned section: the first, the last or the only */
1462 if (!buffer_aligned)
1463 buffer_aligned = malloc(prog_size_bytes);
1465 memset(buffer_aligned, 0xff, size_aligned);
1466 memcpy(buffer_aligned + align_begin, buffer, size);
1468 result = target_write_memory(bank->target, k_chip->progr_accel_ram,
1469 4, size_aligned / 4, buffer_aligned);
1471 LOG_DEBUG("section @ %08" PRIx32 " aligned begin %" PRIu32 ", end %" PRIu32,
1472 bank->base + offset, align_begin, align_end);
1474 result = target_write_memory(bank->target, k_chip->progr_accel_ram,
1475 4, size_aligned / 4, buffer);
1477 LOG_DEBUG("write section @ %08" PRIx32 " with length %" PRIu32 " bytes",
1478 bank->base + offset, size);
1480 if (result != ERROR_OK) {
1481 LOG_ERROR("target_write_memory failed");
1485 /* execute section-write command */
1486 result = kinetis_ftfx_command(bank->target, FTFx_CMD_SECTWRITE,
1487 k_bank->prog_base + offset - align_begin,
1488 chunk_count>>8, chunk_count, 0, 0,
1489 0, 0, 0, 0, &ftfx_fstat);
1491 if (result != ERROR_OK) {
1492 LOG_ERROR("Error writing section at %08" PRIx32, bank->base + offset);
1496 if (ftfx_fstat & 0x01) {
1497 LOG_ERROR("Flash write error at %08" PRIx32, bank->base + offset);
1498 if (k_bank->prog_base == 0 && offset == FCF_ADDRESS + FCF_SIZE
1499 && (k_chip->flash_support & FS_WIDTH_256BIT)) {
1500 LOG_ERROR("Flash write immediately after the end of Flash Config Field shows error");
1501 LOG_ERROR("because the flash memory is 256 bits wide (data were written correctly).");
1502 LOG_ERROR("Either change the linker script to add a gap of 16 bytes after FCF");
1503 LOG_ERROR("or set 'kinetis fcf_source write'");
1512 free(buffer_aligned);
1517 static int kinetis_write_inner(struct flash_bank *bank, const uint8_t *buffer,
1518 uint32_t offset, uint32_t count)
1520 int result, fallback = 0;
1521 struct kinetis_flash_bank *k_bank = bank->driver_priv;
1522 struct kinetis_chip *k_chip = k_bank->k_chip;
1524 if (!(k_chip->flash_support & FS_PROGRAM_SECTOR)) {
1525 /* fallback to longword write */
1527 LOG_INFO("This device supports Program Longword execution only.");
1529 result = kinetis_make_ram_ready(bank->target);
1530 if (result != ERROR_OK) {
1532 LOG_WARNING("FlexRAM not ready, fallback to slow longword write.");
1536 LOG_DEBUG("flash write @ %08" PRIx32, bank->base + offset);
1538 if (fallback == 0) {
1539 /* program section command */
1540 kinetis_write_sections(bank, buffer, offset, count);
1541 } else if (k_chip->flash_support & FS_PROGRAM_LONGWORD) {
1542 /* program longword command, not supported in FTFE */
1543 uint8_t *new_buffer = NULL;
1545 /* check word alignment */
1547 LOG_ERROR("offset 0x%" PRIx32 " breaks the required alignment", offset);
1548 return ERROR_FLASH_DST_BREAKS_ALIGNMENT;
1552 uint32_t old_count = count;
1553 count = (old_count | 3) + 1;
1554 new_buffer = malloc(count);
1555 if (new_buffer == NULL) {
1556 LOG_ERROR("odd number of bytes to write and no memory "
1557 "for padding buffer");
1560 LOG_INFO("odd number of bytes to write (%" PRIu32 "), extending to %" PRIu32 " "
1561 "and padding with 0xff", old_count, count);
1562 memset(new_buffer + old_count, 0xff, count - old_count);
1563 buffer = memcpy(new_buffer, buffer, old_count);
1566 uint32_t words_remaining = count / 4;
1568 kinetis_disable_wdog(bank->target, k_chip->sim_sdid);
1570 /* try using a block write */
1571 result = kinetis_write_block(bank, buffer, offset, words_remaining);
1573 if (result == ERROR_TARGET_RESOURCE_NOT_AVAILABLE) {
1574 /* if block write failed (no sufficient working area),
1575 * we use normal (slow) single word accesses */
1576 LOG_WARNING("couldn't use block writes, falling back to single "
1579 while (words_remaining) {
1582 LOG_DEBUG("write longword @ %08" PRIx32, (uint32_t)(bank->base + offset));
1584 result = kinetis_ftfx_command(bank->target, FTFx_CMD_LWORDPROG, k_bank->prog_base + offset,
1585 buffer[3], buffer[2], buffer[1], buffer[0],
1586 0, 0, 0, 0, &ftfx_fstat);
1588 if (result != ERROR_OK) {
1589 LOG_ERROR("Error writing longword at %08" PRIx32, bank->base + offset);
1593 if (ftfx_fstat & 0x01)
1594 LOG_ERROR("Flash write error at %08" PRIx32, bank->base + offset);
1603 LOG_ERROR("Flash write strategy not implemented");
1604 return ERROR_FLASH_OPERATION_FAILED;
1607 kinetis_invalidate_flash_cache(k_chip);
1612 static int kinetis_write(struct flash_bank *bank, const uint8_t *buffer,
1613 uint32_t offset, uint32_t count)
1616 bool set_fcf = false;
1618 struct kinetis_flash_bank *k_bank = bank->driver_priv;
1620 result = kinetis_check_run_mode(bank->target);
1621 if (result != ERROR_OK)
1624 /* reset error flags */
1625 result = kinetis_ftfx_prepare(bank->target);
1626 if (result != ERROR_OK)
1629 if (k_bank->prog_base == 0 && !allow_fcf_writes) {
1630 if (bank->sectors[1].offset <= FCF_ADDRESS)
1631 sect = 1; /* 1kb sector, FCF in 2nd sector */
1633 if (offset < bank->sectors[sect].offset + bank->sectors[sect].size
1634 && offset + count > bank->sectors[sect].offset)
1635 set_fcf = true; /* write to any part of sector with FCF */
1639 uint8_t fcf_buffer[FCF_SIZE];
1640 uint8_t fcf_current[FCF_SIZE];
1642 kinetis_fill_fcf(bank, fcf_buffer);
1644 if (offset < FCF_ADDRESS) {
1645 /* write part preceding FCF */
1646 result = kinetis_write_inner(bank, buffer, offset, FCF_ADDRESS - offset);
1647 if (result != ERROR_OK)
1651 result = target_read_memory(bank->target, bank->base + FCF_ADDRESS, 4, FCF_SIZE / 4, fcf_current);
1652 if (result == ERROR_OK && memcmp(fcf_current, fcf_buffer, FCF_SIZE) == 0)
1656 /* write FCF if differs from flash - eliminate multiple writes */
1657 result = kinetis_write_inner(bank, fcf_buffer, FCF_ADDRESS, FCF_SIZE);
1658 if (result != ERROR_OK)
1662 LOG_WARNING("Flash Configuration Field written.");
1663 LOG_WARNING("Reset or power off the device to make settings effective.");
1665 if (offset + count > FCF_ADDRESS + FCF_SIZE) {
1666 uint32_t delta = FCF_ADDRESS + FCF_SIZE - offset;
1667 /* write part after FCF */
1668 result = kinetis_write_inner(bank, buffer + delta, FCF_ADDRESS + FCF_SIZE, count - delta);
1673 /* no FCF fiddling, normal write */
1674 return kinetis_write_inner(bank, buffer, offset, count);
1678 static int kinetis_probe_chip(struct kinetis_chip *k_chip)
1681 uint8_t fcfg1_nvmsize, fcfg1_pfsize, fcfg1_eesize, fcfg1_depart;
1682 uint8_t fcfg2_pflsh;
1683 uint32_t ee_size = 0;
1684 uint32_t pflash_size_k, nvm_size_k, dflash_size_k;
1685 uint32_t pflash_size_m;
1686 unsigned num_blocks = 0;
1687 unsigned maxaddr_shift = 13;
1688 struct target *target = k_chip->target;
1690 unsigned familyid = 0, subfamid = 0;
1691 unsigned cpu_mhz = 120;
1693 bool use_nvm_marking = false;
1694 char flash_marking[8], nvm_marking[2];
1697 k_chip->probed = false;
1698 k_chip->pflash_sector_size = 0;
1699 k_chip->pflash_base = 0;
1700 k_chip->nvm_base = 0x10000000;
1701 k_chip->progr_accel_ram = FLEXRAM;
1705 result = target_read_u32(target, SIM_SDID, &k_chip->sim_sdid);
1706 if (result != ERROR_OK)
1709 if ((k_chip->sim_sdid & (~KINETIS_SDID_K_SERIES_MASK)) == 0) {
1710 /* older K-series MCU */
1711 uint32_t mcu_type = k_chip->sim_sdid & KINETIS_K_SDID_TYPE_MASK;
1714 case KINETIS_K_SDID_K10_M50:
1715 case KINETIS_K_SDID_K20_M50:
1717 k_chip->pflash_sector_size = 1<<10;
1718 k_chip->nvm_sector_size = 1<<10;
1720 k_chip->flash_support = FS_PROGRAM_LONGWORD | FS_PROGRAM_SECTOR | FS_INVALIDATE_CACHE_K;
1722 case KINETIS_K_SDID_K10_M72:
1723 case KINETIS_K_SDID_K20_M72:
1724 case KINETIS_K_SDID_K30_M72:
1725 case KINETIS_K_SDID_K30_M100:
1726 case KINETIS_K_SDID_K40_M72:
1727 case KINETIS_K_SDID_K40_M100:
1728 case KINETIS_K_SDID_K50_M72:
1729 /* 2kB sectors, 1kB FlexNVM sectors */
1730 k_chip->pflash_sector_size = 2<<10;
1731 k_chip->nvm_sector_size = 1<<10;
1733 k_chip->flash_support = FS_PROGRAM_LONGWORD | FS_PROGRAM_SECTOR | FS_INVALIDATE_CACHE_K;
1734 k_chip->max_flash_prog_size = 1<<10;
1736 case KINETIS_K_SDID_K10_M100:
1737 case KINETIS_K_SDID_K20_M100:
1738 case KINETIS_K_SDID_K11:
1739 case KINETIS_K_SDID_K12:
1740 case KINETIS_K_SDID_K21_M50:
1741 case KINETIS_K_SDID_K22_M50:
1742 case KINETIS_K_SDID_K51_M72:
1743 case KINETIS_K_SDID_K53:
1744 case KINETIS_K_SDID_K60_M100:
1746 k_chip->pflash_sector_size = 2<<10;
1747 k_chip->nvm_sector_size = 2<<10;
1749 k_chip->flash_support = FS_PROGRAM_LONGWORD | FS_PROGRAM_SECTOR | FS_INVALIDATE_CACHE_K;
1751 case KINETIS_K_SDID_K21_M120:
1752 case KINETIS_K_SDID_K22_M120:
1753 /* 4kB sectors (MK21FN1M0, MK21FX512, MK22FN1M0, MK22FX512) */
1754 k_chip->pflash_sector_size = 4<<10;
1755 k_chip->max_flash_prog_size = 1<<10;
1756 k_chip->nvm_sector_size = 4<<10;
1758 k_chip->flash_support = FS_PROGRAM_PHRASE | FS_PROGRAM_SECTOR | FS_INVALIDATE_CACHE_K;
1760 case KINETIS_K_SDID_K10_M120:
1761 case KINETIS_K_SDID_K20_M120:
1762 case KINETIS_K_SDID_K60_M150:
1763 case KINETIS_K_SDID_K70_M150:
1765 k_chip->pflash_sector_size = 4<<10;
1766 k_chip->nvm_sector_size = 4<<10;
1768 k_chip->flash_support = FS_PROGRAM_PHRASE | FS_PROGRAM_SECTOR | FS_INVALIDATE_CACHE_K;
1771 LOG_ERROR("Unsupported K-family FAMID");
1774 for (idx = 0; idx < ARRAY_SIZE(kinetis_types_old); idx++) {
1775 if (kinetis_types_old[idx].sdid == mcu_type) {
1776 strcpy(name, kinetis_types_old[idx].name);
1777 use_nvm_marking = true;
1783 /* Newer K-series or KL series MCU */
1784 familyid = (k_chip->sim_sdid & KINETIS_SDID_FAMILYID_MASK) >> KINETIS_SDID_FAMILYID_SHIFT;
1785 subfamid = (k_chip->sim_sdid & KINETIS_SDID_SUBFAMID_MASK) >> KINETIS_SDID_SUBFAMID_SHIFT;
1787 switch (k_chip->sim_sdid & KINETIS_SDID_SERIESID_MASK) {
1788 case KINETIS_SDID_SERIESID_K:
1789 use_nvm_marking = true;
1790 switch (k_chip->sim_sdid & (KINETIS_SDID_FAMILYID_MASK | KINETIS_SDID_SUBFAMID_MASK)) {
1791 case KINETIS_SDID_FAMILYID_K0X | KINETIS_SDID_SUBFAMID_KX2:
1792 /* K02FN64, K02FN128: FTFA, 2kB sectors */
1793 k_chip->pflash_sector_size = 2<<10;
1795 k_chip->flash_support = FS_PROGRAM_LONGWORD | FS_INVALIDATE_CACHE_K;
1799 case KINETIS_SDID_FAMILYID_K2X | KINETIS_SDID_SUBFAMID_KX2: {
1800 /* MK24FN1M reports as K22, this should detect it (according to errata note 1N83J) */
1802 result = target_read_u32(target, SIM_SOPT1, &sopt1);
1803 if (result != ERROR_OK)
1806 if (((k_chip->sim_sdid & (KINETIS_SDID_DIEID_MASK)) == KINETIS_SDID_DIEID_K24FN1M) &&
1807 ((sopt1 & KINETIS_SOPT1_RAMSIZE_MASK) == KINETIS_SOPT1_RAMSIZE_K24FN1M)) {
1809 k_chip->pflash_sector_size = 4<<10;
1811 k_chip->flash_support = FS_PROGRAM_PHRASE | FS_PROGRAM_SECTOR | FS_INVALIDATE_CACHE_K;
1812 k_chip->max_flash_prog_size = 1<<10;
1813 subfamid = 4; /* errata 1N83J fix */
1816 if ((k_chip->sim_sdid & (KINETIS_SDID_DIEID_MASK)) == KINETIS_SDID_DIEID_K22FN128
1817 || (k_chip->sim_sdid & (KINETIS_SDID_DIEID_MASK)) == KINETIS_SDID_DIEID_K22FN256
1818 || (k_chip->sim_sdid & (KINETIS_SDID_DIEID_MASK)) == KINETIS_SDID_DIEID_K22FN512) {
1819 /* K22 with new-style SDID - smaller pflash with FTFA, 2kB sectors */
1820 k_chip->pflash_sector_size = 2<<10;
1821 /* autodetect 1 or 2 blocks */
1822 k_chip->flash_support = FS_PROGRAM_LONGWORD | FS_INVALIDATE_CACHE_K;
1825 LOG_ERROR("Unsupported Kinetis K22 DIEID");
1828 case KINETIS_SDID_FAMILYID_K2X | KINETIS_SDID_SUBFAMID_KX4:
1829 k_chip->pflash_sector_size = 4<<10;
1830 if ((k_chip->sim_sdid & (KINETIS_SDID_DIEID_MASK)) == KINETIS_SDID_DIEID_K24FN256) {
1831 /* K24FN256 - smaller pflash with FTFA */
1833 k_chip->flash_support = FS_PROGRAM_LONGWORD | FS_INVALIDATE_CACHE_K;
1836 /* K24FN1M without errata 7534 */
1838 k_chip->flash_support = FS_PROGRAM_PHRASE | FS_PROGRAM_SECTOR | FS_INVALIDATE_CACHE_K;
1839 k_chip->max_flash_prog_size = 1<<10;
1842 case KINETIS_SDID_FAMILYID_K6X | KINETIS_SDID_SUBFAMID_KX1: /* errata 7534 - should be K63 */
1843 case KINETIS_SDID_FAMILYID_K6X | KINETIS_SDID_SUBFAMID_KX2: /* errata 7534 - should be K64 */
1844 subfamid += 2; /* errata 7534 fix */
1845 case KINETIS_SDID_FAMILYID_K6X | KINETIS_SDID_SUBFAMID_KX3:
1847 case KINETIS_SDID_FAMILYID_K6X | KINETIS_SDID_SUBFAMID_KX4:
1848 /* K64FN1M0, K64FX512 */
1849 k_chip->pflash_sector_size = 4<<10;
1850 k_chip->nvm_sector_size = 4<<10;
1851 k_chip->max_flash_prog_size = 1<<10;
1853 k_chip->flash_support = FS_PROGRAM_PHRASE | FS_PROGRAM_SECTOR | FS_INVALIDATE_CACHE_K;
1856 case KINETIS_SDID_FAMILYID_K2X | KINETIS_SDID_SUBFAMID_KX6:
1858 case KINETIS_SDID_FAMILYID_K6X | KINETIS_SDID_SUBFAMID_KX6:
1859 /* K66FN2M0, K66FX1M0 */
1860 k_chip->pflash_sector_size = 4<<10;
1861 k_chip->nvm_sector_size = 4<<10;
1862 k_chip->max_flash_prog_size = 1<<10;
1864 k_chip->flash_support = FS_PROGRAM_PHRASE | FS_PROGRAM_SECTOR | FS_INVALIDATE_CACHE_K;
1868 case KINETIS_SDID_FAMILYID_K8X | KINETIS_SDID_SUBFAMID_KX0:
1869 case KINETIS_SDID_FAMILYID_K8X | KINETIS_SDID_SUBFAMID_KX1:
1870 case KINETIS_SDID_FAMILYID_K8X | KINETIS_SDID_SUBFAMID_KX2:
1871 /* K80FN256, K81FN256, K82FN256 */
1872 k_chip->pflash_sector_size = 4<<10;
1874 k_chip->flash_support = FS_PROGRAM_LONGWORD | FS_INVALIDATE_CACHE_K | FS_NO_CMD_BLOCKSTAT;
1878 case KINETIS_SDID_FAMILYID_KL8X | KINETIS_SDID_SUBFAMID_KX1:
1879 case KINETIS_SDID_FAMILYID_KL8X | KINETIS_SDID_SUBFAMID_KX2:
1880 /* KL81Z128, KL82Z128 */
1881 k_chip->pflash_sector_size = 2<<10;
1883 k_chip->flash_support = FS_PROGRAM_LONGWORD | FS_INVALIDATE_CACHE_L | FS_NO_CMD_BLOCKSTAT;
1884 use_nvm_marking = false;
1885 snprintf(name, sizeof(name), "MKL8%uZ%%s7",
1890 LOG_ERROR("Unsupported Kinetis FAMILYID SUBFAMID");
1893 if (name[0] == '\0')
1894 snprintf(name, sizeof(name), "MK%u%uF%%s%u",
1895 familyid, subfamid, cpu_mhz / 10);
1898 case KINETIS_SDID_SERIESID_KL:
1900 k_chip->pflash_sector_size = 1<<10;
1901 k_chip->nvm_sector_size = 1<<10;
1902 /* autodetect 1 or 2 blocks */
1903 k_chip->flash_support = FS_PROGRAM_LONGWORD | FS_INVALIDATE_CACHE_L;
1906 if (subfamid == 3 && (familyid == 1 || familyid == 2))
1908 snprintf(name, sizeof(name), "MKL%u%uZ%%s%u",
1909 familyid, subfamid, cpu_mhz / 10);
1912 case KINETIS_SDID_SERIESID_KV:
1914 switch (k_chip->sim_sdid & (KINETIS_SDID_FAMILYID_MASK | KINETIS_SDID_SUBFAMID_MASK)) {
1915 case KINETIS_SDID_FAMILYID_K1X | KINETIS_SDID_SUBFAMID_KX0:
1916 /* KV10: FTFA, 1kB sectors */
1917 k_chip->pflash_sector_size = 1<<10;
1919 k_chip->flash_support = FS_PROGRAM_LONGWORD | FS_INVALIDATE_CACHE_L;
1920 strcpy(name, "MKV10Z%s7");
1923 case KINETIS_SDID_FAMILYID_K1X | KINETIS_SDID_SUBFAMID_KX1:
1924 /* KV11: FTFA, 2kB sectors */
1925 k_chip->pflash_sector_size = 2<<10;
1927 k_chip->flash_support = FS_PROGRAM_LONGWORD | FS_INVALIDATE_CACHE_L;
1928 strcpy(name, "MKV11Z%s7");
1931 case KINETIS_SDID_FAMILYID_K3X | KINETIS_SDID_SUBFAMID_KX0:
1932 /* KV30: FTFA, 2kB sectors, 1 block */
1933 case KINETIS_SDID_FAMILYID_K3X | KINETIS_SDID_SUBFAMID_KX1:
1934 /* KV31: FTFA, 2kB sectors, 2 blocks */
1935 k_chip->pflash_sector_size = 2<<10;
1936 /* autodetect 1 or 2 blocks */
1937 k_chip->flash_support = FS_PROGRAM_LONGWORD | FS_INVALIDATE_CACHE_K;
1940 case KINETIS_SDID_FAMILYID_K4X | KINETIS_SDID_SUBFAMID_KX2:
1941 case KINETIS_SDID_FAMILYID_K4X | KINETIS_SDID_SUBFAMID_KX4:
1942 case KINETIS_SDID_FAMILYID_K4X | KINETIS_SDID_SUBFAMID_KX6:
1943 /* KV4x: FTFA, 4kB sectors */
1944 k_chip->pflash_sector_size = 4<<10;
1946 k_chip->flash_support = FS_PROGRAM_LONGWORD | FS_INVALIDATE_CACHE_K;
1950 case KINETIS_SDID_FAMILYID_K5X | KINETIS_SDID_SUBFAMID_KX6:
1951 case KINETIS_SDID_FAMILYID_K5X | KINETIS_SDID_SUBFAMID_KX8:
1952 /* KV5x: FTFE, 8kB sectors */
1953 k_chip->pflash_sector_size = 8<<10;
1954 k_chip->max_flash_prog_size = 1<<10;
1957 k_chip->flash_support = FS_PROGRAM_PHRASE | FS_PROGRAM_SECTOR | FS_WIDTH_256BIT;
1958 k_chip->pflash_base = 0x10000000;
1959 k_chip->progr_accel_ram = 0x18000000;
1964 LOG_ERROR("Unsupported KV FAMILYID SUBFAMID");
1967 if (name[0] == '\0')
1968 snprintf(name, sizeof(name), "MKV%u%uF%%s%u",
1969 familyid, subfamid, cpu_mhz / 10);
1972 case KINETIS_SDID_SERIESID_KE:
1974 switch (k_chip->sim_sdid &
1975 (KINETIS_SDID_FAMILYID_MASK | KINETIS_SDID_SUBFAMID_MASK | KINETIS_SDID_PROJECTID_MASK)) {
1976 case KINETIS_SDID_FAMILYID_K1X | KINETIS_SDID_SUBFAMID_KX4 | KINETIS_SDID_PROJECTID_KE1xZ:
1977 case KINETIS_SDID_FAMILYID_K1X | KINETIS_SDID_SUBFAMID_KX5 | KINETIS_SDID_PROJECTID_KE1xZ:
1978 /* KE1xZ: FTFE, 2kB sectors */
1979 k_chip->pflash_sector_size = 2<<10;
1980 k_chip->nvm_sector_size = 2<<10;
1981 k_chip->max_flash_prog_size = 1<<9;
1983 k_chip->flash_support = FS_PROGRAM_PHRASE | FS_PROGRAM_SECTOR | FS_INVALIDATE_CACHE_L;
1986 snprintf(name, sizeof(name), "MKE%u%uZ%%s%u",
1987 familyid, subfamid, cpu_mhz / 10);
1990 case KINETIS_SDID_FAMILYID_K1X | KINETIS_SDID_SUBFAMID_KX4 | KINETIS_SDID_PROJECTID_KE1xF:
1991 case KINETIS_SDID_FAMILYID_K1X | KINETIS_SDID_SUBFAMID_KX6 | KINETIS_SDID_PROJECTID_KE1xF:
1992 case KINETIS_SDID_FAMILYID_K1X | KINETIS_SDID_SUBFAMID_KX8 | KINETIS_SDID_PROJECTID_KE1xF:
1993 /* KE1xF: FTFE, 4kB sectors */
1994 k_chip->pflash_sector_size = 4<<10;
1995 k_chip->nvm_sector_size = 2<<10;
1996 k_chip->max_flash_prog_size = 1<<10;
1998 k_chip->flash_support = FS_PROGRAM_PHRASE | FS_PROGRAM_SECTOR | FS_INVALIDATE_CACHE_MSCM;
2001 snprintf(name, sizeof(name), "MKE%u%uF%%s%u",
2002 familyid, subfamid, cpu_mhz / 10);
2006 LOG_ERROR("Unsupported KE FAMILYID SUBFAMID");
2011 LOG_ERROR("Unsupported K-series");
2015 if (k_chip->pflash_sector_size == 0) {
2016 LOG_ERROR("MCU is unsupported, SDID 0x%08" PRIx32, k_chip->sim_sdid);
2017 return ERROR_FLASH_OPER_UNSUPPORTED;
2020 result = target_read_u32(target, SIM_FCFG1, &k_chip->sim_fcfg1);
2021 if (result != ERROR_OK)
2024 result = target_read_u32(target, SIM_FCFG2, &k_chip->sim_fcfg2);
2025 if (result != ERROR_OK)
2028 LOG_DEBUG("SDID: 0x%08" PRIX32 " FCFG1: 0x%08" PRIX32 " FCFG2: 0x%08" PRIX32, k_chip->sim_sdid,
2029 k_chip->sim_fcfg1, k_chip->sim_fcfg2);
2031 fcfg1_nvmsize = (uint8_t)((k_chip->sim_fcfg1 >> 28) & 0x0f);
2032 fcfg1_pfsize = (uint8_t)((k_chip->sim_fcfg1 >> 24) & 0x0f);
2033 fcfg1_eesize = (uint8_t)((k_chip->sim_fcfg1 >> 16) & 0x0f);
2034 fcfg1_depart = (uint8_t)((k_chip->sim_fcfg1 >> 8) & 0x0f);
2036 fcfg2_pflsh = (uint8_t)((k_chip->sim_fcfg2 >> 23) & 0x01);
2037 k_chip->fcfg2_maxaddr0_shifted = ((k_chip->sim_fcfg2 >> 24) & 0x7f) << maxaddr_shift;
2038 k_chip->fcfg2_maxaddr1_shifted = ((k_chip->sim_fcfg2 >> 16) & 0x7f) << maxaddr_shift;
2040 if (num_blocks == 0)
2041 num_blocks = k_chip->fcfg2_maxaddr1_shifted ? 2 : 1;
2042 else if (k_chip->fcfg2_maxaddr1_shifted == 0 && num_blocks >= 2) {
2044 LOG_WARNING("MAXADDR1 is zero, number of flash banks adjusted to 1");
2045 } else if (k_chip->fcfg2_maxaddr1_shifted != 0 && num_blocks == 1) {
2047 LOG_WARNING("MAXADDR1 is non zero, number of flash banks adjusted to 2");
2050 /* when the PFLSH bit is set, there is no FlexNVM/FlexRAM */
2052 switch (fcfg1_nvmsize) {
2058 k_chip->nvm_size = 1 << (14 + (fcfg1_nvmsize >> 1));
2061 if (k_chip->pflash_sector_size >= 4<<10)
2062 k_chip->nvm_size = 512<<10;
2065 k_chip->nvm_size = 256<<10;
2068 k_chip->nvm_size = 0;
2072 switch (fcfg1_eesize) {
2083 ee_size = (16 << (10 - fcfg1_eesize));
2090 switch (fcfg1_depart) {
2097 k_chip->dflash_size = k_chip->nvm_size - (4096 << fcfg1_depart);
2100 k_chip->dflash_size = 0;
2107 k_chip->dflash_size = 4096 << (fcfg1_depart & 0x7);
2110 k_chip->dflash_size = k_chip->nvm_size;
2115 switch (fcfg1_pfsize) {
2122 k_chip->pflash_size = 1 << (14 + (fcfg1_pfsize >> 1));
2125 /* a peculiar case: Freescale states different sizes for 0xf
2126 * K02P64M100SFARM 128 KB ... duplicate of code 0x7
2127 * K22P121M120SF8RM 256 KB ... duplicate of code 0x9
2128 * K22P121M120SF7RM 512 KB ... duplicate of code 0xb
2129 * K22P100M120SF5RM 1024 KB ... duplicate of code 0xd
2130 * K26P169M180SF5RM 2048 KB ... the only unique value
2131 * fcfg2_maxaddr0 seems to be the only clue to pflash_size
2132 * Checking fcfg2_maxaddr0 in bank probe is pointless then
2135 k_chip->pflash_size = k_chip->fcfg2_maxaddr0_shifted * num_blocks;
2137 k_chip->pflash_size = k_chip->fcfg2_maxaddr0_shifted * num_blocks / 2;
2138 if (k_chip->pflash_size != 2048<<10)
2139 LOG_WARNING("SIM_FCFG1 PFSIZE = 0xf: please check if pflash is %u KB", k_chip->pflash_size>>10);
2143 k_chip->pflash_size = 0;
2147 if (k_chip->flash_support & FS_PROGRAM_SECTOR && k_chip->max_flash_prog_size == 0) {
2148 k_chip->max_flash_prog_size = k_chip->pflash_sector_size;
2149 /* Program section size is equal to sector size by default */
2152 k_chip->num_pflash_blocks = num_blocks / (2 - fcfg2_pflsh);
2153 k_chip->num_nvm_blocks = num_blocks - k_chip->num_pflash_blocks;
2155 if (use_nvm_marking) {
2156 nvm_marking[0] = k_chip->num_nvm_blocks ? 'X' : 'N';
2157 nvm_marking[1] = '\0';
2159 nvm_marking[0] = '\0';
2161 pflash_size_k = k_chip->pflash_size / 1024;
2162 pflash_size_m = pflash_size_k / 1024;
2164 snprintf(flash_marking, sizeof(flash_marking), "%s%" PRIu32 "M0xxx", nvm_marking, pflash_size_m);
2166 snprintf(flash_marking, sizeof(flash_marking), "%s%" PRIu32 "xxx", nvm_marking, pflash_size_k);
2168 snprintf(k_chip->name, sizeof(k_chip->name), name, flash_marking);
2169 LOG_INFO("Kinetis %s detected: %u flash blocks", k_chip->name, num_blocks);
2170 LOG_INFO("%u PFlash banks: %" PRIu32 "k total", k_chip->num_pflash_blocks, pflash_size_k);
2171 if (k_chip->num_nvm_blocks) {
2172 nvm_size_k = k_chip->nvm_size / 1024;
2173 dflash_size_k = k_chip->dflash_size / 1024;
2174 LOG_INFO("%u FlexNVM banks: %" PRIu32 "k total, %" PRIu32 "k available as data flash, %" PRIu32 "bytes FlexRAM",
2175 k_chip->num_nvm_blocks, nvm_size_k, dflash_size_k, ee_size);
2178 k_chip->probed = true;
2182 static int kinetis_probe(struct flash_bank *bank)
2185 uint8_t fcfg2_maxaddr0, fcfg2_pflsh, fcfg2_maxaddr1;
2186 unsigned num_blocks, first_nvm_bank;
2188 struct kinetis_flash_bank *k_bank = bank->driver_priv;
2189 struct kinetis_chip *k_chip = k_bank->k_chip;
2191 k_bank->probed = false;
2193 if (!k_chip->probed) {
2194 result = kinetis_probe_chip(k_chip);
2195 if (result != ERROR_OK)
2199 num_blocks = k_chip->num_pflash_blocks + k_chip->num_nvm_blocks;
2200 first_nvm_bank = k_chip->num_pflash_blocks;
2202 if (k_bank->bank_number < k_chip->num_pflash_blocks) {
2203 /* pflash, banks start at address zero */
2204 k_bank->flash_class = FC_PFLASH;
2205 bank->size = (k_chip->pflash_size / k_chip->num_pflash_blocks);
2206 bank->base = k_chip->pflash_base + bank->size * k_bank->bank_number;
2207 k_bank->prog_base = 0x00000000 + bank->size * k_bank->bank_number;
2208 k_bank->sector_size = k_chip->pflash_sector_size;
2209 /* pflash is divided into 32 protection areas for
2210 * parts with more than 32K of PFlash. For parts with
2211 * less the protection unit is set to 1024 bytes */
2212 k_bank->protection_size = MAX(k_chip->pflash_size / 32, 1024);
2213 bank->num_prot_blocks = 32 / k_chip->num_pflash_blocks;
2214 k_bank->protection_block = bank->num_prot_blocks * k_bank->bank_number;
2216 size_k = bank->size / 1024;
2217 LOG_DEBUG("Kinetis bank %u: %" PRIu32 "k PFlash, FTFx base 0x%08" PRIx32 ", sect %u",
2218 k_bank->bank_number, size_k, k_bank->prog_base, k_bank->sector_size);
2220 } else if (k_bank->bank_number < num_blocks) {
2221 /* nvm, banks start at address 0x10000000 */
2222 unsigned nvm_ord = k_bank->bank_number - first_nvm_bank;
2225 k_bank->flash_class = FC_FLEX_NVM;
2226 bank->size = k_chip->nvm_size / k_chip->num_nvm_blocks;
2227 bank->base = k_chip->nvm_base + bank->size * nvm_ord;
2228 k_bank->prog_base = 0x00800000 + bank->size * nvm_ord;
2229 k_bank->sector_size = k_chip->nvm_sector_size;
2230 if (k_chip->dflash_size == 0) {
2231 k_bank->protection_size = 0;
2233 for (i = k_chip->dflash_size; ~i & 1; i >>= 1)
2236 k_bank->protection_size = k_chip->dflash_size / 8; /* data flash size = 2^^n */
2238 k_bank->protection_size = k_chip->nvm_size / 8; /* TODO: verify on SF1, not documented in RM */
2240 bank->num_prot_blocks = 8 / k_chip->num_nvm_blocks;
2241 k_bank->protection_block = bank->num_prot_blocks * nvm_ord;
2243 /* EEPROM backup part of FlexNVM is not accessible, use dflash_size as a limit */
2244 if (k_chip->dflash_size > bank->size * nvm_ord)
2245 limit = k_chip->dflash_size - bank->size * nvm_ord;
2249 if (bank->size > limit) {
2251 LOG_DEBUG("FlexNVM bank %d limited to 0x%08" PRIx32 " due to active EEPROM backup",
2252 k_bank->bank_number, limit);
2255 size_k = bank->size / 1024;
2256 LOG_DEBUG("Kinetis bank %u: %" PRIu32 "k FlexNVM, FTFx base 0x%08" PRIx32 ", sect %u",
2257 k_bank->bank_number, size_k, k_bank->prog_base, k_bank->sector_size);
2260 LOG_ERROR("Cannot determine parameters for bank %d, only %d banks on device",
2261 k_bank->bank_number, num_blocks);
2262 return ERROR_FLASH_BANK_INVALID;
2265 fcfg2_pflsh = (uint8_t)((k_chip->sim_fcfg2 >> 23) & 0x01);
2266 fcfg2_maxaddr0 = (uint8_t)((k_chip->sim_fcfg2 >> 24) & 0x7f);
2267 fcfg2_maxaddr1 = (uint8_t)((k_chip->sim_fcfg2 >> 16) & 0x7f);
2269 if (k_bank->bank_number == 0 && k_chip->fcfg2_maxaddr0_shifted != bank->size)
2270 LOG_WARNING("MAXADDR0 0x%02" PRIx8 " check failed,"
2271 " please report to OpenOCD mailing list", fcfg2_maxaddr0);
2274 if (k_bank->bank_number == 1 && k_chip->fcfg2_maxaddr1_shifted != bank->size)
2275 LOG_WARNING("MAXADDR1 0x%02" PRIx8 " check failed,"
2276 " please report to OpenOCD mailing list", fcfg2_maxaddr1);
2278 if (k_bank->bank_number == first_nvm_bank
2279 && k_chip->fcfg2_maxaddr1_shifted != k_chip->dflash_size)
2280 LOG_WARNING("FlexNVM MAXADDR1 0x%02" PRIx8 " check failed,"
2281 " please report to OpenOCD mailing list", fcfg2_maxaddr1);
2284 if (bank->sectors) {
2285 free(bank->sectors);
2286 bank->sectors = NULL;
2288 if (bank->prot_blocks) {
2289 free(bank->prot_blocks);
2290 bank->prot_blocks = NULL;
2293 if (k_bank->sector_size == 0) {
2294 LOG_ERROR("Unknown sector size for bank %d", bank->bank_number);
2295 return ERROR_FLASH_BANK_INVALID;
2298 bank->num_sectors = bank->size / k_bank->sector_size;
2300 if (bank->num_sectors > 0) {
2301 /* FlexNVM bank can be used for EEPROM backup therefore zero sized */
2302 bank->sectors = alloc_block_array(0, k_bank->sector_size, bank->num_sectors);
2306 bank->prot_blocks = alloc_block_array(0, k_bank->protection_size, bank->num_prot_blocks);
2307 if (!bank->prot_blocks)
2311 bank->num_prot_blocks = 0;
2314 k_bank->probed = true;
2319 static int kinetis_auto_probe(struct flash_bank *bank)
2321 struct kinetis_flash_bank *k_bank = bank->driver_priv;
2323 if (k_bank && k_bank->probed)
2326 return kinetis_probe(bank);
2329 static int kinetis_info(struct flash_bank *bank, char *buf, int buf_size)
2331 const char *bank_class_names[] = {
2332 "(ANY)", "PFlash", "FlexNVM", "FlexRAM"
2335 struct kinetis_flash_bank *k_bank = bank->driver_priv;
2336 struct kinetis_chip *k_chip = k_bank->k_chip;
2337 uint32_t size_k = bank->size / 1024;
2339 snprintf(buf, buf_size,
2340 "%s %s: %" PRIu32 "k %s bank %s at 0x%08" PRIx32,
2341 bank->driver->name, k_chip->name,
2342 size_k, bank_class_names[k_bank->flash_class],
2343 bank->name, bank->base);
2348 static int kinetis_blank_check(struct flash_bank *bank)
2350 struct kinetis_flash_bank *k_bank = bank->driver_priv;
2351 struct kinetis_chip *k_chip = k_bank->k_chip;
2354 /* suprisingly blank check does not work in VLPR and HSRUN modes */
2355 result = kinetis_check_run_mode(bank->target);
2356 if (result != ERROR_OK)
2359 /* reset error flags */
2360 result = kinetis_ftfx_prepare(bank->target);
2361 if (result != ERROR_OK)
2364 if (k_bank->flash_class == FC_PFLASH || k_bank->flash_class == FC_FLEX_NVM) {
2365 bool block_dirty = true;
2366 bool use_block_cmd = !(k_chip->flash_support & FS_NO_CMD_BLOCKSTAT);
2369 if (use_block_cmd && k_bank->flash_class == FC_FLEX_NVM) {
2370 uint8_t fcfg1_depart = (uint8_t)((k_chip->sim_fcfg1 >> 8) & 0x0f);
2371 /* block operation cannot be used on FlexNVM when EEPROM backup partition is set */
2372 if (fcfg1_depart != 0xf && fcfg1_depart != 0)
2373 use_block_cmd = false;
2376 if (use_block_cmd) {
2377 /* check if whole bank is blank */
2378 result = kinetis_ftfx_command(bank->target, FTFx_CMD_BLOCKSTAT, k_bank->prog_base,
2379 0, 0, 0, 0, 0, 0, 0, 0, &ftfx_fstat);
2381 if (result != ERROR_OK)
2382 kinetis_ftfx_clear_error(bank->target);
2383 else if ((ftfx_fstat & 0x01) == 0)
2384 block_dirty = false;
2388 /* the whole bank is not erased, check sector-by-sector */
2390 for (i = 0; i < bank->num_sectors; i++) {
2392 result = kinetis_ftfx_command(bank->target, FTFx_CMD_SECTSTAT,
2393 k_bank->prog_base + bank->sectors[i].offset,
2394 1, 0, 0, 0, 0, 0, 0, 0, &ftfx_fstat);
2396 if (result == ERROR_OK) {
2397 bank->sectors[i].is_erased = !(ftfx_fstat & 0x01);
2399 LOG_DEBUG("Ignoring errored PFlash sector blank-check");
2400 kinetis_ftfx_clear_error(bank->target);
2401 bank->sectors[i].is_erased = -1;
2405 /* the whole bank is erased, update all sectors */
2407 for (i = 0; i < bank->num_sectors; i++)
2408 bank->sectors[i].is_erased = 1;
2411 LOG_WARNING("kinetis_blank_check not supported yet for FlexRAM");
2412 return ERROR_FLASH_OPERATION_FAILED;
2419 COMMAND_HANDLER(kinetis_nvm_partition)
2423 unsigned num_blocks, first_nvm_bank;
2424 unsigned long par, log2 = 0, ee1 = 0, ee2 = 0;
2425 enum { SHOW_INFO, DF_SIZE, EEBKP_SIZE } sz_type = SHOW_INFO;
2427 uint8_t load_flex_ram = 1;
2428 uint8_t ee_size_code = 0x3f;
2429 uint8_t flex_nvm_partition_code = 0;
2430 uint8_t ee_split = 3;
2431 struct target *target = get_current_target(CMD_CTX);
2432 struct kinetis_chip *k_chip;
2435 if (CMD_ARGC >= 2) {
2436 if (strcmp(CMD_ARGV[0], "dataflash") == 0)
2438 else if (strcmp(CMD_ARGV[0], "eebkp") == 0)
2439 sz_type = EEBKP_SIZE;
2441 par = strtoul(CMD_ARGV[1], NULL, 10);
2442 while (par >> (log2 + 3))
2447 result = target_read_u32(target, SIM_FCFG1, &sim_fcfg1);
2448 if (result != ERROR_OK)
2451 flex_nvm_partition_code = (uint8_t)((sim_fcfg1 >> 8) & 0x0f);
2452 switch (flex_nvm_partition_code) {
2454 command_print(CMD_CTX, "No EEPROM backup, data flash only");
2462 command_print(CMD_CTX, "EEPROM backup %d KB", 4 << flex_nvm_partition_code);
2465 command_print(CMD_CTX, "No data flash, EEPROM backup only");
2473 command_print(CMD_CTX, "data flash %d KB", 4 << (flex_nvm_partition_code & 7));
2476 command_print(CMD_CTX, "No EEPROM backup, data flash only (DEPART not set)");
2479 command_print(CMD_CTX, "Unsupported EEPROM backup size code 0x%02" PRIx8, flex_nvm_partition_code);
2484 flex_nvm_partition_code = 0x8 | log2;
2488 flex_nvm_partition_code = log2;
2493 ee1 = ee2 = strtoul(CMD_ARGV[2], NULL, 10) / 2;
2494 else if (CMD_ARGC >= 4) {
2495 ee1 = strtoul(CMD_ARGV[2], NULL, 10);
2496 ee2 = strtoul(CMD_ARGV[3], NULL, 10);
2499 enable = ee1 + ee2 > 0;
2501 for (log2 = 2; ; log2++) {
2502 if (ee1 + ee2 == (16u << 10) >> log2)
2504 if (ee1 + ee2 > (16u << 10) >> log2 || log2 >= 9) {
2505 LOG_ERROR("Unsupported EEPROM size");
2506 return ERROR_FLASH_OPERATION_FAILED;
2512 else if (ee1 * 7 == ee2)
2514 else if (ee1 != ee2) {
2515 LOG_ERROR("Unsupported EEPROM sizes ratio");
2516 return ERROR_FLASH_OPERATION_FAILED;
2519 ee_size_code = log2 | ee_split << 4;
2523 COMMAND_PARSE_ON_OFF(CMD_ARGV[4], enable);
2527 LOG_INFO("DEPART 0x%" PRIx8 ", EEPROM size code 0x%" PRIx8,
2528 flex_nvm_partition_code, ee_size_code);
2530 result = kinetis_check_run_mode(target);
2531 if (result != ERROR_OK)
2534 /* reset error flags */
2535 result = kinetis_ftfx_prepare(target);
2536 if (result != ERROR_OK)
2539 result = kinetis_ftfx_command(target, FTFx_CMD_PGMPART, load_flex_ram,
2540 ee_size_code, flex_nvm_partition_code, 0, 0,
2542 if (result != ERROR_OK)
2545 command_print(CMD_CTX, "FlexNVM partition set. Please reset MCU.");
2547 k_chip = kinetis_get_chip(target);
2549 first_nvm_bank = k_chip->num_pflash_blocks;
2550 num_blocks = k_chip->num_pflash_blocks + k_chip->num_nvm_blocks;
2551 for (bank_idx = first_nvm_bank; bank_idx < num_blocks; bank_idx++)
2552 k_chip->banks[bank_idx].probed = false; /* re-probe before next use */
2553 k_chip->probed = false;
2556 command_print(CMD_CTX, "FlexNVM banks will be re-probed to set new data flash size.");
2560 COMMAND_HANDLER(kinetis_fcf_source_handler)
2563 return ERROR_COMMAND_SYNTAX_ERROR;
2565 if (CMD_ARGC == 1) {
2566 if (strcmp(CMD_ARGV[0], "write") == 0)
2567 allow_fcf_writes = true;
2568 else if (strcmp(CMD_ARGV[0], "protection") == 0)
2569 allow_fcf_writes = false;
2571 return ERROR_COMMAND_SYNTAX_ERROR;
2574 if (allow_fcf_writes) {
2575 command_print(CMD_CTX, "Arbitrary Flash Configuration Field writes enabled.");
2576 command_print(CMD_CTX, "Protection info writes to FCF disabled.");
2577 LOG_WARNING("BEWARE: incorrect flash configuration may permanently lock the device.");
2579 command_print(CMD_CTX, "Protection info writes to Flash Configuration Field enabled.");
2580 command_print(CMD_CTX, "Arbitrary FCF writes disabled. Mode safe from unwanted locking of the device.");
2586 COMMAND_HANDLER(kinetis_fopt_handler)
2589 return ERROR_COMMAND_SYNTAX_ERROR;
2592 fcf_fopt = (uint8_t)strtoul(CMD_ARGV[0], NULL, 0);
2594 command_print(CMD_CTX, "FCF_FOPT 0x%02" PRIx8, fcf_fopt);
2600 static const struct command_registration kinetis_security_command_handlers[] = {
2602 .name = "check_security",
2603 .mode = COMMAND_EXEC,
2604 .help = "Check status of device security lock",
2606 .handler = kinetis_check_flash_security_status,
2610 .mode = COMMAND_EXEC,
2611 .help = "Issue a halt via the MDM-AP",
2613 .handler = kinetis_mdm_halt,
2616 .name = "mass_erase",
2617 .mode = COMMAND_EXEC,
2618 .help = "Issue a complete flash erase via the MDM-AP",
2620 .handler = kinetis_mdm_mass_erase,
2623 .mode = COMMAND_EXEC,
2624 .help = "Issue a reset via the MDM-AP",
2626 .handler = kinetis_mdm_reset,
2628 COMMAND_REGISTRATION_DONE
2631 static const struct command_registration kinetis_exec_command_handlers[] = {
2634 .mode = COMMAND_ANY,
2635 .help = "MDM-AP command group",
2637 .chain = kinetis_security_command_handlers,
2640 .name = "disable_wdog",
2641 .mode = COMMAND_EXEC,
2642 .help = "Disable the watchdog timer",
2644 .handler = kinetis_disable_wdog_handler,
2647 .name = "nvm_partition",
2648 .mode = COMMAND_EXEC,
2649 .help = "Show/set data flash or EEPROM backup size in kilobytes,"
2650 " set two EEPROM sizes in bytes and FlexRAM loading during reset",
2651 .usage = "('info'|'dataflash' size|'eebkp' size) [eesize1 eesize2] ['on'|'off']",
2652 .handler = kinetis_nvm_partition,
2655 .name = "fcf_source",
2656 .mode = COMMAND_EXEC,
2657 .help = "Use protection as a source for Flash Configuration Field or allow writing arbitrary values to the FCF"
2658 " Mode 'protection' is safe from unwanted locking of the device.",
2659 .usage = "['protection'|'write']",
2660 .handler = kinetis_fcf_source_handler,
2664 .mode = COMMAND_EXEC,
2665 .help = "FCF_FOPT value source in 'kinetis fcf_source protection' mode",
2667 .handler = kinetis_fopt_handler,
2669 COMMAND_REGISTRATION_DONE
2672 static const struct command_registration kinetis_command_handler[] = {
2675 .mode = COMMAND_ANY,
2676 .help = "Kinetis flash controller commands",
2678 .chain = kinetis_exec_command_handlers,
2680 COMMAND_REGISTRATION_DONE
2685 struct flash_driver kinetis_flash = {
2687 .commands = kinetis_command_handler,
2688 .flash_bank_command = kinetis_flash_bank_command,
2689 .erase = kinetis_erase,
2690 .protect = kinetis_protect,
2691 .write = kinetis_write,
2692 .read = default_flash_read,
2693 .probe = kinetis_probe,
2694 .auto_probe = kinetis_auto_probe,
2695 .erase_check = kinetis_blank_check,
2696 .protect_check = kinetis_protect_check,
2697 .info = kinetis_info,