1 /***************************************************************************
2 * Copyright (C) 2011 by Mathias Kuester *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
25 #include "helper/binarybuffer.h"
27 static int kinetis_get_master_bank(struct flash_bank
*bank
,
28 struct flash_bank
**master_bank
)
30 *master_bank
= get_flash_bank_by_name_noprobe(bank
->name
);
31 if (*master_bank
== NULL
) {
32 LOG_ERROR("master flash bank '%s' does not exist",
33 (char *)bank
->driver_priv
);
34 return ERROR_FLASH_OPERATION_FAILED
;
40 static int kinetis_update_bank_info(struct flash_bank
*bank
)
43 struct flash_bank
*master_bank
;
45 result
= kinetis_get_master_bank(bank
, &master_bank
);
47 if (result
!= ERROR_OK
) {
51 /* update the info we do not have */
52 bank
->size
= master_bank
->size
;
53 bank
->chip_width
= master_bank
->chip_width
;
54 bank
->bus_width
= master_bank
->bus_width
;
55 bank
->num_sectors
= master_bank
->num_sectors
;
56 bank
->sectors
= master_bank
->sectors
;
61 FLASH_BANK_COMMAND_HANDLER(kinetis_flash_bank_command
)
64 LOG_ERROR("incomplete flash_bank kinetis configuration %d",
66 return ERROR_FLASH_OPERATION_FAILED
;
69 LOG_INFO("add flash_bank kinetis %s", bank
->name
);
74 static int kinetis_protect(struct flash_bank
*bank
, int set
, int first
,
78 struct flash_bank
*master_bank
;
80 result
= kinetis_get_master_bank(bank
, &master_bank
);
82 if (result
!= ERROR_OK
) {
86 LOG_WARNING("kinetis_protect not supported yet");
88 if (bank
->target
->state
!= TARGET_HALTED
) {
89 LOG_ERROR("Target not halted");
90 return ERROR_TARGET_NOT_HALTED
;
96 static int kinetis_protect_check(struct flash_bank
*bank
)
99 struct flash_bank
*master_bank
;
101 uint32_t fprot
, psize
, psec
;
104 if (bank
->target
->state
!= TARGET_HALTED
) {
105 LOG_ERROR("Target not halted");
106 return ERROR_TARGET_NOT_HALTED
;
109 result
= kinetis_get_master_bank(bank
, &master_bank
);
111 if (result
!= ERROR_OK
) {
115 /* read protection register FTFL_FPROT */
116 result
= target_read_memory(bank
->target
, 0x40020010, 1, 4, buffer
);
118 if (result
!= ERROR_OK
) {
122 fprot
= target_buffer_get_u32(bank
->target
, buffer
);
124 /* every bit protect 1/32 of the full flash */
125 psize
= bank
->size
/ 32;
129 for (i
= 0; i
< bank
->num_sectors
; i
++) {
130 if ((fprot
>> b
) & 1)
131 bank
->sectors
[i
].is_protected
= 0;
133 bank
->sectors
[i
].is_protected
= 1;
135 psec
+= bank
->sectors
[i
].size
;
146 static int kinetis_ftfl_command(struct flash_bank
*bank
, uint32_t w0
,
147 uint32_t w1
, uint32_t w2
)
153 for (i
= 0; i
< 50; i
++) {
155 target_read_memory(bank
->target
, 0x40020000, 1, 1, buffer
);
157 if (result
!= ERROR_OK
) {
161 if (buffer
[0] & 0x80)
167 if (buffer
[0] != 0x80) {
168 /* reset error flags */
171 target_write_memory(bank
->target
, 0x40020000, 1, 1, buffer
);
172 if (result
!= ERROR_OK
) {
177 target_buffer_set_u32(bank
->target
, buffer
, w0
);
178 target_buffer_set_u32(bank
->target
, buffer
+ 4, w1
);
179 target_buffer_set_u32(bank
->target
, buffer
+ 8, w2
);
181 result
= target_write_memory(bank
->target
, 0x40020004, 4, 3, buffer
);
183 if (result
!= ERROR_OK
) {
189 result
= target_write_memory(bank
->target
, 0x40020000, 1, 1, buffer
);
190 if (result
!= ERROR_OK
) {
195 for (i
= 0; i
< 50; i
++) {
197 target_read_memory(bank
->target
, 0x40020000, 1, 1, buffer
);
199 if (result
!= ERROR_OK
) {
203 if (buffer
[0] & 0x80)
209 if (buffer
[0] != 0x80) {
211 ("ftfl command failed FSTAT: %02X W0: %08X W1: %08X W2: %08X",
212 buffer
[0], w0
, w1
, w2
);
214 return ERROR_FLASH_OPERATION_FAILED
;
220 static int kinetis_erase(struct flash_bank
*bank
, int first
, int last
)
222 struct flash_bank
*master_bank
;
224 uint32_t w0
= 0, w1
= 0, w2
= 0;
226 if (bank
->target
->state
!= TARGET_HALTED
) {
227 LOG_ERROR("Target not halted");
228 return ERROR_TARGET_NOT_HALTED
;
231 result
= kinetis_get_master_bank(bank
, &master_bank
);
233 if (result
!= ERROR_OK
) {
237 if ((first
> bank
->num_sectors
) || (last
> bank
->num_sectors
)) {
238 return ERROR_FLASH_OPERATION_FAILED
;
241 for (i
= first
; i
<= last
; i
++) {
242 /* set command and sector address */
243 w0
= (0x09 << 24) | bank
->sectors
[i
].offset
;
245 result
= kinetis_ftfl_command(bank
, w0
, w1
, w2
);
247 if (result
!= ERROR_OK
) {
248 LOG_WARNING("erase sector %d failed", i
);
249 return ERROR_FLASH_OPERATION_FAILED
;
252 bank
->sectors
[i
].is_erased
= 1;
257 ("flash configuration field erased, please reset the device");
263 static int kinetis_write(struct flash_bank
*bank
, uint8_t * buffer
,
264 uint32_t offset
, uint32_t count
)
266 struct flash_bank
*master_bank
;
267 unsigned int i
, result
, fallback
= 0;
269 uint32_t wc
, w0
= 0, w1
= 0, w2
= 0;
271 if (bank
->target
->state
!= TARGET_HALTED
) {
272 LOG_ERROR("Target not halted");
273 return ERROR_TARGET_NOT_HALTED
;
276 result
= kinetis_get_master_bank(bank
, &master_bank
);
278 if (result
!= ERROR_OK
) {
282 /* make flex ram available */
283 w0
= (0x81 << 24) | 0x00ff0000;
285 result
= kinetis_ftfl_command(bank
, w0
, w1
, w2
);
287 if (result
!= ERROR_OK
) {
288 return ERROR_FLASH_OPERATION_FAILED
;
291 /* check if ram ready */
292 result
= target_read_memory(bank
->target
, 0x40020001, 1, 1, buf
);
294 if (result
!= ERROR_OK
) {
298 if (!(buf
[0] & (1 << 1))) {
299 /* fallback to longword write */
303 ("ram not ready, fallback to slow longword write (FCNFG: %02X)",
307 /* program section command */
309 for (i
= 0; i
< count
; i
+= (2 * 1024)) {
312 if ((count
- i
) < (2 * 1024)) {
317 LOG_DEBUG("write section @ %08X with length %d",
320 /* write data to flexram */
322 target_write_memory(bank
->target
, 0x14000000, 4, wc
,
325 if (result
!= ERROR_OK
) {
326 LOG_ERROR("target_write_memory failed");
331 /* execute section command */
332 w0
= (0x0b << 24) | (offset
+ i
);
335 result
= kinetis_ftfl_command(bank
, w0
, w1
, w2
);
337 if (result
!= ERROR_OK
) {
338 return ERROR_FLASH_OPERATION_FAILED
;
342 /* program longword command */
344 for (i
= 0; i
< count
; i
+= 4) {
345 LOG_DEBUG("write longword @ %08X", offset
+ i
);
347 w0
= (0x06 << 24) | (offset
+ i
);
348 w1
= buf_get_u32(buffer
+ offset
+ i
, 0, 32);
350 result
= kinetis_ftfl_command(bank
, w0
, w1
, w2
);
352 if (result
!= ERROR_OK
) {
353 return ERROR_FLASH_OPERATION_FAILED
;
361 static int kinetis_probe(struct flash_bank
*bank
)
363 struct flash_bank
*master_bank
;
366 uint32_t sim_sdid
, sim_fcfg1
, sim_fcfg2
, offset
= 0;
367 uint32_t nvm_size
, pf_size
, flash_size
, ee_size
;
369 if (bank
->target
->state
!= TARGET_HALTED
) {
370 LOG_ERROR("Target not halted");
371 return ERROR_TARGET_NOT_HALTED
;
374 result
= kinetis_get_master_bank(bank
, &master_bank
);
376 if (result
!= ERROR_OK
) {
380 result
= target_read_memory(bank
->target
, 0x40048024, 1, 4, buf
);
381 if (result
!= ERROR_OK
) {
384 sim_sdid
= target_buffer_get_u32(bank
->target
, buf
);
385 result
= target_read_memory(bank
->target
, 0x4004804c, 1, 4, buf
);
386 if (result
!= ERROR_OK
) {
389 sim_fcfg1
= target_buffer_get_u32(bank
->target
, buf
);
390 result
= target_read_memory(bank
->target
, 0x40048050, 1, 4, buf
);
391 if (result
!= ERROR_OK
) {
394 sim_fcfg2
= target_buffer_get_u32(bank
->target
, buf
);
396 LOG_DEBUG("SDID: %08X FCFG1: %08X FCFG2: %08X", sim_sdid
, sim_fcfg1
,
399 switch ((sim_fcfg1
>> 28) & 0x0f) {
401 nvm_size
= 128 * 1024;
405 nvm_size
= 256 * 1024;
412 switch ((sim_fcfg1
>> 24) & 0x0f) {
414 pf_size
= 128 * 1024;
417 pf_size
= 256 * 1024;
421 pf_size
= 512 * 1024;
428 /* pf_size is the total size */
429 flash_size
= pf_size
- nvm_size
;
431 switch ((sim_fcfg1
>> 16) & 0x0f) {
461 LOG_DEBUG("NVM: %d PF: %d EE: %d BL1: %d", nvm_size
, pf_size
, ee_size
,
462 (sim_fcfg2
>> 23) & 1);
464 if (flash_size
!= bank
->size
) {
465 LOG_WARNING("flash size is different %d != %d", flash_size
,
469 bank
->num_sectors
= bank
->size
/ (2 * 1024);
470 assert(bank
->num_sectors
> 0);
471 bank
->sectors
= malloc(sizeof(struct flash_sector
) * bank
->num_sectors
);
473 for (i
= 0; i
< bank
->num_sectors
; i
++) {
474 bank
->sectors
[i
].offset
= offset
;
475 bank
->sectors
[i
].size
= 2 * 1024;
476 offset
+= bank
->sectors
[i
].size
;
477 bank
->sectors
[i
].is_erased
= -1;
478 bank
->sectors
[i
].is_protected
= 1;
481 /* update the info we do not have */
482 return kinetis_update_bank_info(bank
);
485 static int kinetis_auto_probe(struct flash_bank
*bank
)
487 return kinetis_probe(bank
);
490 static int kinetis_info(struct flash_bank
*bank
, char *buf
, int buf_size
)
493 struct flash_bank
*master_bank
;
495 result
= kinetis_get_master_bank(bank
, &master_bank
);
497 if (result
!= ERROR_OK
) {
501 snprintf(buf
, buf_size
,
502 "%s driver for flash bank %s at 0x%8.8" PRIx32
"",
503 bank
->driver
->name
, master_bank
->name
, master_bank
->base
);
508 static int kinetis_blank_check(struct flash_bank
*bank
)
511 struct flash_bank
*master_bank
;
513 LOG_WARNING("kinetis_blank_check not supported yet");
515 if (bank
->target
->state
!= TARGET_HALTED
) {
516 LOG_ERROR("Target not halted");
517 return ERROR_TARGET_NOT_HALTED
;
520 result
= kinetis_get_master_bank(bank
, &master_bank
);
522 if (result
!= ERROR_OK
) {
529 static int kinetis_flash_read(struct flash_bank
*bank
,
530 uint8_t * buffer
, uint32_t offset
, uint32_t count
)
533 struct flash_bank
*master_bank
;
535 LOG_WARNING("kinetis_flash_read not supported yet");
537 if (bank
->target
->state
!= TARGET_HALTED
) {
538 LOG_ERROR("Target not halted");
539 return ERROR_TARGET_NOT_HALTED
;
542 result
= kinetis_get_master_bank(bank
, &master_bank
);
544 if (result
!= ERROR_OK
) {
551 struct flash_driver kinetis_flash
= {
553 .flash_bank_command
= kinetis_flash_bank_command
,
554 .erase
= kinetis_erase
,
555 .protect
= kinetis_protect
,
556 .write
= kinetis_write
,
557 .read
= kinetis_flash_read
,
558 .probe
= kinetis_probe
,
559 .auto_probe
= kinetis_auto_probe
,
560 .erase_check
= kinetis_blank_check
,
561 .protect_check
= kinetis_protect_check
,
562 .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)