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_MASK 0x0F000000
196 #define KINETIS_SDID_SUBFAMID_KX0 0x00000000
197 #define KINETIS_SDID_SUBFAMID_KX1 0x01000000
198 #define KINETIS_SDID_SUBFAMID_KX2 0x02000000
199 #define KINETIS_SDID_SUBFAMID_KX3 0x03000000
200 #define KINETIS_SDID_SUBFAMID_KX4 0x04000000
201 #define KINETIS_SDID_SUBFAMID_KX5 0x05000000
202 #define KINETIS_SDID_SUBFAMID_KX6 0x06000000
203 #define KINETIS_SDID_SUBFAMID_KX7 0x07000000
204 #define KINETIS_SDID_SUBFAMID_KX8 0x08000000
206 #define KINETIS_SDID_FAMILYID_MASK 0xF0000000
207 #define KINETIS_SDID_FAMILYID_K0X 0x00000000
208 #define KINETIS_SDID_FAMILYID_K1X 0x10000000
209 #define KINETIS_SDID_FAMILYID_K2X 0x20000000
210 #define KINETIS_SDID_FAMILYID_K3X 0x30000000
211 #define KINETIS_SDID_FAMILYID_K4X 0x40000000
212 #define KINETIS_SDID_FAMILYID_K5X 0x50000000
213 #define KINETIS_SDID_FAMILYID_K6X 0x60000000
214 #define KINETIS_SDID_FAMILYID_K7X 0x70000000
215 #define KINETIS_SDID_FAMILYID_K8X 0x80000000
216 #define KINETIS_SDID_FAMILYID_KL8X 0x90000000
218 /* The field originally named DIEID has new name/meaning on KE1x */
219 #define KINETIS_SDID_PROJECTID_MASK KINETIS_SDID_DIEID_MASK
220 #define KINETIS_SDID_PROJECTID_KE1xF 0x00000080
221 #define KINETIS_SDID_PROJECTID_KE1xZ 0x00000100
223 struct kinetis_flash_bank
{
225 uint32_t sector_size
;
226 uint32_t max_flash_prog_size
;
227 uint32_t protection_size
;
228 uint32_t prog_base
; /* base address for FTFx operations */
229 /* same as bank->base for pflash, differs for FlexNVM */
230 uint32_t protection_block
; /* number of first protection block in this bank */
240 FS_PROGRAM_SECTOR
= 1,
241 FS_PROGRAM_LONGWORD
= 2,
242 FS_PROGRAM_PHRASE
= 4, /* Unsupported */
243 FS_INVALIDATE_CACHE_K
= 8, /* using FMC->PFB0CR/PFB01CR */
244 FS_INVALIDATE_CACHE_L
= 0x10, /* using MCM->PLACR */
245 FS_INVALIDATE_CACHE_MSCM
= 0x20,
246 FS_NO_CMD_BLOCKSTAT
= 0x40,
247 FS_WIDTH_256BIT
= 0x80,
250 /* device parameters - should be same for all probed banks of one device */
255 uint32_t progr_accel_ram
;
260 #define MDM_REG_STAT 0x00
261 #define MDM_REG_CTRL 0x04
262 #define MDM_REG_ID 0xfc
264 #define MDM_STAT_FMEACK (1<<0)
265 #define MDM_STAT_FREADY (1<<1)
266 #define MDM_STAT_SYSSEC (1<<2)
267 #define MDM_STAT_SYSRES (1<<3)
268 #define MDM_STAT_FMEEN (1<<5)
269 #define MDM_STAT_BACKDOOREN (1<<6)
270 #define MDM_STAT_LPEN (1<<7)
271 #define MDM_STAT_VLPEN (1<<8)
272 #define MDM_STAT_LLSMODEXIT (1<<9)
273 #define MDM_STAT_VLLSXMODEXIT (1<<10)
274 #define MDM_STAT_CORE_HALTED (1<<16)
275 #define MDM_STAT_CORE_SLEEPDEEP (1<<17)
276 #define MDM_STAT_CORESLEEPING (1<<18)
278 #define MDM_CTRL_FMEIP (1<<0)
279 #define MDM_CTRL_DBG_DIS (1<<1)
280 #define MDM_CTRL_DBG_REQ (1<<2)
281 #define MDM_CTRL_SYS_RES_REQ (1<<3)
282 #define MDM_CTRL_CORE_HOLD_RES (1<<4)
283 #define MDM_CTRL_VLLSX_DBG_REQ (1<<5)
284 #define MDM_CTRL_VLLSX_DBG_ACK (1<<6)
285 #define MDM_CTRL_VLLSX_STAT_ACK (1<<7)
287 #define MDM_ACCESS_TIMEOUT 500 /* msec */
290 static bool allow_fcf_writes
;
291 static uint8_t fcf_fopt
= 0xff;
294 struct flash_driver kinetis_flash
;
295 static int kinetis_write_inner(struct flash_bank
*bank
, const uint8_t *buffer
,
296 uint32_t offset
, uint32_t count
);
297 static int kinetis_auto_probe(struct flash_bank
*bank
);
300 static int kinetis_mdm_write_register(struct adiv5_dap
*dap
, unsigned reg
, uint32_t value
)
303 LOG_DEBUG("MDM_REG[0x%02x] <- %08" PRIX32
, reg
, value
);
305 retval
= dap_queue_ap_write(dap_ap(dap
, MDM_AP
), reg
, value
);
306 if (retval
!= ERROR_OK
) {
307 LOG_DEBUG("MDM: failed to queue a write request");
311 retval
= dap_run(dap
);
312 if (retval
!= ERROR_OK
) {
313 LOG_DEBUG("MDM: dap_run failed");
321 static int kinetis_mdm_read_register(struct adiv5_dap
*dap
, unsigned reg
, uint32_t *result
)
325 retval
= dap_queue_ap_read(dap_ap(dap
, MDM_AP
), reg
, result
);
326 if (retval
!= ERROR_OK
) {
327 LOG_DEBUG("MDM: failed to queue a read request");
331 retval
= dap_run(dap
);
332 if (retval
!= ERROR_OK
) {
333 LOG_DEBUG("MDM: dap_run failed");
337 LOG_DEBUG("MDM_REG[0x%02x]: %08" PRIX32
, reg
, *result
);
341 static int kinetis_mdm_poll_register(struct adiv5_dap
*dap
, unsigned reg
,
342 uint32_t mask
, uint32_t value
, uint32_t timeout_ms
)
346 int64_t ms_timeout
= timeval_ms() + timeout_ms
;
349 retval
= kinetis_mdm_read_register(dap
, reg
, &val
);
350 if (retval
!= ERROR_OK
|| (val
& mask
) == value
)
354 } while (timeval_ms() < ms_timeout
);
356 LOG_DEBUG("MDM: polling timed out");
361 * This command can be used to break a watchdog reset loop when
362 * connecting to an unsecured target. Unlike other commands, halt will
363 * automatically retry as it does not know how far into the boot process
364 * it is when the command is called.
366 COMMAND_HANDLER(kinetis_mdm_halt
)
368 struct target
*target
= get_current_target(CMD_CTX
);
369 struct cortex_m_common
*cortex_m
= target_to_cm(target
);
370 struct adiv5_dap
*dap
= cortex_m
->armv7m
.arm
.dap
;
374 int64_t ms_timeout
= timeval_ms() + MDM_ACCESS_TIMEOUT
;
377 LOG_ERROR("Cannot perform halt with a high-level adapter");
384 kinetis_mdm_write_register(dap
, MDM_REG_CTRL
, MDM_CTRL_CORE_HOLD_RES
);
388 retval
= kinetis_mdm_read_register(dap
, MDM_REG_STAT
, &stat
);
389 if (retval
!= ERROR_OK
) {
390 LOG_DEBUG("MDM: failed to read MDM_REG_STAT");
394 /* Repeat setting MDM_CTRL_CORE_HOLD_RES until system is out of
395 * reset with flash ready and without security
397 if ((stat
& (MDM_STAT_FREADY
| MDM_STAT_SYSSEC
| MDM_STAT_SYSRES
))
398 == (MDM_STAT_FREADY
| MDM_STAT_SYSRES
))
401 if (timeval_ms() >= ms_timeout
) {
402 LOG_ERROR("MDM: halt timed out");
407 LOG_DEBUG("MDM: halt succeded after %d attempts.", tries
);
410 /* enable polling in case kinetis_check_flash_security_status disabled it */
411 jtag_poll_set_enabled(true);
415 target
->reset_halt
= true;
416 target
->type
->assert_reset(target
);
418 retval
= kinetis_mdm_write_register(dap
, MDM_REG_CTRL
, 0);
419 if (retval
!= ERROR_OK
) {
420 LOG_ERROR("MDM: failed to clear MDM_REG_CTRL");
424 target
->type
->deassert_reset(target
);
429 COMMAND_HANDLER(kinetis_mdm_reset
)
431 struct target
*target
= get_current_target(CMD_CTX
);
432 struct cortex_m_common
*cortex_m
= target_to_cm(target
);
433 struct adiv5_dap
*dap
= cortex_m
->armv7m
.arm
.dap
;
437 LOG_ERROR("Cannot perform reset with a high-level adapter");
441 retval
= kinetis_mdm_write_register(dap
, MDM_REG_CTRL
, MDM_CTRL_SYS_RES_REQ
);
442 if (retval
!= ERROR_OK
) {
443 LOG_ERROR("MDM: failed to write MDM_REG_CTRL");
447 retval
= kinetis_mdm_poll_register(dap
, MDM_REG_STAT
, MDM_STAT_SYSRES
, 0, 500);
448 if (retval
!= ERROR_OK
) {
449 LOG_ERROR("MDM: failed to assert reset");
453 retval
= kinetis_mdm_write_register(dap
, MDM_REG_CTRL
, 0);
454 if (retval
!= ERROR_OK
) {
455 LOG_ERROR("MDM: failed to clear MDM_REG_CTRL");
463 * This function implements the procedure to mass erase the flash via
464 * SWD/JTAG on Kinetis K and L series of devices as it is described in
465 * AN4835 "Production Flash Programming Best Practices for Kinetis K-
466 * and L-series MCUs" Section 4.2.1. To prevent a watchdog reset loop,
467 * the core remains halted after this function completes as suggested
468 * by the application note.
470 COMMAND_HANDLER(kinetis_mdm_mass_erase
)
472 struct target
*target
= get_current_target(CMD_CTX
);
473 struct cortex_m_common
*cortex_m
= target_to_cm(target
);
474 struct adiv5_dap
*dap
= cortex_m
->armv7m
.arm
.dap
;
477 LOG_ERROR("Cannot perform mass erase with a high-level adapter");
484 * ... Power on the processor, or if power has already been
485 * applied, assert the RESET pin to reset the processor. For
486 * devices that do not have a RESET pin, write the System
487 * Reset Request bit in the MDM-AP control register after
488 * establishing communication...
491 /* assert SRST if configured */
492 bool has_srst
= jtag_get_reset_config() & RESET_HAS_SRST
;
494 adapter_assert_reset();
496 retval
= kinetis_mdm_write_register(dap
, MDM_REG_CTRL
, MDM_CTRL_SYS_RES_REQ
);
497 if (retval
!= ERROR_OK
&& !has_srst
) {
498 LOG_ERROR("MDM: failed to assert reset");
499 goto deassert_reset_and_exit
;
503 * ... Read the MDM-AP status register repeatedly and wait for
504 * stable conditions suitable for mass erase:
505 * - mass erase is enabled
507 * - reset is finished
509 * Mass erase is started as soon as all conditions are met in 32
510 * subsequent status reads.
512 * In case of not stable conditions (RESET/WDOG loop in secured device)
513 * the user is asked for manual pressing of RESET button
516 int cnt_mass_erase_disabled
= 0;
518 int64_t ms_start
= timeval_ms();
519 bool man_reset_requested
= false;
523 int64_t ms_elapsed
= timeval_ms() - ms_start
;
525 if (!man_reset_requested
&& ms_elapsed
> 100) {
526 LOG_INFO("MDM: Press RESET button now if possible.");
527 man_reset_requested
= true;
530 if (ms_elapsed
> 3000) {
531 LOG_ERROR("MDM: waiting for mass erase conditions timed out.");
532 LOG_INFO("Mass erase of a secured MCU is not possible without hardware reset.");
533 LOG_INFO("Connect SRST, use 'reset_config srst_only' and retry.");
534 goto deassert_reset_and_exit
;
536 retval
= kinetis_mdm_read_register(dap
, MDM_REG_STAT
, &stat
);
537 if (retval
!= ERROR_OK
) {
542 if (!(stat
& MDM_STAT_FMEEN
)) {
544 cnt_mass_erase_disabled
++;
545 if (cnt_mass_erase_disabled
> 10) {
546 LOG_ERROR("MDM: mass erase is disabled");
547 goto deassert_reset_and_exit
;
552 if ((stat
& (MDM_STAT_FREADY
| MDM_STAT_SYSRES
)) == MDM_STAT_FREADY
)
557 } while (cnt_ready
< 32);
560 * ... Write the MDM-AP control register to set the Flash Mass
561 * Erase in Progress bit. This will start the mass erase
564 retval
= kinetis_mdm_write_register(dap
, MDM_REG_CTRL
, MDM_CTRL_SYS_RES_REQ
| MDM_CTRL_FMEIP
);
565 if (retval
!= ERROR_OK
) {
566 LOG_ERROR("MDM: failed to start mass erase");
567 goto deassert_reset_and_exit
;
571 * ... Read the MDM-AP control register until the Flash Mass
572 * Erase in Progress bit clears...
573 * Data sheed defines erase time <3.6 sec/512kB flash block.
574 * The biggest device has 4 pflash blocks => timeout 16 sec.
576 retval
= kinetis_mdm_poll_register(dap
, MDM_REG_CTRL
, MDM_CTRL_FMEIP
, 0, 16000);
577 if (retval
!= ERROR_OK
) {
578 LOG_ERROR("MDM: mass erase timeout");
579 goto deassert_reset_and_exit
;
583 /* enable polling in case kinetis_check_flash_security_status disabled it */
584 jtag_poll_set_enabled(true);
588 target
->reset_halt
= true;
589 target
->type
->assert_reset(target
);
592 * ... Negate the RESET signal or clear the System Reset Request
593 * bit in the MDM-AP control register.
595 retval
= kinetis_mdm_write_register(dap
, MDM_REG_CTRL
, 0);
596 if (retval
!= ERROR_OK
)
597 LOG_ERROR("MDM: failed to clear MDM_REG_CTRL");
599 target
->type
->deassert_reset(target
);
603 deassert_reset_and_exit
:
604 kinetis_mdm_write_register(dap
, MDM_REG_CTRL
, 0);
606 adapter_deassert_reset();
610 static const uint32_t kinetis_known_mdm_ids
[] = {
611 0x001C0000, /* Kinetis-K Series */
612 0x001C0020, /* Kinetis-L/M/V/E Series */
613 0x001C0030, /* Kinetis with a Cortex-M7, in time of writing KV58 */
617 * This function implements the procedure to connect to
618 * SWD/JTAG on Kinetis K and L series of devices as it is described in
619 * AN4835 "Production Flash Programming Best Practices for Kinetis K-
620 * and L-series MCUs" Section 4.1.1
622 COMMAND_HANDLER(kinetis_check_flash_security_status
)
624 struct target
*target
= get_current_target(CMD_CTX
);
625 struct cortex_m_common
*cortex_m
= target_to_cm(target
);
626 struct adiv5_dap
*dap
= cortex_m
->armv7m
.arm
.dap
;
629 LOG_WARNING("Cannot check flash security status with a high-level adapter");
634 return ERROR_OK
; /* too early to check, in JTAG mode ops may not be initialised */
640 * ... The MDM-AP ID register can be read to verify that the
641 * connection is working correctly...
643 retval
= kinetis_mdm_read_register(dap
, MDM_REG_ID
, &val
);
644 if (retval
!= ERROR_OK
) {
645 LOG_ERROR("MDM: failed to read ID register");
650 return ERROR_OK
; /* dap not yet initialised */
653 for (size_t i
= 0; i
< ARRAY_SIZE(kinetis_known_mdm_ids
); i
++) {
654 if (val
== kinetis_known_mdm_ids
[i
]) {
661 LOG_WARNING("MDM: unknown ID %08" PRIX32
, val
);
664 * ... Read the System Security bit to determine if security is enabled.
665 * If System Security = 0, then proceed. If System Security = 1, then
666 * communication with the internals of the processor, including the
667 * flash, will not be possible without issuing a mass erase command or
668 * unsecuring the part through other means (backdoor key unlock)...
670 retval
= kinetis_mdm_read_register(dap
, MDM_REG_STAT
, &val
);
671 if (retval
!= ERROR_OK
) {
672 LOG_ERROR("MDM: failed to read MDM_REG_STAT");
677 * System Security bit is also active for short time during reset.
678 * If a MCU has blank flash and runs in RESET/WDOG loop,
679 * System Security bit is active most of time!
680 * We should observe Flash Ready bit and read status several times
681 * to avoid false detection of secured MCU
683 int secured_score
= 0, flash_not_ready_score
= 0;
685 if ((val
& (MDM_STAT_SYSSEC
| MDM_STAT_FREADY
)) != MDM_STAT_FREADY
) {
689 for (i
= 0; i
< 32; i
++) {
690 stats
[i
] = MDM_STAT_FREADY
;
691 dap_queue_ap_read(dap_ap(dap
, MDM_AP
), MDM_REG_STAT
, &stats
[i
]);
693 retval
= dap_run(dap
);
694 if (retval
!= ERROR_OK
) {
695 LOG_DEBUG("MDM: dap_run failed when validating secured state");
698 for (i
= 0; i
< 32; i
++) {
699 if (stats
[i
] & MDM_STAT_SYSSEC
)
701 if (!(stats
[i
] & MDM_STAT_FREADY
))
702 flash_not_ready_score
++;
706 if (flash_not_ready_score
<= 8 && secured_score
> 24) {
707 jtag_poll_set_enabled(false);
709 LOG_WARNING("*********** ATTENTION! ATTENTION! ATTENTION! ATTENTION! **********");
710 LOG_WARNING("**** ****");
711 LOG_WARNING("**** Your Kinetis MCU is in secured state, which means that, ****");
712 LOG_WARNING("**** with exception for very basic communication, JTAG/SWD ****");
713 LOG_WARNING("**** interface will NOT work. In order to restore its ****");
714 LOG_WARNING("**** functionality please issue 'kinetis mdm mass_erase' ****");
715 LOG_WARNING("**** command, power cycle the MCU and restart OpenOCD. ****");
716 LOG_WARNING("**** ****");
717 LOG_WARNING("*********** ATTENTION! ATTENTION! ATTENTION! ATTENTION! **********");
719 } else if (flash_not_ready_score
> 24) {
720 jtag_poll_set_enabled(false);
721 LOG_WARNING("**** Your Kinetis MCU is probably locked-up in RESET/WDOG loop. ****");
722 LOG_WARNING("**** Common reason is a blank flash (at least a reset vector). ****");
723 LOG_WARNING("**** Issue 'kinetis mdm halt' command or if SRST is connected ****");
724 LOG_WARNING("**** and configured, use 'reset halt' ****");
725 LOG_WARNING("**** If MCU cannot be halted, it is likely secured and running ****");
726 LOG_WARNING("**** in RESET/WDOG loop. Issue 'kinetis mdm mass_erase' ****");
729 LOG_INFO("MDM: Chip is unsecured. Continuing.");
730 jtag_poll_set_enabled(true);
736 FLASH_BANK_COMMAND_HANDLER(kinetis_flash_bank_command
)
738 struct kinetis_flash_bank
*bank_info
;
741 return ERROR_COMMAND_SYNTAX_ERROR
;
743 LOG_INFO("add flash_bank kinetis %s", bank
->name
);
745 bank_info
= malloc(sizeof(struct kinetis_flash_bank
));
747 memset(bank_info
, 0, sizeof(struct kinetis_flash_bank
));
749 bank
->driver_priv
= bank_info
;
754 /* Disable the watchdog on Kinetis devices */
755 int kinetis_disable_wdog(struct target
*target
, uint32_t sim_sdid
)
757 struct working_area
*wdog_algorithm
;
758 struct armv7m_algorithm armv7m_info
;
762 static const uint8_t kinetis_unlock_wdog_code
[] = {
763 #include "../../../contrib/loaders/watchdog/armv7m_kinetis_wdog.inc"
766 /* Decide whether the connected device needs watchdog disabling.
767 * Disable for all Kx and KVx devices, return if it is a KLx */
769 if ((sim_sdid
& KINETIS_SDID_SERIESID_MASK
) == KINETIS_SDID_SERIESID_KL
)
772 /* The connected device requires watchdog disabling. */
773 retval
= target_read_u16(target
, WDOG_STCTRH
, &wdog
);
774 if (retval
!= ERROR_OK
)
777 if ((wdog
& 0x1) == 0) {
778 /* watchdog already disabled */
781 LOG_INFO("Disabling Kinetis watchdog (initial WDOG_STCTRLH = 0x%x)", wdog
);
783 if (target
->state
!= TARGET_HALTED
) {
784 LOG_ERROR("Target not halted");
785 return ERROR_TARGET_NOT_HALTED
;
788 retval
= target_alloc_working_area(target
, sizeof(kinetis_unlock_wdog_code
), &wdog_algorithm
);
789 if (retval
!= ERROR_OK
)
792 retval
= target_write_buffer(target
, wdog_algorithm
->address
,
793 sizeof(kinetis_unlock_wdog_code
), (uint8_t *)kinetis_unlock_wdog_code
);
794 if (retval
!= ERROR_OK
) {
795 target_free_working_area(target
, wdog_algorithm
);
799 armv7m_info
.common_magic
= ARMV7M_COMMON_MAGIC
;
800 armv7m_info
.core_mode
= ARM_MODE_THREAD
;
802 retval
= target_run_algorithm(target
, 0, NULL
, 0, NULL
, wdog_algorithm
->address
,
803 wdog_algorithm
->address
+ (sizeof(kinetis_unlock_wdog_code
) - 2),
804 10000, &armv7m_info
);
806 if (retval
!= ERROR_OK
)
807 LOG_ERROR("error executing kinetis wdog unlock algorithm");
809 retval
= target_read_u16(target
, WDOG_STCTRH
, &wdog
);
810 if (retval
!= ERROR_OK
)
812 LOG_INFO("WDOG_STCTRLH = 0x%x", wdog
);
814 target_free_working_area(target
, wdog_algorithm
);
819 COMMAND_HANDLER(kinetis_disable_wdog_handler
)
823 struct target
*target
= get_current_target(CMD_CTX
);
826 return ERROR_COMMAND_SYNTAX_ERROR
;
828 result
= target_read_u32(target
, SIM_SDID
, &sim_sdid
);
829 if (result
!= ERROR_OK
) {
830 LOG_ERROR("Failed to read SIMSDID");
834 result
= kinetis_disable_wdog(target
, sim_sdid
);
839 static int kinetis_ftfx_decode_error(uint8_t fstat
)
842 LOG_ERROR("Flash operation failed, illegal command");
843 return ERROR_FLASH_OPER_UNSUPPORTED
;
845 } else if (fstat
& 0x10)
846 LOG_ERROR("Flash operation failed, protection violated");
848 else if (fstat
& 0x40)
849 LOG_ERROR("Flash operation failed, read collision");
851 else if (fstat
& 0x80)
855 LOG_ERROR("Flash operation timed out");
857 return ERROR_FLASH_OPERATION_FAILED
;
860 static int kinetis_ftfx_clear_error(struct target
*target
)
862 /* reset error flags */
863 return target_write_u8(target
, FTFx_FSTAT
, 0x70);
867 static int kinetis_ftfx_prepare(struct target
*target
)
872 /* wait until busy */
873 for (i
= 0; i
< 50; i
++) {
874 result
= target_read_u8(target
, FTFx_FSTAT
, &fstat
);
875 if (result
!= ERROR_OK
)
882 if ((fstat
& 0x80) == 0) {
883 LOG_ERROR("Flash controller is busy");
884 return ERROR_FLASH_OPERATION_FAILED
;
887 /* reset error flags */
888 result
= kinetis_ftfx_clear_error(target
);
893 /* Kinetis Program-LongWord Microcodes */
894 static const uint8_t kinetis_flash_write_code
[] = {
895 #include "../../../contrib/loaders/flash/kinetis/kinetis_flash.inc"
898 /* Program LongWord Block Write */
899 static int kinetis_write_block(struct flash_bank
*bank
, const uint8_t *buffer
,
900 uint32_t offset
, uint32_t wcount
)
902 struct target
*target
= bank
->target
;
903 uint32_t buffer_size
= 2048; /* Default minimum value */
904 struct working_area
*write_algorithm
;
905 struct working_area
*source
;
906 struct kinetis_flash_bank
*kinfo
= bank
->driver_priv
;
907 uint32_t address
= kinfo
->prog_base
+ offset
;
908 uint32_t end_address
;
909 struct reg_param reg_params
[5];
910 struct armv7m_algorithm armv7m_info
;
914 /* Increase buffer_size if needed */
915 if (buffer_size
< (target
->working_area_size
/2))
916 buffer_size
= (target
->working_area_size
/2);
918 /* allocate working area with flash programming code */
919 if (target_alloc_working_area(target
, sizeof(kinetis_flash_write_code
),
920 &write_algorithm
) != ERROR_OK
) {
921 LOG_WARNING("no working area available, can't do block memory writes");
922 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
925 retval
= target_write_buffer(target
, write_algorithm
->address
,
926 sizeof(kinetis_flash_write_code
), kinetis_flash_write_code
);
927 if (retval
!= ERROR_OK
)
931 while (target_alloc_working_area(target
, buffer_size
, &source
) != ERROR_OK
) {
933 if (buffer_size
<= 256) {
934 /* free working area, write algorithm already allocated */
935 target_free_working_area(target
, write_algorithm
);
937 LOG_WARNING("No large enough working area available, can't do block memory writes");
938 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
942 armv7m_info
.common_magic
= ARMV7M_COMMON_MAGIC
;
943 armv7m_info
.core_mode
= ARM_MODE_THREAD
;
945 init_reg_param(®_params
[0], "r0", 32, PARAM_IN_OUT
); /* address */
946 init_reg_param(®_params
[1], "r1", 32, PARAM_OUT
); /* word count */
947 init_reg_param(®_params
[2], "r2", 32, PARAM_OUT
);
948 init_reg_param(®_params
[3], "r3", 32, PARAM_OUT
);
949 init_reg_param(®_params
[4], "r4", 32, PARAM_OUT
);
951 buf_set_u32(reg_params
[0].value
, 0, 32, address
);
952 buf_set_u32(reg_params
[1].value
, 0, 32, wcount
);
953 buf_set_u32(reg_params
[2].value
, 0, 32, source
->address
);
954 buf_set_u32(reg_params
[3].value
, 0, 32, source
->address
+ source
->size
);
955 buf_set_u32(reg_params
[4].value
, 0, 32, FTFx_FSTAT
);
957 retval
= target_run_flash_async_algorithm(target
, buffer
, wcount
, 4,
960 source
->address
, source
->size
,
961 write_algorithm
->address
, 0,
964 if (retval
== ERROR_FLASH_OPERATION_FAILED
) {
965 end_address
= buf_get_u32(reg_params
[0].value
, 0, 32);
967 LOG_ERROR("Error writing flash at %08" PRIx32
, end_address
);
969 retval
= target_read_u8(target
, FTFx_FSTAT
, &fstat
);
970 if (retval
== ERROR_OK
) {
971 retval
= kinetis_ftfx_decode_error(fstat
);
973 /* reset error flags */
974 target_write_u8(target
, FTFx_FSTAT
, 0x70);
976 } else if (retval
!= ERROR_OK
)
977 LOG_ERROR("Error executing kinetis Flash programming algorithm");
979 target_free_working_area(target
, source
);
980 target_free_working_area(target
, write_algorithm
);
982 destroy_reg_param(®_params
[0]);
983 destroy_reg_param(®_params
[1]);
984 destroy_reg_param(®_params
[2]);
985 destroy_reg_param(®_params
[3]);
986 destroy_reg_param(®_params
[4]);
991 static int kinetis_protect(struct flash_bank
*bank
, int set
, int first
, int last
)
995 if (allow_fcf_writes
) {
996 LOG_ERROR("Protection setting is possible with 'kinetis fcf_source protection' only!");
1000 if (!bank
->prot_blocks
|| bank
->num_prot_blocks
== 0) {
1001 LOG_ERROR("No protection possible for current bank!");
1002 return ERROR_FLASH_BANK_INVALID
;
1005 for (i
= first
; i
< bank
->num_prot_blocks
&& i
<= last
; i
++)
1006 bank
->prot_blocks
[i
].is_protected
= set
;
1008 LOG_INFO("Protection bits will be written at the next FCF sector erase or write.");
1009 LOG_INFO("Do not issue 'flash info' command until protection is written,");
1010 LOG_INFO("doing so would re-read protection status from MCU.");
1015 static int kinetis_protect_check(struct flash_bank
*bank
)
1017 struct kinetis_flash_bank
*kinfo
= bank
->driver_priv
;
1022 if (kinfo
->flash_class
== FC_PFLASH
) {
1024 /* read protection register */
1025 result
= target_read_u32(bank
->target
, FTFx_FPROT3
, &fprot
);
1026 if (result
!= ERROR_OK
)
1029 /* Every bit protects 1/32 of the full flash (not necessarily just this bank) */
1031 } else if (kinfo
->flash_class
== FC_FLEX_NVM
) {
1034 /* read protection register */
1035 result
= target_read_u8(bank
->target
, FTFx_FDPROT
, &fdprot
);
1036 if (result
!= ERROR_OK
)
1042 LOG_ERROR("Protection checks for FlexRAM not supported");
1043 return ERROR_FLASH_BANK_INVALID
;
1046 b
= kinfo
->protection_block
;
1047 for (i
= 0; i
< bank
->num_prot_blocks
; i
++) {
1048 if ((fprot
>> b
) & 1)
1049 bank
->prot_blocks
[i
].is_protected
= 0;
1051 bank
->prot_blocks
[i
].is_protected
= 1;
1060 static int kinetis_fill_fcf(struct flash_bank
*bank
, uint8_t *fcf
)
1062 uint32_t fprot
= 0xffffffff;
1063 uint8_t fsec
= 0xfe; /* set MCU unsecure */
1064 uint8_t fdprot
= 0xff;
1066 uint32_t pflash_bit
;
1068 struct flash_bank
*bank_iter
;
1069 struct kinetis_flash_bank
*kinfo
;
1071 memset(fcf
, 0xff, FCF_SIZE
);
1076 /* iterate over all kinetis banks */
1077 /* current bank is bank 0, it contains FCF */
1078 for (bank_iter
= bank
; bank_iter
; bank_iter
= bank_iter
->next
) {
1079 if (bank_iter
->driver
!= &kinetis_flash
1080 || bank_iter
->target
!= bank
->target
)
1083 kinetis_auto_probe(bank_iter
);
1085 kinfo
= bank
->driver_priv
;
1089 if (kinfo
->flash_class
== FC_PFLASH
) {
1090 for (i
= 0; i
< bank_iter
->num_prot_blocks
; i
++) {
1091 if (bank_iter
->prot_blocks
[i
].is_protected
== 1)
1092 fprot
&= ~pflash_bit
;
1097 } else if (kinfo
->flash_class
== FC_FLEX_NVM
) {
1098 for (i
= 0; i
< bank_iter
->num_prot_blocks
; i
++) {
1099 if (bank_iter
->prot_blocks
[i
].is_protected
== 1)
1100 fdprot
&= ~dflash_bit
;
1108 target_buffer_set_u32(bank
->target
, fcf
+ FCF_FPROT
, fprot
);
1109 fcf
[FCF_FSEC
] = fsec
;
1110 fcf
[FCF_FOPT
] = fcf_fopt
;
1111 fcf
[FCF_FDPROT
] = fdprot
;
1115 static int kinetis_ftfx_command(struct target
*target
, uint8_t fcmd
, uint32_t faddr
,
1116 uint8_t fccob4
, uint8_t fccob5
, uint8_t fccob6
, uint8_t fccob7
,
1117 uint8_t fccob8
, uint8_t fccob9
, uint8_t fccoba
, uint8_t fccobb
,
1118 uint8_t *ftfx_fstat
)
1120 uint8_t command
[12] = {faddr
& 0xff, (faddr
>> 8) & 0xff, (faddr
>> 16) & 0xff, fcmd
,
1121 fccob7
, fccob6
, fccob5
, fccob4
,
1122 fccobb
, fccoba
, fccob9
, fccob8
};
1125 int64_t ms_timeout
= timeval_ms() + 250;
1127 result
= target_write_memory(target
, FTFx_FCCOB3
, 4, 3, command
);
1128 if (result
!= ERROR_OK
)
1132 result
= target_write_u8(target
, FTFx_FSTAT
, 0x80);
1133 if (result
!= ERROR_OK
)
1138 result
= target_read_u8(target
, FTFx_FSTAT
, &fstat
);
1140 if (result
!= ERROR_OK
)
1146 } while (timeval_ms() < ms_timeout
);
1149 *ftfx_fstat
= fstat
;
1151 if ((fstat
& 0xf0) != 0x80) {
1152 LOG_DEBUG("ftfx command failed FSTAT: %02X FCCOB: %02X%02X%02X%02X %02X%02X%02X%02X %02X%02X%02X%02X",
1153 fstat
, command
[3], command
[2], command
[1], command
[0],
1154 command
[7], command
[6], command
[5], command
[4],
1155 command
[11], command
[10], command
[9], command
[8]);
1157 return kinetis_ftfx_decode_error(fstat
);
1164 static int kinetis_check_run_mode(struct target
*target
)
1167 uint8_t pmctrl
, pmstat
;
1169 if (target
->state
!= TARGET_HALTED
) {
1170 LOG_ERROR("Target not halted");
1171 return ERROR_TARGET_NOT_HALTED
;
1174 result
= target_read_u8(target
, SMC_PMSTAT
, &pmstat
);
1175 if (result
!= ERROR_OK
)
1178 if (pmstat
== PM_STAT_RUN
)
1181 if (pmstat
== PM_STAT_VLPR
) {
1182 /* It is safe to switch from VLPR to RUN mode without changing clock */
1183 LOG_INFO("Switching from VLPR to RUN mode.");
1184 pmctrl
= PM_CTRL_RUNM_RUN
;
1185 result
= target_write_u8(target
, SMC_PMCTRL
, pmctrl
);
1186 if (result
!= ERROR_OK
)
1189 for (i
= 100; i
; i
--) {
1190 result
= target_read_u8(target
, SMC_PMSTAT
, &pmstat
);
1191 if (result
!= ERROR_OK
)
1194 if (pmstat
== PM_STAT_RUN
)
1199 LOG_ERROR("Flash operation not possible in current run mode: SMC_PMSTAT: 0x%x", pmstat
);
1200 LOG_ERROR("Issue a 'reset init' command.");
1201 return ERROR_TARGET_NOT_HALTED
;
1205 static void kinetis_invalidate_flash_cache(struct flash_bank
*bank
)
1207 struct kinetis_flash_bank
*kinfo
= bank
->driver_priv
;
1209 if (kinfo
->flash_support
& FS_INVALIDATE_CACHE_K
)
1210 target_write_u8(bank
->target
, FMC_PFB01CR
+ 2, 0xf0);
1211 /* Set CINV_WAY bits - request invalidate of all cache ways */
1212 /* FMC_PFB0CR has same address and CINV_WAY bits as FMC_PFB01CR */
1214 else if (kinfo
->flash_support
& FS_INVALIDATE_CACHE_L
)
1215 target_write_u8(bank
->target
, MCM_PLACR
+ 1, 0x04);
1216 /* set bit CFCC - Clear Flash Controller Cache */
1218 else if (kinfo
->flash_support
& FS_INVALIDATE_CACHE_MSCM
)
1219 target_write_u32(bank
->target
, MSCM_OCMDR0
, 0x30);
1220 /* disable data prefetch and flash speculate */
1226 static int kinetis_erase(struct flash_bank
*bank
, int first
, int last
)
1229 struct kinetis_flash_bank
*kinfo
= bank
->driver_priv
;
1231 result
= kinetis_check_run_mode(bank
->target
);
1232 if (result
!= ERROR_OK
)
1235 /* reset error flags */
1236 result
= kinetis_ftfx_prepare(bank
->target
);
1237 if (result
!= ERROR_OK
)
1240 if ((first
> bank
->num_sectors
) || (last
> bank
->num_sectors
))
1241 return ERROR_FLASH_OPERATION_FAILED
;
1244 * FIXME: TODO: use the 'Erase Flash Block' command if the
1245 * requested erase is PFlash or NVM and encompasses the entire
1246 * block. Should be quicker.
1248 for (i
= first
; i
<= last
; i
++) {
1249 /* set command and sector address */
1250 result
= kinetis_ftfx_command(bank
->target
, FTFx_CMD_SECTERASE
, kinfo
->prog_base
+ bank
->sectors
[i
].offset
,
1251 0, 0, 0, 0, 0, 0, 0, 0, NULL
);
1253 if (result
!= ERROR_OK
) {
1254 LOG_WARNING("erase sector %d failed", i
);
1255 return ERROR_FLASH_OPERATION_FAILED
;
1258 bank
->sectors
[i
].is_erased
= 1;
1260 if (kinfo
->prog_base
== 0
1261 && bank
->sectors
[i
].offset
<= FCF_ADDRESS
1262 && bank
->sectors
[i
].offset
+ bank
->sectors
[i
].size
> FCF_ADDRESS
+ FCF_SIZE
) {
1263 if (allow_fcf_writes
) {
1264 LOG_WARNING("Flash Configuration Field erased, DO NOT reset or power off the device");
1265 LOG_WARNING("until correct FCF is programmed or MCU gets security lock.");
1267 uint8_t fcf_buffer
[FCF_SIZE
];
1269 kinetis_fill_fcf(bank
, fcf_buffer
);
1270 result
= kinetis_write_inner(bank
, fcf_buffer
, FCF_ADDRESS
, FCF_SIZE
);
1271 if (result
!= ERROR_OK
)
1272 LOG_WARNING("Flash Configuration Field write failed");
1273 bank
->sectors
[i
].is_erased
= 0;
1278 kinetis_invalidate_flash_cache(bank
);
1283 static int kinetis_make_ram_ready(struct target
*target
)
1288 /* check if ram ready */
1289 result
= target_read_u8(target
, FTFx_FCNFG
, &ftfx_fcnfg
);
1290 if (result
!= ERROR_OK
)
1293 if (ftfx_fcnfg
& (1 << 1))
1294 return ERROR_OK
; /* ram ready */
1296 /* make flex ram available */
1297 result
= kinetis_ftfx_command(target
, FTFx_CMD_SETFLEXRAM
, 0x00ff0000,
1298 0, 0, 0, 0, 0, 0, 0, 0, NULL
);
1299 if (result
!= ERROR_OK
)
1300 return ERROR_FLASH_OPERATION_FAILED
;
1303 result
= target_read_u8(target
, FTFx_FCNFG
, &ftfx_fcnfg
);
1304 if (result
!= ERROR_OK
)
1307 if (ftfx_fcnfg
& (1 << 1))
1308 return ERROR_OK
; /* ram ready */
1310 return ERROR_FLASH_OPERATION_FAILED
;
1314 static int kinetis_write_sections(struct flash_bank
*bank
, const uint8_t *buffer
,
1315 uint32_t offset
, uint32_t count
)
1317 int result
= ERROR_OK
;
1318 struct kinetis_flash_bank
*kinfo
= bank
->driver_priv
;
1319 uint8_t *buffer_aligned
= NULL
;
1321 * Kinetis uses different terms for the granularity of
1322 * sector writes, e.g. "phrase" or "128 bits". We use
1323 * the generic term "chunk". The largest possible
1324 * Kinetis "chunk" is 16 bytes (128 bits).
1326 uint32_t prog_section_chunk_bytes
= kinfo
->sector_size
>> 8;
1327 uint32_t prog_size_bytes
= kinfo
->max_flash_prog_size
;
1330 uint32_t size
= prog_size_bytes
- offset
% prog_size_bytes
;
1331 uint32_t align_begin
= offset
% prog_section_chunk_bytes
;
1333 uint32_t size_aligned
;
1334 uint16_t chunk_count
;
1340 align_end
= (align_begin
+ size
) % prog_section_chunk_bytes
;
1342 align_end
= prog_section_chunk_bytes
- align_end
;
1344 size_aligned
= align_begin
+ size
+ align_end
;
1345 chunk_count
= size_aligned
/ prog_section_chunk_bytes
;
1347 if (size
!= size_aligned
) {
1348 /* aligned section: the first, the last or the only */
1349 if (!buffer_aligned
)
1350 buffer_aligned
= malloc(prog_size_bytes
);
1352 memset(buffer_aligned
, 0xff, size_aligned
);
1353 memcpy(buffer_aligned
+ align_begin
, buffer
, size
);
1355 result
= target_write_memory(bank
->target
, kinfo
->progr_accel_ram
,
1356 4, size_aligned
/ 4, buffer_aligned
);
1358 LOG_DEBUG("section @ %08" PRIx32
" aligned begin %" PRIu32
", end %" PRIu32
,
1359 bank
->base
+ offset
, align_begin
, align_end
);
1361 result
= target_write_memory(bank
->target
, kinfo
->progr_accel_ram
,
1362 4, size_aligned
/ 4, buffer
);
1364 LOG_DEBUG("write section @ %08" PRIx32
" with length %" PRIu32
" bytes",
1365 bank
->base
+ offset
, size
);
1367 if (result
!= ERROR_OK
) {
1368 LOG_ERROR("target_write_memory failed");
1372 /* execute section-write command */
1373 result
= kinetis_ftfx_command(bank
->target
, FTFx_CMD_SECTWRITE
,
1374 kinfo
->prog_base
+ offset
- align_begin
,
1375 chunk_count
>>8, chunk_count
, 0, 0,
1376 0, 0, 0, 0, &ftfx_fstat
);
1378 if (result
!= ERROR_OK
) {
1379 LOG_ERROR("Error writing section at %08" PRIx32
, bank
->base
+ offset
);
1383 if (ftfx_fstat
& 0x01) {
1384 LOG_ERROR("Flash write error at %08" PRIx32
, bank
->base
+ offset
);
1385 if (kinfo
->prog_base
== 0 && offset
== FCF_ADDRESS
+ FCF_SIZE
1386 && (kinfo
->flash_support
& FS_WIDTH_256BIT
)) {
1387 LOG_ERROR("Flash write immediately after the end of Flash Config Field shows error");
1388 LOG_ERROR("because the flash memory is 256 bits wide (data were written correctly).");
1389 LOG_ERROR("Either change the linker script to add a gap of 16 bytes after FCF");
1390 LOG_ERROR("or set 'kinetis fcf_source write'");
1399 free(buffer_aligned
);
1404 static int kinetis_write_inner(struct flash_bank
*bank
, const uint8_t *buffer
,
1405 uint32_t offset
, uint32_t count
)
1407 int result
, fallback
= 0;
1408 struct kinetis_flash_bank
*kinfo
= bank
->driver_priv
;
1410 if (!(kinfo
->flash_support
& FS_PROGRAM_SECTOR
)) {
1411 /* fallback to longword write */
1413 LOG_INFO("This device supports Program Longword execution only.");
1415 result
= kinetis_make_ram_ready(bank
->target
);
1416 if (result
!= ERROR_OK
) {
1418 LOG_WARNING("FlexRAM not ready, fallback to slow longword write.");
1422 LOG_DEBUG("flash write @ %08" PRIx32
, bank
->base
+ offset
);
1424 if (fallback
== 0) {
1425 /* program section command */
1426 kinetis_write_sections(bank
, buffer
, offset
, count
);
1428 else if (kinfo
->flash_support
& FS_PROGRAM_LONGWORD
) {
1429 /* program longword command, not supported in FTFE */
1430 uint8_t *new_buffer
= NULL
;
1432 /* check word alignment */
1434 LOG_ERROR("offset 0x%" PRIx32
" breaks the required alignment", offset
);
1435 return ERROR_FLASH_DST_BREAKS_ALIGNMENT
;
1439 uint32_t old_count
= count
;
1440 count
= (old_count
| 3) + 1;
1441 new_buffer
= malloc(count
);
1442 if (new_buffer
== NULL
) {
1443 LOG_ERROR("odd number of bytes to write and no memory "
1444 "for padding buffer");
1447 LOG_INFO("odd number of bytes to write (%" PRIu32
"), extending to %" PRIu32
" "
1448 "and padding with 0xff", old_count
, count
);
1449 memset(new_buffer
+ old_count
, 0xff, count
- old_count
);
1450 buffer
= memcpy(new_buffer
, buffer
, old_count
);
1453 uint32_t words_remaining
= count
/ 4;
1455 kinetis_disable_wdog(bank
->target
, kinfo
->sim_sdid
);
1457 /* try using a block write */
1458 result
= kinetis_write_block(bank
, buffer
, offset
, words_remaining
);
1460 if (result
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
) {
1461 /* if block write failed (no sufficient working area),
1462 * we use normal (slow) single word accesses */
1463 LOG_WARNING("couldn't use block writes, falling back to single "
1466 while (words_remaining
) {
1469 LOG_DEBUG("write longword @ %08" PRIx32
, (uint32_t)(bank
->base
+ offset
));
1471 result
= kinetis_ftfx_command(bank
->target
, FTFx_CMD_LWORDPROG
, kinfo
->prog_base
+ offset
,
1472 buffer
[3], buffer
[2], buffer
[1], buffer
[0],
1473 0, 0, 0, 0, &ftfx_fstat
);
1475 if (result
!= ERROR_OK
) {
1476 LOG_ERROR("Error writing longword at %08" PRIx32
, bank
->base
+ offset
);
1480 if (ftfx_fstat
& 0x01)
1481 LOG_ERROR("Flash write error at %08" PRIx32
, bank
->base
+ offset
);
1490 LOG_ERROR("Flash write strategy not implemented");
1491 return ERROR_FLASH_OPERATION_FAILED
;
1494 kinetis_invalidate_flash_cache(bank
);
1499 static int kinetis_write(struct flash_bank
*bank
, const uint8_t *buffer
,
1500 uint32_t offset
, uint32_t count
)
1503 bool set_fcf
= false;
1505 struct kinetis_flash_bank
*kinfo
= bank
->driver_priv
;
1507 result
= kinetis_check_run_mode(bank
->target
);
1508 if (result
!= ERROR_OK
)
1511 /* reset error flags */
1512 result
= kinetis_ftfx_prepare(bank
->target
);
1513 if (result
!= ERROR_OK
)
1516 if (kinfo
->prog_base
== 0 && !allow_fcf_writes
) {
1517 if (bank
->sectors
[1].offset
<= FCF_ADDRESS
)
1518 sect
= 1; /* 1kb sector, FCF in 2nd sector */
1520 if (offset
< bank
->sectors
[sect
].offset
+ bank
->sectors
[sect
].size
1521 && offset
+ count
> bank
->sectors
[sect
].offset
)
1522 set_fcf
= true; /* write to any part of sector with FCF */
1526 uint8_t fcf_buffer
[FCF_SIZE
];
1527 uint8_t fcf_current
[FCF_SIZE
];
1529 kinetis_fill_fcf(bank
, fcf_buffer
);
1531 if (offset
< FCF_ADDRESS
) {
1532 /* write part preceding FCF */
1533 result
= kinetis_write_inner(bank
, buffer
, offset
, FCF_ADDRESS
- offset
);
1534 if (result
!= ERROR_OK
)
1538 result
= target_read_memory(bank
->target
, bank
->base
+ FCF_ADDRESS
, 4, FCF_SIZE
/ 4, fcf_current
);
1539 if (result
== ERROR_OK
&& memcmp(fcf_current
, fcf_buffer
, FCF_SIZE
) == 0)
1543 /* write FCF if differs from flash - eliminate multiple writes */
1544 result
= kinetis_write_inner(bank
, fcf_buffer
, FCF_ADDRESS
, FCF_SIZE
);
1545 if (result
!= ERROR_OK
)
1549 LOG_WARNING("Flash Configuration Field written.");
1550 LOG_WARNING("Reset or power off the device to make settings effective.");
1552 if (offset
+ count
> FCF_ADDRESS
+ FCF_SIZE
) {
1553 uint32_t delta
= FCF_ADDRESS
+ FCF_SIZE
- offset
;
1554 /* write part after FCF */
1555 result
= kinetis_write_inner(bank
, buffer
+ delta
, FCF_ADDRESS
+ FCF_SIZE
, count
- delta
);
1560 /* no FCF fiddling, normal write */
1561 return kinetis_write_inner(bank
, buffer
, offset
, count
);
1565 static int kinetis_probe(struct flash_bank
*bank
)
1568 uint8_t fcfg1_nvmsize
, fcfg1_pfsize
, fcfg1_eesize
, fcfg1_depart
;
1569 uint8_t fcfg2_maxaddr0
, fcfg2_pflsh
, fcfg2_maxaddr1
;
1570 uint32_t nvm_size
= 0, pf_size
= 0, df_size
= 0, ee_size
= 0;
1571 unsigned num_blocks
= 0, num_pflash_blocks
= 0, num_nvm_blocks
= 0, first_nvm_bank
= 0,
1572 pflash_sector_size_bytes
= 0, nvm_sector_size_bytes
= 0;
1573 unsigned maxaddr_shift
= 13;
1574 struct target
*target
= bank
->target
;
1575 struct kinetis_flash_bank
*kinfo
= bank
->driver_priv
;
1577 kinfo
->probed
= false;
1578 kinfo
->progr_accel_ram
= FLEXRAM
;
1580 result
= target_read_u32(target
, SIM_SDID
, &kinfo
->sim_sdid
);
1581 if (result
!= ERROR_OK
)
1584 if ((kinfo
->sim_sdid
& (~KINETIS_SDID_K_SERIES_MASK
)) == 0) {
1585 /* older K-series MCU */
1586 uint32_t mcu_type
= kinfo
->sim_sdid
& KINETIS_K_SDID_TYPE_MASK
;
1589 case KINETIS_K_SDID_K10_M50
:
1590 case KINETIS_K_SDID_K20_M50
:
1592 pflash_sector_size_bytes
= 1<<10;
1593 nvm_sector_size_bytes
= 1<<10;
1595 kinfo
->flash_support
= FS_PROGRAM_LONGWORD
| FS_PROGRAM_SECTOR
| FS_INVALIDATE_CACHE_K
;
1597 case KINETIS_K_SDID_K10_M72
:
1598 case KINETIS_K_SDID_K20_M72
:
1599 case KINETIS_K_SDID_K30_M72
:
1600 case KINETIS_K_SDID_K30_M100
:
1601 case KINETIS_K_SDID_K40_M72
:
1602 case KINETIS_K_SDID_K40_M100
:
1603 case KINETIS_K_SDID_K50_M72
:
1604 /* 2kB sectors, 1kB FlexNVM sectors */
1605 pflash_sector_size_bytes
= 2<<10;
1606 nvm_sector_size_bytes
= 1<<10;
1608 kinfo
->flash_support
= FS_PROGRAM_LONGWORD
| FS_PROGRAM_SECTOR
| FS_INVALIDATE_CACHE_K
;
1609 kinfo
->max_flash_prog_size
= 1<<10;
1611 case KINETIS_K_SDID_K10_M100
:
1612 case KINETIS_K_SDID_K20_M100
:
1613 case KINETIS_K_SDID_K11
:
1614 case KINETIS_K_SDID_K12
:
1615 case KINETIS_K_SDID_K21_M50
:
1616 case KINETIS_K_SDID_K22_M50
:
1617 case KINETIS_K_SDID_K51_M72
:
1618 case KINETIS_K_SDID_K53
:
1619 case KINETIS_K_SDID_K60_M100
:
1621 pflash_sector_size_bytes
= 2<<10;
1622 nvm_sector_size_bytes
= 2<<10;
1624 kinfo
->flash_support
= FS_PROGRAM_LONGWORD
| FS_PROGRAM_SECTOR
| FS_INVALIDATE_CACHE_K
;
1626 case KINETIS_K_SDID_K21_M120
:
1627 case KINETIS_K_SDID_K22_M120
:
1628 /* 4kB sectors (MK21FN1M0, MK21FX512, MK22FN1M0, MK22FX512) */
1629 pflash_sector_size_bytes
= 4<<10;
1630 kinfo
->max_flash_prog_size
= 1<<10;
1631 nvm_sector_size_bytes
= 4<<10;
1633 kinfo
->flash_support
= FS_PROGRAM_PHRASE
| FS_PROGRAM_SECTOR
| FS_INVALIDATE_CACHE_K
;
1635 case KINETIS_K_SDID_K10_M120
:
1636 case KINETIS_K_SDID_K20_M120
:
1637 case KINETIS_K_SDID_K60_M150
:
1638 case KINETIS_K_SDID_K70_M150
:
1640 pflash_sector_size_bytes
= 4<<10;
1641 nvm_sector_size_bytes
= 4<<10;
1643 kinfo
->flash_support
= FS_PROGRAM_PHRASE
| FS_PROGRAM_SECTOR
| FS_INVALIDATE_CACHE_K
;
1646 LOG_ERROR("Unsupported K-family FAMID");
1649 /* Newer K-series or KL series MCU */
1650 switch (kinfo
->sim_sdid
& KINETIS_SDID_SERIESID_MASK
) {
1651 case KINETIS_SDID_SERIESID_K
:
1652 switch (kinfo
->sim_sdid
& (KINETIS_SDID_FAMILYID_MASK
| KINETIS_SDID_SUBFAMID_MASK
)) {
1653 case KINETIS_SDID_FAMILYID_K0X
| KINETIS_SDID_SUBFAMID_KX2
:
1654 /* K02FN64, K02FN128: FTFA, 2kB sectors */
1655 pflash_sector_size_bytes
= 2<<10;
1657 kinfo
->flash_support
= FS_PROGRAM_LONGWORD
| FS_INVALIDATE_CACHE_K
;
1660 case KINETIS_SDID_FAMILYID_K2X
| KINETIS_SDID_SUBFAMID_KX2
: {
1661 /* MK24FN1M reports as K22, this should detect it (according to errata note 1N83J) */
1663 result
= target_read_u32(target
, SIM_SOPT1
, &sopt1
);
1664 if (result
!= ERROR_OK
)
1667 if (((kinfo
->sim_sdid
& (KINETIS_SDID_DIEID_MASK
)) == KINETIS_SDID_DIEID_K24FN1M
) &&
1668 ((sopt1
& KINETIS_SOPT1_RAMSIZE_MASK
) == KINETIS_SOPT1_RAMSIZE_K24FN1M
)) {
1670 pflash_sector_size_bytes
= 4<<10;
1672 kinfo
->flash_support
= FS_PROGRAM_PHRASE
| FS_PROGRAM_SECTOR
| FS_INVALIDATE_CACHE_K
;
1673 kinfo
->max_flash_prog_size
= 1<<10;
1676 if ((kinfo
->sim_sdid
& (KINETIS_SDID_DIEID_MASK
)) == KINETIS_SDID_DIEID_K22FN128
1677 || (kinfo
->sim_sdid
& (KINETIS_SDID_DIEID_MASK
)) == KINETIS_SDID_DIEID_K22FN256
1678 || (kinfo
->sim_sdid
& (KINETIS_SDID_DIEID_MASK
)) == KINETIS_SDID_DIEID_K22FN512
) {
1679 /* K22 with new-style SDID - smaller pflash with FTFA, 2kB sectors */
1680 pflash_sector_size_bytes
= 2<<10;
1681 /* autodetect 1 or 2 blocks */
1682 kinfo
->flash_support
= FS_PROGRAM_LONGWORD
| FS_INVALIDATE_CACHE_K
;
1685 LOG_ERROR("Unsupported Kinetis K22 DIEID");
1688 case KINETIS_SDID_FAMILYID_K2X
| KINETIS_SDID_SUBFAMID_KX4
:
1689 pflash_sector_size_bytes
= 4<<10;
1690 if ((kinfo
->sim_sdid
& (KINETIS_SDID_DIEID_MASK
)) == KINETIS_SDID_DIEID_K24FN256
) {
1691 /* K24FN256 - smaller pflash with FTFA */
1693 kinfo
->flash_support
= FS_PROGRAM_LONGWORD
| FS_INVALIDATE_CACHE_K
;
1696 /* K24FN1M without errata 7534 */
1698 kinfo
->flash_support
= FS_PROGRAM_PHRASE
| FS_PROGRAM_SECTOR
| FS_INVALIDATE_CACHE_K
;
1699 kinfo
->max_flash_prog_size
= 1<<10;
1702 case KINETIS_SDID_FAMILYID_K6X
| KINETIS_SDID_SUBFAMID_KX3
:
1703 case KINETIS_SDID_FAMILYID_K6X
| KINETIS_SDID_SUBFAMID_KX1
: /* errata 7534 - should be K63 */
1705 case KINETIS_SDID_FAMILYID_K6X
| KINETIS_SDID_SUBFAMID_KX4
:
1706 case KINETIS_SDID_FAMILYID_K6X
| KINETIS_SDID_SUBFAMID_KX2
: /* errata 7534 - should be K64 */
1707 /* K64FN1M0, K64FX512 */
1708 pflash_sector_size_bytes
= 4<<10;
1709 nvm_sector_size_bytes
= 4<<10;
1710 kinfo
->max_flash_prog_size
= 1<<10;
1712 kinfo
->flash_support
= FS_PROGRAM_PHRASE
| FS_PROGRAM_SECTOR
| FS_INVALIDATE_CACHE_K
;
1715 case KINETIS_SDID_FAMILYID_K2X
| KINETIS_SDID_SUBFAMID_KX6
:
1717 case KINETIS_SDID_FAMILYID_K6X
| KINETIS_SDID_SUBFAMID_KX6
:
1718 /* K66FN2M0, K66FX1M0 */
1719 pflash_sector_size_bytes
= 4<<10;
1720 nvm_sector_size_bytes
= 4<<10;
1721 kinfo
->max_flash_prog_size
= 1<<10;
1723 kinfo
->flash_support
= FS_PROGRAM_PHRASE
| FS_PROGRAM_SECTOR
| FS_INVALIDATE_CACHE_K
;
1726 case KINETIS_SDID_FAMILYID_K8X
| KINETIS_SDID_SUBFAMID_KX0
:
1727 case KINETIS_SDID_FAMILYID_K8X
| KINETIS_SDID_SUBFAMID_KX1
:
1728 case KINETIS_SDID_FAMILYID_K8X
| KINETIS_SDID_SUBFAMID_KX2
:
1729 /* K80FN256, K81FN256, K82FN256 */
1730 pflash_sector_size_bytes
= 4<<10;
1732 kinfo
->flash_support
= FS_PROGRAM_LONGWORD
| FS_INVALIDATE_CACHE_K
| FS_NO_CMD_BLOCKSTAT
;
1735 case KINETIS_SDID_FAMILYID_KL8X
| KINETIS_SDID_SUBFAMID_KX1
:
1736 case KINETIS_SDID_FAMILYID_KL8X
| KINETIS_SDID_SUBFAMID_KX2
:
1737 /* KL81Z128, KL82Z128 */
1738 pflash_sector_size_bytes
= 2<<10;
1740 kinfo
->flash_support
= FS_PROGRAM_LONGWORD
| FS_INVALIDATE_CACHE_L
| FS_NO_CMD_BLOCKSTAT
;
1744 LOG_ERROR("Unsupported Kinetis FAMILYID SUBFAMID");
1748 case KINETIS_SDID_SERIESID_KL
:
1750 pflash_sector_size_bytes
= 1<<10;
1751 nvm_sector_size_bytes
= 1<<10;
1752 /* autodetect 1 or 2 blocks */
1753 kinfo
->flash_support
= FS_PROGRAM_LONGWORD
| FS_INVALIDATE_CACHE_L
;
1756 case KINETIS_SDID_SERIESID_KV
:
1758 switch (kinfo
->sim_sdid
& (KINETIS_SDID_FAMILYID_MASK
| KINETIS_SDID_SUBFAMID_MASK
)) {
1759 case KINETIS_SDID_FAMILYID_K1X
| KINETIS_SDID_SUBFAMID_KX0
:
1760 /* KV10: FTFA, 1kB sectors */
1761 pflash_sector_size_bytes
= 1<<10;
1763 kinfo
->flash_support
= FS_PROGRAM_LONGWORD
| FS_INVALIDATE_CACHE_L
;
1766 case KINETIS_SDID_FAMILYID_K1X
| KINETIS_SDID_SUBFAMID_KX1
:
1767 /* KV11: FTFA, 2kB sectors */
1768 pflash_sector_size_bytes
= 2<<10;
1770 kinfo
->flash_support
= FS_PROGRAM_LONGWORD
| FS_INVALIDATE_CACHE_L
;
1773 case KINETIS_SDID_FAMILYID_K3X
| KINETIS_SDID_SUBFAMID_KX0
:
1774 /* KV30: FTFA, 2kB sectors, 1 block */
1775 case KINETIS_SDID_FAMILYID_K3X
| KINETIS_SDID_SUBFAMID_KX1
:
1776 /* KV31: FTFA, 2kB sectors, 2 blocks */
1777 pflash_sector_size_bytes
= 2<<10;
1778 /* autodetect 1 or 2 blocks */
1779 kinfo
->flash_support
= FS_PROGRAM_LONGWORD
| FS_INVALIDATE_CACHE_K
;
1782 case KINETIS_SDID_FAMILYID_K4X
| KINETIS_SDID_SUBFAMID_KX2
:
1783 case KINETIS_SDID_FAMILYID_K4X
| KINETIS_SDID_SUBFAMID_KX4
:
1784 case KINETIS_SDID_FAMILYID_K4X
| KINETIS_SDID_SUBFAMID_KX6
:
1785 /* KV4x: FTFA, 4kB sectors */
1786 pflash_sector_size_bytes
= 4<<10;
1788 kinfo
->flash_support
= FS_PROGRAM_LONGWORD
| FS_INVALIDATE_CACHE_K
;
1791 case KINETIS_SDID_FAMILYID_K5X
| KINETIS_SDID_SUBFAMID_KX6
:
1792 case KINETIS_SDID_FAMILYID_K5X
| KINETIS_SDID_SUBFAMID_KX8
:
1793 /* KV5x: FTFE, 8kB sectors */
1794 pflash_sector_size_bytes
= 8<<10;
1796 kinfo
->max_flash_prog_size
= 1<<10;
1798 kinfo
->flash_support
= FS_PROGRAM_PHRASE
| FS_PROGRAM_SECTOR
| FS_WIDTH_256BIT
;
1799 bank
->base
= 0x10000000;
1800 kinfo
->prog_base
= 0;
1801 kinfo
->progr_accel_ram
= 0x18000000;
1805 LOG_ERROR("Unsupported KV FAMILYID SUBFAMID");
1809 case KINETIS_SDID_SERIESID_KE
:
1811 switch (kinfo
->sim_sdid
&
1812 (KINETIS_SDID_FAMILYID_MASK
| KINETIS_SDID_SUBFAMID_MASK
| KINETIS_SDID_PROJECTID_MASK
)) {
1813 case KINETIS_SDID_FAMILYID_K1X
| KINETIS_SDID_SUBFAMID_KX4
| KINETIS_SDID_PROJECTID_KE1xZ
:
1814 case KINETIS_SDID_FAMILYID_K1X
| KINETIS_SDID_SUBFAMID_KX5
| KINETIS_SDID_PROJECTID_KE1xZ
:
1815 /* KE1xZ: FTFE, 2kB sectors */
1816 pflash_sector_size_bytes
= 2<<10;
1817 nvm_sector_size_bytes
= 2<<10;
1818 kinfo
->max_flash_prog_size
= 1<<9;
1820 kinfo
->flash_support
= FS_PROGRAM_PHRASE
| FS_PROGRAM_SECTOR
| FS_INVALIDATE_CACHE_L
;
1823 case KINETIS_SDID_FAMILYID_K1X
| KINETIS_SDID_SUBFAMID_KX4
| KINETIS_SDID_PROJECTID_KE1xF
:
1824 case KINETIS_SDID_FAMILYID_K1X
| KINETIS_SDID_SUBFAMID_KX6
| KINETIS_SDID_PROJECTID_KE1xF
:
1825 case KINETIS_SDID_FAMILYID_K1X
| KINETIS_SDID_SUBFAMID_KX8
| KINETIS_SDID_PROJECTID_KE1xF
:
1826 /* KE1xF: FTFE, 4kB sectors */
1827 pflash_sector_size_bytes
= 4<<10;
1828 nvm_sector_size_bytes
= 2<<10;
1829 kinfo
->max_flash_prog_size
= 1<<10;
1831 kinfo
->flash_support
= FS_PROGRAM_PHRASE
| FS_PROGRAM_SECTOR
| FS_INVALIDATE_CACHE_MSCM
;
1835 LOG_ERROR("Unsupported KE FAMILYID SUBFAMID");
1840 LOG_ERROR("Unsupported K-series");
1844 if (pflash_sector_size_bytes
== 0) {
1845 LOG_ERROR("MCU is unsupported, SDID 0x%08" PRIx32
, kinfo
->sim_sdid
);
1846 return ERROR_FLASH_OPER_UNSUPPORTED
;
1849 result
= target_read_u32(target
, SIM_FCFG1
, &kinfo
->sim_fcfg1
);
1850 if (result
!= ERROR_OK
)
1853 result
= target_read_u32(target
, SIM_FCFG2
, &kinfo
->sim_fcfg2
);
1854 if (result
!= ERROR_OK
)
1857 LOG_DEBUG("SDID: 0x%08" PRIX32
" FCFG1: 0x%08" PRIX32
" FCFG2: 0x%08" PRIX32
, kinfo
->sim_sdid
,
1858 kinfo
->sim_fcfg1
, kinfo
->sim_fcfg2
);
1860 fcfg1_nvmsize
= (uint8_t)((kinfo
->sim_fcfg1
>> 28) & 0x0f);
1861 fcfg1_pfsize
= (uint8_t)((kinfo
->sim_fcfg1
>> 24) & 0x0f);
1862 fcfg1_eesize
= (uint8_t)((kinfo
->sim_fcfg1
>> 16) & 0x0f);
1863 fcfg1_depart
= (uint8_t)((kinfo
->sim_fcfg1
>> 8) & 0x0f);
1865 fcfg2_pflsh
= (uint8_t)((kinfo
->sim_fcfg2
>> 23) & 0x01);
1866 fcfg2_maxaddr0
= (uint8_t)((kinfo
->sim_fcfg2
>> 24) & 0x7f);
1867 fcfg2_maxaddr1
= (uint8_t)((kinfo
->sim_fcfg2
>> 16) & 0x7f);
1869 if (num_blocks
== 0)
1870 num_blocks
= fcfg2_maxaddr1
? 2 : 1;
1871 else if (fcfg2_maxaddr1
== 0 && num_blocks
>= 2) {
1873 LOG_WARNING("MAXADDR1 is zero, number of flash banks adjusted to 1");
1874 } else if (fcfg2_maxaddr1
!= 0 && num_blocks
== 1) {
1876 LOG_WARNING("MAXADDR1 is non zero, number of flash banks adjusted to 2");
1879 /* when the PFLSH bit is set, there is no FlexNVM/FlexRAM */
1881 switch (fcfg1_nvmsize
) {
1887 nvm_size
= 1 << (14 + (fcfg1_nvmsize
>> 1));
1890 if (pflash_sector_size_bytes
>= 4<<10)
1901 switch (fcfg1_eesize
) {
1912 ee_size
= (16 << (10 - fcfg1_eesize
));
1919 switch (fcfg1_depart
) {
1926 df_size
= nvm_size
- (4096 << fcfg1_depart
);
1936 df_size
= 4096 << (fcfg1_depart
& 0x7);
1944 switch (fcfg1_pfsize
) {
1951 pf_size
= 1 << (14 + (fcfg1_pfsize
>> 1));
1954 /* a peculiar case: Freescale states different sizes for 0xf
1955 * K02P64M100SFARM 128 KB ... duplicate of code 0x7
1956 * K22P121M120SF8RM 256 KB ... duplicate of code 0x9
1957 * K22P121M120SF7RM 512 KB ... duplicate of code 0xb
1958 * K22P100M120SF5RM 1024 KB ... duplicate of code 0xd
1959 * K26P169M180SF5RM 2048 KB ... the only unique value
1960 * fcfg2_maxaddr0 seems to be the only clue to pf_size
1961 * Checking fcfg2_maxaddr0 later in this routine is pointless then
1964 pf_size
= ((uint32_t)fcfg2_maxaddr0
<< maxaddr_shift
) * num_blocks
;
1966 pf_size
= ((uint32_t)fcfg2_maxaddr0
<< maxaddr_shift
) * num_blocks
/ 2;
1967 if (pf_size
!= 2048<<10)
1968 LOG_WARNING("SIM_FCFG1 PFSIZE = 0xf: please check if pflash is %u KB", pf_size
>>10);
1976 LOG_DEBUG("FlexNVM: %" PRIu32
" PFlash: %" PRIu32
" FlexRAM: %" PRIu32
" PFLSH: %d",
1977 nvm_size
, pf_size
, ee_size
, fcfg2_pflsh
);
1979 num_pflash_blocks
= num_blocks
/ (2 - fcfg2_pflsh
);
1980 first_nvm_bank
= num_pflash_blocks
;
1981 num_nvm_blocks
= num_blocks
- num_pflash_blocks
;
1983 LOG_DEBUG("%d blocks total: %d PFlash, %d FlexNVM",
1984 num_blocks
, num_pflash_blocks
, num_nvm_blocks
);
1986 LOG_INFO("Probing flash info for bank %d", bank
->bank_number
);
1988 if ((unsigned)bank
->bank_number
< num_pflash_blocks
) {
1989 /* pflash, banks start at address zero */
1990 kinfo
->flash_class
= FC_PFLASH
;
1991 bank
->size
= (pf_size
/ num_pflash_blocks
);
1992 if (bank
->base
== 0) {
1993 bank
->base
= 0x00000000 + bank
->size
* bank
->bank_number
;
1994 kinfo
->prog_base
= bank
->base
;
1996 kinfo
->sector_size
= pflash_sector_size_bytes
;
1997 /* pflash is divided into 32 protection areas for
1998 * parts with more than 32K of PFlash. For parts with
1999 * less the protection unit is set to 1024 bytes */
2000 kinfo
->protection_size
= MAX(pf_size
/ 32, 1024);
2001 bank
->num_prot_blocks
= 32 / num_pflash_blocks
;
2002 kinfo
->protection_block
= bank
->num_prot_blocks
* bank
->bank_number
;
2004 } else if ((unsigned)bank
->bank_number
< num_blocks
) {
2005 /* nvm, banks start at address 0x10000000 */
2006 unsigned nvm_ord
= bank
->bank_number
- first_nvm_bank
;
2009 kinfo
->flash_class
= FC_FLEX_NVM
;
2010 bank
->size
= (nvm_size
/ num_nvm_blocks
);
2011 bank
->base
= 0x10000000 + bank
->size
* nvm_ord
;
2012 kinfo
->prog_base
= 0x00800000 + bank
->size
* nvm_ord
;
2013 kinfo
->sector_size
= nvm_sector_size_bytes
;
2015 kinfo
->protection_size
= 0;
2017 for (i
= df_size
; ~i
& 1; i
>>= 1)
2020 kinfo
->protection_size
= df_size
/ 8; /* data flash size = 2^^n */
2022 kinfo
->protection_size
= nvm_size
/ 8; /* TODO: verify on SF1, not documented in RM */
2024 bank
->num_prot_blocks
= 8 / num_nvm_blocks
;
2025 kinfo
->protection_block
= bank
->num_prot_blocks
* nvm_ord
;
2027 /* EEPROM backup part of FlexNVM is not accessible, use df_size as a limit */
2028 if (df_size
> bank
->size
* nvm_ord
)
2029 limit
= df_size
- bank
->size
* nvm_ord
;
2033 if (bank
->size
> limit
) {
2035 LOG_DEBUG("FlexNVM bank %d limited to 0x%08" PRIx32
" due to active EEPROM backup",
2036 bank
->bank_number
, limit
);
2039 } else if ((unsigned)bank
->bank_number
== num_blocks
) {
2040 LOG_ERROR("FlexRAM support not yet implemented");
2041 return ERROR_FLASH_OPER_UNSUPPORTED
;
2043 LOG_ERROR("Cannot determine parameters for bank %d, only %d banks on device",
2044 bank
->bank_number
, num_blocks
);
2045 return ERROR_FLASH_BANK_INVALID
;
2048 if (bank
->bank_number
== 0 && ((uint32_t)fcfg2_maxaddr0
<< maxaddr_shift
) != bank
->size
)
2049 LOG_WARNING("MAXADDR0 0x%02" PRIx8
" check failed,"
2050 " please report to OpenOCD mailing list", fcfg2_maxaddr0
);
2052 if (bank
->bank_number
== 1 && ((uint32_t)fcfg2_maxaddr1
<< maxaddr_shift
) != bank
->size
)
2053 LOG_WARNING("MAXADDR1 0x%02" PRIx8
" check failed,"
2054 " please report to OpenOCD mailing list", fcfg2_maxaddr1
);
2056 if ((unsigned)bank
->bank_number
== first_nvm_bank
2057 && ((uint32_t)fcfg2_maxaddr1
<< maxaddr_shift
) != df_size
)
2058 LOG_WARNING("FlexNVM MAXADDR1 0x%02" PRIx8
" check failed,"
2059 " please report to OpenOCD mailing list", fcfg2_maxaddr1
);
2062 if (bank
->sectors
) {
2063 free(bank
->sectors
);
2064 bank
->sectors
= NULL
;
2066 if (bank
->prot_blocks
) {
2067 free(bank
->prot_blocks
);
2068 bank
->prot_blocks
= NULL
;
2071 if (kinfo
->sector_size
== 0) {
2072 LOG_ERROR("Unknown sector size for bank %d", bank
->bank_number
);
2073 return ERROR_FLASH_BANK_INVALID
;
2076 if (kinfo
->flash_support
& FS_PROGRAM_SECTOR
2077 && kinfo
->max_flash_prog_size
== 0) {
2078 kinfo
->max_flash_prog_size
= kinfo
->sector_size
;
2079 /* Program section size is equal to sector size by default */
2082 bank
->num_sectors
= bank
->size
/ kinfo
->sector_size
;
2084 if (bank
->num_sectors
> 0) {
2085 /* FlexNVM bank can be used for EEPROM backup therefore zero sized */
2086 bank
->sectors
= alloc_block_array(0, kinfo
->sector_size
, bank
->num_sectors
);
2090 bank
->prot_blocks
= alloc_block_array(0, kinfo
->protection_size
, bank
->num_prot_blocks
);
2091 if (!bank
->prot_blocks
)
2095 bank
->num_prot_blocks
= 0;
2098 kinfo
->probed
= true;
2103 static int kinetis_auto_probe(struct flash_bank
*bank
)
2105 struct kinetis_flash_bank
*kinfo
= bank
->driver_priv
;
2107 if (kinfo
&& kinfo
->probed
)
2110 return kinetis_probe(bank
);
2113 static int kinetis_info(struct flash_bank
*bank
, char *buf
, int buf_size
)
2115 const char *bank_class_names
[] = {
2116 "(ANY)", "PFlash", "FlexNVM", "FlexRAM"
2119 struct kinetis_flash_bank
*kinfo
= bank
->driver_priv
;
2121 (void) snprintf(buf
, buf_size
,
2122 "%s driver for %s flash bank %s at 0x%8.8" PRIx32
"",
2123 bank
->driver
->name
, bank_class_names
[kinfo
->flash_class
],
2124 bank
->name
, bank
->base
);
2129 static int kinetis_blank_check(struct flash_bank
*bank
)
2131 struct kinetis_flash_bank
*kinfo
= bank
->driver_priv
;
2134 /* suprisingly blank check does not work in VLPR and HSRUN modes */
2135 result
= kinetis_check_run_mode(bank
->target
);
2136 if (result
!= ERROR_OK
)
2139 /* reset error flags */
2140 result
= kinetis_ftfx_prepare(bank
->target
);
2141 if (result
!= ERROR_OK
)
2144 if (kinfo
->flash_class
== FC_PFLASH
|| kinfo
->flash_class
== FC_FLEX_NVM
) {
2145 bool block_dirty
= true;
2146 bool use_block_cmd
= !(kinfo
->flash_support
& FS_NO_CMD_BLOCKSTAT
);
2149 if (use_block_cmd
&& kinfo
->flash_class
== FC_FLEX_NVM
) {
2150 uint8_t fcfg1_depart
= (uint8_t)((kinfo
->sim_fcfg1
>> 8) & 0x0f);
2151 /* block operation cannot be used on FlexNVM when EEPROM backup partition is set */
2152 if (fcfg1_depart
!= 0xf && fcfg1_depart
!= 0)
2153 use_block_cmd
= false;
2156 if (use_block_cmd
) {
2157 /* check if whole bank is blank */
2158 result
= kinetis_ftfx_command(bank
->target
, FTFx_CMD_BLOCKSTAT
, kinfo
->prog_base
,
2159 0, 0, 0, 0, 0, 0, 0, 0, &ftfx_fstat
);
2161 if (result
!= ERROR_OK
)
2162 kinetis_ftfx_clear_error(bank
->target
);
2163 else if ((ftfx_fstat
& 0x01) == 0)
2164 block_dirty
= false;
2168 /* the whole bank is not erased, check sector-by-sector */
2170 for (i
= 0; i
< bank
->num_sectors
; i
++) {
2172 result
= kinetis_ftfx_command(bank
->target
, FTFx_CMD_SECTSTAT
,
2173 kinfo
->prog_base
+ bank
->sectors
[i
].offset
,
2174 1, 0, 0, 0, 0, 0, 0, 0, &ftfx_fstat
);
2176 if (result
== ERROR_OK
) {
2177 bank
->sectors
[i
].is_erased
= !(ftfx_fstat
& 0x01);
2179 LOG_DEBUG("Ignoring errored PFlash sector blank-check");
2180 kinetis_ftfx_clear_error(bank
->target
);
2181 bank
->sectors
[i
].is_erased
= -1;
2185 /* the whole bank is erased, update all sectors */
2187 for (i
= 0; i
< bank
->num_sectors
; i
++)
2188 bank
->sectors
[i
].is_erased
= 1;
2191 LOG_WARNING("kinetis_blank_check not supported yet for FlexRAM");
2192 return ERROR_FLASH_OPERATION_FAILED
;
2199 COMMAND_HANDLER(kinetis_nvm_partition
)
2202 unsigned long par
, log2
= 0, ee1
= 0, ee2
= 0;
2203 enum { SHOW_INFO
, DF_SIZE
, EEBKP_SIZE
} sz_type
= SHOW_INFO
;
2205 uint8_t load_flex_ram
= 1;
2206 uint8_t ee_size_code
= 0x3f;
2207 uint8_t flex_nvm_partition_code
= 0;
2208 uint8_t ee_split
= 3;
2209 struct target
*target
= get_current_target(CMD_CTX
);
2210 struct flash_bank
*bank
;
2211 struct kinetis_flash_bank
*kinfo
;
2214 if (CMD_ARGC
>= 2) {
2215 if (strcmp(CMD_ARGV
[0], "dataflash") == 0)
2217 else if (strcmp(CMD_ARGV
[0], "eebkp") == 0)
2218 sz_type
= EEBKP_SIZE
;
2220 par
= strtoul(CMD_ARGV
[1], NULL
, 10);
2221 while (par
>> (log2
+ 3))
2226 result
= target_read_u32(target
, SIM_FCFG1
, &sim_fcfg1
);
2227 if (result
!= ERROR_OK
)
2230 flex_nvm_partition_code
= (uint8_t)((sim_fcfg1
>> 8) & 0x0f);
2231 switch (flex_nvm_partition_code
) {
2233 command_print(CMD_CTX
, "No EEPROM backup, data flash only");
2241 command_print(CMD_CTX
, "EEPROM backup %d KB", 4 << flex_nvm_partition_code
);
2244 command_print(CMD_CTX
, "No data flash, EEPROM backup only");
2252 command_print(CMD_CTX
, "data flash %d KB", 4 << (flex_nvm_partition_code
& 7));
2255 command_print(CMD_CTX
, "No EEPROM backup, data flash only (DEPART not set)");
2258 command_print(CMD_CTX
, "Unsupported EEPROM backup size code 0x%02" PRIx8
, flex_nvm_partition_code
);
2263 flex_nvm_partition_code
= 0x8 | log2
;
2267 flex_nvm_partition_code
= log2
;
2272 ee1
= ee2
= strtoul(CMD_ARGV
[2], NULL
, 10) / 2;
2273 else if (CMD_ARGC
>= 4) {
2274 ee1
= strtoul(CMD_ARGV
[2], NULL
, 10);
2275 ee2
= strtoul(CMD_ARGV
[3], NULL
, 10);
2278 enable
= ee1
+ ee2
> 0;
2280 for (log2
= 2; ; log2
++) {
2281 if (ee1
+ ee2
== (16u << 10) >> log2
)
2283 if (ee1
+ ee2
> (16u << 10) >> log2
|| log2
>= 9) {
2284 LOG_ERROR("Unsupported EEPROM size");
2285 return ERROR_FLASH_OPERATION_FAILED
;
2291 else if (ee1
* 7 == ee2
)
2293 else if (ee1
!= ee2
) {
2294 LOG_ERROR("Unsupported EEPROM sizes ratio");
2295 return ERROR_FLASH_OPERATION_FAILED
;
2298 ee_size_code
= log2
| ee_split
<< 4;
2302 COMMAND_PARSE_ON_OFF(CMD_ARGV
[4], enable
);
2306 LOG_INFO("DEPART 0x%" PRIx8
", EEPROM size code 0x%" PRIx8
,
2307 flex_nvm_partition_code
, ee_size_code
);
2309 result
= kinetis_check_run_mode(target
);
2310 if (result
!= ERROR_OK
)
2313 /* reset error flags */
2314 result
= kinetis_ftfx_prepare(target
);
2315 if (result
!= ERROR_OK
)
2318 result
= kinetis_ftfx_command(target
, FTFx_CMD_PGMPART
, load_flex_ram
,
2319 ee_size_code
, flex_nvm_partition_code
, 0, 0,
2321 if (result
!= ERROR_OK
)
2324 command_print(CMD_CTX
, "FlexNVM partition set. Please reset MCU.");
2326 for (i
= 1; i
< 4; i
++) {
2327 bank
= get_flash_bank_by_num_noprobe(i
);
2331 kinfo
= bank
->driver_priv
;
2332 if (kinfo
&& kinfo
->flash_class
== FC_FLEX_NVM
)
2333 kinfo
->probed
= false; /* re-probe before next use */
2336 command_print(CMD_CTX
, "FlexNVM banks will be re-probed to set new data flash size.");
2340 COMMAND_HANDLER(kinetis_fcf_source_handler
)
2343 return ERROR_COMMAND_SYNTAX_ERROR
;
2345 if (CMD_ARGC
== 1) {
2346 if (strcmp(CMD_ARGV
[0], "write") == 0)
2347 allow_fcf_writes
= true;
2348 else if (strcmp(CMD_ARGV
[0], "protection") == 0)
2349 allow_fcf_writes
= false;
2351 return ERROR_COMMAND_SYNTAX_ERROR
;
2354 if (allow_fcf_writes
) {
2355 command_print(CMD_CTX
, "Arbitrary Flash Configuration Field writes enabled.");
2356 command_print(CMD_CTX
, "Protection info writes to FCF disabled.");
2357 LOG_WARNING("BEWARE: incorrect flash configuration may permanently lock the device.");
2359 command_print(CMD_CTX
, "Protection info writes to Flash Configuration Field enabled.");
2360 command_print(CMD_CTX
, "Arbitrary FCF writes disabled. Mode safe from unwanted locking of the device.");
2366 COMMAND_HANDLER(kinetis_fopt_handler
)
2369 return ERROR_COMMAND_SYNTAX_ERROR
;
2372 fcf_fopt
= (uint8_t)strtoul(CMD_ARGV
[0], NULL
, 0);
2374 command_print(CMD_CTX
, "FCF_FOPT 0x%02" PRIx8
, fcf_fopt
);
2380 static const struct command_registration kinetis_security_command_handlers
[] = {
2382 .name
= "check_security",
2383 .mode
= COMMAND_EXEC
,
2384 .help
= "Check status of device security lock",
2386 .handler
= kinetis_check_flash_security_status
,
2390 .mode
= COMMAND_EXEC
,
2391 .help
= "Issue a halt via the MDM-AP",
2393 .handler
= kinetis_mdm_halt
,
2396 .name
= "mass_erase",
2397 .mode
= COMMAND_EXEC
,
2398 .help
= "Issue a complete flash erase via the MDM-AP",
2400 .handler
= kinetis_mdm_mass_erase
,
2403 .mode
= COMMAND_EXEC
,
2404 .help
= "Issue a reset via the MDM-AP",
2406 .handler
= kinetis_mdm_reset
,
2408 COMMAND_REGISTRATION_DONE
2411 static const struct command_registration kinetis_exec_command_handlers
[] = {
2414 .mode
= COMMAND_ANY
,
2415 .help
= "MDM-AP command group",
2417 .chain
= kinetis_security_command_handlers
,
2420 .name
= "disable_wdog",
2421 .mode
= COMMAND_EXEC
,
2422 .help
= "Disable the watchdog timer",
2424 .handler
= kinetis_disable_wdog_handler
,
2427 .name
= "nvm_partition",
2428 .mode
= COMMAND_EXEC
,
2429 .help
= "Show/set data flash or EEPROM backup size in kilobytes,"
2430 " set two EEPROM sizes in bytes and FlexRAM loading during reset",
2431 .usage
= "('info'|'dataflash' size|'eebkp' size) [eesize1 eesize2] ['on'|'off']",
2432 .handler
= kinetis_nvm_partition
,
2435 .name
= "fcf_source",
2436 .mode
= COMMAND_EXEC
,
2437 .help
= "Use protection as a source for Flash Configuration Field or allow writing arbitrary values to the FCF"
2438 " Mode 'protection' is safe from unwanted locking of the device.",
2439 .usage
= "['protection'|'write']",
2440 .handler
= kinetis_fcf_source_handler
,
2444 .mode
= COMMAND_EXEC
,
2445 .help
= "FCF_FOPT value source in 'kinetis fcf_source protection' mode",
2447 .handler
= kinetis_fopt_handler
,
2449 COMMAND_REGISTRATION_DONE
2452 static const struct command_registration kinetis_command_handler
[] = {
2455 .mode
= COMMAND_ANY
,
2456 .help
= "Kinetis flash controller commands",
2458 .chain
= kinetis_exec_command_handlers
,
2460 COMMAND_REGISTRATION_DONE
2465 struct flash_driver kinetis_flash
= {
2467 .commands
= kinetis_command_handler
,
2468 .flash_bank_command
= kinetis_flash_bank_command
,
2469 .erase
= kinetis_erase
,
2470 .protect
= kinetis_protect
,
2471 .write
= kinetis_write
,
2472 .read
= default_flash_read
,
2473 .probe
= kinetis_probe
,
2474 .auto_probe
= kinetis_auto_probe
,
2475 .erase_check
= kinetis_blank_check
,
2476 .protect_check
= kinetis_protect_check
,
2477 .info
= kinetis_info
,