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

Subversion Repositories mips_enhanced

[/] [mips_enhanced/] [trunk/] [grlib-gpl-1.0.19-b3188/] [lib/] [gaisler/] [ddr/] [ddr2sp64a.vhd] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 dimamali
------------------------------------------------------------------------------
2
--  This file is a part of the GRLIB VHDL IP LIBRARY
3
--  Copyright (C) 2003, Gaisler Research
4
--
5
--  This program is free software; you can redistribute it and/or modify
6
--  it under the terms of the GNU General Public License as published by
7
--  the Free Software Foundation; either version 2 of the License, or
8
--  (at your option) any later version.
9
--
10
--  This program is distributed in the hope that it will be useful,
11
--  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
--  GNU General Public License for more details.
14
--
15
--  You should have received a copy of the GNU General Public License
16
--  along with this program; if not, write to the Free Software
17
--  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
18
-----------------------------------------------------------------------------
19
-- Entity:      ddr2sp64a
20
-- File:        ddr2sp64a.vhd
21
-- Author:      Nils-Johan Wessman - Gaisler Research
22
-- Description: 64-bit DDR2 memory controller with asych AHB interface
23
------------------------------------------------------------------------------
24
 
25
library ieee;
26
use ieee.std_logic_1164.all;
27
library grlib;
28
use grlib.amba.all;
29
use grlib.stdlib.all;
30
library gaisler;
31
use grlib.devices.all;
32
use gaisler.memctrl.all;
33
library techmap;
34
use techmap.gencomp.all;
35
 
36
entity ddr2sp64a is
37
   generic (
38
      memtech : integer := 0;
39
      hindex  : integer := 0;
40
      haddr   : integer := 0;
41
      hmask   : integer := 16#f00#;
42
      ioaddr  : integer := 16#000#;
43
      iomask  : integer := 16#fff#;
44
      MHz     : integer := 100;
45
      TRFC    : integer := 130;
46
      col     : integer := 9;
47
      Mbyte   : integer := 8;
48
      fast    : integer := 0;
49
      pwron   : integer := 0;
50
      oepol   : integer := 0;
51
      readdly : integer := 1;
52
      odten   : integer := 0
53
   );
54
   port (
55
      rst     : in  std_ulogic;
56
      clk_ddr : in  std_ulogic;
57
      clk_ahb : in  std_ulogic;
58
      ahbsi   : in  ahb_slv_in_type;
59
      ahbso   : out ahb_slv_out_type;
60
      sdi     : in  sdctrl_in_type;
61
      sdo     : out sdctrl_out_type
62
   );
63
end;
64
 
65
architecture rtl of ddr2sp64a is
66
 
67
constant REVISION  : integer := 0;
68
 
69
constant CMD_PRE  : std_logic_vector(2 downto 0) := "010";
70
constant CMD_REF  : std_logic_vector(2 downto 0) := "100";
71
constant CMD_LMR  : std_logic_vector(2 downto 0) := "110";
72
constant CMD_EMR  : std_logic_vector(2 downto 0) := "111";
73
 
74
constant odtvalue : std_logic_vector(1 downto 0) := conv_std_logic_vector(odten, 2);
75
 
76
constant abuf : integer := 6;
77
constant hconfig : ahb_config_type := (
78
 
79
   4 => ahb_membar(haddr, '1', '1', hmask),
80
   5 => ahb_iobar(ioaddr, iomask),
81
   others => zero32);
82
 
83
type mcycletype is (midle, active, ext, leadout);
84
type ahb_state_type is (midle, rhold, dread, dwrite, whold1, whold2);
85
type sdcycletype is (act1, act2, act3, rd1, rd2, rd3, rd4, rd5, rd6, rd7, rd8,
86
                     wr0, wr1, wr2, wr3, wr4a, wr4b, wr4, wr5, sidle, ioreg1, ioreg2);
87
type icycletype is (iidle, pre, ref1, ref2, emode23, emode, lmode, emodeocd, finish);
88
 
89
-- sdram configuration register
90
 
91
type sdram_cfg_type is record
92
   command  : std_logic_vector(2 downto 0);
93
   csize    : std_logic_vector(1 downto 0);
94
   bsize    : std_logic_vector(2 downto 0);
95
   trcd     : std_ulogic;  -- tCD : 2/3 clock cycles
96
   trfc     : std_logic_vector(4 downto 0);
97
   trp      : std_ulogic;  -- precharge to activate: 2/3 clock cycles
98
   refresh  : std_logic_vector(11 downto 0);
99
   renable  : std_ulogic;
100
   dllrst   : std_ulogic;
101
   refon    : std_ulogic;
102
   cke      : std_ulogic;
103
   cal_en   : std_logic_vector(7 downto 0);
104
   cal_inc  : std_logic_vector(7 downto 0);
105
   cal_pll  : std_logic_vector(1 downto 0);  -- *** ??? pll_reconf
106
   cal_rst  : std_logic;
107
   readdly  : std_logic_vector(1 downto 0);
108
   twr      : std_logic_vector(4 downto 0);
109
   emr      : std_logic_vector(1 downto 0); -- selects EM register
110
   ocd      : std_ulogic; -- enable/disable ocd
111
end record;
112
 
113
type access_param is record
114
   haddr    : std_logic_vector(31 downto 0);
115
   size     : std_logic_vector(1 downto 0);
116
   hwrite   : std_ulogic;
117
   hio      : std_ulogic;
118
end record;
119
-- local registers
120
 
