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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [orpsocv2/] [boards/] [xilinx/] [ml501/] [rtl/] [verilog/] [xilinx_ddr2/] [xilinx_ddr2_if.v] - Blame information for rev 479

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

Line No. Rev Author Line
1 412 julius
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3 479 julius
////  Xilinx ML501 DDR2 controller Wishbone Interface             ////
4 412 julius
////                                                              ////
5
////  Description                                                 ////
6
////  Simple interface to the Xilinx MIG generated DDR2 controller////
7
////                                                              ////
8
////  To Do:                                                      ////
9 479 julius
////   Use full capacity of BRAM                                  ////
10
////   Employ LRU replacement scheme                              ////
11
////   Remove hard-coding of things relating to number of lines   ////
12 412 julius
////                                                              ////
13
////  Author(s):                                                  ////
14
////      - Julius Baxter, julius.baxter@orsoc.se                 ////
15
////                                                              ////
16
////                                                              ////
17
//////////////////////////////////////////////////////////////////////
18
////                                                              ////
19
//// Copyright (C) 2010 Authors and OPENCORES.ORG                 ////
20
////                                                              ////
21
//// This source file may be used and distributed without         ////
22
//// restriction provided that this copyright statement is not    ////
23
//// removed from the file and that any derivative work contains  ////
24
//// the original copyright notice and the associated disclaimer. ////
25
////                                                              ////
26
//// This source file is free software; you can redistribute it   ////
27
//// and/or modify it under the terms of the GNU Lesser General   ////
28
//// Public License as published by the Free Software Foundation; ////
29
//// either version 2.1 of the License, or (at your option) any   ////
30
//// later version.                                               ////
31
////                                                              ////
32
//// This source is distributed in the hope that it will be       ////
33
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
34
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
35
//// PURPOSE.  See the GNU Lesser General Public License for more ////
36
//// details.                                                     ////
37
////                                                              ////
38
//// You should have received a copy of the GNU Lesser General    ////
39
//// Public License along with this source; if not, download it   ////
40
//// from http://www.opencores.org/lgpl.shtml                     ////
41
////                                                              ////
42
//////////////////////////////////////////////////////////////////////
43
/*
44 479 julius
 * This is an interface to the Xilinx MIG-sourced DDR2 controller.
45 412 julius
 *
46 479 julius
 * The controller's interface is via FIFO buffers - one for address and control
47
 * the other is for data. The data FIFO interface is 128-bits wide.
48 412 julius
 *
49 479 julius
 * This module has a cache with different aspects on each port. As we're to
50
 * ultimately interface to a 32-bit wide Wishbone bus, one side is 32-bits
51
 * and the other is 128-bits wide to accommodate the DDR2 controller's data
52
 * path.
53 412 julius
 *
54 479 julius
 * At present, the cache controller doesn't employ associativity, so any
55
 * line can be used for any location. A round-robin approach to line
56
 * use is employed. TODO is LRU scheme instead of round robin.
57 412 julius
 *
58 479 julius
 * The cache is a macro generated by Xilinx's IP generation tool. This is
59
 * because memories with dual-aspect ratios cannot be inferred via HDL.
60 412 julius
 *
61 479 julius
 * The size of lines, as set by the defines, controls how long each read
62
 * and write burst to/from the SDRAM is.
63 412 julius
 *
64 479 julius
 * There are two clock domains - the Wishbone and the DDR2 controller domain.
65 412 julius
 *
66 479 julius
 * A signal is sent to control logic in the DDR2 domain side to load and store
67
 * the contents of a particular line from and to the DDR2 controller's data
68
 * FIFOs. This loading and storing is done at the DDR2 clock domain's rate.
69 412 julius
 *
70 479 julius
 * The writing of address and control data is done from the Wishbone domain.
71
 *
72 412 julius
 * Multi-cycle paths:
73
 * Write:
74
 * To indicate that a writeback is occuring, a system-bus domain (wishbone, in
75
 * this case) signal is set, and then sampled in the controller domain whenever
76
 * a system-bus domain clock edge is detected. This register is "do_writeback"
77
 * and then the controller domain register "ddr2_write_done" is asserted when
78
 * the data has been written out of the RAMs and into the controller's fifos.
79
 * "ddr2_write_done" is then sampled by the system-bus domain and "do_writeback"
80
 * So there are paths between:
81
 * ( register -> (sampled by) -> register )
82 479 julius
 * wb_clk:do_writeback -> ddr2_clk:do_writeback_ddr2
83 412 julius
 * wb_clk:do_writeback -> ddr2_clk:ddr2_write_done
84
 * ddr2_clk:ddr2_write_done -> wb_clk:do_writeback
85
 *
86
 * Read:
87
 * The only signal crossing we have here is the one indicating the read data
88
 * has arrived into the cache RAM from the controller. The controller domain
89
 * register "ddr2_read_done" is set, and sampled in the system-bus domain by the
90
 * logic controlling the "do_readfrom" register. "ddr2_read_done" is cleared
91
 * when the controller domain sees that "do_readfrom" has been de-asserted.
92
 * So there are paths between:
93
 * ( register -> (sampled by) -> register )
94
 * ddr2_clk:ddr2_read_done -> wb_clk:do_readfrom
95
 * wb_clk:do_readfrom -> ddr2_clk:ddr2_read_done
96
 *
97 479 julius
*/
98
module xilinx_ddr2_if2 (
99 412 julius
    input [31:0]       wb_adr_i,
100
    input              wb_stb_i,
101
    input              wb_cyc_i,
102 439 julius
    input [2:0]        wb_cti_i,
103
    input [1:0]        wb_bte_i,
104 412 julius
    input              wb_we_i,
105
    input [3:0]        wb_sel_i,
106
    input [31:0]       wb_dat_i,
107
    output [31:0]      wb_dat_o,
108
    output reg         wb_ack_o,
109 479 julius
 
110 412 julius
    output [12:0]      ddr2_a,
111
    output [1:0]       ddr2_ba,
112
    output             ddr2_ras_n,
113
    output             ddr2_cas_n,
114
    output             ddr2_we_n,
115
    output [1:0]       ddr2_cs_n,
116
    output [1:0]       ddr2_odt,
117
    output [1:0]       ddr2_cke,
118
    output [7:0]       ddr2_dm,
119
 
120
    inout [63:0]       ddr2_dq,
121
    inout [7:0]        ddr2_dqs,
122
    inout [7:0]        ddr2_dqs_n,
123
    output [1:0]       ddr2_ck,
124
    output [1:0]       ddr2_ck_n,
125 479 julius
 
126 412 julius
    input              ddr2_if_clk,
127 479 julius
    input              ddr2_if_rst,
128 412 julius
    input              idly_clk_200,
129 479 julius
 
130 412 julius
    input              wb_clk,
131
    input              wb_rst);
