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

Subversion Repositories qspiflash

[/] [qspiflash/] [trunk/] [rtl/] [wbqspiflash.v] - Blame information for rev 3

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

Line No. Rev Author Line
1 3 dgisselq
///////////////////////////////////////////////////////////////////////////
2 2 dgisselq
//
3
// Filename:    wbspiflash.v
4
//
5 3 dgisselq
// Project:     Wishbone Controlled Quad SPI Flash Controller
6 2 dgisselq
//
7
// Purpose:     Access a Quad SPI flash via a WISHBONE interface.  This
8
//              includes both read and write (and erase) commands to the SPI
9
//              flash.  All read/write commands are accomplished using the
10
//              high speed (4-bit) interface.  Further, the device will be
11
//              left/kept in the 4-bit read interface mode between accesses,
12
//              for a minimum read latency.
13
//
14 3 dgisselq
//      Wishbone Registers (See spec sheet for more detail):
15 2 dgisselq
//      0: local config(r) / erase commands(w) / deep power down cmds / etc.
16
//      R: (Write in Progress), (dirty-block), (spi_port_busy), 1'b0, 9'h00,
17
//              { last_erased_sector, 14'h00 } if (WIP)
18
//              else { current_sector_being_erased, 14'h00 }
19
//              current if write in progress, last if written
20
//      W: (1'b1 to erase), (12'h ignored), next_erased_block, 14'h ignored)
21 3 dgisselq
//      1: Configuration register
22
//      2: Status register (R/w)
23
//      3: Read ID (read only)
24 2 dgisselq
//      (19 bits): Data (R/w, but expect writes to take a while)
25
//              
26
//
27
// Creator:     Dan Gisselquist
28
//              Gisselquist Tecnology, LLC
29
//
30 3 dgisselq
///////////////////////////////////////////////////////////////////////////
31 2 dgisselq
//
32 3 dgisselq
// Copyright (C) 2015, Gisselquist Technology, LLC
33 2 dgisselq
//
34 3 dgisselq
// This program is free software (firmware): you can redistribute it and/or
35
// modify it under the terms of  the GNU General Public License as published
36
// by the Free Software Foundation, either version 3 of the License, or (at
37
// your option) any later version.
38
//
39
// This program is distributed in the hope that it will be useful, but WITHOUT
40
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
41
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
42
// for more details.
43
//
44
// You should have received a copy of the GNU General Public License along
45
// with this program.  (It's in the $(ROOT)/doc directory, run make with no
46
// target there if the PDF file isn't present.)  If not, see
47
// <http://www.gnu.org/licenses/> for a copy.
48
//
49
// License:     GPL, v3, as defined and found on www.gnu.org,
50
//              http://www.gnu.org/licenses/gpl.html
51
//
52
//
53
///////////////////////////////////////////////////////////////////////////
54 2 dgisselq
`define WBQSPI_RESET            0
55
`define WBQSPI_RESET_WEN        1
56
`define WBQSPI_IDLE             2
57
`define WBQSPI_RDIDLE           3       // Idle, but in fast read mode
58
`define WBQSPI_WBDECODE         4
59
`define WBQSPI_WAIT_WIP_CLEAR   5
60
`define WBQSPI_CHECK_WIP_CLEAR  6
61
`define WBQSPI_CHECK_WIP_DONE   7
62
`define WBQSPI_WEN              8
63
`define WBQSPI_PP               9       // Program page
64
`define WBQSPI_QPP              10      // Program page, 4 bit mode
65
`define WBQSPI_WR_DATA          11
66
`define WBQSPI_WR_BUS_CYCLE     12
67
`define WBQSPI_RD_DUMMY         13
68
`define WBQSPI_QRD_ADDRESS      14
69
`define WBQSPI_QRD_DUMMY        15
70
`define WBQSPI_READ_CMD         16
71
`define WBQSPI_READ_DATA        17
72
`define WBQSPI_WAIT_TIL_RDIDLE  18
73
`define WBQSPI_READ_ID_CMD      19
74
`define WBQSPI_READ_ID          20
75
`define WBQSPI_READ_STATUS      21
76
`define WBQSPI_READ_CONFIG      22
77
`define WBQSPI_WRITE_STATUS     23
78
`define WBQSPI_WRITE_CONFIG     24
79
`define WBQSPI_ERASE_WEN        25
80
`define WBQSPI_ERASE_CMD        26
81
`define WBQSPI_ERASE_BLOCK      27
82
`define WBQSPI_CLEAR_STATUS     28
83
`define WBQSPI_IDLE_CHECK_WIP   29
84
`define WBQSPI_WAIT_TIL_IDLE    30
85
 
86
module  wbqspiflash(i_clk_100mhz,
87
                // Internal wishbone connections
88
                i_wb_cyc, i_wb_data_stb, i_wb_ctrl_stb, i_wb_we,
89
                i_wb_addr, i_wb_data,
90
                // Wishbone return values
91
                o_wb_ack, o_wb_stall, o_wb_data,
92
                // Quad Spi connections to the external device
93
                o_qspi_sck, o_qspi_cs_n, o_qspi_mod, o_qspi_dat, i_qspi_dat,
94 3 dgisselq
                o_interrupt);
95 2 dgisselq
        input                   i_clk_100mhz;
96
        // Wishbone, inputs first
97
        input                   i_wb_cyc, i_wb_data_stb, i_wb_ctrl_stb, i_wb_we;
98
        input           [19:0]   i_wb_addr;
99
        input           [31:0]   i_wb_data;
100
        // then outputs
101
        output  reg             o_wb_ack;
102
        output  reg             o_wb_stall;
103
        output  reg     [31:0]   o_wb_data;
104
        // Quad SPI control wires
105
        output  wire            o_qspi_sck, o_qspi_cs_n;
106
        output  wire    [1:0]    o_qspi_mod;
107
        output  wire    [3:0]    o_qspi_dat;
108
        input           [3:0]    i_qspi_dat;
109
        // Interrupt line
110
        output  reg             o_interrupt;
111
 
112
        reg             spi_wr, spi_hold, spi_spd, spi_dir;
113
        reg     [31:0]   spi_in;
114
        reg     [1:0]    spi_len;
115
        wire    [31:0]   spi_out;
116
        wire            spi_valid, spi_busy;
117
        llqspi  lldriver(i_clk_100mhz,
118
                        spi_wr, spi_hold, spi_in, spi_len, spi_spd, spi_dir,
119
                                spi_out, spi_valid, spi_busy,
120
                        o_qspi_sck, o_qspi_cs_n, o_qspi_mod, o_qspi_dat,
121 3 dgisselq
                                i_qspi_dat);
122 2 dgisselq
 
123
        // Erase status tracking
124
        reg             write_in_progress, write_protect;
125
        reg     [5:0]    erased_sector;
126
        reg             dirty_sector;
127
        initial begin
128
                write_in_progress = 1'b0;
129
                erased_sector = 6'h00;
130
                dirty_sector  = 1'b1;
131
                write_protect = 1'b1;
132
        end
133
 
134
        reg     [7:0]    last_status;
135
        reg             quad_mode_enabled;
136
        reg             spif_cmd;
137
        reg     [19:0]   spif_addr;
138
        reg     [31:0]   spif_data;
139
        reg     [5:0]    state;
140
        reg     [5:0]    spif_return;
141
        reg             spif_ctrl, spif_req;
142
        wire    [5:0]    spif_sector;
143
        assign  spif_sector = spif_addr[19:14];
144
 
145
        initial state = `WBQSPI_RESET;
