-
Notifications
You must be signed in to change notification settings - Fork 35
/
sdfrontend.v
1592 lines (1414 loc) · 40.2 KB
/
sdfrontend.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/sdfrontend.v
// {{{
// Project: SD-Card controller
//
// Purpose: This is the "front-end" for the SDIO controller. It's designed
// to support all modes up to HS400 if OPT_SERDES is enabled,
// or just the backwards compatibility modes (up to 50MHz) if not.
//
// Status:
// OPT_DDR=0 (Without IDDR and ODDR hardware elements)
// Verified in H/W for 1, 4, and 8b. (Not yet for DDR)
// No support for data strobe.
// OPT_DDR=1 (With IDDR/ODDR hardware elements, but no SERDES)
// Verified in H/W for 1 and 4b. (Not yet for DDR, not yet for 8b)
// No support for data strobe.
// OPT_SERDES=1
// NOT YET VERIFIED IN H/W
//
// Creator: Dan Gisselquist, Ph.D.
// Gisselquist Technology, LLC
//
////////////////////////////////////////////////////////////////////////////////
// }}}
// Copyright (C) 2016-2024, 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 sdfrontend #(
// {{{
parameter [0:0] OPT_SERDES = 1'b0,
parameter [0:0] OPT_DDR = 1'b0,
parameter [0:0] OPT_DS = OPT_SERDES,
parameter [0:0] OPT_COLLISION = 1'b0,
parameter [0:0] OPT_CRCTOKEN = 1'b1,
parameter BUSY_CLOCKS = 4,
parameter HWBIAS = 0,
parameter NUMIO = 8
// }}}
) (
// {{{
input wire i_clk, i_hsclk,
// Configuration
input wire i_reset,
input wire i_cfg_ddr,
input wire i_cfg_ds, i_cfg_dscmd,
input wire [4:0] i_sample_shift,
// Control signals
// Tx path
// {{{
// MSB "first" incoming data.
input wire [7:0] i_sdclk,
// Verilator lint_off SYNCASYNCNET
input wire i_cmd_en, i_cmd_tristate,
// Verilator lint_on SYNCASYNCNET
input wire [1:0] i_cmd_data,
//
input wire i_data_en, i_rx_en, i_data_tristate,
input wire [31:0] i_tx_data,
// }}}
output wire o_data_busy,
// Synchronous Rx path
// {{{
output wire [1:0] o_cmd_strb,
output wire [1:0] o_cmd_data,
output wire o_cmd_collision,
//
output wire o_crcack, o_crcnak,
//
output wire [1:0] o_rx_strb,
output wire [15:0] o_rx_data,
// }}}
// Async Rx path
// {{{
output wire MAC_VALID,
output wire [1:0] MAC_DATA,
output wire MAD_VALID,
output wire [31:0] MAD_DATA,
// output wire MAD_LAST,
// }}}
// I/O ports
// {{{
output wire o_ck,
input wire i_ds,
//
`ifdef VERILATOR
output wire io_cmd_tristate,
output wire o_cmd,
input wire i_cmd,
//
output wire [NUMIO-1:0] io_dat_tristate,
output wire [NUMIO-1:0] o_dat,
input wire [NUMIO-1:0] i_dat,
`else
inout wire io_cmd,
inout wire [NUMIO-1:0] io_dat,
`endif
// }}}
output wire [31:0] o_debug
// }}}
);
// Local declarations
// {{{
genvar gk;
reg dat0_busy, wait_for_busy;
reg [$clog2(BUSY_CLOCKS+1)-1:0] busy_count;
wire raw_cmd;
wire [NUMIO-1:0] raw_iodat;
wire w_cmd_collision;
`ifndef VERILATOR
wire io_cmd_tristate, i_cmd, o_cmd;
wire [NUMIO-1:0] io_dat_tristate, i_dat, o_dat;
`endif
reg last_ck, sync_ack, sync_nak;
wire [7:0] w_pedges, next_pedge, next_nedge, next_dedge;
wire async_ack, async_nak;
reg [4:0] acknak_sreg;
// }}}
// Common setup
// {{{
initial last_ck = 1'b0;
always @(posedge i_clk)
last_ck <= i_sdclk[0];
assign next_pedge = ~{ last_ck, i_sdclk[7:1] } & i_sdclk[7:0];
assign next_nedge = { last_ck, i_sdclk[7:1] } & ~i_sdclk[7:0];
assign next_dedge = next_pedge | (i_cfg_ddr ? next_nedge : 8'h0);
// }}}
generate if (!OPT_SERDES && !OPT_DDR)
begin : GEN_NO_SERDES
// {{{
// This is sort of the "No-PHY" option. Maximum speed, when
// using this option, is the incoming clock speed/2. Without
// SERDES support, there's no support for the DS (data strobe)
// pin either. Think of this as a compatibility mode.
//
// Fastest clock supported = incoming clock speed / 2. That's
// also the fastest clock supported *if you get lucky*. It's
// possible that there won't be enough sub-clock resolution
// to land close enough to the middle of the eye at this
// frequency.
//
reg resp_started;
reg [1:0] io_started;
reg r_cmd_data, r_cmd_strb, r_rx_strb;
reg [7:0] r_rx_data;
wire [HWBIAS+3:0] wide_dedge, wide_pedge, wide_cmdedge;
reg [HWBIAS+3:0] ck_sreg, pck_sreg, ck_psreg;
reg sample_ck, cmd_sample_ck, sample_pck;
assign o_ck = i_sdclk[7];
assign io_cmd_tristate = i_cmd_tristate || o_cmd_collision;
assign o_cmd = i_cmd_data[1];
assign raw_cmd = i_cmd;
assign o_dat = i_tx_data[24 +: NUMIO];
assign io_dat_tristate = {(NUMIO){i_data_tristate}};
assign w_cmd_collision = OPT_COLLISION && io_cmd_tristate
&& i_cmd_en && !i_cmd && |next_pedge;
if (OPT_COLLISION)
begin : GEN_COLLISION
reg r_collision;
always @(posedge i_clk)
if (i_reset || !i_cmd_en)
r_collision <= 1'b0;
else if (w_cmd_collision)
r_collision <= 1'b1;
assign o_cmd_collision = r_collision;
end else begin : NO_COLLISION
assign o_cmd_collision = 1'b0;
// Verilator lint_off UNUSED
wire unused_collision;
assign unused_collision = &{ 1'b0, w_cmd_collision };
// Verilator lint_on UNUSED
end
// sample_ck
// {{{
assign wide_dedge = { ck_sreg[HWBIAS+2:0], |next_dedge };
initial ck_sreg = 0;
always @(posedge i_clk)
if (i_reset || i_data_en)
ck_sreg <= 0;
else
ck_sreg <= wide_dedge[HWBIAS+2:0];
initial sample_ck = 0;
always @(*)
if (i_data_en)
sample_ck = 0;
else
// Verilator lint_off WIDTH
sample_ck = wide_dedge[HWBIAS +: 4] >> i_sample_shift[4:3];
// Verilator lint_on WIDTH
// }}}
// sample_pck
// {{{
assign wide_pedge = { ck_psreg[HWBIAS+2:0], |next_pedge };
initial ck_sreg = 0;
always @(posedge i_clk)
if (i_reset || i_data_en)
ck_psreg <= 0;
else
ck_psreg <= wide_pedge[HWBIAS+2:0];
initial sample_ck = 0;
always @(*)
if (i_data_en)
sample_pck = 0;
else
// Verilator lint_off WIDTH
sample_pck = wide_pedge[HWBIAS +: 4] >> i_sample_shift[4:3];
// Verilator lint_on WIDTH
// }}}
// cmd_sample_ck: When do we sample the command line?
// {{{
assign wide_cmdedge = { pck_sreg[HWBIAS+2:0], |next_pedge };
always @(posedge i_clk)
if (i_reset || i_cmd_en)
pck_sreg <= 0;
else
pck_sreg <= wide_cmdedge[HWBIAS+2:0];
always @(*)
if (i_cmd_en)
cmd_sample_ck = 0;
else
// Verilator lint_off WIDTH
cmd_sample_ck = wide_cmdedge[HWBIAS +: 4] >> i_sample_shift[4:3];
// Verilator lint_on WIDTH
// }}}
assign raw_iodat = i_dat;
// CRC TOKEN detection
// {{{
always @(posedge i_clk)
if(i_reset || i_data_en || i_cfg_ds || !OPT_CRCTOKEN)
acknak_sreg <= -1;
else if (acknak_sreg[4] && sample_pck)
acknak_sreg <= { acknak_sreg[3:0], raw_iodat[0] };
initial { sync_ack, sync_nak } = 2'b00;
always @(posedge i_clk)
if(i_reset || i_data_en || i_cfg_ds || !OPT_CRCTOKEN)
begin
sync_ack <= 1'b0;
sync_nak <= 1'b0;
end else begin
sync_ack <= (acknak_sreg == 5'b00101);
sync_nak <= (acknak_sreg == 5'b01011);
end
// }}}
always @(posedge i_clk)
if (i_reset || i_cmd_en || i_cfg_dscmd)
resp_started <= 1'b0;
else if (!i_cmd && cmd_sample_ck)
resp_started <= 1'b1;
always @(posedge i_clk)
if (i_reset || i_data_en || !i_rx_en || i_cfg_ds)
io_started <= 2'b0;
else if (i_cfg_ddr && io_started[0] && sample_ck)
io_started <= 2'b11;
else if (!i_dat[0] && sample_pck)
io_started <= (i_cfg_ddr) ? 2'b01 : 2'b11;
// dat0_busy, wait_for_busy
// {{{
/*
if (OPT_CRCTOKEN)
begin : GEN_WAIT_ON_TOKEN
reg wait_for_token;
reg [3:0] token_sreg;
always @(posedge i_clk)
if (i_reset || i_data_en || i_rx_en || !wait_for_token)
token_sreg <= -1;
else if (sample_pck)
token_sreg <= { token_sreg[2:0], i_dat[0] };
always @(posedge i_clk)
if (i_reset || i_rx_en)
wait_for_token <= 1'b0;
else if (data_en)
wait_for_token <= 1'b1;
else if (!token_sreg[3])
wait_for_token <= 1'b0;
always @(posedge i_clk)
if (i_reset || !wait_for_token)
{ r_crcnak, r_crcack } <= 2'b00;
end else begin
end
*/
// busy_count: SD Clock cycles to wait before busy asserted
// {{{
initial busy_count = 0;
always @(posedge i_clk)
if (i_reset || i_cmd_en || i_data_en)
busy_count <= BUSY_CLOCKS;
else if (sample_pck && busy_count > 0)
busy_count <= busy_count-1;
// }}}
initial { dat0_busy, wait_for_busy } = 2'b01;
always @(posedge i_clk)
if (i_reset || i_data_en)
begin
// *MUST* clear busy on i_data_en, else we'd overwrite
// the busy bit anyway by transmitting
dat0_busy <= 1'b0;
wait_for_busy <= 1'b1;
end else if (dat0_busy && !i_dat[0] && !wait_for_busy)
begin
// If busy is already set, keep it set until D0 rises
dat0_busy <= 1'b1;
// wait_for_busy <= 1'b0;
end else if (i_cmd_en)
begin
dat0_busy <= 1'b0; // Should already be zero
wait_for_busy <= 1'b1;
end else if (wait_for_busy)
begin
dat0_busy <= 1'b1;
wait_for_busy <= (busy_count > 1);
end else if (i_dat[0])
dat0_busy <= 1'b0;
assign o_data_busy = dat0_busy;
// }}}
always @(posedge i_clk)
begin
if (i_cmd_en || !cmd_sample_ck || i_cfg_dscmd)
r_cmd_strb <= 1'b0;
else if (!i_cmd || resp_started)
r_cmd_strb <= 1'b1;
else
r_cmd_strb <= 1'b0;
if (i_data_en || sample_ck == 0 || i_cfg_ds)
r_rx_strb <= 1'b0;
else if (io_started[1])
r_rx_strb <= 1'b1;
else
r_rx_strb <= 1'b0;
if (cmd_sample_ck)
r_cmd_data <= i_cmd;
if (sample_ck)
begin
r_rx_data <= 0;
r_rx_data[NUMIO-1:0] <= i_dat;
end
end
assign o_cmd_strb = { r_cmd_strb, 1'b0 };
assign o_cmd_data = { r_cmd_data, 1'b0 };
assign o_rx_strb = { r_rx_strb, 1'b0 };
assign o_rx_data = { r_rx_data, 8'h0 };
reg [7:0] w_out;
always @(*)
begin
w_out = 0;
w_out[NUMIO-1:0] = i_dat;
end
assign o_debug = {
i_cmd_en || i_data_en,
5'h0,
i_sdclk[7], 1'b0,
i_cmd_en, i_cmd_data[1], i_cmd,
(io_cmd_tristate) ? i_cmd: o_cmd,//w_cmd
r_cmd_strb, r_cmd_data, // 2b
i_data_en, r_rx_strb, r_rx_data, // 10b
//
((i_data_en) ? i_tx_data[31:24] : w_out) // 8b
};
// Keep Verilator happy
// {{{
// Verilator coverage_off
// Verilator lint_off UNUSED
wire unused_no_serdes;
assign unused_no_serdes = &{ 1'b0,
i_cfg_ds, i_ds,
i_sdclk[6:0], i_tx_data[23:0],
i_cmd_data[0], i_hsclk,i_sample_shift
};
// Verilator lint_on UNUSED
// Verilator coverage_on
// }}}
// }}}
end else if (!OPT_SERDES && OPT_DDR)
begin : GEN_IODDR_IO
// {{{
// Notes:
// {{{
// The idea is, if we only have DDR elements and no SERDES
// elements, can we do better than with just IOs?
//
// The answer is, Yes. Even though we aren't going to run at
// 2x the clock speed w/o OPT_SERDES, we can output a DDR clk,
// and we can also access sub-sample timing via IDDR elements.
// Even in DDR mode, however, there will be no possibility of
// two outputs per clock.
//
// Fastest clock supported = incoming clock speed
// Practically, you won't be likely to achieve this unless
// you get really lucky, but it is technically the fastest
// speed this version supports.
// A more realistic speed will be the incoming clock speed / 2,
// and done with more reliability than the non-DDR mode.
// }}}
// Local declarations
// {{{
wire [1:0] w_cmd;
wire [15:0] w_dat;
reg [5:0] ck_sreg;
reg [6:0] pck_sreg, ck_psreg;
reg [1:0] sample_ck, cmd_sample_ck, sample_pck;
reg resp_started, r_last_cmd_enabled,
r_cmd_strb, r_cmd_data, r_rx_strb;
wire [1:0] my_cmd_data;
reg [1:0] io_started;
reg [7:0] r_rx_data;
reg [1:0] busy_delay;
wire [HWBIAS+7:0] wide_pedge, wide_dedge, wide_cmdedge;
// Verilator lint_off UNUSED
wire io_clk_tristate, ign_clk;
assign ign_clk = o_ck;
// Verilator lint_on UNUSED
// }}}
// Clock
// {{{
xsdddr #(.OPT_BIDIR(1'b0))
u_clk_oddr(
.i_clk(i_clk), .i_en(1'b1),
.i_data({ i_sdclk[7], i_sdclk[3] }),
.io_pin_tristate(io_clk_tristate),
.o_pin(o_ck),
.i_pin(ign_clk),
// Verilator lint_off PINCONNECTEMPTY
.o_mine(),
.o_wide()
// Verilator lint_on PINCONNECTEMPTY
);
// }}}
// CMD
// {{{
always @(posedge i_clk)
r_last_cmd_enabled <= i_cmd_en;
xsdddr #(.OPT_BIDIR(1'b1))
u_cmd_ddr(
.i_clk(i_clk),
.i_en(!i_cmd_tristate && !o_cmd_collision),
.i_data({(2){ i_reset || i_cmd_data[1] }}),
.io_pin_tristate(io_cmd_tristate),
.o_pin(o_cmd),
.i_pin(i_cmd),
.o_mine(my_cmd_data),
.o_wide(w_cmd)
);
assign raw_cmd = i_cmd;
assign w_cmd_collision = OPT_COLLISION && i_cmd_en
&& |(my_cmd_data & ~w_cmd);
if (OPT_COLLISION)
begin : GEN_COLLISION
reg r_collision;
always @(posedge i_clk)
if (i_reset || !i_cmd_en)
r_collision <= 1'b0;
else if (w_cmd_collision)
r_collision <= 1'b1;
assign o_cmd_collision = r_collision;
end else begin : NO_COLLISION
assign o_cmd_collision = 1'b0;
// Verilator lint_off UNUSED
wire unused_collision;
assign unused_collision = &{ 1'b0, w_cmd_collision };
// Verilator lint_on UNUSED
end
// }}}
// DATA
// {{{
for(gk=0; gk<NUMIO; gk=gk+1)
begin : DRIVE_DDR_IO
xsdddr #(.OPT_BIDIR(1'b1))
u_dat_ddr(
.i_clk(i_clk),
.i_en(!i_data_tristate),
.i_data({(2){ i_reset || i_tx_data[24+gk] }}),
.io_pin_tristate(io_dat_tristate[gk]),
.o_pin(o_dat[gk]),
.i_pin(i_dat[gk]),
// Verilator lint_off PINCONNECTEMPTY
.o_mine(),
// Verilator lint_on PINCONNECTEMPTY
.o_wide({ w_dat[gk+8], w_dat[gk] })
);
assign raw_iodat[gk] = i_dat[gk];
end for(gk=NUMIO; gk<8; gk=gk+1)
begin : NO_DDR_IO
assign { w_dat[8+gk], w_dat[gk] } = 2'b00;
end
// }}}
// sample_ck
// {{{
assign wide_dedge = { ck_sreg[HWBIAS+5:0], |next_dedge[7:4], |next_dedge[3:0] };
initial ck_sreg = 0;
always @(posedge i_clk)
if (i_data_en || i_cfg_ds)
ck_sreg <= 0;
else
ck_sreg <= wide_dedge[HWBIAS+5:0];
initial sample_ck = 0;
always @(*)
if (i_data_en || !i_rx_en || i_cfg_ds)
sample_ck = 0;
else
// Verilator lint_off WIDTH
sample_ck = wide_dedge[HWBIAS +: 8] >> i_sample_shift[4:2];
// Verilator lint_on WIDTH
// }}}
// sample_pck -- positive edge data sampl clock
// {{{
assign wide_pedge = { ck_psreg[HWBIAS+5:0], |next_pedge[7:4], |next_pedge[3:0] };
initial ck_psreg = 0;
always @(posedge i_clk)
if (i_data_en)
ck_psreg <= 0;
else
ck_psreg <= wide_pedge[HWBIAS+5:0];
initial sample_pck = 0;
always @(*)
if (i_data_en)
sample_pck = 0;
else
// Verilator lint_off WIDTH
sample_pck = wide_pedge[HWBIAS +: 8] >> i_sample_shift[4:2];
// Verilator lint_on WIDTH
// }}}
// cmd_sample_ck: When do we sample the command line?
// {{{
assign wide_cmdedge = { pck_sreg[HWBIAS+5:0], |next_pedge[7:4], |next_pedge[3:0] };
always @(posedge i_clk)
if (i_reset || i_cmd_en || r_last_cmd_enabled || i_cfg_dscmd)
pck_sreg <= 0;
else
pck_sreg <= wide_cmdedge[HWBIAS + 5:0];
always @(*)
if (i_cmd_en || r_last_cmd_enabled || i_cfg_dscmd)
cmd_sample_ck = 0;
else
// Verilator lint_off WIDTH
cmd_sample_ck = wide_cmdedge[HWBIAS +: 8] >> i_sample_shift[4:2];
// Verilator lint_on WIDTH
// }}}
// CRC TOKEN detection
// {{{
always @(posedge i_clk)
if(i_reset || i_data_en || i_cfg_ds || !OPT_CRCTOKEN)
acknak_sreg <= -1;
else if (acknak_sreg[4])
begin
if (sample_pck[1:0] == 2'b11 && acknak_sreg[3])
acknak_sreg <= { acknak_sreg[2:0], w_dat[8], w_dat[0] };
else if (sample_pck[1])
acknak_sreg <= { acknak_sreg[3:0], w_dat[8] };
else if (sample_pck[0])
acknak_sreg <= { acknak_sreg[3:0], w_dat[0] };
end
initial { sync_ack, sync_nak } = 2'b00;
always @(posedge i_clk)
if(i_reset || i_data_en || i_cfg_ds || !OPT_CRCTOKEN)
begin
sync_ack <= 1'b0;
sync_nak <= 1'b0;
end else begin
sync_ack <= (acknak_sreg == 5'b00101);
sync_nak <= (acknak_sreg == 5'b01011);
end
// }}}
always @(posedge i_clk)
if (i_reset || i_cmd_en || r_last_cmd_enabled || i_cfg_dscmd)
resp_started <= 1'b0;
else if ((cmd_sample_ck != 0) && (cmd_sample_ck & w_cmd)==0)
resp_started <= 1'b1;
always @(posedge i_clk)
if (i_reset || i_data_en || !i_rx_en || i_cfg_ds)
io_started <= 2'b0;
else if (sample_pck != 0
&& ((sample_pck & { w_dat[8], w_dat[0] }) == 0))
begin
io_started[0] <= 1'b1;
if (!i_cfg_ddr)
io_started[1] <= 1'b1;
else if (sample_pck[1] && sample_ck[0])
io_started[1] <= 1'b1;
end else if (io_started == 2'b01 && sample_ck != 0)
io_started <= 2'b11;
// dat0_busy, wait_for_busy, busy_delay
// {{{
initial busy_count = (OPT_CRCTOKEN) ? 3'h0 : 3'h4;
always @(posedge i_clk)
if (i_reset || i_cmd_en || i_data_en)
// Clock periods to wait until busy is active
busy_count <= BUSY_CLOCKS;
else if (sample_pck != 0 && busy_count > 0)
busy_count <= busy_count - 1;
initial busy_delay = -1;
always @(posedge i_clk)
if (i_reset || i_data_en)
// System clock cycles to wait until busy can be read
busy_delay <= -1;
else if (busy_delay != 0)
busy_delay <= busy_delay - 1;
initial { dat0_busy, wait_for_busy } = 2'b01;
always @(posedge i_clk)
if (i_reset || i_data_en)
begin
dat0_busy <= 1'b0;
wait_for_busy <= 1'b1;
end else if (dat0_busy && !wait_for_busy &&
((sample_pck == 0)
|| (sample_pck & {w_dat[8],w_dat[0]})!=sample_pck))
begin
// Still busy ...
dat0_busy <= 1'b1;
wait_for_busy <= 1'b0;
end else if (i_cmd_en)
begin
dat0_busy <= 1'b0; // Should already be zero
wait_for_busy <= 1'b1;
end else if (wait_for_busy)
begin
dat0_busy <= 1'b1;
wait_for_busy <= (busy_delay > 0) || (busy_count > 1);
end else if ((sample_pck != 0)
&& (sample_pck & {w_dat[8],w_dat[0]})!=2'b0)
dat0_busy <= 1'b0;
assign o_data_busy = dat0_busy;
// }}}
always @(posedge i_clk)
begin
// The command response
// {{{
if (i_reset || i_cmd_en || cmd_sample_ck == 0 || i_cfg_dscmd)
begin
r_cmd_strb <= 1'b0;
// r_cmd_data <= r_cmd_data;
end else if (resp_started)
begin
r_cmd_strb <= 1'b1;
r_cmd_data <= |(cmd_sample_ck & w_cmd);
end else if ((cmd_sample_ck[1] && !w_cmd[1])
||(cmd_sample_ck[0] && !w_cmd[0]))
begin
r_cmd_strb <= 1'b1;
r_cmd_data <= 1'b0;
end else
r_cmd_strb <= 1'b0;
// }}}
// The data response
// {{{
if (i_data_en || sample_ck == 0 || i_cfg_ds)
r_rx_strb <= 1'b0;
else if (io_started[1])
r_rx_strb <= 1'b1;
else
r_rx_strb <= 1'b0;
// }}}
if (sample_ck[1])
r_rx_data <= w_dat[15:8];
else
r_rx_data <= w_dat[7:0];
end
assign o_cmd_strb = { r_cmd_strb, 1'b0 };
assign o_cmd_data = { r_cmd_data, 1'b0 };
assign o_rx_strb = { r_rx_strb, 1'b0 };
assign o_rx_data = { r_rx_data, 8'h0 };
reg [7:0] w_out;
always @(*)
begin
w_out = 0;
w_out[NUMIO-1:0] = w_dat[8 +: NUMIO]& w_dat[0 +: NUMIO];
end
assign o_debug = {
i_cmd_en || i_data_en, 2'h0, i_rx_en,
sample_ck, i_sdclk[7], i_sdclk[3],
i_cmd_en, i_cmd_data[1:0],
(&w_cmd), r_cmd_strb, r_cmd_data,
i_data_en, r_rx_strb, r_rx_data,
//
((i_data_en) ? i_tx_data[31:24] : w_out)
};
// Keep Verilator happy
// {{{
// Verilator coverage_off
// Verilator lint_off UNUSED
wire unused_ddr;
assign unused_ddr = &{ 1'b0, i_hsclk,
i_cfg_ds, i_ds, i_tx_data[23:0],
i_sdclk[6:4], i_sdclk[2:0],
i_sample_shift[1:0] };
// Verilator lint_on UNUSED
// Verilator coverage_on
// }}}
// }}}
end else begin : GEN_WIDE_IO
// {{{
// Generic PHY handler, designed to support up to HS400.
// Outputs 4 data periods per incoming clock, and 8 clock values
// per incoming clock--hence the outgoing clock may have a
// 90 degree shift from the data. When dealing with non-DS
// data, the clock edge is detected on output, and a sample
// controller decides when to sample it on input.
//
// Fastest clock supported = incoming clock speed * 2
// Local declarations
// {{{
reg r_last_cmd_enabled;
reg [1:0] w_cmd_data;
reg [15:0] r_rx_data;
wire [15:0] w_rx_data;
wire [7:0] next_ck_sreg, next_ck_psreg;
reg [24:0] ck_sreg, ck_psreg;
wire [7:0] wide_cmd_data;
reg [7:0] sample_ck, sample_pck;
reg [1:0] r_cmd_data;
reg busy_strb;
reg [1:0] r_rx_strb;
reg [1:0] start_io;
reg [1:0] io_started;
reg resp_started;
reg [1:0] r_cmd_strb;
reg [24:0] pck_sreg;
reg [7:0] cmd_sample_ck;
wire busy_pin;
reg [1:0] busy_delay, itok;
wire [HWBIAS+31:0] wide_pedge, wide_dedge, wide_cmdedge;
// Verilator lint_off UNUSED
wire [7:0] my_cmd_data;
// Verilator lint_on UNUSED
reg r_cmd_tristate, r_data_tristate;
// }}}
// Clock
// {{{
// Verilator lint_off UNUSED
wire io_clk_tristate, ign_clk_raw;
wire [7:0] ign_clk_mine, ign_clk_wide;
// Verilator lint_on UNUSED
xsdserdes8x #(.OPT_BIDIR(1'b0))
u_clk_oserdes(
.i_clk(i_clk),
.i_hsclk(i_hsclk),
.i_en(1'b1),
.i_data(i_sdclk),
.io_tristate(io_clk_tristate),
.o_pin(o_ck),
.i_pin(1'b0),
// Verilator lint_off PINCONNECTEMPTY
.o_raw(ign_clk_raw), .o_mine(ign_clk_mine),
.o_wide(ign_clk_wide)
// Verilator lint_on PINCONNECTEMPTY
);
// }}}
assign next_ck_sreg = (i_data_en) ? 8'h0 : next_dedge;
assign next_ck_psreg = (i_data_en) ? 8'h0 : next_pedge;
// sample_ck
// {{{
assign wide_dedge = { ck_sreg[HWBIAS+23:0], next_dedge };
// We use this for busy detection, as well as reception
always @(posedge i_clk)
if (i_reset || i_data_en)
//||(!wait_for_busy && (!i_rx_en || i_cfg_ds)))
ck_sreg <= 0;
else
ck_sreg <= wide_dedge[HWBIAS+23:0];
initial sample_ck = 0;
always @(posedge i_clk)
if (i_reset || i_data_en)
//||(!wait_for_busy && (!i_rx_en || i_cfg_ds)))
sample_ck <= 0;
else
// Verilator lint_off WIDTH
sample_ck <= wide_dedge[HWBIAS +: 32] >> i_sample_shift;
// Verilator lint_on WIDTH
// }}}
// sample_pck
// {{{
assign wide_pedge = { ck_psreg[HWBIAS+23:0], next_pedge };
always @(posedge i_clk)
if (i_reset || i_data_en)
ck_psreg <= 0;
else
ck_psreg <= wide_pedge[HWBIAS + 23:0];
initial sample_pck = 0;
always @(posedge i_clk)
if (i_reset || i_data_en)
sample_pck <= 0;
else
// Verilator lint_off WIDTH
sample_pck <= wide_pedge[HWBIAS +: 32] >> i_sample_shift;
// Verilator lint_on WIDTH
// }}}
always @(posedge i_clk)
r_data_tristate <= i_data_tristate;
for(gk=0; gk<NUMIO; gk=gk+1)
begin : GEN_WIDE_DATIO
// {{{
reg [7:0] out_pin;
wire [7:0] in_pin;
integer ik;
reg [1:0] lcl_data;
always @(*)
for(ik=0; ik<4; ik=ik+1)
out_pin[ik*2 +: 2] = {(2){i_tx_data[ik*8+gk]}};
xsdserdes8x #(
.OPT_BIDIR(1'b1)
) io_serdes(
.i_clk(i_clk),
.i_hsclk(i_hsclk),
.i_en(!r_data_tristate),
.i_data(out_pin),
.io_tristate(io_dat_tristate[gk]),
.o_pin(o_dat[gk]),
.i_pin(i_dat[gk]),
// Verilator lint_off PINCONNECTEMPTY
.o_mine(),
// Verilator lint_on PINCONNECTEMPTY
.o_raw(raw_iodat[gk]), .o_wide(in_pin)
);
if (gk == 0)
begin : GEN_START_SIGNAL
always @(*)
begin
start_io[1] = (|sample_pck[7:4])
&&(0 == (sample_pck[7:4]&in_pin[7:4]));
start_io[0] = (|sample_pck[3:0])
&&(0 == (sample_pck[3:0]&in_pin[3:0]));
itok[1] = |(sample_pck[7:4] & in_pin[7:4]);
itok[0] = |(sample_pck[3:0] & in_pin[3:0]);
end
assign busy_pin = !in_pin[0];
end
always @(*)
begin
lcl_data[1] = |(sample_ck[7:4]&in_pin[7:4]);
lcl_data[0] = |(sample_ck[3:0]&in_pin[3:0]);
end
assign w_rx_data[8+gk] = lcl_data[1];
assign w_rx_data[ gk] = lcl_data[0];
// }}}
end
for(gk=NUMIO; gk<8; gk=gk+1)
begin : NULL_DATIO
// {{{
assign w_rx_data[8+gk] = 1'b1;
assign w_rx_data[ gk] = 1'b1;
// Keep Verilator happy
// {{{
// Verilator coverage_off
// Verilator lint_off UNUSED
wire unused_outputs;
assign unused_outputs = &{ 1'b0 }; // , out_pin
// Verilator lint_on UNUSED
// Verilator coverage_on
// }}}
// }}}
end
// o_rx_strb, o_rx_data
// {{{
always @(posedge i_clk)
if (i_reset || i_data_en || !i_rx_en || i_cfg_ds)
io_started <= 2'b0;
else if (!io_started[1])
begin
if (start_io[1] && (|sample_ck[3:0]))
io_started <= 2'b11;
else if (io_started[0])
io_started[1] <= |sample_ck;
else begin // if (!io_started[0] && start_io[0])
io_started[0] <= |start_io;
if (!i_cfg_ddr)
io_started[1] <= |start_io;
end
end
always @(posedge i_clk)
if (i_reset || i_cfg_ds || !i_rx_en || i_data_en)
r_rx_strb <= 2'b0;
else if (sample_ck == 0)
r_rx_strb <= 2'b0;
else if (io_started == 0