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 LOG_WARNING("incomplete flash_bank em357 configuration");
108 return ERROR_FLASH_BANK_INVALID
;
111 em357_info
= malloc(sizeof(struct em357_flash_bank
));
112 bank
->driver_priv
= em357_info
;
114 em357_info
->write_algorithm
= NULL
;
115 em357_info
->probed
= 0;
120 static inline int em357_get_flash_status(struct flash_bank
*bank
, uint32_t *status
)
122 struct target
*target
= bank
->target
;
123 return target_read_u32(target
, EM357_FLASH_SR
, status
);
126 static int em357_wait_status_busy(struct flash_bank
*bank
, int timeout
)
128 struct target
*target
= bank
->target
;
130 int retval
= ERROR_OK
;
132 /* wait for busy to clear */
135 retval
= em357_get_flash_status(bank
, &status
);
136 if (retval
!= ERROR_OK
)
138 LOG_DEBUG("status: 0x%" PRIx32
"", status
);
139 if ((status
& FLASH_BSY
) == 0)
143 LOG_ERROR("timed out waiting for flash");
149 if (status
& FLASH_WRPRTERR
)
151 LOG_ERROR("em357 device protected");
155 if (status
& FLASH_PGERR
)
157 LOG_ERROR("em357 device programming failed");
161 /* Clear but report errors */
162 if (status
& (FLASH_WRPRTERR
| FLASH_PGERR
))
164 /* If this operation fails, we ignore it and report the original
167 target_write_u32(target
, EM357_FLASH_SR
, FLASH_WRPRTERR
| FLASH_PGERR
);
172 static int em357_read_options(struct flash_bank
*bank
)
175 struct em357_flash_bank
*em357_info
= NULL
;
176 struct target
*target
= bank
->target
;
178 em357_info
= bank
->driver_priv
;
180 /* read current option bytes */
181 int retval
= target_read_u32(target
, EM357_FLASH_OBR
, &optiondata
);
182 if (retval
!= ERROR_OK
)
185 em357_info
->option_bytes
.user_options
= (uint16_t)0xFFFC | ((optiondata
>> 2) & 0x03);
186 em357_info
->option_bytes
.RDP
= (optiondata
& (1 << OPT_READOUT
)) ? 0xFFFF : 0x5AA5;
188 if (optiondata
& (1 << OPT_READOUT
))
189 LOG_INFO("Device Security Bit Set");
191 /* each bit refers to a 4bank protection */
192 retval
= target_read_u32(target
, EM357_FLASH_WRPR
, &optiondata
);
193 if (retval
!= ERROR_OK
)
196 em357_info
->option_bytes
.protection
[0] = (uint16_t)optiondata
;
197 em357_info
->option_bytes
.protection
[1] = (uint16_t)(optiondata
>> 8);
198 em357_info
->option_bytes
.protection
[2] = (uint16_t)(optiondata
>> 16);
203 static int em357_erase_options(struct flash_bank
*bank
)
205 struct em357_flash_bank
*em357_info
= NULL
;
206 struct target
*target
= bank
->target
;
208 em357_info
= bank
->driver_priv
;
210 /* read current options */
211 em357_read_options(bank
);
213 /* unlock flash registers */
214 int retval
= target_write_u32(target
, EM357_FLASH_KEYR
, KEY1
);
215 if (retval
!= ERROR_OK
)
218 retval
= target_write_u32(target
, EM357_FLASH_KEYR
, KEY2
);
219 if (retval
!= ERROR_OK
)
222 /* unlock option flash registers */
223 retval
= target_write_u32(target
, EM357_FLASH_OPTKEYR
, KEY1
);
224 if (retval
!= ERROR_OK
)
226 retval
= target_write_u32(target
, EM357_FLASH_OPTKEYR
, KEY2
);
227 if (retval
!= ERROR_OK
)
230 /* erase option bytes */
231 retval
= target_write_u32(target
, EM357_FLASH_CR
, FLASH_OPTER
| FLASH_OPTWRE
);
232 if (retval
!= ERROR_OK
)
234 retval
= target_write_u32(target
, EM357_FLASH_CR
, FLASH_OPTER
| FLASH_STRT
| FLASH_OPTWRE
);
235 if (retval
!= ERROR_OK
)
238 retval
= em357_wait_status_busy(bank
, 10);
239 if (retval
!= ERROR_OK
)
242 /* clear readout protection and complementary option bytes
243 * this will also force a device unlock if set */
244 em357_info
->option_bytes
.RDP
= 0x5AA5;
249 static int em357_write_options(struct flash_bank
*bank
)
251 struct em357_flash_bank
*em357_info
= NULL
;
252 struct target
*target
= bank
->target
;
254 em357_info
= bank
->driver_priv
;
256 /* unlock flash registers */
257 int retval
= target_write_u32(target
, EM357_FLASH_KEYR
, KEY1
);
258 if (retval
!= ERROR_OK
)
260 retval
= target_write_u32(target
, EM357_FLASH_KEYR
, KEY2
);
261 if (retval
!= ERROR_OK
)
264 /* unlock option flash registers */
265 retval
= target_write_u32(target
, EM357_FLASH_OPTKEYR
, KEY1
);
266 if (retval
!= ERROR_OK
)
268 retval
= target_write_u32(target
, EM357_FLASH_OPTKEYR
, KEY2
);
269 if (retval
!= ERROR_OK
)
272 /* program option bytes */
273 retval
= target_write_u32(target
, EM357_FLASH_CR
, FLASH_OPTPG
| FLASH_OPTWRE
);
274 if (retval
!= ERROR_OK
)
277 retval
= em357_wait_status_busy(bank
, 10);
278 if (retval
!= ERROR_OK
)
281 /* write protection byte 1 */
282 retval
= target_write_u16(target
, EM357_OB_WRP0
, em357_info
->option_bytes
.protection
[0]);
283 if (retval
!= ERROR_OK
)
286 retval
= em357_wait_status_busy(bank
, 10);
287 if (retval
!= ERROR_OK
)
290 /* write protection byte 2 */
291 retval
= target_write_u16(target
, EM357_OB_WRP1
, em357_info
->option_bytes
.protection
[1]);
292 if (retval
!= ERROR_OK
)
295 retval
= em357_wait_status_busy(bank
, 10);
296 if (retval
!= ERROR_OK
)
299 /* write protection byte 3 */
300 retval
= target_write_u16(target
, EM357_OB_WRP2
, em357_info
->option_bytes
.protection
[2]);
301 if (retval
!= ERROR_OK
)
304 retval
= em357_wait_status_busy(bank
, 10);
305 if (retval
!= ERROR_OK
)
308 /* write readout protection bit */
309 retval
= target_write_u16(target
, EM357_OB_RDP
, em357_info
->option_bytes
.RDP
);
310 if (retval
!= ERROR_OK
)
313 retval
= em357_wait_status_busy(bank
, 10);
314 if (retval
!= ERROR_OK
)
317 retval
= target_write_u32(target
, EM357_FLASH_CR
, FLASH_LOCK
);
318 if (retval
!= ERROR_OK
)
324 static int em357_protect_check(struct flash_bank
*bank
)
326 struct target
*target
= bank
->target
;
327 struct em357_flash_bank
*em357_info
= bank
->driver_priv
;
334 if (target
->state
!= TARGET_HALTED
)
336 LOG_ERROR("Target not halted");
337 return ERROR_TARGET_NOT_HALTED
;
340 /* each bit refers to a 4bank protection (bit 0-23) */
341 int retval
= target_read_u32(target
, EM357_FLASH_WRPR
, &protection
);
342 if (retval
!= ERROR_OK
)
345 /* each protection bit is for 4 * 2K pages */
346 num_bits
= (bank
->num_sectors
/ em357_info
->ppage_size
);
348 for (i
= 0; i
< num_bits
; i
++)
351 if (protection
& (1 << i
))
354 for (s
= 0; s
< em357_info
->ppage_size
; s
++)
355 bank
->sectors
[(i
* em357_info
->ppage_size
) + s
].is_protected
= set
;
361 static int em357_erase(struct flash_bank
*bank
, int first
, int last
)
363 struct target
*target
= bank
->target
;
366 if (bank
->target
->state
!= TARGET_HALTED
)
368 LOG_ERROR("Target not halted");
369 return ERROR_TARGET_NOT_HALTED
;
372 if ((first
== 0) && (last
== (bank
->num_sectors
- 1)))
374 return em357_mass_erase(bank
);
377 /* unlock flash registers */
378 int retval
= target_write_u32(target
, EM357_FLASH_KEYR
, KEY1
);
379 if (retval
!= ERROR_OK
)
381 retval
= target_write_u32(target
, EM357_FLASH_KEYR
, KEY2
);
382 if (retval
!= ERROR_OK
)
385 for (i
= first
; i
<= last
; i
++)
387 retval
= target_write_u32(target
, EM357_FLASH_CR
, FLASH_PER
);
388 if (retval
!= ERROR_OK
)
390 retval
= target_write_u32(target
, EM357_FLASH_AR
,
391 bank
->base
+ bank
->sectors
[i
].offset
);
392 if (retval
!= ERROR_OK
)
394 retval
= target_write_u32(target
, EM357_FLASH_CR
, FLASH_PER
| FLASH_STRT
);
395 if (retval
!= ERROR_OK
)
398 retval
= em357_wait_status_busy(bank
, 100);
399 if (retval
!= ERROR_OK
)
402 bank
->sectors
[i
].is_erased
= 1;
405 retval
= target_write_u32(target
, EM357_FLASH_CR
, FLASH_LOCK
);
406 if (retval
!= ERROR_OK
)
412 static int em357_protect(struct flash_bank
*bank
, int set
, int first
, int last
)
414 struct em357_flash_bank
*em357_info
= NULL
;
415 struct target
*target
= bank
->target
;
416 uint16_t prot_reg
[4] = {0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF};
421 em357_info
= bank
->driver_priv
;
423 if (target
->state
!= TARGET_HALTED
)
425 LOG_ERROR("Target not halted");
426 return ERROR_TARGET_NOT_HALTED
;
429 if ((first
% em357_info
->ppage_size
) != 0)
431 LOG_WARNING("aligned start protect sector to a %d sector boundary",
432 em357_info
->ppage_size
);
433 first
= first
- (first
% em357_info
->ppage_size
);
435 if (((last
+ 1) % em357_info
->ppage_size
) != 0)
437 LOG_WARNING("aligned end protect sector to a %d sector boundary",
438 em357_info
->ppage_size
);
440 last
= last
- (last
% em357_info
->ppage_size
);
444 /* each bit refers to a 4bank protection */
445 int retval
= target_read_u32(target
, EM357_FLASH_WRPR
, &protection
);
446 if (retval
!= ERROR_OK
)
449 prot_reg
[0] = (uint16_t)protection
;
450 prot_reg
[1] = (uint16_t)(protection
>> 8);
451 prot_reg
[2] = (uint16_t)(protection
>> 16);
453 for (i
= first
; i
<= last
; i
++)
455 reg
= (i
/ em357_info
->ppage_size
) / 8;
456 bit
= (i
/ em357_info
->ppage_size
) - (reg
* 8);
458 LOG_WARNING("reg, bit: %d, %d", reg
, bit
);
460 prot_reg
[reg
] &= ~(1 << bit
);
462 prot_reg
[reg
] |= (1 << bit
);
465 if ((status
= em357_erase_options(bank
)) != ERROR_OK
)
468 em357_info
->option_bytes
.protection
[0] = prot_reg
[0];
469 em357_info
->option_bytes
.protection
[1] = prot_reg
[1];
470 em357_info
->option_bytes
.protection
[2] = prot_reg
[2];
472 return em357_write_options(bank
);
475 static int em357_write_block(struct flash_bank
*bank
, uint8_t *buffer
,
476 uint32_t offset
, uint32_t count
)
478 struct em357_flash_bank
*em357_info
= bank
->driver_priv
;
479 struct target
*target
= bank
->target
;
480 uint32_t buffer_size
= 16384;
481 struct working_area
*source
;
482 uint32_t address
= bank
->base
+ offset
;
483 struct reg_param reg_params
[4];
484 struct armv7m_algorithm armv7m_info
;
485 int retval
= ERROR_OK
;
487 /* see contib/loaders/flash/stm32x.s for src, the same is used here except for
488 * a modified *_FLASH_BASE */
490 static const uint8_t em357_flash_write_code
[] = {
491 /* #define EM357_FLASH_CR_OFFSET 0x10 */
492 /* #define EM357_FLASH_SR_OFFSET 0x0C */
494 0x08, 0x4c, /* ldr r4, EM357_FLASH_BASE */
495 0x1c, 0x44, /* add r4, r3 */
496 /* write_half_word: */
497 0x01, 0x23, /* movs r3, #0x01 */
498 0x23, 0x61, /* str r3, [r4, #EM357_FLASH_CR_OFFSET] */
499 0x30, 0xf8, 0x02, 0x3b, /* ldrh r3, [r0], #0x02 */
500 0x21, 0xf8, 0x02, 0x3b, /* strh r3, [r1], #0x02 */
502 0xe3, 0x68, /* ldr r3, [r4, #EM357_FLASH_SR_OFFSET] */
503 0x13, 0xf0, 0x01, 0x0f, /* tst r3, #0x01 */
504 0xfb, 0xd0, /* beq busy */
505 0x13, 0xf0, 0x14, 0x0f, /* tst r3, #0x14 */
506 0x01, 0xd1, /* bne exit */
507 0x01, 0x3a, /* subs r2, r2, #0x01 */
508 0xf0, 0xd1, /* bne write_half_word */
510 0x00, 0xbe, /* bkpt #0x00 */
511 0x00, 0x80, 0x00, 0x40, /* EM357_FLASH_BASE: .word 0x40008000 */
514 /* flash write code */
515 if (target_alloc_working_area(target
, sizeof(em357_flash_write_code
),
516 &em357_info
->write_algorithm
) != ERROR_OK
)
518 LOG_WARNING("no working area available, can't do block memory writes");
519 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
522 if ((retval
= target_write_buffer(target
, em357_info
->write_algorithm
->address
,
523 sizeof(em357_flash_write_code
),
524 (uint8_t*)em357_flash_write_code
)) != ERROR_OK
)
528 while (target_alloc_working_area_try(target
, buffer_size
, &source
) != ERROR_OK
)
531 if (buffer_size
<= 256)
533 /* if we already allocated the writing code, but failed to get a
534 * buffer, free the algorithm */
535 if (em357_info
->write_algorithm
)
536 target_free_working_area(target
, em357_info
->write_algorithm
);
538 LOG_WARNING("no large enough working area available, can't do block memory writes");
539 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
543 armv7m_info
.common_magic
= ARMV7M_COMMON_MAGIC
;
544 armv7m_info
.core_mode
= ARMV7M_MODE_ANY
;
546 init_reg_param(®_params
[0], "r0", 32, PARAM_OUT
);
547 init_reg_param(®_params
[1], "r1", 32, PARAM_OUT
);
548 init_reg_param(®_params
[2], "r2", 32, PARAM_OUT
);
549 init_reg_param(®_params
[3], "r3", 32, PARAM_IN_OUT
);
553 uint32_t thisrun_count
= (count
> (buffer_size
/ 2)) ?
554 (buffer_size
/ 2) : count
;
556 if ((retval
= target_write_buffer(target
, source
->address
,
557 thisrun_count
* 2, buffer
)) != ERROR_OK
)
560 buf_set_u32(reg_params
[0].value
, 0, 32, source
->address
);
561 buf_set_u32(reg_params
[1].value
, 0, 32, address
);
562 buf_set_u32(reg_params
[2].value
, 0, 32, thisrun_count
);
563 buf_set_u32(reg_params
[3].value
, 0, 32, 0);
565 if ((retval
= target_run_algorithm(target
, 0, NULL
, 4, reg_params
,
566 em357_info
->write_algorithm
->address
,
568 10000, &armv7m_info
)) != ERROR_OK
)
570 LOG_ERROR("error executing em357 flash write algorithm");
574 if (buf_get_u32(reg_params
[3].value
, 0, 32) & FLASH_PGERR
)
576 LOG_ERROR("flash memory not erased before writing");
577 /* Clear but report errors */
578 target_write_u32(target
, EM357_FLASH_SR
, FLASH_PGERR
);
583 if (buf_get_u32(reg_params
[3].value
, 0, 32) & FLASH_WRPRTERR
)
585 LOG_ERROR("flash memory write protected");
586 /* Clear but report errors */
587 target_write_u32(target
, EM357_FLASH_SR
, FLASH_WRPRTERR
);
592 buffer
+= thisrun_count
* 2;
593 address
+= thisrun_count
* 2;
594 count
-= thisrun_count
;
597 target_free_working_area(target
, source
);
598 target_free_working_area(target
, em357_info
->write_algorithm
);
600 destroy_reg_param(®_params
[0]);
601 destroy_reg_param(®_params
[1]);
602 destroy_reg_param(®_params
[2]);
603 destroy_reg_param(®_params
[3]);
608 static int em357_write(struct flash_bank
*bank
, uint8_t *buffer
,
609 uint32_t offset
, uint32_t count
)
611 struct target
*target
= bank
->target
;
612 uint32_t words_remaining
= (count
/ 2);
613 uint32_t bytes_remaining
= (count
& 0x00000001);
614 uint32_t address
= bank
->base
+ offset
;
615 uint32_t bytes_written
= 0;
618 if (bank
->target
->state
!= TARGET_HALTED
)
620 LOG_ERROR("Target not halted");
621 return ERROR_TARGET_NOT_HALTED
;
626 LOG_WARNING("offset 0x%" PRIx32
" breaks required 2-byte alignment", offset
);
627 return ERROR_FLASH_DST_BREAKS_ALIGNMENT
;
630 /* unlock flash registers */
631 retval
= target_write_u32(target
, EM357_FLASH_KEYR
, KEY1
);
632 if (retval
!= ERROR_OK
)
634 retval
= target_write_u32(target
, EM357_FLASH_KEYR
, KEY2
);
635 if (retval
!= ERROR_OK
)
638 /* multiple half words (2-byte) to be programmed? */
639 if (words_remaining
> 0)
641 /* try using a block write */
642 if ((retval
= em357_write_block(bank
, buffer
, offset
, words_remaining
)) != ERROR_OK
)
644 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
646 /* if block write failed (no sufficient working area),
647 * we use normal (slow) single dword accesses */
648 LOG_WARNING("couldn't use block writes, falling back to single memory accesses");
653 buffer
+= words_remaining
* 2;
654 address
+= words_remaining
* 2;
659 if ((retval
!= ERROR_OK
) && (retval
!= ERROR_TARGET_RESOURCE_NOT_AVAILABLE
))
662 while (words_remaining
> 0)
665 memcpy(&value
, buffer
+ bytes_written
, sizeof(uint16_t));
667 retval
= target_write_u32(target
, EM357_FLASH_CR
, FLASH_PG
);
668 if (retval
!= ERROR_OK
)
670 retval
= target_write_u16(target
, address
, value
);
671 if (retval
!= ERROR_OK
)
674 retval
= em357_wait_status_busy(bank
, 5);
675 if (retval
!= ERROR_OK
)
685 uint16_t value
= 0xffff;
686 memcpy(&value
, buffer
+ bytes_written
, bytes_remaining
);
688 retval
= target_write_u32(target
, EM357_FLASH_CR
, FLASH_PG
);
689 if (retval
!= ERROR_OK
)
691 retval
= target_write_u16(target
, address
, value
);
692 if (retval
!= ERROR_OK
)
695 retval
= em357_wait_status_busy(bank
, 5);
696 if (retval
!= ERROR_OK
)
700 return target_write_u32(target
, EM357_FLASH_CR
, FLASH_LOCK
);
703 static int em357_probe(struct flash_bank
*bank
)
705 struct target
*target
= bank
->target
;
706 struct em357_flash_bank
*em357_info
= bank
->driver_priv
;
710 uint32_t base_address
= 0x08000000;
712 em357_info
->probed
= 0;
714 /* Enable FPEC CLK */
715 int retval
= target_write_u32(target
, EM357_FPEC_CLK
, 0x00000001);
716 if (retval
!= ERROR_OK
)
720 em357_info
->ppage_size
= 4;
723 LOG_INFO("flash size = %dkbytes", num_pages
*page_size
/1024);
728 bank
->sectors
= NULL
;
731 bank
->base
= base_address
;
732 bank
->size
= (num_pages
* page_size
);
733 bank
->num_sectors
= num_pages
;
734 bank
->sectors
= malloc(sizeof(struct flash_sector
) * num_pages
);
736 for (i
= 0; i
< num_pages
; i
++)
738 bank
->sectors
[i
].offset
= i
* page_size
;
739 bank
->sectors
[i
].size
= page_size
;
740 bank
->sectors
[i
].is_erased
= -1;
741 bank
->sectors
[i
].is_protected
= 1;
744 em357_info
->probed
= 1;
749 static int em357_auto_probe(struct flash_bank
*bank
)
751 struct em357_flash_bank
*em357_info
= bank
->driver_priv
;
752 if (em357_info
->probed
)
754 return em357_probe(bank
);
758 static int get_em357_info(struct flash_bank
*bank
, char *buf
, int buf_size
)
761 printed
= snprintf(buf
, buf_size
, "em357\n");
767 COMMAND_HANDLER(em357_handle_lock_command
)
769 struct target
*target
= NULL
;
770 struct em357_flash_bank
*em357_info
= NULL
;
774 command_print(CMD_CTX
, "em357 lock <bank>");
778 struct flash_bank
*bank
;
779 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
780 if (ERROR_OK
!= retval
)
783 em357_info
= bank
->driver_priv
;
785 target
= bank
->target
;
787 if (target
->state
!= TARGET_HALTED
)
789 LOG_ERROR("Target not halted");
790 return ERROR_TARGET_NOT_HALTED
;
793 if (em357_erase_options(bank
) != ERROR_OK
)
795 command_print(CMD_CTX
, "em357 failed to erase options");
799 /* set readout protection */
800 em357_info
->option_bytes
.RDP
= 0;
802 if (em357_write_options(bank
) != ERROR_OK
)
804 command_print(CMD_CTX
, "em357 failed to lock device");
808 command_print(CMD_CTX
, "em357 locked");
813 COMMAND_HANDLER(em357_handle_unlock_command
)
815 struct target
*target
= NULL
;
819 command_print(CMD_CTX
, "em357 unlock <bank>");
823 struct flash_bank
*bank
;
824 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
825 if (ERROR_OK
!= retval
)
828 target
= bank
->target
;
830 if (target
->state
!= TARGET_HALTED
)
832 LOG_ERROR("Target not halted");
833 return ERROR_TARGET_NOT_HALTED
;
836 if (em357_erase_options(bank
) != ERROR_OK
)
838 command_print(CMD_CTX
, "em357 failed to unlock device");
842 if (em357_write_options(bank
) != ERROR_OK
)
844 command_print(CMD_CTX
, "em357 failed to lock device");
848 command_print(CMD_CTX
, "em357 unlocked.\n"
849 "INFO: a reset or power cycle is required "
850 "for the new settings to take effect.");
855 static int em357_mass_erase(struct flash_bank
*bank
)
857 struct target
*target
= bank
->target
;
859 if (target
->state
!= TARGET_HALTED
)
861 LOG_ERROR("Target not halted");
862 return ERROR_TARGET_NOT_HALTED
;
865 /* unlock option flash registers */
866 int retval
= target_write_u32(target
, EM357_FLASH_KEYR
, KEY1
);
867 if (retval
!= ERROR_OK
)
869 retval
= target_write_u32(target
, EM357_FLASH_KEYR
, KEY2
);
870 if (retval
!= ERROR_OK
)
873 /* mass erase flash memory */
874 retval
= target_write_u32(target
, EM357_FLASH_CR
, FLASH_MER
);
875 if (retval
!= ERROR_OK
)
877 retval
= target_write_u32(target
, EM357_FLASH_CR
, FLASH_MER
| FLASH_STRT
);
878 if (retval
!= ERROR_OK
)
881 retval
= em357_wait_status_busy(bank
, 100);
882 if (retval
!= ERROR_OK
)
885 retval
= target_write_u32(target
, EM357_FLASH_CR
, FLASH_LOCK
);
886 if (retval
!= ERROR_OK
)
892 COMMAND_HANDLER(em357_handle_mass_erase_command
)
898 command_print(CMD_CTX
, "em357 mass_erase <bank>");
902 struct flash_bank
*bank
;
903 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
904 if (ERROR_OK
!= retval
)
907 retval
= em357_mass_erase(bank
);
908 if (retval
== ERROR_OK
)
910 /* set all sectors as erased */
911 for (i
= 0; i
< bank
->num_sectors
; i
++)
913 bank
->sectors
[i
].is_erased
= 1;
916 command_print(CMD_CTX
, "em357 mass erase complete");
920 command_print(CMD_CTX
, "em357 mass erase failed");
926 static const struct command_registration em357_exec_command_handlers
[] = {
929 .handler
= em357_handle_lock_command
,
930 .mode
= COMMAND_EXEC
,
932 .help
= "Lock entire flash device.",
936 .handler
= em357_handle_unlock_command
,
937 .mode
= COMMAND_EXEC
,
939 .help
= "Unlock entire protected flash device.",
942 .name
= "mass_erase",
943 .handler
= em357_handle_mass_erase_command
,
944 .mode
= COMMAND_EXEC
,
946 .help
= "Erase entire flash device.",
948 COMMAND_REGISTRATION_DONE
951 static const struct command_registration em357_command_handlers
[] = {
955 .help
= "em357 flash command group",
956 .chain
= em357_exec_command_handlers
,
958 COMMAND_REGISTRATION_DONE
961 struct flash_driver em357_flash
= {
963 .commands
= em357_command_handlers
,
964 .flash_bank_command
= em357_flash_bank_command
,
965 .erase
= em357_erase
,
966 .protect
= em357_protect
,
967 .write
= em357_write
,
968 .read
= default_flash_read
,
969 .probe
= em357_probe
,
970 .auto_probe
= em357_auto_probe
,
971 .erase_check
= default_flash_mem_blank_check
,
972 .protect_check
= em357_protect_check
,
973 .info
= get_em357_info
,