121
type ahb_reg_type is record
122
   hready   : std_ulogic;
123
   hsel     : std_ulogic;
124
   hio      : std_ulogic;
125
   startsd  : std_ulogic;
126
   write    : std_logic_vector(3 downto 0);
127
   state    : ahb_state_type;
128
   haddr    : std_logic_vector(31 downto 0);
129
   hrdata   : std_logic_vector(31 downto 0);
130
   hwdata   : std_logic_vector(31 downto 0);
131
   hwrite   : std_ulogic;
132
   htrans   : std_logic_vector(1 downto 0);
133
   hresp    : std_logic_vector(1 downto 0);
134
   raddr    : std_logic_vector(abuf-1 downto 0);
135
   size     : std_logic_vector(1 downto 0);
136
   acc      : access_param;
137
   sync        : std_logic_vector(2 downto 1);
138
   startsd_ack : std_logic;
139
end record;
140
 
141
type ddr_reg_type is record
142
   startsd      : std_ulogic;
143
   startsdold   : std_ulogic;
144
   hready       : std_ulogic;
145
   bdrive       : std_ulogic;
146
   qdrive       : std_ulogic;
147
   nbdrive      : std_ulogic;
148
   mstate       : mcycletype;
149
   sdstate      : sdcycletype;
150
   cmstate      : mcycletype;
151
   istate       : icycletype;
152
   trfc         : std_logic_vector(4 downto 0);
153
   refresh      : std_logic_vector(11 downto 0);
154
   sdcsn        : std_logic_vector(1  downto 0);
155
   sdwen        : std_ulogic;
156
   rasn         : std_ulogic;
157
   casn         : std_ulogic;
158
   dqm          : std_logic_vector(15 downto 0);
159
   dqm_dly      : std_logic_vector(15 downto 0);  -- *** ??? delay ctrl
160
   address      : std_logic_vector(15 downto 2);  -- memory address
161
   ba           : std_logic_vector( 1 downto 0);
162
   waddr        : std_logic_vector(abuf-1 downto 0);
163
   waddr_d      : std_logic_vector(abuf-1 downto 0); -- Same as waddr but delayed to compensate for pipelined output data
164
   cfg          : sdram_cfg_type;
165
   hrdata       : std_logic_vector(127 downto 0);
166
   readdly      : std_logic_vector(1 downto 0); -- added read latency
167
   wdata        : std_logic_vector(127 downto 0); -- pipeline register for output data
168
   initnopdly   : std_logic_vector(7 downto 0); -- 400 ns delay
169
   sync         : std_logic;
170
   odt          : std_logic_vector(1 downto 0);
171
   sdo_bdrive   : std_logic; -- *** ??? delay ctrl
172
   sdo_qdrive   : std_logic; -- *** ??? delay ctrl
173
end record;
174
 
175
signal vcc : std_ulogic;
176
signal r, ri : ddr_reg_type;
177
signal ra, rai : ahb_reg_type;
178
signal rbdrive, ribdrive : std_logic_vector(31 downto 0);
179
signal rdata, wdata : std_logic_vector(127 downto 0);
180
signal ddr_rst : std_logic;
181
signal ddr_rst_gen  : std_logic_vector(3 downto 0);
182
attribute syn_preserve : boolean;
183
attribute syn_preserve of rbdrive : signal is true;
184
 
185
begin
186
 
187
   vcc <= '1';
188
 
189
   ddr_rst <= (ddr_rst_gen(3) and ddr_rst_gen(2) and ddr_rst_gen(1) and rst); -- Reset signal in DDR clock domain
190
 
191
   ahb_ctrl : process(rst, ahbsi, r, ra, rdata)
192
   variable v       : ahb_reg_type;  -- local variables for registers
193
   variable startsd : std_ulogic;
194
   variable dout    : std_logic_vector(31 downto 0);
195
   variable ready   : std_logic;
196
   begin
197
 
198
      v := ra; v.hresp := HRESP_OKAY; v.write := "0000";
199
 
200
      case ra.raddr(1 downto 0) is
201
      when "00" => v.hrdata := rdata(127 downto 96);
202
      when "01" => v.hrdata := rdata(95 downto 64);
203
      when "10" => v.hrdata := rdata(63 downto 32);
204
      when others => v.hrdata := rdata(31 downto 0);
205
      end case;
206
 
207
      -- Sync ------------------------------------------------
208
      v.sync(1) := r.startsdold; v.sync(2) := ra.sync(1);
209
      ready := ra.startsd_ack xor ra.sync(2);
210
      --------------------------------------------------------
211
 
212
      if ((ahbsi.hready and ahbsi.hsel(hindex)) = '1') then
213
         v.htrans := ahbsi.htrans; v.haddr := ahbsi.haddr;
214
         v.size := ahbsi.hsize(1 downto 0); v.hwrite := ahbsi.hwrite;
215
         if ahbsi.htrans(1) = '1' then
216
            v.hio := ahbsi.hmbsel(1);
217
            v.hsel := '1'; v.hready := '0';
218
         end if;
219
      end if;
220
 
221
      if ahbsi.hready = '1' then v.hsel := ahbsi.hsel(hindex); end if;
222
 
223
      case ra.state is
224
      when midle =>
225
         if ((v.hsel and v.htrans(1)) = '1') then
226
            if v.hwrite = '0' then
227
               v.state := rhold; v.startsd := not ra.startsd;
228
            else
229
               v.state := dwrite; v.hready := '1';
