-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathmdl_emmc.v
1510 lines (1403 loc) · 36.8 KB
/
mdl_emmc.v
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
////////////////////////////////////////////////////////////////////////////////
//
// Filename: bench/verilog/mdl_emmc.v
// {{{
// Project: SD-Card controller
//
// Purpose: The SDIO SD-Card controller can operate on either SD cards or
// eMMC cards. Since the two aren't quite the same, a separate
// model is needed when interacting with an eMMC card. One difference,
// for example, is that the eMMC bus is defined for up to 8 IO pins,
// whereas the SDIO model only ever uses up to 4 IO pins. A second
// key difference, is that the eMMC protocol allows data-strobe qualified
// IO--something never exploited by the SDIO interface. Other differences
// exist at the command layer.
//
// Creator: Dan Gisselquist, Ph.D.
// Gisselquist Technology, LLC
//
////////////////////////////////////////////////////////////////////////////////
// }}}
// Copyright (C) 2016-2025, Gisselquist Technology, LLC
// {{{
// This program is free software (firmware): you can redistribute it and/or
// modify it under the terms of the GNU General Public License as published
// by the Free Software Foundation, either version 3 of the License, or (at
// your option) any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program. (It's in the $(ROOT)/doc directory. Run make with no
// target there if the PDF file isn't present.) If not, see
// <http://www.gnu.org/licenses/> for a copy.
// }}}
// License: GPL, v3, as defined and found on www.gnu.org,
// {{{
// http://www.gnu.org/licenses/gpl.html
//
////////////////////////////////////////////////////////////////////////////////
//
`timescale 1ns/1ps
// }}}
module mdl_emmc #(
// {{{
parameter [0:0] OPT_DUAL_VOLTAGE = 1'b0,
parameter [0:0] OPT_HIGH_CAPACITY = 1'b0,
parameter LGMEMSZ = 20, // Log_2(Mem size in bytes)
parameter LGBOOTSZ = 17, // Minimum of 17, for 128kB
parameter MAX_BLKLEN = 512, // Max Blk Size in bytes
// MEM_HEX: If non-zero, is the name of a hex file to be used
// to initialize the main memory of the device.
parameter MEM_HEX = 0,
// BOOT_HEX: If non-zero, is the name of a hex file to be used
// to initialize the first boot memory of the device on
// startup.
parameter BOOT_HEX = 0,
localparam LGBLKSZ = $clog2(MAX_BLKLEN/4),
localparam MEMSZ = (1<<LGMEMSZ),
localparam BOOTSZ = (1<<LGBOOTSZ)
// }}}
) (
// {{{
input wire rst_n,
input wire sd_clk,
inout wire sd_cmd,
inout wire [7:0] sd_dat,
output wire sd_ds,
input wire i_1p8v
// }}}
);
// Local declarations
// {{{
localparam [3:0] EMMC_IDLE = 4'h0,
EMMC_READY = 4'h1,
EMMC_IDENTIFICATION = 4'h2,
EMMC_STANDBY = 4'h3,
EMMC_TRANSFER = 4'h4,
EMMC_SEND_DATA = 4'h5, // = DATA
EMMC_RECEIVE_DATA = 4'h6, // = RCV
EMMC_PROGRAMMING = 4'h7, // = PRG
EMMC_DISCONNECT = 4'h8,
EMMC_BUS_TEST = 4'h9,
//
EMMC_INACTIVE = 4'ha,
EMMC_PRE_IDLE = 4'hb,
// EMMC_PRE_BOOT = 4'hc,
EMMC_BOOT = 4'hd,
EMMC_WAIT_IRQ = 4'he,
EMMC_SLEEP = 4'hf;
localparam [0:0] REPLY_48B = 1'b0,
REPLY_136B = 1'b1;
localparam [31:0] ERR_ADDRRANGE = (32'h1 << 31),
ERR_ILLEGALCMD = (32'h1 << 22),
ERR_BLOCKLEN = (32'h1 << 20),
ERR_SWITCH = (32'h1 << 7);
parameter realtime tPROG = 5120; // 512ns, or about a half uS
parameter realtime WRITE_TIME = 512;
parameter realtime DS_DELAY = 0.4;
reg [3:0] card_state;
reg cfg_ddr, cfg_ppull;
reg [1:0] cfg_width;
reg [2:0] cfg_partition;
wire cmd_valid, cmd_ds, cmd_crc_err;
wire [5:0] cmd;
wire [31:0] cmd_arg;
reg reply_valid, reply_type, reply_crc;
wire reply_busy;
reg [5:0] reply;
reg [119:0] reply_data;
reg [119:0] CID, CSD;
reg [31:0] ocr;
reg power_up_busy, cmd_alt, card_reset, sector_addressing;
// reg r_1p8v_request, r_1p8v;
reg [15:0] RCA;
reg [31:0] R1;
reg drive_cmd, card_selected;
wire cmd_collision;
reg read_en, pending_read, multi_block;
wire rx_valid, rx_last, rx_good, rx_err;
wire [31:0] rx_data;
integer read_ik;
reg write_en, tx_valid, tx_last, pending_write;
reg [31:0] tx_data;
wire tx_ready, tx_ds;
reg [LGBLKSZ-1:0] tx_addr;
reg [31:0] mem_buf [0:(MAX_BLKLEN/4)-1];
reg [LGBLKSZ-1:0] rx_addr;
reg [31:0] mem [0:(MEMSZ/4)-1];
reg [31:0] boot_mem1 [0:(BOOTSZ/4)-1];
reg [31:0] boot_mem2 [0:(BOOTSZ/4)-1];
reg [LGMEMSZ-1:0] read_posn;
integer write_ik;
reg [7:0] ext_csd [0:511];
reg [15:0] block_len;
reg bustest_w, bustest_r, clear_errors, write_ext_csd,
SQS;
wire [31:0] QSR;
reg [6:0] boot_clk_count;
reg boot_mode, boot_active;
reg busy_programming;
reg err_addr_out_of_range, err_address_misalign,
err_erase_seq_error,
err_erase_param, err_wp_violation,
err_device_is_locked, err_lock_unlock_failed,
err_com_crc_error, err_illegal_command,
err_device_ecc_failed, err_cc_error,
err_generic_error, err_cidcsd_overwrite,
err_wp_erase_skip, err_erase_reset,
err_switch_error;
// err_block_len_error,
// R1[6] = exception_event;
// R1[5] = cmd_alt;
reg [7:0] ext_index, next_ext_byte;
reg ds_enabled = 1'b0, enhanced_ds_enabled = 1'b0;
assign QSR = 32'hffff_ffff;
// }}}
////////////////////////////////////////////////////////////////////////
//
// Initial memory load
// {{{
////////////////////////////////////////////////////////////////////////
//
//
generate if (BOOT_HEX != 0)
begin
initial begin
$readmemh(BOOT_HEX, boot_mem1);
end
end endgenerate
generate if (MEM_HEX != 0)
begin
initial begin
$readmemh(MEM_HEX, boot_mem1);
end
end endgenerate
// }}}
////////////////////////////////////////////////////////////////////////
//
// Command wire handler
// {{{
mdl_sdcmd
tb_sdcmd (
// {{{
.rst_n(rst_n), .sd_clk(sd_clk), .sd_cmd(sd_cmd),
.sd_ds(cmd_ds),
//
.o_cmd_valid(cmd_valid), .o_cmd(cmd), .o_arg(cmd_arg),
.o_crc_err(cmd_crc_err),
//
.i_valid(reply_valid), .i_type(reply_type),
.o_busy(reply_busy), .i_reply(reply),
.i_arg(reply_data), .i_use_crc(reply_crc),
.i_drive(drive_cmd), .o_collision(cmd_collision)
// }}}
);
// }}}
////////////////////////////////////////////////////////////////////////
//
// Read data from the host (write operation)
// {{{
mdl_sdrx
tb_sdrx (
// {{{
.rst_n(rst_n), .sd_clk(sd_clk), .sd_dat( sd_dat ),
//
.i_rx_en(read_en), .i_width(cfg_width), .i_ddr(cfg_ddr),
.i_len(block_len),
//
.o_valid(rx_valid), .o_data(rx_data), .o_last(rx_last),
.o_good(rx_good), .o_err(rx_err)
// }}}
);
// }}}
////////////////////////////////////////////////////////////////////////
//
// Read from the card, send to the host/user (read operation)
// {{{
reg r_crcack, r_crcnak;
reg pending_ack, pending_nak;
mdl_sdtx
tb_sdtx (
// {{{
.rst_n(rst_n && (!boot_mode || sd_cmd === 1'b0)),
.sd_clk(sd_clk), .sd_dat( sd_dat ), .sd_ds(tx_ds),
//
.i_en(write_en),
.i_width(cfg_width), .i_ddr(cfg_ddr),
.i_ppull(cfg_ppull),
//
.i_crcack(r_crcack), .i_crcnak(r_crcnak),
//
.i_valid(tx_valid), .o_ready(tx_ready),
.i_data(tx_data), .i_last(tx_last)
// }}}
);
// }}}
////////////////////////////////////////////////////////////////////////
//
// Boot setup
// {{{
initial boot_mode = 1'b0;
initial boot_clk_count = 0;
always @(posedge sd_clk or negedge rst_n)
if (!rst_n)
boot_clk_count <= 0;
else if (sd_cmd !== 1'b0 || card_state != EMMC_PRE_IDLE)
boot_clk_count <= 0;
else if (!(&boot_clk_count))
boot_clk_count <= boot_clk_count + 1;
// }}}
////////////////////////////////////////////////////////////////////////
//
// eMMC FSM
// {{{
////////////////////////////////////////////////////////////////////////
//
// EXT CSD register support
// {{{
initial begin
for(read_ik=0; read_ik < 512; read_ik=read_ik + 1)
ext_csd[read_ik] = 8'h0;
// BOOT INFO
ext_csd[228] = 8'h7;
// BOOT_SIZE_MULT
ext_csd[226] = (8'h1 << (LGBOOTSZ - 17));
// Sector count
{ ext_csd[215], ext_csd[214], ext_csd[213], ext_csd[212] }
= (32'd1<<(LGMEMSZ-9));
// DRIVER_STRENGTH
ext_csd[197] = 8'h1;
// DEVICE_TYPE
ext_csd[196] = 8'hff; // Support all speeds up to HS400
// EXT_CSD_REV
ext_csd[192] = 8'd8;
// CMD_SET
ext_csd[191] = 8'd8;
// HS_TIMING
ext_csd[185] = 8'd0;
// STROBE_SUPPORT
ext_csd[184] = 8'd1;
// BUS_WIDTH
ext_csd[183] = 8'd0;
// Only some of these registers are properly implemented
end
always @(ext_csd[185])
begin
ds_enabled = (ext_csd[185][3:0] == 4'h3);
end
assign #DS_DELAY sd_ds = (ds_enabled
&& (tx_ds || (enhanced_ds_enabled && cmd_ds)));
// EXT-CSD[183]: cfg_ddr, cfg_width, and enhanced_ds_enabled
// {{{
reg [7:0] ext_csd_183;
always @(*)
ext_csd_183 = ext_csd[183];
always @(ext_csd[183], boot_mode, ext_csd[177])
begin
if (boot_mode)
begin
cfg_ddr = (ext_csd[177][4:3] == 2'h2);
case(ext_csd[177][1:0])
2'h0: cfg_width = (cfg_ddr) ? 2'h1 : 2'h0;
2'h1: cfg_width = 2'h1;
2'h2: cfg_width = 2'h2;
endcase
enhanced_ds_enabled = 1'b0;
end else begin
case(ext_csd[183][3:0])
4'h0: begin cfg_ddr = 1'b0; cfg_width = 2'b0; end
4'h1: begin cfg_ddr = 1'b0; cfg_width = 2'd1; end
4'h2: begin cfg_ddr = 1'b0; cfg_width = 2'd2; end
//
4'h5: begin cfg_ddr = 1'b1; cfg_width = 2'd1; end
4'h6: begin cfg_ddr = 1'b1; cfg_width = 2'd2; end
endcase
enhanced_ds_enabled = ext_csd[183][7];
end
end
// }}}
always @(*)
begin
ext_index = cmd_arg[23:16];
next_ext_byte = ext_csd[ext_index];
if (cmd_arg[25:24] == 2'b01)
next_ext_byte = cmd_arg[15:8];
else if (cmd_arg[25:24] == 2'b10)
next_ext_byte = next_ext_byte & (~cmd_arg[15:8]);
else if (cmd_arg[25:24] == 2'b11)
next_ext_byte = next_ext_byte | (cmd_arg[15:8]);
end
// SWITCH command processing to update ext_csd
always @(posedge sd_clk or negedge rst_n)
if (!rst_n)
begin
ext_csd[191] <= 8'd8;
ext_csd[185] <= 8'h0;
ext_csd[184] <= 8'd1;
ext_csd[183] <= 8'h0;
end else if (boot_mode)
begin
ext_csd[183] <= 8'h0;
if (ext_csd[179][5:3] == 3'h1)
cfg_partition <= 3'h1;
else if (ext_csd[179][5:3] == 3'h2)
cfg_partition <= 3'h2;
else
cfg_partition <= 3'h0;
if (sd_cmd !== 1'b0)
cfg_partition <= 3'h0;
end else if (cmd_valid && !cmd_alt && cmd[5:0] == 6'd0)
begin
// GO IDLE
ext_csd[183] <= 8'h0;
end else if (cmd_valid && !cmd_alt && cmd[5:0] == 6'd6
&& card_selected)
begin
// [25:24] = access
// [23:16] = index
// [15: 8] = value
// [ 7: 3] = 5'h0
// [ 2: 0] = Cmd Set
if (cmd_arg[23:16] >= 192)
begin
// Read only registers
end else if (cmd_arg[25:24] != 2'b00)
begin
case(ext_index)
default: ext_csd[ext_index] <= next_ext_byte;
endcase
end
end
// }}}
/*
initial cmd_alt = 1'b0;
initial r_1p8v_request = 1'b0;
initial r_1p8v = 1'b0;
initial R1 = 32'h0;
*/
initial drive_cmd = 1'b0;
initial power_up_busy = 1'b1;
initial ocr = 32'h00ff_8000;
initial RCA = 16'h1;
initial sector_addressing = OPT_HIGH_CAPACITY;
initial card_selected = 1'b1;
initial cfg_width = 2'b0; // 1b width
initial cfg_ddr = 1'b0; // SDR
initial read_en = 1'b0;
initial write_ext_csd = 1'b0;
initial bustest_r = 1'b0;
initial bustest_w = 1'b0;
initial busy_programming = 1'b0;
initial cfg_partition = 2'b0;
initial begin
reply_valid = 1'b0;
reply_type = REPLY_48B;
reply = 6'h0;
reply_data = {(120){1'b0}};
card_state = EMMC_PRE_IDLE;
CID[119:112] = $random; // MFGR ID
CID[111:106] = $random; // BIN: Bank index number
CID[105:104] = $random; // Device / BGA
CID[103: 96] = "GT"; // OED/ Application ID
CID[ 95: 48] = "SM-MDL"; // Product name
CID[ 47: 40] = $random; // Product revision
CID[ 39: 8] = $random; // Serial number
CID[ 7: 0] = $random; // Manufacturing date
// CRC not included in our version
block_len = 16'd512;
clear_errors = 1'b0;
end
always @(*)
begin
if(power_up_busy)
CSD[119] = 1'b0;
CSD = 120'h0;
end
always @(*)
ocr[31] = !power_up_busy;
always @(*)
ocr[30] = sector_addressing && OPT_HIGH_CAPACITY;
// CRC error handling
// {{{
always @(posedge sd_clk or negedge rst_n)
if (!rst_n)
err_com_crc_error <= 1'b0;
else if (clear_errors)
begin
if (reply_valid && !reply_busy)
err_com_crc_error <= 1'b0;
end else if (cmd_crc_err || rx_err)
err_com_crc_error <= 1'b1;
// }}}
// Other error handling
// {{{
always @(*)
begin
R1 = 32'h0;
R1[31] = err_addr_out_of_range;
R1[30] = err_address_misalign;
// R1[29] = err_block_len_error;
R1[28] = err_erase_seq_error;
R1[27] = err_erase_param;
R1[26] = err_wp_violation;
R1[25] = err_device_is_locked;
R1[24] = err_lock_unlock_failed;
R1[23] = err_com_crc_error;
R1[22] = err_illegal_command;
R1[21] = err_device_ecc_failed;
R1[20] = err_cc_error;
R1[19] = err_generic_error;
R1[16] = err_cidcsd_overwrite;
R1[15] = err_wp_erase_skip;
R1[13] = err_erase_reset;
R1[12:9] = card_state;
// R1[8] = ready_for_data;
R1[7] = err_switch_error;
// R1[6] = exception_event;
R1[5] = cmd_alt;
end
always @(posedge sd_clk or negedge rst_n)
if (!rst_n)
begin
err_addr_out_of_range <= 1'b0;
err_address_misalign <= 1'b0;
// err_block_len_error <= 1'b0;
err_erase_seq_error <= 1'b0;
err_erase_param <= 1'b0;
err_wp_violation <= 1'b0;
err_device_is_locked <= 1'b0;
err_lock_unlock_failed <= 1'b0;
// err_com_crc_error <= 1'b0;
err_illegal_command <= 1'b0;
err_device_ecc_failed <= 1'b0;
err_cc_error <= 1'b0;
err_generic_error <= 1'b0;
err_cidcsd_overwrite <= 1'b0;
err_wp_erase_skip <= 1'b0;
err_erase_reset <= 1'b0;
err_switch_error <= 1'b0;
end else if (clear_errors)
begin
err_addr_out_of_range <= 1'b0;
err_address_misalign <= 1'b0;
// err_block_len_error <= 1'b0;
err_erase_seq_error <= 1'b0;
err_erase_param <= 1'b0;
err_wp_violation <= 1'b0;
err_device_is_locked <= 1'b0;
err_lock_unlock_failed <= 1'b0;
// err_com_crc_error <= 1'b0;
err_illegal_command <= 1'b0;
err_device_ecc_failed <= 1'b0;
err_cc_error <= 1'b0;
err_generic_error <= 1'b0;
err_cidcsd_overwrite <= 1'b0;
err_wp_erase_skip <= 1'b0;
err_erase_reset <= 1'b0;
err_switch_error <= 1'b0;
end
// }}}
always @(posedge sd_clk or negedge rst_n)
if (!rst_n)
begin
// {{{
card_state <= EMMC_PRE_IDLE;
cmd_alt <= 1'b0;
reply_crc <= 1'b1;
reply_type <= REPLY_48B;
card_reset <= 0;
// r_1p8v_request <= 1'b0;
reply_valid <= 1'b0;
pending_read <= 1'b0;
pending_write <= 1'b0;
write_ext_csd <= 1'b0;
clear_errors <= 1'b0;
bustest_w <= 1'b0;
bustest_r <= 1'b0;
boot_mode <= 1'b1;
boot_active <= 1'b0;
cfg_ppull <= 1'b0;
// }}}
end else if (card_state == EMMC_INACTIVE)
begin
clear_errors <= 1'b0;
bustest_w <= 1'b0;
bustest_r <= 1'b0;
cfg_ppull <= 1'b0;
end else if (cmd_valid && cmd[5:0] == 6'h0
&& cmd_arg != 32'hfffffffa && cmd_arg != 32'hf0f0f0f0)
begin // CMD0: GO_IDLE_STATE, overrides all other internal states
// {{{
if (cmd_arg == 32'hf0f0f0f0)
card_state <= EMMC_PRE_IDLE;
else if (cmd_arg == 32'hffff_fffa)
begin
card_state <= EMMC_PRE_IDLE;
boot_mode <= 1;
end
reply_valid <= 1'b0;
// ocr[31] <= power_up_busy;
ocr[7] <= 1'b0;
card_reset <= 1;
pending_read <= 1'b0;
read_en <= 1'b0;
pending_write <= 1'b0;
write_en <= 1'b0;
RCA <= 16'h0;
drive_cmd <= 1'b0;
card_selected <= 1'b1;
cfg_width <= 2'b0;
write_ext_csd <= 1'b0;
bustest_w <= 1'b0;
bustest_r <= 1'b0;
cfg_ppull <= 1'b0;
// }}}
end else if (card_state == EMMC_PRE_IDLE)
begin
if (sd_cmd !== 1'b0)
card_state <= EMMC_IDLE;
else if (boot_clk_count >= 73 && boot_mode)
card_state <= EMMC_BOOT;
clear_errors <= 1'b0;
bustest_w <= 1'b0;
bustest_r <= 1'b0;
/*
end else if (card_state == EMMC_PRE_BOOT
&& (!cmd_valid || cmd_crc_err || cmd[5:0] != 6'd1))
begin
clear_errors <= 1'b0;
bustest_w <= 1'b0;
bustest_r <= 1'b0;
*/
end else if (card_state == EMMC_BOOT)
begin
clear_errors <= 1'b0;
bustest_w <= 1'b0;
bustest_r <= 1'b0;
write_ext_csd <= 1'b0;
if (sd_cmd !== 1'b0 || (tx_valid && tx_last && read_posn >= BOOTSZ))
begin
pending_write <= 1'b0;
multi_block <= 1'b0;
card_state <= EMMC_IDLE;
end else if (!boot_active)
begin
read_posn <= cmd_arg;
pending_write <= 1'b1;
multi_block <= 1'b1;
boot_active <= 1'b1;
end
end else if (cmd_valid && !cmd_crc_err && (card_state != EMMC_IDLE
|| cmd[5:0] == 6'd1))
begin
cmd_alt <= 1'b0;
reply <= cmd[5:0];
reply_crc <= 1'b1; // All replies get CRCs by default
reply_type <= REPLY_48B;
card_reset <= 0;
write_ext_csd <= 1'b0;
clear_errors <= 1'b0;
bustest_w <= 1'b0;
bustest_r <= 1'b0;
// r_1p8v_request <= 1'b0;
casez({ cmd_alt, cmd[5:0] })
// ACMDs
{ 1'b1, 6'd41 }: begin // (An SDIO command ...)
// {{{
assert(0);
end
// }}}
// Regular commands
{ 1'b?, 6'd0 }: begin //! CMD0: Go idle
// {{{
if (cmd_arg == 32'hf0f0f0f0)
card_state <= EMMC_PRE_IDLE;
else if (cmd_arg == 32'hffff_fffa)
begin
card_state <= EMMC_PRE_IDLE;
boot_mode <= 1;
end
reply_valid <= 1'b0;
// ocr[31] <= power_up_busy;
ocr[7] <= 1'b0;
card_reset <= 1;
pending_read <= 1'b0;
read_en <= 1'b0;
pending_write <= 1'b0;
write_en <= 1'b0;
RCA <= 16'h0;
drive_cmd <= 1'b0;
card_selected <= 1'b1;
cfg_width <= 2'b0;
cfg_ppull <= 1'b0;
end
// }}}
{ 1'b?, 6'd1 }: begin //! CMD1: SEND_OP_COND
// {{{
// Possibly card_state <= EMMC_INACTIVE
if (card_state == EMMC_IDLE || card_state == EMMC_READY)
begin
card_state <= EMMC_READY;
sector_addressing <= (cmd_arg[30:29] == 2'b10) && OPT_HIGH_CAPACITY;
reply_valid <= #7 1'b1;
reply <= 6'b111111;
// r_1p8v_request <= (OPT_DUAL_VOLTAGE && cmd_arg[24] && !power_up_busy);
reply_data <= { {(120-32){1'b0}}, ocr[31],
((cmd_arg[30] && sector_addressing)
&& OPT_HIGH_CAPACITY),
30'h0ff8080};
reply_crc <= 1'b0;
if (0 == (ocr[23:8] & cmd_arg[23:8]))
begin
reply_data[31] <= 1'b0;
power_up_busy <= 1;
end else begin
// 5 command/reply cycles at 100kHz
// power_up_busy <= #(5*2*48*10000)1'b0;
// or ... 5 command/reply cycles at 1MHz
power_up_busy <= #(5*2*48*1000) 1'b0;
end
end end
// }}}
{ 1'b?, 6'd2 }: begin //! CMD2: ALL_SEND_CID
// {{{
if (card_selected && !power_up_busy)
begin
reply_valid <= #7 1'b1;
reply_type <= REPLY_136B;
reply <= 6'd2;
reply_data <= CID;
drive_cmd <= 1'b0;
end end
// }}}
{ 1'b?, 6'd3 }: begin //! CMD3: SET_RELATIVE_ADDR
// {{{
card_state <= EMMC_STANDBY;
if (!cmd_collision)
begin
RCA <= cmd_arg[31:16];
reply_valid <= #7 1'b1;
reply <= 6'd3;
reply_data <= { {(120-32){1'b0}}, R1};
clear_errors <= 1'b1;
card_selected <= 1'b0;
end end
// }}}
{ 1'b?, 6'd4 }: begin // CMD4: SET_DSR
// {{{
card_state <= EMMC_SLEEP;
end
// }}}
{ 1'b?, 6'd5 }: begin // CMD5: SLEEP_AWAKE
// {{{
if (cmd_arg[31:16] == RCA)
begin
if (card_state == EMMC_SLEEP)
begin
card_state <= EMMC_STANDBY;
cfg_ppull <= 1'b1;
end else if (card_state == EMMC_STANDBY)
begin
card_state <= EMMC_SLEEP;
cfg_ppull <= 1'b0;
end
end end
// }}}
{ 1'b?, 6'd6 }: begin //!! CMD6: SWITCH_FUNCTION (+/- DDR, etc)
// {{{
if (card_selected)
begin
reply_valid <= #7 1'b1;
reply_data <= { {(120-32){1'b0}}, R1};
clear_errors <= 1'b1;
if (cmd_arg[23:16] >= 192 || cmd_arg[25:24] == 2'b00)
reply_data[31:0] <= R1 | ERR_SWITCH;
end end
// }}}
{ 1'b?, 6'd7 }: begin //! CMD7: SELECT_DESELECT_CARD
// {{{
if (cmd_arg[31:16] == RCA)
begin
card_selected <= 1'b1;
reply_valid <= #7 1'b1;
reply <= 6'd2;
reply_data <= { {(120-32){1'b0}}, R1};
clear_errors <= 1'b1;
cfg_ppull <= 1'b1;
if (busy_programming)
card_state <= EMMC_PROGRAMMING;
else
card_state <= EMMC_TRANSFER;
end else begin
cfg_ppull <= 1'b0;
if (busy_programming)
card_state <= EMMC_DISCONNECT;
else
card_state <= EMMC_STANDBY;
card_selected <= 1'b0;
end end
// }}}
{ 1'b?, 6'd8 }: begin // CMD8: SEND_EXT_CSD
// {{{
if (card_selected)
begin
card_state <= EMMC_SEND_DATA;
reply_valid <= #7 1'b1;
reply_data <= { {(120-32){1'b0}}, R1 };
clear_errors <= 1'b1;
pending_write <= 1'b1;
multi_block <= 1'b0;
write_ext_csd <= 1'b1;
end end
// }}}
{ 1'b?, 6'd9 }: begin //! CMD9: SEND_CSD
// {{{
if (cmd_arg[31:16] == RCA && card_state == EMMC_STANDBY)
begin
card_state <= EMMC_STANDBY;
// card_state <= EMMC_SEND_DATA;
reply_valid <= #7 1'b1;
reply <= 6'h3f;
reply_type <= REPLY_136B;
reply_data <= CSD;
cfg_ppull <= 1'b0;
end end
// }}}
{ 1'b?, 6'd10 }: begin // CMD10: SEND_CID
// {{{
if (cmd_arg[31:16] == RCA && card_state == EMMC_STANDBY)
begin
card_state <= EMMC_STANDBY;
reply_valid <= #7 1'b1;
reply <= 6'd10;
reply_type <= REPLY_136B;
reply_data <= CID;
cfg_ppull <= 1'b0;
end end
// }}}
{ 1'b?, 6'd13 }: begin // CMD13: SEND_STATUS
// {{{
if (cmd_arg[31:16] == RCA)
begin
card_state <= EMMC_STANDBY;
reply_valid <= #7 1'b1;
// reply <= 6'd13;
SQS = cmd_arg[15];
// HPI = cmd_arg[ 0]
cfg_ppull <= 1'b0;
if (!SQS)
begin
reply_data <= { {(120-32){1'b0}}, R1};
clear_errors <= 1'b1;
end else
// Query the QSR (Queue Status Register)
// which we haven't yet implemented.
reply_data <= { {(120-32){1'b0}},QSR};
end end
// }}}
{ 1'b?, 6'd12 }: begin //! CMD12: STOP_TRANSMISSION
// {{{
if (cmd_arg[31:16] == RCA)
begin
multi_block <= 1'b0;
pending_read <= 0;
pending_write <= 0;
reply_valid <= #7 1'b1;
reply_data <= { {(120-32){1'b0}}, R1 };
if (card_state == EMMC_SEND_DATA)
card_state <= EMMC_TRANSFER;
else if (card_state == EMMC_RECEIVE_DATA)
card_state <= EMMC_PROGRAMMING;
end end
// }}}
{ 1'b?, 6'd14 }: begin //!! CMD14: BUSTEST_R
// {{{
if (card_selected && card_state == EMMC_BUS_TEST)
begin
card_state <= EMMC_TRANSFER;
read_en <= 0;
// rx_addr <= 0;
reply_valid <= #7 1'b1;
reply_data <= { {(120-32){1'b0}}, R1 };
pending_write <= (rx_addr != 0);
multi_block <= 1'b0;
bustest_r <= 1'b1;
end end
// }}}
{ 1'b?, 6'd15 }: begin // CMD15: GO_INACTIVE_STATE
// {{{
if (cmd_arg[31:16] == RCA)
begin
card_state <= EMMC_INACTIVE;
cfg_ppull <= 1'b0;
end end
// }}}
{ 1'b?, 6'd16 }: begin // CMD16: SET_BLOCKLEN
// {{{
if (card_selected)
begin
// assert(block_len[31:16] == 0);
// assert(block_len <= MAX_BLKLEN);
//
reply_valid <= #7 1'b1;
reply <= 6'd16;
if (block_len > MAX_BLKLEN)
begin
reply_data <= { {(120-32){1'b0}},
R1 | ERR_BLOCKLEN};
end else begin
block_len <= cmd_arg[15:0];
reply_data <= { {(120-32){1'b0}}, R1 };
end
clear_errors <= 1'b1;
end end
// }}}
{ 1'b?, 6'd17 }: begin //! CMD17: READ_SINGLE_BLOCK
// {{{
$display("CMD17 request");
if (card_selected && card_state == EMMC_TRANSFER)
begin
$display("CMD17 request, card selected, in proper state");
card_state <= EMMC_SEND_DATA;
//
pending_write <= 1'b1;
multi_block <= 1'b0;
// write_en <= 1'b1;
reply_valid <= #7 1'b1;
reply_data <= { {(120-32){1'b0}}, R1};
clear_errors <= 1'b1;
read_posn <= cmd_arg;
if (busy_programming)
begin
$display("READ-CMD-ERR: Already busy");
reply_data<= { {(120-32){1'b0}},
R1 | ERR_ILLEGALCMD };
pending_write <= 1'b0;
end else if (sector_addressing)
begin
read_posn <= cmd_arg << 9;
if (cmd_arg << 9 >= (40'd1 << LGMEMSZ)
- block_len)
begin
reply_data<= { {(120-32){1'b0}},
R1 | ERR_ADDRRANGE };
pending_write <= 1'b0;
$display("READ-CMD-ERR: Sector out of bounds");
end
end else if (cmd_arg >= (40'd1 << LGMEMSZ)
- block_len)
begin
reply_data<= { {(120-32){1'b0}},
R1 | ERR_ADDRRANGE };
pending_write <= 1'b0;
$display("READ-CMD-ERR: Small Sector out of bounds");
end
rx_addr <= 0;
end else if (card_selected)
begin
reply_valid <= #7 1'b1;
reply_data <= { {(120-32){1'b0}}, R1};
reply_data[31:0] <= R1 | ERR_ILLEGALCMD;
clear_errors <= 1'b1;
end end
// }}}
{ 1'b?, 6'd18 }: begin //! CMD18: READ_MULTIPLE_BLOCK
// {{{
if (card_selected && card_state == EMMC_TRANSFER)
begin
card_state <= EMMC_SEND_DATA;
//
pending_write <= 1'b1;
multi_block <= 1'b1;
// write_en <= 1'b1;
reply_valid <= #7 1'b1;