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

Subversion Repositories openarty

[/] [openarty/] [trunk/] [rtl/] [wbddrsdram.v] - Blame information for rev 39

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

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

powered by: WebSVN 2.1.0

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