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 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * (at your option) any later version. *
19 * This program is distributed in the hope that it will be useful, *
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22 * GNU General Public License for more details. *
24 * You should have received a copy of the GNU General Public License *
25 * along with this program; if not, write to the *
26 * Free Software Foundation, Inc., *
27 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
28 ***************************************************************************/
34 #include "jtag/interface.h"
36 #include <helper/binarybuffer.h>
37 #include <target/algorithm.h>
38 #include <target/armv7m.h>
39 #include <target/cortex_m.h>
42 * Implementation Notes
44 * The persistent memories in the Kinetis chip families K10 through
45 * K70 are all manipulated with the Flash Memory Module. Some
46 * variants call this module the FTFE, others call it the FTFL. To
47 * indicate that both are considered here, we use FTFX.
49 * Within the module, according to the chip variant, the persistent
50 * memory is divided into what Freescale terms Program Flash, FlexNVM,
51 * and FlexRAM. All chip variants have Program Flash. Some chip
52 * variants also have FlexNVM and FlexRAM, which always appear
55 * A given Kinetis chip may have 1, 2 or 4 blocks of flash. Here we map
56 * each block to a separate bank. Each block size varies by chip and
57 * may be determined by the read-only SIM_FCFG1 register. The sector
58 * size within each bank/block varies by chip, and may be 1, 2 or 4k.
59 * The sector size may be different for flash and FlexNVM.
61 * The first half of the flash (1 or 2 blocks) is always Program Flash
62 * and always starts at address 0x00000000. The "PFLSH" flag, bit 23
63 * of the read-only SIM_FCFG2 register, determines whether the second
64 * half of the flash is also Program Flash or FlexNVM+FlexRAM. When
65 * PFLSH is set, the second from the first half. When PFLSH is clear,
66 * the second half of flash is FlexNVM and always starts at address
67 * 0x10000000. FlexRAM, which is also present when PFLSH is clear,
68 * always starts at address 0x14000000.
70 * The Flash Memory Module provides a register set where flash
71 * commands are loaded to perform flash operations like erase and
72 * program. Different commands are available depending on whether
73 * Program Flash or FlexNVM/FlexRAM is being manipulated. Although
74 * the commands used are quite consistent between flash blocks, the
75 * parameters they accept differ according to the flash sector size.
80 #define FLEXRAM 0x14000000
81 #define FTFx_FSTAT 0x40020000
82 #define FTFx_FCNFG 0x40020001
83 #define FTFx_FCCOB3 0x40020004
84 #define FTFx_FPROT3 0x40020010
85 #define SIM_SDID 0x40048024
86 #define SIM_SOPT1 0x40047000
87 #define SIM_FCFG1 0x4004804c
88 #define SIM_FCFG2 0x40048050
91 #define FTFx_CMD_BLOCKSTAT 0x00
92 #define FTFx_CMD_SECTSTAT 0x01
93 #define FTFx_CMD_LWORDPROG 0x06
94 #define FTFx_CMD_SECTERASE 0x09
95 #define FTFx_CMD_SECTWRITE 0x0b
96 #define FTFx_CMD_SETFLEXRAM 0x81
97 #define FTFx_CMD_MASSERASE 0x44
99 /* The older Kinetis K series uses the following SDID layout :
106 * The newer Kinetis series uses the following SDID layout :
108 * Bit 27-24 : SUBFAMID
109 * Bit 23-20 : SERIESID
110 * Bit 19-16 : SRAMSIZE
112 * Bit 6-4 : Reserved (0)
115 * We assume that if bits 31-16 are 0 then it's an older
119 #define KINETIS_SOPT1_RAMSIZE_MASK 0x0000F000
120 #define KINETIS_SOPT1_RAMSIZE_K24FN1M 0x0000B000
122 #define KINETIS_SDID_K_SERIES_MASK 0x0000FFFF
124 #define KINETIS_SDID_DIEID_MASK 0x00000F80
126 #define KINETIS_SDID_DIEID_K22FN128 0x00000680 /* smaller pflash with FTFA */
127 #define KINETIS_SDID_DIEID_K22FN256 0x00000A80
128 #define KINETIS_SDID_DIEID_K22FN512 0x00000E80
129 #define KINETIS_SDID_DIEID_K24FN256 0x00000700
131 #define KINETIS_SDID_DIEID_K24FN1M 0x00000300 /* Detect Errata 7534 */
133 /* We can't rely solely on the FAMID field to determine the MCU
134 * type since some FAMID values identify multiple MCUs with
135 * different flash sector sizes (K20 and K22 for instance).
136 * Therefore we combine it with the DIEID bits which may possibly
137 * break if Freescale bumps the DIEID for a particular MCU. */
138 #define KINETIS_K_SDID_TYPE_MASK 0x00000FF0
139 #define KINETIS_K_SDID_K10_M50 0x00000000
140 #define KINETIS_K_SDID_K10_M72 0x00000080
141 #define KINETIS_K_SDID_K10_M100 0x00000100
142 #define KINETIS_K_SDID_K10_M120 0x00000180
143 #define KINETIS_K_SDID_K11 0x00000220
144 #define KINETIS_K_SDID_K12 0x00000200
145 #define KINETIS_K_SDID_K20_M50 0x00000010
146 #define KINETIS_K_SDID_K20_M72 0x00000090
147 #define KINETIS_K_SDID_K20_M100 0x00000110
148 #define KINETIS_K_SDID_K20_M120 0x00000190
149 #define KINETIS_K_SDID_K21_M50 0x00000230
150 #define KINETIS_K_SDID_K21_M120 0x00000330
151 #define KINETIS_K_SDID_K22_M50 0x00000210
152 #define KINETIS_K_SDID_K22_M120 0x00000310
153 #define KINETIS_K_SDID_K30_M72 0x000000A0
154 #define KINETIS_K_SDID_K30_M100 0x00000120
155 #define KINETIS_K_SDID_K40_M72 0x000000B0
156 #define KINETIS_K_SDID_K40_M100 0x00000130
157 #define KINETIS_K_SDID_K50_M72 0x000000E0
158 #define KINETIS_K_SDID_K51_M72 0x000000F0
159 #define KINETIS_K_SDID_K53 0x00000170
160 #define KINETIS_K_SDID_K60_M100 0x00000140
161 #define KINETIS_K_SDID_K60_M150 0x000001C0
162 #define KINETIS_K_SDID_K70_M150 0x000001D0
164 #define KINETIS_SDID_SERIESID_MASK 0x00F00000
165 #define KINETIS_SDID_SERIESID_K 0x00000000
166 #define KINETIS_SDID_SERIESID_KL 0x00100000
167 #define KINETIS_SDID_SERIESID_KW 0x00500000
168 #define KINETIS_SDID_SERIESID_KV 0x00600000
170 #define KINETIS_SDID_SUBFAMID_MASK 0x0F000000
171 #define KINETIS_SDID_SUBFAMID_KX0 0x00000000
172 #define KINETIS_SDID_SUBFAMID_KX1 0x01000000
173 #define KINETIS_SDID_SUBFAMID_KX2 0x02000000
174 #define KINETIS_SDID_SUBFAMID_KX3 0x03000000
175 #define KINETIS_SDID_SUBFAMID_KX4 0x04000000
176 #define KINETIS_SDID_SUBFAMID_KX5 0x05000000
177 #define KINETIS_SDID_SUBFAMID_KX6 0x06000000
179 #define KINETIS_SDID_FAMILYID_MASK 0xF0000000
180 #define KINETIS_SDID_FAMILYID_K0X 0x00000000
181 #define KINETIS_SDID_FAMILYID_K1X 0x10000000
182 #define KINETIS_SDID_FAMILYID_K2X 0x20000000
183 #define KINETIS_SDID_FAMILYID_K3X 0x30000000
184 #define KINETIS_SDID_FAMILYID_K4X 0x40000000
185 #define KINETIS_SDID_FAMILYID_K6X 0x60000000
186 #define KINETIS_SDID_FAMILYID_K7X 0x70000000
188 struct kinetis_flash_bank
{
189 unsigned bank_ordinal
;
190 uint32_t sector_size
;
191 uint32_t max_flash_prog_size
;
192 uint32_t protection_size
;
206 FS_PROGRAM_SECTOR
= 1,
207 FS_PROGRAM_LONGWORD
= 2,
208 FS_PROGRAM_PHRASE
= 4, /* Unsupported */
212 #define MDM_REG_STAT 0x00
213 #define MDM_REG_CTRL 0x04
214 #define MDM_REG_ID 0xfc
216 #define MDM_STAT_FMEACK (1<<0)
217 #define MDM_STAT_FREADY (1<<1)
218 #define MDM_STAT_SYSSEC (1<<2)
219 #define MDM_STAT_SYSRES (1<<3)
220 #define MDM_STAT_FMEEN (1<<5)
221 #define MDM_STAT_BACKDOOREN (1<<6)
222 #define MDM_STAT_LPEN (1<<7)
223 #define MDM_STAT_VLPEN (1<<8)
224 #define MDM_STAT_LLSMODEXIT (1<<9)
225 #define MDM_STAT_VLLSXMODEXIT (1<<10)
226 #define MDM_STAT_CORE_HALTED (1<<16)
227 #define MDM_STAT_CORE_SLEEPDEEP (1<<17)
228 #define MDM_STAT_CORESLEEPING (1<<18)
230 #define MEM_CTRL_FMEIP (1<<0)
231 #define MEM_CTRL_DBG_DIS (1<<1)
232 #define MEM_CTRL_DBG_REQ (1<<2)
233 #define MEM_CTRL_SYS_RES_REQ (1<<3)
234 #define MEM_CTRL_CORE_HOLD_RES (1<<4)
235 #define MEM_CTRL_VLLSX_DBG_REQ (1<<5)
236 #define MEM_CTRL_VLLSX_DBG_ACK (1<<6)
237 #define MEM_CTRL_VLLSX_STAT_ACK (1<<7)
239 #define MDM_ACCESS_TIMEOUT 3000 /* iterations */
241 static int kinetis_mdm_write_register(struct adiv5_dap
*dap
, unsigned reg
, uint32_t value
)
244 LOG_DEBUG("MDM_REG[0x%02x] <- %08" PRIX32
, reg
, value
);
246 retval
= dap_queue_ap_write(dap
, reg
, value
);
247 if (retval
!= ERROR_OK
) {
248 LOG_DEBUG("MDM: failed to queue a write request");
252 retval
= dap_run(dap
);
253 if (retval
!= ERROR_OK
) {
254 LOG_DEBUG("MDM: dap_run failed");
262 static int kinetis_mdm_read_register(struct adiv5_dap
*dap
, unsigned reg
, uint32_t *result
)
265 retval
= dap_queue_ap_read(dap
, reg
, result
);
266 if (retval
!= ERROR_OK
) {
267 LOG_DEBUG("MDM: failed to queue a read request");
271 retval
= dap_run(dap
);
272 if (retval
!= ERROR_OK
) {
273 LOG_DEBUG("MDM: dap_run failed");
277 LOG_DEBUG("MDM_REG[0x%02x]: %08" PRIX32
, reg
, *result
);
281 static int kinetis_mdm_poll_register(struct adiv5_dap
*dap
, unsigned reg
, uint32_t mask
, uint32_t value
)
285 int timeout
= MDM_ACCESS_TIMEOUT
;
288 retval
= kinetis_mdm_read_register(dap
, reg
, &val
);
289 if (retval
!= ERROR_OK
|| (val
& mask
) == value
)
295 LOG_DEBUG("MDM: polling timed out");
300 * This function implements the procedure to mass erase the flash via
301 * SWD/JTAG on Kinetis K and L series of devices as it is described in
302 * AN4835 "Production Flash Programming Best Practices for Kinetis K-
303 * and L-series MCUs" Section 4.2.1
305 COMMAND_HANDLER(kinetis_mdm_mass_erase
)
307 struct target
*target
= get_current_target(CMD_CTX
);
308 struct cortex_m_common
*cortex_m
= target_to_cm(target
);
309 struct adiv5_dap
*dap
= cortex_m
->armv7m
.arm
.dap
;
312 LOG_ERROR("Cannot perform mass erase with a high-level adapter");
317 const uint8_t original_ap
= dap
->ap_current
;
320 * ... Power on the processor, or if power has already been
321 * applied, assert the RESET pin to reset the processor. For
322 * devices that do not have a RESET pin, write the System
323 * Reset Request bit in the MDM-AP control register after
324 * establishing communication...
328 if (jtag_get_reset_config() & RESET_HAS_SRST
)
329 adapter_assert_reset();
331 LOG_WARNING("Attempting mass erase without hardware reset. This is not reliable; "
332 "it's recommended you connect SRST and use ``reset_config srst_only''.");
334 dap_ap_select(dap
, 1);
336 retval
= kinetis_mdm_write_register(dap
, MDM_REG_CTRL
, MEM_CTRL_SYS_RES_REQ
);
337 if (retval
!= ERROR_OK
)
341 * ... Read the MDM-AP status register until the Flash Ready bit sets...
343 retval
= kinetis_mdm_poll_register(dap
, MDM_REG_STAT
,
344 MDM_STAT_FREADY
| MDM_STAT_SYSRES
,
346 if (retval
!= ERROR_OK
) {
347 LOG_ERROR("MDM : flash ready timeout");
352 * ... Write the MDM-AP control register to set the Flash Mass
353 * Erase in Progress bit. This will start the mass erase
356 retval
= kinetis_mdm_write_register(dap
, MDM_REG_CTRL
,
357 MEM_CTRL_SYS_RES_REQ
| MEM_CTRL_FMEIP
);
358 if (retval
!= ERROR_OK
)
361 /* As a sanity check make sure that device started mass erase procedure */
362 retval
= kinetis_mdm_poll_register(dap
, MDM_REG_STAT
,
363 MDM_STAT_FMEACK
, MDM_STAT_FMEACK
);
364 if (retval
!= ERROR_OK
)
368 * ... Read the MDM-AP control register until the Flash Mass
369 * Erase in Progress bit clears...
371 retval
= kinetis_mdm_poll_register(dap
, MDM_REG_CTRL
,
374 if (retval
!= ERROR_OK
)
378 * ... Negate the RESET signal or clear the System Reset Request
379 * bit in the MDM-AP control register...
381 retval
= kinetis_mdm_write_register(dap
, MDM_REG_CTRL
, 0);
382 if (retval
!= ERROR_OK
)
385 if (jtag_get_reset_config() & RESET_HAS_SRST
)
386 adapter_deassert_reset();
388 dap_ap_select(dap
, original_ap
);
392 static const uint32_t kinetis_known_mdm_ids
[] = {
393 0x001C0000, /* Kinetis-K Series */
394 0x001C0020, /* Kinetis-L/M/V/E Series */
398 * This function implements the procedure to connect to
399 * SWD/JTAG on Kinetis K and L series of devices as it is described in
400 * AN4835 "Production Flash Programming Best Practices for Kinetis K-
401 * and L-series MCUs" Section 4.1.1
403 COMMAND_HANDLER(kinetis_check_flash_security_status
)
405 struct target
*target
= get_current_target(CMD_CTX
);
406 struct cortex_m_common
*cortex_m
= target_to_cm(target
);
407 struct adiv5_dap
*dap
= cortex_m
->armv7m
.arm
.dap
;
410 LOG_WARNING("Cannot check flash security status with a high-level adapter");
416 const uint8_t origninal_ap
= dap
->ap_current
;
418 dap_ap_select(dap
, 1);
422 * ... The MDM-AP ID register can be read to verify that the
423 * connection is working correctly...
425 retval
= kinetis_mdm_read_register(dap
, MDM_REG_ID
, &val
);
426 if (retval
!= ERROR_OK
) {
427 LOG_ERROR("MDM: failed to read ID register");
432 for (size_t i
= 0; i
< ARRAY_SIZE(kinetis_known_mdm_ids
); i
++) {
433 if (val
== kinetis_known_mdm_ids
[i
]) {
440 LOG_WARNING("MDM: unknown ID %08" PRIX32
, val
);
443 * ... Read the MDM-AP status register until the Flash Ready bit sets...
445 retval
= kinetis_mdm_poll_register(dap
, MDM_REG_STAT
,
448 if (retval
!= ERROR_OK
) {
449 LOG_ERROR("MDM: flash ready timeout");
454 * ... Read the System Security bit to determine if security is enabled.
455 * If System Security = 0, then proceed. If System Security = 1, then
456 * communication with the internals of the processor, including the
457 * flash, will not be possible without issuing a mass erase command or
458 * unsecuring the part through other means (backdoor key unlock)...
460 retval
= kinetis_mdm_read_register(dap
, MDM_REG_STAT
, &val
);
461 if (retval
!= ERROR_OK
) {
462 LOG_ERROR("MDM: failed to read MDM_REG_STAT");
466 if (val
& MDM_STAT_SYSSEC
) {
467 jtag_poll_set_enabled(false);
469 LOG_WARNING("*********** ATTENTION! ATTENTION! ATTENTION! ATTENTION! **********");
470 LOG_WARNING("**** ****");
471 LOG_WARNING("**** Your Kinetis MCU is in secured state, which means that, ****");
472 LOG_WARNING("**** with exception for very basic communication, JTAG/SWD ****");
473 LOG_WARNING("**** interface will NOT work. In order to restore its ****");
474 LOG_WARNING("**** functionality please issue 'kinetis mdm mass_erase' ****");
475 LOG_WARNING("**** command, power cycle the MCU and restart OpenOCD. ****");
476 LOG_WARNING("**** ****");
477 LOG_WARNING("*********** ATTENTION! ATTENTION! ATTENTION! ATTENTION! **********");
479 LOG_INFO("MDM: Chip is unsecured. Continuing.");
480 jtag_poll_set_enabled(true);
483 dap_ap_select(dap
, origninal_ap
);
488 LOG_ERROR("MDM: Failed to check security status of the MCU. Cannot proceed further");
489 jtag_poll_set_enabled(false);
493 FLASH_BANK_COMMAND_HANDLER(kinetis_flash_bank_command
)
495 struct kinetis_flash_bank
*bank_info
;
498 return ERROR_COMMAND_SYNTAX_ERROR
;
500 LOG_INFO("add flash_bank kinetis %s", bank
->name
);
502 bank_info
= malloc(sizeof(struct kinetis_flash_bank
));
504 memset(bank_info
, 0, sizeof(struct kinetis_flash_bank
));
506 bank
->driver_priv
= bank_info
;
511 /* Kinetis Program-LongWord Microcodes */
512 static const uint8_t kinetis_flash_write_code
[] = {
514 * r0 - workarea buffer
515 * r1 - target address
525 /* for(register uint32_t i=0;i<wcount;i++){ */
526 0x04, 0x1C, /* mov r4, r0 */
527 0x00, 0x23, /* mov r3, #0 */
529 0x0E, 0x1A, /* sub r6, r1, r0 */
530 0xA6, 0x19, /* add r6, r4, r6 */
531 0x93, 0x42, /* cmp r3, r2 */
532 0x16, 0xD0, /* beq .L9 */
534 /* while((FTFx_FSTAT&FTFA_FSTAT_CCIF_MASK) != FTFA_FSTAT_CCIF_MASK){}; */
535 0x0B, 0x4D, /* ldr r5, .L10 */
536 0x2F, 0x78, /* ldrb r7, [r5] */
537 0x7F, 0xB2, /* sxtb r7, r7 */
538 0x00, 0x2F, /* cmp r7, #0 */
539 0xFA, 0xDA, /* bge .L5 */
540 /* FTFx_FSTAT = FTFA_FSTAT_ACCERR_MASK|FTFA_FSTAT_FPVIOL_MASK|FTFA_FSTAT_RDCO */
541 0x70, 0x27, /* mov r7, #112 */
542 0x2F, 0x70, /* strb r7, [r5] */
543 /* FTFx_FCCOB3 = faddr; */
544 0x09, 0x4F, /* ldr r7, .L10+4 */
545 0x3E, 0x60, /* str r6, [r7] */
546 0x06, 0x27, /* mov r7, #6 */
547 /* FTFx_FCCOB0 = 0x06; */
548 0x08, 0x4E, /* ldr r6, .L10+8 */
549 0x37, 0x70, /* strb r7, [r6] */
550 /* FTFx_FCCOB7 = *pLW; */
551 0x80, 0xCC, /* ldmia r4!, {r7} */
552 0x08, 0x4E, /* ldr r6, .L10+12 */
553 0x37, 0x60, /* str r7, [r6] */
554 /* FTFx_FSTAT = FTFA_FSTAT_CCIF_MASK; */
555 0x80, 0x27, /* mov r7, #128 */
556 0x2F, 0x70, /* strb r7, [r5] */
558 /* while((FTFx_FSTAT&FTFA_FSTAT_CCIF_MASK) != FTFA_FSTAT_CCIF_MASK){}; */
559 0x2E, 0x78, /* ldrb r6, [r5] */
560 0x77, 0xB2, /* sxtb r7, r6 */
561 0x00, 0x2F, /* cmp r7, #0 */
562 0xFB, 0xDA, /* bge .L4 */
563 0x01, 0x33, /* add r3, r3, #1 */
564 0xE4, 0xE7, /* b .L2 */
566 0x00, 0xBE, /* bkpt #0 */
568 0x00, 0x00, 0x02, 0x40, /* .word 1073872896 */
569 0x04, 0x00, 0x02, 0x40, /* .word 1073872900 */
570 0x07, 0x00, 0x02, 0x40, /* .word 1073872903 */
571 0x08, 0x00, 0x02, 0x40, /* .word 1073872904 */
574 /* Program LongWord Block Write */
575 static int kinetis_write_block(struct flash_bank
*bank
, const uint8_t *buffer
,
576 uint32_t offset
, uint32_t wcount
)
578 struct target
*target
= bank
->target
;
579 uint32_t buffer_size
= 2048; /* Default minimum value */
580 struct working_area
*write_algorithm
;
581 struct working_area
*source
;
582 uint32_t address
= bank
->base
+ offset
;
583 struct reg_param reg_params
[3];
584 struct armv7m_algorithm armv7m_info
;
585 int retval
= ERROR_OK
;
588 * r0 - workarea buffer
589 * r1 - target address
598 /* Increase buffer_size if needed */
599 if (buffer_size
< (target
->working_area_size
/2))
600 buffer_size
= (target
->working_area_size
/2);
602 LOG_INFO("Kinetis: FLASH Write ...");
604 /* check code alignment */
606 LOG_WARNING("offset 0x%" PRIx32
" breaks required 2-byte alignment", offset
);
607 return ERROR_FLASH_DST_BREAKS_ALIGNMENT
;
610 /* allocate working area with flash programming code */
611 if (target_alloc_working_area(target
, sizeof(kinetis_flash_write_code
),
612 &write_algorithm
) != ERROR_OK
) {
613 LOG_WARNING("no working area available, can't do block memory writes");
614 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
617 retval
= target_write_buffer(target
, write_algorithm
->address
,
618 sizeof(kinetis_flash_write_code
), kinetis_flash_write_code
);
619 if (retval
!= ERROR_OK
)
623 while (target_alloc_working_area(target
, buffer_size
, &source
) != ERROR_OK
) {
625 if (buffer_size
<= 256) {
626 /* free working area, write algorithm already allocated */
627 target_free_working_area(target
, write_algorithm
);
629 LOG_WARNING("No large enough working area available, can't do block memory writes");
630 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
634 armv7m_info
.common_magic
= ARMV7M_COMMON_MAGIC
;
635 armv7m_info
.core_mode
= ARM_MODE_THREAD
;
637 init_reg_param(®_params
[0], "r0", 32, PARAM_OUT
); /* *pLW (*buffer) */
638 init_reg_param(®_params
[1], "r1", 32, PARAM_OUT
); /* faddr */
639 init_reg_param(®_params
[2], "r2", 32, PARAM_OUT
); /* number of words to program */
641 /* write code buffer and use Flash programming code within kinetis */
642 /* Set breakpoint to 0 with time-out of 1000 ms */
644 uint32_t thisrun_count
= (wcount
> (buffer_size
/ 4)) ? (buffer_size
/ 4) : wcount
;
646 retval
= target_write_buffer(target
, source
->address
, thisrun_count
* 4, buffer
);
647 if (retval
!= ERROR_OK
)
650 buf_set_u32(reg_params
[0].value
, 0, 32, source
->address
);
651 buf_set_u32(reg_params
[1].value
, 0, 32, address
);
652 buf_set_u32(reg_params
[2].value
, 0, 32, thisrun_count
);
654 retval
= target_run_algorithm(target
, 0, NULL
, 3, reg_params
,
655 write_algorithm
->address
, 0, 100000, &armv7m_info
);
656 if (retval
!= ERROR_OK
) {
657 LOG_ERROR("Error executing kinetis Flash programming algorithm");
658 retval
= ERROR_FLASH_OPERATION_FAILED
;
662 buffer
+= thisrun_count
* 4;
663 address
+= thisrun_count
* 4;
664 wcount
-= thisrun_count
;
667 target_free_working_area(target
, source
);
668 target_free_working_area(target
, write_algorithm
);
670 destroy_reg_param(®_params
[0]);
671 destroy_reg_param(®_params
[1]);
672 destroy_reg_param(®_params
[2]);
677 static int kinetis_protect(struct flash_bank
*bank
, int set
, int first
, int last
)
679 LOG_WARNING("kinetis_protect not supported yet");
682 if (bank
->target
->state
!= TARGET_HALTED
) {
683 LOG_ERROR("Target not halted");
684 return ERROR_TARGET_NOT_HALTED
;
687 return ERROR_FLASH_BANK_INVALID
;
690 static int kinetis_protect_check(struct flash_bank
*bank
)
692 struct kinetis_flash_bank
*kinfo
= bank
->driver_priv
;
694 if (bank
->target
->state
!= TARGET_HALTED
) {
695 LOG_ERROR("Target not halted");
696 return ERROR_TARGET_NOT_HALTED
;
699 if (kinfo
->flash_class
== FC_PFLASH
) {
702 uint32_t fprot
, psec
;
705 /* read protection register */
706 result
= target_read_memory(bank
->target
, FTFx_FPROT3
, 1, 4, buffer
);
708 if (result
!= ERROR_OK
)
711 fprot
= target_buffer_get_u32(bank
->target
, buffer
);
714 * Every bit protects 1/32 of the full flash (not necessarily
715 * just this bank), but we enforce the bank ordinals for
716 * PFlash to start at zero.
718 b
= kinfo
->bank_ordinal
* (bank
->size
/ kinfo
->protection_size
);
719 for (psec
= 0, i
= 0; i
< bank
->num_sectors
; i
++) {
720 if ((fprot
>> b
) & 1)
721 bank
->sectors
[i
].is_protected
= 0;
723 bank
->sectors
[i
].is_protected
= 1;
725 psec
+= bank
->sectors
[i
].size
;
727 if (psec
>= kinfo
->protection_size
) {
733 LOG_ERROR("Protection checks for FlexNVM not yet supported");
734 return ERROR_FLASH_BANK_INVALID
;
740 static int kinetis_ftfx_command(struct flash_bank
*bank
, uint8_t fcmd
, uint32_t faddr
,
741 uint8_t fccob4
, uint8_t fccob5
, uint8_t fccob6
, uint8_t fccob7
,
742 uint8_t fccob8
, uint8_t fccob9
, uint8_t fccoba
, uint8_t fccobb
,
745 uint8_t command
[12] = {faddr
& 0xff, (faddr
>> 8) & 0xff, (faddr
>> 16) & 0xff, fcmd
,
746 fccob7
, fccob6
, fccob5
, fccob4
,
747 fccobb
, fccoba
, fccob9
, fccob8
};
752 for (i
= 0; i
< 50; i
++) {
754 target_read_memory(bank
->target
, FTFx_FSTAT
, 1, 1, &buffer
);
756 if (result
!= ERROR_OK
)
765 if (buffer
!= 0x80) {
766 /* reset error flags */
769 target_write_memory(bank
->target
, FTFx_FSTAT
, 1, 1, &buffer
);
770 if (result
!= ERROR_OK
)
774 result
= target_write_memory(bank
->target
, FTFx_FCCOB3
, 4, 3, command
);
776 if (result
!= ERROR_OK
)
781 result
= target_write_memory(bank
->target
, FTFx_FSTAT
, 1, 1, &buffer
);
782 if (result
!= ERROR_OK
)
786 for (i
= 0; i
< 240; i
++) { /* Need longtime for "Mass Erase" Command Nemui Changed */
788 target_read_memory(bank
->target
, FTFx_FSTAT
, 1, 1, ftfx_fstat
);
790 if (result
!= ERROR_OK
)
793 if (*ftfx_fstat
& 0x80)
797 if ((*ftfx_fstat
& 0xf0) != 0x80) {
799 ("ftfx command failed FSTAT: %02X FCCOB: %02X%02X%02X%02X %02X%02X%02X%02X %02X%02X%02X%02X",
800 *ftfx_fstat
, command
[3], command
[2], command
[1], command
[0],
801 command
[7], command
[6], command
[5], command
[4],
802 command
[11], command
[10], command
[9], command
[8]);
803 return ERROR_FLASH_OPERATION_FAILED
;
809 COMMAND_HANDLER(kinetis_securing_test
)
813 struct target
*target
= get_current_target(CMD_CTX
);
814 struct flash_bank
*bank
= NULL
;
816 result
= get_flash_bank_by_addr(target
, 0x00000000, true, &bank
);
817 if (result
!= ERROR_OK
)
820 assert(bank
!= NULL
);
822 if (target
->state
!= TARGET_HALTED
) {
823 LOG_ERROR("Target not halted");
824 return ERROR_TARGET_NOT_HALTED
;
827 return kinetis_ftfx_command(bank
, FTFx_CMD_SECTERASE
, bank
->base
+ 0x00000400,
828 0, 0, 0, 0, 0, 0, 0, 0, &ftfx_fstat
);
831 static int kinetis_erase(struct flash_bank
*bank
, int first
, int last
)
835 if (bank
->target
->state
!= TARGET_HALTED
) {
836 LOG_ERROR("Target not halted");
837 return ERROR_TARGET_NOT_HALTED
;
840 if ((first
> bank
->num_sectors
) || (last
> bank
->num_sectors
))
841 return ERROR_FLASH_OPERATION_FAILED
;
844 * FIXME: TODO: use the 'Erase Flash Block' command if the
845 * requested erase is PFlash or NVM and encompasses the entire
846 * block. Should be quicker.
848 for (i
= first
; i
<= last
; i
++) {
850 /* set command and sector address */
851 result
= kinetis_ftfx_command(bank
, FTFx_CMD_SECTERASE
, bank
->base
+ bank
->sectors
[i
].offset
,
852 0, 0, 0, 0, 0, 0, 0, 0, &ftfx_fstat
);
854 if (result
!= ERROR_OK
) {
855 LOG_WARNING("erase sector %d failed", i
);
856 return ERROR_FLASH_OPERATION_FAILED
;
859 bank
->sectors
[i
].is_erased
= 1;
864 ("flash configuration field erased, please reset the device");
870 static int kinetis_write(struct flash_bank
*bank
, const uint8_t *buffer
,
871 uint32_t offset
, uint32_t count
)
873 unsigned int i
, result
, fallback
= 0;
876 struct kinetis_flash_bank
*kinfo
= bank
->driver_priv
;
877 uint8_t *new_buffer
= NULL
;
879 if (bank
->target
->state
!= TARGET_HALTED
) {
880 LOG_ERROR("Target not halted");
881 return ERROR_TARGET_NOT_HALTED
;
884 if (!(kinfo
->flash_support
& FS_PROGRAM_SECTOR
)) {
885 /* fallback to longword write */
887 LOG_WARNING("This device supports Program Longword execution only.");
888 LOG_DEBUG("flash write into PFLASH @08%" PRIX32
, offset
);
890 } else if (kinfo
->flash_class
== FC_FLEX_NVM
) {
893 LOG_DEBUG("flash write into FlexNVM @%08" PRIX32
, offset
);
895 /* make flex ram available */
896 result
= kinetis_ftfx_command(bank
, FTFx_CMD_SETFLEXRAM
, 0x00ff0000, 0, 0, 0, 0, 0, 0, 0, 0, &ftfx_fstat
);
898 if (result
!= ERROR_OK
)
899 return ERROR_FLASH_OPERATION_FAILED
;
901 /* check if ram ready */
902 result
= target_read_memory(bank
->target
, FTFx_FCNFG
, 1, 1, buf
);
904 if (result
!= ERROR_OK
)
907 if (!(buf
[0] & (1 << 1))) {
908 /* fallback to longword write */
911 LOG_WARNING("ram not ready, fallback to slow longword write (FCNFG: %02X)", buf
[0]);
914 LOG_DEBUG("flash write into PFLASH @08%" PRIX32
, offset
);
918 /* program section command */
921 * Kinetis uses different terms for the granularity of
922 * sector writes, e.g. "phrase" or "128 bits". We use
923 * the generic term "chunk". The largest possible
924 * Kinetis "chunk" is 16 bytes (128 bits).
926 unsigned prog_section_chunk_bytes
= kinfo
->sector_size
>> 8;
927 unsigned prog_size_bytes
= kinfo
->max_flash_prog_size
;
928 for (i
= 0; i
< count
; i
+= prog_size_bytes
) {
929 uint8_t residual_buffer
[16];
931 uint32_t section_count
= prog_size_bytes
/ prog_section_chunk_bytes
;
932 uint32_t residual_wc
= 0;
935 * Assume the word count covers an entire
938 wc
= prog_size_bytes
/ 4;
941 * If bytes to be programmed are less than the
942 * full sector, then determine the number of
943 * full-words to program, and put together the
944 * residual buffer so that a full "section"
945 * may always be programmed.
947 if ((count
- i
) < prog_size_bytes
) {
948 /* number of bytes to program beyond full section */
949 unsigned residual_bc
= (count
-i
) % prog_section_chunk_bytes
;
951 /* number of complete words to copy directly from buffer */
952 wc
= (count
- i
) / 4;
954 /* number of total sections to write, including residual */
955 section_count
= DIV_ROUND_UP((count
-i
), prog_section_chunk_bytes
);
957 /* any residual bytes delivers a whole residual section */
958 residual_wc
= (residual_bc
? prog_section_chunk_bytes
: 0)/4;
960 /* clear residual buffer then populate residual bytes */
961 (void) memset(residual_buffer
, 0xff, prog_section_chunk_bytes
);
962 (void) memcpy(residual_buffer
, &buffer
[i
+4*wc
], residual_bc
);
965 LOG_DEBUG("write section @ %08" PRIX32
" with length %" PRIu32
" bytes",
966 offset
+ i
, (uint32_t)wc
*4);
968 /* write data to flexram as whole-words */
969 result
= target_write_memory(bank
->target
, FLEXRAM
, 4, wc
,
972 if (result
!= ERROR_OK
) {
973 LOG_ERROR("target_write_memory failed");
977 /* write the residual words to the flexram */
979 result
= target_write_memory(bank
->target
,
984 if (result
!= ERROR_OK
) {
985 LOG_ERROR("target_write_memory failed");
990 /* execute section-write command */
991 result
= kinetis_ftfx_command(bank
, FTFx_CMD_SECTWRITE
, bank
->base
+ offset
+ i
,
992 section_count
>>8, section_count
, 0, 0,
993 0, 0, 0, 0, &ftfx_fstat
);
995 if (result
!= ERROR_OK
)
996 return ERROR_FLASH_OPERATION_FAILED
;
999 /* program longword command, not supported in "SF3" devices */
1000 else if (kinfo
->flash_support
& FS_PROGRAM_LONGWORD
) {
1002 uint32_t old_count
= count
;
1003 count
= (old_count
| 3) + 1;
1004 new_buffer
= malloc(count
);
1005 if (new_buffer
== NULL
) {
1006 LOG_ERROR("odd number of bytes to write and no memory "
1007 "for padding buffer");
1010 LOG_INFO("odd number of bytes to write (%" PRIu32
"), extending to %" PRIu32
" "
1011 "and padding with 0xff", old_count
, count
);
1012 memset(new_buffer
, 0xff, count
);
1013 buffer
= memcpy(new_buffer
, buffer
, old_count
);
1016 uint32_t words_remaining
= count
/ 4;
1018 /* try using a block write */
1019 int retval
= kinetis_write_block(bank
, buffer
, offset
, words_remaining
);
1021 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
) {
1022 /* if block write failed (no sufficient working area),
1023 * we use normal (slow) single word accesses */
1024 LOG_WARNING("couldn't use block writes, falling back to single "
1027 for (i
= 0; i
< count
; i
+= 4) {
1030 LOG_DEBUG("write longword @ %08" PRIX32
, (uint32_t)(offset
+ i
));
1032 uint8_t padding
[4] = {0xff, 0xff, 0xff, 0xff};
1033 memcpy(padding
, buffer
+ i
, MIN(4, count
-i
));
1035 result
= kinetis_ftfx_command(bank
, FTFx_CMD_LWORDPROG
, bank
->base
+ offset
+ i
,
1036 padding
[3], padding
[2], padding
[1], padding
[0],
1037 0, 0, 0, 0, &ftfx_fstat
);
1039 if (result
!= ERROR_OK
)
1040 return ERROR_FLASH_OPERATION_FAILED
;
1044 LOG_ERROR("Flash write strategy not implemented");
1045 return ERROR_FLASH_OPERATION_FAILED
;
1051 static int kinetis_read_part_info(struct flash_bank
*bank
)
1054 uint32_t offset
= 0;
1055 uint8_t fcfg1_nvmsize
, fcfg1_pfsize
, fcfg1_eesize
, fcfg2_pflsh
;
1056 uint32_t nvm_size
= 0, pf_size
= 0, ee_size
= 0;
1057 unsigned num_blocks
= 0, num_pflash_blocks
= 0, num_nvm_blocks
= 0, first_nvm_bank
= 0,
1058 reassign
= 0, pflash_sector_size_bytes
= 0, nvm_sector_size_bytes
= 0;
1059 struct target
*target
= bank
->target
;
1060 struct kinetis_flash_bank
*kinfo
= bank
->driver_priv
;
1062 result
= target_read_u32(target
, SIM_SDID
, &kinfo
->sim_sdid
);
1063 if (result
!= ERROR_OK
)
1066 if ((kinfo
->sim_sdid
& (~KINETIS_SDID_K_SERIES_MASK
)) == 0) {
1067 /* older K-series MCU */
1068 uint32_t mcu_type
= kinfo
->sim_sdid
& KINETIS_K_SDID_TYPE_MASK
;
1071 case KINETIS_K_SDID_K10_M50
:
1072 case KINETIS_K_SDID_K20_M50
:
1074 pflash_sector_size_bytes
= 1<<10;
1075 nvm_sector_size_bytes
= 1<<10;
1077 kinfo
->flash_support
= FS_PROGRAM_LONGWORD
| FS_PROGRAM_SECTOR
;
1079 case KINETIS_K_SDID_K10_M72
:
1080 case KINETIS_K_SDID_K20_M72
:
1081 case KINETIS_K_SDID_K30_M72
:
1082 case KINETIS_K_SDID_K30_M100
:
1083 case KINETIS_K_SDID_K40_M72
:
1084 case KINETIS_K_SDID_K40_M100
:
1085 case KINETIS_K_SDID_K50_M72
:
1086 /* 2kB sectors, 1kB FlexNVM sectors */
1087 pflash_sector_size_bytes
= 2<<10;
1088 nvm_sector_size_bytes
= 1<<10;
1090 kinfo
->flash_support
= FS_PROGRAM_LONGWORD
| FS_PROGRAM_SECTOR
;
1091 kinfo
->max_flash_prog_size
= 1<<10;
1093 case KINETIS_K_SDID_K10_M100
:
1094 case KINETIS_K_SDID_K20_M100
:
1095 case KINETIS_K_SDID_K11
:
1096 case KINETIS_K_SDID_K12
:
1097 case KINETIS_K_SDID_K21_M50
:
1098 case KINETIS_K_SDID_K22_M50
:
1099 case KINETIS_K_SDID_K51_M72
:
1100 case KINETIS_K_SDID_K53
:
1101 case KINETIS_K_SDID_K60_M100
:
1103 pflash_sector_size_bytes
= 2<<10;
1104 nvm_sector_size_bytes
= 2<<10;
1106 kinfo
->flash_support
= FS_PROGRAM_LONGWORD
| FS_PROGRAM_SECTOR
;
1108 case KINETIS_K_SDID_K21_M120
:
1109 case KINETIS_K_SDID_K22_M120
:
1110 /* 4kB sectors (MK21FN1M0, MK21FX512, MK22FN1M0, MK22FX512) */
1111 pflash_sector_size_bytes
= 4<<10;
1112 kinfo
->max_flash_prog_size
= 1<<10;
1113 nvm_sector_size_bytes
= 4<<10;
1115 kinfo
->flash_support
= FS_PROGRAM_PHRASE
| FS_PROGRAM_SECTOR
;
1117 case KINETIS_K_SDID_K10_M120
:
1118 case KINETIS_K_SDID_K20_M120
:
1119 case KINETIS_K_SDID_K60_M150
:
1120 case KINETIS_K_SDID_K70_M150
:
1122 pflash_sector_size_bytes
= 4<<10;
1123 nvm_sector_size_bytes
= 4<<10;
1125 kinfo
->flash_support
= FS_PROGRAM_PHRASE
| FS_PROGRAM_SECTOR
;
1128 LOG_ERROR("Unsupported K-family FAMID");
1131 /* Newer K-series or KL series MCU */
1132 switch (kinfo
->sim_sdid
& KINETIS_SDID_SERIESID_MASK
) {
1133 case KINETIS_SDID_SERIESID_K
:
1134 switch (kinfo
->sim_sdid
& (KINETIS_SDID_FAMILYID_MASK
| KINETIS_SDID_SUBFAMID_MASK
)) {
1135 case KINETIS_SDID_FAMILYID_K0X
| KINETIS_SDID_SUBFAMID_KX2
:
1136 /* K02FN64, K02FN128: FTFA, 2kB sectors */
1137 pflash_sector_size_bytes
= 2<<10;
1139 kinfo
->flash_support
= FS_PROGRAM_LONGWORD
;
1142 case KINETIS_SDID_FAMILYID_K2X
| KINETIS_SDID_SUBFAMID_KX2
: {
1143 /* MK24FN1M reports as K22, this should detect it (according to errata note 1N83J) */
1145 result
= target_read_u32(target
, SIM_SOPT1
, &sopt1
);
1146 if (result
!= ERROR_OK
)
1149 if (((kinfo
->sim_sdid
& (KINETIS_SDID_DIEID_MASK
)) == KINETIS_SDID_DIEID_K24FN1M
) &&
1150 ((sopt1
& KINETIS_SOPT1_RAMSIZE_MASK
) == KINETIS_SOPT1_RAMSIZE_K24FN1M
)) {
1152 pflash_sector_size_bytes
= 4<<10;
1154 kinfo
->flash_support
= FS_PROGRAM_PHRASE
| FS_PROGRAM_SECTOR
;
1155 kinfo
->max_flash_prog_size
= 1<<10;
1158 if ((kinfo
->sim_sdid
& (KINETIS_SDID_DIEID_MASK
)) == KINETIS_SDID_DIEID_K22FN128
1159 || (kinfo
->sim_sdid
& (KINETIS_SDID_DIEID_MASK
)) == KINETIS_SDID_DIEID_K22FN256
1160 || (kinfo
->sim_sdid
& (KINETIS_SDID_DIEID_MASK
)) == KINETIS_SDID_DIEID_K22FN512
) {
1161 /* K22 with new-style SDID - smaller pflash with FTFA, 2kB sectors */
1162 pflash_sector_size_bytes
= 2<<10;
1163 num_blocks
= 2; /* 1 or 2 blocks */
1164 kinfo
->flash_support
= FS_PROGRAM_LONGWORD
;
1167 LOG_ERROR("Unsupported Kinetis K22 DIEID");
1170 case KINETIS_SDID_FAMILYID_K2X
| KINETIS_SDID_SUBFAMID_KX4
:
1171 pflash_sector_size_bytes
= 4<<10;
1172 if ((kinfo
->sim_sdid
& (KINETIS_SDID_DIEID_MASK
)) == KINETIS_SDID_DIEID_K24FN256
) {
1173 /* K24FN256 - smaller pflash with FTFA */
1175 kinfo
->flash_support
= FS_PROGRAM_LONGWORD
;
1178 /* K24FN1M without errata 7534 */
1180 kinfo
->flash_support
= FS_PROGRAM_PHRASE
| FS_PROGRAM_SECTOR
;
1181 kinfo
->max_flash_prog_size
= 1<<10;
1184 case KINETIS_SDID_FAMILYID_K6X
| KINETIS_SDID_SUBFAMID_KX3
:
1185 case KINETIS_SDID_FAMILYID_K6X
| KINETIS_SDID_SUBFAMID_KX1
: /* errata 7534 - should be K63 */
1187 case KINETIS_SDID_FAMILYID_K6X
| KINETIS_SDID_SUBFAMID_KX4
:
1188 case KINETIS_SDID_FAMILYID_K6X
| KINETIS_SDID_SUBFAMID_KX2
: /* errata 7534 - should be K64 */
1189 /* K64FN1M0, K64FX512 */
1190 pflash_sector_size_bytes
= 4<<10;
1191 nvm_sector_size_bytes
= 4<<10;
1192 kinfo
->max_flash_prog_size
= 1<<10;
1194 kinfo
->flash_support
= FS_PROGRAM_PHRASE
| FS_PROGRAM_SECTOR
;
1197 case KINETIS_SDID_FAMILYID_K2X
| KINETIS_SDID_SUBFAMID_KX6
:
1199 case KINETIS_SDID_FAMILYID_K6X
| KINETIS_SDID_SUBFAMID_KX6
:
1200 /* K66FN2M0, K66FX1M0 */
1201 pflash_sector_size_bytes
= 4<<10;
1202 nvm_sector_size_bytes
= 4<<10;
1203 kinfo
->max_flash_prog_size
= 1<<10;
1205 kinfo
->flash_support
= FS_PROGRAM_PHRASE
| FS_PROGRAM_SECTOR
;
1208 LOG_ERROR("Unsupported Kinetis FAMILYID SUBFAMID");
1211 case KINETIS_SDID_SERIESID_KL
:
1213 pflash_sector_size_bytes
= 1<<10;
1214 nvm_sector_size_bytes
= 1<<10;
1216 kinfo
->flash_support
= FS_PROGRAM_LONGWORD
;
1219 LOG_ERROR("Unsupported K-series");
1223 if (pflash_sector_size_bytes
== 0) {
1224 LOG_ERROR("MCU is unsupported, SDID 0x%08" PRIx32
, kinfo
->sim_sdid
);
1225 return ERROR_FLASH_OPER_UNSUPPORTED
;
1228 result
= target_read_u32(target
, SIM_FCFG1
, &kinfo
->sim_fcfg1
);
1229 if (result
!= ERROR_OK
)
1232 result
= target_read_u32(target
, SIM_FCFG2
, &kinfo
->sim_fcfg2
);
1233 if (result
!= ERROR_OK
)
1235 fcfg2_pflsh
= (kinfo
->sim_fcfg2
>> 23) & 0x01;
1237 LOG_DEBUG("SDID: 0x%08" PRIX32
" FCFG1: 0x%08" PRIX32
" FCFG2: 0x%08" PRIX32
, kinfo
->sim_sdid
,
1238 kinfo
->sim_fcfg1
, kinfo
->sim_fcfg2
);
1240 fcfg1_nvmsize
= (uint8_t)((kinfo
->sim_fcfg1
>> 28) & 0x0f);
1241 fcfg1_pfsize
= (uint8_t)((kinfo
->sim_fcfg1
>> 24) & 0x0f);
1242 fcfg1_eesize
= (uint8_t)((kinfo
->sim_fcfg1
>> 16) & 0x0f);
1244 /* when the PFLSH bit is set, there is no FlexNVM/FlexRAM */
1246 switch (fcfg1_nvmsize
) {
1251 nvm_size
= 1 << (14 + (fcfg1_nvmsize
>> 1));
1254 if (pflash_sector_size_bytes
>= 4<<10)
1265 switch (fcfg1_eesize
) {
1276 ee_size
= (16 << (10 - fcfg1_eesize
));
1284 switch (fcfg1_pfsize
) {
1291 pf_size
= 1 << (14 + (fcfg1_pfsize
>> 1));
1294 if (pflash_sector_size_bytes
>= 4<<10)
1296 else if (fcfg2_pflsh
)
1306 LOG_DEBUG("FlexNVM: %" PRIu32
" PFlash: %" PRIu32
" FlexRAM: %" PRIu32
" PFLSH: %d",
1307 nvm_size
, pf_size
, ee_size
, fcfg2_pflsh
);
1309 num_pflash_blocks
= num_blocks
/ (2 - fcfg2_pflsh
);
1310 first_nvm_bank
= num_pflash_blocks
;
1311 num_nvm_blocks
= num_blocks
- num_pflash_blocks
;
1313 LOG_DEBUG("%d blocks total: %d PFlash, %d FlexNVM",
1314 num_blocks
, num_pflash_blocks
, num_nvm_blocks
);
1317 * If the flash class is already assigned, verify the
1320 if (kinfo
->flash_class
!= FC_AUTO
) {
1321 if (kinfo
->bank_ordinal
!= (unsigned) bank
->bank_number
) {
1322 LOG_WARNING("Flash ordinal/bank number mismatch");
1325 switch (kinfo
->flash_class
) {
1327 if (kinfo
->bank_ordinal
>= first_nvm_bank
) {
1328 LOG_WARNING("Class mismatch, bank %d is not PFlash", bank
->bank_number
);
1330 } else if (bank
->size
!= (pf_size
/ num_pflash_blocks
)) {
1331 LOG_WARNING("PFlash size mismatch");
1333 } else if (bank
->base
!=
1334 (0x00000000 + bank
->size
* kinfo
->bank_ordinal
)) {
1335 LOG_WARNING("PFlash address range mismatch");
1337 } else if (kinfo
->sector_size
!= pflash_sector_size_bytes
) {
1338 LOG_WARNING("PFlash sector size mismatch");
1341 LOG_DEBUG("PFlash bank %d already configured okay",
1342 kinfo
->bank_ordinal
);
1346 if ((kinfo
->bank_ordinal
>= num_blocks
) ||
1347 (kinfo
->bank_ordinal
< first_nvm_bank
)) {
1348 LOG_WARNING("Class mismatch, bank %d is not FlexNVM", bank
->bank_number
);
1350 } else if (bank
->size
!= (nvm_size
/ num_nvm_blocks
)) {
1351 LOG_WARNING("FlexNVM size mismatch");
1353 } else if (bank
->base
!=
1354 (0x10000000 + bank
->size
* kinfo
->bank_ordinal
)) {
1355 LOG_WARNING("FlexNVM address range mismatch");
1357 } else if (kinfo
->sector_size
!= nvm_sector_size_bytes
) {
1358 LOG_WARNING("FlexNVM sector size mismatch");
1361 LOG_DEBUG("FlexNVM bank %d already configured okay",
1362 kinfo
->bank_ordinal
);
1366 if (kinfo
->bank_ordinal
!= num_blocks
) {
1367 LOG_WARNING("Class mismatch, bank %d is not FlexRAM", bank
->bank_number
);
1369 } else if (bank
->size
!= ee_size
) {
1370 LOG_WARNING("FlexRAM size mismatch");
1372 } else if (bank
->base
!= FLEXRAM
) {
1373 LOG_WARNING("FlexRAM address mismatch");
1375 } else if (kinfo
->sector_size
!= nvm_sector_size_bytes
) {
1376 LOG_WARNING("FlexRAM sector size mismatch");
1379 LOG_DEBUG("FlexRAM bank %d already configured okay", kinfo
->bank_ordinal
);
1384 LOG_WARNING("Unknown or inconsistent flash class");
1390 LOG_INFO("Probing flash info for bank %d", bank
->bank_number
);
1397 if ((unsigned)bank
->bank_number
< num_pflash_blocks
) {
1398 /* pflash, banks start at address zero */
1399 kinfo
->flash_class
= FC_PFLASH
;
1400 bank
->size
= (pf_size
/ num_pflash_blocks
);
1401 bank
->base
= 0x00000000 + bank
->size
* bank
->bank_number
;
1402 kinfo
->sector_size
= pflash_sector_size_bytes
;
1403 kinfo
->protection_size
= pf_size
/ 32;
1404 } else if ((unsigned)bank
->bank_number
< num_blocks
) {
1405 /* nvm, banks start at address 0x10000000 */
1406 kinfo
->flash_class
= FC_FLEX_NVM
;
1407 bank
->size
= (nvm_size
/ num_nvm_blocks
);
1408 bank
->base
= 0x10000000 + bank
->size
* (bank
->bank_number
- first_nvm_bank
);
1409 kinfo
->sector_size
= nvm_sector_size_bytes
;
1410 kinfo
->protection_size
= 0; /* FIXME: TODO: depends on DEPART bits, chip */
1411 } else if ((unsigned)bank
->bank_number
== num_blocks
) {
1412 LOG_ERROR("FlexRAM support not yet implemented");
1413 return ERROR_FLASH_OPER_UNSUPPORTED
;
1415 LOG_ERROR("Cannot determine parameters for bank %d, only %d banks on device",
1416 bank
->bank_number
, num_blocks
);
1417 return ERROR_FLASH_BANK_INVALID
;
1420 if (bank
->sectors
) {
1421 free(bank
->sectors
);
1422 bank
->sectors
= NULL
;
1425 if (kinfo
->sector_size
== 0) {
1426 LOG_ERROR("Unknown sector size for bank %d", bank
->bank_number
);
1427 return ERROR_FLASH_BANK_INVALID
;
1430 if (kinfo
->flash_support
& FS_PROGRAM_SECTOR
1431 && kinfo
->max_flash_prog_size
== 0) {
1432 kinfo
->max_flash_prog_size
= kinfo
->sector_size
;
1433 /* Program section size is equal to sector size by default */
1436 bank
->num_sectors
= bank
->size
/ kinfo
->sector_size
;
1437 assert(bank
->num_sectors
> 0);
1438 bank
->sectors
= malloc(sizeof(struct flash_sector
) * bank
->num_sectors
);
1440 for (i
= 0; i
< bank
->num_sectors
; i
++) {
1441 bank
->sectors
[i
].offset
= offset
;
1442 bank
->sectors
[i
].size
= kinfo
->sector_size
;
1443 offset
+= kinfo
->sector_size
;
1444 bank
->sectors
[i
].is_erased
= -1;
1445 bank
->sectors
[i
].is_protected
= 1;
1451 static int kinetis_probe(struct flash_bank
*bank
)
1453 if (bank
->target
->state
!= TARGET_HALTED
) {
1454 LOG_WARNING("Cannot communicate... target not halted.");
1455 return ERROR_TARGET_NOT_HALTED
;
1458 return kinetis_read_part_info(bank
);
1461 static int kinetis_auto_probe(struct flash_bank
*bank
)
1463 struct kinetis_flash_bank
*kinfo
= bank
->driver_priv
;
1465 if (kinfo
->sim_sdid
)
1468 return kinetis_probe(bank
);
1471 static int kinetis_info(struct flash_bank
*bank
, char *buf
, int buf_size
)
1473 const char *bank_class_names
[] = {
1474 "(ANY)", "PFlash", "FlexNVM", "FlexRAM"
1477 struct kinetis_flash_bank
*kinfo
= bank
->driver_priv
;
1479 (void) snprintf(buf
, buf_size
,
1480 "%s driver for %s flash bank %s at 0x%8.8" PRIx32
"",
1481 bank
->driver
->name
, bank_class_names
[kinfo
->flash_class
],
1482 bank
->name
, bank
->base
);
1487 static int kinetis_blank_check(struct flash_bank
*bank
)
1489 struct kinetis_flash_bank
*kinfo
= bank
->driver_priv
;
1491 if (bank
->target
->state
!= TARGET_HALTED
) {
1492 LOG_ERROR("Target not halted");
1493 return ERROR_TARGET_NOT_HALTED
;
1496 if (kinfo
->flash_class
== FC_PFLASH
) {
1500 /* check if whole bank is blank */
1501 result
= kinetis_ftfx_command(bank
, FTFx_CMD_BLOCKSTAT
, bank
->base
, 0, 0, 0, 0, 0, 0, 0, 0, &ftfx_fstat
);
1503 if (result
!= ERROR_OK
)
1506 if (ftfx_fstat
& 0x01) {
1507 /* the whole bank is not erased, check sector-by-sector */
1509 for (i
= 0; i
< bank
->num_sectors
; i
++) {
1511 result
= kinetis_ftfx_command(bank
, FTFx_CMD_SECTSTAT
, bank
->base
+ bank
->sectors
[i
].offset
,
1512 1, 0, 0, 0, 0, 0, 0, 0, &ftfx_fstat
);
1514 if (result
== ERROR_OK
) {
1515 bank
->sectors
[i
].is_erased
= !(ftfx_fstat
& 0x01);
1517 LOG_DEBUG("Ignoring errored PFlash sector blank-check");
1518 bank
->sectors
[i
].is_erased
= -1;
1522 /* the whole bank is erased, update all sectors */
1524 for (i
= 0; i
< bank
->num_sectors
; i
++)
1525 bank
->sectors
[i
].is_erased
= 1;
1528 LOG_WARNING("kinetis_blank_check not supported yet for FlexNVM");
1529 return ERROR_FLASH_OPERATION_FAILED
;
1535 static const struct command_registration kinetis_securtiy_command_handlers
[] = {
1537 .name
= "check_security",
1538 .mode
= COMMAND_EXEC
,
1541 .handler
= kinetis_check_flash_security_status
,
1544 .name
= "mass_erase",
1545 .mode
= COMMAND_EXEC
,
1548 .handler
= kinetis_mdm_mass_erase
,
1551 .name
= "test_securing",
1552 .mode
= COMMAND_EXEC
,
1555 .handler
= kinetis_securing_test
,
1557 COMMAND_REGISTRATION_DONE
1560 static const struct command_registration kinetis_exec_command_handlers
[] = {
1563 .mode
= COMMAND_ANY
,
1566 .chain
= kinetis_securtiy_command_handlers
,
1568 COMMAND_REGISTRATION_DONE
1571 static const struct command_registration kinetis_command_handler
[] = {
1574 .mode
= COMMAND_ANY
,
1575 .help
= "kinetis NAND flash controller commands",
1577 .chain
= kinetis_exec_command_handlers
,
1579 COMMAND_REGISTRATION_DONE
1584 struct flash_driver kinetis_flash
= {
1586 .commands
= kinetis_command_handler
,
1587 .flash_bank_command
= kinetis_flash_bank_command
,
1588 .erase
= kinetis_erase
,
1589 .protect
= kinetis_protect
,
1590 .write
= kinetis_write
,
1591 .read
= default_flash_read
,
1592 .probe
= kinetis_probe
,
1593 .auto_probe
= kinetis_auto_probe
,
1594 .erase_check
= kinetis_blank_check
,
1595 .protect_check
= kinetis_protect_check
,
1596 .info
= kinetis_info
,
Linking to existing account procedure
If you already have an account and want to add another login method
you
MUST first sign in with your existing account and
then change URL to read
https://review.openocd.org/login/?link
to get to this page again but this time it'll work for linking. Thank you.
SSH host keys fingerprints
1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=.. |
|+o.. . |
|*.o . . |
|+B . . . |
|Bo. = o S |
|Oo.+ + = |
|oB=.* = . o |
| =+=.+ + E |
|. .=o . o |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)