source [find target/Hi3716MV420/Hi3716MV420_otp_STBM.cfg]

proc write_jtag_prt_mode {{flag 0x0}} {
    puts stdout "************************************************"
	puts stdout "*          write jtag_prt_mode                 *"
	puts stdout "************************************************"
	
	if {$flag == 0} {
    	#write byte 0xae 0x42
		set byteae 0x42
		write_otp_byte 0xae $byteae
	} else {
		#write byte 0x18
		set bit18_1 [expr 0x1 << 1]
		set byte18 $bit18_1
		write_otp_byte 0x18 $byte18	
		
		#write byte 0xae 0xff
		set byteae 0xff
		write_otp_byte 0xae $byteae
	}

}
proc write_uart {{flag 0x0}} {
    puts stdout "************************************************"
	puts stdout "*          write uart                  *"
	puts stdout "************************************************"
	
	if {$flag != 0} {
	    #write byte 0x404
		set bit404_4 [expr 0x1 << 4]
		set bit404_5 [expr 0x1 << 5]
		set byte404 [expr $bit404_4 | $bit404_5]
		write_otp_byte 0x404 $byte404
    }
	#write byte 0x414
	set bit414_4 [expr 0x1 << 4]
	set bit414_5 [expr 0x1 << 5]	
	set byte414 [expr $bit414_4 | $bit414_5]
	write_otp_byte 0x414 $byte414
}

proc write_self_boot {{flag 0x0}} {
    puts stdout "************************************************"
	puts stdout "*          write uart disable                  *"
	puts stdout "************************************************"
	
	if {$flag == 0} {
	    set  byte1c 0x11
	} else {
		#write byte 0
		set bit0_3 [expr 0x1 << 3]
		set byte0 $bit0_3
		write_otp_byte 0x0 $byte0
		
		#write byte 0x1c
		set  byte1c 0xff
	}
	
	#write byte 8
	set bit8_3 [expr 0x1 << 3]
	set byte8 $bit8_3
	write_otp_byte 0x8 $byte8
	
	write_otp_byte 0x1c $byte1c
	
}

proc write_SCS_activation {{flag 0x0}} {
	#write byte 0xad
	if {$flag != 0} {
		#write byte 0x18
		set bit18_0 0x1
		set byte18 $bit18_0 
		write_otp_byte 0x18 $byte18	
	
		set bytead 0xff
	} else {
		set bytead 0x81
	}
	write_otp_byte 0xad $bytead
}

proc write_otp_other_1 {} {
    #write ts_csa2_hardonly_en、ts_sp_hardonly_en、ts_csa3_hardonly_en
	#write byte 1
	set bit1_2 [expr 1 << 2]
	set bit1_3 [expr 1 << 3] 
	set bit1_6 [expr 1 << 6]
	set byte1 [expr $bit1_2 | $bit1_3 | $bit1_6]
	write_otp_byte 0x1 $byte1	

	#write byte 9
	set bit9_2 [expr 0x1 << 2]
	set bit9_3 [expr 0x1 << 3]
	set bit9_6 [expr 0x1 << 6]
	set byte9 [expr $bit9_2 | $bit9_3 | $bit9_6]
	write_otp_byte 0x9 $byte9	
}

proc write_otp_other_2 {} {
	#pcie_disable sf_disable usb2_disable gmii_disable
	
	global pcie_disable
	global sf_disable
	global gmii_disable

	#write byte 0x404
	set bit404_0 [expr $pcie_disable & 0x1]
	set bit404_3 [expr ($sf_disable & 0x1) << 3]
	set byte404 [expr $bit404_0 | $bit404_3 ]
	write_otp_byte 0x404 $byte404
	
	#write byte 0x409
	set bit409_1 [expr ($gmii_disable & 0x1) << 1]
	set byte409 $bit409_1
	write_otp_byte 0x409 $byte409
	
	#write byte 0x414
	set bit414_0 [expr 0x1]
	set bit414_3 [expr 0x1 << 3]
	set byte414 [expr $bit414_0 | $bit414_3]
	write_otp_byte 0x414 $byte414
	
	#write byte 0x416
	set bit416_3 [expr 0x1 << 3]
	set byte416 $bit416_3
	write_otp_byte 0x416 $byte416
	
	#write byte 0x419
	set bit419_1 [expr 0x1 << 1]
	set byte419 $bit419_1
	write_otp_byte 0x419 $byte419
}

