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

Subversion Repositories nand_controller

[/] [nand_controller/] [trunk/] [VHDL/] [nand_master.vhd] - Blame information for rev 9

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

Line No. Rev Author Line
1 9 pradd
-------------------------------------------------------------------------------------------------
2
-------------------------------------------------------------------------------------------------
3
-- Title                                                : ONFI compliant NAND interface
4
-- File                                                 : nand_master.vhd
5
-- Author                                               : Alexey Lyashko <pradd@opencores.org>
6
-- License                                              : LGPL
7
-------------------------------------------------------------------------------------------------
8
-- Description:
9
-- The nand_master entity is the topmost entity of this ONFi (Open NAND Flash interface <http://www.onfi.org>)
10
-- compliant NAND flash controller. It provides very simple interface and short and easy to use
11
-- set of commands.
12
-- It is important to mention, that the controller takes care of delays and proper NAND command
13
-- sequences. See documentation for further details.
14
-------------------------------------------------------------------------------------------------
15
-------------------------------------------------------------------------------------------------
16
 
17
 
18
 
19
library ieee;
20
use ieee.std_logic_1164.all;
21
use ieee.numeric_std.all;
22
use work.onfi.all;
23
 
24
entity nand_master is
25
        port
26
        (
27
                -- System clock
28
                clk                                     : in    std_logic;
29
                -- NAND chip control hardware interface. These signals should be bound to physical pins.
30
                nand_cle                                : out   std_logic := '0';
31
                nand_ale                                : out   std_logic := '0';
32
                nand_nwe                                : out   std_logic := '1';
33
                nand_nwp                                : out   std_logic := '0';
34
                nand_nce                                : out   std_logic := '1';
35
                nand_nre                                : out std_logic := '1';
36
                nand_rnb                                : in    std_logic;
37
                -- NAND chip data hardware interface. These signals should be boiund to physical pins.
38
                nand_data                       : inout std_logic_vector(15 downto 0);
39
 
40
                -- Component interface
41
                nreset                          : in    std_logic;
42
                data_out                                : out   std_logic_vector(7 downto 0);
43
                data_in                         : in    std_logic_vector(7 downto 0);
44
                busy                                    : out   std_logic := '0';
45
                activate                                : in    std_logic;
46
                cmd_in                          : in    std_logic_vector(7 downto 0)
47
        );
48
end nand_master;
49
 
50
-- Structural architecture of the NAND_MASTER component.
51
architecture struct of nand_master is
52
        -- Latch Unit.
53
        -- This component implements NAND's address and command latch interfaces.
54
        component latch_unit
55
                generic(latch_type : latch_t);
56
                port(
57
                        activate,
58
                        clk : in std_logic;
59
                        data_in : in std_logic_vector(15 downto 0);
60
                        latch_ctrl,
61
                        write_enable,
62
                        busy : out std_logic;
63
                        data_out : out std_logic_vector(15 downto 0)
64
                );
65
        end component;
66
 
67
        -- Latch unit related signals
68
        signal cle_activate                     :       std_logic;
69
        signal cle_latch_ctrl           :       std_logic;
70
        signal cle_write_enable         :       std_logic;
71
        signal cle_busy                         :       std_logic;
72
        signal cle_data_in                      :       std_logic_vector(15 downto 0);
73
        signal cle_data_out                     :       std_logic_vector(15 downto 0);
74
 
75
        signal ale_activate                     :       std_logic;
76
        signal ale_latch_ctrl           :       std_logic;
77
        signal ale_write_enable         :       std_logic;
78
        signal ale_busy                         :       std_logic;
79
        signal ale_data_in                      :       std_logic_vector(15 downto 0);
80
        signal ale_data_out                     :       std_logic_vector(15 downto 0);
81
 
82
        -- IO Unit.
83
        -- This component implements NAND's read/write interfaces.
84
        component io_unit
85
                generic (io_type : io_t);
86
                port(
87
                        activate,
88
                        clk : in std_logic;
89
                        data_in : in std_logic_vector(15 downto 0);
90
                        io_ctrl,
91
                        busy : out std_logic;
92
                        data_out : out std_logic_vector(15 downto 0)
93
                );
94
        end component;
95
 
96
        -- IO Unit related signals
97
        signal io_rd_activate           :       std_logic;
98
        signal io_rd_io_ctrl                    :       std_logic;
99
        signal io_rd_busy                               :       std_logic;
100
        signal io_rd_data_in                    :       std_logic_vector(15 downto 0);
101
        signal io_rd_data_out           :       std_logic_vector(15 downto 0);
102
 
103
        signal io_wr_activate           :       std_logic;
104
        signal io_wr_io_ctrl                    :       std_logic;
105
        signal io_wr_busy                               :       std_logic;
106
        signal io_wr_data_in                    :       std_logic_vector(15 downto 0);
107
        signal io_wr_data_out           :       std_logic_vector(15 downto 0);
108
 
109
        -- FSM
110
        signal state                                    :       master_state_t  := M_RESET;
111
        signal n_state                                  :       master_state_t := M_RESET;
112
        signal substate                         :       master_substate_t       := MS_BEGIN;
113
        signal n_substate                               :       master_substate_t := MS_BEGIN;
114
 
115
        signal delay                                    :       integer := 0;
116
 
117
        signal byte_count                               :       integer := 0;
118
        signal page_idx                         :       integer := 0;
119
        signal page_data                                :       page_t;
120
        signal page_param                               :       param_page_t;
121
        signal chip_id                                  :       nand_id_t;
122
        signal current_address          :       nand_address_t;
123
        signal data_bytes_per_page      :       integer;
124
        signal oob_bytes_per_page       :       integer;
125
        signal addr_cycles                      :       integer;
126
        signal state_switch                     :       states_t        :=
127
                (
128
 
129
                        1       =>      M_NAND_RESET,
130
                        2       =>      M_NAND_READ_PARAM_PAGE,
131
                        3       =>      M_NAND_READ_ID,
132
                        4       =>      M_NAND_BLOCK_ERASE,
133
                        5       =>      M_NAND_READ_STATUS,
134
                        6       =>      M_NAND_READ,
135
                        7       =>      M_NAND_PAGE_PROGRAM,
136
                        8       =>      MI_GET_STATUS,
137
                        9       =>      MI_CHIP_ENABLE,
138
                        10      =>      MI_CHIP_DISABLE,
139
                        11      =>      MI_WRITE_PROTECT,
140
                        12      =>      MI_WRITE_ENABLE,
141
                        13      =>      MI_RESET_INDEX,
142
                        14      =>      MI_GET_ID_BYTE,
143
                        15      =>      MI_GET_PARAM_PAGE_BYTE,
144
                        16      =>      MI_GET_DATA_PAGE_BYTE,
145
                        17      =>      MI_SET_DATA_PAGE_BYTE,
146
                        18      =>      MI_GET_CURRENT_ADDRESS_BYTE,
147
                        19      =>      MI_SET_CURRENT_ADDRESS_BYTE,
148
                        others => M_IDLE
149
                );
150
 
151
--      The following is a sort of a status register. Bit set to 1 means TRUE, bit set to 0 means FALSE:
152
--      0 - is ONFI compliant
153
--      1 - bus width (0 - x8 / 1 - x16)
154
--      2 - is chip enabled
155
--      3 - is chip write protected
156
--      4 - array pointer out of bounds
157
--      5 - 
158
--      6 - 
159
--      7 - 
160
        signal status                                   :       std_logic_vector(7 downto 0) := x"00";
161
begin
162
 
163
        -- Asynchronous command latch interface.
164
        ACL: latch_unit
165
        generic map (latch_type => LATCH_CMD)
166
        port map
167
        (
168
                activate => cle_activate,
169
                latch_ctrl => cle_latch_ctrl,
170
                write_enable => cle_write_enable,
171
                busy => cle_busy,
172
                clk => clk,
173
                data_in => cle_data_in,
174
                data_out => cle_data_out
175
        );
176
 
177
        -- Asynchronous address latch interface.
178
        AAL: latch_unit
179
        generic map (latch_type => LATCH_ADDR)
180
        port map
181
        (
182
                activate => ale_activate,
183
                latch_ctrl => ale_latch_ctrl,
184
                write_enable => ale_write_enable,
185
                busy => ale_busy,
186
                clk => clk,
187
                data_in => ale_data_in,
188
                data_out => ale_data_out
189
        );
190
 
191
        -- Output to NAND
192
        IO_WR: io_unit
193
        generic map (io_type => IO_WRITE)
194
        port map
195
        (
196
                clk => clk,
197
                activate => io_wr_activate,
198
                data_in => io_wr_data_in,
199
                data_out => io_wr_data_out,
200
                io_ctrl => io_wr_io_ctrl,
201
                busy => io_wr_busy
202
        );
203
 
204
        -- Input from NAND
205
        IO_RD: io_unit
206
        generic map (io_type => IO_READ)
207
        port map
208
        (
209
                clk => clk,
210
                activate => io_rd_activate,
211
                data_in => io_rd_data_in,
212
                data_out => io_rd_data_out,
213
                io_ctrl => io_rd_io_ctrl,
214
                busy => io_rd_busy
215
        );
216
 
217
        -- Busy indicator
218
        busy    <= '0'   when state = M_IDLE else
219
                                '1';
220
 
221
        -- Bidirection NAND data interface.
222
        --nand_data     <=      (cle_data_out or ale_data_out or io_wr_data_out) when (cle_busy or ale_busy or io_wr_busy) = '1' else x"ZZZZ";
223
        nand_data       <=      cle_data_out when cle_busy = '1' else
224
                                                ale_data_out when ale_busy = '1' else
225
                                                io_wr_data_out when io_wr_busy = '1' else
226
                                                "ZZZZZZZZZZZZZZZZ";
227
        io_rd_data_in   <=      nand_data;
228
 
229
        -- Command Latch Enable
230
        nand_cle                <= cle_latch_ctrl;
231
 
232
        -- Address Latch Enable
233
        nand_ale                <= ale_latch_ctrl;
234
 
235
        -- Write Enable
236
        nand_nwe                <=      cle_write_enable and ale_write_enable and io_wr_io_ctrl;
237
 
238
        -- Read Enable
239
        nand_nre                <= io_rd_io_ctrl;
240
 
241
        -- Activation of command latch unit
242
        cle_activate    <=      '1'     when    state = M_NAND_RESET or                                                                                                                                                         -- initiate submission of RESET command
243
                                                                                        (state = M_NAND_READ_PARAM_PAGE and substate = MS_BEGIN) or                                                     -- initiate submission of READ PARAMETER PAGE command
244
                                                                                        (state = M_NAND_BLOCK_ERASE and substate = MS_BEGIN) or                                                         -- initiate submission of BLOCK ERASE command
245
                                                                                        (state = M_NAND_BLOCK_ERASE and substate = MS_SUBMIT_COMMAND1) or                                       -- initiate submission of BLOCK ERASE 2 command
246
                                                                                        (state = M_NAND_READ_STATUS and substate = MS_BEGIN) or                                                         -- initiate submission of READ STATUS command
247
                                                                                        (state = M_NAND_READ and substate = MS_BEGIN) or                                                                                -- initiate read mode for READ command
248
                                                                                        (state = M_NAND_READ and substate = MS_SUBMIT_COMMAND1) or                                                      -- initiate submission of READ command
249
                                                                                        (state = M_NAND_PAGE_PROGRAM and substate = MS_BEGIN) or                                                                -- initiate write mode for PAGE PROGRAM command
250
                                                                                        (state = M_NAND_PAGE_PROGRAM and substate = MS_SUBMIT_COMMAND1) or                              -- initiate submission for PAGE PROGRAM command
251
                                                                                        (state = M_NAND_READ_ID and substate = MS_BEGIN) else                                                                   -- initiate submission of READ ID command
252
                                                        '0';
253
 
254
        -- Activation of address latch unit
255
        ale_activate    <=      '1'     when    (state = M_NAND_READ_PARAM_PAGE and substate = MS_SUBMIT_COMMAND) or                            -- initiate address submission for READ PARAMETER PAGE command
256
                                                                                        (state = M_NAND_BLOCK_ERASE and substate = MS_SUBMIT_COMMAND) or                                        -- initiate address submission for BLOCK ERASE command
257
                                                                                        (state = M_NAND_READ and substate = MS_SUBMIT_COMMAND) or                                                       -- initiate address submission for READ command
258
                                                                                        (state = M_NAND_PAGE_PROGRAM and substate = MS_SUBMIT_ADDRESS) or                                       -- initiate address submission for PAGE PROGRAM command
259
                                                                                        (state = M_NAND_READ_ID and substate = MS_SUBMIT_COMMAND) else                                          -- initiate address submission for READ ID command
260
                                                        '0';
261
 
262
        -- Activation of read byte mechanism
263
        io_rd_activate  <=      '1'     when    (state = M_NAND_READ_PARAM_PAGE and substate = MS_READ_DATA0) or                                        -- initiate byte read for READ PARAMETER PAGE command
264
                                                                                        (state = M_NAND_READ_STATUS and substate = MS_READ_DATA0) or                                            -- initiate byte read for READ STATUS command
265
                                                                                        (state = M_NAND_READ and substate = MS_READ_DATA0) or                                                                   -- initiate byte read for READ command
266
                                                                                        (state = M_NAND_READ_ID and substate = MS_READ_DATA0) else                                                      -- initiate byte read for READ ID command
267
                                                        '0';
268
 
269
        -- Activation of write byte mechanism
270
        io_wr_activate  <=      '1'     when    (state = M_NAND_PAGE_PROGRAM and substate = MS_WRITE_DATA3)     else                                    -- initiate byte write for PAGE_PROGRAM command
271
                                                        '0';
272
 
273
        MASTER: process(clk, nreset, activate, cmd_in, data_in, state_switch)
274
                variable tmp_int                :       std_logic_vector(31 downto 0);
275
                variable tmp                    :       integer;
276
        begin
277
                if(nreset = '0')then
278
                        state                                                   <= M_RESET;
279
 
280
--              elsif(activate = '1')then
281
--                      state                                                   <= state_switch(to_integer(unsigned(cmd_in)));
282
 
283
                elsif(rising_edge(clk))then
284
                        case state is
285
                                -- RESET state. Speaks for itself
286
                                when M_RESET =>
287
                                        state                                   <= M_IDLE;
288
                                        substate                                <= MS_BEGIN;
289
                                        delay                                   <= 0;
290
                                        byte_count                      <= 0;
291
                                        page_idx                                <= 0;
292
                                        current_address(0)<= x"00";
293
                                        current_address(1)<= x"00";
294
                                        current_address(2)<= x"00";
295
                                        current_address(3)<= x"00";
296
                                        current_address(4)<= x"00";
297
                                        data_bytes_per_page     <= 0;
298
                                        oob_bytes_per_page      <= 0;
299
                                        addr_cycles                     <= 0;
300
                                        status                          <= x"08";               -- We start write protected!
301
                                        nand_nce                                <= '1';
302
                                        nand_nwp                                <= '0';
303
 
304
                                -- This is in fact a command interpreter
305
                                when M_IDLE =>
306
                                        if(activate = '1')then
307
                                                state                           <= state_switch(to_integer(unsigned(cmd_in)));
308
                                        end if;
309
 
310
                                -- Reset the NAND chip
311
                                when M_NAND_RESET =>
312
                                        cle_data_in                     <= x"00ff";
313
                                        state                                   <= M_WAIT;
314
                                        n_state                         <= M_IDLE;
315
 
316
                                when MI_GET_STATUS =>
317
                                        data_out                                <= status;
318
                                        state                                   <= M_IDLE;
319
 
320
                                when MI_CHIP_ENABLE =>
321
                                        nand_nce                                <= '0';
322
                                        state                                   <= M_IDLE;
323
                                        status(2)                       <= '1';
324
 
325
                                when MI_CHIP_DISABLE =>
326
                                        nand_nce                                <= '1';
327
                                        state                                   <= M_IDLE;
328
                                        status(2)                       <= '0';
329
 
330
                                when MI_WRITE_PROTECT =>
331
                                        nand_nwp                                <= '0';
332
                                        status(3)                       <= '1';
333
                                        state                                   <= M_IDLE;
334
 
335
                                when MI_WRITE_ENABLE =>
336
                                        nand_nwp                                <= '1';
337
                                        status(3)                       <= '0';
338
                                        state                                   <= M_IDLE;
339
 
340
                                when MI_RESET_INDEX =>
341
                                        page_idx                                <= 0;
342
                                        state                                   <= M_IDLE;
343
 
344
                                when MI_GET_ID_BYTE =>
345
                                        if(page_idx < 5)then
346
                                                data_out                                <= chip_id(page_idx);
347
                                                page_idx                                <= page_idx + 1;
348
                                                status(4)                       <= '0';
349
                                        else
350
                                                data_out                                <= x"00";
351
                                                page_idx                                <= 0;
352
                                                status(4)                       <= '1';
353
                                        end if;
354
                                        state                                           <= M_IDLE;
355
 
356
                                when MI_GET_PARAM_PAGE_BYTE =>
357
                                        if(page_idx < 256)then
358
                                                data_out                                <= page_param(page_idx);
359
                                                page_idx                                <= page_idx + 1;
360
                                                status(4)                       <= '0';
361
                                        else
362
                                                data_out                                <= x"00";
363
                                                page_idx                                <= 0;
364
                                                status(4)                       <= '1';
365
                                        end if;
366
                                        state                                           <= M_IDLE;
367
 
368
                                when MI_GET_DATA_PAGE_BYTE =>
369
                                        if(page_idx < data_bytes_per_page + oob_bytes_per_page)then
370
                                                data_out                                <= page_data(page_idx);
371
                                                page_idx                                <= page_idx + 1;
372
                                                status(4)                       <= '0';
373
                                        else
374
                                                data_out                                <= x"00";
375
                                                page_idx                                <= 0;
376
                                                status(4)                       <= '1';
377
                                        end if;
378
                                        state                                           <= M_IDLE;
379
 
380
                                when MI_SET_DATA_PAGE_BYTE =>
381
                                        if(page_idx < data_bytes_per_page + oob_bytes_per_page)then
382
                                                page_data(page_idx)     <= data_in;
383
                                                page_idx                                        <= page_idx + 1;
384
                                                status(4)                               <= '0';
385
                                        else
386
                                                page_idx                                        <= 0;
387
                                                status(4)                               <= '1';
388
                                        end if;
389
                                        state                                           <= M_IDLE;
390
 
391
                                when MI_GET_CURRENT_ADDRESS_BYTE =>
392
                                        if(page_idx < addr_cycles)then
393
                                                data_out                                        <= current_address(page_idx);
394
                                                page_idx                                        <= page_idx + 1;
395
                                                status(4)                               <= '0';
396
                                        else
397
                                                page_idx                                        <= 0;
398
                                                status(4)                               <= '1';
399
                                        end if;
400
                                        state                                           <= M_IDLE;
401
 
402
                                when MI_SET_CURRENT_ADDRESS_BYTE =>
403
                                        if(page_idx < addr_cycles)then
404
                                                current_address(page_idx) <= data_in;
405
                                                page_idx                                        <= page_idx + 1;
406
                                                status(4)                               <= '0';
407
                                        else
408
                                                page_idx                                        <= 0;
409
                                                status(4)                               <= '1';
410
                                        end if;
411
                                        state                                           <= M_IDLE;
412
 
413
                                -- Program one page.
414
                                when M_NAND_PAGE_PROGRAM =>
415
                                        if(substate = MS_BEGIN)then
416
                                                cle_data_in             <= x"0080";
417
                                                substate                        <= MS_SUBMIT_COMMAND;
418
                                                state                   <= M_WAIT;
419
                                                n_state                 <= M_NAND_PAGE_PROGRAM;
420
                                                byte_count              <= 0;
421
 
422
                                        elsif(substate = MS_SUBMIT_COMMAND)then
423
                                                byte_count              <= byte_count + 1;
424
                                                ale_data_in             <= x"00"&current_address(byte_count);
425
                                                substate                        <= MS_SUBMIT_ADDRESS;
426
 
427
                                        elsif(substate = MS_SUBMIT_ADDRESS)then
428
                                                if(byte_count < addr_cycles)then
429
                                                        substate                <= MS_SUBMIT_COMMAND;
430
                                                else
431
                                                        substate                <= MS_WRITE_DATA0;
432
                                                end if;
433
                                                state                           <= M_WAIT;
434
                                                n_state                 <= M_NAND_PAGE_PROGRAM;
435
 
436
                                        elsif(substate = MS_WRITE_DATA0)then
437
                                                delay                           <= t_adl;
438
                                                state                           <= M_DELAY;
439
                                                n_state                 <= M_NAND_PAGE_PROGRAM;
440
                                                substate                        <=      MS_WRITE_DATA1;
441
                                                page_idx                        <= 0;
442
                                                byte_count              <= 0;
443
 
444
                                        elsif(substate = MS_WRITE_DATA1)then
445
                                                byte_count              <= byte_count + 1;
446
                                                page_idx                        <= page_idx + 1;
447
                                                io_wr_data_in   <= x"00"&page_data(page_idx);
448
                                                if(status(1) = '0')then
449
                                                        substate                <= MS_WRITE_DATA3;
450
                                                else
451
                                                        substate                <= MS_WRITE_DATA2;
452
                                                end if;
453
 
454
                                        elsif(substate = MS_WRITE_DATA2)then
455
                                                page_idx                        <= page_idx + 1;
456
                                                io_wr_data_in(15 downto 8) <= page_data(page_idx);
457
                                                substate                        <= MS_WRITE_DATA3;
458
 
459
                                        elsif(substate = MS_WRITE_DATA3)then
460
                                                if(byte_count < data_bytes_per_page + oob_bytes_per_page)then
461
                                                        substate                <= MS_WRITE_DATA1;
462
                                                else
463
                                                        substate                <= MS_SUBMIT_COMMAND1;
464
                                                end if;
465
                                                n_state                 <= M_NAND_PAGE_PROGRAM;
466
                                                state                           <= M_WAIT;
467
 
468
                                        elsif(substate = MS_SUBMIT_COMMAND1)then
469
                                                cle_data_in             <= x"0010";
470
                                                n_state                 <= M_NAND_PAGE_PROGRAM;
471
                                                state                           <= M_WAIT;
472
                                                substate                        <= MS_WAIT;
473
 
474
                                        elsif(substate = MS_WAIT)then
475
                                                delay                           <= t_wb + t_prog;
476
                                                state                           <= M_DELAY;
477
                                                n_state                 <= M_NAND_PAGE_PROGRAM;
478
                                                substate                        <= MS_END;
479
                                                byte_count              <= 0;
480
                                                page_idx                        <= 0;
481
 
482
                                        elsif(substate = MS_END)then
483
                                                state                           <= M_WAIT;
484
                                                n_state                 <= M_IDLE;
485
                                                substate                        <= MS_BEGIN;
486
                                        end if;
487
 
488
 
489
                                -- Reads single page into the buffer.
490
                                when M_NAND_READ =>
491
                                        if(substate = MS_BEGIN)then
492
                                                cle_data_in             <= x"0000";
493
                                                substate                        <= MS_SUBMIT_COMMAND;
494
                                                state                           <= M_WAIT;
495
                                                n_state                 <= M_NAND_READ;
496
                                                byte_count              <= 0;
497
 
498
                                        elsif(substate = MS_SUBMIT_COMMAND)then
499
                                                byte_count              <= byte_count + 1;
500
                                                ale_data_in             <= x"00"&current_address(byte_count);
501
                                                substate                        <= MS_SUBMIT_ADDRESS;
502
 
503
                                        elsif(substate = MS_SUBMIT_ADDRESS)then
504
                                                if(byte_count < addr_cycles)then
505
                                                        substate                <= MS_SUBMIT_COMMAND;
506
                                                else
507
                                                        substate                <= MS_SUBMIT_COMMAND1;
508
                                                end if;
509
                                                state                           <= M_WAIT;
510
                                                n_state                 <= M_NAND_READ;
511
 
512
                                        elsif(substate = MS_SUBMIT_COMMAND1)then
513
                                                cle_data_in             <= x"0030";
514
                                                substate                        <= MS_DELAY;
515
                                                state                   <= M_WAIT;
516
                                                n_state                 <= M_NAND_READ;
517
 
518
                                        elsif(substate = MS_DELAY)then
519
                                                delay                           <= t_wb;
520
                                                substate                        <= MS_READ_DATA0;
521
                                                state                           <= M_DELAY;
522
                                                n_state                 <= M_NAND_READ;
523
                                                byte_count              <= 0;
524
                                                page_idx                        <= 0;
525
 
526
                                        elsif(substate = MS_READ_DATA0)then
527
                                                byte_count              <= byte_count + 1;
528
                                                n_state                 <= M_NAND_READ;
529
                                                state                           <= M_WAIT;
530
                                                substate                        <= MS_READ_DATA1;
531
 
532
                                        elsif(substate = MS_READ_DATA1)then
533
                                                page_data(page_idx)     <= io_rd_data_out(7 downto 0);
534
                                                page_idx                        <= page_idx + 1;
535
                                                if(byte_count = data_bytes_per_page + oob_bytes_per_page and status(1) = '0')then
536
                                                        substate                <= MS_END;
537
                                                else
538
                                                        if(status(1) = '0')then
539
                                                                substate                <= MS_READ_DATA0;
540
                                                        else
541
                                                                substate                <= MS_READ_DATA2;
542
                                                        end if;
543
                                                end if;
544
 
545
                                        elsif(substate = MS_READ_DATA2)then
546
                                                page_idx                        <= page_idx + 1;
547
                                                page_data(page_idx)     <= io_rd_data_out(15 downto 8);
548
                                                if(byte_count = data_bytes_per_page + oob_bytes_per_page)then
549
                                                        substate                <= MS_END;
550
                                                else
551
                                                        substate                <= MS_READ_DATA0;
552
                                                end if;
553
 
554
                                        elsif(substate = MS_END)then
555
                                                substate                        <= MS_BEGIN;
556
                                                state                           <= M_IDLE;
557
                                                byte_count              <= 0;
558
                                        end if;
559
 
560
                                -- Read status byte
561
                                when M_NAND_READ_STATUS =>
562
                                        if(substate = MS_BEGIN)then
563
                                                cle_data_in             <= x"0070";
564
                                                substate                        <= MS_SUBMIT_COMMAND;
565
                                                state                           <= M_WAIT;
566
                                                n_state                 <= M_NAND_READ_STATUS;
567
 
568
                                        elsif(substate = MS_SUBMIT_COMMAND)then
569
                                                delay                           <= t_whr;
570
                                                substate                        <= MS_READ_DATA0;
571
                                                state                           <= M_DELAY;
572
                                                n_state                 <= M_NAND_READ_STATUS;
573
 
574
                                        elsif(substate = MS_READ_DATA0)then
575
                                                substate                        <= MS_READ_DATA1;
576
                                                state                           <= M_WAIT;
577
                                                n_state                 <= M_NAND_READ_STATUS;
578
 
579
                                        elsif(substate = MS_READ_DATA1)then                                             -- This is to make sure 'data_out' has valid data before 'busy' goes low.
580
                                                data_out                        <= io_rd_data_out(7 downto 0);
581
                                                state                           <= M_NAND_READ_STATUS;
582
                                                substate                        <= MS_END;
583
 
584
                                        elsif(substate = MS_END)then
585
                                                substate                        <= MS_BEGIN;
586
                                                state                           <= M_IDLE;
587
                                        end if;
588
 
589
                                -- Erase block specified by current_address
590
                                when M_NAND_BLOCK_ERASE =>
591
                                        if(substate = MS_BEGIN)then
592
                                                cle_data_in             <= x"0060";
593
                                                substate                        <= MS_SUBMIT_COMMAND;
594
                                                state                           <= M_WAIT;
595
                                                n_state                 <= M_NAND_BLOCK_ERASE;
596
                                                byte_count              <= 3;                                                   -- number of address bytes to submit
597
 
598
                                        elsif(substate = MS_SUBMIT_COMMAND)then
599
                                                byte_count              <= byte_count - 1;
600
                                                ale_data_in(15 downto 8) <= x"00";
601
                                                ale_data_in(7 downto 0)  <= current_address(5 - byte_count);
602
                                                substate                        <= MS_SUBMIT_ADDRESS;
603
                                                state                           <= M_WAIT;
604
                                                n_state                 <= M_NAND_BLOCK_ERASE;
605
 
606
                                        elsif(substate = MS_SUBMIT_ADDRESS)then
607
                                                if(0 < byte_count)then
608
                                                        substate                <= MS_SUBMIT_COMMAND;
609
                                                else
610
                                                        substate                <= MS_SUBMIT_COMMAND1;
611
                                                end if;
612
 
613
                                        elsif(substate = MS_SUBMIT_COMMAND1)then
614
                                                cle_data_in             <= x"00d0";
615
                                                substate                        <= MS_END;
616
                                                state                           <= M_WAIT;
617
                                                n_state                 <= M_NAND_BLOCK_ERASE;
618
 
619
                                        elsif(substate = MS_END)then
620
                                                n_state                 <= M_IDLE;
621
                                                delay                   <= t_wb + t_bers;
622
                                                state                           <= M_DELAY;
623
                                                substate                        <= MS_BEGIN;
624
                                                byte_count              <= 0;
625
                                        end if;
626
 
627
                                -- Read NAND chip JEDEC ID
628
                                when M_NAND_READ_ID =>
629
                                        if(substate = MS_BEGIN)then
630
                                                cle_data_in             <= x"0090";
631
                                                substate                        <= MS_SUBMIT_COMMAND;
632
                                                state                           <= M_WAIT;
633
                                                n_state                         <= M_NAND_READ_ID;
634
 
635
                                        elsif(substate = MS_SUBMIT_COMMAND)then
636
                                                ale_data_in             <= X"0000";
637
                                                substate                        <= MS_SUBMIT_ADDRESS;
638
                                                state                   <= M_WAIT;
639
                                                n_state                 <= M_NAND_READ_ID;
640
 
641
                                        elsif(substate = MS_SUBMIT_ADDRESS)then
642
                                                delay                           <= t_wb;
643
                                                state                           <= M_DELAY;
644
                                                n_state                 <= M_NAND_READ_ID;
645
                                                substate                        <= MS_READ_DATA0;
646
                                                byte_count              <= 5;
647
                                                page_idx                        <= 0;
648
 
649
                                        elsif(substate = MS_READ_DATA0)then
650
                                                byte_count              <= byte_count - 1;
651
                                                state                           <= M_WAIT;
652
                                                n_state                 <= M_NAND_READ_ID;
653
                                                substate                        <= MS_READ_DATA1;
654
 
655
                                        elsif(substate = MS_READ_DATA1)then
656
                                                chip_id(page_idx)       <= io_rd_data_out(7 downto 0);
657
                                                if(0 < byte_count)then
658
                                                        page_idx                                        <= page_idx + 1;
659
                                                        substate                                        <= MS_READ_DATA0;
660
                                                else
661
                                                        substate                                        <= MS_END;
662
                                                end if;
663
 
664
                                        elsif(substate = MS_END)then
665
                                                byte_count              <= 0;
666
                                                page_idx                        <= 0;
667
                                                substate                        <= MS_BEGIN;
668
                                                state                           <= M_IDLE;
669
                                        end if;
670
 
671
                                -- *data_in is assigned one clock cycle after *_activate is triggered!!!!
672
                                -- According to ONFI's timing diagrams this should be normal, but who knows...
673
                                when M_NAND_READ_PARAM_PAGE =>
674
                                        if(substate = MS_BEGIN)then
675
                                                cle_data_in             <= x"00ec";
676
                                                substate                        <= MS_SUBMIT_COMMAND;
677
                                                state                           <= M_WAIT;
678
                                                n_state                         <= M_NAND_READ_PARAM_PAGE;
679
 
680
                                        elsif(substate = MS_SUBMIT_COMMAND)then
681
                                                ale_data_in             <= X"0000";
682
                                                substate                        <= MS_SUBMIT_ADDRESS;
683
                                                state                   <= M_WAIT;
684
                                                n_state                 <= M_NAND_READ_PARAM_PAGE;
685
 
686
                                        elsif(substate = MS_SUBMIT_ADDRESS)then
687
                                                delay                           <= t_wb;
688
                                                state                           <= M_DELAY;
689
                                                n_state                 <= M_NAND_READ_PARAM_PAGE;
690
                                                substate                        <= MS_READ_DATA0;
691
                                                byte_count              <= 256;
692
                                                page_idx                        <= 0;
693
 
694
                                        elsif(substate = MS_READ_DATA0)then
695
                                                byte_count              <= byte_count - 1;
696
                                                state                           <= M_WAIT;
697
                                                n_state                 <= M_NAND_READ_PARAM_PAGE;
698
                                                substate                        <= MS_READ_DATA1;
699
 
700
                                        elsif(substate = MS_READ_DATA1)then
701
                                                page_param(page_idx)    <= io_rd_data_out(7 downto 0);
702
                                                if(0 < byte_count)then
703
                                                        page_idx                                        <= page_idx + 1;
704
                                                        substate                                        <= MS_READ_DATA0;
705
                                                else
706
                                                        substate                                        <= MS_END;
707
                                                end if;
708
 
709
                                        elsif(substate = MS_END)then
710
                                                byte_count              <= 0;
711
                                                page_idx                        <= 0;
712
                                                substate                        <= MS_BEGIN;
713
                                                state                           <= M_IDLE;
714
 
715
                                                -- Check the chip for being ONFI compliant
716
                                                if(page_param(0) = x"4f" and page_param(1) = x"4e" and page_param(2) = x"46" and page_param(3) = x"49")then
717
                                                        -- Set status bit 0
718
                                                        status(0)                                        <= '1';
719
 
720
                                                        -- Bus width
721
                                                        status(1)                                       <= page_param(6)(0);
722
 
723
                                                        -- Setup counters:
724
                                                        -- Number of bytes per page
725
                                                        tmp_int                                         := page_param(83)&page_param(82)&page_param(81)&page_param(80);
726
                                                        data_bytes_per_page             <= to_integer(unsigned(tmp_int));
727
 
728
                                                        -- Number of spare bytes per page (OOB)
729
                                                        tmp_int                                         := "0000000000000000" & page_param(85) & page_param(84);
730
                                                        oob_bytes_per_page              <= to_integer(unsigned(tmp_int));
731
 
732
                                                        -- Number of address cycles
733
                                                        addr_cycles                                     <= to_integer(unsigned(page_param(101)(3 downto 0))) + to_integer(unsigned(page_param(101)(7 downto 4)));
734
                                                end if;
735
                                        end if;
736
 
737
                                -- Wait for latch and IO modules to become ready as well as for NAND's R/B# to be '1'
738
                                when M_WAIT =>
739
                                        if('0' = (cle_busy or ale_busy or io_rd_busy or io_wr_busy or (not nand_rnb)))then
740
                                                state                           <= n_state;
741
                                        end if;
742
 
743
                                -- Simple delay mechanism
744
                                when M_DELAY =>
745
                                        if(delay > 1)then
746
                                                delay                   <= delay - 1;
747
                                        else
748
                                                state                           <= n_state;
749
                                        end if;
750
 
751
                                -- For just in case ("Shit happens..." (C) Forrest Gump)
752
                                when others =>
753
                                        state                           <= M_RESET;
754
                        end case;
755
                end if;
756
        end process;
757
end struct;
758
 

powered by: WebSVN 2.1.0

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