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

Subversion Repositories layer2

[/] [layer2/] [trunk/] [vhdl/] [ddr/] [rtl/] [ddr.vhd] - Blame information for rev 2

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 idiolatrie
--------------------------------------------------------------------------------
2
-- Mycron® DDR SDRAM - MT46V32M16 - 8 Meg x 16 x 4 banks                      --
3
--------------------------------------------------------------------------------
4
-- Copyright (C)2012  Mathias Hörtnagl <mathias.hoertnagl@gmail.comt>         --
5
--                                                                            --
6
-- This program is free software: you can redistribute it and/or modify       --
7
-- it under the terms of the GNU General Public License as published by       --
8
-- the Free Software Foundation, either version 3 of the License, or          --
9
-- (at your option) any later version.                                        --
10
--                                                                            --
11
-- This program is distributed in the hope that it will be useful,            --
12
-- but WITHOUT ANY WARRANTY; without even the implied warranty of             --
13
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              --
14
-- GNU General Public License for more details.                               --
15
--                                                                            --
16
-- You should have received a copy of the GNU General Public License          --
17
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.      --
18
--------------------------------------------------------------------------------
19
library ieee;
20
use ieee.std_logic_1164.all;
21
use ieee.numeric_std.all;
22
 
23
library UNISIM;
24
use UNISIM.vcomponents.all;
25
 
26
library work;
27
use work.iwb.all;
28
use work.iddr.all;
29
 
30
entity ddr is
31
   port (
32
      si       : in    slave_in_t;
33
      so       : out   slave_out_t;
34
   -- Non Wishbone Signals
35
      clk0     : in    std_logic;
36
      clk90    : in    std_logic;
37
      SD_CK_N  : out   std_logic;
38
      SD_CK_P  : out   std_logic;
39
      SD_CKE   : out   std_logic;
40
      SD_BA    : out   std_logic_vector(1 downto 0);
41
      SD_A     : out   std_logic_vector(12 downto 0);
42
      SD_CMD   : out   std_logic_vector(3 downto 0);
43
      SD_DM    : out   std_logic_vector(1 downto 0);
44
      SD_DQS   : inout std_logic_vector(1 downto 0);
45
      SD_DQ    : inout std_logic_vector(15 downto 0)
46
   );
47
end ddr;
48
 
49
architecture rtl of ddr is
50
 
51
   -----------------------------------------------------------------------------
52
   -- General                                                                 --
53
   -----------------------------------------------------------------------------
54
   -- Average periodic refresh interval tREFI: 7.8 µs
55
   constant AR_RATE : natural := 160; -- x 40 ns = 5.8 µs.
56
 
57
   -----------------------------------------------------------------------------
58
   -- Controller Commands                                                     --
59
   -----------------------------------------------------------------------------
60
   constant CMD_AUTO_REFRESH : std_logic_vector(3 downto 0) := "0001";
61
   constant CMD_PRECHARGE    : std_logic_vector(3 downto 0) := "0010";
62
   constant CMD_ACTIVE       : std_logic_vector(3 downto 0) := "0011";
63
   constant CMD_WRITE        : std_logic_vector(3 downto 0) := "0100";
64
   constant CMD_READ         : std_logic_vector(3 downto 0) := "0101";
65
   constant CMD_NOP          : std_logic_vector(3 downto 0) := "0111";
66
 
67
   -----------------------------------------------------------------------------
68
   -- Wishbone Controller                                                     --
69
   -----------------------------------------------------------------------------
70
   type wb_state_t is (
71
      Initialize,                            -- Initialization.
72
      Idle,                                  -- Wait for user or autorefresh.
73
      Ack                                    -- WB wait for ack.
74
   );
75
 
76
   signal w, win  : wb_state_t := Initialize;
77
 
78
   signal ddr_done : boolean;                -- Successful read or wirte.
79
   signal read_wb  : boolean;                -- Pending WB read.
80
   signal write_wb : boolean;                -- Pending WB write.
81
 
82
   -----------------------------------------------------------------------------
83
   -- Main Controller                                                         --
84
   -----------------------------------------------------------------------------
