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