1 /***************************************************************************
2 * Copyright (C) 2006 by Magnus Lundin *
5 * Copyright (C) 2008 by Spencer Oliver *
6 * spen@spen-soft.co.uk *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
13 * This program is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
18 * You should have received a copy of the GNU General Public License *
19 * along with this program; if not, write to the *
20 * Free Software Foundation, Inc., *
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22 ***************************************************************************/
24 /***************************************************************************
25 * STELLARIS is tested on LM3S811, LM3S6965
26 ***************************************************************************/
32 #include "stellaris.h"
33 #include <helper/binarybuffer.h>
34 #include <target/algorithm.h>
35 #include <target/armv7m.h>
38 #define DID0_VER(did0) ((did0 >> 28)&0x07)
40 static int stellaris_read_part_info(struct flash_bank
*bank
);
41 static uint32_t stellaris_get_flash_status(struct flash_bank
*bank
);
42 static void stellaris_set_flash_mode(struct flash_bank
*bank
,int mode
);
43 //static uint32_t stellaris_wait_status_busy(struct flash_bank *bank, uint32_t waitbits, int timeout);
45 static int stellaris_mass_erase(struct flash_bank
*bank
);
84 /*{0x33,"LM3S2616"},*/
203 static char * StellarisClassname
[5] =
212 /***************************************************************************
213 * openocd command interface *
214 ***************************************************************************/
216 /* flash_bank stellaris <base> <size> 0 0 <target#>
218 FLASH_BANK_COMMAND_HANDLER(stellaris_flash_bank_command
)
220 struct stellaris_flash_bank
*stellaris_info
;
224 LOG_WARNING("incomplete flash_bank stellaris configuration");
225 return ERROR_FLASH_BANK_INVALID
;
228 stellaris_info
= calloc(sizeof(struct stellaris_flash_bank
), 1);
230 struct flash_bank
*bank
= flash_bank_from_object(object
);
232 set_flash_bank_data(bank
, stellaris_info
);
234 stellaris_info
->target_name
= "Unknown target";
236 /* part wasn't probed for info yet */
237 stellaris_info
->did1
= 0;
239 /* TODO Specify the main crystal speed in kHz using an optional
240 * argument; ditto, the speed of an external oscillator used
241 * instead of a crystal. Avoid programming flash using IOSC.
246 static struct stellaris_flash_bank
*stellaris_bank_data(struct flash_bank
*bank
)
248 return (struct stellaris_flash_bank
*)flash_bank_data(bank
);
251 static int stellaris_info(struct flash_bank
*bank
, char *buf
, int buf_size
)
253 int printed
, device_class
;
254 struct stellaris_flash_bank
*stellaris_info
= stellaris_bank_data(bank
);
256 stellaris_read_part_info(bank
);
258 if (stellaris_info
->did1
== 0)
260 printed
= snprintf(buf
, buf_size
, "Cannot identify target as a Stellaris\n");
263 return ERROR_FLASH_OPERATION_FAILED
;
266 if (DID0_VER(stellaris_info
->did0
) > 0)
268 device_class
= (stellaris_info
->did0
>> 16) & 0xFF;
274 printed
= snprintf(buf
,
276 "\nTI/LMI Stellaris information: Chip is "
277 "class %i (%s) %s rev %c%i\n",
279 StellarisClassname
[device_class
],
280 stellaris_info
->target_name
,
281 (int)('A' + ((stellaris_info
->did0
>> 8) & 0xFF)),
282 (int)((stellaris_info
->did0
) & 0xFF));
286 printed
= snprintf(buf
,
288 "did1: 0x%8.8" PRIx32
", arch: 0x%4.4" PRIx32
289 ", eproc: %s, ramsize: %ik, flashsize: %ik\n",
290 stellaris_info
->did1
,
291 stellaris_info
->did1
,
293 (int)((1 + ((stellaris_info
->dc0
>> 16) & 0xFFFF))/4),
294 (int)((1 + (stellaris_info
->dc0
& 0xFFFF))*2));
298 printed
= snprintf(buf
,
300 "master clock: %ikHz%s, "
301 "rcc is 0x%" PRIx32
", rcc2 is 0x%" PRIx32
"\n",
302 (int)(stellaris_info
->mck_freq
/ 1000),
303 stellaris_info
->mck_desc
,
305 stellaris_info
->rcc2
);
309 if (stellaris_info
->num_lockbits
> 0)
311 printed
= snprintf(buf
,
313 "pagesize: %" PRIi32
", lockbits: %i 0x%4.4" PRIx32
", pages in lock region: %i \n",
314 stellaris_info
->pagesize
,
315 stellaris_info
->num_lockbits
,
316 stellaris_info
->lockbits
,
317 (int)(stellaris_info
->num_pages
/stellaris_info
->num_lockbits
));
324 /***************************************************************************
325 * chip identification and status *
326 ***************************************************************************/
328 static uint32_t stellaris_get_flash_status(struct flash_bank
*bank
)
330 struct target
*target
= bank
->target
;
333 target_read_u32(target
, FLASH_CONTROL_BASE
| FLASH_FMC
, &fmc
);
338 /** Read clock configuration and set stellaris_info->usec_clocks*/
340 static const unsigned rcc_xtal
[32] = {
341 [0x00] = 1000000, /* no pll */
342 [0x01] = 1843200, /* no pll */
343 [0x02] = 2000000, /* no pll */
344 [0x03] = 2457600, /* no pll */
348 [0x06] = 4000000, /* usb */
352 [0x09] = 5000000, /* usb */
354 [0x0b] = 6000000, /* (reset) usb */
358 [0x0e] = 8000000, /* usb */
361 /* parts before DustDevil use just 4 bits for xtal spec */
363 [0x10] = 10000000, /* usb */
364 [0x11] = 12000000, /* usb */
369 [0x15] = 16000000, /* usb */
373 static void stellaris_read_clock_info(struct flash_bank
*bank
)
375 struct stellaris_flash_bank
*stellaris_info
= stellaris_bank_data(bank
);
376 struct target
*target
= bank
->target
;
377 uint32_t rcc
, rcc2
, pllcfg
, sysdiv
, usesysdiv
, bypass
, oscsrc
;
379 unsigned long mainfreq
;
381 target_read_u32(target
, SCB_BASE
| RCC
, &rcc
);
382 LOG_DEBUG("Stellaris RCC %" PRIx32
"", rcc
);
384 target_read_u32(target
, SCB_BASE
| RCC2
, &rcc2
);
385 LOG_DEBUG("Stellaris RCC2 %" PRIx32
"", rcc
);
387 target_read_u32(target
, SCB_BASE
| PLLCFG
, &pllcfg
);
388 LOG_DEBUG("Stellaris PLLCFG %" PRIx32
"", pllcfg
);
390 stellaris_info
->rcc
= rcc
;
391 stellaris_info
->rcc
= rcc2
;
393 sysdiv
= (rcc
>> 23) & 0xF;
394 usesysdiv
= (rcc
>> 22) & 0x1;
395 bypass
= (rcc
>> 11) & 0x1;
396 oscsrc
= (rcc
>> 4) & 0x3;
397 xtal
= (rcc
>> 6) & stellaris_info
->xtal_mask
;
399 /* NOTE: post-Sandstorm parts have RCC2 which may override
400 * parts of RCC ... with more sysdiv options, option for
401 * 32768 Hz mainfreq, PLL controls. On Sandstorm it reads
402 * as zero, so the "use RCC2" flag is always clear.
404 if (rcc2
& (1 << 31)) {
405 sysdiv
= (rcc2
>> 23) & 0x3F;
406 bypass
= (rcc2
>> 11) & 0x1;
407 oscsrc
= (rcc2
>> 4) & 0x7;
409 /* FIXME Tempest parts have an additional lsb for
410 * fractional sysdiv (200 MHz / 2.5 == 80 MHz)
414 stellaris_info
->mck_desc
= "";
419 mainfreq
= rcc_xtal
[xtal
];
422 mainfreq
= stellaris_info
->iosc_freq
;
423 stellaris_info
->mck_desc
= stellaris_info
->iosc_desc
;
426 mainfreq
= stellaris_info
->iosc_freq
/ 4;
427 stellaris_info
->mck_desc
= stellaris_info
->iosc_desc
;
429 case 3: /* lowspeed */
430 /* Sandstorm doesn't have this 30K +/- 30% osc */
432 stellaris_info
->mck_desc
= " (±30%)";
434 case 8: /* hibernation osc */
435 /* not all parts support hibernation */
439 default: /* NOTREACHED */
444 /* PLL is used if it's not bypassed; its output is 200 MHz
445 * even when it runs at 400 MHz (adds divide-by-two stage).
448 mainfreq
= 200000000;
451 stellaris_info
->mck_freq
= mainfreq
/(1 + sysdiv
);
453 stellaris_info
->mck_freq
= mainfreq
;
455 /* Forget old flash timing */
456 stellaris_set_flash_mode(bank
, 0);
459 /* Setup the timimg registers */
460 static void stellaris_set_flash_mode(struct flash_bank
*bank
,int mode
)
462 struct stellaris_flash_bank
*stellaris_info
= stellaris_bank_data(bank
);
463 struct target
*target
= bank
->target
;
465 uint32_t usecrl
= (stellaris_info
->mck_freq
/1000000ul-1);
466 LOG_DEBUG("usecrl = %i",(int)(usecrl
));
467 target_write_u32(target
, SCB_BASE
| USECRL
, usecrl
);
471 static uint32_t stellaris_wait_status_busy(struct flash_bank
*bank
, uint32_t waitbits
, int timeout
)
475 /* Stellaris waits for cmdbit to clear */
476 while (((status
= stellaris_get_flash_status(bank
)) & waitbits
) && (timeout
-- > 0))
478 LOG_DEBUG("status: 0x%x", status
);
482 /* Flash errors are reflected in the FLASH_CRIS register */
487 /* Send one command to the flash controller */
488 static int stellaris_flash_command(struct flash_bank
*bank
,uint8_t cmd
,uint16_t pagen
)
491 struct target
*target
= bank
->target
;
493 fmc
= FMC_WRKEY
| cmd
;
494 target_write_u32(target
, FLASH_CONTROL_BASE
| FLASH_FMC
, fmc
);
495 LOG_DEBUG("Flash command: 0x%x", fmc
);
497 if (stellaris_wait_status_busy(bank
, cmd
, 100))
499 return ERROR_FLASH_OPERATION_FAILED
;
506 /* Read device id register, main clock frequency register and fill in driver info structure */
507 static int stellaris_read_part_info(struct flash_bank
*bank
)
509 struct stellaris_flash_bank
*stellaris_info
= stellaris_bank_data(bank
);
510 struct target
*target
= bank
->target
;
511 uint32_t did0
, did1
, ver
, fam
, status
;
514 /* Read and parse chip identification register */
515 target_read_u32(target
, SCB_BASE
| DID0
, &did0
);
516 target_read_u32(target
, SCB_BASE
| DID1
, &did1
);
517 target_read_u32(target
, SCB_BASE
| DC0
, &stellaris_info
->dc0
);
518 target_read_u32(target
, SCB_BASE
| DC1
, &stellaris_info
->dc1
);
519 LOG_DEBUG("did0 0x%" PRIx32
", did1 0x%" PRIx32
", dc0 0x%" PRIx32
", dc1 0x%" PRIx32
"",
520 did0
, did1
, stellaris_info
->dc0
, stellaris_info
->dc1
);
523 if ((ver
!= 0) && (ver
!= 1))
525 LOG_WARNING("Unknown did0 version, cannot identify target");
526 return ERROR_FLASH_OPERATION_FAILED
;
531 LOG_WARNING("Cannot identify target as a Stellaris");
532 return ERROR_FLASH_OPERATION_FAILED
;
536 fam
= (did1
>> 24) & 0xF;
537 if (((ver
!= 0) && (ver
!= 1)) || (fam
!= 0))
539 LOG_WARNING("Unknown did1 version/family, cannot positively identify target as a Stellaris");
542 /* For Sandstorm, Fury, DustDevil: current data sheets say IOSC
543 * is 12 MHz, but some older parts have 15 MHz. A few data sheets
544 * even give _both_ numbers! We'll use current numbers; IOSC is
545 * always approximate.
547 * For Tempest: IOSC is calibrated, 16 MHz
549 stellaris_info
->iosc_freq
= 12000000;
550 stellaris_info
->iosc_desc
= " (±30%)";
551 stellaris_info
->xtal_mask
= 0x0f;
553 switch ((did0
>> 28) & 0x7) {
554 case 0: /* Sandstorm */
556 * Current (2009-August) parts seem to be rev C2 and use 12 MHz.
557 * Parts before rev C0 used 15 MHz; some C0 parts use 15 MHz
558 * (LM3S618), but some other C0 parts are 12 MHz (LM3S811).
560 if (((did0
>> 8) & 0xff) < 2) {
561 stellaris_info
->iosc_freq
= 15000000;
562 stellaris_info
->iosc_desc
= " (±50%)";
566 switch ((did0
>> 16) & 0xff) {
569 case 4: /* Tempest */
570 stellaris_info
->iosc_freq
= 16000000; /* +/- 1% */
571 stellaris_info
->iosc_desc
= " (±1%)";
573 case 3: /* DustDevil */
574 stellaris_info
->xtal_mask
= 0x1f;
577 LOG_WARNING("Unknown did0 class");
581 LOG_WARNING("Unknown did0 version");
584 for (i
= 0; StellarisParts
[i
].partno
; i
++)
586 if (StellarisParts
[i
].partno
== ((did1
>> 16) & 0xFF))
590 stellaris_info
->target_name
= StellarisParts
[i
].partname
;
592 stellaris_info
->did0
= did0
;
593 stellaris_info
->did1
= did1
;
595 stellaris_info
->num_lockbits
= 1 + (stellaris_info
->dc0
& 0xFFFF);
596 stellaris_info
->num_pages
= 2 *(1 + (stellaris_info
->dc0
& 0xFFFF));
597 stellaris_info
->pagesize
= 1024;
598 bank
->size
= 1024 * stellaris_info
->num_pages
;
599 stellaris_info
->pages_in_lockregion
= 2;
600 target_read_u32(target
, SCB_BASE
| FMPPE
, &stellaris_info
->lockbits
);
602 /* provide this for the benefit of the higher flash driver layers */
603 bank
->num_sectors
= stellaris_info
->num_pages
;
604 bank
->sectors
= malloc(sizeof(struct flash_sector
) * bank
->num_sectors
);
605 for (i
= 0; i
< bank
->num_sectors
; i
++)
607 bank
->sectors
[i
].offset
= i
* stellaris_info
->pagesize
;
608 bank
->sectors
[i
].size
= stellaris_info
->pagesize
;
609 bank
->sectors
[i
].is_erased
= -1;
610 bank
->sectors
[i
].is_protected
= -1;
613 /* Read main and master clock freqency register */
614 stellaris_read_clock_info(bank
);
616 status
= stellaris_get_flash_status(bank
);
621 /***************************************************************************
623 ***************************************************************************/
625 static int stellaris_protect_check(struct flash_bank
*bank
)
629 struct stellaris_flash_bank
*stellaris_info
= stellaris_bank_data(bank
);
631 if (bank
->target
->state
!= TARGET_HALTED
)
633 LOG_ERROR("Target not halted");
634 return ERROR_TARGET_NOT_HALTED
;
637 if (stellaris_info
->did1
== 0)
639 stellaris_read_part_info(bank
);
642 if (stellaris_info
->did1
== 0)
644 LOG_WARNING("Cannot identify target as Stellaris");
645 return ERROR_FLASH_OPERATION_FAILED
;
648 status
= stellaris_get_flash_status(bank
);
649 stellaris_info
->lockbits
= status
>> 16;
654 static int stellaris_erase(struct flash_bank
*bank
, int first
, int last
)
657 uint32_t flash_fmc
, flash_cris
;
658 struct stellaris_flash_bank
*stellaris_info
= stellaris_bank_data(bank
);
659 struct target
*target
= bank
->target
;
661 if (bank
->target
->state
!= TARGET_HALTED
)
663 LOG_ERROR("Target not halted");
664 return ERROR_TARGET_NOT_HALTED
;
667 if (stellaris_info
->did1
== 0)
669 stellaris_read_part_info(bank
);
672 if (stellaris_info
->did1
== 0)
674 LOG_WARNING("Cannot identify target as Stellaris");
675 return ERROR_FLASH_OPERATION_FAILED
;
678 if ((first
< 0) || (last
< first
) || (last
>= (int)stellaris_info
->num_pages
))
680 return ERROR_FLASH_SECTOR_INVALID
;
683 if ((first
== 0) && (last
== ((int)stellaris_info
->num_pages
-1)))
685 return stellaris_mass_erase(bank
);
688 /* Configure the flash controller timing */
689 stellaris_read_clock_info(bank
);
690 stellaris_set_flash_mode(bank
,0);
692 /* Clear and disable flash programming interrupts */
693 target_write_u32(target
, FLASH_CIM
, 0);
694 target_write_u32(target
, FLASH_MISC
, PMISC
| AMISC
);
696 for (banknr
= first
; banknr
<= last
; banknr
++)
698 /* Address is first word in page */
699 target_write_u32(target
, FLASH_FMA
, banknr
* stellaris_info
->pagesize
);
700 /* Write erase command */
701 target_write_u32(target
, FLASH_FMC
, FMC_WRKEY
| FMC_ERASE
);
702 /* Wait until erase complete */
705 target_read_u32(target
, FLASH_FMC
, &flash_fmc
);
707 while (flash_fmc
& FMC_ERASE
);
709 /* Check acess violations */
710 target_read_u32(target
, FLASH_CRIS
, &flash_cris
);
711 if (flash_cris
& (AMASK
))
713 LOG_WARNING("Error erasing flash page %i, flash_cris 0x%" PRIx32
"", banknr
, flash_cris
);
714 target_write_u32(target
, FLASH_CRIS
, 0);
715 return ERROR_FLASH_OPERATION_FAILED
;
718 bank
->sectors
[banknr
].is_erased
= 1;
724 static int stellaris_protect(struct flash_bank
*bank
, int set
, int first
, int last
)
726 uint32_t fmppe
, flash_fmc
, flash_cris
;
729 struct stellaris_flash_bank
*stellaris_info
= stellaris_bank_data(bank
);
730 struct target
*target
= bank
->target
;
732 if (bank
->target
->state
!= TARGET_HALTED
)
734 LOG_ERROR("Target not halted");
735 return ERROR_TARGET_NOT_HALTED
;
738 if ((first
< 0) || (last
< first
) || (last
>= stellaris_info
->num_lockbits
))
740 return ERROR_FLASH_SECTOR_INVALID
;
743 if (stellaris_info
->did1
== 0)
745 stellaris_read_part_info(bank
);
748 if (stellaris_info
->did1
== 0)
750 LOG_WARNING("Cannot identify target as an Stellaris MCU");
751 return ERROR_FLASH_OPERATION_FAILED
;
754 /* Configure the flash controller timing */
755 stellaris_read_clock_info(bank
);
756 stellaris_set_flash_mode(bank
, 0);
758 fmppe
= stellaris_info
->lockbits
;
759 for (lockregion
= first
; lockregion
<= last
; lockregion
++)
762 fmppe
&= ~(1 << lockregion
);
764 fmppe
|= (1 << lockregion
);
767 /* Clear and disable flash programming interrupts */
768 target_write_u32(target
, FLASH_CIM
, 0);
769 target_write_u32(target
, FLASH_MISC
, PMISC
| AMISC
);
771 LOG_DEBUG("fmppe 0x%" PRIx32
"",fmppe
);
772 target_write_u32(target
, SCB_BASE
| FMPPE
, fmppe
);
774 target_write_u32(target
, FLASH_FMA
, 1);
775 /* Write commit command */
776 /* TODO safety check, sice this cannot be undone */
777 LOG_WARNING("Flash protection cannot be removed once commited, commit is NOT executed !");
778 /* target_write_u32(target, FLASH_FMC, FMC_WRKEY | FMC_COMT); */
779 /* Wait until erase complete */
782 target_read_u32(target
, FLASH_FMC
, &flash_fmc
);
784 while (flash_fmc
& FMC_COMT
);
786 /* Check acess violations */
787 target_read_u32(target
, FLASH_CRIS
, &flash_cris
);
788 if (flash_cris
& (AMASK
))
790 LOG_WARNING("Error setting flash page protection, flash_cris 0x%" PRIx32
"", flash_cris
);
791 target_write_u32(target
, FLASH_CRIS
, 0);
792 return ERROR_FLASH_OPERATION_FAILED
;
795 target_read_u32(target
, SCB_BASE
| FMPPE
, &stellaris_info
->lockbits
);
800 static uint8_t stellaris_write_code
[] =
805 r1 = destination address
806 r2 = bytecount (in) - endaddr (work)
809 r3 = pFLASH_CTRL_BASE
815 0x07,0x4B, /* ldr r3,pFLASH_CTRL_BASE */
816 0x08,0x4C, /* ldr r4,FLASHWRITECMD */
817 0x01,0x25, /* movs r5, 1 */
818 0x00,0x26, /* movs r6, #0 */
820 0x19,0x60, /* str r1, [r3, #0] */
821 0x87,0x59, /* ldr r7, [r0, r6] */
822 0x5F,0x60, /* str r7, [r3, #4] */
823 0x9C,0x60, /* str r4, [r3, #8] */
825 0x9F,0x68, /* ldr r7, [r3, #8] */
826 0x2F,0x42, /* tst r7, r5 */
827 0xFC,0xD1, /* bne waitloop */
828 0x04,0x31, /* adds r1, r1, #4 */
829 0x04,0x36, /* adds r6, r6, #4 */
830 0x96,0x42, /* cmp r6, r2 */
831 0xF4,0xD1, /* bne mainloop */
833 0xFE,0xE7, /* b exit */
834 /* pFLASH_CTRL_BASE: */
835 0x00,0xD0,0x0F,0x40, /* .word 0x400FD000 */
837 0x01,0x00,0x42,0xA4 /* .word 0xA4420001 */
840 static int stellaris_write_block(struct flash_bank
*bank
, uint8_t *buffer
, uint32_t offset
, uint32_t wcount
)
842 struct target
*target
= bank
->target
;
843 uint32_t buffer_size
= 8192;
844 struct working_area
*source
;
845 struct working_area
*write_algorithm
;
846 uint32_t address
= bank
->base
+ offset
;
847 struct reg_param reg_params
[3];
848 struct armv7m_algorithm armv7m_info
;
849 int retval
= ERROR_OK
;
851 LOG_DEBUG("(bank=%p buffer=%p offset=%08" PRIx32
" wcount=%08" PRIx32
"",
852 bank
, buffer
, offset
, wcount
);
854 /* flash write code */
855 if (target_alloc_working_area(target
, sizeof(stellaris_write_code
), &write_algorithm
) != ERROR_OK
)
857 LOG_WARNING("no working area available, can't do block memory writes");
858 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
861 target_write_buffer(target
, write_algorithm
->address
, sizeof(stellaris_write_code
), stellaris_write_code
);
864 while (target_alloc_working_area(target
, buffer_size
, &source
) != ERROR_OK
)
866 LOG_DEBUG("called target_alloc_working_area(target=%p buffer_size=%08" PRIx32
" source=%p)",
867 target
, buffer_size
, source
);
869 if (buffer_size
<= 256)
871 /* if we already allocated the writing code, but failed to get a buffer, free the algorithm */
873 target_free_working_area(target
, write_algorithm
);
875 LOG_WARNING("no large enough working area available, can't do block memory writes");
876 return ERROR_TARGET_RESOURCE_NOT_AVAILABLE
;
880 armv7m_info
.common_magic
= ARMV7M_COMMON_MAGIC
;
881 armv7m_info
.core_mode
= ARMV7M_MODE_ANY
;
883 init_reg_param(®_params
[0], "r0", 32, PARAM_OUT
);
884 init_reg_param(®_params
[1], "r1", 32, PARAM_OUT
);
885 init_reg_param(®_params
[2], "r2", 32, PARAM_OUT
);
889 uint32_t thisrun_count
= (wcount
> (buffer_size
/ 4)) ? (buffer_size
/ 4) : wcount
;
891 target_write_buffer(target
, source
->address
, thisrun_count
* 4, buffer
);
893 buf_set_u32(reg_params
[0].value
, 0, 32, source
->address
);
894 buf_set_u32(reg_params
[1].value
, 0, 32, address
);
895 buf_set_u32(reg_params
[2].value
, 0, 32, 4*thisrun_count
);
896 LOG_INFO("Algorithm flash write %" PRIi32
" words to 0x%" PRIx32
", %" PRIi32
" remaining", thisrun_count
, address
, (wcount
- thisrun_count
));
897 LOG_DEBUG("Algorithm flash write %" PRIi32
" words to 0x%" PRIx32
", %" PRIi32
" remaining", thisrun_count
, address
, (wcount
- thisrun_count
));
898 if ((retval
= target_run_algorithm(target
, 0, NULL
, 3, reg_params
, write_algorithm
->address
, write_algorithm
->address
+ sizeof(stellaris_write_code
)-10, 10000, &armv7m_info
)) != ERROR_OK
)
900 LOG_ERROR("error executing stellaris flash write algorithm");
901 retval
= ERROR_FLASH_OPERATION_FAILED
;
905 buffer
+= thisrun_count
* 4;
906 address
+= thisrun_count
* 4;
907 wcount
-= thisrun_count
;
910 target_free_working_area(target
, write_algorithm
);
911 target_free_working_area(target
, source
);
913 destroy_reg_param(®_params
[0]);
914 destroy_reg_param(®_params
[1]);
915 destroy_reg_param(®_params
[2]);
920 static int stellaris_write(struct flash_bank
*bank
, uint8_t *buffer
, uint32_t offset
, uint32_t count
)
922 struct stellaris_flash_bank
*stellaris_info
= stellaris_bank_data(bank
);
923 struct target
*target
= bank
->target
;
924 uint32_t address
= offset
;
925 uint32_t flash_cris
, flash_fmc
;
926 uint32_t words_remaining
= (count
/ 4);
927 uint32_t bytes_remaining
= (count
& 0x00000003);
928 uint32_t bytes_written
= 0;
931 if (bank
->target
->state
!= TARGET_HALTED
)
933 LOG_ERROR("Target not halted");
934 return ERROR_TARGET_NOT_HALTED
;
937 LOG_DEBUG("(bank=%p buffer=%p offset=%08" PRIx32
" count=%08" PRIx32
"",
938 bank
, buffer
, offset
, count
);
940 if (stellaris_info
->did1
== 0)
942 stellaris_read_part_info(bank
);
945 if (stellaris_info
->did1
== 0)
947 LOG_WARNING("Cannot identify target as a Stellaris processor");
948 return ERROR_FLASH_OPERATION_FAILED
;
953 LOG_WARNING("offset size must be word aligned");
954 return ERROR_FLASH_DST_BREAKS_ALIGNMENT
;
957 if (offset
+ count
> bank
->size
)
958 return ERROR_FLASH_DST_OUT_OF_BANK
;
960 /* Configure the flash controller timing */
961 stellaris_read_clock_info(bank
);
962 stellaris_set_flash_mode(bank
, 0);
964 /* Clear and disable flash programming interrupts */
965 target_write_u32(target
, FLASH_CIM
, 0);
966 target_write_u32(target
, FLASH_MISC
, PMISC
| AMISC
);
968 /* multiple words to be programmed? */
969 if (words_remaining
> 0)
971 /* try using a block write */
972 if ((retval
= stellaris_write_block(bank
, buffer
, offset
, words_remaining
)) != ERROR_OK
)
974 if (retval
== ERROR_TARGET_RESOURCE_NOT_AVAILABLE
)
976 /* if block write failed (no sufficient working area),
977 * we use normal (slow) single dword accesses */
978 LOG_WARNING("couldn't use block writes, falling back to single memory accesses");
980 else if (retval
== ERROR_FLASH_OPERATION_FAILED
)
982 /* if an error occured, we examine the reason, and quit */
983 target_read_u32(target
, FLASH_CRIS
, &flash_cris
);
985 LOG_ERROR("flash writing failed with CRIS: 0x%" PRIx32
"", flash_cris
);
986 return ERROR_FLASH_OPERATION_FAILED
;
991 buffer
+= words_remaining
* 4;
992 address
+= words_remaining
* 4;
997 while (words_remaining
> 0)
999 if (!(address
& 0xff))
1000 LOG_DEBUG("0x%" PRIx32
"", address
);
1002 /* Program one word */
1003 target_write_u32(target
, FLASH_FMA
, address
);
1004 target_write_buffer(target
, FLASH_FMD
, 4, buffer
);
1005 target_write_u32(target
, FLASH_FMC
, FMC_WRKEY
| FMC_WRITE
);
1006 /* LOG_DEBUG("0x%x 0x%x 0x%x",address,buf_get_u32(buffer, 0, 32),FMC_WRKEY | FMC_WRITE); */
1007 /* Wait until write complete */
1010 target_read_u32(target
, FLASH_FMC
, &flash_fmc
);
1011 } while (flash_fmc
& FMC_WRITE
);
1018 if (bytes_remaining
)
1020 uint8_t last_word
[4] = {0xff, 0xff, 0xff, 0xff};
1023 while (bytes_remaining
> 0)
1025 last_word
[i
++] = *(buffer
+ bytes_written
);
1030 if (!(address
& 0xff))
1031 LOG_DEBUG("0x%" PRIx32
"", address
);
1033 /* Program one word */
1034 target_write_u32(target
, FLASH_FMA
, address
);
1035 target_write_buffer(target
, FLASH_FMD
, 4, last_word
);
1036 target_write_u32(target
, FLASH_FMC
, FMC_WRKEY
| FMC_WRITE
);
1037 /* LOG_DEBUG("0x%x 0x%x 0x%x",address,buf_get_u32(buffer, 0, 32),FMC_WRKEY | FMC_WRITE); */
1038 /* Wait until write complete */
1041 target_read_u32(target
, FLASH_FMC
, &flash_fmc
);
1042 } while (flash_fmc
& FMC_WRITE
);
1045 /* Check access violations */
1046 target_read_u32(target
, FLASH_CRIS
, &flash_cris
);
1047 if (flash_cris
& (AMASK
))
1049 LOG_DEBUG("flash_cris 0x%" PRIx32
"", flash_cris
);
1050 return ERROR_FLASH_OPERATION_FAILED
;
1055 static int stellaris_probe(struct flash_bank
*bank
)
1057 /* we can't probe on an stellaris
1058 * if this is an stellaris, it has the configured flash
1061 if (bank
->target
->state
!= TARGET_HALTED
)
1063 LOG_ERROR("Target not halted");
1064 return ERROR_TARGET_NOT_HALTED
;
1067 /* stellaris_read_part_info() already takes care about error checking and reporting */
1068 return stellaris_read_part_info(bank
);
1071 static int stellaris_auto_probe(struct flash_bank
*bank
)
1073 struct stellaris_flash_bank
*stellaris_info
= stellaris_bank_data(bank
);
1074 if (stellaris_info
->did1
)
1076 return stellaris_probe(bank
);
1079 static int stellaris_mass_erase(struct flash_bank
*bank
)
1081 struct target
*target
= NULL
;
1082 struct stellaris_flash_bank
*stellaris_info
= NULL
;
1085 stellaris_info
= stellaris_bank_data(bank
);
1086 target
= bank
->target
;
1088 if (target
->state
!= TARGET_HALTED
)
1090 LOG_ERROR("Target not halted");
1091 return ERROR_TARGET_NOT_HALTED
;
1094 if (stellaris_info
->did1
== 0)
1096 stellaris_read_part_info(bank
);
1099 if (stellaris_info
->did1
== 0)
1101 LOG_WARNING("Cannot identify target as Stellaris");
1102 return ERROR_FLASH_OPERATION_FAILED
;
1105 /* Configure the flash controller timing */
1106 stellaris_read_clock_info(bank
);
1107 stellaris_set_flash_mode(bank
, 0);
1109 /* Clear and disable flash programming interrupts */
1110 target_write_u32(target
, FLASH_CIM
, 0);
1111 target_write_u32(target
, FLASH_MISC
, PMISC
| AMISC
);
1113 target_write_u32(target
, FLASH_FMA
, 0);
1114 target_write_u32(target
, FLASH_FMC
, FMC_WRKEY
| FMC_MERASE
);
1115 /* Wait until erase complete */
1118 target_read_u32(target
, FLASH_FMC
, &flash_fmc
);
1120 while (flash_fmc
& FMC_MERASE
);
1122 /* if device has > 128k, then second erase cycle is needed
1123 * this is only valid for older devices, but will not hurt */
1124 if (stellaris_info
->num_pages
* stellaris_info
->pagesize
> 0x20000)
1126 target_write_u32(target
, FLASH_FMA
, 0x20000);
1127 target_write_u32(target
, FLASH_FMC
, FMC_WRKEY
| FMC_MERASE
);
1128 /* Wait until erase complete */
1131 target_read_u32(target
, FLASH_FMC
, &flash_fmc
);
1133 while (flash_fmc
& FMC_MERASE
);
1139 COMMAND_HANDLER(stellaris_handle_mass_erase_command
)
1145 command_print(CMD_CTX
, "stellaris mass_erase <bank>");
1149 struct flash_bank
*bank
;
1150 int retval
= CALL_COMMAND_HANDLER(flash_command_get_bank
, 0, &bank
);
1151 if (ERROR_OK
!= retval
)
1154 if (stellaris_mass_erase(bank
) == ERROR_OK
)
1156 /* set all sectors as erased */
1157 for (i
= 0; i
< bank
->num_sectors
; i
++)
1159 bank
->sectors
[i
].is_erased
= 1;
1162 command_print(CMD_CTX
, "stellaris mass erase complete");
1166 command_print(CMD_CTX
, "stellaris mass erase failed");
1172 static const struct command_registration stellaris_exec_command_handlers
[] = {
1174 .name
= "mass_erase",
1175 .handler
= &stellaris_handle_mass_erase_command
,
1176 .mode
= COMMAND_EXEC
,
1177 .help
= "erase entire device",
1179 COMMAND_REGISTRATION_DONE
1181 static const struct command_registration stellaris_command_handlers
[] = {
1183 .name
= "stellaris",
1184 .mode
= COMMAND_ANY
,
1185 .help
= "Stellaris flash command group",
1186 .chain
= stellaris_exec_command_handlers
,
1188 COMMAND_REGISTRATION_DONE
1191 FLASH_DRIVER(stellaris
,
1192 &stellaris_flash_bank_command
, stellaris_command_handlers
,
1193 .erase
= &stellaris_erase
,
1194 .protect
= &stellaris_protect
,
1195 .write
= &stellaris_write
,
1196 .probe
= &stellaris_probe
,
1197 .auto_probe
= &stellaris_auto_probe
,
1198 .erase_check
= &default_flash_mem_blank_check
,
1199 .protect_check
= &stellaris_protect_check
,
1200 .info
= &stellaris_info
,