[22] | 1 | `timescale 1ns / 1ps |
---|
| 2 | ////////////////////////////////////////////////////////////////////////////////// |
---|
| 3 | // Company: (C) Athree, 2009 |
---|
| 4 | // Engineer: Dmitry Rozhdestvenskiy |
---|
| 5 | // Email dmitry.rozhdestvenskiy@srisc.com dmitryr@a3.spb.ru divx4log@narod.ru |
---|
| 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 | ////////////////////////////////////////////////////////////////////////////////// |
---|
[17] | 21 | |
---|
[22] | 22 | module dram_wb( |
---|
| 23 | input clk200, |
---|
| 24 | // input rup, |
---|
| 25 | // input rdn, |
---|
[17] | 26 | |
---|
[22] | 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 | |
---|
[23] | 42 | inout [63:0] ddr2_dq, |
---|
| 43 | inout [ 7:0] ddr2_dqs, |
---|
| 44 | inout [ 7:0] ddr2_dqs_n, |
---|
| 45 | inout ddr2_ck, |
---|
| 46 | inout ddr2_ck_n, |
---|
[22] | 47 | //output ddr3_reset, |
---|
[23] | 48 | output [12:0] ddr2_a, |
---|
| 49 | output [ 1:0] ddr2_ba, |
---|
| 50 | output ddr2_ras_n, |
---|
| 51 | output ddr2_cas_n, |
---|
| 52 | output ddr2_we_n, |
---|
| 53 | output ddr2_cs_n, |
---|
| 54 | output ddr2_odt, |
---|
| 55 | output ddr2_ce, |
---|
| 56 | output [ 7:0] ddr2_dm, |
---|
[22] | 57 | |
---|
| 58 | output phy_init_done, |
---|
| 59 | |
---|
| 60 | output [ 7:0] fifo_used, |
---|
| 61 | |
---|
| 62 | input dcm_locked, |
---|
| 63 | input sysrst |
---|
| 64 | ); |
---|
| 65 | |
---|
| 66 | wire app_af_afull; |
---|
| 67 | wire [127:0] rd_data_fifo_out; |
---|
| 68 | reg [ 23:0] rd_addr_cache; |
---|
| 69 | wire [ 71:0] wr_dout; |
---|
| 70 | wire [ 31:0] cmd_out; |
---|
| 71 | reg wb_stb_i_d; |
---|
| 72 | reg [ 15:0] mask_data; |
---|
| 73 | |
---|
| 74 | wire dram_ready; |
---|
| 75 | wire fifo_empty; |
---|
[23] | 76 | reg push_tran_wdf; |
---|
| 77 | reg push_tran; |
---|
[22] | 78 | |
---|
| 79 | //wire [13:0] parallelterminationcontrol; |
---|
| 80 | //wire [13:0] seriesterminationcontrol; |
---|
| 81 | |
---|
| 82 | dram # |
---|
| 83 | ( |
---|
| 84 | //synthesis traslate off |
---|
| 85 | .SIM_ONLY (1) |
---|
| 86 | //synthesis traslate on |
---|
| 87 | ) |
---|
[23] | 88 | // cmd_out[31] è il WE, CMD_OUT[30:0] corrisponde ad wb_addr[33:3] |
---|
| 89 | dram_ctrl( |
---|
[17] | 90 | .sys_clk(clk200), |
---|
| 91 | .sys_rst_n(sysrst), // Resets all |
---|
| 92 | .phy_init_done(phy_init_done), |
---|
[22] | 93 | |
---|
| 94 | .app_af_cmd({2'b00,!cmd_out[31]}), //command for the controller 000:write 001:read |
---|
| 95 | .app_af_addr(cmd_out[30:0]), |
---|
| 96 | .app_af_wren(push_tran), //write enable for address fifo |
---|
[23] | 97 | .app_wdf_wren(push_tran_wdf), // write enable for write data fifo |
---|
[22] | 98 | .app_wdf_data({wr_dout[63:0],wr_dout[63:0]}), |
---|
[17] | 99 | .app_wdf_mask_data(mask_data), |
---|
[22] | 100 | |
---|
[6] | 101 | .rd_data_valid(rd_data_valid), |
---|
[17] | 102 | .rd_data_fifo_out(rd_data_fifo_out), |
---|
[10] | 103 | |
---|
[22] | 104 | .clk0_tb(ddr_clk), |
---|
| 105 | .rst0_tb(ddr3_reset), |
---|
| 106 | .app_af_afull(app_af_afull), |
---|
| 107 | .app_wdf_afull(), |
---|
| 108 | .idly_clk_200(clk200), |
---|
| 109 | |
---|
[23] | 110 | .ddr2_ck(ddr2_ck), |
---|
| 111 | .ddr2_ck_n(ddr2_ck_n), |
---|
| 112 | .ddr2_dq(ddr2_dq), |
---|
| 113 | .ddr2_dqs(ddr2_dqs), |
---|
| 114 | .ddr2_dqs_n(ddr2_dqs_n), |
---|
| 115 | .ddr2_ras_n(ddr2_ras_n), |
---|
| 116 | .ddr2_cas_n(ddr2_cas_n), |
---|
| 117 | .ddr2_odt(ddr2_odt), |
---|
| 118 | .ddr2_cs_n(ddr2_cs_n), |
---|
| 119 | .ddr2_cke(ddr2_ce), |
---|
| 120 | .ddr2_we_n(ddr2_we_n), |
---|
| 121 | .ddr2_ba(ddr2_ba), |
---|
| 122 | .ddr2_a(ddr2_a), |
---|
| 123 | .ddr2_dm(ddr2_dm) |
---|
[6] | 124 | ); |
---|
| 125 | |
---|
[22] | 126 | assign dram_ready = phy_init_done && !app_af_afull; |
---|
| 127 | |
---|
[6] | 128 | /* comment by sal |
---|
[22] | 129 | dram dram_ctrl( |
---|
| 130 | .pll_ref_clk(clk200), |
---|
| 131 | .global_reset_n(sysrst), // Resets all |
---|
| 132 | .soft_reset_n(1), // Resets all but PLL |
---|
| 133 | |
---|
| 134 | .reset_request_n(), // Active when not ready (PLL not locked) |
---|
| 135 | .reset_phy_clk_n(), // Reset input sync to phy_clk |
---|
| 136 | |
---|
| 137 | .phy_clk(ddr_clk), // User clock |
---|
| 138 | .dll_reference_clk(), // For external DLL |
---|
| 139 | |
---|
| 140 | .dqs_delay_ctrl_export(), |
---|
| 141 | .aux_scan_clk(), |
---|
| 142 | .aux_scan_clk_reset_n(), |
---|
| 143 | .aux_full_rate_clk(), |
---|
| 144 | .aux_half_rate_clk(), |
---|
| 145 | |
---|
| 146 | .oct_ctl_rs_value(seriesterminationcontrol), |
---|
| 147 | .oct_ctl_rt_value(parallelterminationcontrol), |
---|
| 148 | |
---|
| 149 | .local_init_done(phy_init_done), |
---|
| 150 | |
---|
| 151 | .local_ready(dram_ready), |
---|
| 152 | .local_address(cmd_out[25:2]), |
---|
| 153 | .local_burstbegin(push_tran), |
---|
| 154 | .local_read_req(!cmd_out[31] && push_tran), |
---|
| 155 | .local_write_req(cmd_out[31] && push_tran), |
---|
| 156 | .local_wdata_req(), |
---|
| 157 | .local_wdata({wr_dout[63:0],wr_dout[63:0],wr_dout[63:0],wr_dout[63:0]}), |
---|
| 158 | .local_be(mask_data), |
---|
| 159 | .local_size(3'b001), |
---|
| 160 | .local_rdata_valid(rd_data_valid), |
---|
| 161 | .local_rdata(rd_data_fifo_out), |
---|
| 162 | .local_refresh_ack(), |
---|
| 163 | |
---|
| 164 | .mem_clk(ddr3_ck), |
---|
| 165 | .mem_clk_n(ddr3_ck_n), |
---|
| 166 | .mem_reset_n(ddr3_reset), |
---|
| 167 | .mem_dq(ddr3_dq), |
---|
| 168 | .mem_dqs(ddr3_dqs), |
---|
| 169 | .mem_dqsn(ddr3_dqs_n), |
---|
| 170 | .mem_odt(ddr3_odt), |
---|
| 171 | .mem_cs_n(ddr3_cs_n), |
---|
| 172 | .mem_cke(ddr3_ce), |
---|
| 173 | .mem_addr(ddr3_a), |
---|
| 174 | .mem_ba(ddr3_ba), |
---|
| 175 | .mem_ras_n(ddr3_ras_n), |
---|
| 176 | .mem_cas_n(ddr3_cas_n), |
---|
| 177 | .mem_we_n(ddr3_we_n), |
---|
| 178 | .mem_dm(ddr3_dm) |
---|
| 179 | ); |
---|
[17] | 180 | */ |
---|
| 181 | |
---|
[22] | 182 | assign ddr_rst=!phy_init_done; |
---|
[17] | 183 | |
---|
[22] | 184 | /*oct_alt_oct_power_f4c oct |
---|
| 185 | ( |
---|
| 186 | .parallelterminationcontrol(parallelterminationcontrol), |
---|
| 187 | .seriesterminationcontrol(seriesterminationcontrol), |
---|
| 188 | .rdn(rdn), |
---|
| 189 | .rup(rup) |
---|
| 190 | ) ; */ |
---|
| 191 | |
---|
| 192 | always @( * ) |
---|
[23] | 193 | case(push_tran & cmd_out[31]) |
---|
| 194 | 1'b1:mask_data<=16'hffff; |
---|
| 195 | 1'b0:mask_data<={wr_dout[71:64],8'h00}; |
---|
| 196 | //1'b1:mask_data<={wr_dout[71:64] ^ 8'hff,8'hff}; FIXME il sel e' in logica negata |
---|
[22] | 197 | endcase |
---|
| 198 | |
---|
[23] | 199 | //always @( * ) |
---|
| 200 | // case(cmd_out[0]) |
---|
| 201 | // 1'b0:mask_data<={8'h00,wr_dout[71:64]}; |
---|
| 202 | // 1'b1:mask_data<={wr_dout[71:64],8'h00}; |
---|
| 203 | // endcase |
---|
| 204 | |
---|
[22] | 205 | //wire [254:0] trig0; |
---|
| 206 | |
---|
| 207 | /*ila1 ila1_inst ( |
---|
| 208 | .CONTROL(CONTROL), |
---|
| 209 | .CLK(ddr_clk), |
---|
| 210 | .TRIG0(trig0) |
---|
| 211 | );*/ |
---|
| 212 | |
---|
[6] | 213 | /*assign trig0[127:0]=rd_data_fifo_out; |
---|
[22] | 214 | assign trig0[199:128]=wr_dout; |
---|
| 215 | assign trig0[231:200]=cmd_out; |
---|
| 216 | assign trig0[232]=0; |
---|
| 217 | assign trig0[233]=0; |
---|
| 218 | assign trig0[234]=rd_data_valid; |
---|
| 219 | assign trig0[235]=0; |
---|
| 220 | assign trig0[236]=fifo_empty; |
---|
| 221 | assign trig0[237]=0; |
---|
| 222 | assign trig0[238]=0; |
---|
| 223 | assign trig0[254:239]=0; |
---|
| 224 | */ |
---|
[6] | 225 | |
---|
[22] | 226 | reg fifo_full_d; |
---|
| 227 | reg written; |
---|
[23] | 228 | reg fifo_read; |
---|
[22] | 229 | |
---|
[23] | 230 | dram_fifo_fall fifo( |
---|
[22] | 231 | .rst(ddr_rst), |
---|
| 232 | .wr_clk(wb_clk_i), |
---|
| 233 | .rd_clk(ddr_clk), |
---|
| 234 | .din({wb_sel_i,wb_dat_i,wb_we_i,wb_adr_i[33:3]}), |
---|
| 235 | .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)), |
---|
| 236 | .full(fifo_full), |
---|
| 237 | .rd_en(fifo_read), |
---|
| 238 | .dout({wr_dout,cmd_out}), |
---|
| 239 | .wr_data_count(fifo_used), |
---|
| 240 | .empty(fifo_empty) |
---|
| 241 | ); |
---|
| 242 | |
---|
| 243 | `define DDR_IDLE 3'b000 |
---|
| 244 | `define DDR_WRITE_1 3'b001 |
---|
| 245 | `define DDR_WRITE_2 3'b010 |
---|
[23] | 246 | `define DDR_WRITE_3 3'b110 |
---|
[22] | 247 | `define DDR_READ_1 3'b011 |
---|
| 248 | `define DDR_READ_2 3'b100 |
---|
| 249 | |
---|
| 250 | reg [2:0] ddr_state; |
---|
| 251 | reg rd_data_valid_stb; |
---|
| 252 | reg wb_ack_d1; |
---|
| 253 | |
---|
| 254 | always @(posedge ddr_clk or posedge ddr_rst) |
---|
| 255 | if(ddr_rst) |
---|
| 256 | begin |
---|
| 257 | ddr_state<=`DDR_IDLE; |
---|
| 258 | fifo_read<=0; |
---|
[23] | 259 | push_tran_wdf<=0; |
---|
[22] | 260 | push_tran<=0; |
---|
| 261 | rd_data_valid_stb<=0; |
---|
| 262 | end |
---|
| 263 | else |
---|
| 264 | case(ddr_state) |
---|
| 265 | `DDR_IDLE: |
---|
| 266 | if(!fifo_empty && dram_ready) |
---|
| 267 | begin |
---|
| 268 | if(cmd_out[31]) |
---|
| 269 | begin |
---|
[23] | 270 | push_tran_wdf<=1; |
---|
[22] | 271 | ddr_state<=`DDR_WRITE_1; |
---|
| 272 | end |
---|
| 273 | else |
---|
[23] | 274 | begin |
---|
| 275 | push_tran<=1; |
---|
[22] | 276 | ddr_state<=`DDR_READ_1; |
---|
[23] | 277 | end |
---|
| 278 | end |
---|
[22] | 279 | `DDR_WRITE_1: |
---|
| 280 | begin |
---|
[23] | 281 | fifo_read<=1; |
---|
| 282 | push_tran_wdf<=1; |
---|
| 283 | push_tran<=1; |
---|
[22] | 284 | ddr_state<=`DDR_WRITE_2; // Protect against FIFO empty signal latency |
---|
| 285 | end |
---|
| 286 | `DDR_WRITE_2: |
---|
[23] | 287 | begin |
---|
| 288 | fifo_read<=0; |
---|
| 289 | push_tran_wdf<=0; |
---|
| 290 | push_tran<=0; |
---|
| 291 | ddr_state<=`DDR_WRITE_3; |
---|
| 292 | end |
---|
| 293 | `DDR_WRITE_3: |
---|
| 294 | ddr_state<=`DDR_IDLE; |
---|
[22] | 295 | `DDR_READ_1: |
---|
| 296 | begin |
---|
| 297 | push_tran<=0; |
---|
| 298 | if(rd_data_valid) |
---|
| 299 | begin |
---|
| 300 | rd_data_valid_stb<=1; |
---|
| 301 | fifo_read<=1; |
---|
| 302 | ddr_state<=`DDR_READ_2; |
---|
| 303 | end |
---|
| 304 | end |
---|
| 305 | `DDR_READ_2: |
---|
| 306 | begin |
---|
| 307 | fifo_read<=0; |
---|
| 308 | if(wb_ack_d1) // Enought delay to protect against FIFO empty signal latency |
---|
| 309 | begin |
---|
| 310 | rd_data_valid_stb<=0; |
---|
| 311 | ddr_state<=`DDR_IDLE; |
---|
| 312 | end |
---|
| 313 | end |
---|
| 314 | endcase |
---|
| 315 | |
---|
| 316 | reg rd_data_valid_stb_d1; |
---|
| 317 | reg rd_data_valid_stb_d2; |
---|
| 318 | reg rd_data_valid_stb_d3; |
---|
| 319 | reg rd_data_valid_stb_d4; |
---|
[23] | 320 | reg [127:0] rd_data_fifo_out_dH; |
---|
| 321 | reg [127:0] rd_data_fifo_out_dL; |
---|
[22] | 322 | reg wb_ack_d; |
---|
| 323 | |
---|
| 324 | always @( * ) |
---|
[23] | 325 | case(wb_adr_i[4:3]) |
---|
| 326 | 2'b00:wb_dat_o<=rd_data_fifo_out_dL[63:0]; |
---|
| 327 | 2'b01:wb_dat_o<=rd_data_fifo_out_dL[127:64]; |
---|
| 328 | 2'b10:wb_dat_o<=rd_data_fifo_out_dH[63:0]; |
---|
| 329 | 2'b11:wb_dat_o<=rd_data_fifo_out_dH[127:64]; |
---|
[22] | 330 | endcase |
---|
| 331 | |
---|
| 332 | always @(posedge wb_clk_i or posedge wb_rst_i) |
---|
| 333 | if(wb_rst_i) |
---|
| 334 | begin |
---|
| 335 | //written<=0; |
---|
| 336 | rd_addr_cache<=24'hFFFFFF; |
---|
| 337 | end |
---|
| 338 | else |
---|
| 339 | begin |
---|
| 340 | wb_stb_i_d<=wb_stb_i; |
---|
| 341 | if(wb_cyc_i && wb_stb_i) |
---|
| 342 | if(!wb_we_i) |
---|
| 343 | rd_addr_cache<=wb_ack_o ? wb_adr_i[28:5]:rd_addr_cache; |
---|
| 344 | else |
---|
| 345 | if(rd_addr_cache==wb_adr_i[28:5]) |
---|
| 346 | rd_addr_cache<=24'hFFFFFF; |
---|
| 347 | rd_data_valid_stb_d1<=rd_data_valid_stb; |
---|
| 348 | rd_data_valid_stb_d2<=rd_data_valid_stb_d1; |
---|
| 349 | rd_data_valid_stb_d3<=rd_data_valid_stb_d2; |
---|
| 350 | rd_data_valid_stb_d4<=rd_data_valid_stb_d3; |
---|
| 351 | fifo_full_d<=fifo_full; |
---|
| 352 | if(wb_ack_o) |
---|
| 353 | written<=0; |
---|
| 354 | else |
---|
| 355 | if(!fifo_full && fifo_full_d) |
---|
| 356 | written<=1; |
---|
| 357 | end |
---|
| 358 | |
---|
| 359 | 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]); |
---|
| 360 | |
---|
| 361 | always @(posedge ddr_clk) |
---|
| 362 | begin |
---|
| 363 | wb_ack_d<=wb_ack_o; |
---|
| 364 | wb_ack_d1<=wb_ack_d; |
---|
| 365 | if(rd_data_valid) |
---|
[23] | 366 | rd_data_fifo_out_dH<=rd_data_fifo_out; |
---|
| 367 | if(rd_data_valid && !rd_data_valid_stb) |
---|
| 368 | rd_data_fifo_out_dL<=rd_data_fifo_out; |
---|
| 369 | |
---|
[22] | 370 | end |
---|
| 371 | |
---|
| 372 | endmodule |
---|
[23] | 373 | |
---|