132
 
133
`include "xilinx_ddr2_params.v"
134
 
135 479 julius
   // Define to add a counter, signaling error if the controller locks up
136
   // (no ack after a certain period of time)
137
   //`define ERR_COUNTER
138
 
139
/*
140
`define DDR2_CACHE_NUM_LINES 16
141
`define DDR2_CACHE_NUM_LINES_ENC_WIDTH 4 // log2(`DDR2_CACHE_NUM_LINES)
142
 */
143
`define DDR2_CACHE_NUM_LINES 4
144
`define DDR2_CACHE_NUM_LINES_ENC_WIDTH 2 // log2(`DDR2_CACHE_NUM_LINES)
145
 
146
`define DDR2_CACHE_NUM_WORDS_PER_LINE 256
147
`define DDR2_CACHE_ADDR_WIDTH_WORDS_PER_LINE 8
148
`define DDR2_CACHE_TAG_ADDR_WIDTH (32-`DDR2_CACHE_ADDR_WIDTH_WORDS_PER_LINE-2)
149
 
150
`define DDR2_CACHE_DDR2_SIDE_NUM_WORDS_PER_LINE (`DDR2_CACHE_NUM_WORDS_PER_LINE/4)
151
`define DDR2_CACHE_DDR2_SIDE_ADDR_WIDTH_NUM_WORDS_PER_LINE (`DDR2_CACHE_ADDR_WIDTH_WORDS_PER_LINE - 2)
152
`define DDR2_CACHE_DDR2_SIDE_ADDR_WIDTH (`DDR2_CACHE_NUM_LINES_ENC_WIDTH + `DDR2_CACHE_DDR2_SIDE_ADDR_WIDTH_NUM_WORDS_PER_LINE)
153
 
154
`define DDR2_CACHE_TAG_BITS 31:(`DDR2_CACHE_ADDR_WIDTH_WORDS_PER_LINE+2)
155
 
156 412 julius
   wire                ddr2_clk; // DDR2 iface domain clock.
157
   wire                ddr2_rst; // reset from the ddr2 module
158
 
159
   wire                wb_req;
160
   reg                 wb_req_r;
161
   reg                 wb_ack_o_r;
162
 
163
   wire                wb_req_new;
164
   reg                 wb_req_new_r;
165
 
166 479 julius
   wire                wb_req_addr_hit;
167 412 julius
 
168 479 julius
   wire                cached_addr_valid;
169 412 julius
 
170 479 julius
 
171
   wire [31:(32 -`DDR2_CACHE_TAG_ADDR_WIDTH)] cached_addr;
172 412 julius
 
173 479 julius
`define DDR2_BURST_8_DQ64_ADDR_WIDTH 4 // = log2(burst of 8 64-bits = 16 words)
174
`define DDR2_BURST_4_DQ64_ADDR_WIDTH 3 // = log2(burst of 4 64-bits = 8 words)
175
   // This counts how many addresses we should write to the fifo - the number 
176
   // of discrete FIFO transactions.
177
   reg [`DDR2_CACHE_ADDR_WIDTH_WORDS_PER_LINE-`DDR2_BURST_8_DQ64_ADDR_WIDTH - 1:0] addr_counter;
178
 
179
   wire                cache_write;
180
 
181 412 julius
   wire                cache_hit;
182
 
183
   wire                wb_cache_en;
184
 
185
   reg                 do_writeback, do_writeback_r;
186
   wire                do_writeback_start, do_writeback_finished;
187 479 julius
   // Wire to indicate writing to data FIFO of MIG has completed
188
   wire                do_writeback_data_finished;
189
   // Wire to indicate that address FIFO of MIG should be written to to 
190
   // initiate memory accesses.
191
   reg                 do_writeback_addresses, do_writeback_addresses_r;
192
 
193 412 julius
   reg                 do_readfrom, do_readfrom_r;
194
   wire                do_readfrom_start, do_readfrom_finished;
195
   wire                doing_readfrom;
196
 
197 479 julius
   reg                 do_af_write;
198 412 julius
 
199
   // Domain crossing logic
200
   reg                 wb_clk_r;
201
   reg                 wb_clk_in_ddr2_clk;
202
 
203
   reg                 wb_clk_in_ddr2_clk_r;
204
   wire                wb_clk_edge;
