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

Subversion Repositories wbddr3

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

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 16 dgisselq
// Project:     A wishbone controlled DDR3 SDRAM memory controller.
6
// Used in:     OpenArty, an entirely open SoC based upon the Arty platform
7 2 dgisselq
//
8 16 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 2 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
 
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 4 dgisselq
`define DDR_ZQS         4'b0110
55 2 dgisselq
`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 5 dgisselq
`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 7 dgisselq
//
66
// Refresh command bit fields
67 18 dgisselq
`define DDR_PREREFRESH_STALL    24
68 7 dgisselq
`define DDR_NEEDREFRESH 23
69
`define DDR_RFTIMER     22
70
`define DDR_RFBEGIN     21
71
//
72 5 dgisselq
`define DDR_CMDLEN      21
73
`define DDR_CSBIT       20
74
`define DDR_RASBIT      19
75
`define DDR_CASBIT      18
76
`define DDR_WEBIT       17
77
`define DDR_NOPTIMER    16      // Steal this from BA bits
78 2 dgisselq
`define DDR_BABITS      3       // BABITS are really from 18:16, they are 3 bits
79 3 dgisselq
`define DDR_ADDR_BITS   14
80 7 dgisselq
//
81 16 dgisselq
//
82 3 dgisselq
module  wbddrsdram(i_clk, i_reset,
83 16 dgisselq
                // Wishbone inputs
84 2 dgisselq
                i_wb_cyc, i_wb_stb, i_wb_we, i_wb_addr, i_wb_data,
85 16 dgisselq
                        i_wb_sel,
86
                // Wishbone outputs
87
                o_wb_ack, o_wb_stall, o_wb_data,
88 17 dgisselq
                // Memory command wires
89 16 dgisselq
                o_ddr_reset_n, o_ddr_cke, o_ddr_bus_oe,
90 18 dgisselq
                o_ddr_cmd_a, o_ddr_cmd_b, o_ddr_cmd_c, o_ddr_cmd_d,
91 17 dgisselq
                // And the data wires to go with them ....
92 18 dgisselq
                o_ddr_data, i_ddr_data, o_bus);
93 16 dgisselq
        // These parameters are not really meant for adjusting from the
94
        // top level.  These are more internal variables, recorded here
95
        // so that things can be automatically adjusted without much
96
        // problem.
97 18 dgisselq
        parameter       CKRP = 0;
98
        parameter       BUSNOW = 2, BUSREG = BUSNOW-1;
99 16 dgisselq
        // The commands (above) include (in this order):
100
        //      o_ddr_cs_n, o_ddr_ras_n, o_ddr_cas_n, o_ddr_we_n,
101
        //      o_ddr_dqs, o_ddr_dm, o_ddr_odt
102
        input           i_clk,  // *MUST* be at 200 MHz for this to work
103
                        i_reset;
104 2 dgisselq
        // Wishbone inputs
105 18 dgisselq
        input                   i_wb_cyc, i_wb_stb, i_wb_we;
106
        // The bus address needs to identify a single 128-bit word of interest
107
        input   [23:0]           i_wb_addr;
108
        input   [127:0]          i_wb_data;
109
        input   [15:0]           i_wb_sel;
110 16 dgisselq
        // Wishbone responses/outputs
111
        output  reg             o_wb_ack, o_wb_stall;
112 18 dgisselq
        output  reg     [127:0]  o_wb_data;
113 16 dgisselq
        // DDR memory command wires
114 18 dgisselq
        output  reg             o_ddr_reset_n, o_ddr_cke;
115
        output  reg     [1:0]    o_ddr_bus_oe;
116 16 dgisselq
        // CMDs are:
117
        //       4 bits of CS, RAS, CAS, WE
118
        //       3 bits of bank
119
        //      14 bits of Address
120
        //       1 bit  of DQS (strobe active, or not)
121
        //       4 bits of mask (one per byte)
122
        //       1 bit  of ODT
123
        //      ----
124
        //      27 bits total
125 18 dgisselq
        output  wire    [26:0]   o_ddr_cmd_a, o_ddr_cmd_b,
126
                                o_ddr_cmd_c, o_ddr_cmd_d;
127
        output  reg     [127:0]  o_ddr_data;
128
        input           [127:0]  i_ddr_data;
129
        output  reg             o_bus;
130
        reg             [2:0]    cmd_pipe;
131
        reg             [1:0]    nxt_pipe;
132 2 dgisselq
 
133 18 dgisselq
        always @(posedge i_clk)
134
                o_bus <= (i_wb_cyc)&&(i_wb_stb)&&(!o_wb_stall);
135 3 dgisselq
 
136 18 dgisselq
 
137 16 dgisselq
//////////
138 2 dgisselq
//
139
//
140 16 dgisselq
//      Reset Logic
141 2 dgisselq
//
142
//
143 16 dgisselq
//////////
144
//
145
//
146 2 dgisselq
// Reset logic should be simple, and is given as follows:
147
// note that it depends upon a ROM memory, reset_mem, and an address into that
148
// memory: reset_address.  Each memory location provides either a "command" to
149
// the DDR3 SDRAM, or a timer to wait until the next command.  Further, the
150
// timer commands indicate whether or not the command during the timer is to
151
// be set to idle, or whether the command is instead left as it was.
152 9 dgisselq
        reg             reset_override, reset_ztimer, maintenance_override;
153 18 dgisselq
        reg     [3:0]    reset_address;
154
        reg     [(`DDR_CMDLEN-1):0]      reset_cmd, cmd_a, cmd_b, cmd_c, cmd_d,
