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