205
   reg [2:0]            ddr2_clk_phase;
206
   // Sample when clk phase is 0
207 479 julius
   reg                 do_writeback_ddr2;
208 412 julius
   reg                 do_writeback_ddr2_fifo_we;
209
   reg                 ddr2_write_done;
210 479 julius
   reg [`DDR2_CACHE_DDR2_SIDE_ADDR_WIDTH_NUM_WORDS_PER_LINE - 1:0] ddr2_cache_line_word_addr;
211 412 julius
   wire [127:0]        ddr2_cache_data_o;
212
   reg                 rd_data_valid_r;
213
   reg                 ddr2_read_done;
214
 
215
   // DDR2 MIG interface wires
216
   wire                app_af_afull;
217
   wire                app_wdf_afull;
218
   wire                app_wdf_wren;
219
   wire                app_af_wren;
220 479 julius
   wire [30:0]          writeback_af_addr;
221
   wire [30:0]          readfrom_af_addr;
222 412 julius
   wire [30:0]          app_af_addr;
223
   wire [2:0]           app_af_cmd;
224 479 julius
 
225 412 julius
   wire [(APPDATA_WIDTH)-1:0] app_wdf_data;
226
   wire [(APPDATA_WIDTH/8)-1:0] app_wdf_mask_data;
227
   wire                         rd_data_valid;
228
   wire [(APPDATA_WIDTH)-1:0]    rd_data_fifo_out;
229
   wire                         phy_init_done;
230
 
231 479 julius
   wire [`DDR2_CACHE_NUM_LINES - 1 :0]   cache_line_addr_validate;
232
   wire [`DDR2_CACHE_NUM_LINES - 1 :0]   cache_line_addr_invalidate;
233
   wire [`DDR2_CACHE_NUM_LINES - 1 :0]   cache_line_addr_valid;
234
   wire [`DDR2_CACHE_NUM_LINES - 1 :0]   cache_line_hit;
235
   wire [`DDR2_CACHE_TAG_BITS]  cache_line_addr [0:`DDR2_CACHE_NUM_LINES-1] ;
236
 
237
   // Cache control signals
238
   // Wishbone side
239
   wire [`DDR2_CACHE_ADDR_WIDTH_WORDS_PER_LINE-1:0] wb_cache_adr;
240
   wire [3:0]                    wb_cache_sel_we;
241
   // DDR side
242
   wire                         ddr2_cache_en;
243
   wire [15:0]                   ddr2_cache_we;
244
 
245
   reg                          wb_bursting; // Indicate if burst is enabled
246
   reg [3:0]                     wb_burst_addr; // Burst counter, up to 16
247
   wire [1:0]                    wb_burst_addr_4beat;
248
   wire [2:0]                    wb_burst_addr_8beat;
249
   wire                         wb_burst_addr_incr;
250
   wire                         ack_err;
251
   reg                          ack_err_r;
252
 
253 412 julius
 
254 479 julius
 
255
   // Decoded select line
256
   wire [`DDR2_CACHE_NUM_LINES-1:0] selected_cache_line;
257
   wire [`DDR2_CACHE_NUM_LINES_ENC_WIDTH-1:0] selected_cache_line_enc;
258
   reg [`DDR2_CACHE_NUM_LINES_ENC_WIDTH-1:0] selected_cache_line_enc_ddr2_clk;
259
 
260
   genvar                                     i;
261
   generate
262
      for (i=0;i<`DDR2_CACHE_NUM_LINES;i=i+1) begin : cache_addr
263
         xilinx_ddr2_wb_if_cache_adr_reg cache_addr_reg_inst
