# SPDX-License-Identifier: GPL-2.0-or-later

# Xilinx Ultrascale (Kintex, Virtex, Zynq)
# https://www.xilinx.com/support/documentation/user_guides/ug570-ultrascale-configuration.pdf

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

# The various chips in the Ultrascale family have different IR length.
# Set $CHIP before including this file to determine the device.
array set _XCU_DATA {
	XCKU025         {0x03824093  6}
	XCKU035         {0x03823093  6}
	XCKU040         {0x03822093  6}
	XCKU060         {0x03919093  6}
	XCKU060_CIV     {0x0381b093  6}
	XCKU095         {0x03844093  6}
	XCKU095_CIV     {0x03845093  6}
	XCKU3P          {0x04A63093  6}
	XCKU5P          {0x04A62093  6}
	XCKU9P          {0x0484A093  6}
	XCKU11P         {0x04A4E093  6}
	XCKU11P_CIV     {0x04A51093  6}
	XCKU13P         {0x04A52093  6}
	XCKU15P         {0x04A56093  6}
	XCKU15P_CIV     {0x04A59093  6}
	XCVU065         {0x03939093  6}
	XCVU065_CIV     {0x0393b093  6}
	XCVU080         {0x03843093  6}
	XCVU080_CIV     {0x03845093  6}
	XCVU095         {0x03842093  6}
	XCVU2P          {0x04aea093  6}
	XCVU3P          {0x04B39093  6}
	XCVU3P_CIV      {0x04b3d093  6}
	XCAU10P         {0x04AC4033  6}
	XCAU10P_FFVB676 {0x04AC4093  6}
	XCAU15P         {0x04AC2033  6}
	XCAU15P_FFVB676 {0x04AC2093  6}
	XCAU20P         {0x04A65093  6}
	XCAU25P         {0x04A64093  6}
	XCKU5P_CIV      {0x04A64093  6}
	XCKU19P         {0x04ACF093  6}
	XCKU19P_CIV     {0x04AD3093  6}
	XCKU085         {0x0380F093 12}
	XCKU115         {0x0390D093 12}
	XCVU125         {0x0392D093 12}
	XCVU125_CIV     {0x0392f093 12}
	XCVU5P          {0x04B2B093 12}
	XCVU5P_CIV      {0x04b2f093 12}
	XCVU7P          {0x04B29093 12}
	XCVU7P_CIV      {0x04b2d093 12}
	XCVU160         {0x03933093 18}
	XCVU190         {0x03931093 18}
	XCVU440         {0x0396D093 18}
	XCVU440_CIV     {0x0396f093 18}
	XCVU9P          {0x04B31093 18}
	XCVU9P_CIV      {0x04b35093 18}
	XCVU11P         {0x04B49093 18}
	XCVU11P_CIV     {0x04b4f093 18}
	XCU200_FSGD2104 {0x04b37093 18}
	XCU250          {0x04b57093 24}
	XCVU13P         {0x04B51093 24}
	XCVU13P_CIV     {0x04b55093 24}
	XCVU15P         {0x04ba3093 24}
	XCVU19P         {0x04ba1093 24}
	XCVU19P_CIV     {0x04ba5093 24}
}

if { ![info exists CHIP] } {
	error "set CHIP to one of "[concat [array names _XCU_DATA]]
}

if { ![llength [array names _XCU_DATA $CHIP]] } {
	error "unknown CHIP: "$CHIP
}

set _EXPID [lindex $_XCU_DATA($CHIP) 0]
set _IRLEN [lindex $_XCU_DATA($CHIP) 1]

# the 4 top bits (28:31) are the die stepping/revisions. ignore it.
jtag newtap $_CHIPNAME tap -irlen $_IRLEN -ignore-version -expected-id $_EXPID

pld create $_CHIPNAME.pld virtex2 -chain-position $_CHIPNAME.tap -no_jstart

# set the correct instruction codes for jtag hub and
# at least the right code for jprogb, jstart and jshutdown for SSI devices
if { $_IRLEN == 6 } {
	virtex2 set_user_codes $_CHIPNAME.pld 0x2 0x3 0x22 0x23
} elseif {$_IRLEN == 12 } {
	puts "loading bitstream through jtag will not work, but reprogram (refresh)"
	virtex2 set_instr_codes $_CHIPNAME.pld 0x905 0x904 0x2cb 0x30c 0x34d
	virtex2 set_user_codes $_CHIPNAME.pld 0x0a4 0x0e4 0x8a4 0x8e4
} elseif {$_IRLEN == 18 } {
	puts "loading bitstream through jtag will not work, but reprogram (refresh)"
	virtex2 set_instr_codes $_CHIPNAME.pld 0x24905 0x24904 0x0b2cb 0x0c30c 0x0d34d
	virtex2 set_user_codes $_CHIPNAME.pld 0x000a4 0x000e4 0x008a4 0x008e4
} else {
	puts "loading bitstream through jtag will not work, but reprogram (refresh)"
	virtex2 set_instr_codes $_CHIPNAME.pld 0x924905 0x924904 0x2cb2cb 0x30c30c 0x34d34d
	virtex2 set_user_codes $_CHIPNAME.pld 0x0a4924 0x0e4924 0x8a4924 0x8e4924
}

set XCU_JSHUTDOWN 0x0d
set XCU_JPROGRAM 0x0b
set XCU_JSTART 0x0c
set XCU_BYPASS 0x3f

proc xcu_program {tap} {
	echo "DEPRECATED! use 'virtex2 refresh XXXX.pld' not 'xcu_program'"
	global XCU_JSHUTDOWN XCU_JPROGRAM XCU_JSTART XCU_BYPASS
	irscan $tap $XCU_JSHUTDOWN
	irscan $tap $XCU_JPROGRAM
	runtest 60000
	#JSTART prevents this from working...
	#irscan $tap $XCU_JSTART
	runtest 2000
	irscan $tap $XCU_BYPASS
	runtest 2000
}
