]> jspc29.x-matter.uni-frankfurt.de Git - trb5sc.git/blob
37a1f5b64879d6366af7f05dcc2ce00302716ecc
[trb5sc.git] /
1
2
3 //   ===========================================================================
4 //   >>>>>>>>>>>>>>>>>>>>>>> COPYRIGHT NOTICE <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
5 //   ---------------------------------------------------------------------------
6 //   Copyright (c) 2016 by Lattice Semiconductor Corporation
7 //   ALL RIGHTS RESERVED
8 //   ------------------------------------------------------------------
9 //
10 //   Permission:
11 //
12 //      Lattice SG Pte. Ltd. grants permission to use this code
13 //      pursuant to the terms of the Lattice Reference Design License Agreement.
14 //
15 //
16 //   Disclaimer:
17 //
18 //      This VHDL or Verilog source code is intended as a design reference
19 //      which illustrates how these types of functions can be implemented.
20 //      It is the user's responsibility to verify their design for
21 //      consistency and functionality through the use of formal
22 //      verification methods.  Lattice provides no warranty
23 //      regarding the use or functionality of this code.
24 //
25 //   ---------------------------------------------------------------------------
26 //
27 //                  Lattice SG Pte. Ltd.
28 //                  101 Thomson Road, United Square #07-02
29 //                  Singapore 307591
30 //
31 //
32 //                  TEL: 1-800-Lattice (USA and Canada)
33 //                       +65-6631-2000 (Singapore)
34 //                       +1-503-268-8001 (other locations)
35 //
36 //                  web: http://www.latticesemi.com/
37 //                  email: techsupport@latticesemi.com
38 //
39 //   ---------------------------------------------------------------------------
40 //
41 // =============================================================================
42 //                         FILE DETAILS
43 // Project               : RSL- Reset Sequence Logic
44 // File                  : rsl_core.v
45 // Title                 : Top-level file for RSL
46 // Dependencies          : 1.
47 //                       : 2.
48 // Description           :
49 // =============================================================================
50 //                        REVISION HISTORY
51 // Version               : 1.0
52 // Author(s)             : BM
53 // Mod. Date             : October 28, 2013
54 // Changes Made          : Initial Creation
55 // -----------------------------------------------------------------------------
56 // Version               : 1.1
57 // Author(s)             : BM
58 // Mod. Date             : November 06, 2013
59 // Changes Made          : Tx/Rx separation, ready port code exclusion
60 // -----------------------------------------------------------------------------
61 // Version               : 1.2
62 // Author(s)             : BM
63 // Mod. Date             : June 13, 2014
64 // Changes Made          : Updated Rx PCS reset method
65 // -----------------------------------------------------------------------------
66 // -----------------------------------------------------------------------------
67 // Version               : 1.3
68 // Author(s)             : UA
69 // Mod. Date             : Dec 19, 2014
70 // Changes Made          : Added new parameter fro PCIE
71 // -----------------------------------------------------------------------------
72 // Version               : 1.31
73 // Author(s)             : BM/UM
74 // Mod. Date             : Feb 23, 2016
75 // Changes Made          : Behavior of rx_rdy output modified. The output rx_rdy
76 //                         and the rx_rdy wait counter are reset to zero on
77 //                         LOL or LOS. Reverted back the counter value change for PCIE.
78 // -----------------------------------------------------------------------------
79 // Version               : 1.4
80 // Author(s)             : EB
81 // Mod. Date:            : March 21, 2017
82 // Changes Made          :
83 // -----------------------------------------------------------------------------
84 // Version               : 1.5
85 // Author(s)             : ES
86 // Mod. Date:            : May 8, 2017
87 // Changes Made          : Implemented common RSL behaviour as proposed by BM.
88 // =============================================================================
89
90 `timescale 1ns/10ps
91
92 module sgmii_channel_smi_pcsrsl_core (
93       // ------------ Inputs
94       // Common
95       rui_rst,               // Active high reset for the RSL module
96       rui_serdes_rst_dual_c, // SERDES macro reset user command
97       rui_rst_dual_c,        // PCS dual reset user command
98       rui_rsl_disable,       // Active high signal that disables all reset outputs of RSL
99       // Tx
100       rui_tx_ref_clk,        // Tx reference clock
101       rui_tx_serdes_rst_c,   // Tx SERDES reset user command
102       rui_tx_pcs_rst_c,      // Tx lane reset user command
103       rdi_pll_lol,           // Tx PLL Loss of Lock status input from the SERDES
104       // Rx
105       rui_rx_ref_clk,        // Rx reference clock
106       rui_rx_serdes_rst_c,   // SERDES Receive channel reset user command
107       rui_rx_pcs_rst_c,      // Rx lane reset user command
108       rdi_rx_los_low_s,      // Receive loss of signal status input from SERDES
109       rdi_rx_cdr_lol_s,      // Receive CDR loss of lock status input from SERDES
110
111       // ------------ Outputs
112       // Common
113       rdo_serdes_rst_dual_c, // SERDES macro reset command output
114       rdo_rst_dual_c,        // PCS dual reset command output
115       // Tx
116       ruo_tx_rdy,            // Tx lane ready status output
117       rdo_tx_serdes_rst_c,   // SERDES Tx reset command output
118       rdo_tx_pcs_rst_c,      // PCS Tx lane reset command output
119       // Rx
120       ruo_rx_rdy,            // Rx lane ready status output
121       rdo_rx_serdes_rst_c,   // SERDES Rx channel reset command output
122       rdo_rx_pcs_rst_c       // PCS Rx lane reset command output
123       );
124
125 // ------------ Module parameters
126 `ifdef NUM_CHANNELS
127    parameter pnum_channels = `NUM_CHANNELS;    // 1,2,4
128 `else
129    parameter pnum_channels = 1;
130 `endif
131
132 `ifdef PCIE
133    parameter pprotocol    = "PCIE";
134 `else
135    parameter pprotocol    = "";
136 `endif
137
138 `ifdef RX_ONLY
139    parameter pserdes_mode    = "RX ONLY";
140 `else
141    `ifdef TX_ONLY
142       parameter pserdes_mode = "TX ONLY";
143    `else
144       parameter pserdes_mode = "RX AND TX";
145    `endif
146 `endif
147
148 `ifdef PORT_TX_RDY
149    parameter pport_tx_rdy = "ENABLED";
150 `else
151    parameter pport_tx_rdy = "DISABLED";
152 `endif
153
154 `ifdef WAIT_TX_RDY
155    parameter pwait_tx_rdy = `WAIT_TX_RDY;
156 `else
157    parameter pwait_tx_rdy = 3000;
158 `endif
159
160 `ifdef PORT_RX_RDY
161    parameter pport_rx_rdy = "ENABLED";
162 `else
163    parameter pport_rx_rdy = "DISABLED";
164 `endif
165
166 `ifdef WAIT_RX_RDY
167    parameter pwait_rx_rdy = `WAIT_RX_RDY;
168 `else
169    parameter pwait_rx_rdy = 3000;
170 `endif
171
172 // ------------ Local parameters
173    localparam wa_num_cycles      = 1024;
174    localparam dac_num_cycles     = 3;
175    localparam lreset_pwidth      = 3;      // reset pulse width-1, default=4-1=3
176    localparam lwait_b4_trst      = 781250; // 5ms wait with worst-case Fmax=156 MHz
177    localparam lwait_b4_trst_s    = 781;    // for simulation
178    localparam lplol_cnt_width    = 20;     // width for lwait_b4_trst
179    localparam lwait_after_plol0  = 4;
180    localparam lwait_b4_rrst      = 180224; // total calibration time
181    localparam lrrst_wait_width   = 20;
182    localparam lwait_after_rrst   = 800000; // For CPRI- unused
183    localparam lwait_b4_rrst_s    = 460;    // wait cycles provided by design team
184    localparam lrlol_cnt_width    = 19;     // width for lwait_b4_rrst
185    localparam lwait_after_lols   = (16384 * dac_num_cycles) + wa_num_cycles;  // 16384 cycles * dac_num_cycles + 1024 cycles
186    localparam lwait_after_lols_s = 150;    // wait cycles provided by design team
187    localparam llols_cnt_width    = 18;     // lols count width
188    localparam lrdb_max           = 15;     // maximum debounce count
189    localparam ltxr_wait_width    = 12;     // width of tx ready wait counter
190    localparam lrxr_wait_width    = 12;     // width of tx ready wait counter
191
192 // ------------ input ports
193    input                         rui_rst;
194    input                         rui_serdes_rst_dual_c;
195    input                         rui_rst_dual_c;
196    input                         rui_rsl_disable;
197
198    input                         rui_tx_ref_clk;
199    input                         rui_tx_serdes_rst_c;
200    input  [3:0]                  rui_tx_pcs_rst_c;
201    input                         rdi_pll_lol;
202
203    input                         rui_rx_ref_clk;
204    input  [3:0]                  rui_rx_serdes_rst_c;
205    input  [3:0]                  rui_rx_pcs_rst_c;
206    input  [3:0]                  rdi_rx_los_low_s;
207    input  [3:0]                  rdi_rx_cdr_lol_s;
208
209 // ------------ output ports
210    output                        rdo_serdes_rst_dual_c;
211    output                        rdo_rst_dual_c;
212
213    output                        ruo_tx_rdy;
214    output                        rdo_tx_serdes_rst_c;
215    output [3:0]                  rdo_tx_pcs_rst_c;
216
217    output                        ruo_rx_rdy;
218    output [3:0]                  rdo_rx_serdes_rst_c;
219    output [3:0]                  rdo_rx_pcs_rst_c;
220
221 // ------------ Internal registers and wires
222    // inputs
223    wire                          rui_rst;
224    wire                          rui_serdes_rst_dual_c;
225    wire                          rui_rst_dual_c;
226    wire                          rui_rsl_disable;
227    wire                          rui_tx_ref_clk;
228    wire                          rui_tx_serdes_rst_c;
229    wire   [3:0]                  rui_tx_pcs_rst_c;
230    wire                          rdi_pll_lol;
231    wire                          rui_rx_ref_clk;
232    wire   [3:0]                  rui_rx_serdes_rst_c;
233    wire   [3:0]                  rui_rx_pcs_rst_c;
234    wire   [3:0]                  rdi_rx_los_low_s;
235    wire   [3:0]                  rdi_rx_cdr_lol_s;
236
237    // outputs
238    wire                          rdo_serdes_rst_dual_c;
239    wire                          rdo_rst_dual_c;
240    wire                          ruo_tx_rdy;
241    wire                          rdo_tx_serdes_rst_c;
242    wire   [3:0]                  rdo_tx_pcs_rst_c;
243    wire                          ruo_rx_rdy;
244    wire   [3:0]                  rdo_rx_serdes_rst_c;
245    wire   [3:0]                  rdo_rx_pcs_rst_c;
246
247    // internal signals
248    // common
249    wire                          rsl_enable;
250    wire   [lplol_cnt_width-1:0]  wait_b4_trst;
251    wire   [lrlol_cnt_width-1:0]  wait_b4_rrst;
252    wire   [llols_cnt_width-1:0]  wait_after_lols;
253    reg                           pll_lol_p1;
254    reg                           pll_lol_p2;
255    reg                           pll_lol_p3;
256    // ------------ Tx
257    // rdo_tx_serdes_rst_c
258    reg    [lplol_cnt_width-1:0]  plol_cnt;
259    wire                          plol_cnt_tc;
260
261    reg    [2:0]                  txs_cnt;
262    reg                           txs_rst;
263    wire                          txs_cnt_tc;
264    // rdo_tx_pcs_rst_c
265    wire                          plol_fedge;
266    wire                          plol_redge;
267    reg                           waita_plol0;
268    reg    [2:0]                  plol0_cnt;
269    wire                          plol0_cnt_tc;
270    reg    [2:0]                  txp_cnt;
271    reg                           txp_rst;
272    wire                          txp_cnt_tc;
273    // ruo_tx_rdy
274    wire                          dual_or_serd_rst;
275    wire                          tx_any_pcs_rst;
276    wire                          tx_any_rst;
277    reg                           txsr_appd /* synthesis syn_keep=1 */;
278    reg                           txdpr_appd;
279    reg    [pnum_channels-1:0]    txpr_appd;
280    reg                           txr_wt_en;
281    reg    [ltxr_wait_width-1:0]  txr_wt_cnt;
282    wire                          txr_wt_tc;
283    reg                           ruo_tx_rdyr;
284
285    // ------------ Rx
286    wire                          comb_rlos;
287    wire                          comb_rlol;
288    //wire                          rlols;
289    wire                          rx_all_well;
290
291    //reg                           rlols_p1;
292    //reg                           rlols_p2;
293    //reg                           rlols_p3;
294
295    reg                           rlol_p1;
296    reg                           rlol_p2;
297    reg                           rlol_p3;
298    reg                           rlos_p1;
299    reg                           rlos_p2;
300    reg                           rlos_p3;
301
302    //reg    [3:0]                  rdb_cnt;
303    //wire                          rdb_cnt_max;
304    //wire                          rdb_cnt_zero;
305    //reg                           rlols_db;
306    //reg                           rlols_db_p1;
307
308    reg    [3:0]                  rlol_db_cnt;
309    wire                          rlol_db_cnt_max;
310    wire                          rlol_db_cnt_zero;
311    reg                           rlol_db;
312    reg                           rlol_db_p1;
313
314    reg    [3:0]                  rlos_db_cnt;
315    wire                          rlos_db_cnt_max;
316    wire                          rlos_db_cnt_zero;
317    reg                           rlos_db;
318    reg                           rlos_db_p1;
319
320    // rdo_rx_serdes_rst_c
321    reg    [lrlol_cnt_width-1:0]  rlol1_cnt;
322    wire                          rlol1_cnt_tc;
323    reg    [2:0]                  rxs_cnt;
324    reg                           rxs_rst;
325    wire                          rxs_cnt_tc;
326    reg    [lrrst_wait_width-1:0] rrst_cnt;
327    wire                          rrst_cnt_tc;
328    reg                           rrst_wait;
329    // rdo_rx_pcs_rst_c
330    //wire                          rlols_fedge;
331    //wire                          rlols_redge;
332    wire                          rlol_fedge;
333    wire                          rlol_redge;
334    wire                          rlos_fedge;
335    wire                          rlos_redge;
336
337    reg                           wait_calib;
338    reg                           waita_rlols0;
339    reg    [llols_cnt_width-1:0]  rlols0_cnt;
340    wire                          rlols0_cnt_tc;
341    reg    [2:0]                  rxp_cnt;
342    reg                           rxp_rst;
343    wire                          rxp_cnt_tc;
344
345    wire                          rx_any_serd_rst;
346    reg    [llols_cnt_width-1:0]  rlolsz_cnt;
347    wire                          rlolsz_cnt_tc;
348    reg [2:0]                     rxp_cnt2;
349    reg                           rxp_rst2;
350    wire                          rxp_cnt2_tc;
351    reg [15:0]                    data_loop_b_cnt;
352    reg                           data_loop_b;
353    wire                          data_loop_b_tc;
354
355    // ruo_rx_rdy
356    reg    [pnum_channels-1:0]    rxsr_appd;
357    reg    [pnum_channels-1:0]    rxpr_appd;
358    reg                           rxsdr_appd /* synthesis syn_keep=1 */;
359    reg                           rxdpr_appd;
360    wire                          rxsdr_or_sr_appd;
361    wire                          dual_or_rserd_rst;
362    wire                          rx_any_pcs_rst;
363    wire                          rx_any_rst;
364    reg                           rxr_wt_en;
365    reg    [lrxr_wait_width-1:0]  rxr_wt_cnt;
366    wire                          rxr_wt_tc;
367    reg                           ruo_rx_rdyr;
368
369 // ==================================================================
370 //                          Start of code
371 // ==================================================================
372    assign rsl_enable = ~rui_rsl_disable;
373
374 // ------------ rdo_serdes_rst_dual_c
375    assign rdo_serdes_rst_dual_c = (rui_rst&rsl_enable) | rui_serdes_rst_dual_c;
376
377 // ------------ rdo_rst_dual_c
378    assign rdo_rst_dual_c = rui_rst_dual_c;
379
380 // ------------ Setting counter values for RSL_SIM_MODE
381    `ifdef RSL_SIM_MODE
382       assign wait_b4_trst    = lwait_b4_trst_s;
383       assign wait_b4_rrst    = lwait_b4_rrst_s;
384       assign wait_after_lols = lwait_after_lols_s;
385    `else
386       assign wait_b4_trst    = lwait_b4_trst;
387       assign wait_b4_rrst    = lwait_b4_rrst;
388       assign wait_after_lols = lwait_after_lols;
389    `endif
390
391 // ==================================================================
392 //                                 Tx
393 // ==================================================================
394    generate
395    if((pserdes_mode=="RX AND TX")||(pserdes_mode=="TX ONLY")) begin
396
397 // ------------ Synchronizing pll_lol to the tx clock
398    always @(posedge rui_tx_ref_clk or posedge rui_rst) begin
399       if(rui_rst==1'b1) begin
400          pll_lol_p1 <= 1'd0;
401          pll_lol_p2 <= 1'd0;
402          pll_lol_p3 <= 1'd0;
403       end
404       else begin
405          pll_lol_p1 <= rdi_pll_lol;
406          pll_lol_p2 <= pll_lol_p1;
407          pll_lol_p3 <= pll_lol_p2;
408       end
409    end
410
411 // ------------ rdo_tx_serdes_rst_c
412    always @(posedge rui_tx_ref_clk or posedge rui_rst) begin
413       if(rui_rst==1'b1)
414          plol_cnt    <= 'd0;
415       else if((pll_lol_p2==0)||(plol_cnt_tc==1)||(rdo_tx_serdes_rst_c==1))
416          plol_cnt    <= 'd0;
417       else
418          plol_cnt    <= plol_cnt+1;
419    end
420    assign plol_cnt_tc = (plol_cnt==wait_b4_trst)?1'b1:1'b0;
421
422    always @(posedge rui_tx_ref_clk or posedge rui_rst) begin
423       if(rui_rst==1'b1) begin
424          txs_cnt  <= 'd0;   // tx serdes reset pulse count
425          txs_rst  <= 1'b0;  // tx serdes reset
426       end
427       else if(plol_cnt_tc==1)
428          txs_rst  <= 1'b1;
429       else if(txs_cnt_tc==1) begin
430          txs_cnt  <= 'd0;
431          txs_rst  <= 1'b0;
432       end
433       else if(txs_rst==1)
434          txs_cnt  <= txs_cnt+1;
435    end
436    assign txs_cnt_tc = (txs_cnt==lreset_pwidth)?1'b1:1'b0;
437
438    assign rdo_tx_serdes_rst_c = (rsl_enable&txs_rst)| rui_tx_serdes_rst_c;
439
440 // ------------ rdo_tx_pcs_rst_c
441    assign plol_fedge = ~pll_lol_p2 & pll_lol_p3;
442    assign plol_redge = pll_lol_p2 & ~pll_lol_p3;
443    always @(posedge rui_tx_ref_clk or posedge rui_rst) begin
444       if(rui_rst==1'b1)
445          waita_plol0  <= 1'd0;
446       else if(plol_fedge==1'b1)
447          waita_plol0  <= 1'b1;
448       else if((plol0_cnt_tc==1)||(plol_redge==1))
449          waita_plol0  <= 1'd0;
450    end
451    always @(posedge rui_tx_ref_clk or posedge rui_rst) begin
452       if(rui_rst==1'b1)
453          plol0_cnt    <= 'd0;
454       else if((pll_lol_p2==1)||(plol0_cnt_tc==1))
455          plol0_cnt    <= 'd0;
456       else if(waita_plol0==1'b1)
457          plol0_cnt    <= plol0_cnt+1;
458    end
459    assign plol0_cnt_tc = (plol0_cnt==lwait_after_plol0)?1'b1:1'b0;
460
461    always @(posedge rui_tx_ref_clk or posedge rui_rst) begin
462       if(rui_rst==1'b1) begin
463          txp_cnt  <= 'd0;   // tx serdes reset pulse count
464          txp_rst  <= 1'b0;  // tx serdes reset
465       end
466       else if(plol0_cnt_tc==1)
467          txp_rst  <= 1'b1;
468       else if(txp_cnt_tc==1) begin
469          txp_cnt  <= 'd0;
470          txp_rst  <= 1'b0;
471       end
472       else if(txp_rst==1)
473          txp_cnt  <= txp_cnt+1;
474    end
475    assign txp_cnt_tc = (txp_cnt==lreset_pwidth)?1'b1:1'b0;
476
477    genvar i;
478    for(i=0;i<pnum_channels;i=i+1) begin : ifor
479       assign rdo_tx_pcs_rst_c[i] = (rsl_enable&txp_rst)| rui_tx_pcs_rst_c[i];
480    end
481    if(pnum_channels==1)
482       assign rdo_tx_pcs_rst_c[3:1] = 3'b000;
483    else if(pnum_channels==2)
484       assign rdo_tx_pcs_rst_c[3:2] = 2'b00;
485
486    // ------------ ruo_tx_rdy
487    if(pport_tx_rdy=="ENABLED") begin
488    assign dual_or_serd_rst = rdo_serdes_rst_dual_c|rdo_tx_serdes_rst_c;
489    assign tx_any_pcs_rst = rdo_rst_dual_c|(|rdo_tx_pcs_rst_c[pnum_channels-1:0]);
490    assign tx_any_rst = dual_or_serd_rst | tx_any_pcs_rst;
491
492    always @(posedge rui_tx_ref_clk or posedge rui_rst) begin
493       if(rui_rst==1'b1)
494          txsr_appd <= 1'b1; // tx serdes reset applied
495       else if(dual_or_serd_rst==1)
496          txsr_appd <= 1'b1;
497    end
498    always @(posedge rui_tx_ref_clk or posedge rui_rst) begin
499       if(rui_rst==1'b1)
500          txdpr_appd <= 1'b0; // tx dual (pcs) reset applied
501       else if(pll_lol_p2|rdo_serdes_rst_dual_c|rdo_tx_serdes_rst_c)
502          txdpr_appd <= 1'b0;
503       else if(rdo_rst_dual_c==1)
504          txdpr_appd <= 1'b1;
505    end
506
507    genvar m;
508    for(m=0;m<pnum_channels;m=m+1) begin :mfor
509       always @(posedge rui_tx_ref_clk or posedge rui_rst) begin
510       if(rui_rst==1'b1)
511          txpr_appd[m] <= 1'b0; // tx pcs reset applied
512       else if(pll_lol_p2|rdo_serdes_rst_dual_c|rdo_tx_serdes_rst_c)
513          txpr_appd[m] <= 1'b0;
514       else if(txsr_appd&(rdo_tx_pcs_rst_c[m]|txdpr_appd))
515          txpr_appd[m] <= 1'b1;
516       end
517    end
518
519    always @(posedge rui_tx_ref_clk or posedge rui_rst) begin
520       if(rui_rst==1'b1)
521          txr_wt_en  <= 0;  // tx ready wait counter enable
522       else if((txr_wt_tc==1)||(dual_or_serd_rst==1))
523          txr_wt_en  <= 0;
524       else if((~ruo_tx_rdyr)&(~pll_lol_p2)&(&txpr_appd))
525          txr_wt_en  <= 1;
526    end
527    always @(posedge rui_tx_ref_clk or posedge rui_rst) begin
528       if(rui_rst==1'b1)
529          txr_wt_cnt  <= 'd0;  // tx ready wait count
530       else if((txr_wt_tc==1)||(tx_any_rst==1))
531          txr_wt_cnt  <= 'd0;
532       else if(txr_wt_en==1)
533          txr_wt_cnt  <= txr_wt_cnt+1;
534    end
535    assign txr_wt_tc = (txr_wt_cnt==pwait_tx_rdy)?1'b1:1'b0;
536
537    always @(posedge rui_tx_ref_clk or posedge rui_rst) begin
538       if(rui_rst==1'b1)
539          ruo_tx_rdyr <= 1'b0; // tx serdes reset applied
540       else if((tx_any_rst==1)||(pll_lol_p2==1))
541          ruo_tx_rdyr <= 1'b0;
542       else if(txr_wt_tc==1)
543          ruo_tx_rdyr <= 1'b1;
544    end
545    assign ruo_tx_rdy = ruo_tx_rdyr;
546    end         // if pport_tx_rdy
547    else
548       assign ruo_tx_rdy = 1'b0;
549    end         // generate if(Rx and Tx) or (Tx only)
550    else begin  // generate else (Rx only)
551       assign rdo_tx_serdes_rst_c = 1'b0;
552       assign rdo_tx_pcs_rst_c = 4'd0;
553       assign ruo_tx_rdy = 1'b0;
554    end
555    endgenerate
556
557 // ==================================================================
558 //                                 Rx
559 // ==================================================================
560    generate
561    if((pserdes_mode=="RX AND TX")||(pserdes_mode=="RX ONLY")) begin
562    assign comb_rlos = |rdi_rx_los_low_s[pnum_channels-1:0];
563    assign comb_rlol = |rdi_rx_cdr_lol_s[pnum_channels-1:0];
564    //assign rlols     = comb_rlos|comb_rlol;
565
566    // ------------ Synchronizing rlols to the rx ref clock
567    always @(posedge rui_rx_ref_clk or posedge rui_rst) begin
568       if(rui_rst==1'b1) begin
569         //rlols_p1    <= 1'd0;
570         //rlols_p2    <= 1'd0;
571         //rlols_p3    <= 1'd0;
572         //rlols_db_p1 <= 1'd1;
573
574         rlol_p1     <= 1'd0;
575         rlol_p2     <= 1'd0;
576         rlol_p3     <= 1'd0;
577         rlol_db_p1  <= 1'd1;
578
579         rlos_p1    <= 1'd0;
580         rlos_p2    <= 1'd0;
581         rlos_p3    <= 1'd0;
582         rlos_db_p1 <= 1'd1;
583       end
584       else begin
585         //rlols_p1    <= rlols;
586         //rlols_p2    <= rlols_p1;
587         //rlols_p3    <= rlols_p2;
588         //rlols_db_p1 <= rlols_db;
589
590         rlol_p1     <= comb_rlol;
591         rlol_p2     <= rlol_p1;
592         rlol_p3     <= rlol_p2;
593         rlol_db_p1  <= rlol_db;
594
595         rlos_p1    <= comb_rlos;
596         rlos_p2    <= rlos_p1;
597         rlos_p3    <= rlos_p2;
598         rlos_db_p1 <= rlos_db;
599       end
600    end
601    assign rx_all_well = ~rlol_db && ~rlos_db;
602
603 //******************************************************************************
604 // [ES:05.03.17] Unused registers for clean-up
605 //------------------------------------------------------------------------------
606 // ------------ Debouncing rlols
607 //   always @(posedge rui_rx_ref_clk or posedge rui_rst) begin
608 //      if(rui_rst==1'b1) rdb_cnt  <= lrdb_max;
609 //      else if(rlols_p2==1) begin
610 //         if(!rdb_cnt_max) rdb_cnt <= rdb_cnt+1;
611 //      end
612 //      else if(!rdb_cnt_zero) rdb_cnt <= rdb_cnt-1;
613 //   end
614 //   assign rdb_cnt_max  = (rdb_cnt==lrdb_max);
615 //   assign rdb_cnt_zero = (rdb_cnt==0);
616 //   always @(posedge rui_rx_ref_clk or posedge rui_rst) begin
617 //      if(rui_rst==1'b1) rlols_db <= 1;
618 //      else if(rdb_cnt_max)  rlols_db <= 1;
619 //      else if(rdb_cnt_zero) rlols_db <= 0;
620 //   end
621 //******************************************************************************
622
623 // ------------ Debouncing rlol
624    always @(posedge rui_rx_ref_clk or posedge rui_rst) begin
625       if(rui_rst==1'b1) rlol_db_cnt  <= lrdb_max;
626       else if(rlol_p2==1) begin
627          if(!rlol_db_cnt_max) rlol_db_cnt <= rlol_db_cnt+1;
628       end
629       else if(!rlol_db_cnt_zero) rlol_db_cnt <= rlol_db_cnt-1;
630    end
631    assign rlol_db_cnt_max  = (rlol_db_cnt==lrdb_max);
632    assign rlol_db_cnt_zero = (rlol_db_cnt==0);
633    always @(posedge rui_rx_ref_clk or posedge rui_rst) begin
634       if(rui_rst==1'b1) rlol_db <= 1;
635       else if(rlol_db_cnt_max)  rlol_db <= 1;
636       else if(rlol_db_cnt_zero) rlol_db <= 0;
637    end
638
639 // ------------ Debouncing rlos
640    always @(posedge rui_rx_ref_clk or posedge rui_rst) begin
641       if(rui_rst==1'b1) rlos_db_cnt  <= lrdb_max;
642       else if(rlos_p2==1) begin
643          if(!rlos_db_cnt_max) rlos_db_cnt <= rlos_db_cnt+1;
644       end
645       else if(!rlos_db_cnt_zero) rlos_db_cnt <= rlos_db_cnt-1;
646    end
647    assign rlos_db_cnt_max  = (rlos_db_cnt==lrdb_max);
648    assign rlos_db_cnt_zero = (rlos_db_cnt==0);
649    always @(posedge rui_rx_ref_clk or posedge rui_rst) begin
650       if(rui_rst==1'b1) rlos_db <= 1;
651       else if(rlos_db_cnt_max)  rlos_db <= 1;
652       else if(rlos_db_cnt_zero) rlos_db <= 0;
653    end
654
655 // ------------ Calib time trigger
656    always @(posedge rui_rx_ref_clk or posedge rui_rst) begin
657      if (rui_rst==1'b1) begin
658        wait_calib <= 1'd1;
659      end
660      else begin
661        if (rlol1_cnt_tc) begin
662          if (rlol_db)
663            wait_calib <= 1'd1;
664          else
665            wait_calib <= 1'd0;
666        end
667        else if (rlos_redge)
668          wait_calib <= 1'd0;
669        else if (rlos_fedge) begin
670          wait_calib <= 1'd1;
671        end
672      end
673    end
674
675    //***************************************************************************
676    // Total calibration time counter
677    // - this covers the band calibration time (256 cycles * 64) and
678    //   DAC calibration time (16384 cycles * 10 bits)
679    //---------------------------------------------------------------------------
680    always @(posedge rui_rx_ref_clk or posedge rui_rst) begin
681      if (rui_rst==1'b1) begin
682        rlol1_cnt  <= 'd0;  // Counting when Rx LOL is 1 and Rx LOS is 0
683      end
684      else begin
685        if(rxs_rst || rlol1_cnt_tc || rlos_redge)
686          rlol1_cnt  <= 'd0;
687        else if (wait_calib)
688          rlol1_cnt <= rlol1_cnt+1;
689      end
690    end
691    assign rlol1_cnt_tc = (rlol1_cnt==wait_b4_rrst);
692
693 // ------------ rdo_rx_serdes_rst_c
694    always @(posedge rui_rx_ref_clk or posedge rui_rst) begin
695      if (rui_rst==1'b1) begin
696        rxs_cnt  <= 'd0;   // rx serdes reset pulse count
697        rxs_rst  <= 1'b0;  // rx serdes reset
698      end
699      else begin
700        if (rlos_db)
701          rxs_rst <= 1'b0;
702        else if (rlol1_cnt_tc && rlol_db)
703          rxs_rst <= 1'b1;
704        else if (rxs_cnt_tc==1) begin
705          rxs_rst  <= 1'b0;
706        end
707
708        if (rxs_cnt_tc)
709          rxs_cnt <= 'd0;
710        else
711          if (rxs_rst==1)
712            rxs_cnt <= rxs_cnt+1;
713      end
714    end
715    assign rxs_cnt_tc  = (rxs_cnt==lreset_pwidth)?1'b1:1'b0;
716
717    //***************************************************************************
718    // [ES:05.03.17] Unused logic from CPRI rrst_wait
719    //---------------------------------------------------------------------------
720    // always @(posedge rui_rx_ref_clk or posedge rui_rst) begin
721    //    if(rui_rst==1'b1)
722    //       rrst_cnt    <= 'd0;
723    //    else if(rlol1_cnt_tc)
724    //       rrst_cnt    <= 'd0;
725    //    else if(rrst_wait)
726    //       rrst_cnt    <= rrst_cnt+1;
727    // end
728    // assign rrst_cnt_tc = (rrst_cnt==lwait_after_rrst) ? 1'b1 : 1'b0;
729    // always @(posedge rui_rx_ref_clk or posedge rui_rst) begin
730    //    if(rui_rst==1'b1)
731    //       rrst_wait    <= 0;
732    //    else if(pprotocol != "CPRI")
733    //       rrst_wait    <= 0;
734    //    else if(rlol1_cnt_tc)
735    //       rrst_wait    <= 1;
736    //    else if(rrst_cnt_tc==1)
737    //       rrst_wait    <= 0;
738    // end
739    //***************************************************************************
740
741    genvar j;
742    for(j=0;j<pnum_channels;j=j+1) begin :jfor
743       assign rdo_rx_serdes_rst_c[j] = (rsl_enable&rxs_rst)| rui_rx_serdes_rst_c[j];
744    end
745    if(pnum_channels==1)
746       assign rdo_rx_serdes_rst_c[3:1] = 3'b000;
747    else if(pnum_channels==2)
748       assign rdo_rx_serdes_rst_c[3:2] = 2'b00;
749
750 // ------------ rdo_rx_pcs_rst_c
751    //assign rlols_fedge = ~rlols_db & rlols_db_p1;
752    //assign rlols_redge = rlols_db & ~rlols_db_p1;
753
754    assign rlol_fedge  = ~rlol_db & rlol_db_p1;
755    assign rlol_redge  = rlol_db & ~rlol_db_p1;
756    assign rlos_fedge  = ~rlos_db & rlos_db_p1;
757    assign rlos_redge  = rlos_db & ~rlos_db_p1;
758
759    always @(posedge rui_rx_ref_clk or posedge rui_rst) begin
760       if (rui_rst==1'b1) begin
761         waita_rlols0 <= 1'd0;
762       end
763       else begin
764         if ((rlos_fedge && ~rlol_db) || (rlol_fedge && ~rlos_db))
765           waita_rlols0 <= 1'b1;
766         else if (rlos_redge || rlol_redge)
767           waita_rlols0 <= 1'd0;
768         else if (rlols0_cnt_tc==1)
769           waita_rlols0 <= 1'd0;
770       end
771    end
772
773    //***************************************************************************
774    // Post RLOL check before pcs_rst deassertion
775    // - allowance of 2-4 DAC calibration cycles + 1024 cycles for WA module
776    //   (word alignment).
777    //---------------------------------------------------------------------------
778    always @(posedge rui_rx_ref_clk or posedge rui_rst) begin
779      if (rui_rst==1'b1) begin
780        rlols0_cnt <= 'd0;
781      end
782      else begin
783        if (rlol_redge || rlos_redge || rlols0_cnt_tc)
784          rlols0_cnt <= 'd0;
785        else if (waita_rlols0==1)
786          rlols0_cnt <= rlols0_cnt+1;
787      end
788    end
789    assign rlols0_cnt_tc   = (rlols0_cnt == wait_after_lols);
790    assign rx_any_serd_rst = rdo_serdes_rst_dual_c|(|rdo_rx_serdes_rst_c);
791
792    //***************************************************************************
793    // [ES:05.03.17] Unused registers for clean-up
794    //---------------------------------------------------------------------------
795    // always @(posedge rui_rx_ref_clk or posedge rui_rst) begin
796    //    if(rui_rst==1'b1)
797    //       rlolsz_cnt  <= 'd0;  // Counting when both Rx LOL is 0 and Rx LOS is 0
798    //    else if((rlol_db|rx_any_serd_rst)||(rlolsz_cnt_tc==1))
799    //       rlolsz_cnt  <= 'd0;
800    //    else if((rlolsz_cnt_tc==0)&&(rlol_db==0))
801    //       rlolsz_cnt  <= rlolsz_cnt+1;
802    // end
803    // assign rlolsz_cnt_tc = (rlolsz_cnt==wait_after_lols);
804    //***************************************************************************
805
806    always @(posedge rui_rx_ref_clk or posedge rui_rst) begin
807      if (rui_rst==1'b1) begin
808        rxp_cnt2  <= 'd0;   // pcs serdes reset pulse count
809        rxp_rst2  <= 1'b1;  // rx pcs reset
810      end
811      else begin
812        if (rx_any_serd_rst || rlos_redge) begin
813          rxp_rst2 <= 1'b1;
814        end
815        else if (rlols0_cnt_tc) begin
816          rxp_rst2 <= 1'b0;
817        end
818        //***********************************************************************
819        // [ES:05.03.17] No need for pulse width
820        //-----------------------------------------------------------------------
821        // else if(rxp_cnt2_tc==1) begin
822        //   rxp_cnt2  <= 'd0;
823        //   rxp_rst2  <= 1'b0;
824        // end
825        //***********************************************************************
826        // [ES:05.03.17] No need for pulse width
827        //-----------------------------------------------------------------------
828        // else if (rxp_rst2==1)
829        //   rxp_cnt2 <= rxp_cnt2+1;
830        //***********************************************************************
831      end // else: !if(rui_rst==1'b1)
832    end // always @ (posedge rui_rx_ref_clk or posedge rui_rst)
833    //assign rxp_cnt2_tc = (rxp_cnt2==lreset_pwidth)?1'b1:1'b0;
834
835    //***************************************************************************
836    // [ES:05.03.17] No need for pulse width
837    //---------------------------------------------------------------------------
838    //else begin
839    //   always @(posedge rui_rx_ref_clk or posedge rui_rst) begin
840    //      if(rui_rst==1'b1)
841    //         rxp_rst2  <= 1'b1;  // rx pcs reset
842    //      else if(rx_any_serd_rst)
843    //         rxp_rst2  <= 1'b1;
844    //      else if(rlolsz_cnt_tc==1)
845    //         rxp_rst2  <= 1'b0;
846    //   end
847    //end
848    //***************************************************************************
849
850    genvar k;
851    for(k=0;k<pnum_channels;k=k+1) begin: kfor
852       assign rdo_rx_pcs_rst_c[k] = (rsl_enable&rxp_rst2)| rui_rx_pcs_rst_c[k];
853    end
854    if(pnum_channels==1)
855       assign rdo_rx_pcs_rst_c[3:1] = 3'b000;
856    else if(pnum_channels==2)
857       assign rdo_rx_pcs_rst_c[3:2] = 2'b00;
858
859 // ------------ ruo_rx_rdy
860    if(pport_rx_rdy=="ENABLED") begin
861    assign dual_or_rserd_rst = rdo_serdes_rst_dual_c|(|rdo_rx_serdes_rst_c[pnum_channels-1:0]);
862    assign rx_any_pcs_rst = rdo_rst_dual_c|(|rdo_rx_pcs_rst_c[pnum_channels-1:0]);
863    assign rx_any_rst = dual_or_rserd_rst | rx_any_pcs_rst;
864
865    always @(posedge rui_rx_ref_clk or posedge rui_rst) begin
866       if(rui_rst==1'b1)
867          rxsdr_appd <= 1'b1;  // Serdes dual reset (macro reset) applied
868       else if(rdo_serdes_rst_dual_c==1)
869          rxsdr_appd <= 1'b1;
870    end
871    always @(posedge rui_rx_ref_clk or posedge rui_rst) begin
872       if(rui_rst==1'b1)
873          rxdpr_appd <= 1'b0;  // Rx dual PCS reset (dual reset) applied
874       else if(~rx_all_well|dual_or_rserd_rst)
875          rxdpr_appd <= 1'b0;
876       else if(rdo_rst_dual_c==1)
877          rxdpr_appd <= 1'b1;
878    end
879
880    genvar l;
881    for(l=0;l<pnum_channels;l=l+1) begin : lfor
882       always @(posedge rui_rx_ref_clk or posedge rui_rst) begin
883          if(rui_rst==1'b1)
884             rxsr_appd[l] <= 1'b0; // rx serdes reset applied
885          else if(rdo_rx_serdes_rst_c[l]==1)
886             rxsr_appd[l] <= 1'b1;
887       end
888       always @(posedge rui_rx_ref_clk or posedge rui_rst) begin
889       if(rui_rst==1'b1)
890          rxpr_appd[l] <= 1'b0; // rx pcs reset applied
891       else if(rdi_rx_los_low_s[l]|rdi_rx_cdr_lol_s[l]|rdo_serdes_rst_dual_c|rdo_rx_serdes_rst_c[l])
892          rxpr_appd[l] <= 1'b0;
893       else if(rxsdr_or_sr_appd&(~rx_all_well)&rdo_rx_pcs_rst_c[l])
894          rxpr_appd[l] <= 1'b1;
895       end
896    end
897
898    assign rxsdr_or_sr_appd = rxsdr_appd|(&rxsr_appd);
899
900    always @(posedge rui_rx_ref_clk or posedge rui_rst) begin
901       if(rui_rst==1'b1)
902          rxr_wt_en  <= 0;  // rx ready wait counter enable
903       //else if((rxr_wt_tc==1)||(dual_or_rserd_rst==1))
904       else if((rxr_wt_tc==1)||(dual_or_rserd_rst==1)||(rx_all_well==0)) // BM, 2/4/16
905          rxr_wt_en  <= 0;
906       else if(~ruo_rx_rdyr&rx_all_well&((&rxpr_appd)|rxdpr_appd))
907          rxr_wt_en  <= 1;
908    end
909    always @(posedge rui_rx_ref_clk or posedge rui_rst) begin
910       if(rui_rst==1'b1)
911          rxr_wt_cnt  <= 'd0;  // rx ready wait count
912       //else if((rxr_wt_tc==1)||(rx_any_rst==1))
913       else if((rxr_wt_tc==1)||(rx_any_rst==1)||(rx_all_well==0)) // BM, 2/4/16
914          rxr_wt_cnt  <= 'd0;
915       else if(rxr_wt_en==1)
916          rxr_wt_cnt  <= rxr_wt_cnt+1;
917    end
918    assign rxr_wt_tc = (rxr_wt_cnt==pwait_rx_rdy)?1'b1:1'b0;
919
920    always @(posedge rui_rx_ref_clk or posedge rui_rst) begin
921       if(rui_rst==1'b1)
922          ruo_rx_rdyr <= 1'b0; // rx serdes reset applied
923       else if((rx_any_rst==1)||(rx_all_well==0))
924          ruo_rx_rdyr <= 1'b0;
925       else if(rxr_wt_tc==1)
926          ruo_rx_rdyr <= 1'b1;
927    end
928    assign ruo_rx_rdy = ruo_rx_rdyr;
929    end         // if pport_rx_rdy
930    else
931       assign ruo_rx_rdy = 1'b0;
932    end // if ((pserdes_mode=="RX AND TX")||(pserdes_mode=="RX ONLY"))
933
934    else begin  // generate else (Tx only)
935       assign rdo_rx_serdes_rst_c = 4'd0;
936       assign rdo_rx_pcs_rst_c = 4'd0;
937       assign ruo_rx_rdy = 1'b0;
938    end // else: !if((pserdes_mode=="RX AND TX")||(pserdes_mode=="RX ONLY"))
939
940    endgenerate
941
942 endmodule
943
944
945 //   ===========================================================================
946 //   >>>>>>>>>>>>>>>>>>>>>>> COPYRIGHT NOTICE <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
947 //   ---------------------------------------------------------------------------
948 //   Copyright (c) 2015 by Lattice Semiconductor Corporation
949 //   ALL RIGHTS RESERVED 
950 //   ------------------------------------------------------------------
951 //
952 //   Permission:
953 //
954 //      Lattice SG Pte. Ltd. grants permission to use this code
955 //      pursuant to the terms of the Lattice Reference Design License Agreement. 
956 //
957 //
958 //   Disclaimer:
959 //
960 //      This VHDL or Verilog source code is intended as a design reference
961 //      which illustrates how these types of functions can be implemented.
962 //      It is the user's responsibility to verify their design for
963 //      consistency and functionality through the use of formal
964 //      verification methods.  Lattice provides no warranty
965 //      regarding the use or functionality of this code.
966 //
967 //   ---------------------------------------------------------------------------
968 //
969 //                  Lattice SG Pte. Ltd.
970 //                  101 Thomson Road, United Square #07-02 
971 //                  Singapore 307591
972 //
973 //
974 //                  TEL: 1-800-Lattice (USA and Canada)
975 //                       +65-6631-2000 (Singapore)
976 //                       +1-503-268-8001 (other locations)
977 //
978 //                  web: http://www.latticesemi.com/
979 //                  email: techsupport@latticesemi.com
980 //
981 //   ---------------------------------------------------------------------------
982 //
983 // =============================================================================
984 //                         FILE DETAILS         
985 // Project               : SLL - Soft Loss Of Lock(LOL) Logic
986 // File                  : sll_core.v
987 // Title                 : Top-level file for SLL 
988 // Dependencies          : 1.
989 //                       : 2.
990 // Description           : 
991 // =============================================================================
992 //                         REVISION HISTORY
993 // Version               : 1.0
994 // Author(s)             : AV
995 // Mod. Date             : March 2, 2015
996 // Changes Made          : Initial Creation
997 // =============================================================================
998 //                         REVISION HISTORY
999 // Version               : 1.1
1000 // Author(s)             : AV
1001 // Mod. Date             : June 8, 2015
1002 // Changes Made          : Following updates were made 
1003 //                       : 1. Changed all the PLOL status logic and FSM to run
1004 //                       :    on sli_refclk. 
1005 //                       : 2. Added the HB logic for presence of tx_pclk 
1006 //                       : 3. Changed the lparam assignment scheme for 
1007 //                       :    simulation purposes. 
1008 // =============================================================================
1009 //                         REVISION HISTORY
1010 // Version               : 1.2
1011 // Author(s)             : AV
1012 // Mod. Date             : June 24, 2015
1013 // Changes Made          : Updated the gearing logic for SDI dynamic rate change
1014 // =============================================================================
1015 //                         REVISION HISTORY
1016 // Version               : 1.3
1017 // Author(s)             : AV
1018 // Mod. Date             : July 14, 2015
1019 // Changes Made          : Added the logic for dynamic rate change in CPRI
1020 // =============================================================================
1021 //                         REVISION HISTORY
1022 // Version               : 1.4
1023 // Author(s)             : AV
1024 // Mod. Date             : August 21, 2015
1025 // Changes Made          : Added the logic for dynamic rate change of 5G CPRI & 
1026 //                         PCIe.
1027 // =============================================================================
1028 //                         REVISION HISTORY
1029 // Version               : 1.5
1030 // Author(s)             : ES/EB
1031 // Mod. Date             : March 21, 2017
1032 // Changes Made          : 1. Added pdiff_sync signal to syncrhonize pcount_diff 
1033 //                       :    to sli_refclk.
1034 //                       : 2. Updated terminal count logic for PCIe 5G
1035 //                       : 3. Modified checking of pcount_diff in SLL state
1036 //                       :    machine to cover actual count
1037 //                       :    (from 16-bits to 22-bits)
1038 // =============================================================================
1039 //                         REVISION HISTORY
1040 // Version               : 1.6
1041 // Author(s)             : ES
1042 // Mod. Date             : April 19, 2017
1043 // Changes Made          : 1. Added registered lock and unlock signal from
1044 //                            pdiff_sync to totally decouple pcount_diff from
1045 //                            SLL state machine.
1046 //                       : 2. Modified LPCLK_TC_4 to 1:1 clock ratio when CPRI
1047 //                            is operating @ 4.9125Gbps data rate.
1048 // =============================================================================
1049 `timescale 1ns/10ps
1050
1051 module sgmii_channel_smi_pcssll_core ( 
1052   //Reset and Clock inputs
1053   sli_rst,         //Active high asynchronous reset input
1054   sli_refclk,      //Refclk input to the Tx PLL
1055   sli_pclk,        //Tx pclk output from the PCS
1056   
1057   //Control inputs
1058   sli_div2_rate,   //Divide by 2 control; 0 - Full rate; 1 - Half rate
1059   sli_div11_rate,  //Divide by 11 control; 0 - Full rate; 1 - Div by 11
1060   sli_gear_mode,   //Gear mode control for PCS; 0 - 8/10; 1- 16/20
1061   sli_cpri_mode,   //Mode of operation specific to CPRI protocol
1062   sli_pcie_mode,   //Mode of operation specific to PCIe mode (2.5G or 5G)
1063   
1064   //LOL Output
1065   slo_plol         //Tx PLL Loss of Lock output to the user logic
1066   );
1067   
1068 // Inputs
1069 input       sli_rst;
1070 input       sli_refclk;
1071 input       sli_pclk;
1072 input       sli_div2_rate;
1073 input       sli_div11_rate;
1074 input       sli_gear_mode;
1075 input [2:0] sli_cpri_mode;
1076 input       sli_pcie_mode;
1077
1078 // Outputs
1079 output      slo_plol;
1080
1081
1082 // Parameters
1083 parameter PPROTOCOL              = "PCIE";     //Protocol selected by the User
1084 parameter PLOL_SETTING           = 0;          //PLL LOL setting. Possible values are 0,1,2,3
1085 parameter PDYN_RATE_CTRL         = "DISABLED"; //PCS Dynamic Rate control
1086 parameter PPCIE_MAX_RATE         = "2.5";      //PCIe max data rate
1087 parameter PDIFF_VAL_LOCK         = 20;         //Differential count value for Lock
1088 parameter PDIFF_VAL_UNLOCK       = 39;         //Differential count value for Unlock
1089 parameter PPCLK_TC               = 65535;      //Terminal count value for counter running on sli_pclk
1090 parameter PDIFF_DIV11_VAL_LOCK   = 3;          //Differential count value for Lock for SDI Div11
1091 parameter PDIFF_DIV11_VAL_UNLOCK = 3;          //Differential count value for Unlock for SDI Div11
1092 parameter PPCLK_DIV11_TC         = 2383;       //Terminal count value (SDI Div11) for counter running on sli_pclk
1093
1094
1095 // Local Parameters
1096 localparam [1:0]  LPLL_LOSS_ST         = 2'b00;       //PLL Loss state
1097 localparam [1:0]  LPLL_PRELOSS_ST      = 2'b01;       //PLL Pre-Loss state
1098 localparam [1:0]  LPLL_PRELOCK_ST      = 2'b10;       //PLL Pre-Lock state
1099 localparam [1:0]  LPLL_LOCK_ST         = 2'b11;       //PLL Lock state
1100 `ifdef RSL_SIM_MODE
1101 localparam [15:0] LRCLK_TC             = 16'd63;   //Terminal count value for counter running on sli_refclk
1102 `else
1103 localparam [15:0] LRCLK_TC             = 16'd65535;   //Terminal count value for counter running on sli_refclk
1104 `endif
1105 localparam [15:0] LRCLK_TC_PUL_WIDTH   = 16'd50;      //Pulse width for the Refclk terminal count pulse
1106 localparam [7:0]  LHB_WAIT_CNT         = 8'd255;      //Wait count for the Heartbeat signal
1107
1108 // Local Parameters related to the CPRI dynamic modes
1109 // Terminal count values for the four CPRI modes
1110 localparam LPCLK_TC_0 = 32768;
1111 localparam LPCLK_TC_1 = 65536;
1112 localparam LPCLK_TC_2 = 131072;
1113 localparam LPCLK_TC_3 = 163840;
1114 localparam LPCLK_TC_4 = 65536;
1115
1116 // Lock values count values for the four CPRI modes and four PLOL settings (4x5)
1117 // CPRI rate mode 0                CPRI rate mode 1                   CPRI rate mode 2                    CPRI rate mode 3                     CPRI rate mode 4
1118 localparam LPDIFF_LOCK_00 = 9;     localparam LPDIFF_LOCK_10 = 19;    localparam LPDIFF_LOCK_20 = 39;     localparam LPDIFF_LOCK_30 = 49;      localparam LPDIFF_LOCK_40 = 19;
1119 localparam LPDIFF_LOCK_01 = 9;     localparam LPDIFF_LOCK_11 = 19;    localparam LPDIFF_LOCK_21 = 39;     localparam LPDIFF_LOCK_31 = 49;      localparam LPDIFF_LOCK_41 = 19;
1120 localparam LPDIFF_LOCK_02 = 49;    localparam LPDIFF_LOCK_12 = 98;    localparam LPDIFF_LOCK_22 = 196;    localparam LPDIFF_LOCK_32 = 245;     localparam LPDIFF_LOCK_42 = 98;
1121 localparam LPDIFF_LOCK_03 = 131;   localparam LPDIFF_LOCK_13 = 262;   localparam LPDIFF_LOCK_23 = 524;    localparam LPDIFF_LOCK_33 = 655;     localparam LPDIFF_LOCK_43 = 262;
1122
1123 // Unlock values count values for the four CPRI modes and four PLOL settings (4x5)
1124 // CPRI rate mode 0                  CPRI rate mode 1                      CPRI rate mode 2                       CPRI rate mode 3                         CPRI rate mode 4
1125 localparam LPDIFF_UNLOCK_00 = 19;    localparam LPDIFF_UNLOCK_10 = 39;     localparam LPDIFF_UNLOCK_20 = 78;      localparam LPDIFF_UNLOCK_30 = 98;        localparam LPDIFF_UNLOCK_40 = 39;
1126 localparam LPDIFF_UNLOCK_01 = 65;    localparam LPDIFF_UNLOCK_11 = 131;    localparam LPDIFF_UNLOCK_21 = 262;     localparam LPDIFF_UNLOCK_31 = 327;       localparam LPDIFF_UNLOCK_41 = 131;
1127 localparam LPDIFF_UNLOCK_02 = 72;    localparam LPDIFF_UNLOCK_12 = 144;    localparam LPDIFF_UNLOCK_22 = 288;     localparam LPDIFF_UNLOCK_32 = 360;       localparam LPDIFF_UNLOCK_42 = 144;
1128 localparam LPDIFF_UNLOCK_03 = 196;   localparam LPDIFF_UNLOCK_13 = 393;    localparam LPDIFF_UNLOCK_23 = 786;     localparam LPDIFF_UNLOCK_33 = 983;       localparam LPDIFF_UNLOCK_43 = 393;
1129
1130 // Input and Output reg and wire declarations
1131 wire       sli_rst;
1132 wire       sli_refclk;
1133 wire       sli_pclk;
1134 wire       sli_div2_rate;
1135 wire       sli_div11_rate;
1136 wire       sli_gear_mode;
1137 wire [2:0] sli_cpri_mode;
1138 wire       sli_pcie_mode;
1139 wire       slo_plol;
1140
1141 //-------------- Internal signals reg and wire declarations --------------------
1142
1143 //Signals running on sli_refclk
1144 reg  [15:0] rcount;           //16-bit Counter
1145 reg         rtc_pul;          //Terminal count pulse
1146 reg         rtc_pul_p1;       //Terminal count pulse pipeline
1147 reg         rtc_ctrl;         //Terminal count pulse control
1148
1149 reg  [7:0]  rhb_wait_cnt;     //Heartbeat wait counter
1150
1151 //Heatbeat synchronization and pipeline registers
1152 wire        rhb_sync;
1153 reg         rhb_sync_p2;
1154 reg         rhb_sync_p1;
1155
1156 //Pipeling registers for dynamic control mode
1157 wire        rgear;
1158 wire        rdiv2;
1159 wire        rdiv11;
1160 reg         rgear_p1;
1161 reg         rdiv2_p1;
1162 reg         rdiv11_p1;
1163
1164 reg         rstat_pclk;        //Pclk presence/absence status
1165
1166 reg  [21:0] rcount_tc;         //Tx_pclk terminal count register
1167 reg  [15:0] rdiff_comp_lock;   //Differential comparison value for Lock
1168 reg  [15:0] rdiff_comp_unlock; //Differential compariosn value for Unlock
1169
1170 wire        rpcie_mode;        //PCIe mode signal synchronized to refclk
1171 reg         rpcie_mode_p1;     //PCIe mode pipeline register
1172
1173 wire        rcpri_mod_ch_sync; //CPRI mode change synchronized to refclk
1174 reg         rcpri_mod_ch_p1;   //CPRI mode change pipeline register
1175 reg         rcpri_mod_ch_p2;   //CPRI mode change pipeline register
1176 reg         rcpri_mod_ch_st;   //CPRI mode change status
1177
1178 reg  [1:0]  sll_state;         //Current-state register for LOL FSM
1179
1180 reg         pll_lock;          //PLL Lock signal
1181
1182 //Signals running on sli_pclk
1183 //Synchronization and pipeline registers
1184 wire        ppul_sync;
1185 reg         ppul_sync_p1;
1186 reg         ppul_sync_p2;
1187 reg         ppul_sync_p3;
1188
1189 wire        pdiff_sync;
1190 reg         pdiff_sync_p1;
1191    
1192 reg  [21:0] pcount;            //22-bit counter
1193 reg  [21:0] pcount_diff;       //Differential value between Tx_pclk counter and theoritical value
1194
1195 //Heartbeat counter and heartbeat signal running on pclk
1196 reg  [2:0]  phb_cnt;
1197 reg         phb;
1198
1199 //CPRI dynamic mode releated signals
1200 reg  [2:0]  pcpri_mode;
1201 reg         pcpri_mod_ch;
1202
1203 //Assignment scheme changed mainly for simulation purpose
1204 wire [15:0] LRCLK_TC_w;
1205 assign LRCLK_TC_w = LRCLK_TC;
1206
1207 reg         unlock;
1208 reg         lock;
1209
1210 //Heartbeat synchronization
1211 sync # (.PDATA_RST_VAL(0)) phb_sync_inst ( 
1212   .clk     (sli_refclk),
1213   .rst     (sli_rst),
1214   .data_in (phb),
1215   .data_out(rhb_sync)
1216   );
1217   
1218   
1219 //Terminal count pulse synchronization
1220 sync # (.PDATA_RST_VAL(0)) rtc_sync_inst ( 
1221   .clk     (sli_pclk),
1222   .rst     (sli_rst),
1223   .data_in (rtc_pul),
1224   .data_out(ppul_sync)
1225   );
1226
1227 //Differential value logic update synchronization
1228 sync # (.PDATA_RST_VAL(0)) pdiff_sync_inst ( 
1229   .clk     (sli_refclk),
1230   .rst     (sli_rst),
1231   .data_in (ppul_sync),
1232   .data_out(pdiff_sync)
1233   );
1234
1235 //Gear mode synchronization
1236 sync # (.PDATA_RST_VAL(0)) gear_sync_inst ( 
1237   .clk     (sli_refclk),
1238   .rst     (sli_rst),
1239   .data_in (sli_gear_mode),
1240   .data_out(rgear)
1241   );
1242   
1243 //Div2 synchronization
1244 sync # (.PDATA_RST_VAL(0)) div2_sync_inst ( 
1245   .clk     (sli_refclk),
1246   .rst     (sli_rst),
1247   .data_in (sli_div2_rate),
1248   .data_out(rdiv2)
1249   );
1250   
1251 //Div11 synchronization
1252 sync # (.PDATA_RST_VAL(0)) div11_sync_inst ( 
1253   .clk     (sli_refclk),
1254   .rst     (sli_rst),
1255   .data_in (sli_div11_rate),
1256   .data_out(rdiv11)
1257   );
1258   
1259 //CPRI mode change synchronization
1260 sync # (.PDATA_RST_VAL(0)) cpri_mod_sync_inst ( 
1261   .clk     (sli_refclk),
1262   .rst     (sli_rst),
1263   .data_in (pcpri_mod_ch),
1264   .data_out(rcpri_mod_ch_sync)
1265   );
1266   
1267 //PCIe mode change synchronization
1268 sync # (.PDATA_RST_VAL(0)) pcie_mod_sync_inst ( 
1269   .clk     (sli_refclk),
1270   .rst     (sli_rst),
1271   .data_in (sli_pcie_mode),
1272   .data_out(rpcie_mode)
1273   );  
1274
1275 // =============================================================================
1276 // Synchronized Lock/Unlock signals
1277 // =============================================================================
1278 always @(posedge sli_refclk or posedge sli_rst) begin
1279   if (sli_rst == 1'b1) begin
1280     unlock        <= 1'b0;
1281     lock          <= 1'b0;
1282     pdiff_sync_p1 <= 1'b0;
1283   end
1284   else begin
1285     pdiff_sync_p1 <= pdiff_sync;
1286     if (unlock) begin
1287       unlock <= ~pdiff_sync && pdiff_sync_p1 ? 1'b0 : unlock;
1288     end
1289     else begin
1290       unlock <= pdiff_sync ? (pcount_diff[21:0] > {6'd0, rdiff_comp_unlock}) : 1'b0;
1291     end
1292     if (lock) begin
1293       lock <= ~pdiff_sync && pdiff_sync_p1 ? 1'b0 : lock;
1294     end
1295     else begin
1296       lock <= pdiff_sync ? (pcount_diff[21:0] <= {6'd0, rdiff_comp_lock}) : 1'b0;
1297     end
1298   end
1299 end
1300
1301 // =============================================================================
1302 // Refclk Counter, pulse generation logic and Heartbeat monitor logic
1303 // =============================================================================
1304 always @(posedge sli_refclk or posedge sli_rst) begin
1305   if (sli_rst == 1'b1) begin
1306     rcount     <= 16'd0;
1307     rtc_pul    <= 1'b0;
1308     rtc_ctrl   <= 1'b0;
1309     rtc_pul_p1 <= 1'b0;
1310   end
1311   else begin
1312     //Counter logic
1313     if ((rgear_p1^rgear == 1'b1) || (rdiv2_p1^rdiv2 == 1'b1) || (rdiv11_p1^rdiv11 == 1'b1) || (rcpri_mod_ch_p1^rcpri_mod_ch_p2 == 1'b1) || (rpcie_mode_p1^rpcie_mode == 1'b1)) begin
1314       if (rtc_ctrl == 1'b1) begin
1315         rcount <= LRCLK_TC_PUL_WIDTH;
1316       end  
1317     end
1318     else begin
1319       if (rcount != LRCLK_TC_w) begin
1320         rcount <= rcount + 1;
1321       end
1322       else begin
1323         rcount <= 16'd0;   
1324       end
1325     end
1326     
1327     //Pulse control logic
1328     if (rcount == LRCLK_TC_w - 1) begin
1329       rtc_ctrl <= 1'b1;
1330     end
1331     
1332     //Pulse Generation logic
1333     if (rtc_ctrl == 1'b1) begin
1334       if ((rcount == LRCLK_TC_w) || (rcount < LRCLK_TC_PUL_WIDTH)) begin
1335         rtc_pul <= 1'b1;
1336     end  
1337       else begin
1338         rtc_pul <= 1'b0;  
1339       end
1340     end
1341     
1342     rtc_pul_p1 <= rtc_pul;  
1343   end  
1344 end
1345
1346
1347 // =============================================================================
1348 // Heartbeat synchronization & monitor logic and Dynamic mode pipeline logic 
1349 // =============================================================================
1350 always @(posedge sli_refclk or posedge sli_rst) begin
1351   if (sli_rst == 1'b1) begin
1352     rhb_sync_p1     <= 1'b0;
1353     rhb_sync_p2     <= 1'b0;
1354     rhb_wait_cnt    <= 8'd0;
1355     rstat_pclk      <= 1'b0;
1356     rgear_p1        <= 1'b0;
1357     rdiv2_p1        <= 1'b0;
1358     rdiv11_p1       <= 1'b0;
1359     rcpri_mod_ch_p1 <= 1'b0;
1360     rcpri_mod_ch_p2 <= 1'b0;
1361     rcpri_mod_ch_st <= 1'b0;
1362     rpcie_mode_p1   <= 1'b0;
1363     
1364   end
1365   else begin
1366     //Pipeline stages for the Heartbeat
1367     rhb_sync_p1 <= rhb_sync;
1368     rhb_sync_p2 <= rhb_sync_p1;
1369     
1370     //Pipeline stages of the Dynamic rate control signals
1371     rgear_p1  <= rgear;
1372     rdiv2_p1  <= rdiv2;
1373     rdiv11_p1 <= rdiv11;
1374     
1375     //Pipeline stage for PCIe mode
1376     rpcie_mode_p1 <= rpcie_mode;
1377     
1378     //Pipeline stage for CPRI mode change
1379     rcpri_mod_ch_p1 <= rcpri_mod_ch_sync;
1380     rcpri_mod_ch_p2 <= rcpri_mod_ch_p1;
1381     
1382     //CPRI mode change status logic
1383     if (rcpri_mod_ch_p1^rcpri_mod_ch_sync == 1'b1) begin
1384       rcpri_mod_ch_st <= 1'b1;
1385     end 
1386     
1387     //Heartbeat wait counter and monitor logic
1388     if (rtc_ctrl == 1'b1) begin
1389       if (rhb_sync_p1 == 1'b1 && rhb_sync_p2 == 1'b0) begin
1390         rhb_wait_cnt <= 8'd0;
1391         rstat_pclk   <= 1'b1;
1392       end
1393       else if (rhb_wait_cnt == LHB_WAIT_CNT) begin
1394         rhb_wait_cnt <= 8'd0;
1395         rstat_pclk   <= 1'b0;
1396       end
1397       else begin
1398         rhb_wait_cnt <= rhb_wait_cnt + 1;
1399       end
1400     end
1401   end  
1402 end
1403
1404
1405 // =============================================================================
1406 // Pipleline registers for the TC pulse and CPRI mode change logic
1407 // =============================================================================
1408 always @(posedge sli_pclk or posedge sli_rst) begin
1409   if (sli_rst == 1'b1) begin
1410     ppul_sync_p1 <= 1'b0;
1411     ppul_sync_p2 <= 1'b0;
1412     ppul_sync_p3 <= 1'b0;
1413     pcpri_mode   <= 3'b0;
1414     pcpri_mod_ch <= 1'b0;
1415   end
1416   else begin
1417     ppul_sync_p1 <= ppul_sync;
1418     ppul_sync_p2 <= ppul_sync_p1;
1419     ppul_sync_p3 <= ppul_sync_p2;
1420     
1421     //CPRI mode change logic
1422     pcpri_mode <= sli_cpri_mode;
1423     
1424     if (pcpri_mode != sli_cpri_mode) begin
1425       pcpri_mod_ch <= ~pcpri_mod_ch;
1426     end 
1427   end  
1428 end
1429    
1430
1431 // =============================================================================
1432 // Terminal count logic
1433 // =============================================================================
1434
1435 //For SDI protocol with Dynamic rate control enabled
1436 generate
1437 if ((PDYN_RATE_CTRL == "ENABLED") && (PPROTOCOL == "SDI")) begin
1438 always @(posedge sli_refclk or posedge sli_rst) begin
1439   if (sli_rst == 1'b1) begin
1440     rcount_tc         <= 22'd0;
1441     rdiff_comp_lock   <= 16'd0;
1442     rdiff_comp_unlock <= 16'd0;
1443   end
1444   else begin
1445     //Terminal count logic
1446     //Div by 11 is enabled
1447     if (rdiv11 == 1'b1) begin
1448       //Gear mode is 16/20
1449       if (rgear == 1'b1) begin
1450         rcount_tc         <= PPCLK_DIV11_TC;
1451         rdiff_comp_lock   <= PDIFF_DIV11_VAL_LOCK;
1452         rdiff_comp_unlock <= PDIFF_DIV11_VAL_UNLOCK;
1453       end
1454       else begin
1455         rcount_tc         <= {PPCLK_DIV11_TC[20:0], 1'b0};
1456         rdiff_comp_lock   <= {PDIFF_DIV11_VAL_LOCK[14:0], 1'b0};
1457         rdiff_comp_unlock <= {PDIFF_DIV11_VAL_UNLOCK[14:0], 1'b0};
1458       end
1459     end
1460     //Div by 2 is enabled
1461     else if (rdiv2 == 1'b1) begin
1462       //Gear mode is 16/20
1463       if (rgear == 1'b1) begin
1464         rcount_tc         <= {1'b0,PPCLK_TC[21:1]};
1465         rdiff_comp_lock   <= {1'b0,PDIFF_VAL_LOCK[15:1]};
1466         rdiff_comp_unlock <= {1'b0,PDIFF_VAL_UNLOCK[15:1]};
1467       end
1468       else begin
1469         rcount_tc         <= PPCLK_TC;
1470         rdiff_comp_lock   <= PDIFF_VAL_LOCK;
1471         rdiff_comp_unlock <= PDIFF_VAL_UNLOCK;
1472       end
1473     end
1474     //Both div by 11 and div by 2 are disabled
1475     else begin
1476       //Gear mode is 16/20
1477       if (rgear == 1'b1) begin
1478         rcount_tc         <= PPCLK_TC;
1479         rdiff_comp_lock   <= PDIFF_VAL_LOCK;
1480         rdiff_comp_unlock <= PDIFF_VAL_UNLOCK;
1481       end
1482       else begin
1483         rcount_tc         <= {PPCLK_TC[20:0],1'b0};
1484         rdiff_comp_lock   <= {PDIFF_VAL_LOCK[14:0],1'b0};
1485         rdiff_comp_unlock <= {PDIFF_VAL_UNLOCK[14:0],1'b0};
1486       end
1487     end
1488   end  
1489 end
1490 end
1491 endgenerate
1492
1493 //For G8B10B protocol with Dynamic rate control enabled
1494 generate
1495 if ((PDYN_RATE_CTRL == "ENABLED") && (PPROTOCOL == "G8B10B")) begin
1496 always @(posedge sli_refclk or posedge sli_rst) begin
1497   if (sli_rst == 1'b1) begin
1498     rcount_tc         <= 22'd0;
1499     rdiff_comp_lock   <= 16'd0;
1500     rdiff_comp_unlock <= 16'd0;
1501   end
1502   else begin
1503     //Terminal count logic
1504     //Div by 2 is enabled
1505     if (rdiv2 == 1'b1) begin
1506       rcount_tc         <= {1'b0,PPCLK_TC[21:1]};
1507       rdiff_comp_lock   <= {1'b0,PDIFF_VAL_LOCK[15:1]};
1508       rdiff_comp_unlock <= {1'b0,PDIFF_VAL_UNLOCK[15:1]};
1509     end
1510     else begin
1511       rcount_tc         <= PPCLK_TC;
1512       rdiff_comp_lock   <= PDIFF_VAL_LOCK;
1513       rdiff_comp_unlock <= PDIFF_VAL_UNLOCK;
1514     end
1515   end  
1516 end
1517 end
1518 endgenerate
1519
1520
1521 //For CPRI protocol with Dynamic rate control is disabled
1522 generate
1523 if ((PDYN_RATE_CTRL == "DISABLED") && (PPROTOCOL == "CPRI")) begin
1524 always @(posedge sli_refclk or posedge sli_rst) begin
1525   if (sli_rst == 1'b1) begin
1526     rcount_tc         <= 22'd0;
1527     rdiff_comp_lock   <= 16'd0;
1528     rdiff_comp_unlock <= 16'd0;
1529   end
1530   else begin
1531     //Terminal count logic for CPRI protocol
1532     //Only if there is a change in the rate mode from the default
1533     if (rcpri_mod_ch_st == 1'b1) begin
1534       if (rcpri_mod_ch_p1^rcpri_mod_ch_p2 == 1'b1) begin
1535         case(sli_cpri_mode)
1536           3'd0 : begin //For 0.6Gbps
1537             rcount_tc         <= LPCLK_TC_0;
1538             case(PLOL_SETTING)
1539               'd0 : begin
1540                 rdiff_comp_lock   <= LPDIFF_LOCK_00;
1541                 rdiff_comp_unlock <= LPDIFF_UNLOCK_00;
1542               end
1543               
1544               'd1 : begin
1545                 rdiff_comp_lock   <= LPDIFF_LOCK_01;
1546                 rdiff_comp_unlock <= LPDIFF_UNLOCK_01;
1547               end
1548               
1549               'd2 : begin
1550                 rdiff_comp_lock   <= LPDIFF_LOCK_02;
1551                 rdiff_comp_unlock <= LPDIFF_UNLOCK_02;
1552               end
1553               
1554               'd3 : begin
1555                 rdiff_comp_lock   <= LPDIFF_LOCK_03;
1556                 rdiff_comp_unlock <= LPDIFF_UNLOCK_03;
1557               end
1558               
1559               default : begin
1560                 rdiff_comp_lock   <= LPDIFF_LOCK_00;
1561                 rdiff_comp_unlock <= LPDIFF_UNLOCK_00;
1562               end
1563             endcase
1564           end
1565           
1566           3'd1 : begin //For 1.2Gbps
1567             rcount_tc         <= LPCLK_TC_1;
1568             case(PLOL_SETTING)
1569               'd0 : begin
1570                 rdiff_comp_lock   <= LPDIFF_LOCK_10;
1571                 rdiff_comp_unlock <= LPDIFF_UNLOCK_10;
1572               end
1573               
1574               'd1 : begin
1575                 rdiff_comp_lock   <= LPDIFF_LOCK_11;
1576                 rdiff_comp_unlock <= LPDIFF_UNLOCK_11;
1577               end
1578               
1579               'd2 : begin
1580                 rdiff_comp_lock   <= LPDIFF_LOCK_12;
1581                 rdiff_comp_unlock <= LPDIFF_UNLOCK_12;
1582               end
1583               
1584               'd3 : begin
1585                 rdiff_comp_lock   <= LPDIFF_LOCK_13;
1586                 rdiff_comp_unlock <= LPDIFF_UNLOCK_13;
1587               end
1588               
1589               default : begin
1590                 rdiff_comp_lock   <= LPDIFF_LOCK_10;
1591                 rdiff_comp_unlock <= LPDIFF_UNLOCK_10;
1592               end
1593             endcase
1594           end
1595           
1596           3'd2 : begin //For 2.4Gbps
1597             rcount_tc         <= LPCLK_TC_2;
1598             case(PLOL_SETTING)
1599               'd0 : begin
1600                 rdiff_comp_lock   <= LPDIFF_LOCK_20;
1601                 rdiff_comp_unlock <= LPDIFF_UNLOCK_20;
1602               end
1603               
1604               'd1 : begin
1605                 rdiff_comp_lock   <= LPDIFF_LOCK_21;
1606                 rdiff_comp_unlock <= LPDIFF_UNLOCK_21;
1607               end
1608               
1609               'd2 : begin
1610                 rdiff_comp_lock   <= LPDIFF_LOCK_22;
1611                 rdiff_comp_unlock <= LPDIFF_UNLOCK_22;
1612               end
1613               
1614               'd3 : begin
1615                 rdiff_comp_lock   <= LPDIFF_LOCK_23;
1616                 rdiff_comp_unlock <= LPDIFF_UNLOCK_23;
1617               end
1618               
1619               default : begin
1620                 rdiff_comp_lock   <= LPDIFF_LOCK_20;
1621                 rdiff_comp_unlock <= LPDIFF_UNLOCK_20;
1622               end
1623             endcase
1624           end
1625           
1626           3'd3 : begin //For 3.07Gbps
1627             rcount_tc         <= LPCLK_TC_3;
1628             case(PLOL_SETTING)
1629               'd0 : begin
1630                 rdiff_comp_lock   <= LPDIFF_LOCK_30;
1631                 rdiff_comp_unlock <= LPDIFF_UNLOCK_30;
1632               end
1633               
1634               'd1 : begin
1635                 rdiff_comp_lock   <= LPDIFF_LOCK_31;
1636                 rdiff_comp_unlock <= LPDIFF_UNLOCK_31;
1637               end
1638               
1639               'd2 : begin
1640                 rdiff_comp_lock   <= LPDIFF_LOCK_32;
1641                 rdiff_comp_unlock <= LPDIFF_UNLOCK_32;
1642               end
1643               
1644               'd3 : begin
1645                 rdiff_comp_lock   <= LPDIFF_LOCK_33;
1646                 rdiff_comp_unlock <= LPDIFF_UNLOCK_33;
1647               end
1648             endcase
1649           end    
1650               
1651           3'd4 : begin //For 4.9125bps
1652             rcount_tc         <= LPCLK_TC_4;
1653             case(PLOL_SETTING)
1654               'd0 : begin
1655                 rdiff_comp_lock   <= LPDIFF_LOCK_40;
1656                 rdiff_comp_unlock <= LPDIFF_UNLOCK_40;
1657               end
1658               
1659               'd1 : begin
1660                 rdiff_comp_lock   <= LPDIFF_LOCK_41;
1661                 rdiff_comp_unlock <= LPDIFF_UNLOCK_41;
1662               end
1663               
1664               'd2 : begin
1665                 rdiff_comp_lock   <= LPDIFF_LOCK_42;
1666                 rdiff_comp_unlock <= LPDIFF_UNLOCK_42;
1667               end
1668               
1669               'd3 : begin
1670                 rdiff_comp_lock   <= LPDIFF_LOCK_43;
1671                 rdiff_comp_unlock <= LPDIFF_UNLOCK_43;
1672               end  
1673             
1674               default : begin
1675                 rdiff_comp_lock   <= LPDIFF_LOCK_40;
1676                 rdiff_comp_unlock <= LPDIFF_UNLOCK_40;
1677               end
1678             endcase
1679           end
1680         
1681           default : begin
1682             rcount_tc         <= LPCLK_TC_0;
1683             rdiff_comp_lock   <= LPDIFF_LOCK_00;
1684             rdiff_comp_unlock <= LPDIFF_UNLOCK_00;
1685           end
1686         endcase
1687       end
1688     end
1689     else begin
1690       //If there is no change in the CPRI rate mode from default
1691       rcount_tc         <= PPCLK_TC;
1692       rdiff_comp_lock   <= PDIFF_VAL_LOCK;
1693       rdiff_comp_unlock <= PDIFF_VAL_UNLOCK;
1694     end  
1695   end  
1696 end
1697 end
1698 endgenerate
1699
1700 //For PCIe protocol with Dynamic rate control disabled
1701 generate
1702 if ((PDYN_RATE_CTRL == "DISABLED") && (PPROTOCOL == "PCIE")) begin
1703 always @(posedge sli_refclk or posedge sli_rst) begin
1704   if (sli_rst == 1'b1) begin
1705     rcount_tc         <= 22'd0;
1706     rdiff_comp_lock   <= 16'd0;
1707     rdiff_comp_unlock <= 16'd0;
1708   end
1709   else begin
1710     //Terminal count logic
1711     if (PPCIE_MAX_RATE == "2.5") begin
1712       //2.5G mode is enabled
1713       rcount_tc         <= PPCLK_TC;
1714       rdiff_comp_lock   <= PDIFF_VAL_LOCK;
1715       rdiff_comp_unlock <= PDIFF_VAL_UNLOCK;
1716     end
1717     else begin
1718       //5G mode is enabled
1719       if (rpcie_mode == 1'b1) begin
1720         rcount_tc         <= PPCLK_TC;
1721         rdiff_comp_lock   <= PDIFF_VAL_LOCK;
1722         rdiff_comp_unlock <= PDIFF_VAL_UNLOCK;
1723       end
1724       else begin
1725         //2.5G mode is enabled
1726         rcount_tc         <= {1'b0,PPCLK_TC[21:1]};
1727         rdiff_comp_lock   <= {1'b0,PDIFF_VAL_LOCK[15:1]};
1728         rdiff_comp_unlock <= {1'b0,PDIFF_VAL_UNLOCK[15:1]};
1729       end 
1730     end           
1731   end  
1732 end
1733 end
1734 endgenerate
1735
1736 //For all protocols other than CPRI & PCIe
1737 generate
1738 if ((PDYN_RATE_CTRL == "DISABLED") && ((PPROTOCOL != "CPRI") && (PPROTOCOL != "PCIE"))) begin
1739 always @(posedge sli_refclk or posedge sli_rst) begin
1740   if (sli_rst == 1'b1) begin
1741     rcount_tc         <= 22'd0;
1742     rdiff_comp_lock   <= 16'd0;
1743     rdiff_comp_unlock <= 16'd0;
1744   end
1745   else begin
1746     //Terminal count logic for all protocols other than CPRI & PCIe
1747     rcount_tc         <= PPCLK_TC;
1748     rdiff_comp_lock   <= PDIFF_VAL_LOCK;
1749     rdiff_comp_unlock <= PDIFF_VAL_UNLOCK;
1750   end  
1751 end
1752 end
1753 endgenerate
1754
1755
1756 // =============================================================================
1757 // Tx_pclk counter, Heartbeat and Differential value logic
1758 // =============================================================================
1759 always @(posedge sli_pclk or posedge sli_rst) begin
1760   if (sli_rst == 1'b1) begin
1761     pcount      <= 22'd0;
1762     pcount_diff <= 22'd65535;
1763     phb_cnt     <= 3'd0;
1764     phb         <= 1'b0;
1765   end
1766   else begin
1767     //Counter logic
1768     if (ppul_sync_p1 == 1'b1 && ppul_sync_p2 == 1'b0) begin
1769       pcount <= 22'd0;
1770     end
1771     else begin
1772       pcount <= pcount + 1;
1773     end
1774     
1775     //Heartbeat logic
1776     phb_cnt <= phb_cnt + 1;
1777     
1778     if ((phb_cnt < 3'd4) && (phb_cnt >= 3'd0)) begin
1779       phb <= 1'b1;
1780     end  
1781     else begin
1782       phb <= 1'b0;  
1783     end 
1784     
1785     //Differential value logic
1786     if (ppul_sync_p1 == 1'b1 && ppul_sync_p2 == 1'b0) begin
1787       pcount_diff <= rcount_tc + ~(pcount) + 1;
1788     end  
1789     else if (ppul_sync_p2 == 1'b1 && ppul_sync_p3 == 1'b0) begin
1790       if (pcount_diff[21] == 1'b1) begin
1791         pcount_diff <= ~(pcount_diff) + 1;
1792       end
1793     end
1794   end  
1795 end
1796
1797
1798 // =============================================================================
1799 // State transition logic for SLL FSM
1800 // =============================================================================
1801 always @(posedge sli_refclk or posedge sli_rst) begin
1802   if (sli_rst == 1'b1) begin
1803     sll_state <= LPLL_LOSS_ST; 
1804   end
1805   else begin
1806     //Reasons to declare an immediate loss - Absence of Tx_pclk, Dynamic rate change for SDI or CPRI
1807     if ((rstat_pclk == 1'b0) || (rgear_p1^rgear == 1'b1) || (rdiv2_p1^rdiv2 == 1'b1) || 
1808     (rdiv11_p1^rdiv11 == 1'b1) || (rcpri_mod_ch_p1^rcpri_mod_ch_p2 == 1'b1) || (rpcie_mode_p1^rpcie_mode == 1'b1)) begin
1809       sll_state <= LPLL_LOSS_ST;
1810     end
1811     else begin  
1812       case(sll_state)
1813         LPLL_LOSS_ST : begin
1814           if (rtc_pul_p1 == 1'b1 && rtc_pul == 1'b0) begin
1815             if (unlock) begin
1816               sll_state <= LPLL_LOSS_ST;
1817             end
1818             else if (lock) begin
1819               if (PLOL_SETTING == 2'd0) begin
1820                 sll_state <= LPLL_PRELOCK_ST;
1821               end
1822               else begin
1823                 sll_state <= LPLL_LOCK_ST;
1824               end
1825             end
1826           end
1827         end
1828         
1829         LPLL_LOCK_ST : begin
1830           if (rtc_pul_p1 == 1'b1 && rtc_pul == 1'b0) begin
1831             if (lock) begin
1832               sll_state <= LPLL_LOCK_ST;
1833             end
1834             else begin
1835               if (PLOL_SETTING == 2'd0) begin
1836                 sll_state <= LPLL_LOSS_ST;
1837               end
1838               else begin
1839                 sll_state <= LPLL_PRELOSS_ST;
1840               end
1841             end
1842           end
1843         end
1844         
1845         LPLL_PRELOCK_ST : begin
1846           if (rtc_pul_p1 == 1'b1 && rtc_pul == 1'b0) begin
1847             if (lock) begin
1848               sll_state <= LPLL_LOCK_ST;
1849             end
1850             else begin
1851               sll_state <= LPLL_PRELOSS_ST;
1852             end
1853           end
1854         end
1855         
1856         LPLL_PRELOSS_ST : begin
1857           if (rtc_pul_p1 == 1'b1 && rtc_pul == 1'b0) begin
1858             if (unlock) begin
1859               sll_state <= LPLL_PRELOSS_ST;
1860             end
1861             else if (lock) begin
1862               sll_state <= LPLL_LOCK_ST;
1863             end
1864           end
1865         end
1866         
1867         default: begin
1868           sll_state <= LPLL_LOSS_ST;
1869         end
1870       endcase
1871     end  
1872   end  
1873 end
1874
1875
1876 // =============================================================================
1877 // Logic for Tx PLL Lock
1878 // =============================================================================
1879 always @(posedge sli_refclk or posedge sli_rst) begin
1880   if (sli_rst == 1'b1) begin
1881     pll_lock <= 1'b0; 
1882   end
1883   else begin
1884     case(sll_state)
1885       LPLL_LOSS_ST : begin
1886         pll_lock <= 1'b0;
1887       end
1888       
1889       LPLL_LOCK_ST : begin
1890         pll_lock <= 1'b1;
1891       end
1892       
1893       LPLL_PRELOSS_ST : begin
1894         pll_lock <= 1'b0;
1895       end
1896       
1897       default: begin
1898         pll_lock <= 1'b0;
1899       end
1900     endcase
1901   end  
1902 end
1903
1904 assign slo_plol = ~(pll_lock);
1905
1906 endmodule  
1907
1908
1909 //   ===========================================================================
1910 //   >>>>>>>>>>>>>>>>>>>>>>> COPYRIGHT NOTICE <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1911 //   ---------------------------------------------------------------------------
1912 //   Copyright (c) 2015 by Lattice Semiconductor Corporation
1913 //   ALL RIGHTS RESERVED 
1914 //   ------------------------------------------------------------------
1915 //
1916 //   Permission:
1917 //
1918 //      Lattice SG Pte. Ltd. grants permission to use this code
1919 //      pursuant to the terms of the Lattice Reference Design License Agreement. 
1920 //
1921 //
1922 //   Disclaimer:
1923 //
1924 //      This VHDL or Verilog source code is intended as a design reference
1925 //      which illustrates how these types of functions can be implemented.
1926 //      It is the user's responsibility to verify their design for
1927 //      consistency and functionality through the use of formal
1928 //      verification methods.  Lattice provides no warranty
1929 //      regarding the use or functionality of this code.
1930 //
1931 //   ---------------------------------------------------------------------------
1932 //
1933 //                  Lattice SG Pte. Ltd.
1934 //                  101 Thomson Road, United Square #07-02 
1935 //                  Singapore 307591
1936 //
1937 //
1938 //                  TEL: 1-800-Lattice (USA and Canada)
1939 //                       +65-6631-2000 (Singapore)
1940 //                       +1-503-268-8001 (other locations)
1941 //
1942 //                  web: http://www.latticesemi.com/
1943 //                  email: techsupport@latticesemi.com
1944 //
1945 //   ---------------------------------------------------------------------------
1946 //
1947 // =============================================================================
1948 //                         FILE DETAILS
1949 // Project               : Synchronizer Logic
1950 // File                  : sync.v
1951 // Title                 : Synchronizer module
1952 // Description           : 
1953 // =============================================================================
1954 //                         REVISION HISTORY
1955 // Version               : 1.0
1956 // Author(s)             : AV
1957 // Mod. Date             : July 7, 2015
1958 // Changes Made          : Initial Creation
1959 // -----------------------------------------------------------------------------
1960 // Version               : 1.1
1961 // Author(s)             : EB
1962 // Mod. Date             : March 21, 2017
1963 // Changes Made          : 
1964 // =============================================================================
1965
1966 `ifndef PCS_SYNC_MODULE
1967 `define PCS_SYNC_MODULE
1968 module sync ( 
1969   clk,
1970   rst,
1971   data_in,
1972   data_out
1973   );
1974   
1975 input  clk;                  //Clock in which the async data needs to be synchronized to
1976 input  rst;                  //Active high reset
1977 input  data_in;              //Asynchronous data
1978 output data_out;             //Synchronized data
1979
1980 parameter PDATA_RST_VAL = 0; //Reset value for the registers
1981
1982 reg data_p1;
1983 reg data_p2;
1984
1985 // =============================================================================
1986 // Synchronization logic
1987 // =============================================================================
1988 always @(posedge clk or posedge rst) begin
1989   if (rst == 1'b1) begin
1990     data_p1 <= PDATA_RST_VAL;
1991     data_p2 <= PDATA_RST_VAL; 
1992   end
1993   else begin
1994     data_p1 <= data_in;
1995     data_p2 <= data_p1;
1996   end  
1997 end
1998
1999 assign data_out = data_p2; 
2000
2001 endmodule    
2002 `endif
2003