; --------------------------------------------------------------------------------
; @Title: Example script for programming of MPC551x internal flash.
;
; @Description:
; Internal Flash Memory 512 kByte .. 1.5 MByte:
;   Low address range   256 kByte (8x 16kByte, 2x 64 kByte)
;                       0x00000000--0x0003ffff
;   Mid address range   256 kByte (2 * 128 kByte)
;                       0x00040000--0x0007ffff
;   High address range  0..1 MByte (0..8 * 128 kByte)
;   MPC5514:            none
;   MPC5515:            0x00080000--0x000bffff
;   MPC5516:            0x00080000--0x000fffff
;   MPC5517:            0x00080000--0x0017ffff
;   Shadow row          32 kByte
;                       0x00ff8000--0x00ffffff
; 
; Internal SRAM         32--80 kByte
;                       0x40000000--0x40007fff 
;
; Flash register base address is 0xFFFF8000
;
; Flash algorithm may depend on processor revision:
;   MPC551x all revisions       h7fb5510_v323.bin or higher
;
;   h7fb5510.bin contains latest version of flash algorithm.
;
; NOTES:
; 
;   Flash register base address has to be 0xffff8000, because target
;   program does currently not support another base address.
;
; @Author: WRD
; @Chip: MPC551*
; @Copyright: (C) 1989-2017 Lauterbach GmbH, licensed for use with TRACE32(R) only
; --------------------------------------------------------------------------------
; $Rev: 5830 $
; $Id: mpc551x.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 &flashdriver &flashsize &optimize
&flashdriver="tbd.bin"
&flashsize=0x000100000

; Optimize flash programming time by switching on PLL
; Check thar the PLL setup is matching your target before 
; enabling this option
&optimize=0

LOCAL &DualPort
IF VERSION.BUILD.BASE()>=45520.
  &DualPort="/DualPort"

; --------------------------------------------------------------------------------
; CPU setup

IF !&param_skipconfig
(
  SYStem.RESet
  SYStem.BdmClock 4.0MHz
  SYStem.CPU MPC55XX
  SYStem.Up

  IF &optimize==1
  (
    ; setup PLL to 80 MHz (Values for Freescale MPC5510 evaluation board with 8 MHz external oscillator)
    Data.Set ANC:0xFFFF0008 %Long 0xF000002C ; FMPLL_ESYNCR1: EPREDIV=1:1, EMFD=44
    ; wait for FMPLL_SYNSR[LOCK] (timeout 2 seconds)
    WAIT (Data.Long(ANC:0xFFFF0004)&0x00000008)==0x00000008 2.s
    IF (Data.Long(ANC:0xFFFF0004)&0x00000008)!=0x00000008
    (
      DIALOG.OK "FMPLL did not lock. Aborting."
      ENDDO
    )
    Data.Set ANC:0xFFFE89A0 %Long 0x80000000 ;SIU_SYSCLK[SYSCLKSEL]=FMPLL
    ; set JTAG clock to 10 MHz (-> minimum TCK cycle time is 100ns)
    SYStem.BdmClock 10MHz
  )
)

; 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 0xC0000400 0x40000008 0x4000003F
MMU.Set TLB1 0x3 0xC0000700 0x00000000 0x0000003F

; read flash size from FLASH_MCR
LOCAL &flash_mcr_size
&flash_mcr_size=(Data.Long(A:0xFFFF8000)>>24.)&0x0F

IF &flash_mcr_size==1
  &flashsize=0x000080000 ; 512K
ELSE IF &flash_mcr_size==2
  &flashsize=0x0000C0000 ; 768K
ELSE IF &flash_mcr_size==3
  &flashsize=0x000100000 ; 1M
ELSE IF &flash_mcr_size==5
  &flashsize=0x000180000 ; 1.5M
ELSE
(
  PRINT %ERROR "Error: unknown flash size &flash_mcr_size (processor "+CPU()+")"
  STOP
)

; --------------------------------------------------------------------------------
; Check SIU_MIDR register to select flash driver depending on processor 
; revision
&flashdriver="~~/demo/powerpc/flash/quad/h7fb5510.bin"

; --------------------------------------------------------------------------------
; Flash declaration

FLASH.RESet

FLASH.Create 1. 0x00000000++0x03fff TARGET Quad 0. ; L0
FLASH.Create 1. 0x00004000++0x03fff TARGET Quad 1. ; L1
FLASH.Create 1. 0x00008000++0x03fff TARGET Quad 2. ; L2
FLASH.Create 1. 0x0000C000++0x03fff TARGET Quad 3. ; L3
FLASH.Create 1. 0x00010000++0x03fff TARGET Quad 4. ; L4
FLASH.Create 1. 0x00014000++0x03fff TARGET Quad 5. ; L5
FLASH.Create 1. 0x00018000++0x03fff TARGET Quad 6. ; L6
FLASH.Create 1. 0x0001C000++0x03fff TARGET Quad 7. ; L7
FLASH.Create 1. 0x00020000++0x0ffff TARGET Quad 8. ; L8
FLASH.Create 1. 0x00030000++0x0ffff TARGET Quad 9. ; L9
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<&flashsize
(
  FLASH.Create 3. &flashaddr++0x1ffff TARGET Quad &Hx ; H0..H3
  &flashaddr=&flashaddr+0x20000
  &Hx=&Hx+1.
)
; Shadow row
FLASH.Create 4. 0x00ff8000++0x7fff NOP Quad /CENSORSHIP 0x00FFFDD8--0x00FFFDE7

FLASH.TARGET E:0x40000000 E:0x40002000 0x1000 &flashdriver &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
)

;set JTAG clock back to default
SYStem.BdmClock 4MHz

ENDDO
