1 |
3 |
dgisselq |
////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
//
|
3 |
|
|
// Filename: wbddrsdram.v
|
4 |
|
|
//
|
5 |
|
|
// Project: OpenArty, an entirely open SoC based upon the Arty platform
|
6 |
|
|
//
|
7 |
|
|
// Purpose:
|
8 |
|
|
//
|
9 |
|
|
// Creator: Dan Gisselquist, Ph.D.
|
10 |
|
|
// Gisselquist Technology, LLC
|
11 |
|
|
//
|
12 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
13 |
|
|
//
|
14 |
|
|
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
|
15 |
|
|
//
|
16 |
|
|
// This program is free software (firmware): you can redistribute it and/or
|
17 |
|
|
// modify it under the terms of the GNU General Public License as published
|
18 |
|
|
// by the Free Software Foundation, either version 3 of the License, or (at
|
19 |
|
|
// your option) any later version.
|
20 |
|
|
//
|
21 |
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
22 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
|
23 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
24 |
|
|
// for more details.
|
25 |
|
|
//
|
26 |
|
|
// You should have received a copy of the GNU General Public License along
|
27 |
|
|
// with this program. (It's in the $(ROOT)/doc directory, run make with no
|
28 |
|
|
// target there if the PDF file isn't present.) If not, see
|
29 |
|
|
// <http://www.gnu.org/licenses/> for a copy.
|
30 |
|
|
//
|
31 |
|
|
// License: GPL, v3, as defined and found on www.gnu.org,
|
32 |
|
|
// http://www.gnu.org/licenses/gpl.html
|
33 |
|
|
//
|
34 |
|
|
//
|
35 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
36 |
|
|
//
|
37 |
|
|
//
|
38 |
17 |
dgisselq |
|
39 |
|
|
// Possible commands to the DDR3 memory. These consist of settings for the
|
40 |
|
|
// bits: o_wb_cs_n, o_wb_ras_n, o_wb_cas_n, and o_wb_we_n, respectively.
|
41 |
|
|
`define DDR_MRSET 4'b0000
|
42 |
|
|
`define DDR_REFRESH 4'b0001
|
43 |
|
|
`define DDR_PRECHARGE 4'b0010
|
44 |
|
|
`define DDR_ACTIVATE 4'b0011
|
45 |
|
|
`define DDR_WRITE 4'b0100
|
46 |
|
|
`define DDR_READ 4'b0101
|
47 |
|
|
`define DDR_ZQS 4'b0110
|
48 |
|
|
`define DDR_NOOP 4'b0111
|
49 |
|
|
//`define DDR_DESELECT 4'b1???
|
50 |
|
|
//
|
51 |
|
|
// In this controller, 24-bit commands tend to be passed around. These
|
52 |
|
|
// 'commands' are bit fields. Here we specify the bits associated with
|
53 |
|
|
// the bit fields.
|
54 |
|
|
`define DDR_RSTDONE 24 // End the reset sequence?
|
55 |
|
|
`define DDR_RSTTIMER 23 // Does this reset command take multiple clocks?
|
56 |
|
|
`define DDR_RSTBIT 22 // Value to place on reset_n
|
57 |
|
|
`define DDR_CKEBIT 21 // Should this reset command set CKE?
|
58 |
|
|
//
|
59 |
|
|
// Refresh command bit fields
|
60 |
|
|
`define DDR_NEEDREFRESH 23
|
61 |
|
|
`define DDR_RFTIMER 22
|
62 |
|
|
`define DDR_RFBEGIN 21
|
63 |
|
|
//
|
64 |
|
|
`define DDR_CMDLEN 21
|
65 |
|
|
`define DDR_CSBIT 20
|
66 |
|
|
`define DDR_RASBIT 19
|
67 |
|
|
`define DDR_CASBIT 18
|
68 |
|
|
`define DDR_WEBIT 17
|
69 |
|
|
`define DDR_NOPTIMER 16 // Steal this from BA bits
|
70 |
|
|
`define DDR_BABITS 3 // BABITS are really from 18:16, they are 3 bits
|
71 |
|
|
`define DDR_ADDR_BITS 14
|
72 |
|
|
//
|
73 |
|
|
|
74 |
|
|
module wbddrsdram(i_clk, i_reset,
|
75 |
3 |
dgisselq |
i_wb_cyc, i_wb_stb, i_wb_we, i_wb_addr, i_wb_data,
|
76 |
17 |
dgisselq |
o_wb_ack, o_wb_stall, o_wb_data,
|
77 |
3 |
dgisselq |
o_ddr_reset_n, o_ddr_cke,
|
78 |
|
|
o_ddr_cs_n, o_ddr_ras_n, o_ddr_cas_n, o_ddr_we_n,
|
79 |
17 |
dgisselq |
o_ddr_dqs, o_ddr_dm, o_ddr_odt, o_ddr_bus_oe,
|
80 |
3 |
dgisselq |
o_ddr_addr, o_ddr_ba, o_ddr_data, i_ddr_data);
|
81 |
17 |
dgisselq |
parameter CKRBITS = 13, // Bits in CKREFI4
|
82 |
|
|
CKREFI = 13'd1560, // 4 * 7.8us at 200 MHz clock
|
83 |
|
|
CKRFC = 320,
|
84 |
|
|
CKWR = 3,
|
85 |
|
|
CKRP = 11, // (=tRTP)Time from precharge to open command
|
86 |
|
|
CKCAS = 11, // CAS Latency, tCL
|
87 |
|
|
CKXPR = CKRFC+5+2, // Clocks per tXPR timeout
|
88 |
|
|
BUSREG= 2+CKCAS,
|
89 |
|
|
BUSNOW= 3+CKCAS;
|
90 |
|
|
input i_clk, i_reset;
|
91 |
3 |
dgisselq |
// Wishbone inputs
|
92 |
|
|
input i_wb_cyc, i_wb_stb, i_wb_we;
|
93 |
|
|
input [25:0] i_wb_addr;
|
94 |
|
|
input [31:0] i_wb_data;
|
95 |
|
|
// Wishbone outputs
|
96 |
|
|
output reg o_wb_ack;
|
97 |
|
|
output reg o_wb_stall;
|
98 |
|
|
output reg [31:0] o_wb_data;
|
99 |
|
|
// DDR3 RAM Controller
|
100 |
17 |
dgisselq |
output reg o_ddr_reset_n, o_ddr_cke;
|
101 |
3 |
dgisselq |
// Control outputs
|
102 |
17 |
dgisselq |
output wire o_ddr_cs_n, o_ddr_ras_n, o_ddr_cas_n,o_ddr_we_n;
|
103 |
3 |
dgisselq |
// DQS outputs:set to 3'b010 when data is active, 3'b100 (i.e. 2'bzz) ow
|
104 |
17 |
dgisselq |
output wire o_ddr_dqs;
|
105 |
|
|
output reg o_ddr_dm;
|
106 |
|
|
output reg o_ddr_odt;
|
107 |
|
|
output wire o_ddr_bus_oe;
|
108 |
3 |
dgisselq |
// Address outputs
|
109 |
17 |
dgisselq |
output wire [13:0] o_ddr_addr;
|
110 |
|
|
output wire [2:0] o_ddr_ba;
|
111 |
3 |
dgisselq |
// And the data inputs and outputs
|
112 |
|
|
output reg [31:0] o_ddr_data;
|
113 |
17 |
dgisselq |
input [31:0] i_ddr_data;
|
114 |
3 |
dgisselq |
|
115 |
17 |
dgisselq |
reg drive_dqs;
|
116 |
|
|
|
117 |
|
|
// The pending transaction
|
118 |
|
|
reg [31:0] r_data;
|
119 |
|
|
reg r_pending, r_we;
|
120 |
|
|
reg [25:0] r_addr;
|
121 |
|
|
reg [13:0] r_row;
|
122 |
|
|
reg [2:0] r_bank;
|
123 |
|
|
reg [9:0] r_col;
|
124 |
|
|
reg [1:0] r_sub;
|
125 |
|
|
reg r_move; // It was accepted, and can move to next stage
|
126 |
|
|
|
127 |
|
|
// The pending transaction, one further into the pipeline. This is
|
128 |
|
|
// the stage where the read/write command is actually given to the
|
129 |
|
|
// interface if we haven't stalled.
|
130 |
|
|
reg [31:0] s_data;
|
131 |
|
|
reg s_pending, s_we; // , s_match;
|
132 |
|
|
reg [25:0] s_addr;
|
133 |
|
|
reg [13:0] s_row, s_nxt_row;
|
134 |
|
|
reg [2:0] s_bank, s_nxt_bank;
|
135 |
|
|
reg [9:0] s_col;
|
136 |
|
|
reg [1:0] s_sub;
|
137 |
|
|
|
138 |
|
|
// Can the pending transaction be satisfied with the current (ongoing)
|
139 |
|
|
// transaction?
|
140 |
|
|
reg m_move, m_match, m_pending, m_we;
|
141 |
|
|
reg [25:0] m_addr;
|
142 |
|
|
reg [13:0] m_row;
|
143 |
|
|
reg [2:0] m_bank;
|
144 |
|
|
reg [9:0] m_col;
|
145 |
|
|
reg [1:0] m_sub;
|
146 |
|
|
|
147 |
|
|
// Can we preload the next bank?
|
148 |
|
|
reg [13:0] r_nxt_row;
|
149 |
|
|
reg [2:0] r_nxt_bank;
|
150 |
|
|
|
151 |
|
|
reg need_close_bank, need_close_this_bank,
|
152 |
|
|
last_close_bank, maybe_close_next_bank,
|
153 |
|
|
last_maybe_close,
|
154 |
|
|
need_open_bank, last_open_bank, maybe_open_next_bank,
|
155 |
|
|
last_maybe_open,
|
156 |
|
|
valid_bank, last_valid_bank;
|
157 |
|
|
reg [(`DDR_CMDLEN-1):0] close_bank_cmd, activate_bank_cmd,
|
158 |
|
|
maybe_close_cmd, maybe_open_cmd, rw_cmd;
|
159 |
|
|
reg [1:0] rw_sub;
|
160 |
|
|
reg rw_we;
|
161 |
|
|
|
162 |
|
|
wire w_this_closing_bank, w_this_opening_bank,
|
163 |
|
|
w_this_maybe_close, w_this_maybe_open,
|
164 |
|
|
w_this_rw_move;
|
165 |
|
|
reg last_closing_bank, last_opening_bank;
|
166 |
|
|
wire w_need_close_this_bank, w_need_open_bank,
|
167 |
|
|
w_r_valid, w_s_valid, w_s_match;
|
168 |
3 |
dgisselq |
//
|
169 |
|
|
// tWTR = 7.5
|
170 |
|
|
// tRRD = 7.5
|
171 |
|
|
// tREFI= 7.8
|
172 |
|
|
// tFAW = 45
|
173 |
|
|
// tRTP = 7.5
|
174 |
|
|
// tCKE = 5.625
|
175 |
|
|
// tRFC = 160
|
176 |
|
|
// tRP = 13.5
|
177 |
|
|
// tRAS = 36
|
178 |
|
|
// tRCD = 13.5
|
179 |
|
|
//
|
180 |
|
|
// RESET:
|
181 |
|
|
// 1. Hold o_reset_n = 1'b0; for 200 us, or 40,000 clocks (65536 perhaps?)
|
182 |
|
|
// Hold cke low during this time as well
|
183 |
|
|
// The clock should be free running into the chip during this time
|
184 |
|
|
// Leave command in NOOP state: {cs,ras,cas,we} = 4'h7;
|
185 |
|
|
// ODT must be held low
|
186 |
|
|
// 2. Hold cke low for another 500us, or 100,000 clocks
|
187 |
|
|
// 3. Raise CKE, continue outputting a NOOP for
|
188 |
|
|
// tXPR, tDLLk, and tZQInit
|
189 |
|
|
// 4. Load MRS2, wait tMRD
|
190 |
|
|
// 4. Load MRS3, wait tMRD
|
191 |
|
|
// 4. Load MRS1, wait tMOD
|
192 |
|
|
// Before using the SDRAM, we'll need to program at least 3 of the mode
|
193 |
|
|
// registers, if not all 4.
|
194 |
|
|
// tMOD clocks are required to program the mode registers, during which
|
195 |
|
|
// time the RAM must be idle.
|
196 |
|
|
//
|
197 |
|
|
// NOOP: CS low, RAS, CAS, and WE high
|
198 |
|
|
|
199 |
17 |
dgisselq |
//
|
200 |
|
|
// Reset logic should be simple, and is given as follows:
|
201 |
|
|
// note that it depends upon a ROM memory, reset_mem, and an address into that
|
202 |
|
|
// memory: reset_address. Each memory location provides either a "command" to
|
203 |
|
|
// the DDR3 SDRAM, or a timer to wait until the next command. Further, the
|
204 |
|
|
// timer commands indicate whether or not the command during the timer is to
|
205 |
|
|
// be set to idle, or whether the command is instead left as it was.
|
206 |
|
|
reg reset_override, reset_ztimer, maintenance_override;
|
207 |
|
|
reg [4:0] reset_address;
|
208 |
|
|
reg [(`DDR_CMDLEN-1):0] reset_cmd, cmd, refresh_cmd,
|
209 |
|
|
maintenance_cmd;
|
210 |
|
|
reg [24:0] reset_instruction;
|
211 |
|
|
reg [16:0] reset_timer;
|
212 |
|
|
initial reset_override = 1'b1;
|
213 |
|
|
initial reset_address = 5'h0;
|
214 |
|
|
always @(posedge i_clk)
|
215 |
|
|
if (i_reset)
|
216 |
|
|
begin
|
217 |
|
|
reset_override <= 1'b1;
|
218 |
|
|
reset_cmd <= { `DDR_NOOP, reset_instruction[16:0]};
|
219 |
|
|
end else if (reset_ztimer)
|
220 |
|
|
begin
|
221 |
|
|
if (reset_instruction[`DDR_RSTDONE])
|
222 |
|
|
reset_override <= 1'b0;
|
223 |
|
|
reset_cmd <= reset_instruction[20:0];
|
224 |
|
|
end
|
225 |
|
|
|
226 |
|
|
initial reset_ztimer = 1'b0; // Is the timer zero?
|
227 |
|
|
initial reset_timer = 17'h02;
|
228 |
|
|
always @(posedge i_clk)
|
229 |
|
|
if (i_reset)
|
230 |
|
|
begin
|
231 |
|
|
reset_ztimer <= 1'b0;
|
232 |
|
|
reset_timer <= 17'd2;
|
233 |
|
|
end else if (!reset_ztimer)
|
234 |
|
|
begin
|
235 |
|
|
reset_ztimer <= (reset_timer == 17'h01);
|
236 |
|
|
reset_timer <= reset_timer - 17'h01;
|
237 |
|
|
end else if (reset_instruction[`DDR_RSTTIMER])
|
238 |
|
|
begin
|
239 |
|
|
reset_ztimer <= 1'b0;
|
240 |
|
|
reset_timer <= reset_instruction[16:0];
|
241 |
|
|
end
|
242 |
|
|
|
243 |
|
|
wire [16:0] w_ckXPR, w_ckRST, w_ckRP,
|
244 |
|
|
w_ckRFC_first;
|
245 |
|
|
wire [2:0] w_ckCAS_MR2, w_ckCAS_MR0, w_ckCAS, w_ckFIVE;
|
246 |
|
|
wire [13:0] w_MR0, w_MR1, w_MR2;
|
247 |
|
|
assign w_ckXPR = CKXPR;
|
248 |
|
|
assign w_ckRST = 4;
|
249 |
|
|
assign w_ckRP = CKRP-2;
|
250 |
|
|
/* verilator lint_off WIDTH */
|
251 |
|
|
assign w_ckCAS = CKCAS;
|
252 |
|
|
/* verilator lint_on WIDTH */
|
253 |
|
|
assign w_ckRFC_first = CKRFC-2-9+8;
|
254 |
|
|
assign w_ckFIVE = 3'h5;
|
255 |
|
|
assign w_ckCAS_MR2 = w_ckFIVE-3'h5; // w_ckCAS-3'h5;
|
256 |
|
|
assign w_ckCAS_MR0 = w_ckFIVE-3'h4; // w_ckCAS-3'h4;
|
257 |
|
|
assign w_MR2 = { 3'h0, 2'b00, 1'b0, 1'b0, 1'b1, w_ckCAS_MR2, 3'b0 };
|
258 |
|
|
assign w_MR1 = {
|
259 |
|
|
1'h0, // Reserved for Future Use (RFU)
|
260 |
|
|
1'b0, // Qoff - output buffer enabled
|
261 |
|
|
1'b1, // TDQS ... enabled
|
262 |
|
|
1'b0, // RFU
|
263 |
|
|
1'b0, // High order bit, Rtt_Nom (3'b011)
|
264 |
|
|
1'b0, // RFU
|
265 |
|
|
//
|
266 |
|
|
1'b0, // Disable write-leveling
|
267 |
|
|
1'b1, // Mid order bit of Rtt_Nom
|
268 |
|
|
1'b0, // High order bit of Output Drvr Impedence Ctrl
|
269 |
|
|
2'b0, // Additive latency = 0
|
270 |
|
|
1'b1, // Low order bit of Rtt_Nom
|
271 |
|
|
1'b0, // DIC set to 2'b00
|
272 |
|
|
1'b0 // MRS1, DLL enable
|
273 |
|
|
};
|
274 |
|
|
assign w_MR0 = {
|
275 |
|
|
1'b0, // Reserved for future use
|
276 |
|
|
1'b0, // PPD control, (slow exit(DLL off))
|
277 |
|
|
3'b1, // Write recovery for auto precharge
|
278 |
|
|
1'b0, // DLL Reset (No)
|
279 |
|
|
//
|
280 |
|
|
1'b0, // TM mode normal
|
281 |
|
|
w_ckCAS_MR0, // High 3-bits, CAS latency (=4'b1110=4'd5,tCL=11)
|
282 |
|
|
//
|
283 |
|
|
1'b0, // Read burst type = nibble sequential
|
284 |
|
|
(CKCAS>11)? 1'b1:1'b0, // Low bit of cas latency
|
285 |
|
|
2'b0 // Burst length = 8 (Fixed)
|
286 |
|
|
};
|
287 |
|
|
always @(posedge i_clk)
|
288 |
|
|
if (i_reset)
|
289 |
|
|
reset_instruction <= { 4'h4, `DDR_NOOP, 17'd40_000 };
|
290 |
|
|
else if (reset_ztimer) case(reset_address) // RSTDONE, TIMER, CKE, ??
|
291 |
|
|
// 1. Reset asserted (active low) for 200 us. (@200MHz)
|
292 |
|
|
5'h0: reset_instruction <= { 4'h4, `DDR_NOOP, 17'd40_000 };
|
293 |
|
|
// 2. Reset de-asserted, wait 500 us before asserting CKE
|
294 |
|
|
5'h1: reset_instruction <= { 4'h6, `DDR_NOOP, 17'd100_000 };
|
295 |
|
|
// 3. Assert CKE, wait minimum of Reset CKE Exit time
|
296 |
|
|
5'h2: reset_instruction <= { 4'h7, `DDR_NOOP, w_ckXPR };
|
297 |
|
|
// 4. Look MR2. (1CK, no TIMER)
|
298 |
|
|
5'h3: reset_instruction <= { 4'h3, `DDR_NOOP, 3'h2, w_MR2 };
|
299 |
|
|
5'h4: reset_instruction <= { 4'h3, `DDR_MRSET, 3'h2, w_MR2 };
|
300 |
|
|
5'h5: reset_instruction <= { 4'h3, `DDR_NOOP, 3'h2, w_MR2 };
|
301 |
|
|
// 3. Wait 4 clocks (tMRD)
|
302 |
|
|
5'h6: reset_instruction <= { 4'h7, `DDR_NOOP, 17'h02 };
|
303 |
|
|
// 5. Set MR1
|
304 |
|
|
5'h7: reset_instruction <= { 4'h3, `DDR_NOOP, 3'h1, w_MR1 };
|
305 |
|
|
5'h8: reset_instruction <= { 4'h3, `DDR_MRSET, 3'h1, w_MR1 };
|
306 |
|
|
5'h9: reset_instruction <= { 4'h3, `DDR_NOOP, 3'h1, w_MR1 };
|
307 |
|
|
// 7. Wait another 4 clocks
|
308 |
|
|
5'ha: reset_instruction <= { 4'h7, `DDR_NOOP, 17'h02 };
|
309 |
|
|
// 8. Send MRS0
|
310 |
|
|
5'hb: reset_instruction <= { 4'h3, `DDR_NOOP, 3'h0, w_MR0 };
|
311 |
|
|
5'hc: reset_instruction <= { 4'h3, `DDR_MRSET, 3'h0, w_MR0 };
|
312 |
|
|
5'hd: reset_instruction <= { 4'h3, `DDR_NOOP, 3'h0, w_MR0 };
|
313 |
|
|
// 9. Wait tMOD, is max(12 clocks, 15ns)
|
314 |
|
|
5'he: reset_instruction <= { 4'h7, `DDR_NOOP, 17'h0a };
|
315 |
|
|
// 10. Issue a ZQCL command to start ZQ calibration, A10 is high
|
316 |
|
|
5'hf: reset_instruction <= { 4'h3, `DDR_ZQS, 6'h0, 1'b1, 10'h0};
|
317 |
|
|
//11.Wait for both tDLLK and tZQinit completed, both are 512 cks
|
318 |
|
|
5'h10: reset_instruction <= { 4'h7, `DDR_NOOP, 17'd512 };
|
319 |
|
|
// 12. Precharge all command
|
320 |
|
|
5'h11: reset_instruction <= { 4'h3, `DDR_PRECHARGE, 6'h0, 1'b1, 10'h0 };
|
321 |
|
|
// 13. Wait for the precharge to complete
|
322 |
|
|
5'h12: reset_instruction <= { 4'h7, `DDR_NOOP, w_ckRP };
|
323 |
|
|
// 14. A single Auto Refresh commands
|
324 |
|
|
5'h13: reset_instruction <= { 4'h3, `DDR_REFRESH, 17'h00 };
|
325 |
|
|
// 15. Wait for the auto refresh to complete
|
326 |
|
|
5'h14: reset_instruction <= { 4'h7, `DDR_NOOP, w_ckRFC_first };
|
327 |
|
|
// Two Auto Refresh commands
|
328 |
|
|
default:
|
329 |
|
|
reset_instruction <={4'hb, `DDR_NOOP, 17'd00_000 };
|
330 |
|
|
endcase
|
331 |
|
|
// reset_instruction <= reset_mem[reset_address];
|
332 |
|
|
|
333 |
|
|
initial reset_address = 5'h0;
|
334 |
|
|
always @(posedge i_clk)
|
335 |
|
|
if (i_reset)
|
336 |
|
|
reset_address <= 5'h1;
|
337 |
|
|
else if ((reset_ztimer)&&(reset_override))
|
338 |
|
|
reset_address <= reset_address + 5'h1;
|
339 |
|
|
//
|
340 |
|
|
// initial reset_mem =
|
341 |
|
|
// 0. !DONE, TIMER,RESET_N=0, CKE=0, CMD = NOOP, TIMER = 200us ( 40,000)
|
342 |
|
|
// 1. !DONE, TIMER,RESET_N=1, CKE=0, CMD = NOOP, TIMER = 500us (100,000)
|
343 |
|
|
// 2. !DONE, TIMER,RESET_N=1, CKE=1, CMD = NOOP, TIMER = (Look me up)
|
344 |
|
|
// 3. !DONE,!TIMER,RESET_N=1, CKE=1, CMD = MODE, MRS
|
345 |
|
|
// 4. !DONE,!TIMER,RESET_N=1, CKE=1, CMD = NOOP, TIMER = tMRS
|
346 |
|
|
// 5. !DONE,!TIMER,RESET_N=1, CKE=1, CMD = MODE, MRS3
|
347 |
|
|
// 6. !DONE,!TIMER,RESET_N=1, CKE=1, CMD = NOOP, TIMER = tMRS
|
348 |
|
|
// 7. !DONE,!TIMER,RESET_N=1, CKE=1, CMD = MODE, MRS1
|
349 |
|
|
// 8. !DONE,!TIMER,RESET_N=1, CKE=1, CMD = NOOP, TIMER = tMRS
|
350 |
|
|
// 9. !DONE,!TIMER,RESET_N=1, CKE=1, CMD = MODE, MRS1
|
351 |
|
|
// 10. !DONE,!TIMER,RESET_N=1, CKE=1, CMD = NOOP, TIMER = tMOD
|
352 |
|
|
// 11. !DONE,!TIMER,RESET_N=1, CKE=1, (Pre-charge all)
|
353 |
|
|
// 12. !DONE,!TIMER,RESET_N=1, CKE=1, (wait)
|
354 |
|
|
// 13. !DONE,!TIMER,RESET_N=1, CKE=1, (Auto-refresh)
|
355 |
|
|
// 14. !DONE,!TIMER,RESET_N=1, CKE=1, (Auto-refresh)
|
356 |
|
|
// 15. !DONE,!TIMER,RESET_N=1, CKE=1, (wait)
|
357 |
|
|
|
358 |
|
|
|
359 |
|
|
//
|
360 |
|
|
//
|
361 |
|
|
// Let's keep track of any open banks. There are 8 of them to keep track of.
|
362 |
|
|
//
|
363 |
|
|
// A precharge requires 3 clocks at 200MHz to complete, 2 clocks at 100MHz.
|
364 |
|
|
//
|
365 |
|
|
//
|
366 |
|
|
//
|
367 |
|
|
reg need_refresh;
|
368 |
|
|
|
369 |
|
|
wire w_precharge_all;
|
370 |
|
|
reg [CKRP:0] bank_status [0:7];
|
371 |
|
|
reg [13:0] bank_address [0:7];
|
372 |
|
|
reg [3:0] bank_wr_ck [0:7]; // tWTR
|
373 |
|
|
reg bank_wr_ckzro [0:7]; // tWTR
|
374 |
|
|
reg [7:0] bank_open;
|
375 |
|
|
reg [7:0] bank_closed;
|
376 |
|
|
|
377 |
|
|
wire [3:0] write_recycle_clocks;
|
378 |
|
|
assign write_recycle_clocks = CKWR+4+4;
|
379 |
|
|
|
380 |
|
|
initial bank_open = 0;
|
381 |
|
|
initial bank_closed = 8'hff;
|
382 |
|
|
always @(posedge i_clk)
|
383 |
|
|
begin
|
384 |
|
|
bank_status[0] <= { bank_status[0][(CKRP-1):0], bank_status[0][0] };
|
385 |
|
|
bank_status[1] <= { bank_status[1][(CKRP-1):0], bank_status[1][0] };
|
386 |
|
|
bank_status[2] <= { bank_status[2][(CKRP-1):0], bank_status[2][0] };
|
387 |
|
|
bank_status[3] <= { bank_status[3][(CKRP-1):0], bank_status[3][0] };
|
388 |
|
|
bank_status[4] <= { bank_status[4][(CKRP-1):0], bank_status[4][0] };
|
389 |
|
|
bank_status[5] <= { bank_status[5][(CKRP-1):0], bank_status[5][0] };
|
390 |
|
|
bank_status[6] <= { bank_status[6][(CKRP-1):0], bank_status[6][0] };
|
391 |
|
|
bank_status[7] <= { bank_status[7][(CKRP-1):0], bank_status[7][0] };
|
392 |
|
|
|
393 |
|
|
bank_wr_ck[0] <= (|bank_wr_ck[0])?(bank_wr_ck[0]-4'h1):4'h0;
|
394 |
|
|
bank_wr_ck[1] <= (|bank_wr_ck[1])?(bank_wr_ck[1]-4'h1):4'h0;
|
395 |
|
|
bank_wr_ck[2] <= (|bank_wr_ck[2])?(bank_wr_ck[2]-4'h1):4'h0;
|
396 |
|
|
bank_wr_ck[3] <= (|bank_wr_ck[3])?(bank_wr_ck[3]-4'h1):4'h0;
|
397 |
|
|
bank_wr_ck[4] <= (|bank_wr_ck[4])?(bank_wr_ck[4]-4'h1):4'h0;
|
398 |
|
|
bank_wr_ck[5] <= (|bank_wr_ck[5])?(bank_wr_ck[5]-4'h1):4'h0;
|
399 |
|
|
bank_wr_ck[6] <= (|bank_wr_ck[6])?(bank_wr_ck[6]-4'h1):4'h0;
|
400 |
|
|
bank_wr_ck[7] <= (|bank_wr_ck[7])?(bank_wr_ck[7]-4'h1):4'h0;
|
401 |
|
|
|
402 |
|
|
bank_wr_ckzro[0] <= (bank_wr_ck[0][3:1]==3'b00);
|
403 |
|
|
bank_wr_ckzro[1] <= (bank_wr_ck[1][3:1]==3'b00);
|
404 |
|
|
bank_wr_ckzro[2] <= (bank_wr_ck[2][3:1]==3'b00);
|
405 |
|
|
bank_wr_ckzro[3] <= (bank_wr_ck[3][3:1]==3'b00);
|
406 |
|
|
bank_wr_ckzro[4] <= (bank_wr_ck[4][3:1]==3'b00);
|
407 |
|
|
bank_wr_ckzro[5] <= (bank_wr_ck[5][3:1]==3'b00);
|
408 |
|
|
bank_wr_ckzro[6] <= (bank_wr_ck[6][3:1]==3'b00);
|
409 |
|
|
bank_wr_ckzro[7] <= (bank_wr_ck[7][3:1]==3'b00);
|
410 |
|
|
|
411 |
|
|
bank_open[0] <= (bank_status[0][(CKRP-2):0] =={(CKRP-1){1'b1}});
|
412 |
|
|
bank_open[1] <= (bank_status[1][(CKRP-2):0] =={(CKRP-1){1'b1}});
|
413 |
|
|
bank_open[2] <= (bank_status[2][(CKRP-2):0] =={(CKRP-1){1'b1}});
|
414 |
|
|
bank_open[3] <= (bank_status[3][(CKRP-2):0] =={(CKRP-1){1'b1}});
|
415 |
|
|
bank_open[4] <= (bank_status[4][(CKRP-2):0] =={(CKRP-1){1'b1}});
|
416 |
|
|
bank_open[5] <= (bank_status[5][(CKRP-2):0] =={(CKRP-1){1'b1}});
|
417 |
|
|
bank_open[6] <= (bank_status[6][(CKRP-2):0] =={(CKRP-1){1'b1}});
|
418 |
|
|
bank_open[7] <= (bank_status[7][(CKRP-2):0] =={(CKRP-1){1'b1}});
|
419 |
|
|
|
420 |
|
|
bank_closed[0] <= (bank_status[0][(CKRP-3):0] == 0);
|
421 |
|
|
bank_closed[1] <= (bank_status[1][(CKRP-3):0] == 0);
|
422 |
|
|
bank_closed[2] <= (bank_status[2][(CKRP-3):0] == 0);
|
423 |
|
|
bank_closed[3] <= (bank_status[3][(CKRP-3):0] == 0);
|
424 |
|
|
bank_closed[4] <= (bank_status[4][(CKRP-3):0] == 0);
|
425 |
|
|
bank_closed[5] <= (bank_status[5][(CKRP-3):0] == 0);
|
426 |
|
|
bank_closed[6] <= (bank_status[6][(CKRP-3):0] == 0);
|
427 |
|
|
bank_closed[7] <= (bank_status[7][(CKRP-3):0] == 0);
|
428 |
|
|
|
429 |
|
|
if (w_this_rw_move)
|
430 |
|
|
bank_wr_ck[rw_cmd[16:14]] <= (rw_cmd[`DDR_WEBIT])? 4'h0
|
431 |
|
|
: write_recycle_clocks;
|
432 |
|
|
|
433 |
|
|
if (maintenance_override)
|
434 |
|
|
begin
|
435 |
|
|
bank_status[0][0] <= 1'b0;
|
436 |
|
|
bank_status[1][0] <= 1'b0;
|
437 |
|
|
bank_status[2][0] <= 1'b0;
|
438 |
|
|
bank_status[3][0] <= 1'b0;
|
439 |
|
|
bank_status[4][0] <= 1'b0;
|
440 |
|
|
bank_status[5][0] <= 1'b0;
|
441 |
|
|
bank_status[6][0] <= 1'b0;
|
442 |
|
|
bank_status[7][0] <= 1'b0;
|
443 |
|
|
bank_open <= 0;
|
444 |
|
|
bank_closed <= 8'hff;
|
445 |
|
|
end else if (need_close_bank)
|
446 |
|
|
begin
|
447 |
|
|
bank_status[close_bank_cmd[16:14]]
|
448 |
|
|
<= { bank_status[close_bank_cmd[16:14]][(CKRP-1):0], 1'b0 };
|
449 |
|
|
bank_open[close_bank_cmd[16:14]] <= 1'b0;
|
450 |
|
|
// bank_status[close_bank_cmd[16:14]][0] <= 1'b0;
|
451 |
|
|
end else if (need_open_bank)
|
452 |
|
|
begin
|
453 |
|
|
bank_status[activate_bank_cmd[16:14]]
|
454 |
|
|
<= { bank_status[activate_bank_cmd[16:14]][(CKRP-1):0], 1'b1 };
|
455 |
|
|
// bank_status[activate_bank_cmd[16:14]][0] <= 1'b1;
|
456 |
|
|
bank_closed[activate_bank_cmd[16:14]] <= 1'b0;
|
457 |
|
|
end else if (valid_bank)
|
458 |
|
|
;
|
459 |
|
|
else if (maybe_close_next_bank)
|
460 |
|
|
begin
|
461 |
|
|
bank_status[maybe_close_cmd[16:14]]
|
462 |
|
|
<= { bank_status[maybe_close_cmd[16:14]][(CKRP-1):0], 1'b0 };
|
463 |
|
|
bank_open[maybe_close_cmd[16:14]] <= 1'b0;
|
464 |
|
|
end else if (maybe_open_next_bank)
|
465 |
|
|
begin
|
466 |
|
|
bank_status[maybe_open_cmd[16:14]]
|
467 |
|
|
<= { bank_status[maybe_open_cmd[16:14]][(CKRP-1):0], 1'b1 };
|
468 |
|
|
// bank_status[activate_bank_cmd[16:14]][0] <= 1'b1;
|
469 |
|
|
bank_closed[maybe_open_cmd[16:14]] <= 1'b0;
|
470 |
|
|
end
|
471 |
|
|
end
|
472 |
|
|
|
473 |
|
|
always @(posedge i_clk)
|
474 |
|
|
// if (cmd[22:19] == `DDR_ACTIVATE)
|
475 |
|
|
if (w_this_opening_bank)
|
476 |
|
|
bank_address[activate_bank_cmd[16:14]]
|
477 |
|
|
<= activate_bank_cmd[13:0];
|
478 |
|
|
else if (!w_this_maybe_open)
|
479 |
|
|
bank_address[maybe_open_cmd[16:14]]
|
480 |
|
|
<= maybe_open_cmd[13:0];
|
481 |
|
|
|
482 |
|
|
//
|
483 |
|
|
//
|
484 |
|
|
// Okay, let's investigate when we need to do a refresh. Our plan will be to
|
485 |
|
|
// do 4 refreshes every tREFI*4 seconds. tREFI = 7.8us, but its a parameter
|
486 |
|
|
// in the number of clocks so that we can handle both 100MHz and 200MHz clocks.
|
487 |
|
|
//
|
488 |
|
|
// Note that 160ns are needed between refresh commands (JEDEC, p172), or
|
489 |
|
|
// 320 clocks @200MHz, or equivalently 160 clocks @100MHz. Thus to issue 4
|
490 |
|
|
// of these refresh cycles will require 4*320=1280 clocks@200 MHz. After this
|
491 |
|
|
// time, no more refreshes will be needed for 6240 clocks.
|
492 |
|
|
//
|
493 |
|
|
// Let's think this through:
|
494 |
|
|
// REFRESH_COST = (n*(320)+24)/(n*1560)
|
495 |
|
|
//
|
496 |
|
|
//
|
497 |
|
|
//
|
498 |
|
|
reg refresh_ztimer;
|
499 |
|
|
reg [16:0] refresh_counter;
|
500 |
|
|
reg [2:0] refresh_addr;
|
501 |
|
|
reg [23:0] refresh_instruction;
|
502 |
|
|
always @(posedge i_clk)
|
503 |
|
|
if (reset_override)
|
504 |
|
|
refresh_addr <= 3'hf;
|
505 |
|
|
else if (refresh_ztimer)
|
506 |
|
|
refresh_addr <= refresh_addr + 3'h1;
|
507 |
|
|
else if (refresh_instruction[`DDR_RFBEGIN])
|
508 |
|
|
refresh_addr <= 3'h0;
|
509 |
|
|
|
510 |
|
|
always @(posedge i_clk)
|
511 |
|
|
if (reset_override)
|
512 |
|
|
begin
|
513 |
|
|
refresh_ztimer <= 1'b1;
|
514 |
|
|
refresh_counter <= 17'd0;
|
515 |
|
|
end else if (!refresh_ztimer)
|
516 |
|
|
begin
|
517 |
|
|
refresh_ztimer <= (refresh_counter == 17'h1);
|
518 |
|
|
refresh_counter <= (refresh_counter - 17'h1);
|
519 |
|
|
end else if (refresh_instruction[`DDR_RFTIMER])
|
520 |
|
|
begin
|
521 |
|
|
refresh_ztimer <= 1'b0;
|
522 |
|
|
refresh_counter <= refresh_instruction[16:0];
|
523 |
|
|
end
|
524 |
|
|
|
525 |
|
|
`ifdef QUADRUPLE_REFRESH
|
526 |
|
|
// REFI4 = 13'd6240
|
527 |
|
|
wire [16:0] w_ckREFIn, w_ckREFRst, w_wait_for_idle,
|
528 |
|
|
w_precharge_to_refresh;
|
529 |
|
|
assign w_wait_for_idle = 5+CKCAS;
|
530 |
|
|
assign w_precharge_to_refresh = CKRP-1;
|
531 |
|
|
assign w_ckREFIn[(CKRBITS-1): 0] = CKREFI4-5*CKRFC-2-10;
|
532 |
|
|
assign w_ckREFIn[ 16:(CKRBITS)] = 0;
|
533 |
|
|
assign w_ckREFRst = CKRFC-2-12;
|
534 |
|
|
|
535 |
|
|
always @(posedge i_clk)
|
536 |
|
|
if (refresh_ztimer)
|
537 |
|
|
case(refresh_addr)//NEED-RFC, HAVE-TIMER,
|
538 |
|
|
4'h0: refresh_instruction <= { 3'h2, `DDR_NOOP, w_ckREFIn };
|
539 |
|
|
// 17'd10 = time to complete write, plus write recovery time
|
540 |
|
|
// minus two (cause we can't count zero or one)
|
541 |
|
|
// = WL+4+tWR-2 = 10
|
542 |
|
|
// = 5+4+3-2 = 10
|
543 |
|
|
4'h1: refresh_instruction <= { 3'h6, `DDR_NOOP, w_wait_for_idle };
|
544 |
|
|
4'h2: refresh_instruction <= { 3'h4, `DDR_PRECHARGE, 17'h0400 };
|
545 |
|
|
4'h3: refresh_instruction <= { 3'h6, `DDR_NOOP, w_precharge_to_refresh };
|
546 |
|
|
4'h4: refresh_instruction <= { 3'h4, `DDR_REFRESH, 17'h00 };
|
547 |
|
|
4'h5: refresh_instruction <= { 3'h6, `DDR_NOOP, w_ckRFC };
|
548 |
|
|
4'h6: refresh_instruction <= { 3'h4, `DDR_REFRESH, 17'h00 };
|
549 |
|
|
4'h7: refresh_instruction <= { 3'h6, `DDR_NOOP, w_ckRFC };
|
550 |
|
|
4'h8: refresh_instruction <= { 3'h4, `DDR_REFRESH, 17'h00 };
|
551 |
|
|
4'h9: refresh_instruction <= { 3'h6, `DDR_NOOP, w_ckRFC };
|
552 |
|
|
4'ha: refresh_instruction <= { 3'h4, `DDR_REFRESH, 17'h00 };
|
553 |
|
|
4'hb: refresh_instruction <= { 3'h6, `DDR_NOOP, w_ckRFC };
|
554 |
|
|
4'hc: refresh_instruction <= { 3'h2, `DDR_NOOP, w_ckREFRst };
|
555 |
|
|
default:
|
556 |
|
|
refresh_instruction <= { 3'h1, `DDR_NOOP, 17'h00 };
|
557 |
|
|
endcase
|
558 |
|
|
`else
|
559 |
|
|
wire [16:0] w_ckREFI_left, w_ckRFC_nxt, w_wait_for_idle,
|
560 |
|
|
w_precharge_to_refresh;
|
561 |
|
|
assign w_wait_for_idle = 5+CKCAS;
|
562 |
|
|
assign w_precharge_to_refresh = CKRP-2;
|
563 |
|
|
assign w_ckREFI_left[16:0] = { 4'h0, CKREFI }-CKRFC-9
|
564 |
|
|
-w_wait_for_idle
|
565 |
|
|
-w_precharge_to_refresh;
|
566 |
|
|
// assign w_ckREFI_left[16:13] = 0;
|
567 |
|
|
assign w_ckRFC_nxt[8:0] = CKRFC+9'h2;
|
568 |
|
|
assign w_ckRFC_nxt[16:9] = 0;
|
569 |
|
|
|
570 |
|
|
always @(posedge i_clk)
|
571 |
|
|
if (refresh_ztimer)
|
572 |
|
|
case(refresh_addr)//NEED-REFRESH, HAVE-TIMER, BEGIN(start-over)
|
573 |
|
|
3'h0: refresh_instruction <= { 3'h2, `DDR_NOOP, w_ckREFI_left };
|
574 |
|
|
3'h1: refresh_instruction <= { 3'h6, `DDR_NOOP, w_wait_for_idle };
|
575 |
|
|
3'h2: refresh_instruction <= { 3'h4, `DDR_PRECHARGE, 17'h0400 };
|
576 |
|
|
3'h3: refresh_instruction <= { 3'h6, `DDR_NOOP, w_precharge_to_refresh };
|
577 |
|
|
3'h4: refresh_instruction <= { 3'h4, `DDR_REFRESH, 17'h00 };
|
578 |
|
|
3'h5: refresh_instruction <= { 3'h6, `DDR_NOOP, w_ckRFC_nxt };
|
579 |
|
|
default:
|
580 |
|
|
refresh_instruction <= { 3'h1, `DDR_NOOP, 17'h00 };
|
581 |
|
|
endcase
|
582 |
|
|
`endif
|
583 |
|
|
|
584 |
|
|
always @(posedge i_clk)
|
585 |
|
|
if (reset_override)
|
586 |
|
|
refresh_cmd <= { `DDR_NOOP, w_ckREFI_left };
|
587 |
|
|
else if (refresh_ztimer)
|
588 |
|
|
refresh_cmd <= refresh_instruction[20:0];
|
589 |
|
|
always @(posedge i_clk)
|
590 |
|
|
if (reset_override)
|
591 |
|
|
need_refresh <= 1'b0;
|
592 |
|
|
else if (refresh_ztimer)
|
593 |
|
|
need_refresh <= refresh_instruction[`DDR_NEEDREFRESH];
|
594 |
|
|
|
595 |
|
|
|
596 |
|
|
//
|
597 |
|
|
//
|
598 |
|
|
// Let's track: when will our bus be active? When will we be reading or
|
599 |
|
|
// writing?
|
600 |
|
|
//
|
601 |
|
|
//
|
602 |
|
|
reg [BUSNOW:0] bus_active, bus_read, bus_new, bus_ack;
|
603 |
|
|
reg [1:0] bus_subaddr [BUSNOW:0];
|
604 |
|
|
initial bus_active = 0;
|
605 |
|
|
initial bus_ack = 0;
|
606 |
|
|
always @(posedge i_clk)
|
607 |
|
|
begin
|
608 |
|
|
bus_active[BUSNOW:0] <= { bus_active[(BUSNOW-1):0], 1'b0 };
|
609 |
|
|
bus_read[BUSNOW:0] <= { bus_read[(BUSNOW-1):0], 1'b0 }; // Drive the d-bus?
|
610 |
|
|
// Is this a new command? i.e., the start of a transaction?
|
611 |
|
|
bus_new[BUSNOW:0] <= { bus_new[(BUSNOW-1):0], 1'b0 };
|
612 |
|
|
// Will this position on the bus get a wishbone acknowledgement?
|
613 |
|
|
bus_ack[BUSNOW:0] <= { bus_ack[(BUSNOW-1):0], 1'b0 };
|
614 |
|
|
//bus_mask[8:0] <= { bus_mask[7:0], 1'b1 }; // Write this value?
|
615 |
|
|
bus_subaddr[8] <= bus_subaddr[7];
|
616 |
|
|
bus_subaddr[7] <= bus_subaddr[6];
|
617 |
|
|
bus_subaddr[6] <= bus_subaddr[5];
|
618 |
|
|
bus_subaddr[5] <= bus_subaddr[4];
|
619 |
|
|
bus_subaddr[4] <= bus_subaddr[3];
|
620 |
|
|
bus_subaddr[3] <= bus_subaddr[2];
|
621 |
|
|
bus_subaddr[2] <= bus_subaddr[1];
|
622 |
|
|
bus_subaddr[1] <= bus_subaddr[0];
|
623 |
|
|
bus_subaddr[0] <= 2'h3;
|
624 |
|
|
|
625 |
|
|
bus_ack[5] <= (bus_ack[4])&&
|
626 |
|
|
((bus_subaddr[5] != bus_subaddr[4])
|
627 |
|
|
||(bus_new[4]));
|
628 |
|
|
if (w_this_rw_move)
|
629 |
|
|
begin
|
630 |
|
|
bus_active[3:0]<= 4'hf; // Once per clock
|
631 |
|
|
bus_subaddr[3] <= 2'h0;
|
632 |
|
|
bus_subaddr[2] <= 2'h1;
|
633 |
|
|
bus_subaddr[1] <= 2'h2;
|
634 |
|
|
bus_new[{ 2'b0, rw_sub }] <= 1'b1;
|
635 |
|
|
bus_ack[3:0] <= 4'h0;
|
636 |
|
|
bus_ack[{ 2'b0, rw_sub }] <= 1'b1;
|
637 |
|
|
|
638 |
|
|
bus_read[3:0] <= (rw_we)? 4'h0:4'hf;
|
639 |
|
|
end else if ((s_pending)&&(!pipe_stall))
|
640 |
|
|
begin
|
641 |
|
|
if (bus_subaddr[3] == s_sub)
|
642 |
|
|
bus_ack[4] <= 1'b1;
|
643 |
|
|
if (bus_subaddr[2] == s_sub)
|
644 |
|
|
bus_ack[3] <= 1'b1;
|
645 |
|
|
if (bus_subaddr[1] == s_sub)
|
646 |
|
|
bus_ack[2] <= 1'b1;
|
647 |
|
|
if (bus_subaddr[0] == s_sub)
|
648 |
|
|
bus_ack[1] <= 1'b1;
|
649 |
|
|
end
|
650 |
|
|
end
|
651 |
|
|
|
652 |
3 |
dgisselq |
// Need to set o_wb_dqs high one clock prior to any read.
|
653 |
17 |
dgisselq |
always @(posedge i_clk)
|
654 |
|
|
drive_dqs <= (|bus_active[BUSREG:(BUSREG-1)])
|
655 |
|
|
&&(~(|bus_read[BUSREG:(BUSREG-1)]));
|
656 |
|
|
|
657 |
|
|
//
|
658 |
|
|
//
|
659 |
|
|
// Now, let's see, can we issue a read command?
|
660 |
|
|
//
|
661 |
|
|
//
|
662 |
|
|
reg pre_valid;
|
663 |
|
|
always @(posedge i_clk)
|
664 |
|
|
if ((refresh_ztimer)&&(refresh_instruction[`DDR_NEEDREFRESH]))
|
665 |
|
|
pre_valid <= 1'b0;
|
666 |
|
|
else if (need_refresh)
|
667 |
|
|
pre_valid <= 1'b0;
|
668 |
|
|
else if (w_this_rw_move)
|
669 |
|
|
pre_valid <= 1'b0;
|
670 |
|
|
else if (bus_active[0])
|
671 |
|
|
pre_valid <= 1'b0;
|
672 |
|
|
else
|
673 |
|
|
pre_valid <= 1'b1;
|
674 |
|
|
|
675 |
|
|
assign w_r_valid = (pre_valid)&&(r_pending)
|
676 |
|
|
&&(bank_status[r_bank][(CKRP-2)])
|
677 |
|
|
&&(bank_address[r_bank]==r_row)
|
678 |
|
|
&&((r_we)||(bank_wr_ckzro[r_bank]));
|
679 |
|
|
assign w_s_valid = (pre_valid)&&(s_pending)
|
680 |
|
|
&&(bank_status[s_bank][(CKRP-2)])
|
681 |
|
|
&&(bank_address[s_bank]==s_row)
|
682 |
|
|
&&((s_we)||(bank_wr_ckzro[s_bank]));
|
683 |
|
|
assign w_s_match = (s_pending)&&(r_pending)&&(r_we == s_we)
|
684 |
|
|
&&(r_row == s_row)&&(r_bank == s_bank)
|
685 |
|
|
&&(r_col == s_col)
|
686 |
|
|
&&(r_sub > s_sub);
|
687 |
|
|
|
688 |
|
|
reg pipe_stall;
|
689 |
|
|
always @(posedge i_clk)
|
690 |
|
|
begin
|
691 |
|
|
r_pending <= (i_wb_stb)&&(~o_wb_stall)
|
692 |
|
|
||(r_pending)&&(pipe_stall);
|
693 |
|
|
if (~pipe_stall)
|
694 |
|
|
s_pending <= r_pending;
|
695 |
|
|
if (~pipe_stall)
|
696 |
|
|
begin
|
697 |
|
|
pipe_stall <= (r_pending)&&(((!w_r_valid)||(valid_bank))&&(!w_s_match));
|
698 |
|
|
o_wb_stall <= (r_pending)&&(((!w_r_valid)||(valid_bank))&&(!w_s_match));
|
699 |
|
|
end else begin // if (pipe_stall)
|
700 |
|
|
pipe_stall <= (s_pending)&&((!w_s_valid)||(valid_bank)||(r_move)||(last_valid_bank));
|
701 |
|
|
o_wb_stall <= (s_pending)&&((!w_s_valid)||(valid_bank)||(r_move)||(last_valid_bank));
|
702 |
|
|
end
|
703 |
|
|
if (need_refresh)
|
704 |
|
|
o_wb_stall <= 1'b1;
|
705 |
|
|
|
706 |
|
|
if (~pipe_stall)
|
707 |
|
|
begin
|
708 |
|
|
r_we <= i_wb_we;
|
709 |
|
|
r_addr <= i_wb_addr;
|
710 |
|
|
r_data <= i_wb_data;
|
711 |
|
|
r_row <= i_wb_addr[25:12];
|
712 |
|
|
r_bank <= i_wb_addr[11:9];
|
713 |
|
|
r_col <= { i_wb_addr[8:2], 3'b000 }; // 9:2
|
714 |
|
|
r_sub <= i_wb_addr[1:0];
|
715 |
|
|
|
716 |
|
|
// pre-emptive work
|
717 |
|
|
r_nxt_row <= (i_wb_addr[11:9]==3'h7)?i_wb_addr[25:12]+14'h1:i_wb_addr[25:12];
|
718 |
|
|
r_nxt_bank <= i_wb_addr[11:9]+3'h1;
|
719 |
|
|
end
|
720 |
|
|
|
721 |
|
|
if (~pipe_stall)
|
722 |
|
|
begin
|
723 |
|
|
// Moving one down the pipeline
|
724 |
|
|
s_we <= r_we;
|
725 |
|
|
s_addr <= r_addr;
|
726 |
|
|
s_data <= r_data;
|
727 |
|
|
s_row <= r_row;
|
728 |
|
|
s_bank <= r_bank;
|
729 |
|
|
s_col <= r_col;
|
730 |
|
|
s_sub <= r_sub;
|
731 |
|
|
|
732 |
|
|
// pre-emptive work
|
733 |
|
|
s_nxt_row <= r_nxt_row;
|
734 |
|
|
s_nxt_bank <= r_nxt_bank;
|
735 |
|
|
|
736 |
|
|
// s_match <= w_s_match;
|
737 |
|
|
end
|
738 |
|
|
end
|
739 |
|
|
|
740 |
|
|
assign w_need_close_this_bank = (r_pending)
|
741 |
|
|
&&(bank_open[r_bank])
|
742 |
|
|
&&(bank_wr_ckzro[r_bank])
|
743 |
|
|
&&(r_row != bank_address[r_bank])
|
744 |
|
|
||(pipe_stall)&&(s_pending)&&(bank_open[s_bank])
|
745 |
|
|
&&(s_row != bank_address[s_bank]);
|
746 |
|
|
assign w_need_open_bank = (r_pending)&&(bank_closed[r_bank])
|
747 |
|
|
||(pipe_stall)&&(s_pending)&&(bank_closed[s_bank]);
|
748 |
|
|
|
749 |
|
|
always @(posedge i_clk)
|
750 |
|
|
begin
|
751 |
|
|
need_close_bank <= (w_need_close_this_bank)
|
752 |
|
|
&&(!need_open_bank)
|
753 |
|
|
&&(!need_close_bank)
|
754 |
|
|
&&(!w_this_closing_bank);
|
755 |
|
|
|
756 |
|
|
maybe_close_next_bank <= (s_pending)
|
757 |
|
|
&&(bank_open[s_nxt_bank])
|
758 |
|
|
&&(bank_wr_ckzro[s_nxt_bank])
|
759 |
|
|
&&(s_nxt_row != bank_address[s_nxt_bank])
|
760 |
|
|
&&(!w_this_maybe_close)&&(!last_maybe_close);
|
761 |
|
|
|
762 |
|
|
close_bank_cmd <= { `DDR_PRECHARGE, r_bank, r_row[13:11], 1'b0, r_row[9:0] };
|
763 |
|
|
maybe_close_cmd <= { `DDR_PRECHARGE, r_nxt_bank, r_nxt_row[13:11], 1'b0, r_nxt_row[9:0] };
|
764 |
|
|
|
765 |
|
|
|
766 |
|
|
need_open_bank <= (w_need_open_bank)
|
767 |
|
|
&&(!w_this_opening_bank);
|
768 |
|
|
last_open_bank <= (w_this_opening_bank);
|
769 |
|
|
|
770 |
|
|
maybe_open_next_bank <= (s_pending)
|
771 |
|
|
&&(!need_close_bank)
|
772 |
|
|
&&(!need_open_bank)
|
773 |
|
|
&&(bank_closed[s_nxt_bank])
|
774 |
|
|
&&(!w_this_maybe_open); // &&(!last_maybe_open);
|
775 |
|
|
|
776 |
|
|
activate_bank_cmd<= { `DDR_ACTIVATE, r_bank, r_row[13:0] };
|
777 |
|
|
maybe_open_cmd <= { `DDR_ACTIVATE,r_nxt_bank, r_nxt_row[13:0] };
|
778 |
|
|
|
779 |
|
|
|
780 |
|
|
|
781 |
|
|
valid_bank <= ((w_r_valid)||((pipe_stall)&&(w_s_valid)))
|
782 |
|
|
&&(!last_valid_bank)&&(!r_move)
|
783 |
|
|
&&(!w_this_rw_move);
|
784 |
|
|
last_valid_bank <= r_move;
|
785 |
|
|
|
786 |
|
|
if ((s_pending)&&(pipe_stall))
|
787 |
|
|
rw_cmd[`DDR_CSBIT:`DDR_WEBIT] <= (s_we)?`DDR_WRITE:`DDR_READ;
|
788 |
|
|
else if (r_pending)
|
789 |
|
|
rw_cmd[`DDR_CSBIT:`DDR_WEBIT] <= (r_we)?`DDR_WRITE:`DDR_READ;
|
790 |
|
|
else
|
791 |
|
|
rw_cmd[`DDR_CSBIT:`DDR_WEBIT] <= `DDR_NOOP;
|
792 |
|
|
if ((s_pending)&&(pipe_stall))
|
793 |
|
|
rw_cmd[`DDR_WEBIT-1:0] <= { s_bank, 3'h0, 1'b0, s_col };
|
794 |
|
|
else
|
795 |
|
|
rw_cmd[`DDR_WEBIT-1:0] <= { r_bank, 3'h0, 1'b0, r_col };
|
796 |
|
|
if ((s_pending)&&(pipe_stall))
|
797 |
|
|
rw_sub <= 2'b11 - s_sub;
|
798 |
|
|
else
|
799 |
|
|
rw_sub <= 2'b11 - r_sub;
|
800 |
|
|
if ((s_pending)&&(pipe_stall))
|
801 |
|
|
rw_we <= s_we;
|
802 |
|
|
else
|
803 |
|
|
rw_we <= r_we;
|
804 |
|
|
|
805 |
|
|
end
|
806 |
|
|
|
807 |
|
|
//
|
808 |
|
|
//
|
809 |
|
|
// Okay, let's look at the last assignment in our chain. It should look
|
810 |
|
|
// something like:
|
811 |
|
|
always @(posedge i_clk)
|
812 |
|
|
if (i_reset)
|
813 |
|
|
o_ddr_reset_n <= 1'b0;
|
814 |
|
|
else if (reset_ztimer)
|
815 |
|
|
o_ddr_reset_n <= reset_instruction[`DDR_RSTBIT];
|
816 |
|
|
always @(posedge i_clk)
|
817 |
|
|
if (i_reset)
|
818 |
|
|
o_ddr_cke <= 1'b0;
|
819 |
|
|
else if (reset_ztimer)
|
820 |
|
|
o_ddr_cke <= reset_instruction[`DDR_CKEBIT];
|
821 |
|
|
|
822 |
|
|
always @(posedge i_clk)
|
823 |
|
|
if (i_reset)
|
824 |
|
|
maintenance_override <= 1'b1;
|
825 |
|
|
else
|
826 |
|
|
maintenance_override <= (reset_override)||(need_refresh);
|
827 |
|
|
|
828 |
|
|
initial maintenance_cmd = { `DDR_NOOP, 17'h00 };
|
829 |
|
|
always @(posedge i_clk)
|
830 |
|
|
if (i_reset)
|
831 |
|
|
maintenance_cmd <= { `DDR_NOOP, 17'h00 };
|
832 |
|
|
else
|
833 |
|
|
maintenance_cmd <= (reset_override)?reset_cmd:refresh_cmd;
|
834 |
|
|
|
835 |
|
|
assign w_this_closing_bank = (!maintenance_override)
|
836 |
|
|
&&(need_close_bank);
|
837 |
|
|
assign w_this_opening_bank = (!maintenance_override)
|
838 |
|
|
&&(!need_close_bank)&&(need_open_bank);
|
839 |
|
|
assign w_this_rw_move = (!maintenance_override)
|
840 |
|
|
&&(!need_close_bank)&&(!need_open_bank)
|
841 |
|
|
&&(valid_bank);
|
842 |
|
|
assign w_this_maybe_close = (!maintenance_override)
|
843 |
|
|
&&(!need_close_bank)&&(!need_open_bank)
|
844 |
|
|
&&(!valid_bank)
|
845 |
|
|
&&(maybe_close_next_bank);
|
846 |
|
|
assign w_this_maybe_open = (!maintenance_override)
|
847 |
|
|
&&(!need_close_bank)&&(!need_open_bank)
|
848 |
|
|
&&(!valid_bank)
|
849 |
|
|
&&(!maybe_close_next_bank)
|
850 |
|
|
&&(maybe_open_next_bank);
|
851 |
|
|
always @(posedge i_clk)
|
852 |
|
|
begin
|
853 |
|
|
last_opening_bank <= 1'b0;
|
854 |
|
|
last_closing_bank <= 1'b0;
|
855 |
|
|
last_maybe_open <= 1'b0;
|
856 |
|
|
last_maybe_close <= 1'b0;
|
857 |
|
|
r_move <= 1'b0;
|
858 |
|
|
if (maintenance_override) // Command from either reset or
|
859 |
|
|
cmd <= maintenance_cmd; // refresh logic
|
860 |
|
|
else if (need_close_bank)
|
861 |
|
|
begin
|
862 |
|
|
cmd <= close_bank_cmd;
|
863 |
|
|
last_closing_bank <= 1'b1;
|
864 |
|
|
end else if (need_open_bank)
|
865 |
|
|
begin
|
866 |
|
|
cmd <= activate_bank_cmd;
|
867 |
|
|
last_opening_bank <= 1'b1;
|
868 |
|
|
end else if (valid_bank)
|
869 |
|
|
begin
|
870 |
|
|
cmd <= rw_cmd;
|
871 |
|
|
r_move <= 1'b1;
|
872 |
|
|
end else if (maybe_close_next_bank)
|
873 |
|
|
begin
|
874 |
|
|
cmd <= maybe_close_cmd;
|
875 |
|
|
last_maybe_close <= 1'b1;
|
876 |
|
|
end else if (maybe_open_next_bank)
|
877 |
|
|
begin
|
878 |
|
|
cmd <= maybe_open_cmd;
|
879 |
|
|
last_maybe_open <= 1'b1;
|
880 |
|
|
end else
|
881 |
|
|
cmd <= { `DDR_NOOP, rw_cmd[(`DDR_WEBIT-1):0] };
|
882 |
|
|
end
|
883 |
|
|
|
884 |
|
|
`define LGFIFOLN 4
|
885 |
|
|
`define FIFOLEN 16
|
886 |
|
|
reg [(`LGFIFOLN-1):0] bus_fifo_head, bus_fifo_tail;
|
887 |
|
|
reg [31:0] bus_fifo_data [0:(`FIFOLEN-1)];
|
888 |
|
|
reg [1:0] bus_fifo_sub [0:(`FIFOLEN-1)];
|
889 |
|
|
reg bus_fifo_new [0:(`FIFOLEN-1)];
|
890 |
|
|
reg pre_ack;
|
891 |
|
|
|
892 |
|
|
// The bus R/W FIFO
|
893 |
|
|
wire w_bus_fifo_read_next_transaction;
|
894 |
|
|
assign w_bus_fifo_read_next_transaction = (bus_ack[BUSREG]);
|
895 |
|
|
always @(posedge i_clk)
|
896 |
|
|
begin
|
897 |
|
|
pre_ack <= 1'b0;
|
898 |
|
|
o_ddr_dm <= 1'b0;
|
899 |
|
|
if (reset_override)
|
900 |
|
|
begin
|
901 |
|
|
bus_fifo_head <= {(`LGFIFOLN){1'b0}};
|
902 |
|
|
bus_fifo_tail <= {(`LGFIFOLN){1'b0}};
|
903 |
|
|
o_ddr_dm <= 1'b0;
|
904 |
|
|
end else begin
|
905 |
|
|
if ((s_pending)&&(!pipe_stall))
|
906 |
|
|
bus_fifo_head <= bus_fifo_head + 1'b1;
|
907 |
|
|
|
908 |
|
|
o_ddr_dm <= (bus_active[BUSREG])&&(!bus_read[BUSREG]);
|
909 |
|
|
if (w_bus_fifo_read_next_transaction)
|
910 |
|
|
begin
|
911 |
|
|
bus_fifo_tail <= bus_fifo_tail + 1'b1;
|
912 |
|
|
pre_ack <= 1'b1;
|
913 |
|
|
o_ddr_dm <= 1'b0;
|
914 |
|
|
end
|
915 |
|
|
end
|
916 |
|
|
bus_fifo_data[bus_fifo_head] <= s_data;
|
917 |
|
|
bus_fifo_sub[bus_fifo_head] <= s_sub;
|
918 |
|
|
bus_fifo_new[bus_fifo_head] <= w_this_rw_move;
|
919 |
|
|
|
920 |
|
|
//
|
921 |
|
|
// if ((s_pending)&&(!pipe_stall)&&(!nxt_valid))
|
922 |
|
|
// nxt_fifo_data <= s_data;
|
923 |
|
|
// nxt_fifo_sub <= s_sub;
|
924 |
|
|
// nxt_fifo_new <= w_this_rw_move;
|
925 |
|
|
// nxt_valid <= 1'b1;
|
926 |
|
|
// bus_fifo_head <= bus_fifo_head+1;
|
927 |
|
|
// bus_fifo_tail <= bus_fifo_tail+1;
|
928 |
|
|
// else if (w_bus_fifo_read_next_transaction)
|
929 |
|
|
// nxt_fifo_data <= bus_fifo_data[bus_fifo_tail]
|
930 |
|
|
// nxt_fifo_sub <= bus_fifo_data[bus_fifo_tail]
|
931 |
|
|
// nxt_fifo_new <= bus_fifo_data[bus_fifo_tail]
|
932 |
|
|
// nxt_valid <= (bus_fifo_tail+1 == bus_fifo_head);
|
933 |
|
|
//
|
934 |
|
|
// if ((!valid)||(w_bus_fifo_next_read_transaction))
|
935 |
|
|
// nxt_ <= bus_fifo_x
|
936 |
|
|
end
|
937 |
|
|
|
938 |
|
|
|
939 |
|
|
assign o_ddr_cs_n = cmd[`DDR_CSBIT];
|
940 |
|
|
assign o_ddr_ras_n = cmd[`DDR_RASBIT];
|
941 |
|
|
assign o_ddr_cas_n = cmd[`DDR_CASBIT];
|
942 |
|
|
assign o_ddr_we_n = cmd[`DDR_WEBIT];
|
943 |
|
|
assign o_ddr_dqs = drive_dqs;
|
944 |
|
|
assign o_ddr_addr = cmd[(`DDR_ADDR_BITS-1):0];
|
945 |
|
|
assign o_ddr_ba = cmd[(`DDR_BABITS+`DDR_ADDR_BITS-1):`DDR_ADDR_BITS];
|
946 |
|
|
always @(posedge i_clk)
|
947 |
|
|
o_ddr_data <= bus_fifo_data[bus_fifo_tail];
|
948 |
|
|
assign w_precharge_all = (cmd[`DDR_CSBIT:`DDR_WEBIT]==`DDR_PRECHARGE)
|
949 |
|
|
&&(o_ddr_addr[10]); // 5 bits
|
950 |
|
|
|
951 |
|
|
assign o_ddr_bus_oe = drive_dqs; // ~bus_read[BUSNOW];
|
952 |
|
|
|
953 |
|
|
// ODT must be in high impedence while reset_n=0, then it can be set
|
954 |
|
|
// to low or high. As per spec, ODT = 0 during reads
|
955 |
|
|
always @(posedge i_clk)
|
956 |
|
|
o_ddr_odt <= (bus_active[BUSREG-3])&&(!bus_read[BUSREG-3])
|
957 |
|
|
||(bus_active[BUSREG-4])&&(!bus_read[BUSREG-4])
|
958 |
|
|
||((w_this_rw_move)&&(rw_we)&&(CKCAS<4))
|
959 |
|
|
||(CKCAS>3)&&(bus_active[BUSREG-5])&&(!bus_read[BUSREG-5]);
|
960 |
|
|
|
961 |
|
|
always @(posedge i_clk)
|
962 |
|
|
o_wb_ack <= pre_ack;
|
963 |
|
|
always @(posedge i_clk)
|
964 |
|
|
o_wb_data <= i_ddr_data;
|
965 |
|
|
|
966 |
3 |
dgisselq |
endmodule
|