-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathsdwb.v
3601 lines (3260 loc) · 92.4 KB
/
sdwb.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: rtl/sdwb.v
// {{{
// Project: SD-Card controller
//
// Purpose: Bus handler. Accepts and responds to Wishbone bus requests.
// Configures clock division, and IO speed and parameters.
// Issues commands to the command handler, TX and RX handlers.
//
//
// Basic command types:
// 0x00000040 Broadcast command, no response expected
// 0x00000140 Standard command, R1 expected response
// 0x00000240 Command expecting an R2 return
// 0x00000940 Read request, read data to follow
// 0x04000d40 Write request, data to follow
// 0x00000800 Continues a data read into a second sector
// 0x00000c00 Continues a data write into a second sector
// 0x00000168 (CMD40) GO_IRQ_STATE eMMC command
// (open drain response)
// How to break an interrupt?
// 0x00000028 (Also requires open-drain mode)
// 0x00000040 (GO_IDLE, expects no response)
// How to reset an error without doing anything?
// 0x00008080
// How to reset the FIFO pointer without doing anything?
// 0x00000080
// How to keep the command controller from timing out while
// waiting for an interrupt? Send a GO_IRQ_STATE command
// The command processor will need to know how to handle
// this internally.
// 0x00000168
//
// 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
`default_nettype none
// }}}
module sdwb #(
// {{{
parameter LGFIFO = 15, // Log_2(FIFO size in bytes)
parameter NUMIO=4,
localparam MW = 32,
parameter [0:0] OPT_LITTLE_ENDIAN = 1'b0,
parameter [0:0] OPT_SERDES = 1'b0,
parameter [0:0] OPT_DS = OPT_SERDES,
parameter [0:0] OPT_DDR = 1'b0,
parameter [0:0] OPT_CARD_DETECT = 1'b1,
parameter [0:0] OPT_EMMC = 1'b1,
parameter [0:0] OPT_CRCTOKEN = 1'b1,
localparam LGFIFOW=LGFIFO-$clog2(MW/8),
parameter [0:0] OPT_DMA = 1'b0,
parameter DMA_AW = 30,
parameter [0:0] OPT_STREAM = 1'b0,
// Set OPT_HWRESET if a reset pin exists for this H/W
parameter [0:0] OPT_HWRESET = OPT_EMMC, // eMMC has resets
parameter [0:0] OPT_1P8V= 1'b0, // 1.8V voltage switch capable?
// OPT_R1B, if set, adds logic to the controller to only expect
// a card busy signal following an R1B command. This insures
// the card busy status is set until the card has an opportunity
// to set it. Any potential card busy returns from the PHY,
// for other reasons (data?) will be will be ignored otherwise
// except following an R1B command. If not set, then anytime
// DAT[0] goes low following a command (and neither actively
// transmitting or receiving), then the card busy will be set
// until it returns high again. This will catch all reasons
// the card may be busy, and actually provide a direct wire
// for reading the card busy bit from the card itself via the
// PHY.
parameter [0:0] OPT_R1B = 1'b1,
// If OPT_R1B is set, then we'll want to wait at least two
// device clocks waiting for busy. Each clock can be as long
// as 1k system clock cycles. Hence, we'll wait for up to 4095
// clock cycles before timing out while waiting for busy.
// Perhaps that's too long, but it's just a backup timeout.
// If the device actually indicates a busy (like it's supposed
// to), then we'll only be busy until the device releases.
parameter LGCARDBUSY = 12,
parameter [0:0] OPT_LOWPOWER = 1'b0
// }}}
) (
// {{{
input wire i_clk, i_reset,
// Wishbone interface
// {{{
input wire i_wb_cyc, i_wb_stb, i_wb_we,
input wire [2:0] i_wb_addr,
input wire [32-1:0] i_wb_data,
input wire [32/8-1:0] i_wb_sel,
output wire o_wb_stall,
output reg o_wb_ack,
output wire [32-1:0] o_wb_data,
// }}}
// Configuration options
// {{{
output reg o_cfg_clk90,
output wire [7:0] o_cfg_ckspeed,
output reg o_cfg_shutdown,
output wire [1:0] o_cfg_width,
output wire o_cfg_ds, o_cfg_dscmd,
output reg o_cfg_ddr,
output reg o_pp_cmd, o_pp_data,
output reg [4:0] o_cfg_sample_shift,
output reg o_cfg_expect_ack,
input wire [7:0] i_ckspd,
output reg o_soft_reset,
// }}}
// External DMA interface
// {{{
output wire o_dma_sd2s,
output wire o_sd2s_valid,
input wire i_sd2s_ready,
output wire [31:0] o_sd2s_data,
output wire o_sd2s_last,
//
output wire o_dma_s2sd,
input wire i_s2sd_valid,
output wire o_s2sd_ready,
input wire [31:0] i_s2sd_data,
output wire [DMA_AW-1:0] o_dma_addr,
// o_dma_len: DMA transfer length (in bytes) = 1<<lgblk
output wire [LGFIFO:0] o_dma_len,
input wire i_dma_busy,
input wire i_dma_err,
output wire o_dma_abort,
// }}}
// CMD interface
// {{{
output wire o_cmd_selfreply,
output reg o_cmd_request,
output reg [1:0] o_cmd_type,
output wire [6:0] o_cmd_id,
output wire [31:0] o_arg,
//
input wire i_cmd_busy, i_cmd_done,
i_cmd_err,
input wire [1:0] i_cmd_ercode,
//
input wire i_cmd_response,
input wire [5:0] i_resp,
input wire [31:0] i_arg,
//
input wire i_cmd_mem_valid,
input wire [MW/8-1:0] i_cmd_mem_strb,
input wire [LGFIFOW-1:0] i_cmd_mem_addr,
input wire [MW-1:0] i_cmd_mem_data,
// }}}
// TX interface
// {{{
output reg o_tx_en,
//
output reg o_tx_mem_valid,
input wire i_tx_mem_ready,
output reg [31:0] o_tx_mem_data,
output reg o_tx_mem_last,
//
input wire i_tx_done, i_tx_err,i_tx_ercode,
// }}}
// RX interface
// {{{
output reg o_rx_en,
output wire o_crc_en,
output wire [LGFIFO:0] o_length,
//
input wire i_rx_mem_valid,
input wire [MW/8-1:0] i_rx_mem_strb,
input wire [LGFIFOW-1:0] i_rx_mem_addr,
input wire [MW-1:0] i_rx_mem_data,
//
input wire i_rx_done, i_rx_err,i_rx_ercode,
// }}}
input wire i_card_detect,
input wire i_card_busy,
output wire o_hwreset_n,
output wire o_1p8v,
input wire i_1p8v,
output reg o_int
// }}}
);
// Local declarations
// {{{
localparam LGFIFO32 = LGFIFO - $clog2(32/8);
localparam [2:0] ADDR_CMD = 0,
ADDR_ARG = 1,
ADDR_FIFOA = 2,
ADDR_FIFOB = 3,
ADDR_PHY = 4;
localparam [1:0] CMD_PREFIX = 2'b01,
NUL_PREFIX = 2'b00;
localparam [1:0] RNO_REPLY = 2'b00,
R2_REPLY = 2'b10,
R1B_REPLY = 2'b11;
// Command register bits
localparam EXPECT_ACK_BIT = 26,
HWRESET_BIT = 25,
CARD_REMOVED_BIT = 18,
ERR_BIT = 15,
USE_DMA_BIT = 13,
FIFO_ID_BIT = 12,
USE_FIFO_BIT = 11,
FIFO_WRITE_BIT = 10; // Write to SD card
// PHY register bits
localparam VOLTAGE_BIT = 22,
DSCMD_BIT = 21,
CLK_SHUTDOWN_BIT = 15,
CLK90_BIT = 14,
PP_CMD_BIT = 13,
PP_DATA_BIT = 12,
DS_BIT = 9,
DDR_BIT = 8;
localparam [1:0] WIDTH_1W = 2'b00,
WIDTH_4W = 2'b01,
WIDTH_8W = 2'b10;
// localparam [15:0] CMD_SELFREPLY = 16'h0028;
reg cmd_busy, new_cmd_request, new_data_request, new_tx_request,
new_r2_request, new_dma_request;
reg w_selfreply_request, r_clk_shutdown;
reg clear_err;
// reg bus_wrvalid, bus_rdvalid;
reg [31:0] bus_rddata;
wire bus_write, bus_read;
wire [31:0] bus_wdata;
wire [3:0] bus_wstrb;
wire [2:0] bus_wraddr, bus_rdaddr;
wire bus_cmd_stb, bus_phy_stb;
reg [6:0] r_cmd;
reg r_tx_request, r_rx_request, r_tx_sent, r_ecode,
r_fifo, r_cmd_err, r_transfer_err;
reg [1:0] r_cmd_ecode;
reg [31:0] r_arg;
reg [3:0] lgblk;
reg [1:0] r_width;
reg [7:0] r_ckspeed;
reg [31:0] w_cmd_word, w_phy_ctrl;
reg [15:0] blk_words;
integer ika, ikb;
localparam NFIFOW = (1<<LGFIFO) / (MW/8);
reg [MW-1:0] fifo_a [0:NFIFOW-1];
reg [MW-1:0] fifo_b [0:NFIFOW-1];
reg [MW-1:0] tx_fifo_a, tx_fifo_b;
wire [(($clog2(MW/32) > 0) ? ($clog2(MW/32)-1):0):0] tx_shift;
reg [LGFIFOW-1:0] fif_wraddr, fif_rdaddr;
reg [LGFIFOW-1:0] fif_a_rdaddr, fif_b_rdaddr;
reg [LGFIFO32-1:0] tx_mem_addr;
reg [MW-1:0] next_tx_mem;
reg tx_fifo_last, pre_tx_last,
tx_pipe_valid;
wire card_present, card_removed;
reg pre_valid;
reg [1:0] pre_sel;
reg [31:0] pre_data;
reg [LGFIFOW-1:0] mem_wr_addr_a, mem_wr_addr_b;
reg [MW/8-1:0] mem_wr_strb_a, mem_wr_strb_b;
reg [MW-1:0] mem_wr_data_a, mem_wr_data_b;
reg r_mem_busy, card_was_busy;
wire w_card_busy;
// DMA signals
wire dma_busy, dma_fifo, dma_write, dma_read_fifo,
dma_error, dma_last, dma_zero_len, dma_int, dma_stopped,
dma_read_active, dma_tx;
wire [31:0] dma_command;
wire [31:0] dma_len_return;
reg [63:0] dma_addr_return;
// }}}
////////////////////////////////////////////////////////////////////////
//
// Registers
// {{{
// CMD/control register
// {{{
assign bus_write =(i_wb_stb && !o_wb_stall && i_wb_we) || dma_write;
assign bus_wraddr = (dma_write) ? 0 : i_wb_addr;
assign bus_wdata = (dma_write) ? dma_command : i_wb_data;
assign bus_wstrb = (dma_write) ? 4'hf : i_wb_sel;
assign bus_read = i_wb_stb && !o_wb_stall && !i_wb_we;
assign bus_rdaddr = i_wb_addr;
assign bus_cmd_stb = bus_write && bus_wraddr == ADDR_CMD
&& (dma_busy == dma_write)
&&((!dma_error && !r_cmd_err && !r_transfer_err)
|| (bus_wstrb[ERR_BIT/8] && bus_wdata[ERR_BIT]));
// o_soft_reset
// {{{
initial o_soft_reset = 1'b1;
always @(posedge i_clk)
if (i_reset || (OPT_CARD_DETECT && (!card_present || card_removed))
|| (OPT_HWRESET && !o_hwreset_n))
begin
o_soft_reset <= 1'b1;
end else if (bus_write && bus_wraddr == ADDR_CMD)
begin
o_soft_reset <= 1'b0;
if (OPT_HWRESET && bus_wstrb[3])
o_soft_reset <= bus_wdata[HWRESET_BIT];
if (&bus_wstrb[3:0] && bus_wdata == 32'h5200_0000)
o_soft_reset <= (bus_wdata == 32'h5200_0000);
end else
o_soft_reset <= 1'b0;
// }}}
// mem_busy
// {{{
always @(posedge i_clk)
if (i_reset || o_soft_reset)
r_mem_busy <= 1'b0;
else if (new_r2_request || (new_data_request
&& (!bus_wdata[USE_DMA_BIT] || !dma_zero_len)
&& (!bus_wdata[FIFO_WRITE_BIT] || new_tx_request))
|| (!i_cmd_err && (r_tx_request || r_rx_request)))
r_mem_busy <= 1'b1;
else begin
if (i_cmd_err && (r_tx_request || r_rx_request))
r_mem_busy <= 1'b0;
if (cmd_busy && i_cmd_done && !o_rx_en && !o_tx_en)
r_mem_busy <= 1'b0;
if (o_tx_mem_valid && i_tx_mem_ready && o_tx_mem_last)
r_mem_busy <= 1'b0;
if (o_rx_en && i_rx_done)
r_mem_busy <= 1'b0;
if (w_selfreply_request && cmd_busy && o_cmd_type == R2_REPLY)
r_mem_busy <= 1'b0;
// if (!mem_busy) r_mem_busy <= 1'b0;
end
`ifdef FORMAL
wire f_mem_busy;
wire [13:0] f_blocksz;
assign f_blocksz = (14'h1 << (lgblk-2));
assign f_mem_busy = (o_tx_en && !r_tx_sent) || r_tx_request || o_rx_en
|| r_rx_request ||(cmd_busy && o_cmd_type == R2_REPLY);
always @(*)
if (!i_reset && !o_soft_reset && !o_hwreset_n)
assert(!r_mem_busy && !f_mem_busy);
always @(*)
if (!i_reset && !o_soft_reset && o_hwreset_n)
assert(r_mem_busy == f_mem_busy);
always @(*)
if (!i_reset && !o_tx_en)
assert(!o_tx_mem_valid);
`endif
// }}}
// o_cmd_request
// {{{
always @(*)
begin
w_selfreply_request = !o_cmd_request && bus_cmd_stb
&& (&bus_wstrb[1:0])
&&(!bus_wdata[USE_DMA_BIT]
&& !bus_wdata[USE_FIFO_BIT])
&& (bus_wdata[9:8] == RNO_REPLY)
&& ((bus_wdata[7:6] == NUL_PREFIX
&& bus_wdata[5:0] != 6'h0) //IRQ Reply
||(bus_wdata[7:6] == CMD_PREFIX
&& bus_wdata[5:0] == 6'h0)); // GO_IDLE
if (OPT_HWRESET && (!o_hwreset_n
|| (bus_wstrb[3] && bus_wdata[HWRESET_BIT])))
w_selfreply_request = 1'b0;
if (i_reset || o_soft_reset || !OPT_EMMC)
w_selfreply_request = 1'b0;
end
always @(*)
begin
// Default values (all == 0)
// {{{
new_cmd_request = 1'b0;
new_data_request = 1'b0;
new_dma_request = 1'b0;
new_r2_request = 1'b0;
// }}}
if (OPT_EMMC && w_selfreply_request)
begin // Self-reply request -- EMMC only
// {{{
new_cmd_request = 1'b1;
new_data_request = 1'b0;
new_dma_request = 1'b0;
new_r2_request = 1'b0;
// }}}
end else if (OPT_HWRESET && bus_wstrb[3] && bus_wdata[HWRESET_BIT])
begin // Hardware reset request -- overrides everything else
// {{{
new_cmd_request = 1'b0;
new_data_request = 1'b0;
new_dma_request = 1'b0;
new_r2_request = 1'b0;
// }}}
end else if (bus_wdata[9:6] == { R2_REPLY, CMD_PREFIX})
begin // R2 request
// {{{
new_cmd_request = 1'b1;
new_data_request = 1'b1;
new_dma_request = 1'b0;
new_r2_request = 1'b1;
if (cmd_busy || r_mem_busy || o_tx_en || dma_busy || i_cmd_err)
begin
new_cmd_request = 1'b0;
new_data_request = 1'b0;
new_r2_request = 1'b0;
// invalid_request = 1'b1
end
// }}}
end else if ((bus_wdata[7:6] == CMD_PREFIX
|| bus_wdata[7:6] == NUL_PREFIX)
&& bus_wdata[USE_DMA_BIT])
begin // DMA request
// {{{
new_cmd_request = (bus_wdata[7:6] == CMD_PREFIX)
&& (!dma_busy || !dma_write);
new_data_request = (!dma_busy || !dma_write);
new_dma_request = (!dma_busy || !dma_write);
new_r2_request = 1'b0;
if (!OPT_DMA || dma_busy || r_mem_busy || o_tx_en
|| dma_zero_len || i_cmd_err
||(cmd_busy && bus_wdata[7:6] == CMD_PREFIX))
begin
new_cmd_request = 1'b0;
new_data_request = 1'b0;
new_dma_request = 1'b0;
// invalid_request = (!OPT_DMA || dma_busy || r_mem_busy);
end else if (bus_wdata[FIFO_WRITE_BIT])
new_data_request = 1'b0;
// }}}
end else if ((bus_wdata[7:6] == CMD_PREFIX || bus_wdata[7:6] == NUL_PREFIX)
&& !bus_wdata[USE_DMA_BIT]
&& bus_wdata[USE_FIFO_BIT])
begin // FIFO request
// {{{
new_cmd_request = (bus_wdata[7:6] == CMD_PREFIX);
new_data_request = 1'b1;
new_dma_request = 1'b0;
if (r_mem_busy// || (OPT_DMA && dma_busy && !dma_write)
|| o_tx_en || i_cmd_err
||(cmd_busy && bus_wdata[7:6] == CMD_PREFIX))
begin
new_cmd_request = 1'b0;
new_data_request = 1'b0;
// invalid_request = 1'b1
end
// }}}
end else if (bus_wdata[7:6] == CMD_PREFIX)
begin // Normal command request
// {{{
new_cmd_request = 1'b1;
new_data_request = 1'b0;
new_dma_request = 1'b0;
new_r2_request = 1'b0;
if (cmd_busy || (OPT_DMA && dma_busy && !dma_write))
begin
new_cmd_request = 1'b0;
end
// }}}
end
if (!bus_cmd_stb || (bus_wstrb[1:0] != 2'b11))
begin // Only act following a bus write
// {{{
new_cmd_request = 1'b0;
new_data_request = 1'b0;
new_dma_request = 1'b0;
new_r2_request = 1'b0;
// }}}
end
if (i_reset || o_soft_reset || (OPT_HWRESET && !o_hwreset_n))
{ new_data_request, new_cmd_request, new_dma_request, new_r2_request } = 4'b0;
end
initial o_cmd_request = 1'b0;
always @(posedge i_clk)
if (i_reset || o_soft_reset)
o_cmd_request <= 1'b0;
else if (new_cmd_request)
begin
o_cmd_request <= 1'b1;
end else if (!i_cmd_busy)
o_cmd_request <= 1'b0;
// }}}
// o_cmd_selfreply -- send a command even if busy waiting on a reply
// {{{
generate if (OPT_EMMC)
begin : GEN_SELFREPLY
reg r_cmd_selfreply;
initial r_cmd_selfreply = 1'b0;
always @(posedge i_clk)
if (i_reset || o_soft_reset)
r_cmd_selfreply <= 1'b0;
else if (w_selfreply_request)
r_cmd_selfreply <= 1'b1;
else if (!i_cmd_busy)
r_cmd_selfreply <= 1'b0;
assign o_cmd_selfreply = r_cmd_selfreply;
`ifdef FORMAL
always @(*)
if (!i_reset && !o_soft_reset && !o_hwreset_n)
assert(!r_cmd_selfreply);
`endif
end else begin : NO_SELFREPLY
assign o_cmd_selfreply = 1'b0;
end endgenerate
// }}}
// cmd_busy: Are we waiting on a command to complete?
// {{{
initial cmd_busy = 1'b0;
always @(posedge i_clk)
if (i_reset || o_soft_reset)
cmd_busy <= 1'b0;
else if (new_cmd_request)
cmd_busy <= 1'b1;
else if (i_cmd_done)
cmd_busy <= 1'b0;
`ifdef FORMAL
always @(*)
if (i_reset && o_cmd_request)
assert(cmd_busy);
always @(posedge i_clk)
if (!i_reset && !$past(i_reset) && !o_soft_reset && !o_hwreset_n)
assert(!cmd_busy);
`endif
// }}}
// o_cmd_id: What command are we issuing?
// {{{
initial r_cmd = 7'b0;
always @(posedge i_clk)
if (i_reset || o_soft_reset)
r_cmd <= 7'b0;
else if (new_cmd_request)
r_cmd <= bus_wdata[6:0];
else if (i_cmd_response)
r_cmd <= { 1'b0, i_resp };
assign o_cmd_id = r_cmd[6:0];
`ifdef FORMAL
always @(*)
if (!i_reset && !o_soft_reset && !o_hwreset_n)
assert(r_cmd == 7'h0);
`endif
// }}}
// o_cmd_type: What response to expect? None, R1, R2, or R1b
// {{{
initial o_cmd_type = 2'b00;
always @(posedge i_clk)
if (i_reset || o_soft_reset)
o_cmd_type <= 2'b00;
else if (new_cmd_request)
o_cmd_type <= bus_wdata[9:8];
`ifdef FORMAL
always @(*)
if (!i_reset && !o_soft_reset && !o_hwreset_n)
assert(o_cmd_type == 2'b00);
`endif
// }}}
// r_expect_busy, r_card_busy
// {{{
generate if (OPT_R1B)
begin : GEN_R1B
// We want to wait at least two device clocks waiting for busy.
// Each clock can be as long as 1k system clock cycles. Hence,
// we'll wait for up to 4095 clock cycles here. Perhaps that's
// too long, but it's just a timeout. If the device actually
// indicates a busy (like it's supposed to), then we'll be
// busy until the device releases.
reg [LGCARDBUSY-1:0] r_busy_counter;
reg r_expect_busy, r_card_busy;
initial r_expect_busy = 1'b0;
always @(posedge i_clk)
if (i_reset || o_soft_reset)
r_expect_busy <= 1'b0;
else if (o_tx_en)
r_expect_busy <= 1'b1;
else if (new_cmd_request)
r_expect_busy <= (bus_wdata[9:8] == R1B_REPLY);
else if (!cmd_busy && (i_card_busy || r_busy_counter == 0))
r_expect_busy <= 1'b0;
initial r_card_busy = 1'b0;
always @(posedge i_clk)
if (i_reset || o_soft_reset)
r_card_busy <= 1'b0;
else if (o_tx_en || (r_card_busy && i_card_busy))
r_card_busy <= 1'b1;
else if (new_cmd_request)
r_card_busy <= (bus_wdata[9:8] == R1B_REPLY);
else if (!i_card_busy && !r_expect_busy && !cmd_busy)
r_card_busy <= 1'b0;
initial r_busy_counter = 0;
always @(posedge i_clk)
if (i_reset || o_soft_reset)
r_busy_counter <= 0;
else if (o_rx_en || i_card_busy
|| (cmd_busy && !r_expect_busy && !o_tx_en))
r_busy_counter <= 0;
else if ((cmd_busy && r_expect_busy) || o_tx_en)
begin
r_busy_counter <= -1;
if (r_ckspeed < 4)
// Max clock rate is 25/3 => 12.5MHz, or 8 cycls
r_busy_counter <= 16; // 2 clock periods
else if (r_ckspeed < 8)
// Max clock rate is 25/5 => 5MHz or 20cycles
r_busy_counter <= 72; // 3.5 clock periods
else if (r_ckspeed < 16)
// Max clock rate is 25/13 => 52 cycles
r_busy_counter <= 192; // 3.6 clock periods
else if (r_ckspeed < 32)
// Max clock rate is 25/29 => 116 cycles
r_busy_counter <= 3*128; // 3.3 clks
end else if (r_busy_counter != 0)
r_busy_counter <= r_busy_counter - 1;
assign w_card_busy = r_card_busy;
`ifdef FORMAL
// We need to stay officially busy as long as we are waiting
// for a response from the card
always @(*)
if (!i_reset && r_expect_busy)
assert(r_card_busy);
// We only check timeouts while we expect a busy signal, and
// before it takes place
always @(*)
if (!i_reset && !r_expect_busy && !cmd_busy)
assert(r_busy_counter == 0);
always @(*)
if (!i_reset && !o_soft_reset && !o_hwreset_n)
begin
assert(r_expect_busy == 1'b0);
assert(r_busy_counter == 0);
assert(r_card_busy == 1'b0);
end
`endif
end else begin : DIRECT_CARD_BUSY
assign w_card_busy = i_card_busy;
// Keep Verilator happy
// {{{
// Verilator coverage_off
// Verilator lint_off UNUSED
wire unused_r1b;
assign unused_r1b = &{ 1'b0, LGCARDBUSY };
// Verilator lint_on UNUSED
// Verilator coverage_on
// }}}
end endgenerate
initial card_was_busy = 1'b0;
always @(posedge i_clk)
if (i_reset || o_soft_reset)
card_was_busy <= 1'b0;
else
card_was_busy <= w_card_busy;
// }}}
// o_tx_en, r_tx_request, r_tx_sent, o_cfg_expect_ack
// {{{
always @(*)
begin
new_tx_request = new_data_request && bus_wdata[FIFO_WRITE_BIT];
if (OPT_DMA && !dma_write && bus_wdata[USE_DMA_BIT])
new_tx_request = 1'b0;
if (bus_wdata[9:8] == R2_REPLY
&& bus_wdata[7:6] == CMD_PREFIX)
new_tx_request = 1'b0;
if (i_reset || o_soft_reset || !o_hwreset_n)
new_tx_request = 1'b0;
end
always @(posedge i_clk)
if (i_reset || o_soft_reset || !OPT_CRCTOKEN)
o_cfg_expect_ack <= 1'b0;
else if (r_rx_request || o_rx_en || (dma_busy && !dma_tx))
begin
o_cfg_expect_ack <= 1'b0;
end else if (dma_busy || r_mem_busy || o_tx_en
|| !bus_write || bus_wraddr != ADDR_CMD)
begin
end else // if (&bus_wstrb[EXPECT_ACK_BIT/8-1:0])
begin
if (bus_wstrb[EXPECT_ACK_BIT/8])
o_cfg_expect_ack <= bus_wdata[EXPECT_ACK_BIT];
if (bus_wstrb[FIFO_WRITE_BIT/8] && !bus_wdata[FIFO_WRITE_BIT])
o_cfg_expect_ack <= 1'b0;
if ((bus_wstrb[USE_FIFO_BIT/8] && !bus_wdata[USE_FIFO_BIT])
&&(!OPT_DMA || !bus_wdata[USE_DMA_BIT]))
o_cfg_expect_ack <= 1'b0;
end
`ifdef FORMAL
always @(posedge i_clk)
if (i_reset || !OPT_CRCTOKEN)
begin
end else if ($past(i_reset || o_soft_reset))
begin
assert(!o_cfg_expect_ack);
end else if (!dma_busy && (o_rx_en || r_rx_request))
begin
assert(!o_cfg_expect_ack);
end else if (o_tx_en || (dma_busy && $past(dma_busy)) || $past(r_tx_request))
begin
assert($stable(o_cfg_expect_ack));
end
`endif
initial r_tx_request = 1'b0;
always @(posedge i_clk)
if (i_reset || o_soft_reset || i_cmd_err)
r_tx_request <= 1'b0;
else if (new_tx_request)
r_tx_request <= 1'b1;
else if (!cmd_busy && !o_cmd_request && !o_tx_en && !w_card_busy)
r_tx_request <= 1'b0;
initial r_tx_sent = 1'b0;
always @(posedge i_clk)
if (i_reset || o_soft_reset || !o_tx_en)
r_tx_sent <= 1'b0;
else if (o_tx_mem_valid && i_tx_mem_ready && o_tx_mem_last)
r_tx_sent <= 1'b1;
initial o_tx_en = 1'b0;
always @(posedge i_clk)
if (i_reset || o_soft_reset || (i_cmd_err && !o_tx_en))
o_tx_en <= 1'b0;
else if (o_tx_en)
begin
if (r_tx_sent && i_tx_done)
o_tx_en <= 1'b0;
end else if (!o_tx_en)
begin
if (!cmd_busy && !o_cmd_request && !w_card_busy && r_tx_request)
o_tx_en <= r_tx_request;
end
`ifdef FORMAL
always @(*)
if (!i_reset && !o_soft_reset && !o_hwreset_n)
assert(!r_tx_request && !r_tx_sent && !o_tx_en);
always @(*)
if (!i_reset && !o_soft_reset)
assert(!r_tx_request || !o_tx_en);
always @(posedge i_clk)
if (!i_reset && $past(!i_reset && !o_soft_reset
&& r_tx_request && !i_cmd_err))
assert(r_tx_request || o_tx_en);
always @(posedge i_clk)
if (!i_reset && $past(!i_reset && !o_soft_reset && new_data_request
&& (!bus_wdata[USE_DMA_BIT] || !dma_zero_len)
&& (!bus_wdata[FIFO_WRITE_BIT] || new_tx_request)))
assert(r_tx_request || r_rx_request
||(o_cmd_request && o_cmd_type == R2_REPLY));
`endif
// }}}
// o_rx_en, r_rx_request
// {{{
initial r_rx_request = 1'b0;
always @(posedge i_clk)
if (i_reset || o_soft_reset || i_cmd_err)
r_rx_request <= 1'b0;
else if (new_data_request && !bus_wdata[FIFO_WRITE_BIT]
&& (!bus_wdata[USE_DMA_BIT] || !dma_zero_len)
&& (bus_wdata[9:8] != R2_REPLY
|| bus_wdata[7:6] == NUL_PREFIX))
r_rx_request <= 1'b1;
else if (!o_cmd_request)
r_rx_request <= 1'b0;
initial o_rx_en = 1'b0;
always @(posedge i_clk)
if (i_reset || o_soft_reset || (i_cmd_err && !o_rx_en))
o_rx_en <= 1'b0;
else if (o_rx_en && i_rx_done)
o_rx_en <= 1'b0;
else if (!o_cmd_request && r_rx_request)
o_rx_en <= 1'b1;
`ifdef FORMAL
always @(*)
if (!i_reset && !o_soft_reset)
assert(!r_rx_request || !o_rx_en);
always @(*)
if (!i_reset && !o_soft_reset && !o_hwreset_n)
assert(!r_rx_request && !o_rx_en);
always @(posedge i_clk)
if (!i_reset && $past(!i_reset && !o_soft_reset && (r_rx_request && !i_cmd_err)))
assert(r_rx_request || o_rx_en);
`endif
// }}}
// r_fifo: Control which FIFO this command uses
// {{{
initial r_fifo = 1'b0;
always @(posedge i_clk)
if (i_reset || o_soft_reset)
r_fifo <= 1'b0;
else if (!r_mem_busy && !o_tx_en
&& bus_cmd_stb && bus_wstrb[FIFO_ID_BIT/8])
r_fifo <= bus_wdata[FIFO_ID_BIT];
// }}}
always @(*)
begin
clear_err = bus_write && bus_wraddr == ADDR_CMD
&& bus_wstrb[ERR_BIT/8] && bus_wdata[ERR_BIT];
if (o_tx_en || r_tx_request || o_rx_en || r_rx_request)
clear_err = 1'b0;
if (dma_busy || cmd_busy)
clear_err = 1'b0;
if (i_reset || o_soft_reset)
clear_err = 1'b1;
end
// r_cmd_err
// {{{
initial r_cmd_err = 1'b0;
always @(posedge i_clk)
if (i_reset || o_soft_reset)
r_cmd_err <= 1'b0;
else if (i_cmd_err) // || (o_rx_en && i_rx_err))
r_cmd_err <= 1'b1;
else if (clear_err && !dma_write)
r_cmd_err <= 1'b0;
initial r_cmd_ecode = 2'b0;
always @(posedge i_clk)
if (i_reset || o_soft_reset)
r_cmd_ecode <= 2'b0;
else if (clear_err)
r_cmd_ecode <= 2'b0;
else if (!r_cmd_err && i_cmd_done)
r_cmd_ecode <= i_cmd_ercode;
`ifdef FORMAL
always @(*)
if (!i_reset && !o_soft_reset && !o_hwreset_n)
begin
assert(r_cmd_err == 1'b0);
assert(r_cmd_ecode == 2'b00);
end
`endif
// }}}
// r_transfer_err
// {{{
initial r_transfer_err = 1'b0;
always @(posedge i_clk)
if (i_reset || o_soft_reset)
r_transfer_err <= 1'b0;
else begin
if (clear_err)
r_transfer_err <= 1'b0;
if (!dma_busy)
begin
if (o_rx_en && i_rx_err)
r_transfer_err <= 1'b1;
if (o_tx_en && i_tx_err)
r_transfer_err <= 1'b1;
end
end
initial r_ecode = 1'b0;
always @(posedge i_clk)
if (i_reset || o_soft_reset)
r_ecode <= 1'b0;
else if (clear_err)
r_ecode <= 1'b0;
else if (!r_transfer_err)
begin
if (i_rx_err && (!dma_busy || !dma_stopped))
r_ecode <= i_rx_ercode;
if (i_tx_err)
r_ecode <= i_tx_ercode;
end
`ifdef FORMAL
always @(posedge i_clk)
if (f_past_valid && $past(o_soft_reset))
assume(!i_rx_done && !i_rx_err);
always @(*)
if (!o_rx_en)
assume(!i_rx_err);
always @(*)
if (!i_reset && !o_soft_reset && !o_hwreset_n)
begin
assert(!r_transfer_err);
assert(!r_ecode);
end
`endif
// }}}
always @(*)
begin
w_cmd_word = 32'h0;
w_cmd_word[26] = o_cfg_expect_ack;
w_cmd_word[25] = !o_hwreset_n;
w_cmd_word[24] = dma_error;
w_cmd_word[23] = r_ecode;
w_cmd_word[22] = r_transfer_err;
w_cmd_word[21] = r_cmd_err;
w_cmd_word[20] = w_card_busy;
w_cmd_word[19] = !card_present;
w_cmd_word[18] = card_removed;
w_cmd_word[17:16] = r_cmd_ecode;
w_cmd_word[15] = r_cmd_err || r_transfer_err || dma_error;
w_cmd_word[14] = cmd_busy;
w_cmd_word[13] = dma_busy;
w_cmd_word[12] = r_fifo;
w_cmd_word[11] = r_mem_busy;
w_cmd_word[10] = (o_tx_en || r_tx_request);
w_cmd_word[9:8] = o_cmd_type;
w_cmd_word[6:0] = r_cmd;
end
// }}}
// Command argument register
// {{{
initial r_arg = 32'b0;
always @(posedge i_clk)
if (i_reset || o_soft_reset)
r_arg <= 0;
// else if (o_cmd_request && !i_cmd_busy)
// r_arg <= 0;
else if (i_cmd_response)
r_arg <= i_arg;
else if (!cmd_busy && bus_write && bus_wraddr == ADDR_ARG)
begin
if (bus_wstrb[0])
r_arg[ 7: 0] <= bus_wdata[ 7: 0];
if (bus_wstrb[1])
r_arg[15: 8] <= bus_wdata[15: 8];