85
   type main_state_t is (
86
      Initialize,                            -- Initialization.
87
      Idle,                                  -- Wait for user or autorefresh.
88
      AutoRefresh, AutoRefreshWait,          -- Autorefresh when idle.
89
      Active, ActiveWait,                    -- Activate Row.
90
      Write, RecoverWrite,                   -- Write 32 bit.
91
      Read, WaitRead,                        -- Read 32 bit.
92
      PrechargeWait,                         -- Wait for precharge after Write.
93
      Ack                                    -- WB wait for ack.
94
   );
95
 
96
   type main_t is record
97
      s    : main_state_t;
98
      c    : natural range 0 to 7;
99
      a    : natural range 0 to AR_RATE-1;    -- Auto refresh counter.
100
      rfsh : boolean;                         -- Pending autorefresh.
101
      cmd  : std_logic_vector(3 downto 0);    -- SD_CS SD_RAS SD_CAS SD_WE.
102
      ba   : std_logic_vector(1 downto 0);    -- DDR bank address.
103
      adr  : std_logic_vector(12 downto 0);   -- DDR address bus.
104
   end record;
105
 
106
   constant main_d : main_t :=
107
      main_t'(Initialize, 0, 0, false, CMD_NOP, "00", (others => '0') );
108
 
109
   signal m, min  : main_t := main_d;
110
 
111
   signal dq      : std_logic_vector(15 downto 0);    -- Data tb be written.
112
   signal dqs     : std_logic_vector(1 downto 0);     -- Data strobe signal.
113
   signal dm      : std_logic_vector(1 downto 0);     -- Data mask signal.
114
   signal mask    : std_logic_vector(3 downto 0);
115
 
116
   signal wr_en   : boolean;
117
   signal wr_en2  : boolean;
118
 
119
   signal rd      : std_logic_vector(31 downto 0);    -- Read data latch.
120
   signal rd_en   : boolean;                          -- Read latch enable.
121
   signal rd_en2  : boolean;
122
 
123
 
124
   -----------------------------------------------------------------------------
125
   -- Initialization                                                          --
126
   -----------------------------------------------------------------------------
127
   component ddr_init is
128
      port (
129
         clk0      : in  std_logic;
130
         rst       : in  std_logic;
131
         SD_CKE    : out std_logic;
132
         SD_BA     : out std_logic_vector(1 downto 0);
133
         SD_A      : out std_logic_vector(12 downto 0);
134
         SD_CMD    : out std_logic_vector(3 downto 0);
135
         init_done : out boolean
136
      );
137
   end component;
138
 
139
   type init_c is record
140
      cmd  : std_logic_vector(3 downto 0);   -- SD_CS | SD_RAS | SD_CAS | SD_WE.
141
      ba   : std_logic_vector(1 downto 0);   -- DDR bank address.
142
      adr  : std_logic_vector(12 downto 0);  -- DDR address bus.
143
      done : boolean;                        -- True on Init completion.
144
   end record;
145
 
146
   signal init : init_c;
147
begin
148
 
149
   SD_CK_P <= not clk0;
150
   SD_CK_N <= clk0;
151
 
152
 
153
   -----------------------------------------------------------------------------
154
   -- Initialization                                                          --
155
   -----------------------------------------------------------------------------
156
   init_fsm : ddr_init port map(
157
      clk0      => clk0,
158
      rst       => si.rst,
159
      SD_CKE    => SD_CKE,
160
      SD_BA     => init.ba,
161
      SD_A      => init.adr,
162
      SD_CMD    => init.cmd,
163
      init_done => init.done
164
   );
165
 
166
   -----------------------------------------------------------------------------
167
   -- Wishbone Controller                                                     --
168
   -----------------------------------------------------------------------------
169
   -- NOTE: The Whishbone Controller runs at 50 MHz. There is a problem with the
170
   --       communication protocol implementation, which does not allow a master 
171
   --       and a slave running at different frequencies.
172
   --       If this problem happens to be fixed someday, the following state 
173
   --       machine can be deleted and the Wishbone signals can be tied directly
174
   --       into the main state machine.   
175
   wbone : process(w, si, init.done)
176
   begin
