; --------------------------------------------------------------------------------
; @Title: eMMC FLASH Program script for the J784S4/AM69
; @Description:
; The MTFC16GAPALBH is connected to the MMCSD0 controller
;
; SRAM:
; MMCSD0 Base: 0x04F80000
;
;
; @Chip:  TDA4VH/J784S4/AM69
; @Board: J784S4-EVM, AM69-SK
; @Author: JIM
; @Keywords:
; @Copyright: (C) 1989-2019 Lauterbach GmbH, licensed for use with TRACE32(R) only
; --------------------------------------------------------------------------------
;   see ../../notes/flash_burning.txt
&path=OS.PPD()
&MMC_BASE=0x04f80000  ;MMC0

&bare_metal="yes"              ; if R5 is not yet booted say yes

IF "&bare_metal"=="yes"
(
  DO "&path/../../x_gel_to_cmm/_J784S4_baremetal_setup.cmm" HS:FS
  TITLE "TRACE32 for ARM CM4-0 - MASTER"
  DO "&path/../../x_gel_to_cmm/_J784S4_m4.cmm" CM4-0 PREPARE
  DO "&path/../../x_gel_to_cmm/J7AHP.cmm" DMSC_FLOW
  wait 1s
)

; --------------------------------------------------------------------------------
; Connect to MCU-CR5 to program flash
TITLE "TRACE32 for ARM CR5-MCU - MASTER"
DO "&path/../../x_gel_to_cmm/_J784S4_r5.cmm" R5-MCU AMP1

; Enable GTC for debug timestamps, 0x3=freeze in debug halt
;Data.Set EZAXI:0x00A90000 %LE %Long 0x1

GOSUB CACHE_MMU_INIT

; --------------------------------------------------------------------------------
; pin multiplexing for MMCSD0
GOSUB PIN_MUX

; --------------------------------------------------------------------------------
; Configure MMCSD0 Host controller
Data.Set &MMC_BASE+0x04 %Long 0x00010200
Data.Set &MMC_BASE+0x28 %Long 0x00000B00 ; high speed disable, data_width 1bit
Data.Set &MMC_BASE+0x2C %Long 0x000EF007|(0x3<<25.) ; Reset cmd&data line & set the mmc clock 400khz
WAIT 100.ms

Data.Set &MMC_BASE+0x34 %Long 0x7f0037 ; Enable the Interrupt Status Register Field (&MMC_BASE+0x30)
Data.Set &MMC_BASE+0x38 %Long 0x7f0037 ; Interrupt Enable, Normal Interrupt Signal Enable Register

  GOSUB READ_ID_TEST

  Break.RESet

  FLASHFILE.RESet

//FLASHFILE.CONFIG <MMCSD0_CTL_CFG>  0x0   0x0
  FLASHFILE.CONFIG &MMC_BASE   0x0   0x0

//FLASHFILE.target <Code_range>         <Data_range>           <Algorithm file>
  FLASHFILE.TARGET 0x41C00000++0x2FFF   E:0x41C03000++0x23FF    ~~/demo/arm/flash/byte/emmc_omap.bin  /KEEP  /DualPort  /STACKSIZE 0x200

  Data.Set &MMC_BASE+0x2C %Long 0x000EF007    ;400Khz
  FLASHFILE.GETID

  Data.Set &MMC_BASE+0x2C %Long 0x000E0407    ;25Mhz

//End of the test prepareonly
IF "&arg1"=="PREPAREONLY"
ENDDO

; Re-enable I cache
 Data.Set C15:0x1 %long (Data.Long(C15:0x1)|(0x1<<12.))  ; enable I cache

//Get EXTended CSD registers into scratch memory
  FLASHFILE.GETEXTCSD /VM
//Set EXTCSD 179. to parition that flashfile will write to
; FLASHFILE.SETEXTCSD 179. 0x48  ; access: partition null (UDA)
; FLASHFILE.SETEXTCSD 179. 0x49  ; access: partition boot 1
; FLASHFILE.SETEXTCSD 179. 0x4A  ; access: partition boot 2
; FLASHFILE.SETEXTCSD 179. 0x4B  ; access: rpmb
; FLASHFILE.SETEXTCSD 179. 0x4C  ; access: GPT 1
; FLASHFILE.SETEXTCSD 179. 0x4D  ; access: GPT 2
; FLASHFILE.SETEXTCSD 179. 0x4E  ; access: GPT 3
; FLASHFILE.SETEXTCSD 179. 0x4F  ; access: GPT 4
// Set EXTCSD to the boot width the ROM is expecting
  &csd177=data.byte(VM:177.)
  IF (&csd177&0x3)!=0x2
  (
   //Bit[1:0] : BOOT_BUS_WIDTH (nonvolatile)
   &width=0x2;   ;0x2=8bit, 0x1=4bit, 0x0=0x1 (or other see spec)
   &csd177=(&csd177&0xf8)|&width;
   FLASHFILE.SETEXTCSD 177. &csd177
  )

 FLASHFILE.DUMP 0x0               ; Read eMMC
