-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy paths25fl256s_x2.vhd
574 lines (499 loc) · 22.4 KB
/
s25fl256s_x2.vhd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
library IEEE;
use IEEE.std_logic_1164.ALL;
use IEEE.NUMERIC_STD.ALL;
entity s25fl256s_x2 is
Port (
CLK_I : in std_logic;
RESET_I : in std_logic;
CMD_I : in std_logic_vector(1 downto 0);
CMD_ADDR_I : in std_logic_vector(23 downto 0);
CMD_EN_I : in std_logic;
CMD_RDY_O : out std_logic;
DATA_READ_O : out std_logic_vector(31 downto 0);
DATA_READ_VALID_O : out std_logic;
READ_CONTINUOUS_I : in std_logic;
WRITE_FIFO_EMPTY_I : in std_logic;
WRITE_FIFO_DATA_I : in std_logic_vector(7 downto 0);
WRITE_FIFO_RDEN_O : out std_logic;
-- Capacity detection
ADDR_WIDTH_O : out std_logic_vector(7 downto 0);
-- QSPI
CSN_O : out std_logic_vector (1 downto 0);
SCK_O : out std_logic;
DQ_IO : inout std_logic_vector (3 downto 0)
);
end s25fl256s_x2;
architecture Behavioral of s25fl256s_x2 is
component BB
port (
I : in std_logic;
T : in std_logic;
O : out std_logic;
B : inout std_logic
);
end component;
constant CMD_NONE : std_logic_vector(1 downto 0) := "00";
constant CMD_READ : std_logic_vector(1 downto 0) := "01";
constant CMD_WRITE : std_logic_vector(1 downto 0) := "10";
constant CMD_ERASE : std_logic_vector(1 downto 0) := "11";
constant FLASH_CMD_READ_JEDEC_ID : std_logic_vector(7 downto 0) := x"9F";
constant FLASH_CMD_FASTREAD_QUADIO : std_logic_vector(7 downto 0) := x"EC";
constant FLASH_CMD_WREN : std_logic_vector(7 downto 0) := x"06";
constant FLASH_CMD_ERASE : std_logic_vector(7 downto 0) := x"DC";
constant FLASH_CMD_QUAD_PAGE_PROGRAM : std_logic_vector(7 downto 0) := x"34";
constant FLASH_CMD_READ_STATUS_REGISTER1 : std_logic_vector(7 downto 0) := x"05";
constant FLASH_CMD_WRITE_STATUS_REGISTER1 : std_logic_vector(7 downto 0) := x"01";
type state_t is (
s_init,
s_readid,
s_readid_end,
s_wren_init_cmd,
s_wren_init_end,
s_set_qe_cmd,
s_set_qe_end,
s_set_qe_wip_cmd,
s_set_qe_wip_end,
s_pause,
s_pre_idle,
s_idle,
s_read_cmd,
s_read_data,
s_wren_cmd,
s_wren_end,
s_erase_cmd,
s_write_cmd,
s_write_data,
s_write_end,
s_read_wip_cmd,
s_read_wip_data
);
signal spi_sck : std_logic;
signal spi_dq_in : std_logic_vector(3 downto 0);
signal spi_dq_out : std_logic_vector(3 downto 0);
signal spi_dq_tris : std_logic_vector(3 downto 0);
signal read_data : std_logic_vector(31 downto 0);
signal state : state_t;
signal cmd_addr : std_logic_vector(23 downto 0);
signal chip_sel : std_logic_vector(1 downto 0);
signal cmd : std_logic_vector(1 downto 0);
signal shift_counter : std_logic_vector(40 downto 0);
signal spi_shift_out : std_logic_vector(7 downto 0);
signal spi_shift_in : std_logic_vector(7 downto 0);
signal spi_clock_en : std_logic;
signal spi_quad_en : std_logic;
signal spi_output_tris : std_logic;
signal fifo_empty : std_logic;
signal manufacturer_id1 : std_logic_vector(7 downto 0);
signal mem_type_id1 : std_logic_vector(7 downto 0);
signal capacity_id1 : std_logic_vector(7 downto 0);
signal manufacturer_id2 : std_logic_vector(7 downto 0);
signal mem_type_id2 : std_logic_vector(7 downto 0);
signal capacity_id2 : std_logic_vector(7 downto 0);
attribute syn_state_machine : boolean;
attribute syn_state_machine of Behavioral : architecture is true;
attribute syn_encoding : string;
attribute syn_encoding of state: signal is "onehot";
begin
SCK_O <= spi_sck;
DQ_BUFFER : for index in 0 to 3 generate
DQ_BB : BB
port map (
I => spi_dq_out(index),
T => spi_dq_tris(index),
O => spi_dq_in(index),
B => DQ_IO(index)
);
end generate DQ_BUFFER;
process (spi_output_tris, spi_quad_en, spi_shift_out)
begin
if spi_quad_en = '0' then
spi_dq_out <= "000" & spi_shift_out(spi_shift_out'high);
spi_dq_tris <= "1110";
else
spi_dq_out <= spi_shift_out(spi_shift_out'high downto spi_shift_out'high - 3);
if spi_output_tris = '0' then
spi_dq_tris <= "0000";
else
spi_dq_tris <= "1111";
end if;
end if;
end process;
DATA_READ_O <= read_data;
CMD_RDY_O <= '1' when state = s_idle else '0';
chip_sel <= not cmd_addr(23) & cmd_addr(23);
ADDR_WIDTH_PROC: process (capacity_id1, capacity_id2, manufacturer_id1, manufacturer_id2)
begin
ADDR_WIDTH_O <= x"00";
if manufacturer_id1 = x"01" and capacity_id1 = x"19" then
if manufacturer_id2 = x"01" and capacity_id2 = x"19" then
ADDR_WIDTH_O <= x"1A";
else
ADDR_WIDTH_O <= x"19";
end if;
end if;
end process;
process (CLK_I, RESET_I)
begin
if RESET_I = '1' then
spi_sck <= '0';
CSN_O <= "11";
state <= s_init;
spi_shift_out <= (others => '0');
spi_shift_in <= (others => '0');
spi_clock_en <= '0';
spi_quad_en <= '0';
spi_output_tris <= '0';
shift_counter <= (others => '0');
read_data <= (others => '0');
DATA_READ_VALID_O <= '0';
WRITE_FIFO_RDEN_O <= '0';
elsif rising_edge(CLK_I) then
DATA_READ_VALID_O <= '0';
WRITE_FIFO_RDEN_O <= '0';
if spi_clock_en = '1' then
spi_sck <= not spi_sck;
if spi_sck = '1' then
if spi_quad_en = '1' then
spi_shift_out <= spi_shift_out(spi_shift_out'high - 4 downto spi_shift_out'low) & "0000";
spi_shift_in <= spi_shift_in(spi_shift_in'high - 4 downto spi_shift_in'low) & spi_dq_in;
else
spi_shift_out <= spi_shift_out(spi_shift_out'high - 1 downto spi_shift_out'low) & '0';
spi_shift_in <= spi_shift_in(spi_shift_in'high - 1 downto spi_shift_in'low) & spi_dq_in(1);
end if;
shift_counter <= shift_counter(shift_counter'high - 1 downto shift_counter'low) & '0';
end if;
else
spi_sck <= '0';
end if;
case state is
when s_init =>
-- state <= s_idle;
state <= s_readid;
spi_shift_out <= FLASH_CMD_READ_JEDEC_ID;
spi_clock_en <= '1';
CSN_O <= "10";
shift_counter <= "00000000000000000000000000000000000000001";
cmd_addr <= (others => '0');
when s_readid =>
if spi_sck = '0' then
if shift_counter(16) = '1' then
if cmd_addr(23) = '0' then
manufacturer_id1 <= spi_shift_in;
else
manufacturer_id2 <= spi_shift_in;
end if;
end if;
if shift_counter(24) = '1' then
if cmd_addr(23) = '0' then
mem_type_id1 <= spi_shift_in;
else
mem_type_id2 <= spi_shift_in;
end if;
end if;
if shift_counter(32) = '1' then
if cmd_addr(23) = '0' then
capacity_id1 <= spi_shift_in;
else
capacity_id2 <= spi_shift_in;
end if;
CSN_O <= "11";
spi_clock_en <= '0';
spi_sck <= '0';
state <= s_readid_end;
shift_counter <= "00100000000000000000000000000000000000000";
end if;
end if;
when s_readid_end =>
spi_shift_out <= FLASH_CMD_WREN;
spi_clock_en <= '1';
CSN_O <= chip_sel;
shift_counter <= "00000000000000000000000000000000000000001";
state <= s_wren_init_cmd;
when s_wren_init_cmd =>
if spi_sck = '0' then
if shift_counter(8) = '1' then
CSN_O <= "11";
spi_clock_en <= '0';
spi_sck <= '0';
state <= s_wren_init_end;
shift_counter <= "00000100000000000000000000000000000000000";
end if;
end if;
when s_wren_init_end =>
shift_counter <= shift_counter(shift_counter'high - 1 downto shift_counter'low) & '0';
if shift_counter(40) = '1' then
spi_shift_out <= FLASH_CMD_WRITE_STATUS_REGISTER1;
spi_clock_en <= '1';
CSN_O <= chip_sel;
shift_counter <= "00000000000000000000000000000000000000001";
state <= s_set_qe_cmd;
end if;
when s_set_qe_cmd =>
if spi_sck = '0' then
if shift_counter(24) = '1' then
CSN_O <= "11";
spi_clock_en <= '0';
spi_sck <= '0';
state <= s_set_qe_end;
end if;
else
if shift_counter(15) = '1' then
spi_shift_out <= x"02";
end if;
end if;
when s_set_qe_end =>
state <= s_set_qe_wip_cmd;
spi_shift_out <= FLASH_CMD_READ_STATUS_REGISTER1;
spi_clock_en <= '1';
CSN_O <= chip_sel;
shift_counter <= "00000000000000000000000000000000000000001";
when s_set_qe_wip_cmd =>
if spi_sck = '1' then
if shift_counter(7) = '1' then
shift_counter <= "00000000000000000000000000000000000000001";
state <= s_set_qe_wip_end;
end if;
end if;
when s_set_qe_wip_end =>
if spi_sck = '0' then
if shift_counter(8) = '1' then
if spi_shift_in(0) = '1' then
shift_counter <= "00000000000000000000000000000000000000001";
else
spi_sck <= '0';
spi_clock_en <= '0';
spi_sck <= '0';
CSN_O <= "11";
-- was this the first chip?
if cmd_addr(23) = '0' then
-- repeat the procedure for the second chip
cmd_addr(23) <= '1';
shift_counter <= "00001000000000000000000000000000000000000";
state <= s_pause;
else
-- second chip done
state <= s_pre_idle;
shift_counter <= "00000100000000000000000000000000000000000";
end if;
end if;
end if;
end if;
when s_pause =>
shift_counter <= shift_counter(shift_counter'high - 1 downto shift_counter'low) & '0';
if shift_counter(40) = '1' then
spi_shift_out <= FLASH_CMD_READ_JEDEC_ID;
spi_clock_en <= '1';
CSN_O <= chip_sel;
shift_counter <= "00000000000000000000000000000000000000001";
state <= s_readid;
end if;
when s_pre_idle =>
-- this state is used to garantee the device deselect time
shift_counter <= shift_counter(shift_counter'high - 1 downto shift_counter'low) & '0';
if shift_counter(40) = '1' then
state <= s_idle;
end if;
when s_idle =>
spi_output_tris <= '0';
spi_quad_en <= '0';
if CMD_EN_I = '1' then
spi_clock_en <= '1';
if CMD_ADDR_I(23) = '0' then
CSN_O <= "10";
else
CSN_O <= "01";
end if;
shift_counter <= "00000000000000000000000000000000000000001";
cmd <= CMD_I;
cmd_addr <= CMD_ADDR_I;
case CMD_I is
when CMD_READ =>
spi_shift_out <= FLASH_CMD_FASTREAD_QUADIO;
state <= s_read_cmd;
when CMD_ERASE =>
spi_shift_out <= FLASH_CMD_WREN;
state <= s_wren_cmd;
when CMD_WRITE =>
spi_shift_out <= FLASH_CMD_WREN;
state <= s_wren_cmd;
when others =>
spi_clock_en <= '0';
CSN_O <= "11";
end case;
end if;
when s_read_cmd =>
if spi_sck = '1' then
if shift_counter(7) = '1' then
spi_quad_en <= '1';
spi_shift_out <= "0000000" & cmd_addr(22);
end if;
if shift_counter(9) = '1' then
spi_shift_out <= cmd_addr(21 downto 14);
end if;
if shift_counter(11) = '1' then
spi_shift_out <= cmd_addr(13 downto 6);
end if;
if shift_counter(13) = '1' then
spi_shift_out <= cmd_addr(5 downto 0) & "00";
end if;
if shift_counter(15) = '1' then
spi_shift_out <= x"FF";
end if;
if shift_counter(17) = '1' then
spi_output_tris <= '1';
end if;
if shift_counter(22) = '1' then
state <= s_read_data;
shift_counter <= "00000000000000000000000000000000000000001";
end if;
end if;
when s_read_data =>
if spi_sck = '0' then
if shift_counter(1) = '1' then
read_data(7 downto 0) <= spi_shift_in;
end if;
if shift_counter(3) = '1' then
read_data(15 downto 8) <= spi_shift_in;
end if;
if shift_counter(5) = '1' then
read_data(23 downto 16) <= spi_shift_in;
end if;
if shift_counter(7) = '1' then
read_data(31 downto 24) <= spi_shift_in;
DATA_READ_VALID_O <= '1';
if READ_CONTINUOUS_I = '0' then
spi_clock_en <= '0';
spi_sck <= '0';
CSN_O <= "11";
shift_counter <= "10000000000000000000000000000000000000000";
state <= s_pre_idle;
end if;
end if;
else
if shift_counter(7) = '1' then
-- we will only get here if READ_CONTINUOUS_I was '1' in the last cycle
shift_counter <= "00000000000000000000000000000000000000001";
end if;
end if;
when s_wren_cmd =>
if spi_sck = '0' then
if shift_counter(8) = '1' then
CSN_O <= "11";
spi_clock_en <= '0';
spi_sck <= '0';
state <= s_wren_end;
end if;
end if;
when s_wren_end =>
spi_clock_en <= '1';
CSN_O <= chip_sel;
shift_counter <= "00000000000000000000000000000000000000001";
if cmd = CMD_ERASE then
spi_shift_out <= FLASH_CMD_ERASE;
state <= s_erase_cmd;
else
spi_shift_out <= FLASH_CMD_QUAD_PAGE_PROGRAM;
state <= s_write_cmd;
end if;
when s_erase_cmd =>
if spi_sck = '0' then
if shift_counter(40) = '1' then
CSN_O <= "11";
spi_clock_en <= '0';
spi_sck <= '0';
state <= s_write_end;
shift_counter <= "00000100000000000000000000000000000000000";
end if;
else
if shift_counter(7) = '1' then
spi_shift_out <= "0000000" & cmd_addr(22);
end if;
if shift_counter(15) = '1' then
spi_shift_out <= cmd_addr(21 downto 14);
end if;
if shift_counter(23) = '1' then
spi_shift_out <= cmd_addr(13 downto 6);
end if;
if shift_counter(31) = '1' then
spi_shift_out <= cmd_addr(5 downto 0) & "00";
end if;
end if;
when s_write_cmd =>
if spi_sck = '1' then
if shift_counter(7) = '1' then
spi_shift_out <= "0000000" & cmd_addr(22);
end if;
if shift_counter(15) = '1' then
spi_shift_out <= cmd_addr(21 downto 14);
end if;
if shift_counter(23) = '1' then
spi_shift_out <= cmd_addr(13 downto 6);
end if;
if shift_counter(31) = '1' then
spi_shift_out <= cmd_addr(5 downto 0) & "00";
-- make the first data available
WRITE_FIFO_RDEN_O <= '1';
end if;
if shift_counter(39) = '1' then
state <= s_write_data;
spi_quad_en <= '1';
spi_shift_out <= WRITE_FIFO_DATA_I;
-- make the next data available
WRITE_FIFO_RDEN_O <= '1';
fifo_empty <= WRITE_FIFO_EMPTY_I;
shift_counter <= "00000000000000000000000000000000000000001";
end if;
end if;
when s_write_data =>
if spi_sck = '1' then
if shift_counter(1) = '1' then
if fifo_empty = '0' then
spi_shift_out <= WRITE_FIFO_DATA_I;
-- make the next data available
WRITE_FIFO_RDEN_O <= '1';
fifo_empty <= WRITE_FIFO_EMPTY_I;
shift_counter <= "00000000000000000000000000000000000000001";
else
CSN_O <= "11";
spi_clock_en <= '0';
spi_quad_en <= '0';
state <= s_write_end;
shift_counter <= "00000100000000000000000000000000000000000";
end if;
end if;
end if;
when s_write_end =>
shift_counter <= shift_counter(shift_counter'high - 1 downto shift_counter'low) & '0';
if shift_counter(40) = '1' then
state <= s_read_wip_cmd;
spi_shift_out <= FLASH_CMD_READ_STATUS_REGISTER1;
spi_clock_en <= '1';
CSN_O <= chip_sel;
shift_counter <= "00000000000000000000000000000000000000001";
end if;
when s_read_wip_cmd =>
if spi_sck = '1' then
if shift_counter(7) = '1' then
shift_counter <= "00000000000000000000000000000000000000001";
state <= s_read_wip_data;
end if;
end if;
when s_read_wip_data =>
if spi_sck = '0' then
if shift_counter(8) = '1' then
if spi_shift_in(0) = '1' then
shift_counter <= "00000000000000000000000000000000000000001";
else
spi_sck <= '0';
spi_clock_en <= '0';
spi_sck <= '0';
CSN_O <= "11";
state <= s_idle;
end if;
end if;
end if;
when others => state <= s_init;
end case;
end if;
end process;
end Behavioral;