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

Subversion Repositories xulalx25soc

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

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

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

powered by: WebSVN 2.1.0

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