264
           ( .adr_i(wb_adr_i[`DDR2_CACHE_TAG_BITS]),
265
             .validate(cache_line_addr_validate[i]),
266
             .invalidate(cache_line_addr_invalidate[i]),
267
             .cache_hit(cache_line_hit[i]),
268
             .adr_valid(cache_line_addr_valid[i]),
269
             .cached_adr_o(cache_line_addr[i]),
270
             .clk(wb_clk),
271
             .rst(wb_rst));
272
      end
273
   endgenerate
274
 
275
   wire start_writeback, start_fill;
276
 
277
   xilinx_ddr2_wb_if_cache_control xilinx_ddr2_wb_if_cache_control0
278
     (
279
      // Outputs
280
      .start_writeback                  (start_writeback),
281
      .start_fill                       (start_fill),
282
      .cache_line_validate              (cache_line_addr_validate),
283
      .cache_line_invalidate            (cache_line_addr_invalidate),
284
      .selected_cache_line              (selected_cache_line),
285
      .selected_cache_line_enc          (selected_cache_line_enc),
286
      // Inputs
287
      .cache_line_addr_valid            (cache_line_addr_valid),
288
      .cache_line_addr_hit              (cache_line_hit),
289
      .wb_req                           (wb_req),
290
      .cache_write                      (cache_write),
291
      .writeback_done                   (do_writeback_finished),
292
      .fill_done                        (do_readfrom_finished),
293
      .wb_clk                           (wb_clk),
294
      .wb_rst                           (wb_rst));
295
 
296
   defparam xilinx_ddr2_wb_if_cache_control0.num_lines = `DDR2_CACHE_NUM_LINES;
297
   defparam xilinx_ddr2_wb_if_cache_control0.num_lines_log2 = `DDR2_CACHE_NUM_LINES_ENC_WIDTH;
298
 
299
   assign cached_addr = selected_cache_line[0] ? cache_line_addr[0] :
300
                        selected_cache_line[1] ? cache_line_addr[1] :
301
                        selected_cache_line[2] ? cache_line_addr[2] :
302
                        selected_cache_line[3] ? cache_line_addr[3] : 0;
303
 
304
   assign cache_write = wb_req & wb_we_i & wb_ack_o;
305
 
306
   assign cache_hit = |(selected_cache_line & cache_line_hit);
307
 
308
   assign cached_addr_valid = |(selected_cache_line & cache_line_addr_valid);
309
 
310
   assign wb_req_addr_hit = (wb_req & cache_hit & cached_addr_valid);
311
 
312 412 julius
   // Wishbone request detection
313
   assign wb_req = wb_stb_i & wb_cyc_i & phy_init_done;
314
 
315
   always @(posedge wb_clk)
316
     wb_req_r <= wb_req;
317
 
318
   assign wb_req_new = wb_req & !wb_req_r;
319 479 julius
 
320 412 julius
   always @(posedge wb_clk)
321
     wb_req_new_r <= wb_req_new;
322 479 julius
 
323
   always @(posedge wb_clk)
324
     if (wb_rst)
325
       wb_bursting <= 0;
326
   // Reset if acking end of transfer
327
     else if (wb_ack_o && wb_cti_i == 3'b111)
328
       wb_bursting <= 0;
329
   // Set if beginning new transaction and incrementing burst indicated
330
   // TODO - double check if this burst is going to go over a cache line
331
   // boundary - if so don't allow burst, fall back to classic cycles.
332
     else if (wb_req_new)
333
       wb_bursting <= (wb_cti_i == 3'b010);
334
 
335
   // Help constrain additions to appropriate bit-width for wrapping
336
   assign wb_burst_addr_4beat = wb_adr_i[3:2] + 1;
337
   assign wb_burst_addr_8beat = wb_adr_i[4:2] + 1;
338 412 julius
 
339 479 julius
   // Increment burst address whenever we get a hit when reading, or
340
   // when acking and writing.
341
   assign wb_burst_addr_incr = (wb_req_addr_hit & (!wb_we_i |
342
                                                (wb_we_i & wb_ack_o)));
343
 
344
   // Calculate burst address depending on burst type indicator
345 412 julius
   always @(posedge wb_clk)
346
     if (wb_rst)
347 479 julius
       wb_burst_addr <= 0;
348
     else if (wb_req_new)
349
       // When we have a bursting read to an address which is in cache then
350
       // initialise the address to the next word in the burst sequence.
351
       // If it's a miss, or it's a write, then we just take what's on the
352
       // bus.
353
       wb_burst_addr <= !(wb_req_addr_hit & !wb_we_i) ? wb_adr_i[5:2] :
354
                    wb_bte_i==2'b01 ? {wb_adr_i[5:4], wb_burst_addr_4beat }:
355
                    wb_bte_i==2'b10 ? {wb_adr_i[5], wb_burst_addr_8beat }:
356
                    wb_bte_i==2'b11 ? wb_adr_i[5:2] + 1 :
357
                    wb_adr_i[5:2];
358
     else if (wb_burst_addr_incr & wb_bte_i==2'b01)
359
       wb_burst_addr[1:0] <= wb_burst_addr[1:0] + 1;
360
     else if (wb_burst_addr_incr & wb_bte_i==2'b10)
361
       wb_burst_addr[2:0] <= wb_burst_addr[2:0] + 1;
362
     else if (wb_burst_addr_incr & wb_bte_i==2'b11)
363
       wb_burst_addr[3:0] <= wb_burst_addr[3:0] + 1;
364 412 julius
 
365 479 julius
`ifdef ERR_COUNTER
366
   reg [26:0] ack_err_cntr;
367
 
368 412 julius
   always @(posedge wb_clk)
369
     if (wb_rst)
370 479 julius
       ack_err_cntr <= 0;
371
     else if (!wb_req)
372
       ack_err_cntr <= 0;
373
     else if (|ack_err_cntr)
374
       ack_err_cntr <= ack_err_cntr + 1;
375
     else if (wb_req_new & !(|ack_err_cntr))
376
       ack_err_cntr <= 1;
377
 
378
   assign ack_err = (&ack_err_cntr);
379
 
380
   always @(posedge wb_clk)
381
     ack_err_r <= ack_err;
382
 
383
   assign wb_err_o = ack_err_r;
384
 
385
`else // !`ifdef ERR_COUNTER
386
 
387
   assign ack_err = 0;
388
   always @(posedge wb_clk)
389
     ack_err_r <= 0;
390
 
391
   assign wb_err_o = 0;
392
 
393
`endif
394
 
395
   always @(posedge wb_clk)
396
     if (wb_rst)
397 412 julius
       wb_ack_o <= 0;
398
     else
399 479 julius
       wb_ack_o <= wb_req_addr_hit &
400
                   (
401
                    // Simple acks on classic cycles
402
                    (!wb_bursting && !wb_ack_o && !wb_ack_o_r)
403
                    // De-assert ack when we see the final transaction
404
                    || (wb_bursting && !(wb_cti_i==3'b111))
405
                    );
406 412 julius
 
407
   always @(posedge wb_clk)
408
     wb_ack_o_r <= wb_ack_o;
409
 
410 479 julius
   // Writeback/readfrom lower address generation
411 412 julius
   always @(posedge wb_clk)
412
     if (wb_rst)
413 479 julius
       addr_counter <= 0;
414
     else if (app_af_wren)
415
       addr_counter <= addr_counter+1;
416 412 julius
 
417 479 julius
   // Determine if we're writing access requests into DDR2 interface AF
418 412 julius
   always @(posedge wb_clk)
419
     if (wb_rst)
420 479 julius
       do_af_write <= 0;
421
     else if (do_readfrom_start | do_writeback_data_finished)
422
       do_af_write <= 1;
423
     else if ((&addr_counter)) // Stop when counter rolls over
424
       do_af_write <= 0;
425 412 julius
 
426 479 julius
   // Wishbone side of cache enable. Always enabled unless doing DDR2-side
427
   // things (fill or writeback).
428
   assign wb_cache_en = !(do_readfrom | do_writeback);
429 412 julius
 
430 479 julius
 
431 412 julius
   // Writeback detect logic
432
   always @(posedge wb_clk)
433
     if (wb_rst)
434
       do_writeback <= 0;
435
     else if (ddr2_write_done) // DDR2 domain signal
436
       do_writeback <= 0;
437 479 julius
     else if (start_writeback)
438 412 julius
       do_writeback <= 1;
439 479 julius
 
440 412 julius
   always @(posedge wb_clk)
441
     do_writeback_r <= do_writeback;
442 479 julius
 
443
   // Detect falling edge of do_writeback
444
   assign do_writeback_data_finished = !do_writeback & do_writeback_r;
445 412 julius
 
446 479 julius
   always @(posedge wb_clk)
447
     if (wb_rst)
448
       do_writeback_addresses <= 0;
449
     else if (do_writeback_data_finished)
450
       do_writeback_addresses <= 1;
451
     else if ((&addr_counter))
452
       do_writeback_addresses <= 0;
453
 
454
   always @(posedge wb_clk)
455
     do_writeback_addresses_r <= do_writeback_addresses;
456
 
457
   // Detect rising edge of do_writeback
458 412 julius
   assign do_writeback_start = do_writeback & !do_writeback_r;
459 479 julius
   // Detect falling edge of address writing control signal
460
   assign do_writeback_finished = !do_writeback_addresses &
461
                                  do_writeback_addresses_r;
462
 
463 412 julius
   // DDR2 Read detect logic
464
   always @(posedge wb_clk)
465
     if (wb_rst)
466
       do_readfrom <= 0;
467
     else if (ddr2_read_done) // DDR2 domain signal
468
       do_readfrom <= 0;
469 479 julius
     else if (start_fill)
470 412 julius
       do_readfrom <= 1;
471
 
472
   always @(posedge wb_clk)
473
     do_readfrom_r <= do_readfrom;
474
 
475 479 julius
   // Detect line fill request rising edge
476 412 julius
   assign do_readfrom_start = do_readfrom & !do_readfrom_r;
477 479 julius
   // Detect line fill request falling edge
478 412 julius
   assign do_readfrom_finished = !do_readfrom & do_readfrom_r;
479
   assign doing_readfrom = do_readfrom | do_readfrom_r;
480
 
481
   // Address fifo signals
482 479 julius
   assign app_af_wren = (do_readfrom_r | do_writeback_addresses_r) &
483
                        !app_af_afull & do_af_write ;
484
   assign app_af_cmd[0] = do_readfrom; // 1 - read, 0 - write
485 412 julius
   assign app_af_cmd[2:1] = 0;
486 479 julius
 
487
   assign writeback_af_addr = {1'd0, cached_addr, addr_counter, 3'd0};
488 412 julius
 
489 479 julius
   assign readfrom_af_addr = {1'd0, wb_adr_i[`DDR2_CACHE_TAG_BITS],
490
                              addr_counter, 3'd0};
491
 
492
   assign app_af_addr = doing_readfrom ?  readfrom_af_addr : writeback_af_addr;
493 412 julius
   assign app_wdf_wren = do_writeback_ddr2_fifo_we;
494
   assign app_wdf_data = ddr2_cache_data_o;
495
   assign app_wdf_mask_data = 0;
496
 
497 479 julius
   always @(posedge wb_clk)
498
     if (wb_rst) wb_clk_r <= 0; else wb_clk_r <= ~wb_clk_r;
499 412 julius
   always @(posedge ddr2_clk) wb_clk_in_ddr2_clk <= wb_clk_r;
500
   always @(posedge ddr2_clk) wb_clk_in_ddr2_clk_r <= wb_clk_in_ddr2_clk;
501
 
502
   assign wb_clk_edge = wb_clk_in_ddr2_clk & !wb_clk_in_ddr2_clk_r;
503
 
504
   always @(posedge ddr2_clk)
505
     if (ddr2_rst)
506
       ddr2_clk_phase <= 0;
507
     else if (wb_clk_edge)
508
       ddr2_clk_phase <= 0;
509
     else
510
       ddr2_clk_phase <= ddr2_clk_phase + 1;
511
 
512
   always @(posedge ddr2_clk)
513
     if (ddr2_rst)
514 479 julius
       do_writeback_ddr2 <= 0;
515
     else if (&ddr2_cache_line_word_addr)
516
       do_writeback_ddr2 <= 0;
517
     else if (!(|ddr2_clk_phase) & do_writeback & // sample WB domain
518
              !ddr2_write_done)
519
       do_writeback_ddr2 <= 1;
520 412 julius
 
521
   always @(posedge ddr2_clk)
522 479 julius
     if (ddr2_rst)
523
       ddr2_cache_line_word_addr <= 0;
524
     else if (rd_data_valid | (do_writeback_ddr2 & !app_wdf_afull))
525
       ddr2_cache_line_word_addr <= ddr2_cache_line_word_addr + 1;
526
     else if (ddr2_write_done | ddr2_read_done)
527
       ddr2_cache_line_word_addr <= 0;
528
 
529
   always @(posedge ddr2_clk)
530
     do_writeback_ddr2_fifo_we <= (do_writeback_ddr2 & !app_wdf_afull);
531 412 julius
 
532
   always @(posedge ddr2_clk)
533
     if (ddr2_rst)
534
       ddr2_write_done <= 0;
535 479 julius
     else if ((&ddr2_cache_line_word_addr))
536 412 julius
       ddr2_write_done <= 1;
537
     else if ((!(|ddr2_clk_phase)) & !do_writeback) // sample WB domain
538
       ddr2_write_done <= 0;
539 479 julius
 
540 412 julius
   always @(posedge ddr2_clk)
541
     rd_data_valid_r <= rd_data_valid;
542
 
543
   // Read done signaling to WB domain
544
   always @(posedge ddr2_clk)
545
     if (ddr2_rst)
546
       ddr2_read_done <= 0;
547 479 julius
     else if (rd_data_valid_r & (&ddr2_cache_line_word_addr))
548 412 julius
       ddr2_read_done <= 1;
549
     else if (!(|ddr2_clk_phase) & !do_readfrom) // Read WB domain
550
       ddr2_read_done <= 0;
551
 
552 479 julius
   // Lower word address uses potentially bursting address counter
553
   assign wb_cache_adr = wb_bursting ?
554
       {wb_adr_i[(`DDR2_CACHE_ADDR_WIDTH_WORDS_PER_LINE+2)-1:6],wb_burst_addr}:
555
       wb_adr_i[(`DDR2_CACHE_ADDR_WIDTH_WORDS_PER_LINE+2)-1:2];
556
 
557
   assign wb_cache_sel_we = {4{wb_we_i & wb_ack_o}} & wb_sel_i;
558
   assign ddr2_cache_en = (rd_data_valid |do_writeback_ddr2);
559 412 julius
   assign ddr2_cache_we = {16{rd_data_valid}};
560 479 julius
 
561
   always @(posedge ddr2_clk)
562
     if (!(|ddr2_clk_phase)) // Read WB domain 
563
       selected_cache_line_enc_ddr2_clk <= selected_cache_line_enc;
564 412 julius
 
565
 
566 479 julius
 
567
   // Xilinx Coregen true dual-port RAMB
568 412 julius
   // Wishbone side : 32-bit
569
   // DDR2 side : 128-bit
570
   xilinx_ddr2_if_cache cache_mem0
571
     (
572
      // Wishbone side
573
      .clka(wb_clk),
574
      .ena(wb_cache_en),
575
      .wea(wb_cache_sel_we),
576 479 julius
      .addra({2'd0, selected_cache_line_enc,wb_cache_adr}),
577 412 julius
      .dina(wb_dat_i),
578
      .douta(wb_dat_o),
579
 
580
      // DDR2 controller side
581
      .clkb(ddr2_clk),
582
      .enb(ddr2_cache_en),
583
      .web(ddr2_cache_we),
584 479 julius
      .addrb({2'd0, selected_cache_line_enc_ddr2_clk,
585
              ddr2_cache_line_word_addr}),
586 412 julius
      .dinb(rd_data_fifo_out),
587
      .doutb(ddr2_cache_data_o));
588 479 julius
 
589 412 julius
   ddr2_mig #
590
     (
591 479 julius
     .BANK_WIDTH            (BANK_WIDTH),
592
     .CKE_WIDTH             (CKE_WIDTH),
593
     .CLK_WIDTH             (CLK_WIDTH),
594
     .COL_WIDTH             (COL_WIDTH),
595
     .CS_NUM                (CS_NUM),
596
     .CS_WIDTH              (CS_WIDTH),
597
     .CS_BITS               (CS_BITS),
598
     .DM_WIDTH                     (DM_WIDTH),
599
     .DQ_WIDTH              (DQ_WIDTH),
600
     .DQ_PER_DQS            (DQ_PER_DQS),
601
     .DQ_BITS               (DQ_BITS),
602
     .DQS_WIDTH             (DQS_WIDTH),
603
     .DQS_BITS              (DQS_BITS),
604
     .HIGH_PERFORMANCE_MODE (HIGH_PERFORMANCE_MODE),
605
     .ODT_WIDTH             (ODT_WIDTH),
606
     .ROW_WIDTH             (ROW_WIDTH),
607
     .APPDATA_WIDTH         (APPDATA_WIDTH),
608
     .ADDITIVE_LAT          (ADDITIVE_LAT),
609
     .BURST_LEN             (BURST_LEN),
610
     .BURST_TYPE            (BURST_TYPE),
611
     .CAS_LAT               (CAS_LAT),
612
     .ECC_ENABLE            (ECC_ENABLE),
613
     .MULTI_BANK_EN         (MULTI_BANK_EN),
614
     .ODT_TYPE              (ODT_TYPE),
615
     .REDUCE_DRV            (REDUCE_DRV),
616
     .REG_ENABLE            (REG_ENABLE),
617
     .TREFI_NS              (TREFI_NS),
618
     .TRAS                  (TRAS),
619
     .TRCD                  (TRCD),
620
     .TRFC                  (TRFC),
621
     .TRP                   (TRP),
622
     .TRTP                  (TRTP),
623
     .TWR                   (TWR),
624
     .TWTR                  (TWTR),
625
     .SIM_ONLY              (SIM_ONLY),
626
     .RST_ACT_LOW           (RST_ACT_LOW),
627
     .CLK_TYPE                     (CLK_TYPE),
628
     .DLL_FREQ_MODE                (DLL_FREQ_MODE),
629
     .CLK_PERIOD            (CLK_PERIOD)
630
       )
631 412 julius
   ddr2_mig0
632
     (
633 479 julius
     .sys_clk           (ddr2_if_clk),
634
     .idly_clk_200      (idly_clk_200),
635
     .sys_rst_n         (ddr2_if_rst),
636
     .ddr2_ras_n        (ddr2_ras_n),
637
     .ddr2_cas_n        (ddr2_cas_n),
638
     .ddr2_we_n         (ddr2_we_n),
639
     .ddr2_cs_n         (ddr2_cs_n),
640
     .ddr2_cke          (ddr2_cke),
641
     .ddr2_odt          (ddr2_odt),
642
     .ddr2_dm           (ddr2_dm),
643
     .ddr2_dq           (ddr2_dq),
644
     .ddr2_dqs          (ddr2_dqs),
645
     .ddr2_dqs_n        (ddr2_dqs_n),
646
     .ddr2_ck           (ddr2_ck),
647
     .ddr2_ck_n         (ddr2_ck_n),
648
     .ddr2_ba           (ddr2_ba),
649
     .ddr2_a            (ddr2_a),
650
 
651 412 julius
      .clk0_tb           (ddr2_clk),
652
      .rst0_tb           (ddr2_rst),
653 479 julius
      .usr_clk (wb_clk),
654
     .app_af_afull      (app_af_afull),
655
     .app_wdf_afull     (app_wdf_afull),
656
     .rd_data_valid     (rd_data_valid),
657
     .rd_data_fifo_out  (rd_data_fifo_out),
658
     .app_af_wren       (app_af_wren),
659
     .app_af_cmd        (app_af_cmd),
660
     .app_af_addr       (app_af_addr),
661
     .app_wdf_wren      (app_wdf_wren),
662
     .app_wdf_data      (app_wdf_data),
663
     .app_wdf_mask_data (app_wdf_mask_data),
664
     .phy_init_done     (phy_init_done)
665 412 julius
      );
666 479 julius
 
667 412 julius
 
668 479 julius
endmodule // xilinx_ddr2_if2
669
 
670 412 julius
// Local Variables:
671
// verilog-library-directories:("." "ddr2_mig")
672
// verilog-library-extensions:(".v" ".h")
673
// End:
674 479 julius
 
675
 
676
module xilinx_ddr2_wb_if_cache_adr_reg
677
  (adr_i, validate, invalidate,
678
   cached_adr_o, cache_hit, adr_valid,
679
   clk, rst);
680
 
681
   parameter full_adr_width = 32;
682
   parameter word_adr_width = 2; // 4 bytes per word   
683
   parameter line_adr_width = 8; // 256 words per "line"
684
 
685
   parameter tag_width = full_adr_width - line_adr_width - word_adr_width;
686
 
687
 
688
   input [full_adr_width-1: word_adr_width + line_adr_width] adr_i;
689
   input                 validate;
690
   input                 invalidate;
691
   output [full_adr_width-1: word_adr_width + line_adr_width] cached_adr_o;
692
   output                 cache_hit;
693
   output reg             adr_valid;
694
 
695
   input                  clk, rst;
696
 
697
   reg [tag_width-1:0]     cached_adr;
698
 
699
   assign cached_adr_o = cached_adr;
700
 
701
   always @(posedge clk)
702
     if (rst)
703
       cached_adr <= 0;
704
     else if (validate)
705
       cached_adr <= adr_i;
706
 
707
   always @(posedge clk)
708
     if (rst)
709
       adr_valid <= 0;
710
     else if (validate)
711
       adr_valid <= 1;
712
     else if (invalidate)
713
       adr_valid <= 0;
714
 
715
   assign cache_hit = (adr_i == cached_adr);
716
 
717
endmodule // xilinx_ddr2_wb_if_cache_adr_reg
718
 
719
module xilinx_ddr2_wb_if_cache_control
720
  ( cache_line_addr_valid, cache_line_addr_hit,
721
    wb_req,
722
    cache_write,
723
    writeback_done, fill_done,
724
    start_writeback, start_fill,
725
    cache_line_validate, cache_line_invalidate,
726
    selected_cache_line, selected_cache_line_enc,
727
    wb_clk, wb_rst);
728
 
729
   parameter num_lines = 16;
730
   parameter num_lines_log2 = 4;
731
 
732
   input [num_lines-1:0] cache_line_addr_valid;
733
   input [num_lines-1:0] cache_line_addr_hit;
734
 
735
   input                 wb_req;
736
   input                 cache_write;
737
   input                 writeback_done, fill_done;
738
 
739
   output reg            start_writeback;
740
   output reg            start_fill;
741
   output reg [num_lines-1:0] cache_line_validate;
742
   output reg [num_lines-1:0] cache_line_invalidate;
743
 
744
   output [num_lines-1:0] selected_cache_line;
745
   output reg [num_lines_log2-1:0] selected_cache_line_enc;
746
 
747
   input                  wb_clk, wb_rst;
748
 
749
   reg [num_lines-1:0]     dirty;
750
 
751
   reg [num_lines-1:0]     selected_cache_line_from_miss;
752
 
753
   reg                    selected_cache_line_new;
754
 
755
   reg                    invalidate_clean_line;
756
 
757
   reg [num_lines-1:0]     selected_cache_line_r;
758
   reg [num_lines-1:0]     selected_cache_line_r2;
759
 
760
   reg                    wb_req_r;
761
 
762
   wire                   wb_req_new;
763
   reg                    wb_req_new_r;
764
 
765
   always @(posedge wb_clk)
766
     wb_req_r <= wb_req;
767
 
768
   assign wb_req_new = wb_req & !wb_req_r;
769
 
770
   always @(posedge wb_clk)
771
     wb_req_new_r <= wb_req_new;
772
 
773
   // Select a cache line when we miss. Currently very simply is round robin
774
   always @(posedge wb_clk)
775
     if (wb_rst)
776
       selected_cache_line_from_miss <= 1;
777
     else if (wb_req_new_r & !(|selected_cache_line_r)) // miss,no line selected
778
       // Shift select bit one
779
       selected_cache_line_from_miss
780
         <= {selected_cache_line_from_miss[num_lines-2:0],
781
             selected_cache_line_from_miss[num_lines-1]};
782
 
783
 
784
   // Line selection logic, when line address is valid and hit, we select
785
   always @(posedge wb_clk)
786
     if (wb_rst)
787
       selected_cache_line_r <= 0;
788
     else if (wb_req_new)
789
       selected_cache_line_r <= cache_line_addr_valid & cache_line_addr_hit;
790
     else if (wb_req_new_r & !(|selected_cache_line_r))
791
       selected_cache_line_r <= selected_cache_line_from_miss;
792
 
793
   always @(posedge wb_clk)
794
     selected_cache_line_r2 <= selected_cache_line_r;
795
 
796
   assign selected_cache_line = selected_cache_line_r2;
797
 
798
   // A new line of cache has been selected
799
   always @(posedge wb_clk)
800
     if (wb_rst)
801
       selected_cache_line_new <= 0;
802
     else if (wb_req_new & (&(cache_line_addr_valid & cache_line_addr_hit)))
803
       // New line address selected
804
       selected_cache_line_new <= 1;
805
     else if ((!selected_cache_line_new) & wb_req_new_r)
806
       // Didn't select one last time, so we must have forced ourselves to 
807
       // select a new one
808
       selected_cache_line_new <= 1;
809
     else if (selected_cache_line_new)
810
       selected_cache_line_new <= 0;
811
 
812
   always @(posedge wb_clk)
813
     if (wb_rst)
814
       dirty <= 0;
815
     else if (cache_write)
816
       dirty <= dirty | selected_cache_line_r;
817
     else if (writeback_done)
818
       dirty <= dirty & ~(selected_cache_line_r);
819
 
820
   // Validate the cache line address in the register when line filled
821
   always @(posedge wb_clk)
822
     if (wb_rst)
823
       cache_line_validate <= 0;
824
     else if (fill_done)
825
       cache_line_validate <= selected_cache_line_r;
826
     else if (|cache_line_validate)
827
       cache_line_validate <= 0;
828
 
829
   // Invalidate the cache line address in the register when line written back
830
   always @(posedge wb_clk)
831
     if (wb_rst)
832
       cache_line_invalidate <= 0;
833
     else if (writeback_done | invalidate_clean_line)
834
       cache_line_invalidate <= selected_cache_line_r;
835
     else if (|cache_line_invalidate)
836
       cache_line_invalidate <= 0;
837
 
838
   // Initiate-writeback logic
839
   always @(posedge wb_clk)
840
     if (wb_rst)
841
       start_writeback <= 0;
842
     else if (selected_cache_line_new & (|(dirty & selected_cache_line_r)) &
843
              (|(selected_cache_line_r & cache_line_addr_valid)) &
844
              !(|(cache_line_addr_hit & selected_cache_line_r)))
845
       start_writeback <= 1;
846
     else if (start_writeback)
847
       start_writeback <= 0;
848
 
849
   // Invalidate lines which we haven't written to so we can fill them
850
   always @(posedge wb_clk)
851
     if (wb_rst)
852
       invalidate_clean_line <= 0;
853
     else if (invalidate_clean_line)
854
       invalidate_clean_line <= 0;
855
     else if ((selected_cache_line_new) &  // New line selected
856
              !(|(dirty & selected_cache_line_r)) & // It's not dirty
857
              // It's valid, but we've selected it so we're trashing it
858
              (|(selected_cache_line_r & cache_line_addr_valid)) &
859
              !(|(cache_line_addr_hit & selected_cache_line_r))) // Not a hit
860
       invalidate_clean_line <= 1;
861
 
862
   reg                    invalidate_clean_line_r;
863
   always @(posedge wb_clk)
864
     invalidate_clean_line_r <= invalidate_clean_line;
865
 
866
 
867
   // Initiate-fill logic
868
   always @(posedge wb_clk)
869
     if (wb_rst)
870
       start_fill <= 0;
871
     else if (((selected_cache_line_new) & // New line selected
872
               // not valid
873
               !(|(cache_line_addr_valid & selected_cache_line_r))) |
874
              writeback_done | invalidate_clean_line_r
875
              )
876
       start_fill <= 1;
877
     else if (start_fill)
878
       start_fill <= 0;
879
 
880
   // Relies on there only being 4 lines
881
   always @(posedge wb_clk)
882
     if (selected_cache_line_r[0])
883
       selected_cache_line_enc <= 0;
884
     else if (selected_cache_line_r[1])
885
       selected_cache_line_enc <= 1;
886
     else if (selected_cache_line_r[2])
887
       selected_cache_line_enc <= 2;
888
     else if (selected_cache_line_r[3])
889
       selected_cache_line_enc <= 3;
890
 
891
 
892
endmodule // xilinx_ddr2_wb_if_cache_control

powered by: WebSVN 2.1.0

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