Merge branch 'wip/wxgui' of http://gnuradio.org/git/jblum
[debian/gnuradio] / usrp2 / fpga / top / u2_fpga / u2_fpga_top.v
1 `timescale 1ns / 1ps
2 //////////////////////////////////////////////////////////////////////////////////
3
4 module u2_fpga_top
5   (
6    // Misc, debug
7    output led1,
8    output led2,
9    output [31:0] debug,
10    output [1:0] debug_clk,
11
12    // Expansion
13    input exp_pps_in_p, // Diff
14    input exp_pps_in_n, // Diff
15    output exp_pps_out_p, // Diff 
16    output exp_pps_out_n, // Diff 
17    
18    // GMII
19    //   GMII-CTRL
20    input GMII_COL,
21    input GMII_CRS,
22
23    //   GMII-TX
24    output reg [7:0] GMII_TXD,
25    output reg GMII_TX_EN,
26    output reg GMII_TX_ER,
27    output GMII_GTX_CLK,
28    input GMII_TX_CLK,  // 100mbps clk
29
30    //   GMII-RX
31    input [7:0] GMII_RXD,
32    input GMII_RX_CLK,
33    input GMII_RX_DV,
34    input GMII_RX_ER,
35
36    //   GMII-Management
37    inout MDIO,
38    output MDC,
39    input PHY_INTn,   // open drain
40    output PHY_RESETn,
41    input PHY_CLK,   // possibly use on-board osc
42
43    // RAM
44    inout [17:0] RAM_D,
45    output [18:0] RAM_A,
46    output RAM_CE1n,
47    output RAM_CENn,
48    output RAM_CLK,
49    output RAM_WEn,
50    output RAM_OEn,
51    output RAM_LDn,
52    
53    // SERDES
54    output ser_enable,
55    output ser_prbsen,
56    output ser_loopen,
57    output ser_rx_en,
58    
59    output ser_tx_clk,
60    output reg [15:0] ser_t,
61    output reg ser_tklsb,
62    output reg ser_tkmsb,
63
64    input ser_rx_clk,
65    input [15:0] ser_r,
66    input ser_rklsb,
67    input ser_rkmsb,
68    
69    // CPLD interface
70    output cpld_start,  // AA9
71    output cpld_mode,   // U12
72    output cpld_done,   // V12
73    input cpld_din,     // AA14 Now shared with CFG_Din
74    input cpld_clk,     // AB14 serial clock
75
76    // ADC
77    input [13:0] adc_a,
78    input adc_ovf_a,
79    output adc_oen_a,
80    output adc_pdn_a,
81    
82    input [13:0] adc_b,
83    input adc_ovf_b,
84    output adc_oen_b,
85    output adc_pdn_b,
86    
87    // DAC
88    output [15:0] dac_a,
89    output [15:0] dac_b,
90
91    // I2C
92    inout SCL,
93    inout SDA,
94    input SCL_force,
95    input SDA_force,
96
97    // Clock Gen Control
98    output [1:0] clk_en,
99    output [1:0] clk_sel,
100    input clk_func,        // FIXME is an input to control the 9510
101    input clk_status,
102
103    // Clocks
104    input clk_fpga_p,  // Diff
105    input clk_fpga_n,  // Diff
106    input clk_to_mac,
107    input pps_in,
108    
109    // Generic SPI
110    output sclk,
111    output sen_clk,
112    output sen_dac,
113    output sdi,
114    input sdo,
115    
116    // TX DBoard
117    output sen_tx_db,
118    output sclk_tx_db,
119    input sdo_tx_db,
120    output sdi_tx_db,
121
122    output sen_tx_adc,
123    output sclk_tx_adc,
124    input sdo_tx_adc,
125    output sdi_tx_adc,
126
127    output sen_tx_dac,
128    output sclk_tx_dac,
129    output sdi_tx_dac,
130
131    inout [15:0] io_tx,
132
133    // RX DBoard
134    output sen_rx_db,
135    output sclk_rx_db,
136    input sdo_rx_db,
137    output sdi_rx_db,
138
139    output sen_rx_adc,
140    output sclk_rx_adc,
141    input sdo_rx_adc,
142    output sdi_rx_adc,
143
144    output sen_rx_dac,
145    output sclk_rx_dac,
146    output sdi_rx_dac,
147    
148    inout [15:0] io_rx
149    );
150
151    // FPGA-specific pins connections
152    wire         aux_clk = PHY_CLK;
153    //wire       cpld_detached = RAM_A[14]; // FIXME  Hacked on with Blue Wire
154    wire         cpld_detached = SDA_force; // FIXME  Hacked on with Blue Wire
155
156    wire         clk_fpga, dsp_clk, clk_div, dcm_out, wb_clk, clock_ready;
157
158    IBUFGDS clk_fpga_pin (.O(clk_fpga),.I(clk_fpga_p),.IB(clk_fpga_n));
159    defparam     clk_fpga_pin.IOSTANDARD = "LVPECL_25";
160    
161    wire         exp_pps_in;
162    IBUFDS exp_pps_in_pin (.O(exp_pps_in),.I(exp_pps_in_p),.IB(exp_pps_in_n));
163    defparam     exp_pps_in_pin.IOSTANDARD = "LVDS_25";
164    
165    wire         exp_pps_out;
166    OBUFDS exp_pps_out_pin (.O(exp_pps_out_p),.OB(exp_pps_out_n),.I(exp_pps_out));
167    defparam     exp_pps_out_pin.IOSTANDARD = "LVDS_25";
168
169    reg [5:0]    clock_ready_d;
170    always @(posedge aux_clk)
171      clock_ready_d[5:0] <= {clock_ready_d[4:0],clock_ready};
172
173    wire         dcm_rst = ~&clock_ready_d & |clock_ready_d;
174    wire         clk_muxed = clock_ready ? clk_fpga : aux_clk;
175
176    wire         adc_on_a, adc_on_b, adc_oe_a, adc_oe_b;
177    assign       adc_oen_a = ~adc_oe_a;
178    assign       adc_oen_b = ~adc_oe_b;
179    assign       adc_pdn_a = ~adc_on_a;  
180    assign       adc_pdn_b = ~adc_on_b;  
181
182    // Handle Clocks
183    DCM DCM_INST (.CLKFB(dsp_clk), 
184                  .CLKIN(clk_muxed), 
185                  .DSSEN(0), 
186                  .PSCLK(0), 
187                  .PSEN(0), 
188                  .PSINCDEC(0), 
189                  .RST(dcm_rst), 
190                  .CLKDV(clk_div), 
191                  .CLKFX(), 
192                  .CLKFX180(), 
193                  .CLK0(dcm_out), 
194                  .CLK2X(), 
195                  .CLK2X180(), 
196                  .CLK90(), 
197                  .CLK180(), 
198                  .CLK270(), 
199                  .LOCKED(LOCKED_OUT), 
200                  .PSDONE(), 
201                  .STATUS());
202    defparam DCM_INST.CLK_FEEDBACK = "1X";
203    defparam DCM_INST.CLKDV_DIVIDE = 2.0;
204    defparam DCM_INST.CLKFX_DIVIDE = 1;
205    defparam DCM_INST.CLKFX_MULTIPLY = 4;
206    defparam DCM_INST.CLKIN_DIVIDE_BY_2 = "FALSE";
207    defparam DCM_INST.CLKIN_PERIOD = 10.000;
208    defparam DCM_INST.CLKOUT_PHASE_SHIFT = "NONE";
209    defparam DCM_INST.DESKEW_ADJUST = "SYSTEM_SYNCHRONOUS";
210    defparam DCM_INST.DFS_FREQUENCY_MODE = "LOW";
211    defparam DCM_INST.DLL_FREQUENCY_MODE = "LOW";
212    defparam DCM_INST.DUTY_CYCLE_CORRECTION = "TRUE";
213    defparam DCM_INST.FACTORY_JF = 16'h8080;
214    defparam DCM_INST.PHASE_SHIFT = 0;
215    defparam DCM_INST.STARTUP_WAIT = "FALSE";
216
217    BUFG dspclk_BUFG (.I(dcm_out), .O(dsp_clk));
218    BUFG wbclk_BUFG (.I(clk_div), .O(wb_clk));
219
220    // I2C -- Don't use external transistors for open drain, the FPGA implements this
221    IOBUF scl_pin(.O(scl_pad_i), .IO(SCL), .I(scl_pad_o), .T(scl_pad_oen_o));
222    IOBUF sda_pin(.O(sda_pad_i), .IO(SDA), .I(sda_pad_o), .T(sda_pad_oen_o));
223
224    // LEDs are active low outputs
225    wire         led1_int, led2_int;
226    assign       led1 = ~led1_int;
227    assign       led2 = ~led2_int;
228
229    // SPI
230    wire         miso, mosi, sclk_int;
231    assign       {sclk,sdi} = (~sen_clk | ~sen_dac) ? {sclk_int,mosi} : 2'b0;
232    assign       {sclk_tx_db,sdi_tx_db} = ~sen_tx_db ? {sclk_int,mosi} : 2'b0;
233    assign       {sclk_tx_dac,sdi_tx_dac} = ~sen_tx_dac ? {sclk_int,mosi} : 2'b0;
234    assign       {sclk_tx_adc,sdi_tx_adc} = ~sen_tx_adc ? {sclk_int,mosi} : 2'b0;
235    assign       {sclk_rx_db,sdi_rx_db} = ~sen_rx_db ? {sclk_int,mosi} : 2'b0;
236    assign       {sclk_rx_dac,sdi_rx_dac} = ~sen_rx_dac ? {sclk_int,mosi} : 2'b0;
237    assign       {sclk_rx_adc,sdi_rx_adc} = ~sen_rx_adc ? {sclk_int,mosi} : 2'b0;
238    
239    assign       miso = (~sen_clk & sdo) | (~sen_dac & sdo) | 
240                 (~sen_tx_db & sdo_tx_db) | (~sen_tx_adc & sdo_tx_adc) |
241                 (~sen_rx_db & sdo_rx_db) | (~sen_rx_adc & sdo_rx_adc);
242
243    wire         GMII_TX_EN_unreg, GMII_TX_ER_unreg;
244    wire [7:0]   GMII_TXD_unreg;
245    wire         GMII_GTX_CLK_int;
246    
247    always @(posedge GMII_GTX_CLK_int)
248      begin
249         GMII_TX_EN <= GMII_TX_EN_unreg;
250         GMII_TX_ER <= GMII_TX_ER_unreg;
251         GMII_TXD <= GMII_TXD_unreg;
252      end
253
254    OFDDRRSE OFDDRRSE_gmii_inst 
255      (.Q(GMII_GTX_CLK),      // Data output (connect directly to top-level port)
256       .C0(GMII_GTX_CLK_int),    // 0 degree clock input
257       .C1(~GMII_GTX_CLK_int),    // 180 degree clock input
258       .CE(1),    // Clock enable input
259       .D0(0),    // Posedge data input
260       .D1(1),    // Negedge data input
261       .R(0),      // Synchronous reset input
262       .S(0)       // Synchronous preset input
263       );
264    
265    wire ser_tklsb_unreg, ser_tkmsb_unreg;
266    wire [15:0] ser_t_unreg;
267    wire        ser_tx_clk_int;
268    
269    always @(posedge ser_tx_clk_int)
270      begin
271         ser_tklsb <= ser_tklsb_unreg;
272         ser_tkmsb <= ser_tkmsb_unreg;
273         ser_t <= ser_t_unreg;
274      end
275
276    assign ser_tx_clk = clk_fpga;
277
278    reg [15:0] ser_r_int;
279    reg        ser_rklsb_int, ser_rkmsb_int;
280
281    always @(posedge ser_rx_clk)
282      begin
283         ser_r_int <= ser_r;
284         ser_rklsb_int <= ser_rklsb;
285         ser_rkmsb_int <= ser_rkmsb;
286      end
287    
288    /*
289    OFDDRRSE OFDDRRSE_serdes_inst 
290      (.Q(ser_tx_clk),      // Data output (connect directly to top-level port)
291       .C0(ser_tx_clk_int),    // 0 degree clock input
292       .C1(~ser_tx_clk_int),    // 180 degree clock input
293       .CE(1),    // Clock enable input
294       .D0(0),    // Posedge data input
295       .D1(1),    // Negedge data input
296       .R(0),      // Synchronous reset input
297       .S(0)       // Synchronous preset input
298       );
299    */
300    u2_basic u2_basic(.dsp_clk           (dsp_clk),
301                      .wb_clk            (wb_clk),
302                      .clock_ready       (clock_ready),
303                      .clk_to_mac        (clk_to_mac),
304                      .pps_in            (pps_in),
305                      .led1              (led1_int),
306                      .led2              (led2_int),
307                      .debug             (debug[31:0]),
308                      .debug_clk         (debug_clk[1:0]),
309                      .exp_pps_in        (exp_pps_in),
310                      .exp_pps_out       (exp_pps_out),
311                      .GMII_COL          (GMII_COL),
312                      .GMII_CRS          (GMII_CRS),
313                      .GMII_TXD          (GMII_TXD_unreg[7:0]),
314                      .GMII_TX_EN        (GMII_TX_EN_unreg),
315                      .GMII_TX_ER        (GMII_TX_ER_unreg),
316                      .GMII_GTX_CLK      (GMII_GTX_CLK_int),
317                      .GMII_TX_CLK       (GMII_TX_CLK),
318                      .GMII_RXD          (GMII_RXD[7:0]),
319                      .GMII_RX_CLK       (GMII_RX_CLK),
320                      .GMII_RX_DV        (GMII_RX_DV),
321                      .GMII_RX_ER        (GMII_RX_ER),
322                      .MDIO              (MDIO),
323                      .MDC               (MDC),
324                      .PHY_INTn          (PHY_INTn),
325                      .PHY_RESETn        (PHY_RESETn),
326                      .PHY_CLK           (PHY_CLK),
327                      .ser_enable        (ser_enable),
328                      .ser_prbsen        (ser_prbsen),
329                      .ser_loopen        (ser_loopen),
330                      .ser_rx_en         (ser_rx_en),
331                      .ser_tx_clk        (ser_tx_clk_int),
332                      .ser_t             (ser_t_unreg[15:0]),
333                      .ser_tklsb         (ser_tklsb_unreg),
334                      .ser_tkmsb         (ser_tkmsb_unreg),
335                      .ser_rx_clk        (ser_rx_clk),
336                      .ser_r             (ser_r_int[15:0]),
337                      .ser_rklsb         (ser_rklsb_int),
338                      .ser_rkmsb         (ser_rkmsb_int),
339                      .cpld_start        (cpld_start),
340                      .cpld_mode         (cpld_mode),
341                      .cpld_done         (cpld_done),
342                      .cpld_din          (cpld_din),
343                      .cpld_clk          (cpld_clk),
344                      .cpld_detached     (cpld_detached),
345                      .adc_a             (adc_a[13:0]),
346                      .adc_ovf_a         (adc_ovf_a),
347                      .adc_on_a          (adc_on_a),
348                      .adc_oe_a          (adc_oe_a),
349                      .adc_b             (adc_b[13:0]),
350                      .adc_ovf_b         (adc_ovf_b),
351                      .adc_on_b          (adc_on_b),
352                      .adc_oe_b          (adc_oe_b),
353                      .dac_a             (dac_a[15:0]),
354                      .dac_b             (dac_b[15:0]),
355                      .scl_pad_i         (scl_pad_i),
356                      .scl_pad_o         (scl_pad_o),
357                      .scl_pad_oen_o     (scl_pad_oen_o),
358                      .sda_pad_i         (sda_pad_i),
359                      .sda_pad_o         (sda_pad_o),
360                      .sda_pad_oen_o     (sda_pad_oen_o),
361                      .clk_en            (clk_en[1:0]),
362                      .clk_sel           (clk_sel[1:0]),
363                      .clk_func          (clk_func),
364                      .clk_status        (clk_status),
365                      .sclk              (sclk_int),
366                      .mosi              (mosi),
367                      .miso              (miso),
368                      .sen_clk           (sen_clk),
369                      .sen_dac           (sen_dac),
370                      .sen_tx_db         (sen_tx_db),
371                      .sen_tx_adc        (sen_tx_adc),
372                      .sen_tx_dac        (sen_tx_dac),
373                      .sen_rx_db         (sen_rx_db),
374                      .sen_rx_adc        (sen_rx_adc),
375                      .sen_rx_dac        (sen_rx_dac),
376                      .io_tx             (io_tx[15:0]),
377                      .io_rx             (io_rx[15:0]),
378                      .RAM_D             (RAM_D),
379                      .RAM_A             (RAM_A),
380                      .RAM_CE1n          (RAM_CE1n),
381                      .RAM_CENn          (RAM_CENn),
382                      .RAM_CLK           (RAM_CLK),
383                      .RAM_WEn           (RAM_WEn),
384                      .RAM_OEn           (RAM_OEn),
385                      .RAM_LDn           (RAM_LDn), 
386                      .uart_tx_o         (),
387                      .uart_rx_i         (),
388                      .uart_baud_o       (),
389                      .sim_mode          (1'b0),
390                      .clock_divider     (2)
391                      );
392    
393 endmodule // u2_fpga_top