177
 
178
      win <= w;
179
 
180
      so.ack   <= '0';
181
      read_wb  <= false;
182
      write_wb <= false;
183
 
184
      case w is
185
         when Initialize =>
186
            if init.done then
187
               win <= Idle;
188
            end if;
189
 
190
         when Idle =>
191
            if wb_read(si) then
192
               read_wb <= true;
193
            elsif wb_write(si) then
194
               write_wb <= true;
195
            end if;
196
            if ddr_done then
197
               win <= Ack;
198
            end if;
199
 
200
         when Ack =>
201
            so.ack <= '1';
202
            if si.stb = '0' then
203
               win <= Idle;
204
            end if;
205
 
206
      end case;
207
   end process;
208
 
209
   wb_reg : process(si.clk)
210
   begin
211
      if rising_edge(si.clk) then
212
         if si.rst = '1' then w <= Initialize; else w <= win; end if;
213
      end if;
214
   end process;
215
 
216
   -----------------------------------------------------------------------------
217
   -- Main Controller                                                         --
218
   -----------------------------------------------------------------------------
219
   -- main : process(m, si, init)
220
   main : process(m, init, read_wb, write_wb)
221
   begin
222
 
223
      min <= m;
224
 
225
      -- Refresh counter.
226
      if m.a = (AR_RATE-1) then
227
         min.rfsh <= true;
228
      else
229
         min.a <= m.a + 1;
230
      end if;
231
 
232
      wr_en  <= false;                 -- Write state machine enable.
233
      rd_en  <= false;                 -- Read state machine enable.
234
      --so.ack <= '0';
235
      ddr_done <= false;               -- Indicates a successful read or wirte.
236
 
237
      case m.s is
238
 
239
         -----------------------------------------------------------------------
240
         -- Initialization (see process initial).                             --
241
         -----------------------------------------------------------------------
242
         when Initialize =>
243
            min.ba  <= init.ba;
244
            min.adr <= init.adr;
245
            min.cmd <= init.cmd;
246
            if init.done then
247
               min.a    <= 0;
248
               min.rfsh <= false;
249
               min.s    <= Idle;
250
            end if;
251
 
252
         -----------------------------------------------------------------------
253
         -- Wait for memory operations or auto refresh.                       --
254
         -----------------------------------------------------------------------
255
         when Idle =>
256
            if m.rfsh then
257
               min.a    <= 0;
258
               min.rfsh <= false;
259
               min.s    <= AutoRefresh;
260
            -- elsif si.stb = '1' then
261
            elsif (read_wb or write_wb) then
262
               min.c <= 0;
263
               min.s <= Active;
264
            end if;
265
 
266
         -----------------------------------------------------------------------
267
         -- Auto Refresh.                                                     --
268
         -----------------------------------------------------------------------
269
         when AutoRefresh =>
270
            min.cmd <= CMD_AUTO_REFRESH;
271
            min.c   <= 0;
272
            min.s   <= AutoRefreshWait;
273
 
274
         -- AUTO REFRESH command period tRFC: 72ns
275
         -- Precharge command cycle + PRECHARGE command period tRP: 15ns
276
         when AutoRefreshWait =>
277
            min.cmd <= CMD_NOP;
278
            if m.c = 1 then
279
               min.c <= 0;
280
               min.s <= Idle;
281
            else
282
               min.c <= m.c + 1;
283
            end if;
284
 
285
         -----------------------------------------------------------------------
286
         -- Activate bank and row.                                            --
287
         -----------------------------------------------------------------------
288
         when Active =>
289
            min.cmd <= CMD_ACTIVE;
290
            min.ba  <= si.adr(25 downto 24);     -- Select bank.
291
            min.adr <= si.adr(23 downto 11);     -- Select row.
292
            min.s   <= ActiveWait;
293
 
294
         -- ACTIVE-to-READ or WRITE delay tRCD: 15ns
295
         when ActiveWait =>
296
            min.cmd <= CMD_NOP;
297
            min.ba  <= "00";                 -- Select bank.
298
            min.adr <= (others => '0');      -- Select row.
299
            -- if si.we = '0' then