proc write_otp_other_3 {} {
    # runtime_check_en wakeup_ddr_check_en
	
	#write byte 6
	set bit6_4 [expr 0x1 << 4]
	set byte6 $bit6_4
	write_otp_byte 0x6 $byte6
	
	#write byte 7
	set bit7_0 0x1
	set byte7 $bit7_0
	write_otp_byte 0x7 $byte7
	
	#write byte 0xe
	set bite_4 [expr 0x1 << 4]
	set bytee  $bite_4
	write_otp_byte 0xe $bytee
	
	#write byte 0xf
	set bitf_0 0x1
	set bytef $bitf_0
	write_otp_byte 0xf $bytef
}

proc write_otp_other_4 {} {
    puts stdout "************************************************"
	puts stdout "*            write otp other                   *"
	puts stdout "************************************************"
	global boot_mode_sel
	global bload_dec_en
	global ddr_wakeup_disable
	global bload_enc_disable
	global version_id_check_en
	global bl_msid_check_en
	global STB_SN_0
	global MSID
	global Version_ID
	global HDCP_RootKey_0_31
	global HDCP_RootKey_32_63
	global HDCP_RootKey_64_95
	global HDCP_RootKey_96_127

	#write byte 0
	set bit0_0 [expr $boot_mode_sel & 0x1]
	set bit0_1 [expr $boot_mode_sel & 0x2]
	set bit0_2 [expr ($bload_dec_en & 0x1) << 2]
	set bit0_4 [expr ($ddr_wakeup_disable & 0x1) << 4]
	set byte0 [expr $bit0_0 | $bit0_1 | $bit0_2 | $bit0_4]
	write_otp_byte 0x0 $byte0
	

	#write byte 3
	set bit3_4 [expr 0x1 << 4]
	write_otp_byte 0x3 $bit3_4
	
	#write byte 5
	set byte5 [expr ($boot_mode_sel & 0x4) << 3]
	if {$byte5 != 0} {
		write_otp_byte 0x5 $byte5
	}
	
	#write byte 6
	set bit6_2 [expr 0x1 << 2]
	set bit6_3 [expr 0x1 << 3]
	set byte6 [expr $bit6_2 | $bit6_3]
	write_otp_byte 0x6 $byte6
	
	#write byte 7
	set bit7_2 [expr ($version_id_check_en & 0x1) << 2]
	set bit7_3 [expr ($bl_msid_check_en & 0x1) << 3]
	set byte7 [expr $bit7_2 | $bit7_3]
	write_otp_byte 0x7 $byte7

	#write byte 8
	set bit8_0 [expr 0x1]
	set bit8_1 [expr 0x1 << 1]
	set bit8_2 [expr ($bload_dec_en & 0x1) << 2]
	set bit8_4 [expr 0x1 << 4]
	set byte8 [expr $bit8_0 | $bit8_1 | $bit8_2 | $bit8_4]
	write_otp_byte 0x8 $byte8


	#write byte 0xb
	set bitb_4 [expr 0x1 << 4]
	set byteb $bitb_4
	write_otp_byte 0xb $byteb
	
	#write byte 0xc
	set bitc_1 [expr 0x1 << 1]
	set bitc_2 [expr 0x1 << 2]
	set bytec [expr $bitc_1 | $bitc_2]
	write_otp_byte 0xc $bytec

	#write byte 0xd
	set bitd_5 [expr 0x1 << 5]
	set byted $bitd_5
	write_otp_byte 0xd $byted


	#write byte 0xe
	set bite_2 [expr 0x1 << 2]
	set bite_3 [expr 0x1 << 3]
	set bytee [expr $bite_2 | $bite_3]
	write_otp_byte 0xe $bytee

	#write byte 0xf
	set bitf_2 [expr ($version_id_check_en & 0x1) << 2]
	set bitf_3 [expr ($bl_msid_check_en & 0x1) << 3]
	set bytef [expr $bitf_2 | $bitf_3 ]
	write_otp_byte 0xf $bytef

	#write STB_SN_0 ,byte 0x90-0x93
	for {set i 0} {$i < 4} {incr i} {
		set byteai [expr $STB_SN_0 & 0xff]
		write_otp_byte [expr 0x90 + $i] $byteai
		set STB_SN_0 [expr $STB_SN_0 >> 8]		
	}
	#write STB_SN_0_lock, byte 0x10
	set bit10_4 [expr 0x1 << 4]
	set byte10 $bit10_4
	write_otp_byte 0x10 $byte10
	
	#write MSID, byte 0xa0-0xa3
	for {set i 0} {$i < 4} {incr i} {
		set byteai [expr $MSID & 0xff]
		write_otp_byte [expr 0xa0 + $i] $byteai
		set MSID [expr $MSID >> 8]		
	}
	#write msid_lock
	if {$MSID != 0} {
		set bit11_0 0x1
		set byte11 $bit11_0
		write_otp_byte 0x11 $byte11
	}
	
	#write Version ID, byte 0xa4-0xa7
	for {set i 0} {$i < 4} {incr i} {
		set byteai [expr $Version_ID & 0xff]
		write_otp_byte [expr 0xa4 + $i] $byteai
		set Version_ID [expr $Version_ID >> 8]		
	}
	
	#write HDCP_RootKey, byte 0xc0-0xcf
	if { $HDCP_RootKey_0_31 != 0 } {
		for {set i 0} {$i < 4} {incr i} {
			set byteai [expr $HDCP_RootKey_0_31 & 0xff]
			write_otp_byte [expr 0xc0 + $i] $byteai
			set HDCP_RootKey_0_31 [expr $HDCP_RootKey_0_31 >> 8]		
		}	
	}
	if { $HDCP_RootKey_32_63 != 0 } {
		for {set i 0} {$i < 4} {incr i} {
			set byteai [expr $HDCP_RootKey_32_63 & 0xff]
			write_otp_byte [expr 0xc4 + $i] $byteai
			set HDCP_RootKey_32_63 [expr $HDCP_RootKey_32_63 >> 8]		
		}
	}
	if { $HDCP_RootKey_64_95 != 0 } {
		for {set i 0} {$i < 4} {incr i} {
			set byteai [expr $HDCP_RootKey_64_95 & 0xff]
			write_otp_byte [expr 0xc8 + $i] $byteai
			set HDCP_RootKey_64_95 [expr $HDCP_RootKey_64_95 >> 8]		
		}
	}
	if { $HDCP_RootKey_96_127 != 0 } {
		for {set i 0} {$i < 4} {incr i} {
			set byteai [expr $HDCP_RootKey_96_127 & 0xff]
			write_otp_byte [expr 0xcc + $i] $byteai
			set HDCP_RootKey_96_127 [expr $HDCP_RootKey_96_127 >> 8]		
		}
	}
	#write HDCP_RootKey_lock
	if {$HDCP_RootKey_0_31 != 0 || $HDCP_RootKey_32_63 != 0 || $HDCP_RootKey_64_95 != 0 || $HDCP_RootKey_96_127 != 0} {
		set bit12_0 0x1
		set byte12 $bit12_0
		write_otp_byte 0x12 $byte12
	}
}

proc write_otp_all {} {
	global jtag_prt_mode_0
	global uart_disable
	global self_boot_disable
	global SCS_activation
	
	write_otp_other_1
	write_otp_other_2
	write_otp_other_3
	write_otp_other_4
	
	write_SCS_activation $SCS_activation
	
	write_uart $uart_disable
	
    write_self_boot $self_boot_disable
	
	write_jtag_prt_mode $jtag_prt_mode_0
}