-
Notifications
You must be signed in to change notification settings - Fork 38
/
sdspi.v
1036 lines (947 loc) · 26 KB
/
sdspi.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/sdspi.v
// {{{
// Project: SD-Card controller
//
// Purpose: SD Card controller, using SPI interface with the card and
// WB interface with the rest of the system. This is the top
// level of the SPI based controller.
//
// See the specification for more information.
//
// 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 sdspi #(
// {{{
parameter [0:0] OPT_CARD_DETECT = 1'b1,
parameter [0:0] OPT_LITTLE_ENDIAN = 1'b0,
//
// LGFIFOLN
// {{{
// LGFIFOLN defines the size of the internal memory in words.
// An LGFIFOLN of 7 is appropriate for a 2^(7+2)=512 byte FIFO
parameter LGFIFOLN = 7,
// }}}
parameter POWERUP_IDLE = 1000,
// STARTUP_CLOCKS
// {{{
// Many SD-Cards require a minimum number of SPI clocks to get
// them started. STARTUP_CLOCKS defines this number. Set this
// to zero if you don't want to use this initialization
// sequence.
parameter STARTUP_CLOCKS = 75,
// }}}
// CKDIV_BITS
// {{{
// For my first design, using an 80MHz clock, 7 bits to the
// clock divider was plenty. Now that I'm starting to use
// faster and faster designs, it becomes important to
// parameterize the number of bits in the clock divider. More
// than 8, however, and the interface will need to change.
parameter CKDIV_BITS = 8,
// }}}
// INITIAL_CLKDIV
// {{{
// The SPI frequency is given by the system clock frequency
// divided by a (clock_divider + 1). INITIAL_CLKDIV provides
// an initial value for this clock divider.
parameter [CKDIV_BITS-1:0] INITIAL_CLKDIV = 8'h7c,
// }}}
// OPT_SPI_ARBITRATION
// {{{
// When I originally built this SDSPI controller, it was for an
// environment where the SPI was shared. Doing this requires
// feedback from an arbiter, to know when one SPI device has
// the bus or not. This feedback is provided in i_bus_grant.
// If you don't have an arbiter, just set i_bus_grant to the
// constant 1'b1 and set OPT_SPI_ARBITRATION to 1'b0 to remove
// this extra logic.
parameter [0:0] OPT_SPI_ARBITRATION = 1'b0,
// }}}
//
//
parameter [0:0] OPT_EXTRA_WB_CLOCK = 1'b0,
//
//
//
localparam AW = 2, DW = 32
// }}}
) (
// {{{
input wire i_clk, i_sd_reset,
// Wishbone interface
// {{{
input wire i_wb_cyc, i_wb_stb, i_wb_we,
input wire [AW-1:0] i_wb_addr,
input wire [DW-1:0] i_wb_data,
input wire [DW/8-1:0] i_wb_sel,
output wire o_wb_stall,
output reg o_wb_ack,
output reg [DW-1:0] o_wb_data,
// }}}
// SDCard interface
// {{{
output wire o_cs_n, o_sck, o_mosi,
input wire i_miso, i_card_detect,
// }}}
// Our interrupt
output reg o_int,
// .. and whether or not we can use the SPI port
input wire i_bus_grant,
// And some wires for debugging it all
//
output reg [DW-1:0] o_debug
// }}}
);
// Signal / parameter declarations
// {{{
localparam [1:0] SDSPI_CMD_ADDRESS = 2'b00,
SDSPI_DAT_ADDRESS = 2'b01,
SDSPI_FIFO_A_ADDR = 2'b10,
SDSPI_FIFO_B_ADDR = 2'b11;
localparam BLKBASE = 16;
//
// Command register bit definitions
//
localparam CARD_REMOVED_BIT= 18,
// CRCERR_BIT = 16,
ERR_BIT = 15,
FIFO_ID_BIT = 12,
USE_FIFO_BIT = 11,
FIFO_WRITE_BIT = 10;
//
// Some WB simplifications:
//
reg r_cmd_busy;
reg dbg_trigger;
wire wb_stb, write_stb, wb_cmd_stb, new_data;
wire [AW-1:0] wb_addr;
wire [DW-1:0] wb_data;
wire [3:0] wb_sel;
reg [1:0] pipe_addr;
reg dly_stb;
reg [31:0] fifo_a [0:((1<<LGFIFOLN)-1)];
reg [31:0] fifo_b [0:((1<<LGFIFOLN)-1)];
reg [(LGFIFOLN-1):0] fifo_wb_addr;
reg [(LGFIFOLN-1):0] write_fifo_a_addr, write_fifo_b_addr,
read_fifo_a_addr, read_fifo_b_addr;
wire [LGFIFOLN:0] spi_read_addr, spi_write_addr;
// reg [3:0] write_fifo_a_mask, write_fifo_b_mask;
reg [31:0] write_fifo_a_data, write_fifo_b_data,
fifo_a_word, fifo_b_word, spi_read_data;
wire [31:0] spi_write_data;
reg write_fifo_a, write_fifo_b;
reg [31:0] r_data_reg;
reg r_cmd_err;
reg [7:0] r_last_r_one;
//
//
wire card_removed, card_present;
//
reg [3:0] r_lgblklen;
wire [3:0] max_lgblklen;
reg [25:0] r_watchdog;
reg r_watchdog_err;
reg [DW-1:0] card_status;
wire ll_advance;
reg [CKDIV_BITS-1:0] r_sdspi_clk;
reg ll_cmd_stb;
reg [7:0] ll_cmd_dat;
wire ll_out_stb, ll_idle;
wire [7:0] ll_out_dat;
reg r_fifo_id, r_use_fifo, write_to_card;
wire w_reset;
wire cmd_out_stb;
wire [7:0] cmd_out_byte;
wire cmd_sent, cmd_valid, cmd_busy;
wire [39:0] cmd_response;
reg rx_start;
wire spi_write_to_fifo;
wire rx_valid, rx_busy;
wire [7:0] rx_response;
reg tx_start;
wire spi_read_from_fifo;
wire tx_stb;
wire [7:0] tx_byte;
wire tx_valid, tx_busy;
wire [7:0] tx_response;
reg last_busy;
// }}}
// Take an extra wishbone clock?
// {{{
generate if (!OPT_EXTRA_WB_CLOCK)
begin : EXTRA_WB_PASSTHROUGH
// {{{
assign wb_stb = ((i_wb_stb)&&(!o_wb_stall));
assign write_stb = ((wb_stb)&&( i_wb_we) && i_wb_sel != 0);
// assign read_stb = ((wb_stb)&&(!i_wb_we));
assign wb_sel = i_wb_sel;
assign wb_cmd_stb = (!r_cmd_busy)&& write_stb && (&i_wb_sel)
&&(i_wb_addr==SDSPI_CMD_ADDRESS);
assign wb_addr = i_wb_addr;
assign wb_data = i_wb_data;
assign new_data = (i_wb_stb)&&(!o_wb_stall)
&&(i_wb_we && i_wb_sel != 0)
&&(i_wb_addr == SDSPI_DAT_ADDRESS);
// }}}
end else begin : GEN_EXTRA_WB_CLOCK
// {{{
reg r_wb_stb, r_write_stb, r_wb_cmd_stb, r_new_data;
reg [AW-1:0] r_wb_addr;
reg [DW-1:0] r_wb_data;
reg [DW/8-1:0] r_wb_sel;
initial r_wb_stb = 1'b0;
always @(posedge i_clk)
r_wb_stb <= ((i_wb_stb)&&(!o_wb_stall));
initial r_write_stb = 1'b0;
always @(posedge i_clk)
r_write_stb <= ((i_wb_stb)&&(!o_wb_stall)&&(i_wb_we && i_wb_sel != 0));
initial r_wb_sel = 1'b0;
always @(posedge i_clk)
r_wb_sel <= i_wb_sel;
initial r_wb_cmd_stb = 1'b0;
always @(posedge i_clk)
r_wb_cmd_stb <= (!r_cmd_busy)&&(i_wb_stb)&&(!o_wb_stall)&&(i_wb_we && i_wb_sel != 0)
&&(i_wb_addr == SDSPI_CMD_ADDRESS);
always @(posedge i_clk)
r_new_data <= (i_wb_stb)&&(!o_wb_stall)
&&(i_wb_we && i_wb_sel != 0)
&&(i_wb_addr == SDSPI_DAT_ADDRESS);
always @(posedge i_clk)
r_wb_addr <= i_wb_addr;
always @(posedge i_clk)
r_wb_data <= i_wb_data;
assign wb_stb = r_wb_stb;
assign write_stb= r_write_stb;
assign wb_cmd_stb = r_wb_cmd_stb;
assign new_data = r_new_data;
assign wb_addr = r_wb_addr;
assign wb_data = r_wb_data;
assign wb_sel = r_wb_sel;
// }}}
end endgenerate
// }}}
////////////////////////////////////////////////////////////////////////
//
// Lower-level SDSPI driver
// {{{
////////////////////////////////////////////////////////////////////////
//
// Access to our lower-level SDSPI driver, the one that actually
// uses/sets the SPI ports
//
llsdspi #(
// {{{
.SPDBITS(CKDIV_BITS),
.STARTUP_CLOCKS(STARTUP_CLOCKS),
.POWERUP_IDLE(POWERUP_IDLE),
.OPT_SPI_ARBITRATION(OPT_SPI_ARBITRATION)
// }}}
) lowlevel(
// {{{
i_clk, i_sd_reset, r_sdspi_clk, r_cmd_busy, ll_cmd_stb,
ll_cmd_dat, o_cs_n, o_sck, o_mosi, i_miso,
ll_out_stb, ll_out_dat, ll_idle,
i_bus_grant
// }}}
);
// }}}
////////////////////////////////////////////////////////////////////////
//
// Command controller
// {{{
////////////////////////////////////////////////////////////////////////
//
//
assign w_reset = i_sd_reset || r_watchdog_err;
spicmd
spicmdi(
// {{{
i_clk, w_reset, (wb_cmd_stb && wb_data[7:6] == 2'b01),
wb_data[9:8], wb_data[5:0], r_data_reg, cmd_busy,
cmd_out_stb, cmd_out_byte, !ll_advance,
ll_out_stb, ll_out_dat,
cmd_sent,
cmd_valid, cmd_response
// }}}
);
// }}}
////////////////////////////////////////////////////////////////////////
//
// Receive data (not commands)
// {{{
////////////////////////////////////////////////////////////////////////
//
//
spirxdata #(
.OPT_LITTLE_ENDIAN(OPT_LITTLE_ENDIAN)
) spirxdatai(
// {{{
i_clk, w_reset | r_cmd_err, rx_start,
r_lgblklen, r_fifo_id, rx_busy,
ll_out_stb && !cmd_busy, ll_out_dat,
spi_write_to_fifo, spi_write_addr, spi_write_data,
rx_valid, rx_response
// }}}
);
// }}}
////////////////////////////////////////////////////////////////////////
//
// Transmit/send data (not commands) to the SD card
// {{{
////////////////////////////////////////////////////////////////////////
//
//
spitxdata #(
.RDDELAY(2),
.OPT_LITTLE_ENDIAN(OPT_LITTLE_ENDIAN)
) spitxdatai(
// {{{
i_clk, w_reset | r_cmd_err, tx_start,
r_lgblklen, r_fifo_id, tx_busy,
spi_read_from_fifo, spi_read_addr, spi_read_data,
!ll_advance || cmd_busy, tx_stb, tx_byte,
ll_out_stb && !cmd_busy, ll_out_dat,
tx_valid, tx_response
// }}}
);
// }}}
////////////////////////////////////////////////////////////////////////
//
// Internal FIFO memory
// {{{
////////////////////////////////////////////////////////////////////////
//
//
//
// Let's work with our FIFO memory here ...
//
always @(posedge i_clk)
begin
if ((write_stb)&&(wb_addr == SDSPI_CMD_ADDRESS))
begin // Command write
// Clear the read/write address
fifo_wb_addr <= {(LGFIFOLN){1'b0}};
end else if ((wb_stb)&&(wb_addr[1] && wb_sel != 0))
begin // On read or write, of either FIFO,
// we increase our pointer
// if (wb_sel[0])
fifo_wb_addr <= fifo_wb_addr + 1;
// And let ourselves know we need to update ourselves
// on the next clock
end
end
////////////////////////////////////////////////////////////////////////
//
// Writes to the FIFO
// {{{
////////////////////////////////////////////////////////////////////////
//
//
initial write_fifo_a = 0;
always @(posedge i_clk)
if (r_use_fifo && rx_busy && !spi_write_addr[LGFIFOLN])
begin
write_fifo_a <= spi_write_to_fifo;
write_fifo_a_data <= spi_write_data;
write_fifo_a_addr <= spi_write_addr[LGFIFOLN-1:0];
// write_fifo_a_mask <= 4'hf;
end else begin
write_fifo_a <= write_stb &&(wb_addr == SDSPI_FIFO_A_ADDR) &&(&wb_sel);
write_fifo_a_data <= wb_data;
write_fifo_a_addr <= fifo_wb_addr;
// write_fifo_a_mask <= 4'hf;
end
initial write_fifo_b = 0;
always @(posedge i_clk)
if (r_use_fifo && rx_busy && spi_write_addr[LGFIFOLN])
begin
write_fifo_b <= spi_write_to_fifo;
write_fifo_b_data <= spi_write_data;
write_fifo_b_addr <= spi_write_addr[LGFIFOLN-1:0];
// write_fifo_b_mask <= 4'hf;
end else begin
write_fifo_b <= write_stb &&(wb_addr == SDSPI_FIFO_B_ADDR) && (&wb_sel);
write_fifo_b_data <= wb_data;
write_fifo_b_addr <= fifo_wb_addr;
// write_fifo_b_mask <= 4'hf;
end
always @(posedge i_clk)
if (write_fifo_a)
fifo_a[write_fifo_a_addr] <= write_fifo_a_data;
always @(posedge i_clk)
if (write_fifo_b)
fifo_b[write_fifo_b_addr] <= write_fifo_b_data;
// }}}
////////////////////////////////////////////////////////////////////////
//
// Reads from the FIFO
// {{{
////////////////////////////////////////////////////////////////////////
//
//
always @(*)
if (r_use_fifo && tx_busy && !spi_read_addr[LGFIFOLN])
read_fifo_a_addr = spi_read_addr[LGFIFOLN-1:0];
else
read_fifo_a_addr = fifo_wb_addr;
always @(*)
if (r_use_fifo && tx_busy && spi_read_addr[LGFIFOLN])
read_fifo_b_addr = spi_read_addr[LGFIFOLN-1:0];
else
read_fifo_b_addr = fifo_wb_addr;
always @(posedge i_clk)
fifo_a_word <= fifo_a[read_fifo_a_addr];
always @(posedge i_clk)
fifo_b_word <= fifo_b[read_fifo_b_addr];
always @(posedge i_clk)
if (!spi_read_addr[LGFIFOLN])
spi_read_data <= fifo_a_word;
else
spi_read_data <= fifo_b_word;
initial r_fifo_id = 0;
always @(posedge i_clk)
if (!r_cmd_busy && wb_cmd_stb)
r_fifo_id <= wb_data[FIFO_ID_BIT];
// }}}
// }}}
// r_cmd_busy, tx_start, rx_start, r_use_fifo, write_to_card
// {{{
initial r_cmd_busy = 0;
initial tx_start = 0;
initial rx_start = 0;
always @(posedge i_clk)
if (i_sd_reset)
begin
// {{{
r_cmd_busy <= 0;
r_use_fifo <= 0;
tx_start <= 0;
rx_start <= 0;
// }}}
end else if (!r_cmd_busy)
begin
// {{{
r_cmd_busy <= wb_cmd_stb && (wb_data[7:6] == 2'b01);
tx_start <= 0;
rx_start <= 0;
if (wb_cmd_stb && wb_data[7:6] == 2'b01)
begin
write_to_card <= wb_data[FIFO_WRITE_BIT];
r_use_fifo <= wb_data[USE_FIFO_BIT];
if (wb_data[USE_FIFO_BIT])
begin
tx_start <= (wb_data[FIFO_WRITE_BIT]);
rx_start <= (!wb_data[FIFO_WRITE_BIT]);
end
end
if (r_watchdog_err)
begin
r_use_fifo <= 0;
tx_start <= 0;
rx_start <= 0;
end
// }}}
end else begin
// {{{
if (ll_idle && !ll_cmd_stb && !cmd_busy && !rx_busy && !tx_busy)
begin
r_cmd_busy <= 0;
r_use_fifo <= 0;
end
if (r_cmd_err || tx_busy || rx_busy)
begin
tx_start <= 0;
rx_start <= 0;
end
if (r_watchdog_err)
begin
r_use_fifo <= 0;
tx_start <= 0;
rx_start <= 0;
end
// }}}
end
// }}}
// r_cmd_err
// {{{
initial r_cmd_err = 0;
always @(posedge i_clk)
if (r_watchdog_err)
r_cmd_err <= 1;
else if (r_cmd_busy)
begin
//
// A command error is a watchdog error, so nothing needed here
//
// if (cmd_valid) r_cmd_err <= |cmd_response[38:33];
//
// A transmit error can be discovered as a response to a
// command
//
// if (tx_valid) r_cmd_err <= 0;
//
// However, we can check read response tokens for errors
if (cmd_valid)
r_cmd_err <= r_cmd_err || (cmd_response[38:33] != 0);
if (rx_valid)
r_cmd_err <= r_cmd_err || rx_response[3];
end else if (wb_cmd_stb)
r_cmd_err <= (r_cmd_err)&&(!wb_data[ERR_BIT]);
// }}}
// r_data_reg
// {{{
always @(posedge i_clk)
if (!r_cmd_busy)
begin
if (new_data)
r_data_reg <= wb_data;
else if (wb_cmd_stb && wb_data[7])
r_data_reg <= {
4'h0, max_lgblklen,
1'b0, // Rsrved for: Read data from CMD wire
3'h0, r_lgblklen,
{(16-CKDIV_BITS){1'b0}},
r_sdspi_clk };
end else begin
if (cmd_valid)
begin
r_data_reg <= cmd_response[31:0];
r_last_r_one <= cmd_response[39:32];
end else if (tx_valid)
r_data_reg <= { 24'h0, tx_response[7:0] };
else if (rx_valid)
r_data_reg <= { 24'h0, rx_response[7:0] };
end
// }}}
assign ll_advance = (!ll_cmd_stb || ll_idle);
// ll_cmd_stb, ll_cmd_dat
// {{{
initial ll_cmd_stb = 0;
always @(posedge i_clk)
begin
if (ll_advance)
begin
if (cmd_busy)
begin
ll_cmd_stb <= (ll_cmd_stb || cmd_out_stb);
ll_cmd_dat <= cmd_out_stb ? cmd_out_byte :8'hff;
end else begin
ll_cmd_stb <= (ll_cmd_stb || tx_stb);
ll_cmd_dat <= tx_stb ? tx_byte : 8'hff;
end
end
if (ll_idle && !cmd_busy && !rx_busy && !tx_busy)
ll_cmd_stb <= 1'b0;
if (!r_cmd_busy || i_sd_reset)
ll_cmd_stb <= 1'b0;
end
// }}}
assign max_lgblklen = LGFIFOLN+2;
// r_sdspi_clk, r_lgblklen
// {{{
initial r_sdspi_clk = INITIAL_CLKDIV;
initial r_lgblklen = 9;
always @(posedge i_clk)
begin
// Update our internal configuration parameters, unconnected
// with the card. These include the speed of the interface,
// and the size of the block length to expect as part of a FIFO
// command.
if ((wb_cmd_stb)&&(wb_data[7:6]==2'b11))
// &&(!r_data_reg[7])
// &&(r_data_reg[15:12]==4'h00))
begin
if (r_data_reg[CKDIV_BITS-1:0] != 0)
r_sdspi_clk <= r_data_reg[CKDIV_BITS-1:0];
if ((r_data_reg[BLKBASE +: 4] >= 3)
&&(r_data_reg[BLKBASE +: 4] <= max_lgblklen))
r_lgblklen <= r_data_reg[BLKBASE +: 4];
end
// if (r_lgblklen > max_lgblklen)
// r_lgblklen <= max_lgblklen;
if (!card_present)
r_sdspi_clk <= INITIAL_CLKDIV;
end
// }}}
////////////////////////////////////////////////////////////////////////
//
// Wishbone return logic
// {{{
////////////////////////////////////////////////////////////////////////
//
//
always @(posedge i_clk)
pipe_addr <= wb_addr;
always @(*)
card_status = { 8'h00, // 8b
2'b0, r_watchdog_err, i_sd_reset, // 4b
!card_present, card_removed, 1'b0, 1'b0,
r_cmd_err, r_cmd_busy, 1'b0, r_fifo_id, // 4b
r_use_fifo, write_to_card, 2'b00, // 4b
r_last_r_one }; // 8b
always @(posedge i_clk)
case(pipe_addr)
SDSPI_CMD_ADDRESS:
o_wb_data <= card_status;
SDSPI_DAT_ADDRESS:
o_wb_data <= r_data_reg;
SDSPI_FIFO_A_ADDR:
o_wb_data <= fifo_a_word;
SDSPI_FIFO_B_ADDR:
o_wb_data <= fifo_b_word;
endcase
initial dly_stb = 0;
always @(posedge i_clk)
if (!i_wb_cyc)
dly_stb <= 0;
else
dly_stb <= wb_stb;
initial o_wb_ack = 0;
always @(posedge i_clk)
if (!i_wb_cyc)
o_wb_ack <= 1'b0;
else
o_wb_ack <= dly_stb;
assign o_wb_stall = 1'b0;
// }}}
////////////////////////////////////////////////////////////////////////
//
// Interrupt generation
// {{{
////////////////////////////////////////////////////////////////////////
//
initial last_busy = 0;
always @(posedge i_clk)
last_busy <= r_cmd_busy;
initial o_int = 0;
always @(posedge i_clk)
o_int <= (!r_cmd_busy)&&(last_busy)
||(!card_removed && !card_present);
// }}}
////////////////////////////////////////////////////////////////////////
//
// Card detection logic --- is the card even present?
// {{{
////////////////////////////////////////////////////////////////////////
//
//
// Depends upon the i_card_detect signal. Set this signal to 1'b1 if
// you your device doesn't have it.
//
//
generate if (OPT_CARD_DETECT)
begin : GEN_CARD_DETECT
reg [2:0] raw_card_present;
reg [9:0] card_detect_counter;
reg r_card_removed, r_card_present;
initial r_card_removed = 1'b1;
always @(posedge i_clk)
if (i_sd_reset)
r_card_removed <= 1'b1;
else if (!card_present)
r_card_removed <= 1'b1;
else if (wb_cmd_stb && wb_data[CARD_REMOVED_BIT])
r_card_removed <= 1'b0;
initial raw_card_present = 0;
always @(posedge i_clk)
raw_card_present <= { raw_card_present[1:0], i_card_detect };
initial card_detect_counter = 0;
always @(posedge i_clk)
if (i_sd_reset || !raw_card_present[2])
card_detect_counter <= 0;
else if (!(&card_detect_counter))
card_detect_counter <= card_detect_counter + 1;
initial r_card_present = 1'b0;
always @(posedge i_clk)
if (i_sd_reset || !raw_card_present[2])
r_card_present <= 1'b0;
else if (&card_detect_counter)
r_card_present <= 1'b1;
assign card_present = r_card_present;
assign card_removed = r_card_removed;
end else begin : NO_CARD_DETECT_SIGNAL
assign card_present = 1'b1;
assign card_removed = 1'b0;
end endgenerate
// }}}
////////////////////////////////////////////////////////////////////////
//
// Watchdog protection logic
// {{{
////////////////////////////////////////////////////////////////////////
//
// Some watchdog logic for us. This way, if we are waiting for the
// card to respond, and something goes wrong, we can timeout the
// transaction and ... figure out what to do about it later. At least
// we'll have an error indication.
//
initial r_watchdog_err = 1'b0;
always @(posedge i_clk)
if (!r_cmd_busy)
r_watchdog_err <= 1'b0;
else if (r_watchdog == 0)
r_watchdog_err <= 1'b1;
initial r_watchdog = 26'h3ffffff;
always @(posedge i_clk)
if (!r_cmd_busy)
r_watchdog <= 26'h3fffff;
else if (|r_watchdog)
r_watchdog <= r_watchdog - 26'h1;
// }}}
////////////////////////////////////////////////////////////////////////
//
// Debug signals
// {{{
////////////////////////////////////////////////////////////////////////
//
//
initial dbg_trigger = 0;
always @(posedge i_clk)
dbg_trigger <= (cmd_valid)&&(cmd_response[38:33] != 0);
always @(posedge i_clk)
o_debug <= { dbg_trigger, ll_cmd_stb,
(ll_cmd_stb & ll_idle), ll_out_stb, // 4'h
o_cs_n, o_sck, o_mosi, i_miso, // 4'h
3'b000, i_sd_reset, // 4'h
3'b000, r_cmd_busy, // 4'h
ll_cmd_dat, // 8'b
ll_out_dat }; // 8'b
// }}}
// Make verilator happy
// {{{
// verilator lint_off UNUSED
wire unused;
assign unused = &{ 1'b0, i_wb_cyc, i_wb_sel, cmd_sent,
spi_read_from_fifo };
// verilator lint_on UNUSED
// }}}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
// Formal verification properties
// {{{
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
`ifdef FORMAL
localparam F_LGDEPTH = 3;
wire [F_LGDEPTH-1:0] f_nacks, f_nreqs, f_outstanding;
reg f_past_valid;
initial f_past_valid = 0;
always @(posedge i_clk)
f_past_valid <= 1;
////////////////////////////////////////////////////////////////////////
//
// Wishbone Bus properties
//
////////////////////////////////////////////////////////////////////////
//
//
fwb_slave #(
.AW(2), .DW(32),
.F_LGDEPTH(F_LGDEPTH),
.F_MAX_STALL(1),
.F_MAX_ACK_DELAY(2),
.F_OPT_DISCONTINUOUS(1),
.F_OPT_MINCLOCK_DELAY(1)
) fwb(
.i_clk(i_clk), .i_reset(!f_past_valid),
.i_wb_cyc(i_wb_cyc), .i_wb_stb(i_wb_stb), .i_wb_we(i_wb_we),
.i_wb_addr(i_wb_addr),.i_wb_data(i_wb_data),.i_wb_sel(i_wb_sel),
.i_wb_stall(o_wb_stall), .i_wb_ack(o_wb_ack),
.i_wb_idata(o_wb_data), .i_wb_err(1'b0),
.f_nreqs(f_nreqs), .f_nacks(f_nacks),
.f_outstanding(f_outstanding)
);
always @(*)
if (i_wb_cyc)
assert(f_outstanding == (o_wb_ack ? 1:0) + (dly_stb ? 1:0)
+ (OPT_EXTRA_WB_CLOCK ? wb_stb : 0));
////////////////////////////////////////////////////////////////////////
//
// Contract checks
//
////////////////////////////////////////////////////////////////////////
//
//
always @(*)
assert(!tx_busy || !rx_busy);
always @(*)
assert(!tx_start || !rx_start);
always @(*)
if (!r_use_fifo)
begin
assert(!tx_start && !rx_start);
assert(!tx_busy && !rx_busy);
if(!write_to_card)
assert(!rx_start && !rx_busy);
else
assert(!tx_start && !tx_busy);
end
always @(*)
if (tx_busy || rx_busy || cmd_busy)
assert(r_cmd_busy);
always @(*)
begin
assert(r_lgblklen >= 3);
assert(r_lgblklen <= 9);
end
always @(*)
if (cmd_busy)
begin
assert(spi_read_addr[LGFIFOLN-1:0] <= 1);
assert(spi_write_addr[LGFIFOLN-1:0] <= 1);
end
//
// Command sequence check
(* anyseq *) reg f_cmd_check_value;
reg [1:0] f_cmd_seq;
reg [7:0] f_cmd_byte;
`ifdef VERIFIC
always @(posedge i_clk)
if (f_cmd_check_value && f_cmd_seq == 0 && cmd_out_stb && !spicmdi.i_ll_busy)
f_cmd_byte <= cmd_out_byte;
initial f_cmd_seq = 0;
always @(posedge i_clk)
if (i_sd_reset || r_watchdog_err)
f_cmd_seq <= 0;
else if (f_cmd_check_value && f_cmd_seq == 0 && cmd_out_stb && !spicmdi.i_ll_busy)
f_cmd_seq <= 1;
else if (!ll_cmd_stb || ll_idle)
f_cmd_seq <= f_cmd_seq << 1;
always @(*)
if (!i_sd_reset && !r_watchdog_err) case(f_cmd_seq)
0: begin end
1: begin
assert(ll_cmd_stb);
assert(ll_cmd_dat == f_cmd_byte);
end
2: begin
end
endcase
`endif
////////////////////////////////////////////////////////////////////////
//
// Abstract LLSDSPI properties
//
////////////////////////////////////////////////////////////////////////
//
//
//`ifdef ABSTRACT_LLSDSPI
//`define LLSDSPI_ASSERT assume
//`else
//`define LLSDSPI_ASSERT assert
//`endif
// reg f_first_byte_accepted;
//
// always @(posedge i_clk)
// if (!f_past_valid)
// `LLSDSPI_ASSERT(!ll_out_stb);
// else if (!$past(r_cmd_busy))
// `LLSDSPI_ASSERT(!ll_out_stb);
// else if ($past(ll_out_stb))
// `LLSDSPI_ASSERT(!ll_out_stb);
//
// always @(posedge i_clk)
// if (!r_cmd_busy)
// f_first_byte_accepted <= 1'b0;
// else if (ll_cmd_stb && ll_idle)
// f_first_byte_accepted <= 1'b1;
//
// always @(posedge i_clk)
// if (f_past_valid && $past(f_past_valid))
// begin
// if ($rose(r_cmd_busy))
// begin
// assert($rose(ll_cmd_stb));
// `LLSDSPI_ASSERT(!ll_out_stb);
// end else if (r_cmd_busy && f_first_byte_accepted && $past(f_first_byte_accepted))
// `LLSDSPI_ASSERT(ll_out_stb == $past(ll_idle));
// end
//
// always @(posedge i_clk)
// if (f_past_valid)
// begin
// if ($past(i_sd_reset || r_watchdog_err))
// assert(!r_cmd_busy);
// else if ($past(r_cmd_busy && ll_out_stb && !ll_idle))
// begin
// assert(ll_out_stb);
// assert($stable(ll_out_dat));
// end
// end
////////////////////////////////////////////////////////////////////////
//
// Watchdog checks
//
////////////////////////////////////////////////////////////////////////
//
//
always @(posedge i_clk)
if (f_past_valid && $past(r_watchdog_err))
assert(!cmd_busy && !tx_busy && !rx_busy);