OpenCores
URL https://opencores.org/ocsvn/wbddr3/wbddr3/trunk

Subversion Repositories wbddr3

[/] [wbddr3/] [trunk/] [rtl/] [wbddrsdram.v] - Blame information for rev 5

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.