155
                                        refresh_cmd, maintenance_cmd;
156 5 dgisselq
        reg     [24:0]   reset_instruction;
157 3 dgisselq
        reg     [16:0]   reset_timer;
158
        initial reset_override = 1'b1;
159 18 dgisselq
        initial reset_address  = 4'h0;
160 2 dgisselq
        always @(posedge i_clk)
161
                if (i_reset)
162
                begin
163
                        reset_override <= 1'b1;
164 5 dgisselq
                        reset_cmd <= { `DDR_NOOP, reset_instruction[16:0]};
165 18 dgisselq
                end else if ((reset_ztimer)&&(reset_override))
166 5 dgisselq
                begin
167
                        if (reset_instruction[`DDR_RSTDONE])
168
                                reset_override <= 1'b0;
169
                        reset_cmd <= reset_instruction[20:0];
170
                end
171 2 dgisselq
 
172 4 dgisselq
        initial reset_ztimer = 1'b0;    // Is the timer zero?
173 5 dgisselq
        initial reset_timer = 17'h02;
174 2 dgisselq
        always @(posedge i_clk)
175
                if (i_reset)
176
                begin
177
                        reset_ztimer <= 1'b0;
178 5 dgisselq
                        reset_timer <= 17'd2;
179 2 dgisselq
                end else if (!reset_ztimer)
180
                begin
