; --------------------------------------------------------------------------------
; @Title: Linux Demo for TRACE32 OS Awareness on the ZC702
; @Description: 
;   This batchfile demonstrates the use of the OS Awareness for Linux
;   The example is generated for the ZC702 board using an ICD.
;   It will NOT run on any other board, but may be used as a template
;   for others.
;   Linux is downloaded to the board via ICD.
; @Keywords: awareness, CortexA9, Linux, RTOS, SMP, XILINX, ZC70*, ZYNQ*
; @Author: KJM
; @Board: ZC702
; @Chip: ZYNQ-7000
; @Copyright: (C) 1989-2019 Lauterbach GmbH, licensed for use with TRACE32(R) only
; --------------------------------------------------------------------------------
; $Id: zc702_linux.cmm 15210 2019-11-04 10:51:00Z bschroefel $


  LOCAL &ka_path &awareness
  SCREEN.ALways
 
 
 ; Debugger Reset
 
  WinPAGE.RESet
  AREA.RESet
  WinPOS 0. 25. 75. 8.
  AREA.view
  
  PRINT "resetting..."
 
  RESet
  
  PRINT "Please power on the target"
  
  DO ~~/demo/etc/terminal/serial/term.cmm COM3 115200.
  
  TERM.view
  
  ON powerup GOTO start
  STOP
   
start:
  ON powerup nothing
  &count=8.;
    
  WHILE (&count>0)
  (
      PRINT "Waiting..." &count
      &count=&count-1;
      AREA.CLEAR
      WAIT 1.s
  )
 
 
 ; setup of ICD
  
  SYStem.RESet
 
  SYStem.CPU ZYNQ-7000
  
  SYStem.CONFIG.DAPIRPRE 6.
  SYStem.CONFIG.DAPIRPOST 0.
  SYStem.CONFIG.DAPDRPRE 1.
  SYStem.CONFIG.DAPDRPOST 0.
 
  SYStem.Option DACR ON          ; give Debugger global write permissions
  TrOnchip.Set DABORT OFF        ; used by Linux for page miss!
  TrOnchip.Set PABORT OFF        ; used by Linux for page miss!
  TrOnchip.Set UNDEF OFF         ; my be used by Linux for FPU detection
  TrOnchip.Set RESET OFF
  SYStem.Option MMUSPACES ON     ; enable space ids to virtual addresses
 
  Trace.METHOD Onchip
 
  SYStem.Up
 
  
  Register.RESet
 
  SETUP.IMASKASM ON          ; lock interrupts while single stepping
  
 
 ; Load the Linux kernel
 
  ; If you are using a flashed kernel, or if you're loading 
  ; your kernel via TFTP, use the boot monitor to do so.
  
  ; Use the next lines (loading the kernel, setting registers
  ; and boot parameters) only to load the kernel into
  ; RAM using the debugger.
 
  ; vmlinux starts physically at RAM start (=0x00008000)
  ; We have to adjust it from the virtual start address (=0xc0008000)
  ; i.e.: Data.LOAD.Elf vmlinux <physical start>-<virtual start>
 
  PRINT "loading Linux kernel..."
  Data.LOAD.Elf vmlinux 0x0-0xc0000000  /gnu /NosYmbol /noreg
  
  CORE.select 0
 
  Register.RESet
 
  ; Set PC on start address of image
  Register.Set PC 0x00008000
  
  Register.Set R1 3378.
  
  Data.LOAD.Binary devicetree.dtb 0x01000000
  
 ; Loading initial ram disk (initrd)
 
  ; Use the next lines only, if you want to use an initrd,
  ; and if you want to download this with the debugger.
 
  ; Load ram file system image into ram disk
  ; The load address is either hardcoded in arch/arm/<board>/arch.c,
  ; The load address must be passed as command line option (initrd=0x80500000)
  
  PRINT "loading ramdisk..."
  Data.LOAD.Binary ramdisk.image.gz 0x00800000 /NoClear /NosYmbol
 
 
 ; Load the Linux kernel symbols into the debugger
  ; use /strippart and /path to map source paths, if necessary
  
  PRINT "loading Linux kernel symbols..."
  Data.LOAD.Elf vmlinux /gnu /NoCODE /StripPART "ZC702"
  
 
 ; Open a Code Window -- we like to see something
 
  WinPOS 0. 0. 75. 20.
  List.auto
  SCREEN.display

  
 
 ; Declare the MMU format to the debugger
  ; - table format is "LINUX"
  ; - table base address is at label "swapper_pg_dir"
  ; - kernel address translation
  ; Map the virtual kernel symbols to physical addresses to give 
  ; the debugger access to it before CPU MMU is initialized
 
  PRINT "initializing debugger MMU..."
  MMU.FORMAT LINUX swapper_pg_dir 0xc0000000--0xdfffffff 0x00000000
  TRANSlation.COMMON 0bf000000--0ffffffff            ; common area for kernel and processes
  TRANSlation.TableWalk ON   ; debugger uses a table walk to decode virtual addresses
  TRANSlation.ON           ; switch on debugger(!) address translation 
  
 
 ; Initialize Linux Awareness
 Go.direct start_kernel /Onchip
 
 WAIT !STATE.RUN()
 
  ; Note that the Linux awareness needs the kernel symbols to work
  
 ; check linux major version
 IF STRing.SCAN(Data.STRing(linux_banner), "Linux version 2.", 0)==0
 (
     &ka_path="~~/demo/arm/kernel/linux/linux-2.x"
     &awareness="linux.t32"
 )
 ELSE
 (
     &ka_path="~~/demo/arm/kernel/linux/linux-3.x"
     &awareness="linux3.t32"  
 )
 
  PRINT "initializing RTOS support..."
 TASK.CONFIG &ka_path/&awareness               ; loads Linux awareness 
 MENU.ReProgram &ka_path/linux                 ; loads Linux menu 
 
  ; Group kernel area to be displayed with red bar
  GROUP.Create "kernel" 0xc0000000--0xffffffff /RED
  
 ; Ok, we're done, let's start Linux
  
  Go.direct
  PRINT "starting Linux... (please wait)"
  WAIT 10.s
 
  PRINT "done."     ; done with loading and starting Linux
 
  
 
  ENDDO
 
