; --------------------------------------------------------------------------------
; @Title: Example script for programming internal flash of JPC563x derivatives.
; 
; @Description:
; Example script for programming internal flash of JPC563x derivatives:
; - SPC563M54 (768 kB flash)
; - MPC5632M (768 kB flash)
; - SPC563M60 (1 MB flash)
; - MPC5633M (1 MB flash)
; - SPC563M64 (1.5 MB flash)
; - MPC5634M (1.5 MB flash)
;
; Flash register base address is 0xc3f88000
;
; NOTES:
; 
;   Flash register base address has to be 0xc3f88000, because target
;   program does currently not support another base address.
;
; @Author: rweiss
; @Chip: SPC563* MPC563*
; @Copyright: (C) 1989-2017 Lauterbach GmbH, licensed for use with TRACE32(R) only
; --------------------------------------------------------------------------------
; $Rev: 5830 $
; $Id: jpc563xm.cmm 5830 2018-07-16 15:25:52Z rweiss $
;

; Check if script is called with parameters
; Valid parameters:
;   PREPAREONLY     : prepare flash programming without user interaction
;   SKIPCONFIG      : skip configuration part to allow script external configuration
LOCAL &parameters &param_prepareonly &param_skipconfig
ENTRY %LINE &parameters
&param_prepareonly=(STRing.SCAN(STRing.UPpeR("&parameters"),"PREPAREONLY",0)!=-1)
&param_skipconfig=(STRing.SCAN(STRing.UPpeR("&parameters"),"SKIPCONFIG",0)!=-1)

LOCAL &DualPort
IF VERSION.BUILD.BASE()>=45520.
  &DualPort="/DualPort"

; --------------------------------------------------------------------------------
; CPU setup

IF !&param_skipconfig
(
  SYStem.RESet
  SYStem.BdmClock 4.0MHz
  SYStem.CPU MPC55XX
  SYStem.DETECT.CPU
  SYStem.Option.WATCHDOG OFF
  SYStem.Up
)

;check processor revision
LOCAL &NPC_DID
&NPC_DID=Data.Long(DBG:0x80)
IF (&NPC_DID&0x0FFFF000)==0x08200000
(
  PRINT "Processor revision 0 detected, calling jpc563xm_rev0.cmm..."
  DO ~~~~/jpc563xm_rev0.cmm &parameters
  ENDDO
)

; initialize internal SRAM
Data.Set EA:0x40000000--0x4000bfff %Quad 0

; setup MMU for flash, RAM and register access
MMU.Set TLB1 0x0 0x00000000 0x00000000 0x00000000
MMU.Set TLB1 0x1 0xC0000500 0xFFF0000A 0xFFF0003F
MMU.Set TLB1 0x2 0xC0000700 0x20000000 0x2000003F
MMU.Set TLB1 0x3 0xC0000400 0x40000008 0x4000003F
MMU.Set TLB1 0x4 0xC0000500 0xC3F00008 0xC3F0003F
MMU.Set TLB1 0x5 0xC0000700 0x00000000 0x0000003F

;enable SPE
Register.Set SPE 1

; --------------------------------------------------------------------------------
; Flash declaration

FLASH.RESet

FLASH.Create 1. 0x00000000++0x03fff TARGET Quad 0. ; L0
FLASH.Create 1. 0x00004000++0x03fff TARGET Quad 1. ; L1a 
FLASH.Create 1. 0x00008000++0x07fff TARGET Quad 2. ; L1b 
FLASH.Create 1. 0x00010000++0x07fff TARGET Quad 3. ; L2a 
FLASH.Create 1. 0x00018000++0x03fff TARGET Quad 4. ; L2b 
FLASH.Create 1. 0x0001c000++0x03fff TARGET Quad 5. ; L3
FLASH.Create 1. 0x00020000++0x0ffff TARGET Quad 6. ; L4
FLASH.Create 1. 0x00030000++0x0ffff TARGET Quad 7. ; L5

IF (CPU()=="MPC5633M")||(CPU()=="MPC5634M")||(CPU()=="SPC563M60")||(CPU()=="SPC563M64")
(
  FLASH.Create 2. 0x00040000++0x1ffff TARGET Quad 0. ; M0
  FLASH.Create 2. 0x00060000++0x1ffff TARGET Quad 1. ; M1
)
LOCAL &flashaddr &Hx
&flashaddr=0x00080000
&Hx=0.
WHILE &flashaddr<0x100000
(
  FLASH.Create 3. &flashaddr++0x1ffff TARGET Quad &Hx ; H0..H3
  &flashaddr=&flashaddr+0x20000
  &Hx=&Hx+1.
)

IF (CPU()=="MPC5634M")||(CPU()=="SPC563M64")
(
  &Hx=0.
  WHILE &flashaddr<0x180000
  (
    FLASH.Create 4. &flashaddr++0x1ffff TARGET Quad &Hx ; H0..H3
    &flashaddr=&flashaddr+0x20000
    &Hx=&Hx+1.
  )
)

; Shadow row
FLASH.Create 5. 0x00ffc000++0x3fff NOP Quad /CENSORSHIP 0x00fffdd8--0x00fffde7 /INFO "Shadow Row"

FLASH.TARGET E:0x40000000 E:0x40002000 0x1000 ~~/demo/powerpc/flash/quad/c90fl563xm.bin /STACKSIZE 0x0200 &DualPort

;flash script ends here if called with parameter PREPAREONLY
IF &param_prepareonly
  ENDDO PREPAREDONE

; --------------------------------------------------------------------------------
; Flash programming example
;
; Flash programming speed is about three times faster when memory class E:
; is used for data buffer (DUALPORT memory access). For DUALPORT access it
; is required to setup MemAccess NEXUS for both, NEXUS and JTAG debugger. 

DIALOG.YESNO "Flash programming prepared. Program flash memory now?"
LOCAL &progflash
ENTRY &progflash

IF &progflash 
(
  FLASH.ReProgram ALL /Erase
  Data.LOAD.auto *
  FLASH.ReProgram.off 
)
ELSE
(
  FLASH.List
)

ENDDO


