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 [ 2: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 [255:0] rd_data_fifo_out; |
---|
67 | reg [ 23:0] rd_addr_cache; |
---|
68 | wire [ 71:0] wr_dout; |
---|
69 | wire [ 31:0] cmd_out; |
---|
70 | reg wb_stb_i_d; |
---|
71 | reg [ 31:0] mask_data; |
---|
72 | |
---|
73 | wire dram_ready; |
---|
74 | wire fifo_empty; |
---|
75 | reg push_tran; |
---|
76 | |
---|
77 | //wire [13:0] parallelterminationcontrol; |
---|
78 | //wire [13:0] seriesterminationcontrol; |
---|
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[25:2]), |
---|
86 | .rd_data_valid(rd_data_valid), |
---|
87 | .rd_data_fifo_out(rd_data_fifo_out), |
---|
88 | .app_wdf_data(wr_dout[63:0]), |
---|
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), |
---|
98 | //.rst0_tb(ddr3_reset), |
---|
99 | |
---|
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), |
---|
112 | .ddr2_a(ddr3_a), |
---|
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 | ); |
---|
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[1:0]) |
---|
192 | 2'b00:mask_data<={24'h000000,wr_dout[71:64]}; |
---|
193 | 2'b01:mask_data<={16'h0000,wr_dout[71:64],8'h00}; |
---|
194 | 2'b10:mask_data<={8'h00,wr_dout[71:64],16'h0000}; |
---|
195 | 2'b11:mask_data<={wr_dout[71:64],24'h000000}; |
---|
196 | endcase |
---|
197 | |
---|
198 | //wire [254:0] trig0; |
---|
199 | |
---|
200 | /*ila1 ila1_inst ( |
---|
201 | .CONTROL(CONTROL), |
---|
202 | .CLK(ddr_clk), |
---|
203 | .TRIG0(trig0) |
---|
204 | );*/ |
---|
205 | |
---|
206 | /*assign trig0[127:0]=rd_data_fifo_out; |
---|
207 | assign trig0[199:128]=wr_dout; |
---|
208 | assign trig0[231:200]=cmd_out; |
---|
209 | assign trig0[232]=0; |
---|
210 | assign trig0[233]=0; |
---|
211 | assign trig0[234]=rd_data_valid; |
---|
212 | assign trig0[235]=0; |
---|
213 | assign trig0[236]=fifo_empty; |
---|
214 | assign trig0[237]=0; |
---|
215 | assign trig0[238]=0; |
---|
216 | assign trig0[254:239]=0; |
---|
217 | */ |
---|
218 | |
---|
219 | reg fifo_full_d; |
---|
220 | reg written; |
---|
221 | reg fifo_read; |
---|
222 | |
---|
223 | dram_fifo fifo( |
---|
224 | .rst(ddr_rst), |
---|
225 | .wr_clk(wb_clk_i), |
---|
226 | .rd_clk(ddr_clk), |
---|
227 | .din({wb_sel_i,wb_dat_i,wb_we_i,wb_adr_i[33:3]}), |
---|
228 | .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)), |
---|
229 | .full(fifo_full), |
---|
230 | .rd_en(fifo_read), |
---|
231 | .dout({wr_dout,cmd_out}), |
---|
232 | .wr_data_count(fifo_used), |
---|
233 | .empty(fifo_empty) |
---|
234 | ); |
---|
235 | |
---|
236 | `define DDR_IDLE 3'b000 |
---|
237 | `define DDR_WRITE_1 3'b001 |
---|
238 | `define DDR_WRITE_2 3'b010 |
---|
239 | `define DDR_READ_1 3'b011 |
---|
240 | `define DDR_READ_2 3'b100 |
---|
241 | |
---|
242 | reg [2:0] ddr_state; |
---|
243 | reg rd_data_valid_stb; |
---|
244 | reg wb_ack_d1; |
---|
245 | |
---|
246 | always @(posedge ddr_clk or posedge ddr_rst) |
---|
247 | if(ddr_rst) |
---|
248 | begin |
---|
249 | ddr_state<=`DDR_IDLE; |
---|
250 | fifo_read<=0; |
---|
251 | push_tran<=0; |
---|
252 | rd_data_valid_stb<=0; |
---|
253 | end |
---|
254 | else |
---|
255 | case(ddr_state) |
---|
256 | `DDR_IDLE: |
---|
257 | if(!fifo_empty && dram_ready) |
---|
258 | begin |
---|
259 | push_tran<=1; |
---|
260 | if(cmd_out[31]) |
---|
261 | begin |
---|
262 | ddr_state<=`DDR_WRITE_1; |
---|
263 | fifo_read<=1; |
---|
264 | end |
---|
265 | else |
---|
266 | ddr_state<=`DDR_READ_1; |
---|
267 | end |
---|
268 | `DDR_WRITE_1: |
---|
269 | begin |
---|
270 | push_tran<=0; |
---|
271 | fifo_read<=0; |
---|
272 | ddr_state<=`DDR_WRITE_2; // Protect against FIFO empty signal latency |
---|
273 | end |
---|
274 | `DDR_WRITE_2: |
---|
275 | ddr_state<=`DDR_IDLE; |
---|
276 | `DDR_READ_1: |
---|
277 | begin |
---|
278 | push_tran<=0; |
---|
279 | if(rd_data_valid) |
---|
280 | begin |
---|
281 | rd_data_valid_stb<=1; |
---|
282 | fifo_read<=1; |
---|
283 | ddr_state<=`DDR_READ_2; |
---|
284 | end |
---|
285 | end |
---|
286 | `DDR_READ_2: |
---|
287 | begin |
---|
288 | fifo_read<=0; |
---|
289 | if(wb_ack_d1) // Enought delay to protect against FIFO empty signal latency |
---|
290 | begin |
---|
291 | rd_data_valid_stb<=0; |
---|
292 | ddr_state<=`DDR_IDLE; |
---|
293 | end |
---|
294 | end |
---|
295 | endcase |
---|
296 | |
---|
297 | reg rd_data_valid_stb_d1; |
---|
298 | reg rd_data_valid_stb_d2; |
---|
299 | reg rd_data_valid_stb_d3; |
---|
300 | reg rd_data_valid_stb_d4; |
---|
301 | reg [255:0] rd_data_fifo_out_d; |
---|
302 | reg wb_ack_d; |
---|
303 | |
---|
304 | always @( * ) |
---|
305 | case(wb_adr_i[4:3]) |
---|
306 | 2'b00:wb_dat_o<=rd_data_fifo_out_d[63:0]; |
---|
307 | 2'b01:wb_dat_o<=rd_data_fifo_out_d[127:64]; |
---|
308 | 2'b10:wb_dat_o<=rd_data_fifo_out_d[191:128]; |
---|
309 | 2'b11:wb_dat_o<=rd_data_fifo_out_d[255:192]; |
---|
310 | endcase |
---|
311 | |
---|
312 | always @(posedge wb_clk_i or posedge wb_rst_i) |
---|
313 | if(wb_rst_i) |
---|
314 | rd_addr_cache<=24'hFFFFFF; |
---|
315 | else |
---|
316 | begin |
---|
317 | wb_stb_i_d<=wb_stb_i; |
---|
318 | if(wb_cyc_i && wb_stb_i) |
---|
319 | if(!wb_we_i) |
---|
320 | rd_addr_cache<=wb_ack_o ? wb_adr_i[28:5]:rd_addr_cache; |
---|
321 | else |
---|
322 | if(rd_addr_cache==wb_adr_i[28:5]) |
---|
323 | rd_addr_cache<=24'hFFFFFF; |
---|
324 | rd_data_valid_stb_d1<=rd_data_valid_stb; |
---|
325 | rd_data_valid_stb_d2<=rd_data_valid_stb_d1; |
---|
326 | rd_data_valid_stb_d3<=rd_data_valid_stb_d2; |
---|
327 | rd_data_valid_stb_d4<=rd_data_valid_stb_d3; |
---|
328 | fifo_full_d<=fifo_full; |
---|
329 | if(wb_ack_o) |
---|
330 | written<=0; |
---|
331 | else |
---|
332 | if(!fifo_full && fifo_full_d) |
---|
333 | written<=1; |
---|
334 | end |
---|
335 | |
---|
336 | 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]); |
---|
337 | |
---|
338 | always @(posedge ddr_clk) |
---|
339 | begin |
---|
340 | wb_ack_d<=wb_ack_o; |
---|
341 | wb_ack_d1<=wb_ack_d; |
---|
342 | if(rd_data_valid) |
---|
343 | rd_data_fifo_out_d<=rd_data_fifo_out; |
---|
344 | end |
---|
345 | |
---|
346 | endmodule |
---|