1 |
2 |
dgisselq |
////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
//
|
3 |
|
|
// Filename: sdspi.v
|
4 |
|
|
//
|
5 |
|
|
// Project: SD-Card controller, using a shared SPI interface
|
6 |
|
|
//
|
7 |
|
|
// Purpose:
|
8 |
|
|
//
|
9 |
|
|
// Creator: Dan Gisselquist, Ph.D.
|
10 |
|
|
// Gisselquist Technology, LLC
|
11 |
|
|
//
|
12 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
13 |
|
|
//
|
14 |
3 |
dgisselq |
// Copyright (C) 2016-2017, Gisselquist Technology, LLC
|
15 |
2 |
dgisselq |
//
|
16 |
|
|
// This program is free software (firmware): you can redistribute it and/or
|
17 |
|
|
// modify it under the terms of the GNU General Public License as published
|
18 |
|
|
// by the Free Software Foundation, either version 3 of the License, or (at
|
19 |
|
|
// your option) any later version.
|
20 |
|
|
//
|
21 |
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
22 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
|
23 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
24 |
|
|
// for more details.
|
25 |
|
|
//
|
26 |
|
|
// You should have received a copy of the GNU General Public License along
|
27 |
3 |
dgisselq |
// with this program. (It's in the $(ROOT)/doc directory. Run make with no
|
28 |
2 |
dgisselq |
// target there if the PDF file isn't present.) If not, see
|
29 |
|
|
// <http://www.gnu.org/licenses/> for a copy.
|
30 |
|
|
//
|
31 |
|
|
// License: GPL, v3, as defined and found on www.gnu.org,
|
32 |
|
|
// http://www.gnu.org/licenses/gpl.html
|
33 |
|
|
//
|
34 |
|
|
//
|
35 |
|
|
////////////////////////////////////////////////////////////////////////////////
|
36 |
|
|
//
|
37 |
|
|
//
|
38 |
3 |
dgisselq |
`default_nettype none
|
39 |
|
|
//
|
40 |
2 |
dgisselq |
`define SDSPI_CMD_ADDRESS 2'h0
|
41 |
|
|
`define SDSPI_DAT_ADDRESS 2'h1
|
42 |
|
|
`define SDSPI_FIFO_A_ADDR 2'h2
|
43 |
|
|
`define SDSPI_FIFO_B_ADDR 2'h3
|
44 |
|
|
//
|
45 |
|
|
// `define SDSPI_CMD_ID 3'h0
|
46 |
|
|
// `define SDSPI_CMD_A1 3'h1
|
47 |
|
|
// `define SDSPI_CMD_A2 3'h2
|
48 |
|
|
// `define SDSPI_CMD_A3 3'h3
|
49 |
|
|
// `define SDSPI_CMD_A4 3'h4
|
50 |
|
|
// `define SDSPI_CMD_CRC 3'h5
|
51 |
|
|
// `define SDSPI_CMD_FIFO 3'h6
|
52 |
|
|
// `define SDSPI_CMD_WAIT 3'h7
|
53 |
|
|
//
|
54 |
|
|
`define SDSPI_EXPECT_R1 2'b00
|
55 |
|
|
`define SDSPI_EXPECT_R1B 2'b01
|
56 |
|
|
`define SDSPI_EXPECT_R3 2'b10
|
57 |
|
|
//
|
58 |
|
|
`define SDSPI_RSP_NONE 3'h0 // No response yet from device
|
59 |
|
|
`define SDSPI_RSP_BSYWAIT 3'h1 // R1b, wait for device to send nonzero
|
60 |
|
|
`define SDSPI_RSP_GETWORD 3'h2 // Get 32-bit data word from device
|
61 |
|
|
`define SDSPI_RSP_GETTOKEN 3'h4 // Write to device, read from FIFO, wait for completion token
|
62 |
|
|
`define SDSPI_RSP_WAIT_WHILE_BUSY 3'h5 // Read from device
|
63 |
|
|
`define SDSPI_RSP_RDCOMPLETE 3'h6
|
64 |
|
|
`define SDSPI_RSP_WRITING 3'h7 // Read from device, write into FIFO
|
65 |
|
|
//
|
66 |
|
|
module sdspi(i_clk,
|
67 |
|
|
// Wishbone interface
|
68 |
|
|
i_wb_cyc, i_wb_stb, i_wb_we, i_wb_addr, i_wb_data,
|
69 |
|
|
o_wb_ack, o_wb_stall, o_wb_data,
|
70 |
|
|
// SDCard interface
|
71 |
|
|
o_cs_n, o_sck, o_mosi, i_miso,
|
72 |
|
|
// Our interrupt
|
73 |
|
|
o_int,
|
74 |
|
|
// And whether or not we own the bus
|
75 |
|
|
i_bus_grant,
|
76 |
|
|
// And some wires for debugging it all
|
77 |
|
|
o_debug);
|
78 |
|
|
parameter LGFIFOLN = 7;
|
79 |
3 |
dgisselq |
input wire i_clk;
|
80 |
2 |
dgisselq |
//
|
81 |
3 |
dgisselq |
input wire i_wb_cyc, i_wb_stb, i_wb_we;
|
82 |
|
|
input wire [1:0] i_wb_addr;
|
83 |
|
|
input wire [31:0] i_wb_data;
|
84 |
2 |
dgisselq |
output reg o_wb_ack;
|
85 |
|
|
output wire o_wb_stall;
|
86 |
|
|
output reg [31:0] o_wb_data;
|
87 |
|
|
//
|
88 |
|
|
output wire o_cs_n, o_sck, o_mosi;
|
89 |
3 |
dgisselq |
input wire i_miso;
|
90 |
2 |
dgisselq |
// The interrupt
|
91 |
|
|
output reg o_int;
|
92 |
|
|
// .. and whether or not we can use the SPI port
|
93 |
3 |
dgisselq |
input wire i_bus_grant;
|
94 |
2 |
dgisselq |
//
|
95 |
|
|
output wire [31:0] o_debug;
|
96 |
|
|
|
97 |
|
|
//
|
98 |
|
|
// Some WB simplifications:
|
99 |
|
|
//
|
100 |
|
|
reg r_cmd_busy;
|
101 |
3 |
dgisselq |
|
102 |
|
|
wire wb_stb, write_stb, cmd_stb, new_data, new_cmd;
|
103 |
|
|
wire [1:0] wb_addr;
|
104 |
|
|
wire [31:0] wb_data;
|
105 |
|
|
`ifdef WB_CLOCK
|
106 |
2 |
dgisselq |
wire wb_stb, write_stb, cmd_stb; // read_stb
|
107 |
3 |
dgisselq |
assign wb_stb = ((i_wb_stb)&&(!o_wb_stall));
|
108 |
2 |
dgisselq |
assign write_stb = ((wb_stb)&&( i_wb_we));
|
109 |
3 |
dgisselq |
// assign read_stb = ((wb_stb)&&(!i_wb_we));
|
110 |
|
|
assign cmd_stb = (!r_cmd_busy)&&(write_stb)
|
111 |
2 |
dgisselq |
&&(i_wb_addr==`SDSPI_CMD_ADDRESS);
|
112 |
3 |
dgisselq |
assign wb_addr = i_wb_addr;
|
113 |
|
|
assign wb_data = i_wb_data;
|
114 |
|
|
assign new_cmd = cmd_stb;
|
115 |
|
|
assign new_data = (i_wb_stb)&&(!o_wb_stall)&&(i_wb_we)
|
116 |
|
|
&&(i_wb_addr == `SDSPI_DAT_ADDRESS);
|
117 |
|
|
`else
|
118 |
|
|
reg r_wb_stb, r_write_stb, r_cmd_stb, r_new_data;
|
119 |
|
|
reg [1:0] r_wb_addr;
|
120 |
|
|
reg [31:0] r_wb_data;
|
121 |
|
|
always @(posedge i_clk)
|
122 |
|
|
r_wb_stb <= ((i_wb_stb)&&(!o_wb_stall));
|
123 |
|
|
always @(posedge i_clk)
|
124 |
|
|
r_write_stb <= ((i_wb_stb)&&(!o_wb_stall)&&(i_wb_we));
|
125 |
|
|
always @(posedge i_clk)
|
126 |
|
|
r_cmd_stb <= (!r_cmd_busy)&&(i_wb_stb)&&(!o_wb_stall)&&(i_wb_we)
|
127 |
|
|
&&(i_wb_addr == `SDSPI_CMD_ADDRESS);
|
128 |
|
|
always @(posedge i_clk)
|
129 |
|
|
r_new_data <= (i_wb_stb)&&(!o_wb_stall)&&(i_wb_we)
|
130 |
|
|
&&(i_wb_addr == `SDSPI_DAT_ADDRESS);
|
131 |
|
|
always @(posedge i_clk)
|
132 |
|
|
r_wb_addr <= i_wb_addr;
|
133 |
|
|
always @(posedge i_clk)
|
134 |
|
|
r_wb_data <= i_wb_data;
|
135 |
2 |
dgisselq |
|
136 |
3 |
dgisselq |
assign wb_stb = r_wb_stb;
|
137 |
|
|
assign write_stb= r_write_stb;
|
138 |
|
|
assign cmd_stb = r_cmd_stb;
|
139 |
|
|
assign new_cmd = r_cmd_stb;
|
140 |
|
|
assign new_data = r_new_data;
|
141 |
|
|
assign wb_addr = r_wb_addr;
|
142 |
|
|
assign wb_data = r_wb_data;
|
143 |
|
|
`endif
|
144 |
2 |
dgisselq |
|
145 |
3 |
dgisselq |
|
146 |
2 |
dgisselq |
//
|
147 |
|
|
// Access to our lower-level SDSPI driver, the one that actually
|
148 |
|
|
// uses/sets the SPI ports
|
149 |
|
|
//
|
150 |
|
|
reg [6:0] r_sdspi_clk;
|
151 |
|
|
reg ll_cmd_stb;
|
152 |
|
|
reg [7:0] ll_cmd_dat;
|
153 |
|
|
wire ll_out_stb, ll_idle;
|
154 |
|
|
wire [7:0] ll_out_dat;
|
155 |
|
|
llsdspi lowlevel(i_clk, r_sdspi_clk, r_cmd_busy, ll_cmd_stb, ll_cmd_dat,
|
156 |
|
|
o_cs_n, o_sck, o_mosi, i_miso,
|
157 |
|
|
ll_out_stb, ll_out_dat, ll_idle,
|
158 |
|
|
i_bus_grant);
|
159 |
|
|
|
160 |
|
|
|
161 |
|
|
// CRC
|
162 |
|
|
reg r_cmd_crc_stb;
|
163 |
|
|
wire [7:0] cmd_crc;
|
164 |
|
|
// State machine
|
165 |
|
|
reg [2:0] r_cmd_state, r_rsp_state;
|
166 |
|
|
// Current status, beyond state
|
167 |
|
|
reg r_have_resp, r_use_fifo, r_fifo_wr,
|
168 |
|
|
ll_fifo_rd_complete, ll_fifo_wr_complete,
|
169 |
|
|
r_fifo_id,
|
170 |
|
|
ll_fifo_wr, ll_fifo_rd,
|
171 |
|
|
r_have_data_response_token,
|
172 |
3 |
dgisselq |
r_have_start_token,
|
173 |
|
|
r_err_token;
|
174 |
|
|
reg [3:0] r_read_err_token;
|
175 |
|
|
reg [1:0] r_data_response_token;
|
176 |
2 |
dgisselq |
reg [7:0] fifo_byte;
|
177 |
|
|
reg [7:0] r_last_r_one;
|
178 |
|
|
//
|
179 |
|
|
reg [31:0] r_data_reg;
|
180 |
|
|
reg [1:0] r_data_fil, r_cmd_resp;
|
181 |
|
|
//
|
182 |
|
|
//
|
183 |
|
|
wire need_reset;
|
184 |
|
|
reg r_cmd_err;
|
185 |
|
|
reg r_cmd_sent;
|
186 |
|
|
reg [31:0] fifo_a_reg, fifo_b_reg;
|
187 |
|
|
//
|
188 |
|
|
reg q_busy;
|
189 |
|
|
//
|
190 |
3 |
dgisselq |
reg [7:0] fifo_a_mem_0[0:((1<<LGFIFOLN)-1)],
|
191 |
|
|
fifo_a_mem_1[0:((1<<LGFIFOLN)-1)],
|
192 |
|
|
fifo_a_mem_2[0:((1<<LGFIFOLN)-1)],
|
193 |
|
|
fifo_a_mem_3[0:((1<<LGFIFOLN)-1)],
|
194 |
|
|
fifo_b_mem_0[0:((1<<LGFIFOLN)-1)],
|
195 |
|
|
fifo_b_mem_1[0:((1<<LGFIFOLN)-1)],
|
196 |
|
|
fifo_b_mem_2[0:((1<<LGFIFOLN)-1)],
|
197 |
|
|
fifo_b_mem_3[0:((1<<LGFIFOLN)-1)];
|
198 |
2 |
dgisselq |
reg [(LGFIFOLN-1):0] fifo_wb_addr;
|
199 |
|
|
//
|
200 |
|
|
reg [(LGFIFOLN+1):0] ll_fifo_addr;
|
201 |
|
|
//
|
202 |
|
|
reg fifo_crc_err;
|
203 |
|
|
reg [1:0] ll_fifo_wr_state;
|
204 |
|
|
reg [7:0] fifo_a_byte, fifo_b_byte;
|
205 |
|
|
//
|
206 |
|
|
reg [2:0] ll_fifo_pkt_state;
|
207 |
|
|
reg fifo_rd_crc_stb, fifo_wr_crc_stb;
|
208 |
|
|
//
|
209 |
|
|
reg [3:0] fifo_rd_crc_count, fifo_wr_crc_count;
|
210 |
|
|
reg [15:0] fifo_rd_crc_reg, fifo_wr_crc_reg;
|
211 |
|
|
//
|
212 |
|
|
reg [3:0] r_cmd_crc_cnt;
|
213 |
|
|
reg [7:0] r_cmd_crc;
|
214 |
|
|
//
|
215 |
|
|
reg r_cmd_crc_ff;
|
216 |
|
|
//
|
217 |
|
|
reg [3:0] r_lgblklen;
|
218 |
|
|
wire [3:0] max_lgblklen;
|
219 |
|
|
assign max_lgblklen = LGFIFOLN;
|
220 |
|
|
//
|
221 |
|
|
reg [25:0] r_watchdog;
|
222 |
|
|
reg r_watchdog_err;
|
223 |
|
|
reg pre_cmd_state;
|
224 |
|
|
|
225 |
3 |
dgisselq |
// Relieve some stress from the WB bus timing
|
226 |
|
|
|
227 |
2 |
dgisselq |
initial r_cmd_busy = 1'b0;
|
228 |
|
|
initial r_data_reg = 32'h00;
|
229 |
|
|
initial r_last_r_one = 8'hff;
|
230 |
|
|
initial ll_cmd_stb = 1'b0;
|
231 |
|
|
initial ll_fifo_rd = 1'b0;
|
232 |
|
|
initial ll_fifo_wr = 1'b0;
|
233 |
|
|
initial r_rsp_state = 3'h0;
|
234 |
|
|
initial r_cmd_state = 3'h0;
|
235 |
|
|
initial r_use_fifo = 1'b0;
|
236 |
|
|
initial r_data_fil = 2'b00;
|
237 |
|
|
initial r_lgblklen = LGFIFOLN;
|
238 |
|
|
initial r_cmd_err = 1'b0;
|
239 |
|
|
always @(posedge i_clk)
|
240 |
|
|
begin
|
241 |
3 |
dgisselq |
if (!ll_cmd_stb)
|
242 |
2 |
dgisselq |
begin
|
243 |
|
|
r_have_resp <= 1'b0;
|
244 |
|
|
ll_fifo_wr <= 1'b0;
|
245 |
|
|
ll_fifo_rd <= 1'b0;
|
246 |
|
|
// r_rsp_state <= 3'h0;
|
247 |
|
|
r_cmd_state <= 3'h0;
|
248 |
|
|
r_use_fifo <= 1'b0;
|
249 |
|
|
r_data_fil <= 2'b00;
|
250 |
|
|
r_cmd_resp <= `SDSPI_EXPECT_R1;
|
251 |
|
|
end
|
252 |
|
|
|
253 |
|
|
r_cmd_crc_stb <= 1'b0;
|
254 |
|
|
if (pre_cmd_state)
|
255 |
|
|
begin // While we are actively sending data, and clocking the
|
256 |
|
|
// interface, do:
|
257 |
|
|
//
|
258 |
|
|
// Here we use the transmit command state, or
|
259 |
|
|
// r_cmd_state, to determine where we are at in this
|
260 |
|
|
// process, and we use (ll_cmd_stb)&&(ll_idle) to
|
261 |
|
|
// determine that we have sent a byte. ll_cmd_dat is
|
262 |
|
|
// set here as well--it's the byte we wish to transmit.
|
263 |
|
|
if (r_cmd_state == 3'h0)
|
264 |
|
|
begin
|
265 |
|
|
r_cmd_state <= r_cmd_state + 3'h1;
|
266 |
|
|
ll_cmd_dat <= r_data_reg[31:24];
|
267 |
|
|
r_cmd_crc_stb <= 1'b1;
|
268 |
|
|
end else if (r_cmd_state == 3'h1)
|
269 |
|
|
begin
|
270 |
|
|
r_cmd_state <= r_cmd_state + 3'h1;
|
271 |
|
|
ll_cmd_dat <= r_data_reg[23:16];
|
272 |
|
|
r_cmd_crc_stb <= 1'b1;
|
273 |
|
|
end else if (r_cmd_state == 3'h2)
|
274 |
|
|
begin
|
275 |
|
|
r_cmd_state <= r_cmd_state + 3'h1;
|
276 |
|
|
ll_cmd_dat <= r_data_reg[15:8];
|
277 |
|
|
r_cmd_crc_stb <= 1'b1;
|
278 |
|
|
end else if (r_cmd_state == 3'h3)
|
279 |
|
|
begin
|
280 |
|
|
r_cmd_state <= r_cmd_state + 3'h1;
|
281 |
|
|
ll_cmd_dat <= r_data_reg[7:0];
|
282 |
|
|
r_cmd_crc_stb <= 1'b1;
|
283 |
|
|
end else if (r_cmd_state == 3'h4)
|
284 |
|
|
begin
|
285 |
|
|
r_cmd_state <= r_cmd_state + 3'h1;
|
286 |
|
|
ll_cmd_dat <= cmd_crc;
|
287 |
|
|
end else if (r_cmd_state == 3'h5)
|
288 |
|
|
begin
|
289 |
|
|
ll_cmd_dat <= 8'hff;
|
290 |
|
|
if (r_have_resp)
|
291 |
|
|
begin
|
292 |
|
|
if (r_use_fifo)
|
293 |
|
|
r_cmd_state <= r_cmd_state + 3'h1;
|
294 |
|
|
else
|
295 |
|
|
r_cmd_state <= r_cmd_state + 3'h2;
|
296 |
|
|
ll_fifo_rd <= (r_use_fifo)&&(r_fifo_wr);
|
297 |
|
|
if ((r_use_fifo)&&(r_fifo_wr))
|
298 |
|
|
ll_cmd_dat <= 8'hfe;
|
299 |
|
|
end
|
300 |
|
|
end else if (r_cmd_state == 3'h6)
|
301 |
|
|
begin
|
302 |
|
|
ll_cmd_dat <= 8'hff;
|
303 |
|
|
if (ll_fifo_rd_complete)
|
304 |
|
|
begin // If we've finished reading from the
|
305 |
|
|
// FIFO, then move on
|
306 |
|
|
r_cmd_state <= r_cmd_state + 3'h1;
|
307 |
|
|
ll_fifo_rd <= 1'b0;
|
308 |
|
|
end else if (ll_fifo_rd)
|
309 |
|
|
ll_cmd_dat <= fifo_byte;
|
310 |
|
|
end else // if (r_cmd_state == 7)
|
311 |
|
|
ll_cmd_dat <= 8'hff;
|
312 |
|
|
|
313 |
|
|
|
314 |
|
|
// Here we handle the receive portion of the interface.
|
315 |
|
|
// Note that the IF begins with an if of ll_out_stb.
|
316 |
|
|
// That is, if a byte is ready from the lower level.
|
317 |
|
|
//
|
318 |
|
|
// Here we depend upon r_cmd_resp, the response we are
|
319 |
|
|
// expecting from the SDCard, and r_rsp_state, the
|
320 |
|
|
// state machine for where we are at receive what we
|
321 |
|
|
// are expecting.
|
322 |
|
|
if (pre_rsp_state)
|
323 |
|
|
begin
|
324 |
3 |
dgisselq |
case(r_rsp_state)
|
325 |
|
|
`SDSPI_RSP_NONE: begin // Waiting on R1
|
326 |
|
|
if (!ll_out_dat[7])
|
327 |
2 |
dgisselq |
begin
|
328 |
|
|
r_last_r_one <= ll_out_dat;
|
329 |
|
|
if (r_cmd_resp == `SDSPI_EXPECT_R1)
|
330 |
|
|
begin // Expecting R1 alone
|
331 |
|
|
r_have_resp <= 1'b1;
|
332 |
|
|
ll_cmd_stb <= (r_use_fifo);
|
333 |
|
|
r_data_reg <= 32'hffffffff;
|
334 |
3 |
dgisselq |
ll_fifo_wr<=(r_use_fifo)&&(!r_fifo_wr);
|
335 |
2 |
dgisselq |
end else if (r_cmd_resp == `SDSPI_EXPECT_R1B)
|
336 |
|
|
begin // Go wait on R1b
|
337 |
|
|
r_data_reg <= 32'hffffffff;
|
338 |
|
|
end // else wait on 32-bit rsp
|
339 |
3 |
dgisselq |
end end
|
340 |
|
|
`SDSPI_RSP_BSYWAIT: begin
|
341 |
|
|
// Waiting on R1b, have R1
|
342 |
2 |
dgisselq |
if (nonzero_out)
|
343 |
|
|
r_have_resp <= 1'b1;
|
344 |
|
|
ll_cmd_stb <= (r_use_fifo);
|
345 |
3 |
dgisselq |
end
|
346 |
|
|
`SDSPI_RSP_GETWORD: begin
|
347 |
|
|
// Have R1, waiting on all of R2/R3/R7
|
348 |
|
|
r_data_reg <= { r_data_reg[23:0],
|
349 |
|
|
ll_out_dat };
|
350 |
2 |
dgisselq |
r_data_fil <= r_data_fil+2'b01;
|
351 |
|
|
if (r_data_fil == 2'b11)
|
352 |
|
|
begin
|
353 |
|
|
ll_cmd_stb <= (r_use_fifo);
|
354 |
|
|
// r_rsp_state <= 3'h3;
|
355 |
3 |
dgisselq |
end end
|
356 |
|
|
`SDSPI_RSP_WAIT_WHILE_BUSY: begin
|
357 |
|
|
// Wait while device is busy writing
|
358 |
2 |
dgisselq |
// if (nonzero_out)
|
359 |
|
|
// begin
|
360 |
|
|
// r_data_reg[31:8] <= 24'h00;
|
361 |
|
|
// r_data_reg[7:0] <= ll_out_dat;
|
362 |
|
|
// // r_rsp_state <= 3'h6;
|
363 |
|
|
// end
|
364 |
3 |
dgisselq |
end
|
365 |
|
|
`SDSPI_RSP_RDCOMPLETE: begin
|
366 |
|
|
// Block write command has completed
|
367 |
2 |
dgisselq |
ll_cmd_stb <= 1'b0;
|
368 |
3 |
dgisselq |
end
|
369 |
|
|
`SDSPI_RSP_WRITING: begin
|
370 |
|
|
// We are reading from the device into
|
371 |
2 |
dgisselq |
// our FIFO
|
372 |
|
|
if ((ll_fifo_wr_complete)
|
373 |
|
|
// Or ... we receive an error
|
374 |
3 |
dgisselq |
||(r_read_err_token[0]))
|
375 |
2 |
dgisselq |
begin
|
376 |
|
|
ll_fifo_wr <= 1'b0;
|
377 |
|
|
ll_cmd_stb <= 1'b0;
|
378 |
3 |
dgisselq |
end end
|
379 |
|
|
// `SDSPI_RSP_GETTOKEN:
|
380 |
|
|
default: begin end
|
381 |
|
|
endcase
|
382 |
2 |
dgisselq |
end
|
383 |
|
|
|
384 |
3 |
dgisselq |
if ((r_use_fifo)&&(ll_out_stb))
|
385 |
|
|
r_data_reg <= { 26'h3ffffff, r_data_response_token, r_read_err_token };
|
386 |
|
|
|
387 |
2 |
dgisselq |
if (r_watchdog_err)
|
388 |
|
|
ll_cmd_stb <= 1'b0;
|
389 |
3 |
dgisselq |
r_cmd_err<= (r_cmd_err)|(fifo_crc_err)|(r_watchdog_err)
|
390 |
|
|
|(r_err_token);
|
391 |
2 |
dgisselq |
end else if (r_cmd_busy)
|
392 |
|
|
begin
|
393 |
3 |
dgisselq |
r_cmd_busy <= (ll_cmd_stb)||(!ll_idle);
|
394 |
|
|
end else if (new_cmd)
|
395 |
2 |
dgisselq |
begin // Command write
|
396 |
|
|
// Clear the error on any write, whether a commanding
|
397 |
|
|
// one or not. -- provided the user requests clearing
|
398 |
|
|
// it (by setting the bit high)
|
399 |
3 |
dgisselq |
r_cmd_err <= (r_cmd_err)&&(!wb_data[15]);
|
400 |
2 |
dgisselq |
// In a similar fashion, we can switch fifos even if
|
401 |
|
|
// not in the middle of a command
|
402 |
3 |
dgisselq |
r_fifo_id <= wb_data[12];
|
403 |
2 |
dgisselq |
//
|
404 |
|
|
// Doesn't matter what this is set to as long as we
|
405 |
|
|
// aren't busy, so we can set it irrelevantly here.
|
406 |
3 |
dgisselq |
ll_cmd_dat <= wb_data[7:0];
|
407 |
2 |
dgisselq |
//
|
408 |
|
|
// Note that we only issue a write upon receiving a
|
409 |
|
|
// valid command. Such a command is 8 bits, and must
|
410 |
|
|
// start with its high order bits set to zero and one.
|
411 |
|
|
// Hence ... we test for that here.
|
412 |
3 |
dgisselq |
if (wb_data[7:6] == 2'b01)
|
413 |
2 |
dgisselq |
begin // Issue a command
|
414 |
|
|
//
|
415 |
|
|
r_cmd_busy <= 1'b1;
|
416 |
|
|
//
|
417 |
|
|
ll_cmd_stb <= 1'b1;
|
418 |
3 |
dgisselq |
r_cmd_resp <= wb_data[9:8];
|
419 |
2 |
dgisselq |
//
|
420 |
|
|
r_cmd_crc_stb <= 1'b1;
|
421 |
|
|
//
|
422 |
3 |
dgisselq |
r_fifo_wr <= wb_data[10];
|
423 |
|
|
r_use_fifo <= wb_data[11];
|
424 |
2 |
dgisselq |
//
|
425 |
3 |
dgisselq |
end else if (wb_data[7])
|
426 |
2 |
dgisselq |
// If, on the other hand, the command was invalid,
|
427 |
|
|
// then it must have been an attempt to read our
|
428 |
|
|
// internal configuration. So we'll place that on
|
429 |
|
|
// our data register.
|
430 |
|
|
r_data_reg <= { 8'h00,
|
431 |
|
|
4'h0, max_lgblklen,
|
432 |
|
|
4'h0, r_lgblklen, 1'b0, r_sdspi_clk };
|
433 |
3 |
dgisselq |
end else if (new_data) // Data write
|
434 |
|
|
r_data_reg <= wb_data;
|
435 |
2 |
dgisselq |
end
|
436 |
|
|
|
437 |
|
|
always @(posedge i_clk)
|
438 |
|
|
pre_cmd_state <= (ll_cmd_stb)&&(ll_idle);
|
439 |
|
|
|
440 |
|
|
reg ready_for_response_token;
|
441 |
|
|
always @(posedge i_clk)
|
442 |
3 |
dgisselq |
if (!r_cmd_busy)
|
443 |
2 |
dgisselq |
ready_for_response_token <= 1'b0;
|
444 |
|
|
else if (ll_fifo_rd)
|
445 |
|
|
ready_for_response_token <= 1'b1;
|
446 |
|
|
always @(posedge i_clk)
|
447 |
3 |
dgisselq |
if (!r_cmd_busy)
|
448 |
2 |
dgisselq |
r_have_data_response_token <= 1'b0;
|
449 |
3 |
dgisselq |
else if ((ll_out_stb)&&(ready_for_response_token)&&(!ll_out_dat[4]))
|
450 |
2 |
dgisselq |
r_have_data_response_token <= 1'b1;
|
451 |
|
|
|
452 |
|
|
reg [2:0] second_rsp_state;
|
453 |
|
|
always @(posedge i_clk)
|
454 |
|
|
if((r_cmd_resp == `SDSPI_EXPECT_R1)&&(r_use_fifo)&&(r_fifo_wr))
|
455 |
|
|
second_rsp_state <= `SDSPI_RSP_GETTOKEN;
|
456 |
|
|
else if (r_cmd_resp == `SDSPI_EXPECT_R1)
|
457 |
|
|
second_rsp_state <= `SDSPI_RSP_WRITING;
|
458 |
|
|
else if (r_cmd_resp == `SDSPI_EXPECT_R1B)
|
459 |
|
|
second_rsp_state <= `SDSPI_RSP_BSYWAIT;
|
460 |
|
|
else
|
461 |
|
|
second_rsp_state <= `SDSPI_RSP_GETWORD;
|
462 |
|
|
|
463 |
|
|
reg pre_rsp_state, nonzero_out;
|
464 |
|
|
always @(posedge i_clk)
|
465 |
|
|
if (ll_out_stb)
|
466 |
|
|
nonzero_out <= (|ll_out_dat);
|
467 |
|
|
always @(posedge i_clk)
|
468 |
|
|
pre_rsp_state <= (ll_out_stb)&&(r_cmd_sent);
|
469 |
|
|
|
470 |
|
|
// Each bit depends upon 8 bits of input
|
471 |
|
|
initial r_rsp_state = 3'h0;
|
472 |
|
|
always @(posedge i_clk)
|
473 |
3 |
dgisselq |
if (!r_cmd_sent)
|
474 |
2 |
dgisselq |
r_rsp_state <= 3'h0;
|
475 |
|
|
else if (pre_rsp_state)
|
476 |
|
|
begin
|
477 |
3 |
dgisselq |
if ((r_rsp_state == `SDSPI_RSP_NONE)&&(!ll_out_dat[7]))
|
478 |
2 |
dgisselq |
begin
|
479 |
|
|
r_rsp_state <= second_rsp_state;
|
480 |
|
|
end else if (r_rsp_state == `SDSPI_RSP_BSYWAIT)
|
481 |
|
|
begin // Waiting on R1b, have R1
|
482 |
|
|
// R1b never uses the FIFO
|
483 |
|
|
if (nonzero_out)
|
484 |
|
|
r_rsp_state <= 3'h6;
|
485 |
|
|
end else if (r_rsp_state == `SDSPI_RSP_GETWORD)
|
486 |
|
|
begin // Have R1, waiting on all of R2/R3/R7
|
487 |
|
|
if (r_data_fil == 2'b11)
|
488 |
|
|
r_rsp_state <= `SDSPI_RSP_RDCOMPLETE;
|
489 |
|
|
end else if (r_rsp_state == `SDSPI_RSP_GETTOKEN)
|
490 |
|
|
begin // Wait on data token response
|
491 |
|
|
if (r_have_data_response_token)
|
492 |
|
|
r_rsp_state <= `SDSPI_RSP_WAIT_WHILE_BUSY;
|
493 |
|
|
end else if (r_rsp_state == `SDSPI_RSP_WAIT_WHILE_BUSY)
|
494 |
|
|
begin // Wait while device is busy writing
|
495 |
|
|
if (nonzero_out)
|
496 |
|
|
r_rsp_state <= `SDSPI_RSP_RDCOMPLETE;
|
497 |
|
|
end
|
498 |
|
|
//else if (r_rsp_state == 3'h6)
|
499 |
|
|
//begin // Block write command has completed
|
500 |
|
|
// // ll_cmd_stb <= 1'b0;
|
501 |
|
|
// end else if (r_rsp_state == 3'h7)
|
502 |
|
|
// begin // We are reading from the device into
|
503 |
|
|
// // our FIFO
|
504 |
|
|
// end
|
505 |
|
|
end
|
506 |
|
|
|
507 |
|
|
always @(posedge i_clk)
|
508 |
|
|
r_cmd_sent <= (ll_cmd_stb)&&(r_cmd_state >= 3'h5);
|
509 |
|
|
|
510 |
|
|
// initial r_sdspi_clk = 6'h3c;
|
511 |
|
|
initial r_sdspi_clk = 7'h63;
|
512 |
|
|
always @(posedge i_clk)
|
513 |
|
|
begin
|
514 |
|
|
// Update our internal configuration parameters, unconnected
|
515 |
|
|
// with the card. These include the speed of the interface,
|
516 |
|
|
// and the size of the block length to expect as part of a FIFO
|
517 |
|
|
// command.
|
518 |
3 |
dgisselq |
if ((new_cmd)&&(wb_data[7:6]==2'b11)&&(!r_data_reg[7])
|
519 |
2 |
dgisselq |
&&(r_data_reg[15:12]==4'h00))
|
520 |
|
|
begin
|
521 |
|
|
if (|r_data_reg[6:0])
|
522 |
|
|
r_sdspi_clk <= r_data_reg[6:0];
|
523 |
|
|
if (|r_data_reg[11:8])
|
524 |
|
|
r_lgblklen <= r_data_reg[11:8];
|
525 |
|
|
end if (r_lgblklen > max_lgblklen)
|
526 |
|
|
r_lgblklen <= max_lgblklen;
|
527 |
|
|
end
|
528 |
|
|
|
529 |
|
|
assign need_reset = 1'b0;
|
530 |
|
|
always @(posedge i_clk)
|
531 |
3 |
dgisselq |
case(wb_addr)
|
532 |
2 |
dgisselq |
`SDSPI_CMD_ADDRESS:
|
533 |
|
|
o_wb_data <= { need_reset, 11'h00,
|
534 |
3 |
dgisselq |
2'h0, r_err_token, fifo_crc_err,
|
535 |
2 |
dgisselq |
r_cmd_err, r_cmd_busy, 1'b0, r_fifo_id,
|
536 |
|
|
r_use_fifo, r_fifo_wr, r_cmd_resp,
|
537 |
|
|
r_last_r_one };
|
538 |
|
|
`SDSPI_DAT_ADDRESS:
|
539 |
|
|
o_wb_data <= r_data_reg;
|
540 |
|
|
`SDSPI_FIFO_A_ADDR:
|
541 |
|
|
o_wb_data <= fifo_a_reg;
|
542 |
|
|
`SDSPI_FIFO_B_ADDR:
|
543 |
|
|
o_wb_data <= fifo_b_reg;
|
544 |
|
|
endcase
|
545 |
|
|
|
546 |
|
|
always @(posedge i_clk)
|
547 |
|
|
o_wb_ack <= wb_stb;
|
548 |
|
|
|
549 |
|
|
initial q_busy = 1'b1;
|
550 |
|
|
always @(posedge i_clk)
|
551 |
|
|
q_busy <= r_cmd_busy;
|
552 |
|
|
always @(posedge i_clk)
|
553 |
3 |
dgisselq |
o_int <= (!r_cmd_busy)&&(q_busy);
|
554 |
2 |
dgisselq |
|
555 |
|
|
assign o_wb_stall = 1'b0;
|
556 |
|
|
|
557 |
|
|
//
|
558 |
|
|
// Let's work with our FIFO memory here ...
|
559 |
|
|
//
|
560 |
|
|
//
|
561 |
|
|
always @(posedge i_clk)
|
562 |
|
|
begin
|
563 |
3 |
dgisselq |
if ((write_stb)&&(wb_addr == `SDSPI_CMD_ADDRESS))
|
564 |
2 |
dgisselq |
begin // Command write
|
565 |
|
|
// Clear the read/write address
|
566 |
|
|
fifo_wb_addr <= {(LGFIFOLN){1'b0}};
|
567 |
3 |
dgisselq |
end else if ((wb_stb)&&(wb_addr[1]))
|
568 |
2 |
dgisselq |
begin // On read or write, of either FIFO,
|
569 |
|
|
// we increase our pointer
|
570 |
|
|
fifo_wb_addr <= fifo_wb_addr + 1;
|
571 |
|
|
// And let ourselves know we need to update ourselves
|
572 |
|
|
// on the next clock
|
573 |
|
|
end
|
574 |
|
|
end
|
575 |
|
|
|
576 |
|
|
// Prepare reading of the FIFO for the WB bus read
|
577 |
|
|
// Memory read #1
|
578 |
|
|
always @(posedge i_clk)
|
579 |
|
|
begin
|
580 |
|
|
fifo_a_reg <= {
|
581 |
3 |
dgisselq |
fifo_a_mem_0[ fifo_wb_addr ],
|
582 |
|
|
fifo_a_mem_1[ fifo_wb_addr ],
|
583 |
|
|
fifo_a_mem_2[ fifo_wb_addr ],
|
584 |
|
|
fifo_a_mem_3[ fifo_wb_addr ] };
|
585 |
2 |
dgisselq |
fifo_b_reg <= {
|
586 |
3 |
dgisselq |
fifo_b_mem_0[ fifo_wb_addr ],
|
587 |
|
|
fifo_b_mem_1[ fifo_wb_addr ],
|
588 |
|
|
fifo_b_mem_2[ fifo_wb_addr ],
|
589 |
|
|
fifo_b_mem_3[ fifo_wb_addr ] };
|
590 |
2 |
dgisselq |
end
|
591 |
|
|
|
592 |
|
|
// Okay, now ... writing our FIFO ...
|
593 |
|
|
reg pre_fifo_addr_inc_rd;
|
594 |
|
|
reg pre_fifo_addr_inc_wr;
|
595 |
|
|
initial pre_fifo_addr_inc_rd = 1'b0;
|
596 |
|
|
initial pre_fifo_addr_inc_wr = 1'b0;
|
597 |
|
|
always @(posedge i_clk)
|
598 |
3 |
dgisselq |
pre_fifo_addr_inc_wr <= ((ll_fifo_wr)&&(ll_out_stb)
|
599 |
|
|
&&(r_have_start_token));
|
600 |
2 |
dgisselq |
always @(posedge i_clk)
|
601 |
3 |
dgisselq |
pre_fifo_addr_inc_rd <= ((ll_fifo_rd)&&(ll_cmd_stb)&&(ll_idle));
|
602 |
2 |
dgisselq |
always @(posedge i_clk)
|
603 |
|
|
begin
|
604 |
3 |
dgisselq |
if (!r_cmd_busy)
|
605 |
2 |
dgisselq |
ll_fifo_addr <= {(LGFIFOLN+2){1'b0}};
|
606 |
|
|
else if ((pre_fifo_addr_inc_wr)||(pre_fifo_addr_inc_rd))
|
607 |
|
|
ll_fifo_addr <= ll_fifo_addr + 1;
|
608 |
|
|
end
|
609 |
|
|
|
610 |
3 |
dgisselq |
//
|
611 |
|
|
// Look for that start token. This will be present when reading from
|
612 |
|
|
// the device into the FIFO.
|
613 |
|
|
//
|
614 |
2 |
dgisselq |
always @(posedge i_clk)
|
615 |
3 |
dgisselq |
if (!r_cmd_busy)
|
616 |
2 |
dgisselq |
r_have_start_token <= 1'b0;
|
617 |
|
|
else if ((ll_fifo_wr)&&(ll_out_stb)&&(ll_out_dat==8'hfe))
|
618 |
|
|
r_have_start_token <= 1'b1;
|
619 |
3 |
dgisselq |
always @(posedge i_clk)
|
620 |
|
|
if (!r_cmd_busy)
|
621 |
|
|
r_read_err_token <= 4'h0;
|
622 |
|
|
else if ((ll_fifo_wr)&&(ll_out_stb)&&(!r_have_start_token)
|
623 |
|
|
&&(ll_out_dat[7:4]==4'h0))
|
624 |
|
|
r_read_err_token <= ll_out_dat[3:0];
|
625 |
|
|
always @(posedge i_clk) // Look for a response to our writing
|
626 |
|
|
if (!r_cmd_busy)
|
627 |
|
|
r_data_response_token <= 2'b00;
|
628 |
|
|
else if ((ready_for_response_token)
|
629 |
|
|
&&(!ll_out_dat[4])&&(ll_out_dat[0]))
|
630 |
|
|
r_data_response_token <= ll_out_dat[3:2];
|
631 |
|
|
initial r_err_token = 1'b0;
|
632 |
|
|
always @(posedge i_clk)
|
633 |
|
|
if (ll_fifo_rd)
|
634 |
|
|
r_err_token <= (r_err_token)|(r_read_err_token[0]);
|
635 |
|
|
else if (ll_fifo_wr)
|
636 |
|
|
r_err_token <= (r_err_token)|
|
637 |
|
|
((|r_data_response_token)&&(r_data_response_token[1]));
|
638 |
|
|
else if (cmd_stb)
|
639 |
|
|
// Clear the error on any write with the bit high
|
640 |
|
|
r_err_token <= (r_err_token)&&(!i_wb_data[16])
|
641 |
|
|
&&(!i_wb_data[15]);
|
642 |
2 |
dgisselq |
|
643 |
|
|
reg last_fifo_byte;
|
644 |
|
|
initial last_fifo_byte = 1'b0;
|
645 |
|
|
always @(posedge i_clk)
|
646 |
|
|
if (ll_fifo_wr)
|
647 |
|
|
last_fifo_byte <= (ll_fifo_addr == w_blklimit);
|
648 |
|
|
else
|
649 |
|
|
last_fifo_byte <= 1'b0;
|
650 |
|
|
|
651 |
|
|
// This is the one (and only allowed) write to the FIFO memory always
|
652 |
|
|
// block.
|
653 |
|
|
//
|
654 |
|
|
// If ll_fifo_wr is true, we'll be writing to the FIFO, and we'll do
|
655 |
|
|
// that here. This is different from r_fifo_wr, which specifies that
|
656 |
|
|
// we will be writing to the SDCard from the FIFO, and hence READING
|
657 |
|
|
// from the FIFO.
|
658 |
|
|
//
|
659 |
|
|
reg pre_fifo_a_wr, pre_fifo_b_wr, pre_fifo_crc_a, pre_fifo_crc_b,
|
660 |
|
|
clear_fifo_crc;
|
661 |
|
|
always @(posedge i_clk)
|
662 |
|
|
begin
|
663 |
3 |
dgisselq |
pre_fifo_a_wr <= (ll_fifo_wr)&&(ll_out_stb)
|
664 |
|
|
&&(!r_fifo_id)&&(ll_fifo_wr_state == 2'b00);
|
665 |
|
|
pre_fifo_b_wr <= (ll_fifo_wr)&&(ll_out_stb)
|
666 |
|
|
&&( r_fifo_id)&&(ll_fifo_wr_state == 2'b00);
|
667 |
|
|
fifo_wr_crc_stb <= (ll_fifo_wr)&&(ll_out_stb)
|
668 |
|
|
&&(ll_fifo_wr_state == 2'b00)&&(r_have_start_token);
|
669 |
|
|
pre_fifo_crc_a<= (ll_fifo_wr)&&(ll_out_stb)
|
670 |
|
|
&&(ll_fifo_wr_state == 2'b01);
|
671 |
|
|
pre_fifo_crc_b<= (ll_fifo_wr)&&(ll_out_stb)
|
672 |
|
|
&&(ll_fifo_wr_state == 2'b10);
|
673 |
|
|
clear_fifo_crc <= (new_cmd)&&(wb_data[15]);
|
674 |
2 |
dgisselq |
end
|
675 |
|
|
|
676 |
3 |
dgisselq |
reg fifo_a_wr, fifo_b_wr;
|
677 |
|
|
reg [3:0] fifo_a_wr_mask, fifo_b_wr_mask;
|
678 |
|
|
reg [(LGFIFOLN-1):0] fifo_a_wr_addr, fifo_b_wr_addr;
|
679 |
|
|
reg [31:0] fifo_a_wr_data, fifo_b_wr_data;
|
680 |
|
|
|
681 |
2 |
dgisselq |
initial fifo_crc_err = 1'b0;
|
682 |
|
|
always @(posedge i_clk)
|
683 |
|
|
begin // One and only memory write allowed
|
684 |
3 |
dgisselq |
fifo_a_wr <= 1'b0;
|
685 |
|
|
fifo_a_wr_data <= { ll_out_dat, ll_out_dat, ll_out_dat, ll_out_dat };
|
686 |
|
|
if ((write_stb)&&(wb_addr[1:0]==2'b10))
|
687 |
|
|
begin
|
688 |
|
|
fifo_a_wr <= 1'b1;
|
689 |
|
|
fifo_a_wr_mask <= 4'b1111;
|
690 |
|
|
fifo_a_wr_addr <= fifo_wb_addr;
|
691 |
|
|
fifo_a_wr_data <= wb_data;
|
692 |
|
|
end else if (pre_fifo_a_wr)
|
693 |
|
|
begin
|
694 |
|
|
fifo_a_wr <= 1'b1;
|
695 |
|
|
fifo_a_wr_addr <= ll_fifo_addr[(LGFIFOLN+1):2];
|
696 |
|
|
case(ll_fifo_addr[1:0])
|
697 |
|
|
2'b00: fifo_a_wr_mask <= 4'b0001;
|
698 |
|
|
2'b01: fifo_a_wr_mask <= 4'b0010;
|
699 |
|
|
2'b10: fifo_a_wr_mask <= 4'b0100;
|
700 |
|
|
2'b11: fifo_a_wr_mask <= 4'b1000;
|
701 |
|
|
endcase
|
702 |
|
|
end
|
703 |
2 |
dgisselq |
|
704 |
3 |
dgisselq |
if ((fifo_a_wr)&&(fifo_a_wr_mask[0]))
|
705 |
|
|
fifo_a_mem_0[fifo_a_wr_addr] <= fifo_a_wr_data[7:0];
|
706 |
|
|
if ((fifo_a_wr)&&(fifo_a_wr_mask[1]))
|
707 |
|
|
fifo_a_mem_1[fifo_a_wr_addr] <= fifo_a_wr_data[15:8];
|
708 |
|
|
if ((fifo_a_wr)&&(fifo_a_wr_mask[2]))
|
709 |
|
|
fifo_a_mem_2[fifo_a_wr_addr] <= fifo_a_wr_data[23:16];
|
710 |
|
|
if ((fifo_a_wr)&&(fifo_a_wr_mask[3]))
|
711 |
|
|
fifo_a_mem_3[fifo_a_wr_addr] <= fifo_a_wr_data[31:24];
|
712 |
2 |
dgisselq |
|
713 |
3 |
dgisselq |
fifo_b_wr <= 1'b0;
|
714 |
|
|
fifo_b_wr_data <= { ll_out_dat, ll_out_dat, ll_out_dat, ll_out_dat };
|
715 |
|
|
if ((write_stb)&&(wb_addr[1:0]==2'b11))
|
716 |
|
|
begin
|
717 |
|
|
fifo_b_wr <= 1'b1;
|
718 |
|
|
fifo_b_wr_mask <= 4'b1111;
|
719 |
|
|
fifo_b_wr_addr <= fifo_wb_addr;
|
720 |
|
|
fifo_b_wr_data <= wb_data;
|
721 |
|
|
end else if (pre_fifo_b_wr)
|
722 |
|
|
begin
|
723 |
|
|
fifo_b_wr <= 1'b1;
|
724 |
|
|
fifo_b_wr_addr <= ll_fifo_addr[(LGFIFOLN+1):2];
|
725 |
|
|
case(ll_fifo_addr[1:0])
|
726 |
|
|
2'b00: fifo_b_wr_mask <= 4'b0001;
|
727 |
|
|
2'b01: fifo_b_wr_mask <= 4'b0010;
|
728 |
|
|
2'b10: fifo_b_wr_mask <= 4'b0100;
|
729 |
|
|
2'b11: fifo_b_wr_mask <= 4'b1000;
|
730 |
|
|
endcase
|
731 |
|
|
end
|
732 |
|
|
|
733 |
|
|
if ((fifo_b_wr)&&(fifo_b_wr_mask[0]))
|
734 |
|
|
fifo_b_mem_0[fifo_b_wr_addr] <= fifo_b_wr_data[7:0];
|
735 |
|
|
if ((fifo_b_wr)&&(fifo_b_wr_mask[1]))
|
736 |
|
|
fifo_b_mem_1[fifo_b_wr_addr] <= fifo_b_wr_data[15:8];
|
737 |
|
|
if ((fifo_b_wr)&&(fifo_b_wr_mask[2]))
|
738 |
|
|
fifo_b_mem_2[fifo_b_wr_addr] <= fifo_b_wr_data[23:16];
|
739 |
|
|
if ((fifo_b_wr)&&(fifo_b_wr_mask[3]))
|
740 |
|
|
fifo_b_mem_3[fifo_b_wr_addr] <= fifo_b_wr_data[31:24];
|
741 |
|
|
|
742 |
|
|
if (!r_cmd_busy)
|
743 |
2 |
dgisselq |
ll_fifo_wr_complete <= 1'b0;
|
744 |
|
|
|
745 |
3 |
dgisselq |
if (!r_cmd_busy)
|
746 |
2 |
dgisselq |
ll_fifo_wr_state <= 2'b00;
|
747 |
|
|
else if ((pre_fifo_a_wr)||(pre_fifo_b_wr))
|
748 |
|
|
ll_fifo_wr_state <= (last_fifo_byte)? 2'b01:2'b00;
|
749 |
|
|
|
750 |
|
|
if (pre_fifo_crc_a)
|
751 |
|
|
begin
|
752 |
|
|
fifo_crc_err <= fifo_crc_err | (fifo_wr_crc_reg[15:8]!=ll_out_dat);
|
753 |
|
|
ll_fifo_wr_state <= ll_fifo_wr_state + 2'b01;
|
754 |
|
|
end if (pre_fifo_crc_b)
|
755 |
|
|
begin
|
756 |
|
|
fifo_crc_err <= fifo_crc_err | (fifo_wr_crc_reg[7:0]!=ll_out_dat);
|
757 |
|
|
ll_fifo_wr_state <= ll_fifo_wr_state + 2'b01;
|
758 |
|
|
ll_fifo_wr_complete <= 1'b1;
|
759 |
|
|
end else if (clear_fifo_crc)
|
760 |
|
|
fifo_crc_err <= 1'b0;
|
761 |
|
|
end
|
762 |
|
|
|
763 |
|
|
always @(posedge i_clk)
|
764 |
|
|
begin // Second memory read, this time for the FIFO
|
765 |
3 |
dgisselq |
case(ll_fifo_addr[1:0])
|
766 |
|
|
2'b00: begin
|
767 |
|
|
fifo_a_byte<=fifo_a_mem_0[ll_fifo_addr[(LGFIFOLN+1):2]];
|
768 |
|
|
fifo_b_byte<=fifo_b_mem_0[ll_fifo_addr[(LGFIFOLN+1):2]];
|
769 |
|
|
end
|
770 |
|
|
2'b01: begin
|
771 |
|
|
fifo_a_byte<=fifo_a_mem_1[ll_fifo_addr[(LGFIFOLN+1):2]];
|
772 |
|
|
fifo_b_byte<=fifo_b_mem_1[ll_fifo_addr[(LGFIFOLN+1):2]];
|
773 |
|
|
end
|
774 |
|
|
2'b10: begin
|
775 |
|
|
fifo_a_byte<=fifo_a_mem_2[ll_fifo_addr[(LGFIFOLN+1):2]];
|
776 |
|
|
fifo_b_byte<=fifo_b_mem_2[ll_fifo_addr[(LGFIFOLN+1):2]];
|
777 |
|
|
end
|
778 |
|
|
2'b11: begin
|
779 |
|
|
fifo_a_byte<=fifo_a_mem_3[ll_fifo_addr[(LGFIFOLN+1):2]];
|
780 |
|
|
fifo_b_byte<=fifo_b_mem_3[ll_fifo_addr[(LGFIFOLN+1):2]];
|
781 |
|
|
end
|
782 |
|
|
endcase
|
783 |
2 |
dgisselq |
end
|
784 |
|
|
|
785 |
|
|
reg [(LGFIFOLN-1):0] r_blklimit;
|
786 |
|
|
wire [(LGFIFOLN+1):0] w_blklimit;
|
787 |
|
|
always @(posedge i_clk)
|
788 |
3 |
dgisselq |
r_blklimit[(LGFIFOLN-1):0] <= (1<<r_lgblklen)-1;
|
789 |
2 |
dgisselq |
assign w_blklimit = { r_blklimit, 2'b11 };
|
790 |
|
|
|
791 |
|
|
// Package the FIFO reads up into a packet
|
792 |
|
|
always @(posedge i_clk)
|
793 |
|
|
begin
|
794 |
|
|
fifo_rd_crc_stb <= 1'b0;
|
795 |
|
|
if (r_cmd_busy)
|
796 |
|
|
begin
|
797 |
|
|
if (ll_fifo_pkt_state[2:0] == 3'b000)
|
798 |
|
|
begin
|
799 |
|
|
if((ll_fifo_rd)&&(ll_cmd_stb)&&(ll_idle))
|
800 |
|
|
begin
|
801 |
|
|
ll_fifo_pkt_state <= ll_fifo_pkt_state + 3'b001;
|
802 |
|
|
end
|
803 |
|
|
end else if (ll_fifo_pkt_state[2:0] == 3'b001)
|
804 |
|
|
begin
|
805 |
|
|
if((ll_fifo_rd)&&(ll_cmd_stb)&&(ll_idle))
|
806 |
|
|
begin
|
807 |
|
|
ll_fifo_pkt_state <= ll_fifo_pkt_state + 3'b001;
|
808 |
|
|
fifo_byte <= (r_fifo_id)
|
809 |
|
|
? fifo_b_byte : fifo_a_byte;
|
810 |
|
|
fifo_rd_crc_stb <= 1'b1;
|
811 |
|
|
end
|
812 |
|
|
end else if (ll_fifo_pkt_state[2:0] == 3'b010)
|
813 |
|
|
begin
|
814 |
|
|
if((ll_fifo_rd)&&(ll_cmd_stb)&&(ll_idle))
|
815 |
|
|
begin
|
816 |
|
|
fifo_byte <= (r_fifo_id)
|
817 |
|
|
? fifo_b_byte : fifo_a_byte;
|
818 |
|
|
fifo_rd_crc_stb <= 1'b1;
|
819 |
|
|
end
|
820 |
|
|
if (ll_fifo_addr == 0)
|
821 |
|
|
ll_fifo_pkt_state <= 3'b011;
|
822 |
|
|
end else if (ll_fifo_pkt_state == 3'b011)
|
823 |
|
|
begin // 1st CRC byte
|
824 |
|
|
if((ll_fifo_rd)&&(ll_cmd_stb)&&(ll_idle))
|
825 |
|
|
begin
|
826 |
|
|
fifo_byte <= fifo_rd_crc_reg[15:8];
|
827 |
|
|
ll_fifo_pkt_state <= 3'b100;
|
828 |
|
|
end
|
829 |
|
|
end else if (ll_fifo_pkt_state == 3'b100)
|
830 |
|
|
begin // 2nd CRC byte
|
831 |
|
|
if((ll_fifo_rd)&&(ll_cmd_stb)&&(ll_idle))
|
832 |
|
|
begin
|
833 |
|
|
fifo_byte <= fifo_rd_crc_reg[7:0];
|
834 |
|
|
ll_fifo_pkt_state <= 3'b101;
|
835 |
|
|
end
|
836 |
|
|
end else if((ll_fifo_rd)&&(ll_cmd_stb)&&(ll_idle))
|
837 |
|
|
begin
|
838 |
|
|
// Idle the channel
|
839 |
|
|
ll_fifo_rd_complete <= 1'b1;
|
840 |
|
|
fifo_byte <= 8'hff;
|
841 |
|
|
end
|
842 |
3 |
dgisselq |
end else if ((write_stb)&&(wb_addr == `SDSPI_CMD_ADDRESS))
|
843 |
2 |
dgisselq |
begin
|
844 |
|
|
ll_fifo_pkt_state <= 3'h0;
|
845 |
|
|
ll_fifo_rd_complete <= 1'b0;
|
846 |
3 |
dgisselq |
fifo_byte <= (wb_data[12]) ? fifo_b_byte : fifo_a_byte;
|
847 |
2 |
dgisselq |
fifo_rd_crc_stb <= 1'b1;
|
848 |
|
|
end else begin // Packet state is IDLE (clear the CRC registers)
|
849 |
|
|
ll_fifo_pkt_state <= 3'b111;
|
850 |
|
|
ll_fifo_rd_complete <= 1'b1;
|
851 |
|
|
end
|
852 |
|
|
end
|
853 |
|
|
|
854 |
|
|
always @(posedge i_clk)
|
855 |
|
|
begin
|
856 |
3 |
dgisselq |
if (!ll_fifo_wr)
|
857 |
2 |
dgisselq |
fifo_wr_crc_reg <= 16'h00;
|
858 |
|
|
else if (fifo_wr_crc_stb)
|
859 |
|
|
begin
|
860 |
|
|
fifo_wr_crc_reg[15:8] <=fifo_wr_crc_reg[15:8]^ll_out_dat;
|
861 |
|
|
fifo_wr_crc_count <= 4'h8;
|
862 |
|
|
end else if (|fifo_wr_crc_count)
|
863 |
|
|
begin
|
864 |
|
|
fifo_wr_crc_count <= fifo_wr_crc_count - 4'h1;
|
865 |
|
|
if (fifo_wr_crc_reg[15])
|
866 |
|
|
fifo_wr_crc_reg <= { fifo_wr_crc_reg[14:0], 1'b0 }
|
867 |
|
|
^ 16'h1021;
|
868 |
|
|
else
|
869 |
|
|
fifo_wr_crc_reg <= { fifo_wr_crc_reg[14:0], 1'b0 };
|
870 |
|
|
end
|
871 |
|
|
end
|
872 |
|
|
|
873 |
|
|
always @(posedge i_clk)
|
874 |
|
|
begin
|
875 |
3 |
dgisselq |
if (!r_cmd_busy)
|
876 |
2 |
dgisselq |
begin
|
877 |
|
|
fifo_rd_crc_reg <= 16'h00;
|
878 |
|
|
fifo_rd_crc_count <= 4'h0;
|
879 |
|
|
end else if (fifo_rd_crc_stb)
|
880 |
|
|
begin
|
881 |
|
|
fifo_rd_crc_reg[15:8] <=fifo_rd_crc_reg[15:8]^fifo_byte;
|
882 |
|
|
fifo_rd_crc_count <= 4'h8;
|
883 |
|
|
end else if (|fifo_rd_crc_count)
|
884 |
|
|
begin
|
885 |
|
|
fifo_rd_crc_count <= fifo_rd_crc_count - 4'h1;
|
886 |
|
|
if (fifo_rd_crc_reg[15])
|
887 |
|
|
fifo_rd_crc_reg <= { fifo_rd_crc_reg[14:0], 1'b0 }
|
888 |
|
|
^ 16'h1021;
|
889 |
|
|
else
|
890 |
|
|
fifo_rd_crc_reg <= { fifo_rd_crc_reg[14:0], 1'b0 };
|
891 |
|
|
end
|
892 |
|
|
end
|
893 |
|
|
|
894 |
|
|
//
|
895 |
|
|
// Calculate a CRC for the command section of our output
|
896 |
|
|
//
|
897 |
|
|
initial r_cmd_crc_ff = 1'b0;
|
898 |
|
|
always @(posedge i_clk)
|
899 |
|
|
begin
|
900 |
3 |
dgisselq |
if (!r_cmd_busy)
|
901 |
2 |
dgisselq |
begin
|
902 |
|
|
r_cmd_crc <= 8'h00;
|
903 |
|
|
r_cmd_crc_cnt <= 4'hf;
|
904 |
|
|
r_cmd_crc_ff <= 1'b0;
|
905 |
3 |
dgisselq |
end else if (!r_cmd_crc_cnt[3])
|
906 |
2 |
dgisselq |
begin
|
907 |
|
|
r_cmd_crc_cnt <= r_cmd_crc_cnt - 4'h1;
|
908 |
|
|
if (r_cmd_crc[7])
|
909 |
|
|
r_cmd_crc <= { r_cmd_crc[6:0], 1'b0 } ^ 8'h12;
|
910 |
|
|
else
|
911 |
|
|
r_cmd_crc <= { r_cmd_crc[6:0], 1'b0 };
|
912 |
|
|
r_cmd_crc_ff <= (r_cmd_crc_ff)||(r_cmd_crc_stb);
|
913 |
|
|
end else if ((r_cmd_crc_stb)||(r_cmd_crc_ff))
|
914 |
|
|
begin
|
915 |
|
|
r_cmd_crc <= r_cmd_crc ^ ll_cmd_dat;
|
916 |
|
|
r_cmd_crc_cnt <= 4'h7;
|
917 |
|
|
r_cmd_crc_ff <= 1'b0;
|
918 |
|
|
end
|
919 |
|
|
end
|
920 |
|
|
assign cmd_crc = { r_cmd_crc[7:1], 1'b1 };
|
921 |
|
|
|
922 |
|
|
//
|
923 |
|
|
// Some watchdog logic for us. This way, if we are waiting for the
|
924 |
|
|
// card to respond, and something goes wrong, we can timeout the
|
925 |
|
|
// transaction and ... figure out what to do about it later. At least
|
926 |
|
|
// we'll have an error indication.
|
927 |
|
|
//
|
928 |
|
|
initial r_watchdog = 26'h3ffffff;
|
929 |
|
|
initial r_watchdog_err = 1'b0;
|
930 |
|
|
always @(posedge i_clk)
|
931 |
3 |
dgisselq |
if (!r_cmd_busy)
|
932 |
2 |
dgisselq |
r_watchdog_err <= 1'b0;
|
933 |
|
|
else if (r_watchdog == 0)
|
934 |
|
|
r_watchdog_err <= 1'b1;
|
935 |
|
|
always @(posedge i_clk)
|
936 |
3 |
dgisselq |
if (!r_cmd_busy)
|
937 |
2 |
dgisselq |
r_watchdog <= 26'h3fffff;
|
938 |
|
|
else if (|r_watchdog)
|
939 |
|
|
r_watchdog <= r_watchdog - 26'h1;
|
940 |
|
|
|
941 |
|
|
assign o_debug = { ((ll_cmd_stb)&&(ll_idle))||(ll_out_stb),
|
942 |
|
|
ll_cmd_stb, ll_idle, ll_out_stb, // 4'h
|
943 |
|
|
o_cs_n, o_sck, o_mosi, i_miso, // 4'h
|
944 |
|
|
r_cmd_state, i_bus_grant, // 4'h
|
945 |
|
|
r_rsp_state, r_cmd_busy, // 4'h
|
946 |
|
|
ll_cmd_dat, // 8'b
|
947 |
|
|
ll_out_dat }; // 8'b
|
948 |
3 |
dgisselq |
|
949 |
|
|
// Make verilator happy
|
950 |
|
|
// verilator lint_off UNUSED
|
951 |
|
|
wire unused;
|
952 |
|
|
assign unused = i_wb_cyc;
|
953 |
|
|
// verilator lint_on UNUSED
|
954 |
2 |
dgisselq |
endmodule
|
955 |
|
|
|