[6] | 1 | `timescale 1ns / 1ps |
---|
| 2 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 3 | // Company: (C) Athree, 2009 |
---|
| 4 | // Engineer: Dmitry Rozhdestvenskiy |
---|
| 5 | // Email [email protected] [email protected] [email protected] |
---|
| 6 | // |
---|
| 7 | // Design Name: Bridge from Wishbone to Altera DDR3 controller |
---|
| 8 | // Module Name: wb2altddr3 |
---|
| 9 | // Project Name: SPARC SoC single-core |
---|
| 10 | // |
---|
| 11 | // LICENSE: |
---|
| 12 | // This is a Free Hardware Design; you can redistribute it and/or |
---|
| 13 | // modify it under the terms of the GNU General Public License |
---|
| 14 | // version 2 as published by the Free Software Foundation. |
---|
| 15 | // The above named program is distributed in the hope that it will |
---|
| 16 | // be useful, but WITHOUT ANY WARRANTY; without even the implied |
---|
| 17 | // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
---|
| 18 | // See the GNU General Public License for more details. |
---|
| 19 | // |
---|
| 20 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 21 | |
---|
| 22 | module dram_wb( |
---|
| 23 | input clk200, |
---|
| 24 | input rup, |
---|
| 25 | input rdn, |
---|
| 26 | |
---|
| 27 | input wb_clk_i, |
---|
| 28 | input wb_rst_i, |
---|
| 29 | |
---|
| 30 | input [63:0] wb_dat_i, |
---|
| 31 | output reg [63:0] wb_dat_o, |
---|
| 32 | input [63:0] wb_adr_i, |
---|
| 33 | input [ 7:0] wb_sel_i, |
---|
| 34 | input wb_we_i, |
---|
| 35 | input wb_cyc_i, |
---|
| 36 | input wb_stb_i, |
---|
| 37 | output wb_ack_o, |
---|
| 38 | output wb_err_o, |
---|
| 39 | output wb_rty_o, |
---|
| 40 | input wb_cab_i, |
---|
| 41 | |
---|
| 42 | inout [63:0] ddr3_dq, |
---|
| 43 | inout [ 7:0] ddr3_dqs, |
---|
| 44 | inout [ 7:0] ddr3_dqs_n, |
---|
| 45 | inout ddr3_ck, |
---|
| 46 | inout ddr3_ck_n, |
---|
[10] | 47 | //output ddr3_reset, |
---|
[6] | 48 | output [12:0] ddr3_a, |
---|
[17] | 49 | output [ 1:0] ddr3_ba, |
---|
[6] | 50 | output ddr3_ras_n, |
---|
| 51 | output ddr3_cas_n, |
---|
| 52 | output ddr3_we_n, |
---|
| 53 | output ddr3_cs_n, |
---|
| 54 | output ddr3_odt, |
---|
| 55 | output ddr3_ce, |
---|
| 56 | output [ 7:0] ddr3_dm, |
---|
| 57 | |
---|
| 58 | output phy_init_done, |
---|
| 59 | |
---|
| 60 | output [ 7:0] fifo_used, |
---|
| 61 | |
---|
| 62 | input dcm_locked, |
---|
| 63 | input sysrst |
---|
| 64 | ); |
---|
| 65 | |
---|
[17] | 66 | wire [127:0] rd_data_fifo_out; |
---|
[6] | 67 | reg [ 23:0] rd_addr_cache; |
---|
[17] | 68 | wire [127:0] wr_dout; |
---|
[6] | 69 | wire [ 31:0] cmd_out; |
---|
| 70 | reg wb_stb_i_d; |
---|
[17] | 71 | reg [ 15:0] mask_data; |
---|
[6] | 72 | |
---|
| 73 | wire dram_ready; |
---|
| 74 | wire fifo_empty; |
---|
| 75 | reg push_tran; |
---|
[17] | 76 | |
---|
[6] | 77 | //wire [13:0] parallelterminationcontrol; |
---|
| 78 | //wire [13:0] seriesterminationcontrol; |
---|
[17] | 79 | |
---|
| 80 | dram dram_ctrl( |
---|
| 81 | .sys_clk(clk200), |
---|
| 82 | .sys_rst_n(sysrst), // Resets all |
---|
| 83 | .phy_init_done(phy_init_done), |
---|
| 84 | .app_wdf_mask_data(mask_data), |
---|
| 85 | .app_af_addr(cmd_out[31:1]), |
---|
[6] | 86 | .rd_data_valid(rd_data_valid), |
---|
[17] | 87 | .rd_data_fifo_out(rd_data_fifo_out), |
---|
| 88 | .app_wdf_data(wr_dout[127:0]), |
---|
[6] | 89 | |
---|
| 90 | // in dubbio |
---|
| 91 | .app_wdf_wren(1'b1), |
---|
| 92 | .app_af_wren(1'b1), |
---|
| 93 | .app_af_afull(), |
---|
| 94 | .app_wdf_afull(), |
---|
| 95 | .app_af_cmd(), |
---|
| 96 | .clk0_tb(), |
---|
| 97 | .idly_clk_200(clk200), |
---|
[17] | 98 | .rst0_tb(ddr3_reset), |
---|
[10] | 99 | |
---|
[6] | 100 | .ddr2_dqs(ddr3_dqs), |
---|
| 101 | .ddr2_dqs_n(ddr3_dqs_n), |
---|
| 102 | .ddr2_ck(ddr3_ck), |
---|
| 103 | .ddr2_ck_n(ddr3_ck_n), |
---|
| 104 | .ddr2_dq(ddr3_dq), |
---|
| 105 | .ddr2_ras_n(ddr3_ras_n), |
---|
| 106 | .ddr2_cas_n(ddr3_cas_n), |
---|
| 107 | .ddr2_odt(ddr3_odt), |
---|
| 108 | .ddr2_cs_n(ddr3_cs_n), |
---|
| 109 | .ddr2_cke(ddr3_ce), |
---|
| 110 | .ddr2_we_n(ddr3_we_n), |
---|
| 111 | .ddr2_ba(ddr3_ba), |
---|
[17] | 112 | .ddr2_a(ddr3_a), |
---|
[6] | 113 | .ddr2_dm(ddr3_dm) |
---|
| 114 | // | |
---|
| 115 | //non sostituiti\|/ |
---|
| 116 | // V |
---|
| 117 | // .phy_clk(ddr_clk), // User clock |
---|
| 118 | // .local_ready(dram_ready), |
---|
| 119 | // .local_burstbegin(push_tran), |
---|
| 120 | // .local_read_req(!cmd_out[31] && push_tran), |
---|
| 121 | // .local_write_req(cmd_out[31] && push_tran), |
---|
| 122 | // .local_wdata({wr_dout[63:0],wr_dout[63:0],wr_dout[63:0],wr_dout[63:0]}), |
---|
| 123 | // .local_size(3'b001) |
---|
| 124 | |
---|
| 125 | ); |
---|
| 126 | |
---|
| 127 | /* comment by sal |
---|
| 128 | dram dram_ctrl( |
---|
| 129 | .pll_ref_clk(clk200), |
---|
| 130 | .global_reset_n(sysrst), // Resets all |
---|
| 131 | .soft_reset_n(1), // Resets all but PLL |
---|
| 132 | |
---|
| 133 | .reset_request_n(), // Active when not ready (PLL not locked) |
---|
| 134 | .reset_phy_clk_n(), // Reset input sync to phy_clk |
---|
| 135 | |
---|
| 136 | .phy_clk(ddr_clk), // User clock |
---|
| 137 | .dll_reference_clk(), // For external DLL |
---|
| 138 | |
---|
| 139 | .dqs_delay_ctrl_export(), |
---|
| 140 | .aux_scan_clk(), |
---|
| 141 | .aux_scan_clk_reset_n(), |
---|
| 142 | .aux_full_rate_clk(), |
---|
| 143 | .aux_half_rate_clk(), |
---|
| 144 | |
---|
| 145 | .oct_ctl_rs_value(seriesterminationcontrol), |
---|
| 146 | .oct_ctl_rt_value(parallelterminationcontrol), |
---|
| 147 | |
---|
| 148 | .local_init_done(phy_init_done), |
---|
| 149 | |
---|
| 150 | .local_ready(dram_ready), |
---|
| 151 | .local_address(cmd_out[25:2]), |
---|
| 152 | .local_burstbegin(push_tran), |
---|
| 153 | .local_read_req(!cmd_out[31] && push_tran), |
---|
| 154 | .local_write_req(cmd_out[31] && push_tran), |
---|
| 155 | .local_wdata_req(), |
---|
| 156 | .local_wdata({wr_dout[63:0],wr_dout[63:0],wr_dout[63:0],wr_dout[63:0]}), |
---|
| 157 | .local_be(mask_data), |
---|
| 158 | .local_size(3'b001), |
---|
| 159 | .local_rdata_valid(rd_data_valid), |
---|
| 160 | .local_rdata(rd_data_fifo_out), |
---|
| 161 | .local_refresh_ack(), |
---|
| 162 | |
---|
| 163 | .mem_clk(ddr3_ck), |
---|
| 164 | .mem_clk_n(ddr3_ck_n), |
---|
| 165 | .mem_reset_n(ddr3_reset), |
---|
| 166 | .mem_dq(ddr3_dq), |
---|
| 167 | .mem_dqs(ddr3_dqs), |
---|
| 168 | .mem_dqsn(ddr3_dqs_n), |
---|
| 169 | .mem_odt(ddr3_odt), |
---|
| 170 | .mem_cs_n(ddr3_cs_n), |
---|
| 171 | .mem_cke(ddr3_ce), |
---|
| 172 | .mem_addr(ddr3_a), |
---|
| 173 | .mem_ba(ddr3_ba), |
---|
| 174 | .mem_ras_n(ddr3_ras_n), |
---|
| 175 | .mem_cas_n(ddr3_cas_n), |
---|
| 176 | .mem_we_n(ddr3_we_n), |
---|
| 177 | .mem_dm(ddr3_dm) |
---|
| 178 | ); |
---|
[17] | 179 | */ |
---|
| 180 | |
---|
[6] | 181 | assign ddr_rst=!phy_init_done; |
---|
[17] | 182 | |
---|
[6] | 183 | /*oct_alt_oct_power_f4c oct |
---|
| 184 | ( |
---|
| 185 | .parallelterminationcontrol(parallelterminationcontrol), |
---|
| 186 | .seriesterminationcontrol(seriesterminationcontrol), |
---|
| 187 | .rdn(rdn), |
---|
| 188 | .rup(rup) |
---|
| 189 | ) ; */ |
---|
| 190 | |
---|
| 191 | always @( * ) |
---|
| 192 | case(cmd_out[1:0]) |
---|
| 193 | 2'b00:mask_data<={24'h000000,wr_dout[71:64]}; |
---|
| 194 | 2'b01:mask_data<={16'h0000,wr_dout[71:64],8'h00}; |
---|
| 195 | 2'b10:mask_data<={8'h00,wr_dout[71:64],16'h0000}; |
---|
| 196 | 2'b11:mask_data<={wr_dout[71:64],24'h000000}; |
---|
| 197 | endcase |
---|
| 198 | |
---|
| 199 | //wire [254:0] trig0; |
---|
| 200 | |
---|
| 201 | /*ila1 ila1_inst ( |
---|
| 202 | .CONTROL(CONTROL), |
---|
| 203 | .CLK(ddr_clk), |
---|
| 204 | .TRIG0(trig0) |
---|
| 205 | );*/ |
---|
| 206 | |
---|
| 207 | /*assign trig0[127:0]=rd_data_fifo_out; |
---|
| 208 | assign trig0[199:128]=wr_dout; |
---|
| 209 | assign trig0[231:200]=cmd_out; |
---|
| 210 | assign trig0[232]=0; |
---|
| 211 | assign trig0[233]=0; |
---|
| 212 | assign trig0[234]=rd_data_valid; |
---|
| 213 | assign trig0[235]=0; |
---|
| 214 | assign trig0[236]=fifo_empty; |
---|
| 215 | assign trig0[237]=0; |
---|
| 216 | assign trig0[238]=0; |
---|
| 217 | assign trig0[254:239]=0; |
---|
| 218 | */ |
---|
| 219 | |
---|
| 220 | reg fifo_full_d; |
---|
| 221 | reg written; |
---|
| 222 | reg fifo_read; |
---|
| 223 | |
---|
| 224 | dram_fifo fifo( |
---|
| 225 | .rst(ddr_rst), |
---|
| 226 | .wr_clk(wb_clk_i), |
---|
| 227 | .rd_clk(ddr_clk), |
---|
| 228 | .din({wb_sel_i,wb_dat_i,wb_we_i,wb_adr_i[33:3]}), |
---|
| 229 | .wr_en(wb_cyc_i && wb_stb_i && (!wb_stb_i_d || (fifo_full_d && !written)) && !fifo_full && !(rd_addr_cache==wb_adr_i[28:5] && !wb_we_i)), |
---|
| 230 | .full(fifo_full), |
---|
| 231 | .rd_en(fifo_read), |
---|
| 232 | .dout({wr_dout,cmd_out}), |
---|
| 233 | .wr_data_count(fifo_used), |
---|
| 234 | .empty(fifo_empty) |
---|
| 235 | ); |
---|
| 236 | |
---|
| 237 | `define DDR_IDLE 3'b000 |
---|
| 238 | `define DDR_WRITE_1 3'b001 |
---|
| 239 | `define DDR_WRITE_2 3'b010 |
---|
| 240 | `define DDR_READ_1 3'b011 |
---|
| 241 | `define DDR_READ_2 3'b100 |
---|
| 242 | |
---|
| 243 | reg [2:0] ddr_state; |
---|
| 244 | reg rd_data_valid_stb; |
---|
| 245 | reg wb_ack_d1; |
---|
| 246 | |
---|
| 247 | always @(posedge ddr_clk or posedge ddr_rst) |
---|
| 248 | if(ddr_rst) |
---|
| 249 | begin |
---|
| 250 | ddr_state<=`DDR_IDLE; |
---|
| 251 | fifo_read<=0; |
---|
| 252 | push_tran<=0; |
---|
| 253 | rd_data_valid_stb<=0; |
---|
| 254 | end |
---|
| 255 | else |
---|
| 256 | case(ddr_state) |
---|
| 257 | `DDR_IDLE: |
---|
| 258 | if(!fifo_empty && dram_ready) |
---|
| 259 | begin |
---|
| 260 | push_tran<=1; |
---|
| 261 | if(cmd_out[31]) |
---|
| 262 | begin |
---|
| 263 | ddr_state<=`DDR_WRITE_1; |
---|
| 264 | fifo_read<=1; |
---|
| 265 | end |
---|
| 266 | else |
---|
| 267 | ddr_state<=`DDR_READ_1; |
---|
| 268 | end |
---|
| 269 | `DDR_WRITE_1: |
---|
| 270 | begin |
---|
| 271 | push_tran<=0; |
---|
| 272 | fifo_read<=0; |
---|
| 273 | ddr_state<=`DDR_WRITE_2; // Protect against FIFO empty signal latency |
---|
| 274 | end |
---|
| 275 | `DDR_WRITE_2: |
---|
| 276 | ddr_state<=`DDR_IDLE; |
---|
| 277 | `DDR_READ_1: |
---|
| 278 | begin |
---|
| 279 | push_tran<=0; |
---|
| 280 | if(rd_data_valid) |
---|
| 281 | begin |
---|
| 282 | rd_data_valid_stb<=1; |
---|
| 283 | fifo_read<=1; |
---|
| 284 | ddr_state<=`DDR_READ_2; |
---|
| 285 | end |
---|
| 286 | end |
---|
| 287 | `DDR_READ_2: |
---|
| 288 | begin |
---|
| 289 | fifo_read<=0; |
---|
| 290 | if(wb_ack_d1) // Enought delay to protect against FIFO empty signal latency |
---|
| 291 | begin |
---|
| 292 | rd_data_valid_stb<=0; |
---|
| 293 | ddr_state<=`DDR_IDLE; |
---|
| 294 | end |
---|
| 295 | end |
---|
| 296 | endcase |
---|
| 297 | |
---|
| 298 | reg rd_data_valid_stb_d1; |
---|
| 299 | reg rd_data_valid_stb_d2; |
---|
| 300 | reg rd_data_valid_stb_d3; |
---|
| 301 | reg rd_data_valid_stb_d4; |
---|
[17] | 302 | reg [127:0] rd_data_fifo_out_d; |
---|
[6] | 303 | reg wb_ack_d; |
---|
| 304 | |
---|
| 305 | always @( * ) |
---|
| 306 | case(wb_adr_i[4:3]) |
---|
| 307 | 2'b00:wb_dat_o<=rd_data_fifo_out_d[63:0]; |
---|
| 308 | 2'b01:wb_dat_o<=rd_data_fifo_out_d[127:64]; |
---|
| 309 | 2'b10:wb_dat_o<=rd_data_fifo_out_d[191:128]; |
---|
| 310 | 2'b11:wb_dat_o<=rd_data_fifo_out_d[255:192]; |
---|
| 311 | endcase |
---|
| 312 | |
---|
| 313 | always @(posedge wb_clk_i or posedge wb_rst_i) |
---|
| 314 | if(wb_rst_i) |
---|
| 315 | rd_addr_cache<=24'hFFFFFF; |
---|
| 316 | else |
---|
| 317 | begin |
---|
| 318 | wb_stb_i_d<=wb_stb_i; |
---|
| 319 | if(wb_cyc_i && wb_stb_i) |
---|
| 320 | if(!wb_we_i) |
---|
| 321 | rd_addr_cache<=wb_ack_o ? wb_adr_i[28:5]:rd_addr_cache; |
---|
| 322 | else |
---|
| 323 | if(rd_addr_cache==wb_adr_i[28:5]) |
---|
| 324 | rd_addr_cache<=24'hFFFFFF; |
---|
| 325 | rd_data_valid_stb_d1<=rd_data_valid_stb; |
---|
| 326 | rd_data_valid_stb_d2<=rd_data_valid_stb_d1; |
---|
| 327 | rd_data_valid_stb_d3<=rd_data_valid_stb_d2; |
---|
| 328 | rd_data_valid_stb_d4<=rd_data_valid_stb_d3; |
---|
| 329 | fifo_full_d<=fifo_full; |
---|
| 330 | if(wb_ack_o) |
---|
| 331 | written<=0; |
---|
| 332 | else |
---|
| 333 | if(!fifo_full && fifo_full_d) |
---|
| 334 | written<=1; |
---|
| 335 | end |
---|
| 336 | |
---|
| 337 | assign wb_ack_o=wb_we_i ? (wb_cyc_i && wb_stb_i && !fifo_full):(rd_data_valid_stb_d2 && !rd_data_valid_stb_d3) || (rd_addr_cache==wb_adr_i[28:5]); |
---|
| 338 | |
---|
| 339 | always @(posedge ddr_clk) |
---|
| 340 | begin |
---|
| 341 | wb_ack_d<=wb_ack_o; |
---|
| 342 | wb_ack_d1<=wb_ack_d; |
---|
| 343 | if(rd_data_valid) |
---|
| 344 | rd_data_fifo_out_d<=rd_data_fifo_out; |
---|
| 345 | end |
---|
| 346 | |
---|
| 347 | endmodule |
---|