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) 2008 by John McCarthy *
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 ***************************************************************************/
32 #include <target/mips32.h>
36 struct pic32mx_devs_s
{
41 { 0x78, "460F512L USB", 512 },
42 { 0x74, "460F256L USB", 256 },
43 { 0x6D, "440F128L USB", 128 },
44 { 0x56, "440F512H USB", 512 },
45 { 0x52, "440F256H USB", 256 },
46 { 0x4D, "440F128H USB", 128 },
47 { 0x42, "420F032H USB", 32 },
48 { 0x38, "360F512L", 512 },
49 { 0x34, "360F256L", 256 },
50 { 0x2D, "340F128L", 128 },
51 { 0x2A, "320F128L", 128 },
52 { 0x16, "340F512H", 512 },
53 { 0x12, "340F256H", 256 },
54 { 0x0D, "340F128H", 128 },
55 { 0x0A, "320F128H", 128 },
56 { 0x06, "320F064H", 64 },
57 { 0x02, "320F032H", 32 },
61 static int pic32mx_write_row(struct flash_bank
*bank
, uint32_t address
, uint32_t srcaddr
);
62 static int pic32mx_write_word(struct flash_bank
*bank
, uint32_t address
, uint32_t word
);
64 /* flash bank pic32mx <base> <size> 0 0 <target#>
66 FLASH_BANK_COMMAND_HANDLER(pic32mx_flash_bank_command
)
68 struct pic32mx_flash_bank
*pic32mx_info
;
72 LOG_WARNING("incomplete flash_bank pic32mx configuration");
73 return ERROR_FLASH_BANK_INVALID
;
76 pic32mx_info
= malloc(sizeof(struct pic32mx_flash_bank
));
78 struct flash_bank
*bank
= flash_bank_from_object(object
);
79 set_flash_bank_data(bank
, pic32mx_info
);
81 pic32mx_info
->write_algorithm
= NULL
;
82 pic32mx_info
->probed
= 0;
87 static uint32_t pic32mx_get_flash_status(struct flash_bank
*bank
)
89 struct target
*target
= bank
->target
;
92 target_read_u32(target
, PIC32MX_NVMCON
, &status
);
97 static uint32_t pic32mx_wait_status_busy(struct flash_bank
*bank
, int timeout
)
101 /* wait for busy to clear */
102 while (((status
= pic32mx_get_flash_status(bank
)) & NVMCON_NVMWR
) && (timeout
-- > 0))
104 LOG_DEBUG("status: 0x%" PRIx32
, status
);
108 LOG_DEBUG("timeout: status: 0x%" PRIx32
, status
);
113 static int pic32mx_nvm_exec(struct flash_bank
*bank
, uint32_t op
, uint32_t timeout
)
115 struct target
*target
= bank
->target
;
118 target_write_u32(target
, PIC32MX_NVMCON
, NVMCON_NVMWREN
| op
);
120 /* unlock flash registers */
121 target_write_u32(target
, PIC32MX_NVMKEY
, NVMKEY1
);
122 target_write_u32(target
, PIC32MX_NVMKEY
, NVMKEY2
);
124 /* start operation */
125 target_write_u32(target
, PIC32MX_NVMCONSET
, NVMCON_NVMWR
);
127 status
= pic32mx_wait_status_busy(bank
, timeout
);
129 /* lock flash registers */
130 target_write_u32(target
, PIC32MX_NVMCONCLR
, NVMCON_NVMWREN
);
135 static int pic32mx_protect_check(struct flash_bank
*bank
)
137 struct target
*target
= bank
->target
;
143 if (target
->state
!= TARGET_HALTED
)
145 LOG_ERROR("Target not halted");
146 return ERROR_TARGET_NOT_HALTED
;
149 target_read_u32(target
, PIC32MX_DEVCFG0
, &devcfg0
);
150 if ((devcfg0
& (1 << 28)) == 0) /* code protect bit */
151 num_pages
= 0xffff; /* All pages protected */
152 else if (bank
->base
== PIC32MX_KSEG1_BOOT_FLASH
)
154 if (devcfg0
& (1 << 24))
155 num_pages
= 0; /* All pages unprotected */
157 num_pages
= 0xffff; /* All pages protected */
160 num_pages
= (~devcfg0
>> 12) & 0xff;
161 for (s
= 0; s
< bank
->num_sectors
&& s
< num_pages
; s
++)
162 bank
->sectors
[s
].is_protected
= 1;
163 for (; s
< bank
->num_sectors
; s
++)
164 bank
->sectors
[s
].is_protected
= 0;
169 static int pic32mx_erase(struct flash_bank
*bank
, int first
, int last
)
171 struct target
*target
= bank
->target
;
175 if (bank
->target
->state
!= TARGET_HALTED
)
177 LOG_ERROR("Target not halted");
178 return ERROR_TARGET_NOT_HALTED
;
181 if ((first
== 0) && (last
== (bank
->num_sectors
- 1)) && (bank
->base
== PIC32MX_KSEG0_PGM_FLASH
|| bank
->base
== PIC32MX_KSEG1_PGM_FLASH
))
183 LOG_DEBUG("Erasing entire program flash");
184 status
= pic32mx_nvm_exec(bank
, NVMCON_OP_PFM_ERASE
, 50);
185 if (status
& NVMCON_NVMERR
)
186 return ERROR_FLASH_OPERATION_FAILED
;
187 if (status
& NVMCON_LVDERR
)
188 return ERROR_FLASH_OPERATION_FAILED
;
192 for (i
= first
; i
<= last
; i
++)
194 if (bank
->base
>= PIC32MX_KSEG1_PGM_FLASH
)
195 target_write_u32(target
, PIC32MX_NVMADDR
, KS1Virt2Phys(bank
->base
+ bank
->sectors
[i
].offset
));
197 target_write_u32(target
, PIC32MX_NVMADDR
, KS0Virt2Phys(bank
->base
+ bank
->sectors
[i
].offset
));
199 status
= pic32mx_nvm_exec(bank
, NVMCON_OP_PAGE_ERASE
, 10);
201 if (status
& NVMCON_NVMERR
)
202 return ERROR_FLASH_OPERATION_FAILED
;
203 if (status
& NVMCON_LVDERR
)
204 return ERROR_FLASH_OPERATION_FAILED
;
205 bank
->sectors
[i
].is_erased
= 1;
211 static struct pic32mx_flash_bank
*pic32mx_bank_data(struct flash_bank
*bank
)
213 return (struct pic32mx_flash_bank
*)flash_bank_data(bank
);
216 static int pic32mx_protect(struct flash_bank
*bank
, int set
, int first
, int last
)
218 struct pic32mx_flash_bank
*pic32mx_info
= NULL
;
219 struct target
*target
= bank
->target
;
221 uint16_t prot_reg
[4] = {0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF};
227 pic32mx_info
= pic32mx_bank_data(bank
);
229 if (target
->state
!= TARGET_HALTED
)
231 LOG_ERROR("Target not halted");
232 return ERROR_TARGET_NOT_HALTED
;
236 if ((first
&& (first
% pic32mx_info
->ppage_size
)) || ((last
+ 1) && (last
+ 1) % pic32mx_info
->ppage_size
))
238 LOG_WARNING("sector start/end incorrect - stm32 has %dK sector protection", pic32mx_info
->ppage_size
);
239 return ERROR_FLASH_SECTOR_INVALID
;
242 /* medium density - each bit refers to a 4bank protection
243 * high density - each bit refers to a 2bank protection */
244 target_read_u32(target
, PIC32MX_FLASH_WRPR
, &protection
);
246 prot_reg
[0] = (uint16_t)protection
;
247 prot_reg
[1] = (uint16_t)(protection
>> 8);
248 prot_reg
[2] = (uint16_t)(protection
>> 16);
249 prot_reg
[3] = (uint16_t)(protection
>> 24);
251 if (pic32mx_info
->ppage_size
== 2)
253 /* high density flash */
255 /* bit 7 controls sector 62 - 255 protection */
259 prot_reg
[3] &= ~(1 << 7);
261 prot_reg
[3] |= (1 << 7);
269 for (i
= first
; i
<= last
; i
++)
271 reg
= (i
/ pic32mx_info
->ppage_size
) / 8;
272 bit
= (i
/ pic32mx_info
->ppage_size
) - (reg
* 8);
275 prot_reg
[reg
] &= ~(1 << bit
);
277 prot_reg
[reg
] |= (1 << bit
);
282 /* medium density flash */
283 for (i
= first
; i
<= last
; i
++)
285 reg
= (i
/ pic32mx_info
->ppage_size
) / 8;
286 bit
= (i
/ pic32mx_info
->ppage_size
) - (reg
* 8);
289 prot_reg
[reg
] &= ~(1 << bit
);
291 prot_reg
[reg
] |= (1 << bit
);
295 if ((status
= pic32mx_erase_options(bank
)) != ERROR_OK
)
298 pic32mx_info
->option_bytes
.protection
[0] = prot_reg
[0];
299 pic32mx_info
->option_bytes
.protection
[1] = prot_reg
[1];
300 pic32mx_info
->option_bytes
.protection
[2] = prot_reg
[2];
301 pic32mx_info
->option_bytes
.protection
[3] = prot_reg
[3];
303 return pic32mx_write_options(bank
);
309 static int pic32mx_write_block(struct flash_bank
*bank
, uint8_t *buffer
, uint32_t offset
, uint32_t count
)
311 struct target
*target
= bank
->target
;
312 uint32_t buffer_size
= 512;
313 struct working_area
*source
;
314 uint32_t address
= bank
->base
+ offset
;
315 int retval
= ERROR_OK
;
317 struct pic32mx_flash_bank
*pic32mx_info
= pic32mx_bank_data(bank
);
318 struct armv7m_algorithm armv7m_info
;
320 uint8_t pic32mx_flash_write_code
[] = {
322 0xDF, 0xF8, 0x24, 0x40, /* ldr r4, PIC32MX_FLASH_CR */
323 0x09, 0x4D, /* ldr r5, PIC32MX_FLASH_SR */
324 0x4F, 0xF0, 0x01, 0x03, /* mov r3, #1 */
325 0x23, 0x60, /* str r3, [r4, #0] */
326 0x30, 0xF8, 0x02, 0x3B, /* ldrh r3, [r0], #2 */
327 0x21, 0xF8, 0x02, 0x3B, /* strh r3, [r1], #2 */
329 0x2B, 0x68, /* ldr r3, [r5, #0] */
330 0x13, 0xF0, 0x01, 0x0F, /* tst r3, #0x01 */
331 0xFB, 0xD0, /* beq busy */
332 0x13, 0xF0, 0x14, 0x0F, /* tst r3, #0x14 */
333 0x01, 0xD1, /* bne exit */
334 0x01, 0x3A, /* subs r2, r2, #1 */
335 0xED, 0xD1, /* bne write */
337 0xFE, 0xE7, /* b exit */
338 0x10, 0x20, 0x02, 0x40, /* PIC32MX_FLASH_CR: .word 0x40022010 */
339 0x0C, 0x20, 0x02, 0x40 /* PIC32MX_FLASH_SR: .word 0x4002200C */
342 /* flash write code */
343 if (target_alloc_working_area(target
, sizeof(pic32mx_flash_write_code
), &pic32mx_info
->write_algorithm
) != ERROR_OK
)
345 LOG_WARNING("no working area available, can't do block memory writes");
346 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
349 if ((retval
= target_write_buffer(target
, pic32mx_info
->write_algorithm
->address
, sizeof(pic32mx_flash_write_code
), pic32mx_flash_write_code
)) != ERROR_OK
)
354 if (target_alloc_working_area(target
, buffer_size
, &source
) != ERROR_OK
)
357 /* if we already allocated the writing code, but failed to get a buffer, free the algorithm */
358 if (pic32mx_info
->write_algorithm
)
359 target_free_working_area(target
, pic32mx_info
->write_algorithm
);
362 LOG_WARNING("no large enough working area available, can't do block memory writes");
363 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
366 while (count
>= buffer_size
/4)
370 if ((retval
= target_write_buffer(target
, source
->address
, buffer_size
, buffer
)) != ERROR_OK
) {
371 LOG_ERROR("Failed to write row buffer (%d words) to RAM", (int)(buffer_size
/4));
376 buf_set_u32(reg_params
[0].value
, 0, 32, source
->address
);
377 buf_set_u32(reg_params
[1].value
, 0, 32, address
);
378 buf_set_u32(reg_params
[2].value
, 0, 32, buffer_size
/4);
380 if ((retval
= target_run_algorithm(target
, 0, NULL
, 4, reg_params
, pic32mx_info
->write_algorithm
->address
, \
381 pic32mx_info
->write_algorithm
->address
+ (sizeof(pic32mx_flash_write_code
) - 10), 10000, &armv7m_info
)) != ERROR_OK
)
383 LOG_ERROR("error executing pic32mx flash write algorithm");
384 retval
= ERROR_FLASH_OPERATION_FAILED
;
388 if (buf_get_u32(reg_params
[3].value
, 0, 32) & 0x14)
390 retval
= ERROR_FLASH_OPERATION_FAILED
;
394 status
= pic32mx_write_row(bank
, address
, source
->address
);
395 if (status
& NVMCON_NVMERR
) {
396 LOG_ERROR("Flash write error NVMERR (status = 0x%08" PRIx32
")", status
);
397 retval
= ERROR_FLASH_OPERATION_FAILED
;
400 if (status
& NVMCON_LVDERR
) {
401 LOG_ERROR("Flash write error LVDERR (status = 0x%08" PRIx32
")", status
);
402 retval
= ERROR_FLASH_OPERATION_FAILED
;
406 buffer
+= buffer_size
;
407 address
+= buffer_size
;
408 count
-= buffer_size
/4;
411 target_free_working_area(target
, source
);
416 memcpy(&value
, buffer
, sizeof(uint32_t));
418 uint32_t status
= pic32mx_write_word(bank
, address
, value
);
419 if (status
& NVMCON_NVMERR
) {
420 LOG_ERROR("Flash write error NVMERR (status = 0x%08" PRIx32
")", status
);
421 retval
= ERROR_FLASH_OPERATION_FAILED
;
424 if (status
& NVMCON_LVDERR
) {
425 LOG_ERROR("Flash write error LVDERR (status = 0x%08" PRIx32
")", status
);
426 retval
= ERROR_FLASH_OPERATION_FAILED
;
438 static int pic32mx_write_word(struct flash_bank
*bank
, uint32_t address
, uint32_t word
)
440 struct target
*target
= bank
->target
;
442 if (bank
->base
>= PIC32MX_KSEG1_PGM_FLASH
)
443 target_write_u32(target
, PIC32MX_NVMADDR
, KS1Virt2Phys(address
));
445 target_write_u32(target
, PIC32MX_NVMADDR
, KS0Virt2Phys(address
));
446 target_write_u32(target
, PIC32MX_NVMDATA
, word
);
448 return pic32mx_nvm_exec(bank
, NVMCON_OP_WORD_PROG
, 5);
452 * Write a 128 word (512 byte) row to flash address from RAM srcaddr.
454 static int pic32mx_write_row(struct flash_bank
*bank
, uint32_t address
, uint32_t srcaddr
)
456 struct target
*target
= bank
->target
;
458 LOG_DEBUG("addr: 0x%08" PRIx32
" srcaddr: 0x%08" PRIx32
"", address
, srcaddr
);
460 if (address
>= PIC32MX_KSEG1_PGM_FLASH
)
461 target_write_u32(target
, PIC32MX_NVMADDR
, KS1Virt2Phys(address
));
463 target_write_u32(target
, PIC32MX_NVMADDR
, KS0Virt2Phys(address
));
464 if (srcaddr
>= PIC32MX_KSEG1_RAM
)
465 target_write_u32(target
, PIC32MX_NVMSRCADDR
, KS1Virt2Phys(srcaddr
));
467 target_write_u32(target
, PIC32MX_NVMSRCADDR
, KS0Virt2Phys(srcaddr
));
469 return pic32mx_nvm_exec(bank
, NVMCON_OP_ROW_PROG
, 100);
472 static int pic32mx_write(struct flash_bank
*bank
, uint8_t *buffer
, uint32_t offset
, uint32_t count
)
474 uint32_t words_remaining
= (count
/ 4);
475 uint32_t bytes_remaining
= (count
& 0x00000003);
476 uint32_t address
= bank
->base
+ offset
;
477 uint32_t bytes_written
= 0;
481 if (bank
->target
->state
!= TARGET_HALTED
)
483 LOG_ERROR("Target not halted");
484 return ERROR_TARGET_NOT_HALTED
;
489 LOG_WARNING("offset 0x%" PRIx32
"breaks required 4-byte alignment", offset
);
490 return ERROR_FLASH_DST_BREAKS_ALIGNMENT
;
493 /* multiple words (4-byte) to be programmed? */
494 if (words_remaining
> 0)
496 /* try using a block write */
497 if ((retval
= pic32mx_write_block(bank
, buffer
, offset
, words_remaining
)) != ERROR_OK
)
499 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
501 /* if block write failed (no sufficient working area),
502 * we use normal (slow) single dword accesses */
503 LOG_WARNING("couldn't use block writes, falling back to single memory accesses");
505 else if (retval
== ERROR_FLASH_OPERATION_FAILED
)
507 LOG_ERROR("flash writing failed with error code: 0x%x", retval
);
508 return ERROR_FLASH_OPERATION_FAILED
;
513 buffer
+= words_remaining
* 4;
514 address
+= words_remaining
* 4;
519 while (words_remaining
> 0)
522 memcpy(&value
, buffer
+ bytes_written
, sizeof(uint32_t));
524 status
= pic32mx_write_word(bank
, address
, value
);
525 if (status
& NVMCON_NVMERR
)
526 return ERROR_FLASH_OPERATION_FAILED
;
527 if (status
& NVMCON_LVDERR
)
528 return ERROR_FLASH_OPERATION_FAILED
;
537 uint32_t value
= 0xffffffff;
538 memcpy(&value
, buffer
+ bytes_written
, bytes_remaining
);
540 status
= pic32mx_write_word(bank
, address
, value
);
541 if (status
& NVMCON_NVMERR
)
542 return ERROR_FLASH_OPERATION_FAILED
;
543 if (status
& NVMCON_LVDERR
)
544 return ERROR_FLASH_OPERATION_FAILED
;
550 static int pic32mx_probe(struct flash_bank
*bank
)
552 struct target
*target
= bank
->target
;
553 struct pic32mx_flash_bank
*pic32mx_info
= pic32mx_bank_data(bank
);
554 struct mips32_common
*mips32
= target
->arch_info
;
555 struct mips_ejtag
*ejtag_info
= &mips32
->ejtag_info
;
557 uint16_t num_pages
= 0;
561 pic32mx_info
->probed
= 0;
563 device_id
= ejtag_info
->idcode
;
564 LOG_INFO("device id = 0x%08" PRIx32
" (manuf 0x%03x dev 0x%02x, ver 0x%03x)",
566 (unsigned)((device_id
>> 1)&0x7ff),
567 (unsigned)((device_id
>> 12)&0xff),
568 (unsigned)((device_id
>> 20)&0xfff));
570 if (((device_id
>> 1)&0x7ff) != PIC32MX_MANUF_ID
) {
571 LOG_WARNING("Cannot identify target as a PIC32MX family.");
572 return ERROR_FLASH_OPERATION_FAILED
;
576 if (bank
->base
== PIC32MX_KSEG1_BOOT_FLASH
|| bank
->base
== 1) {
577 /* 0xBFC00000: Boot flash size fixed at 12k */
580 /* 0xBD000000: Program flash size varies with device */
581 for (i
= 0; pic32mx_devs
[i
].name
!= NULL
; i
++)
582 if (pic32mx_devs
[i
].devid
== ((device_id
>> 12) & 0xff)) {
583 num_pages
= pic32mx_devs
[i
].pfm_size
;
586 if (pic32mx_devs
[i
].name
== NULL
) {
587 LOG_WARNING("Cannot identify target as a PIC32MX family.");
588 return ERROR_FLASH_OPERATION_FAILED
;
593 if (bank
->target
->state
!= TARGET_HALTED
)
595 LOG_ERROR("Target not halted");
596 return ERROR_TARGET_NOT_HALTED
;
599 /* get flash size from target */
600 if (target_read_u16(target
, 0x1FFFF7E0, &num_pages
) != ERROR_OK
)
602 /* failed reading flash size, default to max target family */
607 LOG_INFO("flash size = %dkbytes", num_pages
);
609 /* calculate numbers of pages */
610 num_pages
/= (page_size
/ 1024);
612 if (bank
->base
== 0) bank
->base
= PIC32MX_KSEG1_PGM_FLASH
;
613 if (bank
->base
== 1) bank
->base
= PIC32MX_KSEG1_BOOT_FLASH
;
614 bank
->size
= (num_pages
* page_size
);
615 bank
->num_sectors
= num_pages
;
616 bank
->chip_width
= 4;
618 bank
->sectors
= malloc(sizeof(struct flash_sector
) * num_pages
);
620 for (i
= 0; i
< num_pages
; i
++)
622 bank
->sectors
[i
].offset
= i
* page_size
;
623 bank
->sectors
[i
].size
= page_size
;
624 bank
->sectors
[i
].is_erased
= -1;
625 bank
->sectors
[i
].is_protected
= 1;
628 pic32mx_info
->probed
= 1;
633 static int pic32mx_auto_probe(struct flash_bank
*bank
)
635 struct pic32mx_flash_bank
*pic32mx_info
= pic32mx_bank_data(bank
);
636 if (pic32mx_info
->probed
)
638 return pic32mx_probe(bank
);
642 COMMAND_HANDLER(pic32mx_handle_part_id_command
)
648 static int pic32mx_info(struct flash_bank
*bank
, char *buf
, int buf_size
)
650 struct target
*target
= bank
->target
;
651 struct mips32_common
*mips32
= target
->arch_info
;
652 struct mips_ejtag
*ejtag_info
= &mips32
->ejtag_info
;
656 device_id
= ejtag_info
->idcode
;
658 if (((device_id
>> 1)&0x7ff) != PIC32MX_MANUF_ID
) {
659 snprintf(buf
, buf_size
,
660 "Cannot identify target as a PIC32MX family (manufacturer 0x%03d != 0x%03d)\n",
661 (unsigned)((device_id
>> 1)&0x7ff),
663 return ERROR_FLASH_OPERATION_FAILED
;
665 for (i
= 0; pic32mx_devs
[i
].name
!= NULL
; i
++)
666 if (pic32mx_devs
[i
].devid
== ((device_id
>> 12) & 0xff)) {
667 printed
= snprintf(buf
, buf_size
, "PIC32MX%s", pic32mx_devs
[i
].name
);
670 if (pic32mx_devs
[i
].name
== NULL
) {
671 snprintf(buf
, buf_size
, "Cannot identify target as a PIC32MX family\n");
672 return ERROR_FLASH_OPERATION_FAILED
;
676 printed
= snprintf(buf
, buf_size
, " Ver: 0x%03x",
677 (unsigned)((device_id
>> 20)&0xfff));
683 COMMAND_HANDLER(pic32mx_handle_lock_command
)
685 struct target
*target
= NULL
;
686 struct pic32mx_flash_bank
*pic32mx_info
= NULL
;
690 command_print(CMD_CTX
, "pic32mx lock <bank>");
694 struct flash_bank
*bank
;
695 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
696 if (ERROR_OK
!= retval
)
699 pic32mx_info
= pic32mx_bank_data(bank
);
701 target
= bank
->target
;
703 if (target
->state
!= TARGET_HALTED
)
705 LOG_ERROR("Target not halted");
706 return ERROR_TARGET_NOT_HALTED
;
709 if (pic32mx_erase_options(bank
) != ERROR_OK
)
711 command_print(CMD_CTX
, "pic32mx failed to erase options");
715 /* set readout protection */
716 pic32mx_info
->option_bytes
.RDP
= 0;
718 if (pic32mx_write_options(bank
) != ERROR_OK
)
720 command_print(CMD_CTX
, "pic32mx failed to lock device");
724 command_print(CMD_CTX
, "pic32mx locked");
729 COMMAND_HANDLER(pic32mx_handle_unlock_command
)
731 struct target
*target
= NULL
;
732 struct pic32mx_flash_bank
*pic32mx_info
= NULL
;
736 command_print(CMD_CTX
, "pic32mx unlock <bank>");
740 struct flash_bank
*bank
;
741 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
742 if (ERROR_OK
!= retval
)
745 pic32mx_info
= pic32mx_bank_data(bank
);
747 target
= bank
->target
;
749 if (target
->state
!= TARGET_HALTED
)
751 LOG_ERROR("Target not halted");
752 return ERROR_TARGET_NOT_HALTED
;
755 if (pic32mx_erase_options(bank
) != ERROR_OK
)
757 command_print(CMD_CTX
, "pic32mx failed to unlock device");
761 if (pic32mx_write_options(bank
) != ERROR_OK
)
763 command_print(CMD_CTX
, "pic32mx failed to lock device");
767 command_print(CMD_CTX
, "pic32mx unlocked");
774 static int pic32mx_chip_erase(struct flash_bank
*bank
)
776 struct target
*target
= bank
->target
;
781 if (target
->state
!= TARGET_HALTED
)
783 LOG_ERROR("Target not halted");
784 return ERROR_TARGET_NOT_HALTED
;
787 LOG_INFO("PIC32MX chip erase called");
790 /* unlock option flash registers */
791 target_write_u32(target
, PIC32MX_FLASH_KEYR
, KEY1
);
792 target_write_u32(target
, PIC32MX_FLASH_KEYR
, KEY2
);
794 /* chip erase flash memory */
795 target_write_u32(target
, PIC32MX_FLASH_CR
, FLASH_MER
);
796 target_write_u32(target
, PIC32MX_FLASH_CR
, FLASH_MER
| FLASH_STRT
);
798 status
= pic32mx_wait_status_busy(bank
, 10);
800 target_write_u32(target
, PIC32MX_FLASH_CR
, FLASH_LOCK
);
802 if (status
& FLASH_WRPRTERR
)
804 LOG_ERROR("pic32mx device protected");
808 if (status
& FLASH_PGERR
)
810 LOG_ERROR("pic32mx device programming failed");
819 COMMAND_HANDLER(pic32mx_handle_chip_erase_command
)
826 command_print(CMD_CTX
, "pic32mx chip_erase");
830 struct flash_bank
*bank
;
831 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
832 if (ERROR_OK
!= retval
)
835 if (pic32mx_chip_erase(bank
) == ERROR_OK
)
837 /* set all sectors as erased */
838 for (i
= 0; i
< bank
->num_sectors
; i
++)
840 bank
->sectors
[i
].is_erased
= 1;
843 command_print(CMD_CTX
, "pic32mx chip erase complete");
847 command_print(CMD_CTX
, "pic32mx chip erase failed");
854 COMMAND_HANDLER(pic32mx_handle_pgm_word_command
)
856 uint32_t address
, value
;
861 command_print(CMD_CTX
, "pic32mx pgm_word <addr> <value> <bank>");
865 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[0], address
);
866 COMMAND_PARSE_NUMBER(u32
, CMD_ARGV
[1], value
);
868 struct flash_bank
*bank
;
869 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 2, &bank
);
870 if (ERROR_OK
!= retval
)
873 if (address
< bank
->base
|| address
>= (bank
->base
+ bank
->size
))
875 command_print(CMD_CTX
, "flash address '%s' is out of bounds", CMD_ARGV
[0]);
880 status
= pic32mx_write_word(bank
, address
, value
);
881 if (status
& NVMCON_NVMERR
)
882 res
= ERROR_FLASH_OPERATION_FAILED
;
883 if (status
& NVMCON_LVDERR
)
884 res
= ERROR_FLASH_OPERATION_FAILED
;
887 command_print(CMD_CTX
, "pic32mx pgm word complete");
889 command_print(CMD_CTX
, "pic32mx pgm word failed (status = 0x%x)", status
);
893 static const struct command_registration pic32mx_exec_command_handlers
[] = {
895 .name
= "chip_erase",
896 .handler
= &pic32mx_handle_chip_erase_command
,
897 .mode
= COMMAND_EXEC
,
898 .help
= "erase device",
902 .handler
= &pic32mx_handle_pgm_word_command
,
903 .mode
= COMMAND_EXEC
,
904 .help
= "program a word",
906 COMMAND_REGISTRATION_DONE
908 static const struct command_registration pic32mx_command_handlers
[] = {
912 .help
= "pic32mx flash command group",
913 .chain
= pic32mx_exec_command_handlers
,
915 COMMAND_REGISTRATION_DONE
918 FLASH_DRIVER(pic32mx
, &pic32mx_flash_bank_command
, pic32mx_command_handlers
,
919 .erase
= &pic32mx_erase
,
920 .protect
= &pic32mx_protect
,
921 .write
= &pic32mx_write
,
922 .probe
= &pic32mx_probe
,
923 .auto_probe
= &pic32mx_auto_probe
,
924 .erase_check
= &default_flash_mem_blank_check
,
925 .protect_check
= &pic32mx_protect_check
,
926 .info
= &pic32mx_info
,