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

Subversion Repositories s6soc

[/] [s6soc/] [trunk/] [rtl/] [wbqspiflashp.v] - Blame information for rev 46

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

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

powered by: WebSVN 2.1.0

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