181
                        reset_ztimer <= (reset_timer == 17'h01);
182
                        reset_timer <= reset_timer - 17'h01;
183
                end else if (reset_instruction[`DDR_RSTTIMER])
184
                begin
185
                        reset_ztimer <= 1'b0;
186
                        reset_timer <= reset_instruction[16:0];
187
                end
188
 
189 16 dgisselq
        wire    [16:0]   w_ckXPR, w_ckRFC_first;
190
        wire    [13:0]   w_MR0, w_MR1, w_MR2;
191 18 dgisselq
        assign w_MR0 = 14'h0210;
192 16 dgisselq
        assign w_MR1 = 14'h0044;
193
        assign w_MR2 = 14'h0040;
194 18 dgisselq
        assign w_ckXPR = 17'd12;  // Table 68, p186: 56 nCK / 4 sys clks= 14(-2)
195
        assign  w_ckRFC_first = 17'd11; // i.e. 52 nCK, or ckREFI
196 2 dgisselq
        always @(posedge i_clk)
197 16 dgisselq
                // DONE, TIMER, RESET, CKE, 
198 4 dgisselq
                if (i_reset)
199 5 dgisselq
                        reset_instruction <= { 4'h4, `DDR_NOOP, 17'd40_000 };
200
                else if (reset_ztimer) case(reset_address) // RSTDONE, TIMER, CKE, ??
201 18 dgisselq
                // 1. Reset asserted (active low) for 200 us. (@80MHz)
202
                4'h0: reset_instruction <= { 4'h4, `DDR_NOOP, 17'd16_000 };
203 4 dgisselq
                // 2. Reset de-asserted, wait 500 us before asserting CKE
204 18 dgisselq
                4'h1: reset_instruction <= { 4'h6, `DDR_NOOP, 17'd40_000 };
205 4 dgisselq
                // 3. Assert CKE, wait minimum of Reset CKE Exit time
206 18 dgisselq
                4'h2: reset_instruction <= { 4'h7, `DDR_NOOP, w_ckXPR };
207 16 dgisselq
                // 4. Set MR2.  (4 nCK, no TIMER, but needs a NOOP cycle)
208 18 dgisselq
                4'h3: reset_instruction <= { 4'h3, `DDR_MRSET, 3'h2, w_MR2 };
209 16 dgisselq
                // 5. Set MR1.  (4 nCK, no TIMER, but needs a NOOP cycle)
210 18 dgisselq
                4'h4: reset_instruction <= { 4'h3, `DDR_MRSET, 3'h1, w_MR1 };
211 16 dgisselq
                // 6. Set MR0
212 18 dgisselq
                4'h5: reset_instruction <= { 4'h3, `DDR_MRSET, 3'h0, w_MR0 };
213
                // 7. Wait 12 nCK clocks, or 3 sys clocks
214
                4'h6: reset_instruction <= { 4'h7, `DDR_NOOP,  17'd1 };
215 16 dgisselq
                // 8. Issue a ZQCL command to start ZQ calibration, A10 is high
216 18 dgisselq
                4'h7: reset_instruction <= { 4'h3, `DDR_ZQS, 6'h0, 1'b1, 10'h0};
217 16 dgisselq
                //11.Wait for both tDLLK and tZQinit completed, both are
218
                // 512 cks. Of course, since every one of these commands takes
219 18 dgisselq
                // two clocks, we wait for one quarter as many clocks (minus
220
                // two for our timer logic)
221
                4'h8: reset_instruction <= { 4'h7, `DDR_NOOP, 17'd126 };
222 4 dgisselq
                // 12. Precharge all command
223 18 dgisselq
                4'h9: reset_instruction <= { 4'h3, `DDR_PRECHARGE, 6'h0, 1'b1, 10'h0 };
224
                // 13. Wait 5 memory clocks (8 memory clocks) for the precharge
225
                // to complete.  A single NOOP here will have us waiting
226
                // 8 clocks, so we should be good here.
227
                4'ha: reset_instruction <= { 4'h3, `DDR_NOOP, 17'd0 };
228 4 dgisselq
                // 14. A single Auto Refresh commands
229 18 dgisselq
                4'hb: reset_instruction <= { 4'h3, `DDR_REFRESH, 17'h00 };
230 4 dgisselq
                // 15. Wait for the auto refresh to complete
231 18 dgisselq
                4'hc: reset_instruction <= { 4'h7, `DDR_NOOP, w_ckRFC_first };
232
                4'hd: reset_instruction <= { 4'h7, `DDR_NOOP, 17'd3 };
233 2 dgisselq
                default:
234 5 dgisselq
                        reset_instruction <={4'hb, `DDR_NOOP, 17'd00_000 };
235 2 dgisselq
                endcase
236
 
237 18 dgisselq
        initial reset_address = 4'h0;
238 2 dgisselq
        always @(posedge i_clk)
239
                if (i_reset)
240 18 dgisselq
                        reset_address <= 4'h0;
241
                else if ((reset_ztimer)&&(reset_override)&&(!reset_instruction[`DDR_RSTDONE]))
242
                        reset_address <= reset_address + 4'h1;
243 16 dgisselq
 
244
//////////
245 2 dgisselq
//
246 16 dgisselq
//
247
//      Refresh Logic
248
//
249
//
250
//////////
251
//
252
//
253
//
254
// Okay, let's investigate when we need to do a refresh.  Our plan will be to
255
// do a single refreshes every tREFI seconds.  We will not push off refreshes,
256
// nor pull them in--for simplicity.  tREFI = 7.8us, but it is a parameter
257 18 dgisselq
// in the number of clocks (2496 nCK).  In our case, 7.8us / 12.5ns = 624 clocks
258
// (not nCK!)
259 16 dgisselq
//
260
// Note that 160ns are needed between refresh commands (JEDEC, p172), or
261 18 dgisselq
// 52 clocks @320MHz.  After this time, no more refreshes will be needed for
262
// (2496-52) clocks (@ 320 MHz), or (624-13) clocks (@80MHz).
263 16 dgisselq
//
264
// This logic is very similar to the refresh logic, both use a memory as a 
265
// script.
266
//
267 18 dgisselq
        reg             need_refresh, pre_refresh_stall;
268 16 dgisselq
        reg             refresh_ztimer;
269
        reg     [16:0]   refresh_counter;
270
        reg     [2:0]    refresh_addr;
271 18 dgisselq
        reg     [24:0]   refresh_instruction;
272 16 dgisselq
        always @(posedge i_clk)
273
                if (reset_override)
274 18 dgisselq
                        refresh_addr <= 3'h0;
275 16 dgisselq
                else if (refresh_ztimer)
276
                        refresh_addr <= refresh_addr + 3'h1;
277
                else if (refresh_instruction[`DDR_RFBEGIN])
278
                        refresh_addr <= 3'h0;
279 2 dgisselq
 
280 16 dgisselq
        always @(posedge i_clk)
281
                if (reset_override)
282
                begin
283 18 dgisselq
                        refresh_ztimer <= 1'b0;
284
                        refresh_counter <= 17'd4;
285 16 dgisselq
                end else if (!refresh_ztimer)
286
                begin
287
                        refresh_ztimer <= (refresh_counter == 17'h1);
288
                        refresh_counter <= (refresh_counter - 17'h1);
289
                end else if (refresh_instruction[`DDR_RFTIMER])
290
                begin
291
                        refresh_ztimer <= 1'b0;
292
                        refresh_counter <= refresh_instruction[16:0];
293
                end
294 2 dgisselq
 
295 16 dgisselq
        wire    [16:0]   w_ckREFI;
296
        assign  w_ckREFI = 17'd1560; // == 6240/4
297
 
298
        wire    [16:0]   w_ckREFI_left, w_ckRFC_nxt, w_wait_for_idle,
299 18 dgisselq
                        w_pre_stall_counts;
300 16 dgisselq
 
301
        // We need to wait for the bus to become idle from whatever state
302
        // it is in.  The difficult time for this measurement is assuming
303
        // a write was just given.  In that case, we need to wait for the
304
        // write to complete, and then to wait an additional tWR (write
305
        // recovery time) or 6 nCK clocks from the end of the write.  This
306
        // works out to seven idle bus cycles from the time of the write
307
        // command, or a count of 5 (7-2).
308 18 dgisselq
        assign  w_pre_stall_counts = 17'd3;     //
309
        assign  w_wait_for_idle = 17'd0;        //
310
        assign  w_ckREFI_left[16:0] = 17'd624    // The full interval
311
                                -17'd13  // Minus what we've already waited
312 16 dgisselq
                                -w_wait_for_idle
313 18 dgisselq
                                -17'd19;
314
        assign  w_ckRFC_nxt[16:0] = 17'd12-17'd3;
315 16 dgisselq
 
316
        always @(posedge i_clk)
317 18 dgisselq
        if (reset_override)
318
                refresh_instruction <= { 4'h2, `DDR_NOOP, 17'd1 };
319
        else if (refresh_ztimer)
320 16 dgisselq
                case(refresh_addr)//NEED-REFRESH, HAVE-TIMER, BEGIN(start-over)
321
                // First, a number of clocks needing no refresh
322 18 dgisselq
                3'h0: refresh_instruction <= { 4'h2, `DDR_NOOP, w_ckREFI_left };
323 16 dgisselq
                // Then, we take command of the bus and wait for it to be
324
                // guaranteed idle
325 18 dgisselq
                3'h1: refresh_instruction <= { 4'ha, `DDR_NOOP, w_pre_stall_counts };
326
                3'h2: refresh_instruction <= { 4'hc, `DDR_NOOP, w_wait_for_idle };
327 16 dgisselq
                // Once the bus is idle, all commands complete, and a minimum
328
                // recovery time given, we can issue a precharge all command
329 18 dgisselq
                3'h3: refresh_instruction <= { 4'hc, `DDR_PRECHARGE, 17'h0400 };
330 16 dgisselq
                // Now we need to wait tRP = 3 clocks (6 nCK)
331 18 dgisselq
                3'h4: refresh_instruction <= { 4'hc, `DDR_NOOP, 17'h00 };
332
                3'h5: refresh_instruction <= { 4'hc, `DDR_REFRESH, 17'h00 };
333
                3'h6: refresh_instruction <= { 4'he, `DDR_NOOP, w_ckRFC_nxt };
334
                3'h7: refresh_instruction <= { 4'h2, `DDR_NOOP, 17'd12 };
335
                // default:
336
                        // refresh_instruction <= { 4'h1, `DDR_NOOP, 17'h00 };
337 16 dgisselq
                endcase
338
 
339
        // Note that we don't need to check if (reset_override) here since
340
        // refresh_ztimer will always be true if (reset_override)--in other
341
        // words, it will be true for many, many, clocks--enough for this
342
        // logic to settle out.
343
        always @(posedge i_clk)
344
                if (refresh_ztimer)
345
                        refresh_cmd <= refresh_instruction[20:0];
346
        always @(posedge i_clk)
347
                if (refresh_ztimer)
348
                        need_refresh <= refresh_instruction[`DDR_NEEDREFRESH];
349 18 dgisselq
        always @(posedge i_clk)
350
                if (refresh_ztimer)
351
                        pre_refresh_stall <= refresh_instruction[`DDR_PREREFRESH_STALL];
352 16 dgisselq
 
353
 
354
 
355
        reg     [1:0]    drive_dqs;
356
        // Our chosen timing doesn't require any more resolution than one
357
        // bus clock for ODT.  (Of course, this really isn't necessary, since
358
        // we aren't using ODT as per the MRx registers ... but we keep it
359
        // around in case we change our minds later.)
360 18 dgisselq
        reg     [15:0]   ddr_dm;
361 16 dgisselq
 
362
        // The pending transaction
363 18 dgisselq
        reg     [127:0]  r_data;
364 16 dgisselq
        reg             r_pending, r_we;
365
        reg     [13:0]   r_row;
366
        reg     [2:0]    r_bank;
367
        reg     [9:0]    r_col;
368 18 dgisselq
        reg     [15:0]   r_sel;
369 16 dgisselq
 
370
        // The pending transaction, one further into the pipeline.  This is
371
        // the stage where the read/write command is actually given to the
372
        // interface if we haven't stalled.
373 18 dgisselq
        reg     [127:0]  s_data;
374
        reg             s_pending, s_we;
375 16 dgisselq
        reg     [13:0]   s_row, s_nxt_row;
376
        reg     [2:0]    s_bank, s_nxt_bank;
377
        reg     [9:0]    s_col;
378 18 dgisselq
        reg     [15:0]   s_sel;
379 16 dgisselq
 
380
        // Can we preload the next bank?
381
        reg     [13:0]   r_nxt_row;
382
        reg     [2:0]    r_nxt_bank;
383
 
384
 
385
//////////
386 2 dgisselq
//
387
//
388 16 dgisselq
//      Open Banks
389
//
390
//
391
//////////
392
//
393
//
394
//
395 2 dgisselq
// Let's keep track of any open banks.  There are 8 of them to keep track of.
396
//
397 18 dgisselq
//      A precharge requires 1 clocks at 80MHz to complete.
398
//      An activate also requires 1 clocks at 80MHz to complete.
399
//      By the time we log these, they will be complete.
400 16 dgisselq
//      Precharges are not allowed until the maximum of:
401
//              2 clocks (200 MHz) after a read command
402 18 dgisselq
//              4 clocks after a write command
403 2 dgisselq
//
404
//
405 3 dgisselq
        wire    w_precharge_all;
406 16 dgisselq
        reg     [CKRP:0] bank_status     [0:7];
407 6 dgisselq
        reg     [13:0]   bank_address    [0:7];
408
 
409 2 dgisselq
        always @(posedge i_clk)
410
        begin
411 18 dgisselq
                if (cmd_pipe[0])
412
                begin
413
                        bank_status[s_bank] <= 1'b0;
414
                        if (nxt_pipe[1])
415
                                bank_status[s_nxt_bank] <= 1'b1;
416
                end else begin
417
                        if (cmd_pipe[1])
418
                                bank_status[s_bank] <= 1'b1;
419
                        else if (nxt_pipe[1])
420
                                bank_status[s_nxt_bank] <= 1'b1;
421
                        if (nxt_pipe[0])
422
                                bank_status[s_nxt_bank] <= 1'b0;
423
                end
424
 
425 14 dgisselq
                if (maintenance_override)
426 2 dgisselq
                begin
427 18 dgisselq
                        bank_status[0] <= 1'b0;
428
                        bank_status[1] <= 1'b0;
429
                        bank_status[2] <= 1'b0;
430
                        bank_status[3] <= 1'b0;
431
                        bank_status[4] <= 1'b0;
432
                        bank_status[5] <= 1'b0;
433
                        bank_status[6] <= 1'b0;
434
                        bank_status[7] <= 1'b0;
435 2 dgisselq
                end
436 18 dgisselq
 
437 2 dgisselq
        end
438
 
439
        always @(posedge i_clk)
440 18 dgisselq
                if (cmd_pipe[1])
441
                        bank_address[s_bank] <= s_row;
442
                else if (nxt_pipe[1])
443
                        bank_address[s_nxt_bank] <= s_nxt_row;
444 2 dgisselq
 
445 16 dgisselq
 
446
//////////
447 2 dgisselq
//
448
//
449 16 dgisselq
//      Data BUS information
450 2 dgisselq
//
451
//
452 16 dgisselq
//////////
453 2 dgisselq
//
454
//
455 16 dgisselq
//      Our purpose here is to keep track of when the data bus will be
456
//      active.  This is separate from the FIFO which will contain the
457
//      data to be placed on the bus (when so placed), in that this is
458
//      a group of shift registers--every position has a location in time,
459
//      and time always moves forward.  The FIFO, on the other hand, only
460
//      moves forward when data moves onto the bus.
461 2 dgisselq
//
462
//
463 16 dgisselq
 
464 18 dgisselq
        reg     [BUSNOW:0]       bus_active, bus_read, bus_ack;
465 3 dgisselq
        initial bus_active = 0;
466 14 dgisselq
        initial bus_ack = 0;
467 2 dgisselq
        always @(posedge i_clk)
468
        begin
469 16 dgisselq
                bus_active[BUSNOW:0] <= { bus_active[(BUSNOW-1):0], 1'b0 };
470
                // Drive the d-bus?
471
                bus_read[BUSNOW:0]   <= { bus_read[(BUSNOW-1):0], 1'b0 };
472 13 dgisselq
                // Will this position on the bus get a wishbone acknowledgement?
473 16 dgisselq
                bus_ack[BUSNOW:0]   <= { bus_ack[(BUSNOW-1):0], 1'b0 };
474 13 dgisselq
 
475 18 dgisselq
                if (cmd_pipe[2])
476 2 dgisselq
                begin
477 18 dgisselq
                        bus_active[0]<= 1'b1; // Data transfers in one clocks
478
                        bus_ack[0] <= 1'b1;
479
                        bus_ack[0] <= 1'b1;
480 4 dgisselq
 
481 19 dgisselq
                        bus_read[0] <= !s_we;
482 2 dgisselq
                end
483
        end
484
 
485
//
486
//
487
// Now, let's see, can we issue a read command?
488
//
489
//
490 18 dgisselq
        wire    pre_valid;
491
        assign  pre_valid = !maintenance_override;
492 14 dgisselq
 
493 18 dgisselq
        reg     pipe_stall;
494 14 dgisselq
 
495 2 dgisselq
        always @(posedge i_clk)
496
        begin
497 9 dgisselq
                r_pending <= (i_wb_stb)&&(~o_wb_stall)
498
                                ||(r_pending)&&(pipe_stall);
499
                if (~pipe_stall)
500
                        s_pending <= r_pending;
501
                if (~pipe_stall)
502 2 dgisselq
                begin
503 18 dgisselq
                        if (r_pending)
504
                        begin
505
                                pipe_stall <= 1'b1;
506
                                o_wb_stall <= 1'b1;
507
                                if (!bank_status[r_bank][0])
508
                                        cmd_pipe <= 3'b010;
509
                                else if (bank_address[r_bank] != r_row)
510
                                        cmd_pipe <= 3'b001; // Read in two clocks
511
                                else begin
512
                                        cmd_pipe <= 3'b100; // Read now
513
                                        pipe_stall <= 1'b0;
514
                                        o_wb_stall <= 1'b0;
515
                                end
516
 
517
                                if (!bank_status[r_nxt_bank][0])
518
                                        nxt_pipe <= 2'b10;
519
                                else if (bank_address[r_nxt_bank] != r_row)
520
                                        nxt_pipe <= 2'b01; // Read in two clocks
521
                                else
522
                                        nxt_pipe <= 2'b00; // Next is ready
523
                                if (nxt_pipe[1])
524
                                        nxt_pipe[1] <= 1'b0;
525
                        end else begin
526
                                cmd_pipe <= 3'b000;
527
                                nxt_pipe <= { nxt_pipe[0], 1'b0 };
528
                                pipe_stall <= 1'b0;
529
                                o_wb_stall <= 1'b0;
530
                        end
531 9 dgisselq
                end else begin // if (pipe_stall)
532 18 dgisselq
                        pipe_stall <= (s_pending)&&(cmd_pipe[0]);
533
                        o_wb_stall <= (s_pending)&&(cmd_pipe[0]);
534
                        cmd_pipe <= { cmd_pipe[1:0], 1'b0 };
535
 
536
                        nxt_pipe[0] <= (cmd_pipe[0])&&(nxt_pipe[0]);
537
                        nxt_pipe[1] <= ((cmd_pipe[0])&&(nxt_pipe[0])) ? 1'b0
538
                                        : ((cmd_pipe[1])?(|nxt_pipe[1:0]) : nxt_pipe[0]);
539 9 dgisselq
                end
540 18 dgisselq
                if (pre_refresh_stall)
541 2 dgisselq
                        o_wb_stall <= 1'b1;
542
 
543 9 dgisselq
                if (~pipe_stall)
544 2 dgisselq
                begin
545
                        r_we   <= i_wb_we;
546
                        r_data <= i_wb_data;
547 18 dgisselq
                        r_row  <= i_wb_addr[23:10]; // 14 bits row address
548
                        r_bank <= i_wb_addr[9:7];
549
                        r_col  <= { i_wb_addr[6:0], 3'b000 }; // 10 bits Caddr
550 16 dgisselq
                        r_sel  <= i_wb_sel;
551 2 dgisselq
 
552 16 dgisselq
// i_wb_addr[0] is the  8-bit      byte selector of  16-bits (ignored)
553
// i_wb_addr[1] is the 16-bit half-word selector of  32-bits (ignored)
554
// i_wb_addr[2] is the 32-bit      word selector of  64-bits (ignored)
555
// i_wb_addr[3] is the 64-bit long word selector of 128-bits
556
 
557 2 dgisselq
                        // pre-emptive work
558 18 dgisselq
                        r_nxt_row  <= (i_wb_addr[9:7]==3'h7)
559
                                        ? (i_wb_addr[23:10]+14'h1)
560
                                        : i_wb_addr[23:10];
561
                        r_nxt_bank <= i_wb_addr[9:7]+3'h1;
562 2 dgisselq
                end
563 9 dgisselq
 
564
                if (~pipe_stall)
565
                begin
566
                        // Moving one down the pipeline
567
                        s_we   <= r_we;
568
                        s_data <= r_data;
569
                        s_row  <= r_row;
570
                        s_bank <= r_bank;
571
                        s_col  <= r_col;
572 18 dgisselq
                        s_sel  <= (r_we)?(~r_sel):16'h00;
573 9 dgisselq
 
574
                        // pre-emptive work
575
                        s_nxt_row  <= r_nxt_row;
576
                        s_nxt_bank <= r_nxt_bank;
577
                end
578 2 dgisselq
        end
579
 
580 3 dgisselq
 
581 2 dgisselq
//
582
//
583
// Okay, let's look at the last assignment in our chain.  It should look
584
// something like:
585
        always @(posedge i_clk)
586 4 dgisselq
                if (i_reset)
587
                        o_ddr_reset_n <= 1'b0;
588
                else if (reset_ztimer)
589
                        o_ddr_reset_n <= reset_instruction[`DDR_RSTBIT];
590 2 dgisselq
        always @(posedge i_clk)
591 4 dgisselq
                if (i_reset)
592
                        o_ddr_cke <= 1'b0;
593
                else if (reset_ztimer)
594
                        o_ddr_cke <= reset_instruction[`DDR_CKEBIT];
595 6 dgisselq
 
596 9 dgisselq
        always @(posedge i_clk)
597
                if (i_reset)
598
                        maintenance_override <= 1'b1;
599
                else
600
                        maintenance_override <= (reset_override)||(need_refresh);
601 7 dgisselq
 
602 9 dgisselq
        initial maintenance_cmd = { `DDR_NOOP, 17'h00 };
603
        always @(posedge i_clk)
604
                if (i_reset)
605
                        maintenance_cmd <= { `DDR_NOOP, 17'h00 };
606
                else
607
                        maintenance_cmd <= (reset_override)?reset_cmd:refresh_cmd;
608
 
609 18 dgisselq
 
610 2 dgisselq
        always @(posedge i_clk)
611
        begin
612 18 dgisselq
                // We run our commands by timeslots, A, B, C, and D in that
613
                // order.
614
 
615
                // Timeslot A always contains any maintenance commands we might
616
                //       have.
617
                // Timeslot B always contains any precharge command, excluding
618
                //      the maintenance precharge-all command.
619
                // Timeslot C always contains any activate command
620
                // Timeslot D always contains any read/write command
621
                //
622
                // We can always set these commands to whatever, to reduce the
623
                // used logic, as long as the top bit (CS_N) is used to select
624
                // whether or not the command is active.  If CS_N is 0 the
625
                // command will be applied by the chip, if 1 the command turns
626
                // into a deselect command that the chip will ignore.
627
                //
628
                cmd_a <= maintenance_cmd;
629
 
630
                cmd_b <= { `DDR_PRECHARGE, s_nxt_bank, s_nxt_row[13:11], 1'b0, s_nxt_row[9:0] };
631 19 dgisselq
                cmd_b[`DDR_CSBIT] <= 1'b1; // Deactivate, unless ...
632 18 dgisselq
                if (cmd_pipe[0])
633
                        cmd_b <= { `DDR_PRECHARGE, s_bank, s_row[13:11], 1'b0, s_row[9:0] };
634 19 dgisselq
                cmd_b[`DDR_CSBIT] <= (!cmd_pipe[0])&&(!nxt_pipe[0]);
635 18 dgisselq
 
636
                cmd_c <= { `DDR_ACTIVATE, s_nxt_bank, s_nxt_row[13:11], 1'b0, s_nxt_row[9:0] };
637 19 dgisselq
                cmd_c[`DDR_CSBIT] <= 1'b1; // Disable command, unless ...
638 18 dgisselq
                if (cmd_pipe[1])
639 19 dgisselq
                        cmd_c <= { `DDR_ACTIVATE, s_bank, s_row[13:0] };
640 18 dgisselq
                else if (nxt_pipe[1])
641 19 dgisselq
                        cmd_c[`DDR_CSBIT] <= 1'b0;
642 18 dgisselq
 
643
                if (cmd_pipe[2])
644
                begin
645
                        cmd_d[`DDR_CSBIT:`DDR_WEBIT] <= (s_we)?`DDR_WRITE:`DDR_READ;
646
                        cmd_d[(`DDR_WEBIT-1):0] <= { s_bank, 3'h0, 1'b0, s_col };
647
                end
648 19 dgisselq
                cmd_d[`DDR_CSBIT] <= !(cmd_pipe[2]);
649 18 dgisselq
 
650
 
651
                // Now, if the maintenance mode must override whatever we are
652
                // doing, we only need to apply this more complicated logic
653
                // to the CS_N bit, or bit[20], since this will activate or
654
                // deactivate the rest of the command--making the rest
655
                // either relevant (CS_N=0) or irrelevant (CS_N=1) as we need.
656 16 dgisselq
                if (maintenance_override)
657 18 dgisselq
                begin // Over-ride all commands.  Make them deselect commands,
658
                        // save for the maintenance timeslot.
659 19 dgisselq
                        cmd_a[`DDR_CSBIT] <= 1'b0;
660
                        cmd_b[`DDR_CSBIT] <= 1'b1;
661
                        cmd_c[`DDR_CSBIT] <= 1'b1;
662
                        cmd_d[`DDR_CSBIT] <= 1'b1;
663 2 dgisselq
                end else
664 19 dgisselq
                        cmd_a[`DDR_CSBIT] <= 1'b1; // Disable maintenance timeslot
665 2 dgisselq
        end
666
 
667 18 dgisselq
`define LGFIFOLN        3
668
`define FIFOLEN         8
669 7 dgisselq
        reg     [(`LGFIFOLN-1):0]        bus_fifo_head, bus_fifo_tail;
670 18 dgisselq
        reg     [127:0]  bus_fifo_data   [0:(`FIFOLEN-1)];
671
        reg     [15:0]   bus_fifo_sel    [0:(`FIFOLEN-1)];
672 7 dgisselq
        reg             pre_ack;
673 3 dgisselq
 
674 7 dgisselq
        // The bus R/W FIFO
675
        wire    w_bus_fifo_read_next_transaction;
676 16 dgisselq
        assign  w_bus_fifo_read_next_transaction = (bus_ack[BUSREG]);
677 7 dgisselq
        always @(posedge i_clk)
678
        begin
679
                pre_ack <= 1'b0;
680 13 dgisselq
                if (reset_override)
681 7 dgisselq
                begin
682 13 dgisselq
                        bus_fifo_head <= {(`LGFIFOLN){1'b0}};
683
                        bus_fifo_tail <= {(`LGFIFOLN){1'b0}};
684 7 dgisselq
                end else begin
685 13 dgisselq
                        if ((s_pending)&&(!pipe_stall))
686
                                bus_fifo_head <= bus_fifo_head + 1'b1;
687 7 dgisselq
 
688
                        if (w_bus_fifo_read_next_transaction)
689
                        begin
690 13 dgisselq
                                bus_fifo_tail <= bus_fifo_tail + 1'b1;
691 7 dgisselq
                                pre_ack <= 1'b1;
692
                        end
693
                end
694 9 dgisselq
                bus_fifo_data[bus_fifo_head] <= s_data;
695 16 dgisselq
                bus_fifo_sel[bus_fifo_head] <= s_sel;
696 7 dgisselq
        end
697
 
698
 
699
        always @(posedge i_clk)
700
                o_ddr_data  <= bus_fifo_data[bus_fifo_tail];
701 16 dgisselq
        always @(posedge i_clk)
702
                ddr_dm   <= (bus_ack[BUSREG])? bus_fifo_sel[bus_fifo_tail]
703 18 dgisselq
                        : ((!bus_read[BUSREG])? 16'hffff: 16'h0000);
704 16 dgisselq
        always @(posedge i_clk)
705 18 dgisselq
        begin
706
                drive_dqs[1] <= (bus_active[(BUSREG)])
707
                        &&(!bus_read[(BUSREG)]);
708
                drive_dqs[0] <= (bus_active[BUSREG:(BUSREG-1)] != 2'b00)
709
                        &&(bus_read[BUSREG:(BUSREG-1)] == 2'b00);
710
                //
711
                // Is the strobe on during the last clock?
712
                o_ddr_bus_oe[0] <= (|bus_active[BUSREG:(BUSREG-1)])&&(!bus_read[BUSREG]);
713
                // Is data transmitting the bus throughout?
714
                o_ddr_bus_oe[1] <= (bus_active[BUSREG])&&(!bus_read[BUSREG]);
715
        end
716 2 dgisselq
 
717 18 dgisselq
        // First command
718
        assign  o_ddr_cmd_a = { cmd_a, drive_dqs[1], ddr_dm[15:12], drive_dqs[0] };
719
        // Second command (of four)
720
        assign  o_ddr_cmd_b = { cmd_b, drive_dqs[1], ddr_dm[11: 8], drive_dqs[0] };
721
        // Third command (of four)
722
        assign  o_ddr_cmd_c = { cmd_c, drive_dqs[1], ddr_dm[ 7: 4], drive_dqs[0] };
723
        // Fourth command (of four)--occupies the last timeslot
724
        assign  o_ddr_cmd_d = { cmd_d, drive_dqs[0], ddr_dm[ 3: 0], drive_dqs[0] };
725 2 dgisselq
 
726 16 dgisselq
        assign  w_precharge_all = (cmd_a[`DDR_CSBIT:`DDR_WEBIT]==`DDR_PRECHARGE)
727
                                &&(cmd_a[10]);
728
 
729 13 dgisselq
        always @(posedge i_clk)
730 7 dgisselq
                o_wb_ack <= pre_ack;
731
        always @(posedge i_clk)
732
                o_wb_data <= i_ddr_data;
733 4 dgisselq
 
734 2 dgisselq
endmodule

powered by: WebSVN 2.1.0

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