1 /***************************************************************************
2 * Copyright (C) 2005 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
5 * Copyright (C) 2008 by Spencer Oliver *
6 * spen@spen-soft.co.uk *
8 * Copyright (C) 2011 by Erik Botö
9 * erik.boto@pelagicore.com
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
23 ***************************************************************************/
30 #include <helper/binarybuffer.h>
31 #include <target/algorithm.h>
32 #include <target/armv7m.h>
34 /* em357 register locations */
36 #define EM357_FLASH_ACR 0x40008000
37 #define EM357_FLASH_KEYR 0x40008004
38 #define EM357_FLASH_OPTKEYR 0x40008008
39 #define EM357_FLASH_SR 0x4000800C
40 #define EM357_FLASH_CR 0x40008010
41 #define EM357_FLASH_AR 0x40008014
42 #define EM357_FLASH_OBR 0x4000801C
43 #define EM357_FLASH_WRPR 0x40008020
45 #define EM357_FPEC_CLK 0x4000402c
46 /* option byte location */
48 #define EM357_OB_RDP 0x08040800
49 #define EM357_OB_WRP0 0x08040808
50 #define EM357_OB_WRP1 0x0804080A
51 #define EM357_OB_WRP2 0x0804080C
53 /* FLASH_CR register bits */
55 #define FLASH_PG (1 << 0)
56 #define FLASH_PER (1 << 1)
57 #define FLASH_MER (1 << 2)
58 #define FLASH_OPTPG (1 << 4)
59 #define FLASH_OPTER (1 << 5)
60 #define FLASH_STRT (1 << 6)
61 #define FLASH_LOCK (1 << 7)
62 #define FLASH_OPTWRE (1 << 9)
64 /* FLASH_SR register bits */
66 #define FLASH_BSY (1 << 0)
67 #define FLASH_PGERR (1 << 2)
68 #define FLASH_WRPRTERR (1 << 4)
69 #define FLASH_EOP (1 << 5)
71 /* EM357_FLASH_OBR bit definitions (reading) */
76 /* register unlock keys */
78 #define KEY1 0x45670123
79 #define KEY2 0xCDEF89AB
81 struct em357_options
{
83 uint16_t user_options
;
84 uint16_t protection
[3];
87 struct em357_flash_bank
{
88 struct em357_options option_bytes
;
93 static int em357_mass_erase(struct flash_bank
*bank
);
95 /* flash bank em357 <base> <size> 0 0 <target#>
97 FLASH_BANK_COMMAND_HANDLER(em357_flash_bank_command
)
99 struct em357_flash_bank
*em357_info
;
102 return ERROR_COMMAND_SYNTAX_ERROR
;
104 em357_info
= malloc(sizeof(struct em357_flash_bank
));
105 bank
->driver_priv
= em357_info
;
107 em357_info
->probed
= false;
112 static inline int em357_get_flash_status(struct flash_bank
*bank
, uint32_t *status
)
114 struct target
*target
= bank
->target
;
115 return target_read_u32(target
, EM357_FLASH_SR
, status
);
118 static int em357_wait_status_busy(struct flash_bank
*bank
, int timeout
)
120 struct target
*target
= bank
->target
;
122 int retval
= ERROR_OK
;
124 /* wait for busy to clear */
126 retval
= em357_get_flash_status(bank
, &status
);
127 if (retval
!= ERROR_OK
)
129 LOG_DEBUG("status: 0x%" PRIx32
"", status
);
130 if ((status
& FLASH_BSY
) == 0)
132 if (timeout
-- <= 0) {
133 LOG_ERROR("timed out waiting for flash");
139 if (status
& FLASH_WRPRTERR
) {
140 LOG_ERROR("em357 device protected");
144 if (status
& FLASH_PGERR
) {
145 LOG_ERROR("em357 device programming failed");
149 /* Clear but report errors */
150 if (status
& (FLASH_WRPRTERR
| FLASH_PGERR
)) {
151 /* If this operation fails, we ignore it and report the original
154 target_write_u32(target
, EM357_FLASH_SR
, FLASH_WRPRTERR
| FLASH_PGERR
);
159 static int em357_read_options(struct flash_bank
*bank
)
162 struct em357_flash_bank
*em357_info
= NULL
;
163 struct target
*target
= bank
->target
;
165 em357_info
= bank
->driver_priv
;
167 /* read current option bytes */
168 int retval
= target_read_u32(target
, EM357_FLASH_OBR
, &optiondata
);
169 if (retval
!= ERROR_OK
)
172 em357_info
->option_bytes
.user_options
= (uint16_t)0xFFFC | ((optiondata
>> 2) & 0x03);
173 em357_info
->option_bytes
.RDP
= (optiondata
& (1 << OPT_READOUT
)) ? 0xFFFF : 0x5AA5;
175 if (optiondata
& (1 << OPT_READOUT
))
176 LOG_INFO("Device Security Bit Set");
178 /* each bit refers to a 4bank protection */
179 retval
= target_read_u32(target
, EM357_FLASH_WRPR
, &optiondata
);
180 if (retval
!= ERROR_OK
)
183 em357_info
->option_bytes
.protection
[0] = (uint16_t)optiondata
;
184 em357_info
->option_bytes
.protection
[1] = (uint16_t)(optiondata
>> 8);
185 em357_info
->option_bytes
.protection
[2] = (uint16_t)(optiondata
>> 16);
190 static int em357_erase_options(struct flash_bank
*bank
)
192 struct em357_flash_bank
*em357_info
= NULL
;
193 struct target
*target
= bank
->target
;
195 em357_info
= bank
->driver_priv
;
197 /* read current options */
198 em357_read_options(bank
);
200 /* unlock flash registers */
201 int retval
= target_write_u32(target
, EM357_FLASH_KEYR
, KEY1
);
202 if (retval
!= ERROR_OK
)
205 retval
= target_write_u32(target
, EM357_FLASH_KEYR
, KEY2
);
206 if (retval
!= ERROR_OK
)
209 /* unlock option flash registers */
210 retval
= target_write_u32(target
, EM357_FLASH_OPTKEYR
, KEY1
);
211 if (retval
!= ERROR_OK
)
213 retval
= target_write_u32(target
, EM357_FLASH_OPTKEYR
, KEY2
);
214 if (retval
!= ERROR_OK
)
217 /* erase option bytes */
218 retval
= target_write_u32(target
, EM357_FLASH_CR
, FLASH_OPTER
| FLASH_OPTWRE
);
219 if (retval
!= ERROR_OK
)
221 retval
= target_write_u32(target
, EM357_FLASH_CR
, FLASH_OPTER
| FLASH_STRT
| FLASH_OPTWRE
);
222 if (retval
!= ERROR_OK
)
225 retval
= em357_wait_status_busy(bank
, 10);
226 if (retval
!= ERROR_OK
)
229 /* clear readout protection and complementary option bytes
230 * this will also force a device unlock if set */
231 em357_info
->option_bytes
.RDP
= 0x5AA5;
236 static int em357_write_options(struct flash_bank
*bank
)
238 struct em357_flash_bank
*em357_info
= NULL
;
239 struct target
*target
= bank
->target
;
241 em357_info
= bank
->driver_priv
;
243 /* unlock flash registers */
244 int retval
= target_write_u32(target
, EM357_FLASH_KEYR
, KEY1
);
245 if (retval
!= ERROR_OK
)
247 retval
= target_write_u32(target
, EM357_FLASH_KEYR
, KEY2
);
248 if (retval
!= ERROR_OK
)
251 /* unlock option flash registers */
252 retval
= target_write_u32(target
, EM357_FLASH_OPTKEYR
, KEY1
);
253 if (retval
!= ERROR_OK
)
255 retval
= target_write_u32(target
, EM357_FLASH_OPTKEYR
, KEY2
);
256 if (retval
!= ERROR_OK
)
259 /* program option bytes */
260 retval
= target_write_u32(target
, EM357_FLASH_CR
, FLASH_OPTPG
| FLASH_OPTWRE
);
261 if (retval
!= ERROR_OK
)
264 retval
= em357_wait_status_busy(bank
, 10);
265 if (retval
!= ERROR_OK
)
268 /* write protection byte 1 */
269 retval
= target_write_u16(target
, EM357_OB_WRP0
, em357_info
->option_bytes
.protection
[0]);
270 if (retval
!= ERROR_OK
)
273 retval
= em357_wait_status_busy(bank
, 10);
274 if (retval
!= ERROR_OK
)
277 /* write protection byte 2 */
278 retval
= target_write_u16(target
, EM357_OB_WRP1
, em357_info
->option_bytes
.protection
[1]);
279 if (retval
!= ERROR_OK
)
282 retval
= em357_wait_status_busy(bank
, 10);
283 if (retval
!= ERROR_OK
)
286 /* write protection byte 3 */
287 retval
= target_write_u16(target
, EM357_OB_WRP2
, em357_info
->option_bytes
.protection
[2]);
288 if (retval
!= ERROR_OK
)
291 retval
= em357_wait_status_busy(bank
, 10);
292 if (retval
!= ERROR_OK
)
295 /* write readout protection bit */
296 retval
= target_write_u16(target
, EM357_OB_RDP
, em357_info
->option_bytes
.RDP
);
297 if (retval
!= ERROR_OK
)
300 retval
= em357_wait_status_busy(bank
, 10);
301 if (retval
!= ERROR_OK
)
304 retval
= target_write_u32(target
, EM357_FLASH_CR
, FLASH_LOCK
);
305 if (retval
!= ERROR_OK
)
311 static int em357_protect_check(struct flash_bank
*bank
)
313 struct target
*target
= bank
->target
;
314 struct em357_flash_bank
*em357_info
= bank
->driver_priv
;
321 if (target
->state
!= TARGET_HALTED
) {
322 LOG_ERROR("Target not halted");
323 return ERROR_TARGET_NOT_HALTED
;
326 /* each bit refers to a 4bank protection (bit 0-23) */
327 int retval
= target_read_u32(target
, EM357_FLASH_WRPR
, &protection
);
328 if (retval
!= ERROR_OK
)
331 /* each protection bit is for 4 * 2K pages */
332 num_bits
= (bank
->num_sectors
/ em357_info
->ppage_size
);
334 for (i
= 0; i
< num_bits
; i
++) {
336 if (protection
& (1 << i
))
339 for (s
= 0; s
< em357_info
->ppage_size
; s
++)
340 bank
->sectors
[(i
* em357_info
->ppage_size
) + s
].is_protected
= set
;
346 static int em357_erase(struct flash_bank
*bank
, unsigned int first
,
349 struct target
*target
= bank
->target
;
351 if (bank
->target
->state
!= TARGET_HALTED
) {
352 LOG_ERROR("Target not halted");
353 return ERROR_TARGET_NOT_HALTED
;
356 if ((first
== 0) && (last
== (bank
->num_sectors
- 1)))
357 return em357_mass_erase(bank
);
359 /* Enable FPEC clock */
360 target_write_u32(target
, EM357_FPEC_CLK
, 0x00000001);
362 /* unlock flash registers */
363 int retval
= target_write_u32(target
, EM357_FLASH_KEYR
, KEY1
);
364 if (retval
!= ERROR_OK
)
366 retval
= target_write_u32(target
, EM357_FLASH_KEYR
, KEY2
);
367 if (retval
!= ERROR_OK
)
370 for (unsigned int i
= first
; i
<= last
; i
++) {
371 retval
= target_write_u32(target
, EM357_FLASH_CR
, FLASH_PER
);
372 if (retval
!= ERROR_OK
)
374 retval
= target_write_u32(target
, EM357_FLASH_AR
,
375 bank
->base
+ bank
->sectors
[i
].offset
);
376 if (retval
!= ERROR_OK
)
378 retval
= target_write_u32(target
, EM357_FLASH_CR
, FLASH_PER
| FLASH_STRT
);
379 if (retval
!= ERROR_OK
)
382 retval
= em357_wait_status_busy(bank
, 100);
383 if (retval
!= ERROR_OK
)
387 retval
= target_write_u32(target
, EM357_FLASH_CR
, FLASH_LOCK
);
388 if (retval
!= ERROR_OK
)
394 static int em357_protect(struct flash_bank
*bank
, int set
, unsigned int first
,
397 struct em357_flash_bank
*em357_info
= NULL
;
398 struct target
*target
= bank
->target
;
399 uint16_t prot_reg
[4] = {0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF};
404 em357_info
= bank
->driver_priv
;
406 if (target
->state
!= TARGET_HALTED
) {
407 LOG_ERROR("Target not halted");
408 return ERROR_TARGET_NOT_HALTED
;
411 if ((first
% em357_info
->ppage_size
) != 0) {
412 LOG_WARNING("aligned start protect sector to a %d sector boundary",
413 em357_info
->ppage_size
);
414 first
= first
- (first
% em357_info
->ppage_size
);
416 if (((last
+ 1) % em357_info
->ppage_size
) != 0) {
417 LOG_WARNING("aligned end protect sector to a %d sector boundary",
418 em357_info
->ppage_size
);
420 last
= last
- (last
% em357_info
->ppage_size
);
424 /* each bit refers to a 4bank protection */
425 int retval
= target_read_u32(target
, EM357_FLASH_WRPR
, &protection
);
426 if (retval
!= ERROR_OK
)
429 prot_reg
[0] = (uint16_t)protection
;
430 prot_reg
[1] = (uint16_t)(protection
>> 8);
431 prot_reg
[2] = (uint16_t)(protection
>> 16);
433 for (unsigned int i
= first
; i
<= last
; i
++) {
434 reg
= (i
/ em357_info
->ppage_size
) / 8;
435 bit
= (i
/ em357_info
->ppage_size
) - (reg
* 8);
437 LOG_WARNING("reg, bit: %d, %d", reg
, bit
);
439 prot_reg
[reg
] &= ~(1 << bit
);
441 prot_reg
[reg
] |= (1 << bit
);
444 status
= em357_erase_options(bank
);
445 if (retval
!= ERROR_OK
)
448 em357_info
->option_bytes
.protection
[0] = prot_reg
[0];
449 em357_info
->option_bytes
.protection
[1] = prot_reg
[1];
450 em357_info
->option_bytes
.protection
[2] = prot_reg
[2];
452 return em357_write_options(bank
);
455 static int em357_write_block(struct flash_bank
*bank
, const uint8_t *buffer
,
456 uint32_t offset
, uint32_t count
)
458 struct target
*target
= bank
->target
;
459 uint32_t buffer_size
= 16384;
460 struct working_area
*write_algorithm
;
461 struct working_area
*source
;
462 uint32_t address
= bank
->base
+ offset
;
463 struct reg_param reg_params
[4];
464 struct armv7m_algorithm armv7m_info
;
465 int retval
= ERROR_OK
;
467 /* see contrib/loaders/flash/stm32x.s for src, the same is used here except for
468 * a modified *_FLASH_BASE */
470 static const uint8_t em357_flash_write_code
[] = {
471 /* #define EM357_FLASH_CR_OFFSET 0x10
472 * #define EM357_FLASH_SR_OFFSET 0x0C
474 0x08, 0x4c, /* ldr r4, EM357_FLASH_BASE */
475 0x1c, 0x44, /* add r4, r3 */
476 /* write_half_word: */
477 0x01, 0x23, /* movs r3, #0x01 */
478 0x23, 0x61, /* str r3, [r4,
479 *#EM357_FLASH_CR_OFFSET] */
480 0x30, 0xf8, 0x02, 0x3b, /* ldrh r3, [r0], #0x02 */
481 0x21, 0xf8, 0x02, 0x3b, /* strh r3, [r1], #0x02 */
483 0xe3, 0x68, /* ldr r3, [r4,
484 *#EM357_FLASH_SR_OFFSET] */
485 0x13, 0xf0, 0x01, 0x0f, /* tst r3, #0x01 */
486 0xfb, 0xd0, /* beq busy */
487 0x13, 0xf0, 0x14, 0x0f, /* tst r3, #0x14 */
488 0x01, 0xd1, /* bne exit */
489 0x01, 0x3a, /* subs r2, r2, #0x01 */
490 0xf0, 0xd1, /* bne write_half_word */
492 0x00, 0xbe, /* bkpt #0x00 */
493 0x00, 0x80, 0x00, 0x40, /* EM357_FLASH_BASE: .word 0x40008000 */
496 /* flash write code */
497 if (target_alloc_working_area(target
, sizeof(em357_flash_write_code
),
498 &write_algorithm
) != ERROR_OK
) {
499 LOG_WARNING("no working area available, can't do block memory writes");
500 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
503 retval
= target_write_buffer(target
, write_algorithm
->address
,
504 sizeof(em357_flash_write_code
), em357_flash_write_code
);
505 if (retval
!= ERROR_OK
)
509 while (target_alloc_working_area_try(target
, buffer_size
, &source
) != ERROR_OK
) {
511 if (buffer_size
<= 256) {
512 /* we already allocated the writing code, but failed to get a
513 * buffer, free the algorithm */
514 target_free_working_area(target
, write_algorithm
);
517 "no large enough working area available, can't do block memory writes");
518 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
522 armv7m_info
.common_magic
= ARMV7M_COMMON_MAGIC
;
523 armv7m_info
.core_mode
= ARM_MODE_THREAD
;
525 init_reg_param(®_params
[0], "r0", 32, PARAM_OUT
);
526 init_reg_param(®_params
[1], "r1", 32, PARAM_OUT
);
527 init_reg_param(®_params
[2], "r2", 32, PARAM_OUT
);
528 init_reg_param(®_params
[3], "r3", 32, PARAM_IN_OUT
);
531 uint32_t thisrun_count
= (count
> (buffer_size
/ 2)) ?
532 (buffer_size
/ 2) : count
;
534 retval
= target_write_buffer(target
, source
->address
, thisrun_count
* 2, buffer
);
535 if (retval
!= ERROR_OK
)
538 buf_set_u32(reg_params
[0].value
, 0, 32, source
->address
);
539 buf_set_u32(reg_params
[1].value
, 0, 32, address
);
540 buf_set_u32(reg_params
[2].value
, 0, 32, thisrun_count
);
541 buf_set_u32(reg_params
[3].value
, 0, 32, 0);
543 retval
= target_run_algorithm(target
, 0, NULL
, 4, reg_params
,
544 write_algorithm
->address
, 0, 10000, &armv7m_info
);
545 if (retval
!= ERROR_OK
) {
546 LOG_ERROR("error executing em357 flash write algorithm");
550 if (buf_get_u32(reg_params
[3].value
, 0, 32) & FLASH_PGERR
) {
551 LOG_ERROR("flash memory not erased before writing");
552 /* Clear but report errors */
553 target_write_u32(target
, EM357_FLASH_SR
, FLASH_PGERR
);
558 if (buf_get_u32(reg_params
[3].value
, 0, 32) & FLASH_WRPRTERR
) {
559 LOG_ERROR("flash memory write protected");
560 /* Clear but report errors */
561 target_write_u32(target
, EM357_FLASH_SR
, FLASH_WRPRTERR
);
566 buffer
+= thisrun_count
* 2;
567 address
+= thisrun_count
* 2;
568 count
-= thisrun_count
;
571 target_free_working_area(target
, source
);
572 target_free_working_area(target
, write_algorithm
);
574 destroy_reg_param(®_params
[0]);
575 destroy_reg_param(®_params
[1]);
576 destroy_reg_param(®_params
[2]);
577 destroy_reg_param(®_params
[3]);
582 static int em357_write(struct flash_bank
*bank
, const uint8_t *buffer
,
583 uint32_t offset
, uint32_t count
)
585 struct target
*target
= bank
->target
;
586 uint32_t words_remaining
= (count
/ 2);
587 uint32_t bytes_remaining
= (count
& 0x00000001);
588 uint32_t address
= bank
->base
+ offset
;
589 uint32_t bytes_written
= 0;
592 if (bank
->target
->state
!= TARGET_HALTED
) {
593 LOG_ERROR("Target not halted");
594 return ERROR_TARGET_NOT_HALTED
;
598 LOG_WARNING("offset 0x%" PRIx32
" breaks required 2-byte alignment", offset
);
599 return ERROR_FLASH_DST_BREAKS_ALIGNMENT
;
602 /* unlock flash registers */
603 retval
= target_write_u32(target
, EM357_FLASH_KEYR
, KEY1
);
604 if (retval
!= ERROR_OK
)
606 retval
= target_write_u32(target
, EM357_FLASH_KEYR
, KEY2
);
607 if (retval
!= ERROR_OK
)
610 target_write_u32(target
, EM357_FPEC_CLK
, 0x00000001);
612 /* multiple half words (2-byte) to be programmed? */
613 if (words_remaining
> 0) {
614 /* try using a block write */
615 retval
= em357_write_block(bank
, buffer
, offset
, words_remaining
);
616 if (retval
!= ERROR_OK
) {
617 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
) {
618 /* if block write failed (no sufficient working area),
619 * we use normal (slow) single dword accesses */
621 "couldn't use block writes, falling back to single memory accesses");
624 buffer
+= words_remaining
* 2;
625 address
+= words_remaining
* 2;
630 if ((retval
!= ERROR_OK
) && (retval
!= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
))
633 while (words_remaining
> 0) {
635 memcpy(&value
, buffer
+ bytes_written
, sizeof(uint16_t));
637 retval
= target_write_u32(target
, EM357_FLASH_CR
, FLASH_PG
);
638 if (retval
!= ERROR_OK
)
640 retval
= target_write_u16(target
, address
, value
);
641 if (retval
!= ERROR_OK
)
644 retval
= em357_wait_status_busy(bank
, 5);
645 if (retval
!= ERROR_OK
)
653 if (bytes_remaining
) {
654 uint16_t value
= 0xffff;
655 memcpy(&value
, buffer
+ bytes_written
, bytes_remaining
);
657 retval
= target_write_u32(target
, EM357_FLASH_CR
, FLASH_PG
);
658 if (retval
!= ERROR_OK
)
660 retval
= target_write_u16(target
, address
, value
);
661 if (retval
!= ERROR_OK
)
664 retval
= em357_wait_status_busy(bank
, 5);
665 if (retval
!= ERROR_OK
)
669 return target_write_u32(target
, EM357_FLASH_CR
, FLASH_LOCK
);
672 static int em357_probe(struct flash_bank
*bank
)
674 struct target
*target
= bank
->target
;
675 struct em357_flash_bank
*em357_info
= bank
->driver_priv
;
679 uint32_t base_address
= 0x08000000;
681 em357_info
->probed
= false;
683 switch (bank
->size
) {
685 /* 64k -- 64 1k pages */
690 /* 128k -- 128 1k pages */
695 /* 192k -- 96 2k pages */
700 /* 256k -- 128 2k pages */
705 /* 512k -- 256 2k pages */
710 LOG_WARNING("No size specified for em357 flash driver, assuming 192k!");
716 /* Enable FPEC CLK */
717 int retval
= target_write_u32(target
, EM357_FPEC_CLK
, 0x00000001);
718 if (retval
!= ERROR_OK
)
721 em357_info
->ppage_size
= 4;
723 LOG_INFO("flash size = %dkbytes", num_pages
*page_size
/1024);
727 bank
->base
= base_address
;
728 bank
->size
= (num_pages
* page_size
);
729 bank
->num_sectors
= num_pages
;
730 bank
->sectors
= malloc(sizeof(struct flash_sector
) * num_pages
);
732 for (i
= 0; i
< num_pages
; i
++) {
733 bank
->sectors
[i
].offset
= i
* page_size
;
734 bank
->sectors
[i
].size
= page_size
;
735 bank
->sectors
[i
].is_erased
= -1;
736 bank
->sectors
[i
].is_protected
= 1;
739 em357_info
->probed
= true;
744 static int em357_auto_probe(struct flash_bank
*bank
)
746 struct em357_flash_bank
*em357_info
= bank
->driver_priv
;
747 if (em357_info
->probed
)
749 return em357_probe(bank
);
752 COMMAND_HANDLER(em357_handle_lock_command
)
754 struct target
*target
= NULL
;
755 struct em357_flash_bank
*em357_info
= NULL
;
758 return ERROR_COMMAND_SYNTAX_ERROR
;
760 struct flash_bank
*bank
;
761 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
762 if (retval
!= ERROR_OK
)
765 em357_info
= bank
->driver_priv
;
767 target
= bank
->target
;
769 if (target
->state
!= TARGET_HALTED
) {
770 LOG_ERROR("Target not halted");
771 return ERROR_TARGET_NOT_HALTED
;
774 if (em357_erase_options(bank
) != ERROR_OK
) {
775 command_print(CMD
, "em357 failed to erase options");
779 /* set readout protection */
780 em357_info
->option_bytes
.RDP
= 0;
782 if (em357_write_options(bank
) != ERROR_OK
) {
783 command_print(CMD
, "em357 failed to lock device");
787 command_print(CMD
, "em357 locked");
792 COMMAND_HANDLER(em357_handle_unlock_command
)
794 struct target
*target
= NULL
;
797 return ERROR_COMMAND_SYNTAX_ERROR
;
799 struct flash_bank
*bank
;
800 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
801 if (retval
!= ERROR_OK
)
804 target
= bank
->target
;
806 if (target
->state
!= TARGET_HALTED
) {
807 LOG_ERROR("Target not halted");
808 return ERROR_TARGET_NOT_HALTED
;
811 if (em357_erase_options(bank
) != ERROR_OK
) {
812 command_print(CMD
, "em357 failed to unlock device");
816 if (em357_write_options(bank
) != ERROR_OK
) {
817 command_print(CMD
, "em357 failed to lock device");
821 command_print(CMD
, "em357 unlocked.\n"
822 "INFO: a reset or power cycle is required "
823 "for the new settings to take effect.");
828 static int em357_mass_erase(struct flash_bank
*bank
)
830 struct target
*target
= bank
->target
;
832 if (target
->state
!= TARGET_HALTED
) {
833 LOG_ERROR("Target not halted");
834 return ERROR_TARGET_NOT_HALTED
;
837 /* Make sure the flash clock is on */
838 target_write_u32(target
, EM357_FPEC_CLK
, 0x00000001);
840 /* unlock option flash registers */
841 int retval
= target_write_u32(target
, EM357_FLASH_KEYR
, KEY1
);
842 if (retval
!= ERROR_OK
)
844 retval
= target_write_u32(target
, EM357_FLASH_KEYR
, KEY2
);
845 if (retval
!= ERROR_OK
)
848 /* mass erase flash memory */
849 retval
= target_write_u32(target
, EM357_FLASH_CR
, FLASH_MER
);
850 if (retval
!= ERROR_OK
)
852 retval
= target_write_u32(target
, EM357_FLASH_CR
, FLASH_MER
| FLASH_STRT
);
853 if (retval
!= ERROR_OK
)
856 retval
= em357_wait_status_busy(bank
, 100);
857 if (retval
!= ERROR_OK
)
860 retval
= target_write_u32(target
, EM357_FLASH_CR
, FLASH_LOCK
);
861 if (retval
!= ERROR_OK
)
867 COMMAND_HANDLER(em357_handle_mass_erase_command
)
870 return ERROR_COMMAND_SYNTAX_ERROR
;
872 struct flash_bank
*bank
;
873 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
874 if (retval
!= ERROR_OK
)
877 retval
= em357_mass_erase(bank
);
878 if (retval
== ERROR_OK
)
879 command_print(CMD
, "em357 mass erase complete");
881 command_print(CMD
, "em357 mass erase failed");
886 static const struct command_registration em357_exec_command_handlers
[] = {
890 .handler
= em357_handle_lock_command
,
891 .mode
= COMMAND_EXEC
,
892 .help
= "Lock entire flash device.",
897 .handler
= em357_handle_unlock_command
,
898 .mode
= COMMAND_EXEC
,
899 .help
= "Unlock entire protected flash device.",
902 .name
= "mass_erase",
904 .handler
= em357_handle_mass_erase_command
,
905 .mode
= COMMAND_EXEC
,
906 .help
= "Erase entire flash device.",
908 COMMAND_REGISTRATION_DONE
911 static const struct command_registration em357_command_handlers
[] = {
915 .help
= "em357 flash command group",
917 .chain
= em357_exec_command_handlers
,
919 COMMAND_REGISTRATION_DONE
922 const struct flash_driver em357_flash
= {
924 .commands
= em357_command_handlers
,
925 .flash_bank_command
= em357_flash_bank_command
,
926 .erase
= em357_erase
,
927 .protect
= em357_protect
,
928 .write
= em357_write
,
929 .read
= default_flash_read
,
930 .probe
= em357_probe
,
931 .auto_probe
= em357_auto_probe
,
932 .erase_check
= default_flash_blank_check
,
933 .protect_check
= em357_protect_check
,
934 .free_driver_priv
= default_flash_free_driver_priv
,
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)