230
               v.write := decode(v.haddr(3 downto 2));
231
            end if;
232
         end if;
233
         v.raddr := ra.haddr(7 downto 2);
234
         if ahbsi.hready = '1' then
235
            v.acc := (v.haddr, v.size, v.hwrite, v.hio);
236
         end if;
237
      when rhold =>
238
         v.raddr := ra.haddr(7 downto 2);
239
         if ready = '1' then
240
            v.state := dread; v.hready := '1'; v.raddr := ra.raddr + 1;
241
         end if;
242
      when dread =>
243
         v.raddr := ra.raddr + 1; v.hready := '1';
244
         if ((v.hsel and v.htrans(1) and v.htrans(0)) = '0')
245
            or (ra.raddr(2 downto 0) = "000") then
246
               v.state := midle; v.hready := '0';
247
               v.startsd_ack := ra.startsd;
248
         end if;
249
         v.acc := (v.haddr, v.size, v.hwrite, v.hio);
250
      when dwrite =>
251
         v.raddr := ra.haddr(7 downto 2); v.hready := '1';
252
         v.write := decode(v.haddr(3 downto 2));
253
         if ((v.hsel and v.htrans(1) and v.htrans(0)) = '0')
254
            or (ra.haddr(4 downto 2) = "111") then
255
            v.startsd := not ra.startsd; v.state := whold1;
256
            v.write := "0000"; v.hready := '0';
257
         end if;
258
      when whold1 =>
259
         v.state := whold2;
260
      when whold2 =>
261
         if ready = '1' then
262
            v.state := midle; v.acc := (v.haddr, v.size, v.hwrite, v.hio);
263
            v.startsd_ack := ra.startsd;
264
         end if;
265
      end case;
266
 
267
      v.hwdata := ahbsi.hwdata;
268
 
269
      if (ahbsi.hready and ahbsi.hsel(hindex) ) = '1' then
270
         if ahbsi.htrans(1) = '0' then v.hready := '1'; end if;
271
      end if;
272
 
273
      dout := ra.hrdata(31 downto 0);
274
 
275
      if rst = '0' then
276
         v.hsel         := '0';
277
         v.hready       := '1';
278
         v.state        := midle;
279
         v.startsd      := '0';
280
         v.startsd_ack  := '0';
281
         v.hio          := '0';
282
      end if;
283
 
284
      rai <= v;
285
      ahbso.hready  <= ra.hready;
286
      ahbso.hresp   <= ra.hresp;
287
      ahbso.hrdata  <= dout;
288
      ahbso.hcache  <= not ra.hio;
289
 
290
   end process;
291
 
292
   ddr_ctrl : process(ddr_rst, r, ra, sdi, rbdrive, wdata)
293
   variable v        : ddr_reg_type;   -- local variables for registers
294
   variable startsd  : std_ulogic;
295
   variable dqm      : std_logic_vector(15 downto 0);
296
   variable raddr    : std_logic_vector(13 downto 0);
297
   variable adec     : std_ulogic;
298
   variable rams     : std_logic_vector(1 downto 0);
299
   variable ba       : std_logic_vector(1 downto 0);
300
   variable haddr    : std_logic_vector(31 downto 0);
301
   variable hsize    : std_logic_vector(1 downto 0);
302
   variable hwrite   : std_ulogic;
303
   variable htrans   : std_logic_vector(1 downto 0);
304
   variable hready   : std_ulogic;
305
   variable vbdrive  : std_logic_vector(31 downto 0);
306
   variable bdrive   : std_ulogic;
307
   variable writecfg : std_ulogic;
308
   variable regsd1   : std_logic_vector(31 downto 0);   -- data from registers
309
   variable regsd2   : std_logic_vector(31 downto 0);   -- data from registers
310
   variable regsd3   : std_logic_vector(31 downto 0);   -- data from registers
311
   begin
312
 
313
-- Variable default settings to avoid latches
314
 
315
      v := r; v.hready := '0'; writecfg := '0'; vbdrive := rbdrive;
316
      v.hrdata := sdi.data; v.qdrive :='0';
317
      v.cfg.cal_en    :=  (others => '0'); v.cfg.cal_inc   :=  (others => '0');
318
      v.cfg.cal_pll   :=  (others => '0');                                            -- *** ??? pll_reconf
319
      v.cfg.cal_rst   :=  '0';
320
      v.wdata := wdata; -- pipeline output data
321
      v.dqm_dly := r.dqm;                                                             -- *** ??? delay ctrl
322
 
323
      regsd1 := (others => '0');
324
      regsd1(31 downto 15) := r.cfg.refon & r.cfg.ocd & r.cfg.emr & '0' & r.cfg.trcd &
325
                              r.cfg.bsize & r.cfg.csize & r.cfg.command &
326
                              r.cfg.dllrst & r.cfg.renable & r.cfg.cke;
327
      regsd1(11 downto 0) := r.cfg.refresh;
328
      regsd2 := (others => '0');
329
      regsd2(8 downto 0) := conv_std_logic_vector(MHz, 9);
330
      regsd2(14 downto 12) := conv_std_logic_vector(3, 3);
331
      regsd3 := (others => '0');
332
      regsd3(17 downto 16) := r.cfg.readdly;
333
      regsd3(22 downto 18) := r.cfg.trfc;
334
      regsd3(27 downto 23) := r.cfg.twr;
335
      regsd3(28) := r.cfg.trp;
336
 
337
-- generate DQM from address and write size
338
 
339
      case ra.acc.size is
