; --------------------------------------------------------------------------------
; @Title: Example script for programming of MPC567xK/SPC56HK (Komodo) internal flash.
;
; @Description:
; Internal Flash Memory:
;   Flash A low address range   256 kByte (16, 16, 32, 32, 16, 16, 64, 64 kByte)
;                               0x00000000--0x0003ffff
;   Flash B low address range   256 kByte (16, 16, 32, 32, 16, 16, 64, 64 kByte)
;                               0x00040000--0x0007ffff
;   Flash A/B Mid address range 512 kByte (2 * 256 kByte)
;                               0x00080000--0x000fffff
;   High address range          1 MByte (4 * 256 kByte)
;                               0x00100000--0x001fffff
;
;   Shadow row 0 (Flash A)      16 kByte
;                               0x00200000--0x00203fff
;   Shadow row 1 (Flash B)      16 kByte
;                               0x00280000--0x00283fff
;
;   Data Flash                  64 kByte (4 * 16 kByte)
;                               0x00800000--0x0080ffff
; 
; Internal SRAM                 256..512 kByte
;                               0x40000000--0x4003ffff 
;
; Flash register base addresses are
;   Flash A    (CFLASH 0)       0xC3F88000
;   Flash B    (CFLASH 1)       0xC3FB0000
;   Data Flash (DFLASH 0)       0xC3F8C000
;
; NOTES:
; 
;   Flash register base addresses cannot be remapped, because target
;   program does currently not support another base address.
;
; @Author: WRD
; @Chip: MPC567?K SPC56HK
; @Copyright: (C) 1989-2017 Lauterbach GmbH, licensed for use with TRACE32(R) only
; --------------------------------------------------------------------------------
; $Rev: 4593 $
; $Id: jpc567xk.cmm 4593 2017-08-01 08:21:44Z 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 &flashsize
&flashsize="tbd"

LOCAL &DualPort
IF VERSION.BUILD.BASE()>=45520.
  &DualPort="/DualPort"

; --------------------------------------------------------------------------------
; CPU setup

IF !&param_skipconfig
(
  SYStem.RESet
  SYStem.BdmClock 4.0MHz
  SYStem.CPU MPC55XX
  SYStem.Option.WATCHDOG OFF
  SYStem.DETECT.CPU
  SYStem.Up
  Register.RESet ; ensure lock-step core's GPRs are initialized properly
)

IF (CPU()=="MPC5673K")||(CPU()=="SPC56HK60")
(
  &flashsize=0x00100000
)
ELSE IF (CPU()=="MPC5674K")||(CPU()=="SPC56HK64")
(
  &flashsize=0x00180000
)
ELSE IF (CPU()=="MPC5675K")||(CPU()=="SPC56HK70")
(
  &flashsize=0x00200000
)
ELSE
(
  PRINT %ERROR "Error: " CPU() " not supported by this script."
  ENDDO
)

; initialize internal SRAM
Data.Set EA:0x40000000--0x40007fff %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

; --------------------------------------------------------------------------------
; Flash declaration

FLASH.RESet

; LAS
FLASH.Create 1. 0x00000000++0x03fff TARGET Quad 0.  ; L0 bank 0
FLASH.Create 1. 0x00004000++0x03fff TARGET Quad 1.  ; L1 bank 0
FLASH.Create 1. 0x00008000++0x07fff TARGET Quad 2.  ; L2 bank 0
FLASH.Create 1. 0x00010000++0x07fff TARGET Quad 3.  ; L3 bank 0
FLASH.Create 1. 0x00018000++0x03fff TARGET Quad 4.  ; L4 bank 0
FLASH.Create 1. 0x0001C000++0x03fff TARGET Quad 5.  ; L5 bank 0
FLASH.Create 1. 0x00020000++0x0ffff TARGET Quad 6.  ; L6 bank 0
FLASH.Create 1. 0x00030000++0x0ffff TARGET Quad 7.  ; L7 bank 0

FLASH.Create 2. 0x00040000++0x03fff TARGET Quad 0.  ; L0 bank 1
FLASH.Create 2. 0x00044000++0x03fff TARGET Quad 1.  ; L1 bank 1
FLASH.Create 2. 0x00048000++0x07fff TARGET Quad 2.  ; L2 bank 1
FLASH.Create 2. 0x00050000++0x07fff TARGET Quad 3.  ; L3 bank 1
FLASH.Create 2. 0x00058000++0x03fff TARGET Quad 4.  ; L4 bank 1
FLASH.Create 2. 0x0005C000++0x03fff TARGET Quad 5.  ; L5 bank 1
FLASH.Create 2. 0x00060000++0x0ffff TARGET Quad 6.  ; L6 bank 1
FLASH.Create 2. 0x00070000++0x0ffff TARGET Quad 7.  ; L7 bank 1

; MAS
FLASH.Create 3. 0x00080000++0x3ffff TARGET Quad 0.  ; M0 bank 0+1
FLASH.Create 3. 0x000c0000++0x3ffff TARGET Quad 1.  ; M1 bank 0+1

; HAS
LOCAL &flashaddr &Hx
&flashaddr=0x00100000
&Hx=0.
WHILE &flashaddr<&flashsize
(
  FLASH.Create 4. &flashaddr++0x3ffff TARGET Quad &Hx  ; H0..H3 bank 0+1
  &flashaddr=&flashaddr+0x00040000
  &Hx=&Hx+1.
)

; Shadow rows
FLASH.Create 5. 0x00200000++0x3fff NOP Quad /CENSORSHIP 0x00203DD8--0x00203DE7 /INFO "CFM0 Shadow Row"
FLASH.Create 6. 0x00280000++0x3fff NOP Quad /INFO "CFM1 Shadow Row"

; Data flash
FLASH.Create 7. 0x00800000++0x3fff TARGET Quad 0.
FLASH.Create 7. 0x00804000++0x3fff TARGET Quad 1.
FLASH.Create 7. 0x00808000++0x3fff TARGET Quad 2.
FLASH.Create 7. 0x0080c000++0x3fff TARGET Quad 3.

FLASH.TARGET E:0x40000000 E:0x40004000 0x1000 ~~/demo/powerpc/flash/quad/c90fl567xk.bin /STACKSIZE 0x0200 &DualPort

;flash script ends here if called with parameter PREPAREONLY
IF &param_prepareonly
  ENDDO

; --------------------------------------------------------------------------------
; 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