300
               -- min.s <= Read;
301
            -- else
302
               -- min.s <= Write;
303
            -- end if;
304
            if read_wb then
305
               min.s <= Read;
306
            elsif write_wb then
307
               min.s <= Write;
308
            end if;
309
 
310
         -----------------------------------------------------------------------
311
         -- Read.                                                             --
312
         -----------------------------------------------------------------------
313
         -- At burst length 2 and sequential type, SD_A(0) is zero and the
314
         -- ordering of the burst access is 0-1.
315
         when Read =>
316
            min.cmd             <= CMD_READ;
317
            min.ba              <= si.adr(25 downto 24);
318
            min.adr(10)         <= '1';                  -- Auto precharge.
319
            min.adr(9 downto 1) <= si.adr(10 downto 2);
320
            min.s               <= WaitRead;
321
 
322
         -- CL=2
323
         when WaitRead =>
324
            min.cmd             <= CMD_NOP;
325
            min.ba              <= "00";
326
            min.adr(10)         <= '0';
327
            min.adr(9 downto 1) <= (others => '0');
328
            rd_en               <= true;
329
            min.s               <= PrechargeWait;
330
 
331
         -----------------------------------------------------------------------
332
         -- Write.                                                            --
333
         -----------------------------------------------------------------------
334
         -- At burst length 2 and sequential type, SD_A(0) is fixed to zero and
335
         -- the ordering of the burst accesses is 0-1.
336
         when Write =>
337
            min.cmd             <= CMD_WRITE;
338
            min.ba              <= si.adr(25 downto 24);
339
            min.adr(10)         <= '1';                  -- Auto precharge.
340
            min.adr(9 downto 1) <= si.adr(10 downto 2);
341
            wr_en               <= true;
342
            min.s               <= RecoverWrite;
343
 
344
         -- Write recovery time tWR: 15 ns
345
         when RecoverWrite =>
346
            min.cmd             <= CMD_NOP;
347
            min.ba              <= "00";
348
            min.adr(10)         <= '0';
349
            min.adr(9 downto 1) <= (others => '0');
350
            if m.c = 1 then
351
               min.c <= 0;
352
               min.s <= PrechargeWait;
353
            else
354
               min.c <= m.c + 1;
355
            end if;
356
 
357
         -----------------------------------------------------------------------
358
         -- Auto Precharge.                                                   --
359
         -----------------------------------------------------------------------
360
         -- Precharge command cycle + PRECHARGE command period tRP: 15ns
361
         when PrechargeWait =>
362
            if m.c = 1 then
363
               min.c <= 0;
364
               min.s <= Ack;
365
            else
366
               min.c <= m.c + 1;
367
            end if;
368
 
369
         -----------------------------------------------------------------------
370
         -- WB Ack                                                            --
371
         -----------------------------------------------------------------------
372
         -- NOTE: If the WB master needs too much time to pull strobe low, the 
373
         --       DDR lacks an autorefresh as this only happens in Idle state!
374
         when Ack =>
375
            -- so.ack <= '1';
376
            -- if si.stb = '0' then
377
               -- min.s <= Idle;
378
            -- end if;
379
            ddr_done <= true;
380
            min.s    <= Idle;
381
      end case;
382
   end process;
383
 
384
   SD_CMD <= m.cmd;
385
   SD_BA  <= m.ba;
386
   SD_A   <= m.adr;
387
 
388
 
389
   -----------------------------------------------------------------------------
390
   -- Read                                                                    --
391
   -----------------------------------------------------------------------------
392
   rds : process(clk0, rd_en)
393
      type s_t is (Idle, ReadPreamble, Read);
394
      variable s : s_t := Idle;
395
   begin
396
      if falling_edge(clk0) then
397
         if si.rst = '1' then
398
            s := Idle;
399
         else
400
            case s is
401
               when Idle =>
402
                  rd_en2 <= false;
403
                  if rd_en then s := ReadPreamble; end if;
404
 
405
               when ReadPreamble =>
406
                  rd_en2 <= false;
407
                  s      := Read;
408
 
409
               when Read =>
410
                  rd_en2 <= true;
411
                  s      := Idle;
