0b03524a92d4d1380938f0f99d8fa70fc762beeb
[fleet.git] / src / edu / berkeley / fleet / fpga / greg / ddr2_phy_init.v
1 //*****************************************************************************
2 // DISCLAIMER OF LIABILITY
3 //
4 // This text/file contains proprietary, confidential
5 // information of Xilinx, Inc., is distributed under license
6 // from Xilinx, Inc., and may be used, copied and/or
7 // disclosed only pursuant to the terms of a valid license
8 // agreement with Xilinx, Inc. Xilinx hereby grants you a
9 // license to use this text/file solely for design, simulation,
10 // implementation and creation of design files limited
11 // to Xilinx devices or technologies. Use with non-Xilinx
12 // devices or technologies is expressly prohibited and
13 // immediately terminates your license unless covered by
14 // a separate agreement.
15 //
16 // Xilinx is providing this design, code, or information
17 // "as-is" solely for use in developing programs and
18 // solutions for Xilinx devices, with no obligation on the
19 // part of Xilinx to provide support. By providing this design,
20 // code, or information as one possible implementation of
21 // this feature, application or standard, Xilinx is making no
22 // representation that this implementation is free from any
23 // claims of infringement. You are responsible for
24 // obtaining any rights you may require for your implementation.
25 // Xilinx expressly disclaims any warranty whatsoever with
26 // respect to the adequacy of the implementation, including
27 // but not limited to any warranties or representations that this
28 // implementation is free from claims of infringement, implied
29 // warranties of merchantability or fitness for a particular
30 // purpose.
31 //
32 // Xilinx products are not intended for use in life support
33 // appliances, devices, or systems. Use in such applications is
34 // expressly prohibited.
35 //
36 // Any modifications that are made to the Source Code are
37 // done at the user�s sole risk and will be unsupported.
38 //
39 // Copyright (c) 2006-2007 Xilinx, Inc. All rights reserved.
40 //
41 // This copyright and support notice must be retained as part
42 // of this text at all times.
43 //*****************************************************************************
44 //   ____  ____
45 //  /   /\/   /
46 // /___/  \  /    Vendor: Xilinx
47 // \   \   \/     Version: 2.3
48 //  \   \         Application: MIG
49 //  /   /         Filename: ddr2_phy_init.v
50 // /___/   /\     Date Last Modified: $Date: 2008/07/22 15:41:06 $
51 // \   \  /  \    Date Created: Thu Aug 24 2006
52 //  \___\/\___\
53 //
54 //Device: Virtex-5
55 //Design Name: DDR2
56 //Purpose:
57 //Reference:
58 //   This module is the intialization control logic of the memory interface.
59 //   All commands are issued from here acoording to the burst, CAS Latency and
60 //   the user commands.
61 //Revision History:
62 //*****************************************************************************
63
64 `timescale 1ns/1ps
65
66 module ddr2_phy_init #
67   (
68    // Following parameters are for 72-bit RDIMM design (for ML561 Reference
69    // board design). Actual values may be different. Actual parameters values
70    // are passed from design top module ddr2_sdram module. Please refer to
71    // the ddr2_sdram module for actual values.
72    parameter BANK_WIDTH    = 2,
73    parameter CKE_WIDTH     = 1,
74    parameter COL_WIDTH     = 10,
75    parameter CS_NUM        = 1,
76    parameter DQ_WIDTH      = 72,
77    parameter ODT_WIDTH     = 1,
78    parameter ROW_WIDTH     = 14,
79    parameter ADDITIVE_LAT  = 0,
80    parameter BURST_LEN     = 4,
81    parameter TWO_T_TIME_EN = 0,
82    parameter BURST_TYPE    = 0,
83    parameter CAS_LAT       = 5,
84    parameter ODT_TYPE      = 1,
85    parameter REDUCE_DRV    = 0,
86    parameter REG_ENABLE    = 1,
87    parameter TWR           = 15000,
88    parameter CLK_PERIOD    = 3000,
89    parameter DDR_TYPE      = 1,
90    parameter SIM_ONLY      = 0
91    )
92   (
93    input                                   clk0,
94    input                                   clkdiv0,
95    input                                   rst0,
96    input                                   rstdiv0,
97    input [3:0]                             calib_done,
98    input                                   ctrl_ref_flag,
99    input                                   calib_ref_req,
100    output reg [3:0]                        calib_start,
101    output reg                              calib_ref_done,
102    output reg                              phy_init_wren,
103    output reg                              phy_init_rden,
104    output [ROW_WIDTH-1:0]                  phy_init_addr,
105    output [BANK_WIDTH-1:0]                 phy_init_ba,
106    output                                  phy_init_ras_n,
107    output                                  phy_init_cas_n,
108    output                                  phy_init_we_n,
109    output [CS_NUM-1:0]                     phy_init_cs_n,
110    output [CKE_WIDTH-1:0]                  phy_init_cke,
111    output reg                              phy_init_done,
112    output                                  phy_init_data_sel
113    );
114
115   // time to wait between consecutive commands in PHY_INIT - this is a
116   // generic number, and must be large enough to account for worst case
117   // timing parameter (tRFC - refresh-to-active) across all memory speed
118   // grades and operating frequencies. Expressed in CLKDIV clock cycles.
119   localparam  CNTNEXT_CMD = 7'b1111111;
120   // time to wait between read and read or precharge for stage 3 & 4
121   // the larger CNTNEXT_CMD can also be used, use smaller number to
122   // speed up calibration - avoid tRAS violation, and speeds up simulation
123   localparam  CNTNEXT_RD  = 4'b1111;
124
125   // Write recovery (WR) time - is defined by 
126   // tWR (in nanoseconds) by tCK (in nanoseconds) and rounding up a 
127   // noninteger value to the next integer
128   localparam integer WR_RECOVERY =  ((TWR + CLK_PERIOD) - 1)/CLK_PERIOD;
129
130   localparam  INIT_CAL1_READ            = 5'h00;
131   localparam  INIT_CAL2_READ            = 5'h01;
132   localparam  INIT_CAL3_READ            = 5'h02;
133   localparam  INIT_CAL4_READ            = 5'h03;
134   localparam  INIT_CAL1_WRITE           = 5'h04;
135   localparam  INIT_CAL2_WRITE           = 5'h05;
136   localparam  INIT_CAL3_WRITE           = 5'h06;
137   localparam  INIT_DUMMY_ACTIVE_WAIT    = 5'h07;
138   localparam  INIT_PRECHARGE            = 5'h08;
139   localparam  INIT_LOAD_MODE            = 5'h09;
140   localparam  INIT_AUTO_REFRESH         = 5'h0A;
141   localparam  INIT_IDLE                 = 5'h0B;
142   localparam  INIT_CNT_200              = 5'h0C;
143   localparam  INIT_CNT_200_WAIT         = 5'h0D;
144   localparam  INIT_PRECHARGE_WAIT       = 5'h0E;
145   localparam  INIT_MODE_REGISTER_WAIT   = 5'h0F;
146   localparam  INIT_AUTO_REFRESH_WAIT    = 5'h10;
147   localparam  INIT_DEEP_MEMORY_ST       = 5'h11;
148   localparam  INIT_DUMMY_ACTIVE         = 5'h12;
149   localparam  INIT_CAL1_WRITE_READ      = 5'h13;
150   localparam  INIT_CAL1_READ_WAIT       = 5'h14;
151   localparam  INIT_CAL2_WRITE_READ      = 5'h15;
152   localparam  INIT_CAL2_READ_WAIT       = 5'h16;
153   localparam  INIT_CAL3_WRITE_READ      = 5'h17;
154   localparam  INIT_CAL3_READ_WAIT       = 5'h18;
155   localparam  INIT_CAL4_READ_WAIT       = 5'h19;
156   localparam  INIT_CALIB_REF            = 5'h1A;
157   localparam  INIT_ZQCL                 = 5'h1B;
158   localparam  INIT_WAIT_DLLK_ZQINIT     = 5'h1C;
159
160   localparam  INIT_CNTR_INIT            = 4'h0;
161   localparam  INIT_CNTR_PRECH_1         = 4'h1;
162   localparam  INIT_CNTR_EMR2_INIT       = 4'h2;
163   localparam  INIT_CNTR_EMR3_INIT       = 4'h3;
164   localparam  INIT_CNTR_EMR_EN_DLL      = 4'h4;
165   localparam  INIT_CNTR_MR_RST_DLL      = 4'h5;
166   localparam  INIT_CNTR_CNT_200_WAIT    = 4'h6;
167   localparam  INIT_CNTR_PRECH_2         = 4'h7;
168   localparam  INIT_CNTR_AR_1            = 4'h8;
169   localparam  INIT_CNTR_AR_2            = 4'h9;
170   localparam  INIT_CNTR_MR_ACT_DLL      = 4'hA;
171   localparam  INIT_CNTR_EMR_DEF_OCD     = 4'hB;
172   localparam  INIT_CNTR_EMR_EXIT_OCD    = 4'hC;
173   localparam  INIT_CNTR_DEEP_MEM        = 4'hD;
174   localparam  INIT_CNTR_PRECH_3         = 4'hE;
175   localparam  INIT_CNTR_DONE            = 4'hF;
176
177   localparam   DDR1                     = 0;
178   localparam   DDR2                     = 1;
179   localparam   DDR3                     = 2;
180
181
182   reg [1:0]             burst_addr_r;
183   reg [1:0]             burst_cnt_r;
184   wire [1:0]            burst_val;
185   wire                  cal_read;
186   wire                  cal_write;
187   wire                  cal_write_read;
188   reg                   cal1_started_r;
189   reg                   cal2_started_r;
190   reg                   cal4_started_r;
191   reg [3:0]             calib_done_r;
192   reg                   calib_ref_req_posedge;
193   reg                   calib_ref_req_r;
194   reg [15:0]            calib_start_shift0_r;
195   reg [15:0]            calib_start_shift1_r;
196   reg [15:0]            calib_start_shift2_r;
197   reg [15:0]            calib_start_shift3_r;
198   reg [1:0]             chip_cnt_r;
199   reg [4:0]             cke_200us_cnt_r;
200   reg                   cke_200us_cnt_en_r;
201   reg [7:0]             cnt_200_cycle_r;
202   reg                   cnt_200_cycle_done_r;
203   reg [6:0]             cnt_cmd_r;
204   reg                   cnt_cmd_ok_r;
205   reg [3:0]             cnt_rd_r;
206   reg                   cnt_rd_ok_r;
207   reg                   ctrl_ref_flag_r;
208   reg                   done_200us_r;
209   reg [ROW_WIDTH-1:0]   ddr_addr_r;
210   reg [ROW_WIDTH-1:0]   ddr_addr_r1;
211   reg [BANK_WIDTH-1:0]  ddr_ba_r;
212   reg [BANK_WIDTH-1:0]  ddr_ba_r1;
213   reg                   ddr_cas_n_r;
214   reg                   ddr_cas_n_r1;
215   reg [CKE_WIDTH-1:0]   ddr_cke_r;
216   reg [CS_NUM-1:0]      ddr_cs_n_r;
217   reg [CS_NUM-1:0]      ddr_cs_n_r1;
218   reg [CS_NUM-1:0]      ddr_cs_disable_r;
219   reg                   ddr_ras_n_r;
220   reg                   ddr_ras_n_r1;
221   reg                   ddr_we_n_r;
222   reg                   ddr_we_n_r1;
223   wire [15:0]           ext_mode_reg;
224   reg [3:0]             init_cnt_r;
225   reg                   init_done_r;
226   reg [4:0]             init_next_state;
227   reg [4:0]             init_state_r;
228   reg [4:0]             init_state_r1;
229   reg [4:0]             init_state_r2;
230   wire [15:0]           load_mode_reg;
231   wire [15:0]           load_mode_reg0;
232   wire [15:0]           load_mode_reg1;
233   wire [15:0]           load_mode_reg2;
234   wire [15:0]           load_mode_reg3;
235   reg                   phy_init_done_r;
236   reg                   phy_init_done_r1;
237   reg                   phy_init_done_r2;
238   reg                   phy_init_done_r3;
239   reg                   refresh_req;
240   wire [3:0]            start_cal;
241
242   //***************************************************************************
243
244   //*****************************************************************
245   // DDR1 and DDR2 Load mode register
246   // Mode Register (MR):
247   //   [15:14] - unused          - 00
248   //   [13]    - reserved        - 0
249   //   [12]    - Power-down mode - 0 (normal)
250   //   [11:9]  - write recovery  - for Auto Precharge (tWR/tCK)
251   //   [8]     - DLL reset       - 0 or 1
252   //   [7]     - Test Mode       - 0 (normal)
253   //   [6:4]   - CAS latency     - CAS_LAT
254   //   [3]     - Burst Type      - BURST_TYPE
255   //   [2:0]   - Burst Length    - BURST_LEN
256   //*****************************************************************
257
258   generate
259     if (DDR_TYPE == DDR2) begin: gen_load_mode_reg_ddr2
260       assign load_mode_reg[2:0]   = (BURST_LEN == 8) ? 3'b011 :
261                                     ((BURST_LEN == 4) ? 3'b010 : 3'b111);
262       assign load_mode_reg[3]     = BURST_TYPE;
263       assign load_mode_reg[6:4]   = (CAS_LAT == 3) ? 3'b011 :
264                                     ((CAS_LAT == 4) ? 3'b100 :
265                                      ((CAS_LAT == 5) ? 3'b101 : 3'b111));
266       assign load_mode_reg[7]     = 1'b0;
267       assign load_mode_reg[8]     = 1'b0;    // init value only (DLL not reset)
268       assign load_mode_reg[11:9]  = (WR_RECOVERY == 6) ? 3'b101 :
269                                     ((WR_RECOVERY == 5) ? 3'b100 :
270                                      ((WR_RECOVERY == 4) ? 3'b011 :
271                                       ((WR_RECOVERY == 3) ? 3'b010 :
272                                       3'b001)));
273       assign load_mode_reg[15:12] = 4'b000;
274     end else if (DDR_TYPE == DDR1)begin: gen_load_mode_reg_ddr1
275       assign load_mode_reg[2:0]   = (BURST_LEN == 8) ? 3'b011 :
276                                     ((BURST_LEN == 4) ? 3'b010 :
277                                      ((BURST_LEN == 2) ? 3'b001 : 3'b111));
278       assign load_mode_reg[3]     = BURST_TYPE;
279       assign load_mode_reg[6:4]   = (CAS_LAT == 2) ? 3'b010 :
280                                     ((CAS_LAT == 3) ? 3'b011 :
281                                      ((CAS_LAT == 25) ? 3'b110 : 3'b111));
282       assign load_mode_reg[12:7]  = 6'b000000; // init value only
283       assign load_mode_reg[15:13]  = 3'b000;
284     end
285   endgenerate
286
287   //*****************************************************************
288   // DDR1 and DDR2 ext mode register
289   // Extended Mode Register (MR):
290   //   [15:14] - unused          - 00
291   //   [13]    - reserved        - 0
292   //   [12]    - output enable   - 0 (enabled)
293   //   [11]    - RDQS enable     - 0 (disabled)
294   //   [10]    - DQS# enable     - 0 (enabled)
295   //   [9:7]   - OCD Program     - 111 or 000 (first 111, then 000 during init)
296   //   [6]     - RTT[1]          - RTT[1:0] = 0(no ODT), 1(75), 2(150), 3(50)
297   //   [5:3]   - Additive CAS    - ADDITIVE_CAS
298   //   [2]     - RTT[0]
299   //   [1]     - Output drive    - REDUCE_DRV (= 0(full), = 1 (reduced)
300   //   [0]     - DLL enable      - 0 (normal)
301   //*****************************************************************
302
303   generate
304     if (DDR_TYPE == DDR2) begin: gen_ext_mode_reg_ddr2
305       assign ext_mode_reg[0]     = 1'b0;
306       assign ext_mode_reg[1]     = REDUCE_DRV;
307       assign ext_mode_reg[2]     = ((ODT_TYPE == 1) || (ODT_TYPE == 3)) ?
308                                    1'b1 : 1'b0;
309       assign ext_mode_reg[5:3]   = (ADDITIVE_LAT == 0) ? 3'b000 :
310                                    ((ADDITIVE_LAT == 1) ? 3'b001 :
311                                     ((ADDITIVE_LAT == 2) ? 3'b010 :
312                                      ((ADDITIVE_LAT == 3) ? 3'b011 :
313                                       ((ADDITIVE_LAT == 4) ? 3'b100 :
314                                       3'b111))));
315       assign ext_mode_reg[6]     = ((ODT_TYPE == 2) || (ODT_TYPE == 3)) ?
316                                    1'b1 : 1'b0;
317       assign ext_mode_reg[9:7]   = 3'b000;
318       assign ext_mode_reg[10]    = 1'b0;
319       assign ext_mode_reg[15:10] = 6'b000000;
320     end else if (DDR_TYPE == DDR1)begin: gen_ext_mode_reg_ddr1
321       assign ext_mode_reg[0]     = 1'b0;
322       assign ext_mode_reg[1]     = REDUCE_DRV;
323       assign ext_mode_reg[12:2]  = 11'b00000000000;
324       assign ext_mode_reg[15:13] = 3'b000;
325     end
326   endgenerate
327
328   //*****************************************************************
329   // DDR3 Load mode reg0
330   // Mode Register (MR0):
331   //   [15:13] - unused          - 000
332   //   [12]    - Precharge Power-down DLL usage - 0 (DLL frozen, slow-exit),
333   //             1 (DLL maintained)
334   //   [11:9]  - write recovery for Auto Precharge (tWR/tCK = 6)
335   //   [8]     - DLL reset       - 0 or 1
336   //   [7]     - Test Mode       - 0 (normal)
337   //   [6:4],[2]   - CAS latency     - CAS_LAT
338   //   [3]     - Burst Type      - BURST_TYPE
339   //   [1:0]   - Burst Length    - BURST_LEN
340   //*****************************************************************
341
342   generate
343     if (DDR_TYPE == DDR3) begin: gen_load_mode_reg0_ddr3
344       assign load_mode_reg0[1:0]   = (BURST_LEN == 8) ? 2'b00 :
345                                      ((BURST_LEN == 4) ? 2'b10 : 2'b11);
346       // Part of CAS latency. This bit is '0' for all CAS latencies
347       assign load_mode_reg0[2]     = 1'b0;
348       assign load_mode_reg0[3]     = BURST_TYPE;
349       assign load_mode_reg0[6:4]   = (CAS_LAT == 5) ? 3'b001 :
350                                      (CAS_LAT == 6) ? 3'b010 : 3'b111;
351       assign load_mode_reg0[7]     = 1'b0;
352       // init value only (DLL reset)
353       assign load_mode_reg0[8]     = 1'b1;
354       assign load_mode_reg0[11:9]  = 3'b010;
355       // Precharge Power-Down DLL 'slow-exit'
356       assign load_mode_reg0[12]    = 1'b0;
357       assign load_mode_reg0[15:13] = 3'b000;
358     end
359   endgenerate
360
361   //*****************************************************************
362   // DDR3 Load mode reg1
363   // Mode Register (MR1):
364   //   [15:13] - unused          - 00
365   //   [12]    - output enable   - 0 (enabled for DQ, DQS, DQS#)
366   //   [11]    - TDQS enable     - 0 (TDQS disabled and DM enabled)
367   //   [10]    - reserved   - 0 (must be '0')
368   //   [9]     - RTT[2]     - 0
369   //   [8]     - reserved   - 0 (must be '0')
370   //   [7]     - write leveling - 0 (disabled), 1 (enabled)
371   //   [6]     - RTT[1]          - RTT[1:0] = 0(no ODT), 1(75), 2(150), 3(50)
372   //   [5]     - Output driver impedance[1] - 0 (RZQ/6 and RZQ/7)
373   //   [4:3]   - Additive CAS    - ADDITIVE_CAS
374   //   [2]     - RTT[0]
375   //   [1]     - Output driver impedance[0] - 0(RZQ/6), or 1 (RZQ/7)
376   //   [0]     - DLL enable      - 0 (normal)
377   //*****************************************************************
378
379   generate
380     if (DDR_TYPE == DDR3) begin: gen_ext_mode_reg1_ddr3
381       // DLL enabled during Imitialization
382       assign load_mode_reg1[0]     = 1'b0;
383       // RZQ/6
384       assign load_mode_reg1[1]     = REDUCE_DRV;
385       assign load_mode_reg1[2]     = ((ODT_TYPE == 1) || (ODT_TYPE == 3)) ?
386                                      1'b1 : 1'b0;
387       assign load_mode_reg1[4:3]   = (ADDITIVE_LAT == 0) ? 2'b00 :
388                                      ((ADDITIVE_LAT == 1) ? 2'b01 :
389                                       ((ADDITIVE_LAT == 2) ? 2'b10 :
390                                        3'b111));
391       // RZQ/6
392       assign load_mode_reg1[5]     = 1'b0;
393       assign load_mode_reg1[6]     = ((ODT_TYPE == 2) || (ODT_TYPE == 3)) ?
394                                    1'b1 : 1'b0;
395       // Make zero WRITE_LEVEL
396       assign load_mode_reg1[7]   = 0;
397       assign load_mode_reg1[8]   = 1'b0;
398       assign load_mode_reg1[9]   = 1'b0;
399       assign load_mode_reg1[10]    = 1'b0;
400       assign load_mode_reg1[15:11] = 5'b00000;
401     end
402   endgenerate
403
404   //*****************************************************************
405   // DDR3 Load mode reg2
406   // Mode Register (MR2):
407   //   [15:11] - unused     - 00
408   //   [10:9]  - RTT_WR     - 00 (Dynamic ODT off)
409   //   [8]     - reserved   - 0 (must be '0')
410   //   [7]     - self-refresh temperature range -
411   //               0 (normal), 1 (extended)
412   //   [6]     - Auto Self-Refresh - 0 (manual), 1(auto)
413   //   [5:3]   - CAS Write Latency (CWL) -
414   //               000 (5 for 400 MHz device),
415   //               001 (6 for 400 MHz to 533 MHz devices),
416   //               010 (7 for 533 MHz to 667 MHz devices),
417   //               011 (8 for 667 MHz to 800 MHz)
418   //   [2:0]   - Partial Array Self-Refresh (Optional)      -
419   //               000 (full array)
420   //*****************************************************************
421
422   generate
423     if (DDR_TYPE == DDR3) begin: gen_ext_mode_reg2_ddr3
424       assign load_mode_reg2[2:0]     = 3'b000;
425       assign load_mode_reg2[5:3]   = (CAS_LAT == 5) ? 3'b000 :
426                                      (CAS_LAT == 6) ? 3'b001 : 3'b111;
427       assign load_mode_reg2[6]     = 1'b0; // Manual Self-Refresh
428       assign load_mode_reg2[7]   = 1'b0;
429       assign load_mode_reg2[8]   = 1'b0;
430       assign load_mode_reg2[10:9]  = 2'b00;
431       assign load_mode_reg2[15:11] = 5'b00000;
432     end
433   endgenerate
434
435   //*****************************************************************
436   // DDR3 Load mode reg3
437   // Mode Register (MR3):
438   //   [15:3] - unused          - All zeros
439   //   [2]     - MPR Operation - 0(normal operation), 1(data flow from MPR)
440   //   [1:0]   - MPR location     - 00 (Predefined pattern)
441   //*****************************************************************
442
443   generate
444     if (DDR_TYPE == DDR3)begin: gen_ext_mode_reg3_ddr3
445       assign load_mode_reg3[1:0]   = 2'b00;
446       assign load_mode_reg3[2]     = 1'b0;
447       assign load_mode_reg3[15:3] = 13'b0000000000000;
448     end
449   endgenerate
450
451   //***************************************************************************
452   // Logic for calibration start, and for auto-refresh during cal request
453   // CALIB_REF_REQ is used by calibration logic to request auto-refresh
454   // durign calibration (used to avoid tRAS violation is certain calibration
455   // stages take a long time). Once the auto-refresh is complete and cal can
456   // be resumed, CALIB_REF_DONE is asserted by PHY_INIT.
457   //***************************************************************************
458
459   // generate pulse for each of calibration start controls
460   assign start_cal[0] = ((init_state_r1 == INIT_CAL1_READ) &&
461                          (init_state_r2 != INIT_CAL1_READ));
462   assign start_cal[1] = ((init_state_r1 == INIT_CAL2_READ) &&
463                          (init_state_r2 != INIT_CAL2_READ));
464   assign start_cal[2] = ((init_state_r1 == INIT_CAL3_READ) &&
465                          (init_state_r2 == INIT_CAL3_WRITE_READ));
466   assign start_cal[3] = ((init_state_r1 == INIT_CAL4_READ) &&
467                          (init_state_r2 == INIT_DUMMY_ACTIVE_WAIT));
468
469   // Generate positive-edge triggered, latched signal to force initialization
470   // to pause calibration, and to issue auto-refresh. Clear flag as soon as
471   // refresh initiated
472   always @(posedge clkdiv0)
473     if (rstdiv0) begin
474       calib_ref_req_r       <= 1'b0;
475       calib_ref_req_posedge <= 1'b0;
476       refresh_req           <= 1'b0;
477     end else begin
478       calib_ref_req_r       <= calib_ref_req;
479       calib_ref_req_posedge <= calib_ref_req & ~calib_ref_req_r;
480       if (init_state_r1 == INIT_AUTO_REFRESH)
481         refresh_req <= 1'b0;
482       else if (calib_ref_req_posedge)
483         refresh_req <= 1'b1;
484     end
485
486   // flag to tell cal1 calibration was started.
487   // This flag is used for cal1 auto refreshes
488   // some of these bits may not be needed - only needed for those stages that
489   // need refreshes within the stage (i.e. very long stages)
490   always @(posedge clkdiv0)
491     if (rstdiv0) begin
492       cal1_started_r <= 1'b0;
493       cal2_started_r <= 1'b0;
494       cal4_started_r <= 1'b0;
495     end else begin
496       if (calib_start[0])
497         cal1_started_r <= 1'b1;
498       if (calib_start[1])
499         cal2_started_r <= 1'b1;
500       if (calib_start[3])
501         cal4_started_r <= 1'b1;
502     end
503
504   // Delay start of each calibration by 16 clock cycles to
505   // ensure that when calibration logic begins, that read data is already
506   // appearing on the bus. Don't really need it, it's more for simulation
507   // purposes. Each circuit should synthesize using an SRL16.
508   // In first stage of calibration  periodic auto refreshes
509   // will be issued to meet memory timing. calib_start_shift0_r[15] will be
510   // asserted more than once.calib_start[0] is anded with cal1_started_r so
511   // that it is asserted only once. cal1_refresh_done is anded with
512   // cal1_started_r so that it is asserted after the auto refreshes.
513   always @(posedge clkdiv0) begin
514     calib_start_shift0_r <= {calib_start_shift0_r[14:0], start_cal[0]};
515     calib_start_shift1_r <= {calib_start_shift1_r[14:0], start_cal[1]};
516     calib_start_shift2_r <= {calib_start_shift2_r[14:0], start_cal[2]};
517     calib_start_shift3_r <= {calib_start_shift3_r[14:0], start_cal[3]};
518     calib_start[0]       <= calib_start_shift0_r[15] & ~cal1_started_r;
519     calib_start[1]       <= calib_start_shift1_r[15] & ~cal2_started_r;
520     calib_start[2]       <= calib_start_shift2_r[15];
521     calib_start[3]       <= calib_start_shift3_r[15] & ~cal4_started_r;
522     calib_ref_done       <= calib_start_shift0_r[15] |
523                             calib_start_shift1_r[15] |
524                             calib_start_shift3_r[15];
525   end
526
527   // generate delay for various states that require it (no maximum delay
528   // requirement, make sure that terminal count is large enough to cover
529   // all cases)
530   always @(posedge clkdiv0) begin
531     case (init_state_r)
532       INIT_PRECHARGE_WAIT,
533       INIT_MODE_REGISTER_WAIT,
534       INIT_AUTO_REFRESH_WAIT,
535       INIT_DUMMY_ACTIVE_WAIT,
536       INIT_CAL1_WRITE_READ,
537       INIT_CAL1_READ_WAIT,
538       INIT_CAL2_WRITE_READ,
539       INIT_CAL2_READ_WAIT,
540       INIT_CAL3_WRITE_READ:
541         cnt_cmd_r <= cnt_cmd_r + 1;
542       default:
543         cnt_cmd_r <= 7'b0000000;
544     endcase
545   end
546
547   // assert when count reaches the value
548   always @(posedge clkdiv0) begin
549     if(cnt_cmd_r == CNTNEXT_CMD)
550       cnt_cmd_ok_r <= 1'b1;
551     else
552       cnt_cmd_ok_r <= 1'b0;
553   end
554
555   always @(posedge clkdiv0) begin
556     case (init_state_r)
557       INIT_CAL3_READ_WAIT,
558       INIT_CAL4_READ_WAIT:
559         cnt_rd_r <= cnt_rd_r + 1;
560       default:
561         cnt_rd_r <= 4'b0000;
562     endcase
563   end
564
565   always @(posedge clkdiv0) begin
566     if(cnt_rd_r == CNTNEXT_RD)
567       cnt_rd_ok_r <= 1'b1;
568     else
569       cnt_rd_ok_r <= 1'b0;
570   end
571
572   //***************************************************************************
573   // Initial delay after power-on
574   //***************************************************************************
575
576   // register the refresh flag from the controller.
577   // The refresh flag is in full frequency domain - so a pulsed version must
578   // be generated for half freq domain using 2 consecutive full clk cycles
579   // The registered version is used for the 200us counter
580   always @(posedge clk0)
581     ctrl_ref_flag_r <= ctrl_ref_flag;
582   always @(posedge clkdiv0)
583     cke_200us_cnt_en_r <= ctrl_ref_flag || ctrl_ref_flag_r;
584
585   // 200us counter for cke
586   always @(posedge clkdiv0)
587     if (rstdiv0) begin
588       // skip power-up count if only simulating
589       if (SIM_ONLY)
590         cke_200us_cnt_r <= 5'b00001;
591       else
592         cke_200us_cnt_r <= 5'd27;
593     end else if (cke_200us_cnt_en_r)
594       cke_200us_cnt_r <= cke_200us_cnt_r - 1;
595
596   always @(posedge clkdiv0)
597     if (rstdiv0)
598       done_200us_r <= 1'b0;
599     else if (!done_200us_r)
600       done_200us_r <= (cke_200us_cnt_r == 5'b00000);
601
602   // 200 clocks counter - count value : h'64 required for initialization
603   // Counts 100 divided by two clocks
604   always @(posedge clkdiv0)
605     if (rstdiv0 || (init_state_r == INIT_CNT_200))
606       cnt_200_cycle_r <= 8'h64;
607     else if  (init_state_r == INIT_ZQCL) // ddr3
608       cnt_200_cycle_r <= 8'hC8;
609     else if (cnt_200_cycle_r != 8'h00)
610       cnt_200_cycle_r <= cnt_200_cycle_r - 1;
611
612   always @(posedge clkdiv0)
613     if (rstdiv0 || (init_state_r == INIT_CNT_200)
614         || (init_state_r == INIT_ZQCL))
615       cnt_200_cycle_done_r <= 1'b0;
616     else if (cnt_200_cycle_r == 8'h00)
617       cnt_200_cycle_done_r <= 1'b1;
618
619   //*****************************************************************
620   //   handle deep memory configuration:
621   //   During initialization: Repeat initialization sequence once for each
622   //   chip select. Note that we could perform initalization for all chip
623   //   selects simulataneously. Probably fine - any potential SI issues with
624   //   auto refreshing all chip selects at once?
625   //   Once initialization complete, assert only CS[0] for calibration.
626   //*****************************************************************
627
628   always @(posedge clkdiv0)
629     if (rstdiv0) begin
630       chip_cnt_r <= 2'b00;
631     end else if (init_state_r == INIT_DEEP_MEMORY_ST) begin
632       if (chip_cnt_r != CS_NUM)
633         chip_cnt_r <= chip_cnt_r + 1;
634       else
635         chip_cnt_r <= 2'b00;
636     end
637
638   always @(posedge clkdiv0)
639     if (rstdiv0) begin
640       ddr_cs_n_r <= {CS_NUM{1'b1}};
641     end else begin
642       ddr_cs_n_r <= {CS_NUM{1'b1}};
643       if ((init_state_r == INIT_DUMMY_ACTIVE) ||
644           (init_state_r == INIT_PRECHARGE) ||
645           (init_state_r == INIT_LOAD_MODE) ||
646           (init_state_r == INIT_AUTO_REFRESH) ||
647           (init_state_r  == INIT_ZQCL    ) ||
648           (((init_state_r == INIT_CAL1_READ) ||
649             (init_state_r == INIT_CAL2_READ) ||
650             (init_state_r == INIT_CAL3_READ) ||
651             (init_state_r == INIT_CAL4_READ) ||
652             (init_state_r == INIT_CAL1_WRITE) ||
653             (init_state_r == INIT_CAL2_WRITE) ||
654             (init_state_r == INIT_CAL3_WRITE)) && (burst_cnt_r == 2'b00)))
655         ddr_cs_n_r[chip_cnt_r] <= 1'b0;
656       else
657         ddr_cs_n_r[chip_cnt_r] <= 1'b1;
658     end
659
660   //***************************************************************************
661   // Write/read burst logic
662   //***************************************************************************
663
664   assign cal_write = ((init_state_r == INIT_CAL1_WRITE) ||
665                       (init_state_r == INIT_CAL2_WRITE) ||
666                       (init_state_r == INIT_CAL3_WRITE));
667   assign cal_read = ((init_state_r == INIT_CAL1_READ) ||
668                      (init_state_r == INIT_CAL2_READ) ||
669                      (init_state_r == INIT_CAL3_READ) ||
670                      (init_state_r == INIT_CAL4_READ));
671   assign cal_write_read = ((init_state_r == INIT_CAL1_READ) ||
672                            (init_state_r == INIT_CAL2_READ) ||
673                            (init_state_r == INIT_CAL3_READ) ||
674                            (init_state_r == INIT_CAL4_READ) ||
675                            (init_state_r == INIT_CAL1_WRITE) ||
676                            (init_state_r == INIT_CAL2_WRITE) ||
677                            (init_state_r == INIT_CAL3_WRITE));
678
679   assign burst_val = (BURST_LEN == 4) ? 2'b00 :
680                      (BURST_LEN == 8) ? 2'b01 : 2'b00;
681
682   // keep track of current address - need this if burst length < 8 for
683   // stage 2-4 calibration writes and reads. Make sure value always gets
684   // initialized to 0 before we enter write/read state. This is used to
685   // keep track of when another burst must be issued
686   always @(posedge clkdiv0)
687     if (cal_write_read)
688       burst_addr_r <= burst_addr_r + 2;
689     else
690       burst_addr_r <= 2'b00;
691
692   // write/read burst count
693   always @(posedge clkdiv0)
694     if (cal_write_read)
695       if (burst_cnt_r == 2'b00)
696         burst_cnt_r <= burst_val;
697       else // SHOULD THIS BE -2 CHECK THIS LOGIC
698         burst_cnt_r <= burst_cnt_r - 1;
699     else
700       burst_cnt_r <= 2'b00;
701
702   // indicate when a write is occurring
703   always @(posedge clkdiv0)
704     // MIG 2.1: Remove (burst_addr_r<4) term - not used
705     // phy_init_wren <= cal_write && (burst_addr_r < 3'd4);
706     phy_init_wren <= cal_write;
707
708   // used for read enable calibration, pulse to indicate when read issued
709   always @(posedge clkdiv0)
710     // MIG 2.1: Remove (burst_addr_r<4) term - not used
711     // phy_init_rden <= cal_read && (burst_addr_r < 3'd4);
712     phy_init_rden <= cal_read;
713
714   //***************************************************************************
715   // Initialization state machine
716   //***************************************************************************
717
718   always @(posedge clkdiv0)
719     // every time we need to initialize another rank of memory, need to
720     // reset init count, and repeat the entire initialization (but not
721     // calibration) sequence
722     if (rstdiv0 || (init_state_r == INIT_DEEP_MEMORY_ST))
723       init_cnt_r <= INIT_CNTR_INIT;
724     else if ((DDR_TYPE == DDR1) && (init_state_r == INIT_PRECHARGE) &&
725              (init_cnt_r == INIT_CNTR_PRECH_1))
726       // skip EMR(2) and EMR(3) register loads
727       init_cnt_r <= INIT_CNTR_EMR_EN_DLL;
728     else if ((DDR_TYPE == DDR1) && (init_state_r == INIT_LOAD_MODE) &&
729              (init_cnt_r == INIT_CNTR_MR_ACT_DLL))
730       // skip OCD calibration for DDR1
731       init_cnt_r <= INIT_CNTR_DEEP_MEM;
732     else if ((DDR_TYPE == DDR3) && (init_state_r ==  INIT_ZQCL))
733       // skip states for DDR3
734       init_cnt_r <= INIT_CNTR_DEEP_MEM;
735     else if ((init_state_r == INIT_LOAD_MODE) ||
736              ((init_state_r == INIT_PRECHARGE)
737               && (init_state_r1 != INIT_CALIB_REF))||
738              ((init_state_r == INIT_AUTO_REFRESH)
739               && (~init_done_r))||
740              (init_state_r == INIT_CNT_200))
741       init_cnt_r <= init_cnt_r + 1;
742
743   always @(posedge clkdiv0) begin
744     if ((init_state_r == INIT_IDLE) && (init_cnt_r == INIT_CNTR_DONE)) begin
745       phy_init_done_r <= 1'b1;
746     end else
747       phy_init_done_r <= 1'b0;
748   end
749
750   // phy_init_done to the controller and the user interface.
751   // It is delayed by four clocks to account for the
752   // multi cycle path constraint to the (phy_init_data_sel)
753   // to the phy layer.
754   always @(posedge clkdiv0)begin
755         if (rstdiv0) begin
756                 phy_init_done_r1 <= 1'b0;
757             phy_init_done_r2 <= 1'b0;
758             phy_init_done_r3 <= 1'b0;
759             phy_init_done <= 1'b0;
760         end else begin
761             phy_init_done_r1 <= phy_init_done_r;
762             phy_init_done_r2 <= phy_init_done_r1;
763             phy_init_done_r3 <= phy_init_done_r2;
764             phy_init_done <= phy_init_done_r3;
765         end
766   end
767
768   // Instantiate primitive to allow this flop to be attached to multicycle
769   // path constraint in UCF. This signal goes to PHY_WRITE and PHY_CTL_IO
770   // datapath logic only. Because it is a multi-cycle path, it can be
771   // clocked by either CLKDIV0 or CLK0.
772   FDRSE u_ff_phy_init_data_sel
773     (
774      .Q   (phy_init_data_sel),
775      .C   (clkdiv0),
776      .CE  (1'b1),
777      .D   (phy_init_done_r1),
778      .R   (1'b0),
779      .S   (1'b0)
780      ) /* synthesis syn_preserve=1 */
781        /* synthesis syn_replicate = 0 */;
782
783   //synthesis translate_off
784   always @(posedge calib_done[0])
785       $display ("First Stage Calibration completed at time %t", $time);
786
787   always @(posedge calib_done[1])
788     $display ("Second Stage Calibration completed at time %t", $time);
789
790   always @(posedge calib_done[2]) begin
791     $display ("Third Stage Calibration completed at time %t", $time);
792   end
793
794   always @(posedge calib_done[3]) begin
795     $display ("Fourth Stage Calibration completed at time %t", $time);
796     $display ("Calibration completed at time %t", $time);
797   end
798   //synthesis translate_on
799
800   always @(posedge clkdiv0) begin
801     if ((init_cnt_r >= INIT_CNTR_DEEP_MEM))begin
802        init_done_r <= 1'b1;
803     end else
804        init_done_r <= 1'b0;
805   end
806
807   //*****************************************************************
808
809   always @(posedge clkdiv0)
810     if (rstdiv0) begin
811       init_state_r  <= INIT_IDLE;
812       init_state_r1 <= INIT_IDLE;
813       init_state_r2 <= INIT_IDLE;
814       calib_done_r  <= 4'b0000;
815     end else begin
816       init_state_r  <= init_next_state;
817       init_state_r1 <= init_state_r;
818       init_state_r2 <= init_state_r1;
819       calib_done_r  <= calib_done; // register for timing
820     end
821
822   always @(*) begin
823     init_next_state = init_state_r;
824     (* full_case, parallel_case *) case (init_state_r)
825       INIT_IDLE: begin
826         if (done_200us_r) begin
827           (* parallel_case *) case (init_cnt_r)
828             INIT_CNTR_INIT:
829               init_next_state = INIT_CNT_200;
830             INIT_CNTR_PRECH_1:
831               init_next_state = INIT_PRECHARGE;
832             INIT_CNTR_EMR2_INIT:
833               init_next_state = INIT_LOAD_MODE; // EMR(2)
834             INIT_CNTR_EMR3_INIT:
835               init_next_state = INIT_LOAD_MODE; // EMR(3);
836             INIT_CNTR_EMR_EN_DLL:
837               init_next_state = INIT_LOAD_MODE; // EMR, enable DLL
838             INIT_CNTR_MR_RST_DLL:
839               init_next_state = INIT_LOAD_MODE; // MR, reset DLL
840             INIT_CNTR_CNT_200_WAIT:begin
841               if(DDR_TYPE == DDR3)
842                  init_next_state = INIT_ZQCL; // DDR3
843               else
844                 // Wait 200cc after reset DLL
845                 init_next_state = INIT_CNT_200;
846             end
847             INIT_CNTR_PRECH_2:
848               init_next_state = INIT_PRECHARGE;
849             INIT_CNTR_AR_1:
850               init_next_state = INIT_AUTO_REFRESH;
851             INIT_CNTR_AR_2:
852               init_next_state = INIT_AUTO_REFRESH;
853             INIT_CNTR_MR_ACT_DLL:
854               init_next_state = INIT_LOAD_MODE; // MR, unreset DLL
855             INIT_CNTR_EMR_DEF_OCD:
856               init_next_state = INIT_LOAD_MODE; // EMR, OCD default
857             INIT_CNTR_EMR_EXIT_OCD:
858               init_next_state = INIT_LOAD_MODE; // EMR, enable OCD exit
859             INIT_CNTR_DEEP_MEM: begin
860                if ((chip_cnt_r < CS_NUM-1))
861                   init_next_state = INIT_DEEP_MEMORY_ST;
862               else if (cnt_200_cycle_done_r)
863                 init_next_state = INIT_DUMMY_ACTIVE;
864               else
865                 init_next_state = INIT_IDLE;
866             end
867             INIT_CNTR_PRECH_3:
868               init_next_state = INIT_PRECHARGE;
869             INIT_CNTR_DONE:
870               init_next_state = INIT_IDLE;
871             default :
872               init_next_state = INIT_IDLE;
873           endcase
874         end
875       end
876       INIT_CNT_200:
877         init_next_state = INIT_CNT_200_WAIT;
878       INIT_CNT_200_WAIT:
879         if (cnt_200_cycle_done_r)
880           init_next_state = INIT_IDLE;
881       INIT_PRECHARGE:
882         init_next_state = INIT_PRECHARGE_WAIT;
883       INIT_PRECHARGE_WAIT:
884         if (cnt_cmd_ok_r)begin
885           if (init_done_r && (!(&calib_done_r)))
886             init_next_state = INIT_AUTO_REFRESH;
887           else
888             init_next_state = INIT_IDLE;
889         end
890       INIT_ZQCL:
891         init_next_state = INIT_WAIT_DLLK_ZQINIT;
892       INIT_WAIT_DLLK_ZQINIT:
893         if (cnt_200_cycle_done_r)
894           init_next_state = INIT_IDLE;
895       INIT_LOAD_MODE:
896         init_next_state = INIT_MODE_REGISTER_WAIT;
897       INIT_MODE_REGISTER_WAIT:
898         if (cnt_cmd_ok_r)
899           init_next_state = INIT_IDLE;
900       INIT_AUTO_REFRESH:
901         init_next_state = INIT_AUTO_REFRESH_WAIT;
902       INIT_AUTO_REFRESH_WAIT:
903         if (cnt_cmd_ok_r)begin
904           if(init_done_r)
905             init_next_state = INIT_DUMMY_ACTIVE;
906           else
907             init_next_state = INIT_IDLE;
908         end
909       INIT_DEEP_MEMORY_ST:
910         init_next_state = INIT_IDLE;
911       // single row activate. All subsequent calibration writes and
912       // read will take place in this row
913       INIT_DUMMY_ACTIVE:
914         init_next_state = INIT_DUMMY_ACTIVE_WAIT;
915       INIT_DUMMY_ACTIVE_WAIT:
916         if (cnt_cmd_ok_r)begin
917           if (~calib_done_r[0]) begin
918             // if returning to stg1 after refresh, don't need to write
919             if (cal1_started_r)
920               init_next_state = INIT_CAL1_READ;
921             // if first entering stg1, need to write training pattern
922             else
923               init_next_state = INIT_CAL1_WRITE;
924           end else if (~calib_done[1]) begin
925             if (cal2_started_r)
926               init_next_state = INIT_CAL2_READ;
927             else
928               init_next_state = INIT_CAL2_WRITE;
929           end else if (~calib_done_r[2])
930              init_next_state = INIT_CAL3_WRITE;
931           else
932             init_next_state = INIT_CAL4_READ;
933         end
934       // Stage 1 calibration (write and continuous read)
935       INIT_CAL1_WRITE:
936         if (burst_addr_r == 2'b10)
937           init_next_state = INIT_CAL1_WRITE_READ;
938       INIT_CAL1_WRITE_READ:
939         if (cnt_cmd_ok_r)
940           init_next_state = INIT_CAL1_READ;
941       INIT_CAL1_READ:
942         // Stage 1 requires inter-stage auto-refresh
943         if (calib_done_r[0] || refresh_req)
944           init_next_state = INIT_CAL1_READ_WAIT;
945       INIT_CAL1_READ_WAIT:
946         if (cnt_cmd_ok_r)
947           init_next_state = INIT_CALIB_REF;
948       // Stage 2 calibration (write and continuous read)
949       INIT_CAL2_WRITE:
950         if (burst_addr_r == 2'b10)
951           init_next_state = INIT_CAL2_WRITE_READ;
952       INIT_CAL2_WRITE_READ:
953         if (cnt_cmd_ok_r)
954           init_next_state = INIT_CAL2_READ;
955       INIT_CAL2_READ:
956         // Stage 2 requires inter-stage auto-refresh
957         if (calib_done_r[1] || refresh_req)
958           init_next_state = INIT_CAL2_READ_WAIT;
959       INIT_CAL2_READ_WAIT:
960         if(cnt_cmd_ok_r)
961           init_next_state = INIT_CALIB_REF;
962       // Stage 3 calibration (write and continuous read)
963       INIT_CAL3_WRITE:
964         if (burst_addr_r == 2'b10)
965           init_next_state = INIT_CAL3_WRITE_READ;
966       INIT_CAL3_WRITE_READ:
967         if (cnt_cmd_ok_r)
968           init_next_state = INIT_CAL3_READ;
969       INIT_CAL3_READ:
970         if (burst_addr_r == 2'b10)
971           init_next_state = INIT_CAL3_READ_WAIT;
972       INIT_CAL3_READ_WAIT: begin
973         if (cnt_rd_ok_r)
974           if (calib_done_r[2]) begin
975             init_next_state = INIT_CALIB_REF;
976           end else
977             init_next_state = INIT_CAL3_READ;
978       end
979       // Stage 4 calibration (continuous read only, same pattern as stage 3)
980       // only used if DQS_GATE supported
981       INIT_CAL4_READ:
982         if (burst_addr_r == 2'b10)
983           init_next_state = INIT_CAL4_READ_WAIT;
984       INIT_CAL4_READ_WAIT: begin
985         if (cnt_rd_ok_r)
986           // Stage 4 requires inter-stage auto-refresh
987           if (calib_done_r[3] || refresh_req)
988             init_next_state = INIT_PRECHARGE;
989           else
990             init_next_state = INIT_CAL4_READ;
991       end
992       INIT_CALIB_REF:
993         init_next_state = INIT_PRECHARGE;
994     endcase
995   end
996
997   //***************************************************************************
998   // Memory control/address
999   //***************************************************************************
1000
1001   always @(posedge clkdiv0)
1002     if ((init_state_r == INIT_DUMMY_ACTIVE) ||
1003         (init_state_r == INIT_PRECHARGE) ||
1004         (init_state_r == INIT_LOAD_MODE) ||
1005         (init_state_r == INIT_AUTO_REFRESH)) begin
1006       ddr_ras_n_r <= 1'b0;
1007     end else begin
1008       ddr_ras_n_r <= 1'b1;
1009     end
1010
1011   always @(posedge clkdiv0)
1012     if ((init_state_r == INIT_LOAD_MODE) ||
1013         (init_state_r == INIT_AUTO_REFRESH) ||
1014         (cal_write_read && (burst_cnt_r == 2'b00))) begin
1015       ddr_cas_n_r <= 1'b0;
1016     end else begin
1017       ddr_cas_n_r <= 1'b1;
1018     end
1019
1020   always @(posedge clkdiv0)
1021     if ((init_state_r == INIT_LOAD_MODE) ||
1022         (init_state_r == INIT_PRECHARGE) ||
1023         (init_state_r == INIT_ZQCL) ||
1024         (cal_write && (burst_cnt_r == 2'b00)))begin
1025       ddr_we_n_r <= 1'b0;
1026     end else begin
1027       ddr_we_n_r <= 1'b1;
1028     end
1029
1030   //*****************************************************************
1031   // memory address during init
1032   //*****************************************************************
1033
1034   always @(posedge clkdiv0) begin
1035     if ((init_state_r == INIT_PRECHARGE)
1036         || (init_state_r == INIT_ZQCL))begin
1037       // Precharge all - set A10 = 1
1038       ddr_addr_r <= {ROW_WIDTH{1'b0}};
1039       ddr_addr_r[10] <= 1'b1;
1040       ddr_ba_r <= {BANK_WIDTH{1'b0}};
1041     end else if (init_state_r == INIT_LOAD_MODE) begin
1042       ddr_ba_r <= {BANK_WIDTH{1'b0}};
1043       ddr_addr_r <= {ROW_WIDTH{1'b0}};
1044       case (init_cnt_r)
1045         // EMR (2)
1046         INIT_CNTR_EMR2_INIT: begin
1047           ddr_ba_r[1:0] <= 2'b10;
1048           ddr_addr_r    <= {ROW_WIDTH{1'b0}};
1049         end
1050         // EMR (3)
1051         INIT_CNTR_EMR3_INIT: begin
1052           ddr_ba_r[1:0] <= 2'b11;
1053           if(DDR_TYPE == DDR3)
1054             ddr_addr_r    <= load_mode_reg3[ROW_WIDTH-1:0];
1055           else
1056             ddr_addr_r    <= {ROW_WIDTH{1'b0}};
1057         end
1058         // EMR write - A0 = 0 for DLL enable
1059         INIT_CNTR_EMR_EN_DLL: begin
1060           ddr_ba_r[1:0] <= 2'b01;
1061           if(DDR_TYPE == DDR3)
1062             ddr_addr_r <= load_mode_reg1[ROW_WIDTH-1:0];
1063           else
1064             ddr_addr_r <= ext_mode_reg[ROW_WIDTH-1:0];
1065         end
1066         // MR write, reset DLL (A8=1)
1067         INIT_CNTR_MR_RST_DLL: begin
1068           if(DDR_TYPE == DDR3)
1069             ddr_addr_r <= load_mode_reg0[ROW_WIDTH-1:0];
1070           else
1071             ddr_addr_r <= load_mode_reg[ROW_WIDTH-1:0];
1072           ddr_ba_r[1:0] <= 2'b00;
1073           ddr_addr_r[8] <= 1'b1;
1074         end
1075         // MR write, unreset DLL (A8=0)
1076         INIT_CNTR_MR_ACT_DLL: begin
1077           ddr_ba_r[1:0] <= 2'b00;
1078           ddr_addr_r <= load_mode_reg[ROW_WIDTH-1:0];
1079         end
1080         // EMR write, OCD default state
1081         INIT_CNTR_EMR_DEF_OCD: begin
1082           ddr_ba_r[1:0] <= 2'b01;
1083           ddr_addr_r <= ext_mode_reg[ROW_WIDTH-1:0];
1084           ddr_addr_r[9:7] <= 3'b111;
1085         end
1086         // EMR write - OCD exit
1087         INIT_CNTR_EMR_EXIT_OCD: begin
1088           ddr_ba_r[1:0] <= 2'b01;
1089           ddr_addr_r <= ext_mode_reg[ROW_WIDTH-1:0];
1090         end
1091         default: begin
1092           ddr_ba_r <= {BANK_WIDTH{1'bx}};
1093           ddr_addr_r <= {ROW_WIDTH{1'bx}};
1094         end
1095       endcase
1096     end else if (cal_write_read) begin
1097       // when writing or reading for Stages 2-4, since training pattern is
1098       // either 4 (stage 2) or 8 (stage 3-4) long, if BURST LEN < 8, then
1099       // need to issue multiple bursts to read entire training pattern
1100       ddr_addr_r[ROW_WIDTH-1:3] <= {ROW_WIDTH-4{1'b0}};
1101       ddr_addr_r[2:0]           <= {burst_addr_r, 1'b0};
1102       ddr_ba_r                  <= {BANK_WIDTH-1{1'b0}};
1103     end else if (init_state_r == INIT_DUMMY_ACTIVE) begin
1104       // all calibration writing read takes place in row 0x0 only
1105       ddr_ba_r   <= {BANK_WIDTH{1'b0}};
1106       ddr_addr_r <= {ROW_WIDTH{1'b0}};
1107     end else begin
1108       // otherwise, cry me a river
1109       ddr_ba_r   <= {BANK_WIDTH{1'bx}};
1110       ddr_addr_r <= {ROW_WIDTH{1'bx}};
1111     end
1112   end
1113
1114   // Keep CKE asserted after initial power-on delay
1115   always @(posedge clkdiv0)
1116     ddr_cke_r <= {CKE_WIDTH{done_200us_r}};
1117
1118   // register commands to memory. Two clock cycle delay from state -> output
1119   always @(posedge clk0) begin
1120     ddr_addr_r1   <= ddr_addr_r;
1121     ddr_ba_r1     <= ddr_ba_r;
1122     ddr_cas_n_r1  <= ddr_cas_n_r;
1123     ddr_ras_n_r1  <= ddr_ras_n_r;
1124     ddr_we_n_r1   <= ddr_we_n_r;
1125     ddr_cs_n_r1   <= ddr_cs_n_r;
1126   end // always @ (posedge clk0)
1127
1128   // logic to toggle chip select. The chip_select is
1129   // clocked of clkdiv0 and will be asserted for
1130   // two clock cycles.
1131    always @(posedge clk0) begin
1132       if(rst0)
1133         ddr_cs_disable_r <= {CS_NUM{1'b0}};
1134       else begin
1135          if(ddr_cs_disable_r[chip_cnt_r])
1136             ddr_cs_disable_r[chip_cnt_r] <= 1'b0;
1137          else begin
1138             if(TWO_T_TIME_EN)
1139                ddr_cs_disable_r[chip_cnt_r] <= ~ddr_cs_n_r1[chip_cnt_r];
1140             else
1141                ddr_cs_disable_r[chip_cnt_r] <= ~ddr_cs_n_r[chip_cnt_r];
1142          end
1143        end
1144    end
1145
1146
1147   assign phy_init_addr      = ddr_addr_r;
1148   assign phy_init_ba        = ddr_ba_r;
1149   assign phy_init_cas_n     = ddr_cas_n_r;
1150   assign phy_init_cke       = ddr_cke_r;
1151   assign phy_init_ras_n     = ddr_ras_n_r;
1152   assign phy_init_we_n      = ddr_we_n_r;
1153   assign phy_init_cs_n      = (TWO_T_TIME_EN) ?
1154                               ddr_cs_n_r1 | ddr_cs_disable_r
1155                               : ddr_cs_n_r| ddr_cs_disable_r;
1156
1157 endmodule