1 |
2 |
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 |
|
|
|
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_NOOP 4'b0111
|
48 |
|
|
//`define DDR_DESELECT 4'b1???
|
49 |
|
|
//
|
50 |
|
|
// In this controller, 24-bit commands tend to be passed around. These
|
51 |
|
|
// 'commands' are bit fields. Here we specify the bits associated with
|
52 |
|
|
// the bit fields.
|
53 |
3 |
dgisselq |
`define DDR_RSTDONE 26 // End the reset sequence?
|
54 |
|
|
`define DDR_RSTTIMER 25 // Does this reset command take multiple clocks?
|
55 |
|
|
`define DDR_RSTBIT 24 // Value to place on reset_n
|
56 |
|
|
`define DDR_CKEBIT 23 // Should this reset command set CKE?
|
57 |
|
|
`define DDR_CMDLEN 23
|
58 |
2 |
dgisselq |
`define DDR_CSBIT 22
|
59 |
|
|
`define DDR_RASBIT 21
|
60 |
|
|
`define DDR_CASBIT 20
|
61 |
|
|
`define DDR_WEBIT 19
|
62 |
3 |
dgisselq |
`define DDR_NOPTIMER 18 // Steal this from BA bits
|
63 |
2 |
dgisselq |
`define DDR_BABITS 3 // BABITS are really from 18:16, they are 3 bits
|
64 |
3 |
dgisselq |
`define DDR_ADDR_BITS 14
|
65 |
2 |
dgisselq |
|
66 |
3 |
dgisselq |
module wbddrsdram(i_clk, i_reset,
|
67 |
2 |
dgisselq |
i_wb_cyc, i_wb_stb, i_wb_we, i_wb_addr, i_wb_data,
|
68 |
3 |
dgisselq |
o_wb_ack, o_wb_stall, o_wb_data,
|
69 |
2 |
dgisselq |
o_ddr_reset_n, o_ddr_cke,
|
70 |
|
|
o_ddr_cs_n, o_ddr_ras_n, o_ddr_cas_n, o_ddr_we_n,
|
71 |
|
|
o_ddr_dqs, o_ddr_dm, o_ddr_odt, o_ddr_bus_dir,
|
72 |
|
|
o_ddr_addr, o_ddr_ba, o_ddr_data, i_ddr_data);
|
73 |
3 |
dgisselq |
parameter CKREFI4 = 13'd6240, // 4 * 7.8us at 200 MHz clock
|
74 |
|
|
CKRFC = 140;
|
75 |
|
|
input i_clk, i_reset;
|
76 |
2 |
dgisselq |
// Wishbone inputs
|
77 |
|
|
input i_wb_cyc, i_wb_stb, i_wb_we;
|
78 |
|
|
input [25:0] i_wb_addr;
|
79 |
|
|
input [31:0] i_wb_data;
|
80 |
|
|
// Wishbone outputs
|
81 |
|
|
output reg o_wb_ack;
|
82 |
|
|
output reg o_wb_stall;
|
83 |
|
|
output reg [31:0] o_wb_data;
|
84 |
|
|
// DDR3 RAM Controller
|
85 |
3 |
dgisselq |
output wire o_ddr_reset_n, o_ddr_cke;
|
86 |
2 |
dgisselq |
// Control outputs
|
87 |
|
|
output reg o_ddr_cs_n, o_ddr_ras_n, o_ddr_cas_n,o_ddr_we_n;
|
88 |
|
|
// DQS outputs:set to 3'b010 when data is active, 3'b100 (i.e. 2'bzz) ow
|
89 |
3 |
dgisselq |
output wire o_ddr_dqs;
|
90 |
|
|
output reg o_ddr_dm, o_ddr_odt, o_ddr_bus_dir;
|
91 |
2 |
dgisselq |
// Address outputs
|
92 |
|
|
output reg [13:0] o_ddr_addr;
|
93 |
|
|
output reg [2:0] o_ddr_ba;
|
94 |
|
|
// And the data inputs and outputs
|
95 |
|
|
output reg [31:0] o_ddr_data;
|
96 |
|
|
input i_ddr_data;
|
97 |
|
|
|
98 |
3 |
dgisselq |
reg drive_dqs;
|
99 |
|
|
|
100 |
|
|
// The pending transaction
|
101 |
|
|
reg [31:0] r_data;
|
102 |
|
|
reg r_pending, r_we;
|
103 |
|
|
reg [25:0] r_addr;
|
104 |
|
|
reg [14:0] r_row;
|
105 |
|
|
reg [2:0] r_bank;
|
106 |
|
|
reg [9:0] r_col;
|
107 |
|
|
reg [1:0] r_sub;
|
108 |
|
|
reg r_move; // It was accepted, and can move to next stage
|
109 |
|
|
|
110 |
|
|
// Can the pending transaction be satisfied with the current (ongoing)
|
111 |
|
|
// transaction?
|
112 |
|
|
reg m_move, m_match, m_continue, m_pending, m_we;
|
113 |
|
|
reg [25:0] m_addr;
|
114 |
|
|
reg [14:0] m_row;
|
115 |
|
|
reg [2:0] m_bank;
|
116 |
|
|
reg [9:0] m_col;
|
117 |
|
|
reg [1:0] m_sub;
|
118 |
|
|
|
119 |
|
|
// Can we preload the next bank?
|
120 |
|
|
reg [14:0] r_nxt_row;
|
121 |
|
|
reg [2:0] r_nxt_bank;
|
122 |
|
|
|
123 |
2 |
dgisselq |
//
|
124 |
|
|
// tWTR = 7.5
|
125 |
|
|
// tRRD = 7.5
|
126 |
|
|
// tREFI= 7.8
|
127 |
|
|
// tFAW = 45
|
128 |
|
|
// tRTP = 7.5
|
129 |
|
|
// tCKE = 5.625
|
130 |
|
|
// tRFC = 160
|
131 |
|
|
// tRP = 13.5
|
132 |
|
|
// tRAS = 36
|
133 |
|
|
// tRCD = 13.5
|
134 |
|
|
//
|
135 |
|
|
// RESET:
|
136 |
|
|
// 1. Hold o_reset_n = 1'b0; for 200 us, or 40,000 clocks (65536 perhaps?)
|
137 |
|
|
// Hold cke low during this time as well
|
138 |
|
|
// The clock should be free running into the chip during this time
|
139 |
|
|
// Leave command in NOOP state: {cs,ras,cas,we} = 4'h7;
|
140 |
|
|
// ODT must be held low
|
141 |
|
|
// 2. Hold cke low for another 500us, or 100,000 clocks
|
142 |
|
|
// 3. Raise CKE, continue outputting a NOOP for
|
143 |
|
|
// tXPR, tDLLk, and tZQInit
|
144 |
|
|
// 4. Load MRS2, wait tMRD
|
145 |
|
|
// 4. Load MRS3, wait tMRD
|
146 |
|
|
// 4. Load MRS1, wait tMOD
|
147 |
|
|
// Before using the SDRAM, we'll need to program at least 3 of the mode
|
148 |
|
|
// registers, if not all 4.
|
149 |
|
|
// tMOD clocks are required to program the mode registers, during which
|
150 |
|
|
// time the RAM must be idle.
|
151 |
|
|
//
|
152 |
|
|
// NOOP: CS low, RAS, CAS, and WE high
|
153 |
|
|
|
154 |
|
|
//
|
155 |
|
|
// Reset logic should be simple, and is given as follows:
|
156 |
|
|
// note that it depends upon a ROM memory, reset_mem, and an address into that
|
157 |
|
|
// memory: reset_address. Each memory location provides either a "command" to
|
158 |
|
|
// the DDR3 SDRAM, or a timer to wait until the next command. Further, the
|
159 |
|
|
// timer commands indicate whether or not the command during the timer is to
|
160 |
|
|
// be set to idle, or whether the command is instead left as it was.
|
161 |
3 |
dgisselq |
reg reset_override, reset_ztimer;
|
162 |
2 |
dgisselq |
reg [3:0] reset_address;
|
163 |
3 |
dgisselq |
reg [(`DDR_CMDLEN-1):0] reset_cmd, cmd, refresh_cmd;
|
164 |
2 |
dgisselq |
reg [26:0] reset_instruction;
|
165 |
3 |
dgisselq |
reg [16:0] reset_timer;
|
166 |
|
|
initial reset_override = 1'b1;
|
167 |
|
|
initial reset_address = 4'h0;
|
168 |
2 |
dgisselq |
always @(posedge i_clk)
|
169 |
|
|
if (i_reset)
|
170 |
|
|
begin
|
171 |
|
|
reset_override <= 1'b1;
|
172 |
3 |
dgisselq |
reset_cmd <= { `DDR_NOOP, reset_instruction[18:0]};
|
173 |
2 |
dgisselq |
end else if (!reset_ztimer)
|
174 |
|
|
;
|
175 |
3 |
dgisselq |
else if (reset_instruction[`DDR_RSTDONE])
|
176 |
2 |
dgisselq |
reset_override <= 1'b0;
|
177 |
|
|
else if (reset_instruction[`DDR_RSTTIMER])
|
178 |
|
|
begin
|
179 |
3 |
dgisselq |
if (reset_instruction[`DDR_NOPTIMER])
|
180 |
|
|
reset_cmd <= { `DDR_NOOP, reset_instruction[18:0]};
|
181 |
2 |
dgisselq |
end else begin
|
182 |
3 |
dgisselq |
reset_cmd <= reset_instruction[22:0];
|
183 |
2 |
dgisselq |
end
|
184 |
|
|
always @(posedge i_clk)
|
185 |
|
|
if (i_reset)
|
186 |
|
|
o_ddr_cke <= 1'b0;
|
187 |
3 |
dgisselq |
else if ((reset_override)&&(reset_ztimer))
|
188 |
2 |
dgisselq |
o_ddr_cke <= reset_instruction[`DDR_CKEBIT];
|
189 |
|
|
|
190 |
3 |
dgisselq |
initial reset_ztimer = 1'b1; // Is the timer zero?
|
191 |
|
|
initial reset_timer = 17'h00;
|
192 |
2 |
dgisselq |
always @(posedge i_clk)
|
193 |
|
|
if (i_reset)
|
194 |
|
|
begin
|
195 |
|
|
reset_ztimer <= 1'b0;
|
196 |
|
|
reset_timer <= 17'h00;
|
197 |
|
|
end else if (!reset_ztimer)
|
198 |
|
|
begin
|
199 |
|
|
reset_ztimer <= (reset_timer == 17'h01);
|
200 |
|
|
reset_timer <= reset_timer - 17'h01;
|
201 |
|
|
end else if (reset_instruction[`DDR_RSTTIMER])
|
202 |
|
|
begin
|
203 |
|
|
reset_ztimer <= 1'b0;
|
204 |
|
|
reset_timer <= reset_instruction[16:0];
|
205 |
|
|
end
|
206 |
|
|
|
207 |
|
|
|
208 |
|
|
always @(posedge i_clk)
|
209 |
3 |
dgisselq |
case(reset_address) // RSTDONE, TIMER, CKE, ??
|
210 |
2 |
dgisselq |
4'h0: reset_instruction <= { 4'h4, `DDR_NOOP, 19'd40_000 };
|
211 |
|
|
4'h1: reset_instruction <= { 4'h6, `DDR_NOOP, 19'd100_000 };
|
212 |
|
|
4'h2: reset_instruction <= { 4'h7, `DDR_NOOP, 19'd40_000 };
|
213 |
3 |
dgisselq |
4'h3: reset_instruction <= { 4'h3, `DDR_MRSET, 3'h0, 3'h0, 1'b0, 3'h1, 1'b0, 1'b0, 3'h1, 1'b0, 1'b0, 2'b00 }; // MRS
|
214 |
|
|
4'h4: reset_instruction <= { 4'h7, `DDR_NOOP, 19'd40_000 };
|
215 |
|
|
4'h5: reset_instruction <= { 4'h3, `DDR_MRSET, 3'h2, 5'h0, 2'b00, 1'b0, 1'b0, 1'b1, 3'b0, 3'b0 }; // MRS2
|
216 |
|
|
4'h6: reset_instruction <= { 4'h7, `DDR_NOOP, 19'd40_000 };
|
217 |
|
|
4'h7: reset_instruction <= { 4'h3, `DDR_MRSET, 3'h1, 3'h0, 1'b0, 1'b1, 1'b0, 1'b0, 1'b0, 2'b0, 1'b1, 1'b0, 2'b0, 1'b1, 1'b1, 1'b0 }; // MRS1
|
218 |
2 |
dgisselq |
default:
|
219 |
|
|
reset_instruction <={4'hb, `DDR_NOOP, 19'd00_000 };
|
220 |
|
|
endcase
|
221 |
|
|
// reset_instruction <= reset_mem[reset_address];
|
222 |
|
|
|
223 |
|
|
always @(posedge i_clk)
|
224 |
|
|
if (i_reset)
|
225 |
|
|
reset_address <= 4'h0;
|
226 |
|
|
else if (reset_ztimer)
|
227 |
3 |
dgisselq |
reset_address <= reset_address + 4'h1;
|
228 |
2 |
dgisselq |
//
|
229 |
|
|
// initial reset_mem =
|
230 |
|
|
// 0. !DONE, TIMER,RESET_N=0, CKE=0, CMD = NOOP, TIMER = 200us ( 40,000)
|
231 |
|
|
// 1. !DONE, TIMER,RESET_N=1, CKE=0, CMD = NOOP, TIMER = 500us (100,000)
|
232 |
|
|
// 2. !DONE, TIMER,RESET_N=1, CKE=1, CMD = NOOP, TIMER = (Look me up)
|
233 |
|
|
// 3. !DONE,!TIMER,RESET_N=1, CKE=1, CMD = MODE, MRS
|
234 |
|
|
// 4. !DONE,!TIMER,RESET_N=1, CKE=1, CMD = NOOP, TIMER = tMRS
|
235 |
|
|
// 5. !DONE,!TIMER,RESET_N=1, CKE=1, CMD = MODE, MRS3
|
236 |
|
|
// 6. !DONE,!TIMER,RESET_N=1, CKE=1, CMD = NOOP, TIMER = tMRS
|
237 |
|
|
// 7. !DONE,!TIMER,RESET_N=1, CKE=1, CMD = MODE, MRS1
|
238 |
|
|
// 8. !DONE,!TIMER,RESET_N=1, CKE=1, CMD = NOOP, TIMER = tMRS
|
239 |
|
|
// 9. !DONE,!TIMER,RESET_N=1, CKE=1, CMD = MODE, MRS1
|
240 |
|
|
// 10. !DONE,!TIMER,RESET_N=1, CKE=1, CMD = NOOP, TIMER = tMOD
|
241 |
|
|
// 11. !DONE,!TIMER,RESET_N=1, CKE=1, (Pre-charge all)
|
242 |
|
|
// 12. !DONE,!TIMER,RESET_N=1, CKE=1, (wait)
|
243 |
|
|
// 13. !DONE,!TIMER,RESET_N=1, CKE=1, (Auto-refresh)
|
244 |
|
|
// 14. !DONE,!TIMER,RESET_N=1, CKE=1, (Auto-refresh)
|
245 |
|
|
// 15. !DONE,!TIMER,RESET_N=1, CKE=1, (wait)
|
246 |
|
|
|
247 |
|
|
|
248 |
|
|
//
|
249 |
|
|
//
|
250 |
|
|
// Let's keep track of any open banks. There are 8 of them to keep track of.
|
251 |
|
|
//
|
252 |
|
|
// A precharge requires 3 clocks at 200MHz to complete, 2 clocks at 100MHz.
|
253 |
|
|
//
|
254 |
|
|
//
|
255 |
|
|
//
|
256 |
3 |
dgisselq |
reg need_refresh;
|
257 |
2 |
dgisselq |
|
258 |
3 |
dgisselq |
wire w_precharge_all;
|
259 |
|
|
reg banks_are_closing, all_banks_closed;
|
260 |
2 |
dgisselq |
reg [2:0] bank_status[7:0];
|
261 |
|
|
always @(posedge i_clk)
|
262 |
|
|
begin
|
263 |
|
|
bank_status[0] = { bank_status[0][1:0], bank_status[0][0] };
|
264 |
|
|
bank_status[1] = { bank_status[1][1:0], bank_status[1][0] };
|
265 |
|
|
bank_status[2] = { bank_status[2][1:0], bank_status[2][0] };
|
266 |
|
|
bank_status[3] = { bank_status[3][1:0], bank_status[3][0] };
|
267 |
|
|
bank_status[4] = { bank_status[4][1:0], bank_status[4][0] };
|
268 |
|
|
bank_status[5] = { bank_status[5][1:0], bank_status[5][0] };
|
269 |
|
|
bank_status[6] = { bank_status[6][1:0], bank_status[6][0] };
|
270 |
|
|
bank_status[7] = { bank_status[7][1:0], bank_status[7][0] };
|
271 |
|
|
all_banks_closed <= (bank_status[0][1:0] == 2'b00)
|
272 |
|
|
&&(bank_status[1][1:0] == 2'b00)
|
273 |
|
|
&&(bank_status[2][1:0] == 2'b00)
|
274 |
|
|
&&(bank_status[3][1:0] == 2'b00)
|
275 |
|
|
&&(bank_status[4][1:0] == 2'b00)
|
276 |
|
|
&&(bank_status[5][1:0] == 2'b00)
|
277 |
|
|
&&(bank_status[6][1:0] == 2'b00)
|
278 |
|
|
&&(bank_status[7][1:0] == 2'b00);
|
279 |
|
|
if ((!reset_override)&&(need_refresh)||(w_precharge_all))
|
280 |
|
|
begin
|
281 |
|
|
bank_status[0][0] = 1'b0;
|
282 |
|
|
bank_status[1][0] = 1'b0;
|
283 |
|
|
bank_status[2][0] = 1'b0;
|
284 |
|
|
bank_status[3][0] = 1'b0;
|
285 |
|
|
bank_status[4][0] = 1'b0;
|
286 |
|
|
bank_status[5][0] = 1'b0;
|
287 |
|
|
bank_status[6][0] = 1'b0;
|
288 |
|
|
bank_status[7][0] = 1'b0;
|
289 |
|
|
banks_are_closing <= 1'b1;
|
290 |
|
|
end else if (need_close_bank)
|
291 |
|
|
begin
|
292 |
|
|
bank_status[r_bank][0] = 1'b0;
|
293 |
|
|
end else if (need_open_bank)
|
294 |
|
|
begin
|
295 |
|
|
bank_status[r_bank][0] = 1'b1;
|
296 |
|
|
all_banks_closed <= 1'b0;
|
297 |
|
|
banks_are_closing <= 1'b0;
|
298 |
|
|
end
|
299 |
|
|
end
|
300 |
|
|
|
301 |
|
|
always @(posedge i_clk)
|
302 |
3 |
dgisselq |
// if (cmd[22:19] == `DDR_ACTIVATE)
|
303 |
|
|
if (need_open_bank)
|
304 |
|
|
bank_address[activate_bank_cmd[18:16]]
|
305 |
|
|
<= activate_bank_cmd[14:0];
|
306 |
2 |
dgisselq |
|
307 |
|
|
//
|
308 |
|
|
//
|
309 |
|
|
// Okay, let's investigate when we need to do a refresh. Our plan will be to
|
310 |
|
|
// do 4 refreshes every tREFI*4 seconds. tREFI = 7.8us, but its a parameter
|
311 |
|
|
// in the number of clocks so that we can handle both 100MHz and 200MHz clocks.
|
312 |
|
|
//
|
313 |
|
|
// Note that 160ns are needed between refresh commands (JEDEC, p172), or
|
314 |
|
|
// 320 clocks @200MHz, or equivalently 160 clocks @100MHz. Thus to issue 4
|
315 |
|
|
// of these refresh cycles will require 4*320=1280 clocks@200 MHz. After this
|
316 |
|
|
// time, no more refreshes will be needed for 6240 clocks.
|
317 |
|
|
//
|
318 |
|
|
// Let's think this through:
|
319 |
|
|
// REFRESH_COST = (n*(320)+24)/(n*1560)
|
320 |
|
|
//
|
321 |
|
|
//
|
322 |
|
|
//
|
323 |
3 |
dgisselq |
reg midrefresh, refresh_clear, endrefresh;
|
324 |
2 |
dgisselq |
reg [12:0] refresh_clk;
|
325 |
3 |
dgisselq |
reg [2:0] midrefresh_hctr; // How many refresh cycles?
|
326 |
|
|
reg [8:0] midrefresh_lctr; // How many clks in this refresh cycle
|
327 |
2 |
dgisselq |
always @(posedge i_clk)
|
328 |
3 |
dgisselq |
if ((reset_override)||(refresh_clear))
|
329 |
2 |
dgisselq |
refresh_clk <= CKREFI4;
|
330 |
|
|
else if (|refresh_clk)
|
331 |
|
|
refresh_clk <= refresh_clk-1;
|
332 |
|
|
always @(posedge i_clk)
|
333 |
|
|
need_refresh <= (refresh_clk == 0)||(midrefresh);
|
334 |
|
|
always @(posedge i_clk)
|
335 |
|
|
if (!need_refresh)
|
336 |
3 |
dgisselq |
refresh_cmd <= { `DDR_NOOP, 19'h00 };
|
337 |
2 |
dgisselq |
else if (~banks_are_closing)
|
338 |
3 |
dgisselq |
refresh_cmd <= { `DDR_PRECHARGE, 3'h0, 5'h0, 1'b1, 10'h00 };
|
339 |
2 |
dgisselq |
else if (~all_banks_closed)
|
340 |
3 |
dgisselq |
refresh_cmd <= { `DDR_NOOP, 19'h00 };
|
341 |
2 |
dgisselq |
else
|
342 |
3 |
dgisselq |
refresh_cmd <= { `DDR_REFRESH, 19'h00 };
|
343 |
2 |
dgisselq |
always @(posedge i_clk)
|
344 |
3 |
dgisselq |
midrefresh <= (need_refresh)&&(all_banks_closed)&&(~refresh_clear);
|
345 |
2 |
dgisselq |
|
346 |
|
|
always @(posedge i_clk)
|
347 |
3 |
dgisselq |
if (!midrefresh)
|
348 |
2 |
dgisselq |
midrefresh_hctr <= 3'h4;
|
349 |
|
|
else if ((midrefresh_lctr == 0)&&(|midrefresh_hctr))
|
350 |
|
|
midrefresh_hctr <= midrefresh_hctr - 1;
|
351 |
|
|
always @(posedge i_clk)
|
352 |
3 |
dgisselq |
if ((!need_refresh)||(!midrefresh))
|
353 |
2 |
dgisselq |
endrefresh <= 1'b0;
|
354 |
|
|
else if (midrefresh_hctr == 3'h0)
|
355 |
|
|
endrefresh <= 1'b1;
|
356 |
|
|
always @(posedge i_clk)
|
357 |
3 |
dgisselq |
if (!midrefresh)
|
358 |
2 |
dgisselq |
midrefresh_lctr <= CKRFC;
|
359 |
|
|
else if (midrefresh_lctr == 0)
|
360 |
|
|
midrefresh_lctr <= 0;
|
361 |
|
|
else
|
362 |
|
|
midrefresh_lctr <= CKRFC;
|
363 |
|
|
|
364 |
|
|
always @(posedge i_clk)
|
365 |
3 |
dgisselq |
refresh_clear <= (need_refresh)&&(endrefresh)&&(midrefresh_lctr == 0);
|
366 |
2 |
dgisselq |
|
367 |
|
|
|
368 |
|
|
//
|
369 |
|
|
//
|
370 |
|
|
// Let's track: when will our bus be active? When will we be reading or
|
371 |
|
|
// writing?
|
372 |
|
|
//
|
373 |
|
|
//
|
374 |
3 |
dgisselq |
reg [8:0] bus_active, bus_read;
|
375 |
2 |
dgisselq |
reg [1:0] bus_subaddr [8:0];
|
376 |
3 |
dgisselq |
initial bus_active = 0;
|
377 |
2 |
dgisselq |
always @(posedge i_clk)
|
378 |
|
|
begin
|
379 |
|
|
bus_active[8:0] <= { bus_active[7:0], 1'b0 };
|
380 |
|
|
bus_read[8:0] <= { bus_read[7:0], 1'b0 }; // Drive the d-bus?
|
381 |
3 |
dgisselq |
//bus_mask[8:0] <= { bus_mask[7:0], 1'b1 }; // Write this value?
|
382 |
2 |
dgisselq |
bus_subaddr[8] <= bus_subaddr[7];
|
383 |
|
|
bus_subaddr[7] <= bus_subaddr[6];
|
384 |
|
|
bus_subaddr[6] <= bus_subaddr[5];
|
385 |
|
|
bus_subaddr[5] <= bus_subaddr[4];
|
386 |
|
|
bus_subaddr[4] <= bus_subaddr[3];
|
387 |
|
|
bus_subaddr[3] <= bus_subaddr[2];
|
388 |
|
|
bus_subaddr[2] <= bus_subaddr[1];
|
389 |
|
|
bus_subaddr[1] <= bus_subaddr[0];
|
390 |
|
|
bus_subaddr[0] <= 2'h3;
|
391 |
3 |
dgisselq |
if (cmd[22:19] == `DDR_READ)
|
392 |
2 |
dgisselq |
begin
|
393 |
|
|
bus_active[3:0]<= 4'hf; // Once per clock
|
394 |
|
|
bus_read[3:0] <= 4'hf; // These will be reads
|
395 |
|
|
bus_subaddr[3] <= 2'h0;
|
396 |
|
|
bus_subaddr[2] <= 2'h1;
|
397 |
|
|
bus_subaddr[1] <= 2'h2;
|
398 |
3 |
dgisselq |
end else if (cmd == `DDR_WRITE)
|
399 |
2 |
dgisselq |
begin
|
400 |
|
|
bus_active[3:0] <= 4'hf;
|
401 |
|
|
// bus_read[7:4] = 4'h0;
|
402 |
|
|
bus_subaddr[3] <= 2'h0;
|
403 |
|
|
bus_subaddr[2] <= 2'h1;
|
404 |
|
|
bus_subaddr[1] <= 2'h2;
|
405 |
|
|
end
|
406 |
|
|
end
|
407 |
|
|
|
408 |
|
|
always @(posedge i_clk)
|
409 |
3 |
dgisselq |
drive_dqs <= (~bus_read[8])&&(|bus_active[8:7]);
|
410 |
2 |
dgisselq |
|
411 |
|
|
//
|
412 |
|
|
//
|
413 |
|
|
// Now, let's see, can we issue a read command?
|
414 |
|
|
//
|
415 |
|
|
//
|
416 |
|
|
always @(posedge i_clk)
|
417 |
|
|
begin
|
418 |
|
|
if ((i_wb_stb)&&(~o_wb_stall))
|
419 |
|
|
begin
|
420 |
3 |
dgisselq |
r_pending <= 1'b1;
|
421 |
2 |
dgisselq |
o_wb_stall <= 1'b1;
|
422 |
|
|
end else if ((r_move)||(m_move))
|
423 |
|
|
begin
|
424 |
3 |
dgisselq |
r_pending <= 1'b0;
|
425 |
2 |
dgisselq |
o_wb_stall <= 1'b0;
|
426 |
|
|
end
|
427 |
|
|
|
428 |
|
|
if (~o_wb_stall)
|
429 |
|
|
begin
|
430 |
|
|
r_we <= i_wb_we;
|
431 |
|
|
r_addr <= i_wb_addr;
|
432 |
|
|
r_data <= i_wb_data;
|
433 |
|
|
r_row <= i_wb_addr[25:11];
|
434 |
|
|
r_bank <= i_wb_addr[10:8];
|
435 |
|
|
r_col <= { i_wb_addr[7:2], 2'b00 }; // 9:2
|
436 |
|
|
r_sub <= i_wb_addr[1:0];
|
437 |
|
|
|
438 |
|
|
// pre-emptive work
|
439 |
|
|
r_nxt_row <= i_wb_addr[25:11]+15'h1;
|
440 |
|
|
r_nxt_bank <= i_wb_addr[10:8]+3'h1;
|
441 |
|
|
end
|
442 |
|
|
end
|
443 |
|
|
|
444 |
3 |
dgisselq |
reg [2:0] bank_active[7:0];
|
445 |
|
|
reg [14:0] bank_address[7:0];
|
446 |
|
|
|
447 |
|
|
reg [(`DDR_CMDLEN-1):0] close_bank_cmd, activate_bank_cmd, rw_cmd;
|
448 |
|
|
reg need_close_bank, need_close_this_bank,
|
449 |
|
|
last_close_bank, maybe_close_next_bank,
|
450 |
|
|
need_open_bank, last_open_bank, maybe_open_next_bank,
|
451 |
|
|
valid_bank, last_valid_bank;
|
452 |
2 |
dgisselq |
always @(posedge i_clk)
|
453 |
|
|
begin
|
454 |
|
|
need_close_bank <= (r_pending)&&(bank_active[r_bank][0])
|
455 |
|
|
&&(r_row != bank_address[r_bank])&&(!last_close_bank);
|
456 |
|
|
need_close_this_bank <= (r_pending)&&(bank_active[r_bank][0])
|
457 |
|
|
&&(r_row != bank_address[r_bank]);
|
458 |
|
|
last_close_bank <= need_close_bank;
|
459 |
|
|
|
460 |
|
|
maybe_close_next_bank <= (r_pending)
|
461 |
|
|
&&(bank_active[r_nxt_bank][0])
|
462 |
|
|
&&(r_nxt_row != bank_address[r_nxt_bank])
|
463 |
|
|
&&(!need_close_this_bank);
|
464 |
|
|
|
465 |
|
|
close_bank_cmd <= (maybe_close_next_bank)
|
466 |
3 |
dgisselq |
? { `DDR_PRECHARGE, r_nxt_bank, r_nxt_row[14:10], 1'b0, r_nxt_row[9:0] }
|
467 |
2 |
dgisselq |
: { `DDR_PRECHARGE, r_bank, r_row[15:11], 1'b0, r_row[9:0] };
|
468 |
|
|
|
469 |
|
|
|
470 |
3 |
dgisselq |
need_open_bank <= (r_pending)&&(bank_active[r_bank][1:0]==2'b00)
|
471 |
2 |
dgisselq |
&&(!last_open_bank);
|
472 |
|
|
last_open_bank <= need_open_bank;
|
473 |
|
|
|
474 |
|
|
maybe_open_next_bank <= (r_pending)
|
475 |
3 |
dgisselq |
&&(bank_active[r_nxt_bank][1:0] == 2'b00)
|
476 |
2 |
dgisselq |
&&(!need_open_bank)&&(!need_close_bank);
|
477 |
|
|
|
478 |
|
|
activate_bank_cmd <= (maybe_open_next_bank)
|
479 |
3 |
dgisselq |
? { `DDR_ACTIVATE,r_nxt_bank,1'b0,r_nxt_row[14:0] }
|
480 |
|
|
: { `DDR_ACTIVATE, r_bank, 1'b0,r_row[14:0] };
|
481 |
2 |
dgisselq |
|
482 |
|
|
|
483 |
|
|
|
484 |
3 |
dgisselq |
valid_bank <= (r_pending)&&(bank_active[r_bank][1:0]==2'b11)
|
485 |
2 |
dgisselq |
&&(bank_address[r_bank]==r_row)
|
486 |
|
|
&&(!last_valid_bank);
|
487 |
3 |
dgisselq |
last_valid_bank <= valid_bank;
|
488 |
2 |
dgisselq |
|
489 |
|
|
rw_cmd[`DDR_CSBIT:`DDR_WEBIT] <= (~r_pending)?`DDR_NOOP:((r_we)?`DDR_WRITE:`DDR_READ);
|
490 |
3 |
dgisselq |
rw_cmd[`DDR_WEBIT-1:0] <= { r_bank, 5'h0, 1'b0, r_col };
|
491 |
2 |
dgisselq |
end
|
492 |
|
|
|
493 |
|
|
|
494 |
|
|
// Match registers, to see if we can move forward without sending a
|
495 |
|
|
// new command
|
496 |
|
|
always @(posedge i_clk)
|
497 |
|
|
begin
|
498 |
|
|
if (r_move)
|
499 |
|
|
begin
|
500 |
|
|
m_pending <= r_pending;
|
501 |
|
|
m_we <= r_we;
|
502 |
|
|
m_addr <= r_addr;
|
503 |
|
|
m_row <= r_row;
|
504 |
|
|
m_bank <= r_bank;
|
505 |
|
|
m_col <= r_col;
|
506 |
|
|
m_sub <= r_sub;
|
507 |
|
|
end else if (m_match)
|
508 |
|
|
m_sub <= r_sub;
|
509 |
|
|
|
510 |
3 |
dgisselq |
m_match <= (m_pending)&&(r_pending)&&(r_we == m_we)
|
511 |
2 |
dgisselq |
&&(r_row == m_row)&&(r_bank == m_bank)
|
512 |
|
|
&&(r_col == m_col)
|
513 |
|
|
&&(r_sub > m_sub);
|
514 |
3 |
dgisselq |
m_continue <= (m_pending)&&(r_pending)&&(r_we == m_we)
|
515 |
2 |
dgisselq |
&&(r_row == m_row)&&(r_bank == m_bank)
|
516 |
3 |
dgisselq |
&&(r_col == m_col+10'h1);
|
517 |
|
|
// m_nextbank <= (m_pending)&&(r_pending)&&(r_we == m_we)
|
518 |
|
|
// &&(r_row == m_row)&&(r_bank == m_bank);
|
519 |
2 |
dgisselq |
end
|
520 |
|
|
|
521 |
|
|
//
|
522 |
|
|
//
|
523 |
|
|
// Okay, let's look at the last assignment in our chain. It should look
|
524 |
|
|
// something like:
|
525 |
|
|
always @(posedge i_clk)
|
526 |
3 |
dgisselq |
o_ddr_reset_n <= (~reset_override)||(reset_instruction[`DDR_RSTBIT]);
|
527 |
2 |
dgisselq |
always @(posedge i_clk)
|
528 |
3 |
dgisselq |
o_ddr_cke <= (~reset_override)||(reset_instruction[`DDR_CKEBIT]);
|
529 |
2 |
dgisselq |
always @(posedge i_clk)
|
530 |
|
|
begin
|
531 |
|
|
r_move <= 1'b0;
|
532 |
|
|
if (reset_override)
|
533 |
3 |
dgisselq |
cmd <= reset_cmd[`DDR_CSBIT:0];
|
534 |
2 |
dgisselq |
else if (need_refresh)
|
535 |
|
|
begin
|
536 |
|
|
cmd <= refresh_cmd; // The command from the refresh logc
|
537 |
|
|
end else if (need_close_bank)
|
538 |
|
|
cmd <= close_bank_cmd;
|
539 |
|
|
else if (need_open_bank)
|
540 |
|
|
cmd <= activate_bank_cmd;
|
541 |
|
|
else if ((valid_bank)&&(bus_active[2:0]==3'h0))
|
542 |
|
|
begin
|
543 |
|
|
cmd <= rw_cmd;
|
544 |
|
|
r_move <= 1'b1;
|
545 |
|
|
end else
|
546 |
3 |
dgisselq |
cmd <= { `DDR_NOOP, rw_cmd[20:0] };
|
547 |
2 |
dgisselq |
end
|
548 |
|
|
|
549 |
3 |
dgisselq |
reg [31:0] bus_data[8:0];
|
550 |
|
|
|
551 |
|
|
assign o_ddr_cs_n = cmd[`DDR_CSBIT];
|
552 |
|
|
assign o_ddr_ras_n = cmd[`DDR_RASBIT];
|
553 |
|
|
assign o_ddr_cas_n = cmd[`DDR_CASBIT];
|
554 |
|
|
assign o_ddr_we_n = cmd[`DDR_WEBIT];
|
555 |
2 |
dgisselq |
assign o_ddr_dqs = drive_dqs;
|
556 |
3 |
dgisselq |
assign o_ddr_addr = cmd[(`DDR_ADDR_BITS-1):0];
|
557 |
|
|
assign o_ddr_ba = cmd[(`DDR_BABITS+`DDR_ADDR_BITS-1):`DDR_ADDR_BITS];
|
558 |
2 |
dgisselq |
assign o_ddr_data = bus_data[8];
|
559 |
3 |
dgisselq |
assign w_precharge_all = (cmd[`DDR_CSBIT:`DDR_WEBIT]==`DDR_PRECHARGE)
|
560 |
2 |
dgisselq |
&&(o_ddr_addr[10]); // 5 bits
|
561 |
|
|
|
562 |
|
|
// Need to set o_wb_dqs high one clock prior to any read.
|
563 |
|
|
// As per spec, ODT = 0 during reads
|
564 |
3 |
dgisselq |
assign o_ddr_bus_dir = bus_read[8];
|
565 |
2 |
dgisselq |
assign o_ddr_odt = o_ddr_bus_dir;
|
566 |
|
|
|
567 |
|
|
|
568 |
|
|
endmodule
|