412
            end case;
413
         end if;
414
      end if;
415
   end process;
416
 
417
   process(clk0)
418
   begin
419
      if rising_edge(clk0) then
420
         if rd_en2 then rd(31 downto 16) <= SD_DQ; end if;
421
      end if;
422
   end process;
423
 
424
   process(clk0)
425
   begin
426
      if falling_edge(clk0) then
427
         if rd_en2 then rd(15 downto 0) <= SD_DQ; end if;
428
      end if;
429
   end process;
430
 
431
   so.dat <= rd;
432
 
433
 
434
   -----------------------------------------------------------------------------
435
   -- Write                                                                   --
436
   -----------------------------------------------------------------------------
437
   wrs : process(clk90, wr_en, si.dat, si.sel)
438
      type s_t is (Idle, WritePreamble, Write);
439
      variable s : s_t := Idle;
440
   begin
441
      if rising_edge(clk90) then
442
         if si.rst = '1' then
443
            s := Idle;
444
         else
445
            case s is
446
               when Idle =>
447
                  wr_en2 <= false;
448
                  if wr_en then s := WritePreamble; end if;
449
 
450
               when WritePreamble =>
451
                  wr_en2 <= false;
452
                  s      := Write;
453
 
454
               when Write =>
455
                  wr_en2 <= true;
456
                  s      := Idle;
457
            end case;
458
         end if;
459
      end if;
460
   end process;
461
 
462
   -- This part is bad design practice! Direct usage of clock signals is
463
   -- discouraged. The data mask pins can't be populated with ODDR2s. 
464
   -- DRC gives an error. Could be hacked manually probably.
465
   mask <= not si.sel;
466
   dm   <= mask(3 downto 2) when clk90 = '1' else mask(1 downto 0);
467
   -- dq   <= si.dat(31 downto 16) when clk90 = '1' else si.dat(15 downto 0);
468
   -- dqs  <= clk90 & clk90;
469
 
470
   DQS_GEN : for i in 1 downto 0 generate begin DQS : ODDR2
471
      generic map( DDR_ALIGNMENT => "NONE", INIT => '0', SRTYPE => "SYNC" )
472
      port map (
473
         Q  => dqs(i),
474
         C0 => not clk0, C1 => clk0,
475
         CE => '1',
476
         D0 => '1', D1 => '0',
477
         R  => '0', S  => '0'
478
      );
479
   end generate;
480
 
481
   -- DM_GEN : for i in 1 downto 0 generate begin DM : ODDR2
482
      -- generic map( DDR_ALIGNMENT => "NONE", INIT => '0', SRTYPE => "SYNC" )
483
      -- port map (
484
         -- Q  => dm(i),
485
         -- C0 => clk90, C1 => not clk90,
486
         -- CE => '1',
487
         -- D0 => mask(2 + i), D1 => mask(i),
488
         -- R  => '0', S  => '0'
489
      -- );
490
   -- end generate;
491
 
492
   DQ_GEN : for i in 15 downto 0 generate begin DQ : ODDR2
493
      generic map( DDR_ALIGNMENT => "NONE", INIT => '0', SRTYPE => "SYNC" )
494
      port map (
495
         Q  => dq(i),
496
         C0 => clk90, C1 => not clk90,
497
         CE => '1',
498
         D0 => si.dat(16 + i), D1 => si.dat(i),
499
         R  => '0', S  => '0'
500
      );
501
   end generate;
502
 
503
   SD_DQS <= dqs when wr_en2 else "ZZ";           -- Bi-directional data strobe.
504
   SD_DQ  <= dq when wr_en2 else (others => 'Z'); -- Bi-directional data bus.
505
   SD_DM  <= dm when wr_en2 else "11";
506
 
507
 
508
   -----------------------------------------------------------------------------
509
   -- Register                                                                --
510
   -----------------------------------------------------------------------------
511
   reg : process(clk0)
512
   begin
513
      if rising_edge(clk0) then
514
         if si.rst = '1' then m <= main_d; else m <= min; end if;
515
      end if;
516
   end process;
517
end rtl;

powered by: WebSVN 2.1.0

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