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, write to the *
23 * Free Software Foundation, Inc., *
24 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
25 ***************************************************************************/
31 #include <helper/binarybuffer.h>
32 #include <target/algorithm.h>
33 #include <target/armv7m.h>
35 /* em357 register locations */
37 #define EM357_FLASH_ACR 0x40008000
38 #define EM357_FLASH_KEYR 0x40008004
39 #define EM357_FLASH_OPTKEYR 0x40008008
40 #define EM357_FLASH_SR 0x4000800C
41 #define EM357_FLASH_CR 0x40008010
42 #define EM357_FLASH_AR 0x40008014
43 #define EM357_FLASH_OBR 0x4000801C
44 #define EM357_FLASH_WRPR 0x40008020
46 #define EM357_FPEC_CLK 0x4000402c
47 /* option byte location */
49 #define EM357_OB_RDP 0x08040800
50 #define EM357_OB_WRP0 0x08040808
51 #define EM357_OB_WRP1 0x0804080A
52 #define EM357_OB_WRP2 0x0804080C
54 /* FLASH_CR register bits */
56 #define FLASH_PG (1 << 0)
57 #define FLASH_PER (1 << 1)
58 #define FLASH_MER (1 << 2)
59 #define FLASH_OPTPG (1 << 4)
60 #define FLASH_OPTER (1 << 5)
61 #define FLASH_STRT (1 << 6)
62 #define FLASH_LOCK (1 << 7)
63 #define FLASH_OPTWRE (1 << 9)
65 /* FLASH_SR register bits */
67 #define FLASH_BSY (1 << 0)
68 #define FLASH_PGERR (1 << 2)
69 #define FLASH_WRPRTERR (1 << 4)
70 #define FLASH_EOP (1 << 5)
72 /* EM357_FLASH_OBR bit definitions (reading) */
77 /* register unlock keys */
79 #define KEY1 0x45670123
80 #define KEY2 0xCDEF89AB
85 uint16_t user_options
;
86 uint16_t protection
[3];
89 struct em357_flash_bank
91 struct em357_options option_bytes
;
92 struct working_area
*write_algorithm
;
97 static int em357_mass_erase(struct flash_bank
*bank
);
99 /* flash bank em357 <base> <size> 0 0 <target#>
101 FLASH_BANK_COMMAND_HANDLER(em357_flash_bank_command
)
103 struct em357_flash_bank
*em357_info
;
107 return ERROR_COMMAND_SYNTAX_ERROR
;
110 em357_info
= malloc(sizeof(struct em357_flash_bank
));
111 bank
->driver_priv
= em357_info
;
113 em357_info
->write_algorithm
= NULL
;
114 em357_info
->probed
= 0;
119 static inline int em357_get_flash_status(struct flash_bank
*bank
, uint32_t *status
)
121 struct target
*target
= bank
->target
;
122 return target_read_u32(target
, EM357_FLASH_SR
, status
);
125 static int em357_wait_status_busy(struct flash_bank
*bank
, int timeout
)
127 struct target
*target
= bank
->target
;
129 int retval
= ERROR_OK
;
131 /* wait for busy to clear */
134 retval
= em357_get_flash_status(bank
, &status
);
135 if (retval
!= ERROR_OK
)
137 LOG_DEBUG("status: 0x%" PRIx32
"", status
);
138 if ((status
& FLASH_BSY
) == 0)
142 LOG_ERROR("timed out waiting for flash");
148 if (status
& FLASH_WRPRTERR
)
150 LOG_ERROR("em357 device protected");
154 if (status
& FLASH_PGERR
)
156 LOG_ERROR("em357 device programming failed");
160 /* Clear but report errors */
161 if (status
& (FLASH_WRPRTERR
| FLASH_PGERR
))
163 /* If this operation fails, we ignore it and report the original
166 target_write_u32(target
, EM357_FLASH_SR
, FLASH_WRPRTERR
| FLASH_PGERR
);
171 static int em357_read_options(struct flash_bank
*bank
)
174 struct em357_flash_bank
*em357_info
= NULL
;
175 struct target
*target
= bank
->target
;
177 em357_info
= bank
->driver_priv
;
179 /* read current option bytes */
180 int retval
= target_read_u32(target
, EM357_FLASH_OBR
, &optiondata
);
181 if (retval
!= ERROR_OK
)
184 em357_info
->option_bytes
.user_options
= (uint16_t)0xFFFC | ((optiondata
>> 2) & 0x03);
185 em357_info
->option_bytes
.RDP
= (optiondata
& (1 << OPT_READOUT
)) ? 0xFFFF : 0x5AA5;
187 if (optiondata
& (1 << OPT_READOUT
))
188 LOG_INFO("Device Security Bit Set");
190 /* each bit refers to a 4bank protection */
191 retval
= target_read_u32(target
, EM357_FLASH_WRPR
, &optiondata
);
192 if (retval
!= ERROR_OK
)
195 em357_info
->option_bytes
.protection
[0] = (uint16_t)optiondata
;
196 em357_info
->option_bytes
.protection
[1] = (uint16_t)(optiondata
>> 8);
197 em357_info
->option_bytes
.protection
[2] = (uint16_t)(optiondata
>> 16);
202 static int em357_erase_options(struct flash_bank
*bank
)
204 struct em357_flash_bank
*em357_info
= NULL
;
205 struct target
*target
= bank
->target
;
207 em357_info
= bank
->driver_priv
;
209 /* read current options */
210 em357_read_options(bank
);
212 /* unlock flash registers */
213 int retval
= target_write_u32(target
, EM357_FLASH_KEYR
, KEY1
);
214 if (retval
!= ERROR_OK
)
217 retval
= target_write_u32(target
, EM357_FLASH_KEYR
, KEY2
);
218 if (retval
!= ERROR_OK
)
221 /* unlock option flash registers */
222 retval
= target_write_u32(target
, EM357_FLASH_OPTKEYR
, KEY1
);
223 if (retval
!= ERROR_OK
)
225 retval
= target_write_u32(target
, EM357_FLASH_OPTKEYR
, KEY2
);
226 if (retval
!= ERROR_OK
)
229 /* erase option bytes */
230 retval
= target_write_u32(target
, EM357_FLASH_CR
, FLASH_OPTER
| FLASH_OPTWRE
);
231 if (retval
!= ERROR_OK
)
233 retval
= target_write_u32(target
, EM357_FLASH_CR
, FLASH_OPTER
| FLASH_STRT
| FLASH_OPTWRE
);
234 if (retval
!= ERROR_OK
)
237 retval
= em357_wait_status_busy(bank
, 10);
238 if (retval
!= ERROR_OK
)
241 /* clear readout protection and complementary option bytes
242 * this will also force a device unlock if set */
243 em357_info
->option_bytes
.RDP
= 0x5AA5;
248 static int em357_write_options(struct flash_bank
*bank
)
250 struct em357_flash_bank
*em357_info
= NULL
;
251 struct target
*target
= bank
->target
;
253 em357_info
= bank
->driver_priv
;
255 /* unlock flash registers */
256 int retval
= target_write_u32(target
, EM357_FLASH_KEYR
, KEY1
);
257 if (retval
!= ERROR_OK
)
259 retval
= target_write_u32(target
, EM357_FLASH_KEYR
, KEY2
);
260 if (retval
!= ERROR_OK
)
263 /* unlock option flash registers */
264 retval
= target_write_u32(target
, EM357_FLASH_OPTKEYR
, KEY1
);
265 if (retval
!= ERROR_OK
)
267 retval
= target_write_u32(target
, EM357_FLASH_OPTKEYR
, KEY2
);
268 if (retval
!= ERROR_OK
)
271 /* program option bytes */
272 retval
= target_write_u32(target
, EM357_FLASH_CR
, FLASH_OPTPG
| FLASH_OPTWRE
);
273 if (retval
!= ERROR_OK
)
276 retval
= em357_wait_status_busy(bank
, 10);
277 if (retval
!= ERROR_OK
)
280 /* write protection byte 1 */
281 retval
= target_write_u16(target
, EM357_OB_WRP0
, em357_info
->option_bytes
.protection
[0]);
282 if (retval
!= ERROR_OK
)
285 retval
= em357_wait_status_busy(bank
, 10);
286 if (retval
!= ERROR_OK
)
289 /* write protection byte 2 */
290 retval
= target_write_u16(target
, EM357_OB_WRP1
, em357_info
->option_bytes
.protection
[1]);
291 if (retval
!= ERROR_OK
)
294 retval
= em357_wait_status_busy(bank
, 10);
295 if (retval
!= ERROR_OK
)
298 /* write protection byte 3 */
299 retval
= target_write_u16(target
, EM357_OB_WRP2
, em357_info
->option_bytes
.protection
[2]);
300 if (retval
!= ERROR_OK
)
303 retval
= em357_wait_status_busy(bank
, 10);
304 if (retval
!= ERROR_OK
)
307 /* write readout protection bit */
308 retval
= target_write_u16(target
, EM357_OB_RDP
, em357_info
->option_bytes
.RDP
);
309 if (retval
!= ERROR_OK
)
312 retval
= em357_wait_status_busy(bank
, 10);
313 if (retval
!= ERROR_OK
)
316 retval
= target_write_u32(target
, EM357_FLASH_CR
, FLASH_LOCK
);
317 if (retval
!= ERROR_OK
)
323 static int em357_protect_check(struct flash_bank
*bank
)
325 struct target
*target
= bank
->target
;
326 struct em357_flash_bank
*em357_info
= bank
->driver_priv
;
333 if (target
->state
!= TARGET_HALTED
)
335 LOG_ERROR("Target not halted");
336 return ERROR_TARGET_NOT_HALTED
;
339 /* each bit refers to a 4bank protection (bit 0-23) */
340 int retval
= target_read_u32(target
, EM357_FLASH_WRPR
, &protection
);
341 if (retval
!= ERROR_OK
)
344 /* each protection bit is for 4 * 2K pages */
345 num_bits
= (bank
->num_sectors
/ em357_info
->ppage_size
);
347 for (i
= 0; i
< num_bits
; i
++)
350 if (protection
& (1 << i
))
353 for (s
= 0; s
< em357_info
->ppage_size
; s
++)
354 bank
->sectors
[(i
* em357_info
->ppage_size
) + s
].is_protected
= set
;
360 static int em357_erase(struct flash_bank
*bank
, int first
, int last
)
362 struct target
*target
= bank
->target
;
365 if (bank
->target
->state
!= TARGET_HALTED
)
367 LOG_ERROR("Target not halted");
368 return ERROR_TARGET_NOT_HALTED
;
371 if ((first
== 0) && (last
== (bank
->num_sectors
- 1)))
373 return em357_mass_erase(bank
);
376 /* unlock flash registers */
377 int retval
= target_write_u32(target
, EM357_FLASH_KEYR
, KEY1
);
378 if (retval
!= ERROR_OK
)
380 retval
= target_write_u32(target
, EM357_FLASH_KEYR
, KEY2
);
381 if (retval
!= ERROR_OK
)
384 for (i
= first
; i
<= last
; i
++)
386 retval
= target_write_u32(target
, EM357_FLASH_CR
, FLASH_PER
);
387 if (retval
!= ERROR_OK
)
389 retval
= target_write_u32(target
, EM357_FLASH_AR
,
390 bank
->base
+ bank
->sectors
[i
].offset
);
391 if (retval
!= ERROR_OK
)
393 retval
= target_write_u32(target
, EM357_FLASH_CR
, FLASH_PER
| FLASH_STRT
);
394 if (retval
!= ERROR_OK
)
397 retval
= em357_wait_status_busy(bank
, 100);
398 if (retval
!= ERROR_OK
)
401 bank
->sectors
[i
].is_erased
= 1;
404 retval
= target_write_u32(target
, EM357_FLASH_CR
, FLASH_LOCK
);
405 if (retval
!= ERROR_OK
)
411 static int em357_protect(struct flash_bank
*bank
, int set
, int first
, int last
)
413 struct em357_flash_bank
*em357_info
= NULL
;
414 struct target
*target
= bank
->target
;
415 uint16_t prot_reg
[4] = {0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF};
420 em357_info
= bank
->driver_priv
;
422 if (target
->state
!= TARGET_HALTED
)
424 LOG_ERROR("Target not halted");
425 return ERROR_TARGET_NOT_HALTED
;
428 if ((first
% em357_info
->ppage_size
) != 0)
430 LOG_WARNING("aligned start protect sector to a %d sector boundary",
431 em357_info
->ppage_size
);
432 first
= first
- (first
% em357_info
->ppage_size
);
434 if (((last
+ 1) % em357_info
->ppage_size
) != 0)
436 LOG_WARNING("aligned end protect sector to a %d sector boundary",
437 em357_info
->ppage_size
);
439 last
= last
- (last
% em357_info
->ppage_size
);
443 /* each bit refers to a 4bank protection */
444 int retval
= target_read_u32(target
, EM357_FLASH_WRPR
, &protection
);
445 if (retval
!= ERROR_OK
)
448 prot_reg
[0] = (uint16_t)protection
;
449 prot_reg
[1] = (uint16_t)(protection
>> 8);
450 prot_reg
[2] = (uint16_t)(protection
>> 16);
452 for (i
= first
; i
<= last
; i
++)
454 reg
= (i
/ em357_info
->ppage_size
) / 8;
455 bit
= (i
/ em357_info
->ppage_size
) - (reg
* 8);
457 LOG_WARNING("reg, bit: %d, %d", reg
, bit
);
459 prot_reg
[reg
] &= ~(1 << bit
);
461 prot_reg
[reg
] |= (1 << bit
);
464 if ((status
= em357_erase_options(bank
)) != ERROR_OK
)
467 em357_info
->option_bytes
.protection
[0] = prot_reg
[0];
468 em357_info
->option_bytes
.protection
[1] = prot_reg
[1];
469 em357_info
->option_bytes
.protection
[2] = prot_reg
[2];
471 return em357_write_options(bank
);
474 static int em357_write_block(struct flash_bank
*bank
, uint8_t *buffer
,
475 uint32_t offset
, uint32_t count
)
477 struct em357_flash_bank
*em357_info
= bank
->driver_priv
;
478 struct target
*target
= bank
->target
;
479 uint32_t buffer_size
= 16384;
480 struct working_area
*source
;
481 uint32_t address
= bank
->base
+ offset
;
482 struct reg_param reg_params
[4];
483 struct armv7m_algorithm armv7m_info
;
484 int retval
= ERROR_OK
;
486 /* see contib/loaders/flash/stm32x.s for src, the same is used here except for
487 * a modified *_FLASH_BASE */
489 static const uint8_t em357_flash_write_code
[] = {
490 /* #define EM357_FLASH_CR_OFFSET 0x10 */
491 /* #define EM357_FLASH_SR_OFFSET 0x0C */
493 0x08, 0x4c, /* ldr r4, EM357_FLASH_BASE */
494 0x1c, 0x44, /* add r4, r3 */
495 /* write_half_word: */
496 0x01, 0x23, /* movs r3, #0x01 */
497 0x23, 0x61, /* str r3, [r4, #EM357_FLASH_CR_OFFSET] */
498 0x30, 0xf8, 0x02, 0x3b, /* ldrh r3, [r0], #0x02 */
499 0x21, 0xf8, 0x02, 0x3b, /* strh r3, [r1], #0x02 */
501 0xe3, 0x68, /* ldr r3, [r4, #EM357_FLASH_SR_OFFSET] */
502 0x13, 0xf0, 0x01, 0x0f, /* tst r3, #0x01 */
503 0xfb, 0xd0, /* beq busy */
504 0x13, 0xf0, 0x14, 0x0f, /* tst r3, #0x14 */
505 0x01, 0xd1, /* bne exit */
506 0x01, 0x3a, /* subs r2, r2, #0x01 */
507 0xf0, 0xd1, /* bne write_half_word */
509 0x00, 0xbe, /* bkpt #0x00 */
510 0x00, 0x80, 0x00, 0x40, /* EM357_FLASH_BASE: .word 0x40008000 */
513 /* flash write code */
514 if (target_alloc_working_area(target
, sizeof(em357_flash_write_code
),
515 &em357_info
->write_algorithm
) != ERROR_OK
)
517 LOG_WARNING("no working area available, can't do block memory writes");
518 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
521 if ((retval
= target_write_buffer(target
, em357_info
->write_algorithm
->address
,
522 sizeof(em357_flash_write_code
),
523 (uint8_t*)em357_flash_write_code
)) != ERROR_OK
)
527 while (target_alloc_working_area_try(target
, buffer_size
, &source
) != ERROR_OK
)
530 if (buffer_size
<= 256)
532 /* if we already allocated the writing code, but failed to get a
533 * buffer, free the algorithm */
534 if (em357_info
->write_algorithm
)
535 target_free_working_area(target
, em357_info
->write_algorithm
);
537 LOG_WARNING("no large enough working area available, can't do block memory writes");
538 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
542 armv7m_info
.common_magic
= ARMV7M_COMMON_MAGIC
;
543 armv7m_info
.core_mode
= ARMV7M_MODE_ANY
;
545 init_reg_param(®_params
[0], "r0", 32, PARAM_OUT
);
546 init_reg_param(®_params
[1], "r1", 32, PARAM_OUT
);
547 init_reg_param(®_params
[2], "r2", 32, PARAM_OUT
);
548 init_reg_param(®_params
[3], "r3", 32, PARAM_IN_OUT
);
552 uint32_t thisrun_count
= (count
> (buffer_size
/ 2)) ?
553 (buffer_size
/ 2) : count
;
555 if ((retval
= target_write_buffer(target
, source
->address
,
556 thisrun_count
* 2, buffer
)) != ERROR_OK
)
559 buf_set_u32(reg_params
[0].value
, 0, 32, source
->address
);
560 buf_set_u32(reg_params
[1].value
, 0, 32, address
);
561 buf_set_u32(reg_params
[2].value
, 0, 32, thisrun_count
);
562 buf_set_u32(reg_params
[3].value
, 0, 32, 0);
564 if ((retval
= target_run_algorithm(target
, 0, NULL
, 4, reg_params
,
565 em357_info
->write_algorithm
->address
,
567 10000, &armv7m_info
)) != ERROR_OK
)
569 LOG_ERROR("error executing em357 flash write algorithm");
573 if (buf_get_u32(reg_params
[3].value
, 0, 32) & FLASH_PGERR
)
575 LOG_ERROR("flash memory not erased before writing");
576 /* Clear but report errors */
577 target_write_u32(target
, EM357_FLASH_SR
, FLASH_PGERR
);
582 if (buf_get_u32(reg_params
[3].value
, 0, 32) & FLASH_WRPRTERR
)
584 LOG_ERROR("flash memory write protected");
585 /* Clear but report errors */
586 target_write_u32(target
, EM357_FLASH_SR
, FLASH_WRPRTERR
);
591 buffer
+= thisrun_count
* 2;
592 address
+= thisrun_count
* 2;
593 count
-= thisrun_count
;
596 target_free_working_area(target
, source
);
597 target_free_working_area(target
, em357_info
->write_algorithm
);
599 destroy_reg_param(®_params
[0]);
600 destroy_reg_param(®_params
[1]);
601 destroy_reg_param(®_params
[2]);
602 destroy_reg_param(®_params
[3]);
607 static int em357_write(struct flash_bank
*bank
, uint8_t *buffer
,
608 uint32_t offset
, uint32_t count
)
610 struct target
*target
= bank
->target
;
611 uint32_t words_remaining
= (count
/ 2);
612 uint32_t bytes_remaining
= (count
& 0x00000001);
613 uint32_t address
= bank
->base
+ offset
;
614 uint32_t bytes_written
= 0;
617 if (bank
->target
->state
!= TARGET_HALTED
)
619 LOG_ERROR("Target not halted");
620 return ERROR_TARGET_NOT_HALTED
;
625 LOG_WARNING("offset 0x%" PRIx32
" breaks required 2-byte alignment", offset
);
626 return ERROR_FLASH_DST_BREAKS_ALIGNMENT
;
629 /* unlock flash registers */
630 retval
= target_write_u32(target
, EM357_FLASH_KEYR
, KEY1
);
631 if (retval
!= ERROR_OK
)
633 retval
= target_write_u32(target
, EM357_FLASH_KEYR
, KEY2
);
634 if (retval
!= ERROR_OK
)
637 /* multiple half words (2-byte) to be programmed? */
638 if (words_remaining
> 0)
640 /* try using a block write */
641 if ((retval
= em357_write_block(bank
, buffer
, offset
, words_remaining
)) != ERROR_OK
)
643 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
645 /* if block write failed (no sufficient working area),
646 * we use normal (slow) single dword accesses */
647 LOG_WARNING("couldn't use block writes, falling back to single memory accesses");
652 buffer
+= words_remaining
* 2;
653 address
+= words_remaining
* 2;
658 if ((retval
!= ERROR_OK
) && (retval
!= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
))
661 while (words_remaining
> 0)
664 memcpy(&value
, buffer
+ bytes_written
, sizeof(uint16_t));
666 retval
= target_write_u32(target
, EM357_FLASH_CR
, FLASH_PG
);
667 if (retval
!= ERROR_OK
)
669 retval
= target_write_u16(target
, address
, value
);
670 if (retval
!= ERROR_OK
)
673 retval
= em357_wait_status_busy(bank
, 5);
674 if (retval
!= ERROR_OK
)
684 uint16_t value
= 0xffff;
685 memcpy(&value
, buffer
+ bytes_written
, bytes_remaining
);
687 retval
= target_write_u32(target
, EM357_FLASH_CR
, FLASH_PG
);
688 if (retval
!= ERROR_OK
)
690 retval
= target_write_u16(target
, address
, value
);
691 if (retval
!= ERROR_OK
)
694 retval
= em357_wait_status_busy(bank
, 5);
695 if (retval
!= ERROR_OK
)
699 return target_write_u32(target
, EM357_FLASH_CR
, FLASH_LOCK
);
702 static int em357_probe(struct flash_bank
*bank
)
704 struct target
*target
= bank
->target
;
705 struct em357_flash_bank
*em357_info
= bank
->driver_priv
;
709 uint32_t base_address
= 0x08000000;
711 em357_info
->probed
= 0;
713 /* Enable FPEC CLK */
714 int retval
= target_write_u32(target
, EM357_FPEC_CLK
, 0x00000001);
715 if (retval
!= ERROR_OK
)
719 em357_info
->ppage_size
= 4;
722 LOG_INFO("flash size = %dkbytes", num_pages
*page_size
/1024);
727 bank
->sectors
= NULL
;
730 bank
->base
= base_address
;
731 bank
->size
= (num_pages
* page_size
);
732 bank
->num_sectors
= num_pages
;
733 bank
->sectors
= malloc(sizeof(struct flash_sector
) * num_pages
);
735 for (i
= 0; i
< num_pages
; i
++)
737 bank
->sectors
[i
].offset
= i
* page_size
;
738 bank
->sectors
[i
].size
= page_size
;
739 bank
->sectors
[i
].is_erased
= -1;
740 bank
->sectors
[i
].is_protected
= 1;
743 em357_info
->probed
= 1;
748 static int em357_auto_probe(struct flash_bank
*bank
)
750 struct em357_flash_bank
*em357_info
= bank
->driver_priv
;
751 if (em357_info
->probed
)
753 return em357_probe(bank
);
757 static int get_em357_info(struct flash_bank
*bank
, char *buf
, int buf_size
)
759 snprintf(buf
, buf_size
, "em357\n");
763 COMMAND_HANDLER(em357_handle_lock_command
)
765 struct target
*target
= NULL
;
766 struct em357_flash_bank
*em357_info
= NULL
;
770 return ERROR_COMMAND_SYNTAX_ERROR
;
773 struct flash_bank
*bank
;
774 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
775 if (ERROR_OK
!= retval
)
778 em357_info
= bank
->driver_priv
;
780 target
= bank
->target
;
782 if (target
->state
!= TARGET_HALTED
)
784 LOG_ERROR("Target not halted");
785 return ERROR_TARGET_NOT_HALTED
;
788 if (em357_erase_options(bank
) != ERROR_OK
)
790 command_print(CMD_CTX
, "em357 failed to erase options");
794 /* set readout protection */
795 em357_info
->option_bytes
.RDP
= 0;
797 if (em357_write_options(bank
) != ERROR_OK
)
799 command_print(CMD_CTX
, "em357 failed to lock device");
803 command_print(CMD_CTX
, "em357 locked");
808 COMMAND_HANDLER(em357_handle_unlock_command
)
810 struct target
*target
= NULL
;
814 return ERROR_COMMAND_SYNTAX_ERROR
;
817 struct flash_bank
*bank
;
818 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
819 if (ERROR_OK
!= retval
)
822 target
= bank
->target
;
824 if (target
->state
!= TARGET_HALTED
)
826 LOG_ERROR("Target not halted");
827 return ERROR_TARGET_NOT_HALTED
;
830 if (em357_erase_options(bank
) != ERROR_OK
)
832 command_print(CMD_CTX
, "em357 failed to unlock device");
836 if (em357_write_options(bank
) != ERROR_OK
)
838 command_print(CMD_CTX
, "em357 failed to lock device");
842 command_print(CMD_CTX
, "em357 unlocked.\n"
843 "INFO: a reset or power cycle is required "
844 "for the new settings to take effect.");
849 static int em357_mass_erase(struct flash_bank
*bank
)
851 struct target
*target
= bank
->target
;
853 if (target
->state
!= TARGET_HALTED
)
855 LOG_ERROR("Target not halted");
856 return ERROR_TARGET_NOT_HALTED
;
859 /* unlock option flash registers */
860 int retval
= target_write_u32(target
, EM357_FLASH_KEYR
, KEY1
);
861 if (retval
!= ERROR_OK
)
863 retval
= target_write_u32(target
, EM357_FLASH_KEYR
, KEY2
);
864 if (retval
!= ERROR_OK
)
867 /* mass erase flash memory */
868 retval
= target_write_u32(target
, EM357_FLASH_CR
, FLASH_MER
);
869 if (retval
!= ERROR_OK
)
871 retval
= target_write_u32(target
, EM357_FLASH_CR
, FLASH_MER
| FLASH_STRT
);
872 if (retval
!= ERROR_OK
)
875 retval
= em357_wait_status_busy(bank
, 100);
876 if (retval
!= ERROR_OK
)
879 retval
= target_write_u32(target
, EM357_FLASH_CR
, FLASH_LOCK
);
880 if (retval
!= ERROR_OK
)
886 COMMAND_HANDLER(em357_handle_mass_erase_command
)
892 return ERROR_COMMAND_SYNTAX_ERROR
;
895 struct flash_bank
*bank
;
896 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
897 if (ERROR_OK
!= retval
)
900 retval
= em357_mass_erase(bank
);
901 if (retval
== ERROR_OK
)
903 /* set all sectors as erased */
904 for (i
= 0; i
< bank
->num_sectors
; i
++)
906 bank
->sectors
[i
].is_erased
= 1;
909 command_print(CMD_CTX
, "em357 mass erase complete");
913 command_print(CMD_CTX
, "em357 mass erase failed");
919 static const struct command_registration em357_exec_command_handlers
[] = {
923 .handler
= em357_handle_lock_command
,
924 .mode
= COMMAND_EXEC
,
925 .help
= "Lock entire flash device.",
930 .handler
= em357_handle_unlock_command
,
931 .mode
= COMMAND_EXEC
,
932 .help
= "Unlock entire protected flash device.",
935 .name
= "mass_erase",
937 .handler
= em357_handle_mass_erase_command
,
938 .mode
= COMMAND_EXEC
,
939 .help
= "Erase entire flash device.",
941 COMMAND_REGISTRATION_DONE
944 static const struct command_registration em357_command_handlers
[] = {
948 .help
= "em357 flash command group",
950 .chain
= em357_exec_command_handlers
,
952 COMMAND_REGISTRATION_DONE
955 struct flash_driver em357_flash
= {
957 .commands
= em357_command_handlers
,
958 .flash_bank_command
= em357_flash_bank_command
,
959 .erase
= em357_erase
,
960 .protect
= em357_protect
,
961 .write
= em357_write
,
962 .read
= default_flash_read
,
963 .probe
= em357_probe
,
964 .auto_probe
= em357_auto_probe
,
965 .erase_check
= default_flash_mem_blank_check
,
966 .protect_check
= em357_protect_check
,
967 .info
= get_em357_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)