; --------------------------------------------------------------------------------
; @Title: Example script for programming of MPC555 internal flash 
;
; @Description:
; Example script for programming of MPC555 internal flash and target 
; controlled programming of external flash.
;
; Script arguments:
;
;   DO mpc555-external [PREPAREONLY]
;
;     PREPAREONLY only declares flash but does not execute flash programming
;
; Example:
;
;   DO ~~/demo/powerpc/flash/mpc555-external PREPAREONLY
;
; Internal flash memory 448 kByte:    0x00000000--0x0006ffff
; Internal SRAM 26 kByte:             0x003f9800--0x003fffff 
;
; External flash (CS0) at:            0x80000000
;
; @Author: WRD
; @Chip: MPC555
; @Copyright: (C) 1989-2014 Lauterbach GmbH, licensed for use with TRACE32(R) only
; --------------------------------------------------------------------------------
; $Rev: 3350 $
; $Id: mpc555-external.cmm 3350 2015-07-28 08:10:27Z rweiss $
;

LOCAL &ISBbase &CS0Base
LOCAL &progflash

LOCAL &parameters
ENTRY %LINE &parameters

LOCAL &param_prepareonly
&param_prepareonly=(STRing.SCAN(STRing.UPpeR("&parameters"),"PREPAREONLY",0)!=-1)

; --------------------------------------------------------------------------------
; CPU setup

SYStem.RESet
SYStem.CPU MPC555
SYStem.Up

&ISBbase=(((Data.Long(SPR:0x27e))&0x0e)*0x200000)

; Bugfix if there is no PU on IRQ0.
; If IRQ0 is floating, this non maskable IRQ will force an interrupt.
; Therfore all IRQ pins will be configured as "all_IO" in SIUMCR[MLRC]
; A better way is to put PU's on all IRQ's!
Data.Set &ISBbase+0x2fc000 %Long (Data.Long(D:(&ISBbase+0x2fc000))&0xfffff3ff)+0x800

; Enable internal flash (CMF)
Data.Set SPR:0x27E %Long Data.Long(SPR:0x27E)|0x800

; Configure CS0 for external flash (setup to your specific CSx setting)
;  example 16MB,32-bit data bus width
Data.Set &ISBbase+002fc104 %Long 0xff000012
Data.Set &ISBbase+002fc100 %Long 0x80000003
&CS0Base=(Data.Long(d:(&ISBbase+0x2fc100))&0xffff8000)


; Flash script ends here if called with parameter PREPAREONLY
  IF &param_prepareonly
    ENDDO PREPAREDONE

; --------------------------------------------------------------------------------
; Flash programming of external flash

DIALOG.YESNO "Program external flash memory?"
ENTRY &progflash

IF &progflash 
(
  ; Delete declaration for internal flash and declare external flash
  ; The example is declaring flash device by reading CFI information. For 
  ; the case CFI is not supported by the flash device manual declaration 
  ; has to be used instead.
  FLASH.Delete ALL
  LOCAL &ExtFlashSize
  &ExtFlashSize=FLASH.CFI.SIZE(D:&CS0Base,Long)
  IF &ExtFlashSize==0x0
  (
    PRINT "Flash device probably has no CFI information, please add manual flash declararion here"
    PLIST
    STOP
  )
  ELSE
  (
    FLASH.CFI &CS0Base Long /TARGET &ISBbase+0x003F9800 &ISBbase+0x003FA800 0x1000
  )

  ; Program external flash
  FLASH.ReProgram ALL /Erase
  Data.LOAD.auto * &CS0Base++(&ExtFlashSize-1)
  FLASH.ReProgram.off 

  ; Reset flash declaration to delete declaration of external flash and to
  ; declare internal flash
  FLASH.RESet
)

; --------------------------------------------------------------------------------
; Flash programming of internal flash

DIALOG.YESNO "Program internal flash memory?"
ENTRY &progflash

IF &progflash 
(
  ; Save shadow information to VM:0x100000
  ;   Set CMFMCR.SIE
  ;   Copy shadow information to virtual memory
  ;   Clear CMFMCR.SIE
  Data.Set &ISBbase+0x2fc800 %Long Data.Long(D:&ISBbase+0x2fc800)|0x08000000
  Data.COPY &ISBbase++0xff VM:&ISBbase+0x100000
  Data.Set &ISBbase+0x2fc800 %Long Data.Long(D:&ISBbase+0x2fc800)&(~0x08000000)

  ; Program flash
  LOCAL &partnum
  &partnum=(Data.Long(SPR:0x27E)&0xffff0000)>>16.
  IF &partnum>=0x3030
  (
    ; FLASH.MultiProgram feature is used for programming of MPC555 revision Kx and M
    FLASH.Erase ALL
    Data.Set VM:&ISBbase++0x6ffff %Long 0xffffffff
    Data.LOAD.auto * &ISBbase++0x6ffff /VM
    FLASH.MultiProgram.off &ISBbase++0x6ffff
  )
  ELSE
  (
    ; Flash programming of MPC555 revision older than Kx
    FLASH.Erase ALL
    FLASH.Program ALL
    Data.LOAD.auto * &ISBbase++0x6ffff
    FLASH.Program off
  )

  ; Restore shadow information from VM:0x100000
  ;   Set CMFMCR.SIE
  ;   Program shadow information
  ;   Clear CMFMCR.SIE
  Data.Set &ISBbase+0x2fc800 %Long Data.Long(D:&ISBbase+0x2fc800)|0x08000000
  FLASH.Program.off &ISBbase++0xff
  Data.COPY (VM:&ISBbase+0x100000)++0xff &ISBbase 
  FLASH.Program off
  Data.Set &ISBbase+0x2fc800 %Long Data.Long(D:&ISBbase+0x2fc800)&(~0x08000000)
)

ENDDO


