; --------------------------------------------------------------------------------
; @Title: Linux Demo for TRACE32 OS Awareness
; @Description: 
;   
;   This batchfile demonstrates the use of the OS Awareness for Linux
;   
;   The example is generated for an Artis 8540 board using an ICD.
;   It will NOT run on any other board, but may be used as a template
;   for others.
;   
; @Keywords: artis8540, awareness, linux, RTOS
; @Author: DIE
; @Board: Artis-8540
; @Chip: MPC8540
; @Copyright: (C) 1989-2015 Lauterbach GmbH, licensed for use with TRACE32(R) only
; --------------------------------------------------------------------------------
; $Id: linux.cmm 13987 2019-02-19 16:15:20Z kjmal $


; ATTENTION:
;   1) The debugger needs the MSR:DE bit (debug exception enable)
;      to be set in order to work correctly.
;   2) The DOZE mode must be switched off, otherwise the debugger
;      will halt on every DOZE entry.
; The above prerequisites cause the following changes:
;   Linux kernel 2.4:
;     arch/ppc/kernel/head_e500.S: set MSR:DE bit at MSR initialization
;       (two times before rfi) and disable the DOZE mode
;     include/asm/processor.h: change MSR_KERNEL definition to include MSR_DE
;   Linux kernel 2.6.9:
;     arch/ppc/kernel/head_e500.S: in _start section 3. :
;       "ori r6,r6,0x200 /* set DE bit */" before "bl 1f"
;     arch/ppc/kernel/head_e500.S: in _start section 7. :
;       "li r7,0x200" instead of "li r7,0"
;     include/asm/reg_booke.h: change MSR_KERNEL definition to include MSR_DE
;   Linux kernel 2.6.12:
;     arch/ppc/kernel/head_fsl_booke.S: in _start section 3. :
;       "ori r6,r6,0x200 /* set DE bit */" before "bl 1f"
;     include/asm/reg_booke.h: change MSR_KERNEL definition to include MSR_DE

; Starting Linux example with TRACE32 and console terminal:
; - Connect STRAIGHT cable to serial interface
; - Start TRACE32
; - Open serial terminal: 115200 baud, 8/N/1, no(!) handshake
; - TRACE32: "do linux"
; - terminal: start "hello"

 LOCAL &ka_path &awareness
 LOCAL &srcdrive
 LOCAL &srcpath
 &srcdrive="S:"
 &srcpath="&srcdrive"+"\"


 SCREEN.ON


; Debugger Reset

 WinPAGE.RESet
 AREA.RESet
 WinPOS 0. 20. 70. 9. 0. 0. W000
 AREA.view
 
 PRINT "resetting..."

 SYStem.Down
 TASK.RESet
 Break.RESet
 MAP.RESet
 sYmbol.RESet
 TRANSlation.RESet
 

; Initializing Debugger

 PRINT "initializing..."
 SYStem.CPU MPC8540
 SYStem.Option SlowReset OFF    ; hardware dependent (see manual)
 SYStem.Option Freeze OFF       ; hardware dependent (see manual)
 SYStem.Option MMUSPACES ON     ; enable space ids to virtual addresses
 SYStem.Up
 Register.RESet
 SETUP.IMASKASM ON              ; lock interrupts while single stepping
 

; Target Setup: initialize DRAM controller and peripherals
 PRINT "target setup..."
 
 ; Either let the boot monitor setup the board
    ; go
    ; wait 1.s
    ; break
 ; or use the debugger to initialize it
    ; setup Artis8540 board
    DO a85xx_init
   

; 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 linux image, setting 
 ; registers and board info structure) only when
 ; loading the kernel into RAM using the debugger.

 ; vmlinux starts physically at RAM start.
 ; We have to adjust it from the virtual start address 
 ; to the physical start address
 ; i.e.: Data.LOAD.Elf vmlinux <physical start>-<virtual start>
 
 PRINT "loading Linux kernel..."
 Data.LOAD.Elf vmlinux 0x0-0xc0000000 /NosYmbol /noreg
 
 Register.RESet

 ; set PC on start address of image
 Register.Set IP 0
 
 ; set Debug Enable bit
 Register.Set DE 1
 
 ; prepare peripheral
 PER.Set.simple EA:IOBASE() %Long 0x000f0000  ; CCSRBAR: IO base address 0xf0000000
 PER.Set.simple SPR:0x3F0 %Long Data.Long(spr:0x3f0)|0x4000 ; set HID0:TBEN (time base enable)
  
 ; Fill in board info (bd_t structure) for linux boot
 DO board_info

 ; setup command line arguments

 Data.Set 0x200100 "console=ttyS0,115200 root=/dev/ram" 0

 Register.Set r6 0x200100      ; R6 points to command line
 Register.Set r7 0x2001ff      ; R7 points to end of command line


; 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/ppc/<board>/arch.c,
 ; or must be passed by a boot parameter, or as command line option
 ; Load initial ram disk (initrd)

 PRINT "loading ramdisk..."
 Data.LOAD.Binary ramdisk.image.gz 0x0300000 /NoClear /NosYmbol
   
 Register.Set r4 0x0300000     ; R4 points to initrd
 Register.Set r5 0x0480000     ; R5 points to end of initrd
 

; Load the Linux kernel symbols into the debugger
 ; /strippart and /path are used to map source paths
 
 PRINT "loading Linux kernel symbols..."
 Data.LOAD.Elf vmlinux /gnu /NoCODE /StripPART 3 /PATH "&srcpath" 
 sYmbol.CLEANUP                ; clean up symbol types, necessary!
 

; 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--0xc1ffffff 0x00000000
 TRANSlation.COMMON 0xC0000000--0xffffffff              ; 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

 ; 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/powerpc/kernel/linux/linux-2.x"
     &awareness="linux.t32"
 )
 ELSE
 (
     &ka_path="~~/demo/powerpc/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 6.s
 Break.direct
 

; Scan TLB0 tables for basic translations

 PRINT "scanning TLB0 MMU tables..."
 MMU.TLB0.SCAN      ; scan basic address translations
 TRANSlation.CLEANUP        ; remove double entries (necessary!)
 
 PRINT "done."     ; done with loading and starting Linux
 
; -------------------------------------------------------------------------------- 
;       Application Debugging 
;           e.g. "hello" 
; --------------------------------------------------------------------------------

 ; activate autoloader for processes:
 TASK.sYmbol.Option AutoLoad Process

 PRINT "please log in and start 'hello'"

 ; script based
   ; using the script based application debugging
   ; gives more control over the single commands
   ; see file "app_debug.cmm"
   
   ;LOCAL &extdir
   ;&extdir=TASK.GETDIR()
   ;DO &extdir/app_debug hello
   
 ; process watch system
   ; the process watch system automates the actions
   ; for application debugging
   
   WinPOS 55. 25. 40. 4.
   TASK.Watch.View "hello"

   Go.direct

 ENDDO
 