340
      when "00" =>
341
         case ra.acc.haddr(3 downto 0) is
342
         when "0000" => dqm := "0111111111111111";
343
         when "0001" => dqm := "1011111111111111";
344
         when "0010" => dqm := "1101111111111111";
345
         when "0011" => dqm := "1110111111111111";
346
         when "0100" => dqm := "1111011111111111";
347
         when "0101" => dqm := "1111101111111111";
348
         when "0110" => dqm := "1111110111111111";
349
         when "0111" => dqm := "1111111011111111";
350
         when "1000" => dqm := "1111111101111111";
351
         when "1001" => dqm := "1111111110111111";
352
         when "1010" => dqm := "1111111111011111";
353
         when "1011" => dqm := "1111111111101111";
354
         when "1100" => dqm := "1111111111110111";
355
         when "1101" => dqm := "1111111111111011";
356
         when "1110" => dqm := "1111111111111101";
357
         when others => dqm := "1111111111111110";
358
         end case;
359
      when "01" =>
360
         case ra.acc.haddr(3 downto 1) is
361
         when "000"  => dqm := "0011111111111111";
362
         when "001"  => dqm := "1100111111111111";
363
         when "010"  => dqm := "1111001111111111";
364
         when "011"  => dqm := "1111110011111111";
365
         when "100"  => dqm := "1111111100111111";
366
         when "101"  => dqm := "1111111111001111";
367
         when "110"  => dqm := "1111111111110011";
368
         when others => dqm := "1111111111111100";
369
         end case;
370
      when others =>
371
         dqm := "0000000000000000";
372
      end case;
373
 
374
      -- Sync ------------------------------------------
375
      v.sync := ra.startsd; v.startsd := r.sync;
376
      --------------------------------------------------
377
 
378
      --v.startsd := ra.startsd;
379
 
380
---- main FSM
381
--
382
--      case r.mstate is
383
--      when midle =>
384
--         if  r.startsd = '1' then
385
--            if (r.sdstate = sidle) and (r.cfg.command = "000")
386
--               and (r.cmstate = midle) then
387
--                  startsd := '1'; v.mstate := active;
388
--            end if;
389
--         end if;
390
--      when others => null;
391
--      end case;
392
 
393
      startsd := r.startsd xor r.startsdold;
394
 
395
-- generate row and column address size
396
 
397
      haddr := ra.acc.haddr;
398
      haddr(31 downto 20) := haddr(31 downto 20) and not conv_std_logic_vector(hmask, 12);
399
      case r.cfg.csize is
400
      when "00" => raddr := haddr(25 downto 12);
401
      when "01" => raddr := haddr(26 downto 13);
402
      when "10" => raddr := haddr(27 downto 14);
403
      when others => raddr := haddr(28 downto 15);
404
      end case;
405
 
406
-- generate bank address
407
 
408
      ba := genmux(r.cfg.bsize, haddr(29 downto 22)) &
409
            genmux(r.cfg.bsize, haddr(28 downto 21));
410
 
411
-- generate chip select
412
 
413
      adec := genmux(r.cfg.bsize, haddr(30 downto 23));
414
 
415
      rams := adec & not adec;
416
 
417
-- sdram access FSM
418
 
419
      if r.trfc /= "00000" then v.trfc := r.trfc - 1; end if;
420
 
421
      case r.sdstate is
422
      when sidle =>
423
         if (startsd = '1') and (r.cfg.command = "000") and (r.cmstate = midle)
424
            and (r.istate = finish) then
425
               v.address := raddr; v.ba := ba;
426
               if ra.acc.hio = '0' then
427
                  v.sdcsn := not rams(1 downto 0); v.rasn := '0'; v.sdstate := act1;
428
               else v.sdstate := ioreg1; end if;
429
         end if;
430
         v.waddr := ra.acc.haddr(7 downto 2);
431
      when act1 =>
432
         v.rasn := '1'; v.trfc := r.cfg.trfc;
433
         if r.cfg.trcd = '1' then v.sdstate := act2;
434
         else v.sdstate := act3;
435
         end if;
436
         v.waddr := ra.acc.haddr(7 downto 2);
437
         v.waddr_d := ra.acc.haddr(7 downto 2);
438
         if ra.acc.hwrite = '1' then
439
           if odten /= 0 then v.odt := (others => '1'); end if; -- *** ??? odt
440
         end if;
441
      when act2 =>
442
         v.sdstate := act3;
443
      when act3 =>
444
         v.casn := '0';
445
         v.address := ra.acc.haddr(15 downto 13) & '0' & ra.acc.haddr(12 downto 4) & '0';
446
         v.hready := ra.acc.hwrite;
447
         if ra.acc.hwrite = '1' then
448
            v.sdstate := wr0;
449
            v.sdwen := '0';
450
            --v.waddr := r.waddr + 4; v.waddr(1 downto 0) := "00"; -- inc address to memory, needed because data is pipelined -- *** ??? delay ctrl
451
            v.trfc := r.cfg.twr;
452
         else v.sdstate := rd1; end if;
453
      when wr0 =>
454
         v.casn := '1'; v.sdwen := '1'; v.bdrive := '0'; v.qdrive := '1';
455
         v.dqm := dqm;
456
         v.waddr_d := r.waddr_d + 4; v.waddr_d(1 downto 0) := "00";
457
         v.waddr := r.waddr + 4;
458
         v.sdstate := wr1;
459
         if (r.waddr_d /= ra.raddr) then v.hready := '1';
