-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathsdstream.v
252 lines (221 loc) · 6.73 KB
/
sdstream.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
////////////////////////////////////////////////////////////////////////////////
//
// Filename: bench/testscript/sdstream.v
// {{{
// Project: SD-Card controller
//
// Purpose: Drive and demonstrate the SD card controller's streaming
// interface option that can be used with the DMA controller
// and an (external) DMA source/sink.
//
// 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
//
////////////////////////////////////////////////////////////////////////////////
//
`include "../testscript/sdiolib.v"
// }}}
reg expect_dma_err = 1'b0;
always @(posedge clk)
if (!reset && !expect_dma_err && (u_sdio.u_sdio.dma_err
|| (u_sdio.u_sdio.s2sd_valid
&& (u_sdio.u_sdio.s2sd_data != u_sdio.u_sdio.s2sd_data))
|| (u_sdio.u_sdio.sd2s_valid
&& (u_sdio.u_sdio.sd2s_data != u_sdio.u_sdio.sd2s_data))))
begin
$display("ERROR: Unexpected DMA ERR detected");
error_flag <= 1'b1;
end
always @(posedge clk)
if (!reset && !expect_dma_err && u_sdcard.rx_err && !error_flag)
begin
$display("ERROR: SDCard (Model) RX ERR (bad CRC) detected");
error_flag <= 1'b1;
end
always @(posedge clk)
if (!reset && stream_error_flag)
begin
if (!error_flag)
$display("ERROR: SDCard (Model) DMA stream error detected");
error_flag <= 1'b1;
end
task testscript;
reg [31:0] read_data, ocr_reg, if_cond, op_cond, r6;
reg [15:0] rca;
reg [127:0] CID;
reg [31:0] sample_shift;
begin
@(posedge clk);
while(reset !== 1'b0)
@(posedge clk);
@(posedge clk);
// Start by "inserting" an SD card
// {{{
`ifdef SDIO_AXI
read_data = 32'h0; read_data[2] = 1'b1;
u_bfm.write_f(GPIO_ADDR+4, read_data);
`else
read_data = 32'h0; read_data[18] = 1'b1; read_data[2] = 1'b1;
u_bfm.write_f(GPIO_ADDR, read_data);
`endif
// }}}
// Then wait for that card to be detected
// {{{
wait(sdio_interrupt);
u_bfm.readio(ADDR_SDCARD, read_data);
assert(1'b0 === read_data[19]);
// Clear the SD card removed bit
read_data = 32'h0c_8080;
u_bfm.write_f(ADDR_SDCARD, read_data);
// }}}
// Set clock speed = 25MHz
// {{{
// We can start at 25MHz b/c the simulation model allows us to. We
// might not do this with real hardware.
sample_shift = { 11'h0, 5'h08, 16'h0 };
u_bfm.write_f(ADDR_SDPHY, SECTOR_16B | SPEED_25MHZ | SDPHY_W1 | { 11'h0, 5'h1f, 16'h0 });
u_bfm.readio(ADDR_SDPHY, read_data);
if (3'h0 == read_data[18:16])
// OPT_"RAW" (Neither SERDES nor DDR)
sample_shift = { 11'h0, 5'h08, 16'h0 };
else if (2'b00 == read_data[17:16])
// OPT_DDR
sample_shift = { 11'h0, 5'h0c, 16'h0 };
else
// OPT_SERDES
sample_shift = { 11'h0, 5'h03, 16'h0 };
u_bfm.write_f(ADDR_SDPHY, SECTOR_16B | SPEED_25MHZ | SDPHY_W1 | sample_shift);
do begin
u_bfm.readio(ADDR_SDPHY, read_data);
end while(read_data[7:0] != SPEED_25MHZ[7:0]);
u_bfm.readio(ADDR_SDCARD, read_data);
// }}}
$display("CMD0: GO-IDLE");
sdcard_go_idle;
// IF condition and OP-Cond
// {{{
if_cond = 32'h01a5;
sdcard_send_if_cond(if_cond);
u_bfm.readio(ADDR_SDCARD, read_data);
$display("IF-COND: %08x", read_data);
if (read_data[15] && read_data[17:16] == 2'b00)
begin
// No response from card
do begin
op_cond = 32'h0ff_8000;
sdcard_send_op_cond(op_cond);
end while(1'b0 === op_cond[31]);
end else begin
assert(if_cond[7:0] == 8'ha5);
op_cond = 32'h4000_0000;
op_cond[24] = OPT_1P8V;
sdcard_send_op_cond(op_cond);
do begin
op_cond = 32'h40ff_8000;
op_cond[24] = OPT_1P8V;
sdcard_send_op_cond(op_cond);
end while(1'b0 === op_cond[31]);
end
$display("OP-COND: %08x", op_cond);
// }}}
if (OPT_1P8V && op_cond[24])
begin
sdcard_send_voltage_switch; // CMD11
end
// else max_spd = SPEED_50MHZ;
// Assign RCAs
// {{{
// CMD2 // All send CID
sdcard_all_send_cid;
u_bfm.readio(ADDR_FIFOA, CID[127:96]);
u_bfm.readio(ADDR_FIFOA, CID[ 95:64]);
u_bfm.readio(ADDR_FIFOA, CID[ 63:32]);
u_bfm.readio(ADDR_FIFOA, CID[ 31: 0]);
$display("READ-CID: %08x:%08x:%08x:%08x",
CID[127:96], CID[95:64], CID[63:32], CID[31:0]);
assert(CID[127:8] == u_sdcard.CID);
// CMD3 SEND_RELATIVE_ADDR
sdcard_send_relative_addr(r6);
rca = r6[31:16];
$display("ASSIGNED-RCA: %04x", rca);
// }}}
// CMD7 SELECT/DESELCT Card
sdcard_select_card(rca);
// Set up for 1b @ 25MHz
// {{{
u_bfm.write_f(ADDR_SDPHY, SECTOR_16B | SPEED_DS | SDPHY_W1 | sample_shift);
do begin
u_bfm.readio(ADDR_SDPHY, read_data);
end while(read_data[7:0] != SPEED_DS[7:0]);
// }}}
$display("Set bus width to 4b");
sdcard_set_bus_width(2'b10);
// Test at SPEED_DS=25MHZ SDR = 12.5MB/s, or 32b / 32 clocks
// {{{
$display("Set speed to DS, i.e. 25MHz");
u_bfm.write_f(ADDR_SDPHY, SECTOR_512B | SPEED_DS | SDPHY_W4 | sample_shift);
if (OPT_1P8V && op_cond[24])
sdcard_send_tuning_block;
sdcard_write_stream(5, 32'h3);
sdcard_read_stream(5, 32'h3);
// }}}
// Test at SPEED_100=50MHZ SDR = 100MB/s, or 32b / 8 clocks
// {{{
if (OPT_SERDES || OPT_DDR)
begin
$display("Set speed to SDR50, i.e. 100MHz");
u_bfm.write_f(ADDR_SDPHY, SECTOR_512B | SPEED_SDR100 | SDPHY_W4 | sample_shift);
u_bfm.readio(ADDR_SDPHY, read_data);
while(read_data[7:0] != SPEED_SDR100[7:0])
begin
u_bfm.readio(ADDR_SDPHY, read_data);
end
if (OPT_1P8V && op_cond[24])
sdcard_send_tuning_block;
sdcard_write_stream(7, 32'h2);
sdcard_read_stream(7, 32'h2);
end
// }}}
// Test at SPEED_200=200MHZ SDR = 100MB/s, or 32b / 4 clocks
// {{{
if (OPT_SERDES)
begin
$display("Set speed to SDR104, i.e. 200MHz");
u_bfm.write_f(ADDR_SDPHY, SECTOR_512B | SPEED_SDR200 | SDPHY_W4 | sample_shift);
u_bfm.readio(ADDR_SDPHY, read_data);
while(read_data[7:0] != SPEED_SDR200[7:0])
begin
u_bfm.readio(ADDR_SDPHY, read_data);
end
if (OPT_1P8V && op_cond[24])
sdcard_send_tuning_block;
sdcard_write_stream(6, 32'h4);
sdcard_read_stream(6, 32'h4);
end
// }}}
repeat(512)
@(posedge clk);
end endtask