OpenCores
URL https://opencores.org/ocsvn/sdspi/sdspi/trunk

Subversion Repositories sdspi

[/] [sdspi/] [trunk/] [rtl/] [sdspi.v] - Blame information for rev 2

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
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
// 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
        wire    wb_stb, write_stb, cmd_stb; // read_stb
100
        assign  wb_stb    = ((i_wb_cyc)&&(i_wb_stb)&&(~o_wb_stall));
101
        assign  write_stb = ((wb_stb)&&( i_wb_we));
102
        // assign       read_stb  = ((wb_stb)&&(~i_wb_we));
103
        assign  cmd_stb  = (~r_cmd_busy)&&(write_stb)
104
                                &&(i_wb_addr==`SDSPI_CMD_ADDRESS);
105
 
106
 
107
        //
108
        // Access to our lower-level SDSPI driver, the one that actually
109
        // uses/sets the SPI ports
110
        //
111
        reg     [6:0]    r_sdspi_clk;
112
        reg             ll_cmd_stb;
113
        reg     [7:0]    ll_cmd_dat;
114
        wire            ll_out_stb, ll_idle;
115
        wire    [7:0]    ll_out_dat;
116
        llsdspi lowlevel(i_clk, r_sdspi_clk, r_cmd_busy, ll_cmd_stb, ll_cmd_dat,
117
                        o_cs_n, o_sck, o_mosi, i_miso,
118
                        ll_out_stb, ll_out_dat, ll_idle,
119
                        i_bus_grant);
120
 
121
 
122
        // CRC
123
        reg             r_cmd_crc_stb;
124
        wire    [7:0]    cmd_crc;
125
        // State machine
126
        reg     [2:0]    r_cmd_state, r_rsp_state;
127
        // Current status, beyond state
128
        reg             r_have_resp, r_use_fifo, r_fifo_wr,
129
                                ll_fifo_rd_complete, ll_fifo_wr_complete,
130
                                r_fifo_id,
131
                                ll_fifo_wr, ll_fifo_rd,
132
                                r_have_data_response_token,
133
                                r_have_start_token;
134
        reg     [7:0]    fifo_byte;
135
        reg     [7:0]    r_last_r_one;
136
        //
137
        reg     [31:0]   r_data_reg;
138
        reg     [1:0]    r_data_fil, r_cmd_resp;
139
        //
140
        //
141
        wire            need_reset;
142
        reg             r_cmd_err;
143
        reg             r_cmd_sent;
144
        reg     [31:0]   fifo_a_reg, fifo_b_reg;
145
        //
146
        reg             q_busy;
147
        //
148
        reg     [7:0]    fifo_a_mem[((1<<(LGFIFOLN+2))-1):0];
149
        reg     [7:0]    fifo_b_mem[((1<<(LGFIFOLN+2))-1):0];
150
        reg     [(LGFIFOLN-1):0] fifo_wb_addr;
151
        reg     [(LGFIFOLN+1):0] rd_fifo_sd_addr;
152
        reg     [(LGFIFOLN+1):0] wr_fifo_sd_addr;
153
        //
154
        reg     [(LGFIFOLN+1):0] ll_fifo_addr;
155
        //
156
        reg             fifo_crc_err;
157
        reg     [1:0]    ll_fifo_wr_state;
158
        reg     [7:0]    fifo_a_byte, fifo_b_byte;
159
        //
160
        reg     [2:0]    ll_fifo_pkt_state;
161
        reg             fifo_rd_crc_stb, fifo_wr_crc_stb;
162
        //
163
        reg     [3:0]    fifo_rd_crc_count, fifo_wr_crc_count;
164
        reg     [15:0]   fifo_rd_crc_reg, fifo_wr_crc_reg;
165
        //
166
        reg     [3:0]    r_cmd_crc_cnt;
167
        reg     [7:0]    r_cmd_crc;
168
        //
169
        reg             r_cmd_crc_ff;
170
        //
171
        reg     [3:0]    r_lgblklen;
172
        wire    [3:0]    max_lgblklen;
173
        assign  max_lgblklen = LGFIFOLN;
174
        //
175
        reg     [25:0]   r_watchdog;
176
        reg             r_watchdog_err;
177
        reg     pre_cmd_state;
178
 
179
        initial r_cmd_busy = 1'b0;
180
        initial r_data_reg = 32'h00;
181
        initial r_last_r_one = 8'hff;
182
        initial ll_cmd_stb = 1'b0;
183
        initial ll_fifo_rd = 1'b0;
184
        initial ll_fifo_wr = 1'b0;
185
        initial r_rsp_state = 3'h0;
186
        initial r_cmd_state = 3'h0;
187
        initial r_use_fifo  = 1'b0;
188
        initial r_data_fil  = 2'b00;
189
        initial r_lgblklen  = LGFIFOLN;
190
        initial r_cmd_err   = 1'b0;
191
        always @(posedge i_clk)
192
        begin
193
                if (~ll_cmd_stb)
194
                begin
195
                        r_have_resp <= 1'b0;
196
                        ll_fifo_wr <= 1'b0;
197
                        ll_fifo_rd <= 1'b0;
198
                        // r_rsp_state <= 3'h0;
199
                        r_cmd_state <= 3'h0;
200
                        r_use_fifo  <= 1'b0;
201
                        r_data_fil <= 2'b00;
202
                        r_cmd_resp <= `SDSPI_EXPECT_R1;
203
                end
204
 
205
                r_cmd_crc_stb <= 1'b0;
206
                if (pre_cmd_state)
207
                begin // While we are actively sending data, and clocking the
208
                        // interface, do:
209
                        //
210
                        // Here we use the transmit command state, or
211
                        // r_cmd_state, to determine where we are at in this
212
                        // process, and we use (ll_cmd_stb)&&(ll_idle) to
213
                        // determine that we have sent a byte.  ll_cmd_dat is
214
                        // set here as well--it's the byte we wish to transmit.
215
                        if (r_cmd_state == 3'h0)
216
                        begin
217
                                r_cmd_state <= r_cmd_state + 3'h1;
218
                                ll_cmd_dat <= r_data_reg[31:24];
219
                                r_cmd_crc_stb <= 1'b1;
220
                        end else if (r_cmd_state == 3'h1)
221
                        begin
222
                                r_cmd_state <= r_cmd_state + 3'h1;
223
                                ll_cmd_dat <= r_data_reg[23:16];
224
                                r_cmd_crc_stb <= 1'b1;
225
                        end else if (r_cmd_state == 3'h2)
226
                        begin
227
                                r_cmd_state <= r_cmd_state + 3'h1;
228
                                ll_cmd_dat <= r_data_reg[15:8];
229
                                r_cmd_crc_stb <= 1'b1;
230
                        end else if (r_cmd_state == 3'h3)
231
                        begin
232
                                r_cmd_state <= r_cmd_state + 3'h1;
233
                                ll_cmd_dat <= r_data_reg[7:0];
234
                                r_cmd_crc_stb <= 1'b1;
235
                        end else if (r_cmd_state == 3'h4)
236
                        begin
237
                                r_cmd_state <= r_cmd_state + 3'h1;
238
                                ll_cmd_dat <= cmd_crc;
239
                        end else if (r_cmd_state == 3'h5)
240
                        begin
241
                                ll_cmd_dat <= 8'hff;
242
                                if (r_have_resp)
243
                                begin
244
                                        if (r_use_fifo)
245
                                                r_cmd_state <= r_cmd_state + 3'h1;
246
                                        else
247
                                                r_cmd_state <= r_cmd_state + 3'h2;
248
                                        ll_fifo_rd <= (r_use_fifo)&&(r_fifo_wr);
249
                                        if ((r_use_fifo)&&(r_fifo_wr))
250
                                                ll_cmd_dat <= 8'hfe;
251
                                end
252
                        end else if (r_cmd_state == 3'h6)
253
                        begin
254
                                ll_cmd_dat <= 8'hff;
255
                                if (ll_fifo_rd_complete)
256
                                begin // If we've finished reading from the
257
                                        // FIFO, then move on
258
                                        r_cmd_state <= r_cmd_state + 3'h1;
259
                                        ll_fifo_rd <= 1'b0;
260
                                end else if (ll_fifo_rd)
261
                                        ll_cmd_dat <= fifo_byte;
262
                        end else // if (r_cmd_state == 7)
263
                                ll_cmd_dat <= 8'hff;
264
 
265
 
266
                        // Here we handle the receive portion of the interface.
267
                        // Note that the IF begins with an if of ll_out_stb.
268
                        // That is, if a byte is ready from the lower level.
269
                        //
270
                        // Here we depend upon r_cmd_resp, the response we are
271
                        // expecting from the SDCard, and r_rsp_state, the
272
                        // state machine for where we are at receive what we
273
                        // are expecting.
274
                        if (pre_rsp_state)
275
                        begin
276
                                if (r_rsp_state == `SDSPI_RSP_NONE)
277
                                begin // Waiting on R1
278
                                        if (~ll_out_dat[7])
279
                                        begin
280
                                                r_last_r_one <= ll_out_dat;
281
                                                if (r_cmd_resp == `SDSPI_EXPECT_R1)
282
                                                begin // Expecting R1 alone
283
                                                        r_have_resp <= 1'b1;
284
                                                        ll_cmd_stb <= (r_use_fifo);
285
                                                        r_data_reg <= 32'hffffffff;
286
                                                        ll_fifo_wr<=(r_use_fifo)&&(~r_fifo_wr);
287
                                                end else if (r_cmd_resp == `SDSPI_EXPECT_R1B)
288
                                                begin // Go wait on R1b
289
                                                        r_data_reg <= 32'hffffffff;
290
                                                end // else wait on 32-bit rsp
291
                                        end
292
                                end else if (r_rsp_state == `SDSPI_RSP_BSYWAIT)
293
                                begin // Waiting on R1b, have R1
294
                                        if (nonzero_out)
295
                                                r_have_resp <= 1'b1;
296
                                        ll_cmd_stb <= (r_use_fifo);
297
                                end else if (r_rsp_state == `SDSPI_RSP_GETWORD)
298
                                begin // Have R1, waiting on all of R2/R3/R7
299
                                        r_data_reg <= { r_data_reg[23:0], ll_out_dat };
300
                                        r_data_fil <= r_data_fil+2'b01;
301
                                        if (r_data_fil == 2'b11)
302
                                        begin
303
                                                ll_cmd_stb <= (r_use_fifo);
304
                                                // r_rsp_state <= 3'h3;
305
                                        end
306
                                end else if (r_rsp_state == `SDSPI_RSP_WAIT_WHILE_BUSY)
307
                                begin // Wait while device is busy writing
308
                                        // if (nonzero_out)
309
                                        // begin
310
                                                // r_data_reg[31:8] <= 24'h00;
311
                                                // r_data_reg[7:0] <= ll_out_dat;
312
                                                // // r_rsp_state <= 3'h6;
313
                                        // end
314
                                        ;
315
                                end else if (r_rsp_state == `SDSPI_RSP_RDCOMPLETE)
316
                                begin // Block write command has completed
317
                                        ll_cmd_stb <= 1'b0;
318
                                end else if (r_rsp_state == `SDSPI_RSP_WRITING)
319
                                begin // We are reading from the device into
320
                                        // our FIFO
321
                                        if ((ll_fifo_wr_complete)
322
                                                // Or ... we receive an error
323
                                                ||((~r_have_start_token)
324
                                                &&(~ll_out_dat[4])
325
                                                &&(ll_out_dat[0])))
326
                                        begin
327
                                                ll_fifo_wr <= 1'b0;
328
                                                ll_cmd_stb <= 1'b0;
329
                                        end
330
                                end
331
                        end
332
 
333
                        if (r_watchdog_err)
334
                                ll_cmd_stb <= 1'b0;
335
                        r_cmd_err<= (r_cmd_err)|(fifo_crc_err)|(r_watchdog_err);
336
                end else if (r_cmd_busy)
337
                begin
338
                        r_cmd_busy <= (ll_cmd_stb)||(~ll_idle);
339
                end else if (cmd_stb)
340
                begin // Command write
341
                        // Clear the error on any write, whether a commanding
342
                        // one or not.  -- provided the user requests clearing
343
                        // it (by setting the bit high)
344
                        r_cmd_err  <= (r_cmd_err)&&(~i_wb_data[15]);
345
                        // In a similar fashion, we can switch fifos even if
346
                        // not in the middle of a command
347
                        r_fifo_id  <= i_wb_data[12];
348
                        //
349
                        // Doesn't matter what this is set to as long as we
350
                        // aren't busy, so we can set it irrelevantly here.
351
                        ll_cmd_dat <= i_wb_data[7:0];
352
                        //
353
                        // Note that we only issue a write upon receiving a
354
                        // valid command.  Such a command is 8 bits, and must
355
                        // start with its high order bits set to zero and one.
356
                        // Hence ... we test for that here.
357
                        if (i_wb_data[7:6] == 2'b01)
358
                        begin // Issue a command
359
                                //
360
                                r_cmd_busy <= 1'b1;
361
                                //
362
                                ll_cmd_stb <= 1'b1;
363
                                r_cmd_resp <= i_wb_data[9:8];
364
                                //
365
                                r_cmd_crc_stb <= 1'b1;
366
                                //
367
                                r_fifo_wr  <= i_wb_data[10];
368
                                r_use_fifo <= i_wb_data[11];
369
                                //
370
                        end else if (i_wb_data[7])
371
                        // If, on the other hand, the command was invalid,
372
                        // then it must have been an attempt to read our
373
                        // internal configuration.  So we'll place that on
374
                        // our data register.
375
                                r_data_reg <= { 8'h00,
376
                                        4'h0, max_lgblklen,
377
                                        4'h0, r_lgblklen, 1'b0, r_sdspi_clk };
378
                end else if ((write_stb)&&(i_wb_addr == `SDSPI_DAT_ADDRESS))
379
                begin // Data write
380
                        r_data_reg <= i_wb_data;
381
                end
382
        end
383
 
384
        always @(posedge i_clk)
385
                pre_cmd_state <= (ll_cmd_stb)&&(ll_idle);
386
 
387
        reg     ready_for_response_token;
388
        always @(posedge i_clk)
389
                if (~r_cmd_busy)
390
                        ready_for_response_token <= 1'b0;
391
                else if (ll_fifo_rd)
392
                        ready_for_response_token <= 1'b1;
393
        always @(posedge i_clk)
394
                if (~r_cmd_busy)
395
                        r_have_data_response_token <= 1'b0;
396
                else if ((ll_out_stb)&&(ready_for_response_token)&&(~ll_out_dat[4]))
397
                        r_have_data_response_token <= 1'b1;
398
 
399
        reg     [2:0]    second_rsp_state;
400
        always @(posedge i_clk)
401
                if((r_cmd_resp == `SDSPI_EXPECT_R1)&&(r_use_fifo)&&(r_fifo_wr))
402
                        second_rsp_state <= `SDSPI_RSP_GETTOKEN;
403
                else if (r_cmd_resp == `SDSPI_EXPECT_R1)
404
                        second_rsp_state <= `SDSPI_RSP_WRITING;
405
                else if (r_cmd_resp == `SDSPI_EXPECT_R1B)
406
                        second_rsp_state <= `SDSPI_RSP_BSYWAIT;
407
                else
408
                        second_rsp_state <= `SDSPI_RSP_GETWORD;
409
 
410
        reg     pre_rsp_state, nonzero_out;
411
        always @(posedge i_clk)
412
                if (ll_out_stb)
413
                        nonzero_out <= (|ll_out_dat);
414
        always @(posedge i_clk)
415
                pre_rsp_state <= (ll_out_stb)&&(r_cmd_sent);
416
 
417
        // Each bit depends upon 8 bits of input
418
        initial r_rsp_state = 3'h0;
419
        always @(posedge i_clk)
420
                if (~r_cmd_sent)
421
                        r_rsp_state <= 3'h0;
422
                else if (pre_rsp_state)
423
                begin
424
                        if ((r_rsp_state == `SDSPI_RSP_NONE)&&(~ll_out_dat[7]))
425
                        begin
426
                                r_rsp_state <= second_rsp_state;
427
                        end else if (r_rsp_state == `SDSPI_RSP_BSYWAIT)
428
                        begin // Waiting on R1b, have R1
429
                                // R1b never uses the FIFO
430
                                if (nonzero_out)
431
                                        r_rsp_state <= 3'h6;
432
                        end else if (r_rsp_state == `SDSPI_RSP_GETWORD)
433
                        begin // Have R1, waiting on all of R2/R3/R7
434
                                if (r_data_fil == 2'b11)
435
                                        r_rsp_state <= `SDSPI_RSP_RDCOMPLETE;
436
                        end else if (r_rsp_state == `SDSPI_RSP_GETTOKEN)
437
                        begin // Wait on data token response
438
                                if (r_have_data_response_token)
439
                                        r_rsp_state <= `SDSPI_RSP_WAIT_WHILE_BUSY;
440
                        end else if (r_rsp_state == `SDSPI_RSP_WAIT_WHILE_BUSY)
441
                        begin // Wait while device is busy writing
442
                                if (nonzero_out)
443
                                        r_rsp_state <= `SDSPI_RSP_RDCOMPLETE;
444
                        end
445
                        //else if (r_rsp_state == 3'h6)
446
                        //begin // Block write command has completed
447
                        //      // ll_cmd_stb <= 1'b0;
448
                        // end else if (r_rsp_state == 3'h7)
449
                        // begin // We are reading from the device into
450
                        //      // our FIFO
451
                        // end
452
                end
453
 
454
        always @(posedge i_clk)
455
                r_cmd_sent <= (ll_cmd_stb)&&(r_cmd_state >= 3'h5);
456
 
457
        // initial      r_sdspi_clk = 6'h3c;
458
        initial r_sdspi_clk = 7'h63;
459
        always @(posedge i_clk)
460
        begin
461
                // Update our internal configuration parameters, unconnected
462
                // with the card.  These include the speed of the interface,
463
                // and the size of the block length to expect as part of a FIFO
464
                // command.
465
                if ((cmd_stb)&&(i_wb_data[7:6]==2'b11)&&(~r_data_reg[7])
466
                        &&(r_data_reg[15:12]==4'h00))
467
                begin
468
                        if (|r_data_reg[6:0])
469
                                r_sdspi_clk <= r_data_reg[6:0];
470
                        if (|r_data_reg[11:8])
471
                                r_lgblklen <= r_data_reg[11:8];
472
                end if (r_lgblklen > max_lgblklen)
473
                        r_lgblklen <= max_lgblklen;
474
        end
475
 
476
        assign  need_reset = 1'b0;
477
        always @(posedge i_clk)
478
                case(i_wb_addr)
479
                `SDSPI_CMD_ADDRESS:
480
                        o_wb_data <= { need_reset, 11'h00,
481
                                        3'h0, fifo_crc_err,
482
                                        r_cmd_err, r_cmd_busy, 1'b0, r_fifo_id,
483
                                        r_use_fifo, r_fifo_wr, r_cmd_resp,
484
                                        r_last_r_one };
485
                `SDSPI_DAT_ADDRESS:
486
                        o_wb_data <= r_data_reg;
487
                `SDSPI_FIFO_A_ADDR:
488
                        o_wb_data <= fifo_a_reg;
489
                `SDSPI_FIFO_B_ADDR:
490
                        o_wb_data <= fifo_b_reg;
491
                endcase
492
 
493
        always @(posedge i_clk)
494
                o_wb_ack <= wb_stb;
495
 
496
        initial q_busy = 1'b1;
497
        always @(posedge i_clk)
498
                q_busy <= r_cmd_busy;
499
        always @(posedge i_clk)
500
                o_int <= (~r_cmd_busy)&&(q_busy);
501
 
502
        assign  o_wb_stall = 1'b0;
503
 
504
        //
505
        // Let's work with our FIFO memory here ...
506
        //
507
        //
508
        always @(posedge i_clk)
509
        begin
510
                if ((write_stb)&&(i_wb_addr == `SDSPI_CMD_ADDRESS))
511
                begin // Command write
512
                        // Clear the read/write address
513
                        fifo_wb_addr <= {(LGFIFOLN){1'b0}};
514
                end else if ((wb_stb)&&(i_wb_addr[1]))
515
                begin // On read or write, of either FIFO,
516
                        // we increase our pointer
517
                        fifo_wb_addr <= fifo_wb_addr + 1;
518
                        // And let ourselves know we need to update ourselves
519
                        // on the next clock
520
                end
521
        end
522
 
523
        // Prepare reading of the FIFO for the WB bus read
524
        // Memory read #1
525
        always @(posedge i_clk)
526
        begin
527
                fifo_a_reg <= {
528
                        fifo_a_mem[{ fifo_wb_addr, 2'b00 }],
529
                        fifo_a_mem[{ fifo_wb_addr, 2'b01 }],
530
                        fifo_a_mem[{ fifo_wb_addr, 2'b10 }],
531
                        fifo_a_mem[{ fifo_wb_addr, 2'b11 }] };
532
                fifo_b_reg <= {
533
                        fifo_b_mem[{ fifo_wb_addr, 2'b00 }],
534
                        fifo_b_mem[{ fifo_wb_addr, 2'b01 }],
535
                        fifo_b_mem[{ fifo_wb_addr, 2'b10 }],
536
                        fifo_b_mem[{ fifo_wb_addr, 2'b11 }] };
537
        end
538
 
539
        // Okay, now ... writing our FIFO ...
540
        reg     pre_fifo_addr_inc_rd;
541
        reg     pre_fifo_addr_inc_wr;
542
        initial pre_fifo_addr_inc_rd = 1'b0;
543
        initial pre_fifo_addr_inc_wr = 1'b0;
544
        always @(posedge i_clk)
545
                pre_fifo_addr_inc_wr <= ((ll_fifo_wr)&&(ll_out_stb)&&(r_have_start_token));
546
        always @(posedge i_clk)
547
                pre_fifo_addr_inc_rd <= ((ll_fifo_rd)&&(ll_cmd_stb)&&(ll_idle));//&&(ll_fifo_pkt_state[2:0]!=3'b000));
548
        always @(posedge i_clk)
549
        begin
550
                // if ((write_stb)&&(i_wb_addr == `SDSPI_CMD_ADDRESS)&&(i_wb_data[11]))
551
                        // ll_fifo_addr <= {(LGFIFOLN+2){1'b0}};
552
                if (~r_cmd_busy)
553
                        ll_fifo_addr <= {(LGFIFOLN+2){1'b0}};
554
                else if ((pre_fifo_addr_inc_wr)||(pre_fifo_addr_inc_rd))
555
                        ll_fifo_addr <= ll_fifo_addr + 1;
556
        end
557
 
558
        // Look for that start token
559
        always @(posedge i_clk)
560
                if (~r_cmd_busy)
561
                        r_have_start_token <= 1'b0;
562
                else if ((ll_fifo_wr)&&(ll_out_stb)&&(ll_out_dat==8'hfe))
563
                        r_have_start_token <= 1'b1;
564
 
565
        reg     last_fifo_byte;
566
        initial last_fifo_byte = 1'b0;
567
        always @(posedge i_clk)
568
                if (ll_fifo_wr)
569
                        last_fifo_byte <= (ll_fifo_addr == w_blklimit);
570
                else
571
                        last_fifo_byte <= 1'b0;
572
 
573
        // This is the one (and only allowed) write to the FIFO memory always
574
        // block.
575
        //
576
        // If ll_fifo_wr is true, we'll be writing to the FIFO, and we'll do
577
        // that here.  This is different from r_fifo_wr, which specifies that
578
        // we will be writing to the SDCard from the FIFO, and hence READING
579
        // from the FIFO.
580
        //
581
        reg     pre_fifo_a_wr, pre_fifo_b_wr, pre_fifo_crc_a, pre_fifo_crc_b,
582
                clear_fifo_crc;
583
        always @(posedge i_clk)
584
        begin
585
                pre_fifo_a_wr <= (ll_fifo_wr)&&(ll_out_stb)&&(~r_fifo_id)&&(ll_fifo_wr_state == 2'b00);
586
                pre_fifo_b_wr <= (ll_fifo_wr)&&(ll_out_stb)&&( r_fifo_id)&&(ll_fifo_wr_state == 2'b00);
587
                fifo_wr_crc_stb <= (ll_fifo_wr)&&(ll_out_stb)&&(ll_fifo_wr_state == 2'b00)&&(r_have_start_token);
588
                pre_fifo_crc_a<= (ll_fifo_wr)&&(ll_out_stb)&&(ll_fifo_wr_state == 2'b01);
589
                pre_fifo_crc_b<= (ll_fifo_wr)&&(ll_out_stb)&&(ll_fifo_wr_state == 2'b10);
590
                clear_fifo_crc <= (cmd_stb)&&(i_wb_data[15]);
591
        end
592
 
593
        initial         fifo_crc_err = 1'b0;
594
        always @(posedge i_clk)
595
        begin // One and only memory write allowed
596
                if ((write_stb)&&(i_wb_addr[1:0]==2'b10))
597
                        {fifo_a_mem[{ fifo_wb_addr, 2'b00 }],
598
                        fifo_a_mem[{  fifo_wb_addr, 2'b01 }],
599
                        fifo_a_mem[{  fifo_wb_addr, 2'b10 }],
600
                        fifo_a_mem[{  fifo_wb_addr, 2'b11 }] }
601
                        <= i_wb_data;
602
                else if (pre_fifo_a_wr)
603
                        fifo_a_mem[{ ll_fifo_addr }] <= ll_out_dat;
604
 
605
                if ((write_stb)&&(i_wb_addr[1:0]==2'b11))
606
                        {fifo_b_mem[{fifo_wb_addr, 2'b00 }],
607
                        fifo_b_mem[{ fifo_wb_addr, 2'b01 }],
608
                        fifo_b_mem[{ fifo_wb_addr, 2'b10 }],
609
                        fifo_b_mem[{ fifo_wb_addr, 2'b11 }] }
610
                        <= i_wb_data;
611
                else if (pre_fifo_b_wr)
612
                        fifo_b_mem[{ ll_fifo_addr }] <= ll_out_dat;
613
 
614
                if (~r_cmd_busy)
615
                        ll_fifo_wr_complete <= 1'b0;
616
 
617
                if (~r_cmd_busy)
618
                        ll_fifo_wr_state <= 2'b00;
619
                else if ((pre_fifo_a_wr)||(pre_fifo_b_wr))
620
                        ll_fifo_wr_state <= (last_fifo_byte)? 2'b01:2'b00;
621
 
622
                if (pre_fifo_crc_a)
623
                begin
624
                        fifo_crc_err <= fifo_crc_err | (fifo_wr_crc_reg[15:8]!=ll_out_dat);
625
                        ll_fifo_wr_state <= ll_fifo_wr_state + 2'b01;
626
                end if (pre_fifo_crc_b)
627
                begin
628
                        fifo_crc_err <= fifo_crc_err | (fifo_wr_crc_reg[7:0]!=ll_out_dat);
629
                        ll_fifo_wr_state <= ll_fifo_wr_state + 2'b01;
630
                        ll_fifo_wr_complete <= 1'b1;
631
                end else if (clear_fifo_crc)
632
                        fifo_crc_err <= 1'b0;
633
        end
634
 
635
        always @(posedge i_clk)
636
        begin // Second memory read, this time for the FIFO
637
                fifo_a_byte <= fifo_a_mem[ ll_fifo_addr ];
638
                fifo_b_byte <= fifo_b_mem[ ll_fifo_addr ];
639
        end
640
 
641
        reg     [(LGFIFOLN-1):0] r_blklimit;
642
        wire    [(LGFIFOLN+1):0] w_blklimit;
643
        always @(posedge i_clk)
644
                r_blklimit[(LGFIFOLN-1):0] = (1<<r_lgblklen)-1;
645
        assign  w_blklimit = { r_blklimit, 2'b11 };
646
 
647
        // Package the FIFO reads up into a packet
648
        always @(posedge i_clk)
649
        begin
650
                fifo_rd_crc_stb <= 1'b0;
651
                if (r_cmd_busy)
652
                begin
653
                        if (ll_fifo_pkt_state[2:0] == 3'b000)
654
                        begin
655
                                if((ll_fifo_rd)&&(ll_cmd_stb)&&(ll_idle))
656
                                begin
657
                                        ll_fifo_pkt_state <= ll_fifo_pkt_state + 3'b001;
658
                                end
659
                        end else if (ll_fifo_pkt_state[2:0] == 3'b001)
660
                        begin
661
                                if((ll_fifo_rd)&&(ll_cmd_stb)&&(ll_idle))
662
                                begin
663
                                        ll_fifo_pkt_state <= ll_fifo_pkt_state + 3'b001;
664
                                        fifo_byte <= (r_fifo_id)
665
                                                ? fifo_b_byte : fifo_a_byte;
666
                                        fifo_rd_crc_stb <= 1'b1;
667
                                end
668
                        end else if (ll_fifo_pkt_state[2:0] == 3'b010)
669
                        begin
670
                                if((ll_fifo_rd)&&(ll_cmd_stb)&&(ll_idle))
671
                                begin
672
                                        fifo_byte <= (r_fifo_id)
673
                                                ? fifo_b_byte : fifo_a_byte;
674
                                        fifo_rd_crc_stb <= 1'b1;
675
                                end
676
                                if (ll_fifo_addr == 0)
677
                                        ll_fifo_pkt_state <= 3'b011;
678
                        end else if (ll_fifo_pkt_state == 3'b011)
679
                        begin // 1st CRC byte
680
                                if((ll_fifo_rd)&&(ll_cmd_stb)&&(ll_idle))
681
                                begin
682
                                        fifo_byte <= fifo_rd_crc_reg[15:8];
683
                                        ll_fifo_pkt_state <= 3'b100;
684
                                end
685
                        end else if (ll_fifo_pkt_state == 3'b100)
686
                        begin // 2nd CRC byte
687
                                if((ll_fifo_rd)&&(ll_cmd_stb)&&(ll_idle))
688
                                begin
689
                                        fifo_byte <= fifo_rd_crc_reg[7:0];
690
                                        ll_fifo_pkt_state <= 3'b101;
691
                                end
692
                        end else if((ll_fifo_rd)&&(ll_cmd_stb)&&(ll_idle))
693
                        begin
694
                        // Idle the channel
695
                                ll_fifo_rd_complete <= 1'b1;
696
                                fifo_byte <= 8'hff;
697
                        end
698
                end else if ((write_stb)&&(i_wb_addr == `SDSPI_CMD_ADDRESS))
699
                begin
700
                        ll_fifo_pkt_state <= 3'h0;
701
                        ll_fifo_rd_complete <= 1'b0;
702
                        fifo_byte <= (i_wb_data[12]) ? fifo_b_byte : fifo_a_byte;
703
                        fifo_rd_crc_stb <= 1'b1;
704
                end else begin // Packet state is IDLE (clear the CRC registers)
705
                        ll_fifo_pkt_state <= 3'b111;
706
                        ll_fifo_rd_complete <= 1'b1;
707
                end
708
        end
709
 
710
        always @(posedge i_clk)
711
        begin
712
                if (~ll_fifo_wr)
713
                        fifo_wr_crc_reg <= 16'h00;
714
                else if (fifo_wr_crc_stb)
715
                begin
716
                        fifo_wr_crc_reg[15:8] <=fifo_wr_crc_reg[15:8]^ll_out_dat;
717
                        fifo_wr_crc_count <= 4'h8;
718
                end else if (|fifo_wr_crc_count)
719
                begin
720
                        fifo_wr_crc_count <= fifo_wr_crc_count - 4'h1;
721
                        if (fifo_wr_crc_reg[15])
722
                                fifo_wr_crc_reg <= { fifo_wr_crc_reg[14:0], 1'b0 }
723
                                        ^ 16'h1021;
724
                        else
725
                                fifo_wr_crc_reg <= { fifo_wr_crc_reg[14:0], 1'b0 };
726
                end
727
        end
728
 
729
        always @(posedge i_clk)
730
        begin
731
                if (~r_cmd_busy)
732
                begin
733
                        fifo_rd_crc_reg <= 16'h00;
734
                        fifo_rd_crc_count <= 4'h0;
735
                end else if (fifo_rd_crc_stb)
736
                begin
737
                        fifo_rd_crc_reg[15:8] <=fifo_rd_crc_reg[15:8]^fifo_byte;
738
                        fifo_rd_crc_count <= 4'h8;
739
                end else if (|fifo_rd_crc_count)
740
                begin
741
                        fifo_rd_crc_count <= fifo_rd_crc_count - 4'h1;
742
                        if (fifo_rd_crc_reg[15])
743
                                fifo_rd_crc_reg <= { fifo_rd_crc_reg[14:0], 1'b0 }
744
                                        ^ 16'h1021;
745
                        else
746
                                fifo_rd_crc_reg <= { fifo_rd_crc_reg[14:0], 1'b0 };
747
                end
748
        end
749
 
750
        //
751
        // Calculate a CRC for the command section of our output
752
        //
753
        initial r_cmd_crc_ff = 1'b0;
754
        always @(posedge i_clk)
755
        begin
756
                if (~r_cmd_busy)
757
                begin
758
                        r_cmd_crc <= 8'h00;
759
                        r_cmd_crc_cnt <= 4'hf;
760
                        r_cmd_crc_ff <= 1'b0;
761
                end else if (~r_cmd_crc_cnt[3])
762
                begin
763
                        r_cmd_crc_cnt <= r_cmd_crc_cnt - 4'h1;
764
                        if (r_cmd_crc[7])
765
                                r_cmd_crc <= { r_cmd_crc[6:0], 1'b0 } ^ 8'h12;
766
                        else
767
                                r_cmd_crc <= { r_cmd_crc[6:0], 1'b0 };
768
                        r_cmd_crc_ff <= (r_cmd_crc_ff)||(r_cmd_crc_stb);
769
                end else if ((r_cmd_crc_stb)||(r_cmd_crc_ff))
770
                begin
771
                        r_cmd_crc <= r_cmd_crc ^ ll_cmd_dat;
772
                        r_cmd_crc_cnt <= 4'h7;
773
                        r_cmd_crc_ff <= 1'b0;
774
                end
775
        end
776
        assign  cmd_crc = { r_cmd_crc[7:1], 1'b1 };
777
 
778
        //
779
        // Some watchdog logic for us.  This way, if we are waiting for the
780
        // card to respond, and something goes wrong, we can timeout the
781
        // transaction and ... figure out what to do about it later.  At least
782
        // we'll have an error indication.
783
        //
784
        initial r_watchdog = 26'h3ffffff;
785
        initial r_watchdog_err = 1'b0;
786
        always @(posedge i_clk)
787
                if (~r_cmd_busy)
788
                        r_watchdog_err <= 1'b0;
789
                else if (r_watchdog == 0)
790
                        r_watchdog_err <= 1'b1;
791
        always @(posedge i_clk)
792
                if (~r_cmd_busy)
793
                        r_watchdog <= 26'h3fffff;
794
                else if (|r_watchdog)
795
                        r_watchdog <= r_watchdog - 26'h1;
796
 
797
        assign o_debug = { ((ll_cmd_stb)&&(ll_idle))||(ll_out_stb),
798
                                ll_cmd_stb, ll_idle, ll_out_stb, // 4'h
799
                        o_cs_n, o_sck, o_mosi, i_miso,  // 4'h
800
                        r_cmd_state, i_bus_grant,       // 4'h
801
                        r_rsp_state, r_cmd_busy,        // 4'h
802
                        ll_cmd_dat,             // 8'b
803
                        ll_out_dat };           // 8'b
804
endmodule
805
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.