;FLASHFILE.ERASE 0x0--0xFFFFF     ; Erase eMMC
;FLASHFILE.LOAD * 0x0             ; Write eMMC

ENDDO

READ_ID_TEST:
(
//MMC interface, not SD(HC)
//CMD0
RePeaT 2.
(
Data.Set &MMC_BASE+0x30 %Long 0xFFFFFFFF ;clear status
Data.Set &MMC_BASE+0x8 %Long 0x0  ;arg
Data.Set &MMC_BASE+0xc %Long 0x0  ;cmd
WAIT 10.ms
)

//CMD1
RePeaT 10.
(
Data.Set &MMC_BASE+0x30 %Long 0xFFFFFFFF ;clear status
Data.Set &MMC_BASE+0x8 %Long 0x40FF8000 ;arg
Data.Set &MMC_BASE+0xc %Long 0x01020000 ;cmd1
WAIT 10.ms
&resp=Data.Long(A:(&MMC_BASE+0x10))
//print "CMD1 resp: 0x" &resp
IF (&resp&0x80000000)==0x80000000
(
GOTO jump_cmd2
)
)

PRINT "CMD1 fail"
END

jump_cmd2:
//CMD2
Data.Set &MMC_BASE+0x30 %Long 0xFFFFFFFF ;clear status
Data.Set &MMC_BASE+0x8 %Long 0x0     ;arg
Data.Set &MMC_BASE+0xc %Long 0x02010000 ;cmd2
WAIT 10.ms

//CMD3
Data.Set &MMC_BASE+0x30 %Long 0xFFFFFFFF ;clear status
Data.Set &MMC_BASE+0x8 %Long 0x00010000  ; arg, MMC RCA is (0x0001<<16.)
Data.Set &MMC_BASE+0xc %Long 0x03020000  ;cmd3
WAIT 10.ms

//CMD10
Data.Set &MMC_BASE+0x30 %Long 0xFFFFFFFF ;clear status
Data.Set &MMC_BASE+0x8 %Long 0x00010000 ; arg, MMC RCA is (0x0001<<16.)
Data.Set &MMC_BASE+0xc %Long 0x0A010000  ;cmd10
WAIT 10.ms

//Response2
PRINT "CID register"
PRINT "[127:104] 0x" Data.Long(A:(&MMC_BASE+0x1c))
PRINT "[103:72]  0x" Data.Long(A:(&MMC_BASE+0x18))
PRINT "[71:40]   0x" Data.Long(A:(&MMC_BASE+0x14))
PRINT "[39:8]    0x" Data.Long(A:(&MMC_BASE+0x10))

RETURN
)

PIN_MUX:
(
   ; Dedicted interface for MMC0
   RETURN
)

CACHE_MMU_INIT:
(
	PRIVATE &i &tmpReg

	; Enable MPU and disable I and D caches
	&tmpReg=Data.Long(C15:0x1)
	&tmpReg=&tmpReg|0x1             ; enable MPU
	&tmpReg=(&tmpReg&(~(0x1<<2.)))  ; disable D cache
	&tmpReg=(&tmpReg&(~(0x1<<12.))) ; disable I cache
	Data.Set C15:0x1 %long &tmpReg

	; Configure small region setup for flash programming
	PER.Set.SaveIndex C15:0x026 %Long 0x0 C15:0x016 %Long 0x00000000 ; set default
	PER.Set.SaveIndex C15:0x026 %Long 0x0 C15:0x216 %Long 0x0000003F
	PER.Set.SaveIndex C15:0x026 %Long 0x0 C15:0x416 %Long 0x00001310
	PER.Set.SaveIndex C15:0x026 %Long 0x1 C15:0x016 %Long 0x50000000 ; setup flash window
	PER.Set.SaveIndex C15:0x026 %Long 0x1 C15:0x216 %Long 0x00000035
	PER.Set.SaveIndex C15:0x026 %Long 0x1 C15:0x416 %Long 0x00001301
	PER.Set.SaveIndex C15:0x026 %Long 0x2 C15:0x016 %Long 0x41C00000 ; setup buffer space
	PER.Set.SaveIndex C15:0x026 %Long 0x2 C15:0x216 %Long 0x00000027
	PER.Set.SaveIndex C15:0x026 %Long 0x2 C15:0x416 %Long 0x0000030C
	; Reset all other MPU regions
	&i=3.
	while &i<16.
	(
		PER.Set.SaveIndex C15:0x026 %Long &i C15:0x016 %Long 0x0
		PER.Set.SaveIndex C15:0x026 %Long &i C15:0x216 %Long 0x0
		PER.Set.SaveIndex C15:0x026 %Long &i C15:0x416 %Long 0x0
		&i=&i+1.
	)

	RETURN
)
