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 19

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

powered by: WebSVN 2.1.0

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