1+ # ##############################################################################
2+ # # Copyright (C) 2020-2025 Analog Devices, Inc. All rights reserved.
3+ # ## SPDX short identifier: ADIBSD
4+ # ##############################################################################
5+
6+ # # Unified SPI Engine generation script
7+ # # This script provides a single implementation that works for both Xilinx and Intel
8+ # # by using the vendor-agnostic ad_* procedures from adi_board.tcl or <intel_carrier>_system_qsys.tcl
9+
10+ # # Detect the current vendor environment
11+ proc ad_detect_vendor {} {
12+ # Check for Vivado (Xilinx)
13+ if {[info commands get_bd_cells] != " " } {
14+ return " xilinx"
15+ }
16+ # Check for Platform Designer/Qsys (Intel)
17+ if {[info commands add_instance] != " " } {
18+ return " intel"
19+ }
20+ # Default to xilinx for backward compatibility
21+ return " xilinx"
22+ }
23+
24+ proc spi_engine_create {args} {
25+
26+ # Detect vendor using the abstraction layer
27+ set vendor [ad_detect_vendor]
28+
29+ # Parse arguments based on vendor expectations
30+ if {$vendor == " xilinx" } {
31+ # Xilinx: name + optional parameters
32+ set name [lindex $args 0]
33+ set data_width [expr {[llength $args ] > 1 ? [lindex $args 1] : 32}]
34+ set async_spi_clk [expr {[llength $args ] > 2 ? [lindex $args 2] : 1}]
35+ set num_cs [expr {[llength $args ] > 3 ? [lindex $args 3] : 1}]
36+ set num_sdi [expr {[llength $args ] > 4 ? [lindex $args 4] : 1}]
37+ set num_sdo [expr {[llength $args ] > 5 ? [lindex $args 5] : 1}]
38+ set sdi_delay [expr {[llength $args ] > 6 ? [lindex $args 6] : 0}]
39+ set echo_sclk [expr {[llength $args ] > 7 ? [lindex $args 7] : 0}]
40+ set sdo_streaming [expr {[llength $args ] > 8 ? [lindex $args 8] : 0}]
41+ set cmd_mem_addr_width [expr {[llength $args ] > 9 ? [lindex $args 9] : 4}]
42+ set data_mem_addr_width [expr {[llength $args ] > 10 ? [lindex $args 10] : 4}]
43+ set sdi_fifo_addr_width [expr {[llength $args ] > 11 ? [lindex $args 11] : 5}]
44+ set sdo_fifo_addr_width [expr {[llength $args ] > 12 ? [lindex $args 12] : 5}]
45+ set sync_fifo_addr_width [expr {[llength $args ] > 13 ? [lindex $args 13] : 4}]
46+ set cmd_fifo_addr_width [expr {[llength $args ] > 14 ? [lindex $args 14] : 4}]
47+
48+ } elseif {$vendor == " intel" } {
49+ # Intel: name + clocks + optional parameters
50+ if {[llength $args ] < 4} {
51+ error " ERROR: Intel implementation requires at least: name, axi_clk, axi_reset, spi_clk"
52+ }
53+
54+ set name [lindex $args 0]
55+ set axi_clk [lindex $args 1]
56+ set axi_reset [lindex $args 2]
57+ set spi_clk [lindex $args 3]
58+ set data_width [expr {[llength $args ] > 4 ? [lindex $args 4] : 32}]
59+ set async_spi_clk [expr {[llength $args ] > 5 ? [lindex $args 5] : 1}]
60+ set num_cs [expr {[llength $args ] > 6 ? [lindex $args 6] : 1}]
61+ set num_sdi [expr {[llength $args ] > 7 ? [lindex $args 7] : 1}]
62+ set num_sdo [expr {[llength $args ] > 8 ? [lindex $args 8] : 1}]
63+ set sdi_delay [expr {[llength $args ] > 9 ? [lindex $args 9] : 0}]
64+ set echo_sclk [expr {[llength $args ] > 10 ? [lindex $args 10] : 0}]
65+ set sdo_streaming [expr {[llength $args ] > 11 ? [lindex $args 11] : 0}]
66+ set cmd_mem_addr_width [expr {[llength $args ] > 12 ? [lindex $args 12] : 4}]
67+ set data_mem_addr_width [expr {[llength $args ] > 13 ? [lindex $args 13] : 4}]
68+ set sdi_fifo_addr_width [expr {[llength $args ] > 14 ? [lindex $args 14] : 5}]
69+ set sdo_fifo_addr_width [expr {[llength $args ] > 15 ? [lindex $args 15] : 5}]
70+ set sync_fifo_addr_width [expr {[llength $args ] > 16 ? [lindex $args 16] : 4}]
71+ set cmd_fifo_addr_width [expr {[llength $args ] > 17 ? [lindex $args 17] : 4}]
72+ }
73+
74+ # Debug output for Xilinx
75+ if {$vendor == " xilinx" && $echo_sclk == 1} {
76+ puts " echo_sclk: $echo_sclk "
77+ }
78+
79+ # Component instance names
80+ set execution " ${name} _execution"
81+ set axi_regmap " ${name} _axi_regmap"
82+ set offload " ${name} _offload"
83+ set interconnect " ${name} _interconnect"
84+
85+ # Create hierarchy for Xilinx only
86+ if {$vendor == " xilinx" } {
87+ create_bd_cell -type hier $name
88+ current_bd_instance /$name
89+
90+ # Create pins for the hierarchy
91+ if {$async_spi_clk == 1} {
92+ create_bd_pin -dir I -type clk spi_clk
93+ }
94+ if {$echo_sclk == 1} {
95+ create_bd_pin -dir I -type clk echo_sclk
96+ }
97+ create_bd_pin -dir I -type clk clk
98+ create_bd_pin -dir I -type rst resetn
99+ create_bd_pin -dir I trigger
100+ create_bd_pin -dir O irq
101+ create_bd_intf_pin -mode Master -vlnv analog.com:interface:spi_engine_rtl:1.0 m_spi
102+ create_bd_intf_pin -mode Master -vlnv xilinx.com:interface:axis_rtl:1.0 m_axis_sample
103+ if {$sdo_streaming == 1} {
104+ create_bd_intf_pin -mode Slave -vlnv xilinx.com:interface:axis_rtl:1.0 s_axis_sample
105+ }
106+ }
107+
108+ # Create IP instances using vendor-agnostic functions
109+ ad_ip_instance spi_engine_execution $execution
110+ ad_ip_parameter $execution CONFIG.DATA_WIDTH $data_width
111+ ad_ip_parameter $execution CONFIG.NUM_OF_CS $num_cs
112+ ad_ip_parameter $execution CONFIG.NUM_OF_SDI $num_sdi
113+ ad_ip_parameter $execution CONFIG.SDO_DEFAULT 1
114+ ad_ip_parameter $execution CONFIG.SDI_DELAY $sdi_delay
115+ ad_ip_parameter $execution CONFIG.ECHO_SCLK $echo_sclk
116+
117+ ad_ip_instance axi_spi_engine $axi_regmap
118+ ad_ip_parameter $axi_regmap CONFIG.MM_IF_TYPE 0
119+ ad_ip_parameter $axi_regmap CONFIG.DATA_WIDTH $data_width
120+ ad_ip_parameter $axi_regmap CONFIG.NUM_OFFLOAD 1
121+ ad_ip_parameter $axi_regmap CONFIG.NUM_OF_SDI $num_sdi
122+ ad_ip_parameter $axi_regmap CONFIG.ASYNC_SPI_CLK $async_spi_clk
123+ ad_ip_parameter $axi_regmap CONFIG.OFFLOAD0_CMD_MEM_ADDRESS_WIDTH $cmd_mem_addr_width
124+ ad_ip_parameter $axi_regmap CONFIG.OFFLOAD0_SDO_MEM_ADDRESS_WIDTH $data_mem_addr_width
125+ ad_ip_parameter $axi_regmap CONFIG.SDI_FIFO_ADDRESS_WIDTH $sdi_fifo_addr_width
126+ ad_ip_parameter $axi_regmap CONFIG.SDO_FIFO_ADDRESS_WIDTH $sdo_fifo_addr_width
127+ ad_ip_parameter $axi_regmap CONFIG.SYNC_FIFO_ADDRESS_WIDTH $sync_fifo_addr_width
128+ ad_ip_parameter $axi_regmap CONFIG.CMD_FIFO_ADDRESS_WIDTH $cmd_fifo_addr_width
129+
130+ ad_ip_instance spi_engine_offload $offload
131+ ad_ip_parameter $offload CONFIG.DATA_WIDTH $data_width
132+ ad_ip_parameter $offload CONFIG.ASYNC_SPI_CLK 0
133+ ad_ip_parameter $offload CONFIG.NUM_OF_SDI $num_sdi
134+ ad_ip_parameter $offload CONFIG.CMD_MEM_ADDRESS_WIDTH $cmd_mem_addr_width
135+ ad_ip_parameter $offload CONFIG.SDO_MEM_ADDRESS_WIDTH $data_mem_addr_width
136+ ad_ip_parameter $offload CONFIG.SDO_STREAMING $sdo_streaming
137+
138+ # Intel-specific: Set ASYNC_TRIG parameter if needed
139+ if {$vendor == " intel" } {
140+ ad_ip_parameter $offload CONFIG.ASYNC_TRIG 0
141+ }
142+
143+ ad_ip_instance spi_engine_interconnect $interconnect
144+ ad_ip_parameter $interconnect CONFIG.DATA_WIDTH $data_width
145+ ad_ip_parameter $interconnect CONFIG.NUM_OF_SDI $num_sdi
146+
147+ # Create connections based on vendor
148+ if {$vendor == " xilinx" } {
149+ # Xilinx connection style (hierarchical with internal pins)
150+ ad_connect $axi_regmap /spi_engine_offload_ctrl0 $offload /spi_engine_offload_ctrl
151+ ad_connect $offload /m_interconnect_ctrl $interconnect /s_interconnect_ctrl
152+ ad_connect $offload /spi_engine_ctrl $interconnect /s0_ctrl
153+ ad_connect $axi_regmap /spi_engine_ctrl $interconnect /s1_ctrl
154+ ad_connect $interconnect /m_ctrl $execution /ctrl
155+ ad_connect $offload /offload_sdi m_axis_sample
156+ ad_connect $offload /trigger trigger
157+ ad_connect $execution /spi m_spi
158+
159+ if {$sdo_streaming == 1} {
160+ ad_connect $offload /s_axis_sdo s_axis_sample
161+ }
162+
163+ # Clock connections
164+ ad_connect clk $axi_regmap /s_axi_aclk
165+
166+ if {$async_spi_clk == 1} {
167+ ad_connect spi_clk $offload /spi_clk
168+ ad_connect spi_clk $offload /ctrl_clk
169+ ad_connect spi_clk $execution /clk
170+ ad_connect spi_clk $axi_regmap /spi_clk
171+ ad_connect spi_clk $interconnect /clk
172+ } else {
173+ ad_connect clk $offload /spi_clk
174+ ad_connect clk $offload /ctrl_clk
175+ ad_connect clk $execution /clk
176+ ad_connect clk $axi_regmap /spi_clk
177+ ad_connect clk $interconnect /clk
178+ }
179+
180+ if {$echo_sclk == 1} {
181+ ad_connect echo_sclk $execution /echo_sclk
182+ }
183+
184+ # Reset connections
185+ ad_connect $axi_regmap /spi_resetn $offload /spi_resetn
186+ ad_connect $axi_regmap /spi_resetn $execution /resetn
187+ ad_connect $axi_regmap /spi_resetn $interconnect /resetn
188+ ad_connect resetn $axi_regmap /s_axi_aresetn
189+ ad_connect irq $axi_regmap /irq
190+
191+ # Exit hierarchy
192+ current_bd_instance /
193+
194+ } elseif {$vendor == " intel" } {
195+ # Intel connection style (flat with different interface naming)
196+
197+ # Clock connections
198+ ad_connect $axi_clk $axi_regmap .s_axi_clock
199+ ad_connect $spi_clk $axi_regmap .if_spi_clk
200+ ad_connect $spi_clk $execution .if_clk
201+ ad_connect $spi_clk $interconnect .if_clk
202+ ad_connect $spi_clk $offload .if_ctrl_clk
203+ ad_connect $spi_clk $offload .if_spi_clk
204+
205+ # Reset connections
206+ ad_connect $axi_reset $axi_regmap .s_axi_reset
207+ ad_connect $axi_regmap .if_spi_resetn $execution .if_resetn
208+ ad_connect $axi_regmap .if_spi_resetn $interconnect .if_resetn
209+ ad_connect $axi_regmap .if_spi_resetn $offload .if_spi_resetn
210+
211+ # Data path connections
212+ ad_connect $interconnect .m_cmd $execution .cmd
213+ ad_connect $execution .sdi_data $interconnect .m_sdi
214+ ad_connect $interconnect .m_sdo $execution .sdo_data
215+ ad_connect $execution .sync $interconnect .m_sync
216+ ad_connect $axi_regmap .cmd $interconnect .s1_cmd
217+ ad_connect $interconnect .s1_sdi $axi_regmap .sdi_data
218+ ad_connect $axi_regmap .sdo_data $interconnect .s1_sdo
219+ ad_connect $interconnect .s1_sync $axi_regmap .sync
220+ ad_connect $offload .cmd $interconnect .s0_cmd
221+ ad_connect $interconnect .s0_sdi $offload .sdi_data
222+ ad_connect $offload .sdo_data $interconnect .s0_sdo
223+ ad_connect $interconnect .s0_sync $offload .sync
224+ ad_connect $offload .m_interconnect_ctrl $interconnect .s_interconnect_ctrl
225+ ad_connect $offload .ctrl_cmd_wr $axi_regmap .offload0_cmd
226+ ad_connect $offload .ctrl_sdo_wr $axi_regmap .offload0_sdo
227+ ad_connect $offload .if_ctrl_enable $axi_regmap .if_offload0_enable
228+ ad_connect $offload .if_ctrl_enabled $axi_regmap .if_offload0_enabled
229+ ad_connect $offload .if_ctrl_mem_reset $axi_regmap .if_offload0_mem_reset
230+ ad_connect $offload .status_sync $axi_regmap .offload_sync
231+ }
232+ }
0 commit comments