460
            if (r.waddr_d(5 downto 2) = ra.raddr(5 downto 2)) then
461
               if r.waddr_d(1) = '1' then v.dqm(15 downto 8) := (others => '1');
462
               else
463
                  case ra.raddr(1 downto 0) is
464
                  when "01" => v.dqm(7 downto 0) := (others => '1');
465
                  when "10" => v.dqm(3 downto 0) := (others => '1');
466
                               v.dqm(15 downto 12) := (others => r.waddr_d(0));
467
                  when others => v.dqm(15 downto 12) := (others => r.waddr_d(0));
468
                  end case;
469
               end if;
470
            else
471
               case r.waddr_d(1 downto 0) is
472
               when "01" => v.dqm(15 downto 12) := (others => '1');
473
               when "10" => v.dqm(15 downto 8) := (others => '1');
474
               when "11" => v.dqm(15 downto 4) := (others => '1');
475
               when others => null;
476
               end case;
477
            end if;
478
         else
479
            case r.waddr_d(1 downto 0) is
480
            when "00" => v.dqm(11 downto 0) := (others => '1');
481
            when "01" => v.dqm(15 downto 12) := (others => '1'); v.dqm(7 downto 0) := (others => '1');
482
            when "10" => v.dqm(15 downto 8) := (others => '1'); v.dqm(3 downto 0) := (others => '1');
483
            when others => v.dqm(15 downto 4) := (others => '1');
484
            end case;
485
         end if;
486
      when wr1 =>
487
         v.sdwen := '1';  v.casn := '1';  v.qdrive := '1';
488
         v.waddr_d := r.waddr_d + 4; v.dqm := (others => '0');
489
         v.waddr := r.waddr + 4;
490
         v.address(8 downto 3) := r.waddr_d;
491
         if (r.waddr_d <= ra.raddr) and (r.waddr_d(5 downto 2) /= "0000") and (r.hready = '1') then
492
            v.hready := '1';
493
            if (r.hready = '1') and (r.waddr_d(2 downto 0) = "000") then
494
               v.sdwen := '0'; v.casn := '0';
495
            end if;
496
            if  (r.waddr_d(5 downto 2) = ra.raddr(5 downto 2)) and (r.waddr_d /= "000000") then
497
               case ra.raddr(1 downto 0) is
498
               when "00" => v.dqm(11 downto 0) := (others => '1');
499
               when "01" => v.dqm(7 downto 0) := (others => '1');
500
               when "10" => v.dqm(3 downto 0) := (others => '1');
501
               when others => null;
502
               end case;
503
            end if;
504
         else
505
            v.sdstate := wr2;
506
            v.dqm := (others => '1');
507
            v.startsdold := r.startsd;
508
         end if;
509
      when wr2 =>
510
         v.sdstate := wr3; v.qdrive := '1';
511
      when wr3 =>
512
         v.sdstate := wr4a; v.qdrive := '1';
513
      when wr4a =>
514
         v.bdrive := '1'; v.qdrive := '1';
515
         if r.trfc <= "00000" then -- wait to not violate TWR timing
516
            v.sdstate := wr4b;
517
         end if;
518
      when wr4b =>
519
         v.bdrive := '1';
520
         v.rasn := '0'; v.sdwen := '0'; v.sdstate := wr4; v.qdrive := '1';
521
      when wr4 =>
522
         v.sdcsn := "11"; v.rasn := '1'; v.sdwen := '1';  v.qdrive := '0';
523
         v.sdstate := wr5;
524
      when wr5 =>
525
         v.odt := (others => '0'); -- *** ??? odt
526
         v.sdstate := sidle;
527
      when rd1 =>
528
         v.casn := '1'; v.sdstate := rd7;
529
      when rd7 =>
530
         v.casn := '1'; v.sdstate := rd8;
531
         --v.readdly := r.cfg.readdly;
532
         v.readdly := r.cfg.readdly + 1;                                              -- *** ??? delay ctrl
533
      when rd8 => -- (CL = 3)
534
         v.casn := '1';
535
         if r.readdly = "00" then -- add read delay 
536
            v.sdstate := rd2;
537
         else
538
            v.readdly := r.readdly - 1;
539
         end if;
540
      when rd2 =>
541
         v.casn := '1'; v.sdstate := rd3;
542
      when rd3 =>
543
         v.sdstate := rd4; v.hready := '1'; v.casn := '1';
544
         if fast = 0 then v.startsdold := r.startsd; end if;
545
         if v.hready = '1' then v.waddr := r.waddr + 4; end if;
546
      when rd4 =>
547
         v.hready := '1'; v.casn := '1';
548
         if (r.sdcsn = "11") or (r.waddr(2 downto 2) = "1") then
549
            v.dqm := (others => '1');
550
            if fast /= 0 then v.startsdold := r.startsd; end if;
551
            if (r.sdcsn /= "11") then
552
               v.rasn := '0'; v.sdwen := '0'; v.sdstate := rd5;
553
            else
554
               if r.cfg.trp = '1' then v.sdstate := rd6;
555
               else v.sdstate := sidle; end if;
556
            end if;
557
         end if;
558
         if v.hready = '1' then v.waddr := r.waddr + 4; end if;
559
      when rd5 =>
560
         if r.cfg.trp = '1' then v.sdstate := rd6;
561
         else v.sdstate := sidle; end if;
562
         v.sdcsn := (others => '1'); v.rasn := '1'; v.sdwen := '1';
563
         v.dqm := (others => '1');
