# HiSilicon hi3716 configuration, based on AR71xx setup script.

source [find target/Hi3716MV410/hi3716.tcl ]

if { [info exists CHIPNAME] } {
   set  _CHIPNAME $CHIPNAME
} else {
   set  _CHIPNAME hs3716
}

if { [info exists ENDIAN] } {
   set  _ENDIAN $ENDIAN
} else {
  # this defaults to a bigendian
   set  _ENDIAN little
}

if { [info exists CPUTAPID ] } {
   set _CPUTAPID $CPUTAPID
} else {
  # force an error till we get a good number
   set _CPUTAPID 0x4ba00477
}

adapter_nsrst_delay 500
jtag_ntrst_delay 500
adapter_khz 500
	
# hiSilicon only has one ARM cortex A9 tap. If you get the wrong IDCODE (0x12a,....) 
# your box is open and wired for boundary-scan JTAG. You should rewire it.

# the '477' at the end is indicative for ARM debug TAPs.

jtag newtap $CHIPNAME dap -irlen 4 -ircapture 0x1 -irmask 0x0f -expected-id 0x4ba00477
# This is the tap you see when boundary scan is enabled..
#jtag newtap $CHIPNAME tap -irlen 6 -ircapture 0x1 -irmask 0x03 -expected-id 0x1a0025d1

set TARGETNAME $CHIPNAME.dap
# may need to add -endian big|little here ...
target create $TARGETNAME cortex_a -endian little -chain-position $TARGETNAME -dbgbase 0x80110000

# setup working area somewhere in internal RAM
$TARGETNAME configure -work-area-phys 0x80000000 -work-area-size 0x2000

$TARGETNAME configure -event reset-assert-post {
 echo "init debug.."
 cortex_a dbginit
}
$TARGETNAME configure -event reset-assert {
echo "reset init..."
}
#init
#poll off
#read otp read_otp
proc read_otp { {addr 0x0} {len 0x4} } {
#read otp
echo "*********************************************"
echo "*       read otp script.              *"
echo "*       -Author: HI_Security group          *"
echo "*       -usage: read_otp {addr} {len} *"
echo "*********************************************"
set otp_raddr 0xf8ab0018
set otp_rdata 0xf8ab0010
mww 0xf8ab0000 0x2
mww 0xf8ab0004 0x2

set start_addr $addr
set end_addr [expr $len + $start_addr]
for {set i $start_addr} {$i < $end_addr} {set i [expr $i + 0x4]} {
    set i [format "0x%03x"   $i]
    mww $otp_raddr $i
    echo $i
    mdw $otp_rdata    
}
echo "---------------------------------------------"
}
