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 10

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

Line No. Rev Author Line
1 9 pradd
-------------------------------------------------------------------------------------------------
2
-------------------------------------------------------------------------------------------------
3 10 pradd
-- Title                                                        : ONFI compliant NAND interface
4 9 pradd
-- 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 10 pradd
        cle_activate    <=      '1'     when    state = M_NAND_RESET or                                                                                                                                                 -- initiate submission of RESET command
243 9 pradd
                                                                                        (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 10 pradd
                                -- Read the status register of the controller
317 9 pradd
                                when MI_GET_STATUS =>
318
                                        data_out                                <= status;
319
                                        state                                   <= M_IDLE;
320
 
321 10 pradd
                                -- Set CE# to '0' (enable NAND chip)
322 9 pradd
                                when MI_CHIP_ENABLE =>
323
                                        nand_nce                                <= '0';
324
                                        state                                   <= M_IDLE;
325
                                        status(2)                       <= '1';
326
 
327 10 pradd
                                -- Set CE# to '1' (disable NAND chip)
328 9 pradd
                                when MI_CHIP_DISABLE =>
329
                                        nand_nce                                <= '1';
330
                                        state                                   <= M_IDLE;
331
                                        status(2)                       <= '0';
332
 
333 10 pradd
                                -- Set WP# to '0' (enable write protection)
334 9 pradd
                                when MI_WRITE_PROTECT =>
335
                                        nand_nwp                                <= '0';
336
                                        status(3)                       <= '1';
337
                                        state                                   <= M_IDLE;
338
 
339 10 pradd
                                -- Set WP# to '1' (disable write protection)
340
                                -- By default, this controller has WP# set to 0 on reset
341 9 pradd
                                when MI_WRITE_ENABLE =>
342
                                        nand_nwp                                <= '1';
343
                                        status(3)                       <= '0';
344
                                        state                                   <= M_IDLE;
345
 
346 10 pradd
                                -- Reset the index register.
347
                                -- Index register holds offsets into JEDEC ID, Parameter Page buffer or Data Page buffer depending on
348
                                -- the operation being performed
349 9 pradd
                                when MI_RESET_INDEX =>
350
                                        page_idx                                <= 0;
351
                                        state                                   <= M_IDLE;
352
 
353 10 pradd
                                -- Read 1 byte from JEDEC ID and increment the index register.
354
                                -- If the value points outside the 5 byte JEDEC ID array, 
355
                                -- the register is reset to 0 and bit 4 of the status register
356
                                -- is set to '1'
357 9 pradd
                                when MI_GET_ID_BYTE =>
358
                                        if(page_idx < 5)then
359
                                                data_out                                <= chip_id(page_idx);
360
                                                page_idx                                <= page_idx + 1;
361
                                                status(4)                       <= '0';
362
                                        else
363
                                                data_out                                <= x"00";
364
                                                page_idx                                <= 0;
365
                                                status(4)                       <= '1';
366
                                        end if;
367
                                        state                                           <= M_IDLE;
368
 
369 10 pradd
                                -- Read 1 byte from 256 bytes buffer that holds the Parameter Page.
370
                                -- If the value goes beyond 255, then the register is reset and 
371
                                -- bit 4 of the status register is set to '1'
372 9 pradd
                                when MI_GET_PARAM_PAGE_BYTE =>
373
                                        if(page_idx < 256)then
374
                                                data_out                                <= page_param(page_idx);
375
                                                page_idx                                <= page_idx + 1;
376
                                                status(4)                       <= '0';
377
                                        else
378
                                                data_out                                <= x"00";
379
                                                page_idx                                <= 0;
380
                                                status(4)                       <= '1';
381
                                        end if;
382
                                        state                                           <= M_IDLE;
383
 
384 10 pradd
                                -- Read 1 byte from the buffer that holds the content of last read 
385
                                -- page. The limit is variable and depends on the values in 
386
                                -- the Parameter Page. In case the index register points beyond 
387
                                -- valid page content, its value is reset and bit 4 of the status
388
                                -- register is set to '1'
389 9 pradd
                                when MI_GET_DATA_PAGE_BYTE =>
390
                                        if(page_idx < data_bytes_per_page + oob_bytes_per_page)then
391
                                                data_out                                <= page_data(page_idx);
392
                                                page_idx                                <= page_idx + 1;
393
                                                status(4)                       <= '0';
394
                                        else
395
                                                data_out                                <= x"00";
396
                                                page_idx                                <= 0;
397
                                                status(4)                       <= '1';
398
                                        end if;
399
                                        state                                           <= M_IDLE;
400
 
401 10 pradd
                                -- Write 1 byte into the Data Page buffer at offset specified by
402
                                -- the index register. If the value of the index register points 
403
                                -- beyond valid page content, its value is reset and bit 4 of
404
                                -- the status register is set to '1'
405 9 pradd
                                when MI_SET_DATA_PAGE_BYTE =>
406
                                        if(page_idx < data_bytes_per_page + oob_bytes_per_page)then
407
                                                page_data(page_idx)     <= data_in;
408
                                                page_idx                                        <= page_idx + 1;
409
                                                status(4)                               <= '0';
410
                                        else
411
                                                page_idx                                        <= 0;
412
                                                status(4)                               <= '1';
413
                                        end if;
414
                                        state                                           <= M_IDLE;
415
 
416 10 pradd
                                -- Gets the address byte specified by the index register. Bit 4 
417
                                -- of the status register is set to '1' if the value of the index 
418
                                -- register points beyond valid address data and the value of 
419
                                -- the index register is reset
420 9 pradd
                                when MI_GET_CURRENT_ADDRESS_BYTE =>
421
                                        if(page_idx < addr_cycles)then
422
                                                data_out                                        <= current_address(page_idx);
423
                                                page_idx                                        <= page_idx + 1;
424
                                                status(4)                               <= '0';
425
                                        else
426
                                                page_idx                                        <= 0;
427
                                                status(4)                               <= '1';
428
                                        end if;
429
                                        state                                           <= M_IDLE;
430
 
431 10 pradd
                                -- Sets the value of the address byte specified by the index register.Bit 4 
432
                                -- of the status register is set to '1' if the value of the index 
433
                                -- register points beyond valid address data and the value of 
434
                                -- the index register is reset
435 9 pradd
                                when MI_SET_CURRENT_ADDRESS_BYTE =>
436
                                        if(page_idx < addr_cycles)then
437
                                                current_address(page_idx) <= data_in;
438
                                                page_idx                                        <= page_idx + 1;
439
                                                status(4)                               <= '0';
440
                                        else
441
                                                page_idx                                        <= 0;
442
                                                status(4)                               <= '1';
443
                                        end if;
444
                                        state                                           <= M_IDLE;
445
 
446
                                -- Program one page.
447
                                when M_NAND_PAGE_PROGRAM =>
448
                                        if(substate = MS_BEGIN)then
449
                                                cle_data_in             <= x"0080";
450
                                                substate                        <= MS_SUBMIT_COMMAND;
451
                                                state                   <= M_WAIT;
452
                                                n_state                 <= M_NAND_PAGE_PROGRAM;
453
                                                byte_count              <= 0;
454
 
455
                                        elsif(substate = MS_SUBMIT_COMMAND)then
456
                                                byte_count              <= byte_count + 1;
457
                                                ale_data_in             <= x"00"&current_address(byte_count);
458
                                                substate                        <= MS_SUBMIT_ADDRESS;
459
 
460
                                        elsif(substate = MS_SUBMIT_ADDRESS)then
461
                                                if(byte_count < addr_cycles)then
462
                                                        substate                <= MS_SUBMIT_COMMAND;
463
                                                else
464
                                                        substate                <= MS_WRITE_DATA0;
465
                                                end if;
466
                                                state                           <= M_WAIT;
467
                                                n_state                 <= M_NAND_PAGE_PROGRAM;
468
 
469
                                        elsif(substate = MS_WRITE_DATA0)then
470
                                                delay                           <= t_adl;
471
                                                state                           <= M_DELAY;
472
                                                n_state                 <= M_NAND_PAGE_PROGRAM;
473
                                                substate                        <=      MS_WRITE_DATA1;
474
                                                page_idx                        <= 0;
475
                                                byte_count              <= 0;
476
 
477
                                        elsif(substate = MS_WRITE_DATA1)then
478
                                                byte_count              <= byte_count + 1;
479
                                                page_idx                        <= page_idx + 1;
480
                                                io_wr_data_in   <= x"00"&page_data(page_idx);
481
                                                if(status(1) = '0')then
482
                                                        substate                <= MS_WRITE_DATA3;
483
                                                else
484
                                                        substate                <= MS_WRITE_DATA2;
485
                                                end if;
486
 
487
                                        elsif(substate = MS_WRITE_DATA2)then
488
                                                page_idx                        <= page_idx + 1;
489
                                                io_wr_data_in(15 downto 8) <= page_data(page_idx);
490
                                                substate                        <= MS_WRITE_DATA3;
491
 
492
                                        elsif(substate = MS_WRITE_DATA3)then
493
                                                if(byte_count < data_bytes_per_page + oob_bytes_per_page)then
494
                                                        substate                <= MS_WRITE_DATA1;
495
                                                else
496
                                                        substate                <= MS_SUBMIT_COMMAND1;
497
                                                end if;
498
                                                n_state                 <= M_NAND_PAGE_PROGRAM;
499
                                                state                           <= M_WAIT;
500
 
501
                                        elsif(substate = MS_SUBMIT_COMMAND1)then
502
                                                cle_data_in             <= x"0010";
503
                                                n_state                 <= M_NAND_PAGE_PROGRAM;
504
                                                state                           <= M_WAIT;
505
                                                substate                        <= MS_WAIT;
506
 
507
                                        elsif(substate = MS_WAIT)then
508
                                                delay                           <= t_wb + t_prog;
509
                                                state                           <= M_DELAY;
510
                                                n_state                 <= M_NAND_PAGE_PROGRAM;
511
                                                substate                        <= MS_END;
512
                                                byte_count              <= 0;
513
                                                page_idx                        <= 0;
514
 
515
                                        elsif(substate = MS_END)then
516
                                                state                           <= M_WAIT;
517
                                                n_state                 <= M_IDLE;
518
                                                substate                        <= MS_BEGIN;
519
                                        end if;
520
 
521
 
522
                                -- Reads single page into the buffer.
523
                                when M_NAND_READ =>
524
                                        if(substate = MS_BEGIN)then
525
                                                cle_data_in             <= x"0000";
526
                                                substate                        <= MS_SUBMIT_COMMAND;
527
                                                state                           <= M_WAIT;
528
                                                n_state                 <= M_NAND_READ;
529
                                                byte_count              <= 0;
530
 
531
                                        elsif(substate = MS_SUBMIT_COMMAND)then
532
                                                byte_count              <= byte_count + 1;
533
                                                ale_data_in             <= x"00"&current_address(byte_count);
534
                                                substate                        <= MS_SUBMIT_ADDRESS;
535
 
536
                                        elsif(substate = MS_SUBMIT_ADDRESS)then
537
                                                if(byte_count < addr_cycles)then
538
                                                        substate                <= MS_SUBMIT_COMMAND;
539
                                                else
540
                                                        substate                <= MS_SUBMIT_COMMAND1;
541
                                                end if;
542
                                                state                           <= M_WAIT;
543
                                                n_state                 <= M_NAND_READ;
544
 
545
                                        elsif(substate = MS_SUBMIT_COMMAND1)then
546
                                                cle_data_in             <= x"0030";
547
                                                substate                        <= MS_DELAY;
548
                                                state                   <= M_WAIT;
549
                                                n_state                 <= M_NAND_READ;
550
 
551
                                        elsif(substate = MS_DELAY)then
552
                                                delay                           <= t_wb;
553
                                                substate                        <= MS_READ_DATA0;
554
                                                state                           <= M_DELAY;
555
                                                n_state                 <= M_NAND_READ;
556
                                                byte_count              <= 0;
557
                                                page_idx                        <= 0;
558
 
559
                                        elsif(substate = MS_READ_DATA0)then
560
                                                byte_count              <= byte_count + 1;
561
                                                n_state                 <= M_NAND_READ;
562
                                                state                           <= M_WAIT;
563
                                                substate                        <= MS_READ_DATA1;
564
 
565
                                        elsif(substate = MS_READ_DATA1)then
566
                                                page_data(page_idx)     <= io_rd_data_out(7 downto 0);
567
                                                page_idx                        <= page_idx + 1;
568
                                                if(byte_count = data_bytes_per_page + oob_bytes_per_page and status(1) = '0')then
569
                                                        substate                <= MS_END;
570
                                                else
571
                                                        if(status(1) = '0')then
572
                                                                substate                <= MS_READ_DATA0;
573
                                                        else
574
                                                                substate                <= MS_READ_DATA2;
575
                                                        end if;
576
                                                end if;
577
 
578
                                        elsif(substate = MS_READ_DATA2)then
579
                                                page_idx                        <= page_idx + 1;
580
                                                page_data(page_idx)     <= io_rd_data_out(15 downto 8);
581
                                                if(byte_count = data_bytes_per_page + oob_bytes_per_page)then
582
                                                        substate                <= MS_END;
583
                                                else
584
                                                        substate                <= MS_READ_DATA0;
585
                                                end if;
586
 
587
                                        elsif(substate = MS_END)then
588
                                                substate                        <= MS_BEGIN;
589
                                                state                           <= M_IDLE;
590
                                                byte_count              <= 0;
591
                                        end if;
592
 
593
                                -- Read status byte
594
                                when M_NAND_READ_STATUS =>
595
                                        if(substate = MS_BEGIN)then
596
                                                cle_data_in             <= x"0070";
597
                                                substate                        <= MS_SUBMIT_COMMAND;
598
                                                state                           <= M_WAIT;
599
                                                n_state                 <= M_NAND_READ_STATUS;
600
 
601
                                        elsif(substate = MS_SUBMIT_COMMAND)then
602
                                                delay                           <= t_whr;
603
                                                substate                        <= MS_READ_DATA0;
604
                                                state                           <= M_DELAY;
605
                                                n_state                 <= M_NAND_READ_STATUS;
606
 
607
                                        elsif(substate = MS_READ_DATA0)then
608
                                                substate                        <= MS_READ_DATA1;
609
                                                state                           <= M_WAIT;
610
                                                n_state                 <= M_NAND_READ_STATUS;
611
 
612
                                        elsif(substate = MS_READ_DATA1)then                                             -- This is to make sure 'data_out' has valid data before 'busy' goes low.
613
                                                data_out                        <= io_rd_data_out(7 downto 0);
614
                                                state                           <= M_NAND_READ_STATUS;
615
                                                substate                        <= MS_END;
616
 
617
                                        elsif(substate = MS_END)then
618
                                                substate                        <= MS_BEGIN;
619
                                                state                           <= M_IDLE;
620
                                        end if;
621
 
622
                                -- Erase block specified by current_address
623
                                when M_NAND_BLOCK_ERASE =>
624
                                        if(substate = MS_BEGIN)then
625
                                                cle_data_in             <= x"0060";
626
                                                substate                        <= MS_SUBMIT_COMMAND;
627
                                                state                           <= M_WAIT;
628
                                                n_state                 <= M_NAND_BLOCK_ERASE;
629
                                                byte_count              <= 3;                                                   -- number of address bytes to submit
630
 
631
                                        elsif(substate = MS_SUBMIT_COMMAND)then
632
                                                byte_count              <= byte_count - 1;
633
                                                ale_data_in(15 downto 8) <= x"00";
634
                                                ale_data_in(7 downto 0)  <= current_address(5 - byte_count);
635
                                                substate                        <= MS_SUBMIT_ADDRESS;
636
                                                state                           <= M_WAIT;
637
                                                n_state                 <= M_NAND_BLOCK_ERASE;
638
 
639
                                        elsif(substate = MS_SUBMIT_ADDRESS)then
640
                                                if(0 < byte_count)then
641
                                                        substate                <= MS_SUBMIT_COMMAND;
642
                                                else
643
                                                        substate                <= MS_SUBMIT_COMMAND1;
644
                                                end if;
645
 
646
                                        elsif(substate = MS_SUBMIT_COMMAND1)then
647
                                                cle_data_in             <= x"00d0";
648
                                                substate                        <= MS_END;
649
                                                state                           <= M_WAIT;
650
                                                n_state                 <= M_NAND_BLOCK_ERASE;
651
 
652
                                        elsif(substate = MS_END)then
653
                                                n_state                 <= M_IDLE;
654
                                                delay                   <= t_wb + t_bers;
655
                                                state                           <= M_DELAY;
656
                                                substate                        <= MS_BEGIN;
657
                                                byte_count              <= 0;
658
                                        end if;
659
 
660
                                -- Read NAND chip JEDEC ID
661
                                when M_NAND_READ_ID =>
662
                                        if(substate = MS_BEGIN)then
663
                                                cle_data_in             <= x"0090";
664
                                                substate                        <= MS_SUBMIT_COMMAND;
665
                                                state                           <= M_WAIT;
666
                                                n_state                         <= M_NAND_READ_ID;
667
 
668
                                        elsif(substate = MS_SUBMIT_COMMAND)then
669
                                                ale_data_in             <= X"0000";
670
                                                substate                        <= MS_SUBMIT_ADDRESS;
671
                                                state                   <= M_WAIT;
672
                                                n_state                 <= M_NAND_READ_ID;
673
 
674
                                        elsif(substate = MS_SUBMIT_ADDRESS)then
675
                                                delay                           <= t_wb;
676
                                                state                           <= M_DELAY;
677
                                                n_state                 <= M_NAND_READ_ID;
678
                                                substate                        <= MS_READ_DATA0;
679
                                                byte_count              <= 5;
680
                                                page_idx                        <= 0;
681
 
682
                                        elsif(substate = MS_READ_DATA0)then
683
                                                byte_count              <= byte_count - 1;
684
                                                state                           <= M_WAIT;
685
                                                n_state                 <= M_NAND_READ_ID;
686
                                                substate                        <= MS_READ_DATA1;
687
 
688
                                        elsif(substate = MS_READ_DATA1)then
689
                                                chip_id(page_idx)       <= io_rd_data_out(7 downto 0);
690
                                                if(0 < byte_count)then
691
                                                        page_idx                                        <= page_idx + 1;
692
                                                        substate                                        <= MS_READ_DATA0;
693
                                                else
694
                                                        substate                                        <= MS_END;
695
                                                end if;
696
 
697
                                        elsif(substate = MS_END)then
698
                                                byte_count              <= 0;
699
                                                page_idx                        <= 0;
700
                                                substate                        <= MS_BEGIN;
701
                                                state                           <= M_IDLE;
702
                                        end if;
703
 
704
                                -- *data_in is assigned one clock cycle after *_activate is triggered!!!!
705
                                -- According to ONFI's timing diagrams this should be normal, but who knows...
706
                                when M_NAND_READ_PARAM_PAGE =>
707
                                        if(substate = MS_BEGIN)then
708
                                                cle_data_in             <= x"00ec";
709
                                                substate                        <= MS_SUBMIT_COMMAND;
710
                                                state                           <= M_WAIT;
711
                                                n_state                         <= M_NAND_READ_PARAM_PAGE;
712
 
713
                                        elsif(substate = MS_SUBMIT_COMMAND)then
714
                                                ale_data_in             <= X"0000";
715
                                                substate                        <= MS_SUBMIT_ADDRESS;
716
                                                state                   <= M_WAIT;
717
                                                n_state                 <= M_NAND_READ_PARAM_PAGE;
718
 
719
                                        elsif(substate = MS_SUBMIT_ADDRESS)then
720
                                                delay                           <= t_wb;
721
                                                state                           <= M_DELAY;
722
                                                n_state                 <= M_NAND_READ_PARAM_PAGE;
723
                                                substate                        <= MS_READ_DATA0;
724
                                                byte_count              <= 256;
725
                                                page_idx                        <= 0;
726
 
727
                                        elsif(substate = MS_READ_DATA0)then
728
                                                byte_count              <= byte_count - 1;
729
                                                state                           <= M_WAIT;
730
                                                n_state                 <= M_NAND_READ_PARAM_PAGE;
731
                                                substate                        <= MS_READ_DATA1;
732
 
733
                                        elsif(substate = MS_READ_DATA1)then
734
                                                page_param(page_idx)    <= io_rd_data_out(7 downto 0);
735
                                                if(0 < byte_count)then
736
                                                        page_idx                                        <= page_idx + 1;
737
                                                        substate                                        <= MS_READ_DATA0;
738
                                                else
739
                                                        substate                                        <= MS_END;
740
                                                end if;
741
 
742
                                        elsif(substate = MS_END)then
743
                                                byte_count              <= 0;
744
                                                page_idx                        <= 0;
745
                                                substate                        <= MS_BEGIN;
746
                                                state                           <= M_IDLE;
747
 
748
                                                -- Check the chip for being ONFI compliant
749
                                                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
750
                                                        -- Set status bit 0
751
                                                        status(0)                                        <= '1';
752
 
753
                                                        -- Bus width
754
                                                        status(1)                                       <= page_param(6)(0);
755
 
756
                                                        -- Setup counters:
757
                                                        -- Number of bytes per page
758
                                                        tmp_int                                         := page_param(83)&page_param(82)&page_param(81)&page_param(80);
759
                                                        data_bytes_per_page             <= to_integer(unsigned(tmp_int));
760
 
761
                                                        -- Number of spare bytes per page (OOB)
762
                                                        tmp_int                                         := "0000000000000000" & page_param(85) & page_param(84);
763
                                                        oob_bytes_per_page              <= to_integer(unsigned(tmp_int));
764
 
765
                                                        -- Number of address cycles
766
                                                        addr_cycles                                     <= to_integer(unsigned(page_param(101)(3 downto 0))) + to_integer(unsigned(page_param(101)(7 downto 4)));
767
                                                end if;
768
                                        end if;
769
 
770
                                -- Wait for latch and IO modules to become ready as well as for NAND's R/B# to be '1'
771
                                when M_WAIT =>
772
                                        if('0' = (cle_busy or ale_busy or io_rd_busy or io_wr_busy or (not nand_rnb)))then
773
                                                state                           <= n_state;
774
                                        end if;
775
 
776
                                -- Simple delay mechanism
777
                                when M_DELAY =>
778
                                        if(delay > 1)then
779
                                                delay                   <= delay - 1;
780
                                        else
781
                                                state                           <= n_state;
782
                                        end if;
783
 
784
                                -- For just in case ("Shit happens..." (C) Forrest Gump)
785
                                when others =>
786
                                        state                           <= M_RESET;
787
                        end case;
788
                end if;
789
        end process;
790
end struct;
791
 

powered by: WebSVN 2.1.0

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