564
      when rd6 =>
565
         v.sdstate := sidle; v.dqm := (others => '1');
566
         v.sdcsn := (others => '1'); v.rasn := '1'; v.sdwen := '1';
567
      when ioreg1 =>
568
         v.hrdata(127 downto 32) := regsd1 & regsd2 & regsd3; v.sdstate := ioreg2;
569
         if ra.acc.hwrite = '0' then v.hready := '1'; end if;
570
      when ioreg2 =>
571
         writecfg := ra.acc.hwrite; v.startsdold := r.startsd;
572
         v.sdstate := sidle;
573
      when others =>
574
         v.sdstate := sidle;
575
      end case;
576
 
577
-- sdram commands
578
 
579
      case r.cmstate is
580
      when midle =>
581
         if r.sdstate = sidle then
582
            case r.cfg.command is
583
            when CMD_PRE => -- precharge
584
               v.sdcsn := (others => '0'); v.rasn := '0'; v.sdwen := '0';
585
               v.address(12) := '1'; v.cmstate := active;
586
            when CMD_REF => -- auto-refresh
587
               v.sdcsn := (others => '0'); v.rasn := '0'; v.casn := '0';
588
               v.cmstate := active;
589
            when CMD_EMR => -- load-ext-mode-reg
590
               v.sdcsn := (others => '0'); v.rasn := '0'; v.casn := '0';
591
               v.sdwen := '0'; v.cmstate := active; v.ba := r.cfg.emr; --v.ba select EM register;
592
               --v.address := "0000"&r.cfg.ocd&r.cfg.ocd&r.cfg.ocd&"0000000"; 
593
               if r.cfg.emr = "01" then
594
                 v.address := "0000"&r.cfg.ocd&r.cfg.ocd&r.cfg.ocd
595
                              & odtvalue(1)&"000"&odtvalue(0)&"00";
596
               else
597
                 v.address := "0000"&r.cfg.ocd&r.cfg.ocd&r.cfg.ocd&"0000000";
598
               end if;
599
            when CMD_LMR => -- load-mode-reg
600
               v.sdcsn := (others => '0'); v.rasn := '0'; v.casn := '0';
601
               v.sdwen := '0'; v.cmstate := active; v.ba := "00";
602
               v.address := "00010" & r.cfg.dllrst & "0" & "01" & "10010";  -- CAS = 3 WR = 3
603
            when others => null;
604
            end case;
605
         end if;
606
      when active =>
607
         v.sdcsn := (others => '1'); v.rasn := '1'; v.casn := '1';
608
         v.sdwen := '1'; v.cfg.command := "000";
609
         v.cmstate := leadout; v.trfc := r.cfg.trfc;
610
      when others =>
611
         if r.trfc = "00000" then v.cmstate := midle; end if;
612
      end case;
613
 
614
-- sdram init
615
 
616
      case r.istate is
617
      when iidle =>
618
         if r.cfg.renable = '1' then
619
            v.cfg.cke := '1'; v.cfg.dllrst := '1';
620
            v.ba := "00"; v.cfg.ocd := '0'; v.cfg.emr := "10"; -- EMR(2)
621
            if r.cfg.cke = '1' then
622
               if r.initnopdly = "00000000" then -- 400 ns of NOP and CKE
623
                  v.istate := pre; v.cfg.command := CMD_PRE;
624
               else
625
                  v.initnopdly := r.initnopdly - 1;
626
               end if;
627
            end if;
628
         end if;
629
      when pre =>
630
         if r.cfg.command = "000" then
631
            v.cfg.command := "11" & r.cfg.dllrst; -- CMD_LMR/CMD_EMR 
632
            if r.cfg.dllrst = '1' then v.istate := emode23; else v.istate := lmode; end if;
633
         end if;
634
      when emode23 =>
635
         if r.cfg.command = "000" then
636
            if r.cfg.emr = "11" then
637
               v.cfg.emr := "01"; -- (EMR(1))
638
               v.istate := emode; v.cfg.command := CMD_EMR;
639
            else
640
               v.cfg.emr := "11"; v.cfg.command := CMD_EMR; -- EMR(3)
641
            end if;
642
         end if;
643
      when emode =>
644
         if r.cfg.command = "000" then
645
            v.istate := lmode; v.cfg.command := CMD_LMR;
646
         end if;
647
      when lmode =>
648
         if r.cfg.command = "000" then
649
            if r.cfg.dllrst = '1' then
650
               if r.refresh(9 downto 8) = "00" then -- > 200 clocks delay
651
                  v.cfg.command := CMD_PRE; v.istate := ref1;
652
               end if;
653
            else
654
               v.istate := emodeocd;
655
               v.cfg.ocd := '1'; v.cfg.command := CMD_EMR;
656
            end if;
657
         end if;
658
      when ref1 =>
659
         if r.cfg.command = "000" then
660
            v.cfg.command := CMD_REF; v.cfg.dllrst := '0'; v.istate := ref2;
661
         end if;
662
      when ref2 =>
663
         if r.cfg.command = "000" then
664
            v.cfg.command := CMD_REF; v.istate := pre;
665
         end if;
666
      when emodeocd =>
667
         if r.cfg.command = "000" then
668
            if r.cfg.ocd = '0' then -- Exit OCD
669
               v.istate := finish;
670
               v.cfg.refon := '1'; v.cfg.renable := '0';
671
            else                    -- Default OCD
672
               v.cfg.ocd := '0';
673
               v.cfg.command := CMD_EMR;