146
        initial o_wb_ack   = 1'b0;
147
        initial o_wb_stall = 1'b1;
148
        initial spi_wr     = 1'b0;
149
        initial spi_len    = 2'b00;
150
        initial quad_mode_enabled = 1'b0;
151
        initial o_interrupt = 1'b0;
152
        always @(posedge i_clk_100mhz)
153
        if (state == `WBQSPI_RESET)
154
        begin
155
                // From a reset, we should
156
                //      Enable the Quad I/O mode
157
                //      Disable the Write protection bits in the status register
158
                //      Chip should already be up and running, so we can start
159
                //      immediately ....
160
                o_wb_ack <= 1'b0;
161
                o_wb_stall <= 1'b1;
162
                spi_hold <= 1'b0;
163
                spi_spd  <= 1'b0;
164
                spi_dir  <= 1'b0;
165
                last_status <= 8'h00;
166
                if (spi_len < 2'b11)
167
                        spi_len <= spi_len + 1;
168
                state <= `WBQSPI_IDLE;
169
                spif_req <= 1'b0;
170
        /*
171
        // This next bit of logic is confusing ...
172
        // I wish to enable quad SPI mode automatically.  However, this is
173
        // a configuration register thing, so once written it never needs to
174
        // be adjusted again.  Further, some bits within the configuration
175
        // register cannot be undone, and my inkling might be to set those.
176
        // Finally, the configuration register affects the control of the
177
        // block protect registers in the status register.  So ...
178
        // let's just leave these registers alone, or command them from the
179
        // UART-wishbone interface.
180
                else if (~spi_busy)
181
                begin
182
                        spi_wr  <= 1'b1;
183
                        spi_len <= 2'b00; // 1 byte
184
                        spi_in  <= { 8'h06, 24'h000 };
185
                end
186
        end else if (state == `WBQSPI_RESET_WEN)
187
        begin
188
                spi_wr <= 1'b0;
189
                if ((~spi_busy)&&(~spi_wr)&&(o_qspi_cs_n))
190
                begin
191
                        spi_wr  <= 1'b1;
192
                        spi_spd <= 1'b0;
193
                        spi_len <= 2'b10; // 3 bytes
194
                        // Command, status, config, then don't care
195
                        spi_in <= { 8'h01, 8'h00, 8'h0b, 8'h00 };
196
                        spi_dir <= 1'b0;
197
                        quad_mode_enabled <= 1'b1;
198
                        state <= `WBQSPI_WAIT_TIL_IDLE;
199
                        write_in_progress <= 1'b1;
200
                end
201
        */
202
        end else if (state == `WBQSPI_IDLE)
203
        begin
204
                o_interrupt <= 1'b0;
205
                o_wb_stall <= 1'b0;
206
                o_wb_ack <= 1'b0;
207
                spif_cmd   <= i_wb_we;
208
                spif_addr  <= i_wb_addr;
209
                spif_data  <= i_wb_data;
210
                spif_ctrl  <= (i_wb_ctrl_stb)&&(~i_wb_data_stb);
211
                spif_req   <= (i_wb_ctrl_stb)||(i_wb_data_stb);
212
                spi_wr <= 1'b0; // Keep the port idle, unless told otherwise
213
                spi_hold <= 1'b0;
214
                spi_spd  <= 1'b0;
215
                spi_dir <= 1'b0; // Write (for now, 'cause of cmd)
216
                // Data register access
217
                if ((i_wb_data_stb)&&(i_wb_cyc))
218
                begin
219
 
220
                        if (i_wb_we) // Request to write a page
221
                        begin
222
                                if((~write_protect)&&(~write_in_progress))
223
                                begin // 00
224
                                        spi_wr <= 1'b1;
225
                                        spi_len <= 2'b00; // 8 bits
226
                                        // Send a write enable command
227
                                        spi_in <= { 8'h06, 24'h00 };
228
                                        state <= `WBQSPI_WEN;
229
 
230
                                        o_wb_ack <= 1'b0;
231
                                        o_wb_stall <= 1'b1;
232
                                end else if (write_protect)
233
                                begin // whether or not write-in_progress ...
234
                                        // Do nothing on a write protect
235
                                        // violation
236
                                        //
237
                                        o_wb_ack <= 1'b1;
238
                                        o_wb_stall <= 1'b0;
239
                                end else begin // write is in progress, wait
240
                                        // for it to complete
241
                                        state <= `WBQSPI_WAIT_WIP_CLEAR;
242
                                        o_wb_ack <= 1'b0;
243
                                        o_wb_stall <= 1'b1;
244
                                end
245
                        end else if (~write_in_progress)
246
                        begin // Read access, normal mode(s)
247
                                o_wb_ack   <= 1'b0;
248
                                o_wb_stall <= 1'b1;
249
                                spi_wr     <= 1'b1;     // Write cmd to device
250
                                if (quad_mode_enabled)
251
                                begin
252
                                        spi_in <= { 8'heb, 2'b00, i_wb_addr[19:0], 2'b00 };
253
                                        state <= `WBQSPI_QRD_ADDRESS;
254
                                        spi_len    <= 2'b00; // single byte, cmd only
255
                                end else begin
256
                                        spi_in <= { 8'h0b, 2'b00, i_wb_addr[19:0], 2'b00 };
257
                                        state <= `WBQSPI_RD_DUMMY;
258
                                        spi_len    <= 2'b11; // cmd+addr,32bits
259
                                end
260
                        end else begin
261
                                // A write is in progress ... need to stall
262
                                // the bus until the write is complete.
263
                                state <= `WBQSPI_WAIT_WIP_CLEAR;
264
                                o_wb_ack   <= 1'b0;
265
                                o_wb_stall <= 1'b1;
266
                        end
267
                end else if ((i_wb_cyc)&&(i_wb_ctrl_stb)&&(i_wb_we))
268
                begin
269
                        o_wb_stall <= 1'b1;
270
                        case(i_wb_addr[1:0])
271
                        2'b00: begin // Erase command register
272
                                write_protect <= ~i_wb_data[28];
273
                                o_wb_stall <= 1'b0;
274
 
275
                                if((i_wb_data[31])&&(~write_in_progress))
276
                                begin
277
                                        // Command an erase--ack it immediately
278
 
279
                                        o_wb_ack <= 1'b1;
280
                                        o_wb_stall <= 1'b0;
281
 
282
                                        if ((i_wb_data[31])&&(~write_protect))
283
                                        begin
284
                                                spi_wr <= 1'b1;
285
                                                spi_len <= 2'b00;
286
                                                // Send a write enable command
287
                                                spi_in <= { 8'h06, 24'h00 };
288
                                                state <= `WBQSPI_ERASE_CMD;
289
                                                o_wb_stall <= 1'b1;
290
                                        end
291
                                end else if (i_wb_data[31])
292
                                begin
293
                                        state <= `WBQSPI_WAIT_WIP_CLEAR;
294
                                        o_wb_ack   <= 1'b1;
295
                                        o_wb_stall <= 1'b1;
296
                                end else
297
                                        o_wb_ack   <= 1'b1;
298
                                        o_wb_stall <= 1'b0;
299
                                end
300
                        2'b01: begin
301
                                // Write the configuration register
302
                                o_wb_ack <= 1'b1;
303
                                o_wb_stall <= 1'b1;
304
 
305
                                // Need to send a write enable command first
306
                                spi_wr <= 1'b1;
307
                                spi_len <= 2'b00; // 8 bits
308
                                // Send a write enable command
309
                                spi_in <= { 8'h06, 24'h00 };
310
                                state <= `WBQSPI_WRITE_CONFIG;
311
                                end
312
                        2'b10: begin
313
                                // Write the status register
314
                                o_wb_ack <= 1'b1; // Ack immediately
315
                                o_wb_stall <= 1'b1; // Stall other cmds
316
                                // Need to send a write enable command first
317
                                spi_wr <= 1'b1;
318
                                spi_len <= 2'b00; // 8 bits
319
                                // Send a write enable command
320
                                spi_in <= { 8'h06, 24'h00 };
321
                                state <= `WBQSPI_WRITE_STATUS;
322
                                end
323
                        2'b11: begin // Write the ID register??? makes no sense
324
                                o_wb_ack <= 1'b1;
325
                                o_wb_stall <= 1'b0;
326
                                end
327
                        endcase
328
                end else if ((i_wb_cyc)&&(i_wb_ctrl_stb)) // &&(~i_wb_we))
329
                begin
330
                        case(i_wb_addr[1:0])
331
                        2'b00: begin // Read local register
332
                                if (write_in_progress) // Read status
333
                                begin// register, is write still in progress?
334
                                        state <= `WBQSPI_READ_STATUS;
335
                                        spi_wr <= 1'b1;
336
                                        spi_len <= 2'b01;// 8 bits out, 8 bits in
337
                                        spi_in <= { 8'h05, 24'h00};
338
 
339
                                        o_wb_ack <= 1'b0;
340
                                        o_wb_stall <= 1'b1;
341
                                end else begin // Return w/o talking to device
342
                                        o_wb_ack <= 1'b1;
343
                                        o_wb_stall <= 1'b0;
344
                                        o_wb_data <= { write_in_progress,
345
                                                dirty_sector, spi_busy,
346
                                                ~write_protect,
347
                                                quad_mode_enabled,
348
                                                7'h00,
349
                                                erased_sector, 14'h000 };
350
                                end end
351
                        2'b01: begin // Read configuration register
352
                                state <= `WBQSPI_READ_CONFIG;
353
                                spi_wr <= 1'b1;
354
                                spi_len <= 2'b01;
355
                                spi_in <= { 8'h35, 24'h00};
356
 
357
                                o_wb_ack <= 1'b0;
358
                                o_wb_stall <= 1'b1;
359
                                end
360
                        2'b10: begin // Read status register
361
                                state <= `WBQSPI_READ_STATUS;
362
                                spi_wr <= 1'b1;
363
                                spi_len <= 2'b01; // 8 bits out, 8 bits in
364
                                spi_in <= { 8'h05, 24'h00};
365
 
366
                                o_wb_ack <= 1'b0;
367
                                o_wb_stall <= 1'b1;
368
                                end
369
                        2'b11: begin // Read ID register
370
                                state <= `WBQSPI_READ_ID_CMD;
371
                                spi_wr <= 1'b1;
372
                                spi_len <= 2'b00;
373
                                spi_in <= { 8'h9f, 24'h00};
374
 
375
                                o_wb_ack <= 1'b0;
376
                                o_wb_stall <= 1'b1;
377
                                end
378
                        endcase
379
                end else if ((~i_wb_cyc)&&(write_in_progress))
380
                begin
381
                        state <= `WBQSPI_IDLE_CHECK_WIP;
382
                        spi_wr <= 1'b1;
383
                        spi_len <= 2'b01; // 8 bits out, 8 bits in
384
                        spi_in <= { 8'h05, 24'h00};
385
 
386
                        o_wb_ack <= 1'b0;
387
                        o_wb_stall <= 1'b1;
388
                end
389
        end else if (state == `WBQSPI_RDIDLE)
390
        begin
391
                spi_wr <= 1'b0;
392
                o_wb_stall <= 1'b0;
393
                o_wb_ack <= 1'b0;
394
                spif_cmd   <= i_wb_we;
395
                spif_addr  <= i_wb_addr;
396
                spif_data  <= i_wb_data;
397
                spif_ctrl  <= (i_wb_ctrl_stb)&&(~i_wb_data_stb);
398
                spif_req   <= (i_wb_ctrl_stb)||(i_wb_data_stb);
399
                spi_hold <= 1'b0;
400
                spi_spd<= 1'b1;
401
                spi_dir <= 1'b0; // Write (for now)
402
                if ((i_wb_cyc)&&(i_wb_data_stb)&&(~i_wb_we))
403
                begin // Continue our read ... send the new address / mode
404
                        o_wb_stall <= 1'b1;
405
                        spi_wr <= 1'b1;
406
                        spi_len <= 2'b10;
407
                        spi_in <= { 2'h0, i_wb_addr[19:0], 2'h0, 8'ha0 };
408
                        state <= `WBQSPI_QRD_DUMMY;
409
                end else if((i_wb_cyc)&&(i_wb_ctrl_stb)&&(~i_wb_we)&&(i_wb_addr[1:0] == 2'b00))
410
                begin
411
                        // A local read that doesn't touch the device, so leave
412
                        // the device in its current state
413
                        o_wb_stall <= 1'b0;
414
                        o_wb_ack <= 1'b1;
415
                        o_wb_data <= { write_in_progress,
416
                                        dirty_sector, spi_busy,
417
                                        ~write_protect,
418
                                        quad_mode_enabled,
419
                                        7'h00,
420
                                        erased_sector, 14'h000 };
421
                end else if((i_wb_cyc)&&((i_wb_ctrl_stb)||(i_wb_data_stb)))
422
                begin // Need to release the device from quad mode for all else
423
                        o_wb_ack   <= 1'b0;
424
                        o_wb_stall <= 1'b1;
425
                        spi_wr <= 1'b1;
426
                        spi_len <= 2'b11;
427
                        spi_in <= 32'h00;
428
                        state <= `WBQSPI_WBDECODE;
429
                end
430
        end else if (state == `WBQSPI_WBDECODE)
431
        begin
432
                // We were in quad SPI read mode, and had to get out.
433
                // Now we've got a command (not data read) to read and
434
                // execute.  Accomplish what we would've done while in the
435
                // IDLE state here, save only that we don't have to worry
436
                // about data reads, and we need to operate on a stored
437
                // version of the bus command
438
                o_wb_stall <= 1'b1;
439
                o_wb_ack <= 1'b0;
440
                spi_wr <= 1'b0; // Keep the port idle, unless told otherwise
441
                spi_hold <= 1'b0;
442
                spi_spd <= 1'b0;
443
                spi_dir <= 1'b0;
444
                spif_req<= (spif_req) && (i_wb_cyc);
445
                if ((~spi_busy)&&(o_qspi_cs_n)&&(~spi_wr)) // only in full idle ...
446
                begin
447
                        // Data register access
448
                        if (~spif_ctrl)
449
                        begin
450
                                if (spif_cmd) // Request to write a page
451
                                begin
452
                                        if((~write_protect)&&(~write_in_progress))
453
                                        begin // 00
454
                                                spi_wr <= 1'b1;
455
                                                spi_len <= 2'b00; // 8 bits
456
                                                // Send a write enable command
457
                                                spi_in <= { 8'h06, 24'h00 };
458
                                                state <= `WBQSPI_WEN;
459
 
460
                                                o_wb_ack <= 1'b0;
461
                                                o_wb_stall <= 1'b1;
462
                                        end else if (write_protect)
463
                                        begin // whether or not write-in_progress ...
464
                                                // Do nothing on a write protect
465
                                                // violation
466
                                                //
467
                                                o_wb_ack <= spif_req;
468
                                                o_wb_stall <= 1'b0;
469
                                                state <= `WBQSPI_IDLE;
470
                                        end else begin // write is in progress, wait
471
                                                // for it to complete
472
                                                state <= `WBQSPI_WAIT_WIP_CLEAR;
473
                                                o_wb_ack <= 1'b0;
474
                                                o_wb_stall <= 1'b1;
475
                                        end
476
                                // end else if (~write_in_progress) // always true
477
                                // but ... we wouldn't get here on a normal read access
478
                                end else begin
479
                                        // Something's wrong, we should never get here
480
                                        // Attempt to go to idle to recover
481
                                        state <= `WBQSPI_IDLE;
482
                                end
483
                        end else if ((spif_ctrl)&&(spif_cmd))
484
                        begin
485
                                o_wb_stall <= 1'b1;
486
                                case(spif_addr[1:0])
487
                                2'b00: begin // Erase command register
488
                                        o_wb_ack   <= spif_req;
489
                                        o_wb_stall <= 1'b0;
490
                                        state <= `WBQSPI_IDLE;
491
                                        write_protect <= ~spif_data[28];
492
                                        // Are we commanding an erase?
493
                                        // We're in read mode, writes cannot
494
                                        // be in progress, so ...
495
                                        if (spif_data[31]) // Command an erase
496
                                        begin
497
                                                // Since we're not going back
498
                                                // to IDLE, we must stall the
499
                                                // bus here
500
                                                o_wb_stall <= 1'b1;
501
                                                spi_wr <= 1'b1;
502
                                                spi_len <= 2'b00;
503
                                                // Send a write enable command
504
                                                spi_in <= { 8'h06, 24'h00 };
505
                                                state <= `WBQSPI_ERASE_CMD;
506
                                        end end
507
                                2'b01: begin
508
                                        // Write the configuration register
509
                                        o_wb_ack <= spif_req;
510
                                        o_wb_stall <= 1'b1;
511
 
512
                                        // Need to send a write enable command first
513
                                        spi_wr <= 1'b1;
514
                                        spi_len <= 2'b00; // 8 bits
515
                                        // Send a write enable command
516
                                        spi_in <= { 8'h06, 24'h00 };
517
                                        state <= `WBQSPI_WRITE_CONFIG;
518
                                        end
519
                                2'b10: begin
520
                                        // Write the status register
521
                                        o_wb_ack <= spif_req; // Ack immediately
522
                                        o_wb_stall <= 1'b1; // Stall other cmds
523
                                        // Need to send a write enable command first
524
                                        spi_wr <= 1'b1;
525
                                        spi_len <= 2'b00; // 8 bits
526
                                        // Send a write enable command
527
                                        spi_in <= { 8'h06, 24'h00 };
528
                                        state <= `WBQSPI_WRITE_STATUS;
529
                                        end
530
                                2'b11: begin // Write the ID register??? makes no sense
531
                                        o_wb_ack <= spif_req;
532
                                        o_wb_stall <= 1'b0;
533
                                        state <= `WBQSPI_IDLE;
534
                                        end
535
                                endcase
536
                        end else begin // on (~spif_we)
537
                                case(spif_addr[1:0])
538
                                2'b00: begin // Read local register
539
                                        // Nonsense case--would've done this
540
                                        // already
541
                                        state <= `WBQSPI_IDLE;
542
                                        o_wb_ack <= spif_req;
543
                                        o_wb_stall <= 1'b0;
544
                                        end
545
                                2'b01: begin // Read configuration register
546
                                        state <= `WBQSPI_READ_CONFIG;
547
                                        spi_wr <= 1'b1;
548
                                        spi_len <= 2'b01;
549
                                        spi_in <= { 8'h35, 24'h00};
550
 
551
                                        o_wb_ack <= 1'b0;
552
                                        o_wb_stall <= 1'b1;
553
                                        end
554
                                2'b10: begin // Read status register
555
                                        state <= `WBQSPI_READ_STATUS;
556
                                        spi_wr <= 1'b1;
557
                                        spi_len <= 2'b01; // 8 bits out, 8 bits in
558
                                        spi_in <= { 8'h05, 24'h00};
559
 
560
                                        o_wb_ack <= 1'b0;
561
                                        o_wb_stall <= 1'b1;
562
                                        end
563
                                2'b11: begin // Read ID register
564
                                        state <= `WBQSPI_READ_ID_CMD;
565
                                        spi_wr <= 1'b1;
566
                                        spi_len <= 2'b00;
567
                                        spi_in <= { 8'h9f, 24'h00};
568
 
569
                                        o_wb_ack <= 1'b0;
570
                                        o_wb_stall <= 1'b1;
571
                                        end
572
                                endcase
573
                        end
574
                end
575
        end else if (state == `WBQSPI_WAIT_WIP_CLEAR)
576
        begin
577
                o_wb_stall <= 1'b1;
578
                o_wb_ack   <= 1'b0;
579
                spi_wr <= 1'b0;
580
                spif_req<= (spif_req) && (i_wb_cyc);
581
                if (~spi_busy)
582
                begin
583
                        spi_wr   <= 1'b1;
584
                        spi_in   <= { 8'h05, 24'h0000 };
585
                        spi_hold <= 1'b1;
586
                        spi_len  <= 2'b01; // 16 bits write, so we can read 8
587
                        state <= `WBQSPI_CHECK_WIP_CLEAR;
588
                        spi_spd  <= 1'b0; // Slow speed
589
                        spi_dir  <= 1'b0;
590
                end
591
        end else if (state == `WBQSPI_CHECK_WIP_CLEAR)
592
        begin
593
                o_wb_stall <= 1'b1;
594
                o_wb_ack   <= 1'b0;
595
                // Repeat as often as necessary until we are clear
596
                spi_wr <= 1'b1;
597
                spi_in <= 32'h0000; // Values here are actually irrelevant
598
                spi_hold <= 1'b1;
599
                spi_len <= 2'b00; // One byte at a time
600
                spi_spd  <= 1'b0; // Slow speed
601
                spi_dir  <= 1'b0;
602
                spif_req<= (spif_req) && (i_wb_cyc);
603
                if ((spi_valid)&&(~spi_out[0]))
604
                begin
605
                        state <= `WBQSPI_CHECK_WIP_DONE;
606
                        spi_wr   <= 1'b0;
607
                        spi_hold <= 1'b0;
608
                        write_in_progress <= 1'b0;
609
                        last_status <= spi_out[7:0];
610
                end
611
        end else if (state == `WBQSPI_CHECK_WIP_DONE)
612
        begin
613
                o_wb_stall <= 1'b1;
614
                o_wb_ack   <= 1'b0;
615
                // Let's let the SPI port come back to a full idle,
616
                // and the chip select line go low before continuing
617
                spi_wr   <= 1'b0;
618
                spi_len  <= 2'b00;
619
                spi_hold <= 1'b0;
620
                spi_spd  <= 1'b0; // Slow speed
621
                spi_dir  <= 1'b0;
622
                spif_req<= (spif_req) && (i_wb_cyc);
623
                if ((o_qspi_cs_n)&&(~spi_busy)) // Chip select line is high, we can continue
624
                begin
625
                        spi_wr   <= 1'b0;
626
                        spi_hold <= 1'b0;
627
 
628
                        casez({ spif_cmd, spif_ctrl, spif_addr[1:0] })
629
                        4'b00??: begin // Read data from ... somewhere
630
                                spi_wr     <= 1'b1;     // Write cmd to device
631
                                if (quad_mode_enabled)
632
                                begin
633
                                        spi_in <= { 8'heb, 2'b00, spif_addr[19:0], 2'b00 };
634
                                        state <= `WBQSPI_QRD_DUMMY;
635
                                end else begin
636
                                        spi_in <= { 8'h0b, 2'b00, spif_addr[19:0], 2'b00 };
637
                                        state <= `WBQSPI_RD_DUMMY;
638
                                end end
639
                        4'b10??: begin // Write data to ... anywhere
640
                                spi_wr <= 1'b1;
641
                                spi_len <= 2'b00; // 8 bits
642
                                // Send a write enable command
643
                                spi_in <= { 8'h06, 24'h00 };
644
                                state <= `WBQSPI_WEN;
645
                                end
646
                        4'b0110: begin // Read status register
647
                                state <= `WBQSPI_READ_STATUS;
648
                                spi_wr <= 1'b1;
649
                                spi_len <= 2'b01; // 8 bits out, 8 bits in
650
                                spi_in <= { 8'h05, 24'h00};
651
                                end
652
                        4'b0111: begin
653
                                state <= `WBQSPI_READ_ID_CMD;
654
                                spi_wr <= 1'b1;
655
                                spi_len <= 2'b00;
656
                                spi_in <= { 8'h9f, 24'h00};
657
                                end
658
                        default: begin //
659
                                o_wb_stall <= 1'b1;
660
                                o_wb_ack <= spif_req;
661
                                state <= `WBQSPI_WAIT_TIL_IDLE;
662
                                end
663
                        endcase
664
                // spif_cmd   <= i_wb_we;
665
                // spif_addr  <= i_wb_addr;
666
                // spif_data  <= i_wb_data;
667
                // spif_ctrl  <= (i_wb_ctrl_stb)&&(~i_wb_data_stb);
668
                // spi_wr <= 1'b0; // Keep the port idle, unless told otherwise
669
                end
670
        end else if (state == `WBQSPI_WEN)
671
        begin // We came here after issuing a write enable command
672
                spi_wr <= 1'b0;
673
                o_wb_ack <= 1'b0;
674
                o_wb_stall <= 1'b1;
675
                spif_req<= (spif_req) && (i_wb_cyc);
676
                if ((~spi_busy)&&(o_qspi_cs_n)&&(~spi_wr)) // Let's come to a full stop
677
                        // state <= (quad_mode_enabled)?`WBQSPI_QPP:`WBQSPI_PP;
678
                        state <= `WBQSPI_PP;
679
        end else if (state == `WBQSPI_PP)
680
        begin // We come here under a full stop / full port idle mode
681
                // Issue our command immediately
682
                spi_wr <= 1'b1;
683
                spi_in <= { 8'h02, 2'h0, spif_addr, 2'b00 };
684
                spi_len <= 2'b11;
685
                spi_hold <= 1'b1;
686
                spi_spd  <= 1'b0;
687
                spi_dir  <= 1'b0; // Writing
688
                spif_req<= (spif_req) && (i_wb_cyc);
689
 
690
                // Once we get busy, move on
691
                if (spi_busy)
692
                        state <= `WBQSPI_WR_DATA;
693
                if (spif_sector == erased_sector)
694
                        dirty_sector <= 1'b1;
695
        end else if (state == `WBQSPI_QPP)
696
        begin // We come here under a full stop / full port idle mode
697
                // Issue our command immediately
698
                spi_wr <= 1'b1;
699
                spi_in <= { 8'h32, 2'h0, spif_addr, 2'b00 };
700
                spi_len <= 2'b11;
701
                spi_hold <= 1'b1;
702
                spi_spd  <= 1'b0;
703
                spi_dir  <= 1'b0; // Writing
704
                spif_req<= (spif_req) && (i_wb_cyc);
705
 
706
                // Once we get busy, move on
707
                if (spi_busy)
708
                begin
709
                        // spi_wr is irrelevant here ...
710
                        // Set the speed value once, but wait til we get busy
711
                        // to do so.
712
                        spi_spd <= 1'b1;
713
                        state <= `WBQSPI_WR_DATA;
714
                end
715
                if (spif_sector == erased_sector)
716
                        dirty_sector <= 1'b1;
717
        end else if (state == `WBQSPI_WR_DATA)
718
        begin
719
                o_wb_stall <= 1'b1;
720
                o_wb_ack   <= 1'b0;
721
                spi_wr   <= 1'b1; // write without waiting
722
                spi_in   <= {
723
                        spif_data[ 7: 0],
724
                        spif_data[15: 8],
725
                        spif_data[23:16],
726
                        spif_data[31:24] };
727
                spi_len  <= 2'b11; // Write 4 bytes
728
                spi_hold <= 1'b1;
729
                if (~spi_busy)
730
                begin
731
                        o_wb_ack <= spif_req; // Ack when command given
732
                        state <= `WBQSPI_WR_BUS_CYCLE;
733
                end
734
                spif_req<= (spif_req) && (i_wb_cyc);
735
        end else if (state == `WBQSPI_WR_BUS_CYCLE)
736
        begin
737
                o_wb_ack <= 1'b0; // Turn off our ack and stall flags
738
                o_wb_stall <= 1'b1;
739
                spi_wr <= 1'b0;
740
                spi_hold <= 1'b1;
741
                write_in_progress <= 1'b1;
742
                spif_req<= (spif_req) && (i_wb_cyc);
743
                if (~i_wb_cyc)
744
                begin
745
                        state <= `WBQSPI_WAIT_TIL_IDLE;
746
                        spi_hold <= 1'b0;
747
                end else if (spi_wr)
748
                begin // Give the SPI a chance to get busy on the last write
749
                        // Do nothing here.
750
                end else if ((i_wb_data_stb)&&(i_wb_we)
751
                                &&(i_wb_addr == (spif_addr+1))
752
                                &&(i_wb_addr[19:6]==spif_addr[19:6]))
753
                begin
754
                        spif_cmd  <= 1'b1;
755
                        spif_data <= i_wb_data;
756
                        spif_addr <= i_wb_addr;
757
                        spif_ctrl  <= 1'b0;
758
                        spif_req<= 1'b1;
759
                        // We'll keep the bus stalled on this request
760
                        // for a while
761
                        state <= `WBQSPI_WR_DATA;
762
                        o_wb_ack   <= 1'b0;
763
                        o_wb_stall <= 1'b0;
764
                end else if ((i_wb_data_stb|i_wb_ctrl_stb)&&(~o_wb_ack)) // Writing out of bounds
765
                begin
766
                        spi_hold <= 1'b0;
767
                        spi_wr   <= 1'b0;
768
                        state <= `WBQSPI_WAIT_TIL_IDLE;
769
                end // Otherwise we stay here
770
        end else if (state == `WBQSPI_RD_DUMMY)
771
        begin
772
                o_wb_ack   <= 1'b0;
773
                o_wb_stall <= 1'b1;
774
 
775
                spi_wr <= 1'b1; // Non-stop
776
                // Need to read one byte of dummy data,
777
                // just to consume 8 clocks
778
                spi_in <= { 8'h00, 24'h00 };
779
                spi_len <= 2'b00; // Read 8 bits
780
                spi_spd <= 1'b0;
781
                spi_hold <= 1'b0;
782
                spif_req<= (spif_req) && (i_wb_cyc);
783
 
784
                if ((~spi_busy)&&(~o_qspi_cs_n))
785
                        // Our command was accepted
786
                        state <= `WBQSPI_READ_CMD;
787
        end else if (state == `WBQSPI_QRD_ADDRESS)
788
        begin
789
                // We come in here immediately upon issuing a QRD read
790
                // command (8-bits), but we have to pause to give the
791
                // address (24-bits) and mode (8-bits) in quad speed.
792
                o_wb_ack   <= 1'b0;
793
                o_wb_stall <= 1'b1;
794
 
795
                spi_wr <= 1'b1; // Non-stop
796
                spi_in <= { 2'b0, spif_addr, 2'b0, 8'ha0 };
797
                spi_len <= 2'b11; // Write all 32 bits bits
798
                spi_spd <= 1'b1;
799
                spi_dir <= 1'b0; // Still writing
800
                spi_hold <= 1'b0;
801
                spif_req<= (spif_req) && (i_wb_cyc);
802
 
803
                if ((~spi_busy)&&(spi_spd))
804
                        // Our command was accepted
805
                        state <= `WBQSPI_QRD_DUMMY;
806
        end else if (state == `WBQSPI_QRD_DUMMY)
807
        begin
808
                o_wb_ack   <= 1'b0;
809
                o_wb_stall <= 1'b1;
810
 
811
                spi_wr <= 1'b1; // Non-stop
812
                spi_in <= { 8'ha0, 24'h00 }; // Mode byte, then 2 bytes dummy
813
                spi_len <= 2'b10; // Write 8 bits
814
                spi_spd <= 1'b1;
815
                spi_dir <= 1'b0; // Still writing
816
                spi_hold <= 1'b0;
817
                spif_req<= (spif_req) && (i_wb_cyc);
818
 
819
                if ((~spi_busy)&&(spi_in[31:28] == 4'ha))
820
                        // Our command was accepted
821
                        state <= `WBQSPI_READ_CMD;
822
        end else if (state == `WBQSPI_READ_CMD)
823
        begin // Issue our first command to read 32 bits.
824
                o_wb_ack   <= 1'b0;
825
                o_wb_stall <= 1'b1;
826
 
827
                spi_wr <= 1'b1;
828
                spi_in <= { 8'hff, 24'h00 }; // Empty
829
                spi_len <= 2'b11; // Read 32 bits
830
                spi_dir <= 1'b1; // Now reading
831
                spi_hold <= 1'b0;
832
                spif_req<= (spif_req) && (i_wb_cyc);
833
                if ((spi_valid)&&(spi_len == 2'b11))
834
                        state <= `WBQSPI_READ_DATA;
835
        end else if (state == `WBQSPI_READ_DATA)
836
        begin
837
                // Pipelined read support
838
                spi_wr <=((i_wb_cyc)&&(i_wb_data_stb)&&(~i_wb_we)&&(i_wb_addr== (spif_addr+1)));
839
                // spi_wr <= 1'b0; // No pipelined read support
840
                spi_in <= 32'h00;
841
                spi_len <= 2'b11;
842
                // Don't adjust the speed here, it was set in the setup
843
                spi_dir <= 1'b1; // Now we get to read
844
                spi_hold <= 1'b0;
845
                spif_req<= (spif_req) && (i_wb_cyc);
846
                if ((spi_valid)&&(~spi_in[31]))
847
                begin // Single pulse acknowledge and write data out
848
                        o_wb_ack <= spif_req;
849
                        o_wb_stall <= (~spi_wr);
850
                        // adjust endian-ness to match the PC
851
                        o_wb_data <= { spi_out[7:0], spi_out[15:8],
852
                                spi_out[23:16], spi_out[31:24] };
853
                        state <= (spi_wr)?`WBQSPI_READ_DATA
854
                                : ((spi_spd) ? `WBQSPI_WAIT_TIL_RDIDLE : `WBQSPI_WAIT_TIL_IDLE);
855
                        spif_req <= spi_wr;
856
                        if (spi_wr)
857
                                spif_addr <= i_wb_addr;
858
                end else begin
859
                        o_wb_ack <= 1'b0;
860
                        o_wb_stall <= 1'b1;
861
                end
862
        end else if (state == `WBQSPI_WAIT_TIL_RDIDLE)
863
        begin // Wait 'til idle, but then go to fast read idle instead of full
864
                spi_wr     <= 1'b0;     // idle
865
                spi_hold   <= 1'b0;
866
                o_wb_stall <= 1'b1;
867
                o_wb_ack   <= 1'b0;
868
                spif_req   <= 1'b0;
869
                if ((~spi_busy)&&(o_qspi_cs_n)&&(~spi_wr)) // Wait for a full
870
                begin // clearing of the SPI port before moving on
871
                        state <= `WBQSPI_RDIDLE;
872
                        o_wb_stall <= 1'b0;
873
                        o_wb_ack   <= 1'b0;// Shouldn't be acking anything here
874
                end
875
        end else if (state == `WBQSPI_READ_ID_CMD)
876
        begin // We came into here immediately after issuing a 0x9f command
877
                // Now we need to read 32 bits of data.  Result should be
878
                // 0x0102154d (8'h manufacture ID, 16'h device ID, followed
879
                // by the number of extended bytes available 8'h4d).
880
                o_wb_ack <= 1'b0;
881
                o_wb_stall<= 1'b1;
882
 
883
                spi_wr <= 1'b1; // No data to send, but need four bytes, since
884
                spi_len <= 2'b11; // 32 bits of data are ... useful
885
                spi_in <= 32'h00; // Irrelevant
886
                spi_spd <= 1'b0; // Slow speed
887
                spi_dir <= 1'b1; // Reading
888
                spi_hold <= 1'b0;
889
                spif_req <= (spif_req) && (i_wb_cyc);
890
                if ((~spi_busy)&&(~o_qspi_cs_n)&&(spi_len == 2'b11))
891
                        // Our command was accepted, now go read the result
892
                        state <= `WBQSPI_READ_ID;
893
        end else if (state == `WBQSPI_READ_ID)
894
        begin
895
                o_wb_ack <= 1'b0; // Assuming we're still waiting
896
                o_wb_stall <= 1'b1;
897
 
898
                spi_wr <= 1'b0; // No more writes, we'ev already written the cmd
899
                spi_hold <= 1'b0;
900
                spif_req <= (spif_req) && (i_wb_cyc);
901
 
902
                // Here, we just wait until the result comes back
903
                // The problem is, the result may be the previous result.
904
                // So we use spi_len as an indicator
905
                spi_len <= 2'b00;
906
                if((spi_valid)&&(spi_len==2'b00))
907
                begin // Put the results out as soon as possible
908
                        o_wb_data <= spi_out[31:0];
909
                        o_wb_ack <= spif_req;
910
                        spif_req <= 1'b0;
911
                end else if ((~spi_busy)&&(~o_qspi_cs_n))
912
                begin
913
                        state <= `WBQSPI_IDLE;
914
                        o_wb_stall <= 1'b0;
915
                end
916
        end else if (state == `WBQSPI_READ_STATUS)
917
        begin // We enter after the command has been given, for now just
918
                // read and return
919
                spi_wr <= 1'b0;
920
                o_wb_ack <= 1'b0;
921
                spi_hold <= 1'b0;
922
                spif_req <= (spif_req) && (i_wb_cyc);
923
                if (spi_valid)
924
                begin
925
                        o_wb_ack <= spif_req;
926
                        o_wb_stall <= 1'b1;
927
                        spif_req <= 1'b0;
928
                        last_status <= spi_out[7:0];
929
                        write_in_progress <= spi_out[0];
930
                        if (spif_addr[1:0] == 2'b00) // Local read, checking
931
                        begin // status, 'cause we're writing
932
                                o_wb_data <= { spi_out[0],
933
                                        dirty_sector, spi_busy,
934
                                        ~write_protect,
935
                                        quad_mode_enabled,
936
                                        7'h00,
937
                                        erased_sector, 14'h000 };
938
                        end else begin
939
                                o_wb_data <= { 24'h00, spi_out[7:0] };
940
                        end
941
                end
942
 
943
                if ((~spi_busy)&&(~spi_wr))
944
                        state <= `WBQSPI_IDLE;
945
        end else if (state == `WBQSPI_READ_CONFIG)
946
        begin // We enter after the command has been given, for now just
947
                // read and return
948
                spi_wr <= 1'b0;
949
                o_wb_ack <= 1'b0;
950
                o_wb_stall <= 1'b1;
951
                spi_hold <= 1'b0;
952
                spif_req <= (spif_req) && (i_wb_cyc);
953
 
954
                if (spi_valid)
955
                begin
956
                        o_wb_data <= { 24'h00, spi_out[7:0] };
957
                        quad_mode_enabled <= spi_out[1];
958
                end
959
 
960
                if ((~spi_busy)&&(~spi_wr))
961
                begin
962
                        state <= `WBQSPI_IDLE;
963
                        o_wb_ack   <= spif_req;
964
                        o_wb_stall <= 1'b0;
965
                        spif_req <= 1'b0;
966
                end
967
        end else if (state == `WBQSPI_WRITE_CONFIG)
968
        begin // We enter immediately after commanding a WEN
969
                o_wb_ack   <= 1'b0;
970
                o_wb_stall <= 1'b1;
971
 
972
                spi_len <= 2'b10;
973
                spi_in <= { 8'h01, last_status, spif_data[7:0], 8'h00 };
974
                spi_wr <= 1'b0;
975
                spi_hold <= 1'b0;
976
                spif_req <= (spif_req) && (i_wb_cyc);
977
                if ((~spi_busy)&&(~spi_wr))
978
                begin
979
                        spi_wr <= 1'b1;
980
                        state <= `WBQSPI_WAIT_TIL_IDLE;
981
                        write_in_progress <= 1'b1;
982
                        quad_mode_enabled <= spif_data[1];
983
                end
984
        end else if (state == `WBQSPI_WRITE_STATUS)
985
        begin // We enter immediately after commanding a WEN
986
                o_wb_ack   <= 1'b0;
987
                o_wb_stall <= 1'b1;
988
 
989
                spi_len <= 2'b01;
990
                spi_in <= { 8'h01, spif_data[7:0], 16'h00 };
991
                // last_status <= i_wb_data[7:0]; // We'll read this in a moment
992
                spi_wr <= 1'b0;
993
                spi_hold <= 1'b0;
994
                spif_req <= (spif_req) && (i_wb_cyc);
995
                if ((~spi_busy)&&(~spi_wr))
996
                begin
997
                        spi_wr <= 1'b1;
998
                        last_status <= spif_data[7:0];
999
                        write_in_progress <= 1'b1;
1000
                        if(((last_status[6])||(last_status[5]))
1001
                                &&((~spif_data[6])&&(~spif_data[5])))
1002
                                state <= `WBQSPI_CLEAR_STATUS;
1003
                        else
1004
                                state <= `WBQSPI_WAIT_TIL_IDLE;
1005
                end
1006
        end else if (state == `WBQSPI_ERASE_CMD)
1007
        begin // Know that WIP is clear on entry, WEN has just been commanded
1008
                spi_wr     <= 1'b0;
1009
                o_wb_ack   <= 1'b0;
1010
                o_wb_stall <= 1'b1;
1011
                spi_hold   <= 1'b0;
1012
                spi_spd <= 1'b0;
1013
                spi_dir <= 1'b0;
1014
                spif_req <= (spif_req) && (i_wb_cyc);
1015
 
1016
                // Here's the erase command
1017
                spi_in <= { 8'hd8, 2'h0, spif_data[19:14], 14'h000, 2'b00 };
1018
                spi_len <= 2'b11; // 32 bit write
1019
                // together with setting our copy of the WIP bit
1020
                write_in_progress <= 1'b1;
1021
                // keeping track of which sector we just erased
1022
                erased_sector <= spif_data[19:14];
1023
                // and marking this erase sector as no longer dirty
1024
                dirty_sector <= 1'b0;
1025
 
1026
                // Wait for a full stop before issuing this command
1027
                if ((~spi_busy)&&(~spi_wr)&&(o_qspi_cs_n))
1028
                begin // When our command is accepted, move to the next state
1029
                        spi_wr <= 1'b1;
1030
                        state <= `WBQSPI_ERASE_BLOCK;
1031
                end
1032
        end else if (state == `WBQSPI_ERASE_BLOCK)
1033
        begin
1034
                spi_wr     <= 1'b0;
1035
                spi_hold   <= 1'b0;
1036
                o_wb_stall <= 1'b1;
1037
                o_wb_ack   <= 1'b0;
1038
                spif_req <= (spif_req) && (i_wb_cyc);
1039
                // When the port clears, we can head back to idle
1040
                if ((~spi_busy)&&(~spi_wr))
1041
                begin
1042
                        o_wb_ack <= spif_req;
1043
                        state <= `WBQSPI_IDLE;
1044
                end
1045
        end else if (state == `WBQSPI_CLEAR_STATUS)
1046
        begin // Issue a clear status command
1047
                spi_wr <= 1'b1;
1048
                spi_hold <= 1'b0;
1049
                spi_len <= 2'b00; // 8 bit command
1050
                spi_in <= { 8'h30, 24'h00 };
1051
                spi_spd <= 1'b0;
1052
                spi_dir <= 1'b0;
1053
                last_status[6:5] <= 2'b00;
1054
                spif_req <= (spif_req) && (i_wb_cyc);
1055
                if ((spi_wr)&&(~spi_busy))
1056
                        state <= `WBQSPI_WAIT_TIL_IDLE;
1057
        end else if (state == `WBQSPI_IDLE_CHECK_WIP)
1058
        begin // We are now in read status register mode
1059
 
1060
                // No bus commands have (yet) been given
1061
                o_wb_stall <= 1'b1;
1062
                o_wb_ack   <= 1'b0;
1063
                spif_req <= (spif_req) && (i_wb_cyc);
1064
 
1065
                // Stay in this mode unless/until we get a command, or
1066
                //      the write is over
1067
                spi_wr <= (((~i_wb_cyc)||((~i_wb_data_stb)&&(~i_wb_ctrl_stb)))
1068
                                &&(write_in_progress));
1069
                spi_len <= 2'b00; // 8 bit reads
1070
                spi_spd <= 1'b0;  // SPI, not quad
1071
                spi_dir <= 1'b1;  // Read
1072
                if (spi_valid)
1073
                begin
1074
                        write_in_progress <= spi_out[0];
1075
                        if ((~spi_out[0])&&(write_in_progress))
1076
                                o_interrupt <= 1'b1;
1077
                end else
1078
                        o_interrupt <= 1'b0;
1079
 
1080
                if ((~spi_wr)&&(~spi_busy)&&(o_qspi_cs_n))
1081
                begin // We can now go to idle and process a command
1082
                        o_wb_stall <= 1'b0;
1083
                        o_wb_ack   <= 1'b0;
1084
                        state <= `WBQSPI_IDLE;
1085
                end
1086
        end else // if (state == `WBQSPI_WAIT_TIL_IDLE) or anything else
1087
        begin
1088
                spi_wr     <= 1'b0;
1089
                spi_hold   <= 1'b0;
1090
                o_wb_stall <= 1'b1;
1091
                o_wb_ack   <= 1'b0;
1092
                spif_req   <= 1'b0;
1093
                if ((~spi_busy)&&(o_qspi_cs_n)&&(~spi_wr)) // Wait for a full
1094
                begin // clearing of the SPI port before moving on
1095
                        state <= `WBQSPI_IDLE;
1096
                        o_wb_stall <= 1'b0;
1097
                        o_wb_ack   <= 1'b0; // Shouldn't be acking anything here
1098
                end
1099
        end
1100
 
1101
endmodule

powered by: WebSVN 2.1.0

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