674
            end if;
675
         end if;
676
         v.cfg.cal_rst := '1'; -- reset data bit delay
677
      when others =>
678
         --if odten /= 0 then v.odt := (others => '1'); end if; -- *** ??? odt
679
         if r.cfg.renable = '1' then
680
            v.istate := iidle; v.cfg.dllrst := '1';
681
            v.initnopdly := (others => '1');
682
            v.odt := (others => '0');
683
         end if;
684
      end case;
685
 
686
---- second part of main fsm
687
--
688
--      case r.mstate is
689
--      when active =>
690
--         if v.hready = '1' then
691
--            v.mstate := midle;
692
--         end if;
693
--      when others => null;
694
--      end case;
695
 
696
-- sdram refresh counter
697
 
698
      if ((r.cfg.refon = '1') and (r.istate = finish)) or (r.cfg.dllrst = '1') then
699
         v.refresh := r.refresh - 1;
700
         if (v.refresh(11) and not r.refresh(11))  = '1' then
701
            v.refresh := r.cfg.refresh;
702
            if r.cfg.dllrst = '0' then v.cfg.command := "100"; end if;
703
         end if;
704
      end if;
705
 
706
-- AHB register access
707
 
708
      if (ra.acc.hio and ra.acc.hwrite and writecfg) = '1' then
709
         if r.waddr(1 downto 0) = "00" then
710
            v.cfg.refresh   :=  r.wdata(11+96 downto 0+96);
711
            v.cfg.cke       :=  r.wdata(15+96);
712
            v.cfg.renable   :=  r.wdata(16+96);
713
            v.cfg.dllrst    :=  r.wdata(17+96);
714
            v.cfg.command   :=  r.wdata(20+96 downto 18+96);
715
            v.cfg.csize     :=  r.wdata(22+96 downto 21+96);
716
            v.cfg.bsize     :=  r.wdata(25+96 downto 23+96);
717
            v.cfg.trcd      :=  r.wdata(26+96);
718
            v.cfg.emr       :=  r.wdata(29+96 downto 28+96);
719
            v.cfg.ocd       :=  r.wdata(30+96);
720
            v.cfg.refon     :=  r.wdata(31+96);
721
         elsif r.waddr(1 downto 0) = "10" then
722
            v.cfg.cal_en    :=  r.wdata( 7+32 downto  0+32);
723
            v.cfg.cal_inc   :=  r.wdata(15+32 downto  8+32);
724
            v.cfg.readdly   :=  r.wdata(17+32 downto 16+32);
725
            v.cfg.trfc      :=  r.wdata(22+32 downto 18+32);
726
            v.cfg.twr       :=  r.wdata(27+32 downto 23+32);
727
            v.cfg.trp       :=  r.wdata(28+32);
728
            v.cfg.cal_pll   :=  r.wdata(30+32 downto 29+32);                              -- *** ??? pll_reconf
729
            v.cfg.cal_rst   :=  r.wdata(31+32);
730
         end if;
731
      end if;
732
 
733
      v.nbdrive := not v.bdrive;
734
 
735
      if oepol = 1 then bdrive := r.nbdrive; vbdrive := (others => v.nbdrive);
736
      else bdrive := r.bdrive; vbdrive := (others => v.bdrive);end if;
737
 
738
-- reset
739
 
740
      if ddr_rst = '0' then
741
         v.sdstate      := sidle;
742
         v.mstate       := midle;
743
         v.istate       := finish;
744
         v.cmstate      := midle;
745
         v.cfg.command  := "000";
746
         v.cfg.csize    := conv_std_logic_vector(col-9, 2);
747
         v.cfg.bsize    := conv_std_logic_vector(log2(Mbyte/8), 3);
748
         v.cfg.refon    := '0';
749
         v.cfg.trfc     := conv_std_logic_vector(TRFC*MHz/1000-2, 5);
750
         v.cfg.refresh  := conv_std_logic_vector(7800*MHz/1000, 12);
751
         v.cfg.twr      := conv_std_logic_vector((15)*MHz/1000+3, 5);
752
         v.refresh      :=  (others => '0');
753
         v.dqm          := (others => '1');
754
         v.sdwen        := '1';
755
         v.rasn         := '1';
756
         v.casn         := '1';
757
         v.hready       := '0';
758
         v.startsd      := '0';
759
         v.startsdold   := '0';
760
         v.cfg.dllrst   := '0';
761
         v.cfg.cke      := '0';
762
         v.cfg.ocd          := '0';
763
         v.cfg.readdly  := conv_std_logic_vector(readdly, 2);
764
         v.initnopdly   := (others => '1');
765
         if MHz > 130 then v.cfg.trcd :=  '1'; else v.cfg.trcd :=  '0'; end if;
766
         if MHz > 130 then v.cfg.trp := '1'; else v.cfg.trp := '0'; end if;
767
         if pwron = 1 then v.cfg.renable :=  '1';
768
         else v.cfg.renable :=  '0'; end if;
769
         v.odt          := (others => '0');
770
      end if;
771
 
772
      if oepol = 1 then v.sdo_bdrive := r.nbdrive;            -- *** ??? delay ctrl
773
      else v.sdo_bdrive := r.bdrive; end if;
774
      v.sdo_qdrive := not (v.qdrive or r.nbdrive);
775
 
776
      ri <= v;
777
      ribdrive <= vbdrive;
778
 
779
   end process;
780
 
781
   sdo.sdcke     <= (others => r.cfg.cke);
782
   ahbso.hconfig <= hconfig;
783
   ahbso.hirq    <= (others => '0');
784
   ahbso.hindex  <= hindex;
785
 
786
   ahbregs : process(clk_ahb) begin
787
      if rising_edge(clk_ahb) then
788
         ra <= rai;
789
      end if;
790
   end process;
791
 
792
   ddrregs : process(clk_ddr, rst, ddr_rst) begin
793
      if rising_edge(clk_ddr) then
794
         r <= ri; rbdrive <= ribdrive;
795
         ddr_rst_gen <= ddr_rst_gen(2 downto 0) & '1';
796
      end if;
797
      if (rst = '0') then
798
         ddr_rst_gen <= "0000";
799
      end if;
800
      if (ddr_rst = '0') then
801
         r.sdcsn  <= (others => '1'); r.bdrive <= '1'; r.nbdrive <= '0';
802
         if oepol = 0 then rbdrive <= (others => '1');
803
         else rbdrive <= (others => '0'); end if;
804
         r.cfg.cke <= '0';
805
         r.odt <= (others => '0');
806
      end if;
807
   end process;
808
 
809
   sdo.address  <= '0' & r.address; --'0' & ri.address;                     -- *** ??? delay ctrl
810
   sdo.ba       <= r.ba; --ri.ba;                                           -- *** ??? delay ctrl
811
   sdo.bdrive   <= r.sdo_bdrive; --r.nbdrive when oepol = 1 else r.bdrive;  -- *** ??? delay ctrl
812
   sdo.qdrive   <= r.sdo_qdrive; --not (ri.qdrive or r.nbdrive);            -- *** ??? delay ctrl
813
   sdo.vbdrive  <= rbdrive;
814
   sdo.sdcsn    <= r.sdcsn; --ri.sdcsn;                                     -- *** ??? delay ctrl
815
   sdo.sdwen    <= r.sdwen; --ri.sdwen;                                     -- *** ??? delay ctrl
816
   sdo.dqm      <= r.dqm_dly; --r.dqm;                                      -- *** ??? delay ctrl
817
   sdo.rasn     <= r.rasn; --ri.rasn;                                       -- *** ??? delay ctrl
818
   sdo.casn     <= r.casn; --ri.casn;                                       -- *** ??? delay ctrl
819
   --sdo.data     <= wdata;
820
   sdo.data     <= r.wdata; -- data pipelined
821
   sdo.cal_en   <= r.cfg.cal_en;
822
   sdo.cal_inc  <= r.cfg.cal_inc;
823
   sdo.cal_pll  <= r.cfg.cal_pll;                                           -- *** ??? pll_reconf
824
   sdo.cal_rst  <= r.cfg.cal_rst;
825
   sdo.odt      <= r.odt;
826
 
827
   read_buff : syncram_2p
828
   generic map (tech => memtech, abits => 4, dbits => 128, sepclk => 1, wrfst => 0)
829
   port map ( rclk => clk_ahb, renable => vcc, raddress => rai.raddr(5 downto 2),
830
              dataout => rdata, wclk => clk_ddr, write => ri.hready,
831
              waddress => r.waddr(5 downto 2), datain => ri.hrdata);
832
 
833
   write_buff1 : syncram_2p
834
   generic map (tech => memtech, abits => 4, dbits => 32, sepclk => 1, wrfst => 0)
835
   port map ( rclk => clk_ddr, renable => vcc, raddress => r.waddr(5 downto 2),
836
              dataout => wdata(127 downto 96), wclk => clk_ahb, write => ra.write(0),
837
              waddress => ra.haddr(7 downto 4), datain => ahbsi.hwdata);
838
 
839
   write_buff2 : syncram_2p
840
   generic map (tech => memtech, abits => 4, dbits => 32, sepclk => 1, wrfst => 0)
841
   port map ( rclk => clk_ddr, renable => vcc, raddress => r.waddr(5 downto 2),
842
              dataout => wdata(95 downto 64), wclk => clk_ahb, write => ra.write(1),
843
              waddress => ra.haddr(7 downto 4), datain => ahbsi.hwdata);
844
 
845
   write_buff3 : syncram_2p
846
   generic map (tech => memtech, abits => 4, dbits => 32, sepclk => 1, wrfst => 0)
847
   port map ( rclk => clk_ddr, renable => vcc, raddress => r.waddr(5 downto 2),
848
              dataout => wdata(63 downto 32), wclk => clk_ahb, write => ra.write(2),
849
              waddress => ra.haddr(7 downto 4), datain => ahbsi.hwdata);
850
 
851
   write_buff4 : syncram_2p
852
   generic map (tech => memtech, abits => 4, dbits => 32, sepclk => 1, wrfst => 0)
853
   port map ( rclk => clk_ddr, renable => vcc, raddress => r.waddr(5 downto 2),
854
              dataout => wdata(31 downto 0), wclk => clk_ahb, write => ra.write(3),
855
              waddress => ra.haddr(7 downto 4), datain => ahbsi.hwdata);
856
 
857
-- pragma translate_off
858
   bootmsg : report_version
859
   generic map (
860
      msg1 => "ddr2sp" & tost(hindex) & ": 64-bit DDR2 controller rev " &
861
              tost(REVISION) & ", " & tost(Mbyte) & " Mbyte, " & tost(MHz) &
862
              " MHz DDR clock");
863
-- pragma translate_on
864
 
865
end;
866
 

powered by: WebSVN 2.1.0

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